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