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 if (is_fwd_bypass_session (s))
157 ed_key.l_addr = s->in2out.addr;
158 ed_key.r_addr = s->ext_host_addr;
159 ed_key.l_port = s->in2out.port;
160 ed_key.r_port = s->ext_host_port;
161 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
162 ed_key.fib_index = 0;
163 ed_kv.key[0] = ed_key.as_u64[0];
164 ed_kv.key[1] = ed_key.as_u64[1];
165 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &ed_kv, 0))
166 clib_warning ("in2out_ed key del failed");
170 /* Endpoint dependent session lookup tables */
171 if (is_ed_session (s))
173 ed_key.l_addr = s->out2in.addr;
174 ed_key.r_addr = s->ext_host_addr;
175 ed_key.fib_index = s->out2in.fib_index;
176 if (snat_is_unk_proto_session (s))
178 ed_key.proto = s->in2out.port;
184 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
185 ed_key.l_port = s->out2in.port;
186 ed_key.r_port = s->ext_host_port;
188 ed_kv.key[0] = ed_key.as_u64[0];
189 ed_kv.key[1] = ed_key.as_u64[1];
190 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
191 clib_warning ("out2in_ed key del failed");
193 ed_key.l_addr = s->in2out.addr;
194 ed_key.fib_index = s->in2out.fib_index;
195 if (!snat_is_unk_proto_session (s))
196 ed_key.l_port = s->in2out.port;
197 if (is_twice_nat_session (s))
199 ed_key.r_addr = s->ext_host_nat_addr;
200 ed_key.r_port = s->ext_host_nat_port;
202 ed_kv.key[0] = ed_key.as_u64[0];
203 ed_kv.key[1] = ed_key.as_u64[1];
204 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &ed_kv, 0))
205 clib_warning ("in2out_ed key del failed");
208 if (snat_is_unk_proto_session (s))
212 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
213 s->out2in.addr.as_u32,
217 s->in2out.fib_index);
219 /* Twice NAT address and port for external host */
220 if (is_twice_nat_session (s))
222 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
224 key.protocol = s->in2out.protocol;
225 key.port = s->ext_host_nat_port;
226 a = sm->twice_nat_addresses + i;
227 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
229 snat_free_outside_address_and_port (sm->twice_nat_addresses,
230 thread_index, &key, i);
236 if (is_ed_session (s))
239 /* Session lookup tables */
240 kv.key = s->in2out.as_u64;
241 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
242 clib_warning ("in2out key del failed");
243 kv.key = s->out2in.as_u64;
244 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
245 clib_warning ("out2in key del failed");
247 if (snat_is_session_static (s))
250 if (s->outside_address_index != ~0)
251 snat_free_outside_address_and_port (sm->addresses, thread_index,
252 &s->out2in, s->outside_address_index);
256 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
260 snat_user_key_t user_key;
261 clib_bihash_kv_8_8_t kv, value;
262 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
263 dlist_elt_t * per_user_list_head_elt;
265 user_key.addr.as_u32 = addr->as_u32;
266 user_key.fib_index = fib_index;
267 kv.key = user_key.as_u64;
269 /* Ever heard of the "user" = src ip4 address before? */
270 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
272 /* no, make a new one */
273 pool_get (tsm->users, u);
274 memset (u, 0, sizeof (*u));
275 u->addr.as_u32 = addr->as_u32;
276 u->fib_index = fib_index;
278 pool_get (tsm->list_pool, per_user_list_head_elt);
280 u->sessions_per_user_list_head_index = per_user_list_head_elt -
283 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
285 kv.value = u - tsm->users;
288 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
289 clib_warning ("user_hash keay add failed");
293 u = pool_elt_at_index (tsm->users, value.value);
300 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
303 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
304 u32 oldest_per_user_translation_list_index, session_index;
305 dlist_elt_t * oldest_per_user_translation_list_elt;
306 dlist_elt_t * per_user_translation_list_elt;
308 /* Over quota? Recycle the least recently used translation */
309 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
311 oldest_per_user_translation_list_index =
312 clib_dlist_remove_head (tsm->list_pool,
313 u->sessions_per_user_list_head_index);
315 ASSERT (oldest_per_user_translation_list_index != ~0);
317 /* Add it back to the end of the LRU list */
318 clib_dlist_addtail (tsm->list_pool,
319 u->sessions_per_user_list_head_index,
320 oldest_per_user_translation_list_index);
321 /* Get the list element */
322 oldest_per_user_translation_list_elt =
323 pool_elt_at_index (tsm->list_pool,
324 oldest_per_user_translation_list_index);
326 /* Get the session index from the list element */
327 session_index = oldest_per_user_translation_list_elt->value;
329 /* Get the session */
330 s = pool_elt_at_index (tsm->sessions, session_index);
331 nat_free_session_data (sm, s, thread_index);
332 s->outside_address_index = ~0;
339 pool_get (tsm->sessions, s);
340 memset (s, 0, sizeof (*s));
341 s->outside_address_index = ~0;
343 /* Create list elts */
344 pool_get (tsm->list_pool, per_user_translation_list_elt);
345 clib_dlist_init (tsm->list_pool,
346 per_user_translation_list_elt - tsm->list_pool);
348 per_user_translation_list_elt->value = s - tsm->sessions;
349 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
350 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
352 clib_dlist_addtail (tsm->list_pool,
353 s->per_user_list_head_index,
354 per_user_translation_list_elt - tsm->list_pool);
361 nat44_classify_node_fn_inline (vlib_main_t * vm,
362 vlib_node_runtime_t * node,
363 vlib_frame_t * frame)
365 u32 n_left_from, * from, * to_next;
366 nat44_classify_next_t next_index;
367 snat_main_t *sm = &snat_main;
368 snat_static_mapping_t *m;
370 from = vlib_frame_vector_args (frame);
371 n_left_from = frame->n_vectors;
372 next_index = node->cached_next_index;
374 while (n_left_from > 0)
378 vlib_get_next_frame (vm, node, next_index,
379 to_next, n_left_to_next);
381 while (n_left_from > 0 && n_left_to_next > 0)
385 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
388 snat_session_key_t m_key0;
389 clib_bihash_kv_8_8_t kv0, value0;
391 /* speculatively enqueue b0 to the current next frame */
399 b0 = vlib_get_buffer (vm, bi0);
400 ip0 = vlib_buffer_get_current (b0);
402 vec_foreach (ap, sm->addresses)
404 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
406 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
411 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
413 m_key0.addr = ip0->dst_address;
416 m_key0.fib_index = sm->outside_fib_index;
417 kv0.key = m_key0.as_u64;
418 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
420 m = pool_elt_at_index (sm->static_mappings, value0.value);
421 if (m->local_addr.as_u32 != m->external_addr.as_u32)
422 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
425 udp_header_t * udp0 = ip4_next_header (ip0);
426 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
427 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
428 kv0.key = m_key0.as_u64;
429 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
431 m = pool_elt_at_index (sm->static_mappings, value0.value);
432 if (m->local_addr.as_u32 != m->external_addr.as_u32)
433 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
438 /* verify speculative enqueue, maybe switch current next frame */
439 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
440 to_next, n_left_to_next,
444 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
447 return frame->n_vectors;
451 nat44_classify_node_fn (vlib_main_t * vm,
452 vlib_node_runtime_t * node,
453 vlib_frame_t * frame)
455 return nat44_classify_node_fn_inline (vm, node, frame);
458 VLIB_REGISTER_NODE (nat44_classify_node) = {
459 .function = nat44_classify_node_fn,
460 .name = "nat44-classify",
461 .vector_size = sizeof (u32),
462 .type = VLIB_NODE_TYPE_INTERNAL,
463 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
465 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
466 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
470 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
471 nat44_classify_node_fn);
474 nat44_det_classify_node_fn (vlib_main_t * vm,
475 vlib_node_runtime_t * node,
476 vlib_frame_t * frame)
478 return nat44_classify_node_fn_inline (vm, node, frame);
481 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
482 .function = nat44_det_classify_node_fn,
483 .name = "nat44-det-classify",
484 .vector_size = sizeof (u32),
485 .type = VLIB_NODE_TYPE_INTERNAL,
486 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
488 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
489 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
493 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
494 nat44_det_classify_node_fn);
497 nat44_handoff_classify_node_fn (vlib_main_t * vm,
498 vlib_node_runtime_t * node,
499 vlib_frame_t * frame)
501 return nat44_classify_node_fn_inline (vm, node, frame);
504 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
505 .function = nat44_handoff_classify_node_fn,
506 .name = "nat44-handoff-classify",
507 .vector_size = sizeof (u32),
508 .type = VLIB_NODE_TYPE_INTERNAL,
509 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
511 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
512 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
516 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
517 nat44_handoff_classify_node_fn);
520 * @brief Add/del NAT address to FIB.
522 * Add the external NAT address to the FIB as receive entries. This ensures
523 * that VPP will reply to ARP for this address and we don't need to enable
524 * proxy ARP on the outside interface.
526 * @param addr IPv4 address.
527 * @param plen address prefix length
528 * @param sw_if_index Interface.
529 * @param is_add If 0 delete, otherwise add.
532 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
535 fib_prefix_t prefix = {
537 .fp_proto = FIB_PROTOCOL_IP4,
539 .ip4.as_u32 = addr->as_u32,
542 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
545 fib_table_entry_update_one_path(fib_index,
547 FIB_SOURCE_PLUGIN_LOW,
548 (FIB_ENTRY_FLAG_CONNECTED |
549 FIB_ENTRY_FLAG_LOCAL |
550 FIB_ENTRY_FLAG_EXCLUSIVE),
557 FIB_ROUTE_PATH_FLAG_NONE);
559 fib_table_entry_delete(fib_index,
561 FIB_SOURCE_PLUGIN_LOW);
564 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
569 vlib_thread_main_t *tm = vlib_get_thread_main ();
571 /* Check if address already exists */
572 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
574 if (ap->addr.as_u32 == addr->as_u32)
579 vec_add2 (sm->twice_nat_addresses, ap, 1);
581 vec_add2 (sm->addresses, ap, 1);
586 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
587 FIB_SOURCE_PLUGIN_LOW);
590 #define _(N, i, n, s) \
591 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
592 ap->busy_##n##_ports = 0; \
593 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
594 foreach_snat_protocol
600 /* Add external address to FIB */
601 pool_foreach (i, sm->interfaces,
603 if (nat_interface_is_inside(i) || sm->out2in_dpo)
606 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
609 pool_foreach (i, sm->output_feature_interfaces,
611 if (nat_interface_is_inside(i) || sm->out2in_dpo)
614 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
619 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
622 snat_static_mapping_t *m;
623 pool_foreach (m, sm->static_mappings,
625 if (m->external_addr.as_u32 == addr.as_u32)
632 void increment_v4_address (ip4_address_t * a)
636 v = clib_net_to_host_u32(a->as_u32) + 1;
637 a->as_u32 = clib_host_to_net_u32(v);
641 snat_add_static_mapping_when_resolved (snat_main_t * sm,
642 ip4_address_t l_addr,
647 snat_protocol_t proto,
652 snat_static_map_resolve_t *rp;
654 vec_add2 (sm->to_resolve, rp, 1);
655 rp->l_addr.as_u32 = l_addr.as_u32;
657 rp->sw_if_index = sw_if_index;
661 rp->addr_only = addr_only;
663 rp->tag = vec_dup (tag);
667 * @brief Add static mapping.
669 * Create static mapping between local addr+port and external addr+port.
671 * @param l_addr Local IPv4 address.
672 * @param e_addr External IPv4 address.
673 * @param l_port Local port number.
674 * @param e_port External port number.
675 * @param vrf_id VRF ID.
676 * @param addr_only If 0 address port and pair mapping, otherwise address only.
677 * @param sw_if_index External port instead of specific IP address.
678 * @param is_add If 0 delete static mapping, otherwise add.
679 * @param twice_nat If value is TWICE_NAT then translate external host address
681 * If value is TWICE_NAT_SELF then translate external host
682 * address and port whenever external host address equals
683 * local address of internal host.
684 * @param out2in_only If 1 rule match only out2in direction
685 * @param tag - opaque string tag
689 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
690 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
691 u32 sw_if_index, snat_protocol_t proto, int is_add,
692 twice_nat_type_t twice_nat, u8 out2in_only,
695 snat_main_t * sm = &snat_main;
696 snat_static_mapping_t *m;
697 snat_session_key_t m_key;
698 clib_bihash_kv_8_8_t kv, value;
699 snat_address_t *a = 0;
702 snat_interface_t *interface;
704 snat_main_per_thread_data_t *tsm;
705 snat_user_key_t u_key;
707 dlist_elt_t * head, * elt;
708 u32 elt_index, head_index;
712 snat_static_map_resolve_t *rp, *rp_match = 0;
714 /* If the external address is a specific interface address */
715 if (sw_if_index != ~0)
717 ip4_address_t * first_int_addr;
719 for (i = 0; i < vec_len (sm->to_resolve); i++)
721 rp = sm->to_resolve + i;
722 if (rp->sw_if_index != sw_if_index &&
723 rp->l_addr.as_u32 != l_addr.as_u32 &&
724 rp->vrf_id != vrf_id && rp->addr_only != addr_only)
729 if (rp->l_port != l_port && rp->e_port != e_port && rp->proto != proto)
737 /* Might be already set... */
738 first_int_addr = ip4_interface_first_address
739 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
744 return VNET_API_ERROR_VALUE_EXIST;
746 snat_add_static_mapping_when_resolved
747 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
748 addr_only, is_add, tag);
750 /* DHCP resolution required? */
751 if (first_int_addr == 0)
757 e_addr.as_u32 = first_int_addr->as_u32;
758 /* Identity mapping? */
759 if (l_addr.as_u32 == 0)
760 l_addr.as_u32 = e_addr.as_u32;
766 return VNET_API_ERROR_NO_SUCH_ENTRY;
768 vec_del1 (sm->to_resolve, i);
772 e_addr.as_u32 = first_int_addr->as_u32;
773 /* Identity mapping? */
774 if (l_addr.as_u32 == 0)
775 l_addr.as_u32 = e_addr.as_u32;
783 m_key.port = addr_only ? 0 : e_port;
784 m_key.protocol = addr_only ? 0 : proto;
785 m_key.fib_index = sm->outside_fib_index;
786 kv.key = m_key.as_u64;
787 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
790 m = pool_elt_at_index (sm->static_mappings, value.value);
795 return VNET_API_ERROR_VALUE_EXIST;
797 if (twice_nat && addr_only)
798 return VNET_API_ERROR_UNSUPPORTED;
800 /* Convert VRF id to FIB index */
803 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
805 return VNET_API_ERROR_NO_SUCH_FIB;
808 /* If not specified use inside VRF id from SNAT plugin startup config */
811 fib_index = sm->inside_fib_index;
812 vrf_id = sm->inside_vrf_id;
818 m_key.port = addr_only ? 0 : l_port;
819 m_key.protocol = addr_only ? 0 : proto;
820 m_key.fib_index = fib_index;
821 kv.key = m_key.as_u64;
822 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value))
823 return VNET_API_ERROR_VALUE_EXIST;
826 /* Find external address in allocated addresses and reserve port for
827 address and port pair mapping when dynamic translations enabled */
828 if (!(addr_only || sm->static_mapping_only || out2in_only))
830 for (i = 0; i < vec_len (sm->addresses); i++)
832 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
834 a = sm->addresses + i;
835 /* External port must be unused */
838 #define _(N, j, n, s) \
839 case SNAT_PROTOCOL_##N: \
840 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
841 return VNET_API_ERROR_INVALID_VALUE; \
842 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
845 a->busy_##n##_ports++; \
846 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
849 foreach_snat_protocol
852 clib_warning("unknown_protocol");
853 return VNET_API_ERROR_INVALID_VALUE_2;
858 /* External address must be allocated */
859 if (!a && (l_addr.as_u32 != e_addr.as_u32))
861 if (sw_if_index != ~0)
863 for (i = 0; i < vec_len (sm->to_resolve); i++)
865 rp = sm->to_resolve + i;
868 if (rp->sw_if_index != sw_if_index &&
869 rp->l_addr.as_u32 != l_addr.as_u32 &&
870 rp->vrf_id != vrf_id && rp->l_port != l_port &&
871 rp->e_port != e_port && rp->proto != proto)
874 vec_del1 (sm->to_resolve, i);
878 return VNET_API_ERROR_NO_SUCH_ENTRY;
882 pool_get (sm->static_mappings, m);
883 memset (m, 0, sizeof (*m));
884 m->tag = vec_dup (tag);
885 m->local_addr = l_addr;
886 m->external_addr = e_addr;
887 m->addr_only = addr_only;
889 m->fib_index = fib_index;
890 m->twice_nat = twice_nat;
891 m->out2in_only = out2in_only;
894 m->local_port = l_port;
895 m->external_port = e_port;
902 .src_address = m->local_addr,
904 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
905 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
908 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
910 m_key.addr = m->local_addr;
911 m_key.port = m->local_port;
912 m_key.protocol = m->proto;
913 m_key.fib_index = m->fib_index;
914 kv.key = m_key.as_u64;
915 kv.value = m - sm->static_mappings;
917 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
918 if (twice_nat || out2in_only)
920 m_key.port = clib_host_to_net_u16 (l_port);
921 kv.key = m_key.as_u64;
923 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
924 clib_warning ("in2out key add failed");
927 m_key.addr = m->external_addr;
928 m_key.port = m->external_port;
929 m_key.fib_index = sm->outside_fib_index;
930 kv.key = m_key.as_u64;
931 kv.value = m - sm->static_mappings;
932 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
933 if (twice_nat || out2in_only)
935 m_key.port = clib_host_to_net_u16 (e_port);
936 kv.key = m_key.as_u64;
938 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
939 clib_warning ("out2in key add failed");
942 /* Delete dynamic sessions matching local address (+ local port) */
943 if (!(sm->static_mapping_only))
945 u_key.addr = m->local_addr;
946 u_key.fib_index = m->fib_index;
947 kv.key = u_key.as_u64;
948 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
950 user_index = value.value;
951 u = pool_elt_at_index (tsm->users, user_index);
954 head_index = u->sessions_per_user_list_head_index;
955 head = pool_elt_at_index (tsm->list_pool, head_index);
956 elt_index = head->next;
957 elt = pool_elt_at_index (tsm->list_pool, elt_index);
958 ses_index = elt->value;
959 while (ses_index != ~0)
961 s = pool_elt_at_index (tsm->sessions, ses_index);
962 elt = pool_elt_at_index (tsm->list_pool, elt->next);
963 ses_index = elt->value;
965 if (snat_is_session_static (s))
970 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
971 (clib_net_to_host_u16 (s->out2in.port) != e_port))
975 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
976 clib_dlist_remove (tsm->list_pool, s->per_user_index);
977 pool_put_index (tsm->list_pool, s->per_user_index);
978 pool_put (tsm->sessions, s);
992 if (sw_if_index != ~0)
995 return VNET_API_ERROR_NO_SUCH_ENTRY;
998 /* Free external address port */
999 if (!(addr_only || sm->static_mapping_only || out2in_only))
1001 for (i = 0; i < vec_len (sm->addresses); i++)
1003 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1005 a = sm->addresses + i;
1008 #define _(N, j, n, s) \
1009 case SNAT_PROTOCOL_##N: \
1010 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1011 if (e_port > 1024) \
1013 a->busy_##n##_ports--; \
1014 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1017 foreach_snat_protocol
1020 clib_warning("unknown_protocol");
1021 return VNET_API_ERROR_INVALID_VALUE_2;
1028 if (sm->num_workers > 1)
1029 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1031 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1033 m_key.addr = m->local_addr;
1034 m_key.port = m->local_port;
1035 m_key.protocol = m->proto;
1036 m_key.fib_index = m->fib_index;
1037 kv.key = m_key.as_u64;
1039 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
1040 if (twice_nat || out2in_only)
1042 m_key.port = clib_host_to_net_u16 (m->local_port);
1043 kv.key = m_key.as_u64;
1045 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1046 clib_warning ("in2out key del failed");
1049 m_key.addr = m->external_addr;
1050 m_key.port = m->external_port;
1051 m_key.fib_index = sm->outside_fib_index;
1052 kv.key = m_key.as_u64;
1053 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
1054 if (twice_nat || out2in_only)
1056 m_key.port = clib_host_to_net_u16 (m->external_port);
1057 kv.key = m_key.as_u64;
1059 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1060 clib_warning ("in2out key del failed");
1063 /* Delete session(s) for static mapping if exist */
1064 if (!(sm->static_mapping_only) ||
1065 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1067 u_key.addr = m->local_addr;
1068 u_key.fib_index = m->fib_index;
1069 kv.key = u_key.as_u64;
1070 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1072 user_index = value.value;
1073 u = pool_elt_at_index (tsm->users, user_index);
1074 if (u->nstaticsessions)
1076 head_index = u->sessions_per_user_list_head_index;
1077 head = pool_elt_at_index (tsm->list_pool, head_index);
1078 elt_index = head->next;
1079 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1080 ses_index = elt->value;
1081 while (ses_index != ~0)
1083 s = pool_elt_at_index (tsm->sessions, ses_index);
1084 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1085 ses_index = elt->value;
1089 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
1090 (clib_net_to_host_u16 (s->out2in.port) != e_port))
1094 if (is_lb_session (s))
1097 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1098 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1099 pool_put_index (tsm->list_pool, s->per_user_index);
1100 pool_put (tsm->sessions, s);
1101 u->nstaticsessions--;
1106 if (addr_only && (u->nstaticsessions == 0))
1108 pool_put (tsm->users, u);
1109 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
1116 /* Delete static mapping from pool */
1117 pool_put (sm->static_mappings, m);
1120 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1123 /* Add/delete external address to FIB */
1124 pool_foreach (interface, sm->interfaces,
1126 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1129 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1132 pool_foreach (interface, sm->output_feature_interfaces,
1134 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1137 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1144 static int lb_local_exists (nat44_lb_addr_port_t * local,
1145 ip4_address_t * e_addr, u16 e_port)
1147 snat_main_t *sm = &snat_main;
1148 snat_static_mapping_t *m;
1149 nat44_lb_addr_port_t *ap;
1152 pool_foreach (m, sm->static_mappings,
1154 if (vec_len(m->locals))
1156 if (m->external_port == e_port && m->external_addr.as_u32 == e_addr->as_u32)
1159 vec_foreach (ap, m->locals)
1161 if (ap->port == local->port && ap->addr.as_u32 == local->addr.as_u32)
1171 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1172 snat_protocol_t proto, u32 vrf_id,
1173 nat44_lb_addr_port_t *locals, u8 is_add,
1174 twice_nat_type_t twice_nat, u8 out2in_only,
1177 snat_main_t * sm = &snat_main;
1178 snat_static_mapping_t *m;
1179 snat_session_key_t m_key;
1180 clib_bihash_kv_8_8_t kv, value;
1182 snat_address_t *a = 0;
1184 nat44_lb_addr_port_t *local;
1185 u32 worker_index = 0, elt_index, head_index, ses_index;
1186 snat_main_per_thread_data_t *tsm;
1187 snat_user_key_t u_key;
1190 dlist_elt_t * head, * elt;
1192 m_key.addr = e_addr;
1193 m_key.port = e_port;
1194 m_key.protocol = proto;
1195 m_key.fib_index = sm->outside_fib_index;
1196 kv.key = m_key.as_u64;
1197 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1200 m = pool_elt_at_index (sm->static_mappings, value.value);
1205 return VNET_API_ERROR_VALUE_EXIST;
1207 if (vec_len (locals) < 2)
1208 return VNET_API_ERROR_INVALID_VALUE;
1210 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1212 FIB_SOURCE_PLUGIN_LOW);
1214 /* Find external address in allocated addresses and reserve port for
1215 address and port pair mapping when dynamic translations enabled */
1216 if (!(sm->static_mapping_only || out2in_only))
1218 for (i = 0; i < vec_len (sm->addresses); i++)
1220 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1222 a = sm->addresses + i;
1223 /* External port must be unused */
1226 #define _(N, j, n, s) \
1227 case SNAT_PROTOCOL_##N: \
1228 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1229 return VNET_API_ERROR_INVALID_VALUE; \
1230 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1231 if (e_port > 1024) \
1233 a->busy_##n##_ports++; \
1234 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1237 foreach_snat_protocol
1240 clib_warning("unknown_protocol");
1241 return VNET_API_ERROR_INVALID_VALUE_2;
1246 /* External address must be allocated */
1248 return VNET_API_ERROR_NO_SUCH_ENTRY;
1251 pool_get (sm->static_mappings, m);
1252 memset (m, 0, sizeof (*m));
1253 m->tag = vec_dup (tag);
1254 m->external_addr = e_addr;
1257 m->fib_index = fib_index;
1258 m->external_port = e_port;
1260 m->twice_nat = twice_nat;
1261 m->out2in_only = out2in_only;
1263 m_key.addr = m->external_addr;
1264 m_key.port = m->external_port;
1265 m_key.protocol = m->proto;
1266 m_key.fib_index = sm->outside_fib_index;
1267 kv.key = m_key.as_u64;
1268 kv.value = m - sm->static_mappings;
1269 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1271 clib_warning ("static_mapping_by_external key add failed");
1272 return VNET_API_ERROR_UNSPECIFIED;
1278 worker_index = sm->first_worker_index +
1279 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1280 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1281 m->worker_index = worker_index;
1284 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1286 m_key.port = clib_host_to_net_u16 (m->external_port);
1287 kv.key = m_key.as_u64;
1289 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1291 clib_warning ("out2in key add failed");
1292 return VNET_API_ERROR_UNSPECIFIED;
1295 m_key.fib_index = m->fib_index;
1296 for (i = 0; i < vec_len (locals); i++)
1298 m_key.addr = locals[i].addr;
1301 m_key.port = locals[i].port;
1302 kv.key = m_key.as_u64;
1303 kv.value = m - sm->static_mappings;
1304 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1306 locals[i].prefix = (i == 0) ? locals[i].probability :\
1307 (locals[i - 1].prefix + locals[i].probability);
1308 vec_add1 (m->locals, locals[i]);
1310 m_key.port = clib_host_to_net_u16 (locals[i].port);
1311 kv.key = m_key.as_u64;
1313 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1315 clib_warning ("in2out key add failed");
1316 return VNET_API_ERROR_UNSPECIFIED;
1323 return VNET_API_ERROR_NO_SUCH_ENTRY;
1325 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1327 /* Free external address port */
1328 if (!(sm->static_mapping_only || out2in_only))
1330 for (i = 0; i < vec_len (sm->addresses); i++)
1332 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1334 a = sm->addresses + i;
1337 #define _(N, j, n, s) \
1338 case SNAT_PROTOCOL_##N: \
1339 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1340 if (e_port > 1024) \
1342 a->busy_##n##_ports--; \
1343 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1346 foreach_snat_protocol
1349 clib_warning("unknown_protocol");
1350 return VNET_API_ERROR_INVALID_VALUE_2;
1357 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1358 m_key.addr = m->external_addr;
1359 m_key.port = m->external_port;
1360 m_key.protocol = m->proto;
1361 m_key.fib_index = sm->outside_fib_index;
1362 kv.key = m_key.as_u64;
1363 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1365 clib_warning ("static_mapping_by_external key del failed");
1366 return VNET_API_ERROR_UNSPECIFIED;
1369 m_key.port = clib_host_to_net_u16 (m->external_port);
1370 kv.key = m_key.as_u64;
1371 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1373 clib_warning ("outi2in key del failed");
1374 return VNET_API_ERROR_UNSPECIFIED;
1377 vec_foreach (local, m->locals)
1379 m_key.addr = local->addr;
1382 m_key.port = local->port;
1383 m_key.fib_index = m->fib_index;
1384 kv.key = m_key.as_u64;
1385 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1387 clib_warning ("static_mapping_by_local key del failed");
1388 return VNET_API_ERROR_UNSPECIFIED;
1392 if (!lb_local_exists(local, &e_addr, e_port))
1394 m_key.port = clib_host_to_net_u16 (local->port);
1395 kv.key = m_key.as_u64;
1396 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1398 clib_warning ("in2out key del failed");
1399 return VNET_API_ERROR_UNSPECIFIED;
1402 /* Delete sessions */
1403 u_key.addr = local->addr;
1404 u_key.fib_index = m->fib_index;
1405 kv.key = u_key.as_u64;
1406 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1408 u = pool_elt_at_index (tsm->users, value.value);
1409 if (u->nstaticsessions)
1411 head_index = u->sessions_per_user_list_head_index;
1412 head = pool_elt_at_index (tsm->list_pool, head_index);
1413 elt_index = head->next;
1414 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1415 ses_index = elt->value;
1416 while (ses_index != ~0)
1418 s = pool_elt_at_index (tsm->sessions, ses_index);
1419 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1420 ses_index = elt->value;
1422 if (!(is_lb_session (s)))
1425 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1426 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1429 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1430 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1431 pool_put_index (tsm->list_pool, s->per_user_index);
1432 pool_put (tsm->sessions, s);
1433 u->nstaticsessions--;
1438 vec_free(m->locals);
1441 pool_put (sm->static_mappings, m);
1448 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1451 snat_address_t *a = 0;
1452 snat_session_t *ses;
1453 u32 *ses_to_be_removed = 0, *ses_index;
1454 clib_bihash_kv_8_8_t kv, value;
1455 snat_user_key_t user_key;
1457 snat_main_per_thread_data_t *tsm;
1458 snat_static_mapping_t *m;
1459 snat_interface_t *interface;
1461 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1463 /* Find SNAT address */
1464 for (i=0; i < vec_len (addresses); i++)
1466 if (addresses[i].addr.as_u32 == addr.as_u32)
1473 return VNET_API_ERROR_NO_SUCH_ENTRY;
1477 pool_foreach (m, sm->static_mappings,
1479 if (m->external_addr.as_u32 == addr.as_u32)
1480 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1481 m->local_port, m->external_port,
1482 m->vrf_id, m->addr_only, ~0,
1483 m->proto, 0, m->twice_nat,
1484 m->out2in_only, m->tag);
1489 /* Check if address is used in some static mapping */
1490 if (is_snat_address_used_in_static_mapping(sm, addr))
1492 clib_warning ("address used in static mapping");
1493 return VNET_API_ERROR_UNSPECIFIED;
1497 if (a->fib_index != ~0)
1498 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1499 FIB_SOURCE_PLUGIN_LOW);
1501 /* Delete sessions using address */
1502 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1504 vec_foreach (tsm, sm->per_thread_data)
1506 pool_foreach (ses, tsm->sessions, ({
1507 if (ses->out2in.addr.as_u32 == addr.as_u32)
1509 ses->outside_address_index = ~0;
1510 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1511 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1512 pool_put_index (tsm->list_pool, ses->per_user_index);
1513 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1514 user_key.addr = ses->in2out.addr;
1515 user_key.fib_index = ses->in2out.fib_index;
1516 kv.key = user_key.as_u64;
1517 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1519 u = pool_elt_at_index (tsm->users, value.value);
1525 vec_foreach (ses_index, ses_to_be_removed)
1526 pool_put_index (tsm->sessions, ses_index[0]);
1528 vec_free (ses_to_be_removed);
1534 vec_del1 (sm->twice_nat_addresses, i);
1538 vec_del1 (sm->addresses, i);
1540 /* Delete external address from FIB */
1541 pool_foreach (interface, sm->interfaces,
1543 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1546 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1549 pool_foreach (interface, sm->output_feature_interfaces,
1551 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1554 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1561 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1563 snat_main_t *sm = &snat_main;
1564 snat_interface_t *i;
1565 const char * feature_name, *del_feature_name;
1566 snat_address_t * ap;
1567 snat_static_mapping_t * m;
1568 snat_det_map_t * dm;
1570 if (sm->out2in_dpo && !is_inside)
1571 return VNET_API_ERROR_UNSUPPORTED;
1573 pool_foreach (i, sm->output_feature_interfaces,
1575 if (i->sw_if_index == sw_if_index)
1576 return VNET_API_ERROR_VALUE_EXIST;
1579 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1580 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1583 if (sm->num_workers > 1 && !sm->deterministic)
1584 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1585 else if (sm->deterministic)
1586 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1588 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1591 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1592 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1594 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1595 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1597 pool_foreach (i, sm->interfaces,
1599 if (i->sw_if_index == sw_if_index)
1603 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1606 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1608 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1610 if (sm->num_workers > 1 && !sm->deterministic)
1612 del_feature_name = "nat44-handoff-classify";
1613 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1614 "nat44-out2in-worker-handoff";
1616 else if (sm->deterministic)
1618 del_feature_name = "nat44-det-classify";
1619 feature_name = !is_inside ? "nat44-det-in2out" :
1624 del_feature_name = "nat44-classify";
1625 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1628 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1629 sw_if_index, 0, 0, 0);
1630 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1631 sw_if_index, 1, 0, 0);
1633 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1634 sw_if_index, 1, 0, 0);
1638 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1639 sw_if_index, 0, 0, 0);
1640 pool_put (sm->interfaces, i);
1642 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1643 sw_if_index, 0, 0, 0);
1648 if ((nat_interface_is_inside(i) && is_inside) ||
1649 (nat_interface_is_outside(i) && !is_inside))
1652 if (sm->num_workers > 1 && !sm->deterministic)
1654 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1655 "nat44-out2in-worker-handoff";
1656 feature_name = "nat44-handoff-classify";
1658 else if (sm->deterministic)
1660 del_feature_name = !is_inside ? "nat44-det-in2out" :
1662 feature_name = "nat44-det-classify";
1666 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1667 feature_name = "nat44-classify";
1670 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1671 sw_if_index, 0, 0, 0);
1672 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1673 sw_if_index, 1, 0, 0);
1675 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1676 sw_if_index, 0, 0, 0);
1685 return VNET_API_ERROR_NO_SUCH_ENTRY;
1687 pool_get (sm->interfaces, i);
1688 i->sw_if_index = sw_if_index;
1690 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1692 if (is_inside && !sm->out2in_dpo)
1693 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1694 sw_if_index, 1, 0, 0);
1699 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1703 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1705 /* Add/delete external addresses to FIB */
1707 vec_foreach (ap, sm->addresses)
1708 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1710 pool_foreach (m, sm->static_mappings,
1712 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1715 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1718 pool_foreach (dm, sm->det_maps,
1720 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1726 int snat_interface_add_del_output_feature (u32 sw_if_index,
1730 snat_main_t *sm = &snat_main;
1731 snat_interface_t *i;
1732 snat_address_t * ap;
1733 snat_static_mapping_t * m;
1735 if (sm->deterministic ||
1736 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1737 return VNET_API_ERROR_UNSUPPORTED;
1739 pool_foreach (i, sm->interfaces,
1741 if (i->sw_if_index == sw_if_index)
1742 return VNET_API_ERROR_VALUE_EXIST;
1747 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1748 sw_if_index, !is_del, 0, 0);
1749 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1750 sw_if_index, !is_del, 0, 0);
1754 if (sm->num_workers > 1)
1756 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1757 sw_if_index, !is_del, 0, 0);
1758 vnet_feature_enable_disable ("ip4-output",
1759 "nat44-in2out-output-worker-handoff",
1760 sw_if_index, !is_del, 0, 0);
1764 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1766 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1767 sw_if_index, !is_del, 0, 0);
1771 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1772 sm->fq_in2out_output_index =
1773 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1775 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1776 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1778 pool_foreach (i, sm->output_feature_interfaces,
1780 if (i->sw_if_index == sw_if_index)
1783 pool_put (sm->output_feature_interfaces, i);
1785 return VNET_API_ERROR_VALUE_EXIST;
1792 return VNET_API_ERROR_NO_SUCH_ENTRY;
1794 pool_get (sm->output_feature_interfaces, i);
1795 i->sw_if_index = sw_if_index;
1798 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1800 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1802 /* Add/delete external addresses to FIB */
1807 vec_foreach (ap, sm->addresses)
1808 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1810 pool_foreach (m, sm->static_mappings,
1812 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1815 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1821 int snat_set_workers (uword * bitmap)
1823 snat_main_t *sm = &snat_main;
1826 if (sm->num_workers < 2)
1827 return VNET_API_ERROR_FEATURE_DISABLED;
1829 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1830 return VNET_API_ERROR_INVALID_WORKER;
1832 vec_free (sm->workers);
1833 clib_bitmap_foreach (i, bitmap,
1835 vec_add1(sm->workers, i);
1836 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1840 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1841 sm->num_snat_thread = _vec_len (sm->workers);
1848 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1851 ip4_address_t * address,
1853 u32 if_address_index,
1857 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1860 ip4_address_t * address,
1862 u32 if_address_index,
1866 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1869 snat_session_key_t * k,
1870 u32 * address_indexp,
1871 u16 port_per_thread,
1872 u32 snat_thread_index);
1874 static clib_error_t * snat_init (vlib_main_t * vm)
1876 snat_main_t * sm = &snat_main;
1877 clib_error_t * error = 0;
1878 ip4_main_t * im = &ip4_main;
1879 ip_lookup_main_t * lm = &im->lookup_main;
1881 vlib_thread_registration_t *tr;
1882 vlib_thread_main_t *tm = vlib_get_thread_main ();
1885 ip4_add_del_interface_address_callback_t cb4;
1888 sm->vnet_main = vnet_get_main();
1890 sm->ip4_lookup_main = lm;
1891 sm->api_main = &api_main;
1892 sm->first_worker_index = 0;
1893 sm->next_worker = 0;
1894 sm->num_workers = 0;
1895 sm->num_snat_thread = 1;
1897 sm->port_per_thread = 0xffff - 1024;
1898 sm->fq_in2out_index = ~0;
1899 sm->fq_out2in_index = ~0;
1900 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1901 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1902 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1903 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1904 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1905 sm->forwarding_enabled = 0;
1907 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1910 tr = (vlib_thread_registration_t *) p[0];
1913 sm->num_workers = tr->count;
1914 sm->first_worker_index = tr->first_index;
1918 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1920 /* Use all available workers by default */
1921 if (sm->num_workers > 1)
1923 for (i=0; i < sm->num_workers; i++)
1924 bitmap = clib_bitmap_set (bitmap, i, 1);
1925 snat_set_workers(bitmap);
1926 clib_bitmap_free (bitmap);
1930 sm->per_thread_data[0].snat_thread_index = 0;
1933 error = snat_api_init(vm, sm);
1937 /* Set up the interface address add/del callback */
1938 cb4.function = snat_ip4_add_del_interface_address_cb;
1939 cb4.function_opaque = 0;
1941 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1943 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
1944 cb4.function_opaque = 0;
1946 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1948 nat_dpo_module_init ();
1950 /* Init IPFIX logging */
1951 snat_ipfix_logging_init(vm);
1954 error = nat64_init(vm);
1962 /* Init virtual fragmenentation reassembly */
1963 return nat_reass_init(vm);
1966 VLIB_INIT_FUNCTION (snat_init);
1968 void snat_free_outside_address_and_port (snat_address_t * addresses,
1970 snat_session_key_t * k,
1974 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1976 ASSERT (address_index < vec_len (addresses));
1978 a = addresses + address_index;
1980 switch (k->protocol)
1982 #define _(N, i, n, s) \
1983 case SNAT_PROTOCOL_##N: \
1984 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1985 port_host_byte_order) == 1); \
1986 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1987 port_host_byte_order, 0); \
1988 a->busy_##n##_ports--; \
1989 a->busy_##n##_ports_per_thread[thread_index]--; \
1991 foreach_snat_protocol
1994 clib_warning("unknown_protocol");
2000 * @brief Match NAT44 static mapping.
2002 * @param sm NAT main.
2003 * @param match Address and port to match.
2004 * @param mapping External or local address and port of the matched mapping.
2005 * @param by_external If 0 match by local address otherwise match by external
2007 * @param is_addr_only If matched mapping is address only
2008 * @param twice_nat If matched mapping is twice NAT.
2009 * @param lb If matched mapping is load-balanced.
2011 * @returns 0 if match found otherwise 1.
2013 int snat_static_mapping_match (snat_main_t * sm,
2014 snat_session_key_t match,
2015 snat_session_key_t * mapping,
2018 twice_nat_type_t *twice_nat,
2021 clib_bihash_kv_8_8_t kv, value;
2022 snat_static_mapping_t *m;
2023 snat_session_key_t m_key;
2024 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2025 u32 rand, lo = 0, hi, mid;
2028 mapping_hash = &sm->static_mapping_by_external;
2030 m_key.addr = match.addr;
2031 m_key.port = clib_net_to_host_u16 (match.port);
2032 m_key.protocol = match.protocol;
2033 m_key.fib_index = match.fib_index;
2035 kv.key = m_key.as_u64;
2037 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2039 /* Try address only mapping */
2042 kv.key = m_key.as_u64;
2043 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2047 m = pool_elt_at_index (sm->static_mappings, value.value);
2051 if (vec_len (m->locals))
2053 hi = vec_len (m->locals) - 1;
2054 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2057 mid = ((hi - lo) >> 1) + lo;
2058 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2060 if (!(m->locals[lo].prefix >= rand))
2062 mapping->addr = m->locals[lo].addr;
2063 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2067 mapping->addr = m->local_addr;
2068 /* Address only mapping doesn't change port */
2069 mapping->port = m->addr_only ? match.port
2070 : clib_host_to_net_u16 (m->local_port);
2072 mapping->fib_index = m->fib_index;
2073 mapping->protocol = m->proto;
2077 mapping->addr = m->external_addr;
2078 /* Address only mapping doesn't change port */
2079 mapping->port = m->addr_only ? match.port
2080 : clib_host_to_net_u16 (m->external_port);
2081 mapping->fib_index = sm->outside_fib_index;
2084 if (PREDICT_FALSE(is_addr_only != 0))
2085 *is_addr_only = m->addr_only;
2087 if (PREDICT_FALSE(twice_nat != 0))
2088 *twice_nat = m->twice_nat;
2090 if (PREDICT_FALSE(lb != 0))
2091 *lb = vec_len (m->locals) > 0;
2096 static_always_inline u16
2097 snat_random_port (u16 min, u16 max)
2099 snat_main_t *sm = &snat_main;
2100 return min + random_u32 (&sm->random_seed) /
2101 (random_u32_max() / (max - min + 1) + 1);
2105 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2108 snat_session_key_t * k,
2109 u32 * address_indexp,
2110 u16 port_per_thread,
2111 u32 snat_thread_index)
2113 snat_main_t *sm = &snat_main;
2115 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
2116 address_indexp, port_per_thread,
2121 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2124 snat_session_key_t * k,
2125 u32 * address_indexp,
2126 u16 port_per_thread,
2127 u32 snat_thread_index)
2130 snat_address_t *a, *ga = 0;
2133 for (i = 0; i < vec_len (addresses); i++)
2136 switch (k->protocol)
2138 #define _(N, j, n, s) \
2139 case SNAT_PROTOCOL_##N: \
2140 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2142 if (a->fib_index == fib_index) \
2146 portnum = (port_per_thread * \
2147 snat_thread_index) + \
2148 snat_random_port(1, port_per_thread) + 1024; \
2149 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2151 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2152 a->busy_##n##_ports_per_thread[thread_index]++; \
2153 a->busy_##n##_ports++; \
2154 k->addr = a->addr; \
2155 k->port = clib_host_to_net_u16(portnum); \
2156 *address_indexp = i; \
2160 else if (a->fib_index == ~0) \
2167 foreach_snat_protocol
2170 clib_warning("unknown protocol");
2179 switch (k->protocol)
2181 #define _(N, j, n, s) \
2182 case SNAT_PROTOCOL_##N: \
2185 portnum = (port_per_thread * \
2186 snat_thread_index) + \
2187 snat_random_port(1, port_per_thread) + 1024; \
2188 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2190 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2191 a->busy_##n##_ports_per_thread[thread_index]++; \
2192 a->busy_##n##_ports++; \
2193 k->addr = a->addr; \
2194 k->port = clib_host_to_net_u16(portnum); \
2195 *address_indexp = gi; \
2199 foreach_snat_protocol
2202 clib_warning ("unknown protocol");
2207 /* Totally out of translations to use... */
2208 snat_ipfix_logging_addresses_exhausted(0);
2213 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2216 snat_session_key_t * k,
2217 u32 * address_indexp,
2218 u16 port_per_thread,
2219 u32 snat_thread_index)
2221 snat_main_t *sm = &snat_main;
2222 snat_address_t *a = addresses;
2223 u16 m, ports, portnum, A, j;
2224 m = 16 - (sm->psid_offset + sm->psid_length);
2225 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2227 if (!vec_len (addresses))
2230 switch (k->protocol)
2232 #define _(N, i, n, s) \
2233 case SNAT_PROTOCOL_##N: \
2234 if (a->busy_##n##_ports < ports) \
2238 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2239 j = snat_random_port(0, pow2_mask(m)); \
2240 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2241 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2243 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2244 a->busy_##n##_ports++; \
2245 k->addr = a->addr; \
2246 k->port = clib_host_to_net_u16 (portnum); \
2247 *address_indexp = i; \
2252 foreach_snat_protocol
2255 clib_warning("unknown protocol");
2260 /* Totally out of translations to use... */
2261 snat_ipfix_logging_addresses_exhausted(0);
2266 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2268 dpo_id_t dpo_v4 = DPO_INVALID;
2269 fib_prefix_t pfx = {
2270 .fp_proto = FIB_PROTOCOL_IP4,
2272 .fp_addr.ip4.as_u32 = addr.as_u32,
2277 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2278 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2279 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2280 dpo_reset (&dpo_v4);
2284 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2289 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2291 u32 *r = va_arg (*args, u32 *);
2294 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2295 foreach_snat_protocol
2303 format_snat_protocol (u8 * s, va_list * args)
2305 u32 i = va_arg (*args, u32);
2310 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2311 foreach_snat_protocol
2314 s = format (s, "unknown");
2317 s = format (s, "%s", t);
2322 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2324 snat_main_t *sm = &snat_main;
2325 u32 next_worker_index = 0;
2328 next_worker_index = sm->first_worker_index;
2329 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2330 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2332 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2333 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2335 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2337 return next_worker_index;
2341 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2343 snat_main_t *sm = &snat_main;
2346 snat_session_key_t m_key;
2347 clib_bihash_kv_8_8_t kv, value;
2348 snat_static_mapping_t *m;
2349 nat_ed_ses_key_t key;
2350 clib_bihash_kv_16_8_t s_kv, s_value;
2351 snat_main_per_thread_data_t *tsm;
2355 u32 next_worker_index = 0;
2357 /* first try static mappings without port */
2358 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2360 m_key.addr = ip0->dst_address;
2363 m_key.fib_index = rx_fib_index0;
2364 kv.key = m_key.as_u64;
2365 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2367 m = pool_elt_at_index (sm->static_mappings, value.value);
2368 return m->worker_index;
2372 proto = ip_proto_to_snat_proto (ip0->protocol);
2373 udp = ip4_next_header (ip0);
2374 port = udp->dst_port;
2376 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2378 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2379 return vlib_get_thread_index ();
2381 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2383 nat_reass_ip4_t *reass;
2385 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2386 ip0->fragment_id, ip0->protocol);
2388 if (reass && (reass->thread_index != (u32) ~ 0))
2389 return reass->thread_index;
2391 return vlib_get_thread_index ();
2395 /* unknown protocol */
2396 if (PREDICT_FALSE (proto == ~0))
2398 key.l_addr = ip0->dst_address;
2399 key.r_addr = ip0->src_address;
2400 key.fib_index = rx_fib_index0;
2401 key.proto = ip0->protocol;
2404 s_kv.key[0] = key.as_u64[0];
2405 s_kv.key[1] = key.as_u64[1];
2407 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2409 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2411 tsm = vec_elt_at_index (sm->per_thread_data, i);
2412 if (!pool_is_free_index(tsm->sessions, s_value.value))
2414 s = pool_elt_at_index (tsm->sessions, s_value.value);
2415 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2416 s->out2in.port == ip0->protocol &&
2417 snat_is_unk_proto_session (s))
2423 /* if no session use current thread */
2424 return vlib_get_thread_index ();
2427 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2429 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2430 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2431 if (!icmp_is_error_message (icmp))
2432 port = echo->identifier;
2435 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2436 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2437 void *l4_header = ip4_next_header (inner_ip);
2440 case SNAT_PROTOCOL_ICMP:
2441 icmp = (icmp46_header_t*)l4_header;
2442 echo = (icmp_echo_header_t *)(icmp + 1);
2443 port = echo->identifier;
2445 case SNAT_PROTOCOL_UDP:
2446 case SNAT_PROTOCOL_TCP:
2447 port = ((tcp_udp_header_t*)l4_header)->src_port;
2450 return vlib_get_thread_index ();
2455 /* try static mappings with port */
2456 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2458 m_key.addr = ip0->dst_address;
2459 m_key.port = clib_net_to_host_u16 (port);
2460 m_key.protocol = proto;
2461 m_key.fib_index = rx_fib_index0;
2462 kv.key = m_key.as_u64;
2463 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2465 m = pool_elt_at_index (sm->static_mappings, value.value);
2466 return m->worker_index;
2470 /* worker by outside port */
2471 next_worker_index = sm->first_worker_index;
2472 next_worker_index +=
2473 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2474 return next_worker_index;
2477 static clib_error_t *
2478 snat_config (vlib_main_t * vm, unformat_input_t * input)
2480 snat_main_t * sm = &snat_main;
2481 nat66_main_t * nm = &nat66_main;
2482 u32 translation_buckets = 1024;
2483 u32 translation_memory_size = 128<<20;
2484 u32 user_buckets = 128;
2485 u32 user_memory_size = 64<<20;
2486 u32 max_translations_per_user = 100;
2487 u32 outside_vrf_id = 0;
2488 u32 outside_ip6_vrf_id = 0;
2489 u32 inside_vrf_id = 0;
2490 u32 static_mapping_buckets = 1024;
2491 u32 static_mapping_memory_size = 64<<20;
2492 u32 nat64_bib_buckets = 1024;
2493 u32 nat64_bib_memory_size = 128 << 20;
2494 u32 nat64_st_buckets = 2048;
2495 u32 nat64_st_memory_size = 256 << 20;
2496 u8 static_mapping_only = 0;
2497 u8 static_mapping_connection_tracking = 0;
2498 snat_main_per_thread_data_t *tsm;
2499 dslite_main_t * dm = &dslite_main;
2501 sm->deterministic = 0;
2504 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2506 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2508 else if (unformat (input, "translation hash memory %d",
2509 &translation_memory_size));
2510 else if (unformat (input, "user hash buckets %d", &user_buckets))
2512 else if (unformat (input, "user hash memory %d",
2515 else if (unformat (input, "max translations per user %d",
2516 &max_translations_per_user))
2518 else if (unformat (input, "outside VRF id %d",
2521 else if (unformat (input, "outside ip6 VRF id %d",
2522 &outside_ip6_vrf_id))
2524 else if (unformat (input, "inside VRF id %d",
2527 else if (unformat (input, "static mapping only"))
2529 static_mapping_only = 1;
2530 if (unformat (input, "connection tracking"))
2531 static_mapping_connection_tracking = 1;
2533 else if (unformat (input, "deterministic"))
2534 sm->deterministic = 1;
2535 else if (unformat (input, "nat64 bib hash buckets %d",
2536 &nat64_bib_buckets))
2538 else if (unformat (input, "nat64 bib hash memory %d",
2539 &nat64_bib_memory_size))
2541 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2543 else if (unformat (input, "nat64 st hash memory %d",
2544 &nat64_st_memory_size))
2546 else if (unformat (input, "out2in dpo"))
2548 else if (unformat (input, "dslite ce"))
2549 dslite_set_ce(dm, 1);
2551 return clib_error_return (0, "unknown input '%U'",
2552 format_unformat_error, input);
2555 /* for show commands, etc. */
2556 sm->translation_buckets = translation_buckets;
2557 sm->translation_memory_size = translation_memory_size;
2558 /* do not exceed load factor 10 */
2559 sm->max_translations = 10 * translation_buckets;
2560 sm->user_buckets = user_buckets;
2561 sm->user_memory_size = user_memory_size;
2562 sm->max_translations_per_user = max_translations_per_user;
2563 sm->outside_vrf_id = outside_vrf_id;
2564 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2566 FIB_SOURCE_PLUGIN_HI);
2567 nm->outside_vrf_id = outside_ip6_vrf_id;
2568 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2570 FIB_SOURCE_PLUGIN_HI);
2571 sm->inside_vrf_id = inside_vrf_id;
2572 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2574 FIB_SOURCE_PLUGIN_HI);
2575 sm->static_mapping_only = static_mapping_only;
2576 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2578 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2579 nat64_st_memory_size);
2581 if (sm->deterministic)
2583 sm->in2out_node_index = snat_det_in2out_node.index;
2584 sm->in2out_output_node_index = ~0;
2585 sm->out2in_node_index = snat_det_out2in_node.index;
2586 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2587 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2591 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2592 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2593 sm->in2out_node_index = snat_in2out_node.index;
2594 sm->in2out_output_node_index = snat_in2out_output_node.index;
2595 sm->out2in_node_index = snat_out2in_node.index;
2596 if (!static_mapping_only ||
2597 (static_mapping_only && static_mapping_connection_tracking))
2599 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2600 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2602 vec_foreach (tsm, sm->per_thread_data)
2604 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2605 translation_memory_size);
2607 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2608 translation_memory_size);
2610 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2614 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2615 translation_buckets, translation_memory_size);
2617 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2618 translation_buckets, translation_memory_size);
2622 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2623 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2625 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2626 "static_mapping_by_local", static_mapping_buckets,
2627 static_mapping_memory_size);
2629 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2630 "static_mapping_by_external", static_mapping_buckets,
2631 static_mapping_memory_size);
2637 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2639 u8 * format_snat_session_state (u8 * s, va_list * args)
2641 u32 i = va_arg (*args, u32);
2646 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2647 foreach_snat_session_state
2650 t = format (t, "unknown");
2652 s = format (s, "%s", t);
2656 u8 * format_snat_key (u8 * s, va_list * args)
2658 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2660 s = format (s, "%U proto %U port %d fib %d",
2661 format_ip4_address, &key->addr,
2662 format_snat_protocol, key->protocol,
2663 clib_net_to_host_u16 (key->port), key->fib_index);
2667 u8 * format_snat_session (u8 * s, va_list * args)
2669 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2670 snat_session_t * sess = va_arg (*args, snat_session_t *);
2672 if (snat_is_unk_proto_session (sess))
2674 s = format (s, " i2o %U proto %u fib %u\n",
2675 format_ip4_address, &sess->in2out.addr,
2676 clib_net_to_host_u16 (sess->in2out.port),
2677 sess->in2out.fib_index);
2678 s = format (s, " o2i %U proto %u fib %u\n",
2679 format_ip4_address, &sess->out2in.addr,
2680 clib_net_to_host_u16 (sess->out2in.port),
2681 sess->out2in.fib_index);
2685 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2686 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2688 if (is_twice_nat_session (sess))
2690 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2691 format_ip4_address, &sess->ext_host_addr,
2692 clib_net_to_host_u16 (sess->ext_host_port),
2693 format_ip4_address, &sess->ext_host_nat_addr,
2694 clib_net_to_host_u16 (sess->ext_host_nat_port));
2698 if (sess->ext_host_addr.as_u32)
2699 s = format (s, " external host %U\n",
2700 format_ip4_address, &sess->ext_host_addr);
2702 s = format (s, " last heard %.2f\n", sess->last_heard);
2703 s = format (s, " total pkts %d, total bytes %lld\n",
2704 sess->total_pkts, sess->total_bytes);
2705 if (snat_is_session_static (sess))
2706 s = format (s, " static translation\n");
2708 s = format (s, " dynamic translation\n");
2709 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2710 s = format (s, " load-balancing\n");
2711 if (is_twice_nat_session (sess))
2712 s = format (s, " twice-nat\n");
2717 u8 * format_snat_user (u8 * s, va_list * args)
2719 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2720 snat_user_t * u = va_arg (*args, snat_user_t *);
2721 int verbose = va_arg (*args, int);
2722 dlist_elt_t * head, * elt;
2723 u32 elt_index, head_index;
2725 snat_session_t * sess;
2727 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2728 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2733 if (u->nsessions || u->nstaticsessions)
2735 head_index = u->sessions_per_user_list_head_index;
2736 head = pool_elt_at_index (sm->list_pool, head_index);
2738 elt_index = head->next;
2739 elt = pool_elt_at_index (sm->list_pool, elt_index);
2740 session_index = elt->value;
2742 while (session_index != ~0)
2744 sess = pool_elt_at_index (sm->sessions, session_index);
2746 s = format (s, " %U\n", format_snat_session, sm, sess);
2748 elt_index = elt->next;
2749 elt = pool_elt_at_index (sm->list_pool, elt_index);
2750 session_index = elt->value;
2757 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2759 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2760 nat44_lb_addr_port_t *local;
2763 s = format (s, "local %U external %U vrf %d %s %s",
2764 format_ip4_address, &m->local_addr,
2765 format_ip4_address, &m->external_addr,
2767 m->twice_nat == TWICE_NAT ? "twice-nat" :
2768 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
2769 m->out2in_only ? "out2in-only" : "");
2772 if (vec_len (m->locals))
2774 s = format (s, "%U vrf %d external %U:%d %s %s",
2775 format_snat_protocol, m->proto,
2777 format_ip4_address, &m->external_addr, m->external_port,
2778 m->twice_nat == TWICE_NAT ? "twice-nat" :
2779 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
2780 m->out2in_only ? "out2in-only" : "");
2781 vec_foreach (local, m->locals)
2782 s = format (s, "\n local %U:%d probability %d\%",
2783 format_ip4_address, &local->addr, local->port,
2784 local->probability);
2787 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
2788 format_snat_protocol, m->proto,
2789 format_ip4_address, &m->local_addr, m->local_port,
2790 format_ip4_address, &m->external_addr, m->external_port,
2792 m->twice_nat == TWICE_NAT ? "twice-nat" :
2793 m->twice_nat == TWICE_NAT_SELF ? "self-twice-nat" : "",
2794 m->out2in_only ? "out2in-only" : "");
2799 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2801 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2802 vnet_main_t *vnm = vnet_get_main();
2805 s = format (s, "local %U external %U vrf %d",
2806 format_ip4_address, &m->l_addr,
2807 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2810 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2811 format_snat_protocol, m->proto,
2812 format_ip4_address, &m->l_addr, m->l_port,
2813 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2814 m->e_port, m->vrf_id);
2819 u8 * format_det_map_ses (u8 * s, va_list * args)
2821 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2822 ip4_address_t in_addr, out_addr;
2823 u32 in_offset, out_offset;
2824 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2825 u32 * i = va_arg (*args, u32 *);
2827 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2828 in_addr.as_u32 = clib_host_to_net_u32 (
2829 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2830 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2831 clib_net_to_host_u32(det_map->in_addr.as_u32);
2832 out_offset = in_offset / det_map->sharing_ratio;
2833 out_addr.as_u32 = clib_host_to_net_u32(
2834 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2835 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2836 format_ip4_address, &in_addr,
2837 clib_net_to_host_u16 (ses->in_port),
2838 format_ip4_address, &out_addr,
2839 clib_net_to_host_u16 (ses->out.out_port),
2840 format_ip4_address, &ses->out.ext_host_addr,
2841 clib_net_to_host_u16 (ses->out.ext_host_port),
2842 format_snat_session_state, ses->state,
2849 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2852 ip4_address_t * address,
2854 u32 if_address_index,
2857 snat_main_t *sm = &snat_main;
2858 snat_static_map_resolve_t *rp;
2859 snat_static_mapping_t *m;
2860 snat_session_key_t m_key;
2861 clib_bihash_kv_8_8_t kv, value;
2863 ip4_address_t l_addr;
2865 for (i = 0; i < vec_len (sm->to_resolve); i++)
2867 rp = sm->to_resolve + i;
2868 if (rp->addr_only == 0)
2870 if (rp->sw_if_index == sw_if_index)
2877 m_key.addr.as_u32 = address->as_u32;
2878 m_key.port = rp->addr_only ? 0 : rp->e_port;
2879 m_key.protocol = rp->addr_only ? 0 : rp->proto;
2880 m_key.fib_index = sm->outside_fib_index;
2881 kv.key = m_key.as_u64;
2882 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2885 m = pool_elt_at_index (sm->static_mappings, value.value);
2889 /* Don't trip over lease renewal, static config */
2899 /* Indetity mapping? */
2900 if (rp->l_addr.as_u32 == 0)
2901 l_addr.as_u32 = address[0].as_u32;
2903 l_addr.as_u32 = rp->l_addr.as_u32;
2904 /* Add the static mapping */
2905 rv = snat_add_static_mapping (l_addr,
2911 ~0 /* sw_if_index */,
2916 clib_warning ("snat_add_static_mapping returned %d", rv);
2920 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2923 ip4_address_t * address,
2925 u32 if_address_index,
2928 snat_main_t *sm = &snat_main;
2929 snat_static_map_resolve_t *rp;
2930 ip4_address_t l_addr;
2934 snat_address_t *addresses = sm->addresses;
2936 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2938 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2942 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
2945 addresses = sm->twice_nat_addresses;
2946 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
2955 /* Don't trip over lease renewal, static config */
2956 for (j = 0; j < vec_len(addresses); j++)
2957 if (addresses[j].addr.as_u32 == address->as_u32)
2960 snat_add_address (sm, address, ~0, twice_nat);
2961 /* Scan static map resolution vector */
2962 for (j = 0; j < vec_len (sm->to_resolve); j++)
2964 rp = sm->to_resolve + j;
2967 /* On this interface? */
2968 if (rp->sw_if_index == sw_if_index)
2970 /* Indetity mapping? */
2971 if (rp->l_addr.as_u32 == 0)
2972 l_addr.as_u32 = address[0].as_u32;
2974 l_addr.as_u32 = rp->l_addr.as_u32;
2975 /* Add the static mapping */
2976 rv = snat_add_static_mapping (l_addr,
2982 ~0 /* sw_if_index */,
2987 clib_warning ("snat_add_static_mapping returned %d",
2995 (void) snat_del_address(sm, address[0], 1, twice_nat);
3001 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
3004 ip4_main_t * ip4_main = sm->ip4_main;
3005 ip4_address_t * first_int_addr;
3006 snat_static_map_resolve_t *rp;
3007 u32 *indices_to_delete = 0;
3009 u32 *auto_add_sw_if_indices =
3010 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3012 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3013 0 /* just want the address*/);
3015 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
3017 if (auto_add_sw_if_indices[i] == sw_if_index)
3021 /* if have address remove it */
3023 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3026 for (j = 0; j < vec_len (sm->to_resolve); j++)
3028 rp = sm->to_resolve + j;
3029 if (rp->sw_if_index == sw_if_index)
3030 vec_add1 (indices_to_delete, j);
3032 if (vec_len(indices_to_delete))
3034 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3035 vec_del1(sm->to_resolve, j);
3036 vec_free(indices_to_delete);
3040 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
3042 vec_del1(sm->auto_add_sw_if_indices, i);
3045 return VNET_API_ERROR_VALUE_EXIST;
3052 return VNET_API_ERROR_NO_SUCH_ENTRY;
3054 /* add to the auto-address list */
3056 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3058 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3060 /* If the address is already bound - or static - add it now */
3062 snat_add_address (sm, first_int_addr, ~0, twice_nat);
3068 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3069 snat_protocol_t proto, u32 vrf_id, int is_in)
3071 snat_main_per_thread_data_t *tsm;
3072 clib_bihash_kv_8_8_t kv, value;
3074 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3075 snat_session_key_t key;
3077 clib_bihash_8_8_t *t;
3078 snat_user_key_t u_key;
3081 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3082 if (sm->num_workers > 1)
3084 vec_elt_at_index (sm->per_thread_data,
3085 sm->worker_in2out_cb (&ip, fib_index));
3087 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3089 key.addr.as_u32 = addr->as_u32;
3090 key.port = clib_host_to_net_u16 (port);
3091 key.protocol = proto;
3092 key.fib_index = fib_index;
3093 kv.key = key.as_u64;
3094 t = is_in ? &tsm->in2out : &tsm->out2in;
3095 if (!clib_bihash_search_8_8 (t, &kv, &value))
3097 s = pool_elt_at_index (tsm->sessions, value.value);
3098 kv.key = s->in2out.as_u64;
3099 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
3100 kv.key = s->out2in.as_u64;
3101 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
3102 u_key.addr = s->in2out.addr;
3103 u_key.fib_index = s->in2out.fib_index;
3104 kv.key = u_key.as_u64;
3105 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
3107 u = pool_elt_at_index (tsm->users, value.value);
3110 clib_dlist_remove (tsm->list_pool, s->per_user_index);
3111 pool_put (tsm->sessions, s);
3115 return VNET_API_ERROR_NO_SUCH_ENTRY;
3119 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3121 snat_main_t *sm = &snat_main;
3123 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3125 sm->psid_offset = psid_offset;
3126 sm->psid_length = psid_length;
3130 nat_set_alloc_addr_and_port_default (void)
3132 snat_main_t *sm = &snat_main;
3134 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;