ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / net / wireless / nl80211.c
blobf310a0d90c328bc44d060311f599ba07079b04f4
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 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
31 /* the netlink family */
32 static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
38 .netnsok = true,
39 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
43 /* internal helper: get rdev and dev */
44 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
45 struct cfg80211_registered_device **rdev,
46 struct net_device **dev)
48 struct nlattr **attrs = info->attrs;
49 int ifindex;
51 if (!attrs[NL80211_ATTR_IFINDEX])
52 return -EINVAL;
54 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
55 *dev = dev_get_by_index(genl_info_net(info), ifindex);
56 if (!*dev)
57 return -ENODEV;
59 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
60 if (IS_ERR(*rdev)) {
61 dev_put(*dev);
62 return PTR_ERR(*rdev);
65 return 0;
68 /* policy for the attributes */
69 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
70 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
72 .len = 20-1 },
73 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
74 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
75 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
80 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
86 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
87 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
89 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
90 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
95 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
96 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
124 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
126 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
127 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
129 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
131 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
132 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
133 .len = IEEE80211_MAX_DATA_LEN },
134 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
135 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
137 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_SSID_LEN },
139 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
140 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
141 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
142 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
143 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
144 [NL80211_ATTR_STA_FLAGS2] = {
145 .len = sizeof(struct nl80211_sta_flag_update),
147 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
148 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
149 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
150 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
151 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
152 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
153 [NL80211_ATTR_PID] = { .type = NLA_U32 },
154 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
155 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
156 .len = WLAN_PMKID_LEN },
157 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
158 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
159 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
160 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
161 .len = IEEE80211_MAX_DATA_LEN },
162 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
163 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
164 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
165 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
166 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
169 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
170 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
171 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
172 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
173 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
174 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
175 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
176 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
177 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
180 /* policy for the key attributes */
181 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
182 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
183 [NL80211_KEY_IDX] = { .type = NLA_U8 },
184 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
185 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
186 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
187 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
188 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
189 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
192 /* policy for the key default flags */
193 static const struct nla_policy
194 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
195 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
196 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
199 /* policy for WoWLAN attributes */
200 static const struct nla_policy
201 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
202 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
203 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
204 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
205 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
208 /* ifidx get helper */
209 static int nl80211_get_ifidx(struct netlink_callback *cb)
211 int res;
213 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
214 nl80211_fam.attrbuf, nl80211_fam.maxattr,
215 nl80211_policy);
216 if (res)
217 return res;
219 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
220 return -EINVAL;
222 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
223 if (!res)
224 return -EINVAL;
225 return res;
228 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
229 struct netlink_callback *cb,
230 struct cfg80211_registered_device **rdev,
231 struct net_device **dev)
233 int ifidx = cb->args[0];
234 int err;
236 if (!ifidx)
237 ifidx = nl80211_get_ifidx(cb);
238 if (ifidx < 0)
239 return ifidx;
241 cb->args[0] = ifidx;
243 rtnl_lock();
245 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
246 if (!*dev) {
247 err = -ENODEV;
248 goto out_rtnl;
251 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
252 if (IS_ERR(*rdev)) {
253 err = PTR_ERR(*rdev);
254 goto out_rtnl;
257 return 0;
258 out_rtnl:
259 rtnl_unlock();
260 return err;
263 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
265 cfg80211_unlock_rdev(rdev);
266 rtnl_unlock();
269 /* IE validation */
270 static bool is_valid_ie_attr(const struct nlattr *attr)
272 const u8 *pos;
273 int len;
275 if (!attr)
276 return true;
278 pos = nla_data(attr);
279 len = nla_len(attr);
281 while (len) {
282 u8 elemlen;
284 if (len < 2)
285 return false;
286 len -= 2;
288 elemlen = pos[1];
289 if (elemlen > len)
290 return false;
292 len -= elemlen;
293 pos += 2 + elemlen;
296 return true;
299 /* message building helper */
300 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
301 int flags, u8 cmd)
303 /* since there is no private header just add the generic one */
304 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
307 static int nl80211_msg_put_channel(struct sk_buff *msg,
308 struct ieee80211_channel *chan)
310 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
311 chan->center_freq);
313 if (chan->flags & IEEE80211_CHAN_DISABLED)
314 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
315 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
316 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
317 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
318 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
319 if (chan->flags & IEEE80211_CHAN_RADAR)
320 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
322 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
323 DBM_TO_MBM(chan->max_power));
325 return 0;
327 nla_put_failure:
328 return -ENOBUFS;
331 /* netlink command implementations */
333 struct key_parse {
334 struct key_params p;
335 int idx;
336 int type;
337 bool def, defmgmt;
338 bool def_uni, def_multi;
341 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
343 struct nlattr *tb[NL80211_KEY_MAX + 1];
344 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
345 nl80211_key_policy);
346 if (err)
347 return err;
349 k->def = !!tb[NL80211_KEY_DEFAULT];
350 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
352 if (k->def) {
353 k->def_uni = true;
354 k->def_multi = true;
356 if (k->defmgmt)
357 k->def_multi = true;
359 if (tb[NL80211_KEY_IDX])
360 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
362 if (tb[NL80211_KEY_DATA]) {
363 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
364 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
367 if (tb[NL80211_KEY_SEQ]) {
368 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
369 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
372 if (tb[NL80211_KEY_CIPHER])
373 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
375 if (tb[NL80211_KEY_TYPE]) {
376 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
377 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
378 return -EINVAL;
381 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
382 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
383 int err = nla_parse_nested(kdt,
384 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
385 tb[NL80211_KEY_DEFAULT_TYPES],
386 nl80211_key_default_policy);
387 if (err)
388 return err;
390 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
391 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
394 return 0;
397 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
399 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
400 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
401 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
404 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
405 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
406 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
409 if (info->attrs[NL80211_ATTR_KEY_IDX])
410 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
412 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
413 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
415 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
416 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
418 if (k->def) {
419 k->def_uni = true;
420 k->def_multi = true;
422 if (k->defmgmt)
423 k->def_multi = true;
425 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
426 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
427 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
428 return -EINVAL;
431 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
432 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
433 int err = nla_parse_nested(
434 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
435 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
436 nl80211_key_default_policy);
437 if (err)
438 return err;
440 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
441 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
444 return 0;
447 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
449 int err;
451 memset(k, 0, sizeof(*k));
452 k->idx = -1;
453 k->type = -1;
455 if (info->attrs[NL80211_ATTR_KEY])
456 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
457 else
458 err = nl80211_parse_key_old(info, k);
460 if (err)
461 return err;
463 if (k->def && k->defmgmt)
464 return -EINVAL;
466 if (k->defmgmt) {
467 if (k->def_uni || !k->def_multi)
468 return -EINVAL;
471 if (k->idx != -1) {
472 if (k->defmgmt) {
473 if (k->idx < 4 || k->idx > 5)
474 return -EINVAL;
475 } else if (k->def) {
476 if (k->idx < 0 || k->idx > 3)
477 return -EINVAL;
478 } else {
479 if (k->idx < 0 || k->idx > 5)
480 return -EINVAL;
484 return 0;
487 static struct cfg80211_cached_keys *
488 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
489 struct nlattr *keys)
491 struct key_parse parse;
492 struct nlattr *key;
493 struct cfg80211_cached_keys *result;
494 int rem, err, def = 0;
496 result = kzalloc(sizeof(*result), GFP_KERNEL);
497 if (!result)
498 return ERR_PTR(-ENOMEM);
500 result->def = -1;
501 result->defmgmt = -1;
503 nla_for_each_nested(key, keys, rem) {
504 memset(&parse, 0, sizeof(parse));
505 parse.idx = -1;
507 err = nl80211_parse_key_new(key, &parse);
508 if (err)
509 goto error;
510 err = -EINVAL;
511 if (!parse.p.key)
512 goto error;
513 if (parse.idx < 0 || parse.idx > 4)
514 goto error;
515 if (parse.def) {
516 if (def)
517 goto error;
518 def = 1;
519 result->def = parse.idx;
520 if (!parse.def_uni || !parse.def_multi)
521 goto error;
522 } else if (parse.defmgmt)
523 goto error;
524 err = cfg80211_validate_key_settings(rdev, &parse.p,
525 parse.idx, false, NULL);
526 if (err)
527 goto error;
528 result->params[parse.idx].cipher = parse.p.cipher;
529 result->params[parse.idx].key_len = parse.p.key_len;
530 result->params[parse.idx].key = result->data[parse.idx];
531 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
534 return result;
535 error:
536 kfree(result);
537 return ERR_PTR(err);
540 static int nl80211_key_allowed(struct wireless_dev *wdev)
542 ASSERT_WDEV_LOCK(wdev);
544 switch (wdev->iftype) {
545 case NL80211_IFTYPE_AP:
546 case NL80211_IFTYPE_AP_VLAN:
547 case NL80211_IFTYPE_P2P_GO:
548 case NL80211_IFTYPE_MESH_POINT:
549 break;
550 case NL80211_IFTYPE_ADHOC:
551 if (!wdev->current_bss)
552 return -ENOLINK;
553 break;
554 case NL80211_IFTYPE_STATION:
555 case NL80211_IFTYPE_P2P_CLIENT:
556 if (wdev->sme_state != CFG80211_SME_CONNECTED)
557 return -ENOLINK;
558 break;
559 default:
560 return -EINVAL;
563 return 0;
566 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
568 struct nlattr *nl_modes = nla_nest_start(msg, attr);
569 int i;
571 if (!nl_modes)
572 goto nla_put_failure;
574 i = 0;
575 while (ifmodes) {
576 if (ifmodes & 1)
577 NLA_PUT_FLAG(msg, i);
578 ifmodes >>= 1;
579 i++;
582 nla_nest_end(msg, nl_modes);
583 return 0;
585 nla_put_failure:
586 return -ENOBUFS;
589 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
590 struct sk_buff *msg)
592 struct nlattr *nl_combis;
593 int i, j;
595 nl_combis = nla_nest_start(msg,
596 NL80211_ATTR_INTERFACE_COMBINATIONS);
597 if (!nl_combis)
598 goto nla_put_failure;
600 for (i = 0; i < wiphy->n_iface_combinations; i++) {
601 const struct ieee80211_iface_combination *c;
602 struct nlattr *nl_combi, *nl_limits;
604 c = &wiphy->iface_combinations[i];
606 nl_combi = nla_nest_start(msg, i + 1);
607 if (!nl_combi)
608 goto nla_put_failure;
610 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
611 if (!nl_limits)
612 goto nla_put_failure;
614 for (j = 0; j < c->n_limits; j++) {
615 struct nlattr *nl_limit;
617 nl_limit = nla_nest_start(msg, j + 1);
618 if (!nl_limit)
619 goto nla_put_failure;
620 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
621 c->limits[j].max);
622 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
623 c->limits[j].types))
624 goto nla_put_failure;
625 nla_nest_end(msg, nl_limit);
628 nla_nest_end(msg, nl_limits);
630 if (c->beacon_int_infra_match)
631 NLA_PUT_FLAG(msg,
632 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
633 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
634 c->num_different_channels);
635 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
636 c->max_interfaces);
638 nla_nest_end(msg, nl_combi);
641 nla_nest_end(msg, nl_combis);
643 return 0;
644 nla_put_failure:
645 return -ENOBUFS;
648 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
649 struct cfg80211_registered_device *dev)
651 void *hdr;
652 struct nlattr *nl_bands, *nl_band;
653 struct nlattr *nl_freqs, *nl_freq;
654 struct nlattr *nl_rates, *nl_rate;
655 struct nlattr *nl_cmds;
656 enum ieee80211_band band;
657 struct ieee80211_channel *chan;
658 struct ieee80211_rate *rate;
659 int i;
660 const struct ieee80211_txrx_stypes *mgmt_stypes =
661 dev->wiphy.mgmt_stypes;
663 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
664 if (!hdr)
665 return -1;
667 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
668 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
670 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
671 cfg80211_rdev_list_generation);
673 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
674 dev->wiphy.retry_short);
675 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
676 dev->wiphy.retry_long);
677 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
678 dev->wiphy.frag_threshold);
679 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
680 dev->wiphy.rts_threshold);
681 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
682 dev->wiphy.coverage_class);
683 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
684 dev->wiphy.max_scan_ssids);
685 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
686 dev->wiphy.max_scan_ie_len);
688 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
689 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
690 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
691 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
693 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
694 sizeof(u32) * dev->wiphy.n_cipher_suites,
695 dev->wiphy.cipher_suites);
697 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
698 dev->wiphy.max_num_pmkids);
700 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
701 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
703 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
704 dev->wiphy.available_antennas_tx);
705 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
706 dev->wiphy.available_antennas_rx);
708 if ((dev->wiphy.available_antennas_tx ||
709 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
710 u32 tx_ant = 0, rx_ant = 0;
711 int res;
712 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
713 if (!res) {
714 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
719 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
720 dev->wiphy.interface_modes))
721 goto nla_put_failure;
723 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
724 if (!nl_bands)
725 goto nla_put_failure;
727 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
728 if (!dev->wiphy.bands[band])
729 continue;
731 nl_band = nla_nest_start(msg, band);
732 if (!nl_band)
733 goto nla_put_failure;
735 /* add HT info */
736 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
737 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
738 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
739 &dev->wiphy.bands[band]->ht_cap.mcs);
740 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
741 dev->wiphy.bands[band]->ht_cap.cap);
742 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
743 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
744 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
745 dev->wiphy.bands[band]->ht_cap.ampdu_density);
748 /* add frequencies */
749 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
750 if (!nl_freqs)
751 goto nla_put_failure;
753 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
754 nl_freq = nla_nest_start(msg, i);
755 if (!nl_freq)
756 goto nla_put_failure;
758 chan = &dev->wiphy.bands[band]->channels[i];
760 if (nl80211_msg_put_channel(msg, chan))
761 goto nla_put_failure;
763 nla_nest_end(msg, nl_freq);
766 nla_nest_end(msg, nl_freqs);
768 /* add bitrates */
769 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
770 if (!nl_rates)
771 goto nla_put_failure;
773 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
774 nl_rate = nla_nest_start(msg, i);
775 if (!nl_rate)
776 goto nla_put_failure;
778 rate = &dev->wiphy.bands[band]->bitrates[i];
779 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
780 rate->bitrate);
781 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
782 NLA_PUT_FLAG(msg,
783 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
785 nla_nest_end(msg, nl_rate);
788 nla_nest_end(msg, nl_rates);
790 nla_nest_end(msg, nl_band);
792 nla_nest_end(msg, nl_bands);
794 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
795 if (!nl_cmds)
796 goto nla_put_failure;
798 i = 0;
799 #define CMD(op, n) \
800 do { \
801 if (dev->ops->op) { \
802 i++; \
803 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
805 } while (0)
807 CMD(add_virtual_intf, NEW_INTERFACE);
808 CMD(change_virtual_intf, SET_INTERFACE);
809 CMD(add_key, NEW_KEY);
810 CMD(add_beacon, NEW_BEACON);
811 CMD(add_station, NEW_STATION);
812 CMD(add_mpath, NEW_MPATH);
813 CMD(update_mesh_config, SET_MESH_CONFIG);
814 CMD(change_bss, SET_BSS);
815 CMD(auth, AUTHENTICATE);
816 CMD(assoc, ASSOCIATE);
817 CMD(deauth, DEAUTHENTICATE);
818 CMD(disassoc, DISASSOCIATE);
819 CMD(join_ibss, JOIN_IBSS);
820 CMD(join_mesh, JOIN_MESH);
821 CMD(set_pmksa, SET_PMKSA);
822 CMD(del_pmksa, DEL_PMKSA);
823 CMD(flush_pmksa, FLUSH_PMKSA);
824 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
825 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
826 CMD(mgmt_tx, FRAME);
827 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
828 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
829 i++;
830 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
832 CMD(set_channel, SET_CHANNEL);
833 CMD(set_wds_peer, SET_WDS_PEER);
834 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
835 CMD(sched_scan_start, START_SCHED_SCAN);
837 #undef CMD
839 if (dev->ops->connect || dev->ops->auth) {
840 i++;
841 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
844 if (dev->ops->disconnect || dev->ops->deauth) {
845 i++;
846 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
849 nla_nest_end(msg, nl_cmds);
851 if (dev->ops->remain_on_channel)
852 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
853 dev->wiphy.max_remain_on_channel_duration);
855 /* for now at least assume all drivers have it */
856 if (dev->ops->mgmt_tx)
857 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
859 if (mgmt_stypes) {
860 u16 stypes;
861 struct nlattr *nl_ftypes, *nl_ifs;
862 enum nl80211_iftype ift;
864 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
865 if (!nl_ifs)
866 goto nla_put_failure;
868 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
869 nl_ftypes = nla_nest_start(msg, ift);
870 if (!nl_ftypes)
871 goto nla_put_failure;
872 i = 0;
873 stypes = mgmt_stypes[ift].tx;
874 while (stypes) {
875 if (stypes & 1)
876 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
877 (i << 4) | IEEE80211_FTYPE_MGMT);
878 stypes >>= 1;
879 i++;
881 nla_nest_end(msg, nl_ftypes);
884 nla_nest_end(msg, nl_ifs);
886 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
887 if (!nl_ifs)
888 goto nla_put_failure;
890 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
891 nl_ftypes = nla_nest_start(msg, ift);
892 if (!nl_ftypes)
893 goto nla_put_failure;
894 i = 0;
895 stypes = mgmt_stypes[ift].rx;
896 while (stypes) {
897 if (stypes & 1)
898 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
899 (i << 4) | IEEE80211_FTYPE_MGMT);
900 stypes >>= 1;
901 i++;
903 nla_nest_end(msg, nl_ftypes);
905 nla_nest_end(msg, nl_ifs);
908 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
909 struct nlattr *nl_wowlan;
911 nl_wowlan = nla_nest_start(msg,
912 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
913 if (!nl_wowlan)
914 goto nla_put_failure;
916 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
917 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
918 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
919 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
920 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
921 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
922 if (dev->wiphy.wowlan.n_patterns) {
923 struct nl80211_wowlan_pattern_support pat = {
924 .max_patterns = dev->wiphy.wowlan.n_patterns,
925 .min_pattern_len =
926 dev->wiphy.wowlan.pattern_min_len,
927 .max_pattern_len =
928 dev->wiphy.wowlan.pattern_max_len,
930 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
931 sizeof(pat), &pat);
934 nla_nest_end(msg, nl_wowlan);
937 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
938 dev->wiphy.software_iftypes))
939 goto nla_put_failure;
941 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
942 goto nla_put_failure;
944 return genlmsg_end(msg, hdr);
946 nla_put_failure:
947 genlmsg_cancel(msg, hdr);
948 return -EMSGSIZE;
951 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
953 int idx = 0;
954 int start = cb->args[0];
955 struct cfg80211_registered_device *dev;
957 mutex_lock(&cfg80211_mutex);
958 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
959 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
960 continue;
961 if (++idx <= start)
962 continue;
963 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
964 cb->nlh->nlmsg_seq, NLM_F_MULTI,
965 dev) < 0) {
966 idx--;
967 break;
970 mutex_unlock(&cfg80211_mutex);
972 cb->args[0] = idx;
974 return skb->len;
977 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
979 struct sk_buff *msg;
980 struct cfg80211_registered_device *dev = info->user_ptr[0];
982 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
983 if (!msg)
984 return -ENOMEM;
986 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
987 nlmsg_free(msg);
988 return -ENOBUFS;
991 return genlmsg_reply(msg, info);
994 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
995 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
996 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
997 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
998 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
999 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1002 static int parse_txq_params(struct nlattr *tb[],
1003 struct ieee80211_txq_params *txq_params)
1005 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1006 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1007 !tb[NL80211_TXQ_ATTR_AIFS])
1008 return -EINVAL;
1010 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1011 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1012 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1013 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1014 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1016 return 0;
1019 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1022 * You can only set the channel explicitly for AP, mesh
1023 * and WDS type interfaces; all others have their channel
1024 * managed via their respective "establish a connection"
1025 * command (connect, join, ...)
1027 * Monitors are special as they are normally slaved to
1028 * whatever else is going on, so they behave as though
1029 * you tried setting the wiphy channel itself.
1031 return !wdev ||
1032 wdev->iftype == NL80211_IFTYPE_AP ||
1033 wdev->iftype == NL80211_IFTYPE_WDS ||
1034 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1035 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1036 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1039 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1040 struct wireless_dev *wdev,
1041 struct genl_info *info)
1043 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1044 u32 freq;
1045 int result;
1047 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1048 return -EINVAL;
1050 if (!nl80211_can_set_dev_channel(wdev))
1051 return -EOPNOTSUPP;
1053 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1054 channel_type = nla_get_u32(info->attrs[
1055 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1056 if (channel_type != NL80211_CHAN_NO_HT &&
1057 channel_type != NL80211_CHAN_HT20 &&
1058 channel_type != NL80211_CHAN_HT40PLUS &&
1059 channel_type != NL80211_CHAN_HT40MINUS)
1060 return -EINVAL;
1063 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1065 mutex_lock(&rdev->devlist_mtx);
1066 if (wdev) {
1067 wdev_lock(wdev);
1068 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1069 wdev_unlock(wdev);
1070 } else {
1071 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1073 mutex_unlock(&rdev->devlist_mtx);
1075 return result;
1078 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1080 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1081 struct net_device *netdev = info->user_ptr[1];
1083 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1086 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1089 struct net_device *dev = info->user_ptr[1];
1090 struct wireless_dev *wdev = dev->ieee80211_ptr;
1091 const u8 *bssid;
1093 if (!info->attrs[NL80211_ATTR_MAC])
1094 return -EINVAL;
1096 if (netif_running(dev))
1097 return -EBUSY;
1099 if (!rdev->ops->set_wds_peer)
1100 return -EOPNOTSUPP;
1102 if (wdev->iftype != NL80211_IFTYPE_WDS)
1103 return -EOPNOTSUPP;
1105 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1106 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
1110 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1112 struct cfg80211_registered_device *rdev;
1113 struct net_device *netdev = NULL;
1114 struct wireless_dev *wdev;
1115 int result = 0, rem_txq_params = 0;
1116 struct nlattr *nl_txq_params;
1117 u32 changed;
1118 u8 retry_short = 0, retry_long = 0;
1119 u32 frag_threshold = 0, rts_threshold = 0;
1120 u8 coverage_class = 0;
1123 * Try to find the wiphy and netdev. Normally this
1124 * function shouldn't need the netdev, but this is
1125 * done for backward compatibility -- previously
1126 * setting the channel was done per wiphy, but now
1127 * it is per netdev. Previous userland like hostapd
1128 * also passed a netdev to set_wiphy, so that it is
1129 * possible to let that go to the right netdev!
1131 mutex_lock(&cfg80211_mutex);
1133 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1134 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1136 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1137 if (netdev && netdev->ieee80211_ptr) {
1138 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1139 mutex_lock(&rdev->mtx);
1140 } else
1141 netdev = NULL;
1144 if (!netdev) {
1145 rdev = __cfg80211_rdev_from_info(info);
1146 if (IS_ERR(rdev)) {
1147 mutex_unlock(&cfg80211_mutex);
1148 return PTR_ERR(rdev);
1150 wdev = NULL;
1151 netdev = NULL;
1152 result = 0;
1154 mutex_lock(&rdev->mtx);
1155 } else if (netif_running(netdev) &&
1156 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1157 wdev = netdev->ieee80211_ptr;
1158 else
1159 wdev = NULL;
1162 * end workaround code, by now the rdev is available
1163 * and locked, and wdev may or may not be NULL.
1166 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1167 result = cfg80211_dev_rename(
1168 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1170 mutex_unlock(&cfg80211_mutex);
1172 if (result)
1173 goto bad_res;
1175 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1176 struct ieee80211_txq_params txq_params;
1177 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1179 if (!rdev->ops->set_txq_params) {
1180 result = -EOPNOTSUPP;
1181 goto bad_res;
1184 if (!netif_running(netdev)) {
1185 result = -ENETDOWN;
1186 goto bad_res;
1189 nla_for_each_nested(nl_txq_params,
1190 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1191 rem_txq_params) {
1192 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1193 nla_data(nl_txq_params),
1194 nla_len(nl_txq_params),
1195 txq_params_policy);
1196 result = parse_txq_params(tb, &txq_params);
1197 if (result)
1198 goto bad_res;
1200 result = rdev->ops->set_txq_params(&rdev->wiphy,
1201 &txq_params);
1202 if (result)
1203 goto bad_res;
1207 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1208 result = __nl80211_set_channel(rdev, wdev, info);
1209 if (result)
1210 goto bad_res;
1213 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1214 enum nl80211_tx_power_setting type;
1215 int idx, mbm = 0;
1217 if (!rdev->ops->set_tx_power) {
1218 result = -EOPNOTSUPP;
1219 goto bad_res;
1222 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1223 type = nla_get_u32(info->attrs[idx]);
1225 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1226 (type != NL80211_TX_POWER_AUTOMATIC)) {
1227 result = -EINVAL;
1228 goto bad_res;
1231 if (type != NL80211_TX_POWER_AUTOMATIC) {
1232 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1233 mbm = nla_get_u32(info->attrs[idx]);
1236 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1237 if (result)
1238 goto bad_res;
1241 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1242 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1243 u32 tx_ant, rx_ant;
1244 if ((!rdev->wiphy.available_antennas_tx &&
1245 !rdev->wiphy.available_antennas_rx) ||
1246 !rdev->ops->set_antenna) {
1247 result = -EOPNOTSUPP;
1248 goto bad_res;
1251 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1252 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1254 /* reject antenna configurations which don't match the
1255 * available antenna masks, except for the "all" mask */
1256 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1257 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1258 result = -EINVAL;
1259 goto bad_res;
1262 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1263 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1265 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1266 if (result)
1267 goto bad_res;
1270 changed = 0;
1272 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1273 retry_short = nla_get_u8(
1274 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1275 if (retry_short == 0) {
1276 result = -EINVAL;
1277 goto bad_res;
1279 changed |= WIPHY_PARAM_RETRY_SHORT;
1282 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1283 retry_long = nla_get_u8(
1284 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1285 if (retry_long == 0) {
1286 result = -EINVAL;
1287 goto bad_res;
1289 changed |= WIPHY_PARAM_RETRY_LONG;
1292 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1293 frag_threshold = nla_get_u32(
1294 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1295 if (frag_threshold < 256) {
1296 result = -EINVAL;
1297 goto bad_res;
1299 if (frag_threshold != (u32) -1) {
1301 * Fragments (apart from the last one) are required to
1302 * have even length. Make the fragmentation code
1303 * simpler by stripping LSB should someone try to use
1304 * odd threshold value.
1306 frag_threshold &= ~0x1;
1308 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1311 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1312 rts_threshold = nla_get_u32(
1313 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1314 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1317 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1318 coverage_class = nla_get_u8(
1319 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1320 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1323 if (changed) {
1324 u8 old_retry_short, old_retry_long;
1325 u32 old_frag_threshold, old_rts_threshold;
1326 u8 old_coverage_class;
1328 if (!rdev->ops->set_wiphy_params) {
1329 result = -EOPNOTSUPP;
1330 goto bad_res;
1333 old_retry_short = rdev->wiphy.retry_short;
1334 old_retry_long = rdev->wiphy.retry_long;
1335 old_frag_threshold = rdev->wiphy.frag_threshold;
1336 old_rts_threshold = rdev->wiphy.rts_threshold;
1337 old_coverage_class = rdev->wiphy.coverage_class;
1339 if (changed & WIPHY_PARAM_RETRY_SHORT)
1340 rdev->wiphy.retry_short = retry_short;
1341 if (changed & WIPHY_PARAM_RETRY_LONG)
1342 rdev->wiphy.retry_long = retry_long;
1343 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1344 rdev->wiphy.frag_threshold = frag_threshold;
1345 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1346 rdev->wiphy.rts_threshold = rts_threshold;
1347 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1348 rdev->wiphy.coverage_class = coverage_class;
1350 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1351 if (result) {
1352 rdev->wiphy.retry_short = old_retry_short;
1353 rdev->wiphy.retry_long = old_retry_long;
1354 rdev->wiphy.frag_threshold = old_frag_threshold;
1355 rdev->wiphy.rts_threshold = old_rts_threshold;
1356 rdev->wiphy.coverage_class = old_coverage_class;
1360 bad_res:
1361 mutex_unlock(&rdev->mtx);
1362 if (netdev)
1363 dev_put(netdev);
1364 return result;
1368 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1369 struct cfg80211_registered_device *rdev,
1370 struct net_device *dev)
1372 void *hdr;
1374 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1375 if (!hdr)
1376 return -1;
1378 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1379 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1380 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1381 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1383 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1384 rdev->devlist_generation ^
1385 (cfg80211_rdev_list_generation << 2));
1387 return genlmsg_end(msg, hdr);
1389 nla_put_failure:
1390 genlmsg_cancel(msg, hdr);
1391 return -EMSGSIZE;
1394 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1396 int wp_idx = 0;
1397 int if_idx = 0;
1398 int wp_start = cb->args[0];
1399 int if_start = cb->args[1];
1400 struct cfg80211_registered_device *rdev;
1401 struct wireless_dev *wdev;
1403 mutex_lock(&cfg80211_mutex);
1404 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1405 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1406 continue;
1407 if (wp_idx < wp_start) {
1408 wp_idx++;
1409 continue;
1411 if_idx = 0;
1413 mutex_lock(&rdev->devlist_mtx);
1414 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1415 if (if_idx < if_start) {
1416 if_idx++;
1417 continue;
1419 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1420 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1421 rdev, wdev->netdev) < 0) {
1422 mutex_unlock(&rdev->devlist_mtx);
1423 goto out;
1425 if_idx++;
1427 mutex_unlock(&rdev->devlist_mtx);
1429 wp_idx++;
1431 out:
1432 mutex_unlock(&cfg80211_mutex);
1434 cb->args[0] = wp_idx;
1435 cb->args[1] = if_idx;
1437 return skb->len;
1440 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1442 struct sk_buff *msg;
1443 struct cfg80211_registered_device *dev = info->user_ptr[0];
1444 struct net_device *netdev = info->user_ptr[1];
1446 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1447 if (!msg)
1448 return -ENOMEM;
1450 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1451 dev, netdev) < 0) {
1452 nlmsg_free(msg);
1453 return -ENOBUFS;
1456 return genlmsg_reply(msg, info);
1459 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1460 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1461 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1462 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1463 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1464 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1467 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1469 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1470 int flag;
1472 *mntrflags = 0;
1474 if (!nla)
1475 return -EINVAL;
1477 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1478 nla, mntr_flags_policy))
1479 return -EINVAL;
1481 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1482 if (flags[flag])
1483 *mntrflags |= (1<<flag);
1485 return 0;
1488 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1489 struct net_device *netdev, u8 use_4addr,
1490 enum nl80211_iftype iftype)
1492 if (!use_4addr) {
1493 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1494 return -EBUSY;
1495 return 0;
1498 switch (iftype) {
1499 case NL80211_IFTYPE_AP_VLAN:
1500 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1501 return 0;
1502 break;
1503 case NL80211_IFTYPE_STATION:
1504 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1505 return 0;
1506 break;
1507 default:
1508 break;
1511 return -EOPNOTSUPP;
1514 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1517 struct vif_params params;
1518 int err;
1519 enum nl80211_iftype otype, ntype;
1520 struct net_device *dev = info->user_ptr[1];
1521 u32 _flags, *flags = NULL;
1522 bool change = false;
1524 memset(&params, 0, sizeof(params));
1526 otype = ntype = dev->ieee80211_ptr->iftype;
1528 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1529 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1530 if (otype != ntype)
1531 change = true;
1532 if (ntype > NL80211_IFTYPE_MAX)
1533 return -EINVAL;
1536 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1537 struct wireless_dev *wdev = dev->ieee80211_ptr;
1539 if (ntype != NL80211_IFTYPE_MESH_POINT)
1540 return -EINVAL;
1541 if (netif_running(dev))
1542 return -EBUSY;
1544 wdev_lock(wdev);
1545 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1546 IEEE80211_MAX_MESH_ID_LEN);
1547 wdev->mesh_id_up_len =
1548 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1549 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1550 wdev->mesh_id_up_len);
1551 wdev_unlock(wdev);
1554 if (info->attrs[NL80211_ATTR_4ADDR]) {
1555 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1556 change = true;
1557 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1558 if (err)
1559 return err;
1560 } else {
1561 params.use_4addr = -1;
1564 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1565 if (ntype != NL80211_IFTYPE_MONITOR)
1566 return -EINVAL;
1567 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1568 &_flags);
1569 if (err)
1570 return err;
1572 flags = &_flags;
1573 change = true;
1576 if (change)
1577 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1578 else
1579 err = 0;
1581 if (!err && params.use_4addr != -1)
1582 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1584 return err;
1587 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1590 struct vif_params params;
1591 struct net_device *dev;
1592 int err;
1593 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1594 u32 flags;
1596 memset(&params, 0, sizeof(params));
1598 if (!info->attrs[NL80211_ATTR_IFNAME])
1599 return -EINVAL;
1601 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1602 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1603 if (type > NL80211_IFTYPE_MAX)
1604 return -EINVAL;
1607 if (!rdev->ops->add_virtual_intf ||
1608 !(rdev->wiphy.interface_modes & (1 << type)))
1609 return -EOPNOTSUPP;
1611 if (info->attrs[NL80211_ATTR_4ADDR]) {
1612 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1613 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1614 if (err)
1615 return err;
1618 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1619 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1620 &flags);
1621 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
1622 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1623 type, err ? NULL : &flags, &params);
1624 if (IS_ERR(dev))
1625 return PTR_ERR(dev);
1627 if (type == NL80211_IFTYPE_MESH_POINT &&
1628 info->attrs[NL80211_ATTR_MESH_ID]) {
1629 struct wireless_dev *wdev = dev->ieee80211_ptr;
1631 wdev_lock(wdev);
1632 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1633 IEEE80211_MAX_MESH_ID_LEN);
1634 wdev->mesh_id_up_len =
1635 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1636 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1637 wdev->mesh_id_up_len);
1638 wdev_unlock(wdev);
1641 return 0;
1644 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1646 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1647 struct net_device *dev = info->user_ptr[1];
1649 if (!rdev->ops->del_virtual_intf)
1650 return -EOPNOTSUPP;
1652 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1655 struct get_key_cookie {
1656 struct sk_buff *msg;
1657 int error;
1658 int idx;
1661 static void get_key_callback(void *c, struct key_params *params)
1663 struct nlattr *key;
1664 struct get_key_cookie *cookie = c;
1666 if (params->key)
1667 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1668 params->key_len, params->key);
1670 if (params->seq)
1671 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1672 params->seq_len, params->seq);
1674 if (params->cipher)
1675 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1676 params->cipher);
1678 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1679 if (!key)
1680 goto nla_put_failure;
1682 if (params->key)
1683 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1684 params->key_len, params->key);
1686 if (params->seq)
1687 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1688 params->seq_len, params->seq);
1690 if (params->cipher)
1691 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1692 params->cipher);
1694 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1696 nla_nest_end(cookie->msg, key);
1698 return;
1699 nla_put_failure:
1700 cookie->error = 1;
1703 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1706 int err;
1707 struct net_device *dev = info->user_ptr[1];
1708 u8 key_idx = 0;
1709 const u8 *mac_addr = NULL;
1710 bool pairwise;
1711 struct get_key_cookie cookie = {
1712 .error = 0,
1714 void *hdr;
1715 struct sk_buff *msg;
1717 if (info->attrs[NL80211_ATTR_KEY_IDX])
1718 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1720 if (key_idx > 5)
1721 return -EINVAL;
1723 if (info->attrs[NL80211_ATTR_MAC])
1724 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1726 pairwise = !!mac_addr;
1727 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1728 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1729 if (kt >= NUM_NL80211_KEYTYPES)
1730 return -EINVAL;
1731 if (kt != NL80211_KEYTYPE_GROUP &&
1732 kt != NL80211_KEYTYPE_PAIRWISE)
1733 return -EINVAL;
1734 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1737 if (!rdev->ops->get_key)
1738 return -EOPNOTSUPP;
1740 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1741 if (!msg)
1742 return -ENOMEM;
1744 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1745 NL80211_CMD_NEW_KEY);
1746 if (IS_ERR(hdr))
1747 return PTR_ERR(hdr);
1749 cookie.msg = msg;
1750 cookie.idx = key_idx;
1752 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1753 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1754 if (mac_addr)
1755 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1757 if (pairwise && mac_addr &&
1758 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1759 return -ENOENT;
1761 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1762 mac_addr, &cookie, get_key_callback);
1764 if (err)
1765 goto free_msg;
1767 if (cookie.error)
1768 goto nla_put_failure;
1770 genlmsg_end(msg, hdr);
1771 return genlmsg_reply(msg, info);
1773 nla_put_failure:
1774 err = -ENOBUFS;
1775 free_msg:
1776 nlmsg_free(msg);
1777 return err;
1780 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1782 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1783 struct key_parse key;
1784 int err;
1785 struct net_device *dev = info->user_ptr[1];
1787 err = nl80211_parse_key(info, &key);
1788 if (err)
1789 return err;
1791 if (key.idx < 0)
1792 return -EINVAL;
1794 /* only support setting default key */
1795 if (!key.def && !key.defmgmt)
1796 return -EINVAL;
1798 wdev_lock(dev->ieee80211_ptr);
1800 if (key.def) {
1801 if (!rdev->ops->set_default_key) {
1802 err = -EOPNOTSUPP;
1803 goto out;
1806 err = nl80211_key_allowed(dev->ieee80211_ptr);
1807 if (err)
1808 goto out;
1810 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1811 key.def_uni, key.def_multi);
1813 if (err)
1814 goto out;
1816 #ifdef CONFIG_CFG80211_WEXT
1817 dev->ieee80211_ptr->wext.default_key = key.idx;
1818 #endif
1819 } else {
1820 if (key.def_uni || !key.def_multi) {
1821 err = -EINVAL;
1822 goto out;
1825 if (!rdev->ops->set_default_mgmt_key) {
1826 err = -EOPNOTSUPP;
1827 goto out;
1830 err = nl80211_key_allowed(dev->ieee80211_ptr);
1831 if (err)
1832 goto out;
1834 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1835 dev, key.idx);
1836 if (err)
1837 goto out;
1839 #ifdef CONFIG_CFG80211_WEXT
1840 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1841 #endif
1844 out:
1845 wdev_unlock(dev->ieee80211_ptr);
1847 return err;
1850 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1853 int err;
1854 struct net_device *dev = info->user_ptr[1];
1855 struct key_parse key;
1856 const u8 *mac_addr = NULL;
1858 err = nl80211_parse_key(info, &key);
1859 if (err)
1860 return err;
1862 if (!key.p.key)
1863 return -EINVAL;
1865 if (info->attrs[NL80211_ATTR_MAC])
1866 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1868 if (key.type == -1) {
1869 if (mac_addr)
1870 key.type = NL80211_KEYTYPE_PAIRWISE;
1871 else
1872 key.type = NL80211_KEYTYPE_GROUP;
1875 /* for now */
1876 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1877 key.type != NL80211_KEYTYPE_GROUP)
1878 return -EINVAL;
1880 if (!rdev->ops->add_key)
1881 return -EOPNOTSUPP;
1883 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1884 key.type == NL80211_KEYTYPE_PAIRWISE,
1885 mac_addr))
1886 return -EINVAL;
1888 wdev_lock(dev->ieee80211_ptr);
1889 err = nl80211_key_allowed(dev->ieee80211_ptr);
1890 if (!err)
1891 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1892 key.type == NL80211_KEYTYPE_PAIRWISE,
1893 mac_addr, &key.p);
1894 wdev_unlock(dev->ieee80211_ptr);
1896 return err;
1899 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1902 int err;
1903 struct net_device *dev = info->user_ptr[1];
1904 u8 *mac_addr = NULL;
1905 struct key_parse key;
1907 err = nl80211_parse_key(info, &key);
1908 if (err)
1909 return err;
1911 if (info->attrs[NL80211_ATTR_MAC])
1912 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1914 if (key.type == -1) {
1915 if (mac_addr)
1916 key.type = NL80211_KEYTYPE_PAIRWISE;
1917 else
1918 key.type = NL80211_KEYTYPE_GROUP;
1921 /* for now */
1922 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1923 key.type != NL80211_KEYTYPE_GROUP)
1924 return -EINVAL;
1926 if (!rdev->ops->del_key)
1927 return -EOPNOTSUPP;
1929 wdev_lock(dev->ieee80211_ptr);
1930 err = nl80211_key_allowed(dev->ieee80211_ptr);
1932 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1933 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1934 err = -ENOENT;
1936 if (!err)
1937 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1938 key.type == NL80211_KEYTYPE_PAIRWISE,
1939 mac_addr);
1941 #ifdef CONFIG_CFG80211_WEXT
1942 if (!err) {
1943 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1944 dev->ieee80211_ptr->wext.default_key = -1;
1945 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1946 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1948 #endif
1949 wdev_unlock(dev->ieee80211_ptr);
1951 return err;
1954 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1956 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1957 struct beacon_parameters *info);
1958 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1959 struct net_device *dev = info->user_ptr[1];
1960 struct wireless_dev *wdev = dev->ieee80211_ptr;
1961 struct beacon_parameters params;
1962 int haveinfo = 0, err;
1964 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1965 return -EINVAL;
1967 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1968 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1969 return -EOPNOTSUPP;
1971 memset(&params, 0, sizeof(params));
1973 switch (info->genlhdr->cmd) {
1974 case NL80211_CMD_NEW_BEACON:
1975 /* these are required for NEW_BEACON */
1976 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1977 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1978 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1979 return -EINVAL;
1981 params.interval =
1982 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1983 params.dtim_period =
1984 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1986 err = cfg80211_validate_beacon_int(rdev, params.interval);
1987 if (err)
1988 return err;
1990 call = rdev->ops->add_beacon;
1991 break;
1992 case NL80211_CMD_SET_BEACON:
1993 call = rdev->ops->set_beacon;
1994 break;
1995 default:
1996 WARN_ON(1);
1997 return -EOPNOTSUPP;
2000 if (!call)
2001 return -EOPNOTSUPP;
2003 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2004 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2005 params.head_len =
2006 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2007 haveinfo = 1;
2010 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2011 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2012 params.tail_len =
2013 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2014 haveinfo = 1;
2017 if (!haveinfo)
2018 return -EINVAL;
2020 err = call(&rdev->wiphy, dev, &params);
2021 if (!err && params.interval)
2022 wdev->beacon_interval = params.interval;
2023 return err;
2026 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2028 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2029 struct net_device *dev = info->user_ptr[1];
2030 struct wireless_dev *wdev = dev->ieee80211_ptr;
2031 int err;
2033 if (!rdev->ops->del_beacon)
2034 return -EOPNOTSUPP;
2036 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2037 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2038 return -EOPNOTSUPP;
2040 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2041 if (!err)
2042 wdev->beacon_interval = 0;
2043 return err;
2046 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2047 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2048 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2049 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2050 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2051 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2054 static int parse_station_flags(struct genl_info *info,
2055 struct station_parameters *params)
2057 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2058 struct nlattr *nla;
2059 int flag;
2062 * Try parsing the new attribute first so userspace
2063 * can specify both for older kernels.
2065 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2066 if (nla) {
2067 struct nl80211_sta_flag_update *sta_flags;
2069 sta_flags = nla_data(nla);
2070 params->sta_flags_mask = sta_flags->mask;
2071 params->sta_flags_set = sta_flags->set;
2072 if ((params->sta_flags_mask |
2073 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2074 return -EINVAL;
2075 return 0;
2078 /* if present, parse the old attribute */
2080 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2081 if (!nla)
2082 return 0;
2084 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2085 nla, sta_flags_policy))
2086 return -EINVAL;
2088 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2089 params->sta_flags_mask &= ~1;
2091 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2092 if (flags[flag])
2093 params->sta_flags_set |= (1<<flag);
2095 return 0;
2098 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2099 int attr)
2101 struct nlattr *rate;
2102 u16 bitrate;
2104 rate = nla_nest_start(msg, attr);
2105 if (!rate)
2106 goto nla_put_failure;
2108 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2109 bitrate = cfg80211_calculate_bitrate(info);
2110 if (bitrate > 0)
2111 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2113 if (info->flags & RATE_INFO_FLAGS_MCS)
2114 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2115 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2116 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2117 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2118 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2120 nla_nest_end(msg, rate);
2121 return true;
2123 nla_put_failure:
2124 return false;
2127 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2128 int flags, struct net_device *dev,
2129 const u8 *mac_addr, struct station_info *sinfo)
2131 void *hdr;
2132 struct nlattr *sinfoattr, *bss_param;
2134 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2135 if (!hdr)
2136 return -1;
2138 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2139 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2141 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2143 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2144 if (!sinfoattr)
2145 goto nla_put_failure;
2146 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2147 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2148 sinfo->connected_time);
2149 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2150 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2151 sinfo->inactive_time);
2152 if (sinfo->filled & STATION_INFO_RX_BYTES)
2153 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2154 sinfo->rx_bytes);
2155 if (sinfo->filled & STATION_INFO_TX_BYTES)
2156 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2157 sinfo->tx_bytes);
2158 if (sinfo->filled & STATION_INFO_LLID)
2159 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2160 sinfo->llid);
2161 if (sinfo->filled & STATION_INFO_PLID)
2162 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2163 sinfo->plid);
2164 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2165 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2166 sinfo->plink_state);
2167 if (sinfo->filled & STATION_INFO_SIGNAL)
2168 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2169 sinfo->signal);
2170 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2171 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2172 sinfo->signal_avg);
2173 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2174 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2175 NL80211_STA_INFO_TX_BITRATE))
2176 goto nla_put_failure;
2178 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2179 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2180 NL80211_STA_INFO_RX_BITRATE))
2181 goto nla_put_failure;
2183 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2184 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2185 sinfo->rx_packets);
2186 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2187 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2188 sinfo->tx_packets);
2189 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2190 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2191 sinfo->tx_retries);
2192 if (sinfo->filled & STATION_INFO_TX_FAILED)
2193 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2194 sinfo->tx_failed);
2195 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2196 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2197 if (!bss_param)
2198 goto nla_put_failure;
2200 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2201 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2202 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2203 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2204 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2205 NLA_PUT_FLAG(msg,
2206 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2207 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2208 sinfo->bss_param.dtim_period);
2209 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2210 sinfo->bss_param.beacon_interval);
2212 nla_nest_end(msg, bss_param);
2214 nla_nest_end(msg, sinfoattr);
2216 return genlmsg_end(msg, hdr);
2218 nla_put_failure:
2219 genlmsg_cancel(msg, hdr);
2220 return -EMSGSIZE;
2223 static int nl80211_dump_station(struct sk_buff *skb,
2224 struct netlink_callback *cb)
2226 struct station_info sinfo;
2227 struct cfg80211_registered_device *dev;
2228 struct net_device *netdev;
2229 u8 mac_addr[ETH_ALEN];
2230 int sta_idx = cb->args[1];
2231 int err;
2233 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2234 if (err)
2235 return err;
2237 if (!dev->ops->dump_station) {
2238 err = -EOPNOTSUPP;
2239 goto out_err;
2242 while (1) {
2243 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2244 mac_addr, &sinfo);
2245 if (err == -ENOENT)
2246 break;
2247 if (err)
2248 goto out_err;
2250 if (nl80211_send_station(skb,
2251 NETLINK_CB(cb->skb).pid,
2252 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2253 netdev, mac_addr,
2254 &sinfo) < 0)
2255 goto out;
2257 sta_idx++;
2261 out:
2262 cb->args[1] = sta_idx;
2263 err = skb->len;
2264 out_err:
2265 nl80211_finish_netdev_dump(dev);
2267 return err;
2270 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2273 struct net_device *dev = info->user_ptr[1];
2274 struct station_info sinfo;
2275 struct sk_buff *msg;
2276 u8 *mac_addr = NULL;
2277 int err;
2279 memset(&sinfo, 0, sizeof(sinfo));
2281 if (!info->attrs[NL80211_ATTR_MAC])
2282 return -EINVAL;
2284 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2286 if (!rdev->ops->get_station)
2287 return -EOPNOTSUPP;
2289 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2290 if (err)
2291 return err;
2293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2294 if (!msg)
2295 return -ENOMEM;
2297 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2298 dev, mac_addr, &sinfo) < 0) {
2299 nlmsg_free(msg);
2300 return -ENOBUFS;
2303 return genlmsg_reply(msg, info);
2307 * Get vlan interface making sure it is running and on the right wiphy.
2309 static int get_vlan(struct genl_info *info,
2310 struct cfg80211_registered_device *rdev,
2311 struct net_device **vlan)
2313 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2314 *vlan = NULL;
2316 if (vlanattr) {
2317 *vlan = dev_get_by_index(genl_info_net(info),
2318 nla_get_u32(vlanattr));
2319 if (!*vlan)
2320 return -ENODEV;
2321 if (!(*vlan)->ieee80211_ptr)
2322 return -EINVAL;
2323 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2324 return -EINVAL;
2325 if (!netif_running(*vlan))
2326 return -ENETDOWN;
2328 return 0;
2331 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2334 int err;
2335 struct net_device *dev = info->user_ptr[1];
2336 struct station_parameters params;
2337 u8 *mac_addr = NULL;
2339 memset(&params, 0, sizeof(params));
2341 params.listen_interval = -1;
2342 params.plink_state = -1;
2344 if (info->attrs[NL80211_ATTR_STA_AID])
2345 return -EINVAL;
2347 if (!info->attrs[NL80211_ATTR_MAC])
2348 return -EINVAL;
2350 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2352 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2353 params.supported_rates =
2354 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2355 params.supported_rates_len =
2356 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2359 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2360 params.listen_interval =
2361 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2363 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2364 params.ht_capa =
2365 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2367 if (parse_station_flags(info, &params))
2368 return -EINVAL;
2370 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2371 params.plink_action =
2372 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2374 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2375 params.plink_state =
2376 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2378 err = get_vlan(info, rdev, &params.vlan);
2379 if (err)
2380 goto out;
2382 /* validate settings */
2383 err = 0;
2385 switch (dev->ieee80211_ptr->iftype) {
2386 case NL80211_IFTYPE_AP:
2387 case NL80211_IFTYPE_AP_VLAN:
2388 case NL80211_IFTYPE_P2P_GO:
2389 /* disallow mesh-specific things */
2390 if (params.plink_action)
2391 err = -EINVAL;
2392 break;
2393 case NL80211_IFTYPE_P2P_CLIENT:
2394 case NL80211_IFTYPE_STATION:
2395 /* disallow everything but AUTHORIZED flag */
2396 if (params.plink_action)
2397 err = -EINVAL;
2398 if (params.vlan)
2399 err = -EINVAL;
2400 if (params.supported_rates)
2401 err = -EINVAL;
2402 if (params.ht_capa)
2403 err = -EINVAL;
2404 if (params.listen_interval >= 0)
2405 err = -EINVAL;
2406 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2407 err = -EINVAL;
2408 break;
2409 case NL80211_IFTYPE_MESH_POINT:
2410 /* disallow things mesh doesn't support */
2411 if (params.vlan)
2412 err = -EINVAL;
2413 if (params.ht_capa)
2414 err = -EINVAL;
2415 if (params.listen_interval >= 0)
2416 err = -EINVAL;
2417 if (params.sta_flags_mask &
2418 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2419 BIT(NL80211_STA_FLAG_MFP) |
2420 BIT(NL80211_STA_FLAG_AUTHORIZED)))
2421 err = -EINVAL;
2422 break;
2423 default:
2424 err = -EINVAL;
2427 if (err)
2428 goto out;
2430 if (!rdev->ops->change_station) {
2431 err = -EOPNOTSUPP;
2432 goto out;
2435 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2437 out:
2438 if (params.vlan)
2439 dev_put(params.vlan);
2441 return err;
2444 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2446 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2447 int err;
2448 struct net_device *dev = info->user_ptr[1];
2449 struct station_parameters params;
2450 u8 *mac_addr = NULL;
2452 memset(&params, 0, sizeof(params));
2454 if (!info->attrs[NL80211_ATTR_MAC])
2455 return -EINVAL;
2457 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2458 return -EINVAL;
2460 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2461 return -EINVAL;
2463 if (!info->attrs[NL80211_ATTR_STA_AID])
2464 return -EINVAL;
2466 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2467 params.supported_rates =
2468 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2469 params.supported_rates_len =
2470 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2471 params.listen_interval =
2472 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2474 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2475 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2476 return -EINVAL;
2478 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2479 params.ht_capa =
2480 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2482 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2483 params.plink_action =
2484 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2486 if (parse_station_flags(info, &params))
2487 return -EINVAL;
2489 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2490 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2491 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2492 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2493 return -EINVAL;
2495 err = get_vlan(info, rdev, &params.vlan);
2496 if (err)
2497 goto out;
2499 /* validate settings */
2500 err = 0;
2502 if (!rdev->ops->add_station) {
2503 err = -EOPNOTSUPP;
2504 goto out;
2507 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2509 out:
2510 if (params.vlan)
2511 dev_put(params.vlan);
2512 return err;
2515 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2517 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2518 struct net_device *dev = info->user_ptr[1];
2519 u8 *mac_addr = NULL;
2521 if (info->attrs[NL80211_ATTR_MAC])
2522 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2524 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2525 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2526 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2527 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2528 return -EINVAL;
2530 if (!rdev->ops->del_station)
2531 return -EOPNOTSUPP;
2533 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2536 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2537 int flags, struct net_device *dev,
2538 u8 *dst, u8 *next_hop,
2539 struct mpath_info *pinfo)
2541 void *hdr;
2542 struct nlattr *pinfoattr;
2544 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2545 if (!hdr)
2546 return -1;
2548 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2549 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2550 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2552 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2554 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2555 if (!pinfoattr)
2556 goto nla_put_failure;
2557 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2558 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2559 pinfo->frame_qlen);
2560 if (pinfo->filled & MPATH_INFO_SN)
2561 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2562 pinfo->sn);
2563 if (pinfo->filled & MPATH_INFO_METRIC)
2564 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2565 pinfo->metric);
2566 if (pinfo->filled & MPATH_INFO_EXPTIME)
2567 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2568 pinfo->exptime);
2569 if (pinfo->filled & MPATH_INFO_FLAGS)
2570 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2571 pinfo->flags);
2572 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2573 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2574 pinfo->discovery_timeout);
2575 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2576 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2577 pinfo->discovery_retries);
2579 nla_nest_end(msg, pinfoattr);
2581 return genlmsg_end(msg, hdr);
2583 nla_put_failure:
2584 genlmsg_cancel(msg, hdr);
2585 return -EMSGSIZE;
2588 static int nl80211_dump_mpath(struct sk_buff *skb,
2589 struct netlink_callback *cb)
2591 struct mpath_info pinfo;
2592 struct cfg80211_registered_device *dev;
2593 struct net_device *netdev;
2594 u8 dst[ETH_ALEN];
2595 u8 next_hop[ETH_ALEN];
2596 int path_idx = cb->args[1];
2597 int err;
2599 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2600 if (err)
2601 return err;
2603 if (!dev->ops->dump_mpath) {
2604 err = -EOPNOTSUPP;
2605 goto out_err;
2608 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2609 err = -EOPNOTSUPP;
2610 goto out_err;
2613 while (1) {
2614 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2615 dst, next_hop, &pinfo);
2616 if (err == -ENOENT)
2617 break;
2618 if (err)
2619 goto out_err;
2621 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2622 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2623 netdev, dst, next_hop,
2624 &pinfo) < 0)
2625 goto out;
2627 path_idx++;
2631 out:
2632 cb->args[1] = path_idx;
2633 err = skb->len;
2634 out_err:
2635 nl80211_finish_netdev_dump(dev);
2636 return err;
2639 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2641 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2642 int err;
2643 struct net_device *dev = info->user_ptr[1];
2644 struct mpath_info pinfo;
2645 struct sk_buff *msg;
2646 u8 *dst = NULL;
2647 u8 next_hop[ETH_ALEN];
2649 memset(&pinfo, 0, sizeof(pinfo));
2651 if (!info->attrs[NL80211_ATTR_MAC])
2652 return -EINVAL;
2654 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2656 if (!rdev->ops->get_mpath)
2657 return -EOPNOTSUPP;
2659 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2660 return -EOPNOTSUPP;
2662 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2663 if (err)
2664 return err;
2666 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2667 if (!msg)
2668 return -ENOMEM;
2670 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2671 dev, dst, next_hop, &pinfo) < 0) {
2672 nlmsg_free(msg);
2673 return -ENOBUFS;
2676 return genlmsg_reply(msg, info);
2679 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2681 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2682 struct net_device *dev = info->user_ptr[1];
2683 u8 *dst = NULL;
2684 u8 *next_hop = NULL;
2686 if (!info->attrs[NL80211_ATTR_MAC])
2687 return -EINVAL;
2689 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2690 return -EINVAL;
2692 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2693 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2695 if (!rdev->ops->change_mpath)
2696 return -EOPNOTSUPP;
2698 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2699 return -EOPNOTSUPP;
2701 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2704 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2707 struct net_device *dev = info->user_ptr[1];
2708 u8 *dst = NULL;
2709 u8 *next_hop = NULL;
2711 if (!info->attrs[NL80211_ATTR_MAC])
2712 return -EINVAL;
2714 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2715 return -EINVAL;
2717 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2718 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2720 if (!rdev->ops->add_mpath)
2721 return -EOPNOTSUPP;
2723 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2724 return -EOPNOTSUPP;
2726 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2729 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2732 struct net_device *dev = info->user_ptr[1];
2733 u8 *dst = NULL;
2735 if (info->attrs[NL80211_ATTR_MAC])
2736 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2738 if (!rdev->ops->del_mpath)
2739 return -EOPNOTSUPP;
2741 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2744 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2747 struct net_device *dev = info->user_ptr[1];
2748 struct bss_parameters params;
2750 memset(&params, 0, sizeof(params));
2751 /* default to not changing parameters */
2752 params.use_cts_prot = -1;
2753 params.use_short_preamble = -1;
2754 params.use_short_slot_time = -1;
2755 params.ap_isolate = -1;
2756 params.ht_opmode = -1;
2758 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2759 params.use_cts_prot =
2760 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2761 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2762 params.use_short_preamble =
2763 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2764 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2765 params.use_short_slot_time =
2766 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2767 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2768 params.basic_rates =
2769 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2770 params.basic_rates_len =
2771 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2773 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2774 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2775 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2776 params.ht_opmode =
2777 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
2779 if (!rdev->ops->change_bss)
2780 return -EOPNOTSUPP;
2782 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2783 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2784 return -EOPNOTSUPP;
2786 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2789 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2790 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2791 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2792 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2793 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2794 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2795 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2798 static int parse_reg_rule(struct nlattr *tb[],
2799 struct ieee80211_reg_rule *reg_rule)
2801 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2802 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2804 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2805 return -EINVAL;
2806 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2807 return -EINVAL;
2808 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2809 return -EINVAL;
2810 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2811 return -EINVAL;
2812 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2813 return -EINVAL;
2815 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2817 freq_range->start_freq_khz =
2818 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2819 freq_range->end_freq_khz =
2820 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2821 freq_range->max_bandwidth_khz =
2822 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2824 power_rule->max_eirp =
2825 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2827 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2828 power_rule->max_antenna_gain =
2829 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2831 return 0;
2834 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2836 int r;
2837 char *data = NULL;
2840 * You should only get this when cfg80211 hasn't yet initialized
2841 * completely when built-in to the kernel right between the time
2842 * window between nl80211_init() and regulatory_init(), if that is
2843 * even possible.
2845 mutex_lock(&cfg80211_mutex);
2846 if (unlikely(!cfg80211_regdomain)) {
2847 mutex_unlock(&cfg80211_mutex);
2848 return -EINPROGRESS;
2850 mutex_unlock(&cfg80211_mutex);
2852 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2853 return -EINVAL;
2855 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2857 r = regulatory_hint_user(data);
2859 return r;
2862 static int nl80211_get_mesh_config(struct sk_buff *skb,
2863 struct genl_info *info)
2865 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2866 struct net_device *dev = info->user_ptr[1];
2867 struct wireless_dev *wdev = dev->ieee80211_ptr;
2868 struct mesh_config cur_params;
2869 int err = 0;
2870 void *hdr;
2871 struct nlattr *pinfoattr;
2872 struct sk_buff *msg;
2874 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2875 return -EOPNOTSUPP;
2877 if (!rdev->ops->get_mesh_config)
2878 return -EOPNOTSUPP;
2880 wdev_lock(wdev);
2881 /* If not connected, get default parameters */
2882 if (!wdev->mesh_id_len)
2883 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2884 else
2885 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
2886 &cur_params);
2887 wdev_unlock(wdev);
2889 if (err)
2890 return err;
2892 /* Draw up a netlink message to send back */
2893 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2894 if (!msg)
2895 return -ENOMEM;
2896 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2897 NL80211_CMD_GET_MESH_CONFIG);
2898 if (!hdr)
2899 goto out;
2900 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
2901 if (!pinfoattr)
2902 goto nla_put_failure;
2903 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2904 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2905 cur_params.dot11MeshRetryTimeout);
2906 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2907 cur_params.dot11MeshConfirmTimeout);
2908 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2909 cur_params.dot11MeshHoldingTimeout);
2910 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2911 cur_params.dot11MeshMaxPeerLinks);
2912 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2913 cur_params.dot11MeshMaxRetries);
2914 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2915 cur_params.dot11MeshTTL);
2916 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2917 cur_params.element_ttl);
2918 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2919 cur_params.auto_open_plinks);
2920 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2921 cur_params.dot11MeshHWMPmaxPREQretries);
2922 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2923 cur_params.path_refresh_time);
2924 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2925 cur_params.min_discovery_timeout);
2926 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2927 cur_params.dot11MeshHWMPactivePathTimeout);
2928 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2929 cur_params.dot11MeshHWMPpreqMinInterval);
2930 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2931 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2932 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2933 cur_params.dot11MeshHWMPRootMode);
2934 nla_nest_end(msg, pinfoattr);
2935 genlmsg_end(msg, hdr);
2936 return genlmsg_reply(msg, info);
2938 nla_put_failure:
2939 genlmsg_cancel(msg, hdr);
2940 out:
2941 nlmsg_free(msg);
2942 return -ENOBUFS;
2945 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2946 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2947 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2948 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2949 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2950 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2951 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2952 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
2953 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2955 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2956 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2957 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2958 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2959 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2960 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2963 static const struct nla_policy
2964 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2965 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2966 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
2967 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
2968 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
2969 .len = IEEE80211_MAX_DATA_LEN },
2970 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
2973 static int nl80211_parse_mesh_config(struct genl_info *info,
2974 struct mesh_config *cfg,
2975 u32 *mask_out)
2977 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2978 u32 mask = 0;
2980 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2981 do {\
2982 if (table[attr_num]) {\
2983 cfg->param = nla_fn(table[attr_num]); \
2984 mask |= (1 << (attr_num - 1)); \
2986 } while (0);\
2989 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
2990 return -EINVAL;
2991 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2992 info->attrs[NL80211_ATTR_MESH_CONFIG],
2993 nl80211_meshconf_params_policy))
2994 return -EINVAL;
2996 /* This makes sure that there aren't more than 32 mesh config
2997 * parameters (otherwise our bitfield scheme would not work.) */
2998 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3000 /* Fill in the params struct */
3001 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3002 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3003 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3004 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3005 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3006 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3007 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3008 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3009 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3010 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3011 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3012 mask, NL80211_MESHCONF_TTL, nla_get_u8);
3013 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3014 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
3015 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3016 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3017 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3018 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3019 nla_get_u8);
3020 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3021 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3022 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3023 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3024 nla_get_u16);
3025 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3026 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3027 nla_get_u32);
3028 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3029 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3030 nla_get_u16);
3031 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3032 dot11MeshHWMPnetDiameterTraversalTime,
3033 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3034 nla_get_u16);
3035 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3036 dot11MeshHWMPRootMode, mask,
3037 NL80211_MESHCONF_HWMP_ROOTMODE,
3038 nla_get_u8);
3039 if (mask_out)
3040 *mask_out = mask;
3042 return 0;
3044 #undef FILL_IN_MESH_PARAM_IF_SET
3047 static int nl80211_parse_mesh_setup(struct genl_info *info,
3048 struct mesh_setup *setup)
3050 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3052 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3053 return -EINVAL;
3054 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3055 info->attrs[NL80211_ATTR_MESH_SETUP],
3056 nl80211_mesh_setup_params_policy))
3057 return -EINVAL;
3059 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3060 setup->path_sel_proto =
3061 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3062 IEEE80211_PATH_PROTOCOL_VENDOR :
3063 IEEE80211_PATH_PROTOCOL_HWMP;
3065 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3066 setup->path_metric =
3067 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3068 IEEE80211_PATH_METRIC_VENDOR :
3069 IEEE80211_PATH_METRIC_AIRTIME;
3072 if (tb[NL80211_MESH_SETUP_IE]) {
3073 struct nlattr *ieattr =
3074 tb[NL80211_MESH_SETUP_IE];
3075 if (!is_valid_ie_attr(ieattr))
3076 return -EINVAL;
3077 setup->ie = nla_data(ieattr);
3078 setup->ie_len = nla_len(ieattr);
3080 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3081 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3083 return 0;
3086 static int nl80211_update_mesh_config(struct sk_buff *skb,
3087 struct genl_info *info)
3089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3090 struct net_device *dev = info->user_ptr[1];
3091 struct wireless_dev *wdev = dev->ieee80211_ptr;
3092 struct mesh_config cfg;
3093 u32 mask;
3094 int err;
3096 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3097 return -EOPNOTSUPP;
3099 if (!rdev->ops->update_mesh_config)
3100 return -EOPNOTSUPP;
3102 err = nl80211_parse_mesh_config(info, &cfg, &mask);
3103 if (err)
3104 return err;
3106 wdev_lock(wdev);
3107 if (!wdev->mesh_id_len)
3108 err = -ENOLINK;
3110 if (!err)
3111 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3112 mask, &cfg);
3114 wdev_unlock(wdev);
3116 return err;
3119 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3121 struct sk_buff *msg;
3122 void *hdr = NULL;
3123 struct nlattr *nl_reg_rules;
3124 unsigned int i;
3125 int err = -EINVAL;
3127 mutex_lock(&cfg80211_mutex);
3129 if (!cfg80211_regdomain)
3130 goto out;
3132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3133 if (!msg) {
3134 err = -ENOBUFS;
3135 goto out;
3138 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3139 NL80211_CMD_GET_REG);
3140 if (!hdr)
3141 goto put_failure;
3143 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3144 cfg80211_regdomain->alpha2);
3146 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3147 if (!nl_reg_rules)
3148 goto nla_put_failure;
3150 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3151 struct nlattr *nl_reg_rule;
3152 const struct ieee80211_reg_rule *reg_rule;
3153 const struct ieee80211_freq_range *freq_range;
3154 const struct ieee80211_power_rule *power_rule;
3156 reg_rule = &cfg80211_regdomain->reg_rules[i];
3157 freq_range = &reg_rule->freq_range;
3158 power_rule = &reg_rule->power_rule;
3160 nl_reg_rule = nla_nest_start(msg, i);
3161 if (!nl_reg_rule)
3162 goto nla_put_failure;
3164 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3165 reg_rule->flags);
3166 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3167 freq_range->start_freq_khz);
3168 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3169 freq_range->end_freq_khz);
3170 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3171 freq_range->max_bandwidth_khz);
3172 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3173 power_rule->max_antenna_gain);
3174 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3175 power_rule->max_eirp);
3177 nla_nest_end(msg, nl_reg_rule);
3180 nla_nest_end(msg, nl_reg_rules);
3182 genlmsg_end(msg, hdr);
3183 err = genlmsg_reply(msg, info);
3184 goto out;
3186 nla_put_failure:
3187 genlmsg_cancel(msg, hdr);
3188 put_failure:
3189 nlmsg_free(msg);
3190 err = -EMSGSIZE;
3191 out:
3192 mutex_unlock(&cfg80211_mutex);
3193 return err;
3196 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3198 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3199 struct nlattr *nl_reg_rule;
3200 char *alpha2 = NULL;
3201 int rem_reg_rules = 0, r = 0;
3202 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3203 struct ieee80211_regdomain *rd = NULL;
3205 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3206 return -EINVAL;
3208 if (!info->attrs[NL80211_ATTR_REG_RULES])
3209 return -EINVAL;
3211 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3213 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3214 rem_reg_rules) {
3215 num_rules++;
3216 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3217 return -EINVAL;
3220 mutex_lock(&cfg80211_mutex);
3222 if (!reg_is_valid_request(alpha2)) {
3223 r = -EINVAL;
3224 goto bad_reg;
3227 size_of_regd = sizeof(struct ieee80211_regdomain) +
3228 (num_rules * sizeof(struct ieee80211_reg_rule));
3230 rd = kzalloc(size_of_regd, GFP_KERNEL);
3231 if (!rd) {
3232 r = -ENOMEM;
3233 goto bad_reg;
3236 rd->n_reg_rules = num_rules;
3237 rd->alpha2[0] = alpha2[0];
3238 rd->alpha2[1] = alpha2[1];
3240 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3241 rem_reg_rules) {
3242 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3243 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3244 reg_rule_policy);
3245 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3246 if (r)
3247 goto bad_reg;
3249 rule_idx++;
3251 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3252 r = -EINVAL;
3253 goto bad_reg;
3257 BUG_ON(rule_idx != num_rules);
3259 r = set_regdom(rd);
3261 mutex_unlock(&cfg80211_mutex);
3263 return r;
3265 bad_reg:
3266 mutex_unlock(&cfg80211_mutex);
3267 kfree(rd);
3268 return r;
3271 static int validate_scan_freqs(struct nlattr *freqs)
3273 struct nlattr *attr1, *attr2;
3274 int n_channels = 0, tmp1, tmp2;
3276 nla_for_each_nested(attr1, freqs, tmp1) {
3277 n_channels++;
3279 * Some hardware has a limited channel list for
3280 * scanning, and it is pretty much nonsensical
3281 * to scan for a channel twice, so disallow that
3282 * and don't require drivers to check that the
3283 * channel list they get isn't longer than what
3284 * they can scan, as long as they can scan all
3285 * the channels they registered at once.
3287 nla_for_each_nested(attr2, freqs, tmp2)
3288 if (attr1 != attr2 &&
3289 nla_get_u32(attr1) == nla_get_u32(attr2))
3290 return 0;
3293 return n_channels;
3296 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3298 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3299 struct net_device *dev = info->user_ptr[1];
3300 struct cfg80211_scan_request *request;
3301 struct nlattr *attr;
3302 struct wiphy *wiphy;
3303 int err, tmp, n_ssids = 0, n_channels, i;
3304 enum ieee80211_band band;
3305 size_t ie_len;
3307 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3308 return -EINVAL;
3310 wiphy = &rdev->wiphy;
3312 if (!rdev->ops->scan)
3313 return -EOPNOTSUPP;
3315 if (rdev->scan_req)
3316 return -EBUSY;
3318 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3319 n_channels = validate_scan_freqs(
3320 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3321 if (!n_channels)
3322 return -EINVAL;
3323 } else {
3324 n_channels = 0;
3326 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3327 if (wiphy->bands[band])
3328 n_channels += wiphy->bands[band]->n_channels;
3331 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3332 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3333 n_ssids++;
3335 if (n_ssids > wiphy->max_scan_ssids)
3336 return -EINVAL;
3338 if (info->attrs[NL80211_ATTR_IE])
3339 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3340 else
3341 ie_len = 0;
3343 if (ie_len > wiphy->max_scan_ie_len)
3344 return -EINVAL;
3346 request = kzalloc(sizeof(*request)
3347 + sizeof(*request->ssids) * n_ssids
3348 + sizeof(*request->channels) * n_channels
3349 + ie_len, GFP_KERNEL);
3350 if (!request)
3351 return -ENOMEM;
3353 if (n_ssids)
3354 request->ssids = (void *)&request->channels[n_channels];
3355 request->n_ssids = n_ssids;
3356 if (ie_len) {
3357 if (request->ssids)
3358 request->ie = (void *)(request->ssids + n_ssids);
3359 else
3360 request->ie = (void *)(request->channels + n_channels);
3363 i = 0;
3364 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3365 /* user specified, bail out if channel not found */
3366 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3367 struct ieee80211_channel *chan;
3369 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3371 if (!chan) {
3372 err = -EINVAL;
3373 goto out_free;
3376 /* ignore disabled channels */
3377 if (chan->flags & IEEE80211_CHAN_DISABLED)
3378 continue;
3380 request->channels[i] = chan;
3381 i++;
3383 } else {
3384 /* all channels */
3385 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3386 int j;
3387 if (!wiphy->bands[band])
3388 continue;
3389 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3390 struct ieee80211_channel *chan;
3392 chan = &wiphy->bands[band]->channels[j];
3394 if (chan->flags & IEEE80211_CHAN_DISABLED)
3395 continue;
3397 request->channels[i] = chan;
3398 i++;
3403 if (!i) {
3404 err = -EINVAL;
3405 goto out_free;
3408 request->n_channels = i;
3410 i = 0;
3411 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3412 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3413 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3414 err = -EINVAL;
3415 goto out_free;
3417 request->ssids[i].ssid_len = nla_len(attr);
3418 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3419 i++;
3423 if (info->attrs[NL80211_ATTR_IE]) {
3424 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3425 memcpy((void *)request->ie,
3426 nla_data(info->attrs[NL80211_ATTR_IE]),
3427 request->ie_len);
3430 request->dev = dev;
3431 request->wiphy = &rdev->wiphy;
3433 rdev->scan_req = request;
3434 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3436 if (!err) {
3437 nl80211_send_scan_start(rdev, dev);
3438 dev_hold(dev);
3439 } else {
3440 out_free:
3441 rdev->scan_req = NULL;
3442 kfree(request);
3445 return err;
3448 static int nl80211_start_sched_scan(struct sk_buff *skb,
3449 struct genl_info *info)
3451 struct cfg80211_sched_scan_request *request;
3452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3453 struct net_device *dev = info->user_ptr[1];
3454 struct nlattr *attr;
3455 struct wiphy *wiphy;
3456 int err, tmp, n_ssids = 0, n_channels, i;
3457 u32 interval;
3458 enum ieee80211_band band;
3459 size_t ie_len;
3461 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3462 !rdev->ops->sched_scan_start)
3463 return -EOPNOTSUPP;
3465 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3466 return -EINVAL;
3468 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3469 return -EINVAL;
3471 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3472 if (interval == 0)
3473 return -EINVAL;
3475 wiphy = &rdev->wiphy;
3477 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3478 n_channels = validate_scan_freqs(
3479 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3480 if (!n_channels)
3481 return -EINVAL;
3482 } else {
3483 n_channels = 0;
3485 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3486 if (wiphy->bands[band])
3487 n_channels += wiphy->bands[band]->n_channels;
3490 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3491 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3492 tmp)
3493 n_ssids++;
3495 if (n_ssids > wiphy->max_scan_ssids)
3496 return -EINVAL;
3498 if (info->attrs[NL80211_ATTR_IE])
3499 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3500 else
3501 ie_len = 0;
3503 if (ie_len > wiphy->max_scan_ie_len)
3504 return -EINVAL;
3506 mutex_lock(&rdev->sched_scan_mtx);
3508 if (rdev->sched_scan_req) {
3509 err = -EINPROGRESS;
3510 goto out;
3513 request = kzalloc(sizeof(*request)
3514 + sizeof(*request->ssids) * n_ssids
3515 + sizeof(*request->channels) * n_channels
3516 + ie_len, GFP_KERNEL);
3517 if (!request) {
3518 err = -ENOMEM;
3519 goto out;
3522 if (n_ssids)
3523 request->ssids = (void *)&request->channels[n_channels];
3524 request->n_ssids = n_ssids;
3525 if (ie_len) {
3526 if (request->ssids)
3527 request->ie = (void *)(request->ssids + n_ssids);
3528 else
3529 request->ie = (void *)(request->channels + n_channels);
3532 i = 0;
3533 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3534 /* user specified, bail out if channel not found */
3535 nla_for_each_nested(attr,
3536 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3537 tmp) {
3538 struct ieee80211_channel *chan;
3540 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3542 if (!chan) {
3543 err = -EINVAL;
3544 goto out_free;
3547 /* ignore disabled channels */
3548 if (chan->flags & IEEE80211_CHAN_DISABLED)
3549 continue;
3551 request->channels[i] = chan;
3552 i++;
3554 } else {
3555 /* all channels */
3556 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3557 int j;
3558 if (!wiphy->bands[band])
3559 continue;
3560 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3561 struct ieee80211_channel *chan;
3563 chan = &wiphy->bands[band]->channels[j];
3565 if (chan->flags & IEEE80211_CHAN_DISABLED)
3566 continue;
3568 request->channels[i] = chan;
3569 i++;
3574 if (!i) {
3575 err = -EINVAL;
3576 goto out_free;
3579 request->n_channels = i;
3581 i = 0;
3582 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3583 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3584 tmp) {
3585 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3586 err = -EINVAL;
3587 goto out_free;
3589 request->ssids[i].ssid_len = nla_len(attr);
3590 memcpy(request->ssids[i].ssid, nla_data(attr),
3591 nla_len(attr));
3592 i++;
3596 if (info->attrs[NL80211_ATTR_IE]) {
3597 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3598 memcpy((void *)request->ie,
3599 nla_data(info->attrs[NL80211_ATTR_IE]),
3600 request->ie_len);
3603 request->dev = dev;
3604 request->wiphy = &rdev->wiphy;
3605 request->interval = interval;
3607 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3608 if (!err) {
3609 rdev->sched_scan_req = request;
3610 nl80211_send_sched_scan(rdev, dev,
3611 NL80211_CMD_START_SCHED_SCAN);
3612 goto out;
3615 out_free:
3616 kfree(request);
3617 out:
3618 mutex_unlock(&rdev->sched_scan_mtx);
3619 return err;
3622 static int nl80211_stop_sched_scan(struct sk_buff *skb,
3623 struct genl_info *info)
3625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3626 int err;
3628 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3629 !rdev->ops->sched_scan_stop)
3630 return -EOPNOTSUPP;
3632 mutex_lock(&rdev->sched_scan_mtx);
3633 err = __cfg80211_stop_sched_scan(rdev, false);
3634 mutex_unlock(&rdev->sched_scan_mtx);
3636 return err;
3639 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3640 struct cfg80211_registered_device *rdev,
3641 struct wireless_dev *wdev,
3642 struct cfg80211_internal_bss *intbss)
3644 struct cfg80211_bss *res = &intbss->pub;
3645 void *hdr;
3646 struct nlattr *bss;
3647 int i;
3649 ASSERT_WDEV_LOCK(wdev);
3651 hdr = nl80211hdr_put(msg, pid, seq, flags,
3652 NL80211_CMD_NEW_SCAN_RESULTS);
3653 if (!hdr)
3654 return -1;
3656 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3657 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3659 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3660 if (!bss)
3661 goto nla_put_failure;
3662 if (!is_zero_ether_addr(res->bssid))
3663 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3664 if (res->information_elements && res->len_information_elements)
3665 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3666 res->len_information_elements,
3667 res->information_elements);
3668 if (res->beacon_ies && res->len_beacon_ies &&
3669 res->beacon_ies != res->information_elements)
3670 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3671 res->len_beacon_ies, res->beacon_ies);
3672 if (res->tsf)
3673 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3674 if (res->beacon_interval)
3675 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3676 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3677 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3678 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3679 jiffies_to_msecs(jiffies - intbss->ts));
3681 switch (rdev->wiphy.signal_type) {
3682 case CFG80211_SIGNAL_TYPE_MBM:
3683 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3684 break;
3685 case CFG80211_SIGNAL_TYPE_UNSPEC:
3686 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3687 break;
3688 default:
3689 break;
3692 switch (wdev->iftype) {
3693 case NL80211_IFTYPE_P2P_CLIENT:
3694 case NL80211_IFTYPE_STATION:
3695 if (intbss == wdev->current_bss)
3696 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3697 NL80211_BSS_STATUS_ASSOCIATED);
3698 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3699 if (intbss != wdev->auth_bsses[i])
3700 continue;
3701 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3702 NL80211_BSS_STATUS_AUTHENTICATED);
3703 break;
3705 break;
3706 case NL80211_IFTYPE_ADHOC:
3707 if (intbss == wdev->current_bss)
3708 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3709 NL80211_BSS_STATUS_IBSS_JOINED);
3710 break;
3711 default:
3712 break;
3715 nla_nest_end(msg, bss);
3717 return genlmsg_end(msg, hdr);
3719 nla_put_failure:
3720 genlmsg_cancel(msg, hdr);
3721 return -EMSGSIZE;
3724 static int nl80211_dump_scan(struct sk_buff *skb,
3725 struct netlink_callback *cb)
3727 struct cfg80211_registered_device *rdev;
3728 struct net_device *dev;
3729 struct cfg80211_internal_bss *scan;
3730 struct wireless_dev *wdev;
3731 int start = cb->args[1], idx = 0;
3732 int err;
3734 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3735 if (err)
3736 return err;
3738 wdev = dev->ieee80211_ptr;
3740 wdev_lock(wdev);
3741 spin_lock_bh(&rdev->bss_lock);
3742 cfg80211_bss_expire(rdev);
3744 list_for_each_entry(scan, &rdev->bss_list, list) {
3745 if (++idx <= start)
3746 continue;
3747 if (nl80211_send_bss(skb,
3748 NETLINK_CB(cb->skb).pid,
3749 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3750 rdev, wdev, scan) < 0) {
3751 idx--;
3752 break;
3756 spin_unlock_bh(&rdev->bss_lock);
3757 wdev_unlock(wdev);
3759 cb->args[1] = idx;
3760 nl80211_finish_netdev_dump(rdev);
3762 return skb->len;
3765 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3766 int flags, struct net_device *dev,
3767 struct survey_info *survey)
3769 void *hdr;
3770 struct nlattr *infoattr;
3772 /* Survey without a channel doesn't make sense */
3773 if (!survey->channel)
3774 return -EINVAL;
3776 hdr = nl80211hdr_put(msg, pid, seq, flags,
3777 NL80211_CMD_NEW_SURVEY_RESULTS);
3778 if (!hdr)
3779 return -ENOMEM;
3781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3783 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3784 if (!infoattr)
3785 goto nla_put_failure;
3787 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3788 survey->channel->center_freq);
3789 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3790 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3791 survey->noise);
3792 if (survey->filled & SURVEY_INFO_IN_USE)
3793 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
3794 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3795 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3796 survey->channel_time);
3797 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3798 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3799 survey->channel_time_busy);
3800 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3801 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3802 survey->channel_time_ext_busy);
3803 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3804 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3805 survey->channel_time_rx);
3806 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3807 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3808 survey->channel_time_tx);
3810 nla_nest_end(msg, infoattr);
3812 return genlmsg_end(msg, hdr);
3814 nla_put_failure:
3815 genlmsg_cancel(msg, hdr);
3816 return -EMSGSIZE;
3819 static int nl80211_dump_survey(struct sk_buff *skb,
3820 struct netlink_callback *cb)
3822 struct survey_info survey;
3823 struct cfg80211_registered_device *dev;
3824 struct net_device *netdev;
3825 int survey_idx = cb->args[1];
3826 int res;
3828 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3829 if (res)
3830 return res;
3832 if (!dev->ops->dump_survey) {
3833 res = -EOPNOTSUPP;
3834 goto out_err;
3837 while (1) {
3838 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3839 &survey);
3840 if (res == -ENOENT)
3841 break;
3842 if (res)
3843 goto out_err;
3845 if (nl80211_send_survey(skb,
3846 NETLINK_CB(cb->skb).pid,
3847 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3848 netdev,
3849 &survey) < 0)
3850 goto out;
3851 survey_idx++;
3854 out:
3855 cb->args[1] = survey_idx;
3856 res = skb->len;
3857 out_err:
3858 nl80211_finish_netdev_dump(dev);
3859 return res;
3862 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3864 return auth_type <= NL80211_AUTHTYPE_MAX;
3867 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3869 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3870 NL80211_WPA_VERSION_2));
3873 static bool nl80211_valid_akm_suite(u32 akm)
3875 return akm == WLAN_AKM_SUITE_8021X ||
3876 akm == WLAN_AKM_SUITE_PSK;
3879 static bool nl80211_valid_cipher_suite(u32 cipher)
3881 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3882 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3883 cipher == WLAN_CIPHER_SUITE_TKIP ||
3884 cipher == WLAN_CIPHER_SUITE_CCMP ||
3885 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3889 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3892 struct net_device *dev = info->user_ptr[1];
3893 struct ieee80211_channel *chan;
3894 const u8 *bssid, *ssid, *ie = NULL;
3895 int err, ssid_len, ie_len = 0;
3896 enum nl80211_auth_type auth_type;
3897 struct key_parse key;
3898 bool local_state_change;
3900 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3901 return -EINVAL;
3903 if (!info->attrs[NL80211_ATTR_MAC])
3904 return -EINVAL;
3906 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3907 return -EINVAL;
3909 if (!info->attrs[NL80211_ATTR_SSID])
3910 return -EINVAL;
3912 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3913 return -EINVAL;
3915 err = nl80211_parse_key(info, &key);
3916 if (err)
3917 return err;
3919 if (key.idx >= 0) {
3920 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3921 return -EINVAL;
3922 if (!key.p.key || !key.p.key_len)
3923 return -EINVAL;
3924 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3925 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3926 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3927 key.p.key_len != WLAN_KEY_LEN_WEP104))
3928 return -EINVAL;
3929 if (key.idx > 4)
3930 return -EINVAL;
3931 } else {
3932 key.p.key_len = 0;
3933 key.p.key = NULL;
3936 if (key.idx >= 0) {
3937 int i;
3938 bool ok = false;
3939 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3940 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3941 ok = true;
3942 break;
3945 if (!ok)
3946 return -EINVAL;
3949 if (!rdev->ops->auth)
3950 return -EOPNOTSUPP;
3952 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3953 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3954 return -EOPNOTSUPP;
3956 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3957 chan = ieee80211_get_channel(&rdev->wiphy,
3958 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3959 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3960 return -EINVAL;
3962 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3963 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3965 if (info->attrs[NL80211_ATTR_IE]) {
3966 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3967 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3970 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3971 if (!nl80211_valid_auth_type(auth_type))
3972 return -EINVAL;
3974 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3976 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3977 ssid, ssid_len, ie, ie_len,
3978 key.p.key, key.p.key_len, key.idx,
3979 local_state_change);
3982 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3983 struct genl_info *info,
3984 struct cfg80211_crypto_settings *settings,
3985 int cipher_limit)
3987 memset(settings, 0, sizeof(*settings));
3989 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3991 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3992 u16 proto;
3993 proto = nla_get_u16(
3994 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3995 settings->control_port_ethertype = cpu_to_be16(proto);
3996 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3997 proto != ETH_P_PAE)
3998 return -EINVAL;
3999 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4000 settings->control_port_no_encrypt = true;
4001 } else
4002 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4004 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4005 void *data;
4006 int len, i;
4008 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4009 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4010 settings->n_ciphers_pairwise = len / sizeof(u32);
4012 if (len % sizeof(u32))
4013 return -EINVAL;
4015 if (settings->n_ciphers_pairwise > cipher_limit)
4016 return -EINVAL;
4018 memcpy(settings->ciphers_pairwise, data, len);
4020 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4021 if (!nl80211_valid_cipher_suite(
4022 settings->ciphers_pairwise[i]))
4023 return -EINVAL;
4026 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4027 settings->cipher_group =
4028 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4029 if (!nl80211_valid_cipher_suite(settings->cipher_group))
4030 return -EINVAL;
4033 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4034 settings->wpa_versions =
4035 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4036 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4037 return -EINVAL;
4040 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4041 void *data;
4042 int len, i;
4044 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4045 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4046 settings->n_akm_suites = len / sizeof(u32);
4048 if (len % sizeof(u32))
4049 return -EINVAL;
4051 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4052 return -EINVAL;
4054 memcpy(settings->akm_suites, data, len);
4056 for (i = 0; i < settings->n_akm_suites; i++)
4057 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
4058 return -EINVAL;
4061 return 0;
4064 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4066 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4067 struct net_device *dev = info->user_ptr[1];
4068 struct cfg80211_crypto_settings crypto;
4069 struct ieee80211_channel *chan;
4070 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4071 int err, ssid_len, ie_len = 0;
4072 bool use_mfp = false;
4074 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4075 return -EINVAL;
4077 if (!info->attrs[NL80211_ATTR_MAC] ||
4078 !info->attrs[NL80211_ATTR_SSID] ||
4079 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4080 return -EINVAL;
4082 if (!rdev->ops->assoc)
4083 return -EOPNOTSUPP;
4085 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4086 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4087 return -EOPNOTSUPP;
4089 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4091 chan = ieee80211_get_channel(&rdev->wiphy,
4092 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4093 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4094 return -EINVAL;
4096 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4097 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4099 if (info->attrs[NL80211_ATTR_IE]) {
4100 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4101 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4104 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4105 enum nl80211_mfp mfp =
4106 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4107 if (mfp == NL80211_MFP_REQUIRED)
4108 use_mfp = true;
4109 else if (mfp != NL80211_MFP_NO)
4110 return -EINVAL;
4113 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4114 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4116 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4117 if (!err)
4118 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4119 ssid, ssid_len, ie, ie_len, use_mfp,
4120 &crypto);
4122 return err;
4125 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4128 struct net_device *dev = info->user_ptr[1];
4129 const u8 *ie = NULL, *bssid;
4130 int ie_len = 0;
4131 u16 reason_code;
4132 bool local_state_change;
4134 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4135 return -EINVAL;
4137 if (!info->attrs[NL80211_ATTR_MAC])
4138 return -EINVAL;
4140 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4141 return -EINVAL;
4143 if (!rdev->ops->deauth)
4144 return -EOPNOTSUPP;
4146 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4147 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4148 return -EOPNOTSUPP;
4150 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4152 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4153 if (reason_code == 0) {
4154 /* Reason Code 0 is reserved */
4155 return -EINVAL;
4158 if (info->attrs[NL80211_ATTR_IE]) {
4159 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4160 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4163 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4165 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4166 local_state_change);
4169 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4171 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4172 struct net_device *dev = info->user_ptr[1];
4173 const u8 *ie = NULL, *bssid;
4174 int ie_len = 0;
4175 u16 reason_code;
4176 bool local_state_change;
4178 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4179 return -EINVAL;
4181 if (!info->attrs[NL80211_ATTR_MAC])
4182 return -EINVAL;
4184 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4185 return -EINVAL;
4187 if (!rdev->ops->disassoc)
4188 return -EOPNOTSUPP;
4190 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4191 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4192 return -EOPNOTSUPP;
4194 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4196 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4197 if (reason_code == 0) {
4198 /* Reason Code 0 is reserved */
4199 return -EINVAL;
4202 if (info->attrs[NL80211_ATTR_IE]) {
4203 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4204 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4207 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4209 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4210 local_state_change);
4213 static bool
4214 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4215 int mcast_rate[IEEE80211_NUM_BANDS],
4216 int rateval)
4218 struct wiphy *wiphy = &rdev->wiphy;
4219 bool found = false;
4220 int band, i;
4222 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4223 struct ieee80211_supported_band *sband;
4225 sband = wiphy->bands[band];
4226 if (!sband)
4227 continue;
4229 for (i = 0; i < sband->n_bitrates; i++) {
4230 if (sband->bitrates[i].bitrate == rateval) {
4231 mcast_rate[band] = i + 1;
4232 found = true;
4233 break;
4238 return found;
4241 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4243 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4244 struct net_device *dev = info->user_ptr[1];
4245 struct cfg80211_ibss_params ibss;
4246 struct wiphy *wiphy;
4247 struct cfg80211_cached_keys *connkeys = NULL;
4248 int err;
4250 memset(&ibss, 0, sizeof(ibss));
4252 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4253 return -EINVAL;
4255 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4256 !info->attrs[NL80211_ATTR_SSID] ||
4257 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4258 return -EINVAL;
4260 ibss.beacon_interval = 100;
4262 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4263 ibss.beacon_interval =
4264 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4265 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4266 return -EINVAL;
4269 if (!rdev->ops->join_ibss)
4270 return -EOPNOTSUPP;
4272 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4273 return -EOPNOTSUPP;
4275 wiphy = &rdev->wiphy;
4277 if (info->attrs[NL80211_ATTR_MAC])
4278 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4279 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4280 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4282 if (info->attrs[NL80211_ATTR_IE]) {
4283 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4284 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4287 ibss.channel = ieee80211_get_channel(wiphy,
4288 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4289 if (!ibss.channel ||
4290 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4291 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4292 return -EINVAL;
4294 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
4295 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4297 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4298 u8 *rates =
4299 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4300 int n_rates =
4301 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4302 struct ieee80211_supported_band *sband =
4303 wiphy->bands[ibss.channel->band];
4304 int i, j;
4306 if (n_rates == 0)
4307 return -EINVAL;
4309 for (i = 0; i < n_rates; i++) {
4310 int rate = (rates[i] & 0x7f) * 5;
4311 bool found = false;
4313 for (j = 0; j < sband->n_bitrates; j++) {
4314 if (sband->bitrates[j].bitrate == rate) {
4315 found = true;
4316 ibss.basic_rates |= BIT(j);
4317 break;
4320 if (!found)
4321 return -EINVAL;
4325 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4326 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4327 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4328 return -EINVAL;
4330 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4331 connkeys = nl80211_parse_connkeys(rdev,
4332 info->attrs[NL80211_ATTR_KEYS]);
4333 if (IS_ERR(connkeys))
4334 return PTR_ERR(connkeys);
4337 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
4338 if (err)
4339 kfree(connkeys);
4340 return err;
4343 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4346 struct net_device *dev = info->user_ptr[1];
4348 if (!rdev->ops->leave_ibss)
4349 return -EOPNOTSUPP;
4351 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4352 return -EOPNOTSUPP;
4354 return cfg80211_leave_ibss(rdev, dev, false);
4357 #ifdef CONFIG_NL80211_TESTMODE
4358 static struct genl_multicast_group nl80211_testmode_mcgrp = {
4359 .name = "testmode",
4362 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4365 int err;
4367 if (!info->attrs[NL80211_ATTR_TESTDATA])
4368 return -EINVAL;
4370 err = -EOPNOTSUPP;
4371 if (rdev->ops->testmode_cmd) {
4372 rdev->testmode_info = info;
4373 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4374 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4375 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4376 rdev->testmode_info = NULL;
4379 return err;
4382 static struct sk_buff *
4383 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4384 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4386 struct sk_buff *skb;
4387 void *hdr;
4388 struct nlattr *data;
4390 skb = nlmsg_new(approxlen + 100, gfp);
4391 if (!skb)
4392 return NULL;
4394 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4395 if (!hdr) {
4396 kfree_skb(skb);
4397 return NULL;
4400 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4401 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4403 ((void **)skb->cb)[0] = rdev;
4404 ((void **)skb->cb)[1] = hdr;
4405 ((void **)skb->cb)[2] = data;
4407 return skb;
4409 nla_put_failure:
4410 kfree_skb(skb);
4411 return NULL;
4414 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4415 int approxlen)
4417 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4419 if (WARN_ON(!rdev->testmode_info))
4420 return NULL;
4422 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4423 rdev->testmode_info->snd_pid,
4424 rdev->testmode_info->snd_seq,
4425 GFP_KERNEL);
4427 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4429 int cfg80211_testmode_reply(struct sk_buff *skb)
4431 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4432 void *hdr = ((void **)skb->cb)[1];
4433 struct nlattr *data = ((void **)skb->cb)[2];
4435 if (WARN_ON(!rdev->testmode_info)) {
4436 kfree_skb(skb);
4437 return -EINVAL;
4440 nla_nest_end(skb, data);
4441 genlmsg_end(skb, hdr);
4442 return genlmsg_reply(skb, rdev->testmode_info);
4444 EXPORT_SYMBOL(cfg80211_testmode_reply);
4446 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4447 int approxlen, gfp_t gfp)
4449 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4451 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4453 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4455 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4457 void *hdr = ((void **)skb->cb)[1];
4458 struct nlattr *data = ((void **)skb->cb)[2];
4460 nla_nest_end(skb, data);
4461 genlmsg_end(skb, hdr);
4462 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4464 EXPORT_SYMBOL(cfg80211_testmode_event);
4465 #endif
4467 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4470 struct net_device *dev = info->user_ptr[1];
4471 struct cfg80211_connect_params connect;
4472 struct wiphy *wiphy;
4473 struct cfg80211_cached_keys *connkeys = NULL;
4474 int err;
4476 memset(&connect, 0, sizeof(connect));
4478 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4479 return -EINVAL;
4481 if (!info->attrs[NL80211_ATTR_SSID] ||
4482 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4483 return -EINVAL;
4485 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4486 connect.auth_type =
4487 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4488 if (!nl80211_valid_auth_type(connect.auth_type))
4489 return -EINVAL;
4490 } else
4491 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4493 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4495 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
4496 NL80211_MAX_NR_CIPHER_SUITES);
4497 if (err)
4498 return err;
4500 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4501 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4502 return -EOPNOTSUPP;
4504 wiphy = &rdev->wiphy;
4506 if (info->attrs[NL80211_ATTR_MAC])
4507 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4508 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4509 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4511 if (info->attrs[NL80211_ATTR_IE]) {
4512 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4513 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4516 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4517 connect.channel =
4518 ieee80211_get_channel(wiphy,
4519 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4520 if (!connect.channel ||
4521 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4522 return -EINVAL;
4525 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4526 connkeys = nl80211_parse_connkeys(rdev,
4527 info->attrs[NL80211_ATTR_KEYS]);
4528 if (IS_ERR(connkeys))
4529 return PTR_ERR(connkeys);
4532 err = cfg80211_connect(rdev, dev, &connect, connkeys);
4533 if (err)
4534 kfree(connkeys);
4535 return err;
4538 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4541 struct net_device *dev = info->user_ptr[1];
4542 u16 reason;
4544 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4545 reason = WLAN_REASON_DEAUTH_LEAVING;
4546 else
4547 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4549 if (reason == 0)
4550 return -EINVAL;
4552 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4553 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4554 return -EOPNOTSUPP;
4556 return cfg80211_disconnect(rdev, dev, reason, true);
4559 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4562 struct net *net;
4563 int err;
4564 u32 pid;
4566 if (!info->attrs[NL80211_ATTR_PID])
4567 return -EINVAL;
4569 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4571 net = get_net_ns_by_pid(pid);
4572 if (IS_ERR(net))
4573 return PTR_ERR(net);
4575 err = 0;
4577 /* check if anything to do */
4578 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4579 err = cfg80211_switch_netns(rdev, net);
4581 put_net(net);
4582 return err;
4585 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4588 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4589 struct cfg80211_pmksa *pmksa) = NULL;
4590 struct net_device *dev = info->user_ptr[1];
4591 struct cfg80211_pmksa pmksa;
4593 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4595 if (!info->attrs[NL80211_ATTR_MAC])
4596 return -EINVAL;
4598 if (!info->attrs[NL80211_ATTR_PMKID])
4599 return -EINVAL;
4601 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4602 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4604 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4605 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4606 return -EOPNOTSUPP;
4608 switch (info->genlhdr->cmd) {
4609 case NL80211_CMD_SET_PMKSA:
4610 rdev_ops = rdev->ops->set_pmksa;
4611 break;
4612 case NL80211_CMD_DEL_PMKSA:
4613 rdev_ops = rdev->ops->del_pmksa;
4614 break;
4615 default:
4616 WARN_ON(1);
4617 break;
4620 if (!rdev_ops)
4621 return -EOPNOTSUPP;
4623 return rdev_ops(&rdev->wiphy, dev, &pmksa);
4626 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4628 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4629 struct net_device *dev = info->user_ptr[1];
4631 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4632 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4633 return -EOPNOTSUPP;
4635 if (!rdev->ops->flush_pmksa)
4636 return -EOPNOTSUPP;
4638 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4641 static int nl80211_remain_on_channel(struct sk_buff *skb,
4642 struct genl_info *info)
4644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4645 struct net_device *dev = info->user_ptr[1];
4646 struct ieee80211_channel *chan;
4647 struct sk_buff *msg;
4648 void *hdr;
4649 u64 cookie;
4650 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4651 u32 freq, duration;
4652 int err;
4654 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4655 !info->attrs[NL80211_ATTR_DURATION])
4656 return -EINVAL;
4658 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4661 * We should be on that channel for at least one jiffie,
4662 * and more than 5 seconds seems excessive.
4664 if (!duration || !msecs_to_jiffies(duration) ||
4665 duration > rdev->wiphy.max_remain_on_channel_duration)
4666 return -EINVAL;
4668 if (!rdev->ops->remain_on_channel)
4669 return -EOPNOTSUPP;
4671 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4672 channel_type = nla_get_u32(
4673 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4674 if (channel_type != NL80211_CHAN_NO_HT &&
4675 channel_type != NL80211_CHAN_HT20 &&
4676 channel_type != NL80211_CHAN_HT40PLUS &&
4677 channel_type != NL80211_CHAN_HT40MINUS)
4678 return -EINVAL;
4681 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4682 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4683 if (chan == NULL)
4684 return -EINVAL;
4686 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4687 if (!msg)
4688 return -ENOMEM;
4690 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4691 NL80211_CMD_REMAIN_ON_CHANNEL);
4693 if (IS_ERR(hdr)) {
4694 err = PTR_ERR(hdr);
4695 goto free_msg;
4698 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4699 channel_type, duration, &cookie);
4701 if (err)
4702 goto free_msg;
4704 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4706 genlmsg_end(msg, hdr);
4708 return genlmsg_reply(msg, info);
4710 nla_put_failure:
4711 err = -ENOBUFS;
4712 free_msg:
4713 nlmsg_free(msg);
4714 return err;
4717 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4718 struct genl_info *info)
4720 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4721 struct net_device *dev = info->user_ptr[1];
4722 u64 cookie;
4724 if (!info->attrs[NL80211_ATTR_COOKIE])
4725 return -EINVAL;
4727 if (!rdev->ops->cancel_remain_on_channel)
4728 return -EOPNOTSUPP;
4730 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4732 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4735 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4736 u8 *rates, u8 rates_len)
4738 u8 i;
4739 u32 mask = 0;
4741 for (i = 0; i < rates_len; i++) {
4742 int rate = (rates[i] & 0x7f) * 5;
4743 int ridx;
4744 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4745 struct ieee80211_rate *srate =
4746 &sband->bitrates[ridx];
4747 if (rate == srate->bitrate) {
4748 mask |= 1 << ridx;
4749 break;
4752 if (ridx == sband->n_bitrates)
4753 return 0; /* rate not found */
4756 return mask;
4759 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4760 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4761 .len = NL80211_MAX_SUPP_RATES },
4764 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4765 struct genl_info *info)
4767 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4769 struct cfg80211_bitrate_mask mask;
4770 int rem, i;
4771 struct net_device *dev = info->user_ptr[1];
4772 struct nlattr *tx_rates;
4773 struct ieee80211_supported_band *sband;
4775 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4776 return -EINVAL;
4778 if (!rdev->ops->set_bitrate_mask)
4779 return -EOPNOTSUPP;
4781 memset(&mask, 0, sizeof(mask));
4782 /* Default to all rates enabled */
4783 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4784 sband = rdev->wiphy.bands[i];
4785 mask.control[i].legacy =
4786 sband ? (1 << sband->n_bitrates) - 1 : 0;
4790 * The nested attribute uses enum nl80211_band as the index. This maps
4791 * directly to the enum ieee80211_band values used in cfg80211.
4793 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4795 enum ieee80211_band band = nla_type(tx_rates);
4796 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4797 return -EINVAL;
4798 sband = rdev->wiphy.bands[band];
4799 if (sband == NULL)
4800 return -EINVAL;
4801 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4802 nla_len(tx_rates), nl80211_txattr_policy);
4803 if (tb[NL80211_TXRATE_LEGACY]) {
4804 mask.control[band].legacy = rateset_to_mask(
4805 sband,
4806 nla_data(tb[NL80211_TXRATE_LEGACY]),
4807 nla_len(tb[NL80211_TXRATE_LEGACY]));
4808 if (mask.control[band].legacy == 0)
4809 return -EINVAL;
4813 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4816 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
4818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4819 struct net_device *dev = info->user_ptr[1];
4820 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
4822 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4823 return -EINVAL;
4825 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4826 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
4828 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4829 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4830 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4831 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4832 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4833 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4834 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4835 return -EOPNOTSUPP;
4837 /* not much point in registering if we can't reply */
4838 if (!rdev->ops->mgmt_tx)
4839 return -EOPNOTSUPP;
4841 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
4842 frame_type,
4843 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4844 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4847 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
4849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4850 struct net_device *dev = info->user_ptr[1];
4851 struct ieee80211_channel *chan;
4852 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4853 bool channel_type_valid = false;
4854 u32 freq;
4855 int err;
4856 void *hdr;
4857 u64 cookie;
4858 struct sk_buff *msg;
4859 unsigned int wait = 0;
4860 bool offchan;
4862 if (!info->attrs[NL80211_ATTR_FRAME] ||
4863 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4864 return -EINVAL;
4866 if (!rdev->ops->mgmt_tx)
4867 return -EOPNOTSUPP;
4869 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4870 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4871 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4872 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4873 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4874 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4875 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4876 return -EOPNOTSUPP;
4878 if (info->attrs[NL80211_ATTR_DURATION]) {
4879 if (!rdev->ops->mgmt_tx_cancel_wait)
4880 return -EINVAL;
4881 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4884 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4885 channel_type = nla_get_u32(
4886 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4887 if (channel_type != NL80211_CHAN_NO_HT &&
4888 channel_type != NL80211_CHAN_HT20 &&
4889 channel_type != NL80211_CHAN_HT40PLUS &&
4890 channel_type != NL80211_CHAN_HT40MINUS)
4891 return -EINVAL;
4892 channel_type_valid = true;
4895 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4897 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4898 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4899 if (chan == NULL)
4900 return -EINVAL;
4902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4903 if (!msg)
4904 return -ENOMEM;
4906 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4907 NL80211_CMD_FRAME);
4909 if (IS_ERR(hdr)) {
4910 err = PTR_ERR(hdr);
4911 goto free_msg;
4913 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4914 channel_type_valid, wait,
4915 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4916 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4917 &cookie);
4918 if (err)
4919 goto free_msg;
4921 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4923 genlmsg_end(msg, hdr);
4924 return genlmsg_reply(msg, info);
4926 nla_put_failure:
4927 err = -ENOBUFS;
4928 free_msg:
4929 nlmsg_free(msg);
4930 return err;
4933 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4936 struct net_device *dev = info->user_ptr[1];
4937 u64 cookie;
4939 if (!info->attrs[NL80211_ATTR_COOKIE])
4940 return -EINVAL;
4942 if (!rdev->ops->mgmt_tx_cancel_wait)
4943 return -EOPNOTSUPP;
4945 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4946 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4947 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4948 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4949 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4950 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4951 return -EOPNOTSUPP;
4953 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4955 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4958 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4961 struct wireless_dev *wdev;
4962 struct net_device *dev = info->user_ptr[1];
4963 u8 ps_state;
4964 bool state;
4965 int err;
4967 if (!info->attrs[NL80211_ATTR_PS_STATE])
4968 return -EINVAL;
4970 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4972 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4973 return -EINVAL;
4975 wdev = dev->ieee80211_ptr;
4977 if (!rdev->ops->set_power_mgmt)
4978 return -EOPNOTSUPP;
4980 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4982 if (state == wdev->ps)
4983 return 0;
4985 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4986 wdev->ps_timeout);
4987 if (!err)
4988 wdev->ps = state;
4989 return err;
4992 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4994 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4995 enum nl80211_ps_state ps_state;
4996 struct wireless_dev *wdev;
4997 struct net_device *dev = info->user_ptr[1];
4998 struct sk_buff *msg;
4999 void *hdr;
5000 int err;
5002 wdev = dev->ieee80211_ptr;
5004 if (!rdev->ops->set_power_mgmt)
5005 return -EOPNOTSUPP;
5007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5008 if (!msg)
5009 return -ENOMEM;
5011 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5012 NL80211_CMD_GET_POWER_SAVE);
5013 if (!hdr) {
5014 err = -ENOBUFS;
5015 goto free_msg;
5018 if (wdev->ps)
5019 ps_state = NL80211_PS_ENABLED;
5020 else
5021 ps_state = NL80211_PS_DISABLED;
5023 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5025 genlmsg_end(msg, hdr);
5026 return genlmsg_reply(msg, info);
5028 nla_put_failure:
5029 err = -ENOBUFS;
5030 free_msg:
5031 nlmsg_free(msg);
5032 return err;
5035 static struct nla_policy
5036 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5037 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5038 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5039 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5042 static int nl80211_set_cqm_rssi(struct genl_info *info,
5043 s32 threshold, u32 hysteresis)
5045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5046 struct wireless_dev *wdev;
5047 struct net_device *dev = info->user_ptr[1];
5049 if (threshold > 0)
5050 return -EINVAL;
5052 wdev = dev->ieee80211_ptr;
5054 if (!rdev->ops->set_cqm_rssi_config)
5055 return -EOPNOTSUPP;
5057 if (wdev->iftype != NL80211_IFTYPE_STATION &&
5058 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5059 return -EOPNOTSUPP;
5061 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5062 threshold, hysteresis);
5065 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5067 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5068 struct nlattr *cqm;
5069 int err;
5071 cqm = info->attrs[NL80211_ATTR_CQM];
5072 if (!cqm) {
5073 err = -EINVAL;
5074 goto out;
5077 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5078 nl80211_attr_cqm_policy);
5079 if (err)
5080 goto out;
5082 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5083 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5084 s32 threshold;
5085 u32 hysteresis;
5086 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5087 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5088 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5089 } else
5090 err = -EINVAL;
5092 out:
5093 return err;
5096 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5098 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5099 struct net_device *dev = info->user_ptr[1];
5100 struct mesh_config cfg;
5101 struct mesh_setup setup;
5102 int err;
5104 /* start with default */
5105 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
5106 memcpy(&setup, &default_mesh_setup, sizeof(setup));
5108 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
5109 /* and parse parameters if given */
5110 err = nl80211_parse_mesh_config(info, &cfg, NULL);
5111 if (err)
5112 return err;
5115 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5116 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5117 return -EINVAL;
5119 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5120 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5122 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5123 /* parse additional setup parameters if given */
5124 err = nl80211_parse_mesh_setup(info, &setup);
5125 if (err)
5126 return err;
5129 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
5132 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5135 struct net_device *dev = info->user_ptr[1];
5137 return cfg80211_leave_mesh(rdev, dev);
5140 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5142 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5143 struct sk_buff *msg;
5144 void *hdr;
5146 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5147 return -EOPNOTSUPP;
5149 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5150 if (!msg)
5151 return -ENOMEM;
5153 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5154 NL80211_CMD_GET_WOWLAN);
5155 if (!hdr)
5156 goto nla_put_failure;
5158 if (rdev->wowlan) {
5159 struct nlattr *nl_wowlan;
5161 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5162 if (!nl_wowlan)
5163 goto nla_put_failure;
5165 if (rdev->wowlan->any)
5166 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5167 if (rdev->wowlan->disconnect)
5168 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5169 if (rdev->wowlan->magic_pkt)
5170 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
5171 if (rdev->wowlan->n_patterns) {
5172 struct nlattr *nl_pats, *nl_pat;
5173 int i, pat_len;
5175 nl_pats = nla_nest_start(msg,
5176 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5177 if (!nl_pats)
5178 goto nla_put_failure;
5180 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5181 nl_pat = nla_nest_start(msg, i + 1);
5182 if (!nl_pat)
5183 goto nla_put_failure;
5184 pat_len = rdev->wowlan->patterns[i].pattern_len;
5185 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5186 DIV_ROUND_UP(pat_len, 8),
5187 rdev->wowlan->patterns[i].mask);
5188 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5189 pat_len,
5190 rdev->wowlan->patterns[i].pattern);
5191 nla_nest_end(msg, nl_pat);
5193 nla_nest_end(msg, nl_pats);
5196 nla_nest_end(msg, nl_wowlan);
5199 genlmsg_end(msg, hdr);
5200 return genlmsg_reply(msg, info);
5202 nla_put_failure:
5203 nlmsg_free(msg);
5204 return -ENOBUFS;
5207 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5210 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5211 struct cfg80211_wowlan no_triggers = {};
5212 struct cfg80211_wowlan new_triggers = {};
5213 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5214 int err, i;
5216 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5217 return -EOPNOTSUPP;
5219 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5220 goto no_triggers;
5222 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5223 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5224 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5225 nl80211_wowlan_policy);
5226 if (err)
5227 return err;
5229 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5230 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5231 return -EINVAL;
5232 new_triggers.any = true;
5235 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5236 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5237 return -EINVAL;
5238 new_triggers.disconnect = true;
5241 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5242 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5243 return -EINVAL;
5244 new_triggers.magic_pkt = true;
5247 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5248 struct nlattr *pat;
5249 int n_patterns = 0;
5250 int rem, pat_len, mask_len;
5251 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5253 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5254 rem)
5255 n_patterns++;
5256 if (n_patterns > wowlan->n_patterns)
5257 return -EINVAL;
5259 new_triggers.patterns = kcalloc(n_patterns,
5260 sizeof(new_triggers.patterns[0]),
5261 GFP_KERNEL);
5262 if (!new_triggers.patterns)
5263 return -ENOMEM;
5265 new_triggers.n_patterns = n_patterns;
5266 i = 0;
5268 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5269 rem) {
5270 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5271 nla_data(pat), nla_len(pat), NULL);
5272 err = -EINVAL;
5273 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5274 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5275 goto error;
5276 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5277 mask_len = DIV_ROUND_UP(pat_len, 8);
5278 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5279 mask_len)
5280 goto error;
5281 if (pat_len > wowlan->pattern_max_len ||
5282 pat_len < wowlan->pattern_min_len)
5283 goto error;
5285 new_triggers.patterns[i].mask =
5286 kmalloc(mask_len + pat_len, GFP_KERNEL);
5287 if (!new_triggers.patterns[i].mask) {
5288 err = -ENOMEM;
5289 goto error;
5291 new_triggers.patterns[i].pattern =
5292 new_triggers.patterns[i].mask + mask_len;
5293 memcpy(new_triggers.patterns[i].mask,
5294 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5295 mask_len);
5296 new_triggers.patterns[i].pattern_len = pat_len;
5297 memcpy(new_triggers.patterns[i].pattern,
5298 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5299 pat_len);
5300 i++;
5304 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5305 struct cfg80211_wowlan *ntrig;
5306 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5307 GFP_KERNEL);
5308 if (!ntrig) {
5309 err = -ENOMEM;
5310 goto error;
5312 cfg80211_rdev_free_wowlan(rdev);
5313 rdev->wowlan = ntrig;
5314 } else {
5315 no_triggers:
5316 cfg80211_rdev_free_wowlan(rdev);
5317 rdev->wowlan = NULL;
5320 return 0;
5321 error:
5322 for (i = 0; i < new_triggers.n_patterns; i++)
5323 kfree(new_triggers.patterns[i].mask);
5324 kfree(new_triggers.patterns);
5325 return err;
5328 #define NL80211_FLAG_NEED_WIPHY 0x01
5329 #define NL80211_FLAG_NEED_NETDEV 0x02
5330 #define NL80211_FLAG_NEED_RTNL 0x04
5331 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5332 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5333 NL80211_FLAG_CHECK_NETDEV_UP)
5335 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5336 struct genl_info *info)
5338 struct cfg80211_registered_device *rdev;
5339 struct net_device *dev;
5340 int err;
5341 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5343 if (rtnl)
5344 rtnl_lock();
5346 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5347 rdev = cfg80211_get_dev_from_info(info);
5348 if (IS_ERR(rdev)) {
5349 if (rtnl)
5350 rtnl_unlock();
5351 return PTR_ERR(rdev);
5353 info->user_ptr[0] = rdev;
5354 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5355 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5356 if (err) {
5357 if (rtnl)
5358 rtnl_unlock();
5359 return err;
5361 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5362 !netif_running(dev)) {
5363 cfg80211_unlock_rdev(rdev);
5364 dev_put(dev);
5365 if (rtnl)
5366 rtnl_unlock();
5367 return -ENETDOWN;
5369 info->user_ptr[0] = rdev;
5370 info->user_ptr[1] = dev;
5373 return 0;
5376 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5377 struct genl_info *info)
5379 if (info->user_ptr[0])
5380 cfg80211_unlock_rdev(info->user_ptr[0]);
5381 if (info->user_ptr[1])
5382 dev_put(info->user_ptr[1]);
5383 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5384 rtnl_unlock();
5387 static struct genl_ops nl80211_ops[] = {
5389 .cmd = NL80211_CMD_GET_WIPHY,
5390 .doit = nl80211_get_wiphy,
5391 .dumpit = nl80211_dump_wiphy,
5392 .policy = nl80211_policy,
5393 /* can be retrieved by unprivileged users */
5394 .internal_flags = NL80211_FLAG_NEED_WIPHY,
5397 .cmd = NL80211_CMD_SET_WIPHY,
5398 .doit = nl80211_set_wiphy,
5399 .policy = nl80211_policy,
5400 .flags = GENL_ADMIN_PERM,
5401 .internal_flags = NL80211_FLAG_NEED_RTNL,
5404 .cmd = NL80211_CMD_GET_INTERFACE,
5405 .doit = nl80211_get_interface,
5406 .dumpit = nl80211_dump_interface,
5407 .policy = nl80211_policy,
5408 /* can be retrieved by unprivileged users */
5409 .internal_flags = NL80211_FLAG_NEED_NETDEV,
5412 .cmd = NL80211_CMD_SET_INTERFACE,
5413 .doit = nl80211_set_interface,
5414 .policy = nl80211_policy,
5415 .flags = GENL_ADMIN_PERM,
5416 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5417 NL80211_FLAG_NEED_RTNL,
5420 .cmd = NL80211_CMD_NEW_INTERFACE,
5421 .doit = nl80211_new_interface,
5422 .policy = nl80211_policy,
5423 .flags = GENL_ADMIN_PERM,
5424 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5425 NL80211_FLAG_NEED_RTNL,
5428 .cmd = NL80211_CMD_DEL_INTERFACE,
5429 .doit = nl80211_del_interface,
5430 .policy = nl80211_policy,
5431 .flags = GENL_ADMIN_PERM,
5432 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5433 NL80211_FLAG_NEED_RTNL,
5436 .cmd = NL80211_CMD_GET_KEY,
5437 .doit = nl80211_get_key,
5438 .policy = nl80211_policy,
5439 .flags = GENL_ADMIN_PERM,
5440 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5441 NL80211_FLAG_NEED_RTNL,
5444 .cmd = NL80211_CMD_SET_KEY,
5445 .doit = nl80211_set_key,
5446 .policy = nl80211_policy,
5447 .flags = GENL_ADMIN_PERM,
5448 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5449 NL80211_FLAG_NEED_RTNL,
5452 .cmd = NL80211_CMD_NEW_KEY,
5453 .doit = nl80211_new_key,
5454 .policy = nl80211_policy,
5455 .flags = GENL_ADMIN_PERM,
5456 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5457 NL80211_FLAG_NEED_RTNL,
5460 .cmd = NL80211_CMD_DEL_KEY,
5461 .doit = nl80211_del_key,
5462 .policy = nl80211_policy,
5463 .flags = GENL_ADMIN_PERM,
5464 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5465 NL80211_FLAG_NEED_RTNL,
5468 .cmd = NL80211_CMD_SET_BEACON,
5469 .policy = nl80211_policy,
5470 .flags = GENL_ADMIN_PERM,
5471 .doit = nl80211_addset_beacon,
5472 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5473 NL80211_FLAG_NEED_RTNL,
5476 .cmd = NL80211_CMD_NEW_BEACON,
5477 .policy = nl80211_policy,
5478 .flags = GENL_ADMIN_PERM,
5479 .doit = nl80211_addset_beacon,
5480 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5481 NL80211_FLAG_NEED_RTNL,
5484 .cmd = NL80211_CMD_DEL_BEACON,
5485 .policy = nl80211_policy,
5486 .flags = GENL_ADMIN_PERM,
5487 .doit = nl80211_del_beacon,
5488 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5489 NL80211_FLAG_NEED_RTNL,
5492 .cmd = NL80211_CMD_GET_STATION,
5493 .doit = nl80211_get_station,
5494 .dumpit = nl80211_dump_station,
5495 .policy = nl80211_policy,
5496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5497 NL80211_FLAG_NEED_RTNL,
5500 .cmd = NL80211_CMD_SET_STATION,
5501 .doit = nl80211_set_station,
5502 .policy = nl80211_policy,
5503 .flags = GENL_ADMIN_PERM,
5504 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5505 NL80211_FLAG_NEED_RTNL,
5508 .cmd = NL80211_CMD_NEW_STATION,
5509 .doit = nl80211_new_station,
5510 .policy = nl80211_policy,
5511 .flags = GENL_ADMIN_PERM,
5512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5513 NL80211_FLAG_NEED_RTNL,
5516 .cmd = NL80211_CMD_DEL_STATION,
5517 .doit = nl80211_del_station,
5518 .policy = nl80211_policy,
5519 .flags = GENL_ADMIN_PERM,
5520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5521 NL80211_FLAG_NEED_RTNL,
5524 .cmd = NL80211_CMD_GET_MPATH,
5525 .doit = nl80211_get_mpath,
5526 .dumpit = nl80211_dump_mpath,
5527 .policy = nl80211_policy,
5528 .flags = GENL_ADMIN_PERM,
5529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5530 NL80211_FLAG_NEED_RTNL,
5533 .cmd = NL80211_CMD_SET_MPATH,
5534 .doit = nl80211_set_mpath,
5535 .policy = nl80211_policy,
5536 .flags = GENL_ADMIN_PERM,
5537 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5538 NL80211_FLAG_NEED_RTNL,
5541 .cmd = NL80211_CMD_NEW_MPATH,
5542 .doit = nl80211_new_mpath,
5543 .policy = nl80211_policy,
5544 .flags = GENL_ADMIN_PERM,
5545 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5546 NL80211_FLAG_NEED_RTNL,
5549 .cmd = NL80211_CMD_DEL_MPATH,
5550 .doit = nl80211_del_mpath,
5551 .policy = nl80211_policy,
5552 .flags = GENL_ADMIN_PERM,
5553 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5554 NL80211_FLAG_NEED_RTNL,
5557 .cmd = NL80211_CMD_SET_BSS,
5558 .doit = nl80211_set_bss,
5559 .policy = nl80211_policy,
5560 .flags = GENL_ADMIN_PERM,
5561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5562 NL80211_FLAG_NEED_RTNL,
5565 .cmd = NL80211_CMD_GET_REG,
5566 .doit = nl80211_get_reg,
5567 .policy = nl80211_policy,
5568 /* can be retrieved by unprivileged users */
5571 .cmd = NL80211_CMD_SET_REG,
5572 .doit = nl80211_set_reg,
5573 .policy = nl80211_policy,
5574 .flags = GENL_ADMIN_PERM,
5577 .cmd = NL80211_CMD_REQ_SET_REG,
5578 .doit = nl80211_req_set_reg,
5579 .policy = nl80211_policy,
5580 .flags = GENL_ADMIN_PERM,
5583 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5584 .doit = nl80211_get_mesh_config,
5585 .policy = nl80211_policy,
5586 /* can be retrieved by unprivileged users */
5587 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5588 NL80211_FLAG_NEED_RTNL,
5591 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5592 .doit = nl80211_update_mesh_config,
5593 .policy = nl80211_policy,
5594 .flags = GENL_ADMIN_PERM,
5595 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5596 NL80211_FLAG_NEED_RTNL,
5599 .cmd = NL80211_CMD_TRIGGER_SCAN,
5600 .doit = nl80211_trigger_scan,
5601 .policy = nl80211_policy,
5602 .flags = GENL_ADMIN_PERM,
5603 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5604 NL80211_FLAG_NEED_RTNL,
5607 .cmd = NL80211_CMD_GET_SCAN,
5608 .policy = nl80211_policy,
5609 .dumpit = nl80211_dump_scan,
5612 .cmd = NL80211_CMD_START_SCHED_SCAN,
5613 .doit = nl80211_start_sched_scan,
5614 .policy = nl80211_policy,
5615 .flags = GENL_ADMIN_PERM,
5616 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5617 NL80211_FLAG_NEED_RTNL,
5620 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
5621 .doit = nl80211_stop_sched_scan,
5622 .policy = nl80211_policy,
5623 .flags = GENL_ADMIN_PERM,
5624 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5625 NL80211_FLAG_NEED_RTNL,
5628 .cmd = NL80211_CMD_AUTHENTICATE,
5629 .doit = nl80211_authenticate,
5630 .policy = nl80211_policy,
5631 .flags = GENL_ADMIN_PERM,
5632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5633 NL80211_FLAG_NEED_RTNL,
5636 .cmd = NL80211_CMD_ASSOCIATE,
5637 .doit = nl80211_associate,
5638 .policy = nl80211_policy,
5639 .flags = GENL_ADMIN_PERM,
5640 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5641 NL80211_FLAG_NEED_RTNL,
5644 .cmd = NL80211_CMD_DEAUTHENTICATE,
5645 .doit = nl80211_deauthenticate,
5646 .policy = nl80211_policy,
5647 .flags = GENL_ADMIN_PERM,
5648 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5649 NL80211_FLAG_NEED_RTNL,
5652 .cmd = NL80211_CMD_DISASSOCIATE,
5653 .doit = nl80211_disassociate,
5654 .policy = nl80211_policy,
5655 .flags = GENL_ADMIN_PERM,
5656 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5657 NL80211_FLAG_NEED_RTNL,
5660 .cmd = NL80211_CMD_JOIN_IBSS,
5661 .doit = nl80211_join_ibss,
5662 .policy = nl80211_policy,
5663 .flags = GENL_ADMIN_PERM,
5664 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5665 NL80211_FLAG_NEED_RTNL,
5668 .cmd = NL80211_CMD_LEAVE_IBSS,
5669 .doit = nl80211_leave_ibss,
5670 .policy = nl80211_policy,
5671 .flags = GENL_ADMIN_PERM,
5672 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5673 NL80211_FLAG_NEED_RTNL,
5675 #ifdef CONFIG_NL80211_TESTMODE
5677 .cmd = NL80211_CMD_TESTMODE,
5678 .doit = nl80211_testmode_do,
5679 .policy = nl80211_policy,
5680 .flags = GENL_ADMIN_PERM,
5681 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5682 NL80211_FLAG_NEED_RTNL,
5684 #endif
5686 .cmd = NL80211_CMD_CONNECT,
5687 .doit = nl80211_connect,
5688 .policy = nl80211_policy,
5689 .flags = GENL_ADMIN_PERM,
5690 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5691 NL80211_FLAG_NEED_RTNL,
5694 .cmd = NL80211_CMD_DISCONNECT,
5695 .doit = nl80211_disconnect,
5696 .policy = nl80211_policy,
5697 .flags = GENL_ADMIN_PERM,
5698 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5699 NL80211_FLAG_NEED_RTNL,
5702 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5703 .doit = nl80211_wiphy_netns,
5704 .policy = nl80211_policy,
5705 .flags = GENL_ADMIN_PERM,
5706 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5707 NL80211_FLAG_NEED_RTNL,
5710 .cmd = NL80211_CMD_GET_SURVEY,
5711 .policy = nl80211_policy,
5712 .dumpit = nl80211_dump_survey,
5715 .cmd = NL80211_CMD_SET_PMKSA,
5716 .doit = nl80211_setdel_pmksa,
5717 .policy = nl80211_policy,
5718 .flags = GENL_ADMIN_PERM,
5719 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5720 NL80211_FLAG_NEED_RTNL,
5723 .cmd = NL80211_CMD_DEL_PMKSA,
5724 .doit = nl80211_setdel_pmksa,
5725 .policy = nl80211_policy,
5726 .flags = GENL_ADMIN_PERM,
5727 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5728 NL80211_FLAG_NEED_RTNL,
5731 .cmd = NL80211_CMD_FLUSH_PMKSA,
5732 .doit = nl80211_flush_pmksa,
5733 .policy = nl80211_policy,
5734 .flags = GENL_ADMIN_PERM,
5735 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5736 NL80211_FLAG_NEED_RTNL,
5739 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5740 .doit = nl80211_remain_on_channel,
5741 .policy = nl80211_policy,
5742 .flags = GENL_ADMIN_PERM,
5743 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5744 NL80211_FLAG_NEED_RTNL,
5747 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5748 .doit = nl80211_cancel_remain_on_channel,
5749 .policy = nl80211_policy,
5750 .flags = GENL_ADMIN_PERM,
5751 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5752 NL80211_FLAG_NEED_RTNL,
5755 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5756 .doit = nl80211_set_tx_bitrate_mask,
5757 .policy = nl80211_policy,
5758 .flags = GENL_ADMIN_PERM,
5759 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5760 NL80211_FLAG_NEED_RTNL,
5763 .cmd = NL80211_CMD_REGISTER_FRAME,
5764 .doit = nl80211_register_mgmt,
5765 .policy = nl80211_policy,
5766 .flags = GENL_ADMIN_PERM,
5767 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5768 NL80211_FLAG_NEED_RTNL,
5771 .cmd = NL80211_CMD_FRAME,
5772 .doit = nl80211_tx_mgmt,
5773 .policy = nl80211_policy,
5774 .flags = GENL_ADMIN_PERM,
5775 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5776 NL80211_FLAG_NEED_RTNL,
5779 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5780 .doit = nl80211_tx_mgmt_cancel_wait,
5781 .policy = nl80211_policy,
5782 .flags = GENL_ADMIN_PERM,
5783 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5784 NL80211_FLAG_NEED_RTNL,
5787 .cmd = NL80211_CMD_SET_POWER_SAVE,
5788 .doit = nl80211_set_power_save,
5789 .policy = nl80211_policy,
5790 .flags = GENL_ADMIN_PERM,
5791 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5792 NL80211_FLAG_NEED_RTNL,
5795 .cmd = NL80211_CMD_GET_POWER_SAVE,
5796 .doit = nl80211_get_power_save,
5797 .policy = nl80211_policy,
5798 /* can be retrieved by unprivileged users */
5799 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5800 NL80211_FLAG_NEED_RTNL,
5803 .cmd = NL80211_CMD_SET_CQM,
5804 .doit = nl80211_set_cqm,
5805 .policy = nl80211_policy,
5806 .flags = GENL_ADMIN_PERM,
5807 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5808 NL80211_FLAG_NEED_RTNL,
5811 .cmd = NL80211_CMD_SET_CHANNEL,
5812 .doit = nl80211_set_channel,
5813 .policy = nl80211_policy,
5814 .flags = GENL_ADMIN_PERM,
5815 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5816 NL80211_FLAG_NEED_RTNL,
5819 .cmd = NL80211_CMD_SET_WDS_PEER,
5820 .doit = nl80211_set_wds_peer,
5821 .policy = nl80211_policy,
5822 .flags = GENL_ADMIN_PERM,
5823 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5824 NL80211_FLAG_NEED_RTNL,
5827 .cmd = NL80211_CMD_JOIN_MESH,
5828 .doit = nl80211_join_mesh,
5829 .policy = nl80211_policy,
5830 .flags = GENL_ADMIN_PERM,
5831 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5832 NL80211_FLAG_NEED_RTNL,
5835 .cmd = NL80211_CMD_LEAVE_MESH,
5836 .doit = nl80211_leave_mesh,
5837 .policy = nl80211_policy,
5838 .flags = GENL_ADMIN_PERM,
5839 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5840 NL80211_FLAG_NEED_RTNL,
5843 .cmd = NL80211_CMD_GET_WOWLAN,
5844 .doit = nl80211_get_wowlan,
5845 .policy = nl80211_policy,
5846 /* can be retrieved by unprivileged users */
5847 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5848 NL80211_FLAG_NEED_RTNL,
5851 .cmd = NL80211_CMD_SET_WOWLAN,
5852 .doit = nl80211_set_wowlan,
5853 .policy = nl80211_policy,
5854 .flags = GENL_ADMIN_PERM,
5855 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5856 NL80211_FLAG_NEED_RTNL,
5860 static struct genl_multicast_group nl80211_mlme_mcgrp = {
5861 .name = "mlme",
5864 /* multicast groups */
5865 static struct genl_multicast_group nl80211_config_mcgrp = {
5866 .name = "config",
5868 static struct genl_multicast_group nl80211_scan_mcgrp = {
5869 .name = "scan",
5871 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5872 .name = "regulatory",
5875 /* notification functions */
5877 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5879 struct sk_buff *msg;
5881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5882 if (!msg)
5883 return;
5885 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5886 nlmsg_free(msg);
5887 return;
5890 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5891 nl80211_config_mcgrp.id, GFP_KERNEL);
5894 static int nl80211_add_scan_req(struct sk_buff *msg,
5895 struct cfg80211_registered_device *rdev)
5897 struct cfg80211_scan_request *req = rdev->scan_req;
5898 struct nlattr *nest;
5899 int i;
5901 ASSERT_RDEV_LOCK(rdev);
5903 if (WARN_ON(!req))
5904 return 0;
5906 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5907 if (!nest)
5908 goto nla_put_failure;
5909 for (i = 0; i < req->n_ssids; i++)
5910 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5911 nla_nest_end(msg, nest);
5913 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5914 if (!nest)
5915 goto nla_put_failure;
5916 for (i = 0; i < req->n_channels; i++)
5917 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5918 nla_nest_end(msg, nest);
5920 if (req->ie)
5921 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5923 return 0;
5924 nla_put_failure:
5925 return -ENOBUFS;
5928 static int nl80211_send_scan_msg(struct sk_buff *msg,
5929 struct cfg80211_registered_device *rdev,
5930 struct net_device *netdev,
5931 u32 pid, u32 seq, int flags,
5932 u32 cmd)
5934 void *hdr;
5936 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5937 if (!hdr)
5938 return -1;
5940 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5941 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5943 /* ignore errors and send incomplete event anyway */
5944 nl80211_add_scan_req(msg, rdev);
5946 return genlmsg_end(msg, hdr);
5948 nla_put_failure:
5949 genlmsg_cancel(msg, hdr);
5950 return -EMSGSIZE;
5953 static int
5954 nl80211_send_sched_scan_msg(struct sk_buff *msg,
5955 struct cfg80211_registered_device *rdev,
5956 struct net_device *netdev,
5957 u32 pid, u32 seq, int flags, u32 cmd)
5959 void *hdr;
5961 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5962 if (!hdr)
5963 return -1;
5965 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5966 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5968 return genlmsg_end(msg, hdr);
5970 nla_put_failure:
5971 genlmsg_cancel(msg, hdr);
5972 return -EMSGSIZE;
5975 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5976 struct net_device *netdev)
5978 struct sk_buff *msg;
5980 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5981 if (!msg)
5982 return;
5984 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5985 NL80211_CMD_TRIGGER_SCAN) < 0) {
5986 nlmsg_free(msg);
5987 return;
5990 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5991 nl80211_scan_mcgrp.id, GFP_KERNEL);
5994 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5995 struct net_device *netdev)
5997 struct sk_buff *msg;
5999 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6000 if (!msg)
6001 return;
6003 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6004 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
6005 nlmsg_free(msg);
6006 return;
6009 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6010 nl80211_scan_mcgrp.id, GFP_KERNEL);
6013 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6014 struct net_device *netdev)
6016 struct sk_buff *msg;
6018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6019 if (!msg)
6020 return;
6022 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6023 NL80211_CMD_SCAN_ABORTED) < 0) {
6024 nlmsg_free(msg);
6025 return;
6028 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6029 nl80211_scan_mcgrp.id, GFP_KERNEL);
6032 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6033 struct net_device *netdev)
6035 struct sk_buff *msg;
6037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6038 if (!msg)
6039 return;
6041 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6042 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6043 nlmsg_free(msg);
6044 return;
6047 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6048 nl80211_scan_mcgrp.id, GFP_KERNEL);
6051 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6052 struct net_device *netdev, u32 cmd)
6054 struct sk_buff *msg;
6056 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6057 if (!msg)
6058 return;
6060 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6061 nlmsg_free(msg);
6062 return;
6065 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6066 nl80211_scan_mcgrp.id, GFP_KERNEL);
6070 * This can happen on global regulatory changes or device specific settings
6071 * based on custom world regulatory domains.
6073 void nl80211_send_reg_change_event(struct regulatory_request *request)
6075 struct sk_buff *msg;
6076 void *hdr;
6078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6079 if (!msg)
6080 return;
6082 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6083 if (!hdr) {
6084 nlmsg_free(msg);
6085 return;
6088 /* Userspace can always count this one always being set */
6089 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6091 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6092 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6093 NL80211_REGDOM_TYPE_WORLD);
6094 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6095 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6096 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6097 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6098 request->intersect)
6099 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6100 NL80211_REGDOM_TYPE_INTERSECTION);
6101 else {
6102 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6103 NL80211_REGDOM_TYPE_COUNTRY);
6104 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6107 if (wiphy_idx_valid(request->wiphy_idx))
6108 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6110 if (genlmsg_end(msg, hdr) < 0) {
6111 nlmsg_free(msg);
6112 return;
6115 rcu_read_lock();
6116 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6117 GFP_ATOMIC);
6118 rcu_read_unlock();
6120 return;
6122 nla_put_failure:
6123 genlmsg_cancel(msg, hdr);
6124 nlmsg_free(msg);
6127 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6128 struct net_device *netdev,
6129 const u8 *buf, size_t len,
6130 enum nl80211_commands cmd, gfp_t gfp)
6132 struct sk_buff *msg;
6133 void *hdr;
6135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6136 if (!msg)
6137 return;
6139 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6140 if (!hdr) {
6141 nlmsg_free(msg);
6142 return;
6145 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6146 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6147 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6149 if (genlmsg_end(msg, hdr) < 0) {
6150 nlmsg_free(msg);
6151 return;
6154 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6155 nl80211_mlme_mcgrp.id, gfp);
6156 return;
6158 nla_put_failure:
6159 genlmsg_cancel(msg, hdr);
6160 nlmsg_free(msg);
6163 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
6164 struct net_device *netdev, const u8 *buf,
6165 size_t len, gfp_t gfp)
6167 nl80211_send_mlme_event(rdev, netdev, buf, len,
6168 NL80211_CMD_AUTHENTICATE, gfp);
6171 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6172 struct net_device *netdev, const u8 *buf,
6173 size_t len, gfp_t gfp)
6175 nl80211_send_mlme_event(rdev, netdev, buf, len,
6176 NL80211_CMD_ASSOCIATE, gfp);
6179 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
6180 struct net_device *netdev, const u8 *buf,
6181 size_t len, gfp_t gfp)
6183 nl80211_send_mlme_event(rdev, netdev, buf, len,
6184 NL80211_CMD_DEAUTHENTICATE, gfp);
6187 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6188 struct net_device *netdev, const u8 *buf,
6189 size_t len, gfp_t gfp)
6191 nl80211_send_mlme_event(rdev, netdev, buf, len,
6192 NL80211_CMD_DISASSOCIATE, gfp);
6195 void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6196 struct net_device *netdev, const u8 *buf,
6197 size_t len, gfp_t gfp)
6199 nl80211_send_mlme_event(rdev, netdev, buf, len,
6200 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6203 void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6204 struct net_device *netdev, const u8 *buf,
6205 size_t len, gfp_t gfp)
6207 nl80211_send_mlme_event(rdev, netdev, buf, len,
6208 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6211 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6212 struct net_device *netdev, int cmd,
6213 const u8 *addr, gfp_t gfp)
6215 struct sk_buff *msg;
6216 void *hdr;
6218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6219 if (!msg)
6220 return;
6222 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6223 if (!hdr) {
6224 nlmsg_free(msg);
6225 return;
6228 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6229 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6230 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6231 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6233 if (genlmsg_end(msg, hdr) < 0) {
6234 nlmsg_free(msg);
6235 return;
6238 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6239 nl80211_mlme_mcgrp.id, gfp);
6240 return;
6242 nla_put_failure:
6243 genlmsg_cancel(msg, hdr);
6244 nlmsg_free(msg);
6247 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
6248 struct net_device *netdev, const u8 *addr,
6249 gfp_t gfp)
6251 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
6252 addr, gfp);
6255 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
6256 struct net_device *netdev, const u8 *addr,
6257 gfp_t gfp)
6259 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6260 addr, gfp);
6263 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6264 struct net_device *netdev, const u8 *bssid,
6265 const u8 *req_ie, size_t req_ie_len,
6266 const u8 *resp_ie, size_t resp_ie_len,
6267 u16 status, gfp_t gfp)
6269 struct sk_buff *msg;
6270 void *hdr;
6272 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6273 if (!msg)
6274 return;
6276 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6277 if (!hdr) {
6278 nlmsg_free(msg);
6279 return;
6282 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6283 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6284 if (bssid)
6285 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6286 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6287 if (req_ie)
6288 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6289 if (resp_ie)
6290 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6292 if (genlmsg_end(msg, hdr) < 0) {
6293 nlmsg_free(msg);
6294 return;
6297 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6298 nl80211_mlme_mcgrp.id, gfp);
6299 return;
6301 nla_put_failure:
6302 genlmsg_cancel(msg, hdr);
6303 nlmsg_free(msg);
6307 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6308 struct net_device *netdev, const u8 *bssid,
6309 const u8 *req_ie, size_t req_ie_len,
6310 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6312 struct sk_buff *msg;
6313 void *hdr;
6315 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6316 if (!msg)
6317 return;
6319 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6320 if (!hdr) {
6321 nlmsg_free(msg);
6322 return;
6325 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6326 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6327 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6328 if (req_ie)
6329 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6330 if (resp_ie)
6331 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6333 if (genlmsg_end(msg, hdr) < 0) {
6334 nlmsg_free(msg);
6335 return;
6338 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6339 nl80211_mlme_mcgrp.id, gfp);
6340 return;
6342 nla_put_failure:
6343 genlmsg_cancel(msg, hdr);
6344 nlmsg_free(msg);
6348 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6349 struct net_device *netdev, u16 reason,
6350 const u8 *ie, size_t ie_len, bool from_ap)
6352 struct sk_buff *msg;
6353 void *hdr;
6355 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6356 if (!msg)
6357 return;
6359 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6360 if (!hdr) {
6361 nlmsg_free(msg);
6362 return;
6365 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6366 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6367 if (from_ap && reason)
6368 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6369 if (from_ap)
6370 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6371 if (ie)
6372 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6374 if (genlmsg_end(msg, hdr) < 0) {
6375 nlmsg_free(msg);
6376 return;
6379 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6380 nl80211_mlme_mcgrp.id, GFP_KERNEL);
6381 return;
6383 nla_put_failure:
6384 genlmsg_cancel(msg, hdr);
6385 nlmsg_free(msg);
6389 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6390 struct net_device *netdev, const u8 *bssid,
6391 gfp_t gfp)
6393 struct sk_buff *msg;
6394 void *hdr;
6396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6397 if (!msg)
6398 return;
6400 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6401 if (!hdr) {
6402 nlmsg_free(msg);
6403 return;
6406 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6407 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6408 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6410 if (genlmsg_end(msg, hdr) < 0) {
6411 nlmsg_free(msg);
6412 return;
6415 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6416 nl80211_mlme_mcgrp.id, gfp);
6417 return;
6419 nla_put_failure:
6420 genlmsg_cancel(msg, hdr);
6421 nlmsg_free(msg);
6424 void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6425 struct net_device *netdev,
6426 const u8 *macaddr, const u8* ie, u8 ie_len,
6427 gfp_t gfp)
6429 struct sk_buff *msg;
6430 void *hdr;
6432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6433 if (!msg)
6434 return;
6436 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6437 if (!hdr) {
6438 nlmsg_free(msg);
6439 return;
6442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6444 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6445 if (ie_len && ie)
6446 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6448 if (genlmsg_end(msg, hdr) < 0) {
6449 nlmsg_free(msg);
6450 return;
6453 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6454 nl80211_mlme_mcgrp.id, gfp);
6455 return;
6457 nla_put_failure:
6458 genlmsg_cancel(msg, hdr);
6459 nlmsg_free(msg);
6462 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6463 struct net_device *netdev, const u8 *addr,
6464 enum nl80211_key_type key_type, int key_id,
6465 const u8 *tsc, gfp_t gfp)
6467 struct sk_buff *msg;
6468 void *hdr;
6470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6471 if (!msg)
6472 return;
6474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6475 if (!hdr) {
6476 nlmsg_free(msg);
6477 return;
6480 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6482 if (addr)
6483 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6484 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
6485 if (key_id != -1)
6486 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
6487 if (tsc)
6488 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6490 if (genlmsg_end(msg, hdr) < 0) {
6491 nlmsg_free(msg);
6492 return;
6495 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6496 nl80211_mlme_mcgrp.id, gfp);
6497 return;
6499 nla_put_failure:
6500 genlmsg_cancel(msg, hdr);
6501 nlmsg_free(msg);
6504 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6505 struct ieee80211_channel *channel_before,
6506 struct ieee80211_channel *channel_after)
6508 struct sk_buff *msg;
6509 void *hdr;
6510 struct nlattr *nl_freq;
6512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
6513 if (!msg)
6514 return;
6516 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6517 if (!hdr) {
6518 nlmsg_free(msg);
6519 return;
6523 * Since we are applying the beacon hint to a wiphy we know its
6524 * wiphy_idx is valid
6526 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6528 /* Before */
6529 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6530 if (!nl_freq)
6531 goto nla_put_failure;
6532 if (nl80211_msg_put_channel(msg, channel_before))
6533 goto nla_put_failure;
6534 nla_nest_end(msg, nl_freq);
6536 /* After */
6537 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6538 if (!nl_freq)
6539 goto nla_put_failure;
6540 if (nl80211_msg_put_channel(msg, channel_after))
6541 goto nla_put_failure;
6542 nla_nest_end(msg, nl_freq);
6544 if (genlmsg_end(msg, hdr) < 0) {
6545 nlmsg_free(msg);
6546 return;
6549 rcu_read_lock();
6550 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6551 GFP_ATOMIC);
6552 rcu_read_unlock();
6554 return;
6556 nla_put_failure:
6557 genlmsg_cancel(msg, hdr);
6558 nlmsg_free(msg);
6561 static void nl80211_send_remain_on_chan_event(
6562 int cmd, struct cfg80211_registered_device *rdev,
6563 struct net_device *netdev, u64 cookie,
6564 struct ieee80211_channel *chan,
6565 enum nl80211_channel_type channel_type,
6566 unsigned int duration, gfp_t gfp)
6568 struct sk_buff *msg;
6569 void *hdr;
6571 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6572 if (!msg)
6573 return;
6575 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6576 if (!hdr) {
6577 nlmsg_free(msg);
6578 return;
6581 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6582 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6583 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6584 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6585 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6587 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6588 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6590 if (genlmsg_end(msg, hdr) < 0) {
6591 nlmsg_free(msg);
6592 return;
6595 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6596 nl80211_mlme_mcgrp.id, gfp);
6597 return;
6599 nla_put_failure:
6600 genlmsg_cancel(msg, hdr);
6601 nlmsg_free(msg);
6604 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6605 struct net_device *netdev, u64 cookie,
6606 struct ieee80211_channel *chan,
6607 enum nl80211_channel_type channel_type,
6608 unsigned int duration, gfp_t gfp)
6610 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6611 rdev, netdev, cookie, chan,
6612 channel_type, duration, gfp);
6615 void nl80211_send_remain_on_channel_cancel(
6616 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6617 u64 cookie, struct ieee80211_channel *chan,
6618 enum nl80211_channel_type channel_type, gfp_t gfp)
6620 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6621 rdev, netdev, cookie, chan,
6622 channel_type, 0, gfp);
6625 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6626 struct net_device *dev, const u8 *mac_addr,
6627 struct station_info *sinfo, gfp_t gfp)
6629 struct sk_buff *msg;
6631 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6632 if (!msg)
6633 return;
6635 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6636 nlmsg_free(msg);
6637 return;
6640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6641 nl80211_mlme_mcgrp.id, gfp);
6644 void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
6645 struct net_device *dev, const u8 *mac_addr,
6646 gfp_t gfp)
6648 struct sk_buff *msg;
6649 void *hdr;
6651 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6652 if (!msg)
6653 return;
6655 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6656 if (!hdr) {
6657 nlmsg_free(msg);
6658 return;
6661 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6662 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6664 if (genlmsg_end(msg, hdr) < 0) {
6665 nlmsg_free(msg);
6666 return;
6669 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6670 nl80211_mlme_mcgrp.id, gfp);
6671 return;
6673 nla_put_failure:
6674 genlmsg_cancel(msg, hdr);
6675 nlmsg_free(msg);
6678 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6679 struct net_device *netdev, u32 nlpid,
6680 int freq, const u8 *buf, size_t len, gfp_t gfp)
6682 struct sk_buff *msg;
6683 void *hdr;
6684 int err;
6686 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6687 if (!msg)
6688 return -ENOMEM;
6690 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
6691 if (!hdr) {
6692 nlmsg_free(msg);
6693 return -ENOMEM;
6696 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6697 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6698 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6699 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6701 err = genlmsg_end(msg, hdr);
6702 if (err < 0) {
6703 nlmsg_free(msg);
6704 return err;
6707 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6708 if (err < 0)
6709 return err;
6710 return 0;
6712 nla_put_failure:
6713 genlmsg_cancel(msg, hdr);
6714 nlmsg_free(msg);
6715 return -ENOBUFS;
6718 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6719 struct net_device *netdev, u64 cookie,
6720 const u8 *buf, size_t len, bool ack,
6721 gfp_t gfp)
6723 struct sk_buff *msg;
6724 void *hdr;
6726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6727 if (!msg)
6728 return;
6730 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
6731 if (!hdr) {
6732 nlmsg_free(msg);
6733 return;
6736 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6737 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6738 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6739 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6740 if (ack)
6741 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6743 if (genlmsg_end(msg, hdr) < 0) {
6744 nlmsg_free(msg);
6745 return;
6748 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6749 return;
6751 nla_put_failure:
6752 genlmsg_cancel(msg, hdr);
6753 nlmsg_free(msg);
6756 void
6757 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6758 struct net_device *netdev,
6759 enum nl80211_cqm_rssi_threshold_event rssi_event,
6760 gfp_t gfp)
6762 struct sk_buff *msg;
6763 struct nlattr *pinfoattr;
6764 void *hdr;
6766 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6767 if (!msg)
6768 return;
6770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6771 if (!hdr) {
6772 nlmsg_free(msg);
6773 return;
6776 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6777 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6779 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6780 if (!pinfoattr)
6781 goto nla_put_failure;
6783 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6784 rssi_event);
6786 nla_nest_end(msg, pinfoattr);
6788 if (genlmsg_end(msg, hdr) < 0) {
6789 nlmsg_free(msg);
6790 return;
6793 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6794 nl80211_mlme_mcgrp.id, gfp);
6795 return;
6797 nla_put_failure:
6798 genlmsg_cancel(msg, hdr);
6799 nlmsg_free(msg);
6802 void
6803 nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6804 struct net_device *netdev, const u8 *peer,
6805 u32 num_packets, gfp_t gfp)
6807 struct sk_buff *msg;
6808 struct nlattr *pinfoattr;
6809 void *hdr;
6811 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6812 if (!msg)
6813 return;
6815 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6816 if (!hdr) {
6817 nlmsg_free(msg);
6818 return;
6821 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6822 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6823 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6825 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6826 if (!pinfoattr)
6827 goto nla_put_failure;
6829 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6831 nla_nest_end(msg, pinfoattr);
6833 if (genlmsg_end(msg, hdr) < 0) {
6834 nlmsg_free(msg);
6835 return;
6838 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6839 nl80211_mlme_mcgrp.id, gfp);
6840 return;
6842 nla_put_failure:
6843 genlmsg_cancel(msg, hdr);
6844 nlmsg_free(msg);
6847 static int nl80211_netlink_notify(struct notifier_block * nb,
6848 unsigned long state,
6849 void *_notify)
6851 struct netlink_notify *notify = _notify;
6852 struct cfg80211_registered_device *rdev;
6853 struct wireless_dev *wdev;
6855 if (state != NETLINK_URELEASE)
6856 return NOTIFY_DONE;
6858 rcu_read_lock();
6860 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6861 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
6862 cfg80211_mlme_unregister_socket(wdev, notify->pid);
6864 rcu_read_unlock();
6866 return NOTIFY_DONE;
6869 static struct notifier_block nl80211_netlink_notifier = {
6870 .notifier_call = nl80211_netlink_notify,
6873 /* initialisation/exit functions */
6875 int nl80211_init(void)
6877 int err;
6879 err = genl_register_family_with_ops(&nl80211_fam,
6880 nl80211_ops, ARRAY_SIZE(nl80211_ops));
6881 if (err)
6882 return err;
6884 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6885 if (err)
6886 goto err_out;
6888 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6889 if (err)
6890 goto err_out;
6892 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6893 if (err)
6894 goto err_out;
6896 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6897 if (err)
6898 goto err_out;
6900 #ifdef CONFIG_NL80211_TESTMODE
6901 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6902 if (err)
6903 goto err_out;
6904 #endif
6906 err = netlink_register_notifier(&nl80211_netlink_notifier);
6907 if (err)
6908 goto err_out;
6910 return 0;
6911 err_out:
6912 genl_unregister_family(&nl80211_fam);
6913 return err;
6916 void nl80211_exit(void)
6918 netlink_unregister_notifier(&nl80211_netlink_notifier);
6919 genl_unregister_family(&nl80211_fam);