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