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