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/lib/ipfix_logging.h>
25 #include <nat/lib/nat_syslog.h>
26 #include <nat/nat_inlines.h>
27 #include <nat/nat44/inlines.h>
28 #include <nat/nat_affinity.h>
29 #include <vnet/fib/fib_table.h>
30 #include <vnet/fib/ip4_fib.h>
31 #include <vnet/ip/reass/ip4_sv_reass.h>
32 #include <vppinfra/bihash_16_8.h>
33 #include <nat/nat44/ed_inlines.h>
34 #include <vnet/ip/ip_table.h>
36 #include <nat/nat44-ei/nat44_ei_ha.h>
37 #include <nat/nat44-ei/nat44_ei.h>
39 #include <vpp/app/version.h>
40 #include <nat/lib/nat_inlines.h>
42 snat_main_t snat_main;
44 #define skip_if_disabled() \
47 snat_main_t *sm = &snat_main; \
48 if (PREDICT_FALSE (!sm->enabled)) \
53 #define fail_if_enabled() \
56 snat_main_t *sm = &snat_main; \
57 if (PREDICT_FALSE (sm->enabled)) \
59 nat_log_err ("plugin enabled"); \
65 #define fail_if_disabled() \
68 snat_main_t *sm = &snat_main; \
69 if (PREDICT_FALSE (!sm->enabled)) \
71 nat_log_err ("plugin disabled"); \
78 /* Hook up input features */
79 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
80 .arc_name = "ip4-unicast",
81 .node_name = "nat-pre-in2out",
82 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
83 "ip4-sv-reassembly-feature"),
85 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
86 .arc_name = "ip4-unicast",
87 .node_name = "nat-pre-out2in",
88 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
89 "ip4-dhcp-client-detect",
90 "ip4-sv-reassembly-feature"),
92 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
93 .arc_name = "ip4-unicast",
94 .node_name = "nat44-in2out-worker-handoff",
95 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
97 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
98 .arc_name = "ip4-unicast",
99 .node_name = "nat44-out2in-worker-handoff",
100 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
101 "ip4-dhcp-client-detect"),
103 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
104 .arc_name = "ip4-unicast",
105 .node_name = "nat44-in2out",
106 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
108 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
109 .arc_name = "ip4-unicast",
110 .node_name = "nat44-out2in",
111 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
112 "ip4-dhcp-client-detect"),
114 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
115 .arc_name = "ip4-unicast",
116 .node_name = "nat44-classify",
117 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
119 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
120 .arc_name = "ip4-unicast",
121 .node_name = "nat44-ed-in2out",
122 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
124 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
125 .arc_name = "ip4-unicast",
126 .node_name = "nat44-ed-out2in",
127 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
128 "ip4-dhcp-client-detect"),
130 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
131 .arc_name = "ip4-unicast",
132 .node_name = "nat44-ed-classify",
133 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
135 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
136 .arc_name = "ip4-unicast",
137 .node_name = "nat44-handoff-classify",
138 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
140 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
141 .arc_name = "ip4-unicast",
142 .node_name = "nat44-in2out-fast",
143 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
145 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
146 .arc_name = "ip4-unicast",
147 .node_name = "nat44-out2in-fast",
148 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
149 "ip4-dhcp-client-detect"),
151 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
152 .arc_name = "ip4-unicast",
153 .node_name = "nat44-hairpin-dst",
154 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
157 /* Hook up output features */
158 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
159 .arc_name = "ip4-output",
160 .node_name = "nat44-in2out-output",
161 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
163 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
164 .arc_name = "ip4-output",
165 .node_name = "nat44-in2out-output-worker-handoff",
166 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
168 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
169 .arc_name = "ip4-output",
170 .node_name = "nat44-hairpin-src",
171 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
173 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
174 .arc_name = "ip4-output",
175 .node_name = "nat-pre-in2out-output",
176 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
177 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
179 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
180 .arc_name = "ip4-output",
181 .node_name = "nat44-ed-in2out-output",
182 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
183 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
186 /* Hook up ip4-local features */
187 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
189 .arc_name = "ip4-local",
190 .node_name = "nat44-hairpinning",
191 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
194 VLIB_PLUGIN_REGISTER () = {
195 .version = VPP_BUILD_VER,
196 .description = "Network Address Translation (NAT)",
200 static void nat44_ed_db_init (u32 translations, u32 translation_buckets,
203 static void nat44_ed_db_free ();
206 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
207 u32 rx_fib_index, u8 is_output);
210 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
213 u32 nat_calc_bihash_buckets (u32 n_elts);
216 format_session_kvp (u8 * s, va_list * args)
218 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
220 s = format (s, "%U session-index %llu", format_snat_key, v->key, v->value);
226 format_static_mapping_kvp (u8 * s, va_list * args)
228 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
230 s = format (s, "%U static-mapping-index %llu",
231 format_snat_key, v->key, v->value);
237 format_user_kvp (u8 * s, va_list * args)
239 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
244 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
245 k.fib_index, v->value);
251 format_ed_session_kvp (u8 * s, va_list * args)
253 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
257 ip4_address_t l_addr, r_addr;
260 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
262 "local %U:%d remote %U:%d proto %U fib %d thread-index %u "
264 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
265 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
266 format_ip_protocol, proto, fib_index,
267 ed_value_get_thread_index (v), ed_value_get_session_index (v));
273 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
276 clib_bihash_kv_8_8_t kv;
277 snat_main_per_thread_data_t *tsm =
278 vec_elt_at_index (sm->per_thread_data, thread_index);
280 if (is_ed_session (s))
282 per_vrf_sessions_unregister_session (s, thread_index);
284 if (nat_ed_ses_i2o_flow_hash_add_del (sm, thread_index, s, 0))
285 nat_elog_warn ("flow hash del failed");
287 if (nat_ed_ses_o2i_flow_hash_add_del (sm, thread_index, s, 0))
288 nat_elog_warn ("flow hash del failed");
291 if (is_fwd_bypass_session (s))
296 /* session lookup tables */
297 if (is_ed_session (s))
299 if (is_affinity_sessions (s))
300 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
301 s->nat_proto, s->out2in.port);
304 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
305 &s->in2out.addr, s->in2out.port,
306 &s->ext_host_nat_addr, s->ext_host_nat_port,
307 &s->out2in.addr, s->out2in.port,
308 &s->ext_host_addr, s->ext_host_port,
309 s->nat_proto, is_twice_nat_session (s));
313 init_nat_i2o_k (&kv, s);
314 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
315 nat_elog_warn ("in2out key del failed");
316 init_nat_o2i_k (&kv, s);
317 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
318 nat_elog_warn ("out2in key del failed");
321 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
322 &s->in2out.addr, s->in2out.port,
323 &s->out2in.addr, s->out2in.port,
327 if (snat_is_unk_proto_session (s))
333 nat_ipfix_logging_nat44_ses_delete (thread_index,
334 s->in2out.addr.as_u32,
335 s->out2in.addr.as_u32,
339 s->in2out.fib_index);
341 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
342 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
346 /* Twice NAT address and port for external host */
347 if (is_twice_nat_session (s))
349 snat_free_outside_address_and_port (sm->twice_nat_addresses,
351 &s->ext_host_nat_addr,
352 s->ext_host_nat_port, s->nat_proto);
355 if (snat_is_session_static (s))
358 snat_free_outside_address_and_port (sm->addresses, thread_index,
359 &s->out2in.addr, s->out2in.port,
364 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
368 snat_user_key_t user_key;
369 clib_bihash_kv_8_8_t kv, value;
370 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
371 dlist_elt_t *per_user_list_head_elt;
373 user_key.addr.as_u32 = addr->as_u32;
374 user_key.fib_index = fib_index;
375 kv.key = user_key.as_u64;
377 /* Ever heard of the "user" = src ip4 address before? */
378 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
380 if (pool_elts (tsm->users) >= sm->max_users_per_thread)
382 vlib_increment_simple_counter (&sm->user_limit_reached,
384 nat_elog_warn ("maximum user limit reached");
387 /* no, make a new one */
388 pool_get (tsm->users, u);
389 clib_memset (u, 0, sizeof (*u));
391 u->addr.as_u32 = addr->as_u32;
392 u->fib_index = fib_index;
394 pool_get (tsm->list_pool, per_user_list_head_elt);
396 u->sessions_per_user_list_head_index = per_user_list_head_elt -
399 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
401 kv.value = u - tsm->users;
404 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
406 nat_elog_warn ("user_hash key add failed");
407 nat44_delete_user_with_no_session (sm, u, thread_index);
411 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
412 pool_elts (tsm->users));
416 u = pool_elt_at_index (tsm->users, value.value);
424 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
425 u32 thread_index, f64 now)
428 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
429 u32 oldest_per_user_translation_list_index, session_index;
430 dlist_elt_t *oldest_per_user_translation_list_elt;
431 dlist_elt_t *per_user_translation_list_elt;
433 /* Over quota? Recycle the least recently used translation */
434 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
436 oldest_per_user_translation_list_index =
437 clib_dlist_remove_head (tsm->list_pool,
438 u->sessions_per_user_list_head_index);
440 ASSERT (oldest_per_user_translation_list_index != ~0);
442 /* Add it back to the end of the LRU list */
443 clib_dlist_addtail (tsm->list_pool,
444 u->sessions_per_user_list_head_index,
445 oldest_per_user_translation_list_index);
446 /* Get the list element */
447 oldest_per_user_translation_list_elt =
448 pool_elt_at_index (tsm->list_pool,
449 oldest_per_user_translation_list_index);
451 /* Get the session index from the list element */
452 session_index = oldest_per_user_translation_list_elt->value;
454 /* Get the session */
455 s = pool_elt_at_index (tsm->sessions, session_index);
457 // TODO: ONLY EI version should be called
458 nat_free_session_data (sm, s, thread_index, 0);
459 if (snat_is_session_static (s))
460 u->nstaticsessions--;
467 s->ext_host_addr.as_u32 = 0;
468 s->ext_host_port = 0;
469 s->ext_host_nat_addr.as_u32 = 0;
470 s->ext_host_nat_port = 0;
474 pool_get (tsm->sessions, s);
475 clib_memset (s, 0, sizeof (*s));
477 /* Create list elts */
478 pool_get (tsm->list_pool, per_user_translation_list_elt);
479 clib_dlist_init (tsm->list_pool,
480 per_user_translation_list_elt - tsm->list_pool);
482 per_user_translation_list_elt->value = s - tsm->sessions;
483 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
484 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
486 clib_dlist_addtail (tsm->list_pool,
487 s->per_user_list_head_index,
488 per_user_translation_list_elt - tsm->list_pool);
490 s->user_index = u - tsm->users;
491 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
492 pool_elts (tsm->sessions));
495 s->ha_last_refreshed = now;
501 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
504 snat_main_t *sm = &snat_main;
505 fib_prefix_t prefix = {
507 .fp_proto = FIB_PROTOCOL_IP4,
509 .ip4.as_u32 = addr->as_u32,
512 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
515 fib_table_entry_update_one_path (fib_index,
518 (FIB_ENTRY_FLAG_CONNECTED |
519 FIB_ENTRY_FLAG_LOCAL |
520 FIB_ENTRY_FLAG_EXCLUSIVE),
524 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
526 fib_table_entry_delete (fib_index, &prefix, sm->fib_src_low);
530 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
535 vlib_thread_main_t *tm = vlib_get_thread_main ();
537 if (twice_nat && !sm->endpoint_dependent)
539 nat_log_err ("unsupported");
540 return VNET_API_ERROR_UNSUPPORTED;
543 /* Check if address already exists */
545 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
547 if (ap->addr.as_u32 == addr->as_u32)
549 nat_log_err ("address exist");
550 return VNET_API_ERROR_VALUE_EXIST;
556 vec_add2 (sm->twice_nat_addresses, ap, 1);
558 vec_add2 (sm->addresses, ap, 1);
563 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
569 #define _(N, i, n, s) \
570 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
571 ap->busy_##n##_ports = 0; \
572 ap->busy_##n##_ports_per_thread = 0;\
573 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
581 /* Add external address to FIB */
583 pool_foreach (i, sm->interfaces)
585 if (nat_interface_is_inside(i) || sm->out2in_dpo)
588 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
591 pool_foreach (i, sm->output_feature_interfaces)
593 if (nat_interface_is_inside(i) || sm->out2in_dpo)
596 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
605 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
607 snat_static_mapping_t *m;
609 pool_foreach (m, sm->static_mappings)
611 if (is_addr_only_static_mapping (m) ||
612 is_out2in_only_static_mapping (m) ||
613 is_identity_static_mapping (m))
615 if (m->external_addr.as_u32 == addr.as_u32)
624 snat_add_static_mapping_when_resolved (snat_main_t *sm, ip4_address_t l_addr,
625 u16 l_port, u32 sw_if_index, u16 e_port,
626 u32 vrf_id, nat_protocol_t proto,
627 int addr_only, u8 *tag, int twice_nat,
628 int out2in_only, int identity_nat,
629 ip4_address_t pool_addr, int exact)
631 snat_static_map_resolve_t *rp;
633 vec_add2 (sm->to_resolve, rp, 1);
634 rp->l_addr.as_u32 = l_addr.as_u32;
636 rp->sw_if_index = sw_if_index;
640 rp->addr_only = addr_only;
641 rp->twice_nat = twice_nat;
642 rp->out2in_only = out2in_only;
643 rp->identity_nat = identity_nat;
644 rp->tag = vec_dup (tag);
645 rp->pool_addr = pool_addr;
650 get_thread_idx_by_port (u16 e_port)
652 snat_main_t *sm = &snat_main;
653 u32 thread_idx = sm->num_workers;
654 if (sm->num_workers > 1)
657 sm->first_worker_index +
658 sm->workers[(e_port - 1024) / sm->port_per_thread];
664 nat_ed_static_mapping_del_sessions (snat_main_t * sm,
665 snat_main_per_thread_data_t * tsm,
666 ip4_address_t l_addr,
669 u32 fib_index, int addr_only,
670 ip4_address_t e_addr, u16 e_port)
673 u32 *indexes_to_free = NULL;
675 pool_foreach (s, tsm->sessions) {
676 if (s->in2out.fib_index != fib_index ||
677 s->in2out.addr.as_u32 != l_addr.as_u32)
683 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
684 s->out2in.port != e_port ||
685 s->in2out.port != l_port ||
686 s->nat_proto != protocol)
690 if (is_lb_session (s))
692 if (!snat_is_session_static (s))
694 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
695 vec_add1 (indexes_to_free, s - tsm->sessions);
701 vec_foreach (ses_index, indexes_to_free)
703 s = pool_elt_at_index (tsm->sessions, *ses_index);
704 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
706 vec_free (indexes_to_free);
709 static_always_inline int
710 nat44_ed_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
711 u16 l_port, u16 e_port, u32 vrf_id,
712 int addr_only, u32 sw_if_index,
713 nat_protocol_t proto, int is_add,
714 twice_nat_type_t twice_nat, u8 out2in_only,
715 u8 *tag, u8 identity_nat,
716 ip4_address_t pool_addr, int exact)
718 snat_main_t *sm = &snat_main;
719 snat_static_mapping_t *m;
720 clib_bihash_kv_8_8_t kv, value;
721 snat_address_t *a = 0;
723 snat_interface_t *interface;
725 snat_main_per_thread_data_t *tsm;
726 snat_user_key_t u_key;
728 dlist_elt_t *head, *elt;
729 u32 elt_index, head_index;
733 snat_static_map_resolve_t *rp, *rp_match = 0;
734 nat44_lb_addr_port_t *local;
737 if (!sm->endpoint_dependent)
739 if (twice_nat || out2in_only)
740 return VNET_API_ERROR_UNSUPPORTED;
743 /* If the external address is a specific interface address */
744 if (sw_if_index != ~0)
746 ip4_address_t *first_int_addr;
748 for (i = 0; i < vec_len (sm->to_resolve); i++)
750 rp = sm->to_resolve + i;
751 if (rp->sw_if_index != sw_if_index ||
752 rp->l_addr.as_u32 != l_addr.as_u32 ||
753 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
758 if ((rp->l_port != l_port && rp->e_port != e_port)
759 || rp->proto != proto)
767 /* Might be already set... */
768 first_int_addr = ip4_interface_first_address
769 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
774 return VNET_API_ERROR_VALUE_EXIST;
776 snat_add_static_mapping_when_resolved (
777 sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto, addr_only,
778 tag, twice_nat, out2in_only, identity_nat, pool_addr, exact);
780 /* DHCP resolution required? */
781 if (first_int_addr == 0)
787 e_addr.as_u32 = first_int_addr->as_u32;
788 /* Identity mapping? */
789 if (l_addr.as_u32 == 0)
790 l_addr.as_u32 = e_addr.as_u32;
796 return VNET_API_ERROR_NO_SUCH_ENTRY;
798 vec_del1 (sm->to_resolve, i);
802 e_addr.as_u32 = first_int_addr->as_u32;
803 /* Identity mapping? */
804 if (l_addr.as_u32 == 0)
805 l_addr.as_u32 = e_addr.as_u32;
812 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
813 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
816 m = pool_elt_at_index (sm->static_mappings, value.value);
822 if (is_identity_static_mapping (m))
825 pool_foreach (local, m->locals)
827 if (local->vrf_id == vrf_id)
828 return VNET_API_ERROR_VALUE_EXIST;
831 pool_get (m->locals, local);
832 local->vrf_id = vrf_id;
834 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
836 init_nat_kv (&kv, m->local_addr, m->local_port,
837 local->fib_index, m->proto,
838 m - sm->static_mappings);
839 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
843 return VNET_API_ERROR_VALUE_EXIST;
846 if (twice_nat && addr_only)
847 return VNET_API_ERROR_UNSUPPORTED;
849 /* Convert VRF id to FIB index */
852 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
854 /* If not specified use inside VRF id from SNAT plugin startup config */
857 fib_index = sm->inside_fib_index;
858 vrf_id = sm->inside_vrf_id;
859 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
862 if (!(out2in_only || identity_nat))
864 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
865 addr_only ? 0 : proto);
866 if (!clib_bihash_search_8_8
867 (&sm->static_mapping_by_local, &kv, &value))
868 return VNET_API_ERROR_VALUE_EXIST;
871 /* Find external address in allocated addresses and reserve port for
872 address and port pair mapping when dynamic translations enabled */
873 if (!(addr_only || sm->static_mapping_only || out2in_only))
875 for (i = 0; i < vec_len (sm->addresses); i++)
877 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
879 a = sm->addresses + i;
880 /* External port must be unused */
883 #define _(N, j, n, s) \
884 case NAT_PROTOCOL_##N: \
885 if (a->busy_##n##_port_refcounts[e_port]) \
886 return VNET_API_ERROR_INVALID_VALUE; \
887 ++a->busy_##n##_port_refcounts[e_port]; \
890 a->busy_##n##_ports++; \
891 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
897 nat_elog_info ("unknown protocol");
898 return VNET_API_ERROR_INVALID_VALUE_2;
903 /* External address must be allocated */
904 if (!a && (l_addr.as_u32 != e_addr.as_u32))
906 if (sw_if_index != ~0)
908 for (i = 0; i < vec_len (sm->to_resolve); i++)
910 rp = sm->to_resolve + i;
913 if (rp->sw_if_index != sw_if_index &&
914 rp->l_addr.as_u32 != l_addr.as_u32 &&
915 rp->vrf_id != vrf_id && rp->l_port != l_port &&
916 rp->e_port != e_port && rp->proto != proto)
919 vec_del1 (sm->to_resolve, i);
923 return VNET_API_ERROR_NO_SUCH_ENTRY;
927 pool_get (sm->static_mappings, m);
928 clib_memset (m, 0, sizeof (*m));
929 m->tag = vec_dup (tag);
930 m->local_addr = l_addr;
931 m->external_addr = e_addr;
932 m->twice_nat = twice_nat;
934 if (twice_nat == TWICE_NAT && exact)
936 m->flags |= NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS;
937 m->pool_addr = pool_addr;
941 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
943 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
946 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
947 pool_get (m->locals, local);
948 local->vrf_id = vrf_id;
949 local->fib_index = fib_index;
954 m->fib_index = fib_index;
958 m->local_port = l_port;
959 m->external_port = e_port;
963 if (sm->num_workers > 1)
966 .src_address = m->local_addr,
968 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
969 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
972 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
976 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
977 m - sm->static_mappings);
978 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
981 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
982 m - sm->static_mappings);
983 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
985 /* Delete dynamic sessions matching local address (+ local port) */
986 // TODO: based on type of NAT EI/ED
987 if (!(sm->static_mapping_only))
989 u_key.addr = m->local_addr;
990 u_key.fib_index = m->fib_index;
991 kv.key = u_key.as_u64;
992 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
994 user_index = value.value;
995 u = pool_elt_at_index (tsm->users, user_index);
998 head_index = u->sessions_per_user_list_head_index;
999 head = pool_elt_at_index (tsm->list_pool, head_index);
1000 elt_index = head->next;
1001 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1002 ses_index = elt->value;
1003 while (ses_index != ~0)
1005 s = pool_elt_at_index (tsm->sessions, ses_index);
1006 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1007 ses_index = elt->value;
1009 if (snat_is_session_static (s))
1012 if (!addr_only && s->in2out.port != m->local_port)
1015 nat_free_session_data (sm, s,
1016 tsm - sm->per_thread_data, 0);
1017 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1019 if (!addr_only && !sm->endpoint_dependent)
1030 if (sw_if_index != ~0)
1033 return VNET_API_ERROR_NO_SUCH_ENTRY;
1039 vrf_id = sm->inside_vrf_id;
1042 pool_foreach (local, m->locals)
1044 if (local->vrf_id == vrf_id)
1045 find = local - m->locals;
1049 return VNET_API_ERROR_NO_SUCH_ENTRY;
1051 local = pool_elt_at_index (m->locals, find);
1052 fib_index = local->fib_index;
1053 pool_put (m->locals, local);
1056 fib_index = m->fib_index;
1058 /* Free external address port */
1059 if (!(addr_only || sm->static_mapping_only || out2in_only))
1061 for (i = 0; i < vec_len (sm->addresses); i++)
1063 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1065 a = sm->addresses + i;
1068 #define _(N, j, n, s) \
1069 case NAT_PROTOCOL_##N: \
1070 --a->busy_##n##_port_refcounts[e_port]; \
1071 if (e_port > 1024) \
1073 a->busy_##n##_ports--; \
1074 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1077 foreach_nat_protocol
1080 nat_elog_info ("unknown protocol");
1081 return VNET_API_ERROR_INVALID_VALUE_2;
1088 if (sm->num_workers > 1)
1089 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1091 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1093 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
1095 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1097 /* Delete session(s) for static mapping if exist */
1098 if (!(sm->static_mapping_only) ||
1099 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1101 if (sm->endpoint_dependent)
1103 nat_ed_static_mapping_del_sessions (sm, tsm, m->local_addr,
1104 m->local_port, m->proto,
1105 fib_index, addr_only,
1110 u_key.addr = m->local_addr;
1111 u_key.fib_index = fib_index;
1112 kv.key = u_key.as_u64;
1113 nat44_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
1118 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1119 if (pool_elts (m->locals))
1122 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1123 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1126 vec_free (m->workers);
1127 /* Delete static mapping from pool */
1128 pool_put (sm->static_mappings, m);
1131 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1134 /* Add/delete external address to FIB */
1136 pool_foreach (interface, sm->interfaces)
1138 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1141 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1144 pool_foreach (interface, sm->output_feature_interfaces)
1146 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1149 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1158 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1159 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
1160 u32 sw_if_index, nat_protocol_t proto, int is_add,
1161 twice_nat_type_t twice_nat, u8 out2in_only, u8 *tag,
1162 u8 identity_nat, ip4_address_t pool_addr, int exact)
1164 snat_main_t *sm = &snat_main;
1167 if (sm->endpoint_dependent)
1169 rv = nat44_ed_add_del_static_mapping (
1170 l_addr, e_addr, l_port, e_port, vrf_id, addr_only, sw_if_index, proto,
1171 is_add, twice_nat, out2in_only, tag, identity_nat, pool_addr, exact);
1175 rv = nat44_ei_add_del_static_mapping (
1176 l_addr, e_addr, l_port, e_port, proto, sw_if_index, vrf_id, addr_only,
1177 identity_nat, tag, is_add);
1183 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1184 nat_protocol_t proto,
1185 nat44_lb_addr_port_t * locals, u8 is_add,
1186 twice_nat_type_t twice_nat, u8 out2in_only,
1187 u8 * tag, u32 affinity)
1189 snat_main_t *sm = &snat_main;
1190 snat_static_mapping_t *m;
1191 clib_bihash_kv_8_8_t kv, value;
1192 snat_address_t *a = 0;
1194 nat44_lb_addr_port_t *local;
1195 snat_main_per_thread_data_t *tsm;
1199 if (!sm->endpoint_dependent)
1200 return VNET_API_ERROR_UNSUPPORTED;
1202 init_nat_k (&kv, e_addr, e_port, 0, proto);
1203 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1206 m = pool_elt_at_index (sm->static_mappings, value.value);
1211 return VNET_API_ERROR_VALUE_EXIST;
1213 if (vec_len (locals) < 2)
1214 return VNET_API_ERROR_INVALID_VALUE;
1216 /* Find external address in allocated addresses and reserve port for
1217 address and port pair mapping when dynamic translations enabled */
1218 if (!(sm->static_mapping_only || out2in_only))
1220 for (i = 0; i < vec_len (sm->addresses); i++)
1222 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1224 a = sm->addresses + i;
1225 /* External port must be unused */
1228 #define _(N, j, n, s) \
1229 case NAT_PROTOCOL_##N: \
1230 if (a->busy_##n##_port_refcounts[e_port]) \
1231 return VNET_API_ERROR_INVALID_VALUE; \
1232 ++a->busy_##n##_port_refcounts[e_port]; \
1233 if (e_port > 1024) \
1235 a->busy_##n##_ports++; \
1236 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1239 foreach_nat_protocol
1242 nat_elog_info ("unknown protocol");
1243 return VNET_API_ERROR_INVALID_VALUE_2;
1248 /* External address must be allocated */
1250 return VNET_API_ERROR_NO_SUCH_ENTRY;
1253 pool_get (sm->static_mappings, m);
1254 clib_memset (m, 0, sizeof (*m));
1255 m->tag = vec_dup (tag);
1256 m->external_addr = e_addr;
1257 m->external_port = e_port;
1259 m->twice_nat = twice_nat;
1260 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1262 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1263 m->affinity = affinity;
1266 m->affinity_per_service_list_head_index =
1267 nat_affinity_get_per_service_list_head_index ();
1269 m->affinity_per_service_list_head_index = ~0;
1271 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1272 m - sm->static_mappings);
1273 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1275 nat_elog_err ("static_mapping_by_external key add failed");
1276 return VNET_API_ERROR_UNSPECIFIED;
1279 for (i = 0; i < vec_len (locals); i++)
1281 locals[i].fib_index =
1282 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1287 init_nat_kv (&kv, locals[i].addr, locals[i].port,
1288 locals[i].fib_index, m->proto,
1289 m - sm->static_mappings);
1290 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1292 locals[i].prefix = (i == 0) ? locals[i].probability :
1293 (locals[i - 1].prefix + locals[i].probability);
1294 pool_get (m->locals, local);
1296 if (sm->num_workers > 1)
1299 .src_address = locals[i].addr,
1302 clib_bitmap_set (bitmap,
1303 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1308 /* Assign workers */
1309 if (sm->num_workers > 1)
1312 clib_bitmap_foreach (i, bitmap)
1314 vec_add1(m->workers, i);
1322 return VNET_API_ERROR_NO_SUCH_ENTRY;
1324 if (!is_lb_static_mapping (m))
1325 return VNET_API_ERROR_INVALID_VALUE;
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 NAT_PROTOCOL_##N: \
1339 --a->busy_##n##_port_refcounts[e_port]; \
1340 if (e_port > 1024) \
1342 a->busy_##n##_ports--; \
1343 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1346 foreach_nat_protocol
1349 nat_elog_info ("unknown protocol");
1350 return VNET_API_ERROR_INVALID_VALUE_2;
1357 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1358 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1360 nat_elog_err ("static_mapping_by_external key del failed");
1361 return VNET_API_ERROR_UNSPECIFIED;
1365 pool_foreach (local, m->locals)
1367 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1371 init_nat_k(& kv, local->addr, local->port, local->fib_index, m->proto);
1372 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1374 nat_elog_err ("static_mapping_by_local key del failed");
1375 return VNET_API_ERROR_UNSPECIFIED;
1379 if (sm->num_workers > 1)
1382 .src_address = local->addr,
1384 tsm = vec_elt_at_index (sm->per_thread_data,
1385 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1388 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1390 /* Delete sessions */
1391 pool_foreach (s, tsm->sessions) {
1392 if (!(is_lb_session (s)))
1395 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1396 s->in2out.port != local->port)
1399 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1400 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1405 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1406 pool_free (m->locals);
1408 vec_free (m->workers);
1410 pool_put (sm->static_mappings, m);
1417 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1418 ip4_address_t l_addr, u16 l_port,
1419 nat_protocol_t proto, u32 vrf_id,
1420 u8 probability, u8 is_add)
1422 snat_main_t *sm = &snat_main;
1423 snat_static_mapping_t *m = 0;
1424 clib_bihash_kv_8_8_t kv, value;
1425 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1426 snat_main_per_thread_data_t *tsm;
1432 if (!sm->endpoint_dependent)
1433 return VNET_API_ERROR_FEATURE_DISABLED;
1435 init_nat_k (&kv, e_addr, e_port, 0, proto);
1436 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1437 m = pool_elt_at_index (sm->static_mappings, value.value);
1440 return VNET_API_ERROR_NO_SUCH_ENTRY;
1442 if (!is_lb_static_mapping (m))
1443 return VNET_API_ERROR_INVALID_VALUE;
1446 pool_foreach (local, m->locals)
1448 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1449 (local->vrf_id == vrf_id))
1451 match_local = local;
1460 return VNET_API_ERROR_VALUE_EXIST;
1462 pool_get (m->locals, local);
1463 clib_memset (local, 0, sizeof (*local));
1464 local->addr.as_u32 = l_addr.as_u32;
1465 local->port = l_port;
1466 local->probability = probability;
1467 local->vrf_id = vrf_id;
1469 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1472 if (!is_out2in_only_static_mapping (m))
1474 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto,
1475 m - sm->static_mappings);
1476 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1477 nat_elog_err ("static_mapping_by_local key add failed");
1483 return VNET_API_ERROR_NO_SUCH_ENTRY;
1485 if (pool_elts (m->locals) < 3)
1486 return VNET_API_ERROR_UNSPECIFIED;
1488 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1491 if (!is_out2in_only_static_mapping (m))
1493 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1494 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1495 nat_elog_err ("static_mapping_by_local key del failed");
1498 if (sm->num_workers > 1)
1501 .src_address = local->addr,
1503 tsm = vec_elt_at_index (sm->per_thread_data,
1504 sm->worker_in2out_cb (&ip, m->fib_index,
1508 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1510 /* Delete sessions */
1512 pool_foreach (s, tsm->sessions) {
1513 if (!(is_lb_session (s)))
1516 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1517 s->in2out.port != match_local->port)
1520 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1521 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1525 pool_put (m->locals, match_local);
1528 vec_free (m->workers);
1531 pool_foreach (local, m->locals)
1533 vec_add1 (locals, local - m->locals);
1534 if (sm->num_workers > 1)
1537 ip.src_address.as_u32 = local->addr.as_u32,
1538 bitmap = clib_bitmap_set (bitmap,
1539 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1545 ASSERT (vec_len (locals) > 1);
1547 local = pool_elt_at_index (m->locals, locals[0]);
1548 local->prefix = local->probability;
1549 for (i = 1; i < vec_len (locals); i++)
1551 local = pool_elt_at_index (m->locals, locals[i]);
1552 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1553 local->prefix = local->probability + prev_local->prefix;
1556 /* Assign workers */
1557 if (sm->num_workers > 1)
1560 clib_bitmap_foreach (i, bitmap) { vec_add1(m->workers, i); }
1568 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1571 snat_address_t *a = 0;
1572 snat_session_t *ses;
1573 u32 *ses_to_be_removed = 0, *ses_index;
1574 snat_main_per_thread_data_t *tsm;
1575 snat_static_mapping_t *m;
1576 snat_interface_t *interface;
1578 snat_address_t *addresses =
1579 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1581 /* Find SNAT address */
1582 for (i = 0; i < vec_len (addresses); i++)
1584 if (addresses[i].addr.as_u32 == addr.as_u32)
1592 nat_log_err ("no such address");
1593 return VNET_API_ERROR_NO_SUCH_ENTRY;
1598 ip4_address_t pool_addr = { 0 };
1600 pool_foreach (m, sm->static_mappings)
1602 if (m->external_addr.as_u32 == addr.as_u32)
1603 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1604 m->local_port, m->external_port,
1606 is_addr_only_static_mapping(m), ~0,
1607 m->proto, 0 /* is_add */,
1609 is_out2in_only_static_mapping(m),
1611 is_identity_static_mapping(m),
1618 /* Check if address is used in some static mapping */
1619 if (is_snat_address_used_in_static_mapping (sm, addr))
1621 nat_log_err ("address used in static mapping");
1622 return VNET_API_ERROR_UNSPECIFIED;
1626 if (a->fib_index != ~0)
1627 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1629 /* Delete sessions using address */
1630 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1632 vec_foreach (tsm, sm->per_thread_data)
1635 pool_foreach (ses, tsm->sessions) {
1636 if (ses->out2in.addr.as_u32 == addr.as_u32)
1638 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1639 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1644 if (sm->endpoint_dependent)
1646 vec_foreach (ses_index, ses_to_be_removed)
1648 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1649 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1654 vec_foreach (ses_index, ses_to_be_removed)
1656 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1657 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1661 vec_free (ses_to_be_removed);
1665 #define _(N, i, n, s) \
1666 vec_free (a->busy_##n##_ports_per_thread);
1667 foreach_nat_protocol
1671 vec_del1 (sm->twice_nat_addresses, i);
1675 vec_del1 (sm->addresses, i);
1677 /* Delete external address from FIB */
1679 pool_foreach (interface, sm->interfaces)
1681 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1684 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1687 pool_foreach (interface, sm->output_feature_interfaces)
1689 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1692 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1701 nat_validate_counters (snat_main_t * sm, u32 sw_if_index)
1704 vlib_validate_simple_counter (&sm->counters.fastpath.in2out.x, \
1706 vlib_zero_simple_counter (&sm->counters.fastpath.in2out.x, sw_if_index); \
1707 vlib_validate_simple_counter (&sm->counters.fastpath.out2in.x, \
1709 vlib_zero_simple_counter (&sm->counters.fastpath.out2in.x, sw_if_index); \
1710 vlib_validate_simple_counter (&sm->counters.slowpath.in2out.x, \
1712 vlib_zero_simple_counter (&sm->counters.slowpath.in2out.x, sw_if_index); \
1713 vlib_validate_simple_counter (&sm->counters.slowpath.out2in.x, \
1715 vlib_zero_simple_counter (&sm->counters.slowpath.out2in.x, sw_if_index); \
1716 vlib_validate_simple_counter (&sm->counters.fastpath.in2out_ed.x, \
1718 vlib_zero_simple_counter (&sm->counters.fastpath.in2out_ed.x, sw_if_index); \
1719 vlib_validate_simple_counter (&sm->counters.fastpath.out2in_ed.x, \
1721 vlib_zero_simple_counter (&sm->counters.fastpath.out2in_ed.x, sw_if_index); \
1722 vlib_validate_simple_counter (&sm->counters.slowpath.in2out_ed.x, \
1724 vlib_zero_simple_counter (&sm->counters.slowpath.in2out_ed.x, sw_if_index); \
1725 vlib_validate_simple_counter (&sm->counters.slowpath.out2in_ed.x, \
1727 vlib_zero_simple_counter (&sm->counters.slowpath.out2in_ed.x, sw_if_index);
1728 foreach_nat_counter;
1730 vlib_validate_simple_counter (&sm->counters.hairpinning, sw_if_index);
1731 vlib_zero_simple_counter (&sm->counters.hairpinning, sw_if_index);
1735 expire_per_vrf_sessions (u32 fib_index)
1737 per_vrf_sessions_t *per_vrf_sessions;
1738 snat_main_per_thread_data_t *tsm;
1739 snat_main_t *sm = &snat_main;
1742 vec_foreach (tsm, sm->per_thread_data)
1744 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1746 if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1747 (per_vrf_sessions->tx_fib_index == fib_index))
1749 per_vrf_sessions->expired = 1;
1757 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1759 snat_main_t *sm = &snat_main;
1762 // we don't care if it is outside/inside fib
1763 // we just care about their ref_count
1764 // if it reaches 0 sessions should expire
1765 // because the fib isn't valid for NAT anymore
1767 vec_foreach (fib, sm->fibs)
1769 if (fib->fib_index == fib_index)
1774 if (!fib->ref_count)
1776 vec_del1 (sm->fibs, fib - sm->fibs);
1777 expire_per_vrf_sessions (fib_index);
1787 vec_add2 (sm->fibs, fib, 1);
1789 fib->fib_index = fib_index;
1794 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1796 snat_main_t *sm = &snat_main;
1797 snat_interface_t *i;
1798 const char *feature_name, *del_feature_name;
1800 snat_static_mapping_t *m;
1801 nat_outside_fib_t *outside_fib;
1802 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1807 nat_log_err ("nat44 is disabled");
1808 return VNET_API_ERROR_UNSUPPORTED;
1811 if (sm->out2in_dpo && !is_inside)
1813 nat_log_err ("error unsupported");
1814 return VNET_API_ERROR_UNSUPPORTED;
1818 pool_foreach (i, sm->output_feature_interfaces)
1820 if (i->sw_if_index == sw_if_index)
1822 nat_log_err ("error interface already configured");
1823 return VNET_API_ERROR_VALUE_EXIST;
1828 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1829 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1832 if (sm->num_workers > 1)
1834 is_inside ? "nat44-in2out-worker-handoff" :
1835 "nat44-out2in-worker-handoff";
1836 else if (sm->endpoint_dependent)
1838 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1841 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1844 if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
1845 sm->fq_in2out_index =
1846 vlib_frame_queue_main_init (sm->in2out_node_index, NAT_FQ_NELTS);
1848 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1849 sm->fq_out2in_index =
1850 vlib_frame_queue_main_init (sm->out2in_node_index, NAT_FQ_NELTS);
1852 if (sm->endpoint_dependent)
1853 update_per_vrf_sessions_vec (fib_index, is_del);
1858 vec_foreach (outside_fib, sm->outside_fibs)
1860 if (outside_fib->fib_index == fib_index)
1864 outside_fib->refcount--;
1865 if (!outside_fib->refcount)
1866 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1869 outside_fib->refcount++;
1876 vec_add2 (sm->outside_fibs, outside_fib, 1);
1877 outside_fib->refcount = 1;
1878 outside_fib->fib_index = fib_index;
1884 pool_foreach (i, sm->interfaces)
1886 if (i->sw_if_index == sw_if_index)
1890 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1893 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1895 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1897 if (sm->num_workers > 1)
1899 del_feature_name = "nat44-handoff-classify";
1900 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1901 "nat44-out2in-worker-handoff";
1903 else if (sm->endpoint_dependent)
1905 del_feature_name = "nat44-ed-classify";
1906 feature_name = !is_inside ? "nat-pre-in2out" :
1911 del_feature_name = "nat44-classify";
1912 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1915 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1918 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1919 sw_if_index, 0, 0, 0);
1920 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1921 sw_if_index, 1, 0, 0);
1924 if (!sm->endpoint_dependent)
1925 vnet_feature_enable_disable ("ip4-local",
1926 "nat44-hairpinning",
1927 sw_if_index, 1, 0, 0);
1932 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1935 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1936 sw_if_index, 0, 0, 0);
1937 pool_put (sm->interfaces, i);
1940 if (!sm->endpoint_dependent)
1941 vnet_feature_enable_disable ("ip4-local",
1942 "nat44-hairpinning",
1943 sw_if_index, 0, 0, 0);
1949 if ((nat_interface_is_inside (i) && is_inside) ||
1950 (nat_interface_is_outside (i) && !is_inside))
1953 if (sm->num_workers > 1)
1955 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1956 "nat44-out2in-worker-handoff";
1957 feature_name = "nat44-handoff-classify";
1959 else if (sm->endpoint_dependent)
1962 !is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1964 feature_name = "nat44-ed-classify";
1969 !is_inside ? "nat44-in2out" : "nat44-out2in";
1970 feature_name = "nat44-classify";
1973 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1976 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1977 sw_if_index, 0, 0, 0);
1978 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1979 sw_if_index, 1, 0, 0);
1982 if (!sm->endpoint_dependent)
1983 vnet_feature_enable_disable (
1984 "ip4-local", "nat44-hairpinning", sw_if_index, 0, 0, 0);
1996 nat_log_err ("error interface couldn't be found");
1997 return VNET_API_ERROR_NO_SUCH_ENTRY;
2000 pool_get (sm->interfaces, i);
2001 i->sw_if_index = sw_if_index;
2003 nat_validate_counters (sm, sw_if_index);
2005 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2008 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2012 if (is_inside && !sm->out2in_dpo)
2014 if (!sm->endpoint_dependent)
2015 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2016 sw_if_index, 1, 0, 0);
2022 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2026 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2028 /* Add/delete external addresses to FIB */
2031 vec_foreach (ap, sm->addresses)
2032 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2034 pool_foreach (m, sm->static_mappings)
2036 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2039 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2047 snat_interface_add_del_output_feature (u32 sw_if_index,
2048 u8 is_inside, int is_del)
2050 snat_main_t *sm = &snat_main;
2051 snat_interface_t *i;
2053 snat_static_mapping_t *m;
2054 nat_outside_fib_t *outside_fib;
2055 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2060 nat_log_err ("nat44 is disabled");
2061 return VNET_API_ERROR_UNSUPPORTED;
2064 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2066 nat_log_err ("error unsupported");
2067 return VNET_API_ERROR_UNSUPPORTED;
2071 pool_foreach (i, sm->interfaces)
2073 if (i->sw_if_index == sw_if_index)
2075 nat_log_err ("error interface already configured");
2076 return VNET_API_ERROR_VALUE_EXIST;
2081 if (sm->endpoint_dependent)
2082 update_per_vrf_sessions_vec (fib_index, is_del);
2087 vec_foreach (outside_fib, sm->outside_fibs)
2089 if (outside_fib->fib_index == fib_index)
2093 outside_fib->refcount--;
2094 if (!outside_fib->refcount)
2095 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2098 outside_fib->refcount++;
2105 vec_add2 (sm->outside_fibs, outside_fib, 1);
2106 outside_fib->refcount = 1;
2107 outside_fib->fib_index = fib_index;
2114 if (sm->endpoint_dependent)
2117 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2121 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2129 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2133 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2137 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2138 sw_if_index, !is_del, 0, 0);
2139 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2140 sw_if_index, !is_del, 0, 0);
2145 if (sm->num_workers > 1)
2147 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2151 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2154 vnet_feature_enable_disable ("ip4-unicast",
2155 "nat44-out2in-worker-handoff",
2156 sw_if_index, !is_del, 0, 0);
2157 vnet_feature_enable_disable ("ip4-output",
2158 "nat44-in2out-output-worker-handoff",
2159 sw_if_index, !is_del, 0, 0);
2163 if (sm->endpoint_dependent)
2166 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2170 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2174 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2175 sw_if_index, !is_del, 0, 0);
2176 vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
2177 sw_if_index, !is_del, 0, 0);
2182 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2186 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2190 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2191 sw_if_index, !is_del, 0, 0);
2192 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2193 sw_if_index, !is_del, 0, 0);
2198 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2199 sm->fq_in2out_output_index =
2200 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2202 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2203 sm->fq_out2in_index =
2204 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2207 pool_foreach (i, sm->output_feature_interfaces)
2209 if (i->sw_if_index == sw_if_index)
2212 pool_put (sm->output_feature_interfaces, i);
2214 return VNET_API_ERROR_VALUE_EXIST;
2223 nat_log_err ("error interface couldn't be found");
2224 return VNET_API_ERROR_NO_SUCH_ENTRY;
2227 pool_get (sm->output_feature_interfaces, i);
2228 i->sw_if_index = sw_if_index;
2230 nat_validate_counters (sm, sw_if_index);
2232 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2234 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2236 /* Add/delete external addresses to FIB */
2242 vec_foreach (ap, sm->addresses)
2243 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2245 pool_foreach (m, sm->static_mappings)
2247 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2250 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2258 snat_set_workers (uword * bitmap)
2260 snat_main_t *sm = &snat_main;
2263 if (sm->num_workers < 2)
2264 return VNET_API_ERROR_FEATURE_DISABLED;
2266 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2267 return VNET_API_ERROR_INVALID_WORKER;
2269 vec_free (sm->workers);
2271 clib_bitmap_foreach (i, bitmap)
2273 vec_add1(sm->workers, i);
2274 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2275 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2280 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2286 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2287 u32 sw_if_index, u32 new_fib_index,
2290 snat_main_t *sm = &snat_main;
2291 nat_outside_fib_t *outside_fib;
2292 snat_interface_t *i;
2296 if (!sm->enabled || (new_fib_index == old_fib_index)
2297 || (!vec_len (sm->outside_fibs)))
2303 pool_foreach (i, sm->interfaces)
2305 if (i->sw_if_index == sw_if_index)
2307 if (!(nat_interface_is_outside (i)))
2313 pool_foreach (i, sm->output_feature_interfaces)
2315 if (i->sw_if_index == sw_if_index)
2317 if (!(nat_interface_is_outside (i)))
2327 vec_foreach (outside_fib, sm->outside_fibs)
2329 if (outside_fib->fib_index == old_fib_index)
2331 outside_fib->refcount--;
2332 if (!outside_fib->refcount)
2333 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2338 vec_foreach (outside_fib, sm->outside_fibs)
2340 if (outside_fib->fib_index == new_fib_index)
2342 outside_fib->refcount++;
2350 vec_add2 (sm->outside_fibs, outside_fib, 1);
2351 outside_fib->refcount = 1;
2352 outside_fib->fib_index = new_fib_index;
2357 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2358 u32 sw_if_index, u32 new_fib_index,
2362 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2365 ip4_address_t * address,
2367 u32 if_address_index, u32 is_delete);
2370 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2373 ip4_address_t * address,
2375 u32 if_address_index, u32 is_delete);
2378 test_key_calc_split ()
2380 ip4_address_t l_addr;
2381 l_addr.as_u8[0] = 1;
2382 l_addr.as_u8[1] = 1;
2383 l_addr.as_u8[2] = 1;
2384 l_addr.as_u8[3] = 1;
2385 ip4_address_t r_addr;
2386 r_addr.as_u8[0] = 2;
2387 r_addr.as_u8[1] = 2;
2388 r_addr.as_u8[2] = 2;
2389 r_addr.as_u8[3] = 2;
2393 u32 fib_index = 9000001;
2394 u32 thread_index = 3000000001;
2395 u32 session_index = 3000000221;
2396 clib_bihash_kv_16_8_t kv;
2397 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
2398 thread_index, session_index);
2399 ip4_address_t l_addr2;
2400 ip4_address_t r_addr2;
2401 clib_memset (&l_addr2, 0, sizeof (l_addr2));
2402 clib_memset (&r_addr2, 0, sizeof (r_addr2));
2407 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2409 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2410 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2411 ASSERT (l_port == l_port2);
2412 ASSERT (r_port == r_port2);
2413 ASSERT (proto == proto2);
2414 ASSERT (fib_index == fib_index2);
2415 ASSERT (thread_index == ed_value_get_thread_index (&kv));
2416 ASSERT (session_index == ed_value_get_session_index (&kv));
2420 nat_protocol_t proto3 = ~0;
2421 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
2422 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
2423 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2424 ASSERT (l_port == l_port2);
2425 ASSERT (proto == proto3);
2426 ASSERT (fib_index == fib_index2);
2429 static clib_error_t *
2430 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
2432 snat_main_t *sm = &snat_main;
2435 if (sm->endpoint_dependent)
2437 // TODO: consider removing all NAT interfaces
2440 fib_index = ip4_fib_index_from_table_id (table_id);
2441 if (fib_index != ~0)
2442 expire_per_vrf_sessions (fib_index);
2448 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
2451 nat44_set_node_indexes (snat_main_t * sm, vlib_main_t * vm)
2455 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2456 sm->ei_out2in_node_index = node->index;
2457 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2458 sm->ei_in2out_node_index = node->index;
2459 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2460 sm->ei_in2out_output_node_index = node->index;
2462 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2463 sm->ed_out2in_node_index = node->index;
2464 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2465 sm->ed_in2out_node_index = node->index;
2466 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-output");
2467 sm->ed_in2out_output_node_index = node->index;
2469 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2470 sm->error_node_index = node->index;
2471 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2472 sm->pre_in2out_node_index = node->index;
2473 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2474 sm->pre_out2in_node_index = node->index;
2475 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2476 sm->pre_in2out_node_index = node->index;
2477 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2478 sm->pre_out2in_node_index = node->index;
2479 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2480 sm->in2out_fast_node_index = node->index;
2481 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2482 sm->in2out_slowpath_node_index = node->index;
2483 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2484 sm->in2out_slowpath_output_node_index = node->index;
2485 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2486 sm->ed_in2out_slowpath_node_index = node->index;
2487 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2488 sm->out2in_fast_node_index = node->index;
2489 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2490 sm->ed_out2in_slowpath_node_index = node->index;
2491 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2492 sm->hairpinning_node_index = node->index;
2493 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2494 sm->hairpin_dst_node_index = node->index;
2495 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2496 sm->hairpin_src_node_index = node->index;
2499 #define nat_init_simple_counter(c, n, sn) \
2503 c.stat_segment_name = sn; \
2504 vlib_validate_simple_counter (&c, 0); \
2505 vlib_zero_simple_counter (&c, 0); \
2508 static clib_error_t *
2509 nat_init (vlib_main_t * vm)
2511 snat_main_t *sm = &snat_main;
2512 vlib_thread_main_t *tm = vlib_get_thread_main ();
2513 vlib_thread_registration_t *tr;
2514 ip4_add_del_interface_address_callback_t cbi = { 0 };
2515 ip4_table_bind_callback_t cbt = { 0 };
2516 u32 i, num_threads = 0;
2517 uword *p, *bitmap = 0;
2519 clib_memset (sm, 0, sizeof (*sm));
2522 sm->vnet_main = vnet_get_main ();
2524 sm->ip4_main = &ip4_main;
2525 sm->api_main = vlibapi_get_main ();
2526 sm->ip4_lookup_main = &ip4_main.lookup_main;
2528 // frame queue indices used for handoff
2529 sm->fq_out2in_index = ~0;
2530 sm->fq_in2out_index = ~0;
2531 sm->fq_in2out_output_index = ~0;
2533 sm->log_level = SNAT_LOG_ERROR;
2535 nat44_set_node_indexes (sm, vm);
2536 sm->log_class = vlib_log_register_class ("nat", 0);
2537 nat_ipfix_logging_init (vm);
2539 nat_init_simple_counter (sm->total_users, "total-users",
2540 "/nat44/total-users");
2541 nat_init_simple_counter (sm->total_sessions, "total-sessions",
2542 "/nat44/total-sessions");
2543 nat_init_simple_counter (sm->user_limit_reached, "user-limit-reached",
2544 "/nat44/user-limit-reached");
2547 sm->counters.fastpath.in2out.x.name = #x; \
2548 sm->counters.fastpath.in2out.x.stat_segment_name = \
2549 "/nat44/in2out/fastpath/" #x; \
2550 sm->counters.slowpath.in2out.x.name = #x; \
2551 sm->counters.slowpath.in2out.x.stat_segment_name = \
2552 "/nat44/in2out/slowpath/" #x; \
2553 sm->counters.fastpath.out2in.x.name = #x; \
2554 sm->counters.fastpath.out2in.x.stat_segment_name = \
2555 "/nat44/out2in/fastpath/" #x; \
2556 sm->counters.slowpath.out2in.x.name = #x; \
2557 sm->counters.slowpath.out2in.x.stat_segment_name = \
2558 "/nat44/out2in/slowpath/" #x; \
2559 sm->counters.fastpath.in2out_ed.x.name = #x; \
2560 sm->counters.fastpath.in2out_ed.x.stat_segment_name = \
2561 "/nat44/ed/in2out/fastpath/" #x; \
2562 sm->counters.slowpath.in2out_ed.x.name = #x; \
2563 sm->counters.slowpath.in2out_ed.x.stat_segment_name = \
2564 "/nat44/ed/in2out/slowpath/" #x; \
2565 sm->counters.fastpath.out2in_ed.x.name = #x; \
2566 sm->counters.fastpath.out2in_ed.x.stat_segment_name = \
2567 "/nat44/ed/out2in/fastpath/" #x; \
2568 sm->counters.slowpath.out2in_ed.x.name = #x; \
2569 sm->counters.slowpath.out2in_ed.x.stat_segment_name = \
2570 "/nat44/ed/out2in/slowpath/" #x;
2571 foreach_nat_counter;
2573 sm->counters.hairpinning.name = "hairpinning";
2574 sm->counters.hairpinning.stat_segment_name = "/nat44/hairpinning";
2576 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2579 tr = (vlib_thread_registration_t *) p[0];
2582 sm->num_workers = tr->count;
2583 sm->first_worker_index = tr->first_index;
2586 num_threads = tm->n_vlib_mains - 1;
2587 sm->port_per_thread = 0xffff - 1024;
2588 vec_validate (sm->per_thread_data, num_threads);
2590 /* Use all available workers by default */
2591 if (sm->num_workers > 1)
2594 for (i = 0; i < sm->num_workers; i++)
2595 bitmap = clib_bitmap_set (bitmap, i, 1);
2596 snat_set_workers (bitmap);
2597 clib_bitmap_free (bitmap);
2600 sm->per_thread_data[0].snat_thread_index = 0;
2602 /* callbacks to call when interface address changes. */
2603 cbi.function = snat_ip4_add_del_interface_address_cb;
2604 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2605 cbi.function = nat_ip4_add_del_addr_only_sm_cb;
2606 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2608 /* callbacks to call when interface to table biding changes */
2609 cbt.function = snat_update_outside_fib;
2610 vec_add1 (sm->ip4_main->table_bind_callbacks, cbt);
2613 fib_source_allocate ("nat-low", FIB_SOURCE_PRIORITY_LOW,
2614 FIB_SOURCE_BH_SIMPLE);
2616 fib_source_allocate ("nat-hi", FIB_SOURCE_PRIORITY_HI,
2617 FIB_SOURCE_BH_SIMPLE);
2619 /* used only by out2in-dpo feature */
2620 nat_dpo_module_init ();
2622 nat_affinity_init (vm);
2623 nat_ha_init (vm, sm->num_workers, num_threads);
2625 test_key_calc_split ();
2626 return nat44_api_hookup (vm);
2629 VLIB_INIT_FUNCTION (nat_init);
2632 nat44_ed_plugin_enable (nat44_config_t c)
2634 snat_main_t *sm = &snat_main;
2636 if (c.static_mapping_only && !c.connection_tracking)
2638 nat_log_err ("unsupported combination of configuration");
2642 // nat44 feature configuration
2643 sm->endpoint_dependent = c.endpoint_dependent;
2644 sm->static_mapping_only = c.static_mapping_only;
2645 sm->static_mapping_connection_tracking = c.connection_tracking;
2647 // EI only feature (could break ED)
2648 sm->out2in_dpo = c.out2in_dpo;
2650 sm->forwarding_enabled = 0;
2651 sm->mss_clamping = 0;
2652 sm->pat = (!c.static_mapping_only ||
2653 (c.static_mapping_only && c.connection_tracking));
2658 // EI only feature (could break ED)
2659 sm->max_users_per_thread = c.users;
2660 sm->user_buckets = nat_calc_bihash_buckets (c.users);
2663 c.sessions = 10 * 1024;
2665 sm->max_translations_per_thread = c.sessions;
2666 sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2669 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2671 // EI only feature (could break ED)
2672 sm->max_translations_per_user
2673 = c.user_sessions ? c.user_sessions : sm->max_translations_per_thread;
2675 sm->inside_vrf_id = c.inside_vrf;
2676 sm->inside_fib_index =
2677 fib_table_find_or_create_and_lock
2678 (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2680 sm->outside_vrf_id = c.outside_vrf;
2681 sm->outside_fib_index = fib_table_find_or_create_and_lock (
2682 FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2684 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
2685 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2687 sm->in2out_node_index = sm->ed_in2out_node_index;
2688 sm->out2in_node_index = sm->ed_out2in_node_index;
2690 sm->in2out_output_node_index = sm->ed_in2out_output_node_index;
2694 sm->icmp_match_in2out_cb = NULL;
2695 sm->icmp_match_out2in_cb = NULL;
2699 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2700 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2703 nat44_ed_db_init (sm->max_translations_per_thread, sm->translation_buckets,
2706 nat_affinity_enable ();
2708 nat_reset_timeouts (&sm->timeouts);
2710 // TODO: function for reset counters
2711 vlib_zero_simple_counter (&sm->total_users, 0);
2712 vlib_zero_simple_counter (&sm->total_sessions, 0);
2713 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2722 nat44_plugin_enable (nat44_config_t c)
2726 // c.static_mapping_only + c.connection_tracking
2727 // - supported in NAT EI & NAT ED
2728 // c.out2in_dpo, c.static_mapping_only
2729 // - supported in NAT EI
2731 if (c.endpoint_dependent)
2733 if (c.out2in_dpo || c.users || c.user_sessions)
2735 nat_log_err ("unsupported combination of configuration");
2738 return nat44_ed_plugin_enable (c);
2742 // for now just copy variables
2744 nat44_ei_config_t ei_c = {
2745 .inside_vrf = c.inside_vrf,
2746 .outside_vrf = c.outside_vrf,
2748 .sessions = c.sessions,
2749 .user_sessions = c.user_sessions,
2750 .out2in_dpo = c.out2in_dpo,
2751 .static_mapping_only = c.static_mapping_only,
2752 .connection_tracking = c.connection_tracking,
2755 return nat44_ei_plugin_enable (ei_c);
2759 nat44_addresses_free (snat_address_t ** addresses)
2763 vec_foreach (ap, *addresses)
2765 #define _(N, i, n, s) \
2766 vec_free (ap->busy_##n##_ports_per_thread);
2767 foreach_nat_protocol
2771 vec_free (*addresses);
2776 nat44_ed_plugin_disable ()
2778 snat_main_t *sm = &snat_main;
2779 snat_interface_t *i, *vec;
2782 // first unregister all nodes from interfaces
2783 vec = vec_dup (sm->interfaces);
2785 vec_foreach (i, vec)
2787 if (nat_interface_is_inside(i))
2788 error = snat_interface_add_del (i->sw_if_index, 1, 1);
2789 if (nat_interface_is_outside(i))
2790 error = snat_interface_add_del (i->sw_if_index, 0, 1);
2794 nat_log_err ("error occurred while removing interface %u",
2802 vec = vec_dup (sm->output_feature_interfaces);
2804 vec_foreach (i, vec)
2806 if (nat_interface_is_inside(i))
2807 error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
2808 if (nat_interface_is_outside(i))
2809 error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
2813 nat_log_err ("error occurred while removing interface %u",
2819 sm->output_feature_interfaces = 0;
2821 vec_free (sm->max_translations_per_fib);
2823 nat44_ed_db_free ();
2825 nat44_addresses_free (&sm->addresses);
2826 nat44_addresses_free (&sm->twice_nat_addresses);
2829 vec_free (sm->to_resolve);
2830 vec_free (sm->auto_add_sw_if_indices);
2831 vec_free (sm->auto_add_sw_if_indices_twice_nat);
2834 sm->auto_add_sw_if_indices = 0;
2835 sm->auto_add_sw_if_indices_twice_nat = 0;
2837 sm->forwarding_enabled = 0;
2840 clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
2846 nat44_plugin_disable ()
2848 snat_main_t *sm = &snat_main;
2850 fail_if_disabled ();
2852 if (sm->endpoint_dependent)
2853 return nat44_ed_plugin_disable ();
2854 return nat44_ei_plugin_disable ();
2858 snat_free_outside_address_and_port (snat_address_t *addresses,
2859 u32 thread_index, ip4_address_t *addr,
2860 u16 port, nat_protocol_t protocol)
2864 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2866 for (address_index = 0; address_index < vec_len (addresses);
2869 if (addresses[address_index].addr.as_u32 == addr->as_u32)
2873 ASSERT (address_index < vec_len (addresses));
2875 a = addresses + address_index;
2879 #define _(N, i, n, s) \
2880 case NAT_PROTOCOL_##N: \
2881 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2882 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2883 a->busy_##n##_ports--; \
2884 a->busy_##n##_ports_per_thread[thread_index]--; \
2886 foreach_nat_protocol
2889 nat_elog_info ("unknown protocol");
2895 nat_set_outside_address_and_port (snat_address_t *addresses, u32 thread_index,
2896 ip4_address_t addr, u16 port,
2897 nat_protocol_t protocol)
2899 snat_address_t *a = 0;
2901 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2903 for (address_index = 0; address_index < vec_len (addresses);
2906 if (addresses[address_index].addr.as_u32 != addr.as_u32)
2909 a = addresses + address_index;
2912 #define _(N, j, n, s) \
2913 case NAT_PROTOCOL_##N: \
2914 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2915 return VNET_API_ERROR_INSTANCE_IN_USE; \
2916 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2917 a->busy_##n##_ports_per_thread[thread_index]++; \
2918 a->busy_##n##_ports++; \
2920 foreach_nat_protocol
2923 nat_elog_info ("unknown protocol");
2928 return VNET_API_ERROR_NO_SUCH_ENTRY;
2932 snat_static_mapping_match (snat_main_t * sm,
2933 ip4_address_t match_addr,
2935 u32 match_fib_index,
2936 nat_protocol_t match_protocol,
2937 ip4_address_t * mapping_addr,
2939 u32 * mapping_fib_index,
2942 twice_nat_type_t * twice_nat,
2943 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2944 u8 * is_identity_nat, snat_static_mapping_t ** out)
2946 clib_bihash_kv_8_8_t kv, value;
2947 clib_bihash_8_8_t *mapping_hash;
2948 snat_static_mapping_t *m;
2949 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2950 nat44_lb_addr_port_t *local;
2955 mapping_hash = &sm->static_mapping_by_local;
2956 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2958 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2960 /* Try address only mapping */
2961 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2962 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2968 mapping_hash = &sm->static_mapping_by_external;
2969 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2970 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2972 /* Try address only mapping */
2973 init_nat_k (&kv, match_addr, 0, 0, 0);
2974 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2979 m = pool_elt_at_index (sm->static_mappings, value.value);
2983 if (is_lb_static_mapping (m))
2985 if (PREDICT_FALSE (lb != 0))
2986 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2987 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
2993 local = pool_elt_at_index (m->locals, backend_index);
2994 *mapping_addr = local->addr;
2995 *mapping_port = local->port;
2996 *mapping_fib_index = local->fib_index;
2999 // pick locals matching this worker
3000 if (PREDICT_FALSE (sm->num_workers > 1))
3002 u32 thread_index = vlib_get_thread_index ();
3004 pool_foreach_index (i, m->locals)
3006 local = pool_elt_at_index (m->locals, i);
3009 .src_address = local->addr,
3012 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
3019 ASSERT (vec_len (tmp) != 0);
3024 pool_foreach_index (i, m->locals)
3030 hi = vec_len (tmp) - 1;
3031 local = pool_elt_at_index (m->locals, tmp[hi]);
3032 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
3035 mid = ((hi - lo) >> 1) + lo;
3036 local = pool_elt_at_index (m->locals, tmp[mid]);
3037 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
3039 local = pool_elt_at_index (m->locals, tmp[lo]);
3040 if (!(local->prefix >= rand))
3042 *mapping_addr = local->addr;
3043 *mapping_port = local->port;
3044 *mapping_fib_index = local->fib_index;
3047 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
3048 match_protocol, match_port,
3049 tmp[lo], m->affinity,
3050 m->affinity_per_service_list_head_index))
3051 nat_elog_info ("create affinity record failed");
3057 if (PREDICT_FALSE (lb != 0))
3059 *mapping_fib_index = m->fib_index;
3060 *mapping_addr = m->local_addr;
3061 /* Address only mapping doesn't change port */
3062 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3068 *mapping_addr = m->external_addr;
3069 /* Address only mapping doesn't change port */
3070 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3072 *mapping_fib_index = sm->outside_fib_index;
3076 if (PREDICT_FALSE (is_addr_only != 0))
3077 *is_addr_only = is_addr_only_static_mapping (m);
3079 if (PREDICT_FALSE (twice_nat != 0))
3080 *twice_nat = m->twice_nat;
3082 if (PREDICT_FALSE (is_identity_nat != 0))
3083 *is_identity_nat = is_identity_static_mapping (m);
3092 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3094 snat_main_t *sm = &snat_main;
3095 dpo_id_t dpo_v4 = DPO_INVALID;
3096 fib_prefix_t pfx = {
3097 .fp_proto = FIB_PROTOCOL_IP4,
3099 .fp_addr.ip4.as_u32 = addr.as_u32,
3104 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3105 fib_table_entry_special_dpo_add (0, &pfx, sm->fib_src_hi,
3106 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3107 dpo_reset (&dpo_v4);
3111 fib_table_entry_special_remove (0, &pfx, sm->fib_src_hi);
3116 nat44_ed_get_worker_in2out_cb (ip4_header_t *ip, u32 rx_fib_index,
3119 snat_main_t *sm = &snat_main;
3120 u32 next_worker_index = sm->first_worker_index;
3123 clib_bihash_kv_16_8_t kv16, value16;
3124 snat_main_per_thread_data_t *tsm;
3127 if (PREDICT_FALSE (is_output))
3129 u32 fib_index = sm->outside_fib_index;
3130 nat_outside_fib_t *outside_fib;
3131 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3132 fib_prefix_t pfx = {
3133 .fp_proto = FIB_PROTOCOL_IP4,
3136 .ip4.as_u32 = ip->dst_address.as_u32,
3141 udp = ip4_next_header (ip);
3143 switch (vec_len (sm->outside_fibs))
3146 fib_index = sm->outside_fib_index;
3149 fib_index = sm->outside_fibs[0].fib_index;
3153 vec_foreach (outside_fib, sm->outside_fibs)
3155 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3156 if (FIB_NODE_INDEX_INVALID != fei)
3158 if (fib_entry_get_resolving_interface (fei) != ~0)
3160 fib_index = outside_fib->fib_index;
3169 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3170 udp->dst_port, fib_index, ip->protocol);
3173 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
3176 vec_elt_at_index (sm->per_thread_data,
3177 ed_value_get_thread_index (&value16));
3178 next_worker_index += tsm->thread_index;
3180 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3181 next_worker_index, fib_index,
3182 clib_net_to_host_u32 (ip->
3183 src_address.as_u32),
3184 clib_net_to_host_u32 (ip->
3185 dst_address.as_u32));
3187 return next_worker_index;
3191 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3192 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3194 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3195 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3197 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3199 if (PREDICT_TRUE (!is_output))
3201 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3202 next_worker_index, rx_fib_index,
3203 clib_net_to_host_u32 (ip->src_address.as_u32),
3204 clib_net_to_host_u32 (ip->dst_address.as_u32));
3208 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3209 next_worker_index, rx_fib_index,
3210 clib_net_to_host_u32 (ip->src_address.as_u32),
3211 clib_net_to_host_u32 (ip->dst_address.as_u32));
3214 return next_worker_index;
3218 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3219 u32 rx_fib_index, u8 is_output)
3221 snat_main_t *sm = &snat_main;
3222 clib_bihash_kv_8_8_t kv, value;
3223 clib_bihash_kv_16_8_t kv16, value16;
3224 snat_main_per_thread_data_t *tsm;
3226 u32 proto, next_worker_index = 0;
3229 snat_static_mapping_t *m;
3232 proto = ip_proto_to_nat_proto (ip->protocol);
3234 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3236 udp = ip4_next_header (ip);
3238 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3239 udp->src_port, rx_fib_index, ip->protocol);
3242 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
3245 vec_elt_at_index (sm->per_thread_data,
3246 ed_value_get_thread_index (&value16));
3247 vnet_buffer2 (b)->nat.cached_session_index =
3248 ed_value_get_session_index (&value16);
3249 next_worker_index = sm->first_worker_index + tsm->thread_index;
3250 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3251 next_worker_index, rx_fib_index,
3252 clib_net_to_host_u32 (ip->
3253 src_address.as_u32),
3254 clib_net_to_host_u32 (ip->
3255 dst_address.as_u32));
3256 return next_worker_index;
3259 else if (proto == NAT_PROTOCOL_ICMP)
3261 ip4_address_t lookup_saddr, lookup_daddr;
3262 u16 lookup_sport, lookup_dport;
3264 if (!nat_get_icmp_session_lookup_values (
3265 b, ip, &lookup_saddr, &lookup_sport, &lookup_daddr, &lookup_dport,
3268 init_ed_k (&kv16, lookup_saddr, lookup_sport, lookup_daddr,
3269 lookup_dport, rx_fib_index, lookup_protocol);
3271 !clib_bihash_search_16_8 (&sm->flow_hash, &kv16, &value16)))
3274 vec_elt_at_index (sm->per_thread_data,
3275 ed_value_get_thread_index (&value16));
3276 next_worker_index = sm->first_worker_index + tsm->thread_index;
3277 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3278 next_worker_index, rx_fib_index,
3279 clib_net_to_host_u32 (ip->
3280 src_address.as_u32),
3281 clib_net_to_host_u32 (ip->
3282 dst_address.as_u32));
3283 return next_worker_index;
3288 /* first try static mappings without port */
3289 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3291 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3292 if (!clib_bihash_search_8_8
3293 (&sm->static_mapping_by_external, &kv, &value))
3295 m = pool_elt_at_index (sm->static_mappings, value.value);
3296 next_worker_index = m->workers[0];
3301 /* unknown protocol */
3302 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3304 /* use current thread */
3305 next_worker_index = vlib_get_thread_index ();
3309 udp = ip4_next_header (ip);
3310 port = udp->dst_port;
3312 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3314 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3315 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3316 if (!icmp_type_is_error_message
3317 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3318 port = vnet_buffer (b)->ip.reass.l4_src_port;
3321 /* if error message, then it's not fragmented and we can access it */
3322 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3323 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3324 void *l4_header = ip4_next_header (inner_ip);
3327 case NAT_PROTOCOL_ICMP:
3328 icmp = (icmp46_header_t *) l4_header;
3329 echo = (icmp_echo_header_t *) (icmp + 1);
3330 port = echo->identifier;
3332 case NAT_PROTOCOL_UDP:
3333 case NAT_PROTOCOL_TCP:
3334 port = ((tcp_udp_header_t *) l4_header)->src_port;
3337 next_worker_index = vlib_get_thread_index ();
3343 /* try static mappings with port */
3344 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3346 init_nat_k (&kv, ip->dst_address, port, 0, proto);
3347 if (!clib_bihash_search_8_8
3348 (&sm->static_mapping_by_external, &kv, &value))
3350 m = pool_elt_at_index (sm->static_mappings, value.value);
3351 if (!is_lb_static_mapping (m))
3353 next_worker_index = m->workers[0];
3357 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3358 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3360 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3362 m->workers[hash & (_vec_len (m->workers) - 1)];
3364 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3369 /* worker by outside port */
3370 next_worker_index = sm->first_worker_index;
3371 next_worker_index +=
3372 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3375 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3376 clib_net_to_host_u32 (ip->src_address.as_u32),
3377 clib_net_to_host_u32 (ip->dst_address.as_u32));
3378 return next_worker_index;
3382 nat_calc_bihash_buckets (u32 n_elts)
3384 n_elts = n_elts / 2.5;
3386 while (lower_pow2 * 2 < n_elts)
3388 lower_pow2 = 2 * lower_pow2;
3390 u64 upper_pow2 = 2 * lower_pow2;
3391 if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
3393 if (upper_pow2 <= UINT32_MAX)
3402 nat44_get_max_session_limit ()
3404 snat_main_t *sm = &snat_main;
3405 u32 max_limit = 0, len = 0;
3407 for (; len < vec_len (sm->max_translations_per_fib); len++)
3409 if (max_limit < sm->max_translations_per_fib[len])
3410 max_limit = sm->max_translations_per_fib[len];
3416 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
3418 snat_main_t *sm = &snat_main;
3419 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3420 u32 len = vec_len (sm->max_translations_per_fib);
3422 if (len <= fib_index)
3424 vec_validate (sm->max_translations_per_fib, fib_index + 1);
3426 for (; len < vec_len (sm->max_translations_per_fib); len++)
3427 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
3430 sm->max_translations_per_fib[fib_index] = session_limit;
3435 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
3437 snat_main_t *sm = &snat_main;
3439 if (nat44_set_session_limit (session_limit, vrf_id))
3441 sm->max_translations_per_thread = nat44_get_max_session_limit ();
3443 sm->translation_buckets =
3444 nat_calc_bihash_buckets (sm->max_translations_per_thread);
3446 nat44_sessions_clear ();
3451 nat44_ed_worker_db_init (snat_main_per_thread_data_t *tsm, u32 translations,
3452 u32 translation_buckets, u32 user_buckets)
3456 pool_alloc (tsm->sessions, translations);
3457 pool_alloc (tsm->lru_pool, translations);
3459 pool_get (tsm->lru_pool, head);
3460 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
3461 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
3463 pool_get (tsm->lru_pool, head);
3464 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
3465 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
3467 pool_get (tsm->lru_pool, head);
3468 tsm->udp_lru_head_index = head - tsm->lru_pool;
3469 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
3471 pool_get (tsm->lru_pool, head);
3472 tsm->icmp_lru_head_index = head - tsm->lru_pool;
3473 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
3475 pool_get (tsm->lru_pool, head);
3476 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
3477 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
3479 // TODO: ED nat is not using these
3480 // before removal large refactor required
3481 pool_alloc (tsm->list_pool, translations);
3482 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets, 0);
3483 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
3487 reinit_ed_flow_hash ()
3489 snat_main_t *sm = &snat_main;
3490 // we expect 2 flows per session, so multiply translation_buckets by 2
3491 clib_bihash_init_16_8 (
3492 &sm->flow_hash, "ed-flow-hash",
3493 clib_max (1, sm->num_workers) * 2 * sm->translation_buckets, 0);
3494 clib_bihash_set_kvp_format_fn_16_8 (&sm->flow_hash, format_ed_session_kvp);
3498 nat44_ed_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
3500 snat_main_t *sm = &snat_main;
3501 snat_main_per_thread_data_t *tsm;
3502 u32 static_mapping_buckets = 1024;
3503 u32 static_mapping_memory_size = 64 << 20;
3505 reinit_ed_flow_hash ();
3507 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
3508 "static_mapping_by_local", static_mapping_buckets,
3509 static_mapping_memory_size);
3510 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
3511 format_static_mapping_kvp);
3513 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
3514 "static_mapping_by_external", static_mapping_buckets,
3515 static_mapping_memory_size);
3516 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
3517 format_static_mapping_kvp);
3521 vec_foreach (tsm, sm->per_thread_data)
3523 nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
3524 sm->translation_buckets, sm->user_buckets);
3530 nat44_ed_worker_db_free (snat_main_per_thread_data_t *tsm)
3532 pool_free (tsm->sessions);
3533 pool_free (tsm->lru_pool);
3535 vec_free (tsm->per_vrf_sessions_vec);
3537 // TODO: resolve static mappings (put only to !ED)
3538 pool_free (tsm->list_pool);
3539 pool_free (tsm->users);
3540 clib_bihash_free_8_8 (&tsm->user_hash);
3546 snat_main_t *sm = &snat_main;
3547 snat_main_per_thread_data_t *tsm;
3549 pool_free (sm->static_mappings);
3550 clib_bihash_free_16_8 (&sm->flow_hash);
3551 clib_bihash_free_8_8 (&sm->static_mapping_by_local);
3552 clib_bihash_free_8_8 (&sm->static_mapping_by_external);
3556 vec_foreach (tsm, sm->per_thread_data)
3558 nat44_ed_worker_db_free (tsm);
3564 nat44_ed_sessions_clear ()
3566 snat_main_t *sm = &snat_main;
3567 snat_main_per_thread_data_t *tsm;
3569 reinit_ed_flow_hash ();
3573 vec_foreach (tsm, sm->per_thread_data)
3576 nat44_ed_worker_db_free (tsm);
3577 nat44_ed_worker_db_init (tsm, sm->max_translations_per_thread,
3578 sm->translation_buckets, sm->user_buckets);
3582 // TODO: function for reset counters
3583 vlib_zero_simple_counter (&sm->total_users, 0);
3584 vlib_zero_simple_counter (&sm->total_sessions, 0);
3585 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
3589 nat44_sessions_clear ()
3591 snat_main_t *sm = &snat_main;
3593 if (sm->endpoint_dependent)
3594 nat44_ed_sessions_clear ();
3596 nat44_ei_sessions_clear ();
3600 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3603 ip4_address_t * address,
3605 u32 if_address_index, u32 is_delete)
3607 snat_main_t *sm = &snat_main;
3608 snat_static_map_resolve_t *rp;
3609 snat_static_mapping_t *m;
3610 clib_bihash_kv_8_8_t kv, value;
3612 ip4_address_t l_addr;
3617 for (i = 0; i < vec_len (sm->to_resolve); i++)
3619 rp = sm->to_resolve + i;
3620 if (rp->addr_only == 0)
3622 if (rp->sw_if_index == sw_if_index)
3629 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
3630 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
3631 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3634 m = pool_elt_at_index (sm->static_mappings, value.value);
3638 /* Don't trip over lease renewal, static config */
3648 /* Indetity mapping? */
3649 if (rp->l_addr.as_u32 == 0)
3650 l_addr.as_u32 = address[0].as_u32;
3652 l_addr.as_u32 = rp->l_addr.as_u32;
3653 /* Add the static mapping */
3654 rv = snat_add_static_mapping (l_addr,
3659 rp->addr_only, ~0 /* sw_if_index */ ,
3660 rp->proto, !is_delete, rp->twice_nat,
3661 rp->out2in_only, rp->tag, rp->identity_nat,
3662 rp->pool_addr, rp->exact);
3664 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
3668 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3671 ip4_address_t * address,
3673 u32 if_address_index, u32 is_delete)
3675 snat_main_t *sm = &snat_main;
3676 snat_static_map_resolve_t *rp;
3677 ip4_address_t l_addr;
3681 snat_address_t *addresses = sm->addresses;
3686 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
3688 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3692 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3695 addresses = sm->twice_nat_addresses;
3696 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3705 /* Don't trip over lease renewal, static config */
3706 for (j = 0; j < vec_len (addresses); j++)
3707 if (addresses[j].addr.as_u32 == address->as_u32)
3710 (void) snat_add_address (sm, address, ~0, twice_nat);
3711 /* Scan static map resolution vector */
3712 for (j = 0; j < vec_len (sm->to_resolve); j++)
3714 rp = sm->to_resolve + j;
3717 /* On this interface? */
3718 if (rp->sw_if_index == sw_if_index)
3720 /* Indetity mapping? */
3721 if (rp->l_addr.as_u32 == 0)
3722 l_addr.as_u32 = address[0].as_u32;
3724 l_addr.as_u32 = rp->l_addr.as_u32;
3725 /* Add the static mapping */
3726 rv = snat_add_static_mapping (
3727 l_addr, address[0], rp->l_port, rp->e_port, rp->vrf_id,
3728 rp->addr_only, ~0 /* sw_if_index */, rp->proto, 1,
3729 rp->twice_nat, rp->out2in_only, rp->tag, rp->identity_nat,
3730 rp->pool_addr, rp->exact);
3732 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
3740 (void) snat_del_address (sm, address[0], 1, twice_nat);
3746 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3749 ip4_main_t *ip4_main = sm->ip4_main;
3750 ip4_address_t *first_int_addr;
3751 snat_static_map_resolve_t *rp;
3752 u32 *indices_to_delete = 0;
3754 u32 *auto_add_sw_if_indices =
3756 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3758 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3761 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3763 if (auto_add_sw_if_indices[i] == sw_if_index)
3767 /* if have address remove it */
3769 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3772 for (j = 0; j < vec_len (sm->to_resolve); j++)
3774 rp = sm->to_resolve + j;
3775 if (rp->sw_if_index == sw_if_index)
3776 vec_add1 (indices_to_delete, j);
3778 if (vec_len (indices_to_delete))
3780 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3781 vec_del1 (sm->to_resolve, j);
3782 vec_free (indices_to_delete);
3786 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3788 vec_del1 (sm->auto_add_sw_if_indices, i);
3791 return VNET_API_ERROR_VALUE_EXIST;
3798 return VNET_API_ERROR_NO_SUCH_ENTRY;
3800 /* add to the auto-address list */
3802 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3804 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3806 /* If the address is already bound - or static - add it now */
3808 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3814 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3815 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3816 u32 vrf_id, int is_in)
3819 clib_bihash_kv_16_8_t kv, value;
3820 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3822 snat_main_per_thread_data_t *tsm;
3824 if (!sm->endpoint_dependent)
3825 return VNET_API_ERROR_FEATURE_DISABLED;
3827 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3828 if (sm->num_workers > 1)
3830 vec_elt_at_index (sm->per_thread_data,
3831 sm->worker_in2out_cb (&ip, fib_index, 0));
3833 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3835 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
3836 if (clib_bihash_search_16_8 (&sm->flow_hash, &kv, &value))
3838 return VNET_API_ERROR_NO_SUCH_ENTRY;
3841 if (pool_is_free_index (tsm->sessions, ed_value_get_session_index (&value)))
3842 return VNET_API_ERROR_UNSPECIFIED;
3843 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3844 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
3845 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
3849 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
3850 vlib_node_runtime_t * node,
3851 vlib_frame_t * frame)
3857 VLIB_REGISTER_NODE (nat_default_node) = {
3858 .name = "nat-default",
3859 .vector_size = sizeof (u32),
3861 .type = VLIB_NODE_TYPE_INTERNAL,
3863 .n_next_nodes = NAT_N_NEXT,
3865 [NAT_NEXT_DROP] = "error-drop",
3866 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3867 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
3868 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3869 [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
3870 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3871 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
3872 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
3873 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
3874 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
3880 nat_6t_l3_l4_csum_calc (nat_6t_flow_t *f)
3882 f->l3_csum_delta = 0;
3883 f->l4_csum_delta = 0;
3884 if (f->ops & NAT_FLOW_OP_SADDR_REWRITE &&
3885 f->rewrite.saddr.as_u32 != f->match.saddr.as_u32)
3888 ip_csum_add_even (f->l3_csum_delta, f->rewrite.saddr.as_u32);
3890 ip_csum_sub_even (f->l3_csum_delta, f->match.saddr.as_u32);
3894 f->rewrite.saddr.as_u32 = f->match.saddr.as_u32;
3896 if (f->ops & NAT_FLOW_OP_DADDR_REWRITE &&
3897 f->rewrite.daddr.as_u32 != f->match.daddr.as_u32)
3900 ip_csum_add_even (f->l3_csum_delta, f->rewrite.daddr.as_u32);
3902 ip_csum_sub_even (f->l3_csum_delta, f->match.daddr.as_u32);
3906 f->rewrite.daddr.as_u32 = f->match.daddr.as_u32;
3908 if (f->ops & NAT_FLOW_OP_SPORT_REWRITE && f->rewrite.sport != f->match.sport)
3910 f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.sport);
3911 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.sport);
3915 f->rewrite.sport = f->match.sport;
3917 if (f->ops & NAT_FLOW_OP_DPORT_REWRITE && f->rewrite.dport != f->match.dport)
3919 f->l4_csum_delta = ip_csum_add_even (f->l4_csum_delta, f->rewrite.dport);
3920 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.dport);
3924 f->rewrite.dport = f->match.dport;
3926 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE &&
3927 f->rewrite.icmp_id != f->match.icmp_id)
3930 ip_csum_add_even (f->l4_csum_delta, f->rewrite.icmp_id);
3931 f->l4_csum_delta = ip_csum_sub_even (f->l4_csum_delta, f->match.icmp_id);
3935 f->rewrite.icmp_id = f->match.icmp_id;
3937 if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
3942 f->rewrite.fib_index = f->match.fib_index;
3946 static_always_inline int nat_6t_flow_icmp_translate (snat_main_t *sm,
3951 static_always_inline void
3952 nat_6t_flow_ip4_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
3953 nat_6t_flow_t *f, nat_protocol_t proto,
3954 int is_icmp_inner_ip4)
3956 udp_header_t *udp = ip4_next_header (ip);
3957 tcp_header_t *tcp = (tcp_header_t *) udp;
3959 if ((NAT_PROTOCOL_TCP == proto || NAT_PROTOCOL_UDP == proto) &&
3960 !vnet_buffer (b)->ip.reass.is_non_first_fragment)
3962 if (!is_icmp_inner_ip4)
3964 ip->src_address = f->rewrite.saddr;
3965 ip->dst_address = f->rewrite.daddr;
3966 udp->src_port = f->rewrite.sport;
3967 udp->dst_port = f->rewrite.dport;
3970 { // icmp inner ip4 - reversed saddr/daddr
3971 ip->src_address = f->rewrite.daddr;
3972 ip->dst_address = f->rewrite.saddr;
3973 udp->src_port = f->rewrite.dport;
3974 udp->dst_port = f->rewrite.sport;
3977 if (NAT_PROTOCOL_TCP == proto)
3979 ip_csum_t tcp_sum = tcp->checksum;
3980 tcp_sum = ip_csum_sub_even (tcp_sum, f->l3_csum_delta);
3981 tcp_sum = ip_csum_sub_even (tcp_sum, f->l4_csum_delta);
3982 mss_clamping (sm->mss_clamping, tcp, &tcp_sum);
3983 tcp->checksum = ip_csum_fold (tcp_sum);
3985 else if (proto == NAT_PROTOCOL_UDP && udp->checksum)
3987 ip_csum_t udp_sum = udp->checksum;
3988 udp_sum = ip_csum_sub_even (udp_sum, f->l3_csum_delta);
3989 udp_sum = ip_csum_sub_even (udp_sum, f->l4_csum_delta);
3990 udp->checksum = ip_csum_fold (udp_sum);
3995 if (!is_icmp_inner_ip4)
3997 ip->src_address = f->rewrite.saddr;
3998 ip->dst_address = f->rewrite.daddr;
4001 { // icmp inner ip4 - reversed saddr/daddr
4002 ip->src_address = f->rewrite.daddr;
4003 ip->dst_address = f->rewrite.saddr;
4007 ip_csum_t ip_sum = ip->checksum;
4008 ip_sum = ip_csum_sub_even (ip_sum, f->l3_csum_delta);
4009 ip->checksum = ip_csum_fold (ip_sum);
4010 ASSERT (ip->checksum == ip4_header_checksum (ip));
4013 static_always_inline int
4014 nat_6t_flow_icmp_translate (snat_main_t *sm, vlib_buffer_t *b,
4015 ip4_header_t *ip, nat_6t_flow_t *f)
4017 if (IP_PROTOCOL_ICMP != ip->protocol)
4018 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
4020 icmp46_header_t *icmp = ip4_next_header (ip);
4021 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
4023 if ((!vnet_buffer (b)->ip.reass.is_non_first_fragment))
4025 if (icmp->checksum == 0)
4026 icmp->checksum = 0xffff;
4028 if (!icmp_type_is_error_message (icmp->type))
4030 if ((f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE) &&
4031 (f->rewrite.icmp_id != echo->identifier))
4033 ip_csum_t sum = icmp->checksum;
4034 sum = ip_csum_update (sum, echo->identifier, f->rewrite.icmp_id,
4036 identifier /* changed member */);
4037 echo->identifier = f->rewrite.icmp_id;
4038 icmp->checksum = ip_csum_fold (sum);
4043 // errors are not fragmented
4044 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
4046 if (!ip4_header_checksum_is_valid (inner_ip))
4048 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
4051 nat_protocol_t inner_proto =
4052 ip_proto_to_nat_proto (inner_ip->protocol);
4054 ip_csum_t icmp_sum = icmp->checksum;
4056 switch (inner_proto)
4058 case NAT_PROTOCOL_UDP:
4059 case NAT_PROTOCOL_TCP:
4060 nat_6t_flow_ip4_translate (sm, b, inner_ip, f, inner_proto,
4061 1 /* is_icmp_inner_ip4 */);
4062 icmp_sum = ip_csum_sub_even (icmp_sum, f->l3_csum_delta);
4063 icmp->checksum = ip_csum_fold (icmp_sum);
4065 case NAT_PROTOCOL_ICMP:
4066 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
4068 icmp46_header_t *inner_icmp = ip4_next_header (inner_ip);
4069 icmp_echo_header_t *inner_echo =
4070 (icmp_echo_header_t *) (inner_icmp + 1);
4071 if (f->rewrite.icmp_id != inner_echo->identifier)
4073 ip_csum_t sum = icmp->checksum;
4074 sum = ip_csum_update (
4075 sum, inner_echo->identifier, f->rewrite.icmp_id,
4076 icmp_echo_header_t, identifier /* changed member */);
4077 icmp->checksum = ip_csum_fold (sum);
4078 ip_csum_t inner_sum = inner_icmp->checksum;
4079 inner_sum = ip_csum_update (
4080 sum, inner_echo->identifier, f->rewrite.icmp_id,
4081 icmp_echo_header_t, identifier /* changed member */);
4082 inner_icmp->checksum = ip_csum_fold (inner_sum);
4083 inner_echo->identifier = f->rewrite.icmp_id;
4088 clib_warning ("unexpected NAT protocol value `%d'", inner_proto);
4089 return NAT_ED_TRNSL_ERR_TRANSLATION_FAILED;
4093 return NAT_ED_TRNSL_ERR_SUCCESS;
4096 nat_translation_error_e
4097 nat_6t_flow_buf_translate (snat_main_t *sm, vlib_buffer_t *b, ip4_header_t *ip,
4098 nat_6t_flow_t *f, nat_protocol_t proto,
4099 int is_output_feature)
4101 if (!is_output_feature && f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
4103 vnet_buffer (b)->sw_if_index[VLIB_TX] = f->rewrite.fib_index;
4106 nat_6t_flow_ip4_translate (sm, b, ip, f, proto, 0 /* is_icmp_inner_ip4 */);
4108 if (NAT_PROTOCOL_ICMP == proto)
4110 return nat_6t_flow_icmp_translate (sm, b, ip, f);
4113 return NAT_ED_TRNSL_ERR_SUCCESS;
4117 format_nat_6t (u8 *s, va_list *args)
4119 nat_6t_t *t = va_arg (*args, nat_6t_t *);
4121 s = format (s, "saddr %U sport %u daddr %U dport %u proto %U fib_idx %u",
4122 format_ip4_address, t->saddr.as_u8,
4123 clib_net_to_host_u16 (t->sport), format_ip4_address,
4124 t->daddr.as_u8, clib_net_to_host_u16 (t->dport),
4125 format_ip_protocol, t->proto, t->fib_index);
4130 format_nat_ed_translation_error (u8 *s, va_list *args)
4132 nat_translation_error_e e = va_arg (*args, nat_translation_error_e);
4136 case NAT_ED_TRNSL_ERR_SUCCESS:
4137 s = format (s, "success");
4139 case NAT_ED_TRNSL_ERR_TRANSLATION_FAILED:
4140 s = format (s, "translation-failed");
4142 case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
4143 s = format (s, "flow-mismatch");
4150 format_nat_6t_flow (u8 *s, va_list *args)
4152 nat_6t_flow_t *f = va_arg (*args, nat_6t_flow_t *);
4154 s = format (s, "match: %U ", format_nat_6t, &f->match);
4156 if (f->ops & NAT_FLOW_OP_SADDR_REWRITE)
4158 s = format (s, "rewrite: saddr %U ", format_ip4_address,
4159 f->rewrite.saddr.as_u8);
4162 if (f->ops & NAT_FLOW_OP_SPORT_REWRITE)
4166 s = format (s, "rewrite: ");
4169 s = format (s, "sport %u ", clib_net_to_host_u16 (f->rewrite.sport));
4171 if (f->ops & NAT_FLOW_OP_DADDR_REWRITE)
4175 s = format (s, "rewrite: ");
4178 s = format (s, "daddr %U ", format_ip4_address, f->rewrite.daddr.as_u8);
4180 if (f->ops & NAT_FLOW_OP_DPORT_REWRITE)
4184 s = format (s, "rewrite: ");
4187 s = format (s, "dport %u ", clib_net_to_host_u16 (f->rewrite.dport));
4189 if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
4193 s = format (s, "rewrite: ");
4196 s = format (s, "icmp-id %u ", clib_net_to_host_u16 (f->rewrite.icmp_id));
4198 if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
4202 s = format (s, "rewrite: ");
4205 s = format (s, "txfib %u ", f->rewrite.fib_index);
4211 * fd.io coding-style-patch-verification: ON
4214 * eval: (c-set-style "gnu")