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