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