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