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