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