Out-of-tree Build Error fix
[vpp.git] / src / vnet / ip / ip6_forward.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ip/ip6_forward.c: IP v6 forwarding
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/ethernet/ethernet.h>     /* for ethernet_header_t */
43 #include <vnet/srp/srp.h>       /* for srp_hw_interface_class */
44 #include <vppinfra/cache.h>
45 #include <vnet/fib/fib_urpf_list.h>     /* for FIB uRPF check */
46 #include <vnet/fib/ip6_fib.h>
47 #include <vnet/mfib/ip6_mfib.h>
48 #include <vnet/dpo/load_balance.h>
49 #include <vnet/dpo/classify_dpo.h>
50
51 #include <vppinfra/bihash_template.c>
52
53 /* Flag used by IOAM code. Classifier sets it pop-hop-by-hop checks it */
54 #define OI_DECAP   0x80000000
55
56 /**
57  * @file
58  * @brief IPv6 Forwarding.
59  *
60  * This file contains the source code for IPv6 forwarding.
61  */
62
63 void
64 ip6_forward_next_trace (vlib_main_t * vm,
65                         vlib_node_runtime_t * node,
66                         vlib_frame_t * frame,
67                         vlib_rx_or_tx_t which_adj_index);
68
69 always_inline uword
70 ip6_lookup_inline (vlib_main_t * vm,
71                    vlib_node_runtime_t * node, vlib_frame_t * frame)
72 {
73   ip6_main_t *im = &ip6_main;
74   vlib_combined_counter_main_t *cm = &load_balance_main.lbm_to_counters;
75   u32 n_left_from, n_left_to_next, *from, *to_next;
76   ip_lookup_next_t next;
77   u32 cpu_index = os_get_cpu_number ();
78
79   from = vlib_frame_vector_args (frame);
80   n_left_from = frame->n_vectors;
81   next = node->cached_next_index;
82
83   while (n_left_from > 0)
84     {
85       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
86
87       while (n_left_from >= 4 && n_left_to_next >= 2)
88         {
89           vlib_buffer_t *p0, *p1;
90           u32 pi0, pi1, lbi0, lbi1, wrong_next;
91           ip_lookup_next_t next0, next1;
92           ip6_header_t *ip0, *ip1;
93           ip6_address_t *dst_addr0, *dst_addr1;
94           u32 fib_index0, fib_index1;
95           u32 flow_hash_config0, flow_hash_config1;
96           const dpo_id_t *dpo0, *dpo1;
97           const load_balance_t *lb0, *lb1;
98
99           /* Prefetch next iteration. */
100           {
101             vlib_buffer_t *p2, *p3;
102
103             p2 = vlib_get_buffer (vm, from[2]);
104             p3 = vlib_get_buffer (vm, from[3]);
105
106             vlib_prefetch_buffer_header (p2, LOAD);
107             vlib_prefetch_buffer_header (p3, LOAD);
108             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
109             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
110           }
111
112           pi0 = to_next[0] = from[0];
113           pi1 = to_next[1] = from[1];
114
115           p0 = vlib_get_buffer (vm, pi0);
116           p1 = vlib_get_buffer (vm, pi1);
117
118           ip0 = vlib_buffer_get_current (p0);
119           ip1 = vlib_buffer_get_current (p1);
120
121           dst_addr0 = &ip0->dst_address;
122           dst_addr1 = &ip1->dst_address;
123
124           fib_index0 =
125             vec_elt (im->fib_index_by_sw_if_index,
126                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
127           fib_index1 =
128             vec_elt (im->fib_index_by_sw_if_index,
129                      vnet_buffer (p1)->sw_if_index[VLIB_RX]);
130
131           fib_index0 = (vnet_buffer (p0)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
132             fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
133           fib_index1 = (vnet_buffer (p1)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
134             fib_index1 : vnet_buffer (p1)->sw_if_index[VLIB_TX];
135
136           lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
137           lbi1 = ip6_fib_table_fwding_lookup (im, fib_index1, dst_addr1);
138
139           lb0 = load_balance_get (lbi0);
140           lb1 = load_balance_get (lbi1);
141
142           vnet_buffer (p0)->ip.flow_hash = vnet_buffer (p1)->ip.flow_hash = 0;
143
144           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
145             {
146               flow_hash_config0 = lb0->lb_hash_config;
147               vnet_buffer (p0)->ip.flow_hash =
148                 ip6_compute_flow_hash (ip0, flow_hash_config0);
149             }
150           if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
151             {
152               flow_hash_config1 = lb1->lb_hash_config;
153               vnet_buffer (p1)->ip.flow_hash =
154                 ip6_compute_flow_hash (ip1, flow_hash_config1);
155             }
156
157           ASSERT (lb0->lb_n_buckets > 0);
158           ASSERT (lb1->lb_n_buckets > 0);
159           ASSERT (is_pow2 (lb0->lb_n_buckets));
160           ASSERT (is_pow2 (lb1->lb_n_buckets));
161           dpo0 = load_balance_get_bucket_i (lb0,
162                                             (vnet_buffer (p0)->ip.flow_hash &
163                                              lb0->lb_n_buckets_minus_1));
164           dpo1 = load_balance_get_bucket_i (lb1,
165                                             (vnet_buffer (p1)->ip.flow_hash &
166                                              lb1->lb_n_buckets_minus_1));
167
168           next0 = dpo0->dpoi_next_node;
169           next1 = dpo1->dpoi_next_node;
170
171           /* Only process the HBH Option Header if explicitly configured to do so */
172           if (PREDICT_FALSE
173               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
174             {
175               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
176                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
177             }
178           if (PREDICT_FALSE
179               (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
180             {
181               next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
182                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
183             }
184           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
185           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
186
187           vlib_increment_combined_counter
188             (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
189           vlib_increment_combined_counter
190             (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
191
192           from += 2;
193           to_next += 2;
194           n_left_to_next -= 2;
195           n_left_from -= 2;
196
197           wrong_next = (next0 != next) + 2 * (next1 != next);
198           if (PREDICT_FALSE (wrong_next != 0))
199             {
200               switch (wrong_next)
201                 {
202                 case 1:
203                   /* A B A */
204                   to_next[-2] = pi1;
205                   to_next -= 1;
206                   n_left_to_next += 1;
207                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
208                   break;
209
210                 case 2:
211                   /* A A B */
212                   to_next -= 1;
213                   n_left_to_next += 1;
214                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
215                   break;
216
217                 case 3:
218                   /* A B C */
219                   to_next -= 2;
220                   n_left_to_next += 2;
221                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
222                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
223                   if (next0 == next1)
224                     {
225                       /* A B B */
226                       vlib_put_next_frame (vm, node, next, n_left_to_next);
227                       next = next1;
228                       vlib_get_next_frame (vm, node, next, to_next,
229                                            n_left_to_next);
230                     }
231                 }
232             }
233         }
234
235       while (n_left_from > 0 && n_left_to_next > 0)
236         {
237           vlib_buffer_t *p0;
238           ip6_header_t *ip0;
239           u32 pi0, lbi0;
240           ip_lookup_next_t next0;
241           load_balance_t *lb0;
242           ip6_address_t *dst_addr0;
243           u32 fib_index0, flow_hash_config0;
244           const dpo_id_t *dpo0;
245
246           pi0 = from[0];
247           to_next[0] = pi0;
248
249           p0 = vlib_get_buffer (vm, pi0);
250
251           ip0 = vlib_buffer_get_current (p0);
252
253           dst_addr0 = &ip0->dst_address;
254
255           fib_index0 =
256             vec_elt (im->fib_index_by_sw_if_index,
257                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
258           fib_index0 =
259             (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
260              (u32) ~ 0) ? fib_index0 : vnet_buffer (p0)->sw_if_index[VLIB_TX];
261
262           flow_hash_config0 = ip6_fib_get (fib_index0)->flow_hash_config;
263
264           lbi0 = ip6_fib_table_fwding_lookup (im, fib_index0, dst_addr0);
265
266           lb0 = load_balance_get (lbi0);
267
268           vnet_buffer (p0)->ip.flow_hash = 0;
269
270           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
271             {
272               flow_hash_config0 = lb0->lb_hash_config;
273               vnet_buffer (p0)->ip.flow_hash =
274                 ip6_compute_flow_hash (ip0, flow_hash_config0);
275             }
276
277           ASSERT (lb0->lb_n_buckets > 0);
278           ASSERT (is_pow2 (lb0->lb_n_buckets));
279           dpo0 = load_balance_get_bucket_i (lb0,
280                                             (vnet_buffer (p0)->ip.flow_hash &
281                                              lb0->lb_n_buckets_minus_1));
282           next0 = dpo0->dpoi_next_node;
283
284           /* Only process the HBH Option Header if explicitly configured to do so */
285           if (PREDICT_FALSE
286               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
287             {
288               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
289                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
290             }
291           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
292
293           vlib_increment_combined_counter
294             (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
295
296           from += 1;
297           to_next += 1;
298           n_left_to_next -= 1;
299           n_left_from -= 1;
300
301           if (PREDICT_FALSE (next0 != next))
302             {
303               n_left_to_next += 1;
304               vlib_put_next_frame (vm, node, next, n_left_to_next);
305               next = next0;
306               vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
307               to_next[0] = pi0;
308               to_next += 1;
309               n_left_to_next -= 1;
310             }
311         }
312
313       vlib_put_next_frame (vm, node, next, n_left_to_next);
314     }
315
316   if (node->flags & VLIB_NODE_FLAG_TRACE)
317     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
318
319   return frame->n_vectors;
320 }
321
322 static void
323 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
324                           ip6_main_t * im, u32 fib_index,
325                           ip_interface_address_t * a)
326 {
327   ip_lookup_main_t *lm = &im->lookup_main;
328   ip6_address_t *address = ip_interface_address_get_address (lm, a);
329   fib_prefix_t pfx = {
330     .fp_len = a->address_length,
331     .fp_proto = FIB_PROTOCOL_IP6,
332     .fp_addr.ip6 = *address,
333   };
334
335   a->neighbor_probe_adj_index = ~0;
336   if (a->address_length < 128)
337     {
338       fib_node_index_t fei;
339
340       fei = fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_ATTACHED), FIB_PROTOCOL_IP6, NULL,       /* No next-hop address */
341                                              sw_if_index, ~0,   // invalid FIB index
342                                              1, NULL,   // no label stack
343                                              FIB_ROUTE_PATH_FLAG_NONE);
344       a->neighbor_probe_adj_index = fib_entry_get_adj (fei);
345     }
346
347   pfx.fp_len = 128;
348   if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
349     {
350       u32 classify_table_index =
351         lm->classify_table_index_by_sw_if_index[sw_if_index];
352       if (classify_table_index != (u32) ~ 0)
353         {
354           dpo_id_t dpo = DPO_INVALID;
355
356           dpo_set (&dpo,
357                    DPO_CLASSIFY,
358                    DPO_PROTO_IP6,
359                    classify_dpo_create (DPO_PROTO_IP6, classify_table_index));
360
361           fib_table_entry_special_dpo_add (fib_index,
362                                            &pfx,
363                                            FIB_SOURCE_CLASSIFY,
364                                            FIB_ENTRY_FLAG_NONE, &dpo);
365           dpo_reset (&dpo);
366         }
367     }
368
369   fib_table_entry_update_one_path (fib_index, &pfx, FIB_SOURCE_INTERFACE, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL), FIB_PROTOCOL_IP6, &pfx.fp_addr, sw_if_index, ~0,   // invalid FIB index
370                                    1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
371 }
372
373 static void
374 ip6_del_interface_routes (ip6_main_t * im,
375                           u32 fib_index,
376                           ip6_address_t * address, u32 address_length)
377 {
378   fib_prefix_t pfx = {
379     .fp_len = address_length,
380     .fp_proto = FIB_PROTOCOL_IP6,
381     .fp_addr.ip6 = *address,
382   };
383
384   if (pfx.fp_len < 128)
385     {
386       fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
387
388     }
389
390   pfx.fp_len = 128;
391   fib_table_entry_delete (fib_index, &pfx, FIB_SOURCE_INTERFACE);
392 }
393
394 void
395 ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable)
396 {
397   ip6_main_t *im = &ip6_main;
398
399   vec_validate_init_empty (im->ip_enabled_by_sw_if_index, sw_if_index, 0);
400
401   /*
402    * enable/disable only on the 1<->0 transition
403    */
404   if (is_enable)
405     {
406       if (1 != ++im->ip_enabled_by_sw_if_index[sw_if_index])
407         return;
408     }
409   else
410     {
411       /* The ref count is 0 when an address is removed from an interface that has
412        * no address - this is not a ciritical error */
413       if (0 == im->ip_enabled_by_sw_if_index[sw_if_index] ||
414           0 != --im->ip_enabled_by_sw_if_index[sw_if_index])
415         return;
416     }
417
418   if (sw_if_index != 0)
419     ip6_mfib_interface_enable_disable (sw_if_index, is_enable);
420
421   vnet_feature_enable_disable ("ip6-unicast", "ip6-lookup", sw_if_index,
422                                is_enable, 0, 0);
423
424   vnet_feature_enable_disable ("ip6-multicast", "ip6-mfib-forward-lookup",
425                                sw_if_index, is_enable, 0, 0);
426
427 }
428
429 /* get first interface address */
430 ip6_address_t *
431 ip6_interface_first_address (ip6_main_t * im,
432                              u32 sw_if_index,
433                              ip_interface_address_t ** result_ia)
434 {
435   ip_lookup_main_t *lm = &im->lookup_main;
436   ip_interface_address_t *ia = 0;
437   ip6_address_t *result = 0;
438
439   /* *INDENT-OFF* */
440   foreach_ip_interface_address (lm, ia, sw_if_index,
441                                 1 /* honor unnumbered */,
442   ({
443     ip6_address_t * a = ip_interface_address_get_address (lm, ia);
444     result = a;
445     break;
446   }));
447   /* *INDENT-ON* */
448   if (result_ia)
449     *result_ia = result ? ia : 0;
450   return result;
451 }
452
453 clib_error_t *
454 ip6_add_del_interface_address (vlib_main_t * vm,
455                                u32 sw_if_index,
456                                ip6_address_t * address,
457                                u32 address_length, u32 is_del)
458 {
459   vnet_main_t *vnm = vnet_get_main ();
460   ip6_main_t *im = &ip6_main;
461   ip_lookup_main_t *lm = &im->lookup_main;
462   clib_error_t *error;
463   u32 if_address_index;
464   ip6_address_fib_t ip6_af, *addr_fib = 0;
465
466   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
467   vec_validate (im->mfib_index_by_sw_if_index, sw_if_index);
468
469   ip6_addr_fib_init (&ip6_af, address,
470                      vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
471   vec_add1 (addr_fib, ip6_af);
472
473   {
474     uword elts_before = pool_elts (lm->if_address_pool);
475
476     error = ip_interface_address_add_del
477       (lm, sw_if_index, addr_fib, address_length, is_del, &if_address_index);
478     if (error)
479       goto done;
480
481     /* Pool did not grow: add duplicate address. */
482     if (elts_before == pool_elts (lm->if_address_pool))
483       goto done;
484   }
485
486   ip6_sw_interface_enable_disable (sw_if_index, !is_del);
487
488   if (is_del)
489     ip6_del_interface_routes (im, ip6_af.fib_index, address, address_length);
490   else
491     ip6_add_interface_routes (vnm, sw_if_index,
492                               im, ip6_af.fib_index,
493                               pool_elt_at_index (lm->if_address_pool,
494                                                  if_address_index));
495
496   {
497     ip6_add_del_interface_address_callback_t *cb;
498     vec_foreach (cb, im->add_del_interface_address_callbacks)
499       cb->function (im, cb->function_opaque, sw_if_index,
500                     address, address_length, if_address_index, is_del);
501   }
502
503 done:
504   vec_free (addr_fib);
505   return error;
506 }
507
508 clib_error_t *
509 ip6_sw_interface_admin_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
510 {
511   ip6_main_t *im = &ip6_main;
512   ip_interface_address_t *ia;
513   ip6_address_t *a;
514   u32 is_admin_up, fib_index;
515
516   /* Fill in lookup tables with default table (0). */
517   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
518
519   vec_validate_init_empty (im->
520                            lookup_main.if_address_pool_index_by_sw_if_index,
521                            sw_if_index, ~0);
522
523   is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
524
525   fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
526
527   /* *INDENT-OFF* */
528   foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index,
529                                 0 /* honor unnumbered */,
530   ({
531     a = ip_interface_address_get_address (&im->lookup_main, ia);
532     if (is_admin_up)
533       ip6_add_interface_routes (vnm, sw_if_index,
534                                 im, fib_index,
535                                 ia);
536     else
537       ip6_del_interface_routes (im, fib_index,
538                                 a, ia->address_length);
539   }));
540   /* *INDENT-ON* */
541
542   return 0;
543 }
544
545 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
546
547 /* Built-in ip6 unicast rx feature path definition */
548 /* *INDENT-OFF* */
549 VNET_FEATURE_ARC_INIT (ip6_unicast, static) =
550 {
551   .arc_name  = "ip6-unicast",
552   .start_nodes = VNET_FEATURES ("ip6-input"),
553   .arc_index_ptr = &ip6_main.lookup_main.ucast_feature_arc_index,
554 };
555
556 VNET_FEATURE_INIT (ip6_flow_classify, static) =
557 {
558   .arc_name = "ip6-unicast",
559   .node_name = "ip6-flow-classify",
560   .runs_before = VNET_FEATURES ("ip6-inacl"),
561 };
562
563 VNET_FEATURE_INIT (ip6_inacl, static) =
564 {
565   .arc_name = "ip6-unicast",
566   .node_name = "ip6-inacl",
567   .runs_before = VNET_FEATURES ("ip6-policer-classify"),
568 };
569
570 VNET_FEATURE_INIT (ip6_policer_classify, static) =
571 {
572   .arc_name = "ip6-unicast",
573   .node_name = "ip6-policer-classify",
574   .runs_before = VNET_FEATURES ("ipsec-input-ip6"),
575 };
576
577 VNET_FEATURE_INIT (ip6_ipsec, static) =
578 {
579   .arc_name = "ip6-unicast",
580   .node_name = "ipsec-input-ip6",
581   .runs_before = VNET_FEATURES ("l2tp-decap"),
582 };
583
584 VNET_FEATURE_INIT (ip6_l2tp, static) =
585 {
586   .arc_name = "ip6-unicast",
587   .node_name = "l2tp-decap",
588   .runs_before = VNET_FEATURES ("vpath-input-ip6"),
589 };
590
591 VNET_FEATURE_INIT (ip6_vpath, static) =
592 {
593   .arc_name = "ip6-unicast",
594   .node_name = "vpath-input-ip6",
595   .runs_before = VNET_FEATURES ("ip6-vxlan-bypass"),
596 };
597
598 VNET_FEATURE_INIT (ip6_vxlan_bypass, static) =
599 {
600   .arc_name = "ip6-unicast",
601   .node_name = "ip6-vxlan-bypass",
602   .runs_before = VNET_FEATURES ("ip6-lookup"),
603 };
604
605 VNET_FEATURE_INIT (ip6_lookup, static) =
606 {
607   .arc_name = "ip6-unicast",
608   .node_name = "ip6-lookup",
609   .runs_before = VNET_FEATURES ("ip6-drop"),
610 };
611
612 VNET_FEATURE_INIT (ip6_drop, static) =
613 {
614   .arc_name = "ip6-unicast",
615   .node_name = "ip6-drop",
616   .runs_before = 0,  /*last feature*/
617 };
618
619 /* Built-in ip6 multicast rx feature path definition (none now) */
620 VNET_FEATURE_ARC_INIT (ip6_multicast, static) =
621 {
622   .arc_name  = "ip6-multicast",
623   .start_nodes = VNET_FEATURES ("ip6-input"),
624   .arc_index_ptr = &ip6_main.lookup_main.mcast_feature_arc_index,
625 };
626
627 VNET_FEATURE_INIT (ip6_vpath_mc, static) = {
628   .arc_name = "ip6-multicast",
629   .node_name = "vpath-input-ip6",
630   .runs_before = VNET_FEATURES ("ip6-mfib-forward-lookup"),
631 };
632
633 VNET_FEATURE_INIT (ip6_mc_lookup, static) = {
634   .arc_name = "ip6-multicast",
635   .node_name = "ip6-mfib-forward-lookup",
636   .runs_before = VNET_FEATURES ("ip6-drop"),
637 };
638
639 VNET_FEATURE_INIT (ip6_drop_mc, static) = {
640   .arc_name = "ip6-multicast",
641   .node_name = "ip6-drop",
642   .runs_before = 0, /* last feature */
643 };
644
645 /* Built-in ip4 tx feature path definition */
646 VNET_FEATURE_ARC_INIT (ip6_output, static) =
647 {
648   .arc_name  = "ip6-output",
649   .start_nodes = VNET_FEATURES ("ip6-rewrite", "ip6-midchain"),
650   .arc_index_ptr = &ip6_main.lookup_main.output_feature_arc_index,
651 };
652
653 VNET_FEATURE_INIT (ip6_ipsec_output, static) = {
654   .arc_name = "ip6-output",
655   .node_name = "ipsec-output-ip6",
656   .runs_before = VNET_FEATURES ("interface-output"),
657 };
658
659 VNET_FEATURE_INIT (ip6_interface_output, static) = {
660   .arc_name = "ip6-output",
661   .node_name = "interface-output",
662   .runs_before = 0, /* not before any other features */
663 };
664 /* *INDENT-ON* */
665
666 clib_error_t *
667 ip6_sw_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
668 {
669   vnet_feature_enable_disable ("ip6-unicast", "ip6-drop", sw_if_index,
670                                is_add, 0, 0);
671
672   vnet_feature_enable_disable ("ip6-multicast", "ip6-drop", sw_if_index,
673                                is_add, 0, 0);
674
675   vnet_feature_enable_disable ("ip6-output", "interface-output", sw_if_index,
676                                is_add, 0, 0);
677
678   return /* no error */ 0;
679 }
680
681 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
682
683 static uword
684 ip6_lookup (vlib_main_t * vm,
685             vlib_node_runtime_t * node, vlib_frame_t * frame)
686 {
687   return ip6_lookup_inline (vm, node, frame);
688 }
689
690 static u8 *format_ip6_lookup_trace (u8 * s, va_list * args);
691
692 /* *INDENT-OFF* */
693 VLIB_REGISTER_NODE (ip6_lookup_node) =
694 {
695   .function = ip6_lookup,
696   .name = "ip6-lookup",
697   .vector_size = sizeof (u32),
698   .format_trace = format_ip6_lookup_trace,
699   .n_next_nodes = IP6_LOOKUP_N_NEXT,
700   .next_nodes = IP6_LOOKUP_NEXT_NODES,
701 };
702 /* *INDENT-ON* */
703
704 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup);
705
706 always_inline uword
707 ip6_load_balance (vlib_main_t * vm,
708                   vlib_node_runtime_t * node, vlib_frame_t * frame)
709 {
710   vlib_combined_counter_main_t *cm = &load_balance_main.lbm_via_counters;
711   u32 n_left_from, n_left_to_next, *from, *to_next;
712   ip_lookup_next_t next;
713   u32 cpu_index = os_get_cpu_number ();
714   ip6_main_t *im = &ip6_main;
715
716   from = vlib_frame_vector_args (frame);
717   n_left_from = frame->n_vectors;
718   next = node->cached_next_index;
719
720   if (node->flags & VLIB_NODE_FLAG_TRACE)
721     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
722
723   while (n_left_from > 0)
724     {
725       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
726
727
728       while (n_left_from >= 4 && n_left_to_next >= 2)
729         {
730           ip_lookup_next_t next0, next1;
731           const load_balance_t *lb0, *lb1;
732           vlib_buffer_t *p0, *p1;
733           u32 pi0, lbi0, hc0, pi1, lbi1, hc1;
734           const ip6_header_t *ip0, *ip1;
735           const dpo_id_t *dpo0, *dpo1;
736
737           /* Prefetch next iteration. */
738           {
739             vlib_buffer_t *p2, *p3;
740
741             p2 = vlib_get_buffer (vm, from[2]);
742             p3 = vlib_get_buffer (vm, from[3]);
743
744             vlib_prefetch_buffer_header (p2, STORE);
745             vlib_prefetch_buffer_header (p3, STORE);
746
747             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
748             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
749           }
750
751           pi0 = to_next[0] = from[0];
752           pi1 = to_next[1] = from[1];
753
754           from += 2;
755           n_left_from -= 2;
756           to_next += 2;
757           n_left_to_next -= 2;
758
759           p0 = vlib_get_buffer (vm, pi0);
760           p1 = vlib_get_buffer (vm, pi1);
761
762           ip0 = vlib_buffer_get_current (p0);
763           ip1 = vlib_buffer_get_current (p1);
764           lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
765           lbi1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
766
767           lb0 = load_balance_get (lbi0);
768           lb1 = load_balance_get (lbi1);
769
770           /*
771            * this node is for via FIBs we can re-use the hash value from the
772            * to node if present.
773            * We don't want to use the same hash value at each level in the recursion
774            * graph as that would lead to polarisation
775            */
776           hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
777           hc1 = vnet_buffer (p1)->ip.flow_hash = 0;
778
779           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
780             {
781               if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
782                 {
783                   hc0 = vnet_buffer (p0)->ip.flow_hash =
784                     vnet_buffer (p0)->ip.flow_hash >> 1;
785                 }
786               else
787                 {
788                   hc0 = vnet_buffer (p0)->ip.flow_hash =
789                     ip6_compute_flow_hash (ip0, hc0);
790                 }
791             }
792           if (PREDICT_FALSE (lb1->lb_n_buckets > 1))
793             {
794               if (PREDICT_TRUE (vnet_buffer (p1)->ip.flow_hash))
795                 {
796                   hc1 = vnet_buffer (p1)->ip.flow_hash =
797                     vnet_buffer (p1)->ip.flow_hash >> 1;
798                 }
799               else
800                 {
801                   hc1 = vnet_buffer (p1)->ip.flow_hash =
802                     ip6_compute_flow_hash (ip1, hc1);
803                 }
804             }
805
806           dpo0 =
807             load_balance_get_bucket_i (lb0,
808                                        hc0 & (lb0->lb_n_buckets_minus_1));
809           dpo1 =
810             load_balance_get_bucket_i (lb1,
811                                        hc1 & (lb1->lb_n_buckets_minus_1));
812
813           next0 = dpo0->dpoi_next_node;
814           next1 = dpo1->dpoi_next_node;
815
816           /* Only process the HBH Option Header if explicitly configured to do so */
817           if (PREDICT_FALSE
818               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
819             {
820               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
821                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
822             }
823           /* Only process the HBH Option Header if explicitly configured to do so */
824           if (PREDICT_FALSE
825               (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
826             {
827               next1 = (dpo_is_adj (dpo1) && im->hbh_enabled) ?
828                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next1;
829             }
830
831           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
832           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
833
834           vlib_increment_combined_counter
835             (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
836           vlib_increment_combined_counter
837             (cm, cpu_index, lbi1, 1, vlib_buffer_length_in_chain (vm, p1));
838
839           vlib_validate_buffer_enqueue_x2 (vm, node, next,
840                                            to_next, n_left_to_next,
841                                            pi0, pi1, next0, next1);
842         }
843
844       while (n_left_from > 0 && n_left_to_next > 0)
845         {
846           ip_lookup_next_t next0;
847           const load_balance_t *lb0;
848           vlib_buffer_t *p0;
849           u32 pi0, lbi0, hc0;
850           const ip6_header_t *ip0;
851           const dpo_id_t *dpo0;
852
853           pi0 = from[0];
854           to_next[0] = pi0;
855           from += 1;
856           to_next += 1;
857           n_left_to_next -= 1;
858           n_left_from -= 1;
859
860           p0 = vlib_get_buffer (vm, pi0);
861
862           ip0 = vlib_buffer_get_current (p0);
863           lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
864
865           lb0 = load_balance_get (lbi0);
866
867           hc0 = vnet_buffer (p0)->ip.flow_hash = 0;
868           if (PREDICT_FALSE (lb0->lb_n_buckets > 1))
869             {
870               if (PREDICT_TRUE (vnet_buffer (p0)->ip.flow_hash))
871                 {
872                   hc0 = vnet_buffer (p0)->ip.flow_hash =
873                     vnet_buffer (p0)->ip.flow_hash >> 1;
874                 }
875               else
876                 {
877                   hc0 = vnet_buffer (p0)->ip.flow_hash =
878                     ip6_compute_flow_hash (ip0, hc0);
879                 }
880             }
881           dpo0 =
882             load_balance_get_bucket_i (lb0,
883                                        hc0 & (lb0->lb_n_buckets_minus_1));
884
885           next0 = dpo0->dpoi_next_node;
886           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
887
888           /* Only process the HBH Option Header if explicitly configured to do so */
889           if (PREDICT_FALSE
890               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
891             {
892               next0 = (dpo_is_adj (dpo0) && im->hbh_enabled) ?
893                 (ip_lookup_next_t) IP6_LOOKUP_NEXT_HOP_BY_HOP : next0;
894             }
895
896           vlib_increment_combined_counter
897             (cm, cpu_index, lbi0, 1, vlib_buffer_length_in_chain (vm, p0));
898
899           vlib_validate_buffer_enqueue_x1 (vm, node, next,
900                                            to_next, n_left_to_next,
901                                            pi0, next0);
902         }
903
904       vlib_put_next_frame (vm, node, next, n_left_to_next);
905     }
906
907   return frame->n_vectors;
908 }
909
910 /* *INDENT-OFF* */
911 VLIB_REGISTER_NODE (ip6_load_balance_node) =
912 {
913   .function = ip6_load_balance,
914   .name = "ip6-load-balance",
915   .vector_size = sizeof (u32),
916   .sibling_of = "ip6-lookup",
917   .format_trace = format_ip6_lookup_trace,
918 };
919 /* *INDENT-ON* */
920
921 VLIB_NODE_FUNCTION_MULTIARCH (ip6_load_balance_node, ip6_load_balance);
922
923 typedef struct
924 {
925   /* Adjacency taken. */
926   u32 adj_index;
927   u32 flow_hash;
928   u32 fib_index;
929
930   /* Packet data, possibly *after* rewrite. */
931   u8 packet_data[128 - 1 * sizeof (u32)];
932 }
933 ip6_forward_next_trace_t;
934
935 u8 *
936 format_ip6_forward_next_trace (u8 * s, va_list * args)
937 {
938   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
939   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
940   ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
941   uword indent = format_get_indent (s);
942
943   s = format (s, "%U%U",
944               format_white_space, indent,
945               format_ip6_header, t->packet_data, sizeof (t->packet_data));
946   return s;
947 }
948
949 static u8 *
950 format_ip6_lookup_trace (u8 * s, va_list * args)
951 {
952   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
953   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
954   ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
955   uword indent = format_get_indent (s);
956
957   s = format (s, "fib %d dpo-idx %d flow hash: 0x%08x",
958               t->fib_index, t->adj_index, t->flow_hash);
959   s = format (s, "\n%U%U",
960               format_white_space, indent,
961               format_ip6_header, t->packet_data, sizeof (t->packet_data));
962   return s;
963 }
964
965
966 static u8 *
967 format_ip6_rewrite_trace (u8 * s, va_list * args)
968 {
969   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
970   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
971   ip6_forward_next_trace_t *t = va_arg (*args, ip6_forward_next_trace_t *);
972   vnet_main_t *vnm = vnet_get_main ();
973   uword indent = format_get_indent (s);
974
975   s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
976               t->fib_index, t->adj_index, format_ip_adjacency,
977               t->adj_index, FORMAT_IP_ADJACENCY_NONE, t->flow_hash);
978   s = format (s, "\n%U%U",
979               format_white_space, indent,
980               format_ip_adjacency_packet_data,
981               vnm, t->adj_index, t->packet_data, sizeof (t->packet_data));
982   return s;
983 }
984
985 /* Common trace function for all ip6-forward next nodes. */
986 void
987 ip6_forward_next_trace (vlib_main_t * vm,
988                         vlib_node_runtime_t * node,
989                         vlib_frame_t * frame, vlib_rx_or_tx_t which_adj_index)
990 {
991   u32 *from, n_left;
992   ip6_main_t *im = &ip6_main;
993
994   n_left = frame->n_vectors;
995   from = vlib_frame_vector_args (frame);
996
997   while (n_left >= 4)
998     {
999       u32 bi0, bi1;
1000       vlib_buffer_t *b0, *b1;
1001       ip6_forward_next_trace_t *t0, *t1;
1002
1003       /* Prefetch next iteration. */
1004       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1005       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1006
1007       bi0 = from[0];
1008       bi1 = from[1];
1009
1010       b0 = vlib_get_buffer (vm, bi0);
1011       b1 = vlib_get_buffer (vm, bi1);
1012
1013       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1014         {
1015           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1016           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1017           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1018           t0->fib_index =
1019             (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1020              (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1021             vec_elt (im->fib_index_by_sw_if_index,
1022                      vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1023
1024           clib_memcpy (t0->packet_data,
1025                        vlib_buffer_get_current (b0),
1026                        sizeof (t0->packet_data));
1027         }
1028       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1029         {
1030           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1031           t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1032           t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1033           t1->fib_index =
1034             (vnet_buffer (b1)->sw_if_index[VLIB_TX] !=
1035              (u32) ~ 0) ? vnet_buffer (b1)->sw_if_index[VLIB_TX] :
1036             vec_elt (im->fib_index_by_sw_if_index,
1037                      vnet_buffer (b1)->sw_if_index[VLIB_RX]);
1038
1039           clib_memcpy (t1->packet_data,
1040                        vlib_buffer_get_current (b1),
1041                        sizeof (t1->packet_data));
1042         }
1043       from += 2;
1044       n_left -= 2;
1045     }
1046
1047   while (n_left >= 1)
1048     {
1049       u32 bi0;
1050       vlib_buffer_t *b0;
1051       ip6_forward_next_trace_t *t0;
1052
1053       bi0 = from[0];
1054
1055       b0 = vlib_get_buffer (vm, bi0);
1056
1057       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1058         {
1059           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1060           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1061           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1062           t0->fib_index =
1063             (vnet_buffer (b0)->sw_if_index[VLIB_TX] !=
1064              (u32) ~ 0) ? vnet_buffer (b0)->sw_if_index[VLIB_TX] :
1065             vec_elt (im->fib_index_by_sw_if_index,
1066                      vnet_buffer (b0)->sw_if_index[VLIB_RX]);
1067
1068           clib_memcpy (t0->packet_data,
1069                        vlib_buffer_get_current (b0),
1070                        sizeof (t0->packet_data));
1071         }
1072       from += 1;
1073       n_left -= 1;
1074     }
1075 }
1076
1077 static uword
1078 ip6_drop_or_punt (vlib_main_t * vm,
1079                   vlib_node_runtime_t * node,
1080                   vlib_frame_t * frame, ip6_error_t error_code)
1081 {
1082   u32 *buffers = vlib_frame_vector_args (frame);
1083   uword n_packets = frame->n_vectors;
1084
1085   vlib_error_drop_buffers (vm, node, buffers,
1086                            /* stride */ 1,
1087                            n_packets,
1088                            /* next */ 0,
1089                            ip6_input_node.index, error_code);
1090
1091   if (node->flags & VLIB_NODE_FLAG_TRACE)
1092     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1093
1094   return n_packets;
1095 }
1096
1097 static uword
1098 ip6_drop (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1099 {
1100   return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP);
1101 }
1102
1103 static uword
1104 ip6_punt (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1105 {
1106   return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT);
1107 }
1108
1109 /* *INDENT-OFF* */
1110 VLIB_REGISTER_NODE (ip6_drop_node, static) =
1111 {
1112   .function = ip6_drop,
1113   .name = "ip6-drop",
1114   .vector_size = sizeof (u32),
1115   .format_trace = format_ip6_forward_next_trace,
1116   .n_next_nodes = 1,
1117   .next_nodes =
1118   {
1119     [0] = "error-drop",},
1120 };
1121 /* *INDENT-ON* */
1122
1123 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop);
1124
1125 /* *INDENT-OFF* */
1126 VLIB_REGISTER_NODE (ip6_punt_node, static) =
1127 {
1128   .function = ip6_punt,
1129   .name = "ip6-punt",
1130   .vector_size = sizeof (u32),
1131   .format_trace = format_ip6_forward_next_trace,
1132   .n_next_nodes = 1,
1133   .next_nodes =
1134   {
1135     [0] = "error-punt",},
1136 };
1137 /* *INDENT-ON* */
1138
1139 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt);
1140
1141 /* Compute TCP/UDP/ICMP6 checksum in software. */
1142 u16
1143 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
1144                                    ip6_header_t * ip0, int *bogus_lengthp)
1145 {
1146   ip_csum_t sum0;
1147   u16 sum16, payload_length_host_byte_order;
1148   u32 i, n_this_buffer, n_bytes_left;
1149   u32 headers_size = sizeof (ip0[0]);
1150   void *data_this_buffer;
1151
1152   ASSERT (bogus_lengthp);
1153   *bogus_lengthp = 0;
1154
1155   /* Initialize checksum with ip header. */
1156   sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1157   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1158   data_this_buffer = (void *) (ip0 + 1);
1159
1160   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1161     {
1162       sum0 = ip_csum_with_carry (sum0,
1163                                  clib_mem_unaligned (&ip0->
1164                                                      src_address.as_uword[i],
1165                                                      uword));
1166       sum0 =
1167         ip_csum_with_carry (sum0,
1168                             clib_mem_unaligned (&ip0->dst_address.as_uword[i],
1169                                                 uword));
1170     }
1171
1172   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1173   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1174     {
1175       u32 skip_bytes;
1176       ip6_hop_by_hop_ext_t *ext_hdr =
1177         (ip6_hop_by_hop_ext_t *) data_this_buffer;
1178
1179       /* validate really icmp6 next */
1180       ASSERT (ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1181
1182       skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
1183       data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
1184
1185       payload_length_host_byte_order -= skip_bytes;
1186       headers_size += skip_bytes;
1187     }
1188
1189   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1190   if (p0 && n_this_buffer + headers_size > p0->current_length)
1191     n_this_buffer =
1192       p0->current_length >
1193       headers_size ? p0->current_length - headers_size : 0;
1194   while (1)
1195     {
1196       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1197       n_bytes_left -= n_this_buffer;
1198       if (n_bytes_left == 0)
1199         break;
1200
1201       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1202         {
1203           *bogus_lengthp = 1;
1204           return 0xfefe;
1205         }
1206       p0 = vlib_get_buffer (vm, p0->next_buffer);
1207       data_this_buffer = vlib_buffer_get_current (p0);
1208       n_this_buffer = p0->current_length;
1209     }
1210
1211   sum16 = ~ip_csum_fold (sum0);
1212
1213   return sum16;
1214 }
1215
1216 u32
1217 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1218 {
1219   ip6_header_t *ip0 = vlib_buffer_get_current (p0);
1220   udp_header_t *udp0;
1221   u16 sum16;
1222   int bogus_length;
1223
1224   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1225   ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1226           || ip0->protocol == IP_PROTOCOL_ICMP6
1227           || ip0->protocol == IP_PROTOCOL_UDP
1228           || ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1229
1230   udp0 = (void *) (ip0 + 1);
1231   if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1232     {
1233       p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1234                     | IP_BUFFER_L4_CHECKSUM_CORRECT);
1235       return p0->flags;
1236     }
1237
1238   sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1239
1240   p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1241                 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1242
1243   return p0->flags;
1244 }
1245
1246 /* ip6_locate_header
1247  *
1248  * This function is to search for the header specified by the find_hdr number.
1249  *   1. If the find_hdr < 0 then it finds and returns the protocol number and
1250  *   offset stored in *offset of the transport or ESP header in the chain if
1251  *   found.
1252  *   2. If a header with find_hdr > 0 protocol number is found then the
1253  *      offset is stored in *offset and protocol number of the header is
1254  *      returned.
1255  *   3. If find_hdr header is not found or packet is malformed or
1256  *      it is a non-first fragment -1 is returned.
1257  */
1258 always_inline int
1259 ip6_locate_header (vlib_buffer_t * p0,
1260                    ip6_header_t * ip0, int find_hdr, u32 * offset)
1261 {
1262   u8 next_proto = ip0->protocol;
1263   u8 *next_header;
1264   u8 done = 0;
1265   u32 cur_offset;
1266   u8 *temp_nxthdr = 0;
1267   u32 exthdr_len = 0;
1268
1269   next_header = ip6_next_header (ip0);
1270   cur_offset = sizeof (ip6_header_t);
1271   while (1)
1272     {
1273       done = (next_proto == find_hdr);
1274       if (PREDICT_FALSE
1275           (next_header >=
1276            (u8 *) vlib_buffer_get_current (p0) + p0->current_length))
1277         {
1278           //A malicious packet could set an extension header with a too big size
1279           return (-1);
1280         }
1281       if (done)
1282         break;
1283       if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT)
1284         {
1285           if (find_hdr < 0)
1286             break;
1287           return -1;
1288         }
1289       if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION)
1290         {
1291           ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header;
1292           u16 frag_off = ip6_frag_hdr_offset (frag_hdr);
1293           /* Non first fragment return -1 */
1294           if (frag_off)
1295             return (-1);
1296           exthdr_len = sizeof (ip6_frag_hdr_t);
1297           temp_nxthdr = next_header + exthdr_len;
1298         }
1299       else if (next_proto == IP_PROTOCOL_IPSEC_AH)
1300         {
1301           exthdr_len =
1302             ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header));
1303           temp_nxthdr = next_header + exthdr_len;
1304         }
1305       else
1306         {
1307           exthdr_len =
1308             ip6_ext_header_len (((ip6_ext_header_t *) next_header));
1309           temp_nxthdr = next_header + exthdr_len;
1310         }
1311       next_proto = ((ip6_ext_header_t *) next_header)->next_hdr;
1312       next_header = temp_nxthdr;
1313       cur_offset += exthdr_len;
1314     }
1315
1316   *offset = cur_offset;
1317   return (next_proto);
1318 }
1319
1320 /**
1321  * @brief returns number of links on which src is reachable.
1322  */
1323 always_inline int
1324 ip6_urpf_loose_check (ip6_main_t * im, vlib_buffer_t * b, ip6_header_t * i)
1325 {
1326   const load_balance_t *lb0;
1327   index_t lbi;
1328
1329   lbi = ip6_fib_table_fwding_lookup_with_if_index (im,
1330                                                    vnet_buffer
1331                                                    (b)->sw_if_index[VLIB_RX],
1332                                                    &i->src_address);
1333
1334   lb0 = load_balance_get (lbi);
1335
1336   return (fib_urpf_check_size (lb0->lb_urpf));
1337 }
1338
1339 static uword
1340 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1341 {
1342   ip6_main_t *im = &ip6_main;
1343   ip_lookup_main_t *lm = &im->lookup_main;
1344   ip_local_next_t next_index;
1345   u32 *from, *to_next, n_left_from, n_left_to_next;
1346   vlib_node_runtime_t *error_node =
1347     vlib_node_get_runtime (vm, ip6_input_node.index);
1348
1349   from = vlib_frame_vector_args (frame);
1350   n_left_from = frame->n_vectors;
1351   next_index = node->cached_next_index;
1352
1353   if (node->flags & VLIB_NODE_FLAG_TRACE)
1354     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1355
1356   while (n_left_from > 0)
1357     {
1358       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1359
1360       while (n_left_from >= 4 && n_left_to_next >= 2)
1361         {
1362           vlib_buffer_t *p0, *p1;
1363           ip6_header_t *ip0, *ip1;
1364           udp_header_t *udp0, *udp1;
1365           u32 pi0, ip_len0, udp_len0, flags0, next0;
1366           u32 pi1, ip_len1, udp_len1, flags1, next1;
1367           i32 len_diff0, len_diff1;
1368           u8 error0, type0, good_l4_checksum0;
1369           u8 error1, type1, good_l4_checksum1;
1370           u32 udp_offset0, udp_offset1;
1371
1372           pi0 = to_next[0] = from[0];
1373           pi1 = to_next[1] = from[1];
1374           from += 2;
1375           n_left_from -= 2;
1376           to_next += 2;
1377           n_left_to_next -= 2;
1378
1379           p0 = vlib_get_buffer (vm, pi0);
1380           p1 = vlib_get_buffer (vm, pi1);
1381
1382           ip0 = vlib_buffer_get_current (p0);
1383           ip1 = vlib_buffer_get_current (p1);
1384
1385           vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1386           vnet_buffer (p1)->ip.start_of_ip_header = p1->current_data;
1387
1388           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1389           type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1390
1391           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1392           next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1393
1394           flags0 = p0->flags;
1395           flags1 = p1->flags;
1396
1397           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1398           good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1399           len_diff0 = 0;
1400           len_diff1 = 0;
1401
1402           /* Skip HBH local processing */
1403           if (PREDICT_FALSE
1404               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1405             {
1406               ip6_hop_by_hop_ext_t *ext_hdr =
1407                 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
1408               next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1409               type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1410             }
1411           if (PREDICT_FALSE
1412               (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1413             {
1414               ip6_hop_by_hop_ext_t *ext_hdr =
1415                 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip1);
1416               next1 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1417               type1 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1418             }
1419           if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1420                                                                   IP_PROTOCOL_UDP,
1421                                                                   &udp_offset0)))
1422             {
1423               udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1424               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1425               good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1426                 && udp0->checksum == 0;
1427               /* Verify UDP length. */
1428               ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1429               udp_len0 = clib_net_to_host_u16 (udp0->length);
1430               len_diff0 = ip_len0 - udp_len0;
1431             }
1432           if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p1, ip1,
1433                                                                   IP_PROTOCOL_UDP,
1434                                                                   &udp_offset1)))
1435             {
1436               udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1437               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1438               good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1439                 && udp1->checksum == 0;
1440               /* Verify UDP length. */
1441               ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1442               udp_len1 = clib_net_to_host_u16 (udp1->length);
1443               len_diff1 = ip_len1 - udp_len1;
1444             }
1445
1446           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1447           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1448
1449           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1450           len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1451
1452           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1453                              && !good_l4_checksum0
1454                              && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1455             {
1456               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1457               good_l4_checksum0 =
1458                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1459             }
1460           if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1461                              && !good_l4_checksum1
1462                              && !(flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1463             {
1464               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1465               good_l4_checksum1 =
1466                 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1467             }
1468
1469           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1470
1471           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1472           error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1473
1474           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1475                   IP6_ERROR_UDP_CHECKSUM);
1476           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1477                   IP6_ERROR_ICMP_CHECKSUM);
1478           error0 =
1479             (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1480           error1 =
1481             (!good_l4_checksum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1482
1483           /* Drop packets from unroutable hosts. */
1484           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1485           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1486               type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1487               !ip6_address_is_link_local_unicast (&ip0->src_address))
1488             {
1489               error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1490                         ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1491             }
1492           if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1493               type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1494               !ip6_address_is_link_local_unicast (&ip1->src_address))
1495             {
1496               error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1497                         ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1498             }
1499
1500           next0 =
1501             error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1502           next1 =
1503             error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1504
1505           p0->error = error_node->errors[error0];
1506           p1->error = error_node->errors[error1];
1507
1508           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1509                                            to_next, n_left_to_next,
1510                                            pi0, pi1, next0, next1);
1511         }
1512
1513       while (n_left_from > 0 && n_left_to_next > 0)
1514         {
1515           vlib_buffer_t *p0;
1516           ip6_header_t *ip0;
1517           udp_header_t *udp0;
1518           u32 pi0, ip_len0, udp_len0, flags0, next0;
1519           i32 len_diff0;
1520           u8 error0, type0, good_l4_checksum0;
1521           u32 udp_offset0;
1522
1523           pi0 = to_next[0] = from[0];
1524           from += 1;
1525           n_left_from -= 1;
1526           to_next += 1;
1527           n_left_to_next -= 1;
1528
1529           p0 = vlib_get_buffer (vm, pi0);
1530
1531           ip0 = vlib_buffer_get_current (p0);
1532
1533           vnet_buffer (p0)->ip.start_of_ip_header = p0->current_data;
1534
1535           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1536           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1537
1538           flags0 = p0->flags;
1539
1540           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1541           len_diff0 = 0;
1542
1543           /* Skip HBH local processing */
1544           if (PREDICT_FALSE
1545               (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1546             {
1547               ip6_hop_by_hop_ext_t *ext_hdr =
1548                 (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0);
1549               next0 = lm->local_next_by_ip_protocol[ext_hdr->next_hdr];
1550               type0 = lm->builtin_protocol_by_ip_protocol[ext_hdr->next_hdr];
1551             }
1552           if (PREDICT_TRUE (IP_PROTOCOL_UDP == ip6_locate_header (p0, ip0,
1553                                                                   IP_PROTOCOL_UDP,
1554                                                                   &udp_offset0)))
1555             {
1556               udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1557               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1558               good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1559                 && udp0->checksum == 0;
1560               /* Verify UDP length. */
1561               ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1562               udp_len0 = clib_net_to_host_u16 (udp0->length);
1563               len_diff0 = ip_len0 - udp_len0;
1564             }
1565
1566           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1567           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1568
1569           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1570                              && !good_l4_checksum0
1571                              && !(flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1572             {
1573               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1574               good_l4_checksum0 =
1575                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1576             }
1577
1578           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1579
1580           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1581
1582           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1583                   IP6_ERROR_UDP_CHECKSUM);
1584           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1585                   IP6_ERROR_ICMP_CHECKSUM);
1586           error0 =
1587             (!good_l4_checksum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1588
1589           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1590           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1591               type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1592               !ip6_address_is_link_local_unicast (&ip0->src_address))
1593             {
1594               error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1595                         ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1596             }
1597
1598           next0 =
1599             error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1600
1601           p0->error = error_node->errors[error0];
1602
1603           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1604                                            to_next, n_left_to_next,
1605                                            pi0, next0);
1606         }
1607
1608       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1609     }
1610
1611   return frame->n_vectors;
1612 }
1613
1614 /* *INDENT-OFF* */
1615 VLIB_REGISTER_NODE (ip6_local_node, static) =
1616 {
1617   .function = ip6_local,
1618   .name = "ip6-local",
1619   .vector_size = sizeof (u32),
1620   .format_trace = format_ip6_forward_next_trace,
1621   .n_next_nodes = IP_LOCAL_N_NEXT,
1622   .next_nodes =
1623   {
1624     [IP_LOCAL_NEXT_DROP] = "error-drop",
1625     [IP_LOCAL_NEXT_PUNT] = "error-punt",
1626     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1627     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1628   },
1629 };
1630 /* *INDENT-ON* */
1631
1632 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1633
1634 void
1635 ip6_register_protocol (u32 protocol, u32 node_index)
1636 {
1637   vlib_main_t *vm = vlib_get_main ();
1638   ip6_main_t *im = &ip6_main;
1639   ip_lookup_main_t *lm = &im->lookup_main;
1640
1641   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1642   lm->local_next_by_ip_protocol[protocol] =
1643     vlib_node_add_next (vm, ip6_local_node.index, node_index);
1644 }
1645
1646 typedef enum
1647 {
1648   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1649   IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1650   IP6_DISCOVER_NEIGHBOR_N_NEXT,
1651 } ip6_discover_neighbor_next_t;
1652
1653 typedef enum
1654 {
1655   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1656   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1657   IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1658 } ip6_discover_neighbor_error_t;
1659
1660 static uword
1661 ip6_discover_neighbor_inline (vlib_main_t * vm,
1662                               vlib_node_runtime_t * node,
1663                               vlib_frame_t * frame, int is_glean)
1664 {
1665   vnet_main_t *vnm = vnet_get_main ();
1666   ip6_main_t *im = &ip6_main;
1667   ip_lookup_main_t *lm = &im->lookup_main;
1668   u32 *from, *to_next_drop;
1669   uword n_left_from, n_left_to_next_drop;
1670   static f64 time_last_seed_change = -1e100;
1671   static u32 hash_seeds[3];
1672   static uword hash_bitmap[256 / BITS (uword)];
1673   f64 time_now;
1674   int bogus_length;
1675
1676   if (node->flags & VLIB_NODE_FLAG_TRACE)
1677     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1678
1679   time_now = vlib_time_now (vm);
1680   if (time_now - time_last_seed_change > 1e-3)
1681     {
1682       uword i;
1683       u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1684                                             sizeof (hash_seeds));
1685       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1686         hash_seeds[i] = r[i];
1687
1688       /* Mark all hash keys as been not-seen before. */
1689       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1690         hash_bitmap[i] = 0;
1691
1692       time_last_seed_change = time_now;
1693     }
1694
1695   from = vlib_frame_vector_args (frame);
1696   n_left_from = frame->n_vectors;
1697
1698   while (n_left_from > 0)
1699     {
1700       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1701                            to_next_drop, n_left_to_next_drop);
1702
1703       while (n_left_from > 0 && n_left_to_next_drop > 0)
1704         {
1705           vlib_buffer_t *p0;
1706           ip6_header_t *ip0;
1707           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1708           uword bm0;
1709           ip_adjacency_t *adj0;
1710           vnet_hw_interface_t *hw_if0;
1711           u32 next0;
1712
1713           pi0 = from[0];
1714
1715           p0 = vlib_get_buffer (vm, pi0);
1716
1717           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1718
1719           ip0 = vlib_buffer_get_current (p0);
1720
1721           adj0 = ip_get_adjacency (lm, adj_index0);
1722
1723           if (!is_glean)
1724             {
1725               ip0->dst_address.as_u64[0] =
1726                 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1727               ip0->dst_address.as_u64[1] =
1728                 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1729             }
1730
1731           a0 = hash_seeds[0];
1732           b0 = hash_seeds[1];
1733           c0 = hash_seeds[2];
1734
1735           sw_if_index0 = adj0->rewrite_header.sw_if_index;
1736           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1737
1738           a0 ^= sw_if_index0;
1739           b0 ^= ip0->dst_address.as_u32[0];
1740           c0 ^= ip0->dst_address.as_u32[1];
1741
1742           hash_v3_mix32 (a0, b0, c0);
1743
1744           b0 ^= ip0->dst_address.as_u32[2];
1745           c0 ^= ip0->dst_address.as_u32[3];
1746
1747           hash_v3_finalize32 (a0, b0, c0);
1748
1749           c0 &= BITS (hash_bitmap) - 1;
1750           c0 = c0 / BITS (uword);
1751           m0 = (uword) 1 << (c0 % BITS (uword));
1752
1753           bm0 = hash_bitmap[c0];
1754           drop0 = (bm0 & m0) != 0;
1755
1756           /* Mark it as seen. */
1757           hash_bitmap[c0] = bm0 | m0;
1758
1759           from += 1;
1760           n_left_from -= 1;
1761           to_next_drop[0] = pi0;
1762           to_next_drop += 1;
1763           n_left_to_next_drop -= 1;
1764
1765           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1766
1767           /* If the interface is link-down, drop the pkt */
1768           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1769             drop0 = 1;
1770
1771           p0->error =
1772             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1773                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1774           if (drop0)
1775             continue;
1776
1777           /*
1778            * the adj has been updated to a rewrite but the node the DPO that got
1779            * us here hasn't - yet. no big deal. we'll drop while we wait.
1780            */
1781           if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1782             continue;
1783
1784           {
1785             u32 bi0 = 0;
1786             icmp6_neighbor_solicitation_header_t *h0;
1787             vlib_buffer_t *b0;
1788
1789             h0 = vlib_packet_template_get_packet
1790               (vm, &im->discover_neighbor_packet_template, &bi0);
1791
1792             /*
1793              * Build ethernet header.
1794              * Choose source address based on destination lookup
1795              * adjacency.
1796              */
1797             if (ip6_src_address_for_packet (lm,
1798                                             sw_if_index0,
1799                                             &h0->ip.src_address))
1800               {
1801                 /* There is no address on the interface */
1802                 p0->error =
1803                   node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1804                 vlib_buffer_free (vm, &bi0, 1);
1805                 continue;
1806               }
1807
1808             /*
1809              * Destination address is a solicited node multicast address.
1810              * We need to fill in
1811              * the low 24 bits with low 24 bits of target's address.
1812              */
1813             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1814             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1815             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1816
1817             h0->neighbor.target_address = ip0->dst_address;
1818
1819             clib_memcpy (h0->link_layer_option.ethernet_address,
1820                          hw_if0->hw_address, vec_len (hw_if0->hw_address));
1821
1822             /* $$$$ appears we need this; why is the checksum non-zero? */
1823             h0->neighbor.icmp.checksum = 0;
1824             h0->neighbor.icmp.checksum =
1825               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1826                                                  &bogus_length);
1827
1828             ASSERT (bogus_length == 0);
1829
1830             vlib_buffer_copy_trace_flag (vm, p0, bi0);
1831             b0 = vlib_get_buffer (vm, bi0);
1832             vnet_buffer (b0)->sw_if_index[VLIB_TX]
1833               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1834
1835             /* Add rewrite/encap string. */
1836             vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1837             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1838
1839             next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1840
1841             vlib_set_next_frame_buffer (vm, node, next0, bi0);
1842           }
1843         }
1844
1845       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1846                            n_left_to_next_drop);
1847     }
1848
1849   return frame->n_vectors;
1850 }
1851
1852 static uword
1853 ip6_discover_neighbor (vlib_main_t * vm,
1854                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1855 {
1856   return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1857 }
1858
1859 static uword
1860 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1861 {
1862   return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1863 }
1864
1865 static char *ip6_discover_neighbor_error_strings[] = {
1866   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1867   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1868   [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1869     = "no source address for ND solicitation",
1870 };
1871
1872 /* *INDENT-OFF* */
1873 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1874 {
1875   .function = ip6_discover_neighbor,
1876   .name = "ip6-discover-neighbor",
1877   .vector_size = sizeof (u32),
1878   .format_trace = format_ip6_forward_next_trace,
1879   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1880   .error_strings = ip6_discover_neighbor_error_strings,
1881   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1882   .next_nodes =
1883   {
1884     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1885     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1886   },
1887 };
1888 /* *INDENT-ON* */
1889
1890 /* *INDENT-OFF* */
1891 VLIB_REGISTER_NODE (ip6_glean_node) =
1892 {
1893   .function = ip6_glean,
1894   .name = "ip6-glean",
1895   .vector_size = sizeof (u32),
1896   .format_trace = format_ip6_forward_next_trace,
1897   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1898   .error_strings = ip6_discover_neighbor_error_strings,
1899   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1900   .next_nodes =
1901   {
1902     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1903     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1904   },
1905 };
1906 /* *INDENT-ON* */
1907
1908 clib_error_t *
1909 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1910 {
1911   vnet_main_t *vnm = vnet_get_main ();
1912   ip6_main_t *im = &ip6_main;
1913   icmp6_neighbor_solicitation_header_t *h;
1914   ip6_address_t *src;
1915   ip_interface_address_t *ia;
1916   ip_adjacency_t *adj;
1917   vnet_hw_interface_t *hi;
1918   vnet_sw_interface_t *si;
1919   vlib_buffer_t *b;
1920   u32 bi = 0;
1921   int bogus_length;
1922
1923   si = vnet_get_sw_interface (vnm, sw_if_index);
1924
1925   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1926     {
1927       return clib_error_return (0, "%U: interface %U down",
1928                                 format_ip6_address, dst,
1929                                 format_vnet_sw_if_index_name, vnm,
1930                                 sw_if_index);
1931     }
1932
1933   src =
1934     ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1935   if (!src)
1936     {
1937       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1938       return clib_error_return
1939         (0, "no matching interface address for destination %U (interface %U)",
1940          format_ip6_address, dst,
1941          format_vnet_sw_if_index_name, vnm, sw_if_index);
1942     }
1943
1944   h =
1945     vlib_packet_template_get_packet (vm,
1946                                      &im->discover_neighbor_packet_template,
1947                                      &bi);
1948
1949   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1950
1951   /* Destination address is a solicited node multicast address.  We need to fill in
1952      the low 24 bits with low 24 bits of target's address. */
1953   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1954   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1955   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1956
1957   h->ip.src_address = src[0];
1958   h->neighbor.target_address = dst[0];
1959
1960   clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1961                vec_len (hi->hw_address));
1962
1963   h->neighbor.icmp.checksum =
1964     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1965   ASSERT (bogus_length == 0);
1966
1967   b = vlib_get_buffer (vm, bi);
1968   vnet_buffer (b)->sw_if_index[VLIB_RX] =
1969     vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1970
1971   /* Add encapsulation string for software interface (e.g. ethernet header). */
1972   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
1973   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1974   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1975
1976   {
1977     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1978     u32 *to_next = vlib_frame_vector_args (f);
1979     to_next[0] = bi;
1980     f->n_vectors = 1;
1981     vlib_put_frame_to_node (vm, hi->output_node_index, f);
1982   }
1983
1984   return /* no error */ 0;
1985 }
1986
1987 typedef enum
1988 {
1989   IP6_REWRITE_NEXT_DROP,
1990   IP6_REWRITE_NEXT_ICMP_ERROR,
1991 } ip6_rewrite_next_t;
1992
1993 always_inline uword
1994 ip6_rewrite_inline (vlib_main_t * vm,
1995                     vlib_node_runtime_t * node,
1996                     vlib_frame_t * frame, int is_midchain, int is_mcast)
1997 {
1998   ip_lookup_main_t *lm = &ip6_main.lookup_main;
1999   u32 *from = vlib_frame_vector_args (frame);
2000   u32 n_left_from, n_left_to_next, *to_next, next_index;
2001   vlib_node_runtime_t *error_node =
2002     vlib_node_get_runtime (vm, ip6_input_node.index);
2003
2004   n_left_from = frame->n_vectors;
2005   next_index = node->cached_next_index;
2006   u32 cpu_index = os_get_cpu_number ();
2007
2008   while (n_left_from > 0)
2009     {
2010       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2011
2012       while (n_left_from >= 4 && n_left_to_next >= 2)
2013         {
2014           ip_adjacency_t *adj0, *adj1;
2015           vlib_buffer_t *p0, *p1;
2016           ip6_header_t *ip0, *ip1;
2017           u32 pi0, rw_len0, next0, error0, adj_index0;
2018           u32 pi1, rw_len1, next1, error1, adj_index1;
2019           u32 tx_sw_if_index0, tx_sw_if_index1;
2020
2021           /* Prefetch next iteration. */
2022           {
2023             vlib_buffer_t *p2, *p3;
2024
2025             p2 = vlib_get_buffer (vm, from[2]);
2026             p3 = vlib_get_buffer (vm, from[3]);
2027
2028             vlib_prefetch_buffer_header (p2, LOAD);
2029             vlib_prefetch_buffer_header (p3, LOAD);
2030
2031             CLIB_PREFETCH (p2->pre_data, 32, STORE);
2032             CLIB_PREFETCH (p3->pre_data, 32, STORE);
2033
2034             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2035             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2036           }
2037
2038           pi0 = to_next[0] = from[0];
2039           pi1 = to_next[1] = from[1];
2040
2041           from += 2;
2042           n_left_from -= 2;
2043           to_next += 2;
2044           n_left_to_next -= 2;
2045
2046           p0 = vlib_get_buffer (vm, pi0);
2047           p1 = vlib_get_buffer (vm, pi1);
2048
2049           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2050           adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
2051
2052           /* We should never rewrite a pkt using the MISS adjacency */
2053           ASSERT (adj_index0 && adj_index1);
2054
2055           ip0 = vlib_buffer_get_current (p0);
2056           ip1 = vlib_buffer_get_current (p1);
2057
2058           error0 = error1 = IP6_ERROR_NONE;
2059           next0 = next1 = IP6_REWRITE_NEXT_DROP;
2060
2061           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2062             {
2063               i32 hop_limit0 = ip0->hop_limit;
2064
2065               /* Input node should have reject packets with hop limit 0. */
2066               ASSERT (ip0->hop_limit > 0);
2067
2068               hop_limit0 -= 1;
2069
2070               ip0->hop_limit = hop_limit0;
2071
2072               /*
2073                * If the hop count drops below 1 when forwarding, generate
2074                * an ICMP response.
2075                */
2076               if (PREDICT_FALSE (hop_limit0 <= 0))
2077                 {
2078                   error0 = IP6_ERROR_TIME_EXPIRED;
2079                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2080                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2081                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2082                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
2083                                                0);
2084                 }
2085             }
2086           else
2087             {
2088               p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2089             }
2090           if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2091             {
2092               i32 hop_limit1 = ip1->hop_limit;
2093
2094               /* Input node should have reject packets with hop limit 0. */
2095               ASSERT (ip1->hop_limit > 0);
2096
2097               hop_limit1 -= 1;
2098
2099               ip1->hop_limit = hop_limit1;
2100
2101               /*
2102                * If the hop count drops below 1 when forwarding, generate
2103                * an ICMP response.
2104                */
2105               if (PREDICT_FALSE (hop_limit1 <= 0))
2106                 {
2107                   error1 = IP6_ERROR_TIME_EXPIRED;
2108                   next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
2109                   vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2110                   icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
2111                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
2112                                                0);
2113                 }
2114             }
2115           else
2116             {
2117               p1->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2118             }
2119           adj0 = ip_get_adjacency (lm, adj_index0);
2120           adj1 = ip_get_adjacency (lm, adj_index1);
2121
2122           rw_len0 = adj0[0].rewrite_header.data_bytes;
2123           rw_len1 = adj1[0].rewrite_header.data_bytes;
2124           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2125           vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
2126
2127           vlib_increment_combined_counter
2128             (&adjacency_counters,
2129              cpu_index,
2130              adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2131           vlib_increment_combined_counter
2132             (&adjacency_counters,
2133              cpu_index, adj_index1,
2134              1, vlib_buffer_length_in_chain (vm, p1) + rw_len1);
2135
2136           /* Check MTU of outgoing interface. */
2137           error0 =
2138             (vlib_buffer_length_in_chain (vm, p0) >
2139              adj0[0].
2140              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2141              error0);
2142           error1 =
2143             (vlib_buffer_length_in_chain (vm, p1) >
2144              adj1[0].
2145              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2146              error1);
2147
2148           /* Don't adjust the buffer for hop count issue; icmp-error node
2149            * wants to see the IP headerr */
2150           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2151             {
2152               p0->current_data -= rw_len0;
2153               p0->current_length += rw_len0;
2154
2155               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2156               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2157               next0 = adj0[0].rewrite_header.next_index;
2158
2159               vnet_feature_arc_start (lm->output_feature_arc_index,
2160                                       tx_sw_if_index0, &next0, p0);
2161             }
2162           if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
2163             {
2164               p1->current_data -= rw_len1;
2165               p1->current_length += rw_len1;
2166
2167               tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
2168               vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
2169               next1 = adj1[0].rewrite_header.next_index;
2170
2171               vnet_feature_arc_start (lm->output_feature_arc_index,
2172                                       tx_sw_if_index1, &next1, p1);
2173             }
2174
2175           /* Guess we are only writing on simple Ethernet header. */
2176           vnet_rewrite_two_headers (adj0[0], adj1[0],
2177                                     ip0, ip1, sizeof (ethernet_header_t));
2178
2179           if (is_midchain)
2180             {
2181               adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2182               adj1->sub_type.midchain.fixup_func (vm, adj1, p1);
2183             }
2184           if (is_mcast)
2185             {
2186               /*
2187                * copy bytes from the IP address into the MAC rewrite
2188                */
2189               vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2190               vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1, 0);
2191             }
2192
2193           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2194                                            to_next, n_left_to_next,
2195                                            pi0, pi1, next0, next1);
2196         }
2197
2198       while (n_left_from > 0 && n_left_to_next > 0)
2199         {
2200           ip_adjacency_t *adj0;
2201           vlib_buffer_t *p0;
2202           ip6_header_t *ip0;
2203           u32 pi0, rw_len0;
2204           u32 adj_index0, next0, error0;
2205           u32 tx_sw_if_index0;
2206
2207           pi0 = to_next[0] = from[0];
2208
2209           p0 = vlib_get_buffer (vm, pi0);
2210
2211           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2212
2213           /* We should never rewrite a pkt using the MISS adjacency */
2214           ASSERT (adj_index0);
2215
2216           adj0 = ip_get_adjacency (lm, adj_index0);
2217
2218           ip0 = vlib_buffer_get_current (p0);
2219
2220           error0 = IP6_ERROR_NONE;
2221           next0 = IP6_REWRITE_NEXT_DROP;
2222
2223           /* Check hop limit */
2224           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_LOCALLY_ORIGINATED)))
2225             {
2226               i32 hop_limit0 = ip0->hop_limit;
2227
2228               ASSERT (ip0->hop_limit > 0);
2229
2230               hop_limit0 -= 1;
2231
2232               ip0->hop_limit = hop_limit0;
2233
2234               if (PREDICT_FALSE (hop_limit0 <= 0))
2235                 {
2236                   /*
2237                    * If the hop count drops below 1 when forwarding, generate
2238                    * an ICMP response.
2239                    */
2240                   error0 = IP6_ERROR_TIME_EXPIRED;
2241                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2242                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2243                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2244                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
2245                                                0);
2246                 }
2247             }
2248           else
2249             {
2250               p0->flags &= ~VNET_BUFFER_LOCALLY_ORIGINATED;
2251             }
2252
2253           /* Guess we are only writing on simple Ethernet header. */
2254           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2255
2256           /* Update packet buffer attributes/set output interface. */
2257           rw_len0 = adj0[0].rewrite_header.data_bytes;
2258           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2259
2260           vlib_increment_combined_counter
2261             (&adjacency_counters,
2262              cpu_index,
2263              adj_index0, 1, vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2264
2265           /* Check MTU of outgoing interface. */
2266           error0 =
2267             (vlib_buffer_length_in_chain (vm, p0) >
2268              adj0[0].
2269              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2270              error0);
2271
2272           /* Don't adjust the buffer for hop count issue; icmp-error node
2273            * wants to see the IP headerr */
2274           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2275             {
2276               p0->current_data -= rw_len0;
2277               p0->current_length += rw_len0;
2278
2279               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2280
2281               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2282               next0 = adj0[0].rewrite_header.next_index;
2283
2284               vnet_feature_arc_start (lm->output_feature_arc_index,
2285                                       tx_sw_if_index0, &next0, p0);
2286             }
2287
2288           if (is_midchain)
2289             {
2290               adj0->sub_type.midchain.fixup_func (vm, adj0, p0);
2291             }
2292           if (is_mcast)
2293             {
2294               vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0, 0);
2295             }
2296
2297           p0->error = error_node->errors[error0];
2298
2299           from += 1;
2300           n_left_from -= 1;
2301           to_next += 1;
2302           n_left_to_next -= 1;
2303
2304           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2305                                            to_next, n_left_to_next,
2306                                            pi0, next0);
2307         }
2308
2309       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2310     }
2311
2312   /* Need to do trace after rewrites to pick up new packet data. */
2313   if (node->flags & VLIB_NODE_FLAG_TRACE)
2314     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2315
2316   return frame->n_vectors;
2317 }
2318
2319 static uword
2320 ip6_rewrite (vlib_main_t * vm,
2321              vlib_node_runtime_t * node, vlib_frame_t * frame)
2322 {
2323   return ip6_rewrite_inline (vm, node, frame, 0, 0);
2324 }
2325
2326 static uword
2327 ip6_rewrite_mcast (vlib_main_t * vm,
2328                    vlib_node_runtime_t * node, vlib_frame_t * frame)
2329 {
2330   return ip6_rewrite_inline (vm, node, frame, 0, 1);
2331 }
2332
2333 static uword
2334 ip6_midchain (vlib_main_t * vm,
2335               vlib_node_runtime_t * node, vlib_frame_t * frame)
2336 {
2337   return ip6_rewrite_inline (vm, node, frame, 1, 0);
2338 }
2339
2340 /* *INDENT-OFF* */
2341 VLIB_REGISTER_NODE (ip6_midchain_node) =
2342 {
2343   .function = ip6_midchain,
2344   .name = "ip6-midchain",
2345   .vector_size = sizeof (u32),
2346   .format_trace = format_ip6_forward_next_trace,
2347   .sibling_of = "ip6-rewrite",
2348   };
2349 /* *INDENT-ON* */
2350
2351 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2352
2353 /* *INDENT-OFF* */
2354 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2355 {
2356   .function = ip6_rewrite,
2357   .name = "ip6-rewrite",
2358   .vector_size = sizeof (u32),
2359   .format_trace = format_ip6_rewrite_trace,
2360   .n_next_nodes = 2,
2361   .next_nodes =
2362   {
2363     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2364     [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2365   },
2366 };
2367 /* *INDENT-ON* */
2368
2369 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2370
2371 /* *INDENT-OFF* */
2372 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2373 {
2374   .function = ip6_rewrite_mcast,
2375   .name = "ip6-rewrite-mcast",
2376   .vector_size = sizeof (u32),
2377   .format_trace = format_ip6_rewrite_trace,
2378   .sibling_of = "ip6-rewrite",
2379 };
2380 /* *INDENT-ON* */
2381
2382 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2383
2384 /*
2385  * Hop-by-Hop handling
2386  */
2387 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2388
2389 #define foreach_ip6_hop_by_hop_error \
2390 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2391 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2392 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2393
2394 /* *INDENT-OFF* */
2395 typedef enum
2396 {
2397 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2398   foreach_ip6_hop_by_hop_error
2399 #undef _
2400   IP6_HOP_BY_HOP_N_ERROR,
2401 } ip6_hop_by_hop_error_t;
2402 /* *INDENT-ON* */
2403
2404 /*
2405  * Primary h-b-h handler trace support
2406  * We work pretty hard on the problem for obvious reasons
2407  */
2408 typedef struct
2409 {
2410   u32 next_index;
2411   u32 trace_len;
2412   u8 option_data[256];
2413 } ip6_hop_by_hop_trace_t;
2414
2415 vlib_node_registration_t ip6_hop_by_hop_node;
2416
2417 static char *ip6_hop_by_hop_error_strings[] = {
2418 #define _(sym,string) string,
2419   foreach_ip6_hop_by_hop_error
2420 #undef _
2421 };
2422
2423 static u8 *
2424 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2425 {
2426   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2427   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2428   ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2429   ip6_hop_by_hop_header_t *hbh0;
2430   ip6_hop_by_hop_option_t *opt0, *limit0;
2431   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2432
2433   u8 type0;
2434
2435   hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2436
2437   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2438               t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2439
2440   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2441   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2442
2443   while (opt0 < limit0)
2444     {
2445       type0 = opt0->type;
2446       switch (type0)
2447         {
2448         case 0:         /* Pad, just stop */
2449           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2450           break;
2451
2452         default:
2453           if (hm->trace[type0])
2454             {
2455               s = (*hm->trace[type0]) (s, opt0);
2456             }
2457           else
2458             {
2459               s =
2460                 format (s, "\n    unrecognized option %d length %d", type0,
2461                         opt0->length);
2462             }
2463           opt0 =
2464             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2465                                          sizeof (ip6_hop_by_hop_option_t));
2466           break;
2467         }
2468     }
2469   return s;
2470 }
2471
2472 always_inline u8
2473 ip6_scan_hbh_options (vlib_buffer_t * b0,
2474                       ip6_header_t * ip0,
2475                       ip6_hop_by_hop_header_t * hbh0,
2476                       ip6_hop_by_hop_option_t * opt0,
2477                       ip6_hop_by_hop_option_t * limit0, u32 * next0)
2478 {
2479   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2480   u8 type0;
2481   u8 error0 = 0;
2482
2483   while (opt0 < limit0)
2484     {
2485       type0 = opt0->type;
2486       switch (type0)
2487         {
2488         case 0:         /* Pad1 */
2489           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2490           continue;
2491         case 1:         /* PadN */
2492           break;
2493         default:
2494           if (hm->options[type0])
2495             {
2496               if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2497                 {
2498                   error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2499                   return (error0);
2500                 }
2501             }
2502           else
2503             {
2504               /* Unrecognized mandatory option, check the two high order bits */
2505               switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2506                 {
2507                 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2508                   break;
2509                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2510                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2511                   *next0 = IP_LOOKUP_NEXT_DROP;
2512                   break;
2513                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2514                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2515                   *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2516                   icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2517                                                ICMP6_parameter_problem_unrecognized_option,
2518                                                (u8 *) opt0 - (u8 *) ip0);
2519                   break;
2520                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2521                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2522                   if (!ip6_address_is_multicast (&ip0->dst_address))
2523                     {
2524                       *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2525                       icmp6_error_set_vnet_buffer (b0,
2526                                                    ICMP6_parameter_problem,
2527                                                    ICMP6_parameter_problem_unrecognized_option,
2528                                                    (u8 *) opt0 - (u8 *) ip0);
2529                     }
2530                   else
2531                     {
2532                       *next0 = IP_LOOKUP_NEXT_DROP;
2533                     }
2534                   break;
2535                 }
2536               return (error0);
2537             }
2538         }
2539       opt0 =
2540         (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2541                                      sizeof (ip6_hop_by_hop_option_t));
2542     }
2543   return (error0);
2544 }
2545
2546 /*
2547  * Process the Hop-by-Hop Options header
2548  */
2549 static uword
2550 ip6_hop_by_hop (vlib_main_t * vm,
2551                 vlib_node_runtime_t * node, vlib_frame_t * frame)
2552 {
2553   vlib_node_runtime_t *error_node =
2554     vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2555   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2556   u32 n_left_from, *from, *to_next;
2557   ip_lookup_next_t next_index;
2558   ip6_main_t *im = &ip6_main;
2559   ip_lookup_main_t *lm = &im->lookup_main;
2560
2561   from = vlib_frame_vector_args (frame);
2562   n_left_from = frame->n_vectors;
2563   next_index = node->cached_next_index;
2564
2565   while (n_left_from > 0)
2566     {
2567       u32 n_left_to_next;
2568
2569       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2570
2571       while (n_left_from >= 4 && n_left_to_next >= 2)
2572         {
2573           u32 bi0, bi1;
2574           vlib_buffer_t *b0, *b1;
2575           u32 next0, next1;
2576           ip6_header_t *ip0, *ip1;
2577           ip6_hop_by_hop_header_t *hbh0, *hbh1;
2578           ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2579           u8 error0 = 0, error1 = 0;
2580
2581           /* Prefetch next iteration. */
2582           {
2583             vlib_buffer_t *p2, *p3;
2584
2585             p2 = vlib_get_buffer (vm, from[2]);
2586             p3 = vlib_get_buffer (vm, from[3]);
2587
2588             vlib_prefetch_buffer_header (p2, LOAD);
2589             vlib_prefetch_buffer_header (p3, LOAD);
2590
2591             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2592             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2593           }
2594
2595           /* Speculatively enqueue b0, b1 to the current next frame */
2596           to_next[0] = bi0 = from[0];
2597           to_next[1] = bi1 = from[1];
2598           from += 2;
2599           to_next += 2;
2600           n_left_from -= 2;
2601           n_left_to_next -= 2;
2602
2603           b0 = vlib_get_buffer (vm, bi0);
2604           b1 = vlib_get_buffer (vm, bi1);
2605
2606           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2607           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2608           ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2609           u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2610           ip_adjacency_t *adj1 = ip_get_adjacency (lm, adj_index1);
2611
2612           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2613           next0 = adj0->lookup_next_index;
2614           next1 = adj1->lookup_next_index;
2615
2616           ip0 = vlib_buffer_get_current (b0);
2617           ip1 = vlib_buffer_get_current (b1);
2618           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2619           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2620           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2621           opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2622           limit0 =
2623             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2624                                          ((hbh0->length + 1) << 3));
2625           limit1 =
2626             (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2627                                          ((hbh1->length + 1) << 3));
2628
2629           /*
2630            * Basic validity checks
2631            */
2632           if ((hbh0->length + 1) << 3 >
2633               clib_net_to_host_u16 (ip0->payload_length))
2634             {
2635               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2636               next0 = IP_LOOKUP_NEXT_DROP;
2637               goto outdual;
2638             }
2639           /* Scan the set of h-b-h options, process ones that we understand */
2640           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2641
2642           if ((hbh1->length + 1) << 3 >
2643               clib_net_to_host_u16 (ip1->payload_length))
2644             {
2645               error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2646               next1 = IP_LOOKUP_NEXT_DROP;
2647               goto outdual;
2648             }
2649           /* Scan the set of h-b-h options, process ones that we understand */
2650           error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2651
2652         outdual:
2653           /* Has the classifier flagged this buffer for special treatment? */
2654           if (PREDICT_FALSE
2655               ((error0 == 0)
2656                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2657             next0 = hm->next_override;
2658
2659           /* Has the classifier flagged this buffer for special treatment? */
2660           if (PREDICT_FALSE
2661               ((error1 == 0)
2662                && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2663             next1 = hm->next_override;
2664
2665           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2666             {
2667               if (b0->flags & VLIB_BUFFER_IS_TRACED)
2668                 {
2669                   ip6_hop_by_hop_trace_t *t =
2670                     vlib_add_trace (vm, node, b0, sizeof (*t));
2671                   u32 trace_len = (hbh0->length + 1) << 3;
2672                   t->next_index = next0;
2673                   /* Capture the h-b-h option verbatim */
2674                   trace_len =
2675                     trace_len <
2676                     ARRAY_LEN (t->option_data) ? trace_len :
2677                     ARRAY_LEN (t->option_data);
2678                   t->trace_len = trace_len;
2679                   clib_memcpy (t->option_data, hbh0, trace_len);
2680                 }
2681               if (b1->flags & VLIB_BUFFER_IS_TRACED)
2682                 {
2683                   ip6_hop_by_hop_trace_t *t =
2684                     vlib_add_trace (vm, node, b1, sizeof (*t));
2685                   u32 trace_len = (hbh1->length + 1) << 3;
2686                   t->next_index = next1;
2687                   /* Capture the h-b-h option verbatim */
2688                   trace_len =
2689                     trace_len <
2690                     ARRAY_LEN (t->option_data) ? trace_len :
2691                     ARRAY_LEN (t->option_data);
2692                   t->trace_len = trace_len;
2693                   clib_memcpy (t->option_data, hbh1, trace_len);
2694                 }
2695
2696             }
2697
2698           b0->error = error_node->errors[error0];
2699           b1->error = error_node->errors[error1];
2700
2701           /* verify speculative enqueue, maybe switch current next frame */
2702           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2703                                            n_left_to_next, bi0, bi1, next0,
2704                                            next1);
2705         }
2706
2707       while (n_left_from > 0 && n_left_to_next > 0)
2708         {
2709           u32 bi0;
2710           vlib_buffer_t *b0;
2711           u32 next0;
2712           ip6_header_t *ip0;
2713           ip6_hop_by_hop_header_t *hbh0;
2714           ip6_hop_by_hop_option_t *opt0, *limit0;
2715           u8 error0 = 0;
2716
2717           /* Speculatively enqueue b0 to the current next frame */
2718           bi0 = from[0];
2719           to_next[0] = bi0;
2720           from += 1;
2721           to_next += 1;
2722           n_left_from -= 1;
2723           n_left_to_next -= 1;
2724
2725           b0 = vlib_get_buffer (vm, bi0);
2726           /*
2727            * Default use the next_index from the adjacency.
2728            * A HBH option rarely redirects to a different node
2729            */
2730           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2731           ip_adjacency_t *adj0 = ip_get_adjacency (lm, adj_index0);
2732           next0 = adj0->lookup_next_index;
2733
2734           ip0 = vlib_buffer_get_current (b0);
2735           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2736           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2737           limit0 =
2738             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2739                                          ((hbh0->length + 1) << 3));
2740
2741           /*
2742            * Basic validity checks
2743            */
2744           if ((hbh0->length + 1) << 3 >
2745               clib_net_to_host_u16 (ip0->payload_length))
2746             {
2747               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2748               next0 = IP_LOOKUP_NEXT_DROP;
2749               goto out0;
2750             }
2751
2752           /* Scan the set of h-b-h options, process ones that we understand */
2753           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2754
2755         out0:
2756           /* Has the classifier flagged this buffer for special treatment? */
2757           if (PREDICT_FALSE
2758               ((error0 == 0)
2759                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2760             next0 = hm->next_override;
2761
2762           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2763             {
2764               ip6_hop_by_hop_trace_t *t =
2765                 vlib_add_trace (vm, node, b0, sizeof (*t));
2766               u32 trace_len = (hbh0->length + 1) << 3;
2767               t->next_index = next0;
2768               /* Capture the h-b-h option verbatim */
2769               trace_len =
2770                 trace_len <
2771                 ARRAY_LEN (t->option_data) ? trace_len :
2772                 ARRAY_LEN (t->option_data);
2773               t->trace_len = trace_len;
2774               clib_memcpy (t->option_data, hbh0, trace_len);
2775             }
2776
2777           b0->error = error_node->errors[error0];
2778
2779           /* verify speculative enqueue, maybe switch current next frame */
2780           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2781                                            n_left_to_next, bi0, next0);
2782         }
2783       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2784     }
2785   return frame->n_vectors;
2786 }
2787
2788 /* *INDENT-OFF* */
2789 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2790 {
2791   .function = ip6_hop_by_hop,
2792   .name = "ip6-hop-by-hop",
2793   .sibling_of = "ip6-lookup",
2794   .vector_size = sizeof (u32),
2795   .format_trace = format_ip6_hop_by_hop_trace,
2796   .type = VLIB_NODE_TYPE_INTERNAL,
2797   .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2798   .error_strings = ip6_hop_by_hop_error_strings,
2799   .n_next_nodes = 0,
2800 };
2801 /* *INDENT-ON* */
2802
2803 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2804
2805 static clib_error_t *
2806 ip6_hop_by_hop_init (vlib_main_t * vm)
2807 {
2808   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2809   memset (hm->options, 0, sizeof (hm->options));
2810   memset (hm->trace, 0, sizeof (hm->trace));
2811   hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2812   return (0);
2813 }
2814
2815 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2816
2817 void
2818 ip6_hbh_set_next_override (uword next)
2819 {
2820   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2821
2822   hm->next_override = next;
2823 }
2824
2825 int
2826 ip6_hbh_register_option (u8 option,
2827                          int options (vlib_buffer_t * b, ip6_header_t * ip,
2828                                       ip6_hop_by_hop_option_t * opt),
2829                          u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2830 {
2831   ip6_main_t *im = &ip6_main;
2832   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2833
2834   ASSERT (option < ARRAY_LEN (hm->options));
2835
2836   /* Already registered */
2837   if (hm->options[option])
2838     return (-1);
2839
2840   hm->options[option] = options;
2841   hm->trace[option] = trace;
2842
2843   /* Set global variable */
2844   im->hbh_enabled = 1;
2845
2846   return (0);
2847 }
2848
2849 int
2850 ip6_hbh_unregister_option (u8 option)
2851 {
2852   ip6_main_t *im = &ip6_main;
2853   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2854
2855   ASSERT (option < ARRAY_LEN (hm->options));
2856
2857   /* Not registered */
2858   if (!hm->options[option])
2859     return (-1);
2860
2861   hm->options[option] = NULL;
2862   hm->trace[option] = NULL;
2863
2864   /* Disable global knob if this was the last option configured */
2865   int i;
2866   bool found = false;
2867   for (i = 0; i < 256; i++)
2868     {
2869       if (hm->options[option])
2870         {
2871           found = true;
2872           break;
2873         }
2874     }
2875   if (!found)
2876     im->hbh_enabled = 0;
2877
2878   return (0);
2879 }
2880
2881 /* Global IP6 main. */
2882 ip6_main_t ip6_main;
2883
2884 static clib_error_t *
2885 ip6_lookup_init (vlib_main_t * vm)
2886 {
2887   ip6_main_t *im = &ip6_main;
2888   clib_error_t *error;
2889   uword i;
2890
2891   if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2892     return error;
2893
2894   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2895     {
2896       u32 j, i0, i1;
2897
2898       i0 = i / 32;
2899       i1 = i % 32;
2900
2901       for (j = 0; j < i0; j++)
2902         im->fib_masks[i].as_u32[j] = ~0;
2903
2904       if (i1)
2905         im->fib_masks[i].as_u32[i0] =
2906           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2907     }
2908
2909   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2910
2911   if (im->lookup_table_nbuckets == 0)
2912     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2913
2914   im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2915
2916   if (im->lookup_table_size == 0)
2917     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2918
2919   BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2920                          "ip6 FIB fwding table",
2921                          im->lookup_table_nbuckets, im->lookup_table_size);
2922   BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2923                          "ip6 FIB non-fwding table",
2924                          im->lookup_table_nbuckets, im->lookup_table_size);
2925
2926   /* Create FIB with index 0 and table id of 0. */
2927   fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2928   mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0);
2929
2930   {
2931     pg_node_t *pn;
2932     pn = pg_get_node (ip6_lookup_node.index);
2933     pn->unformat_edit = unformat_pg_ip6_header;
2934   }
2935
2936   /* Unless explicitly configured, don't process HBH options */
2937   im->hbh_enabled = 0;
2938
2939   {
2940     icmp6_neighbor_solicitation_header_t p;
2941
2942     memset (&p, 0, sizeof (p));
2943
2944     p.ip.ip_version_traffic_class_and_flow_label =
2945       clib_host_to_net_u32 (0x6 << 28);
2946     p.ip.payload_length =
2947       clib_host_to_net_u16 (sizeof (p) -
2948                             STRUCT_OFFSET_OF
2949                             (icmp6_neighbor_solicitation_header_t, neighbor));
2950     p.ip.protocol = IP_PROTOCOL_ICMP6;
2951     p.ip.hop_limit = 255;
2952     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2953
2954     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2955
2956     p.link_layer_option.header.type =
2957       ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2958     p.link_layer_option.header.n_data_u64s =
2959       sizeof (p.link_layer_option) / sizeof (u64);
2960
2961     vlib_packet_template_init (vm,
2962                                &im->discover_neighbor_packet_template,
2963                                &p, sizeof (p),
2964                                /* alloc chunk size */ 8,
2965                                "ip6 neighbor discovery");
2966   }
2967
2968   return error;
2969 }
2970
2971 VLIB_INIT_FUNCTION (ip6_lookup_init);
2972
2973 static clib_error_t *
2974 add_del_ip6_interface_table (vlib_main_t * vm,
2975                              unformat_input_t * input,
2976                              vlib_cli_command_t * cmd)
2977 {
2978   vnet_main_t *vnm = vnet_get_main ();
2979   clib_error_t *error = 0;
2980   u32 sw_if_index, table_id;
2981
2982   sw_if_index = ~0;
2983
2984   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2985     {
2986       error = clib_error_return (0, "unknown interface `%U'",
2987                                  format_unformat_error, input);
2988       goto done;
2989     }
2990
2991   if (unformat (input, "%d", &table_id))
2992     ;
2993   else
2994     {
2995       error = clib_error_return (0, "expected table id `%U'",
2996                                  format_unformat_error, input);
2997       goto done;
2998     }
2999
3000   {
3001     u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3002                                                        table_id);
3003
3004     vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
3005     ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
3006
3007     fib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3008                                                     table_id);
3009
3010     vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
3011     ip6_main.mfib_index_by_sw_if_index[sw_if_index] = fib_index;
3012   }
3013
3014
3015 done:
3016   return error;
3017 }
3018
3019 /*?
3020  * Place the indicated interface into the supplied IPv6 FIB table (also known
3021  * as a VRF). If the FIB table does not exist, this command creates it. To
3022  * display the current IPv6 FIB table, use the command '<em>show ip6 fib</em>'.
3023  * FIB table will only be displayed if a route has been added to the table, or
3024  * an IP Address is assigned to an interface in the table (which adds a route
3025  * automatically).
3026  *
3027  * @note IP addresses added after setting the interface IP table end up in
3028  * the indicated FIB table. If the IP address is added prior to adding the
3029  * interface to the FIB table, it will NOT be part of the FIB table. Predictable
3030  * but potentially counter-intuitive results occur if you provision interface
3031  * addresses in multiple FIBs. Upon RX, packets will be processed in the last
3032  * IP table ID provisioned. It might be marginally useful to evade source RPF
3033  * drops to put an interface address into multiple FIBs.
3034  *
3035  * @cliexpar
3036  * Example of how to add an interface to an IPv6 FIB table (where 2 is the table-id):
3037  * @cliexcmd{set interface ip6 table GigabitEthernet2/0/0 2}
3038  ?*/
3039 /* *INDENT-OFF* */
3040 VLIB_CLI_COMMAND (set_interface_ip6_table_command, static) =
3041 {
3042   .path = "set interface ip6 table",
3043   .function = add_del_ip6_interface_table,
3044   .short_help = "set interface ip6 table <interface> <table-id>"
3045 };
3046 /* *INDENT-ON* */
3047
3048 void
3049 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
3050                                                   u8 * mac)
3051 {
3052   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
3053   /* Invert the "u" bit */
3054   ip->as_u8[8] = mac[0] ^ (1 << 1);
3055   ip->as_u8[9] = mac[1];
3056   ip->as_u8[10] = mac[2];
3057   ip->as_u8[11] = 0xFF;
3058   ip->as_u8[12] = 0xFE;
3059   ip->as_u8[13] = mac[3];
3060   ip->as_u8[14] = mac[4];
3061   ip->as_u8[15] = mac[5];
3062 }
3063
3064 void
3065 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
3066                                                   ip6_address_t * ip)
3067 {
3068   /* Invert the previously inverted "u" bit */
3069   mac[0] = ip->as_u8[8] ^ (1 << 1);
3070   mac[1] = ip->as_u8[9];
3071   mac[2] = ip->as_u8[10];
3072   mac[3] = ip->as_u8[13];
3073   mac[4] = ip->as_u8[14];
3074   mac[5] = ip->as_u8[15];
3075 }
3076
3077 static clib_error_t *
3078 test_ip6_link_command_fn (vlib_main_t * vm,
3079                           unformat_input_t * input, vlib_cli_command_t * cmd)
3080 {
3081   u8 mac[6];
3082   ip6_address_t _a, *a = &_a;
3083
3084   if (unformat (input, "%U", unformat_ethernet_address, mac))
3085     {
3086       ip6_link_local_address_from_ethernet_mac_address (a, mac);
3087       vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
3088       ip6_ethernet_mac_address_from_link_local_address (mac, a);
3089       vlib_cli_output (vm, "Original MAC address: %U",
3090                        format_ethernet_address, mac);
3091     }
3092
3093   return 0;
3094 }
3095
3096 /*?
3097  * This command converts the given MAC Address into an IPv6 link-local
3098  * address.
3099  *
3100  * @cliexpar
3101  * Example of how to create an IPv6 link-local address:
3102  * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
3103  * Link local address: fe80::14d9:e0ff:fe91:7986
3104  * Original MAC address: 16:d9:e0:91:79:86
3105  * @cliexend
3106 ?*/
3107 /* *INDENT-OFF* */
3108 VLIB_CLI_COMMAND (test_link_command, static) =
3109 {
3110   .path = "test ip6 link",
3111   .function = test_ip6_link_command_fn,
3112   .short_help = "test ip6 link <mac-address>",
3113 };
3114 /* *INDENT-ON* */
3115
3116 int
3117 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
3118 {
3119   ip6_main_t *im6 = &ip6_main;
3120   ip6_fib_t *fib;
3121   uword *p = hash_get (im6->fib_index_by_table_id, table_id);
3122
3123   if (p == 0)
3124     return -1;
3125
3126   fib = ip6_fib_get (p[0]);
3127
3128   fib->flow_hash_config = flow_hash_config;
3129   return 1;
3130 }
3131
3132 static clib_error_t *
3133 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
3134                               unformat_input_t * input,
3135                               vlib_cli_command_t * cmd)
3136 {
3137   int matched = 0;
3138   u32 table_id = 0;
3139   u32 flow_hash_config = 0;
3140   int rv;
3141
3142   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3143     {
3144       if (unformat (input, "table %d", &table_id))
3145         matched = 1;
3146 #define _(a,v) \
3147     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3148       foreach_flow_hash_bit
3149 #undef _
3150         else
3151         break;
3152     }
3153
3154   if (matched == 0)
3155     return clib_error_return (0, "unknown input `%U'",
3156                               format_unformat_error, input);
3157
3158   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3159   switch (rv)
3160     {
3161     case 1:
3162       break;
3163
3164     case -1:
3165       return clib_error_return (0, "no such FIB table %d", table_id);
3166
3167     default:
3168       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3169       break;
3170     }
3171
3172   return 0;
3173 }
3174
3175 /*?
3176  * Configure the set of IPv6 fields used by the flow hash.
3177  *
3178  * @cliexpar
3179  * @parblock
3180  * Example of how to set the flow hash on a given table:
3181  * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
3182  *
3183  * Example of display the configured flow hash:
3184  * @cliexstart{show ip6 fib}
3185  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
3186  * @::/0
3187  *   unicast-ip6-chain
3188  *   [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
3189  *     [0] [@0]: dpo-drop ip6
3190  * fe80::/10
3191  *   unicast-ip6-chain
3192  *   [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
3193  *     [0] [@2]: dpo-receive
3194  * ff02::1/128
3195  *   unicast-ip6-chain
3196  *   [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
3197  *     [0] [@2]: dpo-receive
3198  * ff02::2/128
3199  *   unicast-ip6-chain
3200  *   [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
3201  *     [0] [@2]: dpo-receive
3202  * ff02::16/128
3203  *   unicast-ip6-chain
3204  *   [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
3205  *     [0] [@2]: dpo-receive
3206  * ff02::1:ff00:0/104
3207  *   unicast-ip6-chain
3208  *   [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3209  *     [0] [@2]: dpo-receive
3210  * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3211  * @::/0
3212  *   unicast-ip6-chain
3213  *   [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3214  *     [0] [@0]: dpo-drop ip6
3215  * @::a:1:1:0:4/126
3216  *   unicast-ip6-chain
3217  *   [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3218  *     [0] [@4]: ipv6-glean: af_packet0
3219  * @::a:1:1:0:7/128
3220  *   unicast-ip6-chain
3221  *   [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3222  *     [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3223  * fe80::/10
3224  *   unicast-ip6-chain
3225  *   [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3226  *     [0] [@2]: dpo-receive
3227  * fe80::fe:3eff:fe3e:9222/128
3228  *   unicast-ip6-chain
3229  *   [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3230  *     [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3231  * ff02::1/128
3232  *   unicast-ip6-chain
3233  *   [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3234  *     [0] [@2]: dpo-receive
3235  * ff02::2/128
3236  *   unicast-ip6-chain
3237  *   [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3238  *     [0] [@2]: dpo-receive
3239  * ff02::16/128
3240  *   unicast-ip6-chain
3241  *   [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3242  *     [0] [@2]: dpo-receive
3243  * ff02::1:ff00:0/104
3244  *   unicast-ip6-chain
3245  *   [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3246  *     [0] [@2]: dpo-receive
3247  * @cliexend
3248  * @endparblock
3249 ?*/
3250 /* *INDENT-OFF* */
3251 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3252 {
3253   .path = "set ip6 flow-hash",
3254   .short_help =
3255   "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3256   .function = set_ip6_flow_hash_command_fn,
3257 };
3258 /* *INDENT-ON* */
3259
3260 static clib_error_t *
3261 show_ip6_local_command_fn (vlib_main_t * vm,
3262                            unformat_input_t * input, vlib_cli_command_t * cmd)
3263 {
3264   ip6_main_t *im = &ip6_main;
3265   ip_lookup_main_t *lm = &im->lookup_main;
3266   int i;
3267
3268   vlib_cli_output (vm, "Protocols handled by ip6_local");
3269   for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3270     {
3271       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3272         vlib_cli_output (vm, "%d", i);
3273     }
3274   return 0;
3275 }
3276
3277
3278
3279 /*?
3280  * Display the set of protocols handled by the local IPv6 stack.
3281  *
3282  * @cliexpar
3283  * Example of how to display local protocol table:
3284  * @cliexstart{show ip6 local}
3285  * Protocols handled by ip6_local
3286  * 17
3287  * 43
3288  * 58
3289  * 115
3290  * @cliexend
3291 ?*/
3292 /* *INDENT-OFF* */
3293 VLIB_CLI_COMMAND (show_ip6_local, static) =
3294 {
3295   .path = "show ip6 local",
3296   .function = show_ip6_local_command_fn,
3297   .short_help = "show ip6 local",
3298 };
3299 /* *INDENT-ON* */
3300
3301 int
3302 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3303                              u32 table_index)
3304 {
3305   vnet_main_t *vnm = vnet_get_main ();
3306   vnet_interface_main_t *im = &vnm->interface_main;
3307   ip6_main_t *ipm = &ip6_main;
3308   ip_lookup_main_t *lm = &ipm->lookup_main;
3309   vnet_classify_main_t *cm = &vnet_classify_main;
3310   ip6_address_t *if_addr;
3311
3312   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3313     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3314
3315   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3316     return VNET_API_ERROR_NO_SUCH_ENTRY;
3317
3318   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3319   lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3320
3321   if_addr = ip6_interface_first_address (ipm, sw_if_index, NULL);
3322
3323   if (NULL != if_addr)
3324     {
3325       fib_prefix_t pfx = {
3326         .fp_len = 128,
3327         .fp_proto = FIB_PROTOCOL_IP6,
3328         .fp_addr.ip6 = *if_addr,
3329       };
3330       u32 fib_index;
3331
3332       fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3333                                                        sw_if_index);
3334
3335
3336       if (table_index != (u32) ~ 0)
3337         {
3338           dpo_id_t dpo = DPO_INVALID;
3339
3340           dpo_set (&dpo,
3341                    DPO_CLASSIFY,
3342                    DPO_PROTO_IP6,
3343                    classify_dpo_create (DPO_PROTO_IP6, table_index));
3344
3345           fib_table_entry_special_dpo_add (fib_index,
3346                                            &pfx,
3347                                            FIB_SOURCE_CLASSIFY,
3348                                            FIB_ENTRY_FLAG_NONE, &dpo);
3349           dpo_reset (&dpo);
3350         }
3351       else
3352         {
3353           fib_table_entry_special_remove (fib_index,
3354                                           &pfx, FIB_SOURCE_CLASSIFY);
3355         }
3356     }
3357
3358   return 0;
3359 }
3360
3361 static clib_error_t *
3362 set_ip6_classify_command_fn (vlib_main_t * vm,
3363                              unformat_input_t * input,
3364                              vlib_cli_command_t * cmd)
3365 {
3366   u32 table_index = ~0;
3367   int table_index_set = 0;
3368   u32 sw_if_index = ~0;
3369   int rv;
3370
3371   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3372     {
3373       if (unformat (input, "table-index %d", &table_index))
3374         table_index_set = 1;
3375       else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3376                          vnet_get_main (), &sw_if_index))
3377         ;
3378       else
3379         break;
3380     }
3381
3382   if (table_index_set == 0)
3383     return clib_error_return (0, "classify table-index must be specified");
3384
3385   if (sw_if_index == ~0)
3386     return clib_error_return (0, "interface / subif must be specified");
3387
3388   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3389
3390   switch (rv)
3391     {
3392     case 0:
3393       break;
3394
3395     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3396       return clib_error_return (0, "No such interface");
3397
3398     case VNET_API_ERROR_NO_SUCH_ENTRY:
3399       return clib_error_return (0, "No such classifier table");
3400     }
3401   return 0;
3402 }
3403
3404 /*?
3405  * Assign a classification table to an interface. The classification
3406  * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3407  * commands. Once the table is create, use this command to filter packets
3408  * on an interface.
3409  *
3410  * @cliexpar
3411  * Example of how to assign a classification table to an interface:
3412  * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3413 ?*/
3414 /* *INDENT-OFF* */
3415 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3416 {
3417   .path = "set ip6 classify",
3418   .short_help =
3419   "set ip6 classify intfc <interface> table-index <classify-idx>",
3420   .function = set_ip6_classify_command_fn,
3421 };
3422 /* *INDENT-ON* */
3423
3424 static clib_error_t *
3425 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3426 {
3427   ip6_main_t *im = &ip6_main;
3428   uword heapsize = 0;
3429   u32 tmp;
3430   u32 nbuckets = 0;
3431
3432   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3433     {
3434       if (unformat (input, "hash-buckets %d", &tmp))
3435         nbuckets = tmp;
3436       else if (unformat (input, "heap-size %dm", &tmp))
3437         heapsize = ((u64) tmp) << 20;
3438       else if (unformat (input, "heap-size %dM", &tmp))
3439         heapsize = ((u64) tmp) << 20;
3440       else if (unformat (input, "heap-size %dg", &tmp))
3441         heapsize = ((u64) tmp) << 30;
3442       else if (unformat (input, "heap-size %dG", &tmp))
3443         heapsize = ((u64) tmp) << 30;
3444       else
3445         return clib_error_return (0, "unknown input '%U'",
3446                                   format_unformat_error, input);
3447     }
3448
3449   im->lookup_table_nbuckets = nbuckets;
3450   im->lookup_table_size = heapsize;
3451
3452   return 0;
3453 }
3454
3455 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3456
3457 /*
3458  * fd.io coding-style-patch-verification: ON
3459  *
3460  * Local Variables:
3461  * eval: (c-set-style "gnu")
3462  * End:
3463  */