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