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