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