FD.io VPP  v17.07.01-10-g3be13f0
Vector Packet Processing
snat.h
Go to the documentation of this file.
1 
2 /*
3  * snat.h - simple nat 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_snat_h__
19 #define __included_snat_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/dlist.h>
28 #include <vppinfra/error.h>
29 #include <vlibapi/api.h>
30 
31 
32 #define SNAT_UDP_TIMEOUT 300
33 #define SNAT_UDP_TIMEOUT_MIN 120
34 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
35 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
36 #define SNAT_TCP_INCOMING_SYN 6
37 #define SNAT_ICMP_TIMEOUT 60
38 
39 /* Key */
40 typedef struct {
41  union
42  {
43  struct
44  {
47  u16 protocol:3,
48  fib_index:13;
49  };
51  };
53 
54 typedef struct {
55  union
56  {
57  struct
58  {
62  };
64  };
66 
67 typedef struct {
68  union
69  {
70  struct
71  {
74  };
76  };
78 
79 typedef struct {
80  union
81  {
82  struct
83  {
87  };
89  };
91 
92 
93 #define foreach_snat_protocol \
94  _(UDP, 0, udp, "udp") \
95  _(TCP, 1, tcp, "tcp") \
96  _(ICMP, 2, icmp, "icmp")
97 
98 typedef enum {
99 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
101 #undef _
103 
104 
105 #define foreach_snat_session_state \
106  _(0, UNKNOWN, "unknown") \
107  _(1, UDP_ACTIVE, "udp-active") \
108  _(2, TCP_SYN_SENT, "tcp-syn-sent") \
109  _(3, TCP_ESTABLISHED, "tcp-established") \
110  _(4, TCP_FIN_WAIT, "tcp-fin-wait") \
111  _(5, TCP_CLOSE_WAIT, "tcp-close-wait") \
112  _(6, TCP_LAST_ACK, "tcp-last-ack") \
113  _(7, ICMP_ACTIVE, "icmp-active")
114 
115 typedef enum {
116 #define _(v, N, s) SNAT_SESSION_##N = v,
118 #undef _
120 
121 
122 #define SNAT_SESSION_FLAG_STATIC_MAPPING 1
123 
124 typedef CLIB_PACKED(struct {
125  snat_session_key_t out2in; /* 0-15 */
126 
127  snat_session_key_t in2out; /* 16-31 */
128 
129  u32 flags; /* 32-35 */
130 
131  /* per-user translations */
132  u32 per_user_index; /* 36-39 */
133 
134  u32 per_user_list_head_index; /* 40-43 */
135 
136  /* Last heard timer */
137  f64 last_heard; /* 44-51 */
138 
139  u64 total_bytes; /* 52-59 */
140 
141  u32 total_pkts; /* 60-63 */
142 
143  /* Outside address */
144  u32 outside_address_index; /* 64-67 */
145 
146 }) snat_session_t;
147 
148 
149 typedef struct {
155 } snat_user_t;
156 
157 typedef struct {
160 #define _(N, i, n, s) \
161  u32 busy_##n##_ports; \
162  uword * busy_##n##_port_bitmap;
164 #undef _
165 } snat_address_t;
166 
167 typedef struct {
173 
174 typedef struct {
182  /* vector of sessions */
185 
186 typedef struct {
194  snat_protocol_t proto;
196 
197 typedef struct {
201 
202 typedef struct {
208  snat_protocol_t proto;
210  int is_add;
212 
213 typedef struct {
214  /* User pool */
216 
217  /* Session pool */
218  snat_session_t * sessions;
219 
220  /* Pool of doubly-linked list elements */
223 
224 struct snat_main_s;
225 
227  vlib_node_runtime_t *node,
228  u32 thread_index,
229  vlib_buffer_t *b0,
230  u8 *p_proto,
231  snat_session_key_t *p_value,
232  u8 *p_dont_translate,
233  void *d,
234  void *e);
235 
236 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip, u32 rx_fib_index);
237 
238 typedef struct snat_main_s {
239  /* Main lookup tables */
240  clib_bihash_8_8_t out2in;
241  clib_bihash_8_8_t in2out;
242 
243  /* Find-a-user => src address lookup */
244  clib_bihash_8_8_t user_hash;
245 
246  /* Non-translated packets worker lookup => src address + VRF */
247  clib_bihash_8_8_t worker_by_in;
248 
249  /* Translated packets worker lookup => IP address + port number */
250  clib_bihash_8_8_t worker_by_out;
251 
254 
261 
262  /* Per thread data */
264 
265  /* Find a static mapping by local */
266  clib_bihash_8_8_t static_mapping_by_local;
267 
268  /* Find a static mapping by external */
269  clib_bihash_8_8_t static_mapping_by_external;
270 
271  /* Static mapping pool */
273 
274  /* Interface pool */
276 
277  /* Vector of outside addresses */
278  snat_address_t * addresses;
279 
280  /* sw_if_indices whose intfc addresses should be auto-added */
282 
283  /* vector of interface address static mappings to resolve. */
285 
286  /* Randomize port allocation order */
288 
289  /* Worker handoff index */
292 
293  /* in2out and out2in node index */
296 
297  /* Deterministic NAT */
299 
300  /* Config parameters */
313 
314  /* tenant VRF aware address pool activation flag */
316 
317  /* values of various timeouts */
322 
323  /* API message ID base */
325 
326  /* convenience */
332 } snat_main_t;
333 
334 extern snat_main_t snat_main;
343 
345  snat_session_key_t * k,
346  u32 address_index);
347 
349  u32 fib_index,
350  snat_session_key_t * k,
351  u32 * address_indexp);
352 
354  snat_session_key_t match,
355  snat_session_key_t * mapping,
356  u8 by_external,
357  u8 *is_addr_only);
358 
360  u8 p_len,
361  u32 sw_if_index,
362  int is_add);
363 
365 
366 typedef struct {
370 
371 /** \brief Check if SNAT session is created from static mapping.
372  @param s SNAT session
373  @return 1 if SNAT session is created from static mapping otherwise 0
374 */
375 #define snat_is_session_static(s) s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING
376 
377 /*
378  * Why is this here? Because we don't need to touch this layer to
379  * simply reply to an icmp. We need to change id to a unique
380  * value to NAT an echo request/reply.
381  */
382 
383 typedef struct {
387 
390 {
391  u32 snat_proto = ~0;
392 
393  snat_proto = (ip_proto == IP_PROTOCOL_UDP) ? SNAT_PROTOCOL_UDP : snat_proto;
394  snat_proto = (ip_proto == IP_PROTOCOL_TCP) ? SNAT_PROTOCOL_TCP : snat_proto;
395  snat_proto = (ip_proto == IP_PROTOCOL_ICMP) ? SNAT_PROTOCOL_ICMP : snat_proto;
396  snat_proto = (ip_proto == IP_PROTOCOL_ICMP6) ? SNAT_PROTOCOL_ICMP : snat_proto;
397 
398  return snat_proto;
399 }
400 
402 snat_proto_to_ip_proto (snat_protocol_t snat_proto)
403 {
404  u8 ip_proto = ~0;
405 
406  ip_proto = (snat_proto == SNAT_PROTOCOL_UDP) ? IP_PROTOCOL_UDP : ip_proto;
407  ip_proto = (snat_proto == SNAT_PROTOCOL_TCP) ? IP_PROTOCOL_TCP : ip_proto;
408  ip_proto = (snat_proto == SNAT_PROTOCOL_ICMP) ? IP_PROTOCOL_ICMP : ip_proto;
409 
410  return ip_proto;
411 }
412 
413 typedef struct {
414  u16 src_port, dst_port;
416 
418  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
419  snat_session_key_t *p_value,
420  u8 *p_dont_translate, void *d, void *e);
422  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
423  snat_session_key_t *p_value,
424  u8 *p_dont_translate, void *d, void *e);
426  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
427  snat_session_key_t *p_value,
428  u8 *p_dont_translate, void *d, void *e);
430  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
431  snat_session_key_t *p_value,
432  u8 *p_dont_translate, void *d, void *e);
434  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
435  snat_session_key_t *p_value,
436  u8 *p_dont_translate, void *d, void *e);
438  u32 thread_index, vlib_buffer_t *b0, u8 *p_proto,
439  snat_session_key_t *p_value,
440  u8 *p_dont_translate, void *d, void *e);
443 int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm);
445  u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
446  u32 sw_if_index, snat_protocol_t proto, int is_add);
448 int snat_set_workers (uword * bitmap);
449 int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del);
450 int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del);
451 uword unformat_snat_protocol(unformat_input_t * input, va_list * args);
452 u8 * format_snat_protocol(u8 * s, va_list * args);
453 
455 icmp_is_error_message (icmp46_header_t * icmp)
456 {
457  switch(icmp->type)
458  {
459  case ICMP4_destination_unreachable:
460  case ICMP4_time_exceeded:
461  case ICMP4_parameter_problem:
462  case ICMP4_source_quench:
463  case ICMP4_redirect:
464  case ICMP4_alternate_host_address:
465  return 1;
466  }
467  return 0;
468 }
469 
472  u32 ip4_addr)
473 {
475  ip4_address_t * first_int_addr;
476 
477  if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
478  {
479  first_int_addr =
480  ip4_interface_first_address (sm->ip4_main, sw_if_index0,
481  0 /* just want the address */);
482  rt->cached_sw_if_index = sw_if_index0;
483  if (first_int_addr)
484  rt->cached_ip4_address = first_int_addr->as_u32;
485  else
486  rt->cached_ip4_address = 0;
487  }
488 
489  if (PREDICT_FALSE(ip4_addr == rt->cached_ip4_address))
490  return 1;
491  else
492  return 0;
493 }
494 
495 #endif /* __included_snat_h__ */
ip4_address_t external_addr
Definition: snat.h:188
u32 user_memory_size
Definition: snat.h:307
typedef CLIB_PACKED(struct{snat_session_key_t out2in;snat_session_key_t in2out;u32 flags;u32 per_user_index;u32 per_user_list_head_index;f64 last_heard;u64 total_bytes;u32 total_pkts;u32 outside_address_index;}) snat_session_t
u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation and create session if needed...
Definition: out2in.c:288
u32 sessions_per_user_list_head_index
Definition: snat.h:152
u16 ext_host_port
Definition: snat.h:60
int snat_alloc_outside_address_and_port(snat_main_t *sm, u32 fib_index, snat_session_key_t *k, u32 *address_indexp)
Definition: snat.c:891
a
Definition: bitmap.h:516
u32 icmp_timeout
Definition: snat.h:321
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:468
u32 snat_icmp_match_function_t(struct snat_main_s *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Definition: snat.h:226
u16 fib_index
Definition: snat.h:86
u32 nsessions
Definition: snat.h:153
snat_protocol_t proto
Definition: snat.h:194
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:684
clib_bihash_8_8_t in2out
Definition: snat.h:241
static_always_inline u8 icmp_is_error_message(icmp46_header_t *icmp)
Definition: snat.h:455
vlib_node_registration_t snat_out2in_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node)
Definition: out2in.c:83
u32 nstaticsessions
Definition: snat.h:154
struct _vlib_node_registration vlib_node_registration_t
vlib_node_registration_t snat_det_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_det_in2out_node)
Definition: in2out.c:90
u32 fib_index
Definition: snat.h:151
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
snat_det_map_t * det_maps
Definition: snat.h:298
u8 vrf_mode
Definition: snat.h:315
u32 icmp_match_out2in_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation.
Definition: out2in.c:398
dlist_elt_t * list_pool
Definition: snat.h:221
u8 in_plen
Definition: snat.h:176
clib_bihash_8_8_t worker_by_out
Definition: snat.h:250
u8 deterministic
Definition: snat.h:303
u32 user_buckets
Definition: snat.h:306
u32 cached_sw_if_index
Definition: snat.h:367
int snat_interface_add_del(u32 sw_if_index, u8 is_inside, int is_del)
Definition: snat.c:621
u32 max_translations_per_user
Definition: snat.h:308
#define static_always_inline
Definition: clib.h:85
u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation and create session if needed...
Definition: in2out.c:477
ip4_address_t ext_host_addr
Definition: snat.h:59
clib_bihash_8_8_t out2in
Definition: snat.h:240
#define always_inline
Definition: clib.h:84
u32 translation_buckets
Definition: snat.h:304
ip4_address_t addr
Definition: snat.h:150
static_always_inline u8 is_interface_addr(snat_main_t *sm, vlib_node_runtime_t *node, u32 sw_if_index0, u32 ip4_addr)
Definition: snat.h:471
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
Definition: in2out.c:86
ip4_main_t * ip4_main
Definition: snat.h:329
unsigned long u64
Definition: types.h:89
void snat_add_address(snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
Definition: snat.c:128
ip4_address_t local_addr
Definition: snat.h:187
snat_protocol_t proto
Definition: snat.h:208
format_function_t format_snat_user
Definition: snat.h:364
snat_session_state_t
Definition: snat.h:115
uword unformat_snat_protocol(unformat_input_t *input, va_list *args)
Definition: snat.c:1112
snat_det_session_t * sessions
Definition: snat.h:183
vlib_main_t * vlib_main
Definition: snat.h:327
int snat_del_address(snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
Definition: snat.c:521
struct _unformat_input_t unformat_input_t
snat_static_mapping_t * static_mappings
Definition: snat.h:272
u32 inside_fib_index
Definition: snat.h:312
int snat_set_workers(uword *bitmap)
Definition: snat.c:695
u32 udp_timeout
Definition: snat.h:318
u8 static_mapping_only
Definition: snat.h:301
clib_bihash_8_8_t user_hash
Definition: snat.h:244
#define PREDICT_FALSE(x)
Definition: clib.h:97
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
Definition: in2out.c:89
u32( snat_get_worker_function_t)(ip4_header_t *ip, u32 rx_fib_index)
Definition: snat.h:236
clib_bihash_8_8_t static_mapping_by_external
Definition: snat.h:269
api_main_t * api_main
Definition: snat.h:331
u8 out_plen
Definition: snat.h:178
ip4_address_t addr
Definition: snat.h:84
vnet_main_t * vnet_main
Definition: snat.h:328
u32 inside_vrf_id
Definition: snat.h:311
void snat_free_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 address_index)
Definition: snat.c:794
u32 fq_out2in_index
Definition: snat.h:291
u16 src_port
Definition: snat.h:414
vlib_node_registration_t snat_det_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_det_out2in_node)
Definition: out2in.c:84
snat_user_t * users
Definition: snat.h:215
vlib_node_registration_t snat_out2in_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_node)
Definition: out2in.c:81
u32 random_seed
Definition: snat.h:287
int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, u32 vrf_id, int addr_only, u32 sw_if_index, snat_protocol_t proto, int is_add)
Add static mapping.
Definition: snat.c:223
struct snat_main_s snat_main_t
u32 outside_vrf_id
Definition: snat.h:309
u8 static_mapping_connection_tracking
Definition: snat.h:302
snat_get_worker_function_t * worker_in2out_cb
Definition: snat.h:259
void increment_v4_address(ip4_address_t *a)
Definition: snat.c:175
u32 sharing_ratio
Definition: snat.h:179
ip4_address_t out_addr
Definition: snat.h:177
u32 outside_fib_index
Definition: snat.h:310
u32 icmp_match_out2in_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation and create session if needed...
Definition: out2in.c:1702
ip4_address_t addr
Definition: snat.h:45
clib_bihash_8_8_t worker_by_in
Definition: snat.h:247
u32 sw_if_index
Definition: snat.h:198
static u8 snat_proto_to_ip_proto(snat_protocol_t snat_proto)
Definition: snat.h:402
u8 * format_snat_protocol(u8 *s, va_list *args)
Definition: snat.c:1126
#define foreach_snat_session_state
Definition: snat.h:105
u32 tcp_transitory_timeout
Definition: snat.h:320
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external, u8 *is_addr_only)
Match SNAT static mapping.
Definition: snat.c:835
clib_error_t * snat_api_init(vlib_main_t *vm, snat_main_t *sm)
Definition: snat_api.c:1850
u32 * auto_add_sw_if_indices
Definition: snat.h:281
snat_protocol_t
Definition: snat.h:98
u32 num_workers
Definition: snat.h:255
unsigned int u32
Definition: types.h:88
u32 first_worker_index
Definition: snat.h:256
snat_get_worker_function_t * worker_out2in_cb
Definition: snat.h:260
ip4_address_t l_addr
Definition: snat.h:203
snat_icmp_match_function_t * icmp_match_out2in_cb
Definition: snat.h:253
IPv4 main type.
Definition: ip4.h:83
vlib_node_registration_t snat_out2in_fast_node
(constructor) VLIB_REGISTER_NODE (snat_out2in_fast_node)
Definition: out2in.c:82
u64 as_u64
Definition: snat.h:75
vlib_node_registration_t snat_in2out_fast_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_fast_node)
Definition: in2out.c:88
snat_main_t snat_main
Definition: jvpp_snat.h:39
ip4_address_t addr
Definition: snat.h:72
ip4_address_t in_addr
Definition: snat.h:175
u32 fq_in2out_index
Definition: snat.h:290
u32 out2in_node_index
Definition: snat.h:295
ip4_address_t addr
Definition: snat.h:158
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
double f64
Definition: types.h:142
u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation.
Definition: in2out.c:569
unsigned char u8
Definition: types.h:56
u16 msg_id_base
Definition: snat.h:324
u16 ports_per_host
Definition: snat.h:180
u32 * workers
Definition: snat.h:258
snat_main_per_thread_data_t * per_thread_data
Definition: snat.h:263
int snat_add_interface_address(snat_main_t *sm, u32 sw_if_index, int is_del)
Definition: snat.c:2043
snat_det_out_key_t out
Definition: snat.h:169
void snat_add_del_addr_to_fib(ip4_address_t *addr, u8 p_len, u32 sw_if_index, int is_add)
Add/del NAT address to FIB.
Definition: snat.c:96
u32 fib_index
Definition: snat.h:73
snat_address_t * addresses
Definition: snat.h:278
static u32 ip_proto_to_snat_proto(u8 ip_proto)
Definition: snat.h:389
u32 in2out_node_index
Definition: snat.h:294
snat_static_map_resolve_t * to_resolve
Definition: snat.h:284
u32 translation_memory_size
Definition: snat.h:305
u32 ses_num
Definition: snat.h:181
u32 next_worker
Definition: snat.h:257
#define foreach_snat_protocol
Definition: snat.h:93
vhost_vring_addr_t addr
Definition: vhost-user.h:82
u32 flags
Definition: vhost-user.h:76
ip_lookup_main_t * ip4_lookup_main
Definition: snat.h:330
snat_session_t * sessions
Definition: snat.h:218
u32 icmp_match_in2out_det(snat_main_t *sm, vlib_node_runtime_t *node, u32 thread_index, vlib_buffer_t *b0, u8 *p_proto, snat_session_key_t *p_value, u8 *p_dont_translate, void *d, void *e)
Get address and port values to be used for packet SNAT translation and create session if needed...
Definition: in2out.c:2251
u32 cached_ip4_address
Definition: snat.h:368
snat_icmp_match_function_t * icmp_match_in2out_cb
Definition: snat.h:252
clib_bihash_8_8_t static_mapping_by_local
Definition: snat.h:266
u32 fib_index
Definition: snat.h:159
snat_interface_t * interfaces
Definition: snat.h:275
u32 tcp_established_timeout
Definition: snat.h:319