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