MTRIE Optimisations 2
[vpp.git] / src / vnet / ip / ip6.h
1 /*
2  * Copyright (c) 2016 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.h: ip6 main include file
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 #ifndef included_ip_ip6_h
41 #define included_ip_ip6_h
42
43 #include <vlib/mc.h>
44 #include <vlib/buffer.h>
45 #include <vnet/ethernet/packet.h>
46 #include <vnet/ip/ip6_packet.h>
47 #include <vnet/ip/ip6_hop_by_hop_packet.h>
48 #include <vnet/ip/lookup.h>
49 #include <stdbool.h>
50 #include <vppinfra/bihash_24_8.h>
51 #include <vppinfra/bihash_template.h>
52 #include <vnet/util/radix.h>
53
54 /*
55  * Default size of the ip6 fib hash table
56  */
57 #define IP6_FIB_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
58 #define IP6_FIB_DEFAULT_HASH_MEMORY_SIZE (32<<20)
59
60 typedef struct
61 {
62   ip6_address_t addr;
63   u32 dst_address_length;
64   u32 vrf_index;
65 } ip6_fib_key_t;
66
67 typedef struct
68 {
69   /* Table ID (hash key) for this FIB. */
70   u32 table_id;
71
72   /* Index into FIB vector. */
73   u32 index;
74
75   /* flow hash configuration */
76   flow_hash_config_t flow_hash_config;
77 } ip6_fib_t;
78
79 typedef struct ip6_mfib_t
80 {
81   /* Table ID (hash key) for this FIB. */
82   u32 table_id;
83
84   /* Index into FIB vector. */
85   u32 index;
86
87   /*
88    *  Pointer to the top of a radix tree.
89    * This cannot be realloc'd, hence it cannot be inlined with this table
90    */
91   struct radix_node_head *rhead;
92 } ip6_mfib_t;
93
94 struct ip6_main_t;
95
96 typedef void (ip6_add_del_interface_address_function_t)
97   (struct ip6_main_t * im,
98    uword opaque,
99    u32 sw_if_index,
100    ip6_address_t * address,
101    u32 address_length, u32 if_address_index, u32 is_del);
102
103 typedef struct
104 {
105   ip6_add_del_interface_address_function_t *function;
106   uword function_opaque;
107 } ip6_add_del_interface_address_callback_t;
108
109 /**
110  * Enumeration of the FIB table instance types
111  */
112 typedef enum ip6_fib_table_instance_type_t_
113 {
114     /**
115      * This table stores the routes that are used to forward traffic.
116      * The key is the prefix, the result the adjacnecy to forward on.
117      */
118   IP6_FIB_TABLE_FWDING,
119     /**
120      * The table that stores ALL routes learned by the DP.
121      * Some of these routes may not be ready to install in forwarding
122      * at a given time.
123      * The key in this table is the prefix, the result is the fib_entry_t
124      */
125   IP6_FIB_TABLE_NON_FWDING,
126 } ip6_fib_table_instance_type_t;
127
128 #define IP6_FIB_NUM_TABLES (IP6_FIB_TABLE_NON_FWDING+1)
129
130 /**
131  * A represenation of a single IP6 table
132  */
133 typedef struct ip6_fib_table_instance_t_
134 {
135   /* The hash table */
136   BVT (clib_bihash) ip6_hash;
137
138   /* bitmap / refcounts / vector of mask widths to search */
139   uword *non_empty_dst_address_length_bitmap;
140   u8 *prefix_lengths_in_search_order;
141   i32 dst_address_length_refcounts[129];
142 } ip6_fib_table_instance_t;
143
144 typedef struct ip6_main_t
145 {
146   /**
147    * The two FIB tables; fwding and non-fwding
148    */
149   ip6_fib_table_instance_t ip6_table[IP6_FIB_NUM_TABLES];
150
151   ip_lookup_main_t lookup_main;
152
153   /* Pool of FIBs. */
154   struct fib_table_t_ *fibs;
155
156   /* Pool of V6 FIBs. */
157   ip6_fib_t *v6_fibs;
158
159   /** Vector of MFIBs. */
160   struct mfib_table_t_ *mfibs;
161
162   /* Network byte orders subnet mask for each prefix length */
163   ip6_address_t fib_masks[129];
164
165   /* Table index indexed by software interface. */
166   u32 *fib_index_by_sw_if_index;
167
168   /** Table index indexed by software interface. */
169   u32 *mfib_index_by_sw_if_index;
170
171   /* IP6 enabled count by software interface */
172   u8 *ip_enabled_by_sw_if_index;
173
174   /* Hash table mapping table id to fib index.
175      ID space is not necessarily dense; index space is dense. */
176   uword *fib_index_by_table_id;
177
178   /** Hash table mapping table id to multicast fib index.
179      ID space is not necessarily dense; index space is dense. */
180   uword *mfib_index_by_table_id;
181
182   /* Hash table mapping interface rewrite adjacency index by sw if index. */
183   uword *interface_route_adj_index_by_sw_if_index;
184
185   /* Functions to call when interface address changes. */
186     ip6_add_del_interface_address_callback_t
187     * add_del_interface_address_callbacks;
188
189   /* Template used to generate IP6 neighbor solicitation packets. */
190   vlib_packet_template_t discover_neighbor_packet_template;
191
192   /* ip6 lookup table config parameters */
193   u32 lookup_table_nbuckets;
194   uword lookup_table_size;
195
196   /* Seed for Jenkins hash used to compute ip6 flow hash. */
197   u32 flow_hash_seed;
198
199   struct
200   {
201     /* TTL to use for host generated packets. */
202     u8 ttl;
203
204     u8 pad[3];
205   } host_config;
206
207   /* HBH processing enabled? */
208   u8 hbh_enabled;
209 } ip6_main_t;
210
211 /* Global ip6 main structure. */
212 extern ip6_main_t ip6_main;
213
214 /* Global ip6 input node.  Errors get attached to ip6 input node. */
215 extern vlib_node_registration_t ip6_input_node;
216 extern vlib_node_registration_t ip6_rewrite_node;
217 extern vlib_node_registration_t ip6_rewrite_mcast_node;
218 extern vlib_node_registration_t ip6_rewrite_local_node;
219 extern vlib_node_registration_t ip6_discover_neighbor_node;
220 extern vlib_node_registration_t ip6_glean_node;
221 extern vlib_node_registration_t ip6_midchain_node;
222
223 always_inline uword
224 ip6_destination_matches_route (const ip6_main_t * im,
225                                const ip6_address_t * key,
226                                const ip6_address_t * dest, uword dest_length)
227 {
228   int i;
229   for (i = 0; i < ARRAY_LEN (key->as_uword); i++)
230     {
231       if ((key->as_uword[i] ^ dest->as_uword[i]) & im->
232           fib_masks[dest_length].as_uword[i])
233         return 0;
234     }
235   return 1;
236 }
237
238 always_inline uword
239 ip6_destination_matches_interface (ip6_main_t * im,
240                                    ip6_address_t * key,
241                                    ip_interface_address_t * ia)
242 {
243   ip6_address_t *a = ip_interface_address_get_address (&im->lookup_main, ia);
244   return ip6_destination_matches_route (im, key, a, ia->address_length);
245 }
246
247 /* As above but allows for unaligned destinations (e.g. works right from IP header of packet). */
248 always_inline uword
249 ip6_unaligned_destination_matches_route (ip6_main_t * im,
250                                          ip6_address_t * key,
251                                          ip6_address_t * dest,
252                                          uword dest_length)
253 {
254   int i;
255   for (i = 0; i < ARRAY_LEN (key->as_uword); i++)
256     {
257       if ((clib_mem_unaligned (&key->as_uword[i], uword) ^ dest->as_uword[i])
258           & im->fib_masks[dest_length].as_uword[i])
259         return 0;
260     }
261   return 1;
262 }
263
264 always_inline int
265 ip6_src_address_for_packet (ip_lookup_main_t * lm,
266                             u32 sw_if_index, ip6_address_t * src)
267 {
268   u32 if_add_index = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
269   if (PREDICT_TRUE (if_add_index != ~0))
270     {
271       ip_interface_address_t *if_add =
272         pool_elt_at_index (lm->if_address_pool, if_add_index);
273       ip6_address_t *if_ip = ip_interface_address_get_address (lm, if_add);
274       *src = *if_ip;
275       return (0);
276     }
277   else
278     {
279       src->as_u64[0] = 0;
280       src->as_u64[1] = 0;
281     }
282   return (!0);
283 }
284
285 /* Find interface address which matches destination. */
286 always_inline ip6_address_t *
287 ip6_interface_address_matching_destination (ip6_main_t * im,
288                                             ip6_address_t * dst,
289                                             u32 sw_if_index,
290                                             ip_interface_address_t **
291                                             result_ia)
292 {
293   ip_lookup_main_t *lm = &im->lookup_main;
294   ip_interface_address_t *ia;
295   ip6_address_t *result = 0;
296
297   /* *INDENT-OFF* */
298   foreach_ip_interface_address (lm, ia, sw_if_index,
299                                 1 /* honor unnumbered */,
300   ({
301     ip6_address_t * a = ip_interface_address_get_address (lm, ia);
302     if (ip6_destination_matches_route (im, dst, a, ia->address_length))
303       {
304         result = a;
305         break;
306       }
307   }));
308   /* *INDENT-ON* */
309   if (result_ia)
310     *result_ia = result ? ia : 0;
311   return result;
312 }
313
314 clib_error_t *ip6_add_del_interface_address (vlib_main_t * vm,
315                                              u32 sw_if_index,
316                                              ip6_address_t * address,
317                                              u32 address_length, u32 is_del);
318 void ip6_sw_interface_enable_disable (u32 sw_if_index, u32 is_enable);
319
320 /**
321  * @brie get first IPv6 interface address
322  */
323 ip6_address_t *ip6_interface_first_address (ip6_main_t * im, u32 sw_if_index);
324
325 int ip6_address_compare (ip6_address_t * a1, ip6_address_t * a2);
326
327 clib_error_t *ip6_probe_neighbor (vlib_main_t * vm, ip6_address_t * dst,
328                                   u32 sw_if_index);
329
330 uword
331 ip6_udp_register_listener (vlib_main_t * vm,
332                            u16 dst_port, u32 next_node_index);
333
334 u16 ip6_tcp_udp_icmp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
335                                        ip6_header_t * ip0,
336                                        int *bogus_lengthp);
337
338 void ip6_register_protocol (u32 protocol, u32 node_index);
339
340 serialize_function_t serialize_vnet_ip6_main, unserialize_vnet_ip6_main;
341
342 void ip6_ethernet_update_adjacency (vnet_main_t * vnm,
343                                     u32 sw_if_index, u32 ai);
344
345
346 void
347 ip6_link_local_address_from_ethernet_mac_address (ip6_address_t * ip,
348                                                   u8 * mac);
349
350 void
351 ip6_ethernet_mac_address_from_link_local_address (u8 * mac,
352                                                   ip6_address_t * ip);
353
354 int vnet_set_ip6_flow_hash (u32 table_id,
355                             flow_hash_config_t flow_hash_config);
356
357 clib_error_t *enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index);
358
359 clib_error_t *disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index);
360
361 int ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index);
362
363 clib_error_t *set_ip6_link_local_address (vlib_main_t * vm,
364                                           u32 sw_if_index,
365                                           ip6_address_t * address);
366
367 int vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
368                                       void *data_callback,
369                                       u32 pid,
370                                       void *address_arg,
371                                       uword node_index,
372                                       uword type_opaque,
373                                       uword data, int is_add);
374
375 int vnet_ip6_nd_term (vlib_main_t * vm,
376                       vlib_node_runtime_t * node,
377                       vlib_buffer_t * p0,
378                       ethernet_header_t * eth,
379                       ip6_header_t * ip, u32 sw_if_index, u16 bd_index);
380
381 u8 *format_ip6_forward_next_trace (u8 * s, va_list * args);
382
383 u32 ip6_tcp_udp_icmp_validate_checksum (vlib_main_t * vm, vlib_buffer_t * p0);
384
385 int vnet_set_ip6_classify_intfc (vlib_main_t * vm, u32 sw_if_index,
386                                  u32 table_index);
387 extern vlib_node_registration_t ip6_lookup_node;
388
389 /* Compute flow hash.  We'll use it to select which Sponge to use for this
390    flow.  And other things. */
391 always_inline u32
392 ip6_compute_flow_hash (const ip6_header_t * ip,
393                        flow_hash_config_t flow_hash_config)
394 {
395   tcp_header_t *tcp;
396   u64 a, b, c;
397   u64 t1, t2;
398   uword is_tcp_udp = 0;
399   u8 protocol = ip->protocol;
400
401   if (PREDICT_TRUE
402       ((ip->protocol == IP_PROTOCOL_TCP)
403        || (ip->protocol == IP_PROTOCOL_UDP)))
404     {
405       is_tcp_udp = 1;
406       tcp = (void *) (ip + 1);
407     }
408   else if (ip->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)
409     {
410       ip6_hop_by_hop_header_t *hbh = (ip6_hop_by_hop_header_t *) (ip + 1);
411       if ((hbh->protocol == IP_PROTOCOL_TCP) ||
412           (hbh->protocol == IP_PROTOCOL_UDP))
413         {
414           is_tcp_udp = 1;
415           tcp = (tcp_header_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
416         }
417       protocol = hbh->protocol;
418     }
419
420   t1 = (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1]);
421   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_ADDR) ? t1 : 0;
422
423   t2 = (ip->dst_address.as_u64[0] ^ ip->dst_address.as_u64[1]);
424   t2 = (flow_hash_config & IP_FLOW_HASH_DST_ADDR) ? t2 : 0;
425
426   a = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t2 : t1;
427   b = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ? t1 : t2;
428   b ^= (flow_hash_config & IP_FLOW_HASH_PROTO) ? protocol : 0;
429
430   t1 = is_tcp_udp ? tcp->src : 0;
431   t2 = is_tcp_udp ? tcp->dst : 0;
432
433   t1 = (flow_hash_config & IP_FLOW_HASH_SRC_PORT) ? t1 : 0;
434   t2 = (flow_hash_config & IP_FLOW_HASH_DST_PORT) ? t2 : 0;
435
436   c = (flow_hash_config & IP_FLOW_HASH_REVERSE_SRC_DST) ?
437     ((t1 << 16) | t2) : ((t2 << 16) | t1);
438
439   hash_mix64 (a, b, c);
440   return (u32) c;
441 }
442
443 /* ip6_locate_header
444  *
445  * This function is to search for the header specified by the protocol number
446  * in find_hdr_type.
447  * This is used to locate a specific IPv6 extension header
448  * or to find transport layer header.
449  *   1. If the find_hdr_type < 0 then it finds and returns the protocol number and
450  *   offset stored in *offset of the transport or ESP header in the chain if
451  *   found.
452  *   2. If a header with find_hdr_type > 0 protocol number is found then the
453  *      offset is stored in *offset and protocol number of the header is
454  *      returned.
455  *   3. If find_hdr_type is not found or packet is malformed or
456  *      it is a non-first fragment -1 is returned.
457  */
458 always_inline int
459 ip6_locate_header (vlib_buffer_t * p0,
460                    ip6_header_t * ip0, int find_hdr_type, u32 * offset)
461 {
462   u8 next_proto = ip0->protocol;
463   u8 *next_header;
464   u8 done = 0;
465   u32 cur_offset;
466   u8 *temp_nxthdr = 0;
467   u32 exthdr_len = 0;
468
469   next_header = ip6_next_header (ip0);
470   cur_offset = sizeof (ip6_header_t);
471   while (1)
472     {
473       done = (next_proto == find_hdr_type);
474       if (PREDICT_FALSE
475           (next_header >=
476            (u8 *) vlib_buffer_get_current (p0) + p0->current_length))
477         {
478           //A malicious packet could set an extension header with a too big size
479           return (-1);
480         }
481       if (done)
482         break;
483       if ((!ip6_ext_hdr (next_proto)) || next_proto == IP_PROTOCOL_IP6_NONXT)
484         {
485           if (find_hdr_type < 0)
486             break;
487           return -1;
488         }
489       if (next_proto == IP_PROTOCOL_IPV6_FRAGMENTATION)
490         {
491           ip6_frag_hdr_t *frag_hdr = (ip6_frag_hdr_t *) next_header;
492           u16 frag_off = ip6_frag_hdr_offset (frag_hdr);
493           /* Non first fragment return -1 */
494           if (frag_off)
495             return (-1);
496           exthdr_len = sizeof (ip6_frag_hdr_t);
497           temp_nxthdr = next_header + exthdr_len;
498         }
499       else if (next_proto == IP_PROTOCOL_IPSEC_AH)
500         {
501           exthdr_len =
502             ip6_ext_authhdr_len (((ip6_ext_header_t *) next_header));
503           temp_nxthdr = next_header + exthdr_len;
504         }
505       else
506         {
507           exthdr_len =
508             ip6_ext_header_len (((ip6_ext_header_t *) next_header));
509           temp_nxthdr = next_header + exthdr_len;
510         }
511       next_proto = ((ip6_ext_header_t *) next_header)->next_hdr;
512       next_header = temp_nxthdr;
513       cur_offset += exthdr_len;
514     }
515
516   *offset = cur_offset;
517   return (next_proto);
518 }
519
520 u8 *format_ip6_hop_by_hop_ext_hdr (u8 * s, va_list * args);
521 /*
522  * Hop-by-Hop handling
523  */
524 typedef struct
525 {
526   /* Array of function pointers to HBH option handling routines */
527   int (*options[256]) (vlib_buffer_t * b, ip6_header_t * ip,
528                        ip6_hop_by_hop_option_t * opt);
529   u8 *(*trace[256]) (u8 * s, ip6_hop_by_hop_option_t * opt);
530   uword next_override;
531 } ip6_hop_by_hop_main_t;
532
533 extern ip6_hop_by_hop_main_t ip6_hop_by_hop_main;
534
535 int ip6_hbh_register_option (u8 option,
536                              int options (vlib_buffer_t * b,
537                                           ip6_header_t * ip,
538                                           ip6_hop_by_hop_option_t * opt),
539                              u8 * trace (u8 * s,
540                                          ip6_hop_by_hop_option_t * opt));
541 int ip6_hbh_unregister_option (u8 option);
542 void ip6_hbh_set_next_override (uword next);
543
544 /**
545  * Push IPv6 header to buffer
546  *
547  * @param vm - vlib_main
548  * @param b - buffer to write the header to
549  * @param src - source IP
550  * @param dst - destination IP
551  * @param prot - payload proto
552  *
553  * @return - pointer to start of IP header
554  */
555 always_inline void *
556 vlib_buffer_push_ip6 (vlib_main_t * vm, vlib_buffer_t * b,
557                       ip6_address_t * src, ip6_address_t * dst, int proto)
558 {
559   ip6_header_t *ip6h;
560   u16 payload_length;
561
562   /* make some room */
563   ip6h = vlib_buffer_push_uninit (b, sizeof (ip6_header_t));
564
565   ip6h->ip_version_traffic_class_and_flow_label =
566     clib_host_to_net_u32 (0x6 << 28);
567
568   /* calculate ip6 payload length */
569   payload_length = vlib_buffer_length_in_chain (vm, b);
570   payload_length -= sizeof (*ip6h);
571
572   ip6h->payload_length = clib_host_to_net_u16 (payload_length);
573
574   ip6h->hop_limit = 0xff;
575   ip6h->protocol = proto;
576   clib_memcpy (ip6h->src_address.as_u8, src->as_u8,
577                sizeof (ip6h->src_address));
578   clib_memcpy (ip6h->dst_address.as_u8, dst->as_u8,
579                sizeof (ip6h->src_address));
580
581   return ip6h;
582 }
583
584 #endif /* included_ip_ip6_h */
585
586 /*
587  * fd.io coding-style-patch-verification: ON
588  *
589  * Local Variables:
590  * eval: (c-set-style "gnu")
591  * End:
592  */