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