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