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