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 <vpp/app/version.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ip/ip4.h>
23 #include <vnet/ip/ip_table.h>
24 #include <vnet/ip/reass/ip4_sv_reass.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/fib/ip4_fib.h>
27 #include <vnet/plugin/plugin.h>
28 #include <vppinfra/bihash_16_8.h>
30 #include <nat/lib/log.h>
31 #include <nat/lib/nat_syslog.h>
32 #include <nat/lib/nat_inlines.h>
33 #include <nat/lib/ipfix_logging.h>
35 #include <nat/nat44-ed/nat44_ed.h>
36 #include <nat/nat44-ed/nat44_ed_affinity.h>
37 #include <nat/nat44-ed/nat44_ed_inlines.h>
39 #include <vpp/stats/stat_segment.h>
41 snat_main_t snat_main;
43 static_always_inline void nat_validate_interface_counters (snat_main_t *sm,
46 #define skip_if_disabled() \
49 snat_main_t *sm = &snat_main; \
50 if (PREDICT_FALSE (!sm->enabled)) \
55 #define fail_if_enabled() \
58 snat_main_t *sm = &snat_main; \
59 if (PREDICT_FALSE (sm->enabled)) \
61 nat_log_err ("plugin enabled"); \
67 #define fail_if_disabled() \
70 snat_main_t *sm = &snat_main; \
71 if (PREDICT_FALSE (!sm->enabled)) \
73 nat_log_err ("plugin disabled"); \
79 /* Hook up input features */
80 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
81 .arc_name = "ip4-unicast",
82 .node_name = "nat-pre-in2out",
83 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
84 "ip4-sv-reassembly-feature"),
86 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
87 .arc_name = "ip4-unicast",
88 .node_name = "nat-pre-out2in",
89 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
90 "ip4-dhcp-client-detect",
91 "ip4-sv-reassembly-feature"),
93 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
94 .arc_name = "ip4-unicast",
95 .node_name = "nat44-in2out-worker-handoff",
96 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
98 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
99 .arc_name = "ip4-unicast",
100 .node_name = "nat44-out2in-worker-handoff",
101 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
102 "ip4-dhcp-client-detect"),
104 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
105 .arc_name = "ip4-unicast",
106 .node_name = "nat44-in2out",
107 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
109 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
110 .arc_name = "ip4-unicast",
111 .node_name = "nat44-out2in",
112 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
113 "ip4-dhcp-client-detect"),
115 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
116 .arc_name = "ip4-unicast",
117 .node_name = "nat44-ed-in2out",
118 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
120 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
121 .arc_name = "ip4-unicast",
122 .node_name = "nat44-ed-out2in",
123 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
124 "ip4-dhcp-client-detect"),
126 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
127 .arc_name = "ip4-unicast",
128 .node_name = "nat44-ed-classify",
129 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
131 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
132 .arc_name = "ip4-unicast",
133 .node_name = "nat44-handoff-classify",
134 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
136 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
137 .arc_name = "ip4-unicast",
138 .node_name = "nat44-in2out-fast",
139 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
141 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
142 .arc_name = "ip4-unicast",
143 .node_name = "nat44-out2in-fast",
144 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
145 "ip4-dhcp-client-detect"),
148 /* Hook up output features */
149 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
150 .arc_name = "ip4-output",
151 .node_name = "nat44-in2out-output",
152 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
153 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
155 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
156 .arc_name = "ip4-output",
157 .node_name = "nat44-in2out-output-worker-handoff",
158 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
159 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
161 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
162 .arc_name = "ip4-output",
163 .node_name = "nat-pre-in2out-output",
164 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
165 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
167 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
168 .arc_name = "ip4-output",
169 .node_name = "nat44-ed-in2out-output",
170 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
171 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
174 VLIB_PLUGIN_REGISTER () = {
175 .version = VPP_BUILD_VER,
176 .description = "Network Address Translation (NAT)",
179 static void nat44_ed_db_init (u32 translations, u32 translation_buckets);
181 static void nat44_ed_db_free ();
183 u32 nat_calc_bihash_buckets (u32 n_elts);
186 format_session_kvp (u8 * s, va_list * args)
188 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
190 s = format (s, "%U thread-index %llu session-index %llu", format_snat_key,
191 v->key, nat_value_get_thread_index (v),
192 nat_value_get_session_index (v));
198 format_static_mapping_kvp (u8 * s, va_list * args)
200 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
202 s = format (s, "%U static-mapping-index %llu",
203 format_snat_key, v->key, v->value);
209 format_ed_session_kvp (u8 * s, va_list * args)
211 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
215 ip4_address_t l_addr, r_addr;
218 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
220 "local %U:%d remote %U:%d proto %U fib %d thread-index %u "
222 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
223 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
224 format_ip_protocol, proto, fib_index,
225 ed_value_get_thread_index (v), ed_value_get_session_index (v));
231 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
234 per_vrf_sessions_unregister_session (s, thread_index);
236 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 0))
237 nat_elog_warn (sm, "flow hash del failed");
239 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
240 nat_elog_warn (sm, "flow hash del failed");
242 if (is_fwd_bypass_session (s))
247 if (is_affinity_sessions (s))
248 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
249 s->nat_proto, s->out2in.port);
252 nat_syslog_nat44_sdel (
253 0, s->in2out.fib_index, &s->in2out.addr, s->in2out.port,
254 &s->ext_host_nat_addr, s->ext_host_nat_port, &s->out2in.addr,
255 s->out2in.port, &s->ext_host_addr, s->ext_host_port, s->nat_proto,
256 is_twice_nat_session (s));
258 if (snat_is_unk_proto_session (s))
264 nat_ipfix_logging_nat44_ses_delete (thread_index,
265 s->in2out.addr.as_u32,
266 s->out2in.addr.as_u32,
270 s->in2out.fib_index);
273 /* Twice NAT address and port for external host */
274 if (is_twice_nat_session (s))
276 snat_free_outside_address_and_port (sm->twice_nat_addresses,
278 &s->ext_host_nat_addr,
279 s->ext_host_nat_port, s->nat_proto);
282 if (snat_is_session_static (s))
285 snat_free_outside_address_and_port (sm->addresses, thread_index,
286 &s->out2in.addr, s->out2in.port,
291 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
294 snat_main_t *sm = &snat_main;
295 fib_prefix_t prefix = {
297 .fp_proto = FIB_PROTOCOL_IP4,
299 .ip4.as_u32 = addr->as_u32,
302 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
305 fib_table_entry_update_one_path (fib_index,
308 (FIB_ENTRY_FLAG_CONNECTED |
309 FIB_ENTRY_FLAG_LOCAL |
310 FIB_ENTRY_FLAG_EXCLUSIVE),
314 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
316 fib_table_entry_delete (fib_index, &prefix, sm->fib_src_low);
320 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
325 vlib_thread_main_t *tm = vlib_get_thread_main ();
327 /* Check if address already exists */
328 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
330 if (ap->addr.as_u32 == addr->as_u32)
332 nat_log_err ("address exist");
333 return VNET_API_ERROR_VALUE_EXIST;
338 vec_add2 (sm->twice_nat_addresses, ap, 1);
340 vec_add2 (sm->addresses, ap, 1);
345 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
350 #define _(N, i, n, s) \
351 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
352 ap->busy_##n##_ports = 0; \
353 ap->busy_##n##_ports_per_thread = 0;\
354 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
361 /* Add external address to FIB */
362 pool_foreach (i, sm->interfaces)
364 if (nat_interface_is_inside (i))
367 snat_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
370 pool_foreach (i, sm->output_feature_interfaces)
372 if (nat_interface_is_inside (i))
375 snat_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
383 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
385 snat_static_mapping_t *m;
386 pool_foreach (m, sm->static_mappings)
388 if (is_addr_only_static_mapping (m) ||
389 is_out2in_only_static_mapping (m) ||
390 is_identity_static_mapping (m))
392 if (m->external_addr.as_u32 == addr.as_u32)
400 snat_add_static_mapping_when_resolved (snat_main_t *sm, ip4_address_t l_addr,
401 u16 l_port, u32 sw_if_index, u16 e_port,
402 u32 vrf_id, nat_protocol_t proto,
403 int addr_only, u8 *tag, int twice_nat,
404 int out2in_only, int identity_nat,
405 ip4_address_t pool_addr, int exact)
407 snat_static_map_resolve_t *rp;
409 vec_add2 (sm->to_resolve, rp, 1);
410 rp->l_addr.as_u32 = l_addr.as_u32;
412 rp->sw_if_index = sw_if_index;
416 rp->addr_only = addr_only;
417 rp->twice_nat = twice_nat;
418 rp->out2in_only = out2in_only;
419 rp->identity_nat = identity_nat;
420 rp->tag = vec_dup (tag);
421 rp->pool_addr = pool_addr;
426 get_thread_idx_by_port (u16 e_port)
428 snat_main_t *sm = &snat_main;
429 u32 thread_idx = sm->num_workers;
430 if (sm->num_workers > 1)
433 sm->first_worker_index +
434 sm->workers[(e_port - 1024) / sm->port_per_thread];
440 nat_ed_static_mapping_del_sessions (snat_main_t * sm,
441 snat_main_per_thread_data_t * tsm,
442 ip4_address_t l_addr,
445 u32 fib_index, int addr_only,
446 ip4_address_t e_addr, u16 e_port)
449 u32 *indexes_to_free = NULL;
450 pool_foreach (s, tsm->sessions) {
451 if (s->in2out.fib_index != fib_index ||
452 s->in2out.addr.as_u32 != l_addr.as_u32)
458 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
459 s->out2in.port != e_port ||
460 s->in2out.port != l_port ||
461 s->nat_proto != protocol)
465 if (is_lb_session (s))
467 if (!snat_is_session_static (s))
469 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
470 vec_add1 (indexes_to_free, s - tsm->sessions);
475 vec_foreach (ses_index, indexes_to_free)
477 s = pool_elt_at_index (tsm->sessions, *ses_index);
478 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
480 vec_free (indexes_to_free);
484 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
485 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
486 u32 sw_if_index, nat_protocol_t proto, int is_add,
487 twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag,
488 u8 identity_nat, ip4_address_t pool_addr, int exact)
490 snat_main_t *sm = &snat_main;
491 snat_static_mapping_t *m;
492 clib_bihash_kv_8_8_t kv, value;
493 snat_address_t *a = 0;
495 snat_interface_t *interface;
496 snat_main_per_thread_data_t *tsm;
497 snat_static_map_resolve_t *rp, *rp_match = 0;
498 nat44_lb_addr_port_t *local;
502 /* If the external address is a specific interface address */
503 if (sw_if_index != ~0)
505 ip4_address_t *first_int_addr;
507 for (i = 0; i < vec_len (sm->to_resolve); i++)
509 rp = sm->to_resolve + i;
510 if (rp->sw_if_index != sw_if_index ||
511 rp->l_addr.as_u32 != l_addr.as_u32 ||
512 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
517 if ((rp->l_port != l_port && rp->e_port != e_port)
518 || rp->proto != proto)
526 /* Might be already set... */
527 first_int_addr = ip4_interface_first_address
528 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
533 return VNET_API_ERROR_VALUE_EXIST;
535 snat_add_static_mapping_when_resolved (
536 sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto, addr_only,
537 tag, twice_nat, out2in_only, identity_nat, pool_addr, exact);
539 /* DHCP resolution required? */
540 if (first_int_addr == 0)
546 e_addr.as_u32 = first_int_addr->as_u32;
547 /* Identity mapping? */
548 if (l_addr.as_u32 == 0)
549 l_addr.as_u32 = e_addr.as_u32;
555 return VNET_API_ERROR_NO_SUCH_ENTRY;
557 vec_del1 (sm->to_resolve, i);
561 e_addr.as_u32 = first_int_addr->as_u32;
562 /* Identity mapping? */
563 if (l_addr.as_u32 == 0)
564 l_addr.as_u32 = e_addr.as_u32;
571 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
572 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
575 m = pool_elt_at_index (sm->static_mappings, value.value);
581 if (is_identity_static_mapping (m))
583 pool_foreach (local, m->locals)
585 if (local->vrf_id == vrf_id)
586 return VNET_API_ERROR_VALUE_EXIST;
588 pool_get (m->locals, local);
589 local->vrf_id = vrf_id;
591 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
593 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
594 m->proto, 0, m - sm->static_mappings);
595 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
599 return VNET_API_ERROR_VALUE_EXIST;
602 if (twice_nat && addr_only)
603 return VNET_API_ERROR_UNSUPPORTED;
605 /* Convert VRF id to FIB index */
608 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
610 /* If not specified use inside VRF id from SNAT plugin startup config */
613 fib_index = sm->inside_fib_index;
614 vrf_id = sm->inside_vrf_id;
615 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
618 if (!(out2in_only || identity_nat))
620 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
621 addr_only ? 0 : proto);
622 if (!clib_bihash_search_8_8
623 (&sm->static_mapping_by_local, &kv, &value))
624 return VNET_API_ERROR_VALUE_EXIST;
627 /* Find external address in allocated addresses and reserve port for
628 address and port pair mapping when dynamic translations enabled */
629 if (!(addr_only || sm->static_mapping_only || out2in_only))
631 for (i = 0; i < vec_len (sm->addresses); i++)
633 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
635 a = sm->addresses + i;
636 /* External port must be unused */
639 #define _(N, j, n, s) \
640 case NAT_PROTOCOL_##N: \
641 if (a->busy_##n##_port_refcounts[e_port]) \
642 return VNET_API_ERROR_INVALID_VALUE; \
643 ++a->busy_##n##_port_refcounts[e_port]; \
646 a->busy_##n##_ports++; \
647 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
652 default : nat_elog_info (sm, "unknown protocol");
653 return VNET_API_ERROR_INVALID_VALUE_2;
658 /* External address must be allocated */
659 if (!a && (l_addr.as_u32 != e_addr.as_u32))
661 if (sw_if_index != ~0)
663 for (i = 0; i < vec_len (sm->to_resolve); i++)
665 rp = sm->to_resolve + i;
668 if (rp->sw_if_index != sw_if_index &&
669 rp->l_addr.as_u32 != l_addr.as_u32 &&
670 rp->vrf_id != vrf_id && rp->l_port != l_port &&
671 rp->e_port != e_port && rp->proto != proto)
674 vec_del1 (sm->to_resolve, i);
678 return VNET_API_ERROR_NO_SUCH_ENTRY;
682 pool_get (sm->static_mappings, m);
683 clib_memset (m, 0, sizeof (*m));
684 m->tag = vec_dup (tag);
685 m->local_addr = l_addr;
686 m->external_addr = e_addr;
687 m->twice_nat = twice_nat;
689 if (twice_nat == TWICE_NAT && exact)
691 m->flags |= NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS;
692 m->pool_addr = pool_addr;
696 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
698 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
701 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
702 pool_get (m->locals, local);
703 local->vrf_id = vrf_id;
704 local->fib_index = fib_index;
709 m->fib_index = fib_index;
713 m->local_port = l_port;
714 m->external_port = e_port;
718 if (sm->num_workers > 1)
721 .src_address = m->local_addr,
723 vec_add1 (m->workers, nat44_ed_get_in2out_worker_index (
724 0, &ip, m->fib_index, 0));
725 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
728 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
732 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
733 0, m - sm->static_mappings);
734 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
737 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
738 m - sm->static_mappings);
739 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
745 if (sw_if_index != ~0)
748 return VNET_API_ERROR_NO_SUCH_ENTRY;
754 vrf_id = sm->inside_vrf_id;
756 pool_foreach (local, m->locals)
758 if (local->vrf_id == vrf_id)
759 find = local - m->locals;
762 return VNET_API_ERROR_NO_SUCH_ENTRY;
764 local = pool_elt_at_index (m->locals, find);
765 fib_index = local->fib_index;
766 pool_put (m->locals, local);
769 fib_index = m->fib_index;
771 /* Free external address port */
772 if (!(addr_only || sm->static_mapping_only || out2in_only))
774 for (i = 0; i < vec_len (sm->addresses); i++)
776 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
778 a = sm->addresses + i;
781 #define _(N, j, n, s) \
782 case NAT_PROTOCOL_##N: \
783 --a->busy_##n##_port_refcounts[e_port]; \
786 a->busy_##n##_ports--; \
787 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
792 default : nat_elog_info (sm, "unknown protocol");
793 return VNET_API_ERROR_INVALID_VALUE_2;
800 if (sm->num_workers > 1)
801 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
803 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
805 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
807 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
809 /* Delete session(s) for static mapping if exist */
810 if (!(sm->static_mapping_only) ||
811 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
813 nat_ed_static_mapping_del_sessions (
814 sm, tsm, m->local_addr, m->local_port, m->proto, fib_index,
815 addr_only, e_addr, e_port);
818 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
819 if (pool_elts (m->locals))
822 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
823 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
826 vec_free (m->workers);
827 /* Delete static mapping from pool */
828 pool_put (sm->static_mappings, m);
831 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
834 /* Add/delete external address to FIB */
835 pool_foreach (interface, sm->interfaces)
837 if (nat_interface_is_inside (interface))
840 snat_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, is_add);
843 pool_foreach (interface, sm->output_feature_interfaces)
845 if (nat_interface_is_inside (interface))
848 snat_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, is_add);
856 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
857 nat_protocol_t proto,
858 nat44_lb_addr_port_t * locals, u8 is_add,
859 twice_nat_type_t twice_nat, u8 out2in_only,
860 u8 * tag, u32 affinity)
862 snat_main_t *sm = &snat_main;
863 snat_static_mapping_t *m;
864 clib_bihash_kv_8_8_t kv, value;
865 snat_address_t *a = 0;
867 nat44_lb_addr_port_t *local;
868 snat_main_per_thread_data_t *tsm;
872 init_nat_k (&kv, e_addr, e_port, 0, proto);
873 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
876 m = pool_elt_at_index (sm->static_mappings, value.value);
881 return VNET_API_ERROR_VALUE_EXIST;
883 if (vec_len (locals) < 2)
884 return VNET_API_ERROR_INVALID_VALUE;
886 /* Find external address in allocated addresses and reserve port for
887 address and port pair mapping when dynamic translations enabled */
888 if (!(sm->static_mapping_only || out2in_only))
890 for (i = 0; i < vec_len (sm->addresses); i++)
892 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
894 a = sm->addresses + i;
895 /* External port must be unused */
898 #define _(N, j, n, s) \
899 case NAT_PROTOCOL_##N: \
900 if (a->busy_##n##_port_refcounts[e_port]) \
901 return VNET_API_ERROR_INVALID_VALUE; \
902 ++a->busy_##n##_port_refcounts[e_port]; \
905 a->busy_##n##_ports++; \
906 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
911 default : nat_elog_info (sm, "unknown protocol");
912 return VNET_API_ERROR_INVALID_VALUE_2;
917 /* External address must be allocated */
919 return VNET_API_ERROR_NO_SUCH_ENTRY;
922 pool_get (sm->static_mappings, m);
923 clib_memset (m, 0, sizeof (*m));
924 m->tag = vec_dup (tag);
925 m->external_addr = e_addr;
926 m->external_port = e_port;
928 m->twice_nat = twice_nat;
929 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
931 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
932 m->affinity = affinity;
935 m->affinity_per_service_list_head_index =
936 nat_affinity_get_per_service_list_head_index ();
938 m->affinity_per_service_list_head_index = ~0;
940 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
941 m - sm->static_mappings);
942 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
944 nat_elog_err (sm, "static_mapping_by_external key add failed");
945 return VNET_API_ERROR_UNSPECIFIED;
948 for (i = 0; i < vec_len (locals); i++)
950 locals[i].fib_index =
951 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
956 init_nat_kv (&kv, locals[i].addr, locals[i].port,
957 locals[i].fib_index, m->proto, 0,
958 m - sm->static_mappings);
959 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
961 locals[i].prefix = (i == 0) ? locals[i].probability :
962 (locals[i - 1].prefix + locals[i].probability);
963 pool_get (m->locals, local);
965 if (sm->num_workers > 1)
968 .src_address = locals[i].addr,
970 bitmap = clib_bitmap_set (
972 nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0), 1);
977 if (sm->num_workers > 1)
979 clib_bitmap_foreach (i, bitmap)
981 vec_add1(m->workers, i);
988 return VNET_API_ERROR_NO_SUCH_ENTRY;
990 if (!is_lb_static_mapping (m))
991 return VNET_API_ERROR_INVALID_VALUE;
993 /* Free external address port */
994 if (!(sm->static_mapping_only || out2in_only))
996 for (i = 0; i < vec_len (sm->addresses); i++)
998 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1000 a = sm->addresses + i;
1003 #define _(N, j, n, s) \
1004 case NAT_PROTOCOL_##N: \
1005 --a->busy_##n##_port_refcounts[e_port]; \
1006 if (e_port > 1024) \
1008 a->busy_##n##_ports--; \
1009 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1012 foreach_nat_protocol
1014 default : nat_elog_info (sm, "unknown protocol");
1015 return VNET_API_ERROR_INVALID_VALUE_2;
1022 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1023 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1025 nat_elog_err (sm, "static_mapping_by_external key del failed");
1026 return VNET_API_ERROR_UNSPECIFIED;
1029 pool_foreach (local, m->locals)
1031 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1035 init_nat_k (&kv, local->addr, local->port, local->fib_index,
1037 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv,
1040 nat_elog_err (sm, "static_mapping_by_local key del failed");
1041 return VNET_API_ERROR_UNSPECIFIED;
1045 if (sm->num_workers > 1)
1048 .src_address = local->addr,
1050 tsm = vec_elt_at_index (
1051 sm->per_thread_data,
1052 nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0));
1055 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1057 /* Delete sessions */
1058 pool_foreach (s, tsm->sessions)
1060 if (!(is_lb_session (s)))
1063 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1064 s->in2out.port != local->port)
1067 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1068 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1072 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1073 pool_free (m->locals);
1075 vec_free (m->workers);
1077 pool_put (sm->static_mappings, m);
1084 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1085 ip4_address_t l_addr, u16 l_port,
1086 nat_protocol_t proto, u32 vrf_id,
1087 u8 probability, u8 is_add)
1089 snat_main_t *sm = &snat_main;
1090 snat_static_mapping_t *m = 0;
1091 clib_bihash_kv_8_8_t kv, value;
1092 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1093 snat_main_per_thread_data_t *tsm;
1099 init_nat_k (&kv, e_addr, e_port, 0, proto);
1100 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1101 m = pool_elt_at_index (sm->static_mappings, value.value);
1104 return VNET_API_ERROR_NO_SUCH_ENTRY;
1106 if (!is_lb_static_mapping (m))
1107 return VNET_API_ERROR_INVALID_VALUE;
1109 pool_foreach (local, m->locals)
1111 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1112 (local->vrf_id == vrf_id))
1114 match_local = local;
1122 return VNET_API_ERROR_VALUE_EXIST;
1124 pool_get (m->locals, local);
1125 clib_memset (local, 0, sizeof (*local));
1126 local->addr.as_u32 = l_addr.as_u32;
1127 local->port = l_port;
1128 local->probability = probability;
1129 local->vrf_id = vrf_id;
1131 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1134 if (!is_out2in_only_static_mapping (m))
1136 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto, 0,
1137 m - sm->static_mappings);
1138 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1139 nat_elog_err (sm, "static_mapping_by_local key add failed");
1145 return VNET_API_ERROR_NO_SUCH_ENTRY;
1147 if (pool_elts (m->locals) < 3)
1148 return VNET_API_ERROR_UNSPECIFIED;
1150 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1153 if (!is_out2in_only_static_mapping (m))
1155 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1156 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1157 nat_elog_err (sm, "static_mapping_by_local key del failed");
1160 if (sm->num_workers > 1)
1163 .src_address = local->addr,
1165 tsm = vec_elt_at_index (
1166 sm->per_thread_data,
1167 nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index, 0));
1170 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1172 /* Delete sessions */
1173 pool_foreach (s, tsm->sessions) {
1174 if (!(is_lb_session (s)))
1177 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1178 s->in2out.port != match_local->port)
1181 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1182 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1185 pool_put (m->locals, match_local);
1188 vec_free (m->workers);
1190 pool_foreach (local, m->locals)
1192 vec_add1 (locals, local - m->locals);
1193 if (sm->num_workers > 1)
1196 ip.src_address.as_u32 = local->addr.as_u32,
1197 bitmap = clib_bitmap_set (
1199 nat44_ed_get_in2out_worker_index (0, &ip, local->fib_index, 0), 1);
1203 ASSERT (vec_len (locals) > 1);
1205 local = pool_elt_at_index (m->locals, locals[0]);
1206 local->prefix = local->probability;
1207 for (i = 1; i < vec_len (locals); i++)
1209 local = pool_elt_at_index (m->locals, locals[i]);
1210 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1211 local->prefix = local->probability + prev_local->prefix;
1214 /* Assign workers */
1215 if (sm->num_workers > 1)
1217 clib_bitmap_foreach (i, bitmap) { vec_add1(m->workers, i); }
1224 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1227 snat_address_t *a = 0;
1228 snat_session_t *ses;
1229 u32 *ses_to_be_removed = 0, *ses_index;
1230 snat_main_per_thread_data_t *tsm;
1231 snat_static_mapping_t *m;
1232 snat_interface_t *interface;
1234 snat_address_t *addresses =
1235 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1237 /* Find SNAT address */
1238 for (i = 0; i < vec_len (addresses); i++)
1240 if (addresses[i].addr.as_u32 == addr.as_u32)
1248 nat_log_err ("no such address");
1249 return VNET_API_ERROR_NO_SUCH_ENTRY;
1254 ip4_address_t pool_addr = { 0 };
1255 pool_foreach (m, sm->static_mappings)
1257 if (m->external_addr.as_u32 == addr.as_u32)
1258 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1259 m->local_port, m->external_port,
1261 is_addr_only_static_mapping(m), ~0,
1262 m->proto, 0 /* is_add */,
1264 is_out2in_only_static_mapping(m),
1266 is_identity_static_mapping(m),
1272 /* Check if address is used in some static mapping */
1273 if (is_snat_address_used_in_static_mapping (sm, addr))
1275 nat_log_err ("address used in static mapping");
1276 return VNET_API_ERROR_UNSPECIFIED;
1280 if (a->fib_index != ~0)
1281 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1283 /* Delete sessions using address */
1284 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1286 vec_foreach (tsm, sm->per_thread_data)
1288 pool_foreach (ses, tsm->sessions) {
1289 if (ses->out2in.addr.as_u32 == addr.as_u32)
1291 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1292 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1296 vec_foreach (ses_index, ses_to_be_removed)
1298 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1299 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1302 vec_free (ses_to_be_removed);
1306 #define _(N, i, n, s) \
1307 vec_free (a->busy_##n##_ports_per_thread);
1308 foreach_nat_protocol
1313 vec_del1 (sm->twice_nat_addresses, i);
1316 else vec_del1 (sm->addresses, i);
1318 /* Delete external address from FIB */
1319 pool_foreach (interface, sm->interfaces)
1321 if (nat_interface_is_inside (interface))
1324 snat_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
1327 pool_foreach (interface, sm->output_feature_interfaces)
1329 if (nat_interface_is_inside (interface))
1332 snat_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
1340 expire_per_vrf_sessions (u32 fib_index)
1342 per_vrf_sessions_t *per_vrf_sessions;
1343 snat_main_per_thread_data_t *tsm;
1344 snat_main_t *sm = &snat_main;
1346 vec_foreach (tsm, sm->per_thread_data)
1348 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1350 if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1351 (per_vrf_sessions->tx_fib_index == fib_index))
1353 per_vrf_sessions->expired = 1;
1360 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1362 snat_main_t *sm = &snat_main;
1365 // we don't care if it is outside/inside fib
1366 // we just care about their ref_count
1367 // if it reaches 0 sessions should expire
1368 // because the fib isn't valid for NAT anymore
1370 vec_foreach (fib, sm->fibs)
1372 if (fib->fib_index == fib_index)
1377 if (!fib->ref_count)
1379 vec_del1 (sm->fibs, fib - sm->fibs);
1380 expire_per_vrf_sessions (fib_index);
1390 vec_add2 (sm->fibs, fib, 1);
1392 fib->fib_index = fib_index;
1397 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1399 snat_main_t *sm = &snat_main;
1400 snat_interface_t *i;
1401 const char *feature_name, *del_feature_name;
1403 snat_static_mapping_t *m;
1404 nat_outside_fib_t *outside_fib;
1405 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1410 nat_log_err ("nat44 is disabled");
1411 return VNET_API_ERROR_UNSUPPORTED;
1414 pool_foreach (i, sm->output_feature_interfaces)
1416 if (i->sw_if_index == sw_if_index)
1418 nat_log_err ("error interface already configured");
1419 return VNET_API_ERROR_VALUE_EXIST;
1423 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1424 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1427 if (sm->num_workers > 1)
1429 is_inside ? "nat44-in2out-worker-handoff" :
1430 "nat44-out2in-worker-handoff";
1432 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1435 ASSERT (sm->frame_queue_nelts > 0);
1437 if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
1438 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1439 sm->frame_queue_nelts);
1441 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1442 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1443 sm->frame_queue_nelts);
1445 update_per_vrf_sessions_vec (fib_index, is_del);
1449 vec_foreach (outside_fib, sm->outside_fibs)
1451 if (outside_fib->fib_index == fib_index)
1455 outside_fib->refcount--;
1456 if (!outside_fib->refcount)
1457 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1460 outside_fib->refcount++;
1466 vec_add2 (sm->outside_fibs, outside_fib, 1);
1467 outside_fib->refcount = 1;
1468 outside_fib->fib_index = fib_index;
1473 pool_foreach (i, sm->interfaces)
1475 if (i->sw_if_index == sw_if_index)
1479 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1482 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1484 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1486 if (sm->num_workers > 1)
1488 del_feature_name = "nat44-handoff-classify";
1489 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1490 "nat44-out2in-worker-handoff";
1494 del_feature_name = "nat44-ed-classify";
1496 !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1499 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1502 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1503 sw_if_index, 0, 0, 0);
1504 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1505 sw_if_index, 1, 0, 0);
1509 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1512 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1513 sw_if_index, 0, 0, 0);
1514 pool_put (sm->interfaces, i);
1519 if ((nat_interface_is_inside (i) && is_inside) ||
1520 (nat_interface_is_outside (i) && !is_inside))
1523 if (sm->num_workers > 1)
1525 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1526 "nat44-out2in-worker-handoff";
1527 feature_name = "nat44-handoff-classify";
1532 !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1534 feature_name = "nat44-ed-classify";
1537 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1540 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1541 sw_if_index, 0, 0, 0);
1542 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1543 sw_if_index, 1, 0, 0);
1553 nat_log_err ("error interface couldn't be found");
1554 return VNET_API_ERROR_NO_SUCH_ENTRY;
1557 pool_get (sm->interfaces, i);
1558 i->sw_if_index = sw_if_index;
1560 nat_validate_interface_counters (sm, sw_if_index);
1562 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
1565 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1572 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1576 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1578 /* Add/delete external addresses to FIB */
1580 vec_foreach (ap, sm->addresses)
1581 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1583 pool_foreach (m, sm->static_mappings)
1585 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1588 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1595 snat_interface_add_del_output_feature (u32 sw_if_index,
1596 u8 is_inside, int is_del)
1598 snat_main_t *sm = &snat_main;
1599 snat_interface_t *i;
1601 snat_static_mapping_t *m;
1602 nat_outside_fib_t *outside_fib;
1603 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1608 nat_log_err ("nat44 is disabled");
1609 return VNET_API_ERROR_UNSUPPORTED;
1612 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1614 nat_log_err ("error unsupported");
1615 return VNET_API_ERROR_UNSUPPORTED;
1618 pool_foreach (i, sm->interfaces)
1620 if (i->sw_if_index == sw_if_index)
1622 nat_log_err ("error interface already configured");
1623 return VNET_API_ERROR_VALUE_EXIST;
1627 update_per_vrf_sessions_vec (fib_index, is_del);
1631 vec_foreach (outside_fib, sm->outside_fibs)
1633 if (outside_fib->fib_index == fib_index)
1637 outside_fib->refcount--;
1638 if (!outside_fib->refcount)
1639 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1642 outside_fib->refcount++;
1648 vec_add2 (sm->outside_fibs, outside_fib, 1);
1649 outside_fib->refcount = 1;
1650 outside_fib->fib_index = fib_index;
1658 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
1662 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
1669 if (sm->num_workers > 1)
1671 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
1675 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
1678 vnet_feature_enable_disable ("ip4-unicast",
1679 "nat44-out2in-worker-handoff",
1680 sw_if_index, !is_del, 0, 0);
1681 vnet_feature_enable_disable ("ip4-output",
1682 "nat44-in2out-output-worker-handoff",
1683 sw_if_index, !is_del, 0, 0);
1688 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
1692 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
1696 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
1697 sw_if_index, !is_del, 0, 0);
1698 vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
1699 sw_if_index, !is_del, 0, 0);
1703 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1704 sm->fq_in2out_output_index =
1705 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1707 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1708 sm->fq_out2in_index =
1709 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1711 pool_foreach (i, sm->output_feature_interfaces)
1713 if (i->sw_if_index == sw_if_index)
1716 pool_put (sm->output_feature_interfaces, i);
1718 return VNET_API_ERROR_VALUE_EXIST;
1726 nat_log_err ("error interface couldn't be found");
1727 return VNET_API_ERROR_NO_SUCH_ENTRY;
1730 pool_get (sm->output_feature_interfaces, i);
1731 i->sw_if_index = sw_if_index;
1733 nat_validate_interface_counters (sm, sw_if_index);
1735 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1737 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1739 /* Add/delete external addresses to FIB */
1744 vec_foreach (ap, sm->addresses)
1745 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1747 pool_foreach (m, sm->static_mappings)
1749 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1752 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1759 snat_set_workers (uword * bitmap)
1761 snat_main_t *sm = &snat_main;
1764 if (sm->num_workers < 2)
1765 return VNET_API_ERROR_FEATURE_DISABLED;
1767 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1768 return VNET_API_ERROR_INVALID_WORKER;
1770 vec_free (sm->workers);
1771 clib_bitmap_foreach (i, bitmap)
1773 vec_add1(sm->workers, i);
1774 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1775 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
1779 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1785 nat44_ed_set_frame_queue_nelts (u32 frame_queue_nelts)
1788 snat_main_t *sm = &snat_main;
1789 sm->frame_queue_nelts = frame_queue_nelts;
1794 snat_update_outside_fib (ip4_main_t * im, uword opaque,
1795 u32 sw_if_index, u32 new_fib_index,
1798 snat_main_t *sm = &snat_main;
1799 nat_outside_fib_t *outside_fib;
1800 snat_interface_t *i;
1804 if (!sm->enabled || (new_fib_index == old_fib_index)
1805 || (!vec_len (sm->outside_fibs)))
1810 pool_foreach (i, sm->interfaces)
1812 if (i->sw_if_index == sw_if_index)
1814 if (!(nat_interface_is_outside (i)))
1820 pool_foreach (i, sm->output_feature_interfaces)
1822 if (i->sw_if_index == sw_if_index)
1824 if (!(nat_interface_is_outside (i)))
1833 vec_foreach (outside_fib, sm->outside_fibs)
1835 if (outside_fib->fib_index == old_fib_index)
1837 outside_fib->refcount--;
1838 if (!outside_fib->refcount)
1839 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1844 vec_foreach (outside_fib, sm->outside_fibs)
1846 if (outside_fib->fib_index == new_fib_index)
1848 outside_fib->refcount++;
1856 vec_add2 (sm->outside_fibs, outside_fib, 1);
1857 outside_fib->refcount = 1;
1858 outside_fib->fib_index = new_fib_index;
1863 snat_update_outside_fib (ip4_main_t * im, uword opaque,
1864 u32 sw_if_index, u32 new_fib_index,
1868 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1871 ip4_address_t * address,
1873 u32 if_address_index, u32 is_delete);
1876 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1879 ip4_address_t * address,
1881 u32 if_address_index, u32 is_delete);
1884 test_key_calc_split ()
1886 ip4_address_t l_addr;
1887 l_addr.as_u8[0] = 1;
1888 l_addr.as_u8[1] = 1;
1889 l_addr.as_u8[2] = 1;
1890 l_addr.as_u8[3] = 1;
1891 ip4_address_t r_addr;
1892 r_addr.as_u8[0] = 2;
1893 r_addr.as_u8[1] = 2;
1894 r_addr.as_u8[2] = 2;
1895 r_addr.as_u8[3] = 2;
1899 u32 fib_index = 9000001;
1900 u32 thread_index = 3000000001;
1901 u32 session_index = 3000000221;
1902 clib_bihash_kv_16_8_t kv;
1903 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
1904 thread_index, session_index);
1905 ip4_address_t l_addr2;
1906 ip4_address_t r_addr2;
1907 clib_memset (&l_addr2, 0, sizeof (l_addr2));
1908 clib_memset (&r_addr2, 0, sizeof (r_addr2));
1913 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
1915 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
1916 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
1917 ASSERT (l_port == l_port2);
1918 ASSERT (r_port == r_port2);
1919 ASSERT (proto == proto2);
1920 ASSERT (fib_index == fib_index2);
1921 ASSERT (thread_index == ed_value_get_thread_index (&kv));
1922 ASSERT (session_index == ed_value_get_session_index (&kv));
1926 nat_protocol_t proto3 = ~0;
1927 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
1928 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
1929 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
1930 ASSERT (l_port == l_port2);
1931 ASSERT (proto == proto3);
1932 ASSERT (fib_index == fib_index2);
1935 static clib_error_t *
1936 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
1940 // TODO: consider removing all NAT interfaces
1943 fib_index = ip4_fib_index_from_table_id (table_id);
1944 if (fib_index != ~0)
1945 expire_per_vrf_sessions (fib_index);
1950 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
1953 nat44_set_node_indexes (snat_main_t * sm, vlib_main_t * vm)
1957 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
1958 sm->out2in_node_index = node->index;
1960 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
1961 sm->in2out_node_index = node->index;
1963 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-output");
1964 sm->in2out_output_node_index = node->index;
1967 #define nat_validate_simple_counter(c, i) \
1970 vlib_validate_simple_counter (&c, i); \
1971 vlib_zero_simple_counter (&c, i); \
1975 #define nat_init_simple_counter(c, n, sn) \
1979 c.stat_segment_name = sn; \
1980 nat_validate_simple_counter (c, 0); \
1984 static_always_inline void
1985 nat_validate_interface_counters (snat_main_t *sm, u32 sw_if_index)
1988 nat_validate_simple_counter (sm->counters.fastpath.in2out.x, sw_if_index); \
1989 nat_validate_simple_counter (sm->counters.fastpath.out2in.x, sw_if_index); \
1990 nat_validate_simple_counter (sm->counters.slowpath.in2out.x, sw_if_index); \
1991 nat_validate_simple_counter (sm->counters.slowpath.out2in.x, sw_if_index);
1992 foreach_nat_counter;
1994 nat_validate_simple_counter (sm->counters.hairpinning, sw_if_index);
1997 static clib_error_t *
1998 nat_init (vlib_main_t * vm)
2000 snat_main_t *sm = &snat_main;
2001 vlib_thread_main_t *tm = vlib_get_thread_main ();
2002 vlib_thread_registration_t *tr;
2003 ip4_add_del_interface_address_callback_t cbi = { 0 };
2004 ip4_table_bind_callback_t cbt = { 0 };
2005 u32 i, num_threads = 0;
2006 uword *p, *bitmap = 0;
2008 clib_memset (sm, 0, sizeof (*sm));
2011 sm->vnet_main = vnet_get_main ();
2013 sm->ip4_main = &ip4_main;
2014 sm->api_main = vlibapi_get_main ();
2015 sm->ip4_lookup_main = &ip4_main.lookup_main;
2017 // frame queue indices used for handoff
2018 sm->fq_out2in_index = ~0;
2019 sm->fq_in2out_index = ~0;
2020 sm->fq_in2out_output_index = ~0;
2022 sm->log_level = NAT_LOG_ERROR;
2024 nat44_set_node_indexes (sm, vm);
2025 sm->log_class = vlib_log_register_class ("nat", 0);
2026 nat_ipfix_logging_init (vm);
2028 nat_init_simple_counter (sm->total_sessions, "total-sessions",
2029 "/nat44-ed/total-sessions");
2030 sm->max_cfg_sessions_gauge = stat_segment_new_entry (
2031 (u8 *) "/nat44-ed/max-cfg-sessions", STAT_DIR_TYPE_SCALAR_INDEX);
2034 nat_init_simple_counter (sm->counters.fastpath.in2out.x, #x, \
2035 "/nat44-ed/in2out/fastpath/" #x); \
2036 nat_init_simple_counter (sm->counters.fastpath.out2in.x, #x, \
2037 "/nat44-ed/out2in/fastpath/" #x); \
2038 nat_init_simple_counter (sm->counters.slowpath.in2out.x, #x, \
2039 "/nat44-ed/in2out/slowpath/" #x); \
2040 nat_init_simple_counter (sm->counters.slowpath.out2in.x, #x, \
2041 "/nat44-ed/out2in/slowpath/" #x);
2042 foreach_nat_counter;
2044 nat_init_simple_counter (sm->counters.hairpinning, "hairpinning",
2045 "/nat44-ed/hairpinning");
2047 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2050 tr = (vlib_thread_registration_t *) p[0];
2053 sm->num_workers = tr->count;
2054 sm->first_worker_index = tr->first_index;
2057 num_threads = tm->n_vlib_mains - 1;
2058 sm->port_per_thread = 0xffff - 1024;
2059 vec_validate (sm->per_thread_data, num_threads);
2061 /* Use all available workers by default */
2062 if (sm->num_workers > 1)
2065 for (i = 0; i < sm->num_workers; i++)
2066 bitmap = clib_bitmap_set (bitmap, i, 1);
2067 snat_set_workers (bitmap);
2068 clib_bitmap_free (bitmap);
2071 sm->per_thread_data[0].snat_thread_index = 0;
2073 /* callbacks to call when interface address changes. */
2074 cbi.function = snat_ip4_add_del_interface_address_cb;
2075 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2076 cbi.function = nat_ip4_add_del_addr_only_sm_cb;
2077 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2079 /* callbacks to call when interface to table biding changes */
2080 cbt.function = snat_update_outside_fib;
2081 vec_add1 (sm->ip4_main->table_bind_callbacks, cbt);
2084 fib_source_allocate ("nat-low", FIB_SOURCE_PRIORITY_LOW,
2085 FIB_SOURCE_BH_SIMPLE);
2087 fib_source_allocate ("nat-hi", FIB_SOURCE_PRIORITY_HI,
2088 FIB_SOURCE_BH_SIMPLE);
2090 nat_affinity_init (vm);
2091 test_key_calc_split ();
2093 return nat44_api_hookup (vm);
2096 VLIB_INIT_FUNCTION (nat_init);
2099 nat44_plugin_enable (nat44_config_t c)
2101 snat_main_t *sm = &snat_main;
2105 // UPDATE based on these appropriate API/CLI
2106 // c.static_mapping_only + c.connection_tracking
2107 // - supported in NAT EI & NAT ED
2108 // c.out2in_dpo, c.static_mapping_only
2109 // - supported in NAT EI
2111 if (c.static_mapping_only && !c.connection_tracking)
2113 nat_log_err ("unsupported combination of configuration");
2117 // nat44 feature configuration
2118 sm->static_mapping_only = c.static_mapping_only;
2119 sm->static_mapping_connection_tracking = c.connection_tracking;
2121 sm->forwarding_enabled = 0;
2122 sm->mss_clamping = 0;
2123 sm->pat = (!c.static_mapping_only ||
2124 (c.static_mapping_only && c.connection_tracking));
2127 c.sessions = 63 * 1024;
2129 sm->max_translations_per_thread = c.sessions;
2130 stat_segment_set_state_counter (sm->max_cfg_sessions_gauge,
2131 sm->max_translations_per_thread);
2132 sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2135 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2137 sm->inside_vrf_id = c.inside_vrf;
2138 sm->inside_fib_index =
2139 fib_table_find_or_create_and_lock
2140 (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2142 sm->outside_vrf_id = c.outside_vrf;
2143 sm->outside_fib_index = fib_table_find_or_create_and_lock (
2144 FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2146 nat44_ed_db_init (sm->max_translations_per_thread, sm->translation_buckets);
2148 nat_affinity_enable ();
2150 nat_reset_timeouts (&sm->timeouts);
2152 vlib_zero_simple_counter (&sm->total_sessions, 0);
2154 if (!sm->frame_queue_nelts)
2155 sm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
2164 nat44_addresses_free (snat_address_t ** addresses)
2167 vec_foreach (ap, *addresses)
2169 #define _(N, i, n, s) \
2170 vec_free (ap->busy_##n##_ports_per_thread);
2171 foreach_nat_protocol
2174 vec_free (*addresses);
2179 nat44_plugin_disable ()
2181 snat_main_t *sm = &snat_main;
2182 snat_interface_t *i, *vec;
2185 fail_if_disabled ();
2187 // first unregister all nodes from interfaces
2188 vec = vec_dup (sm->interfaces);
2189 vec_foreach (i, vec)
2191 if (nat_interface_is_inside(i))
2192 error = snat_interface_add_del (i->sw_if_index, 1, 1);
2193 if (nat_interface_is_outside(i))
2194 error = snat_interface_add_del (i->sw_if_index, 0, 1);
2198 nat_log_err ("error occurred while removing interface %u",
2205 vec = vec_dup (sm->output_feature_interfaces);
2206 vec_foreach (i, vec)
2208 if (nat_interface_is_inside(i))
2209 error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
2210 if (nat_interface_is_outside(i))
2211 error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
2215 nat_log_err ("error occurred while removing interface %u",
2220 sm->output_feature_interfaces = 0;
2222 vec_free (sm->max_translations_per_fib);
2224 nat44_ed_db_free ();
2226 nat44_addresses_free (&sm->addresses);
2227 nat44_addresses_free (&sm->twice_nat_addresses);
2229 vec_free (sm->to_resolve);
2230 vec_free (sm->auto_add_sw_if_indices);
2231 vec_free (sm->auto_add_sw_if_indices_twice_nat);
2234 sm->auto_add_sw_if_indices = 0;
2235 sm->auto_add_sw_if_indices_twice_nat = 0;
2237 sm->forwarding_enabled = 0;
2240 clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
2246 nat44_ed_forwarding_enable_disable (u8 is_enable)
2248 snat_main_per_thread_data_t *tsm;
2249 snat_main_t *sm = &snat_main;
2252 u32 *ses_to_be_removed = 0, *ses_index;
2254 sm->forwarding_enabled = is_enable != 0;
2259 vec_foreach (tsm, sm->per_thread_data)
2261 pool_foreach (s, tsm->sessions)
2263 if (is_fwd_bypass_session (s))
2265 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2268 vec_foreach (ses_index, ses_to_be_removed)
2270 s = pool_elt_at_index (tsm->sessions, ses_index[0]);
2271 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2272 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
2275 vec_free (ses_to_be_removed);
2280 snat_free_outside_address_and_port (snat_address_t *addresses,
2281 u32 thread_index, ip4_address_t *addr,
2282 u16 port, nat_protocol_t protocol)
2284 snat_main_t *sm = &snat_main;
2287 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2289 for (address_index = 0; address_index < vec_len (addresses);
2292 if (addresses[address_index].addr.as_u32 == addr->as_u32)
2296 ASSERT (address_index < vec_len (addresses));
2298 a = addresses + address_index;
2302 #define _(N, i, n, s) \
2303 case NAT_PROTOCOL_##N: \
2304 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2305 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2306 a->busy_##n##_ports--; \
2307 a->busy_##n##_ports_per_thread[thread_index]--; \
2309 foreach_nat_protocol
2311 default : nat_elog_info (sm, "unknown protocol");
2317 nat_set_outside_address_and_port (snat_address_t *addresses, u32 thread_index,
2318 ip4_address_t addr, u16 port,
2319 nat_protocol_t protocol)
2321 snat_main_t *sm = &snat_main;
2322 snat_address_t *a = 0;
2324 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2326 for (address_index = 0; address_index < vec_len (addresses);
2329 if (addresses[address_index].addr.as_u32 != addr.as_u32)
2332 a = addresses + address_index;
2335 #define _(N, j, n, s) \
2336 case NAT_PROTOCOL_##N: \
2337 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2338 return VNET_API_ERROR_INSTANCE_IN_USE; \
2339 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2340 a->busy_##n##_ports_per_thread[thread_index]++; \
2341 a->busy_##n##_ports++; \
2343 foreach_nat_protocol
2345 default : nat_elog_info (sm, "unknown protocol");
2350 return VNET_API_ERROR_NO_SUCH_ENTRY;
2354 snat_static_mapping_match (vlib_main_t *vm, snat_main_t *sm,
2355 ip4_address_t match_addr, u16 match_port,
2356 u32 match_fib_index, nat_protocol_t match_protocol,
2357 ip4_address_t *mapping_addr, u16 *mapping_port,
2358 u32 *mapping_fib_index, u8 by_external,
2359 u8 *is_addr_only, twice_nat_type_t *twice_nat,
2360 lb_nat_type_t *lb, ip4_address_t *ext_host_addr,
2361 u8 *is_identity_nat, snat_static_mapping_t **out)
2363 clib_bihash_kv_8_8_t kv, value;
2364 clib_bihash_8_8_t *mapping_hash;
2365 snat_static_mapping_t *m;
2366 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2367 nat44_lb_addr_port_t *local;
2372 mapping_hash = &sm->static_mapping_by_local;
2373 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2375 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2377 /* Try address only mapping */
2378 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2379 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2385 mapping_hash = &sm->static_mapping_by_external;
2386 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2387 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2389 /* Try address only mapping */
2390 init_nat_k (&kv, match_addr, 0, 0, 0);
2391 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2396 m = pool_elt_at_index (sm->static_mappings, value.value);
2400 if (is_lb_static_mapping (m))
2402 if (PREDICT_FALSE (lb != 0))
2403 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2404 if (m->affinity && !nat_affinity_find_and_lock (
2405 vm, ext_host_addr[0], match_addr,
2406 match_protocol, match_port, &backend_index))
2408 local = pool_elt_at_index (m->locals, backend_index);
2409 *mapping_addr = local->addr;
2410 *mapping_port = local->port;
2411 *mapping_fib_index = local->fib_index;
2414 // pick locals matching this worker
2415 if (PREDICT_FALSE (sm->num_workers > 1))
2417 u32 thread_index = vlib_get_thread_index ();
2418 pool_foreach_index (i, m->locals)
2420 local = pool_elt_at_index (m->locals, i);
2423 .src_address = local->addr,
2426 if (nat44_ed_get_in2out_worker_index (0, &ip, m->fib_index,
2432 ASSERT (vec_len (tmp) != 0);
2436 pool_foreach_index (i, m->locals)
2441 hi = vec_len (tmp) - 1;
2442 local = pool_elt_at_index (m->locals, tmp[hi]);
2443 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2446 mid = ((hi - lo) >> 1) + lo;
2447 local = pool_elt_at_index (m->locals, tmp[mid]);
2448 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2450 local = pool_elt_at_index (m->locals, tmp[lo]);
2451 if (!(local->prefix >= rand))
2453 *mapping_addr = local->addr;
2454 *mapping_port = local->port;
2455 *mapping_fib_index = local->fib_index;
2458 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
2459 match_protocol, match_port,
2460 tmp[lo], m->affinity,
2461 m->affinity_per_service_list_head_index))
2462 nat_elog_info (sm, "create affinity record failed");
2468 if (PREDICT_FALSE (lb != 0))
2470 *mapping_fib_index = m->fib_index;
2471 *mapping_addr = m->local_addr;
2472 /* Address only mapping doesn't change port */
2473 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2479 *mapping_addr = m->external_addr;
2480 /* Address only mapping doesn't change port */
2481 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2483 *mapping_fib_index = sm->outside_fib_index;
2487 if (PREDICT_FALSE (is_addr_only != 0))
2488 *is_addr_only = is_addr_only_static_mapping (m);
2490 if (PREDICT_FALSE (twice_nat != 0))
2491 *twice_nat = m->twice_nat;
2493 if (PREDICT_FALSE (is_identity_nat != 0))
2494 *is_identity_nat = is_identity_static_mapping (m);
2503 nat44_ed_get_in2out_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
2504 u32 rx_fib_index, u8 is_output)
2506 snat_main_t *sm = &snat_main;
2507 u32 next_worker_index = sm->first_worker_index;
2510 clib_bihash_kv_16_8_t kv16, value16;
2512 u32 fib_index = rx_fib_index;
2515 if (PREDICT_FALSE (is_output))
2517 fib_index = sm->outside_fib_index;
2518 nat_outside_fib_t *outside_fib;
2519 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
2520 fib_prefix_t pfx = {
2521 .fp_proto = FIB_PROTOCOL_IP4,
2524 .ip4.as_u32 = ip->dst_address.as_u32,
2528 switch (vec_len (sm->outside_fibs))
2531 fib_index = sm->outside_fib_index;
2534 fib_index = sm->outside_fibs[0].fib_index;
2537 vec_foreach (outside_fib, sm->outside_fibs)
2539 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
2540 if (FIB_NODE_INDEX_INVALID != fei)
2542 if (fib_entry_get_resolving_interface (fei) != ~0)
2544 fib_index = outside_fib->fib_index;
2553 init_ed_k (&kv16, ip->src_address, vnet_buffer (b)->ip.reass.l4_src_port,
2554 ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
2555 fib_index, ip->protocol);
2557 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16))
2559 next_worker_index = ed_value_get_thread_index (&value16);
2560 vnet_buffer2 (b)->nat.cached_session_index =
2561 ed_value_get_session_index (&value16);
2566 init_ed_k (&kv16, ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
2567 ip->src_address, vnet_buffer (b)->ip.reass.l4_src_port,
2568 rx_fib_index, ip->protocol);
2569 if (!clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16))
2571 next_worker_index = ed_value_get_thread_index (&value16);
2572 vnet_buffer2 (b)->nat.cached_dst_nat_session_index =
2573 ed_value_get_session_index (&value16);
2578 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2579 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2581 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2582 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2584 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2587 if (PREDICT_TRUE (!is_output))
2589 nat_elog_debug_handoff (sm, "HANDOFF IN2OUT", next_worker_index,
2591 clib_net_to_host_u32 (ip->src_address.as_u32),
2592 clib_net_to_host_u32 (ip->dst_address.as_u32));
2596 nat_elog_debug_handoff (sm, "HANDOFF IN2OUT-OUTPUT-FEATURE",
2597 next_worker_index, rx_fib_index,
2598 clib_net_to_host_u32 (ip->src_address.as_u32),
2599 clib_net_to_host_u32 (ip->dst_address.as_u32));
2602 return next_worker_index;
2606 nat44_ed_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip,
2607 u32 rx_fib_index, u8 is_output)
2609 snat_main_t *sm = &snat_main;
2610 clib_bihash_kv_8_8_t kv, value;
2611 clib_bihash_kv_16_8_t kv16, value16;
2613 u32 proto, next_worker_index = 0;
2615 snat_static_mapping_t *m;
2618 proto = ip_proto_to_nat_proto (ip->protocol);
2620 if (PREDICT_FALSE (proto == NAT_PROTOCOL_ICMP))
2622 ip4_address_t lookup_saddr, lookup_daddr;
2623 u16 lookup_sport, lookup_dport;
2625 if (!nat_get_icmp_session_lookup_values (
2626 b, ip, &lookup_saddr, &lookup_sport, &lookup_daddr, &lookup_dport,
2629 init_ed_k (&kv16, lookup_saddr, lookup_sport, lookup_daddr,
2630 lookup_dport, rx_fib_index, lookup_protocol);
2632 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
2634 next_worker_index = ed_value_get_thread_index (&value16);
2635 nat_elog_debug_handoff (
2636 sm, "HANDOFF OUT2IN (session)", next_worker_index,
2637 rx_fib_index, clib_net_to_host_u32 (ip->src_address.as_u32),
2638 clib_net_to_host_u32 (ip->dst_address.as_u32));
2639 return next_worker_index;
2644 init_ed_k (&kv16, ip->src_address, vnet_buffer (b)->ip.reass.l4_src_port,
2645 ip->dst_address, vnet_buffer (b)->ip.reass.l4_dst_port,
2646 rx_fib_index, ip->protocol);
2649 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
2651 vnet_buffer2 (b)->nat.cached_session_index =
2652 ed_value_get_session_index (&value16);
2653 next_worker_index = ed_value_get_thread_index (&value16);
2654 nat_elog_debug_handoff (sm, "HANDOFF OUT2IN (session)",
2655 next_worker_index, rx_fib_index,
2656 clib_net_to_host_u32 (ip->src_address.as_u32),
2657 clib_net_to_host_u32 (ip->dst_address.as_u32));
2658 return next_worker_index;
2661 /* first try static mappings without port */
2662 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2664 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
2665 if (!clib_bihash_search_8_8
2666 (&sm->static_mapping_by_external, &kv, &value))
2668 m = pool_elt_at_index (sm->static_mappings, value.value);
2669 next_worker_index = m->workers[0];
2674 /* unknown protocol */
2675 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
2677 /* use current thread */
2678 next_worker_index = vlib_get_thread_index ();
2682 port = vnet_buffer (b)->ip.reass.l4_dst_port;
2684 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2686 udp_header_t *udp = ip4_next_header (ip);
2687 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2688 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2689 if (!icmp_type_is_error_message
2690 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
2691 port = vnet_buffer (b)->ip.reass.l4_src_port;
2694 /* if error message, then it's not fragmented and we can access it */
2695 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2696 proto = ip_proto_to_nat_proto (inner_ip->protocol);
2697 void *l4_header = ip4_next_header (inner_ip);
2700 case NAT_PROTOCOL_ICMP:
2701 icmp = (icmp46_header_t *) l4_header;
2702 echo = (icmp_echo_header_t *) (icmp + 1);
2703 port = echo->identifier;
2705 case NAT_PROTOCOL_UDP:
2706 case NAT_PROTOCOL_TCP:
2707 port = ((tcp_udp_header_t *) l4_header)->src_port;
2710 next_worker_index = vlib_get_thread_index ();
2716 /* try static mappings with port */
2717 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2719 init_nat_k (&kv, ip->dst_address, port, 0, proto);
2720 if (!clib_bihash_search_8_8
2721 (&sm->static_mapping_by_external, &kv, &value))
2723 m = pool_elt_at_index (sm->static_mappings, value.value);
2724 if (!is_lb_static_mapping (m))
2726 next_worker_index = m->workers[0];
2730 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2731 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2733 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2735 m->workers[hash & (_vec_len (m->workers) - 1)];
2737 next_worker_index = m->workers[hash % _vec_len (m->workers)];
2742 /* worker by outside port */
2743 next_worker_index = sm->first_worker_index;
2744 next_worker_index +=
2745 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2748 nat_elog_debug_handoff (sm, "HANDOFF OUT2IN", next_worker_index,
2750 clib_net_to_host_u32 (ip->src_address.as_u32),
2751 clib_net_to_host_u32 (ip->dst_address.as_u32));
2752 return next_worker_index;
2756 nat44_get_max_session_limit ()
2758 snat_main_t *sm = &snat_main;
2759 u32 max_limit = 0, len = 0;
2761 for (; len < vec_len (sm->max_translations_per_fib); len++)
2763 if (max_limit < sm->max_translations_per_fib[len])
2764 max_limit = sm->max_translations_per_fib[len];
2770 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
2772 snat_main_t *sm = &snat_main;
2773 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2774 u32 len = vec_len (sm->max_translations_per_fib);
2776 if (len <= fib_index)
2778 vec_validate (sm->max_translations_per_fib, fib_index + 1);
2780 for (; len < vec_len (sm->max_translations_per_fib); len++)
2781 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
2784 sm->max_translations_per_fib[fib_index] = session_limit;
2789 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
2791 snat_main_t *sm = &snat_main;
2793 if (nat44_set_session_limit (session_limit, vrf_id))
2795 sm->max_translations_per_thread = nat44_get_max_session_limit ();
2797 stat_segment_set_state_counter (sm->max_cfg_sessions_gauge,
2798 sm->max_translations_per_thread);
2800 sm->translation_buckets =
2801 nat_calc_bihash_buckets (sm->max_translations_per_thread);
2803 nat44_ed_sessions_clear ();
2808 nat44_ed_worker_db_init (snat_main_per_thread_data_t *tsm, u32 translations,
2809 u32 translation_buckets)
2813 pool_alloc (tsm->sessions, translations);
2814 pool_alloc (tsm->lru_pool, translations);
2816 pool_get (tsm->lru_pool, head);
2817 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
2818 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
2820 pool_get (tsm->lru_pool, head);
2821 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
2822 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
2824 pool_get (tsm->lru_pool, head);
2825 tsm->udp_lru_head_index = head - tsm->lru_pool;
2826 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
2828 pool_get (tsm->lru_pool, head);
2829 tsm->icmp_lru_head_index = head - tsm->lru_pool;
2830 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
2832 pool_get (tsm->lru_pool, head);
2833 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
2834 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
2838 reinit_ed_flow_hash ()
2840 snat_main_t *sm = &snat_main;
2841 // we expect 2 flows per session, so multiply translation_buckets by 2
2842 clib_bihash_init_16_8 (
2843 &sm->flow_hash, "ed-flow-hash",
2844 clib_max (1, sm->num_workers) * 2 * sm->translation_buckets, 0);
2845 clib_bihash_set_kvp_format_fn_16_8 (&sm->flow_hash, format_ed_session_kvp);
2849 nat44_ed_db_init (u32 translations, u32 translation_buckets)
2851 snat_main_t *sm = &snat_main;
2852 snat_main_per_thread_data_t *tsm;
2853 u32 static_mapping_buckets = 1024;
2854 u32 static_mapping_memory_size = 64 << 20;
2856 reinit_ed_flow_hash ();
2858 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2859 "static_mapping_by_local", static_mapping_buckets,
2860 static_mapping_memory_size);
2861 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2862 format_static_mapping_kvp);
2864 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2865 "static_mapping_by_external", static_mapping_buckets,
2866 static_mapping_memory_size);
2867 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2868 format_static_mapping_kvp);
2872 vec_foreach (tsm, sm->per_thread_data)
2874 nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
2875 sm->translation_buckets);
2881 nat44_ed_worker_db_free (snat_main_per_thread_data_t *tsm)
2883 pool_free (tsm->lru_pool);
2884 pool_free (tsm->sessions);
2885 vec_free (tsm->per_vrf_sessions_vec);
2891 snat_main_t *sm = &snat_main;
2892 snat_main_per_thread_data_t *tsm;
2894 pool_free (sm->static_mappings);
2895 clib_bihash_free_16_8 (&sm->flow_hash);
2896 clib_bihash_free_8_8 (&sm->static_mapping_by_local);
2897 clib_bihash_free_8_8 (&sm->static_mapping_by_external);
2901 vec_foreach (tsm, sm->per_thread_data)
2903 nat44_ed_worker_db_free (tsm);
2909 nat44_ed_sessions_clear ()
2911 snat_main_t *sm = &snat_main;
2912 snat_main_per_thread_data_t *tsm;
2914 reinit_ed_flow_hash ();
2918 vec_foreach (tsm, sm->per_thread_data)
2921 nat44_ed_worker_db_free (tsm);
2922 nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
2923 sm->translation_buckets);
2926 vlib_zero_simple_counter (&sm->total_sessions, 0);
2930 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2933 ip4_address_t * address,
2935 u32 if_address_index, u32 is_delete)
2937 snat_main_t *sm = &snat_main;
2938 snat_static_map_resolve_t *rp;
2939 snat_static_mapping_t *m;
2940 clib_bihash_kv_8_8_t kv, value;
2942 ip4_address_t l_addr;
2947 for (i = 0; i < vec_len (sm->to_resolve); i++)
2949 rp = sm->to_resolve + i;
2950 if (rp->addr_only == 0)
2952 if (rp->sw_if_index == sw_if_index)
2959 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
2960 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
2961 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2964 m = pool_elt_at_index (sm->static_mappings, value.value);
2968 /* Don't trip over lease renewal, static config */
2978 /* Indetity mapping? */
2979 if (rp->l_addr.as_u32 == 0)
2980 l_addr.as_u32 = address[0].as_u32;
2982 l_addr.as_u32 = rp->l_addr.as_u32;
2983 /* Add the static mapping */
2984 rv = snat_add_static_mapping (l_addr,
2989 rp->addr_only, ~0 /* sw_if_index */ ,
2990 rp->proto, !is_delete, rp->twice_nat,
2991 rp->out2in_only, rp->tag, rp->identity_nat,
2992 rp->pool_addr, rp->exact);
2994 nat_elog_notice_X1 (sm, "snat_add_static_mapping returned %d", "i4", rv);
2998 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3001 ip4_address_t * address,
3003 u32 if_address_index, u32 is_delete)
3005 snat_main_t *sm = &snat_main;
3006 snat_static_map_resolve_t *rp;
3007 ip4_address_t l_addr;
3011 snat_address_t *addresses = sm->addresses;
3016 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
3018 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3022 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3025 addresses = sm->twice_nat_addresses;
3026 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3035 /* Don't trip over lease renewal, static config */
3036 for (j = 0; j < vec_len (addresses); j++)
3037 if (addresses[j].addr.as_u32 == address->as_u32)
3040 (void) snat_add_address (sm, address, ~0, twice_nat);
3041 /* Scan static map resolution vector */
3042 for (j = 0; j < vec_len (sm->to_resolve); j++)
3044 rp = sm->to_resolve + j;
3047 /* On this interface? */
3048 if (rp->sw_if_index == sw_if_index)
3050 /* Indetity mapping? */
3051 if (rp->l_addr.as_u32 == 0)
3052 l_addr.as_u32 = address[0].as_u32;
3054 l_addr.as_u32 = rp->l_addr.as_u32;
3055 /* Add the static mapping */
3056 rv = snat_add_static_mapping (
3057 l_addr, address[0], rp->l_port, rp->e_port, rp->vrf_id,
3058 rp->addr_only, ~0 /* sw_if_index */, rp->proto, 1,
3059 rp->twice_nat, rp->out2in_only, rp->tag, rp->identity_nat,
3060 rp->pool_addr, rp->exact);
3062 nat_elog_notice_X1 (sm, "snat_add_static_mapping returned %d",
3070 (void) snat_del_address (sm, address[0], 1, twice_nat);
3076 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3079 ip4_main_t *ip4_main = sm->ip4_main;
3080 ip4_address_t *first_int_addr;
3081 snat_static_map_resolve_t *rp;
3082 u32 *indices_to_delete = 0;
3084 u32 *auto_add_sw_if_indices =
3086 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3088 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3091 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3093 if (auto_add_sw_if_indices[i] == sw_if_index)
3097 /* if have address remove it */
3099 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3102 for (j = 0; j < vec_len (sm->to_resolve); j++)
3104 rp = sm->to_resolve + j;
3105 if (rp->sw_if_index == sw_if_index)
3106 vec_add1 (indices_to_delete, j);
3108 if (vec_len (indices_to_delete))
3110 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3111 vec_del1 (sm->to_resolve, j);
3112 vec_free (indices_to_delete);
3116 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3118 vec_del1 (sm->auto_add_sw_if_indices, i);
3121 return VNET_API_ERROR_VALUE_EXIST;
3128 return VNET_API_ERROR_NO_SUCH_ENTRY;
3130 /* add to the auto-address list */
3132 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3134 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3136 /* If the address is already bound - or static - add it now */
3138 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3144 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3145 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3146 u32 vrf_id, int is_in)
3149 clib_bihash_kv_16_8_t kv, value;
3150 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3152 snat_main_per_thread_data_t *tsm;
3154 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3155 if (sm->num_workers > 1)
3156 tsm = vec_elt_at_index (
3157 sm->per_thread_data,
3158 nat44_ed_get_in2out_worker_index (0, &ip, fib_index, 0));
3160 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3162 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
3163 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
3165 return VNET_API_ERROR_NO_SUCH_ENTRY;
3168 if (pool_is_free_index (tsm->sessions, ed_value_get_session_index (&value)))
3169 return VNET_API_ERROR_UNSPECIFIED;
3170 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3171 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
3172 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
3176 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
3177 vlib_node_runtime_t * node,
3178 vlib_frame_t * frame)
3183 VLIB_REGISTER_NODE (nat_default_node) = {
3184 .name = "nat-default",
3185 .vector_size = sizeof (u32),
3187 .type = VLIB_NODE_TYPE_INTERNAL,
3189 .n_next_nodes = NAT_N_NEXT,
3191 [NAT_NEXT_DROP] = "error-drop",
3192 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3193 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
3194 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3195 [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
3196 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3197 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
3198 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
3199 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
3200 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
3205 nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f)
3207 f->l3_csum_delta = 0;
3208 f->l4_csum_delta = 0;
3209 if (f->ops & NAT_FLOW_OP_SADDR_REWRITE &&
3210 f->rewrite.saddr.as_u32 != f->match.saddr.as_u32)
3213 ip_csum_add_even (f->l3_csum_delta, f->rewrite.saddr.as_u32);
3215 ip_csum_sub_even (f->l3_csum_delta, f->match.saddr.as_u32);
3219 f->rewrite.saddr.as_u32 = f->match.saddr.as_u32;
3221 if (f->ops & NAT_FLOW_OP_DADDR_REWRITE &&
3222 f->rewrite.daddr.as_u32 != f->match.daddr.as_u32)
3225 ip_csum_add_even (f->l3_csum_delta, f->rewrite.daddr.as_u32);
3227 ip_csum_sub_even (f->l3_csum_delta, f->match.daddr.as_u32);
3231 f->rewrite.daddr.as_u32 = f->match.daddr.as_u32;
3233 if (f->ops & NAT_FLOW_OP_SPORT_REWRITE && f->rewrite.sport != f->match.sport)
3235 f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.sport);
3236 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.sport);
3240 f->rewrite.sport = f->match.sport;
3242 if (f->ops & NAT_FLOW_OP_DPORT_REWRITE && f->rewrite.dport != f->match.dport)
3244 f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.dport);
3245 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.dport);
3249 f->rewrite.dport = f->match.dport;
3251 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE &&
3252 f->rewrite.icmp_id != f->match.sport)
3255 ip_csum_add_even (f->l4_csum_delta, f->rewrite.icmp_id);
3256 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.sport);
3260 f->rewrite.icmp_id = f->match.sport;
3262 if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3267 f->rewrite.fib_index = f->match.fib_index;
3271 static_always_inline int nat_6t_flow_icmp_translate (snat_main_t *sm,
3276 static_always_inline void
3277 nat_6t_flow_ip4_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
3278 nat_6t_flow_t *f, nat_protocol_t proto,
3279 int is_icmp_inner_ip4)
3281 udp_header_t *udp = ip4_next_header (ip);
3282 tcp_header_t *tcp = (tcp_header_t *) udp;
3284 if ((NAT_PROTOCOL_TCP == proto || NAT_PROTOCOL_UDP == proto) &&
3285 !vnet_buffer (b)->ip.reass.is_non_first_fragment)
3287 if (!is_icmp_inner_ip4)
3289 ip->src_address = f->rewrite.saddr;
3290 ip->dst_address = f->rewrite.daddr;
3291 udp->src_port = f->rewrite.sport;
3292 udp->dst_port = f->rewrite.dport;
3295 { // icmp inner ip4 - reversed saddr/daddr
3296 ip->src_address = f->rewrite.daddr;
3297 ip->dst_address = f->rewrite.saddr;
3298 udp->src_port = f->rewrite.dport;
3299 udp->dst_port = f->rewrite.sport;
3302 if (NAT_PROTOCOL_TCP == proto)
3304 ip_csum_t tcp_sum = tcp->checksum;
3305 tcp_sum = ip_csum_sub_even (tcp_sum, f->l3_csum_delta);
3306 tcp_sum = ip_csum_sub_even (tcp_sum, f->l4_csum_delta);
3307 mss_clamping (sm->mss_clamping, tcp, &tcp_sum);
3308 tcp->checksum = ip_csum_fold (tcp_sum);
3310 else if (proto == NAT_PROTOCOL_UDP && udp->checksum)
3312 ip_csum_t udp_sum = udp->checksum;
3313 udp_sum = ip_csum_sub_even (udp_sum, f->l3_csum_delta);
3314 udp_sum = ip_csum_sub_even (udp_sum, f->l4_csum_delta);
3315 udp->checksum = ip_csum_fold (udp_sum);
3320 if (!is_icmp_inner_ip4)
3322 ip->src_address = f->rewrite.saddr;
3323 ip->dst_address = f->rewrite.daddr;
3326 { // icmp inner ip4 - reversed saddr/daddr
3327 ip->src_address = f->rewrite.daddr;
3328 ip->dst_address = f->rewrite.saddr;
3332 ip_csum_t ip_sum = ip->checksum;
3333 ip_sum = ip_csum_sub_even (ip_sum, f->l3_csum_delta);
3334 ip->checksum = ip_csum_fold (ip_sum);
3335 if (0xffff == ip->checksum)
3337 ASSERT (ip4_header_checksum_is_valid (ip));
3340 static_always_inline int
3341 nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
3342 ip4_header_t *ip, nat_6t_flow_t *f)
3344 if (IP_PROTOCOL_ICMP != ip->protocol)
3345 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
3347 icmp46_header_t *icmp = ip4_next_header (ip);
3348 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3350 if ((!vnet_buffer (b)->ip.reass.is_non_first_fragment))
3352 if (icmp->checksum == 0)
3353 icmp->checksum = 0xffff;
3355 if (!icmp_type_is_error_message (icmp->type))
3357 if ((f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE) &&
3358 (f->rewrite.icmp_id != echo->identifier))
3360 ip_csum_t sum = icmp->checksum;
3361 sum = ip_csum_update (sum, echo->identifier, f->rewrite.icmp_id,
3363 identifier /* changed member */);
3364 echo->identifier = f->rewrite.icmp_id;
3365 icmp->checksum = ip_csum_fold (sum);
3370 // errors are not fragmented
3371 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3373 if (!ip4_header_checksum_is_valid (inner_ip))
3375 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
3378 nat_protocol_t inner_proto =
3379 ip_proto_to_nat_proto (inner_ip->protocol);
3381 ip_csum_t icmp_sum = icmp->checksum;
3383 switch (inner_proto)
3385 case NAT_PROTOCOL_UDP:
3386 case NAT_PROTOCOL_TCP:
3387 nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
3388 1 /* is_icmp_inner_ip4 */);
3389 icmp_sum = ip_csum_sub_even (icmp_sum, f->l3_csum_delta);
3390 icmp->checksum = ip_csum_fold (icmp_sum);
3392 case NAT_PROTOCOL_ICMP:
3393 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
3395 icmp46_header_t *inner_icmp = ip4_next_header (inner_ip);
3396 icmp_echo_header_t *inner_echo =
3397 (icmp_echo_header_t *) (inner_icmp + 1);
3398 if (f->rewrite.icmp_id != inner_echo->identifier)
3400 ip_csum_t sum = icmp->checksum;
3401 sum = ip_csum_update (
3402 sum, inner_echo->identifier, f->rewrite.icmp_id,
3403 icmp_echo_header_t, identifier /* changed member */);
3404 icmp->checksum = ip_csum_fold (sum);
3405 ip_csum_t inner_sum = inner_icmp->checksum;
3406 inner_sum = ip_csum_update (
3407 sum, inner_echo->identifier, f->rewrite.icmp_id,
3408 icmp_echo_header_t, identifier /* changed member */);
3409 inner_icmp->checksum = ip_csum_fold (inner_sum);
3410 inner_echo->identifier = f->rewrite.icmp_id;
3415 clib_warning ("unexpected NAT protocol value `%d'", inner_proto);
3416 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
3420 return NAT_ED_TRNSL_ERR_SUCCESS;
3423 nat_translation_error_e
3424 nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
3425 nat_6t_flow_t *f, nat_protocol_t proto,
3426 int is_output_feature)
3428 if (!is_output_feature && f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3430 vnet_buffer (b)->sw_if_index[VLIB_TX] = f->rewrite.fib_index;
3433 nat_6t_flow_ip4_translate (sm, b, ip, f, proto, 0 /* is_icmp_inner_ip4 */);
3435 if (NAT_PROTOCOL_ICMP == proto)
3437 return nat_6t_flow_icmp_translate (sm, b, ip, f);
3440 return NAT_ED_TRNSL_ERR_SUCCESS;
3444 format_nat_6t (u8 *s, va_list *args)
3446 nat_6t_t *t = va_arg (*args, nat_6t_t *);
3448 s = format (s, "saddr %U sport %u daddr %U dport %u proto %U fib_idx %u",
3449 format_ip4_address, t->saddr.as_u8,
3450 clib_net_to_host_u16 (t->sport), format_ip4_address,
3451 t->daddr.as_u8, clib_net_to_host_u16 (t->dport),
3452 format_ip_protocol, t->proto, t->fib_index);
3457 format_nat_ed_translation_error (u8 *s, va_list *args)
3459 nat_translation_error_e e = va_arg (*args, nat_translation_error_e);
3463 case NAT_ED_TRNSL_ERR_SUCCESS:
3464 s = format (s, "success");
3466 case NAT_ED_TRNSL_ERR_TRANSLATION_FAILED:
3467 s = format (s, "translation-failed");
3469 case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
3470 s = format (s, "flow-mismatch");
3477 format_nat_6t_flow (u8 *s, va_list *args)
3479 nat_6t_flow_t *f = va_arg (*args, nat_6t_flow_t *);
3481 s = format (s, "match: %U ", format_nat_6t, &f->match);
3483 if (f->ops & NAT_FLOW_OP_SADDR_REWRITE)
3485 s = format (s, "rewrite: saddr %U ", format_ip4_address,
3486 f->rewrite.saddr.as_u8);
3489 if (f->ops & NAT_FLOW_OP_SPORT_REWRITE)
3493 s = format (s, "rewrite: ");
3496 s = format (s, "sport %u ", clib_net_to_host_u16 (f->rewrite.sport));
3498 if (f->ops & NAT_FLOW_OP_DADDR_REWRITE)
3502 s = format (s, "rewrite: ");
3505 s = format (s, "daddr %U ", format_ip4_address, f->rewrite.daddr.as_u8);
3507 if (f->ops & NAT_FLOW_OP_DPORT_REWRITE)
3511 s = format (s, "rewrite: ");
3514 s = format (s, "dport %u ", clib_net_to_host_u16 (f->rewrite.dport));
3516 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
3520 s = format (s, "rewrite: ");
3523 s = format (s, "icmp-id %u ", clib_net_to_host_u16 (f->rewrite.icmp_id));
3525 if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3529 s = format (s, "rewrite: ");
3532 s = format (s, "txfib %u ", f->rewrite.fib_index);
3538 * fd.io coding-style-patch-verification: ON
3541 * eval: (c-set-style "gnu")