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