blob: c27bef8e0c11a3bebd3b0e34f328551a9d5ec3ed [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020017#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include <net/genetlink.h>
19#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020020#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040021#include "core.h"
22#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040024
25/* the netlink family */
26static struct genl_family nl80211_fam = {
27 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
28 .name = "nl80211", /* have users key off the name instead */
29 .hdrsize = 0, /* no private header */
30 .version = 1, /* no particular meaning now */
31 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020032 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040033};
34
Johannes Berg79c97e92009-07-07 03:56:12 +020035/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020036static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020037 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040038 struct net_device **dev)
39{
Johannes Berg463d0182009-07-14 00:33:35 +020040 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040041 int ifindex;
42
Johannes Bergbba95fe2008-07-29 13:22:51 +020043 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040044 return -EINVAL;
45
Johannes Bergbba95fe2008-07-29 13:22:51 +020046 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020047 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040048 if (!*dev)
49 return -ENODEV;
50
Johannes Berg463d0182009-07-14 00:33:35 +020051 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020052 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040053 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020054 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040055 }
56
57 return 0;
58}
59
60/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000061static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040062 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
63 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070064 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020065 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020066 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053067 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020068 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
69 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010072 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040073
74 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010077
78 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020079 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010080
Johannes Bergb9454e82009-07-08 13:29:08 +020081 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010082 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
83 .len = WLAN_MAX_KEY_LEN },
84 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
85 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
86 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030087 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010088
89 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
90 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
91 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_DATA_LEN },
93 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
94 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010095 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
96 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
97 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
98 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
99 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100100 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100101 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200102 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100103 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
104 .len = IEEE80211_MAX_MESH_ID_LEN },
105 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300106
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700107 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
108 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
109
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300110 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200113 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300115
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700116 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
117
Jouni Malinen36aedc92008-08-25 11:58:58 +0300118 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
119 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200120
121 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
122 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
123 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100124 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
125 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200126
127 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
128 .len = IEEE80211_MAX_SSID_LEN },
129 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
130 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200131 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300132 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300133 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300134 [NL80211_ATTR_STA_FLAGS2] = {
135 .len = sizeof(struct nl80211_sta_flag_update),
136 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300137 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200138 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
139 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
140 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200141 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100142 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100143 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
144 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100145 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
146 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200147 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200148 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
149 .len = IEEE80211_MAX_DATA_LEN },
150 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200151 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200152 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300153 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200154 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400155};
156
Johannes Bergb9454e82009-07-08 13:29:08 +0200157/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000158static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200159 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200160 [NL80211_KEY_IDX] = { .type = NLA_U8 },
161 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
162 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
163 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
164 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
165};
166
Holger Schuriga0438972009-11-11 11:30:02 +0100167/* ifidx get helper */
168static int nl80211_get_ifidx(struct netlink_callback *cb)
169{
170 int res;
171
172 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
173 nl80211_fam.attrbuf, nl80211_fam.maxattr,
174 nl80211_policy);
175 if (res)
176 return res;
177
178 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
179 return -EINVAL;
180
181 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
182 if (!res)
183 return -EINVAL;
184 return res;
185}
186
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100187/* IE validation */
188static bool is_valid_ie_attr(const struct nlattr *attr)
189{
190 const u8 *pos;
191 int len;
192
193 if (!attr)
194 return true;
195
196 pos = nla_data(attr);
197 len = nla_len(attr);
198
199 while (len) {
200 u8 elemlen;
201
202 if (len < 2)
203 return false;
204 len -= 2;
205
206 elemlen = pos[1];
207 if (elemlen > len)
208 return false;
209
210 len -= elemlen;
211 pos += 2 + elemlen;
212 }
213
214 return true;
215}
216
Johannes Berg55682962007-09-20 13:09:35 -0400217/* message building helper */
218static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
219 int flags, u8 cmd)
220{
221 /* since there is no private header just add the generic one */
222 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
223}
224
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400225static int nl80211_msg_put_channel(struct sk_buff *msg,
226 struct ieee80211_channel *chan)
227{
228 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
229 chan->center_freq);
230
231 if (chan->flags & IEEE80211_CHAN_DISABLED)
232 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
233 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
234 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
235 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
236 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
237 if (chan->flags & IEEE80211_CHAN_RADAR)
238 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
239
240 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
241 DBM_TO_MBM(chan->max_power));
242
243 return 0;
244
245 nla_put_failure:
246 return -ENOBUFS;
247}
248
Johannes Berg55682962007-09-20 13:09:35 -0400249/* netlink command implementations */
250
Johannes Bergb9454e82009-07-08 13:29:08 +0200251struct key_parse {
252 struct key_params p;
253 int idx;
254 bool def, defmgmt;
255};
256
257static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
258{
259 struct nlattr *tb[NL80211_KEY_MAX + 1];
260 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
261 nl80211_key_policy);
262 if (err)
263 return err;
264
265 k->def = !!tb[NL80211_KEY_DEFAULT];
266 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
267
268 if (tb[NL80211_KEY_IDX])
269 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
270
271 if (tb[NL80211_KEY_DATA]) {
272 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
273 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
274 }
275
276 if (tb[NL80211_KEY_SEQ]) {
277 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
278 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
279 }
280
281 if (tb[NL80211_KEY_CIPHER])
282 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
283
284 return 0;
285}
286
287static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
288{
289 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
290 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
291 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
292 }
293
294 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
295 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
296 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
297 }
298
299 if (info->attrs[NL80211_ATTR_KEY_IDX])
300 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
301
302 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
303 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
304
305 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
306 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
307
308 return 0;
309}
310
311static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
312{
313 int err;
314
315 memset(k, 0, sizeof(*k));
316 k->idx = -1;
317
318 if (info->attrs[NL80211_ATTR_KEY])
319 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
320 else
321 err = nl80211_parse_key_old(info, k);
322
323 if (err)
324 return err;
325
326 if (k->def && k->defmgmt)
327 return -EINVAL;
328
329 if (k->idx != -1) {
330 if (k->defmgmt) {
331 if (k->idx < 4 || k->idx > 5)
332 return -EINVAL;
333 } else if (k->def) {
334 if (k->idx < 0 || k->idx > 3)
335 return -EINVAL;
336 } else {
337 if (k->idx < 0 || k->idx > 5)
338 return -EINVAL;
339 }
340 }
341
342 return 0;
343}
344
Johannes Bergfffd0932009-07-08 14:22:54 +0200345static struct cfg80211_cached_keys *
346nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
347 struct nlattr *keys)
348{
349 struct key_parse parse;
350 struct nlattr *key;
351 struct cfg80211_cached_keys *result;
352 int rem, err, def = 0;
353
354 result = kzalloc(sizeof(*result), GFP_KERNEL);
355 if (!result)
356 return ERR_PTR(-ENOMEM);
357
358 result->def = -1;
359 result->defmgmt = -1;
360
361 nla_for_each_nested(key, keys, rem) {
362 memset(&parse, 0, sizeof(parse));
363 parse.idx = -1;
364
365 err = nl80211_parse_key_new(key, &parse);
366 if (err)
367 goto error;
368 err = -EINVAL;
369 if (!parse.p.key)
370 goto error;
371 if (parse.idx < 0 || parse.idx > 4)
372 goto error;
373 if (parse.def) {
374 if (def)
375 goto error;
376 def = 1;
377 result->def = parse.idx;
378 } else if (parse.defmgmt)
379 goto error;
380 err = cfg80211_validate_key_settings(rdev, &parse.p,
381 parse.idx, NULL);
382 if (err)
383 goto error;
384 result->params[parse.idx].cipher = parse.p.cipher;
385 result->params[parse.idx].key_len = parse.p.key_len;
386 result->params[parse.idx].key = result->data[parse.idx];
387 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
388 }
389
390 return result;
391 error:
392 kfree(result);
393 return ERR_PTR(err);
394}
395
396static int nl80211_key_allowed(struct wireless_dev *wdev)
397{
398 ASSERT_WDEV_LOCK(wdev);
399
400 if (!netif_running(wdev->netdev))
401 return -ENETDOWN;
402
403 switch (wdev->iftype) {
404 case NL80211_IFTYPE_AP:
405 case NL80211_IFTYPE_AP_VLAN:
406 break;
407 case NL80211_IFTYPE_ADHOC:
408 if (!wdev->current_bss)
409 return -ENOLINK;
410 break;
411 case NL80211_IFTYPE_STATION:
412 if (wdev->sme_state != CFG80211_SME_CONNECTED)
413 return -ENOLINK;
414 break;
415 default:
416 return -EINVAL;
417 }
418
419 return 0;
420}
421
Johannes Berg55682962007-09-20 13:09:35 -0400422static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
423 struct cfg80211_registered_device *dev)
424{
425 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100426 struct nlattr *nl_bands, *nl_band;
427 struct nlattr *nl_freqs, *nl_freq;
428 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700429 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100430 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100431 enum ieee80211_band band;
432 struct ieee80211_channel *chan;
433 struct ieee80211_rate *rate;
434 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700435 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400436
437 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
438 if (!hdr)
439 return -1;
440
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500441 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400442 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200443
Johannes Bergf5ea9122009-08-07 16:17:38 +0200444 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
445 cfg80211_rdev_list_generation);
446
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200447 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
448 dev->wiphy.retry_short);
449 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
450 dev->wiphy.retry_long);
451 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
452 dev->wiphy.frag_threshold);
453 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
454 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100455 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
456 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200457
Johannes Berg2a519312009-02-10 21:25:55 +0100458 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
459 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200460 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
461 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100462
Johannes Berg25e47c182009-04-02 20:14:06 +0200463 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
464 sizeof(u32) * dev->wiphy.n_cipher_suites,
465 dev->wiphy.cipher_suites);
466
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100467 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
468 dev->wiphy.max_num_pmkids);
469
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700470 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
471 if (!nl_modes)
472 goto nla_put_failure;
473
474 i = 0;
475 while (ifmodes) {
476 if (ifmodes & 1)
477 NLA_PUT_FLAG(msg, i);
478 ifmodes >>= 1;
479 i++;
480 }
481
482 nla_nest_end(msg, nl_modes);
483
Johannes Bergee688b002008-01-24 19:38:39 +0100484 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
485 if (!nl_bands)
486 goto nla_put_failure;
487
488 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
489 if (!dev->wiphy.bands[band])
490 continue;
491
492 nl_band = nla_nest_start(msg, band);
493 if (!nl_band)
494 goto nla_put_failure;
495
Johannes Bergd51626d2008-10-09 12:20:13 +0200496 /* add HT info */
497 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
498 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
499 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
500 &dev->wiphy.bands[band]->ht_cap.mcs);
501 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
502 dev->wiphy.bands[band]->ht_cap.cap);
503 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
504 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
505 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
506 dev->wiphy.bands[band]->ht_cap.ampdu_density);
507 }
508
Johannes Bergee688b002008-01-24 19:38:39 +0100509 /* add frequencies */
510 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
511 if (!nl_freqs)
512 goto nla_put_failure;
513
514 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
515 nl_freq = nla_nest_start(msg, i);
516 if (!nl_freq)
517 goto nla_put_failure;
518
519 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100520
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400521 if (nl80211_msg_put_channel(msg, chan))
522 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200523
Johannes Bergee688b002008-01-24 19:38:39 +0100524 nla_nest_end(msg, nl_freq);
525 }
526
527 nla_nest_end(msg, nl_freqs);
528
529 /* add bitrates */
530 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
531 if (!nl_rates)
532 goto nla_put_failure;
533
534 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
535 nl_rate = nla_nest_start(msg, i);
536 if (!nl_rate)
537 goto nla_put_failure;
538
539 rate = &dev->wiphy.bands[band]->bitrates[i];
540 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
541 rate->bitrate);
542 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
543 NLA_PUT_FLAG(msg,
544 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
545
546 nla_nest_end(msg, nl_rate);
547 }
548
549 nla_nest_end(msg, nl_rates);
550
551 nla_nest_end(msg, nl_band);
552 }
553 nla_nest_end(msg, nl_bands);
554
Johannes Berg8fdc6212009-03-14 09:34:01 +0100555 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
556 if (!nl_cmds)
557 goto nla_put_failure;
558
559 i = 0;
560#define CMD(op, n) \
561 do { \
562 if (dev->ops->op) { \
563 i++; \
564 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
565 } \
566 } while (0)
567
568 CMD(add_virtual_intf, NEW_INTERFACE);
569 CMD(change_virtual_intf, SET_INTERFACE);
570 CMD(add_key, NEW_KEY);
571 CMD(add_beacon, NEW_BEACON);
572 CMD(add_station, NEW_STATION);
573 CMD(add_mpath, NEW_MPATH);
574 CMD(set_mesh_params, SET_MESH_PARAMS);
575 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200576 CMD(auth, AUTHENTICATE);
577 CMD(assoc, ASSOCIATE);
578 CMD(deauth, DEAUTHENTICATE);
579 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200580 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100581 CMD(set_pmksa, SET_PMKSA);
582 CMD(del_pmksa, DEL_PMKSA);
583 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100584 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200585 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Jouni Malinen026331c2010-02-15 12:53:10 +0200586 CMD(action, ACTION);
Johannes Berg5be83de2009-11-19 00:56:28 +0100587 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200588 i++;
589 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
590 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100591
592#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593
Johannes Berg6829c872009-07-02 09:13:27 +0200594 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595 i++;
596 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
597 }
598
Johannes Berg6829c872009-07-02 09:13:27 +0200599 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200600 i++;
601 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
602 }
603
Johannes Berg8fdc6212009-03-14 09:34:01 +0100604 nla_nest_end(msg, nl_cmds);
605
Johannes Berg55682962007-09-20 13:09:35 -0400606 return genlmsg_end(msg, hdr);
607
608 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700609 genlmsg_cancel(msg, hdr);
610 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400611}
612
613static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
614{
615 int idx = 0;
616 int start = cb->args[0];
617 struct cfg80211_registered_device *dev;
618
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500619 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200620 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200621 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
622 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200623 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400624 continue;
625 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
626 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200627 dev) < 0) {
628 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400629 break;
Julius Volzb4637272008-07-08 14:02:19 +0200630 }
Johannes Berg55682962007-09-20 13:09:35 -0400631 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500632 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400633
634 cb->args[0] = idx;
635
636 return skb->len;
637}
638
639static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
640{
641 struct sk_buff *msg;
642 struct cfg80211_registered_device *dev;
643
644 dev = cfg80211_get_dev_from_info(info);
645 if (IS_ERR(dev))
646 return PTR_ERR(dev);
647
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400649 if (!msg)
650 goto out_err;
651
652 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
653 goto out_free;
654
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200655 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400656
Johannes Berg134e6372009-07-10 09:51:34 +0000657 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400658
659 out_free:
660 nlmsg_free(msg);
661 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200662 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400663 return -ENOBUFS;
664}
665
Jouni Malinen31888482008-10-30 16:59:24 +0200666static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
667 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
668 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
669 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
670 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
671 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
672};
673
674static int parse_txq_params(struct nlattr *tb[],
675 struct ieee80211_txq_params *txq_params)
676{
677 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
678 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
679 !tb[NL80211_TXQ_ATTR_AIFS])
680 return -EINVAL;
681
682 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
683 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
684 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
685 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
686 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
687
688 return 0;
689}
690
Johannes Berg55682962007-09-20 13:09:35 -0400691static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
692{
693 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200694 int result = 0, rem_txq_params = 0;
695 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200696 u32 changed;
697 u8 retry_short = 0, retry_long = 0;
698 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100699 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400700
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100701 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400702
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100703 mutex_lock(&cfg80211_mutex);
704
Johannes Berg79c97e92009-07-07 03:56:12 +0200705 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100706 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200707 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100708 result = PTR_ERR(rdev);
709 goto unlock;
710 }
711
712 mutex_lock(&rdev->mtx);
713
714 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200715 result = cfg80211_dev_rename(
716 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100717
718 mutex_unlock(&cfg80211_mutex);
719
720 if (result)
721 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400722
Jouni Malinen31888482008-10-30 16:59:24 +0200723 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
724 struct ieee80211_txq_params txq_params;
725 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
726
727 if (!rdev->ops->set_txq_params) {
728 result = -EOPNOTSUPP;
729 goto bad_res;
730 }
731
732 nla_for_each_nested(nl_txq_params,
733 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
734 rem_txq_params) {
735 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
736 nla_data(nl_txq_params),
737 nla_len(nl_txq_params),
738 txq_params_policy);
739 result = parse_txq_params(tb, &txq_params);
740 if (result)
741 goto bad_res;
742
743 result = rdev->ops->set_txq_params(&rdev->wiphy,
744 &txq_params);
745 if (result)
746 goto bad_res;
747 }
748 }
749
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200750 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530751 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400752 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200753
Johannes Berg306d6112008-12-08 12:39:04 +0100754 result = -EINVAL;
755
Sujith094d05d2008-12-12 11:57:43 +0530756 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
757 channel_type = nla_get_u32(info->attrs[
758 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
759 if (channel_type != NL80211_CHAN_NO_HT &&
760 channel_type != NL80211_CHAN_HT20 &&
761 channel_type != NL80211_CHAN_HT40PLUS &&
762 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200763 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200764 }
765
766 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100767
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200768 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200769 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200770 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200771 if (result)
772 goto bad_res;
773 }
774
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200775 changed = 0;
776
777 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
778 retry_short = nla_get_u8(
779 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
780 if (retry_short == 0) {
781 result = -EINVAL;
782 goto bad_res;
783 }
784 changed |= WIPHY_PARAM_RETRY_SHORT;
785 }
786
787 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
788 retry_long = nla_get_u8(
789 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
790 if (retry_long == 0) {
791 result = -EINVAL;
792 goto bad_res;
793 }
794 changed |= WIPHY_PARAM_RETRY_LONG;
795 }
796
797 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
798 frag_threshold = nla_get_u32(
799 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
800 if (frag_threshold < 256) {
801 result = -EINVAL;
802 goto bad_res;
803 }
804 if (frag_threshold != (u32) -1) {
805 /*
806 * Fragments (apart from the last one) are required to
807 * have even length. Make the fragmentation code
808 * simpler by stripping LSB should someone try to use
809 * odd threshold value.
810 */
811 frag_threshold &= ~0x1;
812 }
813 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
814 }
815
816 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
817 rts_threshold = nla_get_u32(
818 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
819 changed |= WIPHY_PARAM_RTS_THRESHOLD;
820 }
821
Lukáš Turek81077e82009-12-21 22:50:47 +0100822 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
823 coverage_class = nla_get_u8(
824 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
825 changed |= WIPHY_PARAM_COVERAGE_CLASS;
826 }
827
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200828 if (changed) {
829 u8 old_retry_short, old_retry_long;
830 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100831 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200832
833 if (!rdev->ops->set_wiphy_params) {
834 result = -EOPNOTSUPP;
835 goto bad_res;
836 }
837
838 old_retry_short = rdev->wiphy.retry_short;
839 old_retry_long = rdev->wiphy.retry_long;
840 old_frag_threshold = rdev->wiphy.frag_threshold;
841 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100842 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200843
844 if (changed & WIPHY_PARAM_RETRY_SHORT)
845 rdev->wiphy.retry_short = retry_short;
846 if (changed & WIPHY_PARAM_RETRY_LONG)
847 rdev->wiphy.retry_long = retry_long;
848 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
849 rdev->wiphy.frag_threshold = frag_threshold;
850 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
851 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100852 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
853 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200854
855 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
856 if (result) {
857 rdev->wiphy.retry_short = old_retry_short;
858 rdev->wiphy.retry_long = old_retry_long;
859 rdev->wiphy.frag_threshold = old_frag_threshold;
860 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100861 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200862 }
863 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200864
Johannes Berg306d6112008-12-08 12:39:04 +0100865 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100866 mutex_unlock(&rdev->mtx);
867 unlock:
868 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400869 return result;
870}
871
872
873static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200874 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400875 struct net_device *dev)
876{
877 void *hdr;
878
879 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
880 if (!hdr)
881 return -1;
882
883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200884 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400885 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200886 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200887
888 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
889 rdev->devlist_generation ^
890 (cfg80211_rdev_list_generation << 2));
891
Johannes Berg55682962007-09-20 13:09:35 -0400892 return genlmsg_end(msg, hdr);
893
894 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700895 genlmsg_cancel(msg, hdr);
896 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400897}
898
899static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
900{
901 int wp_idx = 0;
902 int if_idx = 0;
903 int wp_start = cb->args[0];
904 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200905 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400906 struct wireless_dev *wdev;
907
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500908 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200909 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
910 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200911 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200912 if (wp_idx < wp_start) {
913 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400914 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200915 }
Johannes Berg55682962007-09-20 13:09:35 -0400916 if_idx = 0;
917
Johannes Bergf5ea9122009-08-07 16:17:38 +0200918 mutex_lock(&rdev->devlist_mtx);
919 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200920 if (if_idx < if_start) {
921 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400922 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200923 }
Johannes Berg55682962007-09-20 13:09:35 -0400924 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
925 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200926 rdev, wdev->netdev) < 0) {
927 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200928 goto out;
929 }
930 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400931 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200932 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200933
934 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400935 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200936 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500937 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400938
939 cb->args[0] = wp_idx;
940 cb->args[1] = if_idx;
941
942 return skb->len;
943}
944
945static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
946{
947 struct sk_buff *msg;
948 struct cfg80211_registered_device *dev;
949 struct net_device *netdev;
950 int err;
951
Johannes Berg463d0182009-07-14 00:33:35 +0200952 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400953 if (err)
954 return err;
955
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400957 if (!msg)
958 goto out_err;
959
Johannes Bergd7264052009-04-19 16:23:20 +0200960 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
961 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400962 goto out_free;
963
964 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200965 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400966
Johannes Berg134e6372009-07-10 09:51:34 +0000967 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400968
969 out_free:
970 nlmsg_free(msg);
971 out_err:
972 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200973 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400974 return -ENOBUFS;
975}
976
Michael Wu66f7ac52008-01-31 19:48:22 +0100977static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
978 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
979 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
980 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
981 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
982 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
983};
984
985static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
986{
987 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
988 int flag;
989
990 *mntrflags = 0;
991
992 if (!nla)
993 return -EINVAL;
994
995 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
996 nla, mntr_flags_policy))
997 return -EINVAL;
998
999 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1000 if (flags[flag])
1001 *mntrflags |= (1<<flag);
1002
1003 return 0;
1004}
1005
Johannes Berg9bc383d2009-11-19 11:55:19 +01001006static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001007 struct net_device *netdev, u8 use_4addr,
1008 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001009{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001010 if (!use_4addr) {
1011 if (netdev && netdev->br_port)
1012 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001013 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001014 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001015
1016 switch (iftype) {
1017 case NL80211_IFTYPE_AP_VLAN:
1018 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1019 return 0;
1020 break;
1021 case NL80211_IFTYPE_STATION:
1022 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1023 return 0;
1024 break;
1025 default:
1026 break;
1027 }
1028
1029 return -EOPNOTSUPP;
1030}
1031
Johannes Berg55682962007-09-20 13:09:35 -04001032static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1033{
Johannes Berg79c97e92009-07-07 03:56:12 +02001034 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001035 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001036 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001037 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001038 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001039 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001040 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001041
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001042 memset(&params, 0, sizeof(params));
1043
Johannes Berg3b858752009-03-12 09:55:09 +01001044 rtnl_lock();
1045
Johannes Berg463d0182009-07-14 00:33:35 +02001046 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001047 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001048 goto unlock_rtnl;
1049
Johannes Berg04a773a2009-04-19 21:24:32 +02001050 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001051
Johannes Berg723b0382008-09-16 20:22:09 +02001052 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001053 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001054 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001055 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001056 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001057 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001058 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001059 }
Johannes Berg723b0382008-09-16 20:22:09 +02001060 }
1061
Johannes Berg92ffe052008-09-16 20:39:36 +02001062 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001063 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001064 err = -EINVAL;
1065 goto unlock;
1066 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001067 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1068 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001069 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001070 }
1071
Felix Fietkau8b787642009-11-10 18:53:10 +01001072 if (info->attrs[NL80211_ATTR_4ADDR]) {
1073 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1074 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001075 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001076 if (err)
1077 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001078 } else {
1079 params.use_4addr = -1;
1080 }
1081
Johannes Berg92ffe052008-09-16 20:39:36 +02001082 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001083 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001084 err = -EINVAL;
1085 goto unlock;
1086 }
1087 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1088 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001089 if (err)
1090 goto unlock;
1091
1092 flags = &_flags;
1093 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001094 }
Johannes Berg3b858752009-03-12 09:55:09 +01001095
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001096 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001097 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001098 else
1099 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001100
Johannes Berg9bc383d2009-11-19 11:55:19 +01001101 if (!err && params.use_4addr != -1)
1102 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1103
Johannes Berg55682962007-09-20 13:09:35 -04001104 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001105 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001106 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001107 unlock_rtnl:
1108 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001109 return err;
1110}
1111
1112static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1113{
Johannes Berg79c97e92009-07-07 03:56:12 +02001114 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001115 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001116 int err;
1117 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001118 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001119
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001120 memset(&params, 0, sizeof(params));
1121
Johannes Berg55682962007-09-20 13:09:35 -04001122 if (!info->attrs[NL80211_ATTR_IFNAME])
1123 return -EINVAL;
1124
1125 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1126 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1127 if (type > NL80211_IFTYPE_MAX)
1128 return -EINVAL;
1129 }
1130
Johannes Berg3b858752009-03-12 09:55:09 +01001131 rtnl_lock();
1132
Johannes Berg79c97e92009-07-07 03:56:12 +02001133 rdev = cfg80211_get_dev_from_info(info);
1134 if (IS_ERR(rdev)) {
1135 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001136 goto unlock_rtnl;
1137 }
Johannes Berg55682962007-09-20 13:09:35 -04001138
Johannes Berg79c97e92009-07-07 03:56:12 +02001139 if (!rdev->ops->add_virtual_intf ||
1140 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001141 err = -EOPNOTSUPP;
1142 goto unlock;
1143 }
1144
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001145 if (type == NL80211_IFTYPE_MESH_POINT &&
1146 info->attrs[NL80211_ATTR_MESH_ID]) {
1147 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1148 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1149 }
1150
Johannes Berg9bc383d2009-11-19 11:55:19 +01001151 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001152 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001153 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001154 if (err)
1155 goto unlock;
1156 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001157
Michael Wu66f7ac52008-01-31 19:48:22 +01001158 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1159 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1160 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001161 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001162 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001163 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001164
Johannes Berg55682962007-09-20 13:09:35 -04001165 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001166 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001167 unlock_rtnl:
1168 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001169 return err;
1170}
1171
1172static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1173{
Johannes Berg79c97e92009-07-07 03:56:12 +02001174 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001175 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001176 struct net_device *dev;
1177
Johannes Berg3b858752009-03-12 09:55:09 +01001178 rtnl_lock();
1179
Johannes Berg463d0182009-07-14 00:33:35 +02001180 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001181 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001182 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001183
Johannes Berg79c97e92009-07-07 03:56:12 +02001184 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001185 err = -EOPNOTSUPP;
1186 goto out;
1187 }
1188
Johannes Berg463d0182009-07-14 00:33:35 +02001189 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001190
1191 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001192 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001193 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001194 unlock_rtnl:
1195 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001196 return err;
1197}
1198
Johannes Berg41ade002007-12-19 02:03:29 +01001199struct get_key_cookie {
1200 struct sk_buff *msg;
1201 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001202 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001203};
1204
1205static void get_key_callback(void *c, struct key_params *params)
1206{
Johannes Bergb9454e82009-07-08 13:29:08 +02001207 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001208 struct get_key_cookie *cookie = c;
1209
1210 if (params->key)
1211 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1212 params->key_len, params->key);
1213
1214 if (params->seq)
1215 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1216 params->seq_len, params->seq);
1217
1218 if (params->cipher)
1219 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1220 params->cipher);
1221
Johannes Bergb9454e82009-07-08 13:29:08 +02001222 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1223 if (!key)
1224 goto nla_put_failure;
1225
1226 if (params->key)
1227 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1228 params->key_len, params->key);
1229
1230 if (params->seq)
1231 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1232 params->seq_len, params->seq);
1233
1234 if (params->cipher)
1235 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1236 params->cipher);
1237
1238 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1239
1240 nla_nest_end(cookie->msg, key);
1241
Johannes Berg41ade002007-12-19 02:03:29 +01001242 return;
1243 nla_put_failure:
1244 cookie->error = 1;
1245}
1246
1247static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1248{
Johannes Berg79c97e92009-07-07 03:56:12 +02001249 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001250 int err;
1251 struct net_device *dev;
1252 u8 key_idx = 0;
1253 u8 *mac_addr = NULL;
1254 struct get_key_cookie cookie = {
1255 .error = 0,
1256 };
1257 void *hdr;
1258 struct sk_buff *msg;
1259
1260 if (info->attrs[NL80211_ATTR_KEY_IDX])
1261 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1262
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001263 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001264 return -EINVAL;
1265
1266 if (info->attrs[NL80211_ATTR_MAC])
1267 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1268
Johannes Berg3b858752009-03-12 09:55:09 +01001269 rtnl_lock();
1270
Johannes Berg463d0182009-07-14 00:33:35 +02001271 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001272 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001273 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001274
Johannes Berg79c97e92009-07-07 03:56:12 +02001275 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001276 err = -EOPNOTSUPP;
1277 goto out;
1278 }
1279
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001281 if (!msg) {
1282 err = -ENOMEM;
1283 goto out;
1284 }
1285
1286 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1287 NL80211_CMD_NEW_KEY);
1288
1289 if (IS_ERR(hdr)) {
1290 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001291 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001292 }
1293
1294 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001295 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001296
1297 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1298 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1299 if (mac_addr)
1300 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1301
Johannes Berg79c97e92009-07-07 03:56:12 +02001302 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001303 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001304
1305 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001306 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001307
1308 if (cookie.error)
1309 goto nla_put_failure;
1310
1311 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001312 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001313 goto out;
1314
1315 nla_put_failure:
1316 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001317 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001318 nlmsg_free(msg);
1319 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001320 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001321 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001322 unlock_rtnl:
1323 rtnl_unlock();
1324
Johannes Berg41ade002007-12-19 02:03:29 +01001325 return err;
1326}
1327
1328static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1329{
Johannes Berg79c97e92009-07-07 03:56:12 +02001330 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001331 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001332 int err;
1333 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001334 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1335 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001336
Johannes Bergb9454e82009-07-08 13:29:08 +02001337 err = nl80211_parse_key(info, &key);
1338 if (err)
1339 return err;
1340
1341 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001342 return -EINVAL;
1343
Johannes Bergb9454e82009-07-08 13:29:08 +02001344 /* only support setting default key */
1345 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001346 return -EINVAL;
1347
Johannes Berg3b858752009-03-12 09:55:09 +01001348 rtnl_lock();
1349
Johannes Berg463d0182009-07-14 00:33:35 +02001350 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001351 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001352 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001353
Johannes Bergb9454e82009-07-08 13:29:08 +02001354 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001355 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001356 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001357 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001358
1359 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001360 err = -EOPNOTSUPP;
1361 goto out;
1362 }
1363
Johannes Bergfffd0932009-07-08 14:22:54 +02001364 wdev_lock(dev->ieee80211_ptr);
1365 err = nl80211_key_allowed(dev->ieee80211_ptr);
1366 if (!err)
1367 err = func(&rdev->wiphy, dev, key.idx);
1368
Johannes Berg3d23e342009-09-29 23:27:28 +02001369#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001370 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001371 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001372 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001373 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001374 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001375 }
1376#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001377 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001378
1379 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001380 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001381 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001382
1383 unlock_rtnl:
1384 rtnl_unlock();
1385
Johannes Berg41ade002007-12-19 02:03:29 +01001386 return err;
1387}
1388
1389static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1390{
Johannes Berg79c97e92009-07-07 03:56:12 +02001391 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001392 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001393 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001394 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001395 u8 *mac_addr = NULL;
1396
Johannes Bergb9454e82009-07-08 13:29:08 +02001397 err = nl80211_parse_key(info, &key);
1398 if (err)
1399 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001400
Johannes Bergb9454e82009-07-08 13:29:08 +02001401 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001402 return -EINVAL;
1403
Johannes Berg41ade002007-12-19 02:03:29 +01001404 if (info->attrs[NL80211_ATTR_MAC])
1405 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1406
Johannes Berg3b858752009-03-12 09:55:09 +01001407 rtnl_lock();
1408
Johannes Berg463d0182009-07-14 00:33:35 +02001409 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001410 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001411 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001412
Johannes Berg79c97e92009-07-07 03:56:12 +02001413 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001414 err = -EOPNOTSUPP;
1415 goto out;
1416 }
1417
Johannes Bergfffd0932009-07-08 14:22:54 +02001418 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1419 err = -EINVAL;
1420 goto out;
1421 }
1422
1423 wdev_lock(dev->ieee80211_ptr);
1424 err = nl80211_key_allowed(dev->ieee80211_ptr);
1425 if (!err)
1426 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1427 mac_addr, &key.p);
1428 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001429
1430 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001431 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001432 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001433 unlock_rtnl:
1434 rtnl_unlock();
1435
Johannes Berg41ade002007-12-19 02:03:29 +01001436 return err;
1437}
1438
1439static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1440{
Johannes Berg79c97e92009-07-07 03:56:12 +02001441 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001442 int err;
1443 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001444 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001445 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001446
Johannes Bergb9454e82009-07-08 13:29:08 +02001447 err = nl80211_parse_key(info, &key);
1448 if (err)
1449 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001450
1451 if (info->attrs[NL80211_ATTR_MAC])
1452 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1453
Johannes Berg3b858752009-03-12 09:55:09 +01001454 rtnl_lock();
1455
Johannes Berg463d0182009-07-14 00:33:35 +02001456 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001457 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001458 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001459
Johannes Berg79c97e92009-07-07 03:56:12 +02001460 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001461 err = -EOPNOTSUPP;
1462 goto out;
1463 }
1464
Johannes Bergfffd0932009-07-08 14:22:54 +02001465 wdev_lock(dev->ieee80211_ptr);
1466 err = nl80211_key_allowed(dev->ieee80211_ptr);
1467 if (!err)
1468 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001469
Johannes Berg3d23e342009-09-29 23:27:28 +02001470#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001471 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001472 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001473 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001474 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001475 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1476 }
1477#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001478 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001479
Johannes Berg41ade002007-12-19 02:03:29 +01001480 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001481 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001482 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001483
1484 unlock_rtnl:
1485 rtnl_unlock();
1486
Johannes Berg41ade002007-12-19 02:03:29 +01001487 return err;
1488}
1489
Johannes Berged1b6cc2007-12-19 02:03:32 +01001490static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1491{
1492 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1493 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001494 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001495 int err;
1496 struct net_device *dev;
1497 struct beacon_parameters params;
1498 int haveinfo = 0;
1499
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001500 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1501 return -EINVAL;
1502
Johannes Berg3b858752009-03-12 09:55:09 +01001503 rtnl_lock();
1504
Johannes Berg463d0182009-07-14 00:33:35 +02001505 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001506 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001507 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001508
Jouni Malineneec60b02009-03-20 21:21:19 +02001509 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1510 err = -EOPNOTSUPP;
1511 goto out;
1512 }
1513
Johannes Berged1b6cc2007-12-19 02:03:32 +01001514 switch (info->genlhdr->cmd) {
1515 case NL80211_CMD_NEW_BEACON:
1516 /* these are required for NEW_BEACON */
1517 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1518 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1519 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1520 err = -EINVAL;
1521 goto out;
1522 }
1523
Johannes Berg79c97e92009-07-07 03:56:12 +02001524 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001525 break;
1526 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001527 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001528 break;
1529 default:
1530 WARN_ON(1);
1531 err = -EOPNOTSUPP;
1532 goto out;
1533 }
1534
1535 if (!call) {
1536 err = -EOPNOTSUPP;
1537 goto out;
1538 }
1539
1540 memset(&params, 0, sizeof(params));
1541
1542 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1543 params.interval =
1544 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1545 haveinfo = 1;
1546 }
1547
1548 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1549 params.dtim_period =
1550 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1551 haveinfo = 1;
1552 }
1553
1554 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1555 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1556 params.head_len =
1557 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1558 haveinfo = 1;
1559 }
1560
1561 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1562 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1563 params.tail_len =
1564 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1565 haveinfo = 1;
1566 }
1567
1568 if (!haveinfo) {
1569 err = -EINVAL;
1570 goto out;
1571 }
1572
Johannes Berg79c97e92009-07-07 03:56:12 +02001573 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001574
1575 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001576 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001577 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001578 unlock_rtnl:
1579 rtnl_unlock();
1580
Johannes Berged1b6cc2007-12-19 02:03:32 +01001581 return err;
1582}
1583
1584static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1585{
Johannes Berg79c97e92009-07-07 03:56:12 +02001586 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001587 int err;
1588 struct net_device *dev;
1589
Johannes Berg3b858752009-03-12 09:55:09 +01001590 rtnl_lock();
1591
Johannes Berg463d0182009-07-14 00:33:35 +02001592 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001593 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001594 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001595
Johannes Berg79c97e92009-07-07 03:56:12 +02001596 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001597 err = -EOPNOTSUPP;
1598 goto out;
1599 }
1600
Jouni Malineneec60b02009-03-20 21:21:19 +02001601 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1602 err = -EOPNOTSUPP;
1603 goto out;
1604 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001605 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001606
1607 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001608 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001609 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001610 unlock_rtnl:
1611 rtnl_unlock();
1612
Johannes Berged1b6cc2007-12-19 02:03:32 +01001613 return err;
1614}
1615
Johannes Berg5727ef12007-12-19 02:03:34 +01001616static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1617 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1618 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1619 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001620 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001621};
1622
Johannes Bergeccb8e82009-05-11 21:57:56 +03001623static int parse_station_flags(struct genl_info *info,
1624 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001625{
1626 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001627 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001628 int flag;
1629
Johannes Bergeccb8e82009-05-11 21:57:56 +03001630 /*
1631 * Try parsing the new attribute first so userspace
1632 * can specify both for older kernels.
1633 */
1634 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1635 if (nla) {
1636 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001637
Johannes Bergeccb8e82009-05-11 21:57:56 +03001638 sta_flags = nla_data(nla);
1639 params->sta_flags_mask = sta_flags->mask;
1640 params->sta_flags_set = sta_flags->set;
1641 if ((params->sta_flags_mask |
1642 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1643 return -EINVAL;
1644 return 0;
1645 }
1646
1647 /* if present, parse the old attribute */
1648
1649 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001650 if (!nla)
1651 return 0;
1652
1653 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1654 nla, sta_flags_policy))
1655 return -EINVAL;
1656
Johannes Bergeccb8e82009-05-11 21:57:56 +03001657 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1658 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001659
1660 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1661 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001662 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001663
1664 return 0;
1665}
1666
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001667static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1668 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001669 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001670{
1671 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001672 struct nlattr *sinfoattr, *txrate;
1673 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001674
1675 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1676 if (!hdr)
1677 return -1;
1678
1679 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1680 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1681
Johannes Bergf5ea9122009-08-07 16:17:38 +02001682 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1683
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001684 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1685 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001686 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001687 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1688 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1689 sinfo->inactive_time);
1690 if (sinfo->filled & STATION_INFO_RX_BYTES)
1691 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1692 sinfo->rx_bytes);
1693 if (sinfo->filled & STATION_INFO_TX_BYTES)
1694 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1695 sinfo->tx_bytes);
1696 if (sinfo->filled & STATION_INFO_LLID)
1697 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1698 sinfo->llid);
1699 if (sinfo->filled & STATION_INFO_PLID)
1700 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1701 sinfo->plid);
1702 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1703 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1704 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001705 if (sinfo->filled & STATION_INFO_SIGNAL)
1706 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1707 sinfo->signal);
1708 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1709 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1710 if (!txrate)
1711 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001712
John W. Linville254416a2009-12-09 16:43:52 -05001713 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1714 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001715 if (bitrate > 0)
1716 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1717
1718 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1719 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1720 sinfo->txrate.mcs);
1721 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1722 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1723 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1724 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1725
1726 nla_nest_end(msg, txrate);
1727 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001728 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1729 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1730 sinfo->rx_packets);
1731 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1732 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1733 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001734 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001735
1736 return genlmsg_end(msg, hdr);
1737
1738 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001739 genlmsg_cancel(msg, hdr);
1740 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001741}
1742
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001743static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001744 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001745{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001746 struct station_info sinfo;
1747 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001748 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001749 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001750 int ifidx = cb->args[0];
1751 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001752 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001753
Holger Schuriga0438972009-11-11 11:30:02 +01001754 if (!ifidx)
1755 ifidx = nl80211_get_ifidx(cb);
1756 if (ifidx < 0)
1757 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001758
Johannes Berg3b858752009-03-12 09:55:09 +01001759 rtnl_lock();
1760
Johannes Berg463d0182009-07-14 00:33:35 +02001761 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001762 if (!netdev) {
1763 err = -ENODEV;
1764 goto out_rtnl;
1765 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001766
Johannes Berg463d0182009-07-14 00:33:35 +02001767 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001768 if (IS_ERR(dev)) {
1769 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001770 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001771 }
1772
1773 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001774 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001775 goto out_err;
1776 }
1777
Johannes Bergbba95fe2008-07-29 13:22:51 +02001778 while (1) {
1779 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1780 mac_addr, &sinfo);
1781 if (err == -ENOENT)
1782 break;
1783 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001784 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001785
1786 if (nl80211_send_station(skb,
1787 NETLINK_CB(cb->skb).pid,
1788 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1789 netdev, mac_addr,
1790 &sinfo) < 0)
1791 goto out;
1792
1793 sta_idx++;
1794 }
1795
1796
1797 out:
1798 cb->args[1] = sta_idx;
1799 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001800 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001801 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001802 out_rtnl:
1803 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001804
1805 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001806}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001807
Johannes Berg5727ef12007-12-19 02:03:34 +01001808static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1809{
Johannes Berg79c97e92009-07-07 03:56:12 +02001810 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001811 int err;
1812 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001813 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001814 struct sk_buff *msg;
1815 u8 *mac_addr = NULL;
1816
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001817 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001818
1819 if (!info->attrs[NL80211_ATTR_MAC])
1820 return -EINVAL;
1821
1822 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1823
Johannes Berg3b858752009-03-12 09:55:09 +01001824 rtnl_lock();
1825
Johannes Berg463d0182009-07-14 00:33:35 +02001826 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001827 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001828 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001829
Johannes Berg79c97e92009-07-07 03:56:12 +02001830 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001831 err = -EOPNOTSUPP;
1832 goto out;
1833 }
1834
Johannes Berg79c97e92009-07-07 03:56:12 +02001835 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001836 if (err)
1837 goto out;
1838
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001839 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001840 if (!msg)
1841 goto out;
1842
1843 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001844 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001845 goto out_free;
1846
Johannes Berg134e6372009-07-10 09:51:34 +00001847 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001848 goto out;
1849
1850 out_free:
1851 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001852 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001853 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001854 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001855 out_rtnl:
1856 rtnl_unlock();
1857
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001858 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001859}
1860
1861/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001862 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001863 */
Johannes Berg463d0182009-07-14 00:33:35 +02001864static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001865 struct cfg80211_registered_device *rdev,
1866 struct net_device **vlan)
1867{
Johannes Berg463d0182009-07-14 00:33:35 +02001868 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001869 *vlan = NULL;
1870
1871 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001872 *vlan = dev_get_by_index(genl_info_net(info),
1873 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001874 if (!*vlan)
1875 return -ENODEV;
1876 if (!(*vlan)->ieee80211_ptr)
1877 return -EINVAL;
1878 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1879 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001880 if (!netif_running(*vlan))
1881 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001882 }
1883 return 0;
1884}
1885
1886static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1887{
Johannes Berg79c97e92009-07-07 03:56:12 +02001888 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001889 int err;
1890 struct net_device *dev;
1891 struct station_parameters params;
1892 u8 *mac_addr = NULL;
1893
1894 memset(&params, 0, sizeof(params));
1895
1896 params.listen_interval = -1;
1897
1898 if (info->attrs[NL80211_ATTR_STA_AID])
1899 return -EINVAL;
1900
1901 if (!info->attrs[NL80211_ATTR_MAC])
1902 return -EINVAL;
1903
1904 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1905
1906 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1907 params.supported_rates =
1908 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1909 params.supported_rates_len =
1910 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1911 }
1912
1913 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1914 params.listen_interval =
1915 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1916
Jouni Malinen36aedc92008-08-25 11:58:58 +03001917 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1918 params.ht_capa =
1919 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1920
Johannes Bergeccb8e82009-05-11 21:57:56 +03001921 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001922 return -EINVAL;
1923
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001924 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1925 params.plink_action =
1926 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1927
Johannes Berg3b858752009-03-12 09:55:09 +01001928 rtnl_lock();
1929
Johannes Berg463d0182009-07-14 00:33:35 +02001930 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001931 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001932 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001933
Johannes Berg463d0182009-07-14 00:33:35 +02001934 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001935 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001936 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001937
1938 /* validate settings */
1939 err = 0;
1940
1941 switch (dev->ieee80211_ptr->iftype) {
1942 case NL80211_IFTYPE_AP:
1943 case NL80211_IFTYPE_AP_VLAN:
1944 /* disallow mesh-specific things */
1945 if (params.plink_action)
1946 err = -EINVAL;
1947 break;
1948 case NL80211_IFTYPE_STATION:
1949 /* disallow everything but AUTHORIZED flag */
1950 if (params.plink_action)
1951 err = -EINVAL;
1952 if (params.vlan)
1953 err = -EINVAL;
1954 if (params.supported_rates)
1955 err = -EINVAL;
1956 if (params.ht_capa)
1957 err = -EINVAL;
1958 if (params.listen_interval >= 0)
1959 err = -EINVAL;
1960 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1961 err = -EINVAL;
1962 break;
1963 case NL80211_IFTYPE_MESH_POINT:
1964 /* disallow things mesh doesn't support */
1965 if (params.vlan)
1966 err = -EINVAL;
1967 if (params.ht_capa)
1968 err = -EINVAL;
1969 if (params.listen_interval >= 0)
1970 err = -EINVAL;
1971 if (params.supported_rates)
1972 err = -EINVAL;
1973 if (params.sta_flags_mask)
1974 err = -EINVAL;
1975 break;
1976 default:
1977 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001978 }
1979
Johannes Berg5727ef12007-12-19 02:03:34 +01001980 if (err)
1981 goto out;
1982
Johannes Berg79c97e92009-07-07 03:56:12 +02001983 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001984 err = -EOPNOTSUPP;
1985 goto out;
1986 }
1987
Johannes Berg79c97e92009-07-07 03:56:12 +02001988 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001989
1990 out:
1991 if (params.vlan)
1992 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001993 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001994 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001995 out_rtnl:
1996 rtnl_unlock();
1997
Johannes Berg5727ef12007-12-19 02:03:34 +01001998 return err;
1999}
2000
2001static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2002{
Johannes Berg79c97e92009-07-07 03:56:12 +02002003 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002004 int err;
2005 struct net_device *dev;
2006 struct station_parameters params;
2007 u8 *mac_addr = NULL;
2008
2009 memset(&params, 0, sizeof(params));
2010
2011 if (!info->attrs[NL80211_ATTR_MAC])
2012 return -EINVAL;
2013
Johannes Berg5727ef12007-12-19 02:03:34 +01002014 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2015 return -EINVAL;
2016
2017 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2018 return -EINVAL;
2019
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002020 if (!info->attrs[NL80211_ATTR_STA_AID])
2021 return -EINVAL;
2022
Johannes Berg5727ef12007-12-19 02:03:34 +01002023 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2024 params.supported_rates =
2025 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2026 params.supported_rates_len =
2027 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2028 params.listen_interval =
2029 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002030
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002031 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2032 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2033 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002034
Jouni Malinen36aedc92008-08-25 11:58:58 +03002035 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2036 params.ht_capa =
2037 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002038
Johannes Bergeccb8e82009-05-11 21:57:56 +03002039 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002040 return -EINVAL;
2041
Johannes Berg3b858752009-03-12 09:55:09 +01002042 rtnl_lock();
2043
Johannes Berg463d0182009-07-14 00:33:35 +02002044 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002045 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002046 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002047
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
2050 err = -EINVAL;
2051 goto out;
2052 }
2053
Johannes Berg463d0182009-07-14 00:33:35 +02002054 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002055 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002056 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002057
2058 /* validate settings */
2059 err = 0;
2060
Johannes Berg79c97e92009-07-07 03:56:12 +02002061 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002062 err = -EOPNOTSUPP;
2063 goto out;
2064 }
2065
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002066 if (!netif_running(dev)) {
2067 err = -ENETDOWN;
2068 goto out;
2069 }
2070
Johannes Berg79c97e92009-07-07 03:56:12 +02002071 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002072
2073 out:
2074 if (params.vlan)
2075 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002076 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002077 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002078 out_rtnl:
2079 rtnl_unlock();
2080
Johannes Berg5727ef12007-12-19 02:03:34 +01002081 return err;
2082}
2083
2084static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2085{
Johannes Berg79c97e92009-07-07 03:56:12 +02002086 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002087 int err;
2088 struct net_device *dev;
2089 u8 *mac_addr = NULL;
2090
2091 if (info->attrs[NL80211_ATTR_MAC])
2092 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2093
Johannes Berg3b858752009-03-12 09:55:09 +01002094 rtnl_lock();
2095
Johannes Berg463d0182009-07-14 00:33:35 +02002096 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002097 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002098 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002099
Johannes Berge80cf852009-05-11 14:43:13 +02002100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002101 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2102 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002103 err = -EINVAL;
2104 goto out;
2105 }
2106
Johannes Berg79c97e92009-07-07 03:56:12 +02002107 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002108 err = -EOPNOTSUPP;
2109 goto out;
2110 }
2111
Johannes Berg79c97e92009-07-07 03:56:12 +02002112 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002113
2114 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002115 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002116 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002117 out_rtnl:
2118 rtnl_unlock();
2119
Johannes Berg5727ef12007-12-19 02:03:34 +01002120 return err;
2121}
2122
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002123static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2124 int flags, struct net_device *dev,
2125 u8 *dst, u8 *next_hop,
2126 struct mpath_info *pinfo)
2127{
2128 void *hdr;
2129 struct nlattr *pinfoattr;
2130
2131 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2132 if (!hdr)
2133 return -1;
2134
2135 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2136 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2137 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2138
Johannes Bergf5ea9122009-08-07 16:17:38 +02002139 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2140
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002141 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2142 if (!pinfoattr)
2143 goto nla_put_failure;
2144 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2145 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2146 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002147 if (pinfo->filled & MPATH_INFO_SN)
2148 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2149 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002150 if (pinfo->filled & MPATH_INFO_METRIC)
2151 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2152 pinfo->metric);
2153 if (pinfo->filled & MPATH_INFO_EXPTIME)
2154 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2155 pinfo->exptime);
2156 if (pinfo->filled & MPATH_INFO_FLAGS)
2157 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2158 pinfo->flags);
2159 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2160 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2161 pinfo->discovery_timeout);
2162 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2163 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2164 pinfo->discovery_retries);
2165
2166 nla_nest_end(msg, pinfoattr);
2167
2168 return genlmsg_end(msg, hdr);
2169
2170 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002171 genlmsg_cancel(msg, hdr);
2172 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002173}
2174
2175static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002176 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002177{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002178 struct mpath_info pinfo;
2179 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002180 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002181 u8 dst[ETH_ALEN];
2182 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002183 int ifidx = cb->args[0];
2184 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002185 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002186
Holger Schuriga0438972009-11-11 11:30:02 +01002187 if (!ifidx)
2188 ifidx = nl80211_get_ifidx(cb);
2189 if (ifidx < 0)
2190 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002191
Johannes Berg3b858752009-03-12 09:55:09 +01002192 rtnl_lock();
2193
Johannes Berg463d0182009-07-14 00:33:35 +02002194 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002195 if (!netdev) {
2196 err = -ENODEV;
2197 goto out_rtnl;
2198 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002199
Johannes Berg463d0182009-07-14 00:33:35 +02002200 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002201 if (IS_ERR(dev)) {
2202 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002203 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002204 }
2205
2206 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002207 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002208 goto out_err;
2209 }
2210
Jouni Malineneec60b02009-03-20 21:21:19 +02002211 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2212 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002213 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002214 }
2215
Johannes Bergbba95fe2008-07-29 13:22:51 +02002216 while (1) {
2217 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2218 dst, next_hop, &pinfo);
2219 if (err == -ENOENT)
2220 break;
2221 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002222 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002223
2224 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2225 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2226 netdev, dst, next_hop,
2227 &pinfo) < 0)
2228 goto out;
2229
2230 path_idx++;
2231 }
2232
2233
2234 out:
2235 cb->args[1] = path_idx;
2236 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002237 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002238 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002239 out_rtnl:
2240 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002241
2242 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002243}
2244
2245static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2246{
Johannes Berg79c97e92009-07-07 03:56:12 +02002247 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002248 int err;
2249 struct net_device *dev;
2250 struct mpath_info pinfo;
2251 struct sk_buff *msg;
2252 u8 *dst = NULL;
2253 u8 next_hop[ETH_ALEN];
2254
2255 memset(&pinfo, 0, sizeof(pinfo));
2256
2257 if (!info->attrs[NL80211_ATTR_MAC])
2258 return -EINVAL;
2259
2260 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2261
Johannes Berg3b858752009-03-12 09:55:09 +01002262 rtnl_lock();
2263
Johannes Berg463d0182009-07-14 00:33:35 +02002264 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002265 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002266 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002267
Johannes Berg79c97e92009-07-07 03:56:12 +02002268 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002269 err = -EOPNOTSUPP;
2270 goto out;
2271 }
2272
Jouni Malineneec60b02009-03-20 21:21:19 +02002273 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2274 err = -EOPNOTSUPP;
2275 goto out;
2276 }
2277
Johannes Berg79c97e92009-07-07 03:56:12 +02002278 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002279 if (err)
2280 goto out;
2281
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002283 if (!msg)
2284 goto out;
2285
2286 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2287 dev, dst, next_hop, &pinfo) < 0)
2288 goto out_free;
2289
Johannes Berg134e6372009-07-10 09:51:34 +00002290 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002291 goto out;
2292
2293 out_free:
2294 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002296 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002297 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002298 out_rtnl:
2299 rtnl_unlock();
2300
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002301 return err;
2302}
2303
2304static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2305{
Johannes Berg79c97e92009-07-07 03:56:12 +02002306 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002307 int err;
2308 struct net_device *dev;
2309 u8 *dst = NULL;
2310 u8 *next_hop = NULL;
2311
2312 if (!info->attrs[NL80211_ATTR_MAC])
2313 return -EINVAL;
2314
2315 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2316 return -EINVAL;
2317
2318 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2319 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2320
Johannes Berg3b858752009-03-12 09:55:09 +01002321 rtnl_lock();
2322
Johannes Berg463d0182009-07-14 00:33:35 +02002323 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002324 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002325 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326
Johannes Berg79c97e92009-07-07 03:56:12 +02002327 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002328 err = -EOPNOTSUPP;
2329 goto out;
2330 }
2331
Jouni Malineneec60b02009-03-20 21:21:19 +02002332 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2333 err = -EOPNOTSUPP;
2334 goto out;
2335 }
2336
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002337 if (!netif_running(dev)) {
2338 err = -ENETDOWN;
2339 goto out;
2340 }
2341
Johannes Berg79c97e92009-07-07 03:56:12 +02002342 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002343
2344 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002345 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002346 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002347 out_rtnl:
2348 rtnl_unlock();
2349
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002350 return err;
2351}
2352static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2353{
Johannes Berg79c97e92009-07-07 03:56:12 +02002354 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002355 int err;
2356 struct net_device *dev;
2357 u8 *dst = NULL;
2358 u8 *next_hop = NULL;
2359
2360 if (!info->attrs[NL80211_ATTR_MAC])
2361 return -EINVAL;
2362
2363 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2364 return -EINVAL;
2365
2366 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2367 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2368
Johannes Berg3b858752009-03-12 09:55:09 +01002369 rtnl_lock();
2370
Johannes Berg463d0182009-07-14 00:33:35 +02002371 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002373 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002374
Johannes Berg79c97e92009-07-07 03:56:12 +02002375 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002376 err = -EOPNOTSUPP;
2377 goto out;
2378 }
2379
Jouni Malineneec60b02009-03-20 21:21:19 +02002380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2381 err = -EOPNOTSUPP;
2382 goto out;
2383 }
2384
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002385 if (!netif_running(dev)) {
2386 err = -ENETDOWN;
2387 goto out;
2388 }
2389
Johannes Berg79c97e92009-07-07 03:56:12 +02002390 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002391
2392 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002393 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002395 out_rtnl:
2396 rtnl_unlock();
2397
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002398 return err;
2399}
2400
2401static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2402{
Johannes Berg79c97e92009-07-07 03:56:12 +02002403 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 int err;
2405 struct net_device *dev;
2406 u8 *dst = NULL;
2407
2408 if (info->attrs[NL80211_ATTR_MAC])
2409 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2410
Johannes Berg3b858752009-03-12 09:55:09 +01002411 rtnl_lock();
2412
Johannes Berg463d0182009-07-14 00:33:35 +02002413 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002415 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002416
Johannes Berg79c97e92009-07-07 03:56:12 +02002417 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002418 err = -EOPNOTSUPP;
2419 goto out;
2420 }
2421
Johannes Berg79c97e92009-07-07 03:56:12 +02002422 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002423
2424 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002425 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002426 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002427 out_rtnl:
2428 rtnl_unlock();
2429
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002430 return err;
2431}
2432
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002433static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2434{
Johannes Berg79c97e92009-07-07 03:56:12 +02002435 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002436 int err;
2437 struct net_device *dev;
2438 struct bss_parameters params;
2439
2440 memset(&params, 0, sizeof(params));
2441 /* default to not changing parameters */
2442 params.use_cts_prot = -1;
2443 params.use_short_preamble = -1;
2444 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002445 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002446
2447 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2448 params.use_cts_prot =
2449 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2450 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2451 params.use_short_preamble =
2452 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2453 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2454 params.use_short_slot_time =
2455 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002456 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2457 params.basic_rates =
2458 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2459 params.basic_rates_len =
2460 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2461 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002462 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2463 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002464
Johannes Berg3b858752009-03-12 09:55:09 +01002465 rtnl_lock();
2466
Johannes Berg463d0182009-07-14 00:33:35 +02002467 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002468 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002469 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002470
Johannes Berg79c97e92009-07-07 03:56:12 +02002471 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002472 err = -EOPNOTSUPP;
2473 goto out;
2474 }
2475
Jouni Malineneec60b02009-03-20 21:21:19 +02002476 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2477 err = -EOPNOTSUPP;
2478 goto out;
2479 }
2480
Johannes Berg79c97e92009-07-07 03:56:12 +02002481 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002482
2483 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002484 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002485 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002486 out_rtnl:
2487 rtnl_unlock();
2488
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002489 return err;
2490}
2491
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002492static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002493 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2494 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2495 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2496 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2497 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2498 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2499};
2500
2501static int parse_reg_rule(struct nlattr *tb[],
2502 struct ieee80211_reg_rule *reg_rule)
2503{
2504 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2505 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2506
2507 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2508 return -EINVAL;
2509 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2510 return -EINVAL;
2511 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2512 return -EINVAL;
2513 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2514 return -EINVAL;
2515 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2516 return -EINVAL;
2517
2518 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2519
2520 freq_range->start_freq_khz =
2521 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2522 freq_range->end_freq_khz =
2523 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2524 freq_range->max_bandwidth_khz =
2525 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2526
2527 power_rule->max_eirp =
2528 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2529
2530 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2531 power_rule->max_antenna_gain =
2532 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2533
2534 return 0;
2535}
2536
2537static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2538{
2539 int r;
2540 char *data = NULL;
2541
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002542 /*
2543 * You should only get this when cfg80211 hasn't yet initialized
2544 * completely when built-in to the kernel right between the time
2545 * window between nl80211_init() and regulatory_init(), if that is
2546 * even possible.
2547 */
2548 mutex_lock(&cfg80211_mutex);
2549 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002550 mutex_unlock(&cfg80211_mutex);
2551 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002552 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002553 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002554
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002555 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2556 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002557
2558 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2559
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002560 r = regulatory_hint_user(data);
2561
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002562 return r;
2563}
2564
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002565static int nl80211_get_mesh_params(struct sk_buff *skb,
2566 struct genl_info *info)
2567{
Johannes Berg79c97e92009-07-07 03:56:12 +02002568 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002569 struct mesh_config cur_params;
2570 int err;
2571 struct net_device *dev;
2572 void *hdr;
2573 struct nlattr *pinfoattr;
2574 struct sk_buff *msg;
2575
Johannes Berg3b858752009-03-12 09:55:09 +01002576 rtnl_lock();
2577
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002578 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002579 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002580 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002581 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002582
Johannes Berg79c97e92009-07-07 03:56:12 +02002583 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002584 err = -EOPNOTSUPP;
2585 goto out;
2586 }
2587
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002588 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002589 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002590 if (err)
2591 goto out;
2592
2593 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002595 if (!msg) {
2596 err = -ENOBUFS;
2597 goto out;
2598 }
2599 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2600 NL80211_CMD_GET_MESH_PARAMS);
2601 if (!hdr)
2602 goto nla_put_failure;
2603 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2604 if (!pinfoattr)
2605 goto nla_put_failure;
2606 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2607 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2608 cur_params.dot11MeshRetryTimeout);
2609 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2610 cur_params.dot11MeshConfirmTimeout);
2611 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2612 cur_params.dot11MeshHoldingTimeout);
2613 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2614 cur_params.dot11MeshMaxPeerLinks);
2615 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2616 cur_params.dot11MeshMaxRetries);
2617 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2618 cur_params.dot11MeshTTL);
2619 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2620 cur_params.auto_open_plinks);
2621 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2622 cur_params.dot11MeshHWMPmaxPREQretries);
2623 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2624 cur_params.path_refresh_time);
2625 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2626 cur_params.min_discovery_timeout);
2627 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2628 cur_params.dot11MeshHWMPactivePathTimeout);
2629 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2630 cur_params.dot11MeshHWMPpreqMinInterval);
2631 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2632 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002633 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2634 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002635 nla_nest_end(msg, pinfoattr);
2636 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002637 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002638 goto out;
2639
Johannes Berg3b858752009-03-12 09:55:09 +01002640 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002641 genlmsg_cancel(msg, hdr);
2642 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002643 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002644 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002645 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002646 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002647 out_rtnl:
2648 rtnl_unlock();
2649
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002650 return err;
2651}
2652
2653#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2654do {\
2655 if (table[attr_num]) {\
2656 cfg.param = nla_fn(table[attr_num]); \
2657 mask |= (1 << (attr_num - 1)); \
2658 } \
2659} while (0);\
2660
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002661static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002662 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2663 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2664 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2665 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2666 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2667 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2668 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2669
2670 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2671 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2672 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2673 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2674 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2675 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2676};
2677
2678static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2679{
2680 int err;
2681 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002682 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002683 struct net_device *dev;
2684 struct mesh_config cfg;
2685 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2686 struct nlattr *parent_attr;
2687
2688 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2689 if (!parent_attr)
2690 return -EINVAL;
2691 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2692 parent_attr, nl80211_meshconf_params_policy))
2693 return -EINVAL;
2694
Johannes Berg3b858752009-03-12 09:55:09 +01002695 rtnl_lock();
2696
Johannes Berg463d0182009-07-14 00:33:35 +02002697 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002698 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002699 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002700
Johannes Berg79c97e92009-07-07 03:56:12 +02002701 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002702 err = -EOPNOTSUPP;
2703 goto out;
2704 }
2705
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002706 /* This makes sure that there aren't more than 32 mesh config
2707 * parameters (otherwise our bitfield scheme would not work.) */
2708 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2709
2710 /* Fill in the params struct */
2711 mask = 0;
2712 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2713 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2715 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2716 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2717 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2718 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2719 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2720 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2721 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2723 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2724 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2725 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2727 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2728 nla_get_u8);
2729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2730 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2732 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2733 nla_get_u16);
2734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2735 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2736 nla_get_u32);
2737 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2738 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2739 nla_get_u16);
2740 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2741 dot11MeshHWMPnetDiameterTraversalTime,
2742 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2743 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002744 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2745 dot11MeshHWMPRootMode, mask,
2746 NL80211_MESHCONF_HWMP_ROOTMODE,
2747 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002748
2749 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002750 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002751
Jouni Malinenf3f92582009-03-20 17:57:36 +02002752 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002753 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002754 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002755 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002756 out_rtnl:
2757 rtnl_unlock();
2758
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002759 return err;
2760}
2761
2762#undef FILL_IN_MESH_PARAM_IF_SET
2763
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002764static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2765{
2766 struct sk_buff *msg;
2767 void *hdr = NULL;
2768 struct nlattr *nl_reg_rules;
2769 unsigned int i;
2770 int err = -EINVAL;
2771
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002772 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002773
2774 if (!cfg80211_regdomain)
2775 goto out;
2776
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002777 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002778 if (!msg) {
2779 err = -ENOBUFS;
2780 goto out;
2781 }
2782
2783 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2784 NL80211_CMD_GET_REG);
2785 if (!hdr)
2786 goto nla_put_failure;
2787
2788 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2789 cfg80211_regdomain->alpha2);
2790
2791 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2792 if (!nl_reg_rules)
2793 goto nla_put_failure;
2794
2795 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2796 struct nlattr *nl_reg_rule;
2797 const struct ieee80211_reg_rule *reg_rule;
2798 const struct ieee80211_freq_range *freq_range;
2799 const struct ieee80211_power_rule *power_rule;
2800
2801 reg_rule = &cfg80211_regdomain->reg_rules[i];
2802 freq_range = &reg_rule->freq_range;
2803 power_rule = &reg_rule->power_rule;
2804
2805 nl_reg_rule = nla_nest_start(msg, i);
2806 if (!nl_reg_rule)
2807 goto nla_put_failure;
2808
2809 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2810 reg_rule->flags);
2811 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2812 freq_range->start_freq_khz);
2813 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2814 freq_range->end_freq_khz);
2815 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2816 freq_range->max_bandwidth_khz);
2817 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2818 power_rule->max_antenna_gain);
2819 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2820 power_rule->max_eirp);
2821
2822 nla_nest_end(msg, nl_reg_rule);
2823 }
2824
2825 nla_nest_end(msg, nl_reg_rules);
2826
2827 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002828 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002829 goto out;
2830
2831nla_put_failure:
2832 genlmsg_cancel(msg, hdr);
2833 err = -EMSGSIZE;
2834out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002835 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002836 return err;
2837}
2838
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002839static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2840{
2841 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2842 struct nlattr *nl_reg_rule;
2843 char *alpha2 = NULL;
2844 int rem_reg_rules = 0, r = 0;
2845 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2846 struct ieee80211_regdomain *rd = NULL;
2847
2848 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2849 return -EINVAL;
2850
2851 if (!info->attrs[NL80211_ATTR_REG_RULES])
2852 return -EINVAL;
2853
2854 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2855
2856 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2857 rem_reg_rules) {
2858 num_rules++;
2859 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002860 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002861 }
2862
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002863 mutex_lock(&cfg80211_mutex);
2864
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002865 if (!reg_is_valid_request(alpha2)) {
2866 r = -EINVAL;
2867 goto bad_reg;
2868 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002869
2870 size_of_regd = sizeof(struct ieee80211_regdomain) +
2871 (num_rules * sizeof(struct ieee80211_reg_rule));
2872
2873 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002874 if (!rd) {
2875 r = -ENOMEM;
2876 goto bad_reg;
2877 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002878
2879 rd->n_reg_rules = num_rules;
2880 rd->alpha2[0] = alpha2[0];
2881 rd->alpha2[1] = alpha2[1];
2882
2883 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2884 rem_reg_rules) {
2885 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2886 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2887 reg_rule_policy);
2888 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2889 if (r)
2890 goto bad_reg;
2891
2892 rule_idx++;
2893
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002894 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2895 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002896 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002897 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002898 }
2899
2900 BUG_ON(rule_idx != num_rules);
2901
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002902 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002903
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002904 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002905
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002906 return r;
2907
Johannes Bergd2372b32008-10-24 20:32:20 +02002908 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002909 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002910 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002911 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002912}
2913
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002914static int validate_scan_freqs(struct nlattr *freqs)
2915{
2916 struct nlattr *attr1, *attr2;
2917 int n_channels = 0, tmp1, tmp2;
2918
2919 nla_for_each_nested(attr1, freqs, tmp1) {
2920 n_channels++;
2921 /*
2922 * Some hardware has a limited channel list for
2923 * scanning, and it is pretty much nonsensical
2924 * to scan for a channel twice, so disallow that
2925 * and don't require drivers to check that the
2926 * channel list they get isn't longer than what
2927 * they can scan, as long as they can scan all
2928 * the channels they registered at once.
2929 */
2930 nla_for_each_nested(attr2, freqs, tmp2)
2931 if (attr1 != attr2 &&
2932 nla_get_u32(attr1) == nla_get_u32(attr2))
2933 return 0;
2934 }
2935
2936 return n_channels;
2937}
2938
Johannes Berg2a519312009-02-10 21:25:55 +01002939static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2940{
Johannes Berg79c97e92009-07-07 03:56:12 +02002941 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002942 struct net_device *dev;
2943 struct cfg80211_scan_request *request;
2944 struct cfg80211_ssid *ssid;
2945 struct ieee80211_channel *channel;
2946 struct nlattr *attr;
2947 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002948 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002949 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002950 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002951
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002952 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2953 return -EINVAL;
2954
Johannes Berg3b858752009-03-12 09:55:09 +01002955 rtnl_lock();
2956
Johannes Berg463d0182009-07-14 00:33:35 +02002957 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002958 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002959 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002960
Johannes Berg79c97e92009-07-07 03:56:12 +02002961 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002962
Johannes Berg79c97e92009-07-07 03:56:12 +02002963 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002964 err = -EOPNOTSUPP;
2965 goto out;
2966 }
2967
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002968 if (!netif_running(dev)) {
2969 err = -ENETDOWN;
2970 goto out;
2971 }
2972
Johannes Berg79c97e92009-07-07 03:56:12 +02002973 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002974 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002975 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002976 }
2977
2978 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002979 n_channels = validate_scan_freqs(
2980 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002981 if (!n_channels) {
2982 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002983 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002984 }
2985 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002986 n_channels = 0;
2987
Johannes Berg2a519312009-02-10 21:25:55 +01002988 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2989 if (wiphy->bands[band])
2990 n_channels += wiphy->bands[band]->n_channels;
2991 }
2992
2993 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2994 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2995 n_ssids++;
2996
2997 if (n_ssids > wiphy->max_scan_ssids) {
2998 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002999 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003000 }
3001
Jouni Malinen70692ad2009-02-16 19:39:13 +02003002 if (info->attrs[NL80211_ATTR_IE])
3003 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3004 else
3005 ie_len = 0;
3006
Johannes Berg18a83652009-03-31 12:12:05 +02003007 if (ie_len > wiphy->max_scan_ie_len) {
3008 err = -EINVAL;
3009 goto out;
3010 }
3011
Johannes Berg2a519312009-02-10 21:25:55 +01003012 request = kzalloc(sizeof(*request)
3013 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003014 + sizeof(channel) * n_channels
3015 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003016 if (!request) {
3017 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003018 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003019 }
3020
Johannes Berg2a519312009-02-10 21:25:55 +01003021 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003022 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003023 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003024 if (ie_len) {
3025 if (request->ssids)
3026 request->ie = (void *)(request->ssids + n_ssids);
3027 else
3028 request->ie = (void *)(request->channels + n_channels);
3029 }
Johannes Berg2a519312009-02-10 21:25:55 +01003030
Johannes Berg584991d2009-11-02 13:32:03 +01003031 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003032 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3033 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003034 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003035 struct ieee80211_channel *chan;
3036
3037 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3038
3039 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003040 err = -EINVAL;
3041 goto out_free;
3042 }
Johannes Berg584991d2009-11-02 13:32:03 +01003043
3044 /* ignore disabled channels */
3045 if (chan->flags & IEEE80211_CHAN_DISABLED)
3046 continue;
3047
3048 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003049 i++;
3050 }
3051 } else {
3052 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003053 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3054 int j;
3055 if (!wiphy->bands[band])
3056 continue;
3057 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003058 struct ieee80211_channel *chan;
3059
3060 chan = &wiphy->bands[band]->channels[j];
3061
3062 if (chan->flags & IEEE80211_CHAN_DISABLED)
3063 continue;
3064
3065 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003066 i++;
3067 }
3068 }
3069 }
3070
Johannes Berg584991d2009-11-02 13:32:03 +01003071 if (!i) {
3072 err = -EINVAL;
3073 goto out_free;
3074 }
3075
3076 request->n_channels = i;
3077
Johannes Berg2a519312009-02-10 21:25:55 +01003078 i = 0;
3079 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3080 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3081 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3082 err = -EINVAL;
3083 goto out_free;
3084 }
3085 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3086 request->ssids[i].ssid_len = nla_len(attr);
3087 i++;
3088 }
3089 }
3090
Jouni Malinen70692ad2009-02-16 19:39:13 +02003091 if (info->attrs[NL80211_ATTR_IE]) {
3092 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a54b2009-04-01 11:58:36 +02003093 memcpy((void *)request->ie,
3094 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003095 request->ie_len);
3096 }
3097
Johannes Berg463d0182009-07-14 00:33:35 +02003098 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003099 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003100
Johannes Berg79c97e92009-07-07 03:56:12 +02003101 rdev->scan_req = request;
3102 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003103
Johannes Berg463d0182009-07-14 00:33:35 +02003104 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003105 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003106 dev_hold(dev);
3107 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003108
Johannes Berg2a519312009-02-10 21:25:55 +01003109 out_free:
3110 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003111 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003112 kfree(request);
3113 }
Johannes Berg2a519312009-02-10 21:25:55 +01003114 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003115 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003116 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003117 out_rtnl:
3118 rtnl_unlock();
3119
Johannes Berg2a519312009-02-10 21:25:55 +01003120 return err;
3121}
3122
3123static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3124 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003125 struct wireless_dev *wdev,
3126 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003127{
Johannes Berg48ab9052009-07-10 18:42:31 +02003128 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003129 void *hdr;
3130 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003131 int i;
3132
3133 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003134
3135 hdr = nl80211hdr_put(msg, pid, seq, flags,
3136 NL80211_CMD_NEW_SCAN_RESULTS);
3137 if (!hdr)
3138 return -1;
3139
Johannes Bergf5ea9122009-08-07 16:17:38 +02003140 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003141 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003142
3143 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3144 if (!bss)
3145 goto nla_put_failure;
3146 if (!is_zero_ether_addr(res->bssid))
3147 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3148 if (res->information_elements && res->len_information_elements)
3149 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3150 res->len_information_elements,
3151 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003152 if (res->beacon_ies && res->len_beacon_ies &&
3153 res->beacon_ies != res->information_elements)
3154 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3155 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003156 if (res->tsf)
3157 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3158 if (res->beacon_interval)
3159 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3160 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3161 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003162 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3163 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003164
Johannes Berg77965c972009-02-18 18:45:06 +01003165 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003166 case CFG80211_SIGNAL_TYPE_MBM:
3167 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3168 break;
3169 case CFG80211_SIGNAL_TYPE_UNSPEC:
3170 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3171 break;
3172 default:
3173 break;
3174 }
3175
Johannes Berg48ab9052009-07-10 18:42:31 +02003176 switch (wdev->iftype) {
3177 case NL80211_IFTYPE_STATION:
3178 if (intbss == wdev->current_bss)
3179 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3180 NL80211_BSS_STATUS_ASSOCIATED);
3181 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3182 if (intbss != wdev->auth_bsses[i])
3183 continue;
3184 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3185 NL80211_BSS_STATUS_AUTHENTICATED);
3186 break;
3187 }
3188 break;
3189 case NL80211_IFTYPE_ADHOC:
3190 if (intbss == wdev->current_bss)
3191 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3192 NL80211_BSS_STATUS_IBSS_JOINED);
3193 break;
3194 default:
3195 break;
3196 }
3197
Johannes Berg2a519312009-02-10 21:25:55 +01003198 nla_nest_end(msg, bss);
3199
3200 return genlmsg_end(msg, hdr);
3201
3202 nla_put_failure:
3203 genlmsg_cancel(msg, hdr);
3204 return -EMSGSIZE;
3205}
3206
3207static int nl80211_dump_scan(struct sk_buff *skb,
3208 struct netlink_callback *cb)
3209{
Johannes Berg48ab9052009-07-10 18:42:31 +02003210 struct cfg80211_registered_device *rdev;
3211 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003212 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003213 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003214 int ifidx = cb->args[0];
3215 int start = cb->args[1], idx = 0;
3216 int err;
3217
Holger Schuriga0438972009-11-11 11:30:02 +01003218 if (!ifidx)
3219 ifidx = nl80211_get_ifidx(cb);
3220 if (ifidx < 0)
3221 return ifidx;
3222 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003223
Johannes Berg463d0182009-07-14 00:33:35 +02003224 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003225 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003226 return -ENODEV;
3227
Johannes Berg463d0182009-07-14 00:33:35 +02003228 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003229 if (IS_ERR(rdev)) {
3230 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003231 goto out_put_netdev;
3232 }
3233
Johannes Berg48ab9052009-07-10 18:42:31 +02003234 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003235
Johannes Berg48ab9052009-07-10 18:42:31 +02003236 wdev_lock(wdev);
3237 spin_lock_bh(&rdev->bss_lock);
3238 cfg80211_bss_expire(rdev);
3239
3240 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003241 if (++idx <= start)
3242 continue;
3243 if (nl80211_send_bss(skb,
3244 NETLINK_CB(cb->skb).pid,
3245 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003246 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003247 idx--;
3248 goto out;
3249 }
3250 }
3251
3252 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003253 spin_unlock_bh(&rdev->bss_lock);
3254 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003255
3256 cb->args[1] = idx;
3257 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003258 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003259 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003260 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003261
3262 return err;
3263}
3264
Holger Schurig61fa7132009-11-11 12:25:40 +01003265static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3266 int flags, struct net_device *dev,
3267 struct survey_info *survey)
3268{
3269 void *hdr;
3270 struct nlattr *infoattr;
3271
3272 /* Survey without a channel doesn't make sense */
3273 if (!survey->channel)
3274 return -EINVAL;
3275
3276 hdr = nl80211hdr_put(msg, pid, seq, flags,
3277 NL80211_CMD_NEW_SURVEY_RESULTS);
3278 if (!hdr)
3279 return -ENOMEM;
3280
3281 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3282
3283 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3284 if (!infoattr)
3285 goto nla_put_failure;
3286
3287 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3288 survey->channel->center_freq);
3289 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3290 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3291 survey->noise);
3292
3293 nla_nest_end(msg, infoattr);
3294
3295 return genlmsg_end(msg, hdr);
3296
3297 nla_put_failure:
3298 genlmsg_cancel(msg, hdr);
3299 return -EMSGSIZE;
3300}
3301
3302static int nl80211_dump_survey(struct sk_buff *skb,
3303 struct netlink_callback *cb)
3304{
3305 struct survey_info survey;
3306 struct cfg80211_registered_device *dev;
3307 struct net_device *netdev;
3308 int ifidx = cb->args[0];
3309 int survey_idx = cb->args[1];
3310 int res;
3311
3312 if (!ifidx)
3313 ifidx = nl80211_get_ifidx(cb);
3314 if (ifidx < 0)
3315 return ifidx;
3316 cb->args[0] = ifidx;
3317
3318 rtnl_lock();
3319
3320 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3321 if (!netdev) {
3322 res = -ENODEV;
3323 goto out_rtnl;
3324 }
3325
3326 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3327 if (IS_ERR(dev)) {
3328 res = PTR_ERR(dev);
3329 goto out_rtnl;
3330 }
3331
3332 if (!dev->ops->dump_survey) {
3333 res = -EOPNOTSUPP;
3334 goto out_err;
3335 }
3336
3337 while (1) {
3338 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3339 &survey);
3340 if (res == -ENOENT)
3341 break;
3342 if (res)
3343 goto out_err;
3344
3345 if (nl80211_send_survey(skb,
3346 NETLINK_CB(cb->skb).pid,
3347 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3348 netdev,
3349 &survey) < 0)
3350 goto out;
3351 survey_idx++;
3352 }
3353
3354 out:
3355 cb->args[1] = survey_idx;
3356 res = skb->len;
3357 out_err:
3358 cfg80211_unlock_rdev(dev);
3359 out_rtnl:
3360 rtnl_unlock();
3361
3362 return res;
3363}
3364
Jouni Malinen255e7372009-03-20 21:21:17 +02003365static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3366{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003367 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003368}
3369
Samuel Ortizb23aa672009-07-01 21:26:54 +02003370static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3371{
3372 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3373 NL80211_WPA_VERSION_2));
3374}
3375
3376static bool nl80211_valid_akm_suite(u32 akm)
3377{
3378 return akm == WLAN_AKM_SUITE_8021X ||
3379 akm == WLAN_AKM_SUITE_PSK;
3380}
3381
3382static bool nl80211_valid_cipher_suite(u32 cipher)
3383{
3384 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3385 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3386 cipher == WLAN_CIPHER_SUITE_TKIP ||
3387 cipher == WLAN_CIPHER_SUITE_CCMP ||
3388 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3389}
3390
3391
Jouni Malinen636a5d32009-03-19 13:39:22 +02003392static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3393{
Johannes Berg79c97e92009-07-07 03:56:12 +02003394 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003395 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003396 struct ieee80211_channel *chan;
3397 const u8 *bssid, *ssid, *ie = NULL;
3398 int err, ssid_len, ie_len = 0;
3399 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003400 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003401 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003402
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003403 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3404 return -EINVAL;
3405
3406 if (!info->attrs[NL80211_ATTR_MAC])
3407 return -EINVAL;
3408
Jouni Malinen17780922009-03-27 20:52:47 +02003409 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3410 return -EINVAL;
3411
Johannes Berg19957bb2009-07-02 17:20:43 +02003412 if (!info->attrs[NL80211_ATTR_SSID])
3413 return -EINVAL;
3414
3415 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3416 return -EINVAL;
3417
Johannes Bergfffd0932009-07-08 14:22:54 +02003418 err = nl80211_parse_key(info, &key);
3419 if (err)
3420 return err;
3421
3422 if (key.idx >= 0) {
3423 if (!key.p.key || !key.p.key_len)
3424 return -EINVAL;
3425 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3426 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3427 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3428 key.p.key_len != WLAN_KEY_LEN_WEP104))
3429 return -EINVAL;
3430 if (key.idx > 4)
3431 return -EINVAL;
3432 } else {
3433 key.p.key_len = 0;
3434 key.p.key = NULL;
3435 }
3436
Jouni Malinen636a5d32009-03-19 13:39:22 +02003437 rtnl_lock();
3438
Johannes Berg463d0182009-07-14 00:33:35 +02003439 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003440 if (err)
3441 goto unlock_rtnl;
3442
Johannes Berg79c97e92009-07-07 03:56:12 +02003443 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003444 err = -EOPNOTSUPP;
3445 goto out;
3446 }
3447
Jouni Malineneec60b02009-03-20 21:21:19 +02003448 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3449 err = -EOPNOTSUPP;
3450 goto out;
3451 }
3452
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003453 if (!netif_running(dev)) {
3454 err = -ENETDOWN;
3455 goto out;
3456 }
3457
Johannes Berg19957bb2009-07-02 17:20:43 +02003458 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003459 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003460 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3461 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003462 err = -EINVAL;
3463 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003464 }
3465
Johannes Berg19957bb2009-07-02 17:20:43 +02003466 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3467 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3468
3469 if (info->attrs[NL80211_ATTR_IE]) {
3470 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3471 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3472 }
3473
3474 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3475 if (!nl80211_valid_auth_type(auth_type)) {
3476 err = -EINVAL;
3477 goto out;
3478 }
3479
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003480 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3481
Johannes Berg79c97e92009-07-07 03:56:12 +02003482 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003483 ssid, ssid_len, ie, ie_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003484 key.p.key, key.p.key_len, key.idx,
3485 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003486
3487out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003488 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003489 dev_put(dev);
3490unlock_rtnl:
3491 rtnl_unlock();
3492 return err;
3493}
3494
Samuel Ortizb23aa672009-07-01 21:26:54 +02003495static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003496 struct cfg80211_crypto_settings *settings,
3497 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003498{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003499 memset(settings, 0, sizeof(*settings));
3500
Samuel Ortizb23aa672009-07-01 21:26:54 +02003501 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3502
3503 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3504 void *data;
3505 int len, i;
3506
3507 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3508 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3509 settings->n_ciphers_pairwise = len / sizeof(u32);
3510
3511 if (len % sizeof(u32))
3512 return -EINVAL;
3513
Johannes Berg3dc27d22009-07-02 21:36:37 +02003514 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003515 return -EINVAL;
3516
3517 memcpy(settings->ciphers_pairwise, data, len);
3518
3519 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3520 if (!nl80211_valid_cipher_suite(
3521 settings->ciphers_pairwise[i]))
3522 return -EINVAL;
3523 }
3524
3525 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3526 settings->cipher_group =
3527 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3528 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3529 return -EINVAL;
3530 }
3531
3532 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3533 settings->wpa_versions =
3534 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3535 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3536 return -EINVAL;
3537 }
3538
3539 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3540 void *data;
3541 int len, i;
3542
3543 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3544 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3545 settings->n_akm_suites = len / sizeof(u32);
3546
3547 if (len % sizeof(u32))
3548 return -EINVAL;
3549
3550 memcpy(settings->akm_suites, data, len);
3551
3552 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3553 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3554 return -EINVAL;
3555 }
3556
3557 return 0;
3558}
3559
Jouni Malinen636a5d32009-03-19 13:39:22 +02003560static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3561{
Johannes Berg19957bb2009-07-02 17:20:43 +02003562 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003563 struct net_device *dev;
Jouni Malinen27e310c2010-01-19 10:53:30 -08003564 struct wireless_dev *wdev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003565 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003566 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003567 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003568 int err, ssid_len, ie_len = 0;
3569 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003570
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003571 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3572 return -EINVAL;
3573
3574 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003575 !info->attrs[NL80211_ATTR_SSID] ||
3576 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003577 return -EINVAL;
3578
Jouni Malinen636a5d32009-03-19 13:39:22 +02003579 rtnl_lock();
3580
Johannes Berg463d0182009-07-14 00:33:35 +02003581 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003582 if (err)
3583 goto unlock_rtnl;
3584
Johannes Berg19957bb2009-07-02 17:20:43 +02003585 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003586 err = -EOPNOTSUPP;
3587 goto out;
3588 }
3589
Jouni Malineneec60b02009-03-20 21:21:19 +02003590 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3591 err = -EOPNOTSUPP;
3592 goto out;
3593 }
3594
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003595 if (!netif_running(dev)) {
3596 err = -ENETDOWN;
3597 goto out;
3598 }
3599
Johannes Berg19957bb2009-07-02 17:20:43 +02003600 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003601
Johannes Berg19957bb2009-07-02 17:20:43 +02003602 chan = ieee80211_get_channel(&rdev->wiphy,
3603 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3604 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3605 err = -EINVAL;
3606 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003607 }
3608
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003609 mutex_lock(&rdev->devlist_mtx);
Jouni Malinen27e310c2010-01-19 10:53:30 -08003610 wdev = dev->ieee80211_ptr;
3611 fixedchan = rdev_fixed_channel(rdev, wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003612 if (fixedchan && chan != fixedchan) {
3613 err = -EBUSY;
3614 mutex_unlock(&rdev->devlist_mtx);
3615 goto out;
3616 }
3617 mutex_unlock(&rdev->devlist_mtx);
3618
Johannes Berg19957bb2009-07-02 17:20:43 +02003619 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3620 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003621
3622 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003623 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3624 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003625 }
3626
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003627 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003628 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003629 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003630 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003631 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003632 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003633 err = -EINVAL;
3634 goto out;
3635 }
3636 }
3637
Johannes Berg3e5d7642009-07-07 14:37:26 +02003638 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3639 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3640
Johannes Berg3dc27d22009-07-02 21:36:37 +02003641 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003642 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003643 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3644 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003645 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003646
3647out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003648 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003649 dev_put(dev);
3650unlock_rtnl:
3651 rtnl_unlock();
3652 return err;
3653}
3654
3655static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3656{
Johannes Berg79c97e92009-07-07 03:56:12 +02003657 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003658 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003659 const u8 *ie = NULL, *bssid;
3660 int err, ie_len = 0;
3661 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003662 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003663
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003664 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3665 return -EINVAL;
3666
3667 if (!info->attrs[NL80211_ATTR_MAC])
3668 return -EINVAL;
3669
3670 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3671 return -EINVAL;
3672
Jouni Malinen636a5d32009-03-19 13:39:22 +02003673 rtnl_lock();
3674
Johannes Berg463d0182009-07-14 00:33:35 +02003675 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003676 if (err)
3677 goto unlock_rtnl;
3678
Johannes Berg79c97e92009-07-07 03:56:12 +02003679 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003680 err = -EOPNOTSUPP;
3681 goto out;
3682 }
3683
Jouni Malineneec60b02009-03-20 21:21:19 +02003684 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3685 err = -EOPNOTSUPP;
3686 goto out;
3687 }
3688
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003689 if (!netif_running(dev)) {
3690 err = -ENETDOWN;
3691 goto out;
3692 }
3693
Johannes Berg19957bb2009-07-02 17:20:43 +02003694 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003695
Johannes Berg19957bb2009-07-02 17:20:43 +02003696 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3697 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003698 /* Reason Code 0 is reserved */
3699 err = -EINVAL;
3700 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003701 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003702
3703 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003704 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3705 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003706 }
3707
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003708 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3709
3710 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3711 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003712
3713out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003714 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003715 dev_put(dev);
3716unlock_rtnl:
3717 rtnl_unlock();
3718 return err;
3719}
3720
3721static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3722{
Johannes Berg79c97e92009-07-07 03:56:12 +02003723 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003724 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003725 const u8 *ie = NULL, *bssid;
3726 int err, ie_len = 0;
3727 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003728 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003729
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003730 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3731 return -EINVAL;
3732
3733 if (!info->attrs[NL80211_ATTR_MAC])
3734 return -EINVAL;
3735
3736 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3737 return -EINVAL;
3738
Jouni Malinen636a5d32009-03-19 13:39:22 +02003739 rtnl_lock();
3740
Johannes Berg463d0182009-07-14 00:33:35 +02003741 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003742 if (err)
3743 goto unlock_rtnl;
3744
Johannes Berg79c97e92009-07-07 03:56:12 +02003745 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003746 err = -EOPNOTSUPP;
3747 goto out;
3748 }
3749
Jouni Malineneec60b02009-03-20 21:21:19 +02003750 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3751 err = -EOPNOTSUPP;
3752 goto out;
3753 }
3754
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003755 if (!netif_running(dev)) {
3756 err = -ENETDOWN;
3757 goto out;
3758 }
3759
Johannes Berg19957bb2009-07-02 17:20:43 +02003760 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003761
Johannes Berg19957bb2009-07-02 17:20:43 +02003762 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3763 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003764 /* Reason Code 0 is reserved */
3765 err = -EINVAL;
3766 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003767 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003768
3769 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003770 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3771 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003772 }
3773
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003774 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3775
3776 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3777 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003778
3779out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003780 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003781 dev_put(dev);
3782unlock_rtnl:
3783 rtnl_unlock();
3784 return err;
3785}
3786
Johannes Berg04a773a2009-04-19 21:24:32 +02003787static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3788{
Johannes Berg79c97e92009-07-07 03:56:12 +02003789 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003790 struct net_device *dev;
3791 struct cfg80211_ibss_params ibss;
3792 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003793 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003794 int err;
3795
Johannes Berg8e30bc52009-04-22 17:45:38 +02003796 memset(&ibss, 0, sizeof(ibss));
3797
Johannes Berg04a773a2009-04-19 21:24:32 +02003798 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3799 return -EINVAL;
3800
3801 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3802 !info->attrs[NL80211_ATTR_SSID] ||
3803 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3804 return -EINVAL;
3805
Johannes Berg8e30bc52009-04-22 17:45:38 +02003806 ibss.beacon_interval = 100;
3807
3808 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3809 ibss.beacon_interval =
3810 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3811 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3812 return -EINVAL;
3813 }
3814
Johannes Berg04a773a2009-04-19 21:24:32 +02003815 rtnl_lock();
3816
Johannes Berg463d0182009-07-14 00:33:35 +02003817 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003818 if (err)
3819 goto unlock_rtnl;
3820
Johannes Berg79c97e92009-07-07 03:56:12 +02003821 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003822 err = -EOPNOTSUPP;
3823 goto out;
3824 }
3825
3826 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3827 err = -EOPNOTSUPP;
3828 goto out;
3829 }
3830
3831 if (!netif_running(dev)) {
3832 err = -ENETDOWN;
3833 goto out;
3834 }
3835
Johannes Berg79c97e92009-07-07 03:56:12 +02003836 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003837
3838 if (info->attrs[NL80211_ATTR_MAC])
3839 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3840 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3841 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3842
3843 if (info->attrs[NL80211_ATTR_IE]) {
3844 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3845 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3846 }
3847
3848 ibss.channel = ieee80211_get_channel(wiphy,
3849 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3850 if (!ibss.channel ||
3851 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3852 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3853 err = -EINVAL;
3854 goto out;
3855 }
3856
3857 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003858 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003859
Johannes Bergfffd0932009-07-08 14:22:54 +02003860 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3861 connkeys = nl80211_parse_connkeys(rdev,
3862 info->attrs[NL80211_ATTR_KEYS]);
3863 if (IS_ERR(connkeys)) {
3864 err = PTR_ERR(connkeys);
3865 connkeys = NULL;
3866 goto out;
3867 }
3868 }
3869
3870 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003871
3872out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003873 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003874 dev_put(dev);
3875unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003876 if (err)
3877 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003878 rtnl_unlock();
3879 return err;
3880}
3881
3882static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3883{
Johannes Berg79c97e92009-07-07 03:56:12 +02003884 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003885 struct net_device *dev;
3886 int err;
3887
3888 rtnl_lock();
3889
Johannes Berg463d0182009-07-14 00:33:35 +02003890 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003891 if (err)
3892 goto unlock_rtnl;
3893
Johannes Berg79c97e92009-07-07 03:56:12 +02003894 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003895 err = -EOPNOTSUPP;
3896 goto out;
3897 }
3898
3899 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3900 err = -EOPNOTSUPP;
3901 goto out;
3902 }
3903
3904 if (!netif_running(dev)) {
3905 err = -ENETDOWN;
3906 goto out;
3907 }
3908
Johannes Berg79c97e92009-07-07 03:56:12 +02003909 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003910
3911out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003912 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003913 dev_put(dev);
3914unlock_rtnl:
3915 rtnl_unlock();
3916 return err;
3917}
3918
Johannes Bergaff89a92009-07-01 21:26:51 +02003919#ifdef CONFIG_NL80211_TESTMODE
3920static struct genl_multicast_group nl80211_testmode_mcgrp = {
3921 .name = "testmode",
3922};
3923
3924static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3925{
3926 struct cfg80211_registered_device *rdev;
3927 int err;
3928
3929 if (!info->attrs[NL80211_ATTR_TESTDATA])
3930 return -EINVAL;
3931
3932 rtnl_lock();
3933
3934 rdev = cfg80211_get_dev_from_info(info);
3935 if (IS_ERR(rdev)) {
3936 err = PTR_ERR(rdev);
3937 goto unlock_rtnl;
3938 }
3939
3940 err = -EOPNOTSUPP;
3941 if (rdev->ops->testmode_cmd) {
3942 rdev->testmode_info = info;
3943 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3944 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3945 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3946 rdev->testmode_info = NULL;
3947 }
3948
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003949 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003950
3951 unlock_rtnl:
3952 rtnl_unlock();
3953 return err;
3954}
3955
3956static struct sk_buff *
3957__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3958 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3959{
3960 struct sk_buff *skb;
3961 void *hdr;
3962 struct nlattr *data;
3963
3964 skb = nlmsg_new(approxlen + 100, gfp);
3965 if (!skb)
3966 return NULL;
3967
3968 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3969 if (!hdr) {
3970 kfree_skb(skb);
3971 return NULL;
3972 }
3973
3974 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3975 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3976
3977 ((void **)skb->cb)[0] = rdev;
3978 ((void **)skb->cb)[1] = hdr;
3979 ((void **)skb->cb)[2] = data;
3980
3981 return skb;
3982
3983 nla_put_failure:
3984 kfree_skb(skb);
3985 return NULL;
3986}
3987
3988struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3989 int approxlen)
3990{
3991 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3992
3993 if (WARN_ON(!rdev->testmode_info))
3994 return NULL;
3995
3996 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3997 rdev->testmode_info->snd_pid,
3998 rdev->testmode_info->snd_seq,
3999 GFP_KERNEL);
4000}
4001EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4002
4003int cfg80211_testmode_reply(struct sk_buff *skb)
4004{
4005 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4006 void *hdr = ((void **)skb->cb)[1];
4007 struct nlattr *data = ((void **)skb->cb)[2];
4008
4009 if (WARN_ON(!rdev->testmode_info)) {
4010 kfree_skb(skb);
4011 return -EINVAL;
4012 }
4013
4014 nla_nest_end(skb, data);
4015 genlmsg_end(skb, hdr);
4016 return genlmsg_reply(skb, rdev->testmode_info);
4017}
4018EXPORT_SYMBOL(cfg80211_testmode_reply);
4019
4020struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4021 int approxlen, gfp_t gfp)
4022{
4023 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4024
4025 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4026}
4027EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4028
4029void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4030{
4031 void *hdr = ((void **)skb->cb)[1];
4032 struct nlattr *data = ((void **)skb->cb)[2];
4033
4034 nla_nest_end(skb, data);
4035 genlmsg_end(skb, hdr);
4036 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4037}
4038EXPORT_SYMBOL(cfg80211_testmode_event);
4039#endif
4040
Samuel Ortizb23aa672009-07-01 21:26:54 +02004041static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4042{
Johannes Berg79c97e92009-07-07 03:56:12 +02004043 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004044 struct net_device *dev;
4045 struct cfg80211_connect_params connect;
4046 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004047 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004048 int err;
4049
4050 memset(&connect, 0, sizeof(connect));
4051
4052 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4053 return -EINVAL;
4054
4055 if (!info->attrs[NL80211_ATTR_SSID] ||
4056 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4057 return -EINVAL;
4058
4059 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4060 connect.auth_type =
4061 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4062 if (!nl80211_valid_auth_type(connect.auth_type))
4063 return -EINVAL;
4064 } else
4065 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4066
4067 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4068
Johannes Berg3dc27d22009-07-02 21:36:37 +02004069 err = nl80211_crypto_settings(info, &connect.crypto,
4070 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004071 if (err)
4072 return err;
4073 rtnl_lock();
4074
Johannes Berg463d0182009-07-14 00:33:35 +02004075 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004076 if (err)
4077 goto unlock_rtnl;
4078
4079 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4080 err = -EOPNOTSUPP;
4081 goto out;
4082 }
4083
4084 if (!netif_running(dev)) {
4085 err = -ENETDOWN;
4086 goto out;
4087 }
4088
Johannes Berg79c97e92009-07-07 03:56:12 +02004089 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004090
Samuel Ortizb23aa672009-07-01 21:26:54 +02004091 if (info->attrs[NL80211_ATTR_MAC])
4092 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4093 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4094 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4095
4096 if (info->attrs[NL80211_ATTR_IE]) {
4097 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4098 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4099 }
4100
4101 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4102 connect.channel =
4103 ieee80211_get_channel(wiphy,
4104 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4105 if (!connect.channel ||
4106 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4107 err = -EINVAL;
4108 goto out;
4109 }
4110 }
4111
Johannes Bergfffd0932009-07-08 14:22:54 +02004112 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4113 connkeys = nl80211_parse_connkeys(rdev,
4114 info->attrs[NL80211_ATTR_KEYS]);
4115 if (IS_ERR(connkeys)) {
4116 err = PTR_ERR(connkeys);
4117 connkeys = NULL;
4118 goto out;
4119 }
4120 }
4121
4122 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004123
4124out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004125 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004126 dev_put(dev);
4127unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004128 if (err)
4129 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004130 rtnl_unlock();
4131 return err;
4132}
4133
4134static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4135{
Johannes Berg79c97e92009-07-07 03:56:12 +02004136 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004137 struct net_device *dev;
4138 int err;
4139 u16 reason;
4140
4141 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4142 reason = WLAN_REASON_DEAUTH_LEAVING;
4143 else
4144 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4145
4146 if (reason == 0)
4147 return -EINVAL;
4148
4149 rtnl_lock();
4150
Johannes Berg463d0182009-07-14 00:33:35 +02004151 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004152 if (err)
4153 goto unlock_rtnl;
4154
4155 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4156 err = -EOPNOTSUPP;
4157 goto out;
4158 }
4159
4160 if (!netif_running(dev)) {
4161 err = -ENETDOWN;
4162 goto out;
4163 }
4164
Johannes Berg79c97e92009-07-07 03:56:12 +02004165 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004166
4167out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004168 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004169 dev_put(dev);
4170unlock_rtnl:
4171 rtnl_unlock();
4172 return err;
4173}
4174
Johannes Berg463d0182009-07-14 00:33:35 +02004175static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4176{
4177 struct cfg80211_registered_device *rdev;
4178 struct net *net;
4179 int err;
4180 u32 pid;
4181
4182 if (!info->attrs[NL80211_ATTR_PID])
4183 return -EINVAL;
4184
4185 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4186
4187 rtnl_lock();
4188
4189 rdev = cfg80211_get_dev_from_info(info);
4190 if (IS_ERR(rdev)) {
4191 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004192 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004193 }
4194
4195 net = get_net_ns_by_pid(pid);
4196 if (IS_ERR(net)) {
4197 err = PTR_ERR(net);
4198 goto out;
4199 }
4200
4201 err = 0;
4202
4203 /* check if anything to do */
4204 if (net_eq(wiphy_net(&rdev->wiphy), net))
4205 goto out_put_net;
4206
4207 err = cfg80211_switch_netns(rdev, net);
4208 out_put_net:
4209 put_net(net);
4210 out:
4211 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004212 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004213 rtnl_unlock();
4214 return err;
4215}
4216
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004217static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4218{
4219 struct cfg80211_registered_device *rdev;
4220 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4221 struct cfg80211_pmksa *pmksa) = NULL;
4222 int err;
4223 struct net_device *dev;
4224 struct cfg80211_pmksa pmksa;
4225
4226 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4227
4228 if (!info->attrs[NL80211_ATTR_MAC])
4229 return -EINVAL;
4230
4231 if (!info->attrs[NL80211_ATTR_PMKID])
4232 return -EINVAL;
4233
4234 rtnl_lock();
4235
4236 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4237 if (err)
4238 goto out_rtnl;
4239
4240 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4241 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4242
4243 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4244 err = -EOPNOTSUPP;
4245 goto out;
4246 }
4247
4248 switch (info->genlhdr->cmd) {
4249 case NL80211_CMD_SET_PMKSA:
4250 rdev_ops = rdev->ops->set_pmksa;
4251 break;
4252 case NL80211_CMD_DEL_PMKSA:
4253 rdev_ops = rdev->ops->del_pmksa;
4254 break;
4255 default:
4256 WARN_ON(1);
4257 break;
4258 }
4259
4260 if (!rdev_ops) {
4261 err = -EOPNOTSUPP;
4262 goto out;
4263 }
4264
4265 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4266
4267 out:
4268 cfg80211_unlock_rdev(rdev);
4269 dev_put(dev);
4270 out_rtnl:
4271 rtnl_unlock();
4272
4273 return err;
4274}
4275
4276static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4277{
4278 struct cfg80211_registered_device *rdev;
4279 int err;
4280 struct net_device *dev;
4281
4282 rtnl_lock();
4283
4284 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4285 if (err)
4286 goto out_rtnl;
4287
4288 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4289 err = -EOPNOTSUPP;
4290 goto out;
4291 }
4292
4293 if (!rdev->ops->flush_pmksa) {
4294 err = -EOPNOTSUPP;
4295 goto out;
4296 }
4297
4298 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4299
4300 out:
4301 cfg80211_unlock_rdev(rdev);
4302 dev_put(dev);
4303 out_rtnl:
4304 rtnl_unlock();
4305
4306 return err;
4307
4308}
4309
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004310static int nl80211_remain_on_channel(struct sk_buff *skb,
4311 struct genl_info *info)
4312{
4313 struct cfg80211_registered_device *rdev;
4314 struct net_device *dev;
4315 struct ieee80211_channel *chan;
4316 struct sk_buff *msg;
4317 void *hdr;
4318 u64 cookie;
4319 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4320 u32 freq, duration;
4321 int err;
4322
4323 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4324 !info->attrs[NL80211_ATTR_DURATION])
4325 return -EINVAL;
4326
4327 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4328
4329 /*
4330 * We should be on that channel for at least one jiffie,
4331 * and more than 5 seconds seems excessive.
4332 */
4333 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4334 return -EINVAL;
4335
4336 rtnl_lock();
4337
4338 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4339 if (err)
4340 goto unlock_rtnl;
4341
4342 if (!rdev->ops->remain_on_channel) {
4343 err = -EOPNOTSUPP;
4344 goto out;
4345 }
4346
4347 if (!netif_running(dev)) {
4348 err = -ENETDOWN;
4349 goto out;
4350 }
4351
4352 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4353 channel_type = nla_get_u32(
4354 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4355 if (channel_type != NL80211_CHAN_NO_HT &&
4356 channel_type != NL80211_CHAN_HT20 &&
4357 channel_type != NL80211_CHAN_HT40PLUS &&
4358 channel_type != NL80211_CHAN_HT40MINUS)
4359 err = -EINVAL;
4360 goto out;
4361 }
4362
4363 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4364 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4365 if (chan == NULL) {
4366 err = -EINVAL;
4367 goto out;
4368 }
4369
4370 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4371 if (!msg) {
4372 err = -ENOMEM;
4373 goto out;
4374 }
4375
4376 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4377 NL80211_CMD_REMAIN_ON_CHANNEL);
4378
4379 if (IS_ERR(hdr)) {
4380 err = PTR_ERR(hdr);
4381 goto free_msg;
4382 }
4383
4384 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4385 channel_type, duration, &cookie);
4386
4387 if (err)
4388 goto free_msg;
4389
4390 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4391
4392 genlmsg_end(msg, hdr);
4393 err = genlmsg_reply(msg, info);
4394 goto out;
4395
4396 nla_put_failure:
4397 err = -ENOBUFS;
4398 free_msg:
4399 nlmsg_free(msg);
4400 out:
4401 cfg80211_unlock_rdev(rdev);
4402 dev_put(dev);
4403 unlock_rtnl:
4404 rtnl_unlock();
4405 return err;
4406}
4407
4408static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4409 struct genl_info *info)
4410{
4411 struct cfg80211_registered_device *rdev;
4412 struct net_device *dev;
4413 u64 cookie;
4414 int err;
4415
4416 if (!info->attrs[NL80211_ATTR_COOKIE])
4417 return -EINVAL;
4418
4419 rtnl_lock();
4420
4421 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4422 if (err)
4423 goto unlock_rtnl;
4424
4425 if (!rdev->ops->cancel_remain_on_channel) {
4426 err = -EOPNOTSUPP;
4427 goto out;
4428 }
4429
4430 if (!netif_running(dev)) {
4431 err = -ENETDOWN;
4432 goto out;
4433 }
4434
4435 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4436
4437 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4438
4439 out:
4440 cfg80211_unlock_rdev(rdev);
4441 dev_put(dev);
4442 unlock_rtnl:
4443 rtnl_unlock();
4444 return err;
4445}
4446
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004447static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4448 u8 *rates, u8 rates_len)
4449{
4450 u8 i;
4451 u32 mask = 0;
4452
4453 for (i = 0; i < rates_len; i++) {
4454 int rate = (rates[i] & 0x7f) * 5;
4455 int ridx;
4456 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4457 struct ieee80211_rate *srate =
4458 &sband->bitrates[ridx];
4459 if (rate == srate->bitrate) {
4460 mask |= 1 << ridx;
4461 break;
4462 }
4463 }
4464 if (ridx == sband->n_bitrates)
4465 return 0; /* rate not found */
4466 }
4467
4468 return mask;
4469}
4470
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004471static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004472 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4473 .len = NL80211_MAX_SUPP_RATES },
4474};
4475
4476static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4477 struct genl_info *info)
4478{
4479 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4480 struct cfg80211_registered_device *rdev;
4481 struct cfg80211_bitrate_mask mask;
4482 int err, rem, i;
4483 struct net_device *dev;
4484 struct nlattr *tx_rates;
4485 struct ieee80211_supported_band *sband;
4486
4487 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4488 return -EINVAL;
4489
4490 rtnl_lock();
4491
4492 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4493 if (err)
4494 goto unlock_rtnl;
4495
4496 if (!rdev->ops->set_bitrate_mask) {
4497 err = -EOPNOTSUPP;
4498 goto unlock;
4499 }
4500
4501 memset(&mask, 0, sizeof(mask));
4502 /* Default to all rates enabled */
4503 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4504 sband = rdev->wiphy.bands[i];
4505 mask.control[i].legacy =
4506 sband ? (1 << sband->n_bitrates) - 1 : 0;
4507 }
4508
4509 /*
4510 * The nested attribute uses enum nl80211_band as the index. This maps
4511 * directly to the enum ieee80211_band values used in cfg80211.
4512 */
4513 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4514 {
4515 enum ieee80211_band band = nla_type(tx_rates);
4516 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4517 err = -EINVAL;
4518 goto unlock;
4519 }
4520 sband = rdev->wiphy.bands[band];
4521 if (sband == NULL) {
4522 err = -EINVAL;
4523 goto unlock;
4524 }
4525 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4526 nla_len(tx_rates), nl80211_txattr_policy);
4527 if (tb[NL80211_TXRATE_LEGACY]) {
4528 mask.control[band].legacy = rateset_to_mask(
4529 sband,
4530 nla_data(tb[NL80211_TXRATE_LEGACY]),
4531 nla_len(tb[NL80211_TXRATE_LEGACY]));
4532 if (mask.control[band].legacy == 0) {
4533 err = -EINVAL;
4534 goto unlock;
4535 }
4536 }
4537 }
4538
4539 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4540
4541 unlock:
4542 dev_put(dev);
4543 cfg80211_unlock_rdev(rdev);
4544 unlock_rtnl:
4545 rtnl_unlock();
4546 return err;
4547}
4548
Jouni Malinen026331c2010-02-15 12:53:10 +02004549static int nl80211_register_action(struct sk_buff *skb, struct genl_info *info)
4550{
4551 struct cfg80211_registered_device *rdev;
4552 struct net_device *dev;
4553 int err;
4554
4555 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4556 return -EINVAL;
4557
4558 if (nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]) < 1)
4559 return -EINVAL;
4560
4561 rtnl_lock();
4562
4563 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4564 if (err)
4565 goto unlock_rtnl;
4566
4567 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4568 err = -EOPNOTSUPP;
4569 goto out;
4570 }
4571
4572 /* not much point in registering if we can't reply */
4573 if (!rdev->ops->action) {
4574 err = -EOPNOTSUPP;
4575 goto out;
4576 }
4577
4578 err = cfg80211_mlme_register_action(dev->ieee80211_ptr, info->snd_pid,
4579 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4580 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4581 out:
4582 cfg80211_unlock_rdev(rdev);
4583 dev_put(dev);
4584 unlock_rtnl:
4585 rtnl_unlock();
4586 return err;
4587}
4588
4589static int nl80211_action(struct sk_buff *skb, struct genl_info *info)
4590{
4591 struct cfg80211_registered_device *rdev;
4592 struct net_device *dev;
4593 struct ieee80211_channel *chan;
4594 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4595 u32 freq;
4596 int err;
4597 void *hdr;
4598 u64 cookie;
4599 struct sk_buff *msg;
4600
4601 if (!info->attrs[NL80211_ATTR_FRAME] ||
4602 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4603 return -EINVAL;
4604
4605 rtnl_lock();
4606
4607 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4608 if (err)
4609 goto unlock_rtnl;
4610
4611 if (!rdev->ops->action) {
4612 err = -EOPNOTSUPP;
4613 goto out;
4614 }
4615
4616 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4617 err = -EOPNOTSUPP;
4618 goto out;
4619 }
4620
4621 if (!netif_running(dev)) {
4622 err = -ENETDOWN;
4623 goto out;
4624 }
4625
4626 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4627 channel_type = nla_get_u32(
4628 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4629 if (channel_type != NL80211_CHAN_NO_HT &&
4630 channel_type != NL80211_CHAN_HT20 &&
4631 channel_type != NL80211_CHAN_HT40PLUS &&
4632 channel_type != NL80211_CHAN_HT40MINUS)
4633 err = -EINVAL;
4634 goto out;
4635 }
4636
4637 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4638 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4639 if (chan == NULL) {
4640 err = -EINVAL;
4641 goto out;
4642 }
4643
4644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4645 if (!msg) {
4646 err = -ENOMEM;
4647 goto out;
4648 }
4649
4650 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4651 NL80211_CMD_ACTION);
4652
4653 if (IS_ERR(hdr)) {
4654 err = PTR_ERR(hdr);
4655 goto free_msg;
4656 }
4657 err = cfg80211_mlme_action(rdev, dev, chan, channel_type,
4658 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4659 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4660 &cookie);
4661 if (err)
4662 goto free_msg;
4663
4664 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4665
4666 genlmsg_end(msg, hdr);
4667 err = genlmsg_reply(msg, info);
4668 goto out;
4669
4670 nla_put_failure:
4671 err = -ENOBUFS;
4672 free_msg:
4673 nlmsg_free(msg);
4674 out:
4675 cfg80211_unlock_rdev(rdev);
4676 dev_put(dev);
4677unlock_rtnl:
4678 rtnl_unlock();
4679 return err;
4680}
4681
Kalle Valoffb9eb32010-02-17 17:58:10 +02004682static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4683{
4684 struct cfg80211_registered_device *rdev;
4685 struct wireless_dev *wdev;
4686 struct net_device *dev;
4687 u8 ps_state;
4688 bool state;
4689 int err;
4690
4691 if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4692 err = -EINVAL;
4693 goto out;
4694 }
4695
4696 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4697
4698 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4699 err = -EINVAL;
4700 goto out;
4701 }
4702
4703 rtnl_lock();
4704
4705 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4706 if (err)
4707 goto unlock_rdev;
4708
4709 wdev = dev->ieee80211_ptr;
4710
4711 if (!rdev->ops->set_power_mgmt) {
4712 err = -EOPNOTSUPP;
4713 goto unlock_rdev;
4714 }
4715
4716 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4717
4718 if (state == wdev->ps)
4719 goto unlock_rdev;
4720
4721 wdev->ps = state;
4722
4723 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
4724 wdev->ps_timeout))
4725 /* assume this means it's off */
4726 wdev->ps = false;
4727
4728unlock_rdev:
4729 cfg80211_unlock_rdev(rdev);
4730 dev_put(dev);
4731 rtnl_unlock();
4732
4733out:
4734 return err;
4735}
4736
4737static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4738{
4739 struct cfg80211_registered_device *rdev;
4740 enum nl80211_ps_state ps_state;
4741 struct wireless_dev *wdev;
4742 struct net_device *dev;
4743 struct sk_buff *msg;
4744 void *hdr;
4745 int err;
4746
4747 rtnl_lock();
4748
4749 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4750 if (err)
4751 goto unlock_rtnl;
4752
4753 wdev = dev->ieee80211_ptr;
4754
4755 if (!rdev->ops->set_power_mgmt) {
4756 err = -EOPNOTSUPP;
4757 goto out;
4758 }
4759
4760 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4761 if (!msg) {
4762 err = -ENOMEM;
4763 goto out;
4764 }
4765
4766 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4767 NL80211_CMD_GET_POWER_SAVE);
4768 if (!hdr) {
4769 err = -ENOMEM;
4770 goto free_msg;
4771 }
4772
4773 if (wdev->ps)
4774 ps_state = NL80211_PS_ENABLED;
4775 else
4776 ps_state = NL80211_PS_DISABLED;
4777
4778 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4779
4780 genlmsg_end(msg, hdr);
4781 err = genlmsg_reply(msg, info);
4782 goto out;
4783
4784nla_put_failure:
4785 err = -ENOBUFS;
4786
4787free_msg:
4788 nlmsg_free(msg);
4789
4790out:
4791 cfg80211_unlock_rdev(rdev);
4792 dev_put(dev);
4793
4794unlock_rtnl:
4795 rtnl_unlock();
4796
4797 return err;
4798}
4799
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004800static struct nla_policy
4801nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4802 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4803 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4804 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4805};
4806
4807static int nl80211_set_cqm_rssi(struct genl_info *info,
4808 s32 threshold, u32 hysteresis)
4809{
4810 struct cfg80211_registered_device *rdev;
4811 struct wireless_dev *wdev;
4812 struct net_device *dev;
4813 int err;
4814
4815 if (threshold > 0)
4816 return -EINVAL;
4817
4818 rtnl_lock();
4819
4820 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4821 if (err)
4822 goto unlock_rdev;
4823
4824 wdev = dev->ieee80211_ptr;
4825
4826 if (!rdev->ops->set_cqm_rssi_config) {
4827 err = -EOPNOTSUPP;
4828 goto unlock_rdev;
4829 }
4830
4831 if (wdev->iftype != NL80211_IFTYPE_STATION) {
4832 err = -EOPNOTSUPP;
4833 goto unlock_rdev;
4834 }
4835
4836 err = rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4837 threshold, hysteresis);
4838
4839unlock_rdev:
4840 cfg80211_unlock_rdev(rdev);
4841 dev_put(dev);
4842 rtnl_unlock();
4843
4844 return err;
4845}
4846
4847static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4848{
4849 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4850 struct nlattr *cqm;
4851 int err;
4852
4853 cqm = info->attrs[NL80211_ATTR_CQM];
4854 if (!cqm) {
4855 err = -EINVAL;
4856 goto out;
4857 }
4858
4859 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4860 nl80211_attr_cqm_policy);
4861 if (err)
4862 goto out;
4863
4864 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4865 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4866 s32 threshold;
4867 u32 hysteresis;
4868 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4869 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4870 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4871 } else
4872 err = -EINVAL;
4873
4874out:
4875 return err;
4876}
4877
Johannes Berg55682962007-09-20 13:09:35 -04004878static struct genl_ops nl80211_ops[] = {
4879 {
4880 .cmd = NL80211_CMD_GET_WIPHY,
4881 .doit = nl80211_get_wiphy,
4882 .dumpit = nl80211_dump_wiphy,
4883 .policy = nl80211_policy,
4884 /* can be retrieved by unprivileged users */
4885 },
4886 {
4887 .cmd = NL80211_CMD_SET_WIPHY,
4888 .doit = nl80211_set_wiphy,
4889 .policy = nl80211_policy,
4890 .flags = GENL_ADMIN_PERM,
4891 },
4892 {
4893 .cmd = NL80211_CMD_GET_INTERFACE,
4894 .doit = nl80211_get_interface,
4895 .dumpit = nl80211_dump_interface,
4896 .policy = nl80211_policy,
4897 /* can be retrieved by unprivileged users */
4898 },
4899 {
4900 .cmd = NL80211_CMD_SET_INTERFACE,
4901 .doit = nl80211_set_interface,
4902 .policy = nl80211_policy,
4903 .flags = GENL_ADMIN_PERM,
4904 },
4905 {
4906 .cmd = NL80211_CMD_NEW_INTERFACE,
4907 .doit = nl80211_new_interface,
4908 .policy = nl80211_policy,
4909 .flags = GENL_ADMIN_PERM,
4910 },
4911 {
4912 .cmd = NL80211_CMD_DEL_INTERFACE,
4913 .doit = nl80211_del_interface,
4914 .policy = nl80211_policy,
4915 .flags = GENL_ADMIN_PERM,
4916 },
Johannes Berg41ade002007-12-19 02:03:29 +01004917 {
4918 .cmd = NL80211_CMD_GET_KEY,
4919 .doit = nl80211_get_key,
4920 .policy = nl80211_policy,
4921 .flags = GENL_ADMIN_PERM,
4922 },
4923 {
4924 .cmd = NL80211_CMD_SET_KEY,
4925 .doit = nl80211_set_key,
4926 .policy = nl80211_policy,
4927 .flags = GENL_ADMIN_PERM,
4928 },
4929 {
4930 .cmd = NL80211_CMD_NEW_KEY,
4931 .doit = nl80211_new_key,
4932 .policy = nl80211_policy,
4933 .flags = GENL_ADMIN_PERM,
4934 },
4935 {
4936 .cmd = NL80211_CMD_DEL_KEY,
4937 .doit = nl80211_del_key,
4938 .policy = nl80211_policy,
4939 .flags = GENL_ADMIN_PERM,
4940 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004941 {
4942 .cmd = NL80211_CMD_SET_BEACON,
4943 .policy = nl80211_policy,
4944 .flags = GENL_ADMIN_PERM,
4945 .doit = nl80211_addset_beacon,
4946 },
4947 {
4948 .cmd = NL80211_CMD_NEW_BEACON,
4949 .policy = nl80211_policy,
4950 .flags = GENL_ADMIN_PERM,
4951 .doit = nl80211_addset_beacon,
4952 },
4953 {
4954 .cmd = NL80211_CMD_DEL_BEACON,
4955 .policy = nl80211_policy,
4956 .flags = GENL_ADMIN_PERM,
4957 .doit = nl80211_del_beacon,
4958 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004959 {
4960 .cmd = NL80211_CMD_GET_STATION,
4961 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004962 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004963 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004964 },
4965 {
4966 .cmd = NL80211_CMD_SET_STATION,
4967 .doit = nl80211_set_station,
4968 .policy = nl80211_policy,
4969 .flags = GENL_ADMIN_PERM,
4970 },
4971 {
4972 .cmd = NL80211_CMD_NEW_STATION,
4973 .doit = nl80211_new_station,
4974 .policy = nl80211_policy,
4975 .flags = GENL_ADMIN_PERM,
4976 },
4977 {
4978 .cmd = NL80211_CMD_DEL_STATION,
4979 .doit = nl80211_del_station,
4980 .policy = nl80211_policy,
4981 .flags = GENL_ADMIN_PERM,
4982 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004983 {
4984 .cmd = NL80211_CMD_GET_MPATH,
4985 .doit = nl80211_get_mpath,
4986 .dumpit = nl80211_dump_mpath,
4987 .policy = nl80211_policy,
4988 .flags = GENL_ADMIN_PERM,
4989 },
4990 {
4991 .cmd = NL80211_CMD_SET_MPATH,
4992 .doit = nl80211_set_mpath,
4993 .policy = nl80211_policy,
4994 .flags = GENL_ADMIN_PERM,
4995 },
4996 {
4997 .cmd = NL80211_CMD_NEW_MPATH,
4998 .doit = nl80211_new_mpath,
4999 .policy = nl80211_policy,
5000 .flags = GENL_ADMIN_PERM,
5001 },
5002 {
5003 .cmd = NL80211_CMD_DEL_MPATH,
5004 .doit = nl80211_del_mpath,
5005 .policy = nl80211_policy,
5006 .flags = GENL_ADMIN_PERM,
5007 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005008 {
5009 .cmd = NL80211_CMD_SET_BSS,
5010 .doit = nl80211_set_bss,
5011 .policy = nl80211_policy,
5012 .flags = GENL_ADMIN_PERM,
5013 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005014 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005015 .cmd = NL80211_CMD_GET_REG,
5016 .doit = nl80211_get_reg,
5017 .policy = nl80211_policy,
5018 /* can be retrieved by unprivileged users */
5019 },
5020 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005021 .cmd = NL80211_CMD_SET_REG,
5022 .doit = nl80211_set_reg,
5023 .policy = nl80211_policy,
5024 .flags = GENL_ADMIN_PERM,
5025 },
5026 {
5027 .cmd = NL80211_CMD_REQ_SET_REG,
5028 .doit = nl80211_req_set_reg,
5029 .policy = nl80211_policy,
5030 .flags = GENL_ADMIN_PERM,
5031 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005032 {
5033 .cmd = NL80211_CMD_GET_MESH_PARAMS,
5034 .doit = nl80211_get_mesh_params,
5035 .policy = nl80211_policy,
5036 /* can be retrieved by unprivileged users */
5037 },
5038 {
5039 .cmd = NL80211_CMD_SET_MESH_PARAMS,
5040 .doit = nl80211_set_mesh_params,
5041 .policy = nl80211_policy,
5042 .flags = GENL_ADMIN_PERM,
5043 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005044 {
Johannes Berg2a519312009-02-10 21:25:55 +01005045 .cmd = NL80211_CMD_TRIGGER_SCAN,
5046 .doit = nl80211_trigger_scan,
5047 .policy = nl80211_policy,
5048 .flags = GENL_ADMIN_PERM,
5049 },
5050 {
5051 .cmd = NL80211_CMD_GET_SCAN,
5052 .policy = nl80211_policy,
5053 .dumpit = nl80211_dump_scan,
5054 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005055 {
5056 .cmd = NL80211_CMD_AUTHENTICATE,
5057 .doit = nl80211_authenticate,
5058 .policy = nl80211_policy,
5059 .flags = GENL_ADMIN_PERM,
5060 },
5061 {
5062 .cmd = NL80211_CMD_ASSOCIATE,
5063 .doit = nl80211_associate,
5064 .policy = nl80211_policy,
5065 .flags = GENL_ADMIN_PERM,
5066 },
5067 {
5068 .cmd = NL80211_CMD_DEAUTHENTICATE,
5069 .doit = nl80211_deauthenticate,
5070 .policy = nl80211_policy,
5071 .flags = GENL_ADMIN_PERM,
5072 },
5073 {
5074 .cmd = NL80211_CMD_DISASSOCIATE,
5075 .doit = nl80211_disassociate,
5076 .policy = nl80211_policy,
5077 .flags = GENL_ADMIN_PERM,
5078 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005079 {
5080 .cmd = NL80211_CMD_JOIN_IBSS,
5081 .doit = nl80211_join_ibss,
5082 .policy = nl80211_policy,
5083 .flags = GENL_ADMIN_PERM,
5084 },
5085 {
5086 .cmd = NL80211_CMD_LEAVE_IBSS,
5087 .doit = nl80211_leave_ibss,
5088 .policy = nl80211_policy,
5089 .flags = GENL_ADMIN_PERM,
5090 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005091#ifdef CONFIG_NL80211_TESTMODE
5092 {
5093 .cmd = NL80211_CMD_TESTMODE,
5094 .doit = nl80211_testmode_do,
5095 .policy = nl80211_policy,
5096 .flags = GENL_ADMIN_PERM,
5097 },
5098#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005099 {
5100 .cmd = NL80211_CMD_CONNECT,
5101 .doit = nl80211_connect,
5102 .policy = nl80211_policy,
5103 .flags = GENL_ADMIN_PERM,
5104 },
5105 {
5106 .cmd = NL80211_CMD_DISCONNECT,
5107 .doit = nl80211_disconnect,
5108 .policy = nl80211_policy,
5109 .flags = GENL_ADMIN_PERM,
5110 },
Johannes Berg463d0182009-07-14 00:33:35 +02005111 {
5112 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5113 .doit = nl80211_wiphy_netns,
5114 .policy = nl80211_policy,
5115 .flags = GENL_ADMIN_PERM,
5116 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005117 {
5118 .cmd = NL80211_CMD_GET_SURVEY,
5119 .policy = nl80211_policy,
5120 .dumpit = nl80211_dump_survey,
5121 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005122 {
5123 .cmd = NL80211_CMD_SET_PMKSA,
5124 .doit = nl80211_setdel_pmksa,
5125 .policy = nl80211_policy,
5126 .flags = GENL_ADMIN_PERM,
5127 },
5128 {
5129 .cmd = NL80211_CMD_DEL_PMKSA,
5130 .doit = nl80211_setdel_pmksa,
5131 .policy = nl80211_policy,
5132 .flags = GENL_ADMIN_PERM,
5133 },
5134 {
5135 .cmd = NL80211_CMD_FLUSH_PMKSA,
5136 .doit = nl80211_flush_pmksa,
5137 .policy = nl80211_policy,
5138 .flags = GENL_ADMIN_PERM,
5139 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005140 {
5141 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5142 .doit = nl80211_remain_on_channel,
5143 .policy = nl80211_policy,
5144 .flags = GENL_ADMIN_PERM,
5145 },
5146 {
5147 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5148 .doit = nl80211_cancel_remain_on_channel,
5149 .policy = nl80211_policy,
5150 .flags = GENL_ADMIN_PERM,
5151 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005152 {
5153 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5154 .doit = nl80211_set_tx_bitrate_mask,
5155 .policy = nl80211_policy,
5156 .flags = GENL_ADMIN_PERM,
5157 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005158 {
5159 .cmd = NL80211_CMD_REGISTER_ACTION,
5160 .doit = nl80211_register_action,
5161 .policy = nl80211_policy,
5162 .flags = GENL_ADMIN_PERM,
5163 },
5164 {
5165 .cmd = NL80211_CMD_ACTION,
5166 .doit = nl80211_action,
5167 .policy = nl80211_policy,
5168 .flags = GENL_ADMIN_PERM,
5169 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005170 {
5171 .cmd = NL80211_CMD_SET_POWER_SAVE,
5172 .doit = nl80211_set_power_save,
5173 .policy = nl80211_policy,
5174 .flags = GENL_ADMIN_PERM,
5175 },
5176 {
5177 .cmd = NL80211_CMD_GET_POWER_SAVE,
5178 .doit = nl80211_get_power_save,
5179 .policy = nl80211_policy,
5180 /* can be retrieved by unprivileged users */
5181 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005182 {
5183 .cmd = NL80211_CMD_SET_CQM,
5184 .doit = nl80211_set_cqm,
5185 .policy = nl80211_policy,
5186 .flags = GENL_ADMIN_PERM,
5187 },
Johannes Berg55682962007-09-20 13:09:35 -04005188};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005189
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005190static struct genl_multicast_group nl80211_mlme_mcgrp = {
5191 .name = "mlme",
5192};
Johannes Berg55682962007-09-20 13:09:35 -04005193
5194/* multicast groups */
5195static struct genl_multicast_group nl80211_config_mcgrp = {
5196 .name = "config",
5197};
Johannes Berg2a519312009-02-10 21:25:55 +01005198static struct genl_multicast_group nl80211_scan_mcgrp = {
5199 .name = "scan",
5200};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005201static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5202 .name = "regulatory",
5203};
Johannes Berg55682962007-09-20 13:09:35 -04005204
5205/* notification functions */
5206
5207void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5208{
5209 struct sk_buff *msg;
5210
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005212 if (!msg)
5213 return;
5214
5215 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5216 nlmsg_free(msg);
5217 return;
5218 }
5219
Johannes Berg463d0182009-07-14 00:33:35 +02005220 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5221 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005222}
5223
Johannes Berg362a4152009-05-24 16:43:15 +02005224static int nl80211_add_scan_req(struct sk_buff *msg,
5225 struct cfg80211_registered_device *rdev)
5226{
5227 struct cfg80211_scan_request *req = rdev->scan_req;
5228 struct nlattr *nest;
5229 int i;
5230
Johannes Berg667503d2009-07-07 03:56:11 +02005231 ASSERT_RDEV_LOCK(rdev);
5232
Johannes Berg362a4152009-05-24 16:43:15 +02005233 if (WARN_ON(!req))
5234 return 0;
5235
5236 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5237 if (!nest)
5238 goto nla_put_failure;
5239 for (i = 0; i < req->n_ssids; i++)
5240 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5241 nla_nest_end(msg, nest);
5242
5243 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5244 if (!nest)
5245 goto nla_put_failure;
5246 for (i = 0; i < req->n_channels; i++)
5247 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5248 nla_nest_end(msg, nest);
5249
5250 if (req->ie)
5251 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5252
5253 return 0;
5254 nla_put_failure:
5255 return -ENOBUFS;
5256}
5257
Johannes Berga538e2d2009-06-16 19:56:42 +02005258static int nl80211_send_scan_msg(struct sk_buff *msg,
5259 struct cfg80211_registered_device *rdev,
5260 struct net_device *netdev,
5261 u32 pid, u32 seq, int flags,
5262 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005263{
5264 void *hdr;
5265
5266 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5267 if (!hdr)
5268 return -1;
5269
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005270 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5272
Johannes Berg362a4152009-05-24 16:43:15 +02005273 /* ignore errors and send incomplete event anyway */
5274 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005275
5276 return genlmsg_end(msg, hdr);
5277
5278 nla_put_failure:
5279 genlmsg_cancel(msg, hdr);
5280 return -EMSGSIZE;
5281}
5282
Johannes Berga538e2d2009-06-16 19:56:42 +02005283void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5284 struct net_device *netdev)
5285{
5286 struct sk_buff *msg;
5287
5288 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5289 if (!msg)
5290 return;
5291
5292 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5293 NL80211_CMD_TRIGGER_SCAN) < 0) {
5294 nlmsg_free(msg);
5295 return;
5296 }
5297
Johannes Berg463d0182009-07-14 00:33:35 +02005298 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5299 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005300}
5301
Johannes Berg2a519312009-02-10 21:25:55 +01005302void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5303 struct net_device *netdev)
5304{
5305 struct sk_buff *msg;
5306
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005308 if (!msg)
5309 return;
5310
Johannes Berga538e2d2009-06-16 19:56:42 +02005311 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5312 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005313 nlmsg_free(msg);
5314 return;
5315 }
5316
Johannes Berg463d0182009-07-14 00:33:35 +02005317 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5318 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005319}
5320
5321void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5322 struct net_device *netdev)
5323{
5324 struct sk_buff *msg;
5325
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005326 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005327 if (!msg)
5328 return;
5329
Johannes Berga538e2d2009-06-16 19:56:42 +02005330 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5331 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005332 nlmsg_free(msg);
5333 return;
5334 }
5335
Johannes Berg463d0182009-07-14 00:33:35 +02005336 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5337 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005338}
5339
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005340/*
5341 * This can happen on global regulatory changes or device specific settings
5342 * based on custom world regulatory domains.
5343 */
5344void nl80211_send_reg_change_event(struct regulatory_request *request)
5345{
5346 struct sk_buff *msg;
5347 void *hdr;
5348
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005349 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005350 if (!msg)
5351 return;
5352
5353 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5354 if (!hdr) {
5355 nlmsg_free(msg);
5356 return;
5357 }
5358
5359 /* Userspace can always count this one always being set */
5360 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5361
5362 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5363 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5364 NL80211_REGDOM_TYPE_WORLD);
5365 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5366 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5367 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5368 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5369 request->intersect)
5370 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5371 NL80211_REGDOM_TYPE_INTERSECTION);
5372 else {
5373 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5374 NL80211_REGDOM_TYPE_COUNTRY);
5375 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5376 }
5377
5378 if (wiphy_idx_valid(request->wiphy_idx))
5379 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5380
5381 if (genlmsg_end(msg, hdr) < 0) {
5382 nlmsg_free(msg);
5383 return;
5384 }
5385
Johannes Bergbc43b282009-07-25 10:54:13 +02005386 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005387 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005388 GFP_ATOMIC);
5389 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005390
5391 return;
5392
5393nla_put_failure:
5394 genlmsg_cancel(msg, hdr);
5395 nlmsg_free(msg);
5396}
5397
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005398static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5399 struct net_device *netdev,
5400 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005401 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005402{
5403 struct sk_buff *msg;
5404 void *hdr;
5405
Johannes Berge6d6e342009-07-01 21:26:47 +02005406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005407 if (!msg)
5408 return;
5409
5410 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5411 if (!hdr) {
5412 nlmsg_free(msg);
5413 return;
5414 }
5415
5416 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5417 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5418 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5419
5420 if (genlmsg_end(msg, hdr) < 0) {
5421 nlmsg_free(msg);
5422 return;
5423 }
5424
Johannes Berg463d0182009-07-14 00:33:35 +02005425 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5426 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005427 return;
5428
5429 nla_put_failure:
5430 genlmsg_cancel(msg, hdr);
5431 nlmsg_free(msg);
5432}
5433
5434void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005435 struct net_device *netdev, const u8 *buf,
5436 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005437{
5438 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005439 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005440}
5441
5442void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5443 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005444 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005445{
Johannes Berge6d6e342009-07-01 21:26:47 +02005446 nl80211_send_mlme_event(rdev, netdev, buf, len,
5447 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005448}
5449
Jouni Malinen53b46b82009-03-27 20:53:56 +02005450void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005451 struct net_device *netdev, const u8 *buf,
5452 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005453{
5454 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005455 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005456}
5457
Jouni Malinen53b46b82009-03-27 20:53:56 +02005458void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5459 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005460 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005461{
5462 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005463 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005464}
5465
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005466static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5467 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005468 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005469{
5470 struct sk_buff *msg;
5471 void *hdr;
5472
Johannes Berge6d6e342009-07-01 21:26:47 +02005473 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005474 if (!msg)
5475 return;
5476
5477 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5478 if (!hdr) {
5479 nlmsg_free(msg);
5480 return;
5481 }
5482
5483 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5484 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5485 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5486 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5487
5488 if (genlmsg_end(msg, hdr) < 0) {
5489 nlmsg_free(msg);
5490 return;
5491 }
5492
Johannes Berg463d0182009-07-14 00:33:35 +02005493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5494 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005495 return;
5496
5497 nla_put_failure:
5498 genlmsg_cancel(msg, hdr);
5499 nlmsg_free(msg);
5500}
5501
5502void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005503 struct net_device *netdev, const u8 *addr,
5504 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005505{
5506 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005507 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005508}
5509
5510void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005511 struct net_device *netdev, const u8 *addr,
5512 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005513{
Johannes Berge6d6e342009-07-01 21:26:47 +02005514 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5515 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005516}
5517
Samuel Ortizb23aa672009-07-01 21:26:54 +02005518void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5519 struct net_device *netdev, const u8 *bssid,
5520 const u8 *req_ie, size_t req_ie_len,
5521 const u8 *resp_ie, size_t resp_ie_len,
5522 u16 status, gfp_t gfp)
5523{
5524 struct sk_buff *msg;
5525 void *hdr;
5526
5527 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5528 if (!msg)
5529 return;
5530
5531 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5532 if (!hdr) {
5533 nlmsg_free(msg);
5534 return;
5535 }
5536
5537 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5538 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5539 if (bssid)
5540 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5541 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5542 if (req_ie)
5543 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5544 if (resp_ie)
5545 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5546
5547 if (genlmsg_end(msg, hdr) < 0) {
5548 nlmsg_free(msg);
5549 return;
5550 }
5551
Johannes Berg463d0182009-07-14 00:33:35 +02005552 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5553 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005554 return;
5555
5556 nla_put_failure:
5557 genlmsg_cancel(msg, hdr);
5558 nlmsg_free(msg);
5559
5560}
5561
5562void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5563 struct net_device *netdev, const u8 *bssid,
5564 const u8 *req_ie, size_t req_ie_len,
5565 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5566{
5567 struct sk_buff *msg;
5568 void *hdr;
5569
5570 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5571 if (!msg)
5572 return;
5573
5574 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5575 if (!hdr) {
5576 nlmsg_free(msg);
5577 return;
5578 }
5579
5580 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5581 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5582 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5583 if (req_ie)
5584 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5585 if (resp_ie)
5586 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5587
5588 if (genlmsg_end(msg, hdr) < 0) {
5589 nlmsg_free(msg);
5590 return;
5591 }
5592
Johannes Berg463d0182009-07-14 00:33:35 +02005593 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5594 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005595 return;
5596
5597 nla_put_failure:
5598 genlmsg_cancel(msg, hdr);
5599 nlmsg_free(msg);
5600
5601}
5602
5603void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5604 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005605 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005606{
5607 struct sk_buff *msg;
5608 void *hdr;
5609
Johannes Berg667503d2009-07-07 03:56:11 +02005610 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005611 if (!msg)
5612 return;
5613
5614 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5615 if (!hdr) {
5616 nlmsg_free(msg);
5617 return;
5618 }
5619
5620 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5621 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5622 if (from_ap && reason)
5623 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5624 if (from_ap)
5625 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5626 if (ie)
5627 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5628
5629 if (genlmsg_end(msg, hdr) < 0) {
5630 nlmsg_free(msg);
5631 return;
5632 }
5633
Johannes Berg463d0182009-07-14 00:33:35 +02005634 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5635 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005636 return;
5637
5638 nla_put_failure:
5639 genlmsg_cancel(msg, hdr);
5640 nlmsg_free(msg);
5641
5642}
5643
Johannes Berg04a773a2009-04-19 21:24:32 +02005644void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5645 struct net_device *netdev, const u8 *bssid,
5646 gfp_t gfp)
5647{
5648 struct sk_buff *msg;
5649 void *hdr;
5650
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005651 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005652 if (!msg)
5653 return;
5654
5655 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5656 if (!hdr) {
5657 nlmsg_free(msg);
5658 return;
5659 }
5660
5661 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5662 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5663 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5664
5665 if (genlmsg_end(msg, hdr) < 0) {
5666 nlmsg_free(msg);
5667 return;
5668 }
5669
Johannes Berg463d0182009-07-14 00:33:35 +02005670 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5671 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005672 return;
5673
5674 nla_put_failure:
5675 genlmsg_cancel(msg, hdr);
5676 nlmsg_free(msg);
5677}
5678
Jouni Malinena3b8b052009-03-27 21:59:49 +02005679void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5680 struct net_device *netdev, const u8 *addr,
5681 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005682 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005683{
5684 struct sk_buff *msg;
5685 void *hdr;
5686
Johannes Berge6d6e342009-07-01 21:26:47 +02005687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005688 if (!msg)
5689 return;
5690
5691 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5692 if (!hdr) {
5693 nlmsg_free(msg);
5694 return;
5695 }
5696
5697 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5698 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5699 if (addr)
5700 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5701 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5702 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5703 if (tsc)
5704 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5705
5706 if (genlmsg_end(msg, hdr) < 0) {
5707 nlmsg_free(msg);
5708 return;
5709 }
5710
Johannes Berg463d0182009-07-14 00:33:35 +02005711 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5712 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005713 return;
5714
5715 nla_put_failure:
5716 genlmsg_cancel(msg, hdr);
5717 nlmsg_free(msg);
5718}
5719
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005720void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5721 struct ieee80211_channel *channel_before,
5722 struct ieee80211_channel *channel_after)
5723{
5724 struct sk_buff *msg;
5725 void *hdr;
5726 struct nlattr *nl_freq;
5727
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005728 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005729 if (!msg)
5730 return;
5731
5732 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5733 if (!hdr) {
5734 nlmsg_free(msg);
5735 return;
5736 }
5737
5738 /*
5739 * Since we are applying the beacon hint to a wiphy we know its
5740 * wiphy_idx is valid
5741 */
5742 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5743
5744 /* Before */
5745 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5746 if (!nl_freq)
5747 goto nla_put_failure;
5748 if (nl80211_msg_put_channel(msg, channel_before))
5749 goto nla_put_failure;
5750 nla_nest_end(msg, nl_freq);
5751
5752 /* After */
5753 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5754 if (!nl_freq)
5755 goto nla_put_failure;
5756 if (nl80211_msg_put_channel(msg, channel_after))
5757 goto nla_put_failure;
5758 nla_nest_end(msg, nl_freq);
5759
5760 if (genlmsg_end(msg, hdr) < 0) {
5761 nlmsg_free(msg);
5762 return;
5763 }
5764
Johannes Berg463d0182009-07-14 00:33:35 +02005765 rcu_read_lock();
5766 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5767 GFP_ATOMIC);
5768 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005769
5770 return;
5771
5772nla_put_failure:
5773 genlmsg_cancel(msg, hdr);
5774 nlmsg_free(msg);
5775}
5776
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005777static void nl80211_send_remain_on_chan_event(
5778 int cmd, struct cfg80211_registered_device *rdev,
5779 struct net_device *netdev, u64 cookie,
5780 struct ieee80211_channel *chan,
5781 enum nl80211_channel_type channel_type,
5782 unsigned int duration, gfp_t gfp)
5783{
5784 struct sk_buff *msg;
5785 void *hdr;
5786
5787 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5788 if (!msg)
5789 return;
5790
5791 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5792 if (!hdr) {
5793 nlmsg_free(msg);
5794 return;
5795 }
5796
5797 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5798 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5799 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5800 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5801 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5802
5803 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5804 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5805
5806 if (genlmsg_end(msg, hdr) < 0) {
5807 nlmsg_free(msg);
5808 return;
5809 }
5810
5811 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5812 nl80211_mlme_mcgrp.id, gfp);
5813 return;
5814
5815 nla_put_failure:
5816 genlmsg_cancel(msg, hdr);
5817 nlmsg_free(msg);
5818}
5819
5820void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5821 struct net_device *netdev, u64 cookie,
5822 struct ieee80211_channel *chan,
5823 enum nl80211_channel_type channel_type,
5824 unsigned int duration, gfp_t gfp)
5825{
5826 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5827 rdev, netdev, cookie, chan,
5828 channel_type, duration, gfp);
5829}
5830
5831void nl80211_send_remain_on_channel_cancel(
5832 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5833 u64 cookie, struct ieee80211_channel *chan,
5834 enum nl80211_channel_type channel_type, gfp_t gfp)
5835{
5836 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5837 rdev, netdev, cookie, chan,
5838 channel_type, 0, gfp);
5839}
5840
Johannes Berg98b62182009-12-23 13:15:44 +01005841void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5842 struct net_device *dev, const u8 *mac_addr,
5843 struct station_info *sinfo, gfp_t gfp)
5844{
5845 struct sk_buff *msg;
5846
5847 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5848 if (!msg)
5849 return;
5850
5851 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5852 nlmsg_free(msg);
5853 return;
5854 }
5855
5856 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5857 nl80211_mlme_mcgrp.id, gfp);
5858}
5859
Jouni Malinen026331c2010-02-15 12:53:10 +02005860int nl80211_send_action(struct cfg80211_registered_device *rdev,
5861 struct net_device *netdev, u32 nlpid,
5862 int freq, const u8 *buf, size_t len, gfp_t gfp)
5863{
5864 struct sk_buff *msg;
5865 void *hdr;
5866 int err;
5867
5868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5869 if (!msg)
5870 return -ENOMEM;
5871
5872 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION);
5873 if (!hdr) {
5874 nlmsg_free(msg);
5875 return -ENOMEM;
5876 }
5877
5878 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5879 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5880 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5881 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5882
5883 err = genlmsg_end(msg, hdr);
5884 if (err < 0) {
5885 nlmsg_free(msg);
5886 return err;
5887 }
5888
5889 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5890 if (err < 0)
5891 return err;
5892 return 0;
5893
5894 nla_put_failure:
5895 genlmsg_cancel(msg, hdr);
5896 nlmsg_free(msg);
5897 return -ENOBUFS;
5898}
5899
5900void nl80211_send_action_tx_status(struct cfg80211_registered_device *rdev,
5901 struct net_device *netdev, u64 cookie,
5902 const u8 *buf, size_t len, bool ack,
5903 gfp_t gfp)
5904{
5905 struct sk_buff *msg;
5906 void *hdr;
5907
5908 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5909 if (!msg)
5910 return;
5911
5912 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION_TX_STATUS);
5913 if (!hdr) {
5914 nlmsg_free(msg);
5915 return;
5916 }
5917
5918 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5919 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5920 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5921 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5922 if (ack)
5923 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5924
5925 if (genlmsg_end(msg, hdr) < 0) {
5926 nlmsg_free(msg);
5927 return;
5928 }
5929
5930 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5931 return;
5932
5933 nla_put_failure:
5934 genlmsg_cancel(msg, hdr);
5935 nlmsg_free(msg);
5936}
5937
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005938void
5939nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5940 struct net_device *netdev,
5941 enum nl80211_cqm_rssi_threshold_event rssi_event,
5942 gfp_t gfp)
5943{
5944 struct sk_buff *msg;
5945 struct nlattr *pinfoattr;
5946 void *hdr;
5947
5948 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5949 if (!msg)
5950 return;
5951
5952 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5953 if (!hdr) {
5954 nlmsg_free(msg);
5955 return;
5956 }
5957
5958 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5959 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5960
5961 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5962 if (!pinfoattr)
5963 goto nla_put_failure;
5964
5965 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5966 rssi_event);
5967
5968 nla_nest_end(msg, pinfoattr);
5969
5970 if (genlmsg_end(msg, hdr) < 0) {
5971 nlmsg_free(msg);
5972 return;
5973 }
5974
5975 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5976 nl80211_mlme_mcgrp.id, gfp);
5977 return;
5978
5979 nla_put_failure:
5980 genlmsg_cancel(msg, hdr);
5981 nlmsg_free(msg);
5982}
5983
Jouni Malinen026331c2010-02-15 12:53:10 +02005984static int nl80211_netlink_notify(struct notifier_block * nb,
5985 unsigned long state,
5986 void *_notify)
5987{
5988 struct netlink_notify *notify = _notify;
5989 struct cfg80211_registered_device *rdev;
5990 struct wireless_dev *wdev;
5991
5992 if (state != NETLINK_URELEASE)
5993 return NOTIFY_DONE;
5994
5995 rcu_read_lock();
5996
5997 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5998 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
5999 cfg80211_mlme_unregister_actions(wdev, notify->pid);
6000
6001 rcu_read_unlock();
6002
6003 return NOTIFY_DONE;
6004}
6005
6006static struct notifier_block nl80211_netlink_notifier = {
6007 .notifier_call = nl80211_netlink_notify,
6008};
6009
Johannes Berg55682962007-09-20 13:09:35 -04006010/* initialisation/exit functions */
6011
6012int nl80211_init(void)
6013{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006014 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006015
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006016 err = genl_register_family_with_ops(&nl80211_fam,
6017 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006018 if (err)
6019 return err;
6020
Johannes Berg55682962007-09-20 13:09:35 -04006021 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6022 if (err)
6023 goto err_out;
6024
Johannes Berg2a519312009-02-10 21:25:55 +01006025 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6026 if (err)
6027 goto err_out;
6028
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006029 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6030 if (err)
6031 goto err_out;
6032
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006033 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6034 if (err)
6035 goto err_out;
6036
Johannes Bergaff89a92009-07-01 21:26:51 +02006037#ifdef CONFIG_NL80211_TESTMODE
6038 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6039 if (err)
6040 goto err_out;
6041#endif
6042
Jouni Malinen026331c2010-02-15 12:53:10 +02006043 err = netlink_register_notifier(&nl80211_netlink_notifier);
6044 if (err)
6045 goto err_out;
6046
Johannes Berg55682962007-09-20 13:09:35 -04006047 return 0;
6048 err_out:
6049 genl_unregister_family(&nl80211_fam);
6050 return err;
6051}
6052
6053void nl80211_exit(void)
6054{
Jouni Malinen026331c2010-02-15 12:53:10 +02006055 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006056 genl_unregister_family(&nl80211_fam);
6057}