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 <vnet/fib/fib_table.h>
31 #include <vnet/fib/ip4_fib.h>
33 #include <vpp/app/version.h>
35 snat_main_t snat_main;
38 /* Hook up input features */
39 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
40 .arc_name = "ip4-unicast",
41 .node_name = "nat44-in2out",
42 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
44 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
45 .arc_name = "ip4-unicast",
46 .node_name = "nat44-out2in",
47 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
49 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
50 .arc_name = "ip4-unicast",
51 .node_name = "nat44-classify",
52 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
54 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
55 .arc_name = "ip4-unicast",
56 .node_name = "nat44-det-in2out",
57 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
59 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
60 .arc_name = "ip4-unicast",
61 .node_name = "nat44-det-out2in",
62 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
64 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
65 .arc_name = "ip4-unicast",
66 .node_name = "nat44-det-classify",
67 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
69 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
70 .arc_name = "ip4-unicast",
71 .node_name = "nat44-in2out-worker-handoff",
72 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
74 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
75 .arc_name = "ip4-unicast",
76 .node_name = "nat44-out2in-worker-handoff",
77 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
79 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
80 .arc_name = "ip4-unicast",
81 .node_name = "nat44-handoff-classify",
82 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
84 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
85 .arc_name = "ip4-unicast",
86 .node_name = "nat44-in2out-fast",
87 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
89 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
90 .arc_name = "ip4-unicast",
91 .node_name = "nat44-out2in-fast",
92 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
94 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
95 .arc_name = "ip4-unicast",
96 .node_name = "nat44-hairpin-dst",
97 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
100 /* Hook up output features */
101 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
102 .arc_name = "ip4-output",
103 .node_name = "nat44-in2out-output",
104 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
106 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
107 .arc_name = "ip4-output",
108 .node_name = "nat44-in2out-output-worker-handoff",
109 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
111 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
112 .arc_name = "ip4-output",
113 .node_name = "nat44-hairpin-src",
114 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
117 /* Hook up ip4-local features */
118 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
120 .arc_name = "ip4-local",
121 .node_name = "nat44-hairpinning",
122 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
127 VLIB_PLUGIN_REGISTER () = {
128 .version = VPP_BUILD_VER,
129 .description = "Network Address Translation",
133 vlib_node_registration_t nat44_classify_node;
134 vlib_node_registration_t nat44_det_classify_node;
135 vlib_node_registration_t nat44_handoff_classify_node;
138 NAT44_CLASSIFY_NEXT_IN2OUT,
139 NAT44_CLASSIFY_NEXT_OUT2IN,
140 NAT44_CLASSIFY_N_NEXT,
141 } nat44_classify_next_t;
144 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
146 snat_session_key_t key;
147 clib_bihash_kv_8_8_t kv;
148 nat_ed_ses_key_t ed_key;
149 clib_bihash_kv_16_8_t ed_kv;
152 snat_main_per_thread_data_t *tsm =
153 vec_elt_at_index (sm->per_thread_data, thread_index);
155 /* Endpoint dependent session lookup tables */
156 if (is_ed_session (s))
158 ed_key.l_addr = s->out2in.addr;
159 ed_key.r_addr = s->ext_host_addr;
160 ed_key.fib_index = s->out2in.fib_index;
161 if (snat_is_unk_proto_session (s))
163 ed_key.proto = s->in2out.port;
169 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
170 ed_key.l_port = s->out2in.port;
171 ed_key.r_port = s->ext_host_port;
173 ed_kv.key[0] = ed_key.as_u64[0];
174 ed_kv.key[1] = ed_key.as_u64[1];
175 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
176 clib_warning ("out2in_ed key del failed");
178 ed_key.l_addr = s->in2out.addr;
179 ed_key.fib_index = s->in2out.fib_index;
180 if (!snat_is_unk_proto_session (s))
181 ed_key.l_port = s->in2out.port;
182 if (is_twice_nat_session (s))
184 ed_key.r_addr = s->ext_host_nat_addr;
185 ed_key.r_port = s->ext_host_nat_port;
187 ed_kv.key[0] = ed_key.as_u64[0];
188 ed_kv.key[1] = ed_key.as_u64[1];
189 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &ed_kv, 0))
190 clib_warning ("in2out_ed key del failed");
193 if (snat_is_unk_proto_session (s))
197 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
198 s->out2in.addr.as_u32,
202 s->in2out.fib_index);
204 /* Twice NAT address and port for external host */
205 if (is_twice_nat_session (s))
207 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
209 key.protocol = s->in2out.protocol;
210 key.port = s->ext_host_nat_port;
211 a = sm->twice_nat_addresses + i;
212 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
214 snat_free_outside_address_and_port (sm->twice_nat_addresses,
215 thread_index, &key, i);
221 if (is_ed_session (s))
224 /* Session lookup tables */
225 kv.key = s->in2out.as_u64;
226 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
227 clib_warning ("in2out key del failed");
228 kv.key = s->out2in.as_u64;
229 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
230 clib_warning ("out2in key del failed");
232 if (snat_is_session_static (s))
235 if (s->outside_address_index != ~0)
236 snat_free_outside_address_and_port (sm->addresses, thread_index,
237 &s->out2in, s->outside_address_index);
241 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
245 snat_user_key_t user_key;
246 clib_bihash_kv_8_8_t kv, value;
247 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
248 dlist_elt_t * per_user_list_head_elt;
250 user_key.addr.as_u32 = addr->as_u32;
251 user_key.fib_index = fib_index;
252 kv.key = user_key.as_u64;
254 /* Ever heard of the "user" = src ip4 address before? */
255 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
257 /* no, make a new one */
258 pool_get (tsm->users, u);
259 memset (u, 0, sizeof (*u));
260 u->addr.as_u32 = addr->as_u32;
261 u->fib_index = fib_index;
263 pool_get (tsm->list_pool, per_user_list_head_elt);
265 u->sessions_per_user_list_head_index = per_user_list_head_elt -
268 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
270 kv.value = u - tsm->users;
273 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
274 clib_warning ("user_hash keay add failed");
278 u = pool_elt_at_index (tsm->users, value.value);
285 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
288 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
289 u32 oldest_per_user_translation_list_index, session_index;
290 dlist_elt_t * oldest_per_user_translation_list_elt;
291 dlist_elt_t * per_user_translation_list_elt;
293 /* Over quota? Recycle the least recently used translation */
294 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
296 oldest_per_user_translation_list_index =
297 clib_dlist_remove_head (tsm->list_pool,
298 u->sessions_per_user_list_head_index);
300 ASSERT (oldest_per_user_translation_list_index != ~0);
302 /* Add it back to the end of the LRU list */
303 clib_dlist_addtail (tsm->list_pool,
304 u->sessions_per_user_list_head_index,
305 oldest_per_user_translation_list_index);
306 /* Get the list element */
307 oldest_per_user_translation_list_elt =
308 pool_elt_at_index (tsm->list_pool,
309 oldest_per_user_translation_list_index);
311 /* Get the session index from the list element */
312 session_index = oldest_per_user_translation_list_elt->value;
314 /* Get the session */
315 s = pool_elt_at_index (tsm->sessions, session_index);
316 nat_free_session_data (sm, s, thread_index);
317 s->outside_address_index = ~0;
324 pool_get (tsm->sessions, s);
325 memset (s, 0, sizeof (*s));
326 s->outside_address_index = ~0;
328 /* Create list elts */
329 pool_get (tsm->list_pool, per_user_translation_list_elt);
330 clib_dlist_init (tsm->list_pool,
331 per_user_translation_list_elt - tsm->list_pool);
333 per_user_translation_list_elt->value = s - tsm->sessions;
334 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
335 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
337 clib_dlist_addtail (tsm->list_pool,
338 s->per_user_list_head_index,
339 per_user_translation_list_elt - tsm->list_pool);
346 nat44_classify_node_fn_inline (vlib_main_t * vm,
347 vlib_node_runtime_t * node,
348 vlib_frame_t * frame)
350 u32 n_left_from, * from, * to_next;
351 nat44_classify_next_t next_index;
352 snat_main_t *sm = &snat_main;
354 from = vlib_frame_vector_args (frame);
355 n_left_from = frame->n_vectors;
356 next_index = node->cached_next_index;
358 while (n_left_from > 0)
362 vlib_get_next_frame (vm, node, next_index,
363 to_next, n_left_to_next);
365 while (n_left_from > 0 && n_left_to_next > 0)
369 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
372 snat_session_key_t m_key0;
373 clib_bihash_kv_8_8_t kv0, value0;
375 /* speculatively enqueue b0 to the current next frame */
383 b0 = vlib_get_buffer (vm, bi0);
384 ip0 = vlib_buffer_get_current (b0);
386 vec_foreach (ap, sm->addresses)
388 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
390 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
395 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
397 m_key0.addr = ip0->dst_address;
400 m_key0.fib_index = sm->outside_fib_index;
401 kv0.key = m_key0.as_u64;
402 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
404 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
407 udp_header_t * udp0 = ip4_next_header (ip0);
408 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
409 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
410 kv0.key = m_key0.as_u64;
411 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
412 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
416 /* verify speculative enqueue, maybe switch current next frame */
417 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
418 to_next, n_left_to_next,
422 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
425 return frame->n_vectors;
429 nat44_classify_node_fn (vlib_main_t * vm,
430 vlib_node_runtime_t * node,
431 vlib_frame_t * frame)
433 return nat44_classify_node_fn_inline (vm, node, frame);
436 VLIB_REGISTER_NODE (nat44_classify_node) = {
437 .function = nat44_classify_node_fn,
438 .name = "nat44-classify",
439 .vector_size = sizeof (u32),
440 .type = VLIB_NODE_TYPE_INTERNAL,
441 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
443 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
444 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
448 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
449 nat44_classify_node_fn);
452 nat44_det_classify_node_fn (vlib_main_t * vm,
453 vlib_node_runtime_t * node,
454 vlib_frame_t * frame)
456 return nat44_classify_node_fn_inline (vm, node, frame);
459 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
460 .function = nat44_det_classify_node_fn,
461 .name = "nat44-det-classify",
462 .vector_size = sizeof (u32),
463 .type = VLIB_NODE_TYPE_INTERNAL,
464 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
466 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
467 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
471 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
472 nat44_det_classify_node_fn);
475 nat44_handoff_classify_node_fn (vlib_main_t * vm,
476 vlib_node_runtime_t * node,
477 vlib_frame_t * frame)
479 return nat44_classify_node_fn_inline (vm, node, frame);
482 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
483 .function = nat44_handoff_classify_node_fn,
484 .name = "nat44-handoff-classify",
485 .vector_size = sizeof (u32),
486 .type = VLIB_NODE_TYPE_INTERNAL,
487 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
489 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
490 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
494 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
495 nat44_handoff_classify_node_fn);
498 * @brief Add/del NAT address to FIB.
500 * Add the external NAT address to the FIB as receive entries. This ensures
501 * that VPP will reply to ARP for this address and we don't need to enable
502 * proxy ARP on the outside interface.
504 * @param addr IPv4 address.
505 * @param plen address prefix length
506 * @param sw_if_index Interface.
507 * @param is_add If 0 delete, otherwise add.
510 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
513 fib_prefix_t prefix = {
515 .fp_proto = FIB_PROTOCOL_IP4,
517 .ip4.as_u32 = addr->as_u32,
520 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
523 fib_table_entry_update_one_path(fib_index,
525 FIB_SOURCE_PLUGIN_LOW,
526 (FIB_ENTRY_FLAG_CONNECTED |
527 FIB_ENTRY_FLAG_LOCAL |
528 FIB_ENTRY_FLAG_EXCLUSIVE),
535 FIB_ROUTE_PATH_FLAG_NONE);
537 fib_table_entry_delete(fib_index,
539 FIB_SOURCE_PLUGIN_LOW);
542 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
547 vlib_thread_main_t *tm = vlib_get_thread_main ();
549 /* Check if address already exists */
550 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
552 if (ap->addr.as_u32 == addr->as_u32)
557 vec_add2 (sm->twice_nat_addresses, ap, 1);
559 vec_add2 (sm->addresses, ap, 1);
564 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
565 FIB_SOURCE_PLUGIN_LOW);
568 #define _(N, i, n, s) \
569 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
570 ap->busy_##n##_ports = 0; \
571 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
572 foreach_snat_protocol
578 /* Add external address to FIB */
579 pool_foreach (i, sm->interfaces,
581 if (nat_interface_is_inside(i) || sm->out2in_dpo)
584 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
587 pool_foreach (i, sm->output_feature_interfaces,
589 if (nat_interface_is_inside(i) || sm->out2in_dpo)
592 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
597 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
600 snat_static_mapping_t *m;
601 pool_foreach (m, sm->static_mappings,
603 if (m->external_addr.as_u32 == addr.as_u32)
610 void increment_v4_address (ip4_address_t * a)
614 v = clib_net_to_host_u32(a->as_u32) + 1;
615 a->as_u32 = clib_host_to_net_u32(v);
619 snat_add_static_mapping_when_resolved (snat_main_t * sm,
620 ip4_address_t l_addr,
625 snat_protocol_t proto,
630 snat_static_map_resolve_t *rp;
632 vec_add2 (sm->to_resolve, rp, 1);
633 rp->l_addr.as_u32 = l_addr.as_u32;
635 rp->sw_if_index = sw_if_index;
639 rp->addr_only = addr_only;
641 rp->tag = vec_dup (tag);
645 * @brief Add static mapping.
647 * Create static mapping between local addr+port and external addr+port.
649 * @param l_addr Local IPv4 address.
650 * @param e_addr External IPv4 address.
651 * @param l_port Local port number.
652 * @param e_port External port number.
653 * @param vrf_id VRF ID.
654 * @param addr_only If 0 address port and pair mapping, otherwise address only.
655 * @param sw_if_index External port instead of specific IP address.
656 * @param is_add If 0 delete static mapping, otherwise add.
657 * @param twice_nat If 1 translate external host address and port.
658 * @param out2in_only If 1 rule match only out2in direction
659 * @param tag - opaque string tag
663 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
664 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
665 u32 sw_if_index, snat_protocol_t proto, int is_add,
666 u8 twice_nat, u8 out2in_only, u8 * tag)
668 snat_main_t * sm = &snat_main;
669 snat_static_mapping_t *m;
670 snat_session_key_t m_key;
671 clib_bihash_kv_8_8_t kv, value;
672 snat_address_t *a = 0;
675 snat_interface_t *interface;
677 snat_main_per_thread_data_t *tsm;
678 snat_user_key_t u_key;
680 dlist_elt_t * head, * elt;
681 u32 elt_index, head_index;
685 snat_static_map_resolve_t *rp, *rp_match = 0;
687 /* If the external address is a specific interface address */
688 if (sw_if_index != ~0)
690 ip4_address_t * first_int_addr;
692 for (i = 0; i < vec_len (sm->to_resolve); i++)
694 rp = sm->to_resolve + i;
695 if (rp->sw_if_index != sw_if_index &&
696 rp->l_addr.as_u32 != l_addr.as_u32 &&
697 rp->vrf_id != vrf_id && rp->addr_only != addr_only)
702 if (rp->l_port != l_port && rp->e_port != e_port && rp->proto != proto)
710 /* Might be already set... */
711 first_int_addr = ip4_interface_first_address
712 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
717 return VNET_API_ERROR_VALUE_EXIST;
719 snat_add_static_mapping_when_resolved
720 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
721 addr_only, is_add, tag);
723 /* DHCP resolution required? */
724 if (first_int_addr == 0)
730 e_addr.as_u32 = first_int_addr->as_u32;
731 /* Identity mapping? */
732 if (l_addr.as_u32 == 0)
733 l_addr.as_u32 = e_addr.as_u32;
739 return VNET_API_ERROR_NO_SUCH_ENTRY;
741 vec_del1 (sm->to_resolve, i);
745 e_addr.as_u32 = first_int_addr->as_u32;
746 /* Identity mapping? */
747 if (l_addr.as_u32 == 0)
748 l_addr.as_u32 = e_addr.as_u32;
756 m_key.port = addr_only ? 0 : e_port;
757 m_key.protocol = addr_only ? 0 : proto;
758 m_key.fib_index = sm->outside_fib_index;
759 kv.key = m_key.as_u64;
760 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
763 m = pool_elt_at_index (sm->static_mappings, value.value);
768 return VNET_API_ERROR_VALUE_EXIST;
770 if (twice_nat && addr_only)
771 return VNET_API_ERROR_UNSUPPORTED;
773 /* Convert VRF id to FIB index */
776 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
778 return VNET_API_ERROR_NO_SUCH_FIB;
781 /* If not specified use inside VRF id from SNAT plugin startup config */
784 fib_index = sm->inside_fib_index;
785 vrf_id = sm->inside_vrf_id;
791 m_key.port = addr_only ? 0 : l_port;
792 m_key.protocol = addr_only ? 0 : proto;
793 m_key.fib_index = fib_index;
794 kv.key = m_key.as_u64;
795 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
796 return VNET_API_ERROR_VALUE_EXIST;
799 /* Find external address in allocated addresses and reserve port for
800 address and port pair mapping when dynamic translations enabled */
801 if (!(addr_only || sm->static_mapping_only || out2in_only))
803 for (i = 0; i < vec_len (sm->addresses); i++)
805 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
807 a = sm->addresses + i;
808 /* External port must be unused */
811 #define _(N, j, n, s) \
812 case SNAT_PROTOCOL_##N: \
813 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
814 return VNET_API_ERROR_INVALID_VALUE; \
815 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
818 a->busy_##n##_ports++; \
819 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
822 foreach_snat_protocol
825 clib_warning("unknown_protocol");
826 return VNET_API_ERROR_INVALID_VALUE_2;
831 /* External address must be allocated */
832 if (!a && (l_addr.as_u32 != e_addr.as_u32))
834 if (sw_if_index != ~0)
836 for (i = 0; i < vec_len (sm->to_resolve); i++)
838 rp = sm->to_resolve + i;
841 if (rp->sw_if_index != sw_if_index &&
842 rp->l_addr.as_u32 != l_addr.as_u32 &&
843 rp->vrf_id != vrf_id && rp->l_port != l_port &&
844 rp->e_port != e_port && rp->proto != proto)
847 vec_del1 (sm->to_resolve, i);
851 return VNET_API_ERROR_NO_SUCH_ENTRY;
855 pool_get (sm->static_mappings, m);
856 memset (m, 0, sizeof (*m));
857 m->tag = vec_dup (tag);
858 m->local_addr = l_addr;
859 m->external_addr = e_addr;
860 m->addr_only = addr_only;
862 m->fib_index = fib_index;
863 m->twice_nat = twice_nat;
864 m->out2in_only = out2in_only;
867 m->local_port = l_port;
868 m->external_port = e_port;
875 .src_address = m->local_addr,
877 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
878 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
881 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
883 m_key.addr = m->local_addr;
884 m_key.port = m->local_port;
885 m_key.protocol = m->proto;
886 m_key.fib_index = m->fib_index;
887 kv.key = m_key.as_u64;
888 kv.value = m - sm->static_mappings;
890 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
891 if (twice_nat || out2in_only)
893 m_key.port = clib_host_to_net_u16 (l_port);
894 kv.key = m_key.as_u64;
896 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
897 clib_warning ("in2out key add failed");
900 m_key.addr = m->external_addr;
901 m_key.port = m->external_port;
902 m_key.fib_index = sm->outside_fib_index;
903 kv.key = m_key.as_u64;
904 kv.value = m - sm->static_mappings;
905 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
906 if (twice_nat || out2in_only)
908 m_key.port = clib_host_to_net_u16 (e_port);
909 kv.key = m_key.as_u64;
911 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
912 clib_warning ("out2in key add failed");
915 /* Delete dynamic sessions matching local address (+ local port) */
916 if (!(sm->static_mapping_only))
918 u_key.addr = m->local_addr;
919 u_key.fib_index = m->fib_index;
920 kv.key = u_key.as_u64;
921 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
923 user_index = value.value;
924 u = pool_elt_at_index (tsm->users, user_index);
927 head_index = u->sessions_per_user_list_head_index;
928 head = pool_elt_at_index (tsm->list_pool, head_index);
929 elt_index = head->next;
930 elt = pool_elt_at_index (tsm->list_pool, elt_index);
931 ses_index = elt->value;
932 while (ses_index != ~0)
934 s = pool_elt_at_index (tsm->sessions, ses_index);
935 elt = pool_elt_at_index (tsm->list_pool, elt->next);
936 ses_index = elt->value;
938 if (snat_is_session_static (s))
943 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
944 (clib_net_to_host_u16 (s->out2in.port) != e_port))
948 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
949 clib_dlist_remove (tsm->list_pool, s->per_user_index);
950 pool_put_index (tsm->list_pool, s->per_user_index);
951 pool_put (tsm->sessions, s);
965 if (sw_if_index != ~0)
968 return VNET_API_ERROR_NO_SUCH_ENTRY;
971 /* Free external address port */
972 if (!(addr_only || sm->static_mapping_only || out2in_only))
974 for (i = 0; i < vec_len (sm->addresses); i++)
976 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
978 a = sm->addresses + i;
981 #define _(N, j, n, s) \
982 case SNAT_PROTOCOL_##N: \
983 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
986 a->busy_##n##_ports--; \
987 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
990 foreach_snat_protocol
993 clib_warning("unknown_protocol");
994 return VNET_API_ERROR_INVALID_VALUE_2;
1001 if (sm->num_workers > 1)
1002 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1004 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1006 m_key.addr = m->local_addr;
1007 m_key.port = m->local_port;
1008 m_key.protocol = m->proto;
1009 m_key.fib_index = m->fib_index;
1010 kv.key = m_key.as_u64;
1012 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
1013 if (twice_nat || out2in_only)
1015 m_key.port = clib_host_to_net_u16 (m->local_port);
1016 kv.key = m_key.as_u64;
1018 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1019 clib_warning ("in2out key del failed");
1022 m_key.addr = m->external_addr;
1023 m_key.port = m->external_port;
1024 m_key.fib_index = sm->outside_fib_index;
1025 kv.key = m_key.as_u64;
1026 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
1027 if (twice_nat || out2in_only)
1029 m_key.port = clib_host_to_net_u16 (m->external_port);
1030 kv.key = m_key.as_u64;
1032 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1033 clib_warning ("in2out key del failed");
1036 /* Delete session(s) for static mapping if exist */
1037 if (!(sm->static_mapping_only) ||
1038 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1040 u_key.addr = m->local_addr;
1041 u_key.fib_index = m->fib_index;
1042 kv.key = u_key.as_u64;
1043 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1045 user_index = value.value;
1046 u = pool_elt_at_index (tsm->users, user_index);
1047 if (u->nstaticsessions)
1049 head_index = u->sessions_per_user_list_head_index;
1050 head = pool_elt_at_index (tsm->list_pool, head_index);
1051 elt_index = head->next;
1052 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1053 ses_index = elt->value;
1054 while (ses_index != ~0)
1056 s = pool_elt_at_index (tsm->sessions, ses_index);
1057 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1058 ses_index = elt->value;
1062 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
1063 (clib_net_to_host_u16 (s->out2in.port) != e_port))
1067 if (is_lb_session (s))
1070 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1071 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1072 pool_put_index (tsm->list_pool, s->per_user_index);
1073 pool_put (tsm->sessions, s);
1074 u->nstaticsessions--;
1079 if (addr_only && (u->nstaticsessions == 0))
1081 pool_put (tsm->users, u);
1082 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
1089 /* Delete static mapping from pool */
1090 pool_put (sm->static_mappings, m);
1093 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1096 /* Add/delete external address to FIB */
1097 pool_foreach (interface, sm->interfaces,
1099 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1102 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1105 pool_foreach (interface, sm->output_feature_interfaces,
1107 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1110 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1117 static int lb_local_exists (nat44_lb_addr_port_t * local,
1118 ip4_address_t * e_addr, u16 e_port)
1120 snat_main_t *sm = &snat_main;
1121 snat_static_mapping_t *m;
1122 nat44_lb_addr_port_t *ap;
1125 pool_foreach (m, sm->static_mappings,
1127 if (vec_len(m->locals))
1129 if (m->external_port == e_port && m->external_addr.as_u32 == e_addr->as_u32)
1132 vec_foreach (ap, m->locals)
1134 if (ap->port == local->port && ap->addr.as_u32 == local->addr.as_u32)
1144 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1145 snat_protocol_t proto, u32 vrf_id,
1146 nat44_lb_addr_port_t *locals, u8 is_add,
1147 u8 twice_nat, u8 out2in_only, u8 *tag)
1149 snat_main_t * sm = &snat_main;
1150 snat_static_mapping_t *m;
1151 snat_session_key_t m_key;
1152 clib_bihash_kv_8_8_t kv, value;
1154 snat_address_t *a = 0;
1156 nat44_lb_addr_port_t *local;
1157 u32 worker_index = 0, elt_index, head_index, ses_index;
1158 snat_main_per_thread_data_t *tsm;
1159 snat_user_key_t u_key;
1162 dlist_elt_t * head, * elt;
1164 m_key.addr = e_addr;
1165 m_key.port = e_port;
1166 m_key.protocol = proto;
1167 m_key.fib_index = sm->outside_fib_index;
1168 kv.key = m_key.as_u64;
1169 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1172 m = pool_elt_at_index (sm->static_mappings, value.value);
1177 return VNET_API_ERROR_VALUE_EXIST;
1179 if (vec_len (locals) < 2)
1180 return VNET_API_ERROR_INVALID_VALUE;
1182 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1184 FIB_SOURCE_PLUGIN_LOW);
1186 /* Find external address in allocated addresses and reserve port for
1187 address and port pair mapping when dynamic translations enabled */
1188 if (!(sm->static_mapping_only || out2in_only))
1190 for (i = 0; i < vec_len (sm->addresses); i++)
1192 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1194 a = sm->addresses + i;
1195 /* External port must be unused */
1198 #define _(N, j, n, s) \
1199 case SNAT_PROTOCOL_##N: \
1200 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1201 return VNET_API_ERROR_INVALID_VALUE; \
1202 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1203 if (e_port > 1024) \
1205 a->busy_##n##_ports++; \
1206 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1209 foreach_snat_protocol
1212 clib_warning("unknown_protocol");
1213 return VNET_API_ERROR_INVALID_VALUE_2;
1218 /* External address must be allocated */
1220 return VNET_API_ERROR_NO_SUCH_ENTRY;
1223 pool_get (sm->static_mappings, m);
1224 memset (m, 0, sizeof (*m));
1225 m->tag = vec_dup (tag);
1226 m->external_addr = e_addr;
1229 m->fib_index = fib_index;
1230 m->external_port = e_port;
1232 m->twice_nat = twice_nat;
1233 m->out2in_only = out2in_only;
1235 m_key.addr = m->external_addr;
1236 m_key.port = m->external_port;
1237 m_key.protocol = m->proto;
1238 m_key.fib_index = sm->outside_fib_index;
1239 kv.key = m_key.as_u64;
1240 kv.value = m - sm->static_mappings;
1241 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1243 clib_warning ("static_mapping_by_external key add failed");
1244 return VNET_API_ERROR_UNSPECIFIED;
1250 worker_index = sm->first_worker_index +
1251 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1252 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1253 m->worker_index = worker_index;
1256 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1258 m_key.port = clib_host_to_net_u16 (m->external_port);
1259 kv.key = m_key.as_u64;
1261 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1263 clib_warning ("out2in key add failed");
1264 return VNET_API_ERROR_UNSPECIFIED;
1267 m_key.fib_index = m->fib_index;
1268 for (i = 0; i < vec_len (locals); i++)
1270 m_key.addr = locals[i].addr;
1273 m_key.port = locals[i].port;
1274 kv.key = m_key.as_u64;
1275 kv.value = m - sm->static_mappings;
1276 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1278 locals[i].prefix = (i == 0) ? locals[i].probability :\
1279 (locals[i - 1].prefix + locals[i].probability);
1280 vec_add1 (m->locals, locals[i]);
1282 m_key.port = clib_host_to_net_u16 (locals[i].port);
1283 kv.key = m_key.as_u64;
1285 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1287 clib_warning ("in2out key add failed");
1288 return VNET_API_ERROR_UNSPECIFIED;
1295 return VNET_API_ERROR_NO_SUCH_ENTRY;
1297 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1299 /* Free external address port */
1300 if (!(sm->static_mapping_only || out2in_only))
1302 for (i = 0; i < vec_len (sm->addresses); i++)
1304 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1306 a = sm->addresses + i;
1309 #define _(N, j, n, s) \
1310 case SNAT_PROTOCOL_##N: \
1311 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1312 if (e_port > 1024) \
1314 a->busy_##n##_ports--; \
1315 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1318 foreach_snat_protocol
1321 clib_warning("unknown_protocol");
1322 return VNET_API_ERROR_INVALID_VALUE_2;
1329 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1330 m_key.addr = m->external_addr;
1331 m_key.port = m->external_port;
1332 m_key.protocol = m->proto;
1333 m_key.fib_index = sm->outside_fib_index;
1334 kv.key = m_key.as_u64;
1335 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1337 clib_warning ("static_mapping_by_external key del failed");
1338 return VNET_API_ERROR_UNSPECIFIED;
1341 m_key.port = clib_host_to_net_u16 (m->external_port);
1342 kv.key = m_key.as_u64;
1343 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1345 clib_warning ("outi2in key del failed");
1346 return VNET_API_ERROR_UNSPECIFIED;
1349 vec_foreach (local, m->locals)
1351 m_key.addr = local->addr;
1354 m_key.port = local->port;
1355 m_key.fib_index = m->fib_index;
1356 kv.key = m_key.as_u64;
1357 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1359 clib_warning ("static_mapping_by_local key del failed");
1360 return VNET_API_ERROR_UNSPECIFIED;
1364 if (!lb_local_exists(local, &e_addr, e_port))
1366 m_key.port = clib_host_to_net_u16 (local->port);
1367 kv.key = m_key.as_u64;
1368 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1370 clib_warning ("in2out key del failed");
1371 return VNET_API_ERROR_UNSPECIFIED;
1374 /* Delete sessions */
1375 u_key.addr = local->addr;
1376 u_key.fib_index = m->fib_index;
1377 kv.key = u_key.as_u64;
1378 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1380 u = pool_elt_at_index (tsm->users, value.value);
1381 if (u->nstaticsessions)
1383 head_index = u->sessions_per_user_list_head_index;
1384 head = pool_elt_at_index (tsm->list_pool, head_index);
1385 elt_index = head->next;
1386 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1387 ses_index = elt->value;
1388 while (ses_index != ~0)
1390 s = pool_elt_at_index (tsm->sessions, ses_index);
1391 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1392 ses_index = elt->value;
1394 if (!(is_lb_session (s)))
1397 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1398 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1401 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1402 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1403 pool_put_index (tsm->list_pool, s->per_user_index);
1404 pool_put (tsm->sessions, s);
1405 u->nstaticsessions--;
1410 vec_free(m->locals);
1413 pool_put (sm->static_mappings, m);
1420 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1423 snat_address_t *a = 0;
1424 snat_session_t *ses;
1425 u32 *ses_to_be_removed = 0, *ses_index;
1426 clib_bihash_kv_8_8_t kv, value;
1427 snat_user_key_t user_key;
1429 snat_main_per_thread_data_t *tsm;
1430 snat_static_mapping_t *m;
1431 snat_interface_t *interface;
1433 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1435 /* Find SNAT address */
1436 for (i=0; i < vec_len (addresses); i++)
1438 if (addresses[i].addr.as_u32 == addr.as_u32)
1445 return VNET_API_ERROR_NO_SUCH_ENTRY;
1449 pool_foreach (m, sm->static_mappings,
1451 if (m->external_addr.as_u32 == addr.as_u32)
1452 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1453 m->local_port, m->external_port,
1454 m->vrf_id, m->addr_only, ~0,
1455 m->proto, 0, m->twice_nat,
1456 m->out2in_only, m->tag);
1461 /* Check if address is used in some static mapping */
1462 if (is_snat_address_used_in_static_mapping(sm, addr))
1464 clib_warning ("address used in static mapping");
1465 return VNET_API_ERROR_UNSPECIFIED;
1469 if (a->fib_index != ~0)
1470 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1471 FIB_SOURCE_PLUGIN_LOW);
1473 /* Delete sessions using address */
1474 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1476 vec_foreach (tsm, sm->per_thread_data)
1478 pool_foreach (ses, tsm->sessions, ({
1479 if (ses->out2in.addr.as_u32 == addr.as_u32)
1481 ses->outside_address_index = ~0;
1482 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1483 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1484 pool_put_index (tsm->list_pool, ses->per_user_index);
1485 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1486 user_key.addr = ses->in2out.addr;
1487 user_key.fib_index = ses->in2out.fib_index;
1488 kv.key = user_key.as_u64;
1489 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1491 u = pool_elt_at_index (tsm->users, value.value);
1497 vec_foreach (ses_index, ses_to_be_removed)
1498 pool_put_index (tsm->sessions, ses_index[0]);
1500 vec_free (ses_to_be_removed);
1506 vec_del1 (sm->twice_nat_addresses, i);
1510 vec_del1 (sm->addresses, i);
1512 /* Delete external address from FIB */
1513 pool_foreach (interface, sm->interfaces,
1515 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1518 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1521 pool_foreach (interface, sm->output_feature_interfaces,
1523 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1526 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1533 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1535 snat_main_t *sm = &snat_main;
1536 snat_interface_t *i;
1537 const char * feature_name, *del_feature_name;
1538 snat_address_t * ap;
1539 snat_static_mapping_t * m;
1540 snat_det_map_t * dm;
1542 if (sm->out2in_dpo && !is_inside)
1543 return VNET_API_ERROR_UNSUPPORTED;
1545 pool_foreach (i, sm->output_feature_interfaces,
1547 if (i->sw_if_index == sw_if_index)
1548 return VNET_API_ERROR_VALUE_EXIST;
1551 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1552 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1555 if (sm->num_workers > 1 && !sm->deterministic)
1556 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1557 else if (sm->deterministic)
1558 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1560 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1563 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1564 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1566 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1567 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1569 pool_foreach (i, sm->interfaces,
1571 if (i->sw_if_index == sw_if_index)
1575 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1578 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1580 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1582 if (sm->num_workers > 1 && !sm->deterministic)
1584 del_feature_name = "nat44-handoff-classify";
1585 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1586 "nat44-out2in-worker-handoff";
1588 else if (sm->deterministic)
1590 del_feature_name = "nat44-det-classify";
1591 feature_name = !is_inside ? "nat44-det-in2out" :
1596 del_feature_name = "nat44-classify";
1597 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1600 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1601 sw_if_index, 0, 0, 0);
1602 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1603 sw_if_index, 1, 0, 0);
1607 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1608 sw_if_index, 0, 0, 0);
1609 pool_put (sm->interfaces, i);
1614 if ((nat_interface_is_inside(i) && is_inside) ||
1615 (nat_interface_is_outside(i) && !is_inside))
1618 if (sm->num_workers > 1 && !sm->deterministic)
1620 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1621 "nat44-out2in-worker-handoff";
1622 feature_name = "nat44-handoff-classify";
1624 else if (sm->deterministic)
1626 del_feature_name = !is_inside ? "nat44-det-in2out" :
1628 feature_name = "nat44-det-classify";
1632 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1633 feature_name = "nat44-classify";
1636 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1637 sw_if_index, 0, 0, 0);
1638 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1639 sw_if_index, 1, 0, 0);
1648 return VNET_API_ERROR_NO_SUCH_ENTRY;
1650 pool_get (sm->interfaces, i);
1651 i->sw_if_index = sw_if_index;
1653 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1657 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1659 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1661 /* Add/delete external addresses to FIB */
1663 if (is_inside && !sm->out2in_dpo)
1665 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1666 sw_if_index, !is_del, 0, 0);
1670 vec_foreach (ap, sm->addresses)
1671 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1673 pool_foreach (m, sm->static_mappings,
1675 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1678 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1681 pool_foreach (dm, sm->det_maps,
1683 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1689 int snat_interface_add_del_output_feature (u32 sw_if_index,
1693 snat_main_t *sm = &snat_main;
1694 snat_interface_t *i;
1695 snat_address_t * ap;
1696 snat_static_mapping_t * m;
1698 if (sm->deterministic ||
1699 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1700 return VNET_API_ERROR_UNSUPPORTED;
1702 pool_foreach (i, sm->interfaces,
1704 if (i->sw_if_index == sw_if_index)
1705 return VNET_API_ERROR_VALUE_EXIST;
1710 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1711 sw_if_index, !is_del, 0, 0);
1712 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1713 sw_if_index, !is_del, 0, 0);
1717 if (sm->num_workers > 1)
1719 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1720 sw_if_index, !is_del, 0, 0);
1721 vnet_feature_enable_disable ("ip4-output",
1722 "nat44-in2out-output-worker-handoff",
1723 sw_if_index, !is_del, 0, 0);
1727 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1729 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1730 sw_if_index, !is_del, 0, 0);
1734 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1735 sm->fq_in2out_output_index =
1736 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1738 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1739 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1741 pool_foreach (i, sm->output_feature_interfaces,
1743 if (i->sw_if_index == sw_if_index)
1746 pool_put (sm->output_feature_interfaces, i);
1748 return VNET_API_ERROR_VALUE_EXIST;
1755 return VNET_API_ERROR_NO_SUCH_ENTRY;
1757 pool_get (sm->output_feature_interfaces, i);
1758 i->sw_if_index = sw_if_index;
1761 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1763 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1765 /* Add/delete external addresses to FIB */
1770 vec_foreach (ap, sm->addresses)
1771 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1773 pool_foreach (m, sm->static_mappings,
1775 if (!(m->addr_only))
1778 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1784 int snat_set_workers (uword * bitmap)
1786 snat_main_t *sm = &snat_main;
1789 if (sm->num_workers < 2)
1790 return VNET_API_ERROR_FEATURE_DISABLED;
1792 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1793 return VNET_API_ERROR_INVALID_WORKER;
1795 vec_free (sm->workers);
1796 clib_bitmap_foreach (i, bitmap,
1798 vec_add1(sm->workers, i);
1799 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1803 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1804 sm->num_snat_thread = _vec_len (sm->workers);
1811 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1814 ip4_address_t * address,
1816 u32 if_address_index,
1820 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1823 ip4_address_t * address,
1825 u32 if_address_index,
1829 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1832 snat_session_key_t * k,
1833 u32 * address_indexp,
1834 u16 port_per_thread,
1835 u32 snat_thread_index);
1837 static clib_error_t * snat_init (vlib_main_t * vm)
1839 snat_main_t * sm = &snat_main;
1840 clib_error_t * error = 0;
1841 ip4_main_t * im = &ip4_main;
1842 ip_lookup_main_t * lm = &im->lookup_main;
1844 vlib_thread_registration_t *tr;
1845 vlib_thread_main_t *tm = vlib_get_thread_main ();
1848 ip4_add_del_interface_address_callback_t cb4;
1851 sm->vnet_main = vnet_get_main();
1853 sm->ip4_lookup_main = lm;
1854 sm->api_main = &api_main;
1855 sm->first_worker_index = 0;
1856 sm->next_worker = 0;
1857 sm->num_workers = 0;
1858 sm->num_snat_thread = 1;
1860 sm->port_per_thread = 0xffff - 1024;
1861 sm->fq_in2out_index = ~0;
1862 sm->fq_out2in_index = ~0;
1863 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1864 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1865 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1866 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1867 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1868 sm->forwarding_enabled = 0;
1870 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1873 tr = (vlib_thread_registration_t *) p[0];
1876 sm->num_workers = tr->count;
1877 sm->first_worker_index = tr->first_index;
1881 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1883 /* Use all available workers by default */
1884 if (sm->num_workers > 1)
1886 for (i=0; i < sm->num_workers; i++)
1887 bitmap = clib_bitmap_set (bitmap, i, 1);
1888 snat_set_workers(bitmap);
1889 clib_bitmap_free (bitmap);
1893 sm->per_thread_data[0].snat_thread_index = 0;
1896 error = snat_api_init(vm, sm);
1900 /* Set up the interface address add/del callback */
1901 cb4.function = snat_ip4_add_del_interface_address_cb;
1902 cb4.function_opaque = 0;
1904 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1906 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
1907 cb4.function_opaque = 0;
1909 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1911 nat_dpo_module_init ();
1913 /* Init IPFIX logging */
1914 snat_ipfix_logging_init(vm);
1917 error = nat64_init(vm);
1925 /* Init virtual fragmenentation reassembly */
1926 return nat_reass_init(vm);
1929 VLIB_INIT_FUNCTION (snat_init);
1931 void snat_free_outside_address_and_port (snat_address_t * addresses,
1933 snat_session_key_t * k,
1937 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1939 ASSERT (address_index < vec_len (addresses));
1941 a = addresses + address_index;
1943 switch (k->protocol)
1945 #define _(N, i, n, s) \
1946 case SNAT_PROTOCOL_##N: \
1947 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1948 port_host_byte_order) == 1); \
1949 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1950 port_host_byte_order, 0); \
1951 a->busy_##n##_ports--; \
1952 a->busy_##n##_ports_per_thread[thread_index]--; \
1954 foreach_snat_protocol
1957 clib_warning("unknown_protocol");
1963 * @brief Match NAT44 static mapping.
1965 * @param sm NAT main.
1966 * @param match Address and port to match.
1967 * @param mapping External or local address and port of the matched mapping.
1968 * @param by_external If 0 match by local address otherwise match by external
1970 * @param is_addr_only If matched mapping is address only
1971 * @param twice_nat If matched mapping is twice NAT.
1972 * @param lb If matched mapping is load-balanced.
1974 * @returns 0 if match found otherwise 1.
1976 int snat_static_mapping_match (snat_main_t * sm,
1977 snat_session_key_t match,
1978 snat_session_key_t * mapping,
1984 clib_bihash_kv_8_8_t kv, value;
1985 snat_static_mapping_t *m;
1986 snat_session_key_t m_key;
1987 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1988 u32 rand, lo = 0, hi, mid;
1991 mapping_hash = &sm->static_mapping_by_external;
1993 m_key.addr = match.addr;
1994 m_key.port = clib_net_to_host_u16 (match.port);
1995 m_key.protocol = match.protocol;
1996 m_key.fib_index = match.fib_index;
1998 kv.key = m_key.as_u64;
2000 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2002 /* Try address only mapping */
2005 kv.key = m_key.as_u64;
2006 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2010 m = pool_elt_at_index (sm->static_mappings, value.value);
2014 if (vec_len (m->locals))
2016 hi = vec_len (m->locals) - 1;
2017 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2020 mid = ((hi - lo) >> 1) + lo;
2021 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2023 if (!(m->locals[lo].prefix >= rand))
2025 mapping->addr = m->locals[lo].addr;
2026 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2030 mapping->addr = m->local_addr;
2031 /* Address only mapping doesn't change port */
2032 mapping->port = m->addr_only ? match.port
2033 : clib_host_to_net_u16 (m->local_port);
2035 mapping->fib_index = m->fib_index;
2036 mapping->protocol = m->proto;
2040 mapping->addr = m->external_addr;
2041 /* Address only mapping doesn't change port */
2042 mapping->port = m->addr_only ? match.port
2043 : clib_host_to_net_u16 (m->external_port);
2044 mapping->fib_index = sm->outside_fib_index;
2047 if (PREDICT_FALSE(is_addr_only != 0))
2048 *is_addr_only = m->addr_only;
2050 if (PREDICT_FALSE(twice_nat != 0))
2051 *twice_nat = m->twice_nat;
2053 if (PREDICT_FALSE(lb != 0))
2054 *lb = vec_len (m->locals) > 0;
2059 static_always_inline u16
2060 snat_random_port (u16 min, u16 max)
2062 snat_main_t *sm = &snat_main;
2063 return min + random_u32 (&sm->random_seed) /
2064 (random_u32_max() / (max - min + 1) + 1);
2068 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2071 snat_session_key_t * k,
2072 u32 * address_indexp,
2073 u16 port_per_thread,
2074 u32 snat_thread_index)
2076 snat_main_t *sm = &snat_main;
2078 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
2079 address_indexp, port_per_thread,
2084 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2087 snat_session_key_t * k,
2088 u32 * address_indexp,
2089 u16 port_per_thread,
2090 u32 snat_thread_index)
2093 snat_address_t *a, *ga = 0;
2096 for (i = 0; i < vec_len (addresses); i++)
2099 switch (k->protocol)
2101 #define _(N, j, n, s) \
2102 case SNAT_PROTOCOL_##N: \
2103 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2105 if (a->fib_index == fib_index) \
2109 portnum = (port_per_thread * \
2110 snat_thread_index) + \
2111 snat_random_port(1, port_per_thread) + 1024; \
2112 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2114 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2115 a->busy_##n##_ports_per_thread[thread_index]++; \
2116 a->busy_##n##_ports++; \
2117 k->addr = a->addr; \
2118 k->port = clib_host_to_net_u16(portnum); \
2119 *address_indexp = i; \
2123 else if (a->fib_index == ~0) \
2130 foreach_snat_protocol
2133 clib_warning("unknown protocol");
2142 switch (k->protocol)
2144 #define _(N, j, n, s) \
2145 case SNAT_PROTOCOL_##N: \
2148 portnum = (port_per_thread * \
2149 snat_thread_index) + \
2150 snat_random_port(1, port_per_thread) + 1024; \
2151 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2153 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2154 a->busy_##n##_ports_per_thread[thread_index]++; \
2155 a->busy_##n##_ports++; \
2156 k->addr = a->addr; \
2157 k->port = clib_host_to_net_u16(portnum); \
2158 *address_indexp = gi; \
2162 foreach_snat_protocol
2165 clib_warning ("unknown protocol");
2170 /* Totally out of translations to use... */
2171 snat_ipfix_logging_addresses_exhausted(0);
2176 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2179 snat_session_key_t * k,
2180 u32 * address_indexp,
2181 u16 port_per_thread,
2182 u32 snat_thread_index)
2184 snat_main_t *sm = &snat_main;
2185 snat_address_t *a = addresses;
2186 u16 m, ports, portnum, A, j;
2187 m = 16 - (sm->psid_offset + sm->psid_length);
2188 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2190 if (!vec_len (addresses))
2193 switch (k->protocol)
2195 #define _(N, i, n, s) \
2196 case SNAT_PROTOCOL_##N: \
2197 if (a->busy_##n##_ports < ports) \
2201 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2202 j = snat_random_port(0, pow2_mask(m)); \
2203 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2204 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2206 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2207 a->busy_##n##_ports++; \
2208 k->addr = a->addr; \
2209 k->port = clib_host_to_net_u16 (portnum); \
2210 *address_indexp = i; \
2215 foreach_snat_protocol
2218 clib_warning("unknown protocol");
2223 /* Totally out of translations to use... */
2224 snat_ipfix_logging_addresses_exhausted(0);
2229 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2231 dpo_id_t dpo_v4 = DPO_INVALID;
2232 fib_prefix_t pfx = {
2233 .fp_proto = FIB_PROTOCOL_IP4,
2235 .fp_addr.ip4.as_u32 = addr.as_u32,
2240 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2241 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2242 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2243 dpo_reset (&dpo_v4);
2247 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2252 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2254 u32 *r = va_arg (*args, u32 *);
2257 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2258 foreach_snat_protocol
2266 format_snat_protocol (u8 * s, va_list * args)
2268 u32 i = va_arg (*args, u32);
2273 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2274 foreach_snat_protocol
2277 s = format (s, "unknown");
2280 s = format (s, "%s", t);
2285 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2287 snat_main_t *sm = &snat_main;
2288 u32 next_worker_index = 0;
2291 next_worker_index = sm->first_worker_index;
2292 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2293 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2295 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2296 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2298 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2300 return next_worker_index;
2304 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2306 snat_main_t *sm = &snat_main;
2309 snat_session_key_t m_key;
2310 clib_bihash_kv_8_8_t kv, value;
2311 snat_static_mapping_t *m;
2312 nat_ed_ses_key_t key;
2313 clib_bihash_kv_16_8_t s_kv, s_value;
2314 snat_main_per_thread_data_t *tsm;
2318 u32 next_worker_index = 0;
2320 /* first try static mappings without port */
2321 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2323 m_key.addr = ip0->dst_address;
2326 m_key.fib_index = rx_fib_index0;
2327 kv.key = m_key.as_u64;
2328 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2330 m = pool_elt_at_index (sm->static_mappings, value.value);
2331 return m->worker_index;
2335 proto = ip_proto_to_snat_proto (ip0->protocol);
2336 udp = ip4_next_header (ip0);
2337 port = udp->dst_port;
2339 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2341 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2342 return vlib_get_thread_index ();
2344 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2346 nat_reass_ip4_t *reass;
2348 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2349 ip0->fragment_id, ip0->protocol);
2351 if (reass && (reass->thread_index != (u32) ~ 0))
2352 return reass->thread_index;
2354 return vlib_get_thread_index ();
2358 /* unknown protocol */
2359 if (PREDICT_FALSE (proto == ~0))
2361 key.l_addr = ip0->dst_address;
2362 key.r_addr = ip0->src_address;
2363 key.fib_index = rx_fib_index0;
2364 key.proto = ip0->protocol;
2367 s_kv.key[0] = key.as_u64[0];
2368 s_kv.key[1] = key.as_u64[1];
2370 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2372 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2374 tsm = vec_elt_at_index (sm->per_thread_data, i);
2375 if (!pool_is_free_index(tsm->sessions, s_value.value))
2377 s = pool_elt_at_index (tsm->sessions, s_value.value);
2378 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2379 s->out2in.port == ip0->protocol &&
2380 snat_is_unk_proto_session (s))
2386 /* if no session use current thread */
2387 return vlib_get_thread_index ();
2390 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2392 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2393 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2394 if (!icmp_is_error_message (icmp))
2395 port = echo->identifier;
2398 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2399 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2400 void *l4_header = ip4_next_header (inner_ip);
2403 case SNAT_PROTOCOL_ICMP:
2404 icmp = (icmp46_header_t*)l4_header;
2405 echo = (icmp_echo_header_t *)(icmp + 1);
2406 port = echo->identifier;
2408 case SNAT_PROTOCOL_UDP:
2409 case SNAT_PROTOCOL_TCP:
2410 port = ((tcp_udp_header_t*)l4_header)->src_port;
2413 return vlib_get_thread_index ();
2418 /* try static mappings with port */
2419 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2421 m_key.addr = ip0->dst_address;
2422 m_key.port = clib_net_to_host_u16 (port);
2423 m_key.protocol = proto;
2424 m_key.fib_index = rx_fib_index0;
2425 kv.key = m_key.as_u64;
2426 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2428 m = pool_elt_at_index (sm->static_mappings, value.value);
2429 return m->worker_index;
2433 /* worker by outside port */
2434 next_worker_index = sm->first_worker_index;
2435 next_worker_index +=
2436 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2437 return next_worker_index;
2440 static clib_error_t *
2441 snat_config (vlib_main_t * vm, unformat_input_t * input)
2443 snat_main_t * sm = &snat_main;
2444 u32 translation_buckets = 1024;
2445 u32 translation_memory_size = 128<<20;
2446 u32 user_buckets = 128;
2447 u32 user_memory_size = 64<<20;
2448 u32 max_translations_per_user = 100;
2449 u32 outside_vrf_id = 0;
2450 u32 inside_vrf_id = 0;
2451 u32 static_mapping_buckets = 1024;
2452 u32 static_mapping_memory_size = 64<<20;
2453 u32 nat64_bib_buckets = 1024;
2454 u32 nat64_bib_memory_size = 128 << 20;
2455 u32 nat64_st_buckets = 2048;
2456 u32 nat64_st_memory_size = 256 << 20;
2457 u8 static_mapping_only = 0;
2458 u8 static_mapping_connection_tracking = 0;
2459 snat_main_per_thread_data_t *tsm;
2460 dslite_main_t * dm = &dslite_main;
2462 sm->deterministic = 0;
2465 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2467 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2469 else if (unformat (input, "translation hash memory %d",
2470 &translation_memory_size));
2471 else if (unformat (input, "user hash buckets %d", &user_buckets))
2473 else if (unformat (input, "user hash memory %d",
2476 else if (unformat (input, "max translations per user %d",
2477 &max_translations_per_user))
2479 else if (unformat (input, "outside VRF id %d",
2482 else if (unformat (input, "inside VRF id %d",
2485 else if (unformat (input, "static mapping only"))
2487 static_mapping_only = 1;
2488 if (unformat (input, "connection tracking"))
2489 static_mapping_connection_tracking = 1;
2491 else if (unformat (input, "deterministic"))
2492 sm->deterministic = 1;
2493 else if (unformat (input, "nat64 bib hash buckets %d",
2494 &nat64_bib_buckets))
2496 else if (unformat (input, "nat64 bib hash memory %d",
2497 &nat64_bib_memory_size))
2499 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2501 else if (unformat (input, "nat64 st hash memory %d",
2502 &nat64_st_memory_size))
2504 else if (unformat (input, "out2in dpo"))
2506 else if (unformat (input, "dslite ce"))
2507 dslite_set_ce(dm, 1);
2509 return clib_error_return (0, "unknown input '%U'",
2510 format_unformat_error, input);
2513 /* for show commands, etc. */
2514 sm->translation_buckets = translation_buckets;
2515 sm->translation_memory_size = translation_memory_size;
2516 /* do not exceed load factor 10 */
2517 sm->max_translations = 10 * translation_buckets;
2518 sm->user_buckets = user_buckets;
2519 sm->user_memory_size = user_memory_size;
2520 sm->max_translations_per_user = max_translations_per_user;
2521 sm->outside_vrf_id = outside_vrf_id;
2522 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2524 FIB_SOURCE_PLUGIN_HI);
2525 sm->inside_vrf_id = inside_vrf_id;
2526 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2528 FIB_SOURCE_PLUGIN_HI);
2529 sm->static_mapping_only = static_mapping_only;
2530 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2532 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2533 nat64_st_memory_size);
2535 if (sm->deterministic)
2537 sm->in2out_node_index = snat_det_in2out_node.index;
2538 sm->in2out_output_node_index = ~0;
2539 sm->out2in_node_index = snat_det_out2in_node.index;
2540 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2541 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2545 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2546 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2547 sm->in2out_node_index = snat_in2out_node.index;
2548 sm->in2out_output_node_index = snat_in2out_output_node.index;
2549 sm->out2in_node_index = snat_out2in_node.index;
2550 if (!static_mapping_only ||
2551 (static_mapping_only && static_mapping_connection_tracking))
2553 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2554 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2556 vec_foreach (tsm, sm->per_thread_data)
2558 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2559 translation_memory_size);
2561 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2562 translation_memory_size);
2564 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2568 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2569 translation_buckets, translation_memory_size);
2571 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2572 translation_buckets, translation_memory_size);
2576 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2577 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2579 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2580 "static_mapping_by_local", static_mapping_buckets,
2581 static_mapping_memory_size);
2583 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2584 "static_mapping_by_external", static_mapping_buckets,
2585 static_mapping_memory_size);
2591 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2593 u8 * format_snat_session_state (u8 * s, va_list * args)
2595 u32 i = va_arg (*args, u32);
2600 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2601 foreach_snat_session_state
2604 t = format (t, "unknown");
2606 s = format (s, "%s", t);
2610 u8 * format_snat_key (u8 * s, va_list * args)
2612 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2614 s = format (s, "%U proto %U port %d fib %d",
2615 format_ip4_address, &key->addr,
2616 format_snat_protocol, key->protocol,
2617 clib_net_to_host_u16 (key->port), key->fib_index);
2621 u8 * format_snat_session (u8 * s, va_list * args)
2623 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2624 snat_session_t * sess = va_arg (*args, snat_session_t *);
2626 if (snat_is_unk_proto_session (sess))
2628 s = format (s, " i2o %U proto %u fib %u\n",
2629 format_ip4_address, &sess->in2out.addr,
2630 clib_net_to_host_u16 (sess->in2out.port),
2631 sess->in2out.fib_index);
2632 s = format (s, " o2i %U proto %u fib %u\n",
2633 format_ip4_address, &sess->out2in.addr,
2634 clib_net_to_host_u16 (sess->out2in.port),
2635 sess->out2in.fib_index);
2639 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2640 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2642 if (is_twice_nat_session (sess))
2644 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2645 format_ip4_address, &sess->ext_host_addr,
2646 clib_net_to_host_u16 (sess->ext_host_port),
2647 format_ip4_address, &sess->ext_host_nat_addr,
2648 clib_net_to_host_u16 (sess->ext_host_nat_port));
2652 if (sess->ext_host_addr.as_u32)
2653 s = format (s, " external host %U\n",
2654 format_ip4_address, &sess->ext_host_addr);
2656 s = format (s, " last heard %.2f\n", sess->last_heard);
2657 s = format (s, " total pkts %d, total bytes %lld\n",
2658 sess->total_pkts, sess->total_bytes);
2659 if (snat_is_session_static (sess))
2660 s = format (s, " static translation\n");
2662 s = format (s, " dynamic translation\n");
2663 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2664 s = format (s, " load-balancing\n");
2665 if (is_twice_nat_session (sess))
2666 s = format (s, " twice-nat\n");
2671 u8 * format_snat_user (u8 * s, va_list * args)
2673 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2674 snat_user_t * u = va_arg (*args, snat_user_t *);
2675 int verbose = va_arg (*args, int);
2676 dlist_elt_t * head, * elt;
2677 u32 elt_index, head_index;
2679 snat_session_t * sess;
2681 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2682 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2687 if (u->nsessions || u->nstaticsessions)
2689 head_index = u->sessions_per_user_list_head_index;
2690 head = pool_elt_at_index (sm->list_pool, head_index);
2692 elt_index = head->next;
2693 elt = pool_elt_at_index (sm->list_pool, elt_index);
2694 session_index = elt->value;
2696 while (session_index != ~0)
2698 sess = pool_elt_at_index (sm->sessions, session_index);
2700 s = format (s, " %U\n", format_snat_session, sm, sess);
2702 elt_index = elt->next;
2703 elt = pool_elt_at_index (sm->list_pool, elt_index);
2704 session_index = elt->value;
2711 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2713 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2714 nat44_lb_addr_port_t *local;
2717 s = format (s, "local %U external %U vrf %d %s",
2718 format_ip4_address, &m->local_addr,
2719 format_ip4_address, &m->external_addr,
2720 m->vrf_id, m->twice_nat ? "twice-nat" : "");
2723 if (vec_len (m->locals))
2725 s = format (s, "%U vrf %d external %U:%d %s %s",
2726 format_snat_protocol, m->proto,
2728 format_ip4_address, &m->external_addr, m->external_port,
2729 m->twice_nat ? "twice-nat" : "",
2730 m->out2in_only ? "out2in-only" : "");
2731 vec_foreach (local, m->locals)
2732 s = format (s, "\n local %U:%d probability %d\%",
2733 format_ip4_address, &local->addr, local->port,
2734 local->probability);
2737 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
2738 format_snat_protocol, m->proto,
2739 format_ip4_address, &m->local_addr, m->local_port,
2740 format_ip4_address, &m->external_addr, m->external_port,
2741 m->vrf_id, m->twice_nat ? "twice-nat" : "",
2742 m->out2in_only ? "out2in-only" : "");
2747 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2749 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2750 vnet_main_t *vnm = vnet_get_main();
2753 s = format (s, "local %U external %U vrf %d",
2754 format_ip4_address, &m->l_addr,
2755 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2758 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2759 format_snat_protocol, m->proto,
2760 format_ip4_address, &m->l_addr, m->l_port,
2761 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2762 m->e_port, m->vrf_id);
2767 u8 * format_det_map_ses (u8 * s, va_list * args)
2769 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2770 ip4_address_t in_addr, out_addr;
2771 u32 in_offset, out_offset;
2772 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2773 u32 * i = va_arg (*args, u32 *);
2775 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2776 in_addr.as_u32 = clib_host_to_net_u32 (
2777 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2778 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2779 clib_net_to_host_u32(det_map->in_addr.as_u32);
2780 out_offset = in_offset / det_map->sharing_ratio;
2781 out_addr.as_u32 = clib_host_to_net_u32(
2782 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2783 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2784 format_ip4_address, &in_addr,
2785 clib_net_to_host_u16 (ses->in_port),
2786 format_ip4_address, &out_addr,
2787 clib_net_to_host_u16 (ses->out.out_port),
2788 format_ip4_address, &ses->out.ext_host_addr,
2789 clib_net_to_host_u16 (ses->out.ext_host_port),
2790 format_snat_session_state, ses->state,
2797 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2800 ip4_address_t * address,
2802 u32 if_address_index,
2805 snat_main_t *sm = &snat_main;
2806 snat_static_map_resolve_t *rp;
2807 snat_static_mapping_t *m;
2808 snat_session_key_t m_key;
2809 clib_bihash_kv_8_8_t kv, value;
2811 ip4_address_t l_addr;
2813 for (i = 0; i < vec_len (sm->to_resolve); i++)
2815 rp = sm->to_resolve + i;
2816 if (rp->addr_only == 0)
2818 if (rp->sw_if_index == sw_if_index)
2825 m_key.addr.as_u32 = address->as_u32;
2826 m_key.port = rp->addr_only ? 0 : rp->e_port;
2827 m_key.protocol = rp->addr_only ? 0 : rp->proto;
2828 m_key.fib_index = sm->outside_fib_index;
2829 kv.key = m_key.as_u64;
2830 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2833 m = pool_elt_at_index (sm->static_mappings, value.value);
2837 /* Don't trip over lease renewal, static config */
2847 /* Indetity mapping? */
2848 if (rp->l_addr.as_u32 == 0)
2849 l_addr.as_u32 = address[0].as_u32;
2851 l_addr.as_u32 = rp->l_addr.as_u32;
2852 /* Add the static mapping */
2853 rv = snat_add_static_mapping (l_addr,
2859 ~0 /* sw_if_index */,
2864 clib_warning ("snat_add_static_mapping returned %d", rv);
2868 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2871 ip4_address_t * address,
2873 u32 if_address_index,
2876 snat_main_t *sm = &snat_main;
2877 snat_static_map_resolve_t *rp;
2878 ip4_address_t l_addr;
2882 snat_address_t *addresses = sm->addresses;
2884 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2886 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2890 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
2893 addresses = sm->twice_nat_addresses;
2894 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
2903 /* Don't trip over lease renewal, static config */
2904 for (j = 0; j < vec_len(addresses); j++)
2905 if (addresses[j].addr.as_u32 == address->as_u32)
2908 snat_add_address (sm, address, ~0, twice_nat);
2909 /* Scan static map resolution vector */
2910 for (j = 0; j < vec_len (sm->to_resolve); j++)
2912 rp = sm->to_resolve + j;
2915 /* On this interface? */
2916 if (rp->sw_if_index == sw_if_index)
2918 /* Indetity mapping? */
2919 if (rp->l_addr.as_u32 == 0)
2920 l_addr.as_u32 = address[0].as_u32;
2922 l_addr.as_u32 = rp->l_addr.as_u32;
2923 /* Add the static mapping */
2924 rv = snat_add_static_mapping (l_addr,
2930 ~0 /* sw_if_index */,
2935 clib_warning ("snat_add_static_mapping returned %d",
2943 (void) snat_del_address(sm, address[0], 1, twice_nat);
2949 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
2952 ip4_main_t * ip4_main = sm->ip4_main;
2953 ip4_address_t * first_int_addr;
2954 snat_static_map_resolve_t *rp;
2955 u32 *indices_to_delete = 0;
2957 u32 *auto_add_sw_if_indices =
2958 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
2960 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2961 0 /* just want the address*/);
2963 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
2965 if (auto_add_sw_if_indices[i] == sw_if_index)
2969 /* if have address remove it */
2971 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
2974 for (j = 0; j < vec_len (sm->to_resolve); j++)
2976 rp = sm->to_resolve + j;
2977 if (rp->sw_if_index == sw_if_index)
2978 vec_add1 (indices_to_delete, j);
2980 if (vec_len(indices_to_delete))
2982 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2983 vec_del1(sm->to_resolve, j);
2984 vec_free(indices_to_delete);
2988 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
2990 vec_del1(sm->auto_add_sw_if_indices, i);
2993 return VNET_API_ERROR_VALUE_EXIST;
3000 return VNET_API_ERROR_NO_SUCH_ENTRY;
3002 /* add to the auto-address list */
3004 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3006 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3008 /* If the address is already bound - or static - add it now */
3010 snat_add_address (sm, first_int_addr, ~0, twice_nat);
3016 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3017 snat_protocol_t proto, u32 vrf_id, int is_in)
3019 snat_main_per_thread_data_t *tsm;
3020 clib_bihash_kv_8_8_t kv, value;
3022 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3023 snat_session_key_t key;
3025 clib_bihash_8_8_t *t;
3026 snat_user_key_t u_key;
3029 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3030 if (sm->num_workers > 1)
3032 vec_elt_at_index (sm->per_thread_data,
3033 sm->worker_in2out_cb (&ip, fib_index));
3035 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3037 key.addr.as_u32 = addr->as_u32;
3038 key.port = clib_host_to_net_u16 (port);
3039 key.protocol = proto;
3040 key.fib_index = fib_index;
3041 kv.key = key.as_u64;
3042 t = is_in ? &tsm->in2out : &tsm->out2in;
3043 if (!clib_bihash_search_8_8 (t, &kv, &value))
3045 s = pool_elt_at_index (tsm->sessions, value.value);
3046 kv.key = s->in2out.as_u64;
3047 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
3048 kv.key = s->out2in.as_u64;
3049 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
3050 u_key.addr = s->in2out.addr;
3051 u_key.fib_index = s->in2out.fib_index;
3052 kv.key = u_key.as_u64;
3053 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
3055 u = pool_elt_at_index (tsm->users, value.value);
3058 clib_dlist_remove (tsm->list_pool, s->per_user_index);
3059 pool_put (tsm->sessions, s);
3063 return VNET_API_ERROR_NO_SUCH_ENTRY;
3067 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3069 snat_main_t *sm = &snat_main;
3071 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3073 sm->psid_offset = psid_offset;
3074 sm->psid_length = psid_length;
3078 nat_set_alloc_addr_and_port_default (void)
3080 snat_main_t *sm = &snat_main;
3082 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;