blob: f15b1af2c768ca8007902b045e998456d38d595a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
26/* the netlink family */
27static struct genl_family nl80211_fam = {
28 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
29 .name = "nl80211", /* have users key off the name instead */
30 .hdrsize = 0, /* no private header */
31 .version = 1, /* no particular meaning now */
32 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020033 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040034};
35
Johannes Berg79c97e92009-07-07 03:56:12 +020036/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020037static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020038 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040039 struct net_device **dev)
40{
Johannes Berg463d0182009-07-14 00:33:35 +020041 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040042 int ifindex;
43
Johannes Bergbba95fe2008-07-29 13:22:51 +020044 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040045 return -EINVAL;
46
Johannes Bergbba95fe2008-07-29 13:22:51 +020047 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020048 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040049 if (!*dev)
50 return -ENODEV;
51
Johannes Berg463d0182009-07-14 00:33:35 +020052 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020053 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040054 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020055 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040056 }
57
58 return 0;
59}
60
61/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000062static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040063 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
64 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070065 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020066 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020067 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053068 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020069 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
71 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
72 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010073 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040074
75 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
77 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010078
79 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020080 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010081
Johannes Bergb9454e82009-07-08 13:29:08 +020082 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010083 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
84 .len = WLAN_MAX_KEY_LEN },
85 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
86 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
87 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030088 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010089
90 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
91 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
92 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
94 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
95 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010096 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
98 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
99 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
100 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100101 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100102 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200103 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100104 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
105 .len = IEEE80211_MAX_MESH_ID_LEN },
106 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300107
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700108 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
109 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
110
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300111 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
113 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200114 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
115 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300116
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700117 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
118
Jouni Malinen36aedc92008-08-25 11:58:58 +0300119 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
120 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200121
122 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
123 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
124 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100125 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
126 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200127
128 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
129 .len = IEEE80211_MAX_SSID_LEN },
130 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
131 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200132 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300133 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300134 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300135 [NL80211_ATTR_STA_FLAGS2] = {
136 .len = sizeof(struct nl80211_sta_flag_update),
137 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300138 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300139 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
140 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200141 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
142 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
143 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200144 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100145 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100146 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
147 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100148 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
149 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200150 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200151 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
152 .len = IEEE80211_MAX_DATA_LEN },
153 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200154 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200155 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300156 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200157 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300158
159 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
160 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200161 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400162};
163
Johannes Bergb9454e82009-07-08 13:29:08 +0200164/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000165static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200166 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200167 [NL80211_KEY_IDX] = { .type = NLA_U8 },
168 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
169 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
170 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
171 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
172};
173
Holger Schuriga0438972009-11-11 11:30:02 +0100174/* ifidx get helper */
175static int nl80211_get_ifidx(struct netlink_callback *cb)
176{
177 int res;
178
179 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
180 nl80211_fam.attrbuf, nl80211_fam.maxattr,
181 nl80211_policy);
182 if (res)
183 return res;
184
185 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
186 return -EINVAL;
187
188 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
189 if (!res)
190 return -EINVAL;
191 return res;
192}
193
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100194/* IE validation */
195static bool is_valid_ie_attr(const struct nlattr *attr)
196{
197 const u8 *pos;
198 int len;
199
200 if (!attr)
201 return true;
202
203 pos = nla_data(attr);
204 len = nla_len(attr);
205
206 while (len) {
207 u8 elemlen;
208
209 if (len < 2)
210 return false;
211 len -= 2;
212
213 elemlen = pos[1];
214 if (elemlen > len)
215 return false;
216
217 len -= elemlen;
218 pos += 2 + elemlen;
219 }
220
221 return true;
222}
223
Johannes Berg55682962007-09-20 13:09:35 -0400224/* message building helper */
225static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
226 int flags, u8 cmd)
227{
228 /* since there is no private header just add the generic one */
229 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
230}
231
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400232static int nl80211_msg_put_channel(struct sk_buff *msg,
233 struct ieee80211_channel *chan)
234{
235 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
236 chan->center_freq);
237
238 if (chan->flags & IEEE80211_CHAN_DISABLED)
239 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
240 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
241 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
242 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
243 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
244 if (chan->flags & IEEE80211_CHAN_RADAR)
245 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
246
247 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
248 DBM_TO_MBM(chan->max_power));
249
250 return 0;
251
252 nla_put_failure:
253 return -ENOBUFS;
254}
255
Johannes Berg55682962007-09-20 13:09:35 -0400256/* netlink command implementations */
257
Johannes Bergb9454e82009-07-08 13:29:08 +0200258struct key_parse {
259 struct key_params p;
260 int idx;
261 bool def, defmgmt;
262};
263
264static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
265{
266 struct nlattr *tb[NL80211_KEY_MAX + 1];
267 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
268 nl80211_key_policy);
269 if (err)
270 return err;
271
272 k->def = !!tb[NL80211_KEY_DEFAULT];
273 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
274
275 if (tb[NL80211_KEY_IDX])
276 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
277
278 if (tb[NL80211_KEY_DATA]) {
279 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
280 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
281 }
282
283 if (tb[NL80211_KEY_SEQ]) {
284 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
285 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
286 }
287
288 if (tb[NL80211_KEY_CIPHER])
289 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
290
291 return 0;
292}
293
294static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
295{
296 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
297 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
298 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
299 }
300
301 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
302 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
303 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
304 }
305
306 if (info->attrs[NL80211_ATTR_KEY_IDX])
307 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
308
309 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
310 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
311
312 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
313 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
314
315 return 0;
316}
317
318static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
319{
320 int err;
321
322 memset(k, 0, sizeof(*k));
323 k->idx = -1;
324
325 if (info->attrs[NL80211_ATTR_KEY])
326 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
327 else
328 err = nl80211_parse_key_old(info, k);
329
330 if (err)
331 return err;
332
333 if (k->def && k->defmgmt)
334 return -EINVAL;
335
336 if (k->idx != -1) {
337 if (k->defmgmt) {
338 if (k->idx < 4 || k->idx > 5)
339 return -EINVAL;
340 } else if (k->def) {
341 if (k->idx < 0 || k->idx > 3)
342 return -EINVAL;
343 } else {
344 if (k->idx < 0 || k->idx > 5)
345 return -EINVAL;
346 }
347 }
348
349 return 0;
350}
351
Johannes Bergfffd0932009-07-08 14:22:54 +0200352static struct cfg80211_cached_keys *
353nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
354 struct nlattr *keys)
355{
356 struct key_parse parse;
357 struct nlattr *key;
358 struct cfg80211_cached_keys *result;
359 int rem, err, def = 0;
360
361 result = kzalloc(sizeof(*result), GFP_KERNEL);
362 if (!result)
363 return ERR_PTR(-ENOMEM);
364
365 result->def = -1;
366 result->defmgmt = -1;
367
368 nla_for_each_nested(key, keys, rem) {
369 memset(&parse, 0, sizeof(parse));
370 parse.idx = -1;
371
372 err = nl80211_parse_key_new(key, &parse);
373 if (err)
374 goto error;
375 err = -EINVAL;
376 if (!parse.p.key)
377 goto error;
378 if (parse.idx < 0 || parse.idx > 4)
379 goto error;
380 if (parse.def) {
381 if (def)
382 goto error;
383 def = 1;
384 result->def = parse.idx;
385 } else if (parse.defmgmt)
386 goto error;
387 err = cfg80211_validate_key_settings(rdev, &parse.p,
388 parse.idx, NULL);
389 if (err)
390 goto error;
391 result->params[parse.idx].cipher = parse.p.cipher;
392 result->params[parse.idx].key_len = parse.p.key_len;
393 result->params[parse.idx].key = result->data[parse.idx];
394 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
395 }
396
397 return result;
398 error:
399 kfree(result);
400 return ERR_PTR(err);
401}
402
403static int nl80211_key_allowed(struct wireless_dev *wdev)
404{
405 ASSERT_WDEV_LOCK(wdev);
406
407 if (!netif_running(wdev->netdev))
408 return -ENETDOWN;
409
410 switch (wdev->iftype) {
411 case NL80211_IFTYPE_AP:
412 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200413 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200414 break;
415 case NL80211_IFTYPE_ADHOC:
416 if (!wdev->current_bss)
417 return -ENOLINK;
418 break;
419 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200420 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200421 if (wdev->sme_state != CFG80211_SME_CONNECTED)
422 return -ENOLINK;
423 break;
424 default:
425 return -EINVAL;
426 }
427
428 return 0;
429}
430
Johannes Berg55682962007-09-20 13:09:35 -0400431static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
432 struct cfg80211_registered_device *dev)
433{
434 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100435 struct nlattr *nl_bands, *nl_band;
436 struct nlattr *nl_freqs, *nl_freq;
437 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700438 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100439 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100440 enum ieee80211_band band;
441 struct ieee80211_channel *chan;
442 struct ieee80211_rate *rate;
443 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700444 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200445 const struct ieee80211_txrx_stypes *mgmt_stypes =
446 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400447
448 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
449 if (!hdr)
450 return -1;
451
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400453 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200454
Johannes Bergf5ea9122009-08-07 16:17:38 +0200455 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
456 cfg80211_rdev_list_generation);
457
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200458 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
459 dev->wiphy.retry_short);
460 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
461 dev->wiphy.retry_long);
462 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
463 dev->wiphy.frag_threshold);
464 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
465 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100466 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
467 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200468
Johannes Berg2a519312009-02-10 21:25:55 +0100469 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
470 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200471 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
472 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100473
Johannes Berg25e47c12009-04-02 20:14:06 +0200474 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
475 sizeof(u32) * dev->wiphy.n_cipher_suites,
476 dev->wiphy.cipher_suites);
477
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100478 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
479 dev->wiphy.max_num_pmkids);
480
Johannes Bergc0692b82010-08-27 14:26:53 +0300481 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
482 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
483
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700484 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
485 if (!nl_modes)
486 goto nla_put_failure;
487
488 i = 0;
489 while (ifmodes) {
490 if (ifmodes & 1)
491 NLA_PUT_FLAG(msg, i);
492 ifmodes >>= 1;
493 i++;
494 }
495
496 nla_nest_end(msg, nl_modes);
497
Johannes Bergee688b002008-01-24 19:38:39 +0100498 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
499 if (!nl_bands)
500 goto nla_put_failure;
501
502 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
503 if (!dev->wiphy.bands[band])
504 continue;
505
506 nl_band = nla_nest_start(msg, band);
507 if (!nl_band)
508 goto nla_put_failure;
509
Johannes Bergd51626d2008-10-09 12:20:13 +0200510 /* add HT info */
511 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
512 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
513 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
514 &dev->wiphy.bands[band]->ht_cap.mcs);
515 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
516 dev->wiphy.bands[band]->ht_cap.cap);
517 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
518 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
519 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
520 dev->wiphy.bands[band]->ht_cap.ampdu_density);
521 }
522
Johannes Bergee688b002008-01-24 19:38:39 +0100523 /* add frequencies */
524 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
525 if (!nl_freqs)
526 goto nla_put_failure;
527
528 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
529 nl_freq = nla_nest_start(msg, i);
530 if (!nl_freq)
531 goto nla_put_failure;
532
533 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100534
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400535 if (nl80211_msg_put_channel(msg, chan))
536 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200537
Johannes Bergee688b002008-01-24 19:38:39 +0100538 nla_nest_end(msg, nl_freq);
539 }
540
541 nla_nest_end(msg, nl_freqs);
542
543 /* add bitrates */
544 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
545 if (!nl_rates)
546 goto nla_put_failure;
547
548 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
549 nl_rate = nla_nest_start(msg, i);
550 if (!nl_rate)
551 goto nla_put_failure;
552
553 rate = &dev->wiphy.bands[band]->bitrates[i];
554 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
555 rate->bitrate);
556 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
557 NLA_PUT_FLAG(msg,
558 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
559
560 nla_nest_end(msg, nl_rate);
561 }
562
563 nla_nest_end(msg, nl_rates);
564
565 nla_nest_end(msg, nl_band);
566 }
567 nla_nest_end(msg, nl_bands);
568
Johannes Berg8fdc6212009-03-14 09:34:01 +0100569 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
570 if (!nl_cmds)
571 goto nla_put_failure;
572
573 i = 0;
574#define CMD(op, n) \
575 do { \
576 if (dev->ops->op) { \
577 i++; \
578 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
579 } \
580 } while (0)
581
582 CMD(add_virtual_intf, NEW_INTERFACE);
583 CMD(change_virtual_intf, SET_INTERFACE);
584 CMD(add_key, NEW_KEY);
585 CMD(add_beacon, NEW_BEACON);
586 CMD(add_station, NEW_STATION);
587 CMD(add_mpath, NEW_MPATH);
588 CMD(set_mesh_params, SET_MESH_PARAMS);
589 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200590 CMD(auth, AUTHENTICATE);
591 CMD(assoc, ASSOCIATE);
592 CMD(deauth, DEAUTHENTICATE);
593 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200594 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100595 CMD(set_pmksa, SET_PMKSA);
596 CMD(del_pmksa, DEL_PMKSA);
597 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100598 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200599 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200600 CMD(mgmt_tx, FRAME);
Johannes Berg5be83de2009-11-19 00:56:28 +0100601 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200602 i++;
603 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
604 }
Johannes Bergf444de02010-05-05 15:25:02 +0200605 CMD(set_channel, SET_CHANNEL);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100606
607#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200608
Johannes Berg6829c872009-07-02 09:13:27 +0200609 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200610 i++;
611 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
612 }
613
Johannes Berg6829c872009-07-02 09:13:27 +0200614 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200615 i++;
616 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
617 }
618
Johannes Berg8fdc6212009-03-14 09:34:01 +0100619 nla_nest_end(msg, nl_cmds);
620
Johannes Berg2e161f72010-08-12 15:38:38 +0200621 if (mgmt_stypes) {
622 u16 stypes;
623 struct nlattr *nl_ftypes, *nl_ifs;
624 enum nl80211_iftype ift;
625
626 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
627 if (!nl_ifs)
628 goto nla_put_failure;
629
630 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
631 nl_ftypes = nla_nest_start(msg, ift);
632 if (!nl_ftypes)
633 goto nla_put_failure;
634 i = 0;
635 stypes = mgmt_stypes[ift].tx;
636 while (stypes) {
637 if (stypes & 1)
638 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
639 (i << 4) | IEEE80211_FTYPE_MGMT);
640 stypes >>= 1;
641 i++;
642 }
643 nla_nest_end(msg, nl_ftypes);
644 }
645
Johannes Berg74b70a42010-08-24 12:15:53 +0200646 nla_nest_end(msg, nl_ifs);
647
Johannes Berg2e161f72010-08-12 15:38:38 +0200648 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
649 if (!nl_ifs)
650 goto nla_put_failure;
651
652 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
653 nl_ftypes = nla_nest_start(msg, ift);
654 if (!nl_ftypes)
655 goto nla_put_failure;
656 i = 0;
657 stypes = mgmt_stypes[ift].rx;
658 while (stypes) {
659 if (stypes & 1)
660 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
661 (i << 4) | IEEE80211_FTYPE_MGMT);
662 stypes >>= 1;
663 i++;
664 }
665 nla_nest_end(msg, nl_ftypes);
666 }
667 nla_nest_end(msg, nl_ifs);
668 }
669
Johannes Berg55682962007-09-20 13:09:35 -0400670 return genlmsg_end(msg, hdr);
671
672 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700673 genlmsg_cancel(msg, hdr);
674 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400675}
676
677static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
678{
679 int idx = 0;
680 int start = cb->args[0];
681 struct cfg80211_registered_device *dev;
682
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500683 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200684 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200685 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
686 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200687 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400688 continue;
689 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
690 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200691 dev) < 0) {
692 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400693 break;
Julius Volzb4637272008-07-08 14:02:19 +0200694 }
Johannes Berg55682962007-09-20 13:09:35 -0400695 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500696 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400697
698 cb->args[0] = idx;
699
700 return skb->len;
701}
702
703static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
704{
705 struct sk_buff *msg;
706 struct cfg80211_registered_device *dev;
707
708 dev = cfg80211_get_dev_from_info(info);
709 if (IS_ERR(dev))
710 return PTR_ERR(dev);
711
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700712 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400713 if (!msg)
714 goto out_err;
715
716 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
717 goto out_free;
718
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200719 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400720
Johannes Berg134e6372009-07-10 09:51:34 +0000721 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400722
723 out_free:
724 nlmsg_free(msg);
725 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200726 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400727 return -ENOBUFS;
728}
729
Jouni Malinen31888482008-10-30 16:59:24 +0200730static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
731 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
732 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
733 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
734 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
735 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
736};
737
738static int parse_txq_params(struct nlattr *tb[],
739 struct ieee80211_txq_params *txq_params)
740{
741 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
742 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
743 !tb[NL80211_TXQ_ATTR_AIFS])
744 return -EINVAL;
745
746 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
747 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
748 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
749 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
750 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
751
752 return 0;
753}
754
Johannes Bergf444de02010-05-05 15:25:02 +0200755static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
756{
757 /*
758 * You can only set the channel explicitly for AP, mesh
759 * and WDS type interfaces; all others have their channel
760 * managed via their respective "establish a connection"
761 * command (connect, join, ...)
762 *
763 * Monitors are special as they are normally slaved to
764 * whatever else is going on, so they behave as though
765 * you tried setting the wiphy channel itself.
766 */
767 return !wdev ||
768 wdev->iftype == NL80211_IFTYPE_AP ||
769 wdev->iftype == NL80211_IFTYPE_WDS ||
770 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200771 wdev->iftype == NL80211_IFTYPE_MONITOR ||
772 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200773}
774
775static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
776 struct wireless_dev *wdev,
777 struct genl_info *info)
778{
779 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
780 u32 freq;
781 int result;
782
783 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
784 return -EINVAL;
785
786 if (!nl80211_can_set_dev_channel(wdev))
787 return -EOPNOTSUPP;
788
789 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
790 channel_type = nla_get_u32(info->attrs[
791 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
792 if (channel_type != NL80211_CHAN_NO_HT &&
793 channel_type != NL80211_CHAN_HT20 &&
794 channel_type != NL80211_CHAN_HT40PLUS &&
795 channel_type != NL80211_CHAN_HT40MINUS)
796 return -EINVAL;
797 }
798
799 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
800
801 mutex_lock(&rdev->devlist_mtx);
802 if (wdev) {
803 wdev_lock(wdev);
804 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
805 wdev_unlock(wdev);
806 } else {
807 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
808 }
809 mutex_unlock(&rdev->devlist_mtx);
810
811 return result;
812}
813
814static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
815{
816 struct cfg80211_registered_device *rdev;
817 struct net_device *netdev;
818 int result;
819
820 rtnl_lock();
821
822 result = get_rdev_dev_by_info_ifindex(info, &rdev, &netdev);
823 if (result)
824 goto unlock;
825
826 result = __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
827
828 unlock:
829 rtnl_unlock();
830
831 return result;
832}
833
Johannes Berg55682962007-09-20 13:09:35 -0400834static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
835{
836 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200837 struct net_device *netdev = NULL;
838 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400839 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200840 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200841 u32 changed;
842 u8 retry_short = 0, retry_long = 0;
843 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100844 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400845
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100846 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400847
Johannes Bergf444de02010-05-05 15:25:02 +0200848 /*
849 * Try to find the wiphy and netdev. Normally this
850 * function shouldn't need the netdev, but this is
851 * done for backward compatibility -- previously
852 * setting the channel was done per wiphy, but now
853 * it is per netdev. Previous userland like hostapd
854 * also passed a netdev to set_wiphy, so that it is
855 * possible to let that go to the right netdev!
856 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100857 mutex_lock(&cfg80211_mutex);
858
Johannes Bergf444de02010-05-05 15:25:02 +0200859 if (info->attrs[NL80211_ATTR_IFINDEX]) {
860 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
861
862 netdev = dev_get_by_index(genl_info_net(info), ifindex);
863 if (netdev && netdev->ieee80211_ptr) {
864 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
865 mutex_lock(&rdev->mtx);
866 } else
867 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100868 }
869
Johannes Bergf444de02010-05-05 15:25:02 +0200870 if (!netdev) {
871 rdev = __cfg80211_rdev_from_info(info);
872 if (IS_ERR(rdev)) {
873 mutex_unlock(&cfg80211_mutex);
874 result = PTR_ERR(rdev);
875 goto unlock;
876 }
877 wdev = NULL;
878 netdev = NULL;
879 result = 0;
880
881 mutex_lock(&rdev->mtx);
882 } else if (netif_running(netdev) &&
883 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
884 wdev = netdev->ieee80211_ptr;
885 else
886 wdev = NULL;
887
888 /*
889 * end workaround code, by now the rdev is available
890 * and locked, and wdev may or may not be NULL.
891 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100892
893 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200894 result = cfg80211_dev_rename(
895 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100896
897 mutex_unlock(&cfg80211_mutex);
898
899 if (result)
900 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400901
Jouni Malinen31888482008-10-30 16:59:24 +0200902 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
903 struct ieee80211_txq_params txq_params;
904 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
905
906 if (!rdev->ops->set_txq_params) {
907 result = -EOPNOTSUPP;
908 goto bad_res;
909 }
910
911 nla_for_each_nested(nl_txq_params,
912 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
913 rem_txq_params) {
914 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
915 nla_data(nl_txq_params),
916 nla_len(nl_txq_params),
917 txq_params_policy);
918 result = parse_txq_params(tb, &txq_params);
919 if (result)
920 goto bad_res;
921
922 result = rdev->ops->set_txq_params(&rdev->wiphy,
923 &txq_params);
924 if (result)
925 goto bad_res;
926 }
927 }
928
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200929 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +0200930 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200931 if (result)
932 goto bad_res;
933 }
934
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300935 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
936 enum nl80211_tx_power_setting type;
937 int idx, mbm = 0;
938
939 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +0200940 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300941 goto bad_res;
942 }
943
944 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
945 type = nla_get_u32(info->attrs[idx]);
946
947 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
948 (type != NL80211_TX_POWER_AUTOMATIC)) {
949 result = -EINVAL;
950 goto bad_res;
951 }
952
953 if (type != NL80211_TX_POWER_AUTOMATIC) {
954 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
955 mbm = nla_get_u32(info->attrs[idx]);
956 }
957
958 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
959 if (result)
960 goto bad_res;
961 }
962
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200963 changed = 0;
964
965 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
966 retry_short = nla_get_u8(
967 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
968 if (retry_short == 0) {
969 result = -EINVAL;
970 goto bad_res;
971 }
972 changed |= WIPHY_PARAM_RETRY_SHORT;
973 }
974
975 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
976 retry_long = nla_get_u8(
977 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
978 if (retry_long == 0) {
979 result = -EINVAL;
980 goto bad_res;
981 }
982 changed |= WIPHY_PARAM_RETRY_LONG;
983 }
984
985 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
986 frag_threshold = nla_get_u32(
987 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
988 if (frag_threshold < 256) {
989 result = -EINVAL;
990 goto bad_res;
991 }
992 if (frag_threshold != (u32) -1) {
993 /*
994 * Fragments (apart from the last one) are required to
995 * have even length. Make the fragmentation code
996 * simpler by stripping LSB should someone try to use
997 * odd threshold value.
998 */
999 frag_threshold &= ~0x1;
1000 }
1001 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1002 }
1003
1004 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1005 rts_threshold = nla_get_u32(
1006 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1007 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1008 }
1009
Lukáš Turek81077e82009-12-21 22:50:47 +01001010 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1011 coverage_class = nla_get_u8(
1012 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1013 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1014 }
1015
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001016 if (changed) {
1017 u8 old_retry_short, old_retry_long;
1018 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001019 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001020
1021 if (!rdev->ops->set_wiphy_params) {
1022 result = -EOPNOTSUPP;
1023 goto bad_res;
1024 }
1025
1026 old_retry_short = rdev->wiphy.retry_short;
1027 old_retry_long = rdev->wiphy.retry_long;
1028 old_frag_threshold = rdev->wiphy.frag_threshold;
1029 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001030 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001031
1032 if (changed & WIPHY_PARAM_RETRY_SHORT)
1033 rdev->wiphy.retry_short = retry_short;
1034 if (changed & WIPHY_PARAM_RETRY_LONG)
1035 rdev->wiphy.retry_long = retry_long;
1036 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1037 rdev->wiphy.frag_threshold = frag_threshold;
1038 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1039 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001040 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1041 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001042
1043 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1044 if (result) {
1045 rdev->wiphy.retry_short = old_retry_short;
1046 rdev->wiphy.retry_long = old_retry_long;
1047 rdev->wiphy.frag_threshold = old_frag_threshold;
1048 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001049 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001050 }
1051 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001052
Johannes Berg306d6112008-12-08 12:39:04 +01001053 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001054 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001055 if (netdev)
1056 dev_put(netdev);
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001057 unlock:
1058 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001059 return result;
1060}
1061
1062
1063static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001064 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001065 struct net_device *dev)
1066{
1067 void *hdr;
1068
1069 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1070 if (!hdr)
1071 return -1;
1072
1073 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001074 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001075 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001076 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001077
1078 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1079 rdev->devlist_generation ^
1080 (cfg80211_rdev_list_generation << 2));
1081
Johannes Berg55682962007-09-20 13:09:35 -04001082 return genlmsg_end(msg, hdr);
1083
1084 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001085 genlmsg_cancel(msg, hdr);
1086 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001087}
1088
1089static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1090{
1091 int wp_idx = 0;
1092 int if_idx = 0;
1093 int wp_start = cb->args[0];
1094 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001095 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001096 struct wireless_dev *wdev;
1097
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001098 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001099 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1100 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001101 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001102 if (wp_idx < wp_start) {
1103 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001104 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001105 }
Johannes Berg55682962007-09-20 13:09:35 -04001106 if_idx = 0;
1107
Johannes Bergf5ea9122009-08-07 16:17:38 +02001108 mutex_lock(&rdev->devlist_mtx);
1109 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001110 if (if_idx < if_start) {
1111 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001112 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001113 }
Johannes Berg55682962007-09-20 13:09:35 -04001114 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1115 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001116 rdev, wdev->netdev) < 0) {
1117 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001118 goto out;
1119 }
1120 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001121 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001122 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001123
1124 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001125 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001126 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001127 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001128
1129 cb->args[0] = wp_idx;
1130 cb->args[1] = if_idx;
1131
1132 return skb->len;
1133}
1134
1135static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1136{
1137 struct sk_buff *msg;
1138 struct cfg80211_registered_device *dev;
1139 struct net_device *netdev;
1140 int err;
1141
Johannes Berg463d0182009-07-14 00:33:35 +02001142 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001143 if (err)
1144 return err;
1145
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001146 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001147 if (!msg)
1148 goto out_err;
1149
Johannes Bergd7264052009-04-19 16:23:20 +02001150 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1151 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -04001152 goto out_free;
1153
1154 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001155 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -04001156
Johannes Berg134e6372009-07-10 09:51:34 +00001157 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001158
1159 out_free:
1160 nlmsg_free(msg);
1161 out_err:
1162 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001163 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -04001164 return -ENOBUFS;
1165}
1166
Michael Wu66f7ac52008-01-31 19:48:22 +01001167static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1168 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1169 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1170 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1171 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1172 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1173};
1174
1175static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1176{
1177 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1178 int flag;
1179
1180 *mntrflags = 0;
1181
1182 if (!nla)
1183 return -EINVAL;
1184
1185 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1186 nla, mntr_flags_policy))
1187 return -EINVAL;
1188
1189 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1190 if (flags[flag])
1191 *mntrflags |= (1<<flag);
1192
1193 return 0;
1194}
1195
Johannes Berg9bc383d2009-11-19 11:55:19 +01001196static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001197 struct net_device *netdev, u8 use_4addr,
1198 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001199{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001200 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001201 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001202 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001203 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001204 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001205
1206 switch (iftype) {
1207 case NL80211_IFTYPE_AP_VLAN:
1208 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1209 return 0;
1210 break;
1211 case NL80211_IFTYPE_STATION:
1212 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1213 return 0;
1214 break;
1215 default:
1216 break;
1217 }
1218
1219 return -EOPNOTSUPP;
1220}
1221
Johannes Berg55682962007-09-20 13:09:35 -04001222static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1223{
Johannes Berg79c97e92009-07-07 03:56:12 +02001224 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001225 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001226 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001227 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001228 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001229 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001230 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001231
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001232 memset(&params, 0, sizeof(params));
1233
Johannes Berg3b858752009-03-12 09:55:09 +01001234 rtnl_lock();
1235
Johannes Berg463d0182009-07-14 00:33:35 +02001236 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001237 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001238 goto unlock_rtnl;
1239
Johannes Berg04a773a2009-04-19 21:24:32 +02001240 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001241
Johannes Berg723b0382008-09-16 20:22:09 +02001242 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001243 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001244 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001245 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001246 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001247 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001248 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001249 }
Johannes Berg723b0382008-09-16 20:22:09 +02001250 }
1251
Johannes Berg92ffe052008-09-16 20:39:36 +02001252 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001253 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001254 err = -EINVAL;
1255 goto unlock;
1256 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001257 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1258 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001259 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001260 }
1261
Felix Fietkau8b787642009-11-10 18:53:10 +01001262 if (info->attrs[NL80211_ATTR_4ADDR]) {
1263 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1264 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001265 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001266 if (err)
1267 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001268 } else {
1269 params.use_4addr = -1;
1270 }
1271
Johannes Berg92ffe052008-09-16 20:39:36 +02001272 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001273 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001274 err = -EINVAL;
1275 goto unlock;
1276 }
1277 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1278 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001279 if (err)
1280 goto unlock;
1281
1282 flags = &_flags;
1283 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001284 }
Johannes Berg3b858752009-03-12 09:55:09 +01001285
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001286 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001287 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001288 else
1289 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001290
Johannes Berg9bc383d2009-11-19 11:55:19 +01001291 if (!err && params.use_4addr != -1)
1292 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1293
Johannes Berg55682962007-09-20 13:09:35 -04001294 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001295 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001296 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001297 unlock_rtnl:
1298 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001299 return err;
1300}
1301
1302static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1303{
Johannes Berg79c97e92009-07-07 03:56:12 +02001304 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001305 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001306 int err;
1307 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001308 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001309
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001310 memset(&params, 0, sizeof(params));
1311
Johannes Berg55682962007-09-20 13:09:35 -04001312 if (!info->attrs[NL80211_ATTR_IFNAME])
1313 return -EINVAL;
1314
1315 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1316 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1317 if (type > NL80211_IFTYPE_MAX)
1318 return -EINVAL;
1319 }
1320
Johannes Berg3b858752009-03-12 09:55:09 +01001321 rtnl_lock();
1322
Johannes Berg79c97e92009-07-07 03:56:12 +02001323 rdev = cfg80211_get_dev_from_info(info);
1324 if (IS_ERR(rdev)) {
1325 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001326 goto unlock_rtnl;
1327 }
Johannes Berg55682962007-09-20 13:09:35 -04001328
Johannes Berg79c97e92009-07-07 03:56:12 +02001329 if (!rdev->ops->add_virtual_intf ||
1330 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001331 err = -EOPNOTSUPP;
1332 goto unlock;
1333 }
1334
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001335 if (type == NL80211_IFTYPE_MESH_POINT &&
1336 info->attrs[NL80211_ATTR_MESH_ID]) {
1337 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1338 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1339 }
1340
Johannes Berg9bc383d2009-11-19 11:55:19 +01001341 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001342 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001343 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001344 if (err)
1345 goto unlock;
1346 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001347
Michael Wu66f7ac52008-01-31 19:48:22 +01001348 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1349 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1350 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001351 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001352 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001353 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001354
Johannes Berg55682962007-09-20 13:09:35 -04001355 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001356 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001357 unlock_rtnl:
1358 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001359 return err;
1360}
1361
1362static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1363{
Johannes Berg79c97e92009-07-07 03:56:12 +02001364 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001365 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001366 struct net_device *dev;
1367
Johannes Berg3b858752009-03-12 09:55:09 +01001368 rtnl_lock();
1369
Johannes Berg463d0182009-07-14 00:33:35 +02001370 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001371 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001372 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001373
Johannes Berg79c97e92009-07-07 03:56:12 +02001374 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001375 err = -EOPNOTSUPP;
1376 goto out;
1377 }
1378
Johannes Berg463d0182009-07-14 00:33:35 +02001379 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001380
1381 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001382 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001383 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001384 unlock_rtnl:
1385 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001386 return err;
1387}
1388
Johannes Berg41ade002007-12-19 02:03:29 +01001389struct get_key_cookie {
1390 struct sk_buff *msg;
1391 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001392 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001393};
1394
1395static void get_key_callback(void *c, struct key_params *params)
1396{
Johannes Bergb9454e82009-07-08 13:29:08 +02001397 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001398 struct get_key_cookie *cookie = c;
1399
1400 if (params->key)
1401 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1402 params->key_len, params->key);
1403
1404 if (params->seq)
1405 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1406 params->seq_len, params->seq);
1407
1408 if (params->cipher)
1409 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1410 params->cipher);
1411
Johannes Bergb9454e82009-07-08 13:29:08 +02001412 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1413 if (!key)
1414 goto nla_put_failure;
1415
1416 if (params->key)
1417 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1418 params->key_len, params->key);
1419
1420 if (params->seq)
1421 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1422 params->seq_len, params->seq);
1423
1424 if (params->cipher)
1425 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1426 params->cipher);
1427
1428 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1429
1430 nla_nest_end(cookie->msg, key);
1431
Johannes Berg41ade002007-12-19 02:03:29 +01001432 return;
1433 nla_put_failure:
1434 cookie->error = 1;
1435}
1436
1437static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1438{
Johannes Berg79c97e92009-07-07 03:56:12 +02001439 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001440 int err;
1441 struct net_device *dev;
1442 u8 key_idx = 0;
1443 u8 *mac_addr = NULL;
1444 struct get_key_cookie cookie = {
1445 .error = 0,
1446 };
1447 void *hdr;
1448 struct sk_buff *msg;
1449
1450 if (info->attrs[NL80211_ATTR_KEY_IDX])
1451 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1452
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001453 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001454 return -EINVAL;
1455
1456 if (info->attrs[NL80211_ATTR_MAC])
1457 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1458
Johannes Berg3b858752009-03-12 09:55:09 +01001459 rtnl_lock();
1460
Johannes Berg463d0182009-07-14 00:33:35 +02001461 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001462 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001463 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001464
Johannes Berg79c97e92009-07-07 03:56:12 +02001465 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001466 err = -EOPNOTSUPP;
1467 goto out;
1468 }
1469
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001471 if (!msg) {
1472 err = -ENOMEM;
1473 goto out;
1474 }
1475
1476 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1477 NL80211_CMD_NEW_KEY);
1478
1479 if (IS_ERR(hdr)) {
1480 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001481 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001482 }
1483
1484 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001485 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001486
1487 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1488 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1489 if (mac_addr)
1490 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1491
Johannes Berg79c97e92009-07-07 03:56:12 +02001492 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001493 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001494
1495 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001496 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001497
1498 if (cookie.error)
1499 goto nla_put_failure;
1500
1501 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001502 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001503 goto out;
1504
1505 nla_put_failure:
1506 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001507 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001508 nlmsg_free(msg);
1509 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001510 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001511 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001512 unlock_rtnl:
1513 rtnl_unlock();
1514
Johannes Berg41ade002007-12-19 02:03:29 +01001515 return err;
1516}
1517
1518static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1519{
Johannes Berg79c97e92009-07-07 03:56:12 +02001520 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001521 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001522 int err;
1523 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001524 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1525 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001526
Johannes Bergb9454e82009-07-08 13:29:08 +02001527 err = nl80211_parse_key(info, &key);
1528 if (err)
1529 return err;
1530
1531 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001532 return -EINVAL;
1533
Johannes Bergb9454e82009-07-08 13:29:08 +02001534 /* only support setting default key */
1535 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001536 return -EINVAL;
1537
Johannes Berg3b858752009-03-12 09:55:09 +01001538 rtnl_lock();
1539
Johannes Berg463d0182009-07-14 00:33:35 +02001540 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001541 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001542 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001543
Johannes Bergb9454e82009-07-08 13:29:08 +02001544 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001545 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001546 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001547 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001548
1549 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001550 err = -EOPNOTSUPP;
1551 goto out;
1552 }
1553
Johannes Bergfffd0932009-07-08 14:22:54 +02001554 wdev_lock(dev->ieee80211_ptr);
1555 err = nl80211_key_allowed(dev->ieee80211_ptr);
1556 if (!err)
1557 err = func(&rdev->wiphy, dev, key.idx);
1558
Johannes Berg3d23e342009-09-29 23:27:28 +02001559#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001560 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001561 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001562 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001563 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001564 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001565 }
1566#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001567 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001568
1569 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001570 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001571 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001572
1573 unlock_rtnl:
1574 rtnl_unlock();
1575
Johannes Berg41ade002007-12-19 02:03:29 +01001576 return err;
1577}
1578
1579static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1580{
Johannes Berg79c97e92009-07-07 03:56:12 +02001581 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001582 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001583 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001584 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001585 u8 *mac_addr = NULL;
1586
Johannes Bergb9454e82009-07-08 13:29:08 +02001587 err = nl80211_parse_key(info, &key);
1588 if (err)
1589 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001590
Johannes Bergb9454e82009-07-08 13:29:08 +02001591 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001592 return -EINVAL;
1593
Johannes Berg41ade002007-12-19 02:03:29 +01001594 if (info->attrs[NL80211_ATTR_MAC])
1595 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1596
Johannes Berg3b858752009-03-12 09:55:09 +01001597 rtnl_lock();
1598
Johannes Berg463d0182009-07-14 00:33:35 +02001599 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001600 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001601 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001602
Johannes Berg79c97e92009-07-07 03:56:12 +02001603 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001604 err = -EOPNOTSUPP;
1605 goto out;
1606 }
1607
Johannes Bergfffd0932009-07-08 14:22:54 +02001608 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1609 err = -EINVAL;
1610 goto out;
1611 }
1612
1613 wdev_lock(dev->ieee80211_ptr);
1614 err = nl80211_key_allowed(dev->ieee80211_ptr);
1615 if (!err)
1616 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1617 mac_addr, &key.p);
1618 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001619
1620 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001621 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001622 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001623 unlock_rtnl:
1624 rtnl_unlock();
1625
Johannes Berg41ade002007-12-19 02:03:29 +01001626 return err;
1627}
1628
1629static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1630{
Johannes Berg79c97e92009-07-07 03:56:12 +02001631 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001632 int err;
1633 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001634 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001635 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001636
Johannes Bergb9454e82009-07-08 13:29:08 +02001637 err = nl80211_parse_key(info, &key);
1638 if (err)
1639 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001640
1641 if (info->attrs[NL80211_ATTR_MAC])
1642 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1643
Johannes Berg3b858752009-03-12 09:55:09 +01001644 rtnl_lock();
1645
Johannes Berg463d0182009-07-14 00:33:35 +02001646 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001647 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001648 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001649
Johannes Berg79c97e92009-07-07 03:56:12 +02001650 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001651 err = -EOPNOTSUPP;
1652 goto out;
1653 }
1654
Johannes Bergfffd0932009-07-08 14:22:54 +02001655 wdev_lock(dev->ieee80211_ptr);
1656 err = nl80211_key_allowed(dev->ieee80211_ptr);
1657 if (!err)
1658 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001659
Johannes Berg3d23e342009-09-29 23:27:28 +02001660#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001661 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001662 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001663 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001664 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001665 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1666 }
1667#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001668 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001669
Johannes Berg41ade002007-12-19 02:03:29 +01001670 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001671 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001672 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001673
1674 unlock_rtnl:
1675 rtnl_unlock();
1676
Johannes Berg41ade002007-12-19 02:03:29 +01001677 return err;
1678}
1679
Johannes Berged1b6cc2007-12-19 02:03:32 +01001680static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1681{
1682 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1683 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001684 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001685 int err;
1686 struct net_device *dev;
1687 struct beacon_parameters params;
1688 int haveinfo = 0;
1689
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001690 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1691 return -EINVAL;
1692
Johannes Berg3b858752009-03-12 09:55:09 +01001693 rtnl_lock();
1694
Johannes Berg463d0182009-07-14 00:33:35 +02001695 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001696 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001697 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001698
Johannes Berg074ac8d2010-09-16 14:58:22 +02001699 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1700 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001701 err = -EOPNOTSUPP;
1702 goto out;
1703 }
1704
Johannes Berged1b6cc2007-12-19 02:03:32 +01001705 switch (info->genlhdr->cmd) {
1706 case NL80211_CMD_NEW_BEACON:
1707 /* these are required for NEW_BEACON */
1708 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1709 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1710 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1711 err = -EINVAL;
1712 goto out;
1713 }
1714
Johannes Berg79c97e92009-07-07 03:56:12 +02001715 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001716 break;
1717 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001718 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001719 break;
1720 default:
1721 WARN_ON(1);
1722 err = -EOPNOTSUPP;
1723 goto out;
1724 }
1725
1726 if (!call) {
1727 err = -EOPNOTSUPP;
1728 goto out;
1729 }
1730
1731 memset(&params, 0, sizeof(params));
1732
1733 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1734 params.interval =
1735 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1736 haveinfo = 1;
1737 }
1738
1739 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1740 params.dtim_period =
1741 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1742 haveinfo = 1;
1743 }
1744
1745 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1746 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1747 params.head_len =
1748 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1749 haveinfo = 1;
1750 }
1751
1752 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1753 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1754 params.tail_len =
1755 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1756 haveinfo = 1;
1757 }
1758
1759 if (!haveinfo) {
1760 err = -EINVAL;
1761 goto out;
1762 }
1763
Johannes Berg79c97e92009-07-07 03:56:12 +02001764 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001765
1766 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001767 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001768 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001769 unlock_rtnl:
1770 rtnl_unlock();
1771
Johannes Berged1b6cc2007-12-19 02:03:32 +01001772 return err;
1773}
1774
1775static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1776{
Johannes Berg79c97e92009-07-07 03:56:12 +02001777 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001778 int err;
1779 struct net_device *dev;
1780
Johannes Berg3b858752009-03-12 09:55:09 +01001781 rtnl_lock();
1782
Johannes Berg463d0182009-07-14 00:33:35 +02001783 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001784 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001785 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001786
Johannes Berg79c97e92009-07-07 03:56:12 +02001787 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001788 err = -EOPNOTSUPP;
1789 goto out;
1790 }
1791
Johannes Berg074ac8d2010-09-16 14:58:22 +02001792 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1793 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001794 err = -EOPNOTSUPP;
1795 goto out;
1796 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001797 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001798
1799 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001800 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001801 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001802 unlock_rtnl:
1803 rtnl_unlock();
1804
Johannes Berged1b6cc2007-12-19 02:03:32 +01001805 return err;
1806}
1807
Johannes Berg5727ef12007-12-19 02:03:34 +01001808static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1809 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1810 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1811 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001812 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001813};
1814
Johannes Bergeccb8e82009-05-11 21:57:56 +03001815static int parse_station_flags(struct genl_info *info,
1816 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001817{
1818 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001819 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001820 int flag;
1821
Johannes Bergeccb8e82009-05-11 21:57:56 +03001822 /*
1823 * Try parsing the new attribute first so userspace
1824 * can specify both for older kernels.
1825 */
1826 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1827 if (nla) {
1828 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001829
Johannes Bergeccb8e82009-05-11 21:57:56 +03001830 sta_flags = nla_data(nla);
1831 params->sta_flags_mask = sta_flags->mask;
1832 params->sta_flags_set = sta_flags->set;
1833 if ((params->sta_flags_mask |
1834 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1835 return -EINVAL;
1836 return 0;
1837 }
1838
1839 /* if present, parse the old attribute */
1840
1841 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001842 if (!nla)
1843 return 0;
1844
1845 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1846 nla, sta_flags_policy))
1847 return -EINVAL;
1848
Johannes Bergeccb8e82009-05-11 21:57:56 +03001849 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1850 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001851
1852 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1853 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001854 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001855
1856 return 0;
1857}
1858
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001859static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1860 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001861 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001862{
1863 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001864 struct nlattr *sinfoattr, *txrate;
1865 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001866
1867 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1868 if (!hdr)
1869 return -1;
1870
1871 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1872 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1873
Johannes Bergf5ea9122009-08-07 16:17:38 +02001874 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1875
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001876 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1877 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001878 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001879 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1880 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1881 sinfo->inactive_time);
1882 if (sinfo->filled & STATION_INFO_RX_BYTES)
1883 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1884 sinfo->rx_bytes);
1885 if (sinfo->filled & STATION_INFO_TX_BYTES)
1886 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1887 sinfo->tx_bytes);
1888 if (sinfo->filled & STATION_INFO_LLID)
1889 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1890 sinfo->llid);
1891 if (sinfo->filled & STATION_INFO_PLID)
1892 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1893 sinfo->plid);
1894 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1895 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1896 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001897 if (sinfo->filled & STATION_INFO_SIGNAL)
1898 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1899 sinfo->signal);
1900 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1901 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1902 if (!txrate)
1903 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001904
John W. Linville254416a2009-12-09 16:43:52 -05001905 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1906 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001907 if (bitrate > 0)
1908 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1909
1910 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1911 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1912 sinfo->txrate.mcs);
1913 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1914 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1915 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1916 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1917
1918 nla_nest_end(msg, txrate);
1919 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001920 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1921 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1922 sinfo->rx_packets);
1923 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1924 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1925 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001926 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001927
1928 return genlmsg_end(msg, hdr);
1929
1930 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001931 genlmsg_cancel(msg, hdr);
1932 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001933}
1934
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001935static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001936 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001937{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001938 struct station_info sinfo;
1939 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001940 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001941 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001942 int ifidx = cb->args[0];
1943 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001944 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001945
Holger Schuriga0438972009-11-11 11:30:02 +01001946 if (!ifidx)
1947 ifidx = nl80211_get_ifidx(cb);
1948 if (ifidx < 0)
1949 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001950
Johannes Berg3b858752009-03-12 09:55:09 +01001951 rtnl_lock();
1952
Johannes Berg463d0182009-07-14 00:33:35 +02001953 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001954 if (!netdev) {
1955 err = -ENODEV;
1956 goto out_rtnl;
1957 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001958
Johannes Berg463d0182009-07-14 00:33:35 +02001959 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001960 if (IS_ERR(dev)) {
1961 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001962 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001963 }
1964
1965 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001966 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001967 goto out_err;
1968 }
1969
Johannes Bergbba95fe2008-07-29 13:22:51 +02001970 while (1) {
1971 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1972 mac_addr, &sinfo);
1973 if (err == -ENOENT)
1974 break;
1975 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001976 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001977
1978 if (nl80211_send_station(skb,
1979 NETLINK_CB(cb->skb).pid,
1980 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1981 netdev, mac_addr,
1982 &sinfo) < 0)
1983 goto out;
1984
1985 sta_idx++;
1986 }
1987
1988
1989 out:
1990 cb->args[1] = sta_idx;
1991 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001992 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001993 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001994 out_rtnl:
1995 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001996
1997 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001998}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001999
Johannes Berg5727ef12007-12-19 02:03:34 +01002000static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2001{
Johannes Berg79c97e92009-07-07 03:56:12 +02002002 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002003 int err;
2004 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002005 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002006 struct sk_buff *msg;
2007 u8 *mac_addr = NULL;
2008
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002009 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002010
2011 if (!info->attrs[NL80211_ATTR_MAC])
2012 return -EINVAL;
2013
2014 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2015
Johannes Berg3b858752009-03-12 09:55:09 +01002016 rtnl_lock();
2017
Johannes Berg463d0182009-07-14 00:33:35 +02002018 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002019 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002020 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002021
Johannes Berg79c97e92009-07-07 03:56:12 +02002022 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002023 err = -EOPNOTSUPP;
2024 goto out;
2025 }
2026
Johannes Berg79c97e92009-07-07 03:56:12 +02002027 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002028 if (err)
2029 goto out;
2030
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002032 if (!msg)
2033 goto out;
2034
2035 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002036 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002037 goto out_free;
2038
Johannes Berg134e6372009-07-10 09:51:34 +00002039 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002040 goto out;
2041
2042 out_free:
2043 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002044 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002045 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002046 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002047 out_rtnl:
2048 rtnl_unlock();
2049
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002050 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01002051}
2052
2053/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002054 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002055 */
Johannes Berg463d0182009-07-14 00:33:35 +02002056static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002057 struct cfg80211_registered_device *rdev,
2058 struct net_device **vlan)
2059{
Johannes Berg463d0182009-07-14 00:33:35 +02002060 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002061 *vlan = NULL;
2062
2063 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002064 *vlan = dev_get_by_index(genl_info_net(info),
2065 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002066 if (!*vlan)
2067 return -ENODEV;
2068 if (!(*vlan)->ieee80211_ptr)
2069 return -EINVAL;
2070 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2071 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002072 if (!netif_running(*vlan))
2073 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002074 }
2075 return 0;
2076}
2077
2078static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2079{
Johannes Berg79c97e92009-07-07 03:56:12 +02002080 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002081 int err;
2082 struct net_device *dev;
2083 struct station_parameters params;
2084 u8 *mac_addr = NULL;
2085
2086 memset(&params, 0, sizeof(params));
2087
2088 params.listen_interval = -1;
2089
2090 if (info->attrs[NL80211_ATTR_STA_AID])
2091 return -EINVAL;
2092
2093 if (!info->attrs[NL80211_ATTR_MAC])
2094 return -EINVAL;
2095
2096 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2097
2098 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2099 params.supported_rates =
2100 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2101 params.supported_rates_len =
2102 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2103 }
2104
2105 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2106 params.listen_interval =
2107 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2108
Jouni Malinen36aedc92008-08-25 11:58:58 +03002109 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2110 params.ht_capa =
2111 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2112
Johannes Bergeccb8e82009-05-11 21:57:56 +03002113 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002114 return -EINVAL;
2115
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002116 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2117 params.plink_action =
2118 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2119
Johannes Berg3b858752009-03-12 09:55:09 +01002120 rtnl_lock();
2121
Johannes Berg463d0182009-07-14 00:33:35 +02002122 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002123 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002124 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002125
Johannes Berg463d0182009-07-14 00:33:35 +02002126 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002127 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002128 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002129
2130 /* validate settings */
2131 err = 0;
2132
2133 switch (dev->ieee80211_ptr->iftype) {
2134 case NL80211_IFTYPE_AP:
2135 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002136 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002137 /* disallow mesh-specific things */
2138 if (params.plink_action)
2139 err = -EINVAL;
2140 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002141 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002142 case NL80211_IFTYPE_STATION:
2143 /* disallow everything but AUTHORIZED flag */
2144 if (params.plink_action)
2145 err = -EINVAL;
2146 if (params.vlan)
2147 err = -EINVAL;
2148 if (params.supported_rates)
2149 err = -EINVAL;
2150 if (params.ht_capa)
2151 err = -EINVAL;
2152 if (params.listen_interval >= 0)
2153 err = -EINVAL;
2154 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2155 err = -EINVAL;
2156 break;
2157 case NL80211_IFTYPE_MESH_POINT:
2158 /* disallow things mesh doesn't support */
2159 if (params.vlan)
2160 err = -EINVAL;
2161 if (params.ht_capa)
2162 err = -EINVAL;
2163 if (params.listen_interval >= 0)
2164 err = -EINVAL;
2165 if (params.supported_rates)
2166 err = -EINVAL;
2167 if (params.sta_flags_mask)
2168 err = -EINVAL;
2169 break;
2170 default:
2171 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002172 }
2173
Johannes Berg5727ef12007-12-19 02:03:34 +01002174 if (err)
2175 goto out;
2176
Johannes Berg79c97e92009-07-07 03:56:12 +02002177 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002178 err = -EOPNOTSUPP;
2179 goto out;
2180 }
2181
Johannes Berg79c97e92009-07-07 03:56:12 +02002182 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002183
2184 out:
2185 if (params.vlan)
2186 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002187 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002188 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002189 out_rtnl:
2190 rtnl_unlock();
2191
Johannes Berg5727ef12007-12-19 02:03:34 +01002192 return err;
2193}
2194
2195static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2196{
Johannes Berg79c97e92009-07-07 03:56:12 +02002197 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002198 int err;
2199 struct net_device *dev;
2200 struct station_parameters params;
2201 u8 *mac_addr = NULL;
2202
2203 memset(&params, 0, sizeof(params));
2204
2205 if (!info->attrs[NL80211_ATTR_MAC])
2206 return -EINVAL;
2207
Johannes Berg5727ef12007-12-19 02:03:34 +01002208 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2209 return -EINVAL;
2210
2211 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2212 return -EINVAL;
2213
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002214 if (!info->attrs[NL80211_ATTR_STA_AID])
2215 return -EINVAL;
2216
Johannes Berg5727ef12007-12-19 02:03:34 +01002217 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2218 params.supported_rates =
2219 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2220 params.supported_rates_len =
2221 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2222 params.listen_interval =
2223 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002224
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002225 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2226 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2227 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002228
Jouni Malinen36aedc92008-08-25 11:58:58 +03002229 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2230 params.ht_capa =
2231 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002232
Johannes Bergeccb8e82009-05-11 21:57:56 +03002233 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002234 return -EINVAL;
2235
Johannes Berg3b858752009-03-12 09:55:09 +01002236 rtnl_lock();
2237
Johannes Berg463d0182009-07-14 00:33:35 +02002238 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002239 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002240 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002241
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002242 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002243 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2244 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002245 err = -EINVAL;
2246 goto out;
2247 }
2248
Johannes Berg463d0182009-07-14 00:33:35 +02002249 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002250 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002251 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002252
2253 /* validate settings */
2254 err = 0;
2255
Johannes Berg79c97e92009-07-07 03:56:12 +02002256 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002257 err = -EOPNOTSUPP;
2258 goto out;
2259 }
2260
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002261 if (!netif_running(dev)) {
2262 err = -ENETDOWN;
2263 goto out;
2264 }
2265
Johannes Berg79c97e92009-07-07 03:56:12 +02002266 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002267
2268 out:
2269 if (params.vlan)
2270 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002271 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002272 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002273 out_rtnl:
2274 rtnl_unlock();
2275
Johannes Berg5727ef12007-12-19 02:03:34 +01002276 return err;
2277}
2278
2279static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2280{
Johannes Berg79c97e92009-07-07 03:56:12 +02002281 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002282 int err;
2283 struct net_device *dev;
2284 u8 *mac_addr = NULL;
2285
2286 if (info->attrs[NL80211_ATTR_MAC])
2287 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2288
Johannes Berg3b858752009-03-12 09:55:09 +01002289 rtnl_lock();
2290
Johannes Berg463d0182009-07-14 00:33:35 +02002291 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002292 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002293 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002294
Johannes Berge80cf852009-05-11 14:43:13 +02002295 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002296 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002297 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2298 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
Johannes Berge80cf852009-05-11 14:43:13 +02002299 err = -EINVAL;
2300 goto out;
2301 }
2302
Johannes Berg79c97e92009-07-07 03:56:12 +02002303 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002304 err = -EOPNOTSUPP;
2305 goto out;
2306 }
2307
Johannes Berg79c97e92009-07-07 03:56:12 +02002308 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002309
2310 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002311 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002312 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002313 out_rtnl:
2314 rtnl_unlock();
2315
Johannes Berg5727ef12007-12-19 02:03:34 +01002316 return err;
2317}
2318
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2320 int flags, struct net_device *dev,
2321 u8 *dst, u8 *next_hop,
2322 struct mpath_info *pinfo)
2323{
2324 void *hdr;
2325 struct nlattr *pinfoattr;
2326
2327 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2328 if (!hdr)
2329 return -1;
2330
2331 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2332 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2333 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2334
Johannes Bergf5ea9122009-08-07 16:17:38 +02002335 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2336
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2338 if (!pinfoattr)
2339 goto nla_put_failure;
2340 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2341 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2342 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002343 if (pinfo->filled & MPATH_INFO_SN)
2344 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2345 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002346 if (pinfo->filled & MPATH_INFO_METRIC)
2347 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2348 pinfo->metric);
2349 if (pinfo->filled & MPATH_INFO_EXPTIME)
2350 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2351 pinfo->exptime);
2352 if (pinfo->filled & MPATH_INFO_FLAGS)
2353 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2354 pinfo->flags);
2355 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2356 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2357 pinfo->discovery_timeout);
2358 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2359 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2360 pinfo->discovery_retries);
2361
2362 nla_nest_end(msg, pinfoattr);
2363
2364 return genlmsg_end(msg, hdr);
2365
2366 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002367 genlmsg_cancel(msg, hdr);
2368 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002369}
2370
2371static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002372 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002373{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002374 struct mpath_info pinfo;
2375 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002376 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002377 u8 dst[ETH_ALEN];
2378 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002379 int ifidx = cb->args[0];
2380 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002381 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002382
Holger Schuriga0438972009-11-11 11:30:02 +01002383 if (!ifidx)
2384 ifidx = nl80211_get_ifidx(cb);
2385 if (ifidx < 0)
2386 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387
Johannes Berg3b858752009-03-12 09:55:09 +01002388 rtnl_lock();
2389
Johannes Berg463d0182009-07-14 00:33:35 +02002390 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002391 if (!netdev) {
2392 err = -ENODEV;
2393 goto out_rtnl;
2394 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002395
Johannes Berg463d0182009-07-14 00:33:35 +02002396 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002397 if (IS_ERR(dev)) {
2398 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002399 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002400 }
2401
2402 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002403 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002404 goto out_err;
2405 }
2406
Jouni Malineneec60b02009-03-20 21:21:19 +02002407 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2408 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002409 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002410 }
2411
Johannes Bergbba95fe2008-07-29 13:22:51 +02002412 while (1) {
2413 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2414 dst, next_hop, &pinfo);
2415 if (err == -ENOENT)
2416 break;
2417 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002418 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002419
2420 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2421 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2422 netdev, dst, next_hop,
2423 &pinfo) < 0)
2424 goto out;
2425
2426 path_idx++;
2427 }
2428
2429
2430 out:
2431 cb->args[1] = path_idx;
2432 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002433 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002434 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002435 out_rtnl:
2436 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002437
2438 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002439}
2440
2441static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2442{
Johannes Berg79c97e92009-07-07 03:56:12 +02002443 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002444 int err;
2445 struct net_device *dev;
2446 struct mpath_info pinfo;
2447 struct sk_buff *msg;
2448 u8 *dst = NULL;
2449 u8 next_hop[ETH_ALEN];
2450
2451 memset(&pinfo, 0, sizeof(pinfo));
2452
2453 if (!info->attrs[NL80211_ATTR_MAC])
2454 return -EINVAL;
2455
2456 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2457
Johannes Berg3b858752009-03-12 09:55:09 +01002458 rtnl_lock();
2459
Johannes Berg463d0182009-07-14 00:33:35 +02002460 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002461 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002462 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002463
Johannes Berg79c97e92009-07-07 03:56:12 +02002464 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002465 err = -EOPNOTSUPP;
2466 goto out;
2467 }
2468
Jouni Malineneec60b02009-03-20 21:21:19 +02002469 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2470 err = -EOPNOTSUPP;
2471 goto out;
2472 }
2473
Johannes Berg79c97e92009-07-07 03:56:12 +02002474 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 if (err)
2476 goto out;
2477
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002479 if (!msg)
2480 goto out;
2481
2482 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2483 dev, dst, next_hop, &pinfo) < 0)
2484 goto out_free;
2485
Johannes Berg134e6372009-07-10 09:51:34 +00002486 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002487 goto out;
2488
2489 out_free:
2490 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002491 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002492 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002493 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002494 out_rtnl:
2495 rtnl_unlock();
2496
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002497 return err;
2498}
2499
2500static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2501{
Johannes Berg79c97e92009-07-07 03:56:12 +02002502 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 int err;
2504 struct net_device *dev;
2505 u8 *dst = NULL;
2506 u8 *next_hop = NULL;
2507
2508 if (!info->attrs[NL80211_ATTR_MAC])
2509 return -EINVAL;
2510
2511 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2512 return -EINVAL;
2513
2514 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2515 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2516
Johannes Berg3b858752009-03-12 09:55:09 +01002517 rtnl_lock();
2518
Johannes Berg463d0182009-07-14 00:33:35 +02002519 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002520 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002521 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002522
Johannes Berg79c97e92009-07-07 03:56:12 +02002523 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002524 err = -EOPNOTSUPP;
2525 goto out;
2526 }
2527
Jouni Malineneec60b02009-03-20 21:21:19 +02002528 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2529 err = -EOPNOTSUPP;
2530 goto out;
2531 }
2532
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002533 if (!netif_running(dev)) {
2534 err = -ENETDOWN;
2535 goto out;
2536 }
2537
Johannes Berg79c97e92009-07-07 03:56:12 +02002538 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002539
2540 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002541 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002542 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002543 out_rtnl:
2544 rtnl_unlock();
2545
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002546 return err;
2547}
2548static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2549{
Johannes Berg79c97e92009-07-07 03:56:12 +02002550 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002551 int err;
2552 struct net_device *dev;
2553 u8 *dst = NULL;
2554 u8 *next_hop = NULL;
2555
2556 if (!info->attrs[NL80211_ATTR_MAC])
2557 return -EINVAL;
2558
2559 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2560 return -EINVAL;
2561
2562 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2563 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2564
Johannes Berg3b858752009-03-12 09:55:09 +01002565 rtnl_lock();
2566
Johannes Berg463d0182009-07-14 00:33:35 +02002567 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002568 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002569 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002570
Johannes Berg79c97e92009-07-07 03:56:12 +02002571 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002572 err = -EOPNOTSUPP;
2573 goto out;
2574 }
2575
Jouni Malineneec60b02009-03-20 21:21:19 +02002576 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2577 err = -EOPNOTSUPP;
2578 goto out;
2579 }
2580
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002581 if (!netif_running(dev)) {
2582 err = -ENETDOWN;
2583 goto out;
2584 }
2585
Johannes Berg79c97e92009-07-07 03:56:12 +02002586 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002587
2588 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002589 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002590 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002591 out_rtnl:
2592 rtnl_unlock();
2593
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002594 return err;
2595}
2596
2597static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2598{
Johannes Berg79c97e92009-07-07 03:56:12 +02002599 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002600 int err;
2601 struct net_device *dev;
2602 u8 *dst = NULL;
2603
2604 if (info->attrs[NL80211_ATTR_MAC])
2605 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2606
Johannes Berg3b858752009-03-12 09:55:09 +01002607 rtnl_lock();
2608
Johannes Berg463d0182009-07-14 00:33:35 +02002609 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002610 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002611 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002612
Johannes Berg79c97e92009-07-07 03:56:12 +02002613 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002614 err = -EOPNOTSUPP;
2615 goto out;
2616 }
2617
Johannes Berg79c97e92009-07-07 03:56:12 +02002618 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002619
2620 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002621 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002622 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002623 out_rtnl:
2624 rtnl_unlock();
2625
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002626 return err;
2627}
2628
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002629static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2630{
Johannes Berg79c97e92009-07-07 03:56:12 +02002631 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002632 int err;
2633 struct net_device *dev;
2634 struct bss_parameters params;
2635
2636 memset(&params, 0, sizeof(params));
2637 /* default to not changing parameters */
2638 params.use_cts_prot = -1;
2639 params.use_short_preamble = -1;
2640 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002641 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002642
2643 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2644 params.use_cts_prot =
2645 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2646 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2647 params.use_short_preamble =
2648 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2649 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2650 params.use_short_slot_time =
2651 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002652 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2653 params.basic_rates =
2654 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2655 params.basic_rates_len =
2656 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2657 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002658 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2659 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002660
Johannes Berg3b858752009-03-12 09:55:09 +01002661 rtnl_lock();
2662
Johannes Berg463d0182009-07-14 00:33:35 +02002663 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002664 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002665 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002666
Johannes Berg79c97e92009-07-07 03:56:12 +02002667 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002668 err = -EOPNOTSUPP;
2669 goto out;
2670 }
2671
Johannes Berg074ac8d2010-09-16 14:58:22 +02002672 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2673 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002674 err = -EOPNOTSUPP;
2675 goto out;
2676 }
2677
Johannes Berg79c97e92009-07-07 03:56:12 +02002678 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002679
2680 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002681 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002682 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002683 out_rtnl:
2684 rtnl_unlock();
2685
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002686 return err;
2687}
2688
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002689static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002690 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2691 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2692 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2693 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2694 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2695 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2696};
2697
2698static int parse_reg_rule(struct nlattr *tb[],
2699 struct ieee80211_reg_rule *reg_rule)
2700{
2701 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2702 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2703
2704 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2705 return -EINVAL;
2706 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2707 return -EINVAL;
2708 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2709 return -EINVAL;
2710 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2711 return -EINVAL;
2712 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2713 return -EINVAL;
2714
2715 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2716
2717 freq_range->start_freq_khz =
2718 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2719 freq_range->end_freq_khz =
2720 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2721 freq_range->max_bandwidth_khz =
2722 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2723
2724 power_rule->max_eirp =
2725 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2726
2727 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2728 power_rule->max_antenna_gain =
2729 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2730
2731 return 0;
2732}
2733
2734static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2735{
2736 int r;
2737 char *data = NULL;
2738
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002739 /*
2740 * You should only get this when cfg80211 hasn't yet initialized
2741 * completely when built-in to the kernel right between the time
2742 * window between nl80211_init() and regulatory_init(), if that is
2743 * even possible.
2744 */
2745 mutex_lock(&cfg80211_mutex);
2746 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002747 mutex_unlock(&cfg80211_mutex);
2748 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002749 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002750 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002751
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002752 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2753 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002754
2755 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2756
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002757 r = regulatory_hint_user(data);
2758
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002759 return r;
2760}
2761
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002762static int nl80211_get_mesh_params(struct sk_buff *skb,
2763 struct genl_info *info)
2764{
Johannes Berg79c97e92009-07-07 03:56:12 +02002765 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002766 struct mesh_config cur_params;
2767 int err;
2768 struct net_device *dev;
2769 void *hdr;
2770 struct nlattr *pinfoattr;
2771 struct sk_buff *msg;
2772
Johannes Berg3b858752009-03-12 09:55:09 +01002773 rtnl_lock();
2774
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002775 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002776 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002777 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002778 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002779
Johannes Berg79c97e92009-07-07 03:56:12 +02002780 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002781 err = -EOPNOTSUPP;
2782 goto out;
2783 }
2784
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002785 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002786 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002787 if (err)
2788 goto out;
2789
2790 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002792 if (!msg) {
2793 err = -ENOBUFS;
2794 goto out;
2795 }
2796 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2797 NL80211_CMD_GET_MESH_PARAMS);
2798 if (!hdr)
2799 goto nla_put_failure;
2800 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2801 if (!pinfoattr)
2802 goto nla_put_failure;
2803 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2804 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2805 cur_params.dot11MeshRetryTimeout);
2806 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2807 cur_params.dot11MeshConfirmTimeout);
2808 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2809 cur_params.dot11MeshHoldingTimeout);
2810 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2811 cur_params.dot11MeshMaxPeerLinks);
2812 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2813 cur_params.dot11MeshMaxRetries);
2814 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2815 cur_params.dot11MeshTTL);
2816 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2817 cur_params.auto_open_plinks);
2818 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2819 cur_params.dot11MeshHWMPmaxPREQretries);
2820 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2821 cur_params.path_refresh_time);
2822 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2823 cur_params.min_discovery_timeout);
2824 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2825 cur_params.dot11MeshHWMPactivePathTimeout);
2826 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2827 cur_params.dot11MeshHWMPpreqMinInterval);
2828 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2829 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002830 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2831 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002832 nla_nest_end(msg, pinfoattr);
2833 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002834 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002835 goto out;
2836
Johannes Berg3b858752009-03-12 09:55:09 +01002837 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002838 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002839 nlmsg_free(msg);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002840 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002841 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002842 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002843 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002844 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002845 out_rtnl:
2846 rtnl_unlock();
2847
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002848 return err;
2849}
2850
2851#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2852do {\
2853 if (table[attr_num]) {\
2854 cfg.param = nla_fn(table[attr_num]); \
2855 mask |= (1 << (attr_num - 1)); \
2856 } \
2857} while (0);\
2858
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002859static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002860 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2861 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2862 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2863 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2864 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2865 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2866 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2867
2868 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2869 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2870 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2871 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2872 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2873 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2874};
2875
2876static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2877{
2878 int err;
2879 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002880 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002881 struct net_device *dev;
2882 struct mesh_config cfg;
2883 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2884 struct nlattr *parent_attr;
2885
2886 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2887 if (!parent_attr)
2888 return -EINVAL;
2889 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2890 parent_attr, nl80211_meshconf_params_policy))
2891 return -EINVAL;
2892
Johannes Berg3b858752009-03-12 09:55:09 +01002893 rtnl_lock();
2894
Johannes Berg463d0182009-07-14 00:33:35 +02002895 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002896 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002897 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002898
Johannes Berg79c97e92009-07-07 03:56:12 +02002899 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002900 err = -EOPNOTSUPP;
2901 goto out;
2902 }
2903
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002904 /* This makes sure that there aren't more than 32 mesh config
2905 * parameters (otherwise our bitfield scheme would not work.) */
2906 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2907
2908 /* Fill in the params struct */
2909 mask = 0;
2910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2911 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2912 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2913 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2914 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2915 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2917 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2918 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2919 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2920 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2921 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2923 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2925 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2926 nla_get_u8);
2927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2928 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2930 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2931 nla_get_u16);
2932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2933 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2934 nla_get_u32);
2935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2936 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2937 nla_get_u16);
2938 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2939 dot11MeshHWMPnetDiameterTraversalTime,
2940 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2941 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002942 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2943 dot11MeshHWMPRootMode, mask,
2944 NL80211_MESHCONF_HWMP_ROOTMODE,
2945 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002946
2947 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002948 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002949
Jouni Malinenf3f92582009-03-20 17:57:36 +02002950 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002951 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002952 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002953 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002954 out_rtnl:
2955 rtnl_unlock();
2956
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002957 return err;
2958}
2959
2960#undef FILL_IN_MESH_PARAM_IF_SET
2961
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002962static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2963{
2964 struct sk_buff *msg;
2965 void *hdr = NULL;
2966 struct nlattr *nl_reg_rules;
2967 unsigned int i;
2968 int err = -EINVAL;
2969
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002970 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002971
2972 if (!cfg80211_regdomain)
2973 goto out;
2974
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002975 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002976 if (!msg) {
2977 err = -ENOBUFS;
2978 goto out;
2979 }
2980
2981 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2982 NL80211_CMD_GET_REG);
2983 if (!hdr)
2984 goto nla_put_failure;
2985
2986 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2987 cfg80211_regdomain->alpha2);
2988
2989 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2990 if (!nl_reg_rules)
2991 goto nla_put_failure;
2992
2993 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2994 struct nlattr *nl_reg_rule;
2995 const struct ieee80211_reg_rule *reg_rule;
2996 const struct ieee80211_freq_range *freq_range;
2997 const struct ieee80211_power_rule *power_rule;
2998
2999 reg_rule = &cfg80211_regdomain->reg_rules[i];
3000 freq_range = &reg_rule->freq_range;
3001 power_rule = &reg_rule->power_rule;
3002
3003 nl_reg_rule = nla_nest_start(msg, i);
3004 if (!nl_reg_rule)
3005 goto nla_put_failure;
3006
3007 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3008 reg_rule->flags);
3009 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3010 freq_range->start_freq_khz);
3011 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3012 freq_range->end_freq_khz);
3013 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3014 freq_range->max_bandwidth_khz);
3015 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3016 power_rule->max_antenna_gain);
3017 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3018 power_rule->max_eirp);
3019
3020 nla_nest_end(msg, nl_reg_rule);
3021 }
3022
3023 nla_nest_end(msg, nl_reg_rules);
3024
3025 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003026 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003027 goto out;
3028
3029nla_put_failure:
3030 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04003031 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003032 err = -EMSGSIZE;
3033out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003034 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003035 return err;
3036}
3037
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003038static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3039{
3040 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3041 struct nlattr *nl_reg_rule;
3042 char *alpha2 = NULL;
3043 int rem_reg_rules = 0, r = 0;
3044 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3045 struct ieee80211_regdomain *rd = NULL;
3046
3047 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3048 return -EINVAL;
3049
3050 if (!info->attrs[NL80211_ATTR_REG_RULES])
3051 return -EINVAL;
3052
3053 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3054
3055 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3056 rem_reg_rules) {
3057 num_rules++;
3058 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003059 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003060 }
3061
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003062 mutex_lock(&cfg80211_mutex);
3063
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003064 if (!reg_is_valid_request(alpha2)) {
3065 r = -EINVAL;
3066 goto bad_reg;
3067 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003068
3069 size_of_regd = sizeof(struct ieee80211_regdomain) +
3070 (num_rules * sizeof(struct ieee80211_reg_rule));
3071
3072 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003073 if (!rd) {
3074 r = -ENOMEM;
3075 goto bad_reg;
3076 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003077
3078 rd->n_reg_rules = num_rules;
3079 rd->alpha2[0] = alpha2[0];
3080 rd->alpha2[1] = alpha2[1];
3081
3082 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3083 rem_reg_rules) {
3084 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3085 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3086 reg_rule_policy);
3087 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3088 if (r)
3089 goto bad_reg;
3090
3091 rule_idx++;
3092
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003093 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3094 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003095 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003096 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003097 }
3098
3099 BUG_ON(rule_idx != num_rules);
3100
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003101 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003102
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003103 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003104
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003105 return r;
3106
Johannes Bergd2372b32008-10-24 20:32:20 +02003107 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003108 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003109 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003110 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003111}
3112
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003113static int validate_scan_freqs(struct nlattr *freqs)
3114{
3115 struct nlattr *attr1, *attr2;
3116 int n_channels = 0, tmp1, tmp2;
3117
3118 nla_for_each_nested(attr1, freqs, tmp1) {
3119 n_channels++;
3120 /*
3121 * Some hardware has a limited channel list for
3122 * scanning, and it is pretty much nonsensical
3123 * to scan for a channel twice, so disallow that
3124 * and don't require drivers to check that the
3125 * channel list they get isn't longer than what
3126 * they can scan, as long as they can scan all
3127 * the channels they registered at once.
3128 */
3129 nla_for_each_nested(attr2, freqs, tmp2)
3130 if (attr1 != attr2 &&
3131 nla_get_u32(attr1) == nla_get_u32(attr2))
3132 return 0;
3133 }
3134
3135 return n_channels;
3136}
3137
Johannes Berg2a519312009-02-10 21:25:55 +01003138static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3139{
Johannes Berg79c97e92009-07-07 03:56:12 +02003140 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003141 struct net_device *dev;
3142 struct cfg80211_scan_request *request;
3143 struct cfg80211_ssid *ssid;
3144 struct ieee80211_channel *channel;
3145 struct nlattr *attr;
3146 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003147 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003148 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003149 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003150
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003151 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3152 return -EINVAL;
3153
Johannes Berg3b858752009-03-12 09:55:09 +01003154 rtnl_lock();
3155
Johannes Berg463d0182009-07-14 00:33:35 +02003156 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003157 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003158 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01003159
Johannes Berg79c97e92009-07-07 03:56:12 +02003160 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003161
Johannes Berg79c97e92009-07-07 03:56:12 +02003162 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003163 err = -EOPNOTSUPP;
3164 goto out;
3165 }
3166
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003167 if (!netif_running(dev)) {
3168 err = -ENETDOWN;
3169 goto out;
3170 }
3171
Johannes Berg79c97e92009-07-07 03:56:12 +02003172 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01003173 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01003174 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003175 }
3176
3177 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003178 n_channels = validate_scan_freqs(
3179 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01003180 if (!n_channels) {
3181 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003182 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003183 }
3184 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003185 n_channels = 0;
3186
Johannes Berg2a519312009-02-10 21:25:55 +01003187 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3188 if (wiphy->bands[band])
3189 n_channels += wiphy->bands[band]->n_channels;
3190 }
3191
3192 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3193 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3194 n_ssids++;
3195
3196 if (n_ssids > wiphy->max_scan_ssids) {
3197 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003198 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003199 }
3200
Jouni Malinen70692ad2009-02-16 19:39:13 +02003201 if (info->attrs[NL80211_ATTR_IE])
3202 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3203 else
3204 ie_len = 0;
3205
Johannes Berg18a83652009-03-31 12:12:05 +02003206 if (ie_len > wiphy->max_scan_ie_len) {
3207 err = -EINVAL;
3208 goto out;
3209 }
3210
Johannes Berg2a519312009-02-10 21:25:55 +01003211 request = kzalloc(sizeof(*request)
3212 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003213 + sizeof(channel) * n_channels
3214 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003215 if (!request) {
3216 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003217 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003218 }
3219
Johannes Berg2a519312009-02-10 21:25:55 +01003220 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003221 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003222 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003223 if (ie_len) {
3224 if (request->ssids)
3225 request->ie = (void *)(request->ssids + n_ssids);
3226 else
3227 request->ie = (void *)(request->channels + n_channels);
3228 }
Johannes Berg2a519312009-02-10 21:25:55 +01003229
Johannes Berg584991d2009-11-02 13:32:03 +01003230 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003231 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3232 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003233 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003234 struct ieee80211_channel *chan;
3235
3236 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3237
3238 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003239 err = -EINVAL;
3240 goto out_free;
3241 }
Johannes Berg584991d2009-11-02 13:32:03 +01003242
3243 /* ignore disabled channels */
3244 if (chan->flags & IEEE80211_CHAN_DISABLED)
3245 continue;
3246
3247 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003248 i++;
3249 }
3250 } else {
3251 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003252 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3253 int j;
3254 if (!wiphy->bands[band])
3255 continue;
3256 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003257 struct ieee80211_channel *chan;
3258
3259 chan = &wiphy->bands[band]->channels[j];
3260
3261 if (chan->flags & IEEE80211_CHAN_DISABLED)
3262 continue;
3263
3264 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003265 i++;
3266 }
3267 }
3268 }
3269
Johannes Berg584991d2009-11-02 13:32:03 +01003270 if (!i) {
3271 err = -EINVAL;
3272 goto out_free;
3273 }
3274
3275 request->n_channels = i;
3276
Johannes Berg2a519312009-02-10 21:25:55 +01003277 i = 0;
3278 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3279 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3280 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3281 err = -EINVAL;
3282 goto out_free;
3283 }
3284 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3285 request->ssids[i].ssid_len = nla_len(attr);
3286 i++;
3287 }
3288 }
3289
Jouni Malinen70692ad2009-02-16 19:39:13 +02003290 if (info->attrs[NL80211_ATTR_IE]) {
3291 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003292 memcpy((void *)request->ie,
3293 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003294 request->ie_len);
3295 }
3296
Johannes Berg463d0182009-07-14 00:33:35 +02003297 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003298 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003299
Johannes Berg79c97e92009-07-07 03:56:12 +02003300 rdev->scan_req = request;
3301 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003302
Johannes Berg463d0182009-07-14 00:33:35 +02003303 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003304 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003305 dev_hold(dev);
3306 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003307
Johannes Berg2a519312009-02-10 21:25:55 +01003308 out_free:
3309 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003310 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003311 kfree(request);
3312 }
Johannes Berg2a519312009-02-10 21:25:55 +01003313 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003314 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003315 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003316 out_rtnl:
3317 rtnl_unlock();
3318
Johannes Berg2a519312009-02-10 21:25:55 +01003319 return err;
3320}
3321
3322static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3323 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003324 struct wireless_dev *wdev,
3325 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003326{
Johannes Berg48ab9052009-07-10 18:42:31 +02003327 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003328 void *hdr;
3329 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003330 int i;
3331
3332 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003333
3334 hdr = nl80211hdr_put(msg, pid, seq, flags,
3335 NL80211_CMD_NEW_SCAN_RESULTS);
3336 if (!hdr)
3337 return -1;
3338
Johannes Bergf5ea9122009-08-07 16:17:38 +02003339 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003340 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003341
3342 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3343 if (!bss)
3344 goto nla_put_failure;
3345 if (!is_zero_ether_addr(res->bssid))
3346 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3347 if (res->information_elements && res->len_information_elements)
3348 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3349 res->len_information_elements,
3350 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003351 if (res->beacon_ies && res->len_beacon_ies &&
3352 res->beacon_ies != res->information_elements)
3353 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3354 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003355 if (res->tsf)
3356 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3357 if (res->beacon_interval)
3358 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3359 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3360 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003361 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3362 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003363
Johannes Berg77965c92009-02-18 18:45:06 +01003364 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003365 case CFG80211_SIGNAL_TYPE_MBM:
3366 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3367 break;
3368 case CFG80211_SIGNAL_TYPE_UNSPEC:
3369 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3370 break;
3371 default:
3372 break;
3373 }
3374
Johannes Berg48ab9052009-07-10 18:42:31 +02003375 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003376 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003377 case NL80211_IFTYPE_STATION:
3378 if (intbss == wdev->current_bss)
3379 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3380 NL80211_BSS_STATUS_ASSOCIATED);
3381 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3382 if (intbss != wdev->auth_bsses[i])
3383 continue;
3384 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3385 NL80211_BSS_STATUS_AUTHENTICATED);
3386 break;
3387 }
3388 break;
3389 case NL80211_IFTYPE_ADHOC:
3390 if (intbss == wdev->current_bss)
3391 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3392 NL80211_BSS_STATUS_IBSS_JOINED);
3393 break;
3394 default:
3395 break;
3396 }
3397
Johannes Berg2a519312009-02-10 21:25:55 +01003398 nla_nest_end(msg, bss);
3399
3400 return genlmsg_end(msg, hdr);
3401
3402 nla_put_failure:
3403 genlmsg_cancel(msg, hdr);
3404 return -EMSGSIZE;
3405}
3406
3407static int nl80211_dump_scan(struct sk_buff *skb,
3408 struct netlink_callback *cb)
3409{
Johannes Berg48ab9052009-07-10 18:42:31 +02003410 struct cfg80211_registered_device *rdev;
3411 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003412 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003413 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003414 int ifidx = cb->args[0];
3415 int start = cb->args[1], idx = 0;
3416 int err;
3417
Holger Schuriga0438972009-11-11 11:30:02 +01003418 if (!ifidx)
3419 ifidx = nl80211_get_ifidx(cb);
3420 if (ifidx < 0)
3421 return ifidx;
3422 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003423
Johannes Berg463d0182009-07-14 00:33:35 +02003424 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003425 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003426 return -ENODEV;
3427
Johannes Berg463d0182009-07-14 00:33:35 +02003428 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003429 if (IS_ERR(rdev)) {
3430 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003431 goto out_put_netdev;
3432 }
3433
Johannes Berg48ab9052009-07-10 18:42:31 +02003434 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003435
Johannes Berg48ab9052009-07-10 18:42:31 +02003436 wdev_lock(wdev);
3437 spin_lock_bh(&rdev->bss_lock);
3438 cfg80211_bss_expire(rdev);
3439
3440 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003441 if (++idx <= start)
3442 continue;
3443 if (nl80211_send_bss(skb,
3444 NETLINK_CB(cb->skb).pid,
3445 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003446 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003447 idx--;
3448 goto out;
3449 }
3450 }
3451
3452 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003453 spin_unlock_bh(&rdev->bss_lock);
3454 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003455
3456 cb->args[1] = idx;
3457 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003458 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003459 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003460 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003461
3462 return err;
3463}
3464
Holger Schurig61fa7132009-11-11 12:25:40 +01003465static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3466 int flags, struct net_device *dev,
3467 struct survey_info *survey)
3468{
3469 void *hdr;
3470 struct nlattr *infoattr;
3471
3472 /* Survey without a channel doesn't make sense */
3473 if (!survey->channel)
3474 return -EINVAL;
3475
3476 hdr = nl80211hdr_put(msg, pid, seq, flags,
3477 NL80211_CMD_NEW_SURVEY_RESULTS);
3478 if (!hdr)
3479 return -ENOMEM;
3480
3481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3482
3483 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3484 if (!infoattr)
3485 goto nla_put_failure;
3486
3487 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3488 survey->channel->center_freq);
3489 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3490 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3491 survey->noise);
3492
3493 nla_nest_end(msg, infoattr);
3494
3495 return genlmsg_end(msg, hdr);
3496
3497 nla_put_failure:
3498 genlmsg_cancel(msg, hdr);
3499 return -EMSGSIZE;
3500}
3501
3502static int nl80211_dump_survey(struct sk_buff *skb,
3503 struct netlink_callback *cb)
3504{
3505 struct survey_info survey;
3506 struct cfg80211_registered_device *dev;
3507 struct net_device *netdev;
3508 int ifidx = cb->args[0];
3509 int survey_idx = cb->args[1];
3510 int res;
3511
3512 if (!ifidx)
3513 ifidx = nl80211_get_ifidx(cb);
3514 if (ifidx < 0)
3515 return ifidx;
3516 cb->args[0] = ifidx;
3517
3518 rtnl_lock();
3519
3520 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3521 if (!netdev) {
3522 res = -ENODEV;
3523 goto out_rtnl;
3524 }
3525
3526 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3527 if (IS_ERR(dev)) {
3528 res = PTR_ERR(dev);
3529 goto out_rtnl;
3530 }
3531
3532 if (!dev->ops->dump_survey) {
3533 res = -EOPNOTSUPP;
3534 goto out_err;
3535 }
3536
3537 while (1) {
3538 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3539 &survey);
3540 if (res == -ENOENT)
3541 break;
3542 if (res)
3543 goto out_err;
3544
3545 if (nl80211_send_survey(skb,
3546 NETLINK_CB(cb->skb).pid,
3547 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3548 netdev,
3549 &survey) < 0)
3550 goto out;
3551 survey_idx++;
3552 }
3553
3554 out:
3555 cb->args[1] = survey_idx;
3556 res = skb->len;
3557 out_err:
3558 cfg80211_unlock_rdev(dev);
3559 out_rtnl:
3560 rtnl_unlock();
3561
3562 return res;
3563}
3564
Jouni Malinen255e7372009-03-20 21:21:17 +02003565static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3566{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003567 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003568}
3569
Samuel Ortizb23aa672009-07-01 21:26:54 +02003570static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3571{
3572 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3573 NL80211_WPA_VERSION_2));
3574}
3575
3576static bool nl80211_valid_akm_suite(u32 akm)
3577{
3578 return akm == WLAN_AKM_SUITE_8021X ||
3579 akm == WLAN_AKM_SUITE_PSK;
3580}
3581
3582static bool nl80211_valid_cipher_suite(u32 cipher)
3583{
3584 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3585 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3586 cipher == WLAN_CIPHER_SUITE_TKIP ||
3587 cipher == WLAN_CIPHER_SUITE_CCMP ||
3588 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3589}
3590
3591
Jouni Malinen636a5d32009-03-19 13:39:22 +02003592static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3593{
Johannes Berg79c97e92009-07-07 03:56:12 +02003594 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003595 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003596 struct ieee80211_channel *chan;
3597 const u8 *bssid, *ssid, *ie = NULL;
3598 int err, ssid_len, ie_len = 0;
3599 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003600 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003601 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003602
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003603 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3604 return -EINVAL;
3605
3606 if (!info->attrs[NL80211_ATTR_MAC])
3607 return -EINVAL;
3608
Jouni Malinen17780922009-03-27 20:52:47 +02003609 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3610 return -EINVAL;
3611
Johannes Berg19957bb2009-07-02 17:20:43 +02003612 if (!info->attrs[NL80211_ATTR_SSID])
3613 return -EINVAL;
3614
3615 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3616 return -EINVAL;
3617
Johannes Bergfffd0932009-07-08 14:22:54 +02003618 err = nl80211_parse_key(info, &key);
3619 if (err)
3620 return err;
3621
3622 if (key.idx >= 0) {
3623 if (!key.p.key || !key.p.key_len)
3624 return -EINVAL;
3625 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3626 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3627 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3628 key.p.key_len != WLAN_KEY_LEN_WEP104))
3629 return -EINVAL;
3630 if (key.idx > 4)
3631 return -EINVAL;
3632 } else {
3633 key.p.key_len = 0;
3634 key.p.key = NULL;
3635 }
3636
Jouni Malinen636a5d32009-03-19 13:39:22 +02003637 rtnl_lock();
3638
Johannes Berg463d0182009-07-14 00:33:35 +02003639 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003640 if (err)
3641 goto unlock_rtnl;
3642
Johannes Bergafea0b72010-08-10 09:46:42 +02003643 if (key.idx >= 0) {
3644 int i;
3645 bool ok = false;
3646 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3647 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3648 ok = true;
3649 break;
3650 }
3651 }
3652 if (!ok) {
3653 err = -EINVAL;
3654 goto out;
3655 }
3656 }
3657
Johannes Berg79c97e92009-07-07 03:56:12 +02003658 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003659 err = -EOPNOTSUPP;
3660 goto out;
3661 }
3662
Johannes Berg074ac8d2010-09-16 14:58:22 +02003663 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3664 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003665 err = -EOPNOTSUPP;
3666 goto out;
3667 }
3668
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003669 if (!netif_running(dev)) {
3670 err = -ENETDOWN;
3671 goto out;
3672 }
3673
Johannes Berg19957bb2009-07-02 17:20:43 +02003674 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003675 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003676 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3677 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003678 err = -EINVAL;
3679 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003680 }
3681
Johannes Berg19957bb2009-07-02 17:20:43 +02003682 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3683 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3684
3685 if (info->attrs[NL80211_ATTR_IE]) {
3686 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3687 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3688 }
3689
3690 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3691 if (!nl80211_valid_auth_type(auth_type)) {
3692 err = -EINVAL;
3693 goto out;
3694 }
3695
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003696 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3697
Johannes Berg79c97e92009-07-07 03:56:12 +02003698 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003699 ssid, ssid_len, ie, ie_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003700 key.p.key, key.p.key_len, key.idx,
3701 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003702
3703out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003704 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003705 dev_put(dev);
3706unlock_rtnl:
3707 rtnl_unlock();
3708 return err;
3709}
3710
Johannes Bergc0692b82010-08-27 14:26:53 +03003711static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3712 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003713 struct cfg80211_crypto_settings *settings,
3714 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003715{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003716 memset(settings, 0, sizeof(*settings));
3717
Samuel Ortizb23aa672009-07-01 21:26:54 +02003718 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3719
Johannes Bergc0692b82010-08-27 14:26:53 +03003720 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3721 u16 proto;
3722 proto = nla_get_u16(
3723 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3724 settings->control_port_ethertype = cpu_to_be16(proto);
3725 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3726 proto != ETH_P_PAE)
3727 return -EINVAL;
3728 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3729 settings->control_port_no_encrypt = true;
3730 } else
3731 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3732
Samuel Ortizb23aa672009-07-01 21:26:54 +02003733 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3734 void *data;
3735 int len, i;
3736
3737 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3738 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3739 settings->n_ciphers_pairwise = len / sizeof(u32);
3740
3741 if (len % sizeof(u32))
3742 return -EINVAL;
3743
Johannes Berg3dc27d22009-07-02 21:36:37 +02003744 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003745 return -EINVAL;
3746
3747 memcpy(settings->ciphers_pairwise, data, len);
3748
3749 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3750 if (!nl80211_valid_cipher_suite(
3751 settings->ciphers_pairwise[i]))
3752 return -EINVAL;
3753 }
3754
3755 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3756 settings->cipher_group =
3757 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3758 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3759 return -EINVAL;
3760 }
3761
3762 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3763 settings->wpa_versions =
3764 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3765 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3766 return -EINVAL;
3767 }
3768
3769 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3770 void *data;
3771 int len, i;
3772
3773 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3774 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3775 settings->n_akm_suites = len / sizeof(u32);
3776
3777 if (len % sizeof(u32))
3778 return -EINVAL;
3779
3780 memcpy(settings->akm_suites, data, len);
3781
3782 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3783 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3784 return -EINVAL;
3785 }
3786
3787 return 0;
3788}
3789
Jouni Malinen636a5d32009-03-19 13:39:22 +02003790static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3791{
Johannes Berg19957bb2009-07-02 17:20:43 +02003792 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003793 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003794 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003795 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003796 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003797 int err, ssid_len, ie_len = 0;
3798 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003799
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003800 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3801 return -EINVAL;
3802
3803 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003804 !info->attrs[NL80211_ATTR_SSID] ||
3805 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003806 return -EINVAL;
3807
Jouni Malinen636a5d32009-03-19 13:39:22 +02003808 rtnl_lock();
3809
Johannes Berg463d0182009-07-14 00:33:35 +02003810 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003811 if (err)
3812 goto unlock_rtnl;
3813
Johannes Berg19957bb2009-07-02 17:20:43 +02003814 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003815 err = -EOPNOTSUPP;
3816 goto out;
3817 }
3818
Johannes Berg074ac8d2010-09-16 14:58:22 +02003819 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3820 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003821 err = -EOPNOTSUPP;
3822 goto out;
3823 }
3824
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003825 if (!netif_running(dev)) {
3826 err = -ENETDOWN;
3827 goto out;
3828 }
3829
Johannes Berg19957bb2009-07-02 17:20:43 +02003830 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003831
Johannes Berg19957bb2009-07-02 17:20:43 +02003832 chan = ieee80211_get_channel(&rdev->wiphy,
3833 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3834 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3835 err = -EINVAL;
3836 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003837 }
3838
Johannes Berg19957bb2009-07-02 17:20:43 +02003839 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3840 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003841
3842 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003843 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3844 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003845 }
3846
Jouni Malinendc6382c2009-05-06 22:09:37 +03003847 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003848 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003849 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003850 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003851 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003852 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382c2009-05-06 22:09:37 +03003853 err = -EINVAL;
3854 goto out;
3855 }
3856 }
3857
Johannes Berg3e5d7642009-07-07 14:37:26 +02003858 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3859 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3860
Johannes Bergc0692b82010-08-27 14:26:53 +03003861 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003862 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003863 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3864 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003865 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003866
3867out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003868 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003869 dev_put(dev);
3870unlock_rtnl:
3871 rtnl_unlock();
3872 return err;
3873}
3874
3875static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3876{
Johannes Berg79c97e92009-07-07 03:56:12 +02003877 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003878 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003879 const u8 *ie = NULL, *bssid;
3880 int err, ie_len = 0;
3881 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003882 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003883
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003884 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3885 return -EINVAL;
3886
3887 if (!info->attrs[NL80211_ATTR_MAC])
3888 return -EINVAL;
3889
3890 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3891 return -EINVAL;
3892
Jouni Malinen636a5d32009-03-19 13:39:22 +02003893 rtnl_lock();
3894
Johannes Berg463d0182009-07-14 00:33:35 +02003895 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003896 if (err)
3897 goto unlock_rtnl;
3898
Johannes Berg79c97e92009-07-07 03:56:12 +02003899 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003900 err = -EOPNOTSUPP;
3901 goto out;
3902 }
3903
Johannes Berg074ac8d2010-09-16 14:58:22 +02003904 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3905 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003906 err = -EOPNOTSUPP;
3907 goto out;
3908 }
3909
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003910 if (!netif_running(dev)) {
3911 err = -ENETDOWN;
3912 goto out;
3913 }
3914
Johannes Berg19957bb2009-07-02 17:20:43 +02003915 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003916
Johannes Berg19957bb2009-07-02 17:20:43 +02003917 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3918 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003919 /* Reason Code 0 is reserved */
3920 err = -EINVAL;
3921 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003922 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003923
3924 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003925 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3926 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003927 }
3928
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003929 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3930
3931 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3932 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003933
3934out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003935 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003936 dev_put(dev);
3937unlock_rtnl:
3938 rtnl_unlock();
3939 return err;
3940}
3941
3942static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3943{
Johannes Berg79c97e92009-07-07 03:56:12 +02003944 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003945 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003946 const u8 *ie = NULL, *bssid;
3947 int err, ie_len = 0;
3948 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003949 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003950
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003951 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3952 return -EINVAL;
3953
3954 if (!info->attrs[NL80211_ATTR_MAC])
3955 return -EINVAL;
3956
3957 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3958 return -EINVAL;
3959
Jouni Malinen636a5d32009-03-19 13:39:22 +02003960 rtnl_lock();
3961
Johannes Berg463d0182009-07-14 00:33:35 +02003962 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003963 if (err)
3964 goto unlock_rtnl;
3965
Johannes Berg79c97e92009-07-07 03:56:12 +02003966 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003967 err = -EOPNOTSUPP;
3968 goto out;
3969 }
3970
Johannes Berg074ac8d2010-09-16 14:58:22 +02003971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003973 err = -EOPNOTSUPP;
3974 goto out;
3975 }
3976
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003977 if (!netif_running(dev)) {
3978 err = -ENETDOWN;
3979 goto out;
3980 }
3981
Johannes Berg19957bb2009-07-02 17:20:43 +02003982 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003983
Johannes Berg19957bb2009-07-02 17:20:43 +02003984 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3985 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003986 /* Reason Code 0 is reserved */
3987 err = -EINVAL;
3988 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003989 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003990
3991 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003992 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3993 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003994 }
3995
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003996 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3997
3998 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3999 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004000
4001out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004002 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004003 dev_put(dev);
4004unlock_rtnl:
4005 rtnl_unlock();
4006 return err;
4007}
4008
Johannes Berg04a773a2009-04-19 21:24:32 +02004009static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4010{
Johannes Berg79c97e92009-07-07 03:56:12 +02004011 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02004012 struct net_device *dev;
4013 struct cfg80211_ibss_params ibss;
4014 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004015 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004016 int err;
4017
Johannes Berg8e30bc52009-04-22 17:45:38 +02004018 memset(&ibss, 0, sizeof(ibss));
4019
Johannes Berg04a773a2009-04-19 21:24:32 +02004020 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4021 return -EINVAL;
4022
4023 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4024 !info->attrs[NL80211_ATTR_SSID] ||
4025 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4026 return -EINVAL;
4027
Johannes Berg8e30bc52009-04-22 17:45:38 +02004028 ibss.beacon_interval = 100;
4029
4030 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4031 ibss.beacon_interval =
4032 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4033 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4034 return -EINVAL;
4035 }
4036
Johannes Berg04a773a2009-04-19 21:24:32 +02004037 rtnl_lock();
4038
Johannes Berg463d0182009-07-14 00:33:35 +02004039 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02004040 if (err)
4041 goto unlock_rtnl;
4042
Johannes Berg79c97e92009-07-07 03:56:12 +02004043 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004044 err = -EOPNOTSUPP;
4045 goto out;
4046 }
4047
4048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
4049 err = -EOPNOTSUPP;
4050 goto out;
4051 }
4052
4053 if (!netif_running(dev)) {
4054 err = -ENETDOWN;
4055 goto out;
4056 }
4057
Johannes Berg79c97e92009-07-07 03:56:12 +02004058 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004059
4060 if (info->attrs[NL80211_ATTR_MAC])
4061 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4062 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4063 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4064
4065 if (info->attrs[NL80211_ATTR_IE]) {
4066 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4067 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4068 }
4069
4070 ibss.channel = ieee80211_get_channel(wiphy,
4071 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4072 if (!ibss.channel ||
4073 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4074 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
4075 err = -EINVAL;
4076 goto out;
4077 }
4078
4079 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004080 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004081
Johannes Bergfffd0932009-07-08 14:22:54 +02004082 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4083 connkeys = nl80211_parse_connkeys(rdev,
4084 info->attrs[NL80211_ATTR_KEYS]);
4085 if (IS_ERR(connkeys)) {
4086 err = PTR_ERR(connkeys);
4087 connkeys = NULL;
4088 goto out;
4089 }
4090 }
4091
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004092 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4093 u8 *rates =
4094 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4095 int n_rates =
4096 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4097 struct ieee80211_supported_band *sband =
4098 wiphy->bands[ibss.channel->band];
4099 int i, j;
4100
4101 if (n_rates == 0) {
4102 err = -EINVAL;
4103 goto out;
4104 }
4105
4106 for (i = 0; i < n_rates; i++) {
4107 int rate = (rates[i] & 0x7f) * 5;
4108 bool found = false;
4109
4110 for (j = 0; j < sband->n_bitrates; j++) {
4111 if (sband->bitrates[j].bitrate == rate) {
4112 found = true;
4113 ibss.basic_rates |= BIT(j);
4114 break;
4115 }
4116 }
4117 if (!found) {
4118 err = -EINVAL;
4119 goto out;
4120 }
4121 }
4122 } else {
4123 /*
4124 * If no rates were explicitly configured,
4125 * use the mandatory rate set for 11b or
4126 * 11a for maximum compatibility.
4127 */
4128 struct ieee80211_supported_band *sband =
4129 wiphy->bands[ibss.channel->band];
4130 int j;
4131 u32 flag = ibss.channel->band == IEEE80211_BAND_5GHZ ?
4132 IEEE80211_RATE_MANDATORY_A :
4133 IEEE80211_RATE_MANDATORY_B;
4134
4135 for (j = 0; j < sband->n_bitrates; j++) {
4136 if (sband->bitrates[j].flags & flag)
4137 ibss.basic_rates |= BIT(j);
4138 }
4139 }
4140
Johannes Bergfffd0932009-07-08 14:22:54 +02004141 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004142
4143out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004144 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02004145 dev_put(dev);
4146unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004147 if (err)
4148 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004149 rtnl_unlock();
4150 return err;
4151}
4152
4153static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4154{
Johannes Berg79c97e92009-07-07 03:56:12 +02004155 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02004156 struct net_device *dev;
4157 int err;
4158
4159 rtnl_lock();
4160
Johannes Berg463d0182009-07-14 00:33:35 +02004161 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02004162 if (err)
4163 goto unlock_rtnl;
4164
Johannes Berg79c97e92009-07-07 03:56:12 +02004165 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02004166 err = -EOPNOTSUPP;
4167 goto out;
4168 }
4169
4170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
4171 err = -EOPNOTSUPP;
4172 goto out;
4173 }
4174
4175 if (!netif_running(dev)) {
4176 err = -ENETDOWN;
4177 goto out;
4178 }
4179
Johannes Berg79c97e92009-07-07 03:56:12 +02004180 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004181
4182out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004183 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02004184 dev_put(dev);
4185unlock_rtnl:
4186 rtnl_unlock();
4187 return err;
4188}
4189
Johannes Bergaff89a92009-07-01 21:26:51 +02004190#ifdef CONFIG_NL80211_TESTMODE
4191static struct genl_multicast_group nl80211_testmode_mcgrp = {
4192 .name = "testmode",
4193};
4194
4195static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4196{
4197 struct cfg80211_registered_device *rdev;
4198 int err;
4199
4200 if (!info->attrs[NL80211_ATTR_TESTDATA])
4201 return -EINVAL;
4202
4203 rtnl_lock();
4204
4205 rdev = cfg80211_get_dev_from_info(info);
4206 if (IS_ERR(rdev)) {
4207 err = PTR_ERR(rdev);
4208 goto unlock_rtnl;
4209 }
4210
4211 err = -EOPNOTSUPP;
4212 if (rdev->ops->testmode_cmd) {
4213 rdev->testmode_info = info;
4214 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4215 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4216 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4217 rdev->testmode_info = NULL;
4218 }
4219
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02004220 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02004221
4222 unlock_rtnl:
4223 rtnl_unlock();
4224 return err;
4225}
4226
4227static struct sk_buff *
4228__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4229 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4230{
4231 struct sk_buff *skb;
4232 void *hdr;
4233 struct nlattr *data;
4234
4235 skb = nlmsg_new(approxlen + 100, gfp);
4236 if (!skb)
4237 return NULL;
4238
4239 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4240 if (!hdr) {
4241 kfree_skb(skb);
4242 return NULL;
4243 }
4244
4245 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4246 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4247
4248 ((void **)skb->cb)[0] = rdev;
4249 ((void **)skb->cb)[1] = hdr;
4250 ((void **)skb->cb)[2] = data;
4251
4252 return skb;
4253
4254 nla_put_failure:
4255 kfree_skb(skb);
4256 return NULL;
4257}
4258
4259struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4260 int approxlen)
4261{
4262 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4263
4264 if (WARN_ON(!rdev->testmode_info))
4265 return NULL;
4266
4267 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4268 rdev->testmode_info->snd_pid,
4269 rdev->testmode_info->snd_seq,
4270 GFP_KERNEL);
4271}
4272EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4273
4274int cfg80211_testmode_reply(struct sk_buff *skb)
4275{
4276 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4277 void *hdr = ((void **)skb->cb)[1];
4278 struct nlattr *data = ((void **)skb->cb)[2];
4279
4280 if (WARN_ON(!rdev->testmode_info)) {
4281 kfree_skb(skb);
4282 return -EINVAL;
4283 }
4284
4285 nla_nest_end(skb, data);
4286 genlmsg_end(skb, hdr);
4287 return genlmsg_reply(skb, rdev->testmode_info);
4288}
4289EXPORT_SYMBOL(cfg80211_testmode_reply);
4290
4291struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4292 int approxlen, gfp_t gfp)
4293{
4294 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4295
4296 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4297}
4298EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4299
4300void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4301{
4302 void *hdr = ((void **)skb->cb)[1];
4303 struct nlattr *data = ((void **)skb->cb)[2];
4304
4305 nla_nest_end(skb, data);
4306 genlmsg_end(skb, hdr);
4307 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4308}
4309EXPORT_SYMBOL(cfg80211_testmode_event);
4310#endif
4311
Samuel Ortizb23aa672009-07-01 21:26:54 +02004312static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4313{
Johannes Berg79c97e92009-07-07 03:56:12 +02004314 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004315 struct net_device *dev;
4316 struct cfg80211_connect_params connect;
4317 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004318 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004319 int err;
4320
4321 memset(&connect, 0, sizeof(connect));
4322
4323 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4324 return -EINVAL;
4325
4326 if (!info->attrs[NL80211_ATTR_SSID] ||
4327 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4328 return -EINVAL;
4329
4330 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4331 connect.auth_type =
4332 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4333 if (!nl80211_valid_auth_type(connect.auth_type))
4334 return -EINVAL;
4335 } else
4336 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4337
4338 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4339
Johannes Bergc0692b82010-08-27 14:26:53 +03004340 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004341 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004342 if (err)
4343 return err;
4344 rtnl_lock();
4345
Johannes Berg463d0182009-07-14 00:33:35 +02004346 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004347 if (err)
4348 goto unlock_rtnl;
4349
Johannes Berg074ac8d2010-09-16 14:58:22 +02004350 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4351 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02004352 err = -EOPNOTSUPP;
4353 goto out;
4354 }
4355
4356 if (!netif_running(dev)) {
4357 err = -ENETDOWN;
4358 goto out;
4359 }
4360
Johannes Berg79c97e92009-07-07 03:56:12 +02004361 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004362
Samuel Ortizb23aa672009-07-01 21:26:54 +02004363 if (info->attrs[NL80211_ATTR_MAC])
4364 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4365 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4366 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4367
4368 if (info->attrs[NL80211_ATTR_IE]) {
4369 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4370 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4371 }
4372
4373 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4374 connect.channel =
4375 ieee80211_get_channel(wiphy,
4376 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4377 if (!connect.channel ||
4378 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4379 err = -EINVAL;
4380 goto out;
4381 }
4382 }
4383
Johannes Bergfffd0932009-07-08 14:22:54 +02004384 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4385 connkeys = nl80211_parse_connkeys(rdev,
4386 info->attrs[NL80211_ATTR_KEYS]);
4387 if (IS_ERR(connkeys)) {
4388 err = PTR_ERR(connkeys);
4389 connkeys = NULL;
4390 goto out;
4391 }
4392 }
4393
4394 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004395
4396out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004397 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004398 dev_put(dev);
4399unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004400 if (err)
4401 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004402 rtnl_unlock();
4403 return err;
4404}
4405
4406static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4407{
Johannes Berg79c97e92009-07-07 03:56:12 +02004408 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004409 struct net_device *dev;
4410 int err;
4411 u16 reason;
4412
4413 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4414 reason = WLAN_REASON_DEAUTH_LEAVING;
4415 else
4416 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4417
4418 if (reason == 0)
4419 return -EINVAL;
4420
4421 rtnl_lock();
4422
Johannes Berg463d0182009-07-14 00:33:35 +02004423 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004424 if (err)
4425 goto unlock_rtnl;
4426
Johannes Berg074ac8d2010-09-16 14:58:22 +02004427 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4428 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02004429 err = -EOPNOTSUPP;
4430 goto out;
4431 }
4432
4433 if (!netif_running(dev)) {
4434 err = -ENETDOWN;
4435 goto out;
4436 }
4437
Johannes Berg79c97e92009-07-07 03:56:12 +02004438 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004439
4440out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004441 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004442 dev_put(dev);
4443unlock_rtnl:
4444 rtnl_unlock();
4445 return err;
4446}
4447
Johannes Berg463d0182009-07-14 00:33:35 +02004448static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4449{
4450 struct cfg80211_registered_device *rdev;
4451 struct net *net;
4452 int err;
4453 u32 pid;
4454
4455 if (!info->attrs[NL80211_ATTR_PID])
4456 return -EINVAL;
4457
4458 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4459
4460 rtnl_lock();
4461
4462 rdev = cfg80211_get_dev_from_info(info);
4463 if (IS_ERR(rdev)) {
4464 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004465 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004466 }
4467
4468 net = get_net_ns_by_pid(pid);
4469 if (IS_ERR(net)) {
4470 err = PTR_ERR(net);
4471 goto out;
4472 }
4473
4474 err = 0;
4475
4476 /* check if anything to do */
4477 if (net_eq(wiphy_net(&rdev->wiphy), net))
4478 goto out_put_net;
4479
4480 err = cfg80211_switch_netns(rdev, net);
4481 out_put_net:
4482 put_net(net);
4483 out:
4484 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004485 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004486 rtnl_unlock();
4487 return err;
4488}
4489
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004490static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4491{
4492 struct cfg80211_registered_device *rdev;
4493 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4494 struct cfg80211_pmksa *pmksa) = NULL;
4495 int err;
4496 struct net_device *dev;
4497 struct cfg80211_pmksa pmksa;
4498
4499 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4500
4501 if (!info->attrs[NL80211_ATTR_MAC])
4502 return -EINVAL;
4503
4504 if (!info->attrs[NL80211_ATTR_PMKID])
4505 return -EINVAL;
4506
4507 rtnl_lock();
4508
4509 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4510 if (err)
4511 goto out_rtnl;
4512
4513 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4514 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4515
Johannes Berg074ac8d2010-09-16 14:58:22 +02004516 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4517 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004518 err = -EOPNOTSUPP;
4519 goto out;
4520 }
4521
4522 switch (info->genlhdr->cmd) {
4523 case NL80211_CMD_SET_PMKSA:
4524 rdev_ops = rdev->ops->set_pmksa;
4525 break;
4526 case NL80211_CMD_DEL_PMKSA:
4527 rdev_ops = rdev->ops->del_pmksa;
4528 break;
4529 default:
4530 WARN_ON(1);
4531 break;
4532 }
4533
4534 if (!rdev_ops) {
4535 err = -EOPNOTSUPP;
4536 goto out;
4537 }
4538
4539 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4540
4541 out:
4542 cfg80211_unlock_rdev(rdev);
4543 dev_put(dev);
4544 out_rtnl:
4545 rtnl_unlock();
4546
4547 return err;
4548}
4549
4550static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4551{
4552 struct cfg80211_registered_device *rdev;
4553 int err;
4554 struct net_device *dev;
4555
4556 rtnl_lock();
4557
4558 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4559 if (err)
4560 goto out_rtnl;
4561
Johannes Berg074ac8d2010-09-16 14:58:22 +02004562 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4563 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004564 err = -EOPNOTSUPP;
4565 goto out;
4566 }
4567
4568 if (!rdev->ops->flush_pmksa) {
4569 err = -EOPNOTSUPP;
4570 goto out;
4571 }
4572
4573 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4574
4575 out:
4576 cfg80211_unlock_rdev(rdev);
4577 dev_put(dev);
4578 out_rtnl:
4579 rtnl_unlock();
4580
4581 return err;
4582
4583}
4584
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004585static int nl80211_remain_on_channel(struct sk_buff *skb,
4586 struct genl_info *info)
4587{
4588 struct cfg80211_registered_device *rdev;
4589 struct net_device *dev;
4590 struct ieee80211_channel *chan;
4591 struct sk_buff *msg;
4592 void *hdr;
4593 u64 cookie;
4594 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4595 u32 freq, duration;
4596 int err;
4597
4598 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4599 !info->attrs[NL80211_ATTR_DURATION])
4600 return -EINVAL;
4601
4602 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4603
4604 /*
4605 * We should be on that channel for at least one jiffie,
4606 * and more than 5 seconds seems excessive.
4607 */
4608 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4609 return -EINVAL;
4610
4611 rtnl_lock();
4612
4613 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4614 if (err)
4615 goto unlock_rtnl;
4616
4617 if (!rdev->ops->remain_on_channel) {
4618 err = -EOPNOTSUPP;
4619 goto out;
4620 }
4621
4622 if (!netif_running(dev)) {
4623 err = -ENETDOWN;
4624 goto out;
4625 }
4626
4627 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4628 channel_type = nla_get_u32(
4629 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4630 if (channel_type != NL80211_CHAN_NO_HT &&
4631 channel_type != NL80211_CHAN_HT20 &&
4632 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg579d7532010-05-18 14:36:34 +02004633 channel_type != NL80211_CHAN_HT40MINUS) {
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004634 err = -EINVAL;
4635 goto out;
Johannes Berg579d7532010-05-18 14:36:34 +02004636 }
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004637 }
4638
4639 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4640 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4641 if (chan == NULL) {
4642 err = -EINVAL;
4643 goto out;
4644 }
4645
4646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4647 if (!msg) {
4648 err = -ENOMEM;
4649 goto out;
4650 }
4651
4652 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4653 NL80211_CMD_REMAIN_ON_CHANNEL);
4654
4655 if (IS_ERR(hdr)) {
4656 err = PTR_ERR(hdr);
4657 goto free_msg;
4658 }
4659
4660 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4661 channel_type, duration, &cookie);
4662
4663 if (err)
4664 goto free_msg;
4665
4666 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4667
4668 genlmsg_end(msg, hdr);
4669 err = genlmsg_reply(msg, info);
4670 goto out;
4671
4672 nla_put_failure:
4673 err = -ENOBUFS;
4674 free_msg:
4675 nlmsg_free(msg);
4676 out:
4677 cfg80211_unlock_rdev(rdev);
4678 dev_put(dev);
4679 unlock_rtnl:
4680 rtnl_unlock();
4681 return err;
4682}
4683
4684static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4685 struct genl_info *info)
4686{
4687 struct cfg80211_registered_device *rdev;
4688 struct net_device *dev;
4689 u64 cookie;
4690 int err;
4691
4692 if (!info->attrs[NL80211_ATTR_COOKIE])
4693 return -EINVAL;
4694
4695 rtnl_lock();
4696
4697 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4698 if (err)
4699 goto unlock_rtnl;
4700
4701 if (!rdev->ops->cancel_remain_on_channel) {
4702 err = -EOPNOTSUPP;
4703 goto out;
4704 }
4705
4706 if (!netif_running(dev)) {
4707 err = -ENETDOWN;
4708 goto out;
4709 }
4710
4711 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4712
4713 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4714
4715 out:
4716 cfg80211_unlock_rdev(rdev);
4717 dev_put(dev);
4718 unlock_rtnl:
4719 rtnl_unlock();
4720 return err;
4721}
4722
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004723static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4724 u8 *rates, u8 rates_len)
4725{
4726 u8 i;
4727 u32 mask = 0;
4728
4729 for (i = 0; i < rates_len; i++) {
4730 int rate = (rates[i] & 0x7f) * 5;
4731 int ridx;
4732 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4733 struct ieee80211_rate *srate =
4734 &sband->bitrates[ridx];
4735 if (rate == srate->bitrate) {
4736 mask |= 1 << ridx;
4737 break;
4738 }
4739 }
4740 if (ridx == sband->n_bitrates)
4741 return 0; /* rate not found */
4742 }
4743
4744 return mask;
4745}
4746
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004747static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004748 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4749 .len = NL80211_MAX_SUPP_RATES },
4750};
4751
4752static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4753 struct genl_info *info)
4754{
4755 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4756 struct cfg80211_registered_device *rdev;
4757 struct cfg80211_bitrate_mask mask;
4758 int err, rem, i;
4759 struct net_device *dev;
4760 struct nlattr *tx_rates;
4761 struct ieee80211_supported_band *sband;
4762
4763 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4764 return -EINVAL;
4765
4766 rtnl_lock();
4767
4768 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4769 if (err)
4770 goto unlock_rtnl;
4771
4772 if (!rdev->ops->set_bitrate_mask) {
4773 err = -EOPNOTSUPP;
4774 goto unlock;
4775 }
4776
4777 memset(&mask, 0, sizeof(mask));
4778 /* Default to all rates enabled */
4779 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4780 sband = rdev->wiphy.bands[i];
4781 mask.control[i].legacy =
4782 sband ? (1 << sband->n_bitrates) - 1 : 0;
4783 }
4784
4785 /*
4786 * The nested attribute uses enum nl80211_band as the index. This maps
4787 * directly to the enum ieee80211_band values used in cfg80211.
4788 */
4789 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4790 {
4791 enum ieee80211_band band = nla_type(tx_rates);
4792 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4793 err = -EINVAL;
4794 goto unlock;
4795 }
4796 sband = rdev->wiphy.bands[band];
4797 if (sband == NULL) {
4798 err = -EINVAL;
4799 goto unlock;
4800 }
4801 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4802 nla_len(tx_rates), nl80211_txattr_policy);
4803 if (tb[NL80211_TXRATE_LEGACY]) {
4804 mask.control[band].legacy = rateset_to_mask(
4805 sband,
4806 nla_data(tb[NL80211_TXRATE_LEGACY]),
4807 nla_len(tb[NL80211_TXRATE_LEGACY]));
4808 if (mask.control[band].legacy == 0) {
4809 err = -EINVAL;
4810 goto unlock;
4811 }
4812 }
4813 }
4814
4815 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4816
4817 unlock:
4818 dev_put(dev);
4819 cfg80211_unlock_rdev(rdev);
4820 unlock_rtnl:
4821 rtnl_unlock();
4822 return err;
4823}
4824
Johannes Berg2e161f72010-08-12 15:38:38 +02004825static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004826{
4827 struct cfg80211_registered_device *rdev;
4828 struct net_device *dev;
Johannes Berg2e161f72010-08-12 15:38:38 +02004829 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004830 int err;
4831
4832 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4833 return -EINVAL;
4834
Johannes Berg2e161f72010-08-12 15:38:38 +02004835 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4836 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004837
4838 rtnl_lock();
4839
4840 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4841 if (err)
4842 goto unlock_rtnl;
4843
Johannes Berg9d38d852010-06-09 17:20:33 +02004844 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004845 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4846 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004847 err = -EOPNOTSUPP;
4848 goto out;
4849 }
4850
4851 /* not much point in registering if we can't reply */
Johannes Berg2e161f72010-08-12 15:38:38 +02004852 if (!rdev->ops->mgmt_tx) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004853 err = -EOPNOTSUPP;
4854 goto out;
4855 }
4856
Johannes Berg2e161f72010-08-12 15:38:38 +02004857 err = cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
4858 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004859 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4860 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4861 out:
4862 cfg80211_unlock_rdev(rdev);
4863 dev_put(dev);
4864 unlock_rtnl:
4865 rtnl_unlock();
4866 return err;
4867}
4868
Johannes Berg2e161f72010-08-12 15:38:38 +02004869static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004870{
4871 struct cfg80211_registered_device *rdev;
4872 struct net_device *dev;
4873 struct ieee80211_channel *chan;
4874 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004875 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004876 u32 freq;
4877 int err;
4878 void *hdr;
4879 u64 cookie;
4880 struct sk_buff *msg;
4881
4882 if (!info->attrs[NL80211_ATTR_FRAME] ||
4883 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4884 return -EINVAL;
4885
4886 rtnl_lock();
4887
4888 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4889 if (err)
4890 goto unlock_rtnl;
4891
Johannes Berg2e161f72010-08-12 15:38:38 +02004892 if (!rdev->ops->mgmt_tx) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004893 err = -EOPNOTSUPP;
4894 goto out;
4895 }
4896
Johannes Berg9d38d852010-06-09 17:20:33 +02004897 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004898 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4899 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004900 err = -EOPNOTSUPP;
4901 goto out;
4902 }
4903
4904 if (!netif_running(dev)) {
4905 err = -ENETDOWN;
4906 goto out;
4907 }
4908
4909 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4910 channel_type = nla_get_u32(
4911 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4912 if (channel_type != NL80211_CHAN_NO_HT &&
4913 channel_type != NL80211_CHAN_HT20 &&
4914 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg579d7532010-05-18 14:36:34 +02004915 channel_type != NL80211_CHAN_HT40MINUS) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004916 err = -EINVAL;
4917 goto out;
Johannes Berg579d7532010-05-18 14:36:34 +02004918 }
Johannes Berg252aa632010-05-19 12:17:12 +02004919 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004920 }
4921
4922 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4923 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4924 if (chan == NULL) {
4925 err = -EINVAL;
4926 goto out;
4927 }
4928
4929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4930 if (!msg) {
4931 err = -ENOMEM;
4932 goto out;
4933 }
4934
4935 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004936 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004937
4938 if (IS_ERR(hdr)) {
4939 err = PTR_ERR(hdr);
4940 goto free_msg;
4941 }
Johannes Berg2e161f72010-08-12 15:38:38 +02004942 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4943 channel_type_valid,
4944 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4945 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4946 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004947 if (err)
4948 goto free_msg;
4949
4950 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4951
4952 genlmsg_end(msg, hdr);
4953 err = genlmsg_reply(msg, info);
4954 goto out;
4955
4956 nla_put_failure:
4957 err = -ENOBUFS;
4958 free_msg:
4959 nlmsg_free(msg);
4960 out:
4961 cfg80211_unlock_rdev(rdev);
4962 dev_put(dev);
4963unlock_rtnl:
4964 rtnl_unlock();
4965 return err;
4966}
4967
Kalle Valoffb9eb32010-02-17 17:58:10 +02004968static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4969{
4970 struct cfg80211_registered_device *rdev;
4971 struct wireless_dev *wdev;
4972 struct net_device *dev;
4973 u8 ps_state;
4974 bool state;
4975 int err;
4976
4977 if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4978 err = -EINVAL;
4979 goto out;
4980 }
4981
4982 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4983
4984 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4985 err = -EINVAL;
4986 goto out;
4987 }
4988
4989 rtnl_lock();
4990
4991 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4992 if (err)
4993 goto unlock_rdev;
4994
4995 wdev = dev->ieee80211_ptr;
4996
4997 if (!rdev->ops->set_power_mgmt) {
4998 err = -EOPNOTSUPP;
4999 goto unlock_rdev;
5000 }
5001
5002 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5003
5004 if (state == wdev->ps)
5005 goto unlock_rdev;
5006
5007 wdev->ps = state;
5008
5009 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
5010 wdev->ps_timeout))
5011 /* assume this means it's off */
5012 wdev->ps = false;
5013
5014unlock_rdev:
5015 cfg80211_unlock_rdev(rdev);
5016 dev_put(dev);
5017 rtnl_unlock();
5018
5019out:
5020 return err;
5021}
5022
5023static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5024{
5025 struct cfg80211_registered_device *rdev;
5026 enum nl80211_ps_state ps_state;
5027 struct wireless_dev *wdev;
5028 struct net_device *dev;
5029 struct sk_buff *msg;
5030 void *hdr;
5031 int err;
5032
5033 rtnl_lock();
5034
5035 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5036 if (err)
5037 goto unlock_rtnl;
5038
5039 wdev = dev->ieee80211_ptr;
5040
5041 if (!rdev->ops->set_power_mgmt) {
5042 err = -EOPNOTSUPP;
5043 goto out;
5044 }
5045
5046 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5047 if (!msg) {
5048 err = -ENOMEM;
5049 goto out;
5050 }
5051
5052 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5053 NL80211_CMD_GET_POWER_SAVE);
5054 if (!hdr) {
5055 err = -ENOMEM;
5056 goto free_msg;
5057 }
5058
5059 if (wdev->ps)
5060 ps_state = NL80211_PS_ENABLED;
5061 else
5062 ps_state = NL80211_PS_DISABLED;
5063
5064 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5065
5066 genlmsg_end(msg, hdr);
5067 err = genlmsg_reply(msg, info);
5068 goto out;
5069
5070nla_put_failure:
5071 err = -ENOBUFS;
5072
5073free_msg:
5074 nlmsg_free(msg);
5075
5076out:
5077 cfg80211_unlock_rdev(rdev);
5078 dev_put(dev);
5079
5080unlock_rtnl:
5081 rtnl_unlock();
5082
5083 return err;
5084}
5085
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005086static struct nla_policy
5087nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5088 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5089 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5090 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5091};
5092
5093static int nl80211_set_cqm_rssi(struct genl_info *info,
5094 s32 threshold, u32 hysteresis)
5095{
5096 struct cfg80211_registered_device *rdev;
5097 struct wireless_dev *wdev;
5098 struct net_device *dev;
5099 int err;
5100
5101 if (threshold > 0)
5102 return -EINVAL;
5103
5104 rtnl_lock();
5105
5106 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5107 if (err)
5108 goto unlock_rdev;
5109
5110 wdev = dev->ieee80211_ptr;
5111
5112 if (!rdev->ops->set_cqm_rssi_config) {
5113 err = -EOPNOTSUPP;
5114 goto unlock_rdev;
5115 }
5116
Johannes Berg074ac8d2010-09-16 14:58:22 +02005117 if (wdev->iftype != NL80211_IFTYPE_STATION &&
5118 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT) {
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005119 err = -EOPNOTSUPP;
5120 goto unlock_rdev;
5121 }
5122
5123 err = rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5124 threshold, hysteresis);
5125
5126unlock_rdev:
5127 cfg80211_unlock_rdev(rdev);
5128 dev_put(dev);
5129 rtnl_unlock();
5130
5131 return err;
5132}
5133
5134static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5135{
5136 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5137 struct nlattr *cqm;
5138 int err;
5139
5140 cqm = info->attrs[NL80211_ATTR_CQM];
5141 if (!cqm) {
5142 err = -EINVAL;
5143 goto out;
5144 }
5145
5146 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5147 nl80211_attr_cqm_policy);
5148 if (err)
5149 goto out;
5150
5151 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5152 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5153 s32 threshold;
5154 u32 hysteresis;
5155 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5156 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5157 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5158 } else
5159 err = -EINVAL;
5160
5161out:
5162 return err;
5163}
5164
Johannes Berg55682962007-09-20 13:09:35 -04005165static struct genl_ops nl80211_ops[] = {
5166 {
5167 .cmd = NL80211_CMD_GET_WIPHY,
5168 .doit = nl80211_get_wiphy,
5169 .dumpit = nl80211_dump_wiphy,
5170 .policy = nl80211_policy,
5171 /* can be retrieved by unprivileged users */
5172 },
5173 {
5174 .cmd = NL80211_CMD_SET_WIPHY,
5175 .doit = nl80211_set_wiphy,
5176 .policy = nl80211_policy,
5177 .flags = GENL_ADMIN_PERM,
5178 },
5179 {
5180 .cmd = NL80211_CMD_GET_INTERFACE,
5181 .doit = nl80211_get_interface,
5182 .dumpit = nl80211_dump_interface,
5183 .policy = nl80211_policy,
5184 /* can be retrieved by unprivileged users */
5185 },
5186 {
5187 .cmd = NL80211_CMD_SET_INTERFACE,
5188 .doit = nl80211_set_interface,
5189 .policy = nl80211_policy,
5190 .flags = GENL_ADMIN_PERM,
5191 },
5192 {
5193 .cmd = NL80211_CMD_NEW_INTERFACE,
5194 .doit = nl80211_new_interface,
5195 .policy = nl80211_policy,
5196 .flags = GENL_ADMIN_PERM,
5197 },
5198 {
5199 .cmd = NL80211_CMD_DEL_INTERFACE,
5200 .doit = nl80211_del_interface,
5201 .policy = nl80211_policy,
5202 .flags = GENL_ADMIN_PERM,
5203 },
Johannes Berg41ade002007-12-19 02:03:29 +01005204 {
5205 .cmd = NL80211_CMD_GET_KEY,
5206 .doit = nl80211_get_key,
5207 .policy = nl80211_policy,
5208 .flags = GENL_ADMIN_PERM,
5209 },
5210 {
5211 .cmd = NL80211_CMD_SET_KEY,
5212 .doit = nl80211_set_key,
5213 .policy = nl80211_policy,
5214 .flags = GENL_ADMIN_PERM,
5215 },
5216 {
5217 .cmd = NL80211_CMD_NEW_KEY,
5218 .doit = nl80211_new_key,
5219 .policy = nl80211_policy,
5220 .flags = GENL_ADMIN_PERM,
5221 },
5222 {
5223 .cmd = NL80211_CMD_DEL_KEY,
5224 .doit = nl80211_del_key,
5225 .policy = nl80211_policy,
5226 .flags = GENL_ADMIN_PERM,
5227 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005228 {
5229 .cmd = NL80211_CMD_SET_BEACON,
5230 .policy = nl80211_policy,
5231 .flags = GENL_ADMIN_PERM,
5232 .doit = nl80211_addset_beacon,
5233 },
5234 {
5235 .cmd = NL80211_CMD_NEW_BEACON,
5236 .policy = nl80211_policy,
5237 .flags = GENL_ADMIN_PERM,
5238 .doit = nl80211_addset_beacon,
5239 },
5240 {
5241 .cmd = NL80211_CMD_DEL_BEACON,
5242 .policy = nl80211_policy,
5243 .flags = GENL_ADMIN_PERM,
5244 .doit = nl80211_del_beacon,
5245 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005246 {
5247 .cmd = NL80211_CMD_GET_STATION,
5248 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005249 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005250 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01005251 },
5252 {
5253 .cmd = NL80211_CMD_SET_STATION,
5254 .doit = nl80211_set_station,
5255 .policy = nl80211_policy,
5256 .flags = GENL_ADMIN_PERM,
5257 },
5258 {
5259 .cmd = NL80211_CMD_NEW_STATION,
5260 .doit = nl80211_new_station,
5261 .policy = nl80211_policy,
5262 .flags = GENL_ADMIN_PERM,
5263 },
5264 {
5265 .cmd = NL80211_CMD_DEL_STATION,
5266 .doit = nl80211_del_station,
5267 .policy = nl80211_policy,
5268 .flags = GENL_ADMIN_PERM,
5269 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005270 {
5271 .cmd = NL80211_CMD_GET_MPATH,
5272 .doit = nl80211_get_mpath,
5273 .dumpit = nl80211_dump_mpath,
5274 .policy = nl80211_policy,
5275 .flags = GENL_ADMIN_PERM,
5276 },
5277 {
5278 .cmd = NL80211_CMD_SET_MPATH,
5279 .doit = nl80211_set_mpath,
5280 .policy = nl80211_policy,
5281 .flags = GENL_ADMIN_PERM,
5282 },
5283 {
5284 .cmd = NL80211_CMD_NEW_MPATH,
5285 .doit = nl80211_new_mpath,
5286 .policy = nl80211_policy,
5287 .flags = GENL_ADMIN_PERM,
5288 },
5289 {
5290 .cmd = NL80211_CMD_DEL_MPATH,
5291 .doit = nl80211_del_mpath,
5292 .policy = nl80211_policy,
5293 .flags = GENL_ADMIN_PERM,
5294 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005295 {
5296 .cmd = NL80211_CMD_SET_BSS,
5297 .doit = nl80211_set_bss,
5298 .policy = nl80211_policy,
5299 .flags = GENL_ADMIN_PERM,
5300 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005301 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005302 .cmd = NL80211_CMD_GET_REG,
5303 .doit = nl80211_get_reg,
5304 .policy = nl80211_policy,
5305 /* can be retrieved by unprivileged users */
5306 },
5307 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005308 .cmd = NL80211_CMD_SET_REG,
5309 .doit = nl80211_set_reg,
5310 .policy = nl80211_policy,
5311 .flags = GENL_ADMIN_PERM,
5312 },
5313 {
5314 .cmd = NL80211_CMD_REQ_SET_REG,
5315 .doit = nl80211_req_set_reg,
5316 .policy = nl80211_policy,
5317 .flags = GENL_ADMIN_PERM,
5318 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005319 {
5320 .cmd = NL80211_CMD_GET_MESH_PARAMS,
5321 .doit = nl80211_get_mesh_params,
5322 .policy = nl80211_policy,
5323 /* can be retrieved by unprivileged users */
5324 },
5325 {
5326 .cmd = NL80211_CMD_SET_MESH_PARAMS,
5327 .doit = nl80211_set_mesh_params,
5328 .policy = nl80211_policy,
5329 .flags = GENL_ADMIN_PERM,
5330 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005331 {
Johannes Berg2a519312009-02-10 21:25:55 +01005332 .cmd = NL80211_CMD_TRIGGER_SCAN,
5333 .doit = nl80211_trigger_scan,
5334 .policy = nl80211_policy,
5335 .flags = GENL_ADMIN_PERM,
5336 },
5337 {
5338 .cmd = NL80211_CMD_GET_SCAN,
5339 .policy = nl80211_policy,
5340 .dumpit = nl80211_dump_scan,
5341 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005342 {
5343 .cmd = NL80211_CMD_AUTHENTICATE,
5344 .doit = nl80211_authenticate,
5345 .policy = nl80211_policy,
5346 .flags = GENL_ADMIN_PERM,
5347 },
5348 {
5349 .cmd = NL80211_CMD_ASSOCIATE,
5350 .doit = nl80211_associate,
5351 .policy = nl80211_policy,
5352 .flags = GENL_ADMIN_PERM,
5353 },
5354 {
5355 .cmd = NL80211_CMD_DEAUTHENTICATE,
5356 .doit = nl80211_deauthenticate,
5357 .policy = nl80211_policy,
5358 .flags = GENL_ADMIN_PERM,
5359 },
5360 {
5361 .cmd = NL80211_CMD_DISASSOCIATE,
5362 .doit = nl80211_disassociate,
5363 .policy = nl80211_policy,
5364 .flags = GENL_ADMIN_PERM,
5365 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005366 {
5367 .cmd = NL80211_CMD_JOIN_IBSS,
5368 .doit = nl80211_join_ibss,
5369 .policy = nl80211_policy,
5370 .flags = GENL_ADMIN_PERM,
5371 },
5372 {
5373 .cmd = NL80211_CMD_LEAVE_IBSS,
5374 .doit = nl80211_leave_ibss,
5375 .policy = nl80211_policy,
5376 .flags = GENL_ADMIN_PERM,
5377 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005378#ifdef CONFIG_NL80211_TESTMODE
5379 {
5380 .cmd = NL80211_CMD_TESTMODE,
5381 .doit = nl80211_testmode_do,
5382 .policy = nl80211_policy,
5383 .flags = GENL_ADMIN_PERM,
5384 },
5385#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005386 {
5387 .cmd = NL80211_CMD_CONNECT,
5388 .doit = nl80211_connect,
5389 .policy = nl80211_policy,
5390 .flags = GENL_ADMIN_PERM,
5391 },
5392 {
5393 .cmd = NL80211_CMD_DISCONNECT,
5394 .doit = nl80211_disconnect,
5395 .policy = nl80211_policy,
5396 .flags = GENL_ADMIN_PERM,
5397 },
Johannes Berg463d0182009-07-14 00:33:35 +02005398 {
5399 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5400 .doit = nl80211_wiphy_netns,
5401 .policy = nl80211_policy,
5402 .flags = GENL_ADMIN_PERM,
5403 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005404 {
5405 .cmd = NL80211_CMD_GET_SURVEY,
5406 .policy = nl80211_policy,
5407 .dumpit = nl80211_dump_survey,
5408 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005409 {
5410 .cmd = NL80211_CMD_SET_PMKSA,
5411 .doit = nl80211_setdel_pmksa,
5412 .policy = nl80211_policy,
5413 .flags = GENL_ADMIN_PERM,
5414 },
5415 {
5416 .cmd = NL80211_CMD_DEL_PMKSA,
5417 .doit = nl80211_setdel_pmksa,
5418 .policy = nl80211_policy,
5419 .flags = GENL_ADMIN_PERM,
5420 },
5421 {
5422 .cmd = NL80211_CMD_FLUSH_PMKSA,
5423 .doit = nl80211_flush_pmksa,
5424 .policy = nl80211_policy,
5425 .flags = GENL_ADMIN_PERM,
5426 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005427 {
5428 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5429 .doit = nl80211_remain_on_channel,
5430 .policy = nl80211_policy,
5431 .flags = GENL_ADMIN_PERM,
5432 },
5433 {
5434 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5435 .doit = nl80211_cancel_remain_on_channel,
5436 .policy = nl80211_policy,
5437 .flags = GENL_ADMIN_PERM,
5438 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005439 {
5440 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5441 .doit = nl80211_set_tx_bitrate_mask,
5442 .policy = nl80211_policy,
5443 .flags = GENL_ADMIN_PERM,
5444 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005445 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005446 .cmd = NL80211_CMD_REGISTER_FRAME,
5447 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005448 .policy = nl80211_policy,
5449 .flags = GENL_ADMIN_PERM,
5450 },
5451 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005452 .cmd = NL80211_CMD_FRAME,
5453 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005454 .policy = nl80211_policy,
5455 .flags = GENL_ADMIN_PERM,
5456 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005457 {
5458 .cmd = NL80211_CMD_SET_POWER_SAVE,
5459 .doit = nl80211_set_power_save,
5460 .policy = nl80211_policy,
5461 .flags = GENL_ADMIN_PERM,
5462 },
5463 {
5464 .cmd = NL80211_CMD_GET_POWER_SAVE,
5465 .doit = nl80211_get_power_save,
5466 .policy = nl80211_policy,
5467 /* can be retrieved by unprivileged users */
5468 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005469 {
5470 .cmd = NL80211_CMD_SET_CQM,
5471 .doit = nl80211_set_cqm,
5472 .policy = nl80211_policy,
5473 .flags = GENL_ADMIN_PERM,
5474 },
Johannes Bergf444de02010-05-05 15:25:02 +02005475 {
5476 .cmd = NL80211_CMD_SET_CHANNEL,
5477 .doit = nl80211_set_channel,
5478 .policy = nl80211_policy,
5479 .flags = GENL_ADMIN_PERM,
5480 },
Johannes Berg55682962007-09-20 13:09:35 -04005481};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005482
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005483static struct genl_multicast_group nl80211_mlme_mcgrp = {
5484 .name = "mlme",
5485};
Johannes Berg55682962007-09-20 13:09:35 -04005486
5487/* multicast groups */
5488static struct genl_multicast_group nl80211_config_mcgrp = {
5489 .name = "config",
5490};
Johannes Berg2a519312009-02-10 21:25:55 +01005491static struct genl_multicast_group nl80211_scan_mcgrp = {
5492 .name = "scan",
5493};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005494static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5495 .name = "regulatory",
5496};
Johannes Berg55682962007-09-20 13:09:35 -04005497
5498/* notification functions */
5499
5500void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5501{
5502 struct sk_buff *msg;
5503
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005504 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005505 if (!msg)
5506 return;
5507
5508 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5509 nlmsg_free(msg);
5510 return;
5511 }
5512
Johannes Berg463d0182009-07-14 00:33:35 +02005513 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5514 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005515}
5516
Johannes Berg362a4152009-05-24 16:43:15 +02005517static int nl80211_add_scan_req(struct sk_buff *msg,
5518 struct cfg80211_registered_device *rdev)
5519{
5520 struct cfg80211_scan_request *req = rdev->scan_req;
5521 struct nlattr *nest;
5522 int i;
5523
Johannes Berg667503dd2009-07-07 03:56:11 +02005524 ASSERT_RDEV_LOCK(rdev);
5525
Johannes Berg362a4152009-05-24 16:43:15 +02005526 if (WARN_ON(!req))
5527 return 0;
5528
5529 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5530 if (!nest)
5531 goto nla_put_failure;
5532 for (i = 0; i < req->n_ssids; i++)
5533 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5534 nla_nest_end(msg, nest);
5535
5536 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5537 if (!nest)
5538 goto nla_put_failure;
5539 for (i = 0; i < req->n_channels; i++)
5540 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5541 nla_nest_end(msg, nest);
5542
5543 if (req->ie)
5544 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5545
5546 return 0;
5547 nla_put_failure:
5548 return -ENOBUFS;
5549}
5550
Johannes Berga538e2d2009-06-16 19:56:42 +02005551static int nl80211_send_scan_msg(struct sk_buff *msg,
5552 struct cfg80211_registered_device *rdev,
5553 struct net_device *netdev,
5554 u32 pid, u32 seq, int flags,
5555 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005556{
5557 void *hdr;
5558
5559 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5560 if (!hdr)
5561 return -1;
5562
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005563 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005564 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5565
Johannes Berg362a4152009-05-24 16:43:15 +02005566 /* ignore errors and send incomplete event anyway */
5567 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005568
5569 return genlmsg_end(msg, hdr);
5570
5571 nla_put_failure:
5572 genlmsg_cancel(msg, hdr);
5573 return -EMSGSIZE;
5574}
5575
Johannes Berga538e2d2009-06-16 19:56:42 +02005576void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5577 struct net_device *netdev)
5578{
5579 struct sk_buff *msg;
5580
5581 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5582 if (!msg)
5583 return;
5584
5585 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5586 NL80211_CMD_TRIGGER_SCAN) < 0) {
5587 nlmsg_free(msg);
5588 return;
5589 }
5590
Johannes Berg463d0182009-07-14 00:33:35 +02005591 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5592 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005593}
5594
Johannes Berg2a519312009-02-10 21:25:55 +01005595void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5596 struct net_device *netdev)
5597{
5598 struct sk_buff *msg;
5599
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005600 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005601 if (!msg)
5602 return;
5603
Johannes Berga538e2d2009-06-16 19:56:42 +02005604 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5605 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005606 nlmsg_free(msg);
5607 return;
5608 }
5609
Johannes Berg463d0182009-07-14 00:33:35 +02005610 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5611 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005612}
5613
5614void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5615 struct net_device *netdev)
5616{
5617 struct sk_buff *msg;
5618
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005620 if (!msg)
5621 return;
5622
Johannes Berga538e2d2009-06-16 19:56:42 +02005623 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5624 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005625 nlmsg_free(msg);
5626 return;
5627 }
5628
Johannes Berg463d0182009-07-14 00:33:35 +02005629 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5630 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005631}
5632
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005633/*
5634 * This can happen on global regulatory changes or device specific settings
5635 * based on custom world regulatory domains.
5636 */
5637void nl80211_send_reg_change_event(struct regulatory_request *request)
5638{
5639 struct sk_buff *msg;
5640 void *hdr;
5641
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005642 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005643 if (!msg)
5644 return;
5645
5646 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5647 if (!hdr) {
5648 nlmsg_free(msg);
5649 return;
5650 }
5651
5652 /* Userspace can always count this one always being set */
5653 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5654
5655 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5656 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5657 NL80211_REGDOM_TYPE_WORLD);
5658 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5659 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5660 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5661 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5662 request->intersect)
5663 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5664 NL80211_REGDOM_TYPE_INTERSECTION);
5665 else {
5666 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5667 NL80211_REGDOM_TYPE_COUNTRY);
5668 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5669 }
5670
5671 if (wiphy_idx_valid(request->wiphy_idx))
5672 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5673
5674 if (genlmsg_end(msg, hdr) < 0) {
5675 nlmsg_free(msg);
5676 return;
5677 }
5678
Johannes Bergbc43b282009-07-25 10:54:13 +02005679 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005680 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005681 GFP_ATOMIC);
5682 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005683
5684 return;
5685
5686nla_put_failure:
5687 genlmsg_cancel(msg, hdr);
5688 nlmsg_free(msg);
5689}
5690
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005691static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5692 struct net_device *netdev,
5693 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005694 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005695{
5696 struct sk_buff *msg;
5697 void *hdr;
5698
Johannes Berge6d6e342009-07-01 21:26:47 +02005699 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005700 if (!msg)
5701 return;
5702
5703 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5704 if (!hdr) {
5705 nlmsg_free(msg);
5706 return;
5707 }
5708
5709 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5710 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5711 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5712
5713 if (genlmsg_end(msg, hdr) < 0) {
5714 nlmsg_free(msg);
5715 return;
5716 }
5717
Johannes Berg463d0182009-07-14 00:33:35 +02005718 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5719 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005720 return;
5721
5722 nla_put_failure:
5723 genlmsg_cancel(msg, hdr);
5724 nlmsg_free(msg);
5725}
5726
5727void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005728 struct net_device *netdev, const u8 *buf,
5729 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005730{
5731 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005732 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005733}
5734
5735void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5736 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005737 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005738{
Johannes Berge6d6e342009-07-01 21:26:47 +02005739 nl80211_send_mlme_event(rdev, netdev, buf, len,
5740 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005741}
5742
Jouni Malinen53b46b82009-03-27 20:53:56 +02005743void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005744 struct net_device *netdev, const u8 *buf,
5745 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005746{
5747 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005748 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005749}
5750
Jouni Malinen53b46b82009-03-27 20:53:56 +02005751void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5752 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005753 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005754{
5755 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005756 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005757}
5758
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005759static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5760 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005761 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005762{
5763 struct sk_buff *msg;
5764 void *hdr;
5765
Johannes Berge6d6e342009-07-01 21:26:47 +02005766 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005767 if (!msg)
5768 return;
5769
5770 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5771 if (!hdr) {
5772 nlmsg_free(msg);
5773 return;
5774 }
5775
5776 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5777 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5778 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5779 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5780
5781 if (genlmsg_end(msg, hdr) < 0) {
5782 nlmsg_free(msg);
5783 return;
5784 }
5785
Johannes Berg463d0182009-07-14 00:33:35 +02005786 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5787 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005788 return;
5789
5790 nla_put_failure:
5791 genlmsg_cancel(msg, hdr);
5792 nlmsg_free(msg);
5793}
5794
5795void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005796 struct net_device *netdev, const u8 *addr,
5797 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005798{
5799 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005800 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005801}
5802
5803void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005804 struct net_device *netdev, const u8 *addr,
5805 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005806{
Johannes Berge6d6e342009-07-01 21:26:47 +02005807 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5808 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005809}
5810
Samuel Ortizb23aa672009-07-01 21:26:54 +02005811void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5812 struct net_device *netdev, const u8 *bssid,
5813 const u8 *req_ie, size_t req_ie_len,
5814 const u8 *resp_ie, size_t resp_ie_len,
5815 u16 status, gfp_t gfp)
5816{
5817 struct sk_buff *msg;
5818 void *hdr;
5819
5820 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5821 if (!msg)
5822 return;
5823
5824 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5825 if (!hdr) {
5826 nlmsg_free(msg);
5827 return;
5828 }
5829
5830 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5831 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5832 if (bssid)
5833 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5834 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5835 if (req_ie)
5836 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5837 if (resp_ie)
5838 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5839
5840 if (genlmsg_end(msg, hdr) < 0) {
5841 nlmsg_free(msg);
5842 return;
5843 }
5844
Johannes Berg463d0182009-07-14 00:33:35 +02005845 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5846 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005847 return;
5848
5849 nla_put_failure:
5850 genlmsg_cancel(msg, hdr);
5851 nlmsg_free(msg);
5852
5853}
5854
5855void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5856 struct net_device *netdev, const u8 *bssid,
5857 const u8 *req_ie, size_t req_ie_len,
5858 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5859{
5860 struct sk_buff *msg;
5861 void *hdr;
5862
5863 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5864 if (!msg)
5865 return;
5866
5867 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5868 if (!hdr) {
5869 nlmsg_free(msg);
5870 return;
5871 }
5872
5873 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5874 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5875 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5876 if (req_ie)
5877 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5878 if (resp_ie)
5879 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5880
5881 if (genlmsg_end(msg, hdr) < 0) {
5882 nlmsg_free(msg);
5883 return;
5884 }
5885
Johannes Berg463d0182009-07-14 00:33:35 +02005886 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5887 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005888 return;
5889
5890 nla_put_failure:
5891 genlmsg_cancel(msg, hdr);
5892 nlmsg_free(msg);
5893
5894}
5895
5896void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5897 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02005898 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005899{
5900 struct sk_buff *msg;
5901 void *hdr;
5902
Johannes Berg667503dd2009-07-07 03:56:11 +02005903 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005904 if (!msg)
5905 return;
5906
5907 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5908 if (!hdr) {
5909 nlmsg_free(msg);
5910 return;
5911 }
5912
5913 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5914 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5915 if (from_ap && reason)
5916 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5917 if (from_ap)
5918 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5919 if (ie)
5920 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5921
5922 if (genlmsg_end(msg, hdr) < 0) {
5923 nlmsg_free(msg);
5924 return;
5925 }
5926
Johannes Berg463d0182009-07-14 00:33:35 +02005927 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5928 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005929 return;
5930
5931 nla_put_failure:
5932 genlmsg_cancel(msg, hdr);
5933 nlmsg_free(msg);
5934
5935}
5936
Johannes Berg04a773a2009-04-19 21:24:32 +02005937void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5938 struct net_device *netdev, const u8 *bssid,
5939 gfp_t gfp)
5940{
5941 struct sk_buff *msg;
5942 void *hdr;
5943
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005944 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005945 if (!msg)
5946 return;
5947
5948 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5949 if (!hdr) {
5950 nlmsg_free(msg);
5951 return;
5952 }
5953
5954 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5955 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5956 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5957
5958 if (genlmsg_end(msg, hdr) < 0) {
5959 nlmsg_free(msg);
5960 return;
5961 }
5962
Johannes Berg463d0182009-07-14 00:33:35 +02005963 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5964 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005965 return;
5966
5967 nla_put_failure:
5968 genlmsg_cancel(msg, hdr);
5969 nlmsg_free(msg);
5970}
5971
Jouni Malinena3b8b052009-03-27 21:59:49 +02005972void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5973 struct net_device *netdev, const u8 *addr,
5974 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005975 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005976{
5977 struct sk_buff *msg;
5978 void *hdr;
5979
Johannes Berge6d6e342009-07-01 21:26:47 +02005980 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005981 if (!msg)
5982 return;
5983
5984 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5985 if (!hdr) {
5986 nlmsg_free(msg);
5987 return;
5988 }
5989
5990 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5991 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5992 if (addr)
5993 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5994 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5995 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5996 if (tsc)
5997 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5998
5999 if (genlmsg_end(msg, hdr) < 0) {
6000 nlmsg_free(msg);
6001 return;
6002 }
6003
Johannes Berg463d0182009-07-14 00:33:35 +02006004 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6005 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006006 return;
6007
6008 nla_put_failure:
6009 genlmsg_cancel(msg, hdr);
6010 nlmsg_free(msg);
6011}
6012
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006013void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6014 struct ieee80211_channel *channel_before,
6015 struct ieee80211_channel *channel_after)
6016{
6017 struct sk_buff *msg;
6018 void *hdr;
6019 struct nlattr *nl_freq;
6020
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006021 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006022 if (!msg)
6023 return;
6024
6025 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6026 if (!hdr) {
6027 nlmsg_free(msg);
6028 return;
6029 }
6030
6031 /*
6032 * Since we are applying the beacon hint to a wiphy we know its
6033 * wiphy_idx is valid
6034 */
6035 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6036
6037 /* Before */
6038 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6039 if (!nl_freq)
6040 goto nla_put_failure;
6041 if (nl80211_msg_put_channel(msg, channel_before))
6042 goto nla_put_failure;
6043 nla_nest_end(msg, nl_freq);
6044
6045 /* After */
6046 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6047 if (!nl_freq)
6048 goto nla_put_failure;
6049 if (nl80211_msg_put_channel(msg, channel_after))
6050 goto nla_put_failure;
6051 nla_nest_end(msg, nl_freq);
6052
6053 if (genlmsg_end(msg, hdr) < 0) {
6054 nlmsg_free(msg);
6055 return;
6056 }
6057
Johannes Berg463d0182009-07-14 00:33:35 +02006058 rcu_read_lock();
6059 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6060 GFP_ATOMIC);
6061 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006062
6063 return;
6064
6065nla_put_failure:
6066 genlmsg_cancel(msg, hdr);
6067 nlmsg_free(msg);
6068}
6069
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006070static void nl80211_send_remain_on_chan_event(
6071 int cmd, struct cfg80211_registered_device *rdev,
6072 struct net_device *netdev, u64 cookie,
6073 struct ieee80211_channel *chan,
6074 enum nl80211_channel_type channel_type,
6075 unsigned int duration, gfp_t gfp)
6076{
6077 struct sk_buff *msg;
6078 void *hdr;
6079
6080 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6081 if (!msg)
6082 return;
6083
6084 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6085 if (!hdr) {
6086 nlmsg_free(msg);
6087 return;
6088 }
6089
6090 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6091 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6092 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6093 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6094 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6095
6096 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6097 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6098
6099 if (genlmsg_end(msg, hdr) < 0) {
6100 nlmsg_free(msg);
6101 return;
6102 }
6103
6104 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6105 nl80211_mlme_mcgrp.id, gfp);
6106 return;
6107
6108 nla_put_failure:
6109 genlmsg_cancel(msg, hdr);
6110 nlmsg_free(msg);
6111}
6112
6113void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6114 struct net_device *netdev, u64 cookie,
6115 struct ieee80211_channel *chan,
6116 enum nl80211_channel_type channel_type,
6117 unsigned int duration, gfp_t gfp)
6118{
6119 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6120 rdev, netdev, cookie, chan,
6121 channel_type, duration, gfp);
6122}
6123
6124void nl80211_send_remain_on_channel_cancel(
6125 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6126 u64 cookie, struct ieee80211_channel *chan,
6127 enum nl80211_channel_type channel_type, gfp_t gfp)
6128{
6129 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6130 rdev, netdev, cookie, chan,
6131 channel_type, 0, gfp);
6132}
6133
Johannes Berg98b62182009-12-23 13:15:44 +01006134void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6135 struct net_device *dev, const u8 *mac_addr,
6136 struct station_info *sinfo, gfp_t gfp)
6137{
6138 struct sk_buff *msg;
6139
6140 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6141 if (!msg)
6142 return;
6143
6144 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6145 nlmsg_free(msg);
6146 return;
6147 }
6148
6149 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6150 nl80211_mlme_mcgrp.id, gfp);
6151}
6152
Johannes Berg2e161f72010-08-12 15:38:38 +02006153int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6154 struct net_device *netdev, u32 nlpid,
6155 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006156{
6157 struct sk_buff *msg;
6158 void *hdr;
6159 int err;
6160
6161 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6162 if (!msg)
6163 return -ENOMEM;
6164
Johannes Berg2e161f72010-08-12 15:38:38 +02006165 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006166 if (!hdr) {
6167 nlmsg_free(msg);
6168 return -ENOMEM;
6169 }
6170
6171 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6172 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6173 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6174 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6175
6176 err = genlmsg_end(msg, hdr);
6177 if (err < 0) {
6178 nlmsg_free(msg);
6179 return err;
6180 }
6181
6182 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6183 if (err < 0)
6184 return err;
6185 return 0;
6186
6187 nla_put_failure:
6188 genlmsg_cancel(msg, hdr);
6189 nlmsg_free(msg);
6190 return -ENOBUFS;
6191}
6192
Johannes Berg2e161f72010-08-12 15:38:38 +02006193void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6194 struct net_device *netdev, u64 cookie,
6195 const u8 *buf, size_t len, bool ack,
6196 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006197{
6198 struct sk_buff *msg;
6199 void *hdr;
6200
6201 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6202 if (!msg)
6203 return;
6204
Johannes Berg2e161f72010-08-12 15:38:38 +02006205 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02006206 if (!hdr) {
6207 nlmsg_free(msg);
6208 return;
6209 }
6210
6211 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6212 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6213 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6214 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6215 if (ack)
6216 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6217
6218 if (genlmsg_end(msg, hdr) < 0) {
6219 nlmsg_free(msg);
6220 return;
6221 }
6222
6223 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6224 return;
6225
6226 nla_put_failure:
6227 genlmsg_cancel(msg, hdr);
6228 nlmsg_free(msg);
6229}
6230
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006231void
6232nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6233 struct net_device *netdev,
6234 enum nl80211_cqm_rssi_threshold_event rssi_event,
6235 gfp_t gfp)
6236{
6237 struct sk_buff *msg;
6238 struct nlattr *pinfoattr;
6239 void *hdr;
6240
6241 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6242 if (!msg)
6243 return;
6244
6245 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6246 if (!hdr) {
6247 nlmsg_free(msg);
6248 return;
6249 }
6250
6251 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6253
6254 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6255 if (!pinfoattr)
6256 goto nla_put_failure;
6257
6258 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6259 rssi_event);
6260
6261 nla_nest_end(msg, pinfoattr);
6262
6263 if (genlmsg_end(msg, hdr) < 0) {
6264 nlmsg_free(msg);
6265 return;
6266 }
6267
6268 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6269 nl80211_mlme_mcgrp.id, gfp);
6270 return;
6271
6272 nla_put_failure:
6273 genlmsg_cancel(msg, hdr);
6274 nlmsg_free(msg);
6275}
6276
Jouni Malinen026331c2010-02-15 12:53:10 +02006277static int nl80211_netlink_notify(struct notifier_block * nb,
6278 unsigned long state,
6279 void *_notify)
6280{
6281 struct netlink_notify *notify = _notify;
6282 struct cfg80211_registered_device *rdev;
6283 struct wireless_dev *wdev;
6284
6285 if (state != NETLINK_URELEASE)
6286 return NOTIFY_DONE;
6287
6288 rcu_read_lock();
6289
6290 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6291 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006292 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006293
6294 rcu_read_unlock();
6295
6296 return NOTIFY_DONE;
6297}
6298
6299static struct notifier_block nl80211_netlink_notifier = {
6300 .notifier_call = nl80211_netlink_notify,
6301};
6302
Johannes Berg55682962007-09-20 13:09:35 -04006303/* initialisation/exit functions */
6304
6305int nl80211_init(void)
6306{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006307 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006308
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006309 err = genl_register_family_with_ops(&nl80211_fam,
6310 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006311 if (err)
6312 return err;
6313
Johannes Berg55682962007-09-20 13:09:35 -04006314 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6315 if (err)
6316 goto err_out;
6317
Johannes Berg2a519312009-02-10 21:25:55 +01006318 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6319 if (err)
6320 goto err_out;
6321
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006322 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6323 if (err)
6324 goto err_out;
6325
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006326 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6327 if (err)
6328 goto err_out;
6329
Johannes Bergaff89a92009-07-01 21:26:51 +02006330#ifdef CONFIG_NL80211_TESTMODE
6331 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6332 if (err)
6333 goto err_out;
6334#endif
6335
Jouni Malinen026331c2010-02-15 12:53:10 +02006336 err = netlink_register_notifier(&nl80211_netlink_notifier);
6337 if (err)
6338 goto err_out;
6339
Johannes Berg55682962007-09-20 13:09:35 -04006340 return 0;
6341 err_out:
6342 genl_unregister_family(&nl80211_fam);
6343 return err;
6344}
6345
6346void nl80211_exit(void)
6347{
Jouni Malinen026331c2010-02-15 12:53:10 +02006348 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006349 genl_unregister_family(&nl80211_fam);
6350}