blob: da450ef1fc7eb30654dcfe2b65c018ec5877a20f [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Johannes Berg08645122009-05-11 13:54:58 +02004 * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg55682962007-09-20 13:09:35 -040017#include <net/genetlink.h>
18#include <net/cfg80211.h>
19#include "core.h"
20#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040022
23/* the netlink family */
24static struct genl_family nl80211_fam = {
25 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
26 .name = "nl80211", /* have users key off the name instead */
27 .hdrsize = 0, /* no private header */
28 .version = 1, /* no particular meaning now */
29 .maxattr = NL80211_ATTR_MAX,
30};
31
Johannes Berg79c97e92009-07-07 03:56:12 +020032/* internal helper: get rdev and dev */
33static int get_rdev_dev_by_info_ifindex(struct nlattr **attrs,
34 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040035 struct net_device **dev)
36{
37 int ifindex;
38
Johannes Bergbba95fe2008-07-29 13:22:51 +020039 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040040 return -EINVAL;
41
Johannes Bergbba95fe2008-07-29 13:22:51 +020042 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg55682962007-09-20 13:09:35 -040043 *dev = dev_get_by_index(&init_net, ifindex);
44 if (!*dev)
45 return -ENODEV;
46
Johannes Berg79c97e92009-07-07 03:56:12 +020047 *rdev = cfg80211_get_dev_from_ifindex(ifindex);
48 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040049 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020050 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040051 }
52
53 return 0;
54}
55
56/* policy for the attributes */
57static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
58 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
59 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070060 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020061 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020062 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053063 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020064 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
65 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
66 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
67 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040068
69 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
70 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
71 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010072
73 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020074 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010075
Johannes Bergb9454e82009-07-08 13:29:08 +020076 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010077 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
78 .len = WLAN_MAX_KEY_LEN },
79 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
80 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
81 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030082 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010083
84 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
85 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
86 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
87 .len = IEEE80211_MAX_DATA_LEN },
88 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
89 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010090 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
91 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
92 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
93 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
94 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010095 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010096 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020097 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010098 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
99 .len = IEEE80211_MAX_MESH_ID_LEN },
100 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300101
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700102 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
103 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
104
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300105 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
106 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
107 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200108 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
109 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300110
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700111 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
112
Jouni Malinen36aedc92008-08-25 11:58:58 +0300113 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
114 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200115
116 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
117 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
118 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100119 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
120 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200121
122 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
123 .len = IEEE80211_MAX_SSID_LEN },
124 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
125 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200126 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300127 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300128 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300129 [NL80211_ATTR_STA_FLAGS2] = {
130 .len = sizeof(struct nl80211_sta_flag_update),
131 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300132 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200133 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
134 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
135 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400136};
137
Johannes Bergb9454e82009-07-08 13:29:08 +0200138/* policy for the attributes */
139static struct nla_policy
140nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200141 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200142 [NL80211_KEY_IDX] = { .type = NLA_U8 },
143 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
144 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
145 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
146 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
147};
148
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100149/* IE validation */
150static bool is_valid_ie_attr(const struct nlattr *attr)
151{
152 const u8 *pos;
153 int len;
154
155 if (!attr)
156 return true;
157
158 pos = nla_data(attr);
159 len = nla_len(attr);
160
161 while (len) {
162 u8 elemlen;
163
164 if (len < 2)
165 return false;
166 len -= 2;
167
168 elemlen = pos[1];
169 if (elemlen > len)
170 return false;
171
172 len -= elemlen;
173 pos += 2 + elemlen;
174 }
175
176 return true;
177}
178
Johannes Berg55682962007-09-20 13:09:35 -0400179/* message building helper */
180static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
181 int flags, u8 cmd)
182{
183 /* since there is no private header just add the generic one */
184 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
185}
186
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400187static int nl80211_msg_put_channel(struct sk_buff *msg,
188 struct ieee80211_channel *chan)
189{
190 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
191 chan->center_freq);
192
193 if (chan->flags & IEEE80211_CHAN_DISABLED)
194 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
195 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
196 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
197 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
198 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
199 if (chan->flags & IEEE80211_CHAN_RADAR)
200 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
201
202 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
203 DBM_TO_MBM(chan->max_power));
204
205 return 0;
206
207 nla_put_failure:
208 return -ENOBUFS;
209}
210
Johannes Berg55682962007-09-20 13:09:35 -0400211/* netlink command implementations */
212
Johannes Bergb9454e82009-07-08 13:29:08 +0200213struct key_parse {
214 struct key_params p;
215 int idx;
216 bool def, defmgmt;
217};
218
219static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
220{
221 struct nlattr *tb[NL80211_KEY_MAX + 1];
222 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
223 nl80211_key_policy);
224 if (err)
225 return err;
226
227 k->def = !!tb[NL80211_KEY_DEFAULT];
228 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
229
230 if (tb[NL80211_KEY_IDX])
231 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
232
233 if (tb[NL80211_KEY_DATA]) {
234 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
235 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
236 }
237
238 if (tb[NL80211_KEY_SEQ]) {
239 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
240 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
241 }
242
243 if (tb[NL80211_KEY_CIPHER])
244 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
245
246 return 0;
247}
248
249static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
250{
251 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
252 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
253 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
254 }
255
256 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
257 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
258 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
259 }
260
261 if (info->attrs[NL80211_ATTR_KEY_IDX])
262 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
263
264 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
265 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
266
267 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
268 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
269
270 return 0;
271}
272
273static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
274{
275 int err;
276
277 memset(k, 0, sizeof(*k));
278 k->idx = -1;
279
280 if (info->attrs[NL80211_ATTR_KEY])
281 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
282 else
283 err = nl80211_parse_key_old(info, k);
284
285 if (err)
286 return err;
287
288 if (k->def && k->defmgmt)
289 return -EINVAL;
290
291 if (k->idx != -1) {
292 if (k->defmgmt) {
293 if (k->idx < 4 || k->idx > 5)
294 return -EINVAL;
295 } else if (k->def) {
296 if (k->idx < 0 || k->idx > 3)
297 return -EINVAL;
298 } else {
299 if (k->idx < 0 || k->idx > 5)
300 return -EINVAL;
301 }
302 }
303
304 return 0;
305}
306
Johannes Bergfffd0932009-07-08 14:22:54 +0200307static struct cfg80211_cached_keys *
308nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
309 struct nlattr *keys)
310{
311 struct key_parse parse;
312 struct nlattr *key;
313 struct cfg80211_cached_keys *result;
314 int rem, err, def = 0;
315
316 result = kzalloc(sizeof(*result), GFP_KERNEL);
317 if (!result)
318 return ERR_PTR(-ENOMEM);
319
320 result->def = -1;
321 result->defmgmt = -1;
322
323 nla_for_each_nested(key, keys, rem) {
324 memset(&parse, 0, sizeof(parse));
325 parse.idx = -1;
326
327 err = nl80211_parse_key_new(key, &parse);
328 if (err)
329 goto error;
330 err = -EINVAL;
331 if (!parse.p.key)
332 goto error;
333 if (parse.idx < 0 || parse.idx > 4)
334 goto error;
335 if (parse.def) {
336 if (def)
337 goto error;
338 def = 1;
339 result->def = parse.idx;
340 } else if (parse.defmgmt)
341 goto error;
342 err = cfg80211_validate_key_settings(rdev, &parse.p,
343 parse.idx, NULL);
344 if (err)
345 goto error;
346 result->params[parse.idx].cipher = parse.p.cipher;
347 result->params[parse.idx].key_len = parse.p.key_len;
348 result->params[parse.idx].key = result->data[parse.idx];
349 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
350 }
351
352 return result;
353 error:
354 kfree(result);
355 return ERR_PTR(err);
356}
357
358static int nl80211_key_allowed(struct wireless_dev *wdev)
359{
360 ASSERT_WDEV_LOCK(wdev);
361
362 if (!netif_running(wdev->netdev))
363 return -ENETDOWN;
364
365 switch (wdev->iftype) {
366 case NL80211_IFTYPE_AP:
367 case NL80211_IFTYPE_AP_VLAN:
368 break;
369 case NL80211_IFTYPE_ADHOC:
370 if (!wdev->current_bss)
371 return -ENOLINK;
372 break;
373 case NL80211_IFTYPE_STATION:
374 if (wdev->sme_state != CFG80211_SME_CONNECTED)
375 return -ENOLINK;
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 return 0;
382}
383
Johannes Berg55682962007-09-20 13:09:35 -0400384static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
385 struct cfg80211_registered_device *dev)
386{
387 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100388 struct nlattr *nl_bands, *nl_band;
389 struct nlattr *nl_freqs, *nl_freq;
390 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700391 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100392 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100393 enum ieee80211_band band;
394 struct ieee80211_channel *chan;
395 struct ieee80211_rate *rate;
396 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700397 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400398
399 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
400 if (!hdr)
401 return -1;
402
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500403 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400404 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200405
406 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
407 dev->wiphy.retry_short);
408 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
409 dev->wiphy.retry_long);
410 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
411 dev->wiphy.frag_threshold);
412 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
413 dev->wiphy.rts_threshold);
414
Johannes Berg2a519312009-02-10 21:25:55 +0100415 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
416 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200417 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
418 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100419
Johannes Berg25e47c182009-04-02 20:14:06 +0200420 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
421 sizeof(u32) * dev->wiphy.n_cipher_suites,
422 dev->wiphy.cipher_suites);
423
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700424 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
425 if (!nl_modes)
426 goto nla_put_failure;
427
428 i = 0;
429 while (ifmodes) {
430 if (ifmodes & 1)
431 NLA_PUT_FLAG(msg, i);
432 ifmodes >>= 1;
433 i++;
434 }
435
436 nla_nest_end(msg, nl_modes);
437
Johannes Bergee688b002008-01-24 19:38:39 +0100438 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
439 if (!nl_bands)
440 goto nla_put_failure;
441
442 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
443 if (!dev->wiphy.bands[band])
444 continue;
445
446 nl_band = nla_nest_start(msg, band);
447 if (!nl_band)
448 goto nla_put_failure;
449
Johannes Bergd51626d2008-10-09 12:20:13 +0200450 /* add HT info */
451 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
452 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
453 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
454 &dev->wiphy.bands[band]->ht_cap.mcs);
455 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
456 dev->wiphy.bands[band]->ht_cap.cap);
457 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
458 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
459 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
460 dev->wiphy.bands[band]->ht_cap.ampdu_density);
461 }
462
Johannes Bergee688b002008-01-24 19:38:39 +0100463 /* add frequencies */
464 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
465 if (!nl_freqs)
466 goto nla_put_failure;
467
468 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
469 nl_freq = nla_nest_start(msg, i);
470 if (!nl_freq)
471 goto nla_put_failure;
472
473 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100474
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400475 if (nl80211_msg_put_channel(msg, chan))
476 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200477
Johannes Bergee688b002008-01-24 19:38:39 +0100478 nla_nest_end(msg, nl_freq);
479 }
480
481 nla_nest_end(msg, nl_freqs);
482
483 /* add bitrates */
484 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
485 if (!nl_rates)
486 goto nla_put_failure;
487
488 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
489 nl_rate = nla_nest_start(msg, i);
490 if (!nl_rate)
491 goto nla_put_failure;
492
493 rate = &dev->wiphy.bands[band]->bitrates[i];
494 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
495 rate->bitrate);
496 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
497 NLA_PUT_FLAG(msg,
498 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
499
500 nla_nest_end(msg, nl_rate);
501 }
502
503 nla_nest_end(msg, nl_rates);
504
505 nla_nest_end(msg, nl_band);
506 }
507 nla_nest_end(msg, nl_bands);
508
Johannes Berg8fdc6212009-03-14 09:34:01 +0100509 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
510 if (!nl_cmds)
511 goto nla_put_failure;
512
513 i = 0;
514#define CMD(op, n) \
515 do { \
516 if (dev->ops->op) { \
517 i++; \
518 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
519 } \
520 } while (0)
521
522 CMD(add_virtual_intf, NEW_INTERFACE);
523 CMD(change_virtual_intf, SET_INTERFACE);
524 CMD(add_key, NEW_KEY);
525 CMD(add_beacon, NEW_BEACON);
526 CMD(add_station, NEW_STATION);
527 CMD(add_mpath, NEW_MPATH);
528 CMD(set_mesh_params, SET_MESH_PARAMS);
529 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200530 CMD(auth, AUTHENTICATE);
531 CMD(assoc, ASSOCIATE);
532 CMD(deauth, DEAUTHENTICATE);
533 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200534 CMD(join_ibss, JOIN_IBSS);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100535
536#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200537
Johannes Berg6829c872009-07-02 09:13:27 +0200538 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200539 i++;
540 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
541 }
542
Johannes Berg6829c872009-07-02 09:13:27 +0200543 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200544 i++;
545 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
546 }
547
Johannes Berg8fdc6212009-03-14 09:34:01 +0100548 nla_nest_end(msg, nl_cmds);
549
Johannes Berg55682962007-09-20 13:09:35 -0400550 return genlmsg_end(msg, hdr);
551
552 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700553 genlmsg_cancel(msg, hdr);
554 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400555}
556
557static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
558{
559 int idx = 0;
560 int start = cb->args[0];
561 struct cfg80211_registered_device *dev;
562
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500563 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200564 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200565 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400566 continue;
567 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
568 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200569 dev) < 0) {
570 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400571 break;
Julius Volzb4637272008-07-08 14:02:19 +0200572 }
Johannes Berg55682962007-09-20 13:09:35 -0400573 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500574 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400575
576 cb->args[0] = idx;
577
578 return skb->len;
579}
580
581static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
582{
583 struct sk_buff *msg;
584 struct cfg80211_registered_device *dev;
585
586 dev = cfg80211_get_dev_from_info(info);
587 if (IS_ERR(dev))
588 return PTR_ERR(dev);
589
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400591 if (!msg)
592 goto out_err;
593
594 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
595 goto out_free;
596
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200597 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400598
Johannes Berg134e6372009-07-10 09:51:34 +0000599 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400600
601 out_free:
602 nlmsg_free(msg);
603 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200604 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400605 return -ENOBUFS;
606}
607
Jouni Malinen31888482008-10-30 16:59:24 +0200608static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
609 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
610 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
611 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
612 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
613 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
614};
615
616static int parse_txq_params(struct nlattr *tb[],
617 struct ieee80211_txq_params *txq_params)
618{
619 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
620 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
621 !tb[NL80211_TXQ_ATTR_AIFS])
622 return -EINVAL;
623
624 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
625 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
626 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
627 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
628 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
629
630 return 0;
631}
632
Johannes Berg55682962007-09-20 13:09:35 -0400633static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
634{
635 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200636 int result = 0, rem_txq_params = 0;
637 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200638 u32 changed;
639 u8 retry_short = 0, retry_long = 0;
640 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400641
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100642 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400643
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100644 mutex_lock(&cfg80211_mutex);
645
Johannes Berg79c97e92009-07-07 03:56:12 +0200646 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100647 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200648 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100649 result = PTR_ERR(rdev);
650 goto unlock;
651 }
652
653 mutex_lock(&rdev->mtx);
654
655 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200656 result = cfg80211_dev_rename(
657 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100658
659 mutex_unlock(&cfg80211_mutex);
660
661 if (result)
662 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400663
Jouni Malinen31888482008-10-30 16:59:24 +0200664 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
665 struct ieee80211_txq_params txq_params;
666 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
667
668 if (!rdev->ops->set_txq_params) {
669 result = -EOPNOTSUPP;
670 goto bad_res;
671 }
672
673 nla_for_each_nested(nl_txq_params,
674 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
675 rem_txq_params) {
676 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
677 nla_data(nl_txq_params),
678 nla_len(nl_txq_params),
679 txq_params_policy);
680 result = parse_txq_params(tb, &txq_params);
681 if (result)
682 goto bad_res;
683
684 result = rdev->ops->set_txq_params(&rdev->wiphy,
685 &txq_params);
686 if (result)
687 goto bad_res;
688 }
689 }
690
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200691 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530692 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200693 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100694 struct ieee80211_sta_ht_cap *ht_cap;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400695 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200696
697 if (!rdev->ops->set_channel) {
698 result = -EOPNOTSUPP;
699 goto bad_res;
700 }
701
Johannes Berg306d6112008-12-08 12:39:04 +0100702 result = -EINVAL;
703
Sujith094d05d2008-12-12 11:57:43 +0530704 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
705 channel_type = nla_get_u32(info->attrs[
706 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
707 if (channel_type != NL80211_CHAN_NO_HT &&
708 channel_type != NL80211_CHAN_HT20 &&
709 channel_type != NL80211_CHAN_HT40PLUS &&
710 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200711 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200712 }
713
714 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
715 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100716
717 /* Primary channel not allowed */
718 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200719 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100720
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400721 if (channel_type == NL80211_CHAN_HT40MINUS &&
722 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
723 goto bad_res;
724 else if (channel_type == NL80211_CHAN_HT40PLUS &&
725 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
726 goto bad_res;
727
728 /*
729 * At this point we know if that if HT40 was requested
730 * we are allowed to use it and the extension channel
731 * exists.
732 */
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200733
Johannes Berg306d6112008-12-08 12:39:04 +0100734 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
735
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400736 /* no HT capabilities or intolerant */
737 if (channel_type != NL80211_CHAN_NO_HT) {
738 if (!ht_cap->ht_supported)
739 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100740 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
741 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200742 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200743 }
744
745 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530746 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200747 if (result)
748 goto bad_res;
749 }
750
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200751 changed = 0;
752
753 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
754 retry_short = nla_get_u8(
755 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
756 if (retry_short == 0) {
757 result = -EINVAL;
758 goto bad_res;
759 }
760 changed |= WIPHY_PARAM_RETRY_SHORT;
761 }
762
763 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
764 retry_long = nla_get_u8(
765 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
766 if (retry_long == 0) {
767 result = -EINVAL;
768 goto bad_res;
769 }
770 changed |= WIPHY_PARAM_RETRY_LONG;
771 }
772
773 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
774 frag_threshold = nla_get_u32(
775 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
776 if (frag_threshold < 256) {
777 result = -EINVAL;
778 goto bad_res;
779 }
780 if (frag_threshold != (u32) -1) {
781 /*
782 * Fragments (apart from the last one) are required to
783 * have even length. Make the fragmentation code
784 * simpler by stripping LSB should someone try to use
785 * odd threshold value.
786 */
787 frag_threshold &= ~0x1;
788 }
789 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
790 }
791
792 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
793 rts_threshold = nla_get_u32(
794 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
795 changed |= WIPHY_PARAM_RTS_THRESHOLD;
796 }
797
798 if (changed) {
799 u8 old_retry_short, old_retry_long;
800 u32 old_frag_threshold, old_rts_threshold;
801
802 if (!rdev->ops->set_wiphy_params) {
803 result = -EOPNOTSUPP;
804 goto bad_res;
805 }
806
807 old_retry_short = rdev->wiphy.retry_short;
808 old_retry_long = rdev->wiphy.retry_long;
809 old_frag_threshold = rdev->wiphy.frag_threshold;
810 old_rts_threshold = rdev->wiphy.rts_threshold;
811
812 if (changed & WIPHY_PARAM_RETRY_SHORT)
813 rdev->wiphy.retry_short = retry_short;
814 if (changed & WIPHY_PARAM_RETRY_LONG)
815 rdev->wiphy.retry_long = retry_long;
816 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
817 rdev->wiphy.frag_threshold = frag_threshold;
818 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
819 rdev->wiphy.rts_threshold = rts_threshold;
820
821 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
822 if (result) {
823 rdev->wiphy.retry_short = old_retry_short;
824 rdev->wiphy.retry_long = old_retry_long;
825 rdev->wiphy.frag_threshold = old_frag_threshold;
826 rdev->wiphy.rts_threshold = old_rts_threshold;
827 }
828 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200829
Johannes Berg306d6112008-12-08 12:39:04 +0100830 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100831 mutex_unlock(&rdev->mtx);
832 unlock:
833 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400834 return result;
835}
836
837
838static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200839 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400840 struct net_device *dev)
841{
842 void *hdr;
843
844 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
845 if (!hdr)
846 return -1;
847
848 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200849 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400850 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200851 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400852 return genlmsg_end(msg, hdr);
853
854 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700855 genlmsg_cancel(msg, hdr);
856 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400857}
858
859static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
860{
861 int wp_idx = 0;
862 int if_idx = 0;
863 int wp_start = cb->args[0];
864 int if_start = cb->args[1];
865 struct cfg80211_registered_device *dev;
866 struct wireless_dev *wdev;
867
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500868 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200869 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200870 if (wp_idx < wp_start) {
871 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400872 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200873 }
Johannes Berg55682962007-09-20 13:09:35 -0400874 if_idx = 0;
875
876 mutex_lock(&dev->devlist_mtx);
877 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200878 if (if_idx < if_start) {
879 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400880 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200881 }
Johannes Berg55682962007-09-20 13:09:35 -0400882 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergd7264052009-04-19 16:23:20 +0200884 dev, wdev->netdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200885 mutex_unlock(&dev->devlist_mtx);
886 goto out;
887 }
888 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400889 }
890 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200891
892 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400893 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200894 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500895 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400896
897 cb->args[0] = wp_idx;
898 cb->args[1] = if_idx;
899
900 return skb->len;
901}
902
903static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
904{
905 struct sk_buff *msg;
906 struct cfg80211_registered_device *dev;
907 struct net_device *netdev;
908 int err;
909
Johannes Berg79c97e92009-07-07 03:56:12 +0200910 err = get_rdev_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400911 if (err)
912 return err;
913
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400915 if (!msg)
916 goto out_err;
917
Johannes Bergd7264052009-04-19 16:23:20 +0200918 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
919 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400920 goto out_free;
921
922 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200923 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400924
Johannes Berg134e6372009-07-10 09:51:34 +0000925 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400926
927 out_free:
928 nlmsg_free(msg);
929 out_err:
930 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200931 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400932 return -ENOBUFS;
933}
934
Michael Wu66f7ac52008-01-31 19:48:22 +0100935static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
936 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
937 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
938 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
939 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
940 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
941};
942
943static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
944{
945 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
946 int flag;
947
948 *mntrflags = 0;
949
950 if (!nla)
951 return -EINVAL;
952
953 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
954 nla, mntr_flags_policy))
955 return -EINVAL;
956
957 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
958 if (flags[flag])
959 *mntrflags |= (1<<flag);
960
961 return 0;
962}
963
Johannes Berg55682962007-09-20 13:09:35 -0400964static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
965{
Johannes Berg79c97e92009-07-07 03:56:12 +0200966 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100967 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +0200968 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200969 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -0400970 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200971 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100972 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400973
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100974 memset(&params, 0, sizeof(params));
975
Johannes Berg3b858752009-03-12 09:55:09 +0100976 rtnl_lock();
977
Johannes Berg79c97e92009-07-07 03:56:12 +0200978 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400979 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100980 goto unlock_rtnl;
981
Johannes Berg04a773a2009-04-19 21:24:32 +0200982 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400983
Johannes Berg723b0382008-09-16 20:22:09 +0200984 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100985 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +0200986 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100987 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200988 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100989 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200990 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100991 }
Johannes Berg723b0382008-09-16 20:22:09 +0200992 }
993
Johannes Berg79c97e92009-07-07 03:56:12 +0200994 if (!rdev->ops->change_virtual_intf ||
995 !(rdev->wiphy.interface_modes & (1 << ntype))) {
Johannes Berg55682962007-09-20 13:09:35 -0400996 err = -EOPNOTSUPP;
997 goto unlock;
998 }
999
Johannes Berg92ffe052008-09-16 20:39:36 +02001000 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001001 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001002 err = -EINVAL;
1003 goto unlock;
1004 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001005 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1006 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001007 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001008 }
1009
Johannes Berg92ffe052008-09-16 20:39:36 +02001010 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001011 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001012 err = -EINVAL;
1013 goto unlock;
1014 }
1015 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1016 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001017 if (err)
1018 goto unlock;
1019
1020 flags = &_flags;
1021 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001022 }
Johannes Berg3b858752009-03-12 09:55:09 +01001023
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001024 if (change)
Johannes Berg79c97e92009-07-07 03:56:12 +02001025 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
Johannes Berg04a773a2009-04-19 21:24:32 +02001026 ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001027 else
1028 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001029
Johannes Berge36d56b2009-06-09 21:04:43 +02001030 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
Johannes Berg04a773a2009-04-19 21:24:32 +02001031
Johannes Berge36d56b2009-06-09 21:04:43 +02001032 if (!err && (ntype != otype)) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001033 if (otype == NL80211_IFTYPE_ADHOC)
Johannes Berg9d308422009-04-20 18:43:46 +02001034 cfg80211_clear_ibss(dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02001035 }
Johannes Berg60719ff2008-09-16 14:55:09 +02001036
Johannes Berg55682962007-09-20 13:09:35 -04001037 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001038 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001039 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001040 unlock_rtnl:
1041 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001042 return err;
1043}
1044
1045static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1046{
Johannes Berg79c97e92009-07-07 03:56:12 +02001047 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001048 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001049 int err;
1050 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001051 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001052
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001053 memset(&params, 0, sizeof(params));
1054
Johannes Berg55682962007-09-20 13:09:35 -04001055 if (!info->attrs[NL80211_ATTR_IFNAME])
1056 return -EINVAL;
1057
1058 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1059 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1060 if (type > NL80211_IFTYPE_MAX)
1061 return -EINVAL;
1062 }
1063
Johannes Berg3b858752009-03-12 09:55:09 +01001064 rtnl_lock();
1065
Johannes Berg79c97e92009-07-07 03:56:12 +02001066 rdev = cfg80211_get_dev_from_info(info);
1067 if (IS_ERR(rdev)) {
1068 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001069 goto unlock_rtnl;
1070 }
Johannes Berg55682962007-09-20 13:09:35 -04001071
Johannes Berg79c97e92009-07-07 03:56:12 +02001072 if (!rdev->ops->add_virtual_intf ||
1073 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001074 err = -EOPNOTSUPP;
1075 goto unlock;
1076 }
1077
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001078 if (type == NL80211_IFTYPE_MESH_POINT &&
1079 info->attrs[NL80211_ATTR_MESH_ID]) {
1080 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1081 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1082 }
1083
Michael Wu66f7ac52008-01-31 19:48:22 +01001084 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1085 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1086 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001087 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001088 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001089 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001090
Johannes Berg55682962007-09-20 13:09:35 -04001091 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001092 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001093 unlock_rtnl:
1094 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001095 return err;
1096}
1097
1098static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1099{
Johannes Berg79c97e92009-07-07 03:56:12 +02001100 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001101 int ifindex, err;
1102 struct net_device *dev;
1103
Johannes Berg3b858752009-03-12 09:55:09 +01001104 rtnl_lock();
1105
Johannes Berg79c97e92009-07-07 03:56:12 +02001106 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001107 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001108 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001109 ifindex = dev->ifindex;
1110 dev_put(dev);
1111
Johannes Berg79c97e92009-07-07 03:56:12 +02001112 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001113 err = -EOPNOTSUPP;
1114 goto out;
1115 }
1116
Johannes Berg79c97e92009-07-07 03:56:12 +02001117 err = rdev->ops->del_virtual_intf(&rdev->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -04001118
1119 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001120 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001121 unlock_rtnl:
1122 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001123 return err;
1124}
1125
Johannes Berg41ade002007-12-19 02:03:29 +01001126struct get_key_cookie {
1127 struct sk_buff *msg;
1128 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001129 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001130};
1131
1132static void get_key_callback(void *c, struct key_params *params)
1133{
Johannes Bergb9454e82009-07-08 13:29:08 +02001134 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001135 struct get_key_cookie *cookie = c;
1136
1137 if (params->key)
1138 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1139 params->key_len, params->key);
1140
1141 if (params->seq)
1142 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1143 params->seq_len, params->seq);
1144
1145 if (params->cipher)
1146 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1147 params->cipher);
1148
Johannes Bergb9454e82009-07-08 13:29:08 +02001149 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1150 if (!key)
1151 goto nla_put_failure;
1152
1153 if (params->key)
1154 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1155 params->key_len, params->key);
1156
1157 if (params->seq)
1158 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1159 params->seq_len, params->seq);
1160
1161 if (params->cipher)
1162 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1163 params->cipher);
1164
1165 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1166
1167 nla_nest_end(cookie->msg, key);
1168
Johannes Berg41ade002007-12-19 02:03:29 +01001169 return;
1170 nla_put_failure:
1171 cookie->error = 1;
1172}
1173
1174static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1175{
Johannes Berg79c97e92009-07-07 03:56:12 +02001176 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001177 int err;
1178 struct net_device *dev;
1179 u8 key_idx = 0;
1180 u8 *mac_addr = NULL;
1181 struct get_key_cookie cookie = {
1182 .error = 0,
1183 };
1184 void *hdr;
1185 struct sk_buff *msg;
1186
1187 if (info->attrs[NL80211_ATTR_KEY_IDX])
1188 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1189
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001190 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001191 return -EINVAL;
1192
1193 if (info->attrs[NL80211_ATTR_MAC])
1194 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1195
Johannes Berg3b858752009-03-12 09:55:09 +01001196 rtnl_lock();
1197
Johannes Berg79c97e92009-07-07 03:56:12 +02001198 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001199 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001200 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001201
Johannes Berg79c97e92009-07-07 03:56:12 +02001202 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001203 err = -EOPNOTSUPP;
1204 goto out;
1205 }
1206
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001207 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001208 if (!msg) {
1209 err = -ENOMEM;
1210 goto out;
1211 }
1212
1213 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1214 NL80211_CMD_NEW_KEY);
1215
1216 if (IS_ERR(hdr)) {
1217 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001218 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001219 }
1220
1221 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001222 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001223
1224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1225 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1226 if (mac_addr)
1227 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1228
Johannes Berg79c97e92009-07-07 03:56:12 +02001229 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001230 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001231
1232 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001233 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001234
1235 if (cookie.error)
1236 goto nla_put_failure;
1237
1238 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001239 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001240 goto out;
1241
1242 nla_put_failure:
1243 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001244 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001245 nlmsg_free(msg);
1246 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001247 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001248 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001249 unlock_rtnl:
1250 rtnl_unlock();
1251
Johannes Berg41ade002007-12-19 02:03:29 +01001252 return err;
1253}
1254
1255static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1256{
Johannes Berg79c97e92009-07-07 03:56:12 +02001257 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001258 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001259 int err;
1260 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001261 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1262 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001263
Johannes Bergb9454e82009-07-08 13:29:08 +02001264 err = nl80211_parse_key(info, &key);
1265 if (err)
1266 return err;
1267
1268 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001269 return -EINVAL;
1270
Johannes Bergb9454e82009-07-08 13:29:08 +02001271 /* only support setting default key */
1272 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001273 return -EINVAL;
1274
Johannes Berg3b858752009-03-12 09:55:09 +01001275 rtnl_lock();
1276
Johannes Berg79c97e92009-07-07 03:56:12 +02001277 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001278 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001279 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001280
Johannes Bergb9454e82009-07-08 13:29:08 +02001281 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001282 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001283 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001284 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001285
1286 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001287 err = -EOPNOTSUPP;
1288 goto out;
1289 }
1290
Johannes Bergfffd0932009-07-08 14:22:54 +02001291 wdev_lock(dev->ieee80211_ptr);
1292 err = nl80211_key_allowed(dev->ieee80211_ptr);
1293 if (!err)
1294 err = func(&rdev->wiphy, dev, key.idx);
1295
Johannes Berg08645122009-05-11 13:54:58 +02001296#ifdef CONFIG_WIRELESS_EXT
1297 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001298 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001299 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001300 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001301 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001302 }
1303#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001304 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001305
1306 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001307 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001308 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001309
1310 unlock_rtnl:
1311 rtnl_unlock();
1312
Johannes Berg41ade002007-12-19 02:03:29 +01001313 return err;
1314}
1315
1316static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1317{
Johannes Berg79c97e92009-07-07 03:56:12 +02001318 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001319 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001320 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001321 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001322 u8 *mac_addr = NULL;
1323
Johannes Bergb9454e82009-07-08 13:29:08 +02001324 err = nl80211_parse_key(info, &key);
1325 if (err)
1326 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001327
Johannes Bergb9454e82009-07-08 13:29:08 +02001328 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001329 return -EINVAL;
1330
Johannes Berg41ade002007-12-19 02:03:29 +01001331 if (info->attrs[NL80211_ATTR_MAC])
1332 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1333
Johannes Berg3b858752009-03-12 09:55:09 +01001334 rtnl_lock();
1335
Johannes Berg79c97e92009-07-07 03:56:12 +02001336 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001337 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001338 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001339
Johannes Berg79c97e92009-07-07 03:56:12 +02001340 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001341 err = -EOPNOTSUPP;
1342 goto out;
1343 }
1344
Johannes Bergfffd0932009-07-08 14:22:54 +02001345 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1346 err = -EINVAL;
1347 goto out;
1348 }
1349
1350 wdev_lock(dev->ieee80211_ptr);
1351 err = nl80211_key_allowed(dev->ieee80211_ptr);
1352 if (!err)
1353 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1354 mac_addr, &key.p);
1355 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001356
1357 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001358 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001359 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001360 unlock_rtnl:
1361 rtnl_unlock();
1362
Johannes Berg41ade002007-12-19 02:03:29 +01001363 return err;
1364}
1365
1366static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1367{
Johannes Berg79c97e92009-07-07 03:56:12 +02001368 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001369 int err;
1370 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001371 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001372 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001373
Johannes Bergb9454e82009-07-08 13:29:08 +02001374 err = nl80211_parse_key(info, &key);
1375 if (err)
1376 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001377
1378 if (info->attrs[NL80211_ATTR_MAC])
1379 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1380
Johannes Berg3b858752009-03-12 09:55:09 +01001381 rtnl_lock();
1382
Johannes Berg79c97e92009-07-07 03:56:12 +02001383 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001384 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001385 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001386
Johannes Berg79c97e92009-07-07 03:56:12 +02001387 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001388 err = -EOPNOTSUPP;
1389 goto out;
1390 }
1391
Johannes Bergfffd0932009-07-08 14:22:54 +02001392 wdev_lock(dev->ieee80211_ptr);
1393 err = nl80211_key_allowed(dev->ieee80211_ptr);
1394 if (!err)
1395 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001396
Johannes Berg08645122009-05-11 13:54:58 +02001397#ifdef CONFIG_WIRELESS_EXT
1398 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001399 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001400 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001401 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001402 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1403 }
1404#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001405 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001406
Johannes Berg41ade002007-12-19 02:03:29 +01001407 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001408 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001409 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001410
1411 unlock_rtnl:
1412 rtnl_unlock();
1413
Johannes Berg41ade002007-12-19 02:03:29 +01001414 return err;
1415}
1416
Johannes Berged1b6cc2007-12-19 02:03:32 +01001417static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1418{
1419 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1420 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001421 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001422 int err;
1423 struct net_device *dev;
1424 struct beacon_parameters params;
1425 int haveinfo = 0;
1426
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001427 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1428 return -EINVAL;
1429
Johannes Berg3b858752009-03-12 09:55:09 +01001430 rtnl_lock();
1431
Johannes Berg79c97e92009-07-07 03:56:12 +02001432 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001433 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001434 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001435
Jouni Malineneec60b02009-03-20 21:21:19 +02001436 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1437 err = -EOPNOTSUPP;
1438 goto out;
1439 }
1440
Johannes Berged1b6cc2007-12-19 02:03:32 +01001441 switch (info->genlhdr->cmd) {
1442 case NL80211_CMD_NEW_BEACON:
1443 /* these are required for NEW_BEACON */
1444 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1445 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1446 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1447 err = -EINVAL;
1448 goto out;
1449 }
1450
Johannes Berg79c97e92009-07-07 03:56:12 +02001451 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001452 break;
1453 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001454 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001455 break;
1456 default:
1457 WARN_ON(1);
1458 err = -EOPNOTSUPP;
1459 goto out;
1460 }
1461
1462 if (!call) {
1463 err = -EOPNOTSUPP;
1464 goto out;
1465 }
1466
1467 memset(&params, 0, sizeof(params));
1468
1469 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1470 params.interval =
1471 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1472 haveinfo = 1;
1473 }
1474
1475 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1476 params.dtim_period =
1477 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1478 haveinfo = 1;
1479 }
1480
1481 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1482 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1483 params.head_len =
1484 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1485 haveinfo = 1;
1486 }
1487
1488 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1489 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1490 params.tail_len =
1491 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1492 haveinfo = 1;
1493 }
1494
1495 if (!haveinfo) {
1496 err = -EINVAL;
1497 goto out;
1498 }
1499
Johannes Berg79c97e92009-07-07 03:56:12 +02001500 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001501
1502 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001503 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001504 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001505 unlock_rtnl:
1506 rtnl_unlock();
1507
Johannes Berged1b6cc2007-12-19 02:03:32 +01001508 return err;
1509}
1510
1511static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1512{
Johannes Berg79c97e92009-07-07 03:56:12 +02001513 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001514 int err;
1515 struct net_device *dev;
1516
Johannes Berg3b858752009-03-12 09:55:09 +01001517 rtnl_lock();
1518
Johannes Berg79c97e92009-07-07 03:56:12 +02001519 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001520 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001521 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001522
Johannes Berg79c97e92009-07-07 03:56:12 +02001523 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001524 err = -EOPNOTSUPP;
1525 goto out;
1526 }
1527
Jouni Malineneec60b02009-03-20 21:21:19 +02001528 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1529 err = -EOPNOTSUPP;
1530 goto out;
1531 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001532 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001533
1534 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001535 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001536 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001537 unlock_rtnl:
1538 rtnl_unlock();
1539
Johannes Berged1b6cc2007-12-19 02:03:32 +01001540 return err;
1541}
1542
Johannes Berg5727ef12007-12-19 02:03:34 +01001543static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1544 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1545 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1546 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001547 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001548};
1549
Johannes Bergeccb8e82009-05-11 21:57:56 +03001550static int parse_station_flags(struct genl_info *info,
1551 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001552{
1553 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001554 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001555 int flag;
1556
Johannes Bergeccb8e82009-05-11 21:57:56 +03001557 /*
1558 * Try parsing the new attribute first so userspace
1559 * can specify both for older kernels.
1560 */
1561 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1562 if (nla) {
1563 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001564
Johannes Bergeccb8e82009-05-11 21:57:56 +03001565 sta_flags = nla_data(nla);
1566 params->sta_flags_mask = sta_flags->mask;
1567 params->sta_flags_set = sta_flags->set;
1568 if ((params->sta_flags_mask |
1569 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1570 return -EINVAL;
1571 return 0;
1572 }
1573
1574 /* if present, parse the old attribute */
1575
1576 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001577 if (!nla)
1578 return 0;
1579
1580 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1581 nla, sta_flags_policy))
1582 return -EINVAL;
1583
Johannes Bergeccb8e82009-05-11 21:57:56 +03001584 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1585 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001586
1587 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1588 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001589 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001590
1591 return 0;
1592}
1593
Henning Rogge420e7fa2008-12-11 22:04:19 +01001594static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1595{
1596 int modulation, streams, bitrate;
1597
1598 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1599 return rate->legacy;
1600
1601 /* the formula below does only work for MCS values smaller than 32 */
1602 if (rate->mcs >= 32)
1603 return 0;
1604
1605 modulation = rate->mcs & 7;
1606 streams = (rate->mcs >> 3) + 1;
1607
1608 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1609 13500000 : 6500000;
1610
1611 if (modulation < 4)
1612 bitrate *= (modulation + 1);
1613 else if (modulation == 4)
1614 bitrate *= (modulation + 2);
1615 else
1616 bitrate *= (modulation + 3);
1617
1618 bitrate *= streams;
1619
1620 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1621 bitrate = (bitrate / 9) * 10;
1622
1623 /* do NOT round down here */
1624 return (bitrate + 50000) / 100000;
1625}
1626
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001627static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1628 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001629 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001630{
1631 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001632 struct nlattr *sinfoattr, *txrate;
1633 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001634
1635 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1636 if (!hdr)
1637 return -1;
1638
1639 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1640 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1641
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001642 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1643 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001644 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001645 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1646 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1647 sinfo->inactive_time);
1648 if (sinfo->filled & STATION_INFO_RX_BYTES)
1649 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1650 sinfo->rx_bytes);
1651 if (sinfo->filled & STATION_INFO_TX_BYTES)
1652 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1653 sinfo->tx_bytes);
1654 if (sinfo->filled & STATION_INFO_LLID)
1655 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1656 sinfo->llid);
1657 if (sinfo->filled & STATION_INFO_PLID)
1658 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1659 sinfo->plid);
1660 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1661 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1662 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001663 if (sinfo->filled & STATION_INFO_SIGNAL)
1664 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1665 sinfo->signal);
1666 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1667 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1668 if (!txrate)
1669 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001670
Henning Rogge420e7fa2008-12-11 22:04:19 +01001671 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1672 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1673 if (bitrate > 0)
1674 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1675
1676 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1677 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1678 sinfo->txrate.mcs);
1679 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1680 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1681 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1682 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1683
1684 nla_nest_end(msg, txrate);
1685 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001686 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1687 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1688 sinfo->rx_packets);
1689 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1690 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1691 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001692 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001693
1694 return genlmsg_end(msg, hdr);
1695
1696 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001697 genlmsg_cancel(msg, hdr);
1698 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001699}
1700
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001701static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001702 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001703{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001704 struct station_info sinfo;
1705 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001706 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001707 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001708 int ifidx = cb->args[0];
1709 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001710 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001711
Johannes Bergbba95fe2008-07-29 13:22:51 +02001712 if (!ifidx) {
1713 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1714 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1715 nl80211_policy);
1716 if (err)
1717 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001718
Johannes Bergbba95fe2008-07-29 13:22:51 +02001719 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1720 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001721
Johannes Bergbba95fe2008-07-29 13:22:51 +02001722 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1723 if (!ifidx)
1724 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001725 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001726
Johannes Berg3b858752009-03-12 09:55:09 +01001727 rtnl_lock();
1728
1729 netdev = __dev_get_by_index(&init_net, ifidx);
1730 if (!netdev) {
1731 err = -ENODEV;
1732 goto out_rtnl;
1733 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001734
Johannes Bergbba95fe2008-07-29 13:22:51 +02001735 dev = cfg80211_get_dev_from_ifindex(ifidx);
1736 if (IS_ERR(dev)) {
1737 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001738 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001739 }
1740
1741 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001742 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001743 goto out_err;
1744 }
1745
Johannes Bergbba95fe2008-07-29 13:22:51 +02001746 while (1) {
1747 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1748 mac_addr, &sinfo);
1749 if (err == -ENOENT)
1750 break;
1751 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001752 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001753
1754 if (nl80211_send_station(skb,
1755 NETLINK_CB(cb->skb).pid,
1756 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1757 netdev, mac_addr,
1758 &sinfo) < 0)
1759 goto out;
1760
1761 sta_idx++;
1762 }
1763
1764
1765 out:
1766 cb->args[1] = sta_idx;
1767 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001768 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001769 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001770 out_rtnl:
1771 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001772
1773 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001774}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001775
Johannes Berg5727ef12007-12-19 02:03:34 +01001776static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1777{
Johannes Berg79c97e92009-07-07 03:56:12 +02001778 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001779 int err;
1780 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001781 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001782 struct sk_buff *msg;
1783 u8 *mac_addr = NULL;
1784
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001785 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001786
1787 if (!info->attrs[NL80211_ATTR_MAC])
1788 return -EINVAL;
1789
1790 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1791
Johannes Berg3b858752009-03-12 09:55:09 +01001792 rtnl_lock();
1793
Johannes Berg79c97e92009-07-07 03:56:12 +02001794 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001795 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001796 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001797
Johannes Berg79c97e92009-07-07 03:56:12 +02001798 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001799 err = -EOPNOTSUPP;
1800 goto out;
1801 }
1802
Johannes Berg79c97e92009-07-07 03:56:12 +02001803 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001804 if (err)
1805 goto out;
1806
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001807 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001808 if (!msg)
1809 goto out;
1810
1811 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001812 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001813 goto out_free;
1814
Johannes Berg134e6372009-07-10 09:51:34 +00001815 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001816 goto out;
1817
1818 out_free:
1819 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001820 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001821 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001822 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001823 out_rtnl:
1824 rtnl_unlock();
1825
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001826 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001827}
1828
1829/*
1830 * Get vlan interface making sure it is on the right wiphy.
1831 */
1832static int get_vlan(struct nlattr *vlanattr,
1833 struct cfg80211_registered_device *rdev,
1834 struct net_device **vlan)
1835{
1836 *vlan = NULL;
1837
1838 if (vlanattr) {
1839 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1840 if (!*vlan)
1841 return -ENODEV;
1842 if (!(*vlan)->ieee80211_ptr)
1843 return -EINVAL;
1844 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1845 return -EINVAL;
1846 }
1847 return 0;
1848}
1849
1850static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1851{
Johannes Berg79c97e92009-07-07 03:56:12 +02001852 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001853 int err;
1854 struct net_device *dev;
1855 struct station_parameters params;
1856 u8 *mac_addr = NULL;
1857
1858 memset(&params, 0, sizeof(params));
1859
1860 params.listen_interval = -1;
1861
1862 if (info->attrs[NL80211_ATTR_STA_AID])
1863 return -EINVAL;
1864
1865 if (!info->attrs[NL80211_ATTR_MAC])
1866 return -EINVAL;
1867
1868 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1869
1870 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1871 params.supported_rates =
1872 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1873 params.supported_rates_len =
1874 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1875 }
1876
1877 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1878 params.listen_interval =
1879 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1880
Jouni Malinen36aedc92008-08-25 11:58:58 +03001881 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1882 params.ht_capa =
1883 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1884
Johannes Bergeccb8e82009-05-11 21:57:56 +03001885 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001886 return -EINVAL;
1887
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001888 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1889 params.plink_action =
1890 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1891
Johannes Berg3b858752009-03-12 09:55:09 +01001892 rtnl_lock();
1893
Johannes Berg79c97e92009-07-07 03:56:12 +02001894 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001895 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001896 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001897
Johannes Berg79c97e92009-07-07 03:56:12 +02001898 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001899 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001900 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001901
1902 /* validate settings */
1903 err = 0;
1904
1905 switch (dev->ieee80211_ptr->iftype) {
1906 case NL80211_IFTYPE_AP:
1907 case NL80211_IFTYPE_AP_VLAN:
1908 /* disallow mesh-specific things */
1909 if (params.plink_action)
1910 err = -EINVAL;
1911 break;
1912 case NL80211_IFTYPE_STATION:
1913 /* disallow everything but AUTHORIZED flag */
1914 if (params.plink_action)
1915 err = -EINVAL;
1916 if (params.vlan)
1917 err = -EINVAL;
1918 if (params.supported_rates)
1919 err = -EINVAL;
1920 if (params.ht_capa)
1921 err = -EINVAL;
1922 if (params.listen_interval >= 0)
1923 err = -EINVAL;
1924 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1925 err = -EINVAL;
1926 break;
1927 case NL80211_IFTYPE_MESH_POINT:
1928 /* disallow things mesh doesn't support */
1929 if (params.vlan)
1930 err = -EINVAL;
1931 if (params.ht_capa)
1932 err = -EINVAL;
1933 if (params.listen_interval >= 0)
1934 err = -EINVAL;
1935 if (params.supported_rates)
1936 err = -EINVAL;
1937 if (params.sta_flags_mask)
1938 err = -EINVAL;
1939 break;
1940 default:
1941 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001942 }
1943
Johannes Berg5727ef12007-12-19 02:03:34 +01001944 if (err)
1945 goto out;
1946
Johannes Berg79c97e92009-07-07 03:56:12 +02001947 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001948 err = -EOPNOTSUPP;
1949 goto out;
1950 }
1951
Johannes Berg79c97e92009-07-07 03:56:12 +02001952 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001953
1954 out:
1955 if (params.vlan)
1956 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001957 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001958 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001959 out_rtnl:
1960 rtnl_unlock();
1961
Johannes Berg5727ef12007-12-19 02:03:34 +01001962 return err;
1963}
1964
1965static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1966{
Johannes Berg79c97e92009-07-07 03:56:12 +02001967 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001968 int err;
1969 struct net_device *dev;
1970 struct station_parameters params;
1971 u8 *mac_addr = NULL;
1972
1973 memset(&params, 0, sizeof(params));
1974
1975 if (!info->attrs[NL80211_ATTR_MAC])
1976 return -EINVAL;
1977
Johannes Berg5727ef12007-12-19 02:03:34 +01001978 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1979 return -EINVAL;
1980
1981 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1982 return -EINVAL;
1983
1984 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1985 params.supported_rates =
1986 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1987 params.supported_rates_len =
1988 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1989 params.listen_interval =
1990 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02001991
Johannes Berga97f4422009-06-18 17:23:43 +02001992 if (info->attrs[NL80211_ATTR_STA_AID]) {
1993 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1994 if (!params.aid || params.aid > IEEE80211_MAX_AID)
1995 return -EINVAL;
1996 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02001997
Jouni Malinen36aedc92008-08-25 11:58:58 +03001998 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1999 params.ht_capa =
2000 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002001
Johannes Bergeccb8e82009-05-11 21:57:56 +03002002 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002003 return -EINVAL;
2004
Johannes Berg3b858752009-03-12 09:55:09 +01002005 rtnl_lock();
2006
Johannes Berg79c97e92009-07-07 03:56:12 +02002007 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002008 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002009 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002010
Johannes Berg79c97e92009-07-07 03:56:12 +02002011 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002012 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002013 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002014
2015 /* validate settings */
2016 err = 0;
2017
2018 switch (dev->ieee80211_ptr->iftype) {
2019 case NL80211_IFTYPE_AP:
2020 case NL80211_IFTYPE_AP_VLAN:
2021 /* all ok but must have AID */
2022 if (!params.aid)
2023 err = -EINVAL;
2024 break;
2025 case NL80211_IFTYPE_MESH_POINT:
2026 /* disallow things mesh doesn't support */
2027 if (params.vlan)
2028 err = -EINVAL;
2029 if (params.aid)
2030 err = -EINVAL;
2031 if (params.ht_capa)
2032 err = -EINVAL;
2033 if (params.listen_interval >= 0)
2034 err = -EINVAL;
2035 if (params.supported_rates)
2036 err = -EINVAL;
2037 if (params.sta_flags_mask)
2038 err = -EINVAL;
2039 break;
2040 default:
2041 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002042 }
2043
Johannes Berg5727ef12007-12-19 02:03:34 +01002044 if (err)
2045 goto out;
2046
Johannes Berg79c97e92009-07-07 03:56:12 +02002047 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002048 err = -EOPNOTSUPP;
2049 goto out;
2050 }
2051
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002052 if (!netif_running(dev)) {
2053 err = -ENETDOWN;
2054 goto out;
2055 }
2056
Johannes Berg79c97e92009-07-07 03:56:12 +02002057 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002058
2059 out:
2060 if (params.vlan)
2061 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002062 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002063 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002064 out_rtnl:
2065 rtnl_unlock();
2066
Johannes Berg5727ef12007-12-19 02:03:34 +01002067 return err;
2068}
2069
2070static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2071{
Johannes Berg79c97e92009-07-07 03:56:12 +02002072 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002073 int err;
2074 struct net_device *dev;
2075 u8 *mac_addr = NULL;
2076
2077 if (info->attrs[NL80211_ATTR_MAC])
2078 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2079
Johannes Berg3b858752009-03-12 09:55:09 +01002080 rtnl_lock();
2081
Johannes Berg79c97e92009-07-07 03:56:12 +02002082 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002083 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002084 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002085
Johannes Berge80cf852009-05-11 14:43:13 +02002086 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07002087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2088 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002089 err = -EINVAL;
2090 goto out;
2091 }
2092
Johannes Berg79c97e92009-07-07 03:56:12 +02002093 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002094 err = -EOPNOTSUPP;
2095 goto out;
2096 }
2097
Johannes Berg79c97e92009-07-07 03:56:12 +02002098 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002099
2100 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002101 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002102 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002103 out_rtnl:
2104 rtnl_unlock();
2105
Johannes Berg5727ef12007-12-19 02:03:34 +01002106 return err;
2107}
2108
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002109static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2110 int flags, struct net_device *dev,
2111 u8 *dst, u8 *next_hop,
2112 struct mpath_info *pinfo)
2113{
2114 void *hdr;
2115 struct nlattr *pinfoattr;
2116
2117 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2118 if (!hdr)
2119 return -1;
2120
2121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2122 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2123 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2124
2125 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2126 if (!pinfoattr)
2127 goto nla_put_failure;
2128 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2129 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2130 pinfo->frame_qlen);
2131 if (pinfo->filled & MPATH_INFO_DSN)
2132 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
2133 pinfo->dsn);
2134 if (pinfo->filled & MPATH_INFO_METRIC)
2135 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2136 pinfo->metric);
2137 if (pinfo->filled & MPATH_INFO_EXPTIME)
2138 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2139 pinfo->exptime);
2140 if (pinfo->filled & MPATH_INFO_FLAGS)
2141 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2142 pinfo->flags);
2143 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2144 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2145 pinfo->discovery_timeout);
2146 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2147 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2148 pinfo->discovery_retries);
2149
2150 nla_nest_end(msg, pinfoattr);
2151
2152 return genlmsg_end(msg, hdr);
2153
2154 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002155 genlmsg_cancel(msg, hdr);
2156 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002157}
2158
2159static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002160 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002161{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002162 struct mpath_info pinfo;
2163 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002164 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002165 u8 dst[ETH_ALEN];
2166 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002167 int ifidx = cb->args[0];
2168 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002170
Johannes Bergbba95fe2008-07-29 13:22:51 +02002171 if (!ifidx) {
2172 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2173 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2174 nl80211_policy);
2175 if (err)
2176 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002177
Johannes Bergbba95fe2008-07-29 13:22:51 +02002178 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2179 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002180
Johannes Bergbba95fe2008-07-29 13:22:51 +02002181 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2182 if (!ifidx)
2183 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002184 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002185
Johannes Berg3b858752009-03-12 09:55:09 +01002186 rtnl_lock();
2187
2188 netdev = __dev_get_by_index(&init_net, ifidx);
2189 if (!netdev) {
2190 err = -ENODEV;
2191 goto out_rtnl;
2192 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002193
Johannes Bergbba95fe2008-07-29 13:22:51 +02002194 dev = cfg80211_get_dev_from_ifindex(ifidx);
2195 if (IS_ERR(dev)) {
2196 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002197 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002198 }
2199
2200 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002201 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002202 goto out_err;
2203 }
2204
Jouni Malineneec60b02009-03-20 21:21:19 +02002205 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2206 err = -EOPNOTSUPP;
2207 goto out;
2208 }
2209
Johannes Bergbba95fe2008-07-29 13:22:51 +02002210 while (1) {
2211 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2212 dst, next_hop, &pinfo);
2213 if (err == -ENOENT)
2214 break;
2215 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002216 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002217
2218 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2219 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2220 netdev, dst, next_hop,
2221 &pinfo) < 0)
2222 goto out;
2223
2224 path_idx++;
2225 }
2226
2227
2228 out:
2229 cb->args[1] = path_idx;
2230 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002231 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002232 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002233 out_rtnl:
2234 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235
2236 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002237}
2238
2239static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2240{
Johannes Berg79c97e92009-07-07 03:56:12 +02002241 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002242 int err;
2243 struct net_device *dev;
2244 struct mpath_info pinfo;
2245 struct sk_buff *msg;
2246 u8 *dst = NULL;
2247 u8 next_hop[ETH_ALEN];
2248
2249 memset(&pinfo, 0, sizeof(pinfo));
2250
2251 if (!info->attrs[NL80211_ATTR_MAC])
2252 return -EINVAL;
2253
2254 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2255
Johannes Berg3b858752009-03-12 09:55:09 +01002256 rtnl_lock();
2257
Johannes Berg79c97e92009-07-07 03:56:12 +02002258 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002259 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002260 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002261
Johannes Berg79c97e92009-07-07 03:56:12 +02002262 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002263 err = -EOPNOTSUPP;
2264 goto out;
2265 }
2266
Jouni Malineneec60b02009-03-20 21:21:19 +02002267 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2268 err = -EOPNOTSUPP;
2269 goto out;
2270 }
2271
Johannes Berg79c97e92009-07-07 03:56:12 +02002272 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002273 if (err)
2274 goto out;
2275
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002276 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002277 if (!msg)
2278 goto out;
2279
2280 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2281 dev, dst, next_hop, &pinfo) < 0)
2282 goto out_free;
2283
Johannes Berg134e6372009-07-10 09:51:34 +00002284 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002285 goto out;
2286
2287 out_free:
2288 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002289 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002290 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002291 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002292 out_rtnl:
2293 rtnl_unlock();
2294
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295 return err;
2296}
2297
2298static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2299{
Johannes Berg79c97e92009-07-07 03:56:12 +02002300 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002301 int err;
2302 struct net_device *dev;
2303 u8 *dst = NULL;
2304 u8 *next_hop = NULL;
2305
2306 if (!info->attrs[NL80211_ATTR_MAC])
2307 return -EINVAL;
2308
2309 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2310 return -EINVAL;
2311
2312 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2313 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2314
Johannes Berg3b858752009-03-12 09:55:09 +01002315 rtnl_lock();
2316
Johannes Berg79c97e92009-07-07 03:56:12 +02002317 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002318 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002319 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002320
Johannes Berg79c97e92009-07-07 03:56:12 +02002321 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002322 err = -EOPNOTSUPP;
2323 goto out;
2324 }
2325
Jouni Malineneec60b02009-03-20 21:21:19 +02002326 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2327 err = -EOPNOTSUPP;
2328 goto out;
2329 }
2330
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002331 if (!netif_running(dev)) {
2332 err = -ENETDOWN;
2333 goto out;
2334 }
2335
Johannes Berg79c97e92009-07-07 03:56:12 +02002336 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002337
2338 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002339 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002340 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002341 out_rtnl:
2342 rtnl_unlock();
2343
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002344 return err;
2345}
2346static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2347{
Johannes Berg79c97e92009-07-07 03:56:12 +02002348 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002349 int err;
2350 struct net_device *dev;
2351 u8 *dst = NULL;
2352 u8 *next_hop = NULL;
2353
2354 if (!info->attrs[NL80211_ATTR_MAC])
2355 return -EINVAL;
2356
2357 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2358 return -EINVAL;
2359
2360 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2361 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2362
Johannes Berg3b858752009-03-12 09:55:09 +01002363 rtnl_lock();
2364
Johannes Berg79c97e92009-07-07 03:56:12 +02002365 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002367 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368
Johannes Berg79c97e92009-07-07 03:56:12 +02002369 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002370 err = -EOPNOTSUPP;
2371 goto out;
2372 }
2373
Jouni Malineneec60b02009-03-20 21:21:19 +02002374 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2375 err = -EOPNOTSUPP;
2376 goto out;
2377 }
2378
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002379 if (!netif_running(dev)) {
2380 err = -ENETDOWN;
2381 goto out;
2382 }
2383
Johannes Berg79c97e92009-07-07 03:56:12 +02002384 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002385
2386 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002387 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002388 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002389 out_rtnl:
2390 rtnl_unlock();
2391
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002392 return err;
2393}
2394
2395static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2396{
Johannes Berg79c97e92009-07-07 03:56:12 +02002397 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002398 int err;
2399 struct net_device *dev;
2400 u8 *dst = NULL;
2401
2402 if (info->attrs[NL80211_ATTR_MAC])
2403 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2404
Johannes Berg3b858752009-03-12 09:55:09 +01002405 rtnl_lock();
2406
Johannes Berg79c97e92009-07-07 03:56:12 +02002407 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002408 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002409 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002410
Johannes Berg79c97e92009-07-07 03:56:12 +02002411 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412 err = -EOPNOTSUPP;
2413 goto out;
2414 }
2415
Johannes Berg79c97e92009-07-07 03:56:12 +02002416 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002417
2418 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002419 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002420 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002421 out_rtnl:
2422 rtnl_unlock();
2423
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002424 return err;
2425}
2426
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002427static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg79c97e92009-07-07 03:56:12 +02002429 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002430 int err;
2431 struct net_device *dev;
2432 struct bss_parameters params;
2433
2434 memset(&params, 0, sizeof(params));
2435 /* default to not changing parameters */
2436 params.use_cts_prot = -1;
2437 params.use_short_preamble = -1;
2438 params.use_short_slot_time = -1;
2439
2440 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2441 params.use_cts_prot =
2442 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2443 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2444 params.use_short_preamble =
2445 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2446 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2447 params.use_short_slot_time =
2448 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002449 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2450 params.basic_rates =
2451 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2452 params.basic_rates_len =
2453 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2454 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002455
Johannes Berg3b858752009-03-12 09:55:09 +01002456 rtnl_lock();
2457
Johannes Berg79c97e92009-07-07 03:56:12 +02002458 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002459 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002460 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002461
Johannes Berg79c97e92009-07-07 03:56:12 +02002462 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002463 err = -EOPNOTSUPP;
2464 goto out;
2465 }
2466
Jouni Malineneec60b02009-03-20 21:21:19 +02002467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2468 err = -EOPNOTSUPP;
2469 goto out;
2470 }
2471
Johannes Berg79c97e92009-07-07 03:56:12 +02002472 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002473
2474 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002475 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002476 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002477 out_rtnl:
2478 rtnl_unlock();
2479
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002480 return err;
2481}
2482
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002483static const struct nla_policy
2484 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2485 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2486 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2487 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2488 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2489 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2490 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2491};
2492
2493static int parse_reg_rule(struct nlattr *tb[],
2494 struct ieee80211_reg_rule *reg_rule)
2495{
2496 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2497 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2498
2499 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2500 return -EINVAL;
2501 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2502 return -EINVAL;
2503 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2504 return -EINVAL;
2505 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2506 return -EINVAL;
2507 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2508 return -EINVAL;
2509
2510 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2511
2512 freq_range->start_freq_khz =
2513 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2514 freq_range->end_freq_khz =
2515 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2516 freq_range->max_bandwidth_khz =
2517 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2518
2519 power_rule->max_eirp =
2520 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2521
2522 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2523 power_rule->max_antenna_gain =
2524 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2525
2526 return 0;
2527}
2528
2529static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2530{
2531 int r;
2532 char *data = NULL;
2533
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002534 /*
2535 * You should only get this when cfg80211 hasn't yet initialized
2536 * completely when built-in to the kernel right between the time
2537 * window between nl80211_init() and regulatory_init(), if that is
2538 * even possible.
2539 */
2540 mutex_lock(&cfg80211_mutex);
2541 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002542 mutex_unlock(&cfg80211_mutex);
2543 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002544 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002545 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002546
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002547 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2548 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002549
2550 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2551
2552#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2553 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002554 if (is_world_regdom(data))
2555 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002556#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002557
2558 r = regulatory_hint_user(data);
2559
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002560 return r;
2561}
2562
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002563static int nl80211_get_mesh_params(struct sk_buff *skb,
2564 struct genl_info *info)
2565{
Johannes Berg79c97e92009-07-07 03:56:12 +02002566 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002567 struct mesh_config cur_params;
2568 int err;
2569 struct net_device *dev;
2570 void *hdr;
2571 struct nlattr *pinfoattr;
2572 struct sk_buff *msg;
2573
Johannes Berg3b858752009-03-12 09:55:09 +01002574 rtnl_lock();
2575
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002576 /* Look up our device */
Johannes Berg79c97e92009-07-07 03:56:12 +02002577 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002578 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002579 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002580
Johannes Berg79c97e92009-07-07 03:56:12 +02002581 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002582 err = -EOPNOTSUPP;
2583 goto out;
2584 }
2585
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002586 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002587 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002588 if (err)
2589 goto out;
2590
2591 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002592 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002593 if (!msg) {
2594 err = -ENOBUFS;
2595 goto out;
2596 }
2597 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2598 NL80211_CMD_GET_MESH_PARAMS);
2599 if (!hdr)
2600 goto nla_put_failure;
2601 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2602 if (!pinfoattr)
2603 goto nla_put_failure;
2604 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2605 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2606 cur_params.dot11MeshRetryTimeout);
2607 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2608 cur_params.dot11MeshConfirmTimeout);
2609 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2610 cur_params.dot11MeshHoldingTimeout);
2611 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2612 cur_params.dot11MeshMaxPeerLinks);
2613 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2614 cur_params.dot11MeshMaxRetries);
2615 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2616 cur_params.dot11MeshTTL);
2617 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2618 cur_params.auto_open_plinks);
2619 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2620 cur_params.dot11MeshHWMPmaxPREQretries);
2621 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2622 cur_params.path_refresh_time);
2623 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2624 cur_params.min_discovery_timeout);
2625 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2626 cur_params.dot11MeshHWMPactivePathTimeout);
2627 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2628 cur_params.dot11MeshHWMPpreqMinInterval);
2629 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2630 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2631 nla_nest_end(msg, pinfoattr);
2632 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002633 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002634 goto out;
2635
Johannes Berg3b858752009-03-12 09:55:09 +01002636 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002637 genlmsg_cancel(msg, hdr);
2638 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002639 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002640 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002641 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002642 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002643 out_rtnl:
2644 rtnl_unlock();
2645
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002646 return err;
2647}
2648
2649#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2650do {\
2651 if (table[attr_num]) {\
2652 cfg.param = nla_fn(table[attr_num]); \
2653 mask |= (1 << (attr_num - 1)); \
2654 } \
2655} while (0);\
2656
2657static struct nla_policy
2658nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2659 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2660 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2661 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2662 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2663 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2664 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2665 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2666
2667 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2668 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2669 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2670 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2671 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2672 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2673};
2674
2675static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2676{
2677 int err;
2678 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002679 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002680 struct net_device *dev;
2681 struct mesh_config cfg;
2682 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2683 struct nlattr *parent_attr;
2684
2685 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2686 if (!parent_attr)
2687 return -EINVAL;
2688 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2689 parent_attr, nl80211_meshconf_params_policy))
2690 return -EINVAL;
2691
Johannes Berg3b858752009-03-12 09:55:09 +01002692 rtnl_lock();
2693
Johannes Berg79c97e92009-07-07 03:56:12 +02002694 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002695 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002696 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002697
Johannes Berg79c97e92009-07-07 03:56:12 +02002698 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002699 err = -EOPNOTSUPP;
2700 goto out;
2701 }
2702
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002703 /* This makes sure that there aren't more than 32 mesh config
2704 * parameters (otherwise our bitfield scheme would not work.) */
2705 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2706
2707 /* Fill in the params struct */
2708 mask = 0;
2709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2710 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2712 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2714 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2716 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2718 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2720 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2722 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2723 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2724 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2725 nla_get_u8);
2726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2727 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2729 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2730 nla_get_u16);
2731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2732 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2733 nla_get_u32);
2734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2735 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2736 nla_get_u16);
2737 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2738 dot11MeshHWMPnetDiameterTraversalTime,
2739 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2740 nla_get_u16);
2741
2742 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002743 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002744
Jouni Malinenf3f92582009-03-20 17:57:36 +02002745 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002746 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002747 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002748 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002749 out_rtnl:
2750 rtnl_unlock();
2751
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002752 return err;
2753}
2754
2755#undef FILL_IN_MESH_PARAM_IF_SET
2756
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002757static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2758{
2759 struct sk_buff *msg;
2760 void *hdr = NULL;
2761 struct nlattr *nl_reg_rules;
2762 unsigned int i;
2763 int err = -EINVAL;
2764
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002765 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002766
2767 if (!cfg80211_regdomain)
2768 goto out;
2769
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002770 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002771 if (!msg) {
2772 err = -ENOBUFS;
2773 goto out;
2774 }
2775
2776 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2777 NL80211_CMD_GET_REG);
2778 if (!hdr)
2779 goto nla_put_failure;
2780
2781 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2782 cfg80211_regdomain->alpha2);
2783
2784 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2785 if (!nl_reg_rules)
2786 goto nla_put_failure;
2787
2788 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2789 struct nlattr *nl_reg_rule;
2790 const struct ieee80211_reg_rule *reg_rule;
2791 const struct ieee80211_freq_range *freq_range;
2792 const struct ieee80211_power_rule *power_rule;
2793
2794 reg_rule = &cfg80211_regdomain->reg_rules[i];
2795 freq_range = &reg_rule->freq_range;
2796 power_rule = &reg_rule->power_rule;
2797
2798 nl_reg_rule = nla_nest_start(msg, i);
2799 if (!nl_reg_rule)
2800 goto nla_put_failure;
2801
2802 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2803 reg_rule->flags);
2804 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2805 freq_range->start_freq_khz);
2806 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2807 freq_range->end_freq_khz);
2808 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2809 freq_range->max_bandwidth_khz);
2810 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2811 power_rule->max_antenna_gain);
2812 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2813 power_rule->max_eirp);
2814
2815 nla_nest_end(msg, nl_reg_rule);
2816 }
2817
2818 nla_nest_end(msg, nl_reg_rules);
2819
2820 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002821 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002822 goto out;
2823
2824nla_put_failure:
2825 genlmsg_cancel(msg, hdr);
2826 err = -EMSGSIZE;
2827out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002828 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002829 return err;
2830}
2831
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002832static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2833{
2834 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2835 struct nlattr *nl_reg_rule;
2836 char *alpha2 = NULL;
2837 int rem_reg_rules = 0, r = 0;
2838 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2839 struct ieee80211_regdomain *rd = NULL;
2840
2841 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2842 return -EINVAL;
2843
2844 if (!info->attrs[NL80211_ATTR_REG_RULES])
2845 return -EINVAL;
2846
2847 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2848
2849 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2850 rem_reg_rules) {
2851 num_rules++;
2852 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002853 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002854 }
2855
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002856 mutex_lock(&cfg80211_mutex);
2857
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002858 if (!reg_is_valid_request(alpha2)) {
2859 r = -EINVAL;
2860 goto bad_reg;
2861 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002862
2863 size_of_regd = sizeof(struct ieee80211_regdomain) +
2864 (num_rules * sizeof(struct ieee80211_reg_rule));
2865
2866 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002867 if (!rd) {
2868 r = -ENOMEM;
2869 goto bad_reg;
2870 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002871
2872 rd->n_reg_rules = num_rules;
2873 rd->alpha2[0] = alpha2[0];
2874 rd->alpha2[1] = alpha2[1];
2875
2876 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2877 rem_reg_rules) {
2878 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2879 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2880 reg_rule_policy);
2881 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2882 if (r)
2883 goto bad_reg;
2884
2885 rule_idx++;
2886
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002887 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2888 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002889 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002890 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002891 }
2892
2893 BUG_ON(rule_idx != num_rules);
2894
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002895 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002896
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002897 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002898
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002899 return r;
2900
Johannes Bergd2372b32008-10-24 20:32:20 +02002901 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002902 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002903 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002904 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002905}
2906
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002907static int validate_scan_freqs(struct nlattr *freqs)
2908{
2909 struct nlattr *attr1, *attr2;
2910 int n_channels = 0, tmp1, tmp2;
2911
2912 nla_for_each_nested(attr1, freqs, tmp1) {
2913 n_channels++;
2914 /*
2915 * Some hardware has a limited channel list for
2916 * scanning, and it is pretty much nonsensical
2917 * to scan for a channel twice, so disallow that
2918 * and don't require drivers to check that the
2919 * channel list they get isn't longer than what
2920 * they can scan, as long as they can scan all
2921 * the channels they registered at once.
2922 */
2923 nla_for_each_nested(attr2, freqs, tmp2)
2924 if (attr1 != attr2 &&
2925 nla_get_u32(attr1) == nla_get_u32(attr2))
2926 return 0;
2927 }
2928
2929 return n_channels;
2930}
2931
Johannes Berg2a519312009-02-10 21:25:55 +01002932static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2933{
Johannes Berg79c97e92009-07-07 03:56:12 +02002934 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002935 struct net_device *dev;
2936 struct cfg80211_scan_request *request;
2937 struct cfg80211_ssid *ssid;
2938 struct ieee80211_channel *channel;
2939 struct nlattr *attr;
2940 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002941 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002942 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002943 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002944
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002945 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2946 return -EINVAL;
2947
Johannes Berg3b858752009-03-12 09:55:09 +01002948 rtnl_lock();
2949
Johannes Berg79c97e92009-07-07 03:56:12 +02002950 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002951 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002952 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002953
Johannes Berg79c97e92009-07-07 03:56:12 +02002954 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002955
Johannes Berg79c97e92009-07-07 03:56:12 +02002956 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002957 err = -EOPNOTSUPP;
2958 goto out;
2959 }
2960
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002961 if (!netif_running(dev)) {
2962 err = -ENETDOWN;
2963 goto out;
2964 }
2965
Johannes Berg79c97e92009-07-07 03:56:12 +02002966 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002967 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002968 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002969 }
2970
2971 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002972 n_channels = validate_scan_freqs(
2973 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002974 if (!n_channels) {
2975 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002976 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002977 }
2978 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002979 n_channels = 0;
2980
Johannes Berg2a519312009-02-10 21:25:55 +01002981 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2982 if (wiphy->bands[band])
2983 n_channels += wiphy->bands[band]->n_channels;
2984 }
2985
2986 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2987 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2988 n_ssids++;
2989
2990 if (n_ssids > wiphy->max_scan_ssids) {
2991 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002992 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002993 }
2994
Jouni Malinen70692ad2009-02-16 19:39:13 +02002995 if (info->attrs[NL80211_ATTR_IE])
2996 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2997 else
2998 ie_len = 0;
2999
Johannes Berg18a83652009-03-31 12:12:05 +02003000 if (ie_len > wiphy->max_scan_ie_len) {
3001 err = -EINVAL;
3002 goto out;
3003 }
3004
Johannes Berg2a519312009-02-10 21:25:55 +01003005 request = kzalloc(sizeof(*request)
3006 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003007 + sizeof(channel) * n_channels
3008 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003009 if (!request) {
3010 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003011 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003012 }
3013
3014 request->channels = (void *)((char *)request + sizeof(*request));
3015 request->n_channels = n_channels;
3016 if (n_ssids)
3017 request->ssids = (void *)(request->channels + n_channels);
3018 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003019 if (ie_len) {
3020 if (request->ssids)
3021 request->ie = (void *)(request->ssids + n_ssids);
3022 else
3023 request->ie = (void *)(request->channels + n_channels);
3024 }
Johannes Berg2a519312009-02-10 21:25:55 +01003025
3026 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3027 /* user specified, bail out if channel not found */
3028 request->n_channels = n_channels;
3029 i = 0;
3030 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3031 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3032 if (!request->channels[i]) {
3033 err = -EINVAL;
3034 goto out_free;
3035 }
3036 i++;
3037 }
3038 } else {
3039 /* all channels */
3040 i = 0;
3041 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3042 int j;
3043 if (!wiphy->bands[band])
3044 continue;
3045 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3046 request->channels[i] = &wiphy->bands[band]->channels[j];
3047 i++;
3048 }
3049 }
3050 }
3051
3052 i = 0;
3053 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3054 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3055 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3056 err = -EINVAL;
3057 goto out_free;
3058 }
3059 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3060 request->ssids[i].ssid_len = nla_len(attr);
3061 i++;
3062 }
3063 }
3064
Jouni Malinen70692ad2009-02-16 19:39:13 +02003065 if (info->attrs[NL80211_ATTR_IE]) {
3066 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003067 memcpy((void *)request->ie,
3068 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003069 request->ie_len);
3070 }
3071
Johannes Berg2a519312009-02-10 21:25:55 +01003072 request->ifidx = dev->ifindex;
Johannes Berg79c97e92009-07-07 03:56:12 +02003073 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003074
Johannes Berg79c97e92009-07-07 03:56:12 +02003075 rdev->scan_req = request;
3076 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003077
Johannes Berga538e2d2009-06-16 19:56:42 +02003078 if (!err)
Johannes Berg79c97e92009-07-07 03:56:12 +02003079 nl80211_send_scan_start(rdev, dev);
Johannes Berga538e2d2009-06-16 19:56:42 +02003080
Johannes Berg2a519312009-02-10 21:25:55 +01003081 out_free:
3082 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003083 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003084 kfree(request);
3085 }
Johannes Berg2a519312009-02-10 21:25:55 +01003086 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003087 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003088 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003089 out_rtnl:
3090 rtnl_unlock();
3091
Johannes Berg2a519312009-02-10 21:25:55 +01003092 return err;
3093}
3094
3095static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3096 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003097 struct wireless_dev *wdev,
3098 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003099{
Johannes Berg48ab9052009-07-10 18:42:31 +02003100 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003101 void *hdr;
3102 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003103 int i;
3104
3105 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003106
3107 hdr = nl80211hdr_put(msg, pid, seq, flags,
3108 NL80211_CMD_NEW_SCAN_RESULTS);
3109 if (!hdr)
3110 return -1;
3111
3112 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
3113 rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003114 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003115
3116 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3117 if (!bss)
3118 goto nla_put_failure;
3119 if (!is_zero_ether_addr(res->bssid))
3120 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3121 if (res->information_elements && res->len_information_elements)
3122 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3123 res->len_information_elements,
3124 res->information_elements);
3125 if (res->tsf)
3126 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3127 if (res->beacon_interval)
3128 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3129 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3130 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3131
Johannes Berg77965c92009-02-18 18:45:06 +01003132 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003133 case CFG80211_SIGNAL_TYPE_MBM:
3134 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3135 break;
3136 case CFG80211_SIGNAL_TYPE_UNSPEC:
3137 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3138 break;
3139 default:
3140 break;
3141 }
3142
Johannes Berg48ab9052009-07-10 18:42:31 +02003143 switch (wdev->iftype) {
3144 case NL80211_IFTYPE_STATION:
3145 if (intbss == wdev->current_bss)
3146 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3147 NL80211_BSS_STATUS_ASSOCIATED);
3148 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3149 if (intbss != wdev->auth_bsses[i])
3150 continue;
3151 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3152 NL80211_BSS_STATUS_AUTHENTICATED);
3153 break;
3154 }
3155 break;
3156 case NL80211_IFTYPE_ADHOC:
3157 if (intbss == wdev->current_bss)
3158 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3159 NL80211_BSS_STATUS_IBSS_JOINED);
3160 break;
3161 default:
3162 break;
3163 }
3164
Johannes Berg2a519312009-02-10 21:25:55 +01003165 nla_nest_end(msg, bss);
3166
3167 return genlmsg_end(msg, hdr);
3168
3169 nla_put_failure:
3170 genlmsg_cancel(msg, hdr);
3171 return -EMSGSIZE;
3172}
3173
3174static int nl80211_dump_scan(struct sk_buff *skb,
3175 struct netlink_callback *cb)
3176{
Johannes Berg48ab9052009-07-10 18:42:31 +02003177 struct cfg80211_registered_device *rdev;
3178 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003179 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003180 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003181 int ifidx = cb->args[0];
3182 int start = cb->args[1], idx = 0;
3183 int err;
3184
3185 if (!ifidx) {
3186 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
3187 nl80211_fam.attrbuf, nl80211_fam.maxattr,
3188 nl80211_policy);
3189 if (err)
3190 return err;
3191
3192 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
3193 return -EINVAL;
3194
3195 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
3196 if (!ifidx)
3197 return -EINVAL;
3198 cb->args[0] = ifidx;
3199 }
3200
Johannes Berg48ab9052009-07-10 18:42:31 +02003201 dev = dev_get_by_index(&init_net, ifidx);
3202 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003203 return -ENODEV;
3204
Johannes Berg48ab9052009-07-10 18:42:31 +02003205 rdev = cfg80211_get_dev_from_ifindex(ifidx);
3206 if (IS_ERR(rdev)) {
3207 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003208 goto out_put_netdev;
3209 }
3210
Johannes Berg48ab9052009-07-10 18:42:31 +02003211 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003212
Johannes Berg48ab9052009-07-10 18:42:31 +02003213 wdev_lock(wdev);
3214 spin_lock_bh(&rdev->bss_lock);
3215 cfg80211_bss_expire(rdev);
3216
3217 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003218 if (++idx <= start)
3219 continue;
3220 if (nl80211_send_bss(skb,
3221 NETLINK_CB(cb->skb).pid,
3222 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003223 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003224 idx--;
3225 goto out;
3226 }
3227 }
3228
3229 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003230 spin_unlock_bh(&rdev->bss_lock);
3231 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003232
3233 cb->args[1] = idx;
3234 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003235 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003236 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003237 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003238
3239 return err;
3240}
3241
Jouni Malinen255e7372009-03-20 21:21:17 +02003242static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3243{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003244 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003245}
3246
Samuel Ortizb23aa672009-07-01 21:26:54 +02003247static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3248{
3249 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3250 NL80211_WPA_VERSION_2));
3251}
3252
3253static bool nl80211_valid_akm_suite(u32 akm)
3254{
3255 return akm == WLAN_AKM_SUITE_8021X ||
3256 akm == WLAN_AKM_SUITE_PSK;
3257}
3258
3259static bool nl80211_valid_cipher_suite(u32 cipher)
3260{
3261 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3262 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3263 cipher == WLAN_CIPHER_SUITE_TKIP ||
3264 cipher == WLAN_CIPHER_SUITE_CCMP ||
3265 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3266}
3267
3268
Jouni Malinen636a5d32009-03-19 13:39:22 +02003269static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3270{
Johannes Berg79c97e92009-07-07 03:56:12 +02003271 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003272 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003273 struct ieee80211_channel *chan;
3274 const u8 *bssid, *ssid, *ie = NULL;
3275 int err, ssid_len, ie_len = 0;
3276 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003277 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003278
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003279 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3280 return -EINVAL;
3281
3282 if (!info->attrs[NL80211_ATTR_MAC])
3283 return -EINVAL;
3284
Jouni Malinen17780922009-03-27 20:52:47 +02003285 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3286 return -EINVAL;
3287
Johannes Berg19957bb2009-07-02 17:20:43 +02003288 if (!info->attrs[NL80211_ATTR_SSID])
3289 return -EINVAL;
3290
3291 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3292 return -EINVAL;
3293
Johannes Bergfffd0932009-07-08 14:22:54 +02003294 err = nl80211_parse_key(info, &key);
3295 if (err)
3296 return err;
3297
3298 if (key.idx >= 0) {
3299 if (!key.p.key || !key.p.key_len)
3300 return -EINVAL;
3301 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3302 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3303 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3304 key.p.key_len != WLAN_KEY_LEN_WEP104))
3305 return -EINVAL;
3306 if (key.idx > 4)
3307 return -EINVAL;
3308 } else {
3309 key.p.key_len = 0;
3310 key.p.key = NULL;
3311 }
3312
Jouni Malinen636a5d32009-03-19 13:39:22 +02003313 rtnl_lock();
3314
Johannes Berg79c97e92009-07-07 03:56:12 +02003315 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003316 if (err)
3317 goto unlock_rtnl;
3318
Johannes Berg79c97e92009-07-07 03:56:12 +02003319 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003320 err = -EOPNOTSUPP;
3321 goto out;
3322 }
3323
Jouni Malineneec60b02009-03-20 21:21:19 +02003324 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3325 err = -EOPNOTSUPP;
3326 goto out;
3327 }
3328
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003329 if (!netif_running(dev)) {
3330 err = -ENETDOWN;
3331 goto out;
3332 }
3333
Johannes Berg19957bb2009-07-02 17:20:43 +02003334 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003335 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003336 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3337 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003338 err = -EINVAL;
3339 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003340 }
3341
Johannes Berg19957bb2009-07-02 17:20:43 +02003342 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3343 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3344
3345 if (info->attrs[NL80211_ATTR_IE]) {
3346 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3347 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3348 }
3349
3350 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3351 if (!nl80211_valid_auth_type(auth_type)) {
3352 err = -EINVAL;
3353 goto out;
3354 }
3355
Johannes Berg79c97e92009-07-07 03:56:12 +02003356 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003357 ssid, ssid_len, ie, ie_len,
3358 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003359
3360out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003361 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003362 dev_put(dev);
3363unlock_rtnl:
3364 rtnl_unlock();
3365 return err;
3366}
3367
Samuel Ortizb23aa672009-07-01 21:26:54 +02003368static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003369 struct cfg80211_crypto_settings *settings,
3370 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003371{
3372 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3373
3374 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3375 void *data;
3376 int len, i;
3377
3378 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3379 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3380 settings->n_ciphers_pairwise = len / sizeof(u32);
3381
3382 if (len % sizeof(u32))
3383 return -EINVAL;
3384
Johannes Berg3dc27d22009-07-02 21:36:37 +02003385 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003386 return -EINVAL;
3387
3388 memcpy(settings->ciphers_pairwise, data, len);
3389
3390 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3391 if (!nl80211_valid_cipher_suite(
3392 settings->ciphers_pairwise[i]))
3393 return -EINVAL;
3394 }
3395
3396 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3397 settings->cipher_group =
3398 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3399 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3400 return -EINVAL;
3401 }
3402
3403 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3404 settings->wpa_versions =
3405 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3406 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3407 return -EINVAL;
3408 }
3409
3410 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3411 void *data;
3412 int len, i;
3413
3414 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3415 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3416 settings->n_akm_suites = len / sizeof(u32);
3417
3418 if (len % sizeof(u32))
3419 return -EINVAL;
3420
3421 memcpy(settings->akm_suites, data, len);
3422
3423 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3424 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3425 return -EINVAL;
3426 }
3427
3428 return 0;
3429}
3430
Jouni Malinen636a5d32009-03-19 13:39:22 +02003431static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3432{
Johannes Berg19957bb2009-07-02 17:20:43 +02003433 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003434 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003435 struct cfg80211_crypto_settings crypto;
3436 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003437 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003438 int err, ssid_len, ie_len = 0;
3439 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003440
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003441 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3442 return -EINVAL;
3443
3444 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003445 !info->attrs[NL80211_ATTR_SSID] ||
3446 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003447 return -EINVAL;
3448
Jouni Malinen636a5d32009-03-19 13:39:22 +02003449 rtnl_lock();
3450
Johannes Berg79c97e92009-07-07 03:56:12 +02003451 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003452 if (err)
3453 goto unlock_rtnl;
3454
Johannes Berg19957bb2009-07-02 17:20:43 +02003455 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003456 err = -EOPNOTSUPP;
3457 goto out;
3458 }
3459
Jouni Malineneec60b02009-03-20 21:21:19 +02003460 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3461 err = -EOPNOTSUPP;
3462 goto out;
3463 }
3464
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003465 if (!netif_running(dev)) {
3466 err = -ENETDOWN;
3467 goto out;
3468 }
3469
Johannes Berg19957bb2009-07-02 17:20:43 +02003470 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003471
Johannes Berg19957bb2009-07-02 17:20:43 +02003472 chan = ieee80211_get_channel(&rdev->wiphy,
3473 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3474 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3475 err = -EINVAL;
3476 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003477 }
3478
Johannes Berg19957bb2009-07-02 17:20:43 +02003479 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3480 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003481
3482 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003483 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3484 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003485 }
3486
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003487 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003488 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003489 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003490 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003491 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003492 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003493 err = -EINVAL;
3494 goto out;
3495 }
3496 }
3497
Johannes Berg3e5d7642009-07-07 14:37:26 +02003498 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3499 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3500
Johannes Berg3dc27d22009-07-02 21:36:37 +02003501 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003502 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003503 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3504 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003505 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003506
3507out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003508 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003509 dev_put(dev);
3510unlock_rtnl:
3511 rtnl_unlock();
3512 return err;
3513}
3514
3515static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3516{
Johannes Berg79c97e92009-07-07 03:56:12 +02003517 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003518 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003519 const u8 *ie = NULL, *bssid;
3520 int err, ie_len = 0;
3521 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003522
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003523 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3524 return -EINVAL;
3525
3526 if (!info->attrs[NL80211_ATTR_MAC])
3527 return -EINVAL;
3528
3529 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3530 return -EINVAL;
3531
Jouni Malinen636a5d32009-03-19 13:39:22 +02003532 rtnl_lock();
3533
Johannes Berg79c97e92009-07-07 03:56:12 +02003534 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003535 if (err)
3536 goto unlock_rtnl;
3537
Johannes Berg79c97e92009-07-07 03:56:12 +02003538 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003539 err = -EOPNOTSUPP;
3540 goto out;
3541 }
3542
Jouni Malineneec60b02009-03-20 21:21:19 +02003543 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3544 err = -EOPNOTSUPP;
3545 goto out;
3546 }
3547
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003548 if (!netif_running(dev)) {
3549 err = -ENETDOWN;
3550 goto out;
3551 }
3552
Johannes Berg19957bb2009-07-02 17:20:43 +02003553 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003554
Johannes Berg19957bb2009-07-02 17:20:43 +02003555 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3556 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003557 /* Reason Code 0 is reserved */
3558 err = -EINVAL;
3559 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003560 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003561
3562 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003563 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3564 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003565 }
3566
Johannes Berg79c97e92009-07-07 03:56:12 +02003567 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003568
3569out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003570 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003571 dev_put(dev);
3572unlock_rtnl:
3573 rtnl_unlock();
3574 return err;
3575}
3576
3577static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3578{
Johannes Berg79c97e92009-07-07 03:56:12 +02003579 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003580 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003581 const u8 *ie = NULL, *bssid;
3582 int err, ie_len = 0;
3583 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003584
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003585 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3586 return -EINVAL;
3587
3588 if (!info->attrs[NL80211_ATTR_MAC])
3589 return -EINVAL;
3590
3591 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3592 return -EINVAL;
3593
Jouni Malinen636a5d32009-03-19 13:39:22 +02003594 rtnl_lock();
3595
Johannes Berg79c97e92009-07-07 03:56:12 +02003596 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003597 if (err)
3598 goto unlock_rtnl;
3599
Johannes Berg79c97e92009-07-07 03:56:12 +02003600 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003601 err = -EOPNOTSUPP;
3602 goto out;
3603 }
3604
Jouni Malineneec60b02009-03-20 21:21:19 +02003605 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3606 err = -EOPNOTSUPP;
3607 goto out;
3608 }
3609
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003610 if (!netif_running(dev)) {
3611 err = -ENETDOWN;
3612 goto out;
3613 }
3614
Johannes Berg19957bb2009-07-02 17:20:43 +02003615 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003616
Johannes Berg19957bb2009-07-02 17:20:43 +02003617 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3618 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003619 /* Reason Code 0 is reserved */
3620 err = -EINVAL;
3621 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003622 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003623
3624 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003625 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3626 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003627 }
3628
Johannes Berg79c97e92009-07-07 03:56:12 +02003629 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003630
3631out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003632 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003633 dev_put(dev);
3634unlock_rtnl:
3635 rtnl_unlock();
3636 return err;
3637}
3638
Johannes Berg04a773a2009-04-19 21:24:32 +02003639static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3640{
Johannes Berg79c97e92009-07-07 03:56:12 +02003641 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003642 struct net_device *dev;
3643 struct cfg80211_ibss_params ibss;
3644 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003645 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003646 int err;
3647
Johannes Berg8e30bc52009-04-22 17:45:38 +02003648 memset(&ibss, 0, sizeof(ibss));
3649
Johannes Berg04a773a2009-04-19 21:24:32 +02003650 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3651 return -EINVAL;
3652
3653 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3654 !info->attrs[NL80211_ATTR_SSID] ||
3655 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3656 return -EINVAL;
3657
Johannes Berg8e30bc52009-04-22 17:45:38 +02003658 ibss.beacon_interval = 100;
3659
3660 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3661 ibss.beacon_interval =
3662 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3663 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3664 return -EINVAL;
3665 }
3666
Johannes Berg04a773a2009-04-19 21:24:32 +02003667 rtnl_lock();
3668
Johannes Berg79c97e92009-07-07 03:56:12 +02003669 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003670 if (err)
3671 goto unlock_rtnl;
3672
Johannes Berg79c97e92009-07-07 03:56:12 +02003673 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003674 err = -EOPNOTSUPP;
3675 goto out;
3676 }
3677
3678 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3679 err = -EOPNOTSUPP;
3680 goto out;
3681 }
3682
3683 if (!netif_running(dev)) {
3684 err = -ENETDOWN;
3685 goto out;
3686 }
3687
Johannes Berg79c97e92009-07-07 03:56:12 +02003688 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003689
3690 if (info->attrs[NL80211_ATTR_MAC])
3691 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3692 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3693 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3694
3695 if (info->attrs[NL80211_ATTR_IE]) {
3696 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3697 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3698 }
3699
3700 ibss.channel = ieee80211_get_channel(wiphy,
3701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3702 if (!ibss.channel ||
3703 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3704 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3705 err = -EINVAL;
3706 goto out;
3707 }
3708
3709 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003710 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003711
Johannes Bergfffd0932009-07-08 14:22:54 +02003712 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3713 connkeys = nl80211_parse_connkeys(rdev,
3714 info->attrs[NL80211_ATTR_KEYS]);
3715 if (IS_ERR(connkeys)) {
3716 err = PTR_ERR(connkeys);
3717 connkeys = NULL;
3718 goto out;
3719 }
3720 }
3721
3722 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003723
3724out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003725 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003726 dev_put(dev);
3727unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003728 if (err)
3729 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003730 rtnl_unlock();
3731 return err;
3732}
3733
3734static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3735{
Johannes Berg79c97e92009-07-07 03:56:12 +02003736 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003737 struct net_device *dev;
3738 int err;
3739
3740 rtnl_lock();
3741
Johannes Berg79c97e92009-07-07 03:56:12 +02003742 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003743 if (err)
3744 goto unlock_rtnl;
3745
Johannes Berg79c97e92009-07-07 03:56:12 +02003746 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003747 err = -EOPNOTSUPP;
3748 goto out;
3749 }
3750
3751 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3752 err = -EOPNOTSUPP;
3753 goto out;
3754 }
3755
3756 if (!netif_running(dev)) {
3757 err = -ENETDOWN;
3758 goto out;
3759 }
3760
Johannes Berg79c97e92009-07-07 03:56:12 +02003761 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003762
3763out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003764 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003765 dev_put(dev);
3766unlock_rtnl:
3767 rtnl_unlock();
3768 return err;
3769}
3770
Johannes Bergaff89a92009-07-01 21:26:51 +02003771#ifdef CONFIG_NL80211_TESTMODE
3772static struct genl_multicast_group nl80211_testmode_mcgrp = {
3773 .name = "testmode",
3774};
3775
3776static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3777{
3778 struct cfg80211_registered_device *rdev;
3779 int err;
3780
3781 if (!info->attrs[NL80211_ATTR_TESTDATA])
3782 return -EINVAL;
3783
3784 rtnl_lock();
3785
3786 rdev = cfg80211_get_dev_from_info(info);
3787 if (IS_ERR(rdev)) {
3788 err = PTR_ERR(rdev);
3789 goto unlock_rtnl;
3790 }
3791
3792 err = -EOPNOTSUPP;
3793 if (rdev->ops->testmode_cmd) {
3794 rdev->testmode_info = info;
3795 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3796 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3797 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3798 rdev->testmode_info = NULL;
3799 }
3800
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003801 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003802
3803 unlock_rtnl:
3804 rtnl_unlock();
3805 return err;
3806}
3807
3808static struct sk_buff *
3809__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3810 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3811{
3812 struct sk_buff *skb;
3813 void *hdr;
3814 struct nlattr *data;
3815
3816 skb = nlmsg_new(approxlen + 100, gfp);
3817 if (!skb)
3818 return NULL;
3819
3820 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3821 if (!hdr) {
3822 kfree_skb(skb);
3823 return NULL;
3824 }
3825
3826 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3827 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3828
3829 ((void **)skb->cb)[0] = rdev;
3830 ((void **)skb->cb)[1] = hdr;
3831 ((void **)skb->cb)[2] = data;
3832
3833 return skb;
3834
3835 nla_put_failure:
3836 kfree_skb(skb);
3837 return NULL;
3838}
3839
3840struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3841 int approxlen)
3842{
3843 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3844
3845 if (WARN_ON(!rdev->testmode_info))
3846 return NULL;
3847
3848 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3849 rdev->testmode_info->snd_pid,
3850 rdev->testmode_info->snd_seq,
3851 GFP_KERNEL);
3852}
3853EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3854
3855int cfg80211_testmode_reply(struct sk_buff *skb)
3856{
3857 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3858 void *hdr = ((void **)skb->cb)[1];
3859 struct nlattr *data = ((void **)skb->cb)[2];
3860
3861 if (WARN_ON(!rdev->testmode_info)) {
3862 kfree_skb(skb);
3863 return -EINVAL;
3864 }
3865
3866 nla_nest_end(skb, data);
3867 genlmsg_end(skb, hdr);
3868 return genlmsg_reply(skb, rdev->testmode_info);
3869}
3870EXPORT_SYMBOL(cfg80211_testmode_reply);
3871
3872struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3873 int approxlen, gfp_t gfp)
3874{
3875 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3876
3877 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3878}
3879EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3880
3881void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3882{
3883 void *hdr = ((void **)skb->cb)[1];
3884 struct nlattr *data = ((void **)skb->cb)[2];
3885
3886 nla_nest_end(skb, data);
3887 genlmsg_end(skb, hdr);
3888 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3889}
3890EXPORT_SYMBOL(cfg80211_testmode_event);
3891#endif
3892
Samuel Ortizb23aa672009-07-01 21:26:54 +02003893static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3894{
Johannes Berg79c97e92009-07-07 03:56:12 +02003895 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003896 struct net_device *dev;
3897 struct cfg80211_connect_params connect;
3898 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003899 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003900 int err;
3901
3902 memset(&connect, 0, sizeof(connect));
3903
3904 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3905 return -EINVAL;
3906
3907 if (!info->attrs[NL80211_ATTR_SSID] ||
3908 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3909 return -EINVAL;
3910
3911 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3912 connect.auth_type =
3913 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3914 if (!nl80211_valid_auth_type(connect.auth_type))
3915 return -EINVAL;
3916 } else
3917 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3918
3919 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3920
Johannes Berg3dc27d22009-07-02 21:36:37 +02003921 err = nl80211_crypto_settings(info, &connect.crypto,
3922 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003923 if (err)
3924 return err;
3925 rtnl_lock();
3926
Johannes Berg79c97e92009-07-07 03:56:12 +02003927 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003928 if (err)
3929 goto unlock_rtnl;
3930
3931 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3932 err = -EOPNOTSUPP;
3933 goto out;
3934 }
3935
3936 if (!netif_running(dev)) {
3937 err = -ENETDOWN;
3938 goto out;
3939 }
3940
Johannes Berg79c97e92009-07-07 03:56:12 +02003941 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003942
Samuel Ortizb23aa672009-07-01 21:26:54 +02003943 if (info->attrs[NL80211_ATTR_MAC])
3944 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3945 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3946 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3947
3948 if (info->attrs[NL80211_ATTR_IE]) {
3949 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3950 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3951 }
3952
3953 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3954 connect.channel =
3955 ieee80211_get_channel(wiphy,
3956 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3957 if (!connect.channel ||
3958 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
3959 err = -EINVAL;
3960 goto out;
3961 }
3962 }
3963
Johannes Bergfffd0932009-07-08 14:22:54 +02003964 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3965 connkeys = nl80211_parse_connkeys(rdev,
3966 info->attrs[NL80211_ATTR_KEYS]);
3967 if (IS_ERR(connkeys)) {
3968 err = PTR_ERR(connkeys);
3969 connkeys = NULL;
3970 goto out;
3971 }
3972 }
3973
3974 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003975
3976out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003977 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003978 dev_put(dev);
3979unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003980 if (err)
3981 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003982 rtnl_unlock();
3983 return err;
3984}
3985
3986static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3987{
Johannes Berg79c97e92009-07-07 03:56:12 +02003988 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003989 struct net_device *dev;
3990 int err;
3991 u16 reason;
3992
3993 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3994 reason = WLAN_REASON_DEAUTH_LEAVING;
3995 else
3996 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3997
3998 if (reason == 0)
3999 return -EINVAL;
4000
4001 rtnl_lock();
4002
Johannes Berg79c97e92009-07-07 03:56:12 +02004003 err = get_rdev_dev_by_info_ifindex(info->attrs, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004004 if (err)
4005 goto unlock_rtnl;
4006
4007 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4008 err = -EOPNOTSUPP;
4009 goto out;
4010 }
4011
4012 if (!netif_running(dev)) {
4013 err = -ENETDOWN;
4014 goto out;
4015 }
4016
Johannes Berg79c97e92009-07-07 03:56:12 +02004017 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004018
4019out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004020 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004021 dev_put(dev);
4022unlock_rtnl:
4023 rtnl_unlock();
4024 return err;
4025}
4026
Johannes Berg55682962007-09-20 13:09:35 -04004027static struct genl_ops nl80211_ops[] = {
4028 {
4029 .cmd = NL80211_CMD_GET_WIPHY,
4030 .doit = nl80211_get_wiphy,
4031 .dumpit = nl80211_dump_wiphy,
4032 .policy = nl80211_policy,
4033 /* can be retrieved by unprivileged users */
4034 },
4035 {
4036 .cmd = NL80211_CMD_SET_WIPHY,
4037 .doit = nl80211_set_wiphy,
4038 .policy = nl80211_policy,
4039 .flags = GENL_ADMIN_PERM,
4040 },
4041 {
4042 .cmd = NL80211_CMD_GET_INTERFACE,
4043 .doit = nl80211_get_interface,
4044 .dumpit = nl80211_dump_interface,
4045 .policy = nl80211_policy,
4046 /* can be retrieved by unprivileged users */
4047 },
4048 {
4049 .cmd = NL80211_CMD_SET_INTERFACE,
4050 .doit = nl80211_set_interface,
4051 .policy = nl80211_policy,
4052 .flags = GENL_ADMIN_PERM,
4053 },
4054 {
4055 .cmd = NL80211_CMD_NEW_INTERFACE,
4056 .doit = nl80211_new_interface,
4057 .policy = nl80211_policy,
4058 .flags = GENL_ADMIN_PERM,
4059 },
4060 {
4061 .cmd = NL80211_CMD_DEL_INTERFACE,
4062 .doit = nl80211_del_interface,
4063 .policy = nl80211_policy,
4064 .flags = GENL_ADMIN_PERM,
4065 },
Johannes Berg41ade002007-12-19 02:03:29 +01004066 {
4067 .cmd = NL80211_CMD_GET_KEY,
4068 .doit = nl80211_get_key,
4069 .policy = nl80211_policy,
4070 .flags = GENL_ADMIN_PERM,
4071 },
4072 {
4073 .cmd = NL80211_CMD_SET_KEY,
4074 .doit = nl80211_set_key,
4075 .policy = nl80211_policy,
4076 .flags = GENL_ADMIN_PERM,
4077 },
4078 {
4079 .cmd = NL80211_CMD_NEW_KEY,
4080 .doit = nl80211_new_key,
4081 .policy = nl80211_policy,
4082 .flags = GENL_ADMIN_PERM,
4083 },
4084 {
4085 .cmd = NL80211_CMD_DEL_KEY,
4086 .doit = nl80211_del_key,
4087 .policy = nl80211_policy,
4088 .flags = GENL_ADMIN_PERM,
4089 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004090 {
4091 .cmd = NL80211_CMD_SET_BEACON,
4092 .policy = nl80211_policy,
4093 .flags = GENL_ADMIN_PERM,
4094 .doit = nl80211_addset_beacon,
4095 },
4096 {
4097 .cmd = NL80211_CMD_NEW_BEACON,
4098 .policy = nl80211_policy,
4099 .flags = GENL_ADMIN_PERM,
4100 .doit = nl80211_addset_beacon,
4101 },
4102 {
4103 .cmd = NL80211_CMD_DEL_BEACON,
4104 .policy = nl80211_policy,
4105 .flags = GENL_ADMIN_PERM,
4106 .doit = nl80211_del_beacon,
4107 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004108 {
4109 .cmd = NL80211_CMD_GET_STATION,
4110 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004111 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004112 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004113 },
4114 {
4115 .cmd = NL80211_CMD_SET_STATION,
4116 .doit = nl80211_set_station,
4117 .policy = nl80211_policy,
4118 .flags = GENL_ADMIN_PERM,
4119 },
4120 {
4121 .cmd = NL80211_CMD_NEW_STATION,
4122 .doit = nl80211_new_station,
4123 .policy = nl80211_policy,
4124 .flags = GENL_ADMIN_PERM,
4125 },
4126 {
4127 .cmd = NL80211_CMD_DEL_STATION,
4128 .doit = nl80211_del_station,
4129 .policy = nl80211_policy,
4130 .flags = GENL_ADMIN_PERM,
4131 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004132 {
4133 .cmd = NL80211_CMD_GET_MPATH,
4134 .doit = nl80211_get_mpath,
4135 .dumpit = nl80211_dump_mpath,
4136 .policy = nl80211_policy,
4137 .flags = GENL_ADMIN_PERM,
4138 },
4139 {
4140 .cmd = NL80211_CMD_SET_MPATH,
4141 .doit = nl80211_set_mpath,
4142 .policy = nl80211_policy,
4143 .flags = GENL_ADMIN_PERM,
4144 },
4145 {
4146 .cmd = NL80211_CMD_NEW_MPATH,
4147 .doit = nl80211_new_mpath,
4148 .policy = nl80211_policy,
4149 .flags = GENL_ADMIN_PERM,
4150 },
4151 {
4152 .cmd = NL80211_CMD_DEL_MPATH,
4153 .doit = nl80211_del_mpath,
4154 .policy = nl80211_policy,
4155 .flags = GENL_ADMIN_PERM,
4156 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004157 {
4158 .cmd = NL80211_CMD_SET_BSS,
4159 .doit = nl80211_set_bss,
4160 .policy = nl80211_policy,
4161 .flags = GENL_ADMIN_PERM,
4162 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004163 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004164 .cmd = NL80211_CMD_GET_REG,
4165 .doit = nl80211_get_reg,
4166 .policy = nl80211_policy,
4167 /* can be retrieved by unprivileged users */
4168 },
4169 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004170 .cmd = NL80211_CMD_SET_REG,
4171 .doit = nl80211_set_reg,
4172 .policy = nl80211_policy,
4173 .flags = GENL_ADMIN_PERM,
4174 },
4175 {
4176 .cmd = NL80211_CMD_REQ_SET_REG,
4177 .doit = nl80211_req_set_reg,
4178 .policy = nl80211_policy,
4179 .flags = GENL_ADMIN_PERM,
4180 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004181 {
4182 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4183 .doit = nl80211_get_mesh_params,
4184 .policy = nl80211_policy,
4185 /* can be retrieved by unprivileged users */
4186 },
4187 {
4188 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4189 .doit = nl80211_set_mesh_params,
4190 .policy = nl80211_policy,
4191 .flags = GENL_ADMIN_PERM,
4192 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004193 {
Johannes Berg2a519312009-02-10 21:25:55 +01004194 .cmd = NL80211_CMD_TRIGGER_SCAN,
4195 .doit = nl80211_trigger_scan,
4196 .policy = nl80211_policy,
4197 .flags = GENL_ADMIN_PERM,
4198 },
4199 {
4200 .cmd = NL80211_CMD_GET_SCAN,
4201 .policy = nl80211_policy,
4202 .dumpit = nl80211_dump_scan,
4203 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004204 {
4205 .cmd = NL80211_CMD_AUTHENTICATE,
4206 .doit = nl80211_authenticate,
4207 .policy = nl80211_policy,
4208 .flags = GENL_ADMIN_PERM,
4209 },
4210 {
4211 .cmd = NL80211_CMD_ASSOCIATE,
4212 .doit = nl80211_associate,
4213 .policy = nl80211_policy,
4214 .flags = GENL_ADMIN_PERM,
4215 },
4216 {
4217 .cmd = NL80211_CMD_DEAUTHENTICATE,
4218 .doit = nl80211_deauthenticate,
4219 .policy = nl80211_policy,
4220 .flags = GENL_ADMIN_PERM,
4221 },
4222 {
4223 .cmd = NL80211_CMD_DISASSOCIATE,
4224 .doit = nl80211_disassociate,
4225 .policy = nl80211_policy,
4226 .flags = GENL_ADMIN_PERM,
4227 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004228 {
4229 .cmd = NL80211_CMD_JOIN_IBSS,
4230 .doit = nl80211_join_ibss,
4231 .policy = nl80211_policy,
4232 .flags = GENL_ADMIN_PERM,
4233 },
4234 {
4235 .cmd = NL80211_CMD_LEAVE_IBSS,
4236 .doit = nl80211_leave_ibss,
4237 .policy = nl80211_policy,
4238 .flags = GENL_ADMIN_PERM,
4239 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004240#ifdef CONFIG_NL80211_TESTMODE
4241 {
4242 .cmd = NL80211_CMD_TESTMODE,
4243 .doit = nl80211_testmode_do,
4244 .policy = nl80211_policy,
4245 .flags = GENL_ADMIN_PERM,
4246 },
4247#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004248 {
4249 .cmd = NL80211_CMD_CONNECT,
4250 .doit = nl80211_connect,
4251 .policy = nl80211_policy,
4252 .flags = GENL_ADMIN_PERM,
4253 },
4254 {
4255 .cmd = NL80211_CMD_DISCONNECT,
4256 .doit = nl80211_disconnect,
4257 .policy = nl80211_policy,
4258 .flags = GENL_ADMIN_PERM,
4259 },
Johannes Berg55682962007-09-20 13:09:35 -04004260};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004261static struct genl_multicast_group nl80211_mlme_mcgrp = {
4262 .name = "mlme",
4263};
Johannes Berg55682962007-09-20 13:09:35 -04004264
4265/* multicast groups */
4266static struct genl_multicast_group nl80211_config_mcgrp = {
4267 .name = "config",
4268};
Johannes Berg2a519312009-02-10 21:25:55 +01004269static struct genl_multicast_group nl80211_scan_mcgrp = {
4270 .name = "scan",
4271};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004272static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4273 .name = "regulatory",
4274};
Johannes Berg55682962007-09-20 13:09:35 -04004275
4276/* notification functions */
4277
4278void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4279{
4280 struct sk_buff *msg;
4281
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004283 if (!msg)
4284 return;
4285
4286 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4287 nlmsg_free(msg);
4288 return;
4289 }
4290
4291 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
4292}
4293
Johannes Berg362a4152009-05-24 16:43:15 +02004294static int nl80211_add_scan_req(struct sk_buff *msg,
4295 struct cfg80211_registered_device *rdev)
4296{
4297 struct cfg80211_scan_request *req = rdev->scan_req;
4298 struct nlattr *nest;
4299 int i;
4300
Johannes Berg667503d2009-07-07 03:56:11 +02004301 ASSERT_RDEV_LOCK(rdev);
4302
Johannes Berg362a4152009-05-24 16:43:15 +02004303 if (WARN_ON(!req))
4304 return 0;
4305
4306 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4307 if (!nest)
4308 goto nla_put_failure;
4309 for (i = 0; i < req->n_ssids; i++)
4310 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4311 nla_nest_end(msg, nest);
4312
4313 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4314 if (!nest)
4315 goto nla_put_failure;
4316 for (i = 0; i < req->n_channels; i++)
4317 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4318 nla_nest_end(msg, nest);
4319
4320 if (req->ie)
4321 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4322
4323 return 0;
4324 nla_put_failure:
4325 return -ENOBUFS;
4326}
4327
Johannes Berga538e2d2009-06-16 19:56:42 +02004328static int nl80211_send_scan_msg(struct sk_buff *msg,
4329 struct cfg80211_registered_device *rdev,
4330 struct net_device *netdev,
4331 u32 pid, u32 seq, int flags,
4332 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004333{
4334 void *hdr;
4335
4336 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4337 if (!hdr)
4338 return -1;
4339
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004340 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4342
Johannes Berg362a4152009-05-24 16:43:15 +02004343 /* ignore errors and send incomplete event anyway */
4344 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004345
4346 return genlmsg_end(msg, hdr);
4347
4348 nla_put_failure:
4349 genlmsg_cancel(msg, hdr);
4350 return -EMSGSIZE;
4351}
4352
Johannes Berga538e2d2009-06-16 19:56:42 +02004353void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4354 struct net_device *netdev)
4355{
4356 struct sk_buff *msg;
4357
4358 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4359 if (!msg)
4360 return;
4361
4362 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4363 NL80211_CMD_TRIGGER_SCAN) < 0) {
4364 nlmsg_free(msg);
4365 return;
4366 }
4367
4368 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4369}
4370
Johannes Berg2a519312009-02-10 21:25:55 +01004371void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4372 struct net_device *netdev)
4373{
4374 struct sk_buff *msg;
4375
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004377 if (!msg)
4378 return;
4379
Johannes Berga538e2d2009-06-16 19:56:42 +02004380 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4381 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004382 nlmsg_free(msg);
4383 return;
4384 }
4385
4386 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4387}
4388
4389void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4390 struct net_device *netdev)
4391{
4392 struct sk_buff *msg;
4393
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004394 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004395 if (!msg)
4396 return;
4397
Johannes Berga538e2d2009-06-16 19:56:42 +02004398 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4399 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004400 nlmsg_free(msg);
4401 return;
4402 }
4403
4404 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
4405}
4406
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004407/*
4408 * This can happen on global regulatory changes or device specific settings
4409 * based on custom world regulatory domains.
4410 */
4411void nl80211_send_reg_change_event(struct regulatory_request *request)
4412{
4413 struct sk_buff *msg;
4414 void *hdr;
4415
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004416 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004417 if (!msg)
4418 return;
4419
4420 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4421 if (!hdr) {
4422 nlmsg_free(msg);
4423 return;
4424 }
4425
4426 /* Userspace can always count this one always being set */
4427 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4428
4429 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4430 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4431 NL80211_REGDOM_TYPE_WORLD);
4432 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4433 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4434 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4435 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4436 request->intersect)
4437 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4438 NL80211_REGDOM_TYPE_INTERSECTION);
4439 else {
4440 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4441 NL80211_REGDOM_TYPE_COUNTRY);
4442 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4443 }
4444
4445 if (wiphy_idx_valid(request->wiphy_idx))
4446 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4447
4448 if (genlmsg_end(msg, hdr) < 0) {
4449 nlmsg_free(msg);
4450 return;
4451 }
4452
4453 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
4454
4455 return;
4456
4457nla_put_failure:
4458 genlmsg_cancel(msg, hdr);
4459 nlmsg_free(msg);
4460}
4461
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004462static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4463 struct net_device *netdev,
4464 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004465 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004466{
4467 struct sk_buff *msg;
4468 void *hdr;
4469
Johannes Berge6d6e342009-07-01 21:26:47 +02004470 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004471 if (!msg)
4472 return;
4473
4474 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4475 if (!hdr) {
4476 nlmsg_free(msg);
4477 return;
4478 }
4479
4480 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4482 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4483
4484 if (genlmsg_end(msg, hdr) < 0) {
4485 nlmsg_free(msg);
4486 return;
4487 }
4488
Johannes Berge6d6e342009-07-01 21:26:47 +02004489 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004490 return;
4491
4492 nla_put_failure:
4493 genlmsg_cancel(msg, hdr);
4494 nlmsg_free(msg);
4495}
4496
4497void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004498 struct net_device *netdev, const u8 *buf,
4499 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004500{
4501 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004502 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004503}
4504
4505void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4506 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004507 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004508{
Johannes Berge6d6e342009-07-01 21:26:47 +02004509 nl80211_send_mlme_event(rdev, netdev, buf, len,
4510 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004511}
4512
Jouni Malinen53b46b82009-03-27 20:53:56 +02004513void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004514 struct net_device *netdev, const u8 *buf,
4515 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004516{
4517 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004518 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004519}
4520
Jouni Malinen53b46b82009-03-27 20:53:56 +02004521void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4522 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004523 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004524{
4525 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004526 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004527}
4528
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04004529static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4530 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02004531 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004532{
4533 struct sk_buff *msg;
4534 void *hdr;
4535
Johannes Berge6d6e342009-07-01 21:26:47 +02004536 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004537 if (!msg)
4538 return;
4539
4540 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4541 if (!hdr) {
4542 nlmsg_free(msg);
4543 return;
4544 }
4545
4546 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4547 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4548 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4549 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4550
4551 if (genlmsg_end(msg, hdr) < 0) {
4552 nlmsg_free(msg);
4553 return;
4554 }
4555
Johannes Berge6d6e342009-07-01 21:26:47 +02004556 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004557 return;
4558
4559 nla_put_failure:
4560 genlmsg_cancel(msg, hdr);
4561 nlmsg_free(msg);
4562}
4563
4564void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004565 struct net_device *netdev, const u8 *addr,
4566 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004567{
4568 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02004569 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004570}
4571
4572void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004573 struct net_device *netdev, const u8 *addr,
4574 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004575{
Johannes Berge6d6e342009-07-01 21:26:47 +02004576 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4577 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004578}
4579
Samuel Ortizb23aa672009-07-01 21:26:54 +02004580void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4581 struct net_device *netdev, const u8 *bssid,
4582 const u8 *req_ie, size_t req_ie_len,
4583 const u8 *resp_ie, size_t resp_ie_len,
4584 u16 status, gfp_t gfp)
4585{
4586 struct sk_buff *msg;
4587 void *hdr;
4588
4589 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4590 if (!msg)
4591 return;
4592
4593 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4594 if (!hdr) {
4595 nlmsg_free(msg);
4596 return;
4597 }
4598
4599 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4600 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4601 if (bssid)
4602 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4603 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4604 if (req_ie)
4605 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4606 if (resp_ie)
4607 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4608
4609 if (genlmsg_end(msg, hdr) < 0) {
4610 nlmsg_free(msg);
4611 return;
4612 }
4613
4614 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4615 return;
4616
4617 nla_put_failure:
4618 genlmsg_cancel(msg, hdr);
4619 nlmsg_free(msg);
4620
4621}
4622
4623void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4624 struct net_device *netdev, const u8 *bssid,
4625 const u8 *req_ie, size_t req_ie_len,
4626 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4627{
4628 struct sk_buff *msg;
4629 void *hdr;
4630
4631 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4632 if (!msg)
4633 return;
4634
4635 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4636 if (!hdr) {
4637 nlmsg_free(msg);
4638 return;
4639 }
4640
4641 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4642 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4643 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4644 if (req_ie)
4645 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4646 if (resp_ie)
4647 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4648
4649 if (genlmsg_end(msg, hdr) < 0) {
4650 nlmsg_free(msg);
4651 return;
4652 }
4653
4654 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4655 return;
4656
4657 nla_put_failure:
4658 genlmsg_cancel(msg, hdr);
4659 nlmsg_free(msg);
4660
4661}
4662
4663void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
4664 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02004665 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004666{
4667 struct sk_buff *msg;
4668 void *hdr;
4669
Johannes Berg667503d2009-07-07 03:56:11 +02004670 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004671 if (!msg)
4672 return;
4673
4674 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
4675 if (!hdr) {
4676 nlmsg_free(msg);
4677 return;
4678 }
4679
4680 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4681 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4682 if (from_ap && reason)
4683 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
4684 if (from_ap)
4685 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
4686 if (ie)
4687 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
4688
4689 if (genlmsg_end(msg, hdr) < 0) {
4690 nlmsg_free(msg);
4691 return;
4692 }
4693
Johannes Berg667503d2009-07-07 03:56:11 +02004694 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004695 return;
4696
4697 nla_put_failure:
4698 genlmsg_cancel(msg, hdr);
4699 nlmsg_free(msg);
4700
4701}
4702
Johannes Berg04a773a2009-04-19 21:24:32 +02004703void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
4704 struct net_device *netdev, const u8 *bssid,
4705 gfp_t gfp)
4706{
4707 struct sk_buff *msg;
4708 void *hdr;
4709
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02004711 if (!msg)
4712 return;
4713
4714 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
4715 if (!hdr) {
4716 nlmsg_free(msg);
4717 return;
4718 }
4719
4720 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4721 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4722 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4723
4724 if (genlmsg_end(msg, hdr) < 0) {
4725 nlmsg_free(msg);
4726 return;
4727 }
4728
4729 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
4730 return;
4731
4732 nla_put_failure:
4733 genlmsg_cancel(msg, hdr);
4734 nlmsg_free(msg);
4735}
4736
Jouni Malinena3b8b052009-03-27 21:59:49 +02004737void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
4738 struct net_device *netdev, const u8 *addr,
4739 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02004740 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02004741{
4742 struct sk_buff *msg;
4743 void *hdr;
4744
Johannes Berge6d6e342009-07-01 21:26:47 +02004745 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004746 if (!msg)
4747 return;
4748
4749 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
4750 if (!hdr) {
4751 nlmsg_free(msg);
4752 return;
4753 }
4754
4755 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4756 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4757 if (addr)
4758 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4759 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
4760 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
4761 if (tsc)
4762 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
4763
4764 if (genlmsg_end(msg, hdr) < 0) {
4765 nlmsg_free(msg);
4766 return;
4767 }
4768
Johannes Berge6d6e342009-07-01 21:26:47 +02004769 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02004770 return;
4771
4772 nla_put_failure:
4773 genlmsg_cancel(msg, hdr);
4774 nlmsg_free(msg);
4775}
4776
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004777void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
4778 struct ieee80211_channel *channel_before,
4779 struct ieee80211_channel *channel_after)
4780{
4781 struct sk_buff *msg;
4782 void *hdr;
4783 struct nlattr *nl_freq;
4784
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004785 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04004786 if (!msg)
4787 return;
4788
4789 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
4790 if (!hdr) {
4791 nlmsg_free(msg);
4792 return;
4793 }
4794
4795 /*
4796 * Since we are applying the beacon hint to a wiphy we know its
4797 * wiphy_idx is valid
4798 */
4799 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
4800
4801 /* Before */
4802 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
4803 if (!nl_freq)
4804 goto nla_put_failure;
4805 if (nl80211_msg_put_channel(msg, channel_before))
4806 goto nla_put_failure;
4807 nla_nest_end(msg, nl_freq);
4808
4809 /* After */
4810 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
4811 if (!nl_freq)
4812 goto nla_put_failure;
4813 if (nl80211_msg_put_channel(msg, channel_after))
4814 goto nla_put_failure;
4815 nla_nest_end(msg, nl_freq);
4816
4817 if (genlmsg_end(msg, hdr) < 0) {
4818 nlmsg_free(msg);
4819 return;
4820 }
4821
4822 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC);
4823
4824 return;
4825
4826nla_put_failure:
4827 genlmsg_cancel(msg, hdr);
4828 nlmsg_free(msg);
4829}
4830
Johannes Berg55682962007-09-20 13:09:35 -04004831/* initialisation/exit functions */
4832
4833int nl80211_init(void)
4834{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004835 int err;
Johannes Berg55682962007-09-20 13:09:35 -04004836
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00004837 err = genl_register_family_with_ops(&nl80211_fam,
4838 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04004839 if (err)
4840 return err;
4841
Johannes Berg55682962007-09-20 13:09:35 -04004842 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
4843 if (err)
4844 goto err_out;
4845
Johannes Berg2a519312009-02-10 21:25:55 +01004846 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
4847 if (err)
4848 goto err_out;
4849
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004850 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
4851 if (err)
4852 goto err_out;
4853
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004854 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
4855 if (err)
4856 goto err_out;
4857
Johannes Bergaff89a92009-07-01 21:26:51 +02004858#ifdef CONFIG_NL80211_TESTMODE
4859 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
4860 if (err)
4861 goto err_out;
4862#endif
4863
Johannes Berg55682962007-09-20 13:09:35 -04004864 return 0;
4865 err_out:
4866 genl_unregister_family(&nl80211_fam);
4867 return err;
4868}
4869
4870void nl80211_exit(void)
4871{
4872 genl_unregister_family(&nl80211_fam);
4873}