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