Remove unnumbered configuration on interface delete
[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     [IP_LOCAL_NEXT_REASSEMBLY] = "ip6-reassembly",
1356   },
1357 };
1358 /* *INDENT-ON* */
1359
1360 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1361
1362
1363 static uword
1364 ip6_local_end_of_arc (vlib_main_t * vm,
1365                       vlib_node_runtime_t * node, vlib_frame_t * frame)
1366 {
1367   return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1368 }
1369
1370 /* *INDENT-OFF* */
1371 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node,static) = {
1372   .function = ip6_local_end_of_arc,
1373   .name = "ip6-local-end-of-arc",
1374   .vector_size = sizeof (u32),
1375
1376   .format_trace = format_ip6_forward_next_trace,
1377   .sibling_of = "ip6-local",
1378 };
1379
1380 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_end_of_arc_node, ip6_local_end_of_arc)
1381
1382 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1383   .arc_name = "ip6-local",
1384   .node_name = "ip6-local-end-of-arc",
1385   .runs_before = 0, /* not before any other features */
1386 };
1387 /* *INDENT-ON* */
1388
1389 void
1390 ip6_register_protocol (u32 protocol, u32 node_index)
1391 {
1392   vlib_main_t *vm = vlib_get_main ();
1393   ip6_main_t *im = &ip6_main;
1394   ip_lookup_main_t *lm = &im->lookup_main;
1395
1396   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1397   lm->local_next_by_ip_protocol[protocol] =
1398     vlib_node_add_next (vm, ip6_local_node.index, node_index);
1399 }
1400
1401 typedef enum
1402 {
1403   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1404   IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1405   IP6_DISCOVER_NEIGHBOR_N_NEXT,
1406 } ip6_discover_neighbor_next_t;
1407
1408 typedef enum
1409 {
1410   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1411   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1412   IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
1413 } ip6_discover_neighbor_error_t;
1414
1415 static uword
1416 ip6_discover_neighbor_inline (vlib_main_t * vm,
1417                               vlib_node_runtime_t * node,
1418                               vlib_frame_t * frame, int is_glean)
1419 {
1420   vnet_main_t *vnm = vnet_get_main ();
1421   ip6_main_t *im = &ip6_main;
1422   ip_lookup_main_t *lm = &im->lookup_main;
1423   u32 *from, *to_next_drop;
1424   uword n_left_from, n_left_to_next_drop;
1425   static f64 time_last_seed_change = -1e100;
1426   static u32 hash_seeds[3];
1427   static uword hash_bitmap[256 / BITS (uword)];
1428   f64 time_now;
1429   int bogus_length;
1430
1431   if (node->flags & VLIB_NODE_FLAG_TRACE)
1432     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1433
1434   time_now = vlib_time_now (vm);
1435   if (time_now - time_last_seed_change > 1e-3)
1436     {
1437       uword i;
1438       u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
1439                                             sizeof (hash_seeds));
1440       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1441         hash_seeds[i] = r[i];
1442
1443       /* Mark all hash keys as been not-seen before. */
1444       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1445         hash_bitmap[i] = 0;
1446
1447       time_last_seed_change = time_now;
1448     }
1449
1450   from = vlib_frame_vector_args (frame);
1451   n_left_from = frame->n_vectors;
1452
1453   while (n_left_from > 0)
1454     {
1455       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1456                            to_next_drop, n_left_to_next_drop);
1457
1458       while (n_left_from > 0 && n_left_to_next_drop > 0)
1459         {
1460           vlib_buffer_t *p0;
1461           ip6_header_t *ip0;
1462           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1463           uword bm0;
1464           ip_adjacency_t *adj0;
1465           vnet_hw_interface_t *hw_if0;
1466           u32 next0;
1467
1468           pi0 = from[0];
1469
1470           p0 = vlib_get_buffer (vm, pi0);
1471
1472           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1473
1474           ip0 = vlib_buffer_get_current (p0);
1475
1476           adj0 = adj_get (adj_index0);
1477
1478           if (!is_glean)
1479             {
1480               ip0->dst_address.as_u64[0] =
1481                 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
1482               ip0->dst_address.as_u64[1] =
1483                 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
1484             }
1485
1486           a0 = hash_seeds[0];
1487           b0 = hash_seeds[1];
1488           c0 = hash_seeds[2];
1489
1490           sw_if_index0 = adj0->rewrite_header.sw_if_index;
1491           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1492
1493           a0 ^= sw_if_index0;
1494           b0 ^= ip0->dst_address.as_u32[0];
1495           c0 ^= ip0->dst_address.as_u32[1];
1496
1497           hash_v3_mix32 (a0, b0, c0);
1498
1499           b0 ^= ip0->dst_address.as_u32[2];
1500           c0 ^= ip0->dst_address.as_u32[3];
1501
1502           hash_v3_finalize32 (a0, b0, c0);
1503
1504           c0 &= BITS (hash_bitmap) - 1;
1505           c0 = c0 / BITS (uword);
1506           m0 = (uword) 1 << (c0 % BITS (uword));
1507
1508           bm0 = hash_bitmap[c0];
1509           drop0 = (bm0 & m0) != 0;
1510
1511           /* Mark it as seen. */
1512           hash_bitmap[c0] = bm0 | m0;
1513
1514           from += 1;
1515           n_left_from -= 1;
1516           to_next_drop[0] = pi0;
1517           to_next_drop += 1;
1518           n_left_to_next_drop -= 1;
1519
1520           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1521
1522           /* If the interface is link-down, drop the pkt */
1523           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1524             drop0 = 1;
1525
1526           p0->error =
1527             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
1528                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1529           if (drop0)
1530             continue;
1531
1532           /*
1533            * the adj has been updated to a rewrite but the node the DPO that got
1534            * us here hasn't - yet. no big deal. we'll drop while we wait.
1535            */
1536           if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
1537             continue;
1538
1539           {
1540             u32 bi0 = 0;
1541             icmp6_neighbor_solicitation_header_t *h0;
1542             vlib_buffer_t *b0;
1543
1544             h0 = vlib_packet_template_get_packet
1545               (vm, &im->discover_neighbor_packet_template, &bi0);
1546
1547             /*
1548              * Build ethernet header.
1549              * Choose source address based on destination lookup
1550              * adjacency.
1551              */
1552             if (!ip6_src_address_for_packet (lm,
1553                                              sw_if_index0,
1554                                              &ip0->dst_address,
1555                                              &h0->ip.src_address))
1556               {
1557                 /* There is no address on the interface */
1558                 p0->error =
1559                   node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
1560                 vlib_buffer_free (vm, &bi0, 1);
1561                 continue;
1562               }
1563
1564             /*
1565              * Destination address is a solicited node multicast address.
1566              * We need to fill in
1567              * the low 24 bits with low 24 bits of target's address.
1568              */
1569             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1570             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1571             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1572
1573             h0->neighbor.target_address = ip0->dst_address;
1574
1575             clib_memcpy (h0->link_layer_option.ethernet_address,
1576                          hw_if0->hw_address, vec_len (hw_if0->hw_address));
1577
1578             /* $$$$ appears we need this; why is the checksum non-zero? */
1579             h0->neighbor.icmp.checksum = 0;
1580             h0->neighbor.icmp.checksum =
1581               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
1582                                                  &bogus_length);
1583
1584             ASSERT (bogus_length == 0);
1585
1586             vlib_buffer_copy_trace_flag (vm, p0, bi0);
1587             b0 = vlib_get_buffer (vm, bi0);
1588             vnet_buffer (b0)->sw_if_index[VLIB_TX]
1589               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1590
1591             /* Add rewrite/encap string. */
1592             vnet_rewrite_one_header (adj0[0], h0, sizeof (ethernet_header_t));
1593             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1594
1595             next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1596
1597             vlib_set_next_frame_buffer (vm, node, next0, bi0);
1598           }
1599         }
1600
1601       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1602                            n_left_to_next_drop);
1603     }
1604
1605   return frame->n_vectors;
1606 }
1607
1608 static uword
1609 ip6_discover_neighbor (vlib_main_t * vm,
1610                        vlib_node_runtime_t * node, vlib_frame_t * frame)
1611 {
1612   return (ip6_discover_neighbor_inline (vm, node, frame, 0));
1613 }
1614
1615 static uword
1616 ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1617 {
1618   return (ip6_discover_neighbor_inline (vm, node, frame, 1));
1619 }
1620
1621 static char *ip6_discover_neighbor_error_strings[] = {
1622   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1623   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
1624   [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
1625     = "no source address for ND solicitation",
1626 };
1627
1628 /* *INDENT-OFF* */
1629 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
1630 {
1631   .function = ip6_discover_neighbor,
1632   .name = "ip6-discover-neighbor",
1633   .vector_size = sizeof (u32),
1634   .format_trace = format_ip6_forward_next_trace,
1635   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1636   .error_strings = ip6_discover_neighbor_error_strings,
1637   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1638   .next_nodes =
1639   {
1640     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1641     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1642   },
1643 };
1644 /* *INDENT-ON* */
1645
1646 /* *INDENT-OFF* */
1647 VLIB_REGISTER_NODE (ip6_glean_node) =
1648 {
1649   .function = ip6_glean,
1650   .name = "ip6-glean",
1651   .vector_size = sizeof (u32),
1652   .format_trace = format_ip6_forward_next_trace,
1653   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1654   .error_strings = ip6_discover_neighbor_error_strings,
1655   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1656   .next_nodes =
1657   {
1658     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
1659     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1660   },
1661 };
1662 /* *INDENT-ON* */
1663
1664 clib_error_t *
1665 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1666 {
1667   vnet_main_t *vnm = vnet_get_main ();
1668   ip6_main_t *im = &ip6_main;
1669   icmp6_neighbor_solicitation_header_t *h;
1670   ip6_address_t *src;
1671   ip_interface_address_t *ia;
1672   ip_adjacency_t *adj;
1673   vnet_hw_interface_t *hi;
1674   vnet_sw_interface_t *si;
1675   vlib_buffer_t *b;
1676   adj_index_t ai;
1677   u32 bi = 0;
1678   int bogus_length;
1679
1680   si = vnet_get_sw_interface (vnm, sw_if_index);
1681
1682   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1683     {
1684       return clib_error_return (0, "%U: interface %U down",
1685                                 format_ip6_address, dst,
1686                                 format_vnet_sw_if_index_name, vnm,
1687                                 sw_if_index);
1688     }
1689
1690   src =
1691     ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1692   if (!src)
1693     {
1694       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1695       return clib_error_return
1696         (0, "no matching interface address for destination %U (interface %U)",
1697          format_ip6_address, dst,
1698          format_vnet_sw_if_index_name, vnm, sw_if_index);
1699     }
1700
1701   h =
1702     vlib_packet_template_get_packet (vm,
1703                                      &im->discover_neighbor_packet_template,
1704                                      &bi);
1705
1706   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1707
1708   /* Destination address is a solicited node multicast address.  We need to fill in
1709      the low 24 bits with low 24 bits of target's address. */
1710   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1711   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1712   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1713
1714   h->ip.src_address = src[0];
1715   h->neighbor.target_address = dst[0];
1716
1717   if (PREDICT_FALSE (!hi->hw_address))
1718     {
1719       return clib_error_return (0, "%U: interface %U do not support ip probe",
1720                                 format_ip6_address, dst,
1721                                 format_vnet_sw_if_index_name, vnm,
1722                                 sw_if_index);
1723     }
1724
1725   clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1726                vec_len (hi->hw_address));
1727
1728   h->neighbor.icmp.checksum =
1729     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1730   ASSERT (bogus_length == 0);
1731
1732   b = vlib_get_buffer (vm, bi);
1733   vnet_buffer (b)->sw_if_index[VLIB_RX] =
1734     vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1735
1736   /* Add encapsulation string for software interface (e.g. ethernet header). */
1737   ip46_address_t nh = {
1738     .ip6 = *dst,
1739   };
1740
1741   ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1742                             VNET_LINK_IP6, &nh, sw_if_index);
1743   adj = adj_get (ai);
1744
1745   /* Peer has been previously resolved, retrieve glean adj instead */
1746   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1747     {
1748       adj_unlock (ai);
1749       ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1750                                   VNET_LINK_IP6, sw_if_index, &nh);
1751       adj = adj_get (ai);
1752     }
1753
1754   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1755   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1756
1757   {
1758     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1759     u32 *to_next = vlib_frame_vector_args (f);
1760     to_next[0] = bi;
1761     f->n_vectors = 1;
1762     vlib_put_frame_to_node (vm, hi->output_node_index, f);
1763   }
1764
1765   adj_unlock (ai);
1766   return /* no error */ 0;
1767 }
1768
1769 typedef enum
1770 {
1771   IP6_REWRITE_NEXT_DROP,
1772   IP6_REWRITE_NEXT_ICMP_ERROR,
1773 } ip6_rewrite_next_t;
1774
1775 always_inline uword
1776 ip6_rewrite_inline (vlib_main_t * vm,
1777                     vlib_node_runtime_t * node,
1778                     vlib_frame_t * frame,
1779                     int do_counters, int is_midchain, int is_mcast)
1780 {
1781   ip_lookup_main_t *lm = &ip6_main.lookup_main;
1782   u32 *from = vlib_frame_vector_args (frame);
1783   u32 n_left_from, n_left_to_next, *to_next, next_index;
1784   vlib_node_runtime_t *error_node =
1785     vlib_node_get_runtime (vm, ip6_input_node.index);
1786
1787   n_left_from = frame->n_vectors;
1788   next_index = node->cached_next_index;
1789   u32 thread_index = vlib_get_thread_index ();
1790
1791   while (n_left_from > 0)
1792     {
1793       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1794
1795       while (n_left_from >= 4 && n_left_to_next >= 2)
1796         {
1797           ip_adjacency_t *adj0, *adj1;
1798           vlib_buffer_t *p0, *p1;
1799           ip6_header_t *ip0, *ip1;
1800           u32 pi0, rw_len0, next0, error0, adj_index0;
1801           u32 pi1, rw_len1, next1, error1, adj_index1;
1802           u32 tx_sw_if_index0, tx_sw_if_index1;
1803
1804           /* Prefetch next iteration. */
1805           {
1806             vlib_buffer_t *p2, *p3;
1807
1808             p2 = vlib_get_buffer (vm, from[2]);
1809             p3 = vlib_get_buffer (vm, from[3]);
1810
1811             vlib_prefetch_buffer_header (p2, LOAD);
1812             vlib_prefetch_buffer_header (p3, LOAD);
1813
1814             CLIB_PREFETCH (p2->pre_data, 32, STORE);
1815             CLIB_PREFETCH (p3->pre_data, 32, STORE);
1816
1817             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1818             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1819           }
1820
1821           pi0 = to_next[0] = from[0];
1822           pi1 = to_next[1] = from[1];
1823
1824           from += 2;
1825           n_left_from -= 2;
1826           to_next += 2;
1827           n_left_to_next -= 2;
1828
1829           p0 = vlib_get_buffer (vm, pi0);
1830           p1 = vlib_get_buffer (vm, pi1);
1831
1832           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1833           adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1834
1835           ip0 = vlib_buffer_get_current (p0);
1836           ip1 = vlib_buffer_get_current (p1);
1837
1838           error0 = error1 = IP6_ERROR_NONE;
1839           next0 = next1 = IP6_REWRITE_NEXT_DROP;
1840
1841           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1842             {
1843               i32 hop_limit0 = ip0->hop_limit;
1844
1845               /* Input node should have reject packets with hop limit 0. */
1846               ASSERT (ip0->hop_limit > 0);
1847
1848               hop_limit0 -= 1;
1849
1850               ip0->hop_limit = hop_limit0;
1851
1852               /*
1853                * If the hop count drops below 1 when forwarding, generate
1854                * an ICMP response.
1855                */
1856               if (PREDICT_FALSE (hop_limit0 <= 0))
1857                 {
1858                   error0 = IP6_ERROR_TIME_EXPIRED;
1859                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1860                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1861                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1862                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1863                                                0);
1864                 }
1865             }
1866           else
1867             {
1868               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1869             }
1870           if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1871             {
1872               i32 hop_limit1 = ip1->hop_limit;
1873
1874               /* Input node should have reject packets with hop limit 0. */
1875               ASSERT (ip1->hop_limit > 0);
1876
1877               hop_limit1 -= 1;
1878
1879               ip1->hop_limit = hop_limit1;
1880
1881               /*
1882                * If the hop count drops below 1 when forwarding, generate
1883                * an ICMP response.
1884                */
1885               if (PREDICT_FALSE (hop_limit1 <= 0))
1886                 {
1887                   error1 = IP6_ERROR_TIME_EXPIRED;
1888                   next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1889                   vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1890                   icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1891                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1892                                                0);
1893                 }
1894             }
1895           else
1896             {
1897               p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1898             }
1899           adj0 = adj_get (adj_index0);
1900           adj1 = adj_get (adj_index1);
1901
1902           rw_len0 = adj0[0].rewrite_header.data_bytes;
1903           rw_len1 = adj1[0].rewrite_header.data_bytes;
1904           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1905           vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1906
1907           if (do_counters)
1908             {
1909               vlib_increment_combined_counter
1910                 (&adjacency_counters,
1911                  thread_index, adj_index0, 1,
1912                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1913               vlib_increment_combined_counter
1914                 (&adjacency_counters,
1915                  thread_index, adj_index1, 1,
1916                  vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1917             }
1918
1919           /* Check MTU of outgoing interface. */
1920           error0 =
1921             (vlib_buffer_length_in_chain (vm, p0) >
1922              adj0[0].
1923              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1924              error0);
1925           error1 =
1926             (vlib_buffer_length_in_chain (vm, p1) >
1927              adj1[0].
1928              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1929              error1);
1930
1931           /* Don't adjust the buffer for hop count issue; icmp-error node
1932            * wants to see the IP headerr */
1933           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1934             {
1935               p0->current_data -= rw_len0;
1936               p0->current_length += rw_len0;
1937
1938               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1939               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1940               next0 = adj0[0].rewrite_header.next_index;
1941
1942               if (PREDICT_FALSE
1943                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1944                 vnet_feature_arc_start (lm->output_feature_arc_index,
1945                                         tx_sw_if_index0, &next0, p0);
1946             }
1947           if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1948             {
1949               p1->current_data -= rw_len1;
1950               p1->current_length += rw_len1;
1951
1952               tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1953               vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1954               next1 = adj1[0].rewrite_header.next_index;
1955
1956               if (PREDICT_FALSE
1957                   (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1958                 vnet_feature_arc_start (lm->output_feature_arc_index,
1959                                         tx_sw_if_index1, &next1, p1);
1960             }
1961
1962           /* Guess we are only writing on simple Ethernet header. */
1963           vnet_rewrite_two_headers (adj0[0], adj1[0],
1964                                     ip0, ip1, sizeof (ethernet_header_t));
1965
1966           if (is_midchain)
1967             {
1968               adj0->sub_type.midchain.fixup_func
1969                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1970               adj1->sub_type.midchain.fixup_func
1971                 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1972             }
1973           if (is_mcast)
1974             {
1975               /*
1976                * copy bytes from the IP address into the MAC rewrite
1977                */
1978               vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
1979               vnet_fixup_one_header (adj1[0], &ip1->dst_address, ip1);
1980             }
1981
1982           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1983                                            to_next, n_left_to_next,
1984                                            pi0, pi1, next0, next1);
1985         }
1986
1987       while (n_left_from > 0 && n_left_to_next > 0)
1988         {
1989           ip_adjacency_t *adj0;
1990           vlib_buffer_t *p0;
1991           ip6_header_t *ip0;
1992           u32 pi0, rw_len0;
1993           u32 adj_index0, next0, error0;
1994           u32 tx_sw_if_index0;
1995
1996           pi0 = to_next[0] = from[0];
1997
1998           p0 = vlib_get_buffer (vm, pi0);
1999
2000           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
2001
2002           adj0 = adj_get (adj_index0);
2003
2004           ip0 = vlib_buffer_get_current (p0);
2005
2006           error0 = IP6_ERROR_NONE;
2007           next0 = IP6_REWRITE_NEXT_DROP;
2008
2009           /* Check hop limit */
2010           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
2011             {
2012               i32 hop_limit0 = ip0->hop_limit;
2013
2014               ASSERT (ip0->hop_limit > 0);
2015
2016               hop_limit0 -= 1;
2017
2018               ip0->hop_limit = hop_limit0;
2019
2020               if (PREDICT_FALSE (hop_limit0 <= 0))
2021                 {
2022                   /*
2023                    * If the hop count drops below 1 when forwarding, generate
2024                    * an ICMP response.
2025                    */
2026                   error0 = IP6_ERROR_TIME_EXPIRED;
2027                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
2028                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
2029                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
2030                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
2031                                                0);
2032                 }
2033             }
2034           else
2035             {
2036               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
2037             }
2038
2039           /* Guess we are only writing on simple Ethernet header. */
2040           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2041
2042           /* Update packet buffer attributes/set output interface. */
2043           rw_len0 = adj0[0].rewrite_header.data_bytes;
2044           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
2045
2046           if (do_counters)
2047             {
2048               vlib_increment_combined_counter
2049                 (&adjacency_counters,
2050                  thread_index, adj_index0, 1,
2051                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
2052             }
2053
2054           /* Check MTU of outgoing interface. */
2055           error0 =
2056             (vlib_buffer_length_in_chain (vm, p0) >
2057              adj0[0].
2058              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
2059              error0);
2060
2061           /* Don't adjust the buffer for hop count issue; icmp-error node
2062            * wants to see the IP headerr */
2063           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
2064             {
2065               p0->current_data -= rw_len0;
2066               p0->current_length += rw_len0;
2067
2068               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
2069
2070               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
2071               next0 = adj0[0].rewrite_header.next_index;
2072
2073               if (PREDICT_FALSE
2074                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
2075                 vnet_feature_arc_start (lm->output_feature_arc_index,
2076                                         tx_sw_if_index0, &next0, p0);
2077             }
2078
2079           if (is_midchain)
2080             {
2081               adj0->sub_type.midchain.fixup_func
2082                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
2083             }
2084           if (is_mcast)
2085             {
2086               vnet_fixup_one_header (adj0[0], &ip0->dst_address, ip0);
2087             }
2088
2089           p0->error = error_node->errors[error0];
2090
2091           from += 1;
2092           n_left_from -= 1;
2093           to_next += 1;
2094           n_left_to_next -= 1;
2095
2096           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2097                                            to_next, n_left_to_next,
2098                                            pi0, next0);
2099         }
2100
2101       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2102     }
2103
2104   /* Need to do trace after rewrites to pick up new packet data. */
2105   if (node->flags & VLIB_NODE_FLAG_TRACE)
2106     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
2107
2108   return frame->n_vectors;
2109 }
2110
2111 static uword
2112 ip6_rewrite (vlib_main_t * vm,
2113              vlib_node_runtime_t * node, vlib_frame_t * frame)
2114 {
2115   if (adj_are_counters_enabled ())
2116     return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
2117   else
2118     return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
2119 }
2120
2121 static uword
2122 ip6_rewrite_mcast (vlib_main_t * vm,
2123                    vlib_node_runtime_t * node, vlib_frame_t * frame)
2124 {
2125   if (adj_are_counters_enabled ())
2126     return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
2127   else
2128     return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
2129 }
2130
2131 static uword
2132 ip6_midchain (vlib_main_t * vm,
2133               vlib_node_runtime_t * node, vlib_frame_t * frame)
2134 {
2135   if (adj_are_counters_enabled ())
2136     return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
2137   else
2138     return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
2139 }
2140
2141 static uword
2142 ip6_mcast_midchain (vlib_main_t * vm,
2143                     vlib_node_runtime_t * node, vlib_frame_t * frame)
2144 {
2145   if (adj_are_counters_enabled ())
2146     return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
2147   else
2148     return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
2149 }
2150
2151 /* *INDENT-OFF* */
2152 VLIB_REGISTER_NODE (ip6_midchain_node) =
2153 {
2154   .function = ip6_midchain,
2155   .name = "ip6-midchain",
2156   .vector_size = sizeof (u32),
2157   .format_trace = format_ip6_forward_next_trace,
2158   .sibling_of = "ip6-rewrite",
2159   };
2160 /* *INDENT-ON* */
2161
2162 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
2163
2164 /* *INDENT-OFF* */
2165 VLIB_REGISTER_NODE (ip6_rewrite_node) =
2166 {
2167   .function = ip6_rewrite,
2168   .name = "ip6-rewrite",
2169   .vector_size = sizeof (u32),
2170   .format_trace = format_ip6_rewrite_trace,
2171   .n_next_nodes = 2,
2172   .next_nodes =
2173   {
2174     [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
2175     [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
2176   },
2177 };
2178 /* *INDENT-ON* */
2179
2180 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
2181
2182 /* *INDENT-OFF* */
2183 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
2184 {
2185   .function = ip6_rewrite_mcast,
2186   .name = "ip6-rewrite-mcast",
2187   .vector_size = sizeof (u32),
2188   .format_trace = format_ip6_rewrite_trace,
2189   .sibling_of = "ip6-rewrite",
2190 };
2191 /* *INDENT-ON* */
2192
2193 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
2194
2195 /* *INDENT-OFF* */
2196 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
2197 {
2198   .function = ip6_mcast_midchain,
2199   .name = "ip6-mcast-midchain",
2200   .vector_size = sizeof (u32),
2201   .format_trace = format_ip6_rewrite_trace,
2202   .sibling_of = "ip6-rewrite",
2203 };
2204 /* *INDENT-ON* */
2205
2206 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
2207
2208 /*
2209  * Hop-by-Hop handling
2210  */
2211 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2212
2213 #define foreach_ip6_hop_by_hop_error \
2214 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2215 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2216 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2217
2218 /* *INDENT-OFF* */
2219 typedef enum
2220 {
2221 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2222   foreach_ip6_hop_by_hop_error
2223 #undef _
2224   IP6_HOP_BY_HOP_N_ERROR,
2225 } ip6_hop_by_hop_error_t;
2226 /* *INDENT-ON* */
2227
2228 /*
2229  * Primary h-b-h handler trace support
2230  * We work pretty hard on the problem for obvious reasons
2231  */
2232 typedef struct
2233 {
2234   u32 next_index;
2235   u32 trace_len;
2236   u8 option_data[256];
2237 } ip6_hop_by_hop_trace_t;
2238
2239 vlib_node_registration_t ip6_hop_by_hop_node;
2240
2241 static char *ip6_hop_by_hop_error_strings[] = {
2242 #define _(sym,string) string,
2243   foreach_ip6_hop_by_hop_error
2244 #undef _
2245 };
2246
2247 u8 *
2248 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2249 {
2250   ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2251   int total_len = va_arg (*args, int);
2252   ip6_hop_by_hop_option_t *opt0, *limit0;
2253   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2254   u8 type0;
2255
2256   s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2257               hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2258
2259   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2260   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2261
2262   while (opt0 < limit0)
2263     {
2264       type0 = opt0->type;
2265       switch (type0)
2266         {
2267         case 0:         /* Pad, just stop */
2268           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2269           break;
2270
2271         default:
2272           if (hm->trace[type0])
2273             {
2274               s = (*hm->trace[type0]) (s, opt0);
2275             }
2276           else
2277             {
2278               s =
2279                 format (s, "\n    unrecognized option %d length %d", type0,
2280                         opt0->length);
2281             }
2282           opt0 =
2283             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2284                                          sizeof (ip6_hop_by_hop_option_t));
2285           break;
2286         }
2287     }
2288   return s;
2289 }
2290
2291 static u8 *
2292 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2293 {
2294   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2295   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2296   ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2297   ip6_hop_by_hop_header_t *hbh0;
2298   ip6_hop_by_hop_option_t *opt0, *limit0;
2299   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2300
2301   u8 type0;
2302
2303   hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2304
2305   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2306               t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2307
2308   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2309   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2310
2311   while (opt0 < limit0)
2312     {
2313       type0 = opt0->type;
2314       switch (type0)
2315         {
2316         case 0:         /* Pad, just stop */
2317           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2318           break;
2319
2320         default:
2321           if (hm->trace[type0])
2322             {
2323               s = (*hm->trace[type0]) (s, opt0);
2324             }
2325           else
2326             {
2327               s =
2328                 format (s, "\n    unrecognized option %d length %d", type0,
2329                         opt0->length);
2330             }
2331           opt0 =
2332             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2333                                          sizeof (ip6_hop_by_hop_option_t));
2334           break;
2335         }
2336     }
2337   return s;
2338 }
2339
2340 always_inline u8
2341 ip6_scan_hbh_options (vlib_buffer_t * b0,
2342                       ip6_header_t * ip0,
2343                       ip6_hop_by_hop_header_t * hbh0,
2344                       ip6_hop_by_hop_option_t * opt0,
2345                       ip6_hop_by_hop_option_t * limit0, u32 * next0)
2346 {
2347   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2348   u8 type0;
2349   u8 error0 = 0;
2350
2351   while (opt0 < limit0)
2352     {
2353       type0 = opt0->type;
2354       switch (type0)
2355         {
2356         case 0:         /* Pad1 */
2357           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2358           continue;
2359         case 1:         /* PadN */
2360           break;
2361         default:
2362           if (hm->options[type0])
2363             {
2364               if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2365                 {
2366                   error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2367                   return (error0);
2368                 }
2369             }
2370           else
2371             {
2372               /* Unrecognized mandatory option, check the two high order bits */
2373               switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2374                 {
2375                 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2376                   break;
2377                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2378                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2379                   *next0 = IP_LOOKUP_NEXT_DROP;
2380                   break;
2381                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2382                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2383                   *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2384                   icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2385                                                ICMP6_parameter_problem_unrecognized_option,
2386                                                (u8 *) opt0 - (u8 *) ip0);
2387                   break;
2388                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2389                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2390                   if (!ip6_address_is_multicast (&ip0->dst_address))
2391                     {
2392                       *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2393                       icmp6_error_set_vnet_buffer (b0,
2394                                                    ICMP6_parameter_problem,
2395                                                    ICMP6_parameter_problem_unrecognized_option,
2396                                                    (u8 *) opt0 - (u8 *) ip0);
2397                     }
2398                   else
2399                     {
2400                       *next0 = IP_LOOKUP_NEXT_DROP;
2401                     }
2402                   break;
2403                 }
2404               return (error0);
2405             }
2406         }
2407       opt0 =
2408         (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2409                                      sizeof (ip6_hop_by_hop_option_t));
2410     }
2411   return (error0);
2412 }
2413
2414 /*
2415  * Process the Hop-by-Hop Options header
2416  */
2417 static uword
2418 ip6_hop_by_hop (vlib_main_t * vm,
2419                 vlib_node_runtime_t * node, vlib_frame_t * frame)
2420 {
2421   vlib_node_runtime_t *error_node =
2422     vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2423   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2424   u32 n_left_from, *from, *to_next;
2425   ip_lookup_next_t next_index;
2426
2427   from = vlib_frame_vector_args (frame);
2428   n_left_from = frame->n_vectors;
2429   next_index = node->cached_next_index;
2430
2431   while (n_left_from > 0)
2432     {
2433       u32 n_left_to_next;
2434
2435       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2436
2437       while (n_left_from >= 4 && n_left_to_next >= 2)
2438         {
2439           u32 bi0, bi1;
2440           vlib_buffer_t *b0, *b1;
2441           u32 next0, next1;
2442           ip6_header_t *ip0, *ip1;
2443           ip6_hop_by_hop_header_t *hbh0, *hbh1;
2444           ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2445           u8 error0 = 0, error1 = 0;
2446
2447           /* Prefetch next iteration. */
2448           {
2449             vlib_buffer_t *p2, *p3;
2450
2451             p2 = vlib_get_buffer (vm, from[2]);
2452             p3 = vlib_get_buffer (vm, from[3]);
2453
2454             vlib_prefetch_buffer_header (p2, LOAD);
2455             vlib_prefetch_buffer_header (p3, LOAD);
2456
2457             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2458             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2459           }
2460
2461           /* Speculatively enqueue b0, b1 to the current next frame */
2462           to_next[0] = bi0 = from[0];
2463           to_next[1] = bi1 = from[1];
2464           from += 2;
2465           to_next += 2;
2466           n_left_from -= 2;
2467           n_left_to_next -= 2;
2468
2469           b0 = vlib_get_buffer (vm, bi0);
2470           b1 = vlib_get_buffer (vm, bi1);
2471
2472           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2473           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2474           ip_adjacency_t *adj0 = adj_get (adj_index0);
2475           u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2476           ip_adjacency_t *adj1 = adj_get (adj_index1);
2477
2478           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2479           next0 = adj0->lookup_next_index;
2480           next1 = adj1->lookup_next_index;
2481
2482           ip0 = vlib_buffer_get_current (b0);
2483           ip1 = vlib_buffer_get_current (b1);
2484           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2485           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2486           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2487           opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2488           limit0 =
2489             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2490                                          ((hbh0->length + 1) << 3));
2491           limit1 =
2492             (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2493                                          ((hbh1->length + 1) << 3));
2494
2495           /*
2496            * Basic validity checks
2497            */
2498           if ((hbh0->length + 1) << 3 >
2499               clib_net_to_host_u16 (ip0->payload_length))
2500             {
2501               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2502               next0 = IP_LOOKUP_NEXT_DROP;
2503               goto outdual;
2504             }
2505           /* Scan the set of h-b-h options, process ones that we understand */
2506           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2507
2508           if ((hbh1->length + 1) << 3 >
2509               clib_net_to_host_u16 (ip1->payload_length))
2510             {
2511               error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2512               next1 = IP_LOOKUP_NEXT_DROP;
2513               goto outdual;
2514             }
2515           /* Scan the set of h-b-h options, process ones that we understand */
2516           error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2517
2518         outdual:
2519           /* Has the classifier flagged this buffer for special treatment? */
2520           if (PREDICT_FALSE
2521               ((error0 == 0)
2522                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2523             next0 = hm->next_override;
2524
2525           /* Has the classifier flagged this buffer for special treatment? */
2526           if (PREDICT_FALSE
2527               ((error1 == 0)
2528                && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2529             next1 = hm->next_override;
2530
2531           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2532             {
2533               if (b0->flags & VLIB_BUFFER_IS_TRACED)
2534                 {
2535                   ip6_hop_by_hop_trace_t *t =
2536                     vlib_add_trace (vm, node, b0, sizeof (*t));
2537                   u32 trace_len = (hbh0->length + 1) << 3;
2538                   t->next_index = next0;
2539                   /* Capture the h-b-h option verbatim */
2540                   trace_len =
2541                     trace_len <
2542                     ARRAY_LEN (t->option_data) ? trace_len :
2543                     ARRAY_LEN (t->option_data);
2544                   t->trace_len = trace_len;
2545                   clib_memcpy (t->option_data, hbh0, trace_len);
2546                 }
2547               if (b1->flags & VLIB_BUFFER_IS_TRACED)
2548                 {
2549                   ip6_hop_by_hop_trace_t *t =
2550                     vlib_add_trace (vm, node, b1, sizeof (*t));
2551                   u32 trace_len = (hbh1->length + 1) << 3;
2552                   t->next_index = next1;
2553                   /* Capture the h-b-h option verbatim */
2554                   trace_len =
2555                     trace_len <
2556                     ARRAY_LEN (t->option_data) ? trace_len :
2557                     ARRAY_LEN (t->option_data);
2558                   t->trace_len = trace_len;
2559                   clib_memcpy (t->option_data, hbh1, trace_len);
2560                 }
2561
2562             }
2563
2564           b0->error = error_node->errors[error0];
2565           b1->error = error_node->errors[error1];
2566
2567           /* verify speculative enqueue, maybe switch current next frame */
2568           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2569                                            n_left_to_next, bi0, bi1, next0,
2570                                            next1);
2571         }
2572
2573       while (n_left_from > 0 && n_left_to_next > 0)
2574         {
2575           u32 bi0;
2576           vlib_buffer_t *b0;
2577           u32 next0;
2578           ip6_header_t *ip0;
2579           ip6_hop_by_hop_header_t *hbh0;
2580           ip6_hop_by_hop_option_t *opt0, *limit0;
2581           u8 error0 = 0;
2582
2583           /* Speculatively enqueue b0 to the current next frame */
2584           bi0 = from[0];
2585           to_next[0] = bi0;
2586           from += 1;
2587           to_next += 1;
2588           n_left_from -= 1;
2589           n_left_to_next -= 1;
2590
2591           b0 = vlib_get_buffer (vm, bi0);
2592           /*
2593            * Default use the next_index from the adjacency.
2594            * A HBH option rarely redirects to a different node
2595            */
2596           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2597           ip_adjacency_t *adj0 = adj_get (adj_index0);
2598           next0 = adj0->lookup_next_index;
2599
2600           ip0 = vlib_buffer_get_current (b0);
2601           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2602           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2603           limit0 =
2604             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2605                                          ((hbh0->length + 1) << 3));
2606
2607           /*
2608            * Basic validity checks
2609            */
2610           if ((hbh0->length + 1) << 3 >
2611               clib_net_to_host_u16 (ip0->payload_length))
2612             {
2613               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2614               next0 = IP_LOOKUP_NEXT_DROP;
2615               goto out0;
2616             }
2617
2618           /* Scan the set of h-b-h options, process ones that we understand */
2619           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2620
2621         out0:
2622           /* Has the classifier flagged this buffer for special treatment? */
2623           if (PREDICT_FALSE
2624               ((error0 == 0)
2625                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2626             next0 = hm->next_override;
2627
2628           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2629             {
2630               ip6_hop_by_hop_trace_t *t =
2631                 vlib_add_trace (vm, node, b0, sizeof (*t));
2632               u32 trace_len = (hbh0->length + 1) << 3;
2633               t->next_index = next0;
2634               /* Capture the h-b-h option verbatim */
2635               trace_len =
2636                 trace_len <
2637                 ARRAY_LEN (t->option_data) ? trace_len :
2638                 ARRAY_LEN (t->option_data);
2639               t->trace_len = trace_len;
2640               clib_memcpy (t->option_data, hbh0, trace_len);
2641             }
2642
2643           b0->error = error_node->errors[error0];
2644
2645           /* verify speculative enqueue, maybe switch current next frame */
2646           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2647                                            n_left_to_next, bi0, next0);
2648         }
2649       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2650     }
2651   return frame->n_vectors;
2652 }
2653
2654 /* *INDENT-OFF* */
2655 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2656 {
2657   .function = ip6_hop_by_hop,
2658   .name = "ip6-hop-by-hop",
2659   .sibling_of = "ip6-lookup",
2660   .vector_size = sizeof (u32),
2661   .format_trace = format_ip6_hop_by_hop_trace,
2662   .type = VLIB_NODE_TYPE_INTERNAL,
2663   .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2664   .error_strings = ip6_hop_by_hop_error_strings,
2665   .n_next_nodes = 0,
2666 };
2667 /* *INDENT-ON* */
2668
2669 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2670
2671 static clib_error_t *
2672 ip6_hop_by_hop_init (vlib_main_t * vm)
2673 {
2674   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2675   memset (hm->options, 0, sizeof (hm->options));
2676   memset (hm->trace, 0, sizeof (hm->trace));
2677   hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2678   return (0);
2679 }
2680
2681 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2682
2683 void
2684 ip6_hbh_set_next_override (uword next)
2685 {
2686   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2687
2688   hm->next_override = next;
2689 }
2690
2691 int
2692 ip6_hbh_register_option (u8 option,
2693                          int options (vlib_buffer_t * b, ip6_header_t * ip,
2694                                       ip6_hop_by_hop_option_t * opt),
2695                          u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2696 {
2697   ip6_main_t *im = &ip6_main;
2698   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2699
2700   ASSERT (option < ARRAY_LEN (hm->options));
2701
2702   /* Already registered */
2703   if (hm->options[option])
2704     return (-1);
2705
2706   hm->options[option] = options;
2707   hm->trace[option] = trace;
2708
2709   /* Set global variable */
2710   im->hbh_enabled = 1;
2711
2712   return (0);
2713 }
2714
2715 int
2716 ip6_hbh_unregister_option (u8 option)
2717 {
2718   ip6_main_t *im = &ip6_main;
2719   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2720
2721   ASSERT (option < ARRAY_LEN (hm->options));
2722
2723   /* Not registered */
2724   if (!hm->options[option])
2725     return (-1);
2726
2727   hm->options[option] = NULL;
2728   hm->trace[option] = NULL;
2729
2730   /* Disable global knob if this was the last option configured */
2731   int i;
2732   bool found = false;
2733   for (i = 0; i < 256; i++)
2734     {
2735       if (hm->options[option])
2736         {
2737           found = true;
2738           break;
2739         }
2740     }
2741   if (!found)
2742     im->hbh_enabled = 0;
2743
2744   return (0);
2745 }
2746
2747 /* Global IP6 main. */
2748 ip6_main_t ip6_main;
2749
2750 static clib_error_t *
2751 ip6_lookup_init (vlib_main_t * vm)
2752 {
2753   ip6_main_t *im = &ip6_main;
2754   clib_error_t *error;
2755   uword i;
2756
2757   if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2758     return error;
2759
2760   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2761     {
2762       u32 j, i0, i1;
2763
2764       i0 = i / 32;
2765       i1 = i % 32;
2766
2767       for (j = 0; j < i0; j++)
2768         im->fib_masks[i].as_u32[j] = ~0;
2769
2770       if (i1)
2771         im->fib_masks[i].as_u32[i0] =
2772           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2773     }
2774
2775   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2776
2777   if (im->lookup_table_nbuckets == 0)
2778     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2779
2780   im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2781
2782   if (im->lookup_table_size == 0)
2783     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2784
2785   BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2786                          "ip6 FIB fwding table",
2787                          im->lookup_table_nbuckets, im->lookup_table_size);
2788   BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2789                          "ip6 FIB non-fwding table",
2790                          im->lookup_table_nbuckets, im->lookup_table_size);
2791
2792   /* Create FIB with index 0 and table id of 0. */
2793   fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2794                                      FIB_SOURCE_DEFAULT_ROUTE);
2795   mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2796                                       MFIB_SOURCE_DEFAULT_ROUTE);
2797
2798   {
2799     pg_node_t *pn;
2800     pn = pg_get_node (ip6_lookup_node.index);
2801     pn->unformat_edit = unformat_pg_ip6_header;
2802   }
2803
2804   /* Unless explicitly configured, don't process HBH options */
2805   im->hbh_enabled = 0;
2806
2807   {
2808     icmp6_neighbor_solicitation_header_t p;
2809
2810     memset (&p, 0, sizeof (p));
2811
2812     p.ip.ip_version_traffic_class_and_flow_label =
2813       clib_host_to_net_u32 (0x6 << 28);
2814     p.ip.payload_length =
2815       clib_host_to_net_u16 (sizeof (p) -
2816                             STRUCT_OFFSET_OF
2817                             (icmp6_neighbor_solicitation_header_t, neighbor));
2818     p.ip.protocol = IP_PROTOCOL_ICMP6;
2819     p.ip.hop_limit = 255;
2820     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2821
2822     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2823
2824     p.link_layer_option.header.type =
2825       ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2826     p.link_layer_option.header.n_data_u64s =
2827       sizeof (p.link_layer_option) / sizeof (u64);
2828
2829     vlib_packet_template_init (vm,
2830                                &im->discover_neighbor_packet_template,
2831                                &p, sizeof (p),
2832                                /* alloc chunk size */ 8,
2833                                "ip6 neighbor discovery");
2834   }
2835
2836   return error;
2837 }
2838
2839 VLIB_INIT_FUNCTION (ip6_lookup_init);
2840
2841 void
2842 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
2843                                                   u8 * mac)
2844 {
2845   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2846   /* Invert the "u" bit */
2847   ip->as_u8[8] = mac[0] ^ (1 << 1);
2848   ip->as_u8[9] = mac[1];
2849   ip->as_u8[10] = mac[2];
2850   ip->as_u8[11] = 0xFF;
2851   ip->as_u8[12] = 0xFE;
2852   ip->as_u8[13] = mac[3];
2853   ip->as_u8[14] = mac[4];
2854   ip->as_u8[15] = mac[5];
2855 }
2856
2857 void
2858 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
2859                                                   ip6_address_t * ip)
2860 {
2861   /* Invert the previously inverted "u" bit */
2862   mac[0] = ip->as_u8[8] ^ (1 << 1);
2863   mac[1] = ip->as_u8[9];
2864   mac[2] = ip->as_u8[10];
2865   mac[3] = ip->as_u8[13];
2866   mac[4] = ip->as_u8[14];
2867   mac[5] = ip->as_u8[15];
2868 }
2869
2870 static clib_error_t *
2871 test_ip6_link_command_fn (vlib_main_t * vm,
2872                           unformat_input_t * input, vlib_cli_command_t * cmd)
2873 {
2874   u8 mac[6];
2875   ip6_address_t _a, *a = &_a;
2876
2877   if (unformat (input, "%U", unformat_ethernet_address, mac))
2878     {
2879       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2880       vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2881       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2882       vlib_cli_output (vm, "Original MAC address: %U",
2883                        format_ethernet_address, mac);
2884     }
2885
2886   return 0;
2887 }
2888
2889 /*?
2890  * This command converts the given MAC Address into an IPv6 link-local
2891  * address.
2892  *
2893  * @cliexpar
2894  * Example of how to create an IPv6 link-local address:
2895  * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2896  * Link local address: fe80::14d9:e0ff:fe91:7986
2897  * Original MAC address: 16:d9:e0:91:79:86
2898  * @cliexend
2899 ?*/
2900 /* *INDENT-OFF* */
2901 VLIB_CLI_COMMAND (test_link_command, static) =
2902 {
2903   .path = "test ip6 link",
2904   .function = test_ip6_link_command_fn,
2905   .short_help = "test ip6 link <mac-address>",
2906 };
2907 /* *INDENT-ON* */
2908
2909 int
2910 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2911 {
2912   u32 fib_index;
2913
2914   fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2915
2916   if (~0 == fib_index)
2917     return VNET_API_ERROR_NO_SUCH_FIB;
2918
2919   fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2920                                   flow_hash_config);
2921
2922   return 0;
2923 }
2924
2925 static clib_error_t *
2926 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2927                               unformat_input_t * input,
2928                               vlib_cli_command_t * cmd)
2929 {
2930   int matched = 0;
2931   u32 table_id = 0;
2932   u32 flow_hash_config = 0;
2933   int rv;
2934
2935   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2936     {
2937       if (unformat (input, "table %d", &table_id))
2938         matched = 1;
2939 #define _(a,v) \
2940     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2941       foreach_flow_hash_bit
2942 #undef _
2943         else
2944         break;
2945     }
2946
2947   if (matched == 0)
2948     return clib_error_return (0, "unknown input `%U'",
2949                               format_unformat_error, input);
2950
2951   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2952   switch (rv)
2953     {
2954     case 0:
2955       break;
2956
2957     case -1:
2958       return clib_error_return (0, "no such FIB table %d", table_id);
2959
2960     default:
2961       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2962       break;
2963     }
2964
2965   return 0;
2966 }
2967
2968 /*?
2969  * Configure the set of IPv6 fields used by the flow hash.
2970  *
2971  * @cliexpar
2972  * @parblock
2973  * Example of how to set the flow hash on a given table:
2974  * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2975  *
2976  * Example of display the configured flow hash:
2977  * @cliexstart{show ip6 fib}
2978  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2979  * @::/0
2980  *   unicast-ip6-chain
2981  *   [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2982  *     [0] [@0]: dpo-drop ip6
2983  * fe80::/10
2984  *   unicast-ip6-chain
2985  *   [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2986  *     [0] [@2]: dpo-receive
2987  * ff02::1/128
2988  *   unicast-ip6-chain
2989  *   [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2990  *     [0] [@2]: dpo-receive
2991  * ff02::2/128
2992  *   unicast-ip6-chain
2993  *   [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2994  *     [0] [@2]: dpo-receive
2995  * ff02::16/128
2996  *   unicast-ip6-chain
2997  *   [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2998  *     [0] [@2]: dpo-receive
2999  * ff02::1:ff00:0/104
3000  *   unicast-ip6-chain
3001  *   [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
3002  *     [0] [@2]: dpo-receive
3003  * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
3004  * @::/0
3005  *   unicast-ip6-chain
3006  *   [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
3007  *     [0] [@0]: dpo-drop ip6
3008  * @::a:1:1:0:4/126
3009  *   unicast-ip6-chain
3010  *   [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
3011  *     [0] [@4]: ipv6-glean: af_packet0
3012  * @::a:1:1:0:7/128
3013  *   unicast-ip6-chain
3014  *   [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
3015  *     [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
3016  * fe80::/10
3017  *   unicast-ip6-chain
3018  *   [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
3019  *     [0] [@2]: dpo-receive
3020  * fe80::fe:3eff:fe3e:9222/128
3021  *   unicast-ip6-chain
3022  *   [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
3023  *     [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
3024  * ff02::1/128
3025  *   unicast-ip6-chain
3026  *   [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
3027  *     [0] [@2]: dpo-receive
3028  * ff02::2/128
3029  *   unicast-ip6-chain
3030  *   [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
3031  *     [0] [@2]: dpo-receive
3032  * ff02::16/128
3033  *   unicast-ip6-chain
3034  *   [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
3035  *     [0] [@2]: dpo-receive
3036  * ff02::1:ff00:0/104
3037  *   unicast-ip6-chain
3038  *   [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
3039  *     [0] [@2]: dpo-receive
3040  * @cliexend
3041  * @endparblock
3042 ?*/
3043 /* *INDENT-OFF* */
3044 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
3045 {
3046   .path = "set ip6 flow-hash",
3047   .short_help =
3048   "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
3049   .function = set_ip6_flow_hash_command_fn,
3050 };
3051 /* *INDENT-ON* */
3052
3053 static clib_error_t *
3054 show_ip6_local_command_fn (vlib_main_t * vm,
3055                            unformat_input_t * input, vlib_cli_command_t * cmd)
3056 {
3057   ip6_main_t *im = &ip6_main;
3058   ip_lookup_main_t *lm = &im->lookup_main;
3059   int i;
3060
3061   vlib_cli_output (vm, "Protocols handled by ip6_local");
3062   for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
3063     {
3064       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3065         {
3066
3067           u32 node_index = vlib_get_node (vm,
3068                                           ip6_local_node.index)->
3069             next_nodes[lm->local_next_by_ip_protocol[i]];
3070           vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
3071                            node_index);
3072         }
3073     }
3074   return 0;
3075 }
3076
3077
3078
3079 /*?
3080  * Display the set of protocols handled by the local IPv6 stack.
3081  *
3082  * @cliexpar
3083  * Example of how to display local protocol table:
3084  * @cliexstart{show ip6 local}
3085  * Protocols handled by ip6_local
3086  * 17
3087  * 43
3088  * 58
3089  * 115
3090  * @cliexend
3091 ?*/
3092 /* *INDENT-OFF* */
3093 VLIB_CLI_COMMAND (show_ip6_local, static) =
3094 {
3095   .path = "show ip6 local",
3096   .function = show_ip6_local_command_fn,
3097   .short_help = "show ip6 local",
3098 };
3099 /* *INDENT-ON* */
3100
3101 int
3102 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
3103                              u32 table_index)
3104 {
3105   vnet_main_t *vnm = vnet_get_main ();
3106   vnet_interface_main_t *im = &vnm->interface_main;
3107   ip6_main_t *ipm = &ip6_main;
3108   ip_lookup_main_t *lm = &ipm->lookup_main;
3109   vnet_classify_main_t *cm = &vnet_classify_main;
3110   ip6_address_t *if_addr;
3111
3112   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3113     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3114
3115   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3116     return VNET_API_ERROR_NO_SUCH_ENTRY;
3117
3118   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3119   lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
3120
3121   if_addr = ip6_interface_first_address (ipm, sw_if_index);
3122
3123   if (NULL != if_addr)
3124     {
3125       fib_prefix_t pfx = {
3126         .fp_len = 128,
3127         .fp_proto = FIB_PROTOCOL_IP6,
3128         .fp_addr.ip6 = *if_addr,
3129       };
3130       u32 fib_index;
3131
3132       fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
3133                                                        sw_if_index);
3134
3135
3136       if (table_index != (u32) ~ 0)
3137         {
3138           dpo_id_t dpo = DPO_INVALID;
3139
3140           dpo_set (&dpo,
3141                    DPO_CLASSIFY,
3142                    DPO_PROTO_IP6,
3143                    classify_dpo_create (DPO_PROTO_IP6, table_index));
3144
3145           fib_table_entry_special_dpo_add (fib_index,
3146                                            &pfx,
3147                                            FIB_SOURCE_CLASSIFY,
3148                                            FIB_ENTRY_FLAG_NONE, &dpo);
3149           dpo_reset (&dpo);
3150         }
3151       else
3152         {
3153           fib_table_entry_special_remove (fib_index,
3154                                           &pfx, FIB_SOURCE_CLASSIFY);
3155         }
3156     }
3157
3158   return 0;
3159 }
3160
3161 static clib_error_t *
3162 set_ip6_classify_command_fn (vlib_main_t * vm,
3163                              unformat_input_t * input,
3164                              vlib_cli_command_t * cmd)
3165 {
3166   u32 table_index = ~0;
3167   int table_index_set = 0;
3168   u32 sw_if_index = ~0;
3169   int rv;
3170
3171   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3172     {
3173       if (unformat (input, "table-index %d", &table_index))
3174         table_index_set = 1;
3175       else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
3176                          vnet_get_main (), &sw_if_index))
3177         ;
3178       else
3179         break;
3180     }
3181
3182   if (table_index_set == 0)
3183     return clib_error_return (0, "classify table-index must be specified");
3184
3185   if (sw_if_index == ~0)
3186     return clib_error_return (0, "interface / subif must be specified");
3187
3188   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3189
3190   switch (rv)
3191     {
3192     case 0:
3193       break;
3194
3195     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3196       return clib_error_return (0, "No such interface");
3197
3198     case VNET_API_ERROR_NO_SUCH_ENTRY:
3199       return clib_error_return (0, "No such classifier table");
3200     }
3201   return 0;
3202 }
3203
3204 /*?
3205  * Assign a classification table to an interface. The classification
3206  * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
3207  * commands. Once the table is create, use this command to filter packets
3208  * on an interface.
3209  *
3210  * @cliexpar
3211  * Example of how to assign a classification table to an interface:
3212  * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
3213 ?*/
3214 /* *INDENT-OFF* */
3215 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
3216 {
3217   .path = "set ip6 classify",
3218   .short_help =
3219   "set ip6 classify intfc <interface> table-index <classify-idx>",
3220   .function = set_ip6_classify_command_fn,
3221 };
3222 /* *INDENT-ON* */
3223
3224 static clib_error_t *
3225 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3226 {
3227   ip6_main_t *im = &ip6_main;
3228   uword heapsize = 0;
3229   u32 tmp;
3230   u32 nbuckets = 0;
3231
3232   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3233     {
3234       if (unformat (input, "hash-buckets %d", &tmp))
3235         nbuckets = tmp;
3236       else if (unformat (input, "heap-size %U",
3237                          unformat_memory_size, &heapsize))
3238         ;
3239       else
3240         return clib_error_return (0, "unknown input '%U'",
3241                                   format_unformat_error, input);
3242     }
3243
3244   im->lookup_table_nbuckets = nbuckets;
3245   im->lookup_table_size = heapsize;
3246
3247   return 0;
3248 }
3249
3250 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3251
3252 /*
3253  * fd.io coding-style-patch-verification: ON
3254  *
3255  * Local Variables:
3256  * eval: (c-set-style "gnu")
3257  * End:
3258  */