[NETFILTER]: ctnetlink: add support for master tuple event notification and dumping
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wireless / wext.c
blob1e4cf615f8788db0d578b2b1e1231fb61b61f6fa
1 /*
2 * This file implement the Wireless Extensions APIs.
4 * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
5 * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
7 * (As all part of the Linux kernel, this file is GPL)
8 */
10 /************************** DOCUMENTATION **************************/
12 * API definition :
13 * --------------
14 * See <linux/wireless.h> for details of the APIs and the rest.
16 * History :
17 * -------
19 * v1 - 5.12.01 - Jean II
20 * o Created this file.
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
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
36 * v4 - 18.04.02 - Jean II
37 * o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1
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)
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
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
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)
69 * v8 - 17.02.06 - Jean II
70 * o RtNetlink requests support (SET/GET)
72 * v8b - 03.08.06 - Herbert Xu
73 * o Fix Wireless Event locking issues.
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()
80 * v10 - 16.3.07 - Jean II
81 * o Prevent leaking of kernel space in stream on 64 bits.
84 /***************************** INCLUDES *****************************/
86 #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 */
94 #include <linux/etherdevice.h> /* compare_ether_addr */
95 #include <linux/interrupt.h>
96 #include <net/net_namespace.h>
98 #include <linux/wireless.h> /* Pretty obvious */
99 #include <net/iw_handler.h> /* New driver API */
100 #include <net/netlink.h>
101 #include <net/wext.h>
103 #include <asm/uaccess.h> /* copy_to_user() */
105 /************************* GLOBAL VARIABLES *************************/
107 * You should not use global variables, because of re-entrancy.
108 * On our case, it's only const, so it's OK...
111 * Meta-data about all the standard Wireless Extension request we
112 * know about.
114 static const struct iw_ioctl_description standard_ioctl[] = {
115 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
116 .header_type = IW_HEADER_TYPE_NULL,
118 [SIOCGIWNAME - SIOCIWFIRST] = {
119 .header_type = IW_HEADER_TYPE_CHAR,
120 .flags = IW_DESCR_FLAG_DUMP,
122 [SIOCSIWNWID - SIOCIWFIRST] = {
123 .header_type = IW_HEADER_TYPE_PARAM,
124 .flags = IW_DESCR_FLAG_EVENT,
126 [SIOCGIWNWID - SIOCIWFIRST] = {
127 .header_type = IW_HEADER_TYPE_PARAM,
128 .flags = IW_DESCR_FLAG_DUMP,
130 [SIOCSIWFREQ - SIOCIWFIRST] = {
131 .header_type = IW_HEADER_TYPE_FREQ,
132 .flags = IW_DESCR_FLAG_EVENT,
134 [SIOCGIWFREQ - SIOCIWFIRST] = {
135 .header_type = IW_HEADER_TYPE_FREQ,
136 .flags = IW_DESCR_FLAG_DUMP,
138 [SIOCSIWMODE - SIOCIWFIRST] = {
139 .header_type = IW_HEADER_TYPE_UINT,
140 .flags = IW_DESCR_FLAG_EVENT,
142 [SIOCGIWMODE - SIOCIWFIRST] = {
143 .header_type = IW_HEADER_TYPE_UINT,
144 .flags = IW_DESCR_FLAG_DUMP,
146 [SIOCSIWSENS - SIOCIWFIRST] = {
147 .header_type = IW_HEADER_TYPE_PARAM,
149 [SIOCGIWSENS - SIOCIWFIRST] = {
150 .header_type = IW_HEADER_TYPE_PARAM,
152 [SIOCSIWRANGE - SIOCIWFIRST] = {
153 .header_type = IW_HEADER_TYPE_NULL,
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,
161 [SIOCSIWPRIV - SIOCIWFIRST] = {
162 .header_type = IW_HEADER_TYPE_NULL,
164 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
165 .header_type = IW_HEADER_TYPE_POINT,
166 .token_size = sizeof(struct iw_priv_args),
167 .max_tokens = 16,
168 .flags = IW_DESCR_FLAG_NOMAX,
170 [SIOCSIWSTATS - SIOCIWFIRST] = {
171 .header_type = IW_HEADER_TYPE_NULL,
173 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
174 .header_type = IW_HEADER_TYPE_POINT,
175 .token_size = 1,
176 .max_tokens = sizeof(struct iw_statistics),
177 .flags = IW_DESCR_FLAG_DUMP,
179 [SIOCSIWSPY - SIOCIWFIRST] = {
180 .header_type = IW_HEADER_TYPE_POINT,
181 .token_size = sizeof(struct sockaddr),
182 .max_tokens = IW_MAX_SPY,
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,
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,
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,
202 [SIOCSIWAP - SIOCIWFIRST] = {
203 .header_type = IW_HEADER_TYPE_ADDR,
205 [SIOCGIWAP - SIOCIWFIRST] = {
206 .header_type = IW_HEADER_TYPE_ADDR,
207 .flags = IW_DESCR_FLAG_DUMP,
209 [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),
215 [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,
222 [SIOCSIWSCAN - SIOCIWFIRST] = {
223 .header_type = IW_HEADER_TYPE_POINT,
224 .token_size = 1,
225 .min_tokens = 0,
226 .max_tokens = sizeof(struct iw_scan_req),
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,
234 [SIOCSIWESSID - SIOCIWFIRST] = {
235 .header_type = IW_HEADER_TYPE_POINT,
236 .token_size = 1,
237 .max_tokens = IW_ESSID_MAX_SIZE,
238 .flags = IW_DESCR_FLAG_EVENT,
240 [SIOCGIWESSID - SIOCIWFIRST] = {
241 .header_type = IW_HEADER_TYPE_POINT,
242 .token_size = 1,
243 .max_tokens = IW_ESSID_MAX_SIZE,
244 .flags = IW_DESCR_FLAG_DUMP,
246 [SIOCSIWNICKN - SIOCIWFIRST] = {
247 .header_type = IW_HEADER_TYPE_POINT,
248 .token_size = 1,
249 .max_tokens = IW_ESSID_MAX_SIZE,
251 [SIOCGIWNICKN - SIOCIWFIRST] = {
252 .header_type = IW_HEADER_TYPE_POINT,
253 .token_size = 1,
254 .max_tokens = IW_ESSID_MAX_SIZE,
256 [SIOCSIWRATE - SIOCIWFIRST] = {
257 .header_type = IW_HEADER_TYPE_PARAM,
259 [SIOCGIWRATE - SIOCIWFIRST] = {
260 .header_type = IW_HEADER_TYPE_PARAM,
262 [SIOCSIWRTS - SIOCIWFIRST] = {
263 .header_type = IW_HEADER_TYPE_PARAM,
265 [SIOCGIWRTS - SIOCIWFIRST] = {
266 .header_type = IW_HEADER_TYPE_PARAM,
268 [SIOCSIWFRAG - SIOCIWFIRST] = {
269 .header_type = IW_HEADER_TYPE_PARAM,
271 [SIOCGIWFRAG - SIOCIWFIRST] = {
272 .header_type = IW_HEADER_TYPE_PARAM,
274 [SIOCSIWTXPOW - SIOCIWFIRST] = {
275 .header_type = IW_HEADER_TYPE_PARAM,
277 [SIOCGIWTXPOW - SIOCIWFIRST] = {
278 .header_type = IW_HEADER_TYPE_PARAM,
280 [SIOCSIWRETRY - SIOCIWFIRST] = {
281 .header_type = IW_HEADER_TYPE_PARAM,
283 [SIOCGIWRETRY - SIOCIWFIRST] = {
284 .header_type = IW_HEADER_TYPE_PARAM,
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,
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,
298 [SIOCSIWPOWER - SIOCIWFIRST] = {
299 .header_type = IW_HEADER_TYPE_PARAM,
301 [SIOCGIWPOWER - SIOCIWFIRST] = {
302 .header_type = IW_HEADER_TYPE_PARAM,
304 [SIOCSIWGENIE - SIOCIWFIRST] = {
305 .header_type = IW_HEADER_TYPE_POINT,
306 .token_size = 1,
307 .max_tokens = IW_GENERIC_IE_MAX,
309 [SIOCGIWGENIE - SIOCIWFIRST] = {
310 .header_type = IW_HEADER_TYPE_POINT,
311 .token_size = 1,
312 .max_tokens = IW_GENERIC_IE_MAX,
314 [SIOCSIWAUTH - SIOCIWFIRST] = {
315 .header_type = IW_HEADER_TYPE_PARAM,
317 [SIOCGIWAUTH - SIOCIWFIRST] = {
318 .header_type = IW_HEADER_TYPE_PARAM,
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,
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,
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),
341 static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
344 * Meta-data about all the additional standard Wireless Extension events
345 * we know about.
347 static const struct iw_ioctl_description standard_event[] = {
348 [IWEVTXDROP - IWEVFIRST] = {
349 .header_type = IW_HEADER_TYPE_ADDR,
351 [IWEVQUAL - IWEVFIRST] = {
352 .header_type = IW_HEADER_TYPE_QUAL,
354 [IWEVCUSTOM - IWEVFIRST] = {
355 .header_type = IW_HEADER_TYPE_POINT,
356 .token_size = 1,
357 .max_tokens = IW_CUSTOM_MAX,
359 [IWEVREGISTERED - IWEVFIRST] = {
360 .header_type = IW_HEADER_TYPE_ADDR,
362 [IWEVEXPIRED - IWEVFIRST] = {
363 .header_type = IW_HEADER_TYPE_ADDR,
365 [IWEVGENIE - IWEVFIRST] = {
366 .header_type = IW_HEADER_TYPE_POINT,
367 .token_size = 1,
368 .max_tokens = IW_GENERIC_IE_MAX,
370 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
371 .header_type = IW_HEADER_TYPE_POINT,
372 .token_size = 1,
373 .max_tokens = sizeof(struct iw_michaelmicfailure),
375 [IWEVASSOCREQIE - IWEVFIRST] = {
376 .header_type = IW_HEADER_TYPE_POINT,
377 .token_size = 1,
378 .max_tokens = IW_GENERIC_IE_MAX,
380 [IWEVASSOCRESPIE - IWEVFIRST] = {
381 .header_type = IW_HEADER_TYPE_POINT,
382 .token_size = 1,
383 .max_tokens = IW_GENERIC_IE_MAX,
385 [IWEVPMKIDCAND - IWEVFIRST] = {
386 .header_type = IW_HEADER_TYPE_POINT,
387 .token_size = 1,
388 .max_tokens = sizeof(struct iw_pmkid_cand),
391 static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
393 /* Size (in bytes) of the various private data types */
394 static 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 */
405 /* Size (in bytes) of various events */
406 static const int event_type_size[] = {
407 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
409 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
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 */
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 */
420 /* Size (in bytes) of various events, as packed */
421 static const int event_type_pk_size[] = {
422 IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
424 IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
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 */
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 */
435 /************************ COMMON SUBROUTINES ************************/
437 * Stuff that may be used in various place or doesn't fit in one
438 * of the section below.
441 /* ---------------------------------------------------------------- */
443 * Return the driver handler associated with a specific Wireless Extension.
445 static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
447 /* Don't "optimise" the following variable, it will crash */
448 unsigned int index; /* *MUST* be unsigned */
450 /* Check if we have some wireless handlers defined */
451 if (dev->wireless_handlers == NULL)
452 return NULL;
454 /* Try as a standard command */
455 index = cmd - SIOCIWFIRST;
456 if (index < dev->wireless_handlers->num_standard)
457 return dev->wireless_handlers->standard[index];
459 /* Try as a private command */
460 index = cmd - SIOCIWFIRSTPRIV;
461 if (index < dev->wireless_handlers->num_private)
462 return dev->wireless_handlers->private[index];
464 /* Not found */
465 return NULL;
468 /* ---------------------------------------------------------------- */
470 * Get statistics out of the driver
472 static struct iw_statistics *get_wireless_stats(struct net_device *dev)
474 /* New location */
475 if ((dev->wireless_handlers != NULL) &&
476 (dev->wireless_handlers->get_wireless_stats != NULL))
477 return dev->wireless_handlers->get_wireless_stats(dev);
479 /* Not found */
480 return NULL;
483 /* ---------------------------------------------------------------- */
485 * Call the commit handler in the driver
486 * (if exist and if conditions are right)
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.
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()" ;-)
502 static int call_commit_handler(struct net_device *dev)
504 if ((netif_running(dev)) &&
505 (dev->wireless_handlers->standard[0] != NULL))
506 /* Call the commit handler on the driver */
507 return dev->wireless_handlers->standard[0](dev, NULL,
508 NULL, NULL);
509 else
510 return 0; /* Command completed successfully */
513 /* ---------------------------------------------------------------- */
515 * Calculate size of private arguments
517 static inline int get_priv_size(__u16 args)
519 int num = args & IW_PRIV_SIZE_MASK;
520 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
522 return num * iw_priv_type_size[type];
525 /* ---------------------------------------------------------------- */
527 * Re-calculate the size of private arguments
529 static inline int adjust_priv_size(__u16 args,
530 union iwreq_data * wrqu)
532 int num = wrqu->data.length;
533 int max = args & IW_PRIV_SIZE_MASK;
534 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
536 /* Make sure the driver doesn't goof up */
537 if (max < num)
538 num = max;
540 return num * iw_priv_type_size[type];
543 /* ---------------------------------------------------------------- */
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...
549 static int iw_handler_get_iwstats(struct net_device * dev,
550 struct iw_request_info * info,
551 union iwreq_data * wrqu,
552 char * extra)
554 /* Get stats from the driver */
555 struct iw_statistics *stats;
557 stats = get_wireless_stats(dev);
558 if (stats) {
559 /* Copy statistics to extra */
560 memcpy(extra, stats, sizeof(struct iw_statistics));
561 wrqu->data.length = sizeof(struct iw_statistics);
563 /* Check if we need to clear the updated flag */
564 if (wrqu->data.flags != 0)
565 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
566 return 0;
567 } else
568 return -EOPNOTSUPP;
571 /* ---------------------------------------------------------------- */
573 * Standard Wireless Handler : get iwpriv definitions
574 * Export the driver private handler definition
575 * They will be picked up by tools like iwpriv...
577 static int iw_handler_get_private(struct net_device * dev,
578 struct iw_request_info * info,
579 union iwreq_data * wrqu,
580 char * extra)
582 /* Check if the driver has something to export */
583 if ((dev->wireless_handlers->num_private_args == 0) ||
584 (dev->wireless_handlers->private_args == NULL))
585 return -EOPNOTSUPP;
587 /* Check if there is enough buffer up there */
588 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
589 /* 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;
596 /* Set the number of available ioctls. */
597 wrqu->data.length = dev->wireless_handlers->num_private_args;
599 /* Copy structure to the user buffer. */
600 memcpy(extra, dev->wireless_handlers->private_args,
601 sizeof(struct iw_priv_args) * wrqu->data.length);
603 return 0;
607 /******************** /proc/net/wireless SUPPORT ********************/
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 ;-)
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".
617 #ifdef CONFIG_PROC_FS
619 /* ---------------------------------------------------------------- */
621 * Print one entry (line) of /proc/net/wireless
623 static void wireless_seq_printf_stats(struct seq_file *seq,
624 struct net_device *dev)
626 /* Get stats from the driver */
627 struct iw_statistics *stats = get_wireless_stats(dev);
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 ? '.' : ' ',
635 ((__s32) stats->qual.level) -
636 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
637 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
638 ? '.' : ' ',
639 ((__s32) stats->qual.noise) -
640 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
641 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);
646 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
650 /* ---------------------------------------------------------------- */
652 * Print info for /proc/net/wireless (print all entries)
654 static int wireless_seq_show(struct seq_file *seq, void *v)
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;
667 static const struct seq_operations wireless_seq_ops = {
668 .start = dev_seq_start,
669 .next = dev_seq_next,
670 .stop = dev_seq_stop,
671 .show = wireless_seq_show,
674 static int wireless_seq_open(struct inode *inode, struct file *file)
676 return seq_open_net(inode, file, &wireless_seq_ops,
677 sizeof(struct seq_net_private));
680 static const struct file_operations wireless_seq_fops = {
681 .owner = THIS_MODULE,
682 .open = wireless_seq_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = seq_release_net,
688 int wext_proc_init(struct net *net)
690 /* Create /proc/net/wireless entry */
691 if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
692 return -ENOMEM;
694 return 0;
697 void wext_proc_exit(struct net *net)
699 proc_net_remove(net, "wireless");
701 #endif /* CONFIG_PROC_FS */
703 /************************** IOCTL SUPPORT **************************/
705 * The original user space API to configure all those Wireless Extensions
706 * is through IOCTLs.
707 * In there, we check if we need to call the new driver API (iw_handler)
708 * or just call the driver ioctl handler.
711 /* ---------------------------------------------------------------- */
713 * Wrapper to call a standard Wireless Extension handler.
714 * We do various checks and also take care of moving data between
715 * user space and kernel space.
717 static int ioctl_standard_call(struct net_device * dev,
718 struct ifreq * ifr,
719 unsigned int cmd,
720 iw_handler handler)
722 struct iwreq * iwr = (struct iwreq *) ifr;
723 const struct iw_ioctl_description * descr;
724 struct iw_request_info info;
725 int ret = -EINVAL;
727 /* Get the description of the IOCTL */
728 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
729 return -EOPNOTSUPP;
730 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
732 /* Prepare the call */
733 info.cmd = cmd;
734 info.flags = 0;
736 /* Check if we have a pointer to user space data or not */
737 if (descr->header_type != IW_HEADER_TYPE_POINT) {
739 /* No extra arguments. Trivial to handle */
740 ret = handler(dev, &info, &(iwr->u), NULL);
742 /* Generate an event to notify listeners of the change */
743 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
744 ((ret == 0) || (ret == -EIWCOMMIT)))
745 wireless_send_event(dev, cmd, &(iwr->u), NULL);
746 } else {
747 char * extra;
748 int extra_size;
749 int user_length = 0;
750 int err;
751 int essid_compat = 0;
753 /* Calculate space needed by arguments. Always allocate
754 * for max space. Easier, and won't last long... */
755 extra_size = descr->max_tokens * descr->token_size;
757 /* Check need for ESSID compatibility for WE < 21 */
758 switch (cmd) {
759 case SIOCSIWESSID:
760 case SIOCGIWESSID:
761 case SIOCSIWNICKN:
762 case SIOCGIWNICKN:
763 if (iwr->u.data.length == descr->max_tokens + 1)
764 essid_compat = 1;
765 else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
766 char essid[IW_ESSID_MAX_SIZE + 1];
768 err = copy_from_user(essid, iwr->u.data.pointer,
769 iwr->u.data.length *
770 descr->token_size);
771 if (err)
772 return -EFAULT;
774 if (essid[iwr->u.data.length - 1] == '\0')
775 essid_compat = 1;
777 break;
778 default:
779 break;
782 iwr->u.data.length -= essid_compat;
784 /* Check what user space is giving us */
785 if (IW_IS_SET(cmd)) {
786 /* Check NULL pointer */
787 if ((iwr->u.data.pointer == NULL) &&
788 (iwr->u.data.length != 0))
789 return -EFAULT;
790 /* Check if number of token fits within bounds */
791 if (iwr->u.data.length > descr->max_tokens)
792 return -E2BIG;
793 if (iwr->u.data.length < descr->min_tokens)
794 return -EINVAL;
795 } else {
796 /* Check NULL pointer */
797 if (iwr->u.data.pointer == NULL)
798 return -EFAULT;
799 /* Save user space buffer size for checking */
800 user_length = iwr->u.data.length;
802 /* Don't check if user_length > max to allow forward
803 * compatibility. The test user_length < min is
804 * implied by the test at the end. */
806 /* Support for very large requests */
807 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
808 (user_length > descr->max_tokens)) {
809 /* Allow userspace to GET more than max so
810 * we can support any size GET requests.
811 * There is still a limit : -ENOMEM. */
812 extra_size = user_length * descr->token_size;
813 /* Note : user_length is originally a __u16,
814 * and token_size is controlled by us,
815 * so extra_size won't get negative and
816 * won't overflow... */
820 /* Create the kernel buffer */
821 /* kzalloc ensures NULL-termination for essid_compat */
822 extra = kzalloc(extra_size, GFP_KERNEL);
823 if (extra == NULL)
824 return -ENOMEM;
826 /* If it is a SET, get all the extra data in here */
827 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
828 err = copy_from_user(extra, iwr->u.data.pointer,
829 iwr->u.data.length *
830 descr->token_size);
831 if (err) {
832 kfree(extra);
833 return -EFAULT;
837 /* Call the handler */
838 ret = handler(dev, &info, &(iwr->u), extra);
840 iwr->u.data.length += essid_compat;
842 /* If we have something to return to the user */
843 if (!ret && IW_IS_GET(cmd)) {
844 /* Check if there is enough buffer up there */
845 if (user_length < iwr->u.data.length) {
846 kfree(extra);
847 return -E2BIG;
850 err = copy_to_user(iwr->u.data.pointer, extra,
851 iwr->u.data.length *
852 descr->token_size);
853 if (err)
854 ret = -EFAULT;
857 /* Generate an event to notify listeners of the change */
858 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
859 ((ret == 0) || (ret == -EIWCOMMIT))) {
860 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
861 /* If the event is restricted, don't
862 * export the payload */
863 wireless_send_event(dev, cmd, &(iwr->u), NULL);
864 else
865 wireless_send_event(dev, cmd, &(iwr->u),
866 extra);
869 /* Cleanup - I told you it wasn't that long ;-) */
870 kfree(extra);
873 /* Call commit handler if needed and defined */
874 if (ret == -EIWCOMMIT)
875 ret = call_commit_handler(dev);
877 /* Here, we will generate the appropriate event if needed */
879 return ret;
882 /* ---------------------------------------------------------------- */
884 * Wrapper to call a private Wireless Extension handler.
885 * We do various checks and also take care of moving data between
886 * user space and kernel space.
887 * It's not as nice and slimline as the standard wrapper. The cause
888 * is struct iw_priv_args, which was not really designed for the
889 * job we are going here.
891 * IMPORTANT : This function prevent to set and get data on the same
892 * IOCTL and enforce the SET/GET convention. Not doing it would be
893 * far too hairy...
894 * If you need to set and get data at the same time, please don't use
895 * a iw_handler but process it in your ioctl handler (i.e. use the
896 * old driver API).
898 static int ioctl_private_call(struct net_device *dev, struct ifreq *ifr,
899 unsigned int cmd, iw_handler handler)
901 struct iwreq * iwr = (struct iwreq *) ifr;
902 const struct iw_priv_args * descr = NULL;
903 struct iw_request_info info;
904 int extra_size = 0;
905 int i;
906 int ret = -EINVAL;
908 /* Get the description of the IOCTL */
909 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
910 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
911 descr = &(dev->wireless_handlers->private_args[i]);
912 break;
915 /* Compute the size of the set/get arguments */
916 if (descr != NULL) {
917 if (IW_IS_SET(cmd)) {
918 int offset = 0; /* For sub-ioctls */
919 /* Check for sub-ioctl handler */
920 if (descr->name[0] == '\0')
921 /* Reserve one int for sub-ioctl index */
922 offset = sizeof(__u32);
924 /* Size of set arguments */
925 extra_size = get_priv_size(descr->set_args);
927 /* Does it fits in iwr ? */
928 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
929 ((extra_size + offset) <= IFNAMSIZ))
930 extra_size = 0;
931 } else {
932 /* Size of get arguments */
933 extra_size = get_priv_size(descr->get_args);
935 /* Does it fits in iwr ? */
936 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
937 (extra_size <= IFNAMSIZ))
938 extra_size = 0;
942 /* Prepare the call */
943 info.cmd = cmd;
944 info.flags = 0;
946 /* Check if we have a pointer to user space data or not. */
947 if (extra_size == 0) {
948 /* No extra arguments. Trivial to handle */
949 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
950 } else {
951 char * extra;
952 int err;
954 /* Check what user space is giving us */
955 if (IW_IS_SET(cmd)) {
956 /* Check NULL pointer */
957 if ((iwr->u.data.pointer == NULL) &&
958 (iwr->u.data.length != 0))
959 return -EFAULT;
961 /* Does it fits within bounds ? */
962 if (iwr->u.data.length > (descr->set_args &
963 IW_PRIV_SIZE_MASK))
964 return -E2BIG;
965 } else if (iwr->u.data.pointer == NULL)
966 return -EFAULT;
968 /* Always allocate for max space. Easier, and won't last
969 * long... */
970 extra = kmalloc(extra_size, GFP_KERNEL);
971 if (extra == NULL)
972 return -ENOMEM;
974 /* If it is a SET, get all the extra data in here */
975 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
976 err = copy_from_user(extra, iwr->u.data.pointer,
977 extra_size);
978 if (err) {
979 kfree(extra);
980 return -EFAULT;
984 /* Call the handler */
985 ret = handler(dev, &info, &(iwr->u), extra);
987 /* If we have something to return to the user */
988 if (!ret && IW_IS_GET(cmd)) {
990 /* Adjust for the actual length if it's variable,
991 * avoid leaking kernel bits outside. */
992 if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
993 extra_size = adjust_priv_size(descr->get_args,
994 &(iwr->u));
997 err = copy_to_user(iwr->u.data.pointer, extra,
998 extra_size);
999 if (err)
1000 ret = -EFAULT;
1003 /* Cleanup - I told you it wasn't that long ;-) */
1004 kfree(extra);
1008 /* Call commit handler if needed and defined */
1009 if (ret == -EIWCOMMIT)
1010 ret = call_commit_handler(dev);
1012 return ret;
1015 /* ---------------------------------------------------------------- */
1017 * Main IOCTl dispatcher.
1018 * Check the type of IOCTL and call the appropriate wrapper...
1020 static int wireless_process_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd)
1022 struct net_device *dev;
1023 iw_handler handler;
1025 /* Permissions are already checked in dev_ioctl() before calling us.
1026 * The copy_to/from_user() of ifr is also dealt with in there */
1028 /* Make sure the device exist */
1029 if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
1030 return -ENODEV;
1032 /* A bunch of special cases, then the generic case...
1033 * Note that 'cmd' is already filtered in dev_ioctl() with
1034 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
1035 if (cmd == SIOCGIWSTATS)
1036 return ioctl_standard_call(dev, ifr, cmd,
1037 &iw_handler_get_iwstats);
1039 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
1040 return ioctl_standard_call(dev, ifr, cmd,
1041 &iw_handler_get_private);
1043 /* Basic check */
1044 if (!netif_device_present(dev))
1045 return -ENODEV;
1047 /* New driver API : try to find the handler */
1048 handler = get_handler(dev, cmd);
1049 if (handler) {
1050 /* Standard and private are not the same */
1051 if (cmd < SIOCIWFIRSTPRIV)
1052 return ioctl_standard_call(dev, ifr, cmd, handler);
1053 else
1054 return ioctl_private_call(dev, ifr, cmd, handler);
1056 /* Old driver API : call driver ioctl handler */
1057 if (dev->do_ioctl)
1058 return dev->do_ioctl(dev, ifr, cmd);
1059 return -EOPNOTSUPP;
1062 /* entry point from dev ioctl */
1063 int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1064 void __user *arg)
1066 int ret;
1068 /* If command is `set a parameter', or
1069 * `get the encoding parameters', check if
1070 * the user has the right to do it */
1071 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1072 && !capable(CAP_NET_ADMIN))
1073 return -EPERM;
1075 dev_load(net, ifr->ifr_name);
1076 rtnl_lock();
1077 ret = wireless_process_ioctl(net, ifr, cmd);
1078 rtnl_unlock();
1079 if (IW_IS_GET(cmd) && copy_to_user(arg, ifr, sizeof(struct iwreq)))
1080 return -EFAULT;
1081 return ret;
1084 /************************* EVENT PROCESSING *************************/
1086 * Process events generated by the wireless layer or the driver.
1087 * Most often, the event will be propagated through rtnetlink
1090 /* ---------------------------------------------------------------- */
1092 * Locking...
1093 * ----------
1095 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1096 * the locking issue in here and implementing this code !
1098 * The issue : wireless_send_event() is often called in interrupt context,
1099 * while the Netlink layer can never be called in interrupt context.
1100 * The fully formed RtNetlink events are queued, and then a tasklet is run
1101 * to feed those to Netlink.
1102 * The skb_queue is interrupt safe, and its lock is not held while calling
1103 * Netlink, so there is no possibility of dealock.
1104 * Jean II
1107 static struct sk_buff_head wireless_nlevent_queue;
1109 static int __init wireless_nlevent_init(void)
1111 skb_queue_head_init(&wireless_nlevent_queue);
1112 return 0;
1115 subsys_initcall(wireless_nlevent_init);
1117 static void wireless_nlevent_process(unsigned long data)
1119 struct sk_buff *skb;
1121 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
1122 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
1125 static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1127 /* ---------------------------------------------------------------- */
1129 * Fill a rtnetlink message with our event data.
1130 * Note that we propage only the specified event and don't dump the
1131 * current wireless config. Dumping the wireless config is far too
1132 * expensive (for each parameter, the driver need to query the hardware).
1134 static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
1135 int type, char *event, int event_len)
1137 struct ifinfomsg *r;
1138 struct nlmsghdr *nlh;
1140 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
1141 if (nlh == NULL)
1142 return -EMSGSIZE;
1144 r = nlmsg_data(nlh);
1145 r->ifi_family = AF_UNSPEC;
1146 r->__ifi_pad = 0;
1147 r->ifi_type = dev->type;
1148 r->ifi_index = dev->ifindex;
1149 r->ifi_flags = dev_get_flags(dev);
1150 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1152 /* Add the wireless events in the netlink packet */
1153 NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
1155 return nlmsg_end(skb, nlh);
1157 nla_put_failure:
1158 nlmsg_cancel(skb, nlh);
1159 return -EMSGSIZE;
1162 /* ---------------------------------------------------------------- */
1164 * Create and broadcast and send it on the standard rtnetlink socket
1165 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1166 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1167 * within a RTM_NEWLINK event.
1169 static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
1171 struct sk_buff *skb;
1172 int err;
1174 if (dev->nd_net != &init_net)
1175 return;
1177 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1178 if (!skb)
1179 return;
1181 err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
1182 if (err < 0) {
1183 WARN_ON(err == -EMSGSIZE);
1184 kfree_skb(skb);
1185 return;
1188 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
1189 skb_queue_tail(&wireless_nlevent_queue, skb);
1190 tasklet_schedule(&wireless_nlevent_tasklet);
1193 /* ---------------------------------------------------------------- */
1195 * Main event dispatcher. Called from other parts and drivers.
1196 * Send the event on the appropriate channels.
1197 * May be called from interrupt context.
1199 void wireless_send_event(struct net_device * dev,
1200 unsigned int cmd,
1201 union iwreq_data * wrqu,
1202 char * extra)
1204 const struct iw_ioctl_description * descr = NULL;
1205 int extra_len = 0;
1206 struct iw_event *event; /* Mallocated whole event */
1207 int event_len; /* Its size */
1208 int hdr_len; /* Size of the event header */
1209 int wrqu_off = 0; /* Offset in wrqu */
1210 /* Don't "optimise" the following variable, it will crash */
1211 unsigned cmd_index; /* *MUST* be unsigned */
1213 /* Get the description of the Event */
1214 if (cmd <= SIOCIWLAST) {
1215 cmd_index = cmd - SIOCIWFIRST;
1216 if (cmd_index < standard_ioctl_num)
1217 descr = &(standard_ioctl[cmd_index]);
1218 } else {
1219 cmd_index = cmd - IWEVFIRST;
1220 if (cmd_index < standard_event_num)
1221 descr = &(standard_event[cmd_index]);
1223 /* Don't accept unknown events */
1224 if (descr == NULL) {
1225 /* Note : we don't return an error to the driver, because
1226 * the driver would not know what to do about it. It can't
1227 * return an error to the user, because the event is not
1228 * initiated by a user request.
1229 * The best the driver could do is to log an error message.
1230 * We will do it ourselves instead...
1232 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
1233 dev->name, cmd);
1234 return;
1237 /* Check extra parameters and set extra_len */
1238 if (descr->header_type == IW_HEADER_TYPE_POINT) {
1239 /* Check if number of token fits within bounds */
1240 if (wrqu->data.length > descr->max_tokens) {
1241 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
1242 return;
1244 if (wrqu->data.length < descr->min_tokens) {
1245 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
1246 return;
1248 /* Calculate extra_len - extra is NULL for restricted events */
1249 if (extra != NULL)
1250 extra_len = wrqu->data.length * descr->token_size;
1251 /* Always at an offset in wrqu */
1252 wrqu_off = IW_EV_POINT_OFF;
1255 /* Total length of the event */
1256 hdr_len = event_type_size[descr->header_type];
1257 event_len = hdr_len + extra_len;
1259 /* Create temporary buffer to hold the event */
1260 event = kmalloc(event_len, GFP_ATOMIC);
1261 if (event == NULL)
1262 return;
1264 /* Fill event */
1265 event->len = event_len;
1266 event->cmd = cmd;
1267 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
1268 if (extra)
1269 memcpy(((char *) event) + hdr_len, extra, extra_len);
1271 /* Send via the RtNetlink event channel */
1272 rtmsg_iwinfo(dev, (char *) event, event_len);
1274 /* Cleanup */
1275 kfree(event);
1277 return; /* Always success, I guess ;-) */
1279 EXPORT_SYMBOL(wireless_send_event);
1281 /********************** ENHANCED IWSPY SUPPORT **********************/
1283 * In the old days, the driver was handling spy support all by itself.
1284 * Now, the driver can delegate this task to Wireless Extensions.
1285 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1286 * push data to us via wireless_spy_update() and include struct iw_spy_data
1287 * in its private part (and export it in net_device->wireless_data->spy_data).
1288 * One of the main advantage of centralising spy support here is that
1289 * it becomes much easier to improve and extend it without having to touch
1290 * the drivers. One example is the addition of the Spy-Threshold events.
1293 /* ---------------------------------------------------------------- */
1295 * Return the pointer to the spy data in the driver.
1296 * Because this is called on the Rx path via wireless_spy_update(),
1297 * we want it to be efficient...
1299 static inline struct iw_spy_data *get_spydata(struct net_device *dev)
1301 /* This is the new way */
1302 if (dev->wireless_data)
1303 return dev->wireless_data->spy_data;
1304 return NULL;
1307 /*------------------------------------------------------------------*/
1309 * Standard Wireless Handler : set Spy List
1311 int iw_handler_set_spy(struct net_device * dev,
1312 struct iw_request_info * info,
1313 union iwreq_data * wrqu,
1314 char * extra)
1316 struct iw_spy_data * spydata = get_spydata(dev);
1317 struct sockaddr * address = (struct sockaddr *) extra;
1319 /* Make sure driver is not buggy or using the old API */
1320 if (!spydata)
1321 return -EOPNOTSUPP;
1323 /* Disable spy collection while we copy the addresses.
1324 * While we copy addresses, any call to wireless_spy_update()
1325 * will NOP. This is OK, as anyway the addresses are changing. */
1326 spydata->spy_number = 0;
1328 /* We want to operate without locking, because wireless_spy_update()
1329 * most likely will happen in the interrupt handler, and therefore
1330 * have its own locking constraints and needs performance.
1331 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1332 * This make sure wireless_spy_update() "see" that the spy list
1333 * is temporarily disabled. */
1334 smp_wmb();
1336 /* Are there are addresses to copy? */
1337 if (wrqu->data.length > 0) {
1338 int i;
1340 /* Copy addresses */
1341 for (i = 0; i < wrqu->data.length; i++)
1342 memcpy(spydata->spy_address[i], address[i].sa_data,
1343 ETH_ALEN);
1344 /* Reset stats */
1345 memset(spydata->spy_stat, 0,
1346 sizeof(struct iw_quality) * IW_MAX_SPY);
1349 /* Make sure above is updated before re-enabling */
1350 smp_wmb();
1352 /* Enable addresses */
1353 spydata->spy_number = wrqu->data.length;
1355 return 0;
1357 EXPORT_SYMBOL(iw_handler_set_spy);
1359 /*------------------------------------------------------------------*/
1361 * Standard Wireless Handler : get Spy List
1363 int iw_handler_get_spy(struct net_device * dev,
1364 struct iw_request_info * info,
1365 union iwreq_data * wrqu,
1366 char * extra)
1368 struct iw_spy_data * spydata = get_spydata(dev);
1369 struct sockaddr * address = (struct sockaddr *) extra;
1370 int i;
1372 /* Make sure driver is not buggy or using the old API */
1373 if (!spydata)
1374 return -EOPNOTSUPP;
1376 wrqu->data.length = spydata->spy_number;
1378 /* Copy addresses. */
1379 for (i = 0; i < spydata->spy_number; i++) {
1380 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1381 address[i].sa_family = AF_UNIX;
1383 /* Copy stats to the user buffer (just after). */
1384 if (spydata->spy_number > 0)
1385 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1386 spydata->spy_stat,
1387 sizeof(struct iw_quality) * spydata->spy_number);
1388 /* Reset updated flags. */
1389 for (i = 0; i < spydata->spy_number; i++)
1390 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
1391 return 0;
1393 EXPORT_SYMBOL(iw_handler_get_spy);
1395 /*------------------------------------------------------------------*/
1397 * Standard Wireless Handler : set spy threshold
1399 int iw_handler_set_thrspy(struct net_device * dev,
1400 struct iw_request_info *info,
1401 union iwreq_data * wrqu,
1402 char * extra)
1404 struct iw_spy_data * spydata = get_spydata(dev);
1405 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1407 /* Make sure driver is not buggy or using the old API */
1408 if (!spydata)
1409 return -EOPNOTSUPP;
1411 /* Just do it */
1412 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1413 2 * sizeof(struct iw_quality));
1415 /* Clear flag */
1416 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1418 return 0;
1420 EXPORT_SYMBOL(iw_handler_set_thrspy);
1422 /*------------------------------------------------------------------*/
1424 * Standard Wireless Handler : get spy threshold
1426 int iw_handler_get_thrspy(struct net_device * dev,
1427 struct iw_request_info *info,
1428 union iwreq_data * wrqu,
1429 char * extra)
1431 struct iw_spy_data * spydata = get_spydata(dev);
1432 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1434 /* Make sure driver is not buggy or using the old API */
1435 if (!spydata)
1436 return -EOPNOTSUPP;
1438 /* Just do it */
1439 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1440 2 * sizeof(struct iw_quality));
1442 return 0;
1444 EXPORT_SYMBOL(iw_handler_get_thrspy);
1446 /*------------------------------------------------------------------*/
1448 * Prepare and send a Spy Threshold event
1450 static void iw_send_thrspy_event(struct net_device * dev,
1451 struct iw_spy_data * spydata,
1452 unsigned char * address,
1453 struct iw_quality * wstats)
1455 union iwreq_data wrqu;
1456 struct iw_thrspy threshold;
1458 /* Init */
1459 wrqu.data.length = 1;
1460 wrqu.data.flags = 0;
1461 /* Copy address */
1462 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1463 threshold.addr.sa_family = ARPHRD_ETHER;
1464 /* Copy stats */
1465 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1466 /* Copy also thresholds */
1467 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1468 2 * sizeof(struct iw_quality));
1470 /* Send event to user space */
1471 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1474 /* ---------------------------------------------------------------- */
1476 * Call for the driver to update the spy data.
1477 * For now, the spy data is a simple array. As the size of the array is
1478 * small, this is good enough. If we wanted to support larger number of
1479 * spy addresses, we should use something more efficient...
1481 void wireless_spy_update(struct net_device * dev,
1482 unsigned char * address,
1483 struct iw_quality * wstats)
1485 struct iw_spy_data * spydata = get_spydata(dev);
1486 int i;
1487 int match = -1;
1489 /* Make sure driver is not buggy or using the old API */
1490 if (!spydata)
1491 return;
1493 /* Update all records that match */
1494 for (i = 0; i < spydata->spy_number; i++)
1495 if (!compare_ether_addr(address, spydata->spy_address[i])) {
1496 memcpy(&(spydata->spy_stat[i]), wstats,
1497 sizeof(struct iw_quality));
1498 match = i;
1501 /* Generate an event if we cross the spy threshold.
1502 * To avoid event storms, we have a simple hysteresis : we generate
1503 * event only when we go under the low threshold or above the
1504 * high threshold. */
1505 if (match >= 0) {
1506 if (spydata->spy_thr_under[match]) {
1507 if (wstats->level > spydata->spy_thr_high.level) {
1508 spydata->spy_thr_under[match] = 0;
1509 iw_send_thrspy_event(dev, spydata,
1510 address, wstats);
1512 } else {
1513 if (wstats->level < spydata->spy_thr_low.level) {
1514 spydata->spy_thr_under[match] = 1;
1515 iw_send_thrspy_event(dev, spydata,
1516 address, wstats);
1521 EXPORT_SYMBOL(wireless_spy_update);