Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / net / wireless / nl80211.c
blobbf945c9357bd7bcf1aa3721f2e0c556b58a6e5bd
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_ifindex(struct net *netns, struct nlattr **attrs,
51 struct cfg80211_registered_device **rdev,
52 struct net_device **dev)
54 int ifindex;
56 if (!attrs[NL80211_ATTR_IFINDEX])
57 return -EINVAL;
59 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
60 *dev = dev_get_by_index(netns, ifindex);
61 if (!*dev)
62 return -ENODEV;
64 *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
65 if (IS_ERR(*rdev)) {
66 dev_put(*dev);
67 return PTR_ERR(*rdev);
70 return 0;
73 /* policy for the attributes */
74 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
75 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
77 .len = 20-1 },
78 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
79 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
80 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
81 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
82 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
84 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
87 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
88 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
91 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
94 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
95 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
96 .len = WLAN_MAX_KEY_LEN },
97 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
98 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
99 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
100 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
101 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
103 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
104 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
105 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
106 .len = IEEE80211_MAX_DATA_LEN },
107 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
108 .len = IEEE80211_MAX_DATA_LEN },
109 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
110 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
111 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
112 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
114 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
115 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
116 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
117 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_MESH_ID_LEN },
119 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
121 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
125 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128 .len = NL80211_MAX_SUPP_RATES },
129 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
131 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
132 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
134 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
136 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_DATA_LEN },
139 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
142 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
143 .len = IEEE80211_MAX_SSID_LEN },
144 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
145 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
146 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
147 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
148 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
149 [NL80211_ATTR_STA_FLAGS2] = {
150 .len = sizeof(struct nl80211_sta_flag_update),
152 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
153 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
155 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
158 [NL80211_ATTR_PID] = { .type = NLA_U32 },
159 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
160 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161 .len = WLAN_PMKID_LEN },
162 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
164 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
165 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166 .len = IEEE80211_MAX_DATA_LEN },
167 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
168 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
169 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
170 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
171 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
172 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
174 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
175 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
177 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
178 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
179 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
180 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
181 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
182 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
183 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
184 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
185 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
186 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
187 .len = IEEE80211_MAX_DATA_LEN },
188 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
190 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
191 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
192 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
193 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
194 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
195 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
196 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
197 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
198 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
199 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200 .len = IEEE80211_MAX_DATA_LEN },
201 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
202 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204 .len = NL80211_HT_CAPABILITY_LEN
206 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
209 /* policy for the key attributes */
210 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
211 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
212 [NL80211_KEY_IDX] = { .type = NLA_U8 },
213 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
214 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
215 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
216 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
217 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
218 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221 /* policy for the key default flags */
222 static const struct nla_policy
223 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
224 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
225 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
228 /* policy for WoWLAN attributes */
229 static const struct nla_policy
230 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
231 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
232 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
233 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
234 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
235 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
236 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
237 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
238 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
241 /* policy for GTK rekey offload attributes */
242 static const struct nla_policy
243 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
244 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
245 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
246 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
249 static const struct nla_policy
250 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
251 [NL80211_ATTR_SCHED_SCAN_MATCH_SSID] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_SSID_LEN },
255 /* ifidx get helper */
256 static int nl80211_get_ifidx(struct netlink_callback *cb)
258 int res;
260 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
261 nl80211_fam.attrbuf, nl80211_fam.maxattr,
262 nl80211_policy);
263 if (res)
264 return res;
266 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
267 return -EINVAL;
269 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
270 if (!res)
271 return -EINVAL;
272 return res;
275 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
276 struct netlink_callback *cb,
277 struct cfg80211_registered_device **rdev,
278 struct net_device **dev)
280 int ifidx = cb->args[0];
281 int err;
283 if (!ifidx)
284 ifidx = nl80211_get_ifidx(cb);
285 if (ifidx < 0)
286 return ifidx;
288 cb->args[0] = ifidx;
290 rtnl_lock();
292 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
293 if (!*dev) {
294 err = -ENODEV;
295 goto out_rtnl;
298 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
299 if (IS_ERR(*rdev)) {
300 err = PTR_ERR(*rdev);
301 goto out_rtnl;
304 return 0;
305 out_rtnl:
306 rtnl_unlock();
307 return err;
310 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
312 cfg80211_unlock_rdev(rdev);
313 rtnl_unlock();
316 /* IE validation */
317 static bool is_valid_ie_attr(const struct nlattr *attr)
319 const u8 *pos;
320 int len;
322 if (!attr)
323 return true;
325 pos = nla_data(attr);
326 len = nla_len(attr);
328 while (len) {
329 u8 elemlen;
331 if (len < 2)
332 return false;
333 len -= 2;
335 elemlen = pos[1];
336 if (elemlen > len)
337 return false;
339 len -= elemlen;
340 pos += 2 + elemlen;
343 return true;
346 /* message building helper */
347 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
348 int flags, u8 cmd)
350 /* since there is no private header just add the generic one */
351 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
354 static int nl80211_msg_put_channel(struct sk_buff *msg,
355 struct ieee80211_channel *chan)
357 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
358 chan->center_freq);
360 if (chan->flags & IEEE80211_CHAN_DISABLED)
361 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
362 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
363 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
364 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
365 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
366 if (chan->flags & IEEE80211_CHAN_RADAR)
367 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
369 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
370 DBM_TO_MBM(chan->max_power));
372 return 0;
374 nla_put_failure:
375 return -ENOBUFS;
378 /* netlink command implementations */
380 struct key_parse {
381 struct key_params p;
382 int idx;
383 int type;
384 bool def, defmgmt;
385 bool def_uni, def_multi;
388 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
390 struct nlattr *tb[NL80211_KEY_MAX + 1];
391 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
392 nl80211_key_policy);
393 if (err)
394 return err;
396 k->def = !!tb[NL80211_KEY_DEFAULT];
397 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
399 if (k->def) {
400 k->def_uni = true;
401 k->def_multi = true;
403 if (k->defmgmt)
404 k->def_multi = true;
406 if (tb[NL80211_KEY_IDX])
407 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
409 if (tb[NL80211_KEY_DATA]) {
410 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
411 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
414 if (tb[NL80211_KEY_SEQ]) {
415 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
416 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
419 if (tb[NL80211_KEY_CIPHER])
420 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
422 if (tb[NL80211_KEY_TYPE]) {
423 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
424 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
425 return -EINVAL;
428 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
429 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
430 int err = nla_parse_nested(kdt,
431 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
432 tb[NL80211_KEY_DEFAULT_TYPES],
433 nl80211_key_default_policy);
434 if (err)
435 return err;
437 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
438 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
441 return 0;
444 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
446 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
447 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
448 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
451 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
452 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
453 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
456 if (info->attrs[NL80211_ATTR_KEY_IDX])
457 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
459 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
460 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
462 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
463 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
465 if (k->def) {
466 k->def_uni = true;
467 k->def_multi = true;
469 if (k->defmgmt)
470 k->def_multi = true;
472 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
473 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
474 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
475 return -EINVAL;
478 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
479 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
480 int err = nla_parse_nested(
481 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
482 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
483 nl80211_key_default_policy);
484 if (err)
485 return err;
487 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
488 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
491 return 0;
494 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
496 int err;
498 memset(k, 0, sizeof(*k));
499 k->idx = -1;
500 k->type = -1;
502 if (info->attrs[NL80211_ATTR_KEY])
503 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
504 else
505 err = nl80211_parse_key_old(info, k);
507 if (err)
508 return err;
510 if (k->def && k->defmgmt)
511 return -EINVAL;
513 if (k->defmgmt) {
514 if (k->def_uni || !k->def_multi)
515 return -EINVAL;
518 if (k->idx != -1) {
519 if (k->defmgmt) {
520 if (k->idx < 4 || k->idx > 5)
521 return -EINVAL;
522 } else if (k->def) {
523 if (k->idx < 0 || k->idx > 3)
524 return -EINVAL;
525 } else {
526 if (k->idx < 0 || k->idx > 5)
527 return -EINVAL;
531 return 0;
534 static struct cfg80211_cached_keys *
535 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
536 struct nlattr *keys)
538 struct key_parse parse;
539 struct nlattr *key;
540 struct cfg80211_cached_keys *result;
541 int rem, err, def = 0;
543 result = kzalloc(sizeof(*result), GFP_KERNEL);
544 if (!result)
545 return ERR_PTR(-ENOMEM);
547 result->def = -1;
548 result->defmgmt = -1;
550 nla_for_each_nested(key, keys, rem) {
551 memset(&parse, 0, sizeof(parse));
552 parse.idx = -1;
554 err = nl80211_parse_key_new(key, &parse);
555 if (err)
556 goto error;
557 err = -EINVAL;
558 if (!parse.p.key)
559 goto error;
560 if (parse.idx < 0 || parse.idx > 4)
561 goto error;
562 if (parse.def) {
563 if (def)
564 goto error;
565 def = 1;
566 result->def = parse.idx;
567 if (!parse.def_uni || !parse.def_multi)
568 goto error;
569 } else if (parse.defmgmt)
570 goto error;
571 err = cfg80211_validate_key_settings(rdev, &parse.p,
572 parse.idx, false, NULL);
573 if (err)
574 goto error;
575 result->params[parse.idx].cipher = parse.p.cipher;
576 result->params[parse.idx].key_len = parse.p.key_len;
577 result->params[parse.idx].key = result->data[parse.idx];
578 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
581 return result;
582 error:
583 kfree(result);
584 return ERR_PTR(err);
587 static int nl80211_key_allowed(struct wireless_dev *wdev)
589 ASSERT_WDEV_LOCK(wdev);
591 switch (wdev->iftype) {
592 case NL80211_IFTYPE_AP:
593 case NL80211_IFTYPE_AP_VLAN:
594 case NL80211_IFTYPE_P2P_GO:
595 case NL80211_IFTYPE_MESH_POINT:
596 break;
597 case NL80211_IFTYPE_ADHOC:
598 if (!wdev->current_bss)
599 return -ENOLINK;
600 break;
601 case NL80211_IFTYPE_STATION:
602 case NL80211_IFTYPE_P2P_CLIENT:
603 if (wdev->sme_state != CFG80211_SME_CONNECTED)
604 return -ENOLINK;
605 break;
606 default:
607 return -EINVAL;
610 return 0;
613 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
615 struct nlattr *nl_modes = nla_nest_start(msg, attr);
616 int i;
618 if (!nl_modes)
619 goto nla_put_failure;
621 i = 0;
622 while (ifmodes) {
623 if (ifmodes & 1)
624 NLA_PUT_FLAG(msg, i);
625 ifmodes >>= 1;
626 i++;
629 nla_nest_end(msg, nl_modes);
630 return 0;
632 nla_put_failure:
633 return -ENOBUFS;
636 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
637 struct sk_buff *msg)
639 struct nlattr *nl_combis;
640 int i, j;
642 nl_combis = nla_nest_start(msg,
643 NL80211_ATTR_INTERFACE_COMBINATIONS);
644 if (!nl_combis)
645 goto nla_put_failure;
647 for (i = 0; i < wiphy->n_iface_combinations; i++) {
648 const struct ieee80211_iface_combination *c;
649 struct nlattr *nl_combi, *nl_limits;
651 c = &wiphy->iface_combinations[i];
653 nl_combi = nla_nest_start(msg, i + 1);
654 if (!nl_combi)
655 goto nla_put_failure;
657 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
658 if (!nl_limits)
659 goto nla_put_failure;
661 for (j = 0; j < c->n_limits; j++) {
662 struct nlattr *nl_limit;
664 nl_limit = nla_nest_start(msg, j + 1);
665 if (!nl_limit)
666 goto nla_put_failure;
667 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
668 c->limits[j].max);
669 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
670 c->limits[j].types))
671 goto nla_put_failure;
672 nla_nest_end(msg, nl_limit);
675 nla_nest_end(msg, nl_limits);
677 if (c->beacon_int_infra_match)
678 NLA_PUT_FLAG(msg,
679 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
680 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
681 c->num_different_channels);
682 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
683 c->max_interfaces);
685 nla_nest_end(msg, nl_combi);
688 nla_nest_end(msg, nl_combis);
690 return 0;
691 nla_put_failure:
692 return -ENOBUFS;
695 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
696 struct cfg80211_registered_device *dev)
698 void *hdr;
699 struct nlattr *nl_bands, *nl_band;
700 struct nlattr *nl_freqs, *nl_freq;
701 struct nlattr *nl_rates, *nl_rate;
702 struct nlattr *nl_cmds;
703 enum ieee80211_band band;
704 struct ieee80211_channel *chan;
705 struct ieee80211_rate *rate;
706 int i;
707 const struct ieee80211_txrx_stypes *mgmt_stypes =
708 dev->wiphy.mgmt_stypes;
710 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
711 if (!hdr)
712 return -1;
714 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
715 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
717 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
718 cfg80211_rdev_list_generation);
720 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
721 dev->wiphy.retry_short);
722 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
723 dev->wiphy.retry_long);
724 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
725 dev->wiphy.frag_threshold);
726 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
727 dev->wiphy.rts_threshold);
728 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
729 dev->wiphy.coverage_class);
730 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
731 dev->wiphy.max_scan_ssids);
732 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
733 dev->wiphy.max_sched_scan_ssids);
734 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
735 dev->wiphy.max_scan_ie_len);
736 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
737 dev->wiphy.max_sched_scan_ie_len);
738 NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
739 dev->wiphy.max_match_sets);
741 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
742 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
743 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
744 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
745 if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
746 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
747 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
748 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
749 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
750 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
751 if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
752 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
754 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
755 sizeof(u32) * dev->wiphy.n_cipher_suites,
756 dev->wiphy.cipher_suites);
758 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
759 dev->wiphy.max_num_pmkids);
761 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
762 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
764 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
765 dev->wiphy.available_antennas_tx);
766 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
767 dev->wiphy.available_antennas_rx);
769 if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
770 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
771 dev->wiphy.probe_resp_offload);
773 if ((dev->wiphy.available_antennas_tx ||
774 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
775 u32 tx_ant = 0, rx_ant = 0;
776 int res;
777 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
778 if (!res) {
779 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
784 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
785 dev->wiphy.interface_modes))
786 goto nla_put_failure;
788 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
789 if (!nl_bands)
790 goto nla_put_failure;
792 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
793 if (!dev->wiphy.bands[band])
794 continue;
796 nl_band = nla_nest_start(msg, band);
797 if (!nl_band)
798 goto nla_put_failure;
800 /* add HT info */
801 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
802 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
803 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
804 &dev->wiphy.bands[band]->ht_cap.mcs);
805 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
806 dev->wiphy.bands[band]->ht_cap.cap);
807 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
808 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
809 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
810 dev->wiphy.bands[band]->ht_cap.ampdu_density);
813 /* add frequencies */
814 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
815 if (!nl_freqs)
816 goto nla_put_failure;
818 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
819 nl_freq = nla_nest_start(msg, i);
820 if (!nl_freq)
821 goto nla_put_failure;
823 chan = &dev->wiphy.bands[band]->channels[i];
825 if (nl80211_msg_put_channel(msg, chan))
826 goto nla_put_failure;
828 nla_nest_end(msg, nl_freq);
831 nla_nest_end(msg, nl_freqs);
833 /* add bitrates */
834 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
835 if (!nl_rates)
836 goto nla_put_failure;
838 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
839 nl_rate = nla_nest_start(msg, i);
840 if (!nl_rate)
841 goto nla_put_failure;
843 rate = &dev->wiphy.bands[band]->bitrates[i];
844 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
845 rate->bitrate);
846 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
847 NLA_PUT_FLAG(msg,
848 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
850 nla_nest_end(msg, nl_rate);
853 nla_nest_end(msg, nl_rates);
855 nla_nest_end(msg, nl_band);
857 nla_nest_end(msg, nl_bands);
859 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
860 if (!nl_cmds)
861 goto nla_put_failure;
863 i = 0;
864 #define CMD(op, n) \
865 do { \
866 if (dev->ops->op) { \
867 i++; \
868 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
870 } while (0)
872 CMD(add_virtual_intf, NEW_INTERFACE);
873 CMD(change_virtual_intf, SET_INTERFACE);
874 CMD(add_key, NEW_KEY);
875 CMD(add_beacon, NEW_BEACON);
876 CMD(add_station, NEW_STATION);
877 CMD(add_mpath, NEW_MPATH);
878 CMD(update_mesh_config, SET_MESH_CONFIG);
879 CMD(change_bss, SET_BSS);
880 CMD(auth, AUTHENTICATE);
881 CMD(assoc, ASSOCIATE);
882 CMD(deauth, DEAUTHENTICATE);
883 CMD(disassoc, DISASSOCIATE);
884 CMD(join_ibss, JOIN_IBSS);
885 CMD(join_mesh, JOIN_MESH);
886 CMD(set_pmksa, SET_PMKSA);
887 CMD(del_pmksa, DEL_PMKSA);
888 CMD(flush_pmksa, FLUSH_PMKSA);
889 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
890 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
891 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
892 CMD(mgmt_tx, FRAME);
893 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
894 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
895 i++;
896 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
898 CMD(set_channel, SET_CHANNEL);
899 CMD(set_wds_peer, SET_WDS_PEER);
900 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
901 CMD(tdls_mgmt, TDLS_MGMT);
902 CMD(tdls_oper, TDLS_OPER);
904 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
905 CMD(sched_scan_start, START_SCHED_SCAN);
906 CMD(probe_client, PROBE_CLIENT);
907 CMD(set_noack_map, SET_NOACK_MAP);
908 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
909 i++;
910 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
913 #ifdef CONFIG_NL80211_TESTMODE
914 CMD(testmode_cmd, TESTMODE);
915 #endif
917 #undef CMD
919 if (dev->ops->connect || dev->ops->auth) {
920 i++;
921 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
924 if (dev->ops->disconnect || dev->ops->deauth) {
925 i++;
926 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
929 nla_nest_end(msg, nl_cmds);
931 if (dev->ops->remain_on_channel &&
932 dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
933 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
934 dev->wiphy.max_remain_on_channel_duration);
936 if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
937 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
939 if (mgmt_stypes) {
940 u16 stypes;
941 struct nlattr *nl_ftypes, *nl_ifs;
942 enum nl80211_iftype ift;
944 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
945 if (!nl_ifs)
946 goto nla_put_failure;
948 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
949 nl_ftypes = nla_nest_start(msg, ift);
950 if (!nl_ftypes)
951 goto nla_put_failure;
952 i = 0;
953 stypes = mgmt_stypes[ift].tx;
954 while (stypes) {
955 if (stypes & 1)
956 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
957 (i << 4) | IEEE80211_FTYPE_MGMT);
958 stypes >>= 1;
959 i++;
961 nla_nest_end(msg, nl_ftypes);
964 nla_nest_end(msg, nl_ifs);
966 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
967 if (!nl_ifs)
968 goto nla_put_failure;
970 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
971 nl_ftypes = nla_nest_start(msg, ift);
972 if (!nl_ftypes)
973 goto nla_put_failure;
974 i = 0;
975 stypes = mgmt_stypes[ift].rx;
976 while (stypes) {
977 if (stypes & 1)
978 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
979 (i << 4) | IEEE80211_FTYPE_MGMT);
980 stypes >>= 1;
981 i++;
983 nla_nest_end(msg, nl_ftypes);
985 nla_nest_end(msg, nl_ifs);
988 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
989 struct nlattr *nl_wowlan;
991 nl_wowlan = nla_nest_start(msg,
992 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
993 if (!nl_wowlan)
994 goto nla_put_failure;
996 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
997 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
998 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
999 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1000 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1001 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
1002 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1003 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1004 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1005 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1006 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1007 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1008 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1009 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1010 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1011 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
1012 if (dev->wiphy.wowlan.n_patterns) {
1013 struct nl80211_wowlan_pattern_support pat = {
1014 .max_patterns = dev->wiphy.wowlan.n_patterns,
1015 .min_pattern_len =
1016 dev->wiphy.wowlan.pattern_min_len,
1017 .max_pattern_len =
1018 dev->wiphy.wowlan.pattern_max_len,
1020 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1021 sizeof(pat), &pat);
1024 nla_nest_end(msg, nl_wowlan);
1027 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1028 dev->wiphy.software_iftypes))
1029 goto nla_put_failure;
1031 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1032 goto nla_put_failure;
1034 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1035 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1036 dev->wiphy.ap_sme_capa);
1038 NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1040 if (dev->wiphy.ht_capa_mod_mask)
1041 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1042 sizeof(*dev->wiphy.ht_capa_mod_mask),
1043 dev->wiphy.ht_capa_mod_mask);
1045 return genlmsg_end(msg, hdr);
1047 nla_put_failure:
1048 genlmsg_cancel(msg, hdr);
1049 return -EMSGSIZE;
1052 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1054 int idx = 0;
1055 int start = cb->args[0];
1056 struct cfg80211_registered_device *dev;
1058 mutex_lock(&cfg80211_mutex);
1059 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
1060 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1061 continue;
1062 if (++idx <= start)
1063 continue;
1064 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1065 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1066 dev) < 0) {
1067 idx--;
1068 break;
1071 mutex_unlock(&cfg80211_mutex);
1073 cb->args[0] = idx;
1075 return skb->len;
1078 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1080 struct sk_buff *msg;
1081 struct cfg80211_registered_device *dev = info->user_ptr[0];
1083 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1084 if (!msg)
1085 return -ENOMEM;
1087 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1088 nlmsg_free(msg);
1089 return -ENOBUFS;
1092 return genlmsg_reply(msg, info);
1095 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1096 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1097 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1098 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1099 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1100 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1103 static int parse_txq_params(struct nlattr *tb[],
1104 struct ieee80211_txq_params *txq_params)
1106 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1107 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1108 !tb[NL80211_TXQ_ATTR_AIFS])
1109 return -EINVAL;
1111 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1112 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1113 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1114 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1115 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1117 return 0;
1120 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1123 * You can only set the channel explicitly for AP, mesh
1124 * and WDS type interfaces; all others have their channel
1125 * managed via their respective "establish a connection"
1126 * command (connect, join, ...)
1128 * Monitors are special as they are normally slaved to
1129 * whatever else is going on, so they behave as though
1130 * you tried setting the wiphy channel itself.
1132 return !wdev ||
1133 wdev->iftype == NL80211_IFTYPE_AP ||
1134 wdev->iftype == NL80211_IFTYPE_WDS ||
1135 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1136 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1137 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1140 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1141 struct wireless_dev *wdev,
1142 struct genl_info *info)
1144 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1145 u32 freq;
1146 int result;
1148 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1149 return -EINVAL;
1151 if (!nl80211_can_set_dev_channel(wdev))
1152 return -EOPNOTSUPP;
1154 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1155 channel_type = nla_get_u32(info->attrs[
1156 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1157 if (channel_type != NL80211_CHAN_NO_HT &&
1158 channel_type != NL80211_CHAN_HT20 &&
1159 channel_type != NL80211_CHAN_HT40PLUS &&
1160 channel_type != NL80211_CHAN_HT40MINUS)
1161 return -EINVAL;
1164 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1166 mutex_lock(&rdev->devlist_mtx);
1167 if (wdev) {
1168 wdev_lock(wdev);
1169 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1170 wdev_unlock(wdev);
1171 } else {
1172 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1174 mutex_unlock(&rdev->devlist_mtx);
1176 return result;
1179 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1181 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1182 struct net_device *netdev = info->user_ptr[1];
1184 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1187 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1190 struct net_device *dev = info->user_ptr[1];
1191 struct wireless_dev *wdev = dev->ieee80211_ptr;
1192 const u8 *bssid;
1194 if (!info->attrs[NL80211_ATTR_MAC])
1195 return -EINVAL;
1197 if (netif_running(dev))
1198 return -EBUSY;
1200 if (!rdev->ops->set_wds_peer)
1201 return -EOPNOTSUPP;
1203 if (wdev->iftype != NL80211_IFTYPE_WDS)
1204 return -EOPNOTSUPP;
1206 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1207 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
1211 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1213 struct cfg80211_registered_device *rdev;
1214 struct net_device *netdev = NULL;
1215 struct wireless_dev *wdev;
1216 int result = 0, rem_txq_params = 0;
1217 struct nlattr *nl_txq_params;
1218 u32 changed;
1219 u8 retry_short = 0, retry_long = 0;
1220 u32 frag_threshold = 0, rts_threshold = 0;
1221 u8 coverage_class = 0;
1224 * Try to find the wiphy and netdev. Normally this
1225 * function shouldn't need the netdev, but this is
1226 * done for backward compatibility -- previously
1227 * setting the channel was done per wiphy, but now
1228 * it is per netdev. Previous userland like hostapd
1229 * also passed a netdev to set_wiphy, so that it is
1230 * possible to let that go to the right netdev!
1232 mutex_lock(&cfg80211_mutex);
1234 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1235 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1237 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1238 if (netdev && netdev->ieee80211_ptr) {
1239 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1240 mutex_lock(&rdev->mtx);
1241 } else
1242 netdev = NULL;
1245 if (!netdev) {
1246 rdev = __cfg80211_rdev_from_info(info);
1247 if (IS_ERR(rdev)) {
1248 mutex_unlock(&cfg80211_mutex);
1249 return PTR_ERR(rdev);
1251 wdev = NULL;
1252 netdev = NULL;
1253 result = 0;
1255 mutex_lock(&rdev->mtx);
1256 } else if (netif_running(netdev) &&
1257 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1258 wdev = netdev->ieee80211_ptr;
1259 else
1260 wdev = NULL;
1263 * end workaround code, by now the rdev is available
1264 * and locked, and wdev may or may not be NULL.
1267 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1268 result = cfg80211_dev_rename(
1269 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1271 mutex_unlock(&cfg80211_mutex);
1273 if (result)
1274 goto bad_res;
1276 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1277 struct ieee80211_txq_params txq_params;
1278 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1280 if (!rdev->ops->set_txq_params) {
1281 result = -EOPNOTSUPP;
1282 goto bad_res;
1285 if (!netdev) {
1286 result = -EINVAL;
1287 goto bad_res;
1290 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1291 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1292 result = -EINVAL;
1293 goto bad_res;
1296 if (!netif_running(netdev)) {
1297 result = -ENETDOWN;
1298 goto bad_res;
1301 nla_for_each_nested(nl_txq_params,
1302 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1303 rem_txq_params) {
1304 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1305 nla_data(nl_txq_params),
1306 nla_len(nl_txq_params),
1307 txq_params_policy);
1308 result = parse_txq_params(tb, &txq_params);
1309 if (result)
1310 goto bad_res;
1312 result = rdev->ops->set_txq_params(&rdev->wiphy,
1313 netdev,
1314 &txq_params);
1315 if (result)
1316 goto bad_res;
1320 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1321 result = __nl80211_set_channel(rdev, wdev, info);
1322 if (result)
1323 goto bad_res;
1326 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1327 enum nl80211_tx_power_setting type;
1328 int idx, mbm = 0;
1330 if (!rdev->ops->set_tx_power) {
1331 result = -EOPNOTSUPP;
1332 goto bad_res;
1335 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1336 type = nla_get_u32(info->attrs[idx]);
1338 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1339 (type != NL80211_TX_POWER_AUTOMATIC)) {
1340 result = -EINVAL;
1341 goto bad_res;
1344 if (type != NL80211_TX_POWER_AUTOMATIC) {
1345 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1346 mbm = nla_get_u32(info->attrs[idx]);
1349 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1350 if (result)
1351 goto bad_res;
1354 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1355 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1356 u32 tx_ant, rx_ant;
1357 if ((!rdev->wiphy.available_antennas_tx &&
1358 !rdev->wiphy.available_antennas_rx) ||
1359 !rdev->ops->set_antenna) {
1360 result = -EOPNOTSUPP;
1361 goto bad_res;
1364 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1365 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1367 /* reject antenna configurations which don't match the
1368 * available antenna masks, except for the "all" mask */
1369 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1370 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1371 result = -EINVAL;
1372 goto bad_res;
1375 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1376 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1378 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1379 if (result)
1380 goto bad_res;
1383 changed = 0;
1385 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1386 retry_short = nla_get_u8(
1387 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1388 if (retry_short == 0) {
1389 result = -EINVAL;
1390 goto bad_res;
1392 changed |= WIPHY_PARAM_RETRY_SHORT;
1395 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1396 retry_long = nla_get_u8(
1397 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1398 if (retry_long == 0) {
1399 result = -EINVAL;
1400 goto bad_res;
1402 changed |= WIPHY_PARAM_RETRY_LONG;
1405 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1406 frag_threshold = nla_get_u32(
1407 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1408 if (frag_threshold < 256) {
1409 result = -EINVAL;
1410 goto bad_res;
1412 if (frag_threshold != (u32) -1) {
1414 * Fragments (apart from the last one) are required to
1415 * have even length. Make the fragmentation code
1416 * simpler by stripping LSB should someone try to use
1417 * odd threshold value.
1419 frag_threshold &= ~0x1;
1421 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1424 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1425 rts_threshold = nla_get_u32(
1426 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1427 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1430 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1431 coverage_class = nla_get_u8(
1432 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1433 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1436 if (changed) {
1437 u8 old_retry_short, old_retry_long;
1438 u32 old_frag_threshold, old_rts_threshold;
1439 u8 old_coverage_class;
1441 if (!rdev->ops->set_wiphy_params) {
1442 result = -EOPNOTSUPP;
1443 goto bad_res;
1446 old_retry_short = rdev->wiphy.retry_short;
1447 old_retry_long = rdev->wiphy.retry_long;
1448 old_frag_threshold = rdev->wiphy.frag_threshold;
1449 old_rts_threshold = rdev->wiphy.rts_threshold;
1450 old_coverage_class = rdev->wiphy.coverage_class;
1452 if (changed & WIPHY_PARAM_RETRY_SHORT)
1453 rdev->wiphy.retry_short = retry_short;
1454 if (changed & WIPHY_PARAM_RETRY_LONG)
1455 rdev->wiphy.retry_long = retry_long;
1456 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1457 rdev->wiphy.frag_threshold = frag_threshold;
1458 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1459 rdev->wiphy.rts_threshold = rts_threshold;
1460 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1461 rdev->wiphy.coverage_class = coverage_class;
1463 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1464 if (result) {
1465 rdev->wiphy.retry_short = old_retry_short;
1466 rdev->wiphy.retry_long = old_retry_long;
1467 rdev->wiphy.frag_threshold = old_frag_threshold;
1468 rdev->wiphy.rts_threshold = old_rts_threshold;
1469 rdev->wiphy.coverage_class = old_coverage_class;
1473 bad_res:
1474 mutex_unlock(&rdev->mtx);
1475 if (netdev)
1476 dev_put(netdev);
1477 return result;
1481 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1482 struct cfg80211_registered_device *rdev,
1483 struct net_device *dev)
1485 void *hdr;
1487 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1488 if (!hdr)
1489 return -1;
1491 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1492 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1493 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1494 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1496 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1497 rdev->devlist_generation ^
1498 (cfg80211_rdev_list_generation << 2));
1500 return genlmsg_end(msg, hdr);
1502 nla_put_failure:
1503 genlmsg_cancel(msg, hdr);
1504 return -EMSGSIZE;
1507 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1509 int wp_idx = 0;
1510 int if_idx = 0;
1511 int wp_start = cb->args[0];
1512 int if_start = cb->args[1];
1513 struct cfg80211_registered_device *rdev;
1514 struct wireless_dev *wdev;
1516 mutex_lock(&cfg80211_mutex);
1517 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1518 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1519 continue;
1520 if (wp_idx < wp_start) {
1521 wp_idx++;
1522 continue;
1524 if_idx = 0;
1526 mutex_lock(&rdev->devlist_mtx);
1527 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1528 if (if_idx < if_start) {
1529 if_idx++;
1530 continue;
1532 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1533 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1534 rdev, wdev->netdev) < 0) {
1535 mutex_unlock(&rdev->devlist_mtx);
1536 goto out;
1538 if_idx++;
1540 mutex_unlock(&rdev->devlist_mtx);
1542 wp_idx++;
1544 out:
1545 mutex_unlock(&cfg80211_mutex);
1547 cb->args[0] = wp_idx;
1548 cb->args[1] = if_idx;
1550 return skb->len;
1553 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1555 struct sk_buff *msg;
1556 struct cfg80211_registered_device *dev = info->user_ptr[0];
1557 struct net_device *netdev = info->user_ptr[1];
1559 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1560 if (!msg)
1561 return -ENOMEM;
1563 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1564 dev, netdev) < 0) {
1565 nlmsg_free(msg);
1566 return -ENOBUFS;
1569 return genlmsg_reply(msg, info);
1572 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1573 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1574 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1575 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1576 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1577 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1580 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1582 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1583 int flag;
1585 *mntrflags = 0;
1587 if (!nla)
1588 return -EINVAL;
1590 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1591 nla, mntr_flags_policy))
1592 return -EINVAL;
1594 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1595 if (flags[flag])
1596 *mntrflags |= (1<<flag);
1598 return 0;
1601 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1602 struct net_device *netdev, u8 use_4addr,
1603 enum nl80211_iftype iftype)
1605 if (!use_4addr) {
1606 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1607 return -EBUSY;
1608 return 0;
1611 switch (iftype) {
1612 case NL80211_IFTYPE_AP_VLAN:
1613 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1614 return 0;
1615 break;
1616 case NL80211_IFTYPE_STATION:
1617 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1618 return 0;
1619 break;
1620 default:
1621 break;
1624 return -EOPNOTSUPP;
1627 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1629 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1630 struct vif_params params;
1631 int err;
1632 enum nl80211_iftype otype, ntype;
1633 struct net_device *dev = info->user_ptr[1];
1634 u32 _flags, *flags = NULL;
1635 bool change = false;
1637 memset(&params, 0, sizeof(params));
1639 otype = ntype = dev->ieee80211_ptr->iftype;
1641 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1642 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1643 if (otype != ntype)
1644 change = true;
1645 if (ntype > NL80211_IFTYPE_MAX)
1646 return -EINVAL;
1649 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1650 struct wireless_dev *wdev = dev->ieee80211_ptr;
1652 if (ntype != NL80211_IFTYPE_MESH_POINT)
1653 return -EINVAL;
1654 if (netif_running(dev))
1655 return -EBUSY;
1657 wdev_lock(wdev);
1658 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1659 IEEE80211_MAX_MESH_ID_LEN);
1660 wdev->mesh_id_up_len =
1661 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1662 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1663 wdev->mesh_id_up_len);
1664 wdev_unlock(wdev);
1667 if (info->attrs[NL80211_ATTR_4ADDR]) {
1668 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1669 change = true;
1670 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1671 if (err)
1672 return err;
1673 } else {
1674 params.use_4addr = -1;
1677 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1678 if (ntype != NL80211_IFTYPE_MONITOR)
1679 return -EINVAL;
1680 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1681 &_flags);
1682 if (err)
1683 return err;
1685 flags = &_flags;
1686 change = true;
1689 if (change)
1690 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1691 else
1692 err = 0;
1694 if (!err && params.use_4addr != -1)
1695 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1697 return err;
1700 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1703 struct vif_params params;
1704 struct net_device *dev;
1705 int err;
1706 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1707 u32 flags;
1709 memset(&params, 0, sizeof(params));
1711 if (!info->attrs[NL80211_ATTR_IFNAME])
1712 return -EINVAL;
1714 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1715 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1716 if (type > NL80211_IFTYPE_MAX)
1717 return -EINVAL;
1720 if (!rdev->ops->add_virtual_intf ||
1721 !(rdev->wiphy.interface_modes & (1 << type)))
1722 return -EOPNOTSUPP;
1724 if (info->attrs[NL80211_ATTR_4ADDR]) {
1725 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1726 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1727 if (err)
1728 return err;
1731 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1732 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1733 &flags);
1734 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
1735 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1736 type, err ? NULL : &flags, &params);
1737 if (IS_ERR(dev))
1738 return PTR_ERR(dev);
1740 if (type == NL80211_IFTYPE_MESH_POINT &&
1741 info->attrs[NL80211_ATTR_MESH_ID]) {
1742 struct wireless_dev *wdev = dev->ieee80211_ptr;
1744 wdev_lock(wdev);
1745 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1746 IEEE80211_MAX_MESH_ID_LEN);
1747 wdev->mesh_id_up_len =
1748 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1749 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1750 wdev->mesh_id_up_len);
1751 wdev_unlock(wdev);
1754 return 0;
1757 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1759 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1760 struct net_device *dev = info->user_ptr[1];
1762 if (!rdev->ops->del_virtual_intf)
1763 return -EOPNOTSUPP;
1765 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1768 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1771 struct net_device *dev = info->user_ptr[1];
1772 u16 noack_map;
1774 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1775 return -EINVAL;
1777 if (!rdev->ops->set_noack_map)
1778 return -EOPNOTSUPP;
1780 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1782 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1785 struct get_key_cookie {
1786 struct sk_buff *msg;
1787 int error;
1788 int idx;
1791 static void get_key_callback(void *c, struct key_params *params)
1793 struct nlattr *key;
1794 struct get_key_cookie *cookie = c;
1796 if (params->key)
1797 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1798 params->key_len, params->key);
1800 if (params->seq)
1801 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1802 params->seq_len, params->seq);
1804 if (params->cipher)
1805 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1806 params->cipher);
1808 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1809 if (!key)
1810 goto nla_put_failure;
1812 if (params->key)
1813 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1814 params->key_len, params->key);
1816 if (params->seq)
1817 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1818 params->seq_len, params->seq);
1820 if (params->cipher)
1821 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1822 params->cipher);
1824 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1826 nla_nest_end(cookie->msg, key);
1828 return;
1829 nla_put_failure:
1830 cookie->error = 1;
1833 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1836 int err;
1837 struct net_device *dev = info->user_ptr[1];
1838 u8 key_idx = 0;
1839 const u8 *mac_addr = NULL;
1840 bool pairwise;
1841 struct get_key_cookie cookie = {
1842 .error = 0,
1844 void *hdr;
1845 struct sk_buff *msg;
1847 if (info->attrs[NL80211_ATTR_KEY_IDX])
1848 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1850 if (key_idx > 5)
1851 return -EINVAL;
1853 if (info->attrs[NL80211_ATTR_MAC])
1854 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1856 pairwise = !!mac_addr;
1857 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1858 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1859 if (kt >= NUM_NL80211_KEYTYPES)
1860 return -EINVAL;
1861 if (kt != NL80211_KEYTYPE_GROUP &&
1862 kt != NL80211_KEYTYPE_PAIRWISE)
1863 return -EINVAL;
1864 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1867 if (!rdev->ops->get_key)
1868 return -EOPNOTSUPP;
1870 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1871 if (!msg)
1872 return -ENOMEM;
1874 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1875 NL80211_CMD_NEW_KEY);
1876 if (IS_ERR(hdr))
1877 return PTR_ERR(hdr);
1879 cookie.msg = msg;
1880 cookie.idx = key_idx;
1882 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1883 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1884 if (mac_addr)
1885 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1887 if (pairwise && mac_addr &&
1888 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1889 return -ENOENT;
1891 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1892 mac_addr, &cookie, get_key_callback);
1894 if (err)
1895 goto free_msg;
1897 if (cookie.error)
1898 goto nla_put_failure;
1900 genlmsg_end(msg, hdr);
1901 return genlmsg_reply(msg, info);
1903 nla_put_failure:
1904 err = -ENOBUFS;
1905 free_msg:
1906 nlmsg_free(msg);
1907 return err;
1910 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1912 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1913 struct key_parse key;
1914 int err;
1915 struct net_device *dev = info->user_ptr[1];
1917 err = nl80211_parse_key(info, &key);
1918 if (err)
1919 return err;
1921 if (key.idx < 0)
1922 return -EINVAL;
1924 /* only support setting default key */
1925 if (!key.def && !key.defmgmt)
1926 return -EINVAL;
1928 wdev_lock(dev->ieee80211_ptr);
1930 if (key.def) {
1931 if (!rdev->ops->set_default_key) {
1932 err = -EOPNOTSUPP;
1933 goto out;
1936 err = nl80211_key_allowed(dev->ieee80211_ptr);
1937 if (err)
1938 goto out;
1940 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1941 key.def_uni, key.def_multi);
1943 if (err)
1944 goto out;
1946 #ifdef CONFIG_CFG80211_WEXT
1947 dev->ieee80211_ptr->wext.default_key = key.idx;
1948 #endif
1949 } else {
1950 if (key.def_uni || !key.def_multi) {
1951 err = -EINVAL;
1952 goto out;
1955 if (!rdev->ops->set_default_mgmt_key) {
1956 err = -EOPNOTSUPP;
1957 goto out;
1960 err = nl80211_key_allowed(dev->ieee80211_ptr);
1961 if (err)
1962 goto out;
1964 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1965 dev, key.idx);
1966 if (err)
1967 goto out;
1969 #ifdef CONFIG_CFG80211_WEXT
1970 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1971 #endif
1974 out:
1975 wdev_unlock(dev->ieee80211_ptr);
1977 return err;
1980 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1983 int err;
1984 struct net_device *dev = info->user_ptr[1];
1985 struct key_parse key;
1986 const u8 *mac_addr = NULL;
1988 err = nl80211_parse_key(info, &key);
1989 if (err)
1990 return err;
1992 if (!key.p.key)
1993 return -EINVAL;
1995 if (info->attrs[NL80211_ATTR_MAC])
1996 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1998 if (key.type == -1) {
1999 if (mac_addr)
2000 key.type = NL80211_KEYTYPE_PAIRWISE;
2001 else
2002 key.type = NL80211_KEYTYPE_GROUP;
2005 /* for now */
2006 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2007 key.type != NL80211_KEYTYPE_GROUP)
2008 return -EINVAL;
2010 if (!rdev->ops->add_key)
2011 return -EOPNOTSUPP;
2013 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2014 key.type == NL80211_KEYTYPE_PAIRWISE,
2015 mac_addr))
2016 return -EINVAL;
2018 wdev_lock(dev->ieee80211_ptr);
2019 err = nl80211_key_allowed(dev->ieee80211_ptr);
2020 if (!err)
2021 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
2022 key.type == NL80211_KEYTYPE_PAIRWISE,
2023 mac_addr, &key.p);
2024 wdev_unlock(dev->ieee80211_ptr);
2026 return err;
2029 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2031 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2032 int err;
2033 struct net_device *dev = info->user_ptr[1];
2034 u8 *mac_addr = NULL;
2035 struct key_parse key;
2037 err = nl80211_parse_key(info, &key);
2038 if (err)
2039 return err;
2041 if (info->attrs[NL80211_ATTR_MAC])
2042 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2044 if (key.type == -1) {
2045 if (mac_addr)
2046 key.type = NL80211_KEYTYPE_PAIRWISE;
2047 else
2048 key.type = NL80211_KEYTYPE_GROUP;
2051 /* for now */
2052 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2053 key.type != NL80211_KEYTYPE_GROUP)
2054 return -EINVAL;
2056 if (!rdev->ops->del_key)
2057 return -EOPNOTSUPP;
2059 wdev_lock(dev->ieee80211_ptr);
2060 err = nl80211_key_allowed(dev->ieee80211_ptr);
2062 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2063 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2064 err = -ENOENT;
2066 if (!err)
2067 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2068 key.type == NL80211_KEYTYPE_PAIRWISE,
2069 mac_addr);
2071 #ifdef CONFIG_CFG80211_WEXT
2072 if (!err) {
2073 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2074 dev->ieee80211_ptr->wext.default_key = -1;
2075 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2076 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2078 #endif
2079 wdev_unlock(dev->ieee80211_ptr);
2081 return err;
2084 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
2086 int (*call)(struct wiphy *wiphy, struct net_device *dev,
2087 struct beacon_parameters *info);
2088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2089 struct net_device *dev = info->user_ptr[1];
2090 struct wireless_dev *wdev = dev->ieee80211_ptr;
2091 struct beacon_parameters params;
2092 int haveinfo = 0, err;
2094 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2095 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2096 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2097 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
2098 return -EINVAL;
2100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2101 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2102 return -EOPNOTSUPP;
2104 memset(&params, 0, sizeof(params));
2106 switch (info->genlhdr->cmd) {
2107 case NL80211_CMD_NEW_BEACON:
2108 /* these are required for NEW_BEACON */
2109 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2110 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2111 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2112 return -EINVAL;
2114 params.interval =
2115 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2116 params.dtim_period =
2117 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2119 err = cfg80211_validate_beacon_int(rdev, params.interval);
2120 if (err)
2121 return err;
2124 * In theory, some of these attributes could be required for
2125 * NEW_BEACON, but since they were not used when the command was
2126 * originally added, keep them optional for old user space
2127 * programs to work with drivers that do not need the additional
2128 * information.
2130 if (info->attrs[NL80211_ATTR_SSID]) {
2131 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2132 params.ssid_len =
2133 nla_len(info->attrs[NL80211_ATTR_SSID]);
2134 if (params.ssid_len == 0 ||
2135 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2136 return -EINVAL;
2139 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2140 params.hidden_ssid = nla_get_u32(
2141 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2142 if (params.hidden_ssid !=
2143 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2144 params.hidden_ssid !=
2145 NL80211_HIDDEN_SSID_ZERO_LEN &&
2146 params.hidden_ssid !=
2147 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2148 return -EINVAL;
2151 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2153 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2154 params.auth_type = nla_get_u32(
2155 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2156 if (!nl80211_valid_auth_type(params.auth_type))
2157 return -EINVAL;
2158 } else
2159 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2161 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2162 NL80211_MAX_NR_CIPHER_SUITES);
2163 if (err)
2164 return err;
2166 call = rdev->ops->add_beacon;
2167 break;
2168 case NL80211_CMD_SET_BEACON:
2169 call = rdev->ops->set_beacon;
2170 break;
2171 default:
2172 WARN_ON(1);
2173 return -EOPNOTSUPP;
2176 if (!call)
2177 return -EOPNOTSUPP;
2179 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2180 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2181 params.head_len =
2182 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2183 haveinfo = 1;
2186 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2187 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2188 params.tail_len =
2189 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2190 haveinfo = 1;
2193 if (!haveinfo)
2194 return -EINVAL;
2196 if (info->attrs[NL80211_ATTR_IE]) {
2197 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2198 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2201 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2202 params.proberesp_ies =
2203 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2204 params.proberesp_ies_len =
2205 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2208 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2209 params.assocresp_ies =
2210 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2211 params.assocresp_ies_len =
2212 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2215 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2216 params.probe_resp =
2217 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2218 params.probe_resp_len =
2219 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2222 err = call(&rdev->wiphy, dev, &params);
2223 if (!err && params.interval)
2224 wdev->beacon_interval = params.interval;
2225 return err;
2228 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2231 struct net_device *dev = info->user_ptr[1];
2232 struct wireless_dev *wdev = dev->ieee80211_ptr;
2233 int err;
2235 if (!rdev->ops->del_beacon)
2236 return -EOPNOTSUPP;
2238 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2239 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2240 return -EOPNOTSUPP;
2242 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2243 if (!err)
2244 wdev->beacon_interval = 0;
2245 return err;
2248 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2249 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2250 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2251 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2252 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2253 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2254 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
2257 static int parse_station_flags(struct genl_info *info,
2258 enum nl80211_iftype iftype,
2259 struct station_parameters *params)
2261 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2262 struct nlattr *nla;
2263 int flag;
2266 * Try parsing the new attribute first so userspace
2267 * can specify both for older kernels.
2269 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2270 if (nla) {
2271 struct nl80211_sta_flag_update *sta_flags;
2273 sta_flags = nla_data(nla);
2274 params->sta_flags_mask = sta_flags->mask;
2275 params->sta_flags_set = sta_flags->set;
2276 if ((params->sta_flags_mask |
2277 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2278 return -EINVAL;
2279 return 0;
2282 /* if present, parse the old attribute */
2284 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2285 if (!nla)
2286 return 0;
2288 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2289 nla, sta_flags_policy))
2290 return -EINVAL;
2293 * Only allow certain flags for interface types so that
2294 * other attributes are silently ignored. Remember that
2295 * this is backward compatibility code with old userspace
2296 * and shouldn't be hit in other cases anyway.
2298 switch (iftype) {
2299 case NL80211_IFTYPE_AP:
2300 case NL80211_IFTYPE_AP_VLAN:
2301 case NL80211_IFTYPE_P2P_GO:
2302 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2303 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2304 BIT(NL80211_STA_FLAG_WME) |
2305 BIT(NL80211_STA_FLAG_MFP);
2306 break;
2307 case NL80211_IFTYPE_P2P_CLIENT:
2308 case NL80211_IFTYPE_STATION:
2309 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2310 BIT(NL80211_STA_FLAG_TDLS_PEER);
2311 break;
2312 case NL80211_IFTYPE_MESH_POINT:
2313 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2314 BIT(NL80211_STA_FLAG_MFP) |
2315 BIT(NL80211_STA_FLAG_AUTHORIZED);
2316 default:
2317 return -EINVAL;
2320 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2321 if (flags[flag])
2322 params->sta_flags_set |= (1<<flag);
2324 return 0;
2327 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2328 int attr)
2330 struct nlattr *rate;
2331 u16 bitrate;
2333 rate = nla_nest_start(msg, attr);
2334 if (!rate)
2335 goto nla_put_failure;
2337 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2338 bitrate = cfg80211_calculate_bitrate(info);
2339 if (bitrate > 0)
2340 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2342 if (info->flags & RATE_INFO_FLAGS_MCS)
2343 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2344 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2345 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2346 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2347 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2349 nla_nest_end(msg, rate);
2350 return true;
2352 nla_put_failure:
2353 return false;
2356 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2357 int flags, struct net_device *dev,
2358 const u8 *mac_addr, struct station_info *sinfo)
2360 void *hdr;
2361 struct nlattr *sinfoattr, *bss_param;
2363 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2364 if (!hdr)
2365 return -1;
2367 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2368 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2370 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2372 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2373 if (!sinfoattr)
2374 goto nla_put_failure;
2375 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2376 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2377 sinfo->connected_time);
2378 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2379 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2380 sinfo->inactive_time);
2381 if (sinfo->filled & STATION_INFO_RX_BYTES)
2382 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2383 sinfo->rx_bytes);
2384 if (sinfo->filled & STATION_INFO_TX_BYTES)
2385 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2386 sinfo->tx_bytes);
2387 if (sinfo->filled & STATION_INFO_LLID)
2388 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2389 sinfo->llid);
2390 if (sinfo->filled & STATION_INFO_PLID)
2391 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2392 sinfo->plid);
2393 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2394 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2395 sinfo->plink_state);
2396 if (sinfo->filled & STATION_INFO_SIGNAL)
2397 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2398 sinfo->signal);
2399 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2400 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2401 sinfo->signal_avg);
2402 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2403 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2404 NL80211_STA_INFO_TX_BITRATE))
2405 goto nla_put_failure;
2407 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2408 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2409 NL80211_STA_INFO_RX_BITRATE))
2410 goto nla_put_failure;
2412 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2413 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2414 sinfo->rx_packets);
2415 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2416 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2417 sinfo->tx_packets);
2418 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2419 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2420 sinfo->tx_retries);
2421 if (sinfo->filled & STATION_INFO_TX_FAILED)
2422 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2423 sinfo->tx_failed);
2424 if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2425 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2426 sinfo->beacon_loss_count);
2427 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2428 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2429 if (!bss_param)
2430 goto nla_put_failure;
2432 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2433 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2434 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2435 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2436 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2437 NLA_PUT_FLAG(msg,
2438 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2439 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2440 sinfo->bss_param.dtim_period);
2441 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2442 sinfo->bss_param.beacon_interval);
2444 nla_nest_end(msg, bss_param);
2446 if (sinfo->filled & STATION_INFO_STA_FLAGS)
2447 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2448 sizeof(struct nl80211_sta_flag_update),
2449 &sinfo->sta_flags);
2450 nla_nest_end(msg, sinfoattr);
2452 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
2453 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2454 sinfo->assoc_req_ies);
2456 return genlmsg_end(msg, hdr);
2458 nla_put_failure:
2459 genlmsg_cancel(msg, hdr);
2460 return -EMSGSIZE;
2463 static int nl80211_dump_station(struct sk_buff *skb,
2464 struct netlink_callback *cb)
2466 struct station_info sinfo;
2467 struct cfg80211_registered_device *dev;
2468 struct net_device *netdev;
2469 u8 mac_addr[ETH_ALEN];
2470 int sta_idx = cb->args[1];
2471 int err;
2473 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2474 if (err)
2475 return err;
2477 if (!dev->ops->dump_station) {
2478 err = -EOPNOTSUPP;
2479 goto out_err;
2482 while (1) {
2483 memset(&sinfo, 0, sizeof(sinfo));
2484 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2485 mac_addr, &sinfo);
2486 if (err == -ENOENT)
2487 break;
2488 if (err)
2489 goto out_err;
2491 if (nl80211_send_station(skb,
2492 NETLINK_CB(cb->skb).pid,
2493 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2494 netdev, mac_addr,
2495 &sinfo) < 0)
2496 goto out;
2498 sta_idx++;
2502 out:
2503 cb->args[1] = sta_idx;
2504 err = skb->len;
2505 out_err:
2506 nl80211_finish_netdev_dump(dev);
2508 return err;
2511 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2514 struct net_device *dev = info->user_ptr[1];
2515 struct station_info sinfo;
2516 struct sk_buff *msg;
2517 u8 *mac_addr = NULL;
2518 int err;
2520 memset(&sinfo, 0, sizeof(sinfo));
2522 if (!info->attrs[NL80211_ATTR_MAC])
2523 return -EINVAL;
2525 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2527 if (!rdev->ops->get_station)
2528 return -EOPNOTSUPP;
2530 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2531 if (err)
2532 return err;
2534 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2535 if (!msg)
2536 return -ENOMEM;
2538 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2539 dev, mac_addr, &sinfo) < 0) {
2540 nlmsg_free(msg);
2541 return -ENOBUFS;
2544 return genlmsg_reply(msg, info);
2548 * Get vlan interface making sure it is running and on the right wiphy.
2550 static struct net_device *get_vlan(struct genl_info *info,
2551 struct cfg80211_registered_device *rdev)
2553 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2554 struct net_device *v;
2555 int ret;
2557 if (!vlanattr)
2558 return NULL;
2560 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2561 if (!v)
2562 return ERR_PTR(-ENODEV);
2564 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2565 ret = -EINVAL;
2566 goto error;
2569 if (!netif_running(v)) {
2570 ret = -ENETDOWN;
2571 goto error;
2574 return v;
2575 error:
2576 dev_put(v);
2577 return ERR_PTR(ret);
2580 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2582 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2583 int err;
2584 struct net_device *dev = info->user_ptr[1];
2585 struct station_parameters params;
2586 u8 *mac_addr = NULL;
2588 memset(&params, 0, sizeof(params));
2590 params.listen_interval = -1;
2591 params.plink_state = -1;
2593 if (info->attrs[NL80211_ATTR_STA_AID])
2594 return -EINVAL;
2596 if (!info->attrs[NL80211_ATTR_MAC])
2597 return -EINVAL;
2599 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2601 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2602 params.supported_rates =
2603 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2604 params.supported_rates_len =
2605 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2608 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2609 params.listen_interval =
2610 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2612 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2613 params.ht_capa =
2614 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2616 if (!rdev->ops->change_station)
2617 return -EOPNOTSUPP;
2619 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2620 return -EINVAL;
2622 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2623 params.plink_action =
2624 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2626 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2627 params.plink_state =
2628 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2630 switch (dev->ieee80211_ptr->iftype) {
2631 case NL80211_IFTYPE_AP:
2632 case NL80211_IFTYPE_AP_VLAN:
2633 case NL80211_IFTYPE_P2P_GO:
2634 /* disallow mesh-specific things */
2635 if (params.plink_action)
2636 return -EINVAL;
2638 /* TDLS can't be set, ... */
2639 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2640 return -EINVAL;
2642 * ... but don't bother the driver with it. This works around
2643 * a hostapd/wpa_supplicant issue -- it always includes the
2644 * TLDS_PEER flag in the mask even for AP mode.
2646 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2648 /* accept only the listed bits */
2649 if (params.sta_flags_mask &
2650 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2651 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2652 BIT(NL80211_STA_FLAG_WME) |
2653 BIT(NL80211_STA_FLAG_MFP)))
2654 return -EINVAL;
2656 /* must be last in here for error handling */
2657 params.vlan = get_vlan(info, rdev);
2658 if (IS_ERR(params.vlan))
2659 return PTR_ERR(params.vlan);
2660 break;
2661 case NL80211_IFTYPE_P2P_CLIENT:
2662 case NL80211_IFTYPE_STATION:
2663 /* disallow things sta doesn't support */
2664 if (params.plink_action)
2665 return -EINVAL;
2666 if (params.ht_capa)
2667 return -EINVAL;
2668 if (params.listen_interval >= 0)
2669 return -EINVAL;
2671 * Don't allow userspace to change the TDLS_PEER flag,
2672 * but silently ignore attempts to change it since we
2673 * don't have state here to verify that it doesn't try
2674 * to change the flag.
2676 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2678 /* reject any changes other than AUTHORIZED */
2679 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2680 return -EINVAL;
2681 break;
2682 case NL80211_IFTYPE_MESH_POINT:
2683 /* disallow things mesh doesn't support */
2684 if (params.vlan)
2685 return -EINVAL;
2686 if (params.ht_capa)
2687 return -EINVAL;
2688 if (params.listen_interval >= 0)
2689 return -EINVAL;
2691 * No special handling for TDLS here -- the userspace
2692 * mesh code doesn't have this bug.
2694 if (params.sta_flags_mask &
2695 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2696 BIT(NL80211_STA_FLAG_MFP) |
2697 BIT(NL80211_STA_FLAG_AUTHORIZED)))
2698 return -EINVAL;
2699 break;
2700 default:
2701 return -EOPNOTSUPP;
2704 /* be aware of params.vlan when changing code here */
2706 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2708 if (params.vlan)
2709 dev_put(params.vlan);
2711 return err;
2714 static struct nla_policy
2715 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2716 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2717 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2720 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2722 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2723 int err;
2724 struct net_device *dev = info->user_ptr[1];
2725 struct station_parameters params;
2726 u8 *mac_addr = NULL;
2728 memset(&params, 0, sizeof(params));
2730 if (!info->attrs[NL80211_ATTR_MAC])
2731 return -EINVAL;
2733 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2734 return -EINVAL;
2736 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2737 return -EINVAL;
2739 if (!info->attrs[NL80211_ATTR_STA_AID])
2740 return -EINVAL;
2742 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2743 params.supported_rates =
2744 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2745 params.supported_rates_len =
2746 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2747 params.listen_interval =
2748 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2750 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2751 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2752 return -EINVAL;
2754 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2755 params.ht_capa =
2756 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2758 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2759 params.plink_action =
2760 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2762 if (!rdev->ops->add_station)
2763 return -EOPNOTSUPP;
2765 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2766 return -EINVAL;
2768 switch (dev->ieee80211_ptr->iftype) {
2769 case NL80211_IFTYPE_AP:
2770 case NL80211_IFTYPE_AP_VLAN:
2771 case NL80211_IFTYPE_P2P_GO:
2772 /* parse WME attributes if sta is WME capable */
2773 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2774 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2775 info->attrs[NL80211_ATTR_STA_WME]) {
2776 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2777 struct nlattr *nla;
2779 nla = info->attrs[NL80211_ATTR_STA_WME];
2780 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2781 nl80211_sta_wme_policy);
2782 if (err)
2783 return err;
2785 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2786 params.uapsd_queues =
2787 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2788 if (params.uapsd_queues &
2789 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2790 return -EINVAL;
2792 if (tb[NL80211_STA_WME_MAX_SP])
2793 params.max_sp =
2794 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2796 if (params.max_sp &
2797 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2798 return -EINVAL;
2800 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2802 /* TDLS peers cannot be added */
2803 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2804 return -EINVAL;
2805 /* but don't bother the driver with it */
2806 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2808 /* must be last in here for error handling */
2809 params.vlan = get_vlan(info, rdev);
2810 if (IS_ERR(params.vlan))
2811 return PTR_ERR(params.vlan);
2812 break;
2813 case NL80211_IFTYPE_MESH_POINT:
2814 /* TDLS peers cannot be added */
2815 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2816 return -EINVAL;
2817 break;
2818 case NL80211_IFTYPE_STATION:
2819 /* Only TDLS peers can be added */
2820 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2821 return -EINVAL;
2822 /* Can only add if TDLS ... */
2823 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2824 return -EOPNOTSUPP;
2825 /* ... with external setup is supported */
2826 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2827 return -EOPNOTSUPP;
2828 break;
2829 default:
2830 return -EOPNOTSUPP;
2833 /* be aware of params.vlan when changing code here */
2835 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2837 if (params.vlan)
2838 dev_put(params.vlan);
2839 return err;
2842 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2845 struct net_device *dev = info->user_ptr[1];
2846 u8 *mac_addr = NULL;
2848 if (info->attrs[NL80211_ATTR_MAC])
2849 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2851 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2852 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2853 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2854 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2855 return -EINVAL;
2857 if (!rdev->ops->del_station)
2858 return -EOPNOTSUPP;
2860 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2863 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2864 int flags, struct net_device *dev,
2865 u8 *dst, u8 *next_hop,
2866 struct mpath_info *pinfo)
2868 void *hdr;
2869 struct nlattr *pinfoattr;
2871 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2872 if (!hdr)
2873 return -1;
2875 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2876 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2877 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2879 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2881 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2882 if (!pinfoattr)
2883 goto nla_put_failure;
2884 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2885 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2886 pinfo->frame_qlen);
2887 if (pinfo->filled & MPATH_INFO_SN)
2888 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2889 pinfo->sn);
2890 if (pinfo->filled & MPATH_INFO_METRIC)
2891 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2892 pinfo->metric);
2893 if (pinfo->filled & MPATH_INFO_EXPTIME)
2894 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2895 pinfo->exptime);
2896 if (pinfo->filled & MPATH_INFO_FLAGS)
2897 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2898 pinfo->flags);
2899 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2900 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2901 pinfo->discovery_timeout);
2902 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2903 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2904 pinfo->discovery_retries);
2906 nla_nest_end(msg, pinfoattr);
2908 return genlmsg_end(msg, hdr);
2910 nla_put_failure:
2911 genlmsg_cancel(msg, hdr);
2912 return -EMSGSIZE;
2915 static int nl80211_dump_mpath(struct sk_buff *skb,
2916 struct netlink_callback *cb)
2918 struct mpath_info pinfo;
2919 struct cfg80211_registered_device *dev;
2920 struct net_device *netdev;
2921 u8 dst[ETH_ALEN];
2922 u8 next_hop[ETH_ALEN];
2923 int path_idx = cb->args[1];
2924 int err;
2926 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2927 if (err)
2928 return err;
2930 if (!dev->ops->dump_mpath) {
2931 err = -EOPNOTSUPP;
2932 goto out_err;
2935 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2936 err = -EOPNOTSUPP;
2937 goto out_err;
2940 while (1) {
2941 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2942 dst, next_hop, &pinfo);
2943 if (err == -ENOENT)
2944 break;
2945 if (err)
2946 goto out_err;
2948 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2949 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2950 netdev, dst, next_hop,
2951 &pinfo) < 0)
2952 goto out;
2954 path_idx++;
2958 out:
2959 cb->args[1] = path_idx;
2960 err = skb->len;
2961 out_err:
2962 nl80211_finish_netdev_dump(dev);
2963 return err;
2966 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2969 int err;
2970 struct net_device *dev = info->user_ptr[1];
2971 struct mpath_info pinfo;
2972 struct sk_buff *msg;
2973 u8 *dst = NULL;
2974 u8 next_hop[ETH_ALEN];
2976 memset(&pinfo, 0, sizeof(pinfo));
2978 if (!info->attrs[NL80211_ATTR_MAC])
2979 return -EINVAL;
2981 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2983 if (!rdev->ops->get_mpath)
2984 return -EOPNOTSUPP;
2986 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2987 return -EOPNOTSUPP;
2989 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2990 if (err)
2991 return err;
2993 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2994 if (!msg)
2995 return -ENOMEM;
2997 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2998 dev, dst, next_hop, &pinfo) < 0) {
2999 nlmsg_free(msg);
3000 return -ENOBUFS;
3003 return genlmsg_reply(msg, info);
3006 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009 struct net_device *dev = info->user_ptr[1];
3010 u8 *dst = NULL;
3011 u8 *next_hop = NULL;
3013 if (!info->attrs[NL80211_ATTR_MAC])
3014 return -EINVAL;
3016 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3017 return -EINVAL;
3019 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3020 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3022 if (!rdev->ops->change_mpath)
3023 return -EOPNOTSUPP;
3025 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3026 return -EOPNOTSUPP;
3028 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
3031 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3034 struct net_device *dev = info->user_ptr[1];
3035 u8 *dst = NULL;
3036 u8 *next_hop = NULL;
3038 if (!info->attrs[NL80211_ATTR_MAC])
3039 return -EINVAL;
3041 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3042 return -EINVAL;
3044 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3045 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3047 if (!rdev->ops->add_mpath)
3048 return -EOPNOTSUPP;
3050 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3051 return -EOPNOTSUPP;
3053 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
3056 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3058 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3059 struct net_device *dev = info->user_ptr[1];
3060 u8 *dst = NULL;
3062 if (info->attrs[NL80211_ATTR_MAC])
3063 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3065 if (!rdev->ops->del_mpath)
3066 return -EOPNOTSUPP;
3068 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
3071 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3073 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3074 struct net_device *dev = info->user_ptr[1];
3075 struct bss_parameters params;
3077 memset(&params, 0, sizeof(params));
3078 /* default to not changing parameters */
3079 params.use_cts_prot = -1;
3080 params.use_short_preamble = -1;
3081 params.use_short_slot_time = -1;
3082 params.ap_isolate = -1;
3083 params.ht_opmode = -1;
3085 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3086 params.use_cts_prot =
3087 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3088 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3089 params.use_short_preamble =
3090 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3091 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3092 params.use_short_slot_time =
3093 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
3094 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3095 params.basic_rates =
3096 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3097 params.basic_rates_len =
3098 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3100 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3101 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
3102 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3103 params.ht_opmode =
3104 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
3106 if (!rdev->ops->change_bss)
3107 return -EOPNOTSUPP;
3109 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3110 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3111 return -EOPNOTSUPP;
3113 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
3116 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
3117 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3118 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3119 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3120 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3121 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3122 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3125 static int parse_reg_rule(struct nlattr *tb[],
3126 struct ieee80211_reg_rule *reg_rule)
3128 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3129 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3131 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3132 return -EINVAL;
3133 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3134 return -EINVAL;
3135 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3136 return -EINVAL;
3137 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3138 return -EINVAL;
3139 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3140 return -EINVAL;
3142 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3144 freq_range->start_freq_khz =
3145 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3146 freq_range->end_freq_khz =
3147 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3148 freq_range->max_bandwidth_khz =
3149 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3151 power_rule->max_eirp =
3152 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3154 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3155 power_rule->max_antenna_gain =
3156 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3158 return 0;
3161 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3163 int r;
3164 char *data = NULL;
3167 * You should only get this when cfg80211 hasn't yet initialized
3168 * completely when built-in to the kernel right between the time
3169 * window between nl80211_init() and regulatory_init(), if that is
3170 * even possible.
3172 mutex_lock(&cfg80211_mutex);
3173 if (unlikely(!cfg80211_regdomain)) {
3174 mutex_unlock(&cfg80211_mutex);
3175 return -EINPROGRESS;
3177 mutex_unlock(&cfg80211_mutex);
3179 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3180 return -EINVAL;
3182 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3184 r = regulatory_hint_user(data);
3186 return r;
3189 static int nl80211_get_mesh_config(struct sk_buff *skb,
3190 struct genl_info *info)
3192 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3193 struct net_device *dev = info->user_ptr[1];
3194 struct wireless_dev *wdev = dev->ieee80211_ptr;
3195 struct mesh_config cur_params;
3196 int err = 0;
3197 void *hdr;
3198 struct nlattr *pinfoattr;
3199 struct sk_buff *msg;
3201 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3202 return -EOPNOTSUPP;
3204 if (!rdev->ops->get_mesh_config)
3205 return -EOPNOTSUPP;
3207 wdev_lock(wdev);
3208 /* If not connected, get default parameters */
3209 if (!wdev->mesh_id_len)
3210 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3211 else
3212 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
3213 &cur_params);
3214 wdev_unlock(wdev);
3216 if (err)
3217 return err;
3219 /* Draw up a netlink message to send back */
3220 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3221 if (!msg)
3222 return -ENOMEM;
3223 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3224 NL80211_CMD_GET_MESH_CONFIG);
3225 if (!hdr)
3226 goto out;
3227 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
3228 if (!pinfoattr)
3229 goto nla_put_failure;
3230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3231 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3232 cur_params.dot11MeshRetryTimeout);
3233 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3234 cur_params.dot11MeshConfirmTimeout);
3235 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3236 cur_params.dot11MeshHoldingTimeout);
3237 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3238 cur_params.dot11MeshMaxPeerLinks);
3239 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3240 cur_params.dot11MeshMaxRetries);
3241 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3242 cur_params.dot11MeshTTL);
3243 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3244 cur_params.element_ttl);
3245 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3246 cur_params.auto_open_plinks);
3247 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3248 cur_params.dot11MeshHWMPmaxPREQretries);
3249 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3250 cur_params.path_refresh_time);
3251 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3252 cur_params.min_discovery_timeout);
3253 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3254 cur_params.dot11MeshHWMPactivePathTimeout);
3255 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3256 cur_params.dot11MeshHWMPpreqMinInterval);
3257 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3258 cur_params.dot11MeshHWMPperrMinInterval);
3259 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3260 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
3261 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3262 cur_params.dot11MeshHWMPRootMode);
3263 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3264 cur_params.dot11MeshHWMPRannInterval);
3265 NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3266 cur_params.dot11MeshGateAnnouncementProtocol);
3267 nla_nest_end(msg, pinfoattr);
3268 genlmsg_end(msg, hdr);
3269 return genlmsg_reply(msg, info);
3271 nla_put_failure:
3272 genlmsg_cancel(msg, hdr);
3273 out:
3274 nlmsg_free(msg);
3275 return -ENOBUFS;
3278 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
3279 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3280 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3281 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3282 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3283 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3284 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
3285 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
3286 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3288 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3289 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3290 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3291 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3292 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3293 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
3294 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3295 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
3296 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
3297 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
3300 static const struct nla_policy
3301 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3302 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3303 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
3304 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
3305 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
3306 .len = IEEE80211_MAX_DATA_LEN },
3307 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
3310 static int nl80211_parse_mesh_config(struct genl_info *info,
3311 struct mesh_config *cfg,
3312 u32 *mask_out)
3314 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
3315 u32 mask = 0;
3317 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3318 do {\
3319 if (table[attr_num]) {\
3320 cfg->param = nla_fn(table[attr_num]); \
3321 mask |= (1 << (attr_num - 1)); \
3323 } while (0);\
3326 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
3327 return -EINVAL;
3328 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
3329 info->attrs[NL80211_ATTR_MESH_CONFIG],
3330 nl80211_meshconf_params_policy))
3331 return -EINVAL;
3333 /* This makes sure that there aren't more than 32 mesh config
3334 * parameters (otherwise our bitfield scheme would not work.) */
3335 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3337 /* Fill in the params struct */
3338 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3339 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3340 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3341 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3342 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3343 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3344 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3345 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3346 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3347 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3348 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3349 mask, NL80211_MESHCONF_TTL, nla_get_u8);
3350 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3351 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
3352 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3353 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3354 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3355 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3356 nla_get_u8);
3357 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3358 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3359 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3360 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3361 nla_get_u16);
3362 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3363 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3364 nla_get_u32);
3365 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3366 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3367 nla_get_u16);
3368 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3369 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3370 nla_get_u16);
3371 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3372 dot11MeshHWMPnetDiameterTraversalTime,
3373 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3374 nla_get_u16);
3375 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3376 dot11MeshHWMPRootMode, mask,
3377 NL80211_MESHCONF_HWMP_ROOTMODE,
3378 nla_get_u8);
3379 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3380 dot11MeshHWMPRannInterval, mask,
3381 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3382 nla_get_u16);
3383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3384 dot11MeshGateAnnouncementProtocol, mask,
3385 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3386 nla_get_u8);
3387 if (mask_out)
3388 *mask_out = mask;
3390 return 0;
3392 #undef FILL_IN_MESH_PARAM_IF_SET
3395 static int nl80211_parse_mesh_setup(struct genl_info *info,
3396 struct mesh_setup *setup)
3398 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3400 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3401 return -EINVAL;
3402 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3403 info->attrs[NL80211_ATTR_MESH_SETUP],
3404 nl80211_mesh_setup_params_policy))
3405 return -EINVAL;
3407 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3408 setup->path_sel_proto =
3409 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3410 IEEE80211_PATH_PROTOCOL_VENDOR :
3411 IEEE80211_PATH_PROTOCOL_HWMP;
3413 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3414 setup->path_metric =
3415 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3416 IEEE80211_PATH_METRIC_VENDOR :
3417 IEEE80211_PATH_METRIC_AIRTIME;
3420 if (tb[NL80211_MESH_SETUP_IE]) {
3421 struct nlattr *ieattr =
3422 tb[NL80211_MESH_SETUP_IE];
3423 if (!is_valid_ie_attr(ieattr))
3424 return -EINVAL;
3425 setup->ie = nla_data(ieattr);
3426 setup->ie_len = nla_len(ieattr);
3428 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3429 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3431 return 0;
3434 static int nl80211_update_mesh_config(struct sk_buff *skb,
3435 struct genl_info *info)
3437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3438 struct net_device *dev = info->user_ptr[1];
3439 struct wireless_dev *wdev = dev->ieee80211_ptr;
3440 struct mesh_config cfg;
3441 u32 mask;
3442 int err;
3444 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3445 return -EOPNOTSUPP;
3447 if (!rdev->ops->update_mesh_config)
3448 return -EOPNOTSUPP;
3450 err = nl80211_parse_mesh_config(info, &cfg, &mask);
3451 if (err)
3452 return err;
3454 wdev_lock(wdev);
3455 if (!wdev->mesh_id_len)
3456 err = -ENOLINK;
3458 if (!err)
3459 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3460 mask, &cfg);
3462 wdev_unlock(wdev);
3464 return err;
3467 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3469 struct sk_buff *msg;
3470 void *hdr = NULL;
3471 struct nlattr *nl_reg_rules;
3472 unsigned int i;
3473 int err = -EINVAL;
3475 mutex_lock(&cfg80211_mutex);
3477 if (!cfg80211_regdomain)
3478 goto out;
3480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3481 if (!msg) {
3482 err = -ENOBUFS;
3483 goto out;
3486 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3487 NL80211_CMD_GET_REG);
3488 if (!hdr)
3489 goto put_failure;
3491 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3492 cfg80211_regdomain->alpha2);
3493 if (cfg80211_regdomain->dfs_region)
3494 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3495 cfg80211_regdomain->dfs_region);
3497 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3498 if (!nl_reg_rules)
3499 goto nla_put_failure;
3501 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3502 struct nlattr *nl_reg_rule;
3503 const struct ieee80211_reg_rule *reg_rule;
3504 const struct ieee80211_freq_range *freq_range;
3505 const struct ieee80211_power_rule *power_rule;
3507 reg_rule = &cfg80211_regdomain->reg_rules[i];
3508 freq_range = &reg_rule->freq_range;
3509 power_rule = &reg_rule->power_rule;
3511 nl_reg_rule = nla_nest_start(msg, i);
3512 if (!nl_reg_rule)
3513 goto nla_put_failure;
3515 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3516 reg_rule->flags);
3517 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3518 freq_range->start_freq_khz);
3519 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3520 freq_range->end_freq_khz);
3521 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3522 freq_range->max_bandwidth_khz);
3523 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3524 power_rule->max_antenna_gain);
3525 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3526 power_rule->max_eirp);
3528 nla_nest_end(msg, nl_reg_rule);
3531 nla_nest_end(msg, nl_reg_rules);
3533 genlmsg_end(msg, hdr);
3534 err = genlmsg_reply(msg, info);
3535 goto out;
3537 nla_put_failure:
3538 genlmsg_cancel(msg, hdr);
3539 put_failure:
3540 nlmsg_free(msg);
3541 err = -EMSGSIZE;
3542 out:
3543 mutex_unlock(&cfg80211_mutex);
3544 return err;
3547 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3549 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3550 struct nlattr *nl_reg_rule;
3551 char *alpha2 = NULL;
3552 int rem_reg_rules = 0, r = 0;
3553 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3554 u8 dfs_region = 0;
3555 struct ieee80211_regdomain *rd = NULL;
3557 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3558 return -EINVAL;
3560 if (!info->attrs[NL80211_ATTR_REG_RULES])
3561 return -EINVAL;
3563 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3565 if (info->attrs[NL80211_ATTR_DFS_REGION])
3566 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3568 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3569 rem_reg_rules) {
3570 num_rules++;
3571 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3572 return -EINVAL;
3575 mutex_lock(&cfg80211_mutex);
3577 if (!reg_is_valid_request(alpha2)) {
3578 r = -EINVAL;
3579 goto bad_reg;
3582 size_of_regd = sizeof(struct ieee80211_regdomain) +
3583 (num_rules * sizeof(struct ieee80211_reg_rule));
3585 rd = kzalloc(size_of_regd, GFP_KERNEL);
3586 if (!rd) {
3587 r = -ENOMEM;
3588 goto bad_reg;
3591 rd->n_reg_rules = num_rules;
3592 rd->alpha2[0] = alpha2[0];
3593 rd->alpha2[1] = alpha2[1];
3596 * Disable DFS master mode if the DFS region was
3597 * not supported or known on this kernel.
3599 if (reg_supported_dfs_region(dfs_region))
3600 rd->dfs_region = dfs_region;
3602 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3603 rem_reg_rules) {
3604 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3605 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3606 reg_rule_policy);
3607 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3608 if (r)
3609 goto bad_reg;
3611 rule_idx++;
3613 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3614 r = -EINVAL;
3615 goto bad_reg;
3619 BUG_ON(rule_idx != num_rules);
3621 r = set_regdom(rd);
3623 mutex_unlock(&cfg80211_mutex);
3625 return r;
3627 bad_reg:
3628 mutex_unlock(&cfg80211_mutex);
3629 kfree(rd);
3630 return r;
3633 static int validate_scan_freqs(struct nlattr *freqs)
3635 struct nlattr *attr1, *attr2;
3636 int n_channels = 0, tmp1, tmp2;
3638 nla_for_each_nested(attr1, freqs, tmp1) {
3639 n_channels++;
3641 * Some hardware has a limited channel list for
3642 * scanning, and it is pretty much nonsensical
3643 * to scan for a channel twice, so disallow that
3644 * and don't require drivers to check that the
3645 * channel list they get isn't longer than what
3646 * they can scan, as long as they can scan all
3647 * the channels they registered at once.
3649 nla_for_each_nested(attr2, freqs, tmp2)
3650 if (attr1 != attr2 &&
3651 nla_get_u32(attr1) == nla_get_u32(attr2))
3652 return 0;
3655 return n_channels;
3658 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3660 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3661 struct net_device *dev = info->user_ptr[1];
3662 struct cfg80211_scan_request *request;
3663 struct nlattr *attr;
3664 struct wiphy *wiphy;
3665 int err, tmp, n_ssids = 0, n_channels, i;
3666 size_t ie_len;
3668 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3669 return -EINVAL;
3671 wiphy = &rdev->wiphy;
3673 if (!rdev->ops->scan)
3674 return -EOPNOTSUPP;
3676 if (rdev->scan_req)
3677 return -EBUSY;
3679 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3680 n_channels = validate_scan_freqs(
3681 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3682 if (!n_channels)
3683 return -EINVAL;
3684 } else {
3685 enum ieee80211_band band;
3686 n_channels = 0;
3688 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3689 if (wiphy->bands[band])
3690 n_channels += wiphy->bands[band]->n_channels;
3693 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3694 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3695 n_ssids++;
3697 if (n_ssids > wiphy->max_scan_ssids)
3698 return -EINVAL;
3700 if (info->attrs[NL80211_ATTR_IE])
3701 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3702 else
3703 ie_len = 0;
3705 if (ie_len > wiphy->max_scan_ie_len)
3706 return -EINVAL;
3708 request = kzalloc(sizeof(*request)
3709 + sizeof(*request->ssids) * n_ssids
3710 + sizeof(*request->channels) * n_channels
3711 + ie_len, GFP_KERNEL);
3712 if (!request)
3713 return -ENOMEM;
3715 if (n_ssids)
3716 request->ssids = (void *)&request->channels[n_channels];
3717 request->n_ssids = n_ssids;
3718 if (ie_len) {
3719 if (request->ssids)
3720 request->ie = (void *)(request->ssids + n_ssids);
3721 else
3722 request->ie = (void *)(request->channels + n_channels);
3725 i = 0;
3726 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3727 /* user specified, bail out if channel not found */
3728 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3729 struct ieee80211_channel *chan;
3731 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3733 if (!chan) {
3734 err = -EINVAL;
3735 goto out_free;
3738 /* ignore disabled channels */
3739 if (chan->flags & IEEE80211_CHAN_DISABLED)
3740 continue;
3742 request->channels[i] = chan;
3743 i++;
3745 } else {
3746 enum ieee80211_band band;
3748 /* all channels */
3749 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3750 int j;
3751 if (!wiphy->bands[band])
3752 continue;
3753 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3754 struct ieee80211_channel *chan;
3756 chan = &wiphy->bands[band]->channels[j];
3758 if (chan->flags & IEEE80211_CHAN_DISABLED)
3759 continue;
3761 request->channels[i] = chan;
3762 i++;
3767 if (!i) {
3768 err = -EINVAL;
3769 goto out_free;
3772 request->n_channels = i;
3774 i = 0;
3775 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3776 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3777 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3778 err = -EINVAL;
3779 goto out_free;
3781 request->ssids[i].ssid_len = nla_len(attr);
3782 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3783 i++;
3787 if (info->attrs[NL80211_ATTR_IE]) {
3788 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3789 memcpy((void *)request->ie,
3790 nla_data(info->attrs[NL80211_ATTR_IE]),
3791 request->ie_len);
3794 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
3795 if (wiphy->bands[i])
3796 request->rates[i] =
3797 (1 << wiphy->bands[i]->n_bitrates) - 1;
3799 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3800 nla_for_each_nested(attr,
3801 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3802 tmp) {
3803 enum ieee80211_band band = nla_type(attr);
3805 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
3806 err = -EINVAL;
3807 goto out_free;
3809 err = ieee80211_get_ratemask(wiphy->bands[band],
3810 nla_data(attr),
3811 nla_len(attr),
3812 &request->rates[band]);
3813 if (err)
3814 goto out_free;
3818 request->no_cck =
3819 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3821 request->dev = dev;
3822 request->wiphy = &rdev->wiphy;
3824 rdev->scan_req = request;
3825 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3827 if (!err) {
3828 nl80211_send_scan_start(rdev, dev);
3829 dev_hold(dev);
3830 } else {
3831 out_free:
3832 rdev->scan_req = NULL;
3833 kfree(request);
3836 return err;
3839 static int nl80211_start_sched_scan(struct sk_buff *skb,
3840 struct genl_info *info)
3842 struct cfg80211_sched_scan_request *request;
3843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3844 struct net_device *dev = info->user_ptr[1];
3845 struct nlattr *attr;
3846 struct wiphy *wiphy;
3847 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
3848 u32 interval;
3849 enum ieee80211_band band;
3850 size_t ie_len;
3851 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
3853 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3854 !rdev->ops->sched_scan_start)
3855 return -EOPNOTSUPP;
3857 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3858 return -EINVAL;
3860 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3861 return -EINVAL;
3863 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3864 if (interval == 0)
3865 return -EINVAL;
3867 wiphy = &rdev->wiphy;
3869 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3870 n_channels = validate_scan_freqs(
3871 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3872 if (!n_channels)
3873 return -EINVAL;
3874 } else {
3875 n_channels = 0;
3877 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3878 if (wiphy->bands[band])
3879 n_channels += wiphy->bands[band]->n_channels;
3882 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3883 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3884 tmp)
3885 n_ssids++;
3887 if (n_ssids > wiphy->max_sched_scan_ssids)
3888 return -EINVAL;
3890 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3891 nla_for_each_nested(attr,
3892 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3893 tmp)
3894 n_match_sets++;
3896 if (n_match_sets > wiphy->max_match_sets)
3897 return -EINVAL;
3899 if (info->attrs[NL80211_ATTR_IE])
3900 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3901 else
3902 ie_len = 0;
3904 if (ie_len > wiphy->max_sched_scan_ie_len)
3905 return -EINVAL;
3907 mutex_lock(&rdev->sched_scan_mtx);
3909 if (rdev->sched_scan_req) {
3910 err = -EINPROGRESS;
3911 goto out;
3914 request = kzalloc(sizeof(*request)
3915 + sizeof(*request->ssids) * n_ssids
3916 + sizeof(*request->match_sets) * n_match_sets
3917 + sizeof(*request->channels) * n_channels
3918 + ie_len, GFP_KERNEL);
3919 if (!request) {
3920 err = -ENOMEM;
3921 goto out;
3924 if (n_ssids)
3925 request->ssids = (void *)&request->channels[n_channels];
3926 request->n_ssids = n_ssids;
3927 if (ie_len) {
3928 if (request->ssids)
3929 request->ie = (void *)(request->ssids + n_ssids);
3930 else
3931 request->ie = (void *)(request->channels + n_channels);
3934 if (n_match_sets) {
3935 if (request->ie)
3936 request->match_sets = (void *)(request->ie + ie_len);
3937 else if (request->ssids)
3938 request->match_sets =
3939 (void *)(request->ssids + n_ssids);
3940 else
3941 request->match_sets =
3942 (void *)(request->channels + n_channels);
3944 request->n_match_sets = n_match_sets;
3946 i = 0;
3947 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3948 /* user specified, bail out if channel not found */
3949 nla_for_each_nested(attr,
3950 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3951 tmp) {
3952 struct ieee80211_channel *chan;
3954 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3956 if (!chan) {
3957 err = -EINVAL;
3958 goto out_free;
3961 /* ignore disabled channels */
3962 if (chan->flags & IEEE80211_CHAN_DISABLED)
3963 continue;
3965 request->channels[i] = chan;
3966 i++;
3968 } else {
3969 /* all channels */
3970 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3971 int j;
3972 if (!wiphy->bands[band])
3973 continue;
3974 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3975 struct ieee80211_channel *chan;
3977 chan = &wiphy->bands[band]->channels[j];
3979 if (chan->flags & IEEE80211_CHAN_DISABLED)
3980 continue;
3982 request->channels[i] = chan;
3983 i++;
3988 if (!i) {
3989 err = -EINVAL;
3990 goto out_free;
3993 request->n_channels = i;
3995 i = 0;
3996 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3997 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3998 tmp) {
3999 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
4000 err = -EINVAL;
4001 goto out_free;
4003 request->ssids[i].ssid_len = nla_len(attr);
4004 memcpy(request->ssids[i].ssid, nla_data(attr),
4005 nla_len(attr));
4006 i++;
4010 i = 0;
4011 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4012 nla_for_each_nested(attr,
4013 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4014 tmp) {
4015 struct nlattr *ssid;
4017 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4018 nla_data(attr), nla_len(attr),
4019 nl80211_match_policy);
4020 ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4021 if (ssid) {
4022 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4023 err = -EINVAL;
4024 goto out_free;
4026 memcpy(request->match_sets[i].ssid.ssid,
4027 nla_data(ssid), nla_len(ssid));
4028 request->match_sets[i].ssid.ssid_len =
4029 nla_len(ssid);
4031 i++;
4035 if (info->attrs[NL80211_ATTR_IE]) {
4036 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4037 memcpy((void *)request->ie,
4038 nla_data(info->attrs[NL80211_ATTR_IE]),
4039 request->ie_len);
4042 request->dev = dev;
4043 request->wiphy = &rdev->wiphy;
4044 request->interval = interval;
4046 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4047 if (!err) {
4048 rdev->sched_scan_req = request;
4049 nl80211_send_sched_scan(rdev, dev,
4050 NL80211_CMD_START_SCHED_SCAN);
4051 goto out;
4054 out_free:
4055 kfree(request);
4056 out:
4057 mutex_unlock(&rdev->sched_scan_mtx);
4058 return err;
4061 static int nl80211_stop_sched_scan(struct sk_buff *skb,
4062 struct genl_info *info)
4064 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4065 int err;
4067 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4068 !rdev->ops->sched_scan_stop)
4069 return -EOPNOTSUPP;
4071 mutex_lock(&rdev->sched_scan_mtx);
4072 err = __cfg80211_stop_sched_scan(rdev, false);
4073 mutex_unlock(&rdev->sched_scan_mtx);
4075 return err;
4078 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4079 u32 seq, int flags,
4080 struct cfg80211_registered_device *rdev,
4081 struct wireless_dev *wdev,
4082 struct cfg80211_internal_bss *intbss)
4084 struct cfg80211_bss *res = &intbss->pub;
4085 void *hdr;
4086 struct nlattr *bss;
4087 int i;
4089 ASSERT_WDEV_LOCK(wdev);
4091 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
4092 NL80211_CMD_NEW_SCAN_RESULTS);
4093 if (!hdr)
4094 return -1;
4096 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4098 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
4099 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
4101 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4102 if (!bss)
4103 goto nla_put_failure;
4104 if (!is_zero_ether_addr(res->bssid))
4105 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4106 if (res->information_elements && res->len_information_elements)
4107 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4108 res->len_information_elements,
4109 res->information_elements);
4110 if (res->beacon_ies && res->len_beacon_ies &&
4111 res->beacon_ies != res->information_elements)
4112 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4113 res->len_beacon_ies, res->beacon_ies);
4114 if (res->tsf)
4115 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4116 if (res->beacon_interval)
4117 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4118 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4119 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
4120 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4121 jiffies_to_msecs(jiffies - intbss->ts));
4123 switch (rdev->wiphy.signal_type) {
4124 case CFG80211_SIGNAL_TYPE_MBM:
4125 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4126 break;
4127 case CFG80211_SIGNAL_TYPE_UNSPEC:
4128 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4129 break;
4130 default:
4131 break;
4134 switch (wdev->iftype) {
4135 case NL80211_IFTYPE_P2P_CLIENT:
4136 case NL80211_IFTYPE_STATION:
4137 if (intbss == wdev->current_bss)
4138 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4139 NL80211_BSS_STATUS_ASSOCIATED);
4140 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
4141 if (intbss != wdev->auth_bsses[i])
4142 continue;
4143 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4144 NL80211_BSS_STATUS_AUTHENTICATED);
4145 break;
4147 break;
4148 case NL80211_IFTYPE_ADHOC:
4149 if (intbss == wdev->current_bss)
4150 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4151 NL80211_BSS_STATUS_IBSS_JOINED);
4152 break;
4153 default:
4154 break;
4157 nla_nest_end(msg, bss);
4159 return genlmsg_end(msg, hdr);
4161 nla_put_failure:
4162 genlmsg_cancel(msg, hdr);
4163 return -EMSGSIZE;
4166 static int nl80211_dump_scan(struct sk_buff *skb,
4167 struct netlink_callback *cb)
4169 struct cfg80211_registered_device *rdev;
4170 struct net_device *dev;
4171 struct cfg80211_internal_bss *scan;
4172 struct wireless_dev *wdev;
4173 int start = cb->args[1], idx = 0;
4174 int err;
4176 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4177 if (err)
4178 return err;
4180 wdev = dev->ieee80211_ptr;
4182 wdev_lock(wdev);
4183 spin_lock_bh(&rdev->bss_lock);
4184 cfg80211_bss_expire(rdev);
4186 cb->seq = rdev->bss_generation;
4188 list_for_each_entry(scan, &rdev->bss_list, list) {
4189 if (++idx <= start)
4190 continue;
4191 if (nl80211_send_bss(skb, cb,
4192 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4193 rdev, wdev, scan) < 0) {
4194 idx--;
4195 break;
4199 spin_unlock_bh(&rdev->bss_lock);
4200 wdev_unlock(wdev);
4202 cb->args[1] = idx;
4203 nl80211_finish_netdev_dump(rdev);
4205 return skb->len;
4208 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4209 int flags, struct net_device *dev,
4210 struct survey_info *survey)
4212 void *hdr;
4213 struct nlattr *infoattr;
4215 hdr = nl80211hdr_put(msg, pid, seq, flags,
4216 NL80211_CMD_NEW_SURVEY_RESULTS);
4217 if (!hdr)
4218 return -ENOMEM;
4220 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4222 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4223 if (!infoattr)
4224 goto nla_put_failure;
4226 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4227 survey->channel->center_freq);
4228 if (survey->filled & SURVEY_INFO_NOISE_DBM)
4229 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4230 survey->noise);
4231 if (survey->filled & SURVEY_INFO_IN_USE)
4232 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
4233 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4234 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4235 survey->channel_time);
4236 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4237 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4238 survey->channel_time_busy);
4239 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4240 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4241 survey->channel_time_ext_busy);
4242 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4243 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4244 survey->channel_time_rx);
4245 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4246 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4247 survey->channel_time_tx);
4249 nla_nest_end(msg, infoattr);
4251 return genlmsg_end(msg, hdr);
4253 nla_put_failure:
4254 genlmsg_cancel(msg, hdr);
4255 return -EMSGSIZE;
4258 static int nl80211_dump_survey(struct sk_buff *skb,
4259 struct netlink_callback *cb)
4261 struct survey_info survey;
4262 struct cfg80211_registered_device *dev;
4263 struct net_device *netdev;
4264 int survey_idx = cb->args[1];
4265 int res;
4267 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4268 if (res)
4269 return res;
4271 if (!dev->ops->dump_survey) {
4272 res = -EOPNOTSUPP;
4273 goto out_err;
4276 while (1) {
4277 struct ieee80211_channel *chan;
4279 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4280 &survey);
4281 if (res == -ENOENT)
4282 break;
4283 if (res)
4284 goto out_err;
4286 /* Survey without a channel doesn't make sense */
4287 if (!survey.channel) {
4288 res = -EINVAL;
4289 goto out;
4292 chan = ieee80211_get_channel(&dev->wiphy,
4293 survey.channel->center_freq);
4294 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4295 survey_idx++;
4296 continue;
4299 if (nl80211_send_survey(skb,
4300 NETLINK_CB(cb->skb).pid,
4301 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4302 netdev,
4303 &survey) < 0)
4304 goto out;
4305 survey_idx++;
4308 out:
4309 cb->args[1] = survey_idx;
4310 res = skb->len;
4311 out_err:
4312 nl80211_finish_netdev_dump(dev);
4313 return res;
4316 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4318 return auth_type <= NL80211_AUTHTYPE_MAX;
4321 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4323 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4324 NL80211_WPA_VERSION_2));
4327 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4330 struct net_device *dev = info->user_ptr[1];
4331 struct ieee80211_channel *chan;
4332 const u8 *bssid, *ssid, *ie = NULL;
4333 int err, ssid_len, ie_len = 0;
4334 enum nl80211_auth_type auth_type;
4335 struct key_parse key;
4336 bool local_state_change;
4338 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4339 return -EINVAL;
4341 if (!info->attrs[NL80211_ATTR_MAC])
4342 return -EINVAL;
4344 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4345 return -EINVAL;
4347 if (!info->attrs[NL80211_ATTR_SSID])
4348 return -EINVAL;
4350 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4351 return -EINVAL;
4353 err = nl80211_parse_key(info, &key);
4354 if (err)
4355 return err;
4357 if (key.idx >= 0) {
4358 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4359 return -EINVAL;
4360 if (!key.p.key || !key.p.key_len)
4361 return -EINVAL;
4362 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4363 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4364 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4365 key.p.key_len != WLAN_KEY_LEN_WEP104))
4366 return -EINVAL;
4367 if (key.idx > 4)
4368 return -EINVAL;
4369 } else {
4370 key.p.key_len = 0;
4371 key.p.key = NULL;
4374 if (key.idx >= 0) {
4375 int i;
4376 bool ok = false;
4377 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4378 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4379 ok = true;
4380 break;
4383 if (!ok)
4384 return -EINVAL;
4387 if (!rdev->ops->auth)
4388 return -EOPNOTSUPP;
4390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4391 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4392 return -EOPNOTSUPP;
4394 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4395 chan = ieee80211_get_channel(&rdev->wiphy,
4396 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4397 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4398 return -EINVAL;
4400 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4401 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4403 if (info->attrs[NL80211_ATTR_IE]) {
4404 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4405 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4408 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4409 if (!nl80211_valid_auth_type(auth_type))
4410 return -EINVAL;
4412 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4414 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4415 ssid, ssid_len, ie, ie_len,
4416 key.p.key, key.p.key_len, key.idx,
4417 local_state_change);
4420 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4421 struct genl_info *info,
4422 struct cfg80211_crypto_settings *settings,
4423 int cipher_limit)
4425 memset(settings, 0, sizeof(*settings));
4427 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4429 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4430 u16 proto;
4431 proto = nla_get_u16(
4432 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4433 settings->control_port_ethertype = cpu_to_be16(proto);
4434 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4435 proto != ETH_P_PAE)
4436 return -EINVAL;
4437 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4438 settings->control_port_no_encrypt = true;
4439 } else
4440 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4442 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4443 void *data;
4444 int len, i;
4446 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4447 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4448 settings->n_ciphers_pairwise = len / sizeof(u32);
4450 if (len % sizeof(u32))
4451 return -EINVAL;
4453 if (settings->n_ciphers_pairwise > cipher_limit)
4454 return -EINVAL;
4456 memcpy(settings->ciphers_pairwise, data, len);
4458 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4459 if (!cfg80211_supported_cipher_suite(
4460 &rdev->wiphy,
4461 settings->ciphers_pairwise[i]))
4462 return -EINVAL;
4465 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4466 settings->cipher_group =
4467 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4468 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4469 settings->cipher_group))
4470 return -EINVAL;
4473 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4474 settings->wpa_versions =
4475 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4476 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4477 return -EINVAL;
4480 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4481 void *data;
4482 int len;
4484 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4485 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4486 settings->n_akm_suites = len / sizeof(u32);
4488 if (len % sizeof(u32))
4489 return -EINVAL;
4491 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4492 return -EINVAL;
4494 memcpy(settings->akm_suites, data, len);
4497 return 0;
4500 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4502 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4503 struct net_device *dev = info->user_ptr[1];
4504 struct cfg80211_crypto_settings crypto;
4505 struct ieee80211_channel *chan;
4506 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4507 int err, ssid_len, ie_len = 0;
4508 bool use_mfp = false;
4509 u32 flags = 0;
4510 struct ieee80211_ht_cap *ht_capa = NULL;
4511 struct ieee80211_ht_cap *ht_capa_mask = NULL;
4513 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4514 return -EINVAL;
4516 if (!info->attrs[NL80211_ATTR_MAC] ||
4517 !info->attrs[NL80211_ATTR_SSID] ||
4518 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4519 return -EINVAL;
4521 if (!rdev->ops->assoc)
4522 return -EOPNOTSUPP;
4524 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4525 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4526 return -EOPNOTSUPP;
4528 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4530 chan = ieee80211_get_channel(&rdev->wiphy,
4531 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4532 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4533 return -EINVAL;
4535 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4536 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4538 if (info->attrs[NL80211_ATTR_IE]) {
4539 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4540 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4543 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4544 enum nl80211_mfp mfp =
4545 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4546 if (mfp == NL80211_MFP_REQUIRED)
4547 use_mfp = true;
4548 else if (mfp != NL80211_MFP_NO)
4549 return -EINVAL;
4552 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4553 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4555 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4556 flags |= ASSOC_REQ_DISABLE_HT;
4558 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4559 ht_capa_mask =
4560 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4562 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4563 if (!ht_capa_mask)
4564 return -EINVAL;
4565 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4568 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4569 if (!err)
4570 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4571 ssid, ssid_len, ie, ie_len, use_mfp,
4572 &crypto, flags, ht_capa,
4573 ht_capa_mask);
4575 return err;
4578 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4580 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4581 struct net_device *dev = info->user_ptr[1];
4582 const u8 *ie = NULL, *bssid;
4583 int ie_len = 0;
4584 u16 reason_code;
4585 bool local_state_change;
4587 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4588 return -EINVAL;
4590 if (!info->attrs[NL80211_ATTR_MAC])
4591 return -EINVAL;
4593 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4594 return -EINVAL;
4596 if (!rdev->ops->deauth)
4597 return -EOPNOTSUPP;
4599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4601 return -EOPNOTSUPP;
4603 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4605 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4606 if (reason_code == 0) {
4607 /* Reason Code 0 is reserved */
4608 return -EINVAL;
4611 if (info->attrs[NL80211_ATTR_IE]) {
4612 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4613 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4616 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4618 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4619 local_state_change);
4622 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4625 struct net_device *dev = info->user_ptr[1];
4626 const u8 *ie = NULL, *bssid;
4627 int ie_len = 0;
4628 u16 reason_code;
4629 bool local_state_change;
4631 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4632 return -EINVAL;
4634 if (!info->attrs[NL80211_ATTR_MAC])
4635 return -EINVAL;
4637 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4638 return -EINVAL;
4640 if (!rdev->ops->disassoc)
4641 return -EOPNOTSUPP;
4643 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4644 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4645 return -EOPNOTSUPP;
4647 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4649 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4650 if (reason_code == 0) {
4651 /* Reason Code 0 is reserved */
4652 return -EINVAL;
4655 if (info->attrs[NL80211_ATTR_IE]) {
4656 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4657 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4660 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4662 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4663 local_state_change);
4666 static bool
4667 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4668 int mcast_rate[IEEE80211_NUM_BANDS],
4669 int rateval)
4671 struct wiphy *wiphy = &rdev->wiphy;
4672 bool found = false;
4673 int band, i;
4675 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4676 struct ieee80211_supported_band *sband;
4678 sband = wiphy->bands[band];
4679 if (!sband)
4680 continue;
4682 for (i = 0; i < sband->n_bitrates; i++) {
4683 if (sband->bitrates[i].bitrate == rateval) {
4684 mcast_rate[band] = i + 1;
4685 found = true;
4686 break;
4691 return found;
4694 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4696 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4697 struct net_device *dev = info->user_ptr[1];
4698 struct cfg80211_ibss_params ibss;
4699 struct wiphy *wiphy;
4700 struct cfg80211_cached_keys *connkeys = NULL;
4701 int err;
4703 memset(&ibss, 0, sizeof(ibss));
4705 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4706 return -EINVAL;
4708 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4709 !info->attrs[NL80211_ATTR_SSID] ||
4710 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4711 return -EINVAL;
4713 ibss.beacon_interval = 100;
4715 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4716 ibss.beacon_interval =
4717 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4718 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4719 return -EINVAL;
4722 if (!rdev->ops->join_ibss)
4723 return -EOPNOTSUPP;
4725 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4726 return -EOPNOTSUPP;
4728 wiphy = &rdev->wiphy;
4730 if (info->attrs[NL80211_ATTR_MAC]) {
4731 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4733 if (!is_valid_ether_addr(ibss.bssid))
4734 return -EINVAL;
4736 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4737 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4739 if (info->attrs[NL80211_ATTR_IE]) {
4740 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4741 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4744 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4745 enum nl80211_channel_type channel_type;
4747 channel_type = nla_get_u32(
4748 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4749 if (channel_type != NL80211_CHAN_NO_HT &&
4750 channel_type != NL80211_CHAN_HT20 &&
4751 channel_type != NL80211_CHAN_HT40MINUS &&
4752 channel_type != NL80211_CHAN_HT40PLUS)
4753 return -EINVAL;
4755 if (channel_type != NL80211_CHAN_NO_HT &&
4756 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4757 return -EINVAL;
4759 ibss.channel_type = channel_type;
4760 } else {
4761 ibss.channel_type = NL80211_CHAN_NO_HT;
4764 ibss.channel = rdev_freq_to_chan(rdev,
4765 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4766 ibss.channel_type);
4767 if (!ibss.channel ||
4768 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4769 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4770 return -EINVAL;
4772 /* Both channels should be able to initiate communication */
4773 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4774 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4775 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4776 ibss.channel_type))
4777 return -EINVAL;
4779 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
4780 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4782 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4783 u8 *rates =
4784 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4785 int n_rates =
4786 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4787 struct ieee80211_supported_band *sband =
4788 wiphy->bands[ibss.channel->band];
4789 int err;
4791 err = ieee80211_get_ratemask(sband, rates, n_rates,
4792 &ibss.basic_rates);
4793 if (err)
4794 return err;
4797 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4798 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4799 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4800 return -EINVAL;
4802 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4803 connkeys = nl80211_parse_connkeys(rdev,
4804 info->attrs[NL80211_ATTR_KEYS]);
4805 if (IS_ERR(connkeys))
4806 return PTR_ERR(connkeys);
4809 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
4810 if (err)
4811 kfree(connkeys);
4812 return err;
4815 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4818 struct net_device *dev = info->user_ptr[1];
4820 if (!rdev->ops->leave_ibss)
4821 return -EOPNOTSUPP;
4823 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4824 return -EOPNOTSUPP;
4826 return cfg80211_leave_ibss(rdev, dev, false);
4829 #ifdef CONFIG_NL80211_TESTMODE
4830 static struct genl_multicast_group nl80211_testmode_mcgrp = {
4831 .name = "testmode",
4834 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4837 int err;
4839 if (!info->attrs[NL80211_ATTR_TESTDATA])
4840 return -EINVAL;
4842 err = -EOPNOTSUPP;
4843 if (rdev->ops->testmode_cmd) {
4844 rdev->testmode_info = info;
4845 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4846 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4847 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4848 rdev->testmode_info = NULL;
4851 return err;
4854 static int nl80211_testmode_dump(struct sk_buff *skb,
4855 struct netlink_callback *cb)
4857 struct cfg80211_registered_device *rdev;
4858 int err;
4859 long phy_idx;
4860 void *data = NULL;
4861 int data_len = 0;
4863 if (cb->args[0]) {
4865 * 0 is a valid index, but not valid for args[0],
4866 * so we need to offset by 1.
4868 phy_idx = cb->args[0] - 1;
4869 } else {
4870 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4871 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4872 nl80211_policy);
4873 if (err)
4874 return err;
4875 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4876 phy_idx = nla_get_u32(
4877 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4878 } else {
4879 struct net_device *netdev;
4881 err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4882 nl80211_fam.attrbuf,
4883 &rdev, &netdev);
4884 if (err)
4885 return err;
4886 dev_put(netdev);
4887 phy_idx = rdev->wiphy_idx;
4888 cfg80211_unlock_rdev(rdev);
4890 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4891 cb->args[1] =
4892 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4895 if (cb->args[1]) {
4896 data = nla_data((void *)cb->args[1]);
4897 data_len = nla_len((void *)cb->args[1]);
4900 mutex_lock(&cfg80211_mutex);
4901 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4902 if (!rdev) {
4903 mutex_unlock(&cfg80211_mutex);
4904 return -ENOENT;
4906 cfg80211_lock_rdev(rdev);
4907 mutex_unlock(&cfg80211_mutex);
4909 if (!rdev->ops->testmode_dump) {
4910 err = -EOPNOTSUPP;
4911 goto out_err;
4914 while (1) {
4915 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4916 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4917 NL80211_CMD_TESTMODE);
4918 struct nlattr *tmdata;
4920 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
4921 genlmsg_cancel(skb, hdr);
4922 break;
4925 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4926 if (!tmdata) {
4927 genlmsg_cancel(skb, hdr);
4928 break;
4930 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4931 data, data_len);
4932 nla_nest_end(skb, tmdata);
4934 if (err == -ENOBUFS || err == -ENOENT) {
4935 genlmsg_cancel(skb, hdr);
4936 break;
4937 } else if (err) {
4938 genlmsg_cancel(skb, hdr);
4939 goto out_err;
4942 genlmsg_end(skb, hdr);
4945 err = skb->len;
4946 /* see above */
4947 cb->args[0] = phy_idx + 1;
4948 out_err:
4949 cfg80211_unlock_rdev(rdev);
4950 return err;
4953 static struct sk_buff *
4954 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4955 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4957 struct sk_buff *skb;
4958 void *hdr;
4959 struct nlattr *data;
4961 skb = nlmsg_new(approxlen + 100, gfp);
4962 if (!skb)
4963 return NULL;
4965 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4966 if (!hdr) {
4967 kfree_skb(skb);
4968 return NULL;
4971 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4972 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4974 ((void **)skb->cb)[0] = rdev;
4975 ((void **)skb->cb)[1] = hdr;
4976 ((void **)skb->cb)[2] = data;
4978 return skb;
4980 nla_put_failure:
4981 kfree_skb(skb);
4982 return NULL;
4985 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4986 int approxlen)
4988 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4990 if (WARN_ON(!rdev->testmode_info))
4991 return NULL;
4993 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4994 rdev->testmode_info->snd_pid,
4995 rdev->testmode_info->snd_seq,
4996 GFP_KERNEL);
4998 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5000 int cfg80211_testmode_reply(struct sk_buff *skb)
5002 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5003 void *hdr = ((void **)skb->cb)[1];
5004 struct nlattr *data = ((void **)skb->cb)[2];
5006 if (WARN_ON(!rdev->testmode_info)) {
5007 kfree_skb(skb);
5008 return -EINVAL;
5011 nla_nest_end(skb, data);
5012 genlmsg_end(skb, hdr);
5013 return genlmsg_reply(skb, rdev->testmode_info);
5015 EXPORT_SYMBOL(cfg80211_testmode_reply);
5017 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5018 int approxlen, gfp_t gfp)
5020 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5022 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5024 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5026 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5028 void *hdr = ((void **)skb->cb)[1];
5029 struct nlattr *data = ((void **)skb->cb)[2];
5031 nla_nest_end(skb, data);
5032 genlmsg_end(skb, hdr);
5033 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5035 EXPORT_SYMBOL(cfg80211_testmode_event);
5036 #endif
5038 static int nl80211_connect(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 struct cfg80211_connect_params connect;
5043 struct wiphy *wiphy;
5044 struct cfg80211_cached_keys *connkeys = NULL;
5045 int err;
5047 memset(&connect, 0, sizeof(connect));
5049 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5050 return -EINVAL;
5052 if (!info->attrs[NL80211_ATTR_SSID] ||
5053 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5054 return -EINVAL;
5056 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5057 connect.auth_type =
5058 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5059 if (!nl80211_valid_auth_type(connect.auth_type))
5060 return -EINVAL;
5061 } else
5062 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5064 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5066 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
5067 NL80211_MAX_NR_CIPHER_SUITES);
5068 if (err)
5069 return err;
5071 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5072 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5073 return -EOPNOTSUPP;
5075 wiphy = &rdev->wiphy;
5077 if (info->attrs[NL80211_ATTR_MAC])
5078 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5079 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5080 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5082 if (info->attrs[NL80211_ATTR_IE]) {
5083 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5084 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5087 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5088 connect.channel =
5089 ieee80211_get_channel(wiphy,
5090 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5091 if (!connect.channel ||
5092 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5093 return -EINVAL;
5096 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5097 connkeys = nl80211_parse_connkeys(rdev,
5098 info->attrs[NL80211_ATTR_KEYS]);
5099 if (IS_ERR(connkeys))
5100 return PTR_ERR(connkeys);
5103 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5104 connect.flags |= ASSOC_REQ_DISABLE_HT;
5106 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5107 memcpy(&connect.ht_capa_mask,
5108 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5109 sizeof(connect.ht_capa_mask));
5111 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5112 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5113 return -EINVAL;
5114 memcpy(&connect.ht_capa,
5115 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5116 sizeof(connect.ht_capa));
5119 err = cfg80211_connect(rdev, dev, &connect, connkeys);
5120 if (err)
5121 kfree(connkeys);
5122 return err;
5125 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5128 struct net_device *dev = info->user_ptr[1];
5129 u16 reason;
5131 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5132 reason = WLAN_REASON_DEAUTH_LEAVING;
5133 else
5134 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5136 if (reason == 0)
5137 return -EINVAL;
5139 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5140 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5141 return -EOPNOTSUPP;
5143 return cfg80211_disconnect(rdev, dev, reason, true);
5146 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5148 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5149 struct net *net;
5150 int err;
5151 u32 pid;
5153 if (!info->attrs[NL80211_ATTR_PID])
5154 return -EINVAL;
5156 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5158 net = get_net_ns_by_pid(pid);
5159 if (IS_ERR(net))
5160 return PTR_ERR(net);
5162 err = 0;
5164 /* check if anything to do */
5165 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5166 err = cfg80211_switch_netns(rdev, net);
5168 put_net(net);
5169 return err;
5172 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5175 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5176 struct cfg80211_pmksa *pmksa) = NULL;
5177 struct net_device *dev = info->user_ptr[1];
5178 struct cfg80211_pmksa pmksa;
5180 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5182 if (!info->attrs[NL80211_ATTR_MAC])
5183 return -EINVAL;
5185 if (!info->attrs[NL80211_ATTR_PMKID])
5186 return -EINVAL;
5188 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5189 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5191 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5192 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5193 return -EOPNOTSUPP;
5195 switch (info->genlhdr->cmd) {
5196 case NL80211_CMD_SET_PMKSA:
5197 rdev_ops = rdev->ops->set_pmksa;
5198 break;
5199 case NL80211_CMD_DEL_PMKSA:
5200 rdev_ops = rdev->ops->del_pmksa;
5201 break;
5202 default:
5203 WARN_ON(1);
5204 break;
5207 if (!rdev_ops)
5208 return -EOPNOTSUPP;
5210 return rdev_ops(&rdev->wiphy, dev, &pmksa);
5213 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5216 struct net_device *dev = info->user_ptr[1];
5218 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5219 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5220 return -EOPNOTSUPP;
5222 if (!rdev->ops->flush_pmksa)
5223 return -EOPNOTSUPP;
5225 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
5228 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5231 struct net_device *dev = info->user_ptr[1];
5232 u8 action_code, dialog_token;
5233 u16 status_code;
5234 u8 *peer;
5236 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5237 !rdev->ops->tdls_mgmt)
5238 return -EOPNOTSUPP;
5240 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5241 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5242 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5243 !info->attrs[NL80211_ATTR_IE] ||
5244 !info->attrs[NL80211_ATTR_MAC])
5245 return -EINVAL;
5247 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5248 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5249 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5250 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5252 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5253 dialog_token, status_code,
5254 nla_data(info->attrs[NL80211_ATTR_IE]),
5255 nla_len(info->attrs[NL80211_ATTR_IE]));
5258 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5260 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5261 struct net_device *dev = info->user_ptr[1];
5262 enum nl80211_tdls_operation operation;
5263 u8 *peer;
5265 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5266 !rdev->ops->tdls_oper)
5267 return -EOPNOTSUPP;
5269 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5270 !info->attrs[NL80211_ATTR_MAC])
5271 return -EINVAL;
5273 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5274 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5276 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5279 static int nl80211_remain_on_channel(struct sk_buff *skb,
5280 struct genl_info *info)
5282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5283 struct net_device *dev = info->user_ptr[1];
5284 struct ieee80211_channel *chan;
5285 struct sk_buff *msg;
5286 void *hdr;
5287 u64 cookie;
5288 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5289 u32 freq, duration;
5290 int err;
5292 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5293 !info->attrs[NL80211_ATTR_DURATION])
5294 return -EINVAL;
5296 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5299 * We should be on that channel for at least one jiffie,
5300 * and more than 5 seconds seems excessive.
5302 if (!duration || !msecs_to_jiffies(duration) ||
5303 duration > rdev->wiphy.max_remain_on_channel_duration)
5304 return -EINVAL;
5306 if (!rdev->ops->remain_on_channel ||
5307 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
5308 return -EOPNOTSUPP;
5310 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5311 channel_type = nla_get_u32(
5312 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5313 if (channel_type != NL80211_CHAN_NO_HT &&
5314 channel_type != NL80211_CHAN_HT20 &&
5315 channel_type != NL80211_CHAN_HT40PLUS &&
5316 channel_type != NL80211_CHAN_HT40MINUS)
5317 return -EINVAL;
5320 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5321 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5322 if (chan == NULL)
5323 return -EINVAL;
5325 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5326 if (!msg)
5327 return -ENOMEM;
5329 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5330 NL80211_CMD_REMAIN_ON_CHANNEL);
5332 if (IS_ERR(hdr)) {
5333 err = PTR_ERR(hdr);
5334 goto free_msg;
5337 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5338 channel_type, duration, &cookie);
5340 if (err)
5341 goto free_msg;
5343 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5345 genlmsg_end(msg, hdr);
5347 return genlmsg_reply(msg, info);
5349 nla_put_failure:
5350 err = -ENOBUFS;
5351 free_msg:
5352 nlmsg_free(msg);
5353 return err;
5356 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5357 struct genl_info *info)
5359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5360 struct net_device *dev = info->user_ptr[1];
5361 u64 cookie;
5363 if (!info->attrs[NL80211_ATTR_COOKIE])
5364 return -EINVAL;
5366 if (!rdev->ops->cancel_remain_on_channel)
5367 return -EOPNOTSUPP;
5369 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5371 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
5374 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5375 u8 *rates, u8 rates_len)
5377 u8 i;
5378 u32 mask = 0;
5380 for (i = 0; i < rates_len; i++) {
5381 int rate = (rates[i] & 0x7f) * 5;
5382 int ridx;
5383 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5384 struct ieee80211_rate *srate =
5385 &sband->bitrates[ridx];
5386 if (rate == srate->bitrate) {
5387 mask |= 1 << ridx;
5388 break;
5391 if (ridx == sband->n_bitrates)
5392 return 0; /* rate not found */
5395 return mask;
5398 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
5399 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5400 .len = NL80211_MAX_SUPP_RATES },
5403 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5404 struct genl_info *info)
5406 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5408 struct cfg80211_bitrate_mask mask;
5409 int rem, i;
5410 struct net_device *dev = info->user_ptr[1];
5411 struct nlattr *tx_rates;
5412 struct ieee80211_supported_band *sband;
5414 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5415 return -EINVAL;
5417 if (!rdev->ops->set_bitrate_mask)
5418 return -EOPNOTSUPP;
5420 memset(&mask, 0, sizeof(mask));
5421 /* Default to all rates enabled */
5422 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5423 sband = rdev->wiphy.bands[i];
5424 mask.control[i].legacy =
5425 sband ? (1 << sband->n_bitrates) - 1 : 0;
5429 * The nested attribute uses enum nl80211_band as the index. This maps
5430 * directly to the enum ieee80211_band values used in cfg80211.
5432 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5434 enum ieee80211_band band = nla_type(tx_rates);
5435 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5436 return -EINVAL;
5437 sband = rdev->wiphy.bands[band];
5438 if (sband == NULL)
5439 return -EINVAL;
5440 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5441 nla_len(tx_rates), nl80211_txattr_policy);
5442 if (tb[NL80211_TXRATE_LEGACY]) {
5443 mask.control[band].legacy = rateset_to_mask(
5444 sband,
5445 nla_data(tb[NL80211_TXRATE_LEGACY]),
5446 nla_len(tb[NL80211_TXRATE_LEGACY]));
5447 if (mask.control[band].legacy == 0)
5448 return -EINVAL;
5452 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
5455 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
5457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5458 struct net_device *dev = info->user_ptr[1];
5459 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
5461 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5462 return -EINVAL;
5464 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5465 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
5467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5468 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5469 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5470 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5471 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5472 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5473 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5474 return -EOPNOTSUPP;
5476 /* not much point in registering if we can't reply */
5477 if (!rdev->ops->mgmt_tx)
5478 return -EOPNOTSUPP;
5480 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
5481 frame_type,
5482 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5483 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
5486 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
5488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5489 struct net_device *dev = info->user_ptr[1];
5490 struct ieee80211_channel *chan;
5491 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5492 bool channel_type_valid = false;
5493 u32 freq;
5494 int err;
5495 void *hdr = NULL;
5496 u64 cookie;
5497 struct sk_buff *msg = NULL;
5498 unsigned int wait = 0;
5499 bool offchan, no_cck, dont_wait_for_ack;
5501 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
5503 if (!info->attrs[NL80211_ATTR_FRAME] ||
5504 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5505 return -EINVAL;
5507 if (!rdev->ops->mgmt_tx)
5508 return -EOPNOTSUPP;
5510 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5511 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5512 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5513 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5514 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5515 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5516 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5517 return -EOPNOTSUPP;
5519 if (info->attrs[NL80211_ATTR_DURATION]) {
5520 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5521 return -EINVAL;
5522 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5525 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5526 channel_type = nla_get_u32(
5527 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5528 if (channel_type != NL80211_CHAN_NO_HT &&
5529 channel_type != NL80211_CHAN_HT20 &&
5530 channel_type != NL80211_CHAN_HT40PLUS &&
5531 channel_type != NL80211_CHAN_HT40MINUS)
5532 return -EINVAL;
5533 channel_type_valid = true;
5536 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5538 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5539 return -EINVAL;
5541 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5543 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5544 chan = rdev_freq_to_chan(rdev, freq, channel_type);
5545 if (chan == NULL)
5546 return -EINVAL;
5548 if (!dont_wait_for_ack) {
5549 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5550 if (!msg)
5551 return -ENOMEM;
5553 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5554 NL80211_CMD_FRAME);
5556 if (IS_ERR(hdr)) {
5557 err = PTR_ERR(hdr);
5558 goto free_msg;
5562 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5563 channel_type_valid, wait,
5564 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5565 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5566 no_cck, dont_wait_for_ack, &cookie);
5567 if (err)
5568 goto free_msg;
5570 if (msg) {
5571 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5573 genlmsg_end(msg, hdr);
5574 return genlmsg_reply(msg, info);
5577 return 0;
5579 nla_put_failure:
5580 err = -ENOBUFS;
5581 free_msg:
5582 nlmsg_free(msg);
5583 return err;
5586 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5589 struct net_device *dev = info->user_ptr[1];
5590 u64 cookie;
5592 if (!info->attrs[NL80211_ATTR_COOKIE])
5593 return -EINVAL;
5595 if (!rdev->ops->mgmt_tx_cancel_wait)
5596 return -EOPNOTSUPP;
5598 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5599 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5601 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5602 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5603 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5604 return -EOPNOTSUPP;
5606 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5608 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5611 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5614 struct wireless_dev *wdev;
5615 struct net_device *dev = info->user_ptr[1];
5616 u8 ps_state;
5617 bool state;
5618 int err;
5620 if (!info->attrs[NL80211_ATTR_PS_STATE])
5621 return -EINVAL;
5623 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5625 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5626 return -EINVAL;
5628 wdev = dev->ieee80211_ptr;
5630 if (!rdev->ops->set_power_mgmt)
5631 return -EOPNOTSUPP;
5633 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5635 if (state == wdev->ps)
5636 return 0;
5638 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5639 wdev->ps_timeout);
5640 if (!err)
5641 wdev->ps = state;
5642 return err;
5645 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5648 enum nl80211_ps_state ps_state;
5649 struct wireless_dev *wdev;
5650 struct net_device *dev = info->user_ptr[1];
5651 struct sk_buff *msg;
5652 void *hdr;
5653 int err;
5655 wdev = dev->ieee80211_ptr;
5657 if (!rdev->ops->set_power_mgmt)
5658 return -EOPNOTSUPP;
5660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5661 if (!msg)
5662 return -ENOMEM;
5664 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5665 NL80211_CMD_GET_POWER_SAVE);
5666 if (!hdr) {
5667 err = -ENOBUFS;
5668 goto free_msg;
5671 if (wdev->ps)
5672 ps_state = NL80211_PS_ENABLED;
5673 else
5674 ps_state = NL80211_PS_DISABLED;
5676 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5678 genlmsg_end(msg, hdr);
5679 return genlmsg_reply(msg, info);
5681 nla_put_failure:
5682 err = -ENOBUFS;
5683 free_msg:
5684 nlmsg_free(msg);
5685 return err;
5688 static struct nla_policy
5689 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5690 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5691 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5692 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5695 static int nl80211_set_cqm_rssi(struct genl_info *info,
5696 s32 threshold, u32 hysteresis)
5698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5699 struct wireless_dev *wdev;
5700 struct net_device *dev = info->user_ptr[1];
5702 if (threshold > 0)
5703 return -EINVAL;
5705 wdev = dev->ieee80211_ptr;
5707 if (!rdev->ops->set_cqm_rssi_config)
5708 return -EOPNOTSUPP;
5710 if (wdev->iftype != NL80211_IFTYPE_STATION &&
5711 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5712 return -EOPNOTSUPP;
5714 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5715 threshold, hysteresis);
5718 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5720 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5721 struct nlattr *cqm;
5722 int err;
5724 cqm = info->attrs[NL80211_ATTR_CQM];
5725 if (!cqm) {
5726 err = -EINVAL;
5727 goto out;
5730 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5731 nl80211_attr_cqm_policy);
5732 if (err)
5733 goto out;
5735 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5736 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5737 s32 threshold;
5738 u32 hysteresis;
5739 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5740 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5741 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5742 } else
5743 err = -EINVAL;
5745 out:
5746 return err;
5749 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5751 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5752 struct net_device *dev = info->user_ptr[1];
5753 struct mesh_config cfg;
5754 struct mesh_setup setup;
5755 int err;
5757 /* start with default */
5758 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
5759 memcpy(&setup, &default_mesh_setup, sizeof(setup));
5761 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
5762 /* and parse parameters if given */
5763 err = nl80211_parse_mesh_config(info, &cfg, NULL);
5764 if (err)
5765 return err;
5768 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5769 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5770 return -EINVAL;
5772 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5773 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5775 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5776 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5777 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5778 return -EINVAL;
5780 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5781 /* parse additional setup parameters if given */
5782 err = nl80211_parse_mesh_setup(info, &setup);
5783 if (err)
5784 return err;
5787 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
5790 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5793 struct net_device *dev = info->user_ptr[1];
5795 return cfg80211_leave_mesh(rdev, dev);
5798 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5800 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5801 struct sk_buff *msg;
5802 void *hdr;
5804 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5805 return -EOPNOTSUPP;
5807 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5808 if (!msg)
5809 return -ENOMEM;
5811 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5812 NL80211_CMD_GET_WOWLAN);
5813 if (!hdr)
5814 goto nla_put_failure;
5816 if (rdev->wowlan) {
5817 struct nlattr *nl_wowlan;
5819 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5820 if (!nl_wowlan)
5821 goto nla_put_failure;
5823 if (rdev->wowlan->any)
5824 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5825 if (rdev->wowlan->disconnect)
5826 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5827 if (rdev->wowlan->magic_pkt)
5828 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
5829 if (rdev->wowlan->gtk_rekey_failure)
5830 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5831 if (rdev->wowlan->eap_identity_req)
5832 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5833 if (rdev->wowlan->four_way_handshake)
5834 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5835 if (rdev->wowlan->rfkill_release)
5836 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
5837 if (rdev->wowlan->n_patterns) {
5838 struct nlattr *nl_pats, *nl_pat;
5839 int i, pat_len;
5841 nl_pats = nla_nest_start(msg,
5842 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5843 if (!nl_pats)
5844 goto nla_put_failure;
5846 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5847 nl_pat = nla_nest_start(msg, i + 1);
5848 if (!nl_pat)
5849 goto nla_put_failure;
5850 pat_len = rdev->wowlan->patterns[i].pattern_len;
5851 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5852 DIV_ROUND_UP(pat_len, 8),
5853 rdev->wowlan->patterns[i].mask);
5854 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5855 pat_len,
5856 rdev->wowlan->patterns[i].pattern);
5857 nla_nest_end(msg, nl_pat);
5859 nla_nest_end(msg, nl_pats);
5862 nla_nest_end(msg, nl_wowlan);
5865 genlmsg_end(msg, hdr);
5866 return genlmsg_reply(msg, info);
5868 nla_put_failure:
5869 nlmsg_free(msg);
5870 return -ENOBUFS;
5873 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5875 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5876 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5877 struct cfg80211_wowlan no_triggers = {};
5878 struct cfg80211_wowlan new_triggers = {};
5879 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5880 int err, i;
5882 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5883 return -EOPNOTSUPP;
5885 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5886 goto no_triggers;
5888 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5889 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5890 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5891 nl80211_wowlan_policy);
5892 if (err)
5893 return err;
5895 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5896 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5897 return -EINVAL;
5898 new_triggers.any = true;
5901 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5902 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5903 return -EINVAL;
5904 new_triggers.disconnect = true;
5907 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5908 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5909 return -EINVAL;
5910 new_triggers.magic_pkt = true;
5913 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5914 return -EINVAL;
5916 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5917 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5918 return -EINVAL;
5919 new_triggers.gtk_rekey_failure = true;
5922 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5923 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5924 return -EINVAL;
5925 new_triggers.eap_identity_req = true;
5928 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5929 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5930 return -EINVAL;
5931 new_triggers.four_way_handshake = true;
5934 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5935 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5936 return -EINVAL;
5937 new_triggers.rfkill_release = true;
5940 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5941 struct nlattr *pat;
5942 int n_patterns = 0;
5943 int rem, pat_len, mask_len;
5944 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5946 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5947 rem)
5948 n_patterns++;
5949 if (n_patterns > wowlan->n_patterns)
5950 return -EINVAL;
5952 new_triggers.patterns = kcalloc(n_patterns,
5953 sizeof(new_triggers.patterns[0]),
5954 GFP_KERNEL);
5955 if (!new_triggers.patterns)
5956 return -ENOMEM;
5958 new_triggers.n_patterns = n_patterns;
5959 i = 0;
5961 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5962 rem) {
5963 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5964 nla_data(pat), nla_len(pat), NULL);
5965 err = -EINVAL;
5966 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5967 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5968 goto error;
5969 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5970 mask_len = DIV_ROUND_UP(pat_len, 8);
5971 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5972 mask_len)
5973 goto error;
5974 if (pat_len > wowlan->pattern_max_len ||
5975 pat_len < wowlan->pattern_min_len)
5976 goto error;
5978 new_triggers.patterns[i].mask =
5979 kmalloc(mask_len + pat_len, GFP_KERNEL);
5980 if (!new_triggers.patterns[i].mask) {
5981 err = -ENOMEM;
5982 goto error;
5984 new_triggers.patterns[i].pattern =
5985 new_triggers.patterns[i].mask + mask_len;
5986 memcpy(new_triggers.patterns[i].mask,
5987 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5988 mask_len);
5989 new_triggers.patterns[i].pattern_len = pat_len;
5990 memcpy(new_triggers.patterns[i].pattern,
5991 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5992 pat_len);
5993 i++;
5997 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5998 struct cfg80211_wowlan *ntrig;
5999 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6000 GFP_KERNEL);
6001 if (!ntrig) {
6002 err = -ENOMEM;
6003 goto error;
6005 cfg80211_rdev_free_wowlan(rdev);
6006 rdev->wowlan = ntrig;
6007 } else {
6008 no_triggers:
6009 cfg80211_rdev_free_wowlan(rdev);
6010 rdev->wowlan = NULL;
6013 return 0;
6014 error:
6015 for (i = 0; i < new_triggers.n_patterns; i++)
6016 kfree(new_triggers.patterns[i].mask);
6017 kfree(new_triggers.patterns);
6018 return err;
6021 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6023 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6024 struct net_device *dev = info->user_ptr[1];
6025 struct wireless_dev *wdev = dev->ieee80211_ptr;
6026 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6027 struct cfg80211_gtk_rekey_data rekey_data;
6028 int err;
6030 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6031 return -EINVAL;
6033 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6034 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6035 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6036 nl80211_rekey_policy);
6037 if (err)
6038 return err;
6040 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6041 return -ERANGE;
6042 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6043 return -ERANGE;
6044 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6045 return -ERANGE;
6047 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6048 NL80211_KEK_LEN);
6049 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6050 NL80211_KCK_LEN);
6051 memcpy(rekey_data.replay_ctr,
6052 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6053 NL80211_REPLAY_CTR_LEN);
6055 wdev_lock(wdev);
6056 if (!wdev->current_bss) {
6057 err = -ENOTCONN;
6058 goto out;
6061 if (!rdev->ops->set_rekey_data) {
6062 err = -EOPNOTSUPP;
6063 goto out;
6066 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6067 out:
6068 wdev_unlock(wdev);
6069 return err;
6072 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6073 struct genl_info *info)
6075 struct net_device *dev = info->user_ptr[1];
6076 struct wireless_dev *wdev = dev->ieee80211_ptr;
6078 if (wdev->iftype != NL80211_IFTYPE_AP &&
6079 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6080 return -EINVAL;
6082 if (wdev->ap_unexpected_nlpid)
6083 return -EBUSY;
6085 wdev->ap_unexpected_nlpid = info->snd_pid;
6086 return 0;
6089 static int nl80211_probe_client(struct sk_buff *skb,
6090 struct genl_info *info)
6092 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6093 struct net_device *dev = info->user_ptr[1];
6094 struct wireless_dev *wdev = dev->ieee80211_ptr;
6095 struct sk_buff *msg;
6096 void *hdr;
6097 const u8 *addr;
6098 u64 cookie;
6099 int err;
6101 if (wdev->iftype != NL80211_IFTYPE_AP &&
6102 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6103 return -EOPNOTSUPP;
6105 if (!info->attrs[NL80211_ATTR_MAC])
6106 return -EINVAL;
6108 if (!rdev->ops->probe_client)
6109 return -EOPNOTSUPP;
6111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6112 if (!msg)
6113 return -ENOMEM;
6115 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6116 NL80211_CMD_PROBE_CLIENT);
6118 if (IS_ERR(hdr)) {
6119 err = PTR_ERR(hdr);
6120 goto free_msg;
6123 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6125 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6126 if (err)
6127 goto free_msg;
6129 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6131 genlmsg_end(msg, hdr);
6133 return genlmsg_reply(msg, info);
6135 nla_put_failure:
6136 err = -ENOBUFS;
6137 free_msg:
6138 nlmsg_free(msg);
6139 return err;
6142 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6144 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6146 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6147 return -EOPNOTSUPP;
6149 if (rdev->ap_beacons_nlpid)
6150 return -EBUSY;
6152 rdev->ap_beacons_nlpid = info->snd_pid;
6154 return 0;
6157 #define NL80211_FLAG_NEED_WIPHY 0x01
6158 #define NL80211_FLAG_NEED_NETDEV 0x02
6159 #define NL80211_FLAG_NEED_RTNL 0x04
6160 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6161 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6162 NL80211_FLAG_CHECK_NETDEV_UP)
6164 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6165 struct genl_info *info)
6167 struct cfg80211_registered_device *rdev;
6168 struct net_device *dev;
6169 int err;
6170 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6172 if (rtnl)
6173 rtnl_lock();
6175 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
6176 rdev = cfg80211_get_dev_from_info(info);
6177 if (IS_ERR(rdev)) {
6178 if (rtnl)
6179 rtnl_unlock();
6180 return PTR_ERR(rdev);
6182 info->user_ptr[0] = rdev;
6183 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6184 err = get_rdev_dev_by_ifindex(genl_info_net(info), info->attrs,
6185 &rdev, &dev);
6186 if (err) {
6187 if (rtnl)
6188 rtnl_unlock();
6189 return err;
6191 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6192 !netif_running(dev)) {
6193 cfg80211_unlock_rdev(rdev);
6194 dev_put(dev);
6195 if (rtnl)
6196 rtnl_unlock();
6197 return -ENETDOWN;
6199 info->user_ptr[0] = rdev;
6200 info->user_ptr[1] = dev;
6203 return 0;
6206 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6207 struct genl_info *info)
6209 if (info->user_ptr[0])
6210 cfg80211_unlock_rdev(info->user_ptr[0]);
6211 if (info->user_ptr[1])
6212 dev_put(info->user_ptr[1]);
6213 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6214 rtnl_unlock();
6217 static struct genl_ops nl80211_ops[] = {
6219 .cmd = NL80211_CMD_GET_WIPHY,
6220 .doit = nl80211_get_wiphy,
6221 .dumpit = nl80211_dump_wiphy,
6222 .policy = nl80211_policy,
6223 /* can be retrieved by unprivileged users */
6224 .internal_flags = NL80211_FLAG_NEED_WIPHY,
6227 .cmd = NL80211_CMD_SET_WIPHY,
6228 .doit = nl80211_set_wiphy,
6229 .policy = nl80211_policy,
6230 .flags = GENL_ADMIN_PERM,
6231 .internal_flags = NL80211_FLAG_NEED_RTNL,
6234 .cmd = NL80211_CMD_GET_INTERFACE,
6235 .doit = nl80211_get_interface,
6236 .dumpit = nl80211_dump_interface,
6237 .policy = nl80211_policy,
6238 /* can be retrieved by unprivileged users */
6239 .internal_flags = NL80211_FLAG_NEED_NETDEV,
6242 .cmd = NL80211_CMD_SET_INTERFACE,
6243 .doit = nl80211_set_interface,
6244 .policy = nl80211_policy,
6245 .flags = GENL_ADMIN_PERM,
6246 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6247 NL80211_FLAG_NEED_RTNL,
6250 .cmd = NL80211_CMD_NEW_INTERFACE,
6251 .doit = nl80211_new_interface,
6252 .policy = nl80211_policy,
6253 .flags = GENL_ADMIN_PERM,
6254 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6255 NL80211_FLAG_NEED_RTNL,
6258 .cmd = NL80211_CMD_DEL_INTERFACE,
6259 .doit = nl80211_del_interface,
6260 .policy = nl80211_policy,
6261 .flags = GENL_ADMIN_PERM,
6262 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6263 NL80211_FLAG_NEED_RTNL,
6266 .cmd = NL80211_CMD_GET_KEY,
6267 .doit = nl80211_get_key,
6268 .policy = nl80211_policy,
6269 .flags = GENL_ADMIN_PERM,
6270 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6271 NL80211_FLAG_NEED_RTNL,
6274 .cmd = NL80211_CMD_SET_KEY,
6275 .doit = nl80211_set_key,
6276 .policy = nl80211_policy,
6277 .flags = GENL_ADMIN_PERM,
6278 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6279 NL80211_FLAG_NEED_RTNL,
6282 .cmd = NL80211_CMD_NEW_KEY,
6283 .doit = nl80211_new_key,
6284 .policy = nl80211_policy,
6285 .flags = GENL_ADMIN_PERM,
6286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6287 NL80211_FLAG_NEED_RTNL,
6290 .cmd = NL80211_CMD_DEL_KEY,
6291 .doit = nl80211_del_key,
6292 .policy = nl80211_policy,
6293 .flags = GENL_ADMIN_PERM,
6294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6295 NL80211_FLAG_NEED_RTNL,
6298 .cmd = NL80211_CMD_SET_BEACON,
6299 .policy = nl80211_policy,
6300 .flags = GENL_ADMIN_PERM,
6301 .doit = nl80211_addset_beacon,
6302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6303 NL80211_FLAG_NEED_RTNL,
6306 .cmd = NL80211_CMD_NEW_BEACON,
6307 .policy = nl80211_policy,
6308 .flags = GENL_ADMIN_PERM,
6309 .doit = nl80211_addset_beacon,
6310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6311 NL80211_FLAG_NEED_RTNL,
6314 .cmd = NL80211_CMD_DEL_BEACON,
6315 .policy = nl80211_policy,
6316 .flags = GENL_ADMIN_PERM,
6317 .doit = nl80211_del_beacon,
6318 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6319 NL80211_FLAG_NEED_RTNL,
6322 .cmd = NL80211_CMD_GET_STATION,
6323 .doit = nl80211_get_station,
6324 .dumpit = nl80211_dump_station,
6325 .policy = nl80211_policy,
6326 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6327 NL80211_FLAG_NEED_RTNL,
6330 .cmd = NL80211_CMD_SET_STATION,
6331 .doit = nl80211_set_station,
6332 .policy = nl80211_policy,
6333 .flags = GENL_ADMIN_PERM,
6334 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6335 NL80211_FLAG_NEED_RTNL,
6338 .cmd = NL80211_CMD_NEW_STATION,
6339 .doit = nl80211_new_station,
6340 .policy = nl80211_policy,
6341 .flags = GENL_ADMIN_PERM,
6342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6343 NL80211_FLAG_NEED_RTNL,
6346 .cmd = NL80211_CMD_DEL_STATION,
6347 .doit = nl80211_del_station,
6348 .policy = nl80211_policy,
6349 .flags = GENL_ADMIN_PERM,
6350 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6351 NL80211_FLAG_NEED_RTNL,
6354 .cmd = NL80211_CMD_GET_MPATH,
6355 .doit = nl80211_get_mpath,
6356 .dumpit = nl80211_dump_mpath,
6357 .policy = nl80211_policy,
6358 .flags = GENL_ADMIN_PERM,
6359 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6360 NL80211_FLAG_NEED_RTNL,
6363 .cmd = NL80211_CMD_SET_MPATH,
6364 .doit = nl80211_set_mpath,
6365 .policy = nl80211_policy,
6366 .flags = GENL_ADMIN_PERM,
6367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6368 NL80211_FLAG_NEED_RTNL,
6371 .cmd = NL80211_CMD_NEW_MPATH,
6372 .doit = nl80211_new_mpath,
6373 .policy = nl80211_policy,
6374 .flags = GENL_ADMIN_PERM,
6375 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6376 NL80211_FLAG_NEED_RTNL,
6379 .cmd = NL80211_CMD_DEL_MPATH,
6380 .doit = nl80211_del_mpath,
6381 .policy = nl80211_policy,
6382 .flags = GENL_ADMIN_PERM,
6383 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6384 NL80211_FLAG_NEED_RTNL,
6387 .cmd = NL80211_CMD_SET_BSS,
6388 .doit = nl80211_set_bss,
6389 .policy = nl80211_policy,
6390 .flags = GENL_ADMIN_PERM,
6391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6392 NL80211_FLAG_NEED_RTNL,
6395 .cmd = NL80211_CMD_GET_REG,
6396 .doit = nl80211_get_reg,
6397 .policy = nl80211_policy,
6398 /* can be retrieved by unprivileged users */
6401 .cmd = NL80211_CMD_SET_REG,
6402 .doit = nl80211_set_reg,
6403 .policy = nl80211_policy,
6404 .flags = GENL_ADMIN_PERM,
6407 .cmd = NL80211_CMD_REQ_SET_REG,
6408 .doit = nl80211_req_set_reg,
6409 .policy = nl80211_policy,
6410 .flags = GENL_ADMIN_PERM,
6413 .cmd = NL80211_CMD_GET_MESH_CONFIG,
6414 .doit = nl80211_get_mesh_config,
6415 .policy = nl80211_policy,
6416 /* can be retrieved by unprivileged users */
6417 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6418 NL80211_FLAG_NEED_RTNL,
6421 .cmd = NL80211_CMD_SET_MESH_CONFIG,
6422 .doit = nl80211_update_mesh_config,
6423 .policy = nl80211_policy,
6424 .flags = GENL_ADMIN_PERM,
6425 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6426 NL80211_FLAG_NEED_RTNL,
6429 .cmd = NL80211_CMD_TRIGGER_SCAN,
6430 .doit = nl80211_trigger_scan,
6431 .policy = nl80211_policy,
6432 .flags = GENL_ADMIN_PERM,
6433 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6434 NL80211_FLAG_NEED_RTNL,
6437 .cmd = NL80211_CMD_GET_SCAN,
6438 .policy = nl80211_policy,
6439 .dumpit = nl80211_dump_scan,
6442 .cmd = NL80211_CMD_START_SCHED_SCAN,
6443 .doit = nl80211_start_sched_scan,
6444 .policy = nl80211_policy,
6445 .flags = GENL_ADMIN_PERM,
6446 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6447 NL80211_FLAG_NEED_RTNL,
6450 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
6451 .doit = nl80211_stop_sched_scan,
6452 .policy = nl80211_policy,
6453 .flags = GENL_ADMIN_PERM,
6454 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6455 NL80211_FLAG_NEED_RTNL,
6458 .cmd = NL80211_CMD_AUTHENTICATE,
6459 .doit = nl80211_authenticate,
6460 .policy = nl80211_policy,
6461 .flags = GENL_ADMIN_PERM,
6462 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6463 NL80211_FLAG_NEED_RTNL,
6466 .cmd = NL80211_CMD_ASSOCIATE,
6467 .doit = nl80211_associate,
6468 .policy = nl80211_policy,
6469 .flags = GENL_ADMIN_PERM,
6470 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6471 NL80211_FLAG_NEED_RTNL,
6474 .cmd = NL80211_CMD_DEAUTHENTICATE,
6475 .doit = nl80211_deauthenticate,
6476 .policy = nl80211_policy,
6477 .flags = GENL_ADMIN_PERM,
6478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6479 NL80211_FLAG_NEED_RTNL,
6482 .cmd = NL80211_CMD_DISASSOCIATE,
6483 .doit = nl80211_disassociate,
6484 .policy = nl80211_policy,
6485 .flags = GENL_ADMIN_PERM,
6486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6487 NL80211_FLAG_NEED_RTNL,
6490 .cmd = NL80211_CMD_JOIN_IBSS,
6491 .doit = nl80211_join_ibss,
6492 .policy = nl80211_policy,
6493 .flags = GENL_ADMIN_PERM,
6494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6495 NL80211_FLAG_NEED_RTNL,
6498 .cmd = NL80211_CMD_LEAVE_IBSS,
6499 .doit = nl80211_leave_ibss,
6500 .policy = nl80211_policy,
6501 .flags = GENL_ADMIN_PERM,
6502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6503 NL80211_FLAG_NEED_RTNL,
6505 #ifdef CONFIG_NL80211_TESTMODE
6507 .cmd = NL80211_CMD_TESTMODE,
6508 .doit = nl80211_testmode_do,
6509 .dumpit = nl80211_testmode_dump,
6510 .policy = nl80211_policy,
6511 .flags = GENL_ADMIN_PERM,
6512 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6513 NL80211_FLAG_NEED_RTNL,
6515 #endif
6517 .cmd = NL80211_CMD_CONNECT,
6518 .doit = nl80211_connect,
6519 .policy = nl80211_policy,
6520 .flags = GENL_ADMIN_PERM,
6521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6522 NL80211_FLAG_NEED_RTNL,
6525 .cmd = NL80211_CMD_DISCONNECT,
6526 .doit = nl80211_disconnect,
6527 .policy = nl80211_policy,
6528 .flags = GENL_ADMIN_PERM,
6529 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6530 NL80211_FLAG_NEED_RTNL,
6533 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6534 .doit = nl80211_wiphy_netns,
6535 .policy = nl80211_policy,
6536 .flags = GENL_ADMIN_PERM,
6537 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6538 NL80211_FLAG_NEED_RTNL,
6541 .cmd = NL80211_CMD_GET_SURVEY,
6542 .policy = nl80211_policy,
6543 .dumpit = nl80211_dump_survey,
6546 .cmd = NL80211_CMD_SET_PMKSA,
6547 .doit = nl80211_setdel_pmksa,
6548 .policy = nl80211_policy,
6549 .flags = GENL_ADMIN_PERM,
6550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6551 NL80211_FLAG_NEED_RTNL,
6554 .cmd = NL80211_CMD_DEL_PMKSA,
6555 .doit = nl80211_setdel_pmksa,
6556 .policy = nl80211_policy,
6557 .flags = GENL_ADMIN_PERM,
6558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6559 NL80211_FLAG_NEED_RTNL,
6562 .cmd = NL80211_CMD_FLUSH_PMKSA,
6563 .doit = nl80211_flush_pmksa,
6564 .policy = nl80211_policy,
6565 .flags = GENL_ADMIN_PERM,
6566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6567 NL80211_FLAG_NEED_RTNL,
6570 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6571 .doit = nl80211_remain_on_channel,
6572 .policy = nl80211_policy,
6573 .flags = GENL_ADMIN_PERM,
6574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6575 NL80211_FLAG_NEED_RTNL,
6578 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6579 .doit = nl80211_cancel_remain_on_channel,
6580 .policy = nl80211_policy,
6581 .flags = GENL_ADMIN_PERM,
6582 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6583 NL80211_FLAG_NEED_RTNL,
6586 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6587 .doit = nl80211_set_tx_bitrate_mask,
6588 .policy = nl80211_policy,
6589 .flags = GENL_ADMIN_PERM,
6590 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6591 NL80211_FLAG_NEED_RTNL,
6594 .cmd = NL80211_CMD_REGISTER_FRAME,
6595 .doit = nl80211_register_mgmt,
6596 .policy = nl80211_policy,
6597 .flags = GENL_ADMIN_PERM,
6598 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6599 NL80211_FLAG_NEED_RTNL,
6602 .cmd = NL80211_CMD_FRAME,
6603 .doit = nl80211_tx_mgmt,
6604 .policy = nl80211_policy,
6605 .flags = GENL_ADMIN_PERM,
6606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6607 NL80211_FLAG_NEED_RTNL,
6610 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6611 .doit = nl80211_tx_mgmt_cancel_wait,
6612 .policy = nl80211_policy,
6613 .flags = GENL_ADMIN_PERM,
6614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6615 NL80211_FLAG_NEED_RTNL,
6618 .cmd = NL80211_CMD_SET_POWER_SAVE,
6619 .doit = nl80211_set_power_save,
6620 .policy = nl80211_policy,
6621 .flags = GENL_ADMIN_PERM,
6622 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6623 NL80211_FLAG_NEED_RTNL,
6626 .cmd = NL80211_CMD_GET_POWER_SAVE,
6627 .doit = nl80211_get_power_save,
6628 .policy = nl80211_policy,
6629 /* can be retrieved by unprivileged users */
6630 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6631 NL80211_FLAG_NEED_RTNL,
6634 .cmd = NL80211_CMD_SET_CQM,
6635 .doit = nl80211_set_cqm,
6636 .policy = nl80211_policy,
6637 .flags = GENL_ADMIN_PERM,
6638 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6639 NL80211_FLAG_NEED_RTNL,
6642 .cmd = NL80211_CMD_SET_CHANNEL,
6643 .doit = nl80211_set_channel,
6644 .policy = nl80211_policy,
6645 .flags = GENL_ADMIN_PERM,
6646 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6647 NL80211_FLAG_NEED_RTNL,
6650 .cmd = NL80211_CMD_SET_WDS_PEER,
6651 .doit = nl80211_set_wds_peer,
6652 .policy = nl80211_policy,
6653 .flags = GENL_ADMIN_PERM,
6654 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6655 NL80211_FLAG_NEED_RTNL,
6658 .cmd = NL80211_CMD_JOIN_MESH,
6659 .doit = nl80211_join_mesh,
6660 .policy = nl80211_policy,
6661 .flags = GENL_ADMIN_PERM,
6662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6663 NL80211_FLAG_NEED_RTNL,
6666 .cmd = NL80211_CMD_LEAVE_MESH,
6667 .doit = nl80211_leave_mesh,
6668 .policy = nl80211_policy,
6669 .flags = GENL_ADMIN_PERM,
6670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6671 NL80211_FLAG_NEED_RTNL,
6674 .cmd = NL80211_CMD_GET_WOWLAN,
6675 .doit = nl80211_get_wowlan,
6676 .policy = nl80211_policy,
6677 /* can be retrieved by unprivileged users */
6678 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6679 NL80211_FLAG_NEED_RTNL,
6682 .cmd = NL80211_CMD_SET_WOWLAN,
6683 .doit = nl80211_set_wowlan,
6684 .policy = nl80211_policy,
6685 .flags = GENL_ADMIN_PERM,
6686 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6687 NL80211_FLAG_NEED_RTNL,
6690 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6691 .doit = nl80211_set_rekey_data,
6692 .policy = nl80211_policy,
6693 .flags = GENL_ADMIN_PERM,
6694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6695 NL80211_FLAG_NEED_RTNL,
6698 .cmd = NL80211_CMD_TDLS_MGMT,
6699 .doit = nl80211_tdls_mgmt,
6700 .policy = nl80211_policy,
6701 .flags = GENL_ADMIN_PERM,
6702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6703 NL80211_FLAG_NEED_RTNL,
6706 .cmd = NL80211_CMD_TDLS_OPER,
6707 .doit = nl80211_tdls_oper,
6708 .policy = nl80211_policy,
6709 .flags = GENL_ADMIN_PERM,
6710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6711 NL80211_FLAG_NEED_RTNL,
6714 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
6715 .doit = nl80211_register_unexpected_frame,
6716 .policy = nl80211_policy,
6717 .flags = GENL_ADMIN_PERM,
6718 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6719 NL80211_FLAG_NEED_RTNL,
6722 .cmd = NL80211_CMD_PROBE_CLIENT,
6723 .doit = nl80211_probe_client,
6724 .policy = nl80211_policy,
6725 .flags = GENL_ADMIN_PERM,
6726 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6727 NL80211_FLAG_NEED_RTNL,
6730 .cmd = NL80211_CMD_REGISTER_BEACONS,
6731 .doit = nl80211_register_beacons,
6732 .policy = nl80211_policy,
6733 .flags = GENL_ADMIN_PERM,
6734 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6735 NL80211_FLAG_NEED_RTNL,
6738 .cmd = NL80211_CMD_SET_NOACK_MAP,
6739 .doit = nl80211_set_noack_map,
6740 .policy = nl80211_policy,
6741 .flags = GENL_ADMIN_PERM,
6742 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6743 NL80211_FLAG_NEED_RTNL,
6748 static struct genl_multicast_group nl80211_mlme_mcgrp = {
6749 .name = "mlme",
6752 /* multicast groups */
6753 static struct genl_multicast_group nl80211_config_mcgrp = {
6754 .name = "config",
6756 static struct genl_multicast_group nl80211_scan_mcgrp = {
6757 .name = "scan",
6759 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6760 .name = "regulatory",
6763 /* notification functions */
6765 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6767 struct sk_buff *msg;
6769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6770 if (!msg)
6771 return;
6773 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6774 nlmsg_free(msg);
6775 return;
6778 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6779 nl80211_config_mcgrp.id, GFP_KERNEL);
6782 static int nl80211_add_scan_req(struct sk_buff *msg,
6783 struct cfg80211_registered_device *rdev)
6785 struct cfg80211_scan_request *req = rdev->scan_req;
6786 struct nlattr *nest;
6787 int i;
6789 ASSERT_RDEV_LOCK(rdev);
6791 if (WARN_ON(!req))
6792 return 0;
6794 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6795 if (!nest)
6796 goto nla_put_failure;
6797 for (i = 0; i < req->n_ssids; i++)
6798 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6799 nla_nest_end(msg, nest);
6801 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6802 if (!nest)
6803 goto nla_put_failure;
6804 for (i = 0; i < req->n_channels; i++)
6805 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6806 nla_nest_end(msg, nest);
6808 if (req->ie)
6809 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6811 return 0;
6812 nla_put_failure:
6813 return -ENOBUFS;
6816 static int nl80211_send_scan_msg(struct sk_buff *msg,
6817 struct cfg80211_registered_device *rdev,
6818 struct net_device *netdev,
6819 u32 pid, u32 seq, int flags,
6820 u32 cmd)
6822 void *hdr;
6824 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6825 if (!hdr)
6826 return -1;
6828 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6829 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6831 /* ignore errors and send incomplete event anyway */
6832 nl80211_add_scan_req(msg, rdev);
6834 return genlmsg_end(msg, hdr);
6836 nla_put_failure:
6837 genlmsg_cancel(msg, hdr);
6838 return -EMSGSIZE;
6841 static int
6842 nl80211_send_sched_scan_msg(struct sk_buff *msg,
6843 struct cfg80211_registered_device *rdev,
6844 struct net_device *netdev,
6845 u32 pid, u32 seq, int flags, u32 cmd)
6847 void *hdr;
6849 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6850 if (!hdr)
6851 return -1;
6853 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6854 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6856 return genlmsg_end(msg, hdr);
6858 nla_put_failure:
6859 genlmsg_cancel(msg, hdr);
6860 return -EMSGSIZE;
6863 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6864 struct net_device *netdev)
6866 struct sk_buff *msg;
6868 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6869 if (!msg)
6870 return;
6872 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6873 NL80211_CMD_TRIGGER_SCAN) < 0) {
6874 nlmsg_free(msg);
6875 return;
6878 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6879 nl80211_scan_mcgrp.id, GFP_KERNEL);
6882 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6883 struct net_device *netdev)
6885 struct sk_buff *msg;
6887 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6888 if (!msg)
6889 return;
6891 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6892 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
6893 nlmsg_free(msg);
6894 return;
6897 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6898 nl80211_scan_mcgrp.id, GFP_KERNEL);
6901 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6902 struct net_device *netdev)
6904 struct sk_buff *msg;
6906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6907 if (!msg)
6908 return;
6910 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6911 NL80211_CMD_SCAN_ABORTED) < 0) {
6912 nlmsg_free(msg);
6913 return;
6916 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6917 nl80211_scan_mcgrp.id, GFP_KERNEL);
6920 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6921 struct net_device *netdev)
6923 struct sk_buff *msg;
6925 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6926 if (!msg)
6927 return;
6929 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6930 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6931 nlmsg_free(msg);
6932 return;
6935 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6936 nl80211_scan_mcgrp.id, GFP_KERNEL);
6939 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6940 struct net_device *netdev, u32 cmd)
6942 struct sk_buff *msg;
6944 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6945 if (!msg)
6946 return;
6948 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6949 nlmsg_free(msg);
6950 return;
6953 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6954 nl80211_scan_mcgrp.id, GFP_KERNEL);
6958 * This can happen on global regulatory changes or device specific settings
6959 * based on custom world regulatory domains.
6961 void nl80211_send_reg_change_event(struct regulatory_request *request)
6963 struct sk_buff *msg;
6964 void *hdr;
6966 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6967 if (!msg)
6968 return;
6970 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6971 if (!hdr) {
6972 nlmsg_free(msg);
6973 return;
6976 /* Userspace can always count this one always being set */
6977 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6979 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6980 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6981 NL80211_REGDOM_TYPE_WORLD);
6982 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6983 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6984 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6985 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6986 request->intersect)
6987 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6988 NL80211_REGDOM_TYPE_INTERSECTION);
6989 else {
6990 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6991 NL80211_REGDOM_TYPE_COUNTRY);
6992 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6995 if (wiphy_idx_valid(request->wiphy_idx))
6996 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6998 genlmsg_end(msg, hdr);
7000 rcu_read_lock();
7001 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7002 GFP_ATOMIC);
7003 rcu_read_unlock();
7005 return;
7007 nla_put_failure:
7008 genlmsg_cancel(msg, hdr);
7009 nlmsg_free(msg);
7012 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7013 struct net_device *netdev,
7014 const u8 *buf, size_t len,
7015 enum nl80211_commands cmd, gfp_t gfp)
7017 struct sk_buff *msg;
7018 void *hdr;
7020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7021 if (!msg)
7022 return;
7024 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7025 if (!hdr) {
7026 nlmsg_free(msg);
7027 return;
7030 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7031 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7032 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7034 genlmsg_end(msg, hdr);
7036 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7037 nl80211_mlme_mcgrp.id, gfp);
7038 return;
7040 nla_put_failure:
7041 genlmsg_cancel(msg, hdr);
7042 nlmsg_free(msg);
7045 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
7046 struct net_device *netdev, const u8 *buf,
7047 size_t len, gfp_t gfp)
7049 nl80211_send_mlme_event(rdev, netdev, buf, len,
7050 NL80211_CMD_AUTHENTICATE, gfp);
7053 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7054 struct net_device *netdev, const u8 *buf,
7055 size_t len, gfp_t gfp)
7057 nl80211_send_mlme_event(rdev, netdev, buf, len,
7058 NL80211_CMD_ASSOCIATE, gfp);
7061 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
7062 struct net_device *netdev, const u8 *buf,
7063 size_t len, gfp_t gfp)
7065 nl80211_send_mlme_event(rdev, netdev, buf, len,
7066 NL80211_CMD_DEAUTHENTICATE, gfp);
7069 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7070 struct net_device *netdev, const u8 *buf,
7071 size_t len, gfp_t gfp)
7073 nl80211_send_mlme_event(rdev, netdev, buf, len,
7074 NL80211_CMD_DISASSOCIATE, gfp);
7077 void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7078 struct net_device *netdev, const u8 *buf,
7079 size_t len, gfp_t gfp)
7081 nl80211_send_mlme_event(rdev, netdev, buf, len,
7082 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7085 void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7086 struct net_device *netdev, const u8 *buf,
7087 size_t len, gfp_t gfp)
7089 nl80211_send_mlme_event(rdev, netdev, buf, len,
7090 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7093 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7094 struct net_device *netdev, int cmd,
7095 const u8 *addr, gfp_t gfp)
7097 struct sk_buff *msg;
7098 void *hdr;
7100 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7101 if (!msg)
7102 return;
7104 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7105 if (!hdr) {
7106 nlmsg_free(msg);
7107 return;
7110 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7111 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7112 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
7113 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7115 genlmsg_end(msg, hdr);
7117 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7118 nl80211_mlme_mcgrp.id, gfp);
7119 return;
7121 nla_put_failure:
7122 genlmsg_cancel(msg, hdr);
7123 nlmsg_free(msg);
7126 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
7127 struct net_device *netdev, const u8 *addr,
7128 gfp_t gfp)
7130 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
7131 addr, gfp);
7134 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
7135 struct net_device *netdev, const u8 *addr,
7136 gfp_t gfp)
7138 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7139 addr, gfp);
7142 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7143 struct net_device *netdev, const u8 *bssid,
7144 const u8 *req_ie, size_t req_ie_len,
7145 const u8 *resp_ie, size_t resp_ie_len,
7146 u16 status, gfp_t gfp)
7148 struct sk_buff *msg;
7149 void *hdr;
7151 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7152 if (!msg)
7153 return;
7155 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7156 if (!hdr) {
7157 nlmsg_free(msg);
7158 return;
7161 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7162 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7163 if (bssid)
7164 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7165 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
7166 if (req_ie)
7167 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7168 if (resp_ie)
7169 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7171 genlmsg_end(msg, hdr);
7173 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7174 nl80211_mlme_mcgrp.id, gfp);
7175 return;
7177 nla_put_failure:
7178 genlmsg_cancel(msg, hdr);
7179 nlmsg_free(msg);
7183 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7184 struct net_device *netdev, const u8 *bssid,
7185 const u8 *req_ie, size_t req_ie_len,
7186 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7188 struct sk_buff *msg;
7189 void *hdr;
7191 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7192 if (!msg)
7193 return;
7195 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7196 if (!hdr) {
7197 nlmsg_free(msg);
7198 return;
7201 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7202 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7203 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7204 if (req_ie)
7205 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
7206 if (resp_ie)
7207 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
7209 genlmsg_end(msg, hdr);
7211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7212 nl80211_mlme_mcgrp.id, gfp);
7213 return;
7215 nla_put_failure:
7216 genlmsg_cancel(msg, hdr);
7217 nlmsg_free(msg);
7221 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7222 struct net_device *netdev, u16 reason,
7223 const u8 *ie, size_t ie_len, bool from_ap)
7225 struct sk_buff *msg;
7226 void *hdr;
7228 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7229 if (!msg)
7230 return;
7232 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7233 if (!hdr) {
7234 nlmsg_free(msg);
7235 return;
7238 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7239 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7240 if (from_ap && reason)
7241 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
7242 if (from_ap)
7243 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
7244 if (ie)
7245 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
7247 genlmsg_end(msg, hdr);
7249 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7250 nl80211_mlme_mcgrp.id, GFP_KERNEL);
7251 return;
7253 nla_put_failure:
7254 genlmsg_cancel(msg, hdr);
7255 nlmsg_free(msg);
7259 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7260 struct net_device *netdev, const u8 *bssid,
7261 gfp_t gfp)
7263 struct sk_buff *msg;
7264 void *hdr;
7266 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7267 if (!msg)
7268 return;
7270 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7271 if (!hdr) {
7272 nlmsg_free(msg);
7273 return;
7276 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7277 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7278 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7280 genlmsg_end(msg, hdr);
7282 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7283 nl80211_mlme_mcgrp.id, gfp);
7284 return;
7286 nla_put_failure:
7287 genlmsg_cancel(msg, hdr);
7288 nlmsg_free(msg);
7291 void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7292 struct net_device *netdev,
7293 const u8 *macaddr, const u8* ie, u8 ie_len,
7294 gfp_t gfp)
7296 struct sk_buff *msg;
7297 void *hdr;
7299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7300 if (!msg)
7301 return;
7303 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7304 if (!hdr) {
7305 nlmsg_free(msg);
7306 return;
7309 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7310 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7311 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
7312 if (ie_len && ie)
7313 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
7315 genlmsg_end(msg, hdr);
7317 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7318 nl80211_mlme_mcgrp.id, gfp);
7319 return;
7321 nla_put_failure:
7322 genlmsg_cancel(msg, hdr);
7323 nlmsg_free(msg);
7326 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7327 struct net_device *netdev, const u8 *addr,
7328 enum nl80211_key_type key_type, int key_id,
7329 const u8 *tsc, gfp_t gfp)
7331 struct sk_buff *msg;
7332 void *hdr;
7334 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7335 if (!msg)
7336 return;
7338 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7339 if (!hdr) {
7340 nlmsg_free(msg);
7341 return;
7344 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7345 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7346 if (addr)
7347 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7348 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
7349 if (key_id != -1)
7350 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
7351 if (tsc)
7352 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
7354 genlmsg_end(msg, hdr);
7356 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7357 nl80211_mlme_mcgrp.id, gfp);
7358 return;
7360 nla_put_failure:
7361 genlmsg_cancel(msg, hdr);
7362 nlmsg_free(msg);
7365 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7366 struct ieee80211_channel *channel_before,
7367 struct ieee80211_channel *channel_after)
7369 struct sk_buff *msg;
7370 void *hdr;
7371 struct nlattr *nl_freq;
7373 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
7374 if (!msg)
7375 return;
7377 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7378 if (!hdr) {
7379 nlmsg_free(msg);
7380 return;
7384 * Since we are applying the beacon hint to a wiphy we know its
7385 * wiphy_idx is valid
7387 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
7389 /* Before */
7390 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
7391 if (!nl_freq)
7392 goto nla_put_failure;
7393 if (nl80211_msg_put_channel(msg, channel_before))
7394 goto nla_put_failure;
7395 nla_nest_end(msg, nl_freq);
7397 /* After */
7398 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
7399 if (!nl_freq)
7400 goto nla_put_failure;
7401 if (nl80211_msg_put_channel(msg, channel_after))
7402 goto nla_put_failure;
7403 nla_nest_end(msg, nl_freq);
7405 genlmsg_end(msg, hdr);
7407 rcu_read_lock();
7408 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
7409 GFP_ATOMIC);
7410 rcu_read_unlock();
7412 return;
7414 nla_put_failure:
7415 genlmsg_cancel(msg, hdr);
7416 nlmsg_free(msg);
7419 static void nl80211_send_remain_on_chan_event(
7420 int cmd, struct cfg80211_registered_device *rdev,
7421 struct net_device *netdev, u64 cookie,
7422 struct ieee80211_channel *chan,
7423 enum nl80211_channel_type channel_type,
7424 unsigned int duration, gfp_t gfp)
7426 struct sk_buff *msg;
7427 void *hdr;
7429 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7430 if (!msg)
7431 return;
7433 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7434 if (!hdr) {
7435 nlmsg_free(msg);
7436 return;
7439 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7440 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7441 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
7442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
7443 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7445 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
7446 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
7448 genlmsg_end(msg, hdr);
7450 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7451 nl80211_mlme_mcgrp.id, gfp);
7452 return;
7454 nla_put_failure:
7455 genlmsg_cancel(msg, hdr);
7456 nlmsg_free(msg);
7459 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
7460 struct net_device *netdev, u64 cookie,
7461 struct ieee80211_channel *chan,
7462 enum nl80211_channel_type channel_type,
7463 unsigned int duration, gfp_t gfp)
7465 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
7466 rdev, netdev, cookie, chan,
7467 channel_type, duration, gfp);
7470 void nl80211_send_remain_on_channel_cancel(
7471 struct cfg80211_registered_device *rdev, struct net_device *netdev,
7472 u64 cookie, struct ieee80211_channel *chan,
7473 enum nl80211_channel_type channel_type, gfp_t gfp)
7475 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7476 rdev, netdev, cookie, chan,
7477 channel_type, 0, gfp);
7480 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
7481 struct net_device *dev, const u8 *mac_addr,
7482 struct station_info *sinfo, gfp_t gfp)
7484 struct sk_buff *msg;
7486 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7487 if (!msg)
7488 return;
7490 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
7491 nlmsg_free(msg);
7492 return;
7495 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7496 nl80211_mlme_mcgrp.id, gfp);
7499 void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
7500 struct net_device *dev, const u8 *mac_addr,
7501 gfp_t gfp)
7503 struct sk_buff *msg;
7504 void *hdr;
7506 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7507 if (!msg)
7508 return;
7510 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
7511 if (!hdr) {
7512 nlmsg_free(msg);
7513 return;
7516 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7517 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
7519 genlmsg_end(msg, hdr);
7521 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7522 nl80211_mlme_mcgrp.id, gfp);
7523 return;
7525 nla_put_failure:
7526 genlmsg_cancel(msg, hdr);
7527 nlmsg_free(msg);
7530 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
7531 const u8 *addr, gfp_t gfp)
7533 struct wireless_dev *wdev = dev->ieee80211_ptr;
7534 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7535 struct sk_buff *msg;
7536 void *hdr;
7537 int err;
7538 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
7540 if (!nlpid)
7541 return false;
7543 msg = nlmsg_new(100, gfp);
7544 if (!msg)
7545 return true;
7547 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7548 if (!hdr) {
7549 nlmsg_free(msg);
7550 return true;
7553 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7554 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7555 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7557 err = genlmsg_end(msg, hdr);
7558 if (err < 0) {
7559 nlmsg_free(msg);
7560 return true;
7563 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7564 return true;
7566 nla_put_failure:
7567 genlmsg_cancel(msg, hdr);
7568 nlmsg_free(msg);
7569 return true;
7572 bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
7574 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
7575 addr, gfp);
7578 bool nl80211_unexpected_4addr_frame(struct net_device *dev,
7579 const u8 *addr, gfp_t gfp)
7581 return __nl80211_unexpected_frame(dev,
7582 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
7583 addr, gfp);
7586 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
7587 struct net_device *netdev, u32 nlpid,
7588 int freq, const u8 *buf, size_t len, gfp_t gfp)
7590 struct sk_buff *msg;
7591 void *hdr;
7593 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7594 if (!msg)
7595 return -ENOMEM;
7597 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7598 if (!hdr) {
7599 nlmsg_free(msg);
7600 return -ENOMEM;
7603 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7604 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7605 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7606 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7608 genlmsg_end(msg, hdr);
7610 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7612 nla_put_failure:
7613 genlmsg_cancel(msg, hdr);
7614 nlmsg_free(msg);
7615 return -ENOBUFS;
7618 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7619 struct net_device *netdev, u64 cookie,
7620 const u8 *buf, size_t len, bool ack,
7621 gfp_t gfp)
7623 struct sk_buff *msg;
7624 void *hdr;
7626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7627 if (!msg)
7628 return;
7630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
7631 if (!hdr) {
7632 nlmsg_free(msg);
7633 return;
7636 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7637 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7638 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7639 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7640 if (ack)
7641 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7643 genlmsg_end(msg, hdr);
7645 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7646 return;
7648 nla_put_failure:
7649 genlmsg_cancel(msg, hdr);
7650 nlmsg_free(msg);
7653 void
7654 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7655 struct net_device *netdev,
7656 enum nl80211_cqm_rssi_threshold_event rssi_event,
7657 gfp_t gfp)
7659 struct sk_buff *msg;
7660 struct nlattr *pinfoattr;
7661 void *hdr;
7663 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7664 if (!msg)
7665 return;
7667 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7668 if (!hdr) {
7669 nlmsg_free(msg);
7670 return;
7673 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7674 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7676 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7677 if (!pinfoattr)
7678 goto nla_put_failure;
7680 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7681 rssi_event);
7683 nla_nest_end(msg, pinfoattr);
7685 genlmsg_end(msg, hdr);
7687 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7688 nl80211_mlme_mcgrp.id, gfp);
7689 return;
7691 nla_put_failure:
7692 genlmsg_cancel(msg, hdr);
7693 nlmsg_free(msg);
7696 void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7697 struct net_device *netdev, const u8 *bssid,
7698 const u8 *replay_ctr, gfp_t gfp)
7700 struct sk_buff *msg;
7701 struct nlattr *rekey_attr;
7702 void *hdr;
7704 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7705 if (!msg)
7706 return;
7708 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7709 if (!hdr) {
7710 nlmsg_free(msg);
7711 return;
7714 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7715 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7716 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7718 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7719 if (!rekey_attr)
7720 goto nla_put_failure;
7722 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7723 NL80211_REPLAY_CTR_LEN, replay_ctr);
7725 nla_nest_end(msg, rekey_attr);
7727 genlmsg_end(msg, hdr);
7729 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7730 nl80211_mlme_mcgrp.id, gfp);
7731 return;
7733 nla_put_failure:
7734 genlmsg_cancel(msg, hdr);
7735 nlmsg_free(msg);
7738 void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
7739 struct net_device *netdev, int index,
7740 const u8 *bssid, bool preauth, gfp_t gfp)
7742 struct sk_buff *msg;
7743 struct nlattr *attr;
7744 void *hdr;
7746 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7747 if (!msg)
7748 return;
7750 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
7751 if (!hdr) {
7752 nlmsg_free(msg);
7753 return;
7756 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7757 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7759 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
7760 if (!attr)
7761 goto nla_put_failure;
7763 NLA_PUT_U32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index);
7764 NLA_PUT(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid);
7765 if (preauth)
7766 NLA_PUT_FLAG(msg, NL80211_PMKSA_CANDIDATE_PREAUTH);
7768 nla_nest_end(msg, attr);
7770 genlmsg_end(msg, hdr);
7772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7773 nl80211_mlme_mcgrp.id, gfp);
7774 return;
7776 nla_put_failure:
7777 genlmsg_cancel(msg, hdr);
7778 nlmsg_free(msg);
7781 void
7782 nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7783 struct net_device *netdev, const u8 *peer,
7784 u32 num_packets, gfp_t gfp)
7786 struct sk_buff *msg;
7787 struct nlattr *pinfoattr;
7788 void *hdr;
7790 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7791 if (!msg)
7792 return;
7794 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7795 if (!hdr) {
7796 nlmsg_free(msg);
7797 return;
7800 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7801 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7802 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7804 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7805 if (!pinfoattr)
7806 goto nla_put_failure;
7808 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7810 nla_nest_end(msg, pinfoattr);
7812 genlmsg_end(msg, hdr);
7814 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7815 nl80211_mlme_mcgrp.id, gfp);
7816 return;
7818 nla_put_failure:
7819 genlmsg_cancel(msg, hdr);
7820 nlmsg_free(msg);
7823 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
7824 u64 cookie, bool acked, gfp_t gfp)
7826 struct wireless_dev *wdev = dev->ieee80211_ptr;
7827 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
7828 struct sk_buff *msg;
7829 void *hdr;
7830 int err;
7832 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7833 if (!msg)
7834 return;
7836 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
7837 if (!hdr) {
7838 nlmsg_free(msg);
7839 return;
7842 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7843 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
7844 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7845 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7846 if (acked)
7847 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7849 err = genlmsg_end(msg, hdr);
7850 if (err < 0) {
7851 nlmsg_free(msg);
7852 return;
7855 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7856 nl80211_mlme_mcgrp.id, gfp);
7857 return;
7859 nla_put_failure:
7860 genlmsg_cancel(msg, hdr);
7861 nlmsg_free(msg);
7863 EXPORT_SYMBOL(cfg80211_probe_status);
7865 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
7866 const u8 *frame, size_t len,
7867 int freq, gfp_t gfp)
7869 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
7870 struct sk_buff *msg;
7871 void *hdr;
7872 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
7874 if (!nlpid)
7875 return;
7877 msg = nlmsg_new(len + 100, gfp);
7878 if (!msg)
7879 return;
7881 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
7882 if (!hdr) {
7883 nlmsg_free(msg);
7884 return;
7887 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7888 if (freq)
7889 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7890 NLA_PUT(msg, NL80211_ATTR_FRAME, len, frame);
7892 genlmsg_end(msg, hdr);
7894 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7895 return;
7897 nla_put_failure:
7898 genlmsg_cancel(msg, hdr);
7899 nlmsg_free(msg);
7901 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
7903 static int nl80211_netlink_notify(struct notifier_block * nb,
7904 unsigned long state,
7905 void *_notify)
7907 struct netlink_notify *notify = _notify;
7908 struct cfg80211_registered_device *rdev;
7909 struct wireless_dev *wdev;
7911 if (state != NETLINK_URELEASE)
7912 return NOTIFY_DONE;
7914 rcu_read_lock();
7916 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
7917 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
7918 cfg80211_mlme_unregister_socket(wdev, notify->pid);
7919 if (rdev->ap_beacons_nlpid == notify->pid)
7920 rdev->ap_beacons_nlpid = 0;
7923 rcu_read_unlock();
7925 return NOTIFY_DONE;
7928 static struct notifier_block nl80211_netlink_notifier = {
7929 .notifier_call = nl80211_netlink_notify,
7932 /* initialisation/exit functions */
7934 int nl80211_init(void)
7936 int err;
7938 err = genl_register_family_with_ops(&nl80211_fam,
7939 nl80211_ops, ARRAY_SIZE(nl80211_ops));
7940 if (err)
7941 return err;
7943 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7944 if (err)
7945 goto err_out;
7947 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7948 if (err)
7949 goto err_out;
7951 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7952 if (err)
7953 goto err_out;
7955 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7956 if (err)
7957 goto err_out;
7959 #ifdef CONFIG_NL80211_TESTMODE
7960 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7961 if (err)
7962 goto err_out;
7963 #endif
7965 err = netlink_register_notifier(&nl80211_netlink_notifier);
7966 if (err)
7967 goto err_out;
7969 return 0;
7970 err_out:
7971 genl_unregister_family(&nl80211_fam);
7972 return err;
7975 void nl80211_exit(void)
7977 netlink_unregister_notifier(&nl80211_netlink_notifier);
7978 genl_unregister_family(&nl80211_fam);