nl80211: Use nla_nest_start/end instead of nla_put_nested

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 <johannes.berg@intel.com>
This commit is contained in:
Johannes Berg 2013-03-30 20:37:44 +02:00 committed by Jouni Malinen
parent fde43cddb9
commit 8970bae806

View file

@ -3763,7 +3763,6 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
struct wpa_driver_scan_params *params) struct wpa_driver_scan_params *params)
{ {
struct nl_msg *msg; struct nl_msg *msg;
int err;
size_t i; size_t i;
msg = nlmsg_alloc(); msg = nlmsg_alloc();
@ -3776,23 +3775,20 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
goto fail; goto fail;
if (params->num_ssids) { 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) if (ssids == NULL)
goto fail; goto fail;
for (i = 0; i < params->num_ssids; i++) { for (i = 0; i < params->num_ssids; i++) {
wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
params->ssids[i].ssid, params->ssids[i].ssid,
params->ssids[i].ssid_len); params->ssids[i].ssid_len);
if (nla_put(ssids, i + 1, params->ssids[i].ssid_len, if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
params->ssids[i].ssid) < 0) { params->ssids[i].ssid) < 0)
nlmsg_free(ssids);
goto fail; goto fail;
}
} }
err = nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); nla_nest_end(msg, ssids);
nlmsg_free(ssids);
if (err < 0)
goto fail;
} }
if (params->extra_ies) { if (params->extra_ies) {
@ -3804,22 +3800,17 @@ nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
} }
if (params->freqs) { if (params->freqs) {
struct nl_msg *freqs = nlmsg_alloc(); struct nlattr *freqs;
freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
if (freqs == NULL) if (freqs == NULL)
goto fail; goto fail;
for (i = 0; params->freqs[i]; i++) { for (i = 0; params->freqs[i]; i++) {
wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
"MHz", params->freqs[i]); "MHz", params->freqs[i]);
if (nla_put_u32(freqs, i + 1, params->freqs[i]) < 0) { if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
nlmsg_free(freqs);
goto fail; goto fail;
}
} }
err = nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, nla_nest_end(msg, freqs);
freqs);
nlmsg_free(freqs);
if (err < 0)
goto fail;
} }
os_free(drv->filter_ssids); 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; struct wpa_driver_nl80211_data *drv = bss->drv;
int ret = -1, timeout; int ret = -1, timeout;
struct nl_msg *msg, *rates = NULL; struct nl_msg *msg = NULL;
wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request"); wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
drv->scan_for_auth = 0; drv->scan_for_auth = 0;
@ -3856,9 +3847,11 @@ static int wpa_driver_nl80211_scan(struct i802_bss *bss,
return -1; return -1;
if (params->p2p_probe) { if (params->p2p_probe) {
struct nlattr *rates;
wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates"); 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) if (rates == NULL)
goto nla_put_failure; 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 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
* rates are left enabled. * 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"); "\x0c\x12\x18\x24\x30\x48\x60\x6c");
if (nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates) < nla_nest_end(msg, rates);
0)
goto nla_put_failure;
NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE); 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: nla_put_failure:
nlmsg_free(msg); nlmsg_free(msg);
nlmsg_free(rates);
return ret; return ret;
} }
@ -3946,8 +3936,6 @@ static int wpa_driver_nl80211_sched_scan(void *priv,
struct wpa_driver_nl80211_data *drv = bss->drv; struct wpa_driver_nl80211_data *drv = bss->drv;
int ret = -1; int ret = -1;
struct nl_msg *msg; struct nl_msg *msg;
struct nl_msg *match_set_ssid = NULL, *match_sets = NULL;
struct nl_msg *match_set_rssi = NULL;
size_t i; size_t i;
wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request"); wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
@ -3966,46 +3954,42 @@ static int wpa_driver_nl80211_sched_scan(void *priv,
if ((drv->num_filter_ssids && if ((drv->num_filter_ssids &&
(int) drv->num_filter_ssids <= drv->capa.max_match_sets) || (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
params->filter_rssi) { params->filter_rssi) {
match_sets = nlmsg_alloc(); struct nlattr *match_sets;
match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
if (match_sets == NULL) if (match_sets == NULL)
goto nla_put_failure; goto nla_put_failure;
for (i = 0; i < drv->num_filter_ssids; i++) { for (i = 0; i < drv->num_filter_ssids; i++) {
struct nlattr *match_set_ssid;
wpa_hexdump_ascii(MSG_MSGDUMP, wpa_hexdump_ascii(MSG_MSGDUMP,
"nl80211: Sched scan filter SSID", "nl80211: Sched scan filter SSID",
drv->filter_ssids[i].ssid, drv->filter_ssids[i].ssid,
drv->filter_ssids[i].ssid_len); 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) if (match_set_ssid == NULL)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT(match_set_ssid, NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
drv->filter_ssids[i].ssid_len, drv->filter_ssids[i].ssid_len,
drv->filter_ssids[i].ssid); drv->filter_ssids[i].ssid);
if (nla_put_nested(match_sets, i + 1, match_set_ssid) < nla_nest_end(msg, match_sets);
0)
goto nla_put_failure;
} }
if (params->filter_rssi) { if (params->filter_rssi) {
match_set_rssi = nlmsg_alloc(); struct nlattr *match_set_rssi;
match_set_rssi = nla_nest_start(msg, 0);
if (match_set_rssi == NULL) if (match_set_rssi == NULL)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_U32(match_set_rssi, NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
params->filter_rssi); params->filter_rssi);
wpa_printf(MSG_MSGDUMP, wpa_printf(MSG_MSGDUMP,
"nl80211: Sched scan RSSI filter %d dBm", "nl80211: Sched scan RSSI filter %d dBm",
params->filter_rssi); params->filter_rssi);
if (nla_put_nested(match_sets, 0, match_set_rssi) < 0) nla_nest_end(msg, match_set_rssi);
goto nla_put_failure;
} }
if (nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH, nla_nest_end(msg, match_sets);
match_sets) < 0)
goto nla_put_failure;
} }
ret = send_and_recv_msgs(drv, msg, NULL, NULL); ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@ -4023,9 +4007,6 @@ static int wpa_driver_nl80211_sched_scan(void *priv,
"scan interval %d msec", ret, interval); "scan interval %d msec", ret, interval);
nla_put_failure: nla_put_failure:
nlmsg_free(match_set_ssid);
nlmsg_free(match_sets);
nlmsg_free(match_set_rssi);
nlmsg_free(msg); nlmsg_free(msg);
return ret; return ret;
} }
@ -4514,18 +4495,15 @@ static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
NL80211_KEYTYPE_GROUP); NL80211_KEYTYPE_GROUP);
} }
} else if (addr && is_broadcast_ether_addr(addr)) { } else if (addr && is_broadcast_ether_addr(addr)) {
struct nl_msg *types; struct nlattr *types;
int err;
wpa_printf(MSG_DEBUG, " broadcast key"); wpa_printf(MSG_DEBUG, " broadcast key");
types = nlmsg_alloc();
types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
if (!types) if (!types)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, nla_nest_end(msg, types);
types);
nlmsg_free(types);
if (err)
goto nla_put_failure;
} }
NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
@ -4559,29 +4537,21 @@ static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
else else
NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
if (addr && is_broadcast_ether_addr(addr)) { if (addr && is_broadcast_ether_addr(addr)) {
struct nl_msg *types; struct nlattr *types;
int err;
types = nlmsg_alloc(); types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
if (!types) if (!types)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST); NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, nla_nest_end(msg, types);
types);
nlmsg_free(types);
if (err)
goto nla_put_failure;
} else if (addr) { } else if (addr) {
struct nl_msg *types; struct nlattr *types;
int err;
types = nlmsg_alloc(); types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
if (!types) if (!types)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST); NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES, nla_nest_end(msg, types);
types);
nlmsg_free(types);
if (err)
goto nla_put_failure;
} }
ret = send_and_recv_msgs(drv, msg, NULL, NULL); ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@ -6053,7 +6023,7 @@ static int wpa_driver_nl80211_sta_add(void *priv,
{ {
struct i802_bss *bss = priv; struct i802_bss *bss = priv;
struct wpa_driver_nl80211_data *drv = bss->drv; struct wpa_driver_nl80211_data *drv = bss->drv;
struct nl_msg *msg, *wme = NULL; struct nl_msg *msg;
struct nl80211_sta_flag_update upd; struct nl80211_sta_flag_update upd;
int ret = -ENOBUFS; int ret = -ENOBUFS;
@ -6120,18 +6090,18 @@ static int wpa_driver_nl80211_sta_add(void *priv,
NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
if (params->flags & WPA_STA_WMM) { if (params->flags & WPA_STA_WMM) {
wme = nlmsg_alloc(); struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
if (!wme) if (!wme)
goto nla_put_failure; goto nla_put_failure;
wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo); 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); 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) & (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
WMM_QOSINFO_STA_SP_MASK); WMM_QOSINFO_STA_SP_MASK);
if (nla_put_nested(msg, NL80211_ATTR_STA_WME, wme) < 0) nla_nest_end(msg, wme);
goto nla_put_failure;
} }
ret = send_and_recv_msgs(drv, msg, NULL, NULL); ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@ -6143,7 +6113,6 @@ static int wpa_driver_nl80211_sta_add(void *priv,
if (ret == -EEXIST) if (ret == -EEXIST)
ret = 0; ret = 0;
nla_put_failure: nla_put_failure:
nlmsg_free(wme);
nlmsg_free(msg); nlmsg_free(msg);
return ret; return ret;
} }
@ -6227,7 +6196,7 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
enum nl80211_iftype iftype, enum nl80211_iftype iftype,
const u8 *addr, int wds) const u8 *addr, int wds)
{ {
struct nl_msg *msg, *flags = NULL; struct nl_msg *msg;
int ifidx; int ifidx;
int ret = -ENOBUFS; int ret = -ENOBUFS;
@ -6244,20 +6213,15 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
if (iftype == NL80211_IFTYPE_MONITOR) { if (iftype == NL80211_IFTYPE_MONITOR) {
int err; struct nlattr *flags;
flags = nlmsg_alloc(); flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
if (!flags) if (!flags)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES); NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); nla_nest_end(msg, flags);
nlmsg_free(flags);
if (err)
goto nla_put_failure;
} else if (wds) { } else if (wds) {
NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds); NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
} }
@ -6877,19 +6841,14 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
{ {
struct i802_bss *bss = priv; struct i802_bss *bss = priv;
struct wpa_driver_nl80211_data *drv = bss->drv; 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; struct nl80211_sta_flag_update upd;
msg = nlmsg_alloc(); msg = nlmsg_alloc();
if (!msg) if (!msg)
return -ENOMEM; return -ENOMEM;
flags = nlmsg_alloc();
if (!flags) {
nlmsg_free(msg);
return -ENOMEM;
}
nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION); nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
@ -6900,35 +6859,34 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
* Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
* can be removed eventually. * 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) 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) 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) 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) 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) 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)) nla_nest_end(msg, flags);
goto nla_put_failure;
os_memset(&upd, 0, sizeof(upd)); os_memset(&upd, 0, sizeof(upd));
upd.mask = sta_flags_nl80211(flags_or | ~flags_and); upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
upd.set = sta_flags_nl80211(flags_or); upd.set = sta_flags_nl80211(flags_or);
NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
nlmsg_free(flags);
return send_and_recv_msgs(drv, msg, NULL, NULL); return send_and_recv_msgs(drv, msg, NULL, NULL);
nla_put_failure: nla_put_failure:
nlmsg_free(msg); nlmsg_free(msg);
nlmsg_free(flags);
return -ENOBUFS; return -ENOBUFS;
} }
@ -9001,7 +8959,8 @@ static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
{ {
struct i802_bss *bss = priv; struct i802_bss *bss = priv;
struct wpa_driver_nl80211_data *drv = bss->drv; struct wpa_driver_nl80211_data *drv = bss->drv;
struct nl_msg *msg, *cqm = NULL; struct nl_msg *msg;
struct nlattr *cqm;
int ret = -1; int ret = -1;
wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
@ -9015,20 +8974,18 @@ static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
cqm = nlmsg_alloc(); cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
if (cqm == NULL) if (cqm == NULL)
goto nla_put_failure; goto nla_put_failure;
NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold); NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
if (nla_put_nested(msg, NL80211_ATTR_CQM, cqm) < 0) nla_nest_end(msg, cqm);
goto nla_put_failure;
ret = send_and_recv_msgs(drv, msg, NULL, NULL); ret = send_and_recv_msgs(drv, msg, NULL, NULL);
msg = NULL; msg = NULL;
nla_put_failure: nla_put_failure:
nlmsg_free(cqm);
nlmsg_free(msg); nlmsg_free(msg);
return ret; return ret;
} }