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