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