blob: 22378daceb9464865f3be5a4d9ab4d223a2e82cf [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
Nick Andrew6c5cc8e2008-12-06 23:55:32 -080067 * o Don't lose 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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020096#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#include <linux/wireless.h> /* Pretty obvious */
99#include <net/iw_handler.h> /* New driver API */
Thomas Grafbd5785b2006-08-15 00:36:49 -0700100#include <net/netlink.h>
Johannes Berg295f4a12007-04-26 20:43:56 -0700101#include <net/wext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#include <asm/uaccess.h> /* copy_to_user() */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/************************* GLOBAL VARIABLES *************************/
106/*
107 * You should not use global variables, because of re-entrancy.
108 * On our case, it's only const, so it's OK...
109 */
110/*
111 * Meta-data about all the standard Wireless Extension request we
112 * know about.
113 */
114static const struct iw_ioctl_description standard_ioctl[] = {
115 [SIOCSIWCOMMIT - SIOCIWFIRST] = {
116 .header_type = IW_HEADER_TYPE_NULL,
117 },
118 [SIOCGIWNAME - SIOCIWFIRST] = {
119 .header_type = IW_HEADER_TYPE_CHAR,
120 .flags = IW_DESCR_FLAG_DUMP,
121 },
122 [SIOCSIWNWID - SIOCIWFIRST] = {
123 .header_type = IW_HEADER_TYPE_PARAM,
124 .flags = IW_DESCR_FLAG_EVENT,
125 },
126 [SIOCGIWNWID - SIOCIWFIRST] = {
127 .header_type = IW_HEADER_TYPE_PARAM,
128 .flags = IW_DESCR_FLAG_DUMP,
129 },
130 [SIOCSIWFREQ - SIOCIWFIRST] = {
131 .header_type = IW_HEADER_TYPE_FREQ,
132 .flags = IW_DESCR_FLAG_EVENT,
133 },
134 [SIOCGIWFREQ - SIOCIWFIRST] = {
135 .header_type = IW_HEADER_TYPE_FREQ,
136 .flags = IW_DESCR_FLAG_DUMP,
137 },
138 [SIOCSIWMODE - SIOCIWFIRST] = {
139 .header_type = IW_HEADER_TYPE_UINT,
140 .flags = IW_DESCR_FLAG_EVENT,
141 },
142 [SIOCGIWMODE - SIOCIWFIRST] = {
143 .header_type = IW_HEADER_TYPE_UINT,
144 .flags = IW_DESCR_FLAG_DUMP,
145 },
146 [SIOCSIWSENS - SIOCIWFIRST] = {
147 .header_type = IW_HEADER_TYPE_PARAM,
148 },
149 [SIOCGIWSENS - SIOCIWFIRST] = {
150 .header_type = IW_HEADER_TYPE_PARAM,
151 },
152 [SIOCSIWRANGE - SIOCIWFIRST] = {
153 .header_type = IW_HEADER_TYPE_NULL,
154 },
155 [SIOCGIWRANGE - SIOCIWFIRST] = {
156 .header_type = IW_HEADER_TYPE_POINT,
157 .token_size = 1,
158 .max_tokens = sizeof(struct iw_range),
159 .flags = IW_DESCR_FLAG_DUMP,
160 },
161 [SIOCSIWPRIV - SIOCIWFIRST] = {
162 .header_type = IW_HEADER_TYPE_NULL,
163 },
164 [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800165 .header_type = IW_HEADER_TYPE_POINT,
166 .token_size = sizeof(struct iw_priv_args),
167 .max_tokens = 16,
168 .flags = IW_DESCR_FLAG_NOMAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 },
170 [SIOCSIWSTATS - SIOCIWFIRST] = {
171 .header_type = IW_HEADER_TYPE_NULL,
172 },
173 [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800174 .header_type = IW_HEADER_TYPE_POINT,
175 .token_size = 1,
176 .max_tokens = sizeof(struct iw_statistics),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .flags = IW_DESCR_FLAG_DUMP,
178 },
179 [SIOCSIWSPY - SIOCIWFIRST] = {
180 .header_type = IW_HEADER_TYPE_POINT,
181 .token_size = sizeof(struct sockaddr),
182 .max_tokens = IW_MAX_SPY,
183 },
184 [SIOCGIWSPY - SIOCIWFIRST] = {
185 .header_type = IW_HEADER_TYPE_POINT,
186 .token_size = sizeof(struct sockaddr) +
187 sizeof(struct iw_quality),
188 .max_tokens = IW_MAX_SPY,
189 },
190 [SIOCSIWTHRSPY - SIOCIWFIRST] = {
191 .header_type = IW_HEADER_TYPE_POINT,
192 .token_size = sizeof(struct iw_thrspy),
193 .min_tokens = 1,
194 .max_tokens = 1,
195 },
196 [SIOCGIWTHRSPY - SIOCIWFIRST] = {
197 .header_type = IW_HEADER_TYPE_POINT,
198 .token_size = sizeof(struct iw_thrspy),
199 .min_tokens = 1,
200 .max_tokens = 1,
201 },
202 [SIOCSIWAP - SIOCIWFIRST] = {
203 .header_type = IW_HEADER_TYPE_ADDR,
204 },
205 [SIOCGIWAP - SIOCIWFIRST] = {
206 .header_type = IW_HEADER_TYPE_ADDR,
207 .flags = IW_DESCR_FLAG_DUMP,
208 },
fff9cfd2005-05-12 20:24:19 -0400209 [SIOCSIWMLME - SIOCIWFIRST] = {
210 .header_type = IW_HEADER_TYPE_POINT,
211 .token_size = 1,
212 .min_tokens = sizeof(struct iw_mlme),
213 .max_tokens = sizeof(struct iw_mlme),
214 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 [SIOCGIWAPLIST - SIOCIWFIRST] = {
216 .header_type = IW_HEADER_TYPE_POINT,
217 .token_size = sizeof(struct sockaddr) +
218 sizeof(struct iw_quality),
219 .max_tokens = IW_MAX_AP,
220 .flags = IW_DESCR_FLAG_NOMAX,
221 },
222 [SIOCSIWSCAN - SIOCIWFIRST] = {
fff9cfd2005-05-12 20:24:19 -0400223 .header_type = IW_HEADER_TYPE_POINT,
224 .token_size = 1,
225 .min_tokens = 0,
226 .max_tokens = sizeof(struct iw_scan_req),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 },
228 [SIOCGIWSCAN - SIOCIWFIRST] = {
229 .header_type = IW_HEADER_TYPE_POINT,
230 .token_size = 1,
231 .max_tokens = IW_SCAN_MAX_DATA,
232 .flags = IW_DESCR_FLAG_NOMAX,
233 },
234 [SIOCSIWESSID - SIOCIWFIRST] = {
235 .header_type = IW_HEADER_TYPE_POINT,
236 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400237 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .flags = IW_DESCR_FLAG_EVENT,
239 },
240 [SIOCGIWESSID - SIOCIWFIRST] = {
241 .header_type = IW_HEADER_TYPE_POINT,
242 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400243 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .flags = IW_DESCR_FLAG_DUMP,
245 },
246 [SIOCSIWNICKN - SIOCIWFIRST] = {
247 .header_type = IW_HEADER_TYPE_POINT,
248 .token_size = 1,
John W. Linvillebaef1862006-09-08 16:04:05 -0400249 .max_tokens = IW_ESSID_MAX_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 },
251 [SIOCGIWNICKN - 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 },
256 [SIOCSIWRATE - SIOCIWFIRST] = {
257 .header_type = IW_HEADER_TYPE_PARAM,
258 },
259 [SIOCGIWRATE - SIOCIWFIRST] = {
260 .header_type = IW_HEADER_TYPE_PARAM,
261 },
262 [SIOCSIWRTS - SIOCIWFIRST] = {
263 .header_type = IW_HEADER_TYPE_PARAM,
264 },
265 [SIOCGIWRTS - SIOCIWFIRST] = {
266 .header_type = IW_HEADER_TYPE_PARAM,
267 },
268 [SIOCSIWFRAG - SIOCIWFIRST] = {
269 .header_type = IW_HEADER_TYPE_PARAM,
270 },
271 [SIOCGIWFRAG - SIOCIWFIRST] = {
272 .header_type = IW_HEADER_TYPE_PARAM,
273 },
274 [SIOCSIWTXPOW - SIOCIWFIRST] = {
275 .header_type = IW_HEADER_TYPE_PARAM,
276 },
277 [SIOCGIWTXPOW - SIOCIWFIRST] = {
278 .header_type = IW_HEADER_TYPE_PARAM,
279 },
280 [SIOCSIWRETRY - SIOCIWFIRST] = {
281 .header_type = IW_HEADER_TYPE_PARAM,
282 },
283 [SIOCGIWRETRY - SIOCIWFIRST] = {
284 .header_type = IW_HEADER_TYPE_PARAM,
285 },
286 [SIOCSIWENCODE - SIOCIWFIRST] = {
287 .header_type = IW_HEADER_TYPE_POINT,
288 .token_size = 1,
289 .max_tokens = IW_ENCODING_TOKEN_MAX,
290 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
291 },
292 [SIOCGIWENCODE - SIOCIWFIRST] = {
293 .header_type = IW_HEADER_TYPE_POINT,
294 .token_size = 1,
295 .max_tokens = IW_ENCODING_TOKEN_MAX,
296 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
297 },
298 [SIOCSIWPOWER - SIOCIWFIRST] = {
299 .header_type = IW_HEADER_TYPE_PARAM,
300 },
301 [SIOCGIWPOWER - SIOCIWFIRST] = {
302 .header_type = IW_HEADER_TYPE_PARAM,
303 },
fff9cfd2005-05-12 20:24:19 -0400304 [SIOCSIWGENIE - SIOCIWFIRST] = {
305 .header_type = IW_HEADER_TYPE_POINT,
306 .token_size = 1,
307 .max_tokens = IW_GENERIC_IE_MAX,
308 },
309 [SIOCGIWGENIE - SIOCIWFIRST] = {
310 .header_type = IW_HEADER_TYPE_POINT,
311 .token_size = 1,
312 .max_tokens = IW_GENERIC_IE_MAX,
313 },
314 [SIOCSIWAUTH - SIOCIWFIRST] = {
315 .header_type = IW_HEADER_TYPE_PARAM,
316 },
317 [SIOCGIWAUTH - SIOCIWFIRST] = {
318 .header_type = IW_HEADER_TYPE_PARAM,
319 },
320 [SIOCSIWENCODEEXT - SIOCIWFIRST] = {
321 .header_type = IW_HEADER_TYPE_POINT,
322 .token_size = 1,
323 .min_tokens = sizeof(struct iw_encode_ext),
324 .max_tokens = sizeof(struct iw_encode_ext) +
325 IW_ENCODING_TOKEN_MAX,
326 },
327 [SIOCGIWENCODEEXT - SIOCIWFIRST] = {
328 .header_type = IW_HEADER_TYPE_POINT,
329 .token_size = 1,
330 .min_tokens = sizeof(struct iw_encode_ext),
331 .max_tokens = sizeof(struct iw_encode_ext) +
332 IW_ENCODING_TOKEN_MAX,
333 },
334 [SIOCSIWPMKSA - SIOCIWFIRST] = {
335 .header_type = IW_HEADER_TYPE_POINT,
336 .token_size = 1,
337 .min_tokens = sizeof(struct iw_pmksa),
338 .max_tokens = sizeof(struct iw_pmksa),
339 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800341static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343/*
344 * Meta-data about all the additional standard Wireless Extension events
345 * we know about.
346 */
347static const struct iw_ioctl_description standard_event[] = {
348 [IWEVTXDROP - IWEVFIRST] = {
349 .header_type = IW_HEADER_TYPE_ADDR,
350 },
351 [IWEVQUAL - IWEVFIRST] = {
352 .header_type = IW_HEADER_TYPE_QUAL,
353 },
354 [IWEVCUSTOM - IWEVFIRST] = {
355 .header_type = IW_HEADER_TYPE_POINT,
356 .token_size = 1,
357 .max_tokens = IW_CUSTOM_MAX,
358 },
359 [IWEVREGISTERED - IWEVFIRST] = {
360 .header_type = IW_HEADER_TYPE_ADDR,
361 },
362 [IWEVEXPIRED - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900363 .header_type = IW_HEADER_TYPE_ADDR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 },
fff9cfd2005-05-12 20:24:19 -0400365 [IWEVGENIE - IWEVFIRST] = {
366 .header_type = IW_HEADER_TYPE_POINT,
367 .token_size = 1,
368 .max_tokens = IW_GENERIC_IE_MAX,
369 },
370 [IWEVMICHAELMICFAILURE - IWEVFIRST] = {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900371 .header_type = IW_HEADER_TYPE_POINT,
fff9cfd2005-05-12 20:24:19 -0400372 .token_size = 1,
373 .max_tokens = sizeof(struct iw_michaelmicfailure),
374 },
375 [IWEVASSOCREQIE - IWEVFIRST] = {
376 .header_type = IW_HEADER_TYPE_POINT,
377 .token_size = 1,
378 .max_tokens = IW_GENERIC_IE_MAX,
379 },
380 [IWEVASSOCRESPIE - IWEVFIRST] = {
381 .header_type = IW_HEADER_TYPE_POINT,
382 .token_size = 1,
383 .max_tokens = IW_GENERIC_IE_MAX,
384 },
385 [IWEVPMKIDCAND - IWEVFIRST] = {
386 .header_type = IW_HEADER_TYPE_POINT,
387 .token_size = 1,
388 .max_tokens = sizeof(struct iw_pmkid_cand),
389 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
Stephen Hemminger1ac58ee2007-03-08 20:43:49 -0800391static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* Size (in bytes) of the various private data types */
394static const char iw_priv_type_size[] = {
395 0, /* IW_PRIV_TYPE_NONE */
396 1, /* IW_PRIV_TYPE_BYTE */
397 1, /* IW_PRIV_TYPE_CHAR */
398 0, /* Not defined */
399 sizeof(__u32), /* IW_PRIV_TYPE_INT */
400 sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */
401 sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */
402 0, /* Not defined */
403};
404
405/* Size (in bytes) of various events */
406static const int event_type_size[] = {
407 IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
408 0,
409 IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
410 0,
411 IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
412 IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
413 IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
414 0,
415 IW_EV_POINT_LEN, /* Without variable payload */
416 IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
417 IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
418};
419
Jean Tourrilhesc2805fb2007-03-23 00:31:16 +0000420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/************************ COMMON SUBROUTINES ************************/
422/*
423 * Stuff that may be used in various place or doesn't fit in one
424 * of the section below.
425 */
426
427/* ---------------------------------------------------------------- */
428/*
429 * Return the driver handler associated with a specific Wireless Extension.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700431static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 /* Don't "optimise" the following variable, it will crash */
434 unsigned int index; /* *MUST* be unsigned */
435
436 /* Check if we have some wireless handlers defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700437 if (dev->wireless_handlers == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return NULL;
439
440 /* Try as a standard command */
441 index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700442 if (index < dev->wireless_handlers->num_standard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return dev->wireless_handlers->standard[index];
444
445 /* Try as a private command */
446 index = cmd - SIOCIWFIRSTPRIV;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700447 if (index < dev->wireless_handlers->num_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return dev->wireless_handlers->private[index];
449
450 /* Not found */
451 return NULL;
452}
453
454/* ---------------------------------------------------------------- */
455/*
456 * Get statistics out of the driver
457 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700458static struct iw_statistics *get_wireless_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 /* New location */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700461 if ((dev->wireless_handlers != NULL) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 (dev->wireless_handlers->get_wireless_stats != NULL))
463 return dev->wireless_handlers->get_wireless_stats(dev);
464
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700465 /* Not found */
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700466 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/* ---------------------------------------------------------------- */
470/*
471 * Call the commit handler in the driver
472 * (if exist and if conditions are right)
473 *
474 * Note : our current commit strategy is currently pretty dumb,
475 * but we will be able to improve on that...
476 * The goal is to try to agreagate as many changes as possible
477 * before doing the commit. Drivers that will define a commit handler
478 * are usually those that need a reset after changing parameters, so
479 * we want to minimise the number of reset.
480 * A cool idea is to use a timer : at each "set" command, we re-set the
481 * timer, when the timer eventually fires, we call the driver.
482 * Hopefully, more on that later.
483 *
484 * Also, I'm waiting to see how many people will complain about the
485 * netif_running(dev) test. I'm open on that one...
486 * Hopefully, the driver will remember to do a commit in "open()" ;-)
487 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700488static int call_commit_handler(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700490 if ((netif_running(dev)) &&
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700491 (dev->wireless_handlers->standard[0] != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* Call the commit handler on the driver */
493 return dev->wireless_handlers->standard[0](dev, NULL,
494 NULL, NULL);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700495 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0; /* Command completed successfully */
497}
498
499/* ---------------------------------------------------------------- */
500/*
501 * Calculate size of private arguments
502 */
David S. Miller25519a22007-12-21 03:22:38 -0800503static int get_priv_size(__u16 args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int num = args & IW_PRIV_SIZE_MASK;
506 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
507
508 return num * iw_priv_type_size[type];
509}
510
511/* ---------------------------------------------------------------- */
512/*
513 * Re-calculate the size of private arguments
514 */
David S. Miller208887d2007-12-21 03:24:24 -0800515static int adjust_priv_size(__u16 args, struct iw_point *iwp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
David S. Miller208887d2007-12-21 03:24:24 -0800517 int num = iwp->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int max = args & IW_PRIV_SIZE_MASK;
519 int type = (args & IW_PRIV_TYPE_MASK) >> 12;
520
521 /* Make sure the driver doesn't goof up */
522 if (max < num)
523 num = max;
524
525 return num * iw_priv_type_size[type];
526}
527
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800528/* ---------------------------------------------------------------- */
529/*
530 * Standard Wireless Handler : get wireless stats
531 * Allow programatic access to /proc/net/wireless even if /proc
532 * doesn't exist... Also more efficient...
533 */
534static int iw_handler_get_iwstats(struct net_device * dev,
535 struct iw_request_info * info,
536 union iwreq_data * wrqu,
537 char * extra)
538{
539 /* Get stats from the driver */
540 struct iw_statistics *stats;
541
542 stats = get_wireless_stats(dev);
Johannes Berg4d44e0d2007-04-26 20:47:25 -0700543 if (stats) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800544 /* Copy statistics to extra */
545 memcpy(extra, stats, sizeof(struct iw_statistics));
546 wrqu->data.length = sizeof(struct iw_statistics);
547
548 /* Check if we need to clear the updated flag */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700549 if (wrqu->data.flags != 0)
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800550 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
551 return 0;
552 } else
553 return -EOPNOTSUPP;
554}
555
556/* ---------------------------------------------------------------- */
557/*
558 * Standard Wireless Handler : get iwpriv definitions
559 * Export the driver private handler definition
560 * They will be picked up by tools like iwpriv...
561 */
562static int iw_handler_get_private(struct net_device * dev,
563 struct iw_request_info * info,
564 union iwreq_data * wrqu,
565 char * extra)
566{
567 /* Check if the driver has something to export */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700568 if ((dev->wireless_handlers->num_private_args == 0) ||
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800569 (dev->wireless_handlers->private_args == NULL))
570 return -EOPNOTSUPP;
571
572 /* Check if there is enough buffer up there */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700573 if (wrqu->data.length < dev->wireless_handlers->num_private_args) {
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800574 /* User space can't know in advance how large the buffer
575 * needs to be. Give it a hint, so that we can support
576 * any size buffer we want somewhat efficiently... */
577 wrqu->data.length = dev->wireless_handlers->num_private_args;
578 return -E2BIG;
579 }
580
581 /* Set the number of available ioctls. */
582 wrqu->data.length = dev->wireless_handlers->num_private_args;
583
584 /* Copy structure to the user buffer. */
585 memcpy(extra, dev->wireless_handlers->private_args,
586 sizeof(struct iw_priv_args) * wrqu->data.length);
587
588 return 0;
589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592/******************** /proc/net/wireless SUPPORT ********************/
593/*
594 * The /proc/net/wireless file is a human readable user-space interface
595 * exporting various wireless specific statistics from the wireless devices.
596 * This is the most popular part of the Wireless Extensions ;-)
597 *
598 * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
599 * The content of the file is basically the content of "struct iw_statistics".
600 */
601
602#ifdef CONFIG_PROC_FS
603
604/* ---------------------------------------------------------------- */
605/*
606 * Print one entry (line) of /proc/net/wireless
607 */
Johannes Bergbdf51892007-04-26 20:46:55 -0700608static void wireless_seq_printf_stats(struct seq_file *seq,
609 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 /* Get stats from the driver */
612 struct iw_statistics *stats = get_wireless_stats(dev);
613
614 if (stats) {
615 seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d "
616 "%6d %6d %6d\n",
617 dev->name, stats->status, stats->qual.qual,
618 stats->qual.updated & IW_QUAL_QUAL_UPDATED
619 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900620 ((__s32) stats->qual.level) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700621 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 stats->qual.updated & IW_QUAL_LEVEL_UPDATED
623 ? '.' : ' ',
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900624 ((__s32) stats->qual.noise) -
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700625 ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 stats->qual.updated & IW_QUAL_NOISE_UPDATED
627 ? '.' : ' ',
628 stats->discard.nwid, stats->discard.code,
629 stats->discard.fragment, stats->discard.retries,
630 stats->discard.misc, stats->miss.beacon);
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700631 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633}
634
635/* ---------------------------------------------------------------- */
636/*
637 * Print info for /proc/net/wireless (print all entries)
638 */
Johannes Berg87057822009-05-19 17:19:36 +0200639static int wireless_dev_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Johannes Berg87057822009-05-19 17:19:36 +0200641 might_sleep();
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (v == SEQ_START_TOKEN)
644 seq_printf(seq, "Inter-| sta-| Quality | Discarded "
645 "packets | Missed | WE\n"
646 " face | tus | link level noise | nwid "
647 "crypt frag retry misc | beacon | %d\n",
648 WIRELESS_EXT);
649 else
650 wireless_seq_printf_stats(seq, v);
651 return 0;
652}
653
Johannes Berg7be69c02009-05-11 16:06:36 +0200654static void *wireless_dev_seq_start(struct seq_file *seq, loff_t *pos)
Johannes Berg7be69c02009-05-11 16:06:36 +0200655{
Johannes Berg87057822009-05-19 17:19:36 +0200656 struct net *net = seq_file_net(seq);
657 loff_t off;
658 struct net_device *dev;
659
Johannes Berg7be69c02009-05-11 16:06:36 +0200660 rtnl_lock();
Johannes Berg87057822009-05-19 17:19:36 +0200661 if (!*pos)
662 return SEQ_START_TOKEN;
663
664 off = 1;
665 for_each_netdev(net, dev)
666 if (off++ == *pos)
667 return dev;
668 return NULL;
669}
670
671static void *wireless_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
672{
673 struct net *net = seq_file_net(seq);
674
675 ++*pos;
676
677 return v == SEQ_START_TOKEN ?
678 first_net_device(net) : next_net_device(v);
Johannes Berg7be69c02009-05-11 16:06:36 +0200679}
680
681static void wireless_dev_seq_stop(struct seq_file *seq, void *v)
Johannes Berg7be69c02009-05-11 16:06:36 +0200682{
Johannes Berg7be69c02009-05-11 16:06:36 +0200683 rtnl_unlock();
684}
685
Stephen Hemmingerf6908082007-03-12 14:34:29 -0700686static const struct seq_operations wireless_seq_ops = {
Johannes Berg7be69c02009-05-11 16:06:36 +0200687 .start = wireless_dev_seq_start,
Johannes Berg87057822009-05-19 17:19:36 +0200688 .next = wireless_dev_seq_next,
Johannes Berg7be69c02009-05-11 16:06:36 +0200689 .stop = wireless_dev_seq_stop,
Johannes Berg87057822009-05-19 17:19:36 +0200690 .show = wireless_dev_seq_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691};
692
Johannes Berg7be69c02009-05-11 16:06:36 +0200693static int seq_open_wireless(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Denis V. Luneve372c412007-11-19 22:31:54 -0800695 return seq_open_net(inode, file, &wireless_seq_ops,
696 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Arjan van de Ven9a321442007-02-12 00:55:35 -0800699static const struct file_operations wireless_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 .owner = THIS_MODULE,
Johannes Berg7be69c02009-05-11 16:06:36 +0200701 .open = seq_open_wireless,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 .read = seq_read,
703 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -0800704 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
Eric W. Biederman881d9662007-09-17 11:56:21 -0700707int wext_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Jean Tourrilhes6582c162005-09-02 11:32:28 -0700709 /* Create /proc/net/wireless entry */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700710 if (!proc_net_fops_create(net, "wireless", S_IRUGO, &wireless_seq_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -ENOMEM;
712
713 return 0;
714}
Eric W. Biederman881d9662007-09-17 11:56:21 -0700715
716void wext_proc_exit(struct net *net)
717{
718 proc_net_remove(net, "wireless");
719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720#endif /* CONFIG_PROC_FS */
721
722/************************** IOCTL SUPPORT **************************/
723/*
724 * The original user space API to configure all those Wireless Extensions
725 * is through IOCTLs.
726 * In there, we check if we need to call the new driver API (iw_handler)
727 * or just call the driver ioctl handler.
728 */
729
730/* ---------------------------------------------------------------- */
David S. Miller84149b02007-12-21 03:27:17 -0800731static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
732 const struct iw_ioctl_description *descr,
733 iw_handler handler, struct net_device *dev,
734 struct iw_request_info *info)
735{
736 int err, extra_size, user_length = 0, essid_compat = 0;
737 char *extra;
738
739 /* Calculate space needed by arguments. Always allocate
740 * for max space.
741 */
742 extra_size = descr->max_tokens * descr->token_size;
743
744 /* Check need for ESSID compatibility for WE < 21 */
745 switch (cmd) {
746 case SIOCSIWESSID:
747 case SIOCGIWESSID:
748 case SIOCSIWNICKN:
749 case SIOCGIWNICKN:
750 if (iwp->length == descr->max_tokens + 1)
751 essid_compat = 1;
752 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
753 char essid[IW_ESSID_MAX_SIZE + 1];
754
755 err = copy_from_user(essid, iwp->pointer,
756 iwp->length *
757 descr->token_size);
758 if (err)
759 return -EFAULT;
760
761 if (essid[iwp->length - 1] == '\0')
762 essid_compat = 1;
763 }
764 break;
765 default:
766 break;
767 }
768
769 iwp->length -= essid_compat;
770
771 /* Check what user space is giving us */
772 if (IW_IS_SET(cmd)) {
773 /* Check NULL pointer */
774 if (!iwp->pointer && iwp->length != 0)
775 return -EFAULT;
776 /* Check if number of token fits within bounds */
777 if (iwp->length > descr->max_tokens)
778 return -E2BIG;
779 if (iwp->length < descr->min_tokens)
780 return -EINVAL;
781 } else {
782 /* Check NULL pointer */
783 if (!iwp->pointer)
784 return -EFAULT;
785 /* Save user space buffer size for checking */
786 user_length = iwp->length;
787
788 /* Don't check if user_length > max to allow forward
789 * compatibility. The test user_length < min is
790 * implied by the test at the end.
791 */
792
793 /* Support for very large requests */
794 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
795 (user_length > descr->max_tokens)) {
796 /* Allow userspace to GET more than max so
797 * we can support any size GET requests.
798 * There is still a limit : -ENOMEM.
799 */
800 extra_size = user_length * descr->token_size;
801
802 /* Note : user_length is originally a __u16,
803 * and token_size is controlled by us,
804 * so extra_size won't get negative and
805 * won't overflow...
806 */
807 }
808 }
809
810 /* kzalloc() ensures NULL-termination for essid_compat. */
811 extra = kzalloc(extra_size, GFP_KERNEL);
812 if (!extra)
813 return -ENOMEM;
814
815 /* If it is a SET, get all the extra data in here */
816 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
817 if (copy_from_user(extra, iwp->pointer,
818 iwp->length *
819 descr->token_size)) {
820 err = -EFAULT;
821 goto out;
822 }
823 }
824
825 err = handler(dev, info, (union iwreq_data *) iwp, extra);
826
827 iwp->length += essid_compat;
828
829 /* If we have something to return to the user */
830 if (!err && IW_IS_GET(cmd)) {
831 /* Check if there is enough buffer up there */
832 if (user_length < iwp->length) {
833 err = -E2BIG;
834 goto out;
835 }
836
837 if (copy_to_user(iwp->pointer, extra,
838 iwp->length *
839 descr->token_size)) {
840 err = -EFAULT;
841 goto out;
842 }
843 }
844
845 /* Generate an event to notify listeners of the change */
846 if ((descr->flags & IW_DESCR_FLAG_EVENT) && err == -EIWCOMMIT) {
847 union iwreq_data *data = (union iwreq_data *) iwp;
848
849 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
850 /* If the event is restricted, don't
851 * export the payload.
852 */
853 wireless_send_event(dev, cmd, data, NULL);
854 else
855 wireless_send_event(dev, cmd, data, extra);
856 }
857
858out:
859 kfree(extra);
860 return err;
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * Wrapper to call a standard Wireless Extension handler.
865 * We do various checks and also take care of moving data between
866 * user space and kernel space.
867 */
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800868static int ioctl_standard_call(struct net_device * dev,
David S. Millerd2911252007-12-21 03:46:01 -0800869 struct iwreq *iwr,
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800870 unsigned int cmd,
David S. Miller0f5cabb2008-06-03 07:39:16 -0700871 struct iw_request_info *info,
Jean Tourrilhes711e2c32006-02-22 15:10:56 -0800872 iw_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 const struct iw_ioctl_description * descr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int ret = -EINVAL;
876
877 /* Get the description of the IOCTL */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700878 if ((cmd - SIOCIWFIRST) >= standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EOPNOTSUPP;
880 descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* Check if we have a pointer to user space data or not */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700883 if (descr->header_type != IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* No extra arguments. Trivial to handle */
David S. Miller0f5cabb2008-06-03 07:39:16 -0700886 ret = handler(dev, info, &(iwr->u), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Generate an event to notify listeners of the change */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700889 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ((ret == 0) || (ret == -EIWCOMMIT)))
891 wireless_send_event(dev, cmd, &(iwr->u), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 } else {
David S. Miller84149b02007-12-21 03:27:17 -0800893 ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
David S. Miller0f5cabb2008-06-03 07:39:16 -0700894 handler, dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
897 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700898 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 ret = call_commit_handler(dev);
900
901 /* Here, we will generate the appropriate event if needed */
902
903 return ret;
904}
905
906/* ---------------------------------------------------------------- */
907/*
908 * Wrapper to call a private Wireless Extension handler.
909 * We do various checks and also take care of moving data between
910 * user space and kernel space.
911 * It's not as nice and slimline as the standard wrapper. The cause
912 * is struct iw_priv_args, which was not really designed for the
913 * job we are going here.
914 *
915 * IMPORTANT : This function prevent to set and get data on the same
916 * IOCTL and enforce the SET/GET convention. Not doing it would be
917 * far too hairy...
918 * If you need to set and get data at the same time, please don't use
919 * a iw_handler but process it in your ioctl handler (i.e. use the
920 * old driver API).
921 */
David S. Millerd88174e2007-12-21 03:33:46 -0800922static int get_priv_descr_and_size(struct net_device *dev, unsigned int cmd,
923 const struct iw_priv_args **descrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
David S. Millerd88174e2007-12-21 03:33:46 -0800925 const struct iw_priv_args *descr;
926 int i, extra_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
David S. Millerd88174e2007-12-21 03:33:46 -0800928 descr = NULL;
929 for (i = 0; i < dev->wireless_handlers->num_private_args; i++) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700930 if (cmd == dev->wireless_handlers->private_args[i].cmd) {
David S. Millerd88174e2007-12-21 03:33:46 -0800931 descr = &dev->wireless_handlers->private_args[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 }
David S. Millerd88174e2007-12-21 03:33:46 -0800934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
David S. Millerd88174e2007-12-21 03:33:46 -0800936 extra_size = 0;
937 if (descr) {
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700938 if (IW_IS_SET(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int offset = 0; /* For sub-ioctls */
940 /* Check for sub-ioctl handler */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700941 if (descr->name[0] == '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Reserve one int for sub-ioctl index */
943 offset = sizeof(__u32);
944
945 /* Size of set arguments */
946 extra_size = get_priv_size(descr->set_args);
947
948 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700949 if ((descr->set_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ((extra_size + offset) <= IFNAMSIZ))
951 extra_size = 0;
952 } else {
953 /* Size of get arguments */
954 extra_size = get_priv_size(descr->get_args);
955
956 /* Does it fits in iwr ? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700957 if ((descr->get_args & IW_PRIV_SIZE_FIXED) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 (extra_size <= IFNAMSIZ))
959 extra_size = 0;
960 }
961 }
David S. Millerd88174e2007-12-21 03:33:46 -0800962 *descrp = descr;
963 return extra_size;
964}
965
966static int ioctl_private_iw_point(struct iw_point *iwp, unsigned int cmd,
967 const struct iw_priv_args *descr,
968 iw_handler handler, struct net_device *dev,
969 struct iw_request_info *info, int extra_size)
970{
971 char *extra;
972 int err;
973
974 /* Check what user space is giving us */
975 if (IW_IS_SET(cmd)) {
976 if (!iwp->pointer && iwp->length != 0)
977 return -EFAULT;
978
979 if (iwp->length > (descr->set_args & IW_PRIV_SIZE_MASK))
980 return -E2BIG;
981 } else if (!iwp->pointer)
982 return -EFAULT;
983
984 extra = kmalloc(extra_size, GFP_KERNEL);
985 if (!extra)
986 return -ENOMEM;
987
988 /* If it is a SET, get all the extra data in here */
989 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
990 if (copy_from_user(extra, iwp->pointer, extra_size)) {
991 err = -EFAULT;
992 goto out;
993 }
994 }
995
996 /* Call the handler */
997 err = handler(dev, info, (union iwreq_data *) iwp, extra);
998
999 /* If we have something to return to the user */
1000 if (!err && IW_IS_GET(cmd)) {
1001 /* Adjust for the actual length if it's variable,
1002 * avoid leaking kernel bits outside.
1003 */
1004 if (!(descr->get_args & IW_PRIV_SIZE_FIXED))
1005 extra_size = adjust_priv_size(descr->get_args, iwp);
1006
1007 if (copy_to_user(iwp->pointer, extra, extra_size))
1008 err = -EFAULT;
1009 }
1010
1011out:
1012 kfree(extra);
1013 return err;
1014}
1015
David S. Millerd2911252007-12-21 03:46:01 -08001016static int ioctl_private_call(struct net_device *dev, struct iwreq *iwr,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001017 unsigned int cmd, struct iw_request_info *info,
1018 iw_handler handler)
David S. Millerd88174e2007-12-21 03:33:46 -08001019{
David S. Millerd88174e2007-12-21 03:33:46 -08001020 int extra_size = 0, ret = -EINVAL;
1021 const struct iw_priv_args *descr;
David S. Millerd88174e2007-12-21 03:33:46 -08001022
1023 extra_size = get_priv_descr_and_size(dev, cmd, &descr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Check if we have a pointer to user space data or not. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001026 if (extra_size == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* No extra arguments. Trivial to handle */
David S. Miller0f5cabb2008-06-03 07:39:16 -07001028 ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 } else {
David S. Millerd88174e2007-12-21 03:33:46 -08001030 ret = ioctl_private_iw_point(&iwr->u.data, cmd, descr,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001031 handler, dev, info, extra_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* Call commit handler if needed and defined */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001035 if (ret == -EIWCOMMIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ret = call_commit_handler(dev);
1037
1038 return ret;
1039}
1040
1041/* ---------------------------------------------------------------- */
David S. Millerd2911252007-12-21 03:46:01 -08001042typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001043 unsigned int, struct iw_request_info *,
1044 iw_handler);
David S. Millerca1e8bb2007-12-21 03:41:45 -08001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046/*
Johannes Berg295f4a12007-04-26 20:43:56 -07001047 * Main IOCTl dispatcher.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * Check the type of IOCTL and call the appropriate wrapper...
1049 */
David S. Millerca1e8bb2007-12-21 03:41:45 -08001050static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
1051 unsigned int cmd,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001052 struct iw_request_info *info,
David S. Millerca1e8bb2007-12-21 03:41:45 -08001053 wext_ioctl_func standard,
1054 wext_ioctl_func private)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
David S. Millerd2911252007-12-21 03:46:01 -08001056 struct iwreq *iwr = (struct iwreq *) ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct net_device *dev;
1058 iw_handler handler;
1059
1060 /* Permissions are already checked in dev_ioctl() before calling us.
1061 * The copy_to/from_user() of ifr is also dealt with in there */
1062
1063 /* Make sure the device exist */
Eric W. Biederman881d9662007-09-17 11:56:21 -07001064 if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -ENODEV;
1066
1067 /* A bunch of special cases, then the generic case...
1068 * Note that 'cmd' is already filtered in dev_ioctl() with
1069 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001070 if (cmd == SIOCGIWSTATS)
David S. Miller0f5cabb2008-06-03 07:39:16 -07001071 return standard(dev, iwr, cmd, info,
David S. Millerca1e8bb2007-12-21 03:41:45 -08001072 &iw_handler_get_iwstats);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001073
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001074 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
David S. Miller0f5cabb2008-06-03 07:39:16 -07001075 return standard(dev, iwr, cmd, info,
David S. Millerca1e8bb2007-12-21 03:41:45 -08001076 &iw_handler_get_private);
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001077
1078 /* Basic check */
1079 if (!netif_device_present(dev))
1080 return -ENODEV;
1081
1082 /* New driver API : try to find the handler */
1083 handler = get_handler(dev, cmd);
1084 if (handler) {
1085 /* Standard and private are not the same */
1086 if (cmd < SIOCIWFIRSTPRIV)
David S. Miller0f5cabb2008-06-03 07:39:16 -07001087 return standard(dev, iwr, cmd, info, handler);
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001088 else
David S. Miller0f5cabb2008-06-03 07:39:16 -07001089 return private(dev, iwr, cmd, info, handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001091 /* Old driver API : call driver ioctl handler */
Stephen Hemminger148bc432009-01-06 10:42:24 -08001092 if (dev->netdev_ops->ndo_do_ioctl)
1093 return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001094 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
David S. Miller67dd7602007-12-21 03:36:31 -08001097/* If command is `set a parameter', or `get the encoding parameters',
1098 * check if the user has the right to do it.
1099 */
1100static int wext_permission_check(unsigned int cmd)
1101{
1102 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE || cmd == SIOCGIWENCODEEXT)
1103 && !capable(CAP_NET_ADMIN))
1104 return -EPERM;
1105
1106 return 0;
1107}
1108
Johannes Berg295f4a12007-04-26 20:43:56 -07001109/* entry point from dev ioctl */
David S. Millera67fa762008-06-03 07:36:30 -07001110static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001111 unsigned int cmd, struct iw_request_info *info,
David S. Millera67fa762008-06-03 07:36:30 -07001112 wext_ioctl_func standard,
1113 wext_ioctl_func private)
Johannes Berg295f4a12007-04-26 20:43:56 -07001114{
David S. Miller67dd7602007-12-21 03:36:31 -08001115 int ret = wext_permission_check(cmd);
Johannes Berg295f4a12007-04-26 20:43:56 -07001116
David S. Miller67dd7602007-12-21 03:36:31 -08001117 if (ret)
1118 return ret;
Johannes Bergdd8ceab2007-04-26 20:45:47 -07001119
Eric W. Biederman881d9662007-09-17 11:56:21 -07001120 dev_load(net, ifr->ifr_name);
Johannes Berg295f4a12007-04-26 20:43:56 -07001121 rtnl_lock();
David S. Miller0f5cabb2008-06-03 07:39:16 -07001122 ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
Johannes Berg295f4a12007-04-26 20:43:56 -07001123 rtnl_unlock();
David S. Millera67fa762008-06-03 07:36:30 -07001124
1125 return ret;
1126}
1127
1128int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1129 void __user *arg)
1130{
David S. Miller0f5cabb2008-06-03 07:39:16 -07001131 struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1132 int ret;
David S. Millera67fa762008-06-03 07:36:30 -07001133
David S. Miller0f5cabb2008-06-03 07:39:16 -07001134 ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
1135 ioctl_standard_call,
1136 ioctl_private_call);
David S. Millera67fa762008-06-03 07:36:30 -07001137 if (ret >= 0 &&
1138 IW_IS_GET(cmd) &&
1139 copy_to_user(arg, ifr, sizeof(struct iwreq)))
Johannes Berg295f4a12007-04-26 20:43:56 -07001140 return -EFAULT;
David S. Millera67fa762008-06-03 07:36:30 -07001141
Johannes Berg295f4a12007-04-26 20:43:56 -07001142 return ret;
1143}
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001144
David S. Miller87de87d2008-06-03 09:14:03 -07001145#ifdef CONFIG_COMPAT
1146static int compat_standard_call(struct net_device *dev,
1147 struct iwreq *iwr,
1148 unsigned int cmd,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001149 struct iw_request_info *info,
David S. Miller87de87d2008-06-03 09:14:03 -07001150 iw_handler handler)
1151{
1152 const struct iw_ioctl_description *descr;
1153 struct compat_iw_point *iwp_compat;
David S. Miller87de87d2008-06-03 09:14:03 -07001154 struct iw_point iwp;
1155 int err;
1156
1157 descr = standard_ioctl + (cmd - SIOCIWFIRST);
1158
1159 if (descr->header_type != IW_HEADER_TYPE_POINT)
David S. Miller0f5cabb2008-06-03 07:39:16 -07001160 return ioctl_standard_call(dev, iwr, cmd, info, handler);
David S. Miller87de87d2008-06-03 09:14:03 -07001161
1162 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1163 iwp.pointer = compat_ptr(iwp_compat->pointer);
1164 iwp.length = iwp_compat->length;
1165 iwp.flags = iwp_compat->flags;
1166
David S. Miller0f5cabb2008-06-03 07:39:16 -07001167 err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
David S. Miller87de87d2008-06-03 09:14:03 -07001168
1169 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1170 iwp_compat->length = iwp.length;
1171 iwp_compat->flags = iwp.flags;
1172
1173 return err;
1174}
1175
1176static int compat_private_call(struct net_device *dev, struct iwreq *iwr,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001177 unsigned int cmd, struct iw_request_info *info,
1178 iw_handler handler)
David S. Miller87de87d2008-06-03 09:14:03 -07001179{
1180 const struct iw_priv_args *descr;
David S. Miller87de87d2008-06-03 09:14:03 -07001181 int ret, extra_size;
1182
1183 extra_size = get_priv_descr_and_size(dev, cmd, &descr);
1184
David S. Miller87de87d2008-06-03 09:14:03 -07001185 /* Check if we have a pointer to user space data or not. */
1186 if (extra_size == 0) {
1187 /* No extra arguments. Trivial to handle */
David S. Miller0f5cabb2008-06-03 07:39:16 -07001188 ret = handler(dev, info, &(iwr->u), (char *) &(iwr->u));
David S. Miller87de87d2008-06-03 09:14:03 -07001189 } else {
1190 struct compat_iw_point *iwp_compat;
1191 struct iw_point iwp;
1192
1193 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1194 iwp.pointer = compat_ptr(iwp_compat->pointer);
1195 iwp.length = iwp_compat->length;
1196 iwp.flags = iwp_compat->flags;
1197
1198 ret = ioctl_private_iw_point(&iwp, cmd, descr,
David S. Miller0f5cabb2008-06-03 07:39:16 -07001199 handler, dev, info, extra_size);
David S. Miller87de87d2008-06-03 09:14:03 -07001200
1201 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1202 iwp_compat->length = iwp.length;
1203 iwp_compat->flags = iwp.flags;
1204 }
1205
1206 /* Call commit handler if needed and defined */
1207 if (ret == -EIWCOMMIT)
1208 ret = call_commit_handler(dev);
1209
1210 return ret;
1211}
1212
1213int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1214 unsigned long arg)
1215{
1216 void __user *argp = (void __user *)arg;
David S. Miller0f5cabb2008-06-03 07:39:16 -07001217 struct iw_request_info info;
David S. Miller87de87d2008-06-03 09:14:03 -07001218 struct iwreq iwr;
1219 char *colon;
1220 int ret;
1221
1222 if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1223 return -EFAULT;
1224
1225 iwr.ifr_name[IFNAMSIZ-1] = 0;
1226 colon = strchr(iwr.ifr_name, ':');
1227 if (colon)
1228 *colon = 0;
1229
David S. Miller0f5cabb2008-06-03 07:39:16 -07001230 info.cmd = cmd;
1231 info.flags = IW_REQUEST_FLAG_COMPAT;
1232
1233 ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
David S. Miller87de87d2008-06-03 09:14:03 -07001234 compat_standard_call,
1235 compat_private_call);
1236
1237 if (ret >= 0 &&
1238 IW_IS_GET(cmd) &&
1239 copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1240 return -EFAULT;
1241
1242 return ret;
1243}
1244#endif
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/************************* EVENT PROCESSING *************************/
1247/*
1248 * Process events generated by the wireless layer or the driver.
1249 * Most often, the event will be propagated through rtnetlink
1250 */
1251
John W. Linvillebaef1862006-09-08 16:04:05 -04001252/* ---------------------------------------------------------------- */
1253/*
1254 * Locking...
1255 * ----------
1256 *
1257 * Thanks to Herbert Xu <herbert@gondor.apana.org.au> for fixing
1258 * the locking issue in here and implementing this code !
1259 *
1260 * The issue : wireless_send_event() is often called in interrupt context,
1261 * while the Netlink layer can never be called in interrupt context.
1262 * The fully formed RtNetlink events are queued, and then a tasklet is run
1263 * to feed those to Netlink.
1264 * The skb_queue is interrupt safe, and its lock is not held while calling
1265 * Netlink, so there is no possibility of dealock.
1266 * Jean II
1267 */
1268
Herbert Xu782a6672006-08-03 23:54:41 +10001269static struct sk_buff_head wireless_nlevent_queue;
1270
John W. Linvillebaef1862006-09-08 16:04:05 -04001271static int __init wireless_nlevent_init(void)
1272{
1273 skb_queue_head_init(&wireless_nlevent_queue);
1274 return 0;
1275}
1276
1277subsys_initcall(wireless_nlevent_init);
1278
Herbert Xu782a6672006-08-03 23:54:41 +10001279static void wireless_nlevent_process(unsigned long data)
1280{
1281 struct sk_buff *skb;
1282
1283 while ((skb = skb_dequeue(&wireless_nlevent_queue)))
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001284 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
Herbert Xu782a6672006-08-03 23:54:41 +10001285}
1286
1287static DECLARE_TASKLET(wireless_nlevent_tasklet, wireless_nlevent_process, 0);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/* ---------------------------------------------------------------- */
1290/*
1291 * Fill a rtnetlink message with our event data.
1292 * Note that we propage only the specified event and don't dump the
1293 * current wireless config. Dumping the wireless config is far too
1294 * expensive (for each parameter, the driver need to query the hardware).
1295 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001296static int rtnetlink_fill_iwinfo(struct sk_buff *skb, struct net_device *dev,
1297 int type, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 struct ifinfomsg *r;
1300 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Thomas Graf744b0962007-07-22 12:43:32 +02001302 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*r), 0);
1303 if (nlh == NULL)
1304 return -EMSGSIZE;
1305
1306 r = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 r->ifi_family = AF_UNSPEC;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001308 r->__ifi_pad = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 r->ifi_type = dev->type;
1310 r->ifi_index = dev->ifindex;
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001311 r->ifi_flags = dev_get_flags(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 r->ifi_change = 0; /* Wireless changes don't affect those flags */
1313
Jamal Hadi Salim317900c2008-08-13 02:39:56 -07001314 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* Add the wireless events in the netlink packet */
Thomas Graf744b0962007-07-22 12:43:32 +02001316 NLA_PUT(skb, IFLA_WIRELESS, event_len, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Thomas Graf744b0962007-07-22 12:43:32 +02001318 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Thomas Graf744b0962007-07-22 12:43:32 +02001320nla_put_failure:
1321 nlmsg_cancel(skb, nlh);
1322 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325/* ---------------------------------------------------------------- */
1326/*
1327 * Create and broadcast and send it on the standard rtnetlink socket
1328 * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
1329 * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
1330 * within a RTM_NEWLINK event.
1331 */
Johannes Bergbdf51892007-04-26 20:46:55 -07001332static void rtmsg_iwinfo(struct net_device *dev, char *event, int event_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct sk_buff *skb;
Thomas Graf744b0962007-07-22 12:43:32 +02001335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001337 if (!net_eq(dev_net(dev), &init_net))
Denis V. Lunev97c53ca2007-11-19 22:26:51 -08001338 return;
1339
Thomas Graf744b0962007-07-22 12:43:32 +02001340 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!skb)
1342 return;
1343
Thomas Graf744b0962007-07-22 12:43:32 +02001344 err = rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, event, event_len);
1345 if (err < 0) {
1346 WARN_ON(err == -EMSGSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 kfree_skb(skb);
1348 return;
1349 }
Thomas Graf744b0962007-07-22 12:43:32 +02001350
Patrick McHardyac6d4392005-08-14 19:29:52 -07001351 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
Herbert Xu782a6672006-08-03 23:54:41 +10001352 skb_queue_tail(&wireless_nlevent_queue, skb);
1353 tasklet_schedule(&wireless_nlevent_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
Herbert Xu782a6672006-08-03 23:54:41 +10001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/* ---------------------------------------------------------------- */
1357/*
1358 * Main event dispatcher. Called from other parts and drivers.
1359 * Send the event on the appropriate channels.
1360 * May be called from interrupt context.
1361 */
1362void wireless_send_event(struct net_device * dev,
1363 unsigned int cmd,
1364 union iwreq_data * wrqu,
1365 char * extra)
1366{
1367 const struct iw_ioctl_description * descr = NULL;
1368 int extra_len = 0;
1369 struct iw_event *event; /* Mallocated whole event */
1370 int event_len; /* Its size */
1371 int hdr_len; /* Size of the event header */
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001372 int wrqu_off = 0; /* Offset in wrqu */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* Don't "optimise" the following variable, it will crash */
1374 unsigned cmd_index; /* *MUST* be unsigned */
1375
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001376 /* Get the description of the Event */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001377 if (cmd <= SIOCIWLAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 cmd_index = cmd - SIOCIWFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001379 if (cmd_index < standard_ioctl_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 descr = &(standard_ioctl[cmd_index]);
1381 } else {
1382 cmd_index = cmd - IWEVFIRST;
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001383 if (cmd_index < standard_event_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 descr = &(standard_event[cmd_index]);
1385 }
1386 /* Don't accept unknown events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001387 if (descr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Note : we don't return an error to the driver, because
1389 * the driver would not know what to do about it. It can't
1390 * return an error to the user, because the event is not
1391 * initiated by a user request.
1392 * The best the driver could do is to log an error message.
1393 * We will do it ourselves instead...
1394 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001395 printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dev->name, cmd);
1397 return;
1398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /* Check extra parameters and set extra_len */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001401 if (descr->header_type == IW_HEADER_TYPE_POINT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* Check if number of token fits within bounds */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001403 if (wrqu->data.length > descr->max_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001404 printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return;
1406 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001407 if (wrqu->data.length < descr->min_tokens) {
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09001408 printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return;
1410 }
1411 /* Calculate extra_len - extra is NULL for restricted events */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001412 if (extra != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 extra_len = wrqu->data.length * descr->token_size;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001414 /* Always at an offset in wrqu */
1415 wrqu_off = IW_EV_POINT_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 /* Total length of the event */
1419 hdr_len = event_type_size[descr->header_type];
1420 event_len = hdr_len + extra_len;
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /* Create temporary buffer to hold the event */
1423 event = kmalloc(event_len, GFP_ATOMIC);
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001424 if (event == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return;
1426
1427 /* Fill event */
1428 event->len = event_len;
1429 event->cmd = cmd;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001430 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001431 if (extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 memcpy(((char *) event) + hdr_len, extra, extra_len);
1433
Jean Tourrilhes711e2c32006-02-22 15:10:56 -08001434 /* Send via the RtNetlink event channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 rtmsg_iwinfo(dev, (char *) event, event_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /* Cleanup */
1438 kfree(event);
1439
1440 return; /* Always success, I guess ;-) */
1441}
Johannes Berg7a9df162007-04-26 20:46:23 -07001442EXPORT_SYMBOL(wireless_send_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444/********************** ENHANCED IWSPY SUPPORT **********************/
1445/*
1446 * In the old days, the driver was handling spy support all by itself.
1447 * Now, the driver can delegate this task to Wireless Extensions.
1448 * It needs to use those standard spy iw_handler in struct iw_handler_def,
1449 * push data to us via wireless_spy_update() and include struct iw_spy_data
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001450 * in its private part (and export it in net_device->wireless_data->spy_data).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * One of the main advantage of centralising spy support here is that
1452 * it becomes much easier to improve and extend it without having to touch
1453 * the drivers. One example is the addition of the Spy-Threshold events.
1454 */
1455
1456/* ---------------------------------------------------------------- */
1457/*
1458 * Return the pointer to the spy data in the driver.
1459 * Because this is called on the Rx path via wireless_spy_update(),
1460 * we want it to be efficient...
1461 */
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001462static inline struct iw_spy_data *get_spydata(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 /* This is the new way */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001465 if (dev->wireless_data)
Johannes Berg4d44e0d2007-04-26 20:47:25 -07001466 return dev->wireless_data->spy_data;
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001467 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/*------------------------------------------------------------------*/
1471/*
1472 * Standard Wireless Handler : set Spy List
1473 */
1474int iw_handler_set_spy(struct net_device * dev,
1475 struct iw_request_info * info,
1476 union iwreq_data * wrqu,
1477 char * extra)
1478{
1479 struct iw_spy_data * spydata = get_spydata(dev);
1480 struct sockaddr * address = (struct sockaddr *) extra;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001483 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return -EOPNOTSUPP;
1485
1486 /* Disable spy collection while we copy the addresses.
1487 * While we copy addresses, any call to wireless_spy_update()
1488 * will NOP. This is OK, as anyway the addresses are changing. */
1489 spydata->spy_number = 0;
1490
1491 /* We want to operate without locking, because wireless_spy_update()
1492 * most likely will happen in the interrupt handler, and therefore
1493 * have its own locking constraints and needs performance.
1494 * The rtnl_lock() make sure we don't race with the other iw_handlers.
1495 * This make sure wireless_spy_update() "see" that the spy list
1496 * is temporarily disabled. */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001497 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* Are there are addresses to copy? */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001500 if (wrqu->data.length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int i;
1502
1503 /* Copy addresses */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001504 for (i = 0; i < wrqu->data.length; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 memcpy(spydata->spy_address[i], address[i].sa_data,
1506 ETH_ALEN);
1507 /* Reset stats */
1508 memset(spydata->spy_stat, 0,
1509 sizeof(struct iw_quality) * IW_MAX_SPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
1512 /* Make sure above is updated before re-enabling */
Ralf Baechlee16aa202006-12-07 00:11:33 -08001513 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* Enable addresses */
1516 spydata->spy_number = wrqu->data.length;
1517
1518 return 0;
1519}
Johannes Berg7a9df162007-04-26 20:46:23 -07001520EXPORT_SYMBOL(iw_handler_set_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522/*------------------------------------------------------------------*/
1523/*
1524 * Standard Wireless Handler : get Spy List
1525 */
1526int iw_handler_get_spy(struct net_device * dev,
1527 struct iw_request_info * info,
1528 union iwreq_data * wrqu,
1529 char * extra)
1530{
1531 struct iw_spy_data * spydata = get_spydata(dev);
1532 struct sockaddr * address = (struct sockaddr *) extra;
1533 int i;
1534
1535 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001536 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EOPNOTSUPP;
1538
1539 wrqu->data.length = spydata->spy_number;
1540
1541 /* Copy addresses. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001542 for (i = 0; i < spydata->spy_number; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1544 address[i].sa_family = AF_UNIX;
1545 }
1546 /* Copy stats to the user buffer (just after). */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001547 if (spydata->spy_number > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number),
1549 spydata->spy_stat,
1550 sizeof(struct iw_quality) * spydata->spy_number);
1551 /* Reset updated flags. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001552 for (i = 0; i < spydata->spy_number; i++)
Jean Tourrilhes6582c162005-09-02 11:32:28 -07001553 spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
Johannes Berg7a9df162007-04-26 20:46:23 -07001556EXPORT_SYMBOL(iw_handler_get_spy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558/*------------------------------------------------------------------*/
1559/*
1560 * Standard Wireless Handler : set spy threshold
1561 */
1562int iw_handler_set_thrspy(struct net_device * dev,
1563 struct iw_request_info *info,
1564 union iwreq_data * wrqu,
1565 char * extra)
1566{
1567 struct iw_spy_data * spydata = get_spydata(dev);
1568 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1569
1570 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001571 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return -EOPNOTSUPP;
1573
1574 /* Just do it */
1575 memcpy(&(spydata->spy_thr_low), &(threshold->low),
1576 2 * sizeof(struct iw_quality));
1577
1578 /* Clear flag */
1579 memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return 0;
1582}
Johannes Berg7a9df162007-04-26 20:46:23 -07001583EXPORT_SYMBOL(iw_handler_set_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585/*------------------------------------------------------------------*/
1586/*
1587 * Standard Wireless Handler : get spy threshold
1588 */
1589int iw_handler_get_thrspy(struct net_device * dev,
1590 struct iw_request_info *info,
1591 union iwreq_data * wrqu,
1592 char * extra)
1593{
1594 struct iw_spy_data * spydata = get_spydata(dev);
1595 struct iw_thrspy * threshold = (struct iw_thrspy *) extra;
1596
1597 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001598 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return -EOPNOTSUPP;
1600
1601 /* Just do it */
1602 memcpy(&(threshold->low), &(spydata->spy_thr_low),
1603 2 * sizeof(struct iw_quality));
1604
1605 return 0;
1606}
Johannes Berg7a9df162007-04-26 20:46:23 -07001607EXPORT_SYMBOL(iw_handler_get_thrspy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609/*------------------------------------------------------------------*/
1610/*
1611 * Prepare and send a Spy Threshold event
1612 */
1613static void iw_send_thrspy_event(struct net_device * dev,
1614 struct iw_spy_data * spydata,
1615 unsigned char * address,
1616 struct iw_quality * wstats)
1617{
1618 union iwreq_data wrqu;
1619 struct iw_thrspy threshold;
1620
1621 /* Init */
1622 wrqu.data.length = 1;
1623 wrqu.data.flags = 0;
1624 /* Copy address */
1625 memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1626 threshold.addr.sa_family = ARPHRD_ETHER;
1627 /* Copy stats */
1628 memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1629 /* Copy also thresholds */
1630 memcpy(&(threshold.low), &(spydata->spy_thr_low),
1631 2 * sizeof(struct iw_quality));
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Send event to user space */
1634 wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1635}
1636
1637/* ---------------------------------------------------------------- */
1638/*
1639 * Call for the driver to update the spy data.
1640 * For now, the spy data is a simple array. As the size of the array is
1641 * small, this is good enough. If we wanted to support larger number of
1642 * spy addresses, we should use something more efficient...
1643 */
1644void wireless_spy_update(struct net_device * dev,
1645 unsigned char * address,
1646 struct iw_quality * wstats)
1647{
1648 struct iw_spy_data * spydata = get_spydata(dev);
1649 int i;
1650 int match = -1;
1651
1652 /* Make sure driver is not buggy or using the old API */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001653 if (!spydata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* Update all records that match */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001657 for (i = 0; i < spydata->spy_number; i++)
1658 if (!compare_ether_addr(address, spydata->spy_address[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 memcpy(&(spydata->spy_stat[i]), wstats,
1660 sizeof(struct iw_quality));
1661 match = i;
1662 }
1663
1664 /* Generate an event if we cross the spy threshold.
1665 * To avoid event storms, we have a simple hysteresis : we generate
1666 * event only when we go under the low threshold or above the
1667 * high threshold. */
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001668 if (match >= 0) {
1669 if (spydata->spy_thr_under[match]) {
1670 if (wstats->level > spydata->spy_thr_high.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 spydata->spy_thr_under[match] = 0;
1672 iw_send_thrspy_event(dev, spydata,
1673 address, wstats);
1674 }
1675 } else {
Stephen Hemmingere71a4782007-04-10 20:10:33 -07001676 if (wstats->level < spydata->spy_thr_low.level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 spydata->spy_thr_under[match] = 1;
1678 iw_send_thrspy_event(dev, spydata,
1679 address, wstats);
1680 }
1681 }
1682 }
1683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684EXPORT_SYMBOL(wireless_spy_update);