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