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