blob: db71150b8040d07d9590a8229b9070b7461d7279 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
26/* the netlink family */
27static struct genl_family nl80211_fam = {
28 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
29 .name = "nl80211", /* have users key off the name instead */
30 .hdrsize = 0, /* no private header */
31 .version = 1, /* no particular meaning now */
32 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020033 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040034};
35
Johannes Berg79c97e92009-07-07 03:56:12 +020036/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020037static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020038 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040039 struct net_device **dev)
40{
Johannes Berg463d0182009-07-14 00:33:35 +020041 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040042 int ifindex;
43
Johannes Bergbba95fe2008-07-29 13:22:51 +020044 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040045 return -EINVAL;
46
Johannes Bergbba95fe2008-07-29 13:22:51 +020047 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020048 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040049 if (!*dev)
50 return -ENODEV;
51
Johannes Berg463d0182009-07-14 00:33:35 +020052 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020053 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040054 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020055 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040056 }
57
58 return 0;
59}
60
61/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000062static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040063 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
64 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070065 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020066 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020067 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053068 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020069 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
71 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
72 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010073 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040074
75 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
76 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
77 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010078
79 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020080 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010081
Johannes Bergb9454e82009-07-08 13:29:08 +020082 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010083 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
84 .len = WLAN_MAX_KEY_LEN },
85 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
86 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
87 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030088 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010089
90 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
91 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
92 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
94 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
95 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010096 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
98 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
99 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
100 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100101 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100102 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200103 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100104 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
105 .len = IEEE80211_MAX_MESH_ID_LEN },
106 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300107
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700108 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
109 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
110
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300111 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
112 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
113 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200114 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
115 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300116
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700117 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
118
Jouni Malinen36aedc92008-08-25 11:58:58 +0300119 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
120 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200121
122 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
123 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
124 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100125 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
126 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200127
128 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
129 .len = IEEE80211_MAX_SSID_LEN },
130 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
131 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200132 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300133 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300134 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300135 [NL80211_ATTR_STA_FLAGS2] = {
136 .len = sizeof(struct nl80211_sta_flag_update),
137 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300138 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200139 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
140 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
141 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200142 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100143 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100144 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
145 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100146 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
147 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200148 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200149 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
150 .len = IEEE80211_MAX_DATA_LEN },
151 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200152 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200153 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300154 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200155 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400156};
157
Johannes Bergb9454e82009-07-08 13:29:08 +0200158/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000159static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200160 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200161 [NL80211_KEY_IDX] = { .type = NLA_U8 },
162 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
163 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
164 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
165 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
166};
167
Holger Schuriga0438972009-11-11 11:30:02 +0100168/* ifidx get helper */
169static int nl80211_get_ifidx(struct netlink_callback *cb)
170{
171 int res;
172
173 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
174 nl80211_fam.attrbuf, nl80211_fam.maxattr,
175 nl80211_policy);
176 if (res)
177 return res;
178
179 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
180 return -EINVAL;
181
182 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
183 if (!res)
184 return -EINVAL;
185 return res;
186}
187
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100188/* IE validation */
189static bool is_valid_ie_attr(const struct nlattr *attr)
190{
191 const u8 *pos;
192 int len;
193
194 if (!attr)
195 return true;
196
197 pos = nla_data(attr);
198 len = nla_len(attr);
199
200 while (len) {
201 u8 elemlen;
202
203 if (len < 2)
204 return false;
205 len -= 2;
206
207 elemlen = pos[1];
208 if (elemlen > len)
209 return false;
210
211 len -= elemlen;
212 pos += 2 + elemlen;
213 }
214
215 return true;
216}
217
Johannes Berg55682962007-09-20 13:09:35 -0400218/* message building helper */
219static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
220 int flags, u8 cmd)
221{
222 /* since there is no private header just add the generic one */
223 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
224}
225
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400226static int nl80211_msg_put_channel(struct sk_buff *msg,
227 struct ieee80211_channel *chan)
228{
229 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
230 chan->center_freq);
231
232 if (chan->flags & IEEE80211_CHAN_DISABLED)
233 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
234 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
235 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
236 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
237 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
238 if (chan->flags & IEEE80211_CHAN_RADAR)
239 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
240
241 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
242 DBM_TO_MBM(chan->max_power));
243
244 return 0;
245
246 nla_put_failure:
247 return -ENOBUFS;
248}
249
Johannes Berg55682962007-09-20 13:09:35 -0400250/* netlink command implementations */
251
Johannes Bergb9454e82009-07-08 13:29:08 +0200252struct key_parse {
253 struct key_params p;
254 int idx;
255 bool def, defmgmt;
256};
257
258static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
259{
260 struct nlattr *tb[NL80211_KEY_MAX + 1];
261 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
262 nl80211_key_policy);
263 if (err)
264 return err;
265
266 k->def = !!tb[NL80211_KEY_DEFAULT];
267 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
268
269 if (tb[NL80211_KEY_IDX])
270 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
271
272 if (tb[NL80211_KEY_DATA]) {
273 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
274 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
275 }
276
277 if (tb[NL80211_KEY_SEQ]) {
278 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
279 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
280 }
281
282 if (tb[NL80211_KEY_CIPHER])
283 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
284
285 return 0;
286}
287
288static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
289{
290 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
291 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
292 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
293 }
294
295 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
296 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
297 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
298 }
299
300 if (info->attrs[NL80211_ATTR_KEY_IDX])
301 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
302
303 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
304 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
305
306 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
307 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
308
309 return 0;
310}
311
312static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
313{
314 int err;
315
316 memset(k, 0, sizeof(*k));
317 k->idx = -1;
318
319 if (info->attrs[NL80211_ATTR_KEY])
320 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
321 else
322 err = nl80211_parse_key_old(info, k);
323
324 if (err)
325 return err;
326
327 if (k->def && k->defmgmt)
328 return -EINVAL;
329
330 if (k->idx != -1) {
331 if (k->defmgmt) {
332 if (k->idx < 4 || k->idx > 5)
333 return -EINVAL;
334 } else if (k->def) {
335 if (k->idx < 0 || k->idx > 3)
336 return -EINVAL;
337 } else {
338 if (k->idx < 0 || k->idx > 5)
339 return -EINVAL;
340 }
341 }
342
343 return 0;
344}
345
Johannes Bergfffd0932009-07-08 14:22:54 +0200346static struct cfg80211_cached_keys *
347nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
348 struct nlattr *keys)
349{
350 struct key_parse parse;
351 struct nlattr *key;
352 struct cfg80211_cached_keys *result;
353 int rem, err, def = 0;
354
355 result = kzalloc(sizeof(*result), GFP_KERNEL);
356 if (!result)
357 return ERR_PTR(-ENOMEM);
358
359 result->def = -1;
360 result->defmgmt = -1;
361
362 nla_for_each_nested(key, keys, rem) {
363 memset(&parse, 0, sizeof(parse));
364 parse.idx = -1;
365
366 err = nl80211_parse_key_new(key, &parse);
367 if (err)
368 goto error;
369 err = -EINVAL;
370 if (!parse.p.key)
371 goto error;
372 if (parse.idx < 0 || parse.idx > 4)
373 goto error;
374 if (parse.def) {
375 if (def)
376 goto error;
377 def = 1;
378 result->def = parse.idx;
379 } else if (parse.defmgmt)
380 goto error;
381 err = cfg80211_validate_key_settings(rdev, &parse.p,
382 parse.idx, NULL);
383 if (err)
384 goto error;
385 result->params[parse.idx].cipher = parse.p.cipher;
386 result->params[parse.idx].key_len = parse.p.key_len;
387 result->params[parse.idx].key = result->data[parse.idx];
388 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
389 }
390
391 return result;
392 error:
393 kfree(result);
394 return ERR_PTR(err);
395}
396
397static int nl80211_key_allowed(struct wireless_dev *wdev)
398{
399 ASSERT_WDEV_LOCK(wdev);
400
401 if (!netif_running(wdev->netdev))
402 return -ENETDOWN;
403
404 switch (wdev->iftype) {
405 case NL80211_IFTYPE_AP:
406 case NL80211_IFTYPE_AP_VLAN:
407 break;
408 case NL80211_IFTYPE_ADHOC:
409 if (!wdev->current_bss)
410 return -ENOLINK;
411 break;
412 case NL80211_IFTYPE_STATION:
413 if (wdev->sme_state != CFG80211_SME_CONNECTED)
414 return -ENOLINK;
415 break;
416 default:
417 return -EINVAL;
418 }
419
420 return 0;
421}
422
Johannes Berg55682962007-09-20 13:09:35 -0400423static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
424 struct cfg80211_registered_device *dev)
425{
426 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100427 struct nlattr *nl_bands, *nl_band;
428 struct nlattr *nl_freqs, *nl_freq;
429 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700430 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100431 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100432 enum ieee80211_band band;
433 struct ieee80211_channel *chan;
434 struct ieee80211_rate *rate;
435 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700436 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400437
438 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
439 if (!hdr)
440 return -1;
441
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500442 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400443 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200444
Johannes Bergf5ea9122009-08-07 16:17:38 +0200445 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
446 cfg80211_rdev_list_generation);
447
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200448 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
449 dev->wiphy.retry_short);
450 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
451 dev->wiphy.retry_long);
452 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
453 dev->wiphy.frag_threshold);
454 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
455 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100456 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
457 dev->wiphy.coverage_class);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200458
Johannes Berg2a519312009-02-10 21:25:55 +0100459 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
460 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200461 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
462 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100463
Johannes Berg25e47c182009-04-02 20:14:06 +0200464 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
465 sizeof(u32) * dev->wiphy.n_cipher_suites,
466 dev->wiphy.cipher_suites);
467
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100468 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
469 dev->wiphy.max_num_pmkids);
470
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700471 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
472 if (!nl_modes)
473 goto nla_put_failure;
474
475 i = 0;
476 while (ifmodes) {
477 if (ifmodes & 1)
478 NLA_PUT_FLAG(msg, i);
479 ifmodes >>= 1;
480 i++;
481 }
482
483 nla_nest_end(msg, nl_modes);
484
Johannes Bergee688b002008-01-24 19:38:39 +0100485 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
486 if (!nl_bands)
487 goto nla_put_failure;
488
489 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
490 if (!dev->wiphy.bands[band])
491 continue;
492
493 nl_band = nla_nest_start(msg, band);
494 if (!nl_band)
495 goto nla_put_failure;
496
Johannes Bergd51626d2008-10-09 12:20:13 +0200497 /* add HT info */
498 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
499 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
500 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
501 &dev->wiphy.bands[band]->ht_cap.mcs);
502 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
503 dev->wiphy.bands[band]->ht_cap.cap);
504 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
505 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
506 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
507 dev->wiphy.bands[band]->ht_cap.ampdu_density);
508 }
509
Johannes Bergee688b002008-01-24 19:38:39 +0100510 /* add frequencies */
511 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
512 if (!nl_freqs)
513 goto nla_put_failure;
514
515 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
516 nl_freq = nla_nest_start(msg, i);
517 if (!nl_freq)
518 goto nla_put_failure;
519
520 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100521
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400522 if (nl80211_msg_put_channel(msg, chan))
523 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200524
Johannes Bergee688b002008-01-24 19:38:39 +0100525 nla_nest_end(msg, nl_freq);
526 }
527
528 nla_nest_end(msg, nl_freqs);
529
530 /* add bitrates */
531 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
532 if (!nl_rates)
533 goto nla_put_failure;
534
535 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
536 nl_rate = nla_nest_start(msg, i);
537 if (!nl_rate)
538 goto nla_put_failure;
539
540 rate = &dev->wiphy.bands[band]->bitrates[i];
541 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
542 rate->bitrate);
543 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
544 NLA_PUT_FLAG(msg,
545 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
546
547 nla_nest_end(msg, nl_rate);
548 }
549
550 nla_nest_end(msg, nl_rates);
551
552 nla_nest_end(msg, nl_band);
553 }
554 nla_nest_end(msg, nl_bands);
555
Johannes Berg8fdc6212009-03-14 09:34:01 +0100556 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
557 if (!nl_cmds)
558 goto nla_put_failure;
559
560 i = 0;
561#define CMD(op, n) \
562 do { \
563 if (dev->ops->op) { \
564 i++; \
565 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
566 } \
567 } while (0)
568
569 CMD(add_virtual_intf, NEW_INTERFACE);
570 CMD(change_virtual_intf, SET_INTERFACE);
571 CMD(add_key, NEW_KEY);
572 CMD(add_beacon, NEW_BEACON);
573 CMD(add_station, NEW_STATION);
574 CMD(add_mpath, NEW_MPATH);
575 CMD(set_mesh_params, SET_MESH_PARAMS);
576 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200577 CMD(auth, AUTHENTICATE);
578 CMD(assoc, ASSOCIATE);
579 CMD(deauth, DEAUTHENTICATE);
580 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200581 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100582 CMD(set_pmksa, SET_PMKSA);
583 CMD(del_pmksa, DEL_PMKSA);
584 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100585 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200586 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Jouni Malinen026331c2010-02-15 12:53:10 +0200587 CMD(action, ACTION);
Johannes Berg5be83de2009-11-19 00:56:28 +0100588 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200589 i++;
590 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
591 }
Johannes Bergf444de02010-05-05 15:25:02 +0200592 CMD(set_channel, SET_CHANNEL);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100593
594#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200595
Johannes Berg6829c8782009-07-02 09:13:27 +0200596 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200597 i++;
598 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
599 }
600
Johannes Berg6829c8782009-07-02 09:13:27 +0200601 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200602 i++;
603 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
604 }
605
Johannes Berg8fdc6212009-03-14 09:34:01 +0100606 nla_nest_end(msg, nl_cmds);
607
Johannes Berg55682962007-09-20 13:09:35 -0400608 return genlmsg_end(msg, hdr);
609
610 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700611 genlmsg_cancel(msg, hdr);
612 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400613}
614
615static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
616{
617 int idx = 0;
618 int start = cb->args[0];
619 struct cfg80211_registered_device *dev;
620
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500621 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200622 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200623 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
624 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200625 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400626 continue;
627 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
628 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200629 dev) < 0) {
630 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400631 break;
Julius Volzb4637272008-07-08 14:02:19 +0200632 }
Johannes Berg55682962007-09-20 13:09:35 -0400633 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500634 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400635
636 cb->args[0] = idx;
637
638 return skb->len;
639}
640
641static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
642{
643 struct sk_buff *msg;
644 struct cfg80211_registered_device *dev;
645
646 dev = cfg80211_get_dev_from_info(info);
647 if (IS_ERR(dev))
648 return PTR_ERR(dev);
649
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700650 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400651 if (!msg)
652 goto out_err;
653
654 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
655 goto out_free;
656
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200657 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400658
Johannes Berg134e6372009-07-10 09:51:34 +0000659 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400660
661 out_free:
662 nlmsg_free(msg);
663 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200664 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400665 return -ENOBUFS;
666}
667
Jouni Malinen31888482008-10-30 16:59:24 +0200668static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
669 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
670 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
671 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
672 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
673 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
674};
675
676static int parse_txq_params(struct nlattr *tb[],
677 struct ieee80211_txq_params *txq_params)
678{
679 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
680 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
681 !tb[NL80211_TXQ_ATTR_AIFS])
682 return -EINVAL;
683
684 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
685 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
686 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
687 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
688 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
689
690 return 0;
691}
692
Johannes Bergf444de02010-05-05 15:25:02 +0200693static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
694{
695 /*
696 * You can only set the channel explicitly for AP, mesh
697 * and WDS type interfaces; all others have their channel
698 * managed via their respective "establish a connection"
699 * command (connect, join, ...)
700 *
701 * Monitors are special as they are normally slaved to
702 * whatever else is going on, so they behave as though
703 * you tried setting the wiphy channel itself.
704 */
705 return !wdev ||
706 wdev->iftype == NL80211_IFTYPE_AP ||
707 wdev->iftype == NL80211_IFTYPE_WDS ||
708 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
709 wdev->iftype == NL80211_IFTYPE_MONITOR;
710}
711
712static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
713 struct wireless_dev *wdev,
714 struct genl_info *info)
715{
716 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
717 u32 freq;
718 int result;
719
720 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
721 return -EINVAL;
722
723 if (!nl80211_can_set_dev_channel(wdev))
724 return -EOPNOTSUPP;
725
726 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
727 channel_type = nla_get_u32(info->attrs[
728 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
729 if (channel_type != NL80211_CHAN_NO_HT &&
730 channel_type != NL80211_CHAN_HT20 &&
731 channel_type != NL80211_CHAN_HT40PLUS &&
732 channel_type != NL80211_CHAN_HT40MINUS)
733 return -EINVAL;
734 }
735
736 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
737
738 mutex_lock(&rdev->devlist_mtx);
739 if (wdev) {
740 wdev_lock(wdev);
741 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
742 wdev_unlock(wdev);
743 } else {
744 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
745 }
746 mutex_unlock(&rdev->devlist_mtx);
747
748 return result;
749}
750
751static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
752{
753 struct cfg80211_registered_device *rdev;
754 struct net_device *netdev;
755 int result;
756
757 rtnl_lock();
758
759 result = get_rdev_dev_by_info_ifindex(info, &rdev, &netdev);
760 if (result)
761 goto unlock;
762
763 result = __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
764
765 unlock:
766 rtnl_unlock();
767
768 return result;
769}
770
Johannes Berg55682962007-09-20 13:09:35 -0400771static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
772{
773 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200774 struct net_device *netdev = NULL;
775 struct wireless_dev *wdev;
776 int result, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200777 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200778 u32 changed;
779 u8 retry_short = 0, retry_long = 0;
780 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100781 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400782
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100783 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400784
Johannes Bergf444de02010-05-05 15:25:02 +0200785 /*
786 * Try to find the wiphy and netdev. Normally this
787 * function shouldn't need the netdev, but this is
788 * done for backward compatibility -- previously
789 * setting the channel was done per wiphy, but now
790 * it is per netdev. Previous userland like hostapd
791 * also passed a netdev to set_wiphy, so that it is
792 * possible to let that go to the right netdev!
793 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100794 mutex_lock(&cfg80211_mutex);
795
Johannes Bergf444de02010-05-05 15:25:02 +0200796 if (info->attrs[NL80211_ATTR_IFINDEX]) {
797 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
798
799 netdev = dev_get_by_index(genl_info_net(info), ifindex);
800 if (netdev && netdev->ieee80211_ptr) {
801 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
802 mutex_lock(&rdev->mtx);
803 } else
804 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100805 }
806
Johannes Bergf444de02010-05-05 15:25:02 +0200807 if (!netdev) {
808 rdev = __cfg80211_rdev_from_info(info);
809 if (IS_ERR(rdev)) {
810 mutex_unlock(&cfg80211_mutex);
811 result = PTR_ERR(rdev);
812 goto unlock;
813 }
814 wdev = NULL;
815 netdev = NULL;
816 result = 0;
817
818 mutex_lock(&rdev->mtx);
819 } else if (netif_running(netdev) &&
820 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
821 wdev = netdev->ieee80211_ptr;
822 else
823 wdev = NULL;
824
825 /*
826 * end workaround code, by now the rdev is available
827 * and locked, and wdev may or may not be NULL.
828 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100829
830 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200831 result = cfg80211_dev_rename(
832 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100833
834 mutex_unlock(&cfg80211_mutex);
835
836 if (result)
837 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400838
Jouni Malinen31888482008-10-30 16:59:24 +0200839 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
840 struct ieee80211_txq_params txq_params;
841 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
842
843 if (!rdev->ops->set_txq_params) {
844 result = -EOPNOTSUPP;
845 goto bad_res;
846 }
847
848 nla_for_each_nested(nl_txq_params,
849 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
850 rem_txq_params) {
851 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
852 nla_data(nl_txq_params),
853 nla_len(nl_txq_params),
854 txq_params_policy);
855 result = parse_txq_params(tb, &txq_params);
856 if (result)
857 goto bad_res;
858
859 result = rdev->ops->set_txq_params(&rdev->wiphy,
860 &txq_params);
861 if (result)
862 goto bad_res;
863 }
864 }
865
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200866 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +0200867 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200868 if (result)
869 goto bad_res;
870 }
871
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200872 changed = 0;
873
874 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
875 retry_short = nla_get_u8(
876 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
877 if (retry_short == 0) {
878 result = -EINVAL;
879 goto bad_res;
880 }
881 changed |= WIPHY_PARAM_RETRY_SHORT;
882 }
883
884 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
885 retry_long = nla_get_u8(
886 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
887 if (retry_long == 0) {
888 result = -EINVAL;
889 goto bad_res;
890 }
891 changed |= WIPHY_PARAM_RETRY_LONG;
892 }
893
894 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
895 frag_threshold = nla_get_u32(
896 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
897 if (frag_threshold < 256) {
898 result = -EINVAL;
899 goto bad_res;
900 }
901 if (frag_threshold != (u32) -1) {
902 /*
903 * Fragments (apart from the last one) are required to
904 * have even length. Make the fragmentation code
905 * simpler by stripping LSB should someone try to use
906 * odd threshold value.
907 */
908 frag_threshold &= ~0x1;
909 }
910 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
911 }
912
913 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
914 rts_threshold = nla_get_u32(
915 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
916 changed |= WIPHY_PARAM_RTS_THRESHOLD;
917 }
918
Lukáš Turek81077e82009-12-21 22:50:47 +0100919 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
920 coverage_class = nla_get_u8(
921 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
922 changed |= WIPHY_PARAM_COVERAGE_CLASS;
923 }
924
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200925 if (changed) {
926 u8 old_retry_short, old_retry_long;
927 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100928 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200929
930 if (!rdev->ops->set_wiphy_params) {
931 result = -EOPNOTSUPP;
932 goto bad_res;
933 }
934
935 old_retry_short = rdev->wiphy.retry_short;
936 old_retry_long = rdev->wiphy.retry_long;
937 old_frag_threshold = rdev->wiphy.frag_threshold;
938 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100939 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200940
941 if (changed & WIPHY_PARAM_RETRY_SHORT)
942 rdev->wiphy.retry_short = retry_short;
943 if (changed & WIPHY_PARAM_RETRY_LONG)
944 rdev->wiphy.retry_long = retry_long;
945 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
946 rdev->wiphy.frag_threshold = frag_threshold;
947 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
948 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100949 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
950 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200951
952 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
953 if (result) {
954 rdev->wiphy.retry_short = old_retry_short;
955 rdev->wiphy.retry_long = old_retry_long;
956 rdev->wiphy.frag_threshold = old_frag_threshold;
957 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +0100958 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200959 }
960 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200961
Johannes Berg306d6112008-12-08 12:39:04 +0100962 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100963 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +0200964 if (netdev)
965 dev_put(netdev);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100966 unlock:
967 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400968 return result;
969}
970
971
972static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200973 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400974 struct net_device *dev)
975{
976 void *hdr;
977
978 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
979 if (!hdr)
980 return -1;
981
982 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200983 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400984 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200985 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200986
987 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
988 rdev->devlist_generation ^
989 (cfg80211_rdev_list_generation << 2));
990
Johannes Berg55682962007-09-20 13:09:35 -0400991 return genlmsg_end(msg, hdr);
992
993 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700994 genlmsg_cancel(msg, hdr);
995 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400996}
997
998static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
999{
1000 int wp_idx = 0;
1001 int if_idx = 0;
1002 int wp_start = cb->args[0];
1003 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001004 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001005 struct wireless_dev *wdev;
1006
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001007 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001008 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1009 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001010 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001011 if (wp_idx < wp_start) {
1012 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001013 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001014 }
Johannes Berg55682962007-09-20 13:09:35 -04001015 if_idx = 0;
1016
Johannes Bergf5ea9122009-08-07 16:17:38 +02001017 mutex_lock(&rdev->devlist_mtx);
1018 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001019 if (if_idx < if_start) {
1020 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001021 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001022 }
Johannes Berg55682962007-09-20 13:09:35 -04001023 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1024 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001025 rdev, wdev->netdev) < 0) {
1026 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001027 goto out;
1028 }
1029 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001030 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001031 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001032
1033 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001034 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001035 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001036 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001037
1038 cb->args[0] = wp_idx;
1039 cb->args[1] = if_idx;
1040
1041 return skb->len;
1042}
1043
1044static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1045{
1046 struct sk_buff *msg;
1047 struct cfg80211_registered_device *dev;
1048 struct net_device *netdev;
1049 int err;
1050
Johannes Berg463d0182009-07-14 00:33:35 +02001051 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001052 if (err)
1053 return err;
1054
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001055 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001056 if (!msg)
1057 goto out_err;
1058
Johannes Bergd7264052009-04-19 16:23:20 +02001059 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1060 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -04001061 goto out_free;
1062
1063 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001064 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -04001065
Johannes Berg134e6372009-07-10 09:51:34 +00001066 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001067
1068 out_free:
1069 nlmsg_free(msg);
1070 out_err:
1071 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001072 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -04001073 return -ENOBUFS;
1074}
1075
Michael Wu66f7ac52008-01-31 19:48:22 +01001076static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1077 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1078 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1079 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1080 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1081 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1082};
1083
1084static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1085{
1086 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1087 int flag;
1088
1089 *mntrflags = 0;
1090
1091 if (!nla)
1092 return -EINVAL;
1093
1094 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1095 nla, mntr_flags_policy))
1096 return -EINVAL;
1097
1098 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1099 if (flags[flag])
1100 *mntrflags |= (1<<flag);
1101
1102 return 0;
1103}
1104
Johannes Berg9bc383d2009-11-19 11:55:19 +01001105static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001106 struct net_device *netdev, u8 use_4addr,
1107 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001108{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001109 if (!use_4addr) {
1110 if (netdev && netdev->br_port)
1111 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001112 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001113 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001114
1115 switch (iftype) {
1116 case NL80211_IFTYPE_AP_VLAN:
1117 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1118 return 0;
1119 break;
1120 case NL80211_IFTYPE_STATION:
1121 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1122 return 0;
1123 break;
1124 default:
1125 break;
1126 }
1127
1128 return -EOPNOTSUPP;
1129}
1130
Johannes Berg55682962007-09-20 13:09:35 -04001131static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1132{
Johannes Berg79c97e92009-07-07 03:56:12 +02001133 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001134 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001135 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001136 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001137 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001138 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001139 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001140
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001141 memset(&params, 0, sizeof(params));
1142
Johannes Berg3b858752009-03-12 09:55:09 +01001143 rtnl_lock();
1144
Johannes Berg463d0182009-07-14 00:33:35 +02001145 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001146 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001147 goto unlock_rtnl;
1148
Johannes Berg04a773a2009-04-19 21:24:32 +02001149 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001150
Johannes Berg723b0382008-09-16 20:22:09 +02001151 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001152 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001153 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001154 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001155 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001156 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001157 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001158 }
Johannes Berg723b0382008-09-16 20:22:09 +02001159 }
1160
Johannes Berg92ffe052008-09-16 20:39:36 +02001161 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001162 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001163 err = -EINVAL;
1164 goto unlock;
1165 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001166 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1167 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001168 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001169 }
1170
Felix Fietkau8b787642009-11-10 18:53:10 +01001171 if (info->attrs[NL80211_ATTR_4ADDR]) {
1172 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1173 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001174 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001175 if (err)
1176 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001177 } else {
1178 params.use_4addr = -1;
1179 }
1180
Johannes Berg92ffe052008-09-16 20:39:36 +02001181 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001182 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001183 err = -EINVAL;
1184 goto unlock;
1185 }
1186 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1187 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001188 if (err)
1189 goto unlock;
1190
1191 flags = &_flags;
1192 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001193 }
Johannes Berg3b858752009-03-12 09:55:09 +01001194
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001195 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001196 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001197 else
1198 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001199
Johannes Berg9bc383d2009-11-19 11:55:19 +01001200 if (!err && params.use_4addr != -1)
1201 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1202
Johannes Berg55682962007-09-20 13:09:35 -04001203 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001204 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001205 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001206 unlock_rtnl:
1207 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001208 return err;
1209}
1210
1211static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1212{
Johannes Berg79c97e92009-07-07 03:56:12 +02001213 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001214 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001215 int err;
1216 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001217 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001218
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001219 memset(&params, 0, sizeof(params));
1220
Johannes Berg55682962007-09-20 13:09:35 -04001221 if (!info->attrs[NL80211_ATTR_IFNAME])
1222 return -EINVAL;
1223
1224 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1225 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1226 if (type > NL80211_IFTYPE_MAX)
1227 return -EINVAL;
1228 }
1229
Johannes Berg3b858752009-03-12 09:55:09 +01001230 rtnl_lock();
1231
Johannes Berg79c97e92009-07-07 03:56:12 +02001232 rdev = cfg80211_get_dev_from_info(info);
1233 if (IS_ERR(rdev)) {
1234 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001235 goto unlock_rtnl;
1236 }
Johannes Berg55682962007-09-20 13:09:35 -04001237
Johannes Berg79c97e92009-07-07 03:56:12 +02001238 if (!rdev->ops->add_virtual_intf ||
1239 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001240 err = -EOPNOTSUPP;
1241 goto unlock;
1242 }
1243
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001244 if (type == NL80211_IFTYPE_MESH_POINT &&
1245 info->attrs[NL80211_ATTR_MESH_ID]) {
1246 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1247 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1248 }
1249
Johannes Berg9bc383d2009-11-19 11:55:19 +01001250 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001251 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001252 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001253 if (err)
1254 goto unlock;
1255 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001256
Michael Wu66f7ac52008-01-31 19:48:22 +01001257 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1258 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1259 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001260 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001261 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001262 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001263
Johannes Berg55682962007-09-20 13:09:35 -04001264 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001265 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001266 unlock_rtnl:
1267 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001268 return err;
1269}
1270
1271static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1272{
Johannes Berg79c97e92009-07-07 03:56:12 +02001273 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001274 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001275 struct net_device *dev;
1276
Johannes Berg3b858752009-03-12 09:55:09 +01001277 rtnl_lock();
1278
Johannes Berg463d0182009-07-14 00:33:35 +02001279 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001280 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001281 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001282
Johannes Berg79c97e92009-07-07 03:56:12 +02001283 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001284 err = -EOPNOTSUPP;
1285 goto out;
1286 }
1287
Johannes Berg463d0182009-07-14 00:33:35 +02001288 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001289
1290 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001291 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001292 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001293 unlock_rtnl:
1294 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001295 return err;
1296}
1297
Johannes Berg41ade002007-12-19 02:03:29 +01001298struct get_key_cookie {
1299 struct sk_buff *msg;
1300 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001301 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001302};
1303
1304static void get_key_callback(void *c, struct key_params *params)
1305{
Johannes Bergb9454e82009-07-08 13:29:08 +02001306 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001307 struct get_key_cookie *cookie = c;
1308
1309 if (params->key)
1310 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1311 params->key_len, params->key);
1312
1313 if (params->seq)
1314 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1315 params->seq_len, params->seq);
1316
1317 if (params->cipher)
1318 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1319 params->cipher);
1320
Johannes Bergb9454e82009-07-08 13:29:08 +02001321 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1322 if (!key)
1323 goto nla_put_failure;
1324
1325 if (params->key)
1326 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1327 params->key_len, params->key);
1328
1329 if (params->seq)
1330 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1331 params->seq_len, params->seq);
1332
1333 if (params->cipher)
1334 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1335 params->cipher);
1336
1337 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1338
1339 nla_nest_end(cookie->msg, key);
1340
Johannes Berg41ade002007-12-19 02:03:29 +01001341 return;
1342 nla_put_failure:
1343 cookie->error = 1;
1344}
1345
1346static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1347{
Johannes Berg79c97e92009-07-07 03:56:12 +02001348 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001349 int err;
1350 struct net_device *dev;
1351 u8 key_idx = 0;
1352 u8 *mac_addr = NULL;
1353 struct get_key_cookie cookie = {
1354 .error = 0,
1355 };
1356 void *hdr;
1357 struct sk_buff *msg;
1358
1359 if (info->attrs[NL80211_ATTR_KEY_IDX])
1360 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1361
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001362 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001363 return -EINVAL;
1364
1365 if (info->attrs[NL80211_ATTR_MAC])
1366 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1367
Johannes Berg3b858752009-03-12 09:55:09 +01001368 rtnl_lock();
1369
Johannes Berg463d0182009-07-14 00:33:35 +02001370 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001371 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001372 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001373
Johannes Berg79c97e92009-07-07 03:56:12 +02001374 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001375 err = -EOPNOTSUPP;
1376 goto out;
1377 }
1378
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001380 if (!msg) {
1381 err = -ENOMEM;
1382 goto out;
1383 }
1384
1385 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1386 NL80211_CMD_NEW_KEY);
1387
1388 if (IS_ERR(hdr)) {
1389 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001390 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001391 }
1392
1393 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001394 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001395
1396 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1397 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1398 if (mac_addr)
1399 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1400
Johannes Berg79c97e92009-07-07 03:56:12 +02001401 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001402 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001403
1404 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001405 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001406
1407 if (cookie.error)
1408 goto nla_put_failure;
1409
1410 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001411 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001412 goto out;
1413
1414 nla_put_failure:
1415 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001416 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001417 nlmsg_free(msg);
1418 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001419 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001420 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001421 unlock_rtnl:
1422 rtnl_unlock();
1423
Johannes Berg41ade002007-12-19 02:03:29 +01001424 return err;
1425}
1426
1427static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1428{
Johannes Berg79c97e92009-07-07 03:56:12 +02001429 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001430 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001431 int err;
1432 struct net_device *dev;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001433 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1434 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001435
Johannes Bergb9454e82009-07-08 13:29:08 +02001436 err = nl80211_parse_key(info, &key);
1437 if (err)
1438 return err;
1439
1440 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001441 return -EINVAL;
1442
Johannes Bergb9454e82009-07-08 13:29:08 +02001443 /* only support setting default key */
1444 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001445 return -EINVAL;
1446
Johannes Berg3b858752009-03-12 09:55:09 +01001447 rtnl_lock();
1448
Johannes Berg463d0182009-07-14 00:33:35 +02001449 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001450 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001451 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001452
Johannes Bergb9454e82009-07-08 13:29:08 +02001453 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001454 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001455 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001456 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001457
1458 if (!func) {
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 = func(&rdev->wiphy, dev, key.idx);
1467
Johannes Berg3d23e342009-09-29 23:27:28 +02001468#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001469 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001470 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001471 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001472 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001473 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001474 }
1475#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001476 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001477
1478 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
1488static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1489{
Johannes Berg79c97e92009-07-07 03:56:12 +02001490 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001491 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001492 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001493 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001494 u8 *mac_addr = NULL;
1495
Johannes Bergb9454e82009-07-08 13:29:08 +02001496 err = nl80211_parse_key(info, &key);
1497 if (err)
1498 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001499
Johannes Bergb9454e82009-07-08 13:29:08 +02001500 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001501 return -EINVAL;
1502
Johannes Berg41ade002007-12-19 02:03:29 +01001503 if (info->attrs[NL80211_ATTR_MAC])
1504 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1505
Johannes Berg3b858752009-03-12 09:55:09 +01001506 rtnl_lock();
1507
Johannes Berg463d0182009-07-14 00:33:35 +02001508 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001509 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001510 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001511
Johannes Berg79c97e92009-07-07 03:56:12 +02001512 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001513 err = -EOPNOTSUPP;
1514 goto out;
1515 }
1516
Johannes Bergfffd0932009-07-08 14:22:54 +02001517 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1518 err = -EINVAL;
1519 goto out;
1520 }
1521
1522 wdev_lock(dev->ieee80211_ptr);
1523 err = nl80211_key_allowed(dev->ieee80211_ptr);
1524 if (!err)
1525 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1526 mac_addr, &key.p);
1527 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001528
1529 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001530 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001531 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001532 unlock_rtnl:
1533 rtnl_unlock();
1534
Johannes Berg41ade002007-12-19 02:03:29 +01001535 return err;
1536}
1537
1538static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1539{
Johannes Berg79c97e92009-07-07 03:56:12 +02001540 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001541 int err;
1542 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001543 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001544 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001545
Johannes Bergb9454e82009-07-08 13:29:08 +02001546 err = nl80211_parse_key(info, &key);
1547 if (err)
1548 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001549
1550 if (info->attrs[NL80211_ATTR_MAC])
1551 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1552
Johannes Berg3b858752009-03-12 09:55:09 +01001553 rtnl_lock();
1554
Johannes Berg463d0182009-07-14 00:33:35 +02001555 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001556 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001557 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001558
Johannes Berg79c97e92009-07-07 03:56:12 +02001559 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001560 err = -EOPNOTSUPP;
1561 goto out;
1562 }
1563
Johannes Bergfffd0932009-07-08 14:22:54 +02001564 wdev_lock(dev->ieee80211_ptr);
1565 err = nl80211_key_allowed(dev->ieee80211_ptr);
1566 if (!err)
1567 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001568
Johannes Berg3d23e342009-09-29 23:27:28 +02001569#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001570 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001571 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001572 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001573 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001574 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1575 }
1576#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001577 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001578
Johannes Berg41ade002007-12-19 02:03:29 +01001579 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001580 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001581 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001582
1583 unlock_rtnl:
1584 rtnl_unlock();
1585
Johannes Berg41ade002007-12-19 02:03:29 +01001586 return err;
1587}
1588
Johannes Berged1b6cc2007-12-19 02:03:32 +01001589static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1590{
1591 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1592 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001593 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001594 int err;
1595 struct net_device *dev;
1596 struct beacon_parameters params;
1597 int haveinfo = 0;
1598
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001599 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1600 return -EINVAL;
1601
Johannes Berg3b858752009-03-12 09:55:09 +01001602 rtnl_lock();
1603
Johannes Berg463d0182009-07-14 00:33:35 +02001604 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001605 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001606 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001607
Jouni Malineneec60b02009-03-20 21:21:19 +02001608 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1609 err = -EOPNOTSUPP;
1610 goto out;
1611 }
1612
Johannes Berged1b6cc2007-12-19 02:03:32 +01001613 switch (info->genlhdr->cmd) {
1614 case NL80211_CMD_NEW_BEACON:
1615 /* these are required for NEW_BEACON */
1616 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1617 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1618 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1619 err = -EINVAL;
1620 goto out;
1621 }
1622
Johannes Berg79c97e92009-07-07 03:56:12 +02001623 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001624 break;
1625 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001626 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001627 break;
1628 default:
1629 WARN_ON(1);
1630 err = -EOPNOTSUPP;
1631 goto out;
1632 }
1633
1634 if (!call) {
1635 err = -EOPNOTSUPP;
1636 goto out;
1637 }
1638
1639 memset(&params, 0, sizeof(params));
1640
1641 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1642 params.interval =
1643 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1644 haveinfo = 1;
1645 }
1646
1647 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1648 params.dtim_period =
1649 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1650 haveinfo = 1;
1651 }
1652
1653 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1654 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1655 params.head_len =
1656 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1657 haveinfo = 1;
1658 }
1659
1660 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1661 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1662 params.tail_len =
1663 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1664 haveinfo = 1;
1665 }
1666
1667 if (!haveinfo) {
1668 err = -EINVAL;
1669 goto out;
1670 }
1671
Johannes Berg79c97e92009-07-07 03:56:12 +02001672 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001673
1674 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001675 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001676 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001677 unlock_rtnl:
1678 rtnl_unlock();
1679
Johannes Berged1b6cc2007-12-19 02:03:32 +01001680 return err;
1681}
1682
1683static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1684{
Johannes Berg79c97e92009-07-07 03:56:12 +02001685 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001686 int err;
1687 struct net_device *dev;
1688
Johannes Berg3b858752009-03-12 09:55:09 +01001689 rtnl_lock();
1690
Johannes Berg463d0182009-07-14 00:33:35 +02001691 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001692 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001693 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001694
Johannes Berg79c97e92009-07-07 03:56:12 +02001695 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001696 err = -EOPNOTSUPP;
1697 goto out;
1698 }
1699
Jouni Malineneec60b02009-03-20 21:21:19 +02001700 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1701 err = -EOPNOTSUPP;
1702 goto out;
1703 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001704 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001705
1706 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001707 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001708 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001709 unlock_rtnl:
1710 rtnl_unlock();
1711
Johannes Berged1b6cc2007-12-19 02:03:32 +01001712 return err;
1713}
1714
Johannes Berg5727ef12007-12-19 02:03:34 +01001715static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1716 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1717 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1718 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001719 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001720};
1721
Johannes Bergeccb8e82009-05-11 21:57:56 +03001722static int parse_station_flags(struct genl_info *info,
1723 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001724{
1725 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001726 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001727 int flag;
1728
Johannes Bergeccb8e82009-05-11 21:57:56 +03001729 /*
1730 * Try parsing the new attribute first so userspace
1731 * can specify both for older kernels.
1732 */
1733 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1734 if (nla) {
1735 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001736
Johannes Bergeccb8e82009-05-11 21:57:56 +03001737 sta_flags = nla_data(nla);
1738 params->sta_flags_mask = sta_flags->mask;
1739 params->sta_flags_set = sta_flags->set;
1740 if ((params->sta_flags_mask |
1741 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1742 return -EINVAL;
1743 return 0;
1744 }
1745
1746 /* if present, parse the old attribute */
1747
1748 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001749 if (!nla)
1750 return 0;
1751
1752 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1753 nla, sta_flags_policy))
1754 return -EINVAL;
1755
Johannes Bergeccb8e82009-05-11 21:57:56 +03001756 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1757 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001758
1759 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1760 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001761 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001762
1763 return 0;
1764}
1765
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001766static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1767 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001768 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001769{
1770 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001771 struct nlattr *sinfoattr, *txrate;
1772 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001773
1774 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1775 if (!hdr)
1776 return -1;
1777
1778 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1779 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1780
Johannes Bergf5ea9122009-08-07 16:17:38 +02001781 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1782
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001783 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1784 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001785 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001786 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1787 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1788 sinfo->inactive_time);
1789 if (sinfo->filled & STATION_INFO_RX_BYTES)
1790 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1791 sinfo->rx_bytes);
1792 if (sinfo->filled & STATION_INFO_TX_BYTES)
1793 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1794 sinfo->tx_bytes);
1795 if (sinfo->filled & STATION_INFO_LLID)
1796 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1797 sinfo->llid);
1798 if (sinfo->filled & STATION_INFO_PLID)
1799 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1800 sinfo->plid);
1801 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1802 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1803 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001804 if (sinfo->filled & STATION_INFO_SIGNAL)
1805 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1806 sinfo->signal);
1807 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1808 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1809 if (!txrate)
1810 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001811
John W. Linville254416a2009-12-09 16:43:52 -05001812 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1813 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001814 if (bitrate > 0)
1815 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1816
1817 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1818 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1819 sinfo->txrate.mcs);
1820 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1821 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1822 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1823 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1824
1825 nla_nest_end(msg, txrate);
1826 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001827 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1828 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1829 sinfo->rx_packets);
1830 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1831 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1832 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001833 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001834
1835 return genlmsg_end(msg, hdr);
1836
1837 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001838 genlmsg_cancel(msg, hdr);
1839 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001840}
1841
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001842static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001843 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001844{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001845 struct station_info sinfo;
1846 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001847 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001848 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001849 int ifidx = cb->args[0];
1850 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001851 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001852
Holger Schuriga0438972009-11-11 11:30:02 +01001853 if (!ifidx)
1854 ifidx = nl80211_get_ifidx(cb);
1855 if (ifidx < 0)
1856 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001857
Johannes Berg3b858752009-03-12 09:55:09 +01001858 rtnl_lock();
1859
Johannes Berg463d0182009-07-14 00:33:35 +02001860 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001861 if (!netdev) {
1862 err = -ENODEV;
1863 goto out_rtnl;
1864 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001865
Johannes Berg463d0182009-07-14 00:33:35 +02001866 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001867 if (IS_ERR(dev)) {
1868 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001869 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001870 }
1871
1872 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001873 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001874 goto out_err;
1875 }
1876
Johannes Bergbba95fe2008-07-29 13:22:51 +02001877 while (1) {
1878 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1879 mac_addr, &sinfo);
1880 if (err == -ENOENT)
1881 break;
1882 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001883 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001884
1885 if (nl80211_send_station(skb,
1886 NETLINK_CB(cb->skb).pid,
1887 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1888 netdev, mac_addr,
1889 &sinfo) < 0)
1890 goto out;
1891
1892 sta_idx++;
1893 }
1894
1895
1896 out:
1897 cb->args[1] = sta_idx;
1898 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001899 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001900 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001901 out_rtnl:
1902 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001903
1904 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001905}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001906
Johannes Berg5727ef12007-12-19 02:03:34 +01001907static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1908{
Johannes Berg79c97e92009-07-07 03:56:12 +02001909 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001910 int err;
1911 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001912 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001913 struct sk_buff *msg;
1914 u8 *mac_addr = NULL;
1915
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001916 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001917
1918 if (!info->attrs[NL80211_ATTR_MAC])
1919 return -EINVAL;
1920
1921 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1922
Johannes Berg3b858752009-03-12 09:55:09 +01001923 rtnl_lock();
1924
Johannes Berg463d0182009-07-14 00:33:35 +02001925 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001926 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001927 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001928
Johannes Berg79c97e92009-07-07 03:56:12 +02001929 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001930 err = -EOPNOTSUPP;
1931 goto out;
1932 }
1933
Johannes Berg79c97e92009-07-07 03:56:12 +02001934 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001935 if (err)
1936 goto out;
1937
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001938 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001939 if (!msg)
1940 goto out;
1941
1942 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001943 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001944 goto out_free;
1945
Johannes Berg134e6372009-07-10 09:51:34 +00001946 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001947 goto out;
1948
1949 out_free:
1950 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001951 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001952 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001953 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001954 out_rtnl:
1955 rtnl_unlock();
1956
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001957 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001958}
1959
1960/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001961 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001962 */
Johannes Berg463d0182009-07-14 00:33:35 +02001963static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001964 struct cfg80211_registered_device *rdev,
1965 struct net_device **vlan)
1966{
Johannes Berg463d0182009-07-14 00:33:35 +02001967 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001968 *vlan = NULL;
1969
1970 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001971 *vlan = dev_get_by_index(genl_info_net(info),
1972 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001973 if (!*vlan)
1974 return -ENODEV;
1975 if (!(*vlan)->ieee80211_ptr)
1976 return -EINVAL;
1977 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1978 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001979 if (!netif_running(*vlan))
1980 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001981 }
1982 return 0;
1983}
1984
1985static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1986{
Johannes Berg79c97e92009-07-07 03:56:12 +02001987 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001988 int err;
1989 struct net_device *dev;
1990 struct station_parameters params;
1991 u8 *mac_addr = NULL;
1992
1993 memset(&params, 0, sizeof(params));
1994
1995 params.listen_interval = -1;
1996
1997 if (info->attrs[NL80211_ATTR_STA_AID])
1998 return -EINVAL;
1999
2000 if (!info->attrs[NL80211_ATTR_MAC])
2001 return -EINVAL;
2002
2003 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2004
2005 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2006 params.supported_rates =
2007 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2008 params.supported_rates_len =
2009 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2010 }
2011
2012 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2013 params.listen_interval =
2014 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2015
Jouni Malinen36aedc92008-08-25 11:58:58 +03002016 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2017 params.ht_capa =
2018 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2019
Johannes Bergeccb8e82009-05-11 21:57:56 +03002020 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002021 return -EINVAL;
2022
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002023 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2024 params.plink_action =
2025 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2026
Johannes Berg3b858752009-03-12 09:55:09 +01002027 rtnl_lock();
2028
Johannes Berg463d0182009-07-14 00:33:35 +02002029 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002030 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002031 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002032
Johannes Berg463d0182009-07-14 00:33:35 +02002033 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002034 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002035 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002036
2037 /* validate settings */
2038 err = 0;
2039
2040 switch (dev->ieee80211_ptr->iftype) {
2041 case NL80211_IFTYPE_AP:
2042 case NL80211_IFTYPE_AP_VLAN:
2043 /* disallow mesh-specific things */
2044 if (params.plink_action)
2045 err = -EINVAL;
2046 break;
2047 case NL80211_IFTYPE_STATION:
2048 /* disallow everything but AUTHORIZED flag */
2049 if (params.plink_action)
2050 err = -EINVAL;
2051 if (params.vlan)
2052 err = -EINVAL;
2053 if (params.supported_rates)
2054 err = -EINVAL;
2055 if (params.ht_capa)
2056 err = -EINVAL;
2057 if (params.listen_interval >= 0)
2058 err = -EINVAL;
2059 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2060 err = -EINVAL;
2061 break;
2062 case NL80211_IFTYPE_MESH_POINT:
2063 /* disallow things mesh doesn't support */
2064 if (params.vlan)
2065 err = -EINVAL;
2066 if (params.ht_capa)
2067 err = -EINVAL;
2068 if (params.listen_interval >= 0)
2069 err = -EINVAL;
2070 if (params.supported_rates)
2071 err = -EINVAL;
2072 if (params.sta_flags_mask)
2073 err = -EINVAL;
2074 break;
2075 default:
2076 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002077 }
2078
Johannes Berg5727ef12007-12-19 02:03:34 +01002079 if (err)
2080 goto out;
2081
Johannes Berg79c97e92009-07-07 03:56:12 +02002082 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002083 err = -EOPNOTSUPP;
2084 goto out;
2085 }
2086
Johannes Berg79c97e92009-07-07 03:56:12 +02002087 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002088
2089 out:
2090 if (params.vlan)
2091 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002092 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002093 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002094 out_rtnl:
2095 rtnl_unlock();
2096
Johannes Berg5727ef12007-12-19 02:03:34 +01002097 return err;
2098}
2099
2100static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2101{
Johannes Berg79c97e92009-07-07 03:56:12 +02002102 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002103 int err;
2104 struct net_device *dev;
2105 struct station_parameters params;
2106 u8 *mac_addr = NULL;
2107
2108 memset(&params, 0, sizeof(params));
2109
2110 if (!info->attrs[NL80211_ATTR_MAC])
2111 return -EINVAL;
2112
Johannes Berg5727ef12007-12-19 02:03:34 +01002113 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2114 return -EINVAL;
2115
2116 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2117 return -EINVAL;
2118
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002119 if (!info->attrs[NL80211_ATTR_STA_AID])
2120 return -EINVAL;
2121
Johannes Berg5727ef12007-12-19 02:03:34 +01002122 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2123 params.supported_rates =
2124 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2125 params.supported_rates_len =
2126 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2127 params.listen_interval =
2128 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002129
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002130 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2131 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2132 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002133
Jouni Malinen36aedc92008-08-25 11:58:58 +03002134 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2135 params.ht_capa =
2136 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002137
Johannes Bergeccb8e82009-05-11 21:57:56 +03002138 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002139 return -EINVAL;
2140
Johannes Berg3b858752009-03-12 09:55:09 +01002141 rtnl_lock();
2142
Johannes Berg463d0182009-07-14 00:33:35 +02002143 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002144 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002145 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002146
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002147 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2148 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN) {
2149 err = -EINVAL;
2150 goto out;
2151 }
2152
Johannes Berg463d0182009-07-14 00:33:35 +02002153 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002154 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002155 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002156
2157 /* validate settings */
2158 err = 0;
2159
Johannes Berg79c97e92009-07-07 03:56:12 +02002160 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002161 err = -EOPNOTSUPP;
2162 goto out;
2163 }
2164
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002165 if (!netif_running(dev)) {
2166 err = -ENETDOWN;
2167 goto out;
2168 }
2169
Johannes Berg79c97e92009-07-07 03:56:12 +02002170 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002171
2172 out:
2173 if (params.vlan)
2174 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002175 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002176 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002177 out_rtnl:
2178 rtnl_unlock();
2179
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 return err;
2181}
2182
2183static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2184{
Johannes Berg79c97e92009-07-07 03:56:12 +02002185 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002186 int err;
2187 struct net_device *dev;
2188 u8 *mac_addr = NULL;
2189
2190 if (info->attrs[NL80211_ATTR_MAC])
2191 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2192
Johannes Berg3b858752009-03-12 09:55:09 +01002193 rtnl_lock();
2194
Johannes Berg463d0182009-07-14 00:33:35 +02002195 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002196 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002197 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002198
Johannes Berge80cf852009-05-11 14:43:13 +02002199 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002200 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2201 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002202 err = -EINVAL;
2203 goto out;
2204 }
2205
Johannes Berg79c97e92009-07-07 03:56:12 +02002206 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002207 err = -EOPNOTSUPP;
2208 goto out;
2209 }
2210
Johannes Berg79c97e92009-07-07 03:56:12 +02002211 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002212
2213 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002214 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002215 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002216 out_rtnl:
2217 rtnl_unlock();
2218
Johannes Berg5727ef12007-12-19 02:03:34 +01002219 return err;
2220}
2221
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002222static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2223 int flags, struct net_device *dev,
2224 u8 *dst, u8 *next_hop,
2225 struct mpath_info *pinfo)
2226{
2227 void *hdr;
2228 struct nlattr *pinfoattr;
2229
2230 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2231 if (!hdr)
2232 return -1;
2233
2234 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2235 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2236 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2237
Johannes Bergf5ea9122009-08-07 16:17:38 +02002238 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2239
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002240 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2241 if (!pinfoattr)
2242 goto nla_put_failure;
2243 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2244 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2245 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002246 if (pinfo->filled & MPATH_INFO_SN)
2247 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2248 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002249 if (pinfo->filled & MPATH_INFO_METRIC)
2250 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2251 pinfo->metric);
2252 if (pinfo->filled & MPATH_INFO_EXPTIME)
2253 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2254 pinfo->exptime);
2255 if (pinfo->filled & MPATH_INFO_FLAGS)
2256 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2257 pinfo->flags);
2258 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2259 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2260 pinfo->discovery_timeout);
2261 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2262 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2263 pinfo->discovery_retries);
2264
2265 nla_nest_end(msg, pinfoattr);
2266
2267 return genlmsg_end(msg, hdr);
2268
2269 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002270 genlmsg_cancel(msg, hdr);
2271 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002272}
2273
2274static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002275 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002276{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002277 struct mpath_info pinfo;
2278 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002279 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002280 u8 dst[ETH_ALEN];
2281 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002282 int ifidx = cb->args[0];
2283 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002284 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002285
Holger Schuriga0438972009-11-11 11:30:02 +01002286 if (!ifidx)
2287 ifidx = nl80211_get_ifidx(cb);
2288 if (ifidx < 0)
2289 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002290
Johannes Berg3b858752009-03-12 09:55:09 +01002291 rtnl_lock();
2292
Johannes Berg463d0182009-07-14 00:33:35 +02002293 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002294 if (!netdev) {
2295 err = -ENODEV;
2296 goto out_rtnl;
2297 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002298
Johannes Berg463d0182009-07-14 00:33:35 +02002299 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002300 if (IS_ERR(dev)) {
2301 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002302 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002303 }
2304
2305 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002306 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002307 goto out_err;
2308 }
2309
Jouni Malineneec60b02009-03-20 21:21:19 +02002310 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2311 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002312 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002313 }
2314
Johannes Bergbba95fe2008-07-29 13:22:51 +02002315 while (1) {
2316 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2317 dst, next_hop, &pinfo);
2318 if (err == -ENOENT)
2319 break;
2320 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002321 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002322
2323 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2324 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2325 netdev, dst, next_hop,
2326 &pinfo) < 0)
2327 goto out;
2328
2329 path_idx++;
2330 }
2331
2332
2333 out:
2334 cb->args[1] = path_idx;
2335 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002336 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002337 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002338 out_rtnl:
2339 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002340
2341 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002342}
2343
2344static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2345{
Johannes Berg79c97e92009-07-07 03:56:12 +02002346 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002347 int err;
2348 struct net_device *dev;
2349 struct mpath_info pinfo;
2350 struct sk_buff *msg;
2351 u8 *dst = NULL;
2352 u8 next_hop[ETH_ALEN];
2353
2354 memset(&pinfo, 0, sizeof(pinfo));
2355
2356 if (!info->attrs[NL80211_ATTR_MAC])
2357 return -EINVAL;
2358
2359 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2360
Johannes Berg3b858752009-03-12 09:55:09 +01002361 rtnl_lock();
2362
Johannes Berg463d0182009-07-14 00:33:35 +02002363 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002364 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002365 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002366
Johannes Berg79c97e92009-07-07 03:56:12 +02002367 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002368 err = -EOPNOTSUPP;
2369 goto out;
2370 }
2371
Jouni Malineneec60b02009-03-20 21:21:19 +02002372 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2373 err = -EOPNOTSUPP;
2374 goto out;
2375 }
2376
Johannes Berg79c97e92009-07-07 03:56:12 +02002377 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378 if (err)
2379 goto out;
2380
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002381 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002382 if (!msg)
2383 goto out;
2384
2385 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2386 dev, dst, next_hop, &pinfo) < 0)
2387 goto out_free;
2388
Johannes Berg134e6372009-07-10 09:51:34 +00002389 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002390 goto out;
2391
2392 out_free:
2393 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002395 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002397 out_rtnl:
2398 rtnl_unlock();
2399
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002400 return err;
2401}
2402
2403static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2404{
Johannes Berg79c97e92009-07-07 03:56:12 +02002405 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002406 int err;
2407 struct net_device *dev;
2408 u8 *dst = NULL;
2409 u8 *next_hop = NULL;
2410
2411 if (!info->attrs[NL80211_ATTR_MAC])
2412 return -EINVAL;
2413
2414 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2415 return -EINVAL;
2416
2417 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2418 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2419
Johannes Berg3b858752009-03-12 09:55:09 +01002420 rtnl_lock();
2421
Johannes Berg463d0182009-07-14 00:33:35 +02002422 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002423 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002424 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002425
Johannes Berg79c97e92009-07-07 03:56:12 +02002426 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002427 err = -EOPNOTSUPP;
2428 goto out;
2429 }
2430
Jouni Malineneec60b02009-03-20 21:21:19 +02002431 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2432 err = -EOPNOTSUPP;
2433 goto out;
2434 }
2435
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002436 if (!netif_running(dev)) {
2437 err = -ENETDOWN;
2438 goto out;
2439 }
2440
Johannes Berg79c97e92009-07-07 03:56:12 +02002441 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002442
2443 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002444 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002445 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002446 out_rtnl:
2447 rtnl_unlock();
2448
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002449 return err;
2450}
2451static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2452{
Johannes Berg79c97e92009-07-07 03:56:12 +02002453 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002454 int err;
2455 struct net_device *dev;
2456 u8 *dst = NULL;
2457 u8 *next_hop = NULL;
2458
2459 if (!info->attrs[NL80211_ATTR_MAC])
2460 return -EINVAL;
2461
2462 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2463 return -EINVAL;
2464
2465 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2466 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2467
Johannes Berg3b858752009-03-12 09:55:09 +01002468 rtnl_lock();
2469
Johannes Berg463d0182009-07-14 00:33:35 +02002470 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002471 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002472 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002473
Johannes Berg79c97e92009-07-07 03:56:12 +02002474 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 err = -EOPNOTSUPP;
2476 goto out;
2477 }
2478
Jouni Malineneec60b02009-03-20 21:21:19 +02002479 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2480 err = -EOPNOTSUPP;
2481 goto out;
2482 }
2483
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002484 if (!netif_running(dev)) {
2485 err = -ENETDOWN;
2486 goto out;
2487 }
2488
Johannes Berg79c97e92009-07-07 03:56:12 +02002489 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002490
2491 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002492 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002493 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002494 out_rtnl:
2495 rtnl_unlock();
2496
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002497 return err;
2498}
2499
2500static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2501{
Johannes Berg79c97e92009-07-07 03:56:12 +02002502 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 int err;
2504 struct net_device *dev;
2505 u8 *dst = NULL;
2506
2507 if (info->attrs[NL80211_ATTR_MAC])
2508 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2509
Johannes Berg3b858752009-03-12 09:55:09 +01002510 rtnl_lock();
2511
Johannes Berg463d0182009-07-14 00:33:35 +02002512 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002513 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002514 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002515
Johannes Berg79c97e92009-07-07 03:56:12 +02002516 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517 err = -EOPNOTSUPP;
2518 goto out;
2519 }
2520
Johannes Berg79c97e92009-07-07 03:56:12 +02002521 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002522
2523 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002524 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002525 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002526 out_rtnl:
2527 rtnl_unlock();
2528
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002529 return err;
2530}
2531
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002532static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2533{
Johannes Berg79c97e92009-07-07 03:56:12 +02002534 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002535 int err;
2536 struct net_device *dev;
2537 struct bss_parameters params;
2538
2539 memset(&params, 0, sizeof(params));
2540 /* default to not changing parameters */
2541 params.use_cts_prot = -1;
2542 params.use_short_preamble = -1;
2543 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002544 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002545
2546 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2547 params.use_cts_prot =
2548 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2549 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2550 params.use_short_preamble =
2551 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2552 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2553 params.use_short_slot_time =
2554 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002555 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2556 params.basic_rates =
2557 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2558 params.basic_rates_len =
2559 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2560 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002561 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2562 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002563
Johannes Berg3b858752009-03-12 09:55:09 +01002564 rtnl_lock();
2565
Johannes Berg463d0182009-07-14 00:33:35 +02002566 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002567 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002568 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002569
Johannes Berg79c97e92009-07-07 03:56:12 +02002570 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002571 err = -EOPNOTSUPP;
2572 goto out;
2573 }
2574
Jouni Malineneec60b02009-03-20 21:21:19 +02002575 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2576 err = -EOPNOTSUPP;
2577 goto out;
2578 }
2579
Johannes Berg79c97e92009-07-07 03:56:12 +02002580 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002581
2582 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002583 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002584 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002585 out_rtnl:
2586 rtnl_unlock();
2587
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002588 return err;
2589}
2590
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002591static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002592 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2593 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2594 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2595 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2596 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2597 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2598};
2599
2600static int parse_reg_rule(struct nlattr *tb[],
2601 struct ieee80211_reg_rule *reg_rule)
2602{
2603 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2604 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2605
2606 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2607 return -EINVAL;
2608 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2609 return -EINVAL;
2610 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2611 return -EINVAL;
2612 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2613 return -EINVAL;
2614 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2615 return -EINVAL;
2616
2617 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2618
2619 freq_range->start_freq_khz =
2620 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2621 freq_range->end_freq_khz =
2622 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2623 freq_range->max_bandwidth_khz =
2624 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2625
2626 power_rule->max_eirp =
2627 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2628
2629 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2630 power_rule->max_antenna_gain =
2631 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2632
2633 return 0;
2634}
2635
2636static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2637{
2638 int r;
2639 char *data = NULL;
2640
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002641 /*
2642 * You should only get this when cfg80211 hasn't yet initialized
2643 * completely when built-in to the kernel right between the time
2644 * window between nl80211_init() and regulatory_init(), if that is
2645 * even possible.
2646 */
2647 mutex_lock(&cfg80211_mutex);
2648 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002649 mutex_unlock(&cfg80211_mutex);
2650 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002651 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002652 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002653
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002654 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2655 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002656
2657 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2658
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002659 r = regulatory_hint_user(data);
2660
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002661 return r;
2662}
2663
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002664static int nl80211_get_mesh_params(struct sk_buff *skb,
2665 struct genl_info *info)
2666{
Johannes Berg79c97e92009-07-07 03:56:12 +02002667 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002668 struct mesh_config cur_params;
2669 int err;
2670 struct net_device *dev;
2671 void *hdr;
2672 struct nlattr *pinfoattr;
2673 struct sk_buff *msg;
2674
Johannes Berg3b858752009-03-12 09:55:09 +01002675 rtnl_lock();
2676
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002677 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002678 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002679 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002680 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002681
Johannes Berg79c97e92009-07-07 03:56:12 +02002682 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002683 err = -EOPNOTSUPP;
2684 goto out;
2685 }
2686
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002687 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002688 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002689 if (err)
2690 goto out;
2691
2692 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002694 if (!msg) {
2695 err = -ENOBUFS;
2696 goto out;
2697 }
2698 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2699 NL80211_CMD_GET_MESH_PARAMS);
2700 if (!hdr)
2701 goto nla_put_failure;
2702 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2703 if (!pinfoattr)
2704 goto nla_put_failure;
2705 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2706 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2707 cur_params.dot11MeshRetryTimeout);
2708 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2709 cur_params.dot11MeshConfirmTimeout);
2710 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2711 cur_params.dot11MeshHoldingTimeout);
2712 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2713 cur_params.dot11MeshMaxPeerLinks);
2714 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2715 cur_params.dot11MeshMaxRetries);
2716 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2717 cur_params.dot11MeshTTL);
2718 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2719 cur_params.auto_open_plinks);
2720 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2721 cur_params.dot11MeshHWMPmaxPREQretries);
2722 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2723 cur_params.path_refresh_time);
2724 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2725 cur_params.min_discovery_timeout);
2726 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2727 cur_params.dot11MeshHWMPactivePathTimeout);
2728 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2729 cur_params.dot11MeshHWMPpreqMinInterval);
2730 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2731 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002732 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2733 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002734 nla_nest_end(msg, pinfoattr);
2735 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002736 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002737 goto out;
2738
Johannes Berg3b858752009-03-12 09:55:09 +01002739 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002740 genlmsg_cancel(msg, hdr);
2741 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002742 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002743 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002744 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002745 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002746 out_rtnl:
2747 rtnl_unlock();
2748
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002749 return err;
2750}
2751
2752#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2753do {\
2754 if (table[attr_num]) {\
2755 cfg.param = nla_fn(table[attr_num]); \
2756 mask |= (1 << (attr_num - 1)); \
2757 } \
2758} while (0);\
2759
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002760static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002761 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2762 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2763 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2764 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2765 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2766 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2767 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2768
2769 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2770 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2771 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2772 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2773 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2774 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2775};
2776
2777static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2778{
2779 int err;
2780 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002781 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002782 struct net_device *dev;
2783 struct mesh_config cfg;
2784 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2785 struct nlattr *parent_attr;
2786
2787 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2788 if (!parent_attr)
2789 return -EINVAL;
2790 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2791 parent_attr, nl80211_meshconf_params_policy))
2792 return -EINVAL;
2793
Johannes Berg3b858752009-03-12 09:55:09 +01002794 rtnl_lock();
2795
Johannes Berg463d0182009-07-14 00:33:35 +02002796 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002797 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002798 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002799
Johannes Berg79c97e92009-07-07 03:56:12 +02002800 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002801 err = -EOPNOTSUPP;
2802 goto out;
2803 }
2804
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002805 /* This makes sure that there aren't more than 32 mesh config
2806 * parameters (otherwise our bitfield scheme would not work.) */
2807 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2808
2809 /* Fill in the params struct */
2810 mask = 0;
2811 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2812 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2813 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2814 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2816 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2817 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2818 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2819 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2820 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2821 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2822 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2823 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2824 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2825 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2826 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2827 nla_get_u8);
2828 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2829 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2830 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2831 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2832 nla_get_u16);
2833 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2834 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2835 nla_get_u32);
2836 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2837 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2838 nla_get_u16);
2839 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2840 dot11MeshHWMPnetDiameterTraversalTime,
2841 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2842 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002843 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2844 dot11MeshHWMPRootMode, mask,
2845 NL80211_MESHCONF_HWMP_ROOTMODE,
2846 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002847
2848 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002849 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002850
Jouni Malinenf3f92582009-03-20 17:57:36 +02002851 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002852 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002853 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002854 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002855 out_rtnl:
2856 rtnl_unlock();
2857
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002858 return err;
2859}
2860
2861#undef FILL_IN_MESH_PARAM_IF_SET
2862
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002863static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2864{
2865 struct sk_buff *msg;
2866 void *hdr = NULL;
2867 struct nlattr *nl_reg_rules;
2868 unsigned int i;
2869 int err = -EINVAL;
2870
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002871 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002872
2873 if (!cfg80211_regdomain)
2874 goto out;
2875
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002876 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002877 if (!msg) {
2878 err = -ENOBUFS;
2879 goto out;
2880 }
2881
2882 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2883 NL80211_CMD_GET_REG);
2884 if (!hdr)
2885 goto nla_put_failure;
2886
2887 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2888 cfg80211_regdomain->alpha2);
2889
2890 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2891 if (!nl_reg_rules)
2892 goto nla_put_failure;
2893
2894 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2895 struct nlattr *nl_reg_rule;
2896 const struct ieee80211_reg_rule *reg_rule;
2897 const struct ieee80211_freq_range *freq_range;
2898 const struct ieee80211_power_rule *power_rule;
2899
2900 reg_rule = &cfg80211_regdomain->reg_rules[i];
2901 freq_range = &reg_rule->freq_range;
2902 power_rule = &reg_rule->power_rule;
2903
2904 nl_reg_rule = nla_nest_start(msg, i);
2905 if (!nl_reg_rule)
2906 goto nla_put_failure;
2907
2908 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2909 reg_rule->flags);
2910 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2911 freq_range->start_freq_khz);
2912 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2913 freq_range->end_freq_khz);
2914 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2915 freq_range->max_bandwidth_khz);
2916 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2917 power_rule->max_antenna_gain);
2918 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2919 power_rule->max_eirp);
2920
2921 nla_nest_end(msg, nl_reg_rule);
2922 }
2923
2924 nla_nest_end(msg, nl_reg_rules);
2925
2926 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002927 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002928 goto out;
2929
2930nla_put_failure:
2931 genlmsg_cancel(msg, hdr);
2932 err = -EMSGSIZE;
2933out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002934 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002935 return err;
2936}
2937
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002938static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2939{
2940 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2941 struct nlattr *nl_reg_rule;
2942 char *alpha2 = NULL;
2943 int rem_reg_rules = 0, r = 0;
2944 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2945 struct ieee80211_regdomain *rd = NULL;
2946
2947 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2948 return -EINVAL;
2949
2950 if (!info->attrs[NL80211_ATTR_REG_RULES])
2951 return -EINVAL;
2952
2953 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2954
2955 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2956 rem_reg_rules) {
2957 num_rules++;
2958 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002959 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002960 }
2961
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002962 mutex_lock(&cfg80211_mutex);
2963
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002964 if (!reg_is_valid_request(alpha2)) {
2965 r = -EINVAL;
2966 goto bad_reg;
2967 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002968
2969 size_of_regd = sizeof(struct ieee80211_regdomain) +
2970 (num_rules * sizeof(struct ieee80211_reg_rule));
2971
2972 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002973 if (!rd) {
2974 r = -ENOMEM;
2975 goto bad_reg;
2976 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002977
2978 rd->n_reg_rules = num_rules;
2979 rd->alpha2[0] = alpha2[0];
2980 rd->alpha2[1] = alpha2[1];
2981
2982 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2983 rem_reg_rules) {
2984 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2985 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2986 reg_rule_policy);
2987 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2988 if (r)
2989 goto bad_reg;
2990
2991 rule_idx++;
2992
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002993 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2994 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002995 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002996 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002997 }
2998
2999 BUG_ON(rule_idx != num_rules);
3000
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003001 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003002
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003003 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003004
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003005 return r;
3006
Johannes Bergd2372b32008-10-24 20:32:20 +02003007 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003008 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003009 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003010 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003011}
3012
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003013static int validate_scan_freqs(struct nlattr *freqs)
3014{
3015 struct nlattr *attr1, *attr2;
3016 int n_channels = 0, tmp1, tmp2;
3017
3018 nla_for_each_nested(attr1, freqs, tmp1) {
3019 n_channels++;
3020 /*
3021 * Some hardware has a limited channel list for
3022 * scanning, and it is pretty much nonsensical
3023 * to scan for a channel twice, so disallow that
3024 * and don't require drivers to check that the
3025 * channel list they get isn't longer than what
3026 * they can scan, as long as they can scan all
3027 * the channels they registered at once.
3028 */
3029 nla_for_each_nested(attr2, freqs, tmp2)
3030 if (attr1 != attr2 &&
3031 nla_get_u32(attr1) == nla_get_u32(attr2))
3032 return 0;
3033 }
3034
3035 return n_channels;
3036}
3037
Johannes Berg2a519312009-02-10 21:25:55 +01003038static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3039{
Johannes Berg79c97e92009-07-07 03:56:12 +02003040 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003041 struct net_device *dev;
3042 struct cfg80211_scan_request *request;
3043 struct cfg80211_ssid *ssid;
3044 struct ieee80211_channel *channel;
3045 struct nlattr *attr;
3046 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003047 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003048 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003049 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003050
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003051 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3052 return -EINVAL;
3053
Johannes Berg3b858752009-03-12 09:55:09 +01003054 rtnl_lock();
3055
Johannes Berg463d0182009-07-14 00:33:35 +02003056 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003057 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003058 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01003059
Johannes Berg79c97e92009-07-07 03:56:12 +02003060 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003061
Johannes Berg79c97e92009-07-07 03:56:12 +02003062 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003063 err = -EOPNOTSUPP;
3064 goto out;
3065 }
3066
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003067 if (!netif_running(dev)) {
3068 err = -ENETDOWN;
3069 goto out;
3070 }
3071
Johannes Berg79c97e92009-07-07 03:56:12 +02003072 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01003073 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01003074 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003075 }
3076
3077 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003078 n_channels = validate_scan_freqs(
3079 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01003080 if (!n_channels) {
3081 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003082 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003083 }
3084 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003085 n_channels = 0;
3086
Johannes Berg2a519312009-02-10 21:25:55 +01003087 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3088 if (wiphy->bands[band])
3089 n_channels += wiphy->bands[band]->n_channels;
3090 }
3091
3092 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3093 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3094 n_ssids++;
3095
3096 if (n_ssids > wiphy->max_scan_ssids) {
3097 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003098 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003099 }
3100
Jouni Malinen70692ad2009-02-16 19:39:13 +02003101 if (info->attrs[NL80211_ATTR_IE])
3102 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3103 else
3104 ie_len = 0;
3105
Johannes Berg18a83652009-03-31 12:12:05 +02003106 if (ie_len > wiphy->max_scan_ie_len) {
3107 err = -EINVAL;
3108 goto out;
3109 }
3110
Johannes Berg2a519312009-02-10 21:25:55 +01003111 request = kzalloc(sizeof(*request)
3112 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003113 + sizeof(channel) * n_channels
3114 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003115 if (!request) {
3116 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003117 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003118 }
3119
Johannes Berg2a519312009-02-10 21:25:55 +01003120 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003121 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003122 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003123 if (ie_len) {
3124 if (request->ssids)
3125 request->ie = (void *)(request->ssids + n_ssids);
3126 else
3127 request->ie = (void *)(request->channels + n_channels);
3128 }
Johannes Berg2a519312009-02-10 21:25:55 +01003129
Johannes Berg584991d2009-11-02 13:32:03 +01003130 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003131 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3132 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003133 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003134 struct ieee80211_channel *chan;
3135
3136 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3137
3138 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003139 err = -EINVAL;
3140 goto out_free;
3141 }
Johannes Berg584991d2009-11-02 13:32:03 +01003142
3143 /* ignore disabled channels */
3144 if (chan->flags & IEEE80211_CHAN_DISABLED)
3145 continue;
3146
3147 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003148 i++;
3149 }
3150 } else {
3151 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003152 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3153 int j;
3154 if (!wiphy->bands[band])
3155 continue;
3156 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003157 struct ieee80211_channel *chan;
3158
3159 chan = &wiphy->bands[band]->channels[j];
3160
3161 if (chan->flags & IEEE80211_CHAN_DISABLED)
3162 continue;
3163
3164 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003165 i++;
3166 }
3167 }
3168 }
3169
Johannes Berg584991d2009-11-02 13:32:03 +01003170 if (!i) {
3171 err = -EINVAL;
3172 goto out_free;
3173 }
3174
3175 request->n_channels = i;
3176
Johannes Berg2a519312009-02-10 21:25:55 +01003177 i = 0;
3178 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3179 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3180 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3181 err = -EINVAL;
3182 goto out_free;
3183 }
3184 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3185 request->ssids[i].ssid_len = nla_len(attr);
3186 i++;
3187 }
3188 }
3189
Jouni Malinen70692ad2009-02-16 19:39:13 +02003190 if (info->attrs[NL80211_ATTR_IE]) {
3191 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003192 memcpy((void *)request->ie,
3193 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003194 request->ie_len);
3195 }
3196
Johannes Berg463d0182009-07-14 00:33:35 +02003197 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003198 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003199
Johannes Berg79c97e92009-07-07 03:56:12 +02003200 rdev->scan_req = request;
3201 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003202
Johannes Berg463d0182009-07-14 00:33:35 +02003203 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003204 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003205 dev_hold(dev);
3206 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003207
Johannes Berg2a519312009-02-10 21:25:55 +01003208 out_free:
3209 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003210 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003211 kfree(request);
3212 }
Johannes Berg2a519312009-02-10 21:25:55 +01003213 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003214 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003215 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003216 out_rtnl:
3217 rtnl_unlock();
3218
Johannes Berg2a519312009-02-10 21:25:55 +01003219 return err;
3220}
3221
3222static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3223 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003224 struct wireless_dev *wdev,
3225 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003226{
Johannes Berg48ab9052009-07-10 18:42:31 +02003227 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003228 void *hdr;
3229 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003230 int i;
3231
3232 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003233
3234 hdr = nl80211hdr_put(msg, pid, seq, flags,
3235 NL80211_CMD_NEW_SCAN_RESULTS);
3236 if (!hdr)
3237 return -1;
3238
Johannes Bergf5ea9122009-08-07 16:17:38 +02003239 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003240 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003241
3242 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3243 if (!bss)
3244 goto nla_put_failure;
3245 if (!is_zero_ether_addr(res->bssid))
3246 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3247 if (res->information_elements && res->len_information_elements)
3248 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3249 res->len_information_elements,
3250 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003251 if (res->beacon_ies && res->len_beacon_ies &&
3252 res->beacon_ies != res->information_elements)
3253 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3254 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003255 if (res->tsf)
3256 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3257 if (res->beacon_interval)
3258 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3259 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3260 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003261 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3262 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003263
Johannes Berg77965c92009-02-18 18:45:06 +01003264 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003265 case CFG80211_SIGNAL_TYPE_MBM:
3266 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3267 break;
3268 case CFG80211_SIGNAL_TYPE_UNSPEC:
3269 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3270 break;
3271 default:
3272 break;
3273 }
3274
Johannes Berg48ab9052009-07-10 18:42:31 +02003275 switch (wdev->iftype) {
3276 case NL80211_IFTYPE_STATION:
3277 if (intbss == wdev->current_bss)
3278 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3279 NL80211_BSS_STATUS_ASSOCIATED);
3280 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3281 if (intbss != wdev->auth_bsses[i])
3282 continue;
3283 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3284 NL80211_BSS_STATUS_AUTHENTICATED);
3285 break;
3286 }
3287 break;
3288 case NL80211_IFTYPE_ADHOC:
3289 if (intbss == wdev->current_bss)
3290 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3291 NL80211_BSS_STATUS_IBSS_JOINED);
3292 break;
3293 default:
3294 break;
3295 }
3296
Johannes Berg2a519312009-02-10 21:25:55 +01003297 nla_nest_end(msg, bss);
3298
3299 return genlmsg_end(msg, hdr);
3300
3301 nla_put_failure:
3302 genlmsg_cancel(msg, hdr);
3303 return -EMSGSIZE;
3304}
3305
3306static int nl80211_dump_scan(struct sk_buff *skb,
3307 struct netlink_callback *cb)
3308{
Johannes Berg48ab9052009-07-10 18:42:31 +02003309 struct cfg80211_registered_device *rdev;
3310 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003311 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003312 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003313 int ifidx = cb->args[0];
3314 int start = cb->args[1], idx = 0;
3315 int err;
3316
Holger Schuriga0438972009-11-11 11:30:02 +01003317 if (!ifidx)
3318 ifidx = nl80211_get_ifidx(cb);
3319 if (ifidx < 0)
3320 return ifidx;
3321 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003322
Johannes Berg463d0182009-07-14 00:33:35 +02003323 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003324 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003325 return -ENODEV;
3326
Johannes Berg463d0182009-07-14 00:33:35 +02003327 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003328 if (IS_ERR(rdev)) {
3329 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003330 goto out_put_netdev;
3331 }
3332
Johannes Berg48ab9052009-07-10 18:42:31 +02003333 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003334
Johannes Berg48ab9052009-07-10 18:42:31 +02003335 wdev_lock(wdev);
3336 spin_lock_bh(&rdev->bss_lock);
3337 cfg80211_bss_expire(rdev);
3338
3339 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003340 if (++idx <= start)
3341 continue;
3342 if (nl80211_send_bss(skb,
3343 NETLINK_CB(cb->skb).pid,
3344 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003345 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003346 idx--;
3347 goto out;
3348 }
3349 }
3350
3351 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003352 spin_unlock_bh(&rdev->bss_lock);
3353 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003354
3355 cb->args[1] = idx;
3356 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003357 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003358 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003359 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003360
3361 return err;
3362}
3363
Holger Schurig61fa7132009-11-11 12:25:40 +01003364static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3365 int flags, struct net_device *dev,
3366 struct survey_info *survey)
3367{
3368 void *hdr;
3369 struct nlattr *infoattr;
3370
3371 /* Survey without a channel doesn't make sense */
3372 if (!survey->channel)
3373 return -EINVAL;
3374
3375 hdr = nl80211hdr_put(msg, pid, seq, flags,
3376 NL80211_CMD_NEW_SURVEY_RESULTS);
3377 if (!hdr)
3378 return -ENOMEM;
3379
3380 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3381
3382 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3383 if (!infoattr)
3384 goto nla_put_failure;
3385
3386 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3387 survey->channel->center_freq);
3388 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3389 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3390 survey->noise);
3391
3392 nla_nest_end(msg, infoattr);
3393
3394 return genlmsg_end(msg, hdr);
3395
3396 nla_put_failure:
3397 genlmsg_cancel(msg, hdr);
3398 return -EMSGSIZE;
3399}
3400
3401static int nl80211_dump_survey(struct sk_buff *skb,
3402 struct netlink_callback *cb)
3403{
3404 struct survey_info survey;
3405 struct cfg80211_registered_device *dev;
3406 struct net_device *netdev;
3407 int ifidx = cb->args[0];
3408 int survey_idx = cb->args[1];
3409 int res;
3410
3411 if (!ifidx)
3412 ifidx = nl80211_get_ifidx(cb);
3413 if (ifidx < 0)
3414 return ifidx;
3415 cb->args[0] = ifidx;
3416
3417 rtnl_lock();
3418
3419 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3420 if (!netdev) {
3421 res = -ENODEV;
3422 goto out_rtnl;
3423 }
3424
3425 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3426 if (IS_ERR(dev)) {
3427 res = PTR_ERR(dev);
3428 goto out_rtnl;
3429 }
3430
3431 if (!dev->ops->dump_survey) {
3432 res = -EOPNOTSUPP;
3433 goto out_err;
3434 }
3435
3436 while (1) {
3437 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3438 &survey);
3439 if (res == -ENOENT)
3440 break;
3441 if (res)
3442 goto out_err;
3443
3444 if (nl80211_send_survey(skb,
3445 NETLINK_CB(cb->skb).pid,
3446 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3447 netdev,
3448 &survey) < 0)
3449 goto out;
3450 survey_idx++;
3451 }
3452
3453 out:
3454 cb->args[1] = survey_idx;
3455 res = skb->len;
3456 out_err:
3457 cfg80211_unlock_rdev(dev);
3458 out_rtnl:
3459 rtnl_unlock();
3460
3461 return res;
3462}
3463
Jouni Malinen255e7372009-03-20 21:21:17 +02003464static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3465{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003466 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003467}
3468
Samuel Ortizb23aa672009-07-01 21:26:54 +02003469static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3470{
3471 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3472 NL80211_WPA_VERSION_2));
3473}
3474
3475static bool nl80211_valid_akm_suite(u32 akm)
3476{
3477 return akm == WLAN_AKM_SUITE_8021X ||
3478 akm == WLAN_AKM_SUITE_PSK;
3479}
3480
3481static bool nl80211_valid_cipher_suite(u32 cipher)
3482{
3483 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3484 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3485 cipher == WLAN_CIPHER_SUITE_TKIP ||
3486 cipher == WLAN_CIPHER_SUITE_CCMP ||
3487 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3488}
3489
3490
Jouni Malinen636a5d32009-03-19 13:39:22 +02003491static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3492{
Johannes Berg79c97e92009-07-07 03:56:12 +02003493 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003494 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003495 struct ieee80211_channel *chan;
3496 const u8 *bssid, *ssid, *ie = NULL;
3497 int err, ssid_len, ie_len = 0;
3498 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003499 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003500 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003501
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003502 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3503 return -EINVAL;
3504
3505 if (!info->attrs[NL80211_ATTR_MAC])
3506 return -EINVAL;
3507
Jouni Malinen17780922009-03-27 20:52:47 +02003508 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3509 return -EINVAL;
3510
Johannes Berg19957bb2009-07-02 17:20:43 +02003511 if (!info->attrs[NL80211_ATTR_SSID])
3512 return -EINVAL;
3513
3514 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3515 return -EINVAL;
3516
Johannes Bergfffd0932009-07-08 14:22:54 +02003517 err = nl80211_parse_key(info, &key);
3518 if (err)
3519 return err;
3520
3521 if (key.idx >= 0) {
3522 if (!key.p.key || !key.p.key_len)
3523 return -EINVAL;
3524 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3525 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3526 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3527 key.p.key_len != WLAN_KEY_LEN_WEP104))
3528 return -EINVAL;
3529 if (key.idx > 4)
3530 return -EINVAL;
3531 } else {
3532 key.p.key_len = 0;
3533 key.p.key = NULL;
3534 }
3535
Jouni Malinen636a5d32009-03-19 13:39:22 +02003536 rtnl_lock();
3537
Johannes Berg463d0182009-07-14 00:33:35 +02003538 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003539 if (err)
3540 goto unlock_rtnl;
3541
Johannes Berg79c97e92009-07-07 03:56:12 +02003542 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003543 err = -EOPNOTSUPP;
3544 goto out;
3545 }
3546
Jouni Malineneec60b02009-03-20 21:21:19 +02003547 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3548 err = -EOPNOTSUPP;
3549 goto out;
3550 }
3551
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003552 if (!netif_running(dev)) {
3553 err = -ENETDOWN;
3554 goto out;
3555 }
3556
Johannes Berg19957bb2009-07-02 17:20:43 +02003557 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003558 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003559 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3560 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003561 err = -EINVAL;
3562 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003563 }
3564
Johannes Berg19957bb2009-07-02 17:20:43 +02003565 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3566 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3567
3568 if (info->attrs[NL80211_ATTR_IE]) {
3569 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3570 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3571 }
3572
3573 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3574 if (!nl80211_valid_auth_type(auth_type)) {
3575 err = -EINVAL;
3576 goto out;
3577 }
3578
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003579 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3580
Johannes Berg79c97e92009-07-07 03:56:12 +02003581 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003582 ssid, ssid_len, ie, ie_len,
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003583 key.p.key, key.p.key_len, key.idx,
3584 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003585
3586out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003587 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003588 dev_put(dev);
3589unlock_rtnl:
3590 rtnl_unlock();
3591 return err;
3592}
3593
Samuel Ortizb23aa672009-07-01 21:26:54 +02003594static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003595 struct cfg80211_crypto_settings *settings,
3596 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003597{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003598 memset(settings, 0, sizeof(*settings));
3599
Samuel Ortizb23aa672009-07-01 21:26:54 +02003600 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3601
3602 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3603 void *data;
3604 int len, i;
3605
3606 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3607 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3608 settings->n_ciphers_pairwise = len / sizeof(u32);
3609
3610 if (len % sizeof(u32))
3611 return -EINVAL;
3612
Johannes Berg3dc27d22009-07-02 21:36:37 +02003613 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003614 return -EINVAL;
3615
3616 memcpy(settings->ciphers_pairwise, data, len);
3617
3618 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3619 if (!nl80211_valid_cipher_suite(
3620 settings->ciphers_pairwise[i]))
3621 return -EINVAL;
3622 }
3623
3624 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3625 settings->cipher_group =
3626 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3627 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3628 return -EINVAL;
3629 }
3630
3631 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3632 settings->wpa_versions =
3633 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3634 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3635 return -EINVAL;
3636 }
3637
3638 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3639 void *data;
3640 int len, i;
3641
3642 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3643 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3644 settings->n_akm_suites = len / sizeof(u32);
3645
3646 if (len % sizeof(u32))
3647 return -EINVAL;
3648
3649 memcpy(settings->akm_suites, data, len);
3650
3651 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3652 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3653 return -EINVAL;
3654 }
3655
3656 return 0;
3657}
3658
Jouni Malinen636a5d32009-03-19 13:39:22 +02003659static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3660{
Johannes Berg19957bb2009-07-02 17:20:43 +02003661 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003662 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003663 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003664 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003665 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003666 int err, ssid_len, ie_len = 0;
3667 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003668
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003669 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3670 return -EINVAL;
3671
3672 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003673 !info->attrs[NL80211_ATTR_SSID] ||
3674 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003675 return -EINVAL;
3676
Jouni Malinen636a5d32009-03-19 13:39:22 +02003677 rtnl_lock();
3678
Johannes Berg463d0182009-07-14 00:33:35 +02003679 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003680 if (err)
3681 goto unlock_rtnl;
3682
Johannes Berg19957bb2009-07-02 17:20:43 +02003683 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003684 err = -EOPNOTSUPP;
3685 goto out;
3686 }
3687
Jouni Malineneec60b02009-03-20 21:21:19 +02003688 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3689 err = -EOPNOTSUPP;
3690 goto out;
3691 }
3692
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003693 if (!netif_running(dev)) {
3694 err = -ENETDOWN;
3695 goto out;
3696 }
3697
Johannes Berg19957bb2009-07-02 17:20:43 +02003698 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003699
Johannes Berg19957bb2009-07-02 17:20:43 +02003700 chan = ieee80211_get_channel(&rdev->wiphy,
3701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3702 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3703 err = -EINVAL;
3704 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003705 }
3706
Johannes Berg19957bb2009-07-02 17:20:43 +02003707 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3708 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003709
3710 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003711 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3712 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003713 }
3714
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003715 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003716 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003717 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003718 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003719 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003720 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003721 err = -EINVAL;
3722 goto out;
3723 }
3724 }
3725
Johannes Berg3e5d7642009-07-07 14:37:26 +02003726 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3727 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3728
Johannes Berg3dc27d22009-07-02 21:36:37 +02003729 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003730 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003731 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3732 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003733 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003734
3735out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003736 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003737 dev_put(dev);
3738unlock_rtnl:
3739 rtnl_unlock();
3740 return err;
3741}
3742
3743static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3744{
Johannes Berg79c97e92009-07-07 03:56:12 +02003745 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003746 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003747 const u8 *ie = NULL, *bssid;
3748 int err, ie_len = 0;
3749 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003750 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003751
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003752 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3753 return -EINVAL;
3754
3755 if (!info->attrs[NL80211_ATTR_MAC])
3756 return -EINVAL;
3757
3758 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3759 return -EINVAL;
3760
Jouni Malinen636a5d32009-03-19 13:39:22 +02003761 rtnl_lock();
3762
Johannes Berg463d0182009-07-14 00:33:35 +02003763 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003764 if (err)
3765 goto unlock_rtnl;
3766
Johannes Berg79c97e92009-07-07 03:56:12 +02003767 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003768 err = -EOPNOTSUPP;
3769 goto out;
3770 }
3771
Jouni Malineneec60b02009-03-20 21:21:19 +02003772 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3773 err = -EOPNOTSUPP;
3774 goto out;
3775 }
3776
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003777 if (!netif_running(dev)) {
3778 err = -ENETDOWN;
3779 goto out;
3780 }
3781
Johannes Berg19957bb2009-07-02 17:20:43 +02003782 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003783
Johannes Berg19957bb2009-07-02 17:20:43 +02003784 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3785 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003786 /* Reason Code 0 is reserved */
3787 err = -EINVAL;
3788 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003789 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003790
3791 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003792 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3793 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003794 }
3795
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003796 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3797
3798 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3799 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003800
3801out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003802 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003803 dev_put(dev);
3804unlock_rtnl:
3805 rtnl_unlock();
3806 return err;
3807}
3808
3809static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3810{
Johannes Berg79c97e92009-07-07 03:56:12 +02003811 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003812 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003813 const u8 *ie = NULL, *bssid;
3814 int err, ie_len = 0;
3815 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003816 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003817
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003818 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3819 return -EINVAL;
3820
3821 if (!info->attrs[NL80211_ATTR_MAC])
3822 return -EINVAL;
3823
3824 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3825 return -EINVAL;
3826
Jouni Malinen636a5d32009-03-19 13:39:22 +02003827 rtnl_lock();
3828
Johannes Berg463d0182009-07-14 00:33:35 +02003829 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003830 if (err)
3831 goto unlock_rtnl;
3832
Johannes Berg79c97e92009-07-07 03:56:12 +02003833 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003834 err = -EOPNOTSUPP;
3835 goto out;
3836 }
3837
Jouni Malineneec60b02009-03-20 21:21:19 +02003838 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3839 err = -EOPNOTSUPP;
3840 goto out;
3841 }
3842
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003843 if (!netif_running(dev)) {
3844 err = -ENETDOWN;
3845 goto out;
3846 }
3847
Johannes Berg19957bb2009-07-02 17:20:43 +02003848 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003849
Johannes Berg19957bb2009-07-02 17:20:43 +02003850 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3851 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003852 /* Reason Code 0 is reserved */
3853 err = -EINVAL;
3854 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003855 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003856
3857 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003858 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3859 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003860 }
3861
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003862 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3863
3864 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3865 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003866
3867out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003868 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003869 dev_put(dev);
3870unlock_rtnl:
3871 rtnl_unlock();
3872 return err;
3873}
3874
Johannes Berg04a773a2009-04-19 21:24:32 +02003875static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3876{
Johannes Berg79c97e92009-07-07 03:56:12 +02003877 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003878 struct net_device *dev;
3879 struct cfg80211_ibss_params ibss;
3880 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003881 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003882 int err;
3883
Johannes Berg8e30bc52009-04-22 17:45:38 +02003884 memset(&ibss, 0, sizeof(ibss));
3885
Johannes Berg04a773a2009-04-19 21:24:32 +02003886 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3887 return -EINVAL;
3888
3889 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3890 !info->attrs[NL80211_ATTR_SSID] ||
3891 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3892 return -EINVAL;
3893
Johannes Berg8e30bc52009-04-22 17:45:38 +02003894 ibss.beacon_interval = 100;
3895
3896 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3897 ibss.beacon_interval =
3898 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3899 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3900 return -EINVAL;
3901 }
3902
Johannes Berg04a773a2009-04-19 21:24:32 +02003903 rtnl_lock();
3904
Johannes Berg463d0182009-07-14 00:33:35 +02003905 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003906 if (err)
3907 goto unlock_rtnl;
3908
Johannes Berg79c97e92009-07-07 03:56:12 +02003909 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003910 err = -EOPNOTSUPP;
3911 goto out;
3912 }
3913
3914 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3915 err = -EOPNOTSUPP;
3916 goto out;
3917 }
3918
3919 if (!netif_running(dev)) {
3920 err = -ENETDOWN;
3921 goto out;
3922 }
3923
Johannes Berg79c97e92009-07-07 03:56:12 +02003924 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003925
3926 if (info->attrs[NL80211_ATTR_MAC])
3927 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3928 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3929 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3930
3931 if (info->attrs[NL80211_ATTR_IE]) {
3932 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3933 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3934 }
3935
3936 ibss.channel = ieee80211_get_channel(wiphy,
3937 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3938 if (!ibss.channel ||
3939 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3940 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3941 err = -EINVAL;
3942 goto out;
3943 }
3944
3945 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003946 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003947
Johannes Bergfffd0932009-07-08 14:22:54 +02003948 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3949 connkeys = nl80211_parse_connkeys(rdev,
3950 info->attrs[NL80211_ATTR_KEYS]);
3951 if (IS_ERR(connkeys)) {
3952 err = PTR_ERR(connkeys);
3953 connkeys = NULL;
3954 goto out;
3955 }
3956 }
3957
3958 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003959
3960out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003961 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003962 dev_put(dev);
3963unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003964 if (err)
3965 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003966 rtnl_unlock();
3967 return err;
3968}
3969
3970static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3971{
Johannes Berg79c97e92009-07-07 03:56:12 +02003972 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003973 struct net_device *dev;
3974 int err;
3975
3976 rtnl_lock();
3977
Johannes Berg463d0182009-07-14 00:33:35 +02003978 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003979 if (err)
3980 goto unlock_rtnl;
3981
Johannes Berg79c97e92009-07-07 03:56:12 +02003982 if (!rdev->ops->leave_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003983 err = -EOPNOTSUPP;
3984 goto out;
3985 }
3986
3987 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3988 err = -EOPNOTSUPP;
3989 goto out;
3990 }
3991
3992 if (!netif_running(dev)) {
3993 err = -ENETDOWN;
3994 goto out;
3995 }
3996
Johannes Berg79c97e92009-07-07 03:56:12 +02003997 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003998
3999out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004000 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02004001 dev_put(dev);
4002unlock_rtnl:
4003 rtnl_unlock();
4004 return err;
4005}
4006
Johannes Bergaff89a92009-07-01 21:26:51 +02004007#ifdef CONFIG_NL80211_TESTMODE
4008static struct genl_multicast_group nl80211_testmode_mcgrp = {
4009 .name = "testmode",
4010};
4011
4012static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4013{
4014 struct cfg80211_registered_device *rdev;
4015 int err;
4016
4017 if (!info->attrs[NL80211_ATTR_TESTDATA])
4018 return -EINVAL;
4019
4020 rtnl_lock();
4021
4022 rdev = cfg80211_get_dev_from_info(info);
4023 if (IS_ERR(rdev)) {
4024 err = PTR_ERR(rdev);
4025 goto unlock_rtnl;
4026 }
4027
4028 err = -EOPNOTSUPP;
4029 if (rdev->ops->testmode_cmd) {
4030 rdev->testmode_info = info;
4031 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4032 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4033 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4034 rdev->testmode_info = NULL;
4035 }
4036
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02004037 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02004038
4039 unlock_rtnl:
4040 rtnl_unlock();
4041 return err;
4042}
4043
4044static struct sk_buff *
4045__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4046 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4047{
4048 struct sk_buff *skb;
4049 void *hdr;
4050 struct nlattr *data;
4051
4052 skb = nlmsg_new(approxlen + 100, gfp);
4053 if (!skb)
4054 return NULL;
4055
4056 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4057 if (!hdr) {
4058 kfree_skb(skb);
4059 return NULL;
4060 }
4061
4062 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4063 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4064
4065 ((void **)skb->cb)[0] = rdev;
4066 ((void **)skb->cb)[1] = hdr;
4067 ((void **)skb->cb)[2] = data;
4068
4069 return skb;
4070
4071 nla_put_failure:
4072 kfree_skb(skb);
4073 return NULL;
4074}
4075
4076struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4077 int approxlen)
4078{
4079 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4080
4081 if (WARN_ON(!rdev->testmode_info))
4082 return NULL;
4083
4084 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4085 rdev->testmode_info->snd_pid,
4086 rdev->testmode_info->snd_seq,
4087 GFP_KERNEL);
4088}
4089EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4090
4091int cfg80211_testmode_reply(struct sk_buff *skb)
4092{
4093 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4094 void *hdr = ((void **)skb->cb)[1];
4095 struct nlattr *data = ((void **)skb->cb)[2];
4096
4097 if (WARN_ON(!rdev->testmode_info)) {
4098 kfree_skb(skb);
4099 return -EINVAL;
4100 }
4101
4102 nla_nest_end(skb, data);
4103 genlmsg_end(skb, hdr);
4104 return genlmsg_reply(skb, rdev->testmode_info);
4105}
4106EXPORT_SYMBOL(cfg80211_testmode_reply);
4107
4108struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4109 int approxlen, gfp_t gfp)
4110{
4111 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4112
4113 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4114}
4115EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4116
4117void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4118{
4119 void *hdr = ((void **)skb->cb)[1];
4120 struct nlattr *data = ((void **)skb->cb)[2];
4121
4122 nla_nest_end(skb, data);
4123 genlmsg_end(skb, hdr);
4124 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4125}
4126EXPORT_SYMBOL(cfg80211_testmode_event);
4127#endif
4128
Samuel Ortizb23aa672009-07-01 21:26:54 +02004129static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4130{
Johannes Berg79c97e92009-07-07 03:56:12 +02004131 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004132 struct net_device *dev;
4133 struct cfg80211_connect_params connect;
4134 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004135 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004136 int err;
4137
4138 memset(&connect, 0, sizeof(connect));
4139
4140 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4141 return -EINVAL;
4142
4143 if (!info->attrs[NL80211_ATTR_SSID] ||
4144 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4145 return -EINVAL;
4146
4147 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4148 connect.auth_type =
4149 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4150 if (!nl80211_valid_auth_type(connect.auth_type))
4151 return -EINVAL;
4152 } else
4153 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4154
4155 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4156
Johannes Berg3dc27d22009-07-02 21:36:37 +02004157 err = nl80211_crypto_settings(info, &connect.crypto,
4158 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004159 if (err)
4160 return err;
4161 rtnl_lock();
4162
Johannes Berg463d0182009-07-14 00:33:35 +02004163 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004164 if (err)
4165 goto unlock_rtnl;
4166
4167 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4168 err = -EOPNOTSUPP;
4169 goto out;
4170 }
4171
4172 if (!netif_running(dev)) {
4173 err = -ENETDOWN;
4174 goto out;
4175 }
4176
Johannes Berg79c97e92009-07-07 03:56:12 +02004177 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004178
Samuel Ortizb23aa672009-07-01 21:26:54 +02004179 if (info->attrs[NL80211_ATTR_MAC])
4180 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4181 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4182 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4183
4184 if (info->attrs[NL80211_ATTR_IE]) {
4185 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4186 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4187 }
4188
4189 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4190 connect.channel =
4191 ieee80211_get_channel(wiphy,
4192 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4193 if (!connect.channel ||
4194 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4195 err = -EINVAL;
4196 goto out;
4197 }
4198 }
4199
Johannes Bergfffd0932009-07-08 14:22:54 +02004200 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4201 connkeys = nl80211_parse_connkeys(rdev,
4202 info->attrs[NL80211_ATTR_KEYS]);
4203 if (IS_ERR(connkeys)) {
4204 err = PTR_ERR(connkeys);
4205 connkeys = NULL;
4206 goto out;
4207 }
4208 }
4209
4210 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004211
4212out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004213 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004214 dev_put(dev);
4215unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004216 if (err)
4217 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004218 rtnl_unlock();
4219 return err;
4220}
4221
4222static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4223{
Johannes Berg79c97e92009-07-07 03:56:12 +02004224 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004225 struct net_device *dev;
4226 int err;
4227 u16 reason;
4228
4229 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4230 reason = WLAN_REASON_DEAUTH_LEAVING;
4231 else
4232 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4233
4234 if (reason == 0)
4235 return -EINVAL;
4236
4237 rtnl_lock();
4238
Johannes Berg463d0182009-07-14 00:33:35 +02004239 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004240 if (err)
4241 goto unlock_rtnl;
4242
4243 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4244 err = -EOPNOTSUPP;
4245 goto out;
4246 }
4247
4248 if (!netif_running(dev)) {
4249 err = -ENETDOWN;
4250 goto out;
4251 }
4252
Johannes Berg79c97e92009-07-07 03:56:12 +02004253 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004254
4255out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004256 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004257 dev_put(dev);
4258unlock_rtnl:
4259 rtnl_unlock();
4260 return err;
4261}
4262
Johannes Berg463d0182009-07-14 00:33:35 +02004263static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4264{
4265 struct cfg80211_registered_device *rdev;
4266 struct net *net;
4267 int err;
4268 u32 pid;
4269
4270 if (!info->attrs[NL80211_ATTR_PID])
4271 return -EINVAL;
4272
4273 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4274
4275 rtnl_lock();
4276
4277 rdev = cfg80211_get_dev_from_info(info);
4278 if (IS_ERR(rdev)) {
4279 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004280 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004281 }
4282
4283 net = get_net_ns_by_pid(pid);
4284 if (IS_ERR(net)) {
4285 err = PTR_ERR(net);
4286 goto out;
4287 }
4288
4289 err = 0;
4290
4291 /* check if anything to do */
4292 if (net_eq(wiphy_net(&rdev->wiphy), net))
4293 goto out_put_net;
4294
4295 err = cfg80211_switch_netns(rdev, net);
4296 out_put_net:
4297 put_net(net);
4298 out:
4299 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004300 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004301 rtnl_unlock();
4302 return err;
4303}
4304
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004305static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4306{
4307 struct cfg80211_registered_device *rdev;
4308 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4309 struct cfg80211_pmksa *pmksa) = NULL;
4310 int err;
4311 struct net_device *dev;
4312 struct cfg80211_pmksa pmksa;
4313
4314 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4315
4316 if (!info->attrs[NL80211_ATTR_MAC])
4317 return -EINVAL;
4318
4319 if (!info->attrs[NL80211_ATTR_PMKID])
4320 return -EINVAL;
4321
4322 rtnl_lock();
4323
4324 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4325 if (err)
4326 goto out_rtnl;
4327
4328 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4329 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4330
4331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4332 err = -EOPNOTSUPP;
4333 goto out;
4334 }
4335
4336 switch (info->genlhdr->cmd) {
4337 case NL80211_CMD_SET_PMKSA:
4338 rdev_ops = rdev->ops->set_pmksa;
4339 break;
4340 case NL80211_CMD_DEL_PMKSA:
4341 rdev_ops = rdev->ops->del_pmksa;
4342 break;
4343 default:
4344 WARN_ON(1);
4345 break;
4346 }
4347
4348 if (!rdev_ops) {
4349 err = -EOPNOTSUPP;
4350 goto out;
4351 }
4352
4353 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4354
4355 out:
4356 cfg80211_unlock_rdev(rdev);
4357 dev_put(dev);
4358 out_rtnl:
4359 rtnl_unlock();
4360
4361 return err;
4362}
4363
4364static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4365{
4366 struct cfg80211_registered_device *rdev;
4367 int err;
4368 struct net_device *dev;
4369
4370 rtnl_lock();
4371
4372 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4373 if (err)
4374 goto out_rtnl;
4375
4376 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4377 err = -EOPNOTSUPP;
4378 goto out;
4379 }
4380
4381 if (!rdev->ops->flush_pmksa) {
4382 err = -EOPNOTSUPP;
4383 goto out;
4384 }
4385
4386 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4387
4388 out:
4389 cfg80211_unlock_rdev(rdev);
4390 dev_put(dev);
4391 out_rtnl:
4392 rtnl_unlock();
4393
4394 return err;
4395
4396}
4397
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004398static int nl80211_remain_on_channel(struct sk_buff *skb,
4399 struct genl_info *info)
4400{
4401 struct cfg80211_registered_device *rdev;
4402 struct net_device *dev;
4403 struct ieee80211_channel *chan;
4404 struct sk_buff *msg;
4405 void *hdr;
4406 u64 cookie;
4407 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4408 u32 freq, duration;
4409 int err;
4410
4411 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4412 !info->attrs[NL80211_ATTR_DURATION])
4413 return -EINVAL;
4414
4415 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4416
4417 /*
4418 * We should be on that channel for at least one jiffie,
4419 * and more than 5 seconds seems excessive.
4420 */
4421 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4422 return -EINVAL;
4423
4424 rtnl_lock();
4425
4426 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4427 if (err)
4428 goto unlock_rtnl;
4429
4430 if (!rdev->ops->remain_on_channel) {
4431 err = -EOPNOTSUPP;
4432 goto out;
4433 }
4434
4435 if (!netif_running(dev)) {
4436 err = -ENETDOWN;
4437 goto out;
4438 }
4439
4440 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4441 channel_type = nla_get_u32(
4442 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4443 if (channel_type != NL80211_CHAN_NO_HT &&
4444 channel_type != NL80211_CHAN_HT20 &&
4445 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg579d7532010-05-18 14:36:34 +02004446 channel_type != NL80211_CHAN_HT40MINUS) {
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004447 err = -EINVAL;
4448 goto out;
Johannes Berg579d7532010-05-18 14:36:34 +02004449 }
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004450 }
4451
4452 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4453 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4454 if (chan == NULL) {
4455 err = -EINVAL;
4456 goto out;
4457 }
4458
4459 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4460 if (!msg) {
4461 err = -ENOMEM;
4462 goto out;
4463 }
4464
4465 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4466 NL80211_CMD_REMAIN_ON_CHANNEL);
4467
4468 if (IS_ERR(hdr)) {
4469 err = PTR_ERR(hdr);
4470 goto free_msg;
4471 }
4472
4473 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4474 channel_type, duration, &cookie);
4475
4476 if (err)
4477 goto free_msg;
4478
4479 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4480
4481 genlmsg_end(msg, hdr);
4482 err = genlmsg_reply(msg, info);
4483 goto out;
4484
4485 nla_put_failure:
4486 err = -ENOBUFS;
4487 free_msg:
4488 nlmsg_free(msg);
4489 out:
4490 cfg80211_unlock_rdev(rdev);
4491 dev_put(dev);
4492 unlock_rtnl:
4493 rtnl_unlock();
4494 return err;
4495}
4496
4497static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4498 struct genl_info *info)
4499{
4500 struct cfg80211_registered_device *rdev;
4501 struct net_device *dev;
4502 u64 cookie;
4503 int err;
4504
4505 if (!info->attrs[NL80211_ATTR_COOKIE])
4506 return -EINVAL;
4507
4508 rtnl_lock();
4509
4510 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4511 if (err)
4512 goto unlock_rtnl;
4513
4514 if (!rdev->ops->cancel_remain_on_channel) {
4515 err = -EOPNOTSUPP;
4516 goto out;
4517 }
4518
4519 if (!netif_running(dev)) {
4520 err = -ENETDOWN;
4521 goto out;
4522 }
4523
4524 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4525
4526 err = rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4527
4528 out:
4529 cfg80211_unlock_rdev(rdev);
4530 dev_put(dev);
4531 unlock_rtnl:
4532 rtnl_unlock();
4533 return err;
4534}
4535
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004536static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4537 u8 *rates, u8 rates_len)
4538{
4539 u8 i;
4540 u32 mask = 0;
4541
4542 for (i = 0; i < rates_len; i++) {
4543 int rate = (rates[i] & 0x7f) * 5;
4544 int ridx;
4545 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4546 struct ieee80211_rate *srate =
4547 &sband->bitrates[ridx];
4548 if (rate == srate->bitrate) {
4549 mask |= 1 << ridx;
4550 break;
4551 }
4552 }
4553 if (ridx == sband->n_bitrates)
4554 return 0; /* rate not found */
4555 }
4556
4557 return mask;
4558}
4559
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004560static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004561 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4562 .len = NL80211_MAX_SUPP_RATES },
4563};
4564
4565static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4566 struct genl_info *info)
4567{
4568 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4569 struct cfg80211_registered_device *rdev;
4570 struct cfg80211_bitrate_mask mask;
4571 int err, rem, i;
4572 struct net_device *dev;
4573 struct nlattr *tx_rates;
4574 struct ieee80211_supported_band *sband;
4575
4576 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4577 return -EINVAL;
4578
4579 rtnl_lock();
4580
4581 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4582 if (err)
4583 goto unlock_rtnl;
4584
4585 if (!rdev->ops->set_bitrate_mask) {
4586 err = -EOPNOTSUPP;
4587 goto unlock;
4588 }
4589
4590 memset(&mask, 0, sizeof(mask));
4591 /* Default to all rates enabled */
4592 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4593 sband = rdev->wiphy.bands[i];
4594 mask.control[i].legacy =
4595 sband ? (1 << sband->n_bitrates) - 1 : 0;
4596 }
4597
4598 /*
4599 * The nested attribute uses enum nl80211_band as the index. This maps
4600 * directly to the enum ieee80211_band values used in cfg80211.
4601 */
4602 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4603 {
4604 enum ieee80211_band band = nla_type(tx_rates);
4605 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4606 err = -EINVAL;
4607 goto unlock;
4608 }
4609 sband = rdev->wiphy.bands[band];
4610 if (sband == NULL) {
4611 err = -EINVAL;
4612 goto unlock;
4613 }
4614 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4615 nla_len(tx_rates), nl80211_txattr_policy);
4616 if (tb[NL80211_TXRATE_LEGACY]) {
4617 mask.control[band].legacy = rateset_to_mask(
4618 sband,
4619 nla_data(tb[NL80211_TXRATE_LEGACY]),
4620 nla_len(tb[NL80211_TXRATE_LEGACY]));
4621 if (mask.control[band].legacy == 0) {
4622 err = -EINVAL;
4623 goto unlock;
4624 }
4625 }
4626 }
4627
4628 err = rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4629
4630 unlock:
4631 dev_put(dev);
4632 cfg80211_unlock_rdev(rdev);
4633 unlock_rtnl:
4634 rtnl_unlock();
4635 return err;
4636}
4637
Jouni Malinen026331c2010-02-15 12:53:10 +02004638static int nl80211_register_action(struct sk_buff *skb, struct genl_info *info)
4639{
4640 struct cfg80211_registered_device *rdev;
4641 struct net_device *dev;
4642 int err;
4643
4644 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4645 return -EINVAL;
4646
4647 if (nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]) < 1)
4648 return -EINVAL;
4649
4650 rtnl_lock();
4651
4652 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4653 if (err)
4654 goto unlock_rtnl;
4655
4656 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4657 err = -EOPNOTSUPP;
4658 goto out;
4659 }
4660
4661 /* not much point in registering if we can't reply */
4662 if (!rdev->ops->action) {
4663 err = -EOPNOTSUPP;
4664 goto out;
4665 }
4666
4667 err = cfg80211_mlme_register_action(dev->ieee80211_ptr, info->snd_pid,
4668 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4669 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4670 out:
4671 cfg80211_unlock_rdev(rdev);
4672 dev_put(dev);
4673 unlock_rtnl:
4674 rtnl_unlock();
4675 return err;
4676}
4677
4678static int nl80211_action(struct sk_buff *skb, struct genl_info *info)
4679{
4680 struct cfg80211_registered_device *rdev;
4681 struct net_device *dev;
4682 struct ieee80211_channel *chan;
4683 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4684 u32 freq;
4685 int err;
4686 void *hdr;
4687 u64 cookie;
4688 struct sk_buff *msg;
4689
4690 if (!info->attrs[NL80211_ATTR_FRAME] ||
4691 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4692 return -EINVAL;
4693
4694 rtnl_lock();
4695
4696 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4697 if (err)
4698 goto unlock_rtnl;
4699
4700 if (!rdev->ops->action) {
4701 err = -EOPNOTSUPP;
4702 goto out;
4703 }
4704
4705 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4706 err = -EOPNOTSUPP;
4707 goto out;
4708 }
4709
4710 if (!netif_running(dev)) {
4711 err = -ENETDOWN;
4712 goto out;
4713 }
4714
4715 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4716 channel_type = nla_get_u32(
4717 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4718 if (channel_type != NL80211_CHAN_NO_HT &&
4719 channel_type != NL80211_CHAN_HT20 &&
4720 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg579d7532010-05-18 14:36:34 +02004721 channel_type != NL80211_CHAN_HT40MINUS) {
Jouni Malinen026331c2010-02-15 12:53:10 +02004722 err = -EINVAL;
4723 goto out;
Johannes Berg579d7532010-05-18 14:36:34 +02004724 }
Jouni Malinen026331c2010-02-15 12:53:10 +02004725 }
4726
4727 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4728 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4729 if (chan == NULL) {
4730 err = -EINVAL;
4731 goto out;
4732 }
4733
4734 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4735 if (!msg) {
4736 err = -ENOMEM;
4737 goto out;
4738 }
4739
4740 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4741 NL80211_CMD_ACTION);
4742
4743 if (IS_ERR(hdr)) {
4744 err = PTR_ERR(hdr);
4745 goto free_msg;
4746 }
4747 err = cfg80211_mlme_action(rdev, dev, chan, channel_type,
4748 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4749 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4750 &cookie);
4751 if (err)
4752 goto free_msg;
4753
4754 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4755
4756 genlmsg_end(msg, hdr);
4757 err = genlmsg_reply(msg, info);
4758 goto out;
4759
4760 nla_put_failure:
4761 err = -ENOBUFS;
4762 free_msg:
4763 nlmsg_free(msg);
4764 out:
4765 cfg80211_unlock_rdev(rdev);
4766 dev_put(dev);
4767unlock_rtnl:
4768 rtnl_unlock();
4769 return err;
4770}
4771
Kalle Valoffb9eb32010-02-17 17:58:10 +02004772static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4773{
4774 struct cfg80211_registered_device *rdev;
4775 struct wireless_dev *wdev;
4776 struct net_device *dev;
4777 u8 ps_state;
4778 bool state;
4779 int err;
4780
4781 if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4782 err = -EINVAL;
4783 goto out;
4784 }
4785
4786 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4787
4788 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4789 err = -EINVAL;
4790 goto out;
4791 }
4792
4793 rtnl_lock();
4794
4795 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4796 if (err)
4797 goto unlock_rdev;
4798
4799 wdev = dev->ieee80211_ptr;
4800
4801 if (!rdev->ops->set_power_mgmt) {
4802 err = -EOPNOTSUPP;
4803 goto unlock_rdev;
4804 }
4805
4806 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4807
4808 if (state == wdev->ps)
4809 goto unlock_rdev;
4810
4811 wdev->ps = state;
4812
4813 if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
4814 wdev->ps_timeout))
4815 /* assume this means it's off */
4816 wdev->ps = false;
4817
4818unlock_rdev:
4819 cfg80211_unlock_rdev(rdev);
4820 dev_put(dev);
4821 rtnl_unlock();
4822
4823out:
4824 return err;
4825}
4826
4827static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4828{
4829 struct cfg80211_registered_device *rdev;
4830 enum nl80211_ps_state ps_state;
4831 struct wireless_dev *wdev;
4832 struct net_device *dev;
4833 struct sk_buff *msg;
4834 void *hdr;
4835 int err;
4836
4837 rtnl_lock();
4838
4839 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4840 if (err)
4841 goto unlock_rtnl;
4842
4843 wdev = dev->ieee80211_ptr;
4844
4845 if (!rdev->ops->set_power_mgmt) {
4846 err = -EOPNOTSUPP;
4847 goto out;
4848 }
4849
4850 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4851 if (!msg) {
4852 err = -ENOMEM;
4853 goto out;
4854 }
4855
4856 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4857 NL80211_CMD_GET_POWER_SAVE);
4858 if (!hdr) {
4859 err = -ENOMEM;
4860 goto free_msg;
4861 }
4862
4863 if (wdev->ps)
4864 ps_state = NL80211_PS_ENABLED;
4865 else
4866 ps_state = NL80211_PS_DISABLED;
4867
4868 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4869
4870 genlmsg_end(msg, hdr);
4871 err = genlmsg_reply(msg, info);
4872 goto out;
4873
4874nla_put_failure:
4875 err = -ENOBUFS;
4876
4877free_msg:
4878 nlmsg_free(msg);
4879
4880out:
4881 cfg80211_unlock_rdev(rdev);
4882 dev_put(dev);
4883
4884unlock_rtnl:
4885 rtnl_unlock();
4886
4887 return err;
4888}
4889
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004890static struct nla_policy
4891nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4892 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4893 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4894 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4895};
4896
4897static int nl80211_set_cqm_rssi(struct genl_info *info,
4898 s32 threshold, u32 hysteresis)
4899{
4900 struct cfg80211_registered_device *rdev;
4901 struct wireless_dev *wdev;
4902 struct net_device *dev;
4903 int err;
4904
4905 if (threshold > 0)
4906 return -EINVAL;
4907
4908 rtnl_lock();
4909
4910 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4911 if (err)
4912 goto unlock_rdev;
4913
4914 wdev = dev->ieee80211_ptr;
4915
4916 if (!rdev->ops->set_cqm_rssi_config) {
4917 err = -EOPNOTSUPP;
4918 goto unlock_rdev;
4919 }
4920
4921 if (wdev->iftype != NL80211_IFTYPE_STATION) {
4922 err = -EOPNOTSUPP;
4923 goto unlock_rdev;
4924 }
4925
4926 err = rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4927 threshold, hysteresis);
4928
4929unlock_rdev:
4930 cfg80211_unlock_rdev(rdev);
4931 dev_put(dev);
4932 rtnl_unlock();
4933
4934 return err;
4935}
4936
4937static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4938{
4939 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4940 struct nlattr *cqm;
4941 int err;
4942
4943 cqm = info->attrs[NL80211_ATTR_CQM];
4944 if (!cqm) {
4945 err = -EINVAL;
4946 goto out;
4947 }
4948
4949 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4950 nl80211_attr_cqm_policy);
4951 if (err)
4952 goto out;
4953
4954 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4955 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4956 s32 threshold;
4957 u32 hysteresis;
4958 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4959 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4960 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4961 } else
4962 err = -EINVAL;
4963
4964out:
4965 return err;
4966}
4967
Johannes Berg55682962007-09-20 13:09:35 -04004968static struct genl_ops nl80211_ops[] = {
4969 {
4970 .cmd = NL80211_CMD_GET_WIPHY,
4971 .doit = nl80211_get_wiphy,
4972 .dumpit = nl80211_dump_wiphy,
4973 .policy = nl80211_policy,
4974 /* can be retrieved by unprivileged users */
4975 },
4976 {
4977 .cmd = NL80211_CMD_SET_WIPHY,
4978 .doit = nl80211_set_wiphy,
4979 .policy = nl80211_policy,
4980 .flags = GENL_ADMIN_PERM,
4981 },
4982 {
4983 .cmd = NL80211_CMD_GET_INTERFACE,
4984 .doit = nl80211_get_interface,
4985 .dumpit = nl80211_dump_interface,
4986 .policy = nl80211_policy,
4987 /* can be retrieved by unprivileged users */
4988 },
4989 {
4990 .cmd = NL80211_CMD_SET_INTERFACE,
4991 .doit = nl80211_set_interface,
4992 .policy = nl80211_policy,
4993 .flags = GENL_ADMIN_PERM,
4994 },
4995 {
4996 .cmd = NL80211_CMD_NEW_INTERFACE,
4997 .doit = nl80211_new_interface,
4998 .policy = nl80211_policy,
4999 .flags = GENL_ADMIN_PERM,
5000 },
5001 {
5002 .cmd = NL80211_CMD_DEL_INTERFACE,
5003 .doit = nl80211_del_interface,
5004 .policy = nl80211_policy,
5005 .flags = GENL_ADMIN_PERM,
5006 },
Johannes Berg41ade002007-12-19 02:03:29 +01005007 {
5008 .cmd = NL80211_CMD_GET_KEY,
5009 .doit = nl80211_get_key,
5010 .policy = nl80211_policy,
5011 .flags = GENL_ADMIN_PERM,
5012 },
5013 {
5014 .cmd = NL80211_CMD_SET_KEY,
5015 .doit = nl80211_set_key,
5016 .policy = nl80211_policy,
5017 .flags = GENL_ADMIN_PERM,
5018 },
5019 {
5020 .cmd = NL80211_CMD_NEW_KEY,
5021 .doit = nl80211_new_key,
5022 .policy = nl80211_policy,
5023 .flags = GENL_ADMIN_PERM,
5024 },
5025 {
5026 .cmd = NL80211_CMD_DEL_KEY,
5027 .doit = nl80211_del_key,
5028 .policy = nl80211_policy,
5029 .flags = GENL_ADMIN_PERM,
5030 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005031 {
5032 .cmd = NL80211_CMD_SET_BEACON,
5033 .policy = nl80211_policy,
5034 .flags = GENL_ADMIN_PERM,
5035 .doit = nl80211_addset_beacon,
5036 },
5037 {
5038 .cmd = NL80211_CMD_NEW_BEACON,
5039 .policy = nl80211_policy,
5040 .flags = GENL_ADMIN_PERM,
5041 .doit = nl80211_addset_beacon,
5042 },
5043 {
5044 .cmd = NL80211_CMD_DEL_BEACON,
5045 .policy = nl80211_policy,
5046 .flags = GENL_ADMIN_PERM,
5047 .doit = nl80211_del_beacon,
5048 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005049 {
5050 .cmd = NL80211_CMD_GET_STATION,
5051 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005052 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005053 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01005054 },
5055 {
5056 .cmd = NL80211_CMD_SET_STATION,
5057 .doit = nl80211_set_station,
5058 .policy = nl80211_policy,
5059 .flags = GENL_ADMIN_PERM,
5060 },
5061 {
5062 .cmd = NL80211_CMD_NEW_STATION,
5063 .doit = nl80211_new_station,
5064 .policy = nl80211_policy,
5065 .flags = GENL_ADMIN_PERM,
5066 },
5067 {
5068 .cmd = NL80211_CMD_DEL_STATION,
5069 .doit = nl80211_del_station,
5070 .policy = nl80211_policy,
5071 .flags = GENL_ADMIN_PERM,
5072 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005073 {
5074 .cmd = NL80211_CMD_GET_MPATH,
5075 .doit = nl80211_get_mpath,
5076 .dumpit = nl80211_dump_mpath,
5077 .policy = nl80211_policy,
5078 .flags = GENL_ADMIN_PERM,
5079 },
5080 {
5081 .cmd = NL80211_CMD_SET_MPATH,
5082 .doit = nl80211_set_mpath,
5083 .policy = nl80211_policy,
5084 .flags = GENL_ADMIN_PERM,
5085 },
5086 {
5087 .cmd = NL80211_CMD_NEW_MPATH,
5088 .doit = nl80211_new_mpath,
5089 .policy = nl80211_policy,
5090 .flags = GENL_ADMIN_PERM,
5091 },
5092 {
5093 .cmd = NL80211_CMD_DEL_MPATH,
5094 .doit = nl80211_del_mpath,
5095 .policy = nl80211_policy,
5096 .flags = GENL_ADMIN_PERM,
5097 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005098 {
5099 .cmd = NL80211_CMD_SET_BSS,
5100 .doit = nl80211_set_bss,
5101 .policy = nl80211_policy,
5102 .flags = GENL_ADMIN_PERM,
5103 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005104 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005105 .cmd = NL80211_CMD_GET_REG,
5106 .doit = nl80211_get_reg,
5107 .policy = nl80211_policy,
5108 /* can be retrieved by unprivileged users */
5109 },
5110 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005111 .cmd = NL80211_CMD_SET_REG,
5112 .doit = nl80211_set_reg,
5113 .policy = nl80211_policy,
5114 .flags = GENL_ADMIN_PERM,
5115 },
5116 {
5117 .cmd = NL80211_CMD_REQ_SET_REG,
5118 .doit = nl80211_req_set_reg,
5119 .policy = nl80211_policy,
5120 .flags = GENL_ADMIN_PERM,
5121 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005122 {
5123 .cmd = NL80211_CMD_GET_MESH_PARAMS,
5124 .doit = nl80211_get_mesh_params,
5125 .policy = nl80211_policy,
5126 /* can be retrieved by unprivileged users */
5127 },
5128 {
5129 .cmd = NL80211_CMD_SET_MESH_PARAMS,
5130 .doit = nl80211_set_mesh_params,
5131 .policy = nl80211_policy,
5132 .flags = GENL_ADMIN_PERM,
5133 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005134 {
Johannes Berg2a519312009-02-10 21:25:55 +01005135 .cmd = NL80211_CMD_TRIGGER_SCAN,
5136 .doit = nl80211_trigger_scan,
5137 .policy = nl80211_policy,
5138 .flags = GENL_ADMIN_PERM,
5139 },
5140 {
5141 .cmd = NL80211_CMD_GET_SCAN,
5142 .policy = nl80211_policy,
5143 .dumpit = nl80211_dump_scan,
5144 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005145 {
5146 .cmd = NL80211_CMD_AUTHENTICATE,
5147 .doit = nl80211_authenticate,
5148 .policy = nl80211_policy,
5149 .flags = GENL_ADMIN_PERM,
5150 },
5151 {
5152 .cmd = NL80211_CMD_ASSOCIATE,
5153 .doit = nl80211_associate,
5154 .policy = nl80211_policy,
5155 .flags = GENL_ADMIN_PERM,
5156 },
5157 {
5158 .cmd = NL80211_CMD_DEAUTHENTICATE,
5159 .doit = nl80211_deauthenticate,
5160 .policy = nl80211_policy,
5161 .flags = GENL_ADMIN_PERM,
5162 },
5163 {
5164 .cmd = NL80211_CMD_DISASSOCIATE,
5165 .doit = nl80211_disassociate,
5166 .policy = nl80211_policy,
5167 .flags = GENL_ADMIN_PERM,
5168 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005169 {
5170 .cmd = NL80211_CMD_JOIN_IBSS,
5171 .doit = nl80211_join_ibss,
5172 .policy = nl80211_policy,
5173 .flags = GENL_ADMIN_PERM,
5174 },
5175 {
5176 .cmd = NL80211_CMD_LEAVE_IBSS,
5177 .doit = nl80211_leave_ibss,
5178 .policy = nl80211_policy,
5179 .flags = GENL_ADMIN_PERM,
5180 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005181#ifdef CONFIG_NL80211_TESTMODE
5182 {
5183 .cmd = NL80211_CMD_TESTMODE,
5184 .doit = nl80211_testmode_do,
5185 .policy = nl80211_policy,
5186 .flags = GENL_ADMIN_PERM,
5187 },
5188#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005189 {
5190 .cmd = NL80211_CMD_CONNECT,
5191 .doit = nl80211_connect,
5192 .policy = nl80211_policy,
5193 .flags = GENL_ADMIN_PERM,
5194 },
5195 {
5196 .cmd = NL80211_CMD_DISCONNECT,
5197 .doit = nl80211_disconnect,
5198 .policy = nl80211_policy,
5199 .flags = GENL_ADMIN_PERM,
5200 },
Johannes Berg463d0182009-07-14 00:33:35 +02005201 {
5202 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5203 .doit = nl80211_wiphy_netns,
5204 .policy = nl80211_policy,
5205 .flags = GENL_ADMIN_PERM,
5206 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005207 {
5208 .cmd = NL80211_CMD_GET_SURVEY,
5209 .policy = nl80211_policy,
5210 .dumpit = nl80211_dump_survey,
5211 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005212 {
5213 .cmd = NL80211_CMD_SET_PMKSA,
5214 .doit = nl80211_setdel_pmksa,
5215 .policy = nl80211_policy,
5216 .flags = GENL_ADMIN_PERM,
5217 },
5218 {
5219 .cmd = NL80211_CMD_DEL_PMKSA,
5220 .doit = nl80211_setdel_pmksa,
5221 .policy = nl80211_policy,
5222 .flags = GENL_ADMIN_PERM,
5223 },
5224 {
5225 .cmd = NL80211_CMD_FLUSH_PMKSA,
5226 .doit = nl80211_flush_pmksa,
5227 .policy = nl80211_policy,
5228 .flags = GENL_ADMIN_PERM,
5229 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005230 {
5231 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5232 .doit = nl80211_remain_on_channel,
5233 .policy = nl80211_policy,
5234 .flags = GENL_ADMIN_PERM,
5235 },
5236 {
5237 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5238 .doit = nl80211_cancel_remain_on_channel,
5239 .policy = nl80211_policy,
5240 .flags = GENL_ADMIN_PERM,
5241 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005242 {
5243 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5244 .doit = nl80211_set_tx_bitrate_mask,
5245 .policy = nl80211_policy,
5246 .flags = GENL_ADMIN_PERM,
5247 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005248 {
5249 .cmd = NL80211_CMD_REGISTER_ACTION,
5250 .doit = nl80211_register_action,
5251 .policy = nl80211_policy,
5252 .flags = GENL_ADMIN_PERM,
5253 },
5254 {
5255 .cmd = NL80211_CMD_ACTION,
5256 .doit = nl80211_action,
5257 .policy = nl80211_policy,
5258 .flags = GENL_ADMIN_PERM,
5259 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005260 {
5261 .cmd = NL80211_CMD_SET_POWER_SAVE,
5262 .doit = nl80211_set_power_save,
5263 .policy = nl80211_policy,
5264 .flags = GENL_ADMIN_PERM,
5265 },
5266 {
5267 .cmd = NL80211_CMD_GET_POWER_SAVE,
5268 .doit = nl80211_get_power_save,
5269 .policy = nl80211_policy,
5270 /* can be retrieved by unprivileged users */
5271 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005272 {
5273 .cmd = NL80211_CMD_SET_CQM,
5274 .doit = nl80211_set_cqm,
5275 .policy = nl80211_policy,
5276 .flags = GENL_ADMIN_PERM,
5277 },
Johannes Bergf444de02010-05-05 15:25:02 +02005278 {
5279 .cmd = NL80211_CMD_SET_CHANNEL,
5280 .doit = nl80211_set_channel,
5281 .policy = nl80211_policy,
5282 .flags = GENL_ADMIN_PERM,
5283 },
Johannes Berg55682962007-09-20 13:09:35 -04005284};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005285
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005286static struct genl_multicast_group nl80211_mlme_mcgrp = {
5287 .name = "mlme",
5288};
Johannes Berg55682962007-09-20 13:09:35 -04005289
5290/* multicast groups */
5291static struct genl_multicast_group nl80211_config_mcgrp = {
5292 .name = "config",
5293};
Johannes Berg2a519312009-02-10 21:25:55 +01005294static struct genl_multicast_group nl80211_scan_mcgrp = {
5295 .name = "scan",
5296};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005297static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5298 .name = "regulatory",
5299};
Johannes Berg55682962007-09-20 13:09:35 -04005300
5301/* notification functions */
5302
5303void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5304{
5305 struct sk_buff *msg;
5306
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005307 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005308 if (!msg)
5309 return;
5310
5311 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5312 nlmsg_free(msg);
5313 return;
5314 }
5315
Johannes Berg463d0182009-07-14 00:33:35 +02005316 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5317 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005318}
5319
Johannes Berg362a4152009-05-24 16:43:15 +02005320static int nl80211_add_scan_req(struct sk_buff *msg,
5321 struct cfg80211_registered_device *rdev)
5322{
5323 struct cfg80211_scan_request *req = rdev->scan_req;
5324 struct nlattr *nest;
5325 int i;
5326
Johannes Berg667503d2009-07-07 03:56:11 +02005327 ASSERT_RDEV_LOCK(rdev);
5328
Johannes Berg362a4152009-05-24 16:43:15 +02005329 if (WARN_ON(!req))
5330 return 0;
5331
5332 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5333 if (!nest)
5334 goto nla_put_failure;
5335 for (i = 0; i < req->n_ssids; i++)
5336 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5337 nla_nest_end(msg, nest);
5338
5339 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5340 if (!nest)
5341 goto nla_put_failure;
5342 for (i = 0; i < req->n_channels; i++)
5343 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5344 nla_nest_end(msg, nest);
5345
5346 if (req->ie)
5347 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5348
5349 return 0;
5350 nla_put_failure:
5351 return -ENOBUFS;
5352}
5353
Johannes Berga538e2d2009-06-16 19:56:42 +02005354static int nl80211_send_scan_msg(struct sk_buff *msg,
5355 struct cfg80211_registered_device *rdev,
5356 struct net_device *netdev,
5357 u32 pid, u32 seq, int flags,
5358 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005359{
5360 void *hdr;
5361
5362 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5363 if (!hdr)
5364 return -1;
5365
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005366 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005367 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5368
Johannes Berg362a4152009-05-24 16:43:15 +02005369 /* ignore errors and send incomplete event anyway */
5370 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005371
5372 return genlmsg_end(msg, hdr);
5373
5374 nla_put_failure:
5375 genlmsg_cancel(msg, hdr);
5376 return -EMSGSIZE;
5377}
5378
Johannes Berga538e2d2009-06-16 19:56:42 +02005379void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5380 struct net_device *netdev)
5381{
5382 struct sk_buff *msg;
5383
5384 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5385 if (!msg)
5386 return;
5387
5388 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5389 NL80211_CMD_TRIGGER_SCAN) < 0) {
5390 nlmsg_free(msg);
5391 return;
5392 }
5393
Johannes Berg463d0182009-07-14 00:33:35 +02005394 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5395 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005396}
5397
Johannes Berg2a519312009-02-10 21:25:55 +01005398void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5399 struct net_device *netdev)
5400{
5401 struct sk_buff *msg;
5402
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005404 if (!msg)
5405 return;
5406
Johannes Berga538e2d2009-06-16 19:56:42 +02005407 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5408 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005409 nlmsg_free(msg);
5410 return;
5411 }
5412
Johannes Berg463d0182009-07-14 00:33:35 +02005413 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5414 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005415}
5416
5417void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5418 struct net_device *netdev)
5419{
5420 struct sk_buff *msg;
5421
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005423 if (!msg)
5424 return;
5425
Johannes Berga538e2d2009-06-16 19:56:42 +02005426 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5427 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005428 nlmsg_free(msg);
5429 return;
5430 }
5431
Johannes Berg463d0182009-07-14 00:33:35 +02005432 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5433 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005434}
5435
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005436/*
5437 * This can happen on global regulatory changes or device specific settings
5438 * based on custom world regulatory domains.
5439 */
5440void nl80211_send_reg_change_event(struct regulatory_request *request)
5441{
5442 struct sk_buff *msg;
5443 void *hdr;
5444
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005445 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005446 if (!msg)
5447 return;
5448
5449 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5450 if (!hdr) {
5451 nlmsg_free(msg);
5452 return;
5453 }
5454
5455 /* Userspace can always count this one always being set */
5456 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5457
5458 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5459 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5460 NL80211_REGDOM_TYPE_WORLD);
5461 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5462 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5463 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5464 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5465 request->intersect)
5466 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5467 NL80211_REGDOM_TYPE_INTERSECTION);
5468 else {
5469 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5470 NL80211_REGDOM_TYPE_COUNTRY);
5471 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5472 }
5473
5474 if (wiphy_idx_valid(request->wiphy_idx))
5475 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5476
5477 if (genlmsg_end(msg, hdr) < 0) {
5478 nlmsg_free(msg);
5479 return;
5480 }
5481
Johannes Bergbc43b282009-07-25 10:54:13 +02005482 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005483 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005484 GFP_ATOMIC);
5485 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005486
5487 return;
5488
5489nla_put_failure:
5490 genlmsg_cancel(msg, hdr);
5491 nlmsg_free(msg);
5492}
5493
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005494static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5495 struct net_device *netdev,
5496 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005497 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005498{
5499 struct sk_buff *msg;
5500 void *hdr;
5501
Johannes Berge6d6e342009-07-01 21:26:47 +02005502 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005503 if (!msg)
5504 return;
5505
5506 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5507 if (!hdr) {
5508 nlmsg_free(msg);
5509 return;
5510 }
5511
5512 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5513 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5514 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5515
5516 if (genlmsg_end(msg, hdr) < 0) {
5517 nlmsg_free(msg);
5518 return;
5519 }
5520
Johannes Berg463d0182009-07-14 00:33:35 +02005521 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5522 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005523 return;
5524
5525 nla_put_failure:
5526 genlmsg_cancel(msg, hdr);
5527 nlmsg_free(msg);
5528}
5529
5530void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005531 struct net_device *netdev, const u8 *buf,
5532 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005533{
5534 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005535 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005536}
5537
5538void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5539 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005540 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005541{
Johannes Berge6d6e342009-07-01 21:26:47 +02005542 nl80211_send_mlme_event(rdev, netdev, buf, len,
5543 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005544}
5545
Jouni Malinen53b46b82009-03-27 20:53:56 +02005546void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005547 struct net_device *netdev, const u8 *buf,
5548 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005549{
5550 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005551 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005552}
5553
Jouni Malinen53b46b82009-03-27 20:53:56 +02005554void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5555 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005556 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005557{
5558 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005559 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005560}
5561
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005562static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5563 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005564 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005565{
5566 struct sk_buff *msg;
5567 void *hdr;
5568
Johannes Berge6d6e342009-07-01 21:26:47 +02005569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005570 if (!msg)
5571 return;
5572
5573 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5574 if (!hdr) {
5575 nlmsg_free(msg);
5576 return;
5577 }
5578
5579 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5580 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5581 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5582 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5583
5584 if (genlmsg_end(msg, hdr) < 0) {
5585 nlmsg_free(msg);
5586 return;
5587 }
5588
Johannes Berg463d0182009-07-14 00:33:35 +02005589 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5590 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005591 return;
5592
5593 nla_put_failure:
5594 genlmsg_cancel(msg, hdr);
5595 nlmsg_free(msg);
5596}
5597
5598void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005599 struct net_device *netdev, const u8 *addr,
5600 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005601{
5602 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005603 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005604}
5605
5606void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005607 struct net_device *netdev, const u8 *addr,
5608 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005609{
Johannes Berge6d6e342009-07-01 21:26:47 +02005610 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5611 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005612}
5613
Samuel Ortizb23aa672009-07-01 21:26:54 +02005614void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5615 struct net_device *netdev, const u8 *bssid,
5616 const u8 *req_ie, size_t req_ie_len,
5617 const u8 *resp_ie, size_t resp_ie_len,
5618 u16 status, gfp_t gfp)
5619{
5620 struct sk_buff *msg;
5621 void *hdr;
5622
5623 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5624 if (!msg)
5625 return;
5626
5627 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5628 if (!hdr) {
5629 nlmsg_free(msg);
5630 return;
5631 }
5632
5633 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5634 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5635 if (bssid)
5636 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5637 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5638 if (req_ie)
5639 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5640 if (resp_ie)
5641 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5642
5643 if (genlmsg_end(msg, hdr) < 0) {
5644 nlmsg_free(msg);
5645 return;
5646 }
5647
Johannes Berg463d0182009-07-14 00:33:35 +02005648 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5649 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005650 return;
5651
5652 nla_put_failure:
5653 genlmsg_cancel(msg, hdr);
5654 nlmsg_free(msg);
5655
5656}
5657
5658void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5659 struct net_device *netdev, const u8 *bssid,
5660 const u8 *req_ie, size_t req_ie_len,
5661 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5662{
5663 struct sk_buff *msg;
5664 void *hdr;
5665
5666 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5667 if (!msg)
5668 return;
5669
5670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5671 if (!hdr) {
5672 nlmsg_free(msg);
5673 return;
5674 }
5675
5676 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5678 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5679 if (req_ie)
5680 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5681 if (resp_ie)
5682 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5683
5684 if (genlmsg_end(msg, hdr) < 0) {
5685 nlmsg_free(msg);
5686 return;
5687 }
5688
Johannes Berg463d0182009-07-14 00:33:35 +02005689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5690 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005691 return;
5692
5693 nla_put_failure:
5694 genlmsg_cancel(msg, hdr);
5695 nlmsg_free(msg);
5696
5697}
5698
5699void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5700 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005701 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005702{
5703 struct sk_buff *msg;
5704 void *hdr;
5705
Johannes Berg667503d2009-07-07 03:56:11 +02005706 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005707 if (!msg)
5708 return;
5709
5710 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5711 if (!hdr) {
5712 nlmsg_free(msg);
5713 return;
5714 }
5715
5716 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5717 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5718 if (from_ap && reason)
5719 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5720 if (from_ap)
5721 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5722 if (ie)
5723 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5724
5725 if (genlmsg_end(msg, hdr) < 0) {
5726 nlmsg_free(msg);
5727 return;
5728 }
5729
Johannes Berg463d0182009-07-14 00:33:35 +02005730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5731 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005732 return;
5733
5734 nla_put_failure:
5735 genlmsg_cancel(msg, hdr);
5736 nlmsg_free(msg);
5737
5738}
5739
Johannes Berg04a773a2009-04-19 21:24:32 +02005740void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5741 struct net_device *netdev, const u8 *bssid,
5742 gfp_t gfp)
5743{
5744 struct sk_buff *msg;
5745 void *hdr;
5746
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005748 if (!msg)
5749 return;
5750
5751 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5752 if (!hdr) {
5753 nlmsg_free(msg);
5754 return;
5755 }
5756
5757 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5758 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5759 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5760
5761 if (genlmsg_end(msg, hdr) < 0) {
5762 nlmsg_free(msg);
5763 return;
5764 }
5765
Johannes Berg463d0182009-07-14 00:33:35 +02005766 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5767 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005768 return;
5769
5770 nla_put_failure:
5771 genlmsg_cancel(msg, hdr);
5772 nlmsg_free(msg);
5773}
5774
Jouni Malinena3b8b052009-03-27 21:59:49 +02005775void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5776 struct net_device *netdev, const u8 *addr,
5777 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005778 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005779{
5780 struct sk_buff *msg;
5781 void *hdr;
5782
Johannes Berge6d6e342009-07-01 21:26:47 +02005783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005784 if (!msg)
5785 return;
5786
5787 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5788 if (!hdr) {
5789 nlmsg_free(msg);
5790 return;
5791 }
5792
5793 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5794 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5795 if (addr)
5796 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5797 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5798 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5799 if (tsc)
5800 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5801
5802 if (genlmsg_end(msg, hdr) < 0) {
5803 nlmsg_free(msg);
5804 return;
5805 }
5806
Johannes Berg463d0182009-07-14 00:33:35 +02005807 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5808 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005809 return;
5810
5811 nla_put_failure:
5812 genlmsg_cancel(msg, hdr);
5813 nlmsg_free(msg);
5814}
5815
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005816void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5817 struct ieee80211_channel *channel_before,
5818 struct ieee80211_channel *channel_after)
5819{
5820 struct sk_buff *msg;
5821 void *hdr;
5822 struct nlattr *nl_freq;
5823
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005825 if (!msg)
5826 return;
5827
5828 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5829 if (!hdr) {
5830 nlmsg_free(msg);
5831 return;
5832 }
5833
5834 /*
5835 * Since we are applying the beacon hint to a wiphy we know its
5836 * wiphy_idx is valid
5837 */
5838 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5839
5840 /* Before */
5841 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5842 if (!nl_freq)
5843 goto nla_put_failure;
5844 if (nl80211_msg_put_channel(msg, channel_before))
5845 goto nla_put_failure;
5846 nla_nest_end(msg, nl_freq);
5847
5848 /* After */
5849 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5850 if (!nl_freq)
5851 goto nla_put_failure;
5852 if (nl80211_msg_put_channel(msg, channel_after))
5853 goto nla_put_failure;
5854 nla_nest_end(msg, nl_freq);
5855
5856 if (genlmsg_end(msg, hdr) < 0) {
5857 nlmsg_free(msg);
5858 return;
5859 }
5860
Johannes Berg463d0182009-07-14 00:33:35 +02005861 rcu_read_lock();
5862 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5863 GFP_ATOMIC);
5864 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005865
5866 return;
5867
5868nla_put_failure:
5869 genlmsg_cancel(msg, hdr);
5870 nlmsg_free(msg);
5871}
5872
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005873static void nl80211_send_remain_on_chan_event(
5874 int cmd, struct cfg80211_registered_device *rdev,
5875 struct net_device *netdev, u64 cookie,
5876 struct ieee80211_channel *chan,
5877 enum nl80211_channel_type channel_type,
5878 unsigned int duration, gfp_t gfp)
5879{
5880 struct sk_buff *msg;
5881 void *hdr;
5882
5883 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5884 if (!msg)
5885 return;
5886
5887 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5888 if (!hdr) {
5889 nlmsg_free(msg);
5890 return;
5891 }
5892
5893 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5894 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5895 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5896 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5897 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5898
5899 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5900 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5901
5902 if (genlmsg_end(msg, hdr) < 0) {
5903 nlmsg_free(msg);
5904 return;
5905 }
5906
5907 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5908 nl80211_mlme_mcgrp.id, gfp);
5909 return;
5910
5911 nla_put_failure:
5912 genlmsg_cancel(msg, hdr);
5913 nlmsg_free(msg);
5914}
5915
5916void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5917 struct net_device *netdev, u64 cookie,
5918 struct ieee80211_channel *chan,
5919 enum nl80211_channel_type channel_type,
5920 unsigned int duration, gfp_t gfp)
5921{
5922 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5923 rdev, netdev, cookie, chan,
5924 channel_type, duration, gfp);
5925}
5926
5927void nl80211_send_remain_on_channel_cancel(
5928 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5929 u64 cookie, struct ieee80211_channel *chan,
5930 enum nl80211_channel_type channel_type, gfp_t gfp)
5931{
5932 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5933 rdev, netdev, cookie, chan,
5934 channel_type, 0, gfp);
5935}
5936
Johannes Berg98b62182009-12-23 13:15:44 +01005937void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5938 struct net_device *dev, const u8 *mac_addr,
5939 struct station_info *sinfo, gfp_t gfp)
5940{
5941 struct sk_buff *msg;
5942
5943 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5944 if (!msg)
5945 return;
5946
5947 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5948 nlmsg_free(msg);
5949 return;
5950 }
5951
5952 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5953 nl80211_mlme_mcgrp.id, gfp);
5954}
5955
Jouni Malinen026331c2010-02-15 12:53:10 +02005956int nl80211_send_action(struct cfg80211_registered_device *rdev,
5957 struct net_device *netdev, u32 nlpid,
5958 int freq, const u8 *buf, size_t len, gfp_t gfp)
5959{
5960 struct sk_buff *msg;
5961 void *hdr;
5962 int err;
5963
5964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5965 if (!msg)
5966 return -ENOMEM;
5967
5968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION);
5969 if (!hdr) {
5970 nlmsg_free(msg);
5971 return -ENOMEM;
5972 }
5973
5974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5976 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5977 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5978
5979 err = genlmsg_end(msg, hdr);
5980 if (err < 0) {
5981 nlmsg_free(msg);
5982 return err;
5983 }
5984
5985 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5986 if (err < 0)
5987 return err;
5988 return 0;
5989
5990 nla_put_failure:
5991 genlmsg_cancel(msg, hdr);
5992 nlmsg_free(msg);
5993 return -ENOBUFS;
5994}
5995
5996void nl80211_send_action_tx_status(struct cfg80211_registered_device *rdev,
5997 struct net_device *netdev, u64 cookie,
5998 const u8 *buf, size_t len, bool ack,
5999 gfp_t gfp)
6000{
6001 struct sk_buff *msg;
6002 void *hdr;
6003
6004 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6005 if (!msg)
6006 return;
6007
6008 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ACTION_TX_STATUS);
6009 if (!hdr) {
6010 nlmsg_free(msg);
6011 return;
6012 }
6013
6014 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6015 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6016 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6017 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6018 if (ack)
6019 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6020
6021 if (genlmsg_end(msg, hdr) < 0) {
6022 nlmsg_free(msg);
6023 return;
6024 }
6025
6026 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6027 return;
6028
6029 nla_put_failure:
6030 genlmsg_cancel(msg, hdr);
6031 nlmsg_free(msg);
6032}
6033
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006034void
6035nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6036 struct net_device *netdev,
6037 enum nl80211_cqm_rssi_threshold_event rssi_event,
6038 gfp_t gfp)
6039{
6040 struct sk_buff *msg;
6041 struct nlattr *pinfoattr;
6042 void *hdr;
6043
6044 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6045 if (!msg)
6046 return;
6047
6048 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6049 if (!hdr) {
6050 nlmsg_free(msg);
6051 return;
6052 }
6053
6054 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6055 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6056
6057 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6058 if (!pinfoattr)
6059 goto nla_put_failure;
6060
6061 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6062 rssi_event);
6063
6064 nla_nest_end(msg, pinfoattr);
6065
6066 if (genlmsg_end(msg, hdr) < 0) {
6067 nlmsg_free(msg);
6068 return;
6069 }
6070
6071 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6072 nl80211_mlme_mcgrp.id, gfp);
6073 return;
6074
6075 nla_put_failure:
6076 genlmsg_cancel(msg, hdr);
6077 nlmsg_free(msg);
6078}
6079
Jouni Malinen026331c2010-02-15 12:53:10 +02006080static int nl80211_netlink_notify(struct notifier_block * nb,
6081 unsigned long state,
6082 void *_notify)
6083{
6084 struct netlink_notify *notify = _notify;
6085 struct cfg80211_registered_device *rdev;
6086 struct wireless_dev *wdev;
6087
6088 if (state != NETLINK_URELEASE)
6089 return NOTIFY_DONE;
6090
6091 rcu_read_lock();
6092
6093 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6094 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
6095 cfg80211_mlme_unregister_actions(wdev, notify->pid);
6096
6097 rcu_read_unlock();
6098
6099 return NOTIFY_DONE;
6100}
6101
6102static struct notifier_block nl80211_netlink_notifier = {
6103 .notifier_call = nl80211_netlink_notify,
6104};
6105
Johannes Berg55682962007-09-20 13:09:35 -04006106/* initialisation/exit functions */
6107
6108int nl80211_init(void)
6109{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006110 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006111
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006112 err = genl_register_family_with_ops(&nl80211_fam,
6113 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006114 if (err)
6115 return err;
6116
Johannes Berg55682962007-09-20 13:09:35 -04006117 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6118 if (err)
6119 goto err_out;
6120
Johannes Berg2a519312009-02-10 21:25:55 +01006121 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6122 if (err)
6123 goto err_out;
6124
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006125 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6126 if (err)
6127 goto err_out;
6128
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006129 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6130 if (err)
6131 goto err_out;
6132
Johannes Bergaff89a92009-07-01 21:26:51 +02006133#ifdef CONFIG_NL80211_TESTMODE
6134 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6135 if (err)
6136 goto err_out;
6137#endif
6138
Jouni Malinen026331c2010-02-15 12:53:10 +02006139 err = netlink_register_notifier(&nl80211_netlink_notifier);
6140 if (err)
6141 goto err_out;
6142
Johannes Berg55682962007-09-20 13:09:35 -04006143 return 0;
6144 err_out:
6145 genl_unregister_family(&nl80211_fam);
6146 return err;
6147}
6148
6149void nl80211_exit(void)
6150{
Jouni Malinen026331c2010-02-15 12:53:10 +02006151 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006152 genl_unregister_family(&nl80211_fam);
6153}