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