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