Add support for 464XLAT NAT44 mode (VPP-1045)
[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 proto:8,
65           fib_index:24;
66       u16 l_port;
67       u16 r_port;
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 #define SNAT_SESSION_FLAG_TWICE_NAT      8
132
133 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
134 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
135
136 typedef CLIB_PACKED(struct {
137   snat_session_key_t out2in;    /* 0-15 */
138
139   snat_session_key_t in2out;    /* 16-31 */
140
141   u32 flags;                    /* 32-35 */
142
143   /* per-user translations */
144   u32 per_user_index;           /* 36-39 */
145
146   u32 per_user_list_head_index; /* 40-43 */
147
148   /* Last heard timer */
149   f64 last_heard;               /* 44-51 */
150
151   u64 total_bytes;              /* 52-59 */
152
153   u32 total_pkts;               /* 60-63 */
154
155   /* Outside address */
156   u32 outside_address_index;    /* 64-67 */
157
158   /* External host address and port */
159   ip4_address_t ext_host_addr;  /* 68-71 */
160   u16 ext_host_port;            /* 72-73 */
161
162   /* External hos address and port after translation */
163   ip4_address_t ext_host_nat_addr; /* 74-77 */
164   u16 ext_host_nat_port;           /* 78-79 */
165 }) snat_session_t;
166
167
168 typedef struct {
169   ip4_address_t addr;
170   u32 fib_index;
171   u32 sessions_per_user_list_head_index;
172   u32 nsessions;
173   u32 nstaticsessions;
174 } snat_user_t;
175
176 typedef struct {
177   ip4_address_t addr;
178   u32 fib_index;
179 #define _(N, i, n, s) \
180   u16 busy_##n##_ports; \
181   u16 * busy_##n##_ports_per_thread; \
182   uword * busy_##n##_port_bitmap;
183   foreach_snat_protocol
184 #undef _
185 } snat_address_t;
186
187 typedef struct {
188   u16 in_port;
189   snat_det_out_key_t out;
190   u8 state;
191   u32 expire;
192 } snat_det_session_t;
193
194 typedef struct {
195   ip4_address_t in_addr;
196   u8 in_plen;
197   ip4_address_t out_addr;
198   u8 out_plen;
199   u32 sharing_ratio;
200   u16 ports_per_host;
201   u32 ses_num;
202   /* vector of sessions */
203   snat_det_session_t * sessions;
204 } snat_det_map_t;
205
206 typedef struct {
207   ip4_address_t addr;
208   u16 port;
209   u8 probability;
210   u8 prefix;
211 } nat44_lb_addr_port_t;
212
213 typedef struct {
214   ip4_address_t local_addr;
215   ip4_address_t external_addr;
216   u16 local_port;
217   u16 external_port;
218   u8 addr_only;
219   u8 twice_nat;
220   u32 vrf_id;
221   u32 fib_index;
222   snat_protocol_t proto;
223   u32 worker_index;
224   nat44_lb_addr_port_t *locals;
225 } snat_static_mapping_t;
226
227 typedef struct {
228   u32 sw_if_index;
229   u8 flags;
230 } snat_interface_t;
231
232 typedef struct {
233   ip4_address_t l_addr;
234   u16 l_port;
235   u16 e_port;
236   u32 sw_if_index;
237   u32 vrf_id;
238   snat_protocol_t proto;
239   int addr_only;
240   int twice_nat;
241   int is_add;
242 } snat_static_map_resolve_t;
243
244 typedef struct {
245   /* Main lookup tables */
246   clib_bihash_8_8_t out2in;
247   clib_bihash_8_8_t in2out;
248
249   /* Find-a-user => src address lookup */
250   clib_bihash_8_8_t user_hash;
251
252   /* User pool */
253   snat_user_t * users;
254
255   /* Session pool */
256   snat_session_t * sessions;
257
258   /* Pool of doubly-linked list elements */
259   dlist_elt_t * list_pool;
260
261   u32 snat_thread_index;
262 } snat_main_per_thread_data_t;
263
264 struct snat_main_s;
265
266 typedef u32 snat_icmp_match_function_t (struct snat_main_s *sm,
267                                         vlib_node_runtime_t *node,
268                                         u32 thread_index,
269                                         vlib_buffer_t *b0,
270                                         ip4_header_t *ip0,
271                                         u8 *p_proto,
272                                         snat_session_key_t *p_value,
273                                         u8 *p_dont_translate,
274                                         void *d,
275                                         void *e);
276
277 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
278
279 typedef int nat_alloc_out_addr_and_port_function_t (snat_address_t * addresses,
280                                                     u32 fib_index,
281                                                     u32 thread_index,
282                                                     snat_session_key_t * k,
283                                                     u32 * address_indexp,
284                                                     u16 port_per_thread,
285                                                     u32 snat_thread_index);
286
287 typedef struct snat_main_s {
288   /* Endpoint address dependent sessions lookup tables */
289   clib_bihash_16_8_t out2in_ed;
290   clib_bihash_16_8_t in2out_ed;
291
292   snat_icmp_match_function_t * icmp_match_in2out_cb;
293   snat_icmp_match_function_t * icmp_match_out2in_cb;
294
295   u32 num_workers;
296   u32 first_worker_index;
297   u32 next_worker;
298   u32 * workers;
299   snat_get_worker_function_t * worker_in2out_cb;
300   snat_get_worker_function_t * worker_out2in_cb;
301   u16 port_per_thread;
302   u32 num_snat_thread;
303
304   /* Per thread data */
305   snat_main_per_thread_data_t * per_thread_data;
306
307   /* Find a static mapping by local */
308   clib_bihash_8_8_t static_mapping_by_local;
309
310   /* Find a static mapping by external */
311   clib_bihash_8_8_t static_mapping_by_external;
312
313   /* Static mapping pool */
314   snat_static_mapping_t * static_mappings;
315
316   /* Interface pool */
317   snat_interface_t * interfaces;
318   snat_interface_t * output_feature_interfaces;
319
320   /* Vector of outside addresses */
321   snat_address_t * addresses;
322   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
323   u8 psid_offset;
324   u8 psid_length;
325   u16 psid;
326
327   /* Vector of twice NAT addresses for extenal hosts */
328   snat_address_t * twice_nat_addresses;
329
330   /* sw_if_indices whose intfc addresses should be auto-added */
331   u32 * auto_add_sw_if_indices;
332   u32 * auto_add_sw_if_indices_twice_nat;
333
334   /* vector of interface address static mappings to resolve. */
335   snat_static_map_resolve_t *to_resolve;
336
337   /* Randomize port allocation order */
338   u32 random_seed;
339
340   /* Worker handoff index */
341   u32 fq_in2out_index;
342   u32 fq_in2out_output_index;
343   u32 fq_out2in_index;
344
345   /* in2out and out2in node index */
346   u32 in2out_node_index;
347   u32 in2out_output_node_index;
348   u32 out2in_node_index;
349
350   /* Deterministic NAT */
351   snat_det_map_t * det_maps;
352
353   /* If forwarding is enabled */
354   u8 forwarding_enabled;
355
356   /* Config parameters */
357   u8 static_mapping_only;
358   u8 static_mapping_connection_tracking;
359   u8 deterministic;
360   u8 out2in_dpo;
361   u32 translation_buckets;
362   u32 translation_memory_size;
363   u32 max_translations;
364   u32 user_buckets;
365   u32 user_memory_size;
366   u32 max_translations_per_user;
367   u32 outside_vrf_id;
368   u32 outside_fib_index;
369   u32 inside_vrf_id;
370   u32 inside_fib_index;
371
372   /* values of various timeouts */
373   u32 udp_timeout;
374   u32 tcp_established_timeout;
375   u32 tcp_transitory_timeout;
376   u32 icmp_timeout;
377
378   /* API message ID base */
379   u16 msg_id_base;
380
381   /* convenience */
382   vlib_main_t * vlib_main;
383   vnet_main_t * vnet_main;
384   ip4_main_t * ip4_main;
385   ip_lookup_main_t * ip4_lookup_main;
386   api_main_t * api_main;
387 } snat_main_t;
388
389 extern snat_main_t snat_main;
390 extern vlib_node_registration_t snat_in2out_node;
391 extern vlib_node_registration_t snat_in2out_output_node;
392 extern vlib_node_registration_t snat_out2in_node;
393 extern vlib_node_registration_t snat_in2out_fast_node;
394 extern vlib_node_registration_t snat_out2in_fast_node;
395 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
396 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
397 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
398 extern vlib_node_registration_t snat_det_in2out_node;
399 extern vlib_node_registration_t snat_det_out2in_node;
400 extern vlib_node_registration_t snat_hairpin_dst_node;
401 extern vlib_node_registration_t snat_hairpin_src_node;
402
403 void snat_free_outside_address_and_port (snat_address_t * addresses,
404                                          u32 thread_index,
405                                          snat_session_key_t * k,
406                                          u32 address_index);
407
408 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
409                                          u32 fib_index,
410                                          u32 thread_index,
411                                          snat_session_key_t * k,
412                                          u32 * address_indexp,
413                                          u16 port_per_thread,
414                                          u32 snat_thread_index);
415
416 int snat_static_mapping_match (snat_main_t * sm,
417                                snat_session_key_t match,
418                                snat_session_key_t * mapping,
419                                u8 by_external,
420                                u8 *is_addr_only,
421                                u8 *twice_nat);
422
423 void snat_add_del_addr_to_fib (ip4_address_t * addr,
424                                u8 p_len,
425                                u32 sw_if_index,
426                                int is_add);
427
428 format_function_t format_snat_user;
429
430 typedef struct {
431   u32 cached_sw_if_index;
432   u32 cached_ip4_address;
433 } snat_runtime_t;
434
435 /** \brief Check if SNAT session is created from static mapping.
436     @param s SNAT session
437     @return 1 if SNAT session is created from static mapping otherwise 0
438 */
439 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
440
441 /** \brief Check if SNAT session for unknown protocol.
442     @param s SNAT session
443     @return 1 if SNAT session for unknown protocol otherwise 0
444 */
445 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
446
447 /** \brief Check if NAT session is twice NAT.
448     @param s NAT session
449     @return 1 if NAT session is twice NAT
450 */
451 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
452
453 /** \brief Check if NAT session is load-balancing.
454     @param s NAT session
455     @return 1 if NAT session is load-balancing
456 */
457 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
458
459 /** \brief Check if NAT session is endpoint dependent.
460     @param s NAT session
461     @return 1 if NAT session is endpoint dependent
462 */
463 #define is_ed_session(s) (snat_is_unk_proto_session (s) || is_twice_nat_session (s) || is_lb_session (s))
464
465 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
466 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
467
468 /*
469  * Why is this here? Because we don't need to touch this layer to
470  * simply reply to an icmp. We need to change id to a unique
471  * value to NAT an echo request/reply.
472  */
473
474 typedef struct {
475   u16 identifier;
476   u16 sequence;
477 } icmp_echo_header_t;
478
479 always_inline u32
480 ip_proto_to_snat_proto (u8 ip_proto)
481 {
482   u32 snat_proto = ~0;
483
484   snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
485   snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
486   snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
487   snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
488
489   return snat_proto;
490 }
491
492 always_inline u8
493 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
494 {
495   u8 ip_proto = ~0;
496
497   ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
498   ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
499   ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
500
501   return ip_proto;
502 }
503
504 typedef struct {
505   u16 src_port, dst_port;
506 } tcp_udp_header_t;
507
508 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
509                            u32 thread_index, vlib_buffer_t *b0,
510                            ip4_header_t *ip0, u8 *p_proto,
511                            snat_session_key_t *p_value,
512                            u8 *p_dont_translate, void *d, void *e);
513 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
514                            u32 thread_index, vlib_buffer_t *b0,
515                            ip4_header_t *ip0, u8 *p_proto,
516                            snat_session_key_t *p_value,
517                            u8 *p_dont_translate, void *d, void *e);
518 u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node,
519                           u32 thread_index, vlib_buffer_t *b0,
520                           ip4_header_t *ip0, u8 *p_proto,
521                           snat_session_key_t *p_value,
522                           u8 *p_dont_translate, void *d, void *e);
523 u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node,
524                            u32 thread_index, vlib_buffer_t *b0,
525                            ip4_header_t *ip0, u8 *p_proto,
526                            snat_session_key_t *p_value,
527                            u8 *p_dont_translate, void *d, void *e);
528 u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
529                            u32 thread_index, vlib_buffer_t *b0,
530                            ip4_header_t *ip0, u8 *p_proto,
531                            snat_session_key_t *p_value,
532                            u8 *p_dont_translate, void *d, void *e);
533 u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node,
534                           u32 thread_index, vlib_buffer_t *b0,
535                           ip4_header_t *ip0, u8 *p_proto,
536                           snat_session_key_t *p_value,
537                           u8 *p_dont_translate, void *d, void *e);
538 void increment_v4_address(ip4_address_t * a);
539 void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
540                       u8 twice_nat);
541 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
542                      u8 twice_nat);
543 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
544 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
545                             u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
546                             u32 sw_if_index, snat_protocol_t proto, int is_add,
547                             u8 twice_nat);
548 clib_error_t * snat_api_init(vlib_main_t * vm, snat_main_t * sm);
549 int snat_set_workers (uword * bitmap);
550 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
551 int snat_interface_add_del_output_feature(u32 sw_if_index, u8 is_inside,
552                                           int is_del);
553 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del,
554                                u8 twice_nat);
555 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
556 u8 * format_snat_protocol(u8 * s, va_list * args);
557 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
558                                      snat_protocol_t proto, u32 vrf_id,
559                                      nat44_lb_addr_port_t *locals, u8 is_add,
560                                      u8 twice_nat);
561 int nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
562                        snat_protocol_t proto, u32 vrf_id, int is_in);
563 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
564                             u32 thread_index);
565 snat_user_t * nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr,
566                                       u32 fib_index, u32 thread_index);
567 snat_session_t * nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u,
568                                                u32 thread_index);
569
570 static_always_inline u8
571 icmp_is_error_message (icmp46_header_t * icmp)
572 {
573   switch(icmp->type)
574     {
575     case ICMP4_destination_unreachable:
576     case ICMP4_time_exceeded:
577     case ICMP4_parameter_problem:
578     case ICMP4_source_quench:
579     case ICMP4_redirect:
580     case ICMP4_alternate_host_address:
581       return 1;
582     }
583   return 0;
584 }
585
586 static_always_inline u8
587 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0,
588                   u32 ip4_addr)
589 {
590   snat_runtime_t *rt = (snat_runtime_t *) node->runtime_data;
591   ip4_address_t * first_int_addr;
592
593   if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
594     {
595       first_int_addr =
596         ip4_interface_first_address (sm->ip4_main, sw_if_index0,
597                                      0 /* just want the address */);
598       rt->cached_sw_if_index = sw_if_index0;
599       if (first_int_addr)
600         rt->cached_ip4_address = first_int_addr->as_u32;
601       else
602         rt->cached_ip4_address = 0;
603     }
604
605   if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
606     return 1;
607   else
608     return 0;
609 }
610
611 always_inline u8
612 maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
613 {
614   if (pool_elts (sm->per_thread_data[thread_index].sessions) >= sm->max_translations)
615     return 1;
616
617   return 0;
618 }
619
620 static_always_inline void
621 nat_send_all_to_node(vlib_main_t *vm, u32 *bi_vector,
622                      vlib_node_runtime_t *node, vlib_error_t *error, u32 next)
623 {
624   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
625
626   from = bi_vector;
627   n_left_from = vec_len(bi_vector);
628   next_index = node->cached_next_index;
629   while (n_left_from > 0) {
630     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
631     while (n_left_from > 0 && n_left_to_next > 0) {
632       u32 bi0 = to_next[0] = from[0];
633       from += 1;
634       n_left_from -= 1;
635       to_next += 1;
636       n_left_to_next -= 1;
637       vlib_buffer_t *p0 = vlib_get_buffer(vm, bi0);
638       p0->error = *error;
639       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
640                                       n_left_to_next, bi0, next);
641     }
642     vlib_put_next_frame(vm, node, next_index, n_left_to_next);
643   }
644 }
645
646 #endif /* __included_snat_h__ */