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