X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fplugins%2Fnat%2Fnat.c;h=48535d9be0aea17211581d6e30a47dbd436daecd;hb=28c2631c3c396da1e2fc62260838f8ce9810d5b5;hp=959abd7ebbf1ff68efbf09f82165d593b922a126;hpb=16f0546cadb1248f9dce99788ecc50cc2668c7e4;p=vpp.git diff --git a/src/plugins/nat/nat.c b/src/plugins/nat/nat.c old mode 100644 new mode 100755 index 959abd7ebbf..48535d9be0a --- a/src/plugins/nat/nat.c +++ b/src/plugins/nat/nat.c @@ -522,7 +522,7 @@ snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index, if (is_add) fib_table_entry_update_one_path(fib_index, &prefix, - FIB_SOURCE_PLUGIN_HI, + FIB_SOURCE_PLUGIN_LOW, (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL | FIB_ENTRY_FLAG_EXCLUSIVE), @@ -536,7 +536,7 @@ snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index, else fib_table_entry_delete(fib_index, &prefix, - FIB_SOURCE_PLUGIN_HI); + FIB_SOURCE_PLUGIN_LOW); } void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id, @@ -562,7 +562,7 @@ void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id, if (vrf_id != ~0) ap->fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, - FIB_SOURCE_PLUGIN_HI); + FIB_SOURCE_PLUGIN_LOW); else ap->fib_index = ~0; #define _(N, i, n, s) \ @@ -682,30 +682,73 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, u32 ses_index; u64 user_index; snat_session_t * s; + snat_static_map_resolve_t *rp, *rp_match = 0; /* If the external address is a specific interface address */ if (sw_if_index != ~0) { ip4_address_t * first_int_addr; + for (i = 0; i < vec_len (sm->to_resolve); i++) + { + rp = sm->to_resolve + i; + if (rp->sw_if_index != sw_if_index && + rp->l_addr.as_u32 != l_addr.as_u32 && + rp->vrf_id != vrf_id && rp->addr_only != addr_only) + continue; + + if (!addr_only) + { + if (rp->l_port != l_port && rp->e_port != e_port && rp->proto != proto) + continue; + } + + rp_match = rp; + break; + } + /* Might be already set... */ first_int_addr = ip4_interface_first_address (sm->ip4_main, sw_if_index, 0 /* just want the address*/); - /* DHCP resolution required? */ - if (first_int_addr == 0) + if (is_add) { + if (rp_match) + return VNET_API_ERROR_VALUE_EXIST; + snat_add_static_mapping_when_resolved (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto, addr_only, is_add, tag); - return 0; + + /* DHCP resolution required? */ + if (first_int_addr == 0) + { + return 0; + } + else + { + e_addr.as_u32 = first_int_addr->as_u32; + /* Identity mapping? */ + if (l_addr.as_u32 == 0) + l_addr.as_u32 = e_addr.as_u32; + } } - else + else { - e_addr.as_u32 = first_int_addr->as_u32; - /* Identity mapping? */ - if (l_addr.as_u32 == 0) - l_addr.as_u32 = e_addr.as_u32; + if (!rp_match) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + vec_del1 (sm->to_resolve, i); + + if (first_int_addr) + { + e_addr.as_u32 = first_int_addr->as_u32; + /* Identity mapping? */ + if (l_addr.as_u32 == 0) + l_addr.as_u32 = e_addr.as_u32; + } + else + return 0; } } @@ -742,6 +785,17 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, vrf_id = sm->inside_vrf_id; } + if (!out2in_only) + { + m_key.addr = l_addr; + m_key.port = addr_only ? 0 : l_port; + m_key.protocol = addr_only ? 0 : proto; + m_key.fib_index = fib_index; + kv.key = m_key.as_u64; + if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value)) + return VNET_API_ERROR_VALUE_EXIST; + } + /* Find external address in allocated addresses and reserve port for address and port pair mapping when dynamic translations enabled */ if (!(addr_only || sm->static_mapping_only || out2in_only)) @@ -776,7 +830,26 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, } /* External address must be allocated */ if (!a && (l_addr.as_u32 != e_addr.as_u32)) - return VNET_API_ERROR_NO_SUCH_ENTRY; + { + if (sw_if_index != ~0) + { + for (i = 0; i < vec_len (sm->to_resolve); i++) + { + rp = sm->to_resolve + i; + if (rp->addr_only) + continue; + if (rp->sw_if_index != sw_if_index && + rp->l_addr.as_u32 != l_addr.as_u32 && + rp->vrf_id != vrf_id && rp->l_port != l_port && + rp->e_port != e_port && rp->proto != proto) + continue; + + vec_del1 (sm->to_resolve, i); + break; + } + } + return VNET_API_ERROR_NO_SUCH_ENTRY; + } } pool_get (sm->static_mappings, m); @@ -888,7 +961,12 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, else { if (!m) - return VNET_API_ERROR_NO_SUCH_ENTRY; + { + if (sw_if_index != ~0) + return 0; + else + return VNET_API_ERROR_NO_SUCH_ENTRY; + } /* Free external address port */ if (!(addr_only || sm->static_mapping_only || out2in_only)) @@ -985,6 +1063,9 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, (clib_net_to_host_u16 (s->out2in.port) != e_port)) continue; } + + if (s->flags & SNAT_SESSION_FLAG_LOAD_BALANCING) + continue; nat_free_session_data (sm, s, tsm - sm->per_thread_data); clib_dlist_remove (tsm->list_pool, s->per_user_index); @@ -995,7 +1076,7 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, if (!addr_only) break; } - if (addr_only) + if (addr_only && (u->nstaticsessions == 0)) { pool_put (tsm->users, u); clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0); @@ -1033,6 +1114,33 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr, return 0; } +static int lb_local_exists (nat44_lb_addr_port_t * local, + ip4_address_t * e_addr, u16 e_port) +{ + snat_main_t *sm = &snat_main; + snat_static_mapping_t *m; + nat44_lb_addr_port_t *ap; + + /* *INDENT-OFF* */ + pool_foreach (m, sm->static_mappings, + ({ + if (vec_len(m->locals)) + { + if (m->external_port == e_port && m->external_addr.as_u32 == e_addr->as_u32) + continue; + + vec_foreach (ap, m->locals) + { + if (ap->port == local->port && ap->addr.as_u32 == local->addr.as_u32) + return 1; + } + } + })); + /* *INDENT-ON* */ + + return 0; +} + int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port, snat_protocol_t proto, u32 vrf_id, nat44_lb_addr_port_t *locals, u8 is_add, @@ -1073,7 +1181,7 @@ int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port, fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, - FIB_SOURCE_PLUGIN_HI); + FIB_SOURCE_PLUGIN_LOW); /* Find external address in allocated addresses and reserve port for address and port pair mapping when dynamic translations enabled */ @@ -1186,7 +1294,7 @@ int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port, if (!m) return VNET_API_ERROR_NO_SUCH_ENTRY; - fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI); + fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW); /* Free external address port */ if (!(sm->static_mapping_only || out2in_only)) @@ -1253,12 +1361,15 @@ int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port, } } - m_key.port = clib_host_to_net_u16 (local->port); - kv.key = m_key.as_u64; - if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0)) + if (!lb_local_exists(local, &e_addr, e_port)) { - clib_warning ("in2out key del failed"); - return VNET_API_ERROR_UNSPECIFIED; + m_key.port = clib_host_to_net_u16 (local->port); + kv.key = m_key.as_u64; + if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0)) + { + clib_warning ("in2out key del failed"); + return VNET_API_ERROR_UNSPECIFIED; + } } /* Delete sessions */ u_key.addr = local->addr; @@ -1354,7 +1465,7 @@ snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm, if (a->fib_index != ~0) fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4, - FIB_SOURCE_PLUGIN_HI); + FIB_SOURCE_PLUGIN_LOW); /* Delete sessions using address */ if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports) @@ -1428,6 +1539,12 @@ int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) if (sm->out2in_dpo && !is_inside) return VNET_API_ERROR_UNSUPPORTED; + pool_foreach (i, sm->output_feature_interfaces, + ({ + if (i->sw_if_index == sw_if_index) + return VNET_API_ERROR_VALUE_EXIST; + })); + if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)) feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast"; else @@ -1579,6 +1696,12 @@ int snat_interface_add_del_output_feature (u32 sw_if_index, (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))) return VNET_API_ERROR_UNSUPPORTED; + pool_foreach (i, sm->interfaces, + ({ + if (i->sw_if_index == sw_if_index) + return VNET_API_ERROR_VALUE_EXIST; + })); + if (is_inside) { vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst", @@ -1690,6 +1813,15 @@ snat_ip4_add_del_interface_address_cb (ip4_main_t * im, u32 if_address_index, u32 is_delete); +static void +nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im, + uword opaque, + u32 sw_if_index, + ip4_address_t * address, + u32 address_length, + u32 if_address_index, + u32 is_delete); + static int nat_alloc_addr_and_port_default (snat_address_t * addresses, u32 fib_index, @@ -1768,6 +1900,11 @@ static clib_error_t * snat_init (vlib_main_t * vm) vec_add1 (im->add_del_interface_address_callbacks, cb4); + cb4.function = nat_ip4_add_del_addr_only_sm_cb; + cb4.function_opaque = 0; + + vec_add1 (im->add_del_interface_address_callbacks, cb4); + nat_dpo_module_init (); /* Init IPFIX logging */ @@ -2648,6 +2785,77 @@ u8 * format_det_map_ses (u8 * s, va_list * args) return s; } +static void +nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im, + uword opaque, + u32 sw_if_index, + ip4_address_t * address, + u32 address_length, + u32 if_address_index, + u32 is_delete) +{ + snat_main_t *sm = &snat_main; + snat_static_map_resolve_t *rp; + snat_static_mapping_t *m; + snat_session_key_t m_key; + clib_bihash_kv_8_8_t kv, value; + int i, rv; + ip4_address_t l_addr; + + for (i = 0; i < vec_len (sm->to_resolve); i++) + { + rp = sm->to_resolve + i; + if (rp->addr_only == 0) + continue; + if (rp->sw_if_index == sw_if_index) + goto match; + } + + return; + +match: + m_key.addr.as_u32 = address->as_u32; + m_key.port = rp->addr_only ? 0 : rp->e_port; + m_key.protocol = rp->addr_only ? 0 : rp->proto; + m_key.fib_index = sm->outside_fib_index; + kv.key = m_key.as_u64; + if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) + m = 0; + else + m = pool_elt_at_index (sm->static_mappings, value.value); + + if (!is_delete) + { + /* Don't trip over lease renewal, static config */ + if (m) + return; + } + else + { + if (!m) + return; + } + + /* Indetity mapping? */ + if (rp->l_addr.as_u32 == 0) + l_addr.as_u32 = address[0].as_u32; + else + l_addr.as_u32 = rp->l_addr.as_u32; + /* Add the static mapping */ + rv = snat_add_static_mapping (l_addr, + address[0], + rp->l_port, + rp->e_port, + rp->vrf_id, + rp->addr_only, + ~0 /* sw_if_index */, + rp->proto, + !is_delete, + 0, 0, rp->tag); + if (rv) + clib_warning ("snat_add_static_mapping returned %d", rv); +} + static void snat_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque, @@ -2659,7 +2867,6 @@ snat_ip4_add_del_interface_address_cb (ip4_main_t * im, { snat_main_t *sm = &snat_main; snat_static_map_resolve_t *rp; - u32 *indices_to_delete = 0; ip4_address_t l_addr; int i, j; int rv; @@ -2695,6 +2902,8 @@ match: for (j = 0; j < vec_len (sm->to_resolve); j++) { rp = sm->to_resolve + j; + if (rp->addr_only) + continue; /* On this interface? */ if (rp->sw_if_index == sw_if_index) { @@ -2717,18 +2926,8 @@ match: if (rv) clib_warning ("snat_add_static_mapping returned %d", rv); - vec_free (rp->tag); - vec_add1 (indices_to_delete, j); } } - /* If we resolved any of the outstanding static mappings */ - if (vec_len(indices_to_delete)) - { - /* Delete them */ - for (j = vec_len(indices_to_delete)-1; j >= 0; j--) - vec_delete(sm->to_resolve, 1, j); - vec_free(indices_to_delete); - } return; } else @@ -2820,7 +3019,7 @@ nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port, snat_user_t *u; ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32; - if (sm->num_workers) + if (sm->num_workers > 1) tsm = vec_elt_at_index (sm->per_thread_data, sm->worker_in2out_cb (&ip, fib_index));