tap gso: experimental support
[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 always_inline 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   u32 *from, n_left_from;
1103   vlib_node_runtime_t *error_node =
1104     vlib_node_get_runtime (vm, ip6_input_node.index);
1105   u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1106   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1107   u16 nexts[VLIB_FRAME_SIZE], *next;
1108
1109   from = vlib_frame_vector_args (frame);
1110   n_left_from = frame->n_vectors;
1111
1112   if (node->flags & VLIB_NODE_FLAG_TRACE)
1113     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1114
1115   vlib_get_buffers (vm, from, bufs, n_left_from);
1116   b = bufs;
1117   next = nexts;
1118
1119   while (n_left_from > 2)
1120     {
1121       /* Prefetch next iteration. */
1122       if (n_left_from >= 6)
1123         {
1124           vlib_prefetch_buffer_header (b[4], STORE);
1125           vlib_prefetch_buffer_header (b[5], STORE);
1126           vlib_prefetch_buffer_data (b[2], LOAD);
1127           vlib_prefetch_buffer_data (b[3], LOAD);
1128         }
1129
1130       u8 error[2];
1131       error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1132       error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1133
1134       ip6_header_t *ip[2];
1135       ip[0] = vlib_buffer_get_current (b[0]);
1136       ip[1] = vlib_buffer_get_current (b[1]);
1137
1138       if (head_of_feature_arc)
1139         {
1140           vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1141           vnet_buffer (b[1])->l3_hdr_offset = b[1]->current_data;
1142
1143           u8 type[2];
1144           type[0] = lm->builtin_protocol_by_ip_protocol[ip[0]->protocol];
1145           type[1] = lm->builtin_protocol_by_ip_protocol[ip[1]->protocol];
1146
1147           u32 flags[2];
1148           flags[0] = b[0]->flags;
1149           flags[1] = b[1]->flags;
1150
1151           u32 good_l4_csum[2];
1152           good_l4_csum[0] =
1153             flags[0] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1154                         VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1155                         VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1156           good_l4_csum[1] =
1157             flags[1] & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1158                         VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1159                         VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1160
1161           u32 udp_offset[2] = { };
1162           u8 is_tcp_udp[2];
1163           is_tcp_udp[0] =
1164             ip6_next_proto_is_tcp_udp (b[0], ip[0], &udp_offset[0]);
1165           is_tcp_udp[1] =
1166             ip6_next_proto_is_tcp_udp (b[1], ip[1], &udp_offset[1]);
1167           i16 len_diff[2] = { 0 };
1168           if (PREDICT_TRUE (is_tcp_udp[0]))
1169             {
1170               udp_header_t *udp =
1171                 (udp_header_t *) ((u8 *) ip[0] + udp_offset[0]);
1172               good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UDP
1173                 && udp->checksum == 0;
1174               /* optimistically verify UDP length. */
1175               u16 ip_len, udp_len;
1176               ip_len = clib_net_to_host_u16 (ip[0]->payload_length);
1177               udp_len = clib_net_to_host_u16 (udp->length);
1178               len_diff[0] = ip_len - udp_len;
1179             }
1180           if (PREDICT_TRUE (is_tcp_udp[1]))
1181             {
1182               udp_header_t *udp =
1183                 (udp_header_t *) ((u8 *) ip[1] + udp_offset[1]);
1184               good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UDP
1185                 && udp->checksum == 0;
1186               /* optimistically verify UDP length. */
1187               u16 ip_len, udp_len;
1188               ip_len = clib_net_to_host_u16 (ip[1]->payload_length);
1189               udp_len = clib_net_to_host_u16 (udp->length);
1190               len_diff[1] = ip_len - udp_len;
1191             }
1192
1193           good_l4_csum[0] |= type[0] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1194           good_l4_csum[1] |= type[1] == IP_BUILTIN_PROTOCOL_UNKNOWN;
1195
1196           len_diff[0] = type[0] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[0] : 0;
1197           len_diff[1] = type[1] == IP_BUILTIN_PROTOCOL_UDP ? len_diff[1] : 0;
1198
1199           u8 need_csum[2];
1200           need_csum[0] = type[0] != IP_BUILTIN_PROTOCOL_UNKNOWN
1201             && !good_l4_csum[0]
1202             && !(flags[0] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1203           need_csum[1] = type[1] != IP_BUILTIN_PROTOCOL_UNKNOWN
1204             && !good_l4_csum[1]
1205             && !(flags[1] & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1206           if (PREDICT_FALSE (need_csum[0]))
1207             {
1208               flags[0] = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1209               good_l4_csum[0] = flags[0] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1210             }
1211           if (PREDICT_FALSE (need_csum[1]))
1212             {
1213               flags[1] = ip6_tcp_udp_icmp_validate_checksum (vm, b[1]);
1214               good_l4_csum[1] = flags[1] & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1215             }
1216
1217           error[0] = IP6_ERROR_UNKNOWN_PROTOCOL;
1218           error[0] = len_diff[0] < 0 ? IP6_ERROR_UDP_LENGTH : error[0];
1219           error[1] = IP6_ERROR_UNKNOWN_PROTOCOL;
1220           error[1] = len_diff[1] < 0 ? IP6_ERROR_UDP_LENGTH : error[1];
1221
1222           STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1223                          IP6_ERROR_UDP_CHECKSUM,
1224                          "Wrong IP6 errors constants");
1225           STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1226                          IP6_ERROR_ICMP_CHECKSUM,
1227                          "Wrong IP6 errors constants");
1228
1229           error[0] =
1230             !good_l4_csum[0] ? IP6_ERROR_UDP_CHECKSUM + type[0] : error[0];
1231           error[1] =
1232             !good_l4_csum[1] ? IP6_ERROR_UDP_CHECKSUM + type[1] : error[1];
1233
1234           /* Drop packets from unroutable hosts. */
1235           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1236           u8 unroutable[2];
1237           unroutable[0] = error[0] == IP6_ERROR_UNKNOWN_PROTOCOL
1238             && type[0] != IP_BUILTIN_PROTOCOL_ICMP
1239             && !ip6_address_is_link_local_unicast (&ip[0]->src_address);
1240           unroutable[1] = error[1] == IP6_ERROR_UNKNOWN_PROTOCOL
1241             && type[1] != IP_BUILTIN_PROTOCOL_ICMP
1242             && !ip6_address_is_link_local_unicast (&ip[1]->src_address);
1243           if (PREDICT_FALSE (unroutable[0]))
1244             {
1245               error[0] =
1246                 !ip6_urpf_loose_check (im, b[0],
1247                                        ip[0]) ? IP6_ERROR_SRC_LOOKUP_MISS
1248                 : error[0];
1249             }
1250           if (PREDICT_FALSE (unroutable[1]))
1251             {
1252               error[1] =
1253                 !ip6_urpf_loose_check (im, b[1],
1254                                        ip[1]) ? IP6_ERROR_SRC_LOOKUP_MISS
1255                 : error[1];
1256             }
1257
1258           vnet_buffer (b[0])->ip.fib_index =
1259             vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1260             vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1261             vnet_buffer (b[0])->ip.fib_index;
1262           vnet_buffer (b[1])->ip.fib_index =
1263             vnet_buffer (b[1])->sw_if_index[VLIB_TX] != ~0 ?
1264             vnet_buffer (b[1])->sw_if_index[VLIB_TX] :
1265             vnet_buffer (b[1])->ip.fib_index;
1266         }                       /* head_of_feature_arc */
1267
1268       next[0] = lm->local_next_by_ip_protocol[ip[0]->protocol];
1269       next[0] =
1270         error[0] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1271       next[1] = lm->local_next_by_ip_protocol[ip[1]->protocol];
1272       next[1] =
1273         error[1] != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[1];
1274
1275       b[0]->error = error_node->errors[0];
1276       b[1]->error = error_node->errors[1];
1277
1278       if (head_of_feature_arc)
1279         {
1280           u8 ip6_unknown[2];
1281           ip6_unknown[0] = error[0] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1282           ip6_unknown[1] = error[1] == (u8) IP6_ERROR_UNKNOWN_PROTOCOL;
1283           if (PREDICT_TRUE (ip6_unknown[0]))
1284             {
1285               u32 next32 = next[0];
1286               vnet_feature_arc_start (arc_index,
1287                                       vnet_buffer (b[0])->sw_if_index
1288                                       [VLIB_RX], &next32, b[0]);
1289               next[0] = next32;
1290             }
1291           if (PREDICT_TRUE (ip6_unknown[1]))
1292             {
1293               u32 next32 = next[1];
1294               vnet_feature_arc_start (arc_index,
1295                                       vnet_buffer (b[1])->sw_if_index
1296                                       [VLIB_RX], &next32, b[1]);
1297               next[1] = next32;
1298             }
1299         }
1300
1301       /* next */
1302       b += 2;
1303       next += 2;
1304       n_left_from -= 2;
1305     }
1306
1307   while (n_left_from)
1308     {
1309       u8 error;
1310       error = IP6_ERROR_UNKNOWN_PROTOCOL;
1311
1312       ip6_header_t *ip;
1313       ip = vlib_buffer_get_current (b[0]);
1314
1315       if (head_of_feature_arc)
1316         {
1317           vnet_buffer (b[0])->l3_hdr_offset = b[0]->current_data;
1318           u8 type = lm->builtin_protocol_by_ip_protocol[ip->protocol];
1319
1320           u32 flags = b[0]->flags;
1321           u32 good_l4_csum =
1322             flags & (VNET_BUFFER_F_L4_CHECKSUM_CORRECT |
1323                      VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
1324                      VNET_BUFFER_F_OFFLOAD_UDP_CKSUM);
1325
1326           u32 udp_offset;
1327           i16 len_diff = 0;
1328           u8 is_tcp_udp = ip6_next_proto_is_tcp_udp (b[0], ip, &udp_offset);
1329           if (PREDICT_TRUE (is_tcp_udp))
1330             {
1331               udp_header_t *udp = (udp_header_t *) ((u8 *) ip + udp_offset);
1332               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1333               good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UDP
1334                 && udp->checksum == 0;
1335               /* optimistically verify UDP length. */
1336               u16 ip_len, udp_len;
1337               ip_len = clib_net_to_host_u16 (ip->payload_length);
1338               udp_len = clib_net_to_host_u16 (udp->length);
1339               len_diff = ip_len - udp_len;
1340             }
1341
1342           good_l4_csum |= type == IP_BUILTIN_PROTOCOL_UNKNOWN;
1343           len_diff = type == IP_BUILTIN_PROTOCOL_UDP ? len_diff : 0;
1344
1345           u8 need_csum = type != IP_BUILTIN_PROTOCOL_UNKNOWN && !good_l4_csum
1346             && !(flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED);
1347           if (PREDICT_FALSE (need_csum))
1348             {
1349               flags = ip6_tcp_udp_icmp_validate_checksum (vm, b[0]);
1350               good_l4_csum = flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT;
1351             }
1352
1353           error = IP6_ERROR_UNKNOWN_PROTOCOL;
1354           error = len_diff < 0 ? IP6_ERROR_UDP_LENGTH : error;
1355
1356           STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1357                          IP6_ERROR_UDP_CHECKSUM,
1358                          "Wrong IP6 errors constants");
1359           STATIC_ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1360                          IP6_ERROR_ICMP_CHECKSUM,
1361                          "Wrong IP6 errors constants");
1362
1363           error = !good_l4_csum ? IP6_ERROR_UDP_CHECKSUM + type : error;
1364
1365           /* Drop packets from unroutable hosts. */
1366           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1367           u8 unroutable = error == IP6_ERROR_UNKNOWN_PROTOCOL
1368             && type != IP_BUILTIN_PROTOCOL_ICMP
1369             && !ip6_address_is_link_local_unicast (&ip->src_address);
1370           if (PREDICT_FALSE (unroutable))
1371             {
1372               error =
1373                 !ip6_urpf_loose_check (im, b[0],
1374                                        ip) ? IP6_ERROR_SRC_LOOKUP_MISS :
1375                 error;
1376             }
1377
1378           vnet_buffer (b[0])->ip.fib_index =
1379             vnet_buffer (b[0])->sw_if_index[VLIB_TX] != ~0 ?
1380             vnet_buffer (b[0])->sw_if_index[VLIB_TX] :
1381             vnet_buffer (b[0])->ip.fib_index;
1382         }                       /* head_of_feature_arc */
1383
1384       next[0] = lm->local_next_by_ip_protocol[ip->protocol];
1385       next[0] =
1386         error != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next[0];
1387
1388       b[0]->error = error_node->errors[0];
1389
1390       if (head_of_feature_arc)
1391         {
1392           if (PREDICT_TRUE (error == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1393             {
1394               u32 next32 = next[0];
1395               vnet_feature_arc_start (arc_index,
1396                                       vnet_buffer (b[0])->sw_if_index
1397                                       [VLIB_RX], &next32, b[0]);
1398               next[0] = next32;
1399             }
1400         }
1401
1402       /* next */
1403       b += 1;
1404       next += 1;
1405       n_left_from -= 1;
1406     }
1407
1408   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
1409   return frame->n_vectors;
1410 }
1411
1412 VLIB_NODE_FN (ip6_local_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1413                                vlib_frame_t * frame)
1414 {
1415   return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1416 }
1417
1418 /* *INDENT-OFF* */
1419 VLIB_REGISTER_NODE (ip6_local_node) =
1420 {
1421   .name = "ip6-local",
1422   .vector_size = sizeof (u32),
1423   .format_trace = format_ip6_forward_next_trace,
1424   .n_next_nodes = IP_LOCAL_N_NEXT,
1425   .next_nodes =
1426   {
1427     [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1428     [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1429     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1430     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1431     [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
1432   },
1433 };
1434 /* *INDENT-ON* */
1435
1436 VLIB_NODE_FN (ip6_local_end_of_arc_node) (vlib_main_t * vm,
1437                                           vlib_node_runtime_t * node,
1438                                           vlib_frame_t * frame)
1439 {
1440   return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1441 }
1442
1443 /* *INDENT-OFF* */
1444 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node) = {
1445   .name = "ip6-local-end-of-arc",
1446   .vector_size = sizeof (u32),
1447
1448   .format_trace = format_ip6_forward_next_trace,
1449   .sibling_of = "ip6-local",
1450 };
1451
1452 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1453   .arc_name = "ip6-local",
1454   .node_name = "ip6-local-end-of-arc",
1455   .runs_before = 0, /* not before any other features */
1456 };
1457 /* *INDENT-ON* */
1458
1459 #ifdef CLIB_MARCH_VARIANT
1460 extern vlib_node_registration_t ip6_local_node;
1461
1462 #else
1463
1464 void
1465 ip6_register_protocol (u32 protocol, u32 node_index)
1466 {
1467   vlib_main_t *vm = vlib_get_main ();
1468   ip6_main_t *im = &ip6_main;
1469   ip_lookup_main_t *lm = &im->lookup_main;
1470
1471   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1472   lm->local_next_by_ip_protocol[protocol] =
1473     vlib_node_add_next (vm, ip6_local_node.index, node_index);
1474 }
1475
1476 clib_error_t *
1477 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index,
1478                     u8 refresh)
1479 {
1480   vnet_main_t *vnm = vnet_get_main ();
1481   ip6_main_t *im = &ip6_main;
1482   icmp6_neighbor_solicitation_header_t *h;
1483   ip6_address_t *src;
1484   ip_interface_address_t *ia;
1485   ip_adjacency_t *adj;
1486   vnet_hw_interface_t *hi;
1487   vnet_sw_interface_t *si;
1488   vlib_buffer_t *b;
1489   adj_index_t ai;
1490   u32 bi = 0;
1491   int bogus_length;
1492
1493   si = vnet_get_sw_interface (vnm, sw_if_index);
1494
1495   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1496     {
1497       return clib_error_return (0, "%U: interface %U down",
1498                                 format_ip6_address, dst,
1499                                 format_vnet_sw_if_index_name, vnm,
1500                                 sw_if_index);
1501     }
1502
1503   src =
1504     ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1505   if (!src)
1506     {
1507       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1508       return clib_error_return
1509         (0, "no matching interface address for destination %U (interface %U)",
1510          format_ip6_address, dst,
1511          format_vnet_sw_if_index_name, vnm, sw_if_index);
1512     }
1513
1514   h =
1515     vlib_packet_template_get_packet (vm,
1516                                      &im->discover_neighbor_packet_template,
1517                                      &bi);
1518   if (!h)
1519     return clib_error_return (0, "ICMP6 NS packet allocation failed");
1520
1521   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1522
1523   /* Destination address is a solicited node multicast address.  We need to fill in
1524      the low 24 bits with low 24 bits of target's address. */
1525   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1526   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1527   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1528
1529   h->ip.src_address = src[0];
1530   h->neighbor.target_address = dst[0];
1531
1532   if (PREDICT_FALSE (!hi->hw_address))
1533     {
1534       return clib_error_return (0, "%U: interface %U do not support ip probe",
1535                                 format_ip6_address, dst,
1536                                 format_vnet_sw_if_index_name, vnm,
1537                                 sw_if_index);
1538     }
1539
1540   clib_memcpy_fast (h->link_layer_option.ethernet_address, hi->hw_address,
1541                     vec_len (hi->hw_address));
1542
1543   h->neighbor.icmp.checksum =
1544     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1545   ASSERT (bogus_length == 0);
1546
1547   b = vlib_get_buffer (vm, bi);
1548   vnet_buffer (b)->sw_if_index[VLIB_RX] =
1549     vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1550
1551   /* Add encapsulation string for software interface (e.g. ethernet header). */
1552   ip46_address_t nh = {
1553     .ip6 = *dst,
1554   };
1555
1556   ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1557                             VNET_LINK_IP6, &nh, sw_if_index);
1558   adj = adj_get (ai);
1559
1560   /* Peer has been previously resolved, retrieve glean adj instead */
1561   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE && refresh == 0)
1562     {
1563       adj_unlock (ai);
1564       ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1565                                   VNET_LINK_IP6, sw_if_index, &nh);
1566       adj = adj_get (ai);
1567     }
1568
1569   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1570   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1571
1572   {
1573     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1574     u32 *to_next = vlib_frame_vector_args (f);
1575     to_next[0] = bi;
1576     f->n_vectors = 1;
1577     vlib_put_frame_to_node (vm, hi->output_node_index, f);
1578   }
1579
1580   adj_unlock (ai);
1581   return /* no error */ 0;
1582 }
1583 #endif
1584
1585 typedef enum
1586 {
1587   IP6_REWRITE_NEXT_DROP,
1588   IP6_REWRITE_NEXT_ICMP_ERROR,
1589   IP6_REWRITE_NEXT_FRAGMENT,
1590   IP6_REWRITE_N_NEXT            /* Last */
1591 } ip6_rewrite_next_t;
1592
1593 /**
1594  * This bits of an IPv6 address to mask to construct a multicast
1595  * MAC address
1596  */
1597 #define IP6_MCAST_ADDR_MASK 0xffffffff
1598
1599 always_inline void
1600 ip6_mtu_check (vlib_buffer_t * b, u16 packet_bytes,
1601                u16 adj_packet_bytes, bool is_locally_generated,
1602                u32 * next, u32 * error)
1603 {
1604   if (adj_packet_bytes >= 1280 && packet_bytes > adj_packet_bytes)
1605     {
1606       if (is_locally_generated)
1607         {
1608           /* IP fragmentation */
1609           ip_frag_set_vnet_buffer (b, adj_packet_bytes,
1610                                    IP6_FRAG_NEXT_IP6_REWRITE, 0);
1611           *next = IP6_REWRITE_NEXT_FRAGMENT;
1612           *error = IP6_ERROR_MTU_EXCEEDED;
1613         }
1614       else
1615         {
1616           *error = IP6_ERROR_MTU_EXCEEDED;
1617           icmp6_error_set_vnet_buffer (b, ICMP6_packet_too_big, 0,
1618                                        adj_packet_bytes);
1619           *next = IP6_REWRITE_NEXT_ICMP_ERROR;
1620         }
1621     }
1622 }
1623
1624 always_inline uword
1625 ip6_rewrite_inline_with_gso (vlib_main_t * vm,
1626                              vlib_node_runtime_t * node,
1627                              vlib_frame_t * frame,
1628                              int do_counters, int is_midchain, int is_mcast,
1629                              int do_gso)
1630 {
1631   ip_lookup_main_t *lm = &ip6_main.lookup_main;
1632   u32 *from = vlib_frame_vector_args (frame);
1633   u32 n_left_from, n_left_to_next, *to_next, next_index;
1634   vlib_node_runtime_t *error_node =
1635     vlib_node_get_runtime (vm, ip6_input_node.index);
1636
1637   n_left_from = frame->n_vectors;
1638   next_index = node->cached_next_index;
1639   u32 thread_index = vm->thread_index;
1640
1641   while (n_left_from > 0)
1642     {
1643       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1644
1645       while (n_left_from >= 4 && n_left_to_next >= 2)
1646         {
1647           ip_adjacency_t *adj0, *adj1;
1648           vlib_buffer_t *p0, *p1;
1649           ip6_header_t *ip0, *ip1;
1650           u32 pi0, rw_len0, next0, error0, adj_index0;
1651           u32 pi1, rw_len1, next1, error1, adj_index1;
1652           u32 tx_sw_if_index0, tx_sw_if_index1;
1653           bool is_locally_originated0, is_locally_originated1;
1654
1655           /* Prefetch next iteration. */
1656           {
1657             vlib_buffer_t *p2, *p3;
1658
1659             p2 = vlib_get_buffer (vm, from[2]);
1660             p3 = vlib_get_buffer (vm, from[3]);
1661
1662             vlib_prefetch_buffer_header (p2, LOAD);
1663             vlib_prefetch_buffer_header (p3, LOAD);
1664
1665             CLIB_PREFETCH (p2->pre_data, 32, STORE);
1666             CLIB_PREFETCH (p3->pre_data, 32, STORE);
1667
1668             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1669             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1670           }
1671
1672           pi0 = to_next[0] = from[0];
1673           pi1 = to_next[1] = from[1];
1674
1675           from += 2;
1676           n_left_from -= 2;
1677           to_next += 2;
1678           n_left_to_next -= 2;
1679
1680           p0 = vlib_get_buffer (vm, pi0);
1681           p1 = vlib_get_buffer (vm, pi1);
1682
1683           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1684           adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1685
1686           ip0 = vlib_buffer_get_current (p0);
1687           ip1 = vlib_buffer_get_current (p1);
1688
1689           error0 = error1 = IP6_ERROR_NONE;
1690           next0 = next1 = IP6_REWRITE_NEXT_DROP;
1691
1692           is_locally_originated0 =
1693             p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1694           if (PREDICT_TRUE (!is_locally_originated0))
1695             {
1696               i32 hop_limit0 = ip0->hop_limit;
1697
1698               /* Input node should have reject packets with hop limit 0. */
1699               ASSERT (ip0->hop_limit > 0);
1700
1701               hop_limit0 -= 1;
1702
1703               ip0->hop_limit = hop_limit0;
1704
1705               /*
1706                * If the hop count drops below 1 when forwarding, generate
1707                * an ICMP response.
1708                */
1709               if (PREDICT_FALSE (hop_limit0 <= 0))
1710                 {
1711                   error0 = IP6_ERROR_TIME_EXPIRED;
1712                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1713                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1714                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1715                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1716                                                0);
1717                 }
1718             }
1719           else
1720             {
1721               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1722             }
1723           is_locally_originated1 =
1724             p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1725           if (PREDICT_TRUE (!is_locally_originated1))
1726             {
1727               i32 hop_limit1 = ip1->hop_limit;
1728
1729               /* Input node should have reject packets with hop limit 0. */
1730               ASSERT (ip1->hop_limit > 0);
1731
1732               hop_limit1 -= 1;
1733
1734               ip1->hop_limit = hop_limit1;
1735
1736               /*
1737                * If the hop count drops below 1 when forwarding, generate
1738                * an ICMP response.
1739                */
1740               if (PREDICT_FALSE (hop_limit1 <= 0))
1741                 {
1742                   error1 = IP6_ERROR_TIME_EXPIRED;
1743                   next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1744                   vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1745                   icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1746                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1747                                                0);
1748                 }
1749             }
1750           else
1751             {
1752               p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1753             }
1754           adj0 = adj_get (adj_index0);
1755           adj1 = adj_get (adj_index1);
1756
1757           rw_len0 = adj0[0].rewrite_header.data_bytes;
1758           rw_len1 = adj1[0].rewrite_header.data_bytes;
1759           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1760           vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1761
1762           if (do_counters)
1763             {
1764               vlib_increment_combined_counter
1765                 (&adjacency_counters,
1766                  thread_index, adj_index0, 1,
1767                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1768               vlib_increment_combined_counter
1769                 (&adjacency_counters,
1770                  thread_index, adj_index1, 1,
1771                  vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1772             }
1773
1774           /* Check MTU of outgoing interface. */
1775           u16 ip0_len =
1776             clib_net_to_host_u16 (ip0->payload_length) +
1777             sizeof (ip6_header_t);
1778           u16 ip1_len =
1779             clib_net_to_host_u16 (ip1->payload_length) +
1780             sizeof (ip6_header_t);
1781           if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1782             ip0_len = gso_mtu_sz (p0);
1783           if (do_gso && (p1->flags & VNET_BUFFER_F_GSO))
1784             ip1_len = gso_mtu_sz (p1);
1785
1786
1787
1788           ip6_mtu_check (p0, ip0_len,
1789                          adj0[0].rewrite_header.max_l3_packet_bytes,
1790                          is_locally_originated0, &next0, &error0);
1791           ip6_mtu_check (p1, ip1_len,
1792                          adj1[0].rewrite_header.max_l3_packet_bytes,
1793                          is_locally_originated1, &next1, &error1);
1794
1795           /* Don't adjust the buffer for hop count issue; icmp-error node
1796            * wants to see the IP headerr */
1797           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1798             {
1799               p0->current_data -= rw_len0;
1800               p0->current_length += rw_len0;
1801
1802               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1803               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1804               next0 = adj0[0].rewrite_header.next_index;
1805
1806               if (PREDICT_FALSE
1807                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1808                 vnet_feature_arc_start (lm->output_feature_arc_index,
1809                                         tx_sw_if_index0, &next0, p0);
1810             }
1811           if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1812             {
1813               p1->current_data -= rw_len1;
1814               p1->current_length += rw_len1;
1815
1816               tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1817               vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1818               next1 = adj1[0].rewrite_header.next_index;
1819
1820               if (PREDICT_FALSE
1821                   (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1822                 vnet_feature_arc_start (lm->output_feature_arc_index,
1823                                         tx_sw_if_index1, &next1, p1);
1824             }
1825
1826           /* Guess we are only writing on simple Ethernet header. */
1827           vnet_rewrite_two_headers (adj0[0], adj1[0],
1828                                     ip0, ip1, sizeof (ethernet_header_t));
1829
1830           if (is_midchain)
1831             {
1832               adj0->sub_type.midchain.fixup_func
1833                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1834               adj1->sub_type.midchain.fixup_func
1835                 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1836             }
1837           if (is_mcast)
1838             {
1839               /*
1840                * copy bytes from the IP address into the MAC rewrite
1841                */
1842               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1843                                           adj0->
1844                                           rewrite_header.dst_mcast_offset,
1845                                           &ip0->dst_address.as_u32[3],
1846                                           (u8 *) ip0);
1847               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1848                                           adj1->
1849                                           rewrite_header.dst_mcast_offset,
1850                                           &ip1->dst_address.as_u32[3],
1851                                           (u8 *) ip1);
1852             }
1853
1854           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1855                                            to_next, n_left_to_next,
1856                                            pi0, pi1, next0, next1);
1857         }
1858
1859       while (n_left_from > 0 && n_left_to_next > 0)
1860         {
1861           ip_adjacency_t *adj0;
1862           vlib_buffer_t *p0;
1863           ip6_header_t *ip0;
1864           u32 pi0, rw_len0;
1865           u32 adj_index0, next0, error0;
1866           u32 tx_sw_if_index0;
1867           bool is_locally_originated0;
1868
1869           pi0 = to_next[0] = from[0];
1870
1871           p0 = vlib_get_buffer (vm, pi0);
1872
1873           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1874
1875           adj0 = adj_get (adj_index0);
1876
1877           ip0 = vlib_buffer_get_current (p0);
1878
1879           error0 = IP6_ERROR_NONE;
1880           next0 = IP6_REWRITE_NEXT_DROP;
1881
1882           /* Check hop limit */
1883           is_locally_originated0 =
1884             p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED;
1885           if (PREDICT_TRUE (!is_locally_originated0))
1886             {
1887               i32 hop_limit0 = ip0->hop_limit;
1888
1889               ASSERT (ip0->hop_limit > 0);
1890
1891               hop_limit0 -= 1;
1892
1893               ip0->hop_limit = hop_limit0;
1894
1895               if (PREDICT_FALSE (hop_limit0 <= 0))
1896                 {
1897                   /*
1898                    * If the hop count drops below 1 when forwarding, generate
1899                    * an ICMP response.
1900                    */
1901                   error0 = IP6_ERROR_TIME_EXPIRED;
1902                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1903                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1904                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1905                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1906                                                0);
1907                 }
1908             }
1909           else
1910             {
1911               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1912             }
1913
1914           /* Guess we are only writing on simple Ethernet header. */
1915           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
1916
1917           /* Update packet buffer attributes/set output interface. */
1918           rw_len0 = adj0[0].rewrite_header.data_bytes;
1919           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1920
1921           if (do_counters)
1922             {
1923               vlib_increment_combined_counter
1924                 (&adjacency_counters,
1925                  thread_index, adj_index0, 1,
1926                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1927             }
1928
1929           /* Check MTU of outgoing interface. */
1930           u16 ip0_len =
1931             clib_net_to_host_u16 (ip0->payload_length) +
1932             sizeof (ip6_header_t);
1933           if (do_gso && (p0->flags & VNET_BUFFER_F_GSO))
1934             ip0_len = gso_mtu_sz (p0);
1935
1936           ip6_mtu_check (p0, ip0_len,
1937                          adj0[0].rewrite_header.max_l3_packet_bytes,
1938                          is_locally_originated0, &next0, &error0);
1939
1940           /* Don't adjust the buffer for hop count issue; icmp-error node
1941            * wants to see the IP header */
1942           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1943             {
1944               p0->current_data -= rw_len0;
1945               p0->current_length += rw_len0;
1946
1947               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1948
1949               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1950               next0 = adj0[0].rewrite_header.next_index;
1951
1952               if (PREDICT_FALSE
1953                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1954                 vnet_feature_arc_start (lm->output_feature_arc_index,
1955                                         tx_sw_if_index0, &next0, p0);
1956             }
1957
1958           if (is_midchain)
1959             {
1960               adj0->sub_type.midchain.fixup_func
1961                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1962             }
1963           if (is_mcast)
1964             {
1965               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1966                                           adj0->
1967                                           rewrite_header.dst_mcast_offset,
1968                                           &ip0->dst_address.as_u32[3],
1969                                           (u8 *) ip0);
1970             }
1971
1972           p0->error = error_node->errors[error0];
1973
1974           from += 1;
1975           n_left_from -= 1;
1976           to_next += 1;
1977           n_left_to_next -= 1;
1978
1979           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1980                                            to_next, n_left_to_next,
1981                                            pi0, next0);
1982         }
1983
1984       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1985     }
1986
1987   /* Need to do trace after rewrites to pick up new packet data. */
1988   if (node->flags & VLIB_NODE_FLAG_TRACE)
1989     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1990
1991   return frame->n_vectors;
1992 }
1993
1994 always_inline uword
1995 ip6_rewrite_inline (vlib_main_t * vm,
1996                     vlib_node_runtime_t * node,
1997                     vlib_frame_t * frame,
1998                     int do_counters, int is_midchain, int is_mcast)
1999 {
2000   vnet_main_t *vnm = vnet_get_main ();
2001   if (PREDICT_FALSE (vnm->interface_main.gso_interface_count > 0))
2002     return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2003                                         is_midchain, is_mcast,
2004                                         1 /* do_gso */ );
2005   else
2006     return ip6_rewrite_inline_with_gso (vm, node, frame, do_counters,
2007                                         is_midchain, is_mcast,
2008                                         0 /* no do_gso */ );
2009 }
2010
2011 VLIB_NODE_FN (ip6_rewrite_node) (vlib_main_t * vm,
2012                                  vlib_node_runtime_t * node,
2013                                  vlib_frame_t * frame)
2014 {
2015   if (adj_are_counters_enabled ())
2016     return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2017   else
2018     return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2019 }
2020
2021 VLIB_NODE_FN (ip6_rewrite_bcast_node) (vlib_main_t * vm,
2022                                        vlib_node_runtime_t * node,
2023                                        vlib_frame_t * frame)
2024 {
2025   if (adj_are_counters_enabled ())
2026     return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2027   else
2028     return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2029 }
2030
2031 VLIB_NODE_FN (ip6_rewrite_mcast_node) (vlib_main_t * vm,
2032                                        vlib_node_runtime_t * node,
2033                                        vlib_frame_t * frame)
2034 {
2035   if (adj_are_counters_enabled ())
2036     return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2037   else
2038     return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2039 }
2040
2041 VLIB_NODE_FN (ip6_midchain_node) (vlib_main_t * vm,
2042                                   vlib_node_runtime_t * node,
2043                                   vlib_frame_t * frame)
2044 {
2045   if (adj_are_counters_enabled ())
2046     return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2047   else
2048     return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2049 }
2050
2051 VLIB_NODE_FN (ip6_mcast_midchain_node) (vlib_main_t * vm,
2052                                         vlib_node_runtime_t * node,
2053                                         vlib_frame_t * frame)
2054 {
2055   if (adj_are_counters_enabled ())
2056     return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2057   else
2058     return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2059 }
2060
2061 /* *INDENT-OFF* */
2062 VLIB_REGISTER_NODE (ip6_midchain_node) =
2063 {
2064   .name = "ip6-midchain",
2065   .vector_size = sizeof (u32),
2066   .format_trace = format_ip6_forward_next_trace,
2067   .sibling_of = "ip6-rewrite",
2068   };
2069
2070 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2071 {
2072   .name = "ip6-rewrite",
2073   .vector_size = sizeof (u32),
2074   .format_trace = format_ip6_rewrite_trace,
2075   .n_next_nodes = IP6_REWRITE_N_NEXT,
2076   .next_nodes =
2077   {
2078     [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2079     [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2080     [IP6_REWRITE_NEXT_FRAGMENT] = "ip6-frag",
2081   },
2082 };
2083
2084 VLIB_REGISTER_NODE (ip6_rewrite_bcast_node) = {
2085   .name = "ip6-rewrite-bcast",
2086   .vector_size = sizeof (u32),
2087
2088   .format_trace = format_ip6_rewrite_trace,
2089   .sibling_of = "ip6-rewrite",
2090 };
2091
2092 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2093 {
2094   .name = "ip6-rewrite-mcast",
2095   .vector_size = sizeof (u32),
2096   .format_trace = format_ip6_rewrite_trace,
2097   .sibling_of = "ip6-rewrite",
2098 };
2099
2100
2101 VLIB_REGISTER_NODE (ip6_mcast_midchain_node) =
2102 {
2103   .name = "ip6-mcast-midchain",
2104   .vector_size = sizeof (u32),
2105   .format_trace = format_ip6_rewrite_trace,
2106   .sibling_of = "ip6-rewrite",
2107 };
2108
2109 /* *INDENT-ON* */
2110
2111 /*
2112  * Hop-by-Hop handling
2113  */
2114 #ifndef CLIB_MARCH_VARIANT
2115 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2116 #endif /* CLIB_MARCH_VARIANT */
2117
2118 #define foreach_ip6_hop_by_hop_error \
2119 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2120 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2121 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2122
2123 /* *INDENT-OFF* */
2124 typedef enum
2125 {
2126 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2127   foreach_ip6_hop_by_hop_error
2128 #undef _
2129   IP6_HOP_BY_HOP_N_ERROR,
2130 } ip6_hop_by_hop_error_t;
2131 /* *INDENT-ON* */
2132
2133 /*
2134  * Primary h-b-h handler trace support
2135  * We work pretty hard on the problem for obvious reasons
2136  */
2137 typedef struct
2138 {
2139   u32 next_index;
2140   u32 trace_len;
2141   u8 option_data[256];
2142 } ip6_hop_by_hop_trace_t;
2143
2144 extern vlib_node_registration_t ip6_hop_by_hop_node;
2145
2146 static char *ip6_hop_by_hop_error_strings[] = {
2147 #define _(sym,string) string,
2148   foreach_ip6_hop_by_hop_error
2149 #undef _
2150 };
2151
2152 #ifndef CLIB_MARCH_VARIANT
2153 u8 *
2154 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2155 {
2156   ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2157   int total_len = va_arg (*args, int);
2158   ip6_hop_by_hop_option_t *opt0, *limit0;
2159   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2160   u8 type0;
2161
2162   s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2163               hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2164
2165   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2166   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2167
2168   while (opt0 < limit0)
2169     {
2170       type0 = opt0->type;
2171       switch (type0)
2172         {
2173         case 0:         /* Pad, just stop */
2174           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2175           break;
2176
2177         default:
2178           if (hm->trace[type0])
2179             {
2180               s = (*hm->trace[type0]) (s, opt0);
2181             }
2182           else
2183             {
2184               s =
2185                 format (s, "\n    unrecognized option %d length %d", type0,
2186                         opt0->length);
2187             }
2188           opt0 =
2189             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2190                                          sizeof (ip6_hop_by_hop_option_t));
2191           break;
2192         }
2193     }
2194   return s;
2195 }
2196 #endif
2197
2198 static u8 *
2199 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2200 {
2201   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2202   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2203   ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2204   ip6_hop_by_hop_header_t *hbh0;
2205   ip6_hop_by_hop_option_t *opt0, *limit0;
2206   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2207
2208   u8 type0;
2209
2210   hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2211
2212   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2213               t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2214
2215   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2216   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2217
2218   while (opt0 < limit0)
2219     {
2220       type0 = opt0->type;
2221       switch (type0)
2222         {
2223         case 0:         /* Pad, just stop */
2224           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2225           break;
2226
2227         default:
2228           if (hm->trace[type0])
2229             {
2230               s = (*hm->trace[type0]) (s, opt0);
2231             }
2232           else
2233             {
2234               s =
2235                 format (s, "\n    unrecognized option %d length %d", type0,
2236                         opt0->length);
2237             }
2238           opt0 =
2239             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2240                                          sizeof (ip6_hop_by_hop_option_t));
2241           break;
2242         }
2243     }
2244   return s;
2245 }
2246
2247 always_inline u8
2248 ip6_scan_hbh_options (vlib_buffer_t * b0,
2249                       ip6_header_t * ip0,
2250                       ip6_hop_by_hop_header_t * hbh0,
2251                       ip6_hop_by_hop_option_t * opt0,
2252                       ip6_hop_by_hop_option_t * limit0, u32 * next0)
2253 {
2254   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2255   u8 type0;
2256   u8 error0 = 0;
2257
2258   while (opt0 < limit0)
2259     {
2260       type0 = opt0->type;
2261       switch (type0)
2262         {
2263         case 0:         /* Pad1 */
2264           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2265           continue;
2266         case 1:         /* PadN */
2267           break;
2268         default:
2269           if (hm->options[type0])
2270             {
2271               if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2272                 {
2273                   error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2274                   return (error0);
2275                 }
2276             }
2277           else
2278             {
2279               /* Unrecognized mandatory option, check the two high order bits */
2280               switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2281                 {
2282                 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2283                   break;
2284                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2285                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2286                   *next0 = IP_LOOKUP_NEXT_DROP;
2287                   break;
2288                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2289                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2290                   *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2291                   icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2292                                                ICMP6_parameter_problem_unrecognized_option,
2293                                                (u8 *) opt0 - (u8 *) ip0);
2294                   break;
2295                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2296                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2297                   if (!ip6_address_is_multicast (&ip0->dst_address))
2298                     {
2299                       *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2300                       icmp6_error_set_vnet_buffer (b0,
2301                                                    ICMP6_parameter_problem,
2302                                                    ICMP6_parameter_problem_unrecognized_option,
2303                                                    (u8 *) opt0 - (u8 *) ip0);
2304                     }
2305                   else
2306                     {
2307                       *next0 = IP_LOOKUP_NEXT_DROP;
2308                     }
2309                   break;
2310                 }
2311               return (error0);
2312             }
2313         }
2314       opt0 =
2315         (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2316                                      sizeof (ip6_hop_by_hop_option_t));
2317     }
2318   return (error0);
2319 }
2320
2321 /*
2322  * Process the Hop-by-Hop Options header
2323  */
2324 VLIB_NODE_FN (ip6_hop_by_hop_node) (vlib_main_t * vm,
2325                                     vlib_node_runtime_t * node,
2326                                     vlib_frame_t * frame)
2327 {
2328   vlib_node_runtime_t *error_node =
2329     vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2330   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2331   u32 n_left_from, *from, *to_next;
2332   ip_lookup_next_t next_index;
2333
2334   from = vlib_frame_vector_args (frame);
2335   n_left_from = frame->n_vectors;
2336   next_index = node->cached_next_index;
2337
2338   while (n_left_from > 0)
2339     {
2340       u32 n_left_to_next;
2341
2342       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2343
2344       while (n_left_from >= 4 && n_left_to_next >= 2)
2345         {
2346           u32 bi0, bi1;
2347           vlib_buffer_t *b0, *b1;
2348           u32 next0, next1;
2349           ip6_header_t *ip0, *ip1;
2350           ip6_hop_by_hop_header_t *hbh0, *hbh1;
2351           ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2352           u8 error0 = 0, error1 = 0;
2353
2354           /* Prefetch next iteration. */
2355           {
2356             vlib_buffer_t *p2, *p3;
2357
2358             p2 = vlib_get_buffer (vm, from[2]);
2359             p3 = vlib_get_buffer (vm, from[3]);
2360
2361             vlib_prefetch_buffer_header (p2, LOAD);
2362             vlib_prefetch_buffer_header (p3, LOAD);
2363
2364             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2365             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2366           }
2367
2368           /* Speculatively enqueue b0, b1 to the current next frame */
2369           to_next[0] = bi0 = from[0];
2370           to_next[1] = bi1 = from[1];
2371           from += 2;
2372           to_next += 2;
2373           n_left_from -= 2;
2374           n_left_to_next -= 2;
2375
2376           b0 = vlib_get_buffer (vm, bi0);
2377           b1 = vlib_get_buffer (vm, bi1);
2378
2379           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2380           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2381           ip_adjacency_t *adj0 = adj_get (adj_index0);
2382           u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2383           ip_adjacency_t *adj1 = adj_get (adj_index1);
2384
2385           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2386           next0 = adj0->lookup_next_index;
2387           next1 = adj1->lookup_next_index;
2388
2389           ip0 = vlib_buffer_get_current (b0);
2390           ip1 = vlib_buffer_get_current (b1);
2391           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2392           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2393           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2394           opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2395           limit0 =
2396             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2397                                          ((hbh0->length + 1) << 3));
2398           limit1 =
2399             (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2400                                          ((hbh1->length + 1) << 3));
2401
2402           /*
2403            * Basic validity checks
2404            */
2405           if ((hbh0->length + 1) << 3 >
2406               clib_net_to_host_u16 (ip0->payload_length))
2407             {
2408               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2409               next0 = IP_LOOKUP_NEXT_DROP;
2410               goto outdual;
2411             }
2412           /* Scan the set of h-b-h options, process ones that we understand */
2413           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2414
2415           if ((hbh1->length + 1) << 3 >
2416               clib_net_to_host_u16 (ip1->payload_length))
2417             {
2418               error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2419               next1 = IP_LOOKUP_NEXT_DROP;
2420               goto outdual;
2421             }
2422           /* Scan the set of h-b-h options, process ones that we understand */
2423           error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2424
2425         outdual:
2426           /* Has the classifier flagged this buffer for special treatment? */
2427           if (PREDICT_FALSE
2428               ((error0 == 0)
2429                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2430             next0 = hm->next_override;
2431
2432           /* Has the classifier flagged this buffer for special treatment? */
2433           if (PREDICT_FALSE
2434               ((error1 == 0)
2435                && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2436             next1 = hm->next_override;
2437
2438           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2439             {
2440               if (b0->flags & VLIB_BUFFER_IS_TRACED)
2441                 {
2442                   ip6_hop_by_hop_trace_t *t =
2443                     vlib_add_trace (vm, node, b0, sizeof (*t));
2444                   u32 trace_len = (hbh0->length + 1) << 3;
2445                   t->next_index = next0;
2446                   /* Capture the h-b-h option verbatim */
2447                   trace_len =
2448                     trace_len <
2449                     ARRAY_LEN (t->option_data) ? trace_len :
2450                     ARRAY_LEN (t->option_data);
2451                   t->trace_len = trace_len;
2452                   clib_memcpy_fast (t->option_data, hbh0, trace_len);
2453                 }
2454               if (b1->flags & VLIB_BUFFER_IS_TRACED)
2455                 {
2456                   ip6_hop_by_hop_trace_t *t =
2457                     vlib_add_trace (vm, node, b1, sizeof (*t));
2458                   u32 trace_len = (hbh1->length + 1) << 3;
2459                   t->next_index = next1;
2460                   /* Capture the h-b-h option verbatim */
2461                   trace_len =
2462                     trace_len <
2463                     ARRAY_LEN (t->option_data) ? trace_len :
2464                     ARRAY_LEN (t->option_data);
2465                   t->trace_len = trace_len;
2466                   clib_memcpy_fast (t->option_data, hbh1, trace_len);
2467                 }
2468
2469             }
2470
2471           b0->error = error_node->errors[error0];
2472           b1->error = error_node->errors[error1];
2473
2474           /* verify speculative enqueue, maybe switch current next frame */
2475           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2476                                            n_left_to_next, bi0, bi1, next0,
2477                                            next1);
2478         }
2479
2480       while (n_left_from > 0 && n_left_to_next > 0)
2481         {
2482           u32 bi0;
2483           vlib_buffer_t *b0;
2484           u32 next0;
2485           ip6_header_t *ip0;
2486           ip6_hop_by_hop_header_t *hbh0;
2487           ip6_hop_by_hop_option_t *opt0, *limit0;
2488           u8 error0 = 0;
2489
2490           /* Speculatively enqueue b0 to the current next frame */
2491           bi0 = from[0];
2492           to_next[0] = bi0;
2493           from += 1;
2494           to_next += 1;
2495           n_left_from -= 1;
2496           n_left_to_next -= 1;
2497
2498           b0 = vlib_get_buffer (vm, bi0);
2499           /*
2500            * Default use the next_index from the adjacency.
2501            * A HBH option rarely redirects to a different node
2502            */
2503           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2504           ip_adjacency_t *adj0 = adj_get (adj_index0);
2505           next0 = adj0->lookup_next_index;
2506
2507           ip0 = vlib_buffer_get_current (b0);
2508           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2509           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2510           limit0 =
2511             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2512                                          ((hbh0->length + 1) << 3));
2513
2514           /*
2515            * Basic validity checks
2516            */
2517           if ((hbh0->length + 1) << 3 >
2518               clib_net_to_host_u16 (ip0->payload_length))
2519             {
2520               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2521               next0 = IP_LOOKUP_NEXT_DROP;
2522               goto out0;
2523             }
2524
2525           /* Scan the set of h-b-h options, process ones that we understand */
2526           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2527
2528         out0:
2529           /* Has the classifier flagged this buffer for special treatment? */
2530           if (PREDICT_FALSE
2531               ((error0 == 0)
2532                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2533             next0 = hm->next_override;
2534
2535           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2536             {
2537               ip6_hop_by_hop_trace_t *t =
2538                 vlib_add_trace (vm, node, b0, sizeof (*t));
2539               u32 trace_len = (hbh0->length + 1) << 3;
2540               t->next_index = next0;
2541               /* Capture the h-b-h option verbatim */
2542               trace_len =
2543                 trace_len <
2544                 ARRAY_LEN (t->option_data) ? trace_len :
2545                 ARRAY_LEN (t->option_data);
2546               t->trace_len = trace_len;
2547               clib_memcpy_fast (t->option_data, hbh0, trace_len);
2548             }
2549
2550           b0->error = error_node->errors[error0];
2551
2552           /* verify speculative enqueue, maybe switch current next frame */
2553           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2554                                            n_left_to_next, bi0, next0);
2555         }
2556       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2557     }
2558   return frame->n_vectors;
2559 }
2560
2561 /* *INDENT-OFF* */
2562 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2563 {
2564   .name = "ip6-hop-by-hop",
2565   .sibling_of = "ip6-lookup",
2566   .vector_size = sizeof (u32),
2567   .format_trace = format_ip6_hop_by_hop_trace,
2568   .type = VLIB_NODE_TYPE_INTERNAL,
2569   .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2570   .error_strings = ip6_hop_by_hop_error_strings,
2571   .n_next_nodes = 0,
2572 };
2573 /* *INDENT-ON* */
2574
2575 static clib_error_t *
2576 ip6_hop_by_hop_init (vlib_main_t * vm)
2577 {
2578   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2579   clib_memset (hm->options, 0, sizeof (hm->options));
2580   clib_memset (hm->trace, 0, sizeof (hm->trace));
2581   hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2582   return (0);
2583 }
2584
2585 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2586
2587 #ifndef CLIB_MARCH_VARIANT
2588 void
2589 ip6_hbh_set_next_override (uword next)
2590 {
2591   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2592
2593   hm->next_override = next;
2594 }
2595
2596 int
2597 ip6_hbh_register_option (u8 option,
2598                          int options (vlib_buffer_t * b, ip6_header_t * ip,
2599                                       ip6_hop_by_hop_option_t * opt),
2600                          u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2601 {
2602   ip6_main_t *im = &ip6_main;
2603   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2604
2605   ASSERT ((u32) option < ARRAY_LEN (hm->options));
2606
2607   /* Already registered */
2608   if (hm->options[option])
2609     return (-1);
2610
2611   hm->options[option] = options;
2612   hm->trace[option] = trace;
2613
2614   /* Set global variable */
2615   im->hbh_enabled = 1;
2616
2617   return (0);
2618 }
2619
2620 int
2621 ip6_hbh_unregister_option (u8 option)
2622 {
2623   ip6_main_t *im = &ip6_main;
2624   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2625
2626   ASSERT ((u32) option < ARRAY_LEN (hm->options));
2627
2628   /* Not registered */
2629   if (!hm->options[option])
2630     return (-1);
2631
2632   hm->options[option] = NULL;
2633   hm->trace[option] = NULL;
2634
2635   /* Disable global knob if this was the last option configured */
2636   int i;
2637   bool found = false;
2638   for (i = 0; i < 256; i++)
2639     {
2640       if (hm->options[option])
2641         {
2642           found = true;
2643           break;
2644         }
2645     }
2646   if (!found)
2647     im->hbh_enabled = 0;
2648
2649   return (0);
2650 }
2651
2652 /* Global IP6 main. */
2653 ip6_main_t ip6_main;
2654 #endif
2655
2656 static clib_error_t *
2657 ip6_lookup_init (vlib_main_t * vm)
2658 {
2659   ip6_main_t *im = &ip6_main;
2660   clib_error_t *error;
2661   uword i;
2662
2663   if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2664     return error;
2665
2666   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2667     {
2668       u32 j, i0, i1;
2669
2670       i0 = i / 32;
2671       i1 = i % 32;
2672
2673       for (j = 0; j < i0; j++)
2674         im->fib_masks[i].as_u32[j] = ~0;
2675
2676       if (i1)
2677         im->fib_masks[i].as_u32[i0] =
2678           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2679     }
2680
2681   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2682
2683   if (im->lookup_table_nbuckets == 0)
2684     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2685
2686   im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2687
2688   if (im->lookup_table_size == 0)
2689     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2690
2691   clib_bihash_init_24_8 (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2692                          "ip6 FIB fwding table",
2693                          im->lookup_table_nbuckets, im->lookup_table_size);
2694   clib_bihash_init_24_8 (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2695                          "ip6 FIB non-fwding table",
2696                          im->lookup_table_nbuckets, im->lookup_table_size);
2697   clib_bihash_init_40_8 (&im->ip6_mtable.ip6_mhash,
2698                          "ip6 mFIB table",
2699                          im->lookup_table_nbuckets, im->lookup_table_size);
2700
2701   /* Create FIB with index 0 and table id of 0. */
2702   fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2703                                      FIB_SOURCE_DEFAULT_ROUTE);
2704   mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2705                                       MFIB_SOURCE_DEFAULT_ROUTE);
2706
2707   {
2708     pg_node_t *pn;
2709     pn = pg_get_node (ip6_lookup_node.index);
2710     pn->unformat_edit = unformat_pg_ip6_header;
2711   }
2712
2713   /* Unless explicitly configured, don't process HBH options */
2714   im->hbh_enabled = 0;
2715
2716   {
2717     icmp6_neighbor_solicitation_header_t p;
2718
2719     clib_memset (&p, 0, sizeof (p));
2720
2721     p.ip.ip_version_traffic_class_and_flow_label =
2722       clib_host_to_net_u32 (0x6 << 28);
2723     p.ip.payload_length =
2724       clib_host_to_net_u16 (sizeof (p) -
2725                             STRUCT_OFFSET_OF
2726                             (icmp6_neighbor_solicitation_header_t, neighbor));
2727     p.ip.protocol = IP_PROTOCOL_ICMP6;
2728     p.ip.hop_limit = 255;
2729     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2730
2731     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2732
2733     p.link_layer_option.header.type =
2734       ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2735     p.link_layer_option.header.n_data_u64s =
2736       sizeof (p.link_layer_option) / sizeof (u64);
2737
2738     vlib_packet_template_init (vm,
2739                                &im->discover_neighbor_packet_template,
2740                                &p, sizeof (p),
2741                                /* alloc chunk size */ 8,
2742                                "ip6 neighbor discovery");
2743   }
2744
2745   return error;
2746 }
2747
2748 VLIB_INIT_FUNCTION (ip6_lookup_init);
2749
2750 static clib_error_t *
2751 test_ip6_link_command_fn (vlib_main_t * vm,
2752                           unformat_input_t * input, vlib_cli_command_t * cmd)
2753 {
2754   u8 mac[6];
2755   ip6_address_t _a, *a = &_a;
2756
2757   if (unformat (input, "%U", unformat_ethernet_address, mac))
2758     {
2759       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2760       vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2761       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2762       vlib_cli_output (vm, "Original MAC address: %U",
2763                        format_ethernet_address, mac);
2764     }
2765
2766   return 0;
2767 }
2768
2769 /*?
2770  * This command converts the given MAC Address into an IPv6 link-local
2771  * address.
2772  *
2773  * @cliexpar
2774  * Example of how to create an IPv6 link-local address:
2775  * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2776  * Link local address: fe80::14d9:e0ff:fe91:7986
2777  * Original MAC address: 16:d9:e0:91:79:86
2778  * @cliexend
2779 ?*/
2780 /* *INDENT-OFF* */
2781 VLIB_CLI_COMMAND (test_link_command, static) =
2782 {
2783   .path = "test ip6 link",
2784   .function = test_ip6_link_command_fn,
2785   .short_help = "test ip6 link <mac-address>",
2786 };
2787 /* *INDENT-ON* */
2788
2789 #ifndef CLIB_MARCH_VARIANT
2790 int
2791 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2792 {
2793   u32 fib_index;
2794
2795   fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2796
2797   if (~0 == fib_index)
2798     return VNET_API_ERROR_NO_SUCH_FIB;
2799
2800   fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2801                                   flow_hash_config);
2802
2803   return 0;
2804 }
2805 #endif
2806
2807 static clib_error_t *
2808 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2809                               unformat_input_t * input,
2810                               vlib_cli_command_t * cmd)
2811 {
2812   int matched = 0;
2813   u32 table_id = 0;
2814   u32 flow_hash_config = 0;
2815   int rv;
2816
2817   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2818     {
2819       if (unformat (input, "table %d", &table_id))
2820         matched = 1;
2821 #define _(a,v) \
2822     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2823       foreach_flow_hash_bit
2824 #undef _
2825         else
2826         break;
2827     }
2828
2829   if (matched == 0)
2830     return clib_error_return (0, "unknown input `%U'",
2831                               format_unformat_error, input);
2832
2833   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2834   switch (rv)
2835     {
2836     case 0:
2837       break;
2838
2839     case -1:
2840       return clib_error_return (0, "no such FIB table %d", table_id);
2841
2842     default:
2843       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2844       break;
2845     }
2846
2847   return 0;
2848 }
2849
2850 /*?
2851  * Configure the set of IPv6 fields used by the flow hash.
2852  *
2853  * @cliexpar
2854  * @parblock
2855  * Example of how to set the flow hash on a given table:
2856  * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2857  *
2858  * Example of display the configured flow hash:
2859  * @cliexstart{show ip6 fib}
2860  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2861  * @::/0
2862  *   unicast-ip6-chain
2863  *   [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2864  *     [0] [@0]: dpo-drop ip6
2865  * fe80::/10
2866  *   unicast-ip6-chain
2867  *   [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2868  *     [0] [@2]: dpo-receive
2869  * ff02::1/128
2870  *   unicast-ip6-chain
2871  *   [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2872  *     [0] [@2]: dpo-receive
2873  * ff02::2/128
2874  *   unicast-ip6-chain
2875  *   [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2876  *     [0] [@2]: dpo-receive
2877  * ff02::16/128
2878  *   unicast-ip6-chain
2879  *   [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2880  *     [0] [@2]: dpo-receive
2881  * ff02::1:ff00:0/104
2882  *   unicast-ip6-chain
2883  *   [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2884  *     [0] [@2]: dpo-receive
2885  * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2886  * @::/0
2887  *   unicast-ip6-chain
2888  *   [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
2889  *     [0] [@0]: dpo-drop ip6
2890  * @::a:1:1:0:4/126
2891  *   unicast-ip6-chain
2892  *   [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
2893  *     [0] [@4]: ipv6-glean: af_packet0
2894  * @::a:1:1:0:7/128
2895  *   unicast-ip6-chain
2896  *   [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
2897  *     [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
2898  * fe80::/10
2899  *   unicast-ip6-chain
2900  *   [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
2901  *     [0] [@2]: dpo-receive
2902  * fe80::fe:3eff:fe3e:9222/128
2903  *   unicast-ip6-chain
2904  *   [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
2905  *     [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
2906  * ff02::1/128
2907  *   unicast-ip6-chain
2908  *   [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
2909  *     [0] [@2]: dpo-receive
2910  * ff02::2/128
2911  *   unicast-ip6-chain
2912  *   [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
2913  *     [0] [@2]: dpo-receive
2914  * ff02::16/128
2915  *   unicast-ip6-chain
2916  *   [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
2917  *     [0] [@2]: dpo-receive
2918  * ff02::1:ff00:0/104
2919  *   unicast-ip6-chain
2920  *   [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
2921  *     [0] [@2]: dpo-receive
2922  * @cliexend
2923  * @endparblock
2924 ?*/
2925 /* *INDENT-OFF* */
2926 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
2927 {
2928   .path = "set ip6 flow-hash",
2929   .short_help =
2930   "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
2931   .function = set_ip6_flow_hash_command_fn,
2932 };
2933 /* *INDENT-ON* */
2934
2935 static clib_error_t *
2936 show_ip6_local_command_fn (vlib_main_t * vm,
2937                            unformat_input_t * input, vlib_cli_command_t * cmd)
2938 {
2939   ip6_main_t *im = &ip6_main;
2940   ip_lookup_main_t *lm = &im->lookup_main;
2941   int i;
2942
2943   vlib_cli_output (vm, "Protocols handled by ip6_local");
2944   for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
2945     {
2946       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2947         {
2948
2949           u32 node_index = vlib_get_node (vm,
2950                                           ip6_local_node.index)->
2951             next_nodes[lm->local_next_by_ip_protocol[i]];
2952           vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
2953                            node_index);
2954         }
2955     }
2956   return 0;
2957 }
2958
2959
2960
2961 /*?
2962  * Display the set of protocols handled by the local IPv6 stack.
2963  *
2964  * @cliexpar
2965  * Example of how to display local protocol table:
2966  * @cliexstart{show ip6 local}
2967  * Protocols handled by ip6_local
2968  * 17
2969  * 43
2970  * 58
2971  * 115
2972  * @cliexend
2973 ?*/
2974 /* *INDENT-OFF* */
2975 VLIB_CLI_COMMAND (show_ip6_local, static) =
2976 {
2977   .path = "show ip6 local",
2978   .function = show_ip6_local_command_fn,
2979   .short_help = "show ip6 local",
2980 };
2981 /* *INDENT-ON* */
2982
2983 #ifndef CLIB_MARCH_VARIANT
2984 int
2985 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2986                              u32 table_index)
2987 {
2988   vnet_main_t *vnm = vnet_get_main ();
2989   vnet_interface_main_t *im = &vnm->interface_main;
2990   ip6_main_t *ipm = &ip6_main;
2991   ip_lookup_main_t *lm = &ipm->lookup_main;
2992   vnet_classify_main_t *cm = &vnet_classify_main;
2993   ip6_address_t *if_addr;
2994
2995   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2996     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2997
2998   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2999     return VNET_API_ERROR_NO_SUCH_ENTRY;
3000
3001   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3002   lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3003
3004   if_addr = ip6_interface_first_address (ipm, sw_if_index);
3005
3006   if (NULL != if_addr)
3007     {
3008       fib_prefix_t pfx = {
3009         .fp_len = 128,
3010         .fp_proto = FIB_PROTOCOL_IP6,
3011         .fp_addr.ip6 = *if_addr,
3012       };
3013       u32 fib_index;
3014
3015       fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3016                                                        sw_if_index);
3017
3018
3019       if (table_index != (u32) ~ 0)
3020         {
3021           dpo_id_t dpo = DPO_INVALID;
3022
3023           dpo_set (&dpo,
3024                    DPO_CLASSIFY,
3025                    DPO_PROTO_IP6,
3026                    classify_dpo_create (DPO_PROTO_IP6, table_index));
3027
3028           fib_table_entry_special_dpo_add (fib_index,
3029                                            &pfx,
3030                                            FIB_SOURCE_CLASSIFY,
3031                                            FIB_ENTRY_FLAG_NONE, &dpo);
3032           dpo_reset (&dpo);
3033         }
3034       else
3035         {
3036           fib_table_entry_special_remove (fib_index,
3037                                           &pfx, FIB_SOURCE_CLASSIFY);
3038         }
3039     }
3040
3041   return 0;
3042 }
3043 #endif
3044
3045 static clib_error_t *
3046 set_ip6_classify_command_fn (vlib_main_t * vm,
3047                              unformat_input_t * input,
3048                              vlib_cli_command_t * cmd)
3049 {
3050   u32 table_index = ~0;
3051   int table_index_set = 0;
3052   u32 sw_if_index = ~0;
3053   int rv;
3054
3055   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3056     {
3057       if (unformat (input, "table-index %d", &table_index))
3058         table_index_set = 1;
3059       else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3060                          vnet_get_main (), &sw_if_index))
3061         ;
3062       else
3063         break;
3064     }
3065
3066   if (table_index_set == 0)
3067     return clib_error_return (0, "classify table-index must be specified");
3068
3069   if (sw_if_index == ~0)
3070     return clib_error_return (0, "interface / subif must be specified");
3071
3072   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3073
3074   switch (rv)
3075     {
3076     case 0:
3077       break;
3078
3079     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3080       return clib_error_return (0, "No such interface");
3081
3082     case VNET_API_ERROR_NO_SUCH_ENTRY:
3083       return clib_error_return (0, "No such classifier table");
3084     }
3085   return 0;
3086 }
3087
3088 /*?
3089  * Assign a classification table to an interface. The classification
3090  * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3091  * commands. Once the table is create, use this command to filter packets
3092  * on an interface.
3093  *
3094  * @cliexpar
3095  * Example of how to assign a classification table to an interface:
3096  * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3097 ?*/
3098 /* *INDENT-OFF* */
3099 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3100 {
3101   .path = "set ip6 classify",
3102   .short_help =
3103   "set ip6 classify intfc <interface> table-index <classify-idx>",
3104   .function = set_ip6_classify_command_fn,
3105 };
3106 /* *INDENT-ON* */
3107
3108 static clib_error_t *
3109 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3110 {
3111   ip6_main_t *im = &ip6_main;
3112   uword heapsize = 0;
3113   u32 tmp;
3114   u32 nbuckets = 0;
3115
3116   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3117     {
3118       if (unformat (input, "hash-buckets %d", &tmp))
3119         nbuckets = tmp;
3120       else if (unformat (input, "heap-size %U",
3121                          unformat_memory_size, &heapsize))
3122         ;
3123       else
3124         return clib_error_return (0, "unknown input '%U'",
3125                                   format_unformat_error, input);
3126     }
3127
3128   im->lookup_table_nbuckets = nbuckets;
3129   im->lookup_table_size = heapsize;
3130
3131   return 0;
3132 }
3133
3134 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3135
3136 /*
3137  * fd.io coding-style-patch-verification: ON
3138  *
3139  * Local Variables:
3140  * eval: (c-set-style "gnu")
3141  * End:
3142  */