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