blob: f733303f4bde3a3b47c850780f4aeb765fe5410e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <linux/wireless.h> /* Pretty obvious */
98#include <net/iw_handler.h> /* New driver API */
Thomas Grafbd5785b2006-08-15 00:36:49 -070099#include <net/netlink.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700100#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#include <asm/uaccess.h> /* copy_to_user() */
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/************************* GLOBAL VARIABLES *************************/
105/*
106 * You should not use global variables, because of re-entrancy.
107 * On our case, it's only const, so it's OK...
108 */
109/*
110 * Meta-data about all the standard Wireless Extension request we
111 * know about.
112 */
113static const struct iw_ioctl_description standard_ioctl[] = {
114 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
115 .header_type = IW_HEADER_TYPE_NULL,
116 },
117 [SIOCGIWNAME - SIOCIWFIRST] = {
118 .header_type = IW_HEADER_TYPE_CHAR,
119 .flags = IW_DESCR_FLAG_DUMP,
120 },
121 [SIOCSIWNWID - SIOCIWFIRST] = {
122 .header_type = IW_HEADER_TYPE_PARAM,
123 .flags = IW_DESCR_FLAG_EVENT,
124 },
125 [SIOCGIWNWID - SIOCIWFIRST] = {
126 .header_type = IW_HEADER_TYPE_PARAM,
127 .flags = IW_DESCR_FLAG_DUMP,
128 },
129 [SIOCSIWFREQ - SIOCIWFIRST] = {
130 .header_type = IW_HEADER_TYPE_FREQ,
131 .flags = IW_DESCR_FLAG_EVENT,
132 },
133 [SIOCGIWFREQ - SIOCIWFIRST] = {
134 .header_type = IW_HEADER_TYPE_FREQ,
135 .flags = IW_DESCR_FLAG_DUMP,
136 },
137 [SIOCSIWMODE - SIOCIWFIRST] = {
138 .header_type = IW_HEADER_TYPE_UINT,
139 .flags = IW_DESCR_FLAG_EVENT,
140 },
141 [SIOCGIWMODE - SIOCIWFIRST] = {
142 .header_type = IW_HEADER_TYPE_UINT,
143 .flags = IW_DESCR_FLAG_DUMP,
144 },
145 [SIOCSIWSENS - SIOCIWFIRST] = {
146 .header_type = IW_HEADER_TYPE_PARAM,
147 },
148 [SIOCGIWSENS - SIOCIWFIRST] = {
149 .header_type = IW_HEADER_TYPE_PARAM,
150 },
151 [SIOCSIWRANGE - SIOCIWFIRST] = {
152 .header_type = IW_HEADER_TYPE_NULL,
153 },
154 [SIOCGIWRANGE - SIOCIWFIRST] = {
155 .header_type = IW_HEADER_TYPE_POINT,
156 .token_size = 1,
157 .max_tokens = sizeof(struct iw_range),
158 .flags = IW_DESCR_FLAG_DUMP,
159 },
160 [SIOCSIWPRIV - SIOCIWFIRST] = {
161 .header_type = IW_HEADER_TYPE_NULL,
162 },
163 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800164 .header_type = IW_HEADER_TYPE_POINT,
165 .token_size = sizeof(struct iw_priv_args),
166 .max_tokens = 16,
167 .flags = IW_DESCR_FLAG_NOMAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 },
169 [SIOCSIWSTATS - SIOCIWFIRST] = {
170 .header_type = IW_HEADER_TYPE_NULL,
171 },
172 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800173 .header_type = IW_HEADER_TYPE_POINT,
174 .token_size = 1,
175 .max_tokens = sizeof(struct iw_statistics),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .flags = IW_DESCR_FLAG_DUMP,
177 },
178 [SIOCSIWSPY - SIOCIWFIRST] = {
179 .header_type = IW_HEADER_TYPE_POINT,
180 .token_size = sizeof(struct sockaddr),
181 .max_tokens = IW_MAX_SPY,
182 },
183 [SIOCGIWSPY - SIOCIWFIRST] = {
184 .header_type = IW_HEADER_TYPE_POINT,
185 .token_size = sizeof(struct sockaddr) +
186 sizeof(struct iw_quality),
187 .max_tokens = IW_MAX_SPY,
188 },
189 [SIOCSIWTHRSPY - SIOCIWFIRST] = {
190 .header_type = IW_HEADER_TYPE_POINT,
191 .token_size = sizeof(struct iw_thrspy),
192 .min_tokens = 1,
193 .max_tokens = 1,
194 },
195 [SIOCGIWTHRSPY - SIOCIWFIRST] = {
196 .header_type = IW_HEADER_TYPE_POINT,
197 .token_size = sizeof(struct iw_thrspy),
198 .min_tokens = 1,
199 .max_tokens = 1,
200 },
201 [SIOCSIWAP - SIOCIWFIRST] = {
202 .header_type = IW_HEADER_TYPE_ADDR,
203 },
204 [SIOCGIWAP - SIOCIWFIRST] = {
205 .header_type = IW_HEADER_TYPE_ADDR,
206 .flags = IW_DESCR_FLAG_DUMP,
207 },
fff9cfd2005-05-12 20:24:19 -0400208 [SIOCSIWMLME - SIOCIWFIRST] = {
209 .header_type = IW_HEADER_TYPE_POINT,
210 .token_size = 1,
211 .min_tokens = sizeof(struct iw_mlme),
212 .max_tokens = sizeof(struct iw_mlme),
213 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 [SIOCGIWAPLIST - SIOCIWFIRST] = {
215 .header_type = IW_HEADER_TYPE_POINT,
216 .token_size = sizeof(struct sockaddr) +
217 sizeof(struct iw_quality),
218 .max_tokens = IW_MAX_AP,
219 .flags = IW_DESCR_FLAG_NOMAX,
220 },
221 [SIOCSIWSCAN - SIOCIWFIRST] = {
fff9cfd2005-05-12 20:24:19 -0400222 .header_type = IW_HEADER_TYPE_POINT,
223 .token_size = 1,
224 .min_tokens = 0,
225 .max_tokens = sizeof(struct iw_scan_req),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 },
227 [SIOCGIWSCAN - SIOCIWFIRST] = {
228 .header_type = IW_HEADER_TYPE_POINT,
229 .token_size = 1,
230 .max_tokens = IW_SCAN_MAX_DATA,
231 .flags = IW_DESCR_FLAG_NOMAX,
232 },
233 [SIOCSIWESSID - SIOCIWFIRST] = {
234 .header_type = IW_HEADER_TYPE_POINT,
235 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400236 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .flags = IW_DESCR_FLAG_EVENT,
238 },
239 [SIOCGIWESSID - SIOCIWFIRST] = {
240 .header_type = IW_HEADER_TYPE_POINT,
241 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400242 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .flags = IW_DESCR_FLAG_DUMP,
244 },
245 [SIOCSIWNICKN - SIOCIWFIRST] = {
246 .header_type = IW_HEADER_TYPE_POINT,
247 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400248 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 },
250 [SIOCGIWNICKN - SIOCIWFIRST] = {
251 .header_type = IW_HEADER_TYPE_POINT,
252 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400253 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 },
255 [SIOCSIWRATE - SIOCIWFIRST] = {
256 .header_type = IW_HEADER_TYPE_PARAM,
257 },
258 [SIOCGIWRATE - SIOCIWFIRST] = {
259 .header_type = IW_HEADER_TYPE_PARAM,
260 },
261 [SIOCSIWRTS - SIOCIWFIRST] = {
262 .header_type = IW_HEADER_TYPE_PARAM,
263 },
264 [SIOCGIWRTS - SIOCIWFIRST] = {
265 .header_type = IW_HEADER_TYPE_PARAM,
266 },
267 [SIOCSIWFRAG - SIOCIWFIRST] = {
268 .header_type = IW_HEADER_TYPE_PARAM,
269 },
270 [SIOCGIWFRAG - SIOCIWFIRST] = {
271 .header_type = IW_HEADER_TYPE_PARAM,
272 },
273 [SIOCSIWTXPOW - SIOCIWFIRST] = {
274 .header_type = IW_HEADER_TYPE_PARAM,
275 },
276 [SIOCGIWTXPOW - SIOCIWFIRST] = {
277 .header_type = IW_HEADER_TYPE_PARAM,
278 },
279 [SIOCSIWRETRY - SIOCIWFIRST] = {
280 .header_type = IW_HEADER_TYPE_PARAM,
281 },
282 [SIOCGIWRETRY - SIOCIWFIRST] = {
283 .header_type = IW_HEADER_TYPE_PARAM,
284 },
285 [SIOCSIWENCODE - SIOCIWFIRST] = {
286 .header_type = IW_HEADER_TYPE_POINT,
287 .token_size = 1,
288 .max_tokens = IW_ENCODING_TOKEN_MAX,
289 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
290 },
291 [SIOCGIWENCODE - SIOCIWFIRST] = {
292 .header_type = IW_HEADER_TYPE_POINT,
293 .token_size = 1,
294 .max_tokens = IW_ENCODING_TOKEN_MAX,
295 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
296 },
297 [SIOCSIWPOWER - SIOCIWFIRST] = {
298 .header_type = IW_HEADER_TYPE_PARAM,
299 },
300 [SIOCGIWPOWER - SIOCIWFIRST] = {
301 .header_type = IW_HEADER_TYPE_PARAM,
302 },
fff9cfd2005-05-12 20:24:19 -0400303 [SIOCSIWGENIE - SIOCIWFIRST] = {
304 .header_type = IW_HEADER_TYPE_POINT,
305 .token_size = 1,
306 .max_tokens = IW_GENERIC_IE_MAX,
307 },
308 [SIOCGIWGENIE - SIOCIWFIRST] = {
309 .header_type = IW_HEADER_TYPE_POINT,
310 .token_size = 1,
311 .max_tokens = IW_GENERIC_IE_MAX,
312 },
313 [SIOCSIWAUTH - SIOCIWFIRST] = {
314 .header_type = IW_HEADER_TYPE_PARAM,
315 },
316 [SIOCGIWAUTH - SIOCIWFIRST] = {
317 .header_type = IW_HEADER_TYPE_PARAM,
318 },
319 [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
320 .header_type = IW_HEADER_TYPE_POINT,
321 .token_size = 1,
322 .min_tokens = sizeof(struct iw_encode_ext),
323 .max_tokens = sizeof(struct iw_encode_ext) +
324 IW_ENCODING_TOKEN_MAX,
325 },
326 [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
327 .header_type = IW_HEADER_TYPE_POINT,
328 .token_size = 1,
329 .min_tokens = sizeof(struct iw_encode_ext),
330 .max_tokens = sizeof(struct iw_encode_ext) +
331 IW_ENCODING_TOKEN_MAX,
332 },
333 [SIOCSIWPMKSA - SIOCIWFIRST] = {
334 .header_type = IW_HEADER_TYPE_POINT,
335 .token_size = 1,
336 .min_tokens = sizeof(struct iw_pmksa),
337 .max_tokens = sizeof(struct iw_pmksa),
338 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800340static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/*
343 * Meta-data about all the additional standard Wireless Extension events
344 * we know about.
345 */
346static const struct iw_ioctl_description standard_event[] = {
347 [IWEVTXDROP - IWEVFIRST] = {
348 .header_type = IW_HEADER_TYPE_ADDR,
349 },
350 [IWEVQUAL - IWEVFIRST] = {
351 .header_type = IW_HEADER_TYPE_QUAL,
352 },
353 [IWEVCUSTOM - IWEVFIRST] = {
354 .header_type = IW_HEADER_TYPE_POINT,
355 .token_size = 1,
356 .max_tokens = IW_CUSTOM_MAX,
357 },
358 [IWEVREGISTERED - IWEVFIRST] = {
359 .header_type = IW_HEADER_TYPE_ADDR,
360 },
361 [IWEVEXPIRED - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900362 .header_type = IW_HEADER_TYPE_ADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 },
fff9cfd2005-05-12 20:24:19 -0400364 [IWEVGENIE - IWEVFIRST] = {
365 .header_type = IW_HEADER_TYPE_POINT,
366 .token_size = 1,
367 .max_tokens = IW_GENERIC_IE_MAX,
368 },
369 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900370 .header_type = IW_HEADER_TYPE_POINT,
fff9cfd2005-05-12 20:24:19 -0400371 .token_size = 1,
372 .max_tokens = sizeof(struct iw_michaelmicfailure),
373 },
374 [IWEVASSOCREQIE - IWEVFIRST] = {
375 .header_type = IW_HEADER_TYPE_POINT,
376 .token_size = 1,
377 .max_tokens = IW_GENERIC_IE_MAX,
378 },
379 [IWEVASSOCRESPIE - IWEVFIRST] = {
380 .header_type = IW_HEADER_TYPE_POINT,
381 .token_size = 1,
382 .max_tokens = IW_GENERIC_IE_MAX,
383 },
384 [IWEVPMKIDCAND - IWEVFIRST] = {
385 .header_type = IW_HEADER_TYPE_POINT,
386 .token_size = 1,
387 .max_tokens = sizeof(struct iw_pmkid_cand),
388 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800390static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392/* Size (in bytes) of the various private data types */
393static const char iw_priv_type_size[] = {
394 0, /* IW_PRIV_TYPE_NONE */
395 1, /* IW_PRIV_TYPE_BYTE */
396 1, /* IW_PRIV_TYPE_CHAR */
397 0, /* Not defined */
398 sizeof(__u32), /* IW_PRIV_TYPE_INT */
399 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
400 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
401 0, /* Not defined */
402};
403
404/* Size (in bytes) of various events */
405static const int event_type_size[] = {
406 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
407 0,
408 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
409 0,
410 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
411 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
412 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
413 0,
414 IW_EV_POINT_LEN, /* Without variable payload */
415 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
416 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
417};
418
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +0000419/* Size (in bytes) of various events, as packed */
420static const int event_type_pk_size[] = {
421 IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
422 0,
423 IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
424 0,
425 IW_EV_UINT_PK_LEN, /* IW_HEADER_TYPE_UINT */
426 IW_EV_FREQ_PK_LEN, /* IW_HEADER_TYPE_FREQ */
427 IW_EV_ADDR_PK_LEN, /* IW_HEADER_TYPE_ADDR */
428 0,
429 IW_EV_POINT_PK_LEN, /* Without variable payload */
430 IW_EV_PARAM_PK_LEN, /* IW_HEADER_TYPE_PARAM */
431 IW_EV_QUAL_PK_LEN, /* IW_HEADER_TYPE_QUAL */
432};
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/************************ COMMON SUBROUTINES ************************/
435/*
436 * Stuff that may be used in various place or doesn't fit in one
437 * of the section below.
438 */
439
440/* ---------------------------------------------------------------- */
441/*
442 * Return the driver handler associated with a specific Wireless Extension.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700444static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 /* Don't "optimise" the following variable, it will crash */
447 unsigned int index; /* *MUST* be unsigned */
448
449 /* Check if we have some wireless handlers defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700450 if (dev->wireless_handlers == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return NULL;
452
453 /* Try as a standard command */
454 index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700455 if (index < dev->wireless_handlers->num_standard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return dev->wireless_handlers->standard[index];
457
458 /* Try as a private command */
459 index = cmd - SIOCIWFIRSTPRIV;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700460 if (index < dev->wireless_handlers->num_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return dev->wireless_handlers->private[index];
462
463 /* Not found */
464 return NULL;
465}
466
467/* ---------------------------------------------------------------- */
468/*
469 * Get statistics out of the driver
470 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700471static struct iw_statistics *get_wireless_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 /* New location */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700474 if ((dev->wireless_handlers != NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 (dev->wireless_handlers->get_wireless_stats != NULL))
476 return dev->wireless_handlers->get_wireless_stats(dev);
477
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700478 /* Not found */
479 return (struct iw_statistics *) NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/* ---------------------------------------------------------------- */
483/*
484 * Call the commit handler in the driver
485 * (if exist and if conditions are right)
486 *
487 * Note : our current commit strategy is currently pretty dumb,
488 * but we will be able to improve on that...
489 * The goal is to try to agreagate as many changes as possible
490 * before doing the commit. Drivers that will define a commit handler
491 * are usually those that need a reset after changing parameters, so
492 * we want to minimise the number of reset.
493 * A cool idea is to use a timer : at each "set" command, we re-set the
494 * timer, when the timer eventually fires, we call the driver.
495 * Hopefully, more on that later.
496 *
497 * Also, I'm waiting to see how many people will complain about the
498 * netif_running(dev) test. I'm open on that one...
499 * Hopefully, the driver will remember to do a commit in "open()" ;-)
500 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700501static int call_commit_handler(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700503 if ((netif_running(dev)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 (dev->wireless_handlers->standard[0] != NULL)) {
505 /* Call the commit handler on the driver */
506 return dev->wireless_handlers->standard[0](dev, NULL,
507 NULL, NULL);
508 } else
509 return 0; /* Command completed successfully */
510}
511
512/* ---------------------------------------------------------------- */
513/*
514 * Calculate size of private arguments
515 */
516static inline int get_priv_size(__u16 args)
517{
518 int num = args & IW_PRIV_SIZE_MASK;
519 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
520
521 return num * iw_priv_type_size[type];
522}
523
524/* ---------------------------------------------------------------- */
525/*
526 * Re-calculate the size of private arguments
527 */
528static inline int adjust_priv_size(__u16 args,
529 union iwreq_data * wrqu)
530{
531 int num = wrqu->data.length;
532 int max = args & IW_PRIV_SIZE_MASK;
533 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
534
535 /* Make sure the driver doesn't goof up */
536 if (max < num)
537 num = max;
538
539 return num * iw_priv_type_size[type];
540}
541
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800542/* ---------------------------------------------------------------- */
543/*
544 * Standard Wireless Handler : get wireless stats
545 * Allow programatic access to /proc/net/wireless even if /proc
546 * doesn't exist... Also more efficient...
547 */
548static int iw_handler_get_iwstats(struct net_device * dev,
549 struct iw_request_info * info,
550 union iwreq_data * wrqu,
551 char * extra)
552{
553 /* Get stats from the driver */
554 struct iw_statistics *stats;
555
556 stats = get_wireless_stats(dev);
557 if (stats != (struct iw_statistics *) NULL) {
558
559 /* 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{
676 return seq_open(file, &wireless_seq_ops);
677}
678
Arjan van de Ven9a321442007-02-12 00:55:35 -0800679static const struct file_operations wireless_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 .owner = THIS_MODULE,
681 .open = wireless_seq_open,
682 .read = seq_read,
683 .llseek = seq_lseek,
684 .release = seq_release,
685};
686
Johannes Berg295f4a12007-04-26 20:43:56 -0700687int __init wext_proc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700689 /* Create /proc/net/wireless entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
691 return -ENOMEM;
692
693 return 0;
694}
695#endif /* CONFIG_PROC_FS */
696
697/************************** IOCTL SUPPORT **************************/
698/*
699 * The original user space API to configure all those Wireless Extensions
700 * is through IOCTLs.
701 * In there, we check if we need to call the new driver API (iw_handler)
702 * or just call the driver ioctl handler.
703 */
704
705/* ---------------------------------------------------------------- */
706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Wrapper to call a standard Wireless Extension handler.
708 * We do various checks and also take care of moving data between
709 * user space and kernel space.
710 */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800711static int ioctl_standard_call(struct net_device * dev,
712 struct ifreq * ifr,
713 unsigned int cmd,
714 iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct iwreq * iwr = (struct iwreq *) ifr;
717 const struct iw_ioctl_description * descr;
718 struct iw_request_info info;
719 int ret = -EINVAL;
720
721 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700722 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EOPNOTSUPP;
724 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Prepare the call */
727 info.cmd = cmd;
728 info.flags = 0;
729
730 /* Check if we have a pointer to user space data or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700731 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* No extra arguments. Trivial to handle */
734 ret = handler(dev, &info, &(iwr->u), NULL);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700737 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ((ret == 0) || (ret == -EIWCOMMIT)))
739 wireless_send_event(dev, cmd, &(iwr->u), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
741 char * extra;
742 int extra_size;
743 int user_length = 0;
744 int err;
John W. Linville98978ed2006-10-17 10:54:36 -0400745 int essid_compat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Calculate space needed by arguments. Always allocate
748 * for max space. Easier, and won't last long... */
749 extra_size = descr->max_tokens * descr->token_size;
750
John W. Linville98978ed2006-10-17 10:54:36 -0400751 /* Check need for ESSID compatibility for WE < 21 */
752 switch (cmd) {
753 case SIOCSIWESSID:
754 case SIOCGIWESSID:
755 case SIOCSIWNICKN:
756 case SIOCGIWNICKN:
757 if (iwr->u.data.length == descr->max_tokens + 1)
758 essid_compat = 1;
759 else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
760 char essid[IW_ESSID_MAX_SIZE + 1];
761
762 err = copy_from_user(essid, iwr->u.data.pointer,
763 iwr->u.data.length *
764 descr->token_size);
765 if (err)
766 return -EFAULT;
767
768 if (essid[iwr->u.data.length - 1] == '\0')
769 essid_compat = 1;
770 }
771 break;
772 default:
773 break;
774 }
775
776 iwr->u.data.length -= essid_compat;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700779 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700781 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 (iwr->u.data.length != 0))
783 return -EFAULT;
784 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700785 if (iwr->u.data.length > descr->max_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700787 if (iwr->u.data.length < descr->min_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EINVAL;
789 } else {
790 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700791 if (iwr->u.data.pointer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EFAULT;
793 /* Save user space buffer size for checking */
794 user_length = iwr->u.data.length;
795
796 /* Don't check if user_length > max to allow forward
797 * compatibility. The test user_length < min is
798 * implied by the test at the end. */
799
800 /* Support for very large requests */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700801 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 (user_length > descr->max_tokens)) {
803 /* Allow userspace to GET more than max so
804 * we can support any size GET requests.
805 * There is still a limit : -ENOMEM. */
806 extra_size = user_length * descr->token_size;
807 /* Note : user_length is originally a __u16,
808 * and token_size is controlled by us,
809 * so extra_size won't get negative and
810 * won't overflow... */
811 }
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Create the kernel buffer */
John W. Linville98978ed2006-10-17 10:54:36 -0400815 /* kzalloc ensures NULL-termination for essid_compat */
816 extra = kzalloc(extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (extra == NULL) {
818 return -ENOMEM;
819 }
820
821 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700822 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 err = copy_from_user(extra, iwr->u.data.pointer,
824 iwr->u.data.length *
825 descr->token_size);
826 if (err) {
827 kfree(extra);
828 return -EFAULT;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 /* Call the handler */
833 ret = handler(dev, &info, &(iwr->u), extra);
834
John W. Linville98978ed2006-10-17 10:54:36 -0400835 iwr->u.data.length += essid_compat;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* If we have something to return to the user */
838 if (!ret && IW_IS_GET(cmd)) {
839 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700840 if (user_length < iwr->u.data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kfree(extra);
842 return -E2BIG;
843 }
844
845 err = copy_to_user(iwr->u.data.pointer, extra,
846 iwr->u.data.length *
847 descr->token_size);
848 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900849 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700853 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ((ret == 0) || (ret == -EIWCOMMIT))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700855 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* If the event is restricted, don't
857 * export the payload */
858 wireless_send_event(dev, cmd, &(iwr->u), NULL);
859 else
860 wireless_send_event(dev, cmd, &(iwr->u),
861 extra);
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Cleanup - I told you it wasn't that long ;-) */
865 kfree(extra);
866 }
867
868 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700869 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ret = call_commit_handler(dev);
871
872 /* Here, we will generate the appropriate event if needed */
873
874 return ret;
875}
876
877/* ---------------------------------------------------------------- */
878/*
879 * Wrapper to call a private Wireless Extension handler.
880 * We do various checks and also take care of moving data between
881 * user space and kernel space.
882 * It's not as nice and slimline as the standard wrapper. The cause
883 * is struct iw_priv_args, which was not really designed for the
884 * job we are going here.
885 *
886 * IMPORTANT : This function prevent to set and get data on the same
887 * IOCTL and enforce the SET/GET convention. Not doing it would be
888 * far too hairy...
889 * If you need to set and get data at the same time, please don't use
890 * a iw_handler but process it in your ioctl handler (i.e. use the
891 * old driver API).
892 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700893static int ioctl_private_call(struct net_device *dev, struct ifreq *ifr,
894 unsigned int cmd, iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct iwreq * iwr = (struct iwreq *) ifr;
897 const struct iw_priv_args * descr = NULL;
898 struct iw_request_info info;
899 int extra_size = 0;
900 int i;
901 int ret = -EINVAL;
902
903 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700904 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
905 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 descr = &(dev->wireless_handlers->private_args[i]);
907 break;
908 }
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Compute the size of the set/get arguments */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700911 if (descr != NULL) {
912 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int offset = 0; /* For sub-ioctls */
914 /* Check for sub-ioctl handler */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700915 if (descr->name[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* Reserve one int for sub-ioctl index */
917 offset = sizeof(__u32);
918
919 /* Size of set arguments */
920 extra_size = get_priv_size(descr->set_args);
921
922 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700923 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 ((extra_size + offset) <= IFNAMSIZ))
925 extra_size = 0;
926 } else {
927 /* Size of get arguments */
928 extra_size = get_priv_size(descr->get_args);
929
930 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700931 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 (extra_size <= IFNAMSIZ))
933 extra_size = 0;
934 }
935 }
936
937 /* Prepare the call */
938 info.cmd = cmd;
939 info.flags = 0;
940
941 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700942 if (extra_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* No extra arguments. Trivial to handle */
944 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
945 } else {
946 char * extra;
947 int err;
948
949 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700950 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700952 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 (iwr->u.data.length != 0))
954 return -EFAULT;
955
956 /* Does it fits within bounds ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700957 if (iwr->u.data.length > (descr->set_args &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 IW_PRIV_SIZE_MASK))
959 return -E2BIG;
960 } else {
961 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700962 if (iwr->u.data.pointer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EFAULT;
964 }
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* Always allocate for max space. Easier, and won't last
967 * long... */
968 extra = kmalloc(extra_size, GFP_KERNEL);
969 if (extra == NULL) {
970 return -ENOMEM;
971 }
972
973 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700974 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 err = copy_from_user(extra, iwr->u.data.pointer,
976 extra_size);
977 if (err) {
978 kfree(extra);
979 return -EFAULT;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
983 /* Call the handler */
984 ret = handler(dev, &info, &(iwr->u), extra);
985
986 /* If we have something to return to the user */
987 if (!ret && IW_IS_GET(cmd)) {
988
989 /* Adjust for the actual length if it's variable,
990 * avoid leaking kernel bits outside. */
991 if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
992 extra_size = adjust_priv_size(descr->get_args,
993 &(iwr->u));
994 }
995
996 err = copy_to_user(iwr->u.data.pointer, extra,
997 extra_size);
998 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900999 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 /* Cleanup - I told you it wasn't that long ;-) */
1003 kfree(extra);
1004 }
1005
1006
1007 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001008 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 ret = call_commit_handler(dev);
1010
1011 return ret;
1012}
1013
1014/* ---------------------------------------------------------------- */
1015/*
Johannes Berg295f4a12007-04-26 20:43:56 -07001016 * Main IOCTl dispatcher.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * Check the type of IOCTL and call the appropriate wrapper...
1018 */
Johannes Berg295f4a12007-04-26 20:43:56 -07001019static int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct net_device *dev;
1022 iw_handler handler;
1023
1024 /* Permissions are already checked in dev_ioctl() before calling us.
1025 * The copy_to/from_user() of ifr is also dealt with in there */
1026
1027 /* Make sure the device exist */
1028 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
1029 return -ENODEV;
1030
1031 /* A bunch of special cases, then the generic case...
1032 * Note that 'cmd' is already filtered in dev_ioctl() with
1033 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001034 if (cmd == SIOCGIWSTATS)
1035 return ioctl_standard_call(dev, ifr, cmd,
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001036 &iw_handler_get_iwstats);
1037
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001038 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
1039 return ioctl_standard_call(dev, ifr, cmd,
1040 &iw_handler_get_private);
1041
1042 /* Basic check */
1043 if (!netif_device_present(dev))
1044 return -ENODEV;
1045
1046 /* New driver API : try to find the handler */
1047 handler = get_handler(dev, cmd);
1048 if (handler) {
1049 /* Standard and private are not the same */
1050 if (cmd < SIOCIWFIRSTPRIV)
1051 return ioctl_standard_call(dev, ifr, cmd, handler);
1052 else
1053 return ioctl_private_call(dev, ifr, cmd, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001055 /* Old driver API : call driver ioctl handler */
1056 if (dev->do_ioctl)
1057 return dev->do_ioctl(dev, ifr, cmd);
1058 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Johannes Berg295f4a12007-04-26 20:43:56 -07001061/* entry point from dev ioctl */
1062int wext_handle_ioctl(struct ifreq *ifr, unsigned int cmd,
1063 void __user *arg)
1064{
1065 int ret;
1066
1067 /* If command is `set a parameter', or
1068 * `get the encoding parameters', check if
1069 * the user has the right to do it */
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001070 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1071 && !capable(CAP_NET_ADMIN))
1072 return -EPERM;
1073
Johannes Berg295f4a12007-04-26 20:43:56 -07001074 dev_load(ifr->ifr_name);
1075 rtnl_lock();
1076 ret = wireless_process_ioctl(ifr, cmd);
1077 rtnl_unlock();
1078 if (IW_IS_GET(cmd) && copy_to_user(arg, ifr, sizeof(struct ifreq)))
1079 return -EFAULT;
1080 return ret;
1081}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083/************************* EVENT PROCESSING *************************/
1084/*
1085 * Process events generated by the wireless layer or the driver.
1086 * Most often, the event will be propagated through rtnetlink
1087 */
1088
John W. Linvillebaef1862006-09-08 16:04:05 -04001089/* ---------------------------------------------------------------- */
1090/*
1091 * Locking...
1092 * ----------
1093 *
1094 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1095 * the locking issue in here and implementing this code !
1096 *
1097 * The issue : wireless_send_event() is often called in interrupt context,
1098 * while the Netlink layer can never be called in interrupt context.
1099 * The fully formed RtNetlink events are queued, and then a tasklet is run
1100 * to feed those to Netlink.
1101 * The skb_queue is interrupt safe, and its lock is not held while calling
1102 * Netlink, so there is no possibility of dealock.
1103 * Jean II
1104 */
1105
Herbert Xu782a6672006-08-03 23:54:41 +10001106static struct sk_buff_head wireless_nlevent_queue;
1107
John W. Linvillebaef1862006-09-08 16:04:05 -04001108static int __init wireless_nlevent_init(void)
1109{
1110 skb_queue_head_init(&wireless_nlevent_queue);
1111 return 0;
1112}
1113
1114subsys_initcall(wireless_nlevent_init);
1115
Herbert Xu782a6672006-08-03 23:54:41 +10001116static void wireless_nlevent_process(unsigned long data)
1117{
1118 struct sk_buff *skb;
1119
1120 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
Thomas Grafbd5785b2006-08-15 00:36:49 -07001121 rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
Herbert Xu782a6672006-08-03 23:54:41 +10001122}
1123
1124static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/* ---------------------------------------------------------------- */
1127/*
1128 * Fill a rtnetlink message with our event data.
1129 * Note that we propage only the specified event and don't dump the
1130 * current wireless config. Dumping the wireless config is far too
1131 * expensive (for each parameter, the driver need to query the hardware).
1132 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001133static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
1134 int type, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct ifinfomsg *r;
1137 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001138 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
1141 r = NLMSG_DATA(nlh);
1142 r->ifi_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001143 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 r->ifi_type = dev->type;
1145 r->ifi_index = dev->ifindex;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001146 r->ifi_flags = dev_get_flags(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1148
1149 /* Add the wireless events in the netlink packet */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001150 RTA_PUT(skb, IFLA_WIRELESS, event_len, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001152 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return skb->len;
1154
1155nlmsg_failure:
1156rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001157 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -1;
1159}
1160
1161/* ---------------------------------------------------------------- */
1162/*
1163 * Create and broadcast and send it on the standard rtnetlink socket
1164 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1165 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1166 * within a RTM_NEWLINK event.
1167 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001168static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct sk_buff *skb;
1171 int size = NLMSG_GOODSIZE;
1172
1173 skb = alloc_skb(size, GFP_ATOMIC);
1174 if (!skb)
1175 return;
1176
1177 if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
1178 event, event_len) < 0) {
1179 kfree_skb(skb);
1180 return;
1181 }
Patrick McHardyac6d4392005-08-14 19:29:52 -07001182 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
Herbert Xu782a6672006-08-03 23:54:41 +10001183 skb_queue_tail(&wireless_nlevent_queue, skb);
1184 tasklet_schedule(&wireless_nlevent_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
Herbert Xu782a6672006-08-03 23:54:41 +10001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/* ---------------------------------------------------------------- */
1188/*
1189 * Main event dispatcher. Called from other parts and drivers.
1190 * Send the event on the appropriate channels.
1191 * May be called from interrupt context.
1192 */
1193void wireless_send_event(struct net_device * dev,
1194 unsigned int cmd,
1195 union iwreq_data * wrqu,
1196 char * extra)
1197{
1198 const struct iw_ioctl_description * descr = NULL;
1199 int extra_len = 0;
1200 struct iw_event *event; /* Mallocated whole event */
1201 int event_len; /* Its size */
1202 int hdr_len; /* Size of the event header */
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001203 int wrqu_off = 0; /* Offset in wrqu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Don't "optimise" the following variable, it will crash */
1205 unsigned cmd_index; /* *MUST* be unsigned */
1206
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001207 /* Get the description of the Event */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001208 if (cmd <= SIOCIWLAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 cmd_index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001210 if (cmd_index < standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 descr = &(standard_ioctl[cmd_index]);
1212 } else {
1213 cmd_index = cmd - IWEVFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001214 if (cmd_index < standard_event_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 descr = &(standard_event[cmd_index]);
1216 }
1217 /* Don't accept unknown events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001218 if (descr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* Note : we don't return an error to the driver, because
1220 * the driver would not know what to do about it. It can't
1221 * return an error to the user, because the event is not
1222 * initiated by a user request.
1223 * The best the driver could do is to log an error message.
1224 * We will do it ourselves instead...
1225 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001226 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dev->name, cmd);
1228 return;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Check extra parameters and set extra_len */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001232 if (descr->header_type == IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001234 if (wrqu->data.length > descr->max_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001235 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
1237 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001238 if (wrqu->data.length < descr->min_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001239 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242 /* Calculate extra_len - extra is NULL for restricted events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001243 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 extra_len = wrqu->data.length * descr->token_size;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001245 /* Always at an offset in wrqu */
1246 wrqu_off = IW_EV_POINT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 /* Total length of the event */
1250 hdr_len = event_type_size[descr->header_type];
1251 event_len = hdr_len + extra_len;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* Create temporary buffer to hold the event */
1254 event = kmalloc(event_len, GFP_ATOMIC);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001255 if (event == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return;
1257
1258 /* Fill event */
1259 event->len = event_len;
1260 event->cmd = cmd;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001261 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001262 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 memcpy(((char *) event) + hdr_len, extra, extra_len);
1264
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001265 /* Send via the RtNetlink event channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 rtmsg_iwinfo(dev, (char *) event, event_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Cleanup */
1269 kfree(event);
1270
1271 return; /* Always success, I guess ;-) */
1272}
Johannes Berg7a9df162007-04-26 20:46:23 -07001273EXPORT_SYMBOL(wireless_send_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/********************** ENHANCED IWSPY SUPPORT **********************/
1276/*
1277 * In the old days, the driver was handling spy support all by itself.
1278 * Now, the driver can delegate this task to Wireless Extensions.
1279 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1280 * push data to us via wireless_spy_update() and include struct iw_spy_data
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001281 * in its private part (and export it in net_device->wireless_data->spy_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * One of the main advantage of centralising spy support here is that
1283 * it becomes much easier to improve and extend it without having to touch
1284 * the drivers. One example is the addition of the Spy-Threshold events.
1285 */
1286
1287/* ---------------------------------------------------------------- */
1288/*
1289 * Return the pointer to the spy data in the driver.
1290 * Because this is called on the Rx path via wireless_spy_update(),
1291 * we want it to be efficient...
1292 */
1293static inline struct iw_spy_data * get_spydata(struct net_device *dev)
1294{
1295 /* This is the new way */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001296 if (dev->wireless_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return(dev->wireless_data->spy_data);
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001298 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/*------------------------------------------------------------------*/
1302/*
1303 * Standard Wireless Handler : set Spy List
1304 */
1305int iw_handler_set_spy(struct net_device * dev,
1306 struct iw_request_info * info,
1307 union iwreq_data * wrqu,
1308 char * extra)
1309{
1310 struct iw_spy_data * spydata = get_spydata(dev);
1311 struct sockaddr * address = (struct sockaddr *) extra;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001314 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return -EOPNOTSUPP;
1316
1317 /* Disable spy collection while we copy the addresses.
1318 * While we copy addresses, any call to wireless_spy_update()
1319 * will NOP. This is OK, as anyway the addresses are changing. */
1320 spydata->spy_number = 0;
1321
1322 /* We want to operate without locking, because wireless_spy_update()
1323 * most likely will happen in the interrupt handler, and therefore
1324 * have its own locking constraints and needs performance.
1325 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1326 * This make sure wireless_spy_update() "see" that the spy list
1327 * is temporarily disabled. */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001328 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /* Are there are addresses to copy? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001331 if (wrqu->data.length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int i;
1333
1334 /* Copy addresses */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001335 for (i = 0; i < wrqu->data.length; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(spydata->spy_address[i], address[i].sa_data,
1337 ETH_ALEN);
1338 /* Reset stats */
1339 memset(spydata->spy_stat, 0,
1340 sizeof(struct iw_quality) * IW_MAX_SPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 /* Make sure above is updated before re-enabling */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001344 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* Enable addresses */
1347 spydata->spy_number = wrqu->data.length;
1348
1349 return 0;
1350}
Johannes Berg7a9df162007-04-26 20:46:23 -07001351EXPORT_SYMBOL(iw_handler_set_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/*------------------------------------------------------------------*/
1354/*
1355 * Standard Wireless Handler : get Spy List
1356 */
1357int iw_handler_get_spy(struct net_device * dev,
1358 struct iw_request_info * info,
1359 union iwreq_data * wrqu,
1360 char * extra)
1361{
1362 struct iw_spy_data * spydata = get_spydata(dev);
1363 struct sockaddr * address = (struct sockaddr *) extra;
1364 int i;
1365
1366 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001367 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EOPNOTSUPP;
1369
1370 wrqu->data.length = spydata->spy_number;
1371
1372 /* Copy addresses. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001373 for (i = 0; i < spydata->spy_number; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1375 address[i].sa_family = AF_UNIX;
1376 }
1377 /* Copy stats to the user buffer (just after). */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001378 if (spydata->spy_number > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1380 spydata->spy_stat,
1381 sizeof(struct iw_quality) * spydata->spy_number);
1382 /* Reset updated flags. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001383 for (i = 0; i < spydata->spy_number; i++)
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001384 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386}
Johannes Berg7a9df162007-04-26 20:46:23 -07001387EXPORT_SYMBOL(iw_handler_get_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389/*------------------------------------------------------------------*/
1390/*
1391 * Standard Wireless Handler : set spy threshold
1392 */
1393int iw_handler_set_thrspy(struct net_device * dev,
1394 struct iw_request_info *info,
1395 union iwreq_data * wrqu,
1396 char * extra)
1397{
1398 struct iw_spy_data * spydata = get_spydata(dev);
1399 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1400
1401 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001402 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EOPNOTSUPP;
1404
1405 /* Just do it */
1406 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1407 2 * sizeof(struct iw_quality));
1408
1409 /* Clear flag */
1410 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
1413}
Johannes Berg7a9df162007-04-26 20:46:23 -07001414EXPORT_SYMBOL(iw_handler_set_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416/*------------------------------------------------------------------*/
1417/*
1418 * Standard Wireless Handler : get spy threshold
1419 */
1420int iw_handler_get_thrspy(struct net_device * dev,
1421 struct iw_request_info *info,
1422 union iwreq_data * wrqu,
1423 char * extra)
1424{
1425 struct iw_spy_data * spydata = get_spydata(dev);
1426 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1427
1428 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001429 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -EOPNOTSUPP;
1431
1432 /* Just do it */
1433 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1434 2 * sizeof(struct iw_quality));
1435
1436 return 0;
1437}
Johannes Berg7a9df162007-04-26 20:46:23 -07001438EXPORT_SYMBOL(iw_handler_get_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/*------------------------------------------------------------------*/
1441/*
1442 * Prepare and send a Spy Threshold event
1443 */
1444static void iw_send_thrspy_event(struct net_device * dev,
1445 struct iw_spy_data * spydata,
1446 unsigned char * address,
1447 struct iw_quality * wstats)
1448{
1449 union iwreq_data wrqu;
1450 struct iw_thrspy threshold;
1451
1452 /* Init */
1453 wrqu.data.length = 1;
1454 wrqu.data.flags = 0;
1455 /* Copy address */
1456 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1457 threshold.addr.sa_family = ARPHRD_ETHER;
1458 /* Copy stats */
1459 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1460 /* Copy also thresholds */
1461 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1462 2 * sizeof(struct iw_quality));
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /* Send event to user space */
1465 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1466}
1467
1468/* ---------------------------------------------------------------- */
1469/*
1470 * Call for the driver to update the spy data.
1471 * For now, the spy data is a simple array. As the size of the array is
1472 * small, this is good enough. If we wanted to support larger number of
1473 * spy addresses, we should use something more efficient...
1474 */
1475void wireless_spy_update(struct net_device * dev,
1476 unsigned char * address,
1477 struct iw_quality * wstats)
1478{
1479 struct iw_spy_data * spydata = get_spydata(dev);
1480 int i;
1481 int match = -1;
1482
1483 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001484 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* Update all records that match */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001488 for (i = 0; i < spydata->spy_number; i++)
1489 if (!compare_ether_addr(address, spydata->spy_address[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 memcpy(&(spydata->spy_stat[i]), wstats,
1491 sizeof(struct iw_quality));
1492 match = i;
1493 }
1494
1495 /* Generate an event if we cross the spy threshold.
1496 * To avoid event storms, we have a simple hysteresis : we generate
1497 * event only when we go under the low threshold or above the
1498 * high threshold. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001499 if (match >= 0) {
1500 if (spydata->spy_thr_under[match]) {
1501 if (wstats->level > spydata->spy_thr_high.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 spydata->spy_thr_under[match] = 0;
1503 iw_send_thrspy_event(dev, spydata,
1504 address, wstats);
1505 }
1506 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001507 if (wstats->level < spydata->spy_thr_low.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 spydata->spy_thr_under[match] = 1;
1509 iw_send_thrspy_event(dev, spydata,
1510 address, wstats);
1511 }
1512 }
1513 }
1514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515EXPORT_SYMBOL(wireless_spy_update);