blob: db03ed5ce05564a879c531ba2d1c6aeddd116f73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file implement the Wireless Extensions APIs.
3 *
4 * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00005 * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * (As all part of the Linux kernel, this file is GPL)
8 */
9
10/************************** DOCUMENTATION **************************/
11/*
12 * API definition :
13 * --------------
14 * See <linux/wireless.h> for details of the APIs and the rest.
15 *
16 * History :
17 * -------
18 *
19 * v1 - 5.12.01 - Jean II
20 * o Created this file.
21 *
22 * v2 - 13.12.01 - Jean II
23 * o Move /proc/net/wireless stuff from net/core/dev.c to here
24 * o Make Wireless Extension IOCTLs go through here
25 * o Added iw_handler handling ;-)
26 * o Added standard ioctl description
27 * o Initial dumb commit strategy based on orinoco.c
28 *
29 * v3 - 19.12.01 - Jean II
30 * o Make sure we don't go out of standard_ioctl[] in ioctl_standard_call
31 * o Add event dispatcher function
32 * o Add event description
33 * o Propagate events as rtnetlink IFLA_WIRELESS option
34 * o Generate event on selected SET requests
35 *
36 * v4 - 18.04.02 - Jean II
37 * o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1
38 *
39 * v5 - 21.06.02 - Jean II
40 * o Add IW_PRIV_TYPE_ADDR in priv_type_size (+cleanup)
41 * o Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes
42 * o Add IWEVCUSTOM for driver specific event/scanning token
43 * o Turn on WE_STRICT_WRITE by default + kernel warning
44 * o Fix WE_STRICT_WRITE in ioctl_export_private() (32 => iw_num)
45 * o Fix off-by-one in test (extra_size <= IFNAMSIZ)
46 *
47 * v6 - 9.01.03 - Jean II
48 * o Add common spy support : iw_handler_set_spy(), wireless_spy_update()
49 * o Add enhanced spy support : iw_handler_set_thrspy() and event.
50 * o Add WIRELESS_EXT version display in /proc/net/wireless
51 *
52 * v6 - 18.06.04 - Jean II
53 * o Change get_spydata() method for added safety
54 * o Remove spy #ifdef, they are always on -> cleaner code
55 * o Allow any size GET request if user specifies length > max
56 * and if request has IW_DESCR_FLAG_NOMAX flag or is SIOCGIWPRIV
57 * o Start migrating get_wireless_stats to struct iw_handler_def
58 * o Add wmb() in iw_handler_set_spy() for non-coherent archs/cpus
59 * Based on patch from Pavel Roskin <proski@gnu.org> :
60 * o Fix kernel data leak to user space in private handler handling
Jean Tourrilhes6582c162005-09-02 11:32:28 -070061 *
62 * v7 - 18.3.05 - Jean II
63 * o Remove (struct iw_point *)->pointer from events and streams
64 * o Remove spy_offset from struct iw_handler_def
65 * o Start deprecating dev->get_wireless_stats, output a warning
66 * o If IW_QUAL_DBM is set, show dBm values in /proc/net/wireless
67 * o Don't loose INVALID/DBM flags when clearing UPDATED flags (iwstats)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -080068 *
69 * v8 - 17.02.06 - Jean II
70 * o RtNetlink requests support (SET/GET)
John W. Linvillebaef1862006-09-08 16:04:05 -040071 *
72 * v8b - 03.08.06 - Herbert Xu
73 * o Fix Wireless Event locking issues.
74 *
75 * v9 - 14.3.06 - Jean II
76 * o Change length in ESSID and NICK to strlen() instead of strlen()+1
77 * o Make standard_ioctl_num and standard_event_num unsigned
78 * o Remove (struct net_device *)->get_wireless_stats()
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +000079 *
80 * v10 - 16.3.07 - Jean II
81 * o Prevent leaking of kernel space in stream on 64 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84/***************************** INCLUDES *****************************/
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/module.h>
87#include <linux/types.h> /* off_t */
88#include <linux/netdevice.h> /* struct ifreq, dev_get_by_name() */
89#include <linux/proc_fs.h>
90#include <linux/rtnetlink.h> /* rtnetlink stuff */
91#include <linux/seq_file.h>
92#include <linux/init.h> /* for __init */
93#include <linux/if_arp.h> /* ARPHRD_ETHER */
Linus Torvalds9819d852006-01-10 19:35:19 -080094#include <linux/etherdevice.h> /* compare_ether_addr */
Herbert Xu782a6672006-08-03 23:54:41 +100095#include <linux/interrupt.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020096#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#include <linux/wireless.h> /* Pretty obvious */
99#include <net/iw_handler.h> /* New driver API */
Thomas Grafbd5785b2006-08-15 00:36:49 -0700100#include <net/netlink.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700101#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#include <asm/uaccess.h> /* copy_to_user() */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/************************* GLOBAL VARIABLES *************************/
106/*
107 * You should not use global variables, because of re-entrancy.
108 * On our case, it's only const, so it's OK...
109 */
110/*
111 * Meta-data about all the standard Wireless Extension request we
112 * know about.
113 */
114static const struct iw_ioctl_description standard_ioctl[] = {
115 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
116 .header_type = IW_HEADER_TYPE_NULL,
117 },
118 [SIOCGIWNAME - SIOCIWFIRST] = {
119 .header_type = IW_HEADER_TYPE_CHAR,
120 .flags = IW_DESCR_FLAG_DUMP,
121 },
122 [SIOCSIWNWID - SIOCIWFIRST] = {
123 .header_type = IW_HEADER_TYPE_PARAM,
124 .flags = IW_DESCR_FLAG_EVENT,
125 },
126 [SIOCGIWNWID - SIOCIWFIRST] = {
127 .header_type = IW_HEADER_TYPE_PARAM,
128 .flags = IW_DESCR_FLAG_DUMP,
129 },
130 [SIOCSIWFREQ - SIOCIWFIRST] = {
131 .header_type = IW_HEADER_TYPE_FREQ,
132 .flags = IW_DESCR_FLAG_EVENT,
133 },
134 [SIOCGIWFREQ - SIOCIWFIRST] = {
135 .header_type = IW_HEADER_TYPE_FREQ,
136 .flags = IW_DESCR_FLAG_DUMP,
137 },
138 [SIOCSIWMODE - SIOCIWFIRST] = {
139 .header_type = IW_HEADER_TYPE_UINT,
140 .flags = IW_DESCR_FLAG_EVENT,
141 },
142 [SIOCGIWMODE - SIOCIWFIRST] = {
143 .header_type = IW_HEADER_TYPE_UINT,
144 .flags = IW_DESCR_FLAG_DUMP,
145 },
146 [SIOCSIWSENS - SIOCIWFIRST] = {
147 .header_type = IW_HEADER_TYPE_PARAM,
148 },
149 [SIOCGIWSENS - SIOCIWFIRST] = {
150 .header_type = IW_HEADER_TYPE_PARAM,
151 },
152 [SIOCSIWRANGE - SIOCIWFIRST] = {
153 .header_type = IW_HEADER_TYPE_NULL,
154 },
155 [SIOCGIWRANGE - SIOCIWFIRST] = {
156 .header_type = IW_HEADER_TYPE_POINT,
157 .token_size = 1,
158 .max_tokens = sizeof(struct iw_range),
159 .flags = IW_DESCR_FLAG_DUMP,
160 },
161 [SIOCSIWPRIV - SIOCIWFIRST] = {
162 .header_type = IW_HEADER_TYPE_NULL,
163 },
164 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800165 .header_type = IW_HEADER_TYPE_POINT,
166 .token_size = sizeof(struct iw_priv_args),
167 .max_tokens = 16,
168 .flags = IW_DESCR_FLAG_NOMAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 },
170 [SIOCSIWSTATS - SIOCIWFIRST] = {
171 .header_type = IW_HEADER_TYPE_NULL,
172 },
173 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800174 .header_type = IW_HEADER_TYPE_POINT,
175 .token_size = 1,
176 .max_tokens = sizeof(struct iw_statistics),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .flags = IW_DESCR_FLAG_DUMP,
178 },
179 [SIOCSIWSPY - SIOCIWFIRST] = {
180 .header_type = IW_HEADER_TYPE_POINT,
181 .token_size = sizeof(struct sockaddr),
182 .max_tokens = IW_MAX_SPY,
183 },
184 [SIOCGIWSPY - SIOCIWFIRST] = {
185 .header_type = IW_HEADER_TYPE_POINT,
186 .token_size = sizeof(struct sockaddr) +
187 sizeof(struct iw_quality),
188 .max_tokens = IW_MAX_SPY,
189 },
190 [SIOCSIWTHRSPY - SIOCIWFIRST] = {
191 .header_type = IW_HEADER_TYPE_POINT,
192 .token_size = sizeof(struct iw_thrspy),
193 .min_tokens = 1,
194 .max_tokens = 1,
195 },
196 [SIOCGIWTHRSPY - SIOCIWFIRST] = {
197 .header_type = IW_HEADER_TYPE_POINT,
198 .token_size = sizeof(struct iw_thrspy),
199 .min_tokens = 1,
200 .max_tokens = 1,
201 },
202 [SIOCSIWAP - SIOCIWFIRST] = {
203 .header_type = IW_HEADER_TYPE_ADDR,
204 },
205 [SIOCGIWAP - SIOCIWFIRST] = {
206 .header_type = IW_HEADER_TYPE_ADDR,
207 .flags = IW_DESCR_FLAG_DUMP,
208 },
fff9cfd2005-05-12 20:24:19 -0400209 [SIOCSIWMLME - SIOCIWFIRST] = {
210 .header_type = IW_HEADER_TYPE_POINT,
211 .token_size = 1,
212 .min_tokens = sizeof(struct iw_mlme),
213 .max_tokens = sizeof(struct iw_mlme),
214 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 [SIOCGIWAPLIST - SIOCIWFIRST] = {
216 .header_type = IW_HEADER_TYPE_POINT,
217 .token_size = sizeof(struct sockaddr) +
218 sizeof(struct iw_quality),
219 .max_tokens = IW_MAX_AP,
220 .flags = IW_DESCR_FLAG_NOMAX,
221 },
222 [SIOCSIWSCAN - SIOCIWFIRST] = {
fff9cfd2005-05-12 20:24:19 -0400223 .header_type = IW_HEADER_TYPE_POINT,
224 .token_size = 1,
225 .min_tokens = 0,
226 .max_tokens = sizeof(struct iw_scan_req),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 },
228 [SIOCGIWSCAN - SIOCIWFIRST] = {
229 .header_type = IW_HEADER_TYPE_POINT,
230 .token_size = 1,
231 .max_tokens = IW_SCAN_MAX_DATA,
232 .flags = IW_DESCR_FLAG_NOMAX,
233 },
234 [SIOCSIWESSID - SIOCIWFIRST] = {
235 .header_type = IW_HEADER_TYPE_POINT,
236 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400237 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .flags = IW_DESCR_FLAG_EVENT,
239 },
240 [SIOCGIWESSID - SIOCIWFIRST] = {
241 .header_type = IW_HEADER_TYPE_POINT,
242 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400243 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .flags = IW_DESCR_FLAG_DUMP,
245 },
246 [SIOCSIWNICKN - SIOCIWFIRST] = {
247 .header_type = IW_HEADER_TYPE_POINT,
248 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400249 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 },
251 [SIOCGIWNICKN - SIOCIWFIRST] = {
252 .header_type = IW_HEADER_TYPE_POINT,
253 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400254 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 },
256 [SIOCSIWRATE - SIOCIWFIRST] = {
257 .header_type = IW_HEADER_TYPE_PARAM,
258 },
259 [SIOCGIWRATE - SIOCIWFIRST] = {
260 .header_type = IW_HEADER_TYPE_PARAM,
261 },
262 [SIOCSIWRTS - SIOCIWFIRST] = {
263 .header_type = IW_HEADER_TYPE_PARAM,
264 },
265 [SIOCGIWRTS - SIOCIWFIRST] = {
266 .header_type = IW_HEADER_TYPE_PARAM,
267 },
268 [SIOCSIWFRAG - SIOCIWFIRST] = {
269 .header_type = IW_HEADER_TYPE_PARAM,
270 },
271 [SIOCGIWFRAG - SIOCIWFIRST] = {
272 .header_type = IW_HEADER_TYPE_PARAM,
273 },
274 [SIOCSIWTXPOW - SIOCIWFIRST] = {
275 .header_type = IW_HEADER_TYPE_PARAM,
276 },
277 [SIOCGIWTXPOW - SIOCIWFIRST] = {
278 .header_type = IW_HEADER_TYPE_PARAM,
279 },
280 [SIOCSIWRETRY - SIOCIWFIRST] = {
281 .header_type = IW_HEADER_TYPE_PARAM,
282 },
283 [SIOCGIWRETRY - SIOCIWFIRST] = {
284 .header_type = IW_HEADER_TYPE_PARAM,
285 },
286 [SIOCSIWENCODE - SIOCIWFIRST] = {
287 .header_type = IW_HEADER_TYPE_POINT,
288 .token_size = 1,
289 .max_tokens = IW_ENCODING_TOKEN_MAX,
290 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
291 },
292 [SIOCGIWENCODE - SIOCIWFIRST] = {
293 .header_type = IW_HEADER_TYPE_POINT,
294 .token_size = 1,
295 .max_tokens = IW_ENCODING_TOKEN_MAX,
296 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
297 },
298 [SIOCSIWPOWER - SIOCIWFIRST] = {
299 .header_type = IW_HEADER_TYPE_PARAM,
300 },
301 [SIOCGIWPOWER - SIOCIWFIRST] = {
302 .header_type = IW_HEADER_TYPE_PARAM,
303 },
fff9cfd2005-05-12 20:24:19 -0400304 [SIOCSIWGENIE - SIOCIWFIRST] = {
305 .header_type = IW_HEADER_TYPE_POINT,
306 .token_size = 1,
307 .max_tokens = IW_GENERIC_IE_MAX,
308 },
309 [SIOCGIWGENIE - SIOCIWFIRST] = {
310 .header_type = IW_HEADER_TYPE_POINT,
311 .token_size = 1,
312 .max_tokens = IW_GENERIC_IE_MAX,
313 },
314 [SIOCSIWAUTH - SIOCIWFIRST] = {
315 .header_type = IW_HEADER_TYPE_PARAM,
316 },
317 [SIOCGIWAUTH - SIOCIWFIRST] = {
318 .header_type = IW_HEADER_TYPE_PARAM,
319 },
320 [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
321 .header_type = IW_HEADER_TYPE_POINT,
322 .token_size = 1,
323 .min_tokens = sizeof(struct iw_encode_ext),
324 .max_tokens = sizeof(struct iw_encode_ext) +
325 IW_ENCODING_TOKEN_MAX,
326 },
327 [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
328 .header_type = IW_HEADER_TYPE_POINT,
329 .token_size = 1,
330 .min_tokens = sizeof(struct iw_encode_ext),
331 .max_tokens = sizeof(struct iw_encode_ext) +
332 IW_ENCODING_TOKEN_MAX,
333 },
334 [SIOCSIWPMKSA - SIOCIWFIRST] = {
335 .header_type = IW_HEADER_TYPE_POINT,
336 .token_size = 1,
337 .min_tokens = sizeof(struct iw_pmksa),
338 .max_tokens = sizeof(struct iw_pmksa),
339 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800341static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343/*
344 * Meta-data about all the additional standard Wireless Extension events
345 * we know about.
346 */
347static const struct iw_ioctl_description standard_event[] = {
348 [IWEVTXDROP - IWEVFIRST] = {
349 .header_type = IW_HEADER_TYPE_ADDR,
350 },
351 [IWEVQUAL - IWEVFIRST] = {
352 .header_type = IW_HEADER_TYPE_QUAL,
353 },
354 [IWEVCUSTOM - IWEVFIRST] = {
355 .header_type = IW_HEADER_TYPE_POINT,
356 .token_size = 1,
357 .max_tokens = IW_CUSTOM_MAX,
358 },
359 [IWEVREGISTERED - IWEVFIRST] = {
360 .header_type = IW_HEADER_TYPE_ADDR,
361 },
362 [IWEVEXPIRED - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900363 .header_type = IW_HEADER_TYPE_ADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 },
fff9cfd2005-05-12 20:24:19 -0400365 [IWEVGENIE - IWEVFIRST] = {
366 .header_type = IW_HEADER_TYPE_POINT,
367 .token_size = 1,
368 .max_tokens = IW_GENERIC_IE_MAX,
369 },
370 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900371 .header_type = IW_HEADER_TYPE_POINT,
fff9cfd2005-05-12 20:24:19 -0400372 .token_size = 1,
373 .max_tokens = sizeof(struct iw_michaelmicfailure),
374 },
375 [IWEVASSOCREQIE - IWEVFIRST] = {
376 .header_type = IW_HEADER_TYPE_POINT,
377 .token_size = 1,
378 .max_tokens = IW_GENERIC_IE_MAX,
379 },
380 [IWEVASSOCRESPIE - IWEVFIRST] = {
381 .header_type = IW_HEADER_TYPE_POINT,
382 .token_size = 1,
383 .max_tokens = IW_GENERIC_IE_MAX,
384 },
385 [IWEVPMKIDCAND - IWEVFIRST] = {
386 .header_type = IW_HEADER_TYPE_POINT,
387 .token_size = 1,
388 .max_tokens = sizeof(struct iw_pmkid_cand),
389 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800391static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* Size (in bytes) of the various private data types */
394static const char iw_priv_type_size[] = {
395 0, /* IW_PRIV_TYPE_NONE */
396 1, /* IW_PRIV_TYPE_BYTE */
397 1, /* IW_PRIV_TYPE_CHAR */
398 0, /* Not defined */
399 sizeof(__u32), /* IW_PRIV_TYPE_INT */
400 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
401 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
402 0, /* Not defined */
403};
404
405/* Size (in bytes) of various events */
406static const int event_type_size[] = {
407 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
408 0,
409 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
410 0,
411 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
412 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
413 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
414 0,
415 IW_EV_POINT_LEN, /* Without variable payload */
416 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
417 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
418};
419
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +0000420/* Size (in bytes) of various events, as packed */
421static const int event_type_pk_size[] = {
422 IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
423 0,
424 IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
425 0,
426 IW_EV_UINT_PK_LEN, /* IW_HEADER_TYPE_UINT */
427 IW_EV_FREQ_PK_LEN, /* IW_HEADER_TYPE_FREQ */
428 IW_EV_ADDR_PK_LEN, /* IW_HEADER_TYPE_ADDR */
429 0,
430 IW_EV_POINT_PK_LEN, /* Without variable payload */
431 IW_EV_PARAM_PK_LEN, /* IW_HEADER_TYPE_PARAM */
432 IW_EV_QUAL_PK_LEN, /* IW_HEADER_TYPE_QUAL */
433};
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/************************ COMMON SUBROUTINES ************************/
436/*
437 * Stuff that may be used in various place or doesn't fit in one
438 * of the section below.
439 */
440
441/* ---------------------------------------------------------------- */
442/*
443 * Return the driver handler associated with a specific Wireless Extension.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700445static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 /* Don't "optimise" the following variable, it will crash */
448 unsigned int index; /* *MUST* be unsigned */
449
450 /* Check if we have some wireless handlers defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700451 if (dev->wireless_handlers == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return NULL;
453
454 /* Try as a standard command */
455 index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700456 if (index < dev->wireless_handlers->num_standard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return dev->wireless_handlers->standard[index];
458
459 /* Try as a private command */
460 index = cmd - SIOCIWFIRSTPRIV;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700461 if (index < dev->wireless_handlers->num_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return dev->wireless_handlers->private[index];
463
464 /* Not found */
465 return NULL;
466}
467
468/* ---------------------------------------------------------------- */
469/*
470 * Get statistics out of the driver
471 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700472static struct iw_statistics *get_wireless_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 /* New location */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700475 if ((dev->wireless_handlers != NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 (dev->wireless_handlers->get_wireless_stats != NULL))
477 return dev->wireless_handlers->get_wireless_stats(dev);
478
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700479 /* Not found */
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700480 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/* ---------------------------------------------------------------- */
484/*
485 * Call the commit handler in the driver
486 * (if exist and if conditions are right)
487 *
488 * Note : our current commit strategy is currently pretty dumb,
489 * but we will be able to improve on that...
490 * The goal is to try to agreagate as many changes as possible
491 * before doing the commit. Drivers that will define a commit handler
492 * are usually those that need a reset after changing parameters, so
493 * we want to minimise the number of reset.
494 * A cool idea is to use a timer : at each "set" command, we re-set the
495 * timer, when the timer eventually fires, we call the driver.
496 * Hopefully, more on that later.
497 *
498 * Also, I'm waiting to see how many people will complain about the
499 * netif_running(dev) test. I'm open on that one...
500 * Hopefully, the driver will remember to do a commit in "open()" ;-)
501 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700502static int call_commit_handler(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700504 if ((netif_running(dev)) &&
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700505 (dev->wireless_handlers->standard[0] != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Call the commit handler on the driver */
507 return dev->wireless_handlers->standard[0](dev, NULL,
508 NULL, NULL);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700509 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0; /* Command completed successfully */
511}
512
513/* ---------------------------------------------------------------- */
514/*
515 * Calculate size of private arguments
516 */
517static inline int get_priv_size(__u16 args)
518{
519 int num = args & IW_PRIV_SIZE_MASK;
520 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
521
522 return num * iw_priv_type_size[type];
523}
524
525/* ---------------------------------------------------------------- */
526/*
527 * Re-calculate the size of private arguments
528 */
529static inline int adjust_priv_size(__u16 args,
530 union iwreq_data * wrqu)
531{
532 int num = wrqu->data.length;
533 int max = args & IW_PRIV_SIZE_MASK;
534 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
535
536 /* Make sure the driver doesn't goof up */
537 if (max < num)
538 num = max;
539
540 return num * iw_priv_type_size[type];
541}
542
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800543/* ---------------------------------------------------------------- */
544/*
545 * Standard Wireless Handler : get wireless stats
546 * Allow programatic access to /proc/net/wireless even if /proc
547 * doesn't exist... Also more efficient...
548 */
549static int iw_handler_get_iwstats(struct net_device * dev,
550 struct iw_request_info * info,
551 union iwreq_data * wrqu,
552 char * extra)
553{
554 /* Get stats from the driver */
555 struct iw_statistics *stats;
556
557 stats = get_wireless_stats(dev);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700558 if (stats) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800559 /* Copy statistics to extra */
560 memcpy(extra, stats, sizeof(struct iw_statistics));
561 wrqu->data.length = sizeof(struct iw_statistics);
562
563 /* Check if we need to clear the updated flag */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700564 if (wrqu->data.flags != 0)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800565 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
566 return 0;
567 } else
568 return -EOPNOTSUPP;
569}
570
571/* ---------------------------------------------------------------- */
572/*
573 * Standard Wireless Handler : get iwpriv definitions
574 * Export the driver private handler definition
575 * They will be picked up by tools like iwpriv...
576 */
577static int iw_handler_get_private(struct net_device * dev,
578 struct iw_request_info * info,
579 union iwreq_data * wrqu,
580 char * extra)
581{
582 /* Check if the driver has something to export */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700583 if ((dev->wireless_handlers->num_private_args == 0) ||
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800584 (dev->wireless_handlers->private_args == NULL))
585 return -EOPNOTSUPP;
586
587 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700588 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800589 /* User space can't know in advance how large the buffer
590 * needs to be. Give it a hint, so that we can support
591 * any size buffer we want somewhat efficiently... */
592 wrqu->data.length = dev->wireless_handlers->num_private_args;
593 return -E2BIG;
594 }
595
596 /* Set the number of available ioctls. */
597 wrqu->data.length = dev->wireless_handlers->num_private_args;
598
599 /* Copy structure to the user buffer. */
600 memcpy(extra, dev->wireless_handlers->private_args,
601 sizeof(struct iw_priv_args) * wrqu->data.length);
602
603 return 0;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/******************** /proc/net/wireless SUPPORT ********************/
608/*
609 * The /proc/net/wireless file is a human readable user-space interface
610 * exporting various wireless specific statistics from the wireless devices.
611 * This is the most popular part of the Wireless Extensions ;-)
612 *
613 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
614 * The content of the file is basically the content of "struct iw_statistics".
615 */
616
617#ifdef CONFIG_PROC_FS
618
619/* ---------------------------------------------------------------- */
620/*
621 * Print one entry (line) of /proc/net/wireless
622 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700623static void wireless_seq_printf_stats(struct seq_file *seq,
624 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 /* Get stats from the driver */
627 struct iw_statistics *stats = get_wireless_stats(dev);
628
629 if (stats) {
630 seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
631 "%6d %6d %6d\n",
632 dev->name, stats->status, stats->qual.qual,
633 stats->qual.updated & IW_QUAL_QUAL_UPDATED
634 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900635 ((__s32) stats->qual.level) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700636 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
638 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900639 ((__s32) stats->qual.noise) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700640 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 stats->qual.updated & IW_QUAL_NOISE_UPDATED
642 ? '.' : ' ',
643 stats->discard.nwid, stats->discard.code,
644 stats->discard.fragment, stats->discard.retries,
645 stats->discard.misc, stats->miss.beacon);
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700646 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648}
649
650/* ---------------------------------------------------------------- */
651/*
652 * Print info for /proc/net/wireless (print all entries)
653 */
654static int wireless_seq_show(struct seq_file *seq, void *v)
655{
656 if (v == SEQ_START_TOKEN)
657 seq_printf(seq, "Inter-| sta-| Quality | Discarded "
658 "packets | Missed | WE\n"
659 " face | tus | link level noise | nwid "
660 "crypt frag retry misc | beacon | %d\n",
661 WIRELESS_EXT);
662 else
663 wireless_seq_printf_stats(seq, v);
664 return 0;
665}
666
Stephen Hemmingerf6908082007-03-12 14:34:29 -0700667static const struct seq_operations wireless_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 .start = dev_seq_start,
669 .next = dev_seq_next,
670 .stop = dev_seq_stop,
671 .show = wireless_seq_show,
672};
673
674static int wireless_seq_open(struct inode *inode, struct file *file)
675{
Eric W. Biederman881d9662007-09-17 11:56:21 -0700676 struct seq_file *seq;
677 int res;
678 res = seq_open(file, &wireless_seq_ops);
679 if (!res) {
680 seq = file->private_data;
Eric W. Biederman077130c2007-09-13 09:18:57 +0200681 seq->private = get_proc_net(inode);
682 if (!seq->private) {
683 seq_release(inode, file);
684 res = -ENXIO;
685 }
Eric W. Biederman881d9662007-09-17 11:56:21 -0700686 }
687 return res;
688}
689
690static int wireless_seq_release(struct inode *inode, struct file *file)
691{
692 struct seq_file *seq = file->private_data;
693 struct net *net = seq->private;
694 put_net(net);
695 return seq_release(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Arjan van de Ven9a321442007-02-12 00:55:35 -0800698static const struct file_operations wireless_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 .owner = THIS_MODULE,
700 .open = wireless_seq_open,
701 .read = seq_read,
702 .llseek = seq_lseek,
Eric W. Biederman881d9662007-09-17 11:56:21 -0700703 .release = wireless_seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705
Eric W. Biederman881d9662007-09-17 11:56:21 -0700706int wext_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700708 /* Create /proc/net/wireless entry */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700709 if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -ENOMEM;
711
712 return 0;
713}
Eric W. Biederman881d9662007-09-17 11:56:21 -0700714
715void wext_proc_exit(struct net *net)
716{
717 proc_net_remove(net, "wireless");
718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#endif /* CONFIG_PROC_FS */
720
721/************************** IOCTL SUPPORT **************************/
722/*
723 * The original user space API to configure all those Wireless Extensions
724 * is through IOCTLs.
725 * In there, we check if we need to call the new driver API (iw_handler)
726 * or just call the driver ioctl handler.
727 */
728
729/* ---------------------------------------------------------------- */
730/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * Wrapper to call a standard Wireless Extension handler.
732 * We do various checks and also take care of moving data between
733 * user space and kernel space.
734 */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800735static int ioctl_standard_call(struct net_device * dev,
736 struct ifreq * ifr,
737 unsigned int cmd,
738 iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 struct iwreq * iwr = (struct iwreq *) ifr;
741 const struct iw_ioctl_description * descr;
742 struct iw_request_info info;
743 int ret = -EINVAL;
744
745 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700746 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -EOPNOTSUPP;
748 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Prepare the call */
751 info.cmd = cmd;
752 info.flags = 0;
753
754 /* Check if we have a pointer to user space data or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700755 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* No extra arguments. Trivial to handle */
758 ret = handler(dev, &info, &(iwr->u), NULL);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700761 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ((ret == 0) || (ret == -EIWCOMMIT)))
763 wireless_send_event(dev, cmd, &(iwr->u), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 } else {
765 char * extra;
766 int extra_size;
767 int user_length = 0;
768 int err;
John W. Linville98978ed2006-10-17 10:54:36 -0400769 int essid_compat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* Calculate space needed by arguments. Always allocate
772 * for max space. Easier, and won't last long... */
773 extra_size = descr->max_tokens * descr->token_size;
774
John W. Linville98978ed2006-10-17 10:54:36 -0400775 /* Check need for ESSID compatibility for WE < 21 */
776 switch (cmd) {
777 case SIOCSIWESSID:
778 case SIOCGIWESSID:
779 case SIOCSIWNICKN:
780 case SIOCGIWNICKN:
781 if (iwr->u.data.length == descr->max_tokens + 1)
782 essid_compat = 1;
783 else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
784 char essid[IW_ESSID_MAX_SIZE + 1];
785
786 err = copy_from_user(essid, iwr->u.data.pointer,
787 iwr->u.data.length *
788 descr->token_size);
789 if (err)
790 return -EFAULT;
791
792 if (essid[iwr->u.data.length - 1] == '\0')
793 essid_compat = 1;
794 }
795 break;
796 default:
797 break;
798 }
799
800 iwr->u.data.length -= essid_compat;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700803 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700805 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 (iwr->u.data.length != 0))
807 return -EFAULT;
808 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700809 if (iwr->u.data.length > descr->max_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700811 if (iwr->u.data.length < descr->min_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return -EINVAL;
813 } else {
814 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700815 if (iwr->u.data.pointer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EFAULT;
817 /* Save user space buffer size for checking */
818 user_length = iwr->u.data.length;
819
820 /* Don't check if user_length > max to allow forward
821 * compatibility. The test user_length < min is
822 * implied by the test at the end. */
823
824 /* Support for very large requests */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700825 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 (user_length > descr->max_tokens)) {
827 /* Allow userspace to GET more than max so
828 * we can support any size GET requests.
829 * There is still a limit : -ENOMEM. */
830 extra_size = user_length * descr->token_size;
831 /* Note : user_length is originally a __u16,
832 * and token_size is controlled by us,
833 * so extra_size won't get negative and
834 * won't overflow... */
835 }
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Create the kernel buffer */
John W. Linville98978ed2006-10-17 10:54:36 -0400839 /* kzalloc ensures NULL-termination for essid_compat */
840 extra = kzalloc(extra_size, GFP_KERNEL);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700841 if (extra == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700845 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 err = copy_from_user(extra, iwr->u.data.pointer,
847 iwr->u.data.length *
848 descr->token_size);
849 if (err) {
850 kfree(extra);
851 return -EFAULT;
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
855 /* Call the handler */
856 ret = handler(dev, &info, &(iwr->u), extra);
857
John W. Linville98978ed2006-10-17 10:54:36 -0400858 iwr->u.data.length += essid_compat;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* If we have something to return to the user */
861 if (!ret && IW_IS_GET(cmd)) {
862 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700863 if (user_length < iwr->u.data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 kfree(extra);
865 return -E2BIG;
866 }
867
868 err = copy_to_user(iwr->u.data.pointer, extra,
869 iwr->u.data.length *
870 descr->token_size);
871 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900872 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700876 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ((ret == 0) || (ret == -EIWCOMMIT))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700878 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* If the event is restricted, don't
880 * export the payload */
881 wireless_send_event(dev, cmd, &(iwr->u), NULL);
882 else
883 wireless_send_event(dev, cmd, &(iwr->u),
884 extra);
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* Cleanup - I told you it wasn't that long ;-) */
888 kfree(extra);
889 }
890
891 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700892 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 ret = call_commit_handler(dev);
894
895 /* Here, we will generate the appropriate event if needed */
896
897 return ret;
898}
899
900/* ---------------------------------------------------------------- */
901/*
902 * Wrapper to call a private Wireless Extension handler.
903 * We do various checks and also take care of moving data between
904 * user space and kernel space.
905 * It's not as nice and slimline as the standard wrapper. The cause
906 * is struct iw_priv_args, which was not really designed for the
907 * job we are going here.
908 *
909 * IMPORTANT : This function prevent to set and get data on the same
910 * IOCTL and enforce the SET/GET convention. Not doing it would be
911 * far too hairy...
912 * If you need to set and get data at the same time, please don't use
913 * a iw_handler but process it in your ioctl handler (i.e. use the
914 * old driver API).
915 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700916static int ioctl_private_call(struct net_device *dev, struct ifreq *ifr,
917 unsigned int cmd, iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct iwreq * iwr = (struct iwreq *) ifr;
920 const struct iw_priv_args * descr = NULL;
921 struct iw_request_info info;
922 int extra_size = 0;
923 int i;
924 int ret = -EINVAL;
925
926 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700927 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
928 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 descr = &(dev->wireless_handlers->private_args[i]);
930 break;
931 }
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Compute the size of the set/get arguments */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700934 if (descr != NULL) {
935 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int offset = 0; /* For sub-ioctls */
937 /* Check for sub-ioctl handler */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700938 if (descr->name[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* Reserve one int for sub-ioctl index */
940 offset = sizeof(__u32);
941
942 /* Size of set arguments */
943 extra_size = get_priv_size(descr->set_args);
944
945 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700946 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 ((extra_size + offset) <= IFNAMSIZ))
948 extra_size = 0;
949 } else {
950 /* Size of get arguments */
951 extra_size = get_priv_size(descr->get_args);
952
953 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700954 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 (extra_size <= IFNAMSIZ))
956 extra_size = 0;
957 }
958 }
959
960 /* Prepare the call */
961 info.cmd = cmd;
962 info.flags = 0;
963
964 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700965 if (extra_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* No extra arguments. Trivial to handle */
967 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
968 } else {
969 char * extra;
970 int err;
971
972 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700973 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700975 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 (iwr->u.data.length != 0))
977 return -EFAULT;
978
979 /* Does it fits within bounds ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700980 if (iwr->u.data.length > (descr->set_args &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 IW_PRIV_SIZE_MASK))
982 return -E2BIG;
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700983 } else if (iwr->u.data.pointer == NULL)
984 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* Always allocate for max space. Easier, and won't last
987 * long... */
988 extra = kmalloc(extra_size, GFP_KERNEL);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700989 if (extra == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700993 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 err = copy_from_user(extra, iwr->u.data.pointer,
995 extra_size);
996 if (err) {
997 kfree(extra);
998 return -EFAULT;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 /* Call the handler */
1003 ret = handler(dev, &info, &(iwr->u), extra);
1004
1005 /* If we have something to return to the user */
1006 if (!ret && IW_IS_GET(cmd)) {
1007
1008 /* Adjust for the actual length if it's variable,
1009 * avoid leaking kernel bits outside. */
1010 if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
1011 extra_size = adjust_priv_size(descr->get_args,
1012 &(iwr->u));
1013 }
1014
1015 err = copy_to_user(iwr->u.data.pointer, extra,
1016 extra_size);
1017 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001018 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
1021 /* Cleanup - I told you it wasn't that long ;-) */
1022 kfree(extra);
1023 }
1024
1025
1026 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001027 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = call_commit_handler(dev);
1029
1030 return ret;
1031}
1032
1033/* ---------------------------------------------------------------- */
1034/*
Johannes Berg295f4a12007-04-26 20:43:56 -07001035 * Main IOCTl dispatcher.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * Check the type of IOCTL and call the appropriate wrapper...
1037 */
Eric W. Biederman881d9662007-09-17 11:56:21 -07001038static int wireless_process_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct net_device *dev;
1041 iw_handler handler;
1042
1043 /* Permissions are already checked in dev_ioctl() before calling us.
1044 * The copy_to/from_user() of ifr is also dealt with in there */
1045
1046 /* Make sure the device exist */
Eric W. Biederman881d9662007-09-17 11:56:21 -07001047 if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -ENODEV;
1049
1050 /* A bunch of special cases, then the generic case...
1051 * Note that 'cmd' is already filtered in dev_ioctl() with
1052 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001053 if (cmd == SIOCGIWSTATS)
1054 return ioctl_standard_call(dev, ifr, cmd,
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001055 &iw_handler_get_iwstats);
1056
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001057 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
1058 return ioctl_standard_call(dev, ifr, cmd,
1059 &iw_handler_get_private);
1060
1061 /* Basic check */
1062 if (!netif_device_present(dev))
1063 return -ENODEV;
1064
1065 /* New driver API : try to find the handler */
1066 handler = get_handler(dev, cmd);
1067 if (handler) {
1068 /* Standard and private are not the same */
1069 if (cmd < SIOCIWFIRSTPRIV)
1070 return ioctl_standard_call(dev, ifr, cmd, handler);
1071 else
1072 return ioctl_private_call(dev, ifr, cmd, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001074 /* Old driver API : call driver ioctl handler */
1075 if (dev->do_ioctl)
1076 return dev->do_ioctl(dev, ifr, cmd);
1077 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Johannes Berg295f4a12007-04-26 20:43:56 -07001080/* entry point from dev ioctl */
Eric W. Biederman881d9662007-09-17 11:56:21 -07001081int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
Johannes Berg295f4a12007-04-26 20:43:56 -07001082 void __user *arg)
1083{
1084 int ret;
1085
1086 /* If command is `set a parameter', or
1087 * `get the encoding parameters', check if
1088 * the user has the right to do it */
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001089 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1090 && !capable(CAP_NET_ADMIN))
1091 return -EPERM;
1092
Eric W. Biederman881d9662007-09-17 11:56:21 -07001093 dev_load(net, ifr->ifr_name);
Johannes Berg295f4a12007-04-26 20:43:56 -07001094 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07001095 ret = wireless_process_ioctl(net, ifr, cmd);
Johannes Berg295f4a12007-04-26 20:43:56 -07001096 rtnl_unlock();
David S. Miller0a06ea82007-11-20 03:29:53 -08001097 if (IW_IS_GET(cmd) && copy_to_user(arg, ifr, sizeof(struct iwreq)))
Johannes Berg295f4a12007-04-26 20:43:56 -07001098 return -EFAULT;
1099 return ret;
1100}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/************************* EVENT PROCESSING *************************/
1103/*
1104 * Process events generated by the wireless layer or the driver.
1105 * Most often, the event will be propagated through rtnetlink
1106 */
1107
John W. Linvillebaef1862006-09-08 16:04:05 -04001108/* ---------------------------------------------------------------- */
1109/*
1110 * Locking...
1111 * ----------
1112 *
1113 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1114 * the locking issue in here and implementing this code !
1115 *
1116 * The issue : wireless_send_event() is often called in interrupt context,
1117 * while the Netlink layer can never be called in interrupt context.
1118 * The fully formed RtNetlink events are queued, and then a tasklet is run
1119 * to feed those to Netlink.
1120 * The skb_queue is interrupt safe, and its lock is not held while calling
1121 * Netlink, so there is no possibility of dealock.
1122 * Jean II
1123 */
1124
Herbert Xu782a6672006-08-03 23:54:41 +10001125static struct sk_buff_head wireless_nlevent_queue;
1126
John W. Linvillebaef1862006-09-08 16:04:05 -04001127static int __init wireless_nlevent_init(void)
1128{
1129 skb_queue_head_init(&wireless_nlevent_queue);
1130 return 0;
1131}
1132
1133subsys_initcall(wireless_nlevent_init);
1134
Herbert Xu782a6672006-08-03 23:54:41 +10001135static void wireless_nlevent_process(unsigned long data)
1136{
1137 struct sk_buff *skb;
1138
1139 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001140 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
Herbert Xu782a6672006-08-03 23:54:41 +10001141}
1142
1143static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/* ---------------------------------------------------------------- */
1146/*
1147 * Fill a rtnetlink message with our event data.
1148 * Note that we propage only the specified event and don't dump the
1149 * current wireless config. Dumping the wireless config is far too
1150 * expensive (for each parameter, the driver need to query the hardware).
1151 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001152static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
1153 int type, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct ifinfomsg *r;
1156 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Thomas Graf744b0962007-07-22 12:43:32 +02001158 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
1159 if (nlh == NULL)
1160 return -EMSGSIZE;
1161
1162 r = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 r->ifi_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001164 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 r->ifi_type = dev->type;
1166 r->ifi_index = dev->ifindex;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001167 r->ifi_flags = dev_get_flags(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1169
1170 /* Add the wireless events in the netlink packet */
Thomas Graf744b0962007-07-22 12:43:32 +02001171 NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Thomas Graf744b0962007-07-22 12:43:32 +02001173 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Thomas Graf744b0962007-07-22 12:43:32 +02001175nla_put_failure:
1176 nlmsg_cancel(skb, nlh);
1177 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180/* ---------------------------------------------------------------- */
1181/*
1182 * Create and broadcast and send it on the standard rtnetlink socket
1183 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1184 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1185 * within a RTM_NEWLINK event.
1186 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001187static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct sk_buff *skb;
Thomas Graf744b0962007-07-22 12:43:32 +02001190 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001192 if (dev->nd_net != &init_net)
1193 return;
1194
Thomas Graf744b0962007-07-22 12:43:32 +02001195 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (!skb)
1197 return;
1198
Thomas Graf744b0962007-07-22 12:43:32 +02001199 err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
1200 if (err < 0) {
1201 WARN_ON(err == -EMSGSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 kfree_skb(skb);
1203 return;
1204 }
Thomas Graf744b0962007-07-22 12:43:32 +02001205
Patrick McHardyac6d4392005-08-14 19:29:52 -07001206 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
Herbert Xu782a6672006-08-03 23:54:41 +10001207 skb_queue_tail(&wireless_nlevent_queue, skb);
1208 tasklet_schedule(&wireless_nlevent_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
Herbert Xu782a6672006-08-03 23:54:41 +10001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/* ---------------------------------------------------------------- */
1212/*
1213 * Main event dispatcher. Called from other parts and drivers.
1214 * Send the event on the appropriate channels.
1215 * May be called from interrupt context.
1216 */
1217void wireless_send_event(struct net_device * dev,
1218 unsigned int cmd,
1219 union iwreq_data * wrqu,
1220 char * extra)
1221{
1222 const struct iw_ioctl_description * descr = NULL;
1223 int extra_len = 0;
1224 struct iw_event *event; /* Mallocated whole event */
1225 int event_len; /* Its size */
1226 int hdr_len; /* Size of the event header */
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001227 int wrqu_off = 0; /* Offset in wrqu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* Don't "optimise" the following variable, it will crash */
1229 unsigned cmd_index; /* *MUST* be unsigned */
1230
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001231 /* Get the description of the Event */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001232 if (cmd <= SIOCIWLAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 cmd_index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001234 if (cmd_index < standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 descr = &(standard_ioctl[cmd_index]);
1236 } else {
1237 cmd_index = cmd - IWEVFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001238 if (cmd_index < standard_event_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 descr = &(standard_event[cmd_index]);
1240 }
1241 /* Don't accept unknown events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001242 if (descr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Note : we don't return an error to the driver, because
1244 * the driver would not know what to do about it. It can't
1245 * return an error to the user, because the event is not
1246 * initiated by a user request.
1247 * The best the driver could do is to log an error message.
1248 * We will do it ourselves instead...
1249 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001250 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 dev->name, cmd);
1252 return;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /* Check extra parameters and set extra_len */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001256 if (descr->header_type == IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001258 if (wrqu->data.length > descr->max_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001259 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return;
1261 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001262 if (wrqu->data.length < descr->min_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001263 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return;
1265 }
1266 /* Calculate extra_len - extra is NULL for restricted events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001267 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 extra_len = wrqu->data.length * descr->token_size;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001269 /* Always at an offset in wrqu */
1270 wrqu_off = IW_EV_POINT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 /* Total length of the event */
1274 hdr_len = event_type_size[descr->header_type];
1275 event_len = hdr_len + extra_len;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* Create temporary buffer to hold the event */
1278 event = kmalloc(event_len, GFP_ATOMIC);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001279 if (event == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281
1282 /* Fill event */
1283 event->len = event_len;
1284 event->cmd = cmd;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001285 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001286 if (extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 memcpy(((char *) event) + hdr_len, extra, extra_len);
1288
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001289 /* Send via the RtNetlink event channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 rtmsg_iwinfo(dev, (char *) event, event_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /* Cleanup */
1293 kfree(event);
1294
1295 return; /* Always success, I guess ;-) */
1296}
Johannes Berg7a9df162007-04-26 20:46:23 -07001297EXPORT_SYMBOL(wireless_send_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299/********************** ENHANCED IWSPY SUPPORT **********************/
1300/*
1301 * In the old days, the driver was handling spy support all by itself.
1302 * Now, the driver can delegate this task to Wireless Extensions.
1303 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1304 * push data to us via wireless_spy_update() and include struct iw_spy_data
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001305 * in its private part (and export it in net_device->wireless_data->spy_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * One of the main advantage of centralising spy support here is that
1307 * it becomes much easier to improve and extend it without having to touch
1308 * the drivers. One example is the addition of the Spy-Threshold events.
1309 */
1310
1311/* ---------------------------------------------------------------- */
1312/*
1313 * Return the pointer to the spy data in the driver.
1314 * Because this is called on the Rx path via wireless_spy_update(),
1315 * we want it to be efficient...
1316 */
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001317static inline struct iw_spy_data *get_spydata(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 /* This is the new way */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001320 if (dev->wireless_data)
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001321 return dev->wireless_data->spy_data;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001322 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325/*------------------------------------------------------------------*/
1326/*
1327 * Standard Wireless Handler : set Spy List
1328 */
1329int iw_handler_set_spy(struct net_device * dev,
1330 struct iw_request_info * info,
1331 union iwreq_data * wrqu,
1332 char * extra)
1333{
1334 struct iw_spy_data * spydata = get_spydata(dev);
1335 struct sockaddr * address = (struct sockaddr *) extra;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001338 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return -EOPNOTSUPP;
1340
1341 /* Disable spy collection while we copy the addresses.
1342 * While we copy addresses, any call to wireless_spy_update()
1343 * will NOP. This is OK, as anyway the addresses are changing. */
1344 spydata->spy_number = 0;
1345
1346 /* We want to operate without locking, because wireless_spy_update()
1347 * most likely will happen in the interrupt handler, and therefore
1348 * have its own locking constraints and needs performance.
1349 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1350 * This make sure wireless_spy_update() "see" that the spy list
1351 * is temporarily disabled. */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001352 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* Are there are addresses to copy? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001355 if (wrqu->data.length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int i;
1357
1358 /* Copy addresses */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001359 for (i = 0; i < wrqu->data.length; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 memcpy(spydata->spy_address[i], address[i].sa_data,
1361 ETH_ALEN);
1362 /* Reset stats */
1363 memset(spydata->spy_stat, 0,
1364 sizeof(struct iw_quality) * IW_MAX_SPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
1367 /* Make sure above is updated before re-enabling */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001368 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Enable addresses */
1371 spydata->spy_number = wrqu->data.length;
1372
1373 return 0;
1374}
Johannes Berg7a9df162007-04-26 20:46:23 -07001375EXPORT_SYMBOL(iw_handler_set_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377/*------------------------------------------------------------------*/
1378/*
1379 * Standard Wireless Handler : get Spy List
1380 */
1381int iw_handler_get_spy(struct net_device * dev,
1382 struct iw_request_info * info,
1383 union iwreq_data * wrqu,
1384 char * extra)
1385{
1386 struct iw_spy_data * spydata = get_spydata(dev);
1387 struct sockaddr * address = (struct sockaddr *) extra;
1388 int i;
1389
1390 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001391 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EOPNOTSUPP;
1393
1394 wrqu->data.length = spydata->spy_number;
1395
1396 /* Copy addresses. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001397 for (i = 0; i < spydata->spy_number; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1399 address[i].sa_family = AF_UNIX;
1400 }
1401 /* Copy stats to the user buffer (just after). */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001402 if (spydata->spy_number > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1404 spydata->spy_stat,
1405 sizeof(struct iw_quality) * spydata->spy_number);
1406 /* Reset updated flags. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001407 for (i = 0; i < spydata->spy_number; i++)
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001408 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
1410}
Johannes Berg7a9df162007-04-26 20:46:23 -07001411EXPORT_SYMBOL(iw_handler_get_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413/*------------------------------------------------------------------*/
1414/*
1415 * Standard Wireless Handler : set spy threshold
1416 */
1417int iw_handler_set_thrspy(struct net_device * dev,
1418 struct iw_request_info *info,
1419 union iwreq_data * wrqu,
1420 char * extra)
1421{
1422 struct iw_spy_data * spydata = get_spydata(dev);
1423 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1424
1425 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001426 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EOPNOTSUPP;
1428
1429 /* Just do it */
1430 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1431 2 * sizeof(struct iw_quality));
1432
1433 /* Clear flag */
1434 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return 0;
1437}
Johannes Berg7a9df162007-04-26 20:46:23 -07001438EXPORT_SYMBOL(iw_handler_set_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/*------------------------------------------------------------------*/
1441/*
1442 * Standard Wireless Handler : get spy threshold
1443 */
1444int iw_handler_get_thrspy(struct net_device * dev,
1445 struct iw_request_info *info,
1446 union iwreq_data * wrqu,
1447 char * extra)
1448{
1449 struct iw_spy_data * spydata = get_spydata(dev);
1450 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1451
1452 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001453 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EOPNOTSUPP;
1455
1456 /* Just do it */
1457 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1458 2 * sizeof(struct iw_quality));
1459
1460 return 0;
1461}
Johannes Berg7a9df162007-04-26 20:46:23 -07001462EXPORT_SYMBOL(iw_handler_get_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464/*------------------------------------------------------------------*/
1465/*
1466 * Prepare and send a Spy Threshold event
1467 */
1468static void iw_send_thrspy_event(struct net_device * dev,
1469 struct iw_spy_data * spydata,
1470 unsigned char * address,
1471 struct iw_quality * wstats)
1472{
1473 union iwreq_data wrqu;
1474 struct iw_thrspy threshold;
1475
1476 /* Init */
1477 wrqu.data.length = 1;
1478 wrqu.data.flags = 0;
1479 /* Copy address */
1480 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1481 threshold.addr.sa_family = ARPHRD_ETHER;
1482 /* Copy stats */
1483 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1484 /* Copy also thresholds */
1485 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1486 2 * sizeof(struct iw_quality));
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* Send event to user space */
1489 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1490}
1491
1492/* ---------------------------------------------------------------- */
1493/*
1494 * Call for the driver to update the spy data.
1495 * For now, the spy data is a simple array. As the size of the array is
1496 * small, this is good enough. If we wanted to support larger number of
1497 * spy addresses, we should use something more efficient...
1498 */
1499void wireless_spy_update(struct net_device * dev,
1500 unsigned char * address,
1501 struct iw_quality * wstats)
1502{
1503 struct iw_spy_data * spydata = get_spydata(dev);
1504 int i;
1505 int match = -1;
1506
1507 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001508 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return;
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Update all records that match */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001512 for (i = 0; i < spydata->spy_number; i++)
1513 if (!compare_ether_addr(address, spydata->spy_address[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 memcpy(&(spydata->spy_stat[i]), wstats,
1515 sizeof(struct iw_quality));
1516 match = i;
1517 }
1518
1519 /* Generate an event if we cross the spy threshold.
1520 * To avoid event storms, we have a simple hysteresis : we generate
1521 * event only when we go under the low threshold or above the
1522 * high threshold. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001523 if (match >= 0) {
1524 if (spydata->spy_thr_under[match]) {
1525 if (wstats->level > spydata->spy_thr_high.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 spydata->spy_thr_under[match] = 0;
1527 iw_send_thrspy_event(dev, spydata,
1528 address, wstats);
1529 }
1530 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001531 if (wstats->level < spydata->spy_thr_low.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spydata->spy_thr_under[match] = 1;
1533 iw_send_thrspy_event(dev, spydata,
1534 address, wstats);
1535 }
1536 }
1537 }
1538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539EXPORT_SYMBOL(wireless_spy_update);