blob: 95149f3034091c327844c1920ff8d45807278481 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020017#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include <net/genetlink.h>
19#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020020#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040021#include "core.h"
22#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040024
25/* the netlink family */
26static struct genl_family nl80211_fam = {
27 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
28 .name = "nl80211", /* have users key off the name instead */
29 .hdrsize = 0, /* no private header */
30 .version = 1, /* no particular meaning now */
31 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020032 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040033};
34
Johannes Berg79c97e92009-07-07 03:56:12 +020035/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020036static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020037 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040038 struct net_device **dev)
39{
Johannes Berg463d0182009-07-14 00:33:35 +020040 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040041 int ifindex;
42
Johannes Bergbba95fe2008-07-29 13:22:51 +020043 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040044 return -EINVAL;
45
Johannes Bergbba95fe2008-07-29 13:22:51 +020046 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020047 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040048 if (!*dev)
49 return -ENODEV;
50
Johannes Berg463d0182009-07-14 00:33:35 +020051 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020052 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040053 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020054 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040055 }
56
57 return 0;
58}
59
60/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000061static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040062 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
63 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070064 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020065 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020066 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053067 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020068 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
69 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010072 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040073
74 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010077
78 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020079 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010080
Johannes Bergb9454e82009-07-08 13:29:08 +020081 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010082 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
83 .len = WLAN_MAX_KEY_LEN },
84 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
85 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
86 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030087 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010088
89 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
90 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
91 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_DATA_LEN },
93 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
94 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010095 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
96 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
97 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
98 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
99 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100100 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100101 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200102 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100103 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
104 .len = IEEE80211_MAX_MESH_ID_LEN },
105 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300106
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700107 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
108 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
109
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300110 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200113 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300115
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700116 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
117
Jouni Malinen36aedc92008-08-25 11:58:58 +0300118 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
119 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200120
121 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
122 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
123 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100124 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
125 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200126
127 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
128 .len = IEEE80211_MAX_SSID_LEN },
129 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
130 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200131 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300132 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300133 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300134 [NL80211_ATTR_STA_FLAGS2] = {
135 .len = sizeof(struct nl80211_sta_flag_update),
136 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300137 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200138 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
139 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
140 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200141 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100142 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100143 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
144 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100145 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
146 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200147 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200148 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
149 .len = IEEE80211_MAX_DATA_LEN },
150 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200151 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200152 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Johannes Berg55682962007-09-20 13:09:35 -0400153};
154
Johannes Bergb9454e82009-07-08 13:29:08 +0200155/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000156static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200157 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200158 [NL80211_KEY_IDX] = { .type = NLA_U8 },
159 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
160 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
161 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
162 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
163};
164
Holger Schuriga0438972009-11-11 11:30:02 +0100165/* ifidx get helper */
166static int nl80211_get_ifidx(struct netlink_callback *cb)
167{
168 int res;
169
170 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
171 nl80211_fam.attrbuf, nl80211_fam.maxattr,
172 nl80211_policy);
173 if (res)
174 return res;
175
176 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
177 return -EINVAL;
178
179 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
180 if (!res)
181 return -EINVAL;
182 return res;
183}
184
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100185/* IE validation */
186static bool is_valid_ie_attr(const struct nlattr *attr)
187{
188 const u8 *pos;
189 int len;
190
191 if (!attr)
192 return true;
193
194 pos = nla_data(attr);
195 len = nla_len(attr);
196
197 while (len) {
198 u8 elemlen;
199
200 if (len < 2)
201 return false;
202 len -= 2;
203
204 elemlen = pos[1];
205 if (elemlen > len)
206 return false;
207
208 len -= elemlen;
209 pos += 2 + elemlen;
210 }
211
212 return true;
213}
214
Johannes Berg55682962007-09-20 13:09:35 -0400215/* message building helper */
216static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
217 int flags, u8 cmd)
218{
219 /* since there is no private header just add the generic one */
220 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
221}
222
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400223static int nl80211_msg_put_channel(struct sk_buff *msg,
224 struct ieee80211_channel *chan)
225{
226 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
227 chan->center_freq);
228
229 if (chan->flags & IEEE80211_CHAN_DISABLED)
230 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
231 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
232 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
233 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
234 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
235 if (chan->flags & IEEE80211_CHAN_RADAR)
236 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
237
238 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
239 DBM_TO_MBM(chan->max_power));
240
241 return 0;
242
243 nla_put_failure:
244 return -ENOBUFS;
245}
246
Johannes Berg55682962007-09-20 13:09:35 -0400247/* netlink command implementations */
248
Johannes Bergb9454e82009-07-08 13:29:08 +0200249struct key_parse {
250 struct key_params p;
251 int idx;
252 bool def, defmgmt;
253};
254
255static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
256{
257 struct nlattr *tb[NL80211_KEY_MAX + 1];
258 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
259 nl80211_key_policy);
260 if (err)
261 return err;
262
263 k->def = !!tb[NL80211_KEY_DEFAULT];
264 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
265
266 if (tb[NL80211_KEY_IDX])
267 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
268
269 if (tb[NL80211_KEY_DATA]) {
270 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
271 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
272 }
273
274 if (tb[NL80211_KEY_SEQ]) {
275 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
276 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
277 }
278
279 if (tb[NL80211_KEY_CIPHER])
280 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
281
282 return 0;
283}
284
285static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
286{
287 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
288 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
289 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
290 }
291
292 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
293 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
294 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
295 }
296
297 if (info->attrs[NL80211_ATTR_KEY_IDX])
298 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
299
300 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
301 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
302
303 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
304 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
305
306 return 0;
307}
308
309static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
310{
311 int err;
312
313 memset(k, 0, sizeof(*k));
314 k->idx = -1;
315
316 if (info->attrs[NL80211_ATTR_KEY])
317 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
318 else
319 err = nl80211_parse_key_old(info, k);
320
321 if (err)
322 return err;
323
324 if (k->def && k->defmgmt)
325 return -EINVAL;
326
327 if (k->idx != -1) {
328 if (k->defmgmt) {
329 if (k->idx < 4 || k->idx > 5)
330 return -EINVAL;
331 } else if (k->def) {
332 if (k->idx < 0 || k->idx > 3)
333 return -EINVAL;
334 } else {
335 if (k->idx < 0 || k->idx > 5)
336 return -EINVAL;
337 }
338 }
339
340 return 0;
341}
342
Johannes Bergfffd0932009-07-08 14:22:54 +0200343static struct cfg80211_cached_keys *
344nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
345 struct nlattr *keys)
346{
347 struct key_parse parse;
348 struct nlattr *key;
349 struct cfg80211_cached_keys *result;
350 int rem, err, def = 0;
351
352 result = kzalloc(sizeof(*result), GFP_KERNEL);
353 if (!result)
354 return ERR_PTR(-ENOMEM);
355
356 result->def = -1;
357 result->defmgmt = -1;
358
359 nla_for_each_nested(key, keys, rem) {
360 memset(&parse, 0, sizeof(parse));
361 parse.idx = -1;
362
363 err = nl80211_parse_key_new(key, &parse);
364 if (err)
365 goto error;
366 err = -EINVAL;
367 if (!parse.p.key)
368 goto error;
369 if (parse.idx < 0 || parse.idx > 4)
370 goto error;
371 if (parse.def) {
372 if (def)
373 goto error;
374 def = 1;
375 result->def = parse.idx;
376 } else if (parse.defmgmt)
377 goto error;
378 err = cfg80211_validate_key_settings(rdev, &parse.p,
379 parse.idx, NULL);
380 if (err)
381 goto error;
382 result->params[parse.idx].cipher = parse.p.cipher;
383 result->params[parse.idx].key_len = parse.p.key_len;
384 result->params[parse.idx].key = result->data[parse.idx];
385 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
386 }
387
388 return result;
389 error:
390 kfree(result);
391 return ERR_PTR(err);
392}
393
394static int nl80211_key_allowed(struct wireless_dev *wdev)
395{
396 ASSERT_WDEV_LOCK(wdev);
397
398 if (!netif_running(wdev->netdev))
399 return -ENETDOWN;
400
401 switch (wdev->iftype) {
402 case NL80211_IFTYPE_AP:
403 case NL80211_IFTYPE_AP_VLAN:
404 break;
405 case NL80211_IFTYPE_ADHOC:
406 if (!wdev->current_bss)
407 return -ENOLINK;
408 break;
409 case NL80211_IFTYPE_STATION:
410 if (wdev->sme_state != CFG80211_SME_CONNECTED)
411 return -ENOLINK;
412 break;
413 default:
414 return -EINVAL;
415 }
416
417 return 0;
418}
419
Johannes Berg55682962007-09-20 13:09:35 -0400420static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
421 struct cfg80211_registered_device *dev)
422{
423 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100424 struct nlattr *nl_bands, *nl_band;
425 struct nlattr *nl_freqs, *nl_freq;
426 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700427 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100428 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100429 enum ieee80211_band band;
430 struct ieee80211_channel *chan;
431 struct ieee80211_rate *rate;
432 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700433 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400434
435 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
436 if (!hdr)
437 return -1;
438
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500439 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400440 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200441
Johannes Bergf5ea9122009-08-07 16:17:38 +0200442 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
443 cfg80211_rdev_list_generation);
444
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200445 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
446 dev->wiphy.retry_short);
447 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
448 dev->wiphy.retry_long);
449 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
450 dev->wiphy.frag_threshold);
451 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
452 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100453 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
454 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200455
Johannes Berg2a519312009-02-10 21:25:55 +0100456 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
457 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200458 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
459 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100460
Johannes Berg25e47c182009-04-02 20:14:06 +0200461 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
462 sizeof(u32) * dev->wiphy.n_cipher_suites,
463 dev->wiphy.cipher_suites);
464
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100465 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
466 dev->wiphy.max_num_pmkids);
467
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700468 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
469 if (!nl_modes)
470 goto nla_put_failure;
471
472 i = 0;
473 while (ifmodes) {
474 if (ifmodes & 1)
475 NLA_PUT_FLAG(msg, i);
476 ifmodes >>= 1;
477 i++;
478 }
479
480 nla_nest_end(msg, nl_modes);
481
Johannes Bergee688b002008-01-24 19:38:39 +0100482 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
483 if (!nl_bands)
484 goto nla_put_failure;
485
486 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
487 if (!dev->wiphy.bands[band])
488 continue;
489
490 nl_band = nla_nest_start(msg, band);
491 if (!nl_band)
492 goto nla_put_failure;
493
Johannes Bergd51626d2008-10-09 12:20:13 +0200494 /* add HT info */
495 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
496 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
497 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
498 &dev->wiphy.bands[band]->ht_cap.mcs);
499 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
500 dev->wiphy.bands[band]->ht_cap.cap);
501 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
502 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
503 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
504 dev->wiphy.bands[band]->ht_cap.ampdu_density);
505 }
506
Johannes Bergee688b002008-01-24 19:38:39 +0100507 /* add frequencies */
508 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
509 if (!nl_freqs)
510 goto nla_put_failure;
511
512 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
513 nl_freq = nla_nest_start(msg, i);
514 if (!nl_freq)
515 goto nla_put_failure;
516
517 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100518
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400519 if (nl80211_msg_put_channel(msg, chan))
520 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200521
Johannes Bergee688b002008-01-24 19:38:39 +0100522 nla_nest_end(msg, nl_freq);
523 }
524
525 nla_nest_end(msg, nl_freqs);
526
527 /* add bitrates */
528 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
529 if (!nl_rates)
530 goto nla_put_failure;
531
532 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
533 nl_rate = nla_nest_start(msg, i);
534 if (!nl_rate)
535 goto nla_put_failure;
536
537 rate = &dev->wiphy.bands[band]->bitrates[i];
538 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
539 rate->bitrate);
540 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
541 NLA_PUT_FLAG(msg,
542 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
543
544 nla_nest_end(msg, nl_rate);
545 }
546
547 nla_nest_end(msg, nl_rates);
548
549 nla_nest_end(msg, nl_band);
550 }
551 nla_nest_end(msg, nl_bands);
552
Johannes Berg8fdc6212009-03-14 09:34:01 +0100553 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
554 if (!nl_cmds)
555 goto nla_put_failure;
556
557 i = 0;
558#define CMD(op, n) \
559 do { \
560 if (dev->ops->op) { \
561 i++; \
562 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
563 } \
564 } while (0)
565
566 CMD(add_virtual_intf, NEW_INTERFACE);
567 CMD(change_virtual_intf, SET_INTERFACE);
568 CMD(add_key, NEW_KEY);
569 CMD(add_beacon, NEW_BEACON);
570 CMD(add_station, NEW_STATION);
571 CMD(add_mpath, NEW_MPATH);
572 CMD(set_mesh_params, SET_MESH_PARAMS);
573 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200574 CMD(auth, AUTHENTICATE);
575 CMD(assoc, ASSOCIATE);
576 CMD(deauth, DEAUTHENTICATE);
577 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200578 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100579 CMD(set_pmksa, SET_PMKSA);
580 CMD(del_pmksa, DEL_PMKSA);
581 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100582 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200583 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Jouni Malinen026331c2010-02-15 12:53:10 +0200584 CMD(action, ACTION);
Johannes Berg5be83de2009-11-19 00:56:28 +0100585 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200586 i++;
587 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
588 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100589
590#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200591
Johannes Berg6829c8782009-07-02 09:13:27 +0200592 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200593 i++;
594 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
595 }
596
Johannes Berg6829c8782009-07-02 09:13:27 +0200597 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200598 i++;
599 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
600 }
601
Johannes Berg8fdc6212009-03-14 09:34:01 +0100602 nla_nest_end(msg, nl_cmds);
603
Johannes Berg55682962007-09-20 13:09:35 -0400604 return genlmsg_end(msg, hdr);
605
606 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700607 genlmsg_cancel(msg, hdr);
608 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400609}
610
611static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
612{
613 int idx = 0;
614 int start = cb->args[0];
615 struct cfg80211_registered_device *dev;
616
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500617 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200618 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200619 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
620 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200621 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400622 continue;
623 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
624 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200625 dev) < 0) {
626 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400627 break;
Julius Volzb4637272008-07-08 14:02:19 +0200628 }
Johannes Berg55682962007-09-20 13:09:35 -0400629 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500630 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400631
632 cb->args[0] = idx;
633
634 return skb->len;
635}
636
637static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
638{
639 struct sk_buff *msg;
640 struct cfg80211_registered_device *dev;
641
642 dev = cfg80211_get_dev_from_info(info);
643 if (IS_ERR(dev))
644 return PTR_ERR(dev);
645
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400647 if (!msg)
648 goto out_err;
649
650 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
651 goto out_free;
652
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200653 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400654
Johannes Berg134e6372009-07-10 09:51:34 +0000655 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400656
657 out_free:
658 nlmsg_free(msg);
659 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200660 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400661 return -ENOBUFS;
662}
663
Jouni Malinen31888482008-10-30 16:59:24 +0200664static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
665 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
666 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
667 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
668 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
669 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
670};
671
672static int parse_txq_params(struct nlattr *tb[],
673 struct ieee80211_txq_params *txq_params)
674{
675 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
676 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
677 !tb[NL80211_TXQ_ATTR_AIFS])
678 return -EINVAL;
679
680 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
681 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
682 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
683 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
684 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
685
686 return 0;
687}
688
Johannes Berg55682962007-09-20 13:09:35 -0400689static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
690{
691 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200692 int result = 0, rem_txq_params = 0;
693 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200694 u32 changed;
695 u8 retry_short = 0, retry_long = 0;
696 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100697 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400698
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100699 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400700
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100701 mutex_lock(&cfg80211_mutex);
702
Johannes Berg79c97e92009-07-07 03:56:12 +0200703 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100704 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200705 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100706 result = PTR_ERR(rdev);
707 goto unlock;
708 }
709
710 mutex_lock(&rdev->mtx);
711
712 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200713 result = cfg80211_dev_rename(
714 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100715
716 mutex_unlock(&cfg80211_mutex);
717
718 if (result)
719 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400720
Jouni Malinen31888482008-10-30 16:59:24 +0200721 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
722 struct ieee80211_txq_params txq_params;
723 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
724
725 if (!rdev->ops->set_txq_params) {
726 result = -EOPNOTSUPP;
727 goto bad_res;
728 }
729
730 nla_for_each_nested(nl_txq_params,
731 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
732 rem_txq_params) {
733 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
734 nla_data(nl_txq_params),
735 nla_len(nl_txq_params),
736 txq_params_policy);
737 result = parse_txq_params(tb, &txq_params);
738 if (result)
739 goto bad_res;
740
741 result = rdev->ops->set_txq_params(&rdev->wiphy,
742 &txq_params);
743 if (result)
744 goto bad_res;
745 }
746 }
747
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200748 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530749 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400750 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200751
Johannes Berg306d6112008-12-08 12:39:04 +0100752 result = -EINVAL;
753
Sujith094d05d2008-12-12 11:57:43 +0530754 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
755 channel_type = nla_get_u32(info->attrs[
756 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
757 if (channel_type != NL80211_CHAN_NO_HT &&
758 channel_type != NL80211_CHAN_HT20 &&
759 channel_type != NL80211_CHAN_HT40PLUS &&
760 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200761 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200762 }
763
764 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100765
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200766 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200767 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200768 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200769 if (result)
770 goto bad_res;
771 }
772
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200773 changed = 0;
774
775 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
776 retry_short = nla_get_u8(
777 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
778 if (retry_short == 0) {
779 result = -EINVAL;
780 goto bad_res;
781 }
782 changed |= WIPHY_PARAM_RETRY_SHORT;
783 }
784
785 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
786 retry_long = nla_get_u8(
787 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
788 if (retry_long == 0) {
789 result = -EINVAL;
790 goto bad_res;
791 }
792 changed |= WIPHY_PARAM_RETRY_LONG;
793 }
794
795 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
796 frag_threshold = nla_get_u32(
797 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
798 if (frag_threshold < 256) {
799 result = -EINVAL;
800 goto bad_res;
801 }
802 if (frag_threshold != (u32) -1) {
803 /*
804 * Fragments (apart from the last one) are required to
805 * have even length. Make the fragmentation code
806 * simpler by stripping LSB should someone try to use
807 * odd threshold value.
808 */
809 frag_threshold &= ~0x1;
810 }
811 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
812 }
813
814 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
815 rts_threshold = nla_get_u32(
816 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
817 changed |= WIPHY_PARAM_RTS_THRESHOLD;
818 }
819
Lukáš Turek81077e82009-12-21 22:50:47 +0100820 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
821 coverage_class = nla_get_u8(
822 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
823 changed |= WIPHY_PARAM_COVERAGE_CLASS;
824 }
825
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200826 if (changed) {
827 u8 old_retry_short, old_retry_long;
828 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100829 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200830
831 if (!rdev->ops->set_wiphy_params) {
832 result = -EOPNOTSUPP;
833 goto bad_res;
834 }
835
836 old_retry_short = rdev->wiphy.retry_short;
837 old_retry_long = rdev->wiphy.retry_long;
838 old_frag_threshold = rdev->wiphy.frag_threshold;
839 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100840 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200841
842 if (changed & WIPHY_PARAM_RETRY_SHORT)
843 rdev->wiphy.retry_short = retry_short;
844 if (changed & WIPHY_PARAM_RETRY_LONG)
845 rdev->wiphy.retry_long = retry_long;
846 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
847 rdev->wiphy.frag_threshold = frag_threshold;
848 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
849 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100850 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
851 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200852
853 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
854 if (result) {
855 rdev->wiphy.retry_short = old_retry_short;
856 rdev->wiphy.retry_long = old_retry_long;
857 rdev->wiphy.frag_threshold = old_frag_threshold;
858 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100859 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200860 }
861 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200862
Johannes Berg306d6112008-12-08 12:39:04 +0100863 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100864 mutex_unlock(&rdev->mtx);
865 unlock:
866 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400867 return result;
868}
869
870
871static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200872 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400873 struct net_device *dev)
874{
875 void *hdr;
876
877 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
878 if (!hdr)
879 return -1;
880
881 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200882 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400883 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200884 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200885
886 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
887 rdev->devlist_generation ^
888 (cfg80211_rdev_list_generation << 2));
889
Johannes Berg55682962007-09-20 13:09:35 -0400890 return genlmsg_end(msg, hdr);
891
892 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700893 genlmsg_cancel(msg, hdr);
894 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400895}
896
897static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
898{
899 int wp_idx = 0;
900 int if_idx = 0;
901 int wp_start = cb->args[0];
902 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200903 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400904 struct wireless_dev *wdev;
905
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500906 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200907 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
908 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200909 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200910 if (wp_idx < wp_start) {
911 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400912 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200913 }
Johannes Berg55682962007-09-20 13:09:35 -0400914 if_idx = 0;
915
Johannes Bergf5ea9122009-08-07 16:17:38 +0200916 mutex_lock(&rdev->devlist_mtx);
917 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200918 if (if_idx < if_start) {
919 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400920 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200921 }
Johannes Berg55682962007-09-20 13:09:35 -0400922 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
923 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200924 rdev, wdev->netdev) < 0) {
925 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200926 goto out;
927 }
928 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400929 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200930 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200931
932 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400933 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200934 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500935 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400936
937 cb->args[0] = wp_idx;
938 cb->args[1] = if_idx;
939
940 return skb->len;
941}
942
943static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
944{
945 struct sk_buff *msg;
946 struct cfg80211_registered_device *dev;
947 struct net_device *netdev;
948 int err;
949
Johannes Berg463d0182009-07-14 00:33:35 +0200950 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400951 if (err)
952 return err;
953
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700954 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400955 if (!msg)
956 goto out_err;
957
Johannes Bergd7264052009-04-19 16:23:20 +0200958 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
959 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400960 goto out_free;
961
962 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200963 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400964
Johannes Berg134e6372009-07-10 09:51:34 +0000965 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400966
967 out_free:
968 nlmsg_free(msg);
969 out_err:
970 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200971 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400972 return -ENOBUFS;
973}
974
Michael Wu66f7ac52008-01-31 19:48:22 +0100975static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
976 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
977 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
978 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
979 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
980 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
981};
982
983static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
984{
985 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
986 int flag;
987
988 *mntrflags = 0;
989
990 if (!nla)
991 return -EINVAL;
992
993 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
994 nla, mntr_flags_policy))
995 return -EINVAL;
996
997 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
998 if (flags[flag])
999 *mntrflags |= (1<<flag);
1000
1001 return 0;
1002}
1003
Johannes Berg9bc383d2009-11-19 11:55:19 +01001004static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001005 struct net_device *netdev, u8 use_4addr,
1006 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001007{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001008 if (!use_4addr) {
1009 if (netdev && netdev->br_port)
1010 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001011 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001012 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001013
1014 switch (iftype) {
1015 case NL80211_IFTYPE_AP_VLAN:
1016 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1017 return 0;
1018 break;
1019 case NL80211_IFTYPE_STATION:
1020 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1021 return 0;
1022 break;
1023 default:
1024 break;
1025 }
1026
1027 return -EOPNOTSUPP;
1028}
1029
Johannes Berg55682962007-09-20 13:09:35 -04001030static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1031{
Johannes Berg79c97e92009-07-07 03:56:12 +02001032 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001033 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001034 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001035 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001036 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001037 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001038 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001039
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001040 memset(&params, 0, sizeof(params));
1041
Johannes Berg3b858752009-03-12 09:55:09 +01001042 rtnl_lock();
1043
Johannes Berg463d0182009-07-14 00:33:35 +02001044 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001045 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001046 goto unlock_rtnl;
1047
Johannes Berg04a773a2009-04-19 21:24:32 +02001048 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001049
Johannes Berg723b0382008-09-16 20:22:09 +02001050 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001051 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001052 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001053 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001054 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001055 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001056 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001057 }
Johannes Berg723b0382008-09-16 20:22:09 +02001058 }
1059
Johannes Berg92ffe052008-09-16 20:39:36 +02001060 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001061 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001062 err = -EINVAL;
1063 goto unlock;
1064 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001065 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1066 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001067 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001068 }
1069
Felix Fietkau8b787642009-11-10 18:53:10 +01001070 if (info->attrs[NL80211_ATTR_4ADDR]) {
1071 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1072 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001073 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001074 if (err)
1075 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001076 } else {
1077 params.use_4addr = -1;
1078 }
1079
Johannes Berg92ffe052008-09-16 20:39:36 +02001080 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001081 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001082 err = -EINVAL;
1083 goto unlock;
1084 }
1085 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1086 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001087 if (err)
1088 goto unlock;
1089
1090 flags = &_flags;
1091 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001092 }
Johannes Berg3b858752009-03-12 09:55:09 +01001093
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001094 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001095 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001096 else
1097 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001098
Johannes Berg9bc383d2009-11-19 11:55:19 +01001099 if (!err && params.use_4addr != -1)
1100 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1101
Johannes Berg55682962007-09-20 13:09:35 -04001102 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001103 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001104 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001105 unlock_rtnl:
1106 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001107 return err;
1108}
1109
1110static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1111{
Johannes Berg79c97e92009-07-07 03:56:12 +02001112 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001113 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001114 int err;
1115 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001116 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001117
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001118 memset(&params, 0, sizeof(params));
1119
Johannes Berg55682962007-09-20 13:09:35 -04001120 if (!info->attrs[NL80211_ATTR_IFNAME])
1121 return -EINVAL;
1122
1123 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1124 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1125 if (type > NL80211_IFTYPE_MAX)
1126 return -EINVAL;
1127 }
1128
Johannes Berg3b858752009-03-12 09:55:09 +01001129 rtnl_lock();
1130
Johannes Berg79c97e92009-07-07 03:56:12 +02001131 rdev = cfg80211_get_dev_from_info(info);
1132 if (IS_ERR(rdev)) {
1133 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001134 goto unlock_rtnl;
1135 }
Johannes Berg55682962007-09-20 13:09:35 -04001136
Johannes Berg79c97e92009-07-07 03:56:12 +02001137 if (!rdev->ops->add_virtual_intf ||
1138 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001139 err = -EOPNOTSUPP;
1140 goto unlock;
1141 }
1142
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001143 if (type == NL80211_IFTYPE_MESH_POINT &&
1144 info->attrs[NL80211_ATTR_MESH_ID]) {
1145 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1146 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1147 }
1148
Johannes Berg9bc383d2009-11-19 11:55:19 +01001149 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001150 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001151 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001152 if (err)
1153 goto unlock;
1154 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001155
Michael Wu66f7ac52008-01-31 19:48:22 +01001156 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1157 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1158 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001159 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001160 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001161 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001162
Johannes Berg55682962007-09-20 13:09:35 -04001163 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001164 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001165 unlock_rtnl:
1166 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001167 return err;
1168}
1169
1170static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1171{
Johannes Berg79c97e92009-07-07 03:56:12 +02001172 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001173 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001174 struct net_device *dev;
1175
Johannes Berg3b858752009-03-12 09:55:09 +01001176 rtnl_lock();
1177
Johannes Berg463d0182009-07-14 00:33:35 +02001178 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001179 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001180 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001181
Johannes Berg79c97e92009-07-07 03:56:12 +02001182 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001183 err = -EOPNOTSUPP;
1184 goto out;
1185 }
1186
Johannes Berg463d0182009-07-14 00:33:35 +02001187 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001188
1189 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001190 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001191 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001192 unlock_rtnl:
1193 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001194 return err;
1195}
1196
Johannes Berg41ade002007-12-19 02:03:29 +01001197struct get_key_cookie {
1198 struct sk_buff *msg;
1199 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001200 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001201};
1202
1203static void get_key_callback(void *c, struct key_params *params)
1204{
Johannes Bergb9454e82009-07-08 13:29:08 +02001205 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001206 struct get_key_cookie *cookie = c;
1207
1208 if (params->key)
1209 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1210 params->key_len, params->key);
1211
1212 if (params->seq)
1213 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1214 params->seq_len, params->seq);
1215
1216 if (params->cipher)
1217 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1218 params->cipher);
1219
Johannes Bergb9454e82009-07-08 13:29:08 +02001220 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1221 if (!key)
1222 goto nla_put_failure;
1223
1224 if (params->key)
1225 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1226 params->key_len, params->key);
1227
1228 if (params->seq)
1229 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1230 params->seq_len, params->seq);
1231
1232 if (params->cipher)
1233 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1234 params->cipher);
1235
1236 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1237
1238 nla_nest_end(cookie->msg, key);
1239
Johannes Berg41ade002007-12-19 02:03:29 +01001240 return;
1241 nla_put_failure:
1242 cookie->error = 1;
1243}
1244
1245static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1246{
Johannes Berg79c97e92009-07-07 03:56:12 +02001247 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001248 int err;
1249 struct net_device *dev;
1250 u8 key_idx = 0;
1251 u8 *mac_addr = NULL;
1252 struct get_key_cookie cookie = {
1253 .error = 0,
1254 };
1255 void *hdr;
1256 struct sk_buff *msg;
1257
1258 if (info->attrs[NL80211_ATTR_KEY_IDX])
1259 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1260
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001261 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001262 return -EINVAL;
1263
1264 if (info->attrs[NL80211_ATTR_MAC])
1265 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1266
Johannes Berg3b858752009-03-12 09:55:09 +01001267 rtnl_lock();
1268
Johannes Berg463d0182009-07-14 00:33:35 +02001269 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001270 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001271 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001272
Johannes Berg79c97e92009-07-07 03:56:12 +02001273 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001274 err = -EOPNOTSUPP;
1275 goto out;
1276 }
1277
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001278 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001279 if (!msg) {
1280 err = -ENOMEM;
1281 goto out;
1282 }
1283
1284 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1285 NL80211_CMD_NEW_KEY);
1286
1287 if (IS_ERR(hdr)) {
1288 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001289 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001290 }
1291
1292 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001293 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001294
1295 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1296 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1297 if (mac_addr)
1298 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1299
Johannes Berg79c97e92009-07-07 03:56:12 +02001300 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001301 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001302
1303 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001304 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001305
1306 if (cookie.error)
1307 goto nla_put_failure;
1308
1309 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001310 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001311 goto out;
1312
1313 nla_put_failure:
1314 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001315 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001316 nlmsg_free(msg);
1317 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001318 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001319 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001320 unlock_rtnl:
1321 rtnl_unlock();
1322
Johannes Berg41ade002007-12-19 02:03:29 +01001323 return err;
1324}
1325
1326static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1327{
Johannes Berg79c97e92009-07-07 03:56:12 +02001328 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001329 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001330 int err;
1331 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001332 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1333 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001334
Johannes Bergb9454e82009-07-08 13:29:08 +02001335 err = nl80211_parse_key(info, &key);
1336 if (err)
1337 return err;
1338
1339 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001340 return -EINVAL;
1341
Johannes Bergb9454e82009-07-08 13:29:08 +02001342 /* only support setting default key */
1343 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001344 return -EINVAL;
1345
Johannes Berg3b858752009-03-12 09:55:09 +01001346 rtnl_lock();
1347
Johannes Berg463d0182009-07-14 00:33:35 +02001348 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001349 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001350 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001351
Johannes Bergb9454e82009-07-08 13:29:08 +02001352 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001353 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001354 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001355 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001356
1357 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001358 err = -EOPNOTSUPP;
1359 goto out;
1360 }
1361
Johannes Bergfffd0932009-07-08 14:22:54 +02001362 wdev_lock(dev->ieee80211_ptr);
1363 err = nl80211_key_allowed(dev->ieee80211_ptr);
1364 if (!err)
1365 err = func(&rdev->wiphy, dev, key.idx);
1366
Johannes Berg3d23e342009-09-29 23:27:28 +02001367#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001368 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001369 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001370 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001371 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001372 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001373 }
1374#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001375 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001376
1377 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001378 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001379 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001380
1381 unlock_rtnl:
1382 rtnl_unlock();
1383
Johannes Berg41ade002007-12-19 02:03:29 +01001384 return err;
1385}
1386
1387static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1388{
Johannes Berg79c97e92009-07-07 03:56:12 +02001389 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001390 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001391 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001392 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001393 u8 *mac_addr = NULL;
1394
Johannes Bergb9454e82009-07-08 13:29:08 +02001395 err = nl80211_parse_key(info, &key);
1396 if (err)
1397 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001398
Johannes Bergb9454e82009-07-08 13:29:08 +02001399 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001400 return -EINVAL;
1401
Johannes Berg41ade002007-12-19 02:03:29 +01001402 if (info->attrs[NL80211_ATTR_MAC])
1403 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1404
Johannes Berg3b858752009-03-12 09:55:09 +01001405 rtnl_lock();
1406
Johannes Berg463d0182009-07-14 00:33:35 +02001407 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001408 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001409 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001410
Johannes Berg79c97e92009-07-07 03:56:12 +02001411 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001412 err = -EOPNOTSUPP;
1413 goto out;
1414 }
1415
Johannes Bergfffd0932009-07-08 14:22:54 +02001416 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1417 err = -EINVAL;
1418 goto out;
1419 }
1420
1421 wdev_lock(dev->ieee80211_ptr);
1422 err = nl80211_key_allowed(dev->ieee80211_ptr);
1423 if (!err)
1424 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1425 mac_addr, &key.p);
1426 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001427
1428 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001429 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001430 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001431 unlock_rtnl:
1432 rtnl_unlock();
1433
Johannes Berg41ade002007-12-19 02:03:29 +01001434 return err;
1435}
1436
1437static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1438{
Johannes Berg79c97e92009-07-07 03:56:12 +02001439 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001440 int err;
1441 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001442 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001443 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001444
Johannes Bergb9454e82009-07-08 13:29:08 +02001445 err = nl80211_parse_key(info, &key);
1446 if (err)
1447 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001448
1449 if (info->attrs[NL80211_ATTR_MAC])
1450 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1451
Johannes Berg3b858752009-03-12 09:55:09 +01001452 rtnl_lock();
1453
Johannes Berg463d0182009-07-14 00:33:35 +02001454 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001455 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001456 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001457
Johannes Berg79c97e92009-07-07 03:56:12 +02001458 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001459 err = -EOPNOTSUPP;
1460 goto out;
1461 }
1462
Johannes Bergfffd0932009-07-08 14:22:54 +02001463 wdev_lock(dev->ieee80211_ptr);
1464 err = nl80211_key_allowed(dev->ieee80211_ptr);
1465 if (!err)
1466 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001467
Johannes Berg3d23e342009-09-29 23:27:28 +02001468#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001469 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001470 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001471 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001472 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001473 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1474 }
1475#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001476 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001477
Johannes Berg41ade002007-12-19 02:03:29 +01001478 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001479 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001480 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001481
1482 unlock_rtnl:
1483 rtnl_unlock();
1484
Johannes Berg41ade002007-12-19 02:03:29 +01001485 return err;
1486}
1487
Johannes Berged1b6cc2007-12-19 02:03:32 +01001488static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1489{
1490 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1491 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001492 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001493 int err;
1494 struct net_device *dev;
1495 struct beacon_parameters params;
1496 int haveinfo = 0;
1497
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001498 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1499 return -EINVAL;
1500
Johannes Berg3b858752009-03-12 09:55:09 +01001501 rtnl_lock();
1502
Johannes Berg463d0182009-07-14 00:33:35 +02001503 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001504 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001505 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001506
Jouni Malineneec60b02009-03-20 21:21:19 +02001507 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1508 err = -EOPNOTSUPP;
1509 goto out;
1510 }
1511
Johannes Berged1b6cc2007-12-19 02:03:32 +01001512 switch (info->genlhdr->cmd) {
1513 case NL80211_CMD_NEW_BEACON:
1514 /* these are required for NEW_BEACON */
1515 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1516 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1517 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1518 err = -EINVAL;
1519 goto out;
1520 }
1521
Johannes Berg79c97e92009-07-07 03:56:12 +02001522 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001523 break;
1524 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001525 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001526 break;
1527 default:
1528 WARN_ON(1);
1529 err = -EOPNOTSUPP;
1530 goto out;
1531 }
1532
1533 if (!call) {
1534 err = -EOPNOTSUPP;
1535 goto out;
1536 }
1537
1538 memset(&params, 0, sizeof(params));
1539
1540 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1541 params.interval =
1542 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1543 haveinfo = 1;
1544 }
1545
1546 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1547 params.dtim_period =
1548 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1549 haveinfo = 1;
1550 }
1551
1552 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1553 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1554 params.head_len =
1555 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1556 haveinfo = 1;
1557 }
1558
1559 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1560 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1561 params.tail_len =
1562 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1563 haveinfo = 1;
1564 }
1565
1566 if (!haveinfo) {
1567 err = -EINVAL;
1568 goto out;
1569 }
1570
Johannes Berg79c97e92009-07-07 03:56:12 +02001571 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001572
1573 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001574 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001575 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001576 unlock_rtnl:
1577 rtnl_unlock();
1578
Johannes Berged1b6cc2007-12-19 02:03:32 +01001579 return err;
1580}
1581
1582static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1583{
Johannes Berg79c97e92009-07-07 03:56:12 +02001584 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001585 int err;
1586 struct net_device *dev;
1587
Johannes Berg3b858752009-03-12 09:55:09 +01001588 rtnl_lock();
1589
Johannes Berg463d0182009-07-14 00:33:35 +02001590 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001591 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001592 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001593
Johannes Berg79c97e92009-07-07 03:56:12 +02001594 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001595 err = -EOPNOTSUPP;
1596 goto out;
1597 }
1598
Jouni Malineneec60b02009-03-20 21:21:19 +02001599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1600 err = -EOPNOTSUPP;
1601 goto out;
1602 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001603 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001604
1605 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001606 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001607 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001608 unlock_rtnl:
1609 rtnl_unlock();
1610
Johannes Berged1b6cc2007-12-19 02:03:32 +01001611 return err;
1612}
1613
Johannes Berg5727ef12007-12-19 02:03:34 +01001614static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1615 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1616 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1617 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001618 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001619};
1620
Johannes Bergeccb8e82009-05-11 21:57:56 +03001621static int parse_station_flags(struct genl_info *info,
1622 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001623{
1624 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001625 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001626 int flag;
1627
Johannes Bergeccb8e82009-05-11 21:57:56 +03001628 /*
1629 * Try parsing the new attribute first so userspace
1630 * can specify both for older kernels.
1631 */
1632 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1633 if (nla) {
1634 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001635
Johannes Bergeccb8e82009-05-11 21:57:56 +03001636 sta_flags = nla_data(nla);
1637 params->sta_flags_mask = sta_flags->mask;
1638 params->sta_flags_set = sta_flags->set;
1639 if ((params->sta_flags_mask |
1640 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1641 return -EINVAL;
1642 return 0;
1643 }
1644
1645 /* if present, parse the old attribute */
1646
1647 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001648 if (!nla)
1649 return 0;
1650
1651 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1652 nla, sta_flags_policy))
1653 return -EINVAL;
1654
Johannes Bergeccb8e82009-05-11 21:57:56 +03001655 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1656 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001657
1658 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1659 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001660 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001661
1662 return 0;
1663}
1664
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001665static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1666 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001667 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001668{
1669 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001670 struct nlattr *sinfoattr, *txrate;
1671 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001672
1673 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1674 if (!hdr)
1675 return -1;
1676
1677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1678 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1679
Johannes Bergf5ea9122009-08-07 16:17:38 +02001680 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1681
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001682 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1683 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001684 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001685 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1686 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1687 sinfo->inactive_time);
1688 if (sinfo->filled & STATION_INFO_RX_BYTES)
1689 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1690 sinfo->rx_bytes);
1691 if (sinfo->filled & STATION_INFO_TX_BYTES)
1692 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1693 sinfo->tx_bytes);
1694 if (sinfo->filled & STATION_INFO_LLID)
1695 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1696 sinfo->llid);
1697 if (sinfo->filled & STATION_INFO_PLID)
1698 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1699 sinfo->plid);
1700 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1701 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1702 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001703 if (sinfo->filled & STATION_INFO_SIGNAL)
1704 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1705 sinfo->signal);
1706 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1707 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1708 if (!txrate)
1709 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001710
John W. Linville254416a2009-12-09 16:43:52 -05001711 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1712 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001713 if (bitrate > 0)
1714 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1715
1716 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1717 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1718 sinfo->txrate.mcs);
1719 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1720 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1721 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1722 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1723
1724 nla_nest_end(msg, txrate);
1725 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001726 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1727 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1728 sinfo->rx_packets);
1729 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1730 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1731 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001732 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001733
1734 return genlmsg_end(msg, hdr);
1735
1736 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001737 genlmsg_cancel(msg, hdr);
1738 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001739}
1740
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001741static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001742 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001743{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001744 struct station_info sinfo;
1745 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001746 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001747 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001748 int ifidx = cb->args[0];
1749 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001750 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001751
Holger Schuriga0438972009-11-11 11:30:02 +01001752 if (!ifidx)
1753 ifidx = nl80211_get_ifidx(cb);
1754 if (ifidx < 0)
1755 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001756
Johannes Berg3b858752009-03-12 09:55:09 +01001757 rtnl_lock();
1758
Johannes Berg463d0182009-07-14 00:33:35 +02001759 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001760 if (!netdev) {
1761 err = -ENODEV;
1762 goto out_rtnl;
1763 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001764
Johannes Berg463d0182009-07-14 00:33:35 +02001765 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001766 if (IS_ERR(dev)) {
1767 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001768 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001769 }
1770
1771 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001772 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001773 goto out_err;
1774 }
1775
Johannes Bergbba95fe2008-07-29 13:22:51 +02001776 while (1) {
1777 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1778 mac_addr, &sinfo);
1779 if (err == -ENOENT)
1780 break;
1781 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001782 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001783
1784 if (nl80211_send_station(skb,
1785 NETLINK_CB(cb->skb).pid,
1786 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1787 netdev, mac_addr,
1788 &sinfo) < 0)
1789 goto out;
1790
1791 sta_idx++;
1792 }
1793
1794
1795 out:
1796 cb->args[1] = sta_idx;
1797 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001798 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001799 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001800 out_rtnl:
1801 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001802
1803 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001804}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001805
Johannes Berg5727ef12007-12-19 02:03:34 +01001806static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1807{
Johannes Berg79c97e92009-07-07 03:56:12 +02001808 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001809 int err;
1810 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001811 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001812 struct sk_buff *msg;
1813 u8 *mac_addr = NULL;
1814
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001815 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001816
1817 if (!info->attrs[NL80211_ATTR_MAC])
1818 return -EINVAL;
1819
1820 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1821
Johannes Berg3b858752009-03-12 09:55:09 +01001822 rtnl_lock();
1823
Johannes Berg463d0182009-07-14 00:33:35 +02001824 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001825 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001826 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001827
Johannes Berg79c97e92009-07-07 03:56:12 +02001828 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001829 err = -EOPNOTSUPP;
1830 goto out;
1831 }
1832
Johannes Berg79c97e92009-07-07 03:56:12 +02001833 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001834 if (err)
1835 goto out;
1836
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001837 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001838 if (!msg)
1839 goto out;
1840
1841 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001842 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001843 goto out_free;
1844
Johannes Berg134e6372009-07-10 09:51:34 +00001845 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001846 goto out;
1847
1848 out_free:
1849 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001850 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001851 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001852 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001853 out_rtnl:
1854 rtnl_unlock();
1855
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001856 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001857}
1858
1859/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001860 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001861 */
Johannes Berg463d0182009-07-14 00:33:35 +02001862static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001863 struct cfg80211_registered_device *rdev,
1864 struct net_device **vlan)
1865{
Johannes Berg463d0182009-07-14 00:33:35 +02001866 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001867 *vlan = NULL;
1868
1869 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001870 *vlan = dev_get_by_index(genl_info_net(info),
1871 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001872 if (!*vlan)
1873 return -ENODEV;
1874 if (!(*vlan)->ieee80211_ptr)
1875 return -EINVAL;
1876 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1877 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001878 if (!netif_running(*vlan))
1879 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001880 }
1881 return 0;
1882}
1883
1884static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1885{
Johannes Berg79c97e92009-07-07 03:56:12 +02001886 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001887 int err;
1888 struct net_device *dev;
1889 struct station_parameters params;
1890 u8 *mac_addr = NULL;
1891
1892 memset(&params, 0, sizeof(params));
1893
1894 params.listen_interval = -1;
1895
1896 if (info->attrs[NL80211_ATTR_STA_AID])
1897 return -EINVAL;
1898
1899 if (!info->attrs[NL80211_ATTR_MAC])
1900 return -EINVAL;
1901
1902 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1903
1904 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1905 params.supported_rates =
1906 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1907 params.supported_rates_len =
1908 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1909 }
1910
1911 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1912 params.listen_interval =
1913 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1914
Jouni Malinen36aedc92008-08-25 11:58:58 +03001915 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1916 params.ht_capa =
1917 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1918
Johannes Bergeccb8e82009-05-11 21:57:56 +03001919 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001920 return -EINVAL;
1921
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001922 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1923 params.plink_action =
1924 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1925
Johannes Berg3b858752009-03-12 09:55:09 +01001926 rtnl_lock();
1927
Johannes Berg463d0182009-07-14 00:33:35 +02001928 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001929 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001930 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001931
Johannes Berg463d0182009-07-14 00:33:35 +02001932 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001933 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001934 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001935
1936 /* validate settings */
1937 err = 0;
1938
1939 switch (dev->ieee80211_ptr->iftype) {
1940 case NL80211_IFTYPE_AP:
1941 case NL80211_IFTYPE_AP_VLAN:
1942 /* disallow mesh-specific things */
1943 if (params.plink_action)
1944 err = -EINVAL;
1945 break;
1946 case NL80211_IFTYPE_STATION:
1947 /* disallow everything but AUTHORIZED flag */
1948 if (params.plink_action)
1949 err = -EINVAL;
1950 if (params.vlan)
1951 err = -EINVAL;
1952 if (params.supported_rates)
1953 err = -EINVAL;
1954 if (params.ht_capa)
1955 err = -EINVAL;
1956 if (params.listen_interval >= 0)
1957 err = -EINVAL;
1958 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1959 err = -EINVAL;
1960 break;
1961 case NL80211_IFTYPE_MESH_POINT:
1962 /* disallow things mesh doesn't support */
1963 if (params.vlan)
1964 err = -EINVAL;
1965 if (params.ht_capa)
1966 err = -EINVAL;
1967 if (params.listen_interval >= 0)
1968 err = -EINVAL;
1969 if (params.supported_rates)
1970 err = -EINVAL;
1971 if (params.sta_flags_mask)
1972 err = -EINVAL;
1973 break;
1974 default:
1975 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001976 }
1977
Johannes Berg5727ef12007-12-19 02:03:34 +01001978 if (err)
1979 goto out;
1980
Johannes Berg79c97e92009-07-07 03:56:12 +02001981 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001982 err = -EOPNOTSUPP;
1983 goto out;
1984 }
1985
Johannes Berg79c97e92009-07-07 03:56:12 +02001986 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001987
1988 out:
1989 if (params.vlan)
1990 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001991 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001992 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001993 out_rtnl:
1994 rtnl_unlock();
1995
Johannes Berg5727ef12007-12-19 02:03:34 +01001996 return err;
1997}
1998
1999static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2000{
Johannes Berg79c97e92009-07-07 03:56:12 +02002001 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002002 int err;
2003 struct net_device *dev;
2004 struct station_parameters params;
2005 u8 *mac_addr = NULL;
2006
2007 memset(&params, 0, sizeof(params));
2008
2009 if (!info->attrs[NL80211_ATTR_MAC])
2010 return -EINVAL;
2011
Johannes Berg5727ef12007-12-19 02:03:34 +01002012 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2013 return -EINVAL;
2014
2015 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2016 return -EINVAL;
2017
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002018 if (!info->attrs[NL80211_ATTR_STA_AID])
2019 return -EINVAL;
2020
Johannes Berg5727ef12007-12-19 02:03:34 +01002021 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2022 params.supported_rates =
2023 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2024 params.supported_rates_len =
2025 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2026 params.listen_interval =
2027 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002028
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002029 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2030 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2031 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002032
Jouni Malinen36aedc92008-08-25 11:58:58 +03002033 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2034 params.ht_capa =
2035 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002036
Johannes Bergeccb8e82009-05-11 21:57:56 +03002037 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002038 return -EINVAL;
2039
Johannes Berg3b858752009-03-12 09:55:09 +01002040 rtnl_lock();
2041
Johannes Berg463d0182009-07-14 00:33:35 +02002042 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002043 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002044 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002045
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002046 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2047 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
2048 err = -EINVAL;
2049 goto out;
2050 }
2051
Johannes Berg463d0182009-07-14 00:33:35 +02002052 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002053 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002054 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002055
2056 /* validate settings */
2057 err = 0;
2058
Johannes Berg79c97e92009-07-07 03:56:12 +02002059 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002060 err = -EOPNOTSUPP;
2061 goto out;
2062 }
2063
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002064 if (!netif_running(dev)) {
2065 err = -ENETDOWN;
2066 goto out;
2067 }
2068
Johannes Berg79c97e92009-07-07 03:56:12 +02002069 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002070
2071 out:
2072 if (params.vlan)
2073 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002074 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002075 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002076 out_rtnl:
2077 rtnl_unlock();
2078
Johannes Berg5727ef12007-12-19 02:03:34 +01002079 return err;
2080}
2081
2082static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2083{
Johannes Berg79c97e92009-07-07 03:56:12 +02002084 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002085 int err;
2086 struct net_device *dev;
2087 u8 *mac_addr = NULL;
2088
2089 if (info->attrs[NL80211_ATTR_MAC])
2090 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2091
Johannes Berg3b858752009-03-12 09:55:09 +01002092 rtnl_lock();
2093
Johannes Berg463d0182009-07-14 00:33:35 +02002094 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002095 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002096 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002097
Johannes Berge80cf852009-05-11 14:43:13 +02002098 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002099 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2100 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002101 err = -EINVAL;
2102 goto out;
2103 }
2104
Johannes Berg79c97e92009-07-07 03:56:12 +02002105 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002106 err = -EOPNOTSUPP;
2107 goto out;
2108 }
2109
Johannes Berg79c97e92009-07-07 03:56:12 +02002110 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002111
2112 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002113 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002114 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002115 out_rtnl:
2116 rtnl_unlock();
2117
Johannes Berg5727ef12007-12-19 02:03:34 +01002118 return err;
2119}
2120
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002121static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2122 int flags, struct net_device *dev,
2123 u8 *dst, u8 *next_hop,
2124 struct mpath_info *pinfo)
2125{
2126 void *hdr;
2127 struct nlattr *pinfoattr;
2128
2129 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2130 if (!hdr)
2131 return -1;
2132
2133 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2134 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2135 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2136
Johannes Bergf5ea9122009-08-07 16:17:38 +02002137 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2138
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002139 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2140 if (!pinfoattr)
2141 goto nla_put_failure;
2142 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2143 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2144 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002145 if (pinfo->filled & MPATH_INFO_SN)
2146 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2147 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002148 if (pinfo->filled & MPATH_INFO_METRIC)
2149 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2150 pinfo->metric);
2151 if (pinfo->filled & MPATH_INFO_EXPTIME)
2152 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2153 pinfo->exptime);
2154 if (pinfo->filled & MPATH_INFO_FLAGS)
2155 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2156 pinfo->flags);
2157 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2158 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2159 pinfo->discovery_timeout);
2160 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2161 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2162 pinfo->discovery_retries);
2163
2164 nla_nest_end(msg, pinfoattr);
2165
2166 return genlmsg_end(msg, hdr);
2167
2168 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002169 genlmsg_cancel(msg, hdr);
2170 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002171}
2172
2173static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002174 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002175{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002176 struct mpath_info pinfo;
2177 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002178 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002179 u8 dst[ETH_ALEN];
2180 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002181 int ifidx = cb->args[0];
2182 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002183 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002184
Holger Schuriga0438972009-11-11 11:30:02 +01002185 if (!ifidx)
2186 ifidx = nl80211_get_ifidx(cb);
2187 if (ifidx < 0)
2188 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002189
Johannes Berg3b858752009-03-12 09:55:09 +01002190 rtnl_lock();
2191
Johannes Berg463d0182009-07-14 00:33:35 +02002192 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002193 if (!netdev) {
2194 err = -ENODEV;
2195 goto out_rtnl;
2196 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002197
Johannes Berg463d0182009-07-14 00:33:35 +02002198 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002199 if (IS_ERR(dev)) {
2200 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002201 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002202 }
2203
2204 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002205 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002206 goto out_err;
2207 }
2208
Jouni Malineneec60b02009-03-20 21:21:19 +02002209 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2210 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002211 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002212 }
2213
Johannes Bergbba95fe2008-07-29 13:22:51 +02002214 while (1) {
2215 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2216 dst, next_hop, &pinfo);
2217 if (err == -ENOENT)
2218 break;
2219 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002220 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002221
2222 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2223 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2224 netdev, dst, next_hop,
2225 &pinfo) < 0)
2226 goto out;
2227
2228 path_idx++;
2229 }
2230
2231
2232 out:
2233 cb->args[1] = path_idx;
2234 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002235 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002236 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002237 out_rtnl:
2238 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002239
2240 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002241}
2242
2243static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2244{
Johannes Berg79c97e92009-07-07 03:56:12 +02002245 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002246 int err;
2247 struct net_device *dev;
2248 struct mpath_info pinfo;
2249 struct sk_buff *msg;
2250 u8 *dst = NULL;
2251 u8 next_hop[ETH_ALEN];
2252
2253 memset(&pinfo, 0, sizeof(pinfo));
2254
2255 if (!info->attrs[NL80211_ATTR_MAC])
2256 return -EINVAL;
2257
2258 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2259
Johannes Berg3b858752009-03-12 09:55:09 +01002260 rtnl_lock();
2261
Johannes Berg463d0182009-07-14 00:33:35 +02002262 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002263 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002264 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002265
Johannes Berg79c97e92009-07-07 03:56:12 +02002266 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002267 err = -EOPNOTSUPP;
2268 goto out;
2269 }
2270
Jouni Malineneec60b02009-03-20 21:21:19 +02002271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2272 err = -EOPNOTSUPP;
2273 goto out;
2274 }
2275
Johannes Berg79c97e92009-07-07 03:56:12 +02002276 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002277 if (err)
2278 goto out;
2279
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002280 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002281 if (!msg)
2282 goto out;
2283
2284 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2285 dev, dst, next_hop, &pinfo) < 0)
2286 goto out_free;
2287
Johannes Berg134e6372009-07-10 09:51:34 +00002288 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002289 goto out;
2290
2291 out_free:
2292 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002293 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002294 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002296 out_rtnl:
2297 rtnl_unlock();
2298
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299 return err;
2300}
2301
2302static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2303{
Johannes Berg79c97e92009-07-07 03:56:12 +02002304 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002305 int err;
2306 struct net_device *dev;
2307 u8 *dst = NULL;
2308 u8 *next_hop = NULL;
2309
2310 if (!info->attrs[NL80211_ATTR_MAC])
2311 return -EINVAL;
2312
2313 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2314 return -EINVAL;
2315
2316 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2317 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2318
Johannes Berg3b858752009-03-12 09:55:09 +01002319 rtnl_lock();
2320
Johannes Berg463d0182009-07-14 00:33:35 +02002321 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002322 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002323 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002324
Johannes Berg79c97e92009-07-07 03:56:12 +02002325 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326 err = -EOPNOTSUPP;
2327 goto out;
2328 }
2329
Jouni Malineneec60b02009-03-20 21:21:19 +02002330 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2331 err = -EOPNOTSUPP;
2332 goto out;
2333 }
2334
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002335 if (!netif_running(dev)) {
2336 err = -ENETDOWN;
2337 goto out;
2338 }
2339
Johannes Berg79c97e92009-07-07 03:56:12 +02002340 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002341
2342 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002343 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002344 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002345 out_rtnl:
2346 rtnl_unlock();
2347
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002348 return err;
2349}
2350static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2351{
Johannes Berg79c97e92009-07-07 03:56:12 +02002352 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002353 int err;
2354 struct net_device *dev;
2355 u8 *dst = NULL;
2356 u8 *next_hop = NULL;
2357
2358 if (!info->attrs[NL80211_ATTR_MAC])
2359 return -EINVAL;
2360
2361 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2362 return -EINVAL;
2363
2364 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2365 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2366
Johannes Berg3b858752009-03-12 09:55:09 +01002367 rtnl_lock();
2368
Johannes Berg463d0182009-07-14 00:33:35 +02002369 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002370 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002371 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372
Johannes Berg79c97e92009-07-07 03:56:12 +02002373 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002374 err = -EOPNOTSUPP;
2375 goto out;
2376 }
2377
Jouni Malineneec60b02009-03-20 21:21:19 +02002378 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2379 err = -EOPNOTSUPP;
2380 goto out;
2381 }
2382
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002383 if (!netif_running(dev)) {
2384 err = -ENETDOWN;
2385 goto out;
2386 }
2387
Johannes Berg79c97e92009-07-07 03:56:12 +02002388 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002389
2390 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002391 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002392 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002393 out_rtnl:
2394 rtnl_unlock();
2395
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396 return err;
2397}
2398
2399static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2400{
Johannes Berg79c97e92009-07-07 03:56:12 +02002401 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002402 int err;
2403 struct net_device *dev;
2404 u8 *dst = NULL;
2405
2406 if (info->attrs[NL80211_ATTR_MAC])
2407 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2408
Johannes Berg3b858752009-03-12 09:55:09 +01002409 rtnl_lock();
2410
Johannes Berg463d0182009-07-14 00:33:35 +02002411 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002412 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002413 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414
Johannes Berg79c97e92009-07-07 03:56:12 +02002415 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002416 err = -EOPNOTSUPP;
2417 goto out;
2418 }
2419
Johannes Berg79c97e92009-07-07 03:56:12 +02002420 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002421
2422 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002423 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002424 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002425 out_rtnl:
2426 rtnl_unlock();
2427
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002428 return err;
2429}
2430
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002431static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2432{
Johannes Berg79c97e92009-07-07 03:56:12 +02002433 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002434 int err;
2435 struct net_device *dev;
2436 struct bss_parameters params;
2437
2438 memset(&params, 0, sizeof(params));
2439 /* default to not changing parameters */
2440 params.use_cts_prot = -1;
2441 params.use_short_preamble = -1;
2442 params.use_short_slot_time = -1;
2443
2444 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2445 params.use_cts_prot =
2446 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2447 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2448 params.use_short_preamble =
2449 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2450 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2451 params.use_short_slot_time =
2452 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002453 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2454 params.basic_rates =
2455 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2456 params.basic_rates_len =
2457 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2458 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002459
Johannes Berg3b858752009-03-12 09:55:09 +01002460 rtnl_lock();
2461
Johannes Berg463d0182009-07-14 00:33:35 +02002462 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002463 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002464 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002465
Johannes Berg79c97e92009-07-07 03:56:12 +02002466 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002467 err = -EOPNOTSUPP;
2468 goto out;
2469 }
2470
Jouni Malineneec60b02009-03-20 21:21:19 +02002471 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2472 err = -EOPNOTSUPP;
2473 goto out;
2474 }
2475
Johannes Berg79c97e92009-07-07 03:56:12 +02002476 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002477
2478 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002479 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002480 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002481 out_rtnl:
2482 rtnl_unlock();
2483
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002484 return err;
2485}
2486
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002487static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002488 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2489 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2490 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2491 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2492 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2493 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2494};
2495
2496static int parse_reg_rule(struct nlattr *tb[],
2497 struct ieee80211_reg_rule *reg_rule)
2498{
2499 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2500 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2501
2502 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2503 return -EINVAL;
2504 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2505 return -EINVAL;
2506 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2507 return -EINVAL;
2508 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2509 return -EINVAL;
2510 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2511 return -EINVAL;
2512
2513 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2514
2515 freq_range->start_freq_khz =
2516 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2517 freq_range->end_freq_khz =
2518 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2519 freq_range->max_bandwidth_khz =
2520 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2521
2522 power_rule->max_eirp =
2523 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2524
2525 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2526 power_rule->max_antenna_gain =
2527 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2528
2529 return 0;
2530}
2531
2532static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2533{
2534 int r;
2535 char *data = NULL;
2536
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002537 /*
2538 * You should only get this when cfg80211 hasn't yet initialized
2539 * completely when built-in to the kernel right between the time
2540 * window between nl80211_init() and regulatory_init(), if that is
2541 * even possible.
2542 */
2543 mutex_lock(&cfg80211_mutex);
2544 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002545 mutex_unlock(&cfg80211_mutex);
2546 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002547 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002548 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002549
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002550 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2551 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002552
2553 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2554
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002555 r = regulatory_hint_user(data);
2556
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002557 return r;
2558}
2559
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002560static int nl80211_get_mesh_params(struct sk_buff *skb,
2561 struct genl_info *info)
2562{
Johannes Berg79c97e92009-07-07 03:56:12 +02002563 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002564 struct mesh_config cur_params;
2565 int err;
2566 struct net_device *dev;
2567 void *hdr;
2568 struct nlattr *pinfoattr;
2569 struct sk_buff *msg;
2570
Johannes Berg3b858752009-03-12 09:55:09 +01002571 rtnl_lock();
2572
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002573 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002574 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002575 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002576 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002577
Johannes Berg79c97e92009-07-07 03:56:12 +02002578 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002579 err = -EOPNOTSUPP;
2580 goto out;
2581 }
2582
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002583 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002584 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002585 if (err)
2586 goto out;
2587
2588 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002590 if (!msg) {
2591 err = -ENOBUFS;
2592 goto out;
2593 }
2594 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2595 NL80211_CMD_GET_MESH_PARAMS);
2596 if (!hdr)
2597 goto nla_put_failure;
2598 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2599 if (!pinfoattr)
2600 goto nla_put_failure;
2601 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2602 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2603 cur_params.dot11MeshRetryTimeout);
2604 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2605 cur_params.dot11MeshConfirmTimeout);
2606 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2607 cur_params.dot11MeshHoldingTimeout);
2608 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2609 cur_params.dot11MeshMaxPeerLinks);
2610 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2611 cur_params.dot11MeshMaxRetries);
2612 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2613 cur_params.dot11MeshTTL);
2614 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2615 cur_params.auto_open_plinks);
2616 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2617 cur_params.dot11MeshHWMPmaxPREQretries);
2618 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2619 cur_params.path_refresh_time);
2620 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2621 cur_params.min_discovery_timeout);
2622 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2623 cur_params.dot11MeshHWMPactivePathTimeout);
2624 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2625 cur_params.dot11MeshHWMPpreqMinInterval);
2626 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2627 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002628 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2629 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002630 nla_nest_end(msg, pinfoattr);
2631 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002632 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002633 goto out;
2634
Johannes Berg3b858752009-03-12 09:55:09 +01002635 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002636 genlmsg_cancel(msg, hdr);
2637 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002638 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002639 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002640 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002641 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002642 out_rtnl:
2643 rtnl_unlock();
2644
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002645 return err;
2646}
2647
2648#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2649do {\
2650 if (table[attr_num]) {\
2651 cfg.param = nla_fn(table[attr_num]); \
2652 mask |= (1 << (attr_num - 1)); \
2653 } \
2654} while (0);\
2655
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002656static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002657 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2658 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2659 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2660 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2661 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2662 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2663 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2664
2665 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2666 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2667 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2668 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2669 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2670 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2671};
2672
2673static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2674{
2675 int err;
2676 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002677 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002678 struct net_device *dev;
2679 struct mesh_config cfg;
2680 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2681 struct nlattr *parent_attr;
2682
2683 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2684 if (!parent_attr)
2685 return -EINVAL;
2686 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2687 parent_attr, nl80211_meshconf_params_policy))
2688 return -EINVAL;
2689
Johannes Berg3b858752009-03-12 09:55:09 +01002690 rtnl_lock();
2691
Johannes Berg463d0182009-07-14 00:33:35 +02002692 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002693 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002694 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002695
Johannes Berg79c97e92009-07-07 03:56:12 +02002696 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002697 err = -EOPNOTSUPP;
2698 goto out;
2699 }
2700
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002701 /* This makes sure that there aren't more than 32 mesh config
2702 * parameters (otherwise our bitfield scheme would not work.) */
2703 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2704
2705 /* Fill in the params struct */
2706 mask = 0;
2707 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2708 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2710 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2712 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2714 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2716 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2718 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2720 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2722 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2723 nla_get_u8);
2724 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2725 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2727 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2728 nla_get_u16);
2729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2730 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2731 nla_get_u32);
2732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2733 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2734 nla_get_u16);
2735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2736 dot11MeshHWMPnetDiameterTraversalTime,
2737 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2738 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2740 dot11MeshHWMPRootMode, mask,
2741 NL80211_MESHCONF_HWMP_ROOTMODE,
2742 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002743
2744 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002745 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002746
Jouni Malinenf3f92582009-03-20 17:57:36 +02002747 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002748 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002749 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002750 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002751 out_rtnl:
2752 rtnl_unlock();
2753
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002754 return err;
2755}
2756
2757#undef FILL_IN_MESH_PARAM_IF_SET
2758
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002759static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2760{
2761 struct sk_buff *msg;
2762 void *hdr = NULL;
2763 struct nlattr *nl_reg_rules;
2764 unsigned int i;
2765 int err = -EINVAL;
2766
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002767 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002768
2769 if (!cfg80211_regdomain)
2770 goto out;
2771
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002772 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002773 if (!msg) {
2774 err = -ENOBUFS;
2775 goto out;
2776 }
2777
2778 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2779 NL80211_CMD_GET_REG);
2780 if (!hdr)
2781 goto nla_put_failure;
2782
2783 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2784 cfg80211_regdomain->alpha2);
2785
2786 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2787 if (!nl_reg_rules)
2788 goto nla_put_failure;
2789
2790 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2791 struct nlattr *nl_reg_rule;
2792 const struct ieee80211_reg_rule *reg_rule;
2793 const struct ieee80211_freq_range *freq_range;
2794 const struct ieee80211_power_rule *power_rule;
2795
2796 reg_rule = &cfg80211_regdomain->reg_rules[i];
2797 freq_range = &reg_rule->freq_range;
2798 power_rule = &reg_rule->power_rule;
2799
2800 nl_reg_rule = nla_nest_start(msg, i);
2801 if (!nl_reg_rule)
2802 goto nla_put_failure;
2803
2804 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2805 reg_rule->flags);
2806 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2807 freq_range->start_freq_khz);
2808 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2809 freq_range->end_freq_khz);
2810 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2811 freq_range->max_bandwidth_khz);
2812 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2813 power_rule->max_antenna_gain);
2814 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2815 power_rule->max_eirp);
2816
2817 nla_nest_end(msg, nl_reg_rule);
2818 }
2819
2820 nla_nest_end(msg, nl_reg_rules);
2821
2822 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002823 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002824 goto out;
2825
2826nla_put_failure:
2827 genlmsg_cancel(msg, hdr);
2828 err = -EMSGSIZE;
2829out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002830 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002831 return err;
2832}
2833
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002834static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2835{
2836 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2837 struct nlattr *nl_reg_rule;
2838 char *alpha2 = NULL;
2839 int rem_reg_rules = 0, r = 0;
2840 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2841 struct ieee80211_regdomain *rd = NULL;
2842
2843 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2844 return -EINVAL;
2845
2846 if (!info->attrs[NL80211_ATTR_REG_RULES])
2847 return -EINVAL;
2848
2849 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2850
2851 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2852 rem_reg_rules) {
2853 num_rules++;
2854 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002855 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002856 }
2857
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002858 mutex_lock(&cfg80211_mutex);
2859
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002860 if (!reg_is_valid_request(alpha2)) {
2861 r = -EINVAL;
2862 goto bad_reg;
2863 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002864
2865 size_of_regd = sizeof(struct ieee80211_regdomain) +
2866 (num_rules * sizeof(struct ieee80211_reg_rule));
2867
2868 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002869 if (!rd) {
2870 r = -ENOMEM;
2871 goto bad_reg;
2872 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002873
2874 rd->n_reg_rules = num_rules;
2875 rd->alpha2[0] = alpha2[0];
2876 rd->alpha2[1] = alpha2[1];
2877
2878 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2879 rem_reg_rules) {
2880 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2881 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2882 reg_rule_policy);
2883 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2884 if (r)
2885 goto bad_reg;
2886
2887 rule_idx++;
2888
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002889 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2890 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002891 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002892 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002893 }
2894
2895 BUG_ON(rule_idx != num_rules);
2896
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002897 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002898
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002899 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002900
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002901 return r;
2902
Johannes Bergd2372b32008-10-24 20:32:20 +02002903 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002904 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002905 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002906 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002907}
2908
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002909static int validate_scan_freqs(struct nlattr *freqs)
2910{
2911 struct nlattr *attr1, *attr2;
2912 int n_channels = 0, tmp1, tmp2;
2913
2914 nla_for_each_nested(attr1, freqs, tmp1) {
2915 n_channels++;
2916 /*
2917 * Some hardware has a limited channel list for
2918 * scanning, and it is pretty much nonsensical
2919 * to scan for a channel twice, so disallow that
2920 * and don't require drivers to check that the
2921 * channel list they get isn't longer than what
2922 * they can scan, as long as they can scan all
2923 * the channels they registered at once.
2924 */
2925 nla_for_each_nested(attr2, freqs, tmp2)
2926 if (attr1 != attr2 &&
2927 nla_get_u32(attr1) == nla_get_u32(attr2))
2928 return 0;
2929 }
2930
2931 return n_channels;
2932}
2933
Johannes Berg2a519312009-02-10 21:25:55 +01002934static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2935{
Johannes Berg79c97e92009-07-07 03:56:12 +02002936 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002937 struct net_device *dev;
2938 struct cfg80211_scan_request *request;
2939 struct cfg80211_ssid *ssid;
2940 struct ieee80211_channel *channel;
2941 struct nlattr *attr;
2942 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002943 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002944 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002945 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002946
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002947 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2948 return -EINVAL;
2949
Johannes Berg3b858752009-03-12 09:55:09 +01002950 rtnl_lock();
2951
Johannes Berg463d0182009-07-14 00:33:35 +02002952 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002953 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002954 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002955
Johannes Berg79c97e92009-07-07 03:56:12 +02002956 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002957
Johannes Berg79c97e92009-07-07 03:56:12 +02002958 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002959 err = -EOPNOTSUPP;
2960 goto out;
2961 }
2962
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002963 if (!netif_running(dev)) {
2964 err = -ENETDOWN;
2965 goto out;
2966 }
2967
Johannes Berg79c97e92009-07-07 03:56:12 +02002968 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01002969 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002970 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002971 }
2972
2973 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002974 n_channels = validate_scan_freqs(
2975 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01002976 if (!n_channels) {
2977 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002978 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002979 }
2980 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002981 n_channels = 0;
2982
Johannes Berg2a519312009-02-10 21:25:55 +01002983 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2984 if (wiphy->bands[band])
2985 n_channels += wiphy->bands[band]->n_channels;
2986 }
2987
2988 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2989 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2990 n_ssids++;
2991
2992 if (n_ssids > wiphy->max_scan_ssids) {
2993 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002994 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002995 }
2996
Jouni Malinen70692ad2009-02-16 19:39:13 +02002997 if (info->attrs[NL80211_ATTR_IE])
2998 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2999 else
3000 ie_len = 0;
3001
Johannes Berg18a83652009-03-31 12:12:05 +02003002 if (ie_len > wiphy->max_scan_ie_len) {
3003 err = -EINVAL;
3004 goto out;
3005 }
3006
Johannes Berg2a519312009-02-10 21:25:55 +01003007 request = kzalloc(sizeof(*request)
3008 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003009 + sizeof(channel) * n_channels
3010 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003011 if (!request) {
3012 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003013 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003014 }
3015
Johannes Berg2a519312009-02-10 21:25:55 +01003016 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003017 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003018 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
Johannes Berg584991d2009-11-02 13:32:03 +01003026 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003027 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3028 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003029 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003030 struct ieee80211_channel *chan;
3031
3032 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3033
3034 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003035 err = -EINVAL;
3036 goto out_free;
3037 }
Johannes Berg584991d2009-11-02 13:32:03 +01003038
3039 /* ignore disabled channels */
3040 if (chan->flags & IEEE80211_CHAN_DISABLED)
3041 continue;
3042
3043 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003044 i++;
3045 }
3046 } else {
3047 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003048 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3049 int j;
3050 if (!wiphy->bands[band])
3051 continue;
3052 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003053 struct ieee80211_channel *chan;
3054
3055 chan = &wiphy->bands[band]->channels[j];
3056
3057 if (chan->flags & IEEE80211_CHAN_DISABLED)
3058 continue;
3059
3060 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003061 i++;
3062 }
3063 }
3064 }
3065
Johannes Berg584991d2009-11-02 13:32:03 +01003066 if (!i) {
3067 err = -EINVAL;
3068 goto out_free;
3069 }
3070
3071 request->n_channels = i;
3072
Johannes Berg2a519312009-02-10 21:25:55 +01003073 i = 0;
3074 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3075 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3076 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3077 err = -EINVAL;
3078 goto out_free;
3079 }
3080 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3081 request->ssids[i].ssid_len = nla_len(attr);
3082 i++;
3083 }
3084 }
3085
Jouni Malinen70692ad2009-02-16 19:39:13 +02003086 if (info->attrs[NL80211_ATTR_IE]) {
3087 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003088 memcpy((void *)request->ie,
3089 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003090 request->ie_len);
3091 }
3092
Johannes Berg463d0182009-07-14 00:33:35 +02003093 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003094 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003095
Johannes Berg79c97e92009-07-07 03:56:12 +02003096 rdev->scan_req = request;
3097 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003098
Johannes Berg463d0182009-07-14 00:33:35 +02003099 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003100 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003101 dev_hold(dev);
3102 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003103
Johannes Berg2a519312009-02-10 21:25:55 +01003104 out_free:
3105 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003106 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003107 kfree(request);
3108 }
Johannes Berg2a519312009-02-10 21:25:55 +01003109 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003110 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003111 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003112 out_rtnl:
3113 rtnl_unlock();
3114
Johannes Berg2a519312009-02-10 21:25:55 +01003115 return err;
3116}
3117
3118static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3119 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003120 struct wireless_dev *wdev,
3121 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003122{
Johannes Berg48ab9052009-07-10 18:42:31 +02003123 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003124 void *hdr;
3125 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003126 int i;
3127
3128 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003129
3130 hdr = nl80211hdr_put(msg, pid, seq, flags,
3131 NL80211_CMD_NEW_SCAN_RESULTS);
3132 if (!hdr)
3133 return -1;
3134
Johannes Bergf5ea9122009-08-07 16:17:38 +02003135 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003136 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003137
3138 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3139 if (!bss)
3140 goto nla_put_failure;
3141 if (!is_zero_ether_addr(res->bssid))
3142 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3143 if (res->information_elements && res->len_information_elements)
3144 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3145 res->len_information_elements,
3146 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003147 if (res->beacon_ies && res->len_beacon_ies &&
3148 res->beacon_ies != res->information_elements)
3149 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3150 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003151 if (res->tsf)
3152 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3153 if (res->beacon_interval)
3154 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3155 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3156 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003157 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3158 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003159
Johannes Berg77965c92009-02-18 18:45:06 +01003160 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003161 case CFG80211_SIGNAL_TYPE_MBM:
3162 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3163 break;
3164 case CFG80211_SIGNAL_TYPE_UNSPEC:
3165 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3166 break;
3167 default:
3168 break;
3169 }
3170
Johannes Berg48ab9052009-07-10 18:42:31 +02003171 switch (wdev->iftype) {
3172 case NL80211_IFTYPE_STATION:
3173 if (intbss == wdev->current_bss)
3174 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3175 NL80211_BSS_STATUS_ASSOCIATED);
3176 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3177 if (intbss != wdev->auth_bsses[i])
3178 continue;
3179 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3180 NL80211_BSS_STATUS_AUTHENTICATED);
3181 break;
3182 }
3183 break;
3184 case NL80211_IFTYPE_ADHOC:
3185 if (intbss == wdev->current_bss)
3186 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3187 NL80211_BSS_STATUS_IBSS_JOINED);
3188 break;
3189 default:
3190 break;
3191 }
3192
Johannes Berg2a519312009-02-10 21:25:55 +01003193 nla_nest_end(msg, bss);
3194
3195 return genlmsg_end(msg, hdr);
3196
3197 nla_put_failure:
3198 genlmsg_cancel(msg, hdr);
3199 return -EMSGSIZE;
3200}
3201
3202static int nl80211_dump_scan(struct sk_buff *skb,
3203 struct netlink_callback *cb)
3204{
Johannes Berg48ab9052009-07-10 18:42:31 +02003205 struct cfg80211_registered_device *rdev;
3206 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003207 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003208 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003209 int ifidx = cb->args[0];
3210 int start = cb->args[1], idx = 0;
3211 int err;
3212
Holger Schuriga0438972009-11-11 11:30:02 +01003213 if (!ifidx)
3214 ifidx = nl80211_get_ifidx(cb);
3215 if (ifidx < 0)
3216 return ifidx;
3217 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003218
Johannes Berg463d0182009-07-14 00:33:35 +02003219 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003220 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003221 return -ENODEV;
3222
Johannes Berg463d0182009-07-14 00:33:35 +02003223 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003224 if (IS_ERR(rdev)) {
3225 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003226 goto out_put_netdev;
3227 }
3228
Johannes Berg48ab9052009-07-10 18:42:31 +02003229 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003230
Johannes Berg48ab9052009-07-10 18:42:31 +02003231 wdev_lock(wdev);
3232 spin_lock_bh(&rdev->bss_lock);
3233 cfg80211_bss_expire(rdev);
3234
3235 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003236 if (++idx <= start)
3237 continue;
3238 if (nl80211_send_bss(skb,
3239 NETLINK_CB(cb->skb).pid,
3240 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003241 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003242 idx--;
3243 goto out;
3244 }
3245 }
3246
3247 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003248 spin_unlock_bh(&rdev->bss_lock);
3249 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003250
3251 cb->args[1] = idx;
3252 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003253 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003254 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003255 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003256
3257 return err;
3258}
3259
Holger Schurig61fa7132009-11-11 12:25:40 +01003260static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3261 int flags, struct net_device *dev,
3262 struct survey_info *survey)
3263{
3264 void *hdr;
3265 struct nlattr *infoattr;
3266
3267 /* Survey without a channel doesn't make sense */
3268 if (!survey->channel)
3269 return -EINVAL;
3270
3271 hdr = nl80211hdr_put(msg, pid, seq, flags,
3272 NL80211_CMD_NEW_SURVEY_RESULTS);
3273 if (!hdr)
3274 return -ENOMEM;
3275
3276 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3277
3278 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3279 if (!infoattr)
3280 goto nla_put_failure;
3281
3282 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3283 survey->channel->center_freq);
3284 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3285 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3286 survey->noise);
3287
3288 nla_nest_end(msg, infoattr);
3289
3290 return genlmsg_end(msg, hdr);
3291
3292 nla_put_failure:
3293 genlmsg_cancel(msg, hdr);
3294 return -EMSGSIZE;
3295}
3296
3297static int nl80211_dump_survey(struct sk_buff *skb,
3298 struct netlink_callback *cb)
3299{
3300 struct survey_info survey;
3301 struct cfg80211_registered_device *dev;
3302 struct net_device *netdev;
3303 int ifidx = cb->args[0];
3304 int survey_idx = cb->args[1];
3305 int res;
3306
3307 if (!ifidx)
3308 ifidx = nl80211_get_ifidx(cb);
3309 if (ifidx < 0)
3310 return ifidx;
3311 cb->args[0] = ifidx;
3312
3313 rtnl_lock();
3314
3315 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3316 if (!netdev) {
3317 res = -ENODEV;
3318 goto out_rtnl;
3319 }
3320
3321 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3322 if (IS_ERR(dev)) {
3323 res = PTR_ERR(dev);
3324 goto out_rtnl;
3325 }
3326
3327 if (!dev->ops->dump_survey) {
3328 res = -EOPNOTSUPP;
3329 goto out_err;
3330 }
3331
3332 while (1) {
3333 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3334 &survey);
3335 if (res == -ENOENT)
3336 break;
3337 if (res)
3338 goto out_err;
3339
3340 if (nl80211_send_survey(skb,
3341 NETLINK_CB(cb->skb).pid,
3342 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3343 netdev,
3344 &survey) < 0)
3345 goto out;
3346 survey_idx++;
3347 }
3348
3349 out:
3350 cb->args[1] = survey_idx;
3351 res = skb->len;
3352 out_err:
3353 cfg80211_unlock_rdev(dev);
3354 out_rtnl:
3355 rtnl_unlock();
3356
3357 return res;
3358}
3359
Jouni Malinen255e7372009-03-20 21:21:17 +02003360static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3361{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003362 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003363}
3364
Samuel Ortizb23aa672009-07-01 21:26:54 +02003365static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3366{
3367 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3368 NL80211_WPA_VERSION_2));
3369}
3370
3371static bool nl80211_valid_akm_suite(u32 akm)
3372{
3373 return akm == WLAN_AKM_SUITE_8021X ||
3374 akm == WLAN_AKM_SUITE_PSK;
3375}
3376
3377static bool nl80211_valid_cipher_suite(u32 cipher)
3378{
3379 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3380 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3381 cipher == WLAN_CIPHER_SUITE_TKIP ||
3382 cipher == WLAN_CIPHER_SUITE_CCMP ||
3383 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3384}
3385
3386
Jouni Malinen636a5d32009-03-19 13:39:22 +02003387static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3388{
Johannes Berg79c97e92009-07-07 03:56:12 +02003389 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003390 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003391 struct ieee80211_channel *chan;
3392 const u8 *bssid, *ssid, *ie = NULL;
3393 int err, ssid_len, ie_len = 0;
3394 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003395 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003396
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003397 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3398 return -EINVAL;
3399
3400 if (!info->attrs[NL80211_ATTR_MAC])
3401 return -EINVAL;
3402
Jouni Malinen17780922009-03-27 20:52:47 +02003403 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3404 return -EINVAL;
3405
Johannes Berg19957bb2009-07-02 17:20:43 +02003406 if (!info->attrs[NL80211_ATTR_SSID])
3407 return -EINVAL;
3408
3409 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3410 return -EINVAL;
3411
Johannes Bergfffd0932009-07-08 14:22:54 +02003412 err = nl80211_parse_key(info, &key);
3413 if (err)
3414 return err;
3415
3416 if (key.idx >= 0) {
3417 if (!key.p.key || !key.p.key_len)
3418 return -EINVAL;
3419 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3420 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3421 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3422 key.p.key_len != WLAN_KEY_LEN_WEP104))
3423 return -EINVAL;
3424 if (key.idx > 4)
3425 return -EINVAL;
3426 } else {
3427 key.p.key_len = 0;
3428 key.p.key = NULL;
3429 }
3430
Jouni Malinen636a5d32009-03-19 13:39:22 +02003431 rtnl_lock();
3432
Johannes Berg463d0182009-07-14 00:33:35 +02003433 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003434 if (err)
3435 goto unlock_rtnl;
3436
Johannes Berg79c97e92009-07-07 03:56:12 +02003437 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003438 err = -EOPNOTSUPP;
3439 goto out;
3440 }
3441
Jouni Malineneec60b02009-03-20 21:21:19 +02003442 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3443 err = -EOPNOTSUPP;
3444 goto out;
3445 }
3446
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003447 if (!netif_running(dev)) {
3448 err = -ENETDOWN;
3449 goto out;
3450 }
3451
Johannes Berg19957bb2009-07-02 17:20:43 +02003452 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003453 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003454 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3455 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003456 err = -EINVAL;
3457 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003458 }
3459
Johannes Berg19957bb2009-07-02 17:20:43 +02003460 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3461 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3462
3463 if (info->attrs[NL80211_ATTR_IE]) {
3464 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3465 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3466 }
3467
3468 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3469 if (!nl80211_valid_auth_type(auth_type)) {
3470 err = -EINVAL;
3471 goto out;
3472 }
3473
Johannes Berg79c97e92009-07-07 03:56:12 +02003474 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003475 ssid, ssid_len, ie, ie_len,
3476 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003477
3478out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003479 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003480 dev_put(dev);
3481unlock_rtnl:
3482 rtnl_unlock();
3483 return err;
3484}
3485
Samuel Ortizb23aa672009-07-01 21:26:54 +02003486static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003487 struct cfg80211_crypto_settings *settings,
3488 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003489{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003490 memset(settings, 0, sizeof(*settings));
3491
Samuel Ortizb23aa672009-07-01 21:26:54 +02003492 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3493
3494 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3495 void *data;
3496 int len, i;
3497
3498 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3499 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3500 settings->n_ciphers_pairwise = len / sizeof(u32);
3501
3502 if (len % sizeof(u32))
3503 return -EINVAL;
3504
Johannes Berg3dc27d22009-07-02 21:36:37 +02003505 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003506 return -EINVAL;
3507
3508 memcpy(settings->ciphers_pairwise, data, len);
3509
3510 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3511 if (!nl80211_valid_cipher_suite(
3512 settings->ciphers_pairwise[i]))
3513 return -EINVAL;
3514 }
3515
3516 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3517 settings->cipher_group =
3518 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3519 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3520 return -EINVAL;
3521 }
3522
3523 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3524 settings->wpa_versions =
3525 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3526 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3527 return -EINVAL;
3528 }
3529
3530 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3531 void *data;
3532 int len, i;
3533
3534 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3535 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3536 settings->n_akm_suites = len / sizeof(u32);
3537
3538 if (len % sizeof(u32))
3539 return -EINVAL;
3540
3541 memcpy(settings->akm_suites, data, len);
3542
3543 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3544 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3545 return -EINVAL;
3546 }
3547
3548 return 0;
3549}
3550
Jouni Malinen636a5d32009-03-19 13:39:22 +02003551static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3552{
Johannes Berg19957bb2009-07-02 17:20:43 +02003553 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003554 struct net_device *dev;
Jouni Malinen27e310c2010-01-19 10:53:30 -08003555 struct wireless_dev *wdev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003556 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003557 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003558 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003559 int err, ssid_len, ie_len = 0;
3560 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003561
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003562 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3563 return -EINVAL;
3564
3565 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003566 !info->attrs[NL80211_ATTR_SSID] ||
3567 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003568 return -EINVAL;
3569
Jouni Malinen636a5d32009-03-19 13:39:22 +02003570 rtnl_lock();
3571
Johannes Berg463d0182009-07-14 00:33:35 +02003572 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003573 if (err)
3574 goto unlock_rtnl;
3575
Johannes Berg19957bb2009-07-02 17:20:43 +02003576 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003577 err = -EOPNOTSUPP;
3578 goto out;
3579 }
3580
Jouni Malineneec60b02009-03-20 21:21:19 +02003581 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3582 err = -EOPNOTSUPP;
3583 goto out;
3584 }
3585
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003586 if (!netif_running(dev)) {
3587 err = -ENETDOWN;
3588 goto out;
3589 }
3590
Johannes Berg19957bb2009-07-02 17:20:43 +02003591 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003592
Johannes Berg19957bb2009-07-02 17:20:43 +02003593 chan = ieee80211_get_channel(&rdev->wiphy,
3594 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3595 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3596 err = -EINVAL;
3597 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003598 }
3599
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003600 mutex_lock(&rdev->devlist_mtx);
Jouni Malinen27e310c2010-01-19 10:53:30 -08003601 wdev = dev->ieee80211_ptr;
3602 fixedchan = rdev_fixed_channel(rdev, wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003603 if (fixedchan && chan != fixedchan) {
3604 err = -EBUSY;
3605 mutex_unlock(&rdev->devlist_mtx);
3606 goto out;
3607 }
3608 mutex_unlock(&rdev->devlist_mtx);
3609
Johannes Berg19957bb2009-07-02 17:20:43 +02003610 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3611 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003612
3613 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003614 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3615 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003616 }
3617
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003618 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003619 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003620 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003621 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003622 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003623 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003624 err = -EINVAL;
3625 goto out;
3626 }
3627 }
3628
Johannes Berg3e5d7642009-07-07 14:37:26 +02003629 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3630 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3631
Johannes Berg3dc27d22009-07-02 21:36:37 +02003632 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003633 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003634 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3635 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003636 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003637
3638out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003639 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003640 dev_put(dev);
3641unlock_rtnl:
3642 rtnl_unlock();
3643 return err;
3644}
3645
3646static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3647{
Johannes Berg79c97e92009-07-07 03:56:12 +02003648 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003649 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003650 const u8 *ie = NULL, *bssid;
3651 int err, ie_len = 0;
3652 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003653
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003654 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3655 return -EINVAL;
3656
3657 if (!info->attrs[NL80211_ATTR_MAC])
3658 return -EINVAL;
3659
3660 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3661 return -EINVAL;
3662
Jouni Malinen636a5d32009-03-19 13:39:22 +02003663 rtnl_lock();
3664
Johannes Berg463d0182009-07-14 00:33:35 +02003665 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003666 if (err)
3667 goto unlock_rtnl;
3668
Johannes Berg79c97e92009-07-07 03:56:12 +02003669 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003670 err = -EOPNOTSUPP;
3671 goto out;
3672 }
3673
Jouni Malineneec60b02009-03-20 21:21:19 +02003674 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3675 err = -EOPNOTSUPP;
3676 goto out;
3677 }
3678
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003679 if (!netif_running(dev)) {
3680 err = -ENETDOWN;
3681 goto out;
3682 }
3683
Johannes Berg19957bb2009-07-02 17:20:43 +02003684 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003685
Johannes Berg19957bb2009-07-02 17:20:43 +02003686 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3687 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003688 /* Reason Code 0 is reserved */
3689 err = -EINVAL;
3690 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003691 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003692
3693 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003694 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3695 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003696 }
3697
Johannes Berg79c97e92009-07-07 03:56:12 +02003698 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003699
3700out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003701 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003702 dev_put(dev);
3703unlock_rtnl:
3704 rtnl_unlock();
3705 return err;
3706}
3707
3708static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3709{
Johannes Berg79c97e92009-07-07 03:56:12 +02003710 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003711 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003712 const u8 *ie = NULL, *bssid;
3713 int err, ie_len = 0;
3714 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003715
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003716 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3717 return -EINVAL;
3718
3719 if (!info->attrs[NL80211_ATTR_MAC])
3720 return -EINVAL;
3721
3722 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3723 return -EINVAL;
3724
Jouni Malinen636a5d32009-03-19 13:39:22 +02003725 rtnl_lock();
3726
Johannes Berg463d0182009-07-14 00:33:35 +02003727 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003728 if (err)
3729 goto unlock_rtnl;
3730
Johannes Berg79c97e92009-07-07 03:56:12 +02003731 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003732 err = -EOPNOTSUPP;
3733 goto out;
3734 }
3735
Jouni Malineneec60b02009-03-20 21:21:19 +02003736 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3737 err = -EOPNOTSUPP;
3738 goto out;
3739 }
3740
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003741 if (!netif_running(dev)) {
3742 err = -ENETDOWN;
3743 goto out;
3744 }
3745
Johannes Berg19957bb2009-07-02 17:20:43 +02003746 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003747
Johannes Berg19957bb2009-07-02 17:20:43 +02003748 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3749 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003750 /* Reason Code 0 is reserved */
3751 err = -EINVAL;
3752 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003753 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003754
3755 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003756 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3757 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003758 }
3759
Johannes Berg79c97e92009-07-07 03:56:12 +02003760 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003761
3762out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003763 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003764 dev_put(dev);
3765unlock_rtnl:
3766 rtnl_unlock();
3767 return err;
3768}
3769
Johannes Berg04a773a2009-04-19 21:24:32 +02003770static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3771{
Johannes Berg79c97e92009-07-07 03:56:12 +02003772 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003773 struct net_device *dev;
3774 struct cfg80211_ibss_params ibss;
3775 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003776 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003777 int err;
3778
Johannes Berg8e30bc52009-04-22 17:45:38 +02003779 memset(&ibss, 0, sizeof(ibss));
3780
Johannes Berg04a773a2009-04-19 21:24:32 +02003781 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3782 return -EINVAL;
3783
3784 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3785 !info->attrs[NL80211_ATTR_SSID] ||
3786 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3787 return -EINVAL;
3788
Johannes Berg8e30bc52009-04-22 17:45:38 +02003789 ibss.beacon_interval = 100;
3790
3791 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3792 ibss.beacon_interval =
3793 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3794 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3795 return -EINVAL;
3796 }
3797
Johannes Berg04a773a2009-04-19 21:24:32 +02003798 rtnl_lock();
3799
Johannes Berg463d0182009-07-14 00:33:35 +02003800 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003801 if (err)
3802 goto unlock_rtnl;
3803
Johannes Berg79c97e92009-07-07 03:56:12 +02003804 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003805 err = -EOPNOTSUPP;
3806 goto out;
3807 }
3808
3809 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3810 err = -EOPNOTSUPP;
3811 goto out;
3812 }
3813
3814 if (!netif_running(dev)) {
3815 err = -ENETDOWN;
3816 goto out;
3817 }
3818
Johannes Berg79c97e92009-07-07 03:56:12 +02003819 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003820
3821 if (info->attrs[NL80211_ATTR_MAC])
3822 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3823 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3824 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3825
3826 if (info->attrs[NL80211_ATTR_IE]) {
3827 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3828 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3829 }
3830
3831 ibss.channel = ieee80211_get_channel(wiphy,
3832 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3833 if (!ibss.channel ||
3834 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3835 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3836 err = -EINVAL;
3837 goto out;
3838 }
3839
3840 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003841 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003842
Johannes Bergfffd0932009-07-08 14:22:54 +02003843 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3844 connkeys = nl80211_parse_connkeys(rdev,
3845 info->attrs[NL80211_ATTR_KEYS]);
3846 if (IS_ERR(connkeys)) {
3847 err = PTR_ERR(connkeys);
3848 connkeys = NULL;
3849 goto out;
3850 }
3851 }
3852
3853 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003854
3855out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003856 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003857 dev_put(dev);
3858unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003859 if (err)
3860 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003861 rtnl_unlock();
3862 return err;
3863}
3864
3865static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3866{
Johannes Berg79c97e92009-07-07 03:56:12 +02003867 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003868 struct net_device *dev;
3869 int err;
3870
3871 rtnl_lock();
3872
Johannes Berg463d0182009-07-14 00:33:35 +02003873 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003874 if (err)
3875 goto unlock_rtnl;
3876
Johannes Berg79c97e92009-07-07 03:56:12 +02003877 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003878 err = -EOPNOTSUPP;
3879 goto out;
3880 }
3881
3882 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3883 err = -EOPNOTSUPP;
3884 goto out;
3885 }
3886
3887 if (!netif_running(dev)) {
3888 err = -ENETDOWN;
3889 goto out;
3890 }
3891
Johannes Berg79c97e92009-07-07 03:56:12 +02003892 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003893
3894out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003895 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003896 dev_put(dev);
3897unlock_rtnl:
3898 rtnl_unlock();
3899 return err;
3900}
3901
Johannes Bergaff89a92009-07-01 21:26:51 +02003902#ifdef CONFIG_NL80211_TESTMODE
3903static struct genl_multicast_group nl80211_testmode_mcgrp = {
3904 .name = "testmode",
3905};
3906
3907static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3908{
3909 struct cfg80211_registered_device *rdev;
3910 int err;
3911
3912 if (!info->attrs[NL80211_ATTR_TESTDATA])
3913 return -EINVAL;
3914
3915 rtnl_lock();
3916
3917 rdev = cfg80211_get_dev_from_info(info);
3918 if (IS_ERR(rdev)) {
3919 err = PTR_ERR(rdev);
3920 goto unlock_rtnl;
3921 }
3922
3923 err = -EOPNOTSUPP;
3924 if (rdev->ops->testmode_cmd) {
3925 rdev->testmode_info = info;
3926 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3927 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3928 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3929 rdev->testmode_info = NULL;
3930 }
3931
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003932 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003933
3934 unlock_rtnl:
3935 rtnl_unlock();
3936 return err;
3937}
3938
3939static struct sk_buff *
3940__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3941 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3942{
3943 struct sk_buff *skb;
3944 void *hdr;
3945 struct nlattr *data;
3946
3947 skb = nlmsg_new(approxlen + 100, gfp);
3948 if (!skb)
3949 return NULL;
3950
3951 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3952 if (!hdr) {
3953 kfree_skb(skb);
3954 return NULL;
3955 }
3956
3957 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3958 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3959
3960 ((void **)skb->cb)[0] = rdev;
3961 ((void **)skb->cb)[1] = hdr;
3962 ((void **)skb->cb)[2] = data;
3963
3964 return skb;
3965
3966 nla_put_failure:
3967 kfree_skb(skb);
3968 return NULL;
3969}
3970
3971struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3972 int approxlen)
3973{
3974 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3975
3976 if (WARN_ON(!rdev->testmode_info))
3977 return NULL;
3978
3979 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3980 rdev->testmode_info->snd_pid,
3981 rdev->testmode_info->snd_seq,
3982 GFP_KERNEL);
3983}
3984EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3985
3986int cfg80211_testmode_reply(struct sk_buff *skb)
3987{
3988 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3989 void *hdr = ((void **)skb->cb)[1];
3990 struct nlattr *data = ((void **)skb->cb)[2];
3991
3992 if (WARN_ON(!rdev->testmode_info)) {
3993 kfree_skb(skb);
3994 return -EINVAL;
3995 }
3996
3997 nla_nest_end(skb, data);
3998 genlmsg_end(skb, hdr);
3999 return genlmsg_reply(skb, rdev->testmode_info);
4000}
4001EXPORT_SYMBOL(cfg80211_testmode_reply);
4002
4003struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4004 int approxlen, gfp_t gfp)
4005{
4006 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4007
4008 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4009}
4010EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4011
4012void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4013{
4014 void *hdr = ((void **)skb->cb)[1];
4015 struct nlattr *data = ((void **)skb->cb)[2];
4016
4017 nla_nest_end(skb, data);
4018 genlmsg_end(skb, hdr);
4019 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4020}
4021EXPORT_SYMBOL(cfg80211_testmode_event);
4022#endif
4023
Samuel Ortizb23aa672009-07-01 21:26:54 +02004024static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4025{
Johannes Berg79c97e92009-07-07 03:56:12 +02004026 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004027 struct net_device *dev;
4028 struct cfg80211_connect_params connect;
4029 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004030 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004031 int err;
4032
4033 memset(&connect, 0, sizeof(connect));
4034
4035 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4036 return -EINVAL;
4037
4038 if (!info->attrs[NL80211_ATTR_SSID] ||
4039 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4040 return -EINVAL;
4041
4042 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4043 connect.auth_type =
4044 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4045 if (!nl80211_valid_auth_type(connect.auth_type))
4046 return -EINVAL;
4047 } else
4048 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4049
4050 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4051
Johannes Berg3dc27d22009-07-02 21:36:37 +02004052 err = nl80211_crypto_settings(info, &connect.crypto,
4053 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004054 if (err)
4055 return err;
4056 rtnl_lock();
4057
Johannes Berg463d0182009-07-14 00:33:35 +02004058 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004059 if (err)
4060 goto unlock_rtnl;
4061
4062 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4063 err = -EOPNOTSUPP;
4064 goto out;
4065 }
4066
4067 if (!netif_running(dev)) {
4068 err = -ENETDOWN;
4069 goto out;
4070 }
4071
Johannes Berg79c97e92009-07-07 03:56:12 +02004072 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004073
Samuel Ortizb23aa672009-07-01 21:26:54 +02004074 if (info->attrs[NL80211_ATTR_MAC])
4075 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4076 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4077 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4078
4079 if (info->attrs[NL80211_ATTR_IE]) {
4080 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4081 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4082 }
4083
4084 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4085 connect.channel =
4086 ieee80211_get_channel(wiphy,
4087 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4088 if (!connect.channel ||
4089 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4090 err = -EINVAL;
4091 goto out;
4092 }
4093 }
4094
Johannes Bergfffd0932009-07-08 14:22:54 +02004095 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4096 connkeys = nl80211_parse_connkeys(rdev,
4097 info->attrs[NL80211_ATTR_KEYS]);
4098 if (IS_ERR(connkeys)) {
4099 err = PTR_ERR(connkeys);
4100 connkeys = NULL;
4101 goto out;
4102 }
4103 }
4104
4105 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004106
4107out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004108 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004109 dev_put(dev);
4110unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004111 if (err)
4112 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004113 rtnl_unlock();
4114 return err;
4115}
4116
4117static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4118{
Johannes Berg79c97e92009-07-07 03:56:12 +02004119 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004120 struct net_device *dev;
4121 int err;
4122 u16 reason;
4123
4124 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4125 reason = WLAN_REASON_DEAUTH_LEAVING;
4126 else
4127 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4128
4129 if (reason == 0)
4130 return -EINVAL;
4131
4132 rtnl_lock();
4133
Johannes Berg463d0182009-07-14 00:33:35 +02004134 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004135 if (err)
4136 goto unlock_rtnl;
4137
4138 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4139 err = -EOPNOTSUPP;
4140 goto out;
4141 }
4142
4143 if (!netif_running(dev)) {
4144 err = -ENETDOWN;
4145 goto out;
4146 }
4147
Johannes Berg79c97e92009-07-07 03:56:12 +02004148 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004149
4150out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004151 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004152 dev_put(dev);
4153unlock_rtnl:
4154 rtnl_unlock();
4155 return err;
4156}
4157
Johannes Berg463d0182009-07-14 00:33:35 +02004158static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4159{
4160 struct cfg80211_registered_device *rdev;
4161 struct net *net;
4162 int err;
4163 u32 pid;
4164
4165 if (!info->attrs[NL80211_ATTR_PID])
4166 return -EINVAL;
4167
4168 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4169
4170 rtnl_lock();
4171
4172 rdev = cfg80211_get_dev_from_info(info);
4173 if (IS_ERR(rdev)) {
4174 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004175 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004176 }
4177
4178 net = get_net_ns_by_pid(pid);
4179 if (IS_ERR(net)) {
4180 err = PTR_ERR(net);
4181 goto out;
4182 }
4183
4184 err = 0;
4185
4186 /* check if anything to do */
4187 if (net_eq(wiphy_net(&rdev->wiphy), net))
4188 goto out_put_net;
4189
4190 err = cfg80211_switch_netns(rdev, net);
4191 out_put_net:
4192 put_net(net);
4193 out:
4194 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004195 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004196 rtnl_unlock();
4197 return err;
4198}
4199
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004200static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4201{
4202 struct cfg80211_registered_device *rdev;
4203 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4204 struct cfg80211_pmksa *pmksa) = NULL;
4205 int err;
4206 struct net_device *dev;
4207 struct cfg80211_pmksa pmksa;
4208
4209 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4210
4211 if (!info->attrs[NL80211_ATTR_MAC])
4212 return -EINVAL;
4213
4214 if (!info->attrs[NL80211_ATTR_PMKID])
4215 return -EINVAL;
4216
4217 rtnl_lock();
4218
4219 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4220 if (err)
4221 goto out_rtnl;
4222
4223 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4224 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4225
4226 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4227 err = -EOPNOTSUPP;
4228 goto out;
4229 }
4230
4231 switch (info->genlhdr->cmd) {
4232 case NL80211_CMD_SET_PMKSA:
4233 rdev_ops = rdev->ops->set_pmksa;
4234 break;
4235 case NL80211_CMD_DEL_PMKSA:
4236 rdev_ops = rdev->ops->del_pmksa;
4237 break;
4238 default:
4239 WARN_ON(1);
4240 break;
4241 }
4242
4243 if (!rdev_ops) {
4244 err = -EOPNOTSUPP;
4245 goto out;
4246 }
4247
4248 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4249
4250 out:
4251 cfg80211_unlock_rdev(rdev);
4252 dev_put(dev);
4253 out_rtnl:
4254 rtnl_unlock();
4255
4256 return err;
4257}
4258
4259static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4260{
4261 struct cfg80211_registered_device *rdev;
4262 int err;
4263 struct net_device *dev;
4264
4265 rtnl_lock();
4266
4267 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4268 if (err)
4269 goto out_rtnl;
4270
4271 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4272 err = -EOPNOTSUPP;
4273 goto out;
4274 }
4275
4276 if (!rdev->ops->flush_pmksa) {
4277 err = -EOPNOTSUPP;
4278 goto out;
4279 }
4280
4281 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4282
4283 out:
4284 cfg80211_unlock_rdev(rdev);
4285 dev_put(dev);
4286 out_rtnl:
4287 rtnl_unlock();
4288
4289 return err;
4290
4291}
4292
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004293static int nl80211_remain_on_channel(struct sk_buff *skb,
4294 struct genl_info *info)
4295{
4296 struct cfg80211_registered_device *rdev;
4297 struct net_device *dev;
4298 struct ieee80211_channel *chan;
4299 struct sk_buff *msg;
4300 void *hdr;
4301 u64 cookie;
4302 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4303 u32 freq, duration;
4304 int err;
4305
4306 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4307 !info->attrs[NL80211_ATTR_DURATION])
4308 return -EINVAL;
4309
4310 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4311
4312 /*
4313 * We should be on that channel for at least one jiffie,
4314 * and more than 5 seconds seems excessive.
4315 */
4316 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4317 return -EINVAL;
4318
4319 rtnl_lock();
4320
4321 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4322 if (err)
4323 goto unlock_rtnl;
4324
4325 if (!rdev->ops->remain_on_channel) {
4326 err = -EOPNOTSUPP;
4327 goto out;
4328 }
4329
4330 if (!netif_running(dev)) {
4331 err = -ENETDOWN;
4332 goto out;
4333 }
4334
4335 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4336 channel_type = nla_get_u32(
4337 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4338 if (channel_type != NL80211_CHAN_NO_HT &&
4339 channel_type != NL80211_CHAN_HT20 &&
4340 channel_type != NL80211_CHAN_HT40PLUS &&
4341 channel_type != NL80211_CHAN_HT40MINUS)
4342 err = -EINVAL;
4343 goto out;
4344 }
4345
4346 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4347 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4348 if (chan == NULL) {
4349 err = -EINVAL;
4350 goto out;
4351 }
4352
4353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4354 if (!msg) {
4355 err = -ENOMEM;
4356 goto out;
4357 }
4358
4359 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4360 NL80211_CMD_REMAIN_ON_CHANNEL);
4361
4362 if (IS_ERR(hdr)) {
4363 err = PTR_ERR(hdr);
4364 goto free_msg;
4365 }
4366
4367 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4368 channel_type, duration, &cookie);
4369
4370 if (err)
4371 goto free_msg;
4372
4373 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4374
4375 genlmsg_end(msg, hdr);
4376 err = genlmsg_reply(msg, info);
4377 goto out;
4378
4379 nla_put_failure:
4380 err = -ENOBUFS;
4381 free_msg:
4382 nlmsg_free(msg);
4383 out:
4384 cfg80211_unlock_rdev(rdev);
4385 dev_put(dev);
4386 unlock_rtnl:
4387 rtnl_unlock();
4388 return err;
4389}
4390
4391static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4392 struct genl_info *info)
4393{
4394 struct cfg80211_registered_device *rdev;
4395 struct net_device *dev;
4396 u64 cookie;
4397 int err;
4398
4399 if (!info->attrs[NL80211_ATTR_COOKIE])
4400 return -EINVAL;
4401
4402 rtnl_lock();
4403
4404 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4405 if (err)
4406 goto unlock_rtnl;
4407
4408 if (!rdev->ops->cancel_remain_on_channel) {
4409 err = -EOPNOTSUPP;
4410 goto out;
4411 }
4412
4413 if (!netif_running(dev)) {
4414 err = -ENETDOWN;
4415 goto out;
4416 }
4417
4418 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4419
4420 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4421
4422 out:
4423 cfg80211_unlock_rdev(rdev);
4424 dev_put(dev);
4425 unlock_rtnl:
4426 rtnl_unlock();
4427 return err;
4428}
4429
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004430static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4431 u8 *rates, u8 rates_len)
4432{
4433 u8 i;
4434 u32 mask = 0;
4435
4436 for (i = 0; i < rates_len; i++) {
4437 int rate = (rates[i] & 0x7f) * 5;
4438 int ridx;
4439 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4440 struct ieee80211_rate *srate =
4441 &sband->bitrates[ridx];
4442 if (rate == srate->bitrate) {
4443 mask |= 1 << ridx;
4444 break;
4445 }
4446 }
4447 if (ridx == sband->n_bitrates)
4448 return 0; /* rate not found */
4449 }
4450
4451 return mask;
4452}
4453
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004454static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004455 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4456 .len = NL80211_MAX_SUPP_RATES },
4457};
4458
4459static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4460 struct genl_info *info)
4461{
4462 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4463 struct cfg80211_registered_device *rdev;
4464 struct cfg80211_bitrate_mask mask;
4465 int err, rem, i;
4466 struct net_device *dev;
4467 struct nlattr *tx_rates;
4468 struct ieee80211_supported_band *sband;
4469
4470 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4471 return -EINVAL;
4472
4473 rtnl_lock();
4474
4475 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4476 if (err)
4477 goto unlock_rtnl;
4478
4479 if (!rdev->ops->set_bitrate_mask) {
4480 err = -EOPNOTSUPP;
4481 goto unlock;
4482 }
4483
4484 memset(&mask, 0, sizeof(mask));
4485 /* Default to all rates enabled */
4486 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4487 sband = rdev->wiphy.bands[i];
4488 mask.control[i].legacy =
4489 sband ? (1 << sband->n_bitrates) - 1 : 0;
4490 }
4491
4492 /*
4493 * The nested attribute uses enum nl80211_band as the index. This maps
4494 * directly to the enum ieee80211_band values used in cfg80211.
4495 */
4496 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4497 {
4498 enum ieee80211_band band = nla_type(tx_rates);
4499 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4500 err = -EINVAL;
4501 goto unlock;
4502 }
4503 sband = rdev->wiphy.bands[band];
4504 if (sband == NULL) {
4505 err = -EINVAL;
4506 goto unlock;
4507 }
4508 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4509 nla_len(tx_rates), nl80211_txattr_policy);
4510 if (tb[NL80211_TXRATE_LEGACY]) {
4511 mask.control[band].legacy = rateset_to_mask(
4512 sband,
4513 nla_data(tb[NL80211_TXRATE_LEGACY]),
4514 nla_len(tb[NL80211_TXRATE_LEGACY]));
4515 if (mask.control[band].legacy == 0) {
4516 err = -EINVAL;
4517 goto unlock;
4518 }
4519 }
4520 }
4521
4522 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4523
4524 unlock:
4525 dev_put(dev);
4526 cfg80211_unlock_rdev(rdev);
4527 unlock_rtnl:
4528 rtnl_unlock();
4529 return err;
4530}
4531
Jouni Malinen026331c2010-02-15 12:53:10 +02004532static int nl80211_register_action(struct sk_buff *skb, struct genl_info *info)
4533{
4534 struct cfg80211_registered_device *rdev;
4535 struct net_device *dev;
4536 int err;
4537
4538 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4539 return -EINVAL;
4540
4541 if (nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]) < 1)
4542 return -EINVAL;
4543
4544 rtnl_lock();
4545
4546 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4547 if (err)
4548 goto unlock_rtnl;
4549
4550 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4551 err = -EOPNOTSUPP;
4552 goto out;
4553 }
4554
4555 /* not much point in registering if we can't reply */
4556 if (!rdev->ops->action) {
4557 err = -EOPNOTSUPP;
4558 goto out;
4559 }
4560
4561 err = cfg80211_mlme_register_action(dev->ieee80211_ptr, info->snd_pid,
4562 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4563 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4564 out:
4565 cfg80211_unlock_rdev(rdev);
4566 dev_put(dev);
4567 unlock_rtnl:
4568 rtnl_unlock();
4569 return err;
4570}
4571
4572static int nl80211_action(struct sk_buff *skb, struct genl_info *info)
4573{
4574 struct cfg80211_registered_device *rdev;
4575 struct net_device *dev;
4576 struct ieee80211_channel *chan;
4577 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4578 u32 freq;
4579 int err;
4580 void *hdr;
4581 u64 cookie;
4582 struct sk_buff *msg;
4583
4584 if (!info->attrs[NL80211_ATTR_FRAME] ||
4585 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4586 return -EINVAL;
4587
4588 rtnl_lock();
4589
4590 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4591 if (err)
4592 goto unlock_rtnl;
4593
4594 if (!rdev->ops->action) {
4595 err = -EOPNOTSUPP;
4596 goto out;
4597 }
4598
4599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4600 err = -EOPNOTSUPP;
4601 goto out;
4602 }
4603
4604 if (!netif_running(dev)) {
4605 err = -ENETDOWN;
4606 goto out;
4607 }
4608
4609 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4610 channel_type = nla_get_u32(
4611 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4612 if (channel_type != NL80211_CHAN_NO_HT &&
4613 channel_type != NL80211_CHAN_HT20 &&
4614 channel_type != NL80211_CHAN_HT40PLUS &&
4615 channel_type != NL80211_CHAN_HT40MINUS)
4616 err = -EINVAL;
4617 goto out;
4618 }
4619
4620 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4621 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4622 if (chan == NULL) {
4623 err = -EINVAL;
4624 goto out;
4625 }
4626
4627 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4628 if (!msg) {
4629 err = -ENOMEM;
4630 goto out;
4631 }
4632
4633 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4634 NL80211_CMD_ACTION);
4635
4636 if (IS_ERR(hdr)) {
4637 err = PTR_ERR(hdr);
4638 goto free_msg;
4639 }
4640 err = cfg80211_mlme_action(rdev, dev, chan, channel_type,
4641 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4642 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4643 &cookie);
4644 if (err)
4645 goto free_msg;
4646
4647 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4648
4649 genlmsg_end(msg, hdr);
4650 err = genlmsg_reply(msg, info);
4651 goto out;
4652
4653 nla_put_failure:
4654 err = -ENOBUFS;
4655 free_msg:
4656 nlmsg_free(msg);
4657 out:
4658 cfg80211_unlock_rdev(rdev);
4659 dev_put(dev);
4660unlock_rtnl:
4661 rtnl_unlock();
4662 return err;
4663}
4664
Kalle Valoffb9eb32010-02-17 17:58:10 +02004665static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4666{
4667 struct cfg80211_registered_device *rdev;
4668 struct wireless_dev *wdev;
4669 struct net_device *dev;
4670 u8 ps_state;
4671 bool state;
4672 int err;
4673
4674 if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4675 err = -EINVAL;
4676 goto out;
4677 }
4678
4679 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4680
4681 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4682 err = -EINVAL;
4683 goto out;
4684 }
4685
4686 rtnl_lock();
4687
4688 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4689 if (err)
4690 goto unlock_rdev;
4691
4692 wdev = dev->ieee80211_ptr;
4693
4694 if (!rdev->ops->set_power_mgmt) {
4695 err = -EOPNOTSUPP;
4696 goto unlock_rdev;
4697 }
4698
4699 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4700
4701 if (state == wdev->ps)
4702 goto unlock_rdev;
4703
4704 wdev->ps = state;
4705
4706 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
4707 wdev->ps_timeout))
4708 /* assume this means it's off */
4709 wdev->ps = false;
4710
4711unlock_rdev:
4712 cfg80211_unlock_rdev(rdev);
4713 dev_put(dev);
4714 rtnl_unlock();
4715
4716out:
4717 return err;
4718}
4719
4720static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4721{
4722 struct cfg80211_registered_device *rdev;
4723 enum nl80211_ps_state ps_state;
4724 struct wireless_dev *wdev;
4725 struct net_device *dev;
4726 struct sk_buff *msg;
4727 void *hdr;
4728 int err;
4729
4730 rtnl_lock();
4731
4732 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4733 if (err)
4734 goto unlock_rtnl;
4735
4736 wdev = dev->ieee80211_ptr;
4737
4738 if (!rdev->ops->set_power_mgmt) {
4739 err = -EOPNOTSUPP;
4740 goto out;
4741 }
4742
4743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4744 if (!msg) {
4745 err = -ENOMEM;
4746 goto out;
4747 }
4748
4749 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4750 NL80211_CMD_GET_POWER_SAVE);
4751 if (!hdr) {
4752 err = -ENOMEM;
4753 goto free_msg;
4754 }
4755
4756 if (wdev->ps)
4757 ps_state = NL80211_PS_ENABLED;
4758 else
4759 ps_state = NL80211_PS_DISABLED;
4760
4761 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4762
4763 genlmsg_end(msg, hdr);
4764 err = genlmsg_reply(msg, info);
4765 goto out;
4766
4767nla_put_failure:
4768 err = -ENOBUFS;
4769
4770free_msg:
4771 nlmsg_free(msg);
4772
4773out:
4774 cfg80211_unlock_rdev(rdev);
4775 dev_put(dev);
4776
4777unlock_rtnl:
4778 rtnl_unlock();
4779
4780 return err;
4781}
4782
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004783static struct nla_policy
4784nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4785 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4786 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4787 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4788};
4789
4790static int nl80211_set_cqm_rssi(struct genl_info *info,
4791 s32 threshold, u32 hysteresis)
4792{
4793 struct cfg80211_registered_device *rdev;
4794 struct wireless_dev *wdev;
4795 struct net_device *dev;
4796 int err;
4797
4798 if (threshold > 0)
4799 return -EINVAL;
4800
4801 rtnl_lock();
4802
4803 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4804 if (err)
4805 goto unlock_rdev;
4806
4807 wdev = dev->ieee80211_ptr;
4808
4809 if (!rdev->ops->set_cqm_rssi_config) {
4810 err = -EOPNOTSUPP;
4811 goto unlock_rdev;
4812 }
4813
4814 if (wdev->iftype != NL80211_IFTYPE_STATION) {
4815 err = -EOPNOTSUPP;
4816 goto unlock_rdev;
4817 }
4818
4819 err = rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4820 threshold, hysteresis);
4821
4822unlock_rdev:
4823 cfg80211_unlock_rdev(rdev);
4824 dev_put(dev);
4825 rtnl_unlock();
4826
4827 return err;
4828}
4829
4830static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4831{
4832 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4833 struct nlattr *cqm;
4834 int err;
4835
4836 cqm = info->attrs[NL80211_ATTR_CQM];
4837 if (!cqm) {
4838 err = -EINVAL;
4839 goto out;
4840 }
4841
4842 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4843 nl80211_attr_cqm_policy);
4844 if (err)
4845 goto out;
4846
4847 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4848 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4849 s32 threshold;
4850 u32 hysteresis;
4851 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4852 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4853 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4854 } else
4855 err = -EINVAL;
4856
4857out:
4858 return err;
4859}
4860
Johannes Berg55682962007-09-20 13:09:35 -04004861static struct genl_ops nl80211_ops[] = {
4862 {
4863 .cmd = NL80211_CMD_GET_WIPHY,
4864 .doit = nl80211_get_wiphy,
4865 .dumpit = nl80211_dump_wiphy,
4866 .policy = nl80211_policy,
4867 /* can be retrieved by unprivileged users */
4868 },
4869 {
4870 .cmd = NL80211_CMD_SET_WIPHY,
4871 .doit = nl80211_set_wiphy,
4872 .policy = nl80211_policy,
4873 .flags = GENL_ADMIN_PERM,
4874 },
4875 {
4876 .cmd = NL80211_CMD_GET_INTERFACE,
4877 .doit = nl80211_get_interface,
4878 .dumpit = nl80211_dump_interface,
4879 .policy = nl80211_policy,
4880 /* can be retrieved by unprivileged users */
4881 },
4882 {
4883 .cmd = NL80211_CMD_SET_INTERFACE,
4884 .doit = nl80211_set_interface,
4885 .policy = nl80211_policy,
4886 .flags = GENL_ADMIN_PERM,
4887 },
4888 {
4889 .cmd = NL80211_CMD_NEW_INTERFACE,
4890 .doit = nl80211_new_interface,
4891 .policy = nl80211_policy,
4892 .flags = GENL_ADMIN_PERM,
4893 },
4894 {
4895 .cmd = NL80211_CMD_DEL_INTERFACE,
4896 .doit = nl80211_del_interface,
4897 .policy = nl80211_policy,
4898 .flags = GENL_ADMIN_PERM,
4899 },
Johannes Berg41ade002007-12-19 02:03:29 +01004900 {
4901 .cmd = NL80211_CMD_GET_KEY,
4902 .doit = nl80211_get_key,
4903 .policy = nl80211_policy,
4904 .flags = GENL_ADMIN_PERM,
4905 },
4906 {
4907 .cmd = NL80211_CMD_SET_KEY,
4908 .doit = nl80211_set_key,
4909 .policy = nl80211_policy,
4910 .flags = GENL_ADMIN_PERM,
4911 },
4912 {
4913 .cmd = NL80211_CMD_NEW_KEY,
4914 .doit = nl80211_new_key,
4915 .policy = nl80211_policy,
4916 .flags = GENL_ADMIN_PERM,
4917 },
4918 {
4919 .cmd = NL80211_CMD_DEL_KEY,
4920 .doit = nl80211_del_key,
4921 .policy = nl80211_policy,
4922 .flags = GENL_ADMIN_PERM,
4923 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004924 {
4925 .cmd = NL80211_CMD_SET_BEACON,
4926 .policy = nl80211_policy,
4927 .flags = GENL_ADMIN_PERM,
4928 .doit = nl80211_addset_beacon,
4929 },
4930 {
4931 .cmd = NL80211_CMD_NEW_BEACON,
4932 .policy = nl80211_policy,
4933 .flags = GENL_ADMIN_PERM,
4934 .doit = nl80211_addset_beacon,
4935 },
4936 {
4937 .cmd = NL80211_CMD_DEL_BEACON,
4938 .policy = nl80211_policy,
4939 .flags = GENL_ADMIN_PERM,
4940 .doit = nl80211_del_beacon,
4941 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004942 {
4943 .cmd = NL80211_CMD_GET_STATION,
4944 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004945 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004946 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004947 },
4948 {
4949 .cmd = NL80211_CMD_SET_STATION,
4950 .doit = nl80211_set_station,
4951 .policy = nl80211_policy,
4952 .flags = GENL_ADMIN_PERM,
4953 },
4954 {
4955 .cmd = NL80211_CMD_NEW_STATION,
4956 .doit = nl80211_new_station,
4957 .policy = nl80211_policy,
4958 .flags = GENL_ADMIN_PERM,
4959 },
4960 {
4961 .cmd = NL80211_CMD_DEL_STATION,
4962 .doit = nl80211_del_station,
4963 .policy = nl80211_policy,
4964 .flags = GENL_ADMIN_PERM,
4965 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004966 {
4967 .cmd = NL80211_CMD_GET_MPATH,
4968 .doit = nl80211_get_mpath,
4969 .dumpit = nl80211_dump_mpath,
4970 .policy = nl80211_policy,
4971 .flags = GENL_ADMIN_PERM,
4972 },
4973 {
4974 .cmd = NL80211_CMD_SET_MPATH,
4975 .doit = nl80211_set_mpath,
4976 .policy = nl80211_policy,
4977 .flags = GENL_ADMIN_PERM,
4978 },
4979 {
4980 .cmd = NL80211_CMD_NEW_MPATH,
4981 .doit = nl80211_new_mpath,
4982 .policy = nl80211_policy,
4983 .flags = GENL_ADMIN_PERM,
4984 },
4985 {
4986 .cmd = NL80211_CMD_DEL_MPATH,
4987 .doit = nl80211_del_mpath,
4988 .policy = nl80211_policy,
4989 .flags = GENL_ADMIN_PERM,
4990 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004991 {
4992 .cmd = NL80211_CMD_SET_BSS,
4993 .doit = nl80211_set_bss,
4994 .policy = nl80211_policy,
4995 .flags = GENL_ADMIN_PERM,
4996 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004997 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004998 .cmd = NL80211_CMD_GET_REG,
4999 .doit = nl80211_get_reg,
5000 .policy = nl80211_policy,
5001 /* can be retrieved by unprivileged users */
5002 },
5003 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005004 .cmd = NL80211_CMD_SET_REG,
5005 .doit = nl80211_set_reg,
5006 .policy = nl80211_policy,
5007 .flags = GENL_ADMIN_PERM,
5008 },
5009 {
5010 .cmd = NL80211_CMD_REQ_SET_REG,
5011 .doit = nl80211_req_set_reg,
5012 .policy = nl80211_policy,
5013 .flags = GENL_ADMIN_PERM,
5014 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005015 {
5016 .cmd = NL80211_CMD_GET_MESH_PARAMS,
5017 .doit = nl80211_get_mesh_params,
5018 .policy = nl80211_policy,
5019 /* can be retrieved by unprivileged users */
5020 },
5021 {
5022 .cmd = NL80211_CMD_SET_MESH_PARAMS,
5023 .doit = nl80211_set_mesh_params,
5024 .policy = nl80211_policy,
5025 .flags = GENL_ADMIN_PERM,
5026 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005027 {
Johannes Berg2a519312009-02-10 21:25:55 +01005028 .cmd = NL80211_CMD_TRIGGER_SCAN,
5029 .doit = nl80211_trigger_scan,
5030 .policy = nl80211_policy,
5031 .flags = GENL_ADMIN_PERM,
5032 },
5033 {
5034 .cmd = NL80211_CMD_GET_SCAN,
5035 .policy = nl80211_policy,
5036 .dumpit = nl80211_dump_scan,
5037 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005038 {
5039 .cmd = NL80211_CMD_AUTHENTICATE,
5040 .doit = nl80211_authenticate,
5041 .policy = nl80211_policy,
5042 .flags = GENL_ADMIN_PERM,
5043 },
5044 {
5045 .cmd = NL80211_CMD_ASSOCIATE,
5046 .doit = nl80211_associate,
5047 .policy = nl80211_policy,
5048 .flags = GENL_ADMIN_PERM,
5049 },
5050 {
5051 .cmd = NL80211_CMD_DEAUTHENTICATE,
5052 .doit = nl80211_deauthenticate,
5053 .policy = nl80211_policy,
5054 .flags = GENL_ADMIN_PERM,
5055 },
5056 {
5057 .cmd = NL80211_CMD_DISASSOCIATE,
5058 .doit = nl80211_disassociate,
5059 .policy = nl80211_policy,
5060 .flags = GENL_ADMIN_PERM,
5061 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005062 {
5063 .cmd = NL80211_CMD_JOIN_IBSS,
5064 .doit = nl80211_join_ibss,
5065 .policy = nl80211_policy,
5066 .flags = GENL_ADMIN_PERM,
5067 },
5068 {
5069 .cmd = NL80211_CMD_LEAVE_IBSS,
5070 .doit = nl80211_leave_ibss,
5071 .policy = nl80211_policy,
5072 .flags = GENL_ADMIN_PERM,
5073 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005074#ifdef CONFIG_NL80211_TESTMODE
5075 {
5076 .cmd = NL80211_CMD_TESTMODE,
5077 .doit = nl80211_testmode_do,
5078 .policy = nl80211_policy,
5079 .flags = GENL_ADMIN_PERM,
5080 },
5081#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005082 {
5083 .cmd = NL80211_CMD_CONNECT,
5084 .doit = nl80211_connect,
5085 .policy = nl80211_policy,
5086 .flags = GENL_ADMIN_PERM,
5087 },
5088 {
5089 .cmd = NL80211_CMD_DISCONNECT,
5090 .doit = nl80211_disconnect,
5091 .policy = nl80211_policy,
5092 .flags = GENL_ADMIN_PERM,
5093 },
Johannes Berg463d0182009-07-14 00:33:35 +02005094 {
5095 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5096 .doit = nl80211_wiphy_netns,
5097 .policy = nl80211_policy,
5098 .flags = GENL_ADMIN_PERM,
5099 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005100 {
5101 .cmd = NL80211_CMD_GET_SURVEY,
5102 .policy = nl80211_policy,
5103 .dumpit = nl80211_dump_survey,
5104 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005105 {
5106 .cmd = NL80211_CMD_SET_PMKSA,
5107 .doit = nl80211_setdel_pmksa,
5108 .policy = nl80211_policy,
5109 .flags = GENL_ADMIN_PERM,
5110 },
5111 {
5112 .cmd = NL80211_CMD_DEL_PMKSA,
5113 .doit = nl80211_setdel_pmksa,
5114 .policy = nl80211_policy,
5115 .flags = GENL_ADMIN_PERM,
5116 },
5117 {
5118 .cmd = NL80211_CMD_FLUSH_PMKSA,
5119 .doit = nl80211_flush_pmksa,
5120 .policy = nl80211_policy,
5121 .flags = GENL_ADMIN_PERM,
5122 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005123 {
5124 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5125 .doit = nl80211_remain_on_channel,
5126 .policy = nl80211_policy,
5127 .flags = GENL_ADMIN_PERM,
5128 },
5129 {
5130 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5131 .doit = nl80211_cancel_remain_on_channel,
5132 .policy = nl80211_policy,
5133 .flags = GENL_ADMIN_PERM,
5134 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005135 {
5136 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5137 .doit = nl80211_set_tx_bitrate_mask,
5138 .policy = nl80211_policy,
5139 .flags = GENL_ADMIN_PERM,
5140 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005141 {
5142 .cmd = NL80211_CMD_REGISTER_ACTION,
5143 .doit = nl80211_register_action,
5144 .policy = nl80211_policy,
5145 .flags = GENL_ADMIN_PERM,
5146 },
5147 {
5148 .cmd = NL80211_CMD_ACTION,
5149 .doit = nl80211_action,
5150 .policy = nl80211_policy,
5151 .flags = GENL_ADMIN_PERM,
5152 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005153 {
5154 .cmd = NL80211_CMD_SET_POWER_SAVE,
5155 .doit = nl80211_set_power_save,
5156 .policy = nl80211_policy,
5157 .flags = GENL_ADMIN_PERM,
5158 },
5159 {
5160 .cmd = NL80211_CMD_GET_POWER_SAVE,
5161 .doit = nl80211_get_power_save,
5162 .policy = nl80211_policy,
5163 /* can be retrieved by unprivileged users */
5164 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005165 {
5166 .cmd = NL80211_CMD_SET_CQM,
5167 .doit = nl80211_set_cqm,
5168 .policy = nl80211_policy,
5169 .flags = GENL_ADMIN_PERM,
5170 },
Johannes Berg55682962007-09-20 13:09:35 -04005171};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005172
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005173static struct genl_multicast_group nl80211_mlme_mcgrp = {
5174 .name = "mlme",
5175};
Johannes Berg55682962007-09-20 13:09:35 -04005176
5177/* multicast groups */
5178static struct genl_multicast_group nl80211_config_mcgrp = {
5179 .name = "config",
5180};
Johannes Berg2a519312009-02-10 21:25:55 +01005181static struct genl_multicast_group nl80211_scan_mcgrp = {
5182 .name = "scan",
5183};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005184static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5185 .name = "regulatory",
5186};
Johannes Berg55682962007-09-20 13:09:35 -04005187
5188/* notification functions */
5189
5190void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5191{
5192 struct sk_buff *msg;
5193
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005195 if (!msg)
5196 return;
5197
5198 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5199 nlmsg_free(msg);
5200 return;
5201 }
5202
Johannes Berg463d0182009-07-14 00:33:35 +02005203 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5204 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005205}
5206
Johannes Berg362a4152009-05-24 16:43:15 +02005207static int nl80211_add_scan_req(struct sk_buff *msg,
5208 struct cfg80211_registered_device *rdev)
5209{
5210 struct cfg80211_scan_request *req = rdev->scan_req;
5211 struct nlattr *nest;
5212 int i;
5213
Johannes Berg667503d2009-07-07 03:56:11 +02005214 ASSERT_RDEV_LOCK(rdev);
5215
Johannes Berg362a4152009-05-24 16:43:15 +02005216 if (WARN_ON(!req))
5217 return 0;
5218
5219 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5220 if (!nest)
5221 goto nla_put_failure;
5222 for (i = 0; i < req->n_ssids; i++)
5223 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5224 nla_nest_end(msg, nest);
5225
5226 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5227 if (!nest)
5228 goto nla_put_failure;
5229 for (i = 0; i < req->n_channels; i++)
5230 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5231 nla_nest_end(msg, nest);
5232
5233 if (req->ie)
5234 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5235
5236 return 0;
5237 nla_put_failure:
5238 return -ENOBUFS;
5239}
5240
Johannes Berga538e2d2009-06-16 19:56:42 +02005241static int nl80211_send_scan_msg(struct sk_buff *msg,
5242 struct cfg80211_registered_device *rdev,
5243 struct net_device *netdev,
5244 u32 pid, u32 seq, int flags,
5245 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005246{
5247 void *hdr;
5248
5249 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5250 if (!hdr)
5251 return -1;
5252
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005253 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005254 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5255
Johannes Berg362a4152009-05-24 16:43:15 +02005256 /* ignore errors and send incomplete event anyway */
5257 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005258
5259 return genlmsg_end(msg, hdr);
5260
5261 nla_put_failure:
5262 genlmsg_cancel(msg, hdr);
5263 return -EMSGSIZE;
5264}
5265
Johannes Berga538e2d2009-06-16 19:56:42 +02005266void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5267 struct net_device *netdev)
5268{
5269 struct sk_buff *msg;
5270
5271 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5272 if (!msg)
5273 return;
5274
5275 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5276 NL80211_CMD_TRIGGER_SCAN) < 0) {
5277 nlmsg_free(msg);
5278 return;
5279 }
5280
Johannes Berg463d0182009-07-14 00:33:35 +02005281 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5282 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005283}
5284
Johannes Berg2a519312009-02-10 21:25:55 +01005285void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5286 struct net_device *netdev)
5287{
5288 struct sk_buff *msg;
5289
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005290 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005291 if (!msg)
5292 return;
5293
Johannes Berga538e2d2009-06-16 19:56:42 +02005294 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5295 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005296 nlmsg_free(msg);
5297 return;
5298 }
5299
Johannes Berg463d0182009-07-14 00:33:35 +02005300 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5301 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005302}
5303
5304void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5305 struct net_device *netdev)
5306{
5307 struct sk_buff *msg;
5308
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005309 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005310 if (!msg)
5311 return;
5312
Johannes Berga538e2d2009-06-16 19:56:42 +02005313 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5314 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005315 nlmsg_free(msg);
5316 return;
5317 }
5318
Johannes Berg463d0182009-07-14 00:33:35 +02005319 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5320 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005321}
5322
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005323/*
5324 * This can happen on global regulatory changes or device specific settings
5325 * based on custom world regulatory domains.
5326 */
5327void nl80211_send_reg_change_event(struct regulatory_request *request)
5328{
5329 struct sk_buff *msg;
5330 void *hdr;
5331
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005332 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005333 if (!msg)
5334 return;
5335
5336 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5337 if (!hdr) {
5338 nlmsg_free(msg);
5339 return;
5340 }
5341
5342 /* Userspace can always count this one always being set */
5343 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5344
5345 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5346 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5347 NL80211_REGDOM_TYPE_WORLD);
5348 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5349 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5350 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5351 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5352 request->intersect)
5353 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5354 NL80211_REGDOM_TYPE_INTERSECTION);
5355 else {
5356 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5357 NL80211_REGDOM_TYPE_COUNTRY);
5358 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5359 }
5360
5361 if (wiphy_idx_valid(request->wiphy_idx))
5362 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5363
5364 if (genlmsg_end(msg, hdr) < 0) {
5365 nlmsg_free(msg);
5366 return;
5367 }
5368
Johannes Bergbc43b282009-07-25 10:54:13 +02005369 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005370 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005371 GFP_ATOMIC);
5372 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005373
5374 return;
5375
5376nla_put_failure:
5377 genlmsg_cancel(msg, hdr);
5378 nlmsg_free(msg);
5379}
5380
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005381static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5382 struct net_device *netdev,
5383 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005384 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005385{
5386 struct sk_buff *msg;
5387 void *hdr;
5388
Johannes Berge6d6e342009-07-01 21:26:47 +02005389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005390 if (!msg)
5391 return;
5392
5393 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5394 if (!hdr) {
5395 nlmsg_free(msg);
5396 return;
5397 }
5398
5399 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5401 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5402
5403 if (genlmsg_end(msg, hdr) < 0) {
5404 nlmsg_free(msg);
5405 return;
5406 }
5407
Johannes Berg463d0182009-07-14 00:33:35 +02005408 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5409 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005410 return;
5411
5412 nla_put_failure:
5413 genlmsg_cancel(msg, hdr);
5414 nlmsg_free(msg);
5415}
5416
5417void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005418 struct net_device *netdev, const u8 *buf,
5419 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005420{
5421 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005422 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005423}
5424
5425void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5426 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005427 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005428{
Johannes Berge6d6e342009-07-01 21:26:47 +02005429 nl80211_send_mlme_event(rdev, netdev, buf, len,
5430 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005431}
5432
Jouni Malinen53b46b82009-03-27 20:53:56 +02005433void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005434 struct net_device *netdev, const u8 *buf,
5435 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005436{
5437 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005438 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005439}
5440
Jouni Malinen53b46b82009-03-27 20:53:56 +02005441void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5442 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005443 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005444{
5445 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005446 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005447}
5448
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005449static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5450 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005451 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005452{
5453 struct sk_buff *msg;
5454 void *hdr;
5455
Johannes Berge6d6e342009-07-01 21:26:47 +02005456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005457 if (!msg)
5458 return;
5459
5460 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5461 if (!hdr) {
5462 nlmsg_free(msg);
5463 return;
5464 }
5465
5466 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5467 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5468 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5469 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5470
5471 if (genlmsg_end(msg, hdr) < 0) {
5472 nlmsg_free(msg);
5473 return;
5474 }
5475
Johannes Berg463d0182009-07-14 00:33:35 +02005476 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5477 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005478 return;
5479
5480 nla_put_failure:
5481 genlmsg_cancel(msg, hdr);
5482 nlmsg_free(msg);
5483}
5484
5485void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005486 struct net_device *netdev, const u8 *addr,
5487 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005488{
5489 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005490 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005491}
5492
5493void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005494 struct net_device *netdev, const u8 *addr,
5495 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005496{
Johannes Berge6d6e342009-07-01 21:26:47 +02005497 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5498 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005499}
5500
Samuel Ortizb23aa672009-07-01 21:26:54 +02005501void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5502 struct net_device *netdev, const u8 *bssid,
5503 const u8 *req_ie, size_t req_ie_len,
5504 const u8 *resp_ie, size_t resp_ie_len,
5505 u16 status, gfp_t gfp)
5506{
5507 struct sk_buff *msg;
5508 void *hdr;
5509
5510 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5511 if (!msg)
5512 return;
5513
5514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5515 if (!hdr) {
5516 nlmsg_free(msg);
5517 return;
5518 }
5519
5520 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5521 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5522 if (bssid)
5523 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5524 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5525 if (req_ie)
5526 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5527 if (resp_ie)
5528 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5529
5530 if (genlmsg_end(msg, hdr) < 0) {
5531 nlmsg_free(msg);
5532 return;
5533 }
5534
Johannes Berg463d0182009-07-14 00:33:35 +02005535 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5536 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005537 return;
5538
5539 nla_put_failure:
5540 genlmsg_cancel(msg, hdr);
5541 nlmsg_free(msg);
5542
5543}
5544
5545void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5546 struct net_device *netdev, const u8 *bssid,
5547 const u8 *req_ie, size_t req_ie_len,
5548 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5549{
5550 struct sk_buff *msg;
5551 void *hdr;
5552
5553 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5554 if (!msg)
5555 return;
5556
5557 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5558 if (!hdr) {
5559 nlmsg_free(msg);
5560 return;
5561 }
5562
5563 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5564 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5565 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5566 if (req_ie)
5567 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5568 if (resp_ie)
5569 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5570
5571 if (genlmsg_end(msg, hdr) < 0) {
5572 nlmsg_free(msg);
5573 return;
5574 }
5575
Johannes Berg463d0182009-07-14 00:33:35 +02005576 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5577 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005578 return;
5579
5580 nla_put_failure:
5581 genlmsg_cancel(msg, hdr);
5582 nlmsg_free(msg);
5583
5584}
5585
5586void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5587 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005588 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005589{
5590 struct sk_buff *msg;
5591 void *hdr;
5592
Johannes Berg667503d2009-07-07 03:56:11 +02005593 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005594 if (!msg)
5595 return;
5596
5597 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5598 if (!hdr) {
5599 nlmsg_free(msg);
5600 return;
5601 }
5602
5603 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5604 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5605 if (from_ap && reason)
5606 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5607 if (from_ap)
5608 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5609 if (ie)
5610 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5611
5612 if (genlmsg_end(msg, hdr) < 0) {
5613 nlmsg_free(msg);
5614 return;
5615 }
5616
Johannes Berg463d0182009-07-14 00:33:35 +02005617 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5618 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005619 return;
5620
5621 nla_put_failure:
5622 genlmsg_cancel(msg, hdr);
5623 nlmsg_free(msg);
5624
5625}
5626
Johannes Berg04a773a2009-04-19 21:24:32 +02005627void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5628 struct net_device *netdev, const u8 *bssid,
5629 gfp_t gfp)
5630{
5631 struct sk_buff *msg;
5632 void *hdr;
5633
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005634 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005635 if (!msg)
5636 return;
5637
5638 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5639 if (!hdr) {
5640 nlmsg_free(msg);
5641 return;
5642 }
5643
5644 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5645 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5646 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5647
5648 if (genlmsg_end(msg, hdr) < 0) {
5649 nlmsg_free(msg);
5650 return;
5651 }
5652
Johannes Berg463d0182009-07-14 00:33:35 +02005653 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5654 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005655 return;
5656
5657 nla_put_failure:
5658 genlmsg_cancel(msg, hdr);
5659 nlmsg_free(msg);
5660}
5661
Jouni Malinena3b8b052009-03-27 21:59:49 +02005662void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5663 struct net_device *netdev, const u8 *addr,
5664 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005665 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005666{
5667 struct sk_buff *msg;
5668 void *hdr;
5669
Johannes Berge6d6e342009-07-01 21:26:47 +02005670 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005671 if (!msg)
5672 return;
5673
5674 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5675 if (!hdr) {
5676 nlmsg_free(msg);
5677 return;
5678 }
5679
5680 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5681 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5682 if (addr)
5683 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5684 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5685 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5686 if (tsc)
5687 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5688
5689 if (genlmsg_end(msg, hdr) < 0) {
5690 nlmsg_free(msg);
5691 return;
5692 }
5693
Johannes Berg463d0182009-07-14 00:33:35 +02005694 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5695 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005696 return;
5697
5698 nla_put_failure:
5699 genlmsg_cancel(msg, hdr);
5700 nlmsg_free(msg);
5701}
5702
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005703void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5704 struct ieee80211_channel *channel_before,
5705 struct ieee80211_channel *channel_after)
5706{
5707 struct sk_buff *msg;
5708 void *hdr;
5709 struct nlattr *nl_freq;
5710
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005711 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005712 if (!msg)
5713 return;
5714
5715 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5716 if (!hdr) {
5717 nlmsg_free(msg);
5718 return;
5719 }
5720
5721 /*
5722 * Since we are applying the beacon hint to a wiphy we know its
5723 * wiphy_idx is valid
5724 */
5725 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5726
5727 /* Before */
5728 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5729 if (!nl_freq)
5730 goto nla_put_failure;
5731 if (nl80211_msg_put_channel(msg, channel_before))
5732 goto nla_put_failure;
5733 nla_nest_end(msg, nl_freq);
5734
5735 /* After */
5736 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5737 if (!nl_freq)
5738 goto nla_put_failure;
5739 if (nl80211_msg_put_channel(msg, channel_after))
5740 goto nla_put_failure;
5741 nla_nest_end(msg, nl_freq);
5742
5743 if (genlmsg_end(msg, hdr) < 0) {
5744 nlmsg_free(msg);
5745 return;
5746 }
5747
Johannes Berg463d0182009-07-14 00:33:35 +02005748 rcu_read_lock();
5749 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5750 GFP_ATOMIC);
5751 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005752
5753 return;
5754
5755nla_put_failure:
5756 genlmsg_cancel(msg, hdr);
5757 nlmsg_free(msg);
5758}
5759
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005760static void nl80211_send_remain_on_chan_event(
5761 int cmd, struct cfg80211_registered_device *rdev,
5762 struct net_device *netdev, u64 cookie,
5763 struct ieee80211_channel *chan,
5764 enum nl80211_channel_type channel_type,
5765 unsigned int duration, gfp_t gfp)
5766{
5767 struct sk_buff *msg;
5768 void *hdr;
5769
5770 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5771 if (!msg)
5772 return;
5773
5774 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5775 if (!hdr) {
5776 nlmsg_free(msg);
5777 return;
5778 }
5779
5780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5782 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5783 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5784 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5785
5786 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5787 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5788
5789 if (genlmsg_end(msg, hdr) < 0) {
5790 nlmsg_free(msg);
5791 return;
5792 }
5793
5794 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5795 nl80211_mlme_mcgrp.id, gfp);
5796 return;
5797
5798 nla_put_failure:
5799 genlmsg_cancel(msg, hdr);
5800 nlmsg_free(msg);
5801}
5802
5803void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5804 struct net_device *netdev, u64 cookie,
5805 struct ieee80211_channel *chan,
5806 enum nl80211_channel_type channel_type,
5807 unsigned int duration, gfp_t gfp)
5808{
5809 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5810 rdev, netdev, cookie, chan,
5811 channel_type, duration, gfp);
5812}
5813
5814void nl80211_send_remain_on_channel_cancel(
5815 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5816 u64 cookie, struct ieee80211_channel *chan,
5817 enum nl80211_channel_type channel_type, gfp_t gfp)
5818{
5819 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5820 rdev, netdev, cookie, chan,
5821 channel_type, 0, gfp);
5822}
5823
Johannes Berg98b62182009-12-23 13:15:44 +01005824void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5825 struct net_device *dev, const u8 *mac_addr,
5826 struct station_info *sinfo, gfp_t gfp)
5827{
5828 struct sk_buff *msg;
5829
5830 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5831 if (!msg)
5832 return;
5833
5834 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5835 nlmsg_free(msg);
5836 return;
5837 }
5838
5839 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5840 nl80211_mlme_mcgrp.id, gfp);
5841}
5842
Jouni Malinen026331c2010-02-15 12:53:10 +02005843int nl80211_send_action(struct cfg80211_registered_device *rdev,
5844 struct net_device *netdev, u32 nlpid,
5845 int freq, const u8 *buf, size_t len, gfp_t gfp)
5846{
5847 struct sk_buff *msg;
5848 void *hdr;
5849 int err;
5850
5851 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5852 if (!msg)
5853 return -ENOMEM;
5854
5855 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION);
5856 if (!hdr) {
5857 nlmsg_free(msg);
5858 return -ENOMEM;
5859 }
5860
5861 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5862 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5863 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5864 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5865
5866 err = genlmsg_end(msg, hdr);
5867 if (err < 0) {
5868 nlmsg_free(msg);
5869 return err;
5870 }
5871
5872 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5873 if (err < 0)
5874 return err;
5875 return 0;
5876
5877 nla_put_failure:
5878 genlmsg_cancel(msg, hdr);
5879 nlmsg_free(msg);
5880 return -ENOBUFS;
5881}
5882
5883void nl80211_send_action_tx_status(struct cfg80211_registered_device *rdev,
5884 struct net_device *netdev, u64 cookie,
5885 const u8 *buf, size_t len, bool ack,
5886 gfp_t gfp)
5887{
5888 struct sk_buff *msg;
5889 void *hdr;
5890
5891 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5892 if (!msg)
5893 return;
5894
5895 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION_TX_STATUS);
5896 if (!hdr) {
5897 nlmsg_free(msg);
5898 return;
5899 }
5900
5901 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5902 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5903 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5904 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5905 if (ack)
5906 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5907
5908 if (genlmsg_end(msg, hdr) < 0) {
5909 nlmsg_free(msg);
5910 return;
5911 }
5912
5913 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5914 return;
5915
5916 nla_put_failure:
5917 genlmsg_cancel(msg, hdr);
5918 nlmsg_free(msg);
5919}
5920
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005921void
5922nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5923 struct net_device *netdev,
5924 enum nl80211_cqm_rssi_threshold_event rssi_event,
5925 gfp_t gfp)
5926{
5927 struct sk_buff *msg;
5928 struct nlattr *pinfoattr;
5929 void *hdr;
5930
5931 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5932 if (!msg)
5933 return;
5934
5935 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5936 if (!hdr) {
5937 nlmsg_free(msg);
5938 return;
5939 }
5940
5941 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5942 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5943
5944 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5945 if (!pinfoattr)
5946 goto nla_put_failure;
5947
5948 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5949 rssi_event);
5950
5951 nla_nest_end(msg, pinfoattr);
5952
5953 if (genlmsg_end(msg, hdr) < 0) {
5954 nlmsg_free(msg);
5955 return;
5956 }
5957
5958 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5959 nl80211_mlme_mcgrp.id, gfp);
5960 return;
5961
5962 nla_put_failure:
5963 genlmsg_cancel(msg, hdr);
5964 nlmsg_free(msg);
5965}
5966
Jouni Malinen026331c2010-02-15 12:53:10 +02005967static int nl80211_netlink_notify(struct notifier_block * nb,
5968 unsigned long state,
5969 void *_notify)
5970{
5971 struct netlink_notify *notify = _notify;
5972 struct cfg80211_registered_device *rdev;
5973 struct wireless_dev *wdev;
5974
5975 if (state != NETLINK_URELEASE)
5976 return NOTIFY_DONE;
5977
5978 rcu_read_lock();
5979
5980 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5981 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
5982 cfg80211_mlme_unregister_actions(wdev, notify->pid);
5983
5984 rcu_read_unlock();
5985
5986 return NOTIFY_DONE;
5987}
5988
5989static struct notifier_block nl80211_netlink_notifier = {
5990 .notifier_call = nl80211_netlink_notify,
5991};
5992
Johannes Berg55682962007-09-20 13:09:35 -04005993/* initialisation/exit functions */
5994
5995int nl80211_init(void)
5996{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005997 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005998
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005999 err = genl_register_family_with_ops(&nl80211_fam,
6000 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006001 if (err)
6002 return err;
6003
Johannes Berg55682962007-09-20 13:09:35 -04006004 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6005 if (err)
6006 goto err_out;
6007
Johannes Berg2a519312009-02-10 21:25:55 +01006008 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6009 if (err)
6010 goto err_out;
6011
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006012 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6013 if (err)
6014 goto err_out;
6015
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006016 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6017 if (err)
6018 goto err_out;
6019
Johannes Bergaff89a92009-07-01 21:26:51 +02006020#ifdef CONFIG_NL80211_TESTMODE
6021 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6022 if (err)
6023 goto err_out;
6024#endif
6025
Jouni Malinen026331c2010-02-15 12:53:10 +02006026 err = netlink_register_notifier(&nl80211_netlink_notifier);
6027 if (err)
6028 goto err_out;
6029
Johannes Berg55682962007-09-20 13:09:35 -04006030 return 0;
6031 err_out:
6032 genl_unregister_family(&nl80211_fam);
6033 return err;
6034}
6035
6036void nl80211_exit(void)
6037{
Jouni Malinen026331c2010-02-15 12:53:10 +02006038 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006039 genl_unregister_family(&nl80211_fam);
6040}