]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * This file implement the Wireless Extensions APIs. | |
3 | * | |
4 | * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com> | |
711e2c33 | 5 | * Copyright (c) 1997-2006 Jean Tourrilhes, All Rights Reserved. |
1da177e4 LT |
6 | * |
7 | * (As all part of the Linux kernel, this file is GPL) | |
8 | */ | |
9 | ||
10 | /************************** DOCUMENTATION **************************/ | |
11 | /* | |
12 | * API definition : | |
13 | * -------------- | |
14 | * See <linux/wireless.h> for details of the APIs and the rest. | |
15 | * | |
16 | * History : | |
17 | * ------- | |
18 | * | |
19 | * v1 - 5.12.01 - Jean II | |
20 | * o Created this file. | |
21 | * | |
22 | * v2 - 13.12.01 - Jean II | |
23 | * o Move /proc/net/wireless stuff from net/core/dev.c to here | |
24 | * o Make Wireless Extension IOCTLs go through here | |
25 | * o Added iw_handler handling ;-) | |
26 | * o Added standard ioctl description | |
27 | * o Initial dumb commit strategy based on orinoco.c | |
28 | * | |
29 | * v3 - 19.12.01 - Jean II | |
30 | * o Make sure we don't go out of standard_ioctl[] in ioctl_standard_call | |
31 | * o Add event dispatcher function | |
32 | * o Add event description | |
33 | * o Propagate events as rtnetlink IFLA_WIRELESS option | |
34 | * o Generate event on selected SET requests | |
35 | * | |
36 | * v4 - 18.04.02 - Jean II | |
37 | * o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1 | |
38 | * | |
39 | * v5 - 21.06.02 - Jean II | |
40 | * o Add IW_PRIV_TYPE_ADDR in priv_type_size (+cleanup) | |
41 | * o Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes | |
42 | * o Add IWEVCUSTOM for driver specific event/scanning token | |
43 | * o Turn on WE_STRICT_WRITE by default + kernel warning | |
44 | * o Fix WE_STRICT_WRITE in ioctl_export_private() (32 => iw_num) | |
45 | * o Fix off-by-one in test (extra_size <= IFNAMSIZ) | |
46 | * | |
47 | * v6 - 9.01.03 - Jean II | |
48 | * o Add common spy support : iw_handler_set_spy(), wireless_spy_update() | |
49 | * o Add enhanced spy support : iw_handler_set_thrspy() and event. | |
50 | * o Add WIRELESS_EXT version display in /proc/net/wireless | |
51 | * | |
52 | * v6 - 18.06.04 - Jean II | |
53 | * o Change get_spydata() method for added safety | |
54 | * o Remove spy #ifdef, they are always on -> cleaner code | |
55 | * o Allow any size GET request if user specifies length > max | |
56 | * and if request has IW_DESCR_FLAG_NOMAX flag or is SIOCGIWPRIV | |
57 | * o Start migrating get_wireless_stats to struct iw_handler_def | |
58 | * o Add wmb() in iw_handler_set_spy() for non-coherent archs/cpus | |
59 | * Based on patch from Pavel Roskin <proski@gnu.org> : | |
60 | * o Fix kernel data leak to user space in private handler handling | |
6582c164 JT |
61 | * |
62 | * v7 - 18.3.05 - Jean II | |
63 | * o Remove (struct iw_point *)->pointer from events and streams | |
64 | * o Remove spy_offset from struct iw_handler_def | |
65 | * o Start deprecating dev->get_wireless_stats, output a warning | |
66 | * o If IW_QUAL_DBM is set, show dBm values in /proc/net/wireless | |
67 | * o Don't loose INVALID/DBM flags when clearing UPDATED flags (iwstats) | |
711e2c33 JT |
68 | * |
69 | * v8 - 17.02.06 - Jean II | |
70 | * o RtNetlink requests support (SET/GET) | |
1da177e4 LT |
71 | */ |
72 | ||
73 | /***************************** INCLUDES *****************************/ | |
74 | ||
75 | #include <linux/config.h> /* Not needed ??? */ | |
76 | #include <linux/module.h> | |
77 | #include <linux/types.h> /* off_t */ | |
78 | #include <linux/netdevice.h> /* struct ifreq, dev_get_by_name() */ | |
79 | #include <linux/proc_fs.h> | |
80 | #include <linux/rtnetlink.h> /* rtnetlink stuff */ | |
81 | #include <linux/seq_file.h> | |
82 | #include <linux/init.h> /* for __init */ | |
83 | #include <linux/if_arp.h> /* ARPHRD_ETHER */ | |
9819d85c | 84 | #include <linux/etherdevice.h> /* compare_ether_addr */ |
1da177e4 LT |
85 | |
86 | #include <linux/wireless.h> /* Pretty obvious */ | |
87 | #include <net/iw_handler.h> /* New driver API */ | |
88 | ||
89 | #include <asm/uaccess.h> /* copy_to_user() */ | |
90 | ||
91 | /**************************** CONSTANTS ****************************/ | |
92 | ||
93 | /* Debugging stuff */ | |
94 | #undef WE_IOCTL_DEBUG /* Debug IOCTL API */ | |
711e2c33 | 95 | #undef WE_RTNETLINK_DEBUG /* Debug RtNetlink API */ |
1da177e4 LT |
96 | #undef WE_EVENT_DEBUG /* Debug Event dispatcher */ |
97 | #undef WE_SPY_DEBUG /* Debug enhanced spy support */ | |
98 | ||
99 | /* Options */ | |
711e2c33 JT |
100 | //CONFIG_NET_WIRELESS_RTNETLINK /* Wireless requests over RtNetlink */ |
101 | #define WE_EVENT_RTNETLINK /* Propagate events using RtNetlink */ | |
1da177e4 LT |
102 | #define WE_SET_EVENT /* Generate an event on some set commands */ |
103 | ||
104 | /************************* GLOBAL VARIABLES *************************/ | |
105 | /* | |
106 | * You should not use global variables, because of re-entrancy. | |
107 | * On our case, it's only const, so it's OK... | |
108 | */ | |
109 | /* | |
110 | * Meta-data about all the standard Wireless Extension request we | |
111 | * know about. | |
112 | */ | |
113 | static const struct iw_ioctl_description standard_ioctl[] = { | |
114 | [SIOCSIWCOMMIT - SIOCIWFIRST] = { | |
115 | .header_type = IW_HEADER_TYPE_NULL, | |
116 | }, | |
117 | [SIOCGIWNAME - SIOCIWFIRST] = { | |
118 | .header_type = IW_HEADER_TYPE_CHAR, | |
119 | .flags = IW_DESCR_FLAG_DUMP, | |
120 | }, | |
121 | [SIOCSIWNWID - SIOCIWFIRST] = { | |
122 | .header_type = IW_HEADER_TYPE_PARAM, | |
123 | .flags = IW_DESCR_FLAG_EVENT, | |
124 | }, | |
125 | [SIOCGIWNWID - SIOCIWFIRST] = { | |
126 | .header_type = IW_HEADER_TYPE_PARAM, | |
127 | .flags = IW_DESCR_FLAG_DUMP, | |
128 | }, | |
129 | [SIOCSIWFREQ - SIOCIWFIRST] = { | |
130 | .header_type = IW_HEADER_TYPE_FREQ, | |
131 | .flags = IW_DESCR_FLAG_EVENT, | |
132 | }, | |
133 | [SIOCGIWFREQ - SIOCIWFIRST] = { | |
134 | .header_type = IW_HEADER_TYPE_FREQ, | |
135 | .flags = IW_DESCR_FLAG_DUMP, | |
136 | }, | |
137 | [SIOCSIWMODE - SIOCIWFIRST] = { | |
138 | .header_type = IW_HEADER_TYPE_UINT, | |
139 | .flags = IW_DESCR_FLAG_EVENT, | |
140 | }, | |
141 | [SIOCGIWMODE - SIOCIWFIRST] = { | |
142 | .header_type = IW_HEADER_TYPE_UINT, | |
143 | .flags = IW_DESCR_FLAG_DUMP, | |
144 | }, | |
145 | [SIOCSIWSENS - SIOCIWFIRST] = { | |
146 | .header_type = IW_HEADER_TYPE_PARAM, | |
147 | }, | |
148 | [SIOCGIWSENS - SIOCIWFIRST] = { | |
149 | .header_type = IW_HEADER_TYPE_PARAM, | |
150 | }, | |
151 | [SIOCSIWRANGE - SIOCIWFIRST] = { | |
152 | .header_type = IW_HEADER_TYPE_NULL, | |
153 | }, | |
154 | [SIOCGIWRANGE - SIOCIWFIRST] = { | |
155 | .header_type = IW_HEADER_TYPE_POINT, | |
156 | .token_size = 1, | |
157 | .max_tokens = sizeof(struct iw_range), | |
158 | .flags = IW_DESCR_FLAG_DUMP, | |
159 | }, | |
160 | [SIOCSIWPRIV - SIOCIWFIRST] = { | |
161 | .header_type = IW_HEADER_TYPE_NULL, | |
162 | }, | |
163 | [SIOCGIWPRIV - SIOCIWFIRST] = { /* (handled directly by us) */ | |
711e2c33 JT |
164 | .header_type = IW_HEADER_TYPE_POINT, |
165 | .token_size = sizeof(struct iw_priv_args), | |
166 | .max_tokens = 16, | |
167 | .flags = IW_DESCR_FLAG_NOMAX, | |
1da177e4 LT |
168 | }, |
169 | [SIOCSIWSTATS - SIOCIWFIRST] = { | |
170 | .header_type = IW_HEADER_TYPE_NULL, | |
171 | }, | |
172 | [SIOCGIWSTATS - SIOCIWFIRST] = { /* (handled directly by us) */ | |
711e2c33 JT |
173 | .header_type = IW_HEADER_TYPE_POINT, |
174 | .token_size = 1, | |
175 | .max_tokens = sizeof(struct iw_statistics), | |
1da177e4 LT |
176 | .flags = IW_DESCR_FLAG_DUMP, |
177 | }, | |
178 | [SIOCSIWSPY - SIOCIWFIRST] = { | |
179 | .header_type = IW_HEADER_TYPE_POINT, | |
180 | .token_size = sizeof(struct sockaddr), | |
181 | .max_tokens = IW_MAX_SPY, | |
182 | }, | |
183 | [SIOCGIWSPY - SIOCIWFIRST] = { | |
184 | .header_type = IW_HEADER_TYPE_POINT, | |
185 | .token_size = sizeof(struct sockaddr) + | |
186 | sizeof(struct iw_quality), | |
187 | .max_tokens = IW_MAX_SPY, | |
188 | }, | |
189 | [SIOCSIWTHRSPY - SIOCIWFIRST] = { | |
190 | .header_type = IW_HEADER_TYPE_POINT, | |
191 | .token_size = sizeof(struct iw_thrspy), | |
192 | .min_tokens = 1, | |
193 | .max_tokens = 1, | |
194 | }, | |
195 | [SIOCGIWTHRSPY - SIOCIWFIRST] = { | |
196 | .header_type = IW_HEADER_TYPE_POINT, | |
197 | .token_size = sizeof(struct iw_thrspy), | |
198 | .min_tokens = 1, | |
199 | .max_tokens = 1, | |
200 | }, | |
201 | [SIOCSIWAP - SIOCIWFIRST] = { | |
202 | .header_type = IW_HEADER_TYPE_ADDR, | |
203 | }, | |
204 | [SIOCGIWAP - SIOCIWFIRST] = { | |
205 | .header_type = IW_HEADER_TYPE_ADDR, | |
206 | .flags = IW_DESCR_FLAG_DUMP, | |
207 | }, | |
fff9cfd9 JT |
208 | [SIOCSIWMLME - SIOCIWFIRST] = { |
209 | .header_type = IW_HEADER_TYPE_POINT, | |
210 | .token_size = 1, | |
211 | .min_tokens = sizeof(struct iw_mlme), | |
212 | .max_tokens = sizeof(struct iw_mlme), | |
213 | }, | |
1da177e4 LT |
214 | [SIOCGIWAPLIST - SIOCIWFIRST] = { |
215 | .header_type = IW_HEADER_TYPE_POINT, | |
216 | .token_size = sizeof(struct sockaddr) + | |
217 | sizeof(struct iw_quality), | |
218 | .max_tokens = IW_MAX_AP, | |
219 | .flags = IW_DESCR_FLAG_NOMAX, | |
220 | }, | |
221 | [SIOCSIWSCAN - SIOCIWFIRST] = { | |
fff9cfd9 JT |
222 | .header_type = IW_HEADER_TYPE_POINT, |
223 | .token_size = 1, | |
224 | .min_tokens = 0, | |
225 | .max_tokens = sizeof(struct iw_scan_req), | |
1da177e4 LT |
226 | }, |
227 | [SIOCGIWSCAN - SIOCIWFIRST] = { | |
228 | .header_type = IW_HEADER_TYPE_POINT, | |
229 | .token_size = 1, | |
230 | .max_tokens = IW_SCAN_MAX_DATA, | |
231 | .flags = IW_DESCR_FLAG_NOMAX, | |
232 | }, | |
233 | [SIOCSIWESSID - SIOCIWFIRST] = { | |
234 | .header_type = IW_HEADER_TYPE_POINT, | |
235 | .token_size = 1, | |
236 | .max_tokens = IW_ESSID_MAX_SIZE + 1, | |
237 | .flags = IW_DESCR_FLAG_EVENT, | |
238 | }, | |
239 | [SIOCGIWESSID - SIOCIWFIRST] = { | |
240 | .header_type = IW_HEADER_TYPE_POINT, | |
241 | .token_size = 1, | |
242 | .max_tokens = IW_ESSID_MAX_SIZE + 1, | |
243 | .flags = IW_DESCR_FLAG_DUMP, | |
244 | }, | |
245 | [SIOCSIWNICKN - SIOCIWFIRST] = { | |
246 | .header_type = IW_HEADER_TYPE_POINT, | |
247 | .token_size = 1, | |
248 | .max_tokens = IW_ESSID_MAX_SIZE + 1, | |
249 | }, | |
250 | [SIOCGIWNICKN - SIOCIWFIRST] = { | |
251 | .header_type = IW_HEADER_TYPE_POINT, | |
252 | .token_size = 1, | |
253 | .max_tokens = IW_ESSID_MAX_SIZE + 1, | |
254 | }, | |
255 | [SIOCSIWRATE - SIOCIWFIRST] = { | |
256 | .header_type = IW_HEADER_TYPE_PARAM, | |
257 | }, | |
258 | [SIOCGIWRATE - SIOCIWFIRST] = { | |
259 | .header_type = IW_HEADER_TYPE_PARAM, | |
260 | }, | |
261 | [SIOCSIWRTS - SIOCIWFIRST] = { | |
262 | .header_type = IW_HEADER_TYPE_PARAM, | |
263 | }, | |
264 | [SIOCGIWRTS - SIOCIWFIRST] = { | |
265 | .header_type = IW_HEADER_TYPE_PARAM, | |
266 | }, | |
267 | [SIOCSIWFRAG - SIOCIWFIRST] = { | |
268 | .header_type = IW_HEADER_TYPE_PARAM, | |
269 | }, | |
270 | [SIOCGIWFRAG - SIOCIWFIRST] = { | |
271 | .header_type = IW_HEADER_TYPE_PARAM, | |
272 | }, | |
273 | [SIOCSIWTXPOW - SIOCIWFIRST] = { | |
274 | .header_type = IW_HEADER_TYPE_PARAM, | |
275 | }, | |
276 | [SIOCGIWTXPOW - SIOCIWFIRST] = { | |
277 | .header_type = IW_HEADER_TYPE_PARAM, | |
278 | }, | |
279 | [SIOCSIWRETRY - SIOCIWFIRST] = { | |
280 | .header_type = IW_HEADER_TYPE_PARAM, | |
281 | }, | |
282 | [SIOCGIWRETRY - SIOCIWFIRST] = { | |
283 | .header_type = IW_HEADER_TYPE_PARAM, | |
284 | }, | |
285 | [SIOCSIWENCODE - SIOCIWFIRST] = { | |
286 | .header_type = IW_HEADER_TYPE_POINT, | |
287 | .token_size = 1, | |
288 | .max_tokens = IW_ENCODING_TOKEN_MAX, | |
289 | .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT, | |
290 | }, | |
291 | [SIOCGIWENCODE - SIOCIWFIRST] = { | |
292 | .header_type = IW_HEADER_TYPE_POINT, | |
293 | .token_size = 1, | |
294 | .max_tokens = IW_ENCODING_TOKEN_MAX, | |
295 | .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT, | |
296 | }, | |
297 | [SIOCSIWPOWER - SIOCIWFIRST] = { | |
298 | .header_type = IW_HEADER_TYPE_PARAM, | |
299 | }, | |
300 | [SIOCGIWPOWER - SIOCIWFIRST] = { | |
301 | .header_type = IW_HEADER_TYPE_PARAM, | |
302 | }, | |
fff9cfd9 JT |
303 | [SIOCSIWGENIE - SIOCIWFIRST] = { |
304 | .header_type = IW_HEADER_TYPE_POINT, | |
305 | .token_size = 1, | |
306 | .max_tokens = IW_GENERIC_IE_MAX, | |
307 | }, | |
308 | [SIOCGIWGENIE - SIOCIWFIRST] = { | |
309 | .header_type = IW_HEADER_TYPE_POINT, | |
310 | .token_size = 1, | |
311 | .max_tokens = IW_GENERIC_IE_MAX, | |
312 | }, | |
313 | [SIOCSIWAUTH - SIOCIWFIRST] = { | |
314 | .header_type = IW_HEADER_TYPE_PARAM, | |
315 | }, | |
316 | [SIOCGIWAUTH - SIOCIWFIRST] = { | |
317 | .header_type = IW_HEADER_TYPE_PARAM, | |
318 | }, | |
319 | [SIOCSIWENCODEEXT - SIOCIWFIRST] = { | |
320 | .header_type = IW_HEADER_TYPE_POINT, | |
321 | .token_size = 1, | |
322 | .min_tokens = sizeof(struct iw_encode_ext), | |
323 | .max_tokens = sizeof(struct iw_encode_ext) + | |
324 | IW_ENCODING_TOKEN_MAX, | |
325 | }, | |
326 | [SIOCGIWENCODEEXT - SIOCIWFIRST] = { | |
327 | .header_type = IW_HEADER_TYPE_POINT, | |
328 | .token_size = 1, | |
329 | .min_tokens = sizeof(struct iw_encode_ext), | |
330 | .max_tokens = sizeof(struct iw_encode_ext) + | |
331 | IW_ENCODING_TOKEN_MAX, | |
332 | }, | |
333 | [SIOCSIWPMKSA - SIOCIWFIRST] = { | |
334 | .header_type = IW_HEADER_TYPE_POINT, | |
335 | .token_size = 1, | |
336 | .min_tokens = sizeof(struct iw_pmksa), | |
337 | .max_tokens = sizeof(struct iw_pmksa), | |
338 | }, | |
1da177e4 LT |
339 | }; |
340 | static const int standard_ioctl_num = (sizeof(standard_ioctl) / | |
341 | sizeof(struct iw_ioctl_description)); | |
342 | ||
343 | /* | |
344 | * Meta-data about all the additional standard Wireless Extension events | |
345 | * we know about. | |
346 | */ | |
347 | static 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] = { | |
363 | .header_type = IW_HEADER_TYPE_ADDR, | |
364 | }, | |
fff9cfd9 JT |
365 | [IWEVGENIE - IWEVFIRST] = { |
366 | .header_type = IW_HEADER_TYPE_POINT, | |
367 | .token_size = 1, | |
368 | .max_tokens = IW_GENERIC_IE_MAX, | |
369 | }, | |
370 | [IWEVMICHAELMICFAILURE - IWEVFIRST] = { | |
371 | .header_type = IW_HEADER_TYPE_POINT, | |
372 | .token_size = 1, | |
373 | .max_tokens = sizeof(struct iw_michaelmicfailure), | |
374 | }, | |
375 | [IWEVASSOCREQIE - IWEVFIRST] = { | |
376 | .header_type = IW_HEADER_TYPE_POINT, | |
377 | .token_size = 1, | |
378 | .max_tokens = IW_GENERIC_IE_MAX, | |
379 | }, | |
380 | [IWEVASSOCRESPIE - IWEVFIRST] = { | |
381 | .header_type = IW_HEADER_TYPE_POINT, | |
382 | .token_size = 1, | |
383 | .max_tokens = IW_GENERIC_IE_MAX, | |
384 | }, | |
385 | [IWEVPMKIDCAND - IWEVFIRST] = { | |
386 | .header_type = IW_HEADER_TYPE_POINT, | |
387 | .token_size = 1, | |
388 | .max_tokens = sizeof(struct iw_pmkid_cand), | |
389 | }, | |
1da177e4 LT |
390 | }; |
391 | static const int standard_event_num = (sizeof(standard_event) / | |
392 | sizeof(struct iw_ioctl_description)); | |
393 | ||
394 | /* Size (in bytes) of the various private data types */ | |
395 | static const char iw_priv_type_size[] = { | |
396 | 0, /* IW_PRIV_TYPE_NONE */ | |
397 | 1, /* IW_PRIV_TYPE_BYTE */ | |
398 | 1, /* IW_PRIV_TYPE_CHAR */ | |
399 | 0, /* Not defined */ | |
400 | sizeof(__u32), /* IW_PRIV_TYPE_INT */ | |
401 | sizeof(struct iw_freq), /* IW_PRIV_TYPE_FLOAT */ | |
402 | sizeof(struct sockaddr), /* IW_PRIV_TYPE_ADDR */ | |
403 | 0, /* Not defined */ | |
404 | }; | |
405 | ||
406 | /* Size (in bytes) of various events */ | |
407 | static const int event_type_size[] = { | |
408 | IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */ | |
409 | 0, | |
410 | IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */ | |
411 | 0, | |
412 | IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */ | |
413 | IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */ | |
414 | IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */ | |
415 | 0, | |
416 | IW_EV_POINT_LEN, /* Without variable payload */ | |
417 | IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */ | |
418 | IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */ | |
419 | }; | |
420 | ||
421 | /************************ 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. | |
430 | * Called from various place, so make sure it remains efficient. | |
431 | */ | |
432 | static inline iw_handler get_handler(struct net_device *dev, | |
433 | unsigned int cmd) | |
434 | { | |
435 | /* Don't "optimise" the following variable, it will crash */ | |
436 | unsigned int index; /* *MUST* be unsigned */ | |
437 | ||
438 | /* Check if we have some wireless handlers defined */ | |
439 | if(dev->wireless_handlers == NULL) | |
440 | return NULL; | |
441 | ||
442 | /* Try as a standard command */ | |
443 | index = cmd - SIOCIWFIRST; | |
444 | if(index < dev->wireless_handlers->num_standard) | |
445 | return dev->wireless_handlers->standard[index]; | |
446 | ||
447 | /* Try as a private command */ | |
448 | index = cmd - SIOCIWFIRSTPRIV; | |
449 | if(index < dev->wireless_handlers->num_private) | |
450 | return dev->wireless_handlers->private[index]; | |
451 | ||
452 | /* Not found */ | |
453 | return NULL; | |
454 | } | |
455 | ||
456 | /* ---------------------------------------------------------------- */ | |
457 | /* | |
458 | * Get statistics out of the driver | |
459 | */ | |
460 | static inline struct iw_statistics *get_wireless_stats(struct net_device *dev) | |
461 | { | |
462 | /* New location */ | |
463 | if((dev->wireless_handlers != NULL) && | |
464 | (dev->wireless_handlers->get_wireless_stats != NULL)) | |
465 | return dev->wireless_handlers->get_wireless_stats(dev); | |
466 | ||
6582c164 JT |
467 | /* Old location, field to be removed in next WE */ |
468 | if(dev->get_wireless_stats) { | |
35848e04 JG |
469 | static int printed_message; |
470 | ||
471 | if (!printed_message++) | |
472 | printk(KERN_DEBUG "%s (WE) : Driver using old /proc/net/wireless support, please fix driver !\n", | |
473 | dev->name); | |
474 | ||
6582c164 JT |
475 | return dev->get_wireless_stats(dev); |
476 | } | |
35848e04 | 477 | |
6582c164 JT |
478 | /* Not found */ |
479 | return (struct iw_statistics *) NULL; | |
1da177e4 LT |
480 | } |
481 | ||
482 | /* ---------------------------------------------------------------- */ | |
483 | /* | |
484 | * Call the commit handler in the driver | |
485 | * (if exist and if conditions are right) | |
486 | * | |
487 | * Note : our current commit strategy is currently pretty dumb, | |
488 | * but we will be able to improve on that... | |
489 | * The goal is to try to agreagate as many changes as possible | |
490 | * before doing the commit. Drivers that will define a commit handler | |
491 | * are usually those that need a reset after changing parameters, so | |
492 | * we want to minimise the number of reset. | |
493 | * A cool idea is to use a timer : at each "set" command, we re-set the | |
494 | * timer, when the timer eventually fires, we call the driver. | |
495 | * Hopefully, more on that later. | |
496 | * | |
497 | * Also, I'm waiting to see how many people will complain about the | |
498 | * netif_running(dev) test. I'm open on that one... | |
499 | * Hopefully, the driver will remember to do a commit in "open()" ;-) | |
500 | */ | |
501 | static inline int call_commit_handler(struct net_device * dev) | |
502 | { | |
503 | if((netif_running(dev)) && | |
504 | (dev->wireless_handlers->standard[0] != NULL)) { | |
505 | /* Call the commit handler on the driver */ | |
506 | return dev->wireless_handlers->standard[0](dev, NULL, | |
507 | NULL, NULL); | |
508 | } else | |
509 | return 0; /* Command completed successfully */ | |
510 | } | |
511 | ||
512 | /* ---------------------------------------------------------------- */ | |
513 | /* | |
514 | * Calculate size of private arguments | |
515 | */ | |
516 | static inline int get_priv_size(__u16 args) | |
517 | { | |
518 | int num = args & IW_PRIV_SIZE_MASK; | |
519 | int type = (args & IW_PRIV_TYPE_MASK) >> 12; | |
520 | ||
521 | return num * iw_priv_type_size[type]; | |
522 | } | |
523 | ||
524 | /* ---------------------------------------------------------------- */ | |
525 | /* | |
526 | * Re-calculate the size of private arguments | |
527 | */ | |
528 | static inline int adjust_priv_size(__u16 args, | |
529 | union iwreq_data * wrqu) | |
530 | { | |
531 | int num = wrqu->data.length; | |
532 | int max = args & IW_PRIV_SIZE_MASK; | |
533 | int type = (args & IW_PRIV_TYPE_MASK) >> 12; | |
534 | ||
535 | /* Make sure the driver doesn't goof up */ | |
536 | if (max < num) | |
537 | num = max; | |
538 | ||
539 | return num * iw_priv_type_size[type]; | |
540 | } | |
541 | ||
711e2c33 JT |
542 | /* ---------------------------------------------------------------- */ |
543 | /* | |
544 | * Standard Wireless Handler : get wireless stats | |
545 | * Allow programatic access to /proc/net/wireless even if /proc | |
546 | * doesn't exist... Also more efficient... | |
547 | */ | |
548 | static int iw_handler_get_iwstats(struct net_device * dev, | |
549 | struct iw_request_info * info, | |
550 | union iwreq_data * wrqu, | |
551 | char * extra) | |
552 | { | |
553 | /* Get stats from the driver */ | |
554 | struct iw_statistics *stats; | |
555 | ||
556 | stats = get_wireless_stats(dev); | |
557 | if (stats != (struct iw_statistics *) NULL) { | |
558 | ||
559 | /* Copy statistics to extra */ | |
560 | memcpy(extra, stats, sizeof(struct iw_statistics)); | |
561 | wrqu->data.length = sizeof(struct iw_statistics); | |
562 | ||
563 | /* Check if we need to clear the updated flag */ | |
564 | if(wrqu->data.flags != 0) | |
565 | stats->qual.updated &= ~IW_QUAL_ALL_UPDATED; | |
566 | return 0; | |
567 | } else | |
568 | return -EOPNOTSUPP; | |
569 | } | |
570 | ||
571 | /* ---------------------------------------------------------------- */ | |
572 | /* | |
573 | * Standard Wireless Handler : get iwpriv definitions | |
574 | * Export the driver private handler definition | |
575 | * They will be picked up by tools like iwpriv... | |
576 | */ | |
577 | static int iw_handler_get_private(struct net_device * dev, | |
578 | struct iw_request_info * info, | |
579 | union iwreq_data * wrqu, | |
580 | char * extra) | |
581 | { | |
582 | /* Check if the driver has something to export */ | |
583 | if((dev->wireless_handlers->num_private_args == 0) || | |
584 | (dev->wireless_handlers->private_args == NULL)) | |
585 | return -EOPNOTSUPP; | |
586 | ||
587 | /* Check if there is enough buffer up there */ | |
588 | if(wrqu->data.length < dev->wireless_handlers->num_private_args) { | |
589 | /* User space can't know in advance how large the buffer | |
590 | * needs to be. Give it a hint, so that we can support | |
591 | * any size buffer we want somewhat efficiently... */ | |
592 | wrqu->data.length = dev->wireless_handlers->num_private_args; | |
593 | return -E2BIG; | |
594 | } | |
595 | ||
596 | /* Set the number of available ioctls. */ | |
597 | wrqu->data.length = dev->wireless_handlers->num_private_args; | |
598 | ||
599 | /* Copy structure to the user buffer. */ | |
600 | memcpy(extra, dev->wireless_handlers->private_args, | |
601 | sizeof(struct iw_priv_args) * wrqu->data.length); | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
1da177e4 LT |
606 | |
607 | /******************** /proc/net/wireless SUPPORT ********************/ | |
608 | /* | |
609 | * The /proc/net/wireless file is a human readable user-space interface | |
610 | * exporting various wireless specific statistics from the wireless devices. | |
611 | * This is the most popular part of the Wireless Extensions ;-) | |
612 | * | |
613 | * This interface is a pure clone of /proc/net/dev (in net/core/dev.c). | |
614 | * The content of the file is basically the content of "struct iw_statistics". | |
615 | */ | |
616 | ||
617 | #ifdef CONFIG_PROC_FS | |
618 | ||
619 | /* ---------------------------------------------------------------- */ | |
620 | /* | |
621 | * Print one entry (line) of /proc/net/wireless | |
622 | */ | |
623 | static __inline__ void wireless_seq_printf_stats(struct seq_file *seq, | |
624 | struct net_device *dev) | |
625 | { | |
626 | /* Get stats from the driver */ | |
627 | struct iw_statistics *stats = get_wireless_stats(dev); | |
628 | ||
629 | if (stats) { | |
630 | seq_printf(seq, "%6s: %04x %3d%c %3d%c %3d%c %6d %6d %6d " | |
631 | "%6d %6d %6d\n", | |
632 | dev->name, stats->status, stats->qual.qual, | |
633 | stats->qual.updated & IW_QUAL_QUAL_UPDATED | |
634 | ? '.' : ' ', | |
6582c164 JT |
635 | ((__s32) stats->qual.level) - |
636 | ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0), | |
1da177e4 LT |
637 | stats->qual.updated & IW_QUAL_LEVEL_UPDATED |
638 | ? '.' : ' ', | |
6582c164 JT |
639 | ((__s32) stats->qual.noise) - |
640 | ((stats->qual.updated & IW_QUAL_DBM) ? 0x100 : 0), | |
1da177e4 LT |
641 | stats->qual.updated & IW_QUAL_NOISE_UPDATED |
642 | ? '.' : ' ', | |
643 | stats->discard.nwid, stats->discard.code, | |
644 | stats->discard.fragment, stats->discard.retries, | |
645 | stats->discard.misc, stats->miss.beacon); | |
6582c164 | 646 | stats->qual.updated &= ~IW_QUAL_ALL_UPDATED; |
1da177e4 LT |
647 | } |
648 | } | |
649 | ||
650 | /* ---------------------------------------------------------------- */ | |
651 | /* | |
652 | * Print info for /proc/net/wireless (print all entries) | |
653 | */ | |
654 | static int wireless_seq_show(struct seq_file *seq, void *v) | |
655 | { | |
656 | if (v == SEQ_START_TOKEN) | |
657 | seq_printf(seq, "Inter-| sta-| Quality | Discarded " | |
658 | "packets | Missed | WE\n" | |
659 | " face | tus | link level noise | nwid " | |
660 | "crypt frag retry misc | beacon | %d\n", | |
661 | WIRELESS_EXT); | |
662 | else | |
663 | wireless_seq_printf_stats(seq, v); | |
664 | return 0; | |
665 | } | |
666 | ||
1da177e4 LT |
667 | static struct seq_operations wireless_seq_ops = { |
668 | .start = dev_seq_start, | |
669 | .next = dev_seq_next, | |
670 | .stop = dev_seq_stop, | |
671 | .show = wireless_seq_show, | |
672 | }; | |
673 | ||
674 | static int wireless_seq_open(struct inode *inode, struct file *file) | |
675 | { | |
676 | return seq_open(file, &wireless_seq_ops); | |
677 | } | |
678 | ||
679 | static struct file_operations wireless_seq_fops = { | |
680 | .owner = THIS_MODULE, | |
681 | .open = wireless_seq_open, | |
682 | .read = seq_read, | |
683 | .llseek = seq_lseek, | |
684 | .release = seq_release, | |
685 | }; | |
686 | ||
687 | int __init wireless_proc_init(void) | |
688 | { | |
6582c164 | 689 | /* Create /proc/net/wireless entry */ |
1da177e4 LT |
690 | if (!proc_net_fops_create("wireless", S_IRUGO, &wireless_seq_fops)) |
691 | return -ENOMEM; | |
692 | ||
693 | return 0; | |
694 | } | |
695 | #endif /* CONFIG_PROC_FS */ | |
696 | ||
697 | /************************** IOCTL SUPPORT **************************/ | |
698 | /* | |
699 | * The original user space API to configure all those Wireless Extensions | |
700 | * is through IOCTLs. | |
701 | * In there, we check if we need to call the new driver API (iw_handler) | |
702 | * or just call the driver ioctl handler. | |
703 | */ | |
704 | ||
1da177e4 LT |
705 | /* ---------------------------------------------------------------- */ |
706 | /* | |
707 | * Wrapper to call a standard Wireless Extension handler. | |
708 | * We do various checks and also take care of moving data between | |
709 | * user space and kernel space. | |
710 | */ | |
711e2c33 JT |
711 | static int ioctl_standard_call(struct net_device * dev, |
712 | struct ifreq * ifr, | |
713 | unsigned int cmd, | |
714 | iw_handler handler) | |
1da177e4 LT |
715 | { |
716 | struct iwreq * iwr = (struct iwreq *) ifr; | |
717 | const struct iw_ioctl_description * descr; | |
718 | struct iw_request_info info; | |
719 | int ret = -EINVAL; | |
720 | ||
721 | /* Get the description of the IOCTL */ | |
722 | if((cmd - SIOCIWFIRST) >= standard_ioctl_num) | |
723 | return -EOPNOTSUPP; | |
724 | descr = &(standard_ioctl[cmd - SIOCIWFIRST]); | |
725 | ||
726 | #ifdef WE_IOCTL_DEBUG | |
727 | printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n", | |
728 | ifr->ifr_name, cmd); | |
729 | 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); | |
730 | #endif /* WE_IOCTL_DEBUG */ | |
731 | ||
732 | /* Prepare the call */ | |
733 | info.cmd = cmd; | |
734 | info.flags = 0; | |
735 | ||
736 | /* Check if we have a pointer to user space data or not */ | |
737 | if(descr->header_type != IW_HEADER_TYPE_POINT) { | |
738 | ||
739 | /* No extra arguments. Trivial to handle */ | |
740 | ret = handler(dev, &info, &(iwr->u), NULL); | |
741 | ||
742 | #ifdef WE_SET_EVENT | |
743 | /* Generate an event to notify listeners of the change */ | |
744 | if((descr->flags & IW_DESCR_FLAG_EVENT) && | |
745 | ((ret == 0) || (ret == -EIWCOMMIT))) | |
746 | wireless_send_event(dev, cmd, &(iwr->u), NULL); | |
747 | #endif /* WE_SET_EVENT */ | |
748 | } else { | |
749 | char * extra; | |
750 | int extra_size; | |
751 | int user_length = 0; | |
752 | int err; | |
753 | ||
754 | /* Calculate space needed by arguments. Always allocate | |
755 | * for max space. Easier, and won't last long... */ | |
756 | extra_size = descr->max_tokens * descr->token_size; | |
757 | ||
758 | /* Check what user space is giving us */ | |
759 | if(IW_IS_SET(cmd)) { | |
760 | /* Check NULL pointer */ | |
761 | if((iwr->u.data.pointer == NULL) && | |
762 | (iwr->u.data.length != 0)) | |
763 | return -EFAULT; | |
764 | /* Check if number of token fits within bounds */ | |
765 | if(iwr->u.data.length > descr->max_tokens) | |
766 | return -E2BIG; | |
767 | if(iwr->u.data.length < descr->min_tokens) | |
768 | return -EINVAL; | |
769 | } else { | |
770 | /* Check NULL pointer */ | |
771 | if(iwr->u.data.pointer == NULL) | |
772 | return -EFAULT; | |
773 | /* Save user space buffer size for checking */ | |
774 | user_length = iwr->u.data.length; | |
775 | ||
776 | /* Don't check if user_length > max to allow forward | |
777 | * compatibility. The test user_length < min is | |
778 | * implied by the test at the end. */ | |
779 | ||
780 | /* Support for very large requests */ | |
781 | if((descr->flags & IW_DESCR_FLAG_NOMAX) && | |
782 | (user_length > descr->max_tokens)) { | |
783 | /* Allow userspace to GET more than max so | |
784 | * we can support any size GET requests. | |
785 | * There is still a limit : -ENOMEM. */ | |
786 | extra_size = user_length * descr->token_size; | |
787 | /* Note : user_length is originally a __u16, | |
788 | * and token_size is controlled by us, | |
789 | * so extra_size won't get negative and | |
790 | * won't overflow... */ | |
791 | } | |
792 | } | |
793 | ||
794 | #ifdef WE_IOCTL_DEBUG | |
795 | printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n", | |
796 | dev->name, extra_size); | |
797 | #endif /* WE_IOCTL_DEBUG */ | |
798 | ||
799 | /* Create the kernel buffer */ | |
800 | extra = kmalloc(extra_size, GFP_KERNEL); | |
801 | if (extra == NULL) { | |
802 | return -ENOMEM; | |
803 | } | |
804 | ||
805 | /* If it is a SET, get all the extra data in here */ | |
806 | if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) { | |
807 | err = copy_from_user(extra, iwr->u.data.pointer, | |
808 | iwr->u.data.length * | |
809 | descr->token_size); | |
810 | if (err) { | |
811 | kfree(extra); | |
812 | return -EFAULT; | |
813 | } | |
814 | #ifdef WE_IOCTL_DEBUG | |
815 | printk(KERN_DEBUG "%s (WE) : Got %d bytes\n", | |
816 | dev->name, | |
817 | iwr->u.data.length * descr->token_size); | |
818 | #endif /* WE_IOCTL_DEBUG */ | |
819 | } | |
820 | ||
821 | /* Call the handler */ | |
822 | ret = handler(dev, &info, &(iwr->u), extra); | |
823 | ||
824 | /* If we have something to return to the user */ | |
825 | if (!ret && IW_IS_GET(cmd)) { | |
826 | /* Check if there is enough buffer up there */ | |
827 | if(user_length < iwr->u.data.length) { | |
828 | kfree(extra); | |
829 | return -E2BIG; | |
830 | } | |
831 | ||
832 | err = copy_to_user(iwr->u.data.pointer, extra, | |
833 | iwr->u.data.length * | |
834 | descr->token_size); | |
835 | if (err) | |
836 | ret = -EFAULT; | |
837 | #ifdef WE_IOCTL_DEBUG | |
838 | printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n", | |
839 | dev->name, | |
840 | iwr->u.data.length * descr->token_size); | |
841 | #endif /* WE_IOCTL_DEBUG */ | |
842 | } | |
843 | ||
844 | #ifdef WE_SET_EVENT | |
845 | /* Generate an event to notify listeners of the change */ | |
846 | if((descr->flags & IW_DESCR_FLAG_EVENT) && | |
847 | ((ret == 0) || (ret == -EIWCOMMIT))) { | |
848 | if(descr->flags & IW_DESCR_FLAG_RESTRICT) | |
849 | /* If the event is restricted, don't | |
850 | * export the payload */ | |
851 | wireless_send_event(dev, cmd, &(iwr->u), NULL); | |
852 | else | |
853 | wireless_send_event(dev, cmd, &(iwr->u), | |
854 | extra); | |
855 | } | |
856 | #endif /* WE_SET_EVENT */ | |
857 | ||
858 | /* Cleanup - I told you it wasn't that long ;-) */ | |
859 | kfree(extra); | |
860 | } | |
861 | ||
862 | /* Call commit handler if needed and defined */ | |
863 | if(ret == -EIWCOMMIT) | |
864 | ret = call_commit_handler(dev); | |
865 | ||
866 | /* Here, we will generate the appropriate event if needed */ | |
867 | ||
868 | return ret; | |
869 | } | |
870 | ||
871 | /* ---------------------------------------------------------------- */ | |
872 | /* | |
873 | * Wrapper to call a private Wireless Extension handler. | |
874 | * We do various checks and also take care of moving data between | |
875 | * user space and kernel space. | |
876 | * It's not as nice and slimline as the standard wrapper. The cause | |
877 | * is struct iw_priv_args, which was not really designed for the | |
878 | * job we are going here. | |
879 | * | |
880 | * IMPORTANT : This function prevent to set and get data on the same | |
881 | * IOCTL and enforce the SET/GET convention. Not doing it would be | |
882 | * far too hairy... | |
883 | * If you need to set and get data at the same time, please don't use | |
884 | * a iw_handler but process it in your ioctl handler (i.e. use the | |
885 | * old driver API). | |
886 | */ | |
887 | static inline int ioctl_private_call(struct net_device * dev, | |
888 | struct ifreq * ifr, | |
889 | unsigned int cmd, | |
890 | iw_handler handler) | |
891 | { | |
892 | struct iwreq * iwr = (struct iwreq *) ifr; | |
893 | const struct iw_priv_args * descr = NULL; | |
894 | struct iw_request_info info; | |
895 | int extra_size = 0; | |
896 | int i; | |
897 | int ret = -EINVAL; | |
898 | ||
899 | /* Get the description of the IOCTL */ | |
900 | for(i = 0; i < dev->wireless_handlers->num_private_args; i++) | |
901 | if(cmd == dev->wireless_handlers->private_args[i].cmd) { | |
902 | descr = &(dev->wireless_handlers->private_args[i]); | |
903 | break; | |
904 | } | |
905 | ||
906 | #ifdef WE_IOCTL_DEBUG | |
907 | printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n", | |
908 | ifr->ifr_name, cmd); | |
909 | if(descr) { | |
910 | printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n", | |
911 | dev->name, descr->name, | |
912 | descr->set_args, descr->get_args); | |
913 | } | |
914 | #endif /* WE_IOCTL_DEBUG */ | |
915 | ||
916 | /* Compute the size of the set/get arguments */ | |
917 | if(descr != NULL) { | |
918 | if(IW_IS_SET(cmd)) { | |
919 | int offset = 0; /* For sub-ioctls */ | |
920 | /* Check for sub-ioctl handler */ | |
921 | if(descr->name[0] == '\0') | |
922 | /* Reserve one int for sub-ioctl index */ | |
923 | offset = sizeof(__u32); | |
924 | ||
925 | /* Size of set arguments */ | |
926 | extra_size = get_priv_size(descr->set_args); | |
927 | ||
928 | /* Does it fits in iwr ? */ | |
929 | if((descr->set_args & IW_PRIV_SIZE_FIXED) && | |
930 | ((extra_size + offset) <= IFNAMSIZ)) | |
931 | extra_size = 0; | |
932 | } else { | |
933 | /* Size of get arguments */ | |
934 | extra_size = get_priv_size(descr->get_args); | |
935 | ||
936 | /* Does it fits in iwr ? */ | |
937 | if((descr->get_args & IW_PRIV_SIZE_FIXED) && | |
938 | (extra_size <= IFNAMSIZ)) | |
939 | extra_size = 0; | |
940 | } | |
941 | } | |
942 | ||
943 | /* Prepare the call */ | |
944 | info.cmd = cmd; | |
945 | info.flags = 0; | |
946 | ||
947 | /* Check if we have a pointer to user space data or not. */ | |
948 | if(extra_size == 0) { | |
949 | /* No extra arguments. Trivial to handle */ | |
950 | ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u)); | |
951 | } else { | |
952 | char * extra; | |
953 | int err; | |
954 | ||
955 | /* Check what user space is giving us */ | |
956 | if(IW_IS_SET(cmd)) { | |
957 | /* Check NULL pointer */ | |
958 | if((iwr->u.data.pointer == NULL) && | |
959 | (iwr->u.data.length != 0)) | |
960 | return -EFAULT; | |
961 | ||
962 | /* Does it fits within bounds ? */ | |
963 | if(iwr->u.data.length > (descr->set_args & | |
964 | IW_PRIV_SIZE_MASK)) | |
965 | return -E2BIG; | |
966 | } else { | |
967 | /* Check NULL pointer */ | |
968 | if(iwr->u.data.pointer == NULL) | |
969 | return -EFAULT; | |
970 | } | |
971 | ||
972 | #ifdef WE_IOCTL_DEBUG | |
973 | printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n", | |
974 | dev->name, extra_size); | |
975 | #endif /* WE_IOCTL_DEBUG */ | |
976 | ||
977 | /* Always allocate for max space. Easier, and won't last | |
978 | * long... */ | |
979 | extra = kmalloc(extra_size, GFP_KERNEL); | |
980 | if (extra == NULL) { | |
981 | return -ENOMEM; | |
982 | } | |
983 | ||
984 | /* If it is a SET, get all the extra data in here */ | |
985 | if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) { | |
986 | err = copy_from_user(extra, iwr->u.data.pointer, | |
987 | extra_size); | |
988 | if (err) { | |
989 | kfree(extra); | |
990 | return -EFAULT; | |
991 | } | |
992 | #ifdef WE_IOCTL_DEBUG | |
993 | printk(KERN_DEBUG "%s (WE) : Got %d elem\n", | |
994 | dev->name, iwr->u.data.length); | |
995 | #endif /* WE_IOCTL_DEBUG */ | |
996 | } | |
997 | ||
998 | /* Call the handler */ | |
999 | ret = handler(dev, &info, &(iwr->u), extra); | |
1000 | ||
1001 | /* If we have something to return to the user */ | |
1002 | if (!ret && IW_IS_GET(cmd)) { | |
1003 | ||
1004 | /* Adjust for the actual length if it's variable, | |
1005 | * avoid leaking kernel bits outside. */ | |
1006 | if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) { | |
1007 | extra_size = adjust_priv_size(descr->get_args, | |
1008 | &(iwr->u)); | |
1009 | } | |
1010 | ||
1011 | err = copy_to_user(iwr->u.data.pointer, extra, | |
1012 | extra_size); | |
1013 | if (err) | |
1014 | ret = -EFAULT; | |
1015 | #ifdef WE_IOCTL_DEBUG | |
1016 | printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n", | |
1017 | dev->name, iwr->u.data.length); | |
1018 | #endif /* WE_IOCTL_DEBUG */ | |
1019 | } | |
1020 | ||
1021 | /* Cleanup - I told you it wasn't that long ;-) */ | |
1022 | kfree(extra); | |
1023 | } | |
1024 | ||
1025 | ||
1026 | /* Call commit handler if needed and defined */ | |
1027 | if(ret == -EIWCOMMIT) | |
1028 | ret = call_commit_handler(dev); | |
1029 | ||
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | /* ---------------------------------------------------------------- */ | |
1034 | /* | |
1035 | * Main IOCTl dispatcher. Called from the main networking code | |
1036 | * (dev_ioctl() in net/core/dev.c). | |
1037 | * Check the type of IOCTL and call the appropriate wrapper... | |
1038 | */ | |
1039 | int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd) | |
1040 | { | |
1041 | struct net_device *dev; | |
1042 | iw_handler handler; | |
1043 | ||
1044 | /* Permissions are already checked in dev_ioctl() before calling us. | |
1045 | * The copy_to/from_user() of ifr is also dealt with in there */ | |
1046 | ||
1047 | /* Make sure the device exist */ | |
1048 | if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) | |
1049 | return -ENODEV; | |
1050 | ||
1051 | /* A bunch of special cases, then the generic case... | |
1052 | * Note that 'cmd' is already filtered in dev_ioctl() with | |
1053 | * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */ | |
1054 | switch(cmd) | |
1055 | { | |
1056 | case SIOCGIWSTATS: | |
1057 | /* Get Wireless Stats */ | |
711e2c33 JT |
1058 | return ioctl_standard_call(dev, |
1059 | ifr, | |
1060 | cmd, | |
1061 | &iw_handler_get_iwstats); | |
1da177e4 LT |
1062 | |
1063 | case SIOCGIWPRIV: | |
1064 | /* Check if we have some wireless handlers defined */ | |
1065 | if(dev->wireless_handlers != NULL) { | |
1066 | /* We export to user space the definition of | |
1067 | * the private handler ourselves */ | |
711e2c33 JT |
1068 | return ioctl_standard_call(dev, |
1069 | ifr, | |
1070 | cmd, | |
1071 | &iw_handler_get_private); | |
1da177e4 LT |
1072 | } |
1073 | // ## Fall-through for old API ## | |
1074 | default: | |
1075 | /* Generic IOCTL */ | |
1076 | /* Basic check */ | |
1077 | if (!netif_device_present(dev)) | |
1078 | return -ENODEV; | |
1079 | /* New driver API : try to find the handler */ | |
1080 | handler = get_handler(dev, cmd); | |
1081 | if(handler != NULL) { | |
1082 | /* Standard and private are not the same */ | |
1083 | if(cmd < SIOCIWFIRSTPRIV) | |
1084 | return ioctl_standard_call(dev, | |
1085 | ifr, | |
1086 | cmd, | |
1087 | handler); | |
1088 | else | |
1089 | return ioctl_private_call(dev, | |
1090 | ifr, | |
1091 | cmd, | |
1092 | handler); | |
1093 | } | |
1094 | /* Old driver API : call driver ioctl handler */ | |
1095 | if (dev->do_ioctl) { | |
1096 | return dev->do_ioctl(dev, ifr, cmd); | |
1097 | } | |
1098 | return -EOPNOTSUPP; | |
1099 | } | |
1100 | /* Not reached */ | |
1101 | return -EINVAL; | |
1102 | } | |
1103 | ||
711e2c33 JT |
1104 | /********************** RTNETLINK REQUEST API **********************/ |
1105 | /* | |
1106 | * The alternate user space API to configure all those Wireless Extensions | |
1107 | * is through RtNetlink. | |
1108 | * This API support only the new driver API (iw_handler). | |
1109 | * | |
1110 | * This RtNetlink API use the same query/reply model as the ioctl API. | |
1111 | * Maximum effort has been done to fit in the RtNetlink model, and | |
1112 | * we support both RtNetlink Set and RtNelink Get operations. | |
1113 | * On the other hand, we don't offer Dump operations because of the | |
1114 | * following reasons : | |
1115 | * o Large number of parameters, most optional | |
1116 | * o Large size of some parameters (> 100 bytes) | |
1117 | * o Each parameters need to be extracted from hardware | |
1118 | * o Scan requests can take seconds and disable network activity. | |
1119 | * Because of this high cost/overhead, we want to return only the | |
1120 | * parameters the user application is really interested in. | |
1121 | * We could offer partial Dump using the IW_DESCR_FLAG_DUMP flag. | |
1122 | * | |
1123 | * The API uses the standard RtNetlink socket. When the RtNetlink code | |
1124 | * find a IFLA_WIRELESS field in a RtNetlink SET_LINK request, | |
1125 | * it calls here. | |
1126 | */ | |
1127 | ||
1128 | #ifdef CONFIG_NET_WIRELESS_RTNETLINK | |
1129 | /* ---------------------------------------------------------------- */ | |
1130 | /* | |
1131 | * Wrapper to call a standard Wireless Extension GET handler. | |
1132 | * We do various checks and call the handler with the proper args. | |
1133 | */ | |
1134 | static int rtnetlink_standard_get(struct net_device * dev, | |
1135 | struct iw_event * request, | |
1136 | int request_len, | |
1137 | iw_handler handler, | |
1138 | char ** p_buf, | |
1139 | int * p_len) | |
1140 | { | |
1141 | const struct iw_ioctl_description * descr = NULL; | |
1142 | unsigned int cmd; | |
1143 | union iwreq_data * wrqu; | |
1144 | int hdr_len; | |
1145 | struct iw_request_info info; | |
1146 | char * buffer = NULL; | |
1147 | int buffer_size = 0; | |
1148 | int ret = -EINVAL; | |
1149 | ||
1150 | /* Get the description of the Request */ | |
1151 | cmd = request->cmd; | |
1152 | if((cmd - SIOCIWFIRST) >= standard_ioctl_num) | |
1153 | return -EOPNOTSUPP; | |
1154 | descr = &(standard_ioctl[cmd - SIOCIWFIRST]); | |
1155 | ||
1156 | #ifdef WE_RTNETLINK_DEBUG | |
1157 | printk(KERN_DEBUG "%s (WE.r) : Found standard handler for 0x%04X\n", | |
1158 | dev->name, cmd); | |
1159 | 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); | |
1160 | #endif /* WE_RTNETLINK_DEBUG */ | |
1161 | ||
1162 | /* Check if wrqu is complete */ | |
1163 | hdr_len = event_type_size[descr->header_type]; | |
1164 | if(request_len < hdr_len) { | |
1165 | #ifdef WE_RTNETLINK_DEBUG | |
1166 | printk(KERN_DEBUG | |
1167 | "%s (WE.r) : Wireless request too short (%d)\n", | |
1168 | dev->name, request_len); | |
1169 | #endif /* WE_RTNETLINK_DEBUG */ | |
1170 | return -EINVAL; | |
1171 | } | |
1172 | ||
1173 | /* Prepare the call */ | |
1174 | info.cmd = cmd; | |
1175 | info.flags = 0; | |
1176 | ||
1177 | /* Check if we have extra data in the reply or not */ | |
1178 | if(descr->header_type != IW_HEADER_TYPE_POINT) { | |
1179 | ||
1180 | /* Create the kernel buffer that we will return. | |
1181 | * It's at an offset to match the TYPE_POINT case... */ | |
1182 | buffer_size = request_len + IW_EV_POINT_OFF; | |
1183 | buffer = kmalloc(buffer_size, GFP_KERNEL); | |
1184 | if (buffer == NULL) { | |
1185 | return -ENOMEM; | |
1186 | } | |
1187 | /* Copy event data */ | |
1188 | memcpy(buffer + IW_EV_POINT_OFF, request, request_len); | |
1189 | /* Use our own copy of wrqu */ | |
1190 | wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF | |
1191 | + IW_EV_LCP_LEN); | |
1192 | ||
1193 | /* No extra arguments. Trivial to handle */ | |
1194 | ret = handler(dev, &info, wrqu, NULL); | |
1195 | ||
1196 | } else { | |
1197 | union iwreq_data wrqu_point; | |
1198 | char * extra = NULL; | |
1199 | int extra_size = 0; | |
1200 | ||
1201 | /* Get a temp copy of wrqu (skip pointer) */ | |
1202 | memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF, | |
1203 | ((char *) request) + IW_EV_LCP_LEN, | |
1204 | IW_EV_POINT_LEN - IW_EV_LCP_LEN); | |
1205 | ||
1206 | /* Calculate space needed by arguments. Always allocate | |
1207 | * for max space. Easier, and won't last long... */ | |
1208 | extra_size = descr->max_tokens * descr->token_size; | |
1209 | /* Support for very large requests */ | |
1210 | if((descr->flags & IW_DESCR_FLAG_NOMAX) && | |
1211 | (wrqu_point.data.length > descr->max_tokens)) | |
1212 | extra_size = (wrqu_point.data.length | |
1213 | * descr->token_size); | |
1214 | buffer_size = extra_size + IW_EV_POINT_LEN + IW_EV_POINT_OFF; | |
1215 | #ifdef WE_RTNETLINK_DEBUG | |
1216 | printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n", | |
1217 | dev->name, extra_size, buffer_size); | |
1218 | #endif /* WE_RTNETLINK_DEBUG */ | |
1219 | ||
1220 | /* Create the kernel buffer that we will return */ | |
1221 | buffer = kmalloc(buffer_size, GFP_KERNEL); | |
1222 | if (buffer == NULL) { | |
1223 | return -ENOMEM; | |
1224 | } | |
1225 | ||
1226 | /* Put wrqu in the right place (just before extra). | |
1227 | * Leave space for IWE header and dummy pointer... | |
1228 | * Note that IW_EV_LCP_LEN==4 bytes, so it's still aligned... | |
1229 | */ | |
1230 | memcpy(buffer + IW_EV_LCP_LEN + IW_EV_POINT_OFF, | |
1231 | ((char *) &wrqu_point) + IW_EV_POINT_OFF, | |
1232 | IW_EV_POINT_LEN - IW_EV_LCP_LEN); | |
1233 | wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_LEN); | |
1234 | ||
1235 | /* Extra comes logically after that. Offset +12 bytes. */ | |
1236 | extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_LEN; | |
1237 | ||
1238 | /* Call the handler */ | |
1239 | ret = handler(dev, &info, wrqu, extra); | |
1240 | ||
1241 | /* Calculate real returned length */ | |
1242 | extra_size = (wrqu->data.length * descr->token_size); | |
1243 | /* Re-adjust reply size */ | |
1244 | request->len = extra_size + IW_EV_POINT_LEN; | |
1245 | ||
1246 | /* Put the iwe header where it should, i.e. scrap the | |
1247 | * dummy pointer. */ | |
1248 | memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_LEN); | |
1249 | ||
1250 | #ifdef WE_RTNETLINK_DEBUG | |
1251 | 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); | |
1252 | #endif /* WE_RTNETLINK_DEBUG */ | |
1253 | ||
1254 | /* Check if there is enough buffer up there */ | |
1255 | if(wrqu_point.data.length < wrqu->data.length) | |
1256 | ret = -E2BIG; | |
1257 | } | |
1258 | ||
1259 | /* Return the buffer to the caller */ | |
1260 | if (!ret) { | |
1261 | *p_buf = buffer; | |
1262 | *p_len = request->len; | |
1263 | } else { | |
1264 | /* Cleanup */ | |
1265 | if(buffer) | |
1266 | kfree(buffer); | |
1267 | } | |
1268 | ||
1269 | return ret; | |
1270 | } | |
1271 | ||
1272 | /* ---------------------------------------------------------------- */ | |
1273 | /* | |
1274 | * Wrapper to call a standard Wireless Extension SET handler. | |
1275 | * We do various checks and call the handler with the proper args. | |
1276 | */ | |
1277 | static inline int rtnetlink_standard_set(struct net_device * dev, | |
1278 | struct iw_event * request, | |
1279 | int request_len, | |
1280 | iw_handler handler) | |
1281 | { | |
1282 | const struct iw_ioctl_description * descr = NULL; | |
1283 | unsigned int cmd; | |
1284 | union iwreq_data * wrqu; | |
1285 | union iwreq_data wrqu_point; | |
1286 | int hdr_len; | |
1287 | char * extra = NULL; | |
1288 | int extra_size = 0; | |
1289 | struct iw_request_info info; | |
1290 | int ret = -EINVAL; | |
1291 | ||
1292 | /* Get the description of the Request */ | |
1293 | cmd = request->cmd; | |
1294 | if((cmd - SIOCIWFIRST) >= standard_ioctl_num) | |
1295 | return -EOPNOTSUPP; | |
1296 | descr = &(standard_ioctl[cmd - SIOCIWFIRST]); | |
1297 | ||
1298 | #ifdef WE_RTNETLINK_DEBUG | |
1299 | printk(KERN_DEBUG "%s (WE.r) : Found standard SET handler for 0x%04X\n", | |
1300 | dev->name, cmd); | |
1301 | 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); | |
1302 | #endif /* WE_RTNETLINK_DEBUG */ | |
1303 | ||
1304 | /* Extract fixed header from request. This is properly aligned. */ | |
1305 | wrqu = &request->u; | |
1306 | ||
1307 | /* Check if wrqu is complete */ | |
1308 | hdr_len = event_type_size[descr->header_type]; | |
1309 | if(request_len < hdr_len) { | |
1310 | #ifdef WE_RTNETLINK_DEBUG | |
1311 | printk(KERN_DEBUG | |
1312 | "%s (WE.r) : Wireless request too short (%d)\n", | |
1313 | dev->name, request_len); | |
1314 | #endif /* WE_RTNETLINK_DEBUG */ | |
1315 | return -EINVAL; | |
1316 | } | |
1317 | ||
1318 | /* Prepare the call */ | |
1319 | info.cmd = cmd; | |
1320 | info.flags = 0; | |
1321 | ||
1322 | /* Check if we have extra data in the request or not */ | |
1323 | if(descr->header_type != IW_HEADER_TYPE_POINT) { | |
1324 | ||
1325 | /* No extra arguments. Trivial to handle */ | |
1326 | ret = handler(dev, &info, wrqu, NULL); | |
1327 | ||
1328 | } else { | |
1329 | int extra_len; | |
1330 | ||
1331 | /* Put wrqu in the right place (skip pointer) */ | |
1332 | memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF, | |
1333 | wrqu, IW_EV_POINT_LEN - IW_EV_LCP_LEN); | |
1334 | /* Don't forget about the event code... */ | |
1335 | wrqu = &wrqu_point; | |
1336 | ||
1337 | /* Check if number of token fits within bounds */ | |
1338 | if(wrqu_point.data.length > descr->max_tokens) | |
1339 | return -E2BIG; | |
1340 | if(wrqu_point.data.length < descr->min_tokens) | |
1341 | return -EINVAL; | |
1342 | ||
1343 | /* Real length of payload */ | |
1344 | extra_len = wrqu_point.data.length * descr->token_size; | |
1345 | ||
1346 | /* Check if request is self consistent */ | |
1347 | if((request_len - hdr_len) < extra_len) { | |
1348 | #ifdef WE_RTNETLINK_DEBUG | |
1349 | printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n", | |
1350 | dev->name, extra_size); | |
1351 | #endif /* WE_RTNETLINK_DEBUG */ | |
1352 | return -EINVAL; | |
1353 | } | |
1354 | ||
1355 | #ifdef WE_RTNETLINK_DEBUG | |
1356 | printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n", | |
1357 | dev->name, extra_size); | |
1358 | #endif /* WE_RTNETLINK_DEBUG */ | |
1359 | ||
1360 | /* Always allocate for max space. Easier, and won't last | |
1361 | * long... */ | |
1362 | extra_size = descr->max_tokens * descr->token_size; | |
1363 | extra = kmalloc(extra_size, GFP_KERNEL); | |
1364 | if (extra == NULL) | |
1365 | return -ENOMEM; | |
1366 | ||
1367 | /* Copy extra in aligned buffer */ | |
1368 | memcpy(extra, ((char *) request) + hdr_len, extra_len); | |
1369 | ||
1370 | /* Call the handler */ | |
1371 | ret = handler(dev, &info, &wrqu_point, extra); | |
1372 | } | |
1373 | ||
1374 | #ifdef WE_SET_EVENT | |
1375 | /* Generate an event to notify listeners of the change */ | |
1376 | if((descr->flags & IW_DESCR_FLAG_EVENT) && | |
1377 | ((ret == 0) || (ret == -EIWCOMMIT))) { | |
1378 | if(descr->flags & IW_DESCR_FLAG_RESTRICT) | |
1379 | /* If the event is restricted, don't | |
1380 | * export the payload */ | |
1381 | wireless_send_event(dev, cmd, wrqu, NULL); | |
1382 | else | |
1383 | wireless_send_event(dev, cmd, wrqu, extra); | |
1384 | } | |
1385 | #endif /* WE_SET_EVENT */ | |
1386 | ||
1387 | /* Cleanup - I told you it wasn't that long ;-) */ | |
1388 | if(extra) | |
1389 | kfree(extra); | |
1390 | ||
1391 | /* Call commit handler if needed and defined */ | |
1392 | if(ret == -EIWCOMMIT) | |
1393 | ret = call_commit_handler(dev); | |
1394 | ||
1395 | return ret; | |
1396 | } | |
1397 | ||
1398 | /* ---------------------------------------------------------------- */ | |
1399 | /* | |
1400 | * Wrapper to call a private Wireless Extension GET handler. | |
1401 | * Same as above... | |
1402 | * It's not as nice and slimline as the standard wrapper. The cause | |
1403 | * is struct iw_priv_args, which was not really designed for the | |
1404 | * job we are going here. | |
1405 | * | |
1406 | * IMPORTANT : This function prevent to set and get data on the same | |
1407 | * IOCTL and enforce the SET/GET convention. Not doing it would be | |
1408 | * far too hairy... | |
1409 | * If you need to set and get data at the same time, please don't use | |
1410 | * a iw_handler but process it in your ioctl handler (i.e. use the | |
1411 | * old driver API). | |
1412 | */ | |
1413 | static inline int rtnetlink_private_get(struct net_device * dev, | |
1414 | struct iw_event * request, | |
1415 | int request_len, | |
1416 | iw_handler handler, | |
1417 | char ** p_buf, | |
1418 | int * p_len) | |
1419 | { | |
1420 | const struct iw_priv_args * descr = NULL; | |
1421 | unsigned int cmd; | |
1422 | union iwreq_data * wrqu; | |
1423 | int hdr_len; | |
1424 | struct iw_request_info info; | |
1425 | int extra_size = 0; | |
1426 | int i; | |
1427 | char * buffer = NULL; | |
1428 | int buffer_size = 0; | |
1429 | int ret = -EINVAL; | |
1430 | ||
1431 | /* Get the description of the Request */ | |
1432 | cmd = request->cmd; | |
1433 | for(i = 0; i < dev->wireless_handlers->num_private_args; i++) | |
1434 | if(cmd == dev->wireless_handlers->private_args[i].cmd) { | |
1435 | descr = &(dev->wireless_handlers->private_args[i]); | |
1436 | break; | |
1437 | } | |
1438 | if(descr == NULL) | |
1439 | return -EOPNOTSUPP; | |
1440 | ||
1441 | #ifdef WE_RTNETLINK_DEBUG | |
1442 | printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n", | |
1443 | dev->name, cmd); | |
1444 | printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n", | |
1445 | dev->name, descr->name, descr->set_args, descr->get_args); | |
1446 | #endif /* WE_RTNETLINK_DEBUG */ | |
1447 | ||
1448 | /* Compute the max size of the get arguments */ | |
1449 | extra_size = get_priv_size(descr->get_args); | |
1450 | ||
1451 | /* Does it fits in wrqu ? */ | |
1452 | if((descr->get_args & IW_PRIV_SIZE_FIXED) && | |
1453 | (extra_size <= IFNAMSIZ)) { | |
1454 | hdr_len = extra_size; | |
1455 | extra_size = 0; | |
1456 | } else { | |
1457 | hdr_len = IW_EV_POINT_LEN; | |
1458 | } | |
1459 | ||
1460 | /* Check if wrqu is complete */ | |
1461 | if(request_len < hdr_len) { | |
1462 | #ifdef WE_RTNETLINK_DEBUG | |
1463 | printk(KERN_DEBUG | |
1464 | "%s (WE.r) : Wireless request too short (%d)\n", | |
1465 | dev->name, request_len); | |
1466 | #endif /* WE_RTNETLINK_DEBUG */ | |
1467 | return -EINVAL; | |
1468 | } | |
1469 | ||
1470 | /* Prepare the call */ | |
1471 | info.cmd = cmd; | |
1472 | info.flags = 0; | |
1473 | ||
1474 | /* Check if we have a pointer to user space data or not. */ | |
1475 | if(extra_size == 0) { | |
1476 | ||
1477 | /* Create the kernel buffer that we will return. | |
1478 | * It's at an offset to match the TYPE_POINT case... */ | |
1479 | buffer_size = request_len + IW_EV_POINT_OFF; | |
1480 | buffer = kmalloc(buffer_size, GFP_KERNEL); | |
1481 | if (buffer == NULL) { | |
1482 | return -ENOMEM; | |
1483 | } | |
1484 | /* Copy event data */ | |
1485 | memcpy(buffer + IW_EV_POINT_OFF, request, request_len); | |
1486 | /* Use our own copy of wrqu */ | |
1487 | wrqu = (union iwreq_data *) (buffer + IW_EV_POINT_OFF | |
1488 | + IW_EV_LCP_LEN); | |
1489 | ||
1490 | /* No extra arguments. Trivial to handle */ | |
1491 | ret = handler(dev, &info, wrqu, (char *) wrqu); | |
1492 | ||
1493 | } else { | |
1494 | char * extra; | |
1495 | ||
1496 | /* Buffer for full reply */ | |
1497 | buffer_size = extra_size + IW_EV_POINT_LEN + IW_EV_POINT_OFF; | |
1498 | ||
1499 | #ifdef WE_RTNETLINK_DEBUG | |
1500 | printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes (%d bytes)\n", | |
1501 | dev->name, extra_size, buffer_size); | |
1502 | #endif /* WE_RTNETLINK_DEBUG */ | |
1503 | ||
1504 | /* Create the kernel buffer that we will return */ | |
1505 | buffer = kmalloc(buffer_size, GFP_KERNEL); | |
1506 | if (buffer == NULL) { | |
1507 | return -ENOMEM; | |
1508 | } | |
1509 | ||
1510 | /* Put wrqu in the right place (just before extra). | |
1511 | * Leave space for IWE header and dummy pointer... | |
1512 | * Note that IW_EV_LCP_LEN==4 bytes, so it's still aligned... | |
1513 | */ | |
1514 | memcpy(buffer + IW_EV_LCP_LEN + IW_EV_POINT_OFF, | |
1515 | ((char *) request) + IW_EV_LCP_LEN, | |
1516 | IW_EV_POINT_LEN - IW_EV_LCP_LEN); | |
1517 | wrqu = (union iwreq_data *) (buffer + IW_EV_LCP_LEN); | |
1518 | ||
1519 | /* Extra comes logically after that. Offset +12 bytes. */ | |
1520 | extra = buffer + IW_EV_POINT_OFF + IW_EV_POINT_LEN; | |
1521 | ||
1522 | /* Call the handler */ | |
1523 | ret = handler(dev, &info, wrqu, extra); | |
1524 | ||
1525 | /* Adjust for the actual length if it's variable, | |
1526 | * avoid leaking kernel bits outside. */ | |
1527 | if (!(descr->get_args & IW_PRIV_SIZE_FIXED)) | |
1528 | extra_size = adjust_priv_size(descr->get_args, wrqu); | |
1529 | /* Re-adjust reply size */ | |
1530 | request->len = extra_size + IW_EV_POINT_LEN; | |
1531 | ||
1532 | /* Put the iwe header where it should, i.e. scrap the | |
1533 | * dummy pointer. */ | |
1534 | memcpy(buffer + IW_EV_POINT_OFF, request, IW_EV_LCP_LEN); | |
1535 | ||
1536 | #ifdef WE_RTNETLINK_DEBUG | |
1537 | 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); | |
1538 | #endif /* WE_RTNETLINK_DEBUG */ | |
1539 | } | |
1540 | ||
1541 | /* Return the buffer to the caller */ | |
1542 | if (!ret) { | |
1543 | *p_buf = buffer; | |
1544 | *p_len = request->len; | |
1545 | } else { | |
1546 | /* Cleanup */ | |
1547 | if(buffer) | |
1548 | kfree(buffer); | |
1549 | } | |
1550 | ||
1551 | return ret; | |
1552 | } | |
1553 | ||
1554 | /* ---------------------------------------------------------------- */ | |
1555 | /* | |
1556 | * Wrapper to call a private Wireless Extension SET handler. | |
1557 | * Same as above... | |
1558 | * It's not as nice and slimline as the standard wrapper. The cause | |
1559 | * is struct iw_priv_args, which was not really designed for the | |
1560 | * job we are going here. | |
1561 | * | |
1562 | * IMPORTANT : This function prevent to set and get data on the same | |
1563 | * IOCTL and enforce the SET/GET convention. Not doing it would be | |
1564 | * far too hairy... | |
1565 | * If you need to set and get data at the same time, please don't use | |
1566 | * a iw_handler but process it in your ioctl handler (i.e. use the | |
1567 | * old driver API). | |
1568 | */ | |
1569 | static inline int rtnetlink_private_set(struct net_device * dev, | |
1570 | struct iw_event * request, | |
1571 | int request_len, | |
1572 | iw_handler handler) | |
1573 | { | |
1574 | const struct iw_priv_args * descr = NULL; | |
1575 | unsigned int cmd; | |
1576 | union iwreq_data * wrqu; | |
1577 | union iwreq_data wrqu_point; | |
1578 | int hdr_len; | |
1579 | char * extra = NULL; | |
1580 | int extra_size = 0; | |
1581 | int offset = 0; /* For sub-ioctls */ | |
1582 | struct iw_request_info info; | |
1583 | int i; | |
1584 | int ret = -EINVAL; | |
1585 | ||
1586 | /* Get the description of the Request */ | |
1587 | cmd = request->cmd; | |
1588 | for(i = 0; i < dev->wireless_handlers->num_private_args; i++) | |
1589 | if(cmd == dev->wireless_handlers->private_args[i].cmd) { | |
1590 | descr = &(dev->wireless_handlers->private_args[i]); | |
1591 | break; | |
1592 | } | |
1593 | if(descr == NULL) | |
1594 | return -EOPNOTSUPP; | |
1595 | ||
1596 | #ifdef WE_RTNETLINK_DEBUG | |
1597 | printk(KERN_DEBUG "%s (WE.r) : Found private handler for 0x%04X\n", | |
1598 | ifr->ifr_name, cmd); | |
1599 | printk(KERN_DEBUG "%s (WE.r) : Name %s, set %X, get %X\n", | |
1600 | dev->name, descr->name, descr->set_args, descr->get_args); | |
1601 | #endif /* WE_RTNETLINK_DEBUG */ | |
1602 | ||
1603 | /* Compute the size of the set arguments */ | |
1604 | /* Check for sub-ioctl handler */ | |
1605 | if(descr->name[0] == '\0') | |
1606 | /* Reserve one int for sub-ioctl index */ | |
1607 | offset = sizeof(__u32); | |
1608 | ||
1609 | /* Size of set arguments */ | |
1610 | extra_size = get_priv_size(descr->set_args); | |
1611 | ||
1612 | /* Does it fits in wrqu ? */ | |
1613 | if((descr->set_args & IW_PRIV_SIZE_FIXED) && | |
1614 | (extra_size <= IFNAMSIZ)) { | |
1615 | hdr_len = IW_EV_LCP_LEN + extra_size; | |
1616 | extra_size = 0; | |
1617 | } else { | |
1618 | hdr_len = IW_EV_POINT_LEN; | |
1619 | } | |
1620 | ||
1621 | /* Extract fixed header from request. This is properly aligned. */ | |
1622 | wrqu = &request->u; | |
1623 | ||
1624 | /* Check if wrqu is complete */ | |
1625 | if(request_len < hdr_len) { | |
1626 | #ifdef WE_RTNETLINK_DEBUG | |
1627 | printk(KERN_DEBUG | |
1628 | "%s (WE.r) : Wireless request too short (%d)\n", | |
1629 | dev->name, request_len); | |
1630 | #endif /* WE_RTNETLINK_DEBUG */ | |
1631 | return -EINVAL; | |
1632 | } | |
1633 | ||
1634 | /* Prepare the call */ | |
1635 | info.cmd = cmd; | |
1636 | info.flags = 0; | |
1637 | ||
1638 | /* Check if we have a pointer to user space data or not. */ | |
1639 | if(extra_size == 0) { | |
1640 | ||
1641 | /* No extra arguments. Trivial to handle */ | |
1642 | ret = handler(dev, &info, wrqu, (char *) wrqu); | |
1643 | ||
1644 | } else { | |
1645 | int extra_len; | |
1646 | ||
1647 | /* Put wrqu in the right place (skip pointer) */ | |
1648 | memcpy(((char *) &wrqu_point) + IW_EV_POINT_OFF, | |
1649 | wrqu, IW_EV_POINT_LEN - IW_EV_LCP_LEN); | |
1650 | ||
1651 | /* Does it fits within bounds ? */ | |
1652 | if(wrqu_point.data.length > (descr->set_args & | |
1653 | IW_PRIV_SIZE_MASK)) | |
1654 | return -E2BIG; | |
1655 | ||
1656 | /* Real length of payload */ | |
1657 | extra_len = adjust_priv_size(descr->set_args, &wrqu_point); | |
1658 | ||
1659 | /* Check if request is self consistent */ | |
1660 | if((request_len - hdr_len) < extra_len) { | |
1661 | #ifdef WE_RTNETLINK_DEBUG | |
1662 | printk(KERN_DEBUG "%s (WE.r) : Wireless request data too short (%d)\n", | |
1663 | dev->name, extra_size); | |
1664 | #endif /* WE_RTNETLINK_DEBUG */ | |
1665 | return -EINVAL; | |
1666 | } | |
1667 | ||
1668 | #ifdef WE_RTNETLINK_DEBUG | |
1669 | printk(KERN_DEBUG "%s (WE.r) : Malloc %d bytes\n", | |
1670 | dev->name, extra_size); | |
1671 | #endif /* WE_RTNETLINK_DEBUG */ | |
1672 | ||
1673 | /* Always allocate for max space. Easier, and won't last | |
1674 | * long... */ | |
1675 | extra = kmalloc(extra_size, GFP_KERNEL); | |
1676 | if (extra == NULL) | |
1677 | return -ENOMEM; | |
1678 | ||
1679 | /* Copy extra in aligned buffer */ | |
1680 | memcpy(extra, ((char *) request) + hdr_len, extra_len); | |
1681 | ||
1682 | /* Call the handler */ | |
1683 | ret = handler(dev, &info, &wrqu_point, extra); | |
1684 | ||
1685 | /* Cleanup - I told you it wasn't that long ;-) */ | |
1686 | kfree(extra); | |
1687 | } | |
1688 | ||
1689 | /* Call commit handler if needed and defined */ | |
1690 | if(ret == -EIWCOMMIT) | |
1691 | ret = call_commit_handler(dev); | |
1692 | ||
1693 | return ret; | |
1694 | } | |
1695 | ||
1696 | /* ---------------------------------------------------------------- */ | |
1697 | /* | |
1698 | * Main RtNetlink dispatcher. Called from the main networking code | |
1699 | * (do_getlink() in net/core/rtnetlink.c). | |
1700 | * Check the type of Request and call the appropriate wrapper... | |
1701 | */ | |
1702 | int wireless_rtnetlink_get(struct net_device * dev, | |
1703 | char * data, | |
1704 | int len, | |
1705 | char ** p_buf, | |
1706 | int * p_len) | |
1707 | { | |
1708 | struct iw_event * request = (struct iw_event *) data; | |
1709 | iw_handler handler; | |
1710 | ||
1711 | /* Check length */ | |
1712 | if(len < IW_EV_LCP_LEN) { | |
1713 | printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n", | |
1714 | dev->name, len); | |
1715 | return -EINVAL; | |
1716 | } | |
1717 | ||
1718 | /* ReCheck length (len may have padding) */ | |
1719 | if(request->len > len) { | |
1720 | printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n", | |
1721 | dev->name, request->len, len); | |
1722 | return -EINVAL; | |
1723 | } | |
1724 | ||
1725 | /* Only accept GET requests in here */ | |
1726 | if(!IW_IS_GET(request->cmd)) | |
1727 | return -EOPNOTSUPP; | |
1728 | ||
1729 | /* Special cases */ | |
1730 | if(request->cmd == SIOCGIWSTATS) | |
1731 | /* Get Wireless Stats */ | |
1732 | return rtnetlink_standard_get(dev, | |
1733 | request, | |
1734 | request->len, | |
1735 | &iw_handler_get_iwstats, | |
1736 | p_buf, p_len); | |
1737 | if(request->cmd == SIOCGIWPRIV) { | |
1738 | /* Check if we have some wireless handlers defined */ | |
1739 | if(dev->wireless_handlers == NULL) | |
1740 | return -EOPNOTSUPP; | |
1741 | /* Get Wireless Stats */ | |
1742 | return rtnetlink_standard_get(dev, | |
1743 | request, | |
1744 | request->len, | |
1745 | &iw_handler_get_private, | |
1746 | p_buf, p_len); | |
1747 | } | |
1748 | ||
1749 | /* Basic check */ | |
1750 | if (!netif_device_present(dev)) | |
1751 | return -ENODEV; | |
1752 | ||
1753 | /* Try to find the handler */ | |
1754 | handler = get_handler(dev, request->cmd); | |
1755 | if(handler != NULL) { | |
1756 | /* Standard and private are not the same */ | |
1757 | if(request->cmd < SIOCIWFIRSTPRIV) | |
1758 | return rtnetlink_standard_get(dev, | |
1759 | request, | |
1760 | request->len, | |
1761 | handler, | |
1762 | p_buf, p_len); | |
1763 | else | |
1764 | return rtnetlink_private_get(dev, | |
1765 | request, | |
1766 | request->len, | |
1767 | handler, | |
1768 | p_buf, p_len); | |
1769 | } | |
1770 | ||
1771 | return -EOPNOTSUPP; | |
1772 | } | |
1773 | ||
1774 | /* ---------------------------------------------------------------- */ | |
1775 | /* | |
1776 | * Main RtNetlink dispatcher. Called from the main networking code | |
1777 | * (do_setlink() in net/core/rtnetlink.c). | |
1778 | * Check the type of Request and call the appropriate wrapper... | |
1779 | */ | |
1780 | int wireless_rtnetlink_set(struct net_device * dev, | |
1781 | char * data, | |
1782 | int len) | |
1783 | { | |
1784 | struct iw_event * request = (struct iw_event *) data; | |
1785 | iw_handler handler; | |
1786 | ||
1787 | /* Check length */ | |
1788 | if(len < IW_EV_LCP_LEN) { | |
1789 | printk(KERN_DEBUG "%s (WE.r) : RtNetlink request too short (%d)\n", | |
1790 | dev->name, len); | |
1791 | return -EINVAL; | |
1792 | } | |
1793 | ||
1794 | /* ReCheck length (len may have padding) */ | |
1795 | if(request->len > len) { | |
1796 | printk(KERN_DEBUG "%s (WE.r) : RtNetlink request len invalid (%d-%d)\n", | |
1797 | dev->name, request->len, len); | |
1798 | return -EINVAL; | |
1799 | } | |
1800 | ||
1801 | /* Only accept SET requests in here */ | |
1802 | if(!IW_IS_SET(request->cmd)) | |
1803 | return -EOPNOTSUPP; | |
1804 | ||
1805 | /* Basic check */ | |
1806 | if (!netif_device_present(dev)) | |
1807 | return -ENODEV; | |
1808 | ||
1809 | /* New driver API : try to find the handler */ | |
1810 | handler = get_handler(dev, request->cmd); | |
1811 | if(handler != NULL) { | |
1812 | /* Standard and private are not the same */ | |
1813 | if(request->cmd < SIOCIWFIRSTPRIV) | |
1814 | return rtnetlink_standard_set(dev, | |
1815 | request, | |
1816 | request->len, | |
1817 | handler); | |
1818 | else | |
1819 | return rtnetlink_private_set(dev, | |
1820 | request, | |
1821 | request->len, | |
1822 | handler); | |
1823 | } | |
1824 | ||
1825 | return -EOPNOTSUPP; | |
1826 | } | |
1827 | #endif /* CONFIG_NET_WIRELESS_RTNETLINK */ | |
1828 | ||
1829 | ||
1da177e4 LT |
1830 | /************************* EVENT PROCESSING *************************/ |
1831 | /* | |
1832 | * Process events generated by the wireless layer or the driver. | |
1833 | * Most often, the event will be propagated through rtnetlink | |
1834 | */ | |
1835 | ||
711e2c33 | 1836 | #ifdef WE_EVENT_RTNETLINK |
1da177e4 LT |
1837 | /* ---------------------------------------------------------------- */ |
1838 | /* | |
1839 | * Fill a rtnetlink message with our event data. | |
1840 | * Note that we propage only the specified event and don't dump the | |
1841 | * current wireless config. Dumping the wireless config is far too | |
1842 | * expensive (for each parameter, the driver need to query the hardware). | |
1843 | */ | |
1844 | static inline int rtnetlink_fill_iwinfo(struct sk_buff * skb, | |
1845 | struct net_device * dev, | |
1846 | int type, | |
1847 | char * event, | |
1848 | int event_len) | |
1849 | { | |
1850 | struct ifinfomsg *r; | |
1851 | struct nlmsghdr *nlh; | |
1852 | unsigned char *b = skb->tail; | |
1853 | ||
1854 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r)); | |
1855 | r = NLMSG_DATA(nlh); | |
1856 | r->ifi_family = AF_UNSPEC; | |
9ef1d4c7 | 1857 | r->__ifi_pad = 0; |
1da177e4 LT |
1858 | r->ifi_type = dev->type; |
1859 | r->ifi_index = dev->ifindex; | |
711e2c33 | 1860 | r->ifi_flags = dev_get_flags(dev); |
1da177e4 LT |
1861 | r->ifi_change = 0; /* Wireless changes don't affect those flags */ |
1862 | ||
1863 | /* Add the wireless events in the netlink packet */ | |
711e2c33 | 1864 | RTA_PUT(skb, IFLA_WIRELESS, event_len, event); |
1da177e4 LT |
1865 | |
1866 | nlh->nlmsg_len = skb->tail - b; | |
1867 | return skb->len; | |
1868 | ||
1869 | nlmsg_failure: | |
1870 | rtattr_failure: | |
1871 | skb_trim(skb, b - skb->data); | |
1872 | return -1; | |
1873 | } | |
1874 | ||
1875 | /* ---------------------------------------------------------------- */ | |
1876 | /* | |
1877 | * Create and broadcast and send it on the standard rtnetlink socket | |
1878 | * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c | |
1879 | * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field | |
1880 | * within a RTM_NEWLINK event. | |
1881 | */ | |
1882 | static inline void rtmsg_iwinfo(struct net_device * dev, | |
1883 | char * event, | |
1884 | int event_len) | |
1885 | { | |
1886 | struct sk_buff *skb; | |
1887 | int size = NLMSG_GOODSIZE; | |
1888 | ||
1889 | skb = alloc_skb(size, GFP_ATOMIC); | |
1890 | if (!skb) | |
1891 | return; | |
1892 | ||
1893 | if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK, | |
1894 | event, event_len) < 0) { | |
1895 | kfree_skb(skb); | |
1896 | return; | |
1897 | } | |
ac6d439d PM |
1898 | NETLINK_CB(skb).dst_group = RTNLGRP_LINK; |
1899 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_ATOMIC); | |
1da177e4 | 1900 | } |
711e2c33 | 1901 | #endif /* WE_EVENT_RTNETLINK */ |
1da177e4 LT |
1902 | |
1903 | /* ---------------------------------------------------------------- */ | |
1904 | /* | |
1905 | * Main event dispatcher. Called from other parts and drivers. | |
1906 | * Send the event on the appropriate channels. | |
1907 | * May be called from interrupt context. | |
1908 | */ | |
1909 | void wireless_send_event(struct net_device * dev, | |
1910 | unsigned int cmd, | |
1911 | union iwreq_data * wrqu, | |
1912 | char * extra) | |
1913 | { | |
1914 | const struct iw_ioctl_description * descr = NULL; | |
1915 | int extra_len = 0; | |
1916 | struct iw_event *event; /* Mallocated whole event */ | |
1917 | int event_len; /* Its size */ | |
1918 | int hdr_len; /* Size of the event header */ | |
6582c164 | 1919 | int wrqu_off = 0; /* Offset in wrqu */ |
1da177e4 LT |
1920 | /* Don't "optimise" the following variable, it will crash */ |
1921 | unsigned cmd_index; /* *MUST* be unsigned */ | |
1922 | ||
6582c164 | 1923 | /* Get the description of the Event */ |
1da177e4 LT |
1924 | if(cmd <= SIOCIWLAST) { |
1925 | cmd_index = cmd - SIOCIWFIRST; | |
1926 | if(cmd_index < standard_ioctl_num) | |
1927 | descr = &(standard_ioctl[cmd_index]); | |
1928 | } else { | |
1929 | cmd_index = cmd - IWEVFIRST; | |
1930 | if(cmd_index < standard_event_num) | |
1931 | descr = &(standard_event[cmd_index]); | |
1932 | } | |
1933 | /* Don't accept unknown events */ | |
1934 | if(descr == NULL) { | |
1935 | /* Note : we don't return an error to the driver, because | |
1936 | * the driver would not know what to do about it. It can't | |
1937 | * return an error to the user, because the event is not | |
1938 | * initiated by a user request. | |
1939 | * The best the driver could do is to log an error message. | |
1940 | * We will do it ourselves instead... | |
1941 | */ | |
1942 | printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n", | |
1943 | dev->name, cmd); | |
1944 | return; | |
1945 | } | |
1946 | #ifdef WE_EVENT_DEBUG | |
1947 | printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n", | |
1948 | dev->name, cmd); | |
1949 | 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); | |
1950 | #endif /* WE_EVENT_DEBUG */ | |
1951 | ||
1952 | /* Check extra parameters and set extra_len */ | |
1953 | if(descr->header_type == IW_HEADER_TYPE_POINT) { | |
1954 | /* Check if number of token fits within bounds */ | |
1955 | if(wrqu->data.length > descr->max_tokens) { | |
1956 | printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length); | |
1957 | return; | |
1958 | } | |
1959 | if(wrqu->data.length < descr->min_tokens) { | |
1960 | printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length); | |
1961 | return; | |
1962 | } | |
1963 | /* Calculate extra_len - extra is NULL for restricted events */ | |
1964 | if(extra != NULL) | |
1965 | extra_len = wrqu->data.length * descr->token_size; | |
6582c164 JT |
1966 | /* Always at an offset in wrqu */ |
1967 | wrqu_off = IW_EV_POINT_OFF; | |
1da177e4 LT |
1968 | #ifdef WE_EVENT_DEBUG |
1969 | printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len); | |
1970 | #endif /* WE_EVENT_DEBUG */ | |
1971 | } | |
1972 | ||
1973 | /* Total length of the event */ | |
1974 | hdr_len = event_type_size[descr->header_type]; | |
1975 | event_len = hdr_len + extra_len; | |
1976 | ||
1977 | #ifdef WE_EVENT_DEBUG | |
6582c164 | 1978 | 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); |
1da177e4 LT |
1979 | #endif /* WE_EVENT_DEBUG */ |
1980 | ||
1981 | /* Create temporary buffer to hold the event */ | |
1982 | event = kmalloc(event_len, GFP_ATOMIC); | |
1983 | if(event == NULL) | |
1984 | return; | |
1985 | ||
1986 | /* Fill event */ | |
1987 | event->len = event_len; | |
1988 | event->cmd = cmd; | |
6582c164 | 1989 | memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN); |
1da177e4 LT |
1990 | if(extra != NULL) |
1991 | memcpy(((char *) event) + hdr_len, extra, extra_len); | |
1992 | ||
711e2c33 JT |
1993 | #ifdef WE_EVENT_RTNETLINK |
1994 | /* Send via the RtNetlink event channel */ | |
1da177e4 | 1995 | rtmsg_iwinfo(dev, (char *) event, event_len); |
711e2c33 | 1996 | #endif /* WE_EVENT_RTNETLINK */ |
1da177e4 LT |
1997 | |
1998 | /* Cleanup */ | |
1999 | kfree(event); | |
2000 | ||
2001 | return; /* Always success, I guess ;-) */ | |
2002 | } | |
2003 | ||
2004 | /********************** ENHANCED IWSPY SUPPORT **********************/ | |
2005 | /* | |
2006 | * In the old days, the driver was handling spy support all by itself. | |
2007 | * Now, the driver can delegate this task to Wireless Extensions. | |
2008 | * It needs to use those standard spy iw_handler in struct iw_handler_def, | |
2009 | * push data to us via wireless_spy_update() and include struct iw_spy_data | |
6582c164 | 2010 | * in its private part (and export it in net_device->wireless_data->spy_data). |
1da177e4 LT |
2011 | * One of the main advantage of centralising spy support here is that |
2012 | * it becomes much easier to improve and extend it without having to touch | |
2013 | * the drivers. One example is the addition of the Spy-Threshold events. | |
2014 | */ | |
2015 | ||
2016 | /* ---------------------------------------------------------------- */ | |
2017 | /* | |
2018 | * Return the pointer to the spy data in the driver. | |
2019 | * Because this is called on the Rx path via wireless_spy_update(), | |
2020 | * we want it to be efficient... | |
2021 | */ | |
2022 | static inline struct iw_spy_data * get_spydata(struct net_device *dev) | |
2023 | { | |
2024 | /* This is the new way */ | |
2025 | if(dev->wireless_data) | |
2026 | return(dev->wireless_data->spy_data); | |
6582c164 | 2027 | return NULL; |
1da177e4 LT |
2028 | } |
2029 | ||
2030 | /*------------------------------------------------------------------*/ | |
2031 | /* | |
2032 | * Standard Wireless Handler : set Spy List | |
2033 | */ | |
2034 | int iw_handler_set_spy(struct net_device * dev, | |
2035 | struct iw_request_info * info, | |
2036 | union iwreq_data * wrqu, | |
2037 | char * extra) | |
2038 | { | |
2039 | struct iw_spy_data * spydata = get_spydata(dev); | |
2040 | struct sockaddr * address = (struct sockaddr *) extra; | |
2041 | ||
1da177e4 LT |
2042 | /* Make sure driver is not buggy or using the old API */ |
2043 | if(!spydata) | |
2044 | return -EOPNOTSUPP; | |
2045 | ||
2046 | /* Disable spy collection while we copy the addresses. | |
2047 | * While we copy addresses, any call to wireless_spy_update() | |
2048 | * will NOP. This is OK, as anyway the addresses are changing. */ | |
2049 | spydata->spy_number = 0; | |
2050 | ||
2051 | /* We want to operate without locking, because wireless_spy_update() | |
2052 | * most likely will happen in the interrupt handler, and therefore | |
2053 | * have its own locking constraints and needs performance. | |
2054 | * The rtnl_lock() make sure we don't race with the other iw_handlers. | |
2055 | * This make sure wireless_spy_update() "see" that the spy list | |
2056 | * is temporarily disabled. */ | |
2057 | wmb(); | |
2058 | ||
2059 | /* Are there are addresses to copy? */ | |
2060 | if(wrqu->data.length > 0) { | |
2061 | int i; | |
2062 | ||
2063 | /* Copy addresses */ | |
2064 | for(i = 0; i < wrqu->data.length; i++) | |
2065 | memcpy(spydata->spy_address[i], address[i].sa_data, | |
2066 | ETH_ALEN); | |
2067 | /* Reset stats */ | |
2068 | memset(spydata->spy_stat, 0, | |
2069 | sizeof(struct iw_quality) * IW_MAX_SPY); | |
2070 | ||
2071 | #ifdef WE_SPY_DEBUG | |
6582c164 | 2072 | printk(KERN_DEBUG "iw_handler_set_spy() : wireless_data %p, spydata %p, num %d\n", dev->wireless_data, spydata, wrqu->data.length); |
1da177e4 LT |
2073 | for (i = 0; i < wrqu->data.length; i++) |
2074 | printk(KERN_DEBUG | |
2075 | "%02X:%02X:%02X:%02X:%02X:%02X \n", | |
2076 | spydata->spy_address[i][0], | |
2077 | spydata->spy_address[i][1], | |
2078 | spydata->spy_address[i][2], | |
2079 | spydata->spy_address[i][3], | |
2080 | spydata->spy_address[i][4], | |
2081 | spydata->spy_address[i][5]); | |
2082 | #endif /* WE_SPY_DEBUG */ | |
2083 | } | |
2084 | ||
2085 | /* Make sure above is updated before re-enabling */ | |
2086 | wmb(); | |
2087 | ||
2088 | /* Enable addresses */ | |
2089 | spydata->spy_number = wrqu->data.length; | |
2090 | ||
2091 | return 0; | |
2092 | } | |
2093 | ||
2094 | /*------------------------------------------------------------------*/ | |
2095 | /* | |
2096 | * Standard Wireless Handler : get Spy List | |
2097 | */ | |
2098 | int iw_handler_get_spy(struct net_device * dev, | |
2099 | struct iw_request_info * info, | |
2100 | union iwreq_data * wrqu, | |
2101 | char * extra) | |
2102 | { | |
2103 | struct iw_spy_data * spydata = get_spydata(dev); | |
2104 | struct sockaddr * address = (struct sockaddr *) extra; | |
2105 | int i; | |
2106 | ||
2107 | /* Make sure driver is not buggy or using the old API */ | |
2108 | if(!spydata) | |
2109 | return -EOPNOTSUPP; | |
2110 | ||
2111 | wrqu->data.length = spydata->spy_number; | |
2112 | ||
2113 | /* Copy addresses. */ | |
2114 | for(i = 0; i < spydata->spy_number; i++) { | |
2115 | memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN); | |
2116 | address[i].sa_family = AF_UNIX; | |
2117 | } | |
2118 | /* Copy stats to the user buffer (just after). */ | |
2119 | if(spydata->spy_number > 0) | |
2120 | memcpy(extra + (sizeof(struct sockaddr) *spydata->spy_number), | |
2121 | spydata->spy_stat, | |
2122 | sizeof(struct iw_quality) * spydata->spy_number); | |
2123 | /* Reset updated flags. */ | |
2124 | for(i = 0; i < spydata->spy_number; i++) | |
6582c164 | 2125 | spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED; |
1da177e4 LT |
2126 | return 0; |
2127 | } | |
2128 | ||
2129 | /*------------------------------------------------------------------*/ | |
2130 | /* | |
2131 | * Standard Wireless Handler : set spy threshold | |
2132 | */ | |
2133 | int iw_handler_set_thrspy(struct net_device * dev, | |
2134 | struct iw_request_info *info, | |
2135 | union iwreq_data * wrqu, | |
2136 | char * extra) | |
2137 | { | |
2138 | struct iw_spy_data * spydata = get_spydata(dev); | |
2139 | struct iw_thrspy * threshold = (struct iw_thrspy *) extra; | |
2140 | ||
2141 | /* Make sure driver is not buggy or using the old API */ | |
2142 | if(!spydata) | |
2143 | return -EOPNOTSUPP; | |
2144 | ||
2145 | /* Just do it */ | |
2146 | memcpy(&(spydata->spy_thr_low), &(threshold->low), | |
2147 | 2 * sizeof(struct iw_quality)); | |
2148 | ||
2149 | /* Clear flag */ | |
2150 | memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under)); | |
2151 | ||
2152 | #ifdef WE_SPY_DEBUG | |
2153 | printk(KERN_DEBUG "iw_handler_set_thrspy() : low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level); | |
2154 | #endif /* WE_SPY_DEBUG */ | |
2155 | ||
2156 | return 0; | |
2157 | } | |
2158 | ||
2159 | /*------------------------------------------------------------------*/ | |
2160 | /* | |
2161 | * Standard Wireless Handler : get spy threshold | |
2162 | */ | |
2163 | int iw_handler_get_thrspy(struct net_device * dev, | |
2164 | struct iw_request_info *info, | |
2165 | union iwreq_data * wrqu, | |
2166 | char * extra) | |
2167 | { | |
2168 | struct iw_spy_data * spydata = get_spydata(dev); | |
2169 | struct iw_thrspy * threshold = (struct iw_thrspy *) extra; | |
2170 | ||
2171 | /* Make sure driver is not buggy or using the old API */ | |
2172 | if(!spydata) | |
2173 | return -EOPNOTSUPP; | |
2174 | ||
2175 | /* Just do it */ | |
2176 | memcpy(&(threshold->low), &(spydata->spy_thr_low), | |
2177 | 2 * sizeof(struct iw_quality)); | |
2178 | ||
2179 | return 0; | |
2180 | } | |
2181 | ||
2182 | /*------------------------------------------------------------------*/ | |
2183 | /* | |
2184 | * Prepare and send a Spy Threshold event | |
2185 | */ | |
2186 | static void iw_send_thrspy_event(struct net_device * dev, | |
2187 | struct iw_spy_data * spydata, | |
2188 | unsigned char * address, | |
2189 | struct iw_quality * wstats) | |
2190 | { | |
2191 | union iwreq_data wrqu; | |
2192 | struct iw_thrspy threshold; | |
2193 | ||
2194 | /* Init */ | |
2195 | wrqu.data.length = 1; | |
2196 | wrqu.data.flags = 0; | |
2197 | /* Copy address */ | |
2198 | memcpy(threshold.addr.sa_data, address, ETH_ALEN); | |
2199 | threshold.addr.sa_family = ARPHRD_ETHER; | |
2200 | /* Copy stats */ | |
2201 | memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality)); | |
2202 | /* Copy also thresholds */ | |
2203 | memcpy(&(threshold.low), &(spydata->spy_thr_low), | |
2204 | 2 * sizeof(struct iw_quality)); | |
2205 | ||
2206 | #ifdef WE_SPY_DEBUG | |
2207 | printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n", | |
2208 | threshold.addr.sa_data[0], | |
2209 | threshold.addr.sa_data[1], | |
2210 | threshold.addr.sa_data[2], | |
2211 | threshold.addr.sa_data[3], | |
2212 | threshold.addr.sa_data[4], | |
2213 | threshold.addr.sa_data[5], threshold.qual.level); | |
2214 | #endif /* WE_SPY_DEBUG */ | |
2215 | ||
2216 | /* Send event to user space */ | |
2217 | wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold); | |
2218 | } | |
2219 | ||
2220 | /* ---------------------------------------------------------------- */ | |
2221 | /* | |
2222 | * Call for the driver to update the spy data. | |
2223 | * For now, the spy data is a simple array. As the size of the array is | |
2224 | * small, this is good enough. If we wanted to support larger number of | |
2225 | * spy addresses, we should use something more efficient... | |
2226 | */ | |
2227 | void wireless_spy_update(struct net_device * dev, | |
2228 | unsigned char * address, | |
2229 | struct iw_quality * wstats) | |
2230 | { | |
2231 | struct iw_spy_data * spydata = get_spydata(dev); | |
2232 | int i; | |
2233 | int match = -1; | |
2234 | ||
2235 | /* Make sure driver is not buggy or using the old API */ | |
2236 | if(!spydata) | |
2237 | return; | |
2238 | ||
2239 | #ifdef WE_SPY_DEBUG | |
6582c164 | 2240 | 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]); |
1da177e4 LT |
2241 | #endif /* WE_SPY_DEBUG */ |
2242 | ||
2243 | /* Update all records that match */ | |
2244 | for(i = 0; i < spydata->spy_number; i++) | |
d3f4a687 | 2245 | if(!compare_ether_addr(address, spydata->spy_address[i])) { |
1da177e4 LT |
2246 | memcpy(&(spydata->spy_stat[i]), wstats, |
2247 | sizeof(struct iw_quality)); | |
2248 | match = i; | |
2249 | } | |
2250 | ||
2251 | /* Generate an event if we cross the spy threshold. | |
2252 | * To avoid event storms, we have a simple hysteresis : we generate | |
2253 | * event only when we go under the low threshold or above the | |
2254 | * high threshold. */ | |
2255 | if(match >= 0) { | |
2256 | if(spydata->spy_thr_under[match]) { | |
2257 | if(wstats->level > spydata->spy_thr_high.level) { | |
2258 | spydata->spy_thr_under[match] = 0; | |
2259 | iw_send_thrspy_event(dev, spydata, | |
2260 | address, wstats); | |
2261 | } | |
2262 | } else { | |
2263 | if(wstats->level < spydata->spy_thr_low.level) { | |
2264 | spydata->spy_thr_under[match] = 1; | |
2265 | iw_send_thrspy_event(dev, spydata, | |
2266 | address, wstats); | |
2267 | } | |
2268 | } | |
2269 | } | |
2270 | } | |
2271 | ||
2272 | EXPORT_SYMBOL(iw_handler_get_spy); | |
2273 | EXPORT_SYMBOL(iw_handler_get_thrspy); | |
2274 | EXPORT_SYMBOL(iw_handler_set_spy); | |
2275 | EXPORT_SYMBOL(iw_handler_set_thrspy); | |
2276 | EXPORT_SYMBOL(wireless_send_event); | |
2277 | EXPORT_SYMBOL(wireless_spy_update); |