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