2 * snat.c - simple nat plugin
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
23 #include <nat/nat_dpo.h>
24 #include <nat/nat_ipfix_logging.h>
25 #include <nat/nat_det.h>
26 #include <nat/nat64.h>
27 #include <nat/nat66.h>
28 #include <nat/dslite.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_affinity.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vnet/fib/ip4_fib.h>
35 #include <vpp/app/version.h>
37 snat_main_t snat_main;
40 /* Hook up input features */
41 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
42 .arc_name = "ip4-unicast",
43 .node_name = "nat44-in2out",
44 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
46 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
47 .arc_name = "ip4-unicast",
48 .node_name = "nat44-out2in",
49 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
50 "ip4-dhcp-client-detect"),
52 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
53 .arc_name = "ip4-unicast",
54 .node_name = "nat44-classify",
55 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
57 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
58 .arc_name = "ip4-unicast",
59 .node_name = "nat44-det-in2out",
60 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
62 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
63 .arc_name = "ip4-unicast",
64 .node_name = "nat44-det-out2in",
65 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
66 "ip4-dhcp-client-detect"),
68 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
69 .arc_name = "ip4-unicast",
70 .node_name = "nat44-det-classify",
71 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
73 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
74 .arc_name = "ip4-unicast",
75 .node_name = "nat44-ed-in2out",
76 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
78 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
79 .arc_name = "ip4-unicast",
80 .node_name = "nat44-ed-out2in",
81 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
82 "ip4-dhcp-client-detect"),
84 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
85 .arc_name = "ip4-unicast",
86 .node_name = "nat44-ed-classify",
87 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
89 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
90 .arc_name = "ip4-unicast",
91 .node_name = "nat44-in2out-worker-handoff",
92 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
94 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
95 .arc_name = "ip4-unicast",
96 .node_name = "nat44-out2in-worker-handoff",
97 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
98 "ip4-dhcp-client-detect"),
100 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
101 .arc_name = "ip4-unicast",
102 .node_name = "nat44-handoff-classify",
103 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
105 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
106 .arc_name = "ip4-unicast",
107 .node_name = "nat44-in2out-fast",
108 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
110 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
111 .arc_name = "ip4-unicast",
112 .node_name = "nat44-out2in-fast",
113 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
114 "ip4-dhcp-client-detect"),
116 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
117 .arc_name = "ip4-unicast",
118 .node_name = "nat44-hairpin-dst",
119 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
121 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
122 .arc_name = "ip4-unicast",
123 .node_name = "nat44-ed-hairpin-dst",
124 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
127 /* Hook up output features */
128 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
129 .arc_name = "ip4-output",
130 .node_name = "nat44-in2out-output",
131 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
133 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
134 .arc_name = "ip4-output",
135 .node_name = "nat44-in2out-output-worker-handoff",
136 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
138 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
139 .arc_name = "ip4-output",
140 .node_name = "nat44-hairpin-src",
141 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
143 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
144 .arc_name = "ip4-output",
145 .node_name = "nat44-ed-in2out-output",
146 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
148 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
149 .arc_name = "ip4-output",
150 .node_name = "nat44-ed-hairpin-src",
151 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
154 /* Hook up ip4-local features */
155 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
157 .arc_name = "ip4-local",
158 .node_name = "nat44-hairpinning",
159 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
161 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
163 .arc_name = "ip4-local",
164 .node_name = "nat44-ed-hairpinning",
165 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
170 VLIB_PLUGIN_REGISTER () = {
171 .version = VPP_BUILD_VER,
172 .description = "Network Address Translation",
176 vlib_node_registration_t nat44_classify_node;
177 vlib_node_registration_t nat44_ed_classify_node;
178 vlib_node_registration_t nat44_det_classify_node;
179 vlib_node_registration_t nat44_handoff_classify_node;
182 NAT44_CLASSIFY_NEXT_IN2OUT,
183 NAT44_CLASSIFY_NEXT_OUT2IN,
184 NAT44_CLASSIFY_N_NEXT,
185 } nat44_classify_next_t;
188 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
190 snat_session_key_t key;
191 clib_bihash_kv_8_8_t kv;
192 nat_ed_ses_key_t ed_key;
193 clib_bihash_kv_16_8_t ed_kv;
194 snat_main_per_thread_data_t *tsm =
195 vec_elt_at_index (sm->per_thread_data, thread_index);
197 if (is_fwd_bypass_session (s))
199 ed_key.l_addr = s->in2out.addr;
200 ed_key.r_addr = s->ext_host_addr;
201 ed_key.l_port = s->in2out.port;
202 ed_key.r_port = s->ext_host_port;
203 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
204 ed_key.fib_index = 0;
205 ed_kv.key[0] = ed_key.as_u64[0];
206 ed_kv.key[1] = ed_key.as_u64[1];
207 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
208 nat_log_warn ("in2out_ed key del failed");
212 /* session lookup tables */
213 if (is_ed_session (s))
215 if (is_affinity_sessions (s))
216 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
217 s->in2out.protocol, s->out2in.port);
218 ed_key.l_addr = s->out2in.addr;
219 ed_key.r_addr = s->ext_host_addr;
220 ed_key.fib_index = s->out2in.fib_index;
221 if (snat_is_unk_proto_session (s))
223 ed_key.proto = s->in2out.port;
229 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
230 ed_key.l_port = s->out2in.port;
231 ed_key.r_port = s->ext_host_port;
233 ed_kv.key[0] = ed_key.as_u64[0];
234 ed_kv.key[1] = ed_key.as_u64[1];
235 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
236 nat_log_warn ("out2in_ed key del failed");
237 ed_key.l_addr = s->in2out.addr;
238 ed_key.fib_index = s->in2out.fib_index;
239 if (!snat_is_unk_proto_session (s))
240 ed_key.l_port = s->in2out.port;
241 if (is_twice_nat_session (s))
243 ed_key.r_addr = s->ext_host_nat_addr;
244 ed_key.r_port = s->ext_host_nat_port;
246 ed_kv.key[0] = ed_key.as_u64[0];
247 ed_kv.key[1] = ed_key.as_u64[1];
248 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
249 nat_log_warn ("in2out_ed key del failed");
253 kv.key = s->in2out.as_u64;
254 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
255 nat_log_warn ("in2out key del failed");
256 kv.key = s->out2in.as_u64;
257 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
258 nat_log_warn ("out2in key del failed");
261 if (snat_is_unk_proto_session (s))
265 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
266 s->out2in.addr.as_u32,
270 s->in2out.fib_index);
272 /* Twice NAT address and port for external host */
273 if (is_twice_nat_session (s))
275 key.protocol = s->in2out.protocol;
276 key.port = s->ext_host_nat_port;
277 key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
278 snat_free_outside_address_and_port (sm->twice_nat_addresses,
282 if (snat_is_session_static (s))
285 if (s->outside_address_index != ~0)
286 snat_free_outside_address_and_port (sm->addresses, thread_index,
291 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
295 snat_user_key_t user_key;
296 clib_bihash_kv_8_8_t kv, value;
297 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
298 dlist_elt_t * per_user_list_head_elt;
300 user_key.addr.as_u32 = addr->as_u32;
301 user_key.fib_index = fib_index;
302 kv.key = user_key.as_u64;
304 /* Ever heard of the "user" = src ip4 address before? */
305 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
307 /* no, make a new one */
308 pool_get (tsm->users, u);
309 memset (u, 0, sizeof (*u));
310 u->addr.as_u32 = addr->as_u32;
311 u->fib_index = fib_index;
313 pool_get (tsm->list_pool, per_user_list_head_elt);
315 u->sessions_per_user_list_head_index = per_user_list_head_elt -
318 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
320 kv.value = u - tsm->users;
323 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
324 nat_log_warn ("user_hash keay add failed");
328 u = pool_elt_at_index (tsm->users, value.value);
335 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
338 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
339 u32 oldest_per_user_translation_list_index, session_index;
340 dlist_elt_t * oldest_per_user_translation_list_elt;
341 dlist_elt_t * per_user_translation_list_elt;
343 /* Over quota? Recycle the least recently used translation */
344 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
346 oldest_per_user_translation_list_index =
347 clib_dlist_remove_head (tsm->list_pool,
348 u->sessions_per_user_list_head_index);
350 ASSERT (oldest_per_user_translation_list_index != ~0);
352 /* Add it back to the end of the LRU list */
353 clib_dlist_addtail (tsm->list_pool,
354 u->sessions_per_user_list_head_index,
355 oldest_per_user_translation_list_index);
356 /* Get the list element */
357 oldest_per_user_translation_list_elt =
358 pool_elt_at_index (tsm->list_pool,
359 oldest_per_user_translation_list_index);
361 /* Get the session index from the list element */
362 session_index = oldest_per_user_translation_list_elt->value;
364 /* Get the session */
365 s = pool_elt_at_index (tsm->sessions, session_index);
366 nat_free_session_data (sm, s, thread_index);
367 if (snat_is_session_static(s))
368 u->nstaticsessions--;
371 s->outside_address_index = ~0;
376 s->ext_host_addr.as_u32 = 0;
377 s->ext_host_port = 0;
378 s->ext_host_nat_addr.as_u32 = 0;
379 s->ext_host_nat_port = 0;
383 pool_get (tsm->sessions, s);
384 memset (s, 0, sizeof (*s));
385 s->outside_address_index = ~0;
387 /* Create list elts */
388 pool_get (tsm->list_pool, per_user_translation_list_elt);
389 clib_dlist_init (tsm->list_pool,
390 per_user_translation_list_elt - tsm->list_pool);
392 per_user_translation_list_elt->value = s - tsm->sessions;
393 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
394 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
396 clib_dlist_addtail (tsm->list_pool,
397 s->per_user_list_head_index,
398 per_user_translation_list_elt - tsm->list_pool);
405 nat_ed_session_alloc (snat_main_t *sm, snat_user_t *u, u32 thread_index)
408 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
409 dlist_elt_t * per_user_translation_list_elt;
411 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
413 nat_log_warn ("max translations per user %U", format_ip4_address, &u->addr);
414 snat_ipfix_logging_max_entries_per_user (sm->max_translations_per_user,
419 pool_get (tsm->sessions, s);
420 memset (s, 0, sizeof (*s));
421 s->outside_address_index = ~0;
423 /* Create list elts */
424 pool_get (tsm->list_pool, per_user_translation_list_elt);
425 clib_dlist_init (tsm->list_pool,
426 per_user_translation_list_elt - tsm->list_pool);
428 per_user_translation_list_elt->value = s - tsm->sessions;
429 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
430 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
432 clib_dlist_addtail (tsm->list_pool,
433 s->per_user_list_head_index,
434 per_user_translation_list_elt - tsm->list_pool);
441 } nat44_classify_trace_t;
443 static u8 * format_nat44_classify_trace (u8 * s, va_list * args)
445 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
446 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
447 nat44_classify_trace_t *t = va_arg (*args, nat44_classify_trace_t *);
450 next = t->next_in2out ? "nat44-in2out" : "nat44-out2in";
452 s = format (s, "nat44-classify: next %s", next);
458 nat44_classify_node_fn_inline (vlib_main_t * vm,
459 vlib_node_runtime_t * node,
460 vlib_frame_t * frame,
463 u32 n_left_from, * from, * to_next;
464 nat44_classify_next_t next_index;
465 snat_main_t *sm = &snat_main;
466 snat_static_mapping_t *m;
467 u32 thread_index = vm->thread_index;
468 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
470 from = vlib_frame_vector_args (frame);
471 n_left_from = frame->n_vectors;
472 next_index = node->cached_next_index;
474 while (n_left_from > 0)
478 vlib_get_next_frame (vm, node, next_index,
479 to_next, n_left_to_next);
481 while (n_left_from > 0 && n_left_to_next > 0)
485 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT, sw_if_index0, rx_fib_index0;
488 snat_session_key_t m_key0;
489 clib_bihash_kv_8_8_t kv0, value0;
490 clib_bihash_kv_16_8_t ed_kv0, ed_value0;
493 /* speculatively enqueue b0 to the current next frame */
501 b0 = vlib_get_buffer (vm, bi0);
502 ip0 = vlib_buffer_get_current (b0);
503 udp0 = ip4_next_header (ip0);
507 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
509 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
511 make_ed_kv (&ed_kv0, &ip0->src_address, &ip0->dst_address,
512 ip0->protocol, rx_fib_index0, udp0->src_port,
514 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &ed_kv0, &ed_value0))
518 vec_foreach (ap, sm->addresses)
520 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
522 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
527 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
529 m_key0.addr = ip0->dst_address;
532 m_key0.fib_index = 0;
533 kv0.key = m_key0.as_u64;
534 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
536 m = pool_elt_at_index (sm->static_mappings, value0.value);
537 if (m->local_addr.as_u32 != m->external_addr.as_u32)
538 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
541 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
542 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
543 kv0.key = m_key0.as_u64;
544 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
546 m = pool_elt_at_index (sm->static_mappings, value0.value);
547 if (m->local_addr.as_u32 != m->external_addr.as_u32)
548 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
553 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
554 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
556 nat44_classify_trace_t *t =
557 vlib_add_trace (vm, node, b0, sizeof (*t));
558 t->next_in2out = next0 == NAT44_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
561 /* verify speculative enqueue, maybe switch current next frame */
562 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
563 to_next, n_left_to_next,
567 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
570 return frame->n_vectors;
574 nat44_classify_node_fn (vlib_main_t * vm,
575 vlib_node_runtime_t * node,
576 vlib_frame_t * frame)
578 return nat44_classify_node_fn_inline (vm, node, frame, 0);
581 VLIB_REGISTER_NODE (nat44_classify_node) = {
582 .function = nat44_classify_node_fn,
583 .name = "nat44-classify",
584 .vector_size = sizeof (u32),
585 .format_trace = format_nat44_classify_trace,
586 .type = VLIB_NODE_TYPE_INTERNAL,
587 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
589 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
590 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
594 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
595 nat44_classify_node_fn);
597 nat44_ed_classify_node_fn (vlib_main_t * vm,
598 vlib_node_runtime_t * node,
599 vlib_frame_t * frame)
601 return nat44_classify_node_fn_inline (vm, node, frame, 1);
604 VLIB_REGISTER_NODE (nat44_ed_classify_node) = {
605 .function = nat44_ed_classify_node_fn,
606 .name = "nat44-ed-classify",
607 .vector_size = sizeof (u32),
608 .format_trace = format_nat44_classify_trace,
609 .type = VLIB_NODE_TYPE_INTERNAL,
610 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
612 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-ed-in2out",
613 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-ed-out2in",
617 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_classify_node,
618 nat44_ed_classify_node_fn);
621 nat44_det_classify_node_fn (vlib_main_t * vm,
622 vlib_node_runtime_t * node,
623 vlib_frame_t * frame)
625 return nat44_classify_node_fn_inline (vm, node, frame, 0);
628 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
629 .function = nat44_det_classify_node_fn,
630 .name = "nat44-det-classify",
631 .vector_size = sizeof (u32),
632 .format_trace = format_nat44_classify_trace,
633 .type = VLIB_NODE_TYPE_INTERNAL,
634 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
636 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
637 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
641 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
642 nat44_det_classify_node_fn);
645 nat44_handoff_classify_node_fn (vlib_main_t * vm,
646 vlib_node_runtime_t * node,
647 vlib_frame_t * frame)
649 return nat44_classify_node_fn_inline (vm, node, frame, 0);
652 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
653 .function = nat44_handoff_classify_node_fn,
654 .name = "nat44-handoff-classify",
655 .vector_size = sizeof (u32),
656 .format_trace = format_nat44_classify_trace,
657 .type = VLIB_NODE_TYPE_INTERNAL,
658 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
660 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
661 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
665 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
666 nat44_handoff_classify_node_fn);
669 * @brief Add/del NAT address to FIB.
671 * Add the external NAT address to the FIB as receive entries. This ensures
672 * that VPP will reply to ARP for this address and we don't need to enable
673 * proxy ARP on the outside interface.
675 * @param addr IPv4 address.
676 * @param plen address prefix length
677 * @param sw_if_index Interface.
678 * @param is_add If 0 delete, otherwise add.
681 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
684 fib_prefix_t prefix = {
686 .fp_proto = FIB_PROTOCOL_IP4,
688 .ip4.as_u32 = addr->as_u32,
691 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
694 fib_table_entry_update_one_path(fib_index,
696 FIB_SOURCE_PLUGIN_LOW,
697 (FIB_ENTRY_FLAG_CONNECTED |
698 FIB_ENTRY_FLAG_LOCAL |
699 FIB_ENTRY_FLAG_EXCLUSIVE),
706 FIB_ROUTE_PATH_FLAG_NONE);
708 fib_table_entry_delete(fib_index,
710 FIB_SOURCE_PLUGIN_LOW);
713 int snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
718 vlib_thread_main_t *tm = vlib_get_thread_main ();
720 if (twice_nat && !sm->endpoint_dependent)
721 return VNET_API_ERROR_FEATURE_DISABLED;
723 /* Check if address already exists */
724 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
726 if (ap->addr.as_u32 == addr->as_u32)
727 return VNET_API_ERROR_VALUE_EXIST;
731 vec_add2 (sm->twice_nat_addresses, ap, 1);
733 vec_add2 (sm->addresses, ap, 1);
738 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
739 FIB_SOURCE_PLUGIN_LOW);
742 #define _(N, i, n, s) \
743 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
744 ap->busy_##n##_ports = 0; \
745 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
746 foreach_snat_protocol
752 /* Add external address to FIB */
753 pool_foreach (i, sm->interfaces,
755 if (nat_interface_is_inside(i) || sm->out2in_dpo)
758 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
761 pool_foreach (i, sm->output_feature_interfaces,
763 if (nat_interface_is_inside(i) || sm->out2in_dpo)
766 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
773 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
776 snat_static_mapping_t *m;
777 pool_foreach (m, sm->static_mappings,
779 if (m->external_addr.as_u32 == addr.as_u32)
786 void increment_v4_address (ip4_address_t * a)
790 v = clib_net_to_host_u32(a->as_u32) + 1;
791 a->as_u32 = clib_host_to_net_u32(v);
795 snat_add_static_mapping_when_resolved (snat_main_t * sm,
796 ip4_address_t l_addr,
801 snat_protocol_t proto,
806 snat_static_map_resolve_t *rp;
808 vec_add2 (sm->to_resolve, rp, 1);
809 rp->l_addr.as_u32 = l_addr.as_u32;
811 rp->sw_if_index = sw_if_index;
815 rp->addr_only = addr_only;
817 rp->tag = vec_dup (tag);
820 static u32 get_thread_idx_by_port(u16 e_port)
822 snat_main_t * sm = &snat_main;
823 u32 thread_idx = sm->num_workers;
824 if (sm->num_workers > 1)
826 thread_idx = sm->first_worker_index + sm->workers[(e_port - 1024) / sm->port_per_thread];
832 * @brief Add static mapping.
834 * Create static mapping between local addr+port and external addr+port.
836 * @param l_addr Local IPv4 address.
837 * @param e_addr External IPv4 address.
838 * @param l_port Local port number.
839 * @param e_port External port number.
840 * @param vrf_id VRF ID.
841 * @param addr_only If 0 address port and pair mapping, otherwise address only.
842 * @param sw_if_index External port instead of specific IP address.
843 * @param is_add If 0 delete static mapping, otherwise add.
844 * @param twice_nat If value is TWICE_NAT then translate external host address
846 * If value is TWICE_NAT_SELF then translate external host
847 * address and port whenever external host address equals
848 * local address of internal host.
849 * @param out2in_only If 1 rule match only out2in direction
850 * @param tag - opaque string tag
854 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
855 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
856 u32 sw_if_index, snat_protocol_t proto, int is_add,
857 twice_nat_type_t twice_nat, u8 out2in_only,
860 snat_main_t * sm = &snat_main;
861 snat_static_mapping_t *m;
862 snat_session_key_t m_key;
863 clib_bihash_kv_8_8_t kv, value;
864 snat_address_t *a = 0;
867 snat_interface_t *interface;
869 snat_main_per_thread_data_t *tsm;
870 snat_user_key_t u_key;
872 dlist_elt_t * head, * elt;
873 u32 elt_index, head_index;
877 snat_static_map_resolve_t *rp, *rp_match = 0;
879 if (!sm->endpoint_dependent)
881 if (twice_nat || out2in_only)
882 return VNET_API_ERROR_FEATURE_DISABLED;
885 /* If the external address is a specific interface address */
886 if (sw_if_index != ~0)
888 ip4_address_t * first_int_addr;
890 for (i = 0; i < vec_len (sm->to_resolve); i++)
892 rp = sm->to_resolve + i;
893 if (rp->sw_if_index != sw_if_index ||
894 rp->l_addr.as_u32 != l_addr.as_u32 ||
895 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
900 if (rp->l_port != l_port || rp->e_port != e_port || rp->proto != proto)
908 /* Might be already set... */
909 first_int_addr = ip4_interface_first_address
910 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
915 return VNET_API_ERROR_VALUE_EXIST;
917 snat_add_static_mapping_when_resolved
918 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
919 addr_only, is_add, tag);
921 /* DHCP resolution required? */
922 if (first_int_addr == 0)
928 e_addr.as_u32 = first_int_addr->as_u32;
929 /* Identity mapping? */
930 if (l_addr.as_u32 == 0)
931 l_addr.as_u32 = e_addr.as_u32;
937 return VNET_API_ERROR_NO_SUCH_ENTRY;
939 vec_del1 (sm->to_resolve, i);
943 e_addr.as_u32 = first_int_addr->as_u32;
944 /* Identity mapping? */
945 if (l_addr.as_u32 == 0)
946 l_addr.as_u32 = e_addr.as_u32;
954 m_key.port = addr_only ? 0 : e_port;
955 m_key.protocol = addr_only ? 0 : proto;
957 kv.key = m_key.as_u64;
958 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
961 m = pool_elt_at_index (sm->static_mappings, value.value);
966 return VNET_API_ERROR_VALUE_EXIST;
968 if (twice_nat && addr_only)
969 return VNET_API_ERROR_UNSUPPORTED;
971 /* Convert VRF id to FIB index */
974 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
976 return VNET_API_ERROR_NO_SUCH_FIB;
979 /* If not specified use inside VRF id from SNAT plugin startup config */
982 fib_index = sm->inside_fib_index;
983 vrf_id = sm->inside_vrf_id;
989 m_key.port = addr_only ? 0 : l_port;
990 m_key.protocol = addr_only ? 0 : proto;
991 m_key.fib_index = fib_index;
992 kv.key = m_key.as_u64;
993 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
994 return VNET_API_ERROR_VALUE_EXIST;
997 /* Find external address in allocated addresses and reserve port for
998 address and port pair mapping when dynamic translations enabled */
999 if (!(addr_only || sm->static_mapping_only || out2in_only))
1001 for (i = 0; i < vec_len (sm->addresses); i++)
1003 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1005 a = sm->addresses + i;
1006 /* External port must be unused */
1009 #define _(N, j, n, s) \
1010 case SNAT_PROTOCOL_##N: \
1011 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1012 return VNET_API_ERROR_INVALID_VALUE; \
1013 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1014 if (e_port > 1024) \
1016 a->busy_##n##_ports++; \
1017 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1020 foreach_snat_protocol
1023 nat_log_info ("unknown protocol");
1024 return VNET_API_ERROR_INVALID_VALUE_2;
1029 /* External address must be allocated */
1030 if (!a && (l_addr.as_u32 != e_addr.as_u32))
1032 if (sw_if_index != ~0)
1034 for (i = 0; i < vec_len (sm->to_resolve); i++)
1036 rp = sm->to_resolve + i;
1039 if (rp->sw_if_index != sw_if_index &&
1040 rp->l_addr.as_u32 != l_addr.as_u32 &&
1041 rp->vrf_id != vrf_id && rp->l_port != l_port &&
1042 rp->e_port != e_port && rp->proto != proto)
1045 vec_del1 (sm->to_resolve, i);
1049 return VNET_API_ERROR_NO_SUCH_ENTRY;
1053 pool_get (sm->static_mappings, m);
1054 memset (m, 0, sizeof (*m));
1055 m->tag = vec_dup (tag);
1056 m->local_addr = l_addr;
1057 m->external_addr = e_addr;
1058 m->addr_only = addr_only;
1060 m->fib_index = fib_index;
1061 m->twice_nat = twice_nat;
1062 m->out2in_only = out2in_only;
1065 m->local_port = l_port;
1066 m->external_port = e_port;
1070 if (sm->num_workers > 1)
1073 .src_address = m->local_addr,
1075 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index));
1076 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1079 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1081 m_key.addr = m->local_addr;
1082 m_key.port = m->local_port;
1083 m_key.protocol = m->proto;
1084 m_key.fib_index = m->fib_index;
1085 kv.key = m_key.as_u64;
1086 kv.value = m - sm->static_mappings;
1088 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1090 m_key.addr = m->external_addr;
1091 m_key.port = m->external_port;
1092 m_key.fib_index = 0;
1093 kv.key = m_key.as_u64;
1094 kv.value = m - sm->static_mappings;
1095 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
1097 /* Delete dynamic sessions matching local address (+ local port) */
1098 if (!(sm->static_mapping_only))
1100 u_key.addr = m->local_addr;
1101 u_key.fib_index = m->fib_index;
1102 kv.key = u_key.as_u64;
1103 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1105 user_index = value.value;
1106 u = pool_elt_at_index (tsm->users, user_index);
1109 head_index = u->sessions_per_user_list_head_index;
1110 head = pool_elt_at_index (tsm->list_pool, head_index);
1111 elt_index = head->next;
1112 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1113 ses_index = elt->value;
1114 while (ses_index != ~0)
1116 s = pool_elt_at_index (tsm->sessions, ses_index);
1117 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1118 ses_index = elt->value;
1120 if (snat_is_session_static (s))
1123 if (!addr_only && (clib_net_to_host_u16 (s->in2out.port) != m->local_port))
1126 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1127 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1129 if (!addr_only && !sm->endpoint_dependent)
1140 if (sw_if_index != ~0)
1143 return VNET_API_ERROR_NO_SUCH_ENTRY;
1146 /* Free external address port */
1147 if (!(addr_only || sm->static_mapping_only || out2in_only))
1149 for (i = 0; i < vec_len (sm->addresses); i++)
1151 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1153 a = sm->addresses + i;
1156 #define _(N, j, n, s) \
1157 case SNAT_PROTOCOL_##N: \
1158 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1159 if (e_port > 1024) \
1161 a->busy_##n##_ports--; \
1162 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1165 foreach_snat_protocol
1168 nat_log_info ("unknown protocol");
1169 return VNET_API_ERROR_INVALID_VALUE_2;
1176 if (sm->num_workers > 1)
1177 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1179 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1181 m_key.addr = m->local_addr;
1182 m_key.port = m->local_port;
1183 m_key.protocol = m->proto;
1184 m_key.fib_index = m->fib_index;
1185 kv.key = m_key.as_u64;
1187 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
1189 m_key.addr = m->external_addr;
1190 m_key.port = m->external_port;
1191 m_key.fib_index = 0;
1192 kv.key = m_key.as_u64;
1193 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
1195 /* Delete session(s) for static mapping if exist */
1196 if (!(sm->static_mapping_only) ||
1197 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1199 u_key.addr = m->local_addr;
1200 u_key.fib_index = m->fib_index;
1201 kv.key = u_key.as_u64;
1202 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1204 user_index = value.value;
1205 u = pool_elt_at_index (tsm->users, user_index);
1206 if (u->nstaticsessions)
1208 head_index = u->sessions_per_user_list_head_index;
1209 head = pool_elt_at_index (tsm->list_pool, head_index);
1210 elt_index = head->next;
1211 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1212 ses_index = elt->value;
1213 while (ses_index != ~0)
1215 s = pool_elt_at_index (tsm->sessions, ses_index);
1216 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1217 ses_index = elt->value;
1221 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1222 (clib_net_to_host_u16 (s->out2in.port) != e_port))
1226 if (is_lb_session (s))
1229 if (!snat_is_session_static (s))
1232 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1233 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1235 if (!addr_only && !sm->endpoint_dependent)
1243 vec_free (m->workers);
1244 /* Delete static mapping from pool */
1245 pool_put (sm->static_mappings, m);
1248 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1251 /* Add/delete external address to FIB */
1252 pool_foreach (interface, sm->interfaces,
1254 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1257 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1260 pool_foreach (interface, sm->output_feature_interfaces,
1262 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1265 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1272 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1273 snat_protocol_t proto,
1274 nat44_lb_addr_port_t *locals, u8 is_add,
1275 twice_nat_type_t twice_nat, u8 out2in_only,
1276 u8 *tag, u32 affinity)
1278 snat_main_t * sm = &snat_main;
1279 snat_static_mapping_t *m;
1280 snat_session_key_t m_key;
1281 clib_bihash_kv_8_8_t kv, value;
1282 snat_address_t *a = 0;
1284 nat44_lb_addr_port_t *local;
1285 u32 elt_index, head_index, ses_index;
1286 snat_main_per_thread_data_t *tsm;
1287 snat_user_key_t u_key;
1290 dlist_elt_t * head, * elt;
1293 if (!sm->endpoint_dependent)
1294 return VNET_API_ERROR_FEATURE_DISABLED;
1296 m_key.addr = e_addr;
1297 m_key.port = e_port;
1298 m_key.protocol = proto;
1299 m_key.fib_index = 0;
1300 kv.key = m_key.as_u64;
1301 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1304 m = pool_elt_at_index (sm->static_mappings, value.value);
1309 return VNET_API_ERROR_VALUE_EXIST;
1311 if (vec_len (locals) < 2)
1312 return VNET_API_ERROR_INVALID_VALUE;
1314 /* Find external address in allocated addresses and reserve port for
1315 address and port pair mapping when dynamic translations enabled */
1316 if (!(sm->static_mapping_only || out2in_only))
1318 for (i = 0; i < vec_len (sm->addresses); i++)
1320 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1322 a = sm->addresses + i;
1323 /* External port must be unused */
1326 #define _(N, j, n, s) \
1327 case SNAT_PROTOCOL_##N: \
1328 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1329 return VNET_API_ERROR_INVALID_VALUE; \
1330 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1331 if (e_port > 1024) \
1333 a->busy_##n##_ports++; \
1334 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1337 foreach_snat_protocol
1340 nat_log_info ("unknown protocol");
1341 return VNET_API_ERROR_INVALID_VALUE_2;
1346 /* External address must be allocated */
1348 return VNET_API_ERROR_NO_SUCH_ENTRY;
1351 pool_get (sm->static_mappings, m);
1352 memset (m, 0, sizeof (*m));
1353 m->tag = vec_dup (tag);
1354 m->external_addr = e_addr;
1356 m->external_port = e_port;
1358 m->twice_nat = twice_nat;
1359 m->out2in_only = out2in_only;
1360 m->affinity = affinity;
1363 m->affinity_per_service_list_head_index =
1364 nat_affinity_get_per_service_list_head_index();
1366 m->affinity_per_service_list_head_index = ~0;
1368 m_key.addr = m->external_addr;
1369 m_key.port = m->external_port;
1370 m_key.protocol = m->proto;
1371 m_key.fib_index = 0;
1372 kv.key = m_key.as_u64;
1373 kv.value = m - sm->static_mappings;
1374 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1376 nat_log_err ("static_mapping_by_external key add failed");
1377 return VNET_API_ERROR_UNSPECIFIED;
1380 m_key.fib_index = m->fib_index;
1381 for (i = 0; i < vec_len (locals); i++)
1383 locals[i].fib_index = fib_table_find_or_create_and_lock (
1384 FIB_PROTOCOL_IP4, locals[i].vrf_id, FIB_SOURCE_PLUGIN_LOW);
1385 m_key.addr = locals[i].addr;
1386 m_key.fib_index = locals[i].fib_index;
1389 m_key.port = locals[i].port;
1390 kv.key = m_key.as_u64;
1391 kv.value = m - sm->static_mappings;
1392 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1394 locals[i].prefix = (i == 0) ? locals[i].probability :\
1395 (locals[i - 1].prefix + locals[i].probability);
1396 vec_add1 (m->locals, locals[i]);
1397 if (sm->num_workers > 1)
1400 .src_address = locals[i].addr,
1402 bitmap = clib_bitmap_set (
1403 bitmap, sm->worker_in2out_cb (&ip, m->fib_index), 1);
1407 /* Assign workers */
1408 if (sm->num_workers > 1)
1410 clib_bitmap_foreach (i, bitmap,
1412 vec_add1(m->workers, i);
1419 return VNET_API_ERROR_NO_SUCH_ENTRY;
1421 /* Free external address port */
1422 if (!(sm->static_mapping_only || out2in_only))
1424 for (i = 0; i < vec_len (sm->addresses); i++)
1426 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1428 a = sm->addresses + i;
1431 #define _(N, j, n, s) \
1432 case SNAT_PROTOCOL_##N: \
1433 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1434 if (e_port > 1024) \
1436 a->busy_##n##_ports--; \
1437 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1440 foreach_snat_protocol
1443 nat_log_info ("unknown protocol");
1444 return VNET_API_ERROR_INVALID_VALUE_2;
1451 m_key.addr = m->external_addr;
1452 m_key.port = m->external_port;
1453 m_key.protocol = m->proto;
1454 m_key.fib_index = 0;
1455 kv.key = m_key.as_u64;
1456 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1458 nat_log_err ("static_mapping_by_external key del failed");
1459 return VNET_API_ERROR_UNSPECIFIED;
1462 vec_foreach (local, m->locals)
1464 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1465 FIB_SOURCE_PLUGIN_LOW);
1466 m_key.addr = local->addr;
1469 m_key.port = local->port;
1470 m_key.fib_index = local->fib_index;
1471 kv.key = m_key.as_u64;
1472 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1474 nat_log_err ("static_mapping_by_local key del failed");
1475 return VNET_API_ERROR_UNSPECIFIED;
1479 if (sm->num_workers > 1)
1482 .src_address = local->addr,
1484 tsm = vec_elt_at_index (sm->per_thread_data,
1485 sm->worker_in2out_cb (&ip, m->fib_index));
1488 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1490 /* Delete sessions */
1491 u_key.addr = local->addr;
1492 u_key.fib_index = m->fib_index;
1493 kv.key = u_key.as_u64;
1494 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1496 u = pool_elt_at_index (tsm->users, value.value);
1497 if (u->nstaticsessions)
1499 head_index = u->sessions_per_user_list_head_index;
1500 head = pool_elt_at_index (tsm->list_pool, head_index);
1501 elt_index = head->next;
1502 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1503 ses_index = elt->value;
1504 while (ses_index != ~0)
1506 s = pool_elt_at_index (tsm->sessions, ses_index);
1507 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1508 ses_index = elt->value;
1510 if (!(is_lb_session (s)))
1513 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1514 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1517 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1518 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1524 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1525 vec_free(m->locals);
1527 vec_free(m->workers);
1529 pool_put (sm->static_mappings, m);
1536 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1539 snat_address_t *a = 0;
1540 snat_session_t *ses;
1541 u32 *ses_to_be_removed = 0, *ses_index;
1542 snat_main_per_thread_data_t *tsm;
1543 snat_static_mapping_t *m;
1544 snat_interface_t *interface;
1546 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1548 /* Find SNAT address */
1549 for (i=0; i < vec_len (addresses); i++)
1551 if (addresses[i].addr.as_u32 == addr.as_u32)
1558 return VNET_API_ERROR_NO_SUCH_ENTRY;
1562 pool_foreach (m, sm->static_mappings,
1564 if (m->external_addr.as_u32 == addr.as_u32)
1565 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1566 m->local_port, m->external_port,
1567 m->vrf_id, m->addr_only, ~0,
1568 m->proto, 0, m->twice_nat,
1569 m->out2in_only, m->tag);
1574 /* Check if address is used in some static mapping */
1575 if (is_snat_address_used_in_static_mapping(sm, addr))
1577 nat_log_notice ("address used in static mapping");
1578 return VNET_API_ERROR_UNSPECIFIED;
1582 if (a->fib_index != ~0)
1583 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1584 FIB_SOURCE_PLUGIN_LOW);
1586 /* Delete sessions using address */
1587 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1589 vec_foreach (tsm, sm->per_thread_data)
1591 pool_foreach (ses, tsm->sessions, ({
1592 if (ses->out2in.addr.as_u32 == addr.as_u32)
1594 ses->outside_address_index = ~0;
1595 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1596 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1600 vec_foreach (ses_index, ses_to_be_removed)
1602 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1603 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1606 vec_free (ses_to_be_removed);
1612 vec_del1 (sm->twice_nat_addresses, i);
1616 vec_del1 (sm->addresses, i);
1618 /* Delete external address from FIB */
1619 pool_foreach (interface, sm->interfaces,
1621 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1624 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1627 pool_foreach (interface, sm->output_feature_interfaces,
1629 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1632 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1639 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1641 snat_main_t *sm = &snat_main;
1642 snat_interface_t *i;
1643 const char * feature_name, *del_feature_name;
1644 snat_address_t * ap;
1645 snat_static_mapping_t * m;
1646 snat_det_map_t * dm;
1647 nat_outside_fib_t *outside_fib;
1648 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1651 if (sm->out2in_dpo && !is_inside)
1652 return VNET_API_ERROR_UNSUPPORTED;
1654 pool_foreach (i, sm->output_feature_interfaces,
1656 if (i->sw_if_index == sw_if_index)
1657 return VNET_API_ERROR_VALUE_EXIST;
1660 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1661 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1664 if (sm->num_workers > 1 && !sm->deterministic)
1665 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1666 else if (sm->deterministic)
1667 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1668 else if (sm->endpoint_dependent)
1669 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1671 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1674 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1675 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1678 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1679 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1684 vec_foreach (outside_fib, sm->outside_fibs)
1686 if (outside_fib->fib_index == fib_index)
1690 outside_fib->refcount--;
1691 if (!outside_fib->refcount)
1692 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1695 outside_fib->refcount++;
1701 vec_add2 (sm->outside_fibs, outside_fib, 1);
1702 outside_fib->refcount = 1;
1703 outside_fib->fib_index = fib_index;
1707 pool_foreach (i, sm->interfaces,
1709 if (i->sw_if_index == sw_if_index)
1713 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1716 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1718 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1720 if (sm->num_workers > 1 && !sm->deterministic)
1722 del_feature_name = "nat44-handoff-classify";
1723 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1724 "nat44-out2in-worker-handoff";
1726 else if (sm->deterministic)
1728 del_feature_name = "nat44-det-classify";
1729 feature_name = !is_inside ? "nat44-det-in2out" :
1732 else if (sm->endpoint_dependent)
1734 del_feature_name = "nat44-ed-classify";
1735 feature_name = !is_inside ? "nat44-ed-in2out" :
1740 del_feature_name = "nat44-classify";
1741 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1744 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1745 sw_if_index, 0, 0, 0);
1746 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1747 sw_if_index, 1, 0, 0);
1750 if (sm->endpoint_dependent)
1751 vnet_feature_enable_disable ("ip4-local",
1752 "nat44-ed-hairpinning",
1753 sw_if_index, 1, 0, 0);
1754 else if (!sm->deterministic)
1755 vnet_feature_enable_disable ("ip4-local",
1756 "nat44-hairpinning",
1757 sw_if_index, 1, 0, 0);
1762 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1763 sw_if_index, 0, 0, 0);
1764 pool_put (sm->interfaces, i);
1767 if (sm->endpoint_dependent)
1768 vnet_feature_enable_disable ("ip4-local",
1769 "nat44-ed-hairpinning",
1770 sw_if_index, 0, 0, 0);
1771 else if (!sm->deterministic)
1772 vnet_feature_enable_disable ("ip4-local",
1773 "nat44-hairpinning",
1774 sw_if_index, 0, 0, 0);
1780 if ((nat_interface_is_inside(i) && is_inside) ||
1781 (nat_interface_is_outside(i) && !is_inside))
1784 if (sm->num_workers > 1 && !sm->deterministic)
1786 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1787 "nat44-out2in-worker-handoff";
1788 feature_name = "nat44-handoff-classify";
1790 else if (sm->deterministic)
1792 del_feature_name = !is_inside ? "nat44-det-in2out" :
1794 feature_name = "nat44-det-classify";
1796 else if (sm->endpoint_dependent)
1798 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1800 feature_name = "nat44-ed-classify";
1804 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1805 feature_name = "nat44-classify";
1808 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1809 sw_if_index, 0, 0, 0);
1810 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1811 sw_if_index, 1, 0, 0);
1814 if (sm->endpoint_dependent)
1815 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1816 sw_if_index, 0, 0, 0);
1817 else if (!sm->deterministic)
1818 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1819 sw_if_index, 0, 0, 0);
1829 return VNET_API_ERROR_NO_SUCH_ENTRY;
1831 pool_get (sm->interfaces, i);
1832 i->sw_if_index = sw_if_index;
1834 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1836 if (is_inside && !sm->out2in_dpo)
1838 if (sm->endpoint_dependent)
1839 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1840 sw_if_index, 1, 0, 0);
1841 else if (!sm->deterministic)
1842 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1843 sw_if_index, 1, 0, 0);
1849 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1853 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1855 /* Add/delete external addresses to FIB */
1857 vec_foreach (ap, sm->addresses)
1858 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1860 pool_foreach (m, sm->static_mappings,
1862 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1865 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1868 pool_foreach (dm, sm->det_maps,
1870 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1876 int snat_interface_add_del_output_feature (u32 sw_if_index,
1880 snat_main_t *sm = &snat_main;
1881 snat_interface_t *i;
1882 snat_address_t * ap;
1883 snat_static_mapping_t * m;
1885 if (sm->deterministic ||
1886 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1887 return VNET_API_ERROR_UNSUPPORTED;
1889 pool_foreach (i, sm->interfaces,
1891 if (i->sw_if_index == sw_if_index)
1892 return VNET_API_ERROR_VALUE_EXIST;
1897 if (sm->endpoint_dependent)
1899 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
1900 sw_if_index, !is_del, 0, 0);
1901 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
1902 sw_if_index, !is_del, 0, 0);
1906 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1907 sw_if_index, !is_del, 0, 0);
1908 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1909 sw_if_index, !is_del, 0, 0);
1914 if (sm->num_workers > 1)
1916 vnet_feature_enable_disable ("ip4-unicast",
1917 "nat44-out2in-worker-handoff",
1918 sw_if_index, !is_del, 0, 0);
1919 vnet_feature_enable_disable ("ip4-output",
1920 "nat44-in2out-output-worker-handoff",
1921 sw_if_index, !is_del, 0, 0);
1925 if (sm->endpoint_dependent)
1927 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
1928 sw_if_index, !is_del, 0, 0);
1929 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
1930 sw_if_index, !is_del, 0, 0);
1934 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
1935 sw_if_index, !is_del, 0, 0);
1936 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1937 sw_if_index, !is_del, 0, 0);
1942 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1943 sm->fq_in2out_output_index =
1944 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1946 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1947 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1949 pool_foreach (i, sm->output_feature_interfaces,
1951 if (i->sw_if_index == sw_if_index)
1954 pool_put (sm->output_feature_interfaces, i);
1956 return VNET_API_ERROR_VALUE_EXIST;
1963 return VNET_API_ERROR_NO_SUCH_ENTRY;
1965 pool_get (sm->output_feature_interfaces, i);
1966 i->sw_if_index = sw_if_index;
1969 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1971 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1973 /* Add/delete external addresses to FIB */
1978 vec_foreach (ap, sm->addresses)
1979 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1981 pool_foreach (m, sm->static_mappings,
1983 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1986 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1992 int snat_set_workers (uword * bitmap)
1994 snat_main_t *sm = &snat_main;
1997 if (sm->num_workers < 2)
1998 return VNET_API_ERROR_FEATURE_DISABLED;
2000 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2001 return VNET_API_ERROR_INVALID_WORKER;
2003 vec_free (sm->workers);
2004 clib_bitmap_foreach (i, bitmap,
2006 vec_add1(sm->workers, i);
2007 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2011 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2012 sm->num_snat_thread = _vec_len (sm->workers);
2019 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2022 ip4_address_t * address,
2024 u32 if_address_index,
2028 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2031 ip4_address_t * address,
2033 u32 if_address_index,
2037 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2040 snat_session_key_t * k,
2041 u32 * address_indexp,
2042 u16 port_per_thread,
2043 u32 snat_thread_index);
2045 static clib_error_t * snat_init (vlib_main_t * vm)
2047 snat_main_t * sm = &snat_main;
2048 clib_error_t * error = 0;
2049 ip4_main_t * im = &ip4_main;
2050 ip_lookup_main_t * lm = &im->lookup_main;
2052 vlib_thread_registration_t *tr;
2053 vlib_thread_main_t *tm = vlib_get_thread_main ();
2056 ip4_add_del_interface_address_callback_t cb4;
2057 vlib_node_t * error_drop_node;
2060 sm->vnet_main = vnet_get_main();
2062 sm->ip4_lookup_main = lm;
2063 sm->api_main = &api_main;
2064 sm->first_worker_index = 0;
2065 sm->num_workers = 0;
2066 sm->num_snat_thread = 1;
2068 sm->port_per_thread = 0xffff - 1024;
2069 sm->fq_in2out_index = ~0;
2070 sm->fq_out2in_index = ~0;
2071 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2072 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2073 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2074 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2075 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2076 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2077 sm->forwarding_enabled = 0;
2078 sm->log_class = vlib_log_register_class ("nat", 0);
2079 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2080 sm->error_node_index = error_drop_node->index;
2081 sm->mss_clamping = 0;
2083 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2086 tr = (vlib_thread_registration_t *) p[0];
2089 sm->num_workers = tr->count;
2090 sm->first_worker_index = tr->first_index;
2094 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2096 /* Use all available workers by default */
2097 if (sm->num_workers > 1)
2099 for (i=0; i < sm->num_workers; i++)
2100 bitmap = clib_bitmap_set (bitmap, i, 1);
2101 snat_set_workers(bitmap);
2102 clib_bitmap_free (bitmap);
2106 sm->per_thread_data[0].snat_thread_index = 0;
2109 error = snat_api_init(vm, sm);
2113 /* Set up the interface address add/del callback */
2114 cb4.function = snat_ip4_add_del_interface_address_cb;
2115 cb4.function_opaque = 0;
2117 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2119 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2120 cb4.function_opaque = 0;
2122 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2124 nat_dpo_module_init ();
2126 /* Init IPFIX logging */
2127 snat_ipfix_logging_init(vm);
2130 error = nat64_init(vm);
2138 /* Init virtual fragmenentation reassembly */
2139 return nat_reass_init(vm);
2142 VLIB_INIT_FUNCTION (snat_init);
2144 void snat_free_outside_address_and_port (snat_address_t * addresses,
2146 snat_session_key_t * k)
2150 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2152 for (address_index = 0; address_index < vec_len (addresses); address_index++)
2154 if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2158 ASSERT (address_index < vec_len (addresses));
2160 a = addresses + address_index;
2162 switch (k->protocol)
2164 #define _(N, i, n, s) \
2165 case SNAT_PROTOCOL_##N: \
2166 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2167 port_host_byte_order) == 1); \
2168 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2169 port_host_byte_order, 0); \
2170 a->busy_##n##_ports--; \
2171 a->busy_##n##_ports_per_thread[thread_index]--; \
2173 foreach_snat_protocol
2176 nat_log_info ("unknown protocol");
2182 * @brief Match NAT44 static mapping.
2184 * @param sm NAT main.
2185 * @param match Address and port to match.
2186 * @param mapping External or local address and port of the matched mapping.
2187 * @param by_external If 0 match by local address otherwise match by external
2189 * @param is_addr_only If matched mapping is address only
2190 * @param twice_nat If matched mapping is twice NAT.
2191 * @param lb If matched mapping is load-balanced.
2193 * @returns 0 if match found otherwise 1.
2195 int snat_static_mapping_match (snat_main_t * sm,
2196 snat_session_key_t match,
2197 snat_session_key_t * mapping,
2200 twice_nat_type_t *twice_nat,
2202 ip4_address_t * ext_host_addr)
2204 clib_bihash_kv_8_8_t kv, value;
2205 snat_static_mapping_t *m;
2206 snat_session_key_t m_key;
2207 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2208 u32 rand, lo = 0, hi, mid;
2211 m_key.fib_index = match.fib_index;
2214 mapping_hash = &sm->static_mapping_by_external;
2215 m_key.fib_index = 0;
2218 m_key.addr = match.addr;
2219 m_key.port = clib_net_to_host_u16 (match.port);
2220 m_key.protocol = match.protocol;
2222 kv.key = m_key.as_u64;
2224 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2226 /* Try address only mapping */
2229 kv.key = m_key.as_u64;
2230 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2234 m = pool_elt_at_index (sm->static_mappings, value.value);
2238 if (vec_len (m->locals))
2240 if (PREDICT_FALSE(lb != 0))
2241 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2244 if (nat_affinity_find_and_lock (ext_host_addr[0], match.addr,
2245 match.protocol, match.port, &backend_index))
2248 mapping->addr = m->locals[backend_index].addr;
2249 mapping->port = clib_host_to_net_u16 (m->locals[backend_index].port);
2250 mapping->fib_index = m->locals[backend_index].fib_index;
2254 hi = vec_len (m->locals) - 1;
2255 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2258 mid = ((hi - lo) >> 1) + lo;
2259 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2261 if (!(m->locals[lo].prefix >= rand))
2263 if (PREDICT_FALSE (sm->num_workers > 1))
2266 .src_address = m->locals[lo].addr,
2268 if (sm->worker_in2out_cb (&ip, m->fib_index) != vlib_get_thread_index ())
2271 mapping->addr = m->locals[lo].addr;
2272 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2273 mapping->fib_index = m->locals[lo].fib_index;
2276 if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2277 match.protocol, match.port, lo, m->affinity,
2278 m->affinity_per_service_list_head_index))
2279 nat_log_info ("create affinity record failed");
2284 if (PREDICT_FALSE(lb != 0))
2286 mapping->fib_index = m->fib_index;
2287 mapping->addr = m->local_addr;
2288 /* Address only mapping doesn't change port */
2289 mapping->port = m->addr_only ? match.port
2290 : clib_host_to_net_u16 (m->local_port);
2292 mapping->protocol = m->proto;
2296 mapping->addr = m->external_addr;
2297 /* Address only mapping doesn't change port */
2298 mapping->port = m->addr_only ? match.port
2299 : clib_host_to_net_u16 (m->external_port);
2300 mapping->fib_index = sm->outside_fib_index;
2304 if (PREDICT_FALSE(is_addr_only != 0))
2305 *is_addr_only = m->addr_only;
2307 if (PREDICT_FALSE(twice_nat != 0))
2308 *twice_nat = m->twice_nat;
2313 static_always_inline u16
2314 snat_random_port (u16 min, u16 max)
2316 snat_main_t *sm = &snat_main;
2317 return min + random_u32 (&sm->random_seed) /
2318 (random_u32_max() / (max - min + 1) + 1);
2322 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2325 snat_session_key_t * k,
2326 u32 * address_indexp,
2327 u16 port_per_thread,
2328 u32 snat_thread_index)
2330 snat_main_t *sm = &snat_main;
2332 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
2333 address_indexp, port_per_thread,
2338 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2341 snat_session_key_t * k,
2342 u32 * address_indexp,
2343 u16 port_per_thread,
2344 u32 snat_thread_index)
2347 snat_address_t *a, *ga = 0;
2350 for (i = 0; i < vec_len (addresses); i++)
2353 switch (k->protocol)
2355 #define _(N, j, n, s) \
2356 case SNAT_PROTOCOL_##N: \
2357 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2359 if (a->fib_index == fib_index) \
2363 portnum = (port_per_thread * \
2364 snat_thread_index) + \
2365 snat_random_port(1, port_per_thread) + 1024; \
2366 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2368 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2369 a->busy_##n##_ports_per_thread[thread_index]++; \
2370 a->busy_##n##_ports++; \
2371 k->addr = a->addr; \
2372 k->port = clib_host_to_net_u16(portnum); \
2373 *address_indexp = i; \
2377 else if (a->fib_index == ~0) \
2384 foreach_snat_protocol
2387 nat_log_info ("unknown protocol");
2396 switch (k->protocol)
2398 #define _(N, j, n, s) \
2399 case SNAT_PROTOCOL_##N: \
2402 portnum = (port_per_thread * \
2403 snat_thread_index) + \
2404 snat_random_port(1, port_per_thread) + 1024; \
2405 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2407 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2408 a->busy_##n##_ports_per_thread[thread_index]++; \
2409 a->busy_##n##_ports++; \
2410 k->addr = a->addr; \
2411 k->port = clib_host_to_net_u16(portnum); \
2412 *address_indexp = gi; \
2416 foreach_snat_protocol
2419 nat_log_info ("unknown protocol");
2424 /* Totally out of translations to use... */
2425 snat_ipfix_logging_addresses_exhausted(0);
2430 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2433 snat_session_key_t * k,
2434 u32 * address_indexp,
2435 u16 port_per_thread,
2436 u32 snat_thread_index)
2438 snat_main_t *sm = &snat_main;
2439 snat_address_t *a = addresses;
2440 u16 m, ports, portnum, A, j;
2441 m = 16 - (sm->psid_offset + sm->psid_length);
2442 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2444 if (!vec_len (addresses))
2447 switch (k->protocol)
2449 #define _(N, i, n, s) \
2450 case SNAT_PROTOCOL_##N: \
2451 if (a->busy_##n##_ports < ports) \
2455 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2456 j = snat_random_port(0, pow2_mask(m)); \
2457 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2458 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2460 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2461 a->busy_##n##_ports++; \
2462 k->addr = a->addr; \
2463 k->port = clib_host_to_net_u16 (portnum); \
2464 *address_indexp = i; \
2469 foreach_snat_protocol
2472 nat_log_info ("unknown protocol");
2477 /* Totally out of translations to use... */
2478 snat_ipfix_logging_addresses_exhausted(0);
2483 nat_alloc_addr_and_port_range (snat_address_t * addresses,
2486 snat_session_key_t * k,
2487 u32 * address_indexp,
2488 u16 port_per_thread,
2489 u32 snat_thread_index)
2491 snat_main_t *sm = &snat_main;
2492 snat_address_t *a = addresses;
2495 ports = sm->end_port - sm->start_port + 1;
2497 if (!vec_len (addresses))
2500 switch (k->protocol)
2502 #define _(N, i, n, s) \
2503 case SNAT_PROTOCOL_##N: \
2504 if (a->busy_##n##_ports < ports) \
2508 portnum = snat_random_port(sm->start_port, sm->end_port); \
2509 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2511 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2512 a->busy_##n##_ports++; \
2513 k->addr = a->addr; \
2514 k->port = clib_host_to_net_u16 (portnum); \
2515 *address_indexp = i; \
2520 foreach_snat_protocol
2523 nat_log_info ("unknown protocol");
2528 /* Totally out of translations to use... */
2529 snat_ipfix_logging_addresses_exhausted(0);
2534 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2536 dpo_id_t dpo_v4 = DPO_INVALID;
2537 fib_prefix_t pfx = {
2538 .fp_proto = FIB_PROTOCOL_IP4,
2540 .fp_addr.ip4.as_u32 = addr.as_u32,
2545 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2546 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2547 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2548 dpo_reset (&dpo_v4);
2552 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2557 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2559 u32 *r = va_arg (*args, u32 *);
2562 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2563 foreach_snat_protocol
2571 format_snat_protocol (u8 * s, va_list * args)
2573 u32 i = va_arg (*args, u32);
2578 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2579 foreach_snat_protocol
2582 s = format (s, "unknown");
2585 s = format (s, "%s", t);
2590 format_nat_addr_and_port_alloc_alg (u8 * s, va_list * args)
2592 u32 i = va_arg (*args, u32);
2597 #define _(v, N, s) case NAT_ADDR_AND_PORT_ALLOC_ALG_##N: t = (u8 *) s; break;
2598 foreach_nat_addr_and_port_alloc_alg
2601 s = format (s, "unknown");
2604 s = format (s, "%s", t);
2608 u8 * format_snat_key (u8 * s, va_list * args);
2609 u8 * format_static_mapping_key (u8 * s, va_list * args);
2612 format_session_kvp (u8 * s, va_list * args)
2614 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2615 snat_session_key_t k;
2619 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2625 format_static_mapping_kvp (u8 * s, va_list * args)
2627 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2628 snat_session_key_t k;
2632 s = format (s, "%U static-mapping-index %llu",
2633 format_static_mapping_key, &k, v->value);
2639 format_user_kvp (u8 * s, va_list * args)
2641 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2646 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2647 k.fib_index, v->value);
2653 format_ed_session_kvp (u8 * s, va_list * args)
2655 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2658 k.as_u64[0] = v->key[0];
2659 k.as_u64[1] = v->key[1];
2661 s = format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2662 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2663 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2664 format_ip_protocol, k.proto, k.fib_index, v->value);
2670 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2672 snat_main_t *sm = &snat_main;
2673 u32 next_worker_index = 0;
2676 next_worker_index = sm->first_worker_index;
2677 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2678 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2680 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2681 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2683 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2685 return next_worker_index;
2689 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2691 snat_main_t *sm = &snat_main;
2694 snat_session_key_t m_key;
2695 clib_bihash_kv_8_8_t kv, value;
2696 snat_static_mapping_t *m;
2698 u32 next_worker_index = 0;
2700 /* first try static mappings without port */
2701 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2703 m_key.addr = ip0->dst_address;
2706 m_key.fib_index = rx_fib_index0;
2707 kv.key = m_key.as_u64;
2708 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2710 m = pool_elt_at_index (sm->static_mappings, value.value);
2711 return m->workers[0];
2715 proto = ip_proto_to_snat_proto (ip0->protocol);
2716 udp = ip4_next_header (ip0);
2717 port = udp->dst_port;
2719 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2721 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2722 return vlib_get_thread_index ();
2724 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2726 nat_reass_ip4_t *reass;
2728 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2729 ip0->fragment_id, ip0->protocol);
2731 if (reass && (reass->thread_index != (u32) ~ 0))
2732 return reass->thread_index;
2734 return vlib_get_thread_index ();
2738 /* unknown protocol */
2739 if (PREDICT_FALSE (proto == ~0))
2741 /* use current thread */
2742 return vlib_get_thread_index ();
2745 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2747 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2748 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2749 if (!icmp_is_error_message (icmp))
2750 port = echo->identifier;
2753 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2754 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2755 void *l4_header = ip4_next_header (inner_ip);
2758 case SNAT_PROTOCOL_ICMP:
2759 icmp = (icmp46_header_t*)l4_header;
2760 echo = (icmp_echo_header_t *)(icmp + 1);
2761 port = echo->identifier;
2763 case SNAT_PROTOCOL_UDP:
2764 case SNAT_PROTOCOL_TCP:
2765 port = ((tcp_udp_header_t*)l4_header)->src_port;
2768 return vlib_get_thread_index ();
2773 /* try static mappings with port */
2774 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2776 m_key.addr = ip0->dst_address;
2777 m_key.port = clib_net_to_host_u16 (port);
2778 m_key.protocol = proto;
2779 m_key.fib_index = rx_fib_index0;
2780 kv.key = m_key.as_u64;
2781 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2783 m = pool_elt_at_index (sm->static_mappings, value.value);
2784 return m->workers[0];
2788 /* worker by outside port */
2789 next_worker_index = sm->first_worker_index;
2790 next_worker_index +=
2791 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2792 return next_worker_index;
2796 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index)
2798 snat_main_t *sm = &snat_main;
2799 clib_bihash_kv_8_8_t kv, value;
2800 u32 proto, next_worker_index = 0;
2803 snat_static_mapping_t *m;
2806 /* first try static mappings without port */
2807 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2809 make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
2810 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2812 m = pool_elt_at_index (sm->static_mappings, value.value);
2813 return m->workers[0];
2817 proto = ip_proto_to_snat_proto (ip->protocol);
2819 /* unknown protocol */
2820 if (PREDICT_FALSE (proto == ~0))
2822 /* use current thread */
2823 return vlib_get_thread_index ();
2826 udp = ip4_next_header (ip);
2827 port = udp->dst_port;
2829 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2831 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2832 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2833 if (!icmp_is_error_message (icmp))
2834 port = echo->identifier;
2837 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2838 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2839 void *l4_header = ip4_next_header (inner_ip);
2842 case SNAT_PROTOCOL_ICMP:
2843 icmp = (icmp46_header_t*)l4_header;
2844 echo = (icmp_echo_header_t *)(icmp + 1);
2845 port = echo->identifier;
2847 case SNAT_PROTOCOL_UDP:
2848 case SNAT_PROTOCOL_TCP:
2849 port = ((tcp_udp_header_t*)l4_header)->src_port;
2852 return vlib_get_thread_index ();
2857 /* try static mappings with port */
2858 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2860 make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index,
2861 clib_net_to_host_u16 (port));
2862 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2864 m = pool_elt_at_index (sm->static_mappings, value.value);
2865 if (!vec_len(m->locals))
2866 return m->workers[0];
2868 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2869 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >>24);
2871 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2872 return m->workers[hash & (_vec_len (m->workers) - 1)];
2874 return m->workers[hash % _vec_len (m->workers)];
2878 /* worker by outside port */
2879 next_worker_index = sm->first_worker_index;
2880 next_worker_index +=
2881 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2883 return next_worker_index;
2886 static clib_error_t *
2887 snat_config (vlib_main_t * vm, unformat_input_t * input)
2889 snat_main_t * sm = &snat_main;
2890 nat66_main_t * nm = &nat66_main;
2891 u32 translation_buckets = 1024;
2892 u32 translation_memory_size = 128<<20;
2893 u32 user_buckets = 128;
2894 u32 user_memory_size = 64<<20;
2895 u32 max_translations_per_user = 100;
2896 u32 outside_vrf_id = 0;
2897 u32 outside_ip6_vrf_id = 0;
2898 u32 inside_vrf_id = 0;
2899 u32 static_mapping_buckets = 1024;
2900 u32 static_mapping_memory_size = 64<<20;
2901 u32 nat64_bib_buckets = 1024;
2902 u32 nat64_bib_memory_size = 128 << 20;
2903 u32 nat64_st_buckets = 2048;
2904 u32 nat64_st_memory_size = 256 << 20;
2905 u8 static_mapping_only = 0;
2906 u8 static_mapping_connection_tracking = 0;
2907 snat_main_per_thread_data_t *tsm;
2908 dslite_main_t * dm = &dslite_main;
2910 sm->deterministic = 0;
2912 sm->endpoint_dependent = 0;
2914 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2916 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2918 else if (unformat (input, "translation hash memory %d",
2919 &translation_memory_size));
2920 else if (unformat (input, "user hash buckets %d", &user_buckets))
2922 else if (unformat (input, "user hash memory %d",
2925 else if (unformat (input, "max translations per user %d",
2926 &max_translations_per_user))
2928 else if (unformat (input, "outside VRF id %d",
2931 else if (unformat (input, "outside ip6 VRF id %d",
2932 &outside_ip6_vrf_id))
2934 else if (unformat (input, "inside VRF id %d",
2937 else if (unformat (input, "static mapping only"))
2939 static_mapping_only = 1;
2940 if (unformat (input, "connection tracking"))
2941 static_mapping_connection_tracking = 1;
2943 else if (unformat (input, "deterministic"))
2944 sm->deterministic = 1;
2945 else if (unformat (input, "nat64 bib hash buckets %d",
2946 &nat64_bib_buckets))
2948 else if (unformat (input, "nat64 bib hash memory %d",
2949 &nat64_bib_memory_size))
2951 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2953 else if (unformat (input, "nat64 st hash memory %d",
2954 &nat64_st_memory_size))
2956 else if (unformat (input, "out2in dpo"))
2958 else if (unformat (input, "dslite ce"))
2959 dslite_set_ce(dm, 1);
2960 else if (unformat (input, "endpoint-dependent"))
2961 sm->endpoint_dependent = 1;
2963 return clib_error_return (0, "unknown input '%U'",
2964 format_unformat_error, input);
2967 if (sm->deterministic && sm->endpoint_dependent)
2968 return clib_error_return (
2969 0, "deterministic and endpoint-dependent modes are mutually exclusive");
2971 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
2972 return clib_error_return (
2973 0, "static mapping only mode available only for simple nat");
2975 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
2976 return clib_error_return (
2977 0, "out2in dpo mode available only for simple nat");
2979 /* for show commands, etc. */
2980 sm->translation_buckets = translation_buckets;
2981 sm->translation_memory_size = translation_memory_size;
2982 /* do not exceed load factor 10 */
2983 sm->max_translations = 10 * translation_buckets;
2984 sm->user_buckets = user_buckets;
2985 sm->user_memory_size = user_memory_size;
2986 sm->max_translations_per_user = max_translations_per_user;
2987 sm->outside_vrf_id = outside_vrf_id;
2988 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2990 FIB_SOURCE_PLUGIN_HI);
2991 nm->outside_vrf_id = outside_ip6_vrf_id;
2992 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2994 FIB_SOURCE_PLUGIN_HI);
2995 sm->inside_vrf_id = inside_vrf_id;
2996 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2998 FIB_SOURCE_PLUGIN_HI);
2999 sm->static_mapping_only = static_mapping_only;
3000 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
3002 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
3003 nat64_st_memory_size);
3005 if (sm->deterministic)
3007 sm->in2out_node_index = snat_det_in2out_node.index;
3008 sm->in2out_output_node_index = ~0;
3009 sm->out2in_node_index = snat_det_out2in_node.index;
3010 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
3011 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
3015 if (sm->endpoint_dependent)
3017 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3018 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
3019 sm->in2out_node_index = nat44_ed_in2out_node.index;
3020 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
3021 sm->out2in_node_index = nat44_ed_out2in_node.index;
3022 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
3023 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
3024 nat_affinity_init (vm);
3028 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3029 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
3030 sm->in2out_node_index = snat_in2out_node.index;
3031 sm->in2out_output_node_index = snat_in2out_output_node.index;
3032 sm->out2in_node_index = snat_out2in_node.index;
3033 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
3034 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
3036 if (!static_mapping_only ||
3037 (static_mapping_only && static_mapping_connection_tracking))
3039 vec_foreach (tsm, sm->per_thread_data)
3041 if (sm->endpoint_dependent)
3043 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
3044 translation_buckets,
3045 translation_memory_size);
3046 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
3047 format_ed_session_kvp);
3049 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
3050 translation_buckets,
3051 translation_memory_size);
3052 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
3053 format_ed_session_kvp);
3057 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
3058 translation_buckets,
3059 translation_memory_size);
3060 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
3061 format_session_kvp);
3063 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
3064 translation_buckets,
3065 translation_memory_size);
3066 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
3067 format_session_kvp);
3070 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
3072 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
3079 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
3080 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
3082 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
3083 "static_mapping_by_local", static_mapping_buckets,
3084 static_mapping_memory_size);
3085 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
3086 format_static_mapping_kvp);
3088 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
3089 "static_mapping_by_external", static_mapping_buckets,
3090 static_mapping_memory_size);
3091 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
3092 format_static_mapping_kvp);
3098 VLIB_CONFIG_FUNCTION (snat_config, "nat");
3100 u8 * format_snat_session_state (u8 * s, va_list * args)
3102 u32 i = va_arg (*args, u32);
3107 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
3108 foreach_snat_session_state
3111 t = format (t, "unknown");
3113 s = format (s, "%s", t);
3117 u8 * format_snat_key (u8 * s, va_list * args)
3119 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
3121 s = format (s, "%U proto %U port %d fib %d",
3122 format_ip4_address, &key->addr,
3123 format_snat_protocol, key->protocol,
3124 clib_net_to_host_u16 (key->port), key->fib_index);
3128 u8 * format_static_mapping_key (u8 * s, va_list * args)
3130 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
3132 s = format (s, "%U proto %U port %d fib %d",
3133 format_ip4_address, &key->addr,
3134 format_snat_protocol, key->protocol,
3135 key->port, key->fib_index);
3139 u8 * format_snat_session (u8 * s, va_list * args)
3141 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
3142 snat_session_t * sess = va_arg (*args, snat_session_t *);
3144 if (snat_is_unk_proto_session (sess))
3146 s = format (s, " i2o %U proto %u fib %u\n",
3147 format_ip4_address, &sess->in2out.addr,
3148 clib_net_to_host_u16 (sess->in2out.port),
3149 sess->in2out.fib_index);
3150 s = format (s, " o2i %U proto %u fib %u\n",
3151 format_ip4_address, &sess->out2in.addr,
3152 clib_net_to_host_u16 (sess->out2in.port),
3153 sess->out2in.fib_index);
3157 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
3158 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
3160 if (is_ed_session (sess) || is_fwd_bypass_session (sess))
3162 if (is_twice_nat_session (sess))
3164 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
3165 format_ip4_address, &sess->ext_host_addr,
3166 clib_net_to_host_u16 (sess->ext_host_port),
3167 format_ip4_address, &sess->ext_host_nat_addr,
3168 clib_net_to_host_u16 (sess->ext_host_nat_port));
3172 if (sess->ext_host_addr.as_u32)
3173 s = format (s, " external host %U:%u\n",
3174 format_ip4_address, &sess->ext_host_addr,
3175 clib_net_to_host_u16 (sess->ext_host_port));
3178 s = format (s, " index %llu\n", sess - sm->sessions);
3179 s = format (s, " last heard %.2f\n", sess->last_heard);
3180 s = format (s, " total pkts %d, total bytes %lld\n",
3181 sess->total_pkts, sess->total_bytes);
3182 if (snat_is_session_static (sess))
3183 s = format (s, " static translation\n");
3185 s = format (s, " dynamic translation\n");
3186 if (is_fwd_bypass_session (sess))
3187 s = format (s, " forwarding-bypass\n");
3188 if (is_lb_session (sess))
3189 s = format (s, " load-balancing\n");
3190 if (is_twice_nat_session (sess))
3191 s = format (s, " twice-nat\n");
3196 u8 * format_snat_user (u8 * s, va_list * args)
3198 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
3199 snat_user_t * u = va_arg (*args, snat_user_t *);
3200 int verbose = va_arg (*args, int);
3201 dlist_elt_t * head, * elt;
3202 u32 elt_index, head_index;
3204 snat_session_t * sess;
3206 s = format (s, "%U: %d dynamic translations, %d static translations\n",
3207 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
3212 if (u->nsessions || u->nstaticsessions)
3214 head_index = u->sessions_per_user_list_head_index;
3215 head = pool_elt_at_index (sm->list_pool, head_index);
3217 elt_index = head->next;
3218 elt = pool_elt_at_index (sm->list_pool, elt_index);
3219 session_index = elt->value;
3221 while (session_index != ~0)
3223 sess = pool_elt_at_index (sm->sessions, session_index);
3225 s = format (s, " %U\n", format_snat_session, sm, sess);
3227 elt_index = elt->next;
3228 elt = pool_elt_at_index (sm->list_pool, elt_index);
3229 session_index = elt->value;
3236 u8 * format_snat_static_mapping (u8 * s, va_list * args)
3238 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
3239 nat44_lb_addr_port_t *local;
3242 s = format (s, "local %U external %U vrf %d %s %s",
3243 format_ip4_address, &m->local_addr,
3244 format_ip4_address, &m->external_addr,
3246 m->twice_nat == TWICE_NAT ? "twice-nat" :
3247 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3248 m->out2in_only ? "out2in-only" : "");
3251 if (vec_len (m->locals))
3253 s = format (s, "%U external %U:%d %s %s",
3254 format_snat_protocol, m->proto,
3255 format_ip4_address, &m->external_addr, m->external_port,
3256 m->twice_nat == TWICE_NAT ? "twice-nat" :
3257 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3258 m->out2in_only ? "out2in-only" : "");
3259 vec_foreach (local, m->locals)
3260 s = format (s, "\n local %U:%d vrf %d probability %d\%",
3261 format_ip4_address, &local->addr, local->port,
3262 local->vrf_id, local->probability);
3265 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
3266 format_snat_protocol, m->proto,
3267 format_ip4_address, &m->local_addr, m->local_port,
3268 format_ip4_address, &m->external_addr, m->external_port,
3270 m->twice_nat == TWICE_NAT ? "twice-nat" :
3271 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3272 m->out2in_only ? "out2in-only" : "");
3277 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
3279 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
3280 vnet_main_t *vnm = vnet_get_main();
3283 s = format (s, "local %U external %U vrf %d",
3284 format_ip4_address, &m->l_addr,
3285 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
3288 s = format (s, "%U local %U:%d external %U:%d vrf %d",
3289 format_snat_protocol, m->proto,
3290 format_ip4_address, &m->l_addr, m->l_port,
3291 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
3292 m->e_port, m->vrf_id);
3297 u8 * format_det_map_ses (u8 * s, va_list * args)
3299 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
3300 ip4_address_t in_addr, out_addr;
3301 u32 in_offset, out_offset;
3302 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
3303 u32 * i = va_arg (*args, u32 *);
3305 u32 user_index = *i / SNAT_DET_SES_PER_USER;
3306 in_addr.as_u32 = clib_host_to_net_u32 (
3307 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
3308 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
3309 clib_net_to_host_u32(det_map->in_addr.as_u32);
3310 out_offset = in_offset / det_map->sharing_ratio;
3311 out_addr.as_u32 = clib_host_to_net_u32(
3312 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
3313 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
3314 format_ip4_address, &in_addr,
3315 clib_net_to_host_u16 (ses->in_port),
3316 format_ip4_address, &out_addr,
3317 clib_net_to_host_u16 (ses->out.out_port),
3318 format_ip4_address, &ses->out.ext_host_addr,
3319 clib_net_to_host_u16 (ses->out.ext_host_port),
3320 format_snat_session_state, ses->state,
3327 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3330 ip4_address_t * address,
3332 u32 if_address_index,
3335 snat_main_t *sm = &snat_main;
3336 snat_static_map_resolve_t *rp;
3337 snat_static_mapping_t *m;
3338 snat_session_key_t m_key;
3339 clib_bihash_kv_8_8_t kv, value;
3341 ip4_address_t l_addr;
3343 for (i = 0; i < vec_len (sm->to_resolve); i++)
3345 rp = sm->to_resolve + i;
3346 if (rp->addr_only == 0)
3348 if (rp->sw_if_index == sw_if_index)
3355 m_key.addr.as_u32 = address->as_u32;
3356 m_key.port = rp->addr_only ? 0 : rp->e_port;
3357 m_key.protocol = rp->addr_only ? 0 : rp->proto;
3358 m_key.fib_index = sm->outside_fib_index;
3359 kv.key = m_key.as_u64;
3360 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3363 m = pool_elt_at_index (sm->static_mappings, value.value);
3367 /* Don't trip over lease renewal, static config */
3377 /* Indetity mapping? */
3378 if (rp->l_addr.as_u32 == 0)
3379 l_addr.as_u32 = address[0].as_u32;
3381 l_addr.as_u32 = rp->l_addr.as_u32;
3382 /* Add the static mapping */
3383 rv = snat_add_static_mapping (l_addr,
3389 ~0 /* sw_if_index */,
3394 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3398 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3401 ip4_address_t * address,
3403 u32 if_address_index,
3406 snat_main_t *sm = &snat_main;
3407 snat_static_map_resolve_t *rp;
3408 ip4_address_t l_addr;
3412 snat_address_t *addresses = sm->addresses;
3414 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3416 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3420 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
3423 addresses = sm->twice_nat_addresses;
3424 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3433 /* Don't trip over lease renewal, static config */
3434 for (j = 0; j < vec_len(addresses); j++)
3435 if (addresses[j].addr.as_u32 == address->as_u32)
3438 (void) snat_add_address (sm, address, ~0, twice_nat);
3439 /* Scan static map resolution vector */
3440 for (j = 0; j < vec_len (sm->to_resolve); j++)
3442 rp = sm->to_resolve + j;
3445 /* On this interface? */
3446 if (rp->sw_if_index == sw_if_index)
3448 /* Indetity mapping? */
3449 if (rp->l_addr.as_u32 == 0)
3450 l_addr.as_u32 = address[0].as_u32;
3452 l_addr.as_u32 = rp->l_addr.as_u32;
3453 /* Add the static mapping */
3454 rv = snat_add_static_mapping (l_addr,
3460 ~0 /* sw_if_index */,
3465 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3472 (void) snat_del_address(sm, address[0], 1, twice_nat);
3478 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
3481 ip4_main_t * ip4_main = sm->ip4_main;
3482 ip4_address_t * first_int_addr;
3483 snat_static_map_resolve_t *rp;
3484 u32 *indices_to_delete = 0;
3486 u32 *auto_add_sw_if_indices =
3487 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3489 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3490 0 /* just want the address*/);
3492 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
3494 if (auto_add_sw_if_indices[i] == sw_if_index)
3498 /* if have address remove it */
3500 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3503 for (j = 0; j < vec_len (sm->to_resolve); j++)
3505 rp = sm->to_resolve + j;
3506 if (rp->sw_if_index == sw_if_index)
3507 vec_add1 (indices_to_delete, j);
3509 if (vec_len(indices_to_delete))
3511 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3512 vec_del1(sm->to_resolve, j);
3513 vec_free(indices_to_delete);
3517 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
3519 vec_del1(sm->auto_add_sw_if_indices, i);
3522 return VNET_API_ERROR_VALUE_EXIST;
3529 return VNET_API_ERROR_NO_SUCH_ENTRY;
3531 /* add to the auto-address list */
3533 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3535 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3537 /* If the address is already bound - or static - add it now */
3539 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3545 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3546 snat_protocol_t proto, u32 vrf_id, int is_in)
3548 snat_main_per_thread_data_t *tsm;
3549 clib_bihash_kv_8_8_t kv, value;
3551 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3552 snat_session_key_t key;
3554 clib_bihash_8_8_t *t;
3556 if (sm->endpoint_dependent)
3557 return VNET_API_ERROR_UNSUPPORTED;
3559 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3560 if (sm->num_workers > 1)
3562 vec_elt_at_index (sm->per_thread_data,
3563 sm->worker_in2out_cb (&ip, fib_index));
3565 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3567 key.addr.as_u32 = addr->as_u32;
3568 key.port = clib_host_to_net_u16 (port);
3569 key.protocol = proto;
3570 key.fib_index = fib_index;
3571 kv.key = key.as_u64;
3572 t = is_in ? &tsm->in2out : &tsm->out2in;
3573 if (!clib_bihash_search_8_8 (t, &kv, &value))
3575 if (pool_is_free_index (tsm->sessions, value.value))
3576 return VNET_API_ERROR_UNSPECIFIED;
3578 s = pool_elt_at_index (tsm->sessions, value.value);
3579 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3580 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3584 return VNET_API_ERROR_NO_SUCH_ENTRY;
3588 nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3589 ip4_address_t *eh_addr, u16 eh_port, u8 proto,
3590 u32 vrf_id, int is_in)
3593 clib_bihash_16_8_t *t;
3594 nat_ed_ses_key_t key;
3595 clib_bihash_kv_16_8_t kv, value;
3596 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3598 snat_main_per_thread_data_t *tsm;
3600 if (!sm->endpoint_dependent)
3601 return VNET_API_ERROR_FEATURE_DISABLED;
3603 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3604 if (sm->num_workers > 1)
3606 vec_elt_at_index (sm->per_thread_data,
3607 sm->worker_in2out_cb (&ip, fib_index));
3609 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3611 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
3612 key.l_addr.as_u32 = addr->as_u32;
3613 key.r_addr.as_u32 = eh_addr->as_u32;
3614 key.l_port = clib_host_to_net_u16 (port);
3615 key.r_port = clib_host_to_net_u16 (eh_port);
3617 key.fib_index = clib_host_to_net_u32 (fib_index);
3618 kv.key[0] = key.as_u64[0];
3619 kv.key[1] = key.as_u64[1];
3620 if (clib_bihash_search_16_8 (t, &kv, &value))
3621 return VNET_API_ERROR_NO_SUCH_ENTRY;
3623 if (pool_is_free_index (tsm->sessions, value.value))
3624 return VNET_API_ERROR_UNSPECIFIED;
3625 s = pool_elt_at_index (tsm->sessions, value.value);
3626 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3627 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3632 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3634 snat_main_t *sm = &snat_main;
3636 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
3637 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3639 sm->psid_offset = psid_offset;
3640 sm->psid_length = psid_length;
3644 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
3646 snat_main_t *sm = &snat_main;
3648 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
3649 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
3650 sm->start_port = start_port;
3651 sm->end_port = end_port;
3655 nat_set_alloc_addr_and_port_default (void)
3657 snat_main_t *sm = &snat_main;
3659 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
3660 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;