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