vfs: spread struct mount - get_dominating_id / do_make_slave
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wireless / nl80211.c
blobffafda5022c2b72d8f45714458e5fdd16203c4ee
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 bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
32 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
37 /* the netlink family */
38 static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
44 .netnsok = true,
45 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
49 /* internal helper: get rdev and dev */
50 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
54 struct nlattr **attrs = info->attrs;
55 int ifindex;
57 if (!attrs[NL80211_ATTR_IFINDEX])
58 return -EINVAL;
60 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
61 *dev = dev_get_by_index(genl_info_net(info), ifindex);
62 if (!*dev)
63 return -ENODEV;
65 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
66 if (IS_ERR(*rdev)) {
67 dev_put(*dev);
68 return PTR_ERR(*rdev);
71 return 0;
74 /* policy for the attributes */
75 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
76 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
78 .len = 20-1 },
79 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
80 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
81 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
82 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
86 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
92 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
93 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
95 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
96 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
135 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
137 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
138 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_DATA_LEN },
140 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
141 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
143 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
144 .len = IEEE80211_MAX_SSID_LEN },
145 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
146 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
147 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
148 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
149 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
150 [NL80211_ATTR_STA_FLAGS2] = {
151 .len = sizeof(struct nl80211_sta_flag_update),
153 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
154 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
155 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
156 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
157 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
158 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
159 [NL80211_ATTR_PID] = { .type = NLA_U32 },
160 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
161 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
162 .len = WLAN_PMKID_LEN },
163 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
164 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
165 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
166 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
167 .len = IEEE80211_MAX_DATA_LEN },
168 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
169 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
170 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
171 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
172 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
173 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
174 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
175 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
176 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
177 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
178 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
179 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
180 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
181 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
182 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
183 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
184 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
185 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
186 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
187 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
188 .len = IEEE80211_MAX_DATA_LEN },
189 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
190 .len = IEEE80211_MAX_DATA_LEN },
191 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
192 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
193 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
194 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
197 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
198 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
201 /* policy for the key attributes */
202 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
203 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
204 [NL80211_KEY_IDX] = { .type = NLA_U8 },
205 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
206 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
207 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
208 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
209 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
210 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
213 /* policy for the key default flags */
214 static const struct nla_policy
215 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
216 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
217 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
220 /* policy for WoWLAN attributes */
221 static const struct nla_policy
222 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
223 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
224 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
225 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
226 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
227 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
228 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
229 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
230 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
233 /* policy for GTK rekey offload attributes */
234 static const struct nla_policy
235 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
236 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
237 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
238 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
241 static const struct nla_policy
242 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
243 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
244 .len = IEEE80211_MAX_SSID_LEN },
247 /* ifidx get helper */
248 static int nl80211_get_ifidx(struct netlink_callback *cb)
250 int res;
252 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
253 nl80211_fam.attrbuf, nl80211_fam.maxattr,
254 nl80211_policy);
255 if (res)
256 return res;
258 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
259 return -EINVAL;
261 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
262 if (!res)
263 return -EINVAL;
264 return res;
267 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
268 struct netlink_callback *cb,
269 struct cfg80211_registered_device **rdev,
270 struct net_device **dev)
272 int ifidx = cb->args[0];
273 int err;
275 if (!ifidx)
276 ifidx = nl80211_get_ifidx(cb);
277 if (ifidx < 0)
278 return ifidx;
280 cb->args[0] = ifidx;
282 rtnl_lock();
284 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
285 if (!*dev) {
286 err = -ENODEV;
287 goto out_rtnl;
290 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
291 if (IS_ERR(*rdev)) {
292 err = PTR_ERR(*rdev);
293 goto out_rtnl;
296 return 0;
297 out_rtnl:
298 rtnl_unlock();
299 return err;
302 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
304 cfg80211_unlock_rdev(rdev);
305 rtnl_unlock();
308 /* IE validation */
309 static bool is_valid_ie_attr(const struct nlattr *attr)
311 const u8 *pos;
312 int len;
314 if (!attr)
315 return true;
317 pos = nla_data(attr);
318 len = nla_len(attr);
320 while (len) {
321 u8 elemlen;
323 if (len < 2)
324 return false;
325 len -= 2;
327 elemlen = pos[1];
328 if (elemlen > len)
329 return false;
331 len -= elemlen;
332 pos += 2 + elemlen;
335 return true;
338 /* message building helper */
339 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
340 int flags, u8 cmd)
342 /* since there is no private header just add the generic one */
343 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
346 static int nl80211_msg_put_channel(struct sk_buff *msg,
347 struct ieee80211_channel *chan)
349 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
350 chan->center_freq);
352 if (chan->flags & IEEE80211_CHAN_DISABLED)
353 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
354 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
355 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
356 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
357 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
358 if (chan->flags & IEEE80211_CHAN_RADAR)
359 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
361 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
362 DBM_TO_MBM(chan->max_power));
364 return 0;
366 nla_put_failure:
367 return -ENOBUFS;
370 /* netlink command implementations */
372 struct key_parse {
373 struct key_params p;
374 int idx;
375 int type;
376 bool def, defmgmt;
377 bool def_uni, def_multi;
380 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
382 struct nlattr *tb[NL80211_KEY_MAX + 1];
383 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
384 nl80211_key_policy);
385 if (err)
386 return err;
388 k->def = !!tb[NL80211_KEY_DEFAULT];
389 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
391 if (k->def) {
392 k->def_uni = true;
393 k->def_multi = true;
395 if (k->defmgmt)
396 k->def_multi = true;
398 if (tb[NL80211_KEY_IDX])
399 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
401 if (tb[NL80211_KEY_DATA]) {
402 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
403 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
406 if (tb[NL80211_KEY_SEQ]) {
407 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
408 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
411 if (tb[NL80211_KEY_CIPHER])
412 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
414 if (tb[NL80211_KEY_TYPE]) {
415 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
416 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
417 return -EINVAL;
420 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
421 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
422 int err = nla_parse_nested(kdt,
423 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
424 tb[NL80211_KEY_DEFAULT_TYPES],
425 nl80211_key_default_policy);
426 if (err)
427 return err;
429 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
430 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
433 return 0;
436 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
438 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
439 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
440 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
443 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
444 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
445 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
448 if (info->attrs[NL80211_ATTR_KEY_IDX])
449 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
451 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
452 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
454 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
455 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
457 if (k->def) {
458 k->def_uni = true;
459 k->def_multi = true;
461 if (k->defmgmt)
462 k->def_multi = true;
464 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
465 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
466 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
467 return -EINVAL;
470 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
471 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
472 int err = nla_parse_nested(
473 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
474 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
475 nl80211_key_default_policy);
476 if (err)
477 return err;
479 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
480 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
483 return 0;
486 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
488 int err;
490 memset(k, 0, sizeof(*k));
491 k->idx = -1;
492 k->type = -1;
494 if (info->attrs[NL80211_ATTR_KEY])
495 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
496 else
497 err = nl80211_parse_key_old(info, k);
499 if (err)
500 return err;
502 if (k->def && k->defmgmt)
503 return -EINVAL;
505 if (k->defmgmt) {
506 if (k->def_uni || !k->def_multi)
507 return -EINVAL;
510 if (k->idx != -1) {
511 if (k->defmgmt) {
512 if (k->idx < 4 || k->idx > 5)
513 return -EINVAL;
514 } else if (k->def) {
515 if (k->idx < 0 || k->idx > 3)
516 return -EINVAL;
517 } else {
518 if (k->idx < 0 || k->idx > 5)
519 return -EINVAL;
523 return 0;
526 static struct cfg80211_cached_keys *
527 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
528 struct nlattr *keys)
530 struct key_parse parse;
531 struct nlattr *key;
532 struct cfg80211_cached_keys *result;
533 int rem, err, def = 0;
535 result = kzalloc(sizeof(*result), GFP_KERNEL);
536 if (!result)
537 return ERR_PTR(-ENOMEM);
539 result->def = -1;
540 result->defmgmt = -1;
542 nla_for_each_nested(key, keys, rem) {
543 memset(&parse, 0, sizeof(parse));
544 parse.idx = -1;
546 err = nl80211_parse_key_new(key, &parse);
547 if (err)
548 goto error;
549 err = -EINVAL;
550 if (!parse.p.key)
551 goto error;
552 if (parse.idx < 0 || parse.idx > 4)
553 goto error;
554 if (parse.def) {
555 if (def)
556 goto error;
557 def = 1;
558 result->def = parse.idx;
559 if (!parse.def_uni || !parse.def_multi)
560 goto error;
561 } else if (parse.defmgmt)
562 goto error;
563 err = cfg80211_validate_key_settings(rdev, &parse.p,
564 parse.idx, false, NULL);
565 if (err)
566 goto error;
567 result->params[parse.idx].cipher = parse.p.cipher;
568 result->params[parse.idx].key_len = parse.p.key_len;
569 result->params[parse.idx].key = result->data[parse.idx];
570 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
573 return result;
574 error:
575 kfree(result);
576 return ERR_PTR(err);
579 static int nl80211_key_allowed(struct wireless_dev *wdev)
581 ASSERT_WDEV_LOCK(wdev);
583 switch (wdev->iftype) {
584 case NL80211_IFTYPE_AP:
585 case NL80211_IFTYPE_AP_VLAN:
586 case NL80211_IFTYPE_P2P_GO:
587 case NL80211_IFTYPE_MESH_POINT:
588 break;
589 case NL80211_IFTYPE_ADHOC:
590 if (!wdev->current_bss)
591 return -ENOLINK;
592 break;
593 case NL80211_IFTYPE_STATION:
594 case NL80211_IFTYPE_P2P_CLIENT:
595 if (wdev->sme_state != CFG80211_SME_CONNECTED)
596 return -ENOLINK;
597 break;
598 default:
599 return -EINVAL;
602 return 0;
605 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
607 struct nlattr *nl_modes = nla_nest_start(msg, attr);
608 int i;
610 if (!nl_modes)
611 goto nla_put_failure;
613 i = 0;
614 while (ifmodes) {
615 if (ifmodes & 1)
616 NLA_PUT_FLAG(msg, i);
617 ifmodes >>= 1;
618 i++;
621 nla_nest_end(msg, nl_modes);
622 return 0;
624 nla_put_failure:
625 return -ENOBUFS;
628 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
629 struct sk_buff *msg)
631 struct nlattr *nl_combis;
632 int i, j;
634 nl_combis = nla_nest_start(msg,
635 NL80211_ATTR_INTERFACE_COMBINATIONS);
636 if (!nl_combis)
637 goto nla_put_failure;
639 for (i = 0; i < wiphy->n_iface_combinations; i++) {
640 const struct ieee80211_iface_combination *c;
641 struct nlattr *nl_combi, *nl_limits;
643 c = &wiphy->iface_combinations[i];
645 nl_combi = nla_nest_start(msg, i + 1);
646 if (!nl_combi)
647 goto nla_put_failure;
649 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
650 if (!nl_limits)
651 goto nla_put_failure;
653 for (j = 0; j < c->n_limits; j++) {
654 struct nlattr *nl_limit;
656 nl_limit = nla_nest_start(msg, j + 1);
657 if (!nl_limit)
658 goto nla_put_failure;
659 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
660 c->limits[j].max);
661 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
662 c->limits[j].types))
663 goto nla_put_failure;
664 nla_nest_end(msg, nl_limit);
667 nla_nest_end(msg, nl_limits);
669 if (c->beacon_int_infra_match)
670 NLA_PUT_FLAG(msg,
671 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
672 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
673 c->num_different_channels);
674 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
675 c->max_interfaces);
677 nla_nest_end(msg, nl_combi);
680 nla_nest_end(msg, nl_combis);
682 return 0;
683 nla_put_failure:
684 return -ENOBUFS;
687 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
688 struct cfg80211_registered_device *dev)
690 void *hdr;
691 struct nlattr *nl_bands, *nl_band;
692 struct nlattr *nl_freqs, *nl_freq;
693 struct nlattr *nl_rates, *nl_rate;
694 struct nlattr *nl_cmds;
695 enum ieee80211_band band;
696 struct ieee80211_channel *chan;
697 struct ieee80211_rate *rate;
698 int i;
699 const struct ieee80211_txrx_stypes *mgmt_stypes =
700 dev->wiphy.mgmt_stypes;
702 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
703 if (!hdr)
704 return -1;
706 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
707 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
709 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
710 cfg80211_rdev_list_generation);
712 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
713 dev->wiphy.retry_short);
714 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
715 dev->wiphy.retry_long);
716 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
717 dev->wiphy.frag_threshold);
718 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
719 dev->wiphy.rts_threshold);
720 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
721 dev->wiphy.coverage_class);
722 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
723 dev->wiphy.max_scan_ssids);
724 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
725 dev->wiphy.max_sched_scan_ssids);
726 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
727 dev->wiphy.max_scan_ie_len);
728 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
729 dev->wiphy.max_sched_scan_ie_len);
730 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
731 dev->wiphy.max_match_sets);
733 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
734 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
735 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
736 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
737 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
738 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
739 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
740 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
741 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
742 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
743 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
744 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
746 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
747 sizeof(u32) * dev->wiphy.n_cipher_suites,
748 dev->wiphy.cipher_suites);
750 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
751 dev->wiphy.max_num_pmkids);
753 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
754 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
756 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
757 dev->wiphy.available_antennas_tx);
758 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
759 dev->wiphy.available_antennas_rx);
761 if ((dev->wiphy.available_antennas_tx ||
762 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
763 u32 tx_ant = 0, rx_ant = 0;
764 int res;
765 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
766 if (!res) {
767 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
768 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
772 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
773 dev->wiphy.interface_modes))
774 goto nla_put_failure;
776 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
777 if (!nl_bands)
778 goto nla_put_failure;
780 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
781 if (!dev->wiphy.bands[band])
782 continue;
784 nl_band = nla_nest_start(msg, band);
785 if (!nl_band)
786 goto nla_put_failure;
788 /* add HT info */
789 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
790 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
791 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
792 &dev->wiphy.bands[band]->ht_cap.mcs);
793 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
794 dev->wiphy.bands[band]->ht_cap.cap);
795 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
796 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
797 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
798 dev->wiphy.bands[band]->ht_cap.ampdu_density);
801 /* add frequencies */
802 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
803 if (!nl_freqs)
804 goto nla_put_failure;
806 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
807 nl_freq = nla_nest_start(msg, i);
808 if (!nl_freq)
809 goto nla_put_failure;
811 chan = &dev->wiphy.bands[band]->channels[i];
813 if (nl80211_msg_put_channel(msg, chan))
814 goto nla_put_failure;
816 nla_nest_end(msg, nl_freq);
819 nla_nest_end(msg, nl_freqs);
821 /* add bitrates */
822 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
823 if (!nl_rates)
824 goto nla_put_failure;
826 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
827 nl_rate = nla_nest_start(msg, i);
828 if (!nl_rate)
829 goto nla_put_failure;
831 rate = &dev->wiphy.bands[band]->bitrates[i];
832 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
833 rate->bitrate);
834 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
835 NLA_PUT_FLAG(msg,
836 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
838 nla_nest_end(msg, nl_rate);
841 nla_nest_end(msg, nl_rates);
843 nla_nest_end(msg, nl_band);
845 nla_nest_end(msg, nl_bands);
847 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
848 if (!nl_cmds)
849 goto nla_put_failure;
851 i = 0;
852 #define CMD(op, n) \
853 do { \
854 if (dev->ops->op) { \
855 i++; \
856 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
858 } while (0)
860 CMD(add_virtual_intf, NEW_INTERFACE);
861 CMD(change_virtual_intf, SET_INTERFACE);
862 CMD(add_key, NEW_KEY);
863 CMD(add_beacon, NEW_BEACON);
864 CMD(add_station, NEW_STATION);
865 CMD(add_mpath, NEW_MPATH);
866 CMD(update_mesh_config, SET_MESH_CONFIG);
867 CMD(change_bss, SET_BSS);
868 CMD(auth, AUTHENTICATE);
869 CMD(assoc, ASSOCIATE);
870 CMD(deauth, DEAUTHENTICATE);
871 CMD(disassoc, DISASSOCIATE);
872 CMD(join_ibss, JOIN_IBSS);
873 CMD(join_mesh, JOIN_MESH);
874 CMD(set_pmksa, SET_PMKSA);
875 CMD(del_pmksa, DEL_PMKSA);
876 CMD(flush_pmksa, FLUSH_PMKSA);
877 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
878 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
879 CMD(mgmt_tx, FRAME);
880 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
881 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
882 i++;
883 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
885 CMD(set_channel, SET_CHANNEL);
886 CMD(set_wds_peer, SET_WDS_PEER);
887 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
888 CMD(tdls_mgmt, TDLS_MGMT);
889 CMD(tdls_oper, TDLS_OPER);
891 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
892 CMD(sched_scan_start, START_SCHED_SCAN);
894 #undef CMD
896 if (dev->ops->connect || dev->ops->auth) {
897 i++;
898 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
901 if (dev->ops->disconnect || dev->ops->deauth) {
902 i++;
903 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
906 nla_nest_end(msg, nl_cmds);
908 if (dev->ops->remain_on_channel)
909 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
910 dev->wiphy.max_remain_on_channel_duration);
912 if (dev->ops->mgmt_tx_cancel_wait)
913 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
915 if (mgmt_stypes) {
916 u16 stypes;
917 struct nlattr *nl_ftypes, *nl_ifs;
918 enum nl80211_iftype ift;
920 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
921 if (!nl_ifs)
922 goto nla_put_failure;
924 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
925 nl_ftypes = nla_nest_start(msg, ift);
926 if (!nl_ftypes)
927 goto nla_put_failure;
928 i = 0;
929 stypes = mgmt_stypes[ift].tx;
930 while (stypes) {
931 if (stypes & 1)
932 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
933 (i << 4) | IEEE80211_FTYPE_MGMT);
934 stypes >>= 1;
935 i++;
937 nla_nest_end(msg, nl_ftypes);
940 nla_nest_end(msg, nl_ifs);
942 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
943 if (!nl_ifs)
944 goto nla_put_failure;
946 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
947 nl_ftypes = nla_nest_start(msg, ift);
948 if (!nl_ftypes)
949 goto nla_put_failure;
950 i = 0;
951 stypes = mgmt_stypes[ift].rx;
952 while (stypes) {
953 if (stypes & 1)
954 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
955 (i << 4) | IEEE80211_FTYPE_MGMT);
956 stypes >>= 1;
957 i++;
959 nla_nest_end(msg, nl_ftypes);
961 nla_nest_end(msg, nl_ifs);
964 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
965 struct nlattr *nl_wowlan;
967 nl_wowlan = nla_nest_start(msg,
968 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
969 if (!nl_wowlan)
970 goto nla_put_failure;
972 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
973 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
974 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
975 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
976 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
977 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
978 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
979 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
980 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
981 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
982 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
983 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
984 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
985 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
986 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
987 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
988 if (dev->wiphy.wowlan.n_patterns) {
989 struct nl80211_wowlan_pattern_support pat = {
990 .max_patterns = dev->wiphy.wowlan.n_patterns,
991 .min_pattern_len =
992 dev->wiphy.wowlan.pattern_min_len,
993 .max_pattern_len =
994 dev->wiphy.wowlan.pattern_max_len,
996 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
997 sizeof(pat), &pat);
1000 nla_nest_end(msg, nl_wowlan);
1003 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1004 dev->wiphy.software_iftypes))
1005 goto nla_put_failure;
1007 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1008 goto nla_put_failure;
1010 return genlmsg_end(msg, hdr);
1012 nla_put_failure:
1013 genlmsg_cancel(msg, hdr);
1014 return -EMSGSIZE;
1017 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1019 int idx = 0;
1020 int start = cb->args[0];
1021 struct cfg80211_registered_device *dev;
1023 mutex_lock(&cfg80211_mutex);
1024 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
1025 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1026 continue;
1027 if (++idx <= start)
1028 continue;
1029 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1030 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1031 dev) < 0) {
1032 idx--;
1033 break;
1036 mutex_unlock(&cfg80211_mutex);
1038 cb->args[0] = idx;
1040 return skb->len;
1043 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1045 struct sk_buff *msg;
1046 struct cfg80211_registered_device *dev = info->user_ptr[0];
1048 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1049 if (!msg)
1050 return -ENOMEM;
1052 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1053 nlmsg_free(msg);
1054 return -ENOBUFS;
1057 return genlmsg_reply(msg, info);
1060 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1061 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1062 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1063 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1064 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1065 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1068 static int parse_txq_params(struct nlattr *tb[],
1069 struct ieee80211_txq_params *txq_params)
1071 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1072 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1073 !tb[NL80211_TXQ_ATTR_AIFS])
1074 return -EINVAL;
1076 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1077 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1078 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1079 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1080 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1082 return 0;
1085 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1088 * You can only set the channel explicitly for AP, mesh
1089 * and WDS type interfaces; all others have their channel
1090 * managed via their respective "establish a connection"
1091 * command (connect, join, ...)
1093 * Monitors are special as they are normally slaved to
1094 * whatever else is going on, so they behave as though
1095 * you tried setting the wiphy channel itself.
1097 return !wdev ||
1098 wdev->iftype == NL80211_IFTYPE_AP ||
1099 wdev->iftype == NL80211_IFTYPE_WDS ||
1100 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1101 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1102 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1105 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1106 struct wireless_dev *wdev,
1107 struct genl_info *info)
1109 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1110 u32 freq;
1111 int result;
1113 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1114 return -EINVAL;
1116 if (!nl80211_can_set_dev_channel(wdev))
1117 return -EOPNOTSUPP;
1119 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1120 channel_type = nla_get_u32(info->attrs[
1121 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1122 if (channel_type != NL80211_CHAN_NO_HT &&
1123 channel_type != NL80211_CHAN_HT20 &&
1124 channel_type != NL80211_CHAN_HT40PLUS &&
1125 channel_type != NL80211_CHAN_HT40MINUS)
1126 return -EINVAL;
1129 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1131 mutex_lock(&rdev->devlist_mtx);
1132 if (wdev) {
1133 wdev_lock(wdev);
1134 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1135 wdev_unlock(wdev);
1136 } else {
1137 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1139 mutex_unlock(&rdev->devlist_mtx);
1141 return result;
1144 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1146 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1147 struct net_device *netdev = info->user_ptr[1];
1149 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1152 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1155 struct net_device *dev = info->user_ptr[1];
1156 struct wireless_dev *wdev = dev->ieee80211_ptr;
1157 const u8 *bssid;
1159 if (!info->attrs[NL80211_ATTR_MAC])
1160 return -EINVAL;
1162 if (netif_running(dev))
1163 return -EBUSY;
1165 if (!rdev->ops->set_wds_peer)
1166 return -EOPNOTSUPP;
1168 if (wdev->iftype != NL80211_IFTYPE_WDS)
1169 return -EOPNOTSUPP;
1171 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1172 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
1176 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1178 struct cfg80211_registered_device *rdev;
1179 struct net_device *netdev = NULL;
1180 struct wireless_dev *wdev;
1181 int result = 0, rem_txq_params = 0;
1182 struct nlattr *nl_txq_params;
1183 u32 changed;
1184 u8 retry_short = 0, retry_long = 0;
1185 u32 frag_threshold = 0, rts_threshold = 0;
1186 u8 coverage_class = 0;
1189 * Try to find the wiphy and netdev. Normally this
1190 * function shouldn't need the netdev, but this is
1191 * done for backward compatibility -- previously
1192 * setting the channel was done per wiphy, but now
1193 * it is per netdev. Previous userland like hostapd
1194 * also passed a netdev to set_wiphy, so that it is
1195 * possible to let that go to the right netdev!
1197 mutex_lock(&cfg80211_mutex);
1199 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1200 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1202 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1203 if (netdev && netdev->ieee80211_ptr) {
1204 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1205 mutex_lock(&rdev->mtx);
1206 } else
1207 netdev = NULL;
1210 if (!netdev) {
1211 rdev = __cfg80211_rdev_from_info(info);
1212 if (IS_ERR(rdev)) {
1213 mutex_unlock(&cfg80211_mutex);
1214 return PTR_ERR(rdev);
1216 wdev = NULL;
1217 netdev = NULL;
1218 result = 0;
1220 mutex_lock(&rdev->mtx);
1221 } else if (netif_running(netdev) &&
1222 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1223 wdev = netdev->ieee80211_ptr;
1224 else
1225 wdev = NULL;
1228 * end workaround code, by now the rdev is available
1229 * and locked, and wdev may or may not be NULL.
1232 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1233 result = cfg80211_dev_rename(
1234 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1236 mutex_unlock(&cfg80211_mutex);
1238 if (result)
1239 goto bad_res;
1241 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1242 struct ieee80211_txq_params txq_params;
1243 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1245 if (!rdev->ops->set_txq_params) {
1246 result = -EOPNOTSUPP;
1247 goto bad_res;
1250 if (!netdev) {
1251 result = -EINVAL;
1252 goto bad_res;
1255 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1256 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1257 result = -EINVAL;
1258 goto bad_res;
1261 nla_for_each_nested(nl_txq_params,
1262 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1263 rem_txq_params) {
1264 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1265 nla_data(nl_txq_params),
1266 nla_len(nl_txq_params),
1267 txq_params_policy);
1268 result = parse_txq_params(tb, &txq_params);
1269 if (result)
1270 goto bad_res;
1272 result = rdev->ops->set_txq_params(&rdev->wiphy,
1273 netdev,
1274 &txq_params);
1275 if (result)
1276 goto bad_res;
1280 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1281 result = __nl80211_set_channel(rdev, wdev, info);
1282 if (result)
1283 goto bad_res;
1286 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1287 enum nl80211_tx_power_setting type;
1288 int idx, mbm = 0;
1290 if (!rdev->ops->set_tx_power) {
1291 result = -EOPNOTSUPP;
1292 goto bad_res;
1295 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1296 type = nla_get_u32(info->attrs[idx]);
1298 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1299 (type != NL80211_TX_POWER_AUTOMATIC)) {
1300 result = -EINVAL;
1301 goto bad_res;
1304 if (type != NL80211_TX_POWER_AUTOMATIC) {
1305 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1306 mbm = nla_get_u32(info->attrs[idx]);
1309 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1310 if (result)
1311 goto bad_res;
1314 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1315 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1316 u32 tx_ant, rx_ant;
1317 if ((!rdev->wiphy.available_antennas_tx &&
1318 !rdev->wiphy.available_antennas_rx) ||
1319 !rdev->ops->set_antenna) {
1320 result = -EOPNOTSUPP;
1321 goto bad_res;
1324 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1325 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1327 /* reject antenna configurations which don't match the
1328 * available antenna masks, except for the "all" mask */
1329 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1330 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1331 result = -EINVAL;
1332 goto bad_res;
1335 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1336 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1338 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1339 if (result)
1340 goto bad_res;
1343 changed = 0;
1345 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1346 retry_short = nla_get_u8(
1347 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1348 if (retry_short == 0) {
1349 result = -EINVAL;
1350 goto bad_res;
1352 changed |= WIPHY_PARAM_RETRY_SHORT;
1355 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1356 retry_long = nla_get_u8(
1357 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1358 if (retry_long == 0) {
1359 result = -EINVAL;
1360 goto bad_res;
1362 changed |= WIPHY_PARAM_RETRY_LONG;
1365 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1366 frag_threshold = nla_get_u32(
1367 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1368 if (frag_threshold < 256) {
1369 result = -EINVAL;
1370 goto bad_res;
1372 if (frag_threshold != (u32) -1) {
1374 * Fragments (apart from the last one) are required to
1375 * have even length. Make the fragmentation code
1376 * simpler by stripping LSB should someone try to use
1377 * odd threshold value.
1379 frag_threshold &= ~0x1;
1381 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1384 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1385 rts_threshold = nla_get_u32(
1386 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1387 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1390 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1391 coverage_class = nla_get_u8(
1392 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1393 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1396 if (changed) {
1397 u8 old_retry_short, old_retry_long;
1398 u32 old_frag_threshold, old_rts_threshold;
1399 u8 old_coverage_class;
1401 if (!rdev->ops->set_wiphy_params) {
1402 result = -EOPNOTSUPP;
1403 goto bad_res;
1406 old_retry_short = rdev->wiphy.retry_short;
1407 old_retry_long = rdev->wiphy.retry_long;
1408 old_frag_threshold = rdev->wiphy.frag_threshold;
1409 old_rts_threshold = rdev->wiphy.rts_threshold;
1410 old_coverage_class = rdev->wiphy.coverage_class;
1412 if (changed & WIPHY_PARAM_RETRY_SHORT)
1413 rdev->wiphy.retry_short = retry_short;
1414 if (changed & WIPHY_PARAM_RETRY_LONG)
1415 rdev->wiphy.retry_long = retry_long;
1416 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1417 rdev->wiphy.frag_threshold = frag_threshold;
1418 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1419 rdev->wiphy.rts_threshold = rts_threshold;
1420 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1421 rdev->wiphy.coverage_class = coverage_class;
1423 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1424 if (result) {
1425 rdev->wiphy.retry_short = old_retry_short;
1426 rdev->wiphy.retry_long = old_retry_long;
1427 rdev->wiphy.frag_threshold = old_frag_threshold;
1428 rdev->wiphy.rts_threshold = old_rts_threshold;
1429 rdev->wiphy.coverage_class = old_coverage_class;
1433 bad_res:
1434 mutex_unlock(&rdev->mtx);
1435 if (netdev)
1436 dev_put(netdev);
1437 return result;
1441 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1442 struct cfg80211_registered_device *rdev,
1443 struct net_device *dev)
1445 void *hdr;
1447 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1448 if (!hdr)
1449 return -1;
1451 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1453 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1454 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1456 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1457 rdev->devlist_generation ^
1458 (cfg80211_rdev_list_generation << 2));
1460 return genlmsg_end(msg, hdr);
1462 nla_put_failure:
1463 genlmsg_cancel(msg, hdr);
1464 return -EMSGSIZE;
1467 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1469 int wp_idx = 0;
1470 int if_idx = 0;
1471 int wp_start = cb->args[0];
1472 int if_start = cb->args[1];
1473 struct cfg80211_registered_device *rdev;
1474 struct wireless_dev *wdev;
1476 mutex_lock(&cfg80211_mutex);
1477 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1478 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1479 continue;
1480 if (wp_idx < wp_start) {
1481 wp_idx++;
1482 continue;
1484 if_idx = 0;
1486 mutex_lock(&rdev->devlist_mtx);
1487 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1488 if (if_idx < if_start) {
1489 if_idx++;
1490 continue;
1492 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1493 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1494 rdev, wdev->netdev) < 0) {
1495 mutex_unlock(&rdev->devlist_mtx);
1496 goto out;
1498 if_idx++;
1500 mutex_unlock(&rdev->devlist_mtx);
1502 wp_idx++;
1504 out:
1505 mutex_unlock(&cfg80211_mutex);
1507 cb->args[0] = wp_idx;
1508 cb->args[1] = if_idx;
1510 return skb->len;
1513 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1515 struct sk_buff *msg;
1516 struct cfg80211_registered_device *dev = info->user_ptr[0];
1517 struct net_device *netdev = info->user_ptr[1];
1519 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1520 if (!msg)
1521 return -ENOMEM;
1523 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1524 dev, netdev) < 0) {
1525 nlmsg_free(msg);
1526 return -ENOBUFS;
1529 return genlmsg_reply(msg, info);
1532 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1533 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1534 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1535 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1536 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1537 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1540 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1542 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1543 int flag;
1545 *mntrflags = 0;
1547 if (!nla)
1548 return -EINVAL;
1550 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1551 nla, mntr_flags_policy))
1552 return -EINVAL;
1554 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1555 if (flags[flag])
1556 *mntrflags |= (1<<flag);
1558 return 0;
1561 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1562 struct net_device *netdev, u8 use_4addr,
1563 enum nl80211_iftype iftype)
1565 if (!use_4addr) {
1566 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1567 return -EBUSY;
1568 return 0;
1571 switch (iftype) {
1572 case NL80211_IFTYPE_AP_VLAN:
1573 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1574 return 0;
1575 break;
1576 case NL80211_IFTYPE_STATION:
1577 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1578 return 0;
1579 break;
1580 default:
1581 break;
1584 return -EOPNOTSUPP;
1587 static int nl80211_set_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 int err;
1592 enum nl80211_iftype otype, ntype;
1593 struct net_device *dev = info->user_ptr[1];
1594 u32 _flags, *flags = NULL;
1595 bool change = false;
1597 memset(&params, 0, sizeof(params));
1599 otype = ntype = dev->ieee80211_ptr->iftype;
1601 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1602 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1603 if (otype != ntype)
1604 change = true;
1605 if (ntype > NL80211_IFTYPE_MAX)
1606 return -EINVAL;
1609 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1610 struct wireless_dev *wdev = dev->ieee80211_ptr;
1612 if (ntype != NL80211_IFTYPE_MESH_POINT)
1613 return -EINVAL;
1614 if (netif_running(dev))
1615 return -EBUSY;
1617 wdev_lock(wdev);
1618 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1619 IEEE80211_MAX_MESH_ID_LEN);
1620 wdev->mesh_id_up_len =
1621 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1622 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1623 wdev->mesh_id_up_len);
1624 wdev_unlock(wdev);
1627 if (info->attrs[NL80211_ATTR_4ADDR]) {
1628 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1629 change = true;
1630 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1631 if (err)
1632 return err;
1633 } else {
1634 params.use_4addr = -1;
1637 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1638 if (ntype != NL80211_IFTYPE_MONITOR)
1639 return -EINVAL;
1640 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1641 &_flags);
1642 if (err)
1643 return err;
1645 flags = &_flags;
1646 change = true;
1649 if (change)
1650 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1651 else
1652 err = 0;
1654 if (!err && params.use_4addr != -1)
1655 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1657 return err;
1660 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1662 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1663 struct vif_params params;
1664 struct net_device *dev;
1665 int err;
1666 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1667 u32 flags;
1669 memset(&params, 0, sizeof(params));
1671 if (!info->attrs[NL80211_ATTR_IFNAME])
1672 return -EINVAL;
1674 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1675 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1676 if (type > NL80211_IFTYPE_MAX)
1677 return -EINVAL;
1680 if (!rdev->ops->add_virtual_intf ||
1681 !(rdev->wiphy.interface_modes & (1 << type)))
1682 return -EOPNOTSUPP;
1684 if (info->attrs[NL80211_ATTR_4ADDR]) {
1685 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1686 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1687 if (err)
1688 return err;
1691 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1692 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1693 &flags);
1694 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
1695 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1696 type, err ? NULL : &flags, &params);
1697 if (IS_ERR(dev))
1698 return PTR_ERR(dev);
1700 if (type == NL80211_IFTYPE_MESH_POINT &&
1701 info->attrs[NL80211_ATTR_MESH_ID]) {
1702 struct wireless_dev *wdev = dev->ieee80211_ptr;
1704 wdev_lock(wdev);
1705 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1706 IEEE80211_MAX_MESH_ID_LEN);
1707 wdev->mesh_id_up_len =
1708 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1709 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1710 wdev->mesh_id_up_len);
1711 wdev_unlock(wdev);
1714 return 0;
1717 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1719 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1720 struct net_device *dev = info->user_ptr[1];
1722 if (!rdev->ops->del_virtual_intf)
1723 return -EOPNOTSUPP;
1725 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1728 struct get_key_cookie {
1729 struct sk_buff *msg;
1730 int error;
1731 int idx;
1734 static void get_key_callback(void *c, struct key_params *params)
1736 struct nlattr *key;
1737 struct get_key_cookie *cookie = c;
1739 if (params->key)
1740 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1741 params->key_len, params->key);
1743 if (params->seq)
1744 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1745 params->seq_len, params->seq);
1747 if (params->cipher)
1748 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1749 params->cipher);
1751 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1752 if (!key)
1753 goto nla_put_failure;
1755 if (params->key)
1756 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1757 params->key_len, params->key);
1759 if (params->seq)
1760 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1761 params->seq_len, params->seq);
1763 if (params->cipher)
1764 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1765 params->cipher);
1767 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1769 nla_nest_end(cookie->msg, key);
1771 return;
1772 nla_put_failure:
1773 cookie->error = 1;
1776 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1778 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1779 int err;
1780 struct net_device *dev = info->user_ptr[1];
1781 u8 key_idx = 0;
1782 const u8 *mac_addr = NULL;
1783 bool pairwise;
1784 struct get_key_cookie cookie = {
1785 .error = 0,
1787 void *hdr;
1788 struct sk_buff *msg;
1790 if (info->attrs[NL80211_ATTR_KEY_IDX])
1791 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1793 if (key_idx > 5)
1794 return -EINVAL;
1796 if (info->attrs[NL80211_ATTR_MAC])
1797 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1799 pairwise = !!mac_addr;
1800 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1801 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1802 if (kt >= NUM_NL80211_KEYTYPES)
1803 return -EINVAL;
1804 if (kt != NL80211_KEYTYPE_GROUP &&
1805 kt != NL80211_KEYTYPE_PAIRWISE)
1806 return -EINVAL;
1807 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1810 if (!rdev->ops->get_key)
1811 return -EOPNOTSUPP;
1813 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1814 if (!msg)
1815 return -ENOMEM;
1817 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1818 NL80211_CMD_NEW_KEY);
1819 if (IS_ERR(hdr))
1820 return PTR_ERR(hdr);
1822 cookie.msg = msg;
1823 cookie.idx = key_idx;
1825 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1826 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1827 if (mac_addr)
1828 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1830 if (pairwise && mac_addr &&
1831 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1832 return -ENOENT;
1834 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1835 mac_addr, &cookie, get_key_callback);
1837 if (err)
1838 goto free_msg;
1840 if (cookie.error)
1841 goto nla_put_failure;
1843 genlmsg_end(msg, hdr);
1844 return genlmsg_reply(msg, info);
1846 nla_put_failure:
1847 err = -ENOBUFS;
1848 free_msg:
1849 nlmsg_free(msg);
1850 return err;
1853 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1856 struct key_parse key;
1857 int err;
1858 struct net_device *dev = info->user_ptr[1];
1860 err = nl80211_parse_key(info, &key);
1861 if (err)
1862 return err;
1864 if (key.idx < 0)
1865 return -EINVAL;
1867 /* only support setting default key */
1868 if (!key.def && !key.defmgmt)
1869 return -EINVAL;
1871 wdev_lock(dev->ieee80211_ptr);
1873 if (key.def) {
1874 if (!rdev->ops->set_default_key) {
1875 err = -EOPNOTSUPP;
1876 goto out;
1879 err = nl80211_key_allowed(dev->ieee80211_ptr);
1880 if (err)
1881 goto out;
1883 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1884 key.def_uni, key.def_multi);
1886 if (err)
1887 goto out;
1889 #ifdef CONFIG_CFG80211_WEXT
1890 dev->ieee80211_ptr->wext.default_key = key.idx;
1891 #endif
1892 } else {
1893 if (key.def_uni || !key.def_multi) {
1894 err = -EINVAL;
1895 goto out;
1898 if (!rdev->ops->set_default_mgmt_key) {
1899 err = -EOPNOTSUPP;
1900 goto out;
1903 err = nl80211_key_allowed(dev->ieee80211_ptr);
1904 if (err)
1905 goto out;
1907 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1908 dev, key.idx);
1909 if (err)
1910 goto out;
1912 #ifdef CONFIG_CFG80211_WEXT
1913 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1914 #endif
1917 out:
1918 wdev_unlock(dev->ieee80211_ptr);
1920 return err;
1923 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1926 int err;
1927 struct net_device *dev = info->user_ptr[1];
1928 struct key_parse key;
1929 const u8 *mac_addr = NULL;
1931 err = nl80211_parse_key(info, &key);
1932 if (err)
1933 return err;
1935 if (!key.p.key)
1936 return -EINVAL;
1938 if (info->attrs[NL80211_ATTR_MAC])
1939 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1941 if (key.type == -1) {
1942 if (mac_addr)
1943 key.type = NL80211_KEYTYPE_PAIRWISE;
1944 else
1945 key.type = NL80211_KEYTYPE_GROUP;
1948 /* for now */
1949 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1950 key.type != NL80211_KEYTYPE_GROUP)
1951 return -EINVAL;
1953 if (!rdev->ops->add_key)
1954 return -EOPNOTSUPP;
1956 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1957 key.type == NL80211_KEYTYPE_PAIRWISE,
1958 mac_addr))
1959 return -EINVAL;
1961 wdev_lock(dev->ieee80211_ptr);
1962 err = nl80211_key_allowed(dev->ieee80211_ptr);
1963 if (!err)
1964 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1965 key.type == NL80211_KEYTYPE_PAIRWISE,
1966 mac_addr, &key.p);
1967 wdev_unlock(dev->ieee80211_ptr);
1969 return err;
1972 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1974 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1975 int err;
1976 struct net_device *dev = info->user_ptr[1];
1977 u8 *mac_addr = NULL;
1978 struct key_parse key;
1980 err = nl80211_parse_key(info, &key);
1981 if (err)
1982 return err;
1984 if (info->attrs[NL80211_ATTR_MAC])
1985 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1987 if (key.type == -1) {
1988 if (mac_addr)
1989 key.type = NL80211_KEYTYPE_PAIRWISE;
1990 else
1991 key.type = NL80211_KEYTYPE_GROUP;
1994 /* for now */
1995 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1996 key.type != NL80211_KEYTYPE_GROUP)
1997 return -EINVAL;
1999 if (!rdev->ops->del_key)
2000 return -EOPNOTSUPP;
2002 wdev_lock(dev->ieee80211_ptr);
2003 err = nl80211_key_allowed(dev->ieee80211_ptr);
2005 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2006 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2007 err = -ENOENT;
2009 if (!err)
2010 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2011 key.type == NL80211_KEYTYPE_PAIRWISE,
2012 mac_addr);
2014 #ifdef CONFIG_CFG80211_WEXT
2015 if (!err) {
2016 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2017 dev->ieee80211_ptr->wext.default_key = -1;
2018 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2019 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2021 #endif
2022 wdev_unlock(dev->ieee80211_ptr);
2024 return err;
2027 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2029 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2030 struct beacon_parameters *info);
2031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2032 struct net_device *dev = info->user_ptr[1];
2033 struct wireless_dev *wdev = dev->ieee80211_ptr;
2034 struct beacon_parameters params;
2035 int haveinfo = 0, err;
2037 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2038 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2039 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2040 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
2041 return -EINVAL;
2043 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2044 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2045 return -EOPNOTSUPP;
2047 memset(&params, 0, sizeof(params));
2049 switch (info->genlhdr->cmd) {
2050 case NL80211_CMD_NEW_BEACON:
2051 /* these are required for NEW_BEACON */
2052 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2053 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2054 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2055 return -EINVAL;
2057 params.interval =
2058 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2059 params.dtim_period =
2060 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2062 err = cfg80211_validate_beacon_int(rdev, params.interval);
2063 if (err)
2064 return err;
2067 * In theory, some of these attributes could be required for
2068 * NEW_BEACON, but since they were not used when the command was
2069 * originally added, keep them optional for old user space
2070 * programs to work with drivers that do not need the additional
2071 * information.
2073 if (info->attrs[NL80211_ATTR_SSID]) {
2074 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2075 params.ssid_len =
2076 nla_len(info->attrs[NL80211_ATTR_SSID]);
2077 if (params.ssid_len == 0 ||
2078 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2079 return -EINVAL;
2082 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2083 params.hidden_ssid = nla_get_u32(
2084 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2085 if (params.hidden_ssid !=
2086 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2087 params.hidden_ssid !=
2088 NL80211_HIDDEN_SSID_ZERO_LEN &&
2089 params.hidden_ssid !=
2090 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2091 return -EINVAL;
2094 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2096 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2097 params.auth_type = nla_get_u32(
2098 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2099 if (!nl80211_valid_auth_type(params.auth_type))
2100 return -EINVAL;
2101 } else
2102 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2104 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2105 NL80211_MAX_NR_CIPHER_SUITES);
2106 if (err)
2107 return err;
2109 call = rdev->ops->add_beacon;
2110 break;
2111 case NL80211_CMD_SET_BEACON:
2112 call = rdev->ops->set_beacon;
2113 break;
2114 default:
2115 WARN_ON(1);
2116 return -EOPNOTSUPP;
2119 if (!call)
2120 return -EOPNOTSUPP;
2122 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2123 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2124 params.head_len =
2125 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2126 haveinfo = 1;
2129 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2130 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2131 params.tail_len =
2132 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2133 haveinfo = 1;
2136 if (!haveinfo)
2137 return -EINVAL;
2139 if (info->attrs[NL80211_ATTR_IE]) {
2140 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2141 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2144 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2145 params.proberesp_ies =
2146 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2147 params.proberesp_ies_len =
2148 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2151 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2152 params.assocresp_ies =
2153 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2154 params.assocresp_ies_len =
2155 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2158 err = call(&rdev->wiphy, dev, &params);
2159 if (!err && params.interval)
2160 wdev->beacon_interval = params.interval;
2161 return err;
2164 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2167 struct net_device *dev = info->user_ptr[1];
2168 struct wireless_dev *wdev = dev->ieee80211_ptr;
2169 int err;
2171 if (!rdev->ops->del_beacon)
2172 return -EOPNOTSUPP;
2174 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2175 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2176 return -EOPNOTSUPP;
2178 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2179 if (!err)
2180 wdev->beacon_interval = 0;
2181 return err;
2184 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2185 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2186 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2187 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2188 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2189 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2192 static int parse_station_flags(struct genl_info *info,
2193 struct station_parameters *params)
2195 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2196 struct nlattr *nla;
2197 int flag;
2200 * Try parsing the new attribute first so userspace
2201 * can specify both for older kernels.
2203 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2204 if (nla) {
2205 struct nl80211_sta_flag_update *sta_flags;
2207 sta_flags = nla_data(nla);
2208 params->sta_flags_mask = sta_flags->mask;
2209 params->sta_flags_set = sta_flags->set;
2210 if ((params->sta_flags_mask |
2211 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2212 return -EINVAL;
2213 return 0;
2216 /* if present, parse the old attribute */
2218 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2219 if (!nla)
2220 return 0;
2222 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2223 nla, sta_flags_policy))
2224 return -EINVAL;
2226 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2227 params->sta_flags_mask &= ~1;
2229 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2230 if (flags[flag])
2231 params->sta_flags_set |= (1<<flag);
2233 return 0;
2236 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2237 int attr)
2239 struct nlattr *rate;
2240 u16 bitrate;
2242 rate = nla_nest_start(msg, attr);
2243 if (!rate)
2244 goto nla_put_failure;
2246 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2247 bitrate = cfg80211_calculate_bitrate(info);
2248 if (bitrate > 0)
2249 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2251 if (info->flags & RATE_INFO_FLAGS_MCS)
2252 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2253 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2254 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2255 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2256 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2258 nla_nest_end(msg, rate);
2259 return true;
2261 nla_put_failure:
2262 return false;
2265 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2266 int flags, struct net_device *dev,
2267 const u8 *mac_addr, struct station_info *sinfo)
2269 void *hdr;
2270 struct nlattr *sinfoattr, *bss_param;
2272 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2273 if (!hdr)
2274 return -1;
2276 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2277 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2279 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2281 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2282 if (!sinfoattr)
2283 goto nla_put_failure;
2284 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2285 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2286 sinfo->connected_time);
2287 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2288 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2289 sinfo->inactive_time);
2290 if (sinfo->filled & STATION_INFO_RX_BYTES)
2291 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2292 sinfo->rx_bytes);
2293 if (sinfo->filled & STATION_INFO_TX_BYTES)
2294 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2295 sinfo->tx_bytes);
2296 if (sinfo->filled & STATION_INFO_LLID)
2297 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2298 sinfo->llid);
2299 if (sinfo->filled & STATION_INFO_PLID)
2300 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2301 sinfo->plid);
2302 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2303 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2304 sinfo->plink_state);
2305 if (sinfo->filled & STATION_INFO_SIGNAL)
2306 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2307 sinfo->signal);
2308 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2309 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2310 sinfo->signal_avg);
2311 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2312 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2313 NL80211_STA_INFO_TX_BITRATE))
2314 goto nla_put_failure;
2316 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2317 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2318 NL80211_STA_INFO_RX_BITRATE))
2319 goto nla_put_failure;
2321 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2322 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2323 sinfo->rx_packets);
2324 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2325 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2326 sinfo->tx_packets);
2327 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2328 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2329 sinfo->tx_retries);
2330 if (sinfo->filled & STATION_INFO_TX_FAILED)
2331 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2332 sinfo->tx_failed);
2333 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2334 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2335 if (!bss_param)
2336 goto nla_put_failure;
2338 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2339 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2340 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2341 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2342 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2343 NLA_PUT_FLAG(msg,
2344 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2345 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2346 sinfo->bss_param.dtim_period);
2347 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2348 sinfo->bss_param.beacon_interval);
2350 nla_nest_end(msg, bss_param);
2352 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2353 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2354 sizeof(struct nl80211_sta_flag_update),
2355 &sinfo->sta_flags);
2356 nla_nest_end(msg, sinfoattr);
2358 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
2359 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2360 sinfo->assoc_req_ies);
2362 return genlmsg_end(msg, hdr);
2364 nla_put_failure:
2365 genlmsg_cancel(msg, hdr);
2366 return -EMSGSIZE;
2369 static int nl80211_dump_station(struct sk_buff *skb,
2370 struct netlink_callback *cb)
2372 struct station_info sinfo;
2373 struct cfg80211_registered_device *dev;
2374 struct net_device *netdev;
2375 u8 mac_addr[ETH_ALEN];
2376 int sta_idx = cb->args[1];
2377 int err;
2379 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2380 if (err)
2381 return err;
2383 if (!dev->ops->dump_station) {
2384 err = -EOPNOTSUPP;
2385 goto out_err;
2388 while (1) {
2389 memset(&sinfo, 0, sizeof(sinfo));
2390 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2391 mac_addr, &sinfo);
2392 if (err == -ENOENT)
2393 break;
2394 if (err)
2395 goto out_err;
2397 if (nl80211_send_station(skb,
2398 NETLINK_CB(cb->skb).pid,
2399 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2400 netdev, mac_addr,
2401 &sinfo) < 0)
2402 goto out;
2404 sta_idx++;
2408 out:
2409 cb->args[1] = sta_idx;
2410 err = skb->len;
2411 out_err:
2412 nl80211_finish_netdev_dump(dev);
2414 return err;
2417 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2420 struct net_device *dev = info->user_ptr[1];
2421 struct station_info sinfo;
2422 struct sk_buff *msg;
2423 u8 *mac_addr = NULL;
2424 int err;
2426 memset(&sinfo, 0, sizeof(sinfo));
2428 if (!info->attrs[NL80211_ATTR_MAC])
2429 return -EINVAL;
2431 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2433 if (!rdev->ops->get_station)
2434 return -EOPNOTSUPP;
2436 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2437 if (err)
2438 return err;
2440 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2441 if (!msg)
2442 return -ENOMEM;
2444 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2445 dev, mac_addr, &sinfo) < 0) {
2446 nlmsg_free(msg);
2447 return -ENOBUFS;
2450 return genlmsg_reply(msg, info);
2454 * Get vlan interface making sure it is running and on the right wiphy.
2456 static int get_vlan(struct genl_info *info,
2457 struct cfg80211_registered_device *rdev,
2458 struct net_device **vlan)
2460 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2461 *vlan = NULL;
2463 if (vlanattr) {
2464 *vlan = dev_get_by_index(genl_info_net(info),
2465 nla_get_u32(vlanattr));
2466 if (!*vlan)
2467 return -ENODEV;
2468 if (!(*vlan)->ieee80211_ptr)
2469 return -EINVAL;
2470 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2471 return -EINVAL;
2472 if (!netif_running(*vlan))
2473 return -ENETDOWN;
2475 return 0;
2478 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2481 int err;
2482 struct net_device *dev = info->user_ptr[1];
2483 struct station_parameters params;
2484 u8 *mac_addr = NULL;
2486 memset(&params, 0, sizeof(params));
2488 params.listen_interval = -1;
2489 params.plink_state = -1;
2491 if (info->attrs[NL80211_ATTR_STA_AID])
2492 return -EINVAL;
2494 if (!info->attrs[NL80211_ATTR_MAC])
2495 return -EINVAL;
2497 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2499 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2500 params.supported_rates =
2501 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2502 params.supported_rates_len =
2503 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2506 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2507 params.listen_interval =
2508 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2510 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2511 params.ht_capa =
2512 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2514 if (parse_station_flags(info, &params))
2515 return -EINVAL;
2517 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2518 params.plink_action =
2519 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2521 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2522 params.plink_state =
2523 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2525 err = get_vlan(info, rdev, &params.vlan);
2526 if (err)
2527 goto out;
2529 /* validate settings */
2530 err = 0;
2532 switch (dev->ieee80211_ptr->iftype) {
2533 case NL80211_IFTYPE_AP:
2534 case NL80211_IFTYPE_AP_VLAN:
2535 case NL80211_IFTYPE_P2P_GO:
2536 /* disallow mesh-specific things */
2537 if (params.plink_action)
2538 err = -EINVAL;
2539 break;
2540 case NL80211_IFTYPE_P2P_CLIENT:
2541 case NL80211_IFTYPE_STATION:
2542 /* disallow things sta doesn't support */
2543 if (params.plink_action)
2544 err = -EINVAL;
2545 if (params.vlan)
2546 err = -EINVAL;
2547 if (params.supported_rates &&
2548 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2549 err = -EINVAL;
2550 if (params.ht_capa)
2551 err = -EINVAL;
2552 if (params.listen_interval >= 0)
2553 err = -EINVAL;
2554 if (params.sta_flags_mask &
2555 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2556 BIT(NL80211_STA_FLAG_TDLS_PEER)))
2557 err = -EINVAL;
2558 /* can't change the TDLS bit */
2559 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2560 (params.sta_flags_mask & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2561 err = -EINVAL;
2562 break;
2563 case NL80211_IFTYPE_MESH_POINT:
2564 /* disallow things mesh doesn't support */
2565 if (params.vlan)
2566 err = -EINVAL;
2567 if (params.ht_capa)
2568 err = -EINVAL;
2569 if (params.listen_interval >= 0)
2570 err = -EINVAL;
2571 if (params.sta_flags_mask &
2572 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2573 BIT(NL80211_STA_FLAG_MFP) |
2574 BIT(NL80211_STA_FLAG_AUTHORIZED)))
2575 err = -EINVAL;
2576 break;
2577 default:
2578 err = -EINVAL;
2581 if (err)
2582 goto out;
2584 if (!rdev->ops->change_station) {
2585 err = -EOPNOTSUPP;
2586 goto out;
2589 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2591 out:
2592 if (params.vlan)
2593 dev_put(params.vlan);
2595 return err;
2598 static struct nla_policy
2599 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2600 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2601 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2604 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2606 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2607 int err;
2608 struct net_device *dev = info->user_ptr[1];
2609 struct station_parameters params;
2610 u8 *mac_addr = NULL;
2612 memset(&params, 0, sizeof(params));
2614 if (!info->attrs[NL80211_ATTR_MAC])
2615 return -EINVAL;
2617 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2618 return -EINVAL;
2620 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2621 return -EINVAL;
2623 if (!info->attrs[NL80211_ATTR_STA_AID])
2624 return -EINVAL;
2626 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2627 params.supported_rates =
2628 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2629 params.supported_rates_len =
2630 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2631 params.listen_interval =
2632 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2634 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2635 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2636 return -EINVAL;
2638 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2639 params.ht_capa =
2640 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2642 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2643 params.plink_action =
2644 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2646 if (parse_station_flags(info, &params))
2647 return -EINVAL;
2649 /* parse WME attributes if sta is WME capable */
2650 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2651 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2652 info->attrs[NL80211_ATTR_STA_WME]) {
2653 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2654 struct nlattr *nla;
2656 nla = info->attrs[NL80211_ATTR_STA_WME];
2657 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2658 nl80211_sta_wme_policy);
2659 if (err)
2660 return err;
2662 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2663 params.uapsd_queues =
2664 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2665 if (params.uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2666 return -EINVAL;
2668 if (tb[NL80211_STA_WME_MAX_SP])
2669 params.max_sp =
2670 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2672 if (params.max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2673 return -EINVAL;
2675 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2678 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2679 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2680 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2681 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO &&
2682 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
2683 return -EINVAL;
2686 * Only managed stations can add TDLS peers, and only when the
2687 * wiphy supports external TDLS setup.
2689 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION &&
2690 !((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
2691 (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2692 (rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)))
2693 return -EINVAL;
2695 err = get_vlan(info, rdev, &params.vlan);
2696 if (err)
2697 goto out;
2699 /* validate settings */
2700 err = 0;
2702 if (!rdev->ops->add_station) {
2703 err = -EOPNOTSUPP;
2704 goto out;
2707 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2709 out:
2710 if (params.vlan)
2711 dev_put(params.vlan);
2712 return err;
2715 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2718 struct net_device *dev = info->user_ptr[1];
2719 u8 *mac_addr = NULL;
2721 if (info->attrs[NL80211_ATTR_MAC])
2722 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2724 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2725 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2726 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2727 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2728 return -EINVAL;
2730 if (!rdev->ops->del_station)
2731 return -EOPNOTSUPP;
2733 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2736 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2737 int flags, struct net_device *dev,
2738 u8 *dst, u8 *next_hop,
2739 struct mpath_info *pinfo)
2741 void *hdr;
2742 struct nlattr *pinfoattr;
2744 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2745 if (!hdr)
2746 return -1;
2748 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2749 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2750 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2752 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2754 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2755 if (!pinfoattr)
2756 goto nla_put_failure;
2757 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2758 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2759 pinfo->frame_qlen);
2760 if (pinfo->filled & MPATH_INFO_SN)
2761 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2762 pinfo->sn);
2763 if (pinfo->filled & MPATH_INFO_METRIC)
2764 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2765 pinfo->metric);
2766 if (pinfo->filled & MPATH_INFO_EXPTIME)
2767 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2768 pinfo->exptime);
2769 if (pinfo->filled & MPATH_INFO_FLAGS)
2770 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2771 pinfo->flags);
2772 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2773 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2774 pinfo->discovery_timeout);
2775 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2776 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2777 pinfo->discovery_retries);
2779 nla_nest_end(msg, pinfoattr);
2781 return genlmsg_end(msg, hdr);
2783 nla_put_failure:
2784 genlmsg_cancel(msg, hdr);
2785 return -EMSGSIZE;
2788 static int nl80211_dump_mpath(struct sk_buff *skb,
2789 struct netlink_callback *cb)
2791 struct mpath_info pinfo;
2792 struct cfg80211_registered_device *dev;
2793 struct net_device *netdev;
2794 u8 dst[ETH_ALEN];
2795 u8 next_hop[ETH_ALEN];
2796 int path_idx = cb->args[1];
2797 int err;
2799 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2800 if (err)
2801 return err;
2803 if (!dev->ops->dump_mpath) {
2804 err = -EOPNOTSUPP;
2805 goto out_err;
2808 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2809 err = -EOPNOTSUPP;
2810 goto out_err;
2813 while (1) {
2814 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2815 dst, next_hop, &pinfo);
2816 if (err == -ENOENT)
2817 break;
2818 if (err)
2819 goto out_err;
2821 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2822 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2823 netdev, dst, next_hop,
2824 &pinfo) < 0)
2825 goto out;
2827 path_idx++;
2831 out:
2832 cb->args[1] = path_idx;
2833 err = skb->len;
2834 out_err:
2835 nl80211_finish_netdev_dump(dev);
2836 return err;
2839 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2841 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2842 int err;
2843 struct net_device *dev = info->user_ptr[1];
2844 struct mpath_info pinfo;
2845 struct sk_buff *msg;
2846 u8 *dst = NULL;
2847 u8 next_hop[ETH_ALEN];
2849 memset(&pinfo, 0, sizeof(pinfo));
2851 if (!info->attrs[NL80211_ATTR_MAC])
2852 return -EINVAL;
2854 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2856 if (!rdev->ops->get_mpath)
2857 return -EOPNOTSUPP;
2859 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2860 return -EOPNOTSUPP;
2862 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2863 if (err)
2864 return err;
2866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2867 if (!msg)
2868 return -ENOMEM;
2870 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2871 dev, dst, next_hop, &pinfo) < 0) {
2872 nlmsg_free(msg);
2873 return -ENOBUFS;
2876 return genlmsg_reply(msg, info);
2879 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2881 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2882 struct net_device *dev = info->user_ptr[1];
2883 u8 *dst = NULL;
2884 u8 *next_hop = NULL;
2886 if (!info->attrs[NL80211_ATTR_MAC])
2887 return -EINVAL;
2889 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2890 return -EINVAL;
2892 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2893 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2895 if (!rdev->ops->change_mpath)
2896 return -EOPNOTSUPP;
2898 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2899 return -EOPNOTSUPP;
2901 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2904 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2907 struct net_device *dev = info->user_ptr[1];
2908 u8 *dst = NULL;
2909 u8 *next_hop = NULL;
2911 if (!info->attrs[NL80211_ATTR_MAC])
2912 return -EINVAL;
2914 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2915 return -EINVAL;
2917 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2918 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2920 if (!rdev->ops->add_mpath)
2921 return -EOPNOTSUPP;
2923 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2924 return -EOPNOTSUPP;
2926 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2929 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2932 struct net_device *dev = info->user_ptr[1];
2933 u8 *dst = NULL;
2935 if (info->attrs[NL80211_ATTR_MAC])
2936 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2938 if (!rdev->ops->del_mpath)
2939 return -EOPNOTSUPP;
2941 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2944 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2946 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2947 struct net_device *dev = info->user_ptr[1];
2948 struct bss_parameters params;
2950 memset(&params, 0, sizeof(params));
2951 /* default to not changing parameters */
2952 params.use_cts_prot = -1;
2953 params.use_short_preamble = -1;
2954 params.use_short_slot_time = -1;
2955 params.ap_isolate = -1;
2956 params.ht_opmode = -1;
2958 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2959 params.use_cts_prot =
2960 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2961 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2962 params.use_short_preamble =
2963 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2964 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2965 params.use_short_slot_time =
2966 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2967 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2968 params.basic_rates =
2969 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2970 params.basic_rates_len =
2971 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2973 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2974 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2975 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2976 params.ht_opmode =
2977 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
2979 if (!rdev->ops->change_bss)
2980 return -EOPNOTSUPP;
2982 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2983 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2984 return -EOPNOTSUPP;
2986 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2989 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2990 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2991 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2992 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2993 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2994 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2995 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2998 static int parse_reg_rule(struct nlattr *tb[],
2999 struct ieee80211_reg_rule *reg_rule)
3001 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3002 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3004 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3005 return -EINVAL;
3006 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3007 return -EINVAL;
3008 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3009 return -EINVAL;
3010 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3011 return -EINVAL;
3012 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3013 return -EINVAL;
3015 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3017 freq_range->start_freq_khz =
3018 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3019 freq_range->end_freq_khz =
3020 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3021 freq_range->max_bandwidth_khz =
3022 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3024 power_rule->max_eirp =
3025 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3027 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3028 power_rule->max_antenna_gain =
3029 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3031 return 0;
3034 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3036 int r;
3037 char *data = NULL;
3040 * You should only get this when cfg80211 hasn't yet initialized
3041 * completely when built-in to the kernel right between the time
3042 * window between nl80211_init() and regulatory_init(), if that is
3043 * even possible.
3045 mutex_lock(&cfg80211_mutex);
3046 if (unlikely(!cfg80211_regdomain)) {
3047 mutex_unlock(&cfg80211_mutex);
3048 return -EINPROGRESS;
3050 mutex_unlock(&cfg80211_mutex);
3052 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3053 return -EINVAL;
3055 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3057 r = regulatory_hint_user(data);
3059 return r;
3062 static int nl80211_get_mesh_config(struct sk_buff *skb,
3063 struct genl_info *info)
3065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3066 struct net_device *dev = info->user_ptr[1];
3067 struct wireless_dev *wdev = dev->ieee80211_ptr;
3068 struct mesh_config cur_params;
3069 int err = 0;
3070 void *hdr;
3071 struct nlattr *pinfoattr;
3072 struct sk_buff *msg;
3074 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3075 return -EOPNOTSUPP;
3077 if (!rdev->ops->get_mesh_config)
3078 return -EOPNOTSUPP;
3080 wdev_lock(wdev);
3081 /* If not connected, get default parameters */
3082 if (!wdev->mesh_id_len)
3083 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3084 else
3085 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
3086 &cur_params);
3087 wdev_unlock(wdev);
3089 if (err)
3090 return err;
3092 /* Draw up a netlink message to send back */
3093 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3094 if (!msg)
3095 return -ENOMEM;
3096 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3097 NL80211_CMD_GET_MESH_CONFIG);
3098 if (!hdr)
3099 goto out;
3100 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
3101 if (!pinfoattr)
3102 goto nla_put_failure;
3103 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3104 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3105 cur_params.dot11MeshRetryTimeout);
3106 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3107 cur_params.dot11MeshConfirmTimeout);
3108 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3109 cur_params.dot11MeshHoldingTimeout);
3110 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3111 cur_params.dot11MeshMaxPeerLinks);
3112 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3113 cur_params.dot11MeshMaxRetries);
3114 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3115 cur_params.dot11MeshTTL);
3116 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3117 cur_params.element_ttl);
3118 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3119 cur_params.auto_open_plinks);
3120 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3121 cur_params.dot11MeshHWMPmaxPREQretries);
3122 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3123 cur_params.path_refresh_time);
3124 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3125 cur_params.min_discovery_timeout);
3126 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3127 cur_params.dot11MeshHWMPactivePathTimeout);
3128 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3129 cur_params.dot11MeshHWMPpreqMinInterval);
3130 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3131 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
3132 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3133 cur_params.dot11MeshHWMPRootMode);
3134 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3135 cur_params.dot11MeshHWMPRannInterval);
3136 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3137 cur_params.dot11MeshGateAnnouncementProtocol);
3138 nla_nest_end(msg, pinfoattr);
3139 genlmsg_end(msg, hdr);
3140 return genlmsg_reply(msg, info);
3142 nla_put_failure:
3143 genlmsg_cancel(msg, hdr);
3144 out:
3145 nlmsg_free(msg);
3146 return -ENOBUFS;
3149 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
3150 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3151 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3152 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3153 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3154 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3155 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
3156 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
3157 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3159 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3160 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3161 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3162 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3163 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3164 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3165 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
3166 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
3167 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
3170 static const struct nla_policy
3171 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3172 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3173 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
3174 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
3175 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
3176 .len = IEEE80211_MAX_DATA_LEN },
3177 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
3180 static int nl80211_parse_mesh_config(struct genl_info *info,
3181 struct mesh_config *cfg,
3182 u32 *mask_out)
3184 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
3185 u32 mask = 0;
3187 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3188 do {\
3189 if (table[attr_num]) {\
3190 cfg->param = nla_fn(table[attr_num]); \
3191 mask |= (1 << (attr_num - 1)); \
3193 } while (0);\
3196 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
3197 return -EINVAL;
3198 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
3199 info->attrs[NL80211_ATTR_MESH_CONFIG],
3200 nl80211_meshconf_params_policy))
3201 return -EINVAL;
3203 /* This makes sure that there aren't more than 32 mesh config
3204 * parameters (otherwise our bitfield scheme would not work.) */
3205 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3207 /* Fill in the params struct */
3208 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3209 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3210 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3211 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3212 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3213 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3214 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3215 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3216 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3217 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3218 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3219 mask, NL80211_MESHCONF_TTL, nla_get_u8);
3220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3221 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
3222 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3223 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3225 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3226 nla_get_u8);
3227 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3228 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3229 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3230 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3231 nla_get_u16);
3232 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3233 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3234 nla_get_u32);
3235 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3236 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3237 nla_get_u16);
3238 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3239 dot11MeshHWMPnetDiameterTraversalTime,
3240 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3241 nla_get_u16);
3242 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3243 dot11MeshHWMPRootMode, mask,
3244 NL80211_MESHCONF_HWMP_ROOTMODE,
3245 nla_get_u8);
3246 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3247 dot11MeshHWMPRannInterval, mask,
3248 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3249 nla_get_u16);
3250 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3251 dot11MeshGateAnnouncementProtocol, mask,
3252 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3253 nla_get_u8);
3254 if (mask_out)
3255 *mask_out = mask;
3257 return 0;
3259 #undef FILL_IN_MESH_PARAM_IF_SET
3262 static int nl80211_parse_mesh_setup(struct genl_info *info,
3263 struct mesh_setup *setup)
3265 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3267 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3268 return -EINVAL;
3269 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3270 info->attrs[NL80211_ATTR_MESH_SETUP],
3271 nl80211_mesh_setup_params_policy))
3272 return -EINVAL;
3274 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3275 setup->path_sel_proto =
3276 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3277 IEEE80211_PATH_PROTOCOL_VENDOR :
3278 IEEE80211_PATH_PROTOCOL_HWMP;
3280 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3281 setup->path_metric =
3282 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3283 IEEE80211_PATH_METRIC_VENDOR :
3284 IEEE80211_PATH_METRIC_AIRTIME;
3287 if (tb[NL80211_MESH_SETUP_IE]) {
3288 struct nlattr *ieattr =
3289 tb[NL80211_MESH_SETUP_IE];
3290 if (!is_valid_ie_attr(ieattr))
3291 return -EINVAL;
3292 setup->ie = nla_data(ieattr);
3293 setup->ie_len = nla_len(ieattr);
3295 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3296 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3298 return 0;
3301 static int nl80211_update_mesh_config(struct sk_buff *skb,
3302 struct genl_info *info)
3304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3305 struct net_device *dev = info->user_ptr[1];
3306 struct wireless_dev *wdev = dev->ieee80211_ptr;
3307 struct mesh_config cfg;
3308 u32 mask;
3309 int err;
3311 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3312 return -EOPNOTSUPP;
3314 if (!rdev->ops->update_mesh_config)
3315 return -EOPNOTSUPP;
3317 err = nl80211_parse_mesh_config(info, &cfg, &mask);
3318 if (err)
3319 return err;
3321 wdev_lock(wdev);
3322 if (!wdev->mesh_id_len)
3323 err = -ENOLINK;
3325 if (!err)
3326 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3327 mask, &cfg);
3329 wdev_unlock(wdev);
3331 return err;
3334 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3336 struct sk_buff *msg;
3337 void *hdr = NULL;
3338 struct nlattr *nl_reg_rules;
3339 unsigned int i;
3340 int err = -EINVAL;
3342 mutex_lock(&cfg80211_mutex);
3344 if (!cfg80211_regdomain)
3345 goto out;
3347 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3348 if (!msg) {
3349 err = -ENOBUFS;
3350 goto out;
3353 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3354 NL80211_CMD_GET_REG);
3355 if (!hdr)
3356 goto put_failure;
3358 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3359 cfg80211_regdomain->alpha2);
3361 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3362 if (!nl_reg_rules)
3363 goto nla_put_failure;
3365 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3366 struct nlattr *nl_reg_rule;
3367 const struct ieee80211_reg_rule *reg_rule;
3368 const struct ieee80211_freq_range *freq_range;
3369 const struct ieee80211_power_rule *power_rule;
3371 reg_rule = &cfg80211_regdomain->reg_rules[i];
3372 freq_range = &reg_rule->freq_range;
3373 power_rule = &reg_rule->power_rule;
3375 nl_reg_rule = nla_nest_start(msg, i);
3376 if (!nl_reg_rule)
3377 goto nla_put_failure;
3379 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3380 reg_rule->flags);
3381 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3382 freq_range->start_freq_khz);
3383 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3384 freq_range->end_freq_khz);
3385 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3386 freq_range->max_bandwidth_khz);
3387 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3388 power_rule->max_antenna_gain);
3389 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3390 power_rule->max_eirp);
3392 nla_nest_end(msg, nl_reg_rule);
3395 nla_nest_end(msg, nl_reg_rules);
3397 genlmsg_end(msg, hdr);
3398 err = genlmsg_reply(msg, info);
3399 goto out;
3401 nla_put_failure:
3402 genlmsg_cancel(msg, hdr);
3403 put_failure:
3404 nlmsg_free(msg);
3405 err = -EMSGSIZE;
3406 out:
3407 mutex_unlock(&cfg80211_mutex);
3408 return err;
3411 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3413 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3414 struct nlattr *nl_reg_rule;
3415 char *alpha2 = NULL;
3416 int rem_reg_rules = 0, r = 0;
3417 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3418 struct ieee80211_regdomain *rd = NULL;
3420 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3421 return -EINVAL;
3423 if (!info->attrs[NL80211_ATTR_REG_RULES])
3424 return -EINVAL;
3426 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3428 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3429 rem_reg_rules) {
3430 num_rules++;
3431 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3432 return -EINVAL;
3435 mutex_lock(&cfg80211_mutex);
3437 if (!reg_is_valid_request(alpha2)) {
3438 r = -EINVAL;
3439 goto bad_reg;
3442 size_of_regd = sizeof(struct ieee80211_regdomain) +
3443 (num_rules * sizeof(struct ieee80211_reg_rule));
3445 rd = kzalloc(size_of_regd, GFP_KERNEL);
3446 if (!rd) {
3447 r = -ENOMEM;
3448 goto bad_reg;
3451 rd->n_reg_rules = num_rules;
3452 rd->alpha2[0] = alpha2[0];
3453 rd->alpha2[1] = alpha2[1];
3455 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3456 rem_reg_rules) {
3457 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3458 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3459 reg_rule_policy);
3460 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3461 if (r)
3462 goto bad_reg;
3464 rule_idx++;
3466 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3467 r = -EINVAL;
3468 goto bad_reg;
3472 BUG_ON(rule_idx != num_rules);
3474 r = set_regdom(rd);
3476 mutex_unlock(&cfg80211_mutex);
3478 return r;
3480 bad_reg:
3481 mutex_unlock(&cfg80211_mutex);
3482 kfree(rd);
3483 return r;
3486 static int validate_scan_freqs(struct nlattr *freqs)
3488 struct nlattr *attr1, *attr2;
3489 int n_channels = 0, tmp1, tmp2;
3491 nla_for_each_nested(attr1, freqs, tmp1) {
3492 n_channels++;
3494 * Some hardware has a limited channel list for
3495 * scanning, and it is pretty much nonsensical
3496 * to scan for a channel twice, so disallow that
3497 * and don't require drivers to check that the
3498 * channel list they get isn't longer than what
3499 * they can scan, as long as they can scan all
3500 * the channels they registered at once.
3502 nla_for_each_nested(attr2, freqs, tmp2)
3503 if (attr1 != attr2 &&
3504 nla_get_u32(attr1) == nla_get_u32(attr2))
3505 return 0;
3508 return n_channels;
3511 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3514 struct net_device *dev = info->user_ptr[1];
3515 struct cfg80211_scan_request *request;
3516 struct nlattr *attr;
3517 struct wiphy *wiphy;
3518 int err, tmp, n_ssids = 0, n_channels, i;
3519 size_t ie_len;
3521 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3522 return -EINVAL;
3524 wiphy = &rdev->wiphy;
3526 if (!rdev->ops->scan)
3527 return -EOPNOTSUPP;
3529 if (rdev->scan_req)
3530 return -EBUSY;
3532 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3533 n_channels = validate_scan_freqs(
3534 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3535 if (!n_channels)
3536 return -EINVAL;
3537 } else {
3538 enum ieee80211_band band;
3539 n_channels = 0;
3541 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3542 if (wiphy->bands[band])
3543 n_channels += wiphy->bands[band]->n_channels;
3546 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3547 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3548 n_ssids++;
3550 if (n_ssids > wiphy->max_scan_ssids)
3551 return -EINVAL;
3553 if (info->attrs[NL80211_ATTR_IE])
3554 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3555 else
3556 ie_len = 0;
3558 if (ie_len > wiphy->max_scan_ie_len)
3559 return -EINVAL;
3561 request = kzalloc(sizeof(*request)
3562 + sizeof(*request->ssids) * n_ssids
3563 + sizeof(*request->channels) * n_channels
3564 + ie_len, GFP_KERNEL);
3565 if (!request)
3566 return -ENOMEM;
3568 if (n_ssids)
3569 request->ssids = (void *)&request->channels[n_channels];
3570 request->n_ssids = n_ssids;
3571 if (ie_len) {
3572 if (request->ssids)
3573 request->ie = (void *)(request->ssids + n_ssids);
3574 else
3575 request->ie = (void *)(request->channels + n_channels);
3578 i = 0;
3579 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3580 /* user specified, bail out if channel not found */
3581 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3582 struct ieee80211_channel *chan;
3584 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3586 if (!chan) {
3587 err = -EINVAL;
3588 goto out_free;
3591 /* ignore disabled channels */
3592 if (chan->flags & IEEE80211_CHAN_DISABLED)
3593 continue;
3595 request->channels[i] = chan;
3596 i++;
3598 } else {
3599 enum ieee80211_band band;
3601 /* all channels */
3602 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3603 int j;
3604 if (!wiphy->bands[band])
3605 continue;
3606 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3607 struct ieee80211_channel *chan;
3609 chan = &wiphy->bands[band]->channels[j];
3611 if (chan->flags & IEEE80211_CHAN_DISABLED)
3612 continue;
3614 request->channels[i] = chan;
3615 i++;
3620 if (!i) {
3621 err = -EINVAL;
3622 goto out_free;
3625 request->n_channels = i;
3627 i = 0;
3628 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3629 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3630 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3631 err = -EINVAL;
3632 goto out_free;
3634 request->ssids[i].ssid_len = nla_len(attr);
3635 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3636 i++;
3640 if (info->attrs[NL80211_ATTR_IE]) {
3641 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3642 memcpy((void *)request->ie,
3643 nla_data(info->attrs[NL80211_ATTR_IE]),
3644 request->ie_len);
3647 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
3648 if (wiphy->bands[i])
3649 request->rates[i] =
3650 (1 << wiphy->bands[i]->n_bitrates) - 1;
3652 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3653 nla_for_each_nested(attr,
3654 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3655 tmp) {
3656 enum ieee80211_band band = nla_type(attr);
3658 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
3659 err = -EINVAL;
3660 goto out_free;
3662 err = ieee80211_get_ratemask(wiphy->bands[band],
3663 nla_data(attr),
3664 nla_len(attr),
3665 &request->rates[band]);
3666 if (err)
3667 goto out_free;
3671 request->no_cck =
3672 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3674 request->dev = dev;
3675 request->wiphy = &rdev->wiphy;
3677 rdev->scan_req = request;
3678 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3680 if (!err) {
3681 nl80211_send_scan_start(rdev, dev);
3682 dev_hold(dev);
3683 } else {
3684 out_free:
3685 rdev->scan_req = NULL;
3686 kfree(request);
3689 return err;
3692 static int nl80211_start_sched_scan(struct sk_buff *skb,
3693 struct genl_info *info)
3695 struct cfg80211_sched_scan_request *request;
3696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3697 struct net_device *dev = info->user_ptr[1];
3698 struct nlattr *attr;
3699 struct wiphy *wiphy;
3700 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
3701 u32 interval;
3702 enum ieee80211_band band;
3703 size_t ie_len;
3704 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
3706 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3707 !rdev->ops->sched_scan_start)
3708 return -EOPNOTSUPP;
3710 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3711 return -EINVAL;
3713 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3714 return -EINVAL;
3716 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3717 if (interval == 0)
3718 return -EINVAL;
3720 wiphy = &rdev->wiphy;
3722 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3723 n_channels = validate_scan_freqs(
3724 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3725 if (!n_channels)
3726 return -EINVAL;
3727 } else {
3728 n_channels = 0;
3730 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3731 if (wiphy->bands[band])
3732 n_channels += wiphy->bands[band]->n_channels;
3735 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3736 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3737 tmp)
3738 n_ssids++;
3740 if (n_ssids > wiphy->max_sched_scan_ssids)
3741 return -EINVAL;
3743 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3744 nla_for_each_nested(attr,
3745 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3746 tmp)
3747 n_match_sets++;
3749 if (n_match_sets > wiphy->max_match_sets)
3750 return -EINVAL;
3752 if (info->attrs[NL80211_ATTR_IE])
3753 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3754 else
3755 ie_len = 0;
3757 if (ie_len > wiphy->max_sched_scan_ie_len)
3758 return -EINVAL;
3760 mutex_lock(&rdev->sched_scan_mtx);
3762 if (rdev->sched_scan_req) {
3763 err = -EINPROGRESS;
3764 goto out;
3767 request = kzalloc(sizeof(*request)
3768 + sizeof(*request->ssids) * n_ssids
3769 + sizeof(*request->match_sets) * n_match_sets
3770 + sizeof(*request->channels) * n_channels
3771 + ie_len, GFP_KERNEL);
3772 if (!request) {
3773 err = -ENOMEM;
3774 goto out;
3777 if (n_ssids)
3778 request->ssids = (void *)&request->channels[n_channels];
3779 request->n_ssids = n_ssids;
3780 if (ie_len) {
3781 if (request->ssids)
3782 request->ie = (void *)(request->ssids + n_ssids);
3783 else
3784 request->ie = (void *)(request->channels + n_channels);
3787 if (n_match_sets) {
3788 if (request->ie)
3789 request->match_sets = (void *)(request->ie + ie_len);
3790 else if (request->ssids)
3791 request->match_sets =
3792 (void *)(request->ssids + n_ssids);
3793 else
3794 request->match_sets =
3795 (void *)(request->channels + n_channels);
3797 request->n_match_sets = n_match_sets;
3799 i = 0;
3800 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3801 /* user specified, bail out if channel not found */
3802 nla_for_each_nested(attr,
3803 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3804 tmp) {
3805 struct ieee80211_channel *chan;
3807 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3809 if (!chan) {
3810 err = -EINVAL;
3811 goto out_free;
3814 /* ignore disabled channels */
3815 if (chan->flags & IEEE80211_CHAN_DISABLED)
3816 continue;
3818 request->channels[i] = chan;
3819 i++;
3821 } else {
3822 /* all channels */
3823 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3824 int j;
3825 if (!wiphy->bands[band])
3826 continue;
3827 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3828 struct ieee80211_channel *chan;
3830 chan = &wiphy->bands[band]->channels[j];
3832 if (chan->flags & IEEE80211_CHAN_DISABLED)
3833 continue;
3835 request->channels[i] = chan;
3836 i++;
3841 if (!i) {
3842 err = -EINVAL;
3843 goto out_free;
3846 request->n_channels = i;
3848 i = 0;
3849 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3850 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3851 tmp) {
3852 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3853 err = -EINVAL;
3854 goto out_free;
3856 request->ssids[i].ssid_len = nla_len(attr);
3857 memcpy(request->ssids[i].ssid, nla_data(attr),
3858 nla_len(attr));
3859 i++;
3863 i = 0;
3864 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
3865 nla_for_each_nested(attr,
3866 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3867 tmp) {
3868 struct nlattr *ssid;
3870 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
3871 nla_data(attr), nla_len(attr),
3872 nl80211_match_policy);
3873 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
3874 if (ssid) {
3875 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
3876 err = -EINVAL;
3877 goto out_free;
3879 memcpy(request->match_sets[i].ssid.ssid,
3880 nla_data(ssid), nla_len(ssid));
3881 request->match_sets[i].ssid.ssid_len =
3882 nla_len(ssid);
3884 i++;
3888 if (info->attrs[NL80211_ATTR_IE]) {
3889 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3890 memcpy((void *)request->ie,
3891 nla_data(info->attrs[NL80211_ATTR_IE]),
3892 request->ie_len);
3895 request->dev = dev;
3896 request->wiphy = &rdev->wiphy;
3897 request->interval = interval;
3899 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3900 if (!err) {
3901 rdev->sched_scan_req = request;
3902 nl80211_send_sched_scan(rdev, dev,
3903 NL80211_CMD_START_SCHED_SCAN);
3904 goto out;
3907 out_free:
3908 kfree(request);
3909 out:
3910 mutex_unlock(&rdev->sched_scan_mtx);
3911 return err;
3914 static int nl80211_stop_sched_scan(struct sk_buff *skb,
3915 struct genl_info *info)
3917 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3918 int err;
3920 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3921 !rdev->ops->sched_scan_stop)
3922 return -EOPNOTSUPP;
3924 mutex_lock(&rdev->sched_scan_mtx);
3925 err = __cfg80211_stop_sched_scan(rdev, false);
3926 mutex_unlock(&rdev->sched_scan_mtx);
3928 return err;
3931 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3932 u32 seq, int flags,
3933 struct cfg80211_registered_device *rdev,
3934 struct wireless_dev *wdev,
3935 struct cfg80211_internal_bss *intbss)
3937 struct cfg80211_bss *res = &intbss->pub;
3938 void *hdr;
3939 struct nlattr *bss;
3940 int i;
3942 ASSERT_WDEV_LOCK(wdev);
3944 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
3945 NL80211_CMD_NEW_SCAN_RESULTS);
3946 if (!hdr)
3947 return -1;
3949 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3951 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3952 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3954 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3955 if (!bss)
3956 goto nla_put_failure;
3957 if (!is_zero_ether_addr(res->bssid))
3958 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3959 if (res->information_elements && res->len_information_elements)
3960 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3961 res->len_information_elements,
3962 res->information_elements);
3963 if (res->beacon_ies && res->len_beacon_ies &&
3964 res->beacon_ies != res->information_elements)
3965 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3966 res->len_beacon_ies, res->beacon_ies);
3967 if (res->tsf)
3968 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3969 if (res->beacon_interval)
3970 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3971 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3972 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3973 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3974 jiffies_to_msecs(jiffies - intbss->ts));
3976 switch (rdev->wiphy.signal_type) {
3977 case CFG80211_SIGNAL_TYPE_MBM:
3978 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3979 break;
3980 case CFG80211_SIGNAL_TYPE_UNSPEC:
3981 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3982 break;
3983 default:
3984 break;
3987 switch (wdev->iftype) {
3988 case NL80211_IFTYPE_P2P_CLIENT:
3989 case NL80211_IFTYPE_STATION:
3990 if (intbss == wdev->current_bss)
3991 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3992 NL80211_BSS_STATUS_ASSOCIATED);
3993 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3994 if (intbss != wdev->auth_bsses[i])
3995 continue;
3996 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3997 NL80211_BSS_STATUS_AUTHENTICATED);
3998 break;
4000 break;
4001 case NL80211_IFTYPE_ADHOC:
4002 if (intbss == wdev->current_bss)
4003 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4004 NL80211_BSS_STATUS_IBSS_JOINED);
4005 break;
4006 default:
4007 break;
4010 nla_nest_end(msg, bss);
4012 return genlmsg_end(msg, hdr);
4014 nla_put_failure:
4015 genlmsg_cancel(msg, hdr);
4016 return -EMSGSIZE;
4019 static int nl80211_dump_scan(struct sk_buff *skb,
4020 struct netlink_callback *cb)
4022 struct cfg80211_registered_device *rdev;
4023 struct net_device *dev;
4024 struct cfg80211_internal_bss *scan;
4025 struct wireless_dev *wdev;
4026 int start = cb->args[1], idx = 0;
4027 int err;
4029 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4030 if (err)
4031 return err;
4033 wdev = dev->ieee80211_ptr;
4035 wdev_lock(wdev);
4036 spin_lock_bh(&rdev->bss_lock);
4037 cfg80211_bss_expire(rdev);
4039 cb->seq = rdev->bss_generation;
4041 list_for_each_entry(scan, &rdev->bss_list, list) {
4042 if (++idx <= start)
4043 continue;
4044 if (nl80211_send_bss(skb, cb,
4045 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4046 rdev, wdev, scan) < 0) {
4047 idx--;
4048 break;
4052 spin_unlock_bh(&rdev->bss_lock);
4053 wdev_unlock(wdev);
4055 cb->args[1] = idx;
4056 nl80211_finish_netdev_dump(rdev);
4058 return skb->len;
4061 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4062 int flags, struct net_device *dev,
4063 struct survey_info *survey)
4065 void *hdr;
4066 struct nlattr *infoattr;
4068 hdr = nl80211hdr_put(msg, pid, seq, flags,
4069 NL80211_CMD_NEW_SURVEY_RESULTS);
4070 if (!hdr)
4071 return -ENOMEM;
4073 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4075 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4076 if (!infoattr)
4077 goto nla_put_failure;
4079 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4080 survey->channel->center_freq);
4081 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4082 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4083 survey->noise);
4084 if (survey->filled & SURVEY_INFO_IN_USE)
4085 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
4086 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4087 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4088 survey->channel_time);
4089 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4090 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4091 survey->channel_time_busy);
4092 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4093 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4094 survey->channel_time_ext_busy);
4095 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4096 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4097 survey->channel_time_rx);
4098 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4099 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4100 survey->channel_time_tx);
4102 nla_nest_end(msg, infoattr);
4104 return genlmsg_end(msg, hdr);
4106 nla_put_failure:
4107 genlmsg_cancel(msg, hdr);
4108 return -EMSGSIZE;
4111 static int nl80211_dump_survey(struct sk_buff *skb,
4112 struct netlink_callback *cb)
4114 struct survey_info survey;
4115 struct cfg80211_registered_device *dev;
4116 struct net_device *netdev;
4117 int survey_idx = cb->args[1];
4118 int res;
4120 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4121 if (res)
4122 return res;
4124 if (!dev->ops->dump_survey) {
4125 res = -EOPNOTSUPP;
4126 goto out_err;
4129 while (1) {
4130 struct ieee80211_channel *chan;
4132 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4133 &survey);
4134 if (res == -ENOENT)
4135 break;
4136 if (res)
4137 goto out_err;
4139 /* Survey without a channel doesn't make sense */
4140 if (!survey.channel) {
4141 res = -EINVAL;
4142 goto out;
4145 chan = ieee80211_get_channel(&dev->wiphy,
4146 survey.channel->center_freq);
4147 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4148 survey_idx++;
4149 continue;
4152 if (nl80211_send_survey(skb,
4153 NETLINK_CB(cb->skb).pid,
4154 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4155 netdev,
4156 &survey) < 0)
4157 goto out;
4158 survey_idx++;
4161 out:
4162 cb->args[1] = survey_idx;
4163 res = skb->len;
4164 out_err:
4165 nl80211_finish_netdev_dump(dev);
4166 return res;
4169 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4171 return auth_type <= NL80211_AUTHTYPE_MAX;
4174 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4176 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4177 NL80211_WPA_VERSION_2));
4180 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4183 struct net_device *dev = info->user_ptr[1];
4184 struct ieee80211_channel *chan;
4185 const u8 *bssid, *ssid, *ie = NULL;
4186 int err, ssid_len, ie_len = 0;
4187 enum nl80211_auth_type auth_type;
4188 struct key_parse key;
4189 bool local_state_change;
4191 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4192 return -EINVAL;
4194 if (!info->attrs[NL80211_ATTR_MAC])
4195 return -EINVAL;
4197 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4198 return -EINVAL;
4200 if (!info->attrs[NL80211_ATTR_SSID])
4201 return -EINVAL;
4203 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4204 return -EINVAL;
4206 err = nl80211_parse_key(info, &key);
4207 if (err)
4208 return err;
4210 if (key.idx >= 0) {
4211 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4212 return -EINVAL;
4213 if (!key.p.key || !key.p.key_len)
4214 return -EINVAL;
4215 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4216 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4217 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4218 key.p.key_len != WLAN_KEY_LEN_WEP104))
4219 return -EINVAL;
4220 if (key.idx > 4)
4221 return -EINVAL;
4222 } else {
4223 key.p.key_len = 0;
4224 key.p.key = NULL;
4227 if (key.idx >= 0) {
4228 int i;
4229 bool ok = false;
4230 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4231 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4232 ok = true;
4233 break;
4236 if (!ok)
4237 return -EINVAL;
4240 if (!rdev->ops->auth)
4241 return -EOPNOTSUPP;
4243 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4244 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4245 return -EOPNOTSUPP;
4247 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4248 chan = ieee80211_get_channel(&rdev->wiphy,
4249 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4250 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4251 return -EINVAL;
4253 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4254 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4256 if (info->attrs[NL80211_ATTR_IE]) {
4257 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4258 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4261 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4262 if (!nl80211_valid_auth_type(auth_type))
4263 return -EINVAL;
4265 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4267 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4268 ssid, ssid_len, ie, ie_len,
4269 key.p.key, key.p.key_len, key.idx,
4270 local_state_change);
4273 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4274 struct genl_info *info,
4275 struct cfg80211_crypto_settings *settings,
4276 int cipher_limit)
4278 memset(settings, 0, sizeof(*settings));
4280 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4282 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4283 u16 proto;
4284 proto = nla_get_u16(
4285 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4286 settings->control_port_ethertype = cpu_to_be16(proto);
4287 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4288 proto != ETH_P_PAE)
4289 return -EINVAL;
4290 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4291 settings->control_port_no_encrypt = true;
4292 } else
4293 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4295 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4296 void *data;
4297 int len, i;
4299 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4300 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4301 settings->n_ciphers_pairwise = len / sizeof(u32);
4303 if (len % sizeof(u32))
4304 return -EINVAL;
4306 if (settings->n_ciphers_pairwise > cipher_limit)
4307 return -EINVAL;
4309 memcpy(settings->ciphers_pairwise, data, len);
4311 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4312 if (!cfg80211_supported_cipher_suite(
4313 &rdev->wiphy,
4314 settings->ciphers_pairwise[i]))
4315 return -EINVAL;
4318 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4319 settings->cipher_group =
4320 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4321 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4322 settings->cipher_group))
4323 return -EINVAL;
4326 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4327 settings->wpa_versions =
4328 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4329 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4330 return -EINVAL;
4333 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4334 void *data;
4335 int len;
4337 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4338 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4339 settings->n_akm_suites = len / sizeof(u32);
4341 if (len % sizeof(u32))
4342 return -EINVAL;
4344 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4345 return -EINVAL;
4347 memcpy(settings->akm_suites, data, len);
4350 return 0;
4353 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4356 struct net_device *dev = info->user_ptr[1];
4357 struct cfg80211_crypto_settings crypto;
4358 struct ieee80211_channel *chan;
4359 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4360 int err, ssid_len, ie_len = 0;
4361 bool use_mfp = false;
4363 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4364 return -EINVAL;
4366 if (!info->attrs[NL80211_ATTR_MAC] ||
4367 !info->attrs[NL80211_ATTR_SSID] ||
4368 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4369 return -EINVAL;
4371 if (!rdev->ops->assoc)
4372 return -EOPNOTSUPP;
4374 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4375 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4376 return -EOPNOTSUPP;
4378 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4380 chan = ieee80211_get_channel(&rdev->wiphy,
4381 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4382 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4383 return -EINVAL;
4385 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4386 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4388 if (info->attrs[NL80211_ATTR_IE]) {
4389 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4390 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4393 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4394 enum nl80211_mfp mfp =
4395 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4396 if (mfp == NL80211_MFP_REQUIRED)
4397 use_mfp = true;
4398 else if (mfp != NL80211_MFP_NO)
4399 return -EINVAL;
4402 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4403 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4405 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4406 if (!err)
4407 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4408 ssid, ssid_len, ie, ie_len, use_mfp,
4409 &crypto);
4411 return err;
4414 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4417 struct net_device *dev = info->user_ptr[1];
4418 const u8 *ie = NULL, *bssid;
4419 int ie_len = 0;
4420 u16 reason_code;
4421 bool local_state_change;
4423 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4424 return -EINVAL;
4426 if (!info->attrs[NL80211_ATTR_MAC])
4427 return -EINVAL;
4429 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4430 return -EINVAL;
4432 if (!rdev->ops->deauth)
4433 return -EOPNOTSUPP;
4435 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4436 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4437 return -EOPNOTSUPP;
4439 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4441 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4442 if (reason_code == 0) {
4443 /* Reason Code 0 is reserved */
4444 return -EINVAL;
4447 if (info->attrs[NL80211_ATTR_IE]) {
4448 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4449 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4452 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4454 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4455 local_state_change);
4458 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4460 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4461 struct net_device *dev = info->user_ptr[1];
4462 const u8 *ie = NULL, *bssid;
4463 int ie_len = 0;
4464 u16 reason_code;
4465 bool local_state_change;
4467 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4468 return -EINVAL;
4470 if (!info->attrs[NL80211_ATTR_MAC])
4471 return -EINVAL;
4473 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4474 return -EINVAL;
4476 if (!rdev->ops->disassoc)
4477 return -EOPNOTSUPP;
4479 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4480 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4481 return -EOPNOTSUPP;
4483 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4485 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4486 if (reason_code == 0) {
4487 /* Reason Code 0 is reserved */
4488 return -EINVAL;
4491 if (info->attrs[NL80211_ATTR_IE]) {
4492 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4493 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4496 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4498 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4499 local_state_change);
4502 static bool
4503 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4504 int mcast_rate[IEEE80211_NUM_BANDS],
4505 int rateval)
4507 struct wiphy *wiphy = &rdev->wiphy;
4508 bool found = false;
4509 int band, i;
4511 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4512 struct ieee80211_supported_band *sband;
4514 sband = wiphy->bands[band];
4515 if (!sband)
4516 continue;
4518 for (i = 0; i < sband->n_bitrates; i++) {
4519 if (sband->bitrates[i].bitrate == rateval) {
4520 mcast_rate[band] = i + 1;
4521 found = true;
4522 break;
4527 return found;
4530 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4533 struct net_device *dev = info->user_ptr[1];
4534 struct cfg80211_ibss_params ibss;
4535 struct wiphy *wiphy;
4536 struct cfg80211_cached_keys *connkeys = NULL;
4537 int err;
4539 memset(&ibss, 0, sizeof(ibss));
4541 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4542 return -EINVAL;
4544 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4545 !info->attrs[NL80211_ATTR_SSID] ||
4546 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4547 return -EINVAL;
4549 ibss.beacon_interval = 100;
4551 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4552 ibss.beacon_interval =
4553 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4554 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4555 return -EINVAL;
4558 if (!rdev->ops->join_ibss)
4559 return -EOPNOTSUPP;
4561 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4562 return -EOPNOTSUPP;
4564 wiphy = &rdev->wiphy;
4566 if (info->attrs[NL80211_ATTR_MAC]) {
4567 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4569 if (!is_valid_ether_addr(ibss.bssid))
4570 return -EINVAL;
4572 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4573 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4575 if (info->attrs[NL80211_ATTR_IE]) {
4576 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4577 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4580 ibss.channel = ieee80211_get_channel(wiphy,
4581 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4582 if (!ibss.channel ||
4583 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4584 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4585 return -EINVAL;
4587 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
4588 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4590 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4591 u8 *rates =
4592 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4593 int n_rates =
4594 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4595 struct ieee80211_supported_band *sband =
4596 wiphy->bands[ibss.channel->band];
4597 int err;
4599 err = ieee80211_get_ratemask(sband, rates, n_rates,
4600 &ibss.basic_rates);
4601 if (err)
4602 return err;
4605 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4606 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4607 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4608 return -EINVAL;
4610 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4611 connkeys = nl80211_parse_connkeys(rdev,
4612 info->attrs[NL80211_ATTR_KEYS]);
4613 if (IS_ERR(connkeys))
4614 return PTR_ERR(connkeys);
4617 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
4618 if (err)
4619 kfree(connkeys);
4620 return err;
4623 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4626 struct net_device *dev = info->user_ptr[1];
4628 if (!rdev->ops->leave_ibss)
4629 return -EOPNOTSUPP;
4631 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4632 return -EOPNOTSUPP;
4634 return cfg80211_leave_ibss(rdev, dev, false);
4637 #ifdef CONFIG_NL80211_TESTMODE
4638 static struct genl_multicast_group nl80211_testmode_mcgrp = {
4639 .name = "testmode",
4642 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4645 int err;
4647 if (!info->attrs[NL80211_ATTR_TESTDATA])
4648 return -EINVAL;
4650 err = -EOPNOTSUPP;
4651 if (rdev->ops->testmode_cmd) {
4652 rdev->testmode_info = info;
4653 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4654 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4655 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4656 rdev->testmode_info = NULL;
4659 return err;
4662 static int nl80211_testmode_dump(struct sk_buff *skb,
4663 struct netlink_callback *cb)
4665 struct cfg80211_registered_device *dev;
4666 int err;
4667 long phy_idx;
4668 void *data = NULL;
4669 int data_len = 0;
4671 if (cb->args[0]) {
4673 * 0 is a valid index, but not valid for args[0],
4674 * so we need to offset by 1.
4676 phy_idx = cb->args[0] - 1;
4677 } else {
4678 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4679 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4680 nl80211_policy);
4681 if (err)
4682 return err;
4683 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4684 return -EINVAL;
4685 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4686 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4687 cb->args[1] =
4688 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4691 if (cb->args[1]) {
4692 data = nla_data((void *)cb->args[1]);
4693 data_len = nla_len((void *)cb->args[1]);
4696 mutex_lock(&cfg80211_mutex);
4697 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4698 if (!dev) {
4699 mutex_unlock(&cfg80211_mutex);
4700 return -ENOENT;
4702 cfg80211_lock_rdev(dev);
4703 mutex_unlock(&cfg80211_mutex);
4705 if (!dev->ops->testmode_dump) {
4706 err = -EOPNOTSUPP;
4707 goto out_err;
4710 while (1) {
4711 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4712 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4713 NL80211_CMD_TESTMODE);
4714 struct nlattr *tmdata;
4716 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4717 genlmsg_cancel(skb, hdr);
4718 break;
4721 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4722 if (!tmdata) {
4723 genlmsg_cancel(skb, hdr);
4724 break;
4726 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4727 data, data_len);
4728 nla_nest_end(skb, tmdata);
4730 if (err == -ENOBUFS || err == -ENOENT) {
4731 genlmsg_cancel(skb, hdr);
4732 break;
4733 } else if (err) {
4734 genlmsg_cancel(skb, hdr);
4735 goto out_err;
4738 genlmsg_end(skb, hdr);
4741 err = skb->len;
4742 /* see above */
4743 cb->args[0] = phy_idx + 1;
4744 out_err:
4745 cfg80211_unlock_rdev(dev);
4746 return err;
4749 static struct sk_buff *
4750 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4751 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4753 struct sk_buff *skb;
4754 void *hdr;
4755 struct nlattr *data;
4757 skb = nlmsg_new(approxlen + 100, gfp);
4758 if (!skb)
4759 return NULL;
4761 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4762 if (!hdr) {
4763 kfree_skb(skb);
4764 return NULL;
4767 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4768 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4770 ((void **)skb->cb)[0] = rdev;
4771 ((void **)skb->cb)[1] = hdr;
4772 ((void **)skb->cb)[2] = data;
4774 return skb;
4776 nla_put_failure:
4777 kfree_skb(skb);
4778 return NULL;
4781 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4782 int approxlen)
4784 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4786 if (WARN_ON(!rdev->testmode_info))
4787 return NULL;
4789 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4790 rdev->testmode_info->snd_pid,
4791 rdev->testmode_info->snd_seq,
4792 GFP_KERNEL);
4794 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4796 int cfg80211_testmode_reply(struct sk_buff *skb)
4798 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4799 void *hdr = ((void **)skb->cb)[1];
4800 struct nlattr *data = ((void **)skb->cb)[2];
4802 if (WARN_ON(!rdev->testmode_info)) {
4803 kfree_skb(skb);
4804 return -EINVAL;
4807 nla_nest_end(skb, data);
4808 genlmsg_end(skb, hdr);
4809 return genlmsg_reply(skb, rdev->testmode_info);
4811 EXPORT_SYMBOL(cfg80211_testmode_reply);
4813 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4814 int approxlen, gfp_t gfp)
4816 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4818 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4820 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4822 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4824 void *hdr = ((void **)skb->cb)[1];
4825 struct nlattr *data = ((void **)skb->cb)[2];
4827 nla_nest_end(skb, data);
4828 genlmsg_end(skb, hdr);
4829 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4831 EXPORT_SYMBOL(cfg80211_testmode_event);
4832 #endif
4834 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4837 struct net_device *dev = info->user_ptr[1];
4838 struct cfg80211_connect_params connect;
4839 struct wiphy *wiphy;
4840 struct cfg80211_cached_keys *connkeys = NULL;
4841 int err;
4843 memset(&connect, 0, sizeof(connect));
4845 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4846 return -EINVAL;
4848 if (!info->attrs[NL80211_ATTR_SSID] ||
4849 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4850 return -EINVAL;
4852 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4853 connect.auth_type =
4854 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4855 if (!nl80211_valid_auth_type(connect.auth_type))
4856 return -EINVAL;
4857 } else
4858 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4860 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4862 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
4863 NL80211_MAX_NR_CIPHER_SUITES);
4864 if (err)
4865 return err;
4867 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4868 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4869 return -EOPNOTSUPP;
4871 wiphy = &rdev->wiphy;
4873 if (info->attrs[NL80211_ATTR_MAC])
4874 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4875 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4876 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4878 if (info->attrs[NL80211_ATTR_IE]) {
4879 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4880 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4883 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4884 connect.channel =
4885 ieee80211_get_channel(wiphy,
4886 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4887 if (!connect.channel ||
4888 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4889 return -EINVAL;
4892 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4893 connkeys = nl80211_parse_connkeys(rdev,
4894 info->attrs[NL80211_ATTR_KEYS]);
4895 if (IS_ERR(connkeys))
4896 return PTR_ERR(connkeys);
4899 err = cfg80211_connect(rdev, dev, &connect, connkeys);
4900 if (err)
4901 kfree(connkeys);
4902 return err;
4905 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4908 struct net_device *dev = info->user_ptr[1];
4909 u16 reason;
4911 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4912 reason = WLAN_REASON_DEAUTH_LEAVING;
4913 else
4914 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4916 if (reason == 0)
4917 return -EINVAL;
4919 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4920 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4921 return -EOPNOTSUPP;
4923 return cfg80211_disconnect(rdev, dev, reason, true);
4926 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4929 struct net *net;
4930 int err;
4931 u32 pid;
4933 if (!info->attrs[NL80211_ATTR_PID])
4934 return -EINVAL;
4936 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4938 net = get_net_ns_by_pid(pid);
4939 if (IS_ERR(net))
4940 return PTR_ERR(net);
4942 err = 0;
4944 /* check if anything to do */
4945 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4946 err = cfg80211_switch_netns(rdev, net);
4948 put_net(net);
4949 return err;
4952 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4954 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4955 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4956 struct cfg80211_pmksa *pmksa) = NULL;
4957 struct net_device *dev = info->user_ptr[1];
4958 struct cfg80211_pmksa pmksa;
4960 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4962 if (!info->attrs[NL80211_ATTR_MAC])
4963 return -EINVAL;
4965 if (!info->attrs[NL80211_ATTR_PMKID])
4966 return -EINVAL;
4968 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4969 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4973 return -EOPNOTSUPP;
4975 switch (info->genlhdr->cmd) {
4976 case NL80211_CMD_SET_PMKSA:
4977 rdev_ops = rdev->ops->set_pmksa;
4978 break;
4979 case NL80211_CMD_DEL_PMKSA:
4980 rdev_ops = rdev->ops->del_pmksa;
4981 break;
4982 default:
4983 WARN_ON(1);
4984 break;
4987 if (!rdev_ops)
4988 return -EOPNOTSUPP;
4990 return rdev_ops(&rdev->wiphy, dev, &pmksa);
4993 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4996 struct net_device *dev = info->user_ptr[1];
4998 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4999 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5000 return -EOPNOTSUPP;
5002 if (!rdev->ops->flush_pmksa)
5003 return -EOPNOTSUPP;
5005 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
5008 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5010 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5011 struct net_device *dev = info->user_ptr[1];
5012 u8 action_code, dialog_token;
5013 u16 status_code;
5014 u8 *peer;
5016 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5017 !rdev->ops->tdls_mgmt)
5018 return -EOPNOTSUPP;
5020 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5021 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5022 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5023 !info->attrs[NL80211_ATTR_IE] ||
5024 !info->attrs[NL80211_ATTR_MAC])
5025 return -EINVAL;
5027 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5028 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5029 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5030 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5032 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5033 dialog_token, status_code,
5034 nla_data(info->attrs[NL80211_ATTR_IE]),
5035 nla_len(info->attrs[NL80211_ATTR_IE]));
5038 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5041 struct net_device *dev = info->user_ptr[1];
5042 enum nl80211_tdls_operation operation;
5043 u8 *peer;
5045 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5046 !rdev->ops->tdls_oper)
5047 return -EOPNOTSUPP;
5049 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5050 !info->attrs[NL80211_ATTR_MAC])
5051 return -EINVAL;
5053 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5054 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5056 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5059 static int nl80211_remain_on_channel(struct sk_buff *skb,
5060 struct genl_info *info)
5062 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5063 struct net_device *dev = info->user_ptr[1];
5064 struct ieee80211_channel *chan;
5065 struct sk_buff *msg;
5066 void *hdr;
5067 u64 cookie;
5068 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5069 u32 freq, duration;
5070 int err;
5072 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5073 !info->attrs[NL80211_ATTR_DURATION])
5074 return -EINVAL;
5076 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5079 * We should be on that channel for at least one jiffie,
5080 * and more than 5 seconds seems excessive.
5082 if (!duration || !msecs_to_jiffies(duration) ||
5083 duration > rdev->wiphy.max_remain_on_channel_duration)
5084 return -EINVAL;
5086 if (!rdev->ops->remain_on_channel)
5087 return -EOPNOTSUPP;
5089 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5090 channel_type = nla_get_u32(
5091 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5092 if (channel_type != NL80211_CHAN_NO_HT &&
5093 channel_type != NL80211_CHAN_HT20 &&
5094 channel_type != NL80211_CHAN_HT40PLUS &&
5095 channel_type != NL80211_CHAN_HT40MINUS)
5096 return -EINVAL;
5099 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5100 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5101 if (chan == NULL)
5102 return -EINVAL;
5104 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5105 if (!msg)
5106 return -ENOMEM;
5108 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5109 NL80211_CMD_REMAIN_ON_CHANNEL);
5111 if (IS_ERR(hdr)) {
5112 err = PTR_ERR(hdr);
5113 goto free_msg;
5116 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5117 channel_type, duration, &cookie);
5119 if (err)
5120 goto free_msg;
5122 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5124 genlmsg_end(msg, hdr);
5126 return genlmsg_reply(msg, info);
5128 nla_put_failure:
5129 err = -ENOBUFS;
5130 free_msg:
5131 nlmsg_free(msg);
5132 return err;
5135 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5136 struct genl_info *info)
5138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5139 struct net_device *dev = info->user_ptr[1];
5140 u64 cookie;
5142 if (!info->attrs[NL80211_ATTR_COOKIE])
5143 return -EINVAL;
5145 if (!rdev->ops->cancel_remain_on_channel)
5146 return -EOPNOTSUPP;
5148 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5150 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
5153 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5154 u8 *rates, u8 rates_len)
5156 u8 i;
5157 u32 mask = 0;
5159 for (i = 0; i < rates_len; i++) {
5160 int rate = (rates[i] & 0x7f) * 5;
5161 int ridx;
5162 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5163 struct ieee80211_rate *srate =
5164 &sband->bitrates[ridx];
5165 if (rate == srate->bitrate) {
5166 mask |= 1 << ridx;
5167 break;
5170 if (ridx == sband->n_bitrates)
5171 return 0; /* rate not found */
5174 return mask;
5177 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
5178 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5179 .len = NL80211_MAX_SUPP_RATES },
5182 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5183 struct genl_info *info)
5185 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5186 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5187 struct cfg80211_bitrate_mask mask;
5188 int rem, i;
5189 struct net_device *dev = info->user_ptr[1];
5190 struct nlattr *tx_rates;
5191 struct ieee80211_supported_band *sband;
5193 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5194 return -EINVAL;
5196 if (!rdev->ops->set_bitrate_mask)
5197 return -EOPNOTSUPP;
5199 memset(&mask, 0, sizeof(mask));
5200 /* Default to all rates enabled */
5201 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5202 sband = rdev->wiphy.bands[i];
5203 mask.control[i].legacy =
5204 sband ? (1 << sband->n_bitrates) - 1 : 0;
5208 * The nested attribute uses enum nl80211_band as the index. This maps
5209 * directly to the enum ieee80211_band values used in cfg80211.
5211 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5213 enum ieee80211_band band = nla_type(tx_rates);
5214 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5215 return -EINVAL;
5216 sband = rdev->wiphy.bands[band];
5217 if (sband == NULL)
5218 return -EINVAL;
5219 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5220 nla_len(tx_rates), nl80211_txattr_policy);
5221 if (tb[NL80211_TXRATE_LEGACY]) {
5222 mask.control[band].legacy = rateset_to_mask(
5223 sband,
5224 nla_data(tb[NL80211_TXRATE_LEGACY]),
5225 nla_len(tb[NL80211_TXRATE_LEGACY]));
5226 if (mask.control[band].legacy == 0)
5227 return -EINVAL;
5231 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
5234 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
5236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5237 struct net_device *dev = info->user_ptr[1];
5238 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
5240 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5241 return -EINVAL;
5243 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5244 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
5246 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5247 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5248 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5249 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5250 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5251 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5252 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5253 return -EOPNOTSUPP;
5255 /* not much point in registering if we can't reply */
5256 if (!rdev->ops->mgmt_tx)
5257 return -EOPNOTSUPP;
5259 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
5260 frame_type,
5261 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5262 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
5265 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
5267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5268 struct net_device *dev = info->user_ptr[1];
5269 struct ieee80211_channel *chan;
5270 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5271 bool channel_type_valid = false;
5272 u32 freq;
5273 int err;
5274 void *hdr;
5275 u64 cookie;
5276 struct sk_buff *msg;
5277 unsigned int wait = 0;
5278 bool offchan;
5279 bool no_cck;
5281 if (!info->attrs[NL80211_ATTR_FRAME] ||
5282 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5283 return -EINVAL;
5285 if (!rdev->ops->mgmt_tx)
5286 return -EOPNOTSUPP;
5288 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5289 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5290 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5291 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5292 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5293 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5294 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5295 return -EOPNOTSUPP;
5297 if (info->attrs[NL80211_ATTR_DURATION]) {
5298 if (!rdev->ops->mgmt_tx_cancel_wait)
5299 return -EINVAL;
5300 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5303 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5304 channel_type = nla_get_u32(
5305 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5306 if (channel_type != NL80211_CHAN_NO_HT &&
5307 channel_type != NL80211_CHAN_HT20 &&
5308 channel_type != NL80211_CHAN_HT40PLUS &&
5309 channel_type != NL80211_CHAN_HT40MINUS)
5310 return -EINVAL;
5311 channel_type_valid = true;
5314 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5316 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5318 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5319 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5320 if (chan == NULL)
5321 return -EINVAL;
5323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5324 if (!msg)
5325 return -ENOMEM;
5327 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5328 NL80211_CMD_FRAME);
5330 if (IS_ERR(hdr)) {
5331 err = PTR_ERR(hdr);
5332 goto free_msg;
5334 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5335 channel_type_valid, wait,
5336 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5337 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5338 no_cck, &cookie);
5339 if (err)
5340 goto free_msg;
5342 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5344 genlmsg_end(msg, hdr);
5345 return genlmsg_reply(msg, info);
5347 nla_put_failure:
5348 err = -ENOBUFS;
5349 free_msg:
5350 nlmsg_free(msg);
5351 return err;
5354 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5356 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5357 struct net_device *dev = info->user_ptr[1];
5358 u64 cookie;
5360 if (!info->attrs[NL80211_ATTR_COOKIE])
5361 return -EINVAL;
5363 if (!rdev->ops->mgmt_tx_cancel_wait)
5364 return -EOPNOTSUPP;
5366 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5367 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5368 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5369 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5370 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5371 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5372 return -EOPNOTSUPP;
5374 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5376 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5379 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5381 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5382 struct wireless_dev *wdev;
5383 struct net_device *dev = info->user_ptr[1];
5384 u8 ps_state;
5385 bool state;
5386 int err;
5388 if (!info->attrs[NL80211_ATTR_PS_STATE])
5389 return -EINVAL;
5391 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5393 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5394 return -EINVAL;
5396 wdev = dev->ieee80211_ptr;
5398 if (!rdev->ops->set_power_mgmt)
5399 return -EOPNOTSUPP;
5401 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5403 if (state == wdev->ps)
5404 return 0;
5406 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5407 wdev->ps_timeout);
5408 if (!err)
5409 wdev->ps = state;
5410 return err;
5413 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5415 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5416 enum nl80211_ps_state ps_state;
5417 struct wireless_dev *wdev;
5418 struct net_device *dev = info->user_ptr[1];
5419 struct sk_buff *msg;
5420 void *hdr;
5421 int err;
5423 wdev = dev->ieee80211_ptr;
5425 if (!rdev->ops->set_power_mgmt)
5426 return -EOPNOTSUPP;
5428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5429 if (!msg)
5430 return -ENOMEM;
5432 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5433 NL80211_CMD_GET_POWER_SAVE);
5434 if (!hdr) {
5435 err = -ENOBUFS;
5436 goto free_msg;
5439 if (wdev->ps)
5440 ps_state = NL80211_PS_ENABLED;
5441 else
5442 ps_state = NL80211_PS_DISABLED;
5444 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5446 genlmsg_end(msg, hdr);
5447 return genlmsg_reply(msg, info);
5449 nla_put_failure:
5450 err = -ENOBUFS;
5451 free_msg:
5452 nlmsg_free(msg);
5453 return err;
5456 static struct nla_policy
5457 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5458 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5459 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5460 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5463 static int nl80211_set_cqm_rssi(struct genl_info *info,
5464 s32 threshold, u32 hysteresis)
5466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5467 struct wireless_dev *wdev;
5468 struct net_device *dev = info->user_ptr[1];
5470 if (threshold > 0)
5471 return -EINVAL;
5473 wdev = dev->ieee80211_ptr;
5475 if (!rdev->ops->set_cqm_rssi_config)
5476 return -EOPNOTSUPP;
5478 if (wdev->iftype != NL80211_IFTYPE_STATION &&
5479 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5480 return -EOPNOTSUPP;
5482 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5483 threshold, hysteresis);
5486 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5488 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5489 struct nlattr *cqm;
5490 int err;
5492 cqm = info->attrs[NL80211_ATTR_CQM];
5493 if (!cqm) {
5494 err = -EINVAL;
5495 goto out;
5498 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5499 nl80211_attr_cqm_policy);
5500 if (err)
5501 goto out;
5503 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5504 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5505 s32 threshold;
5506 u32 hysteresis;
5507 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5508 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5509 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5510 } else
5511 err = -EINVAL;
5513 out:
5514 return err;
5517 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5519 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5520 struct net_device *dev = info->user_ptr[1];
5521 struct mesh_config cfg;
5522 struct mesh_setup setup;
5523 int err;
5525 /* start with default */
5526 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
5527 memcpy(&setup, &default_mesh_setup, sizeof(setup));
5529 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
5530 /* and parse parameters if given */
5531 err = nl80211_parse_mesh_config(info, &cfg, NULL);
5532 if (err)
5533 return err;
5536 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5537 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5538 return -EINVAL;
5540 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5541 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5543 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5544 /* parse additional setup parameters if given */
5545 err = nl80211_parse_mesh_setup(info, &setup);
5546 if (err)
5547 return err;
5550 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
5553 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5555 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5556 struct net_device *dev = info->user_ptr[1];
5558 return cfg80211_leave_mesh(rdev, dev);
5561 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5563 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5564 struct sk_buff *msg;
5565 void *hdr;
5567 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5568 return -EOPNOTSUPP;
5570 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5571 if (!msg)
5572 return -ENOMEM;
5574 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5575 NL80211_CMD_GET_WOWLAN);
5576 if (!hdr)
5577 goto nla_put_failure;
5579 if (rdev->wowlan) {
5580 struct nlattr *nl_wowlan;
5582 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5583 if (!nl_wowlan)
5584 goto nla_put_failure;
5586 if (rdev->wowlan->any)
5587 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5588 if (rdev->wowlan->disconnect)
5589 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5590 if (rdev->wowlan->magic_pkt)
5591 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
5592 if (rdev->wowlan->gtk_rekey_failure)
5593 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5594 if (rdev->wowlan->eap_identity_req)
5595 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5596 if (rdev->wowlan->four_way_handshake)
5597 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5598 if (rdev->wowlan->rfkill_release)
5599 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
5600 if (rdev->wowlan->n_patterns) {
5601 struct nlattr *nl_pats, *nl_pat;
5602 int i, pat_len;
5604 nl_pats = nla_nest_start(msg,
5605 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5606 if (!nl_pats)
5607 goto nla_put_failure;
5609 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5610 nl_pat = nla_nest_start(msg, i + 1);
5611 if (!nl_pat)
5612 goto nla_put_failure;
5613 pat_len = rdev->wowlan->patterns[i].pattern_len;
5614 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5615 DIV_ROUND_UP(pat_len, 8),
5616 rdev->wowlan->patterns[i].mask);
5617 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5618 pat_len,
5619 rdev->wowlan->patterns[i].pattern);
5620 nla_nest_end(msg, nl_pat);
5622 nla_nest_end(msg, nl_pats);
5625 nla_nest_end(msg, nl_wowlan);
5628 genlmsg_end(msg, hdr);
5629 return genlmsg_reply(msg, info);
5631 nla_put_failure:
5632 nlmsg_free(msg);
5633 return -ENOBUFS;
5636 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5638 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5639 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5640 struct cfg80211_wowlan no_triggers = {};
5641 struct cfg80211_wowlan new_triggers = {};
5642 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5643 int err, i;
5645 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5646 return -EOPNOTSUPP;
5648 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5649 goto no_triggers;
5651 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5652 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5653 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5654 nl80211_wowlan_policy);
5655 if (err)
5656 return err;
5658 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5659 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5660 return -EINVAL;
5661 new_triggers.any = true;
5664 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5665 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5666 return -EINVAL;
5667 new_triggers.disconnect = true;
5670 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5671 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5672 return -EINVAL;
5673 new_triggers.magic_pkt = true;
5676 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5677 return -EINVAL;
5679 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5680 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5681 return -EINVAL;
5682 new_triggers.gtk_rekey_failure = true;
5685 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5686 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5687 return -EINVAL;
5688 new_triggers.eap_identity_req = true;
5691 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5692 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5693 return -EINVAL;
5694 new_triggers.four_way_handshake = true;
5697 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5698 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5699 return -EINVAL;
5700 new_triggers.rfkill_release = true;
5703 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5704 struct nlattr *pat;
5705 int n_patterns = 0;
5706 int rem, pat_len, mask_len;
5707 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5709 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5710 rem)
5711 n_patterns++;
5712 if (n_patterns > wowlan->n_patterns)
5713 return -EINVAL;
5715 new_triggers.patterns = kcalloc(n_patterns,
5716 sizeof(new_triggers.patterns[0]),
5717 GFP_KERNEL);
5718 if (!new_triggers.patterns)
5719 return -ENOMEM;
5721 new_triggers.n_patterns = n_patterns;
5722 i = 0;
5724 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5725 rem) {
5726 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5727 nla_data(pat), nla_len(pat), NULL);
5728 err = -EINVAL;
5729 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5730 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5731 goto error;
5732 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5733 mask_len = DIV_ROUND_UP(pat_len, 8);
5734 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5735 mask_len)
5736 goto error;
5737 if (pat_len > wowlan->pattern_max_len ||
5738 pat_len < wowlan->pattern_min_len)
5739 goto error;
5741 new_triggers.patterns[i].mask =
5742 kmalloc(mask_len + pat_len, GFP_KERNEL);
5743 if (!new_triggers.patterns[i].mask) {
5744 err = -ENOMEM;
5745 goto error;
5747 new_triggers.patterns[i].pattern =
5748 new_triggers.patterns[i].mask + mask_len;
5749 memcpy(new_triggers.patterns[i].mask,
5750 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5751 mask_len);
5752 new_triggers.patterns[i].pattern_len = pat_len;
5753 memcpy(new_triggers.patterns[i].pattern,
5754 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5755 pat_len);
5756 i++;
5760 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5761 struct cfg80211_wowlan *ntrig;
5762 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5763 GFP_KERNEL);
5764 if (!ntrig) {
5765 err = -ENOMEM;
5766 goto error;
5768 cfg80211_rdev_free_wowlan(rdev);
5769 rdev->wowlan = ntrig;
5770 } else {
5771 no_triggers:
5772 cfg80211_rdev_free_wowlan(rdev);
5773 rdev->wowlan = NULL;
5776 return 0;
5777 error:
5778 for (i = 0; i < new_triggers.n_patterns; i++)
5779 kfree(new_triggers.patterns[i].mask);
5780 kfree(new_triggers.patterns);
5781 return err;
5784 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5787 struct net_device *dev = info->user_ptr[1];
5788 struct wireless_dev *wdev = dev->ieee80211_ptr;
5789 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5790 struct cfg80211_gtk_rekey_data rekey_data;
5791 int err;
5793 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5794 return -EINVAL;
5796 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5797 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5798 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5799 nl80211_rekey_policy);
5800 if (err)
5801 return err;
5803 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5804 return -ERANGE;
5805 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5806 return -ERANGE;
5807 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5808 return -ERANGE;
5810 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5811 NL80211_KEK_LEN);
5812 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5813 NL80211_KCK_LEN);
5814 memcpy(rekey_data.replay_ctr,
5815 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5816 NL80211_REPLAY_CTR_LEN);
5818 wdev_lock(wdev);
5819 if (!wdev->current_bss) {
5820 err = -ENOTCONN;
5821 goto out;
5824 if (!rdev->ops->set_rekey_data) {
5825 err = -EOPNOTSUPP;
5826 goto out;
5829 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5830 out:
5831 wdev_unlock(wdev);
5832 return err;
5835 #define NL80211_FLAG_NEED_WIPHY 0x01
5836 #define NL80211_FLAG_NEED_NETDEV 0x02
5837 #define NL80211_FLAG_NEED_RTNL 0x04
5838 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5839 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5840 NL80211_FLAG_CHECK_NETDEV_UP)
5842 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5843 struct genl_info *info)
5845 struct cfg80211_registered_device *rdev;
5846 struct net_device *dev;
5847 int err;
5848 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5850 if (rtnl)
5851 rtnl_lock();
5853 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5854 rdev = cfg80211_get_dev_from_info(info);
5855 if (IS_ERR(rdev)) {
5856 if (rtnl)
5857 rtnl_unlock();
5858 return PTR_ERR(rdev);
5860 info->user_ptr[0] = rdev;
5861 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5862 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5863 if (err) {
5864 if (rtnl)
5865 rtnl_unlock();
5866 return err;
5868 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5869 !netif_running(dev)) {
5870 cfg80211_unlock_rdev(rdev);
5871 dev_put(dev);
5872 if (rtnl)
5873 rtnl_unlock();
5874 return -ENETDOWN;
5876 info->user_ptr[0] = rdev;
5877 info->user_ptr[1] = dev;
5880 return 0;
5883 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5884 struct genl_info *info)
5886 if (info->user_ptr[0])
5887 cfg80211_unlock_rdev(info->user_ptr[0]);
5888 if (info->user_ptr[1])
5889 dev_put(info->user_ptr[1]);
5890 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5891 rtnl_unlock();
5894 static struct genl_ops nl80211_ops[] = {
5896 .cmd = NL80211_CMD_GET_WIPHY,
5897 .doit = nl80211_get_wiphy,
5898 .dumpit = nl80211_dump_wiphy,
5899 .policy = nl80211_policy,
5900 /* can be retrieved by unprivileged users */
5901 .internal_flags = NL80211_FLAG_NEED_WIPHY,
5904 .cmd = NL80211_CMD_SET_WIPHY,
5905 .doit = nl80211_set_wiphy,
5906 .policy = nl80211_policy,
5907 .flags = GENL_ADMIN_PERM,
5908 .internal_flags = NL80211_FLAG_NEED_RTNL,
5911 .cmd = NL80211_CMD_GET_INTERFACE,
5912 .doit = nl80211_get_interface,
5913 .dumpit = nl80211_dump_interface,
5914 .policy = nl80211_policy,
5915 /* can be retrieved by unprivileged users */
5916 .internal_flags = NL80211_FLAG_NEED_NETDEV,
5919 .cmd = NL80211_CMD_SET_INTERFACE,
5920 .doit = nl80211_set_interface,
5921 .policy = nl80211_policy,
5922 .flags = GENL_ADMIN_PERM,
5923 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5924 NL80211_FLAG_NEED_RTNL,
5927 .cmd = NL80211_CMD_NEW_INTERFACE,
5928 .doit = nl80211_new_interface,
5929 .policy = nl80211_policy,
5930 .flags = GENL_ADMIN_PERM,
5931 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5932 NL80211_FLAG_NEED_RTNL,
5935 .cmd = NL80211_CMD_DEL_INTERFACE,
5936 .doit = nl80211_del_interface,
5937 .policy = nl80211_policy,
5938 .flags = GENL_ADMIN_PERM,
5939 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5940 NL80211_FLAG_NEED_RTNL,
5943 .cmd = NL80211_CMD_GET_KEY,
5944 .doit = nl80211_get_key,
5945 .policy = nl80211_policy,
5946 .flags = GENL_ADMIN_PERM,
5947 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5948 NL80211_FLAG_NEED_RTNL,
5951 .cmd = NL80211_CMD_SET_KEY,
5952 .doit = nl80211_set_key,
5953 .policy = nl80211_policy,
5954 .flags = GENL_ADMIN_PERM,
5955 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5956 NL80211_FLAG_NEED_RTNL,
5959 .cmd = NL80211_CMD_NEW_KEY,
5960 .doit = nl80211_new_key,
5961 .policy = nl80211_policy,
5962 .flags = GENL_ADMIN_PERM,
5963 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5964 NL80211_FLAG_NEED_RTNL,
5967 .cmd = NL80211_CMD_DEL_KEY,
5968 .doit = nl80211_del_key,
5969 .policy = nl80211_policy,
5970 .flags = GENL_ADMIN_PERM,
5971 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5972 NL80211_FLAG_NEED_RTNL,
5975 .cmd = NL80211_CMD_SET_BEACON,
5976 .policy = nl80211_policy,
5977 .flags = GENL_ADMIN_PERM,
5978 .doit = nl80211_addset_beacon,
5979 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5980 NL80211_FLAG_NEED_RTNL,
5983 .cmd = NL80211_CMD_NEW_BEACON,
5984 .policy = nl80211_policy,
5985 .flags = GENL_ADMIN_PERM,
5986 .doit = nl80211_addset_beacon,
5987 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5988 NL80211_FLAG_NEED_RTNL,
5991 .cmd = NL80211_CMD_DEL_BEACON,
5992 .policy = nl80211_policy,
5993 .flags = GENL_ADMIN_PERM,
5994 .doit = nl80211_del_beacon,
5995 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5996 NL80211_FLAG_NEED_RTNL,
5999 .cmd = NL80211_CMD_GET_STATION,
6000 .doit = nl80211_get_station,
6001 .dumpit = nl80211_dump_station,
6002 .policy = nl80211_policy,
6003 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6004 NL80211_FLAG_NEED_RTNL,
6007 .cmd = NL80211_CMD_SET_STATION,
6008 .doit = nl80211_set_station,
6009 .policy = nl80211_policy,
6010 .flags = GENL_ADMIN_PERM,
6011 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6012 NL80211_FLAG_NEED_RTNL,
6015 .cmd = NL80211_CMD_NEW_STATION,
6016 .doit = nl80211_new_station,
6017 .policy = nl80211_policy,
6018 .flags = GENL_ADMIN_PERM,
6019 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6020 NL80211_FLAG_NEED_RTNL,
6023 .cmd = NL80211_CMD_DEL_STATION,
6024 .doit = nl80211_del_station,
6025 .policy = nl80211_policy,
6026 .flags = GENL_ADMIN_PERM,
6027 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6028 NL80211_FLAG_NEED_RTNL,
6031 .cmd = NL80211_CMD_GET_MPATH,
6032 .doit = nl80211_get_mpath,
6033 .dumpit = nl80211_dump_mpath,
6034 .policy = nl80211_policy,
6035 .flags = GENL_ADMIN_PERM,
6036 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6037 NL80211_FLAG_NEED_RTNL,
6040 .cmd = NL80211_CMD_SET_MPATH,
6041 .doit = nl80211_set_mpath,
6042 .policy = nl80211_policy,
6043 .flags = GENL_ADMIN_PERM,
6044 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6045 NL80211_FLAG_NEED_RTNL,
6048 .cmd = NL80211_CMD_NEW_MPATH,
6049 .doit = nl80211_new_mpath,
6050 .policy = nl80211_policy,
6051 .flags = GENL_ADMIN_PERM,
6052 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6053 NL80211_FLAG_NEED_RTNL,
6056 .cmd = NL80211_CMD_DEL_MPATH,
6057 .doit = nl80211_del_mpath,
6058 .policy = nl80211_policy,
6059 .flags = GENL_ADMIN_PERM,
6060 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6061 NL80211_FLAG_NEED_RTNL,
6064 .cmd = NL80211_CMD_SET_BSS,
6065 .doit = nl80211_set_bss,
6066 .policy = nl80211_policy,
6067 .flags = GENL_ADMIN_PERM,
6068 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6069 NL80211_FLAG_NEED_RTNL,
6072 .cmd = NL80211_CMD_GET_REG,
6073 .doit = nl80211_get_reg,
6074 .policy = nl80211_policy,
6075 /* can be retrieved by unprivileged users */
6078 .cmd = NL80211_CMD_SET_REG,
6079 .doit = nl80211_set_reg,
6080 .policy = nl80211_policy,
6081 .flags = GENL_ADMIN_PERM,
6084 .cmd = NL80211_CMD_REQ_SET_REG,
6085 .doit = nl80211_req_set_reg,
6086 .policy = nl80211_policy,
6087 .flags = GENL_ADMIN_PERM,
6090 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6091 .doit = nl80211_get_mesh_config,
6092 .policy = nl80211_policy,
6093 /* can be retrieved by unprivileged users */
6094 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6095 NL80211_FLAG_NEED_RTNL,
6098 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6099 .doit = nl80211_update_mesh_config,
6100 .policy = nl80211_policy,
6101 .flags = GENL_ADMIN_PERM,
6102 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6103 NL80211_FLAG_NEED_RTNL,
6106 .cmd = NL80211_CMD_TRIGGER_SCAN,
6107 .doit = nl80211_trigger_scan,
6108 .policy = nl80211_policy,
6109 .flags = GENL_ADMIN_PERM,
6110 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6111 NL80211_FLAG_NEED_RTNL,
6114 .cmd = NL80211_CMD_GET_SCAN,
6115 .policy = nl80211_policy,
6116 .dumpit = nl80211_dump_scan,
6119 .cmd = NL80211_CMD_START_SCHED_SCAN,
6120 .doit = nl80211_start_sched_scan,
6121 .policy = nl80211_policy,
6122 .flags = GENL_ADMIN_PERM,
6123 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6124 NL80211_FLAG_NEED_RTNL,
6127 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6128 .doit = nl80211_stop_sched_scan,
6129 .policy = nl80211_policy,
6130 .flags = GENL_ADMIN_PERM,
6131 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6132 NL80211_FLAG_NEED_RTNL,
6135 .cmd = NL80211_CMD_AUTHENTICATE,
6136 .doit = nl80211_authenticate,
6137 .policy = nl80211_policy,
6138 .flags = GENL_ADMIN_PERM,
6139 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6140 NL80211_FLAG_NEED_RTNL,
6143 .cmd = NL80211_CMD_ASSOCIATE,
6144 .doit = nl80211_associate,
6145 .policy = nl80211_policy,
6146 .flags = GENL_ADMIN_PERM,
6147 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6148 NL80211_FLAG_NEED_RTNL,
6151 .cmd = NL80211_CMD_DEAUTHENTICATE,
6152 .doit = nl80211_deauthenticate,
6153 .policy = nl80211_policy,
6154 .flags = GENL_ADMIN_PERM,
6155 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6156 NL80211_FLAG_NEED_RTNL,
6159 .cmd = NL80211_CMD_DISASSOCIATE,
6160 .doit = nl80211_disassociate,
6161 .policy = nl80211_policy,
6162 .flags = GENL_ADMIN_PERM,
6163 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6164 NL80211_FLAG_NEED_RTNL,
6167 .cmd = NL80211_CMD_JOIN_IBSS,
6168 .doit = nl80211_join_ibss,
6169 .policy = nl80211_policy,
6170 .flags = GENL_ADMIN_PERM,
6171 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6172 NL80211_FLAG_NEED_RTNL,
6175 .cmd = NL80211_CMD_LEAVE_IBSS,
6176 .doit = nl80211_leave_ibss,
6177 .policy = nl80211_policy,
6178 .flags = GENL_ADMIN_PERM,
6179 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6180 NL80211_FLAG_NEED_RTNL,
6182 #ifdef CONFIG_NL80211_TESTMODE
6184 .cmd = NL80211_CMD_TESTMODE,
6185 .doit = nl80211_testmode_do,
6186 .dumpit = nl80211_testmode_dump,
6187 .policy = nl80211_policy,
6188 .flags = GENL_ADMIN_PERM,
6189 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6190 NL80211_FLAG_NEED_RTNL,
6192 #endif
6194 .cmd = NL80211_CMD_CONNECT,
6195 .doit = nl80211_connect,
6196 .policy = nl80211_policy,
6197 .flags = GENL_ADMIN_PERM,
6198 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6199 NL80211_FLAG_NEED_RTNL,
6202 .cmd = NL80211_CMD_DISCONNECT,
6203 .doit = nl80211_disconnect,
6204 .policy = nl80211_policy,
6205 .flags = GENL_ADMIN_PERM,
6206 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6207 NL80211_FLAG_NEED_RTNL,
6210 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6211 .doit = nl80211_wiphy_netns,
6212 .policy = nl80211_policy,
6213 .flags = GENL_ADMIN_PERM,
6214 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6215 NL80211_FLAG_NEED_RTNL,
6218 .cmd = NL80211_CMD_GET_SURVEY,
6219 .policy = nl80211_policy,
6220 .dumpit = nl80211_dump_survey,
6223 .cmd = NL80211_CMD_SET_PMKSA,
6224 .doit = nl80211_setdel_pmksa,
6225 .policy = nl80211_policy,
6226 .flags = GENL_ADMIN_PERM,
6227 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6228 NL80211_FLAG_NEED_RTNL,
6231 .cmd = NL80211_CMD_DEL_PMKSA,
6232 .doit = nl80211_setdel_pmksa,
6233 .policy = nl80211_policy,
6234 .flags = GENL_ADMIN_PERM,
6235 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6236 NL80211_FLAG_NEED_RTNL,
6239 .cmd = NL80211_CMD_FLUSH_PMKSA,
6240 .doit = nl80211_flush_pmksa,
6241 .policy = nl80211_policy,
6242 .flags = GENL_ADMIN_PERM,
6243 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6244 NL80211_FLAG_NEED_RTNL,
6247 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6248 .doit = nl80211_remain_on_channel,
6249 .policy = nl80211_policy,
6250 .flags = GENL_ADMIN_PERM,
6251 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6252 NL80211_FLAG_NEED_RTNL,
6255 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6256 .doit = nl80211_cancel_remain_on_channel,
6257 .policy = nl80211_policy,
6258 .flags = GENL_ADMIN_PERM,
6259 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6260 NL80211_FLAG_NEED_RTNL,
6263 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6264 .doit = nl80211_set_tx_bitrate_mask,
6265 .policy = nl80211_policy,
6266 .flags = GENL_ADMIN_PERM,
6267 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6268 NL80211_FLAG_NEED_RTNL,
6271 .cmd = NL80211_CMD_REGISTER_FRAME,
6272 .doit = nl80211_register_mgmt,
6273 .policy = nl80211_policy,
6274 .flags = GENL_ADMIN_PERM,
6275 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6276 NL80211_FLAG_NEED_RTNL,
6279 .cmd = NL80211_CMD_FRAME,
6280 .doit = nl80211_tx_mgmt,
6281 .policy = nl80211_policy,
6282 .flags = GENL_ADMIN_PERM,
6283 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6284 NL80211_FLAG_NEED_RTNL,
6287 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6288 .doit = nl80211_tx_mgmt_cancel_wait,
6289 .policy = nl80211_policy,
6290 .flags = GENL_ADMIN_PERM,
6291 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6292 NL80211_FLAG_NEED_RTNL,
6295 .cmd = NL80211_CMD_SET_POWER_SAVE,
6296 .doit = nl80211_set_power_save,
6297 .policy = nl80211_policy,
6298 .flags = GENL_ADMIN_PERM,
6299 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6300 NL80211_FLAG_NEED_RTNL,
6303 .cmd = NL80211_CMD_GET_POWER_SAVE,
6304 .doit = nl80211_get_power_save,
6305 .policy = nl80211_policy,
6306 /* can be retrieved by unprivileged users */
6307 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6308 NL80211_FLAG_NEED_RTNL,
6311 .cmd = NL80211_CMD_SET_CQM,
6312 .doit = nl80211_set_cqm,
6313 .policy = nl80211_policy,
6314 .flags = GENL_ADMIN_PERM,
6315 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6316 NL80211_FLAG_NEED_RTNL,
6319 .cmd = NL80211_CMD_SET_CHANNEL,
6320 .doit = nl80211_set_channel,
6321 .policy = nl80211_policy,
6322 .flags = GENL_ADMIN_PERM,
6323 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6324 NL80211_FLAG_NEED_RTNL,
6327 .cmd = NL80211_CMD_SET_WDS_PEER,
6328 .doit = nl80211_set_wds_peer,
6329 .policy = nl80211_policy,
6330 .flags = GENL_ADMIN_PERM,
6331 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6332 NL80211_FLAG_NEED_RTNL,
6335 .cmd = NL80211_CMD_JOIN_MESH,
6336 .doit = nl80211_join_mesh,
6337 .policy = nl80211_policy,
6338 .flags = GENL_ADMIN_PERM,
6339 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6340 NL80211_FLAG_NEED_RTNL,
6343 .cmd = NL80211_CMD_LEAVE_MESH,
6344 .doit = nl80211_leave_mesh,
6345 .policy = nl80211_policy,
6346 .flags = GENL_ADMIN_PERM,
6347 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6348 NL80211_FLAG_NEED_RTNL,
6351 .cmd = NL80211_CMD_GET_WOWLAN,
6352 .doit = nl80211_get_wowlan,
6353 .policy = nl80211_policy,
6354 /* can be retrieved by unprivileged users */
6355 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6356 NL80211_FLAG_NEED_RTNL,
6359 .cmd = NL80211_CMD_SET_WOWLAN,
6360 .doit = nl80211_set_wowlan,
6361 .policy = nl80211_policy,
6362 .flags = GENL_ADMIN_PERM,
6363 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6364 NL80211_FLAG_NEED_RTNL,
6367 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6368 .doit = nl80211_set_rekey_data,
6369 .policy = nl80211_policy,
6370 .flags = GENL_ADMIN_PERM,
6371 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6372 NL80211_FLAG_NEED_RTNL,
6375 .cmd = NL80211_CMD_TDLS_MGMT,
6376 .doit = nl80211_tdls_mgmt,
6377 .policy = nl80211_policy,
6378 .flags = GENL_ADMIN_PERM,
6379 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6380 NL80211_FLAG_NEED_RTNL,
6383 .cmd = NL80211_CMD_TDLS_OPER,
6384 .doit = nl80211_tdls_oper,
6385 .policy = nl80211_policy,
6386 .flags = GENL_ADMIN_PERM,
6387 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6388 NL80211_FLAG_NEED_RTNL,
6392 static struct genl_multicast_group nl80211_mlme_mcgrp = {
6393 .name = "mlme",
6396 /* multicast groups */
6397 static struct genl_multicast_group nl80211_config_mcgrp = {
6398 .name = "config",
6400 static struct genl_multicast_group nl80211_scan_mcgrp = {
6401 .name = "scan",
6403 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6404 .name = "regulatory",
6407 /* notification functions */
6409 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6411 struct sk_buff *msg;
6413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6414 if (!msg)
6415 return;
6417 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6418 nlmsg_free(msg);
6419 return;
6422 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6423 nl80211_config_mcgrp.id, GFP_KERNEL);
6426 static int nl80211_add_scan_req(struct sk_buff *msg,
6427 struct cfg80211_registered_device *rdev)
6429 struct cfg80211_scan_request *req = rdev->scan_req;
6430 struct nlattr *nest;
6431 int i;
6433 ASSERT_RDEV_LOCK(rdev);
6435 if (WARN_ON(!req))
6436 return 0;
6438 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6439 if (!nest)
6440 goto nla_put_failure;
6441 for (i = 0; i < req->n_ssids; i++)
6442 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6443 nla_nest_end(msg, nest);
6445 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6446 if (!nest)
6447 goto nla_put_failure;
6448 for (i = 0; i < req->n_channels; i++)
6449 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6450 nla_nest_end(msg, nest);
6452 if (req->ie)
6453 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6455 return 0;
6456 nla_put_failure:
6457 return -ENOBUFS;
6460 static int nl80211_send_scan_msg(struct sk_buff *msg,
6461 struct cfg80211_registered_device *rdev,
6462 struct net_device *netdev,
6463 u32 pid, u32 seq, int flags,
6464 u32 cmd)
6466 void *hdr;
6468 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6469 if (!hdr)
6470 return -1;
6472 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6473 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6475 /* ignore errors and send incomplete event anyway */
6476 nl80211_add_scan_req(msg, rdev);
6478 return genlmsg_end(msg, hdr);
6480 nla_put_failure:
6481 genlmsg_cancel(msg, hdr);
6482 return -EMSGSIZE;
6485 static int
6486 nl80211_send_sched_scan_msg(struct sk_buff *msg,
6487 struct cfg80211_registered_device *rdev,
6488 struct net_device *netdev,
6489 u32 pid, u32 seq, int flags, u32 cmd)
6491 void *hdr;
6493 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6494 if (!hdr)
6495 return -1;
6497 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6498 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6500 return genlmsg_end(msg, hdr);
6502 nla_put_failure:
6503 genlmsg_cancel(msg, hdr);
6504 return -EMSGSIZE;
6507 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6508 struct net_device *netdev)
6510 struct sk_buff *msg;
6512 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6513 if (!msg)
6514 return;
6516 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6517 NL80211_CMD_TRIGGER_SCAN) < 0) {
6518 nlmsg_free(msg);
6519 return;
6522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6523 nl80211_scan_mcgrp.id, GFP_KERNEL);
6526 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6527 struct net_device *netdev)
6529 struct sk_buff *msg;
6531 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6532 if (!msg)
6533 return;
6535 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6536 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
6537 nlmsg_free(msg);
6538 return;
6541 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6542 nl80211_scan_mcgrp.id, GFP_KERNEL);
6545 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6546 struct net_device *netdev)
6548 struct sk_buff *msg;
6550 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6551 if (!msg)
6552 return;
6554 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6555 NL80211_CMD_SCAN_ABORTED) < 0) {
6556 nlmsg_free(msg);
6557 return;
6560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6561 nl80211_scan_mcgrp.id, GFP_KERNEL);
6564 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6565 struct net_device *netdev)
6567 struct sk_buff *msg;
6569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6570 if (!msg)
6571 return;
6573 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6574 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6575 nlmsg_free(msg);
6576 return;
6579 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6580 nl80211_scan_mcgrp.id, GFP_KERNEL);
6583 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6584 struct net_device *netdev, u32 cmd)
6586 struct sk_buff *msg;
6588 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6589 if (!msg)
6590 return;
6592 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6593 nlmsg_free(msg);
6594 return;
6597 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6598 nl80211_scan_mcgrp.id, GFP_KERNEL);
6602 * This can happen on global regulatory changes or device specific settings
6603 * based on custom world regulatory domains.
6605 void nl80211_send_reg_change_event(struct regulatory_request *request)
6607 struct sk_buff *msg;
6608 void *hdr;
6610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6611 if (!msg)
6612 return;
6614 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6615 if (!hdr) {
6616 nlmsg_free(msg);
6617 return;
6620 /* Userspace can always count this one always being set */
6621 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6623 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6624 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6625 NL80211_REGDOM_TYPE_WORLD);
6626 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6627 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6628 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6629 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6630 request->intersect)
6631 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6632 NL80211_REGDOM_TYPE_INTERSECTION);
6633 else {
6634 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6635 NL80211_REGDOM_TYPE_COUNTRY);
6636 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6639 if (wiphy_idx_valid(request->wiphy_idx))
6640 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6642 if (genlmsg_end(msg, hdr) < 0) {
6643 nlmsg_free(msg);
6644 return;
6647 rcu_read_lock();
6648 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6649 GFP_ATOMIC);
6650 rcu_read_unlock();
6652 return;
6654 nla_put_failure:
6655 genlmsg_cancel(msg, hdr);
6656 nlmsg_free(msg);
6659 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6660 struct net_device *netdev,
6661 const u8 *buf, size_t len,
6662 enum nl80211_commands cmd, gfp_t gfp)
6664 struct sk_buff *msg;
6665 void *hdr;
6667 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6668 if (!msg)
6669 return;
6671 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6672 if (!hdr) {
6673 nlmsg_free(msg);
6674 return;
6677 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6678 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6679 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6681 if (genlmsg_end(msg, hdr) < 0) {
6682 nlmsg_free(msg);
6683 return;
6686 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6687 nl80211_mlme_mcgrp.id, gfp);
6688 return;
6690 nla_put_failure:
6691 genlmsg_cancel(msg, hdr);
6692 nlmsg_free(msg);
6695 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
6696 struct net_device *netdev, const u8 *buf,
6697 size_t len, gfp_t gfp)
6699 nl80211_send_mlme_event(rdev, netdev, buf, len,
6700 NL80211_CMD_AUTHENTICATE, gfp);
6703 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6704 struct net_device *netdev, const u8 *buf,
6705 size_t len, gfp_t gfp)
6707 nl80211_send_mlme_event(rdev, netdev, buf, len,
6708 NL80211_CMD_ASSOCIATE, gfp);
6711 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
6712 struct net_device *netdev, const u8 *buf,
6713 size_t len, gfp_t gfp)
6715 nl80211_send_mlme_event(rdev, netdev, buf, len,
6716 NL80211_CMD_DEAUTHENTICATE, gfp);
6719 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6720 struct net_device *netdev, const u8 *buf,
6721 size_t len, gfp_t gfp)
6723 nl80211_send_mlme_event(rdev, netdev, buf, len,
6724 NL80211_CMD_DISASSOCIATE, gfp);
6727 void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6728 struct net_device *netdev, const u8 *buf,
6729 size_t len, gfp_t gfp)
6731 nl80211_send_mlme_event(rdev, netdev, buf, len,
6732 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6735 void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6736 struct net_device *netdev, const u8 *buf,
6737 size_t len, gfp_t gfp)
6739 nl80211_send_mlme_event(rdev, netdev, buf, len,
6740 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6743 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6744 struct net_device *netdev, int cmd,
6745 const u8 *addr, gfp_t gfp)
6747 struct sk_buff *msg;
6748 void *hdr;
6750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6751 if (!msg)
6752 return;
6754 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6755 if (!hdr) {
6756 nlmsg_free(msg);
6757 return;
6760 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6761 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6762 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6763 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6765 if (genlmsg_end(msg, hdr) < 0) {
6766 nlmsg_free(msg);
6767 return;
6770 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6771 nl80211_mlme_mcgrp.id, gfp);
6772 return;
6774 nla_put_failure:
6775 genlmsg_cancel(msg, hdr);
6776 nlmsg_free(msg);
6779 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
6780 struct net_device *netdev, const u8 *addr,
6781 gfp_t gfp)
6783 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
6784 addr, gfp);
6787 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
6788 struct net_device *netdev, const u8 *addr,
6789 gfp_t gfp)
6791 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6792 addr, gfp);
6795 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6796 struct net_device *netdev, const u8 *bssid,
6797 const u8 *req_ie, size_t req_ie_len,
6798 const u8 *resp_ie, size_t resp_ie_len,
6799 u16 status, gfp_t gfp)
6801 struct sk_buff *msg;
6802 void *hdr;
6804 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6805 if (!msg)
6806 return;
6808 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6809 if (!hdr) {
6810 nlmsg_free(msg);
6811 return;
6814 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6815 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6816 if (bssid)
6817 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6818 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6819 if (req_ie)
6820 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6821 if (resp_ie)
6822 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6824 if (genlmsg_end(msg, hdr) < 0) {
6825 nlmsg_free(msg);
6826 return;
6829 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6830 nl80211_mlme_mcgrp.id, gfp);
6831 return;
6833 nla_put_failure:
6834 genlmsg_cancel(msg, hdr);
6835 nlmsg_free(msg);
6839 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6840 struct net_device *netdev, const u8 *bssid,
6841 const u8 *req_ie, size_t req_ie_len,
6842 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6844 struct sk_buff *msg;
6845 void *hdr;
6847 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6848 if (!msg)
6849 return;
6851 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6852 if (!hdr) {
6853 nlmsg_free(msg);
6854 return;
6857 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6858 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6859 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6860 if (req_ie)
6861 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6862 if (resp_ie)
6863 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6865 if (genlmsg_end(msg, hdr) < 0) {
6866 nlmsg_free(msg);
6867 return;
6870 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6871 nl80211_mlme_mcgrp.id, gfp);
6872 return;
6874 nla_put_failure:
6875 genlmsg_cancel(msg, hdr);
6876 nlmsg_free(msg);
6880 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6881 struct net_device *netdev, u16 reason,
6882 const u8 *ie, size_t ie_len, bool from_ap)
6884 struct sk_buff *msg;
6885 void *hdr;
6887 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6888 if (!msg)
6889 return;
6891 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6892 if (!hdr) {
6893 nlmsg_free(msg);
6894 return;
6897 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6898 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6899 if (from_ap && reason)
6900 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6901 if (from_ap)
6902 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6903 if (ie)
6904 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6906 if (genlmsg_end(msg, hdr) < 0) {
6907 nlmsg_free(msg);
6908 return;
6911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6912 nl80211_mlme_mcgrp.id, GFP_KERNEL);
6913 return;
6915 nla_put_failure:
6916 genlmsg_cancel(msg, hdr);
6917 nlmsg_free(msg);
6921 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6922 struct net_device *netdev, const u8 *bssid,
6923 gfp_t gfp)
6925 struct sk_buff *msg;
6926 void *hdr;
6928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6929 if (!msg)
6930 return;
6932 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6933 if (!hdr) {
6934 nlmsg_free(msg);
6935 return;
6938 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6939 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6940 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6942 if (genlmsg_end(msg, hdr) < 0) {
6943 nlmsg_free(msg);
6944 return;
6947 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6948 nl80211_mlme_mcgrp.id, gfp);
6949 return;
6951 nla_put_failure:
6952 genlmsg_cancel(msg, hdr);
6953 nlmsg_free(msg);
6956 void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6957 struct net_device *netdev,
6958 const u8 *macaddr, const u8* ie, u8 ie_len,
6959 gfp_t gfp)
6961 struct sk_buff *msg;
6962 void *hdr;
6964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6965 if (!msg)
6966 return;
6968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6969 if (!hdr) {
6970 nlmsg_free(msg);
6971 return;
6974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6976 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6977 if (ie_len && ie)
6978 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6980 if (genlmsg_end(msg, hdr) < 0) {
6981 nlmsg_free(msg);
6982 return;
6985 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6986 nl80211_mlme_mcgrp.id, gfp);
6987 return;
6989 nla_put_failure:
6990 genlmsg_cancel(msg, hdr);
6991 nlmsg_free(msg);
6994 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6995 struct net_device *netdev, const u8 *addr,
6996 enum nl80211_key_type key_type, int key_id,
6997 const u8 *tsc, gfp_t gfp)
6999 struct sk_buff *msg;
7000 void *hdr;
7002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7003 if (!msg)
7004 return;
7006 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7007 if (!hdr) {
7008 nlmsg_free(msg);
7009 return;
7012 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7013 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7014 if (addr)
7015 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7016 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
7017 if (key_id != -1)
7018 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
7019 if (tsc)
7020 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7022 if (genlmsg_end(msg, hdr) < 0) {
7023 nlmsg_free(msg);
7024 return;
7027 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7028 nl80211_mlme_mcgrp.id, gfp);
7029 return;
7031 nla_put_failure:
7032 genlmsg_cancel(msg, hdr);
7033 nlmsg_free(msg);
7036 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7037 struct ieee80211_channel *channel_before,
7038 struct ieee80211_channel *channel_after)
7040 struct sk_buff *msg;
7041 void *hdr;
7042 struct nlattr *nl_freq;
7044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
7045 if (!msg)
7046 return;
7048 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7049 if (!hdr) {
7050 nlmsg_free(msg);
7051 return;
7055 * Since we are applying the beacon hint to a wiphy we know its
7056 * wiphy_idx is valid
7058 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7060 /* Before */
7061 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7062 if (!nl_freq)
7063 goto nla_put_failure;
7064 if (nl80211_msg_put_channel(msg, channel_before))
7065 goto nla_put_failure;
7066 nla_nest_end(msg, nl_freq);
7068 /* After */
7069 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7070 if (!nl_freq)
7071 goto nla_put_failure;
7072 if (nl80211_msg_put_channel(msg, channel_after))
7073 goto nla_put_failure;
7074 nla_nest_end(msg, nl_freq);
7076 if (genlmsg_end(msg, hdr) < 0) {
7077 nlmsg_free(msg);
7078 return;
7081 rcu_read_lock();
7082 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7083 GFP_ATOMIC);
7084 rcu_read_unlock();
7086 return;
7088 nla_put_failure:
7089 genlmsg_cancel(msg, hdr);
7090 nlmsg_free(msg);
7093 static void nl80211_send_remain_on_chan_event(
7094 int cmd, struct cfg80211_registered_device *rdev,
7095 struct net_device *netdev, u64 cookie,
7096 struct ieee80211_channel *chan,
7097 enum nl80211_channel_type channel_type,
7098 unsigned int duration, gfp_t gfp)
7100 struct sk_buff *msg;
7101 void *hdr;
7103 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7104 if (!msg)
7105 return;
7107 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7108 if (!hdr) {
7109 nlmsg_free(msg);
7110 return;
7113 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7114 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7115 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7116 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7117 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7119 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7120 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7122 if (genlmsg_end(msg, hdr) < 0) {
7123 nlmsg_free(msg);
7124 return;
7127 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7128 nl80211_mlme_mcgrp.id, gfp);
7129 return;
7131 nla_put_failure:
7132 genlmsg_cancel(msg, hdr);
7133 nlmsg_free(msg);
7136 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7137 struct net_device *netdev, u64 cookie,
7138 struct ieee80211_channel *chan,
7139 enum nl80211_channel_type channel_type,
7140 unsigned int duration, gfp_t gfp)
7142 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7143 rdev, netdev, cookie, chan,
7144 channel_type, duration, gfp);
7147 void nl80211_send_remain_on_channel_cancel(
7148 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7149 u64 cookie, struct ieee80211_channel *chan,
7150 enum nl80211_channel_type channel_type, gfp_t gfp)
7152 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7153 rdev, netdev, cookie, chan,
7154 channel_type, 0, gfp);
7157 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7158 struct net_device *dev, const u8 *mac_addr,
7159 struct station_info *sinfo, gfp_t gfp)
7161 struct sk_buff *msg;
7163 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7164 if (!msg)
7165 return;
7167 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7168 nlmsg_free(msg);
7169 return;
7172 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7173 nl80211_mlme_mcgrp.id, gfp);
7176 void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7177 struct net_device *dev, const u8 *mac_addr,
7178 gfp_t gfp)
7180 struct sk_buff *msg;
7181 void *hdr;
7183 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7184 if (!msg)
7185 return;
7187 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7188 if (!hdr) {
7189 nlmsg_free(msg);
7190 return;
7193 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7194 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7196 if (genlmsg_end(msg, hdr) < 0) {
7197 nlmsg_free(msg);
7198 return;
7201 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7202 nl80211_mlme_mcgrp.id, gfp);
7203 return;
7205 nla_put_failure:
7206 genlmsg_cancel(msg, hdr);
7207 nlmsg_free(msg);
7210 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7211 struct net_device *netdev, u32 nlpid,
7212 int freq, const u8 *buf, size_t len, gfp_t gfp)
7214 struct sk_buff *msg;
7215 void *hdr;
7216 int err;
7218 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7219 if (!msg)
7220 return -ENOMEM;
7222 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7223 if (!hdr) {
7224 nlmsg_free(msg);
7225 return -ENOMEM;
7228 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7229 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7230 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7231 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7233 err = genlmsg_end(msg, hdr);
7234 if (err < 0) {
7235 nlmsg_free(msg);
7236 return err;
7239 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7240 if (err < 0)
7241 return err;
7242 return 0;
7244 nla_put_failure:
7245 genlmsg_cancel(msg, hdr);
7246 nlmsg_free(msg);
7247 return -ENOBUFS;
7250 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7251 struct net_device *netdev, u64 cookie,
7252 const u8 *buf, size_t len, bool ack,
7253 gfp_t gfp)
7255 struct sk_buff *msg;
7256 void *hdr;
7258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7259 if (!msg)
7260 return;
7262 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
7263 if (!hdr) {
7264 nlmsg_free(msg);
7265 return;
7268 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7269 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7270 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7271 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7272 if (ack)
7273 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7275 if (genlmsg_end(msg, hdr) < 0) {
7276 nlmsg_free(msg);
7277 return;
7280 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7281 return;
7283 nla_put_failure:
7284 genlmsg_cancel(msg, hdr);
7285 nlmsg_free(msg);
7288 void
7289 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7290 struct net_device *netdev,
7291 enum nl80211_cqm_rssi_threshold_event rssi_event,
7292 gfp_t gfp)
7294 struct sk_buff *msg;
7295 struct nlattr *pinfoattr;
7296 void *hdr;
7298 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7299 if (!msg)
7300 return;
7302 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7303 if (!hdr) {
7304 nlmsg_free(msg);
7305 return;
7308 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7309 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7311 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7312 if (!pinfoattr)
7313 goto nla_put_failure;
7315 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7316 rssi_event);
7318 nla_nest_end(msg, pinfoattr);
7320 if (genlmsg_end(msg, hdr) < 0) {
7321 nlmsg_free(msg);
7322 return;
7325 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7326 nl80211_mlme_mcgrp.id, gfp);
7327 return;
7329 nla_put_failure:
7330 genlmsg_cancel(msg, hdr);
7331 nlmsg_free(msg);
7334 void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7335 struct net_device *netdev, const u8 *bssid,
7336 const u8 *replay_ctr, gfp_t gfp)
7338 struct sk_buff *msg;
7339 struct nlattr *rekey_attr;
7340 void *hdr;
7342 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7343 if (!msg)
7344 return;
7346 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7347 if (!hdr) {
7348 nlmsg_free(msg);
7349 return;
7352 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7353 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7354 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7356 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7357 if (!rekey_attr)
7358 goto nla_put_failure;
7360 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7361 NL80211_REPLAY_CTR_LEN, replay_ctr);
7363 nla_nest_end(msg, rekey_attr);
7365 if (genlmsg_end(msg, hdr) < 0) {
7366 nlmsg_free(msg);
7367 return;
7370 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7371 nl80211_mlme_mcgrp.id, gfp);
7372 return;
7374 nla_put_failure:
7375 genlmsg_cancel(msg, hdr);
7376 nlmsg_free(msg);
7379 void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7380 struct net_device *netdev, int index,
7381 const u8 *bssid, bool preauth, gfp_t gfp)
7383 struct sk_buff *msg;
7384 struct nlattr *attr;
7385 void *hdr;
7387 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7388 if (!msg)
7389 return;
7391 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7392 if (!hdr) {
7393 nlmsg_free(msg);
7394 return;
7397 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7398 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7400 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7401 if (!attr)
7402 goto nla_put_failure;
7404 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7405 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7406 if (preauth)
7407 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7409 nla_nest_end(msg, attr);
7411 if (genlmsg_end(msg, hdr) < 0) {
7412 nlmsg_free(msg);
7413 return;
7416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7417 nl80211_mlme_mcgrp.id, gfp);
7418 return;
7420 nla_put_failure:
7421 genlmsg_cancel(msg, hdr);
7422 nlmsg_free(msg);
7425 void
7426 nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7427 struct net_device *netdev, const u8 *peer,
7428 u32 num_packets, gfp_t gfp)
7430 struct sk_buff *msg;
7431 struct nlattr *pinfoattr;
7432 void *hdr;
7434 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7435 if (!msg)
7436 return;
7438 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7439 if (!hdr) {
7440 nlmsg_free(msg);
7441 return;
7444 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7445 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7446 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7448 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7449 if (!pinfoattr)
7450 goto nla_put_failure;
7452 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7454 nla_nest_end(msg, pinfoattr);
7456 if (genlmsg_end(msg, hdr) < 0) {
7457 nlmsg_free(msg);
7458 return;
7461 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7462 nl80211_mlme_mcgrp.id, gfp);
7463 return;
7465 nla_put_failure:
7466 genlmsg_cancel(msg, hdr);
7467 nlmsg_free(msg);
7470 static int nl80211_netlink_notify(struct notifier_block * nb,
7471 unsigned long state,
7472 void *_notify)
7474 struct netlink_notify *notify = _notify;
7475 struct cfg80211_registered_device *rdev;
7476 struct wireless_dev *wdev;
7478 if (state != NETLINK_URELEASE)
7479 return NOTIFY_DONE;
7481 rcu_read_lock();
7483 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7484 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
7485 cfg80211_mlme_unregister_socket(wdev, notify->pid);
7487 rcu_read_unlock();
7489 return NOTIFY_DONE;
7492 static struct notifier_block nl80211_netlink_notifier = {
7493 .notifier_call = nl80211_netlink_notify,
7496 /* initialisation/exit functions */
7498 int nl80211_init(void)
7500 int err;
7502 err = genl_register_family_with_ops(&nl80211_fam,
7503 nl80211_ops, ARRAY_SIZE(nl80211_ops));
7504 if (err)
7505 return err;
7507 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7508 if (err)
7509 goto err_out;
7511 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7512 if (err)
7513 goto err_out;
7515 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7516 if (err)
7517 goto err_out;
7519 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7520 if (err)
7521 goto err_out;
7523 #ifdef CONFIG_NL80211_TESTMODE
7524 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7525 if (err)
7526 goto err_out;
7527 #endif
7529 err = netlink_register_notifier(&nl80211_netlink_notifier);
7530 if (err)
7531 goto err_out;
7533 return 0;
7534 err_out:
7535 genl_unregister_family(&nl80211_fam);
7536 return err;
7539 void nl80211_exit(void)
7541 netlink_unregister_notifier(&nl80211_netlink_notifier);
7542 genl_unregister_family(&nl80211_fam);