SNAT: IP fragmentation (VPP-890)
[vpp.git] / src / plugins / nat / nat.h
1
2 /*
3  * nat.h - NAT plugin definitions
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
31
32
33 #define SNAT_UDP_TIMEOUT 300
34 #define SNAT_UDP_TIMEOUT_MIN 120
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_TCP_INCOMING_SYN 6
38 #define SNAT_ICMP_TIMEOUT 60
39
40 #define SNAT_FLAG_HAIRPINNING (1 << 0)
41
42 /* Key */
43 typedef struct {
44   union
45   {
46     struct
47     {
48       ip4_address_t addr;
49       u16 port;
50       u16 protocol:3,
51         fib_index:13;
52     };
53     u64 as_u64;
54   };
55 } snat_session_key_t;
56
57 typedef struct {
58   union
59   {
60     struct
61     {
62       ip4_address_t l_addr;
63       ip4_address_t r_addr;
64       u32 fib_index;
65       u16 l_port;
66       u8 proto;
67       u8 rsvd;
68     };
69     u64 as_u64[2];
70   };
71 } nat_ed_ses_key_t;
72
73 typedef struct {
74   union
75   {
76     struct
77     {
78       ip4_address_t ext_host_addr;
79       u16 ext_host_port;
80       u16 out_port;
81     };
82     u64 as_u64;
83   };
84 } snat_det_out_key_t;
85
86 typedef struct {
87   union
88   {
89     struct
90     {
91       ip4_address_t addr;
92       u32 fib_index;
93     };
94     u64 as_u64;
95   };
96 } snat_user_key_t;
97
98
99 #define foreach_snat_protocol \
100   _(UDP, 0, udp, "udp")       \
101   _(TCP, 1, tcp, "tcp")       \
102   _(ICMP, 2, icmp, "icmp")
103
104 typedef enum {
105 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
106   foreach_snat_protocol
107 #undef _
108 } snat_protocol_t;
109
110
111 #define foreach_snat_session_state          \
112   _(0, UNKNOWN, "unknown")                 \
113   _(1, UDP_ACTIVE, "udp-active")           \
114   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
115   _(3, TCP_ESTABLISHED, "tcp-established") \
116   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
117   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
118   _(6, TCP_LAST_ACK, "tcp-last-ack")       \
119   _(7, ICMP_ACTIVE, "icmp-active")
120
121 typedef enum {
122 #define _(v, N, s) SNAT_SESSION_##N = v,
123   foreach_snat_session_state
124 #undef _
125 } snat_session_state_t;
126
127
128 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
129 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO  2
130 #define SNAT_SESSION_FLAG_LOAD_BALANCING 4
131
132 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
133 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
134
135 typedef CLIB_PACKED(struct {
136   snat_session_key_t out2in;    /* 0-15 */
137
138   snat_session_key_t in2out;    /* 16-31 */
139
140   u32 flags;                    /* 32-35 */
141
142   /* per-user translations */
143   u32 per_user_index;           /* 36-39 */
144
145   u32 per_user_list_head_index; /* 40-43 */
146
147   /* Last heard timer */
148   f64 last_heard;               /* 44-51 */
149
150   u64 total_bytes;              /* 52-59 */
151
152   u32 total_pkts;               /* 60-63 */
153
154   /* Outside address */
155   u32 outside_address_index;    /* 64-67 */
156
157   /* External host address and port */
158   ip4_address_t ext_host_addr;  /* 68-71 */
159   u16 ext_host_port;            /* 72-73 */
160 }) snat_session_t;
161
162
163 typedef struct {
164   ip4_address_t addr;
165   u32 fib_index;
166   u32 sessions_per_user_list_head_index;
167   u32 nsessions;
168   u32 nstaticsessions;
169 } snat_user_t;
170
171 typedef struct {
172   ip4_address_t addr;
173   u32 fib_index;
174 #define _(N, i, n, s) \
175   u16 busy_##n##_ports; \
176   u16 * busy_##n##_ports_per_thread; \
177   uword * busy_##n##_port_bitmap;
178   foreach_snat_protocol
179 #undef _
180 } snat_address_t;
181
182 typedef struct {
183   u16 in_port;
184   snat_det_out_key_t out;
185   u8 state;
186   u32 expire;
187 } snat_det_session_t;
188
189 typedef struct {
190   ip4_address_t in_addr;
191   u8 in_plen;
192   ip4_address_t out_addr;
193   u8 out_plen;
194   u32 sharing_ratio;
195   u16 ports_per_host;
196   u32 ses_num;
197   /* vector of sessions */
198   snat_det_session_t * sessions;
199 } snat_det_map_t;
200
201 typedef struct {
202   ip4_address_t addr;
203   u16 port;
204   u8 probability;
205   u8 prefix;
206 } nat44_lb_addr_port_t;
207
208 typedef struct {
209   ip4_address_t local_addr;
210   ip4_address_t external_addr;
211   u16 local_port;
212   u16 external_port;
213   u8 addr_only;
214   u32 vrf_id;
215   u32 fib_index;
216   snat_protocol_t proto;
217   u32 worker_index;
218   nat44_lb_addr_port_t *locals;
219 } snat_static_mapping_t;
220
221 typedef struct {
222   u32 sw_if_index;
223   u8 flags;
224 } snat_interface_t;
225
226 typedef struct {
227   ip4_address_t l_addr;
228   u16 l_port;
229   u16 e_port;
230   u32 sw_if_index;
231   u32 vrf_id;
232   snat_protocol_t proto;
233   int addr_only;
234   int is_add;
235 } snat_static_map_resolve_t;
236
237 typedef struct {
238   /* Main lookup tables */
239   clib_bihash_8_8_t out2in;
240   clib_bihash_8_8_t in2out;
241
242   /* Find-a-user => src address lookup */
243   clib_bihash_8_8_t user_hash;
244
245   /* User pool */
246   snat_user_t * users;
247
248   /* Session pool */
249   snat_session_t * sessions;
250
251   /* Pool of doubly-linked list elements */
252   dlist_elt_t * list_pool;
253
254   u32 snat_thread_index;
255 } snat_main_per_thread_data_t;
256
257 struct snat_main_s;
258
259 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
260                                         vlib_node_runtime_t *node,
261                                         u32 thread_index,
262                                         vlib_buffer_t *b0,
263                                         ip4_header_t *ip0,
264                                         u8 *p_proto,
265                                         snat_session_key_t *p_value,
266                                         u8 *p_dont_translate,
267                                         void *d,
268                                         void *e);
269
270 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
271
272 typedef struct snat_main_s {
273   /* Endpoint address dependent sessions lookup tables */
274   clib_bihash_16_8_t out2in_ed;
275   clib_bihash_16_8_t in2out_ed;
276
277   snat_icmp_match_function_t * icmp_match_in2out_cb;
278   snat_icmp_match_function_t * icmp_match_out2in_cb;
279
280   u32 num_workers;
281   u32 first_worker_index;
282   u32 next_worker;
283   u32 * workers;
284   snat_get_worker_function_t * worker_in2out_cb;
285   snat_get_worker_function_t * worker_out2in_cb;
286   u16 port_per_thread;
287   u32 num_snat_thread;
288
289   /* Per thread data */
290   snat_main_per_thread_data_t * per_thread_data;
291
292   /* Find a static mapping by local */
293   clib_bihash_8_8_t static_mapping_by_local;
294
295   /* Find a static mapping by external */
296   clib_bihash_8_8_t static_mapping_by_external;
297
298   /* Static mapping pool */
299   snat_static_mapping_t * static_mappings;
300
301   /* Interface pool */
302   snat_interface_t * interfaces;
303   snat_interface_t * output_feature_interfaces;
304
305   /* Vector of outside addresses */
306   snat_address_t * addresses;
307
308   /* sw_if_indices whose intfc addresses should be auto-added */
309   u32 * auto_add_sw_if_indices;
310
311   /* vector of interface address static mappings to resolve. */
312   snat_static_map_resolve_t *to_resolve;
313
314   /* Randomize port allocation order */
315   u32 random_seed;
316
317   /* Worker handoff index */
318   u32 fq_in2out_index;
319   u32 fq_in2out_output_index;
320   u32 fq_out2in_index;
321
322   /* in2out and out2in node index */
323   u32 in2out_node_index;
324   u32 in2out_output_node_index;
325   u32 out2in_node_index;
326
327   /* Deterministic NAT */
328   snat_det_map_t * det_maps;
329
330   /* Config parameters */
331   u8 static_mapping_only;
332   u8 static_mapping_connection_tracking;
333   u8 deterministic;
334   u32 translation_buckets;
335   u32 translation_memory_size;
336   u32 max_translations;
337   u32 user_buckets;
338   u32 user_memory_size;
339   u32 max_translations_per_user;
340   u32 outside_vrf_id;
341   u32 outside_fib_index;
342   u32 inside_vrf_id;
343   u32 inside_fib_index;
344
345   /* tenant VRF aware address pool activation flag */
346   u8 vrf_mode;
347
348   /* values of various timeouts */
349   u32 udp_timeout;
350   u32 tcp_established_timeout;
351   u32 tcp_transitory_timeout;
352   u32 icmp_timeout;
353
354   /* API message ID base */
355   u16 msg_id_base;
356
357   /* convenience */
358   vlib_main_t * vlib_main;
359   vnet_main_t * vnet_main;
360   ip4_main_t * ip4_main;
361   ip_lookup_main_t * ip4_lookup_main;
362   api_main_t * api_main;
363 } snat_main_t;
364
365 extern snat_main_t snat_main;
366 extern vlib_node_registration_t snat_in2out_node;
367 extern vlib_node_registration_t snat_in2out_output_node;
368 extern vlib_node_registration_t snat_out2in_node;
369 extern vlib_node_registration_t snat_in2out_fast_node;
370 extern vlib_node_registration_t snat_out2in_fast_node;
371 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
372 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
373 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
374 extern vlib_node_registration_t snat_det_in2out_node;
375 extern vlib_node_registration_t snat_det_out2in_node;
376 extern vlib_node_registration_t snat_hairpin_dst_node;
377 extern vlib_node_registration_t snat_hairpin_src_node;
378
379 void snat_free_outside_address_and_port (snat_address_t * addresses,
380                                          u32 thread_index,
381                                          snat_session_key_t * k,
382                                          u32 address_index);
383
384 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
385                                          u32 fib_index,
386                                          u32 thread_index,
387                                          snat_session_key_t * k,
388                                          u32 * address_indexp,
389                                          u8 vrf_mode,
390                                          u16 port_per_thread,
391                                          u32 snat_thread_index);
392
393 int snat_static_mapping_match (snat_main_t * sm,
394                                snat_session_key_t match,
395                                snat_session_key_t * mapping,
396                                u8 by_external,
397                                u8 *is_addr_only);
398
399 void snat_add_del_addr_to_fib (ip4_address_t * addr,
400                                u8 p_len,
401                                u32 sw_if_index,
402                                int is_add);
403
404 format_function_t format_snat_user;
405
406 typedef struct {
407   u32 cached_sw_if_index;
408   u32 cached_ip4_address;
409 } snat_runtime_t;
410
411 /** \brief Check if SNAT session is created from static mapping.
412     @param s SNAT session
413     @return 1 if SNAT session is created from static mapping otherwise 0
414 */
415 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
416
417 /** \brief Check if SNAT session for unknown protocol.
418     @param s SNAT session
419     @return 1 if SNAT session for unknown protocol otherwise 0
420 */
421 #define snat_is_unk_proto_session(s) s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO
422
423 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
424 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
425
426 /*
427  * Why is this here? Because we don't need to touch this layer to
428  * simply reply to an icmp. We need to change id to a unique
429  * value to NAT an echo request/reply.
430  */
431
432 typedef struct {
433   u16 identifier;
434   u16 sequence;
435 } icmp_echo_header_t;
436
437 always_inline u32
438 ip_proto_to_snat_proto (u8 ip_proto)
439 {
440   u32 snat_proto = ~0;
441
442   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
443   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
444   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
445   snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
446
447   return snat_proto;
448 }
449
450 always_inline u8
451 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
452 {
453   u8 ip_proto = ~0;
454
455   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
456   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
457   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
458
459   return ip_proto;
460 }
461
462 typedef struct {
463   u16 src_port, dst_port;
464 } tcp_udp_header_t;
465
466 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
467                            u32 thread_index, vlib_buffer_t *b0,
468                            ip4_header_t *ip0, u8 *p_proto,
469                            snat_session_key_t *p_value,
470                            u8 *p_dont_translate, void *d, void *e);
471 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
472                            u32 thread_index, vlib_buffer_t *b0,
473                            ip4_header_t *ip0, u8 *p_proto,
474                            snat_session_key_t *p_value,
475                            u8 *p_dont_translate, void *d, void *e);
476 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
477                           u32 thread_index, vlib_buffer_t *b0,
478                           ip4_header_t *ip0, u8 *p_proto,
479                           snat_session_key_t *p_value,
480                           u8 *p_dont_translate, void *d, void *e);
481 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
482                            u32 thread_index, vlib_buffer_t *b0,
483                            ip4_header_t *ip0, u8 *p_proto,
484                            snat_session_key_t *p_value,
485                            u8 *p_dont_translate, void *d, void *e);
486 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
487                            u32 thread_index, vlib_buffer_t *b0,
488                            ip4_header_t *ip0, u8 *p_proto,
489                            snat_session_key_t *p_value,
490                            u8 *p_dont_translate, void *d, void *e);
491 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
492                           u32 thread_index, vlib_buffer_t *b0,
493                           ip4_header_t *ip0, u8 *p_proto,
494                           snat_session_key_t *p_value,
495                           u8 *p_dont_translate, void *d, void *e);
496 void increment_v4_address(ip4_address_t * a);
497 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id);
498 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
499 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
500                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
501                             u32 sw_if_index, snat_protocol_t proto, int is_add);
502 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
503 int snat_set_workers (uword * bitmap);
504 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
505 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
506                                           int is_del);
507 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
508 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
509 u8 * format_snat_protocol(u8 * s, va_list * args);
510 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
511                                      snat_protocol_t proto, u32 vrf_id,
512                                      nat44_lb_addr_port_t *locals, u8 is_add);
513 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
514                        snat_protocol_t proto, u32 vrf_id, int is_in);
515
516 static_always_inline u8
517 icmp_is_error_message (icmp46_header_t * icmp)
518 {
519   switch(icmp->type)
520     {
521     case ICMP4_destination_unreachable:
522     case ICMP4_time_exceeded:
523     case ICMP4_parameter_problem:
524     case ICMP4_source_quench:
525     case ICMP4_redirect:
526     case ICMP4_alternate_host_address:
527       return 1;
528     }
529   return 0;
530 }
531
532 static_always_inline u8
533 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
534                   u32 ip4_addr)
535 {
536   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
537   ip4_address_t * first_int_addr;
538
539   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
540     {
541       first_int_addr =
542         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
543                                      0 /* just want the address */);
544       rt->cached_sw_if_index = sw_if_index0;
545       if (first_int_addr)
546         rt->cached_ip4_address = first_int_addr->as_u32;
547       else
548         rt->cached_ip4_address = 0;
549     }
550
551   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
552     return 1;
553   else
554     return 0;
555 }
556
557 always_inline u8
558 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
559 {
560   if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
561     return 1;
562
563   return 0;
564 }
565
566 static_always_inline void
567 nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector,
568                      vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
569 {
570   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
571
572   from = bi_vector;
573   n_left_from = vec_len(bi_vector);
574   next_index = node->cached_next_index;
575   while (n_left_from > 0) {
576     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
577     while (n_left_from > 0 && n_left_to_next > 0) {
578       u32 bi0 = to_next[0] = from[0];
579       from += 1;
580       n_left_from -= 1;
581       to_next += 1;
582       n_left_to_next -= 1;
583       vlib_buffer_t *p0 = vlib_get_buffer(vm, bi0);
584       p0->error = *error;
585       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
586                                       n_left_to_next, bi0, next);
587     }
588     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
589   }
590 }
591
592 #endif /* __included_snat_h__ */