2 * This file implement the Wireless Extensions APIs.
4 * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
5 * Copyright (c) 1997-2003 Jean Tourrilhes, All Rights Reserved.
7 * (As all part of the Linux kernel, this file is GPL)
10 /************************** DOCUMENTATION **************************/
14 * See <linux/wireless.h> for details of the APIs and the rest.
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
53 /***************************** INCLUDES *****************************/
55 #include <asm/uaccess.h> /* copy_to_user() */
56 #include <linux/config.h> /* Not needed ??? */
57 #include <linux/types.h> /* off_t */
58 #include <linux/netdevice.h> /* struct ifreq, dev_get_by_name() */
59 #include <linux/rtnetlink.h> /* rtnetlink stuff */
60 #include <linux/if_arp.h> /* ARPHRD_ETHER */
62 #include <linux/wireless.h> /* Pretty obvious */
63 #include <net/iw_handler.h> /* New driver API */
65 /**************************** CONSTANTS ****************************/
67 /* Enough lenience, let's make sure things are proper... */
68 #define WE_STRICT_WRITE /* Check write buffer size */
69 /* I'll probably drop both the define and kernel message in the next version */
72 #undef WE_IOCTL_DEBUG /* Debug IOCTL API */
73 #undef WE_EVENT_DEBUG /* Debug Event dispatcher */
74 #undef WE_SPY_DEBUG /* Debug enhanced spy support */
77 #define WE_EVENT_NETLINK /* Propagate events using rtnetlink */
78 #define WE_SET_EVENT /* Generate an event on some set commands */
80 /************************* GLOBAL VARIABLES *************************/
82 * You should not use global variables, because of re-entrancy.
83 * On our case, it's only const, so it's OK...
86 * Meta-data about all the standard Wireless Extension request we
89 static const struct iw_ioctl_description standard_ioctl[] = {
91 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
93 { IW_HEADER_TYPE_CHAR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
95 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
97 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
99 { IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
101 { IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
103 { IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
105 { IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
107 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
109 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
111 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
113 { IW_HEADER_TYPE_POINT, 0, 1, 0, sizeof(struct iw_range), IW_DESCR_FLAG_DUMP},
115 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
116 /* SIOCGIWPRIV (handled directly by us) */
117 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
119 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
120 /* SIOCGIWSTATS (handled directly by us) */
121 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
123 { IW_HEADER_TYPE_POINT, 0, sizeof(struct sockaddr), 0, IW_MAX_SPY, 0},
125 { IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_SPY, 0},
127 { IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
129 { IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
131 { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
133 { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
135 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
137 { IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_AP, 0},
139 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
141 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_SCAN_MAX_DATA, 0},
143 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_EVENT},
145 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_DUMP},
147 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
149 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
151 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
153 { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
155 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
157 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
159 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
161 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
163 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
165 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
167 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
169 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
171 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
173 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
175 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT},
177 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT},
179 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
181 { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
183 static const int standard_ioctl_num = (sizeof(standard_ioctl) /
184 sizeof(struct iw_ioctl_description));
187 * Meta-data about all the additional standard Wireless Extension events
190 static const struct iw_ioctl_description standard_event[] = {
192 { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
194 { IW_HEADER_TYPE_QUAL, 0, 0, 0, 0, 0},
196 { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_CUSTOM_MAX, 0},
198 { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
200 { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
202 static const int standard_event_num = (sizeof(standard_event) /
203 sizeof(struct iw_ioctl_description));
205 /* Size (in bytes) of the various private data types */
206 static const char priv_type_size[] = {
207 0, /* IW_PRIV_TYPE_NONE */
208 1, /* IW_PRIV_TYPE_BYTE */
209 1, /* IW_PRIV_TYPE_CHAR */
211 sizeof(__u32), /* IW_PRIV_TYPE_INT */
212 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
213 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
217 /* Size (in bytes) of various events */
218 static const int event_type_size[] = {
219 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
221 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
223 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
224 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
225 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
227 IW_EV_POINT_LEN, /* Without variable payload */
228 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
229 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
232 /************************ COMMON SUBROUTINES ************************/
234 * Stuff that may be used in various place or doesn't fit in one
235 * of the section below.
238 /* ---------------------------------------------------------------- */
240 * Return the driver handler associated with a specific Wireless Extension.
241 * Called from various place, so make sure it remains efficient.
243 static inline iw_handler get_handler(struct net_device *dev,
246 /* Don't "optimise" the following variable, it will crash */
247 unsigned int index; /* *MUST* be unsigned */
249 /* Check if we have some wireless handlers defined */
250 if(dev->wireless_handlers == NULL)
253 /* Try as a standard command */
254 index = cmd - SIOCIWFIRST;
255 if(index < dev->wireless_handlers->num_standard)
256 return dev->wireless_handlers->standard[index];
258 /* Try as a private command */
259 index = cmd - SIOCIWFIRSTPRIV;
260 if(index < dev->wireless_handlers->num_private)
261 return dev->wireless_handlers->private[index];
267 /* ---------------------------------------------------------------- */
269 * Get statistics out of the driver
271 static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
273 return (dev->get_wireless_stats ?
274 dev->get_wireless_stats(dev) :
275 (struct iw_statistics *) NULL);
276 /* In the future, get_wireless_stats may move from 'struct net_device'
277 * to 'struct iw_handler_def', to de-bloat struct net_device.
278 * Definitely worse a thought... */
281 /* ---------------------------------------------------------------- */
283 * Call the commit handler in the driver
284 * (if exist and if conditions are right)
286 * Note : our current commit strategy is currently pretty dumb,
287 * but we will be able to improve on that...
288 * The goal is to try to agreagate as many changes as possible
289 * before doing the commit. Drivers that will define a commit handler
290 * are usually those that need a reset after changing parameters, so
291 * we want to minimise the number of reset.
292 * A cool idea is to use a timer : at each "set" command, we re-set the
293 * timer, when the timer eventually fires, we call the driver.
294 * Hopefully, more on that later.
296 * Also, I'm waiting to see how many people will complain about the
297 * netif_running(dev) test. I'm open on that one...
298 * Hopefully, the driver will remember to do a commit in "open()" ;-)
300 static inline int call_commit_handler(struct net_device * dev)
302 if((netif_running(dev)) &&
303 (dev->wireless_handlers->standard[0] != NULL)) {
304 /* Call the commit handler on the driver */
305 return dev->wireless_handlers->standard[0](dev, NULL,
308 return 0; /* Command completed successfully */
311 /* ---------------------------------------------------------------- */
313 * Number of private arguments
315 static inline int get_priv_size(__u16 args)
317 int num = args & IW_PRIV_SIZE_MASK;
318 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
320 return num * priv_type_size[type];
324 /******************** /proc/net/wireless SUPPORT ********************/
326 * The /proc/net/wireless file is a human readable user-space interface
327 * exporting various wireless specific statistics from the wireless devices.
328 * This is the most popular part of the Wireless Extensions ;-)
330 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
331 * The content of the file is basically the content of "struct iw_statistics".
334 #ifdef CONFIG_PROC_FS
336 /* ---------------------------------------------------------------- */
338 * Print one entry (line) of /proc/net/wireless
340 static inline int sprintf_wireless_stats(char *buffer, struct net_device *dev)
342 /* Get stats from the driver */
343 struct iw_statistics *stats;
346 stats = get_wireless_stats(dev);
347 if (stats != (struct iw_statistics *) NULL) {
348 size = sprintf(buffer,
349 "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d %6d %6d %6d\n",
353 stats->qual.updated & 1 ? '.' : ' ',
354 ((__u8) stats->qual.level),
355 stats->qual.updated & 2 ? '.' : ' ',
356 ((__u8) stats->qual.noise),
357 stats->qual.updated & 4 ? '.' : ' ',
360 stats->discard.fragment,
361 stats->discard.retries,
364 stats->qual.updated = 0;
372 /* ---------------------------------------------------------------- */
374 * Print info for /proc/net/wireless (print all entries)
376 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
384 struct net_device * dev;
386 size = sprintf(buffer,
387 "Inter-| sta-| Quality | Discarded packets | Missed | WE\n"
388 " face | tus | link level noise | nwid crypt frag retry misc | beacon | %d\n",
394 read_lock(&dev_base_lock);
395 for (dev = dev_base; dev != NULL; dev = dev->next) {
396 size = sprintf_wireless_stats(buffer + len, dev);
404 if (pos > offset + length)
407 read_unlock(&dev_base_lock);
409 *start = buffer + (offset - begin); /* Start of wanted data */
410 len -= (offset - begin); /* Start slop */
412 len = length; /* Ending slop */
418 #endif /* CONFIG_PROC_FS */
420 /************************** IOCTL SUPPORT **************************/
422 * The original user space API to configure all those Wireless Extensions
424 * In there, we check if we need to call the new driver API (iw_handler)
425 * or just call the driver ioctl handler.
428 /* ---------------------------------------------------------------- */
430 * Allow programatic access to /proc/net/wireless even if /proc
431 * doesn't exist... Also more efficient...
433 static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
435 /* Get stats from the driver */
436 struct iw_statistics *stats;
438 stats = get_wireless_stats(dev);
439 if (stats != (struct iw_statistics *) NULL) {
440 struct iwreq * wrq = (struct iwreq *)ifr;
442 /* Copy statistics to the user buffer */
443 if(copy_to_user(wrq->u.data.pointer, stats,
444 sizeof(struct iw_statistics)))
447 /* Check if we need to clear the update flag */
448 if(wrq->u.data.flags != 0)
449 stats->qual.updated = 0;
455 /* ---------------------------------------------------------------- */
457 * Export the driver private handler definition
458 * They will be picked up by tools like iwpriv...
460 static inline int ioctl_export_private(struct net_device * dev,
463 struct iwreq * iwr = (struct iwreq *) ifr;
465 /* Check if the driver has something to export */
466 if((dev->wireless_handlers->num_private_args == 0) ||
467 (dev->wireless_handlers->private_args == NULL))
470 /* Check NULL pointer */
471 if(iwr->u.data.pointer == NULL)
473 #ifdef WE_STRICT_WRITE
474 /* Check if there is enough buffer up there */
475 if(iwr->u.data.length < dev->wireless_handlers->num_private_args) {
476 printk(KERN_ERR "%s (WE) : Buffer for request SIOCGIWPRIV too small (%d<%d)\n", dev->name, iwr->u.data.length, dev->wireless_handlers->num_private_args);
479 #endif /* WE_STRICT_WRITE */
481 /* Set the number of available ioctls. */
482 iwr->u.data.length = dev->wireless_handlers->num_private_args;
484 /* Copy structure to the user buffer. */
485 if (copy_to_user(iwr->u.data.pointer,
486 dev->wireless_handlers->private_args,
487 sizeof(struct iw_priv_args) * iwr->u.data.length))
493 /* ---------------------------------------------------------------- */
495 * Wrapper to call a standard Wireless Extension handler.
496 * We do various checks and also take care of moving data between
497 * user space and kernel space.
499 static inline int ioctl_standard_call(struct net_device * dev,
504 struct iwreq * iwr = (struct iwreq *) ifr;
505 const struct iw_ioctl_description * descr;
506 struct iw_request_info info;
510 /* Get the description of the IOCTL */
511 if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
513 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
515 #ifdef WE_IOCTL_DEBUG
516 printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
518 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);
519 #endif /* WE_IOCTL_DEBUG */
521 /* Prepare the call */
525 /* Check if we have a pointer to user space data or not */
526 if(descr->header_type != IW_HEADER_TYPE_POINT) {
528 /* No extra arguments. Trivial to handle */
529 ret = handler(dev, &info, &(iwr->u), NULL);
532 /* Generate an event to notify listeners of the change */
533 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
534 ((ret == 0) || (ret == -EIWCOMMIT)))
535 wireless_send_event(dev, cmd, &(iwr->u), NULL);
536 #endif /* WE_SET_EVENT */
541 /* Check what user space is giving us */
543 /* Check NULL pointer */
544 if((iwr->u.data.pointer == NULL) &&
545 (iwr->u.data.length != 0))
547 /* Check if number of token fits within bounds */
548 if(iwr->u.data.length > descr->max_tokens)
550 if(iwr->u.data.length < descr->min_tokens)
553 /* Check NULL pointer */
554 if(iwr->u.data.pointer == NULL)
556 /* Save user space buffer size for checking */
557 user_size = iwr->u.data.length;
560 #ifdef WE_IOCTL_DEBUG
561 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
562 dev->name, descr->max_tokens * descr->token_size);
563 #endif /* WE_IOCTL_DEBUG */
565 /* Always allocate for max space. Easier, and won't last
567 extra = kmalloc(descr->max_tokens * descr->token_size,
573 /* If it is a SET, get all the extra data in here */
574 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
575 err = copy_from_user(extra, iwr->u.data.pointer,
582 #ifdef WE_IOCTL_DEBUG
583 printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
585 iwr->u.data.length * descr->token_size);
586 #endif /* WE_IOCTL_DEBUG */
589 /* Call the handler */
590 ret = handler(dev, &info, &(iwr->u), extra);
592 /* If we have something to return to the user */
593 if (!ret && IW_IS_GET(cmd)) {
594 #ifdef WE_STRICT_WRITE
595 /* Check if there is enough buffer up there */
596 if(user_size < iwr->u.data.length) {
597 printk(KERN_ERR "%s (WE) : Buffer for request %04X too small (%d<%d)\n", dev->name, cmd, user_size, iwr->u.data.length);
601 #endif /* WE_STRICT_WRITE */
603 err = copy_to_user(iwr->u.data.pointer, extra,
608 #ifdef WE_IOCTL_DEBUG
609 printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
611 iwr->u.data.length * descr->token_size);
612 #endif /* WE_IOCTL_DEBUG */
616 /* Generate an event to notify listeners of the change */
617 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
618 ((ret == 0) || (ret == -EIWCOMMIT))) {
619 if(descr->flags & IW_DESCR_FLAG_RESTRICT)
620 /* If the event is restricted, don't
621 * export the payload */
622 wireless_send_event(dev, cmd, &(iwr->u), NULL);
624 wireless_send_event(dev, cmd, &(iwr->u),
627 #endif /* WE_SET_EVENT */
629 /* Cleanup - I told you it wasn't that long ;-) */
633 /* Call commit handler if needed and defined */
634 if(ret == -EIWCOMMIT)
635 ret = call_commit_handler(dev);
637 /* Here, we will generate the appropriate event if needed */
642 /* ---------------------------------------------------------------- */
644 * Wrapper to call a private Wireless Extension handler.
645 * We do various checks and also take care of moving data between
646 * user space and kernel space.
647 * It's not as nice and slimline as the standard wrapper. The cause
648 * is struct iw_priv_args, which was not really designed for the
649 * job we are going here.
651 * IMPORTANT : This function prevent to set and get data on the same
652 * IOCTL and enforce the SET/GET convention. Not doing it would be
654 * If you need to set and get data at the same time, please don't use
655 * a iw_handler but process it in your ioctl handler (i.e. use the
658 static inline int ioctl_private_call(struct net_device * dev,
663 struct iwreq * iwr = (struct iwreq *) ifr;
664 struct iw_priv_args * descr = NULL;
665 struct iw_request_info info;
670 /* Get the description of the IOCTL */
671 for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
672 if(cmd == dev->wireless_handlers->private_args[i].cmd) {
673 descr = &(dev->wireless_handlers->private_args[i]);
677 #ifdef WE_IOCTL_DEBUG
678 printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
681 printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
682 dev->name, descr->name,
683 descr->set_args, descr->get_args);
685 #endif /* WE_IOCTL_DEBUG */
687 /* Compute the size of the set/get arguments */
690 int offset = 0; /* For sub-ioctls */
691 /* Check for sub-ioctl handler */
692 if(descr->name[0] == '\0')
693 /* Reserve one int for sub-ioctl index */
694 offset = sizeof(__u32);
696 /* Size of set arguments */
697 extra_size = get_priv_size(descr->set_args);
699 /* Does it fits in iwr ? */
700 if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
701 ((extra_size + offset) <= IFNAMSIZ))
704 /* Size of set arguments */
705 extra_size = get_priv_size(descr->get_args);
707 /* Does it fits in iwr ? */
708 if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
709 (extra_size <= IFNAMSIZ))
714 /* Prepare the call */
718 /* Check if we have a pointer to user space data or not. */
719 if(extra_size == 0) {
720 /* No extra arguments. Trivial to handle */
721 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
726 /* Check what user space is giving us */
728 /* Check NULL pointer */
729 if((iwr->u.data.pointer == NULL) &&
730 (iwr->u.data.length != 0))
733 /* Does it fits within bounds ? */
734 if(iwr->u.data.length > (descr->set_args &
738 /* Check NULL pointer */
739 if(iwr->u.data.pointer == NULL)
743 #ifdef WE_IOCTL_DEBUG
744 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
745 dev->name, extra_size);
746 #endif /* WE_IOCTL_DEBUG */
748 /* Always allocate for max space. Easier, and won't last
750 extra = kmalloc(extra_size, GFP_KERNEL);
755 /* If it is a SET, get all the extra data in here */
756 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
757 err = copy_from_user(extra, iwr->u.data.pointer,
763 #ifdef WE_IOCTL_DEBUG
764 printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
765 dev->name, iwr->u.data.length);
766 #endif /* WE_IOCTL_DEBUG */
769 /* Call the handler */
770 ret = handler(dev, &info, &(iwr->u), extra);
772 /* If we have something to return to the user */
773 if (!ret && IW_IS_GET(cmd)) {
774 err = copy_to_user(iwr->u.data.pointer, extra,
778 #ifdef WE_IOCTL_DEBUG
779 printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
780 dev->name, iwr->u.data.length);
781 #endif /* WE_IOCTL_DEBUG */
784 /* Cleanup - I told you it wasn't that long ;-) */
789 /* Call commit handler if needed and defined */
790 if(ret == -EIWCOMMIT)
791 ret = call_commit_handler(dev);
796 /* ---------------------------------------------------------------- */
798 * Main IOCTl dispatcher. Called from the main networking code
799 * (dev_ioctl() in net/core/dev.c).
800 * Check the type of IOCTL and call the appropriate wrapper...
802 int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
804 struct net_device *dev;
807 /* Permissions are already checked in dev_ioctl() before calling us.
808 * The copy_to/from_user() of ifr is also dealt with in there */
810 /* Make sure the device exist */
811 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
814 /* A bunch of special cases, then the generic case...
815 * Note that 'cmd' is already filtered in dev_ioctl() with
816 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
820 /* Get Wireless Stats */
821 return dev_iwstats(dev, ifr);
824 /* Check if we have some wireless handlers defined */
825 if(dev->wireless_handlers != NULL) {
826 /* We export to user space the definition of
827 * the private handler ourselves */
828 return ioctl_export_private(dev, ifr);
830 // ## Fall-through for old API ##
834 if (!netif_device_present(dev))
836 /* New driver API : try to find the handler */
837 handler = get_handler(dev, cmd);
838 if(handler != NULL) {
839 /* Standard and private are not the same */
840 if(cmd < SIOCIWFIRSTPRIV)
841 return ioctl_standard_call(dev,
846 return ioctl_private_call(dev,
851 /* Old driver API : call driver ioctl handler */
853 return dev->do_ioctl(dev, ifr, cmd);
861 /************************* EVENT PROCESSING *************************/
863 * Process events generated by the wireless layer or the driver.
864 * Most often, the event will be propagated through rtnetlink
867 #ifdef WE_EVENT_NETLINK
868 /* "rtnl" is defined in net/core/rtnetlink.c, but we need it here.
869 * It is declared in <linux/rtnetlink.h> */
871 /* ---------------------------------------------------------------- */
873 * Fill a rtnetlink message with our event data.
874 * Note that we propage only the specified event and don't dump the
875 * current wireless config. Dumping the wireless config is far too
876 * expensive (for each parameter, the driver need to query the hardware).
878 static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
879 struct net_device * dev,
885 struct nlmsghdr *nlh;
886 unsigned char *b = skb->tail;
888 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
890 r->ifi_family = AF_UNSPEC;
891 r->ifi_type = dev->type;
892 r->ifi_index = dev->ifindex;
893 r->ifi_flags = dev->flags;
894 r->ifi_change = 0; /* Wireless changes don't affect those flags */
896 /* Add the wireless events in the netlink packet */
897 RTA_PUT(skb, IFLA_WIRELESS,
900 nlh->nlmsg_len = skb->tail - b;
905 skb_trim(skb, b - skb->data);
909 /* ---------------------------------------------------------------- */
911 * Create and broadcast and send it on the standard rtnetlink socket
912 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
913 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
914 * within a RTM_NEWLINK event.
916 static inline void rtmsg_iwinfo(struct net_device * dev,
921 int size = NLMSG_GOODSIZE;
923 skb = alloc_skb(size, GFP_ATOMIC);
927 if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
928 event, event_len) < 0) {
932 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
933 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_ATOMIC);
935 #endif /* WE_EVENT_NETLINK */
937 /* ---------------------------------------------------------------- */
939 * Main event dispatcher. Called from other parts and drivers.
940 * Send the event on the apropriate channels.
941 * May be called from interrupt context.
943 void wireless_send_event(struct net_device * dev,
945 union iwreq_data * wrqu,
948 const struct iw_ioctl_description * descr = NULL;
950 struct iw_event *event; /* Mallocated whole event */
951 int event_len; /* Its size */
952 int hdr_len; /* Size of the event header */
953 /* Don't "optimise" the following variable, it will crash */
954 unsigned cmd_index; /* *MUST* be unsigned */
956 /* Get the description of the IOCTL */
957 if(cmd <= SIOCIWLAST) {
958 cmd_index = cmd - SIOCIWFIRST;
959 if(cmd_index < standard_ioctl_num)
960 descr = &(standard_ioctl[cmd_index]);
962 cmd_index = cmd - IWEVFIRST;
963 if(cmd_index < standard_event_num)
964 descr = &(standard_event[cmd_index]);
966 /* Don't accept unknown events */
968 /* Note : we don't return an error to the driver, because
969 * the driver would not know what to do about it. It can't
970 * return an error to the user, because the event is not
971 * initiated by a user request.
972 * The best the driver could do is to log an error message.
973 * We will do it ourselves instead...
975 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
979 #ifdef WE_EVENT_DEBUG
980 printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
982 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);
983 #endif /* WE_EVENT_DEBUG */
985 /* Check extra parameters and set extra_len */
986 if(descr->header_type == IW_HEADER_TYPE_POINT) {
987 /* Check if number of token fits within bounds */
988 if(wrqu->data.length > descr->max_tokens) {
989 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
992 if(wrqu->data.length < descr->min_tokens) {
993 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
996 /* Calculate extra_len - extra is NULL for restricted events */
998 extra_len = wrqu->data.length * descr->token_size;
999 #ifdef WE_EVENT_DEBUG
1000 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
1001 #endif /* WE_EVENT_DEBUG */
1004 /* Total length of the event */
1005 hdr_len = event_type_size[descr->header_type];
1006 event_len = hdr_len + extra_len;
1008 #ifdef WE_EVENT_DEBUG
1009 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, hdr_len %d, event_len %d\n", dev->name, cmd, hdr_len, event_len);
1010 #endif /* WE_EVENT_DEBUG */
1012 /* Create temporary buffer to hold the event */
1013 event = kmalloc(event_len, GFP_ATOMIC);
1018 event->len = event_len;
1020 memcpy(&event->u, wrqu, hdr_len - IW_EV_LCP_LEN);
1022 memcpy(((char *) event) + hdr_len, extra, extra_len);
1024 #ifdef WE_EVENT_NETLINK
1025 /* rtnetlink event channel */
1026 rtmsg_iwinfo(dev, (char *) event, event_len);
1027 #endif /* WE_EVENT_NETLINK */
1032 return; /* Always success, I guess ;-) */
1035 /********************** ENHANCED IWSPY SUPPORT **********************/
1037 * In the old days, the driver was handling spy support all by itself.
1038 * Now, the driver can delegate this task to Wireless Extensions.
1039 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1040 * push data to us via XXX and include struct iw_spy_data in its
1042 * One of the main advantage of centralising spy support here is that
1043 * it becomes much easier to improve and extend it without having to touch
1044 * the drivers. One example is the addition of the Spy-Threshold events.
1045 * Note : IW_WIRELESS_SPY is defined in iw_handler.h
1048 /*------------------------------------------------------------------*/
1050 * Standard Wireless Handler : set Spy List
1052 int iw_handler_set_spy(struct net_device * dev,
1053 struct iw_request_info * info,
1054 union iwreq_data * wrqu,
1057 #ifdef IW_WIRELESS_SPY
1058 struct iw_spy_data * spydata = (dev->priv +
1059 dev->wireless_handlers->spy_offset);
1060 struct sockaddr * address = (struct sockaddr *) extra;
1062 /* Disable spy collection while we copy the addresses.
1063 * As we don't disable interrupts, we need to do this to avoid races.
1064 * As we are the only writer, this is good enough. */
1065 spydata->spy_number = 0;
1067 /* Are there are addresses to copy? */
1068 if(wrqu->data.length > 0) {
1071 /* Copy addresses */
1072 for(i = 0; i < wrqu->data.length; i++)
1073 memcpy(spydata->spy_address[i], address[i].sa_data,
1076 memset(spydata->spy_stat, 0,
1077 sizeof(struct iw_quality) * IW_MAX_SPY);
1080 printk(KERN_DEBUG "iw_handler_set_spy() : offset %ld, spydata %p, num %d\n", dev->wireless_handlers->spy_offset, spydata, wrqu->data.length);
1081 for (i = 0; i < wrqu->data.length; i++)
1083 "%02X:%02X:%02X:%02X:%02X:%02X \n",
1084 spydata->spy_address[i][0],
1085 spydata->spy_address[i][1],
1086 spydata->spy_address[i][2],
1087 spydata->spy_address[i][3],
1088 spydata->spy_address[i][4],
1089 spydata->spy_address[i][5]);
1090 #endif /* WE_SPY_DEBUG */
1092 /* Enable addresses */
1093 spydata->spy_number = wrqu->data.length;
1096 #else /* IW_WIRELESS_SPY */
1098 #endif /* IW_WIRELESS_SPY */
1101 /*------------------------------------------------------------------*/
1103 * Standard Wireless Handler : get Spy List
1105 int iw_handler_get_spy(struct net_device * dev,
1106 struct iw_request_info * info,
1107 union iwreq_data * wrqu,
1110 #ifdef IW_WIRELESS_SPY
1111 struct iw_spy_data * spydata = (dev->priv +
1112 dev->wireless_handlers->spy_offset);
1113 struct sockaddr * address = (struct sockaddr *) extra;
1116 wrqu->data.length = spydata->spy_number;
1118 /* Copy addresses. */
1119 for(i = 0; i < spydata->spy_number; i++) {
1120 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1121 address[i].sa_family = AF_UNIX;
1123 /* Copy stats to the user buffer (just after). */
1124 if(spydata->spy_number > 0)
1125 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1127 sizeof(struct iw_quality) * spydata->spy_number);
1128 /* Reset updated flags. */
1129 for(i = 0; i < spydata->spy_number; i++)
1130 spydata->spy_stat[i].updated = 0;
1132 #else /* IW_WIRELESS_SPY */
1134 #endif /* IW_WIRELESS_SPY */
1137 /*------------------------------------------------------------------*/
1139 * Standard Wireless Handler : set spy threshold
1141 int iw_handler_set_thrspy(struct net_device * dev,
1142 struct iw_request_info *info,
1143 union iwreq_data * wrqu,
1146 #ifdef IW_WIRELESS_THRSPY
1147 struct iw_spy_data * spydata = (dev->priv +
1148 dev->wireless_handlers->spy_offset);
1149 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1152 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1153 2 * sizeof(struct iw_quality));
1156 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1159 printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
1160 #endif /* WE_SPY_DEBUG */
1163 #else /* IW_WIRELESS_THRSPY */
1165 #endif /* IW_WIRELESS_THRSPY */
1168 /*------------------------------------------------------------------*/
1170 * Standard Wireless Handler : get spy threshold
1172 int iw_handler_get_thrspy(struct net_device * dev,
1173 struct iw_request_info *info,
1174 union iwreq_data * wrqu,
1177 #ifdef IW_WIRELESS_THRSPY
1178 struct iw_spy_data * spydata = (dev->priv +
1179 dev->wireless_handlers->spy_offset);
1180 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1183 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1184 2 * sizeof(struct iw_quality));
1187 #else /* IW_WIRELESS_THRSPY */
1189 #endif /* IW_WIRELESS_THRSPY */
1192 #ifdef IW_WIRELESS_THRSPY
1193 /*------------------------------------------------------------------*/
1195 * Prepare and send a Spy Threshold event
1197 static void iw_send_thrspy_event(struct net_device * dev,
1198 struct iw_spy_data * spydata,
1199 unsigned char * address,
1200 struct iw_quality * wstats)
1202 union iwreq_data wrqu;
1203 struct iw_thrspy threshold;
1206 wrqu.data.length = 1;
1207 wrqu.data.flags = 0;
1209 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1210 threshold.addr.sa_family = ARPHRD_ETHER;
1212 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1213 /* Copy also thresholds */
1214 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1215 2 * sizeof(struct iw_quality));
1218 printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
1219 threshold.addr.sa_data[0],
1220 threshold.addr.sa_data[1],
1221 threshold.addr.sa_data[2],
1222 threshold.addr.sa_data[3],
1223 threshold.addr.sa_data[4],
1224 threshold.addr.sa_data[5], threshold.qual.level);
1225 #endif /* WE_SPY_DEBUG */
1227 /* Send event to user space */
1228 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1230 #endif /* IW_WIRELESS_THRSPY */
1232 /* ---------------------------------------------------------------- */
1234 * Call for the driver to update the spy data.
1235 * For now, the spy data is a simple array. As the size of the array is
1236 * small, this is good enough. If we wanted to support larger number of
1237 * spy addresses, we should use something more efficient...
1239 void wireless_spy_update(struct net_device * dev,
1240 unsigned char * address,
1241 struct iw_quality * wstats)
1243 #ifdef IW_WIRELESS_SPY
1244 struct iw_spy_data * spydata = (dev->priv +
1245 dev->wireless_handlers->spy_offset);
1250 printk(KERN_DEBUG "wireless_spy_update() : offset %ld, spydata %p, address %02X:%02X:%02X:%02X:%02X:%02X\n", dev->wireless_handlers->spy_offset, spydata, address[0], address[1], address[2], address[3], address[4], address[5]);
1251 #endif /* WE_SPY_DEBUG */
1253 /* Update all records that match */
1254 for(i = 0; i < spydata->spy_number; i++)
1255 if(!memcmp(address, spydata->spy_address[i], ETH_ALEN)) {
1256 memcpy(&(spydata->spy_stat[i]), wstats,
1257 sizeof(struct iw_quality));
1260 #ifdef IW_WIRELESS_THRSPY
1261 /* Generate an event if we cross the spy threshold.
1262 * To avoid event storms, we have a simple hysteresis : we generate
1263 * event only when we go under the low threshold or above the
1264 * high threshold. */
1266 if(spydata->spy_thr_under[match]) {
1267 if(wstats->level > spydata->spy_thr_high.level) {
1268 spydata->spy_thr_under[match] = 0;
1269 iw_send_thrspy_event(dev, spydata,
1273 if(wstats->level < spydata->spy_thr_low.level) {
1274 spydata->spy_thr_under[match] = 1;
1275 iw_send_thrspy_event(dev, spydata,
1280 #endif /* IW_WIRELESS_THRSPY */
1281 #endif /* IW_WIRELESS_SPY */