nat: fixed cli nat summary and nat limit
[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   /* Is translation memory size calculated or user defined */
594   u8 translation_memory_size_set;
595
596   u32 translation_buckets;
597   uword translation_memory_size;
598   u32 max_translations_per_thread;
599   u32 *max_translations_per_fib;
600   u32 max_users_per_thread;
601   u32 user_buckets;
602   uword user_memory_size;
603   u32 max_translations_per_user;
604
605   u32 outside_vrf_id;
606   u32 outside_fib_index;
607   u32 inside_vrf_id;
608   u32 inside_fib_index;
609
610   /* values of various timeouts */
611   // proto timeouts
612   u32 udp_timeout;
613   u32 tcp_transitory_timeout;
614   u32 tcp_established_timeout;
615   u32 icmp_timeout;
616
617   /* TCP MSS clamping */
618   u16 mss_clamping;
619
620   /* counters/gauges */
621   vlib_simple_counter_main_t total_users;
622   vlib_simple_counter_main_t total_sessions;
623   vlib_simple_counter_main_t user_limit_reached;
624
625 #define _(x) vlib_simple_counter_main_t x;
626   struct
627   {
628     struct
629     {
630       struct
631       {
632         foreach_nat_counter;
633       } in2out;
634
635       struct
636       {
637         foreach_nat_counter;
638       } out2in;
639
640       struct
641       {
642         foreach_nat_counter;
643       } in2out_ed;
644
645       struct
646       {
647         foreach_nat_counter;
648       } out2in_ed;
649     } fastpath;
650
651     struct
652     {
653       struct
654       {
655         foreach_nat_counter;
656       } in2out;
657
658       struct
659       {
660         foreach_nat_counter;
661       } out2in;
662
663       struct
664       {
665         foreach_nat_counter;
666       } in2out_ed;
667
668       struct
669       {
670         foreach_nat_counter;
671       } out2in_ed;
672     } slowpath;
673
674     vlib_simple_counter_main_t hairpinning;
675   } counters;
676 #undef _
677
678   /* API message ID base */
679   u16 msg_id_base;
680
681   /* log class */
682   vlib_log_class_t log_class;
683   /* logging level */
684   u8 log_level;
685
686   /* convenience */
687   vnet_main_t *vnet_main;
688   ip4_main_t *ip4_main;
689   ip_lookup_main_t *ip4_lookup_main;
690   api_main_t *api_main;
691 } snat_main_t;
692
693 typedef struct
694 {
695   u32 thread_index;
696   f64 now;
697 } nat44_is_idle_session_ctx_t;
698
699 typedef struct
700 {
701   u32 cached_sw_if_index;
702   u32 cached_ip4_address;
703 } snat_runtime_t;
704
705 extern snat_main_t snat_main;
706
707 // nat pre ed next_node feature classification
708 extern vlib_node_registration_t nat_default_node;
709 extern vlib_node_registration_t nat_pre_in2out_node;
710 extern vlib_node_registration_t nat_pre_out2in_node;
711
712 extern vlib_node_registration_t snat_in2out_node;
713 extern vlib_node_registration_t snat_in2out_output_node;
714 extern vlib_node_registration_t snat_out2in_node;
715 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
716 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
717 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
718 extern vlib_node_registration_t nat44_ed_in2out_node;
719 extern vlib_node_registration_t nat44_ed_in2out_output_node;
720 extern vlib_node_registration_t nat44_ed_out2in_node;
721
722 extern fib_source_t nat_fib_src_hi;
723 extern fib_source_t nat_fib_src_low;
724
725 /* format functions */
726 format_function_t format_snat_user;
727 format_function_t format_snat_static_mapping;
728 format_function_t format_snat_static_map_to_resolve;
729 format_function_t format_snat_session;
730 format_function_t format_snat_key;
731 format_function_t format_static_mapping_key;
732 format_function_t format_nat_protocol;
733 format_function_t format_nat_addr_and_port_alloc_alg;
734 /* unformat functions */
735 unformat_function_t unformat_nat_protocol;
736
737 /** \brief Check if SNAT session is created from static mapping.
738     @param s SNAT session
739     @return 1 if SNAT session is created from static mapping otherwise 0
740 */
741 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
742
743 /** \brief Check if SNAT session for unknown protocol.
744     @param s SNAT session
745     @return 1 if SNAT session for unknown protocol otherwise 0
746 */
747 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
748
749 /** \brief Check if NAT session is twice NAT.
750     @param s NAT session
751     @return 1 if NAT session is twice NAT
752 */
753 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
754
755 /** \brief Check if NAT session is load-balancing.
756     @param s NAT session
757     @return 1 if NAT session is load-balancing
758 */
759 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
760
761 /** \brief Check if NAT session is forwarding bypass.
762     @param s NAT session
763     @return 1 if NAT session is load-balancing
764 */
765 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
766
767 /** \brief Check if NAT session is endpoint dependent.
768     @param s NAT session
769     @return 1 if NAT session is endpoint dependent
770 */
771 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
772
773 /** \brief Check if NAT session has affinity record.
774     @param s NAT session
775     @return 1 if NAT session has affinity record
776 */
777 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
778
779 /** \brief Check if NAT interface is inside.
780     @param i NAT interface
781     @return 1 if inside interface
782 */
783 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
784
785 /** \brief Check if NAT interface is outside.
786     @param i NAT interface
787     @return 1 if outside interface
788 */
789 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
790
791 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
792     @param s NAT session
793     @return 1 if session is closed
794 */
795 #define nat44_is_ses_closed(s) s->state == 0xf
796
797 /** \brief Check if NAT static mapping is address only (1:1NAT).
798     @param sm NAT static mapping
799     @return 1 if 1:1NAT, 0 if 1:1NAPT
800 */
801 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
802
803 /** \brief Check if NAT static mapping match only out2in direction.
804     @param sm NAT static mapping
805     @return 1 if rule match only out2in direction
806 */
807 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
808
809 /** \brief Check if NAT static mapping is identity NAT.
810     @param sm NAT static mapping
811     @return 1 if identity NAT
812 */
813 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
814
815 /** \brief Check if NAT static mapping is load-balancing.
816     @param sm NAT static mapping
817     @return 1 if load-balancing
818 */
819 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
820
821 /** \brief Check if client initiating TCP connection (received SYN from client)
822     @param t TCP header
823     @return 1 if client initiating TCP connection
824 */
825 always_inline bool
826 tcp_flags_is_init (u8 f)
827 {
828   return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
829 }
830
831 /* logging */
832 #define nat_log_err(...) \
833   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
834 #define nat_log_warn(...) \
835   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
836 #define nat_log_notice(...) \
837   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
838 #define nat_log_info(...) \
839   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
840 #define nat_log_debug(...)\
841   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
842
843 /* NAT API Logging Levels */
844 #define foreach_nat_log_level \
845   _(0x00, LOG_NONE)           \
846   _(0x01, LOG_ERROR)          \
847   _(0x02, LOG_WARNING)        \
848   _(0x03, LOG_NOTICE)         \
849   _(0x04, LOG_INFO)           \
850   _(0x05, LOG_DEBUG)
851
852 typedef enum nat_log_level_t_
853 {
854 #define _(n,f) SNAT_##f = n,
855   foreach_nat_log_level
856 #undef _
857 } nat_log_level_t;
858
859 #define nat_elog(_level, _str)                           \
860 do                                                       \
861   {                                                      \
862     snat_main_t *sm = &snat_main;                        \
863     if (PREDICT_FALSE (sm->log_level >= _level))         \
864       {                                                  \
865         ELOG_TYPE_DECLARE (e) =                          \
866           {                                              \
867             .format = "nat-msg " _str,                   \
868             .format_args = "",                           \
869           };                                             \
870         ELOG_DATA (&vlib_global_main.elog_main, e);      \
871       }                                                  \
872   } while (0);
873
874 #define nat_elog_addr(_level, _str, _addr)               \
875 do                                                       \
876   {                                                      \
877     if (PREDICT_FALSE (sm->log_level >= _level))         \
878       {                                                  \
879         ELOG_TYPE_DECLARE (e) =                          \
880           {                                              \
881             .format = "nat-msg " _str " %d.%d.%d.%d",    \
882             .format_args = "i1i1i1i1",                   \
883           };                                             \
884         CLIB_PACKED(struct                               \
885           {                                              \
886             u8 oct1;                                     \
887             u8 oct2;                                     \
888             u8 oct3;                                     \
889             u8 oct4;                                     \
890           }) *ed;                                        \
891         ed = ELOG_DATA (&vlib_global_main.elog_main, e); \
892         ed->oct4 = _addr >> 24;                          \
893         ed->oct3 = _addr >> 16;                          \
894         ed->oct2 = _addr >> 8;                           \
895         ed->oct1 = _addr;                                \
896     }                                                    \
897   } while (0);
898
899 #define nat_elog_debug_handoff(_str, _tid, _fib, _src, _dst)                \
900 do                                                                          \
901   {                                                                         \
902   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                      \
903     {                                                                       \
904       ELOG_TYPE_DECLARE (e) =                                               \
905         {                                                                   \
906           .format = "nat-msg " _str " ip src: %d.%d.%d.%d dst: %d.%d.%d.%d" \
907                                     " tid from: %d to: %d fib: %d",         \
908         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                            \
909       };                                                                    \
910       CLIB_PACKED(struct                                                    \
911         {                                                                   \
912           u8 src_oct1;                                                      \
913           u8 src_oct2;                                                      \
914           u8 src_oct3;                                                      \
915           u8 src_oct4;                                                      \
916           u8 dst_oct1;                                                      \
917           u8 dst_oct2;                                                      \
918           u8 dst_oct3;                                                      \
919           u8 dst_oct4;                                                      \
920           u32 ftid;                                                         \
921           u32 ttid;                                                         \
922           u32 fib;                                                          \
923         }) *ed;                                                             \
924       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                      \
925       ed->src_oct1 = _src >> 24;                                            \
926       ed->src_oct2 = _src >> 16;                                            \
927       ed->src_oct3 = _src >> 8;                                             \
928       ed->src_oct4 = _src;                                                  \
929       ed->dst_oct1 = _dst >> 24;                                            \
930       ed->dst_oct2 = _dst >> 16;                                            \
931       ed->dst_oct3 = _dst >> 8;                                             \
932       ed->dst_oct4 = _dst;                                                  \
933       ed->ftid = vlib_get_thread_index ();                                  \
934       ed->ttid = _tid;                                                      \
935       ed->fib = _fib;                                                       \
936     }                                                                       \
937   } while (0);
938
939 #define nat_elog_debug_handoff_v2(_str, _prt, _fib, _src, _dst)              \
940 do                                                                           \
941   {                                                                          \
942   if (PREDICT_FALSE (sm->log_level >= SNAT_LOG_DEBUG))                       \
943     {                                                                        \
944       ELOG_TYPE_DECLARE (e) =                                                \
945         {                                                                    \
946           .format = "nat-msg " _str " ip_src:%d.%d.%d.%d ip_dst:%d.%d.%d.%d" \
947                                     " tid:%d prt:%d fib:%d",                 \
948         .format_args = "i1i1i1i1i1i1i1i1i4i4i4",                             \
949       };                                                                     \
950       CLIB_PACKED(struct                                                     \
951         {                                                                    \
952           u8 src_oct1;                                                       \
953           u8 src_oct2;                                                       \
954           u8 src_oct3;                                                       \
955           u8 src_oct4;                                                       \
956           u8 dst_oct1;                                                       \
957           u8 dst_oct2;                                                       \
958           u8 dst_oct3;                                                       \
959           u8 dst_oct4;                                                       \
960           u32 tid;                                                           \
961           u32 prt;                                                           \
962           u32 fib;                                                           \
963         }) *ed;                                                              \
964       ed = ELOG_DATA (&vlib_global_main.elog_main, e);                       \
965       ed->src_oct1 = _src >> 24;                                             \
966       ed->src_oct2 = _src >> 16;                                             \
967       ed->src_oct3 = _src >> 8;                                              \
968       ed->src_oct4 = _src;                                                   \
969       ed->dst_oct1 = _dst >> 24;                                             \
970       ed->dst_oct2 = _dst >> 16;                                             \
971       ed->dst_oct3 = _dst >> 8;                                              \
972       ed->dst_oct4 = _dst;                                                   \
973       ed->tid = vlib_get_thread_index ();                                    \
974       ed->prt = _prt;                                                        \
975       ed->fib = _fib;                                                        \
976     }                                                                        \
977   } while (0);
978
979 #define nat_elog_X1(_level, _fmt, _arg, _val1)            \
980 do                                                        \
981   {                                                       \
982     snat_main_t *sm = &snat_main;                         \
983     if (PREDICT_FALSE (sm->log_level >= _level))          \
984       {                                                   \
985         ELOG_TYPE_DECLARE (e) =                           \
986           {                                               \
987             .format = "nat-msg " _fmt,                    \
988             .format_args = _arg,                          \
989           };                                              \
990         CLIB_PACKED(struct                                \
991           {                                               \
992             typeof (_val1) val1;                          \
993           }) *ed;                                         \
994         ed = ELOG_DATA (&vlib_global_main.elog_main, e);  \
995         ed->val1 = _val1;                                 \
996       }                                                   \
997   } while (0);
998
999 #define nat_elog_notice(nat_elog_str) \
1000   nat_elog(SNAT_LOG_INFO, "[notice] " nat_elog_str)
1001 #define nat_elog_warn(nat_elog_str) \
1002   nat_elog(SNAT_LOG_WARNING, "[warning] " nat_elog_str)
1003 #define nat_elog_err(nat_elog_str) \
1004   nat_elog(SNAT_LOG_ERROR, "[error] " nat_elog_str)
1005 #define nat_elog_debug(nat_elog_str) \
1006   nat_elog(SNAT_LOG_DEBUG, "[debug] " nat_elog_str)
1007 #define nat_elog_info(nat_elog_str) \
1008   nat_elog(SNAT_LOG_INFO, "[info] " nat_elog_str)
1009
1010 #define nat_elog_notice_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1011   nat_elog_X1(SNAT_LOG_NOTICE, "[notice] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1012 #define nat_elog_warn_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1013   nat_elog_X1(SNAT_LOG_WARNING, "[warning] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1014 #define nat_elog_err_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1015   nat_elog_X1(SNAT_LOG_ERROR, "[error] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1016 #define nat_elog_debug_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1017   nat_elog_X1(SNAT_LOG_DEBUG, "[debug] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1018 #define nat_elog_info_X1(nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1) \
1019   nat_elog_X1(SNAT_LOG_INFO, "[info] " nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
1020
1021 /* ICMP session match functions */
1022 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1023                             u32 thread_index, vlib_buffer_t * b0,
1024                             ip4_header_t * ip0, ip4_address_t * addr,
1025                             u16 * port, u32 * fib_index,
1026                             nat_protocol_t * proto, void *d, void *e,
1027                             u8 * dont_translate);
1028 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
1029                             u32 thread_index, vlib_buffer_t * b0,
1030                             ip4_header_t * ip0, ip4_address_t * addr,
1031                             u16 * port, u32 * fib_index,
1032                             nat_protocol_t * proto, void *d, void *e,
1033                             u8 * dont_translate);
1034 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
1035                             u32 thread_index, vlib_buffer_t * b0,
1036                             ip4_header_t * ip0, ip4_address_t * addr,
1037                             u16 * port, u32 * fib_index,
1038                             nat_protocol_t * proto, void *d, void *e,
1039                             u8 * dont_translate);
1040 u32 icmp_match_out2in_slow (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
1047 /* ICMP endpoint-dependent session match functions */
1048 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1049                           u32 thread_index, vlib_buffer_t * b0,
1050                           ip4_header_t * ip0, ip4_address_t * addr,
1051                           u16 * port, u32 * fib_index, nat_protocol_t * proto,
1052                           void *d, void *e, u8 * dont_translate);
1053 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
1054                           u32 thread_index, vlib_buffer_t * b0,
1055                           ip4_header_t * ip0, ip4_address_t * addr,
1056                           u16 * port, u32 * fib_index, nat_protocol_t * proto,
1057                           void *d, void *e, u8 * dont_translate);
1058
1059 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1060                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1061                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1062                  void *d, void *e);
1063
1064 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
1065                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
1066                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
1067                  void *d, void *e);
1068
1069 /* hairpinning functions */
1070 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
1071                            ip4_header_t * ip0, icmp46_header_t * icmp0,
1072                            int is_ed);
1073 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1074                                        ip4_header_t * ip);
1075 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
1076                                          ip4_header_t * ip);
1077 int snat_hairpinning (vlib_main_t * vm, vlib_node_runtime_t * node,
1078                       snat_main_t * sm, vlib_buffer_t * b0,
1079                       ip4_header_t * ip0, udp_header_t * udp0,
1080                       tcp_header_t * tcp0, u32 proto0, int is_ed,
1081                       int do_trace);
1082
1083 /* Call back functions for clib_bihash_add_or_overwrite_stale */
1084 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1085 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
1086 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1087 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
1088
1089 /**
1090  * @brief Add external address to NAT44 pool
1091  *
1092  * @param sm        snat global configuration data
1093  * @param addr      IPv4 address
1094  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
1095  * @param twice_nat 1 if twice NAT address
1096  *
1097  * @return 0 on success, non-zero value otherwise
1098  */
1099 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
1100                       u8 twice_nat);
1101
1102 /**
1103  * @brief Delete external address from NAT44 pool
1104  *
1105  * @param sm        snat global configuration data
1106  * @param addr      IPv4 address
1107  * @param delete_sm 1 if delete static mapping using address
1108  * @param twice_nat 1 if twice NAT address
1109  *
1110  * @return 0 on success, non-zero value otherwise
1111  */
1112 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1113                       u8 twice_nat);
1114
1115 /**
1116  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
1117  *
1118  * @param addr   IPv4 address
1119  * @param is_add 1 = add, 0 = delete
1120  *
1121  * @return 0 on success, non-zero value otherwise
1122  */
1123 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
1124
1125 /**
1126  * @brief Add/delete NAT44 static mapping
1127  *
1128  * @param l_addr       local IPv4 address
1129  * @param e_addr       external IPv4 address
1130  * @param l_port       local port number
1131  * @param e_port       external port number
1132  * @param vrf_id       local VRF ID
1133  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
1134  * @param sw_if_index  use interface address as external IPv4 address
1135  * @param proto        L4 protocol
1136  * @param is_add       1 = add, 0 = delete
1137  * @param twice_nat    twice-nat mode
1138  * @param out2in_only  if 1 rule match only out2in direction
1139  * @param tag          opaque string tag
1140  * @param identity_nat identity NAT
1141  *
1142  * @return 0 on success, non-zero value otherwise
1143  */
1144 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1145                              u16 l_port, u16 e_port, u32 vrf_id,
1146                              int addr_only, u32 sw_if_index,
1147                              nat_protocol_t proto, int is_add,
1148                              twice_nat_type_t twice_nat, u8 out2in_only,
1149                              u8 * tag, u8 identity_nat);
1150
1151 /**
1152  * @brief Add/delete static mapping with load-balancing (multiple backends)
1153  *
1154  * @param e_addr      external IPv4 address
1155  * @param e_port      external port number
1156  * @param proto       L4 protocol
1157  * @param locals      list of local backends
1158  * @param is_add      1 = add, 0 = delete
1159  * @param twice_nat   twice-nat mode
1160  * @param out2in_only if 1 rule match only out2in direction
1161  * @param tag         opaque string tag
1162  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
1163  *
1164  * @return 0 on success, non-zero value otherwise
1165  */
1166 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1167                                      nat_protocol_t proto,
1168                                      nat44_lb_addr_port_t * locals, u8 is_add,
1169                                      twice_nat_type_t twice_nat,
1170                                      u8 out2in_only, u8 * tag, u32 affinity);
1171
1172 int nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1173                                            ip4_address_t l_addr, u16 l_port,
1174                                            nat_protocol_t proto, u32 vrf_id,
1175                                            u8 probability, u8 is_add);
1176
1177 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
1178
1179 /**
1180  * @brief Set NAT plugin workers
1181  *
1182  * @param bitmap NAT workers bitmap
1183  *
1184  * @return 0 on success, non-zero value otherwise
1185  */
1186 int snat_set_workers (uword * bitmap);
1187
1188 /**
1189  * @brief Enable/disable NAT44 feature on the interface
1190  *
1191  * @param sw_if_index software index of the interface
1192  * @param is_inside   1 = inside, 0 = outside
1193  * @param is_del      1 = delete, 0 = add
1194  *
1195  * @return 0 on success, non-zero value otherwise
1196  */
1197 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
1198
1199 /**
1200  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
1201  *
1202  * @param sw_if_index software index of the interface
1203  * @param is_inside   1 = inside, 0 = outside
1204  * @param is_del      1 = delete, 0 = add
1205  *
1206  * @return 0 on success, non-zero value otherwise
1207  */
1208 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
1209                                            int is_del);
1210
1211 /**
1212  * @brief Add/delete NAT44 pool address from specific interface
1213  *
1214  * @param sw_if_index software index of the interface
1215  * @param is_del      1 = delete, 0 = add
1216  * @param twice_nat   1 = twice NAT address for external hosts
1217  *
1218  * @return 0 on success, non-zero value otherwise
1219  */
1220 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
1221                                 u8 twice_nat);
1222
1223 /**
1224  * @brief Delete NAT44 session
1225  *
1226  * @param addr   IPv4 address
1227  * @param port   L4 port number
1228  * @param proto  L4 protocol
1229  * @param vrf_id VRF ID
1230  * @param is_in  1 = inside network address and port pair, 0 = outside
1231  *
1232  * @return 0 on success, non-zero value otherwise
1233  */
1234 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1235                        nat_protocol_t proto, u32 vrf_id, int is_in);
1236
1237 /**
1238  * @brief Delete NAT44 endpoint-dependent session
1239  *
1240  * @param sm     snat global configuration data
1241  * @param addr   IPv4 address
1242  * @param port   L4 port number
1243  * @param proto  L4 protocol
1244  * @param vrf_id VRF ID
1245  * @param is_in  1 = inside network address and port pair, 0 = outside
1246  *
1247  * @return 0 on success, non-zero value otherwise
1248  */
1249 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
1250                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
1251                           u32 vrf_id, int is_in);
1252
1253 /**
1254  * @brief Free NAT44 session data (lookup keys, external address port)
1255  *
1256  * @param sm           snat global configuration data
1257  * @param s            NAT session
1258  * @param thread_index thread index
1259  * @param is_ha        is HA event
1260  */
1261 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
1262                             u32 thread_index, u8 is_ha);
1263
1264 /**
1265  * @brief Set NAT44 session limit (session limit, vrf id)
1266  *
1267  * @param session_limit Session limit
1268  * @param vrf_id        VRF id
1269  * @return 0 on success, non-zero value otherwise
1270  */
1271 int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
1272
1273 /**
1274  * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
1275  *
1276  * @param session_limit Session limit
1277  * @param vrf_id        VRF id
1278  * @return 0 on success, non-zero value otherwise
1279  */
1280 int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
1281 /**
1282  * @brief Free NAT44 ED session data (lookup keys, external address port)
1283  *
1284  * @param s            NAT session
1285  * @param thread_index thread index
1286  * @param is_ha        is HA event
1287  */
1288 void
1289 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
1290                          u32 thread_index, u8 is_ha);
1291
1292 /**
1293  * @brief Initialize NAT44 data
1294  *
1295  * @param tsm          per thread data
1296  */
1297 void nat44_db_init (snat_main_per_thread_data_t * tsm);
1298
1299 /**
1300  * @brief Free NAT44 data
1301  *
1302  * @param tsm          per thread data
1303  */
1304 void nat44_db_free (snat_main_per_thread_data_t * tsm);
1305
1306 void nat44_sessions_clear ();
1307
1308 /**
1309  * @brief Find or create NAT user
1310  *
1311  * @param sm           snat global configuration data
1312  * @param addr         IPv4 address
1313  * @param fib_index    FIB table index
1314  * @param thread_index thread index
1315  *
1316  * @return NAT user data structure on success otherwise zero value
1317  */
1318 snat_user_t *nat_user_get_or_create (snat_main_t * sm,
1319                                      ip4_address_t * addr, u32 fib_index,
1320                                      u32 thread_index);
1321
1322 /**
1323  * @brief Allocate new NAT session or recycle last used
1324  *
1325  * @param sm           snat global configuration data
1326  * @param u            NAT user
1327  * @param thread_index thread index
1328  * @param now          time now
1329  *
1330  * @return session data structure on success otherwise zero value
1331  */
1332 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
1333                                               snat_user_t * u,
1334                                               u32 thread_index, f64 now);
1335
1336 /**
1337  * @brief Set address and port assignment algorithm for MAP-E CE
1338  *
1339  * @param psid        Port Set Identifier value
1340  * @param psid_offset number of offset bits
1341  * @param psid_length length of PSID
1342  */
1343 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
1344                                        u16 psid_length);
1345
1346 /**
1347  * @brief Set address and port assignment algorithm for port range
1348  *
1349  * @param start_port beginning of the port range
1350  * @param end_port   end of the port range
1351  */
1352 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
1353
1354 /**
1355  * @brief Set address and port assignment algorithm to default/standard
1356  */
1357 void nat_set_alloc_addr_and_port_default (void);
1358
1359 /**
1360  * @brief Free outside address and port pair
1361  *
1362  * @param addresses    vector of outside addresses
1363  * @param thread_index thread index
1364  * @param key          address, port and protocol
1365  */
1366 void
1367 snat_free_outside_address_and_port (snat_address_t * addresses,
1368                                     u32 thread_index,
1369                                     ip4_address_t * addr,
1370                                     u16 port, nat_protocol_t protocol);
1371
1372 /**
1373  * @brief Alloc outside address and port
1374  *
1375  * @param addresses         vector of outside addresses
1376  * @param fib_index         FIB table index
1377  * @param thread_index      thread index
1378  * @param port_per_thread   number of ports per thread
1379  * @param snat_thread_index NAT thread index
1380  *
1381  * @return 0 on success, non-zero value otherwise
1382  */
1383 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1384                                          u32 fib_index,
1385                                          u32 thread_index,
1386                                          nat_protocol_t proto,
1387                                          ip4_address_t * addr,
1388                                          u16 * port,
1389                                          u16 port_per_thread,
1390                                          u32 snat_thread_index);
1391
1392 void expire_per_vrf_sessions (u32 fib_index);
1393
1394 /**
1395  * @brief Match NAT44 static mapping.
1396  *
1397  * @param key           address and port to match
1398  * @param addr          external/local address of the matched mapping
1399  * @param port          port of the matched mapping
1400  * @param fib_index     fib index of the matched mapping
1401  * @param by_external   if 0 match by local address otherwise match by external
1402  *                      address
1403  * @param is_addr_only  1 if matched mapping is address only
1404  * @param twice_nat     matched mapping is twice NAT type
1405  * @param lb            1 if matched mapping is load-balanced
1406  * @param ext_host_addr external host address
1407  *
1408  * @returns 0 if match found otherwise 1.
1409  */
1410 int snat_static_mapping_match (snat_main_t * sm,
1411                                ip4_address_t match_addr,
1412                                u16 match_port,
1413                                u32 match_fib_index,
1414                                nat_protocol_t match_protocol,
1415                                ip4_address_t * mapping_addr,
1416                                u16 * mapping_port,
1417                                u32 * mapping_fib_index,
1418                                u8 by_external,
1419                                u8 * is_addr_only,
1420                                twice_nat_type_t * twice_nat,
1421                                lb_nat_type_t * lb,
1422                                ip4_address_t * ext_host_addr,
1423                                u8 * is_identity_nat);
1424
1425 /**
1426  * @brief Add/del NAT address to FIB.
1427  *
1428  * Add the external NAT address to the FIB as receive entries. This ensures
1429  * that VPP will reply to ARP for this address and we don't need to enable
1430  * proxy ARP on the outside interface.
1431  *
1432  * @param addr        IPv4 address
1433  * @param plen        address prefix length
1434  * @param sw_if_index software index of the outside interface
1435  * @param is_add      0 = delete, 1 = add.
1436  */
1437 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1438                                u8 p_len, u32 sw_if_index, int is_add);
1439
1440 /*
1441  * Why is this here? Because we don't need to touch this layer to
1442  * simply reply to an icmp. We need to change id to a unique
1443  * value to NAT an echo request/reply.
1444  */
1445
1446 typedef struct
1447 {
1448   u16 identifier;
1449   u16 sequence;
1450 } icmp_echo_header_t;
1451
1452 typedef struct
1453 {
1454   u16 src_port, dst_port;
1455 } tcp_udp_header_t;
1456
1457 u8 *format_user_kvp (u8 * s, va_list * args);
1458
1459 #endif /* __included_nat_h__ */
1460 /*
1461  * fd.io coding-style-patch-verification: ON
1462  *
1463  * Local Variables:
1464  * eval: (c-set-style "gnu")
1465  * End:
1466  */