cleanup
[linux-2.4.git] / net / core / wireless.c
1 /*
2  * This file implement the Wireless Extensions APIs.
3  *
4  * Authors :    Jean Tourrilhes - HPL - <jt@hpl.hp.com>
5  * Copyright (c) 1997-2003 Jean Tourrilhes, All Rights Reserved.
6  *
7  * (As all part of the Linux kernel, this file is GPL)
8  */
9
10 /************************** DOCUMENTATION **************************/
11 /*
12  * API definition :
13  * --------------
14  * See <linux/wireless.h> for details of the APIs and the rest.
15  *
16  * History :
17  * -------
18  *
19  * v1 - 5.12.01 - Jean II
20  *      o Created this file.
21  *
22  * v2 - 13.12.01 - Jean II
23  *      o Move /proc/net/wireless stuff from net/core/dev.c to here
24  *      o Make Wireless Extension IOCTLs go through here
25  *      o Added iw_handler handling ;-)
26  *      o Added standard ioctl description
27  *      o Initial dumb commit strategy based on orinoco.c
28  *
29  * v3 - 19.12.01 - Jean II
30  *      o Make sure we don't go out of standard_ioctl[] in ioctl_standard_call
31  *      o Add event dispatcher function
32  *      o Add event description
33  *      o Propagate events as rtnetlink IFLA_WIRELESS option
34  *      o Generate event on selected SET requests
35  *
36  * v4 - 18.04.02 - Jean II
37  *      o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1
38  *
39  * v5 - 21.06.02 - Jean II
40  *      o Add IW_PRIV_TYPE_ADDR in priv_type_size (+cleanup)
41  *      o Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes
42  *      o Add IWEVCUSTOM for driver specific event/scanning token
43  *      o Turn on WE_STRICT_WRITE by default + kernel warning
44  *      o Fix WE_STRICT_WRITE in ioctl_export_private() (32 => iw_num)
45  *      o Fix off-by-one in test (extra_size <= IFNAMSIZ)
46  *
47  * v6 - 9.01.03 - Jean II
48  *      o Add common spy support : iw_handler_set_spy(), wireless_spy_update()
49  *      o Add enhanced spy support : iw_handler_set_thrspy() and event.
50  *      o Add WIRELESS_EXT version display in /proc/net/wireless
51  */
52
53 /***************************** INCLUDES *****************************/
54
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 */
61
62 #include <linux/wireless.h>             /* Pretty obvious */
63 #include <net/iw_handler.h>             /* New driver API */
64
65 /**************************** CONSTANTS ****************************/
66
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 */
70
71 /* Debuging stuff */
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 */
75
76 /* Options */
77 #define WE_EVENT_NETLINK        /* Propagate events using rtnetlink */
78 #define WE_SET_EVENT            /* Generate an event on some set commands */
79
80 /************************* GLOBAL VARIABLES *************************/
81 /*
82  * You should not use global variables, because of re-entrancy.
83  * On our case, it's only const, so it's OK...
84  */
85 /*
86  * Meta-data about all the standard Wireless Extension request we
87  * know about.
88  */
89 static const struct iw_ioctl_description        standard_ioctl[] = {
90         /* SIOCSIWCOMMIT */
91         { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
92         /* SIOCGIWNAME */
93         { IW_HEADER_TYPE_CHAR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
94         /* SIOCSIWNWID */
95         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
96         /* SIOCGIWNWID */
97         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
98         /* SIOCSIWFREQ */
99         { IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
100         /* SIOCGIWFREQ */
101         { IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
102         /* SIOCSIWMODE */
103         { IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
104         /* SIOCGIWMODE */
105         { IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
106         /* SIOCSIWSENS */
107         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
108         /* SIOCGIWSENS */
109         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
110         /* SIOCSIWRANGE */
111         { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
112         /* SIOCGIWRANGE */
113         { IW_HEADER_TYPE_POINT, 0, 1, 0, sizeof(struct iw_range), IW_DESCR_FLAG_DUMP},
114         /* SIOCSIWPRIV */
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},
118         /* SIOCSIWSTATS */
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},
122         /* SIOCSIWSPY */
123         { IW_HEADER_TYPE_POINT, 0, sizeof(struct sockaddr), 0, IW_MAX_SPY, 0},
124         /* SIOCGIWSPY */
125         { IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_SPY, 0},
126         /* SIOCSIWTHRSPY */
127         { IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
128         /* SIOCGIWTHRSPY */
129         { IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
130         /* SIOCSIWAP */
131         { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
132         /* SIOCGIWAP */
133         { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
134         /* -- hole -- */
135         { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
136         /* SIOCGIWAPLIST */
137         { IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_AP, 0},
138         /* SIOCSIWSCAN */
139         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
140         /* SIOCGIWSCAN */
141         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_SCAN_MAX_DATA, 0},
142         /* SIOCSIWESSID */
143         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_EVENT},
144         /* SIOCGIWESSID */
145         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_DUMP},
146         /* SIOCSIWNICKN */
147         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
148         /* SIOCGIWNICKN */
149         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
150         /* -- hole -- */
151         { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
152         /* -- hole -- */
153         { IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
154         /* SIOCSIWRATE */
155         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
156         /* SIOCGIWRATE */
157         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
158         /* SIOCSIWRTS */
159         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
160         /* SIOCGIWRTS */
161         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
162         /* SIOCSIWFRAG */
163         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
164         /* SIOCGIWFRAG */
165         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
166         /* SIOCSIWTXPOW */
167         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
168         /* SIOCGIWTXPOW */
169         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
170         /* SIOCSIWRETRY */
171         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
172         /* SIOCGIWRETRY */
173         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
174         /* SIOCSIWENCODE */
175         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT},
176         /* SIOCGIWENCODE */
177         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT},
178         /* SIOCSIWPOWER */
179         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
180         /* SIOCGIWPOWER */
181         { IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
182 };
183 static const int standard_ioctl_num = (sizeof(standard_ioctl) /
184                                        sizeof(struct iw_ioctl_description));
185
186 /*
187  * Meta-data about all the additional standard Wireless Extension events
188  * we know about.
189  */
190 static const struct iw_ioctl_description        standard_event[] = {
191         /* IWEVTXDROP */
192         { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
193         /* IWEVQUAL */
194         { IW_HEADER_TYPE_QUAL, 0, 0, 0, 0, 0},
195         /* IWEVCUSTOM */
196         { IW_HEADER_TYPE_POINT, 0, 1, 0, IW_CUSTOM_MAX, 0},
197         /* IWEVREGISTERED */
198         { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
199         /* IWEVEXPIRED */
200         { IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
201 };
202 static const int standard_event_num = (sizeof(standard_event) /
203                                        sizeof(struct iw_ioctl_description));
204
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 */
210         0,                              /* Not defined */
211         sizeof(__u32),                  /* IW_PRIV_TYPE_INT */
212         sizeof(struct iw_freq),         /* IW_PRIV_TYPE_FLOAT */
213         sizeof(struct sockaddr),        /* IW_PRIV_TYPE_ADDR */
214         0,                              /* Not defined */
215 };
216
217 /* Size (in bytes) of various events */
218 static const int event_type_size[] = {
219         IW_EV_LCP_LEN,                  /* IW_HEADER_TYPE_NULL */
220         0,
221         IW_EV_CHAR_LEN,                 /* IW_HEADER_TYPE_CHAR */
222         0,
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 */
226         0,
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 */
230 };
231
232 /************************ COMMON SUBROUTINES ************************/
233 /*
234  * Stuff that may be used in various place or doesn't fit in one
235  * of the section below.
236  */
237
238 /* ---------------------------------------------------------------- */
239 /*
240  * Return the driver handler associated with a specific Wireless Extension.
241  * Called from various place, so make sure it remains efficient.
242  */
243 static inline iw_handler get_handler(struct net_device *dev,
244                                      unsigned int cmd)
245 {
246         /* Don't "optimise" the following variable, it will crash */
247         unsigned int    index;          /* *MUST* be unsigned */
248
249         /* Check if we have some wireless handlers defined */
250         if(dev->wireless_handlers == NULL)
251                 return NULL;
252
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];
257
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];
262
263         /* Not found */
264         return NULL;
265 }
266
267 /* ---------------------------------------------------------------- */
268 /*
269  * Get statistics out of the driver
270  */
271 static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
272 {
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... */
279 }
280
281 /* ---------------------------------------------------------------- */
282 /*
283  * Call the commit handler in the driver
284  * (if exist and if conditions are right)
285  *
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.
295  *
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()" ;-)
299  */
300 static inline int call_commit_handler(struct net_device *       dev)
301 {
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,
306                                                            NULL, NULL);
307         } else
308                 return 0;               /* Command completed successfully */
309 }
310
311 /* ---------------------------------------------------------------- */
312 /*
313  * Number of private arguments
314  */
315 static inline int get_priv_size(__u16   args)
316 {
317         int     num = args & IW_PRIV_SIZE_MASK;
318         int     type = (args & IW_PRIV_TYPE_MASK) >> 12;
319
320         return num * priv_type_size[type];
321 }
322
323
324 /******************** /proc/net/wireless SUPPORT ********************/
325 /*
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 ;-)
329  *
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".
332  */
333
334 #ifdef CONFIG_PROC_FS
335
336 /* ---------------------------------------------------------------- */
337 /*
338  * Print one entry (line) of /proc/net/wireless
339  */
340 static inline int sprintf_wireless_stats(char *buffer, struct net_device *dev)
341 {
342         /* Get stats from the driver */
343         struct iw_statistics *stats;
344         int size;
345
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",
350                                dev->name,
351                                stats->status,
352                                stats->qual.qual,
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 ? '.' : ' ',
358                                stats->discard.nwid,
359                                stats->discard.code,
360                                stats->discard.fragment,
361                                stats->discard.retries,
362                                stats->discard.misc,
363                                stats->miss.beacon);
364                 stats->qual.updated = 0;
365         }
366         else
367                 size = 0;
368
369         return size;
370 }
371
372 /* ---------------------------------------------------------------- */
373 /*
374  * Print info for /proc/net/wireless (print all entries)
375  */
376 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
377                           int length)
378 {
379         int             len = 0;
380         off_t           begin = 0;
381         off_t           pos = 0;
382         int             size;
383         
384         struct net_device *     dev;
385
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",
389                        WIRELESS_EXT);
390         
391         pos += size;
392         len += size;
393
394         read_lock(&dev_base_lock);
395         for (dev = dev_base; dev != NULL; dev = dev->next) {
396                 size = sprintf_wireless_stats(buffer + len, dev);
397                 len += size;
398                 pos = begin + len;
399
400                 if (pos < offset) {
401                         len = 0;
402                         begin = pos;
403                 }
404                 if (pos > offset + length)
405                         break;
406         }
407         read_unlock(&dev_base_lock);
408
409         *start = buffer + (offset - begin);     /* Start of wanted data */
410         len -= (offset - begin);                /* Start slop */
411         if (len > length)
412                 len = length;                   /* Ending slop */
413         if (len < 0)
414                 len = 0;
415
416         return len;
417 }
418 #endif  /* CONFIG_PROC_FS */
419
420 /************************** IOCTL SUPPORT **************************/
421 /*
422  * The original user space API to configure all those Wireless Extensions
423  * is through IOCTLs.
424  * In there, we check if we need to call the new driver API (iw_handler)
425  * or just call the driver ioctl handler.
426  */
427
428 /* ---------------------------------------------------------------- */
429 /*
430  *      Allow programatic access to /proc/net/wireless even if /proc
431  *      doesn't exist... Also more efficient...
432  */
433 static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
434 {
435         /* Get stats from the driver */
436         struct iw_statistics *stats;
437
438         stats = get_wireless_stats(dev);
439         if (stats != (struct iw_statistics *) NULL) {
440                 struct iwreq *  wrq = (struct iwreq *)ifr;
441
442                 /* Copy statistics to the user buffer */
443                 if(copy_to_user(wrq->u.data.pointer, stats,
444                                 sizeof(struct iw_statistics)))
445                         return -EFAULT;
446
447                 /* Check if we need to clear the update flag */
448                 if(wrq->u.data.flags != 0)
449                         stats->qual.updated = 0;
450                 return 0;
451         } else
452                 return -EOPNOTSUPP;
453 }
454
455 /* ---------------------------------------------------------------- */
456 /*
457  * Export the driver private handler definition
458  * They will be picked up by tools like iwpriv...
459  */
460 static inline int ioctl_export_private(struct net_device *      dev,
461                                        struct ifreq *           ifr)
462 {
463         struct iwreq *                          iwr = (struct iwreq *) ifr;
464
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))
468                 return -EOPNOTSUPP;
469
470         /* Check NULL pointer */
471         if(iwr->u.data.pointer == NULL)
472                 return -EFAULT;
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);
477                 return -E2BIG;
478         }
479 #endif  /* WE_STRICT_WRITE */
480
481         /* Set the number of available ioctls. */
482         iwr->u.data.length = dev->wireless_handlers->num_private_args;
483
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))
488                 return -EFAULT;
489
490         return 0;
491 }
492
493 /* ---------------------------------------------------------------- */
494 /*
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.
498  */
499 static inline int ioctl_standard_call(struct net_device *       dev,
500                                       struct ifreq *            ifr,
501                                       unsigned int              cmd,
502                                       iw_handler                handler)
503 {
504         struct iwreq *                          iwr = (struct iwreq *) ifr;
505         const struct iw_ioctl_description *     descr;
506         struct iw_request_info                  info;
507         int                                     ret = -EINVAL;
508         int                                     user_size = 0;
509
510         /* Get the description of the IOCTL */
511         if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
512                 return -EOPNOTSUPP;
513         descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
514
515 #ifdef WE_IOCTL_DEBUG
516         printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
517                ifr->ifr_name, cmd);
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 */
520
521         /* Prepare the call */
522         info.cmd = cmd;
523         info.flags = 0;
524
525         /* Check if we have a pointer to user space data or not */
526         if(descr->header_type != IW_HEADER_TYPE_POINT) {
527
528                 /* No extra arguments. Trivial to handle */
529                 ret = handler(dev, &info, &(iwr->u), NULL);
530
531 #ifdef WE_SET_EVENT
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 */
537         } else {
538                 char *  extra;
539                 int     err;
540
541                 /* Check what user space is giving us */
542                 if(IW_IS_SET(cmd)) {
543                         /* Check NULL pointer */
544                         if((iwr->u.data.pointer == NULL) &&
545                            (iwr->u.data.length != 0))
546                                 return -EFAULT;
547                         /* Check if number of token fits within bounds */
548                         if(iwr->u.data.length > descr->max_tokens)
549                                 return -E2BIG;
550                         if(iwr->u.data.length < descr->min_tokens)
551                                 return -EINVAL;
552                 } else {
553                         /* Check NULL pointer */
554                         if(iwr->u.data.pointer == NULL)
555                                 return -EFAULT;
556                         /* Save user space buffer size for checking */
557                         user_size = iwr->u.data.length;
558                 }
559
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 */
564
565                 /* Always allocate for max space. Easier, and won't last
566                  * long... */
567                 extra = kmalloc(descr->max_tokens * descr->token_size,
568                                 GFP_KERNEL);
569                 if (extra == NULL) {
570                         return -ENOMEM;
571                 }
572
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,
576                                              iwr->u.data.length *
577                                              descr->token_size);
578                         if (err) {
579                                 kfree(extra);
580                                 return -EFAULT;
581                         }
582 #ifdef WE_IOCTL_DEBUG
583                         printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
584                                dev->name,
585                                iwr->u.data.length * descr->token_size);
586 #endif  /* WE_IOCTL_DEBUG */
587                 }
588
589                 /* Call the handler */
590                 ret = handler(dev, &info, &(iwr->u), extra);
591
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);
598                                 kfree(extra);
599                                 return -E2BIG;
600                         }
601 #endif  /* WE_STRICT_WRITE */
602
603                         err = copy_to_user(iwr->u.data.pointer, extra,
604                                            iwr->u.data.length *
605                                            descr->token_size);
606                         if (err)
607                                 ret =  -EFAULT;                            
608 #ifdef WE_IOCTL_DEBUG
609                         printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
610                                dev->name,
611                                iwr->u.data.length * descr->token_size);
612 #endif  /* WE_IOCTL_DEBUG */
613                 }
614
615 #ifdef WE_SET_EVENT
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);
623                         else
624                                 wireless_send_event(dev, cmd, &(iwr->u),
625                                                     extra);
626                 }
627 #endif  /* WE_SET_EVENT */
628
629                 /* Cleanup - I told you it wasn't that long ;-) */
630                 kfree(extra);
631         }
632
633         /* Call commit handler if needed and defined */
634         if(ret == -EIWCOMMIT)
635                 ret = call_commit_handler(dev);
636
637         /* Here, we will generate the appropriate event if needed */
638
639         return ret;
640 }
641
642 /* ---------------------------------------------------------------- */
643 /*
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.
650  *
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
653  * far too hairy...
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
656  * old driver API).
657  */
658 static inline int ioctl_private_call(struct net_device *        dev,
659                                      struct ifreq *             ifr,
660                                      unsigned int               cmd,
661                                      iw_handler         handler)
662 {
663         struct iwreq *                  iwr = (struct iwreq *) ifr;
664         struct iw_priv_args *           descr = NULL;
665         struct iw_request_info          info;
666         int                             extra_size = 0;
667         int                             i;
668         int                             ret = -EINVAL;
669
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]);
674                         break;
675                 }
676
677 #ifdef WE_IOCTL_DEBUG
678         printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
679                ifr->ifr_name, cmd);
680         if(descr) {
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);
684         }
685 #endif  /* WE_IOCTL_DEBUG */
686
687         /* Compute the size of the set/get arguments */
688         if(descr != NULL) {
689                 if(IW_IS_SET(cmd)) {
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);
695
696                         /* Size of set arguments */
697                         extra_size = get_priv_size(descr->set_args);
698
699                         /* Does it fits in iwr ? */
700                         if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
701                            ((extra_size + offset) <= IFNAMSIZ))
702                                 extra_size = 0;
703                 } else {
704                         /* Size of set arguments */
705                         extra_size = get_priv_size(descr->get_args);
706
707                         /* Does it fits in iwr ? */
708                         if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
709                            (extra_size <= IFNAMSIZ))
710                                 extra_size = 0;
711                 }
712         }
713
714         /* Prepare the call */
715         info.cmd = cmd;
716         info.flags = 0;
717
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));
722         } else {
723                 char *  extra;
724                 int     err;
725
726                 /* Check what user space is giving us */
727                 if(IW_IS_SET(cmd)) {
728                         /* Check NULL pointer */
729                         if((iwr->u.data.pointer == NULL) &&
730                            (iwr->u.data.length != 0))
731                                 return -EFAULT;
732
733                         /* Does it fits within bounds ? */
734                         if(iwr->u.data.length > (descr->set_args &
735                                                  IW_PRIV_SIZE_MASK))
736                                 return -E2BIG;
737                 } else {
738                         /* Check NULL pointer */
739                         if(iwr->u.data.pointer == NULL)
740                                 return -EFAULT;
741                 }
742
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 */
747
748                 /* Always allocate for max space. Easier, and won't last
749                  * long... */
750                 extra = kmalloc(extra_size, GFP_KERNEL);
751                 if (extra == NULL) {
752                         return -ENOMEM;
753                 }
754
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,
758                                              extra_size);
759                         if (err) {
760                                 kfree(extra);
761                                 return -EFAULT;
762                         }
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 */
767                 }
768
769                 /* Call the handler */
770                 ret = handler(dev, &info, &(iwr->u), extra);
771
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,
775                                            extra_size);
776                         if (err)
777                                 ret =  -EFAULT;                            
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 */
782                 }
783
784                 /* Cleanup - I told you it wasn't that long ;-) */
785                 kfree(extra);
786         }
787
788
789         /* Call commit handler if needed and defined */
790         if(ret == -EIWCOMMIT)
791                 ret = call_commit_handler(dev);
792
793         return ret;
794 }
795
796 /* ---------------------------------------------------------------- */
797 /*
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...
801  */
802 int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
803 {
804         struct net_device *dev;
805         iw_handler      handler;
806
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 */
809
810         /* Make sure the device exist */
811         if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
812                 return -ENODEV;
813
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) */
817         switch(cmd) 
818         {
819                 case SIOCGIWSTATS:
820                         /* Get Wireless Stats */
821                         return dev_iwstats(dev, ifr);
822
823                 case SIOCGIWPRIV:
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);
829                         }
830                         // ## Fall-through for old API ##
831                 default:
832                         /* Generic IOCTL */
833                         /* Basic check */
834                         if (!netif_device_present(dev))
835                                 return -ENODEV;
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,
842                                                                    ifr,
843                                                                    cmd,
844                                                                    handler);
845                                 else
846                                         return ioctl_private_call(dev,
847                                                                   ifr,
848                                                                   cmd,
849                                                                   handler);
850                         }
851                         /* Old driver API : call driver ioctl handler */
852                         if (dev->do_ioctl) {
853                                 return dev->do_ioctl(dev, ifr, cmd);
854                         }
855                         return -EOPNOTSUPP;
856         }
857         /* Not reached */
858         return -EINVAL;
859 }
860
861 /************************* EVENT PROCESSING *************************/
862 /*
863  * Process events generated by the wireless layer or the driver.
864  * Most often, the event will be propagated through rtnetlink
865  */
866
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> */
870
871 /* ---------------------------------------------------------------- */
872 /*
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).
877  */
878 static inline int rtnetlink_fill_iwinfo(struct sk_buff *        skb,
879                                         struct net_device *     dev,
880                                         int                     type,
881                                         char *                  event,
882                                         int                     event_len)
883 {
884         struct ifinfomsg *r;
885         struct nlmsghdr  *nlh;
886         unsigned char    *b = skb->tail;
887
888         nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
889         r = NLMSG_DATA(nlh);
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 */
895
896         /* Add the wireless events in the netlink packet */
897         RTA_PUT(skb, IFLA_WIRELESS,
898                 event_len, event);
899
900         nlh->nlmsg_len = skb->tail - b;
901         return skb->len;
902
903 nlmsg_failure:
904 rtattr_failure:
905         skb_trim(skb, b - skb->data);
906         return -1;
907 }
908
909 /* ---------------------------------------------------------------- */
910 /*
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.
915  */
916 static inline void rtmsg_iwinfo(struct net_device *     dev,
917                                 char *                  event,
918                                 int                     event_len)
919 {
920         struct sk_buff *skb;
921         int size = NLMSG_GOODSIZE;
922
923         skb = alloc_skb(size, GFP_ATOMIC);
924         if (!skb)
925                 return;
926
927         if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
928                                   event, event_len) < 0) {
929                 kfree_skb(skb);
930                 return;
931         }
932         NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
933         netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_ATOMIC);
934 }
935 #endif  /* WE_EVENT_NETLINK */
936
937 /* ---------------------------------------------------------------- */
938 /*
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.
942  */
943 void wireless_send_event(struct net_device *    dev,
944                          unsigned int           cmd,
945                          union iwreq_data *     wrqu,
946                          char *                 extra)
947 {
948         const struct iw_ioctl_description *     descr = NULL;
949         int extra_len = 0;
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 */
955
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]);
961         } else {
962                 cmd_index = cmd - IWEVFIRST;
963                 if(cmd_index < standard_event_num)
964                         descr = &(standard_event[cmd_index]);
965         }
966         /* Don't accept unknown events */
967         if(descr == NULL) {
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...
974                  */
975                 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
976                        dev->name, cmd);
977                 return;
978         }
979 #ifdef WE_EVENT_DEBUG
980         printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
981                dev->name, cmd);
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 */
984
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);
990                         return;
991                 }
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);
994                         return;
995                 }
996                 /* Calculate extra_len - extra is NULL for restricted events */
997                 if(extra != NULL)
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 */
1002         }
1003
1004         /* Total length of the event */
1005         hdr_len = event_type_size[descr->header_type];
1006         event_len = hdr_len + extra_len;
1007
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 */
1011
1012         /* Create temporary buffer to hold the event */
1013         event = kmalloc(event_len, GFP_ATOMIC);
1014         if(event == NULL)
1015                 return;
1016
1017         /* Fill event */
1018         event->len = event_len;
1019         event->cmd = cmd;
1020         memcpy(&event->u, wrqu, hdr_len - IW_EV_LCP_LEN);
1021         if(extra != NULL)
1022                 memcpy(((char *) event) + hdr_len, extra, extra_len);
1023
1024 #ifdef WE_EVENT_NETLINK
1025         /* rtnetlink event channel */
1026         rtmsg_iwinfo(dev, (char *) event, event_len);
1027 #endif  /* WE_EVENT_NETLINK */
1028
1029         /* Cleanup */
1030         kfree(event);
1031
1032         return;         /* Always success, I guess ;-) */
1033 }
1034
1035 /********************** ENHANCED IWSPY SUPPORT **********************/
1036 /*
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
1041  * private part.
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
1046  */
1047
1048 /*------------------------------------------------------------------*/
1049 /*
1050  * Standard Wireless Handler : set Spy List
1051  */
1052 int iw_handler_set_spy(struct net_device *      dev,
1053                        struct iw_request_info * info,
1054                        union iwreq_data *       wrqu,
1055                        char *                   extra)
1056 {
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;
1061
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;
1066
1067         /* Are there are addresses to copy? */
1068         if(wrqu->data.length > 0) {
1069                 int i;
1070
1071                 /* Copy addresses */
1072                 for(i = 0; i < wrqu->data.length; i++)
1073                         memcpy(spydata->spy_address[i], address[i].sa_data,
1074                                ETH_ALEN);
1075                 /* Reset stats */
1076                 memset(spydata->spy_stat, 0,
1077                        sizeof(struct iw_quality) * IW_MAX_SPY);
1078
1079 #ifdef WE_SPY_DEBUG
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++)
1082                         printk(KERN_DEBUG
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 */
1091         }
1092         /* Enable addresses */
1093         spydata->spy_number = wrqu->data.length;
1094
1095         return 0;
1096 #else /* IW_WIRELESS_SPY */
1097         return -EOPNOTSUPP;
1098 #endif /* IW_WIRELESS_SPY */
1099 }
1100
1101 /*------------------------------------------------------------------*/
1102 /*
1103  * Standard Wireless Handler : get Spy List
1104  */
1105 int iw_handler_get_spy(struct net_device *      dev,
1106                        struct iw_request_info * info,
1107                        union iwreq_data *       wrqu,
1108                        char *                   extra)
1109 {
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;
1114         int                     i;
1115
1116         wrqu->data.length = spydata->spy_number;
1117
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;
1122         }
1123         /* Copy stats to the user buffer (just after). */
1124         if(spydata->spy_number > 0)
1125                 memcpy(extra  + (sizeof(struct sockaddr) *spydata->spy_number),
1126                        spydata->spy_stat,
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;
1131         return 0;
1132 #else /* IW_WIRELESS_SPY */
1133         return -EOPNOTSUPP;
1134 #endif /* IW_WIRELESS_SPY */
1135 }
1136
1137 /*------------------------------------------------------------------*/
1138 /*
1139  * Standard Wireless Handler : set spy threshold
1140  */
1141 int iw_handler_set_thrspy(struct net_device *   dev,
1142                           struct iw_request_info *info,
1143                           union iwreq_data *    wrqu,
1144                           char *                extra)
1145 {
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;
1150
1151         /* Just do it */
1152         memcpy(&(spydata->spy_thr_low), &(threshold->low),
1153                2 * sizeof(struct iw_quality));
1154
1155         /* Clear flag */
1156         memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1157
1158 #ifdef WE_SPY_DEBUG
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 */
1161
1162         return 0;
1163 #else /* IW_WIRELESS_THRSPY */
1164         return -EOPNOTSUPP;
1165 #endif /* IW_WIRELESS_THRSPY */
1166 }
1167
1168 /*------------------------------------------------------------------*/
1169 /*
1170  * Standard Wireless Handler : get spy threshold
1171  */
1172 int iw_handler_get_thrspy(struct net_device *   dev,
1173                           struct iw_request_info *info,
1174                           union iwreq_data *    wrqu,
1175                           char *                extra)
1176 {
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;
1181
1182         /* Just do it */
1183         memcpy(&(threshold->low), &(spydata->spy_thr_low),
1184                2 * sizeof(struct iw_quality));
1185
1186         return 0;
1187 #else /* IW_WIRELESS_THRSPY */
1188         return -EOPNOTSUPP;
1189 #endif /* IW_WIRELESS_THRSPY */
1190 }
1191
1192 #ifdef IW_WIRELESS_THRSPY
1193 /*------------------------------------------------------------------*/
1194 /*
1195  * Prepare and send a Spy Threshold event
1196  */
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)
1201 {
1202         union iwreq_data        wrqu;
1203         struct iw_thrspy        threshold;
1204
1205         /* Init */
1206         wrqu.data.length = 1;
1207         wrqu.data.flags = 0;
1208         /* Copy address */
1209         memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1210         threshold.addr.sa_family = ARPHRD_ETHER;
1211         /* Copy stats */
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));
1216
1217 #ifdef WE_SPY_DEBUG
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 */
1226
1227         /* Send event to user space */
1228         wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1229 }
1230 #endif /* IW_WIRELESS_THRSPY */
1231
1232 /* ---------------------------------------------------------------- */
1233 /*
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...
1238  */
1239 void wireless_spy_update(struct net_device *    dev,
1240                          unsigned char *        address,
1241                          struct iw_quality *    wstats)
1242 {
1243 #ifdef IW_WIRELESS_SPY
1244         struct iw_spy_data *    spydata = (dev->priv +
1245                                            dev->wireless_handlers->spy_offset);
1246         int                     i;
1247         int                     match = -1;
1248
1249 #ifdef WE_SPY_DEBUG
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 */
1252
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));
1258                         match = i;
1259                 }
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. */
1265         if(match >= 0) {
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,
1270                                                      address, wstats);
1271                         }
1272                 } else {
1273                         if(wstats->level < spydata->spy_thr_low.level) {
1274                                 spydata->spy_thr_under[match] = 1;
1275                                 iw_send_thrspy_event(dev, spydata,
1276                                                      address, wstats);
1277                         }
1278                 }
1279         }
1280 #endif /* IW_WIRELESS_THRSPY */
1281 #endif /* IW_WIRELESS_SPY */
1282 }