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