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