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