Merge master.kernel.org:/home/rmk/linux-2.6-serial
[sfrench/cifs-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-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                 static int printed_message;
459
460                 if (!printed_message++)
461                         printk(KERN_DEBUG "%s (WE) : Driver using old /proc/net/wireless support, please fix driver !\n",
462                                 dev->name);
463
464                 return dev->get_wireless_stats(dev);
465         }
466
467         /* Not found */
468         return (struct iw_statistics *) NULL;
469 }
470
471 /* ---------------------------------------------------------------- */
472 /*
473  * Call the commit handler in the driver
474  * (if exist and if conditions are right)
475  *
476  * Note : our current commit strategy is currently pretty dumb,
477  * but we will be able to improve on that...
478  * The goal is to try to agreagate as many changes as possible
479  * before doing the commit. Drivers that will define a commit handler
480  * are usually those that need a reset after changing parameters, so
481  * we want to minimise the number of reset.
482  * A cool idea is to use a timer : at each "set" command, we re-set the
483  * timer, when the timer eventually fires, we call the driver.
484  * Hopefully, more on that later.
485  *
486  * Also, I'm waiting to see how many people will complain about the
487  * netif_running(dev) test. I'm open on that one...
488  * Hopefully, the driver will remember to do a commit in "open()" ;-)
489  */
490 static inline int call_commit_handler(struct net_device *       dev)
491 {
492         if((netif_running(dev)) &&
493            (dev->wireless_handlers->standard[0] != NULL)) {
494                 /* Call the commit handler on the driver */
495                 return dev->wireless_handlers->standard[0](dev, NULL,
496                                                            NULL, NULL);
497         } else
498                 return 0;               /* Command completed successfully */
499 }
500
501 /* ---------------------------------------------------------------- */
502 /*
503  * Calculate size of private arguments
504  */
505 static inline int get_priv_size(__u16   args)
506 {
507         int     num = args & IW_PRIV_SIZE_MASK;
508         int     type = (args & IW_PRIV_TYPE_MASK) >> 12;
509
510         return num * iw_priv_type_size[type];
511 }
512
513 /* ---------------------------------------------------------------- */
514 /*
515  * Re-calculate the size of private arguments
516  */
517 static inline int adjust_priv_size(__u16                args,
518                                    union iwreq_data *   wrqu)
519 {
520         int     num = wrqu->data.length;
521         int     max = args & IW_PRIV_SIZE_MASK;
522         int     type = (args & IW_PRIV_TYPE_MASK) >> 12;
523
524         /* Make sure the driver doesn't goof up */
525         if (max < num)
526                 num = max;
527
528         return num * iw_priv_type_size[type];
529 }
530
531
532 /******************** /proc/net/wireless SUPPORT ********************/
533 /*
534  * The /proc/net/wireless file is a human readable user-space interface
535  * exporting various wireless specific statistics from the wireless devices.
536  * This is the most popular part of the Wireless Extensions ;-)
537  *
538  * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
539  * The content of the file is basically the content of "struct iw_statistics".
540  */
541
542 #ifdef CONFIG_PROC_FS
543
544 /* ---------------------------------------------------------------- */
545 /*
546  * Print one entry (line) of /proc/net/wireless
547  */
548 static __inline__ void wireless_seq_printf_stats(struct seq_file *seq,
549                                                  struct net_device *dev)
550 {
551         /* Get stats from the driver */
552         struct iw_statistics *stats = get_wireless_stats(dev);
553
554         if (stats) {
555                 seq_printf(seq, "%6s: %04x  %3d%c  %3d%c  %3d%c  %6d %6d %6d "
556                                 "%6d %6d   %6d\n",
557                            dev->name, stats->status, stats->qual.qual,
558                            stats->qual.updated & IW_QUAL_QUAL_UPDATED
559                            ? '.' : ' ',
560                            ((__s32) stats->qual.level) - 
561                            ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
562                            stats->qual.updated & IW_QUAL_LEVEL_UPDATED
563                            ? '.' : ' ',
564                            ((__s32) stats->qual.noise) - 
565                            ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
566                            stats->qual.updated & IW_QUAL_NOISE_UPDATED
567                            ? '.' : ' ',
568                            stats->discard.nwid, stats->discard.code,
569                            stats->discard.fragment, stats->discard.retries,
570                            stats->discard.misc, stats->miss.beacon);
571                 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
572         }
573 }
574
575 /* ---------------------------------------------------------------- */
576 /*
577  * Print info for /proc/net/wireless (print all entries)
578  */
579 static int wireless_seq_show(struct seq_file *seq, void *v)
580 {
581         if (v == SEQ_START_TOKEN)
582                 seq_printf(seq, "Inter-| sta-|   Quality        |   Discarded "
583                                 "packets               | Missed | WE\n"
584                                 " face | tus | link level noise |  nwid  "
585                                 "crypt   frag  retry   misc | beacon | %d\n",
586                            WIRELESS_EXT);
587         else
588                 wireless_seq_printf_stats(seq, v);
589         return 0;
590 }
591
592 static struct seq_operations wireless_seq_ops = {
593         .start = dev_seq_start,
594         .next  = dev_seq_next,
595         .stop  = dev_seq_stop,
596         .show  = wireless_seq_show,
597 };
598
599 static int wireless_seq_open(struct inode *inode, struct file *file)
600 {
601         return seq_open(file, &wireless_seq_ops);
602 }
603
604 static struct file_operations wireless_seq_fops = {
605         .owner   = THIS_MODULE,
606         .open    = wireless_seq_open,
607         .read    = seq_read,
608         .llseek  = seq_lseek,
609         .release = seq_release,
610 };
611
612 int __init wireless_proc_init(void)
613 {
614         /* Create /proc/net/wireless entry */
615         if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
616                 return -ENOMEM;
617
618         return 0;
619 }
620 #endif  /* CONFIG_PROC_FS */
621
622 /************************** IOCTL SUPPORT **************************/
623 /*
624  * The original user space API to configure all those Wireless Extensions
625  * is through IOCTLs.
626  * In there, we check if we need to call the new driver API (iw_handler)
627  * or just call the driver ioctl handler.
628  */
629
630 /* ---------------------------------------------------------------- */
631 /*
632  *      Allow programatic access to /proc/net/wireless even if /proc
633  *      doesn't exist... Also more efficient...
634  */
635 static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
636 {
637         /* Get stats from the driver */
638         struct iw_statistics *stats;
639
640         stats = get_wireless_stats(dev);
641         if (stats != (struct iw_statistics *) NULL) {
642                 struct iwreq *  wrq = (struct iwreq *)ifr;
643
644                 /* Copy statistics to the user buffer */
645                 if(copy_to_user(wrq->u.data.pointer, stats,
646                                 sizeof(struct iw_statistics)))
647                         return -EFAULT;
648
649                 /* Check if we need to clear the updated flag */
650                 if(wrq->u.data.flags != 0)
651                         stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
652                 return 0;
653         } else
654                 return -EOPNOTSUPP;
655 }
656
657 /* ---------------------------------------------------------------- */
658 /*
659  * Export the driver private handler definition
660  * They will be picked up by tools like iwpriv...
661  */
662 static inline int ioctl_export_private(struct net_device *      dev,
663                                        struct ifreq *           ifr)
664 {
665         struct iwreq *                          iwr = (struct iwreq *) ifr;
666
667         /* Check if the driver has something to export */
668         if((dev->wireless_handlers->num_private_args == 0) ||
669            (dev->wireless_handlers->private_args == NULL))
670                 return -EOPNOTSUPP;
671
672         /* Check NULL pointer */
673         if(iwr->u.data.pointer == NULL)
674                 return -EFAULT;
675
676         /* Check if there is enough buffer up there */
677         if(iwr->u.data.length < dev->wireless_handlers->num_private_args) {
678                 /* User space can't know in advance how large the buffer
679                  * needs to be. Give it a hint, so that we can support
680                  * any size buffer we want somewhat efficiently... */
681                 iwr->u.data.length = dev->wireless_handlers->num_private_args;
682                 return -E2BIG;
683         }
684
685         /* Set the number of available ioctls. */
686         iwr->u.data.length = dev->wireless_handlers->num_private_args;
687
688         /* Copy structure to the user buffer. */
689         if (copy_to_user(iwr->u.data.pointer,
690                          dev->wireless_handlers->private_args,
691                          sizeof(struct iw_priv_args) * iwr->u.data.length))
692                 return -EFAULT;
693
694         return 0;
695 }
696
697 /* ---------------------------------------------------------------- */
698 /*
699  * Wrapper to call a standard Wireless Extension handler.
700  * We do various checks and also take care of moving data between
701  * user space and kernel space.
702  */
703 static inline int ioctl_standard_call(struct net_device *       dev,
704                                       struct ifreq *            ifr,
705                                       unsigned int              cmd,
706                                       iw_handler                handler)
707 {
708         struct iwreq *                          iwr = (struct iwreq *) ifr;
709         const struct iw_ioctl_description *     descr;
710         struct iw_request_info                  info;
711         int                                     ret = -EINVAL;
712
713         /* Get the description of the IOCTL */
714         if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
715                 return -EOPNOTSUPP;
716         descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
717
718 #ifdef WE_IOCTL_DEBUG
719         printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
720                ifr->ifr_name, cmd);
721         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);
722 #endif  /* WE_IOCTL_DEBUG */
723
724         /* Prepare the call */
725         info.cmd = cmd;
726         info.flags = 0;
727
728         /* Check if we have a pointer to user space data or not */
729         if(descr->header_type != IW_HEADER_TYPE_POINT) {
730
731                 /* No extra arguments. Trivial to handle */
732                 ret = handler(dev, &info, &(iwr->u), NULL);
733
734 #ifdef WE_SET_EVENT
735                 /* Generate an event to notify listeners of the change */
736                 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
737                    ((ret == 0) || (ret == -EIWCOMMIT)))
738                         wireless_send_event(dev, cmd, &(iwr->u), NULL);
739 #endif  /* WE_SET_EVENT */
740         } else {
741                 char *  extra;
742                 int     extra_size;
743                 int     user_length = 0;
744                 int     err;
745
746                 /* Calculate space needed by arguments. Always allocate
747                  * for max space. Easier, and won't last long... */
748                 extra_size = descr->max_tokens * descr->token_size;
749
750                 /* Check what user space is giving us */
751                 if(IW_IS_SET(cmd)) {
752                         /* Check NULL pointer */
753                         if((iwr->u.data.pointer == NULL) &&
754                            (iwr->u.data.length != 0))
755                                 return -EFAULT;
756                         /* Check if number of token fits within bounds */
757                         if(iwr->u.data.length > descr->max_tokens)
758                                 return -E2BIG;
759                         if(iwr->u.data.length < descr->min_tokens)
760                                 return -EINVAL;
761                 } else {
762                         /* Check NULL pointer */
763                         if(iwr->u.data.pointer == NULL)
764                                 return -EFAULT;
765                         /* Save user space buffer size for checking */
766                         user_length = iwr->u.data.length;
767
768                         /* Don't check if user_length > max to allow forward
769                          * compatibility. The test user_length < min is
770                          * implied by the test at the end. */
771
772                         /* Support for very large requests */
773                         if((descr->flags & IW_DESCR_FLAG_NOMAX) &&
774                            (user_length > descr->max_tokens)) {
775                                 /* Allow userspace to GET more than max so
776                                  * we can support any size GET requests.
777                                  * There is still a limit : -ENOMEM. */
778                                 extra_size = user_length * descr->token_size;
779                                 /* Note : user_length is originally a __u16,
780                                  * and token_size is controlled by us,
781                                  * so extra_size won't get negative and
782                                  * won't overflow... */
783                         }
784                 }
785
786 #ifdef WE_IOCTL_DEBUG
787                 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
788                        dev->name, extra_size);
789 #endif  /* WE_IOCTL_DEBUG */
790
791                 /* Create the kernel buffer */
792                 extra = kmalloc(extra_size, GFP_KERNEL);
793                 if (extra == NULL) {
794                         return -ENOMEM;
795                 }
796
797                 /* If it is a SET, get all the extra data in here */
798                 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
799                         err = copy_from_user(extra, iwr->u.data.pointer,
800                                              iwr->u.data.length *
801                                              descr->token_size);
802                         if (err) {
803                                 kfree(extra);
804                                 return -EFAULT;
805                         }
806 #ifdef WE_IOCTL_DEBUG
807                         printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
808                                dev->name,
809                                iwr->u.data.length * descr->token_size);
810 #endif  /* WE_IOCTL_DEBUG */
811                 }
812
813                 /* Call the handler */
814                 ret = handler(dev, &info, &(iwr->u), extra);
815
816                 /* If we have something to return to the user */
817                 if (!ret && IW_IS_GET(cmd)) {
818                         /* Check if there is enough buffer up there */
819                         if(user_length < iwr->u.data.length) {
820                                 kfree(extra);
821                                 return -E2BIG;
822                         }
823
824                         err = copy_to_user(iwr->u.data.pointer, extra,
825                                            iwr->u.data.length *
826                                            descr->token_size);
827                         if (err)
828                                 ret =  -EFAULT;                            
829 #ifdef WE_IOCTL_DEBUG
830                         printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
831                                dev->name,
832                                iwr->u.data.length * descr->token_size);
833 #endif  /* WE_IOCTL_DEBUG */
834                 }
835
836 #ifdef WE_SET_EVENT
837                 /* Generate an event to notify listeners of the change */
838                 if((descr->flags & IW_DESCR_FLAG_EVENT) &&
839                    ((ret == 0) || (ret == -EIWCOMMIT))) {
840                         if(descr->flags & IW_DESCR_FLAG_RESTRICT)
841                                 /* If the event is restricted, don't
842                                  * export the payload */
843                                 wireless_send_event(dev, cmd, &(iwr->u), NULL);
844                         else
845                                 wireless_send_event(dev, cmd, &(iwr->u),
846                                                     extra);
847                 }
848 #endif  /* WE_SET_EVENT */
849
850                 /* Cleanup - I told you it wasn't that long ;-) */
851                 kfree(extra);
852         }
853
854         /* Call commit handler if needed and defined */
855         if(ret == -EIWCOMMIT)
856                 ret = call_commit_handler(dev);
857
858         /* Here, we will generate the appropriate event if needed */
859
860         return ret;
861 }
862
863 /* ---------------------------------------------------------------- */
864 /*
865  * Wrapper to call a private Wireless Extension handler.
866  * We do various checks and also take care of moving data between
867  * user space and kernel space.
868  * It's not as nice and slimline as the standard wrapper. The cause
869  * is struct iw_priv_args, which was not really designed for the
870  * job we are going here.
871  *
872  * IMPORTANT : This function prevent to set and get data on the same
873  * IOCTL and enforce the SET/GET convention. Not doing it would be
874  * far too hairy...
875  * If you need to set and get data at the same time, please don't use
876  * a iw_handler but process it in your ioctl handler (i.e. use the
877  * old driver API).
878  */
879 static inline int ioctl_private_call(struct net_device *        dev,
880                                      struct ifreq *             ifr,
881                                      unsigned int               cmd,
882                                      iw_handler         handler)
883 {
884         struct iwreq *                  iwr = (struct iwreq *) ifr;
885         const struct iw_priv_args *     descr = NULL;
886         struct iw_request_info          info;
887         int                             extra_size = 0;
888         int                             i;
889         int                             ret = -EINVAL;
890
891         /* Get the description of the IOCTL */
892         for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
893                 if(cmd == dev->wireless_handlers->private_args[i].cmd) {
894                         descr = &(dev->wireless_handlers->private_args[i]);
895                         break;
896                 }
897
898 #ifdef WE_IOCTL_DEBUG
899         printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
900                ifr->ifr_name, cmd);
901         if(descr) {
902                 printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
903                        dev->name, descr->name,
904                        descr->set_args, descr->get_args);
905         }
906 #endif  /* WE_IOCTL_DEBUG */
907
908         /* Compute the size of the set/get arguments */
909         if(descr != NULL) {
910                 if(IW_IS_SET(cmd)) {
911                         int     offset = 0;     /* For sub-ioctls */
912                         /* Check for sub-ioctl handler */
913                         if(descr->name[0] == '\0')
914                                 /* Reserve one int for sub-ioctl index */
915                                 offset = sizeof(__u32);
916
917                         /* Size of set arguments */
918                         extra_size = get_priv_size(descr->set_args);
919
920                         /* Does it fits in iwr ? */
921                         if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
922                            ((extra_size + offset) <= IFNAMSIZ))
923                                 extra_size = 0;
924                 } else {
925                         /* Size of get arguments */
926                         extra_size = get_priv_size(descr->get_args);
927
928                         /* Does it fits in iwr ? */
929                         if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
930                            (extra_size <= IFNAMSIZ))
931                                 extra_size = 0;
932                 }
933         }
934
935         /* Prepare the call */
936         info.cmd = cmd;
937         info.flags = 0;
938
939         /* Check if we have a pointer to user space data or not. */
940         if(extra_size == 0) {
941                 /* No extra arguments. Trivial to handle */
942                 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
943         } else {
944                 char *  extra;
945                 int     err;
946
947                 /* Check what user space is giving us */
948                 if(IW_IS_SET(cmd)) {
949                         /* Check NULL pointer */
950                         if((iwr->u.data.pointer == NULL) &&
951                            (iwr->u.data.length != 0))
952                                 return -EFAULT;
953
954                         /* Does it fits within bounds ? */
955                         if(iwr->u.data.length > (descr->set_args &
956                                                  IW_PRIV_SIZE_MASK))
957                                 return -E2BIG;
958                 } else {
959                         /* Check NULL pointer */
960                         if(iwr->u.data.pointer == NULL)
961                                 return -EFAULT;
962                 }
963
964 #ifdef WE_IOCTL_DEBUG
965                 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
966                        dev->name, extra_size);
967 #endif  /* WE_IOCTL_DEBUG */
968
969                 /* Always allocate for max space. Easier, and won't last
970                  * long... */
971                 extra = kmalloc(extra_size, GFP_KERNEL);
972                 if (extra == NULL) {
973                         return -ENOMEM;
974                 }
975
976                 /* If it is a SET, get all the extra data in here */
977                 if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
978                         err = copy_from_user(extra, iwr->u.data.pointer,
979                                              extra_size);
980                         if (err) {
981                                 kfree(extra);
982                                 return -EFAULT;
983                         }
984 #ifdef WE_IOCTL_DEBUG
985                         printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
986                                dev->name, iwr->u.data.length);
987 #endif  /* WE_IOCTL_DEBUG */
988                 }
989
990                 /* Call the handler */
991                 ret = handler(dev, &info, &(iwr->u), extra);
992
993                 /* If we have something to return to the user */
994                 if (!ret && IW_IS_GET(cmd)) {
995
996                         /* Adjust for the actual length if it's variable,
997                          * avoid leaking kernel bits outside. */
998                         if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
999                                 extra_size = adjust_priv_size(descr->get_args,
1000                                                               &(iwr->u));
1001                         }
1002
1003                         err = copy_to_user(iwr->u.data.pointer, extra,
1004                                            extra_size);
1005                         if (err)
1006                                 ret =  -EFAULT;                            
1007 #ifdef WE_IOCTL_DEBUG
1008                         printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
1009                                dev->name, iwr->u.data.length);
1010 #endif  /* WE_IOCTL_DEBUG */
1011                 }
1012
1013                 /* Cleanup - I told you it wasn't that long ;-) */
1014                 kfree(extra);
1015         }
1016
1017
1018         /* Call commit handler if needed and defined */
1019         if(ret == -EIWCOMMIT)
1020                 ret = call_commit_handler(dev);
1021
1022         return ret;
1023 }
1024
1025 /* ---------------------------------------------------------------- */
1026 /*
1027  * Main IOCTl dispatcher. Called from the main networking code
1028  * (dev_ioctl() in net/core/dev.c).
1029  * Check the type of IOCTL and call the appropriate wrapper...
1030  */
1031 int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
1032 {
1033         struct net_device *dev;
1034         iw_handler      handler;
1035
1036         /* Permissions are already checked in dev_ioctl() before calling us.
1037          * The copy_to/from_user() of ifr is also dealt with in there */
1038
1039         /* Make sure the device exist */
1040         if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
1041                 return -ENODEV;
1042
1043         /* A bunch of special cases, then the generic case...
1044          * Note that 'cmd' is already filtered in dev_ioctl() with
1045          * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
1046         switch(cmd) 
1047         {
1048                 case SIOCGIWSTATS:
1049                         /* Get Wireless Stats */
1050                         return dev_iwstats(dev, ifr);
1051
1052                 case SIOCGIWPRIV:
1053                         /* Check if we have some wireless handlers defined */
1054                         if(dev->wireless_handlers != NULL) {
1055                                 /* We export to user space the definition of
1056                                  * the private handler ourselves */
1057                                 return ioctl_export_private(dev, ifr);
1058                         }
1059                         // ## Fall-through for old API ##
1060                 default:
1061                         /* Generic IOCTL */
1062                         /* Basic check */
1063                         if (!netif_device_present(dev))
1064                                 return -ENODEV;
1065                         /* New driver API : try to find the handler */
1066                         handler = get_handler(dev, cmd);
1067                         if(handler != NULL) {
1068                                 /* Standard and private are not the same */
1069                                 if(cmd < SIOCIWFIRSTPRIV)
1070                                         return ioctl_standard_call(dev,
1071                                                                    ifr,
1072                                                                    cmd,
1073                                                                    handler);
1074                                 else
1075                                         return ioctl_private_call(dev,
1076                                                                   ifr,
1077                                                                   cmd,
1078                                                                   handler);
1079                         }
1080                         /* Old driver API : call driver ioctl handler */
1081                         if (dev->do_ioctl) {
1082                                 return dev->do_ioctl(dev, ifr, cmd);
1083                         }
1084                         return -EOPNOTSUPP;
1085         }
1086         /* Not reached */
1087         return -EINVAL;
1088 }
1089
1090 /************************* EVENT PROCESSING *************************/
1091 /*
1092  * Process events generated by the wireless layer or the driver.
1093  * Most often, the event will be propagated through rtnetlink
1094  */
1095
1096 #ifdef WE_EVENT_NETLINK
1097 /* "rtnl" is defined in net/core/rtnetlink.c, but we need it here.
1098  * It is declared in <linux/rtnetlink.h> */
1099
1100 /* ---------------------------------------------------------------- */
1101 /*
1102  * Fill a rtnetlink message with our event data.
1103  * Note that we propage only the specified event and don't dump the
1104  * current wireless config. Dumping the wireless config is far too
1105  * expensive (for each parameter, the driver need to query the hardware).
1106  */
1107 static inline int rtnetlink_fill_iwinfo(struct sk_buff *        skb,
1108                                         struct net_device *     dev,
1109                                         int                     type,
1110                                         char *                  event,
1111                                         int                     event_len)
1112 {
1113         struct ifinfomsg *r;
1114         struct nlmsghdr  *nlh;
1115         unsigned char    *b = skb->tail;
1116
1117         nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
1118         r = NLMSG_DATA(nlh);
1119         r->ifi_family = AF_UNSPEC;
1120         r->__ifi_pad = 0;
1121         r->ifi_type = dev->type;
1122         r->ifi_index = dev->ifindex;
1123         r->ifi_flags = dev->flags;
1124         r->ifi_change = 0;      /* Wireless changes don't affect those flags */
1125
1126         /* Add the wireless events in the netlink packet */
1127         RTA_PUT(skb, IFLA_WIRELESS,
1128                 event_len, event);
1129
1130         nlh->nlmsg_len = skb->tail - b;
1131         return skb->len;
1132
1133 nlmsg_failure:
1134 rtattr_failure:
1135         skb_trim(skb, b - skb->data);
1136         return -1;
1137 }
1138
1139 /* ---------------------------------------------------------------- */
1140 /*
1141  * Create and broadcast and send it on the standard rtnetlink socket
1142  * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1143  * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1144  * within a RTM_NEWLINK event.
1145  */
1146 static inline void rtmsg_iwinfo(struct net_device *     dev,
1147                                 char *                  event,
1148                                 int                     event_len)
1149 {
1150         struct sk_buff *skb;
1151         int size = NLMSG_GOODSIZE;
1152
1153         skb = alloc_skb(size, GFP_ATOMIC);
1154         if (!skb)
1155                 return;
1156
1157         if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
1158                                   event, event_len) < 0) {
1159                 kfree_skb(skb);
1160                 return;
1161         }
1162         NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
1163         netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_ATOMIC);
1164 }
1165 #endif  /* WE_EVENT_NETLINK */
1166
1167 /* ---------------------------------------------------------------- */
1168 /*
1169  * Main event dispatcher. Called from other parts and drivers.
1170  * Send the event on the appropriate channels.
1171  * May be called from interrupt context.
1172  */
1173 void wireless_send_event(struct net_device *    dev,
1174                          unsigned int           cmd,
1175                          union iwreq_data *     wrqu,
1176                          char *                 extra)
1177 {
1178         const struct iw_ioctl_description *     descr = NULL;
1179         int extra_len = 0;
1180         struct iw_event  *event;                /* Mallocated whole event */
1181         int event_len;                          /* Its size */
1182         int hdr_len;                            /* Size of the event header */
1183         int wrqu_off = 0;                       /* Offset in wrqu */
1184         /* Don't "optimise" the following variable, it will crash */
1185         unsigned        cmd_index;              /* *MUST* be unsigned */
1186
1187         /* Get the description of the Event */
1188         if(cmd <= SIOCIWLAST) {
1189                 cmd_index = cmd - SIOCIWFIRST;
1190                 if(cmd_index < standard_ioctl_num)
1191                         descr = &(standard_ioctl[cmd_index]);
1192         } else {
1193                 cmd_index = cmd - IWEVFIRST;
1194                 if(cmd_index < standard_event_num)
1195                         descr = &(standard_event[cmd_index]);
1196         }
1197         /* Don't accept unknown events */
1198         if(descr == NULL) {
1199                 /* Note : we don't return an error to the driver, because
1200                  * the driver would not know what to do about it. It can't
1201                  * return an error to the user, because the event is not
1202                  * initiated by a user request.
1203                  * The best the driver could do is to log an error message.
1204                  * We will do it ourselves instead...
1205                  */
1206                 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
1207                        dev->name, cmd);
1208                 return;
1209         }
1210 #ifdef WE_EVENT_DEBUG
1211         printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
1212                dev->name, cmd);
1213         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);
1214 #endif  /* WE_EVENT_DEBUG */
1215
1216         /* Check extra parameters and set extra_len */
1217         if(descr->header_type == IW_HEADER_TYPE_POINT) {
1218                 /* Check if number of token fits within bounds */
1219                 if(wrqu->data.length > descr->max_tokens) {
1220                         printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
1221                         return;
1222                 }
1223                 if(wrqu->data.length < descr->min_tokens) {
1224                         printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
1225                         return;
1226                 }
1227                 /* Calculate extra_len - extra is NULL for restricted events */
1228                 if(extra != NULL)
1229                         extra_len = wrqu->data.length * descr->token_size;
1230                 /* Always at an offset in wrqu */
1231                 wrqu_off = IW_EV_POINT_OFF;
1232 #ifdef WE_EVENT_DEBUG
1233                 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
1234 #endif  /* WE_EVENT_DEBUG */
1235         }
1236
1237         /* Total length of the event */
1238         hdr_len = event_type_size[descr->header_type];
1239         event_len = hdr_len + extra_len;
1240
1241 #ifdef WE_EVENT_DEBUG
1242         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);
1243 #endif  /* WE_EVENT_DEBUG */
1244
1245         /* Create temporary buffer to hold the event */
1246         event = kmalloc(event_len, GFP_ATOMIC);
1247         if(event == NULL)
1248                 return;
1249
1250         /* Fill event */
1251         event->len = event_len;
1252         event->cmd = cmd;
1253         memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
1254         if(extra != NULL)
1255                 memcpy(((char *) event) + hdr_len, extra, extra_len);
1256
1257 #ifdef WE_EVENT_NETLINK
1258         /* rtnetlink event channel */
1259         rtmsg_iwinfo(dev, (char *) event, event_len);
1260 #endif  /* WE_EVENT_NETLINK */
1261
1262         /* Cleanup */
1263         kfree(event);
1264
1265         return;         /* Always success, I guess ;-) */
1266 }
1267
1268 /********************** ENHANCED IWSPY SUPPORT **********************/
1269 /*
1270  * In the old days, the driver was handling spy support all by itself.
1271  * Now, the driver can delegate this task to Wireless Extensions.
1272  * It needs to use those standard spy iw_handler in struct iw_handler_def,
1273  * push data to us via wireless_spy_update() and include struct iw_spy_data
1274  * in its private part (and export it in net_device->wireless_data->spy_data).
1275  * One of the main advantage of centralising spy support here is that
1276  * it becomes much easier to improve and extend it without having to touch
1277  * the drivers. One example is the addition of the Spy-Threshold events.
1278  */
1279
1280 /* ---------------------------------------------------------------- */
1281 /*
1282  * Return the pointer to the spy data in the driver.
1283  * Because this is called on the Rx path via wireless_spy_update(),
1284  * we want it to be efficient...
1285  */
1286 static inline struct iw_spy_data * get_spydata(struct net_device *dev)
1287 {
1288         /* This is the new way */
1289         if(dev->wireless_data)
1290                 return(dev->wireless_data->spy_data);
1291         return NULL;
1292 }
1293
1294 /*------------------------------------------------------------------*/
1295 /*
1296  * Standard Wireless Handler : set Spy List
1297  */
1298 int iw_handler_set_spy(struct net_device *      dev,
1299                        struct iw_request_info * info,
1300                        union iwreq_data *       wrqu,
1301                        char *                   extra)
1302 {
1303         struct iw_spy_data *    spydata = get_spydata(dev);
1304         struct sockaddr *       address = (struct sockaddr *) extra;
1305
1306         /* Make sure driver is not buggy or using the old API */
1307         if(!spydata)
1308                 return -EOPNOTSUPP;
1309
1310         /* Disable spy collection while we copy the addresses.
1311          * While we copy addresses, any call to wireless_spy_update()
1312          * will NOP. This is OK, as anyway the addresses are changing. */
1313         spydata->spy_number = 0;
1314
1315         /* We want to operate without locking, because wireless_spy_update()
1316          * most likely will happen in the interrupt handler, and therefore
1317          * have its own locking constraints and needs performance.
1318          * The rtnl_lock() make sure we don't race with the other iw_handlers.
1319          * This make sure wireless_spy_update() "see" that the spy list
1320          * is temporarily disabled. */
1321         wmb();
1322
1323         /* Are there are addresses to copy? */
1324         if(wrqu->data.length > 0) {
1325                 int i;
1326
1327                 /* Copy addresses */
1328                 for(i = 0; i < wrqu->data.length; i++)
1329                         memcpy(spydata->spy_address[i], address[i].sa_data,
1330                                ETH_ALEN);
1331                 /* Reset stats */
1332                 memset(spydata->spy_stat, 0,
1333                        sizeof(struct iw_quality) * IW_MAX_SPY);
1334
1335 #ifdef WE_SPY_DEBUG
1336                 printk(KERN_DEBUG "iw_handler_set_spy() :  wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length);
1337                 for (i = 0; i < wrqu->data.length; i++)
1338                         printk(KERN_DEBUG
1339                                "%02X:%02X:%02X:%02X:%02X:%02X \n",
1340                                spydata->spy_address[i][0],
1341                                spydata->spy_address[i][1],
1342                                spydata->spy_address[i][2],
1343                                spydata->spy_address[i][3],
1344                                spydata->spy_address[i][4],
1345                                spydata->spy_address[i][5]);
1346 #endif  /* WE_SPY_DEBUG */
1347         }
1348
1349         /* Make sure above is updated before re-enabling */
1350         wmb();
1351
1352         /* Enable addresses */
1353         spydata->spy_number = wrqu->data.length;
1354
1355         return 0;
1356 }
1357
1358 /*------------------------------------------------------------------*/
1359 /*
1360  * Standard Wireless Handler : get Spy List
1361  */
1362 int iw_handler_get_spy(struct net_device *      dev,
1363                        struct iw_request_info * info,
1364                        union iwreq_data *       wrqu,
1365                        char *                   extra)
1366 {
1367         struct iw_spy_data *    spydata = get_spydata(dev);
1368         struct sockaddr *       address = (struct sockaddr *) extra;
1369         int                     i;
1370
1371         /* Make sure driver is not buggy or using the old API */
1372         if(!spydata)
1373                 return -EOPNOTSUPP;
1374
1375         wrqu->data.length = spydata->spy_number;
1376
1377         /* Copy addresses. */
1378         for(i = 0; i < spydata->spy_number; i++)        {
1379                 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1380                 address[i].sa_family = AF_UNIX;
1381         }
1382         /* Copy stats to the user buffer (just after). */
1383         if(spydata->spy_number > 0)
1384                 memcpy(extra  + (sizeof(struct sockaddr) *spydata->spy_number),
1385                        spydata->spy_stat,
1386                        sizeof(struct iw_quality) * spydata->spy_number);
1387         /* Reset updated flags. */
1388         for(i = 0; i < spydata->spy_number; i++)
1389                 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
1390         return 0;
1391 }
1392
1393 /*------------------------------------------------------------------*/
1394 /*
1395  * Standard Wireless Handler : set spy threshold
1396  */
1397 int iw_handler_set_thrspy(struct net_device *   dev,
1398                           struct iw_request_info *info,
1399                           union iwreq_data *    wrqu,
1400                           char *                extra)
1401 {
1402         struct iw_spy_data *    spydata = get_spydata(dev);
1403         struct iw_thrspy *      threshold = (struct iw_thrspy *) extra;
1404
1405         /* Make sure driver is not buggy or using the old API */
1406         if(!spydata)
1407                 return -EOPNOTSUPP;
1408
1409         /* Just do it */
1410         memcpy(&(spydata->spy_thr_low), &(threshold->low),
1411                2 * sizeof(struct iw_quality));
1412
1413         /* Clear flag */
1414         memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1415
1416 #ifdef WE_SPY_DEBUG
1417         printk(KERN_DEBUG "iw_handler_set_thrspy() :  low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
1418 #endif  /* WE_SPY_DEBUG */
1419
1420         return 0;
1421 }
1422
1423 /*------------------------------------------------------------------*/
1424 /*
1425  * Standard Wireless Handler : get spy threshold
1426  */
1427 int iw_handler_get_thrspy(struct net_device *   dev,
1428                           struct iw_request_info *info,
1429                           union iwreq_data *    wrqu,
1430                           char *                extra)
1431 {
1432         struct iw_spy_data *    spydata = get_spydata(dev);
1433         struct iw_thrspy *      threshold = (struct iw_thrspy *) extra;
1434
1435         /* Make sure driver is not buggy or using the old API */
1436         if(!spydata)
1437                 return -EOPNOTSUPP;
1438
1439         /* Just do it */
1440         memcpy(&(threshold->low), &(spydata->spy_thr_low),
1441                2 * sizeof(struct iw_quality));
1442
1443         return 0;
1444 }
1445
1446 /*------------------------------------------------------------------*/
1447 /*
1448  * Prepare and send a Spy Threshold event
1449  */
1450 static void iw_send_thrspy_event(struct net_device *    dev,
1451                                  struct iw_spy_data *   spydata,
1452                                  unsigned char *        address,
1453                                  struct iw_quality *    wstats)
1454 {
1455         union iwreq_data        wrqu;
1456         struct iw_thrspy        threshold;
1457
1458         /* Init */
1459         wrqu.data.length = 1;
1460         wrqu.data.flags = 0;
1461         /* Copy address */
1462         memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1463         threshold.addr.sa_family = ARPHRD_ETHER;
1464         /* Copy stats */
1465         memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1466         /* Copy also thresholds */
1467         memcpy(&(threshold.low), &(spydata->spy_thr_low),
1468                2 * sizeof(struct iw_quality));
1469
1470 #ifdef WE_SPY_DEBUG
1471         printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
1472                threshold.addr.sa_data[0],
1473                threshold.addr.sa_data[1],
1474                threshold.addr.sa_data[2],
1475                threshold.addr.sa_data[3],
1476                threshold.addr.sa_data[4],
1477                threshold.addr.sa_data[5], threshold.qual.level);
1478 #endif  /* WE_SPY_DEBUG */
1479
1480         /* Send event to user space */
1481         wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1482 }
1483
1484 /* ---------------------------------------------------------------- */
1485 /*
1486  * Call for the driver to update the spy data.
1487  * For now, the spy data is a simple array. As the size of the array is
1488  * small, this is good enough. If we wanted to support larger number of
1489  * spy addresses, we should use something more efficient...
1490  */
1491 void wireless_spy_update(struct net_device *    dev,
1492                          unsigned char *        address,
1493                          struct iw_quality *    wstats)
1494 {
1495         struct iw_spy_data *    spydata = get_spydata(dev);
1496         int                     i;
1497         int                     match = -1;
1498
1499         /* Make sure driver is not buggy or using the old API */
1500         if(!spydata)
1501                 return;
1502
1503 #ifdef WE_SPY_DEBUG
1504         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]);
1505 #endif  /* WE_SPY_DEBUG */
1506
1507         /* Update all records that match */
1508         for(i = 0; i < spydata->spy_number; i++)
1509                 if(!memcmp(address, spydata->spy_address[i], ETH_ALEN)) {
1510                         memcpy(&(spydata->spy_stat[i]), wstats,
1511                                sizeof(struct iw_quality));
1512                         match = i;
1513                 }
1514
1515         /* Generate an event if we cross the spy threshold.
1516          * To avoid event storms, we have a simple hysteresis : we generate
1517          * event only when we go under the low threshold or above the
1518          * high threshold. */
1519         if(match >= 0) {
1520                 if(spydata->spy_thr_under[match]) {
1521                         if(wstats->level > spydata->spy_thr_high.level) {
1522                                 spydata->spy_thr_under[match] = 0;
1523                                 iw_send_thrspy_event(dev, spydata,
1524                                                      address, wstats);
1525                         }
1526                 } else {
1527                         if(wstats->level < spydata->spy_thr_low.level) {
1528                                 spydata->spy_thr_under[match] = 1;
1529                                 iw_send_thrspy_event(dev, spydata,
1530                                                      address, wstats);
1531                         }
1532                 }
1533         }
1534 }
1535
1536 EXPORT_SYMBOL(iw_handler_get_spy);
1537 EXPORT_SYMBOL(iw_handler_get_thrspy);
1538 EXPORT_SYMBOL(iw_handler_set_spy);
1539 EXPORT_SYMBOL(iw_handler_set_thrspy);
1540 EXPORT_SYMBOL(wireless_send_event);
1541 EXPORT_SYMBOL(wireless_spy_update);