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