VPP-117: Fix ip4 and ip6 lookup and rewrite traces
[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 void
657 ip6_forward_next_trace (vlib_main_t * vm,
658                         vlib_node_runtime_t * node,
659                         vlib_frame_t * frame,
660                         vlib_rx_or_tx_t which_adj_index);
661
662 always_inline uword
663 ip6_lookup_inline (vlib_main_t * vm,
664                    vlib_node_runtime_t * node,
665                    vlib_frame_t * frame,
666                    int is_indirect)
667 {
668   ip6_main_t * im = &ip6_main;
669   ip_lookup_main_t * lm = &im->lookup_main;
670   vlib_combined_counter_main_t * cm = &im->lookup_main.adjacency_counters;
671   u32 n_left_from, n_left_to_next, * from, * to_next;
672   ip_lookup_next_t next;
673   u32 cpu_index = os_get_cpu_number();
674
675   from = vlib_frame_vector_args (frame);
676   n_left_from = frame->n_vectors;
677   next = node->cached_next_index;
678
679   while (n_left_from > 0)
680     {
681       vlib_get_next_frame (vm, node, next,
682                            to_next, n_left_to_next);
683
684       while (n_left_from >= 4 && n_left_to_next >= 2)
685         {
686           vlib_buffer_t * p0, * p1;
687           u32 pi0, pi1, adj_index0, adj_index1, wrong_next;
688           ip_lookup_next_t next0, next1;
689           ip6_header_t * ip0, * ip1;
690           ip_adjacency_t * adj0, * adj1;
691           ip6_address_t * dst_addr0, * dst_addr1;
692           u32 fib_index0, fib_index1;
693           u32 flow_hash_config0, flow_hash_config1;
694
695           /* Prefetch next iteration. */
696           {
697             vlib_buffer_t * p2, * p3;
698
699             p2 = vlib_get_buffer (vm, from[2]);
700             p3 = vlib_get_buffer (vm, from[3]);
701
702             vlib_prefetch_buffer_header (p2, LOAD);
703             vlib_prefetch_buffer_header (p3, LOAD);
704             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
705             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), LOAD);
706           }
707
708           pi0 = to_next[0] = from[0];
709           pi1 = to_next[1] = from[1];
710
711           p0 = vlib_get_buffer (vm, pi0);
712           p1 = vlib_get_buffer (vm, pi1);
713
714           ip0 = vlib_buffer_get_current (p0);
715           ip1 = vlib_buffer_get_current (p1);
716
717           if (is_indirect)
718             {
719               ip_adjacency_t * iadj0, * iadj1;
720               iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
721               iadj1 = ip_get_adjacency (lm, vnet_buffer(p1)->ip.adj_index[VLIB_TX]);
722               dst_addr0 = &iadj0->indirect.next_hop.ip6;
723               dst_addr1 = &iadj1->indirect.next_hop.ip6;
724             }
725           else
726             {
727               dst_addr0 = &ip0->dst_address;
728               dst_addr1 = &ip1->dst_address;
729             }
730
731           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
732           fib_index1 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p1)->sw_if_index[VLIB_RX]);
733
734           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
735             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
736           fib_index1 = (vnet_buffer(p1)->sw_if_index[VLIB_TX] == (u32)~0) ?
737             fib_index1 : vnet_buffer(p1)->sw_if_index[VLIB_TX];
738
739           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
740           adj_index1 = ip6_fib_lookup_with_table (im, fib_index1, dst_addr1);
741
742           adj0 = ip_get_adjacency (lm, adj_index0);
743           adj1 = ip_get_adjacency (lm, adj_index1);
744
745           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
746             {
747               adj_index0 = ip6_fib_lookup_with_table 
748                 (im, adj0->explicit_fib_index, dst_addr0);
749               adj0 = ip_get_adjacency (lm, adj_index0);
750             }
751           if (PREDICT_FALSE (adj1->explicit_fib_index != ~0))
752             {
753               adj_index1 = ip6_fib_lookup_with_table 
754                 (im, adj1->explicit_fib_index, dst_addr1);
755               adj1 = ip_get_adjacency (lm, adj_index1);
756             }
757
758           next0 = adj0->lookup_next_index;
759           next1 = adj1->lookup_next_index;
760
761           /* Only process the HBH Option Header if explicitly configured to do so */
762           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
763             adj_index0 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
764           next1 = (ip1->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
765             adj_index1 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj1->lookup_next_index;
766
767           vnet_buffer (p0)->ip.flow_hash = 
768             vnet_buffer(p1)->ip.flow_hash = 0;
769
770           if (PREDICT_FALSE(adj0->n_adj > 1))
771             {
772               flow_hash_config0 = 
773                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
774               vnet_buffer (p0)->ip.flow_hash = 
775                 ip6_compute_flow_hash (ip0, flow_hash_config0);
776             }
777
778           if (PREDICT_FALSE(adj1->n_adj > 1))
779             {
780               flow_hash_config1 = 
781                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
782
783               vnet_buffer (p1)->ip.flow_hash = 
784                 ip6_compute_flow_hash (ip1, flow_hash_config1);
785             }
786
787           ASSERT (adj0->n_adj > 0);
788           ASSERT (adj1->n_adj > 0);
789           ASSERT (is_pow2 (adj0->n_adj));
790           ASSERT (is_pow2 (adj1->n_adj));
791           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
792           adj_index1 += (vnet_buffer (p1)->ip.flow_hash & (adj1->n_adj - 1));
793
794           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
795           vnet_buffer (p1)->ip.adj_index[VLIB_TX] = adj_index1;
796
797           vlib_increment_combined_counter 
798               (cm, cpu_index, adj_index0, 1,
799                vlib_buffer_length_in_chain (vm, p0));
800           vlib_increment_combined_counter 
801               (cm, cpu_index, adj_index1, 1,
802                vlib_buffer_length_in_chain (vm, p1));
803
804           from += 2;
805           to_next += 2;
806           n_left_to_next -= 2;
807           n_left_from -= 2;
808
809           wrong_next = (next0 != next) + 2*(next1 != next);
810           if (PREDICT_FALSE (wrong_next != 0))
811             {
812               switch (wrong_next)
813                 {
814                 case 1:
815                   /* A B A */
816                   to_next[-2] = pi1;
817                   to_next -= 1;
818                   n_left_to_next += 1;
819                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
820                   break;
821
822                 case 2:
823                   /* A A B */
824                   to_next -= 1;
825                   n_left_to_next += 1;
826                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
827                   break;
828
829                 case 3:
830                   /* A B C */
831                   to_next -= 2;
832                   n_left_to_next += 2;
833                   vlib_set_next_frame_buffer (vm, node, next0, pi0);
834                   vlib_set_next_frame_buffer (vm, node, next1, pi1);
835                   if (next0 == next1)
836                     {
837                       /* A B B */
838                       vlib_put_next_frame (vm, node, next, n_left_to_next);
839                       next = next1;
840                       vlib_get_next_frame (vm, node, next, to_next, n_left_to_next);
841                     }
842                 }
843             }
844         }
845     
846       while (n_left_from > 0 && n_left_to_next > 0)
847         {
848           vlib_buffer_t * p0;
849           ip6_header_t * ip0;
850           u32 pi0, adj_index0;
851           ip_lookup_next_t next0;
852           ip_adjacency_t * adj0;
853           ip6_address_t * dst_addr0;
854           u32 fib_index0, flow_hash_config0;
855
856           pi0 = from[0];
857           to_next[0] = pi0;
858
859           p0 = vlib_get_buffer (vm, pi0);
860
861           ip0 = vlib_buffer_get_current (p0);
862
863           if (is_indirect)
864             {
865               ip_adjacency_t * iadj0;
866               iadj0 = ip_get_adjacency (lm, vnet_buffer(p0)->ip.adj_index[VLIB_TX]);
867               dst_addr0 = &iadj0->indirect.next_hop.ip6;
868             }
869           else
870             {
871               dst_addr0 = &ip0->dst_address;
872             }
873
874           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, vnet_buffer (p0)->sw_if_index[VLIB_RX]);
875           fib_index0 = (vnet_buffer(p0)->sw_if_index[VLIB_TX] == (u32)~0) ?
876             fib_index0 : vnet_buffer(p0)->sw_if_index[VLIB_TX];
877
878           flow_hash_config0 = 
879               vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
880
881           adj_index0 = ip6_fib_lookup_with_table (im, fib_index0, dst_addr0);
882
883           adj0 = ip_get_adjacency (lm, adj_index0);
884
885           if (PREDICT_FALSE (adj0->explicit_fib_index != ~0))
886             {
887               adj_index0 = ip6_fib_lookup_with_table
888                 (im, adj0->explicit_fib_index, dst_addr0);
889               adj0 = ip_get_adjacency (lm, adj_index0);
890             }
891
892           /* Only process the HBH Option Header if explicitly configured to do so */
893           next0 = (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS) && im->hbh_enabled &&
894             adj_index0 ? IP_LOOKUP_NEXT_HOP_BY_HOP : adj0->lookup_next_index;
895
896           vnet_buffer (p0)->ip.flow_hash = 0;
897
898           if (PREDICT_FALSE(adj0->n_adj > 1))
899             {
900               flow_hash_config0 = 
901                 vec_elt_at_index (im->fibs,fib_index0)->flow_hash_config;
902               vnet_buffer (p0)->ip.flow_hash = 
903                 ip6_compute_flow_hash (ip0, flow_hash_config0);
904             }
905
906           ASSERT (adj0->n_adj > 0);
907           ASSERT (is_pow2 (adj0->n_adj));
908           adj_index0 += (vnet_buffer (p0)->ip.flow_hash & (adj0->n_adj - 1));
909
910           vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
911
912           vlib_increment_combined_counter 
913               (cm, cpu_index, adj_index0, 1,
914                vlib_buffer_length_in_chain (vm, p0));
915
916           from += 1;
917           to_next += 1;
918           n_left_to_next -= 1;
919           n_left_from -= 1;
920
921           if (PREDICT_FALSE (next0 != next))
922             {
923               n_left_to_next += 1;
924               vlib_put_next_frame (vm, node, next, n_left_to_next);
925               next = next0;
926               vlib_get_next_frame (vm, node, next,
927                                    to_next, n_left_to_next);
928               to_next[0] = pi0;
929               to_next += 1;
930               n_left_to_next -= 1;
931             }
932         }
933
934       vlib_put_next_frame (vm, node, next, n_left_to_next);
935     }
936
937   if (node->flags & VLIB_NODE_FLAG_TRACE)
938       ip6_forward_next_trace(vm, node, frame, VLIB_TX);
939
940   return frame->n_vectors;
941 }
942
943 void ip6_adjacency_set_interface_route (vnet_main_t * vnm,
944                                         ip_adjacency_t * adj,
945                                         u32 sw_if_index,
946                                         u32 if_address_index)
947 {
948   vnet_hw_interface_t * hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
949   ip_lookup_next_t n;
950   u32 node_index;
951
952   if (hw->hw_class_index == ethernet_hw_interface_class.index
953       || hw->hw_class_index == srp_hw_interface_class.index)
954     {
955       n = IP_LOOKUP_NEXT_ARP;
956       node_index = ip6_discover_neighbor_node.index;
957       adj->if_address_index = if_address_index;
958       adj->arp.next_hop.ip6.as_u64[0] = 0;
959       adj->arp.next_hop.ip6.as_u64[1] = 0;
960   }
961   else
962     {
963       n = IP_LOOKUP_NEXT_REWRITE;
964       node_index = ip6_rewrite_node.index;
965     }
966
967  adj->lookup_next_index = n;
968  adj->explicit_fib_index = ~0;
969
970  vnet_rewrite_for_sw_interface
971    (vnm,
972     VNET_L3_PACKET_TYPE_IP6,
973     sw_if_index,
974     node_index,
975     VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST,
976     &adj->rewrite_header,
977     sizeof (adj->rewrite_data));
978 }
979
980 static void
981 ip6_add_interface_routes (vnet_main_t * vnm, u32 sw_if_index,
982                           ip6_main_t * im, u32 fib_index,
983                           ip_interface_address_t * a)
984 {
985   ip_lookup_main_t * lm = &im->lookup_main;
986   ip_adjacency_t * adj;
987   ip6_address_t * address = ip_interface_address_get_address (lm, a);
988   ip6_add_del_route_args_t x;
989   vnet_hw_interface_t * hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
990   u32 classify_table_index;
991
992   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
993   x.table_index_or_table_id = fib_index;
994   x.flags = (IP6_ROUTE_FLAG_ADD
995              | IP6_ROUTE_FLAG_FIB_INDEX
996              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
997   x.dst_address = address[0];
998   x.dst_address_length = a->address_length;
999   x.n_add_adj = 0;
1000   x.add_adj = 0;
1001
1002   a->neighbor_probe_adj_index = ~0;
1003   if (a->address_length < 128)
1004     {
1005       adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1006                               &x.adj_index);
1007       ip6_adjacency_set_interface_route (vnm, adj, sw_if_index, a - lm->if_address_pool);
1008       ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1009       ip6_add_del_route (im, &x);
1010       a->neighbor_probe_adj_index = x.adj_index;
1011     }
1012
1013   /* Add e.g. ::1/128 as local to this host. */
1014   adj = ip_add_adjacency (lm, /* template */ 0, /* block size */ 1,
1015                           &x.adj_index);
1016
1017   classify_table_index = ~0;
1018   if (sw_if_index < vec_len (lm->classify_table_index_by_sw_if_index))
1019     classify_table_index = lm->classify_table_index_by_sw_if_index [sw_if_index];
1020   if (classify_table_index != (u32) ~0)
1021     {
1022       adj->lookup_next_index = IP_LOOKUP_NEXT_CLASSIFY;
1023       adj->classify.table_index = classify_table_index;
1024     }
1025   else
1026     adj->lookup_next_index = IP_LOOKUP_NEXT_LOCAL;
1027   
1028   adj->if_address_index = a - lm->if_address_pool;
1029   adj->rewrite_header.sw_if_index = sw_if_index;
1030   adj->rewrite_header.max_l3_packet_bytes = hw_if->max_l3_packet_bytes[VLIB_RX];
1031   adj->rewrite_header.data_bytes = 0;
1032   ip_call_add_del_adjacency_callbacks (lm, x.adj_index, /* is_del */ 0);
1033   x.dst_address_length = 128;
1034   ip6_add_del_route (im, &x);
1035 }
1036
1037 static void
1038 ip6_del_interface_routes (ip6_main_t * im, u32 fib_index,
1039                           ip6_address_t * address, u32 address_length)
1040 {
1041   ip6_add_del_route_args_t x;
1042
1043   /* Add e.g. 1.0.0.0/8 as interface route (arp for Ethernet). */
1044   x.table_index_or_table_id = fib_index;
1045   x.flags = (IP6_ROUTE_FLAG_DEL
1046              | IP6_ROUTE_FLAG_FIB_INDEX
1047              | IP6_ROUTE_FLAG_NO_REDISTRIBUTE);
1048   x.dst_address = address[0];
1049   x.dst_address_length = address_length;
1050   x.adj_index = ~0;
1051   x.n_add_adj = 0;
1052   x.add_adj = 0;
1053
1054   if (address_length < 128)
1055     {
1056       /* Don't wipe out fe80::0/64 */
1057       if (address_length != 64 || 
1058           address[0].as_u64[0] != clib_net_to_host_u64(0xfe80000000000000ULL))
1059         ip6_add_del_route (im, &x);
1060     }
1061
1062   x.dst_address_length = 128;
1063   ip6_add_del_route (im, &x);
1064
1065   ip6_delete_matching_routes (im,
1066                               fib_index,
1067                               IP6_ROUTE_FLAG_FIB_INDEX,
1068                               address,
1069                               address_length);
1070 }
1071
1072 typedef struct {
1073     u32 sw_if_index;
1074     ip6_address_t address;
1075     u32 length;
1076 } ip6_interface_address_t;
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 * new_address,
1082                                         u32 new_length,
1083                                         u32 redistribute,
1084                                         u32 insert_routes,
1085                                         u32 is_del);
1086
1087 static clib_error_t *
1088 ip6_add_del_interface_address_internal (vlib_main_t * vm,
1089                                         u32 sw_if_index,
1090                                         ip6_address_t * address,
1091                                         u32 address_length,
1092                                         u32 redistribute,
1093                                         u32 insert_routes,
1094                                         u32 is_del)
1095 {
1096   vnet_main_t * vnm = vnet_get_main();
1097   ip6_main_t * im = &ip6_main;
1098   ip_lookup_main_t * lm = &im->lookup_main;
1099   clib_error_t * error;
1100   u32 if_address_index;
1101   ip6_address_fib_t ip6_af, * addr_fib = 0;
1102
1103   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1104   ip6_addr_fib_init (&ip6_af, address,
1105                      vec_elt (im->fib_index_by_sw_if_index, sw_if_index));
1106   vec_add1 (addr_fib, ip6_af);
1107
1108   {
1109     uword elts_before = pool_elts (lm->if_address_pool);
1110
1111     error = ip_interface_address_add_del
1112       (lm,
1113        sw_if_index,
1114        addr_fib,
1115        address_length,
1116        is_del,
1117        &if_address_index);
1118     if (error)
1119       goto done;
1120
1121     /* Pool did not grow: add duplicate address. */
1122     if (elts_before == pool_elts (lm->if_address_pool))
1123       goto done;
1124   }
1125
1126   if (vnet_sw_interface_is_admin_up (vnm, sw_if_index) && insert_routes)
1127     {
1128       if (is_del)
1129         ip6_del_interface_routes (im, ip6_af.fib_index, address,
1130                                   address_length);
1131
1132       else
1133         ip6_add_interface_routes (vnm, sw_if_index,
1134                                   im, ip6_af.fib_index,
1135                                   pool_elt_at_index (lm->if_address_pool, if_address_index));
1136     }
1137
1138   {
1139     ip6_add_del_interface_address_callback_t * cb;
1140     vec_foreach (cb, im->add_del_interface_address_callbacks)
1141       cb->function (im, cb->function_opaque, sw_if_index,
1142                     address, address_length,
1143                     if_address_index,
1144                     is_del);
1145   }
1146
1147  done:
1148   vec_free (addr_fib);
1149   return error;
1150 }
1151
1152 clib_error_t *
1153 ip6_add_del_interface_address (vlib_main_t * vm, u32 sw_if_index,
1154                                ip6_address_t * address, u32 address_length,
1155                                u32 is_del)
1156 {
1157   return ip6_add_del_interface_address_internal
1158     (vm, sw_if_index, address, address_length,
1159      /* redistribute */ 1,
1160      /* insert_routes */ 1,
1161      is_del);
1162 }
1163
1164 clib_error_t *
1165 ip6_sw_interface_admin_up_down (vnet_main_t * vnm,
1166                                 u32 sw_if_index,
1167                                 u32 flags)
1168 {
1169   ip6_main_t * im = &ip6_main;
1170   ip_interface_address_t * ia;
1171   ip6_address_t * a;
1172   u32 is_admin_up, fib_index;
1173
1174   /* Fill in lookup tables with default table (0). */
1175   vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
1176
1177   vec_validate_init_empty (im->lookup_main.if_address_pool_index_by_sw_if_index, sw_if_index, ~0);
1178
1179   is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1180
1181   fib_index = vec_elt (im->fib_index_by_sw_if_index, sw_if_index);
1182
1183   foreach_ip_interface_address (&im->lookup_main, ia, sw_if_index, 
1184                                 0 /* honor unnumbered */,
1185   ({
1186     a = ip_interface_address_get_address (&im->lookup_main, ia);
1187     if (is_admin_up)
1188       ip6_add_interface_routes (vnm, sw_if_index,
1189                                 im, fib_index,
1190                                 ia);
1191     else
1192       ip6_del_interface_routes (im, fib_index,
1193                                 a, ia->address_length);
1194   }));
1195
1196   return 0;
1197 }
1198
1199 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_sw_interface_admin_up_down);
1200
1201 clib_error_t *
1202 ip6_sw_interface_add_del (vnet_main_t * vnm,
1203                           u32 sw_if_index,
1204                           u32 is_add)
1205 {
1206   vlib_main_t * vm = vnm->vlib_main;
1207   ip6_main_t * im = &ip6_main;
1208   ip_lookup_main_t * lm = &im->lookup_main;
1209   u32 ci, cast;
1210
1211   for (cast = 0; cast < VNET_N_CAST; cast++)
1212     {
1213       ip_config_main_t * cm = &lm->rx_config_mains[cast];
1214       vnet_config_main_t * vcm = &cm->config_main;
1215
1216       /* FIXME multicast. */
1217       if (! vcm->node_index_by_feature_index)
1218         {
1219           char * start_nodes[] = { "ip6-input", };
1220           char * feature_nodes[] = {
1221             [IP6_RX_FEATURE_CHECK_ACCESS] = "ip6-inacl",
1222             [IP6_RX_FEATURE_IPSEC] = "ipsec-input-ip6",
1223             [IP6_RX_FEATURE_L2TPV3] = "l2tp-decap",
1224             [IP6_RX_FEATURE_VPATH]  = "vpath-input-ip6",
1225             [IP6_RX_FEATURE_LOOKUP] = "ip6-lookup",
1226           };
1227           vnet_config_init (vm, vcm,
1228                             start_nodes, ARRAY_LEN (start_nodes),
1229                             feature_nodes, ARRAY_LEN (feature_nodes));
1230         }
1231
1232       vec_validate_init_empty (cm->config_index_by_sw_if_index, sw_if_index, ~0);
1233       ci = cm->config_index_by_sw_if_index[sw_if_index];
1234
1235       if (is_add)
1236         ci = vnet_config_add_feature (vm, vcm,
1237                                       ci,
1238                                       IP6_RX_FEATURE_LOOKUP,
1239                                       /* config data */ 0,
1240                                       /* # bytes of config data */ 0);
1241       else
1242         ci = vnet_config_del_feature (vm, vcm,
1243                                       ci,
1244                                       IP6_RX_FEATURE_LOOKUP,
1245                                       /* config data */ 0,
1246                                       /* # bytes of config data */ 0);
1247
1248       cm->config_index_by_sw_if_index[sw_if_index] = ci;
1249     }
1250   return /* no error */ 0;
1251 }
1252
1253 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ip6_sw_interface_add_del);
1254
1255 static uword
1256 ip6_lookup (vlib_main_t * vm,
1257             vlib_node_runtime_t * node,
1258             vlib_frame_t * frame)
1259 {
1260   return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 0);
1261 }
1262
1263 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args);
1264
1265 VLIB_REGISTER_NODE (ip6_lookup_node) = {
1266   .function = ip6_lookup,
1267   .name = "ip6-lookup",
1268   .vector_size = sizeof (u32),
1269
1270   .format_trace = format_ip6_lookup_trace,
1271
1272   .n_next_nodes = IP_LOOKUP_N_NEXT,
1273   .next_nodes = IP6_LOOKUP_NEXT_NODES,
1274 };
1275
1276 VLIB_NODE_FUNCTION_MULTIARCH (ip6_lookup_node, ip6_lookup)
1277
1278 static uword
1279 ip6_indirect (vlib_main_t * vm,
1280               vlib_node_runtime_t * node,
1281               vlib_frame_t * frame)
1282 {
1283   return ip6_lookup_inline (vm, node, frame, /* is_indirect */ 1);
1284 }
1285
1286
1287 VLIB_REGISTER_NODE (ip6_indirect_node) = {
1288   .function = ip6_indirect,
1289   .name = "ip6-indirect",
1290   .vector_size = sizeof (u32),
1291
1292   .format_trace = format_ip6_lookup_trace,
1293
1294   .n_next_nodes = IP_LOOKUP_N_NEXT,
1295   .next_nodes = IP6_LOOKUP_NEXT_NODES,
1296 };
1297
1298 VLIB_NODE_FUNCTION_MULTIARCH (ip6_indirect_node, ip6_indirect)
1299
1300 typedef struct {
1301   /* Adjacency taken. */
1302   u32 adj_index;
1303   u32 flow_hash;
1304   u32 fib_index;
1305
1306   /* Packet data, possibly *after* rewrite. */
1307   u8 packet_data[128 - 1*sizeof(u32)];
1308 } ip6_forward_next_trace_t;
1309
1310 static u8 * format_ip6_forward_next_trace (u8 * s, va_list * args)
1311 {
1312   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1313   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1314   ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1315   uword indent = format_get_indent (s);
1316
1317   s = format(s, "%U%U",
1318              format_white_space, indent,
1319              format_ip6_header, t->packet_data);
1320   return s;
1321 }
1322
1323 static u8 * format_ip6_lookup_trace (u8 * s, va_list * args)
1324 {
1325   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1326   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1327   ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1328   vnet_main_t * vnm = vnet_get_main();
1329   ip6_main_t * im = &ip6_main;
1330   uword indent = format_get_indent (s);
1331
1332   s = format (s, "fib %d adj-idx %d : %U flow hash: 0x%08x",
1333               t->fib_index, t->adj_index, format_ip_adjacency,
1334               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1335   s = format(s, "\n%U%U",
1336              format_white_space, indent,
1337              format_ip6_header, t->packet_data);
1338   return s;
1339 }
1340
1341
1342 static u8 * format_ip6_rewrite_trace (u8 * s, va_list * args)
1343 {
1344   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1345   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1346   ip6_forward_next_trace_t * t = va_arg (*args, ip6_forward_next_trace_t *);
1347   vnet_main_t * vnm = vnet_get_main();
1348   ip6_main_t * im = &ip6_main;
1349   uword indent = format_get_indent (s);
1350
1351   s = format (s, "tx_sw_if_index %d adj-idx %d : %U flow hash: 0x%08x",
1352               t->fib_index, t->adj_index, format_ip_adjacency,
1353               vnm, &im->lookup_main, t->adj_index, t->flow_hash);
1354   s = format (s, "\n%U%U",
1355               format_white_space, indent,
1356               format_ip_adjacency_packet_data,
1357               vnm, &im->lookup_main, t->adj_index,
1358               t->packet_data, sizeof (t->packet_data));
1359   return s;
1360 }
1361
1362 /* Common trace function for all ip6-forward next nodes. */
1363 void
1364 ip6_forward_next_trace (vlib_main_t * vm,
1365                         vlib_node_runtime_t * node,
1366                         vlib_frame_t * frame,
1367                         vlib_rx_or_tx_t which_adj_index)
1368 {
1369   u32 * from, n_left;
1370   ip6_main_t * im = &ip6_main;
1371
1372   n_left = frame->n_vectors;
1373   from = vlib_frame_vector_args (frame);
1374
1375   while (n_left >= 4)
1376     {
1377       u32 bi0, bi1;
1378       vlib_buffer_t * b0, * b1;
1379       ip6_forward_next_trace_t * t0, * t1;
1380
1381       /* Prefetch next iteration. */
1382       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
1383       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
1384
1385       bi0 = from[0];
1386       bi1 = from[1];
1387
1388       b0 = vlib_get_buffer (vm, bi0);
1389       b1 = vlib_get_buffer (vm, bi1);
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 = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1397               vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1398               vec_elt (im->fib_index_by_sw_if_index,
1399                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1400
1401           clib_memcpy (t0->packet_data,
1402                   vlib_buffer_get_current (b0),
1403                   sizeof (t0->packet_data));
1404         }
1405       if (b1->flags & VLIB_BUFFER_IS_TRACED)
1406         {
1407           t1 = vlib_add_trace (vm, node, b1, sizeof (t1[0]));
1408           t1->adj_index = vnet_buffer (b1)->ip.adj_index[which_adj_index];
1409           t1->flow_hash = vnet_buffer (b1)->ip.flow_hash;
1410           t1->fib_index = (vnet_buffer(b1)->sw_if_index[VLIB_TX] != (u32)~0) ?
1411               vnet_buffer(b1)->sw_if_index[VLIB_TX] :
1412               vec_elt (im->fib_index_by_sw_if_index,
1413                        vnet_buffer(b1)->sw_if_index[VLIB_RX]);
1414
1415           clib_memcpy (t1->packet_data,
1416                   vlib_buffer_get_current (b1),
1417                   sizeof (t1->packet_data));
1418         }
1419       from += 2;
1420       n_left -= 2;
1421     }
1422
1423   while (n_left >= 1)
1424     {
1425       u32 bi0;
1426       vlib_buffer_t * b0;
1427       ip6_forward_next_trace_t * t0;
1428
1429       bi0 = from[0];
1430
1431       b0 = vlib_get_buffer (vm, bi0);
1432
1433       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1434         {
1435           t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
1436           t0->adj_index = vnet_buffer (b0)->ip.adj_index[which_adj_index];
1437           t0->flow_hash = vnet_buffer (b0)->ip.flow_hash;
1438           t0->fib_index = (vnet_buffer(b0)->sw_if_index[VLIB_TX] != (u32)~0) ?
1439               vnet_buffer(b0)->sw_if_index[VLIB_TX] :
1440               vec_elt (im->fib_index_by_sw_if_index,
1441                        vnet_buffer(b0)->sw_if_index[VLIB_RX]);
1442
1443           clib_memcpy (t0->packet_data,
1444                   vlib_buffer_get_current (b0),
1445                   sizeof (t0->packet_data));
1446         }
1447       from += 1;
1448       n_left -= 1;
1449     }
1450 }
1451
1452 static uword
1453 ip6_drop_or_punt (vlib_main_t * vm,
1454                   vlib_node_runtime_t * node,
1455                   vlib_frame_t * frame,
1456                   ip6_error_t error_code)
1457 {
1458   u32 * buffers = vlib_frame_vector_args (frame);
1459   uword n_packets = frame->n_vectors;
1460
1461   vlib_error_drop_buffers (vm, node,
1462                            buffers,
1463                            /* stride */ 1,
1464                            n_packets,
1465                            /* next */ 0,
1466                            ip6_input_node.index,
1467                            error_code);
1468
1469   if (node->flags & VLIB_NODE_FLAG_TRACE)
1470     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1471
1472   return n_packets;
1473 }
1474
1475 static uword
1476 ip6_drop (vlib_main_t * vm,
1477           vlib_node_runtime_t * node,
1478           vlib_frame_t * frame)
1479 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_DROP); }
1480
1481 static uword
1482 ip6_punt (vlib_main_t * vm,
1483           vlib_node_runtime_t * node,
1484           vlib_frame_t * frame)
1485 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_ADJACENCY_PUNT); }
1486
1487 static uword
1488 ip6_miss (vlib_main_t * vm,
1489           vlib_node_runtime_t * node,
1490           vlib_frame_t * frame)
1491 { return ip6_drop_or_punt (vm, node, frame, IP6_ERROR_DST_LOOKUP_MISS); }
1492
1493 VLIB_REGISTER_NODE (ip6_drop_node,static) = {
1494   .function = ip6_drop,
1495   .name = "ip6-drop",
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 VLIB_NODE_FUNCTION_MULTIARCH (ip6_drop_node, ip6_drop)
1507
1508 VLIB_REGISTER_NODE (ip6_punt_node,static) = {
1509   .function = ip6_punt,
1510   .name = "ip6-punt",
1511   .vector_size = sizeof (u32),
1512
1513   .format_trace = format_ip6_forward_next_trace,
1514
1515   .n_next_nodes = 1,
1516   .next_nodes = {
1517     [0] = "error-punt",
1518   },
1519 };
1520
1521 VLIB_NODE_FUNCTION_MULTIARCH (ip6_punt_node, ip6_punt)
1522
1523 VLIB_REGISTER_NODE (ip6_miss_node,static) = {
1524   .function = ip6_miss,
1525   .name = "ip6-miss",
1526   .vector_size = sizeof (u32),
1527
1528   .format_trace = format_ip6_forward_next_trace,
1529
1530   .n_next_nodes = 1,
1531   .next_nodes = {
1532     [0] = "error-drop",
1533   },
1534 };
1535
1536 VLIB_NODE_FUNCTION_MULTIARCH (ip6_miss_node, ip6_miss)
1537
1538 VLIB_REGISTER_NODE (ip6_multicast_node,static) = {
1539   .function = ip6_drop,
1540   .name = "ip6-multicast",
1541   .vector_size = sizeof (u32),
1542
1543   .format_trace = format_ip6_forward_next_trace,
1544
1545   .n_next_nodes = 1,
1546   .next_nodes = {
1547     [0] = "error-drop",
1548   },
1549 };
1550
1551 /* Compute TCP/UDP/ICMP6 checksum in software. */
1552 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0, ip6_header_t * ip0, int *bogus_lengthp)
1553 {
1554   ip_csum_t sum0;
1555   u16 sum16, payload_length_host_byte_order;
1556   u32 i, n_this_buffer, n_bytes_left;
1557   u32 headers_size = sizeof(ip0[0]);
1558   void * data_this_buffer;
1559
1560   ASSERT(bogus_lengthp);
1561   *bogus_lengthp = 0;
1562
1563   /* Initialize checksum with ip header. */
1564   sum0 = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
1565   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
1566   data_this_buffer = (void *) (ip0 + 1);
1567  
1568   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
1569     {
1570       sum0 = ip_csum_with_carry (sum0,
1571                                  clib_mem_unaligned (&ip0->src_address.as_uword[i], uword));
1572       sum0 = ip_csum_with_carry (sum0,
1573                                  clib_mem_unaligned (&ip0->dst_address.as_uword[i], uword));
1574     }
1575
1576   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1577   if (PREDICT_FALSE (ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
1578     {
1579       u32  skip_bytes;
1580       ip6_hop_by_hop_ext_t *ext_hdr = (ip6_hop_by_hop_ext_t  *)data_this_buffer;
1581
1582       /* validate really icmp6 next */
1583       ASSERT(ext_hdr->next_hdr == IP_PROTOCOL_ICMP6);
1584
1585       skip_bytes = 8* (1 + ext_hdr->n_data_u64s);
1586       data_this_buffer  = (void *)((u8 *)data_this_buffer + skip_bytes);
1587  
1588       payload_length_host_byte_order  -= skip_bytes;
1589       headers_size += skip_bytes;
1590    }
1591
1592   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
1593 #if DPDK > 0
1594   if (p0 && n_this_buffer + headers_size  > p0->current_length)
1595   {
1596     struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer(p0);
1597     u8 nb_segs = mb->nb_segs;
1598
1599     n_this_buffer = (p0->current_length > headers_size ?
1600                      p0->current_length - headers_size : 0);
1601     while (n_bytes_left)
1602       {
1603         sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1604         n_bytes_left -= n_this_buffer;
1605
1606         mb = mb->next;
1607         nb_segs--;
1608         if ((nb_segs == 0) || (mb == 0))
1609           break;
1610
1611         data_this_buffer = rte_ctrlmbuf_data(mb);
1612         n_this_buffer = mb->data_len;
1613       }
1614     if (n_bytes_left || nb_segs)
1615       {
1616         *bogus_lengthp = 1;
1617         return 0xfefe;
1618       }
1619   } 
1620   else sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1621 #else
1622   if (p0 && n_this_buffer + headers_size  > p0->current_length)
1623     n_this_buffer = p0->current_length > headers_size  ? p0->current_length - headers_size  : 0;
1624   while (1)
1625     {
1626       sum0 = ip_incremental_checksum (sum0, data_this_buffer, n_this_buffer);
1627       n_bytes_left -= n_this_buffer;
1628       if (n_bytes_left == 0)
1629         break;
1630
1631       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
1632         {
1633           *bogus_lengthp = 1;
1634           return 0xfefe;
1635         }
1636       p0 = vlib_get_buffer (vm, p0->next_buffer);
1637       data_this_buffer = vlib_buffer_get_current (p0);
1638       n_this_buffer = p0->current_length;
1639     }
1640 #endif /* DPDK */
1641
1642   sum16 = ~ ip_csum_fold (sum0);
1643
1644   return sum16;
1645 }
1646
1647 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0)
1648 {
1649   ip6_header_t * ip0 = vlib_buffer_get_current (p0);
1650   udp_header_t * udp0;
1651   u16 sum16;
1652   int bogus_length;
1653
1654   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets) */
1655   ASSERT (ip0->protocol == IP_PROTOCOL_TCP
1656           || ip0->protocol == IP_PROTOCOL_ICMP6
1657           || ip0->protocol == IP_PROTOCOL_UDP
1658           || ip0->protocol ==  IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS);
1659
1660   udp0 = (void *) (ip0 + 1);
1661   if (ip0->protocol == IP_PROTOCOL_UDP && udp0->checksum == 0)
1662     {
1663       p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1664                     | IP_BUFFER_L4_CHECKSUM_CORRECT);
1665       return p0->flags;
1666     }
1667
1668   sum16 = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, &bogus_length);
1669
1670   p0->flags |= (IP_BUFFER_L4_CHECKSUM_COMPUTED
1671                 | ((sum16 == 0) << LOG2_IP_BUFFER_L4_CHECKSUM_CORRECT));
1672
1673   return p0->flags;
1674 }
1675
1676 static uword
1677 ip6_local (vlib_main_t * vm,
1678            vlib_node_runtime_t * node,
1679            vlib_frame_t * frame)
1680 {
1681   ip6_main_t * im = &ip6_main;
1682   ip_lookup_main_t * lm = &im->lookup_main;
1683   ip_local_next_t next_index;
1684   u32 * from, * to_next, n_left_from, n_left_to_next;
1685   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
1686
1687   from = vlib_frame_vector_args (frame);
1688   n_left_from = frame->n_vectors;
1689   next_index = node->cached_next_index;
1690   
1691   if (node->flags & VLIB_NODE_FLAG_TRACE)
1692     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1693
1694   while (n_left_from > 0)
1695     {
1696       vlib_get_next_frame (vm, node, next_index,
1697                            to_next, n_left_to_next);
1698
1699       while (n_left_from >= 4 && n_left_to_next >= 2)
1700         {
1701           vlib_buffer_t * p0, * p1;
1702           ip6_header_t * ip0, * ip1;
1703           udp_header_t * udp0, * udp1;
1704           u32 pi0, ip_len0, udp_len0, flags0, next0;
1705           u32 pi1, ip_len1, udp_len1, flags1, next1;
1706           i32 len_diff0, len_diff1;
1707           u8 error0, type0, good_l4_checksum0;
1708           u8 error1, type1, good_l4_checksum1;
1709       
1710           pi0 = to_next[0] = from[0];
1711           pi1 = to_next[1] = from[1];
1712           from += 2;
1713           n_left_from -= 2;
1714           to_next += 2;
1715           n_left_to_next -= 2;
1716       
1717           p0 = vlib_get_buffer (vm, pi0);
1718           p1 = vlib_get_buffer (vm, pi1);
1719
1720           ip0 = vlib_buffer_get_current (p0);
1721           ip1 = vlib_buffer_get_current (p1);
1722
1723           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1724           type1 = lm->builtin_protocol_by_ip_protocol[ip1->protocol];
1725
1726           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1727           next1 = lm->local_next_by_ip_protocol[ip1->protocol];
1728
1729           flags0 = p0->flags;
1730           flags1 = p1->flags;
1731
1732           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1733           good_l4_checksum1 = (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1734
1735           udp0 = ip6_next_header (ip0);
1736           udp1 = ip6_next_header (ip1);
1737
1738           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1739           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1740           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UDP && udp1->checksum == 0;
1741
1742           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1743           good_l4_checksum1 |= type1 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1744
1745           /* Verify UDP length. */
1746           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1747           ip_len1 = clib_net_to_host_u16 (ip1->payload_length);
1748           udp_len0 = clib_net_to_host_u16 (udp0->length);
1749           udp_len1 = clib_net_to_host_u16 (udp1->length);
1750
1751           len_diff0 = ip_len0 - udp_len0;
1752           len_diff1 = ip_len1 - udp_len1;
1753
1754           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1755           len_diff1 = type1 == IP_BUILTIN_PROTOCOL_UDP ? len_diff1 : 0;
1756
1757           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1758                              && ! good_l4_checksum0
1759                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1760             {
1761               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1762               good_l4_checksum0 =
1763                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1764             }
1765           if (PREDICT_FALSE (type1 != IP_BUILTIN_PROTOCOL_UNKNOWN
1766                              && ! good_l4_checksum1
1767                              && ! (flags1 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1768             {
1769               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, p1);
1770               good_l4_checksum1 =
1771                 (flags1 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1772             }
1773
1774           error0 = error1 = IP6_ERROR_UNKNOWN_PROTOCOL;
1775
1776           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1777           error1 = len_diff1 < 0 ? IP6_ERROR_UDP_LENGTH : error1;
1778
1779           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1780           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1781           error0 = (! good_l4_checksum0
1782                     ? IP6_ERROR_UDP_CHECKSUM + type0
1783                     : error0);
1784           error1 = (! good_l4_checksum1
1785                     ? IP6_ERROR_UDP_CHECKSUM + type1
1786                     : error1);
1787
1788           /* Drop packets from unroutable hosts. */
1789           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1790           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1791             {
1792               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1793               error0 = (lm->miss_adj_index == src_adj_index0
1794                         ? IP6_ERROR_SRC_LOOKUP_MISS
1795                         : error0);
1796             }
1797           if (error1 == IP6_ERROR_UNKNOWN_PROTOCOL && type1 != IP_BUILTIN_PROTOCOL_ICMP)
1798             {
1799               u32 src_adj_index1 = ip6_src_lookup_for_packet (im, p1, ip1);
1800               error1 = (lm->miss_adj_index == src_adj_index1
1801                         ? IP6_ERROR_SRC_LOOKUP_MISS
1802                         : error1);
1803             }
1804
1805           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1806           next1 = error1 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next1;
1807
1808           p0->error = error_node->errors[error0];
1809           p1->error = error_node->errors[error1];
1810
1811           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1812                                            to_next, n_left_to_next,
1813                                            pi0, pi1, next0, next1);
1814         }
1815
1816       while (n_left_from > 0 && n_left_to_next > 0)
1817         {
1818           vlib_buffer_t * p0;
1819           ip6_header_t * ip0;
1820           udp_header_t * udp0;
1821           u32 pi0, ip_len0, udp_len0, flags0, next0;
1822           i32 len_diff0;
1823           u8 error0, type0, good_l4_checksum0;
1824       
1825           pi0 = to_next[0] = from[0];
1826           from += 1;
1827           n_left_from -= 1;
1828           to_next += 1;
1829           n_left_to_next -= 1;
1830       
1831           p0 = vlib_get_buffer (vm, pi0);
1832
1833           ip0 = vlib_buffer_get_current (p0);
1834
1835           type0 = lm->builtin_protocol_by_ip_protocol[ip0->protocol];
1836           next0 = lm->local_next_by_ip_protocol[ip0->protocol];
1837
1838           flags0 = p0->flags;
1839
1840           good_l4_checksum0 = (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1841
1842           udp0 = ip6_next_header (ip0);
1843
1844           /* Don't verify UDP checksum for packets with explicit zero checksum. */
1845           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UDP && udp0->checksum == 0;
1846
1847           good_l4_checksum0 |= type0 == IP_BUILTIN_PROTOCOL_UNKNOWN;
1848
1849           /* Verify UDP length. */
1850           ip_len0 = clib_net_to_host_u16 (ip0->payload_length);
1851           udp_len0 = clib_net_to_host_u16 (udp0->length);
1852
1853           len_diff0 = ip_len0 - udp_len0;
1854
1855           len_diff0 = type0 == IP_BUILTIN_PROTOCOL_UDP ? len_diff0 : 0;
1856
1857           if (PREDICT_FALSE (type0 != IP_BUILTIN_PROTOCOL_UNKNOWN
1858                              && ! good_l4_checksum0
1859                              && ! (flags0 & IP_BUFFER_L4_CHECKSUM_COMPUTED)))
1860             {
1861               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, p0);
1862               good_l4_checksum0 =
1863                 (flags0 & IP_BUFFER_L4_CHECKSUM_CORRECT) != 0;
1864             }
1865
1866           error0 = IP6_ERROR_UNKNOWN_PROTOCOL;
1867
1868           error0 = len_diff0 < 0 ? IP6_ERROR_UDP_LENGTH : error0;
1869
1870           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_UDP == IP6_ERROR_UDP_CHECKSUM);
1871           ASSERT (IP6_ERROR_UDP_CHECKSUM + IP_BUILTIN_PROTOCOL_ICMP == IP6_ERROR_ICMP_CHECKSUM);
1872           error0 = (! good_l4_checksum0
1873                     ? IP6_ERROR_UDP_CHECKSUM + type0
1874                     : error0);
1875
1876           /* If this is a neighbor solicitation (ICMP), skip source RPF check */
1877           if (error0 == IP6_ERROR_UNKNOWN_PROTOCOL && type0 != IP_BUILTIN_PROTOCOL_ICMP)
1878             {
1879               u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
1880               error0 = (lm->miss_adj_index == src_adj_index0
1881                         ? IP6_ERROR_SRC_LOOKUP_MISS
1882                         : error0);
1883             }
1884
1885           next0 = error0 != IP6_ERROR_UNKNOWN_PROTOCOL ? IP_LOCAL_NEXT_DROP : next0;
1886
1887           p0->error = error_node->errors[error0];
1888
1889           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1890                                            to_next, n_left_to_next,
1891                                            pi0, next0);
1892         }
1893   
1894       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1895     }
1896
1897   return frame->n_vectors;
1898 }
1899
1900 VLIB_REGISTER_NODE (ip6_local_node,static) = {
1901   .function = ip6_local,
1902   .name = "ip6-local",
1903   .vector_size = sizeof (u32),
1904
1905   .format_trace = format_ip6_forward_next_trace,
1906
1907   .n_next_nodes = IP_LOCAL_N_NEXT,
1908   .next_nodes = {
1909     [IP_LOCAL_NEXT_DROP] = "error-drop",
1910     [IP_LOCAL_NEXT_PUNT] = "error-punt",
1911     [IP_LOCAL_NEXT_UDP_LOOKUP] = "ip6-udp-lookup",
1912     [IP_LOCAL_NEXT_ICMP] = "ip6-icmp-input",
1913   },
1914 };
1915
1916 VLIB_NODE_FUNCTION_MULTIARCH (ip6_local_node, ip6_local)
1917
1918 void ip6_register_protocol (u32 protocol, u32 node_index)
1919 {
1920   vlib_main_t * vm = vlib_get_main();
1921   ip6_main_t * im = &ip6_main;
1922   ip_lookup_main_t * lm = &im->lookup_main;
1923
1924   ASSERT (protocol < ARRAY_LEN (lm->local_next_by_ip_protocol));
1925   lm->local_next_by_ip_protocol[protocol] = vlib_node_add_next (vm, ip6_local_node.index, node_index);
1926 }
1927
1928 typedef enum {
1929   IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1930   IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
1931   IP6_DISCOVER_NEIGHBOR_N_NEXT,
1932 } ip6_discover_neighbor_next_t;
1933
1934 typedef enum {
1935   IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
1936   IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
1937 } ip6_discover_neighbor_error_t;
1938
1939 static uword
1940 ip6_discover_neighbor (vlib_main_t * vm,
1941                        vlib_node_runtime_t * node,
1942                        vlib_frame_t * frame)
1943 {
1944   vnet_main_t * vnm = vnet_get_main();
1945   ip6_main_t * im = &ip6_main;
1946   ip_lookup_main_t * lm = &im->lookup_main;
1947   u32 * from, * to_next_drop;
1948   uword n_left_from, n_left_to_next_drop;
1949   static f64 time_last_seed_change = -1e100;
1950   static u32 hash_seeds[3];
1951   static uword hash_bitmap[256 / BITS (uword)]; 
1952   f64 time_now;
1953   int bogus_length;
1954
1955   if (node->flags & VLIB_NODE_FLAG_TRACE)
1956     ip6_forward_next_trace (vm, node, frame, VLIB_TX);
1957
1958   time_now = vlib_time_now (vm);
1959   if (time_now - time_last_seed_change > 1e-3)
1960     {
1961       uword i;
1962       u32 * r = clib_random_buffer_get_data (&vm->random_buffer,
1963                                              sizeof (hash_seeds));
1964       for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
1965         hash_seeds[i] = r[i];
1966
1967       /* Mark all hash keys as been not-seen before. */
1968       for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
1969         hash_bitmap[i] = 0;
1970
1971       time_last_seed_change = time_now;
1972     }
1973
1974   from = vlib_frame_vector_args (frame);
1975   n_left_from = frame->n_vectors;
1976
1977   while (n_left_from > 0)
1978     {
1979       vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
1980                            to_next_drop, n_left_to_next_drop);
1981
1982       while (n_left_from > 0 && n_left_to_next_drop > 0)
1983         {
1984           vlib_buffer_t * p0;
1985           ip6_header_t * ip0;
1986           u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
1987           uword bm0;
1988           ip_adjacency_t * adj0;
1989           vnet_hw_interface_t * hw_if0;
1990           u32 next0;
1991
1992           pi0 = from[0];
1993
1994           p0 = vlib_get_buffer (vm, pi0);
1995
1996           adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
1997
1998           ip0 = vlib_buffer_get_current (p0);
1999
2000           adj0 = ip_get_adjacency (lm, adj_index0);
2001
2002           if (adj0->arp.next_hop.ip6.as_u64[0] ||
2003               adj0->arp.next_hop.ip6.as_u64[1]) {
2004             ip0->dst_address.as_u64[0] = adj0->arp.next_hop.ip6.as_u64[0];
2005             ip0->dst_address.as_u64[1] = adj0->arp.next_hop.ip6.as_u64[1];
2006           }
2007
2008           a0 = hash_seeds[0];
2009           b0 = hash_seeds[1];
2010           c0 = hash_seeds[2];
2011
2012           sw_if_index0 = adj0->rewrite_header.sw_if_index;
2013           vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
2014
2015           a0 ^= sw_if_index0;
2016           b0 ^= ip0->dst_address.as_u32[0];
2017           c0 ^= ip0->dst_address.as_u32[1];
2018
2019           hash_v3_mix32 (a0, b0, c0);
2020
2021           b0 ^= ip0->dst_address.as_u32[2];
2022           c0 ^= ip0->dst_address.as_u32[3];
2023
2024           hash_v3_finalize32 (a0, b0, c0);
2025
2026           c0 &= BITS (hash_bitmap) - 1;
2027           c0 = c0 / BITS (uword);
2028           m0 = (uword) 1 << (c0 % BITS (uword));
2029
2030           bm0 = hash_bitmap[c0];
2031           drop0 = (bm0 & m0) != 0;
2032
2033           /* Mark it as seen. */
2034           hash_bitmap[c0] = bm0 | m0;
2035
2036           from += 1;
2037           n_left_from -= 1;
2038           to_next_drop[0] = pi0;
2039           to_next_drop += 1;
2040           n_left_to_next_drop -= 1;
2041
2042           hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
2043
2044           /* If the interface is link-down, drop the pkt */
2045           if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
2046             drop0 = 1;
2047
2048           p0->error = 
2049             node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP 
2050                          : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
2051           if (drop0)
2052             continue;
2053
2054           {
2055             u32 bi0 = 0;
2056             icmp6_neighbor_solicitation_header_t * h0;
2057             vlib_buffer_t * b0;
2058
2059             h0 = vlib_packet_template_get_packet 
2060               (vm, &im->discover_neighbor_packet_template, &bi0);
2061
2062             /* 
2063              * Build ethernet header.
2064              * Choose source address based on destination lookup 
2065              * adjacency. 
2066              */
2067             ip6_src_address_for_packet (im, p0, &h0->ip.src_address, 
2068                                         sw_if_index0);
2069
2070             /* 
2071              * Destination address is a solicited node multicast address.  
2072              * We need to fill in
2073              * the low 24 bits with low 24 bits of target's address. 
2074              */
2075             h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
2076             h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
2077             h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
2078
2079             h0->neighbor.target_address = ip0->dst_address;
2080
2081             clib_memcpy (h0->link_layer_option.ethernet_address, 
2082                     hw_if0->hw_address, vec_len (hw_if0->hw_address));
2083
2084             /* $$$$ appears we need this; why is the checksum non-zero? */
2085             h0->neighbor.icmp.checksum = 0;
2086             h0->neighbor.icmp.checksum = 
2087               ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip, 
2088                                                  &bogus_length);
2089
2090             ASSERT (bogus_length == 0);
2091
2092             vlib_buffer_copy_trace_flag (vm, p0, bi0);
2093             b0 = vlib_get_buffer (vm, bi0);
2094             vnet_buffer (b0)->sw_if_index[VLIB_TX] 
2095               = vnet_buffer (p0)->sw_if_index[VLIB_TX];
2096
2097             /* Add rewrite/encap string. */
2098             vnet_rewrite_one_header (adj0[0], h0, 
2099                                      sizeof (ethernet_header_t));
2100             vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
2101
2102             next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
2103
2104             vlib_set_next_frame_buffer (vm, node, next0, bi0);
2105           }
2106         }
2107
2108       vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP, 
2109                            n_left_to_next_drop);
2110     }
2111
2112   return frame->n_vectors;
2113 }
2114
2115 static char * ip6_discover_neighbor_error_strings[] = {
2116   [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
2117   [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] 
2118   = "neighbor solicitations sent",
2119 };
2120
2121 VLIB_REGISTER_NODE (ip6_discover_neighbor_node) = {
2122   .function = ip6_discover_neighbor,
2123   .name = "ip6-discover-neighbor",
2124   .vector_size = sizeof (u32),
2125
2126   .format_trace = format_ip6_forward_next_trace,
2127
2128   .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
2129   .error_strings = ip6_discover_neighbor_error_strings,
2130
2131   .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
2132   .next_nodes = {
2133     [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "error-drop",
2134     [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "interface-output",
2135   },
2136 };
2137
2138 clib_error_t *
2139 ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst, u32 sw_if_index)
2140 {
2141   vnet_main_t * vnm = vnet_get_main();
2142   ip6_main_t * im = &ip6_main;
2143   icmp6_neighbor_solicitation_header_t * h;
2144   ip6_address_t * src;
2145   ip_interface_address_t * ia;
2146   ip_adjacency_t * adj;
2147   vnet_hw_interface_t * hi;
2148   vnet_sw_interface_t * si;
2149   vlib_buffer_t * b;
2150   u32 bi = 0;
2151   int bogus_length;
2152
2153   si = vnet_get_sw_interface (vnm, sw_if_index);
2154
2155   if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
2156     {
2157       return clib_error_return (0, "%U: interface %U down",
2158                                 format_ip6_address, dst, 
2159                                 format_vnet_sw_if_index_name, vnm, 
2160                                 sw_if_index);
2161     }
2162
2163   src = ip6_interface_address_matching_destination (im, dst, sw_if_index, &ia);
2164   if (! src)
2165     {
2166       vnm->api_errno = VNET_API_ERROR_NO_MATCHING_INTERFACE;
2167       return clib_error_return 
2168         (0, "no matching interface address for destination %U (interface %U)",
2169          format_ip6_address, dst,
2170          format_vnet_sw_if_index_name, vnm, sw_if_index);
2171     }
2172
2173   h = vlib_packet_template_get_packet (vm, &im->discover_neighbor_packet_template, &bi);
2174
2175   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
2176
2177   /* Destination address is a solicited node multicast address.  We need to fill in
2178      the low 24 bits with low 24 bits of target's address. */
2179   h->ip.dst_address.as_u8[13] = dst->as_u8[13];
2180   h->ip.dst_address.as_u8[14] = dst->as_u8[14];
2181   h->ip.dst_address.as_u8[15] = dst->as_u8[15];
2182
2183   h->ip.src_address = src[0];
2184   h->neighbor.target_address = dst[0];
2185
2186   clib_memcpy (h->link_layer_option.ethernet_address, hi->hw_address, vec_len (hi->hw_address));
2187
2188   h->neighbor.icmp.checksum = 
2189     ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
2190   ASSERT(bogus_length == 0);
2191
2192   b = vlib_get_buffer (vm, bi);
2193   vnet_buffer (b)->sw_if_index[VLIB_RX] = vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
2194
2195   /* Add encapsulation string for software interface (e.g. ethernet header). */
2196   adj = ip_get_adjacency (&im->lookup_main, ia->neighbor_probe_adj_index);
2197   vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
2198   vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
2199
2200   {
2201     vlib_frame_t * f = vlib_get_frame_to_node (vm, hi->output_node_index);
2202     u32 * to_next = vlib_frame_vector_args (f);
2203     to_next[0] = bi;
2204     f->n_vectors = 1;
2205     vlib_put_frame_to_node (vm, hi->output_node_index, f);
2206   }
2207
2208   return /* no error */ 0;
2209 }
2210
2211 typedef enum {
2212   IP6_REWRITE_NEXT_DROP,
2213 } ip6_rewrite_next_t;
2214
2215 always_inline uword
2216 ip6_rewrite_inline (vlib_main_t * vm,
2217                     vlib_node_runtime_t * node,
2218                     vlib_frame_t * frame,
2219                     int rewrite_for_locally_received_packets)
2220 {
2221   ip_lookup_main_t * lm = &ip6_main.lookup_main;
2222   u32 * from = vlib_frame_vector_args (frame);
2223   u32 n_left_from, n_left_to_next, * to_next, next_index;
2224   vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip6_input_node.index);
2225   vlib_rx_or_tx_t adj_rx_tx = rewrite_for_locally_received_packets ? VLIB_RX : VLIB_TX;
2226
2227   n_left_from = frame->n_vectors;
2228   next_index = node->cached_next_index;
2229   u32 cpu_index = os_get_cpu_number();
2230   
2231   while (n_left_from > 0)
2232     {
2233       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2234
2235       while (n_left_from >= 4 && n_left_to_next >= 2)
2236         {
2237           ip_adjacency_t * adj0, * adj1;
2238           vlib_buffer_t * p0, * p1;
2239           ip6_header_t * ip0, * ip1;
2240           u32 pi0, rw_len0, next0, error0, adj_index0;
2241           u32 pi1, rw_len1, next1, error1, adj_index1;
2242       
2243           /* Prefetch next iteration. */
2244           {
2245             vlib_buffer_t * p2, * p3;
2246
2247             p2 = vlib_get_buffer (vm, from[2]);
2248             p3 = vlib_get_buffer (vm, from[3]);
2249
2250             vlib_prefetch_buffer_header (p2, LOAD);
2251             vlib_prefetch_buffer_header (p3, LOAD);
2252
2253             CLIB_PREFETCH (p2->pre_data, 32, STORE);
2254             CLIB_PREFETCH (p3->pre_data, 32, STORE);
2255
2256             CLIB_PREFETCH (p2->data, sizeof (ip0[0]), STORE);
2257             CLIB_PREFETCH (p3->data, sizeof (ip0[0]), STORE);
2258           }
2259
2260           pi0 = to_next[0] = from[0];
2261           pi1 = to_next[1] = from[1];
2262
2263           from += 2;
2264           n_left_from -= 2;
2265           to_next += 2;
2266           n_left_to_next -= 2;
2267       
2268           p0 = vlib_get_buffer (vm, pi0);
2269           p1 = vlib_get_buffer (vm, pi1);
2270
2271           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2272           adj_index1 = vnet_buffer (p1)->ip.adj_index[adj_rx_tx];
2273
2274           /* We should never rewrite a pkt using the MISS adjacency */
2275           ASSERT(adj_index0 && adj_index1);
2276
2277           ip0 = vlib_buffer_get_current (p0);
2278           ip1 = vlib_buffer_get_current (p1);
2279
2280           error0 = error1 = IP6_ERROR_NONE;
2281
2282           if (! rewrite_for_locally_received_packets)
2283             {
2284               i32 hop_limit0 = ip0->hop_limit, hop_limit1 = ip1->hop_limit;
2285
2286               /* Input node should have reject packets with hop limit 0. */
2287               ASSERT (ip0->hop_limit > 0);
2288               ASSERT (ip1->hop_limit > 0);
2289
2290               hop_limit0 -= 1;
2291               hop_limit1 -= 1;
2292
2293               ip0->hop_limit = hop_limit0;
2294               ip1->hop_limit = hop_limit1;
2295
2296               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2297               error1 = hop_limit1 <= 0 ? IP6_ERROR_TIME_EXPIRED : error1;
2298             }
2299
2300           adj0 = ip_get_adjacency (lm, adj_index0);
2301           adj1 = ip_get_adjacency (lm, adj_index1);
2302
2303           if (rewrite_for_locally_received_packets)
2304             {
2305               /*
2306                * If someone sends e.g. an icmp6 w/ src = dst = interface addr,
2307                * we end up here with a local adjacency in hand
2308                */
2309               if (PREDICT_FALSE(adj0->lookup_next_index 
2310                                 == IP_LOOKUP_NEXT_LOCAL))
2311                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2312               if (PREDICT_FALSE(adj1->lookup_next_index 
2313                                 == IP_LOOKUP_NEXT_LOCAL))
2314                 error1 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2315             }
2316
2317           rw_len0 = adj0[0].rewrite_header.data_bytes;
2318           rw_len1 = adj1[0].rewrite_header.data_bytes;
2319
2320           vlib_increment_combined_counter (&lm->adjacency_counters,
2321                                            cpu_index, 
2322                                            adj_index0,
2323                                            /* packet increment */ 0,
2324                                            /* byte increment */ rw_len0);
2325           vlib_increment_combined_counter (&lm->adjacency_counters,
2326                                            cpu_index, 
2327                                            adj_index1,
2328                                            /* packet increment */ 0,
2329                                            /* byte increment */ rw_len1);
2330
2331           /* Check MTU of outgoing interface. */
2332           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2333                     ? IP6_ERROR_MTU_EXCEEDED
2334                     : error0);
2335           error1 = (vlib_buffer_length_in_chain (vm, p1) > adj1[0].rewrite_header.max_l3_packet_bytes
2336                     ? IP6_ERROR_MTU_EXCEEDED
2337                     : error1);
2338
2339           p0->current_data -= rw_len0;
2340           p1->current_data -= rw_len1;
2341
2342           p0->current_length += rw_len0;
2343           p1->current_length += rw_len1;
2344
2345           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2346           vnet_buffer (p1)->sw_if_index[VLIB_TX] = adj1[0].rewrite_header.sw_if_index;
2347       
2348           next0 = (error0 == IP6_ERROR_NONE) ? 
2349             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2350           next1 = (error1 == IP6_ERROR_NONE) ? 
2351             adj1[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2352
2353           /* Guess we are only writing on simple Ethernet header. */
2354           vnet_rewrite_two_headers (adj0[0], adj1[0],
2355                                     ip0, ip1,
2356                                     sizeof (ethernet_header_t));
2357       
2358           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
2359                                            to_next, n_left_to_next,
2360                                            pi0, pi1, next0, next1);
2361         }
2362
2363       while (n_left_from > 0 && n_left_to_next > 0)
2364         {
2365           ip_adjacency_t * adj0;
2366           vlib_buffer_t * p0;
2367           ip6_header_t * ip0;
2368           u32 pi0, rw_len0;
2369           u32 adj_index0, next0, error0;
2370       
2371           pi0 = to_next[0] = from[0];
2372
2373           p0 = vlib_get_buffer (vm, pi0);
2374
2375           adj_index0 = vnet_buffer (p0)->ip.adj_index[adj_rx_tx];
2376
2377           /* We should never rewrite a pkt using the MISS adjacency */
2378           ASSERT(adj_index0);
2379
2380           adj0 = ip_get_adjacency (lm, adj_index0);
2381       
2382           ip0 = vlib_buffer_get_current (p0);
2383
2384           error0 = IP6_ERROR_NONE;
2385
2386           /* Check hop limit */
2387           if (! rewrite_for_locally_received_packets)
2388             {
2389               i32 hop_limit0 = ip0->hop_limit;
2390
2391               ASSERT (ip0->hop_limit > 0);
2392
2393               hop_limit0 -= 1;
2394
2395               ip0->hop_limit = hop_limit0;
2396
2397               error0 = hop_limit0 <= 0 ? IP6_ERROR_TIME_EXPIRED : error0;
2398             }
2399
2400           if (rewrite_for_locally_received_packets)
2401             {
2402               if (PREDICT_FALSE(adj0->lookup_next_index 
2403                                 == IP_LOOKUP_NEXT_LOCAL))
2404                 error0 = IP6_ERROR_SPOOFED_LOCAL_PACKETS;
2405             }
2406
2407           /* Guess we are only writing on simple Ethernet header. */
2408           vnet_rewrite_one_header (adj0[0], ip0, sizeof (ethernet_header_t));
2409       
2410           /* Update packet buffer attributes/set output interface. */
2411           rw_len0 = adj0[0].rewrite_header.data_bytes;
2412
2413           vlib_increment_combined_counter (&lm->adjacency_counters,
2414                                            cpu_index, 
2415                                            adj_index0,
2416                                            /* packet increment */ 0,
2417                                            /* byte increment */ rw_len0);
2418
2419           /* Check MTU of outgoing interface. */
2420           error0 = (vlib_buffer_length_in_chain (vm, p0) > adj0[0].rewrite_header.max_l3_packet_bytes
2421                     ? IP6_ERROR_MTU_EXCEEDED
2422                     : error0);
2423
2424           p0->current_data -= rw_len0;
2425           p0->current_length += rw_len0;
2426           vnet_buffer (p0)->sw_if_index[VLIB_TX] = adj0[0].rewrite_header.sw_if_index;
2427       
2428           next0 = (error0 == IP6_ERROR_NONE) ?
2429             adj0[0].rewrite_header.next_index : IP6_REWRITE_NEXT_DROP;
2430
2431           p0->error = error_node->errors[error0];
2432
2433           from += 1;
2434           n_left_from -= 1;
2435           to_next += 1;
2436           n_left_to_next -= 1;
2437       
2438           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2439                                            to_next, n_left_to_next,
2440                                            pi0, next0);
2441         }
2442
2443       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2444     }
2445
2446   /* Need to do trace after rewrites to pick up new packet data. */
2447   if (node->flags & VLIB_NODE_FLAG_TRACE)
2448     ip6_forward_next_trace (vm, node, frame, adj_rx_tx);
2449
2450   return frame->n_vectors;
2451 }
2452
2453 static uword
2454 ip6_rewrite_transit (vlib_main_t * vm,
2455                      vlib_node_runtime_t * node,
2456                      vlib_frame_t * frame)
2457 {
2458   return ip6_rewrite_inline (vm, node, frame,
2459                              /* rewrite_for_locally_received_packets */ 0);
2460 }
2461
2462 static uword
2463 ip6_rewrite_local (vlib_main_t * vm,
2464                    vlib_node_runtime_t * node,
2465                    vlib_frame_t * frame)
2466 {
2467   return ip6_rewrite_inline (vm, node, frame,
2468                              /* rewrite_for_locally_received_packets */ 1);
2469 }
2470
2471 VLIB_REGISTER_NODE (ip6_rewrite_node) = {
2472   .function = ip6_rewrite_transit,
2473   .name = "ip6-rewrite",
2474   .vector_size = sizeof (u32),
2475
2476   .format_trace = format_ip6_rewrite_trace,
2477
2478   .n_next_nodes = 1,
2479   .next_nodes = {
2480     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2481   },
2482 };
2483
2484 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_node, ip6_rewrite_transit)
2485
2486 VLIB_REGISTER_NODE (ip6_rewrite_local_node) = {
2487   .function = ip6_rewrite_local,
2488   .name = "ip6-rewrite-local",
2489   .vector_size = sizeof (u32),
2490
2491   .sibling_of = "ip6-rewrite",
2492
2493   .format_trace = format_ip6_rewrite_trace,
2494
2495   .n_next_nodes = 1,
2496   .next_nodes = {
2497     [IP6_REWRITE_NEXT_DROP] = "error-drop",
2498   },
2499 };
2500
2501 VLIB_NODE_FUNCTION_MULTIARCH (ip6_rewrite_local_node, ip6_rewrite_local)
2502
2503 /*
2504  * Hop-by-Hop handling
2505  */
2506
2507 ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
2508
2509 #define foreach_ip6_hop_by_hop_error \
2510 _(PROCESSED, "pkts with ip6 hop-by-hop options") \
2511 _(FORMAT, "incorrectly formatted hop-by-hop options") \
2512 _(UNKNOWN_OPTION, "unknown ip6 hop-by-hop options")
2513
2514 typedef enum {
2515 #define _(sym,str) IP6_HOP_BY_HOP_ERROR_##sym,
2516   foreach_ip6_hop_by_hop_error
2517 #undef _
2518   IP6_HOP_BY_HOP_N_ERROR,
2519 } ip6_hop_by_hop_error_t;
2520
2521 /*
2522  * Primary h-b-h handler trace support
2523  * We work pretty hard on the problem for obvious reasons
2524  */
2525 typedef struct {
2526   u32 next_index;
2527   u32 trace_len;
2528   u8 option_data[256];
2529 } ip6_hop_by_hop_trace_t;
2530
2531 vlib_node_registration_t ip6_hop_by_hop_node;
2532
2533 static char * ip6_hop_by_hop_error_strings[] = {
2534 #define _(sym,string) string,
2535   foreach_ip6_hop_by_hop_error
2536 #undef _
2537 };
2538
2539 static u8 *
2540 format_ip6_hop_by_hop_trace (u8 * s, va_list * args)
2541 {
2542   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
2543   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
2544   ip6_hop_by_hop_trace_t * t = va_arg (*args, ip6_hop_by_hop_trace_t *);
2545   ip6_hop_by_hop_header_t *hbh0;
2546   ip6_hop_by_hop_option_t *opt0, *limit0;
2547   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2548
2549   u8 type0;
2550
2551   hbh0 = (ip6_hop_by_hop_header_t *)t->option_data;
2552
2553   s = format (s, "IP6_HOP_BY_HOP: next index %d len %d traced %d",
2554               t->next_index, (hbh0->length+1)<<3, t->trace_len);
2555
2556   opt0 = (ip6_hop_by_hop_option_t *) (hbh0+1);
2557   limit0 = (ip6_hop_by_hop_option_t *) ((u8 *)hbh0) + t->trace_len;
2558
2559   while (opt0 < limit0) {
2560     type0 = opt0->type;
2561     switch (type0) {
2562     case 0: /* Pad, just stop */
2563       opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2564       break;
2565
2566     default:
2567       if (hm->trace[type0]) {
2568         s = (*hm->trace[type0])(s, opt0);
2569       } else {
2570         s = format (s, "\n    unrecognized option %d length %d", type0, opt0->length);
2571       }
2572       opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2573       break;
2574     }
2575   }
2576   return s;
2577 }
2578
2579 /*
2580  * Process the Hop-by-Hop Options header
2581  */
2582 static uword
2583 ip6_hop_by_hop (vlib_main_t * vm,
2584                 vlib_node_runtime_t * node,
2585                 vlib_frame_t * frame)
2586 {
2587   vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip6_hop_by_hop_node.index);
2588   ip6_hop_by_hop_main_t *hm = &ip6_hop_by_hop_main;
2589   u32 n_left_from, *from, *to_next;
2590   ip_lookup_next_t next_index;
2591   ip6_main_t * im = &ip6_main;
2592   ip_lookup_main_t *lm = &im->lookup_main;
2593
2594   from = vlib_frame_vector_args (frame);
2595   n_left_from = frame->n_vectors;
2596   next_index = node->cached_next_index;
2597
2598   while (n_left_from > 0) {
2599     u32 n_left_to_next;
2600
2601     vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2602
2603     while (n_left_from > 0 && n_left_to_next > 0) {
2604       u32 bi0;
2605       vlib_buffer_t * b0;
2606       u32 next0;
2607       ip6_header_t * ip0;
2608       ip6_hop_by_hop_header_t *hbh0;
2609       ip6_hop_by_hop_option_t *opt0, *limit0;
2610       u8 type0;
2611       u8 error0 = 0;
2612
2613       /* Speculatively enqueue b0 to the current next frame */
2614       bi0 = from[0];
2615       to_next[0] = bi0;
2616       from += 1;
2617       to_next += 1;
2618       n_left_from -= 1;
2619       n_left_to_next -= 1;
2620
2621       b0 = vlib_get_buffer (vm, bi0);
2622       u32 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
2623       ip_adjacency_t *adj0 = ip_get_adjacency(lm, adj_index0);
2624       /* Default use the next_index from the adjacency. A HBH option rarely redirects to a different node */
2625       next0 = adj0->lookup_next_index;
2626
2627       ip0 = vlib_buffer_get_current (b0);
2628       hbh0 = (ip6_hop_by_hop_header_t *)(ip0+1);
2629       opt0 = (ip6_hop_by_hop_option_t *)(hbh0+1);
2630       limit0 = (ip6_hop_by_hop_option_t *)((u8 *)hbh0 + ((hbh0->length + 1) << 3));
2631
2632       /*
2633        * Basic validity checks
2634        */
2635       if ((hbh0->length + 1) << 3 > clib_net_to_host_u16(ip0->payload_length)) {
2636         error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2637         next0 = IP_LOOKUP_NEXT_DROP;
2638         goto out0;
2639       }
2640
2641       /* Scan the set of h-b-h options, process ones that we understand */
2642       while (opt0 < limit0) {
2643         type0 = opt0->type;
2644         switch (type0) {
2645         case 0: /* Pad1 */
2646           opt0 = (ip6_hop_by_hop_option_t *) ((u8 *)opt0) + 1;
2647           continue;
2648         case 1: /* PadN */
2649           break;
2650         default:
2651           if (hm->options[type0]) {
2652             if ((*hm->options[type0])(b0, ip0, opt0) < 0) {
2653               error0 = IP6_HOP_BY_HOP_ERROR_FORMAT;
2654               goto out0;
2655             }
2656           } else {
2657             /* Unrecognized mandatory option, check the two high order bits */
2658             switch (opt0->type & HBH_OPTION_TYPE_HIGH_ORDER_BITS) {
2659             case HBH_OPTION_TYPE_SKIP_UNKNOWN:
2660               break;
2661             case HBH_OPTION_TYPE_DISCARD_UNKNOWN:
2662               next0 = IP_LOOKUP_NEXT_DROP;
2663               break;
2664             case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP:
2665               next0 = IP_LOOKUP_NEXT_ICMP_ERROR;
2666               icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2667                                           ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2668               break;
2669             case HBH_OPTION_TYPE_DISCARD_UNKNOWN_ICMP_NOT_MCAST:
2670               if (!ip6_address_is_multicast(&ip0->dst_address)) {
2671                 next0 =  IP_LOOKUP_NEXT_ICMP_ERROR;
2672                 icmp6_error_set_vnet_buffer(b0, ICMP6_parameter_problem,
2673                                             ICMP6_parameter_problem_unrecognized_option, (u8 *)opt0 - (u8 *)ip0);
2674               } else {
2675                 next0 =  IP_LOOKUP_NEXT_DROP;
2676               }
2677               break;
2678             }
2679             error0 = IP6_HOP_BY_HOP_ERROR_UNKNOWN_OPTION;
2680             goto out0;
2681           }
2682         }
2683         opt0 = (ip6_hop_by_hop_option_t *) (((u8 *)opt0) + opt0->length + sizeof (ip6_hop_by_hop_option_t));
2684       }
2685
2686     out0:
2687       /* Has the classifier flagged this buffer for special treatment? */
2688       if ((error0 == 0) && (vnet_buffer(b0)->l2_classify.opaque_index == OI_DECAP))
2689         next0 = IP_LOOKUP_NEXT_POP_HOP_BY_HOP;
2690
2691       if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) {
2692         ip6_hop_by_hop_trace_t *t = vlib_add_trace(vm, node, b0, sizeof (*t));
2693         u32 trace_len = (hbh0->length + 1) << 3;
2694         t->next_index = next0;
2695         /* Capture the h-b-h option verbatim */
2696         trace_len = trace_len < ARRAY_LEN(t->option_data) ? trace_len : ARRAY_LEN(t->option_data);
2697         t->trace_len = trace_len;
2698         clib_memcpy(t->option_data, hbh0, trace_len);
2699       }
2700
2701       b0->error = error_node->errors[error0];
2702
2703       /* verify speculative enqueue, maybe switch current next frame */
2704       vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0);
2705     }
2706     vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2707   }
2708   return frame->n_vectors;
2709 }
2710
2711 VLIB_REGISTER_NODE (ip6_hop_by_hop_node) = {
2712   .function = ip6_hop_by_hop,
2713   .name = "ip6-hop-by-hop",
2714   .vector_size = sizeof (u32),
2715   .format_trace = format_ip6_hop_by_hop_trace,
2716   .type = VLIB_NODE_TYPE_INTERNAL,
2717   .n_errors = ARRAY_LEN(ip6_hop_by_hop_error_strings),
2718   .error_strings = ip6_hop_by_hop_error_strings,
2719   .n_next_nodes = IP_LOOKUP_N_NEXT,
2720   .next_nodes = IP6_LOOKUP_NEXT_NODES,
2721 };
2722
2723 VLIB_NODE_FUNCTION_MULTIARCH (ip6_hop_by_hop_node, ip6_hop_by_hop)
2724
2725 static clib_error_t *
2726 ip6_hop_by_hop_init (vlib_main_t * vm)
2727 {
2728   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2729   memset(hm->options, 0, sizeof(hm->options));
2730   memset(hm->trace, 0, sizeof(hm->trace));
2731
2732   return (0);
2733 }
2734
2735 VLIB_INIT_FUNCTION (ip6_hop_by_hop_init);
2736
2737 int
2738 ip6_hbh_register_option (u8 option,
2739                          int options(vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt),
2740                          u8 *trace(u8 *s, ip6_hop_by_hop_option_t *opt))
2741 {
2742   ip6_main_t * im = &ip6_main;
2743   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2744
2745   ASSERT (option < ARRAY_LEN (hm->options));
2746
2747   /* Already registered */
2748   if (hm->options[option])
2749     return (-1);
2750
2751   hm->options[option] = options;
2752   hm->trace[option] = trace;
2753
2754   /* Set global variable */
2755   im->hbh_enabled = 1;
2756
2757   return (0);
2758 }
2759
2760 int
2761 ip6_hbh_unregister_option (u8 option)
2762 {
2763   ip6_main_t * im = &ip6_main;
2764   ip6_hop_by_hop_main_t * hm = &ip6_hop_by_hop_main;
2765
2766   ASSERT (option < ARRAY_LEN (hm->options));
2767
2768   /* Not registered */
2769   if (!hm->options[option])
2770     return (-1);
2771
2772   hm->options[option] = NULL;
2773   hm->trace[option] = NULL;
2774
2775   /* Disable global knob if this was the last option configured */
2776   int i;
2777   bool found = false;
2778   for (i = 0; i < 256; i++) {
2779     if (hm->options[option]) {
2780       found = true;
2781       break;
2782     }
2783   }
2784   if (!found)
2785     im->hbh_enabled = 0;
2786
2787   return (0);
2788 }
2789
2790 /* Global IP6 main. */
2791 ip6_main_t ip6_main;
2792
2793 static clib_error_t *
2794 ip6_lookup_init (vlib_main_t * vm)
2795 {
2796   ip6_main_t * im = &ip6_main;
2797   uword i;
2798
2799   for (i = 0; i < ARRAY_LEN (im->fib_masks); i++)
2800     {
2801       u32 j, i0, i1;
2802
2803       i0 = i / 32;
2804       i1 = i % 32;
2805
2806       for (j = 0; j < i0; j++)
2807         im->fib_masks[i].as_u32[j] = ~0;
2808
2809       if (i1)
2810         im->fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
2811     }
2812
2813   ip_lookup_init (&im->lookup_main, /* is_ip6 */ 1);
2814
2815   if (im->lookup_table_nbuckets == 0)
2816     im->lookup_table_nbuckets = IP6_FIB_DEFAULT_HASH_NUM_BUCKETS;
2817
2818   im->lookup_table_nbuckets = 1<< max_log2 (im->lookup_table_nbuckets);
2819
2820   if (im->lookup_table_size == 0)
2821     im->lookup_table_size = IP6_FIB_DEFAULT_HASH_MEMORY_SIZE;
2822   
2823   BV(clib_bihash_init) (&im->ip6_lookup_table, "ip6 lookup table",
2824                         im->lookup_table_nbuckets,
2825                         im->lookup_table_size);
2826   
2827   /* Create FIB with index 0 and table id of 0. */
2828   find_ip6_fib_by_table_index_or_id (im, /* table id */ 0, IP6_ROUTE_FLAG_TABLE_ID);
2829
2830   {
2831     pg_node_t * pn;
2832     pn = pg_get_node (ip6_lookup_node.index);
2833     pn->unformat_edit = unformat_pg_ip6_header;
2834   }
2835
2836   /* Unless explicitly configured, don't process HBH options */
2837   im->hbh_enabled = 0;
2838
2839   {
2840     icmp6_neighbor_solicitation_header_t p;
2841
2842     memset (&p, 0, sizeof (p));
2843
2844     p.ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2845     p.ip.payload_length = clib_host_to_net_u16 (sizeof (p)
2846                                                 - STRUCT_OFFSET_OF (icmp6_neighbor_solicitation_header_t, neighbor));
2847     p.ip.protocol = IP_PROTOCOL_ICMP6;
2848     p.ip.hop_limit = 255;
2849     ip6_set_solicited_node_multicast_address (&p.ip.dst_address, 0);
2850
2851     p.neighbor.icmp.type = ICMP6_neighbor_solicitation;
2852
2853     p.link_layer_option.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2854     p.link_layer_option.header.n_data_u64s = sizeof (p.link_layer_option) / sizeof (u64);
2855
2856     vlib_packet_template_init (vm,
2857                                &im->discover_neighbor_packet_template,
2858                                &p, sizeof (p),
2859                                /* alloc chunk size */ 8,
2860                                "ip6 neighbor discovery");
2861   }
2862
2863   return 0;
2864 }
2865
2866 VLIB_INIT_FUNCTION (ip6_lookup_init);
2867
2868 static clib_error_t *
2869 add_del_ip6_interface_table (vlib_main_t * vm,
2870                              unformat_input_t * input,
2871                              vlib_cli_command_t * cmd)
2872 {
2873   vnet_main_t * vnm = vnet_get_main();
2874   clib_error_t * error = 0;
2875   u32 sw_if_index, table_id;
2876
2877   sw_if_index = ~0;
2878
2879   if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
2880     {
2881       error = clib_error_return (0, "unknown interface `%U'",
2882                                  format_unformat_error, input);
2883       goto done;
2884     }
2885
2886   if (unformat (input, "%d", &table_id))
2887     ;
2888   else
2889     {
2890       error = clib_error_return (0, "expected table id `%U'",
2891                                  format_unformat_error, input);
2892       goto done;
2893     }
2894
2895   {
2896     ip6_main_t * im = &ip6_main;
2897     ip6_fib_t * fib = 
2898       find_ip6_fib_by_table_index_or_id (im, table_id, IP6_ROUTE_FLAG_TABLE_ID);
2899
2900     if (fib) 
2901       {
2902         vec_validate (im->fib_index_by_sw_if_index, sw_if_index);
2903         im->fib_index_by_sw_if_index[sw_if_index] = fib->index;
2904     }
2905   }
2906
2907  done:
2908   return error;
2909 }
2910
2911 VLIB_CLI_COMMAND (set_interface_ip_table_command, static) = {
2912   .path = "set interface ip6 table",
2913   .function = add_del_ip6_interface_table,
2914   .short_help = "set interface ip6 table <intfc> <table-id>"
2915 };
2916
2917 void 
2918 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t *ip,
2919                                                   u8 *mac)
2920 {
2921   ip->as_u64[0] = clib_host_to_net_u64 (0xFE80000000000000ULL);
2922   /* Invert the "u" bit */
2923   ip->as_u8 [8] = mac[0] ^ (1<<1);
2924   ip->as_u8 [9] = mac[1];
2925   ip->as_u8 [10] = mac[2];
2926   ip->as_u8 [11] = 0xFF;
2927   ip->as_u8 [12] = 0xFE;
2928   ip->as_u8 [13] = mac[3];
2929   ip->as_u8 [14] = mac[4];
2930   ip->as_u8 [15] = mac[5];
2931 }
2932
2933 void 
2934 ip6_ethernet_mac_address_from_link_local_address (u8 *mac, 
2935                                                   ip6_address_t *ip)
2936 {
2937   /* Invert the previously inverted "u" bit */
2938   mac[0] = ip->as_u8 [8] ^ (1<<1);
2939   mac[1] = ip->as_u8 [9];
2940   mac[2] = ip->as_u8 [10];
2941   mac[3] = ip->as_u8 [13];
2942   mac[4] = ip->as_u8 [14];
2943   mac[5] = ip->as_u8 [15];
2944 }
2945
2946 static clib_error_t * 
2947 test_ip6_link_command_fn (vlib_main_t * vm,
2948                           unformat_input_t * input,
2949                           vlib_cli_command_t * cmd)
2950 {
2951   u8 mac[6];
2952   ip6_address_t _a, *a = &_a;
2953
2954   if (unformat (input, "%U", unformat_ethernet_address, mac))
2955     {
2956       ip6_link_local_address_from_ethernet_mac_address (a, mac);
2957       vlib_cli_output (vm, "Link local address: %U",
2958                        format_ip6_address, a);
2959       ip6_ethernet_mac_address_from_link_local_address (mac, a);
2960       vlib_cli_output (vm, "Original MAC address: %U",
2961                        format_ethernet_address, mac);
2962     }
2963                 
2964   return 0;
2965 }
2966
2967 VLIB_CLI_COMMAND (test_link_command, static) = {
2968   .path = "test ip6 link",
2969   .function = test_ip6_link_command_fn, 
2970   .short_help = "test ip6 link <mac-address>",
2971 };
2972
2973 int vnet_set_ip6_flow_hash (u32 table_id, u32 flow_hash_config)
2974 {
2975   ip6_main_t * im6 = &ip6_main;
2976   ip6_fib_t * fib;
2977   uword * p = hash_get (im6->fib_index_by_table_id, table_id);
2978
2979   if (p == 0)
2980     return -1;
2981
2982   fib = vec_elt_at_index (im6->fibs, p[0]);
2983
2984   fib->flow_hash_config = flow_hash_config;
2985   return 1;
2986 }
2987
2988 static clib_error_t *
2989 set_ip6_flow_hash_command_fn (vlib_main_t * vm,
2990                               unformat_input_t * input,
2991                               vlib_cli_command_t * cmd)
2992 {
2993   int matched = 0;
2994   u32 table_id = 0;
2995   u32 flow_hash_config = 0;
2996   int rv;
2997
2998   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
2999     if (unformat (input, "table %d", &table_id))
3000       matched = 1;
3001 #define _(a,v) \
3002     else if (unformat (input, #a)) { flow_hash_config |= v; matched=1;}
3003     foreach_flow_hash_bit
3004 #undef _
3005     else break;
3006   }
3007
3008   if (matched == 0)
3009     return clib_error_return (0, "unknown input `%U'",
3010                               format_unformat_error, input);
3011   
3012   rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
3013   switch (rv)
3014     {
3015     case 1:
3016       break;
3017
3018     case -1:
3019       return clib_error_return (0, "no such FIB table %d", table_id);
3020       
3021     default:
3022       clib_warning ("BUG: illegal flow hash config 0x%x", flow_hash_config);
3023       break;
3024     }
3025   
3026   return 0;
3027 }
3028
3029 VLIB_CLI_COMMAND (set_ip6_flow_hash_command, static) = {
3030     .path = "set ip6 flow-hash",
3031     .short_help = 
3032     "set ip table flow-hash table <fib-id> src dst sport dport proto reverse",
3033     .function = set_ip6_flow_hash_command_fn,
3034 };
3035
3036 static clib_error_t *
3037 show_ip6_local_command_fn (vlib_main_t * vm,
3038                            unformat_input_t * input,
3039                            vlib_cli_command_t * cmd)
3040 {
3041   ip6_main_t * im = &ip6_main;
3042   ip_lookup_main_t * lm = &im->lookup_main;
3043   int i;
3044   
3045   vlib_cli_output (vm, "Protocols handled by ip6_local");
3046   for (i = 0; i < ARRAY_LEN(lm->local_next_by_ip_protocol); i++)
3047     {
3048       if (lm->local_next_by_ip_protocol[i] != IP_LOCAL_NEXT_PUNT)
3049         vlib_cli_output (vm, "%d", i);
3050     }
3051   return 0;
3052 }
3053
3054
3055
3056 VLIB_CLI_COMMAND (show_ip_local, static) = {
3057   .path = "show ip6 local",
3058   .function = show_ip6_local_command_fn,
3059   .short_help = "Show ip6 local protocol table",
3060 };
3061
3062 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index, 
3063                                  u32 table_index)
3064 {
3065   vnet_main_t * vnm = vnet_get_main();
3066   vnet_interface_main_t * im = &vnm->interface_main;
3067   ip6_main_t * ipm = &ip6_main;
3068   ip_lookup_main_t * lm = &ipm->lookup_main;
3069   vnet_classify_main_t * cm = &vnet_classify_main;
3070
3071   if (pool_is_free_index (im->sw_interfaces, sw_if_index))
3072     return VNET_API_ERROR_NO_MATCHING_INTERFACE;
3073
3074   if (table_index != ~0 && pool_is_free_index (cm->tables, table_index))
3075     return VNET_API_ERROR_NO_SUCH_ENTRY;
3076
3077   vec_validate (lm->classify_table_index_by_sw_if_index, sw_if_index);
3078   lm->classify_table_index_by_sw_if_index [sw_if_index] = table_index;
3079
3080   return 0;
3081 }
3082
3083 static clib_error_t *
3084 set_ip6_classify_command_fn (vlib_main_t * vm,
3085                              unformat_input_t * input,
3086                              vlib_cli_command_t * cmd)
3087 {
3088   u32 table_index = ~0;
3089   int table_index_set = 0;
3090   u32 sw_if_index = ~0;
3091   int rv;
3092   
3093   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3094     if (unformat (input, "table-index %d", &table_index))
3095       table_index_set = 1;
3096     else if (unformat (input, "intfc %U", unformat_vnet_sw_interface, 
3097                        vnet_get_main(), &sw_if_index))
3098         ;
3099     else
3100         break;
3101   }
3102   
3103   if (table_index_set == 0)
3104       return clib_error_return (0, "classify table-index must be specified");
3105   
3106   if (sw_if_index == ~0)
3107     return clib_error_return (0, "interface / subif must be specified");
3108
3109   rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index);
3110
3111   switch (rv)
3112     {
3113     case 0:
3114       break;
3115
3116     case VNET_API_ERROR_NO_MATCHING_INTERFACE:
3117       return clib_error_return (0, "No such interface");
3118
3119     case VNET_API_ERROR_NO_SUCH_ENTRY:
3120       return clib_error_return (0, "No such classifier table");
3121     }
3122   return 0;
3123 }
3124
3125 VLIB_CLI_COMMAND (set_ip6_classify_command, static) = {
3126     .path = "set ip6 classify",
3127     .short_help = 
3128     "set ip6 classify intfc <int> table-index <index>",
3129     .function = set_ip6_classify_command_fn,
3130 };
3131
3132 static clib_error_t *
3133 ip6_config (vlib_main_t * vm, unformat_input_t * input)
3134 {
3135   ip6_main_t * im = &ip6_main;
3136   uword heapsize = 0;
3137   u32 tmp;
3138   u32 nbuckets = 0;
3139
3140   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
3141     if (unformat (input, "hash-buckets %d", &tmp))
3142       nbuckets = tmp;
3143     else if (unformat (input, "heap-size %dm", &tmp))
3144       heapsize = ((u64)tmp) << 20;
3145     else if (unformat (input, "heap-size %dM", &tmp))
3146       heapsize = ((u64)tmp) << 20;
3147     else if (unformat (input, "heap-size %dg", &tmp))
3148       heapsize = ((u64)tmp) << 30;
3149     else if (unformat (input, "heap-size %dG", &tmp))
3150       heapsize = ((u64)tmp) << 30;
3151     else
3152       return clib_error_return (0, "unknown input '%U'",
3153                                 format_unformat_error, input);
3154   }
3155
3156   im->lookup_table_nbuckets = nbuckets;
3157   im->lookup_table_size = heapsize;
3158
3159   return 0;
3160 }
3161
3162 VLIB_EARLY_CONFIG_FUNCTION (ip6_config, "ip6");
3163