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