IPv6 NS: use the mcast rewrite node to fill in the destination MAC
[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   u32 fib_index;
979
980   fib_index = vec_elt (im->fib_index_by_sw_if_index,
981                        vnet_buffer (b)->sw_if_index[VLIB_RX]);
982   fib_index =
983     (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ?
984     fib_index : vnet_buffer (b)->sw_if_index[VLIB_TX];
985
986   lbi = ip6_fib_table_fwding_lookup (im, fib_index, &i->src_address);
987   lb0 = load_balance_get (lbi);
988
989   return (fib_urpf_check_size (lb0->lb_urpf));
990 }
991
992 always_inline u8
993 ip6_next_proto_is_tcp_udp (vlib_buffer_t * p0, ip6_header_t * ip0,
994                            u32 * udp_offset0)
995 {
996   u32 proto0;
997   proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_UDP, udp_offset0);
998   if (proto0 != IP_PROTOCOL_UDP)
999     {
1000       proto0 = ip6_locate_header (p0, ip0, IP_PROTOCOL_TCP, udp_offset0);
1001       proto0 = (proto0 == IP_PROTOCOL_TCP) ? proto0 : 0;
1002     }
1003   return proto0;
1004 }
1005
1006 /* *INDENT-OFF* */
1007 VNET_FEATURE_ARC_INIT (ip6_local) =
1008 {
1009   .arc_name  = "ip6-local",
1010   .start_nodes = VNET_FEATURES ("ip6-local"),
1011 };
1012 /* *INDENT-ON* */
1013
1014 static uword
1015 ip6_local_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1016                   vlib_frame_t * frame, int head_of_feature_arc)
1017 {
1018   ip6_main_t *im = &ip6_main;
1019   ip_lookup_main_t *lm = &im->lookup_main;
1020   ip_local_next_t next_index;
1021   u32 *from, *to_next, n_left_from, n_left_to_next;
1022   vlib_node_runtime_t *error_node =
1023     vlib_node_get_runtime (vm, ip6_input_node.index);
1024   u8 arc_index = vnet_feat_arc_ip6_local.feature_arc_index;
1025
1026   from = vlib_frame_vector_args (frame);
1027   n_left_from = frame->n_vectors;
1028   next_index = node->cached_next_index;
1029
1030   if (node->flags & VLIB_NODE_FLAG_TRACE)
1031     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1032
1033   while (n_left_from > 0)
1034     {
1035       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1036
1037       while (n_left_from >= 4 && n_left_to_next >= 2)
1038         {
1039           vlib_buffer_t *p0, *p1;
1040           ip6_header_t *ip0, *ip1;
1041           udp_header_t *udp0, *udp1;
1042           u32 pi0, ip_len0, udp_len0, flags0, next0;
1043           u32 pi1, ip_len1, udp_len1, flags1, next1;
1044           i32 len_diff0, len_diff1;
1045           u8 error0, type0, good_l4_csum0, is_tcp_udp0;
1046           u8 error1, type1, good_l4_csum1, is_tcp_udp1;
1047           u32 udp_offset0, udp_offset1;
1048
1049           pi0 = to_next[0] = from[0];
1050           pi1 = to_next[1] = from[1];
1051           from += 2;
1052           n_left_from -= 2;
1053           to_next += 2;
1054           n_left_to_next -= 2;
1055
1056           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1057
1058           p0 = vlib_get_buffer (vm, pi0);
1059           p1 = vlib_get_buffer (vm, pi1);
1060
1061           ip0 = vlib_buffer_get_current (p0);
1062           ip1 = vlib_buffer_get_current (p1);
1063
1064           if (head_of_feature_arc == 0)
1065             goto skip_checks;
1066
1067           vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1068           vnet_buffer (p1)->l3_hdr_offset = p1->current_data;
1069
1070           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1071           type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1072
1073           flags0 = p0->flags;
1074           flags1 = p1->flags;
1075
1076           is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1077           is_tcp_udp1 = ip6_next_proto_is_tcp_udp (p1, ip1, &udp_offset1);
1078
1079           good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1080                            || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1081                                || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1082             != 0;
1083           good_l4_csum1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1084                            || (flags1 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1085                                || flags1 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1086             != 0;
1087           len_diff0 = 0;
1088           len_diff1 = 0;
1089
1090           if (PREDICT_TRUE (is_tcp_udp0))
1091             {
1092               udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1093               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1094               good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1095                 && udp0->checksum == 0;
1096               /* Verify UDP length. */
1097               if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1098                 {
1099                   ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1100                   udp_len0 = clib_net_to_host_u16 (udp0->length);
1101                   len_diff0 = ip_len0 - udp_len0;
1102                 }
1103             }
1104           if (PREDICT_TRUE (is_tcp_udp1))
1105             {
1106               udp1 = (udp_header_t *) ((u8 *) ip1 + udp_offset1);
1107               /* Don't verify UDP checksum for packets with explicit zero checksum. */
1108               good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP
1109                 && udp1->checksum == 0;
1110               /* Verify UDP length. */
1111               if (is_tcp_udp1 == IP_PROTOCOL_UDP)
1112                 {
1113                   ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1114                   udp_len1 = clib_net_to_host_u16 (udp1->length);
1115                   len_diff1 = ip_len1 - udp_len1;
1116                 }
1117             }
1118
1119           good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1120           good_l4_csum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1121
1122           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1123           len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1124
1125           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1126                              && !good_l4_csum0
1127                              && !(flags0 &
1128                                   VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1129             {
1130               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1131               good_l4_csum0 =
1132                 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1133             }
1134           if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1135                              && !good_l4_csum1
1136                              && !(flags1 &
1137                                   VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1138             {
1139               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1140               good_l4_csum1 =
1141                 (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1142             }
1143
1144           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1145           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1146           error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1147
1148           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1149                   IP6_ERROR_UDP_CHECKSUM);
1150           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1151                   IP6_ERROR_ICMP_CHECKSUM);
1152           error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1153           error1 = (!good_l4_csum1 ? IP6_ERROR_UDP_CHECKSUM + type1 : error1);
1154
1155           /* Drop packets from unroutable hosts. */
1156           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1157           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1158               type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1159               !ip6_address_is_link_local_unicast (&ip0->src_address))
1160             {
1161               error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1162                         ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1163             }
1164           if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1165               type1 != IP_BUILTIN_PROTOCOL_ICMP &&
1166               !ip6_address_is_link_local_unicast (&ip1->src_address))
1167             {
1168               error1 = (!ip6_urpf_loose_check (im, p1, ip1)
1169                         ? IP6_ERROR_SRC_LOOKUP_MISS : error1);
1170             }
1171
1172           /* TODO maybe move to lookup? */
1173           vnet_buffer (p0)->ip.fib_index =
1174             vec_elt (im->fib_index_by_sw_if_index,
1175                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1176           vnet_buffer (p0)->ip.fib_index =
1177             (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1178              (u32) ~ 0) ? vnet_buffer (p0)->ip.
1179             fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1180
1181           vnet_buffer (p1)->ip.fib_index =
1182             vec_elt (im->fib_index_by_sw_if_index,
1183                      vnet_buffer (p1)->sw_if_index[VLIB_RX]);
1184           vnet_buffer (p1)->ip.fib_index =
1185             (vnet_buffer (p1)->sw_if_index[VLIB_TX] ==
1186              (u32) ~ 0) ? vnet_buffer (p1)->ip.
1187             fib_index : vnet_buffer (p1)->sw_if_index[VLIB_TX];
1188
1189
1190         skip_checks:
1191
1192           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1193           next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1194
1195           next0 =
1196             error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1197           next1 =
1198             error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1199
1200           p0->error = error_node->errors[error0];
1201           p1->error = error_node->errors[error1];
1202
1203           if (head_of_feature_arc)
1204             {
1205               if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1206                 vnet_feature_arc_start (arc_index,
1207                                         vnet_buffer (p0)->sw_if_index
1208                                         [VLIB_RX], &next0, p0);
1209               if (PREDICT_TRUE (error1 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1210                 vnet_feature_arc_start (arc_index,
1211                                         vnet_buffer (p1)->sw_if_index
1212                                         [VLIB_RX], &next1, p1);
1213             }
1214
1215           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1216                                            to_next, n_left_to_next,
1217                                            pi0, pi1, next0, next1);
1218         }
1219
1220       while (n_left_from > 0 && n_left_to_next > 0)
1221         {
1222           vlib_buffer_t *p0;
1223           ip6_header_t *ip0;
1224           udp_header_t *udp0;
1225           u32 pi0, ip_len0, udp_len0, flags0, next0;
1226           i32 len_diff0;
1227           u8 error0, type0, good_l4_csum0;
1228           u32 udp_offset0;
1229           u8 is_tcp_udp0;
1230
1231           pi0 = to_next[0] = from[0];
1232           from += 1;
1233           n_left_from -= 1;
1234           to_next += 1;
1235           n_left_to_next -= 1;
1236
1237           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1238
1239           p0 = vlib_get_buffer (vm, pi0);
1240           ip0 = vlib_buffer_get_current (p0);
1241
1242           if (head_of_feature_arc == 0)
1243             goto skip_check;
1244
1245           vnet_buffer (p0)->l3_hdr_offset = p0->current_data;
1246
1247           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1248           flags0 = p0->flags;
1249           is_tcp_udp0 = ip6_next_proto_is_tcp_udp (p0, ip0, &udp_offset0);
1250           good_l4_csum0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT
1251                            || (flags0 & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM
1252                                || flags0 & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
1253             != 0;
1254
1255           len_diff0 = 0;
1256           if (PREDICT_TRUE (is_tcp_udp0))
1257             {
1258               udp0 = (udp_header_t *) ((u8 *) ip0 + udp_offset0);
1259               /* Don't verify UDP checksum for packets with explicit zero
1260                * checksum. */
1261               good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP
1262                 && udp0->checksum == 0;
1263               /* Verify UDP length. */
1264               if (is_tcp_udp0 == IP_PROTOCOL_UDP)
1265                 {
1266                   ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1267                   udp_len0 = clib_net_to_host_u16 (udp0->length);
1268                   len_diff0 = ip_len0 - udp_len0;
1269                 }
1270             }
1271
1272           good_l4_csum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1273           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1274
1275           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1276                              && !good_l4_csum0
1277                              && !(flags0 &
1278                                   VNET_BUFFER_F_L4_CHECKSUM_COMPUTED)))
1279             {
1280               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1281               good_l4_csum0 =
1282                 (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
1283             }
1284
1285           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1286           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1287
1288           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP ==
1289                   IP6_ERROR_UDP_CHECKSUM);
1290           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP ==
1291                   IP6_ERROR_ICMP_CHECKSUM);
1292           error0 = (!good_l4_csum0 ? IP6_ERROR_UDP_CHECKSUM + type0 : error0);
1293
1294           /* If this is a neighbor solicitation (ICMP), skip src RPF check */
1295           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL &&
1296               type0 != IP_BUILTIN_PROTOCOL_ICMP &&
1297               !ip6_address_is_link_local_unicast (&ip0->src_address))
1298             {
1299               error0 = (!ip6_urpf_loose_check (im, p0, ip0)
1300                         ? IP6_ERROR_SRC_LOOKUP_MISS : error0);
1301             }
1302
1303           vnet_buffer (p0)->ip.fib_index =
1304             vec_elt (im->fib_index_by_sw_if_index,
1305                      vnet_buffer (p0)->sw_if_index[VLIB_RX]);
1306           vnet_buffer (p0)->ip.fib_index =
1307             (vnet_buffer (p0)->sw_if_index[VLIB_TX] ==
1308              (u32) ~ 0) ? vnet_buffer (p0)->ip.
1309             fib_index : vnet_buffer (p0)->sw_if_index[VLIB_TX];
1310
1311         skip_check:
1312
1313           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1314           next0 =
1315             error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1316
1317           p0->error = error_node->errors[error0];
1318
1319           if (head_of_feature_arc)
1320             {
1321               if (PREDICT_TRUE (error0 == (u8) IP6_ERROR_UNKNOWN_PROTOCOL))
1322                 vnet_feature_arc_start (arc_index,
1323                                         vnet_buffer (p0)->sw_if_index
1324                                         [VLIB_RX], &next0, p0);
1325             }
1326
1327           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1328                                            to_next, n_left_to_next,
1329                                            pi0, next0);
1330         }
1331
1332       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1333     }
1334
1335   return frame->n_vectors;
1336 }
1337
1338 static uword
1339 ip6_local (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
1340 {
1341   return ip6_local_inline (vm, node, frame, 1 /* head of feature arc */ );
1342 }
1343
1344 /* *INDENT-OFF* */
1345 VLIB_REGISTER_NODE (ip6_local_node, static) =
1346 {
1347   .function = ip6_local,
1348   .name = "ip6-local",
1349   .vector_size = sizeof (u32),
1350   .format_trace = format_ip6_forward_next_trace,
1351   .n_next_nodes = IP_LOCAL_N_NEXT,
1352   .next_nodes =
1353   {
1354     [IP_LOCAL_NEXT_DROP] = "ip6-drop",
1355     [IP_LOCAL_NEXT_PUNT] = "ip6-punt",
1356     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1357     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1358   },
1359 };
1360 /* *INDENT-ON* */
1361
1362 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local);
1363
1364
1365 static uword
1366 ip6_local_end_of_arc (vlib_main_t * vm,
1367                       vlib_node_runtime_t * node, vlib_frame_t * frame)
1368 {
1369   return ip6_local_inline (vm, node, frame, 0 /* head of feature arc */ );
1370 }
1371
1372 /* *INDENT-OFF* */
1373 VLIB_REGISTER_NODE (ip6_local_end_of_arc_node,static) = {
1374   .function = ip6_local_end_of_arc,
1375   .name = "ip6-local-end-of-arc",
1376   .vector_size = sizeof (u32),
1377
1378   .format_trace = format_ip6_forward_next_trace,
1379   .sibling_of = "ip6-local",
1380 };
1381
1382 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_end_of_arc_node, ip6_local_end_of_arc)
1383
1384 VNET_FEATURE_INIT (ip6_local_end_of_arc, static) = {
1385   .arc_name = "ip6-local",
1386   .node_name = "ip6-local-end-of-arc",
1387   .runs_before = 0, /* not before any other features */
1388 };
1389 /* *INDENT-ON* */
1390
1391 void
1392 ip6_register_protocol (u32 protocol, u32 node_index)
1393 {
1394   vlib_main_t *vm = vlib_get_main ();
1395   ip6_main_t *im = &ip6_main;
1396   ip_lookup_main_t *lm = &im->lookup_main;
1397
1398   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1399   lm->local_next_by_ip_protocol[protocol] =
1400     vlib_node_add_next (vm, ip6_local_node.index, node_index);
1401 }
1402
1403 clib_error_t *
1404 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1405 {
1406   vnet_main_t *vnm = vnet_get_main ();
1407   ip6_main_t *im = &ip6_main;
1408   icmp6_neighbor_solicitation_header_t *h;
1409   ip6_address_t *src;
1410   ip_interface_address_t *ia;
1411   ip_adjacency_t *adj;
1412   vnet_hw_interface_t *hi;
1413   vnet_sw_interface_t *si;
1414   vlib_buffer_t *b;
1415   adj_index_t ai;
1416   u32 bi = 0;
1417   int bogus_length;
1418
1419   si = vnet_get_sw_interface (vnm, sw_if_index);
1420
1421   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1422     {
1423       return clib_error_return (0, "%U: interface %U down",
1424                                 format_ip6_address, dst,
1425                                 format_vnet_sw_if_index_name, vnm,
1426                                 sw_if_index);
1427     }
1428
1429   src =
1430     ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1431   if (!src)
1432     {
1433       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
1434       return clib_error_return
1435         (0, "no matching interface address for destination %U (interface %U)",
1436          format_ip6_address, dst,
1437          format_vnet_sw_if_index_name, vnm, sw_if_index);
1438     }
1439
1440   h =
1441     vlib_packet_template_get_packet (vm,
1442                                      &im->discover_neighbor_packet_template,
1443                                      &bi);
1444
1445   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1446
1447   /* Destination address is a solicited node multicast address.  We need to fill in
1448      the low 24 bits with low 24 bits of target's address. */
1449   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
1450   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
1451   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
1452
1453   h->ip.src_address = src[0];
1454   h->neighbor.target_address = dst[0];
1455
1456   if (PREDICT_FALSE (!hi->hw_address))
1457     {
1458       return clib_error_return (0, "%U: interface %U do not support ip probe",
1459                                 format_ip6_address, dst,
1460                                 format_vnet_sw_if_index_name, vnm,
1461                                 sw_if_index);
1462     }
1463
1464   clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address,
1465                vec_len (hi->hw_address));
1466
1467   h->neighbor.icmp.checksum =
1468     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
1469   ASSERT (bogus_length == 0);
1470
1471   b = vlib_get_buffer (vm, bi);
1472   vnet_buffer (b)->sw_if_index[VLIB_RX] =
1473     vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
1474
1475   /* Add encapsulation string for software interface (e.g. ethernet header). */
1476   ip46_address_t nh = {
1477     .ip6 = *dst,
1478   };
1479
1480   ai = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6,
1481                             VNET_LINK_IP6, &nh, sw_if_index);
1482   adj = adj_get (ai);
1483
1484   /* Peer has been previously resolved, retrieve glean adj instead */
1485   if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
1486     {
1487       adj_unlock (ai);
1488       ai = adj_glean_add_or_lock (FIB_PROTOCOL_IP6,
1489                                   VNET_LINK_IP6, sw_if_index, &nh);
1490       adj = adj_get (ai);
1491     }
1492
1493   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
1494   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
1495
1496   {
1497     vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
1498     u32 *to_next = vlib_frame_vector_args (f);
1499     to_next[0] = bi;
1500     f->n_vectors = 1;
1501     vlib_put_frame_to_node (vm, hi->output_node_index, f);
1502   }
1503
1504   adj_unlock (ai);
1505   return /* no error */ 0;
1506 }
1507
1508 typedef enum
1509 {
1510   IP6_REWRITE_NEXT_DROP,
1511   IP6_REWRITE_NEXT_ICMP_ERROR,
1512 } ip6_rewrite_next_t;
1513
1514 /**
1515  * This bits of an IPv6 address to mask to construct a multicast
1516  * MAC address
1517  */
1518 #define IP6_MCAST_ADDR_MASK 0xffffffff
1519
1520 always_inline uword
1521 ip6_rewrite_inline (vlib_main_t * vm,
1522                     vlib_node_runtime_t * node,
1523                     vlib_frame_t * frame,
1524                     int do_counters, int is_midchain, int is_mcast)
1525 {
1526   ip_lookup_main_t *lm = &ip6_main.lookup_main;
1527   u32 *from = vlib_frame_vector_args (frame);
1528   u32 n_left_from, n_left_to_next, *to_next, next_index;
1529   vlib_node_runtime_t *error_node =
1530     vlib_node_get_runtime (vm, ip6_input_node.index);
1531
1532   n_left_from = frame->n_vectors;
1533   next_index = node->cached_next_index;
1534   u32 thread_index = vlib_get_thread_index ();
1535
1536   while (n_left_from > 0)
1537     {
1538       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1539
1540       while (n_left_from >= 4 && n_left_to_next >= 2)
1541         {
1542           ip_adjacency_t *adj0, *adj1;
1543           vlib_buffer_t *p0, *p1;
1544           ip6_header_t *ip0, *ip1;
1545           u32 pi0, rw_len0, next0, error0, adj_index0;
1546           u32 pi1, rw_len1, next1, error1, adj_index1;
1547           u32 tx_sw_if_index0, tx_sw_if_index1;
1548
1549           /* Prefetch next iteration. */
1550           {
1551             vlib_buffer_t *p2, *p3;
1552
1553             p2 = vlib_get_buffer (vm, from[2]);
1554             p3 = vlib_get_buffer (vm, from[3]);
1555
1556             vlib_prefetch_buffer_header (p2, LOAD);
1557             vlib_prefetch_buffer_header (p3, LOAD);
1558
1559             CLIB_PREFETCH (p2->pre_data, 32, STORE);
1560             CLIB_PREFETCH (p3->pre_data, 32, STORE);
1561
1562             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
1563             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
1564           }
1565
1566           pi0 = to_next[0] = from[0];
1567           pi1 = to_next[1] = from[1];
1568
1569           from += 2;
1570           n_left_from -= 2;
1571           to_next += 2;
1572           n_left_to_next -= 2;
1573
1574           p0 = vlib_get_buffer (vm, pi0);
1575           p1 = vlib_get_buffer (vm, pi1);
1576
1577           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1578           adj_index1 = vnet_buffer (p1)->ip.adj_index[VLIB_TX];
1579
1580           ip0 = vlib_buffer_get_current (p0);
1581           ip1 = vlib_buffer_get_current (p1);
1582
1583           error0 = error1 = IP6_ERROR_NONE;
1584           next0 = next1 = IP6_REWRITE_NEXT_DROP;
1585
1586           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1587             {
1588               i32 hop_limit0 = ip0->hop_limit;
1589
1590               /* Input node should have reject packets with hop limit 0. */
1591               ASSERT (ip0->hop_limit > 0);
1592
1593               hop_limit0 -= 1;
1594
1595               ip0->hop_limit = hop_limit0;
1596
1597               /*
1598                * If the hop count drops below 1 when forwarding, generate
1599                * an ICMP response.
1600                */
1601               if (PREDICT_FALSE (hop_limit0 <= 0))
1602                 {
1603                   error0 = IP6_ERROR_TIME_EXPIRED;
1604                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1605                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1606                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1607                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1608                                                0);
1609                 }
1610             }
1611           else
1612             {
1613               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1614             }
1615           if (PREDICT_TRUE (!(p1->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1616             {
1617               i32 hop_limit1 = ip1->hop_limit;
1618
1619               /* Input node should have reject packets with hop limit 0. */
1620               ASSERT (ip1->hop_limit > 0);
1621
1622               hop_limit1 -= 1;
1623
1624               ip1->hop_limit = hop_limit1;
1625
1626               /*
1627                * If the hop count drops below 1 when forwarding, generate
1628                * an ICMP response.
1629                */
1630               if (PREDICT_FALSE (hop_limit1 <= 0))
1631                 {
1632                   error1 = IP6_ERROR_TIME_EXPIRED;
1633                   next1 = IP6_REWRITE_NEXT_ICMP_ERROR;
1634                   vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1635                   icmp6_error_set_vnet_buffer (p1, ICMP6_time_exceeded,
1636                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1637                                                0);
1638                 }
1639             }
1640           else
1641             {
1642               p1->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1643             }
1644           adj0 = adj_get (adj_index0);
1645           adj1 = adj_get (adj_index1);
1646
1647           rw_len0 = adj0[0].rewrite_header.data_bytes;
1648           rw_len1 = adj1[0].rewrite_header.data_bytes;
1649           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1650           vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
1651
1652           if (do_counters)
1653             {
1654               vlib_increment_combined_counter
1655                 (&adjacency_counters,
1656                  thread_index, adj_index0, 1,
1657                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1658               vlib_increment_combined_counter
1659                 (&adjacency_counters,
1660                  thread_index, adj_index1, 1,
1661                  vlib_buffer_length_in_chain (vm, p1) + rw_len1);
1662             }
1663
1664           /* Check MTU of outgoing interface. */
1665           error0 =
1666             (vlib_buffer_length_in_chain (vm, p0) >
1667              adj0[0].
1668              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1669              error0);
1670           error1 =
1671             (vlib_buffer_length_in_chain (vm, p1) >
1672              adj1[0].
1673              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1674              error1);
1675
1676           /* Don't adjust the buffer for hop count issue; icmp-error node
1677            * wants to see the IP headerr */
1678           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1679             {
1680               p0->current_data -= rw_len0;
1681               p0->current_length += rw_len0;
1682
1683               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1684               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1685               next0 = adj0[0].rewrite_header.next_index;
1686
1687               if (PREDICT_FALSE
1688                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1689                 vnet_feature_arc_start (lm->output_feature_arc_index,
1690                                         tx_sw_if_index0, &next0, p0);
1691             }
1692           if (PREDICT_TRUE (error1 == IP6_ERROR_NONE))
1693             {
1694               p1->current_data -= rw_len1;
1695               p1->current_length += rw_len1;
1696
1697               tx_sw_if_index1 = adj1[0].rewrite_header.sw_if_index;
1698               vnet_buffer (p1)->sw_if_index[VLIB_TX] = tx_sw_if_index1;
1699               next1 = adj1[0].rewrite_header.next_index;
1700
1701               if (PREDICT_FALSE
1702                   (adj1[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1703                 vnet_feature_arc_start (lm->output_feature_arc_index,
1704                                         tx_sw_if_index1, &next1, p1);
1705             }
1706
1707           /* Guess we are only writing on simple Ethernet header. */
1708           vnet_rewrite_two_headers (adj0[0], adj1[0],
1709                                     ip0, ip1, sizeof (ethernet_header_t));
1710
1711           if (is_midchain)
1712             {
1713               adj0->sub_type.midchain.fixup_func
1714                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1715               adj1->sub_type.midchain.fixup_func
1716                 (vm, adj1, p1, adj1->sub_type.midchain.fixup_data);
1717             }
1718           if (is_mcast)
1719             {
1720               /*
1721                * copy bytes from the IP address into the MAC rewrite
1722                */
1723               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1724                                           adj0->
1725                                           rewrite_header.dst_mcast_offset,
1726                                           &ip0->dst_address.as_u32[3],
1727                                           (u8 *) ip0);
1728               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1729                                           adj1->
1730                                           rewrite_header.dst_mcast_offset,
1731                                           &ip1->dst_address.as_u32[3],
1732                                           (u8 *) ip1);
1733             }
1734
1735           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1736                                            to_next, n_left_to_next,
1737                                            pi0, pi1, next0, next1);
1738         }
1739
1740       while (n_left_from > 0 && n_left_to_next > 0)
1741         {
1742           ip_adjacency_t *adj0;
1743           vlib_buffer_t *p0;
1744           ip6_header_t *ip0;
1745           u32 pi0, rw_len0;
1746           u32 adj_index0, next0, error0;
1747           u32 tx_sw_if_index0;
1748
1749           pi0 = to_next[0] = from[0];
1750
1751           p0 = vlib_get_buffer (vm, pi0);
1752
1753           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1754
1755           adj0 = adj_get (adj_index0);
1756
1757           ip0 = vlib_buffer_get_current (p0);
1758
1759           error0 = IP6_ERROR_NONE;
1760           next0 = IP6_REWRITE_NEXT_DROP;
1761
1762           /* Check hop limit */
1763           if (PREDICT_TRUE (!(p0->flags & VNET_BUFFER_F_LOCALLY_ORIGINATED)))
1764             {
1765               i32 hop_limit0 = ip0->hop_limit;
1766
1767               ASSERT (ip0->hop_limit > 0);
1768
1769               hop_limit0 -= 1;
1770
1771               ip0->hop_limit = hop_limit0;
1772
1773               if (PREDICT_FALSE (hop_limit0 <= 0))
1774                 {
1775                   /*
1776                    * If the hop count drops below 1 when forwarding, generate
1777                    * an ICMP response.
1778                    */
1779                   error0 = IP6_ERROR_TIME_EXPIRED;
1780                   next0 = IP6_REWRITE_NEXT_ICMP_ERROR;
1781                   vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1782                   icmp6_error_set_vnet_buffer (p0, ICMP6_time_exceeded,
1783                                                ICMP6_time_exceeded_ttl_exceeded_in_transit,
1784                                                0);
1785                 }
1786             }
1787           else
1788             {
1789               p0->flags &= ~VNET_BUFFER_F_LOCALLY_ORIGINATED;
1790             }
1791
1792           /* Guess we are only writing on simple Ethernet header. */
1793           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
1794
1795           /* Update packet buffer attributes/set output interface. */
1796           rw_len0 = adj0[0].rewrite_header.data_bytes;
1797           vnet_buffer (p0)->ip.save_rewrite_length = rw_len0;
1798
1799           if (do_counters)
1800             {
1801               vlib_increment_combined_counter
1802                 (&adjacency_counters,
1803                  thread_index, adj_index0, 1,
1804                  vlib_buffer_length_in_chain (vm, p0) + rw_len0);
1805             }
1806
1807           /* Check MTU of outgoing interface. */
1808           error0 =
1809             (vlib_buffer_length_in_chain (vm, p0) >
1810              adj0[0].
1811              rewrite_header.max_l3_packet_bytes ? IP6_ERROR_MTU_EXCEEDED :
1812              error0);
1813
1814           /* Don't adjust the buffer for hop count issue; icmp-error node
1815            * wants to see the IP headerr */
1816           if (PREDICT_TRUE (error0 == IP6_ERROR_NONE))
1817             {
1818               p0->current_data -= rw_len0;
1819               p0->current_length += rw_len0;
1820
1821               tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
1822
1823               vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
1824               next0 = adj0[0].rewrite_header.next_index;
1825
1826               if (PREDICT_FALSE
1827                   (adj0[0].rewrite_header.flags & VNET_REWRITE_HAS_FEATURES))
1828                 vnet_feature_arc_start (lm->output_feature_arc_index,
1829                                         tx_sw_if_index0, &next0, p0);
1830             }
1831
1832           if (is_midchain)
1833             {
1834               adj0->sub_type.midchain.fixup_func
1835                 (vm, adj0, p0, adj0->sub_type.midchain.fixup_data);
1836             }
1837           if (is_mcast)
1838             {
1839               vnet_ip_mcast_fixup_header (IP6_MCAST_ADDR_MASK,
1840                                           adj0->
1841                                           rewrite_header.dst_mcast_offset,
1842                                           &ip0->dst_address.as_u32[3],
1843                                           (u8 *) ip0);
1844             }
1845
1846           p0->error = error_node->errors[error0];
1847
1848           from += 1;
1849           n_left_from -= 1;
1850           to_next += 1;
1851           n_left_to_next -= 1;
1852
1853           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1854                                            to_next, n_left_to_next,
1855                                            pi0, next0);
1856         }
1857
1858       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1859     }
1860
1861   /* Need to do trace after rewrites to pick up new packet data. */
1862   if (node->flags & VLIB_NODE_FLAG_TRACE)
1863     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1864
1865   return frame->n_vectors;
1866 }
1867
1868 static uword
1869 ip6_rewrite (vlib_main_t * vm,
1870              vlib_node_runtime_t * node, vlib_frame_t * frame)
1871 {
1872   if (adj_are_counters_enabled ())
1873     return ip6_rewrite_inline (vm, node, frame, 1, 0, 0);
1874   else
1875     return ip6_rewrite_inline (vm, node, frame, 0, 0, 0);
1876 }
1877
1878 static uword
1879 ip6_rewrite_mcast (vlib_main_t * vm,
1880                    vlib_node_runtime_t * node, vlib_frame_t * frame)
1881 {
1882   if (adj_are_counters_enabled ())
1883     return ip6_rewrite_inline (vm, node, frame, 1, 0, 1);
1884   else
1885     return ip6_rewrite_inline (vm, node, frame, 0, 0, 1);
1886 }
1887
1888 static uword
1889 ip6_midchain (vlib_main_t * vm,
1890               vlib_node_runtime_t * node, vlib_frame_t * frame)
1891 {
1892   if (adj_are_counters_enabled ())
1893     return ip6_rewrite_inline (vm, node, frame, 1, 1, 0);
1894   else
1895     return ip6_rewrite_inline (vm, node, frame, 0, 1, 0);
1896 }
1897
1898 static uword
1899 ip6_mcast_midchain (vlib_main_t * vm,
1900                     vlib_node_runtime_t * node, vlib_frame_t * frame)
1901 {
1902   if (adj_are_counters_enabled ())
1903     return ip6_rewrite_inline (vm, node, frame, 1, 1, 1);
1904   else
1905     return ip6_rewrite_inline (vm, node, frame, 0, 1, 1);
1906 }
1907
1908 /* *INDENT-OFF* */
1909 VLIB_REGISTER_NODE (ip6_midchain_node) =
1910 {
1911   .function = ip6_midchain,
1912   .name = "ip6-midchain",
1913   .vector_size = sizeof (u32),
1914   .format_trace = format_ip6_forward_next_trace,
1915   .sibling_of = "ip6-rewrite",
1916   };
1917 /* *INDENT-ON* */
1918
1919 VLIB_NODE_FUNCTION_MULTIARCH (ip6_midchain_node, ip6_midchain);
1920
1921 /* *INDENT-OFF* */
1922 VLIB_REGISTER_NODE (ip6_rewrite_node) =
1923 {
1924   .function = ip6_rewrite,
1925   .name = "ip6-rewrite",
1926   .vector_size = sizeof (u32),
1927   .format_trace = format_ip6_rewrite_trace,
1928   .n_next_nodes = 2,
1929   .next_nodes =
1930   {
1931     [IP6_REWRITE_NEXT_DROP] = "ip6-drop",
1932     [IP6_REWRITE_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1933   },
1934 };
1935 /* *INDENT-ON* */
1936
1937 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite);
1938
1939 /* *INDENT-OFF* */
1940 VLIB_REGISTER_NODE (ip6_rewrite_mcast_node) =
1941 {
1942   .function = ip6_rewrite_mcast,
1943   .name = "ip6-rewrite-mcast",
1944   .vector_size = sizeof (u32),
1945   .format_trace = format_ip6_rewrite_trace,
1946   .sibling_of = "ip6-rewrite",
1947 };
1948 /* *INDENT-ON* */
1949
1950 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_mcast_node, ip6_rewrite_mcast);
1951
1952 /* *INDENT-OFF* */
1953 VLIB_REGISTER_NODE (ip6_mcast_midchain_node, static) =
1954 {
1955   .function = ip6_mcast_midchain,
1956   .name = "ip6-mcast-midchain",
1957   .vector_size = sizeof (u32),
1958   .format_trace = format_ip6_rewrite_trace,
1959   .sibling_of = "ip6-rewrite",
1960 };
1961 /* *INDENT-ON* */
1962
1963 VLIB_NODE_FUNCTION_MULTIARCH (ip6_mcast_midchain_node, ip6_mcast_midchain);
1964
1965 /*
1966  * Hop-by-Hop handling
1967  */
1968 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
1969
1970 #define foreach_ip6_hop_by_hop_error \
1971 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
1972 _(FORMAT, "incorrectly formatted hop-by-hop options") \
1973 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
1974
1975 /* *INDENT-OFF* */
1976 typedef enum
1977 {
1978 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
1979   foreach_ip6_hop_by_hop_error
1980 #undef _
1981   IP6_HOP_BY_HOP_N_ERROR,
1982 } ip6_hop_by_hop_error_t;
1983 /* *INDENT-ON* */
1984
1985 /*
1986  * Primary h-b-h handler trace support
1987  * We work pretty hard on the problem for obvious reasons
1988  */
1989 typedef struct
1990 {
1991   u32 next_index;
1992   u32 trace_len;
1993   u8 option_data[256];
1994 } ip6_hop_by_hop_trace_t;
1995
1996 vlib_node_registration_t ip6_hop_by_hop_node;
1997
1998 static char *ip6_hop_by_hop_error_strings[] = {
1999 #define _(sym,string) string,
2000   foreach_ip6_hop_by_hop_error
2001 #undef _
2002 };
2003
2004 u8 *
2005 format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args)
2006 {
2007   ip6_hop_by_hop_header_t *hbh0 = va_arg (*args, ip6_hop_by_hop_header_t *);
2008   int total_len = va_arg (*args, int);
2009   ip6_hop_by_hop_option_t *opt0, *limit0;
2010   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2011   u8 type0;
2012
2013   s = format (s, "IP6_HOP_BY_HOP: next protocol %d len %d total %d",
2014               hbh0->protocol, (hbh0->length + 1) << 3, total_len);
2015
2016   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2017   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 + total_len);
2018
2019   while (opt0 < limit0)
2020     {
2021       type0 = opt0->type;
2022       switch (type0)
2023         {
2024         case 0:         /* Pad, just stop */
2025           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0 + 1);
2026           break;
2027
2028         default:
2029           if (hm->trace[type0])
2030             {
2031               s = (*hm->trace[type0]) (s, opt0);
2032             }
2033           else
2034             {
2035               s =
2036                 format (s, "\n    unrecognized option %d length %d", type0,
2037                         opt0->length);
2038             }
2039           opt0 =
2040             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2041                                          sizeof (ip6_hop_by_hop_option_t));
2042           break;
2043         }
2044     }
2045   return s;
2046 }
2047
2048 static u8 *
2049 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2050 {
2051   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2052   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2053   ip6_hop_by_hop_trace_t *t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2054   ip6_hop_by_hop_header_t *hbh0;
2055   ip6_hop_by_hop_option_t *opt0, *limit0;
2056   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2057
2058   u8 type0;
2059
2060   hbh0 = (ip6_hop_by_hop_header_t *) t->option_data;
2061
2062   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2063               t->next_index, (hbh0->length + 1) << 3, t->trace_len);
2064
2065   opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2066   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *) hbh0) + t->trace_len;
2067
2068   while (opt0 < limit0)
2069     {
2070       type0 = opt0->type;
2071       switch (type0)
2072         {
2073         case 0:         /* Pad, just stop */
2074           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2075           break;
2076
2077         default:
2078           if (hm->trace[type0])
2079             {
2080               s = (*hm->trace[type0]) (s, opt0);
2081             }
2082           else
2083             {
2084               s =
2085                 format (s, "\n    unrecognized option %d length %d", type0,
2086                         opt0->length);
2087             }
2088           opt0 =
2089             (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2090                                          sizeof (ip6_hop_by_hop_option_t));
2091           break;
2092         }
2093     }
2094   return s;
2095 }
2096
2097 always_inline u8
2098 ip6_scan_hbh_options (vlib_buffer_t * b0,
2099                       ip6_header_t * ip0,
2100                       ip6_hop_by_hop_header_t * hbh0,
2101                       ip6_hop_by_hop_option_t * opt0,
2102                       ip6_hop_by_hop_option_t * limit0, u32 * next0)
2103 {
2104   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2105   u8 type0;
2106   u8 error0 = 0;
2107
2108   while (opt0 < limit0)
2109     {
2110       type0 = opt0->type;
2111       switch (type0)
2112         {
2113         case 0:         /* Pad1 */
2114           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *) opt0) + 1;
2115           continue;
2116         case 1:         /* PadN */
2117           break;
2118         default:
2119           if (hm->options[type0])
2120             {
2121               if ((*hm->options[type0]) (b0, ip0, opt0) < 0)
2122                 {
2123                   error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2124                   return (error0);
2125                 }
2126             }
2127           else
2128             {
2129               /* Unrecognized mandatory option, check the two high order bits */
2130               switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS)
2131                 {
2132                 case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2133                   break;
2134                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2135                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2136                   *next0 = IP_LOOKUP_NEXT_DROP;
2137                   break;
2138                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2139                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2140                   *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2141                   icmp6_error_set_vnet_buffer (b0, ICMP6_parameter_problem,
2142                                                ICMP6_parameter_problem_unrecognized_option,
2143                                                (u8 *) opt0 - (u8 *) ip0);
2144                   break;
2145                 case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2146                   error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2147                   if (!ip6_address_is_multicast (&ip0->dst_address))
2148                     {
2149                       *next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2150                       icmp6_error_set_vnet_buffer (b0,
2151                                                    ICMP6_parameter_problem,
2152                                                    ICMP6_parameter_problem_unrecognized_option,
2153                                                    (u8 *) opt0 - (u8 *) ip0);
2154                     }
2155                   else
2156                     {
2157                       *next0 = IP_LOOKUP_NEXT_DROP;
2158                     }
2159                   break;
2160                 }
2161               return (error0);
2162             }
2163         }
2164       opt0 =
2165         (ip6_hop_by_hop_option_t *) (((u8 *) opt0) + opt0->length +
2166                                      sizeof (ip6_hop_by_hop_option_t));
2167     }
2168   return (error0);
2169 }
2170
2171 /*
2172  * Process the Hop-by-Hop Options header
2173  */
2174 static uword
2175 ip6_hop_by_hop (vlib_main_t * vm,
2176                 vlib_node_runtime_t * node, vlib_frame_t * frame)
2177 {
2178   vlib_node_runtime_t *error_node =
2179     vlib_node_get_runtime (vm, ip6_hop_by_hop_node.index);
2180   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2181   u32 n_left_from, *from, *to_next;
2182   ip_lookup_next_t next_index;
2183
2184   from = vlib_frame_vector_args (frame);
2185   n_left_from = frame->n_vectors;
2186   next_index = node->cached_next_index;
2187
2188   while (n_left_from > 0)
2189     {
2190       u32 n_left_to_next;
2191
2192       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2193
2194       while (n_left_from >= 4 && n_left_to_next >= 2)
2195         {
2196           u32 bi0, bi1;
2197           vlib_buffer_t *b0, *b1;
2198           u32 next0, next1;
2199           ip6_header_t *ip0, *ip1;
2200           ip6_hop_by_hop_header_t *hbh0, *hbh1;
2201           ip6_hop_by_hop_option_t *opt0, *limit0, *opt1, *limit1;
2202           u8 error0 = 0, error1 = 0;
2203
2204           /* Prefetch next iteration. */
2205           {
2206             vlib_buffer_t *p2, *p3;
2207
2208             p2 = vlib_get_buffer (vm, from[2]);
2209             p3 = vlib_get_buffer (vm, from[3]);
2210
2211             vlib_prefetch_buffer_header (p2, LOAD);
2212             vlib_prefetch_buffer_header (p3, LOAD);
2213
2214             CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2215             CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
2216           }
2217
2218           /* Speculatively enqueue b0, b1 to the current next frame */
2219           to_next[0] = bi0 = from[0];
2220           to_next[1] = bi1 = from[1];
2221           from += 2;
2222           to_next += 2;
2223           n_left_from -= 2;
2224           n_left_to_next -= 2;
2225
2226           b0 = vlib_get_buffer (vm, bi0);
2227           b1 = vlib_get_buffer (vm, bi1);
2228
2229           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2230           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2231           ip_adjacency_t *adj0 = adj_get (adj_index0);
2232           u32 adj_index1 = vnet_buffer (b1)->ip.adj_index[VLIB_TX];
2233           ip_adjacency_t *adj1 = adj_get (adj_index1);
2234
2235           /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2236           next0 = adj0->lookup_next_index;
2237           next1 = adj1->lookup_next_index;
2238
2239           ip0 = vlib_buffer_get_current (b0);
2240           ip1 = vlib_buffer_get_current (b1);
2241           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2242           hbh1 = (ip6_hop_by_hop_header_t *) (ip1 + 1);
2243           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2244           opt1 = (ip6_hop_by_hop_option_t *) (hbh1 + 1);
2245           limit0 =
2246             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2247                                          ((hbh0->length + 1) << 3));
2248           limit1 =
2249             (ip6_hop_by_hop_option_t *) ((u8 *) hbh1 +
2250                                          ((hbh1->length + 1) << 3));
2251
2252           /*
2253            * Basic validity checks
2254            */
2255           if ((hbh0->length + 1) << 3 >
2256               clib_net_to_host_u16 (ip0->payload_length))
2257             {
2258               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2259               next0 = IP_LOOKUP_NEXT_DROP;
2260               goto outdual;
2261             }
2262           /* Scan the set of h-b-h options, process ones that we understand */
2263           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2264
2265           if ((hbh1->length + 1) << 3 >
2266               clib_net_to_host_u16 (ip1->payload_length))
2267             {
2268               error1 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2269               next1 = IP_LOOKUP_NEXT_DROP;
2270               goto outdual;
2271             }
2272           /* Scan the set of h-b-h options, process ones that we understand */
2273           error1 = ip6_scan_hbh_options (b1, ip1, hbh1, opt1, limit1, &next1);
2274
2275         outdual:
2276           /* Has the classifier flagged this buffer for special treatment? */
2277           if (PREDICT_FALSE
2278               ((error0 == 0)
2279                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2280             next0 = hm->next_override;
2281
2282           /* Has the classifier flagged this buffer for special treatment? */
2283           if (PREDICT_FALSE
2284               ((error1 == 0)
2285                && (vnet_buffer (b1)->l2_classify.opaque_index & OI_DECAP)))
2286             next1 = hm->next_override;
2287
2288           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
2289             {
2290               if (b0->flags & VLIB_BUFFER_IS_TRACED)
2291                 {
2292                   ip6_hop_by_hop_trace_t *t =
2293                     vlib_add_trace (vm, node, b0, sizeof (*t));
2294                   u32 trace_len = (hbh0->length + 1) << 3;
2295                   t->next_index = next0;
2296                   /* Capture the h-b-h option verbatim */
2297                   trace_len =
2298                     trace_len <
2299                     ARRAY_LEN (t->option_data) ? trace_len :
2300                     ARRAY_LEN (t->option_data);
2301                   t->trace_len = trace_len;
2302                   clib_memcpy (t->option_data, hbh0, trace_len);
2303                 }
2304               if (b1->flags & VLIB_BUFFER_IS_TRACED)
2305                 {
2306                   ip6_hop_by_hop_trace_t *t =
2307                     vlib_add_trace (vm, node, b1, sizeof (*t));
2308                   u32 trace_len = (hbh1->length + 1) << 3;
2309                   t->next_index = next1;
2310                   /* Capture the h-b-h option verbatim */
2311                   trace_len =
2312                     trace_len <
2313                     ARRAY_LEN (t->option_data) ? trace_len :
2314                     ARRAY_LEN (t->option_data);
2315                   t->trace_len = trace_len;
2316                   clib_memcpy (t->option_data, hbh1, trace_len);
2317                 }
2318
2319             }
2320
2321           b0->error = error_node->errors[error0];
2322           b1->error = error_node->errors[error1];
2323
2324           /* verify speculative enqueue, maybe switch current next frame */
2325           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
2326                                            n_left_to_next, bi0, bi1, next0,
2327                                            next1);
2328         }
2329
2330       while (n_left_from > 0 && n_left_to_next > 0)
2331         {
2332           u32 bi0;
2333           vlib_buffer_t *b0;
2334           u32 next0;
2335           ip6_header_t *ip0;
2336           ip6_hop_by_hop_header_t *hbh0;
2337           ip6_hop_by_hop_option_t *opt0, *limit0;
2338           u8 error0 = 0;
2339
2340           /* Speculatively enqueue b0 to the current next frame */
2341           bi0 = from[0];
2342           to_next[0] = bi0;
2343           from += 1;
2344           to_next += 1;
2345           n_left_from -= 1;
2346           n_left_to_next -= 1;
2347
2348           b0 = vlib_get_buffer (vm, bi0);
2349           /*
2350            * Default use the next_index from the adjacency.
2351            * A HBH option rarely redirects to a different node
2352            */
2353           u32 adj_index0 = vnet_buffer (b0)->ip.adj_index[VLIB_TX];
2354           ip_adjacency_t *adj0 = adj_get (adj_index0);
2355           next0 = adj0->lookup_next_index;
2356
2357           ip0 = vlib_buffer_get_current (b0);
2358           hbh0 = (ip6_hop_by_hop_header_t *) (ip0 + 1);
2359           opt0 = (ip6_hop_by_hop_option_t *) (hbh0 + 1);
2360           limit0 =
2361             (ip6_hop_by_hop_option_t *) ((u8 *) hbh0 +
2362                                          ((hbh0->length + 1) << 3));
2363
2364           /*
2365            * Basic validity checks
2366            */
2367           if ((hbh0->length + 1) << 3 >
2368               clib_net_to_host_u16 (ip0->payload_length))
2369             {
2370               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2371               next0 = IP_LOOKUP_NEXT_DROP;
2372               goto out0;
2373             }
2374
2375           /* Scan the set of h-b-h options, process ones that we understand */
2376           error0 = ip6_scan_hbh_options (b0, ip0, hbh0, opt0, limit0, &next0);
2377
2378         out0:
2379           /* Has the classifier flagged this buffer for special treatment? */
2380           if (PREDICT_FALSE
2381               ((error0 == 0)
2382                && (vnet_buffer (b0)->l2_classify.opaque_index & OI_DECAP)))
2383             next0 = hm->next_override;
2384
2385           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2386             {
2387               ip6_hop_by_hop_trace_t *t =
2388                 vlib_add_trace (vm, node, b0, sizeof (*t));
2389               u32 trace_len = (hbh0->length + 1) << 3;
2390               t->next_index = next0;
2391               /* Capture the h-b-h option verbatim */
2392               trace_len =
2393                 trace_len <
2394                 ARRAY_LEN (t->option_data) ? trace_len :
2395                 ARRAY_LEN (t->option_data);
2396               t->trace_len = trace_len;
2397               clib_memcpy (t->option_data, hbh0, trace_len);
2398             }
2399
2400           b0->error = error_node->errors[error0];
2401
2402           /* verify speculative enqueue, maybe switch current next frame */
2403           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2404                                            n_left_to_next, bi0, next0);
2405         }
2406       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2407     }
2408   return frame->n_vectors;
2409 }
2410
2411 /* *INDENT-OFF* */
2412 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) =
2413 {
2414   .function = ip6_hop_by_hop,
2415   .name = "ip6-hop-by-hop",
2416   .sibling_of = "ip6-lookup",
2417   .vector_size = sizeof (u32),
2418   .format_trace = format_ip6_hop_by_hop_trace,
2419   .type = VLIB_NODE_TYPE_INTERNAL,
2420   .n_errors = ARRAY_LEN (ip6_hop_by_hop_error_strings),
2421   .error_strings = ip6_hop_by_hop_error_strings,
2422   .n_next_nodes = 0,
2423 };
2424 /* *INDENT-ON* */
2425
2426 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop);
2427
2428 static clib_error_t *
2429 ip6_hop_by_hop_init (vlib_main_t * vm)
2430 {
2431   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2432   memset (hm->options, 0, sizeof (hm->options));
2433   memset (hm->trace, 0, sizeof (hm->trace));
2434   hm->next_override = IP6_LOOKUP_NEXT_POP_HOP_BY_HOP;
2435   return (0);
2436 }
2437
2438 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2439
2440 void
2441 ip6_hbh_set_next_override (uword next)
2442 {
2443   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2444
2445   hm->next_override = next;
2446 }
2447
2448 int
2449 ip6_hbh_register_option (u8 option,
2450                          int options (vlib_buffer_t * b, ip6_header_t * ip,
2451                                       ip6_hop_by_hop_option_t * opt),
2452                          u8 * trace (u8 * s, ip6_hop_by_hop_option_t * opt))
2453 {
2454   ip6_main_t *im = &ip6_main;
2455   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2456
2457   ASSERT ((u32) option < ARRAY_LEN (hm->options));
2458
2459   /* Already registered */
2460   if (hm->options[option])
2461     return (-1);
2462
2463   hm->options[option] = options;
2464   hm->trace[option] = trace;
2465
2466   /* Set global variable */
2467   im->hbh_enabled = 1;
2468
2469   return (0);
2470 }
2471
2472 int
2473 ip6_hbh_unregister_option (u8 option)
2474 {
2475   ip6_main_t *im = &ip6_main;
2476   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2477
2478   ASSERT ((u32) option < ARRAY_LEN (hm->options));
2479
2480   /* Not registered */
2481   if (!hm->options[option])
2482     return (-1);
2483
2484   hm->options[option] = NULL;
2485   hm->trace[option] = NULL;
2486
2487   /* Disable global knob if this was the last option configured */
2488   int i;
2489   bool found = false;
2490   for (i = 0; i < 256; i++)
2491     {
2492       if (hm->options[option])
2493         {
2494           found = true;
2495           break;
2496         }
2497     }
2498   if (!found)
2499     im->hbh_enabled = 0;
2500
2501   return (0);
2502 }
2503
2504 /* Global IP6 main. */
2505 ip6_main_t ip6_main;
2506
2507 static clib_error_t *
2508 ip6_lookup_init (vlib_main_t * vm)
2509 {
2510   ip6_main_t *im = &ip6_main;
2511   clib_error_t *error;
2512   uword i;
2513
2514   if ((error = vlib_call_init_function (vm, vnet_feature_init)))
2515     return error;
2516
2517   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2518     {
2519       u32 j, i0, i1;
2520
2521       i0 = i / 32;
2522       i1 = i % 32;
2523
2524       for (j = 0; j < i0; j++)
2525         im->fib_masks[i].as_u32[j] = ~0;
2526
2527       if (i1)
2528         im->fib_masks[i].as_u32[i0] =
2529           clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2530     }
2531
2532   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2533
2534   if (im->lookup_table_nbuckets == 0)
2535     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2536
2537   im->lookup_table_nbuckets = 1 << max_log2 (im->lookup_table_nbuckets);
2538
2539   if (im->lookup_table_size == 0)
2540     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2541
2542   BV (clib_bihash_init) (&(im->ip6_table[IP6_FIB_TABLE_FWDING].ip6_hash),
2543                          "ip6 FIB fwding table",
2544                          im->lookup_table_nbuckets, im->lookup_table_size);
2545   BV (clib_bihash_init) (&im->ip6_table[IP6_FIB_TABLE_NON_FWDING].ip6_hash,
2546                          "ip6 FIB non-fwding table",
2547                          im->lookup_table_nbuckets, im->lookup_table_size);
2548
2549   /* Create FIB with index 0 and table id of 0. */
2550   fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2551                                      FIB_SOURCE_DEFAULT_ROUTE);
2552   mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, 0,
2553                                       MFIB_SOURCE_DEFAULT_ROUTE);
2554
2555   {
2556     pg_node_t *pn;
2557     pn = pg_get_node (ip6_lookup_node.index);
2558     pn->unformat_edit = unformat_pg_ip6_header;
2559   }
2560
2561   /* Unless explicitly configured, don't process HBH options */
2562   im->hbh_enabled = 0;
2563
2564   {
2565     icmp6_neighbor_solicitation_header_t p;
2566
2567     memset (&p, 0, sizeof (p));
2568
2569     p.ip.ip_version_traffic_class_and_flow_label =
2570       clib_host_to_net_u32 (0x6 << 28);
2571     p.ip.payload_length =
2572       clib_host_to_net_u16 (sizeof (p) -
2573                             STRUCT_OFFSET_OF
2574                             (icmp6_neighbor_solicitation_header_t, neighbor));
2575     p.ip.protocol = IP_PROTOCOL_ICMP6;
2576     p.ip.hop_limit = 255;
2577     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2578
2579     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2580
2581     p.link_layer_option.header.type =
2582       ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2583     p.link_layer_option.header.n_data_u64s =
2584       sizeof (p.link_layer_option) / sizeof (u64);
2585
2586     vlib_packet_template_init (vm,
2587                                &im->discover_neighbor_packet_template,
2588                                &p, sizeof (p),
2589                                /* alloc chunk size */ 8,
2590                                "ip6 neighbor discovery");
2591   }
2592
2593   return error;
2594 }
2595
2596 VLIB_INIT_FUNCTION (ip6_lookup_init);
2597
2598 void
2599 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
2600                                                   u8 * mac)
2601 {
2602   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2603   /* Invert the "u" bit */
2604   ip->as_u8[8] = mac[0] ^ (1 << 1);
2605   ip->as_u8[9] = mac[1];
2606   ip->as_u8[10] = mac[2];
2607   ip->as_u8[11] = 0xFF;
2608   ip->as_u8[12] = 0xFE;
2609   ip->as_u8[13] = mac[3];
2610   ip->as_u8[14] = mac[4];
2611   ip->as_u8[15] = mac[5];
2612 }
2613
2614 void
2615 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
2616                                                   ip6_address_t * ip)
2617 {
2618   /* Invert the previously inverted "u" bit */
2619   mac[0] = ip->as_u8[8] ^ (1 << 1);
2620   mac[1] = ip->as_u8[9];
2621   mac[2] = ip->as_u8[10];
2622   mac[3] = ip->as_u8[13];
2623   mac[4] = ip->as_u8[14];
2624   mac[5] = ip->as_u8[15];
2625 }
2626
2627 static clib_error_t *
2628 test_ip6_link_command_fn (vlib_main_t * vm,
2629                           unformat_input_t * input, vlib_cli_command_t * cmd)
2630 {
2631   u8 mac[6];
2632   ip6_address_t _a, *a = &_a;
2633
2634   if (unformat (input, "%U", unformat_ethernet_address, mac))
2635     {
2636       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2637       vlib_cli_output (vm, "Link local address: %U", format_ip6_address, a);
2638       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2639       vlib_cli_output (vm, "Original MAC address: %U",
2640                        format_ethernet_address, mac);
2641     }
2642
2643   return 0;
2644 }
2645
2646 /*?
2647  * This command converts the given MAC Address into an IPv6 link-local
2648  * address.
2649  *
2650  * @cliexpar
2651  * Example of how to create an IPv6 link-local address:
2652  * @cliexstart{test ip6 link 16:d9:e0:91:79:86}
2653  * Link local address: fe80::14d9:e0ff:fe91:7986
2654  * Original MAC address: 16:d9:e0:91:79:86
2655  * @cliexend
2656 ?*/
2657 /* *INDENT-OFF* */
2658 VLIB_CLI_COMMAND (test_link_command, static) =
2659 {
2660   .path = "test ip6 link",
2661   .function = test_ip6_link_command_fn,
2662   .short_help = "test ip6 link <mac-address>",
2663 };
2664 /* *INDENT-ON* */
2665
2666 int
2667 vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2668 {
2669   u32 fib_index;
2670
2671   fib_index = fib_table_find (FIB_PROTOCOL_IP6, table_id);
2672
2673   if (~0 == fib_index)
2674     return VNET_API_ERROR_NO_SUCH_FIB;
2675
2676   fib_table_set_flow_hash_config (fib_index, FIB_PROTOCOL_IP6,
2677                                   flow_hash_config);
2678
2679   return 0;
2680 }
2681
2682 static clib_error_t *
2683 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2684                               unformat_input_t * input,
2685                               vlib_cli_command_t * cmd)
2686 {
2687   int matched = 0;
2688   u32 table_id = 0;
2689   u32 flow_hash_config = 0;
2690   int rv;
2691
2692   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2693     {
2694       if (unformat (input, "table %d", &table_id))
2695         matched = 1;
2696 #define _(a,v) \
2697     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2698       foreach_flow_hash_bit
2699 #undef _
2700         else
2701         break;
2702     }
2703
2704   if (matched == 0)
2705     return clib_error_return (0, "unknown input `%U'",
2706                               format_unformat_error, input);
2707
2708   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2709   switch (rv)
2710     {
2711     case 0:
2712       break;
2713
2714     case -1:
2715       return clib_error_return (0, "no such FIB table %d", table_id);
2716
2717     default:
2718       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2719       break;
2720     }
2721
2722   return 0;
2723 }
2724
2725 /*?
2726  * Configure the set of IPv6 fields used by the flow hash.
2727  *
2728  * @cliexpar
2729  * @parblock
2730  * Example of how to set the flow hash on a given table:
2731  * @cliexcmd{set ip6 flow-hash table 8 dst sport dport proto}
2732  *
2733  * Example of display the configured flow hash:
2734  * @cliexstart{show ip6 fib}
2735  * ipv6-VRF:0, fib_index 0, flow hash: src dst sport dport proto
2736  * @::/0
2737  *   unicast-ip6-chain
2738  *   [@0]: dpo-load-balance: [index:5 buckets:1 uRPF:5 to:[0:0]]
2739  *     [0] [@0]: dpo-drop ip6
2740  * fe80::/10
2741  *   unicast-ip6-chain
2742  *   [@0]: dpo-load-balance: [index:10 buckets:1 uRPF:10 to:[0:0]]
2743  *     [0] [@2]: dpo-receive
2744  * ff02::1/128
2745  *   unicast-ip6-chain
2746  *   [@0]: dpo-load-balance: [index:8 buckets:1 uRPF:8 to:[0:0]]
2747  *     [0] [@2]: dpo-receive
2748  * ff02::2/128
2749  *   unicast-ip6-chain
2750  *   [@0]: dpo-load-balance: [index:7 buckets:1 uRPF:7 to:[0:0]]
2751  *     [0] [@2]: dpo-receive
2752  * ff02::16/128
2753  *   unicast-ip6-chain
2754  *   [@0]: dpo-load-balance: [index:9 buckets:1 uRPF:9 to:[0:0]]
2755  *     [0] [@2]: dpo-receive
2756  * ff02::1:ff00:0/104
2757  *   unicast-ip6-chain
2758  *   [@0]: dpo-load-balance: [index:6 buckets:1 uRPF:6 to:[0:0]]
2759  *     [0] [@2]: dpo-receive
2760  * ipv6-VRF:8, fib_index 1, flow hash: dst sport dport proto
2761  * @::/0
2762  *   unicast-ip6-chain
2763  *   [@0]: dpo-load-balance: [index:21 buckets:1 uRPF:20 to:[0:0]]
2764  *     [0] [@0]: dpo-drop ip6
2765  * @::a:1:1:0:4/126
2766  *   unicast-ip6-chain
2767  *   [@0]: dpo-load-balance: [index:27 buckets:1 uRPF:26 to:[0:0]]
2768  *     [0] [@4]: ipv6-glean: af_packet0
2769  * @::a:1:1:0:7/128
2770  *   unicast-ip6-chain
2771  *   [@0]: dpo-load-balance: [index:28 buckets:1 uRPF:27 to:[0:0]]
2772  *     [0] [@2]: dpo-receive: @::a:1:1:0:7 on af_packet0
2773  * fe80::/10
2774  *   unicast-ip6-chain
2775  *   [@0]: dpo-load-balance: [index:26 buckets:1 uRPF:25 to:[0:0]]
2776  *     [0] [@2]: dpo-receive
2777  * fe80::fe:3eff:fe3e:9222/128
2778  *   unicast-ip6-chain
2779  *   [@0]: dpo-load-balance: [index:29 buckets:1 uRPF:28 to:[0:0]]
2780  *     [0] [@2]: dpo-receive: fe80::fe:3eff:fe3e:9222 on af_packet0
2781  * ff02::1/128
2782  *   unicast-ip6-chain
2783  *   [@0]: dpo-load-balance: [index:24 buckets:1 uRPF:23 to:[0:0]]
2784  *     [0] [@2]: dpo-receive
2785  * ff02::2/128
2786  *   unicast-ip6-chain
2787  *   [@0]: dpo-load-balance: [index:23 buckets:1 uRPF:22 to:[0:0]]
2788  *     [0] [@2]: dpo-receive
2789  * ff02::16/128
2790  *   unicast-ip6-chain
2791  *   [@0]: dpo-load-balance: [index:25 buckets:1 uRPF:24 to:[0:0]]
2792  *     [0] [@2]: dpo-receive
2793  * ff02::1:ff00:0/104
2794  *   unicast-ip6-chain
2795  *   [@0]: dpo-load-balance: [index:22 buckets:1 uRPF:21 to:[0:0]]
2796  *     [0] [@2]: dpo-receive
2797  * @cliexend
2798  * @endparblock
2799 ?*/
2800 /* *INDENT-OFF* */
2801 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) =
2802 {
2803   .path = "set ip6 flow-hash",
2804   .short_help =
2805   "set ip6 flow-hash table <table-id> [src] [dst] [sport] [dport] [proto] [reverse]",
2806   .function = set_ip6_flow_hash_command_fn,
2807 };
2808 /* *INDENT-ON* */
2809
2810 static clib_error_t *
2811 show_ip6_local_command_fn (vlib_main_t * vm,
2812                            unformat_input_t * input, vlib_cli_command_t * cmd)
2813 {
2814   ip6_main_t *im = &ip6_main;
2815   ip_lookup_main_t *lm = &im->lookup_main;
2816   int i;
2817
2818   vlib_cli_output (vm, "Protocols handled by ip6_local");
2819   for (i = 0; i < ARRAY_LEN (lm->local_next_by_ip_protocol); i++)
2820     {
2821       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2822         {
2823
2824           u32 node_index = vlib_get_node (vm,
2825                                           ip6_local_node.index)->
2826             next_nodes[lm->local_next_by_ip_protocol[i]];
2827           vlib_cli_output (vm, "%d: %U", i, format_vlib_node_name, vm,
2828                            node_index);
2829         }
2830     }
2831   return 0;
2832 }
2833
2834
2835
2836 /*?
2837  * Display the set of protocols handled by the local IPv6 stack.
2838  *
2839  * @cliexpar
2840  * Example of how to display local protocol table:
2841  * @cliexstart{show ip6 local}
2842  * Protocols handled by ip6_local
2843  * 17
2844  * 43
2845  * 58
2846  * 115
2847  * @cliexend
2848 ?*/
2849 /* *INDENT-OFF* */
2850 VLIB_CLI_COMMAND (show_ip6_local, static) =
2851 {
2852   .path = "show ip6 local",
2853   .function = show_ip6_local_command_fn,
2854   .short_help = "show ip6 local",
2855 };
2856 /* *INDENT-ON* */
2857
2858 int
2859 vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
2860                              u32 table_index)
2861 {
2862   vnet_main_t *vnm = vnet_get_main ();
2863   vnet_interface_main_t *im = &vnm->interface_main;
2864   ip6_main_t *ipm = &ip6_main;
2865   ip_lookup_main_t *lm = &ipm->lookup_main;
2866   vnet_classify_main_t *cm = &vnet_classify_main;
2867   ip6_address_t *if_addr;
2868
2869   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2870     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2871
2872   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2873     return VNET_API_ERROR_NO_SUCH_ENTRY;
2874
2875   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2876   lm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
2877
2878   if_addr = ip6_interface_first_address (ipm, sw_if_index);
2879
2880   if (NULL != if_addr)
2881     {
2882       fib_prefix_t pfx = {
2883         .fp_len = 128,
2884         .fp_proto = FIB_PROTOCOL_IP6,
2885         .fp_addr.ip6 = *if_addr,
2886       };
2887       u32 fib_index;
2888
2889       fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2890                                                        sw_if_index);
2891
2892
2893       if (table_index != (u32) ~ 0)
2894         {
2895           dpo_id_t dpo = DPO_INVALID;
2896
2897           dpo_set (&dpo,
2898                    DPO_CLASSIFY,
2899                    DPO_PROTO_IP6,
2900                    classify_dpo_create (DPO_PROTO_IP6, table_index));
2901
2902           fib_table_entry_special_dpo_add (fib_index,
2903                                            &pfx,
2904                                            FIB_SOURCE_CLASSIFY,
2905                                            FIB_ENTRY_FLAG_NONE, &dpo);
2906           dpo_reset (&dpo);
2907         }
2908       else
2909         {
2910           fib_table_entry_special_remove (fib_index,
2911                                           &pfx, FIB_SOURCE_CLASSIFY);
2912         }
2913     }
2914
2915   return 0;
2916 }
2917
2918 static clib_error_t *
2919 set_ip6_classify_command_fn (vlib_main_t * vm,
2920                              unformat_input_t * input,
2921                              vlib_cli_command_t * cmd)
2922 {
2923   u32 table_index = ~0;
2924   int table_index_set = 0;
2925   u32 sw_if_index = ~0;
2926   int rv;
2927
2928   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2929     {
2930       if (unformat (input, "table-index %d", &table_index))
2931         table_index_set = 1;
2932       else if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
2933                          vnet_get_main (), &sw_if_index))
2934         ;
2935       else
2936         break;
2937     }
2938
2939   if (table_index_set == 0)
2940     return clib_error_return (0, "classify table-index must be specified");
2941
2942   if (sw_if_index == ~0)
2943     return clib_error_return (0, "interface / subif must be specified");
2944
2945   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2946
2947   switch (rv)
2948     {
2949     case 0:
2950       break;
2951
2952     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2953       return clib_error_return (0, "No such interface");
2954
2955     case VNET_API_ERROR_NO_SUCH_ENTRY:
2956       return clib_error_return (0, "No such classifier table");
2957     }
2958   return 0;
2959 }
2960
2961 /*?
2962  * Assign a classification table to an interface. The classification
2963  * table is created using the '<em>classify table</em>' and '<em>classify session</em>'
2964  * commands. Once the table is create, use this command to filter packets
2965  * on an interface.
2966  *
2967  * @cliexpar
2968  * Example of how to assign a classification table to an interface:
2969  * @cliexcmd{set ip6 classify intfc GigabitEthernet2/0/0 table-index 1}
2970 ?*/
2971 /* *INDENT-OFF* */
2972 VLIB_CLI_COMMAND (set_ip6_classify_command, static) =
2973 {
2974   .path = "set ip6 classify",
2975   .short_help =
2976   "set ip6 classify intfc <interface> table-index <classify-idx>",
2977   .function = set_ip6_classify_command_fn,
2978 };
2979 /* *INDENT-ON* */
2980
2981 static clib_error_t *
2982 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2983 {
2984   ip6_main_t *im = &ip6_main;
2985   uword heapsize = 0;
2986   u32 tmp;
2987   u32 nbuckets = 0;
2988
2989   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2990     {
2991       if (unformat (input, "hash-buckets %d", &tmp))
2992         nbuckets = tmp;
2993       else if (unformat (input, "heap-size %U",
2994                          unformat_memory_size, &heapsize))
2995         ;
2996       else
2997         return clib_error_return (0, "unknown input '%U'",
2998                                   format_unformat_error, input);
2999     }
3000
3001   im->lookup_table_nbuckets = nbuckets;
3002   im->lookup_table_size = heapsize;
3003
3004   return 0;
3005 }
3006
3007 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3008
3009 /*
3010  * fd.io coding-style-patch-verification: ON
3011  *
3012  * Local Variables:
3013  * eval: (c-set-style "gnu")
3014  * End:
3015  */