NAT44: fix bug in TCP close with output-feature interface (VPP-1493)
[vpp.git] / src / plugins / nat / nat.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file NAT plugin global declarations
17  */
18 #ifndef __included_nat_h__
19 #define __included_nat_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/ip/icmp46_packet.h>
25 #include <vnet/api_errno.h>
26 #include <vppinfra/bihash_8_8.h>
27 #include <vppinfra/bihash_16_8.h>
28 #include <vppinfra/dlist.h>
29 #include <vppinfra/error.h>
30 #include <vlibapi/api.h>
31 #include <vlib/log.h>
32
33 /* default session timeouts */
34 #define SNAT_UDP_TIMEOUT 300
35 #define SNAT_TCP_TRANSITORY_TIMEOUT 240
36 #define SNAT_TCP_ESTABLISHED_TIMEOUT 7440
37 #define SNAT_ICMP_TIMEOUT 60
38
39 /* number of worker handoff frame queue elements */
40 #define NAT_FQ_NELTS 64
41
42 /* NAT buffer flags */
43 #define SNAT_FLAG_HAIRPINNING (1 << 0)
44
45 /* session key (4-tuple) */
46 typedef struct
47 {
48   union
49   {
50     struct
51     {
52       ip4_address_t addr;
53       u16 port;
54       u16 protocol:3, fib_index:13;
55     };
56     u64 as_u64;
57   };
58 } snat_session_key_t;
59
60 /* endpoint-dependent session key (6-tuple) */
61 typedef struct
62 {
63   union
64   {
65     struct
66     {
67       ip4_address_t l_addr;
68       ip4_address_t r_addr;
69       u32 proto:8, fib_index:24;
70       u16 l_port;
71       u16 r_port;
72     };
73     u64 as_u64[2];
74   };
75 } nat_ed_ses_key_t;
76
77 /* deterministic session outside key */
78 typedef struct
79 {
80   union
81   {
82     struct
83     {
84       ip4_address_t ext_host_addr;
85       u16 ext_host_port;
86       u16 out_port;
87     };
88     u64 as_u64;
89   };
90 } snat_det_out_key_t;
91
92 /* user (internal host) key */
93 typedef struct
94 {
95   union
96   {
97     struct
98     {
99       ip4_address_t addr;
100       u32 fib_index;
101     };
102     u64 as_u64;
103   };
104 } snat_user_key_t;
105
106 typedef struct
107 {
108   u32 sw_if_index;
109   u32 next_index;
110   u8 cached;
111 } nat44_reass_trace_t;
112
113 /* External address and port allocation modes */
114 #define foreach_nat_addr_and_port_alloc_alg \
115   _(0, DEFAULT, "default")         \
116   _(1, MAPE, "map-e")              \
117   _(2, RANGE, "port-range")
118
119 typedef enum
120 {
121 #define _(v, N, s) NAT_ADDR_AND_PORT_ALLOC_ALG_##N = v,
122   foreach_nat_addr_and_port_alloc_alg
123 #undef _
124 } nat_addr_and_port_alloc_alg_t;
125
126
127 /* Supported L4 protocols */
128 #define foreach_snat_protocol \
129   _(UDP, 0, udp, "udp")       \
130   _(TCP, 1, tcp, "tcp")       \
131   _(ICMP, 2, icmp, "icmp")
132
133 typedef enum
134 {
135 #define _(N, i, n, s) SNAT_PROTOCOL_##N = i,
136   foreach_snat_protocol
137 #undef _
138 } snat_protocol_t;
139
140
141 /* Session state */
142 #define foreach_snat_session_state          \
143   _(0, UNKNOWN, "unknown")                 \
144   _(1, UDP_ACTIVE, "udp-active")           \
145   _(2, TCP_SYN_SENT, "tcp-syn-sent")       \
146   _(3, TCP_ESTABLISHED, "tcp-established") \
147   _(4, TCP_FIN_WAIT, "tcp-fin-wait")       \
148   _(5, TCP_CLOSE_WAIT, "tcp-close-wait")   \
149   _(6, TCP_CLOSING, "tcp-closing")         \
150   _(7, TCP_LAST_ACK, "tcp-last-ack")       \
151   _(8, TCP_CLOSED, "tcp-closed")           \
152   _(9, ICMP_ACTIVE, "icmp-active")
153
154 typedef enum
155 {
156 #define _(v, N, s) SNAT_SESSION_##N = v,
157   foreach_snat_session_state
158 #undef _
159 } snat_session_state_t;
160
161 /* Endpoint dependent TCP session state */
162 #define NAT44_SES_I2O_FIN 1
163 #define NAT44_SES_O2I_FIN 2
164 #define NAT44_SES_I2O_FIN_ACK 4
165 #define NAT44_SES_O2I_FIN_ACK 8
166 #define NAT44_SES_I2O_SYN 16
167 #define NAT44_SES_O2I_SYN 32
168
169 /* Session flags */
170 #define SNAT_SESSION_FLAG_STATIC_MAPPING       1
171 #define SNAT_SESSION_FLAG_UNKNOWN_PROTO        2
172 #define SNAT_SESSION_FLAG_LOAD_BALANCING       4
173 #define SNAT_SESSION_FLAG_TWICE_NAT            8
174 #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT   16
175 #define SNAT_SESSION_FLAG_FWD_BYPASS           32
176 #define SNAT_SESSION_FLAG_AFFINITY             64
177 #define SNAT_SESSION_FLAG_OUTPUT_FEATURE       128
178
179 /* NAT interface flags */
180 #define NAT_INTERFACE_FLAG_IS_INSIDE 1
181 #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
182
183 /* Static mapping flags */
184 #define NAT_STATIC_MAPPING_FLAG_ADDR_ONLY    1
185 #define NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY  2
186 #define NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT 4
187 #define NAT_STATIC_MAPPING_FLAG_LB           8
188
189 /* *INDENT-OFF* */
190 typedef CLIB_PACKED(struct
191 {
192   /* Outside network key */
193   snat_session_key_t out2in;
194
195   /* Inside network key */
196   snat_session_key_t in2out;
197
198   /* Flags */
199   u32 flags;
200
201   /* Per-user translations */
202   u32 per_user_index;
203   u32 per_user_list_head_index;
204
205   /* Last heard timer */
206   f64 last_heard;
207
208   /* Counters */
209   u64 total_bytes;
210   u32 total_pkts;
211
212   /* External host address and port */
213   ip4_address_t ext_host_addr;
214   u16 ext_host_port;
215
216   /* External host address and port after translation */
217   ip4_address_t ext_host_nat_addr;
218   u16 ext_host_nat_port;
219
220   /* TCP session state */
221   u8 state;
222   u32 i2o_fin_seq;
223   u32 o2i_fin_seq;
224 }) snat_session_t;
225 /* *INDENT-ON* */
226
227
228 typedef struct
229 {
230   ip4_address_t addr;
231   u32 fib_index;
232   u32 sessions_per_user_list_head_index;
233   u32 nsessions;
234   u32 nstaticsessions;
235 } snat_user_t;
236
237 typedef struct
238 {
239   ip4_address_t addr;
240   u32 fib_index;
241 /* *INDENT-OFF* */
242 #define _(N, i, n, s) \
243   u16 busy_##n##_ports; \
244   u16 * busy_##n##_ports_per_thread; \
245   uword * busy_##n##_port_bitmap;
246   foreach_snat_protocol
247 #undef _
248 /* *INDENT-ON* */
249 } snat_address_t;
250
251 typedef struct
252 {
253   u32 fib_index;
254   u32 refcount;
255 } nat_outside_fib_t;
256
257 typedef struct
258 {
259   /* Inside network port */
260   u16 in_port;
261   /* Outside network address and port */
262   snat_det_out_key_t out;
263   /* Session state */
264   u8 state;
265   /* Expire timeout */
266   u32 expire;
267 } snat_det_session_t;
268
269 typedef struct
270 {
271   /* inside IP address range */
272   ip4_address_t in_addr;
273   u8 in_plen;
274   /* outside IP address range */
275   ip4_address_t out_addr;
276   u8 out_plen;
277   /* inside IP addresses / outside IP addresses */
278   u32 sharing_ratio;
279   /* number of ports available to internal host */
280   u16 ports_per_host;
281   /* session counter */
282   u32 ses_num;
283   /* vector of sessions */
284   snat_det_session_t *sessions;
285 } snat_det_map_t;
286
287 typedef struct
288 {
289   /* backend IP address */
290   ip4_address_t addr;
291   /* backend port number */
292   u16 port;
293   /* probability of the backend to be randomly matched */
294   u8 probability;
295   u8 prefix;
296   /* backend FIB table */
297   u32 vrf_id;
298   u32 fib_index;
299 } nat44_lb_addr_port_t;
300
301 typedef enum
302 {
303   /* twice-nat disabled */
304   TWICE_NAT_DISABLED,
305   /* twice-nat enabled */
306   TWICE_NAT,
307   /* twice-nat only when src IP equals dst IP after translation */
308   TWICE_NAT_SELF,
309 } twice_nat_type_t;
310
311 typedef enum
312 {
313   /* no load-balancing */
314   NO_LB_NAT,
315   /* load-balancing */
316   LB_NAT,
317   /* load-balancing with affinity */
318   AFFINITY_LB_NAT,
319 } lb_nat_type_t;
320
321 typedef struct
322 {
323   /* local IP address */
324   ip4_address_t local_addr;
325   /* external IP address */
326   ip4_address_t external_addr;
327   /* local port */
328   u16 local_port;
329   /* external port */
330   u16 external_port;
331   /* is twice-nat */
332   twice_nat_type_t twice_nat;
333   /* local FIB table */
334   u32 vrf_id;
335   u32 fib_index;
336   /* protocol */
337   snat_protocol_t proto;
338   /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
339   u32 affinity;
340   /* worker threads used by backends/local host */
341   u32 *workers;
342   /* opaque string tag */
343   u8 *tag;
344   /* backends for load-balancing mode */
345   nat44_lb_addr_port_t *locals;
346   /* affinity per service lis */
347   u32 affinity_per_service_list_head_index;
348   /* flags */
349   u32 flags;
350 } snat_static_mapping_t;
351
352 typedef struct
353 {
354   u32 sw_if_index;
355   u8 flags;
356 } snat_interface_t;
357
358 typedef struct
359 {
360   ip4_address_t l_addr;
361   u16 l_port;
362   u16 e_port;
363   u32 sw_if_index;
364   u32 vrf_id;
365   snat_protocol_t proto;
366   u32 flags;
367   int addr_only;
368   int twice_nat;
369   int is_add;
370   int out2in_only;
371   int identity_nat;
372   u8 *tag;
373 } snat_static_map_resolve_t;
374
375 typedef struct
376 {
377   /* Main lookup tables */
378   clib_bihash_8_8_t out2in;
379   clib_bihash_8_8_t in2out;
380
381   /* Endpoint dependent sessions lookup tables */
382   clib_bihash_16_8_t out2in_ed;
383   clib_bihash_16_8_t in2out_ed;
384
385   /* Find-a-user => src address lookup */
386   clib_bihash_8_8_t user_hash;
387
388   /* User pool */
389   snat_user_t *users;
390
391   /* Session pool */
392   snat_session_t *sessions;
393
394   /* Pool of doubly-linked list elements */
395   dlist_elt_t *list_pool;
396
397   /* NAT thread index */
398   u32 snat_thread_index;
399 } snat_main_per_thread_data_t;
400
401 struct snat_main_s;
402
403 /* ICMP session match function */
404 typedef u32 (snat_icmp_match_function_t) (struct snat_main_s * sm,
405                                           vlib_node_runtime_t * node,
406                                           u32 thread_index,
407                                           vlib_buffer_t * b0,
408                                           ip4_header_t * ip0, u8 * p_proto,
409                                           snat_session_key_t * p_value,
410                                           u8 * p_dont_translate, void *d,
411                                           void *e);
412
413 /* Return worker thread index for given packet */
414 typedef u32 (snat_get_worker_function_t) (ip4_header_t * ip,
415                                           u32 rx_fib_index);
416
417 /* NAT address and port allacotaion function */
418 typedef int (nat_alloc_out_addr_and_port_function_t) (snat_address_t *
419                                                       addresses,
420                                                       u32 fib_index,
421                                                       u32 thread_index,
422                                                       snat_session_key_t * k,
423                                                       u16 port_per_thread,
424                                                       u32 snat_thread_index);
425
426 typedef struct snat_main_s
427 {
428   /* ICMP session match functions */
429   snat_icmp_match_function_t *icmp_match_in2out_cb;
430   snat_icmp_match_function_t *icmp_match_out2in_cb;
431
432   /* Thread settings */
433   u32 num_workers;
434   u32 first_worker_index;
435   u32 *workers;
436   snat_get_worker_function_t *worker_in2out_cb;
437   snat_get_worker_function_t *worker_out2in_cb;
438   u16 port_per_thread;
439   u32 num_snat_thread;
440
441   /* Per thread data */
442   snat_main_per_thread_data_t *per_thread_data;
443
444   /* Find a static mapping by local */
445   clib_bihash_8_8_t static_mapping_by_local;
446
447   /* Find a static mapping by external */
448   clib_bihash_8_8_t static_mapping_by_external;
449
450   /* Static mapping pool */
451   snat_static_mapping_t *static_mappings;
452
453   /* Interface pool */
454   snat_interface_t *interfaces;
455   snat_interface_t *output_feature_interfaces;
456
457   /* Vector of outside addresses */
458   snat_address_t *addresses;
459   /* Address and port allocation function */
460   nat_alloc_out_addr_and_port_function_t *alloc_addr_and_port;
461   /* Address and port allocation type */
462   nat_addr_and_port_alloc_alg_t addr_and_port_alloc_alg;
463   /* Port set parameters (MAP-E) */
464   u8 psid_offset;
465   u8 psid_length;
466   u16 psid;
467   /* Port range parameters */
468   u16 start_port;
469   u16 end_port;
470
471   /* vector of outside fibs */
472   nat_outside_fib_t *outside_fibs;
473
474   /* Vector of twice NAT addresses for extenal hosts */
475   snat_address_t *twice_nat_addresses;
476
477   /* sw_if_indices whose intfc addresses should be auto-added */
478   u32 *auto_add_sw_if_indices;
479   u32 *auto_add_sw_if_indices_twice_nat;
480
481   /* vector of interface address static mappings to resolve. */
482   snat_static_map_resolve_t *to_resolve;
483
484   /* Randomize port allocation order */
485   u32 random_seed;
486
487   /* Worker handoff frame-queue index */
488   u32 fq_in2out_index;
489   u32 fq_in2out_output_index;
490   u32 fq_out2in_index;
491
492   /* in2out and out2in node index */
493   u32 in2out_node_index;
494   u32 in2out_output_node_index;
495   u32 out2in_node_index;
496   u32 error_node_index;
497
498   /* Deterministic NAT mappings */
499   snat_det_map_t *det_maps;
500
501   /* If forwarding is enabled */
502   u8 forwarding_enabled;
503
504   /* Config parameters */
505   u8 static_mapping_only;
506   u8 static_mapping_connection_tracking;
507   u8 deterministic;
508   u8 out2in_dpo;
509   u8 endpoint_dependent;
510   u32 translation_buckets;
511   u32 translation_memory_size;
512   u32 max_translations;
513   u32 user_buckets;
514   u32 user_memory_size;
515   u32 max_translations_per_user;
516   u32 outside_vrf_id;
517   u32 outside_fib_index;
518   u32 inside_vrf_id;
519   u32 inside_fib_index;
520
521   /* values of various timeouts */
522   u32 udp_timeout;
523   u32 tcp_established_timeout;
524   u32 tcp_transitory_timeout;
525   u32 icmp_timeout;
526
527   /* TCP MSS clamping */
528   u16 mss_clamping;
529   u16 mss_value_net;
530
531   /* API message ID base */
532   u16 msg_id_base;
533
534   /* log class */
535   vlib_log_class_t log_class;
536
537   /* convenience */
538   vlib_main_t *vlib_main;
539   vnet_main_t *vnet_main;
540   ip4_main_t *ip4_main;
541   ip_lookup_main_t *ip4_lookup_main;
542   api_main_t *api_main;
543 } snat_main_t;
544
545 typedef struct
546 {
547   u32 thread_index;
548   f64 now;
549 } nat44_is_idle_session_ctx_t;
550
551 typedef struct
552 {
553   u32 cached_sw_if_index;
554   u32 cached_ip4_address;
555 } snat_runtime_t;
556
557 extern snat_main_t snat_main;
558 extern vlib_node_registration_t snat_in2out_node;
559 extern vlib_node_registration_t snat_in2out_output_node;
560 extern vlib_node_registration_t snat_out2in_node;
561 extern vlib_node_registration_t snat_in2out_fast_node;
562 extern vlib_node_registration_t snat_out2in_fast_node;
563 extern vlib_node_registration_t snat_in2out_worker_handoff_node;
564 extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
565 extern vlib_node_registration_t snat_out2in_worker_handoff_node;
566 extern vlib_node_registration_t snat_det_in2out_node;
567 extern vlib_node_registration_t snat_det_out2in_node;
568 extern vlib_node_registration_t snat_hairpin_dst_node;
569 extern vlib_node_registration_t snat_hairpin_src_node;
570 extern vlib_node_registration_t nat44_ed_in2out_node;
571 extern vlib_node_registration_t nat44_ed_in2out_output_node;
572 extern vlib_node_registration_t nat44_ed_out2in_node;
573 extern vlib_node_registration_t nat44_ed_hairpin_dst_node;
574 extern vlib_node_registration_t nat44_ed_hairpin_src_node;
575 extern vlib_node_registration_t nat44_ed_in2out_worker_handoff_node;
576 extern vlib_node_registration_t nat44_ed_in2out_output_worker_handoff_node;
577 extern vlib_node_registration_t nat44_ed_out2in_worker_handoff_node;
578
579 /* format functions */
580 format_function_t format_snat_user;
581 format_function_t format_snat_static_mapping;
582 format_function_t format_snat_static_map_to_resolve;
583 format_function_t format_snat_session;
584 format_function_t format_det_map_ses;
585 format_function_t format_snat_key;
586 format_function_t format_static_mapping_key;
587 format_function_t format_snat_protocol;
588 format_function_t format_nat_addr_and_port_alloc_alg;
589 format_function_t format_nat44_reass_trace;
590 /* unformat functions */
591 unformat_function_t unformat_snat_protocol;
592
593 /** \brief Check if SNAT session is created from static mapping.
594     @param s SNAT session
595     @return 1 if SNAT session is created from static mapping otherwise 0
596 */
597 #define snat_is_session_static(s) (s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING)
598
599 /** \brief Check if SNAT session for unknown protocol.
600     @param s SNAT session
601     @return 1 if SNAT session for unknown protocol otherwise 0
602 */
603 #define snat_is_unk_proto_session(s) (s->flags & SNAT_SESSION_FLAG_UNKNOWN_PROTO)
604
605 /** \brief Check if NAT session is twice NAT.
606     @param s NAT session
607     @return 1 if NAT session is twice NAT
608 */
609 #define is_twice_nat_session(s) (s->flags & SNAT_SESSION_FLAG_TWICE_NAT)
610
611 /** \brief Check if NAT session is load-balancing.
612     @param s NAT session
613     @return 1 if NAT session is load-balancing
614 */
615 #define is_lb_session(s) (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
616
617 /** \brief Check if NAT session is forwarding bypass.
618     @param s NAT session
619     @return 1 if NAT session is load-balancing
620 */
621 #define is_fwd_bypass_session(s) (s->flags & SNAT_SESSION_FLAG_FWD_BYPASS)
622
623 /** \brief Check if NAT session is endpoint dependent.
624     @param s NAT session
625     @return 1 if NAT session is endpoint dependent
626 */
627 #define is_ed_session(s) (s->flags & SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT)
628
629 /** \brief Check if NAT session has affinity record.
630     @param s NAT session
631     @return 1 if NAT session has affinity record
632 */
633 #define is_affinity_sessions(s) (s->flags & SNAT_SESSION_FLAG_AFFINITY)
634
635 /** \brief Check if NAT interface is inside.
636     @param i NAT interfce
637     @return 1 if inside interface
638 */
639 #define nat_interface_is_inside(i) i->flags & NAT_INTERFACE_FLAG_IS_INSIDE
640
641 /** \brief Check if NAT interface is outside.
642     @param i NAT interfce
643     @return 1 if outside interface
644 */
645 #define nat_interface_is_outside(i) i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE
646
647 /** \brief Check if NAT44 endpoint-dependent TCP session is closed.
648     @param s NAT session
649     @return 1 if session is closed
650 */
651 #define nat44_is_ses_closed(s) s->state == 0xf
652
653 /** \brief Check if NAT static mapping is address only (1:1NAT).
654     @param sm NAT static mapping
655     @return 1 if 1:1NAT, 0 if 1:1NAPT
656 */
657 #define is_addr_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_ADDR_ONLY)
658
659 /** \brief Check if NAT static mapping match only out2in direction.
660     @param sm NAT static mapping
661     @return 1 if rule match only out2in direction
662 */
663 #define is_out2in_only_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY)
664
665 /** \brief Check if NAT static mapping is identity NAT.
666     @param sm NAT static mapping
667     @return 1 if identity NAT
668 */
669 #define is_identity_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT)
670
671 /** \brief Check if NAT static mapping is load-balancing.
672     @param sm NAT static mapping
673     @return 1 if load-balancing
674 */
675 #define is_lb_static_mapping(sm) (sm->flags & NAT_STATIC_MAPPING_FLAG_LB)
676
677 /** \brief Check if client initiating TCP connection (received SYN from client)
678     @param t TCP header
679     @return 1 if client initiating TCP connection
680 */
681 #define tcp_is_init(t) ((t->flags & TCP_FLAG_SYN) && !(t->flags & TCP_FLAG_ACK))
682
683 /* logging */
684 #define nat_log_err(...) \
685   vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
686 #define nat_log_warn(...) \
687   vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
688 #define nat_log_notice(...) \
689   vlib_log(VLIB_LOG_LEVEL_NOTICE, snat_main.log_class, __VA_ARGS__)
690 #define nat_log_info(...) \
691   vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
692 #define nat_log_debug(...)\
693   vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
694
695 /* ICMP session match functions */
696 u32 icmp_match_in2out_fast (snat_main_t * sm, vlib_node_runtime_t * node,
697                             u32 thread_index, vlib_buffer_t * b0,
698                             ip4_header_t * ip0, u8 * p_proto,
699                             snat_session_key_t * p_value,
700                             u8 * p_dont_translate, void *d, void *e);
701 u32 icmp_match_in2out_slow (snat_main_t * sm, vlib_node_runtime_t * node,
702                             u32 thread_index, vlib_buffer_t * b0,
703                             ip4_header_t * ip0, u8 * p_proto,
704                             snat_session_key_t * p_value,
705                             u8 * p_dont_translate, void *d, void *e);
706 u32 icmp_match_out2in_fast (snat_main_t * sm, vlib_node_runtime_t * node,
707                             u32 thread_index, vlib_buffer_t * b0,
708                             ip4_header_t * ip0, u8 * p_proto,
709                             snat_session_key_t * p_value,
710                             u8 * p_dont_translate, void *d, void *e);
711 u32 icmp_match_out2in_slow (snat_main_t * sm, vlib_node_runtime_t * node,
712                             u32 thread_index, vlib_buffer_t * b0,
713                             ip4_header_t * ip0, u8 * p_proto,
714                             snat_session_key_t * p_value,
715                             u8 * p_dont_translate, void *d, void *e);
716
717 /* ICMP deterministic NAT session match functions */
718 u32 icmp_match_out2in_det (snat_main_t * sm, vlib_node_runtime_t * node,
719                            u32 thread_index, vlib_buffer_t * b0,
720                            ip4_header_t * ip0, u8 * p_proto,
721                            snat_session_key_t * p_value,
722                            u8 * p_dont_translate, void *d, void *e);
723 u32 icmp_match_in2out_det (snat_main_t * sm, vlib_node_runtime_t * node,
724                            u32 thread_index, vlib_buffer_t * b0,
725                            ip4_header_t * ip0, u8 * p_proto,
726                            snat_session_key_t * p_value,
727                            u8 * p_dont_translate, void *d, void *e);
728
729 /* ICMP endpoint-dependent session match functions */
730 u32 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
731                           u32 thread_index, vlib_buffer_t * b0,
732                           ip4_header_t * ip0, u8 * p_proto,
733                           snat_session_key_t * p_value,
734                           u8 * p_dont_translate, void *d, void *e);
735 u32 icmp_match_in2out_ed (snat_main_t * sm, vlib_node_runtime_t * node,
736                           u32 thread_index, vlib_buffer_t * b0,
737                           ip4_header_t * ip0, u8 * p_proto,
738                           snat_session_key_t * p_value,
739                           u8 * p_dont_translate, void *d, void *e);
740
741 u32 icmp_in2out (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
742                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
743                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
744                  void *d, void *e);
745
746 u32 icmp_out2in (snat_main_t * sm, vlib_buffer_t * b0, ip4_header_t * ip0,
747                  icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0,
748                  vlib_node_runtime_t * node, u32 next0, u32 thread_index,
749                  void *d, void *e);
750
751 /* hairpinning functions */
752 u32 snat_icmp_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
753                            ip4_header_t * ip0, icmp46_header_t * icmp0,
754                            int is_ed);
755 void nat_hairpinning_sm_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
756                                        ip4_header_t * ip);
757 void nat44_ed_hairpinning_unknown_proto (snat_main_t * sm, vlib_buffer_t * b,
758                                          ip4_header_t * ip);
759 int snat_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
760                       ip4_header_t * ip0, udp_header_t * udp0,
761                       tcp_header_t * tcp0, u32 proto0, int is_ed);
762 void nat44_reass_hairpinning (snat_main_t * sm, vlib_buffer_t * b0,
763                               ip4_header_t * ip0, u16 sport, u16 dport,
764                               u32 proto0, int is_ed);
765
766 /* Call back functions for clib_bihash_add_or_overwrite_stale */
767 int nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
768 int nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg);
769 int nat44_i2o_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
770 int nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void *arg);
771
772 /**
773  * @brief Increment IPv4 address
774  */
775 void increment_v4_address (ip4_address_t * a);
776
777 /**
778  * @brief Add external address to NAT44 pool
779  *
780  * @param addr      IPv4 address
781  * @param vrf_id    VRF id of tenant, ~0 means independent of VRF
782  * @param twice_nat 1 if twice NAT address
783  *
784  * @return 0 on success, non-zero value otherwise
785  */
786 int snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
787                       u8 twice_nat);
788
789 /**
790  * @brief Delete external address from NAT44 pool
791  *
792  * @param addr      IPv4 address
793  * @param delete_sm 1 if delete static mapping using address
794  * @param twice_nat 1 if twice NAT address
795  *
796  * @return 0 on success, non-zero value otherwise
797  */
798 int snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
799                       u8 twice_nat);
800
801 /**
802  * @brief Add/delete external address to FIB DPO (out2in DPO mode)
803  *
804  * @param addr   IPv4 address
805  * @param is_add 1 = add, 0 = delete
806  *
807  * @return 0 on success, non-zero value otherwise
808  */
809 void nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add);
810
811 /**
812  * @brief Add/delete NAT44 static mapping
813  *
814  * @param l_addr       local IPv4 address
815  * @param e_addr       external IPv4 address
816  * @param l_port       local port number
817  * @param e_port       external port number
818  * @param vrf_id       local VRF ID
819  * @param addr_only    1 = 1:1NAT, 0 = 1:1NAPT
820  * @param sw_if_index  use interface address as external IPv4 address
821  * @param proto        L4 protocol
822  * @param is_add       1 = add, 0 = delete
823  * @param twice_nat    twice-nat mode
824  * @param out2in_only  if 1 rule match only out2in direction
825  * @param tag          opaque string tag
826  * @param identity_nat identity NAT
827  *
828  * @return 0 on success, non-zero value otherwise
829  */
830 int snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
831                              u16 l_port, u16 e_port, u32 vrf_id,
832                              int addr_only, u32 sw_if_index,
833                              snat_protocol_t proto, int is_add,
834                              twice_nat_type_t twice_nat, u8 out2in_only,
835                              u8 * tag, u8 identity_nat);
836
837 /**
838  * @brief Add/delete static mapping with load-balancing (multiple backends)
839  *
840  * @param e_addr      external IPv4 address
841  * @param e_port      external port number
842  * @param proto       L4 protocol
843  * @param locals      list of local backends
844  * @param is_add      1 = add, 0 = delete
845  * @param twice_nat   twice-nat mode
846  * @param out2in_only if 1 rule match only out2in direction
847  * @param tag         opaque string tag
848  * @param affinity    0 = disabled, otherwise client IP affinity sticky time
849  *
850  * @return 0 on success, non-zero value otherwise
851  */
852 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
853                                      snat_protocol_t proto,
854                                      nat44_lb_addr_port_t * locals, u8 is_add,
855                                      twice_nat_type_t twice_nat,
856                                      u8 out2in_only, u8 * tag, u32 affinity);
857
858 clib_error_t *snat_api_init (vlib_main_t * vm, snat_main_t * sm);
859
860 /**
861  * @brief Set NAT plugin workers
862  *
863  * @param bitmap NAT workers bitmap
864  *
865  * @return 0 on success, non-zero value otherwise
866  */
867 int snat_set_workers (uword * bitmap);
868
869 /**
870  * @brief Enable/disable NAT44 feature on the interface
871  *
872  * @param sw_if_index software index of the interface
873  * @param is_inside   1 = inside, 0 = outside
874  * @param is_del      1 = delete, 0 = add
875  *
876  * @return 0 on success, non-zero value otherwise
877  */
878 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del);
879
880 /**
881  * @brief Enable/disable NAT44 output feature on the interface (postrouting NAT)
882  *
883  * @param sw_if_index software index of the interface
884  * @param is_inside   1 = inside, 0 = outside
885  * @param is_del      1 = delete, 0 = add
886  *
887  * @return 0 on success, non-zero value otherwise
888  */
889 int snat_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
890                                            int is_del);
891
892 /**
893  * @brief Add/delete NAT44 pool address from specific interfce
894  *
895  * @param sw_if_index software index of the interface
896  * @param is_del      1 = delete, 0 = add
897  * @param twice_nat   1 = twice NAT address for extenal hosts
898  *
899  * @return 0 on success, non-zero value otherwise
900  */
901 int snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
902                                 u8 twice_nat);
903
904 /**
905  * @brief Delete NAT44 session
906  *
907  * @param addr   IPv4 address
908  * @param port   L4 port number
909  * @param proto  L4 protocol
910  * @param vrf_id VRF ID
911  * @param is_in  1 = inside network addres and por pair, 0 = outside
912  *
913  * @return 0 on success, non-zero value otherwise
914  */
915 int nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
916                        snat_protocol_t proto, u32 vrf_id, int is_in);
917
918 /**
919  * @brief Delete NAT44 endpoint-dependent session
920  *
921  * @param addr   IPv4 address
922  * @param port   L4 port number
923  * @param proto  L4 protocol
924  * @param vrf_id VRF ID
925  * @param is_in  1 = inside network addres and por pair, 0 = outside
926  *
927  * @return 0 on success, non-zero value otherwise
928  */
929 int nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
930                           ip4_address_t * eh_addr, u16 eh_port, u8 proto,
931                           u32 vrf_id, int is_in);
932
933 /**
934  * @brief Free NAT44 session data (lookup keys, external addrres port)
935  *
936  * @param s            NAT session
937  * @param thread_index thread index
938  */
939 void nat_free_session_data (snat_main_t * sm, snat_session_t * s,
940                             u32 thread_index);
941
942 /**
943  * @brief Find or create NAT user
944  *
945  * @param addr         IPv4 address
946  * @param fib_index    FIB table index
947  * @param thread_index thread index
948  *
949  * @return NAT user data structure on success otherwise zero value
950  */
951 snat_user_t *nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr,
952                                      u32 fib_index, u32 thread_index);
953
954 /**
955  * @brief Allocate new NAT session or recycle last used
956  *
957  * @param u            NAT user
958  * @param thread_index thread index
959  *
960  * @return session data structure on success otherwise zero value
961  */
962 snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
963                                               snat_user_t * u,
964                                               u32 thread_index);
965
966 /**
967  * @brief Allocate NAT endpoint-dependent session
968  *
969  * @param u            NAT user
970  * @param thread_index thread index
971  *
972  * @return session data structure on success otherwise zero value
973  */
974 snat_session_t *nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u,
975                                       u32 thread_index, f64 now);
976
977 /**
978  * @brief Set address and port assignment algorithm for MAP-E CE
979  *
980  * @param psid        Port Set Identifier value
981  * @param psid_offset number of offset bits
982  * @param psid_length length of PSID
983  */
984 void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
985                                        u16 psid_length);
986
987 /**
988  * @brief Set address and port assignment algorithm for port range
989  *
990  * @param start_port beginning of the port range
991  * @param end_port   end of the port range
992  */
993 void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
994
995 /**
996  * @brief Set address and port assignment algorithm to default/standard
997  */
998 void nat_set_alloc_addr_and_port_default (void);
999
1000 /**
1001  * @brief Free outside address and port pair
1002  *
1003  * @param addresses    vector of outside addresses
1004  * @param thread_index thread index
1005  * @param k            adddress, port and protocol
1006  */
1007 void snat_free_outside_address_and_port (snat_address_t * addresses,
1008                                          u32 thread_index,
1009                                          snat_session_key_t * k);
1010
1011 /**
1012  * @brief Alloc outside address and port
1013  *
1014  * @param addresses         vector of outside addresses
1015  * @param fib_index         FIB table index
1016  * @param thread_index      thread index
1017  * @param k                 allocated address and port pair
1018  * @param port_per_thread   number of ports per threead
1019  * @param snat_thread_index NAT thread index
1020  *
1021  * @return 0 on success, non-zero value otherwise
1022  */
1023 int snat_alloc_outside_address_and_port (snat_address_t * addresses,
1024                                          u32 fib_index,
1025                                          u32 thread_index,
1026                                          snat_session_key_t * k,
1027                                          u16 port_per_thread,
1028                                          u32 snat_thread_index);
1029
1030 /**
1031  * @brief Match NAT44 static mapping.
1032  *
1033  * @param match         address and port to match
1034  * @param mapping       external/local address and port of the matched mapping
1035  * @param by_external   if 0 match by local address otherwise match by external
1036  *                      address
1037  * @param is_addr_only  1 if matched mapping is address only
1038  * @param twice_nat     matched mapping is twice NAT type
1039  * @param lb            1 if matched mapping is load-balanced
1040  * @param ext_host_addr external host address
1041  *
1042  * @returns 0 if match found otherwise 1.
1043  */
1044 int snat_static_mapping_match (snat_main_t * sm,
1045                                snat_session_key_t match,
1046                                snat_session_key_t * mapping,
1047                                u8 by_external,
1048                                u8 * is_addr_only,
1049                                twice_nat_type_t * twice_nat,
1050                                lb_nat_type_t * lb,
1051                                ip4_address_t * ext_host_addr,
1052                                u8 * is_identity_nat);
1053
1054 /**
1055  * @brief Add/del NAT address to FIB.
1056  *
1057  * Add the external NAT address to the FIB as receive entries. This ensures
1058  * that VPP will reply to ARP for this address and we don't need to enable
1059  * proxy ARP on the outside interface.
1060  *
1061  * @param addr        IPv4 address
1062  * @param plen        address prefix length
1063  * @param sw_if_index software index of the outside interface
1064  * @param is_add      0 = delete, 1 = add.
1065  */
1066 void snat_add_del_addr_to_fib (ip4_address_t * addr,
1067                                u8 p_len, u32 sw_if_index, int is_add);
1068
1069
1070 /*
1071  * Why is this here? Because we don't need to touch this layer to
1072  * simply reply to an icmp. We need to change id to a unique
1073  * value to NAT an echo request/reply.
1074  */
1075
1076 typedef struct
1077 {
1078   u16 identifier;
1079   u16 sequence;
1080 } icmp_echo_header_t;
1081
1082 typedef struct
1083 {
1084   u16 src_port, dst_port;
1085 } tcp_udp_header_t;
1086
1087 #endif /* __included_nat_h__ */
1088
1089 /*
1090  * fd.io coding-style-patch-verification: ON
1091  *
1092  * Local Variables:
1093  * eval: (c-set-style "gnu")
1094  * End:
1095  */