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