Line data Source code
1 : /*
2 : * Copyright (c) 2020 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 : #ifndef __included_nat44_ed_h__
17 : #define __included_nat44_ed_h__
18 :
19 : #include <vnet/vnet.h>
20 : #include <vnet/ip/ip.h>
21 : #include <vnet/ethernet/ethernet.h>
22 : #include <vnet/ip/icmp46_packet.h>
23 : #include <vnet/api_errno.h>
24 : #include <vnet/fib/fib_source.h>
25 : #include <vppinfra/elog.h>
26 : #include <vppinfra/bihash_8_8.h>
27 : #include <vppinfra/bihash_16_8.h>
28 : #include <vppinfra/hash.h>
29 : #include <vppinfra/dlist.h>
30 : #include <vppinfra/error.h>
31 : #include <vlibapi/api.h>
32 :
33 : #include <nat/lib/lib.h>
34 : #include <nat/lib/inlines.h>
35 :
36 : /* default number of worker handoff frame queue elements */
37 : #define NAT_FQ_NELTS_DEFAULT 64
38 :
39 : /* number of attempts to get a port for ED overloading algorithm, if rolling
40 : * a dice this many times doesn't produce a free port, it's treated
41 : * as if there were no free ports available to conserve resources */
42 : #define ED_PORT_ALLOC_ATTEMPTS (10)
43 :
44 : /* system ports range is 0-1023, first user port is 1024 per
45 : * https://www.rfc-editor.org/rfc/rfc6335#section-6
46 : */
47 : #define ED_USER_PORT_OFFSET 1024
48 :
49 : /* NAT buffer flags */
50 : #define SNAT_FLAG_HAIRPINNING (1 << 0)
51 :
52 : /* NAT44 API Configuration flags */
53 : #define foreach_nat44_config_flag \
54 : _ (0x00, IS_ENDPOINT_INDEPENDENT) \
55 : _ (0x01, IS_ENDPOINT_DEPENDENT) \
56 : _ (0x02, IS_STATIC_MAPPING_ONLY) \
57 : _ (0x04, IS_CONNECTION_TRACKING)
58 :
59 : typedef enum nat44_config_flags_t_
60 : {
61 : #define _(n,f) NAT44_API_##f = n,
62 : foreach_nat44_config_flag
63 : #undef _
64 : } nat44_config_flags_t;
65 :
66 : typedef struct
67 : {
68 : u32 inside_vrf;
69 : u32 outside_vrf;
70 : u32 sessions;
71 : } nat44_config_t;
72 :
73 : typedef enum
74 : {
75 : NAT_NEXT_DROP,
76 : NAT_NEXT_ICMP_ERROR,
77 : NAT_NEXT_IN2OUT_ED_FAST_PATH,
78 : NAT_NEXT_IN2OUT_ED_SLOW_PATH,
79 : NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH,
80 : NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH,
81 : NAT_NEXT_OUT2IN_ED_FAST_PATH,
82 : NAT_NEXT_OUT2IN_ED_SLOW_PATH,
83 : NAT_NEXT_IN2OUT_CLASSIFY,
84 : NAT_NEXT_OUT2IN_CLASSIFY,
85 : NAT_N_NEXT,
86 : } nat_next_t;
87 :
88 : typedef struct
89 : {
90 : u32 next_index;
91 : u32 arc_next_index;
92 : } nat_pre_trace_t;
93 :
94 : #define foreach_nat_in2out_ed_error \
95 : _ (UNSUPPORTED_PROTOCOL, "unsupported protocol") \
96 : _ (OUT_OF_PORTS, "out of ports") \
97 : _ (BAD_ICMP_TYPE, "unsupported ICMP type") \
98 : _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
99 : _ (NON_SYN, "non-SYN packet try to create session") \
100 : _ (TRNSL_FAILED, "couldn't translate packet")
101 :
102 : typedef enum
103 : {
104 : #define _(sym,str) NAT_IN2OUT_ED_ERROR_##sym,
105 : foreach_nat_in2out_ed_error
106 : #undef _
107 : NAT_IN2OUT_ED_N_ERROR,
108 : } nat_in2out_ed_error_t;
109 :
110 : #define foreach_nat_out2in_ed_error \
111 : _ (UNSUPPORTED_PROTOCOL, "unsupported protocol") \
112 : _ (OUT_OF_PORTS, "out of ports") \
113 : _ (BAD_ICMP_TYPE, "unsupported ICMP type") \
114 : _ (NO_TRANSLATION, "no translation") \
115 : _ (MAX_SESSIONS_EXCEEDED, "maximum sessions exceeded") \
116 : _ (NON_SYN, "non-SYN packet try to create session") \
117 : _ (TCP_CLOSED, "drops due to TCP in transitory timeout") \
118 : _ (HASH_ADD_FAILED, "hash table add failed") \
119 : _ (TRNSL_FAILED, "couldn't translate packet")
120 :
121 : typedef enum
122 : {
123 : #define _(sym,str) NAT_OUT2IN_ED_ERROR_##sym,
124 : foreach_nat_out2in_ed_error
125 : #undef _
126 : NAT_OUT2IN_ED_N_ERROR,
127 : } nat_out2in_ed_error_t;
128 :
129 : typedef enum
130 : {
131 : NAT44_ED_TCP_FLAG_FIN = 0,
132 : NAT44_ED_TCP_FLAG_SYN,
133 : NAT44_ED_TCP_FLAG_RST,
134 : NAT44_ED_TCP_FLAG_ACK,
135 : NAT44_ED_TCP_N_FLAG,
136 : } nat44_ed_tcp_flag_e;
137 :
138 : typedef enum
139 : {
140 : NAT44_ED_DIR_I2O = 0,
141 : NAT44_ED_DIR_O2I,
142 : NAT44_ED_N_DIR,
143 : } nat44_ed_dir_e;
144 :
145 : /* Endpoint dependent TCP session state */
146 : typedef enum
147 : {
148 : NAT44_ED_TCP_STATE_CLOSED = 0,
149 : NAT44_ED_TCP_STATE_ESTABLISHED,
150 : NAT44_ED_TCP_STATE_CLOSING,
151 : NAT44_ED_TCP_N_STATE,
152 : } nat44_ed_tcp_state_e;
153 :
154 : format_function_t format_ed_session_kvp;
155 : format_function_t format_snat_session;
156 : format_function_t format_snat_static_mapping;
157 : format_function_t format_snat_static_map_to_resolve;
158 : format_function_t format_nat_ed_translation_error;
159 : format_function_t format_nat_6t_flow;
160 : format_function_t format_nat_6t;
161 : format_function_t format_nat44_ed_tcp_state;
162 :
163 : /* Session flags */
164 : #define SNAT_SESSION_FLAG_STATIC_MAPPING (1 << 0)
165 : #define SNAT_SESSION_FLAG_LOAD_BALANCING (1 << 2)
166 : #define SNAT_SESSION_FLAG_TWICE_NAT (1 << 3)
167 : #define SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT (1 << 4)
168 : #define SNAT_SESSION_FLAG_FWD_BYPASS (1 << 5)
169 : #define SNAT_SESSION_FLAG_AFFINITY (1 << 6)
170 : #define SNAT_SESSION_FLAG_EXACT_ADDRESS (1 << 7)
171 : #define SNAT_SESSION_FLAG_HAIRPINNING (1 << 8)
172 :
173 : /* NAT interface flags */
174 : #define NAT_INTERFACE_FLAG_IS_INSIDE 1
175 : #define NAT_INTERFACE_FLAG_IS_OUTSIDE 2
176 :
177 : /* Static mapping flags */
178 : #define NAT_SM_FLAG_SELF_TWICE_NAT (1 << 1)
179 : #define NAT_SM_FLAG_TWICE_NAT (1 << 2)
180 : #define NAT_SM_FLAG_IDENTITY_NAT (1 << 3)
181 : #define NAT_SM_FLAG_ADDR_ONLY (1 << 4)
182 : #define NAT_SM_FLAG_EXACT_ADDRESS (1 << 5)
183 : #define NAT_SM_FLAG_OUT2IN_ONLY (1 << 6)
184 : #define NAT_SM_FLAG_LB (1 << 7)
185 : #define NAT_SM_FLAG_SWITCH_ADDRESS (1 << 8)
186 :
187 : typedef CLIB_PACKED(struct
188 : {
189 : // number of sessions in this vrf
190 : u32 ses_count;
191 :
192 : u32 rx_fib_index;
193 : u32 tx_fib_index;
194 :
195 : // is this vrf expired
196 : u8 expired;
197 : }) per_vrf_sessions_t;
198 :
199 : typedef union
200 : {
201 : struct
202 : {
203 : ip4_address_t saddr, daddr;
204 : u16 sport; // ICMP id for ICMP case
205 : u16 dport;
206 : u32 fib_index : 24;
207 : u8 proto;
208 : };
209 : u64 as_u64[2];
210 : u64x2u as_u128;
211 : } nat_6t_t;
212 :
213 : STATIC_ASSERT_SIZEOF (nat_6t_t, 2 * sizeof (u64));
214 :
215 : typedef struct
216 : {
217 : #define NAT_FLOW_OP_SADDR_REWRITE (1 << 1)
218 : #define NAT_FLOW_OP_SPORT_REWRITE (1 << 2)
219 : #define NAT_FLOW_OP_DADDR_REWRITE (1 << 3)
220 : #define NAT_FLOW_OP_DPORT_REWRITE (1 << 4)
221 : #define NAT_FLOW_OP_ICMP_ID_REWRITE (1 << 5)
222 : #define NAT_FLOW_OP_TXFIB_REWRITE (1 << 6)
223 : int ops;
224 : nat_6t_t match;
225 : struct
226 : {
227 : ip4_address_t saddr, daddr;
228 : u16 sport;
229 : u16 dport;
230 : u32 fib_index;
231 : u8 proto;
232 : u16 icmp_id;
233 : } rewrite;
234 : uword l3_csum_delta;
235 : uword l4_csum_delta;
236 : } nat_6t_flow_t;
237 :
238 : void nat44_ed_forwarding_enable_disable (u8 is_enable);
239 :
240 : always_inline void
241 28529 : nat_6t_flow_saddr_rewrite_set (nat_6t_flow_t *f, u32 saddr)
242 : {
243 28529 : f->ops |= NAT_FLOW_OP_SADDR_REWRITE;
244 28529 : f->rewrite.saddr.as_u32 = saddr;
245 28529 : }
246 :
247 : always_inline void
248 56881 : nat_6t_flow_daddr_rewrite_set (nat_6t_flow_t *f, u32 daddr)
249 : {
250 56881 : f->ops |= NAT_FLOW_OP_DADDR_REWRITE;
251 56881 : f->rewrite.daddr.as_u32 = daddr;
252 56881 : }
253 :
254 : always_inline void
255 21952 : nat_6t_flow_sport_rewrite_set (nat_6t_flow_t *f, u32 sport)
256 : {
257 21952 : f->ops |= NAT_FLOW_OP_SPORT_REWRITE;
258 21952 : f->rewrite.sport = sport;
259 21952 : }
260 :
261 : always_inline void
262 43743 : nat_6t_flow_dport_rewrite_set (nat_6t_flow_t *f, u32 dport)
263 : {
264 43743 : f->ops |= NAT_FLOW_OP_DPORT_REWRITE;
265 43743 : f->rewrite.dport = dport;
266 43743 : }
267 :
268 : always_inline void
269 56906 : nat_6t_flow_txfib_rewrite_set (nat_6t_flow_t *f, u32 tx_fib_index)
270 : {
271 56906 : f->ops |= NAT_FLOW_OP_TXFIB_REWRITE;
272 56906 : f->rewrite.fib_index = tx_fib_index;
273 56906 : }
274 :
275 : always_inline void
276 13149 : nat_6t_flow_icmp_id_rewrite_set (nat_6t_flow_t *f, u16 id)
277 : {
278 13149 : f->ops |= NAT_FLOW_OP_ICMP_ID_REWRITE;
279 13149 : f->rewrite.icmp_id = id;
280 13149 : }
281 :
282 : typedef CLIB_PACKED(struct
283 : {
284 : /* Outside network tuple */
285 : struct
286 : {
287 : ip4_address_t addr;
288 : u32 fib_index;
289 : u16 port;
290 : } out2in;
291 :
292 : /* Inside network tuple */
293 : struct
294 : {
295 : ip4_address_t addr;
296 : u32 fib_index;
297 : u16 port;
298 : } in2out;
299 :
300 : ip_protocol_t proto;
301 :
302 : nat_6t_flow_t i2o;
303 : nat_6t_flow_t o2i;
304 :
305 : /* Flags */
306 : u32 flags;
307 :
308 : /* head of LRU list in which this session is tracked */
309 : u32 lru_head_index;
310 : /* index in global LRU list */
311 : u32 lru_index;
312 : f64 last_lru_update;
313 :
314 : /* Last heard timer */
315 : f64 last_heard;
316 :
317 : /* Last HA refresh */
318 : f64 ha_last_refreshed;
319 :
320 : /* Counters */
321 : u64 total_bytes;
322 : u32 total_pkts;
323 :
324 : /* External host address and port */
325 : ip4_address_t ext_host_addr;
326 : u16 ext_host_port;
327 :
328 : /* External host address and port after translation */
329 : ip4_address_t ext_host_nat_addr;
330 : u16 ext_host_nat_port;
331 :
332 : /* TCP session state */
333 : u8 tcp_flags[NAT44_ED_N_DIR];
334 : nat44_ed_tcp_state_e tcp_state;
335 :
336 : /* per vrf sessions index */
337 : u32 per_vrf_sessions_index;
338 :
339 : u32 thread_index;
340 : }) snat_session_t;
341 :
342 : typedef struct
343 : {
344 : ip4_address_t addr;
345 : ip4_address_t net;
346 : u32 sw_if_index;
347 : u32 fib_index;
348 : u32 addr_len;
349 : } snat_address_t;
350 :
351 : typedef struct
352 : {
353 : /* backend IP address */
354 : ip4_address_t addr;
355 : /* backend port number */
356 : u16 port;
357 : /* probability of the backend to be randomly matched */
358 : u8 probability;
359 : u8 prefix;
360 : /* backend FIB table */
361 : u32 vrf_id;
362 : u32 fib_index;
363 : } nat44_lb_addr_port_t;
364 :
365 : typedef enum
366 : {
367 : /* twice-nat disabled */
368 : TWICE_NAT_DISABLED,
369 : /* twice-nat enabled */
370 : TWICE_NAT,
371 : /* twice-nat only when src IP equals dst IP after translation */
372 : TWICE_NAT_SELF,
373 : } twice_nat_type_t;
374 :
375 : typedef enum
376 : {
377 : /* no load-balancing */
378 : NO_LB_NAT,
379 : /* load-balancing */
380 : LB_NAT,
381 : /* load-balancing with affinity */
382 : AFFINITY_LB_NAT,
383 : } lb_nat_type_t;
384 :
385 : typedef struct
386 : {
387 : /* preferred pool address */
388 : ip4_address_t pool_addr;
389 : /* local IP address */
390 : ip4_address_t local_addr;
391 : /* external IP address */
392 : ip4_address_t external_addr;
393 : /* local port */
394 : u16 local_port;
395 : /* external port */
396 : u16 external_port;
397 : /* local FIB table */
398 : u32 vrf_id;
399 : u32 fib_index;
400 : /* protocol */
401 : ip_protocol_t proto;
402 : /* 0 = disabled, otherwise client IP affinity sticky time in seconds */
403 : u32 affinity;
404 : /* worker threads used by backends/local host */
405 : u32 *workers;
406 : /* opaque string tag */
407 : u8 *tag;
408 : /* backends for load-balancing mode */
409 : nat44_lb_addr_port_t *locals;
410 : /* affinity per service lis */
411 : u32 affinity_per_service_list_head_index;
412 : /* flags */
413 : u32 flags;
414 : } snat_static_mapping_t;
415 :
416 : typedef struct
417 : {
418 : u32 sw_if_index;
419 : u8 flags;
420 : } snat_interface_t;
421 :
422 : typedef struct
423 : {
424 : u8 is_resolved;
425 : ip4_address_t l_addr;
426 : ip4_address_t pool_addr;
427 : u16 l_port;
428 : u16 e_port;
429 : u32 sw_if_index;
430 : u32 vrf_id;
431 : ip_protocol_t proto;
432 : u32 flags;
433 : u8 *tag;
434 : } snat_static_mapping_resolve_t;
435 :
436 : typedef struct
437 : {
438 : u8 is_resolved;
439 : u8 is_twice_nat;
440 : u32 sw_if_index;
441 : } snat_address_resolve_t;
442 :
443 : typedef struct
444 : {
445 : u32 count;
446 : u32 sw_if_index;
447 : ip4_address_t addr;
448 : } snat_fib_entry_reg_t;
449 :
450 : typedef struct
451 : {
452 : /* Session pool */
453 : snat_session_t *sessions;
454 :
455 : /* Pool of doubly-linked list elements */
456 : dlist_elt_t *list_pool;
457 :
458 : /* LRU session list - head is stale, tail is fresh */
459 : dlist_elt_t *lru_pool;
460 : u32 tcp_trans_lru_head_index;
461 : u32 tcp_estab_lru_head_index;
462 : u32 udp_lru_head_index;
463 : u32 icmp_lru_head_index;
464 : u32 unk_proto_lru_head_index;
465 :
466 : /* NAT thread index */
467 : u32 snat_thread_index;
468 :
469 : /* real thread index */
470 : u32 thread_index;
471 :
472 : per_vrf_sessions_t *per_vrf_sessions_pool;
473 :
474 : } snat_main_per_thread_data_t;
475 :
476 : struct snat_main_s;
477 :
478 : u32 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
479 : u32 rx_fib_index, u8 is_output);
480 : u32 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
481 : u32 rx_fib_index, u8 is_output);
482 :
483 : typedef struct nat_fib_s
484 : {
485 : u32 fib_index;
486 : u32 ref_count;
487 : } nat_fib_t;
488 :
489 : typedef struct vrf_route_s
490 : {
491 : u32 vrf_id;
492 : u32 fib_index;
493 : } vrf_route_t;
494 :
495 : typedef struct vrf_table_s
496 : {
497 : u32 table_vrf_id;
498 : u32 table_fib_index;
499 : vrf_route_t *routes;
500 : } vrf_table_t;
501 :
502 : typedef struct snat_main_s
503 : {
504 : /* Thread settings */
505 : u32 num_workers;
506 : u32 first_worker_index;
507 : u32 *workers;
508 : u16 port_per_thread;
509 :
510 : /* Per thread data */
511 : snat_main_per_thread_data_t *per_thread_data;
512 :
513 : /* Static mapping pool */
514 : snat_static_mapping_t *static_mappings;
515 :
516 : /* Endpoint independent lookup tables */
517 : clib_bihash_8_8_t in2out;
518 : clib_bihash_8_8_t out2in;
519 :
520 : /* Endpoint dependent lookup table */
521 : clib_bihash_16_8_t flow_hash;
522 :
523 : // vector of fibs
524 : nat_fib_t *fibs;
525 :
526 : u32 inside_vrf_id;
527 : u32 inside_fib_index;
528 :
529 : u32 outside_vrf_id;
530 : u32 outside_fib_index;
531 :
532 : // vector of outside fibs
533 : nat_fib_t *outside_fibs;
534 :
535 : // VRF routing table for dynamic sessions
536 : vrf_table_t *vrf_tables;
537 :
538 : /* Interface pool */
539 : snat_interface_t *interfaces;
540 : snat_interface_t *output_feature_interfaces;
541 : // broken api backward compatibility
542 : snat_interface_t *output_feature_dummy_interfaces;
543 :
544 : /* Vector of outside addresses */
545 : snat_address_t *addresses;
546 : /* Vector of twice NAT addresses for external hosts */
547 : snat_address_t *twice_nat_addresses;
548 :
549 : /* first interface address should be auto-added */
550 : snat_address_resolve_t *addr_to_resolve;
551 :
552 : /* vector of fib entries */
553 : snat_fib_entry_reg_t *fib_entry_reg;
554 :
555 : /* vector of interface address static mappings to resolve. */
556 : snat_static_mapping_resolve_t *sm_to_resolve;
557 :
558 : /* Randomize port allocation order */
559 : u32 random_seed;
560 :
561 : /* Worker handoff frame-queue index */
562 : u32 fq_in2out_index;
563 : u32 fq_in2out_output_index;
564 : u32 fq_out2in_index;
565 :
566 : nat44_config_t rconfig;
567 :
568 : /* If forwarding is enabled */
569 : u8 forwarding_enabled;
570 :
571 : /* Is translation memory size calculated or user defined */
572 : u8 translation_memory_size_set;
573 :
574 : u32 translation_buckets;
575 : u32 max_translations_per_thread;
576 : u32 *max_translations_per_fib;
577 :
578 : nat_timeouts_t timeouts;
579 :
580 : /* TCP MSS clamping */
581 : u16 mss_clamping;
582 :
583 : /* counters */
584 : vlib_simple_counter_main_t total_sessions;
585 : u32 max_cfg_sessions_gauge; /* Index of max configured sessions gauge in
586 : stats */
587 :
588 : #define _(x) vlib_simple_counter_main_t x;
589 : struct
590 : {
591 : struct
592 : {
593 : struct
594 : {
595 : foreach_nat_counter;
596 : } in2out;
597 :
598 : struct
599 : {
600 : foreach_nat_counter;
601 : } out2in;
602 : } fastpath;
603 :
604 : struct
605 : {
606 : struct
607 : {
608 : foreach_nat_counter;
609 : } in2out;
610 :
611 : struct
612 : {
613 : foreach_nat_counter;
614 : } out2in;
615 : } slowpath;
616 :
617 : vlib_simple_counter_main_t hairpinning;
618 : } counters;
619 : #undef _
620 :
621 : /* API message ID base */
622 : u16 msg_id_base;
623 :
624 : /* log class */
625 : vlib_log_class_t log_class;
626 : /* logging level */
627 : u8 log_level;
628 :
629 : /* convenience */
630 : ip4_main_t *ip4_main;
631 :
632 : fib_source_t fib_src_hi;
633 : fib_source_t fib_src_low;
634 :
635 : /* number of worker handoff frame queue elements */
636 : u32 frame_queue_nelts;
637 :
638 : /* nat44 plugin enabled */
639 : u8 enabled;
640 :
641 : /* TCP session state machine table:
642 : * first dimension is possible states
643 : * second dimension is direction (in2out/out2in)
644 : * third dimension is TCP flag (SYN, RST, FIN)
645 : *
646 : * value is next state to change to
647 : */
648 : nat44_ed_tcp_state_e tcp_state_change_table[NAT44_ED_TCP_N_STATE]
649 : [NAT44_ED_N_DIR]
650 : [NAT44_ED_TCP_N_FLAG];
651 : } snat_main_t;
652 :
653 : typedef struct
654 : {
655 : u32 thread_index;
656 : f64 now;
657 : } nat44_is_idle_session_ctx_t;
658 :
659 : typedef struct
660 : {
661 : u32 cached_sw_if_index;
662 : uword *cached_presence_by_ip4_address;
663 : } snat_runtime_t;
664 :
665 : /*
666 : * Why is this here? Because we don't need to touch this layer to
667 : * simply reply to an icmp. We need to change id to a unique
668 : * value to NAT an echo request/reply.
669 : */
670 :
671 : extern snat_main_t snat_main;
672 :
673 : extern vlib_node_registration_t nat_default_node;
674 : extern vlib_node_registration_t nat_pre_in2out_node;
675 : extern vlib_node_registration_t nat_pre_out2in_node;
676 :
677 : extern vlib_node_registration_t nat44_ed_in2out_node;
678 : extern vlib_node_registration_t nat44_ed_in2out_output_node;
679 : extern vlib_node_registration_t nat44_ed_out2in_node;
680 :
681 : extern vlib_node_registration_t snat_in2out_worker_handoff_node;
682 : extern vlib_node_registration_t snat_in2out_output_worker_handoff_node;
683 : extern vlib_node_registration_t snat_out2in_worker_handoff_node;
684 :
685 : /** \brief Check if SNAT session is created from static mapping.
686 : @param s SNAT session
687 : @return true if SNAT session is created from static mapping otherwise 0
688 : */
689 : always_inline bool
690 13409 : nat44_ed_is_session_static (snat_session_t *s)
691 : {
692 13409 : return s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING;
693 : }
694 :
695 : /** \brief Check if NAT session is twice NAT.
696 : @param s NAT session
697 : @return true if NAT session is twice NAT
698 : */
699 : always_inline bool
700 55302 : nat44_ed_is_twice_nat_session (snat_session_t *s)
701 : {
702 55302 : return s->flags & SNAT_SESSION_FLAG_TWICE_NAT;
703 : }
704 :
705 : /** \brief Check if NAT session is load-balancing.
706 : @param s NAT session
707 : @return true if NAT session is load-balancing
708 : */
709 : always_inline bool
710 13382 : nat44_ed_is_lb_session (snat_session_t *s)
711 : {
712 13382 : return s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING;
713 : }
714 :
715 : /** \brief Check if NAT session is forwarding bypass.
716 : @param s NAT session
717 : @return true if NAT session is load-balancing
718 : */
719 : always_inline bool
720 41672 : na44_ed_is_fwd_bypass_session (snat_session_t *s)
721 : {
722 41672 : return s->flags & SNAT_SESSION_FLAG_FWD_BYPASS;
723 : }
724 :
725 : /** \brief Check if NAT session has affinity record.
726 : @param s NAT session
727 : @return true if NAT session has affinity record
728 : */
729 : always_inline bool
730 28299 : nat44_ed_is_affinity_session (snat_session_t *s)
731 : {
732 28299 : return s->flags & SNAT_SESSION_FLAG_AFFINITY;
733 : }
734 :
735 : /** \brief Check if exact pool address should be used.
736 : @param s SNAT session
737 : @return true if exact pool address
738 : */
739 : always_inline bool
740 : nat44_ed_is_exact_address_session (snat_session_t *s)
741 : {
742 : return s->flags & SNAT_SESSION_FLAG_EXACT_ADDRESS;
743 : }
744 :
745 : /** \brief Check if NAT interface is inside.
746 : @param i NAT interface
747 : @return true if inside interface
748 : */
749 : always_inline bool
750 396 : nat44_ed_is_interface_inside (snat_interface_t *i)
751 : {
752 396 : return i->flags & NAT_INTERFACE_FLAG_IS_INSIDE;
753 : }
754 :
755 : /** \brief Check if NAT interface is outside.
756 : @param i NAT interface
757 : @return true if outside interface
758 : */
759 : always_inline bool
760 58458 : nat44_ed_is_interface_outside (snat_interface_t *i)
761 : {
762 58458 : return i->flags & NAT_INTERFACE_FLAG_IS_OUTSIDE;
763 : }
764 :
765 : /** \brief Check if client initiating TCP connection (received SYN from client)
766 : @param t TCP header
767 : @return true if client initiating TCP connection
768 : */
769 : always_inline bool
770 1886 : tcp_flags_is_init (u8 f)
771 : {
772 1886 : return (f & TCP_FLAG_SYN) && !(f & TCP_FLAG_ACK);
773 : }
774 :
775 : always_inline bool
776 304 : is_sm_addr_only (u32 f)
777 : {
778 304 : return (f & NAT_SM_FLAG_ADDR_ONLY);
779 : }
780 :
781 : always_inline bool
782 189 : is_sm_out2in_only (u32 f)
783 : {
784 189 : return (f & NAT_SM_FLAG_OUT2IN_ONLY);
785 : }
786 :
787 : always_inline bool
788 608 : is_sm_identity_nat (u32 f)
789 : {
790 608 : return (f & NAT_SM_FLAG_IDENTITY_NAT);
791 : }
792 :
793 : always_inline bool
794 518 : is_sm_lb (u32 f)
795 : {
796 518 : return (f & NAT_SM_FLAG_LB);
797 : }
798 :
799 : always_inline bool
800 11 : is_sm_exact_address (u32 f)
801 : {
802 11 : return (f & NAT_SM_FLAG_EXACT_ADDRESS);
803 : }
804 :
805 : always_inline bool
806 321 : is_sm_self_twice_nat (u32 f)
807 : {
808 321 : return (f & NAT_SM_FLAG_SELF_TWICE_NAT);
809 : }
810 :
811 : always_inline bool
812 338 : is_sm_twice_nat (u32 f)
813 : {
814 338 : return (f & NAT_SM_FLAG_TWICE_NAT);
815 : }
816 :
817 : always_inline bool
818 48 : is_sm_switch_address (u32 f)
819 : {
820 48 : return (f & NAT_SM_FLAG_SWITCH_ADDRESS);
821 : }
822 :
823 : #define nat_log_err(...) \
824 : vlib_log(VLIB_LOG_LEVEL_ERR, snat_main.log_class, __VA_ARGS__)
825 : #define nat_log_warn(...) \
826 : vlib_log(VLIB_LOG_LEVEL_WARNING, snat_main.log_class, __VA_ARGS__)
827 : #define nat_log_info(...) \
828 : vlib_log(VLIB_LOG_LEVEL_INFO, snat_main.log_class, __VA_ARGS__)
829 : #define nat_log_debug(...)\
830 : vlib_log(VLIB_LOG_LEVEL_DEBUG, snat_main.log_class, __VA_ARGS__)
831 :
832 : clib_error_t *nat44_api_hookup (vlib_main_t *vm);
833 :
834 : int snat_set_workers (uword *bitmap);
835 :
836 : int nat44_plugin_enable (nat44_config_t c);
837 : int nat44_plugin_disable ();
838 :
839 : int nat44_ed_add_interface (u32 sw_if_index, u8 is_inside);
840 : int nat44_ed_del_interface (u32 sw_if_index, u8 is_inside);
841 : int nat44_ed_add_output_interface (u32 sw_if_index);
842 : int nat44_ed_del_output_interface (u32 sw_if_index);
843 :
844 : int nat44_ed_add_address (ip4_address_t *addr, u32 vrf_id, u8 twice_nat);
845 : int nat44_ed_del_address (ip4_address_t addr, u8 twice_nat);
846 : int nat44_ed_add_interface_address (u32 sw_if_index, u8 twice_nat);
847 : int nat44_ed_del_interface_address (u32 sw_if_index, u8 twice_nat);
848 :
849 : int nat44_ed_add_del_vrf_table (u32 table_vrf_id, bool is_add);
850 : int nat44_ed_add_del_vrf_route (u32 table_vrf_id, u32 vrf_id, bool is_add);
851 : void nat44_ed_del_vrf_tables ();
852 :
853 : int nat44_ed_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
854 : u16 l_port, u16 e_port, ip_protocol_t proto,
855 : u32 vrf_id, u32 sw_if_index, u32 flags,
856 : ip4_address_t pool_addr, u8 *tag);
857 :
858 : int nat44_ed_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
859 : u16 l_port, u16 e_port, ip_protocol_t proto,
860 : u32 vrf_id, u32 sw_if_index, u32 flags);
861 :
862 : int nat44_ed_add_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
863 : ip_protocol_t proto,
864 : nat44_lb_addr_port_t *locals, u32 flags,
865 : u8 *tag, u32 affinity);
866 :
867 : int nat44_ed_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
868 : ip_protocol_t proto, u32 flags);
869 :
870 : int nat44_ed_add_del_lb_static_mapping_local (ip4_address_t e_addr, u16 e_port,
871 : ip4_address_t l_addr, u16 l_port,
872 : ip_protocol_t proto, u32 vrf_id,
873 : u8 probability, u8 is_add);
874 :
875 : /**
876 : * @brief Delete NAT44 endpoint-dependent session
877 : *
878 : * @param sm snat global configuration data
879 : * @param addr IPv4 address
880 : * @param port L4 port number
881 : * @param proto L4 protocol
882 : * @param vrf_id VRF ID
883 : * @param is_in 1 = inside network address and port pair, 0 = outside
884 : *
885 : * @return 0 on success, non-zero value otherwise
886 : */
887 : int nat44_ed_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
888 : ip4_address_t *eh_addr, u16 eh_port, u8 proto,
889 : u32 vrf_id, int is_in);
890 :
891 : void nat44_ed_free_session_data (snat_main_t *sm, snat_session_t *s,
892 : u32 thread_index, u8 is_ha);
893 :
894 : /**
895 : * @brief Set NAT44 session limit (session limit, vrf id)
896 : *
897 : * @param session_limit Session limit
898 : * @param vrf_id VRF id
899 : * @return 0 on success, non-zero value otherwise
900 : */
901 : int nat44_set_session_limit (u32 session_limit, u32 vrf_id);
902 :
903 : /**
904 : * @brief Update NAT44 session limit flushing all data (session limit, vrf id)
905 : *
906 : * @param session_limit Session limit
907 : * @param vrf_id VRF id
908 : * @return 0 on success, non-zero value otherwise
909 : */
910 : int nat44_update_session_limit (u32 session_limit, u32 vrf_id);
911 :
912 : void expire_per_vrf_sessions (u32 fib_index);
913 :
914 : int snat_static_mapping_match (vlib_main_t *vm, ip4_address_t match_addr,
915 : u16 match_port, u32 match_fib_index,
916 : ip_protocol_t match_protocol,
917 : ip4_address_t *mapping_addr, u16 *mapping_port,
918 : u32 *mapping_fib_index, int by_external,
919 : u8 *is_addr_only, twice_nat_type_t *twice_nat,
920 : lb_nat_type_t *lb, ip4_address_t *ext_host_addr,
921 : u8 *is_identity_nat,
922 : snat_static_mapping_t **out);
923 :
924 : u32 get_thread_idx_by_port (u16 e_port);
925 :
926 : u32 nat_calc_bihash_buckets (u32 n_elts);
927 :
928 : void nat44_addresses_free (snat_address_t **addresses);
929 :
930 : void nat44_ed_sessions_clear ();
931 :
932 : int nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts);
933 :
934 : void nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f);
935 :
936 : snat_static_mapping_t *nat44_ed_sm_i2o_lookup (snat_main_t *sm,
937 : ip4_address_t addr, u16 port,
938 : u32 fib_index, u8 proto);
939 :
940 : snat_static_mapping_t *nat44_ed_sm_o2i_lookup (snat_main_t *sm,
941 : ip4_address_t addr, u16 port,
942 : u32 fib_index, u8 proto);
943 :
944 : void nat_syslog_nat44_sadd (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
945 : u16 isport, ip4_address_t *idaddr, u16 idport,
946 : ip4_address_t *xsaddr, u16 xsport,
947 : ip4_address_t *xdaddr, u16 xdport, u8 proto,
948 : u8 is_twicenat);
949 :
950 : void nat_syslog_nat44_sdel (u32 ssubix, u32 sfibix, ip4_address_t *isaddr,
951 : u16 isport, ip4_address_t *idaddr, u16 idport,
952 : ip4_address_t *xsaddr, u16 xsport,
953 : ip4_address_t *xdaddr, u16 xdport, u8 proto,
954 : u8 is_twicenat);
955 :
956 : typedef enum
957 : {
958 : NAT_ED_TRNSL_ERR_SUCCESS = 0,
959 : NAT_ED_TRNSL_ERR_TRANSLATION_FAILED = 1,
960 : NAT_ED_TRNSL_ERR_FLOW_MISMATCH = 2,
961 : NAT_ED_TRNSL_ERR_PACKET_TRUNCATED = 3,
962 : NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT = 4,
963 : NAT_ED_TRNSL_ERR_INVALID_CSUM = 5,
964 : } nat_translation_error_e;
965 :
966 : nat_translation_error_e nat_6t_flow_buf_translate_i2o (
967 : vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
968 : nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
969 :
970 : nat_translation_error_e nat_6t_flow_buf_translate_o2i (
971 : vlib_main_t *vm, snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
972 : nat_6t_flow_t *f, ip_protocol_t proto, int is_output_feature);
973 :
974 : #endif /* __included_nat44_ed_h__ */
975 : /*
976 : * fd.io coding-style-patch-verification: ON
977 : *
978 : * Local Variables:
979 : * eval: (c-set-style "gnu")
980 : * End:
981 : */
|