nat: improve perf - long read after short write
[vpp.git] / src / plugins / nat / nat.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  * @file NAT plugin global declarations
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 <vnet/fib/fib_source.h>
27 #include <vppinfra/elog.h>
28 #include <vppinfra/bihash_8_8.h>
29 #include <vppinfra/bihash_16_8.h>
30 #include <vppinfra/dlist.h>
31 #include <vppinfra/error.h>
32 #include <vlibapi/api.h>
33 #include <vlib/log.h>
34 #include <vppinfra/bihash_16_8.h>
35
36 /* default session timeouts */
37 #define SNAT_UDP_TIMEOUT 300
38 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
39 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
40 #define SNAT_ICMP_TIMEOUT 60
41
42 /* number of worker handoff frame queue elements */
43 #define NAT_FQ_NELTS 64
44
45 /* NAT buffer flags */
46 #define SNAT_FLAG_HAIRPINNING (1 << 0)
47
48 typedef enum
49 {
50   NAT_NEXT_DROP,
51   NAT_NEXT_ICMP_ERROR,
52   NAT_NEXT_IN2OUT_PRE,
53   NAT_NEXT_OUT2IN_PRE,
54   NAT_NEXT_IN2OUT_ED_FAST_PATH,
55   NAT_NEXT_IN2OUT_ED_SLOW_PATH,
56   NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
57   NAT_NEXT_OUT2IN_ED_FAST_PATH,
58   NAT_NEXT_OUT2IN_ED_SLOW_PATH,
59   NAT_N_NEXT,
60 } nat_next_t;
61
62 typedef struct
63 {
64   u32 next_index;
65 } nat_pre_trace_t;
66
67 /*
68 STATIC_ASSERT (sizeof (nat_buffer_opaque_t) <=
69                STRUCT_SIZE_OF (vnet_buffer_opaque_t, unused),
70                "Custom meta-data too large for vnet_buffer_opaque_t");
71
72 #define nat_buffer_opaque(b) \
73   ((nat_buffer_opaque_t *)((u8 *)((b)->opaque) + \
74     STRUCT_OFFSET_OF (vnet_buffer_opaque_t, unused)))*/
75
76 /* session key (4-tuple) */
77 typedef struct
78 {
79   union
80   {
81     struct
82     {
83       ip4_address_t addr;
84       u16 port;
85       u16 protocol:3, fib_index:13;
86     };
87     u64 as_u64;
88   };
89 } snat_session_key_t;
90
91 /* deterministic session outside key */
92 typedef struct
93 {
94   union
95   {
96     struct
97     {
98       ip4_address_t ext_host_addr;
99       u16 ext_host_port;
100       u16 out_port;
101     };
102     u64 as_u64;
103   };
104 } snat_det_out_key_t;
105
106 /* user (internal host) key */
107 typedef struct
108 {
109   union
110   {
111     struct
112     {
113       ip4_address_t addr;
114       u32 fib_index;
115     };
116     u64 as_u64;
117   };
118 } snat_user_key_t;
119
120 /* NAT API Configuration flags */
121 #define foreach_nat_config_flag \
122   _(0x01, IS_TWICE_NAT)         \
123   _(0x02, IS_SELF_TWICE_NAT)    \
124   _(0x04, IS_OUT2IN_ONLY)       \
125   _(0x08, IS_ADDR_ONLY)         \
126   _(0x10, IS_OUTSIDE)           \
127   _(0x20, IS_INSIDE)            \
128   _(0x40, IS_STATIC)            \
129   _(0x80, IS_EXT_HOST_VALID)    \
130
131 typedef enum nat_config_flags_t_
132 {
133 #define _(n,f) NAT_API_##f = n,
134   foreach_nat_config_flag
135 #undef _
136 } nat_config_flags_t;
137
138 /* External address and port allocation modes */
139 #define foreach_nat_addr_and_port_alloc_alg \
140   _(0, DEFAULT, "default")         \
141   _(1, MAPE, "map-e")              \
142   _(2, RANGE, "port-range")
143
144 typedef enum
145 {
146 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
147   foreach_nat_addr_and_port_alloc_alg
148 #undef _
149 } nat_addr_and_port_alloc_alg_t;
150
151
152 /* Supported L4 protocols */
153 #define foreach_snat_protocol \
154   _(UDP, 0, udp, "udp")       \
155   _(TCP, 1, tcp, "tcp")       \
156   _(ICMP, 2, icmp, "icmp")
157
158 typedef enum
159 {
160 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
161   foreach_snat_protocol
162 #undef _
163 } snat_protocol_t;
164
165
166 /* Session state */
167 #define foreach_snat_session_state          \
168   _(0, UNKNOWN, "unknown")                 \
169   _(1, UDP_ACTIVE, "udp-active")           \
170   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
171   _(3, TCP_ESTABLISHED, "tcp-established") \
172   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
173   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
174   _(6, TCP_CLOSING, "tcp-closing")         \
175   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
176   _(8, TCP_CLOSED, "tcp-closed")           \
177   _(9, ICMP_ACTIVE, "icmp-active")
178
179 typedef enum
180 {
181 #define _(v, N, s) SNAT_SESSION_##N = v,
182   foreach_snat_session_state
183 #undef _
184 } snat_session_state_t;
185
186 #define foreach_nat_in2out_ed_error                     \
187 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
188 _(IN2OUT_PACKETS, "good in2out packets processed")      \
189 _(OUT_OF_PORTS, "out of ports")                         \
190 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
191 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
192 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
193 _(DROP_FRAGMENT, "drop fragment")                       \
194 _(CANNOT_CREATE_USER, "cannot create NAT user")         \
195 _(NON_SYN, "non-SYN packet try to create session")      \
196 _(TCP_PACKETS, "TCP packets")                           \
197 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
198 _(UDP_PACKETS, "UDP packets")                           \
199 _(ICMP_PACKETS, "ICMP packets")                         \
200 _(OTHER_PACKETS, "other protocol packets")              \
201 _(FRAGMENTS, "fragments")                               \
202 _(CACHED_FRAGMENTS, "cached fragments")                 \
203 _(PROCESSED_FRAGMENTS, "processed fragments")
204
205 typedef enum
206 {
207 #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
208   foreach_nat_in2out_ed_error
209 #undef _
210     NAT_IN2OUT_ED_N_ERROR,
211 } nat_in2out_ed_error_t;
212
213 #define foreach_nat_out2in_ed_error                     \
214 _(UNSUPPORTED_PROTOCOL, "unsupported protocol")         \
215 _(OUT2IN_PACKETS, "good out2in packets processed")      \
216 _(OUT_OF_PORTS, "out of ports")                         \
217 _(BAD_ICMP_TYPE, "unsupported ICMP type")               \
218 _(NO_TRANSLATION, "no translation")                     \
219 _(MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded")   \
220 _(MAX_USER_SESS_EXCEEDED, "max user sessions exceeded") \
221 _(DROP_FRAGMENT, "drop fragment")                       \
222 _(CANNOT_CREATE_USER, "cannot create NAT user")         \
223 _(NON_SYN, "non-SYN packet try to create session")      \
224 _(TCP_PACKETS, "TCP packets")                           \
225 _(TCP_CLOSED, "drops due to TCP in transitory timeout") \
226 _(UDP_PACKETS, "UDP packets")                           \
227 _(ICMP_PACKETS, "ICMP packets")                         \
228 _(OTHER_PACKETS, "other protocol packets")              \
229 _(FRAGMENTS, "fragments")                               \
230 _(CACHED_FRAGMENTS, "cached fragments")                 \
231 _(PROCESSED_FRAGMENTS, "processed fragments")
232
233 typedef enum
234 {
235 #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
236   foreach_nat_out2in_ed_error
237 #undef _
238     NAT_OUT2IN_ED_N_ERROR,
239 } nat_out2in_ed_error_t;
240
241
242 /* Endpoint dependent TCP session state */
243 #define NAT44_SES_I2O_FIN 1
244 #define NAT44_SES_O2I_FIN 2
245 #define NAT44_SES_I2O_FIN_ACK 4
246 #define NAT44_SES_O2I_FIN_ACK 8
247 #define NAT44_SES_I2O_SYN 16
248 #define NAT44_SES_O2I_SYN 32
249 #define NAT44_SES_RST     64
250
251 /* Session flags */
252 #define SNAT_SESSION_FLAG_STATIC_MAPPING       1
253 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO        2
254 #define SNAT_SESSION_FLAG_LOAD_BALANCING       4
255 #define SNAT_SESSION_FLAG_TWICE_NAT            8
256 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT   16
257 #define SNAT_SESSION_FLAG_FWD_BYPASS           32
258 #define SNAT_SESSION_FLAG_AFFINITY             64
259 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE       128
260
261 /* NAT interface flags */
262 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
263 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
264
265 /* Static mapping flags */
266 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY    1
267 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY  2
268 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
269 #define NAT_STATIC_MAPPING_FLAG_LB           8
270
271 /* *INDENT-OFF* */
272 typedef CLIB_PACKED(struct
273 {
274   /* Outside network key */
275   snat_session_key_t out2in;
276
277   /* Inside network key */
278   snat_session_key_t in2out;
279
280   /* Flags */
281   u32 flags;
282
283   /* Per-user translations */
284   u32 per_user_index;
285   u32 per_user_list_head_index;
286
287   /* index in global LRU list */
288   u32 global_lru_index;
289   f64 last_lru_update;
290
291   /* Last heard timer */
292   f64 last_heard;
293
294   /* Last HA refresh */
295   f64 ha_last_refreshed;
296
297   /* Counters */
298   u64 total_bytes;
299   u32 total_pkts;
300
301   /* External host address and port */
302   ip4_address_t ext_host_addr;
303   u16 ext_host_port;
304
305   /* External host address and port after translation */
306   ip4_address_t ext_host_nat_addr;
307   u16 ext_host_nat_port;
308
309   /* TCP session state */
310   u8 state;
311   u32 i2o_fin_seq;
312   u32 o2i_fin_seq;
313   u32 tcp_close_timestamp;
314
315   /* user index */
316   u32 user_index;
317 }) snat_session_t;
318 /* *INDENT-ON* */
319
320
321 typedef struct
322 {
323   ip4_address_t addr;
324   u32 fib_index;
325   u32 sessions_per_user_list_head_index;
326   u32 nsessions;
327   u32 nstaticsessions;
328 } snat_user_t;
329
330 typedef struct
331 {
332   ip4_address_t addr;
333   u32 fib_index;
334 /* *INDENT-OFF* */
335 #define _(N, i, n, s) \
336   u16 busy_##n##_ports; \
337   u16 * busy_##n##_ports_per_thread; \
338   u32 busy_##n##_port_refcounts[65535];
339   foreach_snat_protocol
340 #undef _
341 /* *INDENT-ON* */
342 } snat_address_t;
343
344 typedef struct
345 {
346   u32 fib_index;
347   u32 refcount;
348 } nat_outside_fib_t;
349
350 typedef struct
351 {
352   /* Inside network port */
353   u16 in_port;
354   /* Outside network address and port */
355   snat_det_out_key_t out;
356   /* Session state */
357   u8 state;
358   /* Expire timeout */
359   u32 expire;
360 } snat_det_session_t;
361
362 typedef struct
363 {
364   /* inside IP address range */
365   ip4_address_t in_addr;
366   u8 in_plen;
367   /* outside IP address range */
368   ip4_address_t out_addr;
369   u8 out_plen;
370   /* inside IP addresses / outside IP addresses */
371   u32 sharing_ratio;
372   /* number of ports available to internal host */
373   u16 ports_per_host;
374   /* session counter */
375   u32 ses_num;
376   /* vector of sessions */
377   snat_det_session_t *sessions;
378 } snat_det_map_t;
379
380 typedef struct
381 {
382   /* backend IP address */
383   ip4_address_t addr;
384   /* backend port number */
385   u16 port;
386   /* probability of the backend to be randomly matched */
387   u8 probability;
388   u8 prefix;
389   /* backend FIB table */
390   u32 vrf_id;
391   u32 fib_index;
392 } nat44_lb_addr_port_t;
393
394 typedef enum
395 {
396   /* twice-nat disabled */
397   TWICE_NAT_DISABLED,
398   /* twice-nat enabled */
399   TWICE_NAT,
400   /* twice-nat only when src IP equals dst IP after translation */
401   TWICE_NAT_SELF,
402 } twice_nat_type_t;
403
404 typedef enum
405 {
406   /* no load-balancing */
407   NO_LB_NAT,
408   /* load-balancing */
409   LB_NAT,
410   /* load-balancing with affinity */
411   AFFINITY_LB_NAT,
412 } lb_nat_type_t;
413
414 typedef struct
415 {
416   /* local IP address */
417   ip4_address_t local_addr;
418   /* external IP address */
419   ip4_address_t external_addr;
420   /* local port */
421   u16 local_port;
422   /* external port */
423   u16 external_port;
424   /* is twice-nat */
425   twice_nat_type_t twice_nat;
426   /* local FIB table */
427   u32 vrf_id;
428   u32 fib_index;
429   /* protocol */
430   snat_protocol_t proto;
431   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
432   u32 affinity;
433   /* worker threads used by backends/local host */
434   u32 *workers;
435   /* opaque string tag */
436   u8 *tag;
437   /* backends for load-balancing mode */
438   nat44_lb_addr_port_t *locals;
439   /* affinity per service lis */
440   u32 affinity_per_service_list_head_index;
441   /* flags */
442   u32 flags;
443 } snat_static_mapping_t;
444
445 typedef struct
446 {
447   u32 sw_if_index;
448   u8 flags;
449 } snat_interface_t;
450
451 typedef struct
452 {
453   ip4_address_t l_addr;
454   u16 l_port;
455   u16 e_port;
456   u32 sw_if_index;
457   u32 vrf_id;
458   snat_protocol_t proto;
459   u32 flags;
460   int addr_only;
461   int twice_nat;
462   int is_add;
463   int out2in_only;
464   int identity_nat;
465   u8 *tag;
466 } snat_static_map_resolve_t;
467
468 typedef struct
469 {
470   /* Main lookup tables */
471   clib_bihash_8_8_t out2in;
472   clib_bihash_8_8_t in2out;
473
474   /* Endpoint dependent sessions lookup tables */
475   clib_bihash_16_8_t out2in_ed;
476   clib_bihash_16_8_t in2out_ed;
477
478   /* Find-a-user => src address lookup */
479   clib_bihash_8_8_t user_hash;
480
481   /* User pool */
482   snat_user_t *users;
483
484   /* Session pool */
485   snat_session_t *sessions;
486
487   /* Pool of doubly-linked list elements */
488   dlist_elt_t *list_pool;
489
490   /* LRU session list - head is stale, tail is fresh */
491   dlist_elt_t *global_lru_pool;
492   u32 global_lru_head_index;
493
494   /* NAT thread index */
495   u32 snat_thread_index;
496
497   /* real thread index */
498   u32 thread_index;
499
500 } snat_main_per_thread_data_t;
501
502 struct snat_main_s;
503
504 /* ICMP session match function */
505 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
506                                           vlib_node_runtime_t * node,
507                                           u32 thread_index,
508                                           vlib_buffer_t * b0,
509                                           ip4_header_t * ip0, u8 * p_proto,
510                                           snat_session_key_t * p_value,
511                                           u8 * p_dont_translate, void *d,
512                                           void *e);
513
514 /* Return worker thread index for given packet */
515 typedef u32 (snat_get_worker_in2out_function_t) (ip4_header_t * ip,
516                                                  u32 rx_fib_index,
517                                                  u8 is_output);
518
519 typedef u32 (snat_get_worker_out2in_function_t) (vlib_buffer_t * b,
520                                                  ip4_header_t * ip,
521                                                  u32 rx_fib_index,
522                                                  u8 is_output);
523
524 /* NAT address and port allacotaion function */
525 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
526                                                       addresses,
527                                                       u32 fib_index,
528                                                       u32 thread_index,
529                                                       snat_session_key_t * k,
530                                                       u16 port_per_thread,
531                                                       u32 snat_thread_index);
532
533
534 typedef struct snat_main_s
535 {
536   /* ICMP session match functions */
537   snat_icmp_match_function_t *icmp_match_in2out_cb;
538   snat_icmp_match_function_t *icmp_match_out2in_cb;
539
540   /* Thread settings */
541   u32 num_workers;
542   u32 first_worker_index;
543   u32 *workers;
544   snat_get_worker_in2out_function_t *worker_in2out_cb;
545   snat_get_worker_out2in_function_t *worker_out2in_cb;
546   u16 port_per_thread;
547   u32 num_snat_thread;
548
549   /* Per thread data */
550   snat_main_per_thread_data_t *per_thread_data;
551
552   /* Find a static mapping by local */
553   clib_bihash_8_8_t static_mapping_by_local;
554
555   /* Find a static mapping by external */
556   clib_bihash_8_8_t static_mapping_by_external;
557
558   /* Static mapping pool */
559   snat_static_mapping_t *static_mappings;
560
561   /* Interface pool */
562   snat_interface_t *interfaces;
563   snat_interface_t *output_feature_interfaces;
564
565   /* Vector of outside addresses */
566   snat_address_t *addresses;
567   /* Address and port allocation function */
568   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
569   /* Address and port allocation type */
570   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
571   /* Port set parameters (MAP-E) */
572   u8 psid_offset;
573   u8 psid_length;
574   u16 psid;
575   /* Port range parameters */
576   u16 start_port;
577   u16 end_port;
578
579   /* vector of outside fibs */
580   nat_outside_fib_t *outside_fibs;
581
582   /* Vector of twice NAT addresses for extenal hosts */
583   snat_address_t *twice_nat_addresses;
584
585   /* sw_if_indices whose intfc addresses should be auto-added */
586   u32 *auto_add_sw_if_indices;
587   u32 *auto_add_sw_if_indices_twice_nat;
588
589   /* vector of interface address static mappings to resolve. */
590   snat_static_map_resolve_t *to_resolve;
591
592   /* Randomize port allocation order */
593   u32 random_seed;
594
595   /* Worker handoff frame-queue index */
596   u32 fq_in2out_index;
597   u32 fq_in2out_output_index;
598   u32 fq_out2in_index;
599
600   /* node indexes */
601   u32 error_node_index;
602
603   /* handoff fq nodes  */
604   u32 handoff_out2in_index;
605   u32 handoff_in2out_index;
606   u32 handoff_in2out_output_index;
607
608   /* respect feature arc nodes */
609   u32 pre_out2in_node_index;
610   u32 pre_in2out_node_index;
611
612   u32 in2out_node_index;
613   u32 in2out_output_node_index;
614   u32 in2out_fast_node_index;
615   u32 in2out_slowpath_node_index;
616   u32 in2out_slowpath_output_node_index;
617   u32 ed_in2out_node_index;
618   u32 ed_in2out_slowpath_node_index;
619   u32 out2in_node_index;
620   u32 out2in_fast_node_index;
621   u32 ed_out2in_node_index;
622   u32 ed_out2in_slowpath_node_index;
623   u32 det_in2out_node_index;
624   u32 det_out2in_node_index;
625
626   u32 hairpinning_node_index;
627   u32 hairpin_dst_node_index;
628   u32 hairpin_src_node_index;
629   u32 ed_hairpinning_node_index;
630   u32 ed_hairpin_dst_node_index;
631   u32 ed_hairpin_src_node_index;
632
633
634   /* Deterministic NAT mappings */
635   snat_det_map_t *det_maps;
636
637   /* If forwarding is enabled */
638   u8 forwarding_enabled;
639
640   /* Config parameters */
641   u8 static_mapping_only;
642   u8 static_mapping_connection_tracking;
643   u8 deterministic;
644   u8 out2in_dpo;
645   u8 endpoint_dependent;
646   u32 translation_buckets;
647   uword translation_memory_size;
648   u32 max_translations;
649   u32 user_buckets;
650   uword user_memory_size;
651   u32 max_translations_per_user;
652   u32 outside_vrf_id;
653   u32 outside_fib_index;
654   u32 inside_vrf_id;
655   u32 inside_fib_index;
656
657   /* values of various timeouts */
658   // proto timeouts
659   u32 udp_timeout;
660   u32 tcp_transitory_timeout;
661   u32 tcp_established_timeout;
662   u32 icmp_timeout;
663
664   /* TCP MSS clamping */
665   u16 mss_clamping;
666   u16 mss_value_net;
667
668   /* counters/gauges */
669   vlib_simple_counter_main_t total_users;
670   vlib_simple_counter_main_t total_sessions;
671
672   /* API message ID base */
673   u16 msg_id_base;
674
675   /* log class */
676   vlib_log_class_t log_class;
677   /* logging level */
678   u8 log_level;
679
680   /* convenience */
681   vlib_main_t *vlib_main;
682   vnet_main_t *vnet_main;
683   ip4_main_t *ip4_main;
684   ip_lookup_main_t *ip4_lookup_main;
685   api_main_t *api_main;
686 } snat_main_t;
687
688 typedef struct
689 {
690   u32 thread_index;
691   f64 now;
692 } nat44_is_idle_session_ctx_t;
693
694 typedef struct
695 {
696   u32 cached_sw_if_index;
697   u32 cached_ip4_address;
698 } snat_runtime_t;
699
700 extern snat_main_t snat_main;
701
702 // nat pre ed next_node feature classification
703 extern vlib_node_registration_t nat_default_node;
704 extern vlib_node_registration_t nat_pre_in2out_node;
705 extern vlib_node_registration_t nat_pre_out2in_node;
706
707 extern vlib_node_registration_t snat_in2out_node;
708 extern vlib_node_registration_t snat_in2out_output_node;
709 extern vlib_node_registration_t snat_out2in_node;
710 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
711 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
712 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
713 extern vlib_node_registration_t snat_det_in2out_node;
714 extern vlib_node_registration_t snat_det_out2in_node;
715 extern vlib_node_registration_t nat44_ed_in2out_node;
716 extern vlib_node_registration_t nat44_ed_in2out_output_node;
717 extern vlib_node_registration_t nat44_ed_out2in_node;
718
719 extern fib_source_t nat_fib_src_hi;
720 extern fib_source_t nat_fib_src_low;
721
722 /* format functions */
723 format_function_t format_snat_user;
724 format_function_t format_snat_user_v2;
725 format_function_t format_snat_static_mapping;
726 format_function_t format_snat_static_map_to_resolve;
727 format_function_t format_snat_session;
728 format_function_t format_det_map_ses;
729 format_function_t format_snat_key;
730 format_function_t format_static_mapping_key;
731 format_function_t format_snat_protocol;
732 format_function_t format_nat_addr_and_port_alloc_alg;
733 /* unformat functions */
734 unformat_function_t unformat_snat_protocol;
735
736 /** \brief Check if SNAT session is created from static mapping.
737     @param s SNAT session
738     @return 1 if SNAT session is created from static mapping otherwise 0
739 */
740 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
741
742 /** \brief Check if SNAT session for unknown protocol.
743     @param s SNAT session
744     @return 1 if SNAT session for unknown protocol otherwise 0
745 */
746 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
747
748 /** \brief Check if NAT session is twice NAT.
749     @param s NAT session
750     @return 1 if NAT session is twice NAT
751 */
752 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
753
754 /** \brief Check if NAT session is load-balancing.
755     @param s NAT session
756     @return 1 if NAT session is load-balancing
757 */
758 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
759
760 /** \brief Check if NAT session is forwarding bypass.
761     @param s NAT session
762     @return 1 if NAT session is load-balancing
763 */
764 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
765
766 /** \brief Check if NAT session is endpoint dependent.
767     @param s NAT session
768     @return 1 if NAT session is endpoint dependent
769 */
770 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
771
772 /** \brief Check if NAT session has affinity record.
773     @param s NAT session
774     @return 1 if NAT session has affinity record
775 */
776 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
777
778 /** \brief Check if NAT interface is inside.
779     @param i NAT interfce
780     @return 1 if inside interface
781 */
782 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
783
784 /** \brief Check if NAT interface is outside.
785     @param i NAT interfce
786     @return 1 if outside interface
787 */
788 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
789
790 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
791     @param s NAT session
792     @return 1 if session is closed
793 */
794 #define nat44_is_ses_closed(s) s->state == 0xf
795
796 /** \brief Check if NAT static mapping is address only (1:1NAT).
797     @param sm NAT static mapping
798     @return 1 if 1:1NAT, 0 if 1:1NAPT
799 */
800 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
801
802 /** \brief Check if NAT static mapping match only out2in direction.
803     @param sm NAT static mapping
804     @return 1 if rule match only out2in direction
805 */
806 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
807
808 /** \brief Check if NAT static mapping is identity NAT.
809     @param sm NAT static mapping
810     @return 1 if identity NAT
811 */
812 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
813
814 /** \brief Check if NAT static mapping is load-balancing.
815     @param sm NAT static mapping
816     @return 1 if load-balancing
817 */
818 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
819
820 /** \brief Check if client initiating TCP connection (received SYN from client)
821     @param t TCP header
822     @return 1 if client initiating TCP connection
823 */
824 always_inline bool
825 tcp_flags_is_init (u8 f)
826 {
827   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
828 }
829
830 /* logging */
831 #define nat_log_err(...) \
832   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
833 #define nat_log_warn(...) \
834   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
835 #define nat_log_notice(...) \
836   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
837 #define nat_log_info(...) \
838   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
839 #define nat_log_debug(...)\
840   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
841
842 /* NAT API Logging Levels */
843 #define foreach_nat_log_level \
844   _(0x00, LOG_NONE)           \
845   _(0x01, LOG_ERROR)          \
846   _(0x02, LOG_WARNING)        \
847   _(0x03, LOG_NOTICE)         \
848   _(0x04, LOG_INFO)           \
849   _(0x05, LOG_DEBUG)
850
851 typedef enum nat_log_level_t_
852 {
853 #define _(n,f) SNAT_##f = n,
854   foreach_nat_log_level
855 #undef _
856 } nat_log_level_t;
857
858 #define nat_elog(_level, _str)                    \
859 do                                                \
860   {                                               \
861     snat_main_t *sm = &snat_main;                 \
862     if (PREDICT_FALSE (sm->log_level >= _level))  \
863       {                                           \
864         ELOG_TYPE_DECLARE (e) =                   \
865           {                                       \
866             .format = "nat-msg " _str,            \
867             .format_args = "",                    \
868           };                                      \
869         ELOG_DATA (&sm->vlib_main->elog_main, e); \
870       }                                           \
871   } while (0);
872
873 #define nat_elog_addr(_level, _str, _addr)               \
874 do                                                       \
875   {                                                      \
876     if (PREDICT_FALSE (sm->log_level >= _level))         \
877       {                                                  \
878         ELOG_TYPE_DECLARE (e) =                          \
879           {                                              \
880             .format = "nat-msg " _str " %d.%d.%d.%d",    \
881             .format_args = "i1i1i1i1",                   \
882           };                                             \
883         CLIB_PACKED(struct                               \
884           {                                              \
885             u8 oct1;                                     \
886             u8 oct2;                                     \
887             u8 oct3;                                     \
888             u8 oct4;                                     \
889           }) *ed;                                        \
890         ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
891         ed->oct4 = _addr >> 24;                          \
892         ed->oct3 = _addr >> 16;                          \
893         ed->oct2 = _addr >> 8;                           \
894         ed->oct1 = _addr;                                \
895     }                                                    \
896   } while (0);
897
898 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst)                \
899 do                                                                          \
900   {                                                                         \
901   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                      \
902     {                                                                       \
903       ELOG_TYPE_DECLARE (e) =                                               \
904         {                                                                   \
905           .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
906                                     " tid from: %d to: %d fib: %d",         \
907         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                            \
908       };                                                                    \
909       CLIB_PACKED(struct                                                    \
910         {                                                                   \
911           u8 src_oct1;                                                      \
912           u8 src_oct2;                                                      \
913           u8 src_oct3;                                                      \
914           u8 src_oct4;                                                      \
915           u8 dst_oct1;                                                      \
916           u8 dst_oct2;                                                      \
917           u8 dst_oct3;                                                      \
918           u8 dst_oct4;                                                      \
919           u32 ftid;                                                         \
920           u32 ttid;                                                         \
921           u32 fib;                                                          \
922         }) *ed;                                                             \
923       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                      \
924       ed->src_oct1 = _src >> 24;                                            \
925       ed->src_oct2 = _src >> 16;                                            \
926       ed->src_oct3 = _src >> 8;                                             \
927       ed->src_oct4 = _src;                                                  \
928       ed->dst_oct1 = _dst >> 24;                                            \
929       ed->dst_oct2 = _dst >> 16;                                            \
930       ed->dst_oct3 = _dst >> 8;                                             \
931       ed->dst_oct4 = _dst;                                                  \
932       ed->ftid = vlib_get_thread_index ();                                  \
933       ed->ttid = _tid;                                                      \
934       ed->fib = _fib;                                                       \
935     }                                                                       \
936   } while (0);
937
938 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst)              \
939 do                                                                           \
940   {                                                                          \
941   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                       \
942     {                                                                        \
943       ELOG_TYPE_DECLARE (e) =                                                \
944         {                                                                    \
945           .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
946                                     " tid:%d prt:%d fib:%d",                 \
947         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                             \
948       };                                                                     \
949       CLIB_PACKED(struct                                                     \
950         {                                                                    \
951           u8 src_oct1;                                                       \
952           u8 src_oct2;                                                       \
953           u8 src_oct3;                                                       \
954           u8 src_oct4;                                                       \
955           u8 dst_oct1;                                                       \
956           u8 dst_oct2;                                                       \
957           u8 dst_oct3;                                                       \
958           u8 dst_oct4;                                                       \
959           u32 tid;                                                           \
960           u32 prt;                                                           \
961           u32 fib;                                                           \
962         }) *ed;                                                              \
963       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                       \
964       ed->src_oct1 = _src >> 24;                                             \
965       ed->src_oct2 = _src >> 16;                                             \
966       ed->src_oct3 = _src >> 8;                                              \
967       ed->src_oct4 = _src;                                                   \
968       ed->dst_oct1 = _dst >> 24;                                             \
969       ed->dst_oct2 = _dst >> 16;                                             \
970       ed->dst_oct3 = _dst >> 8;                                              \
971       ed->dst_oct4 = _dst;                                                   \
972       ed->tid = vlib_get_thread_index ();                                    \
973       ed->prt = _prt;                                                        \
974       ed->fib = _fib;                                                        \
975     }                                                                        \
976   } while (0);
977
978 #define nat_elog_X1(_level, _fmt, _arg, _val1)         \
979 do                                                     \
980   {                                                    \
981     snat_main_t *sm = &snat_main;                      \
982     if (PREDICT_FALSE (sm->log_level >= _level))       \
983       {                                                \
984         ELOG_TYPE_DECLARE (e) =                        \
985           {                                            \
986             .format = "nat-msg " _fmt,                 \
987             .format_args = _arg,                       \
988           };                                           \
989         CLIB_PACKED(struct                             \
990           {                                            \
991             typeof (_val1) val1;                       \
992           }) *ed;                                      \
993         ed = ELOG_DATA (&sm->vlib_main->elog_main, e); \
994         ed->val1 = _val1;                              \
995       }                                                \
996   } while (0);
997
998 #define nat_elog_notice(nat_elog_str) \
999   nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1000 #define nat_elog_warn(nat_elog_str) \
1001   nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1002 #define nat_elog_err(nat_elog_str) \
1003   nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1004 #define nat_elog_debug(nat_elog_str) \
1005   nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1006 #define nat_elog_info(nat_elog_str) \
1007   nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1008
1009 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1010   nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1011 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1012   nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1013 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1014   nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1015 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1016   nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1017 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1018   nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1019
1020 /* ICMP session match functions */
1021 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1022                             u32 thread_index, vlib_buffer_t * b0,
1023                             ip4_header_t * ip0, u8 * p_proto,
1024                             snat_session_key_t * p_value,
1025                             u8 * p_dont_translate, void *d, void *e);
1026 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1027                             u32 thread_index, vlib_buffer_t * b0,
1028                             ip4_header_t * ip0, u8 * p_proto,
1029                             snat_session_key_t * p_value,
1030                             u8 * p_dont_translate, void *d, void *e);
1031 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1032                             u32 thread_index, vlib_buffer_t * b0,
1033                             ip4_header_t * ip0, u8 * p_proto,
1034                             snat_session_key_t * p_value,
1035                             u8 * p_dont_translate, void *d, void *e);
1036 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1037                             u32 thread_index, vlib_buffer_t * b0,
1038                             ip4_header_t * ip0, u8 * p_proto,
1039                             snat_session_key_t * p_value,
1040                             u8 * p_dont_translate, void *d, void *e);
1041
1042 /* ICMP deterministic NAT session match functions */
1043 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
1044                            u32 thread_index, vlib_buffer_t * b0,
1045                            ip4_header_t * ip0, u8 * p_proto,
1046                            snat_session_key_t * p_value,
1047                            u8 * p_dont_translate, void *d, void *e);
1048 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
1049                            u32 thread_index, vlib_buffer_t * b0,
1050                            ip4_header_t * ip0, u8 * p_proto,
1051                            snat_session_key_t * p_value,
1052                            u8 * p_dont_translate, void *d, void *e);
1053
1054 /* ICMP endpoint-dependent session match functions */
1055 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1056                           u32 thread_index, vlib_buffer_t * b0,
1057                           ip4_header_t * ip0, u8 * p_proto,
1058                           snat_session_key_t * p_value,
1059                           u8 * p_dont_translate, void *d, void *e);
1060 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1061                           u32 thread_index, vlib_buffer_t * b0,
1062                           ip4_header_t * ip0, u8 * p_proto,
1063                           snat_session_key_t * p_value,
1064                           u8 * p_dont_translate, void *d, void *e);
1065
1066 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1067                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1068                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1069                  void *d, void *e);
1070
1071 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1072                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1073                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1074                  void *d, void *e);
1075
1076 /* hairpinning functions */
1077 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1078                            ip4_header_t * ip0, icmp46_header_t * icmp0,
1079                            int is_ed);
1080 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1081                                        ip4_header_t * ip);
1082 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1083                                          ip4_header_t * ip);
1084 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1085                       ip4_header_t * ip0, udp_header_t * udp0,
1086                       tcp_header_t * tcp0, u32 proto0, int is_ed);
1087
1088 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1089 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1090 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1091 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1092 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1093
1094 /**
1095  * @brief Add external address to NAT44 pool
1096  *
1097  * @param addr      IPv4 address
1098  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
1099  * @param twice_nat 1 if twice NAT address
1100  *
1101  * @return 0 on success, non-zero value otherwise
1102  */
1103 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1104                       u8 twice_nat);
1105
1106 /**
1107  * @brief Delete external address from NAT44 pool
1108  *
1109  * @param addr      IPv4 address
1110  * @param delete_sm 1 if delete static mapping using address
1111  * @param twice_nat 1 if twice NAT address
1112  *
1113  * @return 0 on success, non-zero value otherwise
1114  */
1115 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1116                       u8 twice_nat);
1117
1118 /**
1119  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1120  *
1121  * @param addr   IPv4 address
1122  * @param is_add 1 = add, 0 = delete
1123  *
1124  * @return 0 on success, non-zero value otherwise
1125  */
1126 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1127
1128 /**
1129  * @brief Add/delete NAT44 static mapping
1130  *
1131  * @param l_addr       local IPv4 address
1132  * @param e_addr       external IPv4 address
1133  * @param l_port       local port number
1134  * @param e_port       external port number
1135  * @param vrf_id       local VRF ID
1136  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
1137  * @param sw_if_index  use interface address as external IPv4 address
1138  * @param proto        L4 protocol
1139  * @param is_add       1 = add, 0 = delete
1140  * @param twice_nat    twice-nat mode
1141  * @param out2in_only  if 1 rule match only out2in direction
1142  * @param tag          opaque string tag
1143  * @param identity_nat identity NAT
1144  *
1145  * @return 0 on success, non-zero value otherwise
1146  */
1147 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1148                              u16 l_port, u16 e_port, u32 vrf_id,
1149                              int addr_only, u32 sw_if_index,
1150                              snat_protocol_t proto, int is_add,
1151                              twice_nat_type_t twice_nat, u8 out2in_only,
1152                              u8 * tag, u8 identity_nat);
1153
1154 /**
1155  * @brief Add/delete static mapping with load-balancing (multiple backends)
1156  *
1157  * @param e_addr      external IPv4 address
1158  * @param e_port      external port number
1159  * @param proto       L4 protocol
1160  * @param locals      list of local backends
1161  * @param is_add      1 = add, 0 = delete
1162  * @param twice_nat   twice-nat mode
1163  * @param out2in_only if 1 rule match only out2in direction
1164  * @param tag         opaque string tag
1165  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
1166  *
1167  * @return 0 on success, non-zero value otherwise
1168  */
1169 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1170                                      snat_protocol_t proto,
1171                                      nat44_lb_addr_port_t * locals, u8 is_add,
1172                                      twice_nat_type_t twice_nat,
1173                                      u8 out2in_only, u8 * tag, u32 affinity);
1174
1175 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1176                                            ip4_address_t l_addr, u16 l_port,
1177                                            snat_protocol_t proto, u32 vrf_id,
1178                                            u8 probability, u8 is_add);
1179
1180 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1181
1182 /**
1183  * @brief Set NAT plugin workers
1184  *
1185  * @param bitmap NAT workers bitmap
1186  *
1187  * @return 0 on success, non-zero value otherwise
1188  */
1189 int snat_set_workers (uword * bitmap);
1190
1191 /**
1192  * @brief Enable/disable NAT44 feature on the interface
1193  *
1194  * @param sw_if_index software index of the interface
1195  * @param is_inside   1 = inside, 0 = outside
1196  * @param is_del      1 = delete, 0 = add
1197  *
1198  * @return 0 on success, non-zero value otherwise
1199  */
1200 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1201
1202 /**
1203  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1204  *
1205  * @param sw_if_index software index of the interface
1206  * @param is_inside   1 = inside, 0 = outside
1207  * @param is_del      1 = delete, 0 = add
1208  *
1209  * @return 0 on success, non-zero value otherwise
1210  */
1211 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1212                                            int is_del);
1213
1214 /**
1215  * @brief Add/delete NAT44 pool address from specific interfce
1216  *
1217  * @param sw_if_index software index of the interface
1218  * @param is_del      1 = delete, 0 = add
1219  * @param twice_nat   1 = twice NAT address for extenal hosts
1220  *
1221  * @return 0 on success, non-zero value otherwise
1222  */
1223 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1224                                 u8 twice_nat);
1225
1226 /**
1227  * @brief Delete NAT44 session
1228  *
1229  * @param addr   IPv4 address
1230  * @param port   L4 port number
1231  * @param proto  L4 protocol
1232  * @param vrf_id VRF ID
1233  * @param is_in  1 = inside network address and port pair, 0 = outside
1234  *
1235  * @return 0 on success, non-zero value otherwise
1236  */
1237 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1238                        snat_protocol_t proto, u32 vrf_id, int is_in);
1239
1240 /**
1241  * @brief Delete NAT44 endpoint-dependent session
1242  *
1243  * @param addr   IPv4 address
1244  * @param port   L4 port number
1245  * @param proto  L4 protocol
1246  * @param vrf_id VRF ID
1247  * @param is_in  1 = inside network address and port pair, 0 = outside
1248  *
1249  * @return 0 on success, non-zero value otherwise
1250  */
1251 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1252                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1253                           u32 vrf_id, int is_in);
1254
1255 /**
1256  * @brief Free NAT44 session data (lookup keys, external addrres port)
1257  *
1258  * @param s            NAT session
1259  * @param thread_index thread index
1260  * @param is_ha        is HA event
1261  */
1262 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1263                             u32 thread_index, u8 is_ha);
1264
1265 /**
1266  * @brief Free NAT44 ED session data (lookup keys, external addrres port)
1267  *
1268  * @param s            NAT session
1269  * @param thread_index thread index
1270  * @param is_ha        is HA event
1271  */
1272 void
1273 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1274                          u32 thread_index, u8 is_ha);
1275 /**
1276  * @brief Find or create NAT user
1277  *
1278  * @param addr         IPv4 address
1279  * @param fib_index    FIB table index
1280  * @param thread_index thread index
1281  *
1282  * @return NAT user data structure on success otherwise zero value
1283  */
1284 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1285                                      ip4_address_t * addr, u32 fib_index,
1286                                      u32 thread_index);
1287
1288 /**
1289  * @brief Allocate new NAT session or recycle last used
1290  *
1291  * @param u            NAT user
1292  * @param thread_index thread index
1293  *
1294  * @return session data structure on success otherwise zero value
1295  */
1296 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1297                                               snat_user_t * u,
1298                                               u32 thread_index, f64 now);
1299
1300 /**
1301  * @brief Allocate NAT endpoint-dependent session
1302  *
1303  * @param thread_index thread index
1304  *
1305  * @return session data structure on success otherwise zero value
1306  */
1307 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, u32 thread_index,
1308                                       f64 now);
1309
1310 /**
1311  * @brief Set address and port assignment algorithm for MAP-E CE
1312  *
1313  * @param psid        Port Set Identifier value
1314  * @param psid_offset number of offset bits
1315  * @param psid_length length of PSID
1316  */
1317 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1318                                        u16 psid_length);
1319
1320 /**
1321  * @brief Set address and port assignment algorithm for port range
1322  *
1323  * @param start_port beginning of the port range
1324  * @param end_port   end of the port range
1325  */
1326 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1327
1328 /**
1329  * @brief Set address and port assignment algorithm to default/standard
1330  */
1331 void nat_set_alloc_addr_and_port_default (void);
1332
1333 /**
1334  * @brief Free outside address and port pair
1335  *
1336  * @param addresses    vector of outside addresses
1337  * @param thread_index thread index
1338  * @param k            address, port and protocol
1339  */
1340 void snat_free_outside_address_and_port (snat_address_t * addresses,
1341                                          u32 thread_index,
1342                                          snat_session_key_t * k);
1343
1344 /**
1345  * @brief Alloc outside address and port
1346  *
1347  * @param addresses         vector of outside addresses
1348  * @param fib_index         FIB table index
1349  * @param thread_index      thread index
1350  * @param k                 allocated address and port pair
1351  * @param port_per_thread   number of ports per threead
1352  * @param snat_thread_index NAT thread index
1353  *
1354  * @return 0 on success, non-zero value otherwise
1355  */
1356 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1357                                          u32 fib_index,
1358                                          u32 thread_index,
1359                                          snat_session_key_t * k,
1360                                          u16 port_per_thread,
1361                                          u32 snat_thread_index);
1362
1363 /**
1364  * @brief Match NAT44 static mapping.
1365  *
1366  * @param match         address and port to match
1367  * @param mapping       external/local address and port of the matched mapping
1368  * @param by_external   if 0 match by local address otherwise match by external
1369  *                      address
1370  * @param is_addr_only  1 if matched mapping is address only
1371  * @param twice_nat     matched mapping is twice NAT type
1372  * @param lb            1 if matched mapping is load-balanced
1373  * @param ext_host_addr external host address
1374  *
1375  * @returns 0 if match found otherwise 1.
1376  */
1377 int snat_static_mapping_match (snat_main_t * sm,
1378                                snat_session_key_t match,
1379                                snat_session_key_t * mapping,
1380                                u8 by_external,
1381                                u8 * is_addr_only,
1382                                twice_nat_type_t * twice_nat,
1383                                lb_nat_type_t * lb,
1384                                ip4_address_t * ext_host_addr,
1385                                u8 * is_identity_nat);
1386
1387 /**
1388  * @brief Add/del NAT address to FIB.
1389  *
1390  * Add the external NAT address to the FIB as receive entries. This ensures
1391  * that VPP will reply to ARP for this address and we don't need to enable
1392  * proxy ARP on the outside interface.
1393  *
1394  * @param addr        IPv4 address
1395  * @param plen        address prefix length
1396  * @param sw_if_index software index of the outside interface
1397  * @param is_add      0 = delete, 1 = add.
1398  */
1399 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1400                                u8 p_len, u32 sw_if_index, int is_add);
1401
1402 /*
1403  * Why is this here? Because we don't need to touch this layer to
1404  * simply reply to an icmp. We need to change id to a unique
1405  * value to NAT an echo request/reply.
1406  */
1407
1408 typedef struct
1409 {
1410   u16 identifier;
1411   u16 sequence;
1412 } icmp_echo_header_t;
1413
1414 typedef struct
1415 {
1416   u16 src_port, dst_port;
1417 } tcp_udp_header_t;
1418
1419 int nat_global_lru_free_one (snat_main_t * sm, int thread_index, f64 now);
1420
1421 #endif /* __included_nat_h__ */
1422 /*
1423  * fd.io coding-style-patch-verification: ON
1424  *
1425  * Local Variables:
1426  * eval: (c-set-style "gnu")
1427  * End:
1428  */