blob: a6028433e3a0f9583e48c5b2369abed53ed0ddd5 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Johannes Berg08645122009-05-11 13:54:58 +02004 * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020017#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040018#include <net/genetlink.h>
19#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020020#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040021#include "core.h"
22#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070023#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040024
25/* the netlink family */
26static struct genl_family nl80211_fam = {
27 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
28 .name = "nl80211", /* have users key off the name instead */
29 .hdrsize = 0, /* no private header */
30 .version = 1, /* no particular meaning now */
31 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020032 .netnsok = true,
Johannes Berg55682962007-09-20 13:09:35 -040033};
34
Johannes Berg79c97e92009-07-07 03:56:12 +020035/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020036static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020037 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040038 struct net_device **dev)
39{
Johannes Berg463d0182009-07-14 00:33:35 +020040 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040041 int ifindex;
42
Johannes Bergbba95fe2008-07-29 13:22:51 +020043 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040044 return -EINVAL;
45
Johannes Bergbba95fe2008-07-29 13:22:51 +020046 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020047 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040048 if (!*dev)
49 return -ENODEV;
50
Johannes Berg463d0182009-07-14 00:33:35 +020051 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020052 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040053 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020054 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040055 }
56
57 return 0;
58}
59
60/* policy for the attributes */
61static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
62 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
63 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070064 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020065 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020066 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053067 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020068 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
69 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
70 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040072
73 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
74 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
75 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010076
77 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020078 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010079
Johannes Bergb9454e82009-07-08 13:29:08 +020080 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010081 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
82 .len = WLAN_MAX_KEY_LEN },
83 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
84 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
85 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030086 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010087
88 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
89 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
90 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
91 .len = IEEE80211_MAX_DATA_LEN },
92 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010094 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
95 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
96 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
97 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
98 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010099 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100100 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200101 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100102 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_MESH_ID_LEN },
104 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700106 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
107 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
108
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300109 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
110 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
111 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200112 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
113 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300114
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700115 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
116
Jouni Malinen36aedc92008-08-25 11:58:58 +0300117 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
118 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200119
120 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
121 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
122 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100123 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
124 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200125
126 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
127 .len = IEEE80211_MAX_SSID_LEN },
128 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
129 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200130 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300131 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300132 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300133 [NL80211_ATTR_STA_FLAGS2] = {
134 .len = sizeof(struct nl80211_sta_flag_update),
135 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300136 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200137 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
138 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
139 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200140 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100141 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100142 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
143 .len = WLAN_PMKID_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400144};
145
Johannes Bergb9454e82009-07-08 13:29:08 +0200146/* policy for the attributes */
147static struct nla_policy
148nl80211_key_policy[NL80211_KEY_MAX + 1] __read_mostly = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200149 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200150 [NL80211_KEY_IDX] = { .type = NLA_U8 },
151 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
152 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
153 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
154 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
155};
156
Holger Schuriga0438972009-11-11 11:30:02 +0100157/* ifidx get helper */
158static int nl80211_get_ifidx(struct netlink_callback *cb)
159{
160 int res;
161
162 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
163 nl80211_fam.attrbuf, nl80211_fam.maxattr,
164 nl80211_policy);
165 if (res)
166 return res;
167
168 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
169 return -EINVAL;
170
171 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
172 if (!res)
173 return -EINVAL;
174 return res;
175}
176
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100177/* IE validation */
178static bool is_valid_ie_attr(const struct nlattr *attr)
179{
180 const u8 *pos;
181 int len;
182
183 if (!attr)
184 return true;
185
186 pos = nla_data(attr);
187 len = nla_len(attr);
188
189 while (len) {
190 u8 elemlen;
191
192 if (len < 2)
193 return false;
194 len -= 2;
195
196 elemlen = pos[1];
197 if (elemlen > len)
198 return false;
199
200 len -= elemlen;
201 pos += 2 + elemlen;
202 }
203
204 return true;
205}
206
Johannes Berg55682962007-09-20 13:09:35 -0400207/* message building helper */
208static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
209 int flags, u8 cmd)
210{
211 /* since there is no private header just add the generic one */
212 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
213}
214
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400215static int nl80211_msg_put_channel(struct sk_buff *msg,
216 struct ieee80211_channel *chan)
217{
218 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
219 chan->center_freq);
220
221 if (chan->flags & IEEE80211_CHAN_DISABLED)
222 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
223 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
224 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
225 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
226 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
227 if (chan->flags & IEEE80211_CHAN_RADAR)
228 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
229
230 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
231 DBM_TO_MBM(chan->max_power));
232
233 return 0;
234
235 nla_put_failure:
236 return -ENOBUFS;
237}
238
Johannes Berg55682962007-09-20 13:09:35 -0400239/* netlink command implementations */
240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241struct key_parse {
242 struct key_params p;
243 int idx;
244 bool def, defmgmt;
245};
246
247static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
248{
249 struct nlattr *tb[NL80211_KEY_MAX + 1];
250 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
251 nl80211_key_policy);
252 if (err)
253 return err;
254
255 k->def = !!tb[NL80211_KEY_DEFAULT];
256 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
257
258 if (tb[NL80211_KEY_IDX])
259 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
260
261 if (tb[NL80211_KEY_DATA]) {
262 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
263 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
264 }
265
266 if (tb[NL80211_KEY_SEQ]) {
267 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
268 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
269 }
270
271 if (tb[NL80211_KEY_CIPHER])
272 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
273
274 return 0;
275}
276
277static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
278{
279 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
280 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
281 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
282 }
283
284 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
285 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
286 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
287 }
288
289 if (info->attrs[NL80211_ATTR_KEY_IDX])
290 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
291
292 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
293 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
294
295 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
296 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
297
298 return 0;
299}
300
301static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
302{
303 int err;
304
305 memset(k, 0, sizeof(*k));
306 k->idx = -1;
307
308 if (info->attrs[NL80211_ATTR_KEY])
309 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
310 else
311 err = nl80211_parse_key_old(info, k);
312
313 if (err)
314 return err;
315
316 if (k->def && k->defmgmt)
317 return -EINVAL;
318
319 if (k->idx != -1) {
320 if (k->defmgmt) {
321 if (k->idx < 4 || k->idx > 5)
322 return -EINVAL;
323 } else if (k->def) {
324 if (k->idx < 0 || k->idx > 3)
325 return -EINVAL;
326 } else {
327 if (k->idx < 0 || k->idx > 5)
328 return -EINVAL;
329 }
330 }
331
332 return 0;
333}
334
Johannes Bergfffd0932009-07-08 14:22:54 +0200335static struct cfg80211_cached_keys *
336nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
337 struct nlattr *keys)
338{
339 struct key_parse parse;
340 struct nlattr *key;
341 struct cfg80211_cached_keys *result;
342 int rem, err, def = 0;
343
344 result = kzalloc(sizeof(*result), GFP_KERNEL);
345 if (!result)
346 return ERR_PTR(-ENOMEM);
347
348 result->def = -1;
349 result->defmgmt = -1;
350
351 nla_for_each_nested(key, keys, rem) {
352 memset(&parse, 0, sizeof(parse));
353 parse.idx = -1;
354
355 err = nl80211_parse_key_new(key, &parse);
356 if (err)
357 goto error;
358 err = -EINVAL;
359 if (!parse.p.key)
360 goto error;
361 if (parse.idx < 0 || parse.idx > 4)
362 goto error;
363 if (parse.def) {
364 if (def)
365 goto error;
366 def = 1;
367 result->def = parse.idx;
368 } else if (parse.defmgmt)
369 goto error;
370 err = cfg80211_validate_key_settings(rdev, &parse.p,
371 parse.idx, NULL);
372 if (err)
373 goto error;
374 result->params[parse.idx].cipher = parse.p.cipher;
375 result->params[parse.idx].key_len = parse.p.key_len;
376 result->params[parse.idx].key = result->data[parse.idx];
377 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
378 }
379
380 return result;
381 error:
382 kfree(result);
383 return ERR_PTR(err);
384}
385
386static int nl80211_key_allowed(struct wireless_dev *wdev)
387{
388 ASSERT_WDEV_LOCK(wdev);
389
390 if (!netif_running(wdev->netdev))
391 return -ENETDOWN;
392
393 switch (wdev->iftype) {
394 case NL80211_IFTYPE_AP:
395 case NL80211_IFTYPE_AP_VLAN:
396 break;
397 case NL80211_IFTYPE_ADHOC:
398 if (!wdev->current_bss)
399 return -ENOLINK;
400 break;
401 case NL80211_IFTYPE_STATION:
402 if (wdev->sme_state != CFG80211_SME_CONNECTED)
403 return -ENOLINK;
404 break;
405 default:
406 return -EINVAL;
407 }
408
409 return 0;
410}
411
Johannes Berg55682962007-09-20 13:09:35 -0400412static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
413 struct cfg80211_registered_device *dev)
414{
415 void *hdr;
Johannes Bergee688b02008-01-24 19:38:39 +0100416 struct nlattr *nl_bands, *nl_band;
417 struct nlattr *nl_freqs, *nl_freq;
418 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700419 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100420 struct nlattr *nl_cmds;
Johannes Bergee688b02008-01-24 19:38:39 +0100421 enum ieee80211_band band;
422 struct ieee80211_channel *chan;
423 struct ieee80211_rate *rate;
424 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700425 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400426
427 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
428 if (!hdr)
429 return -1;
430
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500431 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400432 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200433
Johannes Bergf5ea9122009-08-07 16:17:38 +0200434 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
435 cfg80211_rdev_list_generation);
436
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200437 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
438 dev->wiphy.retry_short);
439 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
440 dev->wiphy.retry_long);
441 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
442 dev->wiphy.frag_threshold);
443 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
444 dev->wiphy.rts_threshold);
445
Johannes Berg2a519312009-02-10 21:25:55 +0100446 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
447 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200448 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
449 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b02008-01-24 19:38:39 +0100450
Johannes Berg25e47c12009-04-02 20:14:06 +0200451 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
452 sizeof(u32) * dev->wiphy.n_cipher_suites,
453 dev->wiphy.cipher_suites);
454
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100455 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
456 dev->wiphy.max_num_pmkids);
457
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700458 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
459 if (!nl_modes)
460 goto nla_put_failure;
461
462 i = 0;
463 while (ifmodes) {
464 if (ifmodes & 1)
465 NLA_PUT_FLAG(msg, i);
466 ifmodes >>= 1;
467 i++;
468 }
469
470 nla_nest_end(msg, nl_modes);
471
Johannes Bergee688b02008-01-24 19:38:39 +0100472 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
473 if (!nl_bands)
474 goto nla_put_failure;
475
476 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
477 if (!dev->wiphy.bands[band])
478 continue;
479
480 nl_band = nla_nest_start(msg, band);
481 if (!nl_band)
482 goto nla_put_failure;
483
Johannes Bergd51626d2008-10-09 12:20:13 +0200484 /* add HT info */
485 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
486 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
487 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
488 &dev->wiphy.bands[band]->ht_cap.mcs);
489 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
490 dev->wiphy.bands[band]->ht_cap.cap);
491 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
492 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
493 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
494 dev->wiphy.bands[band]->ht_cap.ampdu_density);
495 }
496
Johannes Bergee688b02008-01-24 19:38:39 +0100497 /* add frequencies */
498 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
499 if (!nl_freqs)
500 goto nla_put_failure;
501
502 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
503 nl_freq = nla_nest_start(msg, i);
504 if (!nl_freq)
505 goto nla_put_failure;
506
507 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b02008-01-24 19:38:39 +0100508
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400509 if (nl80211_msg_put_channel(msg, chan))
510 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200511
Johannes Bergee688b02008-01-24 19:38:39 +0100512 nla_nest_end(msg, nl_freq);
513 }
514
515 nla_nest_end(msg, nl_freqs);
516
517 /* add bitrates */
518 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
519 if (!nl_rates)
520 goto nla_put_failure;
521
522 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
523 nl_rate = nla_nest_start(msg, i);
524 if (!nl_rate)
525 goto nla_put_failure;
526
527 rate = &dev->wiphy.bands[band]->bitrates[i];
528 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
529 rate->bitrate);
530 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
531 NLA_PUT_FLAG(msg,
532 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
533
534 nla_nest_end(msg, nl_rate);
535 }
536
537 nla_nest_end(msg, nl_rates);
538
539 nla_nest_end(msg, nl_band);
540 }
541 nla_nest_end(msg, nl_bands);
542
Johannes Berg8fdc6212009-03-14 09:34:01 +0100543 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
544 if (!nl_cmds)
545 goto nla_put_failure;
546
547 i = 0;
548#define CMD(op, n) \
549 do { \
550 if (dev->ops->op) { \
551 i++; \
552 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
553 } \
554 } while (0)
555
556 CMD(add_virtual_intf, NEW_INTERFACE);
557 CMD(change_virtual_intf, SET_INTERFACE);
558 CMD(add_key, NEW_KEY);
559 CMD(add_beacon, NEW_BEACON);
560 CMD(add_station, NEW_STATION);
561 CMD(add_mpath, NEW_MPATH);
562 CMD(set_mesh_params, SET_MESH_PARAMS);
563 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200564 CMD(auth, AUTHENTICATE);
565 CMD(assoc, ASSOCIATE);
566 CMD(deauth, DEAUTHENTICATE);
567 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200568 CMD(join_ibss, JOIN_IBSS);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100569 CMD(set_pmksa, SET_PMKSA);
570 CMD(del_pmksa, DEL_PMKSA);
571 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg5be83de2009-11-19 00:56:28 +0100572 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200573 i++;
574 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
575 }
Johannes Berg8fdc6212009-03-14 09:34:01 +0100576
577#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200578
Johannes Berg6829c872009-07-02 09:13:27 +0200579 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200580 i++;
581 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
582 }
583
Johannes Berg6829c872009-07-02 09:13:27 +0200584 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200585 i++;
586 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
587 }
588
Johannes Berg8fdc6212009-03-14 09:34:01 +0100589 nla_nest_end(msg, nl_cmds);
590
Johannes Berg55682962007-09-20 13:09:35 -0400591 return genlmsg_end(msg, hdr);
592
593 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700594 genlmsg_cancel(msg, hdr);
595 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400596}
597
598static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
599{
600 int idx = 0;
601 int start = cb->args[0];
602 struct cfg80211_registered_device *dev;
603
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500604 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200605 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200606 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
607 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200608 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400609 continue;
610 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
611 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200612 dev) < 0) {
613 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400614 break;
Julius Volzb4637272008-07-08 14:02:19 +0200615 }
Johannes Berg55682962007-09-20 13:09:35 -0400616 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500617 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400618
619 cb->args[0] = idx;
620
621 return skb->len;
622}
623
624static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
625{
626 struct sk_buff *msg;
627 struct cfg80211_registered_device *dev;
628
629 dev = cfg80211_get_dev_from_info(info);
630 if (IS_ERR(dev))
631 return PTR_ERR(dev);
632
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400634 if (!msg)
635 goto out_err;
636
637 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
638 goto out_free;
639
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200640 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400641
Johannes Berg134e6372009-07-10 09:51:34 +0000642 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400643
644 out_free:
645 nlmsg_free(msg);
646 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200647 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400648 return -ENOBUFS;
649}
650
Jouni Malinen31888482008-10-30 16:59:24 +0200651static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
652 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
653 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
654 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
655 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
656 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
657};
658
659static int parse_txq_params(struct nlattr *tb[],
660 struct ieee80211_txq_params *txq_params)
661{
662 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
663 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
664 !tb[NL80211_TXQ_ATTR_AIFS])
665 return -EINVAL;
666
667 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
668 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
669 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
670 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
671 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
672
673 return 0;
674}
675
Johannes Berg55682962007-09-20 13:09:35 -0400676static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
677{
678 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200679 int result = 0, rem_txq_params = 0;
680 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200681 u32 changed;
682 u8 retry_short = 0, retry_long = 0;
683 u32 frag_threshold = 0, rts_threshold = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400684
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100685 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400686
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100687 mutex_lock(&cfg80211_mutex);
688
Johannes Berg79c97e92009-07-07 03:56:12 +0200689 rdev = __cfg80211_rdev_from_info(info);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100690 if (IS_ERR(rdev)) {
Jiri Slaby1f5fc702009-06-20 12:31:11 +0200691 mutex_unlock(&cfg80211_mutex);
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100692 result = PTR_ERR(rdev);
693 goto unlock;
694 }
695
696 mutex_lock(&rdev->mtx);
697
698 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200699 result = cfg80211_dev_rename(
700 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100701
702 mutex_unlock(&cfg80211_mutex);
703
704 if (result)
705 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400706
Jouni Malinen31888482008-10-30 16:59:24 +0200707 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
708 struct ieee80211_txq_params txq_params;
709 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
710
711 if (!rdev->ops->set_txq_params) {
712 result = -EOPNOTSUPP;
713 goto bad_res;
714 }
715
716 nla_for_each_nested(nl_txq_params,
717 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
718 rem_txq_params) {
719 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
720 nla_data(nl_txq_params),
721 nla_len(nl_txq_params),
722 txq_params_policy);
723 result = parse_txq_params(tb, &txq_params);
724 if (result)
725 goto bad_res;
726
727 result = rdev->ops->set_txq_params(&rdev->wiphy,
728 &txq_params);
729 if (result)
730 goto bad_res;
731 }
732 }
733
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200734 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530735 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Luis R. Rodriguez294196a2009-05-02 00:37:20 -0400736 u32 freq;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200737
Johannes Berg306d6112008-12-08 12:39:04 +0100738 result = -EINVAL;
739
Sujith094d05d2008-12-12 11:57:43 +0530740 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
741 channel_type = nla_get_u32(info->attrs[
742 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
743 if (channel_type != NL80211_CHAN_NO_HT &&
744 channel_type != NL80211_CHAN_HT20 &&
745 channel_type != NL80211_CHAN_HT40PLUS &&
746 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200747 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200748 }
749
750 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
Johannes Berg306d6112008-12-08 12:39:04 +0100751
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200752 mutex_lock(&rdev->devlist_mtx);
Johannes Berg4b181142009-08-08 11:03:58 +0200753 result = rdev_set_freq(rdev, NULL, freq, channel_type);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200754 mutex_unlock(&rdev->devlist_mtx);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200755 if (result)
756 goto bad_res;
757 }
758
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200759 changed = 0;
760
761 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
762 retry_short = nla_get_u8(
763 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
764 if (retry_short == 0) {
765 result = -EINVAL;
766 goto bad_res;
767 }
768 changed |= WIPHY_PARAM_RETRY_SHORT;
769 }
770
771 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
772 retry_long = nla_get_u8(
773 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
774 if (retry_long == 0) {
775 result = -EINVAL;
776 goto bad_res;
777 }
778 changed |= WIPHY_PARAM_RETRY_LONG;
779 }
780
781 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
782 frag_threshold = nla_get_u32(
783 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
784 if (frag_threshold < 256) {
785 result = -EINVAL;
786 goto bad_res;
787 }
788 if (frag_threshold != (u32) -1) {
789 /*
790 * Fragments (apart from the last one) are required to
791 * have even length. Make the fragmentation code
792 * simpler by stripping LSB should someone try to use
793 * odd threshold value.
794 */
795 frag_threshold &= ~0x1;
796 }
797 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
798 }
799
800 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
801 rts_threshold = nla_get_u32(
802 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
803 changed |= WIPHY_PARAM_RTS_THRESHOLD;
804 }
805
806 if (changed) {
807 u8 old_retry_short, old_retry_long;
808 u32 old_frag_threshold, old_rts_threshold;
809
810 if (!rdev->ops->set_wiphy_params) {
811 result = -EOPNOTSUPP;
812 goto bad_res;
813 }
814
815 old_retry_short = rdev->wiphy.retry_short;
816 old_retry_long = rdev->wiphy.retry_long;
817 old_frag_threshold = rdev->wiphy.frag_threshold;
818 old_rts_threshold = rdev->wiphy.rts_threshold;
819
820 if (changed & WIPHY_PARAM_RETRY_SHORT)
821 rdev->wiphy.retry_short = retry_short;
822 if (changed & WIPHY_PARAM_RETRY_LONG)
823 rdev->wiphy.retry_long = retry_long;
824 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
825 rdev->wiphy.frag_threshold = frag_threshold;
826 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
827 rdev->wiphy.rts_threshold = rts_threshold;
828
829 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
830 if (result) {
831 rdev->wiphy.retry_short = old_retry_short;
832 rdev->wiphy.retry_long = old_retry_long;
833 rdev->wiphy.frag_threshold = old_frag_threshold;
834 rdev->wiphy.rts_threshold = old_rts_threshold;
835 }
836 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200837
Johannes Berg306d6112008-12-08 12:39:04 +0100838 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100839 mutex_unlock(&rdev->mtx);
840 unlock:
841 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400842 return result;
843}
844
845
846static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +0200847 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -0400848 struct net_device *dev)
849{
850 void *hdr;
851
852 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
853 if (!hdr)
854 return -1;
855
856 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +0200857 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400858 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200859 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200860
861 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
862 rdev->devlist_generation ^
863 (cfg80211_rdev_list_generation << 2));
864
Johannes Berg55682962007-09-20 13:09:35 -0400865 return genlmsg_end(msg, hdr);
866
867 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700868 genlmsg_cancel(msg, hdr);
869 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400870}
871
872static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
873{
874 int wp_idx = 0;
875 int if_idx = 0;
876 int wp_start = cb->args[0];
877 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +0200878 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -0400879 struct wireless_dev *wdev;
880
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500881 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200882 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
883 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +0200884 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200885 if (wp_idx < wp_start) {
886 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400887 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200888 }
Johannes Berg55682962007-09-20 13:09:35 -0400889 if_idx = 0;
890
Johannes Bergf5ea9122009-08-07 16:17:38 +0200891 mutex_lock(&rdev->devlist_mtx);
892 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200893 if (if_idx < if_start) {
894 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400895 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200896 }
Johannes Berg55682962007-09-20 13:09:35 -0400897 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
898 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +0200899 rdev, wdev->netdev) < 0) {
900 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200901 goto out;
902 }
903 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400904 }
Johannes Bergf5ea9122009-08-07 16:17:38 +0200905 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200906
907 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400908 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200909 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500910 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400911
912 cb->args[0] = wp_idx;
913 cb->args[1] = if_idx;
914
915 return skb->len;
916}
917
918static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
919{
920 struct sk_buff *msg;
921 struct cfg80211_registered_device *dev;
922 struct net_device *netdev;
923 int err;
924
Johannes Berg463d0182009-07-14 00:33:35 +0200925 err = get_rdev_dev_by_info_ifindex(info, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400926 if (err)
927 return err;
928
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400930 if (!msg)
931 goto out_err;
932
Johannes Bergd7264052009-04-19 16:23:20 +0200933 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
934 dev, netdev) < 0)
Johannes Berg55682962007-09-20 13:09:35 -0400935 goto out_free;
936
937 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200938 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400939
Johannes Berg134e6372009-07-10 09:51:34 +0000940 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400941
942 out_free:
943 nlmsg_free(msg);
944 out_err:
945 dev_put(netdev);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200946 cfg80211_unlock_rdev(dev);
Johannes Berg55682962007-09-20 13:09:35 -0400947 return -ENOBUFS;
948}
949
Michael Wu66f7ac52008-01-31 19:48:22 +0100950static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
951 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
952 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
953 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
954 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
955 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
956};
957
958static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
959{
960 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
961 int flag;
962
963 *mntrflags = 0;
964
965 if (!nla)
966 return -EINVAL;
967
968 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
969 nla, mntr_flags_policy))
970 return -EINVAL;
971
972 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
973 if (flags[flag])
974 *mntrflags |= (1<<flag);
975
976 return 0;
977}
978
Johannes Berg9bc383d2009-11-19 11:55:19 +0100979static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100980 struct net_device *netdev, u8 use_4addr,
981 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +0100982{
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100983 if (!use_4addr) {
984 if (netdev && netdev->br_port)
985 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100986 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100987 }
Johannes Berg9bc383d2009-11-19 11:55:19 +0100988
989 switch (iftype) {
990 case NL80211_IFTYPE_AP_VLAN:
991 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
992 return 0;
993 break;
994 case NL80211_IFTYPE_STATION:
995 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
996 return 0;
997 break;
998 default:
999 break;
1000 }
1001
1002 return -EOPNOTSUPP;
1003}
1004
Johannes Berg55682962007-09-20 13:09:35 -04001005static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1006{
Johannes Berg79c97e92009-07-07 03:56:12 +02001007 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001008 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001009 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001010 enum nl80211_iftype otype, ntype;
Johannes Berg55682962007-09-20 13:09:35 -04001011 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +02001012 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001013 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001014
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001015 memset(&params, 0, sizeof(params));
1016
Johannes Berg3b858752009-03-12 09:55:09 +01001017 rtnl_lock();
1018
Johannes Berg463d0182009-07-14 00:33:35 +02001019 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001020 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001021 goto unlock_rtnl;
1022
Johannes Berg04a773a2009-04-19 21:24:32 +02001023 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001024
Johannes Berg723b0382008-09-16 20:22:09 +02001025 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001026 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001027 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001028 change = true;
Johannes Berg04a773a2009-04-19 21:24:32 +02001029 if (ntype > NL80211_IFTYPE_MAX) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001030 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001031 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001032 }
Johannes Berg723b0382008-09-16 20:22:09 +02001033 }
1034
Johannes Berg92ffe052008-09-16 20:39:36 +02001035 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001036 if (ntype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001037 err = -EINVAL;
1038 goto unlock;
1039 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001040 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1041 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001042 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001043 }
1044
Felix Fietkau8b787642009-11-10 18:53:10 +01001045 if (info->attrs[NL80211_ATTR_4ADDR]) {
1046 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1047 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001048 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001049 if (err)
1050 goto unlock;
Felix Fietkau8b787642009-11-10 18:53:10 +01001051 } else {
1052 params.use_4addr = -1;
1053 }
1054
Johannes Berg92ffe052008-09-16 20:39:36 +02001055 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02001056 if (ntype != NL80211_IFTYPE_MONITOR) {
Johannes Berg92ffe052008-09-16 20:39:36 +02001057 err = -EINVAL;
1058 goto unlock;
1059 }
1060 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1061 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001062 if (err)
1063 goto unlock;
1064
1065 flags = &_flags;
1066 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001067 }
Johannes Berg3b858752009-03-12 09:55:09 +01001068
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001069 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001070 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001071 else
1072 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001073
Johannes Berg9bc383d2009-11-19 11:55:19 +01001074 if (!err && params.use_4addr != -1)
1075 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1076
Johannes Berg55682962007-09-20 13:09:35 -04001077 unlock:
Johannes Berge36d56b2009-06-09 21:04:43 +02001078 dev_put(dev);
Johannes Berg79c97e92009-07-07 03:56:12 +02001079 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001080 unlock_rtnl:
1081 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001082 return err;
1083}
1084
1085static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1086{
Johannes Berg79c97e92009-07-07 03:56:12 +02001087 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001088 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -04001089 int err;
1090 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001091 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001092
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001093 memset(&params, 0, sizeof(params));
1094
Johannes Berg55682962007-09-20 13:09:35 -04001095 if (!info->attrs[NL80211_ATTR_IFNAME])
1096 return -EINVAL;
1097
1098 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1099 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1100 if (type > NL80211_IFTYPE_MAX)
1101 return -EINVAL;
1102 }
1103
Johannes Berg3b858752009-03-12 09:55:09 +01001104 rtnl_lock();
1105
Johannes Berg79c97e92009-07-07 03:56:12 +02001106 rdev = cfg80211_get_dev_from_info(info);
1107 if (IS_ERR(rdev)) {
1108 err = PTR_ERR(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001109 goto unlock_rtnl;
1110 }
Johannes Berg55682962007-09-20 13:09:35 -04001111
Johannes Berg79c97e92009-07-07 03:56:12 +02001112 if (!rdev->ops->add_virtual_intf ||
1113 !(rdev->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -04001114 err = -EOPNOTSUPP;
1115 goto unlock;
1116 }
1117
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001118 if (type == NL80211_IFTYPE_MESH_POINT &&
1119 info->attrs[NL80211_ATTR_MESH_ID]) {
1120 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1121 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1122 }
1123
Johannes Berg9bc383d2009-11-19 11:55:19 +01001124 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001125 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001126 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001127 if (err)
1128 goto unlock;
1129 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001130
Michael Wu66f7ac52008-01-31 19:48:22 +01001131 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1132 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1133 &flags);
Johannes Berg79c97e92009-07-07 03:56:12 +02001134 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001135 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001136 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001137
Johannes Berg55682962007-09-20 13:09:35 -04001138 unlock:
Johannes Berg79c97e92009-07-07 03:56:12 +02001139 cfg80211_unlock_rdev(rdev);
Johannes Berg3b858752009-03-12 09:55:09 +01001140 unlock_rtnl:
1141 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001142 return err;
1143}
1144
1145static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1146{
Johannes Berg79c97e92009-07-07 03:56:12 +02001147 struct cfg80211_registered_device *rdev;
Johannes Berg463d0182009-07-14 00:33:35 +02001148 int err;
Johannes Berg55682962007-09-20 13:09:35 -04001149 struct net_device *dev;
1150
Johannes Berg3b858752009-03-12 09:55:09 +01001151 rtnl_lock();
1152
Johannes Berg463d0182009-07-14 00:33:35 +02001153 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg55682962007-09-20 13:09:35 -04001154 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001155 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -04001156
Johannes Berg79c97e92009-07-07 03:56:12 +02001157 if (!rdev->ops->del_virtual_intf) {
Johannes Berg55682962007-09-20 13:09:35 -04001158 err = -EOPNOTSUPP;
1159 goto out;
1160 }
1161
Johannes Berg463d0182009-07-14 00:33:35 +02001162 err = rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001163
1164 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001165 cfg80211_unlock_rdev(rdev);
Johannes Berg463d0182009-07-14 00:33:35 +02001166 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001167 unlock_rtnl:
1168 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001169 return err;
1170}
1171
Johannes Berg41ade002007-12-19 02:03:29 +01001172struct get_key_cookie {
1173 struct sk_buff *msg;
1174 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001175 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001176};
1177
1178static void get_key_callback(void *c, struct key_params *params)
1179{
Johannes Bergb9454e82009-07-08 13:29:08 +02001180 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001181 struct get_key_cookie *cookie = c;
1182
1183 if (params->key)
1184 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1185 params->key_len, params->key);
1186
1187 if (params->seq)
1188 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1189 params->seq_len, params->seq);
1190
1191 if (params->cipher)
1192 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1193 params->cipher);
1194
Johannes Bergb9454e82009-07-08 13:29:08 +02001195 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1196 if (!key)
1197 goto nla_put_failure;
1198
1199 if (params->key)
1200 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1201 params->key_len, params->key);
1202
1203 if (params->seq)
1204 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1205 params->seq_len, params->seq);
1206
1207 if (params->cipher)
1208 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1209 params->cipher);
1210
1211 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1212
1213 nla_nest_end(cookie->msg, key);
1214
Johannes Berg41ade002007-12-19 02:03:29 +01001215 return;
1216 nla_put_failure:
1217 cookie->error = 1;
1218}
1219
1220static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1221{
Johannes Berg79c97e92009-07-07 03:56:12 +02001222 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001223 int err;
1224 struct net_device *dev;
1225 u8 key_idx = 0;
1226 u8 *mac_addr = NULL;
1227 struct get_key_cookie cookie = {
1228 .error = 0,
1229 };
1230 void *hdr;
1231 struct sk_buff *msg;
1232
1233 if (info->attrs[NL80211_ATTR_KEY_IDX])
1234 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1235
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001236 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001237 return -EINVAL;
1238
1239 if (info->attrs[NL80211_ATTR_MAC])
1240 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1241
Johannes Berg3b858752009-03-12 09:55:09 +01001242 rtnl_lock();
1243
Johannes Berg463d0182009-07-14 00:33:35 +02001244 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001245 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001246 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001247
Johannes Berg79c97e92009-07-07 03:56:12 +02001248 if (!rdev->ops->get_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001249 err = -EOPNOTSUPP;
1250 goto out;
1251 }
1252
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001253 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg41ade002007-12-19 02:03:29 +01001254 if (!msg) {
1255 err = -ENOMEM;
1256 goto out;
1257 }
1258
1259 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1260 NL80211_CMD_NEW_KEY);
1261
1262 if (IS_ERR(hdr)) {
1263 err = PTR_ERR(hdr);
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001264 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001265 }
1266
1267 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001268 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001269
1270 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1271 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1272 if (mac_addr)
1273 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1274
Johannes Berg79c97e92009-07-07 03:56:12 +02001275 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, mac_addr,
Johannes Berg41ade002007-12-19 02:03:29 +01001276 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001277
1278 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001279 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001280
1281 if (cookie.error)
1282 goto nla_put_failure;
1283
1284 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00001285 err = genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001286 goto out;
1287
1288 nla_put_failure:
1289 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001290 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001291 nlmsg_free(msg);
1292 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001293 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001294 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001295 unlock_rtnl:
1296 rtnl_unlock();
1297
Johannes Berg41ade002007-12-19 02:03:29 +01001298 return err;
1299}
1300
1301static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1302{
Johannes Berg79c97e92009-07-07 03:56:12 +02001303 struct cfg80211_registered_device *rdev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001304 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001305 int err;
1306 struct net_device *dev;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001307 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1308 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001309
Johannes Bergb9454e82009-07-08 13:29:08 +02001310 err = nl80211_parse_key(info, &key);
1311 if (err)
1312 return err;
1313
1314 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001315 return -EINVAL;
1316
Johannes Bergb9454e82009-07-08 13:29:08 +02001317 /* only support setting default key */
1318 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001319 return -EINVAL;
1320
Johannes Berg3b858752009-03-12 09:55:09 +01001321 rtnl_lock();
1322
Johannes Berg463d0182009-07-14 00:33:35 +02001323 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001324 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001325 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001326
Johannes Bergb9454e82009-07-08 13:29:08 +02001327 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001328 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001329 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001330 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6a2009-01-08 13:32:02 +02001331
1332 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +01001333 err = -EOPNOTSUPP;
1334 goto out;
1335 }
1336
Johannes Bergfffd0932009-07-08 14:22:54 +02001337 wdev_lock(dev->ieee80211_ptr);
1338 err = nl80211_key_allowed(dev->ieee80211_ptr);
1339 if (!err)
1340 err = func(&rdev->wiphy, dev, key.idx);
1341
Johannes Berg3d23e342009-09-29 23:27:28 +02001342#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001343 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001344 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001345 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001346 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001347 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001348 }
1349#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001350 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001351
1352 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001353 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001354 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001355
1356 unlock_rtnl:
1357 rtnl_unlock();
1358
Johannes Berg41ade002007-12-19 02:03:29 +01001359 return err;
1360}
1361
1362static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1363{
Johannes Berg79c97e92009-07-07 03:56:12 +02001364 struct cfg80211_registered_device *rdev;
Johannes Bergfffd0932009-07-08 14:22:54 +02001365 int err;
Johannes Berg41ade002007-12-19 02:03:29 +01001366 struct net_device *dev;
Johannes Bergb9454e82009-07-08 13:29:08 +02001367 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001368 u8 *mac_addr = NULL;
1369
Johannes Bergb9454e82009-07-08 13:29:08 +02001370 err = nl80211_parse_key(info, &key);
1371 if (err)
1372 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001373
Johannes Bergb9454e82009-07-08 13:29:08 +02001374 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001375 return -EINVAL;
1376
Johannes Berg41ade002007-12-19 02:03:29 +01001377 if (info->attrs[NL80211_ATTR_MAC])
1378 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1379
Johannes Berg3b858752009-03-12 09:55:09 +01001380 rtnl_lock();
1381
Johannes Berg463d0182009-07-14 00:33:35 +02001382 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001383 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001384 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001385
Johannes Berg79c97e92009-07-07 03:56:12 +02001386 if (!rdev->ops->add_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001387 err = -EOPNOTSUPP;
1388 goto out;
1389 }
1390
Johannes Bergfffd0932009-07-08 14:22:54 +02001391 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx, mac_addr)) {
1392 err = -EINVAL;
1393 goto out;
1394 }
1395
1396 wdev_lock(dev->ieee80211_ptr);
1397 err = nl80211_key_allowed(dev->ieee80211_ptr);
1398 if (!err)
1399 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1400 mac_addr, &key.p);
1401 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001402
1403 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001404 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001405 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001406 unlock_rtnl:
1407 rtnl_unlock();
1408
Johannes Berg41ade002007-12-19 02:03:29 +01001409 return err;
1410}
1411
1412static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1413{
Johannes Berg79c97e92009-07-07 03:56:12 +02001414 struct cfg80211_registered_device *rdev;
Johannes Berg41ade002007-12-19 02:03:29 +01001415 int err;
1416 struct net_device *dev;
Johannes Berg41ade002007-12-19 02:03:29 +01001417 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001418 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001419
Johannes Bergb9454e82009-07-08 13:29:08 +02001420 err = nl80211_parse_key(info, &key);
1421 if (err)
1422 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001423
1424 if (info->attrs[NL80211_ATTR_MAC])
1425 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1426
Johannes Berg3b858752009-03-12 09:55:09 +01001427 rtnl_lock();
1428
Johannes Berg463d0182009-07-14 00:33:35 +02001429 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001430 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001431 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001432
Johannes Berg79c97e92009-07-07 03:56:12 +02001433 if (!rdev->ops->del_key) {
Johannes Berg41ade002007-12-19 02:03:29 +01001434 err = -EOPNOTSUPP;
1435 goto out;
1436 }
1437
Johannes Bergfffd0932009-07-08 14:22:54 +02001438 wdev_lock(dev->ieee80211_ptr);
1439 err = nl80211_key_allowed(dev->ieee80211_ptr);
1440 if (!err)
1441 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001442
Johannes Berg3d23e342009-09-29 23:27:28 +02001443#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001444 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001445 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001446 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001447 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001448 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1449 }
1450#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001451 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001452
Johannes Berg41ade002007-12-19 02:03:29 +01001453 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001454 cfg80211_unlock_rdev(rdev);
Johannes Berg41ade002007-12-19 02:03:29 +01001455 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001456
1457 unlock_rtnl:
1458 rtnl_unlock();
1459
Johannes Berg41ade002007-12-19 02:03:29 +01001460 return err;
1461}
1462
Johannes Berged1b6cc2007-12-19 02:03:32 +01001463static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1464{
1465 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1466 struct beacon_parameters *info);
Johannes Berg79c97e92009-07-07 03:56:12 +02001467 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001468 int err;
1469 struct net_device *dev;
1470 struct beacon_parameters params;
1471 int haveinfo = 0;
1472
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001473 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1474 return -EINVAL;
1475
Johannes Berg3b858752009-03-12 09:55:09 +01001476 rtnl_lock();
1477
Johannes Berg463d0182009-07-14 00:33:35 +02001478 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001479 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001480 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001481
Jouni Malineneec60b02009-03-20 21:21:19 +02001482 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1483 err = -EOPNOTSUPP;
1484 goto out;
1485 }
1486
Johannes Berged1b6cc2007-12-19 02:03:32 +01001487 switch (info->genlhdr->cmd) {
1488 case NL80211_CMD_NEW_BEACON:
1489 /* these are required for NEW_BEACON */
1490 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1491 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1492 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1493 err = -EINVAL;
1494 goto out;
1495 }
1496
Johannes Berg79c97e92009-07-07 03:56:12 +02001497 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001498 break;
1499 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001500 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001501 break;
1502 default:
1503 WARN_ON(1);
1504 err = -EOPNOTSUPP;
1505 goto out;
1506 }
1507
1508 if (!call) {
1509 err = -EOPNOTSUPP;
1510 goto out;
1511 }
1512
1513 memset(&params, 0, sizeof(params));
1514
1515 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1516 params.interval =
1517 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1518 haveinfo = 1;
1519 }
1520
1521 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1522 params.dtim_period =
1523 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1524 haveinfo = 1;
1525 }
1526
1527 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1528 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1529 params.head_len =
1530 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1531 haveinfo = 1;
1532 }
1533
1534 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1535 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1536 params.tail_len =
1537 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1538 haveinfo = 1;
1539 }
1540
1541 if (!haveinfo) {
1542 err = -EINVAL;
1543 goto out;
1544 }
1545
Johannes Berg79c97e92009-07-07 03:56:12 +02001546 err = call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001547
1548 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001549 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001550 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001551 unlock_rtnl:
1552 rtnl_unlock();
1553
Johannes Berged1b6cc2007-12-19 02:03:32 +01001554 return err;
1555}
1556
1557static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1558{
Johannes Berg79c97e92009-07-07 03:56:12 +02001559 struct cfg80211_registered_device *rdev;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001560 int err;
1561 struct net_device *dev;
1562
Johannes Berg3b858752009-03-12 09:55:09 +01001563 rtnl_lock();
1564
Johannes Berg463d0182009-07-14 00:33:35 +02001565 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001566 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001567 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001568
Johannes Berg79c97e92009-07-07 03:56:12 +02001569 if (!rdev->ops->del_beacon) {
Johannes Berged1b6cc2007-12-19 02:03:32 +01001570 err = -EOPNOTSUPP;
1571 goto out;
1572 }
1573
Jouni Malineneec60b02009-03-20 21:21:19 +02001574 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1575 err = -EOPNOTSUPP;
1576 goto out;
1577 }
Johannes Berg79c97e92009-07-07 03:56:12 +02001578 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001579
1580 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001581 cfg80211_unlock_rdev(rdev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001582 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001583 unlock_rtnl:
1584 rtnl_unlock();
1585
Johannes Berged1b6cc2007-12-19 02:03:32 +01001586 return err;
1587}
1588
Johannes Berg5727ef12007-12-19 02:03:34 +01001589static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1590 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1591 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1592 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001593 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001594};
1595
Johannes Bergeccb8e82009-05-11 21:57:56 +03001596static int parse_station_flags(struct genl_info *info,
1597 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001598{
1599 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001600 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001601 int flag;
1602
Johannes Bergeccb8e82009-05-11 21:57:56 +03001603 /*
1604 * Try parsing the new attribute first so userspace
1605 * can specify both for older kernels.
1606 */
1607 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1608 if (nla) {
1609 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001610
Johannes Bergeccb8e82009-05-11 21:57:56 +03001611 sta_flags = nla_data(nla);
1612 params->sta_flags_mask = sta_flags->mask;
1613 params->sta_flags_set = sta_flags->set;
1614 if ((params->sta_flags_mask |
1615 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1616 return -EINVAL;
1617 return 0;
1618 }
1619
1620 /* if present, parse the old attribute */
1621
1622 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001623 if (!nla)
1624 return 0;
1625
1626 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1627 nla, sta_flags_policy))
1628 return -EINVAL;
1629
Johannes Bergeccb8e82009-05-11 21:57:56 +03001630 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1631 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001632
1633 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1634 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001635 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001636
1637 return 0;
1638}
1639
Henning Rogge420e7fa2008-12-11 22:04:19 +01001640static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1641{
1642 int modulation, streams, bitrate;
1643
1644 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1645 return rate->legacy;
1646
1647 /* the formula below does only work for MCS values smaller than 32 */
1648 if (rate->mcs >= 32)
1649 return 0;
1650
1651 modulation = rate->mcs & 7;
1652 streams = (rate->mcs >> 3) + 1;
1653
1654 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1655 13500000 : 6500000;
1656
1657 if (modulation < 4)
1658 bitrate *= (modulation + 1);
1659 else if (modulation == 4)
1660 bitrate *= (modulation + 2);
1661 else
1662 bitrate *= (modulation + 3);
1663
1664 bitrate *= streams;
1665
1666 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1667 bitrate = (bitrate / 9) * 10;
1668
1669 /* do NOT round down here */
1670 return (bitrate + 50000) / 100000;
1671}
1672
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001673static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1674 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001675 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001676{
1677 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001678 struct nlattr *sinfoattr, *txrate;
1679 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001680
1681 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1682 if (!hdr)
1683 return -1;
1684
1685 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1686 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1687
Johannes Bergf5ea9122009-08-07 16:17:38 +02001688 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1689
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001690 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1691 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001692 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001693 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1694 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1695 sinfo->inactive_time);
1696 if (sinfo->filled & STATION_INFO_RX_BYTES)
1697 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1698 sinfo->rx_bytes);
1699 if (sinfo->filled & STATION_INFO_TX_BYTES)
1700 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1701 sinfo->tx_bytes);
1702 if (sinfo->filled & STATION_INFO_LLID)
1703 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1704 sinfo->llid);
1705 if (sinfo->filled & STATION_INFO_PLID)
1706 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1707 sinfo->plid);
1708 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1709 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1710 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001711 if (sinfo->filled & STATION_INFO_SIGNAL)
1712 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1713 sinfo->signal);
1714 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1715 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1716 if (!txrate)
1717 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001718
Henning Rogge420e7fa2008-12-11 22:04:19 +01001719 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1720 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1721 if (bitrate > 0)
1722 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1723
1724 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1725 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1726 sinfo->txrate.mcs);
1727 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1728 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1729 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1730 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1731
1732 nla_nest_end(msg, txrate);
1733 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001734 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1735 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1736 sinfo->rx_packets);
1737 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1738 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1739 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001740 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001741
1742 return genlmsg_end(msg, hdr);
1743
1744 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001745 genlmsg_cancel(msg, hdr);
1746 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001747}
1748
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001749static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001750 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001751{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001752 struct station_info sinfo;
1753 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001754 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001755 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001756 int ifidx = cb->args[0];
1757 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001758 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001759
Holger Schuriga0438972009-11-11 11:30:02 +01001760 if (!ifidx)
1761 ifidx = nl80211_get_ifidx(cb);
1762 if (ifidx < 0)
1763 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001764
Johannes Berg3b858752009-03-12 09:55:09 +01001765 rtnl_lock();
1766
Johannes Berg463d0182009-07-14 00:33:35 +02001767 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01001768 if (!netdev) {
1769 err = -ENODEV;
1770 goto out_rtnl;
1771 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001772
Johannes Berg463d0182009-07-14 00:33:35 +02001773 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001774 if (IS_ERR(dev)) {
1775 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001776 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001777 }
1778
1779 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001780 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001781 goto out_err;
1782 }
1783
Johannes Bergbba95fe2008-07-29 13:22:51 +02001784 while (1) {
1785 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1786 mac_addr, &sinfo);
1787 if (err == -ENOENT)
1788 break;
1789 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001790 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001791
1792 if (nl80211_send_station(skb,
1793 NETLINK_CB(cb->skb).pid,
1794 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1795 netdev, mac_addr,
1796 &sinfo) < 0)
1797 goto out;
1798
1799 sta_idx++;
1800 }
1801
1802
1803 out:
1804 cb->args[1] = sta_idx;
1805 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001806 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001807 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001808 out_rtnl:
1809 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001810
1811 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001812}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001813
Johannes Berg5727ef12007-12-19 02:03:34 +01001814static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1815{
Johannes Berg79c97e92009-07-07 03:56:12 +02001816 struct cfg80211_registered_device *rdev;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001817 int err;
1818 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001819 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001820 struct sk_buff *msg;
1821 u8 *mac_addr = NULL;
1822
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001823 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001824
1825 if (!info->attrs[NL80211_ATTR_MAC])
1826 return -EINVAL;
1827
1828 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1829
Johannes Berg3b858752009-03-12 09:55:09 +01001830 rtnl_lock();
1831
Johannes Berg463d0182009-07-14 00:33:35 +02001832 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001833 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001834 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001835
Johannes Berg79c97e92009-07-07 03:56:12 +02001836 if (!rdev->ops->get_station) {
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001837 err = -EOPNOTSUPP;
1838 goto out;
1839 }
1840
Johannes Berg79c97e92009-07-07 03:56:12 +02001841 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001842 if (err)
1843 goto out;
1844
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001845 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001846 if (!msg)
1847 goto out;
1848
1849 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001850 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001851 goto out_free;
1852
Johannes Berg134e6372009-07-10 09:51:34 +00001853 err = genlmsg_reply(msg, info);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001854 goto out;
1855
1856 out_free:
1857 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001858 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02001859 cfg80211_unlock_rdev(rdev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001860 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001861 out_rtnl:
1862 rtnl_unlock();
1863
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001864 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001865}
1866
1867/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001868 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01001869 */
Johannes Berg463d0182009-07-14 00:33:35 +02001870static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01001871 struct cfg80211_registered_device *rdev,
1872 struct net_device **vlan)
1873{
Johannes Berg463d0182009-07-14 00:33:35 +02001874 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01001875 *vlan = NULL;
1876
1877 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02001878 *vlan = dev_get_by_index(genl_info_net(info),
1879 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01001880 if (!*vlan)
1881 return -ENODEV;
1882 if (!(*vlan)->ieee80211_ptr)
1883 return -EINVAL;
1884 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1885 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01001886 if (!netif_running(*vlan))
1887 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01001888 }
1889 return 0;
1890}
1891
1892static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1893{
Johannes Berg79c97e92009-07-07 03:56:12 +02001894 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01001895 int err;
1896 struct net_device *dev;
1897 struct station_parameters params;
1898 u8 *mac_addr = NULL;
1899
1900 memset(&params, 0, sizeof(params));
1901
1902 params.listen_interval = -1;
1903
1904 if (info->attrs[NL80211_ATTR_STA_AID])
1905 return -EINVAL;
1906
1907 if (!info->attrs[NL80211_ATTR_MAC])
1908 return -EINVAL;
1909
1910 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1911
1912 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1913 params.supported_rates =
1914 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1915 params.supported_rates_len =
1916 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1917 }
1918
1919 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1920 params.listen_interval =
1921 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1922
Jouni Malinen36aedc92008-08-25 11:58:58 +03001923 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1924 params.ht_capa =
1925 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1926
Johannes Bergeccb8e82009-05-11 21:57:56 +03001927 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01001928 return -EINVAL;
1929
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001930 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1931 params.plink_action =
1932 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1933
Johannes Berg3b858752009-03-12 09:55:09 +01001934 rtnl_lock();
1935
Johannes Berg463d0182009-07-14 00:33:35 +02001936 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001937 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001938 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001939
Johannes Berg463d0182009-07-14 00:33:35 +02001940 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02001941 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02001942 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02001943
1944 /* validate settings */
1945 err = 0;
1946
1947 switch (dev->ieee80211_ptr->iftype) {
1948 case NL80211_IFTYPE_AP:
1949 case NL80211_IFTYPE_AP_VLAN:
1950 /* disallow mesh-specific things */
1951 if (params.plink_action)
1952 err = -EINVAL;
1953 break;
1954 case NL80211_IFTYPE_STATION:
1955 /* disallow everything but AUTHORIZED flag */
1956 if (params.plink_action)
1957 err = -EINVAL;
1958 if (params.vlan)
1959 err = -EINVAL;
1960 if (params.supported_rates)
1961 err = -EINVAL;
1962 if (params.ht_capa)
1963 err = -EINVAL;
1964 if (params.listen_interval >= 0)
1965 err = -EINVAL;
1966 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
1967 err = -EINVAL;
1968 break;
1969 case NL80211_IFTYPE_MESH_POINT:
1970 /* disallow things mesh doesn't support */
1971 if (params.vlan)
1972 err = -EINVAL;
1973 if (params.ht_capa)
1974 err = -EINVAL;
1975 if (params.listen_interval >= 0)
1976 err = -EINVAL;
1977 if (params.supported_rates)
1978 err = -EINVAL;
1979 if (params.sta_flags_mask)
1980 err = -EINVAL;
1981 break;
1982 default:
1983 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02001984 }
1985
Johannes Berg5727ef12007-12-19 02:03:34 +01001986 if (err)
1987 goto out;
1988
Johannes Berg79c97e92009-07-07 03:56:12 +02001989 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01001990 err = -EOPNOTSUPP;
1991 goto out;
1992 }
1993
Johannes Berg79c97e92009-07-07 03:56:12 +02001994 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001995
1996 out:
1997 if (params.vlan)
1998 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02001999 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002000 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002001 out_rtnl:
2002 rtnl_unlock();
2003
Johannes Berg5727ef12007-12-19 02:03:34 +01002004 return err;
2005}
2006
2007static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2008{
Johannes Berg79c97e92009-07-07 03:56:12 +02002009 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002010 int err;
2011 struct net_device *dev;
2012 struct station_parameters params;
2013 u8 *mac_addr = NULL;
2014
2015 memset(&params, 0, sizeof(params));
2016
2017 if (!info->attrs[NL80211_ATTR_MAC])
2018 return -EINVAL;
2019
Johannes Berg5727ef12007-12-19 02:03:34 +01002020 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2021 return -EINVAL;
2022
2023 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2024 return -EINVAL;
2025
2026 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2027 params.supported_rates =
2028 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2029 params.supported_rates_len =
2030 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2031 params.listen_interval =
2032 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002033
Johannes Berga97f4422009-06-18 17:23:43 +02002034 if (info->attrs[NL80211_ATTR_STA_AID]) {
2035 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2036 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2037 return -EINVAL;
2038 }
Johannes Berg51b50fb2009-05-24 16:42:30 +02002039
Jouni Malinen36aedc92008-08-25 11:58:58 +03002040 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2041 params.ht_capa =
2042 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002043
Johannes Bergeccb8e82009-05-11 21:57:56 +03002044 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002045 return -EINVAL;
2046
Johannes Berg3b858752009-03-12 09:55:09 +01002047 rtnl_lock();
2048
Johannes Berg463d0182009-07-14 00:33:35 +02002049 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002050 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002051 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002052
Johannes Berg463d0182009-07-14 00:33:35 +02002053 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002054 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002055 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002056
2057 /* validate settings */
2058 err = 0;
2059
2060 switch (dev->ieee80211_ptr->iftype) {
2061 case NL80211_IFTYPE_AP:
2062 case NL80211_IFTYPE_AP_VLAN:
2063 /* all ok but must have AID */
2064 if (!params.aid)
2065 err = -EINVAL;
2066 break;
2067 case NL80211_IFTYPE_MESH_POINT:
2068 /* disallow things mesh doesn't support */
2069 if (params.vlan)
2070 err = -EINVAL;
2071 if (params.aid)
2072 err = -EINVAL;
2073 if (params.ht_capa)
2074 err = -EINVAL;
2075 if (params.listen_interval >= 0)
2076 err = -EINVAL;
2077 if (params.supported_rates)
2078 err = -EINVAL;
2079 if (params.sta_flags_mask)
2080 err = -EINVAL;
2081 break;
2082 default:
2083 err = -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002084 }
2085
Johannes Berg5727ef12007-12-19 02:03:34 +01002086 if (err)
2087 goto out;
2088
Johannes Berg79c97e92009-07-07 03:56:12 +02002089 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002090 err = -EOPNOTSUPP;
2091 goto out;
2092 }
2093
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002094 if (!netif_running(dev)) {
2095 err = -ENETDOWN;
2096 goto out;
2097 }
2098
Johannes Berg79c97e92009-07-07 03:56:12 +02002099 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002100
2101 out:
2102 if (params.vlan)
2103 dev_put(params.vlan);
Johannes Berg79c97e92009-07-07 03:56:12 +02002104 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002105 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002106 out_rtnl:
2107 rtnl_unlock();
2108
Johannes Berg5727ef12007-12-19 02:03:34 +01002109 return err;
2110}
2111
2112static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2113{
Johannes Berg79c97e92009-07-07 03:56:12 +02002114 struct cfg80211_registered_device *rdev;
Johannes Berg5727ef12007-12-19 02:03:34 +01002115 int err;
2116 struct net_device *dev;
2117 u8 *mac_addr = NULL;
2118
2119 if (info->attrs[NL80211_ATTR_MAC])
2120 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2121
Johannes Berg3b858752009-03-12 09:55:09 +01002122 rtnl_lock();
2123
Johannes Berg463d0182009-07-14 00:33:35 +02002124 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002125 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002126 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01002127
Johannes Berge80cf852009-05-11 14:43:13 +02002128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Andrey Yurovsky155cc9e2009-06-16 11:31:04 -07002129 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2130 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
Johannes Berge80cf852009-05-11 14:43:13 +02002131 err = -EINVAL;
2132 goto out;
2133 }
2134
Johannes Berg79c97e92009-07-07 03:56:12 +02002135 if (!rdev->ops->del_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002136 err = -EOPNOTSUPP;
2137 goto out;
2138 }
2139
Johannes Berg79c97e92009-07-07 03:56:12 +02002140 err = rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002141
2142 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002143 cfg80211_unlock_rdev(rdev);
Johannes Berg5727ef12007-12-19 02:03:34 +01002144 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002145 out_rtnl:
2146 rtnl_unlock();
2147
Johannes Berg5727ef12007-12-19 02:03:34 +01002148 return err;
2149}
2150
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002151static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2152 int flags, struct net_device *dev,
2153 u8 *dst, u8 *next_hop,
2154 struct mpath_info *pinfo)
2155{
2156 void *hdr;
2157 struct nlattr *pinfoattr;
2158
2159 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2160 if (!hdr)
2161 return -1;
2162
2163 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2164 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2165 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2166
Johannes Bergf5ea9122009-08-07 16:17:38 +02002167 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2168
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002169 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2170 if (!pinfoattr)
2171 goto nla_put_failure;
2172 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2173 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2174 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002175 if (pinfo->filled & MPATH_INFO_SN)
2176 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2177 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002178 if (pinfo->filled & MPATH_INFO_METRIC)
2179 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2180 pinfo->metric);
2181 if (pinfo->filled & MPATH_INFO_EXPTIME)
2182 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2183 pinfo->exptime);
2184 if (pinfo->filled & MPATH_INFO_FLAGS)
2185 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2186 pinfo->flags);
2187 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2188 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2189 pinfo->discovery_timeout);
2190 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2191 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2192 pinfo->discovery_retries);
2193
2194 nla_nest_end(msg, pinfoattr);
2195
2196 return genlmsg_end(msg, hdr);
2197
2198 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002199 genlmsg_cancel(msg, hdr);
2200 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002201}
2202
2203static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002204 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002205{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002206 struct mpath_info pinfo;
2207 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002208 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002209 u8 dst[ETH_ALEN];
2210 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002211 int ifidx = cb->args[0];
2212 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002213 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002214
Holger Schuriga0438972009-11-11 11:30:02 +01002215 if (!ifidx)
2216 ifidx = nl80211_get_ifidx(cb);
2217 if (ifidx < 0)
2218 return ifidx;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002219
Johannes Berg3b858752009-03-12 09:55:09 +01002220 rtnl_lock();
2221
Johannes Berg463d0182009-07-14 00:33:35 +02002222 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg3b858752009-03-12 09:55:09 +01002223 if (!netdev) {
2224 err = -ENODEV;
2225 goto out_rtnl;
2226 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002227
Johannes Berg463d0182009-07-14 00:33:35 +02002228 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002229 if (IS_ERR(dev)) {
2230 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002231 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002232 }
2233
2234 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002235 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002236 goto out_err;
2237 }
2238
Jouni Malineneec60b02009-03-20 21:21:19 +02002239 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2240 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002241 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002242 }
2243
Johannes Bergbba95fe2008-07-29 13:22:51 +02002244 while (1) {
2245 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2246 dst, next_hop, &pinfo);
2247 if (err == -ENOENT)
2248 break;
2249 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002250 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002251
2252 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2253 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2254 netdev, dst, next_hop,
2255 &pinfo) < 0)
2256 goto out;
2257
2258 path_idx++;
2259 }
2260
2261
2262 out:
2263 cb->args[1] = path_idx;
2264 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002265 out_err:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02002266 cfg80211_unlock_rdev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002267 out_rtnl:
2268 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02002269
2270 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002271}
2272
2273static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2274{
Johannes Berg79c97e92009-07-07 03:56:12 +02002275 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002276 int err;
2277 struct net_device *dev;
2278 struct mpath_info pinfo;
2279 struct sk_buff *msg;
2280 u8 *dst = NULL;
2281 u8 next_hop[ETH_ALEN];
2282
2283 memset(&pinfo, 0, sizeof(pinfo));
2284
2285 if (!info->attrs[NL80211_ATTR_MAC])
2286 return -EINVAL;
2287
2288 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2289
Johannes Berg3b858752009-03-12 09:55:09 +01002290 rtnl_lock();
2291
Johannes Berg463d0182009-07-14 00:33:35 +02002292 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002293 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002294 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295
Johannes Berg79c97e92009-07-07 03:56:12 +02002296 if (!rdev->ops->get_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002297 err = -EOPNOTSUPP;
2298 goto out;
2299 }
2300
Jouni Malineneec60b02009-03-20 21:21:19 +02002301 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2302 err = -EOPNOTSUPP;
2303 goto out;
2304 }
2305
Johannes Berg79c97e92009-07-07 03:56:12 +02002306 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002307 if (err)
2308 goto out;
2309
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002311 if (!msg)
2312 goto out;
2313
2314 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2315 dev, dst, next_hop, &pinfo) < 0)
2316 goto out_free;
2317
Johannes Berg134e6372009-07-10 09:51:34 +00002318 err = genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319 goto out;
2320
2321 out_free:
2322 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002323 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002324 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002325 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002326 out_rtnl:
2327 rtnl_unlock();
2328
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002329 return err;
2330}
2331
2332static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2333{
Johannes Berg79c97e92009-07-07 03:56:12 +02002334 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335 int err;
2336 struct net_device *dev;
2337 u8 *dst = NULL;
2338 u8 *next_hop = NULL;
2339
2340 if (!info->attrs[NL80211_ATTR_MAC])
2341 return -EINVAL;
2342
2343 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2344 return -EINVAL;
2345
2346 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2347 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2348
Johannes Berg3b858752009-03-12 09:55:09 +01002349 rtnl_lock();
2350
Johannes Berg463d0182009-07-14 00:33:35 +02002351 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002352 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002353 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002354
Johannes Berg79c97e92009-07-07 03:56:12 +02002355 if (!rdev->ops->change_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002356 err = -EOPNOTSUPP;
2357 goto out;
2358 }
2359
Jouni Malineneec60b02009-03-20 21:21:19 +02002360 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2361 err = -EOPNOTSUPP;
2362 goto out;
2363 }
2364
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002365 if (!netif_running(dev)) {
2366 err = -ENETDOWN;
2367 goto out;
2368 }
2369
Johannes Berg79c97e92009-07-07 03:56:12 +02002370 err = rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002371
2372 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002373 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002374 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002375 out_rtnl:
2376 rtnl_unlock();
2377
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378 return err;
2379}
2380static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2381{
Johannes Berg79c97e92009-07-07 03:56:12 +02002382 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002383 int err;
2384 struct net_device *dev;
2385 u8 *dst = NULL;
2386 u8 *next_hop = NULL;
2387
2388 if (!info->attrs[NL80211_ATTR_MAC])
2389 return -EINVAL;
2390
2391 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2392 return -EINVAL;
2393
2394 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2395 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2396
Johannes Berg3b858752009-03-12 09:55:09 +01002397 rtnl_lock();
2398
Johannes Berg463d0182009-07-14 00:33:35 +02002399 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002400 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002401 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002402
Johannes Berg79c97e92009-07-07 03:56:12 +02002403 if (!rdev->ops->add_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 err = -EOPNOTSUPP;
2405 goto out;
2406 }
2407
Jouni Malineneec60b02009-03-20 21:21:19 +02002408 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2409 err = -EOPNOTSUPP;
2410 goto out;
2411 }
2412
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002413 if (!netif_running(dev)) {
2414 err = -ENETDOWN;
2415 goto out;
2416 }
2417
Johannes Berg79c97e92009-07-07 03:56:12 +02002418 err = rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002419
2420 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002421 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002422 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002423 out_rtnl:
2424 rtnl_unlock();
2425
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002426 return err;
2427}
2428
2429static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2430{
Johannes Berg79c97e92009-07-07 03:56:12 +02002431 struct cfg80211_registered_device *rdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002432 int err;
2433 struct net_device *dev;
2434 u8 *dst = NULL;
2435
2436 if (info->attrs[NL80211_ATTR_MAC])
2437 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2438
Johannes Berg3b858752009-03-12 09:55:09 +01002439 rtnl_lock();
2440
Johannes Berg463d0182009-07-14 00:33:35 +02002441 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002442 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002443 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002444
Johannes Berg79c97e92009-07-07 03:56:12 +02002445 if (!rdev->ops->del_mpath) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002446 err = -EOPNOTSUPP;
2447 goto out;
2448 }
2449
Johannes Berg79c97e92009-07-07 03:56:12 +02002450 err = rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002451
2452 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002453 cfg80211_unlock_rdev(rdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002454 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002455 out_rtnl:
2456 rtnl_unlock();
2457
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002458 return err;
2459}
2460
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002461static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2462{
Johannes Berg79c97e92009-07-07 03:56:12 +02002463 struct cfg80211_registered_device *rdev;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002464 int err;
2465 struct net_device *dev;
2466 struct bss_parameters params;
2467
2468 memset(&params, 0, sizeof(params));
2469 /* default to not changing parameters */
2470 params.use_cts_prot = -1;
2471 params.use_short_preamble = -1;
2472 params.use_short_slot_time = -1;
2473
2474 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2475 params.use_cts_prot =
2476 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2477 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2478 params.use_short_preamble =
2479 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2480 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2481 params.use_short_slot_time =
2482 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002483 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2484 params.basic_rates =
2485 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2486 params.basic_rates_len =
2487 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2488 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002489
Johannes Berg3b858752009-03-12 09:55:09 +01002490 rtnl_lock();
2491
Johannes Berg463d0182009-07-14 00:33:35 +02002492 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002493 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002494 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002495
Johannes Berg79c97e92009-07-07 03:56:12 +02002496 if (!rdev->ops->change_bss) {
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002497 err = -EOPNOTSUPP;
2498 goto out;
2499 }
2500
Jouni Malineneec60b02009-03-20 21:21:19 +02002501 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2502 err = -EOPNOTSUPP;
2503 goto out;
2504 }
2505
Johannes Berg79c97e92009-07-07 03:56:12 +02002506 err = rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002507
2508 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02002509 cfg80211_unlock_rdev(rdev);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002510 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002511 out_rtnl:
2512 rtnl_unlock();
2513
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002514 return err;
2515}
2516
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002517static const struct nla_policy
2518 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2519 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2520 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2521 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2522 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2523 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2524 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2525};
2526
2527static int parse_reg_rule(struct nlattr *tb[],
2528 struct ieee80211_reg_rule *reg_rule)
2529{
2530 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2531 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2532
2533 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2534 return -EINVAL;
2535 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2536 return -EINVAL;
2537 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2538 return -EINVAL;
2539 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2540 return -EINVAL;
2541 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2542 return -EINVAL;
2543
2544 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2545
2546 freq_range->start_freq_khz =
2547 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2548 freq_range->end_freq_khz =
2549 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2550 freq_range->max_bandwidth_khz =
2551 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2552
2553 power_rule->max_eirp =
2554 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2555
2556 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2557 power_rule->max_antenna_gain =
2558 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2559
2560 return 0;
2561}
2562
2563static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2564{
2565 int r;
2566 char *data = NULL;
2567
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002568 /*
2569 * You should only get this when cfg80211 hasn't yet initialized
2570 * completely when built-in to the kernel right between the time
2571 * window between nl80211_init() and regulatory_init(), if that is
2572 * even possible.
2573 */
2574 mutex_lock(&cfg80211_mutex);
2575 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002576 mutex_unlock(&cfg80211_mutex);
2577 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002578 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002579 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002580
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002581 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2582 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002583
2584 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2585
2586#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2587 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002588 if (is_world_regdom(data))
2589 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002590#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002591
2592 r = regulatory_hint_user(data);
2593
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002594 return r;
2595}
2596
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002597static int nl80211_get_mesh_params(struct sk_buff *skb,
2598 struct genl_info *info)
2599{
Johannes Berg79c97e92009-07-07 03:56:12 +02002600 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002601 struct mesh_config cur_params;
2602 int err;
2603 struct net_device *dev;
2604 void *hdr;
2605 struct nlattr *pinfoattr;
2606 struct sk_buff *msg;
2607
Johannes Berg3b858752009-03-12 09:55:09 +01002608 rtnl_lock();
2609
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002610 /* Look up our device */
Johannes Berg463d0182009-07-14 00:33:35 +02002611 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002612 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002613 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002614
Johannes Berg79c97e92009-07-07 03:56:12 +02002615 if (!rdev->ops->get_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002616 err = -EOPNOTSUPP;
2617 goto out;
2618 }
2619
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002620 /* Get the mesh params */
Johannes Berg79c97e92009-07-07 03:56:12 +02002621 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002622 if (err)
2623 goto out;
2624
2625 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002627 if (!msg) {
2628 err = -ENOBUFS;
2629 goto out;
2630 }
2631 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2632 NL80211_CMD_GET_MESH_PARAMS);
2633 if (!hdr)
2634 goto nla_put_failure;
2635 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2636 if (!pinfoattr)
2637 goto nla_put_failure;
2638 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2639 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2640 cur_params.dot11MeshRetryTimeout);
2641 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2642 cur_params.dot11MeshConfirmTimeout);
2643 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2644 cur_params.dot11MeshHoldingTimeout);
2645 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2646 cur_params.dot11MeshMaxPeerLinks);
2647 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2648 cur_params.dot11MeshMaxRetries);
2649 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2650 cur_params.dot11MeshTTL);
2651 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2652 cur_params.auto_open_plinks);
2653 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2654 cur_params.dot11MeshHWMPmaxPREQretries);
2655 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2656 cur_params.path_refresh_time);
2657 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2658 cur_params.min_discovery_timeout);
2659 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2660 cur_params.dot11MeshHWMPactivePathTimeout);
2661 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2662 cur_params.dot11MeshHWMPpreqMinInterval);
2663 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2664 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002665 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2666 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002667 nla_nest_end(msg, pinfoattr);
2668 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002669 err = genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002670 goto out;
2671
Johannes Berg3b858752009-03-12 09:55:09 +01002672 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002673 genlmsg_cancel(msg, hdr);
2674 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002675 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002676 /* Cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002677 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002678 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002679 out_rtnl:
2680 rtnl_unlock();
2681
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002682 return err;
2683}
2684
2685#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2686do {\
2687 if (table[attr_num]) {\
2688 cfg.param = nla_fn(table[attr_num]); \
2689 mask |= (1 << (attr_num - 1)); \
2690 } \
2691} while (0);\
2692
2693static struct nla_policy
2694nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2695 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2696 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2697 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2698 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2699 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2700 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2701 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2702
2703 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2704 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2705 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2706 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2707 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2708 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2709};
2710
2711static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2712{
2713 int err;
2714 u32 mask;
Johannes Berg79c97e92009-07-07 03:56:12 +02002715 struct cfg80211_registered_device *rdev;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002716 struct net_device *dev;
2717 struct mesh_config cfg;
2718 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2719 struct nlattr *parent_attr;
2720
2721 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2722 if (!parent_attr)
2723 return -EINVAL;
2724 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2725 parent_attr, nl80211_meshconf_params_policy))
2726 return -EINVAL;
2727
Johannes Berg3b858752009-03-12 09:55:09 +01002728 rtnl_lock();
2729
Johannes Berg463d0182009-07-14 00:33:35 +02002730 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002731 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002732 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002733
Johannes Berg79c97e92009-07-07 03:56:12 +02002734 if (!rdev->ops->set_mesh_params) {
Jouni Malinenf3f92582009-03-20 17:57:36 +02002735 err = -EOPNOTSUPP;
2736 goto out;
2737 }
2738
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002739 /* This makes sure that there aren't more than 32 mesh config
2740 * parameters (otherwise our bitfield scheme would not work.) */
2741 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2742
2743 /* Fill in the params struct */
2744 mask = 0;
2745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2746 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2747 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2748 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2749 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2750 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2751 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2752 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2754 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2755 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2756 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2757 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2758 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2759 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2760 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2761 nla_get_u8);
2762 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2763 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2764 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2765 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2766 nla_get_u16);
2767 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2768 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2769 nla_get_u32);
2770 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2771 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2772 nla_get_u16);
2773 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2774 dot11MeshHWMPnetDiameterTraversalTime,
2775 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2776 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002777 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2778 dot11MeshHWMPRootMode, mask,
2779 NL80211_MESHCONF_HWMP_ROOTMODE,
2780 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002781
2782 /* Apply changes */
Johannes Berg79c97e92009-07-07 03:56:12 +02002783 err = rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002784
Jouni Malinenf3f92582009-03-20 17:57:36 +02002785 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002786 /* cleanup */
Johannes Berg79c97e92009-07-07 03:56:12 +02002787 cfg80211_unlock_rdev(rdev);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002788 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002789 out_rtnl:
2790 rtnl_unlock();
2791
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002792 return err;
2793}
2794
2795#undef FILL_IN_MESH_PARAM_IF_SET
2796
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002797static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2798{
2799 struct sk_buff *msg;
2800 void *hdr = NULL;
2801 struct nlattr *nl_reg_rules;
2802 unsigned int i;
2803 int err = -EINVAL;
2804
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002805 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002806
2807 if (!cfg80211_regdomain)
2808 goto out;
2809
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002810 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002811 if (!msg) {
2812 err = -ENOBUFS;
2813 goto out;
2814 }
2815
2816 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2817 NL80211_CMD_GET_REG);
2818 if (!hdr)
2819 goto nla_put_failure;
2820
2821 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2822 cfg80211_regdomain->alpha2);
2823
2824 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2825 if (!nl_reg_rules)
2826 goto nla_put_failure;
2827
2828 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2829 struct nlattr *nl_reg_rule;
2830 const struct ieee80211_reg_rule *reg_rule;
2831 const struct ieee80211_freq_range *freq_range;
2832 const struct ieee80211_power_rule *power_rule;
2833
2834 reg_rule = &cfg80211_regdomain->reg_rules[i];
2835 freq_range = &reg_rule->freq_range;
2836 power_rule = &reg_rule->power_rule;
2837
2838 nl_reg_rule = nla_nest_start(msg, i);
2839 if (!nl_reg_rule)
2840 goto nla_put_failure;
2841
2842 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2843 reg_rule->flags);
2844 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2845 freq_range->start_freq_khz);
2846 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2847 freq_range->end_freq_khz);
2848 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2849 freq_range->max_bandwidth_khz);
2850 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2851 power_rule->max_antenna_gain);
2852 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2853 power_rule->max_eirp);
2854
2855 nla_nest_end(msg, nl_reg_rule);
2856 }
2857
2858 nla_nest_end(msg, nl_reg_rules);
2859
2860 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002861 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002862 goto out;
2863
2864nla_put_failure:
2865 genlmsg_cancel(msg, hdr);
2866 err = -EMSGSIZE;
2867out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002868 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002869 return err;
2870}
2871
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002872static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2873{
2874 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2875 struct nlattr *nl_reg_rule;
2876 char *alpha2 = NULL;
2877 int rem_reg_rules = 0, r = 0;
2878 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2879 struct ieee80211_regdomain *rd = NULL;
2880
2881 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2882 return -EINVAL;
2883
2884 if (!info->attrs[NL80211_ATTR_REG_RULES])
2885 return -EINVAL;
2886
2887 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2888
2889 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2890 rem_reg_rules) {
2891 num_rules++;
2892 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002893 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002894 }
2895
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002896 mutex_lock(&cfg80211_mutex);
2897
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002898 if (!reg_is_valid_request(alpha2)) {
2899 r = -EINVAL;
2900 goto bad_reg;
2901 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002902
2903 size_of_regd = sizeof(struct ieee80211_regdomain) +
2904 (num_rules * sizeof(struct ieee80211_reg_rule));
2905
2906 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002907 if (!rd) {
2908 r = -ENOMEM;
2909 goto bad_reg;
2910 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002911
2912 rd->n_reg_rules = num_rules;
2913 rd->alpha2[0] = alpha2[0];
2914 rd->alpha2[1] = alpha2[1];
2915
2916 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2917 rem_reg_rules) {
2918 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2919 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2920 reg_rule_policy);
2921 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2922 if (r)
2923 goto bad_reg;
2924
2925 rule_idx++;
2926
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002927 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2928 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002929 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002930 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002931 }
2932
2933 BUG_ON(rule_idx != num_rules);
2934
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002935 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002936
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002937 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002938
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002939 return r;
2940
Johannes Bergd2372b32008-10-24 20:32:20 +02002941 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002942 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002943 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002944 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002945}
2946
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002947static int validate_scan_freqs(struct nlattr *freqs)
2948{
2949 struct nlattr *attr1, *attr2;
2950 int n_channels = 0, tmp1, tmp2;
2951
2952 nla_for_each_nested(attr1, freqs, tmp1) {
2953 n_channels++;
2954 /*
2955 * Some hardware has a limited channel list for
2956 * scanning, and it is pretty much nonsensical
2957 * to scan for a channel twice, so disallow that
2958 * and don't require drivers to check that the
2959 * channel list they get isn't longer than what
2960 * they can scan, as long as they can scan all
2961 * the channels they registered at once.
2962 */
2963 nla_for_each_nested(attr2, freqs, tmp2)
2964 if (attr1 != attr2 &&
2965 nla_get_u32(attr1) == nla_get_u32(attr2))
2966 return 0;
2967 }
2968
2969 return n_channels;
2970}
2971
Johannes Berg2a519312009-02-10 21:25:55 +01002972static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2973{
Johannes Berg79c97e92009-07-07 03:56:12 +02002974 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01002975 struct net_device *dev;
2976 struct cfg80211_scan_request *request;
2977 struct cfg80211_ssid *ssid;
2978 struct ieee80211_channel *channel;
2979 struct nlattr *attr;
2980 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002981 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002982 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002983 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002984
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002985 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2986 return -EINVAL;
2987
Johannes Berg3b858752009-03-12 09:55:09 +01002988 rtnl_lock();
2989
Johannes Berg463d0182009-07-14 00:33:35 +02002990 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg2a519312009-02-10 21:25:55 +01002991 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002992 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002993
Johannes Berg79c97e92009-07-07 03:56:12 +02002994 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002995
Johannes Berg79c97e92009-07-07 03:56:12 +02002996 if (!rdev->ops->scan) {
Johannes Berg2a519312009-02-10 21:25:55 +01002997 err = -EOPNOTSUPP;
2998 goto out;
2999 }
3000
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003001 if (!netif_running(dev)) {
3002 err = -ENETDOWN;
3003 goto out;
3004 }
3005
Johannes Berg79c97e92009-07-07 03:56:12 +02003006 if (rdev->scan_req) {
Johannes Berg2a519312009-02-10 21:25:55 +01003007 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01003008 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003009 }
3010
3011 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003012 n_channels = validate_scan_freqs(
3013 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg2a519312009-02-10 21:25:55 +01003014 if (!n_channels) {
3015 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003016 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003017 }
3018 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003019 n_channels = 0;
3020
Johannes Berg2a519312009-02-10 21:25:55 +01003021 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3022 if (wiphy->bands[band])
3023 n_channels += wiphy->bands[band]->n_channels;
3024 }
3025
3026 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3027 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3028 n_ssids++;
3029
3030 if (n_ssids > wiphy->max_scan_ssids) {
3031 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01003032 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003033 }
3034
Jouni Malinen70692ad2009-02-16 19:39:13 +02003035 if (info->attrs[NL80211_ATTR_IE])
3036 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3037 else
3038 ie_len = 0;
3039
Johannes Berg18a83652009-03-31 12:12:05 +02003040 if (ie_len > wiphy->max_scan_ie_len) {
3041 err = -EINVAL;
3042 goto out;
3043 }
3044
Johannes Berg2a519312009-02-10 21:25:55 +01003045 request = kzalloc(sizeof(*request)
3046 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003047 + sizeof(channel) * n_channels
3048 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01003049 if (!request) {
3050 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01003051 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01003052 }
3053
Johannes Berg2a519312009-02-10 21:25:55 +01003054 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003055 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003056 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003057 if (ie_len) {
3058 if (request->ssids)
3059 request->ie = (void *)(request->ssids + n_ssids);
3060 else
3061 request->ie = (void *)(request->channels + n_channels);
3062 }
Johannes Berg2a519312009-02-10 21:25:55 +01003063
Johannes Berg584991d2009-11-02 13:32:03 +01003064 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003065 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3066 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003067 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003068 struct ieee80211_channel *chan;
3069
3070 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3071
3072 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003073 err = -EINVAL;
3074 goto out_free;
3075 }
Johannes Berg584991d2009-11-02 13:32:03 +01003076
3077 /* ignore disabled channels */
3078 if (chan->flags & IEEE80211_CHAN_DISABLED)
3079 continue;
3080
3081 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003082 i++;
3083 }
3084 } else {
3085 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003086 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3087 int j;
3088 if (!wiphy->bands[band])
3089 continue;
3090 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003091 struct ieee80211_channel *chan;
3092
3093 chan = &wiphy->bands[band]->channels[j];
3094
3095 if (chan->flags & IEEE80211_CHAN_DISABLED)
3096 continue;
3097
3098 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003099 i++;
3100 }
3101 }
3102 }
3103
Johannes Berg584991d2009-11-02 13:32:03 +01003104 if (!i) {
3105 err = -EINVAL;
3106 goto out_free;
3107 }
3108
3109 request->n_channels = i;
3110
Johannes Berg2a519312009-02-10 21:25:55 +01003111 i = 0;
3112 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3113 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3114 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3115 err = -EINVAL;
3116 goto out_free;
3117 }
3118 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3119 request->ssids[i].ssid_len = nla_len(attr);
3120 i++;
3121 }
3122 }
3123
Jouni Malinen70692ad2009-02-16 19:39:13 +02003124 if (info->attrs[NL80211_ATTR_IE]) {
3125 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003126 memcpy((void *)request->ie,
3127 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003128 request->ie_len);
3129 }
3130
Johannes Berg463d0182009-07-14 00:33:35 +02003131 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003132 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003133
Johannes Berg79c97e92009-07-07 03:56:12 +02003134 rdev->scan_req = request;
3135 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003136
Johannes Berg463d0182009-07-14 00:33:35 +02003137 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003138 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003139 dev_hold(dev);
3140 }
Johannes Berga538e2d2009-06-16 19:56:42 +02003141
Johannes Berg2a519312009-02-10 21:25:55 +01003142 out_free:
3143 if (err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003144 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003145 kfree(request);
3146 }
Johannes Berg2a519312009-02-10 21:25:55 +01003147 out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003148 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003149 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01003150 out_rtnl:
3151 rtnl_unlock();
3152
Johannes Berg2a519312009-02-10 21:25:55 +01003153 return err;
3154}
3155
3156static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3157 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003158 struct wireless_dev *wdev,
3159 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003160{
Johannes Berg48ab9052009-07-10 18:42:31 +02003161 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003162 void *hdr;
3163 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003164 int i;
3165
3166 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003167
3168 hdr = nl80211hdr_put(msg, pid, seq, flags,
3169 NL80211_CMD_NEW_SCAN_RESULTS);
3170 if (!hdr)
3171 return -1;
3172
Johannes Bergf5ea9122009-08-07 16:17:38 +02003173 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003174 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003175
3176 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3177 if (!bss)
3178 goto nla_put_failure;
3179 if (!is_zero_ether_addr(res->bssid))
3180 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3181 if (res->information_elements && res->len_information_elements)
3182 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3183 res->len_information_elements,
3184 res->information_elements);
3185 if (res->tsf)
3186 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3187 if (res->beacon_interval)
3188 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3189 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3190 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003191 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3192 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003193
Johannes Berg77965c92009-02-18 18:45:06 +01003194 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003195 case CFG80211_SIGNAL_TYPE_MBM:
3196 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3197 break;
3198 case CFG80211_SIGNAL_TYPE_UNSPEC:
3199 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3200 break;
3201 default:
3202 break;
3203 }
3204
Johannes Berg48ab9052009-07-10 18:42:31 +02003205 switch (wdev->iftype) {
3206 case NL80211_IFTYPE_STATION:
3207 if (intbss == wdev->current_bss)
3208 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3209 NL80211_BSS_STATUS_ASSOCIATED);
3210 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3211 if (intbss != wdev->auth_bsses[i])
3212 continue;
3213 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3214 NL80211_BSS_STATUS_AUTHENTICATED);
3215 break;
3216 }
3217 break;
3218 case NL80211_IFTYPE_ADHOC:
3219 if (intbss == wdev->current_bss)
3220 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3221 NL80211_BSS_STATUS_IBSS_JOINED);
3222 break;
3223 default:
3224 break;
3225 }
3226
Johannes Berg2a519312009-02-10 21:25:55 +01003227 nla_nest_end(msg, bss);
3228
3229 return genlmsg_end(msg, hdr);
3230
3231 nla_put_failure:
3232 genlmsg_cancel(msg, hdr);
3233 return -EMSGSIZE;
3234}
3235
3236static int nl80211_dump_scan(struct sk_buff *skb,
3237 struct netlink_callback *cb)
3238{
Johannes Berg48ab9052009-07-10 18:42:31 +02003239 struct cfg80211_registered_device *rdev;
3240 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003241 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003242 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003243 int ifidx = cb->args[0];
3244 int start = cb->args[1], idx = 0;
3245 int err;
3246
Holger Schuriga0438972009-11-11 11:30:02 +01003247 if (!ifidx)
3248 ifidx = nl80211_get_ifidx(cb);
3249 if (ifidx < 0)
3250 return ifidx;
3251 cb->args[0] = ifidx;
Johannes Berg2a519312009-02-10 21:25:55 +01003252
Johannes Berg463d0182009-07-14 00:33:35 +02003253 dev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003254 if (!dev)
Johannes Berg2a519312009-02-10 21:25:55 +01003255 return -ENODEV;
3256
Johannes Berg463d0182009-07-14 00:33:35 +02003257 rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Johannes Berg48ab9052009-07-10 18:42:31 +02003258 if (IS_ERR(rdev)) {
3259 err = PTR_ERR(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003260 goto out_put_netdev;
3261 }
3262
Johannes Berg48ab9052009-07-10 18:42:31 +02003263 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003264
Johannes Berg48ab9052009-07-10 18:42:31 +02003265 wdev_lock(wdev);
3266 spin_lock_bh(&rdev->bss_lock);
3267 cfg80211_bss_expire(rdev);
3268
3269 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003270 if (++idx <= start)
3271 continue;
3272 if (nl80211_send_bss(skb,
3273 NETLINK_CB(cb->skb).pid,
3274 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003275 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003276 idx--;
3277 goto out;
3278 }
3279 }
3280
3281 out:
Johannes Berg48ab9052009-07-10 18:42:31 +02003282 spin_unlock_bh(&rdev->bss_lock);
3283 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003284
3285 cb->args[1] = idx;
3286 err = skb->len;
Johannes Berg48ab9052009-07-10 18:42:31 +02003287 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003288 out_put_netdev:
Johannes Berg48ab9052009-07-10 18:42:31 +02003289 dev_put(dev);
Johannes Berg2a519312009-02-10 21:25:55 +01003290
3291 return err;
3292}
3293
Holger Schurig61fa7132009-11-11 12:25:40 +01003294static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3295 int flags, struct net_device *dev,
3296 struct survey_info *survey)
3297{
3298 void *hdr;
3299 struct nlattr *infoattr;
3300
3301 /* Survey without a channel doesn't make sense */
3302 if (!survey->channel)
3303 return -EINVAL;
3304
3305 hdr = nl80211hdr_put(msg, pid, seq, flags,
3306 NL80211_CMD_NEW_SURVEY_RESULTS);
3307 if (!hdr)
3308 return -ENOMEM;
3309
3310 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3311
3312 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3313 if (!infoattr)
3314 goto nla_put_failure;
3315
3316 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3317 survey->channel->center_freq);
3318 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3319 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3320 survey->noise);
3321
3322 nla_nest_end(msg, infoattr);
3323
3324 return genlmsg_end(msg, hdr);
3325
3326 nla_put_failure:
3327 genlmsg_cancel(msg, hdr);
3328 return -EMSGSIZE;
3329}
3330
3331static int nl80211_dump_survey(struct sk_buff *skb,
3332 struct netlink_callback *cb)
3333{
3334 struct survey_info survey;
3335 struct cfg80211_registered_device *dev;
3336 struct net_device *netdev;
3337 int ifidx = cb->args[0];
3338 int survey_idx = cb->args[1];
3339 int res;
3340
3341 if (!ifidx)
3342 ifidx = nl80211_get_ifidx(cb);
3343 if (ifidx < 0)
3344 return ifidx;
3345 cb->args[0] = ifidx;
3346
3347 rtnl_lock();
3348
3349 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3350 if (!netdev) {
3351 res = -ENODEV;
3352 goto out_rtnl;
3353 }
3354
3355 dev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3356 if (IS_ERR(dev)) {
3357 res = PTR_ERR(dev);
3358 goto out_rtnl;
3359 }
3360
3361 if (!dev->ops->dump_survey) {
3362 res = -EOPNOTSUPP;
3363 goto out_err;
3364 }
3365
3366 while (1) {
3367 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3368 &survey);
3369 if (res == -ENOENT)
3370 break;
3371 if (res)
3372 goto out_err;
3373
3374 if (nl80211_send_survey(skb,
3375 NETLINK_CB(cb->skb).pid,
3376 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3377 netdev,
3378 &survey) < 0)
3379 goto out;
3380 survey_idx++;
3381 }
3382
3383 out:
3384 cb->args[1] = survey_idx;
3385 res = skb->len;
3386 out_err:
3387 cfg80211_unlock_rdev(dev);
3388 out_rtnl:
3389 rtnl_unlock();
3390
3391 return res;
3392}
3393
Jouni Malinen255e7372009-03-20 21:21:17 +02003394static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3395{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003396 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003397}
3398
Samuel Ortizb23aa672009-07-01 21:26:54 +02003399static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3400{
3401 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3402 NL80211_WPA_VERSION_2));
3403}
3404
3405static bool nl80211_valid_akm_suite(u32 akm)
3406{
3407 return akm == WLAN_AKM_SUITE_8021X ||
3408 akm == WLAN_AKM_SUITE_PSK;
3409}
3410
3411static bool nl80211_valid_cipher_suite(u32 cipher)
3412{
3413 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3414 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3415 cipher == WLAN_CIPHER_SUITE_TKIP ||
3416 cipher == WLAN_CIPHER_SUITE_CCMP ||
3417 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3418}
3419
3420
Jouni Malinen636a5d32009-03-19 13:39:22 +02003421static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3422{
Johannes Berg79c97e92009-07-07 03:56:12 +02003423 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003424 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003425 struct ieee80211_channel *chan;
3426 const u8 *bssid, *ssid, *ie = NULL;
3427 int err, ssid_len, ie_len = 0;
3428 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003429 struct key_parse key;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003430
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003431 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3432 return -EINVAL;
3433
3434 if (!info->attrs[NL80211_ATTR_MAC])
3435 return -EINVAL;
3436
Jouni Malinen17780922009-03-27 20:52:47 +02003437 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3438 return -EINVAL;
3439
Johannes Berg19957bb2009-07-02 17:20:43 +02003440 if (!info->attrs[NL80211_ATTR_SSID])
3441 return -EINVAL;
3442
3443 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3444 return -EINVAL;
3445
Johannes Bergfffd0932009-07-08 14:22:54 +02003446 err = nl80211_parse_key(info, &key);
3447 if (err)
3448 return err;
3449
3450 if (key.idx >= 0) {
3451 if (!key.p.key || !key.p.key_len)
3452 return -EINVAL;
3453 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3454 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3455 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3456 key.p.key_len != WLAN_KEY_LEN_WEP104))
3457 return -EINVAL;
3458 if (key.idx > 4)
3459 return -EINVAL;
3460 } else {
3461 key.p.key_len = 0;
3462 key.p.key = NULL;
3463 }
3464
Jouni Malinen636a5d32009-03-19 13:39:22 +02003465 rtnl_lock();
3466
Johannes Berg463d0182009-07-14 00:33:35 +02003467 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003468 if (err)
3469 goto unlock_rtnl;
3470
Johannes Berg79c97e92009-07-07 03:56:12 +02003471 if (!rdev->ops->auth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003472 err = -EOPNOTSUPP;
3473 goto out;
3474 }
3475
Jouni Malineneec60b02009-03-20 21:21:19 +02003476 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3477 err = -EOPNOTSUPP;
3478 goto out;
3479 }
3480
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003481 if (!netif_running(dev)) {
3482 err = -ENETDOWN;
3483 goto out;
3484 }
3485
Johannes Berg19957bb2009-07-02 17:20:43 +02003486 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003487 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003488 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3489 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
Jouni Malinen17780922009-03-27 20:52:47 +02003490 err = -EINVAL;
3491 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003492 }
3493
Johannes Berg19957bb2009-07-02 17:20:43 +02003494 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3495 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3496
3497 if (info->attrs[NL80211_ATTR_IE]) {
3498 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3499 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3500 }
3501
3502 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3503 if (!nl80211_valid_auth_type(auth_type)) {
3504 err = -EINVAL;
3505 goto out;
3506 }
3507
Johannes Berg79c97e92009-07-07 03:56:12 +02003508 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
Johannes Bergfffd0932009-07-08 14:22:54 +02003509 ssid, ssid_len, ie, ie_len,
3510 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003511
3512out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003513 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003514 dev_put(dev);
3515unlock_rtnl:
3516 rtnl_unlock();
3517 return err;
3518}
3519
Samuel Ortizb23aa672009-07-01 21:26:54 +02003520static int nl80211_crypto_settings(struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003521 struct cfg80211_crypto_settings *settings,
3522 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003523{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003524 memset(settings, 0, sizeof(*settings));
3525
Samuel Ortizb23aa672009-07-01 21:26:54 +02003526 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3527
3528 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3529 void *data;
3530 int len, i;
3531
3532 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3533 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3534 settings->n_ciphers_pairwise = len / sizeof(u32);
3535
3536 if (len % sizeof(u32))
3537 return -EINVAL;
3538
Johannes Berg3dc27d22009-07-02 21:36:37 +02003539 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003540 return -EINVAL;
3541
3542 memcpy(settings->ciphers_pairwise, data, len);
3543
3544 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3545 if (!nl80211_valid_cipher_suite(
3546 settings->ciphers_pairwise[i]))
3547 return -EINVAL;
3548 }
3549
3550 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3551 settings->cipher_group =
3552 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3553 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3554 return -EINVAL;
3555 }
3556
3557 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3558 settings->wpa_versions =
3559 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3560 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3561 return -EINVAL;
3562 }
3563
3564 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3565 void *data;
3566 int len, i;
3567
3568 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3569 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3570 settings->n_akm_suites = len / sizeof(u32);
3571
3572 if (len % sizeof(u32))
3573 return -EINVAL;
3574
3575 memcpy(settings->akm_suites, data, len);
3576
3577 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3578 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3579 return -EINVAL;
3580 }
3581
3582 return 0;
3583}
3584
Jouni Malinen636a5d32009-03-19 13:39:22 +02003585static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3586{
Johannes Berg19957bb2009-07-02 17:20:43 +02003587 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003588 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003589 struct cfg80211_crypto_settings crypto;
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003590 struct ieee80211_channel *chan, *fixedchan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003591 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003592 int err, ssid_len, ie_len = 0;
3593 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003594
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003595 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3596 return -EINVAL;
3597
3598 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003599 !info->attrs[NL80211_ATTR_SSID] ||
3600 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003601 return -EINVAL;
3602
Jouni Malinen636a5d32009-03-19 13:39:22 +02003603 rtnl_lock();
3604
Johannes Berg463d0182009-07-14 00:33:35 +02003605 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003606 if (err)
3607 goto unlock_rtnl;
3608
Johannes Berg19957bb2009-07-02 17:20:43 +02003609 if (!rdev->ops->assoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003610 err = -EOPNOTSUPP;
3611 goto out;
3612 }
3613
Jouni Malineneec60b02009-03-20 21:21:19 +02003614 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3615 err = -EOPNOTSUPP;
3616 goto out;
3617 }
3618
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003619 if (!netif_running(dev)) {
3620 err = -ENETDOWN;
3621 goto out;
3622 }
3623
Johannes Berg19957bb2009-07-02 17:20:43 +02003624 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003625
Johannes Berg19957bb2009-07-02 17:20:43 +02003626 chan = ieee80211_get_channel(&rdev->wiphy,
3627 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3628 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) {
3629 err = -EINVAL;
3630 goto out;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003631 }
3632
Johannes Berg59bbb6f2009-08-07 17:22:35 +02003633 mutex_lock(&rdev->devlist_mtx);
3634 fixedchan = rdev_fixed_channel(rdev, NULL);
3635 if (fixedchan && chan != fixedchan) {
3636 err = -EBUSY;
3637 mutex_unlock(&rdev->devlist_mtx);
3638 goto out;
3639 }
3640 mutex_unlock(&rdev->devlist_mtx);
3641
Johannes Berg19957bb2009-07-02 17:20:43 +02003642 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3643 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003644
3645 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003646 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3647 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003648 }
3649
Jouni Malinendc6382c2009-05-06 22:09:37 +03003650 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003651 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003652 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003653 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003654 use_mfp = true;
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003655 else if (mfp != NL80211_MFP_NO) {
Jouni Malinendc6382c2009-05-06 22:09:37 +03003656 err = -EINVAL;
3657 goto out;
3658 }
3659 }
3660
Johannes Berg3e5d7642009-07-07 14:37:26 +02003661 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3662 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3663
Johannes Berg3dc27d22009-07-02 21:36:37 +02003664 err = nl80211_crypto_settings(info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003665 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003666 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3667 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003668 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003669
3670out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003671 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003672 dev_put(dev);
3673unlock_rtnl:
3674 rtnl_unlock();
3675 return err;
3676}
3677
3678static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3679{
Johannes Berg79c97e92009-07-07 03:56:12 +02003680 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003681 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003682 const u8 *ie = NULL, *bssid;
3683 int err, ie_len = 0;
3684 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003685
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003686 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3687 return -EINVAL;
3688
3689 if (!info->attrs[NL80211_ATTR_MAC])
3690 return -EINVAL;
3691
3692 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3693 return -EINVAL;
3694
Jouni Malinen636a5d32009-03-19 13:39:22 +02003695 rtnl_lock();
3696
Johannes Berg463d0182009-07-14 00:33:35 +02003697 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003698 if (err)
3699 goto unlock_rtnl;
3700
Johannes Berg79c97e92009-07-07 03:56:12 +02003701 if (!rdev->ops->deauth) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003702 err = -EOPNOTSUPP;
3703 goto out;
3704 }
3705
Jouni Malineneec60b02009-03-20 21:21:19 +02003706 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3707 err = -EOPNOTSUPP;
3708 goto out;
3709 }
3710
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003711 if (!netif_running(dev)) {
3712 err = -ENETDOWN;
3713 goto out;
3714 }
3715
Johannes Berg19957bb2009-07-02 17:20:43 +02003716 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003717
Johannes Berg19957bb2009-07-02 17:20:43 +02003718 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3719 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003720 /* Reason Code 0 is reserved */
3721 err = -EINVAL;
3722 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003723 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003724
3725 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003726 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3727 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003728 }
3729
Johannes Berg79c97e92009-07-07 03:56:12 +02003730 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003731
3732out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003733 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003734 dev_put(dev);
3735unlock_rtnl:
3736 rtnl_unlock();
3737 return err;
3738}
3739
3740static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3741{
Johannes Berg79c97e92009-07-07 03:56:12 +02003742 struct cfg80211_registered_device *rdev;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003743 struct net_device *dev;
Johannes Berg19957bb2009-07-02 17:20:43 +02003744 const u8 *ie = NULL, *bssid;
3745 int err, ie_len = 0;
3746 u16 reason_code;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003747
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003748 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3749 return -EINVAL;
3750
3751 if (!info->attrs[NL80211_ATTR_MAC])
3752 return -EINVAL;
3753
3754 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3755 return -EINVAL;
3756
Jouni Malinen636a5d32009-03-19 13:39:22 +02003757 rtnl_lock();
3758
Johannes Berg463d0182009-07-14 00:33:35 +02003759 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003760 if (err)
3761 goto unlock_rtnl;
3762
Johannes Berg79c97e92009-07-07 03:56:12 +02003763 if (!rdev->ops->disassoc) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02003764 err = -EOPNOTSUPP;
3765 goto out;
3766 }
3767
Jouni Malineneec60b02009-03-20 21:21:19 +02003768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3769 err = -EOPNOTSUPP;
3770 goto out;
3771 }
3772
Jouni Malinen35a8efe2009-03-20 21:21:18 +02003773 if (!netif_running(dev)) {
3774 err = -ENETDOWN;
3775 goto out;
3776 }
3777
Johannes Berg19957bb2009-07-02 17:20:43 +02003778 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003779
Johannes Berg19957bb2009-07-02 17:20:43 +02003780 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3781 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003782 /* Reason Code 0 is reserved */
3783 err = -EINVAL;
3784 goto out;
Jouni Malinen255e7372009-03-20 21:21:17 +02003785 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003786
3787 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003788 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3789 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003790 }
3791
Johannes Berg79c97e92009-07-07 03:56:12 +02003792 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003793
3794out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003795 cfg80211_unlock_rdev(rdev);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003796 dev_put(dev);
3797unlock_rtnl:
3798 rtnl_unlock();
3799 return err;
3800}
3801
Johannes Berg04a773a2009-04-19 21:24:32 +02003802static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3803{
Johannes Berg79c97e92009-07-07 03:56:12 +02003804 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003805 struct net_device *dev;
3806 struct cfg80211_ibss_params ibss;
3807 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003808 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003809 int err;
3810
Johannes Berg8e30bc52009-04-22 17:45:38 +02003811 memset(&ibss, 0, sizeof(ibss));
3812
Johannes Berg04a773a2009-04-19 21:24:32 +02003813 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3814 return -EINVAL;
3815
3816 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3817 !info->attrs[NL80211_ATTR_SSID] ||
3818 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3819 return -EINVAL;
3820
Johannes Berg8e30bc52009-04-22 17:45:38 +02003821 ibss.beacon_interval = 100;
3822
3823 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3824 ibss.beacon_interval =
3825 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3826 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3827 return -EINVAL;
3828 }
3829
Johannes Berg04a773a2009-04-19 21:24:32 +02003830 rtnl_lock();
3831
Johannes Berg463d0182009-07-14 00:33:35 +02003832 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003833 if (err)
3834 goto unlock_rtnl;
3835
Johannes Berg79c97e92009-07-07 03:56:12 +02003836 if (!rdev->ops->join_ibss) {
Johannes Berg04a773a2009-04-19 21:24:32 +02003837 err = -EOPNOTSUPP;
3838 goto out;
3839 }
3840
3841 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3842 err = -EOPNOTSUPP;
3843 goto out;
3844 }
3845
3846 if (!netif_running(dev)) {
3847 err = -ENETDOWN;
3848 goto out;
3849 }
3850
Johannes Berg79c97e92009-07-07 03:56:12 +02003851 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003852
3853 if (info->attrs[NL80211_ATTR_MAC])
3854 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3855 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3856 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3857
3858 if (info->attrs[NL80211_ATTR_IE]) {
3859 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3860 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3861 }
3862
3863 ibss.channel = ieee80211_get_channel(wiphy,
3864 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3865 if (!ibss.channel ||
3866 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3867 ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3868 err = -EINVAL;
3869 goto out;
3870 }
3871
3872 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003873 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003874
Johannes Bergfffd0932009-07-08 14:22:54 +02003875 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3876 connkeys = nl80211_parse_connkeys(rdev,
3877 info->attrs[NL80211_ATTR_KEYS]);
3878 if (IS_ERR(connkeys)) {
3879 err = PTR_ERR(connkeys);
3880 connkeys = NULL;
3881 goto out;
3882 }
3883 }
3884
3885 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003886
3887out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003888 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003889 dev_put(dev);
3890unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02003891 if (err)
3892 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003893 rtnl_unlock();
3894 return err;
3895}
3896
3897static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3898{
Johannes Berg79c97e92009-07-07 03:56:12 +02003899 struct cfg80211_registered_device *rdev;
Johannes Berg04a773a2009-04-19 21:24:32 +02003900 struct net_device *dev;
3901 int err;
3902
3903 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->leave_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 err = cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003925
3926out:
Johannes Berg79c97e92009-07-07 03:56:12 +02003927 cfg80211_unlock_rdev(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +02003928 dev_put(dev);
3929unlock_rtnl:
3930 rtnl_unlock();
3931 return err;
3932}
3933
Johannes Bergaff89a92009-07-01 21:26:51 +02003934#ifdef CONFIG_NL80211_TESTMODE
3935static struct genl_multicast_group nl80211_testmode_mcgrp = {
3936 .name = "testmode",
3937};
3938
3939static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3940{
3941 struct cfg80211_registered_device *rdev;
3942 int err;
3943
3944 if (!info->attrs[NL80211_ATTR_TESTDATA])
3945 return -EINVAL;
3946
3947 rtnl_lock();
3948
3949 rdev = cfg80211_get_dev_from_info(info);
3950 if (IS_ERR(rdev)) {
3951 err = PTR_ERR(rdev);
3952 goto unlock_rtnl;
3953 }
3954
3955 err = -EOPNOTSUPP;
3956 if (rdev->ops->testmode_cmd) {
3957 rdev->testmode_info = info;
3958 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3959 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3960 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3961 rdev->testmode_info = NULL;
3962 }
3963
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02003964 cfg80211_unlock_rdev(rdev);
Johannes Bergaff89a92009-07-01 21:26:51 +02003965
3966 unlock_rtnl:
3967 rtnl_unlock();
3968 return err;
3969}
3970
3971static struct sk_buff *
3972__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3973 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3974{
3975 struct sk_buff *skb;
3976 void *hdr;
3977 struct nlattr *data;
3978
3979 skb = nlmsg_new(approxlen + 100, gfp);
3980 if (!skb)
3981 return NULL;
3982
3983 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3984 if (!hdr) {
3985 kfree_skb(skb);
3986 return NULL;
3987 }
3988
3989 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3990 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3991
3992 ((void **)skb->cb)[0] = rdev;
3993 ((void **)skb->cb)[1] = hdr;
3994 ((void **)skb->cb)[2] = data;
3995
3996 return skb;
3997
3998 nla_put_failure:
3999 kfree_skb(skb);
4000 return NULL;
4001}
4002
4003struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4004 int approxlen)
4005{
4006 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4007
4008 if (WARN_ON(!rdev->testmode_info))
4009 return NULL;
4010
4011 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4012 rdev->testmode_info->snd_pid,
4013 rdev->testmode_info->snd_seq,
4014 GFP_KERNEL);
4015}
4016EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4017
4018int cfg80211_testmode_reply(struct sk_buff *skb)
4019{
4020 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4021 void *hdr = ((void **)skb->cb)[1];
4022 struct nlattr *data = ((void **)skb->cb)[2];
4023
4024 if (WARN_ON(!rdev->testmode_info)) {
4025 kfree_skb(skb);
4026 return -EINVAL;
4027 }
4028
4029 nla_nest_end(skb, data);
4030 genlmsg_end(skb, hdr);
4031 return genlmsg_reply(skb, rdev->testmode_info);
4032}
4033EXPORT_SYMBOL(cfg80211_testmode_reply);
4034
4035struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4036 int approxlen, gfp_t gfp)
4037{
4038 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4039
4040 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4041}
4042EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4043
4044void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4045{
4046 void *hdr = ((void **)skb->cb)[1];
4047 struct nlattr *data = ((void **)skb->cb)[2];
4048
4049 nla_nest_end(skb, data);
4050 genlmsg_end(skb, hdr);
4051 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4052}
4053EXPORT_SYMBOL(cfg80211_testmode_event);
4054#endif
4055
Samuel Ortizb23aa672009-07-01 21:26:54 +02004056static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4057{
Johannes Berg79c97e92009-07-07 03:56:12 +02004058 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004059 struct net_device *dev;
4060 struct cfg80211_connect_params connect;
4061 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004062 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004063 int err;
4064
4065 memset(&connect, 0, sizeof(connect));
4066
4067 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4068 return -EINVAL;
4069
4070 if (!info->attrs[NL80211_ATTR_SSID] ||
4071 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4072 return -EINVAL;
4073
4074 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4075 connect.auth_type =
4076 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4077 if (!nl80211_valid_auth_type(connect.auth_type))
4078 return -EINVAL;
4079 } else
4080 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4081
4082 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4083
Johannes Berg3dc27d22009-07-02 21:36:37 +02004084 err = nl80211_crypto_settings(info, &connect.crypto,
4085 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004086 if (err)
4087 return err;
4088 rtnl_lock();
4089
Johannes Berg463d0182009-07-14 00:33:35 +02004090 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004091 if (err)
4092 goto unlock_rtnl;
4093
4094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4095 err = -EOPNOTSUPP;
4096 goto out;
4097 }
4098
4099 if (!netif_running(dev)) {
4100 err = -ENETDOWN;
4101 goto out;
4102 }
4103
Johannes Berg79c97e92009-07-07 03:56:12 +02004104 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004105
Samuel Ortizb23aa672009-07-01 21:26:54 +02004106 if (info->attrs[NL80211_ATTR_MAC])
4107 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4108 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4109 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4110
4111 if (info->attrs[NL80211_ATTR_IE]) {
4112 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4113 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4114 }
4115
4116 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4117 connect.channel =
4118 ieee80211_get_channel(wiphy,
4119 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4120 if (!connect.channel ||
4121 connect.channel->flags & IEEE80211_CHAN_DISABLED) {
4122 err = -EINVAL;
4123 goto out;
4124 }
4125 }
4126
Johannes Bergfffd0932009-07-08 14:22:54 +02004127 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4128 connkeys = nl80211_parse_connkeys(rdev,
4129 info->attrs[NL80211_ATTR_KEYS]);
4130 if (IS_ERR(connkeys)) {
4131 err = PTR_ERR(connkeys);
4132 connkeys = NULL;
4133 goto out;
4134 }
4135 }
4136
4137 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004138
4139out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004140 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004141 dev_put(dev);
4142unlock_rtnl:
Johannes Bergfffd0932009-07-08 14:22:54 +02004143 if (err)
4144 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004145 rtnl_unlock();
4146 return err;
4147}
4148
4149static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4150{
Johannes Berg79c97e92009-07-07 03:56:12 +02004151 struct cfg80211_registered_device *rdev;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004152 struct net_device *dev;
4153 int err;
4154 u16 reason;
4155
4156 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4157 reason = WLAN_REASON_DEAUTH_LEAVING;
4158 else
4159 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4160
4161 if (reason == 0)
4162 return -EINVAL;
4163
4164 rtnl_lock();
4165
Johannes Berg463d0182009-07-14 00:33:35 +02004166 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004167 if (err)
4168 goto unlock_rtnl;
4169
4170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4171 err = -EOPNOTSUPP;
4172 goto out;
4173 }
4174
4175 if (!netif_running(dev)) {
4176 err = -ENETDOWN;
4177 goto out;
4178 }
4179
Johannes Berg79c97e92009-07-07 03:56:12 +02004180 err = cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004181
4182out:
Johannes Berg79c97e92009-07-07 03:56:12 +02004183 cfg80211_unlock_rdev(rdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004184 dev_put(dev);
4185unlock_rtnl:
4186 rtnl_unlock();
4187 return err;
4188}
4189
Johannes Berg463d0182009-07-14 00:33:35 +02004190static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4191{
4192 struct cfg80211_registered_device *rdev;
4193 struct net *net;
4194 int err;
4195 u32 pid;
4196
4197 if (!info->attrs[NL80211_ATTR_PID])
4198 return -EINVAL;
4199
4200 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4201
4202 rtnl_lock();
4203
4204 rdev = cfg80211_get_dev_from_info(info);
4205 if (IS_ERR(rdev)) {
4206 err = PTR_ERR(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004207 goto out_rtnl;
Johannes Berg463d0182009-07-14 00:33:35 +02004208 }
4209
4210 net = get_net_ns_by_pid(pid);
4211 if (IS_ERR(net)) {
4212 err = PTR_ERR(net);
4213 goto out;
4214 }
4215
4216 err = 0;
4217
4218 /* check if anything to do */
4219 if (net_eq(wiphy_net(&rdev->wiphy), net))
4220 goto out_put_net;
4221
4222 err = cfg80211_switch_netns(rdev, net);
4223 out_put_net:
4224 put_net(net);
4225 out:
4226 cfg80211_unlock_rdev(rdev);
Johannes Berg8a8e05e2009-10-08 21:02:02 +02004227 out_rtnl:
Johannes Berg463d0182009-07-14 00:33:35 +02004228 rtnl_unlock();
4229 return err;
4230}
4231
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004232static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4233{
4234 struct cfg80211_registered_device *rdev;
4235 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4236 struct cfg80211_pmksa *pmksa) = NULL;
4237 int err;
4238 struct net_device *dev;
4239 struct cfg80211_pmksa pmksa;
4240
4241 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4242
4243 if (!info->attrs[NL80211_ATTR_MAC])
4244 return -EINVAL;
4245
4246 if (!info->attrs[NL80211_ATTR_PMKID])
4247 return -EINVAL;
4248
4249 rtnl_lock();
4250
4251 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4252 if (err)
4253 goto out_rtnl;
4254
4255 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4256 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4257
4258 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4259 err = -EOPNOTSUPP;
4260 goto out;
4261 }
4262
4263 switch (info->genlhdr->cmd) {
4264 case NL80211_CMD_SET_PMKSA:
4265 rdev_ops = rdev->ops->set_pmksa;
4266 break;
4267 case NL80211_CMD_DEL_PMKSA:
4268 rdev_ops = rdev->ops->del_pmksa;
4269 break;
4270 default:
4271 WARN_ON(1);
4272 break;
4273 }
4274
4275 if (!rdev_ops) {
4276 err = -EOPNOTSUPP;
4277 goto out;
4278 }
4279
4280 err = rdev_ops(&rdev->wiphy, dev, &pmksa);
4281
4282 out:
4283 cfg80211_unlock_rdev(rdev);
4284 dev_put(dev);
4285 out_rtnl:
4286 rtnl_unlock();
4287
4288 return err;
4289}
4290
4291static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4292{
4293 struct cfg80211_registered_device *rdev;
4294 int err;
4295 struct net_device *dev;
4296
4297 rtnl_lock();
4298
4299 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4300 if (err)
4301 goto out_rtnl;
4302
4303 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
4304 err = -EOPNOTSUPP;
4305 goto out;
4306 }
4307
4308 if (!rdev->ops->flush_pmksa) {
4309 err = -EOPNOTSUPP;
4310 goto out;
4311 }
4312
4313 err = rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4314
4315 out:
4316 cfg80211_unlock_rdev(rdev);
4317 dev_put(dev);
4318 out_rtnl:
4319 rtnl_unlock();
4320
4321 return err;
4322
4323}
4324
Johannes Berg55682962007-09-20 13:09:35 -04004325static struct genl_ops nl80211_ops[] = {
4326 {
4327 .cmd = NL80211_CMD_GET_WIPHY,
4328 .doit = nl80211_get_wiphy,
4329 .dumpit = nl80211_dump_wiphy,
4330 .policy = nl80211_policy,
4331 /* can be retrieved by unprivileged users */
4332 },
4333 {
4334 .cmd = NL80211_CMD_SET_WIPHY,
4335 .doit = nl80211_set_wiphy,
4336 .policy = nl80211_policy,
4337 .flags = GENL_ADMIN_PERM,
4338 },
4339 {
4340 .cmd = NL80211_CMD_GET_INTERFACE,
4341 .doit = nl80211_get_interface,
4342 .dumpit = nl80211_dump_interface,
4343 .policy = nl80211_policy,
4344 /* can be retrieved by unprivileged users */
4345 },
4346 {
4347 .cmd = NL80211_CMD_SET_INTERFACE,
4348 .doit = nl80211_set_interface,
4349 .policy = nl80211_policy,
4350 .flags = GENL_ADMIN_PERM,
4351 },
4352 {
4353 .cmd = NL80211_CMD_NEW_INTERFACE,
4354 .doit = nl80211_new_interface,
4355 .policy = nl80211_policy,
4356 .flags = GENL_ADMIN_PERM,
4357 },
4358 {
4359 .cmd = NL80211_CMD_DEL_INTERFACE,
4360 .doit = nl80211_del_interface,
4361 .policy = nl80211_policy,
4362 .flags = GENL_ADMIN_PERM,
4363 },
Johannes Berg41ade002007-12-19 02:03:29 +01004364 {
4365 .cmd = NL80211_CMD_GET_KEY,
4366 .doit = nl80211_get_key,
4367 .policy = nl80211_policy,
4368 .flags = GENL_ADMIN_PERM,
4369 },
4370 {
4371 .cmd = NL80211_CMD_SET_KEY,
4372 .doit = nl80211_set_key,
4373 .policy = nl80211_policy,
4374 .flags = GENL_ADMIN_PERM,
4375 },
4376 {
4377 .cmd = NL80211_CMD_NEW_KEY,
4378 .doit = nl80211_new_key,
4379 .policy = nl80211_policy,
4380 .flags = GENL_ADMIN_PERM,
4381 },
4382 {
4383 .cmd = NL80211_CMD_DEL_KEY,
4384 .doit = nl80211_del_key,
4385 .policy = nl80211_policy,
4386 .flags = GENL_ADMIN_PERM,
4387 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004388 {
4389 .cmd = NL80211_CMD_SET_BEACON,
4390 .policy = nl80211_policy,
4391 .flags = GENL_ADMIN_PERM,
4392 .doit = nl80211_addset_beacon,
4393 },
4394 {
4395 .cmd = NL80211_CMD_NEW_BEACON,
4396 .policy = nl80211_policy,
4397 .flags = GENL_ADMIN_PERM,
4398 .doit = nl80211_addset_beacon,
4399 },
4400 {
4401 .cmd = NL80211_CMD_DEL_BEACON,
4402 .policy = nl80211_policy,
4403 .flags = GENL_ADMIN_PERM,
4404 .doit = nl80211_del_beacon,
4405 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004406 {
4407 .cmd = NL80211_CMD_GET_STATION,
4408 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004409 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004410 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01004411 },
4412 {
4413 .cmd = NL80211_CMD_SET_STATION,
4414 .doit = nl80211_set_station,
4415 .policy = nl80211_policy,
4416 .flags = GENL_ADMIN_PERM,
4417 },
4418 {
4419 .cmd = NL80211_CMD_NEW_STATION,
4420 .doit = nl80211_new_station,
4421 .policy = nl80211_policy,
4422 .flags = GENL_ADMIN_PERM,
4423 },
4424 {
4425 .cmd = NL80211_CMD_DEL_STATION,
4426 .doit = nl80211_del_station,
4427 .policy = nl80211_policy,
4428 .flags = GENL_ADMIN_PERM,
4429 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004430 {
4431 .cmd = NL80211_CMD_GET_MPATH,
4432 .doit = nl80211_get_mpath,
4433 .dumpit = nl80211_dump_mpath,
4434 .policy = nl80211_policy,
4435 .flags = GENL_ADMIN_PERM,
4436 },
4437 {
4438 .cmd = NL80211_CMD_SET_MPATH,
4439 .doit = nl80211_set_mpath,
4440 .policy = nl80211_policy,
4441 .flags = GENL_ADMIN_PERM,
4442 },
4443 {
4444 .cmd = NL80211_CMD_NEW_MPATH,
4445 .doit = nl80211_new_mpath,
4446 .policy = nl80211_policy,
4447 .flags = GENL_ADMIN_PERM,
4448 },
4449 {
4450 .cmd = NL80211_CMD_DEL_MPATH,
4451 .doit = nl80211_del_mpath,
4452 .policy = nl80211_policy,
4453 .flags = GENL_ADMIN_PERM,
4454 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004455 {
4456 .cmd = NL80211_CMD_SET_BSS,
4457 .doit = nl80211_set_bss,
4458 .policy = nl80211_policy,
4459 .flags = GENL_ADMIN_PERM,
4460 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004461 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004462 .cmd = NL80211_CMD_GET_REG,
4463 .doit = nl80211_get_reg,
4464 .policy = nl80211_policy,
4465 /* can be retrieved by unprivileged users */
4466 },
4467 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004468 .cmd = NL80211_CMD_SET_REG,
4469 .doit = nl80211_set_reg,
4470 .policy = nl80211_policy,
4471 .flags = GENL_ADMIN_PERM,
4472 },
4473 {
4474 .cmd = NL80211_CMD_REQ_SET_REG,
4475 .doit = nl80211_req_set_reg,
4476 .policy = nl80211_policy,
4477 .flags = GENL_ADMIN_PERM,
4478 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004479 {
4480 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4481 .doit = nl80211_get_mesh_params,
4482 .policy = nl80211_policy,
4483 /* can be retrieved by unprivileged users */
4484 },
4485 {
4486 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4487 .doit = nl80211_set_mesh_params,
4488 .policy = nl80211_policy,
4489 .flags = GENL_ADMIN_PERM,
4490 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004491 {
Johannes Berg2a519312009-02-10 21:25:55 +01004492 .cmd = NL80211_CMD_TRIGGER_SCAN,
4493 .doit = nl80211_trigger_scan,
4494 .policy = nl80211_policy,
4495 .flags = GENL_ADMIN_PERM,
4496 },
4497 {
4498 .cmd = NL80211_CMD_GET_SCAN,
4499 .policy = nl80211_policy,
4500 .dumpit = nl80211_dump_scan,
4501 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004502 {
4503 .cmd = NL80211_CMD_AUTHENTICATE,
4504 .doit = nl80211_authenticate,
4505 .policy = nl80211_policy,
4506 .flags = GENL_ADMIN_PERM,
4507 },
4508 {
4509 .cmd = NL80211_CMD_ASSOCIATE,
4510 .doit = nl80211_associate,
4511 .policy = nl80211_policy,
4512 .flags = GENL_ADMIN_PERM,
4513 },
4514 {
4515 .cmd = NL80211_CMD_DEAUTHENTICATE,
4516 .doit = nl80211_deauthenticate,
4517 .policy = nl80211_policy,
4518 .flags = GENL_ADMIN_PERM,
4519 },
4520 {
4521 .cmd = NL80211_CMD_DISASSOCIATE,
4522 .doit = nl80211_disassociate,
4523 .policy = nl80211_policy,
4524 .flags = GENL_ADMIN_PERM,
4525 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004526 {
4527 .cmd = NL80211_CMD_JOIN_IBSS,
4528 .doit = nl80211_join_ibss,
4529 .policy = nl80211_policy,
4530 .flags = GENL_ADMIN_PERM,
4531 },
4532 {
4533 .cmd = NL80211_CMD_LEAVE_IBSS,
4534 .doit = nl80211_leave_ibss,
4535 .policy = nl80211_policy,
4536 .flags = GENL_ADMIN_PERM,
4537 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004538#ifdef CONFIG_NL80211_TESTMODE
4539 {
4540 .cmd = NL80211_CMD_TESTMODE,
4541 .doit = nl80211_testmode_do,
4542 .policy = nl80211_policy,
4543 .flags = GENL_ADMIN_PERM,
4544 },
4545#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004546 {
4547 .cmd = NL80211_CMD_CONNECT,
4548 .doit = nl80211_connect,
4549 .policy = nl80211_policy,
4550 .flags = GENL_ADMIN_PERM,
4551 },
4552 {
4553 .cmd = NL80211_CMD_DISCONNECT,
4554 .doit = nl80211_disconnect,
4555 .policy = nl80211_policy,
4556 .flags = GENL_ADMIN_PERM,
4557 },
Johannes Berg463d0182009-07-14 00:33:35 +02004558 {
4559 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4560 .doit = nl80211_wiphy_netns,
4561 .policy = nl80211_policy,
4562 .flags = GENL_ADMIN_PERM,
4563 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004564 {
4565 .cmd = NL80211_CMD_GET_SURVEY,
4566 .policy = nl80211_policy,
4567 .dumpit = nl80211_dump_survey,
4568 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004569 {
4570 .cmd = NL80211_CMD_SET_PMKSA,
4571 .doit = nl80211_setdel_pmksa,
4572 .policy = nl80211_policy,
4573 .flags = GENL_ADMIN_PERM,
4574 },
4575 {
4576 .cmd = NL80211_CMD_DEL_PMKSA,
4577 .doit = nl80211_setdel_pmksa,
4578 .policy = nl80211_policy,
4579 .flags = GENL_ADMIN_PERM,
4580 },
4581 {
4582 .cmd = NL80211_CMD_FLUSH_PMKSA,
4583 .doit = nl80211_flush_pmksa,
4584 .policy = nl80211_policy,
4585 .flags = GENL_ADMIN_PERM,
4586 },
4587
Johannes Berg55682962007-09-20 13:09:35 -04004588};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004589static struct genl_multicast_group nl80211_mlme_mcgrp = {
4590 .name = "mlme",
4591};
Johannes Berg55682962007-09-20 13:09:35 -04004592
4593/* multicast groups */
4594static struct genl_multicast_group nl80211_config_mcgrp = {
4595 .name = "config",
4596};
Johannes Berg2a519312009-02-10 21:25:55 +01004597static struct genl_multicast_group nl80211_scan_mcgrp = {
4598 .name = "scan",
4599};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004600static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4601 .name = "regulatory",
4602};
Johannes Berg55682962007-09-20 13:09:35 -04004603
4604/* notification functions */
4605
4606void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4607{
4608 struct sk_buff *msg;
4609
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004610 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004611 if (!msg)
4612 return;
4613
4614 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4615 nlmsg_free(msg);
4616 return;
4617 }
4618
Johannes Berg463d0182009-07-14 00:33:35 +02004619 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4620 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04004621}
4622
Johannes Berg362a4152009-05-24 16:43:15 +02004623static int nl80211_add_scan_req(struct sk_buff *msg,
4624 struct cfg80211_registered_device *rdev)
4625{
4626 struct cfg80211_scan_request *req = rdev->scan_req;
4627 struct nlattr *nest;
4628 int i;
4629
Johannes Berg667503d2009-07-07 03:56:11 +02004630 ASSERT_RDEV_LOCK(rdev);
4631
Johannes Berg362a4152009-05-24 16:43:15 +02004632 if (WARN_ON(!req))
4633 return 0;
4634
4635 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4636 if (!nest)
4637 goto nla_put_failure;
4638 for (i = 0; i < req->n_ssids; i++)
4639 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4640 nla_nest_end(msg, nest);
4641
4642 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4643 if (!nest)
4644 goto nla_put_failure;
4645 for (i = 0; i < req->n_channels; i++)
4646 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4647 nla_nest_end(msg, nest);
4648
4649 if (req->ie)
4650 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4651
4652 return 0;
4653 nla_put_failure:
4654 return -ENOBUFS;
4655}
4656
Johannes Berga538e2d2009-06-16 19:56:42 +02004657static int nl80211_send_scan_msg(struct sk_buff *msg,
4658 struct cfg80211_registered_device *rdev,
4659 struct net_device *netdev,
4660 u32 pid, u32 seq, int flags,
4661 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01004662{
4663 void *hdr;
4664
4665 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4666 if (!hdr)
4667 return -1;
4668
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05004669 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01004670 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4671
Johannes Berg362a4152009-05-24 16:43:15 +02004672 /* ignore errors and send incomplete event anyway */
4673 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004674
4675 return genlmsg_end(msg, hdr);
4676
4677 nla_put_failure:
4678 genlmsg_cancel(msg, hdr);
4679 return -EMSGSIZE;
4680}
4681
Johannes Berga538e2d2009-06-16 19:56:42 +02004682void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4683 struct net_device *netdev)
4684{
4685 struct sk_buff *msg;
4686
4687 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4688 if (!msg)
4689 return;
4690
4691 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4692 NL80211_CMD_TRIGGER_SCAN) < 0) {
4693 nlmsg_free(msg);
4694 return;
4695 }
4696
Johannes Berg463d0182009-07-14 00:33:35 +02004697 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4698 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02004699}
4700
Johannes Berg2a519312009-02-10 21:25:55 +01004701void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4702 struct net_device *netdev)
4703{
4704 struct sk_buff *msg;
4705
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004707 if (!msg)
4708 return;
4709
Johannes Berga538e2d2009-06-16 19:56:42 +02004710 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4711 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004712 nlmsg_free(msg);
4713 return;
4714 }
4715
Johannes Berg463d0182009-07-14 00:33:35 +02004716 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4717 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004718}
4719
4720void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4721 struct net_device *netdev)
4722{
4723 struct sk_buff *msg;
4724
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004726 if (!msg)
4727 return;
4728
Johannes Berga538e2d2009-06-16 19:56:42 +02004729 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4730 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004731 nlmsg_free(msg);
4732 return;
4733 }
4734
Johannes Berg463d0182009-07-14 00:33:35 +02004735 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4736 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01004737}
4738
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004739/*
4740 * This can happen on global regulatory changes or device specific settings
4741 * based on custom world regulatory domains.
4742 */
4743void nl80211_send_reg_change_event(struct regulatory_request *request)
4744{
4745 struct sk_buff *msg;
4746 void *hdr;
4747
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004748 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004749 if (!msg)
4750 return;
4751
4752 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
4753 if (!hdr) {
4754 nlmsg_free(msg);
4755 return;
4756 }
4757
4758 /* Userspace can always count this one always being set */
4759 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
4760
4761 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
4762 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4763 NL80211_REGDOM_TYPE_WORLD);
4764 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
4765 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4766 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
4767 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
4768 request->intersect)
4769 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4770 NL80211_REGDOM_TYPE_INTERSECTION);
4771 else {
4772 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
4773 NL80211_REGDOM_TYPE_COUNTRY);
4774 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
4775 }
4776
4777 if (wiphy_idx_valid(request->wiphy_idx))
4778 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
4779
4780 if (genlmsg_end(msg, hdr) < 0) {
4781 nlmsg_free(msg);
4782 return;
4783 }
4784
Johannes Bergbc43b282009-07-25 10:54:13 +02004785 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02004786 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02004787 GFP_ATOMIC);
4788 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04004789
4790 return;
4791
4792nla_put_failure:
4793 genlmsg_cancel(msg, hdr);
4794 nlmsg_free(msg);
4795}
4796
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004797static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
4798 struct net_device *netdev,
4799 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004800 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004801{
4802 struct sk_buff *msg;
4803 void *hdr;
4804
Johannes Berge6d6e342009-07-01 21:26:47 +02004805 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004806 if (!msg)
4807 return;
4808
4809 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4810 if (!hdr) {
4811 nlmsg_free(msg);
4812 return;
4813 }
4814
4815 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4816 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4817 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
4818
4819 if (genlmsg_end(msg, hdr) < 0) {
4820 nlmsg_free(msg);
4821 return;
4822 }
4823
Johannes Berg463d0182009-07-14 00:33:35 +02004824 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4825 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004826 return;
4827
4828 nla_put_failure:
4829 genlmsg_cancel(msg, hdr);
4830 nlmsg_free(msg);
4831}
4832
4833void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004834 struct net_device *netdev, const u8 *buf,
4835 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004836{
4837 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004838 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004839}
4840
4841void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
4842 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004843 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004844{
Johannes Berge6d6e342009-07-01 21:26:47 +02004845 nl80211_send_mlme_event(rdev, netdev, buf, len,
4846 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004847}
4848
Jouni Malinen53b46b82009-03-27 20:53:56 +02004849void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004850 struct net_device *netdev, const u8 *buf,
4851 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004852{
4853 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004854 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004855}
4856
Jouni Malinen53b46b82009-03-27 20:53:56 +02004857void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
4858 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02004859 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004860{
4861 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02004862 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02004863}
4864
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04004865static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
4866 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02004867 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004868{
4869 struct sk_buff *msg;
4870 void *hdr;
4871
Johannes Berge6d6e342009-07-01 21:26:47 +02004872 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004873 if (!msg)
4874 return;
4875
4876 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
4877 if (!hdr) {
4878 nlmsg_free(msg);
4879 return;
4880 }
4881
4882 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4884 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
4885 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4886
4887 if (genlmsg_end(msg, hdr) < 0) {
4888 nlmsg_free(msg);
4889 return;
4890 }
4891
Johannes Berg463d0182009-07-14 00:33:35 +02004892 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4893 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004894 return;
4895
4896 nla_put_failure:
4897 genlmsg_cancel(msg, hdr);
4898 nlmsg_free(msg);
4899}
4900
4901void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004902 struct net_device *netdev, const u8 *addr,
4903 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004904{
4905 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02004906 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004907}
4908
4909void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02004910 struct net_device *netdev, const u8 *addr,
4911 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03004912{
Johannes Berge6d6e342009-07-01 21:26:47 +02004913 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
4914 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03004915}
4916
Samuel Ortizb23aa672009-07-01 21:26:54 +02004917void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
4918 struct net_device *netdev, const u8 *bssid,
4919 const u8 *req_ie, size_t req_ie_len,
4920 const u8 *resp_ie, size_t resp_ie_len,
4921 u16 status, gfp_t gfp)
4922{
4923 struct sk_buff *msg;
4924 void *hdr;
4925
4926 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4927 if (!msg)
4928 return;
4929
4930 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
4931 if (!hdr) {
4932 nlmsg_free(msg);
4933 return;
4934 }
4935
4936 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4937 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4938 if (bssid)
4939 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4940 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
4941 if (req_ie)
4942 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4943 if (resp_ie)
4944 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4945
4946 if (genlmsg_end(msg, hdr) < 0) {
4947 nlmsg_free(msg);
4948 return;
4949 }
4950
Johannes Berg463d0182009-07-14 00:33:35 +02004951 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4952 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004953 return;
4954
4955 nla_put_failure:
4956 genlmsg_cancel(msg, hdr);
4957 nlmsg_free(msg);
4958
4959}
4960
4961void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
4962 struct net_device *netdev, const u8 *bssid,
4963 const u8 *req_ie, size_t req_ie_len,
4964 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
4965{
4966 struct sk_buff *msg;
4967 void *hdr;
4968
4969 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
4970 if (!msg)
4971 return;
4972
4973 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
4974 if (!hdr) {
4975 nlmsg_free(msg);
4976 return;
4977 }
4978
4979 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4980 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4981 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
4982 if (req_ie)
4983 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
4984 if (resp_ie)
4985 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
4986
4987 if (genlmsg_end(msg, hdr) < 0) {
4988 nlmsg_free(msg);
4989 return;
4990 }
4991
Johannes Berg463d0182009-07-14 00:33:35 +02004992 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4993 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004994 return;
4995
4996 nla_put_failure:
4997 genlmsg_cancel(msg, hdr);
4998 nlmsg_free(msg);
4999
5000}
5001
5002void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5003 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005004 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005005{
5006 struct sk_buff *msg;
5007 void *hdr;
5008
Johannes Berg667503d2009-07-07 03:56:11 +02005009 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005010 if (!msg)
5011 return;
5012
5013 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5014 if (!hdr) {
5015 nlmsg_free(msg);
5016 return;
5017 }
5018
5019 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5020 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5021 if (from_ap && reason)
5022 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5023 if (from_ap)
5024 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5025 if (ie)
5026 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5027
5028 if (genlmsg_end(msg, hdr) < 0) {
5029 nlmsg_free(msg);
5030 return;
5031 }
5032
Johannes Berg463d0182009-07-14 00:33:35 +02005033 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5034 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005035 return;
5036
5037 nla_put_failure:
5038 genlmsg_cancel(msg, hdr);
5039 nlmsg_free(msg);
5040
5041}
5042
Johannes Berg04a773a2009-04-19 21:24:32 +02005043void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5044 struct net_device *netdev, const u8 *bssid,
5045 gfp_t gfp)
5046{
5047 struct sk_buff *msg;
5048 void *hdr;
5049
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005050 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005051 if (!msg)
5052 return;
5053
5054 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5055 if (!hdr) {
5056 nlmsg_free(msg);
5057 return;
5058 }
5059
5060 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5061 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5062 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5063
5064 if (genlmsg_end(msg, hdr) < 0) {
5065 nlmsg_free(msg);
5066 return;
5067 }
5068
Johannes Berg463d0182009-07-14 00:33:35 +02005069 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5070 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005071 return;
5072
5073 nla_put_failure:
5074 genlmsg_cancel(msg, hdr);
5075 nlmsg_free(msg);
5076}
5077
Jouni Malinena3b8b052009-03-27 21:59:49 +02005078void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5079 struct net_device *netdev, const u8 *addr,
5080 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005081 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005082{
5083 struct sk_buff *msg;
5084 void *hdr;
5085
Johannes Berge6d6e342009-07-01 21:26:47 +02005086 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005087 if (!msg)
5088 return;
5089
5090 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5091 if (!hdr) {
5092 nlmsg_free(msg);
5093 return;
5094 }
5095
5096 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5097 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5098 if (addr)
5099 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5100 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5101 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5102 if (tsc)
5103 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5104
5105 if (genlmsg_end(msg, hdr) < 0) {
5106 nlmsg_free(msg);
5107 return;
5108 }
5109
Johannes Berg463d0182009-07-14 00:33:35 +02005110 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5111 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005112 return;
5113
5114 nla_put_failure:
5115 genlmsg_cancel(msg, hdr);
5116 nlmsg_free(msg);
5117}
5118
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005119void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5120 struct ieee80211_channel *channel_before,
5121 struct ieee80211_channel *channel_after)
5122{
5123 struct sk_buff *msg;
5124 void *hdr;
5125 struct nlattr *nl_freq;
5126
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005127 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005128 if (!msg)
5129 return;
5130
5131 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5132 if (!hdr) {
5133 nlmsg_free(msg);
5134 return;
5135 }
5136
5137 /*
5138 * Since we are applying the beacon hint to a wiphy we know its
5139 * wiphy_idx is valid
5140 */
5141 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5142
5143 /* Before */
5144 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5145 if (!nl_freq)
5146 goto nla_put_failure;
5147 if (nl80211_msg_put_channel(msg, channel_before))
5148 goto nla_put_failure;
5149 nla_nest_end(msg, nl_freq);
5150
5151 /* After */
5152 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5153 if (!nl_freq)
5154 goto nla_put_failure;
5155 if (nl80211_msg_put_channel(msg, channel_after))
5156 goto nla_put_failure;
5157 nla_nest_end(msg, nl_freq);
5158
5159 if (genlmsg_end(msg, hdr) < 0) {
5160 nlmsg_free(msg);
5161 return;
5162 }
5163
Johannes Berg463d0182009-07-14 00:33:35 +02005164 rcu_read_lock();
5165 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5166 GFP_ATOMIC);
5167 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005168
5169 return;
5170
5171nla_put_failure:
5172 genlmsg_cancel(msg, hdr);
5173 nlmsg_free(msg);
5174}
5175
Johannes Berg55682962007-09-20 13:09:35 -04005176/* initialisation/exit functions */
5177
5178int nl80211_init(void)
5179{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005180 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005181
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005182 err = genl_register_family_with_ops(&nl80211_fam,
5183 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005184 if (err)
5185 return err;
5186
Johannes Berg55682962007-09-20 13:09:35 -04005187 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5188 if (err)
5189 goto err_out;
5190
Johannes Berg2a519312009-02-10 21:25:55 +01005191 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5192 if (err)
5193 goto err_out;
5194
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005195 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5196 if (err)
5197 goto err_out;
5198
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005199 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5200 if (err)
5201 goto err_out;
5202
Johannes Bergaff89a92009-07-01 21:26:51 +02005203#ifdef CONFIG_NL80211_TESTMODE
5204 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5205 if (err)
5206 goto err_out;
5207#endif
5208
Johannes Berg55682962007-09-20 13:09:35 -04005209 return 0;
5210 err_out:
5211 genl_unregister_family(&nl80211_fam);
5212 return err;
5213}
5214
5215void nl80211_exit(void)
5216{
5217 genl_unregister_family(&nl80211_fam);
5218}