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