Create macro for next_nodes used in lookup family of nodes
[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   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           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           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           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_N_NEXT,
1772 } ip6_discover_neighbor_next_t;
1773
1774 typedef enum {
1775   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1776   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1777 } ip6_discover_neighbor_error_t;
1778
1779 static uword
1780 ip6_discover_neighbor (vlib_main_t * vm,
1781                        vlib_node_runtime_t * node,
1782                        vlib_frame_t * frame)
1783 {
1784   vnet_main_t * vnm = vnet_get_main();
1785   ip6_main_t * im = &ip6_main;
1786   ip_lookup_main_t * lm = &im->lookup_main;
1787   u32 * from, * to_next_drop;
1788   uword n_left_from, n_left_to_next_drop;
1789   static f64 time_last_seed_change = -1e100;
1790   static u32 hash_seeds[3];
1791   static uword hash_bitmap[256 / BITS (uword)]; 
1792   f64 time_now;
1793   int bogus_length;
1794
1795   if (node->flags & VLIB_NODE_FLAG_TRACE)
1796     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1797
1798   time_now = vlib_time_now (vm);
1799   if (time_now - time_last_seed_change > 1e-3)
1800     {
1801       uword i;
1802       u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1803                                              sizeof (hash_seeds));
1804       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1805         hash_seeds[i] = r[i];
1806
1807       /* Mark all hash keys as been not-seen before. */
1808       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1809         hash_bitmap[i] = 0;
1810
1811       time_last_seed_change = time_now;
1812     }
1813
1814   from = vlib_frame_vector_args (frame);
1815   n_left_from = frame->n_vectors;
1816
1817   while (n_left_from > 0)
1818     {
1819       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1820                            to_next_drop, n_left_to_next_drop);
1821
1822       while (n_left_from > 0 && n_left_to_next_drop > 0)
1823         {
1824           vlib_buffer_t * p0;
1825           ip6_header_t * ip0;
1826           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1827           uword bm0;
1828           ip_adjacency_t * adj0;
1829           vnet_hw_interface_t * hw_if0;
1830           u32 next0;
1831
1832           pi0 = from[0];
1833
1834           p0 = vlib_get_buffer (vm, pi0);
1835
1836           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1837
1838           ip0 = vlib_buffer_get_current (p0);
1839
1840           adj0 = ip_get_adjacency (lm, adj_index0);
1841
1842           a0 = hash_seeds[0];
1843           b0 = hash_seeds[1];
1844           c0 = hash_seeds[2];
1845
1846           sw_if_index0 = adj0->rewrite_header.sw_if_index;
1847           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1848
1849           a0 ^= sw_if_index0;
1850           b0 ^= ip0->dst_address.as_u32[0];
1851           c0 ^= ip0->dst_address.as_u32[1];
1852
1853           hash_v3_mix32 (a0, b0, c0);
1854
1855           b0 ^= ip0->dst_address.as_u32[2];
1856           c0 ^= ip0->dst_address.as_u32[3];
1857
1858           hash_v3_finalize32 (a0, b0, c0);
1859
1860           c0 &= BITS (hash_bitmap) - 1;
1861           c0 = c0 / BITS (uword);
1862           m0 = (uword) 1 << (c0 % BITS (uword));
1863
1864           bm0 = hash_bitmap[c0];
1865           drop0 = (bm0 & m0) != 0;
1866
1867           /* Mark it as seen. */
1868           hash_bitmap[c0] = bm0 | m0;
1869
1870           from += 1;
1871           n_left_from -= 1;
1872           to_next_drop[0] = pi0;
1873           to_next_drop += 1;
1874           n_left_to_next_drop -= 1;
1875
1876           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
1877
1878           /* If the interface is link-down, drop the pkt */
1879           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
1880             drop0 = 1;
1881
1882           p0->error = 
1883             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP 
1884                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
1885           if (drop0)
1886             continue;
1887
1888           {
1889             u32 bi0 = 0;
1890             icmp6_neighbor_solicitation_header_t * h0;
1891             vlib_buffer_t * b0;
1892
1893             h0 = vlib_packet_template_get_packet 
1894               (vm, &im->discover_neighbor_packet_template, &bi0);
1895
1896             /* 
1897              * Build ethernet header.
1898              * Choose source address based on destination lookup 
1899              * adjacency. 
1900              */
1901             ip6_src_address_for_packet (im, p0, &h0->ip.src_address, 
1902                                         sw_if_index0);
1903
1904             /* 
1905              * Destination address is a solicited node multicast address.  
1906              * We need to fill in
1907              * the low 24 bits with low 24 bits of target's address. 
1908              */
1909             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
1910             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
1911             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
1912
1913             h0->neighbor.target_address = ip0->dst_address;
1914
1915             memcpy (h0->link_layer_option.ethernet_address, 
1916                     hw_if0->hw_address, vec_len (hw_if0->hw_address));
1917
1918             /* $$$$ appears we need this; why is the checksum non-zero? */
1919             h0->neighbor.icmp.checksum = 0;
1920             h0->neighbor.icmp.checksum = 
1921               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip, 
1922                                                  &bogus_length);
1923
1924             ASSERT (bogus_length == 0);
1925
1926             vlib_buffer_copy_trace_flag (vm, p0, bi0);
1927             b0 = vlib_get_buffer (vm, bi0);
1928             vnet_buffer (b0)->sw_if_index[VLIB_TX] 
1929               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
1930
1931             /* Add rewrite/encap string. */
1932             vnet_rewrite_one_header (adj0[0], h0, 
1933                                      sizeof (ethernet_header_t));
1934             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
1935
1936             /* $$$$ hack in case next0 == 0 */
1937             b0->error = node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_DROP];
1938             next0 = 
1939               vec_elt (im->discover_neighbor_next_index_by_hw_if_index, 
1940                        hw_if0->hw_if_index);
1941
1942             vlib_set_next_frame_buffer (vm, node, next0, bi0);
1943           }
1944         }
1945
1946       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP, 
1947                            n_left_to_next_drop);
1948     }
1949
1950   return frame->n_vectors;
1951 }
1952
1953 static char * ip6_discover_neighbor_error_strings[] = {
1954   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
1955   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] 
1956   = "neighbor solicitations sent",
1957 };
1958
1959 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
1960   .function = ip6_discover_neighbor,
1961   .name = "ip6-discover-neighbor",
1962   .vector_size = sizeof (u32),
1963
1964   .format_trace = format_ip6_forward_next_trace,
1965
1966   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
1967   .error_strings = ip6_discover_neighbor_error_strings,
1968
1969   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
1970   .next_nodes = {
1971     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
1972   },
1973 };
1974
1975 clib_error_t *
1976 ip6_discover_neighbor_hw_interface_link_up_down (vnet_main_t * vnm,
1977                                                  u32 hw_if_index,
1978                                                  u32 flags)
1979 {
1980   vlib_main_t * vm = vnm->vlib_main;
1981   ip6_main_t * im = &ip6_main;
1982   vnet_hw_interface_t * hw_if;
1983
1984   hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1985
1986   vec_validate_init_empty 
1987     (im->discover_neighbor_next_index_by_hw_if_index, hw_if_index, 0);
1988   im->discover_neighbor_next_index_by_hw_if_index[hw_if_index]
1989     = vlib_node_add_next (vm, ip6_discover_neighbor_node.index,
1990                           hw_if->output_node_index);
1991   return 0;
1992 }
1993
1994 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION 
1995 (ip6_discover_neighbor_hw_interface_link_up_down);
1996
1997 clib_error_t *
1998 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
1999 {
2000   vnet_main_t * vnm = vnet_get_main();
2001   ip6_main_t * im = &ip6_main;
2002   icmp6_neighbor_solicitation_header_t * h;
2003   ip6_address_t * src;
2004   ip_interface_address_t * ia;
2005   ip_adjacency_t * adj;
2006   vnet_hw_interface_t * hi;
2007   vnet_sw_interface_t * si;
2008   vlib_buffer_t * b;
2009   u32 bi = 0;
2010   int bogus_length;
2011
2012   si = vnet_get_sw_interface (vnm, sw_if_index);
2013
2014   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2015     {
2016       return clib_error_return (0, "%U: interface %U down",
2017                                 format_ip6_address, dst, 
2018                                 format_vnet_sw_if_index_name, vnm, 
2019                                 sw_if_index);
2020     }
2021
2022   src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2023   if (! src)
2024     {
2025       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2026       return clib_error_return 
2027         (0, "no matching interface address for destination %U (interface %U)",
2028          format_ip6_address, dst,
2029          format_vnet_sw_if_index_name, vnm, sw_if_index);
2030     }
2031
2032   h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2033
2034   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2035
2036   /* Destination address is a solicited node multicast address.  We need to fill in
2037      the low 24 bits with low 24 bits of target's address. */
2038   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2039   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2040   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2041
2042   h->ip.src_address = src[0];
2043   h->neighbor.target_address = dst[0];
2044
2045   memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2046
2047   h->neighbor.icmp.checksum = 
2048     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2049   ASSERT(bogus_length == 0);
2050
2051   b = vlib_get_buffer (vm, bi);
2052   vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2053
2054   /* Add encapsulation string for software interface (e.g. ethernet header). */
2055   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2056   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2057   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2058
2059   {
2060     vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2061     u32 * to_next = vlib_frame_vector_args (f);
2062     to_next[0] = bi;
2063     f->n_vectors = 1;
2064     vlib_put_frame_to_node (vm, hi->output_node_index, f);
2065   }
2066
2067   return /* no error */ 0;
2068 }
2069
2070 typedef enum {
2071   IP6_REWRITE_NEXT_DROP,
2072 } ip6_rewrite_next_t;
2073
2074 always_inline uword
2075 ip6_rewrite_inline (vlib_main_t * vm,
2076                     vlib_node_runtime_t * node,
2077                     vlib_frame_t * frame,
2078                     int rewrite_for_locally_received_packets)
2079 {
2080   ip_lookup_main_t * lm = &ip6_main.lookup_main;
2081   u32 * from = vlib_frame_vector_args (frame);
2082   u32 n_left_from, n_left_to_next, * to_next, next_index;
2083   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2084   vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2085
2086   n_left_from = frame->n_vectors;
2087   next_index = node->cached_next_index;
2088   u32 cpu_index = os_get_cpu_number();
2089   
2090   while (n_left_from > 0)
2091     {
2092       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2093
2094       while (n_left_from >= 4 && n_left_to_next >= 2)
2095         {
2096           ip_adjacency_t * adj0, * adj1;
2097           vlib_buffer_t * p0, * p1;
2098           ip6_header_t * ip0, * ip1;
2099           u32 pi0, rw_len0, next0, error0, adj_index0;
2100           u32 pi1, rw_len1, next1, error1, adj_index1;
2101       
2102           /* Prefetch next iteration. */
2103           {
2104             vlib_buffer_t * p2, * p3;
2105
2106             p2 = vlib_get_buffer (vm, from[2]);
2107             p3 = vlib_get_buffer (vm, from[3]);
2108
2109             vlib_prefetch_buffer_header (p2, LOAD);
2110             vlib_prefetch_buffer_header (p3, LOAD);
2111
2112             CLIB_PREFETCH (p2->pre_data, 32, STORE);
2113             CLIB_PREFETCH (p3->pre_data, 32, STORE);
2114
2115             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2116             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2117           }
2118
2119           pi0 = to_next[0] = from[0];
2120           pi1 = to_next[1] = from[1];
2121
2122           from += 2;
2123           n_left_from -= 2;
2124           to_next += 2;
2125           n_left_to_next -= 2;
2126       
2127           p0 = vlib_get_buffer (vm, pi0);
2128           p1 = vlib_get_buffer (vm, pi1);
2129
2130           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2131           adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2132
2133           /* We should never rewrite a pkt using the MISS adjacency */
2134           ASSERT(adj_index0 && adj_index1);
2135
2136           ip0 = vlib_buffer_get_current (p0);
2137           ip1 = vlib_buffer_get_current (p1);
2138
2139           error0 = error1 = IP6_ERROR_NONE;
2140
2141           if (! rewrite_for_locally_received_packets)
2142             {
2143               i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2144
2145               /* Input node should have reject packets with hop limit 0. */
2146               ASSERT (ip0->hop_limit > 0);
2147               ASSERT (ip1->hop_limit > 0);
2148
2149               hop_limit0 -= 1;
2150               hop_limit1 -= 1;
2151
2152               ip0->hop_limit = hop_limit0;
2153               ip1->hop_limit = hop_limit1;
2154
2155               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2156               error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2157             }
2158
2159           adj0 = ip_get_adjacency (lm, adj_index0);
2160           adj1 = ip_get_adjacency (lm, adj_index1);
2161
2162           if (rewrite_for_locally_received_packets)
2163             {
2164               /*
2165                * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2166                * we end up here with a local adjacency in hand
2167                */
2168               if (PREDICT_FALSE(adj0->lookup_next_index 
2169                                 == IP_LOOKUP_NEXT_LOCAL))
2170                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2171               if (PREDICT_FALSE(adj1->lookup_next_index 
2172                                 == IP_LOOKUP_NEXT_LOCAL))
2173                 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2174             }
2175
2176           rw_len0 = adj0[0].rewrite_header.data_bytes;
2177           rw_len1 = adj1[0].rewrite_header.data_bytes;
2178
2179           vlib_increment_combined_counter (&lm->adjacency_counters,
2180                                            cpu_index, 
2181                                            adj_index0,
2182                                            /* packet increment */ 0,
2183                                            /* byte increment */ rw_len0);
2184           vlib_increment_combined_counter (&lm->adjacency_counters,
2185                                            cpu_index, 
2186                                            adj_index1,
2187                                            /* packet increment */ 0,
2188                                            /* byte increment */ rw_len1);
2189
2190           /* Check MTU of outgoing interface. */
2191           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2192                     ? IP6_ERROR_MTU_EXCEEDED
2193                     : error0);
2194           error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2195                     ? IP6_ERROR_MTU_EXCEEDED
2196                     : error1);
2197
2198           p0->current_data -= rw_len0;
2199           p1->current_data -= rw_len1;
2200
2201           p0->current_length += rw_len0;
2202           p1->current_length += rw_len1;
2203
2204           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2205           vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2206       
2207           next0 = (error0 == IP6_ERROR_NONE) ? 
2208             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2209           next1 = (error1 == IP6_ERROR_NONE) ? 
2210             adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2211
2212           /* Guess we are only writing on simple Ethernet header. */
2213           vnet_rewrite_two_headers (adj0[0], adj1[0],
2214                                     ip0, ip1,
2215                                     sizeof (ethernet_header_t));
2216       
2217           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2218                                            to_next, n_left_to_next,
2219                                            pi0, pi1, next0, next1);
2220         }
2221
2222       while (n_left_from > 0 && n_left_to_next > 0)
2223         {
2224           ip_adjacency_t * adj0;
2225           vlib_buffer_t * p0;
2226           ip6_header_t * ip0;
2227           u32 pi0, rw_len0;
2228           u32 adj_index0, next0, error0;
2229       
2230           pi0 = to_next[0] = from[0];
2231
2232           p0 = vlib_get_buffer (vm, pi0);
2233
2234           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2235
2236           /* We should never rewrite a pkt using the MISS adjacency */
2237           ASSERT(adj_index0);
2238
2239           adj0 = ip_get_adjacency (lm, adj_index0);
2240       
2241           ip0 = vlib_buffer_get_current (p0);
2242
2243           error0 = IP6_ERROR_NONE;
2244
2245           /* Check hop limit */
2246           if (! rewrite_for_locally_received_packets)
2247             {
2248               i32 hop_limit0 = ip0->hop_limit;
2249
2250               ASSERT (ip0->hop_limit > 0);
2251
2252               hop_limit0 -= 1;
2253
2254               ip0->hop_limit = hop_limit0;
2255
2256               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2257             }
2258
2259           if (rewrite_for_locally_received_packets)
2260             {
2261               if (PREDICT_FALSE(adj0->lookup_next_index 
2262                                 == IP_LOOKUP_NEXT_LOCAL))
2263                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2264             }
2265
2266           /* Guess we are only writing on simple Ethernet header. */
2267           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2268       
2269           /* Update packet buffer attributes/set output interface. */
2270           rw_len0 = adj0[0].rewrite_header.data_bytes;
2271
2272           vlib_increment_combined_counter (&lm->adjacency_counters,
2273                                            cpu_index, 
2274                                            adj_index0,
2275                                            /* packet increment */ 0,
2276                                            /* byte increment */ rw_len0);
2277
2278           /* Check MTU of outgoing interface. */
2279           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2280                     ? IP6_ERROR_MTU_EXCEEDED
2281                     : error0);
2282
2283           p0->current_data -= rw_len0;
2284           p0->current_length += rw_len0;
2285           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2286       
2287           next0 = (error0 == IP6_ERROR_NONE) ?
2288             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2289
2290           p0->error = error_node->errors[error0];
2291
2292           from += 1;
2293           n_left_from -= 1;
2294           to_next += 1;
2295           n_left_to_next -= 1;
2296       
2297           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2298                                            to_next, n_left_to_next,
2299                                            pi0, next0);
2300         }
2301
2302       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2303     }
2304
2305   /* Need to do trace after rewrites to pick up new packet data. */
2306   if (node->flags & VLIB_NODE_FLAG_TRACE)
2307     ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2308
2309   return frame->n_vectors;
2310 }
2311
2312 static uword
2313 ip6_rewrite_transit (vlib_main_t * vm,
2314                      vlib_node_runtime_t * node,
2315                      vlib_frame_t * frame)
2316 {
2317   return ip6_rewrite_inline (vm, node, frame,
2318                              /* rewrite_for_locally_received_packets */ 0);
2319 }
2320
2321 static uword
2322 ip6_rewrite_local (vlib_main_t * vm,
2323                    vlib_node_runtime_t * node,
2324                    vlib_frame_t * frame)
2325 {
2326   return ip6_rewrite_inline (vm, node, frame,
2327                              /* rewrite_for_locally_received_packets */ 1);
2328 }
2329
2330 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2331   .function = ip6_rewrite_transit,
2332   .name = "ip6-rewrite",
2333   .vector_size = sizeof (u32),
2334
2335   .format_trace = format_ip6_forward_next_trace,
2336
2337   .n_next_nodes = 1,
2338   .next_nodes = {
2339     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2340   },
2341 };
2342
2343 VLIB_REGISTER_NODE (ip6_rewrite_local_node,static) = {
2344   .function = ip6_rewrite_local,
2345   .name = "ip6-rewrite-local",
2346   .vector_size = sizeof (u32),
2347
2348   .sibling_of = "ip6-rewrite",
2349
2350   .format_trace = format_ip6_forward_next_trace,
2351
2352   .n_next_nodes = 1,
2353   .next_nodes = {
2354     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2355   },
2356 };
2357
2358 /* Global IP6 main. */
2359 ip6_main_t ip6_main;
2360
2361 static clib_error_t *
2362 ip6_lookup_init (vlib_main_t * vm)
2363 {
2364   ip6_main_t * im = &ip6_main;
2365   uword i;
2366
2367   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2368     {
2369       u32 j, i0, i1;
2370
2371       i0 = i / 32;
2372       i1 = i % 32;
2373
2374       for (j = 0; j < i0; j++)
2375         im->fib_masks[i].as_u32[j] = ~0;
2376
2377       if (i1)
2378         im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2379     }
2380
2381   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2382
2383   if (im->lookup_table_nbuckets == 0)
2384     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2385
2386   im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2387
2388   if (im->lookup_table_size == 0)
2389     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2390   
2391   BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2392                         im->lookup_table_nbuckets,
2393                         im->lookup_table_size);
2394   
2395   /* Create FIB with index 0 and table id of 0. */
2396   find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2397
2398   {
2399     pg_node_t * pn;
2400     pn = pg_get_node (ip6_lookup_node.index);
2401     pn->unformat_edit = unformat_pg_ip6_header;
2402   }
2403
2404   {
2405     icmp6_neighbor_solicitation_header_t p;
2406
2407     memset (&p, 0, sizeof (p));
2408
2409     p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2410     p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2411                                                 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2412     p.ip.protocol = IP_PROTOCOL_ICMP6;
2413     p.ip.hop_limit = 255;
2414     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2415
2416     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2417
2418     p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2419     p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2420
2421     vlib_packet_template_init (vm,
2422                                &im->discover_neighbor_packet_template,
2423                                &p, sizeof (p),
2424                                /* alloc chunk size */ 8,
2425                                "ip6 neighbor discovery");
2426   }
2427
2428   return 0;
2429 }
2430
2431 VLIB_INIT_FUNCTION (ip6_lookup_init);
2432
2433 static clib_error_t *
2434 add_del_ip6_interface_table (vlib_main_t * vm,
2435                              unformat_input_t * input,
2436                              vlib_cli_command_t * cmd)
2437 {
2438   vnet_main_t * vnm = vnet_get_main();
2439   clib_error_t * error = 0;
2440   u32 sw_if_index, table_id;
2441
2442   sw_if_index = ~0;
2443
2444   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2445     {
2446       error = clib_error_return (0, "unknown interface `%U'",
2447                                  format_unformat_error, input);
2448       goto done;
2449     }
2450
2451   if (unformat (input, "%d", &table_id))
2452     ;
2453   else
2454     {
2455       error = clib_error_return (0, "expected table id `%U'",
2456                                  format_unformat_error, input);
2457       goto done;
2458     }
2459
2460   {
2461     ip6_main_t * im = &ip6_main;
2462     ip6_fib_t * fib = 
2463       find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2464
2465     if (fib) 
2466       {
2467         vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2468         im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2469     }
2470   }
2471
2472  done:
2473   return error;
2474 }
2475
2476 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2477   .path = "set interface ip6 table",
2478   .function = add_del_ip6_interface_table,
2479   .short_help = "set interface ip6 table <intfc> <table-id>"
2480 };
2481
2482 void 
2483 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2484                                                   u8 *mac)
2485 {
2486   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2487   /* Invert the "u" bit */
2488   ip->as_u8 [8] = mac[0] ^ (1<<1);
2489   ip->as_u8 [9] = mac[1];
2490   ip->as_u8 [10] = mac[2];
2491   ip->as_u8 [11] = 0xFF;
2492   ip->as_u8 [12] = 0xFE;
2493   ip->as_u8 [13] = mac[3];
2494   ip->as_u8 [14] = mac[4];
2495   ip->as_u8 [15] = mac[5];
2496 }
2497
2498 void 
2499 ip6_ethernet_mac_address_from_link_local_address (u8 *mac, 
2500                                                   ip6_address_t *ip)
2501 {
2502   /* Invert the previously inverted "u" bit */
2503   mac[0] = ip->as_u8 [8] ^ (1<<1);
2504   mac[1] = ip->as_u8 [9];
2505   mac[2] = ip->as_u8 [10];
2506   mac[3] = ip->as_u8 [13];
2507   mac[4] = ip->as_u8 [14];
2508   mac[5] = ip->as_u8 [15];
2509 }
2510
2511 static clib_error_t * 
2512 test_ip6_link_command_fn (vlib_main_t * vm,
2513                           unformat_input_t * input,
2514                           vlib_cli_command_t * cmd)
2515 {
2516   u8 mac[6];
2517   ip6_address_t _a, *a = &_a;
2518
2519   if (unformat (input, "%U", unformat_ethernet_address, mac))
2520     {
2521       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2522       vlib_cli_output (vm, "Link local address: %U",
2523                        format_ip6_address, a);
2524       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2525       vlib_cli_output (vm, "Original MAC address: %U",
2526                        format_ethernet_address, mac);
2527     }
2528                 
2529   return 0;
2530 }
2531
2532 VLIB_CLI_COMMAND (test_link_command, static) = {
2533   .path = "test ip6 link",
2534   .function = test_ip6_link_command_fn, 
2535   .short_help = "test ip6 link <mac-address>",
2536 };
2537
2538 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2539 {
2540   ip6_main_t * im6 = &ip6_main;
2541   ip6_fib_t * fib;
2542   uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2543
2544   if (p == 0)
2545     return -1;
2546
2547   fib = vec_elt_at_index (im6->fibs, p[0]);
2548
2549   fib->flow_hash_config = flow_hash_config;
2550   return 1;
2551 }
2552
2553 static clib_error_t *
2554 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2555                               unformat_input_t * input,
2556                               vlib_cli_command_t * cmd)
2557 {
2558   int matched = 0;
2559   u32 table_id = 0;
2560   u32 flow_hash_config = 0;
2561   int rv;
2562
2563   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2564     if (unformat (input, "table %d", &table_id))
2565       matched = 1;
2566 #define _(a,v) \
2567     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
2568     foreach_flow_hash_bit
2569 #undef _
2570     else break;
2571   }
2572
2573   if (matched == 0)
2574     return clib_error_return (0, "unknown input `%U'",
2575                               format_unformat_error, input);
2576   
2577   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
2578   switch (rv)
2579     {
2580     case 1:
2581       break;
2582
2583     case -1:
2584       return clib_error_return (0, "no such FIB table %d", table_id);
2585       
2586     default:
2587       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
2588       break;
2589     }
2590   
2591   return 0;
2592 }
2593
2594 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
2595     .path = "set ip6 flow-hash",
2596     .short_help = 
2597     "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
2598     .function = set_ip6_flow_hash_command_fn,
2599 };
2600
2601 static clib_error_t *
2602 show_ip6_local_command_fn (vlib_main_t * vm,
2603                            unformat_input_t * input,
2604                            vlib_cli_command_t * cmd)
2605 {
2606   ip6_main_t * im = &ip6_main;
2607   ip_lookup_main_t * lm = &im->lookup_main;
2608   int i;
2609   
2610   vlib_cli_output (vm, "Protocols handled by ip6_local");
2611   for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
2612     {
2613       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
2614         vlib_cli_output (vm, "%d", i);
2615     }
2616   return 0;
2617 }
2618
2619
2620
2621 VLIB_CLI_COMMAND (show_ip_local, static) = {
2622   .path = "show ip6 local",
2623   .function = show_ip6_local_command_fn,
2624   .short_help = "Show ip6 local protocol table",
2625 };
2626
2627 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
2628                                  u32 table_index)
2629 {
2630   vnet_main_t * vnm = vnet_get_main();
2631   vnet_interface_main_t * im = &vnm->interface_main;
2632   ip6_main_t * ipm = &ip6_main;
2633   ip_lookup_main_t * lm = &ipm->lookup_main;
2634   vnet_classify_main_t * cm = &vnet_classify_main;
2635
2636   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
2637     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
2638
2639   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
2640     return VNET_API_ERROR_NO_SUCH_ENTRY;
2641
2642   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
2643   lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
2644
2645   return 0;
2646 }
2647
2648 static clib_error_t *
2649 set_ip6_classify_command_fn (vlib_main_t * vm,
2650                              unformat_input_t * input,
2651                              vlib_cli_command_t * cmd)
2652 {
2653   u32 table_index = ~0;
2654   int table_index_set = 0;
2655   u32 sw_if_index = ~0;
2656   int rv;
2657   
2658   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2659     if (unformat (input, "table-index %d", &table_index))
2660       table_index_set = 1;
2661     else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
2662                        vnet_get_main(), &sw_if_index))
2663         ;
2664     else
2665         break;
2666   }
2667   
2668   if (table_index_set == 0)
2669       return clib_error_return (0, "classify table-index must be specified");
2670   
2671   if (sw_if_index == ~0)
2672     return clib_error_return (0, "interface / subif must be specified");
2673
2674   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
2675
2676   switch (rv)
2677     {
2678     case 0:
2679       break;
2680
2681     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
2682       return clib_error_return (0, "No such interface");
2683
2684     case VNET_API_ERROR_NO_SUCH_ENTRY:
2685       return clib_error_return (0, "No such classifier table");
2686     }
2687   return 0;
2688 }
2689
2690 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
2691     .path = "set ip6 classify",
2692     .short_help = 
2693     "set ip6 classify intfc <int> table-index <index>",
2694     .function = set_ip6_classify_command_fn,
2695 };
2696
2697 static clib_error_t *
2698 ip6_config (vlib_main_t * vm, unformat_input_t * input)
2699 {
2700   ip6_main_t * im = &ip6_main;
2701   uword heapsize = 0;
2702   u32 tmp;
2703   u32 nbuckets = 0;
2704
2705   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2706     if (unformat (input, "hash-buckets %d", &tmp))
2707       nbuckets = tmp;
2708     else if (unformat (input, "heap-size %dm", &tmp))
2709       heapsize = ((u64)tmp) << 20;
2710     else if (unformat (input, "heap-size %dM", &tmp))
2711       heapsize = ((u64)tmp) << 20;
2712     else if (unformat (input, "heap-size %dg", &tmp))
2713       heapsize = ((u64)tmp) << 30;
2714     else if (unformat (input, "heap-size %dG", &tmp))
2715       heapsize = ((u64)tmp) << 30;
2716     else
2717       return clib_error_return (0, "unknown input '%U'",
2718                                 format_unformat_error, input);
2719   }
2720
2721   im->lookup_table_nbuckets = nbuckets;
2722   im->lookup_table_size = heapsize;
2723
2724   return 0;
2725 }
2726
2727 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
2728