vserver 1.9.5.x5
[linux-2.6.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-2004 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  * v6 - 18.06.04 - Jean II
53  *      o Change get_spydata() method for added safety
54  *      o Remove spy #ifdef, they are always on -> cleaner code
55  *      o Allow any size GET request if user specifies length > max
56  *              and if request has IW_DESCR_FLAG_NOMAX flag or is SIOCGIWPRIV
57  *      o Start migrating get_wireless_stats to struct iw_handler_def
58  *      o Add wmb() in iw_handler_set_spy() for non-coherent archs/cpus
59  * Based on patch from Pavel Roskin <proski@gnu.org> :
60  *      o Fix kernel data leak to user space in private handler handling
61  */
62
63 /***************************** INCLUDES *****************************/
64
65 #include <linux/config.h>               /* Not needed ??? */
66 #include <linux/module.h>
67 #include <linux/types.h>                /* off_t */
68 #include <linux/netdevice.h>            /* struct ifreq, dev_get_by_name() */
69 #include <linux/proc_fs.h>
70 #include <linux/rtnetlink.h>            /* rtnetlink stuff */
71 #include <linux/seq_file.h>
72 #include <linux/init.h>                 /* for __init */
73 #include <linux/if_arp.h>               /* ARPHRD_ETHER */
74
75 #include <linux/wireless.h>             /* Pretty obvious */
76 #include <net/iw_handler.h>             /* New driver API */
77
78 #include <asm/uaccess.h>                /* copy_to_user() */
79
80 /**************************** CONSTANTS ****************************/
81
82 /* Debugging stuff */
83 #undef WE_IOCTL_DEBUG           /* Debug IOCTL API */
84 #undef WE_EVENT_DEBUG           /* Debug Event dispatcher */
85 #undef WE_SPY_DEBUG             /* Debug enhanced spy support */
86
87 /* Options */
88 #define WE_EVENT_NETLINK        /* Propagate events using rtnetlink */
89 #define WE_SET_EVENT            /* Generate an event on some set commands */
90
91 /************************* GLOBAL VARIABLES *************************/
92 /*
93  * You should not use global variables, because of re-entrancy.
94  * On our case, it's only const, so it's OK...
95  */
96 /*
97  * Meta-data about all the standard Wireless Extension request we
98  * know about.
99  */
100 static const struct iw_ioctl_description standard_ioctl[] = {
101         [SIOCSIWCOMMIT  - SIOCIWFIRST] = {
102                 .header_type    = IW_HEADER_TYPE_NULL,
103         },
104         [SIOCGIWNAME    - SIOCIWFIRST] = {
105                 .header_type    = IW_HEADER_TYPE_CHAR,
106                 .flags          = IW_DESCR_FLAG_DUMP,
107         },
108         [SIOCSIWNWID    - SIOCIWFIRST] = {
109                 .header_type    = IW_HEADER_TYPE_PARAM,
110                 .flags          = IW_DESCR_FLAG_EVENT,
111         },
112         [SIOCGIWNWID    - SIOCIWFIRST] = {
113                 .header_type    = IW_HEADER_TYPE_PARAM,
114                 .flags          = IW_DESCR_FLAG_DUMP,
115         },
116         [SIOCSIWFREQ    - SIOCIWFIRST] = {
117                 .header_type    = IW_HEADER_TYPE_FREQ,
118                 .flags          = IW_DESCR_FLAG_EVENT,
119         },
120         [SIOCGIWFREQ    - SIOCIWFIRST] = {
121                 .header_type    = IW_HEADER_TYPE_FREQ,
122                 .flags          = IW_DESCR_FLAG_DUMP,
123         },
124         [SIOCSIWMODE    - SIOCIWFIRST] = {
125                 .header_type    = IW_HEADER_TYPE_UINT,
126                 .flags          = IW_DESCR_FLAG_EVENT,
127         },
128         [SIOCGIWMODE    - SIOCIWFIRST] = {
129                 .header_type    = IW_HEADER_TYPE_UINT,
130                 .flags          = IW_DESCR_FLAG_DUMP,
131         },
132         [SIOCSIWSENS    - SIOCIWFIRST] = {
133                 .header_type    = IW_HEADER_TYPE_PARAM,
134         },
135         [SIOCGIWSENS    - SIOCIWFIRST] = {
136                 .header_type    = IW_HEADER_TYPE_PARAM,
137         },
138         [SIOCSIWRANGE   - SIOCIWFIRST] = {
139                 .header_type    = IW_HEADER_TYPE_NULL,
140         },
141         [SIOCGIWRANGE   - SIOCIWFIRST] = {
142                 .header_type    = IW_HEADER_TYPE_POINT,
143                 .token_size     = 1,
144                 .max_tokens     = sizeof(struct iw_range),
145                 .flags          = IW_DESCR_FLAG_DUMP,
146         },
147         [SIOCSIWPRIV    - SIOCIWFIRST] = {
148                 .header_type    = IW_HEADER_TYPE_NULL,
149         },
150         [SIOCGIWPRIV    - SIOCIWFIRST] = { /* (handled directly by us) */
151                 .header_type    = IW_HEADER_TYPE_NULL,
152         },
153         [SIOCSIWSTATS   - SIOCIWFIRST] = {
154                 .header_type    = IW_HEADER_TYPE_NULL,
155         },
156         [SIOCGIWSTATS   - SIOCIWFIRST] = { /* (handled directly by us) */
157                 .header_type    = IW_HEADER_TYPE_NULL,
158                 .flags          = IW_DESCR_FLAG_DUMP,
159         },
160         [SIOCSIWSPY     - SIOCIWFIRST] = {
161                 .header_type    = IW_HEADER_TYPE_POINT,
162                 .token_size     = sizeof(struct sockaddr),
163                 .max_tokens     = IW_MAX_SPY,
164         },
165         [SIOCGIWSPY     - SIOCIWFIRST] = {
166                 .header_type    = IW_HEADER_TYPE_POINT,
167                 .token_size     = sizeof(struct sockaddr) +
168                                   sizeof(struct iw_quality),
169                 .max_tokens     = IW_MAX_SPY,
170         },
171         [SIOCSIWTHRSPY  - SIOCIWFIRST] = {
172                 .header_type    = IW_HEADER_TYPE_POINT,
173                 .token_size     = sizeof(struct iw_thrspy),
174                 .min_tokens     = 1,
175                 .max_tokens     = 1,
176         },
177         [SIOCGIWTHRSPY  - SIOCIWFIRST] = {
178                 .header_type    = IW_HEADER_TYPE_POINT,
179                 .token_size     = sizeof(struct iw_thrspy),
180                 .min_tokens     = 1,
181                 .max_tokens     = 1,
182         },
183         [SIOCSIWAP      - SIOCIWFIRST] = {
184                 .header_type    = IW_HEADER_TYPE_ADDR,
185         },
186         [SIOCGIWAP      - SIOCIWFIRST] = {
187                 .header_type    = IW_HEADER_TYPE_ADDR,
188                 .flags          = IW_DESCR_FLAG_DUMP,
189         },
190         [SIOCGIWAPLIST  - SIOCIWFIRST] = {
191                 .header_type    = IW_HEADER_TYPE_POINT,
192                 .token_size     = sizeof(struct sockaddr) +
193                                   sizeof(struct iw_quality),
194                 .max_tokens     = IW_MAX_AP,
195                 .flags          = IW_DESCR_FLAG_NOMAX,
196         },
197         [SIOCSIWSCAN    - SIOCIWFIRST] = {
198                 .header_type    = IW_HEADER_TYPE_PARAM,
199         },
200         [SIOCGIWSCAN    - SIOCIWFIRST] = {
201                 .header_type    = IW_HEADER_TYPE_POINT,
202                 .token_size     = 1,
203                 .max_tokens     = IW_SCAN_MAX_DATA,
204                 .flags          = IW_DESCR_FLAG_NOMAX,
205         },
206         [SIOCSIWESSID   - SIOCIWFIRST] = {
207                 .header_type    = IW_HEADER_TYPE_POINT,
208                 .token_size     = 1,
209                 .max_tokens     = IW_ESSID_MAX_SIZE + 1,
210                 .flags          = IW_DESCR_FLAG_EVENT,
211         },
212         [SIOCGIWESSID   - SIOCIWFIRST] = {
213                 .header_type    = IW_HEADER_TYPE_POINT,
214                 .token_size     = 1,
215                 .max_tokens     = IW_ESSID_MAX_SIZE + 1,
216                 .flags          = IW_DESCR_FLAG_DUMP,
217         },
218         [SIOCSIWNICKN   - SIOCIWFIRST] = {
219                 .header_type    = IW_HEADER_TYPE_POINT,
220                 .token_size     = 1,
221                 .max_tokens     = IW_ESSID_MAX_SIZE + 1,
222         },
223         [SIOCGIWNICKN   - SIOCIWFIRST] = {
224                 .header_type    = IW_HEADER_TYPE_POINT,
225                 .token_size     = 1,
226                 .max_tokens     = IW_ESSID_MAX_SIZE + 1,
227         },
228         [SIOCSIWRATE    - SIOCIWFIRST] = {
229                 .header_type    = IW_HEADER_TYPE_PARAM,
230         },
231         [SIOCGIWRATE    - SIOCIWFIRST] = {
232                 .header_type    = IW_HEADER_TYPE_PARAM,
233         },
234         [SIOCSIWRTS     - SIOCIWFIRST] = {
235                 .header_type    = IW_HEADER_TYPE_PARAM,
236         },
237         [SIOCGIWRTS     - SIOCIWFIRST] = {
238                 .header_type    = IW_HEADER_TYPE_PARAM,
239         },
240         [SIOCSIWFRAG    - SIOCIWFIRST] = {
241                 .header_type    = IW_HEADER_TYPE_PARAM,
242         },
243         [SIOCGIWFRAG    - SIOCIWFIRST] = {
244                 .header_type    = IW_HEADER_TYPE_PARAM,
245         },
246         [SIOCSIWTXPOW   - SIOCIWFIRST] = {
247                 .header_type    = IW_HEADER_TYPE_PARAM,
248         },
249         [SIOCGIWTXPOW   - SIOCIWFIRST] = {
250                 .header_type    = IW_HEADER_TYPE_PARAM,
251         },
252         [SIOCSIWRETRY   - SIOCIWFIRST] = {
253                 .header_type    = IW_HEADER_TYPE_PARAM,
254         },
255         [SIOCGIWRETRY   - SIOCIWFIRST] = {
256                 .header_type    = IW_HEADER_TYPE_PARAM,
257         },
258         [SIOCSIWENCODE  - SIOCIWFIRST] = {
259                 .header_type    = IW_HEADER_TYPE_POINT,
260                 .token_size     = 1,
261                 .max_tokens     = IW_ENCODING_TOKEN_MAX,
262                 .flags          = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
263         },
264         [SIOCGIWENCODE  - SIOCIWFIRST] = {
265                 .header_type    = IW_HEADER_TYPE_POINT,
266                 .token_size     = 1,
267                 .max_tokens     = IW_ENCODING_TOKEN_MAX,
268                 .flags          = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
269         },
270         [SIOCSIWPOWER   - SIOCIWFIRST] = {
271                 .header_type    = IW_HEADER_TYPE_PARAM,
272         },
273         [SIOCGIWPOWER   - SIOCIWFIRST] = {
274                 .header_type    = IW_HEADER_TYPE_PARAM,
275         },
276 };
277 static const int standard_ioctl_num = (sizeof(standard_ioctl) /
278                                        sizeof(struct iw_ioctl_description));
279
280 /*
281  * Meta-data about all the additional standard Wireless Extension events
282  * we know about.
283  */
284 static const struct iw_ioctl_description standard_event[] = {
285         [IWEVTXDROP     - IWEVFIRST] = {
286                 .header_type    = IW_HEADER_TYPE_ADDR,
287         },
288         [IWEVQUAL       - IWEVFIRST] = {
289                 .header_type    = IW_HEADER_TYPE_QUAL,
290         },
291         [IWEVCUSTOM     - IWEVFIRST] = {
292                 .header_type    = IW_HEADER_TYPE_POINT,
293                 .token_size     = 1,
294                 .max_tokens     = IW_CUSTOM_MAX,
295         },
296         [IWEVREGISTERED - IWEVFIRST] = {
297                 .header_type    = IW_HEADER_TYPE_ADDR,
298         },
299         [IWEVEXPIRED    - IWEVFIRST] = {
300                 .header_type    = IW_HEADER_TYPE_ADDR, 
301         },
302 };
303 static const int standard_event_num = (sizeof(standard_event) /
304                                        sizeof(struct iw_ioctl_description));
305
306 /* Size (in bytes) of the various private data types */
307 static const char iw_priv_type_size[] = {
308         0,                              /* IW_PRIV_TYPE_NONE */
309         1,                              /* IW_PRIV_TYPE_BYTE */
310         1,                              /* IW_PRIV_TYPE_CHAR */
311         0,                              /* Not defined */
312         sizeof(__u32),                  /* IW_PRIV_TYPE_INT */
313         sizeof(struct iw_freq),         /* IW_PRIV_TYPE_FLOAT */
314         sizeof(struct sockaddr),        /* IW_PRIV_TYPE_ADDR */
315         0,                              /* Not defined */
316 };
317
318 /* Size (in bytes) of various events */
319 static const int event_type_size[] = {
320         IW_EV_LCP_LEN,                  /* IW_HEADER_TYPE_NULL */
321         0,
322         IW_EV_CHAR_LEN,                 /* IW_HEADER_TYPE_CHAR */
323         0,
324         IW_EV_UINT_LEN,                 /* IW_HEADER_TYPE_UINT */
325         IW_EV_FREQ_LEN,                 /* IW_HEADER_TYPE_FREQ */
326         IW_EV_ADDR_LEN,                 /* IW_HEADER_TYPE_ADDR */
327         0,
328         IW_EV_POINT_LEN,                /* Without variable payload */
329         IW_EV_PARAM_LEN,                /* IW_HEADER_TYPE_PARAM */
330         IW_EV_QUAL_LEN,                 /* IW_HEADER_TYPE_QUAL */
331 };
332
333 /************************ COMMON SUBROUTINES ************************/
334 /*
335  * Stuff that may be used in various place or doesn't fit in one
336  * of the section below.
337  */
338
339 /* ---------------------------------------------------------------- */
340 /*
341  * Return the driver handler associated with a specific Wireless Extension.
342  * Called from various place, so make sure it remains efficient.
343  */
344 static inline iw_handler get_handler(struct net_device *dev,
345                                      unsigned int cmd)
346 {
347         /* Don't "optimise" the following variable, it will crash */
348         unsigned int    index;          /* *MUST* be unsigned */
349
350         /* Check if we have some wireless handlers defined */
351         if(dev->wireless_handlers == NULL)
352                 return NULL;
353
354         /* Try as a standard command */
355         index = cmd - SIOCIWFIRST;
356         if(index < dev->wireless_handlers->num_standard)
357                 return dev->wireless_handlers->standard[index];
358
359         /* Try as a private command */
360         index = cmd - SIOCIWFIRSTPRIV;
361         if(index < dev->wireless_handlers->num_private)
362                 return dev->wireless_handlers->private[index];
363
364         /* Not found */
365         return NULL;
366 }
367
368 /* ---------------------------------------------------------------- */
369 /*
370  * Get statistics out of the driver
371  */
372 static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
373 {
374         /* New location */
375         if((dev->wireless_handlers != NULL) &&
376            (dev->wireless_handlers->get_wireless_stats != NULL))
377                 return dev->wireless_handlers->get_wireless_stats(dev);
378
379         /* Old location, will be phased out in next WE */
380         return (dev->get_wireless_stats ?
381                 dev->get_wireless_stats(dev) :
382                 (struct iw_statistics *) NULL);
383 }
384
385 /* ---------------------------------------------------------------- */
386 /*
387  * Call the commit handler in the driver
388  * (if exist and if conditions are right)
389  *
390  * Note : our current commit strategy is currently pretty dumb,
391  * but we will be able to improve on that...
392  * The goal is to try to agreagate as many changes as possible
393  * before doing the commit. Drivers that will define a commit handler
394  * are usually those that need a reset after changing parameters, so
395  * we want to minimise the number of reset.
396  * A cool idea is to use a timer : at each "set" command, we re-set the
397  * timer, when the timer eventually fires, we call the driver.
398  * Hopefully, more on that later.
399  *
400  * Also, I'm waiting to see how many people will complain about the
401  * netif_running(dev) test. I'm open on that one...
402  * Hopefully, the driver will remember to do a commit in "open()" ;-)
403  */
404 static inline int call_commit_handler(struct net_device *       dev)
405 {
406         if((netif_running(dev)) &&
407            (dev->wireless_handlers->standard[0] != NULL)) {
408                 /* Call the commit handler on the driver */
409                 return dev->wireless_handlers->standard[0](dev, NULL,
410                                                            NULL, NULL);
411         } else
412                 return 0;               /* Command completed successfully */
413 }
414
415 /* ---------------------------------------------------------------- */
416 /*
417  * Calculate size of private arguments
418  */
419 static inline int get_priv_size(__u16   args)
420 {
421         int     num = args & IW_PRIV_SIZE_MASK;
422         int     type = (args & IW_PRIV_TYPE_MASK) >> 12;
423
424         return num * iw_priv_type_size[type];
425 }
426
427 /* ---------------------------------------------------------------- */
428 /*
429  * Re-calculate the size of private arguments
430  */
431 static inline int adjust_priv_size(__u16                args,
432                                    union iwreq_data *   wrqu)
433 {
434         int     num = wrqu->data.length;
435         int     max = args & IW_PRIV_SIZE_MASK;
436         int     type = (args & IW_PRIV_TYPE_MASK) >> 12;
437
438         /* Make sure the driver doesn't goof up */
439         if (max < num)
440                 num = max;
441
442         return num * iw_priv_type_size[type];
443 }
444
445
446 /******************** /proc/net/wireless SUPPORT ********************/
447 /*
448  * The /proc/net/wireless file is a human readable user-space interface
449  * exporting various wireless specific statistics from the wireless devices.
450  * This is the most popular part of the Wireless Extensions ;-)
451  *
452  * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
453  * The content of the file is basically the content of "struct iw_statistics".
454  */
455
456 #ifdef CONFIG_PROC_FS
457
458 /* ---------------------------------------------------------------- */
459 /*
460  * Print one entry (line) of /proc/net/wireless
461  */
462 static __inline__ void wireless_seq_printf_stats(struct seq_file *seq,
463                                                  struct net_device *dev)
464 {
465         /* Get stats from the driver */
466         struct iw_statistics *stats = get_wireless_stats(dev);
467
468         if (stats) {
469                 seq_printf(seq, "%6s: %04x  %3d%c  %3d%c  %3d%c  %6d %6d %6d "
470                                 "%6d %6d   %6d\n",
471                            dev->name, stats->status, stats->qual.qual,
472                            stats->qual.updated & IW_QUAL_QUAL_UPDATED
473                            ? '.' : ' ',
474                            ((__u8) stats->qual.level),
475                            stats->qual.updated & IW_QUAL_LEVEL_UPDATED
476                            ? '.' : ' ',
477                            ((__u8) stats->qual.noise),
478                            stats->qual.updated & IW_QUAL_NOISE_UPDATED
479                            ? '.' : ' ',
480                            stats->discard.nwid, stats->discard.code,
481                            stats->discard.fragment, stats->discard.retries,
482                            stats->discard.misc, stats->miss.beacon);
483                 stats->qual.updated = 0;
484         }
485 }
486
487 /* ---------------------------------------------------------------- */
488 /*
489  * Print info for /proc/net/wireless (print all entries)
490  */
491 static int wireless_seq_show(struct seq_file *seq, void *v)
492 {
493         if (v == SEQ_START_TOKEN)
494                 seq_printf(seq, "Inter-| sta-|   Quality        |   Discarded "
495                                 "packets               | Missed | WE\n"
496                                 " face | tus | link level noise |  nwid  "
497                                 "crypt   frag  retry   misc | beacon | %d\n",
498                            WIRELESS_EXT);
499         else
500                 wireless_seq_printf_stats(seq, v);
501         return 0;
502 }
503
504 extern void *dev_seq_start(struct seq_file *seq, loff_t *pos);
505 extern void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos);
506 extern void dev_seq_stop(struct seq_file *seq, void *v);
507
508 static struct seq_operations wireless_seq_ops = {
509         .start = dev_seq_start,
510         .next  = dev_seq_next,
511         .stop  = dev_seq_stop,
512         .show  = wireless_seq_show,
513 };
514
515 static int wireless_seq_open(struct inode *inode, struct file *file)
516 {
517         return seq_open(file, &wireless_seq_ops);
518 }
519
520 static struct file_operations wireless_seq_fops = {
521         .owner   = THIS_MODULE,
522         .open    = wireless_seq_open,
523         .read    = seq_read,
524         .llseek  = seq_lseek,
525         .release = seq_release,
526 };
527
528 int __init wireless_proc_init(void)
529 {
530         if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
531                 return -ENOMEM;
532
533         return 0;
534 }
535 #endif  /* CONFIG_PROC_FS */
536
537 /************************** IOCTL SUPPORT **************************/
538 /*
539  * The original user space API to configure all those Wireless Extensions
540  * is through IOCTLs.
541  * In there, we check if we need to call the new driver API (iw_handler)
542  * or just call the driver ioctl handler.
543  */
544
545 /* ---------------------------------------------------------------- */
546 /*
547  *      Allow programatic access to /proc/net/wireless even if /proc
548  *      doesn't exist... Also more efficient...
549  */
550 static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
551 {
552         /* Get stats from the driver */
553         struct iw_statistics *stats;
554
555         stats = get_wireless_stats(dev);
556         if (stats != (struct iw_statistics *) NULL) {
557                 struct iwreq *  wrq = (struct iwreq *)ifr;
558
559                 /* Copy statistics to the user buffer */
560                 if(copy_to_user(wrq->u.data.pointer, stats,
561                                 sizeof(struct iw_statistics)))
562                         return -EFAULT;
563
564                 /* Check if we need to clear the update flag */
565                 if(wrq->u.data.flags != 0)
566                         stats->qual.updated = 0;
567                 return 0;
568         } else
569                 return -EOPNOTSUPP;
570 }
571
572 /* ---------------------------------------------------------------- */
573 /*
574  * Export the driver private handler definition
575  * They will be picked up by tools like iwpriv...
576  */
577 static inline int ioctl_export_private(struct net_device *      dev,
578                                        struct ifreq *           ifr)
579 {
580         struct iwreq *                          iwr = (struct iwreq *) ifr;
581
582         /* Check if the driver has something to export */
583         if((dev->wireless_handlers->num_private_args == 0) ||
584            (dev->wireless_handlers->private_args == NULL))
585                 return -EOPNOTSUPP;
586
587         /* Check NULL pointer */
588         if(iwr->u.data.pointer == NULL)
589                 return -EFAULT;
590
591         /* Check if there is enough buffer up there */
592         if(iwr->u.data.length < dev->wireless_handlers->num_private_args) {
593                 /* User space can't know in advance how large the buffer
594                  * needs to be. Give it a hint, so that we can support
595                  * any size buffer we want somewhat efficiently... */
596                 iwr->u.data.length = dev->wireless_handlers->num_private_args;
597                 return -E2BIG;
598         }
599
600         /* Set the number of available ioctls. */
601         iwr->u.data.length = dev->wireless_handlers->num_private_args;
602
603         /* Copy structure to the user buffer. */
604         if (copy_to_user(iwr->u.data.pointer,
605                          dev->wireless_handlers->private_args,
606                          sizeof(struct iw_priv_args) * iwr->u.data.length))
607                 return -EFAULT;
608
609         return 0;
610 }
611
612 /* ---------------------------------------------------------------- */
613 /*
614  * Wrapper to call a standard Wireless Extension handler.
615  * We do various checks and also take care of moving data between
616  * user space and kernel space.
617  */
618 static inline int ioctl_standard_call(struct net_device *       dev,
619                                       struct ifreq *            ifr,
620                                       unsigned int              cmd,
621                                       iw_handler                handler)
622 {
623         struct iwreq *                          iwr = (struct iwreq *) ifr;
624         const struct iw_ioctl_description *     descr;
625         struct iw_request_info                  info;
626         int                                     ret = -EINVAL;
627
628         /* Get the description of the IOCTL */
629         if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
630                 return -EOPNOTSUPP;
631         descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
632
633 #ifdef WE_IOCTL_DEBUG
634         printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
635                ifr->ifr_name, cmd);
636         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);
637 #endif  /* WE_IOCTL_DEBUG */
638
639         /* Prepare the call */
640         info.cmd = cmd;
641         info.flags = 0;
642
643         /* Check if we have a pointer to user space data or not */
644         if(descr->header_type != IW_HEADER_TYPE_POINT) {
645
646                 /* No extra arguments. Trivial to handle */
647                 ret = handler(dev, &info, &(iwr->u), NULL);
648
649 #ifdef WE_SET_EVENT
650                 /* Generate an event to notify listeners of the change */
651                 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
652                    ((ret == 0) || (ret == -EIWCOMMIT)))
653                         wireless_send_event(dev, cmd, &(iwr->u), NULL);
654 #endif  /* WE_SET_EVENT */
655         } else {
656                 char *  extra;
657                 int     extra_size;
658                 int     user_length = 0;
659                 int     err;
660
661                 /* Calculate space needed by arguments. Always allocate
662                  * for max space. Easier, and won't last long... */
663                 extra_size = descr->max_tokens * descr->token_size;
664
665                 /* Check what user space is giving us */
666                 if(IW_IS_SET(cmd)) {
667                         /* Check NULL pointer */
668                         if((iwr->u.data.pointer == NULL) &&
669                            (iwr->u.data.length != 0))
670                                 return -EFAULT;
671                         /* Check if number of token fits within bounds */
672                         if(iwr->u.data.length > descr->max_tokens)
673                                 return -E2BIG;
674                         if(iwr->u.data.length < descr->min_tokens)
675                                 return -EINVAL;
676                 } else {
677                         /* Check NULL pointer */
678                         if(iwr->u.data.pointer == NULL)
679                                 return -EFAULT;
680                         /* Save user space buffer size for checking */
681                         user_length = iwr->u.data.length;
682
683                         /* Don't check if user_length > max to allow forward
684                          * compatibility. The test user_length < min is
685                          * implied by the test at the end. */
686
687                         /* Support for very large requests */
688                         if((descr->flags & IW_DESCR_FLAG_NOMAX) &&
689                            (user_length > descr->max_tokens)) {
690                                 /* Allow userspace to GET more than max so
691                                  * we can support any size GET requests.
692                                  * There is still a limit : -ENOMEM. */
693                                 extra_size = user_length * descr->token_size;
694                                 /* Note : user_length is originally a __u16,
695                                  * and token_size is controlled by us,
696                                  * so extra_size won't get negative and
697                                  * won't overflow... */
698                         }
699                 }
700
701 #ifdef WE_IOCTL_DEBUG
702                 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
703                        dev->name, extra_size);
704 #endif  /* WE_IOCTL_DEBUG */
705
706                 /* Create the kernel buffer */
707                 extra = kmalloc(extra_size, GFP_KERNEL);
708                 if (extra == NULL) {
709                         return -ENOMEM;
710                 }
711
712                 /* If it is a SET, get all the extra data in here */
713                 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
714                         err = copy_from_user(extra, iwr->u.data.pointer,
715                                              iwr->u.data.length *
716                                              descr->token_size);
717                         if (err) {
718                                 kfree(extra);
719                                 return -EFAULT;
720                         }
721 #ifdef WE_IOCTL_DEBUG
722                         printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
723                                dev->name,
724                                iwr->u.data.length * descr->token_size);
725 #endif  /* WE_IOCTL_DEBUG */
726                 }
727
728                 /* Call the handler */
729                 ret = handler(dev, &info, &(iwr->u), extra);
730
731                 /* If we have something to return to the user */
732                 if (!ret && IW_IS_GET(cmd)) {
733                         /* Check if there is enough buffer up there */
734                         if(user_length < iwr->u.data.length) {
735                                 kfree(extra);
736                                 return -E2BIG;
737                         }
738
739                         err = copy_to_user(iwr->u.data.pointer, extra,
740                                            iwr->u.data.length *
741                                            descr->token_size);
742                         if (err)
743                                 ret =  -EFAULT;                            
744 #ifdef WE_IOCTL_DEBUG
745                         printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
746                                dev->name,
747                                iwr->u.data.length * descr->token_size);
748 #endif  /* WE_IOCTL_DEBUG */
749                 }
750
751 #ifdef WE_SET_EVENT
752                 /* Generate an event to notify listeners of the change */
753                 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
754                    ((ret == 0) || (ret == -EIWCOMMIT))) {
755                         if(descr->flags & IW_DESCR_FLAG_RESTRICT)
756                                 /* If the event is restricted, don't
757                                  * export the payload */
758                                 wireless_send_event(dev, cmd, &(iwr->u), NULL);
759                         else
760                                 wireless_send_event(dev, cmd, &(iwr->u),
761                                                     extra);
762                 }
763 #endif  /* WE_SET_EVENT */
764
765                 /* Cleanup - I told you it wasn't that long ;-) */
766                 kfree(extra);
767         }
768
769         /* Call commit handler if needed and defined */
770         if(ret == -EIWCOMMIT)
771                 ret = call_commit_handler(dev);
772
773         /* Here, we will generate the appropriate event if needed */
774
775         return ret;
776 }
777
778 /* ---------------------------------------------------------------- */
779 /*
780  * Wrapper to call a private Wireless Extension handler.
781  * We do various checks and also take care of moving data between
782  * user space and kernel space.
783  * It's not as nice and slimline as the standard wrapper. The cause
784  * is struct iw_priv_args, which was not really designed for the
785  * job we are going here.
786  *
787  * IMPORTANT : This function prevent to set and get data on the same
788  * IOCTL and enforce the SET/GET convention. Not doing it would be
789  * far too hairy...
790  * If you need to set and get data at the same time, please don't use
791  * a iw_handler but process it in your ioctl handler (i.e. use the
792  * old driver API).
793  */
794 static inline int ioctl_private_call(struct net_device *        dev,
795                                      struct ifreq *             ifr,
796                                      unsigned int               cmd,
797                                      iw_handler         handler)
798 {
799         struct iwreq *                  iwr = (struct iwreq *) ifr;
800         const struct iw_priv_args *     descr = NULL;
801         struct iw_request_info          info;
802         int                             extra_size = 0;
803         int                             i;
804         int                             ret = -EINVAL;
805
806         /* Get the description of the IOCTL */
807         for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
808                 if(cmd == dev->wireless_handlers->private_args[i].cmd) {
809                         descr = &(dev->wireless_handlers->private_args[i]);
810                         break;
811                 }
812
813 #ifdef WE_IOCTL_DEBUG
814         printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
815                ifr->ifr_name, cmd);
816         if(descr) {
817                 printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
818                        dev->name, descr->name,
819                        descr->set_args, descr->get_args);
820         }
821 #endif  /* WE_IOCTL_DEBUG */
822
823         /* Compute the size of the set/get arguments */
824         if(descr != NULL) {
825                 if(IW_IS_SET(cmd)) {
826                         int     offset = 0;     /* For sub-ioctls */
827                         /* Check for sub-ioctl handler */
828                         if(descr->name[0] == '\0')
829                                 /* Reserve one int for sub-ioctl index */
830                                 offset = sizeof(__u32);
831
832                         /* Size of set arguments */
833                         extra_size = get_priv_size(descr->set_args);
834
835                         /* Does it fits in iwr ? */
836                         if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
837                            ((extra_size + offset) <= IFNAMSIZ))
838                                 extra_size = 0;
839                 } else {
840                         /* Size of get arguments */
841                         extra_size = get_priv_size(descr->get_args);
842
843                         /* Does it fits in iwr ? */
844                         if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
845                            (extra_size <= IFNAMSIZ))
846                                 extra_size = 0;
847                 }
848         }
849
850         /* Prepare the call */
851         info.cmd = cmd;
852         info.flags = 0;
853
854         /* Check if we have a pointer to user space data or not. */
855         if(extra_size == 0) {
856                 /* No extra arguments. Trivial to handle */
857                 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
858         } else {
859                 char *  extra;
860                 int     err;
861
862                 /* Check what user space is giving us */
863                 if(IW_IS_SET(cmd)) {
864                         /* Check NULL pointer */
865                         if((iwr->u.data.pointer == NULL) &&
866                            (iwr->u.data.length != 0))
867                                 return -EFAULT;
868
869                         /* Does it fits within bounds ? */
870                         if(iwr->u.data.length > (descr->set_args &
871                                                  IW_PRIV_SIZE_MASK))
872                                 return -E2BIG;
873                 } else {
874                         /* Check NULL pointer */
875                         if(iwr->u.data.pointer == NULL)
876                                 return -EFAULT;
877                 }
878
879 #ifdef WE_IOCTL_DEBUG
880                 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
881                        dev->name, extra_size);
882 #endif  /* WE_IOCTL_DEBUG */
883
884                 /* Always allocate for max space. Easier, and won't last
885                  * long... */
886                 extra = kmalloc(extra_size, GFP_KERNEL);
887                 if (extra == NULL) {
888                         return -ENOMEM;
889                 }
890
891                 /* If it is a SET, get all the extra data in here */
892                 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
893                         err = copy_from_user(extra, iwr->u.data.pointer,
894                                              extra_size);
895                         if (err) {
896                                 kfree(extra);
897                                 return -EFAULT;
898                         }
899 #ifdef WE_IOCTL_DEBUG
900                         printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
901                                dev->name, iwr->u.data.length);
902 #endif  /* WE_IOCTL_DEBUG */
903                 }
904
905                 /* Call the handler */
906                 ret = handler(dev, &info, &(iwr->u), extra);
907
908                 /* If we have something to return to the user */
909                 if (!ret && IW_IS_GET(cmd)) {
910
911                         /* Adjust for the actual length if it's variable,
912                          * avoid leaking kernel bits outside. */
913                         if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
914                                 extra_size = adjust_priv_size(descr->get_args,
915                                                               &(iwr->u));
916                         }
917
918                         err = copy_to_user(iwr->u.data.pointer, extra,
919                                            extra_size);
920                         if (err)
921                                 ret =  -EFAULT;                            
922 #ifdef WE_IOCTL_DEBUG
923                         printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
924                                dev->name, iwr->u.data.length);
925 #endif  /* WE_IOCTL_DEBUG */
926                 }
927
928                 /* Cleanup - I told you it wasn't that long ;-) */
929                 kfree(extra);
930         }
931
932
933         /* Call commit handler if needed and defined */
934         if(ret == -EIWCOMMIT)
935                 ret = call_commit_handler(dev);
936
937         return ret;
938 }
939
940 /* ---------------------------------------------------------------- */
941 /*
942  * Main IOCTl dispatcher. Called from the main networking code
943  * (dev_ioctl() in net/core/dev.c).
944  * Check the type of IOCTL and call the appropriate wrapper...
945  */
946 int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
947 {
948         struct net_device *dev;
949         iw_handler      handler;
950
951         /* Permissions are already checked in dev_ioctl() before calling us.
952          * The copy_to/from_user() of ifr is also dealt with in there */
953
954         /* Make sure the device exist */
955         if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
956                 return -ENODEV;
957
958         /* A bunch of special cases, then the generic case...
959          * Note that 'cmd' is already filtered in dev_ioctl() with
960          * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
961         switch(cmd) 
962         {
963                 case SIOCGIWSTATS:
964                         /* Get Wireless Stats */
965                         return dev_iwstats(dev, ifr);
966
967                 case SIOCGIWPRIV:
968                         /* Check if we have some wireless handlers defined */
969                         if(dev->wireless_handlers != NULL) {
970                                 /* We export to user space the definition of
971                                  * the private handler ourselves */
972                                 return ioctl_export_private(dev, ifr);
973                         }
974                         // ## Fall-through for old API ##
975                 default:
976                         /* Generic IOCTL */
977                         /* Basic check */
978                         if (!netif_device_present(dev))
979                                 return -ENODEV;
980                         /* New driver API : try to find the handler */
981                         handler = get_handler(dev, cmd);
982                         if(handler != NULL) {
983                                 /* Standard and private are not the same */
984                                 if(cmd < SIOCIWFIRSTPRIV)
985                                         return ioctl_standard_call(dev,
986                                                                    ifr,
987                                                                    cmd,
988                                                                    handler);
989                                 else
990                                         return ioctl_private_call(dev,
991                                                                   ifr,
992                                                                   cmd,
993                                                                   handler);
994                         }
995                         /* Old driver API : call driver ioctl handler */
996                         if (dev->do_ioctl) {
997                                 return dev->do_ioctl(dev, ifr, cmd);
998                         }
999                         return -EOPNOTSUPP;
1000         }
1001         /* Not reached */
1002         return -EINVAL;
1003 }
1004
1005 /************************* EVENT PROCESSING *************************/
1006 /*
1007  * Process events generated by the wireless layer or the driver.
1008  * Most often, the event will be propagated through rtnetlink
1009  */
1010
1011 #ifdef WE_EVENT_NETLINK
1012 /* "rtnl" is defined in net/core/rtnetlink.c, but we need it here.
1013  * It is declared in <linux/rtnetlink.h> */
1014
1015 /* ---------------------------------------------------------------- */
1016 /*
1017  * Fill a rtnetlink message with our event data.
1018  * Note that we propage only the specified event and don't dump the
1019  * current wireless config. Dumping the wireless config is far too
1020  * expensive (for each parameter, the driver need to query the hardware).
1021  */
1022 static inline int rtnetlink_fill_iwinfo(struct sk_buff *        skb,
1023                                         struct net_device *     dev,
1024                                         int                     type,
1025                                         char *                  event,
1026                                         int                     event_len)
1027 {
1028         struct ifinfomsg *r;
1029         struct nlmsghdr  *nlh;
1030         unsigned char    *b = skb->tail;
1031
1032         nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
1033         r = NLMSG_DATA(nlh);
1034         r->ifi_family = AF_UNSPEC;
1035         r->ifi_type = dev->type;
1036         r->ifi_index = dev->ifindex;
1037         r->ifi_flags = dev->flags;
1038         r->ifi_change = 0;      /* Wireless changes don't affect those flags */
1039
1040         /* Add the wireless events in the netlink packet */
1041         RTA_PUT(skb, IFLA_WIRELESS,
1042                 event_len, event);
1043
1044         nlh->nlmsg_len = skb->tail - b;
1045         return skb->len;
1046
1047 nlmsg_failure:
1048 rtattr_failure:
1049         skb_trim(skb, b - skb->data);
1050         return -1;
1051 }
1052
1053 /* ---------------------------------------------------------------- */
1054 /*
1055  * Create and broadcast and send it on the standard rtnetlink socket
1056  * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1057  * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1058  * within a RTM_NEWLINK event.
1059  */
1060 static inline void rtmsg_iwinfo(struct net_device *     dev,
1061                                 char *                  event,
1062                                 int                     event_len)
1063 {
1064         struct sk_buff *skb;
1065         int size = NLMSG_GOODSIZE;
1066
1067         skb = alloc_skb(size, GFP_ATOMIC);
1068         if (!skb)
1069                 return;
1070
1071         if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
1072                                   event, event_len) < 0) {
1073                 kfree_skb(skb);
1074                 return;
1075         }
1076         NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
1077         netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_ATOMIC);
1078 }
1079 #endif  /* WE_EVENT_NETLINK */
1080
1081 /* ---------------------------------------------------------------- */
1082 /*
1083  * Main event dispatcher. Called from other parts and drivers.
1084  * Send the event on the appropriate channels.
1085  * May be called from interrupt context.
1086  */
1087 void wireless_send_event(struct net_device *    dev,
1088                          unsigned int           cmd,
1089                          union iwreq_data *     wrqu,
1090                          char *                 extra)
1091 {
1092         const struct iw_ioctl_description *     descr = NULL;
1093         int extra_len = 0;
1094         struct iw_event  *event;                /* Mallocated whole event */
1095         int event_len;                          /* Its size */
1096         int hdr_len;                            /* Size of the event header */
1097         /* Don't "optimise" the following variable, it will crash */
1098         unsigned        cmd_index;              /* *MUST* be unsigned */
1099
1100         /* Get the description of the IOCTL */
1101         if(cmd <= SIOCIWLAST) {
1102                 cmd_index = cmd - SIOCIWFIRST;
1103                 if(cmd_index < standard_ioctl_num)
1104                         descr = &(standard_ioctl[cmd_index]);
1105         } else {
1106                 cmd_index = cmd - IWEVFIRST;
1107                 if(cmd_index < standard_event_num)
1108                         descr = &(standard_event[cmd_index]);
1109         }
1110         /* Don't accept unknown events */
1111         if(descr == NULL) {
1112                 /* Note : we don't return an error to the driver, because
1113                  * the driver would not know what to do about it. It can't
1114                  * return an error to the user, because the event is not
1115                  * initiated by a user request.
1116                  * The best the driver could do is to log an error message.
1117                  * We will do it ourselves instead...
1118                  */
1119                 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
1120                        dev->name, cmd);
1121                 return;
1122         }
1123 #ifdef WE_EVENT_DEBUG
1124         printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
1125                dev->name, cmd);
1126         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);
1127 #endif  /* WE_EVENT_DEBUG */
1128
1129         /* Check extra parameters and set extra_len */
1130         if(descr->header_type == IW_HEADER_TYPE_POINT) {
1131                 /* Check if number of token fits within bounds */
1132                 if(wrqu->data.length > descr->max_tokens) {
1133                         printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
1134                         return;
1135                 }
1136                 if(wrqu->data.length < descr->min_tokens) {
1137                         printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
1138                         return;
1139                 }
1140                 /* Calculate extra_len - extra is NULL for restricted events */
1141                 if(extra != NULL)
1142                         extra_len = wrqu->data.length * descr->token_size;
1143 #ifdef WE_EVENT_DEBUG
1144                 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
1145 #endif  /* WE_EVENT_DEBUG */
1146         }
1147
1148         /* Total length of the event */
1149         hdr_len = event_type_size[descr->header_type];
1150         event_len = hdr_len + extra_len;
1151
1152 #ifdef WE_EVENT_DEBUG
1153         printk(KERN_DEBUG "%s (WE) : Event 0x%04X, hdr_len %d, event_len %d\n", dev->name, cmd, hdr_len, event_len);
1154 #endif  /* WE_EVENT_DEBUG */
1155
1156         /* Create temporary buffer to hold the event */
1157         event = kmalloc(event_len, GFP_ATOMIC);
1158         if(event == NULL)
1159                 return;
1160
1161         /* Fill event */
1162         event->len = event_len;
1163         event->cmd = cmd;
1164         memcpy(&event->u, wrqu, hdr_len - IW_EV_LCP_LEN);
1165         if(extra != NULL)
1166                 memcpy(((char *) event) + hdr_len, extra, extra_len);
1167
1168 #ifdef WE_EVENT_NETLINK
1169         /* rtnetlink event channel */
1170         rtmsg_iwinfo(dev, (char *) event, event_len);
1171 #endif  /* WE_EVENT_NETLINK */
1172
1173         /* Cleanup */
1174         kfree(event);
1175
1176         return;         /* Always success, I guess ;-) */
1177 }
1178
1179 /********************** ENHANCED IWSPY SUPPORT **********************/
1180 /*
1181  * In the old days, the driver was handling spy support all by itself.
1182  * Now, the driver can delegate this task to Wireless Extensions.
1183  * It needs to use those standard spy iw_handler in struct iw_handler_def,
1184  * push data to us via wireless_spy_update() and include struct iw_spy_data
1185  * in its private part (and advertise it in iw_handler_def->spy_offset).
1186  * One of the main advantage of centralising spy support here is that
1187  * it becomes much easier to improve and extend it without having to touch
1188  * the drivers. One example is the addition of the Spy-Threshold events.
1189  */
1190
1191 /* ---------------------------------------------------------------- */
1192 /*
1193  * Return the pointer to the spy data in the driver.
1194  * Because this is called on the Rx path via wireless_spy_update(),
1195  * we want it to be efficient...
1196  */
1197 static inline struct iw_spy_data * get_spydata(struct net_device *dev)
1198 {
1199         /* This is the new way */
1200         if(dev->wireless_data)
1201                 return(dev->wireless_data->spy_data);
1202
1203         /* This is the old way. Doesn't work for multi-headed drivers.
1204          * It will be removed in the next version of WE. */
1205         return (dev->priv + dev->wireless_handlers->spy_offset);
1206 }
1207
1208 /*------------------------------------------------------------------*/
1209 /*
1210  * Standard Wireless Handler : set Spy List
1211  */
1212 int iw_handler_set_spy(struct net_device *      dev,
1213                        struct iw_request_info * info,
1214                        union iwreq_data *       wrqu,
1215                        char *                   extra)
1216 {
1217         struct iw_spy_data *    spydata = get_spydata(dev);
1218         struct sockaddr *       address = (struct sockaddr *) extra;
1219
1220         if(!dev->wireless_data)
1221                 /* Help user know that driver needs updating */
1222                 printk(KERN_DEBUG "%s (WE) : Driver using old/buggy spy support, please fix driver !\n",
1223                        dev->name);
1224         /* Make sure driver is not buggy or using the old API */
1225         if(!spydata)
1226                 return -EOPNOTSUPP;
1227
1228         /* Disable spy collection while we copy the addresses.
1229          * While we copy addresses, any call to wireless_spy_update()
1230          * will NOP. This is OK, as anyway the addresses are changing. */
1231         spydata->spy_number = 0;
1232
1233         /* We want to operate without locking, because wireless_spy_update()
1234          * most likely will happen in the interrupt handler, and therefore
1235          * have its own locking constraints and needs performance.
1236          * The rtnl_lock() make sure we don't race with the other iw_handlers.
1237          * This make sure wireless_spy_update() "see" that the spy list
1238          * is temporarily disabled. */
1239         wmb();
1240
1241         /* Are there are addresses to copy? */
1242         if(wrqu->data.length > 0) {
1243                 int i;
1244
1245                 /* Copy addresses */
1246                 for(i = 0; i < wrqu->data.length; i++)
1247                         memcpy(spydata->spy_address[i], address[i].sa_data,
1248                                ETH_ALEN);
1249                 /* Reset stats */
1250                 memset(spydata->spy_stat, 0,
1251                        sizeof(struct iw_quality) * IW_MAX_SPY);
1252
1253 #ifdef WE_SPY_DEBUG
1254                 printk(KERN_DEBUG "iw_handler_set_spy() :  offset %ld, spydata %p, num %d\n", dev->wireless_handlers->spy_offset, spydata, wrqu->data.length);
1255                 for (i = 0; i < wrqu->data.length; i++)
1256                         printk(KERN_DEBUG
1257                                "%02X:%02X:%02X:%02X:%02X:%02X \n",
1258                                spydata->spy_address[i][0],
1259                                spydata->spy_address[i][1],
1260                                spydata->spy_address[i][2],
1261                                spydata->spy_address[i][3],
1262                                spydata->spy_address[i][4],
1263                                spydata->spy_address[i][5]);
1264 #endif  /* WE_SPY_DEBUG */
1265         }
1266
1267         /* Make sure above is updated before re-enabling */
1268         wmb();
1269
1270         /* Enable addresses */
1271         spydata->spy_number = wrqu->data.length;
1272
1273         return 0;
1274 }
1275
1276 /*------------------------------------------------------------------*/
1277 /*
1278  * Standard Wireless Handler : get Spy List
1279  */
1280 int iw_handler_get_spy(struct net_device *      dev,
1281                        struct iw_request_info * info,
1282                        union iwreq_data *       wrqu,
1283                        char *                   extra)
1284 {
1285         struct iw_spy_data *    spydata = get_spydata(dev);
1286         struct sockaddr *       address = (struct sockaddr *) extra;
1287         int                     i;
1288
1289         /* Make sure driver is not buggy or using the old API */
1290         if(!spydata)
1291                 return -EOPNOTSUPP;
1292
1293         wrqu->data.length = spydata->spy_number;
1294
1295         /* Copy addresses. */
1296         for(i = 0; i < spydata->spy_number; i++)        {
1297                 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1298                 address[i].sa_family = AF_UNIX;
1299         }
1300         /* Copy stats to the user buffer (just after). */
1301         if(spydata->spy_number > 0)
1302                 memcpy(extra  + (sizeof(struct sockaddr) *spydata->spy_number),
1303                        spydata->spy_stat,
1304                        sizeof(struct iw_quality) * spydata->spy_number);
1305         /* Reset updated flags. */
1306         for(i = 0; i < spydata->spy_number; i++)
1307                 spydata->spy_stat[i].updated = 0;
1308         return 0;
1309 }
1310
1311 /*------------------------------------------------------------------*/
1312 /*
1313  * Standard Wireless Handler : set spy threshold
1314  */
1315 int iw_handler_set_thrspy(struct net_device *   dev,
1316                           struct iw_request_info *info,
1317                           union iwreq_data *    wrqu,
1318                           char *                extra)
1319 {
1320         struct iw_spy_data *    spydata = get_spydata(dev);
1321         struct iw_thrspy *      threshold = (struct iw_thrspy *) extra;
1322
1323         /* Make sure driver is not buggy or using the old API */
1324         if(!spydata)
1325                 return -EOPNOTSUPP;
1326
1327         /* Just do it */
1328         memcpy(&(spydata->spy_thr_low), &(threshold->low),
1329                2 * sizeof(struct iw_quality));
1330
1331         /* Clear flag */
1332         memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1333
1334 #ifdef WE_SPY_DEBUG
1335         printk(KERN_DEBUG "iw_handler_set_thrspy() :  low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
1336 #endif  /* WE_SPY_DEBUG */
1337
1338         return 0;
1339 }
1340
1341 /*------------------------------------------------------------------*/
1342 /*
1343  * Standard Wireless Handler : get spy threshold
1344  */
1345 int iw_handler_get_thrspy(struct net_device *   dev,
1346                           struct iw_request_info *info,
1347                           union iwreq_data *    wrqu,
1348                           char *                extra)
1349 {
1350         struct iw_spy_data *    spydata = get_spydata(dev);
1351         struct iw_thrspy *      threshold = (struct iw_thrspy *) extra;
1352
1353         /* Make sure driver is not buggy or using the old API */
1354         if(!spydata)
1355                 return -EOPNOTSUPP;
1356
1357         /* Just do it */
1358         memcpy(&(threshold->low), &(spydata->spy_thr_low),
1359                2 * sizeof(struct iw_quality));
1360
1361         return 0;
1362 }
1363
1364 /*------------------------------------------------------------------*/
1365 /*
1366  * Prepare and send a Spy Threshold event
1367  */
1368 static void iw_send_thrspy_event(struct net_device *    dev,
1369                                  struct iw_spy_data *   spydata,
1370                                  unsigned char *        address,
1371                                  struct iw_quality *    wstats)
1372 {
1373         union iwreq_data        wrqu;
1374         struct iw_thrspy        threshold;
1375
1376         /* Init */
1377         wrqu.data.length = 1;
1378         wrqu.data.flags = 0;
1379         /* Copy address */
1380         memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1381         threshold.addr.sa_family = ARPHRD_ETHER;
1382         /* Copy stats */
1383         memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1384         /* Copy also thresholds */
1385         memcpy(&(threshold.low), &(spydata->spy_thr_low),
1386                2 * sizeof(struct iw_quality));
1387
1388 #ifdef WE_SPY_DEBUG
1389         printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
1390                threshold.addr.sa_data[0],
1391                threshold.addr.sa_data[1],
1392                threshold.addr.sa_data[2],
1393                threshold.addr.sa_data[3],
1394                threshold.addr.sa_data[4],
1395                threshold.addr.sa_data[5], threshold.qual.level);
1396 #endif  /* WE_SPY_DEBUG */
1397
1398         /* Send event to user space */
1399         wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1400 }
1401
1402 /* ---------------------------------------------------------------- */
1403 /*
1404  * Call for the driver to update the spy data.
1405  * For now, the spy data is a simple array. As the size of the array is
1406  * small, this is good enough. If we wanted to support larger number of
1407  * spy addresses, we should use something more efficient...
1408  */
1409 void wireless_spy_update(struct net_device *    dev,
1410                          unsigned char *        address,
1411                          struct iw_quality *    wstats)
1412 {
1413         struct iw_spy_data *    spydata = get_spydata(dev);
1414         int                     i;
1415         int                     match = -1;
1416
1417         /* Make sure driver is not buggy or using the old API */
1418         if(!spydata)
1419                 return;
1420
1421 #ifdef WE_SPY_DEBUG
1422         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]);
1423 #endif  /* WE_SPY_DEBUG */
1424
1425         /* Update all records that match */
1426         for(i = 0; i < spydata->spy_number; i++)
1427                 if(!memcmp(address, spydata->spy_address[i], ETH_ALEN)) {
1428                         memcpy(&(spydata->spy_stat[i]), wstats,
1429                                sizeof(struct iw_quality));
1430                         match = i;
1431                 }
1432
1433         /* Generate an event if we cross the spy threshold.
1434          * To avoid event storms, we have a simple hysteresis : we generate
1435          * event only when we go under the low threshold or above the
1436          * high threshold. */
1437         if(match >= 0) {
1438                 if(spydata->spy_thr_under[match]) {
1439                         if(wstats->level > spydata->spy_thr_high.level) {
1440                                 spydata->spy_thr_under[match] = 0;
1441                                 iw_send_thrspy_event(dev, spydata,
1442                                                      address, wstats);
1443                         }
1444                 } else {
1445                         if(wstats->level < spydata->spy_thr_low.level) {
1446                                 spydata->spy_thr_under[match] = 1;
1447                                 iw_send_thrspy_event(dev, spydata,
1448                                                      address, wstats);
1449                         }
1450                 }
1451         }
1452 }
1453
1454 EXPORT_SYMBOL(iw_handler_get_spy);
1455 EXPORT_SYMBOL(iw_handler_get_thrspy);
1456 EXPORT_SYMBOL(iw_handler_set_spy);
1457 EXPORT_SYMBOL(iw_handler_set_thrspy);
1458 EXPORT_SYMBOL(wireless_send_event);
1459 EXPORT_SYMBOL(wireless_spy_update);