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