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