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