From 1c82cd4f491ff83127fbacfb6b09b9492eff1b62 Mon Sep 17 00:00:00 2001 From: Eyal Bari Date: Tue, 14 Mar 2017 14:39:51 +0200 Subject: [PATCH] API:support hidden sw interfaces validate interfaces - added check for hidden interfaces interface dump - dont send hidden interfaces set_unnumbered - test for hidden vl_api_create_vlan_subif_t_handler, vl_api_create_subif_t_handler - fixed potential memory leak some other minor refactors to make code clearer and shorter Change-Id: Icce6b724336b7d1536fbd07a74bf7abe4916d2c0 Signed-off-by: Eyal Bari --- src/vlibapi/api_helper_macros.h | 18 ++++++------ src/vnet/interface_api.c | 65 +++++++++++++++++------------------------ src/vpp/api/api.c | 63 +++++++++++++++------------------------ 3 files changed, 60 insertions(+), 86 deletions(-) diff --git a/src/vlibapi/api_helper_macros.h b/src/vlibapi/api_helper_macros.h index 7f94e446e18..4e281342db5 100644 --- a/src/vlibapi/api_helper_macros.h +++ b/src/vlibapi/api_helper_macros.h @@ -103,11 +103,15 @@ do { \ /* "trust, but verify" */ +static inline uword +vnet_sw_if_index_is_api_valid (u32 sw_if_index) +{ + return vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index); +} + #define VALIDATE_SW_IF_INDEX(mp) \ do { u32 __sw_if_index = ntohl(mp->sw_if_index); \ - vnet_main_t *__vnm = vnet_get_main(); \ - if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ - __sw_if_index)) { \ + if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \ rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ goto bad_sw_if_index; \ } \ @@ -121,9 +125,7 @@ bad_sw_if_index: \ #define VALIDATE_RX_SW_IF_INDEX(mp) \ do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \ - vnet_main_t *__vnm = vnet_get_main(); \ - if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ - __rx_sw_if_index)) { \ + if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \ rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ goto bad_rx_sw_if_index; \ } \ @@ -137,9 +139,7 @@ bad_rx_sw_if_index: \ #define VALIDATE_TX_SW_IF_INDEX(mp) \ do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \ - vnet_main_t *__vnm = vnet_get_main(); \ - if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \ - __tx_sw_if_index)) { \ + if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \ rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \ goto bad_tx_sw_if_index; \ } \ diff --git a/src/vnet/interface_api.c b/src/vnet/interface_api.c index 2b6ff0c590a..28b09b55598 100644 --- a/src/vnet/interface_api.c +++ b/src/vnet/interface_api.c @@ -133,14 +133,10 @@ send_sw_interface_details (vpe_api_main_t * am, vnet_sw_interface_t * swif, u8 * interface_name, u32 context) { - vl_api_sw_interface_details_t *mp; - vnet_main_t *vnm = vnet_get_main (); - vnet_hw_interface_t *hi; - u8 *tag; + vnet_hw_interface_t *hi = + vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); - hi = vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index); - - mp = vl_msg_api_alloc (sizeof (*mp)); + vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS); mp->sw_if_index = ntohl (swif->sw_if_index); @@ -224,7 +220,7 @@ send_sw_interface_details (vpe_api_main_t * am, mp->i_sid = i_sid; } - tag = vnet_get_sw_interface_tag (vnm, swif->sw_if_index); + u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index); if (tag) strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1); @@ -237,39 +233,38 @@ vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp) vpe_api_main_t *am = &vpe_api_main; vnet_sw_interface_t *swif; vnet_interface_main_t *im = &am->vnet_main->interface_main; - u8 *filter_string = 0, *name_string = 0; - unix_shared_memory_queue_t *q; - char *strcasestr (char *, char *); /* lnx hdr file botch */ - - q = vl_api_client_index_to_input_queue (mp->client_index); + unix_shared_memory_queue_t *q = + vl_api_client_index_to_input_queue (mp->client_index); if (q == 0) return; + u8 *filter = 0, *name = 0; if (mp->name_filter_valid) { mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0; - filter_string = format (0, "%s%c", mp->name_filter, 0); + filter = format (0, "%s%c", mp->name_filter, 0); } + char *strcasestr (char *, char *); /* lnx hdr file botch */ /* *INDENT-OFF* */ pool_foreach (swif, im->sw_interfaces, ({ - name_string = format (name_string, "%U%c", - format_vnet_sw_interface_name, - am->vnet_main, swif, 0); + if (!vnet_swif_is_api_visible (swif)) + continue; + vec_reset_length(name); + name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main, + swif, 0); - if (mp->name_filter_valid == 0 || - strcasestr((char *) name_string, (char *) filter_string)) { + if (filter && !strcasestr((char *) name, (char *) filter)) + continue; - send_sw_interface_details (am, q, swif, name_string, mp->context); - } - _vec_len (name_string) = 0; + send_sw_interface_details (am, q, swif, name, mp->context); })); /* *INDENT-ON* */ - vec_free (name_string); - vec_free (filter_string); + vec_free (name); + vec_free (filter); } static void @@ -435,49 +430,43 @@ static void vl_api_sw_interface_set_unnumbered_t_handler { vl_api_sw_interface_set_unnumbered_reply_t *rmp; int rv = 0; - vnet_sw_interface_t *si; vnet_main_t *vnm = vnet_get_main (); - u32 sw_if_index, unnumbered_sw_if_index; - - sw_if_index = ntohl (mp->sw_if_index); - unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index); + u32 sw_if_index = ntohl (mp->sw_if_index); + u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index); /* * The API message field names are backwards from * the underlying data structure names. * It's not worth changing them now. */ - if (pool_is_free_index (vnm->interface_main.sw_interfaces, - unnumbered_sw_if_index)) + if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index)) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; goto done; } /* Only check the "use loop0" field when setting the binding */ - if (mp->is_add && - pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) + if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index)) { rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2; goto done; } - si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index); + vnet_sw_interface_t *si = + vnet_get_sw_interface (vnm, unnumbered_sw_if_index); if (mp->is_add) { si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED; si->unnumbered_sw_if_index = sw_if_index; - ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 1); - ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 1); } else { si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED); si->unnumbered_sw_if_index = (u32) ~ 0; - ip4_sw_interface_enable_disable (unnumbered_sw_if_index, 0); - ip6_sw_interface_enable_disable (unnumbered_sw_if_index, 0); } + ip4_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add); + ip6_sw_interface_enable_disable (unnumbered_sw_if_index, mp->is_add); done: REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY); diff --git a/src/vpp/api/api.c b/src/vpp/api/api.c index 673ffe5618c..d8301fa69bb 100644 --- a/src/vpp/api/api.c +++ b/src/vpp/api/api.c @@ -477,7 +477,6 @@ vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp) uword *p; vnet_interface_main_t *im = &vnm->interface_main; u64 sup_and_sub_key; - u64 *kp; unix_shared_memory_queue_t *q; clib_error_t *error; @@ -507,9 +506,6 @@ vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp) goto out; } - kp = clib_mem_alloc (sizeof (*kp)); - *kp = sup_and_sub_key; - memset (&template, 0, sizeof (template)); template.type = VNET_SW_INTERFACE_TYPE_SUB; template.sup_sw_if_index = hi->sw_if_index; @@ -526,6 +522,10 @@ vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp) rv = VNET_API_ERROR_INVALID_REGISTRATION; goto out; } + + u64 *kp = clib_mem_alloc (sizeof (*kp)); + *kp = sup_and_sub_key; + hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index); hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index); @@ -537,10 +537,10 @@ out: return; rmp = vl_msg_api_alloc (sizeof (*rmp)); - rmp->_vl_msg_id = ntohs (VL_API_CREATE_VLAN_SUBIF_REPLY); + rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY); rmp->context = mp->context; - rmp->retval = ntohl (rv); - rmp->sw_if_index = ntohl (sw_if_index); + rmp->retval = htonl (rv); + rmp->sw_if_index = htonl (sw_if_index); vl_msg_api_send_shmem (q, (u8 *) & rmp); } @@ -558,7 +558,6 @@ vl_api_create_subif_t_handler (vl_api_create_subif_t * mp) uword *p; vnet_interface_main_t *im = &vnm->interface_main; u64 sup_and_sub_key; - u64 *kp; clib_error_t *error; VALIDATE_SW_IF_INDEX (mp); @@ -587,9 +586,6 @@ vl_api_create_subif_t_handler (vl_api_create_subif_t * mp) goto out; } - kp = clib_mem_alloc (sizeof (*kp)); - *kp = sup_and_sub_key; - memset (&template, 0, sizeof (template)); template.type = VNET_SW_INTERFACE_TYPE_SUB; template.sup_sw_if_index = sw_if_index; @@ -613,6 +609,9 @@ vl_api_create_subif_t_handler (vl_api_create_subif_t * mp) goto out; } + u64 *kp = clib_mem_alloc (sizeof (*kp)); + *kp = sup_and_sub_key; + hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index); hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index); @@ -669,20 +668,11 @@ static void int rv = 0; vnet_main_t *vnm = vnet_get_main (); vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp; - vnet_sw_interface_t *si; - u32 sw_if_index; VALIDATE_SW_IF_INDEX (mp); - sw_if_index = ntohl (mp->sw_if_index); - - if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) - { - rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; - goto out; - } - - si = vnet_get_sw_interface (vnm, sw_if_index); + vnet_sw_interface_t *si = + vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index)); ASSERT (si); @@ -1223,13 +1213,12 @@ static void vl_api_classify_set_interface_ip_table_t_handler vlib_main_t *vm = vlib_get_main (); vl_api_classify_set_interface_ip_table_reply_t *rmp; int rv; - u32 table_index, sw_if_index; - - table_index = ntohl (mp->table_index); - sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); + u32 table_index = ntohl (mp->table_index); + u32 sw_if_index = ntohl (mp->sw_if_index); + if (mp->is_ipv6) rv = vnet_set_ip6_classify_intfc (vm, sw_if_index, table_index); else @@ -1658,15 +1647,14 @@ static void vl_api_input_acl_set_interface_t_handler vlib_main_t *vm = vlib_get_main (); vl_api_input_acl_set_interface_reply_t *rmp; int rv; - u32 sw_if_index, ip4_table_index, ip6_table_index, l2_table_index; - - ip4_table_index = ntohl (mp->ip4_table_index); - ip6_table_index = ntohl (mp->ip6_table_index); - l2_table_index = ntohl (mp->l2_table_index); - sw_if_index = ntohl (mp->sw_if_index); VALIDATE_SW_IF_INDEX (mp); + u32 ip4_table_index = ntohl (mp->ip4_table_index); + u32 ip6_table_index = ntohl (mp->ip6_table_index); + u32 l2_table_index = ntohl (mp->l2_table_index); + u32 sw_if_index = ntohl (mp->sw_if_index); + rv = vnet_set_input_acl_intfc (vm, sw_if_index, ip4_table_index, ip6_table_index, l2_table_index, mp->is_add); @@ -2013,25 +2001,22 @@ vl_api_feature_enable_disable_t_handler (vl_api_feature_enable_disable_t * mp) { vl_api_feature_enable_disable_reply_t *rmp; int rv = 0; - u8 *arc_name, *feature_name; VALIDATE_SW_IF_INDEX (mp); - arc_name = format (0, "%s%c", mp->arc_name, 0); - feature_name = format (0, "%s%c", mp->feature_name, 0); + u8 *arc_name = format (0, "%s%c", mp->arc_name, 0); + u8 *feature_name = format (0, "%s%c", mp->feature_name, 0); - vnet_feature_registration_t *reg; - reg = + vnet_feature_registration_t *reg = vnet_get_feature_reg ((const char *) arc_name, (const char *) feature_name); if (reg == 0) rv = VNET_API_ERROR_INVALID_VALUE; else { - u32 sw_if_index; + u32 sw_if_index = ntohl (mp->sw_if_index); clib_error_t *error = 0; - sw_if_index = ntohl (mp->sw_if_index); if (reg->enable_disable_cb) error = reg->enable_disable_cb (sw_if_index, mp->enable); if (!error) -- 2.16.6