ae58cdb91206bbec0d0625f7fe51af67ac7d8ba1
[vpp.git] / vnet / vnet / map / map.h
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 #include <stdbool.h>
16 #include <vppinfra/error.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vlib/vlib.h>
20
21 #define MAP_SKIP_IP6_LOOKUP 1
22
23 typedef enum {
24   MAP_SENDER,
25   MAP_RECEIVER
26 } map_dir_e;
27
28 int map_create_domain(ip4_address_t *ip4_prefix, u8 ip4_prefix_len,
29                       ip6_address_t *ip6_prefix, u8 ip6_prefix_len,
30                       ip6_address_t *ip6_src, u8 ip6_src_len,
31                       u8 ea_bits_len, u8 psid_offset, u8 psid_length,
32                       u32 *map_domain_index, u16 mtu, u8 flags);
33 int map_delete_domain(u32 map_domain_index);
34 int map_add_del_psid(u32 map_domain_index, u16 psid, ip6_address_t *tep, u8 is_add);
35 u8 *format_map_trace(u8 *s, va_list *args);
36 i32 ip4_get_port(ip4_header_t *ip, map_dir_e dir, u16 buffer_len);
37 i32 ip6_get_port(ip6_header_t *ip6, map_dir_e dir, u16 buffer_len);
38 u16 ip4_map_get_port (ip4_header_t *ip, map_dir_e dir);
39
40 typedef enum __attribute__ ((__packed__)) {
41   MAP_DOMAIN_PREFIX        = 1 << 0,
42   MAP_DOMAIN_TRANSLATION   = 1 << 1, // The domain uses MAP-T
43 } map_domain_flags_e;
44
45 /**
46  * IP4 reassembly logic:
47  * One virtually reassembled flow requires a map_ip4_reass_t structure in order
48  * to keep the first-fragment port number and, optionally, cache out of sequence
49  * packets.
50  * There are up to MAP_IP4_REASS_MAX_REASSEMBLY such structures.
51  * When in use, those structures are stored in a hash table of MAP_IP4_REASS_BUCKETS buckets.
52  * When a new structure needs to be used, it is allocated from available ones.
53  * If there is no structure available, the oldest in use is selected and used if and
54  * only if it was first allocated more than MAP_IP4_REASS_LIFETIME seconds ago.
55  * In case no structure can be allocated, the fragment is dropped.
56  */
57
58 #define MAP_IP4_REASS_LIFETIME_DEFAULT (100) /* ms */
59 #define MAP_IP4_REASS_HT_RATIO_DEFAULT (1.0)
60 #define MAP_IP4_REASS_POOL_SIZE_DEFAULT 1024 // Number of reassembly structures
61 #define MAP_IP4_REASS_BUFFERS_DEFAULT 2048
62
63 #define MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY 5    // Number of fragment per reassembly
64
65 #define MAP_IP6_REASS_LIFETIME_DEFAULT (100) /* ms */
66 #define MAP_IP6_REASS_HT_RATIO_DEFAULT (1.0)
67 #define MAP_IP6_REASS_POOL_SIZE_DEFAULT 1024 // Number of reassembly structures
68 #define MAP_IP6_REASS_BUFFERS_DEFAULT 2048
69
70 #define MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY 5
71
72 #define MAP_IP6_REASS_COUNT_BYTES
73 #define MAP_IP4_REASS_COUNT_BYTES
74
75 //#define IP6_MAP_T_OVERRIDE_TOS 0
76
77 /*
78  * This structure _MUST_ be no larger than a single cache line (64 bytes).
79  * If more space is needed make a union of ip6_prefix and *rules, those are mutually exclusive.
80  */
81 typedef struct {
82   ip6_address_t ip6_src;
83   ip6_address_t ip6_prefix;
84   ip6_address_t *rules;
85   u32 suffix_mask;
86   ip4_address_t ip4_prefix;
87   u16 psid_mask;
88   u16 mtu;
89   map_domain_flags_e flags;
90   u8 ip6_prefix_len;
91   u8 ip6_src_len;
92   u8 ea_bits_len;
93   u8 psid_offset;
94   u8 psid_length;
95
96   /* helpers */
97   u8 psid_shift;
98   u8 suffix_shift;
99   u8 ea_shift;
100
101   /* not used by forwarding */
102   u8 ip4_prefix_len;
103 } map_domain_t;
104
105 #define MAP_REASS_INDEX_NONE ((u16)0xffff)
106
107 /*
108  * Hash key, padded out to 16 bytes for fast compare
109  */
110 typedef union {
111   CLIB_PACKED (struct {
112     ip4_address_t src;
113     ip4_address_t dst;
114     u16 fragment_id;
115     u8 protocol;
116   });
117   u64 as_u64[2];
118   u32 as_u32[4];
119 } map_ip4_reass_key_t;
120
121 typedef struct {
122   map_ip4_reass_key_t key;
123   f64 ts;
124 #ifdef MAP_IP4_REASS_COUNT_BYTES
125   u16 expected_total;
126   u16 forwarded;
127 #endif
128   i32 port;
129   u16 bucket;
130   u16 bucket_next;
131   u16 fifo_prev;
132   u16 fifo_next;
133   u32 fragments[MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY];
134 } map_ip4_reass_t;
135
136 /*
137  * MAP domain counters
138  */
139 typedef enum {
140   /* Simple counters */
141   MAP_DOMAIN_IPV4_FRAGMENT = 0,
142   /* Combined counters */
143   MAP_DOMAIN_COUNTER_RX = 0,
144   MAP_DOMAIN_COUNTER_TX,
145   MAP_N_DOMAIN_COUNTER
146 } map_domain_counter_t;
147
148 /*
149  * main_main_t
150  */
151 typedef union {
152   CLIB_PACKED (struct {
153     ip6_address_t src;
154     ip6_address_t dst;
155     u32 fragment_id;
156     u8 protocol;
157   });
158   u64 as_u64[5];
159   u32 as_u32[10];
160 } map_ip6_reass_key_t;
161
162 typedef struct {
163   u32 pi; //Cached packet or ~0
164   u16 next_data_offset; //The data offset of the additional 20 bytes or ~0
165   u8 next_data_len; //Number of bytes ready to be copied (20 if not last fragment)
166   u8 next_data[20]; //The 20 additional bytes
167 } map_ip6_fragment_t;
168
169 typedef struct {
170   map_ip6_reass_key_t key;
171   f64 ts;
172 #ifdef MAP_IP6_REASS_COUNT_BYTES
173   u16 expected_total;
174   u16 forwarded;
175 #endif
176   u16 bucket; //What hash bucket this element is linked in
177   u16 bucket_next;
178   u16 fifo_prev;
179   u16 fifo_next;
180   ip4_header_t ip4_header;
181   map_ip6_fragment_t fragments[MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY];
182 } map_ip6_reass_t;
183
184 typedef struct {
185   /* pool of MAP domains */
186   map_domain_t *domains;
187
188   /* MAP Domain packet/byte counters indexed by map domain index */
189   vlib_simple_counter_main_t *simple_domain_counters;
190   vlib_combined_counter_main_t *domain_counters;
191   volatile u32 *counter_lock;
192
193   /* Global counters */
194   vlib_simple_counter_main_t icmp_relayed;
195
196 #ifdef MAP_SKIP_IP6_LOOKUP
197   /* pre-presolve */
198   u32 adj6_index, adj4_index;
199   ip4_address_t preresolve_ip4;
200   ip6_address_t preresolve_ip6;
201 #endif
202
203   /* Traffic class: zero, copy (~0) or fixed value */
204   u8 tc;
205   bool tc_copy;
206   bool sec_check;
207   bool sec_check_frag;
208
209   /* ICMPv6 -> ICMPv4 relay parameters */
210   ip4_address_t icmp_src_address;
211
212   /* convenience */
213   vlib_main_t *vlib_main;
214   vnet_main_t *vnet_main;
215
216   /*
217    * IPv4 encap and decap reassembly
218    */
219   //Conf
220   f32 ip4_reass_conf_ht_ratio; //Size of ht is 2^ceil(log2(ratio*pool_size))
221   u16 ip4_reass_conf_pool_size; //Max number of allocated reass structures
222   u16 ip4_reass_conf_lifetime_ms; //Time a reassembly struct is considered valid in ms
223   u32 ip4_reass_conf_buffers; //Maximum number of buffers used by ip4 reassembly
224
225   //Runtime
226   map_ip4_reass_t *ip4_reass_pool;
227   u8 ip4_reass_ht_log2len; //Hash table size is 2^log2len
228   u16 ip4_reass_allocated;
229   u16 *ip4_reass_hash_table;
230   u16 ip4_reass_fifo_last;
231   volatile u32 *ip4_reass_lock;
232
233   //Counters
234   u32 ip4_reass_buffered_counter;
235
236   /*
237    * IPv6 decap reassembly
238    */
239   //Conf
240   f32 ip6_reass_conf_ht_ratio; //Size of ht is 2^ceil(log2(ratio*pool_size))
241   u16 ip6_reass_conf_pool_size; //Max number of allocated reass structures
242   u16 ip6_reass_conf_lifetime_ms; //Time a reassembly struct is considered valid in ms
243   u32 ip6_reass_conf_buffers; //Maximum number of buffers used by ip6 reassembly
244
245   //Runtime
246   map_ip6_reass_t *ip6_reass_pool;
247   u8 ip6_reass_ht_log2len; //Hash table size is 2^log2len
248   u16 ip6_reass_allocated;
249   u16 *ip6_reass_hash_table;
250   u16 ip6_reass_fifo_last;
251   volatile u32 *ip6_reass_lock;
252
253   //Counters
254   u32 ip6_reass_buffered_counter;
255
256 } map_main_t;
257
258 /*
259  * TODO: Remove SEC_CHECK / TRANSLATED_4TO6 / TRANSLATED_6TO4
260  */
261 #define foreach_map_error                               \
262   /* Must be first. */                                  \
263  _(NONE, "valid MAP packets")                           \
264  _(BAD_PROTOCOL, "bad protocol")                        \
265  _(WRONG_ICMP_TYPE, "wrong icmp type")                  \
266  _(SEC_CHECK, "security check failed")                  \
267  _(ENCAP_SEC_CHECK, "encap security check failed")      \
268  _(DECAP_SEC_CHECK, "decap security check failed")      \
269  _(ICMP, "unable to translate ICMP")                    \
270  _(ICMP_RELAY, "unable to relay ICMP")                  \
271  _(UNKNOWN, "unknown")                                  \
272  _(NO_DOMAIN, "no domain")                              \
273  _(FRAGMENTED, "packet is a fragment")                  \
274  _(FRAGMENT_MEMORY, "could not cache fragment")         \
275  _(FRAGMENT_MALFORMED, "fragment has unexpected format")\
276  _(FRAGMENT_DROPPED, "dropped cached fragment")         \
277  _(MALFORMED, "malformed packet")
278
279 typedef enum {
280 #define _(sym,str) MAP_ERROR_##sym,
281    foreach_map_error
282 #undef _
283    MAP_N_ERROR,
284  } map_error_t;
285
286 u64 map_error_counter_get(u32 node_index, map_error_t map_error);
287
288 typedef struct {
289   u32 map_domain_index;
290   u16 port;
291 } map_trace_t;
292
293 map_main_t map_main;
294
295 vlib_node_registration_t ip4_map_node;
296 vlib_node_registration_t ip6_map_node;
297
298 vlib_node_registration_t ip4_map_t_node;
299 vlib_node_registration_t ip4_map_t_fragmented_node;
300 vlib_node_registration_t ip4_map_t_tcp_udp_node;
301 vlib_node_registration_t ip4_map_t_icmp_node;
302
303 vlib_node_registration_t ip6_map_t_node;
304 vlib_node_registration_t ip6_map_t_fragmented_node;
305 vlib_node_registration_t ip6_map_t_tcp_udp_node;
306 vlib_node_registration_t ip6_map_t_icmp_node;
307
308 /*
309  * map_get_pfx
310  */
311 static_always_inline u64
312 map_get_pfx (map_domain_t *d, u32 addr, u16 port)
313 {
314   u16 psid = (port >> d->psid_shift) & d->psid_mask;
315
316   if (d->ea_bits_len == 0 && d->rules)
317     return clib_net_to_host_u64(d->rules[psid].as_u64[0]);
318
319   u32 suffix = (addr >> d->suffix_shift) & d->suffix_mask;
320   u64 ea = d->ea_bits_len == 0 ? 0 : (((u64) suffix << d->psid_length)) | psid;
321
322   return clib_net_to_host_u64(d->ip6_prefix.as_u64[0]) | ea << d->ea_shift;
323 }
324
325 static_always_inline u64
326 map_get_pfx_net (map_domain_t *d, u32 addr, u16 port)
327 {
328   return clib_host_to_net_u64(map_get_pfx(d, clib_net_to_host_u32(addr),
329                                           clib_net_to_host_u16(port)));
330 }
331
332 /*
333  * map_get_sfx
334  */
335 static_always_inline u64
336 map_get_sfx (map_domain_t *d, u32 addr, u16 port)
337 {
338   u16 psid = (port >> d->psid_shift) & d->psid_mask;
339
340   /* Shared 1:1 mode. */
341   if (d->ea_bits_len == 0 && d->rules)
342     return clib_net_to_host_u64(d->rules[psid].as_u64[1]);
343   if (d->ip6_prefix_len == 128)
344     return clib_net_to_host_u64(d->ip6_prefix.as_u64[1]);
345
346   /* IPv4 prefix */
347   if (d->flags & MAP_DOMAIN_PREFIX)
348     return (u64) (addr & ~d->suffix_mask) << 16;
349
350   /* Shared or full IPv4 address */
351   return ((u64) addr << 16) | psid;
352 }
353
354 static_always_inline u64
355 map_get_sfx_net (map_domain_t *d, u32 addr, u16 port)
356 {
357   return clib_host_to_net_u64(map_get_sfx(d, clib_net_to_host_u32(addr),
358                                           clib_net_to_host_u16(port)));
359 }
360
361 static_always_inline u32
362 map_get_ip4 (ip6_address_t *addr)
363 {
364   return clib_host_to_net_u32(clib_net_to_host_u64(addr->as_u64[1]) >> 16);
365 }
366
367 /*
368  * Get the MAP domain from an IPv4 lookup adjacency.
369  */
370 static_always_inline map_domain_t *
371 ip4_map_get_domain (u32 adj_index, u32 *map_domain_index)
372 {
373   map_main_t *mm = &map_main;
374   ip_lookup_main_t *lm = &ip4_main.lookup_main;
375   ip_adjacency_t *adj = ip_get_adjacency(lm, adj_index);
376   ASSERT(adj);
377   uword *p = (uword *)adj->rewrite_data;
378   ASSERT(p);
379   *map_domain_index = p[0];
380   return pool_elt_at_index(mm->domains, p[0]);
381 }
382
383 /*
384  * Get the MAP domain from an IPv6 lookup adjacency.
385  * If the IPv6 address or prefix is not shared, no lookup is required.
386  * The IPv4 address is used otherwise.
387  */
388 static_always_inline map_domain_t *
389 ip6_map_get_domain (u32 adj_index, ip4_address_t *addr,
390                     u32 *map_domain_index, u8 *error)
391 {
392   map_main_t *mm = &map_main;
393   ip4_main_t *im4 = &ip4_main;
394   ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
395   ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
396   ip_adjacency_t *adj = ip_get_adjacency(lm6, adj_index);
397   ASSERT(adj);
398   uword *p = (uword *)adj->rewrite_data;
399   ASSERT(p);
400   *map_domain_index = p[0];
401   if (p[0] != ~0)
402     return pool_elt_at_index(mm->domains, p[0]);
403
404   u32 ai = ip4_fib_lookup_with_table(im4, 0, addr, 0);
405   ip_adjacency_t *adj4 = ip_get_adjacency (lm4, ai);
406   if (PREDICT_TRUE(adj4->lookup_next_index == IP_LOOKUP_NEXT_MAP ||
407                    adj4->lookup_next_index == IP_LOOKUP_NEXT_MAP_T)) {
408     uword *p = (uword *)adj4->rewrite_data;
409     *map_domain_index = p[0];
410     return pool_elt_at_index(mm->domains, *map_domain_index);
411   }
412   *error = MAP_ERROR_NO_DOMAIN;
413   return NULL;
414 }
415
416 map_ip4_reass_t *
417 map_ip4_reass_get(u32 src, u32 dst, u16 fragment_id,
418                   u8 protocol, u32 **pi_to_drop);
419 void
420 map_ip4_reass_free(map_ip4_reass_t *r, u32 **pi_to_drop);
421
422 #define map_ip4_reass_lock() while (__sync_lock_test_and_set(map_main.ip4_reass_lock, 1)) {}
423 #define map_ip4_reass_unlock() do {CLIB_MEMORY_BARRIER(); *map_main.ip4_reass_lock = 0;} while(0)
424
425 static_always_inline void
426 map_ip4_reass_get_fragments(map_ip4_reass_t *r, u32 **pi)
427 {
428   int i;
429   for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
430     if(r->fragments[i] != ~0) {
431       vec_add1(*pi, r->fragments[i]);
432       r->fragments[i] = ~0;
433       map_main.ip4_reass_buffered_counter--;
434     }
435 }
436
437 int map_ip4_reass_add_fragment(map_ip4_reass_t *r, u32 pi);
438
439 map_ip6_reass_t *
440 map_ip6_reass_get(ip6_address_t *src, ip6_address_t *dst, u32 fragment_id,
441                   u8 protocol, u32 **pi_to_drop);
442 void
443 map_ip6_reass_free(map_ip6_reass_t *r, u32 **pi_to_drop);
444
445 #define map_ip6_reass_lock() while (__sync_lock_test_and_set(map_main.ip6_reass_lock, 1)) {}
446 #define map_ip6_reass_unlock() do {CLIB_MEMORY_BARRIER(); *map_main.ip6_reass_lock = 0;} while(0)
447
448 int
449 map_ip6_reass_add_fragment(map_ip6_reass_t *r, u32 pi,
450                            u16 data_offset, u16 next_data_offset,
451                            u8 *data_start, u16 data_len);
452
453 void map_ip4_drop_pi(u32 pi);
454
455 int map_ip4_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets);
456 #define MAP_IP4_REASS_CONF_HT_RATIO_MAX 100
457 int map_ip4_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets);
458 #define MAP_IP4_REASS_CONF_POOL_SIZE_MAX (0xfeff)
459 int map_ip4_reass_conf_lifetime(u16 lifetime_ms);
460 #define MAP_IP4_REASS_CONF_LIFETIME_MAX 0xffff
461 int map_ip4_reass_conf_buffers(u32 buffers);
462 #define MAP_IP4_REASS_CONF_BUFFERS_MAX (0xffffffff)
463
464 void map_ip6_drop_pi(u32 pi);
465
466
467 int map_ip6_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets);
468 #define MAP_IP6_REASS_CONF_HT_RATIO_MAX 100
469 int map_ip6_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets);
470 #define MAP_IP6_REASS_CONF_POOL_SIZE_MAX (0xfeff)
471 int map_ip6_reass_conf_lifetime(u16 lifetime_ms);
472 #define MAP_IP6_REASS_CONF_LIFETIME_MAX 0xffff
473 int map_ip6_reass_conf_buffers(u32 buffers);
474 #define MAP_IP6_REASS_CONF_BUFFERS_MAX (0xffffffff)
475
476 static_always_inline
477 int ip6_parse(const ip6_header_t *ip6, u32 buff_len,
478               u8 *l4_protocol, u16 *l4_offset, u16 *frag_hdr_offset)
479 {
480   if (ip6->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION) {
481     *l4_protocol = ((ip6_frag_hdr_t *)(ip6 + 1))->next_hdr;
482     *frag_hdr_offset = sizeof(*ip6);
483     *l4_offset = sizeof(*ip6) + sizeof(ip6_frag_hdr_t);
484   } else {
485     *l4_protocol = ip6->protocol;
486     *frag_hdr_offset = 0;
487     *l4_offset = sizeof(*ip6);
488   }
489
490   return (buff_len < (*l4_offset + 4)) ||
491       (clib_net_to_host_u16(ip6->payload_length) < (*l4_offset + 4 - sizeof(*ip6)));
492 }
493
494
495 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
496 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
497
498 #define frag_id_6to4(id) ((id) ^ ((id) >> 16))
499
500 static_always_inline void
501 ip4_map_t_embedded_address (map_domain_t *d,
502                                 ip6_address_t *ip6, const ip4_address_t *ip4)
503 {
504   ASSERT(d->ip6_src_len == 96); //No support for other lengths for now
505   ip6->as_u64[0] = d->ip6_src.as_u64[0];
506   ip6->as_u32[2] = d->ip6_src.as_u32[2];
507   ip6->as_u32[3] = ip4->as_u32;
508 }
509
510 static_always_inline u32
511 ip6_map_t_embedded_address (map_domain_t *d, ip6_address_t *addr)
512 {
513   ASSERT(d->ip6_src_len == 96); //No support for other lengths for now
514   return addr->as_u32[3];
515 }
516
517 static inline void
518 map_domain_counter_lock (map_main_t *mm)
519 {
520   if (mm->counter_lock)
521     while (__sync_lock_test_and_set(mm->counter_lock, 1))
522       /* zzzz */ ;
523 }
524 static inline void
525 map_domain_counter_unlock (map_main_t *mm)
526 {
527   if (mm->counter_lock)
528     *mm->counter_lock = 0;
529 }
530
531
532 static_always_inline void
533 map_send_all_to_node(vlib_main_t *vm, u32 *pi_vector,
534                      vlib_node_runtime_t *node, vlib_error_t *error,
535                      u32 next)
536 {
537   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
538   //Deal with fragments that are ready
539   from = pi_vector;
540   n_left_from = vec_len(pi_vector);
541   next_index = node->cached_next_index;
542   while (n_left_from > 0) {
543     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
544     while (n_left_from > 0 && n_left_to_next > 0) {
545       u32 pi0 = to_next[0] = from[0];
546       from += 1;
547       n_left_from -= 1;
548       to_next += 1;
549       n_left_to_next -= 1;
550       vlib_buffer_t *p0 = vlib_get_buffer(vm, pi0);
551       p0->error = *error;
552       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, pi0, next);
553     }
554     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
555   }
556 }