wext: remove seq_start/stop sparse annotations
[linux-2.6/x86.git] / net / wireless / wext.c
blobd3bbef70cc7c3624882460569197b10c644c331a
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 lose 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 */
421 /************************ COMMON SUBROUTINES ************************/
423 * Stuff that may be used in various place or doesn't fit in one
424 * of the section below.
427 /* ---------------------------------------------------------------- */
429 * Return the driver handler associated with a specific Wireless Extension.
431 static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
433 /* Don't "optimise" the following variable, it will crash */
434 unsigned int index; /* *MUST* be unsigned */
436 /* Check if we have some wireless handlers defined */
437 if (dev->wireless_handlers == NULL)
438 return NULL;
440 /* Try as a standard command */
441 index = cmd - SIOCIWFIRST;
442 if (index < dev->wireless_handlers->num_standard)
443 return dev->wireless_handlers->standard[index];
445 /* Try as a private command */
446 index = cmd - SIOCIWFIRSTPRIV;
447 if (index < dev->wireless_handlers->num_private)
448 return dev->wireless_handlers->private[index];
450 /* Not found */
451 return NULL;
454 /* ---------------------------------------------------------------- */
456 * Get statistics out of the driver
458 static struct iw_statistics *get_wireless_stats(struct net_device *dev)
460 /* New location */
461 if ((dev->wireless_handlers != NULL) &&
462 (dev->wireless_handlers->get_wireless_stats != NULL))
463 return dev->wireless_handlers->get_wireless_stats(dev);
465 /* Not found */
466 return NULL;
469 /* ---------------------------------------------------------------- */
471 * Call the commit handler in the driver
472 * (if exist and if conditions are right)
474 * Note : our current commit strategy is currently pretty dumb,
475 * but we will be able to improve on that...
476 * The goal is to try to agreagate as many changes as possible
477 * before doing the commit. Drivers that will define a commit handler
478 * are usually those that need a reset after changing parameters, so
479 * we want to minimise the number of reset.
480 * A cool idea is to use a timer : at each "set" command, we re-set the
481 * timer, when the timer eventually fires, we call the driver.
482 * Hopefully, more on that later.
484 * Also, I'm waiting to see how many people will complain about the
485 * netif_running(dev) test. I'm open on that one...
486 * Hopefully, the driver will remember to do a commit in "open()" ;-)
488 static int call_commit_handler(struct net_device *dev)
490 if ((netif_running(dev)) &&
491 (dev->wireless_handlers->standard[0] != NULL))
492 /* Call the commit handler on the driver */
493 return dev->wireless_handlers->standard[0](dev, NULL,
494 NULL, NULL);
495 else
496 return 0; /* Command completed successfully */
499 /* ---------------------------------------------------------------- */
501 * Calculate size of private arguments
503 static int get_priv_size(__u16 args)
505 int num = args & IW_PRIV_SIZE_MASK;
506 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
508 return num * iw_priv_type_size[type];
511 /* ---------------------------------------------------------------- */
513 * Re-calculate the size of private arguments
515 static int adjust_priv_size(__u16 args, struct iw_point *iwp)
517 int num = iwp->length;
518 int max = args & IW_PRIV_SIZE_MASK;
519 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
521 /* Make sure the driver doesn't goof up */
522 if (max < num)
523 num = max;
525 return num * iw_priv_type_size[type];
528 /* ---------------------------------------------------------------- */
530 * Standard Wireless Handler : get wireless stats
531 * Allow programatic access to /proc/net/wireless even if /proc
532 * doesn't exist... Also more efficient...
534 static int iw_handler_get_iwstats(struct net_device * dev,
535 struct iw_request_info * info,
536 union iwreq_data * wrqu,
537 char * extra)
539 /* Get stats from the driver */
540 struct iw_statistics *stats;
542 stats = get_wireless_stats(dev);
543 if (stats) {
544 /* Copy statistics to extra */
545 memcpy(extra, stats, sizeof(struct iw_statistics));
546 wrqu->data.length = sizeof(struct iw_statistics);
548 /* Check if we need to clear the updated flag */
549 if (wrqu->data.flags != 0)
550 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
551 return 0;
552 } else
553 return -EOPNOTSUPP;
556 /* ---------------------------------------------------------------- */
558 * Standard Wireless Handler : get iwpriv definitions
559 * Export the driver private handler definition
560 * They will be picked up by tools like iwpriv...
562 static int iw_handler_get_private(struct net_device * dev,
563 struct iw_request_info * info,
564 union iwreq_data * wrqu,
565 char * extra)
567 /* Check if the driver has something to export */
568 if ((dev->wireless_handlers->num_private_args == 0) ||
569 (dev->wireless_handlers->private_args == NULL))
570 return -EOPNOTSUPP;
572 /* Check if there is enough buffer up there */
573 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
574 /* User space can't know in advance how large the buffer
575 * needs to be. Give it a hint, so that we can support
576 * any size buffer we want somewhat efficiently... */
577 wrqu->data.length = dev->wireless_handlers->num_private_args;
578 return -E2BIG;
581 /* Set the number of available ioctls. */
582 wrqu->data.length = dev->wireless_handlers->num_private_args;
584 /* Copy structure to the user buffer. */
585 memcpy(extra, dev->wireless_handlers->private_args,
586 sizeof(struct iw_priv_args) * wrqu->data.length);
588 return 0;
592 /******************** /proc/net/wireless SUPPORT ********************/
594 * The /proc/net/wireless file is a human readable user-space interface
595 * exporting various wireless specific statistics from the wireless devices.
596 * This is the most popular part of the Wireless Extensions ;-)
598 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
599 * The content of the file is basically the content of "struct iw_statistics".
602 #ifdef CONFIG_PROC_FS
604 /* ---------------------------------------------------------------- */
606 * Print one entry (line) of /proc/net/wireless
608 static void wireless_seq_printf_stats(struct seq_file *seq,
609 struct net_device *dev)
611 /* Get stats from the driver */
612 struct iw_statistics *stats = get_wireless_stats(dev);
614 if (stats) {
615 seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
616 "%6d %6d %6d\n",
617 dev->name, stats->status, stats->qual.qual,
618 stats->qual.updated & IW_QUAL_QUAL_UPDATED
619 ? '.' : ' ',
620 ((__s32) stats->qual.level) -
621 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
622 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
623 ? '.' : ' ',
624 ((__s32) stats->qual.noise) -
625 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
626 stats->qual.updated & IW_QUAL_NOISE_UPDATED
627 ? '.' : ' ',
628 stats->discard.nwid, stats->discard.code,
629 stats->discard.fragment, stats->discard.retries,
630 stats->discard.misc, stats->miss.beacon);
631 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
635 /* ---------------------------------------------------------------- */
637 * Print info for /proc/net/wireless (print all entries)
639 static int wireless_seq_show(struct seq_file *seq, void *v)
641 if (v == SEQ_START_TOKEN)
642 seq_printf(seq, "Inter-| sta-| Quality | Discarded "
643 "packets | Missed | WE\n"
644 " face | tus | link level noise | nwid "
645 "crypt frag retry misc | beacon | %d\n",
646 WIRELESS_EXT);
647 else
648 wireless_seq_printf_stats(seq, v);
649 return 0;
652 static void *wireless_dev_seq_start(struct seq_file *seq, loff_t *pos)
654 rtnl_lock();
655 return dev_seq_start(seq, pos);
658 static void wireless_dev_seq_stop(struct seq_file *seq, void *v)
660 dev_seq_stop(seq, v);
661 rtnl_unlock();
664 static const struct seq_operations wireless_seq_ops = {
665 .start = wireless_dev_seq_start,
666 .next = dev_seq_next,
667 .stop = wireless_dev_seq_stop,
668 .show = wireless_seq_show,
671 static int seq_open_wireless(struct inode *inode, struct file *file)
673 return seq_open_net(inode, file, &wireless_seq_ops,
674 sizeof(struct seq_net_private));
677 static const struct file_operations wireless_seq_fops = {
678 .owner = THIS_MODULE,
679 .open = seq_open_wireless,
680 .read = seq_read,
681 .llseek = seq_lseek,
682 .release = seq_release_net,
685 int wext_proc_init(struct net *net)
687 /* Create /proc/net/wireless entry */
688 if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
689 return -ENOMEM;
691 return 0;
694 void wext_proc_exit(struct net *net)
696 proc_net_remove(net, "wireless");
698 #endif /* CONFIG_PROC_FS */
700 /************************** IOCTL SUPPORT **************************/
702 * The original user space API to configure all those Wireless Extensions
703 * is through IOCTLs.
704 * In there, we check if we need to call the new driver API (iw_handler)
705 * or just call the driver ioctl handler.
708 /* ---------------------------------------------------------------- */
709 static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
710 const struct iw_ioctl_description *descr,
711 iw_handler handler, struct net_device *dev,
712 struct iw_request_info *info)
714 int err, extra_size, user_length = 0, essid_compat = 0;
715 char *extra;
717 /* Calculate space needed by arguments. Always allocate
718 * for max space.
720 extra_size = descr->max_tokens * descr->token_size;
722 /* Check need for ESSID compatibility for WE < 21 */
723 switch (cmd) {
724 case SIOCSIWESSID:
725 case SIOCGIWESSID:
726 case SIOCSIWNICKN:
727 case SIOCGIWNICKN:
728 if (iwp->length == descr->max_tokens + 1)
729 essid_compat = 1;
730 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
731 char essid[IW_ESSID_MAX_SIZE + 1];
733 err = copy_from_user(essid, iwp->pointer,
734 iwp->length *
735 descr->token_size);
736 if (err)
737 return -EFAULT;
739 if (essid[iwp->length - 1] == '\0')
740 essid_compat = 1;
742 break;
743 default:
744 break;
747 iwp->length -= essid_compat;
749 /* Check what user space is giving us */
750 if (IW_IS_SET(cmd)) {
751 /* Check NULL pointer */
752 if (!iwp->pointer && iwp->length != 0)
753 return -EFAULT;
754 /* Check if number of token fits within bounds */
755 if (iwp->length > descr->max_tokens)
756 return -E2BIG;
757 if (iwp->length < descr->min_tokens)
758 return -EINVAL;
759 } else {
760 /* Check NULL pointer */
761 if (!iwp->pointer)
762 return -EFAULT;
763 /* Save user space buffer size for checking */
764 user_length = iwp->length;
766 /* Don't check if user_length > max to allow forward
767 * compatibility. The test user_length < min is
768 * implied by the test at the end.
771 /* Support for very large requests */
772 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
773 (user_length > descr->max_tokens)) {
774 /* Allow userspace to GET more than max so
775 * we can support any size GET requests.
776 * There is still a limit : -ENOMEM.
778 extra_size = user_length * descr->token_size;
780 /* Note : user_length is originally a __u16,
781 * and token_size is controlled by us,
782 * so extra_size won't get negative and
783 * won't overflow...
788 /* kzalloc() ensures NULL-termination for essid_compat. */
789 extra = kzalloc(extra_size, GFP_KERNEL);
790 if (!extra)
791 return -ENOMEM;
793 /* If it is a SET, get all the extra data in here */
794 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
795 if (copy_from_user(extra, iwp->pointer,
796 iwp->length *
797 descr->token_size)) {
798 err = -EFAULT;
799 goto out;
803 err = handler(dev, info, (union iwreq_data *) iwp, extra);
805 iwp->length += essid_compat;
807 /* If we have something to return to the user */
808 if (!err && IW_IS_GET(cmd)) {
809 /* Check if there is enough buffer up there */
810 if (user_length < iwp->length) {
811 err = -E2BIG;
812 goto out;
815 if (copy_to_user(iwp->pointer, extra,
816 iwp->length *
817 descr->token_size)) {
818 err = -EFAULT;
819 goto out;
823 /* Generate an event to notify listeners of the change */
824 if ((descr->flags & IW_DESCR_FLAG_EVENT) && err == -EIWCOMMIT) {
825 union iwreq_data *data = (union iwreq_data *) iwp;
827 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
828 /* If the event is restricted, don't
829 * export the payload.
831 wireless_send_event(dev, cmd, data, NULL);
832 else
833 wireless_send_event(dev, cmd, data, extra);
836 out:
837 kfree(extra);
838 return err;
842 * Wrapper to call a standard Wireless Extension handler.
843 * We do various checks and also take care of moving data between
844 * user space and kernel space.
846 static int ioctl_standard_call(struct net_device * dev,
847 struct iwreq *iwr,
848 unsigned int cmd,
849 struct iw_request_info *info,
850 iw_handler handler)
852 const struct iw_ioctl_description * descr;
853 int ret = -EINVAL;
855 /* Get the description of the IOCTL */
856 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
857 return -EOPNOTSUPP;
858 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
860 /* Check if we have a pointer to user space data or not */
861 if (descr->header_type != IW_HEADER_TYPE_POINT) {
863 /* No extra arguments. Trivial to handle */
864 ret = handler(dev, info, &(iwr->u), NULL);
866 /* Generate an event to notify listeners of the change */
867 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
868 ((ret == 0) || (ret == -EIWCOMMIT)))
869 wireless_send_event(dev, cmd, &(iwr->u), NULL);
870 } else {
871 ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
872 handler, dev, info);
875 /* Call commit handler if needed and defined */
876 if (ret == -EIWCOMMIT)
877 ret = call_commit_handler(dev);
879 /* Here, we will generate the appropriate event if needed */
881 return ret;
884 /* ---------------------------------------------------------------- */
886 * Wrapper to call a private Wireless Extension handler.
887 * We do various checks and also take care of moving data between
888 * user space and kernel space.
889 * It's not as nice and slimline as the standard wrapper. The cause
890 * is struct iw_priv_args, which was not really designed for the
891 * job we are going here.
893 * IMPORTANT : This function prevent to set and get data on the same
894 * IOCTL and enforce the SET/GET convention. Not doing it would be
895 * far too hairy...
896 * If you need to set and get data at the same time, please don't use
897 * a iw_handler but process it in your ioctl handler (i.e. use the
898 * old driver API).
900 static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
901 const struct iw_priv_args **descrp)
903 const struct iw_priv_args *descr;
904 int i, extra_size;
906 descr = NULL;
907 for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
908 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
909 descr = &dev->wireless_handlers->private_args[i];
910 break;
914 extra_size = 0;
915 if (descr) {
916 if (IW_IS_SET(cmd)) {
917 int offset = 0; /* For sub-ioctls */
918 /* Check for sub-ioctl handler */
919 if (descr->name[0] == '\0')
920 /* Reserve one int for sub-ioctl index */
921 offset = sizeof(__u32);
923 /* Size of set arguments */
924 extra_size = get_priv_size(descr->set_args);
926 /* Does it fits in iwr ? */
927 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
928 ((extra_size + offset) <= IFNAMSIZ))
929 extra_size = 0;
930 } else {
931 /* Size of get arguments */
932 extra_size = get_priv_size(descr->get_args);
934 /* Does it fits in iwr ? */
935 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
936 (extra_size <= IFNAMSIZ))
937 extra_size = 0;
940 *descrp = descr;
941 return extra_size;
944 static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
945 const struct iw_priv_args *descr,
946 iw_handler handler, struct net_device *dev,
947 struct iw_request_info *info, int extra_size)
949 char *extra;
950 int err;
952 /* Check what user space is giving us */
953 if (IW_IS_SET(cmd)) {
954 if (!iwp->pointer && iwp->length != 0)
955 return -EFAULT;
957 if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
958 return -E2BIG;
959 } else if (!iwp->pointer)
960 return -EFAULT;
962 extra = kmalloc(extra_size, GFP_KERNEL);
963 if (!extra)
964 return -ENOMEM;
966 /* If it is a SET, get all the extra data in here */
967 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
968 if (copy_from_user(extra, iwp->pointer, extra_size)) {
969 err = -EFAULT;
970 goto out;
974 /* Call the handler */
975 err = handler(dev, info, (union iwreq_data *) iwp, extra);
977 /* If we have something to return to the user */
978 if (!err && IW_IS_GET(cmd)) {
979 /* Adjust for the actual length if it's variable,
980 * avoid leaking kernel bits outside.
982 if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
983 extra_size = adjust_priv_size(descr->get_args, iwp);
985 if (copy_to_user(iwp->pointer, extra, extra_size))
986 err = -EFAULT;
989 out:
990 kfree(extra);
991 return err;
994 static int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
995 unsigned int cmd, struct iw_request_info *info,
996 iw_handler handler)
998 int extra_size = 0, ret = -EINVAL;
999 const struct iw_priv_args *descr;
1001 extra_size = get_priv_descr_and_size(dev, cmd, &descr);
1003 /* Check if we have a pointer to user space data or not. */
1004 if (extra_size == 0) {
1005 /* No extra arguments. Trivial to handle */
1006 ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
1007 } else {
1008 ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
1009 handler, dev, info, extra_size);
1012 /* Call commit handler if needed and defined */
1013 if (ret == -EIWCOMMIT)
1014 ret = call_commit_handler(dev);
1016 return ret;
1019 /* ---------------------------------------------------------------- */
1020 typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
1021 unsigned int, struct iw_request_info *,
1022 iw_handler);
1025 * Main IOCTl dispatcher.
1026 * Check the type of IOCTL and call the appropriate wrapper...
1028 static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
1029 unsigned int cmd,
1030 struct iw_request_info *info,
1031 wext_ioctl_func standard,
1032 wext_ioctl_func private)
1034 struct iwreq *iwr = (struct iwreq *) ifr;
1035 struct net_device *dev;
1036 iw_handler handler;
1038 /* Permissions are already checked in dev_ioctl() before calling us.
1039 * The copy_to/from_user() of ifr is also dealt with in there */
1041 /* Make sure the device exist */
1042 if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
1043 return -ENODEV;
1045 /* A bunch of special cases, then the generic case...
1046 * Note that 'cmd' is already filtered in dev_ioctl() with
1047 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
1048 if (cmd == SIOCGIWSTATS)
1049 return standard(dev, iwr, cmd, info,
1050 &iw_handler_get_iwstats);
1052 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
1053 return standard(dev, iwr, cmd, info,
1054 &iw_handler_get_private);
1056 /* Basic check */
1057 if (!netif_device_present(dev))
1058 return -ENODEV;
1060 /* New driver API : try to find the handler */
1061 handler = get_handler(dev, cmd);
1062 if (handler) {
1063 /* Standard and private are not the same */
1064 if (cmd < SIOCIWFIRSTPRIV)
1065 return standard(dev, iwr, cmd, info, handler);
1066 else
1067 return private(dev, iwr, cmd, info, handler);
1069 /* Old driver API : call driver ioctl handler */
1070 if (dev->netdev_ops->ndo_do_ioctl)
1071 return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
1072 return -EOPNOTSUPP;
1075 /* If command is `set a parameter', or `get the encoding parameters',
1076 * check if the user has the right to do it.
1078 static int wext_permission_check(unsigned int cmd)
1080 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1081 && !capable(CAP_NET_ADMIN))
1082 return -EPERM;
1084 return 0;
1087 /* entry point from dev ioctl */
1088 static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
1089 unsigned int cmd, struct iw_request_info *info,
1090 wext_ioctl_func standard,
1091 wext_ioctl_func private)
1093 int ret = wext_permission_check(cmd);
1095 if (ret)
1096 return ret;
1098 dev_load(net, ifr->ifr_name);
1099 rtnl_lock();
1100 ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
1101 rtnl_unlock();
1103 return ret;
1106 int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1107 void __user *arg)
1109 struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1110 int ret;
1112 ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
1113 ioctl_standard_call,
1114 ioctl_private_call);
1115 if (ret >= 0 &&
1116 IW_IS_GET(cmd) &&
1117 copy_to_user(arg, ifr, sizeof(struct iwreq)))
1118 return -EFAULT;
1120 return ret;
1123 #ifdef CONFIG_COMPAT
1124 static int compat_standard_call(struct net_device *dev,
1125 struct iwreq *iwr,
1126 unsigned int cmd,
1127 struct iw_request_info *info,
1128 iw_handler handler)
1130 const struct iw_ioctl_description *descr;
1131 struct compat_iw_point *iwp_compat;
1132 struct iw_point iwp;
1133 int err;
1135 descr = standard_ioctl + (cmd - SIOCIWFIRST);
1137 if (descr->header_type != IW_HEADER_TYPE_POINT)
1138 return ioctl_standard_call(dev, iwr, cmd, info, handler);
1140 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1141 iwp.pointer = compat_ptr(iwp_compat->pointer);
1142 iwp.length = iwp_compat->length;
1143 iwp.flags = iwp_compat->flags;
1145 err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1147 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1148 iwp_compat->length = iwp.length;
1149 iwp_compat->flags = iwp.flags;
1151 return err;
1154 static int compat_private_call(struct net_device *dev, struct iwreq *iwr,
1155 unsigned int cmd, struct iw_request_info *info,
1156 iw_handler handler)
1158 const struct iw_priv_args *descr;
1159 int ret, extra_size;
1161 extra_size = get_priv_descr_and_size(dev, cmd, &descr);
1163 /* Check if we have a pointer to user space data or not. */
1164 if (extra_size == 0) {
1165 /* No extra arguments. Trivial to handle */
1166 ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
1167 } else {
1168 struct compat_iw_point *iwp_compat;
1169 struct iw_point iwp;
1171 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1172 iwp.pointer = compat_ptr(iwp_compat->pointer);
1173 iwp.length = iwp_compat->length;
1174 iwp.flags = iwp_compat->flags;
1176 ret = ioctl_private_iw_point(&iwp, cmd, descr,
1177 handler, dev, info, extra_size);
1179 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1180 iwp_compat->length = iwp.length;
1181 iwp_compat->flags = iwp.flags;
1184 /* Call commit handler if needed and defined */
1185 if (ret == -EIWCOMMIT)
1186 ret = call_commit_handler(dev);
1188 return ret;
1191 int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1192 unsigned long arg)
1194 void __user *argp = (void __user *)arg;
1195 struct iw_request_info info;
1196 struct iwreq iwr;
1197 char *colon;
1198 int ret;
1200 if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1201 return -EFAULT;
1203 iwr.ifr_name[IFNAMSIZ-1] = 0;
1204 colon = strchr(iwr.ifr_name, ':');
1205 if (colon)
1206 *colon = 0;
1208 info.cmd = cmd;
1209 info.flags = IW_REQUEST_FLAG_COMPAT;
1211 ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
1212 compat_standard_call,
1213 compat_private_call);
1215 if (ret >= 0 &&
1216 IW_IS_GET(cmd) &&
1217 copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1218 return -EFAULT;
1220 return ret;
1222 #endif
1224 /************************* EVENT PROCESSING *************************/
1226 * Process events generated by the wireless layer or the driver.
1227 * Most often, the event will be propagated through rtnetlink
1230 /* ---------------------------------------------------------------- */
1232 * Locking...
1233 * ----------
1235 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1236 * the locking issue in here and implementing this code !
1238 * The issue : wireless_send_event() is often called in interrupt context,
1239 * while the Netlink layer can never be called in interrupt context.
1240 * The fully formed RtNetlink events are queued, and then a tasklet is run
1241 * to feed those to Netlink.
1242 * The skb_queue is interrupt safe, and its lock is not held while calling
1243 * Netlink, so there is no possibility of dealock.
1244 * Jean II
1247 static struct sk_buff_head wireless_nlevent_queue;
1249 static int __init wireless_nlevent_init(void)
1251 skb_queue_head_init(&wireless_nlevent_queue);
1252 return 0;
1255 subsys_initcall(wireless_nlevent_init);
1257 static void wireless_nlevent_process(unsigned long data)
1259 struct sk_buff *skb;
1261 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
1262 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
1265 static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1267 /* ---------------------------------------------------------------- */
1269 * Fill a rtnetlink message with our event data.
1270 * Note that we propage only the specified event and don't dump the
1271 * current wireless config. Dumping the wireless config is far too
1272 * expensive (for each parameter, the driver need to query the hardware).
1274 static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
1275 int type, char *event, int event_len)
1277 struct ifinfomsg *r;
1278 struct nlmsghdr *nlh;
1280 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
1281 if (nlh == NULL)
1282 return -EMSGSIZE;
1284 r = nlmsg_data(nlh);
1285 r->ifi_family = AF_UNSPEC;
1286 r->__ifi_pad = 0;
1287 r->ifi_type = dev->type;
1288 r->ifi_index = dev->ifindex;
1289 r->ifi_flags = dev_get_flags(dev);
1290 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1292 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
1293 /* Add the wireless events in the netlink packet */
1294 NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
1296 return nlmsg_end(skb, nlh);
1298 nla_put_failure:
1299 nlmsg_cancel(skb, nlh);
1300 return -EMSGSIZE;
1303 /* ---------------------------------------------------------------- */
1305 * Create and broadcast and send it on the standard rtnetlink socket
1306 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1307 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1308 * within a RTM_NEWLINK event.
1310 static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
1312 struct sk_buff *skb;
1313 int err;
1315 if (!net_eq(dev_net(dev), &init_net))
1316 return;
1318 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1319 if (!skb)
1320 return;
1322 err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
1323 if (err < 0) {
1324 WARN_ON(err == -EMSGSIZE);
1325 kfree_skb(skb);
1326 return;
1329 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
1330 skb_queue_tail(&wireless_nlevent_queue, skb);
1331 tasklet_schedule(&wireless_nlevent_tasklet);
1334 /* ---------------------------------------------------------------- */
1336 * Main event dispatcher. Called from other parts and drivers.
1337 * Send the event on the appropriate channels.
1338 * May be called from interrupt context.
1340 void wireless_send_event(struct net_device * dev,
1341 unsigned int cmd,
1342 union iwreq_data * wrqu,
1343 char * extra)
1345 const struct iw_ioctl_description * descr = NULL;
1346 int extra_len = 0;
1347 struct iw_event *event; /* Mallocated whole event */
1348 int event_len; /* Its size */
1349 int hdr_len; /* Size of the event header */
1350 int wrqu_off = 0; /* Offset in wrqu */
1351 /* Don't "optimise" the following variable, it will crash */
1352 unsigned cmd_index; /* *MUST* be unsigned */
1354 /* Get the description of the Event */
1355 if (cmd <= SIOCIWLAST) {
1356 cmd_index = cmd - SIOCIWFIRST;
1357 if (cmd_index < standard_ioctl_num)
1358 descr = &(standard_ioctl[cmd_index]);
1359 } else {
1360 cmd_index = cmd - IWEVFIRST;
1361 if (cmd_index < standard_event_num)
1362 descr = &(standard_event[cmd_index]);
1364 /* Don't accept unknown events */
1365 if (descr == NULL) {
1366 /* Note : we don't return an error to the driver, because
1367 * the driver would not know what to do about it. It can't
1368 * return an error to the user, because the event is not
1369 * initiated by a user request.
1370 * The best the driver could do is to log an error message.
1371 * We will do it ourselves instead...
1373 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
1374 dev->name, cmd);
1375 return;
1378 /* Check extra parameters and set extra_len */
1379 if (descr->header_type == IW_HEADER_TYPE_POINT) {
1380 /* Check if number of token fits within bounds */
1381 if (wrqu->data.length > descr->max_tokens) {
1382 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
1383 return;
1385 if (wrqu->data.length < descr->min_tokens) {
1386 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
1387 return;
1389 /* Calculate extra_len - extra is NULL for restricted events */
1390 if (extra != NULL)
1391 extra_len = wrqu->data.length * descr->token_size;
1392 /* Always at an offset in wrqu */
1393 wrqu_off = IW_EV_POINT_OFF;
1396 /* Total length of the event */
1397 hdr_len = event_type_size[descr->header_type];
1398 event_len = hdr_len + extra_len;
1400 /* Create temporary buffer to hold the event */
1401 event = kmalloc(event_len, GFP_ATOMIC);
1402 if (event == NULL)
1403 return;
1405 /* Fill event */
1406 event->len = event_len;
1407 event->cmd = cmd;
1408 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
1409 if (extra)
1410 memcpy(((char *) event) + hdr_len, extra, extra_len);
1412 /* Send via the RtNetlink event channel */
1413 rtmsg_iwinfo(dev, (char *) event, event_len);
1415 /* Cleanup */
1416 kfree(event);
1418 return; /* Always success, I guess ;-) */
1420 EXPORT_SYMBOL(wireless_send_event);
1422 /********************** ENHANCED IWSPY SUPPORT **********************/
1424 * In the old days, the driver was handling spy support all by itself.
1425 * Now, the driver can delegate this task to Wireless Extensions.
1426 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1427 * push data to us via wireless_spy_update() and include struct iw_spy_data
1428 * in its private part (and export it in net_device->wireless_data->spy_data).
1429 * One of the main advantage of centralising spy support here is that
1430 * it becomes much easier to improve and extend it without having to touch
1431 * the drivers. One example is the addition of the Spy-Threshold events.
1434 /* ---------------------------------------------------------------- */
1436 * Return the pointer to the spy data in the driver.
1437 * Because this is called on the Rx path via wireless_spy_update(),
1438 * we want it to be efficient...
1440 static inline struct iw_spy_data *get_spydata(struct net_device *dev)
1442 /* This is the new way */
1443 if (dev->wireless_data)
1444 return dev->wireless_data->spy_data;
1445 return NULL;
1448 /*------------------------------------------------------------------*/
1450 * Standard Wireless Handler : set Spy List
1452 int iw_handler_set_spy(struct net_device * dev,
1453 struct iw_request_info * info,
1454 union iwreq_data * wrqu,
1455 char * extra)
1457 struct iw_spy_data * spydata = get_spydata(dev);
1458 struct sockaddr * address = (struct sockaddr *) extra;
1460 /* Make sure driver is not buggy or using the old API */
1461 if (!spydata)
1462 return -EOPNOTSUPP;
1464 /* Disable spy collection while we copy the addresses.
1465 * While we copy addresses, any call to wireless_spy_update()
1466 * will NOP. This is OK, as anyway the addresses are changing. */
1467 spydata->spy_number = 0;
1469 /* We want to operate without locking, because wireless_spy_update()
1470 * most likely will happen in the interrupt handler, and therefore
1471 * have its own locking constraints and needs performance.
1472 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1473 * This make sure wireless_spy_update() "see" that the spy list
1474 * is temporarily disabled. */
1475 smp_wmb();
1477 /* Are there are addresses to copy? */
1478 if (wrqu->data.length > 0) {
1479 int i;
1481 /* Copy addresses */
1482 for (i = 0; i < wrqu->data.length; i++)
1483 memcpy(spydata->spy_address[i], address[i].sa_data,
1484 ETH_ALEN);
1485 /* Reset stats */
1486 memset(spydata->spy_stat, 0,
1487 sizeof(struct iw_quality) * IW_MAX_SPY);
1490 /* Make sure above is updated before re-enabling */
1491 smp_wmb();
1493 /* Enable addresses */
1494 spydata->spy_number = wrqu->data.length;
1496 return 0;
1498 EXPORT_SYMBOL(iw_handler_set_spy);
1500 /*------------------------------------------------------------------*/
1502 * Standard Wireless Handler : get Spy List
1504 int iw_handler_get_spy(struct net_device * dev,
1505 struct iw_request_info * info,
1506 union iwreq_data * wrqu,
1507 char * extra)
1509 struct iw_spy_data * spydata = get_spydata(dev);
1510 struct sockaddr * address = (struct sockaddr *) extra;
1511 int i;
1513 /* Make sure driver is not buggy or using the old API */
1514 if (!spydata)
1515 return -EOPNOTSUPP;
1517 wrqu->data.length = spydata->spy_number;
1519 /* Copy addresses. */
1520 for (i = 0; i < spydata->spy_number; i++) {
1521 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1522 address[i].sa_family = AF_UNIX;
1524 /* Copy stats to the user buffer (just after). */
1525 if (spydata->spy_number > 0)
1526 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1527 spydata->spy_stat,
1528 sizeof(struct iw_quality) * spydata->spy_number);
1529 /* Reset updated flags. */
1530 for (i = 0; i < spydata->spy_number; i++)
1531 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
1532 return 0;
1534 EXPORT_SYMBOL(iw_handler_get_spy);
1536 /*------------------------------------------------------------------*/
1538 * Standard Wireless Handler : set spy threshold
1540 int iw_handler_set_thrspy(struct net_device * dev,
1541 struct iw_request_info *info,
1542 union iwreq_data * wrqu,
1543 char * extra)
1545 struct iw_spy_data * spydata = get_spydata(dev);
1546 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1548 /* Make sure driver is not buggy or using the old API */
1549 if (!spydata)
1550 return -EOPNOTSUPP;
1552 /* Just do it */
1553 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1554 2 * sizeof(struct iw_quality));
1556 /* Clear flag */
1557 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1559 return 0;
1561 EXPORT_SYMBOL(iw_handler_set_thrspy);
1563 /*------------------------------------------------------------------*/
1565 * Standard Wireless Handler : get spy threshold
1567 int iw_handler_get_thrspy(struct net_device * dev,
1568 struct iw_request_info *info,
1569 union iwreq_data * wrqu,
1570 char * extra)
1572 struct iw_spy_data * spydata = get_spydata(dev);
1573 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1575 /* Make sure driver is not buggy or using the old API */
1576 if (!spydata)
1577 return -EOPNOTSUPP;
1579 /* Just do it */
1580 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1581 2 * sizeof(struct iw_quality));
1583 return 0;
1585 EXPORT_SYMBOL(iw_handler_get_thrspy);
1587 /*------------------------------------------------------------------*/
1589 * Prepare and send a Spy Threshold event
1591 static void iw_send_thrspy_event(struct net_device * dev,
1592 struct iw_spy_data * spydata,
1593 unsigned char * address,
1594 struct iw_quality * wstats)
1596 union iwreq_data wrqu;
1597 struct iw_thrspy threshold;
1599 /* Init */
1600 wrqu.data.length = 1;
1601 wrqu.data.flags = 0;
1602 /* Copy address */
1603 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1604 threshold.addr.sa_family = ARPHRD_ETHER;
1605 /* Copy stats */
1606 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1607 /* Copy also thresholds */
1608 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1609 2 * sizeof(struct iw_quality));
1611 /* Send event to user space */
1612 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1615 /* ---------------------------------------------------------------- */
1617 * Call for the driver to update the spy data.
1618 * For now, the spy data is a simple array. As the size of the array is
1619 * small, this is good enough. If we wanted to support larger number of
1620 * spy addresses, we should use something more efficient...
1622 void wireless_spy_update(struct net_device * dev,
1623 unsigned char * address,
1624 struct iw_quality * wstats)
1626 struct iw_spy_data * spydata = get_spydata(dev);
1627 int i;
1628 int match = -1;
1630 /* Make sure driver is not buggy or using the old API */
1631 if (!spydata)
1632 return;
1634 /* Update all records that match */
1635 for (i = 0; i < spydata->spy_number; i++)
1636 if (!compare_ether_addr(address, spydata->spy_address[i])) {
1637 memcpy(&(spydata->spy_stat[i]), wstats,
1638 sizeof(struct iw_quality));
1639 match = i;
1642 /* Generate an event if we cross the spy threshold.
1643 * To avoid event storms, we have a simple hysteresis : we generate
1644 * event only when we go under the low threshold or above the
1645 * high threshold. */
1646 if (match >= 0) {
1647 if (spydata->spy_thr_under[match]) {
1648 if (wstats->level > spydata->spy_thr_high.level) {
1649 spydata->spy_thr_under[match] = 0;
1650 iw_send_thrspy_event(dev, spydata,
1651 address, wstats);
1653 } else {
1654 if (wstats->level < spydata->spy_thr_low.level) {
1655 spydata->spy_thr_under[match] = 1;
1656 iw_send_thrspy_event(dev, spydata,
1657 address, wstats);
1662 EXPORT_SYMBOL(wireless_spy_update);