wl1251: use wiphy_dev instead of wl->spi->dev
[linux/fpc-iii.git] / net / wireless / nl80211.c
blob0cd548267d4a717a11e636dce9d548dd03ebd7ea
1 /*
2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <linux/if_ether.h>
12 #include <linux/ieee80211.h>
13 #include <linux/nl80211.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/netlink.h>
16 #include <linux/etherdevice.h>
17 #include <net/net_namespace.h>
18 #include <net/genetlink.h>
19 #include <net/cfg80211.h>
20 #include <net/sock.h>
21 #include "core.h"
22 #include "nl80211.h"
23 #include "reg.h"
25 /* the netlink family */
26 static struct genl_family nl80211_fam = {
27 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
28 .name = "nl80211", /* have users key off the name instead */
29 .hdrsize = 0, /* no private header */
30 .version = 1, /* no particular meaning now */
31 .maxattr = NL80211_ATTR_MAX,
32 .netnsok = true,
35 /* internal helper: get rdev and dev */
36 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
37 struct cfg80211_registered_device **rdev,
38 struct net_device **dev)
40 struct nlattr **attrs = info->attrs;
41 int ifindex;
43 if (!attrs[NL80211_ATTR_IFINDEX])
44 return -EINVAL;
46 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
47 *dev = dev_get_by_index(genl_info_net(info), ifindex);
48 if (!*dev)
49 return -ENODEV;
51 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
52 if (IS_ERR(*rdev)) {
53 dev_put(*dev);
54 return PTR_ERR(*rdev);
57 return 0;
60 /* policy for the attributes */
61 static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
62 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
63 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
64 .len = 20-1 },
65 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
66 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
67 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
68 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
69 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
73 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
74 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
77 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
78 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
80 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
81 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
82 .len = WLAN_MAX_KEY_LEN },
83 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
84 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
85 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
86 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
88 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
89 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
90 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
91 .len = IEEE80211_MAX_DATA_LEN },
92 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
94 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
95 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
96 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
98 .len = NL80211_MAX_SUPP_RATES },
99 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
100 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
101 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
102 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_MESH_ID_LEN },
104 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
106 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
107 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
109 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
110 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
115 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
117 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
118 .len = NL80211_HT_CAPABILITY_LEN },
120 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
121 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
122 .len = IEEE80211_MAX_DATA_LEN },
123 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
124 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
126 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
127 .len = IEEE80211_MAX_SSID_LEN },
128 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
129 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
130 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
131 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
132 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
133 [NL80211_ATTR_STA_FLAGS2] = {
134 .len = sizeof(struct nl80211_sta_flag_update),
136 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
137 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
138 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
139 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
140 [NL80211_ATTR_PID] = { .type = NLA_U32 },
143 /* policy for the attributes */
144 static struct nla_policy
145 nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
146 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
147 [NL80211_KEY_IDX] = { .type = NLA_U8 },
148 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
149 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
150 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
151 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
154 /* IE validation */
155 static bool is_valid_ie_attr(const struct nlattr *attr)
157 const u8 *pos;
158 int len;
160 if (!attr)
161 return true;
163 pos = nla_data(attr);
164 len = nla_len(attr);
166 while (len) {
167 u8 elemlen;
169 if (len < 2)
170 return false;
171 len -= 2;
173 elemlen = pos[1];
174 if (elemlen > len)
175 return false;
177 len -= elemlen;
178 pos += 2 + elemlen;
181 return true;
184 /* message building helper */
185 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
186 int flags, u8 cmd)
188 /* since there is no private header just add the generic one */
189 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
192 static int nl80211_msg_put_channel(struct sk_buff *msg,
193 struct ieee80211_channel *chan)
195 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
196 chan->center_freq);
198 if (chan->flags & IEEE80211_CHAN_DISABLED)
199 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
200 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
201 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
202 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
203 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
204 if (chan->flags & IEEE80211_CHAN_RADAR)
205 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
207 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
208 DBM_TO_MBM(chan->max_power));
210 return 0;
212 nla_put_failure:
213 return -ENOBUFS;
216 /* netlink command implementations */
218 struct key_parse {
219 struct key_params p;
220 int idx;
221 bool def, defmgmt;
224 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
226 struct nlattr *tb[NL80211_KEY_MAX + 1];
227 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
228 nl80211_key_policy);
229 if (err)
230 return err;
232 k->def = !!tb[NL80211_KEY_DEFAULT];
233 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
235 if (tb[NL80211_KEY_IDX])
236 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
238 if (tb[NL80211_KEY_DATA]) {
239 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
240 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
243 if (tb[NL80211_KEY_SEQ]) {
244 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
245 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
248 if (tb[NL80211_KEY_CIPHER])
249 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
251 return 0;
254 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
256 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
257 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
258 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
261 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
262 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
263 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
266 if (info->attrs[NL80211_ATTR_KEY_IDX])
267 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
269 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
270 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
272 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
273 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
275 return 0;
278 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
280 int err;
282 memset(k, 0, sizeof(*k));
283 k->idx = -1;
285 if (info->attrs[NL80211_ATTR_KEY])
286 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
287 else
288 err = nl80211_parse_key_old(info, k);
290 if (err)
291 return err;
293 if (k->def && k->defmgmt)
294 return -EINVAL;
296 if (k->idx != -1) {
297 if (k->defmgmt) {
298 if (k->idx < 4 || k->idx > 5)
299 return -EINVAL;
300 } else if (k->def) {
301 if (k->idx < 0 || k->idx > 3)
302 return -EINVAL;
303 } else {
304 if (k->idx < 0 || k->idx > 5)
305 return -EINVAL;
309 return 0;
312 static struct cfg80211_cached_keys *
313 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
314 struct nlattr *keys)
316 struct key_parse parse;
317 struct nlattr *key;
318 struct cfg80211_cached_keys *result;
319 int rem, err, def = 0;
321 result = kzalloc(sizeof(*result), GFP_KERNEL);
322 if (!result)
323 return ERR_PTR(-ENOMEM);
325 result->def = -1;
326 result->defmgmt = -1;
328 nla_for_each_nested(key, keys, rem) {
329 memset(&parse, 0, sizeof(parse));
330 parse.idx = -1;
332 err = nl80211_parse_key_new(key, &parse);
333 if (err)
334 goto error;
335 err = -EINVAL;
336 if (!parse.p.key)
337 goto error;
338 if (parse.idx < 0 || parse.idx > 4)
339 goto error;
340 if (parse.def) {
341 if (def)
342 goto error;
343 def = 1;
344 result->def = parse.idx;
345 } else if (parse.defmgmt)
346 goto error;
347 err = cfg80211_validate_key_settings(rdev, &parse.p,
348 parse.idx, NULL);
349 if (err)
350 goto error;
351 result->params[parse.idx].cipher = parse.p.cipher;
352 result->params[parse.idx].key_len = parse.p.key_len;
353 result->params[parse.idx].key = result->data[parse.idx];
354 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
357 return result;
358 error:
359 kfree(result);
360 return ERR_PTR(err);
363 static int nl80211_key_allowed(struct wireless_dev *wdev)
365 ASSERT_WDEV_LOCK(wdev);
367 if (!netif_running(wdev->netdev))
368 return -ENETDOWN;
370 switch (wdev->iftype) {
371 case NL80211_IFTYPE_AP:
372 case NL80211_IFTYPE_AP_VLAN:
373 break;
374 case NL80211_IFTYPE_ADHOC:
375 if (!wdev->current_bss)
376 return -ENOLINK;
377 break;
378 case NL80211_IFTYPE_STATION:
379 if (wdev->sme_state != CFG80211_SME_CONNECTED)
380 return -ENOLINK;
381 break;
382 default:
383 return -EINVAL;
386 return 0;
389 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
390 struct cfg80211_registered_device *dev)
392 void *hdr;
393 struct nlattr *nl_bands, *nl_band;
394 struct nlattr *nl_freqs, *nl_freq;
395 struct nlattr *nl_rates, *nl_rate;
396 struct nlattr *nl_modes;
397 struct nlattr *nl_cmds;
398 enum ieee80211_band band;
399 struct ieee80211_channel *chan;
400 struct ieee80211_rate *rate;
401 int i;
402 u16 ifmodes = dev->wiphy.interface_modes;
404 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
405 if (!hdr)
406 return -1;
408 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
409 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
411 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
412 dev->wiphy.retry_short);
413 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
414 dev->wiphy.retry_long);
415 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
416 dev->wiphy.frag_threshold);
417 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
418 dev->wiphy.rts_threshold);
420 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
421 dev->wiphy.max_scan_ssids);
422 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
423 dev->wiphy.max_scan_ie_len);
425 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
426 sizeof(u32) * dev->wiphy.n_cipher_suites,
427 dev->wiphy.cipher_suites);
429 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
430 if (!nl_modes)
431 goto nla_put_failure;
433 i = 0;
434 while (ifmodes) {
435 if (ifmodes & 1)
436 NLA_PUT_FLAG(msg, i);
437 ifmodes >>= 1;
438 i++;
441 nla_nest_end(msg, nl_modes);
443 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
444 if (!nl_bands)
445 goto nla_put_failure;
447 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
448 if (!dev->wiphy.bands[band])
449 continue;
451 nl_band = nla_nest_start(msg, band);
452 if (!nl_band)
453 goto nla_put_failure;
455 /* add HT info */
456 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
457 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
458 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
459 &dev->wiphy.bands[band]->ht_cap.mcs);
460 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
461 dev->wiphy.bands[band]->ht_cap.cap);
462 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
463 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
464 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
465 dev->wiphy.bands[band]->ht_cap.ampdu_density);
468 /* add frequencies */
469 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
470 if (!nl_freqs)
471 goto nla_put_failure;
473 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
474 nl_freq = nla_nest_start(msg, i);
475 if (!nl_freq)
476 goto nla_put_failure;
478 chan = &dev->wiphy.bands[band]->channels[i];
480 if (nl80211_msg_put_channel(msg, chan))
481 goto nla_put_failure;
483 nla_nest_end(msg, nl_freq);
486 nla_nest_end(msg, nl_freqs);
488 /* add bitrates */
489 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
490 if (!nl_rates)
491 goto nla_put_failure;
493 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
494 nl_rate = nla_nest_start(msg, i);
495 if (!nl_rate)
496 goto nla_put_failure;
498 rate = &dev->wiphy.bands[band]->bitrates[i];
499 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
500 rate->bitrate);
501 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
502 NLA_PUT_FLAG(msg,
503 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
505 nla_nest_end(msg, nl_rate);
508 nla_nest_end(msg, nl_rates);
510 nla_nest_end(msg, nl_band);
512 nla_nest_end(msg, nl_bands);
514 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
515 if (!nl_cmds)
516 goto nla_put_failure;
518 i = 0;
519 #define CMD(op, n) \
520 do { \
521 if (dev->ops->op) { \
522 i++; \
523 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
525 } while (0)
527 CMD(add_virtual_intf, NEW_INTERFACE);
528 CMD(change_virtual_intf, SET_INTERFACE);
529 CMD(add_key, NEW_KEY);
530 CMD(add_beacon, NEW_BEACON);
531 CMD(add_station, NEW_STATION);
532 CMD(add_mpath, NEW_MPATH);
533 CMD(set_mesh_params, SET_MESH_PARAMS);
534 CMD(change_bss, SET_BSS);
535 CMD(auth, AUTHENTICATE);
536 CMD(assoc, ASSOCIATE);
537 CMD(deauth, DEAUTHENTICATE);
538 CMD(disassoc, DISASSOCIATE);
539 CMD(join_ibss, JOIN_IBSS);
540 if (dev->wiphy.netnsok) {
541 i++;
542 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
545 #undef CMD
547 if (dev->ops->connect || dev->ops->auth) {
548 i++;
549 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
552 if (dev->ops->disconnect || dev->ops->deauth) {
553 i++;
554 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
557 nla_nest_end(msg, nl_cmds);
559 return genlmsg_end(msg, hdr);
561 nla_put_failure:
562 genlmsg_cancel(msg, hdr);
563 return -EMSGSIZE;
566 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
568 int idx = 0;
569 int start = cb->args[0];
570 struct cfg80211_registered_device *dev;
572 mutex_lock(&cfg80211_mutex);
573 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
574 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
575 continue;
576 if (++idx <= start)
577 continue;
578 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
579 cb->nlh->nlmsg_seq, NLM_F_MULTI,
580 dev) < 0) {
581 idx--;
582 break;
585 mutex_unlock(&cfg80211_mutex);
587 cb->args[0] = idx;
589 return skb->len;
592 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
594 struct sk_buff *msg;
595 struct cfg80211_registered_device *dev;
597 dev = cfg80211_get_dev_from_info(info);
598 if (IS_ERR(dev))
599 return PTR_ERR(dev);
601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
602 if (!msg)
603 goto out_err;
605 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
606 goto out_free;
608 cfg80211_unlock_rdev(dev);
610 return genlmsg_reply(msg, info);
612 out_free:
613 nlmsg_free(msg);
614 out_err:
615 cfg80211_unlock_rdev(dev);
616 return -ENOBUFS;
619 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
620 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
621 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
622 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
623 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
624 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
627 static int parse_txq_params(struct nlattr *tb[],
628 struct ieee80211_txq_params *txq_params)
630 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
631 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
632 !tb[NL80211_TXQ_ATTR_AIFS])
633 return -EINVAL;
635 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
636 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
637 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
638 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
639 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
641 return 0;
644 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
646 struct cfg80211_registered_device *rdev;
647 int result = 0, rem_txq_params = 0;
648 struct nlattr *nl_txq_params;
649 u32 changed;
650 u8 retry_short = 0, retry_long = 0;
651 u32 frag_threshold = 0, rts_threshold = 0;
653 rtnl_lock();
655 mutex_lock(&cfg80211_mutex);
657 rdev = __cfg80211_rdev_from_info(info);
658 if (IS_ERR(rdev)) {
659 mutex_unlock(&cfg80211_mutex);
660 result = PTR_ERR(rdev);
661 goto unlock;
664 mutex_lock(&rdev->mtx);
666 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
667 result = cfg80211_dev_rename(
668 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
670 mutex_unlock(&cfg80211_mutex);
672 if (result)
673 goto bad_res;
675 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
676 struct ieee80211_txq_params txq_params;
677 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
679 if (!rdev->ops->set_txq_params) {
680 result = -EOPNOTSUPP;
681 goto bad_res;
684 nla_for_each_nested(nl_txq_params,
685 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
686 rem_txq_params) {
687 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
688 nla_data(nl_txq_params),
689 nla_len(nl_txq_params),
690 txq_params_policy);
691 result = parse_txq_params(tb, &txq_params);
692 if (result)
693 goto bad_res;
695 result = rdev->ops->set_txq_params(&rdev->wiphy,
696 &txq_params);
697 if (result)
698 goto bad_res;
702 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
703 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
704 struct ieee80211_channel *chan;
705 struct ieee80211_sta_ht_cap *ht_cap;
706 u32 freq;
708 if (!rdev->ops->set_channel) {
709 result = -EOPNOTSUPP;
710 goto bad_res;
713 result = -EINVAL;
715 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
716 channel_type = nla_get_u32(info->attrs[
717 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
718 if (channel_type != NL80211_CHAN_NO_HT &&
719 channel_type != NL80211_CHAN_HT20 &&
720 channel_type != NL80211_CHAN_HT40PLUS &&
721 channel_type != NL80211_CHAN_HT40MINUS)
722 goto bad_res;
725 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
726 chan = ieee80211_get_channel(&rdev->wiphy, freq);
728 /* Primary channel not allowed */
729 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
730 goto bad_res;
732 if (channel_type == NL80211_CHAN_HT40MINUS &&
733 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
734 goto bad_res;
735 else if (channel_type == NL80211_CHAN_HT40PLUS &&
736 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
737 goto bad_res;
740 * At this point we know if that if HT40 was requested
741 * we are allowed to use it and the extension channel
742 * exists.
745 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
747 /* no HT capabilities or intolerant */
748 if (channel_type != NL80211_CHAN_NO_HT) {
749 if (!ht_cap->ht_supported)
750 goto bad_res;
751 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
752 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
753 goto bad_res;
756 result = rdev->ops->set_channel(&rdev->wiphy, chan,
757 channel_type);
758 if (result)
759 goto bad_res;
761 rdev->channel = chan;
764 changed = 0;
766 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
767 retry_short = nla_get_u8(
768 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
769 if (retry_short == 0) {
770 result = -EINVAL;
771 goto bad_res;
773 changed |= WIPHY_PARAM_RETRY_SHORT;
776 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
777 retry_long = nla_get_u8(
778 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
779 if (retry_long == 0) {
780 result = -EINVAL;
781 goto bad_res;
783 changed |= WIPHY_PARAM_RETRY_LONG;
786 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
787 frag_threshold = nla_get_u32(
788 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
789 if (frag_threshold < 256) {
790 result = -EINVAL;
791 goto bad_res;
793 if (frag_threshold != (u32) -1) {
795 * Fragments (apart from the last one) are required to
796 * have even length. Make the fragmentation code
797 * simpler by stripping LSB should someone try to use
798 * odd threshold value.
800 frag_threshold &= ~0x1;
802 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
805 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
806 rts_threshold = nla_get_u32(
807 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
808 changed |= WIPHY_PARAM_RTS_THRESHOLD;
811 if (changed) {
812 u8 old_retry_short, old_retry_long;
813 u32 old_frag_threshold, old_rts_threshold;
815 if (!rdev->ops->set_wiphy_params) {
816 result = -EOPNOTSUPP;
817 goto bad_res;
820 old_retry_short = rdev->wiphy.retry_short;
821 old_retry_long = rdev->wiphy.retry_long;
822 old_frag_threshold = rdev->wiphy.frag_threshold;
823 old_rts_threshold = rdev->wiphy.rts_threshold;
825 if (changed & WIPHY_PARAM_RETRY_SHORT)
826 rdev->wiphy.retry_short = retry_short;
827 if (changed & WIPHY_PARAM_RETRY_LONG)
828 rdev->wiphy.retry_long = retry_long;
829 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
830 rdev->wiphy.frag_threshold = frag_threshold;
831 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
832 rdev->wiphy.rts_threshold = rts_threshold;
834 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
835 if (result) {
836 rdev->wiphy.retry_short = old_retry_short;
837 rdev->wiphy.retry_long = old_retry_long;
838 rdev->wiphy.frag_threshold = old_frag_threshold;
839 rdev->wiphy.rts_threshold = old_rts_threshold;
843 bad_res:
844 mutex_unlock(&rdev->mtx);
845 unlock:
846 rtnl_unlock();
847 return result;
851 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
852 struct cfg80211_registered_device *rdev,
853 struct net_device *dev)
855 void *hdr;
857 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
858 if (!hdr)
859 return -1;
861 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
862 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
863 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
864 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
865 return genlmsg_end(msg, hdr);
867 nla_put_failure:
868 genlmsg_cancel(msg, hdr);
869 return -EMSGSIZE;
872 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
874 int wp_idx = 0;
875 int if_idx = 0;
876 int wp_start = cb->args[0];
877 int if_start = cb->args[1];
878 struct cfg80211_registered_device *dev;
879 struct wireless_dev *wdev;
881 mutex_lock(&cfg80211_mutex);
882 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
883 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
884 continue;
885 if (wp_idx < wp_start) {
886 wp_idx++;
887 continue;
889 if_idx = 0;
891 mutex_lock(&dev->devlist_mtx);
892 list_for_each_entry(wdev, &dev->netdev_list, list) {
893 if (if_idx < if_start) {
894 if_idx++;
895 continue;
897 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
898 cb->nlh->nlmsg_seq, NLM_F_MULTI,
899 dev, wdev->netdev) < 0) {
900 mutex_unlock(&dev->devlist_mtx);
901 goto out;
903 if_idx++;
905 mutex_unlock(&dev->devlist_mtx);
907 wp_idx++;
909 out:
910 mutex_unlock(&cfg80211_mutex);
912 cb->args[0] = wp_idx;
913 cb->args[1] = if_idx;
915 return skb->len;
918 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
920 struct sk_buff *msg;
921 struct cfg80211_registered_device *dev;
922 struct net_device *netdev;
923 int err;
925 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
926 if (err)
927 return err;
929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
930 if (!msg)
931 goto out_err;
933 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
934 dev, netdev) < 0)
935 goto out_free;
937 dev_put(netdev);
938 cfg80211_unlock_rdev(dev);
940 return genlmsg_reply(msg, info);
942 out_free:
943 nlmsg_free(msg);
944 out_err:
945 dev_put(netdev);
946 cfg80211_unlock_rdev(dev);
947 return -ENOBUFS;
950 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
951 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
952 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
953 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
954 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
955 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
958 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
960 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
961 int flag;
963 *mntrflags = 0;
965 if (!nla)
966 return -EINVAL;
968 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
969 nla, mntr_flags_policy))
970 return -EINVAL;
972 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
973 if (flags[flag])
974 *mntrflags |= (1<<flag);
976 return 0;
979 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
981 struct cfg80211_registered_device *rdev;
982 struct vif_params params;
983 int err;
984 enum nl80211_iftype otype, ntype;
985 struct net_device *dev;
986 u32 _flags, *flags = NULL;
987 bool change = false;
989 memset(&params, 0, sizeof(params));
991 rtnl_lock();
993 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
994 if (err)
995 goto unlock_rtnl;
997 otype = ntype = dev->ieee80211_ptr->iftype;
999 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1000 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1001 if (otype != ntype)
1002 change = true;
1003 if (ntype > NL80211_IFTYPE_MAX) {
1004 err = -EINVAL;
1005 goto unlock;
1009 if (!rdev->ops->change_virtual_intf ||
1010 !(rdev->wiphy.interface_modes & (1 << ntype))) {
1011 err = -EOPNOTSUPP;
1012 goto unlock;
1015 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1016 if (ntype != NL80211_IFTYPE_MESH_POINT) {
1017 err = -EINVAL;
1018 goto unlock;
1020 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1021 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1022 change = true;
1025 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1026 if (ntype != NL80211_IFTYPE_MONITOR) {
1027 err = -EINVAL;
1028 goto unlock;
1030 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1031 &_flags);
1032 if (err)
1033 goto unlock;
1035 flags = &_flags;
1036 change = true;
1039 if (change)
1040 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
1041 ntype, flags, &params);
1042 else
1043 err = 0;
1045 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1047 if (!err && (ntype != otype)) {
1048 if (otype == NL80211_IFTYPE_ADHOC)
1049 cfg80211_clear_ibss(dev, false);
1052 unlock:
1053 dev_put(dev);
1054 cfg80211_unlock_rdev(rdev);
1055 unlock_rtnl:
1056 rtnl_unlock();
1057 return err;
1060 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1062 struct cfg80211_registered_device *rdev;
1063 struct vif_params params;
1064 int err;
1065 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1066 u32 flags;
1068 memset(&params, 0, sizeof(params));
1070 if (!info->attrs[NL80211_ATTR_IFNAME])
1071 return -EINVAL;
1073 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1074 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1075 if (type > NL80211_IFTYPE_MAX)
1076 return -EINVAL;
1079 rtnl_lock();
1081 rdev = cfg80211_get_dev_from_info(info);
1082 if (IS_ERR(rdev)) {
1083 err = PTR_ERR(rdev);
1084 goto unlock_rtnl;
1087 if (!rdev->ops->add_virtual_intf ||
1088 !(rdev->wiphy.interface_modes & (1 << type))) {
1089 err = -EOPNOTSUPP;
1090 goto unlock;
1093 if (type == NL80211_IFTYPE_MESH_POINT &&
1094 info->attrs[NL80211_ATTR_MESH_ID]) {
1095 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1096 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1099 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1100 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1101 &flags);
1102 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1103 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1104 type, err ? NULL : &flags, &params);
1106 unlock:
1107 cfg80211_unlock_rdev(rdev);
1108 unlock_rtnl:
1109 rtnl_unlock();
1110 return err;
1113 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1115 struct cfg80211_registered_device *rdev;
1116 int err;
1117 struct net_device *dev;
1119 rtnl_lock();
1121 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1122 if (err)
1123 goto unlock_rtnl;
1125 if (!rdev->ops->del_virtual_intf) {
1126 err = -EOPNOTSUPP;
1127 goto out;
1130 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1132 out:
1133 cfg80211_unlock_rdev(rdev);
1134 dev_put(dev);
1135 unlock_rtnl:
1136 rtnl_unlock();
1137 return err;
1140 struct get_key_cookie {
1141 struct sk_buff *msg;
1142 int error;
1143 int idx;
1146 static void get_key_callback(void *c, struct key_params *params)
1148 struct nlattr *key;
1149 struct get_key_cookie *cookie = c;
1151 if (params->key)
1152 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1153 params->key_len, params->key);
1155 if (params->seq)
1156 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1157 params->seq_len, params->seq);
1159 if (params->cipher)
1160 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1161 params->cipher);
1163 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1164 if (!key)
1165 goto nla_put_failure;
1167 if (params->key)
1168 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1169 params->key_len, params->key);
1171 if (params->seq)
1172 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1173 params->seq_len, params->seq);
1175 if (params->cipher)
1176 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1177 params->cipher);
1179 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1181 nla_nest_end(cookie->msg, key);
1183 return;
1184 nla_put_failure:
1185 cookie->error = 1;
1188 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1190 struct cfg80211_registered_device *rdev;
1191 int err;
1192 struct net_device *dev;
1193 u8 key_idx = 0;
1194 u8 *mac_addr = NULL;
1195 struct get_key_cookie cookie = {
1196 .error = 0,
1198 void *hdr;
1199 struct sk_buff *msg;
1201 if (info->attrs[NL80211_ATTR_KEY_IDX])
1202 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1204 if (key_idx > 5)
1205 return -EINVAL;
1207 if (info->attrs[NL80211_ATTR_MAC])
1208 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1210 rtnl_lock();
1212 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1213 if (err)
1214 goto unlock_rtnl;
1216 if (!rdev->ops->get_key) {
1217 err = -EOPNOTSUPP;
1218 goto out;
1221 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1222 if (!msg) {
1223 err = -ENOMEM;
1224 goto out;
1227 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1228 NL80211_CMD_NEW_KEY);
1230 if (IS_ERR(hdr)) {
1231 err = PTR_ERR(hdr);
1232 goto free_msg;
1235 cookie.msg = msg;
1236 cookie.idx = key_idx;
1238 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1239 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1240 if (mac_addr)
1241 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1243 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
1244 &cookie, get_key_callback);
1246 if (err)
1247 goto free_msg;
1249 if (cookie.error)
1250 goto nla_put_failure;
1252 genlmsg_end(msg, hdr);
1253 err = genlmsg_reply(msg, info);
1254 goto out;
1256 nla_put_failure:
1257 err = -ENOBUFS;
1258 free_msg:
1259 nlmsg_free(msg);
1260 out:
1261 cfg80211_unlock_rdev(rdev);
1262 dev_put(dev);
1263 unlock_rtnl:
1264 rtnl_unlock();
1266 return err;
1269 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1271 struct cfg80211_registered_device *rdev;
1272 struct key_parse key;
1273 int err;
1274 struct net_device *dev;
1275 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1276 u8 key_index);
1278 err = nl80211_parse_key(info, &key);
1279 if (err)
1280 return err;
1282 if (key.idx < 0)
1283 return -EINVAL;
1285 /* only support setting default key */
1286 if (!key.def && !key.defmgmt)
1287 return -EINVAL;
1289 rtnl_lock();
1291 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1292 if (err)
1293 goto unlock_rtnl;
1295 if (key.def)
1296 func = rdev->ops->set_default_key;
1297 else
1298 func = rdev->ops->set_default_mgmt_key;
1300 if (!func) {
1301 err = -EOPNOTSUPP;
1302 goto out;
1305 wdev_lock(dev->ieee80211_ptr);
1306 err = nl80211_key_allowed(dev->ieee80211_ptr);
1307 if (!err)
1308 err = func(&rdev->wiphy, dev, key.idx);
1310 #ifdef CONFIG_WIRELESS_EXT
1311 if (!err) {
1312 if (func == rdev->ops->set_default_key)
1313 dev->ieee80211_ptr->wext.default_key = key.idx;
1314 else
1315 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1317 #endif
1318 wdev_unlock(dev->ieee80211_ptr);
1320 out:
1321 cfg80211_unlock_rdev(rdev);
1322 dev_put(dev);
1324 unlock_rtnl:
1325 rtnl_unlock();
1327 return err;
1330 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1332 struct cfg80211_registered_device *rdev;
1333 int err;
1334 struct net_device *dev;
1335 struct key_parse key;
1336 u8 *mac_addr = NULL;
1338 err = nl80211_parse_key(info, &key);
1339 if (err)
1340 return err;
1342 if (!key.p.key)
1343 return -EINVAL;
1345 if (info->attrs[NL80211_ATTR_MAC])
1346 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1348 rtnl_lock();
1350 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1351 if (err)
1352 goto unlock_rtnl;
1354 if (!rdev->ops->add_key) {
1355 err = -EOPNOTSUPP;
1356 goto out;
1359 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1360 err = -EINVAL;
1361 goto out;
1364 wdev_lock(dev->ieee80211_ptr);
1365 err = nl80211_key_allowed(dev->ieee80211_ptr);
1366 if (!err)
1367 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1368 mac_addr, &key.p);
1369 wdev_unlock(dev->ieee80211_ptr);
1371 out:
1372 cfg80211_unlock_rdev(rdev);
1373 dev_put(dev);
1374 unlock_rtnl:
1375 rtnl_unlock();
1377 return err;
1380 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1382 struct cfg80211_registered_device *rdev;
1383 int err;
1384 struct net_device *dev;
1385 u8 *mac_addr = NULL;
1386 struct key_parse key;
1388 err = nl80211_parse_key(info, &key);
1389 if (err)
1390 return err;
1392 if (info->attrs[NL80211_ATTR_MAC])
1393 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1395 rtnl_lock();
1397 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1398 if (err)
1399 goto unlock_rtnl;
1401 if (!rdev->ops->del_key) {
1402 err = -EOPNOTSUPP;
1403 goto out;
1406 wdev_lock(dev->ieee80211_ptr);
1407 err = nl80211_key_allowed(dev->ieee80211_ptr);
1408 if (!err)
1409 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
1411 #ifdef CONFIG_WIRELESS_EXT
1412 if (!err) {
1413 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1414 dev->ieee80211_ptr->wext.default_key = -1;
1415 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1416 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1418 #endif
1419 wdev_unlock(dev->ieee80211_ptr);
1421 out:
1422 cfg80211_unlock_rdev(rdev);
1423 dev_put(dev);
1425 unlock_rtnl:
1426 rtnl_unlock();
1428 return err;
1431 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1433 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1434 struct beacon_parameters *info);
1435 struct cfg80211_registered_device *rdev;
1436 int err;
1437 struct net_device *dev;
1438 struct beacon_parameters params;
1439 int haveinfo = 0;
1441 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1442 return -EINVAL;
1444 rtnl_lock();
1446 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1447 if (err)
1448 goto unlock_rtnl;
1450 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1451 err = -EOPNOTSUPP;
1452 goto out;
1455 switch (info->genlhdr->cmd) {
1456 case NL80211_CMD_NEW_BEACON:
1457 /* these are required for NEW_BEACON */
1458 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1459 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1460 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1461 err = -EINVAL;
1462 goto out;
1465 call = rdev->ops->add_beacon;
1466 break;
1467 case NL80211_CMD_SET_BEACON:
1468 call = rdev->ops->set_beacon;
1469 break;
1470 default:
1471 WARN_ON(1);
1472 err = -EOPNOTSUPP;
1473 goto out;
1476 if (!call) {
1477 err = -EOPNOTSUPP;
1478 goto out;
1481 memset(&params, 0, sizeof(params));
1483 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1484 params.interval =
1485 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1486 haveinfo = 1;
1489 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1490 params.dtim_period =
1491 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1492 haveinfo = 1;
1495 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1496 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1497 params.head_len =
1498 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1499 haveinfo = 1;
1502 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1503 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1504 params.tail_len =
1505 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1506 haveinfo = 1;
1509 if (!haveinfo) {
1510 err = -EINVAL;
1511 goto out;
1514 err = call(&rdev->wiphy, dev, &params);
1516 out:
1517 cfg80211_unlock_rdev(rdev);
1518 dev_put(dev);
1519 unlock_rtnl:
1520 rtnl_unlock();
1522 return err;
1525 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1527 struct cfg80211_registered_device *rdev;
1528 int err;
1529 struct net_device *dev;
1531 rtnl_lock();
1533 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1534 if (err)
1535 goto unlock_rtnl;
1537 if (!rdev->ops->del_beacon) {
1538 err = -EOPNOTSUPP;
1539 goto out;
1542 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1543 err = -EOPNOTSUPP;
1544 goto out;
1546 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
1548 out:
1549 cfg80211_unlock_rdev(rdev);
1550 dev_put(dev);
1551 unlock_rtnl:
1552 rtnl_unlock();
1554 return err;
1557 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1558 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1559 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1560 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1561 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1564 static int parse_station_flags(struct genl_info *info,
1565 struct station_parameters *params)
1567 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1568 struct nlattr *nla;
1569 int flag;
1572 * Try parsing the new attribute first so userspace
1573 * can specify both for older kernels.
1575 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1576 if (nla) {
1577 struct nl80211_sta_flag_update *sta_flags;
1579 sta_flags = nla_data(nla);
1580 params->sta_flags_mask = sta_flags->mask;
1581 params->sta_flags_set = sta_flags->set;
1582 if ((params->sta_flags_mask |
1583 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1584 return -EINVAL;
1585 return 0;
1588 /* if present, parse the old attribute */
1590 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1591 if (!nla)
1592 return 0;
1594 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1595 nla, sta_flags_policy))
1596 return -EINVAL;
1598 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1599 params->sta_flags_mask &= ~1;
1601 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1602 if (flags[flag])
1603 params->sta_flags_set |= (1<<flag);
1605 return 0;
1608 static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1610 int modulation, streams, bitrate;
1612 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1613 return rate->legacy;
1615 /* the formula below does only work for MCS values smaller than 32 */
1616 if (rate->mcs >= 32)
1617 return 0;
1619 modulation = rate->mcs & 7;
1620 streams = (rate->mcs >> 3) + 1;
1622 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1623 13500000 : 6500000;
1625 if (modulation < 4)
1626 bitrate *= (modulation + 1);
1627 else if (modulation == 4)
1628 bitrate *= (modulation + 2);
1629 else
1630 bitrate *= (modulation + 3);
1632 bitrate *= streams;
1634 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1635 bitrate = (bitrate / 9) * 10;
1637 /* do NOT round down here */
1638 return (bitrate + 50000) / 100000;
1641 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1642 int flags, struct net_device *dev,
1643 u8 *mac_addr, struct station_info *sinfo)
1645 void *hdr;
1646 struct nlattr *sinfoattr, *txrate;
1647 u16 bitrate;
1649 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1650 if (!hdr)
1651 return -1;
1653 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1654 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1656 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1657 if (!sinfoattr)
1658 goto nla_put_failure;
1659 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1660 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1661 sinfo->inactive_time);
1662 if (sinfo->filled & STATION_INFO_RX_BYTES)
1663 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1664 sinfo->rx_bytes);
1665 if (sinfo->filled & STATION_INFO_TX_BYTES)
1666 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1667 sinfo->tx_bytes);
1668 if (sinfo->filled & STATION_INFO_LLID)
1669 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1670 sinfo->llid);
1671 if (sinfo->filled & STATION_INFO_PLID)
1672 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1673 sinfo->plid);
1674 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1675 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1676 sinfo->plink_state);
1677 if (sinfo->filled & STATION_INFO_SIGNAL)
1678 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1679 sinfo->signal);
1680 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1681 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1682 if (!txrate)
1683 goto nla_put_failure;
1685 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1686 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1687 if (bitrate > 0)
1688 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1690 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1691 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1692 sinfo->txrate.mcs);
1693 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1694 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1695 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1696 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1698 nla_nest_end(msg, txrate);
1700 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1701 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1702 sinfo->rx_packets);
1703 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1704 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1705 sinfo->tx_packets);
1706 nla_nest_end(msg, sinfoattr);
1708 return genlmsg_end(msg, hdr);
1710 nla_put_failure:
1711 genlmsg_cancel(msg, hdr);
1712 return -EMSGSIZE;
1715 static int nl80211_dump_station(struct sk_buff *skb,
1716 struct netlink_callback *cb)
1718 struct station_info sinfo;
1719 struct cfg80211_registered_device *dev;
1720 struct net_device *netdev;
1721 u8 mac_addr[ETH_ALEN];
1722 int ifidx = cb->args[0];
1723 int sta_idx = cb->args[1];
1724 int err;
1726 if (!ifidx) {
1727 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1728 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1729 nl80211_policy);
1730 if (err)
1731 return err;
1733 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1734 return -EINVAL;
1736 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1737 if (!ifidx)
1738 return -EINVAL;
1741 rtnl_lock();
1743 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1744 if (!netdev) {
1745 err = -ENODEV;
1746 goto out_rtnl;
1749 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
1750 if (IS_ERR(dev)) {
1751 err = PTR_ERR(dev);
1752 goto out_rtnl;
1755 if (!dev->ops->dump_station) {
1756 err = -EOPNOTSUPP;
1757 goto out_err;
1760 while (1) {
1761 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1762 mac_addr, &sinfo);
1763 if (err == -ENOENT)
1764 break;
1765 if (err)
1766 goto out_err;
1768 if (nl80211_send_station(skb,
1769 NETLINK_CB(cb->skb).pid,
1770 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1771 netdev, mac_addr,
1772 &sinfo) < 0)
1773 goto out;
1775 sta_idx++;
1779 out:
1780 cb->args[1] = sta_idx;
1781 err = skb->len;
1782 out_err:
1783 cfg80211_unlock_rdev(dev);
1784 out_rtnl:
1785 rtnl_unlock();
1787 return err;
1790 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1792 struct cfg80211_registered_device *rdev;
1793 int err;
1794 struct net_device *dev;
1795 struct station_info sinfo;
1796 struct sk_buff *msg;
1797 u8 *mac_addr = NULL;
1799 memset(&sinfo, 0, sizeof(sinfo));
1801 if (!info->attrs[NL80211_ATTR_MAC])
1802 return -EINVAL;
1804 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1806 rtnl_lock();
1808 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1809 if (err)
1810 goto out_rtnl;
1812 if (!rdev->ops->get_station) {
1813 err = -EOPNOTSUPP;
1814 goto out;
1817 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1818 if (err)
1819 goto out;
1821 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1822 if (!msg)
1823 goto out;
1825 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1826 dev, mac_addr, &sinfo) < 0)
1827 goto out_free;
1829 err = genlmsg_reply(msg, info);
1830 goto out;
1832 out_free:
1833 nlmsg_free(msg);
1834 out:
1835 cfg80211_unlock_rdev(rdev);
1836 dev_put(dev);
1837 out_rtnl:
1838 rtnl_unlock();
1840 return err;
1844 * Get vlan interface making sure it is on the right wiphy.
1846 static int get_vlan(struct genl_info *info,
1847 struct cfg80211_registered_device *rdev,
1848 struct net_device **vlan)
1850 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
1851 *vlan = NULL;
1853 if (vlanattr) {
1854 *vlan = dev_get_by_index(genl_info_net(info),
1855 nla_get_u32(vlanattr));
1856 if (!*vlan)
1857 return -ENODEV;
1858 if (!(*vlan)->ieee80211_ptr)
1859 return -EINVAL;
1860 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1861 return -EINVAL;
1863 return 0;
1866 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1868 struct cfg80211_registered_device *rdev;
1869 int err;
1870 struct net_device *dev;
1871 struct station_parameters params;
1872 u8 *mac_addr = NULL;
1874 memset(&params, 0, sizeof(params));
1876 params.listen_interval = -1;
1878 if (info->attrs[NL80211_ATTR_STA_AID])
1879 return -EINVAL;
1881 if (!info->attrs[NL80211_ATTR_MAC])
1882 return -EINVAL;
1884 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1886 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1887 params.supported_rates =
1888 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1889 params.supported_rates_len =
1890 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1893 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1894 params.listen_interval =
1895 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1897 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1898 params.ht_capa =
1899 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1901 if (parse_station_flags(info, &params))
1902 return -EINVAL;
1904 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1905 params.plink_action =
1906 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1908 rtnl_lock();
1910 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1911 if (err)
1912 goto out_rtnl;
1914 err = get_vlan(info, rdev, &params.vlan);
1915 if (err)
1916 goto out;
1918 /* validate settings */
1919 err = 0;
1921 switch (dev->ieee80211_ptr->iftype) {
1922 case NL80211_IFTYPE_AP:
1923 case NL80211_IFTYPE_AP_VLAN:
1924 /* disallow mesh-specific things */
1925 if (params.plink_action)
1926 err = -EINVAL;
1927 break;
1928 case NL80211_IFTYPE_STATION:
1929 /* disallow everything but AUTHORIZED flag */
1930 if (params.plink_action)
1931 err = -EINVAL;
1932 if (params.vlan)
1933 err = -EINVAL;
1934 if (params.supported_rates)
1935 err = -EINVAL;
1936 if (params.ht_capa)
1937 err = -EINVAL;
1938 if (params.listen_interval >= 0)
1939 err = -EINVAL;
1940 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1941 err = -EINVAL;
1942 break;
1943 case NL80211_IFTYPE_MESH_POINT:
1944 /* disallow things mesh doesn't support */
1945 if (params.vlan)
1946 err = -EINVAL;
1947 if (params.ht_capa)
1948 err = -EINVAL;
1949 if (params.listen_interval >= 0)
1950 err = -EINVAL;
1951 if (params.supported_rates)
1952 err = -EINVAL;
1953 if (params.sta_flags_mask)
1954 err = -EINVAL;
1955 break;
1956 default:
1957 err = -EINVAL;
1960 if (err)
1961 goto out;
1963 if (!rdev->ops->change_station) {
1964 err = -EOPNOTSUPP;
1965 goto out;
1968 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
1970 out:
1971 if (params.vlan)
1972 dev_put(params.vlan);
1973 cfg80211_unlock_rdev(rdev);
1974 dev_put(dev);
1975 out_rtnl:
1976 rtnl_unlock();
1978 return err;
1981 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1983 struct cfg80211_registered_device *rdev;
1984 int err;
1985 struct net_device *dev;
1986 struct station_parameters params;
1987 u8 *mac_addr = NULL;
1989 memset(&params, 0, sizeof(params));
1991 if (!info->attrs[NL80211_ATTR_MAC])
1992 return -EINVAL;
1994 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1995 return -EINVAL;
1997 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1998 return -EINVAL;
2000 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2001 params.supported_rates =
2002 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2003 params.supported_rates_len =
2004 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2005 params.listen_interval =
2006 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2008 if (info->attrs[NL80211_ATTR_STA_AID]) {
2009 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2010 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2011 return -EINVAL;
2014 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2015 params.ht_capa =
2016 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2018 if (parse_station_flags(info, &params))
2019 return -EINVAL;
2021 rtnl_lock();
2023 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2024 if (err)
2025 goto out_rtnl;
2027 err = get_vlan(info, rdev, &params.vlan);
2028 if (err)
2029 goto out;
2031 /* validate settings */
2032 err = 0;
2034 switch (dev->ieee80211_ptr->iftype) {
2035 case NL80211_IFTYPE_AP:
2036 case NL80211_IFTYPE_AP_VLAN:
2037 /* all ok but must have AID */
2038 if (!params.aid)
2039 err = -EINVAL;
2040 break;
2041 case NL80211_IFTYPE_MESH_POINT:
2042 /* disallow things mesh doesn't support */
2043 if (params.vlan)
2044 err = -EINVAL;
2045 if (params.aid)
2046 err = -EINVAL;
2047 if (params.ht_capa)
2048 err = -EINVAL;
2049 if (params.listen_interval >= 0)
2050 err = -EINVAL;
2051 if (params.supported_rates)
2052 err = -EINVAL;
2053 if (params.sta_flags_mask)
2054 err = -EINVAL;
2055 break;
2056 default:
2057 err = -EINVAL;
2060 if (err)
2061 goto out;
2063 if (!rdev->ops->add_station) {
2064 err = -EOPNOTSUPP;
2065 goto out;
2068 if (!netif_running(dev)) {
2069 err = -ENETDOWN;
2070 goto out;
2073 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2075 out:
2076 if (params.vlan)
2077 dev_put(params.vlan);
2078 cfg80211_unlock_rdev(rdev);
2079 dev_put(dev);
2080 out_rtnl:
2081 rtnl_unlock();
2083 return err;
2086 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2088 struct cfg80211_registered_device *rdev;
2089 int err;
2090 struct net_device *dev;
2091 u8 *mac_addr = NULL;
2093 if (info->attrs[NL80211_ATTR_MAC])
2094 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2096 rtnl_lock();
2098 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2099 if (err)
2100 goto out_rtnl;
2102 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2103 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2104 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2105 err = -EINVAL;
2106 goto out;
2109 if (!rdev->ops->del_station) {
2110 err = -EOPNOTSUPP;
2111 goto out;
2114 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2116 out:
2117 cfg80211_unlock_rdev(rdev);
2118 dev_put(dev);
2119 out_rtnl:
2120 rtnl_unlock();
2122 return err;
2125 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2126 int flags, struct net_device *dev,
2127 u8 *dst, u8 *next_hop,
2128 struct mpath_info *pinfo)
2130 void *hdr;
2131 struct nlattr *pinfoattr;
2133 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2134 if (!hdr)
2135 return -1;
2137 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2138 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2139 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2141 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2142 if (!pinfoattr)
2143 goto nla_put_failure;
2144 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2145 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2146 pinfo->frame_qlen);
2147 if (pinfo->filled & MPATH_INFO_DSN)
2148 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
2149 pinfo->dsn);
2150 if (pinfo->filled & MPATH_INFO_METRIC)
2151 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2152 pinfo->metric);
2153 if (pinfo->filled & MPATH_INFO_EXPTIME)
2154 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2155 pinfo->exptime);
2156 if (pinfo->filled & MPATH_INFO_FLAGS)
2157 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2158 pinfo->flags);
2159 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2160 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2161 pinfo->discovery_timeout);
2162 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2163 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2164 pinfo->discovery_retries);
2166 nla_nest_end(msg, pinfoattr);
2168 return genlmsg_end(msg, hdr);
2170 nla_put_failure:
2171 genlmsg_cancel(msg, hdr);
2172 return -EMSGSIZE;
2175 static int nl80211_dump_mpath(struct sk_buff *skb,
2176 struct netlink_callback *cb)
2178 struct mpath_info pinfo;
2179 struct cfg80211_registered_device *dev;
2180 struct net_device *netdev;
2181 u8 dst[ETH_ALEN];
2182 u8 next_hop[ETH_ALEN];
2183 int ifidx = cb->args[0];
2184 int path_idx = cb->args[1];
2185 int err;
2187 if (!ifidx) {
2188 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2189 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2190 nl80211_policy);
2191 if (err)
2192 return err;
2194 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2195 return -EINVAL;
2197 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2198 if (!ifidx)
2199 return -EINVAL;
2202 rtnl_lock();
2204 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2205 if (!netdev) {
2206 err = -ENODEV;
2207 goto out_rtnl;
2210 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
2211 if (IS_ERR(dev)) {
2212 err = PTR_ERR(dev);
2213 goto out_rtnl;
2216 if (!dev->ops->dump_mpath) {
2217 err = -EOPNOTSUPP;
2218 goto out_err;
2221 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2222 err = -EOPNOTSUPP;
2223 goto out;
2226 while (1) {
2227 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2228 dst, next_hop, &pinfo);
2229 if (err == -ENOENT)
2230 break;
2231 if (err)
2232 goto out_err;
2234 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2235 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2236 netdev, dst, next_hop,
2237 &pinfo) < 0)
2238 goto out;
2240 path_idx++;
2244 out:
2245 cb->args[1] = path_idx;
2246 err = skb->len;
2247 out_err:
2248 cfg80211_unlock_rdev(dev);
2249 out_rtnl:
2250 rtnl_unlock();
2252 return err;
2255 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2257 struct cfg80211_registered_device *rdev;
2258 int err;
2259 struct net_device *dev;
2260 struct mpath_info pinfo;
2261 struct sk_buff *msg;
2262 u8 *dst = NULL;
2263 u8 next_hop[ETH_ALEN];
2265 memset(&pinfo, 0, sizeof(pinfo));
2267 if (!info->attrs[NL80211_ATTR_MAC])
2268 return -EINVAL;
2270 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2272 rtnl_lock();
2274 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2275 if (err)
2276 goto out_rtnl;
2278 if (!rdev->ops->get_mpath) {
2279 err = -EOPNOTSUPP;
2280 goto out;
2283 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2284 err = -EOPNOTSUPP;
2285 goto out;
2288 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2289 if (err)
2290 goto out;
2292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2293 if (!msg)
2294 goto out;
2296 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2297 dev, dst, next_hop, &pinfo) < 0)
2298 goto out_free;
2300 err = genlmsg_reply(msg, info);
2301 goto out;
2303 out_free:
2304 nlmsg_free(msg);
2305 out:
2306 cfg80211_unlock_rdev(rdev);
2307 dev_put(dev);
2308 out_rtnl:
2309 rtnl_unlock();
2311 return err;
2314 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2316 struct cfg80211_registered_device *rdev;
2317 int err;
2318 struct net_device *dev;
2319 u8 *dst = NULL;
2320 u8 *next_hop = NULL;
2322 if (!info->attrs[NL80211_ATTR_MAC])
2323 return -EINVAL;
2325 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2326 return -EINVAL;
2328 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2329 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2331 rtnl_lock();
2333 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2334 if (err)
2335 goto out_rtnl;
2337 if (!rdev->ops->change_mpath) {
2338 err = -EOPNOTSUPP;
2339 goto out;
2342 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2343 err = -EOPNOTSUPP;
2344 goto out;
2347 if (!netif_running(dev)) {
2348 err = -ENETDOWN;
2349 goto out;
2352 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2354 out:
2355 cfg80211_unlock_rdev(rdev);
2356 dev_put(dev);
2357 out_rtnl:
2358 rtnl_unlock();
2360 return err;
2362 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2364 struct cfg80211_registered_device *rdev;
2365 int err;
2366 struct net_device *dev;
2367 u8 *dst = NULL;
2368 u8 *next_hop = NULL;
2370 if (!info->attrs[NL80211_ATTR_MAC])
2371 return -EINVAL;
2373 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2374 return -EINVAL;
2376 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2377 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2379 rtnl_lock();
2381 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2382 if (err)
2383 goto out_rtnl;
2385 if (!rdev->ops->add_mpath) {
2386 err = -EOPNOTSUPP;
2387 goto out;
2390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2391 err = -EOPNOTSUPP;
2392 goto out;
2395 if (!netif_running(dev)) {
2396 err = -ENETDOWN;
2397 goto out;
2400 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2402 out:
2403 cfg80211_unlock_rdev(rdev);
2404 dev_put(dev);
2405 out_rtnl:
2406 rtnl_unlock();
2408 return err;
2411 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2413 struct cfg80211_registered_device *rdev;
2414 int err;
2415 struct net_device *dev;
2416 u8 *dst = NULL;
2418 if (info->attrs[NL80211_ATTR_MAC])
2419 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2421 rtnl_lock();
2423 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2424 if (err)
2425 goto out_rtnl;
2427 if (!rdev->ops->del_mpath) {
2428 err = -EOPNOTSUPP;
2429 goto out;
2432 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2434 out:
2435 cfg80211_unlock_rdev(rdev);
2436 dev_put(dev);
2437 out_rtnl:
2438 rtnl_unlock();
2440 return err;
2443 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2445 struct cfg80211_registered_device *rdev;
2446 int err;
2447 struct net_device *dev;
2448 struct bss_parameters params;
2450 memset(&params, 0, sizeof(params));
2451 /* default to not changing parameters */
2452 params.use_cts_prot = -1;
2453 params.use_short_preamble = -1;
2454 params.use_short_slot_time = -1;
2456 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2457 params.use_cts_prot =
2458 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2459 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2460 params.use_short_preamble =
2461 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2462 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2463 params.use_short_slot_time =
2464 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2465 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2466 params.basic_rates =
2467 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2468 params.basic_rates_len =
2469 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2472 rtnl_lock();
2474 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2475 if (err)
2476 goto out_rtnl;
2478 if (!rdev->ops->change_bss) {
2479 err = -EOPNOTSUPP;
2480 goto out;
2483 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2484 err = -EOPNOTSUPP;
2485 goto out;
2488 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2490 out:
2491 cfg80211_unlock_rdev(rdev);
2492 dev_put(dev);
2493 out_rtnl:
2494 rtnl_unlock();
2496 return err;
2499 static const struct nla_policy
2500 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2501 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2502 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2503 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2504 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2505 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2506 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2509 static int parse_reg_rule(struct nlattr *tb[],
2510 struct ieee80211_reg_rule *reg_rule)
2512 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2513 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2515 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2516 return -EINVAL;
2517 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2518 return -EINVAL;
2519 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2520 return -EINVAL;
2521 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2522 return -EINVAL;
2523 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2524 return -EINVAL;
2526 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2528 freq_range->start_freq_khz =
2529 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2530 freq_range->end_freq_khz =
2531 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2532 freq_range->max_bandwidth_khz =
2533 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2535 power_rule->max_eirp =
2536 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2538 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2539 power_rule->max_antenna_gain =
2540 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2542 return 0;
2545 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2547 int r;
2548 char *data = NULL;
2551 * You should only get this when cfg80211 hasn't yet initialized
2552 * completely when built-in to the kernel right between the time
2553 * window between nl80211_init() and regulatory_init(), if that is
2554 * even possible.
2556 mutex_lock(&cfg80211_mutex);
2557 if (unlikely(!cfg80211_regdomain)) {
2558 mutex_unlock(&cfg80211_mutex);
2559 return -EINPROGRESS;
2561 mutex_unlock(&cfg80211_mutex);
2563 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2564 return -EINVAL;
2566 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2568 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2569 /* We ignore world regdom requests with the old regdom setup */
2570 if (is_world_regdom(data))
2571 return -EINVAL;
2572 #endif
2574 r = regulatory_hint_user(data);
2576 return r;
2579 static int nl80211_get_mesh_params(struct sk_buff *skb,
2580 struct genl_info *info)
2582 struct cfg80211_registered_device *rdev;
2583 struct mesh_config cur_params;
2584 int err;
2585 struct net_device *dev;
2586 void *hdr;
2587 struct nlattr *pinfoattr;
2588 struct sk_buff *msg;
2590 rtnl_lock();
2592 /* Look up our device */
2593 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2594 if (err)
2595 goto out_rtnl;
2597 if (!rdev->ops->get_mesh_params) {
2598 err = -EOPNOTSUPP;
2599 goto out;
2602 /* Get the mesh params */
2603 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2604 if (err)
2605 goto out;
2607 /* Draw up a netlink message to send back */
2608 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2609 if (!msg) {
2610 err = -ENOBUFS;
2611 goto out;
2613 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2614 NL80211_CMD_GET_MESH_PARAMS);
2615 if (!hdr)
2616 goto nla_put_failure;
2617 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2618 if (!pinfoattr)
2619 goto nla_put_failure;
2620 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2621 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2622 cur_params.dot11MeshRetryTimeout);
2623 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2624 cur_params.dot11MeshConfirmTimeout);
2625 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2626 cur_params.dot11MeshHoldingTimeout);
2627 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2628 cur_params.dot11MeshMaxPeerLinks);
2629 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2630 cur_params.dot11MeshMaxRetries);
2631 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2632 cur_params.dot11MeshTTL);
2633 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2634 cur_params.auto_open_plinks);
2635 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2636 cur_params.dot11MeshHWMPmaxPREQretries);
2637 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2638 cur_params.path_refresh_time);
2639 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2640 cur_params.min_discovery_timeout);
2641 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2642 cur_params.dot11MeshHWMPactivePathTimeout);
2643 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2644 cur_params.dot11MeshHWMPpreqMinInterval);
2645 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2646 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2647 nla_nest_end(msg, pinfoattr);
2648 genlmsg_end(msg, hdr);
2649 err = genlmsg_reply(msg, info);
2650 goto out;
2652 nla_put_failure:
2653 genlmsg_cancel(msg, hdr);
2654 err = -EMSGSIZE;
2655 out:
2656 /* Cleanup */
2657 cfg80211_unlock_rdev(rdev);
2658 dev_put(dev);
2659 out_rtnl:
2660 rtnl_unlock();
2662 return err;
2665 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2666 do {\
2667 if (table[attr_num]) {\
2668 cfg.param = nla_fn(table[attr_num]); \
2669 mask |= (1 << (attr_num - 1)); \
2671 } while (0);\
2673 static struct nla_policy
2674 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2675 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2676 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2677 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2678 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2679 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2680 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2681 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2683 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2684 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2685 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2686 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2687 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2688 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2691 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2693 int err;
2694 u32 mask;
2695 struct cfg80211_registered_device *rdev;
2696 struct net_device *dev;
2697 struct mesh_config cfg;
2698 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2699 struct nlattr *parent_attr;
2701 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2702 if (!parent_attr)
2703 return -EINVAL;
2704 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2705 parent_attr, nl80211_meshconf_params_policy))
2706 return -EINVAL;
2708 rtnl_lock();
2710 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2711 if (err)
2712 goto out_rtnl;
2714 if (!rdev->ops->set_mesh_params) {
2715 err = -EOPNOTSUPP;
2716 goto out;
2719 /* This makes sure that there aren't more than 32 mesh config
2720 * parameters (otherwise our bitfield scheme would not work.) */
2721 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2723 /* Fill in the params struct */
2724 mask = 0;
2725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2726 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2727 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2728 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2730 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2732 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2733 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2734 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2736 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2737 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2738 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2740 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2741 nla_get_u8);
2742 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2743 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2744 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2745 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2746 nla_get_u16);
2747 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2748 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2749 nla_get_u32);
2750 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2751 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2752 nla_get_u16);
2753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2754 dot11MeshHWMPnetDiameterTraversalTime,
2755 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2756 nla_get_u16);
2758 /* Apply changes */
2759 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2761 out:
2762 /* cleanup */
2763 cfg80211_unlock_rdev(rdev);
2764 dev_put(dev);
2765 out_rtnl:
2766 rtnl_unlock();
2768 return err;
2771 #undef FILL_IN_MESH_PARAM_IF_SET
2773 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2775 struct sk_buff *msg;
2776 void *hdr = NULL;
2777 struct nlattr *nl_reg_rules;
2778 unsigned int i;
2779 int err = -EINVAL;
2781 mutex_lock(&cfg80211_mutex);
2783 if (!cfg80211_regdomain)
2784 goto out;
2786 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2787 if (!msg) {
2788 err = -ENOBUFS;
2789 goto out;
2792 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2793 NL80211_CMD_GET_REG);
2794 if (!hdr)
2795 goto nla_put_failure;
2797 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2798 cfg80211_regdomain->alpha2);
2800 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2801 if (!nl_reg_rules)
2802 goto nla_put_failure;
2804 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2805 struct nlattr *nl_reg_rule;
2806 const struct ieee80211_reg_rule *reg_rule;
2807 const struct ieee80211_freq_range *freq_range;
2808 const struct ieee80211_power_rule *power_rule;
2810 reg_rule = &cfg80211_regdomain->reg_rules[i];
2811 freq_range = &reg_rule->freq_range;
2812 power_rule = &reg_rule->power_rule;
2814 nl_reg_rule = nla_nest_start(msg, i);
2815 if (!nl_reg_rule)
2816 goto nla_put_failure;
2818 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2819 reg_rule->flags);
2820 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2821 freq_range->start_freq_khz);
2822 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2823 freq_range->end_freq_khz);
2824 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2825 freq_range->max_bandwidth_khz);
2826 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2827 power_rule->max_antenna_gain);
2828 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2829 power_rule->max_eirp);
2831 nla_nest_end(msg, nl_reg_rule);
2834 nla_nest_end(msg, nl_reg_rules);
2836 genlmsg_end(msg, hdr);
2837 err = genlmsg_reply(msg, info);
2838 goto out;
2840 nla_put_failure:
2841 genlmsg_cancel(msg, hdr);
2842 err = -EMSGSIZE;
2843 out:
2844 mutex_unlock(&cfg80211_mutex);
2845 return err;
2848 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2850 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2851 struct nlattr *nl_reg_rule;
2852 char *alpha2 = NULL;
2853 int rem_reg_rules = 0, r = 0;
2854 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2855 struct ieee80211_regdomain *rd = NULL;
2857 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2858 return -EINVAL;
2860 if (!info->attrs[NL80211_ATTR_REG_RULES])
2861 return -EINVAL;
2863 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2865 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2866 rem_reg_rules) {
2867 num_rules++;
2868 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2869 return -EINVAL;
2872 mutex_lock(&cfg80211_mutex);
2874 if (!reg_is_valid_request(alpha2)) {
2875 r = -EINVAL;
2876 goto bad_reg;
2879 size_of_regd = sizeof(struct ieee80211_regdomain) +
2880 (num_rules * sizeof(struct ieee80211_reg_rule));
2882 rd = kzalloc(size_of_regd, GFP_KERNEL);
2883 if (!rd) {
2884 r = -ENOMEM;
2885 goto bad_reg;
2888 rd->n_reg_rules = num_rules;
2889 rd->alpha2[0] = alpha2[0];
2890 rd->alpha2[1] = alpha2[1];
2892 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2893 rem_reg_rules) {
2894 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2895 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2896 reg_rule_policy);
2897 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2898 if (r)
2899 goto bad_reg;
2901 rule_idx++;
2903 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2904 r = -EINVAL;
2905 goto bad_reg;
2909 BUG_ON(rule_idx != num_rules);
2911 r = set_regdom(rd);
2913 mutex_unlock(&cfg80211_mutex);
2915 return r;
2917 bad_reg:
2918 mutex_unlock(&cfg80211_mutex);
2919 kfree(rd);
2920 return r;
2923 static int validate_scan_freqs(struct nlattr *freqs)
2925 struct nlattr *attr1, *attr2;
2926 int n_channels = 0, tmp1, tmp2;
2928 nla_for_each_nested(attr1, freqs, tmp1) {
2929 n_channels++;
2931 * Some hardware has a limited channel list for
2932 * scanning, and it is pretty much nonsensical
2933 * to scan for a channel twice, so disallow that
2934 * and don't require drivers to check that the
2935 * channel list they get isn't longer than what
2936 * they can scan, as long as they can scan all
2937 * the channels they registered at once.
2939 nla_for_each_nested(attr2, freqs, tmp2)
2940 if (attr1 != attr2 &&
2941 nla_get_u32(attr1) == nla_get_u32(attr2))
2942 return 0;
2945 return n_channels;
2948 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2950 struct cfg80211_registered_device *rdev;
2951 struct net_device *dev;
2952 struct cfg80211_scan_request *request;
2953 struct cfg80211_ssid *ssid;
2954 struct ieee80211_channel *channel;
2955 struct nlattr *attr;
2956 struct wiphy *wiphy;
2957 int err, tmp, n_ssids = 0, n_channels, i;
2958 enum ieee80211_band band;
2959 size_t ie_len;
2961 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2962 return -EINVAL;
2964 rtnl_lock();
2966 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2967 if (err)
2968 goto out_rtnl;
2970 wiphy = &rdev->wiphy;
2972 if (!rdev->ops->scan) {
2973 err = -EOPNOTSUPP;
2974 goto out;
2977 if (!netif_running(dev)) {
2978 err = -ENETDOWN;
2979 goto out;
2982 if (rdev->scan_req) {
2983 err = -EBUSY;
2984 goto out;
2987 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2988 n_channels = validate_scan_freqs(
2989 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
2990 if (!n_channels) {
2991 err = -EINVAL;
2992 goto out;
2994 } else {
2995 n_channels = 0;
2997 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2998 if (wiphy->bands[band])
2999 n_channels += wiphy->bands[band]->n_channels;
3002 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3003 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3004 n_ssids++;
3006 if (n_ssids > wiphy->max_scan_ssids) {
3007 err = -EINVAL;
3008 goto out;
3011 if (info->attrs[NL80211_ATTR_IE])
3012 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3013 else
3014 ie_len = 0;
3016 if (ie_len > wiphy->max_scan_ie_len) {
3017 err = -EINVAL;
3018 goto out;
3021 request = kzalloc(sizeof(*request)
3022 + sizeof(*ssid) * n_ssids
3023 + sizeof(channel) * n_channels
3024 + ie_len, GFP_KERNEL);
3025 if (!request) {
3026 err = -ENOMEM;
3027 goto out;
3030 request->channels = (void *)((char *)request + sizeof(*request));
3031 request->n_channels = n_channels;
3032 if (n_ssids)
3033 request->ssids = (void *)(request->channels + n_channels);
3034 request->n_ssids = n_ssids;
3035 if (ie_len) {
3036 if (request->ssids)
3037 request->ie = (void *)(request->ssids + n_ssids);
3038 else
3039 request->ie = (void *)(request->channels + n_channels);
3042 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3043 /* user specified, bail out if channel not found */
3044 request->n_channels = n_channels;
3045 i = 0;
3046 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3047 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3048 if (!request->channels[i]) {
3049 err = -EINVAL;
3050 goto out_free;
3052 i++;
3054 } else {
3055 /* all channels */
3056 i = 0;
3057 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3058 int j;
3059 if (!wiphy->bands[band])
3060 continue;
3061 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3062 request->channels[i] = &wiphy->bands[band]->channels[j];
3063 i++;
3068 i = 0;
3069 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3070 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3071 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3072 err = -EINVAL;
3073 goto out_free;
3075 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3076 request->ssids[i].ssid_len = nla_len(attr);
3077 i++;
3081 if (info->attrs[NL80211_ATTR_IE]) {
3082 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3083 memcpy((void *)request->ie,
3084 nla_data(info->attrs[NL80211_ATTR_IE]),
3085 request->ie_len);
3088 request->dev = dev;
3089 request->wiphy = &rdev->wiphy;
3091 rdev->scan_req = request;
3092 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3094 if (!err) {
3095 nl80211_send_scan_start(rdev, dev);
3096 dev_hold(dev);
3099 out_free:
3100 if (err) {
3101 rdev->scan_req = NULL;
3102 kfree(request);
3104 out:
3105 cfg80211_unlock_rdev(rdev);
3106 dev_put(dev);
3107 out_rtnl:
3108 rtnl_unlock();
3110 return err;
3113 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3114 struct cfg80211_registered_device *rdev,
3115 struct wireless_dev *wdev,
3116 struct cfg80211_internal_bss *intbss)
3118 struct cfg80211_bss *res = &intbss->pub;
3119 void *hdr;
3120 struct nlattr *bss;
3121 int i;
3123 ASSERT_WDEV_LOCK(wdev);
3125 hdr = nl80211hdr_put(msg, pid, seq, flags,
3126 NL80211_CMD_NEW_SCAN_RESULTS);
3127 if (!hdr)
3128 return -1;
3130 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
3131 rdev->bss_generation);
3132 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3134 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3135 if (!bss)
3136 goto nla_put_failure;
3137 if (!is_zero_ether_addr(res->bssid))
3138 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3139 if (res->information_elements && res->len_information_elements)
3140 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3141 res->len_information_elements,
3142 res->information_elements);
3143 if (res->tsf)
3144 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3145 if (res->beacon_interval)
3146 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3147 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3148 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3150 switch (rdev->wiphy.signal_type) {
3151 case CFG80211_SIGNAL_TYPE_MBM:
3152 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3153 break;
3154 case CFG80211_SIGNAL_TYPE_UNSPEC:
3155 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3156 break;
3157 default:
3158 break;
3161 switch (wdev->iftype) {
3162 case NL80211_IFTYPE_STATION:
3163 if (intbss == wdev->current_bss)
3164 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3165 NL80211_BSS_STATUS_ASSOCIATED);
3166 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3167 if (intbss != wdev->auth_bsses[i])
3168 continue;
3169 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3170 NL80211_BSS_STATUS_AUTHENTICATED);
3171 break;
3173 break;
3174 case NL80211_IFTYPE_ADHOC:
3175 if (intbss == wdev->current_bss)
3176 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3177 NL80211_BSS_STATUS_IBSS_JOINED);
3178 break;
3179 default:
3180 break;
3183 nla_nest_end(msg, bss);
3185 return genlmsg_end(msg, hdr);
3187 nla_put_failure:
3188 genlmsg_cancel(msg, hdr);
3189 return -EMSGSIZE;
3192 static int nl80211_dump_scan(struct sk_buff *skb,
3193 struct netlink_callback *cb)
3195 struct cfg80211_registered_device *rdev;
3196 struct net_device *dev;
3197 struct cfg80211_internal_bss *scan;
3198 struct wireless_dev *wdev;
3199 int ifidx = cb->args[0];
3200 int start = cb->args[1], idx = 0;
3201 int err;
3203 if (!ifidx) {
3204 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
3205 nl80211_fam.attrbuf, nl80211_fam.maxattr,
3206 nl80211_policy);
3207 if (err)
3208 return err;
3210 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
3211 return -EINVAL;
3213 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
3214 if (!ifidx)
3215 return -EINVAL;
3216 cb->args[0] = ifidx;
3219 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
3220 if (!dev)
3221 return -ENODEV;
3223 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3224 if (IS_ERR(rdev)) {
3225 err = PTR_ERR(rdev);
3226 goto out_put_netdev;
3229 wdev = dev->ieee80211_ptr;
3231 wdev_lock(wdev);
3232 spin_lock_bh(&rdev->bss_lock);
3233 cfg80211_bss_expire(rdev);
3235 list_for_each_entry(scan, &rdev->bss_list, list) {
3236 if (++idx <= start)
3237 continue;
3238 if (nl80211_send_bss(skb,
3239 NETLINK_CB(cb->skb).pid,
3240 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3241 rdev, wdev, scan) < 0) {
3242 idx--;
3243 goto out;
3247 out:
3248 spin_unlock_bh(&rdev->bss_lock);
3249 wdev_unlock(wdev);
3251 cb->args[1] = idx;
3252 err = skb->len;
3253 cfg80211_unlock_rdev(rdev);
3254 out_put_netdev:
3255 dev_put(dev);
3257 return err;
3260 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3262 return auth_type <= NL80211_AUTHTYPE_MAX;
3265 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3267 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3268 NL80211_WPA_VERSION_2));
3271 static bool nl80211_valid_akm_suite(u32 akm)
3273 return akm == WLAN_AKM_SUITE_8021X ||
3274 akm == WLAN_AKM_SUITE_PSK;
3277 static bool nl80211_valid_cipher_suite(u32 cipher)
3279 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3280 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3281 cipher == WLAN_CIPHER_SUITE_TKIP ||
3282 cipher == WLAN_CIPHER_SUITE_CCMP ||
3283 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3287 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3289 struct cfg80211_registered_device *rdev;
3290 struct net_device *dev;
3291 struct ieee80211_channel *chan;
3292 const u8 *bssid, *ssid, *ie = NULL;
3293 int err, ssid_len, ie_len = 0;
3294 enum nl80211_auth_type auth_type;
3295 struct key_parse key;
3297 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3298 return -EINVAL;
3300 if (!info->attrs[NL80211_ATTR_MAC])
3301 return -EINVAL;
3303 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3304 return -EINVAL;
3306 if (!info->attrs[NL80211_ATTR_SSID])
3307 return -EINVAL;
3309 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3310 return -EINVAL;
3312 err = nl80211_parse_key(info, &key);
3313 if (err)
3314 return err;
3316 if (key.idx >= 0) {
3317 if (!key.p.key || !key.p.key_len)
3318 return -EINVAL;
3319 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3320 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3321 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3322 key.p.key_len != WLAN_KEY_LEN_WEP104))
3323 return -EINVAL;
3324 if (key.idx > 4)
3325 return -EINVAL;
3326 } else {
3327 key.p.key_len = 0;
3328 key.p.key = NULL;
3331 rtnl_lock();
3333 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3334 if (err)
3335 goto unlock_rtnl;
3337 if (!rdev->ops->auth) {
3338 err = -EOPNOTSUPP;
3339 goto out;
3342 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3343 err = -EOPNOTSUPP;
3344 goto out;
3347 if (!netif_running(dev)) {
3348 err = -ENETDOWN;
3349 goto out;
3352 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3353 chan = ieee80211_get_channel(&rdev->wiphy,
3354 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3355 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3356 err = -EINVAL;
3357 goto out;
3360 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3361 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3363 if (info->attrs[NL80211_ATTR_IE]) {
3364 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3365 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3368 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3369 if (!nl80211_valid_auth_type(auth_type)) {
3370 err = -EINVAL;
3371 goto out;
3374 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3375 ssid, ssid_len, ie, ie_len,
3376 key.p.key, key.p.key_len, key.idx);
3378 out:
3379 cfg80211_unlock_rdev(rdev);
3380 dev_put(dev);
3381 unlock_rtnl:
3382 rtnl_unlock();
3383 return err;
3386 static int nl80211_crypto_settings(struct genl_info *info,
3387 struct cfg80211_crypto_settings *settings,
3388 int cipher_limit)
3390 memset(settings, 0, sizeof(*settings));
3392 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3394 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3395 void *data;
3396 int len, i;
3398 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3399 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3400 settings->n_ciphers_pairwise = len / sizeof(u32);
3402 if (len % sizeof(u32))
3403 return -EINVAL;
3405 if (settings->n_ciphers_pairwise > cipher_limit)
3406 return -EINVAL;
3408 memcpy(settings->ciphers_pairwise, data, len);
3410 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3411 if (!nl80211_valid_cipher_suite(
3412 settings->ciphers_pairwise[i]))
3413 return -EINVAL;
3416 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3417 settings->cipher_group =
3418 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3419 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3420 return -EINVAL;
3423 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3424 settings->wpa_versions =
3425 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3426 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3427 return -EINVAL;
3430 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3431 void *data;
3432 int len, i;
3434 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3435 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3436 settings->n_akm_suites = len / sizeof(u32);
3438 if (len % sizeof(u32))
3439 return -EINVAL;
3441 memcpy(settings->akm_suites, data, len);
3443 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3444 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3445 return -EINVAL;
3448 return 0;
3451 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3453 struct cfg80211_registered_device *rdev;
3454 struct net_device *dev;
3455 struct cfg80211_crypto_settings crypto;
3456 struct ieee80211_channel *chan;
3457 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
3458 int err, ssid_len, ie_len = 0;
3459 bool use_mfp = false;
3461 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3462 return -EINVAL;
3464 if (!info->attrs[NL80211_ATTR_MAC] ||
3465 !info->attrs[NL80211_ATTR_SSID] ||
3466 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
3467 return -EINVAL;
3469 rtnl_lock();
3471 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3472 if (err)
3473 goto unlock_rtnl;
3475 if (!rdev->ops->assoc) {
3476 err = -EOPNOTSUPP;
3477 goto out;
3480 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3481 err = -EOPNOTSUPP;
3482 goto out;
3485 if (!netif_running(dev)) {
3486 err = -ENETDOWN;
3487 goto out;
3490 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3492 chan = ieee80211_get_channel(&rdev->wiphy,
3493 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3494 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3495 err = -EINVAL;
3496 goto out;
3499 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3500 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3502 if (info->attrs[NL80211_ATTR_IE]) {
3503 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3504 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3507 if (info->attrs[NL80211_ATTR_USE_MFP]) {
3508 enum nl80211_mfp mfp =
3509 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3510 if (mfp == NL80211_MFP_REQUIRED)
3511 use_mfp = true;
3512 else if (mfp != NL80211_MFP_NO) {
3513 err = -EINVAL;
3514 goto out;
3518 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3519 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3521 err = nl80211_crypto_settings(info, &crypto, 1);
3522 if (!err)
3523 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3524 ssid, ssid_len, ie, ie_len, use_mfp,
3525 &crypto);
3527 out:
3528 cfg80211_unlock_rdev(rdev);
3529 dev_put(dev);
3530 unlock_rtnl:
3531 rtnl_unlock();
3532 return err;
3535 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3537 struct cfg80211_registered_device *rdev;
3538 struct net_device *dev;
3539 const u8 *ie = NULL, *bssid;
3540 int err, ie_len = 0;
3541 u16 reason_code;
3543 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3544 return -EINVAL;
3546 if (!info->attrs[NL80211_ATTR_MAC])
3547 return -EINVAL;
3549 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3550 return -EINVAL;
3552 rtnl_lock();
3554 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3555 if (err)
3556 goto unlock_rtnl;
3558 if (!rdev->ops->deauth) {
3559 err = -EOPNOTSUPP;
3560 goto out;
3563 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3564 err = -EOPNOTSUPP;
3565 goto out;
3568 if (!netif_running(dev)) {
3569 err = -ENETDOWN;
3570 goto out;
3573 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3575 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3576 if (reason_code == 0) {
3577 /* Reason Code 0 is reserved */
3578 err = -EINVAL;
3579 goto out;
3582 if (info->attrs[NL80211_ATTR_IE]) {
3583 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3584 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3587 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
3589 out:
3590 cfg80211_unlock_rdev(rdev);
3591 dev_put(dev);
3592 unlock_rtnl:
3593 rtnl_unlock();
3594 return err;
3597 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3599 struct cfg80211_registered_device *rdev;
3600 struct net_device *dev;
3601 const u8 *ie = NULL, *bssid;
3602 int err, ie_len = 0;
3603 u16 reason_code;
3605 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3606 return -EINVAL;
3608 if (!info->attrs[NL80211_ATTR_MAC])
3609 return -EINVAL;
3611 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3612 return -EINVAL;
3614 rtnl_lock();
3616 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3617 if (err)
3618 goto unlock_rtnl;
3620 if (!rdev->ops->disassoc) {
3621 err = -EOPNOTSUPP;
3622 goto out;
3625 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3626 err = -EOPNOTSUPP;
3627 goto out;
3630 if (!netif_running(dev)) {
3631 err = -ENETDOWN;
3632 goto out;
3635 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3637 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3638 if (reason_code == 0) {
3639 /* Reason Code 0 is reserved */
3640 err = -EINVAL;
3641 goto out;
3644 if (info->attrs[NL80211_ATTR_IE]) {
3645 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3646 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3649 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
3651 out:
3652 cfg80211_unlock_rdev(rdev);
3653 dev_put(dev);
3654 unlock_rtnl:
3655 rtnl_unlock();
3656 return err;
3659 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3661 struct cfg80211_registered_device *rdev;
3662 struct net_device *dev;
3663 struct cfg80211_ibss_params ibss;
3664 struct wiphy *wiphy;
3665 struct cfg80211_cached_keys *connkeys = NULL;
3666 int err;
3668 memset(&ibss, 0, sizeof(ibss));
3670 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3671 return -EINVAL;
3673 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3674 !info->attrs[NL80211_ATTR_SSID] ||
3675 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3676 return -EINVAL;
3678 ibss.beacon_interval = 100;
3680 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3681 ibss.beacon_interval =
3682 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3683 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3684 return -EINVAL;
3687 rtnl_lock();
3689 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3690 if (err)
3691 goto unlock_rtnl;
3693 if (!rdev->ops->join_ibss) {
3694 err = -EOPNOTSUPP;
3695 goto out;
3698 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3699 err = -EOPNOTSUPP;
3700 goto out;
3703 if (!netif_running(dev)) {
3704 err = -ENETDOWN;
3705 goto out;
3708 wiphy = &rdev->wiphy;
3710 if (info->attrs[NL80211_ATTR_MAC])
3711 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3712 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3713 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3715 if (info->attrs[NL80211_ATTR_IE]) {
3716 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3717 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3720 ibss.channel = ieee80211_get_channel(wiphy,
3721 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3722 if (!ibss.channel ||
3723 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3724 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3725 err = -EINVAL;
3726 goto out;
3729 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3730 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3732 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3733 connkeys = nl80211_parse_connkeys(rdev,
3734 info->attrs[NL80211_ATTR_KEYS]);
3735 if (IS_ERR(connkeys)) {
3736 err = PTR_ERR(connkeys);
3737 connkeys = NULL;
3738 goto out;
3742 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
3744 out:
3745 cfg80211_unlock_rdev(rdev);
3746 dev_put(dev);
3747 unlock_rtnl:
3748 if (err)
3749 kfree(connkeys);
3750 rtnl_unlock();
3751 return err;
3754 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3756 struct cfg80211_registered_device *rdev;
3757 struct net_device *dev;
3758 int err;
3760 rtnl_lock();
3762 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3763 if (err)
3764 goto unlock_rtnl;
3766 if (!rdev->ops->leave_ibss) {
3767 err = -EOPNOTSUPP;
3768 goto out;
3771 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3772 err = -EOPNOTSUPP;
3773 goto out;
3776 if (!netif_running(dev)) {
3777 err = -ENETDOWN;
3778 goto out;
3781 err = cfg80211_leave_ibss(rdev, dev, false);
3783 out:
3784 cfg80211_unlock_rdev(rdev);
3785 dev_put(dev);
3786 unlock_rtnl:
3787 rtnl_unlock();
3788 return err;
3791 #ifdef CONFIG_NL80211_TESTMODE
3792 static struct genl_multicast_group nl80211_testmode_mcgrp = {
3793 .name = "testmode",
3796 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3798 struct cfg80211_registered_device *rdev;
3799 int err;
3801 if (!info->attrs[NL80211_ATTR_TESTDATA])
3802 return -EINVAL;
3804 rtnl_lock();
3806 rdev = cfg80211_get_dev_from_info(info);
3807 if (IS_ERR(rdev)) {
3808 err = PTR_ERR(rdev);
3809 goto unlock_rtnl;
3812 err = -EOPNOTSUPP;
3813 if (rdev->ops->testmode_cmd) {
3814 rdev->testmode_info = info;
3815 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3816 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3817 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3818 rdev->testmode_info = NULL;
3821 cfg80211_unlock_rdev(rdev);
3823 unlock_rtnl:
3824 rtnl_unlock();
3825 return err;
3828 static struct sk_buff *
3829 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3830 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3832 struct sk_buff *skb;
3833 void *hdr;
3834 struct nlattr *data;
3836 skb = nlmsg_new(approxlen + 100, gfp);
3837 if (!skb)
3838 return NULL;
3840 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3841 if (!hdr) {
3842 kfree_skb(skb);
3843 return NULL;
3846 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3847 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3849 ((void **)skb->cb)[0] = rdev;
3850 ((void **)skb->cb)[1] = hdr;
3851 ((void **)skb->cb)[2] = data;
3853 return skb;
3855 nla_put_failure:
3856 kfree_skb(skb);
3857 return NULL;
3860 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3861 int approxlen)
3863 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3865 if (WARN_ON(!rdev->testmode_info))
3866 return NULL;
3868 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3869 rdev->testmode_info->snd_pid,
3870 rdev->testmode_info->snd_seq,
3871 GFP_KERNEL);
3873 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3875 int cfg80211_testmode_reply(struct sk_buff *skb)
3877 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3878 void *hdr = ((void **)skb->cb)[1];
3879 struct nlattr *data = ((void **)skb->cb)[2];
3881 if (WARN_ON(!rdev->testmode_info)) {
3882 kfree_skb(skb);
3883 return -EINVAL;
3886 nla_nest_end(skb, data);
3887 genlmsg_end(skb, hdr);
3888 return genlmsg_reply(skb, rdev->testmode_info);
3890 EXPORT_SYMBOL(cfg80211_testmode_reply);
3892 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3893 int approxlen, gfp_t gfp)
3895 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3897 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3899 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3901 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3903 void *hdr = ((void **)skb->cb)[1];
3904 struct nlattr *data = ((void **)skb->cb)[2];
3906 nla_nest_end(skb, data);
3907 genlmsg_end(skb, hdr);
3908 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3910 EXPORT_SYMBOL(cfg80211_testmode_event);
3911 #endif
3913 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3915 struct cfg80211_registered_device *rdev;
3916 struct net_device *dev;
3917 struct cfg80211_connect_params connect;
3918 struct wiphy *wiphy;
3919 struct cfg80211_cached_keys *connkeys = NULL;
3920 int err;
3922 memset(&connect, 0, sizeof(connect));
3924 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3925 return -EINVAL;
3927 if (!info->attrs[NL80211_ATTR_SSID] ||
3928 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3929 return -EINVAL;
3931 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3932 connect.auth_type =
3933 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3934 if (!nl80211_valid_auth_type(connect.auth_type))
3935 return -EINVAL;
3936 } else
3937 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3939 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3941 err = nl80211_crypto_settings(info, &connect.crypto,
3942 NL80211_MAX_NR_CIPHER_SUITES);
3943 if (err)
3944 return err;
3945 rtnl_lock();
3947 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3948 if (err)
3949 goto unlock_rtnl;
3951 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3952 err = -EOPNOTSUPP;
3953 goto out;
3956 if (!netif_running(dev)) {
3957 err = -ENETDOWN;
3958 goto out;
3961 wiphy = &rdev->wiphy;
3963 if (info->attrs[NL80211_ATTR_MAC])
3964 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3965 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3966 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3968 if (info->attrs[NL80211_ATTR_IE]) {
3969 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3970 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3973 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3974 connect.channel =
3975 ieee80211_get_channel(wiphy,
3976 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3977 if (!connect.channel ||
3978 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
3979 err = -EINVAL;
3980 goto out;
3984 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3985 connkeys = nl80211_parse_connkeys(rdev,
3986 info->attrs[NL80211_ATTR_KEYS]);
3987 if (IS_ERR(connkeys)) {
3988 err = PTR_ERR(connkeys);
3989 connkeys = NULL;
3990 goto out;
3994 err = cfg80211_connect(rdev, dev, &connect, connkeys);
3996 out:
3997 cfg80211_unlock_rdev(rdev);
3998 dev_put(dev);
3999 unlock_rtnl:
4000 if (err)
4001 kfree(connkeys);
4002 rtnl_unlock();
4003 return err;
4006 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4008 struct cfg80211_registered_device *rdev;
4009 struct net_device *dev;
4010 int err;
4011 u16 reason;
4013 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4014 reason = WLAN_REASON_DEAUTH_LEAVING;
4015 else
4016 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4018 if (reason == 0)
4019 return -EINVAL;
4021 rtnl_lock();
4023 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4024 if (err)
4025 goto unlock_rtnl;
4027 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4028 err = -EOPNOTSUPP;
4029 goto out;
4032 if (!netif_running(dev)) {
4033 err = -ENETDOWN;
4034 goto out;
4037 err = cfg80211_disconnect(rdev, dev, reason, true);
4039 out:
4040 cfg80211_unlock_rdev(rdev);
4041 dev_put(dev);
4042 unlock_rtnl:
4043 rtnl_unlock();
4044 return err;
4047 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4049 struct cfg80211_registered_device *rdev;
4050 struct net *net;
4051 int err;
4052 u32 pid;
4054 if (!info->attrs[NL80211_ATTR_PID])
4055 return -EINVAL;
4057 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4059 rtnl_lock();
4061 rdev = cfg80211_get_dev_from_info(info);
4062 if (IS_ERR(rdev)) {
4063 err = PTR_ERR(rdev);
4064 goto out;
4067 net = get_net_ns_by_pid(pid);
4068 if (IS_ERR(net)) {
4069 err = PTR_ERR(net);
4070 goto out;
4073 err = 0;
4075 /* check if anything to do */
4076 if (net_eq(wiphy_net(&rdev->wiphy), net))
4077 goto out_put_net;
4079 err = cfg80211_switch_netns(rdev, net);
4080 out_put_net:
4081 put_net(net);
4082 out:
4083 cfg80211_unlock_rdev(rdev);
4084 rtnl_unlock();
4085 return err;
4088 static struct genl_ops nl80211_ops[] = {
4090 .cmd = NL80211_CMD_GET_WIPHY,
4091 .doit = nl80211_get_wiphy,
4092 .dumpit = nl80211_dump_wiphy,
4093 .policy = nl80211_policy,
4094 /* can be retrieved by unprivileged users */
4097 .cmd = NL80211_CMD_SET_WIPHY,
4098 .doit = nl80211_set_wiphy,
4099 .policy = nl80211_policy,
4100 .flags = GENL_ADMIN_PERM,
4103 .cmd = NL80211_CMD_GET_INTERFACE,
4104 .doit = nl80211_get_interface,
4105 .dumpit = nl80211_dump_interface,
4106 .policy = nl80211_policy,
4107 /* can be retrieved by unprivileged users */
4110 .cmd = NL80211_CMD_SET_INTERFACE,
4111 .doit = nl80211_set_interface,
4112 .policy = nl80211_policy,
4113 .flags = GENL_ADMIN_PERM,
4116 .cmd = NL80211_CMD_NEW_INTERFACE,
4117 .doit = nl80211_new_interface,
4118 .policy = nl80211_policy,
4119 .flags = GENL_ADMIN_PERM,
4122 .cmd = NL80211_CMD_DEL_INTERFACE,
4123 .doit = nl80211_del_interface,
4124 .policy = nl80211_policy,
4125 .flags = GENL_ADMIN_PERM,
4128 .cmd = NL80211_CMD_GET_KEY,
4129 .doit = nl80211_get_key,
4130 .policy = nl80211_policy,
4131 .flags = GENL_ADMIN_PERM,
4134 .cmd = NL80211_CMD_SET_KEY,
4135 .doit = nl80211_set_key,
4136 .policy = nl80211_policy,
4137 .flags = GENL_ADMIN_PERM,
4140 .cmd = NL80211_CMD_NEW_KEY,
4141 .doit = nl80211_new_key,
4142 .policy = nl80211_policy,
4143 .flags = GENL_ADMIN_PERM,
4146 .cmd = NL80211_CMD_DEL_KEY,
4147 .doit = nl80211_del_key,
4148 .policy = nl80211_policy,
4149 .flags = GENL_ADMIN_PERM,
4152 .cmd = NL80211_CMD_SET_BEACON,
4153 .policy = nl80211_policy,
4154 .flags = GENL_ADMIN_PERM,
4155 .doit = nl80211_addset_beacon,
4158 .cmd = NL80211_CMD_NEW_BEACON,
4159 .policy = nl80211_policy,
4160 .flags = GENL_ADMIN_PERM,
4161 .doit = nl80211_addset_beacon,
4164 .cmd = NL80211_CMD_DEL_BEACON,
4165 .policy = nl80211_policy,
4166 .flags = GENL_ADMIN_PERM,
4167 .doit = nl80211_del_beacon,
4170 .cmd = NL80211_CMD_GET_STATION,
4171 .doit = nl80211_get_station,
4172 .dumpit = nl80211_dump_station,
4173 .policy = nl80211_policy,
4176 .cmd = NL80211_CMD_SET_STATION,
4177 .doit = nl80211_set_station,
4178 .policy = nl80211_policy,
4179 .flags = GENL_ADMIN_PERM,
4182 .cmd = NL80211_CMD_NEW_STATION,
4183 .doit = nl80211_new_station,
4184 .policy = nl80211_policy,
4185 .flags = GENL_ADMIN_PERM,
4188 .cmd = NL80211_CMD_DEL_STATION,
4189 .doit = nl80211_del_station,
4190 .policy = nl80211_policy,
4191 .flags = GENL_ADMIN_PERM,
4194 .cmd = NL80211_CMD_GET_MPATH,
4195 .doit = nl80211_get_mpath,
4196 .dumpit = nl80211_dump_mpath,
4197 .policy = nl80211_policy,
4198 .flags = GENL_ADMIN_PERM,
4201 .cmd = NL80211_CMD_SET_MPATH,
4202 .doit = nl80211_set_mpath,
4203 .policy = nl80211_policy,
4204 .flags = GENL_ADMIN_PERM,
4207 .cmd = NL80211_CMD_NEW_MPATH,
4208 .doit = nl80211_new_mpath,
4209 .policy = nl80211_policy,
4210 .flags = GENL_ADMIN_PERM,
4213 .cmd = NL80211_CMD_DEL_MPATH,
4214 .doit = nl80211_del_mpath,
4215 .policy = nl80211_policy,
4216 .flags = GENL_ADMIN_PERM,
4219 .cmd = NL80211_CMD_SET_BSS,
4220 .doit = nl80211_set_bss,
4221 .policy = nl80211_policy,
4222 .flags = GENL_ADMIN_PERM,
4225 .cmd = NL80211_CMD_GET_REG,
4226 .doit = nl80211_get_reg,
4227 .policy = nl80211_policy,
4228 /* can be retrieved by unprivileged users */
4231 .cmd = NL80211_CMD_SET_REG,
4232 .doit = nl80211_set_reg,
4233 .policy = nl80211_policy,
4234 .flags = GENL_ADMIN_PERM,
4237 .cmd = NL80211_CMD_REQ_SET_REG,
4238 .doit = nl80211_req_set_reg,
4239 .policy = nl80211_policy,
4240 .flags = GENL_ADMIN_PERM,
4243 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4244 .doit = nl80211_get_mesh_params,
4245 .policy = nl80211_policy,
4246 /* can be retrieved by unprivileged users */
4249 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4250 .doit = nl80211_set_mesh_params,
4251 .policy = nl80211_policy,
4252 .flags = GENL_ADMIN_PERM,
4255 .cmd = NL80211_CMD_TRIGGER_SCAN,
4256 .doit = nl80211_trigger_scan,
4257 .policy = nl80211_policy,
4258 .flags = GENL_ADMIN_PERM,
4261 .cmd = NL80211_CMD_GET_SCAN,
4262 .policy = nl80211_policy,
4263 .dumpit = nl80211_dump_scan,
4266 .cmd = NL80211_CMD_AUTHENTICATE,
4267 .doit = nl80211_authenticate,
4268 .policy = nl80211_policy,
4269 .flags = GENL_ADMIN_PERM,
4272 .cmd = NL80211_CMD_ASSOCIATE,
4273 .doit = nl80211_associate,
4274 .policy = nl80211_policy,
4275 .flags = GENL_ADMIN_PERM,
4278 .cmd = NL80211_CMD_DEAUTHENTICATE,
4279 .doit = nl80211_deauthenticate,
4280 .policy = nl80211_policy,
4281 .flags = GENL_ADMIN_PERM,
4284 .cmd = NL80211_CMD_DISASSOCIATE,
4285 .doit = nl80211_disassociate,
4286 .policy = nl80211_policy,
4287 .flags = GENL_ADMIN_PERM,
4290 .cmd = NL80211_CMD_JOIN_IBSS,
4291 .doit = nl80211_join_ibss,
4292 .policy = nl80211_policy,
4293 .flags = GENL_ADMIN_PERM,
4296 .cmd = NL80211_CMD_LEAVE_IBSS,
4297 .doit = nl80211_leave_ibss,
4298 .policy = nl80211_policy,
4299 .flags = GENL_ADMIN_PERM,
4301 #ifdef CONFIG_NL80211_TESTMODE
4303 .cmd = NL80211_CMD_TESTMODE,
4304 .doit = nl80211_testmode_do,
4305 .policy = nl80211_policy,
4306 .flags = GENL_ADMIN_PERM,
4308 #endif
4310 .cmd = NL80211_CMD_CONNECT,
4311 .doit = nl80211_connect,
4312 .policy = nl80211_policy,
4313 .flags = GENL_ADMIN_PERM,
4316 .cmd = NL80211_CMD_DISCONNECT,
4317 .doit = nl80211_disconnect,
4318 .policy = nl80211_policy,
4319 .flags = GENL_ADMIN_PERM,
4322 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4323 .doit = nl80211_wiphy_netns,
4324 .policy = nl80211_policy,
4325 .flags = GENL_ADMIN_PERM,
4328 static struct genl_multicast_group nl80211_mlme_mcgrp = {
4329 .name = "mlme",
4332 /* multicast groups */
4333 static struct genl_multicast_group nl80211_config_mcgrp = {
4334 .name = "config",
4336 static struct genl_multicast_group nl80211_scan_mcgrp = {
4337 .name = "scan",
4339 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4340 .name = "regulatory",
4343 /* notification functions */
4345 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4347 struct sk_buff *msg;
4349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4350 if (!msg)
4351 return;
4353 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4354 nlmsg_free(msg);
4355 return;
4358 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4359 nl80211_config_mcgrp.id, GFP_KERNEL);
4362 static int nl80211_add_scan_req(struct sk_buff *msg,
4363 struct cfg80211_registered_device *rdev)
4365 struct cfg80211_scan_request *req = rdev->scan_req;
4366 struct nlattr *nest;
4367 int i;
4369 ASSERT_RDEV_LOCK(rdev);
4371 if (WARN_ON(!req))
4372 return 0;
4374 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4375 if (!nest)
4376 goto nla_put_failure;
4377 for (i = 0; i < req->n_ssids; i++)
4378 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4379 nla_nest_end(msg, nest);
4381 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4382 if (!nest)
4383 goto nla_put_failure;
4384 for (i = 0; i < req->n_channels; i++)
4385 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4386 nla_nest_end(msg, nest);
4388 if (req->ie)
4389 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4391 return 0;
4392 nla_put_failure:
4393 return -ENOBUFS;
4396 static int nl80211_send_scan_msg(struct sk_buff *msg,
4397 struct cfg80211_registered_device *rdev,
4398 struct net_device *netdev,
4399 u32 pid, u32 seq, int flags,
4400 u32 cmd)
4402 void *hdr;
4404 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4405 if (!hdr)
4406 return -1;
4408 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4409 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4411 /* ignore errors and send incomplete event anyway */
4412 nl80211_add_scan_req(msg, rdev);
4414 return genlmsg_end(msg, hdr);
4416 nla_put_failure:
4417 genlmsg_cancel(msg, hdr);
4418 return -EMSGSIZE;
4421 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4422 struct net_device *netdev)
4424 struct sk_buff *msg;
4426 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4427 if (!msg)
4428 return;
4430 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4431 NL80211_CMD_TRIGGER_SCAN) < 0) {
4432 nlmsg_free(msg);
4433 return;
4436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4437 nl80211_scan_mcgrp.id, GFP_KERNEL);
4440 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4441 struct net_device *netdev)
4443 struct sk_buff *msg;
4445 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4446 if (!msg)
4447 return;
4449 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4450 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
4451 nlmsg_free(msg);
4452 return;
4455 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4456 nl80211_scan_mcgrp.id, GFP_KERNEL);
4459 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4460 struct net_device *netdev)
4462 struct sk_buff *msg;
4464 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4465 if (!msg)
4466 return;
4468 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4469 NL80211_CMD_SCAN_ABORTED) < 0) {
4470 nlmsg_free(msg);
4471 return;
4474 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4475 nl80211_scan_mcgrp.id, GFP_KERNEL);
4479 * This can happen on global regulatory changes or device specific settings
4480 * based on custom world regulatory domains.
4482 void nl80211_send_reg_change_event(struct regulatory_request *request)
4484 struct sk_buff *msg;
4485 void *hdr;
4487 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4488 if (!msg)
4489 return;
4491 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4492 if (!hdr) {
4493 nlmsg_free(msg);
4494 return;
4497 /* Userspace can always count this one always being set */
4498 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4500 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4501 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4502 NL80211_REGDOM_TYPE_WORLD);
4503 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4504 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4505 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4506 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4507 request->intersect)
4508 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4509 NL80211_REGDOM_TYPE_INTERSECTION);
4510 else {
4511 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4512 NL80211_REGDOM_TYPE_COUNTRY);
4513 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4516 if (wiphy_idx_valid(request->wiphy_idx))
4517 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4519 if (genlmsg_end(msg, hdr) < 0) {
4520 nlmsg_free(msg);
4521 return;
4524 rcu_read_lock();
4525 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
4526 GFP_ATOMIC);
4527 rcu_read_unlock();
4529 return;
4531 nla_put_failure:
4532 genlmsg_cancel(msg, hdr);
4533 nlmsg_free(msg);
4536 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4537 struct net_device *netdev,
4538 const u8 *buf, size_t len,
4539 enum nl80211_commands cmd, gfp_t gfp)
4541 struct sk_buff *msg;
4542 void *hdr;
4544 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
4545 if (!msg)
4546 return;
4548 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4549 if (!hdr) {
4550 nlmsg_free(msg);
4551 return;
4554 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4555 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4556 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4558 if (genlmsg_end(msg, hdr) < 0) {
4559 nlmsg_free(msg);
4560 return;
4563 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4564 nl80211_mlme_mcgrp.id, gfp);
4565 return;
4567 nla_put_failure:
4568 genlmsg_cancel(msg, hdr);
4569 nlmsg_free(msg);
4572 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
4573 struct net_device *netdev, const u8 *buf,
4574 size_t len, gfp_t gfp)
4576 nl80211_send_mlme_event(rdev, netdev, buf, len,
4577 NL80211_CMD_AUTHENTICATE, gfp);
4580 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4581 struct net_device *netdev, const u8 *buf,
4582 size_t len, gfp_t gfp)
4584 nl80211_send_mlme_event(rdev, netdev, buf, len,
4585 NL80211_CMD_ASSOCIATE, gfp);
4588 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
4589 struct net_device *netdev, const u8 *buf,
4590 size_t len, gfp_t gfp)
4592 nl80211_send_mlme_event(rdev, netdev, buf, len,
4593 NL80211_CMD_DEAUTHENTICATE, gfp);
4596 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4597 struct net_device *netdev, const u8 *buf,
4598 size_t len, gfp_t gfp)
4600 nl80211_send_mlme_event(rdev, netdev, buf, len,
4601 NL80211_CMD_DISASSOCIATE, gfp);
4604 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4605 struct net_device *netdev, int cmd,
4606 const u8 *addr, gfp_t gfp)
4608 struct sk_buff *msg;
4609 void *hdr;
4611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
4612 if (!msg)
4613 return;
4615 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4616 if (!hdr) {
4617 nlmsg_free(msg);
4618 return;
4621 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4622 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4623 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4624 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4626 if (genlmsg_end(msg, hdr) < 0) {
4627 nlmsg_free(msg);
4628 return;
4631 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4632 nl80211_mlme_mcgrp.id, gfp);
4633 return;
4635 nla_put_failure:
4636 genlmsg_cancel(msg, hdr);
4637 nlmsg_free(msg);
4640 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
4641 struct net_device *netdev, const u8 *addr,
4642 gfp_t gfp)
4644 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
4645 addr, gfp);
4648 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
4649 struct net_device *netdev, const u8 *addr,
4650 gfp_t gfp)
4652 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4653 addr, gfp);
4656 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4657 struct net_device *netdev, const u8 *bssid,
4658 const u8 *req_ie, size_t req_ie_len,
4659 const u8 *resp_ie, size_t resp_ie_len,
4660 u16 status, gfp_t gfp)
4662 struct sk_buff *msg;
4663 void *hdr;
4665 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4666 if (!msg)
4667 return;
4669 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4670 if (!hdr) {
4671 nlmsg_free(msg);
4672 return;
4675 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4676 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4677 if (bssid)
4678 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4679 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4680 if (req_ie)
4681 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4682 if (resp_ie)
4683 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4685 if (genlmsg_end(msg, hdr) < 0) {
4686 nlmsg_free(msg);
4687 return;
4690 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4691 nl80211_mlme_mcgrp.id, gfp);
4692 return;
4694 nla_put_failure:
4695 genlmsg_cancel(msg, hdr);
4696 nlmsg_free(msg);
4700 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4701 struct net_device *netdev, const u8 *bssid,
4702 const u8 *req_ie, size_t req_ie_len,
4703 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4705 struct sk_buff *msg;
4706 void *hdr;
4708 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4709 if (!msg)
4710 return;
4712 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4713 if (!hdr) {
4714 nlmsg_free(msg);
4715 return;
4718 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4719 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4720 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4721 if (req_ie)
4722 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4723 if (resp_ie)
4724 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4726 if (genlmsg_end(msg, hdr) < 0) {
4727 nlmsg_free(msg);
4728 return;
4731 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4732 nl80211_mlme_mcgrp.id, gfp);
4733 return;
4735 nla_put_failure:
4736 genlmsg_cancel(msg, hdr);
4737 nlmsg_free(msg);
4741 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
4742 struct net_device *netdev, u16 reason,
4743 const u8 *ie, size_t ie_len, bool from_ap)
4745 struct sk_buff *msg;
4746 void *hdr;
4748 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4749 if (!msg)
4750 return;
4752 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
4753 if (!hdr) {
4754 nlmsg_free(msg);
4755 return;
4758 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4759 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4760 if (from_ap && reason)
4761 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
4762 if (from_ap)
4763 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
4764 if (ie)
4765 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
4767 if (genlmsg_end(msg, hdr) < 0) {
4768 nlmsg_free(msg);
4769 return;
4772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4773 nl80211_mlme_mcgrp.id, GFP_KERNEL);
4774 return;
4776 nla_put_failure:
4777 genlmsg_cancel(msg, hdr);
4778 nlmsg_free(msg);
4782 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
4783 struct net_device *netdev, const u8 *bssid,
4784 gfp_t gfp)
4786 struct sk_buff *msg;
4787 void *hdr;
4789 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
4790 if (!msg)
4791 return;
4793 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
4794 if (!hdr) {
4795 nlmsg_free(msg);
4796 return;
4799 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4800 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4801 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4803 if (genlmsg_end(msg, hdr) < 0) {
4804 nlmsg_free(msg);
4805 return;
4808 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4809 nl80211_mlme_mcgrp.id, gfp);
4810 return;
4812 nla_put_failure:
4813 genlmsg_cancel(msg, hdr);
4814 nlmsg_free(msg);
4817 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
4818 struct net_device *netdev, const u8 *addr,
4819 enum nl80211_key_type key_type, int key_id,
4820 const u8 *tsc, gfp_t gfp)
4822 struct sk_buff *msg;
4823 void *hdr;
4825 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
4826 if (!msg)
4827 return;
4829 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
4830 if (!hdr) {
4831 nlmsg_free(msg);
4832 return;
4835 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4836 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4837 if (addr)
4838 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4839 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
4840 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
4841 if (tsc)
4842 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
4844 if (genlmsg_end(msg, hdr) < 0) {
4845 nlmsg_free(msg);
4846 return;
4849 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4850 nl80211_mlme_mcgrp.id, gfp);
4851 return;
4853 nla_put_failure:
4854 genlmsg_cancel(msg, hdr);
4855 nlmsg_free(msg);
4858 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
4859 struct ieee80211_channel *channel_before,
4860 struct ieee80211_channel *channel_after)
4862 struct sk_buff *msg;
4863 void *hdr;
4864 struct nlattr *nl_freq;
4866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
4867 if (!msg)
4868 return;
4870 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
4871 if (!hdr) {
4872 nlmsg_free(msg);
4873 return;
4877 * Since we are applying the beacon hint to a wiphy we know its
4878 * wiphy_idx is valid
4880 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
4882 /* Before */
4883 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
4884 if (!nl_freq)
4885 goto nla_put_failure;
4886 if (nl80211_msg_put_channel(msg, channel_before))
4887 goto nla_put_failure;
4888 nla_nest_end(msg, nl_freq);
4890 /* After */
4891 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4892 if (!nl_freq)
4893 goto nla_put_failure;
4894 if (nl80211_msg_put_channel(msg, channel_after))
4895 goto nla_put_failure;
4896 nla_nest_end(msg, nl_freq);
4898 if (genlmsg_end(msg, hdr) < 0) {
4899 nlmsg_free(msg);
4900 return;
4903 rcu_read_lock();
4904 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
4905 GFP_ATOMIC);
4906 rcu_read_unlock();
4908 return;
4910 nla_put_failure:
4911 genlmsg_cancel(msg, hdr);
4912 nlmsg_free(msg);
4915 /* initialisation/exit functions */
4917 int nl80211_init(void)
4919 int err;
4921 err = genl_register_family_with_ops(&nl80211_fam,
4922 nl80211_ops, ARRAY_SIZE(nl80211_ops));
4923 if (err)
4924 return err;
4926 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4927 if (err)
4928 goto err_out;
4930 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4931 if (err)
4932 goto err_out;
4934 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4935 if (err)
4936 goto err_out;
4938 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4939 if (err)
4940 goto err_out;
4942 #ifdef CONFIG_NL80211_TESTMODE
4943 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
4944 if (err)
4945 goto err_out;
4946 #endif
4948 return 0;
4949 err_out:
4950 genl_unregister_family(&nl80211_fam);
4951 return err;
4954 void nl80211_exit(void)
4956 genl_unregister_family(&nl80211_fam);