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 <vnet/fib/fib_table.h>
32 #include <vnet/fib/ip4_fib.h>
34 #include <vpp/app/version.h>
36 snat_main_t snat_main;
39 /* Hook up input features */
40 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
41 .arc_name = "ip4-unicast",
42 .node_name = "nat44-in2out",
43 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
45 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "nat44-out2in",
48 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
49 "ip4-dhcp-client-detect"),
51 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
52 .arc_name = "ip4-unicast",
53 .node_name = "nat44-classify",
54 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
56 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
57 .arc_name = "ip4-unicast",
58 .node_name = "nat44-det-in2out",
59 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
61 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
62 .arc_name = "ip4-unicast",
63 .node_name = "nat44-det-out2in",
64 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
65 "ip4-dhcp-client-detect"),
67 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
68 .arc_name = "ip4-unicast",
69 .node_name = "nat44-det-classify",
70 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
72 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
73 .arc_name = "ip4-unicast",
74 .node_name = "nat44-ed-in2out",
75 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
77 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
78 .arc_name = "ip4-unicast",
79 .node_name = "nat44-ed-out2in",
80 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
81 "ip4-dhcp-client-detect"),
83 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
84 .arc_name = "ip4-unicast",
85 .node_name = "nat44-ed-classify",
86 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
88 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
89 .arc_name = "ip4-unicast",
90 .node_name = "nat44-in2out-worker-handoff",
91 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
93 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
94 .arc_name = "ip4-unicast",
95 .node_name = "nat44-out2in-worker-handoff",
96 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
97 "ip4-dhcp-client-detect"),
99 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
100 .arc_name = "ip4-unicast",
101 .node_name = "nat44-handoff-classify",
102 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
104 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
105 .arc_name = "ip4-unicast",
106 .node_name = "nat44-in2out-fast",
107 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
109 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
110 .arc_name = "ip4-unicast",
111 .node_name = "nat44-out2in-fast",
112 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
113 "ip4-dhcp-client-detect"),
115 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
116 .arc_name = "ip4-unicast",
117 .node_name = "nat44-hairpin-dst",
118 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
120 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
121 .arc_name = "ip4-unicast",
122 .node_name = "nat44-ed-hairpin-dst",
123 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
126 /* Hook up output features */
127 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
128 .arc_name = "ip4-output",
129 .node_name = "nat44-in2out-output",
130 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
132 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
133 .arc_name = "ip4-output",
134 .node_name = "nat44-in2out-output-worker-handoff",
135 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
137 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
138 .arc_name = "ip4-output",
139 .node_name = "nat44-hairpin-src",
140 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
142 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
143 .arc_name = "ip4-output",
144 .node_name = "nat44-ed-in2out-output",
145 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
147 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
148 .arc_name = "ip4-output",
149 .node_name = "nat44-ed-hairpin-src",
150 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
153 /* Hook up ip4-local features */
154 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
156 .arc_name = "ip4-local",
157 .node_name = "nat44-hairpinning",
158 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
160 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
162 .arc_name = "ip4-local",
163 .node_name = "nat44-ed-hairpinning",
164 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
169 VLIB_PLUGIN_REGISTER () = {
170 .version = VPP_BUILD_VER,
171 .description = "Network Address Translation",
175 vlib_node_registration_t nat44_classify_node;
176 vlib_node_registration_t nat44_ed_classify_node;
177 vlib_node_registration_t nat44_det_classify_node;
178 vlib_node_registration_t nat44_handoff_classify_node;
181 NAT44_CLASSIFY_NEXT_IN2OUT,
182 NAT44_CLASSIFY_NEXT_OUT2IN,
183 NAT44_CLASSIFY_N_NEXT,
184 } nat44_classify_next_t;
187 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
189 snat_session_key_t key;
190 clib_bihash_kv_8_8_t kv;
191 nat_ed_ses_key_t ed_key;
192 clib_bihash_kv_16_8_t ed_kv;
195 snat_main_per_thread_data_t *tsm =
196 vec_elt_at_index (sm->per_thread_data, thread_index);
198 if (is_fwd_bypass_session (s))
200 ed_key.l_addr = s->in2out.addr;
201 ed_key.r_addr = s->ext_host_addr;
202 ed_key.l_port = s->in2out.port;
203 ed_key.r_port = s->ext_host_port;
204 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
205 ed_key.fib_index = 0;
206 ed_kv.key[0] = ed_key.as_u64[0];
207 ed_kv.key[1] = ed_key.as_u64[1];
208 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
209 nat_log_warn ("in2out_ed key del failed");
213 /* session lookup tables */
214 if (is_ed_session (s))
216 ed_key.l_addr = s->out2in.addr;
217 ed_key.r_addr = s->ext_host_addr;
218 ed_key.fib_index = s->out2in.fib_index;
219 if (snat_is_unk_proto_session (s))
221 ed_key.proto = s->in2out.port;
227 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
228 ed_key.l_port = s->out2in.port;
229 ed_key.r_port = s->ext_host_port;
231 ed_kv.key[0] = ed_key.as_u64[0];
232 ed_kv.key[1] = ed_key.as_u64[1];
233 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
234 nat_log_warn ("out2in_ed key del failed");
236 ed_key.l_addr = s->in2out.addr;
237 ed_key.fib_index = s->in2out.fib_index;
238 if (!snat_is_unk_proto_session (s))
239 ed_key.l_port = s->in2out.port;
240 if (is_twice_nat_session (s))
242 ed_key.r_addr = s->ext_host_nat_addr;
243 ed_key.r_port = s->ext_host_nat_port;
245 ed_kv.key[0] = ed_key.as_u64[0];
246 ed_kv.key[1] = ed_key.as_u64[1];
247 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
248 nat_log_warn ("in2out_ed key del failed");
252 kv.key = s->in2out.as_u64;
253 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
254 nat_log_warn ("in2out key del failed");
255 kv.key = s->out2in.as_u64;
256 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
257 nat_log_warn ("out2in key del failed");
260 if (snat_is_unk_proto_session (s))
264 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
265 s->out2in.addr.as_u32,
269 s->in2out.fib_index);
271 /* Twice NAT address and port for external host */
272 if (is_twice_nat_session (s))
274 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
276 key.protocol = s->in2out.protocol;
277 key.port = s->ext_host_nat_port;
278 a = sm->twice_nat_addresses + i;
279 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
281 snat_free_outside_address_and_port (sm->twice_nat_addresses,
282 thread_index, &key, i);
288 if (snat_is_session_static (s))
291 if (s->outside_address_index != ~0)
292 snat_free_outside_address_and_port (sm->addresses, thread_index,
293 &s->out2in, s->outside_address_index);
297 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
301 snat_user_key_t user_key;
302 clib_bihash_kv_8_8_t kv, value;
303 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
304 dlist_elt_t * per_user_list_head_elt;
306 user_key.addr.as_u32 = addr->as_u32;
307 user_key.fib_index = fib_index;
308 kv.key = user_key.as_u64;
310 /* Ever heard of the "user" = src ip4 address before? */
311 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
313 /* no, make a new one */
314 pool_get (tsm->users, u);
315 memset (u, 0, sizeof (*u));
316 u->addr.as_u32 = addr->as_u32;
317 u->fib_index = fib_index;
319 pool_get (tsm->list_pool, per_user_list_head_elt);
321 u->sessions_per_user_list_head_index = per_user_list_head_elt -
324 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
326 kv.value = u - tsm->users;
329 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
330 nat_log_warn ("user_hash keay add failed");
334 u = pool_elt_at_index (tsm->users, value.value);
341 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
344 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
345 u32 oldest_per_user_translation_list_index, session_index;
346 dlist_elt_t * oldest_per_user_translation_list_elt;
347 dlist_elt_t * per_user_translation_list_elt;
349 /* Over quota? Recycle the least recently used translation */
350 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
352 oldest_per_user_translation_list_index =
353 clib_dlist_remove_head (tsm->list_pool,
354 u->sessions_per_user_list_head_index);
356 ASSERT (oldest_per_user_translation_list_index != ~0);
358 /* Add it back to the end of the LRU list */
359 clib_dlist_addtail (tsm->list_pool,
360 u->sessions_per_user_list_head_index,
361 oldest_per_user_translation_list_index);
362 /* Get the list element */
363 oldest_per_user_translation_list_elt =
364 pool_elt_at_index (tsm->list_pool,
365 oldest_per_user_translation_list_index);
367 /* Get the session index from the list element */
368 session_index = oldest_per_user_translation_list_elt->value;
370 /* Get the session */
371 s = pool_elt_at_index (tsm->sessions, session_index);
372 nat_free_session_data (sm, s, thread_index);
373 if (snat_is_session_static(s))
374 u->nstaticsessions--;
377 s->outside_address_index = ~0;
382 s->ext_host_addr.as_u32 = 0;
383 s->ext_host_port = 0;
384 s->ext_host_nat_addr.as_u32 = 0;
385 s->ext_host_nat_port = 0;
389 pool_get (tsm->sessions, s);
390 memset (s, 0, sizeof (*s));
391 s->outside_address_index = ~0;
393 /* Create list elts */
394 pool_get (tsm->list_pool, per_user_translation_list_elt);
395 clib_dlist_init (tsm->list_pool,
396 per_user_translation_list_elt - tsm->list_pool);
398 per_user_translation_list_elt->value = s - tsm->sessions;
399 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
400 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
402 clib_dlist_addtail (tsm->list_pool,
403 s->per_user_list_head_index,
404 per_user_translation_list_elt - tsm->list_pool);
412 } nat44_classify_trace_t;
414 static u8 * format_nat44_classify_trace (u8 * s, va_list * args)
416 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
417 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
418 nat44_classify_trace_t *t = va_arg (*args, nat44_classify_trace_t *);
421 next = t->next_in2out ? "nat44-in2out" : "nat44-out2in";
423 s = format (s, "nat44-classify: next %s", next);
429 nat44_classify_node_fn_inline (vlib_main_t * vm,
430 vlib_node_runtime_t * node,
431 vlib_frame_t * frame,
434 u32 n_left_from, * from, * to_next;
435 nat44_classify_next_t next_index;
436 snat_main_t *sm = &snat_main;
437 snat_static_mapping_t *m;
438 u32 thread_index = vm->thread_index;
439 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
441 from = vlib_frame_vector_args (frame);
442 n_left_from = frame->n_vectors;
443 next_index = node->cached_next_index;
445 while (n_left_from > 0)
449 vlib_get_next_frame (vm, node, next_index,
450 to_next, n_left_to_next);
452 while (n_left_from > 0 && n_left_to_next > 0)
456 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT, sw_if_index0, rx_fib_index0;
459 snat_session_key_t m_key0;
460 clib_bihash_kv_8_8_t kv0, value0;
461 clib_bihash_kv_16_8_t ed_kv0, ed_value0;
464 /* speculatively enqueue b0 to the current next frame */
472 b0 = vlib_get_buffer (vm, bi0);
473 ip0 = vlib_buffer_get_current (b0);
474 udp0 = ip4_next_header (ip0);
478 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
480 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
482 make_ed_kv (&ed_kv0, &ip0->src_address, &ip0->dst_address,
483 ip0->protocol, rx_fib_index0, udp0->src_port,
485 if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &ed_kv0, &ed_value0))
489 vec_foreach (ap, sm->addresses)
491 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
493 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
498 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
500 m_key0.addr = ip0->dst_address;
503 m_key0.fib_index = 0;
504 kv0.key = m_key0.as_u64;
505 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
507 m = pool_elt_at_index (sm->static_mappings, value0.value);
508 if (m->local_addr.as_u32 != m->external_addr.as_u32)
509 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
512 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
513 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
514 kv0.key = m_key0.as_u64;
515 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
517 m = pool_elt_at_index (sm->static_mappings, value0.value);
518 if (m->local_addr.as_u32 != m->external_addr.as_u32)
519 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
524 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
525 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
527 nat44_classify_trace_t *t =
528 vlib_add_trace (vm, node, b0, sizeof (*t));
529 t->next_in2out = next0 == NAT44_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
532 /* verify speculative enqueue, maybe switch current next frame */
533 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
534 to_next, n_left_to_next,
538 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
541 return frame->n_vectors;
545 nat44_classify_node_fn (vlib_main_t * vm,
546 vlib_node_runtime_t * node,
547 vlib_frame_t * frame)
549 return nat44_classify_node_fn_inline (vm, node, frame, 0);
552 VLIB_REGISTER_NODE (nat44_classify_node) = {
553 .function = nat44_classify_node_fn,
554 .name = "nat44-classify",
555 .vector_size = sizeof (u32),
556 .format_trace = format_nat44_classify_trace,
557 .type = VLIB_NODE_TYPE_INTERNAL,
558 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
560 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
561 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
565 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
566 nat44_classify_node_fn);
568 nat44_ed_classify_node_fn (vlib_main_t * vm,
569 vlib_node_runtime_t * node,
570 vlib_frame_t * frame)
572 return nat44_classify_node_fn_inline (vm, node, frame, 1);
575 VLIB_REGISTER_NODE (nat44_ed_classify_node) = {
576 .function = nat44_ed_classify_node_fn,
577 .name = "nat44-ed-classify",
578 .vector_size = sizeof (u32),
579 .format_trace = format_nat44_classify_trace,
580 .type = VLIB_NODE_TYPE_INTERNAL,
581 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
583 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-ed-in2out",
584 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-ed-out2in",
588 VLIB_NODE_FUNCTION_MULTIARCH (nat44_ed_classify_node,
589 nat44_ed_classify_node_fn);
592 nat44_det_classify_node_fn (vlib_main_t * vm,
593 vlib_node_runtime_t * node,
594 vlib_frame_t * frame)
596 return nat44_classify_node_fn_inline (vm, node, frame, 0);
599 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
600 .function = nat44_det_classify_node_fn,
601 .name = "nat44-det-classify",
602 .vector_size = sizeof (u32),
603 .format_trace = format_nat44_classify_trace,
604 .type = VLIB_NODE_TYPE_INTERNAL,
605 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
607 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
608 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
612 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
613 nat44_det_classify_node_fn);
616 nat44_handoff_classify_node_fn (vlib_main_t * vm,
617 vlib_node_runtime_t * node,
618 vlib_frame_t * frame)
620 return nat44_classify_node_fn_inline (vm, node, frame, 0);
623 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
624 .function = nat44_handoff_classify_node_fn,
625 .name = "nat44-handoff-classify",
626 .vector_size = sizeof (u32),
627 .format_trace = format_nat44_classify_trace,
628 .type = VLIB_NODE_TYPE_INTERNAL,
629 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
631 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
632 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
636 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
637 nat44_handoff_classify_node_fn);
640 * @brief Add/del NAT address to FIB.
642 * Add the external NAT address to the FIB as receive entries. This ensures
643 * that VPP will reply to ARP for this address and we don't need to enable
644 * proxy ARP on the outside interface.
646 * @param addr IPv4 address.
647 * @param plen address prefix length
648 * @param sw_if_index Interface.
649 * @param is_add If 0 delete, otherwise add.
652 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
655 fib_prefix_t prefix = {
657 .fp_proto = FIB_PROTOCOL_IP4,
659 .ip4.as_u32 = addr->as_u32,
662 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
665 fib_table_entry_update_one_path(fib_index,
667 FIB_SOURCE_PLUGIN_LOW,
668 (FIB_ENTRY_FLAG_CONNECTED |
669 FIB_ENTRY_FLAG_LOCAL |
670 FIB_ENTRY_FLAG_EXCLUSIVE),
677 FIB_ROUTE_PATH_FLAG_NONE);
679 fib_table_entry_delete(fib_index,
681 FIB_SOURCE_PLUGIN_LOW);
684 int snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
689 vlib_thread_main_t *tm = vlib_get_thread_main ();
691 if (twice_nat && !sm->endpoint_dependent)
692 return VNET_API_ERROR_FEATURE_DISABLED;
694 /* Check if address already exists */
695 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
697 if (ap->addr.as_u32 == addr->as_u32)
698 return VNET_API_ERROR_VALUE_EXIST;
702 vec_add2 (sm->twice_nat_addresses, ap, 1);
704 vec_add2 (sm->addresses, ap, 1);
709 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
710 FIB_SOURCE_PLUGIN_LOW);
713 #define _(N, i, n, s) \
714 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
715 ap->busy_##n##_ports = 0; \
716 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
717 foreach_snat_protocol
723 /* Add external address to FIB */
724 pool_foreach (i, sm->interfaces,
726 if (nat_interface_is_inside(i) || sm->out2in_dpo)
729 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
732 pool_foreach (i, sm->output_feature_interfaces,
734 if (nat_interface_is_inside(i) || sm->out2in_dpo)
737 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
744 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
747 snat_static_mapping_t *m;
748 pool_foreach (m, sm->static_mappings,
750 if (m->external_addr.as_u32 == addr.as_u32)
757 void increment_v4_address (ip4_address_t * a)
761 v = clib_net_to_host_u32(a->as_u32) + 1;
762 a->as_u32 = clib_host_to_net_u32(v);
766 snat_add_static_mapping_when_resolved (snat_main_t * sm,
767 ip4_address_t l_addr,
772 snat_protocol_t proto,
777 snat_static_map_resolve_t *rp;
779 vec_add2 (sm->to_resolve, rp, 1);
780 rp->l_addr.as_u32 = l_addr.as_u32;
782 rp->sw_if_index = sw_if_index;
786 rp->addr_only = addr_only;
788 rp->tag = vec_dup (tag);
792 * @brief Add static mapping.
794 * Create static mapping between local addr+port and external addr+port.
796 * @param l_addr Local IPv4 address.
797 * @param e_addr External IPv4 address.
798 * @param l_port Local port number.
799 * @param e_port External port number.
800 * @param vrf_id VRF ID.
801 * @param addr_only If 0 address port and pair mapping, otherwise address only.
802 * @param sw_if_index External port instead of specific IP address.
803 * @param is_add If 0 delete static mapping, otherwise add.
804 * @param twice_nat If value is TWICE_NAT then translate external host address
806 * If value is TWICE_NAT_SELF then translate external host
807 * address and port whenever external host address equals
808 * local address of internal host.
809 * @param out2in_only If 1 rule match only out2in direction
810 * @param tag - opaque string tag
814 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
815 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
816 u32 sw_if_index, snat_protocol_t proto, int is_add,
817 twice_nat_type_t twice_nat, u8 out2in_only,
820 snat_main_t * sm = &snat_main;
821 snat_static_mapping_t *m;
822 snat_session_key_t m_key;
823 clib_bihash_kv_8_8_t kv, value;
824 snat_address_t *a = 0;
827 snat_interface_t *interface;
829 snat_main_per_thread_data_t *tsm;
830 snat_user_key_t u_key;
832 dlist_elt_t * head, * elt;
833 u32 elt_index, head_index;
837 snat_static_map_resolve_t *rp, *rp_match = 0;
839 if (!sm->endpoint_dependent)
841 if (twice_nat || out2in_only)
842 return VNET_API_ERROR_FEATURE_DISABLED;
845 /* If the external address is a specific interface address */
846 if (sw_if_index != ~0)
848 ip4_address_t * first_int_addr;
850 for (i = 0; i < vec_len (sm->to_resolve); i++)
852 rp = sm->to_resolve + i;
853 if (rp->sw_if_index != sw_if_index ||
854 rp->l_addr.as_u32 != l_addr.as_u32 ||
855 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
860 if (rp->l_port != l_port || rp->e_port != e_port || rp->proto != proto)
868 /* Might be already set... */
869 first_int_addr = ip4_interface_first_address
870 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
875 return VNET_API_ERROR_VALUE_EXIST;
877 snat_add_static_mapping_when_resolved
878 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
879 addr_only, is_add, tag);
881 /* DHCP resolution required? */
882 if (first_int_addr == 0)
888 e_addr.as_u32 = first_int_addr->as_u32;
889 /* Identity mapping? */
890 if (l_addr.as_u32 == 0)
891 l_addr.as_u32 = e_addr.as_u32;
897 return VNET_API_ERROR_NO_SUCH_ENTRY;
899 vec_del1 (sm->to_resolve, i);
903 e_addr.as_u32 = first_int_addr->as_u32;
904 /* Identity mapping? */
905 if (l_addr.as_u32 == 0)
906 l_addr.as_u32 = e_addr.as_u32;
914 m_key.port = addr_only ? 0 : e_port;
915 m_key.protocol = addr_only ? 0 : proto;
917 kv.key = m_key.as_u64;
918 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
921 m = pool_elt_at_index (sm->static_mappings, value.value);
926 return VNET_API_ERROR_VALUE_EXIST;
928 if (twice_nat && addr_only)
929 return VNET_API_ERROR_UNSUPPORTED;
931 /* Convert VRF id to FIB index */
934 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
936 return VNET_API_ERROR_NO_SUCH_FIB;
939 /* If not specified use inside VRF id from SNAT plugin startup config */
942 fib_index = sm->inside_fib_index;
943 vrf_id = sm->inside_vrf_id;
949 m_key.port = addr_only ? 0 : l_port;
950 m_key.protocol = addr_only ? 0 : proto;
951 m_key.fib_index = fib_index;
952 kv.key = m_key.as_u64;
953 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
954 return VNET_API_ERROR_VALUE_EXIST;
957 /* Find external address in allocated addresses and reserve port for
958 address and port pair mapping when dynamic translations enabled */
959 if (!(addr_only || sm->static_mapping_only || out2in_only))
961 for (i = 0; i < vec_len (sm->addresses); i++)
963 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
965 a = sm->addresses + i;
966 /* External port must be unused */
969 #define _(N, j, n, s) \
970 case SNAT_PROTOCOL_##N: \
971 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
972 return VNET_API_ERROR_INVALID_VALUE; \
973 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
976 a->busy_##n##_ports++; \
977 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
980 foreach_snat_protocol
983 nat_log_info ("unknown protocol");
984 return VNET_API_ERROR_INVALID_VALUE_2;
989 /* External address must be allocated */
990 if (!a && (l_addr.as_u32 != e_addr.as_u32))
992 if (sw_if_index != ~0)
994 for (i = 0; i < vec_len (sm->to_resolve); i++)
996 rp = sm->to_resolve + i;
999 if (rp->sw_if_index != sw_if_index &&
1000 rp->l_addr.as_u32 != l_addr.as_u32 &&
1001 rp->vrf_id != vrf_id && rp->l_port != l_port &&
1002 rp->e_port != e_port && rp->proto != proto)
1005 vec_del1 (sm->to_resolve, i);
1009 return VNET_API_ERROR_NO_SUCH_ENTRY;
1013 pool_get (sm->static_mappings, m);
1014 memset (m, 0, sizeof (*m));
1015 m->tag = vec_dup (tag);
1016 m->local_addr = l_addr;
1017 m->external_addr = e_addr;
1018 m->addr_only = addr_only;
1020 m->fib_index = fib_index;
1021 m->twice_nat = twice_nat;
1022 m->out2in_only = out2in_only;
1025 m->local_port = l_port;
1026 m->external_port = e_port;
1030 if (sm->num_workers > 1)
1033 .src_address = m->local_addr,
1035 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index));
1036 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1039 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1041 m_key.addr = m->local_addr;
1042 m_key.port = m->local_port;
1043 m_key.protocol = m->proto;
1044 m_key.fib_index = m->fib_index;
1045 kv.key = m_key.as_u64;
1046 kv.value = m - sm->static_mappings;
1048 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1050 m_key.addr = m->external_addr;
1051 m_key.port = m->external_port;
1052 m_key.fib_index = 0;
1053 kv.key = m_key.as_u64;
1054 kv.value = m - sm->static_mappings;
1055 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
1057 /* Delete dynamic sessions matching local address (+ local port) */
1058 if (!(sm->static_mapping_only))
1060 u_key.addr = m->local_addr;
1061 u_key.fib_index = m->fib_index;
1062 kv.key = u_key.as_u64;
1063 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1065 user_index = value.value;
1066 u = pool_elt_at_index (tsm->users, user_index);
1069 head_index = u->sessions_per_user_list_head_index;
1070 head = pool_elt_at_index (tsm->list_pool, head_index);
1071 elt_index = head->next;
1072 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1073 ses_index = elt->value;
1074 while (ses_index != ~0)
1076 s = pool_elt_at_index (tsm->sessions, ses_index);
1077 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1078 ses_index = elt->value;
1080 if (snat_is_session_static (s))
1083 if (!addr_only && (clib_net_to_host_u16 (s->in2out.port) != m->local_port))
1086 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1087 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1100 if (sw_if_index != ~0)
1103 return VNET_API_ERROR_NO_SUCH_ENTRY;
1106 /* Free external address port */
1107 if (!(addr_only || sm->static_mapping_only || out2in_only))
1109 for (i = 0; i < vec_len (sm->addresses); i++)
1111 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1113 a = sm->addresses + i;
1116 #define _(N, j, n, s) \
1117 case SNAT_PROTOCOL_##N: \
1118 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1119 if (e_port > 1024) \
1121 a->busy_##n##_ports--; \
1122 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1125 foreach_snat_protocol
1128 nat_log_info ("unknown protocol");
1129 return VNET_API_ERROR_INVALID_VALUE_2;
1136 if (sm->num_workers > 1)
1137 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1139 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1141 m_key.addr = m->local_addr;
1142 m_key.port = m->local_port;
1143 m_key.protocol = m->proto;
1144 m_key.fib_index = m->fib_index;
1145 kv.key = m_key.as_u64;
1147 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
1149 m_key.addr = m->external_addr;
1150 m_key.port = m->external_port;
1151 m_key.fib_index = 0;
1152 kv.key = m_key.as_u64;
1153 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
1155 /* Delete session(s) for static mapping if exist */
1156 if (!(sm->static_mapping_only) ||
1157 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1159 u_key.addr = m->local_addr;
1160 u_key.fib_index = m->fib_index;
1161 kv.key = u_key.as_u64;
1162 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1164 user_index = value.value;
1165 u = pool_elt_at_index (tsm->users, user_index);
1166 if (u->nstaticsessions)
1168 head_index = u->sessions_per_user_list_head_index;
1169 head = pool_elt_at_index (tsm->list_pool, head_index);
1170 elt_index = head->next;
1171 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1172 ses_index = elt->value;
1173 while (ses_index != ~0)
1175 s = pool_elt_at_index (tsm->sessions, ses_index);
1176 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1177 ses_index = elt->value;
1181 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
1182 (clib_net_to_host_u16 (s->out2in.port) != e_port))
1186 if (is_lb_session (s))
1189 if (!snat_is_session_static (s))
1192 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1193 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1198 if (addr_only && (u->nstaticsessions == 0) && (u->nsessions == 0))
1200 pool_put (tsm->users, u);
1201 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
1208 vec_free (m->workers);
1209 /* Delete static mapping from pool */
1210 pool_put (sm->static_mappings, m);
1213 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1216 /* Add/delete external address to FIB */
1217 pool_foreach (interface, sm->interfaces,
1219 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1222 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1225 pool_foreach (interface, sm->output_feature_interfaces,
1227 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1230 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1237 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1238 snat_protocol_t proto,
1239 nat44_lb_addr_port_t *locals, u8 is_add,
1240 twice_nat_type_t twice_nat, u8 out2in_only,
1243 snat_main_t * sm = &snat_main;
1244 snat_static_mapping_t *m;
1245 snat_session_key_t m_key;
1246 clib_bihash_kv_8_8_t kv, value;
1247 snat_address_t *a = 0;
1249 nat44_lb_addr_port_t *local;
1250 u32 elt_index, head_index, ses_index;
1251 snat_main_per_thread_data_t *tsm;
1252 snat_user_key_t u_key;
1255 dlist_elt_t * head, * elt;
1258 if (!sm->endpoint_dependent)
1259 return VNET_API_ERROR_FEATURE_DISABLED;
1261 m_key.addr = e_addr;
1262 m_key.port = e_port;
1263 m_key.protocol = proto;
1264 m_key.fib_index = 0;
1265 kv.key = m_key.as_u64;
1266 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1269 m = pool_elt_at_index (sm->static_mappings, value.value);
1274 return VNET_API_ERROR_VALUE_EXIST;
1276 if (vec_len (locals) < 2)
1277 return VNET_API_ERROR_INVALID_VALUE;
1279 /* Find external address in allocated addresses and reserve port for
1280 address and port pair mapping when dynamic translations enabled */
1281 if (!(sm->static_mapping_only || out2in_only))
1283 for (i = 0; i < vec_len (sm->addresses); i++)
1285 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1287 a = sm->addresses + i;
1288 /* External port must be unused */
1291 #define _(N, j, n, s) \
1292 case SNAT_PROTOCOL_##N: \
1293 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1294 return VNET_API_ERROR_INVALID_VALUE; \
1295 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1296 if (e_port > 1024) \
1298 a->busy_##n##_ports++; \
1299 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1302 foreach_snat_protocol
1305 nat_log_info ("unknown protocol");
1306 return VNET_API_ERROR_INVALID_VALUE_2;
1311 /* External address must be allocated */
1313 return VNET_API_ERROR_NO_SUCH_ENTRY;
1316 pool_get (sm->static_mappings, m);
1317 memset (m, 0, sizeof (*m));
1318 m->tag = vec_dup (tag);
1319 m->external_addr = e_addr;
1321 m->external_port = e_port;
1323 m->twice_nat = twice_nat;
1324 m->out2in_only = out2in_only;
1326 m_key.addr = m->external_addr;
1327 m_key.port = m->external_port;
1328 m_key.protocol = m->proto;
1329 m_key.fib_index = 0;
1330 kv.key = m_key.as_u64;
1331 kv.value = m - sm->static_mappings;
1332 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1334 nat_log_err ("static_mapping_by_external key add failed");
1335 return VNET_API_ERROR_UNSPECIFIED;
1338 m_key.fib_index = m->fib_index;
1339 for (i = 0; i < vec_len (locals); i++)
1341 locals[i].fib_index = fib_table_find_or_create_and_lock (
1342 FIB_PROTOCOL_IP4, locals[i].vrf_id, FIB_SOURCE_PLUGIN_LOW);
1343 m_key.addr = locals[i].addr;
1344 m_key.fib_index = locals[i].fib_index;
1347 m_key.port = locals[i].port;
1348 kv.key = m_key.as_u64;
1349 kv.value = m - sm->static_mappings;
1350 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1352 locals[i].prefix = (i == 0) ? locals[i].probability :\
1353 (locals[i - 1].prefix + locals[i].probability);
1354 vec_add1 (m->locals, locals[i]);
1355 if (sm->num_workers > 1)
1358 .src_address = locals[i].addr,
1360 bitmap = clib_bitmap_set (
1361 bitmap, sm->worker_in2out_cb (&ip, m->fib_index), 1);
1365 /* Assign workers */
1366 if (sm->num_workers > 1)
1368 clib_bitmap_foreach (i, bitmap,
1370 vec_add1(m->workers, i);
1377 return VNET_API_ERROR_NO_SUCH_ENTRY;
1379 /* Free external address port */
1380 if (!(sm->static_mapping_only || out2in_only))
1382 for (i = 0; i < vec_len (sm->addresses); i++)
1384 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1386 a = sm->addresses + i;
1389 #define _(N, j, n, s) \
1390 case SNAT_PROTOCOL_##N: \
1391 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1392 if (e_port > 1024) \
1394 a->busy_##n##_ports--; \
1395 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1398 foreach_snat_protocol
1401 nat_log_info ("unknown protocol");
1402 return VNET_API_ERROR_INVALID_VALUE_2;
1409 m_key.addr = m->external_addr;
1410 m_key.port = m->external_port;
1411 m_key.protocol = m->proto;
1412 m_key.fib_index = 0;
1413 kv.key = m_key.as_u64;
1414 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1416 nat_log_err ("static_mapping_by_external key del failed");
1417 return VNET_API_ERROR_UNSPECIFIED;
1420 vec_foreach (local, m->locals)
1422 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1423 FIB_SOURCE_PLUGIN_LOW);
1424 m_key.addr = local->addr;
1427 m_key.port = local->port;
1428 m_key.fib_index = local->fib_index;
1429 kv.key = m_key.as_u64;
1430 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1432 nat_log_err ("static_mapping_by_local key del failed");
1433 return VNET_API_ERROR_UNSPECIFIED;
1437 if (sm->num_workers > 1)
1440 .src_address = local->addr,
1442 tsm = vec_elt_at_index (sm->per_thread_data,
1443 sm->worker_in2out_cb (&ip, m->fib_index));
1446 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1448 /* Delete sessions */
1449 u_key.addr = local->addr;
1450 u_key.fib_index = m->fib_index;
1451 kv.key = u_key.as_u64;
1452 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1454 u = pool_elt_at_index (tsm->users, value.value);
1455 if (u->nstaticsessions)
1457 head_index = u->sessions_per_user_list_head_index;
1458 head = pool_elt_at_index (tsm->list_pool, head_index);
1459 elt_index = head->next;
1460 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1461 ses_index = elt->value;
1462 while (ses_index != ~0)
1464 s = pool_elt_at_index (tsm->sessions, ses_index);
1465 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1466 ses_index = elt->value;
1468 if (!(is_lb_session (s)))
1471 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1472 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1475 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1476 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1481 vec_free(m->locals);
1483 vec_free(m->workers);
1485 pool_put (sm->static_mappings, m);
1492 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1495 snat_address_t *a = 0;
1496 snat_session_t *ses;
1497 u32 *ses_to_be_removed = 0, *ses_index;
1498 snat_main_per_thread_data_t *tsm;
1499 snat_static_mapping_t *m;
1500 snat_interface_t *interface;
1502 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1504 /* Find SNAT address */
1505 for (i=0; i < vec_len (addresses); i++)
1507 if (addresses[i].addr.as_u32 == addr.as_u32)
1514 return VNET_API_ERROR_NO_SUCH_ENTRY;
1518 pool_foreach (m, sm->static_mappings,
1520 if (m->external_addr.as_u32 == addr.as_u32)
1521 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1522 m->local_port, m->external_port,
1523 m->vrf_id, m->addr_only, ~0,
1524 m->proto, 0, m->twice_nat,
1525 m->out2in_only, m->tag);
1530 /* Check if address is used in some static mapping */
1531 if (is_snat_address_used_in_static_mapping(sm, addr))
1533 nat_log_notice ("address used in static mapping");
1534 return VNET_API_ERROR_UNSPECIFIED;
1538 if (a->fib_index != ~0)
1539 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1540 FIB_SOURCE_PLUGIN_LOW);
1542 /* Delete sessions using address */
1543 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1545 vec_foreach (tsm, sm->per_thread_data)
1547 pool_foreach (ses, tsm->sessions, ({
1548 if (ses->out2in.addr.as_u32 == addr.as_u32)
1550 ses->outside_address_index = ~0;
1551 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1552 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1556 vec_foreach (ses_index, ses_to_be_removed)
1558 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1559 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1562 vec_free (ses_to_be_removed);
1568 vec_del1 (sm->twice_nat_addresses, i);
1572 vec_del1 (sm->addresses, i);
1574 /* Delete external address from FIB */
1575 pool_foreach (interface, sm->interfaces,
1577 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1580 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1583 pool_foreach (interface, sm->output_feature_interfaces,
1585 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1588 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1595 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1597 snat_main_t *sm = &snat_main;
1598 snat_interface_t *i;
1599 const char * feature_name, *del_feature_name;
1600 snat_address_t * ap;
1601 snat_static_mapping_t * m;
1602 snat_det_map_t * dm;
1603 nat_outside_fib_t *outside_fib;
1604 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1607 if (sm->out2in_dpo && !is_inside)
1608 return VNET_API_ERROR_UNSUPPORTED;
1610 pool_foreach (i, sm->output_feature_interfaces,
1612 if (i->sw_if_index == sw_if_index)
1613 return VNET_API_ERROR_VALUE_EXIST;
1616 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1617 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1620 if (sm->num_workers > 1 && !sm->deterministic)
1621 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1622 else if (sm->deterministic)
1623 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1624 else if (sm->endpoint_dependent)
1625 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1627 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1630 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1631 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1634 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1635 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1640 vec_foreach (outside_fib, sm->outside_fibs)
1642 if (outside_fib->fib_index == fib_index)
1646 outside_fib->refcount--;
1647 if (!outside_fib->refcount)
1648 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1651 outside_fib->refcount++;
1657 vec_add2 (sm->outside_fibs, outside_fib, 1);
1658 outside_fib->refcount = 1;
1659 outside_fib->fib_index = fib_index;
1663 pool_foreach (i, sm->interfaces,
1665 if (i->sw_if_index == sw_if_index)
1669 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1672 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1674 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1676 if (sm->num_workers > 1 && !sm->deterministic)
1678 del_feature_name = "nat44-handoff-classify";
1679 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1680 "nat44-out2in-worker-handoff";
1682 else if (sm->deterministic)
1684 del_feature_name = "nat44-det-classify";
1685 feature_name = !is_inside ? "nat44-det-in2out" :
1688 else if (sm->endpoint_dependent)
1690 del_feature_name = "nat44-ed-classify";
1691 feature_name = !is_inside ? "nat44-ed-in2out" :
1696 del_feature_name = "nat44-classify";
1697 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1700 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1701 sw_if_index, 0, 0, 0);
1702 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1703 sw_if_index, 1, 0, 0);
1706 if (sm->endpoint_dependent)
1707 vnet_feature_enable_disable ("ip4-local",
1708 "nat44-ed-hairpinning",
1709 sw_if_index, 1, 0, 0);
1711 vnet_feature_enable_disable ("ip4-local",
1712 "nat44-hairpinning",
1713 sw_if_index, 1, 0, 0);
1718 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1719 sw_if_index, 0, 0, 0);
1720 pool_put (sm->interfaces, i);
1723 if (sm->endpoint_dependent)
1724 vnet_feature_enable_disable ("ip4-local",
1725 "nat44-ed-hairpinning",
1726 sw_if_index, 0, 0, 0);
1728 vnet_feature_enable_disable ("ip4-local",
1729 "nat44-hairpinning",
1730 sw_if_index, 0, 0, 0);
1736 if ((nat_interface_is_inside(i) && is_inside) ||
1737 (nat_interface_is_outside(i) && !is_inside))
1740 if (sm->num_workers > 1 && !sm->deterministic)
1742 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1743 "nat44-out2in-worker-handoff";
1744 feature_name = "nat44-handoff-classify";
1746 else if (sm->deterministic)
1748 del_feature_name = !is_inside ? "nat44-det-in2out" :
1750 feature_name = "nat44-det-classify";
1752 else if (sm->endpoint_dependent)
1754 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1756 feature_name = "nat44-ed-classify";
1760 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1761 feature_name = "nat44-classify";
1764 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1765 sw_if_index, 0, 0, 0);
1766 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1767 sw_if_index, 1, 0, 0);
1770 if (sm->endpoint_dependent)
1771 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1772 sw_if_index, 0, 0, 0);
1774 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1775 sw_if_index, 0, 0, 0);
1785 return VNET_API_ERROR_NO_SUCH_ENTRY;
1787 pool_get (sm->interfaces, i);
1788 i->sw_if_index = sw_if_index;
1790 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1792 if (is_inside && !sm->out2in_dpo)
1794 if (sm->endpoint_dependent)
1795 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1796 sw_if_index, 1, 0, 0);
1798 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1799 sw_if_index, 1, 0, 0);
1805 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1809 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1811 /* Add/delete external addresses to FIB */
1813 vec_foreach (ap, sm->addresses)
1814 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1816 pool_foreach (m, sm->static_mappings,
1818 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1821 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1824 pool_foreach (dm, sm->det_maps,
1826 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1832 int snat_interface_add_del_output_feature (u32 sw_if_index,
1836 snat_main_t *sm = &snat_main;
1837 snat_interface_t *i;
1838 snat_address_t * ap;
1839 snat_static_mapping_t * m;
1841 if (sm->deterministic ||
1842 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1843 return VNET_API_ERROR_UNSUPPORTED;
1845 pool_foreach (i, sm->interfaces,
1847 if (i->sw_if_index == sw_if_index)
1848 return VNET_API_ERROR_VALUE_EXIST;
1853 if (sm->endpoint_dependent)
1855 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
1856 sw_if_index, !is_del, 0, 0);
1857 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
1858 sw_if_index, !is_del, 0, 0);
1862 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1863 sw_if_index, !is_del, 0, 0);
1864 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1865 sw_if_index, !is_del, 0, 0);
1870 if (sm->num_workers > 1)
1872 vnet_feature_enable_disable ("ip4-unicast",
1873 "nat44-out2in-worker-handoff",
1874 sw_if_index, !is_del, 0, 0);
1875 vnet_feature_enable_disable ("ip4-output",
1876 "nat44-in2out-output-worker-handoff",
1877 sw_if_index, !is_del, 0, 0);
1881 if (sm->endpoint_dependent)
1883 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
1884 sw_if_index, !is_del, 0, 0);
1885 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
1886 sw_if_index, !is_del, 0, 0);
1890 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
1891 sw_if_index, !is_del, 0, 0);
1892 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1893 sw_if_index, !is_del, 0, 0);
1898 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1899 sm->fq_in2out_output_index =
1900 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1902 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1903 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1905 pool_foreach (i, sm->output_feature_interfaces,
1907 if (i->sw_if_index == sw_if_index)
1910 pool_put (sm->output_feature_interfaces, i);
1912 return VNET_API_ERROR_VALUE_EXIST;
1919 return VNET_API_ERROR_NO_SUCH_ENTRY;
1921 pool_get (sm->output_feature_interfaces, i);
1922 i->sw_if_index = sw_if_index;
1925 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1927 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1929 /* Add/delete external addresses to FIB */
1934 vec_foreach (ap, sm->addresses)
1935 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1937 pool_foreach (m, sm->static_mappings,
1939 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1942 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1948 int snat_set_workers (uword * bitmap)
1950 snat_main_t *sm = &snat_main;
1953 if (sm->num_workers < 2)
1954 return VNET_API_ERROR_FEATURE_DISABLED;
1956 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1957 return VNET_API_ERROR_INVALID_WORKER;
1959 vec_free (sm->workers);
1960 clib_bitmap_foreach (i, bitmap,
1962 vec_add1(sm->workers, i);
1963 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1967 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1968 sm->num_snat_thread = _vec_len (sm->workers);
1975 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1978 ip4_address_t * address,
1980 u32 if_address_index,
1984 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1987 ip4_address_t * address,
1989 u32 if_address_index,
1993 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1996 snat_session_key_t * k,
1997 u32 * address_indexp,
1998 u16 port_per_thread,
1999 u32 snat_thread_index);
2001 static clib_error_t * snat_init (vlib_main_t * vm)
2003 snat_main_t * sm = &snat_main;
2004 clib_error_t * error = 0;
2005 ip4_main_t * im = &ip4_main;
2006 ip_lookup_main_t * lm = &im->lookup_main;
2008 vlib_thread_registration_t *tr;
2009 vlib_thread_main_t *tm = vlib_get_thread_main ();
2012 ip4_add_del_interface_address_callback_t cb4;
2013 vlib_node_t * error_drop_node;
2016 sm->vnet_main = vnet_get_main();
2018 sm->ip4_lookup_main = lm;
2019 sm->api_main = &api_main;
2020 sm->first_worker_index = 0;
2021 sm->num_workers = 0;
2022 sm->num_snat_thread = 1;
2024 sm->port_per_thread = 0xffff - 1024;
2025 sm->fq_in2out_index = ~0;
2026 sm->fq_out2in_index = ~0;
2027 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2028 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2029 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2030 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2031 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2032 sm->forwarding_enabled = 0;
2033 sm->log_class = vlib_log_register_class ("nat", 0);
2034 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2035 sm->error_node_index = error_drop_node->index;
2037 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2040 tr = (vlib_thread_registration_t *) p[0];
2043 sm->num_workers = tr->count;
2044 sm->first_worker_index = tr->first_index;
2048 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2050 /* Use all available workers by default */
2051 if (sm->num_workers > 1)
2053 for (i=0; i < sm->num_workers; i++)
2054 bitmap = clib_bitmap_set (bitmap, i, 1);
2055 snat_set_workers(bitmap);
2056 clib_bitmap_free (bitmap);
2060 sm->per_thread_data[0].snat_thread_index = 0;
2063 error = snat_api_init(vm, sm);
2067 /* Set up the interface address add/del callback */
2068 cb4.function = snat_ip4_add_del_interface_address_cb;
2069 cb4.function_opaque = 0;
2071 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2073 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2074 cb4.function_opaque = 0;
2076 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2078 nat_dpo_module_init ();
2080 /* Init IPFIX logging */
2081 snat_ipfix_logging_init(vm);
2084 error = nat64_init(vm);
2092 /* Init virtual fragmenentation reassembly */
2093 return nat_reass_init(vm);
2096 VLIB_INIT_FUNCTION (snat_init);
2098 void snat_free_outside_address_and_port (snat_address_t * addresses,
2100 snat_session_key_t * k,
2104 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2106 ASSERT (address_index < vec_len (addresses));
2108 a = addresses + address_index;
2110 switch (k->protocol)
2112 #define _(N, i, n, s) \
2113 case SNAT_PROTOCOL_##N: \
2114 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2115 port_host_byte_order) == 1); \
2116 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2117 port_host_byte_order, 0); \
2118 a->busy_##n##_ports--; \
2119 a->busy_##n##_ports_per_thread[thread_index]--; \
2121 foreach_snat_protocol
2124 nat_log_info ("unknown protocol");
2130 * @brief Match NAT44 static mapping.
2132 * @param sm NAT main.
2133 * @param match Address and port to match.
2134 * @param mapping External or local address and port of the matched mapping.
2135 * @param by_external If 0 match by local address otherwise match by external
2137 * @param is_addr_only If matched mapping is address only
2138 * @param twice_nat If matched mapping is twice NAT.
2139 * @param lb If matched mapping is load-balanced.
2141 * @returns 0 if match found otherwise 1.
2143 int snat_static_mapping_match (snat_main_t * sm,
2144 snat_session_key_t match,
2145 snat_session_key_t * mapping,
2148 twice_nat_type_t *twice_nat,
2151 clib_bihash_kv_8_8_t kv, value;
2152 snat_static_mapping_t *m;
2153 snat_session_key_t m_key;
2154 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2155 u32 rand, lo = 0, hi, mid;
2157 m_key.fib_index = match.fib_index;
2160 mapping_hash = &sm->static_mapping_by_external;
2161 m_key.fib_index = 0;
2164 m_key.addr = match.addr;
2165 m_key.port = clib_net_to_host_u16 (match.port);
2166 m_key.protocol = match.protocol;
2168 kv.key = m_key.as_u64;
2170 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2172 /* Try address only mapping */
2175 kv.key = m_key.as_u64;
2176 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2180 m = pool_elt_at_index (sm->static_mappings, value.value);
2184 if (vec_len (m->locals))
2187 hi = vec_len (m->locals) - 1;
2188 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2191 mid = ((hi - lo) >> 1) + lo;
2192 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2194 if (!(m->locals[lo].prefix >= rand))
2196 if (PREDICT_FALSE (sm->num_workers > 1))
2199 .src_address = m->locals[lo].addr,
2201 if (sm->worker_in2out_cb (&ip, m->fib_index) != vlib_get_thread_index ())
2204 mapping->addr = m->locals[lo].addr;
2205 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2206 mapping->fib_index = m->locals[lo].fib_index;
2210 mapping->fib_index = m->fib_index;
2211 mapping->addr = m->local_addr;
2212 /* Address only mapping doesn't change port */
2213 mapping->port = m->addr_only ? match.port
2214 : clib_host_to_net_u16 (m->local_port);
2216 mapping->protocol = m->proto;
2220 mapping->addr = m->external_addr;
2221 /* Address only mapping doesn't change port */
2222 mapping->port = m->addr_only ? match.port
2223 : clib_host_to_net_u16 (m->external_port);
2224 mapping->fib_index = sm->outside_fib_index;
2227 if (PREDICT_FALSE(is_addr_only != 0))
2228 *is_addr_only = m->addr_only;
2230 if (PREDICT_FALSE(twice_nat != 0))
2231 *twice_nat = m->twice_nat;
2233 if (PREDICT_FALSE(lb != 0))
2234 *lb = vec_len (m->locals) > 0;
2239 static_always_inline u16
2240 snat_random_port (u16 min, u16 max)
2242 snat_main_t *sm = &snat_main;
2243 return min + random_u32 (&sm->random_seed) /
2244 (random_u32_max() / (max - min + 1) + 1);
2248 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2251 snat_session_key_t * k,
2252 u32 * address_indexp,
2253 u16 port_per_thread,
2254 u32 snat_thread_index)
2256 snat_main_t *sm = &snat_main;
2258 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
2259 address_indexp, port_per_thread,
2264 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2267 snat_session_key_t * k,
2268 u32 * address_indexp,
2269 u16 port_per_thread,
2270 u32 snat_thread_index)
2273 snat_address_t *a, *ga = 0;
2276 for (i = 0; i < vec_len (addresses); i++)
2279 switch (k->protocol)
2281 #define _(N, j, n, s) \
2282 case SNAT_PROTOCOL_##N: \
2283 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2285 if (a->fib_index == fib_index) \
2289 portnum = (port_per_thread * \
2290 snat_thread_index) + \
2291 snat_random_port(1, port_per_thread) + 1024; \
2292 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2294 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2295 a->busy_##n##_ports_per_thread[thread_index]++; \
2296 a->busy_##n##_ports++; \
2297 k->addr = a->addr; \
2298 k->port = clib_host_to_net_u16(portnum); \
2299 *address_indexp = i; \
2303 else if (a->fib_index == ~0) \
2310 foreach_snat_protocol
2313 nat_log_info ("unknown protocol");
2322 switch (k->protocol)
2324 #define _(N, j, n, s) \
2325 case SNAT_PROTOCOL_##N: \
2328 portnum = (port_per_thread * \
2329 snat_thread_index) + \
2330 snat_random_port(1, port_per_thread) + 1024; \
2331 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2333 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2334 a->busy_##n##_ports_per_thread[thread_index]++; \
2335 a->busy_##n##_ports++; \
2336 k->addr = a->addr; \
2337 k->port = clib_host_to_net_u16(portnum); \
2338 *address_indexp = gi; \
2342 foreach_snat_protocol
2345 nat_log_info ("unknown protocol");
2350 /* Totally out of translations to use... */
2351 snat_ipfix_logging_addresses_exhausted(0);
2356 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2359 snat_session_key_t * k,
2360 u32 * address_indexp,
2361 u16 port_per_thread,
2362 u32 snat_thread_index)
2364 snat_main_t *sm = &snat_main;
2365 snat_address_t *a = addresses;
2366 u16 m, ports, portnum, A, j;
2367 m = 16 - (sm->psid_offset + sm->psid_length);
2368 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2370 if (!vec_len (addresses))
2373 switch (k->protocol)
2375 #define _(N, i, n, s) \
2376 case SNAT_PROTOCOL_##N: \
2377 if (a->busy_##n##_ports < ports) \
2381 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2382 j = snat_random_port(0, pow2_mask(m)); \
2383 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2384 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2386 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2387 a->busy_##n##_ports++; \
2388 k->addr = a->addr; \
2389 k->port = clib_host_to_net_u16 (portnum); \
2390 *address_indexp = i; \
2395 foreach_snat_protocol
2398 nat_log_info ("unknown protocol");
2403 /* Totally out of translations to use... */
2404 snat_ipfix_logging_addresses_exhausted(0);
2409 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2411 dpo_id_t dpo_v4 = DPO_INVALID;
2412 fib_prefix_t pfx = {
2413 .fp_proto = FIB_PROTOCOL_IP4,
2415 .fp_addr.ip4.as_u32 = addr.as_u32,
2420 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2421 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2422 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2423 dpo_reset (&dpo_v4);
2427 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2432 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2434 u32 *r = va_arg (*args, u32 *);
2437 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2438 foreach_snat_protocol
2446 format_snat_protocol (u8 * s, va_list * args)
2448 u32 i = va_arg (*args, u32);
2453 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2454 foreach_snat_protocol
2457 s = format (s, "unknown");
2460 s = format (s, "%s", t);
2464 u8 * format_snat_key (u8 * s, va_list * args);
2467 format_session_kvp (u8 * s, va_list * args)
2469 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2470 snat_session_key_t k;
2474 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2480 format_static_mapping_kvp (u8 * s, va_list * args)
2482 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2483 snat_session_key_t k;
2487 s = format (s, "%U static-mapping-index %llu", format_snat_key, &k, v->value);
2493 format_user_kvp (u8 * s, va_list * args)
2495 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2500 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2501 k.fib_index, v->value);
2507 format_ed_session_kvp (u8 * s, va_list * args)
2509 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2512 k.as_u64[0] = v->key[0];
2513 k.as_u64[1] = v->key[1];
2515 s = format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2516 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2517 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2518 format_ip_protocol, k.proto, k.fib_index, v->value);
2524 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2526 snat_main_t *sm = &snat_main;
2527 u32 next_worker_index = 0;
2530 next_worker_index = sm->first_worker_index;
2531 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2532 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2534 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2535 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2537 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2539 return next_worker_index;
2543 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2545 snat_main_t *sm = &snat_main;
2548 snat_session_key_t m_key;
2549 clib_bihash_kv_8_8_t kv, value;
2550 snat_static_mapping_t *m;
2552 u32 next_worker_index = 0;
2554 /* first try static mappings without port */
2555 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2557 m_key.addr = ip0->dst_address;
2560 m_key.fib_index = rx_fib_index0;
2561 kv.key = m_key.as_u64;
2562 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2564 m = pool_elt_at_index (sm->static_mappings, value.value);
2565 return m->workers[0];
2569 proto = ip_proto_to_snat_proto (ip0->protocol);
2570 udp = ip4_next_header (ip0);
2571 port = udp->dst_port;
2573 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2575 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2576 return vlib_get_thread_index ();
2578 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2580 nat_reass_ip4_t *reass;
2582 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2583 ip0->fragment_id, ip0->protocol);
2585 if (reass && (reass->thread_index != (u32) ~ 0))
2586 return reass->thread_index;
2588 return vlib_get_thread_index ();
2592 /* unknown protocol */
2593 if (PREDICT_FALSE (proto == ~0))
2595 /* use current thread */
2596 return vlib_get_thread_index ();
2599 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2601 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2602 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2603 if (!icmp_is_error_message (icmp))
2604 port = echo->identifier;
2607 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2608 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2609 void *l4_header = ip4_next_header (inner_ip);
2612 case SNAT_PROTOCOL_ICMP:
2613 icmp = (icmp46_header_t*)l4_header;
2614 echo = (icmp_echo_header_t *)(icmp + 1);
2615 port = echo->identifier;
2617 case SNAT_PROTOCOL_UDP:
2618 case SNAT_PROTOCOL_TCP:
2619 port = ((tcp_udp_header_t*)l4_header)->src_port;
2622 return vlib_get_thread_index ();
2627 /* try static mappings with port */
2628 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2630 m_key.addr = ip0->dst_address;
2631 m_key.port = clib_net_to_host_u16 (port);
2632 m_key.protocol = proto;
2633 m_key.fib_index = rx_fib_index0;
2634 kv.key = m_key.as_u64;
2635 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2637 m = pool_elt_at_index (sm->static_mappings, value.value);
2638 return m->workers[0];
2642 /* worker by outside port */
2643 next_worker_index = sm->first_worker_index;
2644 next_worker_index +=
2645 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2646 return next_worker_index;
2650 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index)
2652 snat_main_t *sm = &snat_main;
2653 clib_bihash_kv_8_8_t kv, value;
2654 u32 proto, next_worker_index = 0;
2657 snat_static_mapping_t *m;
2660 /* first try static mappings without port */
2661 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2663 make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
2664 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2666 m = pool_elt_at_index (sm->static_mappings, value.value);
2667 return m->workers[0];
2671 proto = ip_proto_to_snat_proto (ip->protocol);
2673 /* unknown protocol */
2674 if (PREDICT_FALSE (proto == ~0))
2676 /* use current thread */
2677 return vlib_get_thread_index ();
2680 udp = ip4_next_header (ip);
2681 port = udp->dst_port;
2683 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2685 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2686 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2687 if (!icmp_is_error_message (icmp))
2688 port = echo->identifier;
2691 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2692 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2693 void *l4_header = ip4_next_header (inner_ip);
2696 case SNAT_PROTOCOL_ICMP:
2697 icmp = (icmp46_header_t*)l4_header;
2698 echo = (icmp_echo_header_t *)(icmp + 1);
2699 port = echo->identifier;
2701 case SNAT_PROTOCOL_UDP:
2702 case SNAT_PROTOCOL_TCP:
2703 port = ((tcp_udp_header_t*)l4_header)->src_port;
2706 return vlib_get_thread_index ();
2711 /* try static mappings with port */
2712 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2714 make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index,
2715 clib_net_to_host_u16 (port));
2716 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2718 m = pool_elt_at_index (sm->static_mappings, value.value);
2719 if (!vec_len(m->locals))
2720 return m->workers[0];
2722 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2723 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >>24);
2725 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2726 return m->workers[hash & (_vec_len (m->workers) - 1)];
2728 return m->workers[hash % _vec_len (m->workers)];
2732 /* worker by outside port */
2733 next_worker_index = sm->first_worker_index;
2734 next_worker_index +=
2735 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2737 return next_worker_index;
2740 static clib_error_t *
2741 snat_config (vlib_main_t * vm, unformat_input_t * input)
2743 snat_main_t * sm = &snat_main;
2744 nat66_main_t * nm = &nat66_main;
2745 u32 translation_buckets = 1024;
2746 u32 translation_memory_size = 128<<20;
2747 u32 user_buckets = 128;
2748 u32 user_memory_size = 64<<20;
2749 u32 max_translations_per_user = 100;
2750 u32 outside_vrf_id = 0;
2751 u32 outside_ip6_vrf_id = 0;
2752 u32 inside_vrf_id = 0;
2753 u32 static_mapping_buckets = 1024;
2754 u32 static_mapping_memory_size = 64<<20;
2755 u32 nat64_bib_buckets = 1024;
2756 u32 nat64_bib_memory_size = 128 << 20;
2757 u32 nat64_st_buckets = 2048;
2758 u32 nat64_st_memory_size = 256 << 20;
2759 u8 static_mapping_only = 0;
2760 u8 static_mapping_connection_tracking = 0;
2761 snat_main_per_thread_data_t *tsm;
2762 dslite_main_t * dm = &dslite_main;
2764 sm->deterministic = 0;
2766 sm->endpoint_dependent = 0;
2768 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2770 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2772 else if (unformat (input, "translation hash memory %d",
2773 &translation_memory_size));
2774 else if (unformat (input, "user hash buckets %d", &user_buckets))
2776 else if (unformat (input, "user hash memory %d",
2779 else if (unformat (input, "max translations per user %d",
2780 &max_translations_per_user))
2782 else if (unformat (input, "outside VRF id %d",
2785 else if (unformat (input, "outside ip6 VRF id %d",
2786 &outside_ip6_vrf_id))
2788 else if (unformat (input, "inside VRF id %d",
2791 else if (unformat (input, "static mapping only"))
2793 static_mapping_only = 1;
2794 if (unformat (input, "connection tracking"))
2795 static_mapping_connection_tracking = 1;
2797 else if (unformat (input, "deterministic"))
2798 sm->deterministic = 1;
2799 else if (unformat (input, "nat64 bib hash buckets %d",
2800 &nat64_bib_buckets))
2802 else if (unformat (input, "nat64 bib hash memory %d",
2803 &nat64_bib_memory_size))
2805 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2807 else if (unformat (input, "nat64 st hash memory %d",
2808 &nat64_st_memory_size))
2810 else if (unformat (input, "out2in dpo"))
2812 else if (unformat (input, "dslite ce"))
2813 dslite_set_ce(dm, 1);
2814 else if (unformat (input, "endpoint-dependent"))
2815 sm->endpoint_dependent = 1;
2817 return clib_error_return (0, "unknown input '%U'",
2818 format_unformat_error, input);
2821 /* for show commands, etc. */
2822 sm->translation_buckets = translation_buckets;
2823 sm->translation_memory_size = translation_memory_size;
2824 /* do not exceed load factor 10 */
2825 sm->max_translations = 10 * translation_buckets;
2826 sm->user_buckets = user_buckets;
2827 sm->user_memory_size = user_memory_size;
2828 sm->max_translations_per_user = max_translations_per_user;
2829 sm->outside_vrf_id = outside_vrf_id;
2830 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2832 FIB_SOURCE_PLUGIN_HI);
2833 nm->outside_vrf_id = outside_ip6_vrf_id;
2834 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2836 FIB_SOURCE_PLUGIN_HI);
2837 sm->inside_vrf_id = inside_vrf_id;
2838 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2840 FIB_SOURCE_PLUGIN_HI);
2841 sm->static_mapping_only = static_mapping_only;
2842 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2844 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2845 nat64_st_memory_size);
2847 if (sm->deterministic)
2849 sm->in2out_node_index = snat_det_in2out_node.index;
2850 sm->in2out_output_node_index = ~0;
2851 sm->out2in_node_index = snat_det_out2in_node.index;
2852 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2853 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2857 if (sm->endpoint_dependent)
2859 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2860 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2861 sm->in2out_node_index = nat44_ed_in2out_node.index;
2862 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
2863 sm->out2in_node_index = nat44_ed_out2in_node.index;
2864 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2865 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2869 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2870 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2871 sm->in2out_node_index = snat_in2out_node.index;
2872 sm->in2out_output_node_index = snat_in2out_output_node.index;
2873 sm->out2in_node_index = snat_out2in_node.index;
2874 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2875 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2877 if (!static_mapping_only ||
2878 (static_mapping_only && static_mapping_connection_tracking))
2880 vec_foreach (tsm, sm->per_thread_data)
2882 if (sm->endpoint_dependent)
2884 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
2885 translation_buckets,
2886 translation_memory_size);
2887 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
2888 format_ed_session_kvp);
2890 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
2891 translation_buckets,
2892 translation_memory_size);
2893 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
2894 format_ed_session_kvp);
2898 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
2899 translation_buckets,
2900 translation_memory_size);
2901 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
2902 format_session_kvp);
2904 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
2905 translation_buckets,
2906 translation_memory_size);
2907 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
2908 format_session_kvp);
2911 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2913 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
2920 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2921 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2923 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2924 "static_mapping_by_local", static_mapping_buckets,
2925 static_mapping_memory_size);
2926 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2927 format_static_mapping_kvp);
2929 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2930 "static_mapping_by_external", static_mapping_buckets,
2931 static_mapping_memory_size);
2932 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2933 format_static_mapping_kvp);
2939 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2941 u8 * format_snat_session_state (u8 * s, va_list * args)
2943 u32 i = va_arg (*args, u32);
2948 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2949 foreach_snat_session_state
2952 t = format (t, "unknown");
2954 s = format (s, "%s", t);
2958 u8 * format_snat_key (u8 * s, va_list * args)
2960 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2962 s = format (s, "%U proto %U port %d fib %d",
2963 format_ip4_address, &key->addr,
2964 format_snat_protocol, key->protocol,
2965 clib_net_to_host_u16 (key->port), key->fib_index);
2969 u8 * format_snat_session (u8 * s, va_list * args)
2971 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2972 snat_session_t * sess = va_arg (*args, snat_session_t *);
2974 if (snat_is_unk_proto_session (sess))
2976 s = format (s, " i2o %U proto %u fib %u\n",
2977 format_ip4_address, &sess->in2out.addr,
2978 clib_net_to_host_u16 (sess->in2out.port),
2979 sess->in2out.fib_index);
2980 s = format (s, " o2i %U proto %u fib %u\n",
2981 format_ip4_address, &sess->out2in.addr,
2982 clib_net_to_host_u16 (sess->out2in.port),
2983 sess->out2in.fib_index);
2987 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2988 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2990 if (is_ed_session (sess) || is_fwd_bypass_session (sess))
2992 if (is_twice_nat_session (sess))
2994 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2995 format_ip4_address, &sess->ext_host_addr,
2996 clib_net_to_host_u16 (sess->ext_host_port),
2997 format_ip4_address, &sess->ext_host_nat_addr,
2998 clib_net_to_host_u16 (sess->ext_host_nat_port));
3002 if (sess->ext_host_addr.as_u32)
3003 s = format (s, " external host %U:%u\n",
3004 format_ip4_address, &sess->ext_host_addr,
3005 clib_net_to_host_u16 (sess->ext_host_port));
3008 s = format (s, " index %llu\n", sess - sm->sessions);
3009 s = format (s, " last heard %.2f\n", sess->last_heard);
3010 s = format (s, " total pkts %d, total bytes %lld\n",
3011 sess->total_pkts, sess->total_bytes);
3012 if (snat_is_session_static (sess))
3013 s = format (s, " static translation\n");
3015 s = format (s, " dynamic translation\n");
3016 if (is_fwd_bypass_session (sess))
3017 s = format (s, " forwarding-bypass\n");
3018 if (is_lb_session (sess))
3019 s = format (s, " load-balancing\n");
3020 if (is_twice_nat_session (sess))
3021 s = format (s, " twice-nat\n");
3026 u8 * format_snat_user (u8 * s, va_list * args)
3028 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
3029 snat_user_t * u = va_arg (*args, snat_user_t *);
3030 int verbose = va_arg (*args, int);
3031 dlist_elt_t * head, * elt;
3032 u32 elt_index, head_index;
3034 snat_session_t * sess;
3036 s = format (s, "%U: %d dynamic translations, %d static translations\n",
3037 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
3042 if (u->nsessions || u->nstaticsessions)
3044 head_index = u->sessions_per_user_list_head_index;
3045 head = pool_elt_at_index (sm->list_pool, head_index);
3047 elt_index = head->next;
3048 elt = pool_elt_at_index (sm->list_pool, elt_index);
3049 session_index = elt->value;
3051 while (session_index != ~0)
3053 sess = pool_elt_at_index (sm->sessions, session_index);
3055 s = format (s, " %U\n", format_snat_session, sm, sess);
3057 elt_index = elt->next;
3058 elt = pool_elt_at_index (sm->list_pool, elt_index);
3059 session_index = elt->value;
3066 u8 * format_snat_static_mapping (u8 * s, va_list * args)
3068 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
3069 nat44_lb_addr_port_t *local;
3072 s = format (s, "local %U external %U vrf %d %s %s",
3073 format_ip4_address, &m->local_addr,
3074 format_ip4_address, &m->external_addr,
3076 m->twice_nat == TWICE_NAT ? "twice-nat" :
3077 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3078 m->out2in_only ? "out2in-only" : "");
3081 if (vec_len (m->locals))
3083 s = format (s, "%U external %U:%d %s %s",
3084 format_snat_protocol, m->proto,
3085 format_ip4_address, &m->external_addr, m->external_port,
3086 m->twice_nat == TWICE_NAT ? "twice-nat" :
3087 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3088 m->out2in_only ? "out2in-only" : "");
3089 vec_foreach (local, m->locals)
3090 s = format (s, "\n local %U:%d vrf %d probability %d\%",
3091 format_ip4_address, &local->addr, local->port,
3092 local->vrf_id, local->probability);
3095 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
3096 format_snat_protocol, m->proto,
3097 format_ip4_address, &m->local_addr, m->local_port,
3098 format_ip4_address, &m->external_addr, m->external_port,
3100 m->twice_nat == TWICE_NAT ? "twice-nat" :
3101 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
3102 m->out2in_only ? "out2in-only" : "");
3107 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
3109 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
3110 vnet_main_t *vnm = vnet_get_main();
3113 s = format (s, "local %U external %U vrf %d",
3114 format_ip4_address, &m->l_addr,
3115 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
3118 s = format (s, "%U local %U:%d external %U:%d vrf %d",
3119 format_snat_protocol, m->proto,
3120 format_ip4_address, &m->l_addr, m->l_port,
3121 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
3122 m->e_port, m->vrf_id);
3127 u8 * format_det_map_ses (u8 * s, va_list * args)
3129 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
3130 ip4_address_t in_addr, out_addr;
3131 u32 in_offset, out_offset;
3132 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
3133 u32 * i = va_arg (*args, u32 *);
3135 u32 user_index = *i / SNAT_DET_SES_PER_USER;
3136 in_addr.as_u32 = clib_host_to_net_u32 (
3137 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
3138 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
3139 clib_net_to_host_u32(det_map->in_addr.as_u32);
3140 out_offset = in_offset / det_map->sharing_ratio;
3141 out_addr.as_u32 = clib_host_to_net_u32(
3142 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
3143 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
3144 format_ip4_address, &in_addr,
3145 clib_net_to_host_u16 (ses->in_port),
3146 format_ip4_address, &out_addr,
3147 clib_net_to_host_u16 (ses->out.out_port),
3148 format_ip4_address, &ses->out.ext_host_addr,
3149 clib_net_to_host_u16 (ses->out.ext_host_port),
3150 format_snat_session_state, ses->state,
3157 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3160 ip4_address_t * address,
3162 u32 if_address_index,
3165 snat_main_t *sm = &snat_main;
3166 snat_static_map_resolve_t *rp;
3167 snat_static_mapping_t *m;
3168 snat_session_key_t m_key;
3169 clib_bihash_kv_8_8_t kv, value;
3171 ip4_address_t l_addr;
3173 for (i = 0; i < vec_len (sm->to_resolve); i++)
3175 rp = sm->to_resolve + i;
3176 if (rp->addr_only == 0)
3178 if (rp->sw_if_index == sw_if_index)
3185 m_key.addr.as_u32 = address->as_u32;
3186 m_key.port = rp->addr_only ? 0 : rp->e_port;
3187 m_key.protocol = rp->addr_only ? 0 : rp->proto;
3188 m_key.fib_index = sm->outside_fib_index;
3189 kv.key = m_key.as_u64;
3190 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3193 m = pool_elt_at_index (sm->static_mappings, value.value);
3197 /* Don't trip over lease renewal, static config */
3207 /* Indetity mapping? */
3208 if (rp->l_addr.as_u32 == 0)
3209 l_addr.as_u32 = address[0].as_u32;
3211 l_addr.as_u32 = rp->l_addr.as_u32;
3212 /* Add the static mapping */
3213 rv = snat_add_static_mapping (l_addr,
3219 ~0 /* sw_if_index */,
3224 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3228 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3231 ip4_address_t * address,
3233 u32 if_address_index,
3236 snat_main_t *sm = &snat_main;
3237 snat_static_map_resolve_t *rp;
3238 ip4_address_t l_addr;
3242 snat_address_t *addresses = sm->addresses;
3244 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3246 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3250 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
3253 addresses = sm->twice_nat_addresses;
3254 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3263 /* Don't trip over lease renewal, static config */
3264 for (j = 0; j < vec_len(addresses); j++)
3265 if (addresses[j].addr.as_u32 == address->as_u32)
3268 (void) snat_add_address (sm, address, ~0, twice_nat);
3269 /* Scan static map resolution vector */
3270 for (j = 0; j < vec_len (sm->to_resolve); j++)
3272 rp = sm->to_resolve + j;
3275 /* On this interface? */
3276 if (rp->sw_if_index == sw_if_index)
3278 /* Indetity mapping? */
3279 if (rp->l_addr.as_u32 == 0)
3280 l_addr.as_u32 = address[0].as_u32;
3282 l_addr.as_u32 = rp->l_addr.as_u32;
3283 /* Add the static mapping */
3284 rv = snat_add_static_mapping (l_addr,
3290 ~0 /* sw_if_index */,
3295 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3302 (void) snat_del_address(sm, address[0], 1, twice_nat);
3308 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
3311 ip4_main_t * ip4_main = sm->ip4_main;
3312 ip4_address_t * first_int_addr;
3313 snat_static_map_resolve_t *rp;
3314 u32 *indices_to_delete = 0;
3316 u32 *auto_add_sw_if_indices =
3317 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3319 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3320 0 /* just want the address*/);
3322 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
3324 if (auto_add_sw_if_indices[i] == sw_if_index)
3328 /* if have address remove it */
3330 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3333 for (j = 0; j < vec_len (sm->to_resolve); j++)
3335 rp = sm->to_resolve + j;
3336 if (rp->sw_if_index == sw_if_index)
3337 vec_add1 (indices_to_delete, j);
3339 if (vec_len(indices_to_delete))
3341 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3342 vec_del1(sm->to_resolve, j);
3343 vec_free(indices_to_delete);
3347 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
3349 vec_del1(sm->auto_add_sw_if_indices, i);
3352 return VNET_API_ERROR_VALUE_EXIST;
3359 return VNET_API_ERROR_NO_SUCH_ENTRY;
3361 /* add to the auto-address list */
3363 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3365 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3367 /* If the address is already bound - or static - add it now */
3369 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3375 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3376 snat_protocol_t proto, u32 vrf_id, int is_in)
3378 snat_main_per_thread_data_t *tsm;
3379 clib_bihash_kv_8_8_t kv, value;
3381 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3382 snat_session_key_t key;
3384 clib_bihash_8_8_t *t;
3386 if (sm->endpoint_dependent)
3387 return VNET_API_ERROR_UNSUPPORTED;
3389 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3390 if (sm->num_workers > 1)
3392 vec_elt_at_index (sm->per_thread_data,
3393 sm->worker_in2out_cb (&ip, fib_index));
3395 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3397 key.addr.as_u32 = addr->as_u32;
3398 key.port = clib_host_to_net_u16 (port);
3399 key.protocol = proto;
3400 key.fib_index = fib_index;
3401 kv.key = key.as_u64;
3402 t = is_in ? &tsm->in2out : &tsm->out2in;
3403 if (!clib_bihash_search_8_8 (t, &kv, &value))
3405 if (pool_is_free_index (tsm->sessions, value.value))
3406 return VNET_API_ERROR_UNSPECIFIED;
3408 s = pool_elt_at_index (tsm->sessions, value.value);
3409 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3410 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3414 return VNET_API_ERROR_NO_SUCH_ENTRY;
3418 nat44_del_ed_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3419 ip4_address_t *eh_addr, u16 eh_port, u8 proto,
3420 u32 vrf_id, int is_in)
3423 clib_bihash_16_8_t *t;
3424 nat_ed_ses_key_t key;
3425 clib_bihash_kv_16_8_t kv, value;
3426 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3428 snat_main_per_thread_data_t *tsm;
3430 if (!sm->endpoint_dependent)
3431 return VNET_API_ERROR_FEATURE_DISABLED;
3433 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3434 if (sm->num_workers > 1)
3436 vec_elt_at_index (sm->per_thread_data,
3437 sm->worker_in2out_cb (&ip, fib_index));
3439 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3441 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
3442 key.l_addr.as_u32 = addr->as_u32;
3443 key.r_addr.as_u32 = eh_addr->as_u32;
3444 key.l_port = clib_host_to_net_u16 (port);
3445 key.r_port = clib_host_to_net_u16 (eh_port);
3447 key.fib_index = clib_host_to_net_u32 (fib_index);
3448 kv.key[0] = key.as_u64[0];
3449 kv.key[1] = key.as_u64[1];
3450 if (clib_bihash_search_16_8 (t, &kv, &value))
3451 return VNET_API_ERROR_NO_SUCH_ENTRY;
3453 if (pool_is_free_index (tsm->sessions, value.value))
3454 return VNET_API_ERROR_UNSPECIFIED;
3455 s = pool_elt_at_index (tsm->sessions, value.value);
3456 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3457 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3462 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3464 snat_main_t *sm = &snat_main;
3466 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3468 sm->psid_offset = psid_offset;
3469 sm->psid_length = psid_length;
3473 nat_set_alloc_addr_and_port_default (void)
3475 snat_main_t *sm = &snat_main;
3477 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;