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