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