X-Git-Url: https://gerrit.fd.io/r/gitweb?p=vpp.git;a=blobdiff_plain;f=src%2Fplugins%2Fnat%2Fnat_api.c;h=8055259616f1ea6202e4dba27a8dd050ae00a09b;hp=a1d70f8d46e6b42a5160496990d41c695a51a997;hb=878c646a;hpb=ebdf190a9c4a514329de7e5e9b9178c3af055122 diff --git a/src/plugins/nat/nat_api.c b/src/plugins/nat/nat_api.c index a1d70f8d46e..8055259616f 100644 --- a/src/plugins/nat/nat_api.c +++ b/src/plugins/nat/nat_api.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -96,6 +97,8 @@ vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp) { vl_api_nat_show_config_reply_t *rmp; snat_main_t *sm = &snat_main; + dslite_main_t *dm = &dslite_main; + nat64_main_t *n64m = &nat64_main; int rv = 0; /* *INDENT-OFF* */ @@ -112,6 +115,13 @@ vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp) rmp->static_mapping_connection_tracking = sm->static_mapping_connection_tracking; rmp->deterministic = sm->deterministic; + rmp->endpoint_dependent = sm->endpoint_dependent; + rmp->out2in_dpo = sm->out2in_dpo; + rmp->dslite_ce = dm->is_ce; + rmp->nat64_bib_buckets = n64m->bib_buckets; + rmp->nat64_bib_memory_size = n64m->bib_memory_size; + rmp->nat64_st_buckets = n64m->st_buckets; + rmp->nat64_st_memory_size = n64m->st_memory_size; })); /* *INDENT-ON* */ } @@ -406,6 +416,74 @@ vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle) FINISH; } +static void +vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp) +{ + snat_main_t *sm = &snat_main; + vl_api_nat_set_timeouts_reply_t *rmp; + int rv = 0; + + sm->udp_timeout = ntohl (mp->udp); + sm->tcp_established_timeout = ntohl (mp->tcp_established); + sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory); + sm->icmp_timeout = ntohl (mp->icmp); + + rv = nat64_set_icmp_timeout (ntohl (mp->icmp)); + if (rv) + goto send_reply; + rv = nat64_set_udp_timeout (ntohl (mp->udp)); + if (rv) + goto send_reply; + rv = + nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory), + ntohl (mp->tcp_established)); + +send_reply: + REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY); +} + +static void * +vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle) +{ + u8 *s; + + s = format (0, "SCRIPT: nat_set_timeouts "); + s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n", + ntohl (mp->udp), + ntohl (mp->tcp_established), + ntohl (mp->tcp_transitory), ntohl (mp->icmp)); + + FINISH; +} + +static void +vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp) +{ + snat_main_t *sm = &snat_main; + vl_api_nat_get_timeouts_reply_t *rmp; + int rv = 0; + + /* *INDENT-OFF* */ + REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY, + ({ + rmp->udp = htonl (sm->udp_timeout); + rmp->tcp_established = htonl (sm->tcp_established_timeout); + rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout); + rmp->icmp = htonl (sm->icmp_timeout); + })) + /* *INDENT-ON* */ +} + +static void * +vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle) +{ + u8 *s; + + s = format (0, "SCRIPT: nat_get_timeouts"); + + FINISH; +} + /*************/ /*** NAT44 ***/ /*************/ @@ -444,7 +522,7 @@ static void vrf_id = clib_host_to_net_u32 (mp->vrf_id); if (count > 1024) - clib_warning ("%U - %U, %d addresses...", + nat_log_info ("%U - %U, %d addresses...", format_ip4_address, mp->first_ip_address, format_ip4_address, mp->last_ip_address, count); @@ -453,7 +531,7 @@ static void for (i = 0; i < count; i++) { if (mp->is_add) - snat_add_address (sm, &this_addr, vrf_id, mp->twice_nat); + rv = snat_add_address (sm, &this_addr, vrf_id, mp->twice_nat); else rv = snat_del_address (sm, this_addr, 0, mp->twice_nat); @@ -1157,13 +1235,17 @@ send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg, { vl_api_nat44_user_details_t *rmp; snat_main_t *sm = &snat_main; - fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4); + ip4_main_t *im = &ip4_main; rmp = vl_msg_api_alloc (sizeof (*rmp)); memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base); - rmp->vrf_id = ntohl (fib->ft_table_id); + if (!pool_is_free_index (im->fibs, u->fib_index)) + { + fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4); + rmp->vrf_id = ntohl (fib->ft_table_id); + } clib_memcpy (rmp->ip_address, &(u->addr), 4); rmp->nsessions = ntohl (u->nsessions); @@ -1190,8 +1272,12 @@ vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp) /* *INDENT-OFF* */ vec_foreach (tsm, sm->per_thread_data) - vec_foreach (u, tsm->users) - send_nat44_user_details (u, reg, mp->context); + { + pool_foreach (u, tsm->users, + ({ + send_nat44_user_details (u, reg, mp->context); + })); + } /* *INDENT-ON* */ } @@ -1218,7 +1304,10 @@ send_nat44_user_session_details (snat_session_t * s, ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base); clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4); clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4); - rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0; + rmp->is_static = snat_is_session_static (s) ? 1 : 0; + rmp->is_twicenat = is_twice_nat_session (s) ? 1 : 0; + rmp->ext_host_valid = is_ed_session (s) + || is_fwd_bypass_session (s) ? 1 : 0; rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard); rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes); rmp->total_pkts = ntohl (s->total_pkts); @@ -1235,8 +1324,16 @@ send_nat44_user_session_details (snat_session_t * s, rmp->inside_port = s->in2out.port; rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol)); } - if (s->in2out.protocol == SNAT_PROTOCOL_TCP) - rmp->is_closed = s->state == SNAT_SESSION_TCP_CLOSED ? 1 : 0; + if (is_ed_session (s) || is_fwd_bypass_session (s)) + { + clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4); + rmp->ext_host_port = s->ext_host_port; + if (is_twice_nat_session (s)) + { + clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4); + rmp->ext_host_nat_port = s->ext_host_nat_port; + } + } vl_api_send_msg (reg, (u8 *) rmp); } @@ -1325,6 +1422,7 @@ unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs, clib_memcpy (&lb_addr_port.addr, ap->addr, 4); lb_addr_port.port = clib_net_to_host_u16 (ap->port); lb_addr_port.probability = ap->probability; + lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id); vec_add1 (lb_addr_port_pairs, lb_addr_port); } @@ -1344,7 +1442,7 @@ static void snat_protocol_t proto; u8 *tag = 0; - if (sm->deterministic) + if (!sm->endpoint_dependent) { rv = VNET_API_ERROR_UNSUPPORTED; goto send_reply; @@ -1364,8 +1462,7 @@ static void rv = nat44_add_del_lb_static_mapping (e_addr, clib_net_to_host_u16 (mp->external_port), - proto, clib_net_to_host_u32 (mp->vrf_id), - locals, mp->is_add, twice_nat, + proto, locals, mp->is_add, twice_nat, mp->out2in_only, tag); vec_free (locals); @@ -1407,7 +1504,6 @@ send_nat44_lb_static_mapping_details (snat_static_mapping_t * m, clib_memcpy (rmp->external_addr, &(m->external_addr), 4); rmp->external_port = ntohs (m->external_port); rmp->protocol = snat_proto_to_ip_proto (m->proto); - rmp->vrf_id = ntohl (m->vrf_id); rmp->context = context; if (m->twice_nat == TWICE_NAT) rmp->twice_nat = 1; @@ -1423,6 +1519,7 @@ send_nat44_lb_static_mapping_details (snat_static_mapping_t * m, clib_memcpy (locals->addr, &(ap->addr), 4); locals->port = htons (ap->port); locals->probability = ap->probability; + locals->vrf_id = ntohl (ap->vrf_id); locals++; rmp->local_num++; } @@ -1438,7 +1535,7 @@ static void snat_main_t *sm = &snat_main; snat_static_mapping_t *m; - if (sm->deterministic) + if (!sm->endpoint_dependent) return; reg = vl_api_client_index_to_registration (mp->client_index); @@ -1469,8 +1566,8 @@ vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp) { snat_main_t *sm = &snat_main; vl_api_nat44_del_session_reply_t *rmp; - ip4_address_t addr; - u16 port; + ip4_address_t addr, eh_addr; + u16 port, eh_port; u32 vrf_id; int rv = 0; snat_protocol_t proto; @@ -1485,8 +1582,15 @@ vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp) port = clib_net_to_host_u16 (mp->port); vrf_id = clib_net_to_host_u32 (mp->vrf_id); proto = ip_proto_to_snat_proto (mp->protocol); + memcpy (&eh_addr.as_u8, mp->ext_host_address, 4); + eh_port = clib_net_to_host_u16 (mp->ext_host_port); - rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in); + if (mp->ext_host_valid) + rv = + nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol, + vrf_id, mp->is_in); + else + rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in); send_reply: REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY); @@ -1503,6 +1607,10 @@ vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp, format_ip4_address, mp->address, clib_net_to_host_u16 (mp->port), mp->protocol, clib_net_to_host_u32 (mp->vrf_id), mp->is_in); + if (mp->ext_host_valid) + s = format (s, "ext_host_address %U ext_host_port %d", + format_ip4_address, mp->ext_host_address, + clib_net_to_host_u16 (mp->ext_host_port)); FINISH; } @@ -1514,9 +1622,35 @@ static void snat_main_t *sm = &snat_main; vl_api_nat44_forwarding_enable_disable_reply_t *rmp; int rv = 0; + u32 *ses_to_be_removed = 0, *ses_index; + snat_main_per_thread_data_t *tsm; + snat_session_t *s; sm->forwarding_enabled = mp->enable != 0; + if (mp->enable == 0) + { + /* *INDENT-OFF* */ + vec_foreach (tsm, sm->per_thread_data) + { + pool_foreach (s, tsm->sessions, + ({ + if (is_fwd_bypass_session(s)) + { + vec_add1 (ses_to_be_removed, s - tsm->sessions); + } + })); + vec_foreach (ses_index, ses_to_be_removed) + { + s = pool_elt_at_index(tsm->sessions, ses_index[0]); + nat_free_session_data (sm, s, tsm - sm->per_thread_data); + nat44_delete_session (sm, s, tsm - sm->per_thread_data); + } + vec_free (ses_to_be_removed); + } + /* *INDENT-ON* */ + } + REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY); } @@ -1772,79 +1906,6 @@ vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle) FINISH; } -static void -vl_api_nat_det_set_timeouts_t_handler (vl_api_nat_det_set_timeouts_t * mp) -{ - snat_main_t *sm = &snat_main; - vl_api_nat_det_set_timeouts_reply_t *rmp; - int rv = 0; - - if (!sm->deterministic) - { - rv = VNET_API_ERROR_UNSUPPORTED; - goto send_reply; - } - - sm->udp_timeout = ntohl (mp->udp); - sm->tcp_established_timeout = ntohl (mp->tcp_established); - sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory); - sm->icmp_timeout = ntohl (mp->icmp); - -send_reply: - REPLY_MACRO (VL_API_NAT_DET_SET_TIMEOUTS_REPLY); -} - -static void * -vl_api_nat_det_set_timeouts_t_print (vl_api_nat_det_set_timeouts_t * mp, - void *handle) -{ - u8 *s; - - s = format (0, "SCRIPT: nat_det_set_timeouts "); - s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n", - ntohl (mp->udp), - ntohl (mp->tcp_established), - ntohl (mp->tcp_transitory), ntohl (mp->icmp)); - - FINISH; -} - -static void -vl_api_nat_det_get_timeouts_t_handler (vl_api_nat_det_get_timeouts_t * mp) -{ - snat_main_t *sm = &snat_main; - vl_api_nat_det_get_timeouts_reply_t *rmp; - int rv = 0; - - if (!sm->deterministic) - { - rv = VNET_API_ERROR_UNSUPPORTED; - REPLY_MACRO (VL_API_NAT_DET_GET_TIMEOUTS_REPLY); - return; - } - - /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_NAT_DET_GET_TIMEOUTS_REPLY, - ({ - rmp->udp = htonl (sm->udp_timeout); - rmp->tcp_established = htonl (sm->tcp_established_timeout); - rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout); - rmp->icmp = htonl (sm->icmp_timeout); - })) - /* *INDENT-ON* */ -} - -static void * -vl_api_nat_det_get_timeouts_t_print (vl_api_nat_det_get_timeouts_t * mp, - void *handle) -{ - u8 *s; - - s = format (0, "SCRIPT: nat_det_get_timeouts"); - - FINISH; -} - static void vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t * mp) @@ -2330,71 +2391,6 @@ vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle) FINISH; } -static void -vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp) -{ - snat_main_t *sm = &snat_main; - vl_api_nat64_set_timeouts_reply_t *rmp; - int rv = 0; - - rv = nat64_set_icmp_timeout (ntohl (mp->icmp)); - if (rv) - goto send_reply; - rv = nat64_set_udp_timeout (ntohl (mp->udp)); - if (rv) - goto send_reply; - rv = - nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est), - ntohl (mp->tcp_incoming_syn)); - -send_reply: - REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY); -} - -static void *vl_api_nat64_set_timeouts_t_print - (vl_api_nat64_set_timeouts_t * mp, void *handle) -{ - u8 *s; - - s = format (0, "SCRIPT: nat64_set_timeouts "); - s = - format (s, - "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n", - ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans), - ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn)); - - FINISH; -} - -static void -vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp) -{ - snat_main_t *sm = &snat_main; - vl_api_nat64_get_timeouts_reply_t *rmp; - int rv = 0; - - /* *INDENT-OFF* */ - REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY, - ({ - rmp->udp = htonl (nat64_get_udp_timeout()); - rmp->icmp = htonl (nat64_get_icmp_timeout()); - rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout()); - rmp->tcp_est = htonl (nat64_get_tcp_est_timeout()); - rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout()); - })) - /* *INDENT-ON* */ -} - -static void *vl_api_nat64_get_timeouts_t_print - (vl_api_nat64_get_timeouts_t * mp, void *handle) -{ - u8 *s; - - s = format (0, "SCRIPT: nat64_get_timeouts"); - - FINISH; -} - static int nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg) { @@ -2993,6 +2989,8 @@ _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \ _(NAT_SET_REASS, nat_set_reass) \ _(NAT_GET_REASS, nat_get_reass) \ _(NAT_REASS_DUMP, nat_reass_dump) \ +_(NAT_SET_TIMEOUTS, nat_set_timeouts) \ +_(NAT_GET_TIMEOUTS, nat_get_timeouts) \ _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \ _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \ _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \ @@ -3018,8 +3016,6 @@ _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \ _(NAT_DET_FORWARD, nat_det_forward) \ _(NAT_DET_REVERSE, nat_det_reverse) \ _(NAT_DET_MAP_DUMP, nat_det_map_dump) \ -_(NAT_DET_SET_TIMEOUTS, nat_det_set_timeouts) \ -_(NAT_DET_GET_TIMEOUTS, nat_det_get_timeouts) \ _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \ _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \ _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \ @@ -3029,8 +3025,6 @@ _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \ _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \ _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \ _(NAT64_BIB_DUMP, nat64_bib_dump) \ -_(NAT64_SET_TIMEOUTS, nat64_set_timeouts) \ -_(NAT64_GET_TIMEOUTS, nat64_get_timeouts) \ _(NAT64_ST_DUMP, nat64_st_dump) \ _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \ _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \