blob: 86db63d7f7602a7cd746ef3d69f9803670375c31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file implement the Wireless Extensions APIs.
3 *
4 * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00005 * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Jean Tourrilhes6582c162005-09-02 11:32:28 -070061 *
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)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -080068 *
69 * v8 - 17.02.06 - Jean II
70 * o RtNetlink requests support (SET/GET)
John W. Linvillebaef1862006-09-08 16:04:05 -040071 *
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()
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +000079 *
80 * v10 - 16.3.07 - Jean II
81 * o Prevent leaking of kernel space in stream on 64 bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84/***************************** INCLUDES *****************************/
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#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 */
Linus Torvalds9819d852006-01-10 19:35:19 -080094#include <linux/etherdevice.h> /* compare_ether_addr */
Herbert Xu782a6672006-08-03 23:54:41 +100095#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#include <linux/wireless.h> /* Pretty obvious */
98#include <net/iw_handler.h> /* New driver API */
Thomas Grafbd5785b2006-08-15 00:36:49 -070099#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include <asm/uaccess.h> /* copy_to_user() */
102
103/**************************** CONSTANTS ****************************/
104
105/* Debugging stuff */
106#undef WE_IOCTL_DEBUG /* Debug IOCTL API */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800107#undef WE_RTNETLINK_DEBUG /* Debug RtNetlink API */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#undef WE_EVENT_DEBUG /* Debug Event dispatcher */
109#undef WE_SPY_DEBUG /* Debug enhanced spy support */
110
111/* Options */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800112//CONFIG_NET_WIRELESS_RTNETLINK /* Wireless requests over RtNetlink */
113#define WE_EVENT_RTNETLINK /* Propagate events using RtNetlink */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define WE_SET_EVENT /* Generate an event on some set commands */
115
116/************************* GLOBAL VARIABLES *************************/
117/*
118 * You should not use global variables, because of re-entrancy.
119 * On our case, it's only const, so it's OK...
120 */
121/*
122 * Meta-data about all the standard Wireless Extension request we
123 * know about.
124 */
125static const struct iw_ioctl_description standard_ioctl[] = {
126 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
127 .header_type = IW_HEADER_TYPE_NULL,
128 },
129 [SIOCGIWNAME - SIOCIWFIRST] = {
130 .header_type = IW_HEADER_TYPE_CHAR,
131 .flags = IW_DESCR_FLAG_DUMP,
132 },
133 [SIOCSIWNWID - SIOCIWFIRST] = {
134 .header_type = IW_HEADER_TYPE_PARAM,
135 .flags = IW_DESCR_FLAG_EVENT,
136 },
137 [SIOCGIWNWID - SIOCIWFIRST] = {
138 .header_type = IW_HEADER_TYPE_PARAM,
139 .flags = IW_DESCR_FLAG_DUMP,
140 },
141 [SIOCSIWFREQ - SIOCIWFIRST] = {
142 .header_type = IW_HEADER_TYPE_FREQ,
143 .flags = IW_DESCR_FLAG_EVENT,
144 },
145 [SIOCGIWFREQ - SIOCIWFIRST] = {
146 .header_type = IW_HEADER_TYPE_FREQ,
147 .flags = IW_DESCR_FLAG_DUMP,
148 },
149 [SIOCSIWMODE - SIOCIWFIRST] = {
150 .header_type = IW_HEADER_TYPE_UINT,
151 .flags = IW_DESCR_FLAG_EVENT,
152 },
153 [SIOCGIWMODE - SIOCIWFIRST] = {
154 .header_type = IW_HEADER_TYPE_UINT,
155 .flags = IW_DESCR_FLAG_DUMP,
156 },
157 [SIOCSIWSENS - SIOCIWFIRST] = {
158 .header_type = IW_HEADER_TYPE_PARAM,
159 },
160 [SIOCGIWSENS - SIOCIWFIRST] = {
161 .header_type = IW_HEADER_TYPE_PARAM,
162 },
163 [SIOCSIWRANGE - SIOCIWFIRST] = {
164 .header_type = IW_HEADER_TYPE_NULL,
165 },
166 [SIOCGIWRANGE - SIOCIWFIRST] = {
167 .header_type = IW_HEADER_TYPE_POINT,
168 .token_size = 1,
169 .max_tokens = sizeof(struct iw_range),
170 .flags = IW_DESCR_FLAG_DUMP,
171 },
172 [SIOCSIWPRIV - SIOCIWFIRST] = {
173 .header_type = IW_HEADER_TYPE_NULL,
174 },
175 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800176 .header_type = IW_HEADER_TYPE_POINT,
177 .token_size = sizeof(struct iw_priv_args),
178 .max_tokens = 16,
179 .flags = IW_DESCR_FLAG_NOMAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 },
181 [SIOCSIWSTATS - SIOCIWFIRST] = {
182 .header_type = IW_HEADER_TYPE_NULL,
183 },
184 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800185 .header_type = IW_HEADER_TYPE_POINT,
186 .token_size = 1,
187 .max_tokens = sizeof(struct iw_statistics),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .flags = IW_DESCR_FLAG_DUMP,
189 },
190 [SIOCSIWSPY - SIOCIWFIRST] = {
191 .header_type = IW_HEADER_TYPE_POINT,
192 .token_size = sizeof(struct sockaddr),
193 .max_tokens = IW_MAX_SPY,
194 },
195 [SIOCGIWSPY - SIOCIWFIRST] = {
196 .header_type = IW_HEADER_TYPE_POINT,
197 .token_size = sizeof(struct sockaddr) +
198 sizeof(struct iw_quality),
199 .max_tokens = IW_MAX_SPY,
200 },
201 [SIOCSIWTHRSPY - SIOCIWFIRST] = {
202 .header_type = IW_HEADER_TYPE_POINT,
203 .token_size = sizeof(struct iw_thrspy),
204 .min_tokens = 1,
205 .max_tokens = 1,
206 },
207 [SIOCGIWTHRSPY - SIOCIWFIRST] = {
208 .header_type = IW_HEADER_TYPE_POINT,
209 .token_size = sizeof(struct iw_thrspy),
210 .min_tokens = 1,
211 .max_tokens = 1,
212 },
213 [SIOCSIWAP - SIOCIWFIRST] = {
214 .header_type = IW_HEADER_TYPE_ADDR,
215 },
216 [SIOCGIWAP - SIOCIWFIRST] = {
217 .header_type = IW_HEADER_TYPE_ADDR,
218 .flags = IW_DESCR_FLAG_DUMP,
219 },
fff9cfd2005-05-12 20:24:19 -0400220 [SIOCSIWMLME - SIOCIWFIRST] = {
221 .header_type = IW_HEADER_TYPE_POINT,
222 .token_size = 1,
223 .min_tokens = sizeof(struct iw_mlme),
224 .max_tokens = sizeof(struct iw_mlme),
225 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 [SIOCGIWAPLIST - SIOCIWFIRST] = {
227 .header_type = IW_HEADER_TYPE_POINT,
228 .token_size = sizeof(struct sockaddr) +
229 sizeof(struct iw_quality),
230 .max_tokens = IW_MAX_AP,
231 .flags = IW_DESCR_FLAG_NOMAX,
232 },
233 [SIOCSIWSCAN - SIOCIWFIRST] = {
fff9cfd2005-05-12 20:24:19 -0400234 .header_type = IW_HEADER_TYPE_POINT,
235 .token_size = 1,
236 .min_tokens = 0,
237 .max_tokens = sizeof(struct iw_scan_req),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 },
239 [SIOCGIWSCAN - SIOCIWFIRST] = {
240 .header_type = IW_HEADER_TYPE_POINT,
241 .token_size = 1,
242 .max_tokens = IW_SCAN_MAX_DATA,
243 .flags = IW_DESCR_FLAG_NOMAX,
244 },
245 [SIOCSIWESSID - SIOCIWFIRST] = {
246 .header_type = IW_HEADER_TYPE_POINT,
247 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400248 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .flags = IW_DESCR_FLAG_EVENT,
250 },
251 [SIOCGIWESSID - SIOCIWFIRST] = {
252 .header_type = IW_HEADER_TYPE_POINT,
253 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400254 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .flags = IW_DESCR_FLAG_DUMP,
256 },
257 [SIOCSIWNICKN - SIOCIWFIRST] = {
258 .header_type = IW_HEADER_TYPE_POINT,
259 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400260 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 },
262 [SIOCGIWNICKN - SIOCIWFIRST] = {
263 .header_type = IW_HEADER_TYPE_POINT,
264 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400265 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 },
267 [SIOCSIWRATE - SIOCIWFIRST] = {
268 .header_type = IW_HEADER_TYPE_PARAM,
269 },
270 [SIOCGIWRATE - SIOCIWFIRST] = {
271 .header_type = IW_HEADER_TYPE_PARAM,
272 },
273 [SIOCSIWRTS - SIOCIWFIRST] = {
274 .header_type = IW_HEADER_TYPE_PARAM,
275 },
276 [SIOCGIWRTS - SIOCIWFIRST] = {
277 .header_type = IW_HEADER_TYPE_PARAM,
278 },
279 [SIOCSIWFRAG - SIOCIWFIRST] = {
280 .header_type = IW_HEADER_TYPE_PARAM,
281 },
282 [SIOCGIWFRAG - SIOCIWFIRST] = {
283 .header_type = IW_HEADER_TYPE_PARAM,
284 },
285 [SIOCSIWTXPOW - SIOCIWFIRST] = {
286 .header_type = IW_HEADER_TYPE_PARAM,
287 },
288 [SIOCGIWTXPOW - SIOCIWFIRST] = {
289 .header_type = IW_HEADER_TYPE_PARAM,
290 },
291 [SIOCSIWRETRY - SIOCIWFIRST] = {
292 .header_type = IW_HEADER_TYPE_PARAM,
293 },
294 [SIOCGIWRETRY - SIOCIWFIRST] = {
295 .header_type = IW_HEADER_TYPE_PARAM,
296 },
297 [SIOCSIWENCODE - SIOCIWFIRST] = {
298 .header_type = IW_HEADER_TYPE_POINT,
299 .token_size = 1,
300 .max_tokens = IW_ENCODING_TOKEN_MAX,
301 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
302 },
303 [SIOCGIWENCODE - SIOCIWFIRST] = {
304 .header_type = IW_HEADER_TYPE_POINT,
305 .token_size = 1,
306 .max_tokens = IW_ENCODING_TOKEN_MAX,
307 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
308 },
309 [SIOCSIWPOWER - SIOCIWFIRST] = {
310 .header_type = IW_HEADER_TYPE_PARAM,
311 },
312 [SIOCGIWPOWER - SIOCIWFIRST] = {
313 .header_type = IW_HEADER_TYPE_PARAM,
314 },
fff9cfd2005-05-12 20:24:19 -0400315 [SIOCSIWGENIE - SIOCIWFIRST] = {
316 .header_type = IW_HEADER_TYPE_POINT,
317 .token_size = 1,
318 .max_tokens = IW_GENERIC_IE_MAX,
319 },
320 [SIOCGIWGENIE - SIOCIWFIRST] = {
321 .header_type = IW_HEADER_TYPE_POINT,
322 .token_size = 1,
323 .max_tokens = IW_GENERIC_IE_MAX,
324 },
325 [SIOCSIWAUTH - SIOCIWFIRST] = {
326 .header_type = IW_HEADER_TYPE_PARAM,
327 },
328 [SIOCGIWAUTH - SIOCIWFIRST] = {
329 .header_type = IW_HEADER_TYPE_PARAM,
330 },
331 [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
332 .header_type = IW_HEADER_TYPE_POINT,
333 .token_size = 1,
334 .min_tokens = sizeof(struct iw_encode_ext),
335 .max_tokens = sizeof(struct iw_encode_ext) +
336 IW_ENCODING_TOKEN_MAX,
337 },
338 [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
339 .header_type = IW_HEADER_TYPE_POINT,
340 .token_size = 1,
341 .min_tokens = sizeof(struct iw_encode_ext),
342 .max_tokens = sizeof(struct iw_encode_ext) +
343 IW_ENCODING_TOKEN_MAX,
344 },
345 [SIOCSIWPMKSA - SIOCIWFIRST] = {
346 .header_type = IW_HEADER_TYPE_POINT,
347 .token_size = 1,
348 .min_tokens = sizeof(struct iw_pmksa),
349 .max_tokens = sizeof(struct iw_pmksa),
350 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800352static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354/*
355 * Meta-data about all the additional standard Wireless Extension events
356 * we know about.
357 */
358static const struct iw_ioctl_description standard_event[] = {
359 [IWEVTXDROP - IWEVFIRST] = {
360 .header_type = IW_HEADER_TYPE_ADDR,
361 },
362 [IWEVQUAL - IWEVFIRST] = {
363 .header_type = IW_HEADER_TYPE_QUAL,
364 },
365 [IWEVCUSTOM - IWEVFIRST] = {
366 .header_type = IW_HEADER_TYPE_POINT,
367 .token_size = 1,
368 .max_tokens = IW_CUSTOM_MAX,
369 },
370 [IWEVREGISTERED - IWEVFIRST] = {
371 .header_type = IW_HEADER_TYPE_ADDR,
372 },
373 [IWEVEXPIRED - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900374 .header_type = IW_HEADER_TYPE_ADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 },
fff9cfd2005-05-12 20:24:19 -0400376 [IWEVGENIE - IWEVFIRST] = {
377 .header_type = IW_HEADER_TYPE_POINT,
378 .token_size = 1,
379 .max_tokens = IW_GENERIC_IE_MAX,
380 },
381 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900382 .header_type = IW_HEADER_TYPE_POINT,
fff9cfd2005-05-12 20:24:19 -0400383 .token_size = 1,
384 .max_tokens = sizeof(struct iw_michaelmicfailure),
385 },
386 [IWEVASSOCREQIE - IWEVFIRST] = {
387 .header_type = IW_HEADER_TYPE_POINT,
388 .token_size = 1,
389 .max_tokens = IW_GENERIC_IE_MAX,
390 },
391 [IWEVASSOCRESPIE - IWEVFIRST] = {
392 .header_type = IW_HEADER_TYPE_POINT,
393 .token_size = 1,
394 .max_tokens = IW_GENERIC_IE_MAX,
395 },
396 [IWEVPMKIDCAND - IWEVFIRST] = {
397 .header_type = IW_HEADER_TYPE_POINT,
398 .token_size = 1,
399 .max_tokens = sizeof(struct iw_pmkid_cand),
400 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800402static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/* Size (in bytes) of the various private data types */
405static const char iw_priv_type_size[] = {
406 0, /* IW_PRIV_TYPE_NONE */
407 1, /* IW_PRIV_TYPE_BYTE */
408 1, /* IW_PRIV_TYPE_CHAR */
409 0, /* Not defined */
410 sizeof(__u32), /* IW_PRIV_TYPE_INT */
411 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
412 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
413 0, /* Not defined */
414};
415
416/* Size (in bytes) of various events */
417static const int event_type_size[] = {
418 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
419 0,
420 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
421 0,
422 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
423 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
424 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
425 0,
426 IW_EV_POINT_LEN, /* Without variable payload */
427 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
428 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
429};
430
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +0000431/* Size (in bytes) of various events, as packed */
432static const int event_type_pk_size[] = {
433 IW_EV_LCP_PK_LEN, /* IW_HEADER_TYPE_NULL */
434 0,
435 IW_EV_CHAR_PK_LEN, /* IW_HEADER_TYPE_CHAR */
436 0,
437 IW_EV_UINT_PK_LEN, /* IW_HEADER_TYPE_UINT */
438 IW_EV_FREQ_PK_LEN, /* IW_HEADER_TYPE_FREQ */
439 IW_EV_ADDR_PK_LEN, /* IW_HEADER_TYPE_ADDR */
440 0,
441 IW_EV_POINT_PK_LEN, /* Without variable payload */
442 IW_EV_PARAM_PK_LEN, /* IW_HEADER_TYPE_PARAM */
443 IW_EV_QUAL_PK_LEN, /* IW_HEADER_TYPE_QUAL */
444};
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/************************ COMMON SUBROUTINES ************************/
447/*
448 * Stuff that may be used in various place or doesn't fit in one
449 * of the section below.
450 */
451
452/* ---------------------------------------------------------------- */
453/*
454 * Return the driver handler associated with a specific Wireless Extension.
455 * Called from various place, so make sure it remains efficient.
456 */
457static inline iw_handler get_handler(struct net_device *dev,
458 unsigned int cmd)
459{
460 /* Don't "optimise" the following variable, it will crash */
461 unsigned int index; /* *MUST* be unsigned */
462
463 /* Check if we have some wireless handlers defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700464 if (dev->wireless_handlers == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return NULL;
466
467 /* Try as a standard command */
468 index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700469 if (index < dev->wireless_handlers->num_standard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return dev->wireless_handlers->standard[index];
471
472 /* Try as a private command */
473 index = cmd - SIOCIWFIRSTPRIV;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700474 if (index < dev->wireless_handlers->num_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return dev->wireless_handlers->private[index];
476
477 /* Not found */
478 return NULL;
479}
480
481/* ---------------------------------------------------------------- */
482/*
483 * Get statistics out of the driver
484 */
485static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
486{
487 /* New location */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700488 if ((dev->wireless_handlers != NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 (dev->wireless_handlers->get_wireless_stats != NULL))
490 return dev->wireless_handlers->get_wireless_stats(dev);
491
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700492 /* Not found */
493 return (struct iw_statistics *) NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/* ---------------------------------------------------------------- */
497/*
498 * Call the commit handler in the driver
499 * (if exist and if conditions are right)
500 *
501 * Note : our current commit strategy is currently pretty dumb,
502 * but we will be able to improve on that...
503 * The goal is to try to agreagate as many changes as possible
504 * before doing the commit. Drivers that will define a commit handler
505 * are usually those that need a reset after changing parameters, so
506 * we want to minimise the number of reset.
507 * A cool idea is to use a timer : at each "set" command, we re-set the
508 * timer, when the timer eventually fires, we call the driver.
509 * Hopefully, more on that later.
510 *
511 * Also, I'm waiting to see how many people will complain about the
512 * netif_running(dev) test. I'm open on that one...
513 * Hopefully, the driver will remember to do a commit in "open()" ;-)
514 */
515static inline int call_commit_handler(struct net_device * dev)
516{
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700517 if ((netif_running(dev)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 (dev->wireless_handlers->standard[0] != NULL)) {
519 /* Call the commit handler on the driver */
520 return dev->wireless_handlers->standard[0](dev, NULL,
521 NULL, NULL);
522 } else
523 return 0; /* Command completed successfully */
524}
525
526/* ---------------------------------------------------------------- */
527/*
528 * Calculate size of private arguments
529 */
530static inline int get_priv_size(__u16 args)
531{
532 int num = args & IW_PRIV_SIZE_MASK;
533 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
534
535 return num * iw_priv_type_size[type];
536}
537
538/* ---------------------------------------------------------------- */
539/*
540 * Re-calculate the size of private arguments
541 */
542static inline int adjust_priv_size(__u16 args,
543 union iwreq_data * wrqu)
544{
545 int num = wrqu->data.length;
546 int max = args & IW_PRIV_SIZE_MASK;
547 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
548
549 /* Make sure the driver doesn't goof up */
550 if (max < num)
551 num = max;
552
553 return num * iw_priv_type_size[type];
554}
555
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800556/* ---------------------------------------------------------------- */
557/*
558 * Standard Wireless Handler : get wireless stats
559 * Allow programatic access to /proc/net/wireless even if /proc
560 * doesn't exist... Also more efficient...
561 */
562static int iw_handler_get_iwstats(struct net_device * dev,
563 struct iw_request_info * info,
564 union iwreq_data * wrqu,
565 char * extra)
566{
567 /* Get stats from the driver */
568 struct iw_statistics *stats;
569
570 stats = get_wireless_stats(dev);
571 if (stats != (struct iw_statistics *) NULL) {
572
573 /* Copy statistics to extra */
574 memcpy(extra, stats, sizeof(struct iw_statistics));
575 wrqu->data.length = sizeof(struct iw_statistics);
576
577 /* Check if we need to clear the updated flag */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700578 if (wrqu->data.flags != 0)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800579 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
580 return 0;
581 } else
582 return -EOPNOTSUPP;
583}
584
585/* ---------------------------------------------------------------- */
586/*
587 * Standard Wireless Handler : get iwpriv definitions
588 * Export the driver private handler definition
589 * They will be picked up by tools like iwpriv...
590 */
591static int iw_handler_get_private(struct net_device * dev,
592 struct iw_request_info * info,
593 union iwreq_data * wrqu,
594 char * extra)
595{
596 /* Check if the driver has something to export */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700597 if ((dev->wireless_handlers->num_private_args == 0) ||
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800598 (dev->wireless_handlers->private_args == NULL))
599 return -EOPNOTSUPP;
600
601 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700602 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800603 /* User space can't know in advance how large the buffer
604 * needs to be. Give it a hint, so that we can support
605 * any size buffer we want somewhat efficiently... */
606 wrqu->data.length = dev->wireless_handlers->num_private_args;
607 return -E2BIG;
608 }
609
610 /* Set the number of available ioctls. */
611 wrqu->data.length = dev->wireless_handlers->num_private_args;
612
613 /* Copy structure to the user buffer. */
614 memcpy(extra, dev->wireless_handlers->private_args,
615 sizeof(struct iw_priv_args) * wrqu->data.length);
616
617 return 0;
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/******************** /proc/net/wireless SUPPORT ********************/
622/*
623 * The /proc/net/wireless file is a human readable user-space interface
624 * exporting various wireless specific statistics from the wireless devices.
625 * This is the most popular part of the Wireless Extensions ;-)
626 *
627 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
628 * The content of the file is basically the content of "struct iw_statistics".
629 */
630
631#ifdef CONFIG_PROC_FS
632
633/* ---------------------------------------------------------------- */
634/*
635 * Print one entry (line) of /proc/net/wireless
636 */
637static __inline__ void wireless_seq_printf_stats(struct seq_file *seq,
638 struct net_device *dev)
639{
640 /* Get stats from the driver */
641 struct iw_statistics *stats = get_wireless_stats(dev);
642
643 if (stats) {
644 seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
645 "%6d %6d %6d\n",
646 dev->name, stats->status, stats->qual.qual,
647 stats->qual.updated & IW_QUAL_QUAL_UPDATED
648 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900649 ((__s32) stats->qual.level) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700650 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
652 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900653 ((__s32) stats->qual.noise) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700654 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 stats->qual.updated & IW_QUAL_NOISE_UPDATED
656 ? '.' : ' ',
657 stats->discard.nwid, stats->discard.code,
658 stats->discard.fragment, stats->discard.retries,
659 stats->discard.misc, stats->miss.beacon);
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700660 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662}
663
664/* ---------------------------------------------------------------- */
665/*
666 * Print info for /proc/net/wireless (print all entries)
667 */
668static int wireless_seq_show(struct seq_file *seq, void *v)
669{
670 if (v == SEQ_START_TOKEN)
671 seq_printf(seq, "Inter-| sta-| Quality | Discarded "
672 "packets | Missed | WE\n"
673 " face | tus | link level noise | nwid "
674 "crypt frag retry misc | beacon | %d\n",
675 WIRELESS_EXT);
676 else
677 wireless_seq_printf_stats(seq, v);
678 return 0;
679}
680
Stephen Hemmingerf6908082007-03-12 14:34:29 -0700681static const struct seq_operations wireless_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 .start = dev_seq_start,
683 .next = dev_seq_next,
684 .stop = dev_seq_stop,
685 .show = wireless_seq_show,
686};
687
688static int wireless_seq_open(struct inode *inode, struct file *file)
689{
690 return seq_open(file, &wireless_seq_ops);
691}
692
Arjan van de Ven9a321442007-02-12 00:55:35 -0800693static const struct file_operations wireless_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .owner = THIS_MODULE,
695 .open = wireless_seq_open,
696 .read = seq_read,
697 .llseek = seq_lseek,
698 .release = seq_release,
699};
700
701int __init wireless_proc_init(void)
702{
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700703 /* Create /proc/net/wireless entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops))
705 return -ENOMEM;
706
707 return 0;
708}
709#endif /* CONFIG_PROC_FS */
710
711/************************** IOCTL SUPPORT **************************/
712/*
713 * The original user space API to configure all those Wireless Extensions
714 * is through IOCTLs.
715 * In there, we check if we need to call the new driver API (iw_handler)
716 * or just call the driver ioctl handler.
717 */
718
719/* ---------------------------------------------------------------- */
720/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Wrapper to call a standard Wireless Extension handler.
722 * We do various checks and also take care of moving data between
723 * user space and kernel space.
724 */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800725static int ioctl_standard_call(struct net_device * dev,
726 struct ifreq * ifr,
727 unsigned int cmd,
728 iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct iwreq * iwr = (struct iwreq *) ifr;
731 const struct iw_ioctl_description * descr;
732 struct iw_request_info info;
733 int ret = -EINVAL;
734
735 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700736 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EOPNOTSUPP;
738 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
739
740#ifdef WE_IOCTL_DEBUG
741 printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
742 ifr->ifr_name, cmd);
743 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);
744#endif /* WE_IOCTL_DEBUG */
745
746 /* Prepare the call */
747 info.cmd = cmd;
748 info.flags = 0;
749
750 /* Check if we have a pointer to user space data or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700751 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* No extra arguments. Trivial to handle */
754 ret = handler(dev, &info, &(iwr->u), NULL);
755
756#ifdef WE_SET_EVENT
757 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700758 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ((ret == 0) || (ret == -EIWCOMMIT)))
760 wireless_send_event(dev, cmd, &(iwr->u), NULL);
761#endif /* WE_SET_EVENT */
762 } else {
763 char * extra;
764 int extra_size;
765 int user_length = 0;
766 int err;
John W. Linville98978ed2006-10-17 10:54:36 -0400767 int essid_compat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* Calculate space needed by arguments. Always allocate
770 * for max space. Easier, and won't last long... */
771 extra_size = descr->max_tokens * descr->token_size;
772
John W. Linville98978ed2006-10-17 10:54:36 -0400773 /* Check need for ESSID compatibility for WE < 21 */
774 switch (cmd) {
775 case SIOCSIWESSID:
776 case SIOCGIWESSID:
777 case SIOCSIWNICKN:
778 case SIOCGIWNICKN:
779 if (iwr->u.data.length == descr->max_tokens + 1)
780 essid_compat = 1;
781 else if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
782 char essid[IW_ESSID_MAX_SIZE + 1];
783
784 err = copy_from_user(essid, iwr->u.data.pointer,
785 iwr->u.data.length *
786 descr->token_size);
787 if (err)
788 return -EFAULT;
789
790 if (essid[iwr->u.data.length - 1] == '\0')
791 essid_compat = 1;
792 }
793 break;
794 default:
795 break;
796 }
797
798 iwr->u.data.length -= essid_compat;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700801 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700803 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 (iwr->u.data.length != 0))
805 return -EFAULT;
806 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700807 if (iwr->u.data.length > descr->max_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700809 if (iwr->u.data.length < descr->min_tokens)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EINVAL;
811 } else {
812 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700813 if (iwr->u.data.pointer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EFAULT;
815 /* Save user space buffer size for checking */
816 user_length = iwr->u.data.length;
817
818 /* Don't check if user_length > max to allow forward
819 * compatibility. The test user_length < min is
820 * implied by the test at the end. */
821
822 /* Support for very large requests */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700823 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 (user_length > descr->max_tokens)) {
825 /* Allow userspace to GET more than max so
826 * we can support any size GET requests.
827 * There is still a limit : -ENOMEM. */
828 extra_size = user_length * descr->token_size;
829 /* Note : user_length is originally a __u16,
830 * and token_size is controlled by us,
831 * so extra_size won't get negative and
832 * won't overflow... */
833 }
834 }
835
836#ifdef WE_IOCTL_DEBUG
837 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
838 dev->name, extra_size);
839#endif /* WE_IOCTL_DEBUG */
840
841 /* Create the kernel buffer */
John W. Linville98978ed2006-10-17 10:54:36 -0400842 /* kzalloc ensures NULL-termination for essid_compat */
843 extra = kzalloc(extra_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (extra == NULL) {
845 return -ENOMEM;
846 }
847
848 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700849 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 err = copy_from_user(extra, iwr->u.data.pointer,
851 iwr->u.data.length *
852 descr->token_size);
853 if (err) {
854 kfree(extra);
855 return -EFAULT;
856 }
857#ifdef WE_IOCTL_DEBUG
858 printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
859 dev->name,
860 iwr->u.data.length * descr->token_size);
861#endif /* WE_IOCTL_DEBUG */
862 }
863
864 /* Call the handler */
865 ret = handler(dev, &info, &(iwr->u), extra);
866
John W. Linville98978ed2006-10-17 10:54:36 -0400867 iwr->u.data.length += essid_compat;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 /* If we have something to return to the user */
870 if (!ret && IW_IS_GET(cmd)) {
871 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700872 if (user_length < iwr->u.data.length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 kfree(extra);
874 return -E2BIG;
875 }
876
877 err = copy_to_user(iwr->u.data.pointer, extra,
878 iwr->u.data.length *
879 descr->token_size);
880 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900881 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#ifdef WE_IOCTL_DEBUG
883 printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
884 dev->name,
885 iwr->u.data.length * descr->token_size);
886#endif /* WE_IOCTL_DEBUG */
887 }
888
889#ifdef WE_SET_EVENT
890 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700891 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ((ret == 0) || (ret == -EIWCOMMIT))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700893 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* If the event is restricted, don't
895 * export the payload */
896 wireless_send_event(dev, cmd, &(iwr->u), NULL);
897 else
898 wireless_send_event(dev, cmd, &(iwr->u),
899 extra);
900 }
901#endif /* WE_SET_EVENT */
902
903 /* Cleanup - I told you it wasn't that long ;-) */
904 kfree(extra);
905 }
906
907 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700908 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ret = call_commit_handler(dev);
910
911 /* Here, we will generate the appropriate event if needed */
912
913 return ret;
914}
915
916/* ---------------------------------------------------------------- */
917/*
918 * Wrapper to call a private Wireless Extension handler.
919 * We do various checks and also take care of moving data between
920 * user space and kernel space.
921 * It's not as nice and slimline as the standard wrapper. The cause
922 * is struct iw_priv_args, which was not really designed for the
923 * job we are going here.
924 *
925 * IMPORTANT : This function prevent to set and get data on the same
926 * IOCTL and enforce the SET/GET convention. Not doing it would be
927 * far too hairy...
928 * If you need to set and get data at the same time, please don't use
929 * a iw_handler but process it in your ioctl handler (i.e. use the
930 * old driver API).
931 */
932static inline int ioctl_private_call(struct net_device * dev,
933 struct ifreq * ifr,
934 unsigned int cmd,
935 iw_handler handler)
936{
937 struct iwreq * iwr = (struct iwreq *) ifr;
938 const struct iw_priv_args * descr = NULL;
939 struct iw_request_info info;
940 int extra_size = 0;
941 int i;
942 int ret = -EINVAL;
943
944 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700945 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
946 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 descr = &(dev->wireless_handlers->private_args[i]);
948 break;
949 }
950
951#ifdef WE_IOCTL_DEBUG
952 printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
953 ifr->ifr_name, cmd);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700954 if (descr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
956 dev->name, descr->name,
957 descr->set_args, descr->get_args);
958 }
959#endif /* WE_IOCTL_DEBUG */
960
961 /* Compute the size of the set/get arguments */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700962 if (descr != NULL) {
963 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int offset = 0; /* For sub-ioctls */
965 /* Check for sub-ioctl handler */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700966 if (descr->name[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* Reserve one int for sub-ioctl index */
968 offset = sizeof(__u32);
969
970 /* Size of set arguments */
971 extra_size = get_priv_size(descr->set_args);
972
973 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700974 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ((extra_size + offset) <= IFNAMSIZ))
976 extra_size = 0;
977 } else {
978 /* Size of get arguments */
979 extra_size = get_priv_size(descr->get_args);
980
981 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700982 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 (extra_size <= IFNAMSIZ))
984 extra_size = 0;
985 }
986 }
987
988 /* Prepare the call */
989 info.cmd = cmd;
990 info.flags = 0;
991
992 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700993 if (extra_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* No extra arguments. Trivial to handle */
995 ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
996 } else {
997 char * extra;
998 int err;
999
1000 /* Check what user space is giving us */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001001 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001003 if ((iwr->u.data.pointer == NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 (iwr->u.data.length != 0))
1005 return -EFAULT;
1006
1007 /* Does it fits within bounds ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001008 if (iwr->u.data.length > (descr->set_args &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 IW_PRIV_SIZE_MASK))
1010 return -E2BIG;
1011 } else {
1012 /* Check NULL pointer */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001013 if (iwr->u.data.pointer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EFAULT;
1015 }
1016
1017#ifdef WE_IOCTL_DEBUG
1018 printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
1019 dev->name, extra_size);
1020#endif /* WE_IOCTL_DEBUG */
1021
1022 /* Always allocate for max space. Easier, and won't last
1023 * long... */
1024 extra = kmalloc(extra_size, GFP_KERNEL);
1025 if (extra == NULL) {
1026 return -ENOMEM;
1027 }
1028
1029 /* If it is a SET, get all the extra data in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001030 if (IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 err = copy_from_user(extra, iwr->u.data.pointer,
1032 extra_size);
1033 if (err) {
1034 kfree(extra);
1035 return -EFAULT;
1036 }
1037#ifdef WE_IOCTL_DEBUG
1038 printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
1039 dev->name, iwr->u.data.length);
1040#endif /* WE_IOCTL_DEBUG */
1041 }
1042
1043 /* Call the handler */
1044 ret = handler(dev, &info, &(iwr->u), extra);
1045
1046 /* If we have something to return to the user */
1047 if (!ret && IW_IS_GET(cmd)) {
1048
1049 /* Adjust for the actual length if it's variable,
1050 * avoid leaking kernel bits outside. */
1051 if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) {
1052 extra_size = adjust_priv_size(descr->get_args,
1053 &(iwr->u));
1054 }
1055
1056 err = copy_to_user(iwr->u.data.pointer, extra,
1057 extra_size);
1058 if (err)
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001059 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#ifdef WE_IOCTL_DEBUG
1061 printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
1062 dev->name, iwr->u.data.length);
1063#endif /* WE_IOCTL_DEBUG */
1064 }
1065
1066 /* Cleanup - I told you it wasn't that long ;-) */
1067 kfree(extra);
1068 }
1069
1070
1071 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001072 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ret = call_commit_handler(dev);
1074
1075 return ret;
1076}
1077
1078/* ---------------------------------------------------------------- */
1079/*
1080 * Main IOCTl dispatcher. Called from the main networking code
1081 * (dev_ioctl() in net/core/dev.c).
1082 * Check the type of IOCTL and call the appropriate wrapper...
1083 */
1084int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
1085{
1086 struct net_device *dev;
1087 iw_handler handler;
1088
1089 /* Permissions are already checked in dev_ioctl() before calling us.
1090 * The copy_to/from_user() of ifr is also dealt with in there */
1091
1092 /* Make sure the device exist */
1093 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
1094 return -ENODEV;
1095
1096 /* A bunch of special cases, then the generic case...
1097 * Note that 'cmd' is already filtered in dev_ioctl() with
1098 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001099 switch (cmd) {
1100 case SIOCGIWSTATS:
1101 /* Get Wireless Stats */
1102 return ioctl_standard_call(dev,
1103 ifr,
1104 cmd,
1105 &iw_handler_get_iwstats);
1106
1107 case SIOCGIWPRIV:
1108 /* Check if we have some wireless handlers defined */
1109 if (dev->wireless_handlers != NULL) {
1110 /* We export to user space the definition of
1111 * the private handler ourselves */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001112 return ioctl_standard_call(dev,
1113 ifr,
1114 cmd,
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001115 &iw_handler_get_private);
1116 }
1117 // ## Fall-through for old API ##
1118 default:
1119 /* Generic IOCTL */
1120 /* Basic check */
1121 if (!netif_device_present(dev))
1122 return -ENODEV;
1123 /* New driver API : try to find the handler */
1124 handler = get_handler(dev, cmd);
1125 if (handler != NULL) {
1126 /* Standard and private are not the same */
1127 if (cmd < SIOCIWFIRSTPRIV)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001128 return ioctl_standard_call(dev,
1129 ifr,
1130 cmd,
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001131 handler);
1132 else
1133 return ioctl_private_call(dev,
1134 ifr,
1135 cmd,
1136 handler);
1137 }
1138 /* Old driver API : call driver ioctl handler */
1139 if (dev->do_ioctl) {
1140 return dev->do_ioctl(dev, ifr, cmd);
1141 }
1142 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 /* Not reached */
1145 return -EINVAL;
1146}
1147
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001148/********************** RTNETLINK REQUEST API **********************/
1149/*
1150 * The alternate user space API to configure all those Wireless Extensions
1151 * is through RtNetlink.
1152 * This API support only the new driver API (iw_handler).
1153 *
1154 * This RtNetlink API use the same query/reply model as the ioctl API.
1155 * Maximum effort has been done to fit in the RtNetlink model, and
1156 * we support both RtNetlink Set and RtNelink Get operations.
1157 * On the other hand, we don't offer Dump operations because of the
1158 * following reasons :
1159 * o Large number of parameters, most optional
1160 * o Large size of some parameters (> 100 bytes)
1161 * o Each parameters need to be extracted from hardware
1162 * o Scan requests can take seconds and disable network activity.
1163 * Because of this high cost/overhead, we want to return only the
1164 * parameters the user application is really interested in.
1165 * We could offer partial Dump using the IW_DESCR_FLAG_DUMP flag.
1166 *
1167 * The API uses the standard RtNetlink socket. When the RtNetlink code
1168 * find a IFLA_WIRELESS field in a RtNetlink SET_LINK request,
1169 * it calls here.
1170 */
1171
1172#ifdef CONFIG_NET_WIRELESS_RTNETLINK
1173/* ---------------------------------------------------------------- */
1174/*
1175 * Wrapper to call a standard Wireless Extension GET handler.
1176 * We do various checks and call the handler with the proper args.
1177 */
1178static int rtnetlink_standard_get(struct net_device * dev,
1179 struct iw_event * request,
1180 int request_len,
1181 iw_handler handler,
1182 char ** p_buf,
1183 int * p_len)
1184{
1185 const struct iw_ioctl_description * descr = NULL;
1186 unsigned int cmd;
1187 union iwreq_data * wrqu;
1188 int hdr_len;
1189 struct iw_request_info info;
1190 char * buffer = NULL;
1191 int buffer_size = 0;
1192 int ret = -EINVAL;
1193
1194 /* Get the description of the Request */
1195 cmd = request->cmd;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001196 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001197 return -EOPNOTSUPP;
1198 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
1199
1200#ifdef WE_RTNETLINK_DEBUG
1201 printk(KERN_DEBUG "%s (WE.r) : Found standard handler for 0x%04X\n",
1202 dev->name, cmd);
1203 printk(KERN_DEBUG "%s (WE.r) : 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);
1204#endif /* WE_RTNETLINK_DEBUG */
1205
1206 /* Check if wrqu is complete */
1207 hdr_len = event_type_size[descr->header_type];
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001208 if (request_len < hdr_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001209#ifdef WE_RTNETLINK_DEBUG
1210 printk(KERN_DEBUG
1211 "%s (WE.r) : Wireless request too short (%d)\n",
1212 dev->name, request_len);
1213#endif /* WE_RTNETLINK_DEBUG */
1214 return -EINVAL;
1215 }
1216
1217 /* Prepare the call */
1218 info.cmd = cmd;
1219 info.flags = 0;
1220
1221 /* Check if we have extra data in the reply or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001222 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001223
1224 /* Create the kernel buffer that we will return.
1225 * It's at an offset to match the TYPE_POINT case... */
1226 buffer_size = request_len + IW_EV_POINT_OFF;
1227 buffer = kmalloc(buffer_size, GFP_KERNEL);
1228 if (buffer == NULL) {
1229 return -ENOMEM;
1230 }
1231 /* Copy event data */
1232 memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
1233 /* Use our own copy of wrqu */
1234 wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001235 + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001236
1237 /* No extra arguments. Trivial to handle */
1238 ret = handler(dev, &info, wrqu, NULL);
1239
1240 } else {
1241 union iwreq_data wrqu_point;
1242 char * extra = NULL;
1243 int extra_size = 0;
1244
1245 /* Get a temp copy of wrqu (skip pointer) */
1246 memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001247 ((char *) request) + IW_EV_LCP_PK_LEN,
1248 IW_EV_POINT_LEN - IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001249
1250 /* Calculate space needed by arguments. Always allocate
1251 * for max space. Easier, and won't last long... */
1252 extra_size = descr->max_tokens * descr->token_size;
1253 /* Support for very large requests */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001254 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001255 (wrqu_point.data.length > descr->max_tokens))
1256 extra_size = (wrqu_point.data.length
1257 * descr->token_size);
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001258 buffer_size = extra_size + IW_EV_POINT_PK_LEN + IW_EV_POINT_OFF;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001259#ifdef WE_RTNETLINK_DEBUG
1260 printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
1261 dev->name, extra_size, buffer_size);
1262#endif /* WE_RTNETLINK_DEBUG */
1263
1264 /* Create the kernel buffer that we will return */
1265 buffer = kmalloc(buffer_size, GFP_KERNEL);
1266 if (buffer == NULL) {
1267 return -ENOMEM;
1268 }
1269
1270 /* Put wrqu in the right place (just before extra).
1271 * Leave space for IWE header and dummy pointer...
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001272 * Note that IW_EV_LCP_PK_LEN==4 bytes, so it's still aligned.
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001273 */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001274 memcpy(buffer + IW_EV_LCP_PK_LEN + IW_EV_POINT_OFF,
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001275 ((char *) &wrqu_point) + IW_EV_POINT_OFF,
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001276 IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1277 wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001278
1279 /* Extra comes logically after that. Offset +12 bytes. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001280 extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001281
1282 /* Call the handler */
1283 ret = handler(dev, &info, wrqu, extra);
1284
1285 /* Calculate real returned length */
1286 extra_size = (wrqu->data.length * descr->token_size);
1287 /* Re-adjust reply size */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001288 request->len = extra_size + IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001289
1290 /* Put the iwe header where it should, i.e. scrap the
1291 * dummy pointer. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001292 memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001293
1294#ifdef WE_RTNETLINK_DEBUG
1295 printk(KERN_DEBUG "%s (WE.r) : Reply 0x%04X, hdr_len %d, tokens %d, extra_size %d, buffer_size %d\n", dev->name, cmd, hdr_len, wrqu->data.length, extra_size, buffer_size);
1296#endif /* WE_RTNETLINK_DEBUG */
1297
1298 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001299 if (wrqu_point.data.length < wrqu->data.length)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001300 ret = -E2BIG;
1301 }
1302
1303 /* Return the buffer to the caller */
1304 if (!ret) {
1305 *p_buf = buffer;
1306 *p_len = request->len;
1307 } else {
1308 /* Cleanup */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001309 if (buffer)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001310 kfree(buffer);
1311 }
1312
1313 return ret;
1314}
1315
1316/* ---------------------------------------------------------------- */
1317/*
1318 * Wrapper to call a standard Wireless Extension SET handler.
1319 * We do various checks and call the handler with the proper args.
1320 */
1321static inline int rtnetlink_standard_set(struct net_device * dev,
1322 struct iw_event * request,
1323 int request_len,
1324 iw_handler handler)
1325{
1326 const struct iw_ioctl_description * descr = NULL;
1327 unsigned int cmd;
1328 union iwreq_data * wrqu;
1329 union iwreq_data wrqu_point;
1330 int hdr_len;
1331 char * extra = NULL;
1332 int extra_size = 0;
1333 struct iw_request_info info;
1334 int ret = -EINVAL;
1335
1336 /* Get the description of the Request */
1337 cmd = request->cmd;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001338 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001339 return -EOPNOTSUPP;
1340 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
1341
1342#ifdef WE_RTNETLINK_DEBUG
1343 printk(KERN_DEBUG "%s (WE.r) : Found standard SET handler for 0x%04X\n",
1344 dev->name, cmd);
1345 printk(KERN_DEBUG "%s (WE.r) : 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);
1346#endif /* WE_RTNETLINK_DEBUG */
1347
1348 /* Extract fixed header from request. This is properly aligned. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001349 wrqu = (union iwreq_data *) (((char *) request) + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001350
1351 /* Check if wrqu is complete */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001352 hdr_len = event_type_pk_size[descr->header_type];
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001353 if (request_len < hdr_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001354#ifdef WE_RTNETLINK_DEBUG
1355 printk(KERN_DEBUG
1356 "%s (WE.r) : Wireless request too short (%d)\n",
1357 dev->name, request_len);
1358#endif /* WE_RTNETLINK_DEBUG */
1359 return -EINVAL;
1360 }
1361
1362 /* Prepare the call */
1363 info.cmd = cmd;
1364 info.flags = 0;
1365
1366 /* Check if we have extra data in the request or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001367 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001368
1369 /* No extra arguments. Trivial to handle */
1370 ret = handler(dev, &info, wrqu, NULL);
1371
1372 } else {
1373 int extra_len;
1374
1375 /* Put wrqu in the right place (skip pointer) */
1376 memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001377 wrqu, IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001378 /* Don't forget about the event code... */
1379 wrqu = &wrqu_point;
1380
1381 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001382 if (wrqu_point.data.length > descr->max_tokens)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001383 return -E2BIG;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001384 if (wrqu_point.data.length < descr->min_tokens)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001385 return -EINVAL;
1386
1387 /* Real length of payload */
1388 extra_len = wrqu_point.data.length * descr->token_size;
1389
1390 /* Check if request is self consistent */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001391 if ((request_len - hdr_len) < extra_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001392#ifdef WE_RTNETLINK_DEBUG
1393 printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
1394 dev->name, extra_size);
1395#endif /* WE_RTNETLINK_DEBUG */
1396 return -EINVAL;
1397 }
1398
1399#ifdef WE_RTNETLINK_DEBUG
1400 printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
1401 dev->name, extra_size);
1402#endif /* WE_RTNETLINK_DEBUG */
1403
1404 /* Always allocate for max space. Easier, and won't last
1405 * long... */
1406 extra_size = descr->max_tokens * descr->token_size;
1407 extra = kmalloc(extra_size, GFP_KERNEL);
1408 if (extra == NULL)
1409 return -ENOMEM;
1410
1411 /* Copy extra in aligned buffer */
1412 memcpy(extra, ((char *) request) + hdr_len, extra_len);
1413
1414 /* Call the handler */
1415 ret = handler(dev, &info, &wrqu_point, extra);
1416 }
1417
1418#ifdef WE_SET_EVENT
1419 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001420 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001421 ((ret == 0) || (ret == -EIWCOMMIT))) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001422 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001423 /* If the event is restricted, don't
1424 * export the payload */
1425 wireless_send_event(dev, cmd, wrqu, NULL);
1426 else
1427 wireless_send_event(dev, cmd, wrqu, extra);
1428 }
1429#endif /* WE_SET_EVENT */
1430
1431 /* Cleanup - I told you it wasn't that long ;-) */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001432 if (extra)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001433 kfree(extra);
1434
1435 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001436 if (ret == -EIWCOMMIT)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001437 ret = call_commit_handler(dev);
1438
1439 return ret;
1440}
1441
1442/* ---------------------------------------------------------------- */
1443/*
1444 * Wrapper to call a private Wireless Extension GET handler.
1445 * Same as above...
1446 * It's not as nice and slimline as the standard wrapper. The cause
1447 * is struct iw_priv_args, which was not really designed for the
1448 * job we are going here.
1449 *
1450 * IMPORTANT : This function prevent to set and get data on the same
1451 * IOCTL and enforce the SET/GET convention. Not doing it would be
1452 * far too hairy...
1453 * If you need to set and get data at the same time, please don't use
1454 * a iw_handler but process it in your ioctl handler (i.e. use the
1455 * old driver API).
1456 */
1457static inline int rtnetlink_private_get(struct net_device * dev,
1458 struct iw_event * request,
1459 int request_len,
1460 iw_handler handler,
1461 char ** p_buf,
1462 int * p_len)
1463{
1464 const struct iw_priv_args * descr = NULL;
1465 unsigned int cmd;
1466 union iwreq_data * wrqu;
1467 int hdr_len;
1468 struct iw_request_info info;
1469 int extra_size = 0;
1470 int i;
1471 char * buffer = NULL;
1472 int buffer_size = 0;
1473 int ret = -EINVAL;
1474
1475 /* Get the description of the Request */
1476 cmd = request->cmd;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001477 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
1478 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001479 descr = &(dev->wireless_handlers->private_args[i]);
1480 break;
1481 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001482 if (descr == NULL)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001483 return -EOPNOTSUPP;
1484
1485#ifdef WE_RTNETLINK_DEBUG
1486 printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
1487 dev->name, cmd);
1488 printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
1489 dev->name, descr->name, descr->set_args, descr->get_args);
1490#endif /* WE_RTNETLINK_DEBUG */
1491
1492 /* Compute the max size of the get arguments */
1493 extra_size = get_priv_size(descr->get_args);
1494
1495 /* Does it fits in wrqu ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001496 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001497 (extra_size <= IFNAMSIZ)) {
1498 hdr_len = extra_size;
1499 extra_size = 0;
1500 } else {
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001501 hdr_len = IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001502 }
1503
1504 /* Check if wrqu is complete */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001505 if (request_len < hdr_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001506#ifdef WE_RTNETLINK_DEBUG
1507 printk(KERN_DEBUG
1508 "%s (WE.r) : Wireless request too short (%d)\n",
1509 dev->name, request_len);
1510#endif /* WE_RTNETLINK_DEBUG */
1511 return -EINVAL;
1512 }
1513
1514 /* Prepare the call */
1515 info.cmd = cmd;
1516 info.flags = 0;
1517
1518 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001519 if (extra_size == 0) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001520
1521 /* Create the kernel buffer that we will return.
1522 * It's at an offset to match the TYPE_POINT case... */
1523 buffer_size = request_len + IW_EV_POINT_OFF;
1524 buffer = kmalloc(buffer_size, GFP_KERNEL);
1525 if (buffer == NULL) {
1526 return -ENOMEM;
1527 }
1528 /* Copy event data */
1529 memcpy(buffer + IW_EV_POINT_OFF, request, request_len);
1530 /* Use our own copy of wrqu */
1531 wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001532 + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001533
1534 /* No extra arguments. Trivial to handle */
1535 ret = handler(dev, &info, wrqu, (char *) wrqu);
1536
1537 } else {
1538 char * extra;
1539
1540 /* Buffer for full reply */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001541 buffer_size = extra_size + IW_EV_POINT_PK_LEN + IW_EV_POINT_OFF;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001542
1543#ifdef WE_RTNETLINK_DEBUG
1544 printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n",
1545 dev->name, extra_size, buffer_size);
1546#endif /* WE_RTNETLINK_DEBUG */
1547
1548 /* Create the kernel buffer that we will return */
1549 buffer = kmalloc(buffer_size, GFP_KERNEL);
1550 if (buffer == NULL) {
1551 return -ENOMEM;
1552 }
1553
1554 /* Put wrqu in the right place (just before extra).
1555 * Leave space for IWE header and dummy pointer...
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001556 * Note that IW_EV_LCP_PK_LEN==4 bytes, so it's still aligned.
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001557 */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001558 memcpy(buffer + IW_EV_LCP_PK_LEN + IW_EV_POINT_OFF,
1559 ((char *) request) + IW_EV_LCP_PK_LEN,
1560 IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1561 wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001562
1563 /* Extra comes logically after that. Offset +12 bytes. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001564 extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001565
1566 /* Call the handler */
1567 ret = handler(dev, &info, wrqu, extra);
1568
1569 /* Adjust for the actual length if it's variable,
1570 * avoid leaking kernel bits outside. */
1571 if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
1572 extra_size = adjust_priv_size(descr->get_args, wrqu);
1573 /* Re-adjust reply size */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001574 request->len = extra_size + IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001575
1576 /* Put the iwe header where it should, i.e. scrap the
1577 * dummy pointer. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001578 memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001579
1580#ifdef WE_RTNETLINK_DEBUG
1581 printk(KERN_DEBUG "%s (WE.r) : Reply 0x%04X, hdr_len %d, tokens %d, extra_size %d, buffer_size %d\n", dev->name, cmd, hdr_len, wrqu->data.length, extra_size, buffer_size);
1582#endif /* WE_RTNETLINK_DEBUG */
1583 }
1584
1585 /* Return the buffer to the caller */
1586 if (!ret) {
1587 *p_buf = buffer;
1588 *p_len = request->len;
1589 } else {
1590 /* Cleanup */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001591 if (buffer)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001592 kfree(buffer);
1593 }
1594
1595 return ret;
1596}
1597
1598/* ---------------------------------------------------------------- */
1599/*
1600 * Wrapper to call a private Wireless Extension SET handler.
1601 * Same as above...
1602 * It's not as nice and slimline as the standard wrapper. The cause
1603 * is struct iw_priv_args, which was not really designed for the
1604 * job we are going here.
1605 *
1606 * IMPORTANT : This function prevent to set and get data on the same
1607 * IOCTL and enforce the SET/GET convention. Not doing it would be
1608 * far too hairy...
1609 * If you need to set and get data at the same time, please don't use
1610 * a iw_handler but process it in your ioctl handler (i.e. use the
1611 * old driver API).
1612 */
1613static inline int rtnetlink_private_set(struct net_device * dev,
1614 struct iw_event * request,
1615 int request_len,
1616 iw_handler handler)
1617{
1618 const struct iw_priv_args * descr = NULL;
1619 unsigned int cmd;
1620 union iwreq_data * wrqu;
1621 union iwreq_data wrqu_point;
1622 int hdr_len;
1623 char * extra = NULL;
1624 int extra_size = 0;
1625 int offset = 0; /* For sub-ioctls */
1626 struct iw_request_info info;
1627 int i;
1628 int ret = -EINVAL;
1629
1630 /* Get the description of the Request */
1631 cmd = request->cmd;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001632 for (i = 0; i < dev->wireless_handlers->num_private_args; i++)
1633 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001634 descr = &(dev->wireless_handlers->private_args[i]);
1635 break;
1636 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001637 if (descr == NULL)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001638 return -EOPNOTSUPP;
1639
1640#ifdef WE_RTNETLINK_DEBUG
1641 printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n",
1642 ifr->ifr_name, cmd);
1643 printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n",
1644 dev->name, descr->name, descr->set_args, descr->get_args);
1645#endif /* WE_RTNETLINK_DEBUG */
1646
1647 /* Compute the size of the set arguments */
1648 /* Check for sub-ioctl handler */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001649 if (descr->name[0] == '\0')
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001650 /* Reserve one int for sub-ioctl index */
1651 offset = sizeof(__u32);
1652
1653 /* Size of set arguments */
1654 extra_size = get_priv_size(descr->set_args);
1655
1656 /* Does it fits in wrqu ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001657 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001658 (extra_size <= IFNAMSIZ)) {
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001659 hdr_len = IW_EV_LCP_PK_LEN + extra_size;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001660 extra_size = 0;
1661 } else {
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001662 hdr_len = IW_EV_POINT_PK_LEN;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001663 }
1664
1665 /* Extract fixed header from request. This is properly aligned. */
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001666 wrqu = (union iwreq_data *) (((char *) request) + IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001667
1668 /* Check if wrqu is complete */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001669 if (request_len < hdr_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001670#ifdef WE_RTNETLINK_DEBUG
1671 printk(KERN_DEBUG
1672 "%s (WE.r) : Wireless request too short (%d)\n",
1673 dev->name, request_len);
1674#endif /* WE_RTNETLINK_DEBUG */
1675 return -EINVAL;
1676 }
1677
1678 /* Prepare the call */
1679 info.cmd = cmd;
1680 info.flags = 0;
1681
1682 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001683 if (extra_size == 0) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001684
1685 /* No extra arguments. Trivial to handle */
1686 ret = handler(dev, &info, wrqu, (char *) wrqu);
1687
1688 } else {
1689 int extra_len;
1690
1691 /* Put wrqu in the right place (skip pointer) */
1692 memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF,
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +00001693 wrqu, IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001694
1695 /* Does it fits within bounds ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001696 if (wrqu_point.data.length > (descr->set_args &
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001697 IW_PRIV_SIZE_MASK))
1698 return -E2BIG;
1699
1700 /* Real length of payload */
1701 extra_len = adjust_priv_size(descr->set_args, &wrqu_point);
1702
1703 /* Check if request is self consistent */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001704 if ((request_len - hdr_len) < extra_len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001705#ifdef WE_RTNETLINK_DEBUG
1706 printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n",
1707 dev->name, extra_size);
1708#endif /* WE_RTNETLINK_DEBUG */
1709 return -EINVAL;
1710 }
1711
1712#ifdef WE_RTNETLINK_DEBUG
1713 printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n",
1714 dev->name, extra_size);
1715#endif /* WE_RTNETLINK_DEBUG */
1716
1717 /* Always allocate for max space. Easier, and won't last
1718 * long... */
1719 extra = kmalloc(extra_size, GFP_KERNEL);
1720 if (extra == NULL)
1721 return -ENOMEM;
1722
1723 /* Copy extra in aligned buffer */
1724 memcpy(extra, ((char *) request) + hdr_len, extra_len);
1725
1726 /* Call the handler */
1727 ret = handler(dev, &info, &wrqu_point, extra);
1728
1729 /* Cleanup - I told you it wasn't that long ;-) */
1730 kfree(extra);
1731 }
1732
1733 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001734 if (ret == -EIWCOMMIT)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001735 ret = call_commit_handler(dev);
1736
1737 return ret;
1738}
1739
1740/* ---------------------------------------------------------------- */
1741/*
1742 * Main RtNetlink dispatcher. Called from the main networking code
1743 * (do_getlink() in net/core/rtnetlink.c).
1744 * Check the type of Request and call the appropriate wrapper...
1745 */
1746int wireless_rtnetlink_get(struct net_device * dev,
1747 char * data,
1748 int len,
1749 char ** p_buf,
1750 int * p_len)
1751{
1752 struct iw_event * request = (struct iw_event *) data;
1753 iw_handler handler;
1754
1755 /* Check length */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001756 if (len < IW_EV_LCP_PK_LEN) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001757 printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
1758 dev->name, len);
1759 return -EINVAL;
1760 }
1761
1762 /* ReCheck length (len may have padding) */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001763 if (request->len > len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001764 printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
1765 dev->name, request->len, len);
1766 return -EINVAL;
1767 }
1768
1769 /* Only accept GET requests in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001770 if (!IW_IS_GET(request->cmd))
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001771 return -EOPNOTSUPP;
1772
Jean Tourrilhes848ef852006-04-14 10:47:26 -07001773 /* If command is `get the encoding parameters', check if
1774 * the user has the right to do it */
1775 if (request->cmd == SIOCGIWENCODE ||
1776 request->cmd == SIOCGIWENCODEEXT) {
1777 if (!capable(CAP_NET_ADMIN))
1778 return -EPERM;
1779 }
1780
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001781 /* Special cases */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001782 if (request->cmd == SIOCGIWSTATS)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001783 /* Get Wireless Stats */
1784 return rtnetlink_standard_get(dev,
1785 request,
1786 request->len,
1787 &iw_handler_get_iwstats,
1788 p_buf, p_len);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001789 if (request->cmd == SIOCGIWPRIV) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001790 /* Check if we have some wireless handlers defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001791 if (dev->wireless_handlers == NULL)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001792 return -EOPNOTSUPP;
1793 /* Get Wireless Stats */
1794 return rtnetlink_standard_get(dev,
1795 request,
1796 request->len,
1797 &iw_handler_get_private,
1798 p_buf, p_len);
1799 }
1800
1801 /* Basic check */
1802 if (!netif_device_present(dev))
1803 return -ENODEV;
1804
1805 /* Try to find the handler */
1806 handler = get_handler(dev, request->cmd);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001807 if (handler != NULL) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001808 /* Standard and private are not the same */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001809 if (request->cmd < SIOCIWFIRSTPRIV)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001810 return rtnetlink_standard_get(dev,
1811 request,
1812 request->len,
1813 handler,
1814 p_buf, p_len);
1815 else
1816 return rtnetlink_private_get(dev,
1817 request,
1818 request->len,
1819 handler,
1820 p_buf, p_len);
1821 }
1822
1823 return -EOPNOTSUPP;
1824}
1825
1826/* ---------------------------------------------------------------- */
1827/*
1828 * Main RtNetlink dispatcher. Called from the main networking code
1829 * (do_setlink() in net/core/rtnetlink.c).
1830 * Check the type of Request and call the appropriate wrapper...
1831 */
1832int wireless_rtnetlink_set(struct net_device * dev,
1833 char * data,
1834 int len)
1835{
1836 struct iw_event * request = (struct iw_event *) data;
1837 iw_handler handler;
1838
1839 /* Check length */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001840 if (len < IW_EV_LCP_PK_LEN) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001841 printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n",
1842 dev->name, len);
1843 return -EINVAL;
1844 }
1845
1846 /* ReCheck length (len may have padding) */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001847 if (request->len > len) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001848 printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n",
1849 dev->name, request->len, len);
1850 return -EINVAL;
1851 }
1852
1853 /* Only accept SET requests in here */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001854 if (!IW_IS_SET(request->cmd))
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001855 return -EOPNOTSUPP;
1856
1857 /* Basic check */
1858 if (!netif_device_present(dev))
1859 return -ENODEV;
1860
1861 /* New driver API : try to find the handler */
1862 handler = get_handler(dev, request->cmd);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001863 if (handler != NULL) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001864 /* Standard and private are not the same */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001865 if (request->cmd < SIOCIWFIRSTPRIV)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001866 return rtnetlink_standard_set(dev,
1867 request,
1868 request->len,
1869 handler);
1870 else
1871 return rtnetlink_private_set(dev,
1872 request,
1873 request->len,
1874 handler);
1875 }
1876
1877 return -EOPNOTSUPP;
1878}
1879#endif /* CONFIG_NET_WIRELESS_RTNETLINK */
1880
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/************************* EVENT PROCESSING *************************/
1883/*
1884 * Process events generated by the wireless layer or the driver.
1885 * Most often, the event will be propagated through rtnetlink
1886 */
1887
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001888#ifdef WE_EVENT_RTNETLINK
John W. Linvillebaef1862006-09-08 16:04:05 -04001889/* ---------------------------------------------------------------- */
1890/*
1891 * Locking...
1892 * ----------
1893 *
1894 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1895 * the locking issue in here and implementing this code !
1896 *
1897 * The issue : wireless_send_event() is often called in interrupt context,
1898 * while the Netlink layer can never be called in interrupt context.
1899 * The fully formed RtNetlink events are queued, and then a tasklet is run
1900 * to feed those to Netlink.
1901 * The skb_queue is interrupt safe, and its lock is not held while calling
1902 * Netlink, so there is no possibility of dealock.
1903 * Jean II
1904 */
1905
Herbert Xu782a6672006-08-03 23:54:41 +10001906static struct sk_buff_head wireless_nlevent_queue;
1907
John W. Linvillebaef1862006-09-08 16:04:05 -04001908static int __init wireless_nlevent_init(void)
1909{
1910 skb_queue_head_init(&wireless_nlevent_queue);
1911 return 0;
1912}
1913
1914subsys_initcall(wireless_nlevent_init);
1915
Herbert Xu782a6672006-08-03 23:54:41 +10001916static void wireless_nlevent_process(unsigned long data)
1917{
1918 struct sk_buff *skb;
1919
1920 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
Thomas Grafbd5785b2006-08-15 00:36:49 -07001921 rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
Herbert Xu782a6672006-08-03 23:54:41 +10001922}
1923
1924static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/* ---------------------------------------------------------------- */
1927/*
1928 * Fill a rtnetlink message with our event data.
1929 * Note that we propage only the specified event and don't dump the
1930 * current wireless config. Dumping the wireless config is far too
1931 * expensive (for each parameter, the driver need to query the hardware).
1932 */
1933static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb,
1934 struct net_device * dev,
1935 int type,
1936 char * event,
1937 int event_len)
1938{
1939 struct ifinfomsg *r;
1940 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001941 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
1944 r = NLMSG_DATA(nlh);
1945 r->ifi_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001946 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 r->ifi_type = dev->type;
1948 r->ifi_index = dev->ifindex;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001949 r->ifi_flags = dev_get_flags(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1951
1952 /* Add the wireless events in the netlink packet */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001953 RTA_PUT(skb, IFLA_WIRELESS, event_len, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001955 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return skb->len;
1957
1958nlmsg_failure:
1959rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001960 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -1;
1962}
1963
1964/* ---------------------------------------------------------------- */
1965/*
1966 * Create and broadcast and send it on the standard rtnetlink socket
1967 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1968 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1969 * within a RTM_NEWLINK event.
1970 */
1971static inline void rtmsg_iwinfo(struct net_device * dev,
1972 char * event,
1973 int event_len)
1974{
1975 struct sk_buff *skb;
1976 int size = NLMSG_GOODSIZE;
1977
1978 skb = alloc_skb(size, GFP_ATOMIC);
1979 if (!skb)
1980 return;
1981
1982 if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
1983 event, event_len) < 0) {
1984 kfree_skb(skb);
1985 return;
1986 }
Patrick McHardyac6d4392005-08-14 19:29:52 -07001987 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
Herbert Xu782a6672006-08-03 23:54:41 +10001988 skb_queue_tail(&wireless_nlevent_queue, skb);
1989 tasklet_schedule(&wireless_nlevent_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
Herbert Xu782a6672006-08-03 23:54:41 +10001991
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001992#endif /* WE_EVENT_RTNETLINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994/* ---------------------------------------------------------------- */
1995/*
1996 * Main event dispatcher. Called from other parts and drivers.
1997 * Send the event on the appropriate channels.
1998 * May be called from interrupt context.
1999 */
2000void wireless_send_event(struct net_device * dev,
2001 unsigned int cmd,
2002 union iwreq_data * wrqu,
2003 char * extra)
2004{
2005 const struct iw_ioctl_description * descr = NULL;
2006 int extra_len = 0;
2007 struct iw_event *event; /* Mallocated whole event */
2008 int event_len; /* Its size */
2009 int hdr_len; /* Size of the event header */
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002010 int wrqu_off = 0; /* Offset in wrqu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* Don't "optimise" the following variable, it will crash */
2012 unsigned cmd_index; /* *MUST* be unsigned */
2013
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002014 /* Get the description of the Event */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002015 if (cmd <= SIOCIWLAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 cmd_index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002017 if (cmd_index < standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 descr = &(standard_ioctl[cmd_index]);
2019 } else {
2020 cmd_index = cmd - IWEVFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002021 if (cmd_index < standard_event_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 descr = &(standard_event[cmd_index]);
2023 }
2024 /* Don't accept unknown events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002025 if (descr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 /* Note : we don't return an error to the driver, because
2027 * the driver would not know what to do about it. It can't
2028 * return an error to the user, because the event is not
2029 * initiated by a user request.
2030 * The best the driver could do is to log an error message.
2031 * We will do it ourselves instead...
2032 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002033 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 dev->name, cmd);
2035 return;
2036 }
2037#ifdef WE_EVENT_DEBUG
2038 printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
2039 dev->name, cmd);
2040 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);
2041#endif /* WE_EVENT_DEBUG */
2042
2043 /* Check extra parameters and set extra_len */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002044 if (descr->header_type == IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002046 if (wrqu->data.length > descr->max_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002047 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return;
2049 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002050 if (wrqu->data.length < descr->min_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002051 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return;
2053 }
2054 /* Calculate extra_len - extra is NULL for restricted events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002055 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 extra_len = wrqu->data.length * descr->token_size;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002057 /* Always at an offset in wrqu */
2058 wrqu_off = IW_EV_POINT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#ifdef WE_EVENT_DEBUG
2060 printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
2061#endif /* WE_EVENT_DEBUG */
2062 }
2063
2064 /* Total length of the event */
2065 hdr_len = event_type_size[descr->header_type];
2066 event_len = hdr_len + extra_len;
2067
2068#ifdef WE_EVENT_DEBUG
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002069 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#endif /* WE_EVENT_DEBUG */
2071
2072 /* Create temporary buffer to hold the event */
2073 event = kmalloc(event_len, GFP_ATOMIC);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002074 if (event == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return;
2076
2077 /* Fill event */
2078 event->len = event_len;
2079 event->cmd = cmd;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002080 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002081 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 memcpy(((char *) event) + hdr_len, extra, extra_len);
2083
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002084#ifdef WE_EVENT_RTNETLINK
2085 /* Send via the RtNetlink event channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 rtmsg_iwinfo(dev, (char *) event, event_len);
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08002087#endif /* WE_EVENT_RTNETLINK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 /* Cleanup */
2090 kfree(event);
2091
2092 return; /* Always success, I guess ;-) */
2093}
2094
2095/********************** ENHANCED IWSPY SUPPORT **********************/
2096/*
2097 * In the old days, the driver was handling spy support all by itself.
2098 * Now, the driver can delegate this task to Wireless Extensions.
2099 * It needs to use those standard spy iw_handler in struct iw_handler_def,
2100 * push data to us via wireless_spy_update() and include struct iw_spy_data
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002101 * in its private part (and export it in net_device->wireless_data->spy_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * One of the main advantage of centralising spy support here is that
2103 * it becomes much easier to improve and extend it without having to touch
2104 * the drivers. One example is the addition of the Spy-Threshold events.
2105 */
2106
2107/* ---------------------------------------------------------------- */
2108/*
2109 * Return the pointer to the spy data in the driver.
2110 * Because this is called on the Rx path via wireless_spy_update(),
2111 * we want it to be efficient...
2112 */
2113static inline struct iw_spy_data * get_spydata(struct net_device *dev)
2114{
2115 /* This is the new way */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002116 if (dev->wireless_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return(dev->wireless_data->spy_data);
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/*------------------------------------------------------------------*/
2122/*
2123 * Standard Wireless Handler : set Spy List
2124 */
2125int iw_handler_set_spy(struct net_device * dev,
2126 struct iw_request_info * info,
2127 union iwreq_data * wrqu,
2128 char * extra)
2129{
2130 struct iw_spy_data * spydata = get_spydata(dev);
2131 struct sockaddr * address = (struct sockaddr *) extra;
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002134 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return -EOPNOTSUPP;
2136
2137 /* Disable spy collection while we copy the addresses.
2138 * While we copy addresses, any call to wireless_spy_update()
2139 * will NOP. This is OK, as anyway the addresses are changing. */
2140 spydata->spy_number = 0;
2141
2142 /* We want to operate without locking, because wireless_spy_update()
2143 * most likely will happen in the interrupt handler, and therefore
2144 * have its own locking constraints and needs performance.
2145 * The rtnl_lock() make sure we don't race with the other iw_handlers.
2146 * This make sure wireless_spy_update() "see" that the spy list
2147 * is temporarily disabled. */
Ralf Baechlee16aa202006-12-07 00:11:33 -08002148 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Are there are addresses to copy? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002151 if (wrqu->data.length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 int i;
2153
2154 /* Copy addresses */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002155 for (i = 0; i < wrqu->data.length; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 memcpy(spydata->spy_address[i], address[i].sa_data,
2157 ETH_ALEN);
2158 /* Reset stats */
2159 memset(spydata->spy_stat, 0,
2160 sizeof(struct iw_quality) * IW_MAX_SPY);
2161
2162#ifdef WE_SPY_DEBUG
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002163 printk(KERN_DEBUG "iw_handler_set_spy() : wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 for (i = 0; i < wrqu->data.length; i++)
2165 printk(KERN_DEBUG
2166 "%02X:%02X:%02X:%02X:%02X:%02X \n",
2167 spydata->spy_address[i][0],
2168 spydata->spy_address[i][1],
2169 spydata->spy_address[i][2],
2170 spydata->spy_address[i][3],
2171 spydata->spy_address[i][4],
2172 spydata->spy_address[i][5]);
2173#endif /* WE_SPY_DEBUG */
2174 }
2175
2176 /* Make sure above is updated before re-enabling */
Ralf Baechlee16aa202006-12-07 00:11:33 -08002177 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /* Enable addresses */
2180 spydata->spy_number = wrqu->data.length;
2181
2182 return 0;
2183}
2184
2185/*------------------------------------------------------------------*/
2186/*
2187 * Standard Wireless Handler : get Spy List
2188 */
2189int iw_handler_get_spy(struct net_device * dev,
2190 struct iw_request_info * info,
2191 union iwreq_data * wrqu,
2192 char * extra)
2193{
2194 struct iw_spy_data * spydata = get_spydata(dev);
2195 struct sockaddr * address = (struct sockaddr *) extra;
2196 int i;
2197
2198 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002199 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return -EOPNOTSUPP;
2201
2202 wrqu->data.length = spydata->spy_number;
2203
2204 /* Copy addresses. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002205 for (i = 0; i < spydata->spy_number; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
2207 address[i].sa_family = AF_UNIX;
2208 }
2209 /* Copy stats to the user buffer (just after). */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002210 if (spydata->spy_number > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
2212 spydata->spy_stat,
2213 sizeof(struct iw_quality) * spydata->spy_number);
2214 /* Reset updated flags. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002215 for (i = 0; i < spydata->spy_number; i++)
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002216 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218}
2219
2220/*------------------------------------------------------------------*/
2221/*
2222 * Standard Wireless Handler : set spy threshold
2223 */
2224int iw_handler_set_thrspy(struct net_device * dev,
2225 struct iw_request_info *info,
2226 union iwreq_data * wrqu,
2227 char * extra)
2228{
2229 struct iw_spy_data * spydata = get_spydata(dev);
2230 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
2231
2232 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002233 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return -EOPNOTSUPP;
2235
2236 /* Just do it */
2237 memcpy(&(spydata->spy_thr_low), &(threshold->low),
2238 2 * sizeof(struct iw_quality));
2239
2240 /* Clear flag */
2241 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
2242
2243#ifdef WE_SPY_DEBUG
2244 printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
2245#endif /* WE_SPY_DEBUG */
2246
2247 return 0;
2248}
2249
2250/*------------------------------------------------------------------*/
2251/*
2252 * Standard Wireless Handler : get spy threshold
2253 */
2254int iw_handler_get_thrspy(struct net_device * dev,
2255 struct iw_request_info *info,
2256 union iwreq_data * wrqu,
2257 char * extra)
2258{
2259 struct iw_spy_data * spydata = get_spydata(dev);
2260 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
2261
2262 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002263 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return -EOPNOTSUPP;
2265
2266 /* Just do it */
2267 memcpy(&(threshold->low), &(spydata->spy_thr_low),
2268 2 * sizeof(struct iw_quality));
2269
2270 return 0;
2271}
2272
2273/*------------------------------------------------------------------*/
2274/*
2275 * Prepare and send a Spy Threshold event
2276 */
2277static void iw_send_thrspy_event(struct net_device * dev,
2278 struct iw_spy_data * spydata,
2279 unsigned char * address,
2280 struct iw_quality * wstats)
2281{
2282 union iwreq_data wrqu;
2283 struct iw_thrspy threshold;
2284
2285 /* Init */
2286 wrqu.data.length = 1;
2287 wrqu.data.flags = 0;
2288 /* Copy address */
2289 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
2290 threshold.addr.sa_family = ARPHRD_ETHER;
2291 /* Copy stats */
2292 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
2293 /* Copy also thresholds */
2294 memcpy(&(threshold.low), &(spydata->spy_thr_low),
2295 2 * sizeof(struct iw_quality));
2296
2297#ifdef WE_SPY_DEBUG
2298 printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
2299 threshold.addr.sa_data[0],
2300 threshold.addr.sa_data[1],
2301 threshold.addr.sa_data[2],
2302 threshold.addr.sa_data[3],
2303 threshold.addr.sa_data[4],
2304 threshold.addr.sa_data[5], threshold.qual.level);
2305#endif /* WE_SPY_DEBUG */
2306
2307 /* Send event to user space */
2308 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
2309}
2310
2311/* ---------------------------------------------------------------- */
2312/*
2313 * Call for the driver to update the spy data.
2314 * For now, the spy data is a simple array. As the size of the array is
2315 * small, this is good enough. If we wanted to support larger number of
2316 * spy addresses, we should use something more efficient...
2317 */
2318void wireless_spy_update(struct net_device * dev,
2319 unsigned char * address,
2320 struct iw_quality * wstats)
2321{
2322 struct iw_spy_data * spydata = get_spydata(dev);
2323 int i;
2324 int match = -1;
2325
2326 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002327 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return;
2329
2330#ifdef WE_SPY_DEBUG
Jean Tourrilhes6582c162005-09-02 11:32:28 -07002331 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332#endif /* WE_SPY_DEBUG */
2333
2334 /* Update all records that match */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002335 for (i = 0; i < spydata->spy_number; i++)
2336 if (!compare_ether_addr(address, spydata->spy_address[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 memcpy(&(spydata->spy_stat[i]), wstats,
2338 sizeof(struct iw_quality));
2339 match = i;
2340 }
2341
2342 /* Generate an event if we cross the spy threshold.
2343 * To avoid event storms, we have a simple hysteresis : we generate
2344 * event only when we go under the low threshold or above the
2345 * high threshold. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002346 if (match >= 0) {
2347 if (spydata->spy_thr_under[match]) {
2348 if (wstats->level > spydata->spy_thr_high.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 spydata->spy_thr_under[match] = 0;
2350 iw_send_thrspy_event(dev, spydata,
2351 address, wstats);
2352 }
2353 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002354 if (wstats->level < spydata->spy_thr_low.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 spydata->spy_thr_under[match] = 1;
2356 iw_send_thrspy_event(dev, spydata,
2357 address, wstats);
2358 }
2359 }
2360 }
2361}
2362
2363EXPORT_SYMBOL(iw_handler_get_spy);
2364EXPORT_SYMBOL(iw_handler_get_thrspy);
2365EXPORT_SYMBOL(iw_handler_set_spy);
2366EXPORT_SYMBOL(iw_handler_set_thrspy);
2367EXPORT_SYMBOL(wireless_send_event);
2368EXPORT_SYMBOL(wireless_spy_update);