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