c8b9df01a88ad5d11798d053c9ca0af6c99c225f
[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       p = hash_get (im->fib_index_by_table_id, table_index_or_id);
193       if (! p)
194         return create_fib_with_table_id (im, table_index_or_id);
195       fib_index = p[0];
196     }
197   return vec_elt_at_index (im->fibs, fib_index);
198 }
199
200 void ip6_add_del_route (ip6_main_t * im, ip6_add_del_route_args_t * a)
201 {
202   ip_lookup_main_t * lm = &im->lookup_main;
203   ip6_fib_t * fib;
204   ip6_address_t dst_address;
205   u32 dst_address_length, adj_index;
206   uword is_del;
207   u32 old_adj_index = ~0;
208   BVT(clib_bihash_kv) kv, value;
209
210   vlib_smp_unsafe_warning();
211
212   is_del = (a->flags & IP6_ROUTE_FLAG_DEL) != 0;
213
214   /* Either create new adjacency or use given one depending on arguments. */
215   if (a->n_add_adj > 0)
216     {
217       ip_add_adjacency (lm, a->add_adj, a->n_add_adj, &adj_index);
218       ip_call_add_del_adjacency_callbacks (lm, adj_index, /* is_del */ 0);
219     }
220   else
221     adj_index = a->adj_index;
222
223   dst_address = a->dst_address;
224   dst_address_length = a->dst_address_length;
225   fib = find_ip6_fib_by_table_index_or_id (im, a->table_index_or_table_id, 
226                                            a->flags);
227
228   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
229   ip6_address_mask (&dst_address, &im->fib_masks[dst_address_length]);
230
231   /* refcount accounting */
232   if (is_del)
233     {
234       ASSERT (im->dst_address_length_refcounts[dst_address_length] > 0);
235       if (--im->dst_address_length_refcounts[dst_address_length] == 0)
236         {
237           im->non_empty_dst_address_length_bitmap =
238             clib_bitmap_set (im->non_empty_dst_address_length_bitmap, 
239                              128 - dst_address_length, 0);
240           compute_prefix_lengths_in_search_order (im);
241         }
242     }
243   else
244     {
245       im->dst_address_length_refcounts[dst_address_length]++;
246
247       im->non_empty_dst_address_length_bitmap =
248         clib_bitmap_set (im->non_empty_dst_address_length_bitmap, 
249                              128 - dst_address_length, 1);
250       compute_prefix_lengths_in_search_order (im);
251     }
252     
253   kv.key[0] = dst_address.as_u64[0];
254   kv.key[1] = dst_address.as_u64[1];
255   kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
256
257   if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
258     old_adj_index = value.value;
259
260   if (is_del)
261     BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 0 /* is_add */);
262   else
263     {
264       /* Make sure adj index is valid. */
265       if (CLIB_DEBUG > 0)
266         (void) ip_get_adjacency (lm, adj_index);
267
268       kv.value = adj_index;
269
270       BV(clib_bihash_add_del) (&im->ip6_lookup_table, &kv, 1 /* is_add */);
271     }
272
273   /* Avoid spurious reference count increments */
274   if (old_adj_index == adj_index && !(a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY))
275     {
276       ip_adjacency_t * adj = ip_get_adjacency (lm, adj_index);
277       if (adj->share_count > 0)
278         adj->share_count --;
279     }
280
281   /* Delete old adjacency index if present and changed. */
282   {
283     if (! (a->flags & IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY)
284         && old_adj_index != ~0
285         && old_adj_index != adj_index)
286       ip_del_adjacency (lm, old_adj_index);
287   }
288 }
289
290 void
291 ip6_add_del_route_next_hop (ip6_main_t * im,
292                             u32 flags,
293                             ip6_address_t * dst_address,
294                             u32 dst_address_length,
295                             ip6_address_t * next_hop,
296                             u32 next_hop_sw_if_index,
297                             u32 next_hop_weight, u32 adj_index,
298                             u32 explicit_fib_index)
299 {
300   vnet_main_t * vnm = vnet_get_main();
301   ip_lookup_main_t * lm = &im->lookup_main;
302   u32 fib_index;
303   ip6_fib_t * fib;
304   ip6_address_t masked_dst_address;
305   u32 old_mp_adj_index, new_mp_adj_index;
306   u32 dst_adj_index, nh_adj_index;
307   int rv;
308   ip_adjacency_t * dst_adj;
309   ip_multipath_adjacency_t * old_mp, * new_mp;
310   int is_del = (flags & IP6_ROUTE_FLAG_DEL) != 0;
311   int is_interface_next_hop;
312   clib_error_t * error = 0;
313   uword * nh_result;
314   BVT(clib_bihash_kv) kv, value;
315
316   vlib_smp_unsafe_warning();
317
318   if (explicit_fib_index == (u32)~0)
319     fib_index = vec_elt (im->fib_index_by_sw_if_index, next_hop_sw_if_index);
320   else
321     fib_index = explicit_fib_index;
322
323   fib = vec_elt_at_index (im->fibs, fib_index);
324
325   /* Lookup next hop to be added or deleted. */
326   is_interface_next_hop = ip6_address_is_zero (next_hop);
327   if (adj_index == (u32)~0)
328     {
329       if (is_interface_next_hop)
330         {
331           nh_result = hash_get (im->interface_route_adj_index_by_sw_if_index, 
332                                 next_hop_sw_if_index);
333           if (nh_result)
334             nh_adj_index = *nh_result;
335           else
336             {
337               ip_adjacency_t * adj;
338               adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
339                                       &nh_adj_index);
340               ip6_adjacency_set_interface_route (vnm, adj, 
341                                                  next_hop_sw_if_index, ~0);
342               ip_call_add_del_adjacency_callbacks 
343                 (lm, next_hop_sw_if_index, /* is_del */ 0);
344               hash_set (im->interface_route_adj_index_by_sw_if_index, 
345                         next_hop_sw_if_index, nh_adj_index);
346             }
347         }
348       else
349         {
350           /* Look for the interface /128 route */
351           kv.key[0] = next_hop->as_u64[0];
352           kv.key[1] = next_hop->as_u64[1];
353           kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
354
355           if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
356             {
357               vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
358               error = clib_error_return (0, "next-hop %U/128 not in FIB",
359                                          format_ip6_address, next_hop);
360               goto done;
361             }
362           
363           nh_adj_index = value.value;
364         }
365     }
366   else
367     {
368       /* Look for the interface /128 route */
369       kv.key[0] = next_hop->as_u64[0];
370       kv.key[1] = next_hop->as_u64[1];
371       kv.key[2] = ((u64)((fib - im->fibs))<<32) | 128;
372       
373       if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) < 0)
374         {
375           vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
376           error = clib_error_return (0, "next-hop %U/128 not in FIB",
377                                      format_ip6_address, next_hop);
378           goto done;
379         }
380       
381       nh_adj_index = value.value;
382     }
383
384   ASSERT (dst_address_length < ARRAY_LEN (im->fib_masks));
385   masked_dst_address = dst_address[0];
386   ip6_address_mask (&masked_dst_address, &im->fib_masks[dst_address_length]);
387
388   kv.key[0] = masked_dst_address.as_u64[0];
389   kv.key[1] = masked_dst_address.as_u64[1];
390   kv.key[2] = ((u64)((fib - im->fibs))<<32) | dst_address_length;
391
392   rv = BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value);
393
394   if (rv == 0)
395     {
396       dst_adj_index = value.value;
397       dst_adj = ip_get_adjacency (lm, dst_adj_index);
398     }
399   else
400     {
401       /* For deletes destination must be known. */
402       if (is_del)
403         {
404           vnm->api_errno = VNET_API_ERROR_UNKNOWN_DESTINATION;
405           error = clib_error_return (0, "unknown destination %U/%d",
406                                      format_ip6_address, dst_address,
407                                      dst_address_length);
408           goto done;
409         }
410
411       dst_adj_index = ~0;
412       dst_adj = 0;
413     }
414
415   /* Ignore adds of X/128 with next hop of X. */
416   if (! is_del
417       && dst_address_length == 128
418       && ip6_address_is_equal (dst_address, next_hop))
419     {
420       vnm->api_errno = VNET_API_ERROR_PREFIX_MATCHES_NEXT_HOP;
421       error = clib_error_return (0, "prefix matches next hop %U/%d",
422                                  format_ip6_address, dst_address,
423                                  dst_address_length);
424       goto done;
425     }
426
427   old_mp_adj_index = dst_adj ? dst_adj->heap_handle : ~0;
428
429   if (! ip_multipath_adjacency_add_del_next_hop
430       (lm, is_del,
431        dst_adj ? dst_adj->heap_handle : ~0,
432        nh_adj_index,
433        next_hop_weight,
434        &new_mp_adj_index))
435     {
436       vnm->api_errno = VNET_API_ERROR_NEXT_HOP_NOT_FOUND_MP;
437       error = clib_error_return 
438         (0, "requested deleting next-hop %U not found in multi-path",
439          format_ip6_address, next_hop);
440       goto done;
441     }
442   
443   old_mp = new_mp = 0;
444   if (old_mp_adj_index != ~0)
445     old_mp = vec_elt_at_index (lm->multipath_adjacencies, old_mp_adj_index);
446   if (new_mp_adj_index != ~0)
447     new_mp = vec_elt_at_index (lm->multipath_adjacencies, new_mp_adj_index);
448
449   if (old_mp != new_mp)
450     {
451       ip6_add_del_route_args_t a;
452       a.table_index_or_table_id = fib_index;
453       a.flags = ((is_del ? IP6_ROUTE_FLAG_DEL : IP6_ROUTE_FLAG_ADD)
454                  | IP6_ROUTE_FLAG_FIB_INDEX
455                  | IP6_ROUTE_FLAG_KEEP_OLD_ADJACENCY
456                  | (flags & IP6_ROUTE_FLAG_NO_REDISTRIBUTE));
457       a.dst_address = dst_address[0];
458       a.dst_address_length = dst_address_length;
459       a.adj_index = new_mp ? new_mp->adj_index : dst_adj_index;
460       a.add_adj = 0;
461       a.n_add_adj = 0;
462
463       ip6_add_del_route (im, &a);
464     }
465
466  done:
467   if (error)
468     clib_error_report (error);
469 }
470
471 u32
472 ip6_get_route (ip6_main_t * im,
473                u32 table_index_or_table_id,
474                u32 flags,
475                ip6_address_t * address,
476                u32 address_length)
477 {
478   ip6_fib_t * fib = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
479   ip6_address_t masked_address;
480   BVT(clib_bihash_kv) kv, value;
481
482   ASSERT (address_length < ARRAY_LEN (im->fib_masks));
483   clib_memcpy (&masked_address, address, sizeof (masked_address));
484   ip6_address_mask (&masked_address, &im->fib_masks[address_length]);
485
486   kv.key[0] = masked_address.as_u64[0];
487   kv.key[1] = masked_address.as_u64[1];
488   kv.key[2] = ((u64)((fib - im->fibs))<<32) | address_length;
489
490   if (BV(clib_bihash_search)(&im->ip6_lookup_table, &kv, &value) == 0)
491     return (value.value);
492   return 0;
493 }
494
495 void
496 ip6_foreach_matching_route (ip6_main_t * im,
497                             u32 table_index_or_table_id,
498                             u32 flags,
499                             ip6_address_t * dst_address,
500                             u32 address_length,
501                             ip6_address_t ** results,
502                             u8 ** result_lengths)
503 {
504   ip6_fib_t * fib = 
505     find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
506   BVT(clib_bihash) * h = &im->ip6_lookup_table;
507   BVT(clib_bihash_value) * v;
508   clib_bihash_bucket_t * b;
509   int i, j, k;
510   
511   if (*results)
512     _vec_len (*results) = 0;
513   if (*result_lengths)
514     _vec_len (*result_lengths) = 0;
515
516   /* Walk the table looking for routes which match the supplied address */
517   for (i = 0; i < h->nbuckets; i++)
518     {
519       b = &h->buckets [i];
520       if (b->offset == 0)
521           continue;
522
523       v = BV(clib_bihash_get_value) (h, b->offset);
524       for (j = 0; j < (1<<b->log2_pages); j++)
525         {
526           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
527             {
528               if (BV(clib_bihash_is_free)(&v->kvp[k]))
529                 continue;
530               
531               if ((v->kvp[k].key[2] 
532                    == (((u64)((fib - im->fibs))<<32) | address_length))
533                   && ip6_destination_matches_route 
534                   (im, dst_address, (ip6_address_t *) &v->kvp[k], 
535                    address_length))
536                 {
537                   ip6_address_t * a;
538
539                   a = (ip6_address_t *)(&v->kvp[k]);
540
541                   vec_add1 (*results, a[0]);
542                   vec_add1 (*result_lengths, address_length);
543                 }
544             }
545           v++;
546         }
547     }
548 }
549
550 void ip6_maybe_remap_adjacencies (ip6_main_t * im,
551                                   u32 table_index_or_table_id,
552                                   u32 flags)
553 {
554 #if SOONE
555   ip6_fib_t * fib 
556     = find_ip6_fib_by_table_index_or_id (im, table_index_or_table_id, flags);
557 #endif
558   ip_lookup_main_t * lm = &im->lookup_main;
559
560   if (lm->n_adjacency_remaps == 0)
561     return;
562
563   clib_warning ("unimplemented, please report to vpp-dev@cisco.com");
564
565   /* All remaps have been performed. */
566   lm->n_adjacency_remaps = 0;
567 }
568
569 void ip6_delete_matching_routes (ip6_main_t * im,
570                                  u32 table_index_or_table_id,
571                                  u32 flags,
572                                  ip6_address_t * address,
573                                  u32 address_length)
574 {
575   /* $$$$ static may be OK - this should happen only on thread 0 */
576   static ip6_address_t * matching_addresses;
577   static u8 * matching_address_lengths;
578   u32 l, i;
579   ip6_add_del_route_args_t a;
580
581   vlib_smp_unsafe_warning();
582
583   a.flags = IP6_ROUTE_FLAG_DEL | IP6_ROUTE_FLAG_NO_REDISTRIBUTE | flags;
584   a.table_index_or_table_id = table_index_or_table_id;
585   a.adj_index = ~0;
586   a.add_adj = 0;
587   a.n_add_adj = 0;
588
589   for (l = address_length + 1; l <= 128; l++)
590     {
591       ip6_foreach_matching_route (im, table_index_or_table_id, flags,
592                                   address,
593                                   l,
594                                   &matching_addresses,
595                                   &matching_address_lengths);
596       for (i = 0; i < vec_len (matching_addresses); i++)
597         {
598           a.dst_address = matching_addresses[i];
599           a.dst_address_length = matching_address_lengths[i];
600           ip6_add_del_route (im, &a);
601         }
602     }
603
604   ip6_maybe_remap_adjacencies (im, table_index_or_table_id, flags);
605 }
606
607 static uword
608 ip6_lookup (vlib_main_t * vm,
609             vlib_node_runtime_t * node,
610             vlib_frame_t * frame)
611 {
612   ip6_main_t * im = &ip6_main;
613   ip_lookup_main_t * lm = &im->lookup_main;
614   vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
615   u32 n_left_from, n_left_to_next, * from, * to_next;
616   ip_lookup_next_t next;
617   u32 cpu_index = os_get_cpu_number();
618
619   from = vlib_frame_vector_args (frame);
620   n_left_from = frame->n_vectors;
621   next = node->cached_next_index;
622
623   while (n_left_from > 0)
624     {
625       vlib_get_next_frame (vm, node, next,
626                            to_next, n_left_to_next);
627
628       while (n_left_from >= 4 && n_left_to_next >= 2)
629         {
630           vlib_buffer_t * p0, * p1;
631           u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
632           ip_lookup_next_t next0, next1;
633           ip6_header_t * ip0, * ip1;
634           ip_adjacency_t * adj0, * adj1;
635           u32 fib_index0, fib_index1;
636           u32 flow_hash_config0, flow_hash_config1;
637
638           /* Prefetch next iteration. */
639           {
640             vlib_buffer_t * p2, * p3;
641
642             p2 = vlib_get_buffer (vm, from[2]);
643             p3 = vlib_get_buffer (vm, from[3]);
644
645             vlib_prefetch_buffer_header (p2, LOAD);
646             vlib_prefetch_buffer_header (p3, LOAD);
647             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
648             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
649           }
650
651           pi0 = to_next[0] = from[0];
652           pi1 = to_next[1] = from[1];
653
654           p0 = vlib_get_buffer (vm, pi0);
655           p1 = vlib_get_buffer (vm, pi1);
656
657           ip0 = vlib_buffer_get_current (p0);
658           ip1 = vlib_buffer_get_current (p1);
659
660           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
661           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
662
663           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
664             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
665           fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
666             fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
667
668           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, 
669                                                   &ip0->dst_address);
670           adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, 
671                                                   &ip1->dst_address);
672
673           adj0 = ip_get_adjacency (lm, adj_index0);
674           adj1 = ip_get_adjacency (lm, adj_index1);
675
676           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
677             {
678               adj_index0 = ip6_fib_lookup_with_table 
679                 (im, adj0->explicit_fib_index, &ip0->dst_address);
680               adj0 = ip_get_adjacency (lm, adj_index0);
681             }
682           if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
683             {
684               adj_index1 = ip6_fib_lookup_with_table 
685                 (im, adj1->explicit_fib_index, &ip1->dst_address);
686               adj1 = ip_get_adjacency (lm, adj_index1);
687             }
688
689           next0 = adj0->lookup_next_index;
690           next1 = adj1->lookup_next_index;
691
692           /* Process hop-by-hop options if present */
693           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
694               IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
695           next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
696               IP_LOOKUP_NEXT_HOP_BY_HOP : next1;
697
698           vnet_buffer (p0)->ip.flow_hash = 
699             vnet_buffer(p1)->ip.flow_hash = 0;
700
701           if (PREDICT_FALSE(adj0->n_adj > 1))
702             {
703               flow_hash_config0 = 
704                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
705               vnet_buffer (p0)->ip.flow_hash = 
706                 ip6_compute_flow_hash (ip0, flow_hash_config0);
707             }
708
709           if (PREDICT_FALSE(adj1->n_adj > 1))
710             {
711               flow_hash_config1 = 
712                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
713
714               vnet_buffer (p1)->ip.flow_hash = 
715                 ip6_compute_flow_hash (ip1, flow_hash_config1);
716             }
717
718           ASSERT (adj0->n_adj > 0);
719           ASSERT (adj1->n_adj > 0);
720           ASSERT (is_pow2 (adj0->n_adj));
721           ASSERT (is_pow2 (adj1->n_adj));
722           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
723           adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
724
725           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
726           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
727
728           vlib_increment_combined_counter 
729               (cm, cpu_index, adj_index0, 1,
730                vlib_buffer_length_in_chain (vm, p0));
731           vlib_increment_combined_counter 
732               (cm, cpu_index, adj_index1, 1,
733                vlib_buffer_length_in_chain (vm, p1));
734
735           from += 2;
736           to_next += 2;
737           n_left_to_next -= 2;
738           n_left_from -= 2;
739
740           wrong_next = (next0 != next) + 2*(next1 != next);
741           if (PREDICT_FALSE (wrong_next != 0))
742             {
743               switch (wrong_next)
744                 {
745                 case 1:
746                   /* A B A */
747                   to_next[-2] = pi1;
748                   to_next -= 1;
749                   n_left_to_next += 1;
750                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
751                   break;
752
753                 case 2:
754                   /* A A B */
755                   to_next -= 1;
756                   n_left_to_next += 1;
757                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
758                   break;
759
760                 case 3:
761                   /* A B C */
762                   to_next -= 2;
763                   n_left_to_next += 2;
764                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
765                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
766                   if (next0 == next1)
767                     {
768                       /* A B B */
769                       vlib_put_next_frame (vm, node, next, n_left_to_next);
770                       next = next1;
771                       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
772                     }
773                 }
774             }
775         }
776     
777       while (n_left_from > 0 && n_left_to_next > 0)
778         {
779           vlib_buffer_t * p0;
780           ip6_header_t * ip0;
781           u32 pi0, adj_index0;
782           ip_lookup_next_t next0;
783           ip_adjacency_t * adj0;
784           u32 fib_index0, flow_hash_config0;
785
786           pi0 = from[0];
787           to_next[0] = pi0;
788
789           p0 = vlib_get_buffer (vm, pi0);
790
791           ip0 = vlib_buffer_get_current (p0);
792
793           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
794           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
795             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
796
797           flow_hash_config0 = 
798               vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
799
800           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, 
801                                                   &ip0->dst_address);
802
803           adj0 = ip_get_adjacency (lm, adj_index0);
804
805           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
806             {
807               adj_index0 = ip6_fib_lookup_with_table 
808                 (im, adj0->explicit_fib_index, &ip0->dst_address);
809               adj0 = ip_get_adjacency (lm, adj_index0);
810             }
811
812           next0 = adj0->lookup_next_index;
813           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) ?
814               IP_LOOKUP_NEXT_HOP_BY_HOP : next0;
815
816           vnet_buffer (p0)->ip.flow_hash = 0;
817
818           if (PREDICT_FALSE(adj0->n_adj > 1))
819             {
820               flow_hash_config0 = 
821                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
822               vnet_buffer (p0)->ip.flow_hash = 
823                 ip6_compute_flow_hash (ip0, flow_hash_config0);
824             }
825
826           ASSERT (adj0->n_adj > 0);
827           ASSERT (is_pow2 (adj0->n_adj));
828           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
829
830           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
831
832           vlib_increment_combined_counter 
833               (cm, cpu_index, adj_index0, 1,
834                vlib_buffer_length_in_chain (vm, p0));
835
836           from += 1;
837           to_next += 1;
838           n_left_to_next -= 1;
839           n_left_from -= 1;
840
841           if (PREDICT_FALSE (next0 != next))
842             {
843               n_left_to_next += 1;
844               vlib_put_next_frame (vm, node, next, n_left_to_next);
845               next = next0;
846               vlib_get_next_frame (vm, node, next,
847                                    to_next, n_left_to_next);
848               to_next[0] = pi0;
849               to_next += 1;
850               n_left_to_next -= 1;
851             }
852         }
853
854       vlib_put_next_frame (vm, node, next, n_left_to_next);
855     }
856
857   return frame->n_vectors;
858 }
859
860 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
861                                         ip_adjacency_t * adj,
862                                         u32 sw_if_index,
863                                         u32 if_address_index)
864 {
865   vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
866   ip_lookup_next_t n;
867   u32 node_index;
868
869   if (hw->hw_class_index == ethernet_hw_interface_class.index
870       || hw->hw_class_index == srp_hw_interface_class.index)
871     {
872       n = IP_LOOKUP_NEXT_ARP;
873       node_index = ip6_discover_neighbor_node.index;
874       adj->if_address_index = if_address_index;
875   }
876   else
877     {
878       n = IP_LOOKUP_NEXT_REWRITE;
879       node_index = ip6_rewrite_node.index;
880     }
881
882  adj->lookup_next_index = n;
883  adj->explicit_fib_index = ~0;
884
885  vnet_rewrite_for_sw_interface
886    (vnm,
887     VNET_L3_PACKET_TYPE_IP6,
888     sw_if_index,
889     node_index,
890     VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
891     &adj->rewrite_header,
892     sizeof (adj->rewrite_data));
893 }
894
895 static void
896 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
897                           ip6_main_t * im, u32 fib_index,
898                           ip_interface_address_t * a)
899 {
900   ip_lookup_main_t * lm = &im->lookup_main;
901   ip_adjacency_t * adj;
902   ip6_address_t * address = ip_interface_address_get_address (lm, a);
903   ip6_add_del_route_args_t x;
904   vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
905   u32 classify_table_index;
906
907   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
908   x.table_index_or_table_id = fib_index;
909   x.flags = (IP6_ROUTE_FLAG_ADD
910              | IP6_ROUTE_FLAG_FIB_INDEX
911              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
912   x.dst_address = address[0];
913   x.dst_address_length = a->address_length;
914   x.n_add_adj = 0;
915   x.add_adj = 0;
916
917   a->neighbor_probe_adj_index = ~0;
918   if (a->address_length < 128)
919     {
920       adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
921                               &x.adj_index);
922       ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
923       ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
924       ip6_add_del_route (im, &x);
925       a->neighbor_probe_adj_index = x.adj_index;
926     }
927
928   /* Add e.g. ::1/128 as local to this host. */
929   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
930                           &x.adj_index);
931
932   classify_table_index = ~0;
933   if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
934     classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
935   if (classify_table_index != (u32) ~0)
936     {
937       adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
938       adj->classify.table_index = classify_table_index;
939     }
940   else
941     adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
942   
943   adj->if_address_index = a - lm->if_address_pool;
944   adj->rewrite_header.sw_if_index = sw_if_index;
945   adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
946   adj->rewrite_header.data_bytes = 0;
947   ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
948   x.dst_address_length = 128;
949   ip6_add_del_route (im, &x);
950 }
951
952 static void
953 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
954                           ip6_address_t * address, u32 address_length)
955 {
956   ip6_add_del_route_args_t x;
957
958   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
959   x.table_index_or_table_id = fib_index;
960   x.flags = (IP6_ROUTE_FLAG_DEL
961              | IP6_ROUTE_FLAG_FIB_INDEX
962              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
963   x.dst_address = address[0];
964   x.dst_address_length = address_length;
965   x.adj_index = ~0;
966   x.n_add_adj = 0;
967   x.add_adj = 0;
968
969   if (address_length < 128)
970     {
971       /* Don't wipe out fe80::0/64 */
972       if (address_length != 64 || 
973           address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
974         ip6_add_del_route (im, &x);
975     }
976
977   x.dst_address_length = 128;
978   ip6_add_del_route (im, &x);
979
980   ip6_delete_matching_routes (im,
981                               fib_index,
982                               IP6_ROUTE_FLAG_FIB_INDEX,
983                               address,
984                               address_length);
985 }
986
987 typedef struct {
988     u32 sw_if_index;
989     ip6_address_t address;
990     u32 length;
991 } ip6_interface_address_t;
992
993 static clib_error_t *
994 ip6_add_del_interface_address_internal (vlib_main_t * vm,
995                                         u32 sw_if_index,
996                                         ip6_address_t * new_address,
997                                         u32 new_length,
998                                         u32 redistribute,
999                                         u32 insert_routes,
1000                                         u32 is_del);
1001
1002 static clib_error_t *
1003 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1004                                         u32 sw_if_index,
1005                                         ip6_address_t * address,
1006                                         u32 address_length,
1007                                         u32 redistribute,
1008                                         u32 insert_routes,
1009                                         u32 is_del)
1010 {
1011   vnet_main_t * vnm = vnet_get_main();
1012   ip6_main_t * im = &ip6_main;
1013   ip_lookup_main_t * lm = &im->lookup_main;
1014   clib_error_t * error;
1015   u32 if_address_index;
1016   ip6_address_fib_t ip6_af, * addr_fib = 0;
1017
1018   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1019   ip6_addr_fib_init (&ip6_af, address,
1020                      vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1021   vec_add1 (addr_fib, ip6_af);
1022
1023   {
1024     uword elts_before = pool_elts (lm->if_address_pool);
1025
1026     error = ip_interface_address_add_del
1027       (lm,
1028        sw_if_index,
1029        addr_fib,
1030        address_length,
1031        is_del,
1032        &if_address_index);
1033     if (error)
1034       goto done;
1035
1036     /* Pool did not grow: add duplicate address. */
1037     if (elts_before == pool_elts (lm->if_address_pool))
1038       goto done;
1039   }
1040
1041   if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1042     {
1043       if (is_del)
1044         ip6_del_interface_routes (im, ip6_af.fib_index, address,
1045                                   address_length);
1046
1047       else
1048         ip6_add_interface_routes (vnm, sw_if_index,
1049                                   im, ip6_af.fib_index,
1050                                   pool_elt_at_index (lm->if_address_pool, if_address_index));
1051     }
1052
1053   {
1054     ip6_add_del_interface_address_callback_t * cb;
1055     vec_foreach (cb, im->add_del_interface_address_callbacks)
1056       cb->function (im, cb->function_opaque, sw_if_index,
1057                     address, address_length,
1058                     if_address_index,
1059                     is_del);
1060   }
1061
1062  done:
1063   vec_free (addr_fib);
1064   return error;
1065 }
1066
1067 clib_error_t *
1068 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1069                                ip6_address_t * address, u32 address_length,
1070                                u32 is_del)
1071 {
1072   return ip6_add_del_interface_address_internal
1073     (vm, sw_if_index, address, address_length,
1074      /* redistribute */ 1,
1075      /* insert_routes */ 1,
1076      is_del);
1077 }
1078
1079 clib_error_t *
1080 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1081                                 u32 sw_if_index,
1082                                 u32 flags)
1083 {
1084   ip6_main_t * im = &ip6_main;
1085   ip_interface_address_t * ia;
1086   ip6_address_t * a;
1087   u32 is_admin_up, fib_index;
1088
1089   /* Fill in lookup tables with default table (0). */
1090   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1091
1092   vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1093
1094   is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1095
1096   fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1097
1098   foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index, 
1099                                 0 /* honor unnumbered */,
1100   ({
1101     a = ip_interface_address_get_address (&im->lookup_main, ia);
1102     if (is_admin_up)
1103       ip6_add_interface_routes (vnm, sw_if_index,
1104                                 im, fib_index,
1105                                 ia);
1106     else
1107       ip6_del_interface_routes (im, fib_index,
1108                                 a, ia->address_length);
1109   }));
1110
1111   return 0;
1112 }
1113
1114 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1115
1116 clib_error_t *
1117 ip6_sw_interface_add_del (vnet_main_t * vnm,
1118                           u32 sw_if_index,
1119                           u32 is_add)
1120 {
1121   vlib_main_t * vm = vnm->vlib_main;
1122   ip6_main_t * im = &ip6_main;
1123   ip_lookup_main_t * lm = &im->lookup_main;
1124   u32 ci, cast;
1125
1126   for (cast = 0; cast < VNET_N_CAST; cast++)
1127     {
1128       ip_config_main_t * cm = &lm->rx_config_mains[cast];
1129       vnet_config_main_t * vcm = &cm->config_main;
1130
1131       /* FIXME multicast. */
1132       if (! vcm->node_index_by_feature_index)
1133         {
1134           char * start_nodes[] = { "ip6-input", };
1135           char * feature_nodes[] = {
1136             [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1137             [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1138             [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1139             [IP6_RX_FEATURE_VPATH]  = "vpath-input-ip6",
1140             [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1141           };
1142           vnet_config_init (vm, vcm,
1143                             start_nodes, ARRAY_LEN (start_nodes),
1144                             feature_nodes, ARRAY_LEN (feature_nodes));
1145         }
1146
1147       vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1148       ci = cm->config_index_by_sw_if_index[sw_if_index];
1149
1150       if (is_add)
1151         ci = vnet_config_add_feature (vm, vcm,
1152                                       ci,
1153                                       IP6_RX_FEATURE_LOOKUP,
1154                                       /* config data */ 0,
1155                                       /* # bytes of config data */ 0);
1156       else
1157         ci = vnet_config_del_feature (vm, vcm,
1158                                       ci,
1159                                       IP6_RX_FEATURE_LOOKUP,
1160                                       /* config data */ 0,
1161                                       /* # bytes of config data */ 0);
1162
1163       cm->config_index_by_sw_if_index[sw_if_index] = ci;
1164     }
1165   return /* no error */ 0;
1166 }
1167
1168 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1169
1170 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1171   .function = ip6_lookup,
1172   .name = "ip6-lookup",
1173   .vector_size = sizeof (u32),
1174
1175   .n_next_nodes = IP_LOOKUP_N_NEXT,
1176   .next_nodes = IP6_LOOKUP_NEXT_NODES,
1177 };
1178
1179 typedef struct {
1180   /* Adjacency taken. */
1181   u32 adj_index;
1182   u32 flow_hash;
1183   u32 fib_index;
1184
1185   /* Packet data, possibly *after* rewrite. */
1186   u8 packet_data[64 - 1*sizeof(u32)];
1187 } ip6_forward_next_trace_t;
1188
1189 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1190 {
1191   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1192   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1193   ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1194   vnet_main_t * vnm = vnet_get_main();
1195   ip6_main_t * im = &ip6_main;
1196   ip_adjacency_t * adj;
1197   uword indent = format_get_indent (s);
1198
1199   adj = ip_get_adjacency (&im->lookup_main, t->adj_index);
1200   s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1201               t->fib_index, t->adj_index, format_ip_adjacency,
1202               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1203   switch (adj->lookup_next_index)
1204     {
1205     case IP_LOOKUP_NEXT_REWRITE:
1206       s = format (s, "\n%U%U",
1207                   format_white_space, indent,
1208                   format_ip_adjacency_packet_data,
1209                   vnm, &im->lookup_main, t->adj_index,
1210                   t->packet_data, sizeof (t->packet_data));
1211       break;
1212
1213     default:
1214       break;
1215     }
1216
1217   return s;
1218 }
1219
1220 /* Common trace function for all ip6-forward next nodes. */
1221 void
1222 ip6_forward_next_trace (vlib_main_t * vm,
1223                         vlib_node_runtime_t * node,
1224                         vlib_frame_t * frame,
1225                         vlib_rx_or_tx_t which_adj_index)
1226 {
1227   u32 * from, n_left;
1228   ip6_main_t * im = &ip6_main;
1229
1230   n_left = frame->n_vectors;
1231   from = vlib_frame_vector_args (frame);
1232   
1233   while (n_left >= 4)
1234     {
1235       u32 bi0, bi1;
1236       vlib_buffer_t * b0, * b1;
1237       ip6_forward_next_trace_t * t0, * t1;
1238
1239       /* Prefetch next iteration. */
1240       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1241       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1242
1243       bi0 = from[0];
1244       bi1 = from[1];
1245
1246       b0 = vlib_get_buffer (vm, bi0);
1247       b1 = vlib_get_buffer (vm, bi1);
1248
1249       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1250         {
1251           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1252           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1253           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1254           t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1255                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1256           clib_memcpy (t0->packet_data,
1257                   vlib_buffer_get_current (b0),
1258                   sizeof (t0->packet_data));
1259         }
1260       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1261         {
1262           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1263           t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1264           t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1265           t1->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1266                              vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1267           clib_memcpy (t1->packet_data,
1268                   vlib_buffer_get_current (b1),
1269                   sizeof (t1->packet_data));
1270         }
1271       from += 2;
1272       n_left -= 2;
1273     }
1274
1275   while (n_left >= 1)
1276     {
1277       u32 bi0;
1278       vlib_buffer_t * b0;
1279       ip6_forward_next_trace_t * t0;
1280
1281       bi0 = from[0];
1282
1283       b0 = vlib_get_buffer (vm, bi0);
1284
1285       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1286         {
1287           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1288           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1289           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1290           t0->fib_index = vec_elt (im->fib_index_by_sw_if_index, 
1291                              vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1292           clib_memcpy (t0->packet_data,
1293                   vlib_buffer_get_current (b0),
1294                   sizeof (t0->packet_data));
1295         }
1296       from += 1;
1297       n_left -= 1;
1298     }
1299 }
1300
1301 static uword
1302 ip6_drop_or_punt (vlib_main_t * vm,
1303                   vlib_node_runtime_t * node,
1304                   vlib_frame_t * frame,
1305                   ip6_error_t error_code)
1306 {
1307   u32 * buffers = vlib_frame_vector_args (frame);
1308   uword n_packets = frame->n_vectors;
1309
1310   vlib_error_drop_buffers (vm, node,
1311                            buffers,
1312                            /* stride */ 1,
1313                            n_packets,
1314                            /* next */ 0,
1315                            ip6_input_node.index,
1316                            error_code);
1317
1318   if (node->flags & VLIB_NODE_FLAG_TRACE)
1319     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1320
1321   return n_packets;
1322 }
1323
1324 static uword
1325 ip6_drop (vlib_main_t * vm,
1326           vlib_node_runtime_t * node,
1327           vlib_frame_t * frame)
1328 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1329
1330 static uword
1331 ip6_punt (vlib_main_t * vm,
1332           vlib_node_runtime_t * node,
1333           vlib_frame_t * frame)
1334 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1335
1336 static uword
1337 ip6_miss (vlib_main_t * vm,
1338           vlib_node_runtime_t * node,
1339           vlib_frame_t * frame)
1340 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1341
1342 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1343   .function = ip6_drop,
1344   .name = "ip6-drop",
1345   .vector_size = sizeof (u32),
1346
1347   .format_trace = format_ip6_forward_next_trace,
1348
1349   .n_next_nodes = 1,
1350   .next_nodes = {
1351     [0] = "error-drop",
1352   },
1353 };
1354
1355 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1356   .function = ip6_punt,
1357   .name = "ip6-punt",
1358   .vector_size = sizeof (u32),
1359
1360   .format_trace = format_ip6_forward_next_trace,
1361
1362   .n_next_nodes = 1,
1363   .next_nodes = {
1364     [0] = "error-punt",
1365   },
1366 };
1367
1368 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1369   .function = ip6_miss,
1370   .name = "ip6-miss",
1371   .vector_size = sizeof (u32),
1372
1373   .format_trace = format_ip6_forward_next_trace,
1374
1375   .n_next_nodes = 1,
1376   .next_nodes = {
1377     [0] = "error-drop",
1378   },
1379 };
1380
1381 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1382   .function = ip6_drop,
1383   .name = "ip6-multicast",
1384   .vector_size = sizeof (u32),
1385
1386   .format_trace = format_ip6_forward_next_trace,
1387
1388   .n_next_nodes = 1,
1389   .next_nodes = {
1390     [0] = "error-drop",
1391   },
1392 };
1393
1394 /* Compute TCP/UDP/ICMP6 checksum in software. */
1395 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1396 {
1397   ip_csum_t sum0;
1398   u16 sum16, payload_length_host_byte_order;
1399   u32 i, n_this_buffer, n_bytes_left;
1400   u32 headers_size = sizeof(ip0[0]);
1401   void * data_this_buffer;
1402
1403   ASSERT(bogus_lengthp);
1404   *bogus_lengthp = 0;
1405
1406   /* Initialize checksum with ip header. */
1407   sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1408   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1409   data_this_buffer = (void *) (ip0 + 1);
1410  
1411   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1412     {
1413       sum0 = ip_csum_with_carry (sum0,
1414                                  clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1415       sum0 = ip_csum_with_carry (sum0,
1416                                  clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1417     }
1418
1419   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1420   if (PREDICT_FALSE (ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1421     {
1422       u32  skip_bytes;
1423       ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t  *)data_this_buffer;
1424
1425       /* validate really icmp6 next */
1426       ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1427
1428       skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1429       data_this_buffer  = (void *)((u8 *)data_this_buffer + skip_bytes);
1430  
1431       payload_length_host_byte_order  -= skip_bytes;
1432       headers_size += skip_bytes;
1433    }
1434
1435   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1436 #if DPDK > 0
1437   if (p0) 
1438   {
1439     struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1440     u8 nb_segs = mb->nb_segs;
1441
1442     n_this_buffer = (p0->current_length > headers_size ?
1443                      p0->current_length - headers_size : 0);
1444     while (n_bytes_left)
1445       {
1446         sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1447         n_bytes_left -= n_this_buffer;
1448
1449         mb = mb->next;
1450         nb_segs--;
1451         if ((nb_segs == 0) || (mb == 0))
1452           break;
1453
1454         data_this_buffer = rte_ctrlmbuf_data(mb);
1455         n_this_buffer = mb->data_len;
1456       }
1457     if (n_bytes_left || nb_segs)
1458       {
1459         *bogus_lengthp = 1;
1460         return 0xfefe;
1461       }
1462   } 
1463   else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1464 #else
1465   if (p0 && n_this_buffer + headers_size  > p0->current_length)
1466     n_this_buffer = p0->current_length > headers_size  ? p0->current_length - headers_size  : 0;
1467   while (1)
1468     {
1469       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1470       n_bytes_left -= n_this_buffer;
1471       if (n_bytes_left == 0)
1472         break;
1473
1474       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1475         {
1476           *bogus_lengthp = 1;
1477           return 0xfefe;
1478         }
1479       p0 = vlib_get_buffer (vm, p0->next_buffer);
1480       data_this_buffer = vlib_buffer_get_current (p0);
1481       n_this_buffer = p0->current_length;
1482     }
1483 #endif /* DPDK */
1484
1485   sum16 = ~ ip_csum_fold (sum0);
1486
1487   return sum16;
1488 }
1489
1490 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1491 {
1492   ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1493   udp_header_t * udp0;
1494   u16 sum16;
1495   int bogus_length;
1496
1497   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1498   ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1499           || ip0->protocol == IP_PROTOCOL_ICMP6
1500           || ip0->protocol == IP_PROTOCOL_UDP
1501           || ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1502
1503   udp0 = (void *) (ip0 + 1);
1504   if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1505     {
1506       p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1507                     | IP_BUFFER_L4_CHECKSUM_CORRECT);
1508       return p0->flags;
1509     }
1510
1511   sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1512
1513   p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1514                 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1515
1516   return p0->flags;
1517 }
1518
1519 static uword
1520 ip6_local (vlib_main_t * vm,
1521            vlib_node_runtime_t * node,
1522            vlib_frame_t * frame)
1523 {
1524   ip6_main_t * im = &ip6_main;
1525   ip_lookup_main_t * lm = &im->lookup_main;
1526   ip_local_next_t next_index;
1527   u32 * from, * to_next, n_left_from, n_left_to_next;
1528   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1529
1530   from = vlib_frame_vector_args (frame);
1531   n_left_from = frame->n_vectors;
1532   next_index = node->cached_next_index;
1533   
1534   if (node->flags & VLIB_NODE_FLAG_TRACE)
1535     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1536
1537   while (n_left_from > 0)
1538     {
1539       vlib_get_next_frame (vm, node, next_index,
1540                            to_next, n_left_to_next);
1541
1542       while (n_left_from >= 4 && n_left_to_next >= 2)
1543         {
1544           vlib_buffer_t * p0, * p1;
1545           ip6_header_t * ip0, * ip1;
1546           udp_header_t * udp0, * udp1;
1547           u32 pi0, ip_len0, udp_len0, flags0, next0;
1548           u32 pi1, ip_len1, udp_len1, flags1, next1;
1549           i32 len_diff0, len_diff1;
1550           u8 error0, type0, good_l4_checksum0;
1551           u8 error1, type1, good_l4_checksum1;
1552       
1553           pi0 = to_next[0] = from[0];
1554           pi1 = to_next[1] = from[1];
1555           from += 2;
1556           n_left_from -= 2;
1557           to_next += 2;
1558           n_left_to_next -= 2;
1559       
1560           p0 = vlib_get_buffer (vm, pi0);
1561           p1 = vlib_get_buffer (vm, pi1);
1562
1563           ip0 = vlib_buffer_get_current (p0);
1564           ip1 = vlib_buffer_get_current (p1);
1565
1566           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1567           type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1568
1569           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1570           next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1571
1572           flags0 = p0->flags;
1573           flags1 = p1->flags;
1574
1575           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1576           good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1577
1578           udp0 = ip6_next_header (ip0);
1579           udp1 = ip6_next_header (ip1);
1580
1581           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1582           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1583           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1584
1585           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1586           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1587
1588           /* Verify UDP length. */
1589           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1590           ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1591           udp_len0 = clib_net_to_host_u16 (udp0->length);
1592           udp_len1 = clib_net_to_host_u16 (udp1->length);
1593
1594           len_diff0 = ip_len0 - udp_len0;
1595           len_diff1 = ip_len1 - udp_len1;
1596
1597           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1598           len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1599
1600           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1601                              && ! good_l4_checksum0
1602                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1603             {
1604               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1605               good_l4_checksum0 =
1606                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1607             }
1608           if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1609                              && ! good_l4_checksum1
1610                              && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1611             {
1612               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1613               good_l4_checksum1 =
1614                 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1615             }
1616
1617           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1618
1619           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1620           error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1621
1622           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1623           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1624           error0 = (! good_l4_checksum0
1625                     ? IP6_ERROR_UDP_CHECKSUM + type0
1626                     : error0);
1627           error1 = (! good_l4_checksum1
1628                     ? IP6_ERROR_UDP_CHECKSUM + type1
1629                     : error1);
1630
1631           /* Drop packets from unroutable hosts. */
1632           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1633           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1634             {
1635               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1636               error0 = (lm->miss_adj_index == src_adj_index0
1637                         ? IP6_ERROR_SRC_LOOKUP_MISS
1638                         : error0);
1639             }
1640           if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1641             {
1642               u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1643               error1 = (lm->miss_adj_index == src_adj_index1
1644                         ? IP6_ERROR_SRC_LOOKUP_MISS
1645                         : error1);
1646             }
1647
1648           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1649           next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1650
1651           p0->error = error_node->errors[error0];
1652           p1->error = error_node->errors[error1];
1653
1654           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1655                                            to_next, n_left_to_next,
1656                                            pi0, pi1, next0, next1);
1657         }
1658
1659       while (n_left_from > 0 && n_left_to_next > 0)
1660         {
1661           vlib_buffer_t * p0;
1662           ip6_header_t * ip0;
1663           udp_header_t * udp0;
1664           u32 pi0, ip_len0, udp_len0, flags0, next0;
1665           i32 len_diff0;
1666           u8 error0, type0, good_l4_checksum0;
1667       
1668           pi0 = to_next[0] = from[0];
1669           from += 1;
1670           n_left_from -= 1;
1671           to_next += 1;
1672           n_left_to_next -= 1;
1673       
1674           p0 = vlib_get_buffer (vm, pi0);
1675
1676           ip0 = vlib_buffer_get_current (p0);
1677
1678           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1679           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1680
1681           flags0 = p0->flags;
1682
1683           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1684
1685           udp0 = ip6_next_header (ip0);
1686
1687           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1688           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1689
1690           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1691
1692           /* Verify UDP length. */
1693           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1694           udp_len0 = clib_net_to_host_u16 (udp0->length);
1695
1696           len_diff0 = ip_len0 - udp_len0;
1697
1698           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1699
1700           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1701                              && ! good_l4_checksum0
1702                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1703             {
1704               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1705               good_l4_checksum0 =
1706                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1707             }
1708
1709           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1710
1711           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1712
1713           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1714           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1715           error0 = (! good_l4_checksum0
1716                     ? IP6_ERROR_UDP_CHECKSUM + type0
1717                     : error0);
1718
1719           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1720           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1721             {
1722               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1723               error0 = (lm->miss_adj_index == src_adj_index0
1724                         ? IP6_ERROR_SRC_LOOKUP_MISS
1725                         : error0);
1726             }
1727
1728           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1729
1730           p0->error = error_node->errors[error0];
1731
1732           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1733                                            to_next, n_left_to_next,
1734                                            pi0, next0);
1735         }
1736   
1737       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1738     }
1739
1740   return frame->n_vectors;
1741 }
1742
1743 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1744   .function = ip6_local,
1745   .name = "ip6-local",
1746   .vector_size = sizeof (u32),
1747
1748   .format_trace = format_ip6_forward_next_trace,
1749
1750   .n_next_nodes = IP_LOCAL_N_NEXT,
1751   .next_nodes = {
1752     [IP_LOCAL_NEXT_DROP] = "error-drop",
1753     [IP_LOCAL_NEXT_PUNT] = "error-punt",
1754     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1755     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1756   },
1757 };
1758
1759 void ip6_register_protocol (u32 protocol, u32 node_index)
1760 {
1761   vlib_main_t * vm = vlib_get_main();
1762   ip6_main_t * im = &ip6_main;
1763   ip_lookup_main_t * lm = &im->lookup_main;
1764
1765   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1766   lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1767 }
1768
1769 typedef enum {
1770   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1771   IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1772   IP6_DISCOVER_NEIGHBOR_N_NEXT,
1773 } ip6_discover_neighbor_next_t;
1774
1775 typedef enum {
1776   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1777   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1778 } ip6_discover_neighbor_error_t;
1779
1780 static uword
1781 ip6_discover_neighbor (vlib_main_t * vm,
1782                        vlib_node_runtime_t * node,
1783                        vlib_frame_t * frame)
1784 {
1785   vnet_main_t * vnm = vnet_get_main();
1786   ip6_main_t * im = &ip6_main;
1787   ip_lookup_main_t * lm = &im->lookup_main;
1788   u32 * from, * to_next_drop;
1789   uword n_left_from, n_left_to_next_drop;
1790   static f64 time_last_seed_change = -1e100;
1791   static u32 hash_seeds[3];
1792   static uword hash_bitmap[256 / BITS (uword)]; 
1793   f64 time_now;
1794   int bogus_length;
1795
1796   if (node->flags & VLIB_NODE_FLAG_TRACE)
1797     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1798
1799   time_now = vlib_time_now (vm);
1800   if (time_now - time_last_seed_change > 1e-3)
1801     {
1802       uword i;
1803       u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1804                                              sizeof (hash_seeds));
1805       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1806         hash_seeds[i] = r[i];
1807
1808       /* Mark all hash keys as been not-seen before. */
1809       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1810         hash_bitmap[i] = 0;
1811
1812       time_last_seed_change = time_now;
1813     }
1814
1815   from = vlib_frame_vector_args (frame);
1816   n_left_from = frame->n_vectors;
1817
1818   while (n_left_from > 0)
1819     {
1820       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1821                            to_next_drop, n_left_to_next_drop);
1822
1823       while (n_left_from > 0 && n_left_to_next_drop > 0)
1824         {
1825           vlib_buffer_t * p0;
1826           ip6_header_t * ip0;
1827           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1828           uword bm0;
1829           ip_adjacency_t * adj0;
1830           vnet_hw_interface_t * hw_if0;
1831           u32 next0;
1832
1833           pi0 = from[0];
1834
1835           p0 = vlib_get_buffer (vm, pi0);
1836
1837           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1838
1839           ip0 = vlib_buffer_get_current (p0);
1840
1841           adj0 = ip_get_adjacency (lm, adj_index0);
1842
1843           a0 = hash_seeds[0];
1844           b0 = hash_seeds[1];
1845           c0 = hash_seeds[2];
1846
1847           sw_if_index0 = adj0->rewrite_header.sw_if_index;
1848           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1849
1850           a0 ^= sw_if_index0;
1851           b0 ^= ip0->dst_address.as_u32[0];
1852           c0 ^= ip0->dst_address.as_u32[1];
1853
1854           hash_v3_mix32 (a0, b0, c0);
1855
1856           b0 ^= ip0->dst_address.as_u32[2];
1857           c0 ^= ip0->dst_address.as_u32[3];
1858
1859           hash_v3_finalize32 (a0, b0, c0);
1860
1861           c0 &= BITS (hash_bitmap) - 1;
1862           c0 = c0 / BITS (uword);
1863           m0 = (uword) 1 << (c0 % BITS (uword));
1864
1865           bm0 = hash_bitmap[c0];
1866           drop0 = (bm0 & m0) != 0;
1867
1868           /* Mark it as seen. */
1869           hash_bitmap[c0] = bm0 | m0;
1870
1871           from += 1;
1872           n_left_from -= 1;
1873           to_next_drop[0] = pi0;
1874           to_next_drop += 1;
1875           n_left_to_next_drop -= 1;
1876
1877           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1878
1879           /* If the interface is link-down, drop the pkt */
1880           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1881             drop0 = 1;
1882
1883           p0->error = 
1884             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP 
1885                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1886           if (drop0)
1887             continue;
1888
1889           {
1890             u32 bi0 = 0;
1891             icmp6_neighbor_solicitation_header_t * h0;
1892             vlib_buffer_t * b0;
1893
1894             h0 = vlib_packet_template_get_packet 
1895               (vm, &im->discover_neighbor_packet_template, &bi0);
1896
1897             /* 
1898              * Build ethernet header.
1899              * Choose source address based on destination lookup 
1900              * adjacency. 
1901              */
1902             ip6_src_address_for_packet (im, p0, &h0->ip.src_address, 
1903                                         sw_if_index0);
1904
1905             /* 
1906              * Destination address is a solicited node multicast address.  
1907              * We need to fill in
1908              * the low 24 bits with low 24 bits of target's address. 
1909              */
1910             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1911             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1912             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1913
1914             h0->neighbor.target_address = ip0->dst_address;
1915
1916             clib_memcpy (h0->link_layer_option.ethernet_address, 
1917                     hw_if0->hw_address, vec_len (hw_if0->hw_address));
1918
1919             /* $$$$ appears we need this; why is the checksum non-zero? */
1920             h0->neighbor.icmp.checksum = 0;
1921             h0->neighbor.icmp.checksum = 
1922               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip, 
1923                                                  &bogus_length);
1924
1925             ASSERT (bogus_length == 0);
1926
1927             vlib_buffer_copy_trace_flag (vm, p0, bi0);
1928             b0 = vlib_get_buffer (vm, bi0);
1929             vnet_buffer (b0)->sw_if_index[VLIB_TX] 
1930               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1931
1932             /* Add rewrite/encap string. */
1933             vnet_rewrite_one_header (adj0[0], h0, 
1934                                      sizeof (ethernet_header_t));
1935             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1936
1937             next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
1938
1939             vlib_set_next_frame_buffer (vm, node, next0, bi0);
1940           }
1941         }
1942
1943       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP, 
1944                            n_left_to_next_drop);
1945     }
1946
1947   return frame->n_vectors;
1948 }
1949
1950 static char * ip6_discover_neighbor_error_strings[] = {
1951   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1952   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] 
1953   = "neighbor solicitations sent",
1954 };
1955
1956 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1957   .function = ip6_discover_neighbor,
1958   .name = "ip6-discover-neighbor",
1959   .vector_size = sizeof (u32),
1960
1961   .format_trace = format_ip6_forward_next_trace,
1962
1963   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1964   .error_strings = ip6_discover_neighbor_error_strings,
1965
1966   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1967   .next_nodes = {
1968     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1969     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
1970   },
1971 };
1972
1973 clib_error_t *
1974 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1975 {
1976   vnet_main_t * vnm = vnet_get_main();
1977   ip6_main_t * im = &ip6_main;
1978   icmp6_neighbor_solicitation_header_t * h;
1979   ip6_address_t * src;
1980   ip_interface_address_t * ia;
1981   ip_adjacency_t * adj;
1982   vnet_hw_interface_t * hi;
1983   vnet_sw_interface_t * si;
1984   vlib_buffer_t * b;
1985   u32 bi = 0;
1986   int bogus_length;
1987
1988   si = vnet_get_sw_interface (vnm, sw_if_index);
1989
1990   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1991     {
1992       return clib_error_return (0, "%U: interface %U down",
1993                                 format_ip6_address, dst, 
1994                                 format_vnet_sw_if_index_name, vnm, 
1995                                 sw_if_index);
1996     }
1997
1998   src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
1999   if (! src)
2000     {
2001       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2002       return clib_error_return 
2003         (0, "no matching interface address for destination %U (interface %U)",
2004          format_ip6_address, dst,
2005          format_vnet_sw_if_index_name, vnm, sw_if_index);
2006     }
2007
2008   h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2009
2010   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2011
2012   /* Destination address is a solicited node multicast address.  We need to fill in
2013      the low 24 bits with low 24 bits of target's address. */
2014   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2015   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2016   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2017
2018   h->ip.src_address = src[0];
2019   h->neighbor.target_address = dst[0];
2020
2021   clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2022
2023   h->neighbor.icmp.checksum = 
2024     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2025   ASSERT(bogus_length == 0);
2026
2027   b = vlib_get_buffer (vm, bi);
2028   vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2029
2030   /* Add encapsulation string for software interface (e.g. ethernet header). */
2031   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2032   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2033   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2034
2035   {
2036     vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2037     u32 * to_next = vlib_frame_vector_args (f);
2038     to_next[0] = bi;
2039     f->n_vectors = 1;
2040     vlib_put_frame_to_node (vm, hi->output_node_index, f);
2041   }
2042
2043   return /* no error */ 0;
2044 }
2045
2046 typedef enum {
2047   IP6_REWRITE_NEXT_DROP,
2048 } ip6_rewrite_next_t;
2049
2050 always_inline uword
2051 ip6_rewrite_inline (vlib_main_t * vm,
2052                     vlib_node_runtime_t * node,
2053                     vlib_frame_t * frame,
2054                     int rewrite_for_locally_received_packets)
2055 {
2056   ip_lookup_main_t * lm = &ip6_main.lookup_main;
2057   u32 * from = vlib_frame_vector_args (frame);
2058   u32 n_left_from, n_left_to_next, * to_next, next_index;
2059   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2060   vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2061
2062   n_left_from = frame->n_vectors;
2063   next_index = node->cached_next_index;
2064   u32 cpu_index = os_get_cpu_number();
2065   
2066   while (n_left_from > 0)
2067     {
2068       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2069
2070       while (n_left_from >= 4 && n_left_to_next >= 2)
2071         {
2072           ip_adjacency_t * adj0, * adj1;
2073           vlib_buffer_t * p0, * p1;
2074           ip6_header_t * ip0, * ip1;
2075           u32 pi0, rw_len0, next0, error0, adj_index0;
2076           u32 pi1, rw_len1, next1, error1, adj_index1;
2077       
2078           /* Prefetch next iteration. */
2079           {
2080             vlib_buffer_t * p2, * p3;
2081
2082             p2 = vlib_get_buffer (vm, from[2]);
2083             p3 = vlib_get_buffer (vm, from[3]);
2084
2085             vlib_prefetch_buffer_header (p2, LOAD);
2086             vlib_prefetch_buffer_header (p3, LOAD);
2087
2088             CLIB_PREFETCH (p2->pre_data, 32, STORE);
2089             CLIB_PREFETCH (p3->pre_data, 32, STORE);
2090
2091             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2092             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2093           }
2094
2095           pi0 = to_next[0] = from[0];
2096           pi1 = to_next[1] = from[1];
2097
2098           from += 2;
2099           n_left_from -= 2;
2100           to_next += 2;
2101           n_left_to_next -= 2;
2102       
2103           p0 = vlib_get_buffer (vm, pi0);
2104           p1 = vlib_get_buffer (vm, pi1);
2105
2106           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2107           adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2108
2109           /* We should never rewrite a pkt using the MISS adjacency */
2110           ASSERT(adj_index0 && adj_index1);
2111
2112           ip0 = vlib_buffer_get_current (p0);
2113           ip1 = vlib_buffer_get_current (p1);
2114
2115           error0 = error1 = IP6_ERROR_NONE;
2116
2117           if (! rewrite_for_locally_received_packets)
2118             {
2119               i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2120
2121               /* Input node should have reject packets with hop limit 0. */
2122               ASSERT (ip0->hop_limit > 0);
2123               ASSERT (ip1->hop_limit > 0);
2124
2125               hop_limit0 -= 1;
2126               hop_limit1 -= 1;
2127
2128               ip0->hop_limit = hop_limit0;
2129               ip1->hop_limit = hop_limit1;
2130
2131               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2132               error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2133             }
2134
2135           adj0 = ip_get_adjacency (lm, adj_index0);
2136           adj1 = ip_get_adjacency (lm, adj_index1);
2137
2138           if (rewrite_for_locally_received_packets)
2139             {
2140               /*
2141                * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2142                * we end up here with a local adjacency in hand
2143                */
2144               if (PREDICT_FALSE(adj0->lookup_next_index 
2145                                 == IP_LOOKUP_NEXT_LOCAL))
2146                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2147               if (PREDICT_FALSE(adj1->lookup_next_index 
2148                                 == IP_LOOKUP_NEXT_LOCAL))
2149                 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2150             }
2151
2152           rw_len0 = adj0[0].rewrite_header.data_bytes;
2153           rw_len1 = adj1[0].rewrite_header.data_bytes;
2154
2155           vlib_increment_combined_counter (&lm->adjacency_counters,
2156                                            cpu_index, 
2157                                            adj_index0,
2158                                            /* packet increment */ 0,
2159                                            /* byte increment */ rw_len0);
2160           vlib_increment_combined_counter (&lm->adjacency_counters,
2161                                            cpu_index, 
2162                                            adj_index1,
2163                                            /* packet increment */ 0,
2164                                            /* byte increment */ rw_len1);
2165
2166           /* Check MTU of outgoing interface. */
2167           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2168                     ? IP6_ERROR_MTU_EXCEEDED
2169                     : error0);
2170           error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2171                     ? IP6_ERROR_MTU_EXCEEDED
2172                     : error1);
2173
2174           p0->current_data -= rw_len0;
2175           p1->current_data -= rw_len1;
2176
2177           p0->current_length += rw_len0;
2178           p1->current_length += rw_len1;
2179
2180           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2181           vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2182       
2183           next0 = (error0 == IP6_ERROR_NONE) ? 
2184             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2185           next1 = (error1 == IP6_ERROR_NONE) ? 
2186             adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2187
2188           /* Guess we are only writing on simple Ethernet header. */
2189           vnet_rewrite_two_headers (adj0[0], adj1[0],
2190                                     ip0, ip1,
2191                                     sizeof (ethernet_header_t));
2192       
2193           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2194                                            to_next, n_left_to_next,
2195                                            pi0, pi1, next0, next1);
2196         }
2197
2198       while (n_left_from > 0 && n_left_to_next > 0)
2199         {
2200           ip_adjacency_t * adj0;
2201           vlib_buffer_t * p0;
2202           ip6_header_t * ip0;
2203           u32 pi0, rw_len0;
2204           u32 adj_index0, next0, error0;
2205       
2206           pi0 = to_next[0] = from[0];
2207
2208           p0 = vlib_get_buffer (vm, pi0);
2209
2210           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2211
2212           /* We should never rewrite a pkt using the MISS adjacency */
2213           ASSERT(adj_index0);
2214
2215           adj0 = ip_get_adjacency (lm, adj_index0);
2216       
2217           ip0 = vlib_buffer_get_current (p0);
2218
2219           error0 = IP6_ERROR_NONE;
2220
2221           /* Check hop limit */
2222           if (! rewrite_for_locally_received_packets)
2223             {
2224               i32 hop_limit0 = ip0->hop_limit;
2225
2226               ASSERT (ip0->hop_limit > 0);
2227
2228               hop_limit0 -= 1;
2229
2230               ip0->hop_limit = hop_limit0;
2231
2232               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2233             }
2234
2235           if (rewrite_for_locally_received_packets)
2236             {
2237               if (PREDICT_FALSE(adj0->lookup_next_index 
2238                                 == IP_LOOKUP_NEXT_LOCAL))
2239                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2240             }
2241
2242           /* Guess we are only writing on simple Ethernet header. */
2243           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2244       
2245           /* Update packet buffer attributes/set output interface. */
2246           rw_len0 = adj0[0].rewrite_header.data_bytes;
2247
2248           vlib_increment_combined_counter (&lm->adjacency_counters,
2249                                            cpu_index, 
2250                                            adj_index0,
2251                                            /* packet increment */ 0,
2252                                            /* byte increment */ rw_len0);
2253
2254           /* Check MTU of outgoing interface. */
2255           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2256                     ? IP6_ERROR_MTU_EXCEEDED
2257                     : error0);
2258
2259           p0->current_data -= rw_len0;
2260           p0->current_length += rw_len0;
2261           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2262       
2263           next0 = (error0 == IP6_ERROR_NONE) ?
2264             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2265
2266           p0->error = error_node->errors[error0];
2267
2268           from += 1;
2269           n_left_from -= 1;
2270           to_next += 1;
2271           n_left_to_next -= 1;
2272       
2273           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2274                                            to_next, n_left_to_next,
2275                                            pi0, next0);
2276         }
2277
2278       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2279     }
2280
2281   /* Need to do trace after rewrites to pick up new packet data. */
2282   if (node->flags & VLIB_NODE_FLAG_TRACE)
2283     ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2284
2285   return frame->n_vectors;
2286 }
2287
2288 static uword
2289 ip6_rewrite_transit (vlib_main_t * vm,
2290                      vlib_node_runtime_t * node,
2291                      vlib_frame_t * frame)
2292 {
2293   return ip6_rewrite_inline (vm, node, frame,
2294                              /* rewrite_for_locally_received_packets */ 0);
2295 }
2296
2297 static uword
2298 ip6_rewrite_local (vlib_main_t * vm,
2299                    vlib_node_runtime_t * node,
2300                    vlib_frame_t * frame)
2301 {
2302   return ip6_rewrite_inline (vm, node, frame,
2303                              /* rewrite_for_locally_received_packets */ 1);
2304 }
2305
2306 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2307   .function = ip6_rewrite_transit,
2308   .name = "ip6-rewrite",
2309   .vector_size = sizeof (u32),
2310
2311   .format_trace = format_ip6_forward_next_trace,
2312
2313   .n_next_nodes = 1,
2314   .next_nodes = {
2315     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2316   },
2317 };
2318
2319 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2320   .function = ip6_rewrite_local,
2321   .name = "ip6-rewrite-local",
2322   .vector_size = sizeof (u32),
2323
2324   .sibling_of = "ip6-rewrite",
2325
2326   .format_trace = format_ip6_forward_next_trace,
2327
2328   .n_next_nodes = 1,
2329   .next_nodes = {
2330     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2331   },
2332 };
2333
2334 /* Global IP6 main. */
2335 ip6_main_t ip6_main;
2336
2337 static clib_error_t *
2338 ip6_lookup_init (vlib_main_t * vm)
2339 {
2340   ip6_main_t * im = &ip6_main;
2341   uword i;
2342
2343   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2344     {
2345       u32 j, i0, i1;
2346
2347       i0 = i / 32;
2348       i1 = i % 32;
2349
2350       for (j = 0; j < i0; j++)
2351         im->fib_masks[i].as_u32[j] = ~0;
2352
2353       if (i1)
2354         im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2355     }
2356
2357   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2358
2359   if (im->lookup_table_nbuckets == 0)
2360     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2361
2362   im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2363
2364   if (im->lookup_table_size == 0)
2365     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2366   
2367   BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2368                         im->lookup_table_nbuckets,
2369                         im->lookup_table_size);
2370   
2371   /* Create FIB with index 0 and table id of 0. */
2372   find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2373
2374   {
2375     pg_node_t * pn;
2376     pn = pg_get_node (ip6_lookup_node.index);
2377     pn->unformat_edit = unformat_pg_ip6_header;
2378   }
2379
2380   {
2381     icmp6_neighbor_solicitation_header_t p;
2382
2383     memset (&p, 0, sizeof (p));
2384
2385     p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2386     p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2387                                                 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2388     p.ip.protocol = IP_PROTOCOL_ICMP6;
2389     p.ip.hop_limit = 255;
2390     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2391
2392     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2393
2394     p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2395     p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2396
2397     vlib_packet_template_init (vm,
2398                                &im->discover_neighbor_packet_template,
2399                                &p, sizeof (p),
2400                                /* alloc chunk size */ 8,
2401                                "ip6 neighbor discovery");
2402   }
2403
2404   return 0;
2405 }
2406
2407 VLIB_INIT_FUNCTION (ip6_lookup_init);
2408
2409 static clib_error_t *
2410 add_del_ip6_interface_table (vlib_main_t * vm,
2411                              unformat_input_t * input,
2412                              vlib_cli_command_t * cmd)
2413 {
2414   vnet_main_t * vnm = vnet_get_main();
2415   clib_error_t * error = 0;
2416   u32 sw_if_index, table_id;
2417
2418   sw_if_index = ~0;
2419
2420   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2421     {
2422       error = clib_error_return (0, "unknown interface `%U'",
2423                                  format_unformat_error, input);
2424       goto done;
2425     }
2426
2427   if (unformat (input, "%d", &table_id))
2428     ;
2429   else
2430     {
2431       error = clib_error_return (0, "expected table id `%U'",
2432                                  format_unformat_error, input);
2433       goto done;
2434     }
2435
2436   {
2437     ip6_main_t * im = &ip6_main;
2438     ip6_fib_t * fib = 
2439       find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2440
2441     if (fib) 
2442       {
2443         vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2444         im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2445     }
2446   }
2447
2448  done:
2449   return error;
2450 }
2451
2452 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2453   .path = "set interface ip6 table",
2454   .function = add_del_ip6_interface_table,
2455   .short_help = "set interface ip6 table <intfc> <table-id>"
2456 };
2457
2458 void 
2459 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2460                                                   u8 *mac)
2461 {
2462   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2463   /* Invert the "u" bit */
2464   ip->as_u8 [8] = mac[0] ^ (1<<1);
2465   ip->as_u8 [9] = mac[1];
2466   ip->as_u8 [10] = mac[2];
2467   ip->as_u8 [11] = 0xFF;
2468   ip->as_u8 [12] = 0xFE;
2469   ip->as_u8 [13] = mac[3];
2470   ip->as_u8 [14] = mac[4];
2471   ip->as_u8 [15] = mac[5];
2472 }
2473
2474 void 
2475 ip6_ethernet_mac_address_from_link_local_address (u8 *mac, 
2476                                                   ip6_address_t *ip)
2477 {
2478   /* Invert the previously inverted "u" bit */
2479   mac[0] = ip->as_u8 [8] ^ (1<<1);
2480   mac[1] = ip->as_u8 [9];
2481   mac[2] = ip->as_u8 [10];
2482   mac[3] = ip->as_u8 [13];
2483   mac[4] = ip->as_u8 [14];
2484   mac[5] = ip->as_u8 [15];
2485 }
2486
2487 static clib_error_t * 
2488 test_ip6_link_command_fn (vlib_main_t * vm,
2489                           unformat_input_t * input,
2490                           vlib_cli_command_t * cmd)
2491 {
2492   u8 mac[6];
2493   ip6_address_t _a, *a = &_a;
2494
2495   if (unformat (input, "%U", unformat_ethernet_address, mac))
2496     {
2497       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2498       vlib_cli_output (vm, "Link local address: %U",
2499                        format_ip6_address, a);
2500       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2501       vlib_cli_output (vm, "Original MAC address: %U",
2502                        format_ethernet_address, mac);
2503     }
2504                 
2505   return 0;
2506 }
2507
2508 VLIB_CLI_COMMAND (test_link_command, static) = {
2509   .path = "test ip6 link",
2510   .function = test_ip6_link_command_fn, 
2511   .short_help = "test ip6 link <mac-address>",
2512 };
2513
2514 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2515 {
2516   ip6_main_t * im6 = &ip6_main;
2517   ip6_fib_t * fib;
2518   uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2519
2520   if (p == 0)
2521     return -1;
2522
2523   fib = vec_elt_at_index (im6->fibs, p[0]);
2524
2525   fib->flow_hash_config = flow_hash_config;
2526   return 1;
2527 }
2528
2529 static clib_error_t *
2530 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2531                               unformat_input_t * input,
2532                               vlib_cli_command_t * cmd)
2533 {
2534   int matched = 0;
2535   u32 table_id = 0;
2536   u32 flow_hash_config = 0;
2537   int rv;
2538
2539   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2540     if (unformat (input, "table %d", &table_id))
2541       matched = 1;
2542 #define _(a,v) \
2543     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2544     foreach_flow_hash_bit
2545 #undef _
2546     else break;
2547   }
2548
2549   if (matched == 0)
2550     return clib_error_return (0, "unknown input `%U'",
2551                               format_unformat_error, input);
2552   
2553   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2554   switch (rv)
2555     {
2556     case 1:
2557       break;
2558
2559     case -1:
2560       return clib_error_return (0, "no such FIB table %d", table_id);
2561       
2562     default:
2563       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2564       break;
2565     }
2566   
2567   return 0;
2568 }
2569
2570 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2571     .path = "set ip6 flow-hash",
2572     .short_help = 
2573     "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2574     .function = set_ip6_flow_hash_command_fn,
2575 };
2576
2577 static clib_error_t *
2578 show_ip6_local_command_fn (vlib_main_t * vm,
2579                            unformat_input_t * input,
2580                            vlib_cli_command_t * cmd)
2581 {
2582   ip6_main_t * im = &ip6_main;
2583   ip_lookup_main_t * lm = &im->lookup_main;
2584   int i;
2585   
2586   vlib_cli_output (vm, "Protocols handled by ip6_local");
2587   for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2588     {
2589       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2590         vlib_cli_output (vm, "%d", i);
2591     }
2592   return 0;
2593 }
2594
2595
2596
2597 VLIB_CLI_COMMAND (show_ip_local, static) = {
2598   .path = "show ip6 local",
2599   .function = show_ip6_local_command_fn,
2600   .short_help = "Show ip6 local protocol table",
2601 };
2602
2603 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
2604                                  u32 table_index)
2605 {
2606   vnet_main_t * vnm = vnet_get_main();
2607   vnet_interface_main_t * im = &vnm->interface_main;
2608   ip6_main_t * ipm = &ip6_main;
2609   ip_lookup_main_t * lm = &ipm->lookup_main;
2610   vnet_classify_main_t * cm = &vnet_classify_main;
2611
2612   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2613     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2614
2615   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2616     return VNET_API_ERROR_NO_SUCH_ENTRY;
2617
2618   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2619   lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2620
2621   return 0;
2622 }
2623
2624 static clib_error_t *
2625 set_ip6_classify_command_fn (vlib_main_t * vm,
2626                              unformat_input_t * input,
2627                              vlib_cli_command_t * cmd)
2628 {
2629   u32 table_index = ~0;
2630   int table_index_set = 0;
2631   u32 sw_if_index = ~0;
2632   int rv;
2633   
2634   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2635     if (unformat (input, "table-index %d", &table_index))
2636       table_index_set = 1;
2637     else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
2638                        vnet_get_main(), &sw_if_index))
2639         ;
2640     else
2641         break;
2642   }
2643   
2644   if (table_index_set == 0)
2645       return clib_error_return (0, "classify table-index must be specified");
2646   
2647   if (sw_if_index == ~0)
2648     return clib_error_return (0, "interface / subif must be specified");
2649
2650   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2651
2652   switch (rv)
2653     {
2654     case 0:
2655       break;
2656
2657     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2658       return clib_error_return (0, "No such interface");
2659
2660     case VNET_API_ERROR_NO_SUCH_ENTRY:
2661       return clib_error_return (0, "No such classifier table");
2662     }
2663   return 0;
2664 }
2665
2666 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2667     .path = "set ip6 classify",
2668     .short_help = 
2669     "set ip6 classify intfc <int> table-index <index>",
2670     .function = set_ip6_classify_command_fn,
2671 };
2672
2673 static clib_error_t *
2674 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2675 {
2676   ip6_main_t * im = &ip6_main;
2677   uword heapsize = 0;
2678   u32 tmp;
2679   u32 nbuckets = 0;
2680
2681   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2682     if (unformat (input, "hash-buckets %d", &tmp))
2683       nbuckets = tmp;
2684     else if (unformat (input, "heap-size %dm", &tmp))
2685       heapsize = ((u64)tmp) << 20;
2686     else if (unformat (input, "heap-size %dM", &tmp))
2687       heapsize = ((u64)tmp) << 20;
2688     else if (unformat (input, "heap-size %dg", &tmp))
2689       heapsize = ((u64)tmp) << 30;
2690     else if (unformat (input, "heap-size %dG", &tmp))
2691       heapsize = ((u64)tmp) << 30;
2692     else
2693       return clib_error_return (0, "unknown input '%U'",
2694                                 format_unformat_error, input);
2695   }
2696
2697   im->lookup_table_nbuckets = nbuckets;
2698   im->lookup_table_size = heapsize;
2699
2700   return 0;
2701 }
2702
2703 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
2704