From patchwork Mon Mar 25 14:39:37 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 230729 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from maxx.maxx.shmoo.com (maxx.shmoo.com [205.134.188.171]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "maxx.shmoo.com", Issuer "CA Cert Signing Authority" (not verified)) by ozlabs.org (Postfix) with ESMTPS id D803D2C00A5 for ; Tue, 26 Mar 2013 01:40:06 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id CB1A39D24E; Mon, 25 Mar 2013 10:40:01 -0400 (EDT) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PDm294yUrG4O; Mon, 25 Mar 2013 10:40:01 -0400 (EDT) Received: from maxx.shmoo.com (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id D5C3C9D2B7; Mon, 25 Mar 2013 10:39:56 -0400 (EDT) X-Original-To: mailman-post+hostap@maxx.shmoo.com Delivered-To: mailman-post+hostap@maxx.shmoo.com Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 8C6259D2B7 for ; Mon, 25 Mar 2013 10:39:55 -0400 (EDT) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id r1hrXa9AQ2H7 for ; Mon, 25 Mar 2013 10:39:50 -0400 (EDT) Received: from sipsolutions.net (he.sipsolutions.net [78.46.109.217]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client did not present a certificate) by maxx.maxx.shmoo.com (Postfix) with ESMTPS id 310C29D24E for ; Mon, 25 Mar 2013 10:39:50 -0400 (EDT) Received: by sipsolutions.net with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1UK8Yt-0005k9-Fl; Mon, 25 Mar 2013 15:39:47 +0100 From: Johannes Berg To: hostap@lists.shmoo.com Subject: [PATCH] nl80211: use nla_nest_start Date: Mon, 25 Mar 2013 15:39:37 +0100 Message-Id: <1364222377-23344-1-git-send-email-johannes@sipsolutions.net> X-Mailer: git-send-email 1.8.0 Cc: Johannes Berg X-BeenThere: hostap@lists.shmoo.com X-Mailman-Version: 2.1.9 Precedence: list List-Id: HostAP Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: hostap-bounces@lists.shmoo.com Errors-To: hostap-bounces@lists.shmoo.com From: Johannes Berg Instead of allocating a new message and then moving that into the message being built, use nla_nest_start() and put the data into the message directly. Signed-hostap: Johannes Berg --- src/drivers/driver_nl80211.c | 184 ++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 114 deletions(-) diff --git a/src/drivers/driver_nl80211.c b/src/drivers/driver_nl80211.c index 4637c59..b3a4963 100644 --- a/src/drivers/driver_nl80211.c +++ b/src/drivers/driver_nl80211.c @@ -3763,7 +3763,6 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd, struct wpa_driver_scan_params *params) { struct nl_msg *msg; - int err; size_t i; msg = nlmsg_alloc(); @@ -3776,23 +3775,20 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd, goto fail; if (params->num_ssids) { - struct nl_msg *ssids = nlmsg_alloc(); + struct nlattr *ssids; + + ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS); if (ssids == NULL) goto fail; for (i = 0; i < params->num_ssids; i++) { wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", params->ssids[i].ssid, params->ssids[i].ssid_len); - if (nla_put(ssids, i + 1, params->ssids[i].ssid_len, - params->ssids[i].ssid) < 0) { - nlmsg_free(ssids); + if (nla_put(msg, i + 1, params->ssids[i].ssid_len, + params->ssids[i].ssid) < 0) goto fail; - } } - err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); - nlmsg_free(ssids); - if (err < 0) - goto fail; + nla_nest_end(msg, ssids); } if (params->extra_ies) { @@ -3804,22 +3800,17 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd, } if (params->freqs) { - struct nl_msg *freqs = nlmsg_alloc(); + struct nlattr *freqs; + freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES); if (freqs == NULL) goto fail; for (i = 0; params->freqs[i]; i++) { wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " "MHz", params->freqs[i]); - if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) { - nlmsg_free(freqs); + if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0) goto fail; - } } - err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, - freqs); - nlmsg_free(freqs); - if (err < 0) - goto fail; + nla_nest_end(msg, freqs); } os_free(drv->filter_ssids); @@ -3846,7 +3837,7 @@ static int wpa_driver_nl80211_scan(struct i802_bss *bss, { struct wpa_driver_nl80211_data *drv = bss->drv; int ret = -1, timeout; - struct nl_msg *msg, *rates = NULL; + struct nl_msg *msg = NULL; wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request"); drv->scan_for_auth = 0; @@ -3856,9 +3847,11 @@ static int wpa_driver_nl80211_scan(struct i802_bss *bss, return -1; if (params->p2p_probe) { + struct nlattr *rates; + wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates"); - rates = nlmsg_alloc(); + rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES); if (rates == NULL) goto nla_put_failure; @@ -3868,11 +3861,9 @@ static int wpa_driver_nl80211_scan(struct i802_bss *bss, * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz * rates are left enabled. */ - NLA_PUT(rates, NL80211_BAND_2GHZ, 8, + NLA_PUT(msg, NL80211_BAND_2GHZ, 8, "\x0c\x12\x18\x24\x30\x48\x60\x6c"); - if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) < - 0) - goto nla_put_failure; + nla_nest_end(msg, rates); NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); } @@ -3926,7 +3917,6 @@ static int wpa_driver_nl80211_scan(struct i802_bss *bss, nla_put_failure: nlmsg_free(msg); - nlmsg_free(rates); return ret; } @@ -3946,8 +3936,8 @@ static int wpa_driver_nl80211_sched_scan(void *priv, struct wpa_driver_nl80211_data *drv = bss->drv; int ret = -1; struct nl_msg *msg; - struct nl_msg *match_set_ssid = NULL, *match_sets = NULL; - struct nl_msg *match_set_rssi = NULL; + struct nlattr *match_set_ssid, *match_sets; + struct nlattr *match_set_rssi; size_t i; wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request"); @@ -3966,7 +3956,7 @@ static int wpa_driver_nl80211_sched_scan(void *priv, if ((drv->num_filter_ssids && (int) drv->num_filter_ssids <= drv->capa.max_match_sets) || params->filter_rssi) { - match_sets = nlmsg_alloc(); + match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH); if (match_sets == NULL) goto nla_put_failure; @@ -3976,36 +3966,29 @@ static int wpa_driver_nl80211_sched_scan(void *priv, drv->filter_ssids[i].ssid, drv->filter_ssids[i].ssid_len); - match_set_ssid = nlmsg_alloc(); + match_set_ssid = nla_nest_start(msg, i + 1); if (match_set_ssid == NULL) goto nla_put_failure; - NLA_PUT(match_set_ssid, - NL80211_ATTR_SCHED_SCAN_MATCH_SSID, + NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID, drv->filter_ssids[i].ssid_len, drv->filter_ssids[i].ssid); - if (nla_put_nested(match_sets, i + 1, match_set_ssid) < - 0) - goto nla_put_failure; + nla_nest_end(msg, match_sets); } if (params->filter_rssi) { - match_set_rssi = nlmsg_alloc(); + match_set_rssi = nla_nest_start(msg, 0); if (match_set_rssi == NULL) goto nla_put_failure; - NLA_PUT_U32(match_set_rssi, - NL80211_SCHED_SCAN_MATCH_ATTR_RSSI, + NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI, params->filter_rssi); wpa_printf(MSG_MSGDUMP, "nl80211: Sched scan RSSI filter %d dBm", params->filter_rssi); - if (nla_put_nested(match_sets, 0, match_set_rssi) < 0) - goto nla_put_failure; + nla_nest_end(msg, match_set_rssi); } - if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH, - match_sets) < 0) - goto nla_put_failure; + nla_nest_end(msg, match_sets); } ret = send_and_recv_msgs(drv, msg, NULL, NULL); @@ -4023,9 +4006,6 @@ static int wpa_driver_nl80211_sched_scan(void *priv, "scan interval %d msec", ret, interval); nla_put_failure: - nlmsg_free(match_set_ssid); - nlmsg_free(match_sets); - nlmsg_free(match_set_rssi); nlmsg_free(msg); return ret; } @@ -4514,18 +4494,15 @@ static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss, NL80211_KEYTYPE_GROUP); } } else if (addr && is_broadcast_ether_addr(addr)) { - struct nl_msg *types; - int err; + struct nlattr *types; + wpa_printf(MSG_DEBUG, " broadcast key"); - types = nlmsg_alloc(); + + types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); if (!types) goto nla_put_failure; - NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); - err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, - types); - nlmsg_free(types); - if (err) - goto nla_put_failure; + NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST); + nla_nest_end(msg, types); } NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); @@ -4559,29 +4536,21 @@ static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss, else NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); if (addr && is_broadcast_ether_addr(addr)) { - struct nl_msg *types; - int err; - types = nlmsg_alloc(); + struct nlattr *types; + + types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); if (!types) goto nla_put_failure; - NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); - err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, - types); - nlmsg_free(types); - if (err) - goto nla_put_failure; + NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST); + nla_nest_end(msg, types); } else if (addr) { - struct nl_msg *types; - int err; - types = nlmsg_alloc(); + struct nlattr *types; + + types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES); if (!types) goto nla_put_failure; - NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST); - err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, - types); - nlmsg_free(types); - if (err) - goto nla_put_failure; + NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST); + nla_nest_end(msg, types); } ret = send_and_recv_msgs(drv, msg, NULL, NULL); @@ -6053,7 +6022,7 @@ static int wpa_driver_nl80211_sta_add(void *priv, { struct i802_bss *bss = priv; struct wpa_driver_nl80211_data *drv = bss->drv; - struct nl_msg *msg, *wme = NULL; + struct nl_msg *msg; struct nl80211_sta_flag_update upd; int ret = -ENOBUFS; @@ -6120,18 +6089,18 @@ static int wpa_driver_nl80211_sta_add(void *priv, NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); if (params->flags & WPA_STA_WMM) { - wme = nlmsg_alloc(); + struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME); + if (!wme) goto nla_put_failure; wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo); - NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES, + NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES, params->qosinfo & WMM_QOSINFO_STA_AC_MASK); - NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP, + NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP, (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) & WMM_QOSINFO_STA_SP_MASK); - if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0) - goto nla_put_failure; + nla_nest_end(msg, wme); } ret = send_and_recv_msgs(drv, msg, NULL, NULL); @@ -6143,7 +6112,6 @@ static int wpa_driver_nl80211_sta_add(void *priv, if (ret == -EEXIST) ret = 0; nla_put_failure: - nlmsg_free(wme); nlmsg_free(msg); return ret; } @@ -6227,7 +6195,7 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, enum nl80211_iftype iftype, const u8 *addr, int wds) { - struct nl_msg *msg, *flags = NULL; + struct nl_msg *msg; int ifidx; int ret = -ENOBUFS; @@ -6244,20 +6212,15 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); if (iftype == NL80211_IFTYPE_MONITOR) { - int err; + struct nlattr *flags; - flags = nlmsg_alloc(); + flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS); if (!flags) goto nla_put_failure; - NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES); - - err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); + NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES); - nlmsg_free(flags); - - if (err) - goto nla_put_failure; + nla_nest_end(msg, flags); } else if (wds) { NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds); } @@ -6877,19 +6840,14 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, { struct i802_bss *bss = priv; struct wpa_driver_nl80211_data *drv = bss->drv; - struct nl_msg *msg, *flags = NULL; + struct nl_msg *msg; + struct nlattr *flags; struct nl80211_sta_flag_update upd; msg = nlmsg_alloc(); if (!msg) return -ENOMEM; - flags = nlmsg_alloc(); - if (!flags) { - nlmsg_free(msg); - return -ENOMEM; - } - nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, @@ -6900,35 +6858,34 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This * can be removed eventually. */ + flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS); + if (!flags) + goto nla_put_failure; if (total_flags & WPA_STA_AUTHORIZED) - NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED); + NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED); if (total_flags & WPA_STA_WMM) - NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME); + NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME); if (total_flags & WPA_STA_SHORT_PREAMBLE) - NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE); + NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE); if (total_flags & WPA_STA_MFP) - NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP); + NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP); if (total_flags & WPA_STA_TDLS_PEER) - NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER); + NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER); - if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags)) - goto nla_put_failure; + nla_nest_end(msg, flags); os_memset(&upd, 0, sizeof(upd)); upd.mask = sta_flags_nl80211(flags_or | ~flags_and); upd.set = sta_flags_nl80211(flags_or); NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); - nlmsg_free(flags); - return send_and_recv_msgs(drv, msg, NULL, NULL); nla_put_failure: nlmsg_free(msg); - nlmsg_free(flags); return -ENOBUFS; } @@ -9001,7 +8958,8 @@ static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) { struct i802_bss *bss = priv; struct wpa_driver_nl80211_data *drv = bss->drv; - struct nl_msg *msg, *cqm = NULL; + struct nl_msg *msg; + struct nlattr *cqm; int ret = -1; wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " @@ -9015,20 +8973,18 @@ static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); - cqm = nlmsg_alloc(); + cqm = nla_nest_start(msg, NL80211_ATTR_CQM); if (cqm == NULL) goto nla_put_failure; - NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold); - NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); - if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0) - goto nla_put_failure; + NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold); + NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); + nla_nest_end(msg, cqm); ret = send_and_recv_msgs(drv, msg, NULL, NULL); msg = NULL; nla_put_failure: - nlmsg_free(cqm); nlmsg_free(msg); return ret; }