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