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