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