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