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