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