[WEXT]: Clean up how wext is called.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wireless / wext.c
bloba6cf1034e07c0f08cc12d1199fe9cf1a8b314416
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>
97 #include <linux/wireless.h> /* Pretty obvious */
98 #include <net/iw_handler.h> /* New driver API */
99 #include <net/netlink.h>
100 #include <net/wext.h>
102 #include <asm/uaccess.h> /* copy_to_user() */
104 /**************************** CONSTANTS ****************************/
106 /* Debugging stuff */
107 #undef WE_IOCTL_DEBUG /* Debug IOCTL API */
108 #undef WE_EVENT_DEBUG /* Debug Event dispatcher */
109 #undef WE_SPY_DEBUG /* Debug enhanced spy support */
111 /* Options */
112 #define WE_EVENT_RTNETLINK /* Propagate events using RtNetlink */
113 #define WE_SET_EVENT /* Generate an event on some set commands */
115 /************************* GLOBAL VARIABLES *************************/
117 * You should not use global variables, because of re-entrancy.
118 * On our case, it's only const, so it's OK...
121 * Meta-data about all the standard Wireless Extension request we
122 * know about.
124 static const struct iw_ioctl_description standard_ioctl[] = {
125 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
126 .header_type = IW_HEADER_TYPE_NULL,
128 [SIOCGIWNAME - SIOCIWFIRST] = {
129 .header_type = IW_HEADER_TYPE_CHAR,
130 .flags = IW_DESCR_FLAG_DUMP,
132 [SIOCSIWNWID - SIOCIWFIRST] = {
133 .header_type = IW_HEADER_TYPE_PARAM,
134 .flags = IW_DESCR_FLAG_EVENT,
136 [SIOCGIWNWID - SIOCIWFIRST] = {
137 .header_type = IW_HEADER_TYPE_PARAM,
138 .flags = IW_DESCR_FLAG_DUMP,
140 [SIOCSIWFREQ - SIOCIWFIRST] = {
141 .header_type = IW_HEADER_TYPE_FREQ,
142 .flags = IW_DESCR_FLAG_EVENT,
144 [SIOCGIWFREQ - SIOCIWFIRST] = {
145 .header_type = IW_HEADER_TYPE_FREQ,
146 .flags = IW_DESCR_FLAG_DUMP,
148 [SIOCSIWMODE - SIOCIWFIRST] = {
149 .header_type = IW_HEADER_TYPE_UINT,
150 .flags = IW_DESCR_FLAG_EVENT,
152 [SIOCGIWMODE - SIOCIWFIRST] = {
153 .header_type = IW_HEADER_TYPE_UINT,
154 .flags = IW_DESCR_FLAG_DUMP,
156 [SIOCSIWSENS - SIOCIWFIRST] = {
157 .header_type = IW_HEADER_TYPE_PARAM,
159 [SIOCGIWSENS - SIOCIWFIRST] = {
160 .header_type = IW_HEADER_TYPE_PARAM,
162 [SIOCSIWRANGE - SIOCIWFIRST] = {
163 .header_type = IW_HEADER_TYPE_NULL,
165 [SIOCGIWRANGE - SIOCIWFIRST] = {
166 .header_type = IW_HEADER_TYPE_POINT,
167 .token_size = 1,
168 .max_tokens = sizeof(struct iw_range),
169 .flags = IW_DESCR_FLAG_DUMP,
171 [SIOCSIWPRIV - SIOCIWFIRST] = {
172 .header_type = IW_HEADER_TYPE_NULL,
174 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
175 .header_type = IW_HEADER_TYPE_POINT,
176 .token_size = sizeof(struct iw_priv_args),
177 .max_tokens = 16,
178 .flags = IW_DESCR_FLAG_NOMAX,
180 [SIOCSIWSTATS - SIOCIWFIRST] = {
181 .header_type = IW_HEADER_TYPE_NULL,
183 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
184 .header_type = IW_HEADER_TYPE_POINT,
185 .token_size = 1,
186 .max_tokens = sizeof(struct iw_statistics),
187 .flags = IW_DESCR_FLAG_DUMP,
189 [SIOCSIWSPY - SIOCIWFIRST] = {
190 .header_type = IW_HEADER_TYPE_POINT,
191 .token_size = sizeof(struct sockaddr),
192 .max_tokens = IW_MAX_SPY,
194 [SIOCGIWSPY - SIOCIWFIRST] = {
195 .header_type = IW_HEADER_TYPE_POINT,
196 .token_size = sizeof(struct sockaddr) +
197 sizeof(struct iw_quality),
198 .max_tokens = IW_MAX_SPY,
200 [SIOCSIWTHRSPY - SIOCIWFIRST] = {
201 .header_type = IW_HEADER_TYPE_POINT,
202 .token_size = sizeof(struct iw_thrspy),
203 .min_tokens = 1,
204 .max_tokens = 1,
206 [SIOCGIWTHRSPY - SIOCIWFIRST] = {
207 .header_type = IW_HEADER_TYPE_POINT,
208 .token_size = sizeof(struct iw_thrspy),
209 .min_tokens = 1,
210 .max_tokens = 1,
212 [SIOCSIWAP - SIOCIWFIRST] = {
213 .header_type = IW_HEADER_TYPE_ADDR,
215 [SIOCGIWAP - SIOCIWFIRST] = {
216 .header_type = IW_HEADER_TYPE_ADDR,
217 .flags = IW_DESCR_FLAG_DUMP,
219 [SIOCSIWMLME - SIOCIWFIRST] = {
220 .header_type = IW_HEADER_TYPE_POINT,
221 .token_size = 1,
222 .min_tokens = sizeof(struct iw_mlme),
223 .max_tokens = sizeof(struct iw_mlme),
225 [SIOCGIWAPLIST - SIOCIWFIRST] = {
226 .header_type = IW_HEADER_TYPE_POINT,
227 .token_size = sizeof(struct sockaddr) +
228 sizeof(struct iw_quality),
229 .max_tokens = IW_MAX_AP,
230 .flags = IW_DESCR_FLAG_NOMAX,
232 [SIOCSIWSCAN - SIOCIWFIRST] = {
233 .header_type = IW_HEADER_TYPE_POINT,
234 .token_size = 1,
235 .min_tokens = 0,
236 .max_tokens = sizeof(struct iw_scan_req),
238 [SIOCGIWSCAN - SIOCIWFIRST] = {
239 .header_type = IW_HEADER_TYPE_POINT,
240 .token_size = 1,
241 .max_tokens = IW_SCAN_MAX_DATA,
242 .flags = IW_DESCR_FLAG_NOMAX,
244 [SIOCSIWESSID - SIOCIWFIRST] = {
245 .header_type = IW_HEADER_TYPE_POINT,
246 .token_size = 1,
247 .max_tokens = IW_ESSID_MAX_SIZE,
248 .flags = IW_DESCR_FLAG_EVENT,
250 [SIOCGIWESSID - SIOCIWFIRST] = {
251 .header_type = IW_HEADER_TYPE_POINT,
252 .token_size = 1,
253 .max_tokens = IW_ESSID_MAX_SIZE,
254 .flags = IW_DESCR_FLAG_DUMP,
256 [SIOCSIWNICKN - SIOCIWFIRST] = {
257 .header_type = IW_HEADER_TYPE_POINT,
258 .token_size = 1,
259 .max_tokens = IW_ESSID_MAX_SIZE,
261 [SIOCGIWNICKN - SIOCIWFIRST] = {
262 .header_type = IW_HEADER_TYPE_POINT,
263 .token_size = 1,
264 .max_tokens = IW_ESSID_MAX_SIZE,
266 [SIOCSIWRATE - SIOCIWFIRST] = {
267 .header_type = IW_HEADER_TYPE_PARAM,
269 [SIOCGIWRATE - SIOCIWFIRST] = {
270 .header_type = IW_HEADER_TYPE_PARAM,
272 [SIOCSIWRTS - SIOCIWFIRST] = {
273 .header_type = IW_HEADER_TYPE_PARAM,
275 [SIOCGIWRTS - SIOCIWFIRST] = {
276 .header_type = IW_HEADER_TYPE_PARAM,
278 [SIOCSIWFRAG - SIOCIWFIRST] = {
279 .header_type = IW_HEADER_TYPE_PARAM,
281 [SIOCGIWFRAG - SIOCIWFIRST] = {
282 .header_type = IW_HEADER_TYPE_PARAM,
284 [SIOCSIWTXPOW - SIOCIWFIRST] = {
285 .header_type = IW_HEADER_TYPE_PARAM,
287 [SIOCGIWTXPOW - SIOCIWFIRST] = {
288 .header_type = IW_HEADER_TYPE_PARAM,
290 [SIOCSIWRETRY - SIOCIWFIRST] = {
291 .header_type = IW_HEADER_TYPE_PARAM,
293 [SIOCGIWRETRY - SIOCIWFIRST] = {
294 .header_type = IW_HEADER_TYPE_PARAM,
296 [SIOCSIWENCODE - SIOCIWFIRST] = {
297 .header_type = IW_HEADER_TYPE_POINT,
298 .token_size = 1,
299 .max_tokens = IW_ENCODING_TOKEN_MAX,
300 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
302 [SIOCGIWENCODE - SIOCIWFIRST] = {
303 .header_type = IW_HEADER_TYPE_POINT,
304 .token_size = 1,
305 .max_tokens = IW_ENCODING_TOKEN_MAX,
306 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
308 [SIOCSIWPOWER - SIOCIWFIRST] = {
309 .header_type = IW_HEADER_TYPE_PARAM,
311 [SIOCGIWPOWER - SIOCIWFIRST] = {
312 .header_type = IW_HEADER_TYPE_PARAM,
314 [SIOCSIWGENIE - SIOCIWFIRST] = {
315 .header_type = IW_HEADER_TYPE_POINT,
316 .token_size = 1,
317 .max_tokens = IW_GENERIC_IE_MAX,
319 [SIOCGIWGENIE - SIOCIWFIRST] = {
320 .header_type = IW_HEADER_TYPE_POINT,
321 .token_size = 1,
322 .max_tokens = IW_GENERIC_IE_MAX,
324 [SIOCSIWAUTH - SIOCIWFIRST] = {
325 .header_type = IW_HEADER_TYPE_PARAM,
327 [SIOCGIWAUTH - SIOCIWFIRST] = {
328 .header_type = IW_HEADER_TYPE_PARAM,
330 [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
331 .header_type = IW_HEADER_TYPE_POINT,
332 .token_size = 1,
333 .min_tokens = sizeof(struct iw_encode_ext),
334 .max_tokens = sizeof(struct iw_encode_ext) +
335 IW_ENCODING_TOKEN_MAX,
337 [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
338 .header_type = IW_HEADER_TYPE_POINT,
339 .token_size = 1,
340 .min_tokens = sizeof(struct iw_encode_ext),
341 .max_tokens = sizeof(struct iw_encode_ext) +
342 IW_ENCODING_TOKEN_MAX,
344 [SIOCSIWPMKSA - SIOCIWFIRST] = {
345 .header_type = IW_HEADER_TYPE_POINT,
346 .token_size = 1,
347 .min_tokens = sizeof(struct iw_pmksa),
348 .max_tokens = sizeof(struct iw_pmksa),
351 static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
354 * Meta-data about all the additional standard Wireless Extension events
355 * we know about.
357 static const struct iw_ioctl_description standard_event[] = {
358 [IWEVTXDROP - IWEVFIRST] = {
359 .header_type = IW_HEADER_TYPE_ADDR,
361 [IWEVQUAL - IWEVFIRST] = {
362 .header_type = IW_HEADER_TYPE_QUAL,
364 [IWEVCUSTOM - IWEVFIRST] = {
365 .header_type = IW_HEADER_TYPE_POINT,
366 .token_size = 1,
367 .max_tokens = IW_CUSTOM_MAX,
369 [IWEVREGISTERED - IWEVFIRST] = {
370 .header_type = IW_HEADER_TYPE_ADDR,
372 [IWEVEXPIRED - IWEVFIRST] = {
373 .header_type = IW_HEADER_TYPE_ADDR,
375 [IWEVGENIE - IWEVFIRST] = {
376 .header_type = IW_HEADER_TYPE_POINT,
377 .token_size = 1,
378 .max_tokens = IW_GENERIC_IE_MAX,
380 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
381 .header_type = IW_HEADER_TYPE_POINT,
382 .token_size = 1,
383 .max_tokens = sizeof(struct iw_michaelmicfailure),
385 [IWEVASSOCREQIE - IWEVFIRST] = {
386 .header_type = IW_HEADER_TYPE_POINT,
387 .token_size = 1,
388 .max_tokens = IW_GENERIC_IE_MAX,
390 [IWEVASSOCRESPIE - IWEVFIRST] = {
391 .header_type = IW_HEADER_TYPE_POINT,
392 .token_size = 1,
393 .max_tokens = IW_GENERIC_IE_MAX,
395 [IWEVPMKIDCAND - IWEVFIRST] = {
396 .header_type = IW_HEADER_TYPE_POINT,
397 .token_size = 1,
398 .max_tokens = sizeof(struct iw_pmkid_cand),
401 static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
403 /* Size (in bytes) of the various private data types */
404 static const char iw_priv_type_size[] = {
405 0, /* IW_PRIV_TYPE_NONE */
406 1, /* IW_PRIV_TYPE_BYTE */
407 1, /* IW_PRIV_TYPE_CHAR */
408 0, /* Not defined */
409 sizeof(__u32), /* IW_PRIV_TYPE_INT */
410 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
411 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
412 0, /* Not defined */
415 /* Size (in bytes) of various events */
416 static const int event_type_size[] = {
417 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
419 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
421 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
422 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
423 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
425 IW_EV_POINT_LEN, /* Without variable payload */
426 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
427 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
430 /* Size (in bytes) of various events, as packed */
431 static const int event_type_pk_size[] = {
432 IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
434 IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
436 IW_EV_UINT_PK_LEN, /* IW_HEADER_TYPE_UINT */
437 IW_EV_FREQ_PK_LEN, /* IW_HEADER_TYPE_FREQ */
438 IW_EV_ADDR_PK_LEN, /* IW_HEADER_TYPE_ADDR */
440 IW_EV_POINT_PK_LEN, /* Without variable payload */
441 IW_EV_PARAM_PK_LEN, /* IW_HEADER_TYPE_PARAM */
442 IW_EV_QUAL_PK_LEN, /* IW_HEADER_TYPE_QUAL */
445 /************************ COMMON SUBROUTINES ************************/
447 * Stuff that may be used in various place or doesn't fit in one
448 * of the section below.
451 /* ---------------------------------------------------------------- */
453 * Return the driver handler associated with a specific Wireless Extension.
454 * Called from various place, so make sure it remains efficient.
456 static inline iw_handler get_handler(struct net_device *dev,
457 unsigned int cmd)
459 /* Don't "optimise" the following variable, it will crash */
460 unsigned int index; /* *MUST* be unsigned */
462 /* Check if we have some wireless handlers defined */
463 if (dev->wireless_handlers == NULL)
464 return NULL;
466 /* Try as a standard command */
467 index = cmd - SIOCIWFIRST;
468 if (index < dev->wireless_handlers->num_standard)
469 return dev->wireless_handlers->standard[index];
471 /* Try as a private command */
472 index = cmd - SIOCIWFIRSTPRIV;
473 if (index < dev->wireless_handlers->num_private)
474 return dev->wireless_handlers->private[index];
476 /* Not found */
477 return NULL;
480 /* ---------------------------------------------------------------- */
482 * Get statistics out of the driver
484 static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
486 /* New location */
487 if ((dev->wireless_handlers != NULL) &&
488 (dev->wireless_handlers->get_wireless_stats != NULL))
489 return dev->wireless_handlers->get_wireless_stats(dev);
491 /* Not found */
492 return (struct iw_statistics *) NULL;
495 /* ---------------------------------------------------------------- */
497 * Call the commit handler in the driver
498 * (if exist and if conditions are right)
500 * Note : our current commit strategy is currently pretty dumb,
501 * but we will be able to improve on that...
502 * The goal is to try to agreagate as many changes as possible
503 * before doing the commit. Drivers that will define a commit handler
504 * are usually those that need a reset after changing parameters, so
505 * we want to minimise the number of reset.
506 * A cool idea is to use a timer : at each "set" command, we re-set the
507 * timer, when the timer eventually fires, we call the driver.
508 * Hopefully, more on that later.
510 * Also, I'm waiting to see how many people will complain about the
511 * netif_running(dev) test. I'm open on that one...
512 * Hopefully, the driver will remember to do a commit in "open()" ;-)
514 static inline int call_commit_handler(struct net_device * dev)
516 if ((netif_running(dev)) &&
517 (dev->wireless_handlers->standard[0] != NULL)) {
518 /* Call the commit handler on the driver */
519 return dev->wireless_handlers->standard[0](dev, NULL,
520 NULL, NULL);
521 } else
522 return 0; /* Command completed successfully */
525 /* ---------------------------------------------------------------- */
527 * Calculate size of private arguments
529 static inline int get_priv_size(__u16 args)
531 int num = args & IW_PRIV_SIZE_MASK;
532 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
534 return num * iw_priv_type_size[type];
537 /* ---------------------------------------------------------------- */
539 * Re-calculate the size of private arguments
541 static inline int adjust_priv_size(__u16 args,
542 union iwreq_data * wrqu)
544 int num = wrqu->data.length;
545 int max = args & IW_PRIV_SIZE_MASK;
546 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
548 /* Make sure the driver doesn't goof up */
549 if (max < num)
550 num = max;
552 return num * iw_priv_type_size[type];
555 /* ---------------------------------------------------------------- */
557 * Standard Wireless Handler : get wireless stats
558 * Allow programatic access to /proc/net/wireless even if /proc
559 * doesn't exist... Also more efficient...
561 static int iw_handler_get_iwstats(struct net_device * dev,
562 struct iw_request_info * info,
563 union iwreq_data * wrqu,
564 char * extra)
566 /* Get stats from the driver */
567 struct iw_statistics *stats;
569 stats = get_wireless_stats(dev);
570 if (stats != (struct iw_statistics *) NULL) {
572 /* Copy statistics to extra */
573 memcpy(extra, stats, sizeof(struct iw_statistics));
574 wrqu->data.length = sizeof(struct iw_statistics);
576 /* Check if we need to clear the updated flag */
577 if (wrqu->data.flags != 0)
578 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
579 return 0;
580 } else
581 return -EOPNOTSUPP;
584 /* ---------------------------------------------------------------- */
586 * Standard Wireless Handler : get iwpriv definitions
587 * Export the driver private handler definition
588 * They will be picked up by tools like iwpriv...
590 static int iw_handler_get_private(struct net_device * dev,
591 struct iw_request_info * info,
592 union iwreq_data * wrqu,
593 char * extra)
595 /* Check if the driver has something to export */
596 if ((dev->wireless_handlers->num_private_args == 0) ||
597 (dev->wireless_handlers->private_args == NULL))
598 return -EOPNOTSUPP;
600 /* Check if there is enough buffer up there */
601 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
602 /* User space can't know in advance how large the buffer
603 * needs to be. Give it a hint, so that we can support
604 * any size buffer we want somewhat efficiently... */
605 wrqu->data.length = dev->wireless_handlers->num_private_args;
606 return -E2BIG;
609 /* Set the number of available ioctls. */
610 wrqu->data.length = dev->wireless_handlers->num_private_args;
612 /* Copy structure to the user buffer. */
613 memcpy(extra, dev->wireless_handlers->private_args,
614 sizeof(struct iw_priv_args) * wrqu->data.length);
616 return 0;
620 /******************** /proc/net/wireless SUPPORT ********************/
622 * The /proc/net/wireless file is a human readable user-space interface
623 * exporting various wireless specific statistics from the wireless devices.
624 * This is the most popular part of the Wireless Extensions ;-)
626 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
627 * The content of the file is basically the content of "struct iw_statistics".
630 #ifdef CONFIG_PROC_FS
632 /* ---------------------------------------------------------------- */
634 * Print one entry (line) of /proc/net/wireless
636 static __inline__ void wireless_seq_printf_stats(struct seq_file *seq,
637 struct net_device *dev)
639 /* Get stats from the driver */
640 struct iw_statistics *stats = get_wireless_stats(dev);
642 if (stats) {
643 seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
644 "%6d %6d %6d\n",
645 dev->name, stats->status, stats->qual.qual,
646 stats->qual.updated & IW_QUAL_QUAL_UPDATED
647 ? '.' : ' ',
648 ((__s32) stats->qual.level) -
649 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
650 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
651 ? '.' : ' ',
652 ((__s32) stats->qual.noise) -
653 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
654 stats->qual.updated & IW_QUAL_NOISE_UPDATED
655 ? '.' : ' ',
656 stats->discard.nwid, stats->discard.code,
657 stats->discard.fragment, stats->discard.retries,
658 stats->discard.misc, stats->miss.beacon);
659 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
663 /* ---------------------------------------------------------------- */
665 * Print info for /proc/net/wireless (print all entries)
667 static int wireless_seq_show(struct seq_file *seq, void *v)
669 if (v == SEQ_START_TOKEN)
670 seq_printf(seq, "Inter-| sta-| Quality | Discarded "
671 "packets | Missed | WE\n"
672 " face | tus | link level noise | nwid "
673 "crypt frag retry misc | beacon | %d\n",
674 WIRELESS_EXT);
675 else
676 wireless_seq_printf_stats(seq, v);
677 return 0;
680 static const struct seq_operations wireless_seq_ops = {
681 .start = dev_seq_start,
682 .next = dev_seq_next,
683 .stop = dev_seq_stop,
684 .show = wireless_seq_show,
687 static int wireless_seq_open(struct inode *inode, struct file *file)
689 return seq_open(file, &wireless_seq_ops);
692 static const struct file_operations wireless_seq_fops = {
693 .owner = THIS_MODULE,
694 .open = wireless_seq_open,
695 .read = seq_read,
696 .llseek = seq_lseek,
697 .release = seq_release,
700 int __init wext_proc_init(void)
702 /* Create /proc/net/wireless entry */
703 if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
704 return -ENOMEM;
706 return 0;
708 #endif /* CONFIG_PROC_FS */
710 /************************** IOCTL SUPPORT **************************/
712 * The original user space API to configure all those Wireless Extensions
713 * is through IOCTLs.
714 * In there, we check if we need to call the new driver API (iw_handler)
715 * or just call the driver ioctl handler.
718 /* ---------------------------------------------------------------- */
720 * Wrapper to call a standard Wireless Extension handler.
721 * We do various checks and also take care of moving data between
722 * user space and kernel space.
724 static int ioctl_standard_call(struct net_device * dev,
725 struct ifreq * ifr,
726 unsigned int cmd,
727 iw_handler handler)
729 struct iwreq * iwr = (struct iwreq *) ifr;
730 const struct iw_ioctl_description * descr;
731 struct iw_request_info info;
732 int ret = -EINVAL;
734 /* Get the description of the IOCTL */
735 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
736 return -EOPNOTSUPP;
737 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
739 #ifdef WE_IOCTL_DEBUG
740 printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
741 ifr->ifr_name, cmd);
742 printk(KERN_DEBUG "%s (WE) : Header type : %d, Token type : %d, size : %d, token : %d\n", dev->name, descr->header_type, descr->token_type, descr->token_size, descr->max_tokens);
743 #endif /* WE_IOCTL_DEBUG */
745 /* Prepare the call */
746 info.cmd = cmd;
747 info.flags = 0;
749 /* Check if we have a pointer to user space data or not */
750 if (descr->header_type != IW_HEADER_TYPE_POINT) {
752 /* No extra arguments. Trivial to handle */
753 ret = handler(dev, &info, &(iwr->u), NULL);
755 #ifdef WE_SET_EVENT
756 /* Generate an event to notify listeners of the change */
757 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
758 ((ret == 0) || (ret == -EIWCOMMIT)))
759 wireless_send_event(dev, cmd, &(iwr->u), NULL);
760 #endif /* WE_SET_EVENT */
761 } else {
762 char * extra;
763 int extra_size;
764 int user_length = 0;
765 int err;
766 int essid_compat = 0;
768 /* Calculate space needed by arguments. Always allocate
769 * for max space. Easier, and won't last long... */
770 extra_size = descr->max_tokens * descr->token_size;
772 /* Check need for ESSID compatibility for WE < 21 */
773 switch (cmd) {
774 case SIOCSIWESSID:
775 case SIOCGIWESSID:
776 case SIOCSIWNICKN:
777 case SIOCGIWNICKN:
778 if (iwr->u.data.length == descr->max_tokens + 1)
779 essid_compat = 1;
780 else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
781 char essid[IW_ESSID_MAX_SIZE + 1];
783 err = copy_from_user(essid, iwr->u.data.pointer,
784 iwr->u.data.length *
785 descr->token_size);
786 if (err)
787 return -EFAULT;
789 if (essid[iwr->u.data.length - 1] == '\0')
790 essid_compat = 1;
792 break;
793 default:
794 break;
797 iwr->u.data.length -= essid_compat;
799 /* Check what user space is giving us */
800 if (IW_IS_SET(cmd)) {
801 /* Check NULL pointer */
802 if ((iwr->u.data.pointer == NULL) &&
803 (iwr->u.data.length != 0))
804 return -EFAULT;
805 /* Check if number of token fits within bounds */
806 if (iwr->u.data.length > descr->max_tokens)
807 return -E2BIG;
808 if (iwr->u.data.length < descr->min_tokens)
809 return -EINVAL;
810 } else {
811 /* Check NULL pointer */
812 if (iwr->u.data.pointer == NULL)
813 return -EFAULT;
814 /* Save user space buffer size for checking */
815 user_length = iwr->u.data.length;
817 /* Don't check if user_length > max to allow forward
818 * compatibility. The test user_length < min is
819 * implied by the test at the end. */
821 /* Support for very large requests */
822 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
823 (user_length > descr->max_tokens)) {
824 /* Allow userspace to GET more than max so
825 * we can support any size GET requests.
826 * There is still a limit : -ENOMEM. */
827 extra_size = user_length * descr->token_size;
828 /* Note : user_length is originally a __u16,
829 * and token_size is controlled by us,
830 * so extra_size won't get negative and
831 * won't overflow... */
835 #ifdef WE_IOCTL_DEBUG
836 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
837 dev->name, extra_size);
838 #endif /* WE_IOCTL_DEBUG */
840 /* Create the kernel buffer */
841 /* kzalloc ensures NULL-termination for essid_compat */
842 extra = kzalloc(extra_size, GFP_KERNEL);
843 if (extra == NULL) {
844 return -ENOMEM;
847 /* If it is a SET, get all the extra data in here */
848 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
849 err = copy_from_user(extra, iwr->u.data.pointer,
850 iwr->u.data.length *
851 descr->token_size);
852 if (err) {
853 kfree(extra);
854 return -EFAULT;
856 #ifdef WE_IOCTL_DEBUG
857 printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
858 dev->name,
859 iwr->u.data.length * descr->token_size);
860 #endif /* WE_IOCTL_DEBUG */
863 /* Call the handler */
864 ret = handler(dev, &info, &(iwr->u), extra);
866 iwr->u.data.length += essid_compat;
868 /* If we have something to return to the user */
869 if (!ret && IW_IS_GET(cmd)) {
870 /* Check if there is enough buffer up there */
871 if (user_length < iwr->u.data.length) {
872 kfree(extra);
873 return -E2BIG;
876 err = copy_to_user(iwr->u.data.pointer, extra,
877 iwr->u.data.length *
878 descr->token_size);
879 if (err)
880 ret = -EFAULT;
881 #ifdef WE_IOCTL_DEBUG
882 printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
883 dev->name,
884 iwr->u.data.length * descr->token_size);
885 #endif /* WE_IOCTL_DEBUG */
888 #ifdef WE_SET_EVENT
889 /* Generate an event to notify listeners of the change */
890 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
891 ((ret == 0) || (ret == -EIWCOMMIT))) {
892 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
893 /* If the event is restricted, don't
894 * export the payload */
895 wireless_send_event(dev, cmd, &(iwr->u), NULL);
896 else
897 wireless_send_event(dev, cmd, &(iwr->u),
898 extra);
900 #endif /* WE_SET_EVENT */
902 /* Cleanup - I told you it wasn't that long ;-) */
903 kfree(extra);
906 /* Call commit handler if needed and defined */
907 if (ret == -EIWCOMMIT)
908 ret = call_commit_handler(dev);
910 /* Here, we will generate the appropriate event if needed */
912 return ret;
915 /* ---------------------------------------------------------------- */
917 * Wrapper to call a private Wireless Extension handler.
918 * We do various checks and also take care of moving data between
919 * user space and kernel space.
920 * It's not as nice and slimline as the standard wrapper. The cause
921 * is struct iw_priv_args, which was not really designed for the
922 * job we are going here.
924 * IMPORTANT : This function prevent to set and get data on the same
925 * IOCTL and enforce the SET/GET convention. Not doing it would be
926 * far too hairy...
927 * If you need to set and get data at the same time, please don't use
928 * a iw_handler but process it in your ioctl handler (i.e. use the
929 * old driver API).
931 static inline int ioctl_private_call(struct net_device * dev,
932 struct ifreq * ifr,
933 unsigned int cmd,
934 iw_handler handler)
936 struct iwreq * iwr = (struct iwreq *) ifr;
937 const struct iw_priv_args * descr = NULL;
938 struct iw_request_info info;
939 int extra_size = 0;
940 int i;
941 int ret = -EINVAL;
943 /* Get the description of the IOCTL */
944 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
945 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
946 descr = &(dev->wireless_handlers->private_args[i]);
947 break;
950 #ifdef WE_IOCTL_DEBUG
951 printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
952 ifr->ifr_name, cmd);
953 if (descr) {
954 printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
955 dev->name, descr->name,
956 descr->set_args, descr->get_args);
958 #endif /* WE_IOCTL_DEBUG */
960 /* Compute the size of the set/get arguments */
961 if (descr != NULL) {
962 if (IW_IS_SET(cmd)) {
963 int offset = 0; /* For sub-ioctls */
964 /* Check for sub-ioctl handler */
965 if (descr->name[0] == '\0')
966 /* Reserve one int for sub-ioctl index */
967 offset = sizeof(__u32);
969 /* Size of set arguments */
970 extra_size = get_priv_size(descr->set_args);
972 /* Does it fits in iwr ? */
973 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
974 ((extra_size + offset) <= IFNAMSIZ))
975 extra_size = 0;
976 } else {
977 /* Size of get arguments */
978 extra_size = get_priv_size(descr->get_args);
980 /* Does it fits in iwr ? */
981 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
982 (extra_size <= IFNAMSIZ))
983 extra_size = 0;
987 /* Prepare the call */
988 info.cmd = cmd;
989 info.flags = 0;
991 /* Check if we have a pointer to user space data or not. */
992 if (extra_size == 0) {
993 /* No extra arguments. Trivial to handle */
994 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
995 } else {
996 char * extra;
997 int err;
999 /* Check what user space is giving us */
1000 if (IW_IS_SET(cmd)) {
1001 /* Check NULL pointer */
1002 if ((iwr->u.data.pointer == NULL) &&
1003 (iwr->u.data.length != 0))
1004 return -EFAULT;
1006 /* Does it fits within bounds ? */
1007 if (iwr->u.data.length > (descr->set_args &
1008 IW_PRIV_SIZE_MASK))
1009 return -E2BIG;
1010 } else {
1011 /* Check NULL pointer */
1012 if (iwr->u.data.pointer == NULL)
1013 return -EFAULT;
1016 #ifdef WE_IOCTL_DEBUG
1017 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
1018 dev->name, extra_size);
1019 #endif /* WE_IOCTL_DEBUG */
1021 /* Always allocate for max space. Easier, and won't last
1022 * long... */
1023 extra = kmalloc(extra_size, GFP_KERNEL);
1024 if (extra == NULL) {
1025 return -ENOMEM;
1028 /* If it is a SET, get all the extra data in here */
1029 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
1030 err = copy_from_user(extra, iwr->u.data.pointer,
1031 extra_size);
1032 if (err) {
1033 kfree(extra);
1034 return -EFAULT;
1036 #ifdef WE_IOCTL_DEBUG
1037 printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
1038 dev->name, iwr->u.data.length);
1039 #endif /* WE_IOCTL_DEBUG */
1042 /* Call the handler */
1043 ret = handler(dev, &info, &(iwr->u), extra);
1045 /* If we have something to return to the user */
1046 if (!ret && IW_IS_GET(cmd)) {
1048 /* Adjust for the actual length if it's variable,
1049 * avoid leaking kernel bits outside. */
1050 if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
1051 extra_size = adjust_priv_size(descr->get_args,
1052 &(iwr->u));
1055 err = copy_to_user(iwr->u.data.pointer, extra,
1056 extra_size);
1057 if (err)
1058 ret = -EFAULT;
1059 #ifdef WE_IOCTL_DEBUG
1060 printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
1061 dev->name, iwr->u.data.length);
1062 #endif /* WE_IOCTL_DEBUG */
1065 /* Cleanup - I told you it wasn't that long ;-) */
1066 kfree(extra);
1070 /* Call commit handler if needed and defined */
1071 if (ret == -EIWCOMMIT)
1072 ret = call_commit_handler(dev);
1074 return ret;
1077 /* ---------------------------------------------------------------- */
1079 * Main IOCTl dispatcher.
1080 * Check the type of IOCTL and call the appropriate wrapper...
1082 static int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
1084 struct net_device *dev;
1085 iw_handler handler;
1087 /* Permissions are already checked in dev_ioctl() before calling us.
1088 * The copy_to/from_user() of ifr is also dealt with in there */
1090 /* Make sure the device exist */
1091 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
1092 return -ENODEV;
1094 /* A bunch of special cases, then the generic case...
1095 * Note that 'cmd' is already filtered in dev_ioctl() with
1096 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
1097 switch (cmd) {
1098 case SIOCGIWSTATS:
1099 /* Get Wireless Stats */
1100 return ioctl_standard_call(dev,
1101 ifr,
1102 cmd,
1103 &iw_handler_get_iwstats);
1105 case SIOCGIWPRIV:
1106 /* Check if we have some wireless handlers defined */
1107 if (dev->wireless_handlers != NULL) {
1108 /* We export to user space the definition of
1109 * the private handler ourselves */
1110 return ioctl_standard_call(dev,
1111 ifr,
1112 cmd,
1113 &iw_handler_get_private);
1115 // ## Fall-through for old API ##
1116 default:
1117 /* Generic IOCTL */
1118 /* Basic check */
1119 if (!netif_device_present(dev))
1120 return -ENODEV;
1121 /* New driver API : try to find the handler */
1122 handler = get_handler(dev, cmd);
1123 if (handler != NULL) {
1124 /* Standard and private are not the same */
1125 if (cmd < SIOCIWFIRSTPRIV)
1126 return ioctl_standard_call(dev,
1127 ifr,
1128 cmd,
1129 handler);
1130 else
1131 return ioctl_private_call(dev,
1132 ifr,
1133 cmd,
1134 handler);
1136 /* Old driver API : call driver ioctl handler */
1137 if (dev->do_ioctl) {
1138 return dev->do_ioctl(dev, ifr, cmd);
1140 return -EOPNOTSUPP;
1142 /* Not reached */
1143 return -EINVAL;
1146 /* entry point from dev ioctl */
1147 int wext_handle_ioctl(struct ifreq *ifr, unsigned int cmd,
1148 void __user *arg)
1150 int ret;
1152 /* If command is `set a parameter', or
1153 * `get the encoding parameters', check if
1154 * the user has the right to do it */
1155 if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1156 if (!capable(CAP_NET_ADMIN))
1157 return -EPERM;
1158 dev_load(ifr->ifr_name);
1159 rtnl_lock();
1160 ret = wireless_process_ioctl(ifr, cmd);
1161 rtnl_unlock();
1162 if (IW_IS_GET(cmd) && copy_to_user(arg, ifr, sizeof(struct ifreq)))
1163 return -EFAULT;
1164 return ret;
1167 /************************* EVENT PROCESSING *************************/
1169 * Process events generated by the wireless layer or the driver.
1170 * Most often, the event will be propagated through rtnetlink
1173 #ifdef WE_EVENT_RTNETLINK
1174 /* ---------------------------------------------------------------- */
1176 * Locking...
1177 * ----------
1179 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1180 * the locking issue in here and implementing this code !
1182 * The issue : wireless_send_event() is often called in interrupt context,
1183 * while the Netlink layer can never be called in interrupt context.
1184 * The fully formed RtNetlink events are queued, and then a tasklet is run
1185 * to feed those to Netlink.
1186 * The skb_queue is interrupt safe, and its lock is not held while calling
1187 * Netlink, so there is no possibility of dealock.
1188 * Jean II
1191 static struct sk_buff_head wireless_nlevent_queue;
1193 static int __init wireless_nlevent_init(void)
1195 skb_queue_head_init(&wireless_nlevent_queue);
1196 return 0;
1199 subsys_initcall(wireless_nlevent_init);
1201 static void wireless_nlevent_process(unsigned long data)
1203 struct sk_buff *skb;
1205 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
1206 rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
1209 static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1211 /* ---------------------------------------------------------------- */
1213 * Fill a rtnetlink message with our event data.
1214 * Note that we propage only the specified event and don't dump the
1215 * current wireless config. Dumping the wireless config is far too
1216 * expensive (for each parameter, the driver need to query the hardware).
1218 static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
1219 struct net_device * dev,
1220 int type,
1221 char * event,
1222 int event_len)
1224 struct ifinfomsg *r;
1225 struct nlmsghdr *nlh;
1226 unsigned char *b = skb_tail_pointer(skb);
1228 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
1229 r = NLMSG_DATA(nlh);
1230 r->ifi_family = AF_UNSPEC;
1231 r->__ifi_pad = 0;
1232 r->ifi_type = dev->type;
1233 r->ifi_index = dev->ifindex;
1234 r->ifi_flags = dev_get_flags(dev);
1235 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1237 /* Add the wireless events in the netlink packet */
1238 RTA_PUT(skb, IFLA_WIRELESS, event_len, event);
1240 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1241 return skb->len;
1243 nlmsg_failure:
1244 rtattr_failure:
1245 nlmsg_trim(skb, b);
1246 return -1;
1249 /* ---------------------------------------------------------------- */
1251 * Create and broadcast and send it on the standard rtnetlink socket
1252 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1253 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1254 * within a RTM_NEWLINK event.
1256 static inline void rtmsg_iwinfo(struct net_device * dev,
1257 char * event,
1258 int event_len)
1260 struct sk_buff *skb;
1261 int size = NLMSG_GOODSIZE;
1263 skb = alloc_skb(size, GFP_ATOMIC);
1264 if (!skb)
1265 return;
1267 if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
1268 event, event_len) < 0) {
1269 kfree_skb(skb);
1270 return;
1272 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
1273 skb_queue_tail(&wireless_nlevent_queue, skb);
1274 tasklet_schedule(&wireless_nlevent_tasklet);
1277 #endif /* WE_EVENT_RTNETLINK */
1279 /* ---------------------------------------------------------------- */
1281 * Main event dispatcher. Called from other parts and drivers.
1282 * Send the event on the appropriate channels.
1283 * May be called from interrupt context.
1285 void wireless_send_event(struct net_device * dev,
1286 unsigned int cmd,
1287 union iwreq_data * wrqu,
1288 char * extra)
1290 const struct iw_ioctl_description * descr = NULL;
1291 int extra_len = 0;
1292 struct iw_event *event; /* Mallocated whole event */
1293 int event_len; /* Its size */
1294 int hdr_len; /* Size of the event header */
1295 int wrqu_off = 0; /* Offset in wrqu */
1296 /* Don't "optimise" the following variable, it will crash */
1297 unsigned cmd_index; /* *MUST* be unsigned */
1299 /* Get the description of the Event */
1300 if (cmd <= SIOCIWLAST) {
1301 cmd_index = cmd - SIOCIWFIRST;
1302 if (cmd_index < standard_ioctl_num)
1303 descr = &(standard_ioctl[cmd_index]);
1304 } else {
1305 cmd_index = cmd - IWEVFIRST;
1306 if (cmd_index < standard_event_num)
1307 descr = &(standard_event[cmd_index]);
1309 /* Don't accept unknown events */
1310 if (descr == NULL) {
1311 /* Note : we don't return an error to the driver, because
1312 * the driver would not know what to do about it. It can't
1313 * return an error to the user, because the event is not
1314 * initiated by a user request.
1315 * The best the driver could do is to log an error message.
1316 * We will do it ourselves instead...
1318 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
1319 dev->name, cmd);
1320 return;
1322 #ifdef WE_EVENT_DEBUG
1323 printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
1324 dev->name, cmd);
1325 printk(KERN_DEBUG "%s (WE) : Header type : %d, Token type : %d, size : %d, token : %d\n", dev->name, descr->header_type, descr->token_type, descr->token_size, descr->max_tokens);
1326 #endif /* WE_EVENT_DEBUG */
1328 /* Check extra parameters and set extra_len */
1329 if (descr->header_type == IW_HEADER_TYPE_POINT) {
1330 /* Check if number of token fits within bounds */
1331 if (wrqu->data.length > descr->max_tokens) {
1332 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
1333 return;
1335 if (wrqu->data.length < descr->min_tokens) {
1336 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
1337 return;
1339 /* Calculate extra_len - extra is NULL for restricted events */
1340 if (extra != NULL)
1341 extra_len = wrqu->data.length * descr->token_size;
1342 /* Always at an offset in wrqu */
1343 wrqu_off = IW_EV_POINT_OFF;
1344 #ifdef WE_EVENT_DEBUG
1345 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
1346 #endif /* WE_EVENT_DEBUG */
1349 /* Total length of the event */
1350 hdr_len = event_type_size[descr->header_type];
1351 event_len = hdr_len + extra_len;
1353 #ifdef WE_EVENT_DEBUG
1354 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, hdr_len %d, wrqu_off %d, event_len %d\n", dev->name, cmd, hdr_len, wrqu_off, event_len);
1355 #endif /* WE_EVENT_DEBUG */
1357 /* Create temporary buffer to hold the event */
1358 event = kmalloc(event_len, GFP_ATOMIC);
1359 if (event == NULL)
1360 return;
1362 /* Fill event */
1363 event->len = event_len;
1364 event->cmd = cmd;
1365 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
1366 if (extra != NULL)
1367 memcpy(((char *) event) + hdr_len, extra, extra_len);
1369 #ifdef WE_EVENT_RTNETLINK
1370 /* Send via the RtNetlink event channel */
1371 rtmsg_iwinfo(dev, (char *) event, event_len);
1372 #endif /* WE_EVENT_RTNETLINK */
1374 /* Cleanup */
1375 kfree(event);
1377 return; /* Always success, I guess ;-) */
1380 /********************** ENHANCED IWSPY SUPPORT **********************/
1382 * In the old days, the driver was handling spy support all by itself.
1383 * Now, the driver can delegate this task to Wireless Extensions.
1384 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1385 * push data to us via wireless_spy_update() and include struct iw_spy_data
1386 * in its private part (and export it in net_device->wireless_data->spy_data).
1387 * One of the main advantage of centralising spy support here is that
1388 * it becomes much easier to improve and extend it without having to touch
1389 * the drivers. One example is the addition of the Spy-Threshold events.
1392 /* ---------------------------------------------------------------- */
1394 * Return the pointer to the spy data in the driver.
1395 * Because this is called on the Rx path via wireless_spy_update(),
1396 * we want it to be efficient...
1398 static inline struct iw_spy_data * get_spydata(struct net_device *dev)
1400 /* This is the new way */
1401 if (dev->wireless_data)
1402 return(dev->wireless_data->spy_data);
1403 return NULL;
1406 /*------------------------------------------------------------------*/
1408 * Standard Wireless Handler : set Spy List
1410 int iw_handler_set_spy(struct net_device * dev,
1411 struct iw_request_info * info,
1412 union iwreq_data * wrqu,
1413 char * extra)
1415 struct iw_spy_data * spydata = get_spydata(dev);
1416 struct sockaddr * address = (struct sockaddr *) extra;
1418 /* Make sure driver is not buggy or using the old API */
1419 if (!spydata)
1420 return -EOPNOTSUPP;
1422 /* Disable spy collection while we copy the addresses.
1423 * While we copy addresses, any call to wireless_spy_update()
1424 * will NOP. This is OK, as anyway the addresses are changing. */
1425 spydata->spy_number = 0;
1427 /* We want to operate without locking, because wireless_spy_update()
1428 * most likely will happen in the interrupt handler, and therefore
1429 * have its own locking constraints and needs performance.
1430 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1431 * This make sure wireless_spy_update() "see" that the spy list
1432 * is temporarily disabled. */
1433 smp_wmb();
1435 /* Are there are addresses to copy? */
1436 if (wrqu->data.length > 0) {
1437 int i;
1439 /* Copy addresses */
1440 for (i = 0; i < wrqu->data.length; i++)
1441 memcpy(spydata->spy_address[i], address[i].sa_data,
1442 ETH_ALEN);
1443 /* Reset stats */
1444 memset(spydata->spy_stat, 0,
1445 sizeof(struct iw_quality) * IW_MAX_SPY);
1447 #ifdef WE_SPY_DEBUG
1448 printk(KERN_DEBUG "iw_handler_set_spy() : wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length);
1449 for (i = 0; i < wrqu->data.length; i++)
1450 printk(KERN_DEBUG
1451 "%02X:%02X:%02X:%02X:%02X:%02X \n",
1452 spydata->spy_address[i][0],
1453 spydata->spy_address[i][1],
1454 spydata->spy_address[i][2],
1455 spydata->spy_address[i][3],
1456 spydata->spy_address[i][4],
1457 spydata->spy_address[i][5]);
1458 #endif /* WE_SPY_DEBUG */
1461 /* Make sure above is updated before re-enabling */
1462 smp_wmb();
1464 /* Enable addresses */
1465 spydata->spy_number = wrqu->data.length;
1467 return 0;
1470 /*------------------------------------------------------------------*/
1472 * Standard Wireless Handler : get Spy List
1474 int iw_handler_get_spy(struct net_device * dev,
1475 struct iw_request_info * info,
1476 union iwreq_data * wrqu,
1477 char * extra)
1479 struct iw_spy_data * spydata = get_spydata(dev);
1480 struct sockaddr * address = (struct sockaddr *) extra;
1481 int i;
1483 /* Make sure driver is not buggy or using the old API */
1484 if (!spydata)
1485 return -EOPNOTSUPP;
1487 wrqu->data.length = spydata->spy_number;
1489 /* Copy addresses. */
1490 for (i = 0; i < spydata->spy_number; i++) {
1491 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1492 address[i].sa_family = AF_UNIX;
1494 /* Copy stats to the user buffer (just after). */
1495 if (spydata->spy_number > 0)
1496 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1497 spydata->spy_stat,
1498 sizeof(struct iw_quality) * spydata->spy_number);
1499 /* Reset updated flags. */
1500 for (i = 0; i < spydata->spy_number; i++)
1501 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
1502 return 0;
1505 /*------------------------------------------------------------------*/
1507 * Standard Wireless Handler : set spy threshold
1509 int iw_handler_set_thrspy(struct net_device * dev,
1510 struct iw_request_info *info,
1511 union iwreq_data * wrqu,
1512 char * extra)
1514 struct iw_spy_data * spydata = get_spydata(dev);
1515 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1517 /* Make sure driver is not buggy or using the old API */
1518 if (!spydata)
1519 return -EOPNOTSUPP;
1521 /* Just do it */
1522 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1523 2 * sizeof(struct iw_quality));
1525 /* Clear flag */
1526 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1528 #ifdef WE_SPY_DEBUG
1529 printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
1530 #endif /* WE_SPY_DEBUG */
1532 return 0;
1535 /*------------------------------------------------------------------*/
1537 * Standard Wireless Handler : get spy threshold
1539 int iw_handler_get_thrspy(struct net_device * dev,
1540 struct iw_request_info *info,
1541 union iwreq_data * wrqu,
1542 char * extra)
1544 struct iw_spy_data * spydata = get_spydata(dev);
1545 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1547 /* Make sure driver is not buggy or using the old API */
1548 if (!spydata)
1549 return -EOPNOTSUPP;
1551 /* Just do it */
1552 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1553 2 * sizeof(struct iw_quality));
1555 return 0;
1558 /*------------------------------------------------------------------*/
1560 * Prepare and send a Spy Threshold event
1562 static void iw_send_thrspy_event(struct net_device * dev,
1563 struct iw_spy_data * spydata,
1564 unsigned char * address,
1565 struct iw_quality * wstats)
1567 union iwreq_data wrqu;
1568 struct iw_thrspy threshold;
1570 /* Init */
1571 wrqu.data.length = 1;
1572 wrqu.data.flags = 0;
1573 /* Copy address */
1574 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1575 threshold.addr.sa_family = ARPHRD_ETHER;
1576 /* Copy stats */
1577 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1578 /* Copy also thresholds */
1579 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1580 2 * sizeof(struct iw_quality));
1582 #ifdef WE_SPY_DEBUG
1583 printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
1584 threshold.addr.sa_data[0],
1585 threshold.addr.sa_data[1],
1586 threshold.addr.sa_data[2],
1587 threshold.addr.sa_data[3],
1588 threshold.addr.sa_data[4],
1589 threshold.addr.sa_data[5], threshold.qual.level);
1590 #endif /* WE_SPY_DEBUG */
1592 /* Send event to user space */
1593 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1596 /* ---------------------------------------------------------------- */
1598 * Call for the driver to update the spy data.
1599 * For now, the spy data is a simple array. As the size of the array is
1600 * small, this is good enough. If we wanted to support larger number of
1601 * spy addresses, we should use something more efficient...
1603 void wireless_spy_update(struct net_device * dev,
1604 unsigned char * address,
1605 struct iw_quality * wstats)
1607 struct iw_spy_data * spydata = get_spydata(dev);
1608 int i;
1609 int match = -1;
1611 /* Make sure driver is not buggy or using the old API */
1612 if (!spydata)
1613 return;
1615 #ifdef WE_SPY_DEBUG
1616 printk(KERN_DEBUG "wireless_spy_update() : wireless_data %p, spydata %p, address %02X:%02X:%02X:%02X:%02X:%02X\n", dev->wireless_data, spydata, address[0], address[1], address[2], address[3], address[4], address[5]);
1617 #endif /* WE_SPY_DEBUG */
1619 /* Update all records that match */
1620 for (i = 0; i < spydata->spy_number; i++)
1621 if (!compare_ether_addr(address, spydata->spy_address[i])) {
1622 memcpy(&(spydata->spy_stat[i]), wstats,
1623 sizeof(struct iw_quality));
1624 match = i;
1627 /* Generate an event if we cross the spy threshold.
1628 * To avoid event storms, we have a simple hysteresis : we generate
1629 * event only when we go under the low threshold or above the
1630 * high threshold. */
1631 if (match >= 0) {
1632 if (spydata->spy_thr_under[match]) {
1633 if (wstats->level > spydata->spy_thr_high.level) {
1634 spydata->spy_thr_under[match] = 0;
1635 iw_send_thrspy_event(dev, spydata,
1636 address, wstats);
1638 } else {
1639 if (wstats->level < spydata->spy_thr_low.level) {
1640 spydata->spy_thr_under[match] = 1;
1641 iw_send_thrspy_event(dev, spydata,
1642 address, wstats);
1648 EXPORT_SYMBOL(iw_handler_get_spy);
1649 EXPORT_SYMBOL(iw_handler_get_thrspy);
1650 EXPORT_SYMBOL(iw_handler_set_spy);
1651 EXPORT_SYMBOL(iw_handler_set_thrspy);
1652 EXPORT_SYMBOL(wireless_send_event);
1653 EXPORT_SYMBOL(wireless_spy_update);