inet_diag: fix inet_diag_bc_audit()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wireless / nl80211.c
blobd1f58e5250f689caa68f3f3fba002a88b928a675
1 /*
2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
21 #include <net/sock.h>
22 #include "core.h"
23 #include "nl80211.h"
24 #include "reg.h"
26 /* the netlink family */
27 static struct genl_family nl80211_fam = {
28 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
29 .name = "nl80211", /* have users key off the name instead */
30 .hdrsize = 0, /* no private header */
31 .version = 1, /* no particular meaning now */
32 .maxattr = NL80211_ATTR_MAX,
33 .netnsok = true,
36 /* internal helper: get rdev and dev */
37 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
38 struct cfg80211_registered_device **rdev,
39 struct net_device **dev)
41 struct nlattr **attrs = info->attrs;
42 int ifindex;
44 if (!attrs[NL80211_ATTR_IFINDEX])
45 return -EINVAL;
47 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
48 *dev = dev_get_by_index(genl_info_net(info), ifindex);
49 if (!*dev)
50 return -ENODEV;
52 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
53 if (IS_ERR(*rdev)) {
54 dev_put(*dev);
55 return PTR_ERR(*rdev);
58 return 0;
61 /* policy for the attributes */
62 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
63 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
64 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
65 .len = 20-1 },
66 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
67 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
68 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
69 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
71 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
72 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
73 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
75 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
77 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
79 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
80 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
82 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
83 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
84 .len = WLAN_MAX_KEY_LEN },
85 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
86 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
87 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
88 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
90 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
91 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
92 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
94 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
95 .len = IEEE80211_MAX_DATA_LEN },
96 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
98 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
99 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
100 .len = NL80211_MAX_SUPP_RATES },
101 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
102 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
103 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
104 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
105 .len = IEEE80211_MAX_MESH_ID_LEN },
106 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
108 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
109 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
111 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
113 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
114 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
115 .len = NL80211_MAX_SUPP_RATES },
117 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
119 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
120 .len = NL80211_HT_CAPABILITY_LEN },
122 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
123 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
124 .len = IEEE80211_MAX_DATA_LEN },
125 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
126 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
128 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
129 .len = IEEE80211_MAX_SSID_LEN },
130 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
131 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
132 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
133 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
134 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
135 [NL80211_ATTR_STA_FLAGS2] = {
136 .len = sizeof(struct nl80211_sta_flag_update),
138 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
139 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
140 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
141 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
142 [NL80211_ATTR_PID] = { .type = NLA_U32 },
143 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
144 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
145 .len = WLAN_PMKID_LEN },
146 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
147 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
148 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
149 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
150 .len = IEEE80211_MAX_DATA_LEN },
151 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
152 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
153 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
154 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
155 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
158 /* policy for the attributes */
159 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
160 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
161 [NL80211_KEY_IDX] = { .type = NLA_U8 },
162 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
163 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
164 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
165 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
168 /* ifidx get helper */
169 static int nl80211_get_ifidx(struct netlink_callback *cb)
171 int res;
173 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
174 nl80211_fam.attrbuf, nl80211_fam.maxattr,
175 nl80211_policy);
176 if (res)
177 return res;
179 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
180 return -EINVAL;
182 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
183 if (!res)
184 return -EINVAL;
185 return res;
188 /* IE validation */
189 static bool is_valid_ie_attr(const struct nlattr *attr)
191 const u8 *pos;
192 int len;
194 if (!attr)
195 return true;
197 pos = nla_data(attr);
198 len = nla_len(attr);
200 while (len) {
201 u8 elemlen;
203 if (len < 2)
204 return false;
205 len -= 2;
207 elemlen = pos[1];
208 if (elemlen > len)
209 return false;
211 len -= elemlen;
212 pos += 2 + elemlen;
215 return true;
218 /* message building helper */
219 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
220 int flags, u8 cmd)
222 /* since there is no private header just add the generic one */
223 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
226 static int nl80211_msg_put_channel(struct sk_buff *msg,
227 struct ieee80211_channel *chan)
229 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
230 chan->center_freq);
232 if (chan->flags & IEEE80211_CHAN_DISABLED)
233 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
234 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
235 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
236 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
237 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
238 if (chan->flags & IEEE80211_CHAN_RADAR)
239 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
241 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
242 DBM_TO_MBM(chan->max_power));
244 return 0;
246 nla_put_failure:
247 return -ENOBUFS;
250 /* netlink command implementations */
252 struct key_parse {
253 struct key_params p;
254 int idx;
255 bool def, defmgmt;
258 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
260 struct nlattr *tb[NL80211_KEY_MAX + 1];
261 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
262 nl80211_key_policy);
263 if (err)
264 return err;
266 k->def = !!tb[NL80211_KEY_DEFAULT];
267 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
269 if (tb[NL80211_KEY_IDX])
270 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
272 if (tb[NL80211_KEY_DATA]) {
273 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
274 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
277 if (tb[NL80211_KEY_SEQ]) {
278 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
279 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
282 if (tb[NL80211_KEY_CIPHER])
283 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
285 return 0;
288 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
290 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
291 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
292 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
295 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
296 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
297 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
300 if (info->attrs[NL80211_ATTR_KEY_IDX])
301 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
303 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
304 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
306 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
307 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
309 return 0;
312 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
314 int err;
316 memset(k, 0, sizeof(*k));
317 k->idx = -1;
319 if (info->attrs[NL80211_ATTR_KEY])
320 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
321 else
322 err = nl80211_parse_key_old(info, k);
324 if (err)
325 return err;
327 if (k->def && k->defmgmt)
328 return -EINVAL;
330 if (k->idx != -1) {
331 if (k->defmgmt) {
332 if (k->idx < 4 || k->idx > 5)
333 return -EINVAL;
334 } else if (k->def) {
335 if (k->idx < 0 || k->idx > 3)
336 return -EINVAL;
337 } else {
338 if (k->idx < 0 || k->idx > 5)
339 return -EINVAL;
343 return 0;
346 static struct cfg80211_cached_keys *
347 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
348 struct nlattr *keys)
350 struct key_parse parse;
351 struct nlattr *key;
352 struct cfg80211_cached_keys *result;
353 int rem, err, def = 0;
355 result = kzalloc(sizeof(*result), GFP_KERNEL);
356 if (!result)
357 return ERR_PTR(-ENOMEM);
359 result->def = -1;
360 result->defmgmt = -1;
362 nla_for_each_nested(key, keys, rem) {
363 memset(&parse, 0, sizeof(parse));
364 parse.idx = -1;
366 err = nl80211_parse_key_new(key, &parse);
367 if (err)
368 goto error;
369 err = -EINVAL;
370 if (!parse.p.key)
371 goto error;
372 if (parse.idx < 0 || parse.idx > 4)
373 goto error;
374 if (parse.def) {
375 if (def)
376 goto error;
377 def = 1;
378 result->def = parse.idx;
379 } else if (parse.defmgmt)
380 goto error;
381 err = cfg80211_validate_key_settings(rdev, &parse.p,
382 parse.idx, NULL);
383 if (err)
384 goto error;
385 result->params[parse.idx].cipher = parse.p.cipher;
386 result->params[parse.idx].key_len = parse.p.key_len;
387 result->params[parse.idx].key = result->data[parse.idx];
388 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
391 return result;
392 error:
393 kfree(result);
394 return ERR_PTR(err);
397 static int nl80211_key_allowed(struct wireless_dev *wdev)
399 ASSERT_WDEV_LOCK(wdev);
401 if (!netif_running(wdev->netdev))
402 return -ENETDOWN;
404 switch (wdev->iftype) {
405 case NL80211_IFTYPE_AP:
406 case NL80211_IFTYPE_AP_VLAN:
407 break;
408 case NL80211_IFTYPE_ADHOC:
409 if (!wdev->current_bss)
410 return -ENOLINK;
411 break;
412 case NL80211_IFTYPE_STATION:
413 if (wdev->sme_state != CFG80211_SME_CONNECTED)
414 return -ENOLINK;
415 break;
416 default:
417 return -EINVAL;
420 return 0;
423 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
424 struct cfg80211_registered_device *dev)
426 void *hdr;
427 struct nlattr *nl_bands, *nl_band;
428 struct nlattr *nl_freqs, *nl_freq;
429 struct nlattr *nl_rates, *nl_rate;
430 struct nlattr *nl_modes;
431 struct nlattr *nl_cmds;
432 enum ieee80211_band band;
433 struct ieee80211_channel *chan;
434 struct ieee80211_rate *rate;
435 int i;
436 u16 ifmodes = dev->wiphy.interface_modes;
438 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
439 if (!hdr)
440 return -1;
442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
443 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
445 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
446 cfg80211_rdev_list_generation);
448 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
449 dev->wiphy.retry_short);
450 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
451 dev->wiphy.retry_long);
452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
453 dev->wiphy.frag_threshold);
454 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
455 dev->wiphy.rts_threshold);
456 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
457 dev->wiphy.coverage_class);
459 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
460 dev->wiphy.max_scan_ssids);
461 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
462 dev->wiphy.max_scan_ie_len);
464 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
465 sizeof(u32) * dev->wiphy.n_cipher_suites,
466 dev->wiphy.cipher_suites);
468 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
469 dev->wiphy.max_num_pmkids);
471 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
472 if (!nl_modes)
473 goto nla_put_failure;
475 i = 0;
476 while (ifmodes) {
477 if (ifmodes & 1)
478 NLA_PUT_FLAG(msg, i);
479 ifmodes >>= 1;
480 i++;
483 nla_nest_end(msg, nl_modes);
485 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
486 if (!nl_bands)
487 goto nla_put_failure;
489 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
490 if (!dev->wiphy.bands[band])
491 continue;
493 nl_band = nla_nest_start(msg, band);
494 if (!nl_band)
495 goto nla_put_failure;
497 /* add HT info */
498 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
499 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
500 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
501 &dev->wiphy.bands[band]->ht_cap.mcs);
502 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
503 dev->wiphy.bands[band]->ht_cap.cap);
504 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
505 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
506 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
507 dev->wiphy.bands[band]->ht_cap.ampdu_density);
510 /* add frequencies */
511 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
512 if (!nl_freqs)
513 goto nla_put_failure;
515 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
516 nl_freq = nla_nest_start(msg, i);
517 if (!nl_freq)
518 goto nla_put_failure;
520 chan = &dev->wiphy.bands[band]->channels[i];
522 if (nl80211_msg_put_channel(msg, chan))
523 goto nla_put_failure;
525 nla_nest_end(msg, nl_freq);
528 nla_nest_end(msg, nl_freqs);
530 /* add bitrates */
531 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
532 if (!nl_rates)
533 goto nla_put_failure;
535 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
536 nl_rate = nla_nest_start(msg, i);
537 if (!nl_rate)
538 goto nla_put_failure;
540 rate = &dev->wiphy.bands[band]->bitrates[i];
541 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
542 rate->bitrate);
543 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
544 NLA_PUT_FLAG(msg,
545 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
547 nla_nest_end(msg, nl_rate);
550 nla_nest_end(msg, nl_rates);
552 nla_nest_end(msg, nl_band);
554 nla_nest_end(msg, nl_bands);
556 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
557 if (!nl_cmds)
558 goto nla_put_failure;
560 i = 0;
561 #define CMD(op, n) \
562 do { \
563 if (dev->ops->op) { \
564 i++; \
565 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
567 } while (0)
569 CMD(add_virtual_intf, NEW_INTERFACE);
570 CMD(change_virtual_intf, SET_INTERFACE);
571 CMD(add_key, NEW_KEY);
572 CMD(add_beacon, NEW_BEACON);
573 CMD(add_station, NEW_STATION);
574 CMD(add_mpath, NEW_MPATH);
575 CMD(set_mesh_params, SET_MESH_PARAMS);
576 CMD(change_bss, SET_BSS);
577 CMD(auth, AUTHENTICATE);
578 CMD(assoc, ASSOCIATE);
579 CMD(deauth, DEAUTHENTICATE);
580 CMD(disassoc, DISASSOCIATE);
581 CMD(join_ibss, JOIN_IBSS);
582 CMD(set_pmksa, SET_PMKSA);
583 CMD(del_pmksa, DEL_PMKSA);
584 CMD(flush_pmksa, FLUSH_PMKSA);
585 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
586 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
587 CMD(action, ACTION);
588 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
589 i++;
590 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
592 CMD(set_channel, SET_CHANNEL);
594 #undef CMD
596 if (dev->ops->connect || dev->ops->auth) {
597 i++;
598 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
601 if (dev->ops->disconnect || dev->ops->deauth) {
602 i++;
603 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
606 nla_nest_end(msg, nl_cmds);
608 return genlmsg_end(msg, hdr);
610 nla_put_failure:
611 genlmsg_cancel(msg, hdr);
612 return -EMSGSIZE;
615 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
617 int idx = 0;
618 int start = cb->args[0];
619 struct cfg80211_registered_device *dev;
621 mutex_lock(&cfg80211_mutex);
622 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
623 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
624 continue;
625 if (++idx <= start)
626 continue;
627 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
628 cb->nlh->nlmsg_seq, NLM_F_MULTI,
629 dev) < 0) {
630 idx--;
631 break;
634 mutex_unlock(&cfg80211_mutex);
636 cb->args[0] = idx;
638 return skb->len;
641 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
643 struct sk_buff *msg;
644 struct cfg80211_registered_device *dev;
646 dev = cfg80211_get_dev_from_info(info);
647 if (IS_ERR(dev))
648 return PTR_ERR(dev);
650 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
651 if (!msg)
652 goto out_err;
654 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
655 goto out_free;
657 cfg80211_unlock_rdev(dev);
659 return genlmsg_reply(msg, info);
661 out_free:
662 nlmsg_free(msg);
663 out_err:
664 cfg80211_unlock_rdev(dev);
665 return -ENOBUFS;
668 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
669 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
670 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
671 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
672 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
673 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
676 static int parse_txq_params(struct nlattr *tb[],
677 struct ieee80211_txq_params *txq_params)
679 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
680 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
681 !tb[NL80211_TXQ_ATTR_AIFS])
682 return -EINVAL;
684 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
685 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
686 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
687 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
688 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
690 return 0;
693 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
696 * You can only set the channel explicitly for AP, mesh
697 * and WDS type interfaces; all others have their channel
698 * managed via their respective "establish a connection"
699 * command (connect, join, ...)
701 * Monitors are special as they are normally slaved to
702 * whatever else is going on, so they behave as though
703 * you tried setting the wiphy channel itself.
705 return !wdev ||
706 wdev->iftype == NL80211_IFTYPE_AP ||
707 wdev->iftype == NL80211_IFTYPE_WDS ||
708 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
709 wdev->iftype == NL80211_IFTYPE_MONITOR;
712 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
713 struct wireless_dev *wdev,
714 struct genl_info *info)
716 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
717 u32 freq;
718 int result;
720 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
721 return -EINVAL;
723 if (!nl80211_can_set_dev_channel(wdev))
724 return -EOPNOTSUPP;
726 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
727 channel_type = nla_get_u32(info->attrs[
728 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
729 if (channel_type != NL80211_CHAN_NO_HT &&
730 channel_type != NL80211_CHAN_HT20 &&
731 channel_type != NL80211_CHAN_HT40PLUS &&
732 channel_type != NL80211_CHAN_HT40MINUS)
733 return -EINVAL;
736 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
738 mutex_lock(&rdev->devlist_mtx);
739 if (wdev) {
740 wdev_lock(wdev);
741 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
742 wdev_unlock(wdev);
743 } else {
744 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
746 mutex_unlock(&rdev->devlist_mtx);
748 return result;
751 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
753 struct cfg80211_registered_device *rdev;
754 struct net_device *netdev;
755 int result;
757 rtnl_lock();
759 result = get_rdev_dev_by_info_ifindex(info, &rdev, &netdev);
760 if (result)
761 goto unlock_rtnl;
763 result = __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
765 dev_put(netdev);
766 cfg80211_unlock_rdev(rdev);
767 unlock_rtnl:
768 rtnl_unlock();
770 return result;
773 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
775 struct cfg80211_registered_device *rdev;
776 struct net_device *netdev = NULL;
777 struct wireless_dev *wdev;
778 int result, rem_txq_params = 0;
779 struct nlattr *nl_txq_params;
780 u32 changed;
781 u8 retry_short = 0, retry_long = 0;
782 u32 frag_threshold = 0, rts_threshold = 0;
783 u8 coverage_class = 0;
785 rtnl_lock();
788 * Try to find the wiphy and netdev. Normally this
789 * function shouldn't need the netdev, but this is
790 * done for backward compatibility -- previously
791 * setting the channel was done per wiphy, but now
792 * it is per netdev. Previous userland like hostapd
793 * also passed a netdev to set_wiphy, so that it is
794 * possible to let that go to the right netdev!
796 mutex_lock(&cfg80211_mutex);
798 if (info->attrs[NL80211_ATTR_IFINDEX]) {
799 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
801 netdev = dev_get_by_index(genl_info_net(info), ifindex);
802 if (netdev && netdev->ieee80211_ptr) {
803 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
804 mutex_lock(&rdev->mtx);
805 } else
806 netdev = NULL;
809 if (!netdev) {
810 rdev = __cfg80211_rdev_from_info(info);
811 if (IS_ERR(rdev)) {
812 mutex_unlock(&cfg80211_mutex);
813 result = PTR_ERR(rdev);
814 goto unlock;
816 wdev = NULL;
817 netdev = NULL;
818 result = 0;
820 mutex_lock(&rdev->mtx);
821 } else if (netif_running(netdev) &&
822 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
823 wdev = netdev->ieee80211_ptr;
824 else
825 wdev = NULL;
828 * end workaround code, by now the rdev is available
829 * and locked, and wdev may or may not be NULL.
832 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
833 result = cfg80211_dev_rename(
834 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
836 mutex_unlock(&cfg80211_mutex);
838 if (result)
839 goto bad_res;
841 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
842 struct ieee80211_txq_params txq_params;
843 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
845 if (!rdev->ops->set_txq_params) {
846 result = -EOPNOTSUPP;
847 goto bad_res;
850 nla_for_each_nested(nl_txq_params,
851 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
852 rem_txq_params) {
853 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
854 nla_data(nl_txq_params),
855 nla_len(nl_txq_params),
856 txq_params_policy);
857 result = parse_txq_params(tb, &txq_params);
858 if (result)
859 goto bad_res;
861 result = rdev->ops->set_txq_params(&rdev->wiphy,
862 &txq_params);
863 if (result)
864 goto bad_res;
868 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
869 result = __nl80211_set_channel(rdev, wdev, info);
870 if (result)
871 goto bad_res;
874 changed = 0;
876 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
877 retry_short = nla_get_u8(
878 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
879 if (retry_short == 0) {
880 result = -EINVAL;
881 goto bad_res;
883 changed |= WIPHY_PARAM_RETRY_SHORT;
886 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
887 retry_long = nla_get_u8(
888 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
889 if (retry_long == 0) {
890 result = -EINVAL;
891 goto bad_res;
893 changed |= WIPHY_PARAM_RETRY_LONG;
896 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
897 frag_threshold = nla_get_u32(
898 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
899 if (frag_threshold < 256) {
900 result = -EINVAL;
901 goto bad_res;
903 if (frag_threshold != (u32) -1) {
905 * Fragments (apart from the last one) are required to
906 * have even length. Make the fragmentation code
907 * simpler by stripping LSB should someone try to use
908 * odd threshold value.
910 frag_threshold &= ~0x1;
912 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
915 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
916 rts_threshold = nla_get_u32(
917 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
918 changed |= WIPHY_PARAM_RTS_THRESHOLD;
921 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
922 coverage_class = nla_get_u8(
923 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
924 changed |= WIPHY_PARAM_COVERAGE_CLASS;
927 if (changed) {
928 u8 old_retry_short, old_retry_long;
929 u32 old_frag_threshold, old_rts_threshold;
930 u8 old_coverage_class;
932 if (!rdev->ops->set_wiphy_params) {
933 result = -EOPNOTSUPP;
934 goto bad_res;
937 old_retry_short = rdev->wiphy.retry_short;
938 old_retry_long = rdev->wiphy.retry_long;
939 old_frag_threshold = rdev->wiphy.frag_threshold;
940 old_rts_threshold = rdev->wiphy.rts_threshold;
941 old_coverage_class = rdev->wiphy.coverage_class;
943 if (changed & WIPHY_PARAM_RETRY_SHORT)
944 rdev->wiphy.retry_short = retry_short;
945 if (changed & WIPHY_PARAM_RETRY_LONG)
946 rdev->wiphy.retry_long = retry_long;
947 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
948 rdev->wiphy.frag_threshold = frag_threshold;
949 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
950 rdev->wiphy.rts_threshold = rts_threshold;
951 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
952 rdev->wiphy.coverage_class = coverage_class;
954 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
955 if (result) {
956 rdev->wiphy.retry_short = old_retry_short;
957 rdev->wiphy.retry_long = old_retry_long;
958 rdev->wiphy.frag_threshold = old_frag_threshold;
959 rdev->wiphy.rts_threshold = old_rts_threshold;
960 rdev->wiphy.coverage_class = old_coverage_class;
964 bad_res:
965 mutex_unlock(&rdev->mtx);
966 if (netdev)
967 dev_put(netdev);
968 unlock:
969 rtnl_unlock();
970 return result;
974 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
975 struct cfg80211_registered_device *rdev,
976 struct net_device *dev)
978 void *hdr;
980 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
981 if (!hdr)
982 return -1;
984 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
985 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
986 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
987 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
989 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
990 rdev->devlist_generation ^
991 (cfg80211_rdev_list_generation << 2));
993 return genlmsg_end(msg, hdr);
995 nla_put_failure:
996 genlmsg_cancel(msg, hdr);
997 return -EMSGSIZE;
1000 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1002 int wp_idx = 0;
1003 int if_idx = 0;
1004 int wp_start = cb->args[0];
1005 int if_start = cb->args[1];
1006 struct cfg80211_registered_device *rdev;
1007 struct wireless_dev *wdev;
1009 mutex_lock(&cfg80211_mutex);
1010 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1011 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1012 continue;
1013 if (wp_idx < wp_start) {
1014 wp_idx++;
1015 continue;
1017 if_idx = 0;
1019 mutex_lock(&rdev->devlist_mtx);
1020 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1021 if (if_idx < if_start) {
1022 if_idx++;
1023 continue;
1025 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1026 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1027 rdev, wdev->netdev) < 0) {
1028 mutex_unlock(&rdev->devlist_mtx);
1029 goto out;
1031 if_idx++;
1033 mutex_unlock(&rdev->devlist_mtx);
1035 wp_idx++;
1037 out:
1038 mutex_unlock(&cfg80211_mutex);
1040 cb->args[0] = wp_idx;
1041 cb->args[1] = if_idx;
1043 return skb->len;
1046 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1048 struct sk_buff *msg;
1049 struct cfg80211_registered_device *dev;
1050 struct net_device *netdev;
1051 int err;
1053 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
1054 if (err)
1055 return err;
1057 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1058 if (!msg)
1059 goto out_err;
1061 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1062 dev, netdev) < 0)
1063 goto out_free;
1065 dev_put(netdev);
1066 cfg80211_unlock_rdev(dev);
1068 return genlmsg_reply(msg, info);
1070 out_free:
1071 nlmsg_free(msg);
1072 out_err:
1073 dev_put(netdev);
1074 cfg80211_unlock_rdev(dev);
1075 return -ENOBUFS;
1078 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1079 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1080 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1081 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1082 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1083 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1086 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1088 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1089 int flag;
1091 *mntrflags = 0;
1093 if (!nla)
1094 return -EINVAL;
1096 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1097 nla, mntr_flags_policy))
1098 return -EINVAL;
1100 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1101 if (flags[flag])
1102 *mntrflags |= (1<<flag);
1104 return 0;
1107 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1108 struct net_device *netdev, u8 use_4addr,
1109 enum nl80211_iftype iftype)
1111 if (!use_4addr) {
1112 if (netdev && netdev->br_port)
1113 return -EBUSY;
1114 return 0;
1117 switch (iftype) {
1118 case NL80211_IFTYPE_AP_VLAN:
1119 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1120 return 0;
1121 break;
1122 case NL80211_IFTYPE_STATION:
1123 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1124 return 0;
1125 break;
1126 default:
1127 break;
1130 return -EOPNOTSUPP;
1133 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1135 struct cfg80211_registered_device *rdev;
1136 struct vif_params params;
1137 int err;
1138 enum nl80211_iftype otype, ntype;
1139 struct net_device *dev;
1140 u32 _flags, *flags = NULL;
1141 bool change = false;
1143 memset(&params, 0, sizeof(params));
1145 rtnl_lock();
1147 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1148 if (err)
1149 goto unlock_rtnl;
1151 otype = ntype = dev->ieee80211_ptr->iftype;
1153 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1154 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1155 if (otype != ntype)
1156 change = true;
1157 if (ntype > NL80211_IFTYPE_MAX) {
1158 err = -EINVAL;
1159 goto unlock;
1163 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1164 if (ntype != NL80211_IFTYPE_MESH_POINT) {
1165 err = -EINVAL;
1166 goto unlock;
1168 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1169 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1170 change = true;
1173 if (info->attrs[NL80211_ATTR_4ADDR]) {
1174 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1175 change = true;
1176 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1177 if (err)
1178 goto unlock;
1179 } else {
1180 params.use_4addr = -1;
1183 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1184 if (ntype != NL80211_IFTYPE_MONITOR) {
1185 err = -EINVAL;
1186 goto unlock;
1188 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1189 &_flags);
1190 if (err)
1191 goto unlock;
1193 flags = &_flags;
1194 change = true;
1197 if (change)
1198 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1199 else
1200 err = 0;
1202 if (!err && params.use_4addr != -1)
1203 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1205 unlock:
1206 dev_put(dev);
1207 cfg80211_unlock_rdev(rdev);
1208 unlock_rtnl:
1209 rtnl_unlock();
1210 return err;
1213 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1215 struct cfg80211_registered_device *rdev;
1216 struct vif_params params;
1217 int err;
1218 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1219 u32 flags;
1221 memset(&params, 0, sizeof(params));
1223 if (!info->attrs[NL80211_ATTR_IFNAME])
1224 return -EINVAL;
1226 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1227 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1228 if (type > NL80211_IFTYPE_MAX)
1229 return -EINVAL;
1232 rtnl_lock();
1234 rdev = cfg80211_get_dev_from_info(info);
1235 if (IS_ERR(rdev)) {
1236 err = PTR_ERR(rdev);
1237 goto unlock_rtnl;
1240 if (!rdev->ops->add_virtual_intf ||
1241 !(rdev->wiphy.interface_modes & (1 << type))) {
1242 err = -EOPNOTSUPP;
1243 goto unlock;
1246 if (type == NL80211_IFTYPE_MESH_POINT &&
1247 info->attrs[NL80211_ATTR_MESH_ID]) {
1248 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1249 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1252 if (info->attrs[NL80211_ATTR_4ADDR]) {
1253 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1254 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1255 if (err)
1256 goto unlock;
1259 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1260 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1261 &flags);
1262 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1263 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1264 type, err ? NULL : &flags, &params);
1266 unlock:
1267 cfg80211_unlock_rdev(rdev);
1268 unlock_rtnl:
1269 rtnl_unlock();
1270 return err;
1273 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1275 struct cfg80211_registered_device *rdev;
1276 int err;
1277 struct net_device *dev;
1279 rtnl_lock();
1281 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1282 if (err)
1283 goto unlock_rtnl;
1285 if (!rdev->ops->del_virtual_intf) {
1286 err = -EOPNOTSUPP;
1287 goto out;
1290 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1292 out:
1293 cfg80211_unlock_rdev(rdev);
1294 dev_put(dev);
1295 unlock_rtnl:
1296 rtnl_unlock();
1297 return err;
1300 struct get_key_cookie {
1301 struct sk_buff *msg;
1302 int error;
1303 int idx;
1306 static void get_key_callback(void *c, struct key_params *params)
1308 struct nlattr *key;
1309 struct get_key_cookie *cookie = c;
1311 if (params->key)
1312 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1313 params->key_len, params->key);
1315 if (params->seq)
1316 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1317 params->seq_len, params->seq);
1319 if (params->cipher)
1320 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1321 params->cipher);
1323 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1324 if (!key)
1325 goto nla_put_failure;
1327 if (params->key)
1328 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1329 params->key_len, params->key);
1331 if (params->seq)
1332 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1333 params->seq_len, params->seq);
1335 if (params->cipher)
1336 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1337 params->cipher);
1339 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1341 nla_nest_end(cookie->msg, key);
1343 return;
1344 nla_put_failure:
1345 cookie->error = 1;
1348 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1350 struct cfg80211_registered_device *rdev;
1351 int err;
1352 struct net_device *dev;
1353 u8 key_idx = 0;
1354 u8 *mac_addr = NULL;
1355 struct get_key_cookie cookie = {
1356 .error = 0,
1358 void *hdr;
1359 struct sk_buff *msg;
1361 if (info->attrs[NL80211_ATTR_KEY_IDX])
1362 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1364 if (key_idx > 5)
1365 return -EINVAL;
1367 if (info->attrs[NL80211_ATTR_MAC])
1368 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1370 rtnl_lock();
1372 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1373 if (err)
1374 goto unlock_rtnl;
1376 if (!rdev->ops->get_key) {
1377 err = -EOPNOTSUPP;
1378 goto out;
1381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1382 if (!msg) {
1383 err = -ENOMEM;
1384 goto out;
1387 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1388 NL80211_CMD_NEW_KEY);
1390 if (IS_ERR(hdr)) {
1391 err = PTR_ERR(hdr);
1392 goto free_msg;
1395 cookie.msg = msg;
1396 cookie.idx = key_idx;
1398 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1399 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1400 if (mac_addr)
1401 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1403 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
1404 &cookie, get_key_callback);
1406 if (err)
1407 goto free_msg;
1409 if (cookie.error)
1410 goto nla_put_failure;
1412 genlmsg_end(msg, hdr);
1413 err = genlmsg_reply(msg, info);
1414 goto out;
1416 nla_put_failure:
1417 err = -ENOBUFS;
1418 free_msg:
1419 nlmsg_free(msg);
1420 out:
1421 cfg80211_unlock_rdev(rdev);
1422 dev_put(dev);
1423 unlock_rtnl:
1424 rtnl_unlock();
1426 return err;
1429 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1431 struct cfg80211_registered_device *rdev;
1432 struct key_parse key;
1433 int err;
1434 struct net_device *dev;
1435 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1436 u8 key_index);
1438 err = nl80211_parse_key(info, &key);
1439 if (err)
1440 return err;
1442 if (key.idx < 0)
1443 return -EINVAL;
1445 /* only support setting default key */
1446 if (!key.def && !key.defmgmt)
1447 return -EINVAL;
1449 rtnl_lock();
1451 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1452 if (err)
1453 goto unlock_rtnl;
1455 if (key.def)
1456 func = rdev->ops->set_default_key;
1457 else
1458 func = rdev->ops->set_default_mgmt_key;
1460 if (!func) {
1461 err = -EOPNOTSUPP;
1462 goto out;
1465 wdev_lock(dev->ieee80211_ptr);
1466 err = nl80211_key_allowed(dev->ieee80211_ptr);
1467 if (!err)
1468 err = func(&rdev->wiphy, dev, key.idx);
1470 #ifdef CONFIG_CFG80211_WEXT
1471 if (!err) {
1472 if (func == rdev->ops->set_default_key)
1473 dev->ieee80211_ptr->wext.default_key = key.idx;
1474 else
1475 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1477 #endif
1478 wdev_unlock(dev->ieee80211_ptr);
1480 out:
1481 cfg80211_unlock_rdev(rdev);
1482 dev_put(dev);
1484 unlock_rtnl:
1485 rtnl_unlock();
1487 return err;
1490 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1492 struct cfg80211_registered_device *rdev;
1493 int err;
1494 struct net_device *dev;
1495 struct key_parse key;
1496 u8 *mac_addr = NULL;
1498 err = nl80211_parse_key(info, &key);
1499 if (err)
1500 return err;
1502 if (!key.p.key)
1503 return -EINVAL;
1505 if (info->attrs[NL80211_ATTR_MAC])
1506 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1508 rtnl_lock();
1510 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1511 if (err)
1512 goto unlock_rtnl;
1514 if (!rdev->ops->add_key) {
1515 err = -EOPNOTSUPP;
1516 goto out;
1519 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1520 err = -EINVAL;
1521 goto out;
1524 wdev_lock(dev->ieee80211_ptr);
1525 err = nl80211_key_allowed(dev->ieee80211_ptr);
1526 if (!err)
1527 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1528 mac_addr, &key.p);
1529 wdev_unlock(dev->ieee80211_ptr);
1531 out:
1532 cfg80211_unlock_rdev(rdev);
1533 dev_put(dev);
1534 unlock_rtnl:
1535 rtnl_unlock();
1537 return err;
1540 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1542 struct cfg80211_registered_device *rdev;
1543 int err;
1544 struct net_device *dev;
1545 u8 *mac_addr = NULL;
1546 struct key_parse key;
1548 err = nl80211_parse_key(info, &key);
1549 if (err)
1550 return err;
1552 if (info->attrs[NL80211_ATTR_MAC])
1553 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1555 rtnl_lock();
1557 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1558 if (err)
1559 goto unlock_rtnl;
1561 if (!rdev->ops->del_key) {
1562 err = -EOPNOTSUPP;
1563 goto out;
1566 wdev_lock(dev->ieee80211_ptr);
1567 err = nl80211_key_allowed(dev->ieee80211_ptr);
1568 if (!err)
1569 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
1571 #ifdef CONFIG_CFG80211_WEXT
1572 if (!err) {
1573 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1574 dev->ieee80211_ptr->wext.default_key = -1;
1575 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1576 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1578 #endif
1579 wdev_unlock(dev->ieee80211_ptr);
1581 out:
1582 cfg80211_unlock_rdev(rdev);
1583 dev_put(dev);
1585 unlock_rtnl:
1586 rtnl_unlock();
1588 return err;
1591 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1593 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1594 struct beacon_parameters *info);
1595 struct cfg80211_registered_device *rdev;
1596 int err;
1597 struct net_device *dev;
1598 struct beacon_parameters params;
1599 int haveinfo = 0;
1601 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1602 return -EINVAL;
1604 rtnl_lock();
1606 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1607 if (err)
1608 goto unlock_rtnl;
1610 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1611 err = -EOPNOTSUPP;
1612 goto out;
1615 switch (info->genlhdr->cmd) {
1616 case NL80211_CMD_NEW_BEACON:
1617 /* these are required for NEW_BEACON */
1618 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1619 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1620 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1621 err = -EINVAL;
1622 goto out;
1625 call = rdev->ops->add_beacon;
1626 break;
1627 case NL80211_CMD_SET_BEACON:
1628 call = rdev->ops->set_beacon;
1629 break;
1630 default:
1631 WARN_ON(1);
1632 err = -EOPNOTSUPP;
1633 goto out;
1636 if (!call) {
1637 err = -EOPNOTSUPP;
1638 goto out;
1641 memset(&params, 0, sizeof(params));
1643 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1644 params.interval =
1645 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1646 haveinfo = 1;
1649 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1650 params.dtim_period =
1651 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1652 haveinfo = 1;
1655 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1656 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1657 params.head_len =
1658 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1659 haveinfo = 1;
1662 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1663 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1664 params.tail_len =
1665 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1666 haveinfo = 1;
1669 if (!haveinfo) {
1670 err = -EINVAL;
1671 goto out;
1674 err = call(&rdev->wiphy, dev, &params);
1676 out:
1677 cfg80211_unlock_rdev(rdev);
1678 dev_put(dev);
1679 unlock_rtnl:
1680 rtnl_unlock();
1682 return err;
1685 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1687 struct cfg80211_registered_device *rdev;
1688 int err;
1689 struct net_device *dev;
1691 rtnl_lock();
1693 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1694 if (err)
1695 goto unlock_rtnl;
1697 if (!rdev->ops->del_beacon) {
1698 err = -EOPNOTSUPP;
1699 goto out;
1702 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1703 err = -EOPNOTSUPP;
1704 goto out;
1706 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
1708 out:
1709 cfg80211_unlock_rdev(rdev);
1710 dev_put(dev);
1711 unlock_rtnl:
1712 rtnl_unlock();
1714 return err;
1717 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1718 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1719 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1720 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1721 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1724 static int parse_station_flags(struct genl_info *info,
1725 struct station_parameters *params)
1727 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1728 struct nlattr *nla;
1729 int flag;
1732 * Try parsing the new attribute first so userspace
1733 * can specify both for older kernels.
1735 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1736 if (nla) {
1737 struct nl80211_sta_flag_update *sta_flags;
1739 sta_flags = nla_data(nla);
1740 params->sta_flags_mask = sta_flags->mask;
1741 params->sta_flags_set = sta_flags->set;
1742 if ((params->sta_flags_mask |
1743 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1744 return -EINVAL;
1745 return 0;
1748 /* if present, parse the old attribute */
1750 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1751 if (!nla)
1752 return 0;
1754 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1755 nla, sta_flags_policy))
1756 return -EINVAL;
1758 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1759 params->sta_flags_mask &= ~1;
1761 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1762 if (flags[flag])
1763 params->sta_flags_set |= (1<<flag);
1765 return 0;
1768 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1769 int flags, struct net_device *dev,
1770 const u8 *mac_addr, struct station_info *sinfo)
1772 void *hdr;
1773 struct nlattr *sinfoattr, *txrate;
1774 u16 bitrate;
1776 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1777 if (!hdr)
1778 return -1;
1780 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1781 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1783 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1785 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1786 if (!sinfoattr)
1787 goto nla_put_failure;
1788 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1789 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1790 sinfo->inactive_time);
1791 if (sinfo->filled & STATION_INFO_RX_BYTES)
1792 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1793 sinfo->rx_bytes);
1794 if (sinfo->filled & STATION_INFO_TX_BYTES)
1795 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1796 sinfo->tx_bytes);
1797 if (sinfo->filled & STATION_INFO_LLID)
1798 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1799 sinfo->llid);
1800 if (sinfo->filled & STATION_INFO_PLID)
1801 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1802 sinfo->plid);
1803 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1804 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1805 sinfo->plink_state);
1806 if (sinfo->filled & STATION_INFO_SIGNAL)
1807 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1808 sinfo->signal);
1809 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1810 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1811 if (!txrate)
1812 goto nla_put_failure;
1814 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1815 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
1816 if (bitrate > 0)
1817 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1819 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1820 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1821 sinfo->txrate.mcs);
1822 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1823 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1824 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1825 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1827 nla_nest_end(msg, txrate);
1829 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1830 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1831 sinfo->rx_packets);
1832 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1833 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1834 sinfo->tx_packets);
1835 nla_nest_end(msg, sinfoattr);
1837 return genlmsg_end(msg, hdr);
1839 nla_put_failure:
1840 genlmsg_cancel(msg, hdr);
1841 return -EMSGSIZE;
1844 static int nl80211_dump_station(struct sk_buff *skb,
1845 struct netlink_callback *cb)
1847 struct station_info sinfo;
1848 struct cfg80211_registered_device *dev;
1849 struct net_device *netdev;
1850 u8 mac_addr[ETH_ALEN];
1851 int ifidx = cb->args[0];
1852 int sta_idx = cb->args[1];
1853 int err;
1855 if (!ifidx)
1856 ifidx = nl80211_get_ifidx(cb);
1857 if (ifidx < 0)
1858 return ifidx;
1860 rtnl_lock();
1862 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1863 if (!netdev) {
1864 err = -ENODEV;
1865 goto out_rtnl;
1868 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
1869 if (IS_ERR(dev)) {
1870 err = PTR_ERR(dev);
1871 goto out_rtnl;
1874 if (!dev->ops->dump_station) {
1875 err = -EOPNOTSUPP;
1876 goto out_err;
1879 while (1) {
1880 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1881 mac_addr, &sinfo);
1882 if (err == -ENOENT)
1883 break;
1884 if (err)
1885 goto out_err;
1887 if (nl80211_send_station(skb,
1888 NETLINK_CB(cb->skb).pid,
1889 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1890 netdev, mac_addr,
1891 &sinfo) < 0)
1892 goto out;
1894 sta_idx++;
1898 out:
1899 cb->args[1] = sta_idx;
1900 err = skb->len;
1901 out_err:
1902 cfg80211_unlock_rdev(dev);
1903 out_rtnl:
1904 rtnl_unlock();
1906 return err;
1909 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1911 struct cfg80211_registered_device *rdev;
1912 int err;
1913 struct net_device *dev;
1914 struct station_info sinfo;
1915 struct sk_buff *msg;
1916 u8 *mac_addr = NULL;
1918 memset(&sinfo, 0, sizeof(sinfo));
1920 if (!info->attrs[NL80211_ATTR_MAC])
1921 return -EINVAL;
1923 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1925 rtnl_lock();
1927 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
1928 if (err)
1929 goto out_rtnl;
1931 if (!rdev->ops->get_station) {
1932 err = -EOPNOTSUPP;
1933 goto out;
1936 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1937 if (err)
1938 goto out;
1940 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1941 if (!msg)
1942 goto out;
1944 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1945 dev, mac_addr, &sinfo) < 0)
1946 goto out_free;
1948 err = genlmsg_reply(msg, info);
1949 goto out;
1951 out_free:
1952 nlmsg_free(msg);
1953 out:
1954 cfg80211_unlock_rdev(rdev);
1955 dev_put(dev);
1956 out_rtnl:
1957 rtnl_unlock();
1959 return err;
1963 * Get vlan interface making sure it is running and on the right wiphy.
1965 static int get_vlan(struct genl_info *info,
1966 struct cfg80211_registered_device *rdev,
1967 struct net_device **vlan)
1969 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
1970 *vlan = NULL;
1972 if (vlanattr) {
1973 *vlan = dev_get_by_index(genl_info_net(info),
1974 nla_get_u32(vlanattr));
1975 if (!*vlan)
1976 return -ENODEV;
1977 if (!(*vlan)->ieee80211_ptr)
1978 return -EINVAL;
1979 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1980 return -EINVAL;
1981 if (!netif_running(*vlan))
1982 return -ENETDOWN;
1984 return 0;
1987 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1989 struct cfg80211_registered_device *rdev;
1990 int err;
1991 struct net_device *dev;
1992 struct station_parameters params;
1993 u8 *mac_addr = NULL;
1995 memset(&params, 0, sizeof(params));
1997 params.listen_interval = -1;
1999 if (info->attrs[NL80211_ATTR_STA_AID])
2000 return -EINVAL;
2002 if (!info->attrs[NL80211_ATTR_MAC])
2003 return -EINVAL;
2005 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2007 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2008 params.supported_rates =
2009 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2010 params.supported_rates_len =
2011 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2014 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2015 params.listen_interval =
2016 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2018 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2019 params.ht_capa =
2020 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2022 if (parse_station_flags(info, &params))
2023 return -EINVAL;
2025 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2026 params.plink_action =
2027 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2029 rtnl_lock();
2031 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2032 if (err)
2033 goto out_rtnl;
2035 err = get_vlan(info, rdev, &params.vlan);
2036 if (err)
2037 goto out;
2039 /* validate settings */
2040 err = 0;
2042 switch (dev->ieee80211_ptr->iftype) {
2043 case NL80211_IFTYPE_AP:
2044 case NL80211_IFTYPE_AP_VLAN:
2045 /* disallow mesh-specific things */
2046 if (params.plink_action)
2047 err = -EINVAL;
2048 break;
2049 case NL80211_IFTYPE_STATION:
2050 /* disallow everything but AUTHORIZED flag */
2051 if (params.plink_action)
2052 err = -EINVAL;
2053 if (params.vlan)
2054 err = -EINVAL;
2055 if (params.supported_rates)
2056 err = -EINVAL;
2057 if (params.ht_capa)
2058 err = -EINVAL;
2059 if (params.listen_interval >= 0)
2060 err = -EINVAL;
2061 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2062 err = -EINVAL;
2063 break;
2064 case NL80211_IFTYPE_MESH_POINT:
2065 /* disallow things mesh doesn't support */
2066 if (params.vlan)
2067 err = -EINVAL;
2068 if (params.ht_capa)
2069 err = -EINVAL;
2070 if (params.listen_interval >= 0)
2071 err = -EINVAL;
2072 if (params.supported_rates)
2073 err = -EINVAL;
2074 if (params.sta_flags_mask)
2075 err = -EINVAL;
2076 break;
2077 default:
2078 err = -EINVAL;
2081 if (err)
2082 goto out;
2084 if (!rdev->ops->change_station) {
2085 err = -EOPNOTSUPP;
2086 goto out;
2089 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2091 out:
2092 if (params.vlan)
2093 dev_put(params.vlan);
2094 cfg80211_unlock_rdev(rdev);
2095 dev_put(dev);
2096 out_rtnl:
2097 rtnl_unlock();
2099 return err;
2102 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2104 struct cfg80211_registered_device *rdev;
2105 int err;
2106 struct net_device *dev;
2107 struct station_parameters params;
2108 u8 *mac_addr = NULL;
2110 memset(&params, 0, sizeof(params));
2112 if (!info->attrs[NL80211_ATTR_MAC])
2113 return -EINVAL;
2115 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2116 return -EINVAL;
2118 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2119 return -EINVAL;
2121 if (!info->attrs[NL80211_ATTR_STA_AID])
2122 return -EINVAL;
2124 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2125 params.supported_rates =
2126 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2127 params.supported_rates_len =
2128 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2129 params.listen_interval =
2130 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2132 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2133 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2134 return -EINVAL;
2136 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2137 params.ht_capa =
2138 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2140 if (parse_station_flags(info, &params))
2141 return -EINVAL;
2143 rtnl_lock();
2145 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2146 if (err)
2147 goto out_rtnl;
2149 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2150 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
2151 err = -EINVAL;
2152 goto out;
2155 err = get_vlan(info, rdev, &params.vlan);
2156 if (err)
2157 goto out;
2159 /* validate settings */
2160 err = 0;
2162 if (!rdev->ops->add_station) {
2163 err = -EOPNOTSUPP;
2164 goto out;
2167 if (!netif_running(dev)) {
2168 err = -ENETDOWN;
2169 goto out;
2172 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2174 out:
2175 if (params.vlan)
2176 dev_put(params.vlan);
2177 cfg80211_unlock_rdev(rdev);
2178 dev_put(dev);
2179 out_rtnl:
2180 rtnl_unlock();
2182 return err;
2185 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2187 struct cfg80211_registered_device *rdev;
2188 int err;
2189 struct net_device *dev;
2190 u8 *mac_addr = NULL;
2192 if (info->attrs[NL80211_ATTR_MAC])
2193 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2195 rtnl_lock();
2197 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2198 if (err)
2199 goto out_rtnl;
2201 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2202 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2203 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2204 err = -EINVAL;
2205 goto out;
2208 if (!rdev->ops->del_station) {
2209 err = -EOPNOTSUPP;
2210 goto out;
2213 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2215 out:
2216 cfg80211_unlock_rdev(rdev);
2217 dev_put(dev);
2218 out_rtnl:
2219 rtnl_unlock();
2221 return err;
2224 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2225 int flags, struct net_device *dev,
2226 u8 *dst, u8 *next_hop,
2227 struct mpath_info *pinfo)
2229 void *hdr;
2230 struct nlattr *pinfoattr;
2232 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2233 if (!hdr)
2234 return -1;
2236 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2237 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2238 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2240 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2242 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2243 if (!pinfoattr)
2244 goto nla_put_failure;
2245 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2246 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2247 pinfo->frame_qlen);
2248 if (pinfo->filled & MPATH_INFO_SN)
2249 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2250 pinfo->sn);
2251 if (pinfo->filled & MPATH_INFO_METRIC)
2252 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2253 pinfo->metric);
2254 if (pinfo->filled & MPATH_INFO_EXPTIME)
2255 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2256 pinfo->exptime);
2257 if (pinfo->filled & MPATH_INFO_FLAGS)
2258 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2259 pinfo->flags);
2260 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2261 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2262 pinfo->discovery_timeout);
2263 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2264 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2265 pinfo->discovery_retries);
2267 nla_nest_end(msg, pinfoattr);
2269 return genlmsg_end(msg, hdr);
2271 nla_put_failure:
2272 genlmsg_cancel(msg, hdr);
2273 return -EMSGSIZE;
2276 static int nl80211_dump_mpath(struct sk_buff *skb,
2277 struct netlink_callback *cb)
2279 struct mpath_info pinfo;
2280 struct cfg80211_registered_device *dev;
2281 struct net_device *netdev;
2282 u8 dst[ETH_ALEN];
2283 u8 next_hop[ETH_ALEN];
2284 int ifidx = cb->args[0];
2285 int path_idx = cb->args[1];
2286 int err;
2288 if (!ifidx)
2289 ifidx = nl80211_get_ifidx(cb);
2290 if (ifidx < 0)
2291 return ifidx;
2293 rtnl_lock();
2295 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2296 if (!netdev) {
2297 err = -ENODEV;
2298 goto out_rtnl;
2301 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
2302 if (IS_ERR(dev)) {
2303 err = PTR_ERR(dev);
2304 goto out_rtnl;
2307 if (!dev->ops->dump_mpath) {
2308 err = -EOPNOTSUPP;
2309 goto out_err;
2312 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2313 err = -EOPNOTSUPP;
2314 goto out_err;
2317 while (1) {
2318 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2319 dst, next_hop, &pinfo);
2320 if (err == -ENOENT)
2321 break;
2322 if (err)
2323 goto out_err;
2325 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2326 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2327 netdev, dst, next_hop,
2328 &pinfo) < 0)
2329 goto out;
2331 path_idx++;
2335 out:
2336 cb->args[1] = path_idx;
2337 err = skb->len;
2338 out_err:
2339 cfg80211_unlock_rdev(dev);
2340 out_rtnl:
2341 rtnl_unlock();
2343 return err;
2346 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2348 struct cfg80211_registered_device *rdev;
2349 int err;
2350 struct net_device *dev;
2351 struct mpath_info pinfo;
2352 struct sk_buff *msg;
2353 u8 *dst = NULL;
2354 u8 next_hop[ETH_ALEN];
2356 memset(&pinfo, 0, sizeof(pinfo));
2358 if (!info->attrs[NL80211_ATTR_MAC])
2359 return -EINVAL;
2361 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2363 rtnl_lock();
2365 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2366 if (err)
2367 goto out_rtnl;
2369 if (!rdev->ops->get_mpath) {
2370 err = -EOPNOTSUPP;
2371 goto out;
2374 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2375 err = -EOPNOTSUPP;
2376 goto out;
2379 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2380 if (err)
2381 goto out;
2383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2384 if (!msg)
2385 goto out;
2387 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2388 dev, dst, next_hop, &pinfo) < 0)
2389 goto out_free;
2391 err = genlmsg_reply(msg, info);
2392 goto out;
2394 out_free:
2395 nlmsg_free(msg);
2396 out:
2397 cfg80211_unlock_rdev(rdev);
2398 dev_put(dev);
2399 out_rtnl:
2400 rtnl_unlock();
2402 return err;
2405 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2407 struct cfg80211_registered_device *rdev;
2408 int err;
2409 struct net_device *dev;
2410 u8 *dst = NULL;
2411 u8 *next_hop = NULL;
2413 if (!info->attrs[NL80211_ATTR_MAC])
2414 return -EINVAL;
2416 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2417 return -EINVAL;
2419 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2420 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2422 rtnl_lock();
2424 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2425 if (err)
2426 goto out_rtnl;
2428 if (!rdev->ops->change_mpath) {
2429 err = -EOPNOTSUPP;
2430 goto out;
2433 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2434 err = -EOPNOTSUPP;
2435 goto out;
2438 if (!netif_running(dev)) {
2439 err = -ENETDOWN;
2440 goto out;
2443 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2445 out:
2446 cfg80211_unlock_rdev(rdev);
2447 dev_put(dev);
2448 out_rtnl:
2449 rtnl_unlock();
2451 return err;
2453 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2455 struct cfg80211_registered_device *rdev;
2456 int err;
2457 struct net_device *dev;
2458 u8 *dst = NULL;
2459 u8 *next_hop = NULL;
2461 if (!info->attrs[NL80211_ATTR_MAC])
2462 return -EINVAL;
2464 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2465 return -EINVAL;
2467 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2468 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2470 rtnl_lock();
2472 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2473 if (err)
2474 goto out_rtnl;
2476 if (!rdev->ops->add_mpath) {
2477 err = -EOPNOTSUPP;
2478 goto out;
2481 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2482 err = -EOPNOTSUPP;
2483 goto out;
2486 if (!netif_running(dev)) {
2487 err = -ENETDOWN;
2488 goto out;
2491 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2493 out:
2494 cfg80211_unlock_rdev(rdev);
2495 dev_put(dev);
2496 out_rtnl:
2497 rtnl_unlock();
2499 return err;
2502 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2504 struct cfg80211_registered_device *rdev;
2505 int err;
2506 struct net_device *dev;
2507 u8 *dst = NULL;
2509 if (info->attrs[NL80211_ATTR_MAC])
2510 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2512 rtnl_lock();
2514 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2515 if (err)
2516 goto out_rtnl;
2518 if (!rdev->ops->del_mpath) {
2519 err = -EOPNOTSUPP;
2520 goto out;
2523 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2525 out:
2526 cfg80211_unlock_rdev(rdev);
2527 dev_put(dev);
2528 out_rtnl:
2529 rtnl_unlock();
2531 return err;
2534 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2536 struct cfg80211_registered_device *rdev;
2537 int err;
2538 struct net_device *dev;
2539 struct bss_parameters params;
2541 memset(&params, 0, sizeof(params));
2542 /* default to not changing parameters */
2543 params.use_cts_prot = -1;
2544 params.use_short_preamble = -1;
2545 params.use_short_slot_time = -1;
2546 params.ap_isolate = -1;
2548 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2549 params.use_cts_prot =
2550 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2551 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2552 params.use_short_preamble =
2553 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2554 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2555 params.use_short_slot_time =
2556 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2557 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2558 params.basic_rates =
2559 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2560 params.basic_rates_len =
2561 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2563 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2564 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2566 rtnl_lock();
2568 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2569 if (err)
2570 goto out_rtnl;
2572 if (!rdev->ops->change_bss) {
2573 err = -EOPNOTSUPP;
2574 goto out;
2577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2578 err = -EOPNOTSUPP;
2579 goto out;
2582 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2584 out:
2585 cfg80211_unlock_rdev(rdev);
2586 dev_put(dev);
2587 out_rtnl:
2588 rtnl_unlock();
2590 return err;
2593 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2594 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2595 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2596 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2597 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2598 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2599 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2602 static int parse_reg_rule(struct nlattr *tb[],
2603 struct ieee80211_reg_rule *reg_rule)
2605 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2606 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2608 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2609 return -EINVAL;
2610 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2611 return -EINVAL;
2612 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2613 return -EINVAL;
2614 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2615 return -EINVAL;
2616 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2617 return -EINVAL;
2619 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2621 freq_range->start_freq_khz =
2622 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2623 freq_range->end_freq_khz =
2624 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2625 freq_range->max_bandwidth_khz =
2626 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2628 power_rule->max_eirp =
2629 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2631 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2632 power_rule->max_antenna_gain =
2633 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2635 return 0;
2638 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2640 int r;
2641 char *data = NULL;
2644 * You should only get this when cfg80211 hasn't yet initialized
2645 * completely when built-in to the kernel right between the time
2646 * window between nl80211_init() and regulatory_init(), if that is
2647 * even possible.
2649 mutex_lock(&cfg80211_mutex);
2650 if (unlikely(!cfg80211_regdomain)) {
2651 mutex_unlock(&cfg80211_mutex);
2652 return -EINPROGRESS;
2654 mutex_unlock(&cfg80211_mutex);
2656 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2657 return -EINVAL;
2659 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2661 r = regulatory_hint_user(data);
2663 return r;
2666 static int nl80211_get_mesh_params(struct sk_buff *skb,
2667 struct genl_info *info)
2669 struct cfg80211_registered_device *rdev;
2670 struct mesh_config cur_params;
2671 int err;
2672 struct net_device *dev;
2673 void *hdr;
2674 struct nlattr *pinfoattr;
2675 struct sk_buff *msg;
2677 rtnl_lock();
2679 /* Look up our device */
2680 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2681 if (err)
2682 goto out_rtnl;
2684 if (!rdev->ops->get_mesh_params) {
2685 err = -EOPNOTSUPP;
2686 goto out;
2689 /* Get the mesh params */
2690 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2691 if (err)
2692 goto out;
2694 /* Draw up a netlink message to send back */
2695 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2696 if (!msg) {
2697 err = -ENOBUFS;
2698 goto out;
2700 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2701 NL80211_CMD_GET_MESH_PARAMS);
2702 if (!hdr)
2703 goto nla_put_failure;
2704 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2705 if (!pinfoattr)
2706 goto nla_put_failure;
2707 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2708 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2709 cur_params.dot11MeshRetryTimeout);
2710 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2711 cur_params.dot11MeshConfirmTimeout);
2712 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2713 cur_params.dot11MeshHoldingTimeout);
2714 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2715 cur_params.dot11MeshMaxPeerLinks);
2716 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2717 cur_params.dot11MeshMaxRetries);
2718 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2719 cur_params.dot11MeshTTL);
2720 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2721 cur_params.auto_open_plinks);
2722 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2723 cur_params.dot11MeshHWMPmaxPREQretries);
2724 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2725 cur_params.path_refresh_time);
2726 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2727 cur_params.min_discovery_timeout);
2728 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2729 cur_params.dot11MeshHWMPactivePathTimeout);
2730 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2731 cur_params.dot11MeshHWMPpreqMinInterval);
2732 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2733 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2734 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2735 cur_params.dot11MeshHWMPRootMode);
2736 nla_nest_end(msg, pinfoattr);
2737 genlmsg_end(msg, hdr);
2738 err = genlmsg_reply(msg, info);
2739 goto out;
2741 nla_put_failure:
2742 genlmsg_cancel(msg, hdr);
2743 err = -EMSGSIZE;
2744 out:
2745 /* Cleanup */
2746 cfg80211_unlock_rdev(rdev);
2747 dev_put(dev);
2748 out_rtnl:
2749 rtnl_unlock();
2751 return err;
2754 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2755 do {\
2756 if (table[attr_num]) {\
2757 cfg.param = nla_fn(table[attr_num]); \
2758 mask |= (1 << (attr_num - 1)); \
2760 } while (0);\
2762 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2763 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2764 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2765 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2766 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2767 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2768 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2769 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2771 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2772 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2773 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2774 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2775 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2776 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2779 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2781 int err;
2782 u32 mask;
2783 struct cfg80211_registered_device *rdev;
2784 struct net_device *dev;
2785 struct mesh_config cfg;
2786 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2787 struct nlattr *parent_attr;
2789 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2790 if (!parent_attr)
2791 return -EINVAL;
2792 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2793 parent_attr, nl80211_meshconf_params_policy))
2794 return -EINVAL;
2796 rtnl_lock();
2798 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
2799 if (err)
2800 goto out_rtnl;
2802 if (!rdev->ops->set_mesh_params) {
2803 err = -EOPNOTSUPP;
2804 goto out;
2807 /* This makes sure that there aren't more than 32 mesh config
2808 * parameters (otherwise our bitfield scheme would not work.) */
2809 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2811 /* Fill in the params struct */
2812 mask = 0;
2813 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2814 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2816 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2817 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2818 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2819 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2820 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2821 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2822 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2823 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2824 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2825 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2826 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2827 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2828 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2829 nla_get_u8);
2830 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2831 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2832 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2833 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2834 nla_get_u16);
2835 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2836 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2837 nla_get_u32);
2838 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2839 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2840 nla_get_u16);
2841 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2842 dot11MeshHWMPnetDiameterTraversalTime,
2843 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2844 nla_get_u16);
2845 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2846 dot11MeshHWMPRootMode, mask,
2847 NL80211_MESHCONF_HWMP_ROOTMODE,
2848 nla_get_u8);
2850 /* Apply changes */
2851 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2853 out:
2854 /* cleanup */
2855 cfg80211_unlock_rdev(rdev);
2856 dev_put(dev);
2857 out_rtnl:
2858 rtnl_unlock();
2860 return err;
2863 #undef FILL_IN_MESH_PARAM_IF_SET
2865 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2867 struct sk_buff *msg;
2868 void *hdr = NULL;
2869 struct nlattr *nl_reg_rules;
2870 unsigned int i;
2871 int err = -EINVAL;
2873 mutex_lock(&cfg80211_mutex);
2875 if (!cfg80211_regdomain)
2876 goto out;
2878 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2879 if (!msg) {
2880 err = -ENOBUFS;
2881 goto out;
2884 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2885 NL80211_CMD_GET_REG);
2886 if (!hdr)
2887 goto nla_put_failure;
2889 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2890 cfg80211_regdomain->alpha2);
2892 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2893 if (!nl_reg_rules)
2894 goto nla_put_failure;
2896 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2897 struct nlattr *nl_reg_rule;
2898 const struct ieee80211_reg_rule *reg_rule;
2899 const struct ieee80211_freq_range *freq_range;
2900 const struct ieee80211_power_rule *power_rule;
2902 reg_rule = &cfg80211_regdomain->reg_rules[i];
2903 freq_range = &reg_rule->freq_range;
2904 power_rule = &reg_rule->power_rule;
2906 nl_reg_rule = nla_nest_start(msg, i);
2907 if (!nl_reg_rule)
2908 goto nla_put_failure;
2910 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2911 reg_rule->flags);
2912 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2913 freq_range->start_freq_khz);
2914 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2915 freq_range->end_freq_khz);
2916 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2917 freq_range->max_bandwidth_khz);
2918 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2919 power_rule->max_antenna_gain);
2920 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2921 power_rule->max_eirp);
2923 nla_nest_end(msg, nl_reg_rule);
2926 nla_nest_end(msg, nl_reg_rules);
2928 genlmsg_end(msg, hdr);
2929 err = genlmsg_reply(msg, info);
2930 goto out;
2932 nla_put_failure:
2933 genlmsg_cancel(msg, hdr);
2934 err = -EMSGSIZE;
2935 out:
2936 mutex_unlock(&cfg80211_mutex);
2937 return err;
2940 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2942 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2943 struct nlattr *nl_reg_rule;
2944 char *alpha2 = NULL;
2945 int rem_reg_rules = 0, r = 0;
2946 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2947 struct ieee80211_regdomain *rd = NULL;
2949 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2950 return -EINVAL;
2952 if (!info->attrs[NL80211_ATTR_REG_RULES])
2953 return -EINVAL;
2955 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2957 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2958 rem_reg_rules) {
2959 num_rules++;
2960 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2961 return -EINVAL;
2964 mutex_lock(&cfg80211_mutex);
2966 if (!reg_is_valid_request(alpha2)) {
2967 r = -EINVAL;
2968 goto bad_reg;
2971 size_of_regd = sizeof(struct ieee80211_regdomain) +
2972 (num_rules * sizeof(struct ieee80211_reg_rule));
2974 rd = kzalloc(size_of_regd, GFP_KERNEL);
2975 if (!rd) {
2976 r = -ENOMEM;
2977 goto bad_reg;
2980 rd->n_reg_rules = num_rules;
2981 rd->alpha2[0] = alpha2[0];
2982 rd->alpha2[1] = alpha2[1];
2984 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2985 rem_reg_rules) {
2986 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2987 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2988 reg_rule_policy);
2989 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2990 if (r)
2991 goto bad_reg;
2993 rule_idx++;
2995 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2996 r = -EINVAL;
2997 goto bad_reg;
3001 BUG_ON(rule_idx != num_rules);
3003 r = set_regdom(rd);
3005 mutex_unlock(&cfg80211_mutex);
3007 return r;
3009 bad_reg:
3010 mutex_unlock(&cfg80211_mutex);
3011 kfree(rd);
3012 return r;
3015 static int validate_scan_freqs(struct nlattr *freqs)
3017 struct nlattr *attr1, *attr2;
3018 int n_channels = 0, tmp1, tmp2;
3020 nla_for_each_nested(attr1, freqs, tmp1) {
3021 n_channels++;
3023 * Some hardware has a limited channel list for
3024 * scanning, and it is pretty much nonsensical
3025 * to scan for a channel twice, so disallow that
3026 * and don't require drivers to check that the
3027 * channel list they get isn't longer than what
3028 * they can scan, as long as they can scan all
3029 * the channels they registered at once.
3031 nla_for_each_nested(attr2, freqs, tmp2)
3032 if (attr1 != attr2 &&
3033 nla_get_u32(attr1) == nla_get_u32(attr2))
3034 return 0;
3037 return n_channels;
3040 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3042 struct cfg80211_registered_device *rdev;
3043 struct net_device *dev;
3044 struct cfg80211_scan_request *request;
3045 struct cfg80211_ssid *ssid;
3046 struct ieee80211_channel *channel;
3047 struct nlattr *attr;
3048 struct wiphy *wiphy;
3049 int err, tmp, n_ssids = 0, n_channels, i;
3050 enum ieee80211_band band;
3051 size_t ie_len;
3053 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3054 return -EINVAL;
3056 rtnl_lock();
3058 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3059 if (err)
3060 goto out_rtnl;
3062 wiphy = &rdev->wiphy;
3064 if (!rdev->ops->scan) {
3065 err = -EOPNOTSUPP;
3066 goto out;
3069 if (!netif_running(dev)) {
3070 err = -ENETDOWN;
3071 goto out;
3074 if (rdev->scan_req) {
3075 err = -EBUSY;
3076 goto out;
3079 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3080 n_channels = validate_scan_freqs(
3081 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3082 if (!n_channels) {
3083 err = -EINVAL;
3084 goto out;
3086 } else {
3087 n_channels = 0;
3089 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3090 if (wiphy->bands[band])
3091 n_channels += wiphy->bands[band]->n_channels;
3094 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3095 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3096 n_ssids++;
3098 if (n_ssids > wiphy->max_scan_ssids) {
3099 err = -EINVAL;
3100 goto out;
3103 if (info->attrs[NL80211_ATTR_IE])
3104 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3105 else
3106 ie_len = 0;
3108 if (ie_len > wiphy->max_scan_ie_len) {
3109 err = -EINVAL;
3110 goto out;
3113 request = kzalloc(sizeof(*request)
3114 + sizeof(*ssid) * n_ssids
3115 + sizeof(channel) * n_channels
3116 + ie_len, GFP_KERNEL);
3117 if (!request) {
3118 err = -ENOMEM;
3119 goto out;
3122 if (n_ssids)
3123 request->ssids = (void *)&request->channels[n_channels];
3124 request->n_ssids = n_ssids;
3125 if (ie_len) {
3126 if (request->ssids)
3127 request->ie = (void *)(request->ssids + n_ssids);
3128 else
3129 request->ie = (void *)(request->channels + n_channels);
3132 i = 0;
3133 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3134 /* user specified, bail out if channel not found */
3135 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3136 struct ieee80211_channel *chan;
3138 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3140 if (!chan) {
3141 err = -EINVAL;
3142 goto out_free;
3145 /* ignore disabled channels */
3146 if (chan->flags & IEEE80211_CHAN_DISABLED)
3147 continue;
3149 request->channels[i] = chan;
3150 i++;
3152 } else {
3153 /* all channels */
3154 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3155 int j;
3156 if (!wiphy->bands[band])
3157 continue;
3158 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3159 struct ieee80211_channel *chan;
3161 chan = &wiphy->bands[band]->channels[j];
3163 if (chan->flags & IEEE80211_CHAN_DISABLED)
3164 continue;
3166 request->channels[i] = chan;
3167 i++;
3172 if (!i) {
3173 err = -EINVAL;
3174 goto out_free;
3177 request->n_channels = i;
3179 i = 0;
3180 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3181 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3182 request->ssids[i].ssid_len = nla_len(attr);
3183 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3184 err = -EINVAL;
3185 goto out_free;
3187 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3188 i++;
3192 if (info->attrs[NL80211_ATTR_IE]) {
3193 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3194 memcpy((void *)request->ie,
3195 nla_data(info->attrs[NL80211_ATTR_IE]),
3196 request->ie_len);
3199 request->dev = dev;
3200 request->wiphy = &rdev->wiphy;
3202 rdev->scan_req = request;
3203 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3205 if (!err) {
3206 nl80211_send_scan_start(rdev, dev);
3207 dev_hold(dev);
3210 out_free:
3211 if (err) {
3212 rdev->scan_req = NULL;
3213 kfree(request);
3215 out:
3216 cfg80211_unlock_rdev(rdev);
3217 dev_put(dev);
3218 out_rtnl:
3219 rtnl_unlock();
3221 return err;
3224 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3225 struct cfg80211_registered_device *rdev,
3226 struct wireless_dev *wdev,
3227 struct cfg80211_internal_bss *intbss)
3229 struct cfg80211_bss *res = &intbss->pub;
3230 void *hdr;
3231 struct nlattr *bss;
3232 int i;
3234 ASSERT_WDEV_LOCK(wdev);
3236 hdr = nl80211hdr_put(msg, pid, seq, flags,
3237 NL80211_CMD_NEW_SCAN_RESULTS);
3238 if (!hdr)
3239 return -1;
3241 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3244 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3245 if (!bss)
3246 goto nla_put_failure;
3247 if (!is_zero_ether_addr(res->bssid))
3248 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3249 if (res->information_elements && res->len_information_elements)
3250 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3251 res->len_information_elements,
3252 res->information_elements);
3253 if (res->beacon_ies && res->len_beacon_ies &&
3254 res->beacon_ies != res->information_elements)
3255 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3256 res->len_beacon_ies, res->beacon_ies);
3257 if (res->tsf)
3258 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3259 if (res->beacon_interval)
3260 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3261 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3262 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3263 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3264 jiffies_to_msecs(jiffies - intbss->ts));
3266 switch (rdev->wiphy.signal_type) {
3267 case CFG80211_SIGNAL_TYPE_MBM:
3268 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3269 break;
3270 case CFG80211_SIGNAL_TYPE_UNSPEC:
3271 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3272 break;
3273 default:
3274 break;
3277 switch (wdev->iftype) {
3278 case NL80211_IFTYPE_STATION:
3279 if (intbss == wdev->current_bss)
3280 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3281 NL80211_BSS_STATUS_ASSOCIATED);
3282 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3283 if (intbss != wdev->auth_bsses[i])
3284 continue;
3285 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3286 NL80211_BSS_STATUS_AUTHENTICATED);
3287 break;
3289 break;
3290 case NL80211_IFTYPE_ADHOC:
3291 if (intbss == wdev->current_bss)
3292 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3293 NL80211_BSS_STATUS_IBSS_JOINED);
3294 break;
3295 default:
3296 break;
3299 nla_nest_end(msg, bss);
3301 return genlmsg_end(msg, hdr);
3303 nla_put_failure:
3304 genlmsg_cancel(msg, hdr);
3305 return -EMSGSIZE;
3308 static int nl80211_dump_scan(struct sk_buff *skb,
3309 struct netlink_callback *cb)
3311 struct cfg80211_registered_device *rdev;
3312 struct net_device *dev;
3313 struct cfg80211_internal_bss *scan;
3314 struct wireless_dev *wdev;
3315 int ifidx = cb->args[0];
3316 int start = cb->args[1], idx = 0;
3317 int err;
3319 if (!ifidx)
3320 ifidx = nl80211_get_ifidx(cb);
3321 if (ifidx < 0)
3322 return ifidx;
3323 cb->args[0] = ifidx;
3325 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
3326 if (!dev)
3327 return -ENODEV;
3329 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3330 if (IS_ERR(rdev)) {
3331 err = PTR_ERR(rdev);
3332 goto out_put_netdev;
3335 wdev = dev->ieee80211_ptr;
3337 wdev_lock(wdev);
3338 spin_lock_bh(&rdev->bss_lock);
3339 cfg80211_bss_expire(rdev);
3341 list_for_each_entry(scan, &rdev->bss_list, list) {
3342 if (++idx <= start)
3343 continue;
3344 if (nl80211_send_bss(skb,
3345 NETLINK_CB(cb->skb).pid,
3346 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3347 rdev, wdev, scan) < 0) {
3348 idx--;
3349 goto out;
3353 out:
3354 spin_unlock_bh(&rdev->bss_lock);
3355 wdev_unlock(wdev);
3357 cb->args[1] = idx;
3358 err = skb->len;
3359 cfg80211_unlock_rdev(rdev);
3360 out_put_netdev:
3361 dev_put(dev);
3363 return err;
3366 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3367 int flags, struct net_device *dev,
3368 struct survey_info *survey)
3370 void *hdr;
3371 struct nlattr *infoattr;
3373 /* Survey without a channel doesn't make sense */
3374 if (!survey->channel)
3375 return -EINVAL;
3377 hdr = nl80211hdr_put(msg, pid, seq, flags,
3378 NL80211_CMD_NEW_SURVEY_RESULTS);
3379 if (!hdr)
3380 return -ENOMEM;
3382 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3384 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3385 if (!infoattr)
3386 goto nla_put_failure;
3388 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3389 survey->channel->center_freq);
3390 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3391 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3392 survey->noise);
3394 nla_nest_end(msg, infoattr);
3396 return genlmsg_end(msg, hdr);
3398 nla_put_failure:
3399 genlmsg_cancel(msg, hdr);
3400 return -EMSGSIZE;
3403 static int nl80211_dump_survey(struct sk_buff *skb,
3404 struct netlink_callback *cb)
3406 struct survey_info survey;
3407 struct cfg80211_registered_device *dev;
3408 struct net_device *netdev;
3409 int ifidx = cb->args[0];
3410 int survey_idx = cb->args[1];
3411 int res;
3413 if (!ifidx)
3414 ifidx = nl80211_get_ifidx(cb);
3415 if (ifidx < 0)
3416 return ifidx;
3417 cb->args[0] = ifidx;
3419 rtnl_lock();
3421 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3422 if (!netdev) {
3423 res = -ENODEV;
3424 goto out_rtnl;
3427 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3428 if (IS_ERR(dev)) {
3429 res = PTR_ERR(dev);
3430 goto out_rtnl;
3433 if (!dev->ops->dump_survey) {
3434 res = -EOPNOTSUPP;
3435 goto out_err;
3438 while (1) {
3439 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3440 &survey);
3441 if (res == -ENOENT)
3442 break;
3443 if (res)
3444 goto out_err;
3446 if (nl80211_send_survey(skb,
3447 NETLINK_CB(cb->skb).pid,
3448 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3449 netdev,
3450 &survey) < 0)
3451 goto out;
3452 survey_idx++;
3455 out:
3456 cb->args[1] = survey_idx;
3457 res = skb->len;
3458 out_err:
3459 cfg80211_unlock_rdev(dev);
3460 out_rtnl:
3461 rtnl_unlock();
3463 return res;
3466 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3468 return auth_type <= NL80211_AUTHTYPE_MAX;
3471 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3473 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3474 NL80211_WPA_VERSION_2));
3477 static bool nl80211_valid_akm_suite(u32 akm)
3479 return akm == WLAN_AKM_SUITE_8021X ||
3480 akm == WLAN_AKM_SUITE_PSK;
3483 static bool nl80211_valid_cipher_suite(u32 cipher)
3485 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3486 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3487 cipher == WLAN_CIPHER_SUITE_TKIP ||
3488 cipher == WLAN_CIPHER_SUITE_CCMP ||
3489 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3493 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3495 struct cfg80211_registered_device *rdev;
3496 struct net_device *dev;
3497 struct ieee80211_channel *chan;
3498 const u8 *bssid, *ssid, *ie = NULL;
3499 int err, ssid_len, ie_len = 0;
3500 enum nl80211_auth_type auth_type;
3501 struct key_parse key;
3502 bool local_state_change;
3504 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3505 return -EINVAL;
3507 if (!info->attrs[NL80211_ATTR_MAC])
3508 return -EINVAL;
3510 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3511 return -EINVAL;
3513 if (!info->attrs[NL80211_ATTR_SSID])
3514 return -EINVAL;
3516 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3517 return -EINVAL;
3519 err = nl80211_parse_key(info, &key);
3520 if (err)
3521 return err;
3523 if (key.idx >= 0) {
3524 if (!key.p.key || !key.p.key_len)
3525 return -EINVAL;
3526 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3527 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3528 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3529 key.p.key_len != WLAN_KEY_LEN_WEP104))
3530 return -EINVAL;
3531 if (key.idx > 4)
3532 return -EINVAL;
3533 } else {
3534 key.p.key_len = 0;
3535 key.p.key = NULL;
3538 rtnl_lock();
3540 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3541 if (err)
3542 goto unlock_rtnl;
3544 if (!rdev->ops->auth) {
3545 err = -EOPNOTSUPP;
3546 goto out;
3549 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3550 err = -EOPNOTSUPP;
3551 goto out;
3554 if (!netif_running(dev)) {
3555 err = -ENETDOWN;
3556 goto out;
3559 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3560 chan = ieee80211_get_channel(&rdev->wiphy,
3561 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3562 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3563 err = -EINVAL;
3564 goto out;
3567 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3568 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3570 if (info->attrs[NL80211_ATTR_IE]) {
3571 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3572 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3575 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3576 if (!nl80211_valid_auth_type(auth_type)) {
3577 err = -EINVAL;
3578 goto out;
3581 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3583 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3584 ssid, ssid_len, ie, ie_len,
3585 key.p.key, key.p.key_len, key.idx,
3586 local_state_change);
3588 out:
3589 cfg80211_unlock_rdev(rdev);
3590 dev_put(dev);
3591 unlock_rtnl:
3592 rtnl_unlock();
3593 return err;
3596 static int nl80211_crypto_settings(struct genl_info *info,
3597 struct cfg80211_crypto_settings *settings,
3598 int cipher_limit)
3600 memset(settings, 0, sizeof(*settings));
3602 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3604 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3605 void *data;
3606 int len, i;
3608 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3609 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3610 settings->n_ciphers_pairwise = len / sizeof(u32);
3612 if (len % sizeof(u32))
3613 return -EINVAL;
3615 if (settings->n_ciphers_pairwise > cipher_limit)
3616 return -EINVAL;
3618 memcpy(settings->ciphers_pairwise, data, len);
3620 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3621 if (!nl80211_valid_cipher_suite(
3622 settings->ciphers_pairwise[i]))
3623 return -EINVAL;
3626 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3627 settings->cipher_group =
3628 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3629 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3630 return -EINVAL;
3633 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3634 settings->wpa_versions =
3635 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3636 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3637 return -EINVAL;
3640 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3641 void *data;
3642 int len, i;
3644 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3645 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3646 settings->n_akm_suites = len / sizeof(u32);
3648 if (len % sizeof(u32))
3649 return -EINVAL;
3651 memcpy(settings->akm_suites, data, len);
3653 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3654 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3655 return -EINVAL;
3658 return 0;
3661 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3663 struct cfg80211_registered_device *rdev;
3664 struct net_device *dev;
3665 struct cfg80211_crypto_settings crypto;
3666 struct ieee80211_channel *chan;
3667 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
3668 int err, ssid_len, ie_len = 0;
3669 bool use_mfp = false;
3671 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3672 return -EINVAL;
3674 if (!info->attrs[NL80211_ATTR_MAC] ||
3675 !info->attrs[NL80211_ATTR_SSID] ||
3676 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
3677 return -EINVAL;
3679 rtnl_lock();
3681 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3682 if (err)
3683 goto unlock_rtnl;
3685 if (!rdev->ops->assoc) {
3686 err = -EOPNOTSUPP;
3687 goto out;
3690 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3691 err = -EOPNOTSUPP;
3692 goto out;
3695 if (!netif_running(dev)) {
3696 err = -ENETDOWN;
3697 goto out;
3700 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3702 chan = ieee80211_get_channel(&rdev->wiphy,
3703 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3704 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3705 err = -EINVAL;
3706 goto out;
3709 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3710 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3712 if (info->attrs[NL80211_ATTR_IE]) {
3713 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3714 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3717 if (info->attrs[NL80211_ATTR_USE_MFP]) {
3718 enum nl80211_mfp mfp =
3719 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3720 if (mfp == NL80211_MFP_REQUIRED)
3721 use_mfp = true;
3722 else if (mfp != NL80211_MFP_NO) {
3723 err = -EINVAL;
3724 goto out;
3728 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3729 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3731 err = nl80211_crypto_settings(info, &crypto, 1);
3732 if (!err)
3733 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3734 ssid, ssid_len, ie, ie_len, use_mfp,
3735 &crypto);
3737 out:
3738 cfg80211_unlock_rdev(rdev);
3739 dev_put(dev);
3740 unlock_rtnl:
3741 rtnl_unlock();
3742 return err;
3745 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3747 struct cfg80211_registered_device *rdev;
3748 struct net_device *dev;
3749 const u8 *ie = NULL, *bssid;
3750 int err, ie_len = 0;
3751 u16 reason_code;
3752 bool local_state_change;
3754 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3755 return -EINVAL;
3757 if (!info->attrs[NL80211_ATTR_MAC])
3758 return -EINVAL;
3760 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3761 return -EINVAL;
3763 rtnl_lock();
3765 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3766 if (err)
3767 goto unlock_rtnl;
3769 if (!rdev->ops->deauth) {
3770 err = -EOPNOTSUPP;
3771 goto out;
3774 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3775 err = -EOPNOTSUPP;
3776 goto out;
3779 if (!netif_running(dev)) {
3780 err = -ENETDOWN;
3781 goto out;
3784 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3786 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3787 if (reason_code == 0) {
3788 /* Reason Code 0 is reserved */
3789 err = -EINVAL;
3790 goto out;
3793 if (info->attrs[NL80211_ATTR_IE]) {
3794 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3795 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3798 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3800 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3801 local_state_change);
3803 out:
3804 cfg80211_unlock_rdev(rdev);
3805 dev_put(dev);
3806 unlock_rtnl:
3807 rtnl_unlock();
3808 return err;
3811 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3813 struct cfg80211_registered_device *rdev;
3814 struct net_device *dev;
3815 const u8 *ie = NULL, *bssid;
3816 int err, ie_len = 0;
3817 u16 reason_code;
3818 bool local_state_change;
3820 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3821 return -EINVAL;
3823 if (!info->attrs[NL80211_ATTR_MAC])
3824 return -EINVAL;
3826 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3827 return -EINVAL;
3829 rtnl_lock();
3831 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3832 if (err)
3833 goto unlock_rtnl;
3835 if (!rdev->ops->disassoc) {
3836 err = -EOPNOTSUPP;
3837 goto out;
3840 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3841 err = -EOPNOTSUPP;
3842 goto out;
3845 if (!netif_running(dev)) {
3846 err = -ENETDOWN;
3847 goto out;
3850 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3852 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3853 if (reason_code == 0) {
3854 /* Reason Code 0 is reserved */
3855 err = -EINVAL;
3856 goto out;
3859 if (info->attrs[NL80211_ATTR_IE]) {
3860 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3861 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3864 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3866 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3867 local_state_change);
3869 out:
3870 cfg80211_unlock_rdev(rdev);
3871 dev_put(dev);
3872 unlock_rtnl:
3873 rtnl_unlock();
3874 return err;
3877 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3879 struct cfg80211_registered_device *rdev;
3880 struct net_device *dev;
3881 struct cfg80211_ibss_params ibss;
3882 struct wiphy *wiphy;
3883 struct cfg80211_cached_keys *connkeys = NULL;
3884 int err;
3886 memset(&ibss, 0, sizeof(ibss));
3888 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3889 return -EINVAL;
3891 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3892 !info->attrs[NL80211_ATTR_SSID] ||
3893 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3894 return -EINVAL;
3896 ibss.beacon_interval = 100;
3898 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3899 ibss.beacon_interval =
3900 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3901 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3902 return -EINVAL;
3905 rtnl_lock();
3907 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3908 if (err)
3909 goto unlock_rtnl;
3911 if (!rdev->ops->join_ibss) {
3912 err = -EOPNOTSUPP;
3913 goto out;
3916 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3917 err = -EOPNOTSUPP;
3918 goto out;
3921 if (!netif_running(dev)) {
3922 err = -ENETDOWN;
3923 goto out;
3926 wiphy = &rdev->wiphy;
3928 if (info->attrs[NL80211_ATTR_MAC])
3929 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3930 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3931 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3933 if (info->attrs[NL80211_ATTR_IE]) {
3934 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3935 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3938 ibss.channel = ieee80211_get_channel(wiphy,
3939 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3940 if (!ibss.channel ||
3941 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3942 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3943 err = -EINVAL;
3944 goto out;
3947 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3948 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3950 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3951 connkeys = nl80211_parse_connkeys(rdev,
3952 info->attrs[NL80211_ATTR_KEYS]);
3953 if (IS_ERR(connkeys)) {
3954 err = PTR_ERR(connkeys);
3955 connkeys = NULL;
3956 goto out;
3960 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
3962 out:
3963 cfg80211_unlock_rdev(rdev);
3964 dev_put(dev);
3965 unlock_rtnl:
3966 if (err)
3967 kfree(connkeys);
3968 rtnl_unlock();
3969 return err;
3972 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3974 struct cfg80211_registered_device *rdev;
3975 struct net_device *dev;
3976 int err;
3978 rtnl_lock();
3980 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
3981 if (err)
3982 goto unlock_rtnl;
3984 if (!rdev->ops->leave_ibss) {
3985 err = -EOPNOTSUPP;
3986 goto out;
3989 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3990 err = -EOPNOTSUPP;
3991 goto out;
3994 if (!netif_running(dev)) {
3995 err = -ENETDOWN;
3996 goto out;
3999 err = cfg80211_leave_ibss(rdev, dev, false);
4001 out:
4002 cfg80211_unlock_rdev(rdev);
4003 dev_put(dev);
4004 unlock_rtnl:
4005 rtnl_unlock();
4006 return err;
4009 #ifdef CONFIG_NL80211_TESTMODE
4010 static struct genl_multicast_group nl80211_testmode_mcgrp = {
4011 .name = "testmode",
4014 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4016 struct cfg80211_registered_device *rdev;
4017 int err;
4019 if (!info->attrs[NL80211_ATTR_TESTDATA])
4020 return -EINVAL;
4022 rtnl_lock();
4024 rdev = cfg80211_get_dev_from_info(info);
4025 if (IS_ERR(rdev)) {
4026 err = PTR_ERR(rdev);
4027 goto unlock_rtnl;
4030 err = -EOPNOTSUPP;
4031 if (rdev->ops->testmode_cmd) {
4032 rdev->testmode_info = info;
4033 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4034 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4035 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4036 rdev->testmode_info = NULL;
4039 cfg80211_unlock_rdev(rdev);
4041 unlock_rtnl:
4042 rtnl_unlock();
4043 return err;
4046 static struct sk_buff *
4047 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4048 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4050 struct sk_buff *skb;
4051 void *hdr;
4052 struct nlattr *data;
4054 skb = nlmsg_new(approxlen + 100, gfp);
4055 if (!skb)
4056 return NULL;
4058 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4059 if (!hdr) {
4060 kfree_skb(skb);
4061 return NULL;
4064 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4065 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4067 ((void **)skb->cb)[0] = rdev;
4068 ((void **)skb->cb)[1] = hdr;
4069 ((void **)skb->cb)[2] = data;
4071 return skb;
4073 nla_put_failure:
4074 kfree_skb(skb);
4075 return NULL;
4078 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4079 int approxlen)
4081 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4083 if (WARN_ON(!rdev->testmode_info))
4084 return NULL;
4086 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4087 rdev->testmode_info->snd_pid,
4088 rdev->testmode_info->snd_seq,
4089 GFP_KERNEL);
4091 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4093 int cfg80211_testmode_reply(struct sk_buff *skb)
4095 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4096 void *hdr = ((void **)skb->cb)[1];
4097 struct nlattr *data = ((void **)skb->cb)[2];
4099 if (WARN_ON(!rdev->testmode_info)) {
4100 kfree_skb(skb);
4101 return -EINVAL;
4104 nla_nest_end(skb, data);
4105 genlmsg_end(skb, hdr);
4106 return genlmsg_reply(skb, rdev->testmode_info);
4108 EXPORT_SYMBOL(cfg80211_testmode_reply);
4110 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4111 int approxlen, gfp_t gfp)
4113 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4115 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4117 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4119 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4121 void *hdr = ((void **)skb->cb)[1];
4122 struct nlattr *data = ((void **)skb->cb)[2];
4124 nla_nest_end(skb, data);
4125 genlmsg_end(skb, hdr);
4126 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4128 EXPORT_SYMBOL(cfg80211_testmode_event);
4129 #endif
4131 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4133 struct cfg80211_registered_device *rdev;
4134 struct net_device *dev;
4135 struct cfg80211_connect_params connect;
4136 struct wiphy *wiphy;
4137 struct cfg80211_cached_keys *connkeys = NULL;
4138 int err;
4140 memset(&connect, 0, sizeof(connect));
4142 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4143 return -EINVAL;
4145 if (!info->attrs[NL80211_ATTR_SSID] ||
4146 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4147 return -EINVAL;
4149 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4150 connect.auth_type =
4151 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4152 if (!nl80211_valid_auth_type(connect.auth_type))
4153 return -EINVAL;
4154 } else
4155 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4157 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4159 err = nl80211_crypto_settings(info, &connect.crypto,
4160 NL80211_MAX_NR_CIPHER_SUITES);
4161 if (err)
4162 return err;
4163 rtnl_lock();
4165 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4166 if (err)
4167 goto unlock_rtnl;
4169 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4170 err = -EOPNOTSUPP;
4171 goto out;
4174 if (!netif_running(dev)) {
4175 err = -ENETDOWN;
4176 goto out;
4179 wiphy = &rdev->wiphy;
4181 if (info->attrs[NL80211_ATTR_MAC])
4182 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4183 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4184 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4186 if (info->attrs[NL80211_ATTR_IE]) {
4187 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4188 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4191 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4192 connect.channel =
4193 ieee80211_get_channel(wiphy,
4194 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4195 if (!connect.channel ||
4196 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4197 err = -EINVAL;
4198 goto out;
4202 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4203 connkeys = nl80211_parse_connkeys(rdev,
4204 info->attrs[NL80211_ATTR_KEYS]);
4205 if (IS_ERR(connkeys)) {
4206 err = PTR_ERR(connkeys);
4207 connkeys = NULL;
4208 goto out;
4212 err = cfg80211_connect(rdev, dev, &connect, connkeys);
4214 out:
4215 cfg80211_unlock_rdev(rdev);
4216 dev_put(dev);
4217 unlock_rtnl:
4218 if (err)
4219 kfree(connkeys);
4220 rtnl_unlock();
4221 return err;
4224 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4226 struct cfg80211_registered_device *rdev;
4227 struct net_device *dev;
4228 int err;
4229 u16 reason;
4231 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4232 reason = WLAN_REASON_DEAUTH_LEAVING;
4233 else
4234 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4236 if (reason == 0)
4237 return -EINVAL;
4239 rtnl_lock();
4241 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4242 if (err)
4243 goto unlock_rtnl;
4245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4246 err = -EOPNOTSUPP;
4247 goto out;
4250 if (!netif_running(dev)) {
4251 err = -ENETDOWN;
4252 goto out;
4255 err = cfg80211_disconnect(rdev, dev, reason, true);
4257 out:
4258 cfg80211_unlock_rdev(rdev);
4259 dev_put(dev);
4260 unlock_rtnl:
4261 rtnl_unlock();
4262 return err;
4265 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4267 struct cfg80211_registered_device *rdev;
4268 struct net *net;
4269 int err;
4270 u32 pid;
4272 if (!info->attrs[NL80211_ATTR_PID])
4273 return -EINVAL;
4275 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4277 rtnl_lock();
4279 rdev = cfg80211_get_dev_from_info(info);
4280 if (IS_ERR(rdev)) {
4281 err = PTR_ERR(rdev);
4282 goto out_rtnl;
4285 net = get_net_ns_by_pid(pid);
4286 if (IS_ERR(net)) {
4287 err = PTR_ERR(net);
4288 goto out;
4291 err = 0;
4293 /* check if anything to do */
4294 if (net_eq(wiphy_net(&rdev->wiphy), net))
4295 goto out_put_net;
4297 err = cfg80211_switch_netns(rdev, net);
4298 out_put_net:
4299 put_net(net);
4300 out:
4301 cfg80211_unlock_rdev(rdev);
4302 out_rtnl:
4303 rtnl_unlock();
4304 return err;
4307 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4309 struct cfg80211_registered_device *rdev;
4310 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4311 struct cfg80211_pmksa *pmksa) = NULL;
4312 int err;
4313 struct net_device *dev;
4314 struct cfg80211_pmksa pmksa;
4316 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4318 if (!info->attrs[NL80211_ATTR_MAC])
4319 return -EINVAL;
4321 if (!info->attrs[NL80211_ATTR_PMKID])
4322 return -EINVAL;
4324 rtnl_lock();
4326 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4327 if (err)
4328 goto out_rtnl;
4330 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4331 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4333 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4334 err = -EOPNOTSUPP;
4335 goto out;
4338 switch (info->genlhdr->cmd) {
4339 case NL80211_CMD_SET_PMKSA:
4340 rdev_ops = rdev->ops->set_pmksa;
4341 break;
4342 case NL80211_CMD_DEL_PMKSA:
4343 rdev_ops = rdev->ops->del_pmksa;
4344 break;
4345 default:
4346 WARN_ON(1);
4347 break;
4350 if (!rdev_ops) {
4351 err = -EOPNOTSUPP;
4352 goto out;
4355 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4357 out:
4358 cfg80211_unlock_rdev(rdev);
4359 dev_put(dev);
4360 out_rtnl:
4361 rtnl_unlock();
4363 return err;
4366 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4368 struct cfg80211_registered_device *rdev;
4369 int err;
4370 struct net_device *dev;
4372 rtnl_lock();
4374 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4375 if (err)
4376 goto out_rtnl;
4378 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4379 err = -EOPNOTSUPP;
4380 goto out;
4383 if (!rdev->ops->flush_pmksa) {
4384 err = -EOPNOTSUPP;
4385 goto out;
4388 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4390 out:
4391 cfg80211_unlock_rdev(rdev);
4392 dev_put(dev);
4393 out_rtnl:
4394 rtnl_unlock();
4396 return err;
4400 static int nl80211_remain_on_channel(struct sk_buff *skb,
4401 struct genl_info *info)
4403 struct cfg80211_registered_device *rdev;
4404 struct net_device *dev;
4405 struct ieee80211_channel *chan;
4406 struct sk_buff *msg;
4407 void *hdr;
4408 u64 cookie;
4409 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4410 u32 freq, duration;
4411 int err;
4413 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4414 !info->attrs[NL80211_ATTR_DURATION])
4415 return -EINVAL;
4417 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4420 * We should be on that channel for at least one jiffie,
4421 * and more than 5 seconds seems excessive.
4423 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4424 return -EINVAL;
4426 rtnl_lock();
4428 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4429 if (err)
4430 goto unlock_rtnl;
4432 if (!rdev->ops->remain_on_channel) {
4433 err = -EOPNOTSUPP;
4434 goto out;
4437 if (!netif_running(dev)) {
4438 err = -ENETDOWN;
4439 goto out;
4442 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4443 channel_type = nla_get_u32(
4444 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4445 if (channel_type != NL80211_CHAN_NO_HT &&
4446 channel_type != NL80211_CHAN_HT20 &&
4447 channel_type != NL80211_CHAN_HT40PLUS &&
4448 channel_type != NL80211_CHAN_HT40MINUS) {
4449 err = -EINVAL;
4450 goto out;
4454 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4455 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4456 if (chan == NULL) {
4457 err = -EINVAL;
4458 goto out;
4461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4462 if (!msg) {
4463 err = -ENOMEM;
4464 goto out;
4467 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4468 NL80211_CMD_REMAIN_ON_CHANNEL);
4470 if (IS_ERR(hdr)) {
4471 err = PTR_ERR(hdr);
4472 goto free_msg;
4475 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4476 channel_type, duration, &cookie);
4478 if (err)
4479 goto free_msg;
4481 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4483 genlmsg_end(msg, hdr);
4484 err = genlmsg_reply(msg, info);
4485 goto out;
4487 nla_put_failure:
4488 err = -ENOBUFS;
4489 free_msg:
4490 nlmsg_free(msg);
4491 out:
4492 cfg80211_unlock_rdev(rdev);
4493 dev_put(dev);
4494 unlock_rtnl:
4495 rtnl_unlock();
4496 return err;
4499 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4500 struct genl_info *info)
4502 struct cfg80211_registered_device *rdev;
4503 struct net_device *dev;
4504 u64 cookie;
4505 int err;
4507 if (!info->attrs[NL80211_ATTR_COOKIE])
4508 return -EINVAL;
4510 rtnl_lock();
4512 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4513 if (err)
4514 goto unlock_rtnl;
4516 if (!rdev->ops->cancel_remain_on_channel) {
4517 err = -EOPNOTSUPP;
4518 goto out;
4521 if (!netif_running(dev)) {
4522 err = -ENETDOWN;
4523 goto out;
4526 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4528 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4530 out:
4531 cfg80211_unlock_rdev(rdev);
4532 dev_put(dev);
4533 unlock_rtnl:
4534 rtnl_unlock();
4535 return err;
4538 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4539 u8 *rates, u8 rates_len)
4541 u8 i;
4542 u32 mask = 0;
4544 for (i = 0; i < rates_len; i++) {
4545 int rate = (rates[i] & 0x7f) * 5;
4546 int ridx;
4547 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4548 struct ieee80211_rate *srate =
4549 &sband->bitrates[ridx];
4550 if (rate == srate->bitrate) {
4551 mask |= 1 << ridx;
4552 break;
4555 if (ridx == sband->n_bitrates)
4556 return 0; /* rate not found */
4559 return mask;
4562 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4563 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4564 .len = NL80211_MAX_SUPP_RATES },
4567 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4568 struct genl_info *info)
4570 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4571 struct cfg80211_registered_device *rdev;
4572 struct cfg80211_bitrate_mask mask;
4573 int err, rem, i;
4574 struct net_device *dev;
4575 struct nlattr *tx_rates;
4576 struct ieee80211_supported_band *sband;
4578 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4579 return -EINVAL;
4581 rtnl_lock();
4583 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4584 if (err)
4585 goto unlock_rtnl;
4587 if (!rdev->ops->set_bitrate_mask) {
4588 err = -EOPNOTSUPP;
4589 goto unlock;
4592 memset(&mask, 0, sizeof(mask));
4593 /* Default to all rates enabled */
4594 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4595 sband = rdev->wiphy.bands[i];
4596 mask.control[i].legacy =
4597 sband ? (1 << sband->n_bitrates) - 1 : 0;
4601 * The nested attribute uses enum nl80211_band as the index. This maps
4602 * directly to the enum ieee80211_band values used in cfg80211.
4604 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4606 enum ieee80211_band band = nla_type(tx_rates);
4607 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4608 err = -EINVAL;
4609 goto unlock;
4611 sband = rdev->wiphy.bands[band];
4612 if (sband == NULL) {
4613 err = -EINVAL;
4614 goto unlock;
4616 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4617 nla_len(tx_rates), nl80211_txattr_policy);
4618 if (tb[NL80211_TXRATE_LEGACY]) {
4619 mask.control[band].legacy = rateset_to_mask(
4620 sband,
4621 nla_data(tb[NL80211_TXRATE_LEGACY]),
4622 nla_len(tb[NL80211_TXRATE_LEGACY]));
4623 if (mask.control[band].legacy == 0) {
4624 err = -EINVAL;
4625 goto unlock;
4630 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4632 unlock:
4633 dev_put(dev);
4634 cfg80211_unlock_rdev(rdev);
4635 unlock_rtnl:
4636 rtnl_unlock();
4637 return err;
4640 static int nl80211_register_action(struct sk_buff *skb, struct genl_info *info)
4642 struct cfg80211_registered_device *rdev;
4643 struct net_device *dev;
4644 int err;
4646 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4647 return -EINVAL;
4649 if (nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]) < 1)
4650 return -EINVAL;
4652 rtnl_lock();
4654 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4655 if (err)
4656 goto unlock_rtnl;
4658 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4659 err = -EOPNOTSUPP;
4660 goto out;
4663 /* not much point in registering if we can't reply */
4664 if (!rdev->ops->action) {
4665 err = -EOPNOTSUPP;
4666 goto out;
4669 err = cfg80211_mlme_register_action(dev->ieee80211_ptr, info->snd_pid,
4670 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4671 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4672 out:
4673 cfg80211_unlock_rdev(rdev);
4674 dev_put(dev);
4675 unlock_rtnl:
4676 rtnl_unlock();
4677 return err;
4680 static int nl80211_action(struct sk_buff *skb, struct genl_info *info)
4682 struct cfg80211_registered_device *rdev;
4683 struct net_device *dev;
4684 struct ieee80211_channel *chan;
4685 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4686 u32 freq;
4687 int err;
4688 void *hdr;
4689 u64 cookie;
4690 struct sk_buff *msg;
4692 if (!info->attrs[NL80211_ATTR_FRAME] ||
4693 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4694 return -EINVAL;
4696 rtnl_lock();
4698 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4699 if (err)
4700 goto unlock_rtnl;
4702 if (!rdev->ops->action) {
4703 err = -EOPNOTSUPP;
4704 goto out;
4707 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4708 err = -EOPNOTSUPP;
4709 goto out;
4712 if (!netif_running(dev)) {
4713 err = -ENETDOWN;
4714 goto out;
4717 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4718 channel_type = nla_get_u32(
4719 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4720 if (channel_type != NL80211_CHAN_NO_HT &&
4721 channel_type != NL80211_CHAN_HT20 &&
4722 channel_type != NL80211_CHAN_HT40PLUS &&
4723 channel_type != NL80211_CHAN_HT40MINUS) {
4724 err = -EINVAL;
4725 goto out;
4729 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4730 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4731 if (chan == NULL) {
4732 err = -EINVAL;
4733 goto out;
4736 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4737 if (!msg) {
4738 err = -ENOMEM;
4739 goto out;
4742 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4743 NL80211_CMD_ACTION);
4745 if (IS_ERR(hdr)) {
4746 err = PTR_ERR(hdr);
4747 goto free_msg;
4749 err = cfg80211_mlme_action(rdev, dev, chan, channel_type,
4750 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4751 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4752 &cookie);
4753 if (err)
4754 goto free_msg;
4756 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4758 genlmsg_end(msg, hdr);
4759 err = genlmsg_reply(msg, info);
4760 goto out;
4762 nla_put_failure:
4763 err = -ENOBUFS;
4764 free_msg:
4765 nlmsg_free(msg);
4766 out:
4767 cfg80211_unlock_rdev(rdev);
4768 dev_put(dev);
4769 unlock_rtnl:
4770 rtnl_unlock();
4771 return err;
4774 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4776 struct cfg80211_registered_device *rdev;
4777 struct wireless_dev *wdev;
4778 struct net_device *dev;
4779 u8 ps_state;
4780 bool state;
4781 int err;
4783 if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4784 err = -EINVAL;
4785 goto out;
4788 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4790 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4791 err = -EINVAL;
4792 goto out;
4795 rtnl_lock();
4797 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4798 if (err)
4799 goto unlock_rdev;
4801 wdev = dev->ieee80211_ptr;
4803 if (!rdev->ops->set_power_mgmt) {
4804 err = -EOPNOTSUPP;
4805 goto unlock_rdev;
4808 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4810 if (state == wdev->ps)
4811 goto unlock_rdev;
4813 wdev->ps = state;
4815 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
4816 wdev->ps_timeout))
4817 /* assume this means it's off */
4818 wdev->ps = false;
4820 unlock_rdev:
4821 cfg80211_unlock_rdev(rdev);
4822 dev_put(dev);
4823 rtnl_unlock();
4825 out:
4826 return err;
4829 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4831 struct cfg80211_registered_device *rdev;
4832 enum nl80211_ps_state ps_state;
4833 struct wireless_dev *wdev;
4834 struct net_device *dev;
4835 struct sk_buff *msg;
4836 void *hdr;
4837 int err;
4839 rtnl_lock();
4841 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4842 if (err)
4843 goto unlock_rtnl;
4845 wdev = dev->ieee80211_ptr;
4847 if (!rdev->ops->set_power_mgmt) {
4848 err = -EOPNOTSUPP;
4849 goto out;
4852 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4853 if (!msg) {
4854 err = -ENOMEM;
4855 goto out;
4858 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4859 NL80211_CMD_GET_POWER_SAVE);
4860 if (!hdr) {
4861 err = -ENOMEM;
4862 goto free_msg;
4865 if (wdev->ps)
4866 ps_state = NL80211_PS_ENABLED;
4867 else
4868 ps_state = NL80211_PS_DISABLED;
4870 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4872 genlmsg_end(msg, hdr);
4873 err = genlmsg_reply(msg, info);
4874 goto out;
4876 nla_put_failure:
4877 err = -ENOBUFS;
4879 free_msg:
4880 nlmsg_free(msg);
4882 out:
4883 cfg80211_unlock_rdev(rdev);
4884 dev_put(dev);
4886 unlock_rtnl:
4887 rtnl_unlock();
4889 return err;
4892 static struct nla_policy
4893 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4894 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4895 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4896 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4899 static int nl80211_set_cqm_rssi(struct genl_info *info,
4900 s32 threshold, u32 hysteresis)
4902 struct cfg80211_registered_device *rdev;
4903 struct wireless_dev *wdev;
4904 struct net_device *dev;
4905 int err;
4907 if (threshold > 0)
4908 return -EINVAL;
4910 rtnl_lock();
4912 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4913 if (err)
4914 goto unlock_rtnl;
4916 wdev = dev->ieee80211_ptr;
4918 if (!rdev->ops->set_cqm_rssi_config) {
4919 err = -EOPNOTSUPP;
4920 goto unlock_rdev;
4923 if (wdev->iftype != NL80211_IFTYPE_STATION) {
4924 err = -EOPNOTSUPP;
4925 goto unlock_rdev;
4928 err = rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4929 threshold, hysteresis);
4931 unlock_rdev:
4932 cfg80211_unlock_rdev(rdev);
4933 dev_put(dev);
4934 unlock_rtnl:
4935 rtnl_unlock();
4937 return err;
4940 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4942 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4943 struct nlattr *cqm;
4944 int err;
4946 cqm = info->attrs[NL80211_ATTR_CQM];
4947 if (!cqm) {
4948 err = -EINVAL;
4949 goto out;
4952 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4953 nl80211_attr_cqm_policy);
4954 if (err)
4955 goto out;
4957 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4958 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4959 s32 threshold;
4960 u32 hysteresis;
4961 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4962 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4963 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4964 } else
4965 err = -EINVAL;
4967 out:
4968 return err;
4971 static struct genl_ops nl80211_ops[] = {
4973 .cmd = NL80211_CMD_GET_WIPHY,
4974 .doit = nl80211_get_wiphy,
4975 .dumpit = nl80211_dump_wiphy,
4976 .policy = nl80211_policy,
4977 /* can be retrieved by unprivileged users */
4980 .cmd = NL80211_CMD_SET_WIPHY,
4981 .doit = nl80211_set_wiphy,
4982 .policy = nl80211_policy,
4983 .flags = GENL_ADMIN_PERM,
4986 .cmd = NL80211_CMD_GET_INTERFACE,
4987 .doit = nl80211_get_interface,
4988 .dumpit = nl80211_dump_interface,
4989 .policy = nl80211_policy,
4990 /* can be retrieved by unprivileged users */
4993 .cmd = NL80211_CMD_SET_INTERFACE,
4994 .doit = nl80211_set_interface,
4995 .policy = nl80211_policy,
4996 .flags = GENL_ADMIN_PERM,
4999 .cmd = NL80211_CMD_NEW_INTERFACE,
5000 .doit = nl80211_new_interface,
5001 .policy = nl80211_policy,
5002 .flags = GENL_ADMIN_PERM,
5005 .cmd = NL80211_CMD_DEL_INTERFACE,
5006 .doit = nl80211_del_interface,
5007 .policy = nl80211_policy,
5008 .flags = GENL_ADMIN_PERM,
5011 .cmd = NL80211_CMD_GET_KEY,
5012 .doit = nl80211_get_key,
5013 .policy = nl80211_policy,
5014 .flags = GENL_ADMIN_PERM,
5017 .cmd = NL80211_CMD_SET_KEY,
5018 .doit = nl80211_set_key,
5019 .policy = nl80211_policy,
5020 .flags = GENL_ADMIN_PERM,
5023 .cmd = NL80211_CMD_NEW_KEY,
5024 .doit = nl80211_new_key,
5025 .policy = nl80211_policy,
5026 .flags = GENL_ADMIN_PERM,
5029 .cmd = NL80211_CMD_DEL_KEY,
5030 .doit = nl80211_del_key,
5031 .policy = nl80211_policy,
5032 .flags = GENL_ADMIN_PERM,
5035 .cmd = NL80211_CMD_SET_BEACON,
5036 .policy = nl80211_policy,
5037 .flags = GENL_ADMIN_PERM,
5038 .doit = nl80211_addset_beacon,
5041 .cmd = NL80211_CMD_NEW_BEACON,
5042 .policy = nl80211_policy,
5043 .flags = GENL_ADMIN_PERM,
5044 .doit = nl80211_addset_beacon,
5047 .cmd = NL80211_CMD_DEL_BEACON,
5048 .policy = nl80211_policy,
5049 .flags = GENL_ADMIN_PERM,
5050 .doit = nl80211_del_beacon,
5053 .cmd = NL80211_CMD_GET_STATION,
5054 .doit = nl80211_get_station,
5055 .dumpit = nl80211_dump_station,
5056 .policy = nl80211_policy,
5059 .cmd = NL80211_CMD_SET_STATION,
5060 .doit = nl80211_set_station,
5061 .policy = nl80211_policy,
5062 .flags = GENL_ADMIN_PERM,
5065 .cmd = NL80211_CMD_NEW_STATION,
5066 .doit = nl80211_new_station,
5067 .policy = nl80211_policy,
5068 .flags = GENL_ADMIN_PERM,
5071 .cmd = NL80211_CMD_DEL_STATION,
5072 .doit = nl80211_del_station,
5073 .policy = nl80211_policy,
5074 .flags = GENL_ADMIN_PERM,
5077 .cmd = NL80211_CMD_GET_MPATH,
5078 .doit = nl80211_get_mpath,
5079 .dumpit = nl80211_dump_mpath,
5080 .policy = nl80211_policy,
5081 .flags = GENL_ADMIN_PERM,
5084 .cmd = NL80211_CMD_SET_MPATH,
5085 .doit = nl80211_set_mpath,
5086 .policy = nl80211_policy,
5087 .flags = GENL_ADMIN_PERM,
5090 .cmd = NL80211_CMD_NEW_MPATH,
5091 .doit = nl80211_new_mpath,
5092 .policy = nl80211_policy,
5093 .flags = GENL_ADMIN_PERM,
5096 .cmd = NL80211_CMD_DEL_MPATH,
5097 .doit = nl80211_del_mpath,
5098 .policy = nl80211_policy,
5099 .flags = GENL_ADMIN_PERM,
5102 .cmd = NL80211_CMD_SET_BSS,
5103 .doit = nl80211_set_bss,
5104 .policy = nl80211_policy,
5105 .flags = GENL_ADMIN_PERM,
5108 .cmd = NL80211_CMD_GET_REG,
5109 .doit = nl80211_get_reg,
5110 .policy = nl80211_policy,
5111 /* can be retrieved by unprivileged users */
5114 .cmd = NL80211_CMD_SET_REG,
5115 .doit = nl80211_set_reg,
5116 .policy = nl80211_policy,
5117 .flags = GENL_ADMIN_PERM,
5120 .cmd = NL80211_CMD_REQ_SET_REG,
5121 .doit = nl80211_req_set_reg,
5122 .policy = nl80211_policy,
5123 .flags = GENL_ADMIN_PERM,
5126 .cmd = NL80211_CMD_GET_MESH_PARAMS,
5127 .doit = nl80211_get_mesh_params,
5128 .policy = nl80211_policy,
5129 /* can be retrieved by unprivileged users */
5132 .cmd = NL80211_CMD_SET_MESH_PARAMS,
5133 .doit = nl80211_set_mesh_params,
5134 .policy = nl80211_policy,
5135 .flags = GENL_ADMIN_PERM,
5138 .cmd = NL80211_CMD_TRIGGER_SCAN,
5139 .doit = nl80211_trigger_scan,
5140 .policy = nl80211_policy,
5141 .flags = GENL_ADMIN_PERM,
5144 .cmd = NL80211_CMD_GET_SCAN,
5145 .policy = nl80211_policy,
5146 .dumpit = nl80211_dump_scan,
5149 .cmd = NL80211_CMD_AUTHENTICATE,
5150 .doit = nl80211_authenticate,
5151 .policy = nl80211_policy,
5152 .flags = GENL_ADMIN_PERM,
5155 .cmd = NL80211_CMD_ASSOCIATE,
5156 .doit = nl80211_associate,
5157 .policy = nl80211_policy,
5158 .flags = GENL_ADMIN_PERM,
5161 .cmd = NL80211_CMD_DEAUTHENTICATE,
5162 .doit = nl80211_deauthenticate,
5163 .policy = nl80211_policy,
5164 .flags = GENL_ADMIN_PERM,
5167 .cmd = NL80211_CMD_DISASSOCIATE,
5168 .doit = nl80211_disassociate,
5169 .policy = nl80211_policy,
5170 .flags = GENL_ADMIN_PERM,
5173 .cmd = NL80211_CMD_JOIN_IBSS,
5174 .doit = nl80211_join_ibss,
5175 .policy = nl80211_policy,
5176 .flags = GENL_ADMIN_PERM,
5179 .cmd = NL80211_CMD_LEAVE_IBSS,
5180 .doit = nl80211_leave_ibss,
5181 .policy = nl80211_policy,
5182 .flags = GENL_ADMIN_PERM,
5184 #ifdef CONFIG_NL80211_TESTMODE
5186 .cmd = NL80211_CMD_TESTMODE,
5187 .doit = nl80211_testmode_do,
5188 .policy = nl80211_policy,
5189 .flags = GENL_ADMIN_PERM,
5191 #endif
5193 .cmd = NL80211_CMD_CONNECT,
5194 .doit = nl80211_connect,
5195 .policy = nl80211_policy,
5196 .flags = GENL_ADMIN_PERM,
5199 .cmd = NL80211_CMD_DISCONNECT,
5200 .doit = nl80211_disconnect,
5201 .policy = nl80211_policy,
5202 .flags = GENL_ADMIN_PERM,
5205 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5206 .doit = nl80211_wiphy_netns,
5207 .policy = nl80211_policy,
5208 .flags = GENL_ADMIN_PERM,
5211 .cmd = NL80211_CMD_GET_SURVEY,
5212 .policy = nl80211_policy,
5213 .dumpit = nl80211_dump_survey,
5216 .cmd = NL80211_CMD_SET_PMKSA,
5217 .doit = nl80211_setdel_pmksa,
5218 .policy = nl80211_policy,
5219 .flags = GENL_ADMIN_PERM,
5222 .cmd = NL80211_CMD_DEL_PMKSA,
5223 .doit = nl80211_setdel_pmksa,
5224 .policy = nl80211_policy,
5225 .flags = GENL_ADMIN_PERM,
5228 .cmd = NL80211_CMD_FLUSH_PMKSA,
5229 .doit = nl80211_flush_pmksa,
5230 .policy = nl80211_policy,
5231 .flags = GENL_ADMIN_PERM,
5234 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5235 .doit = nl80211_remain_on_channel,
5236 .policy = nl80211_policy,
5237 .flags = GENL_ADMIN_PERM,
5240 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5241 .doit = nl80211_cancel_remain_on_channel,
5242 .policy = nl80211_policy,
5243 .flags = GENL_ADMIN_PERM,
5246 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5247 .doit = nl80211_set_tx_bitrate_mask,
5248 .policy = nl80211_policy,
5249 .flags = GENL_ADMIN_PERM,
5252 .cmd = NL80211_CMD_REGISTER_ACTION,
5253 .doit = nl80211_register_action,
5254 .policy = nl80211_policy,
5255 .flags = GENL_ADMIN_PERM,
5258 .cmd = NL80211_CMD_ACTION,
5259 .doit = nl80211_action,
5260 .policy = nl80211_policy,
5261 .flags = GENL_ADMIN_PERM,
5264 .cmd = NL80211_CMD_SET_POWER_SAVE,
5265 .doit = nl80211_set_power_save,
5266 .policy = nl80211_policy,
5267 .flags = GENL_ADMIN_PERM,
5270 .cmd = NL80211_CMD_GET_POWER_SAVE,
5271 .doit = nl80211_get_power_save,
5272 .policy = nl80211_policy,
5273 /* can be retrieved by unprivileged users */
5276 .cmd = NL80211_CMD_SET_CQM,
5277 .doit = nl80211_set_cqm,
5278 .policy = nl80211_policy,
5279 .flags = GENL_ADMIN_PERM,
5282 .cmd = NL80211_CMD_SET_CHANNEL,
5283 .doit = nl80211_set_channel,
5284 .policy = nl80211_policy,
5285 .flags = GENL_ADMIN_PERM,
5289 static struct genl_multicast_group nl80211_mlme_mcgrp = {
5290 .name = "mlme",
5293 /* multicast groups */
5294 static struct genl_multicast_group nl80211_config_mcgrp = {
5295 .name = "config",
5297 static struct genl_multicast_group nl80211_scan_mcgrp = {
5298 .name = "scan",
5300 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5301 .name = "regulatory",
5304 /* notification functions */
5306 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5308 struct sk_buff *msg;
5310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5311 if (!msg)
5312 return;
5314 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5315 nlmsg_free(msg);
5316 return;
5319 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5320 nl80211_config_mcgrp.id, GFP_KERNEL);
5323 static int nl80211_add_scan_req(struct sk_buff *msg,
5324 struct cfg80211_registered_device *rdev)
5326 struct cfg80211_scan_request *req = rdev->scan_req;
5327 struct nlattr *nest;
5328 int i;
5330 ASSERT_RDEV_LOCK(rdev);
5332 if (WARN_ON(!req))
5333 return 0;
5335 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5336 if (!nest)
5337 goto nla_put_failure;
5338 for (i = 0; i < req->n_ssids; i++)
5339 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5340 nla_nest_end(msg, nest);
5342 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5343 if (!nest)
5344 goto nla_put_failure;
5345 for (i = 0; i < req->n_channels; i++)
5346 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5347 nla_nest_end(msg, nest);
5349 if (req->ie)
5350 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5352 return 0;
5353 nla_put_failure:
5354 return -ENOBUFS;
5357 static int nl80211_send_scan_msg(struct sk_buff *msg,
5358 struct cfg80211_registered_device *rdev,
5359 struct net_device *netdev,
5360 u32 pid, u32 seq, int flags,
5361 u32 cmd)
5363 void *hdr;
5365 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5366 if (!hdr)
5367 return -1;
5369 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5370 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5372 /* ignore errors and send incomplete event anyway */
5373 nl80211_add_scan_req(msg, rdev);
5375 return genlmsg_end(msg, hdr);
5377 nla_put_failure:
5378 genlmsg_cancel(msg, hdr);
5379 return -EMSGSIZE;
5382 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5383 struct net_device *netdev)
5385 struct sk_buff *msg;
5387 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5388 if (!msg)
5389 return;
5391 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5392 NL80211_CMD_TRIGGER_SCAN) < 0) {
5393 nlmsg_free(msg);
5394 return;
5397 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5398 nl80211_scan_mcgrp.id, GFP_KERNEL);
5401 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5402 struct net_device *netdev)
5404 struct sk_buff *msg;
5406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5407 if (!msg)
5408 return;
5410 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5411 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
5412 nlmsg_free(msg);
5413 return;
5416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5417 nl80211_scan_mcgrp.id, GFP_KERNEL);
5420 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5421 struct net_device *netdev)
5423 struct sk_buff *msg;
5425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5426 if (!msg)
5427 return;
5429 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5430 NL80211_CMD_SCAN_ABORTED) < 0) {
5431 nlmsg_free(msg);
5432 return;
5435 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5436 nl80211_scan_mcgrp.id, GFP_KERNEL);
5440 * This can happen on global regulatory changes or device specific settings
5441 * based on custom world regulatory domains.
5443 void nl80211_send_reg_change_event(struct regulatory_request *request)
5445 struct sk_buff *msg;
5446 void *hdr;
5448 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5449 if (!msg)
5450 return;
5452 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5453 if (!hdr) {
5454 nlmsg_free(msg);
5455 return;
5458 /* Userspace can always count this one always being set */
5459 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5461 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5462 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5463 NL80211_REGDOM_TYPE_WORLD);
5464 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5465 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5466 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5467 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5468 request->intersect)
5469 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5470 NL80211_REGDOM_TYPE_INTERSECTION);
5471 else {
5472 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5473 NL80211_REGDOM_TYPE_COUNTRY);
5474 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5477 if (wiphy_idx_valid(request->wiphy_idx))
5478 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5480 if (genlmsg_end(msg, hdr) < 0) {
5481 nlmsg_free(msg);
5482 return;
5485 rcu_read_lock();
5486 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5487 GFP_ATOMIC);
5488 rcu_read_unlock();
5490 return;
5492 nla_put_failure:
5493 genlmsg_cancel(msg, hdr);
5494 nlmsg_free(msg);
5497 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5498 struct net_device *netdev,
5499 const u8 *buf, size_t len,
5500 enum nl80211_commands cmd, gfp_t gfp)
5502 struct sk_buff *msg;
5503 void *hdr;
5505 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5506 if (!msg)
5507 return;
5509 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5510 if (!hdr) {
5511 nlmsg_free(msg);
5512 return;
5515 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5517 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5519 if (genlmsg_end(msg, hdr) < 0) {
5520 nlmsg_free(msg);
5521 return;
5524 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5525 nl80211_mlme_mcgrp.id, gfp);
5526 return;
5528 nla_put_failure:
5529 genlmsg_cancel(msg, hdr);
5530 nlmsg_free(msg);
5533 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
5534 struct net_device *netdev, const u8 *buf,
5535 size_t len, gfp_t gfp)
5537 nl80211_send_mlme_event(rdev, netdev, buf, len,
5538 NL80211_CMD_AUTHENTICATE, gfp);
5541 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5542 struct net_device *netdev, const u8 *buf,
5543 size_t len, gfp_t gfp)
5545 nl80211_send_mlme_event(rdev, netdev, buf, len,
5546 NL80211_CMD_ASSOCIATE, gfp);
5549 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
5550 struct net_device *netdev, const u8 *buf,
5551 size_t len, gfp_t gfp)
5553 nl80211_send_mlme_event(rdev, netdev, buf, len,
5554 NL80211_CMD_DEAUTHENTICATE, gfp);
5557 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5558 struct net_device *netdev, const u8 *buf,
5559 size_t len, gfp_t gfp)
5561 nl80211_send_mlme_event(rdev, netdev, buf, len,
5562 NL80211_CMD_DISASSOCIATE, gfp);
5565 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5566 struct net_device *netdev, int cmd,
5567 const u8 *addr, gfp_t gfp)
5569 struct sk_buff *msg;
5570 void *hdr;
5572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5573 if (!msg)
5574 return;
5576 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5577 if (!hdr) {
5578 nlmsg_free(msg);
5579 return;
5582 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5584 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5585 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5587 if (genlmsg_end(msg, hdr) < 0) {
5588 nlmsg_free(msg);
5589 return;
5592 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5593 nl80211_mlme_mcgrp.id, gfp);
5594 return;
5596 nla_put_failure:
5597 genlmsg_cancel(msg, hdr);
5598 nlmsg_free(msg);
5601 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
5602 struct net_device *netdev, const u8 *addr,
5603 gfp_t gfp)
5605 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
5606 addr, gfp);
5609 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
5610 struct net_device *netdev, const u8 *addr,
5611 gfp_t gfp)
5613 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5614 addr, gfp);
5617 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5618 struct net_device *netdev, const u8 *bssid,
5619 const u8 *req_ie, size_t req_ie_len,
5620 const u8 *resp_ie, size_t resp_ie_len,
5621 u16 status, gfp_t gfp)
5623 struct sk_buff *msg;
5624 void *hdr;
5626 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5627 if (!msg)
5628 return;
5630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5631 if (!hdr) {
5632 nlmsg_free(msg);
5633 return;
5636 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5637 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5638 if (bssid)
5639 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5640 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5641 if (req_ie)
5642 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5643 if (resp_ie)
5644 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5646 if (genlmsg_end(msg, hdr) < 0) {
5647 nlmsg_free(msg);
5648 return;
5651 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5652 nl80211_mlme_mcgrp.id, gfp);
5653 return;
5655 nla_put_failure:
5656 genlmsg_cancel(msg, hdr);
5657 nlmsg_free(msg);
5661 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5662 struct net_device *netdev, const u8 *bssid,
5663 const u8 *req_ie, size_t req_ie_len,
5664 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5666 struct sk_buff *msg;
5667 void *hdr;
5669 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5670 if (!msg)
5671 return;
5673 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5674 if (!hdr) {
5675 nlmsg_free(msg);
5676 return;
5679 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5680 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5681 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5682 if (req_ie)
5683 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5684 if (resp_ie)
5685 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5687 if (genlmsg_end(msg, hdr) < 0) {
5688 nlmsg_free(msg);
5689 return;
5692 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5693 nl80211_mlme_mcgrp.id, gfp);
5694 return;
5696 nla_put_failure:
5697 genlmsg_cancel(msg, hdr);
5698 nlmsg_free(msg);
5702 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5703 struct net_device *netdev, u16 reason,
5704 const u8 *ie, size_t ie_len, bool from_ap)
5706 struct sk_buff *msg;
5707 void *hdr;
5709 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5710 if (!msg)
5711 return;
5713 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5714 if (!hdr) {
5715 nlmsg_free(msg);
5716 return;
5719 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5720 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5721 if (from_ap && reason)
5722 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5723 if (from_ap)
5724 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5725 if (ie)
5726 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5728 if (genlmsg_end(msg, hdr) < 0) {
5729 nlmsg_free(msg);
5730 return;
5733 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5734 nl80211_mlme_mcgrp.id, GFP_KERNEL);
5735 return;
5737 nla_put_failure:
5738 genlmsg_cancel(msg, hdr);
5739 nlmsg_free(msg);
5743 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5744 struct net_device *netdev, const u8 *bssid,
5745 gfp_t gfp)
5747 struct sk_buff *msg;
5748 void *hdr;
5750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5751 if (!msg)
5752 return;
5754 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5755 if (!hdr) {
5756 nlmsg_free(msg);
5757 return;
5760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5762 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5764 if (genlmsg_end(msg, hdr) < 0) {
5765 nlmsg_free(msg);
5766 return;
5769 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5770 nl80211_mlme_mcgrp.id, gfp);
5771 return;
5773 nla_put_failure:
5774 genlmsg_cancel(msg, hdr);
5775 nlmsg_free(msg);
5778 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5779 struct net_device *netdev, const u8 *addr,
5780 enum nl80211_key_type key_type, int key_id,
5781 const u8 *tsc, gfp_t gfp)
5783 struct sk_buff *msg;
5784 void *hdr;
5786 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5787 if (!msg)
5788 return;
5790 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5791 if (!hdr) {
5792 nlmsg_free(msg);
5793 return;
5796 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5797 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5798 if (addr)
5799 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5800 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5801 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5802 if (tsc)
5803 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5805 if (genlmsg_end(msg, hdr) < 0) {
5806 nlmsg_free(msg);
5807 return;
5810 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5811 nl80211_mlme_mcgrp.id, gfp);
5812 return;
5814 nla_put_failure:
5815 genlmsg_cancel(msg, hdr);
5816 nlmsg_free(msg);
5819 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5820 struct ieee80211_channel *channel_before,
5821 struct ieee80211_channel *channel_after)
5823 struct sk_buff *msg;
5824 void *hdr;
5825 struct nlattr *nl_freq;
5827 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
5828 if (!msg)
5829 return;
5831 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5832 if (!hdr) {
5833 nlmsg_free(msg);
5834 return;
5838 * Since we are applying the beacon hint to a wiphy we know its
5839 * wiphy_idx is valid
5841 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5843 /* Before */
5844 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5845 if (!nl_freq)
5846 goto nla_put_failure;
5847 if (nl80211_msg_put_channel(msg, channel_before))
5848 goto nla_put_failure;
5849 nla_nest_end(msg, nl_freq);
5851 /* After */
5852 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5853 if (!nl_freq)
5854 goto nla_put_failure;
5855 if (nl80211_msg_put_channel(msg, channel_after))
5856 goto nla_put_failure;
5857 nla_nest_end(msg, nl_freq);
5859 if (genlmsg_end(msg, hdr) < 0) {
5860 nlmsg_free(msg);
5861 return;
5864 rcu_read_lock();
5865 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5866 GFP_ATOMIC);
5867 rcu_read_unlock();
5869 return;
5871 nla_put_failure:
5872 genlmsg_cancel(msg, hdr);
5873 nlmsg_free(msg);
5876 static void nl80211_send_remain_on_chan_event(
5877 int cmd, struct cfg80211_registered_device *rdev,
5878 struct net_device *netdev, u64 cookie,
5879 struct ieee80211_channel *chan,
5880 enum nl80211_channel_type channel_type,
5881 unsigned int duration, gfp_t gfp)
5883 struct sk_buff *msg;
5884 void *hdr;
5886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5887 if (!msg)
5888 return;
5890 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5891 if (!hdr) {
5892 nlmsg_free(msg);
5893 return;
5896 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5897 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5898 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5899 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5900 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5902 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5903 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5905 if (genlmsg_end(msg, hdr) < 0) {
5906 nlmsg_free(msg);
5907 return;
5910 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5911 nl80211_mlme_mcgrp.id, gfp);
5912 return;
5914 nla_put_failure:
5915 genlmsg_cancel(msg, hdr);
5916 nlmsg_free(msg);
5919 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5920 struct net_device *netdev, u64 cookie,
5921 struct ieee80211_channel *chan,
5922 enum nl80211_channel_type channel_type,
5923 unsigned int duration, gfp_t gfp)
5925 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5926 rdev, netdev, cookie, chan,
5927 channel_type, duration, gfp);
5930 void nl80211_send_remain_on_channel_cancel(
5931 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5932 u64 cookie, struct ieee80211_channel *chan,
5933 enum nl80211_channel_type channel_type, gfp_t gfp)
5935 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5936 rdev, netdev, cookie, chan,
5937 channel_type, 0, gfp);
5940 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5941 struct net_device *dev, const u8 *mac_addr,
5942 struct station_info *sinfo, gfp_t gfp)
5944 struct sk_buff *msg;
5946 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5947 if (!msg)
5948 return;
5950 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5951 nlmsg_free(msg);
5952 return;
5955 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5956 nl80211_mlme_mcgrp.id, gfp);
5959 int nl80211_send_action(struct cfg80211_registered_device *rdev,
5960 struct net_device *netdev, u32 nlpid,
5961 int freq, const u8 *buf, size_t len, gfp_t gfp)
5963 struct sk_buff *msg;
5964 void *hdr;
5965 int err;
5967 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5968 if (!msg)
5969 return -ENOMEM;
5971 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION);
5972 if (!hdr) {
5973 nlmsg_free(msg);
5974 return -ENOMEM;
5977 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5978 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5979 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5980 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5982 err = genlmsg_end(msg, hdr);
5983 if (err < 0) {
5984 nlmsg_free(msg);
5985 return err;
5988 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5989 if (err < 0)
5990 return err;
5991 return 0;
5993 nla_put_failure:
5994 genlmsg_cancel(msg, hdr);
5995 nlmsg_free(msg);
5996 return -ENOBUFS;
5999 void nl80211_send_action_tx_status(struct cfg80211_registered_device *rdev,
6000 struct net_device *netdev, u64 cookie,
6001 const u8 *buf, size_t len, bool ack,
6002 gfp_t gfp)
6004 struct sk_buff *msg;
6005 void *hdr;
6007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6008 if (!msg)
6009 return;
6011 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION_TX_STATUS);
6012 if (!hdr) {
6013 nlmsg_free(msg);
6014 return;
6017 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6018 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6019 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6020 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6021 if (ack)
6022 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6024 if (genlmsg_end(msg, hdr) < 0) {
6025 nlmsg_free(msg);
6026 return;
6029 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6030 return;
6032 nla_put_failure:
6033 genlmsg_cancel(msg, hdr);
6034 nlmsg_free(msg);
6037 void
6038 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6039 struct net_device *netdev,
6040 enum nl80211_cqm_rssi_threshold_event rssi_event,
6041 gfp_t gfp)
6043 struct sk_buff *msg;
6044 struct nlattr *pinfoattr;
6045 void *hdr;
6047 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6048 if (!msg)
6049 return;
6051 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6052 if (!hdr) {
6053 nlmsg_free(msg);
6054 return;
6057 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6058 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6060 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6061 if (!pinfoattr)
6062 goto nla_put_failure;
6064 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6065 rssi_event);
6067 nla_nest_end(msg, pinfoattr);
6069 if (genlmsg_end(msg, hdr) < 0) {
6070 nlmsg_free(msg);
6071 return;
6074 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6075 nl80211_mlme_mcgrp.id, gfp);
6076 return;
6078 nla_put_failure:
6079 genlmsg_cancel(msg, hdr);
6080 nlmsg_free(msg);
6083 static int nl80211_netlink_notify(struct notifier_block * nb,
6084 unsigned long state,
6085 void *_notify)
6087 struct netlink_notify *notify = _notify;
6088 struct cfg80211_registered_device *rdev;
6089 struct wireless_dev *wdev;
6091 if (state != NETLINK_URELEASE)
6092 return NOTIFY_DONE;
6094 rcu_read_lock();
6096 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6097 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
6098 cfg80211_mlme_unregister_actions(wdev, notify->pid);
6100 rcu_read_unlock();
6102 return NOTIFY_DONE;
6105 static struct notifier_block nl80211_netlink_notifier = {
6106 .notifier_call = nl80211_netlink_notify,
6109 /* initialisation/exit functions */
6111 int nl80211_init(void)
6113 int err;
6115 err = genl_register_family_with_ops(&nl80211_fam,
6116 nl80211_ops, ARRAY_SIZE(nl80211_ops));
6117 if (err)
6118 return err;
6120 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6121 if (err)
6122 goto err_out;
6124 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6125 if (err)
6126 goto err_out;
6128 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6129 if (err)
6130 goto err_out;
6132 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6133 if (err)
6134 goto err_out;
6136 #ifdef CONFIG_NL80211_TESTMODE
6137 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6138 if (err)
6139 goto err_out;
6140 #endif
6142 err = netlink_register_notifier(&nl80211_netlink_notifier);
6143 if (err)
6144 goto err_out;
6146 return 0;
6147 err_out:
6148 genl_unregister_family(&nl80211_fam);
6149 return err;
6152 void nl80211_exit(void)
6154 netlink_unregister_notifier(&nl80211_netlink_notifier);
6155 genl_unregister_family(&nl80211_fam);