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