1 /*
2  * This file implement the Wireless Extensions APIs.
3  *
4  * Authors :	Jean Tourrilhes - HPL - <jt@hpl.hp.com>
5  * Copyright (c) 1997-2003 Jean Tourrilhes, All Rights Reserved.
6  *
7  * (As all part of the Linux kernel, this file is GPL)
8  */
9 
10 /************************** DOCUMENTATION **************************/
11 /*
12  * API definition :
13  * --------------
14  * See <linux/wireless.h> for details of the APIs and the rest.
15  *
16  * History :
17  * -------
18  *
19  * v1 - 5.12.01 - Jean II
20  *	o Created this file.
21  *
22  * v2 - 13.12.01 - Jean II
23  *	o Move /proc/net/wireless stuff from net/core/dev.c to here
24  *	o Make Wireless Extension IOCTLs go through here
25  *	o Added iw_handler handling ;-)
26  *	o Added standard ioctl description
27  *	o Initial dumb commit strategy based on orinoco.c
28  *
29  * v3 - 19.12.01 - Jean II
30  *	o Make sure we don't go out of standard_ioctl[] in ioctl_standard_call
31  *	o Add event dispatcher function
32  *	o Add event description
33  *	o Propagate events as rtnetlink IFLA_WIRELESS option
34  *	o Generate event on selected SET requests
35  *
36  * v4 - 18.04.02 - Jean II
37  *	o Fix stupid off by one in iw_ioctl_description : IW_ESSID_MAX_SIZE + 1
38  *
39  * v5 - 21.06.02 - Jean II
40  *	o Add IW_PRIV_TYPE_ADDR in priv_type_size (+cleanup)
41  *	o Reshuffle IW_HEADER_TYPE_XXX to map IW_PRIV_TYPE_XXX changes
42  *	o Add IWEVCUSTOM for driver specific event/scanning token
43  *	o Turn on WE_STRICT_WRITE by default + kernel warning
44  *	o Fix WE_STRICT_WRITE in ioctl_export_private() (32 => iw_num)
45  *	o Fix off-by-one in test (extra_size <= IFNAMSIZ)
46  *
47  * v6 - 9.01.03 - Jean II
48  *	o Add common spy support : iw_handler_set_spy(), wireless_spy_update()
49  *	o Add enhanced spy support : iw_handler_set_thrspy() and event.
50  *	o Add WIRELESS_EXT version display in /proc/net/wireless
51  */
52 
53 /***************************** INCLUDES *****************************/
54 
55 #include <asm/uaccess.h>		/* copy_to_user() */
56 #include <linux/config.h>		/* Not needed ??? */
57 #include <linux/types.h>		/* off_t */
58 #include <linux/netdevice.h>		/* struct ifreq, dev_get_by_name() */
59 #include <linux/rtnetlink.h>		/* rtnetlink stuff */
60 #include <linux/if_arp.h>		/* ARPHRD_ETHER */
61 
62 #include <linux/wireless.h>		/* Pretty obvious */
63 #include <net/iw_handler.h>		/* New driver API */
64 
65 /**************************** CONSTANTS ****************************/
66 
67 /* Enough lenience, let's make sure things are proper... */
68 #define WE_STRICT_WRITE		/* Check write buffer size */
69 /* I'll probably drop both the define and kernel message in the next version */
70 
71 /* Debuging stuff */
72 #undef WE_IOCTL_DEBUG		/* Debug IOCTL API */
73 #undef WE_EVENT_DEBUG		/* Debug Event dispatcher */
74 #undef WE_SPY_DEBUG		/* Debug enhanced spy support */
75 
76 /* Options */
77 #define WE_EVENT_NETLINK	/* Propagate events using rtnetlink */
78 #define WE_SET_EVENT		/* Generate an event on some set commands */
79 
80 /************************* GLOBAL VARIABLES *************************/
81 /*
82  * You should not use global variables, because of re-entrancy.
83  * On our case, it's only const, so it's OK...
84  */
85 /*
86  * Meta-data about all the standard Wireless Extension request we
87  * know about.
88  */
89 static const struct iw_ioctl_description	standard_ioctl[] = {
90 	/* SIOCSIWCOMMIT */
91 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
92 	/* SIOCGIWNAME */
93 	{ IW_HEADER_TYPE_CHAR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
94 	/* SIOCSIWNWID */
95 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
96 	/* SIOCGIWNWID */
97 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
98 	/* SIOCSIWFREQ */
99 	{ IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
100 	/* SIOCGIWFREQ */
101 	{ IW_HEADER_TYPE_FREQ, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
102 	/* SIOCSIWMODE */
103 	{ IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_EVENT},
104 	/* SIOCGIWMODE */
105 	{ IW_HEADER_TYPE_UINT, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
106 	/* SIOCSIWSENS */
107 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
108 	/* SIOCGIWSENS */
109 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
110 	/* SIOCSIWRANGE */
111 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
112 	/* SIOCGIWRANGE */
113 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, sizeof(struct iw_range), IW_DESCR_FLAG_DUMP},
114 	/* SIOCSIWPRIV */
115 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
116 	/* SIOCGIWPRIV (handled directly by us) */
117 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
118 	/* SIOCSIWSTATS */
119 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
120 	/* SIOCGIWSTATS (handled directly by us) */
121 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
122 	/* SIOCSIWSPY */
123 	{ IW_HEADER_TYPE_POINT, 0, sizeof(struct sockaddr), 0, IW_MAX_SPY, 0},
124 	/* SIOCGIWSPY */
125 	{ IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_SPY, 0},
126 	/* SIOCSIWTHRSPY */
127 	{ IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
128 	/* SIOCGIWTHRSPY */
129 	{ IW_HEADER_TYPE_POINT, 0, sizeof(struct iw_thrspy), 1, 1, 0},
130 	/* SIOCSIWAP */
131 	{ IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
132 	/* SIOCGIWAP */
133 	{ IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, IW_DESCR_FLAG_DUMP},
134 	/* -- hole -- */
135 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
136 	/* SIOCGIWAPLIST */
137 	{ IW_HEADER_TYPE_POINT, 0, (sizeof(struct sockaddr) + sizeof(struct iw_quality)), 0, IW_MAX_AP, 0},
138 	/* SIOCSIWSCAN */
139 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
140 	/* SIOCGIWSCAN */
141 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_SCAN_MAX_DATA, 0},
142 	/* SIOCSIWESSID */
143 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_EVENT},
144 	/* SIOCGIWESSID */
145 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, IW_DESCR_FLAG_DUMP},
146 	/* SIOCSIWNICKN */
147 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
148 	/* SIOCGIWNICKN */
149 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ESSID_MAX_SIZE + 1, 0},
150 	/* -- hole -- */
151 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
152 	/* -- hole -- */
153 	{ IW_HEADER_TYPE_NULL, 0, 0, 0, 0, 0},
154 	/* SIOCSIWRATE */
155 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
156 	/* SIOCGIWRATE */
157 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
158 	/* SIOCSIWRTS */
159 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
160 	/* SIOCGIWRTS */
161 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
162 	/* SIOCSIWFRAG */
163 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
164 	/* SIOCGIWFRAG */
165 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
166 	/* SIOCSIWTXPOW */
167 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
168 	/* SIOCGIWTXPOW */
169 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
170 	/* SIOCSIWRETRY */
171 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
172 	/* SIOCGIWRETRY */
173 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
174 	/* SIOCSIWENCODE */
175 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT},
176 	/* SIOCGIWENCODE */
177 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_ENCODING_TOKEN_MAX, IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT},
178 	/* SIOCSIWPOWER */
179 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
180 	/* SIOCGIWPOWER */
181 	{ IW_HEADER_TYPE_PARAM, 0, 0, 0, 0, 0},
182 };
183 static const int standard_ioctl_num = (sizeof(standard_ioctl) /
184 				       sizeof(struct iw_ioctl_description));
185 
186 /*
187  * Meta-data about all the additional standard Wireless Extension events
188  * we know about.
189  */
190 static const struct iw_ioctl_description	standard_event[] = {
191 	/* IWEVTXDROP */
192 	{ IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
193 	/* IWEVQUAL */
194 	{ IW_HEADER_TYPE_QUAL, 0, 0, 0, 0, 0},
195 	/* IWEVCUSTOM */
196 	{ IW_HEADER_TYPE_POINT, 0, 1, 0, IW_CUSTOM_MAX, 0},
197 	/* IWEVREGISTERED */
198 	{ IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
199 	/* IWEVEXPIRED */
200 	{ IW_HEADER_TYPE_ADDR, 0, 0, 0, 0, 0},
201 };
202 static const int standard_event_num = (sizeof(standard_event) /
203 				       sizeof(struct iw_ioctl_description));
204 
205 /* Size (in bytes) of the various private data types */
206 static const char priv_type_size[] = {
207 	0,				/* IW_PRIV_TYPE_NONE */
208 	1,				/* IW_PRIV_TYPE_BYTE */
209 	1,				/* IW_PRIV_TYPE_CHAR */
210 	0,				/* Not defined */
211 	sizeof(__u32),			/* IW_PRIV_TYPE_INT */
212 	sizeof(struct iw_freq),		/* IW_PRIV_TYPE_FLOAT */
213 	sizeof(struct sockaddr),	/* IW_PRIV_TYPE_ADDR */
214 	0,				/* Not defined */
215 };
216 
217 /* Size (in bytes) of various events */
218 static const int event_type_size[] = {
219 	IW_EV_LCP_LEN,			/* IW_HEADER_TYPE_NULL */
220 	0,
221 	IW_EV_CHAR_LEN,			/* IW_HEADER_TYPE_CHAR */
222 	0,
223 	IW_EV_UINT_LEN,			/* IW_HEADER_TYPE_UINT */
224 	IW_EV_FREQ_LEN,			/* IW_HEADER_TYPE_FREQ */
225 	IW_EV_ADDR_LEN,			/* IW_HEADER_TYPE_ADDR */
226 	0,
227 	IW_EV_POINT_LEN,		/* Without variable payload */
228 	IW_EV_PARAM_LEN,		/* IW_HEADER_TYPE_PARAM */
229 	IW_EV_QUAL_LEN,			/* IW_HEADER_TYPE_QUAL */
230 };
231 
232 /************************ COMMON SUBROUTINES ************************/
233 /*
234  * Stuff that may be used in various place or doesn't fit in one
235  * of the section below.
236  */
237 
238 /* ---------------------------------------------------------------- */
239 /*
240  * Return the driver handler associated with a specific Wireless Extension.
241  * Called from various place, so make sure it remains efficient.
242  */
get_handler(struct net_device * dev,unsigned int cmd)243 static inline iw_handler get_handler(struct net_device *dev,
244 				     unsigned int cmd)
245 {
246 	/* Don't "optimise" the following variable, it will crash */
247 	unsigned int	index;		/* *MUST* be unsigned */
248 
249 	/* Check if we have some wireless handlers defined */
250 	if(dev->wireless_handlers == NULL)
251 		return NULL;
252 
253 	/* Try as a standard command */
254 	index = cmd - SIOCIWFIRST;
255 	if(index < dev->wireless_handlers->num_standard)
256 		return dev->wireless_handlers->standard[index];
257 
258 	/* Try as a private command */
259 	index = cmd - SIOCIWFIRSTPRIV;
260 	if(index < dev->wireless_handlers->num_private)
261 		return dev->wireless_handlers->private[index];
262 
263 	/* Not found */
264 	return NULL;
265 }
266 
267 /* ---------------------------------------------------------------- */
268 /*
269  * Get statistics out of the driver
270  */
get_wireless_stats(struct net_device * dev)271 static inline struct iw_statistics *get_wireless_stats(struct net_device *dev)
272 {
273 	return (dev->get_wireless_stats ?
274 		dev->get_wireless_stats(dev) :
275 		(struct iw_statistics *) NULL);
276 	/* In the future, get_wireless_stats may move from 'struct net_device'
277 	 * to 'struct iw_handler_def', to de-bloat struct net_device.
278 	 * Definitely worse a thought... */
279 }
280 
281 /* ---------------------------------------------------------------- */
282 /*
283  * Call the commit handler in the driver
284  * (if exist and if conditions are right)
285  *
286  * Note : our current commit strategy is currently pretty dumb,
287  * but we will be able to improve on that...
288  * The goal is to try to agreagate as many changes as possible
289  * before doing the commit. Drivers that will define a commit handler
290  * are usually those that need a reset after changing parameters, so
291  * we want to minimise the number of reset.
292  * A cool idea is to use a timer : at each "set" command, we re-set the
293  * timer, when the timer eventually fires, we call the driver.
294  * Hopefully, more on that later.
295  *
296  * Also, I'm waiting to see how many people will complain about the
297  * netif_running(dev) test. I'm open on that one...
298  * Hopefully, the driver will remember to do a commit in "open()" ;-)
299  */
call_commit_handler(struct net_device * dev)300 static inline int call_commit_handler(struct net_device *	dev)
301 {
302 	if((netif_running(dev)) &&
303 	   (dev->wireless_handlers->standard[0] != NULL)) {
304 		/* Call the commit handler on the driver */
305 		return dev->wireless_handlers->standard[0](dev, NULL,
306 							   NULL, NULL);
307 	} else
308 		return 0;		/* Command completed successfully */
309 }
310 
311 /* ---------------------------------------------------------------- */
312 /*
313  * Number of private arguments
314  */
get_priv_size(__u16 args)315 static inline int get_priv_size(__u16	args)
316 {
317 	int	num = args & IW_PRIV_SIZE_MASK;
318 	int	type = (args & IW_PRIV_TYPE_MASK) >> 12;
319 
320 	return num * priv_type_size[type];
321 }
322 
323 
324 /******************** /proc/net/wireless SUPPORT ********************/
325 /*
326  * The /proc/net/wireless file is a human readable user-space interface
327  * exporting various wireless specific statistics from the wireless devices.
328  * This is the most popular part of the Wireless Extensions ;-)
329  *
330  * This interface is a pure clone of /proc/net/dev (in net/core/dev.c).
331  * The content of the file is basically the content of "struct iw_statistics".
332  */
333 
334 #ifdef CONFIG_PROC_FS
335 
336 /* ---------------------------------------------------------------- */
337 /*
338  * Print one entry (line) of /proc/net/wireless
339  */
sprintf_wireless_stats(char * buffer,struct net_device * dev)340 static inline int sprintf_wireless_stats(char *buffer, struct net_device *dev)
341 {
342 	/* Get stats from the driver */
343 	struct iw_statistics *stats;
344 	int size;
345 
346 	stats = get_wireless_stats(dev);
347 	if (stats != (struct iw_statistics *) NULL) {
348 		size = sprintf(buffer,
349 			       "%6s: %04x  %3d%c  %3d%c  %3d%c  %6d %6d %6d %6d %6d   %6d\n",
350 			       dev->name,
351 			       stats->status,
352 			       stats->qual.qual,
353 			       stats->qual.updated & 1 ? '.' : ' ',
354 			       ((__u8) stats->qual.level),
355 			       stats->qual.updated & 2 ? '.' : ' ',
356 			       ((__u8) stats->qual.noise),
357 			       stats->qual.updated & 4 ? '.' : ' ',
358 			       stats->discard.nwid,
359 			       stats->discard.code,
360 			       stats->discard.fragment,
361 			       stats->discard.retries,
362 			       stats->discard.misc,
363 			       stats->miss.beacon);
364 		stats->qual.updated = 0;
365 	}
366 	else
367 		size = 0;
368 
369 	return size;
370 }
371 
372 /* ---------------------------------------------------------------- */
373 /*
374  * Print info for /proc/net/wireless (print all entries)
375  */
dev_get_wireless_info(char * buffer,char ** start,off_t offset,int length)376 int dev_get_wireless_info(char * buffer, char **start, off_t offset,
377 			  int length)
378 {
379 	int		len = 0;
380 	off_t		begin = 0;
381 	off_t		pos = 0;
382 	int		size;
383 
384 	struct net_device *	dev;
385 
386 	size = sprintf(buffer,
387 		       "Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE\n"
388 		       " face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | %d\n",
389 		       WIRELESS_EXT);
390 
391 	pos += size;
392 	len += size;
393 
394 	read_lock(&dev_base_lock);
395 	for (dev = dev_base; dev != NULL; dev = dev->next) {
396 		size = sprintf_wireless_stats(buffer + len, dev);
397 		len += size;
398 		pos = begin + len;
399 
400 		if (pos < offset) {
401 			len = 0;
402 			begin = pos;
403 		}
404 		if (pos > offset + length)
405 			break;
406 	}
407 	read_unlock(&dev_base_lock);
408 
409 	*start = buffer + (offset - begin);	/* Start of wanted data */
410 	len -= (offset - begin);		/* Start slop */
411 	if (len > length)
412 		len = length;			/* Ending slop */
413 	if (len < 0)
414 		len = 0;
415 
416 	return len;
417 }
418 #endif	/* CONFIG_PROC_FS */
419 
420 /************************** IOCTL SUPPORT **************************/
421 /*
422  * The original user space API to configure all those Wireless Extensions
423  * is through IOCTLs.
424  * In there, we check if we need to call the new driver API (iw_handler)
425  * or just call the driver ioctl handler.
426  */
427 
428 /* ---------------------------------------------------------------- */
429 /*
430  *	Allow programatic access to /proc/net/wireless even if /proc
431  *	doesn't exist... Also more efficient...
432  */
dev_iwstats(struct net_device * dev,struct ifreq * ifr)433 static inline int dev_iwstats(struct net_device *dev, struct ifreq *ifr)
434 {
435 	/* Get stats from the driver */
436 	struct iw_statistics *stats;
437 
438 	stats = get_wireless_stats(dev);
439 	if (stats != (struct iw_statistics *) NULL) {
440 		struct iwreq *	wrq = (struct iwreq *)ifr;
441 
442 		/* Copy statistics to the user buffer */
443 		if(copy_to_user(wrq->u.data.pointer, stats,
444 				sizeof(struct iw_statistics)))
445 			return -EFAULT;
446 
447 		/* Check if we need to clear the update flag */
448 		if(wrq->u.data.flags != 0)
449 			stats->qual.updated = 0;
450 		return 0;
451 	} else
452 		return -EOPNOTSUPP;
453 }
454 
455 /* ---------------------------------------------------------------- */
456 /*
457  * Export the driver private handler definition
458  * They will be picked up by tools like iwpriv...
459  */
ioctl_export_private(struct net_device * dev,struct ifreq * ifr)460 static inline int ioctl_export_private(struct net_device *	dev,
461 				       struct ifreq *		ifr)
462 {
463 	struct iwreq *				iwr = (struct iwreq *) ifr;
464 
465 	/* Check if the driver has something to export */
466 	if((dev->wireless_handlers->num_private_args == 0) ||
467 	   (dev->wireless_handlers->private_args == NULL))
468 		return -EOPNOTSUPP;
469 
470 	/* Check NULL pointer */
471 	if(iwr->u.data.pointer == NULL)
472 		return -EFAULT;
473 #ifdef WE_STRICT_WRITE
474 	/* Check if there is enough buffer up there */
475 	if(iwr->u.data.length < dev->wireless_handlers->num_private_args) {
476 		printk(KERN_ERR "%s (WE) : Buffer for request SIOCGIWPRIV too small (%d<%d)\n", dev->name, iwr->u.data.length, dev->wireless_handlers->num_private_args);
477 		return -E2BIG;
478 	}
479 #endif	/* WE_STRICT_WRITE */
480 
481 	/* Set the number of available ioctls. */
482 	iwr->u.data.length = dev->wireless_handlers->num_private_args;
483 
484 	/* Copy structure to the user buffer. */
485 	if (copy_to_user(iwr->u.data.pointer,
486 			 dev->wireless_handlers->private_args,
487 			 sizeof(struct iw_priv_args) * iwr->u.data.length))
488 		return -EFAULT;
489 
490 	return 0;
491 }
492 
493 /* ---------------------------------------------------------------- */
494 /*
495  * Wrapper to call a standard Wireless Extension handler.
496  * We do various checks and also take care of moving data between
497  * user space and kernel space.
498  */
ioctl_standard_call(struct net_device * dev,struct ifreq * ifr,unsigned int cmd,iw_handler handler)499 static inline int ioctl_standard_call(struct net_device *	dev,
500 				      struct ifreq *		ifr,
501 				      unsigned int		cmd,
502 				      iw_handler		handler)
503 {
504 	struct iwreq *				iwr = (struct iwreq *) ifr;
505 	const struct iw_ioctl_description *	descr;
506 	struct iw_request_info			info;
507 	int					ret = -EINVAL;
508 	int					user_size = 0;
509 
510 	/* Get the description of the IOCTL */
511 	if((cmd - SIOCIWFIRST) >= standard_ioctl_num)
512 		return -EOPNOTSUPP;
513 	descr = &(standard_ioctl[cmd - SIOCIWFIRST]);
514 
515 #ifdef WE_IOCTL_DEBUG
516 	printk(KERN_DEBUG "%s (WE) : Found standard handler for 0x%04X\n",
517 	       ifr->ifr_name, cmd);
518 	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);
519 #endif	/* WE_IOCTL_DEBUG */
520 
521 	/* Prepare the call */
522 	info.cmd = cmd;
523 	info.flags = 0;
524 
525 	/* Check if we have a pointer to user space data or not */
526 	if(descr->header_type != IW_HEADER_TYPE_POINT) {
527 
528 		/* No extra arguments. Trivial to handle */
529 		ret = handler(dev, &info, &(iwr->u), NULL);
530 
531 #ifdef WE_SET_EVENT
532 		/* Generate an event to notify listeners of the change */
533 		if((descr->flags & IW_DESCR_FLAG_EVENT) &&
534 		   ((ret == 0) || (ret == -EIWCOMMIT)))
535 			wireless_send_event(dev, cmd, &(iwr->u), NULL);
536 #endif	/* WE_SET_EVENT */
537 	} else {
538 		char *	extra;
539 		int	err;
540 
541 		/* Check what user space is giving us */
542 		if(IW_IS_SET(cmd)) {
543 			/* Check NULL pointer */
544 			if((iwr->u.data.pointer == NULL) &&
545 			   (iwr->u.data.length != 0))
546 				return -EFAULT;
547 			/* Check if number of token fits within bounds */
548 			if(iwr->u.data.length > descr->max_tokens)
549 				return -E2BIG;
550 			if(iwr->u.data.length < descr->min_tokens)
551 				return -EINVAL;
552 		} else {
553 			/* Check NULL pointer */
554 			if(iwr->u.data.pointer == NULL)
555 				return -EFAULT;
556 			/* Save user space buffer size for checking */
557 			user_size = iwr->u.data.length;
558 		}
559 
560 #ifdef WE_IOCTL_DEBUG
561 		printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
562 		       dev->name, descr->max_tokens * descr->token_size);
563 #endif	/* WE_IOCTL_DEBUG */
564 
565 		/* Always allocate for max space. Easier, and won't last
566 		 * long... */
567 		extra = kmalloc(descr->max_tokens * descr->token_size,
568 				GFP_KERNEL);
569 		if (extra == NULL) {
570 			return -ENOMEM;
571 		}
572 
573 		/* If it is a SET, get all the extra data in here */
574 		if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
575 			err = copy_from_user(extra, iwr->u.data.pointer,
576 					     iwr->u.data.length *
577 					     descr->token_size);
578 			if (err) {
579 				kfree(extra);
580 				return -EFAULT;
581 			}
582 #ifdef WE_IOCTL_DEBUG
583 			printk(KERN_DEBUG "%s (WE) : Got %d bytes\n",
584 			       dev->name,
585 			       iwr->u.data.length * descr->token_size);
586 #endif	/* WE_IOCTL_DEBUG */
587 		}
588 
589 		/* Call the handler */
590 		ret = handler(dev, &info, &(iwr->u), extra);
591 
592 		/* If we have something to return to the user */
593 		if (!ret && IW_IS_GET(cmd)) {
594 #ifdef WE_STRICT_WRITE
595 			/* Check if there is enough buffer up there */
596 			if(user_size < iwr->u.data.length) {
597 				printk(KERN_ERR "%s (WE) : Buffer for request %04X too small (%d<%d)\n", dev->name, cmd, user_size, iwr->u.data.length);
598 				kfree(extra);
599 				return -E2BIG;
600 			}
601 #endif	/* WE_STRICT_WRITE */
602 
603 			err = copy_to_user(iwr->u.data.pointer, extra,
604 					   iwr->u.data.length *
605 					   descr->token_size);
606 			if (err)
607 				ret =  -EFAULT;
608 #ifdef WE_IOCTL_DEBUG
609 			printk(KERN_DEBUG "%s (WE) : Wrote %d bytes\n",
610 			       dev->name,
611 			       iwr->u.data.length * descr->token_size);
612 #endif	/* WE_IOCTL_DEBUG */
613 		}
614 
615 #ifdef WE_SET_EVENT
616 		/* Generate an event to notify listeners of the change */
617 		if((descr->flags & IW_DESCR_FLAG_EVENT) &&
618 		   ((ret == 0) || (ret == -EIWCOMMIT))) {
619 			if(descr->flags & IW_DESCR_FLAG_RESTRICT)
620 				/* If the event is restricted, don't
621 				 * export the payload */
622 				wireless_send_event(dev, cmd, &(iwr->u), NULL);
623 			else
624 				wireless_send_event(dev, cmd, &(iwr->u),
625 						    extra);
626 		}
627 #endif	/* WE_SET_EVENT */
628 
629 		/* Cleanup - I told you it wasn't that long ;-) */
630 		kfree(extra);
631 	}
632 
633 	/* Call commit handler if needed and defined */
634 	if(ret == -EIWCOMMIT)
635 		ret = call_commit_handler(dev);
636 
637 	/* Here, we will generate the appropriate event if needed */
638 
639 	return ret;
640 }
641 
642 /* ---------------------------------------------------------------- */
643 /*
644  * Wrapper to call a private Wireless Extension handler.
645  * We do various checks and also take care of moving data between
646  * user space and kernel space.
647  * It's not as nice and slimline as the standard wrapper. The cause
648  * is struct iw_priv_args, which was not really designed for the
649  * job we are going here.
650  *
651  * IMPORTANT : This function prevent to set and get data on the same
652  * IOCTL and enforce the SET/GET convention. Not doing it would be
653  * far too hairy...
654  * If you need to set and get data at the same time, please don't use
655  * a iw_handler but process it in your ioctl handler (i.e. use the
656  * old driver API).
657  */
ioctl_private_call(struct net_device * dev,struct ifreq * ifr,unsigned int cmd,iw_handler handler)658 static inline int ioctl_private_call(struct net_device *	dev,
659 				     struct ifreq *		ifr,
660 				     unsigned int		cmd,
661 				     iw_handler		handler)
662 {
663 	struct iwreq *			iwr = (struct iwreq *) ifr;
664 	struct iw_priv_args *		descr = NULL;
665 	struct iw_request_info		info;
666 	int				extra_size = 0;
667 	int				i;
668 	int				ret = -EINVAL;
669 
670 	/* Get the description of the IOCTL */
671 	for(i = 0; i < dev->wireless_handlers->num_private_args; i++)
672 		if(cmd == dev->wireless_handlers->private_args[i].cmd) {
673 			descr = &(dev->wireless_handlers->private_args[i]);
674 			break;
675 		}
676 
677 #ifdef WE_IOCTL_DEBUG
678 	printk(KERN_DEBUG "%s (WE) : Found private handler for 0x%04X\n",
679 	       ifr->ifr_name, cmd);
680 	if(descr) {
681 		printk(KERN_DEBUG "%s (WE) : Name %s, set %X, get %X\n",
682 		       dev->name, descr->name,
683 		       descr->set_args, descr->get_args);
684 	}
685 #endif	/* WE_IOCTL_DEBUG */
686 
687 	/* Compute the size of the set/get arguments */
688 	if(descr != NULL) {
689 		if(IW_IS_SET(cmd)) {
690 			int	offset = 0;	/* For sub-ioctls */
691 			/* Check for sub-ioctl handler */
692 			if(descr->name[0] == '\0')
693 				/* Reserve one int for sub-ioctl index */
694 				offset = sizeof(__u32);
695 
696 			/* Size of set arguments */
697 			extra_size = get_priv_size(descr->set_args);
698 
699 			/* Does it fits in iwr ? */
700 			if((descr->set_args & IW_PRIV_SIZE_FIXED) &&
701 			   ((extra_size + offset) <= IFNAMSIZ))
702 				extra_size = 0;
703 		} else {
704 			/* Size of set arguments */
705 			extra_size = get_priv_size(descr->get_args);
706 
707 			/* Does it fits in iwr ? */
708 			if((descr->get_args & IW_PRIV_SIZE_FIXED) &&
709 			   (extra_size <= IFNAMSIZ))
710 				extra_size = 0;
711 		}
712 	}
713 
714 	/* Prepare the call */
715 	info.cmd = cmd;
716 	info.flags = 0;
717 
718 	/* Check if we have a pointer to user space data or not. */
719 	if(extra_size == 0) {
720 		/* No extra arguments. Trivial to handle */
721 		ret = handler(dev, &info, &(iwr->u), (char *) &(iwr->u));
722 	} else {
723 		char *	extra;
724 		int	err;
725 
726 		/* Check what user space is giving us */
727 		if(IW_IS_SET(cmd)) {
728 			/* Check NULL pointer */
729 			if((iwr->u.data.pointer == NULL) &&
730 			   (iwr->u.data.length != 0))
731 				return -EFAULT;
732 
733 			/* Does it fits within bounds ? */
734 			if(iwr->u.data.length > (descr->set_args &
735 						 IW_PRIV_SIZE_MASK))
736 				return -E2BIG;
737 		} else {
738 			/* Check NULL pointer */
739 			if(iwr->u.data.pointer == NULL)
740 				return -EFAULT;
741 		}
742 
743 #ifdef WE_IOCTL_DEBUG
744 		printk(KERN_DEBUG "%s (WE) : Malloc %d bytes\n",
745 		       dev->name, extra_size);
746 #endif	/* WE_IOCTL_DEBUG */
747 
748 		/* Always allocate for max space. Easier, and won't last
749 		 * long... */
750 		extra = kmalloc(extra_size, GFP_KERNEL);
751 		if (extra == NULL) {
752 			return -ENOMEM;
753 		}
754 
755 		/* If it is a SET, get all the extra data in here */
756 		if(IW_IS_SET(cmd) && (iwr->u.data.length != 0)) {
757 			err = copy_from_user(extra, iwr->u.data.pointer,
758 					     extra_size);
759 			if (err) {
760 				kfree(extra);
761 				return -EFAULT;
762 			}
763 #ifdef WE_IOCTL_DEBUG
764 			printk(KERN_DEBUG "%s (WE) : Got %d elem\n",
765 			       dev->name, iwr->u.data.length);
766 #endif	/* WE_IOCTL_DEBUG */
767 		}
768 
769 		/* Call the handler */
770 		ret = handler(dev, &info, &(iwr->u), extra);
771 
772 		/* If we have something to return to the user */
773 		if (!ret && IW_IS_GET(cmd)) {
774 			err = copy_to_user(iwr->u.data.pointer, extra,
775 					   extra_size);
776 			if (err)
777 				ret =  -EFAULT;
778 #ifdef WE_IOCTL_DEBUG
779 			printk(KERN_DEBUG "%s (WE) : Wrote %d elem\n",
780 			       dev->name, iwr->u.data.length);
781 #endif	/* WE_IOCTL_DEBUG */
782 		}
783 
784 		/* Cleanup - I told you it wasn't that long ;-) */
785 		kfree(extra);
786 	}
787 
788 
789 	/* Call commit handler if needed and defined */
790 	if(ret == -EIWCOMMIT)
791 		ret = call_commit_handler(dev);
792 
793 	return ret;
794 }
795 
796 /* ---------------------------------------------------------------- */
797 /*
798  * Main IOCTl dispatcher. Called from the main networking code
799  * (dev_ioctl() in net/core/dev.c).
800  * Check the type of IOCTL and call the appropriate wrapper...
801  */
wireless_process_ioctl(struct ifreq * ifr,unsigned int cmd)802 int wireless_process_ioctl(struct ifreq *ifr, unsigned int cmd)
803 {
804 	struct net_device *dev;
805 	iw_handler	handler;
806 
807 	/* Permissions are already checked in dev_ioctl() before calling us.
808 	 * The copy_to/from_user() of ifr is also dealt with in there */
809 
810 	/* Make sure the device exist */
811 	if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
812 		return -ENODEV;
813 
814 	/* A bunch of special cases, then the generic case...
815 	 * Note that 'cmd' is already filtered in dev_ioctl() with
816 	 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
817 	switch(cmd)
818 	{
819 		case SIOCGIWSTATS:
820 			/* Get Wireless Stats */
821 			return dev_iwstats(dev, ifr);
822 
823 		case SIOCGIWPRIV:
824 			/* Check if we have some wireless handlers defined */
825 			if(dev->wireless_handlers != NULL) {
826 				/* We export to user space the definition of
827 				 * the private handler ourselves */
828 				return ioctl_export_private(dev, ifr);
829 			}
830 			// ## Fall-through for old API ##
831 		default:
832 			/* Generic IOCTL */
833 			/* Basic check */
834 			if (!netif_device_present(dev))
835 				return -ENODEV;
836 			/* New driver API : try to find the handler */
837 			handler = get_handler(dev, cmd);
838 			if(handler != NULL) {
839 				/* Standard and private are not the same */
840 				if(cmd < SIOCIWFIRSTPRIV)
841 					return ioctl_standard_call(dev,
842 								   ifr,
843 								   cmd,
844 								   handler);
845 				else
846 					return ioctl_private_call(dev,
847 								  ifr,
848 								  cmd,
849 								  handler);
850 			}
851 			/* Old driver API : call driver ioctl handler */
852 			if (dev->do_ioctl) {
853 				return dev->do_ioctl(dev, ifr, cmd);
854 			}
855 			return -EOPNOTSUPP;
856 	}
857 	/* Not reached */
858 	return -EINVAL;
859 }
860 
861 /************************* EVENT PROCESSING *************************/
862 /*
863  * Process events generated by the wireless layer or the driver.
864  * Most often, the event will be propagated through rtnetlink
865  */
866 
867 #ifdef WE_EVENT_NETLINK
868 /* "rtnl" is defined in net/core/rtnetlink.c, but we need it here.
869  * It is declared in <linux/rtnetlink.h> */
870 
871 /* ---------------------------------------------------------------- */
872 /*
873  * Fill a rtnetlink message with our event data.
874  * Note that we propage only the specified event and don't dump the
875  * current wireless config. Dumping the wireless config is far too
876  * expensive (for each parameter, the driver need to query the hardware).
877  */
rtnetlink_fill_iwinfo(struct sk_buff * skb,struct net_device * dev,int type,char * event,int event_len)878 static inline int rtnetlink_fill_iwinfo(struct sk_buff *	skb,
879 					struct net_device *	dev,
880 					int			type,
881 					char *			event,
882 					int			event_len)
883 {
884 	struct ifinfomsg *r;
885 	struct nlmsghdr  *nlh;
886 	unsigned char	 *b = skb->tail;
887 
888 	nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(*r));
889 	r = NLMSG_DATA(nlh);
890 	r->ifi_family = AF_UNSPEC;
891 	r->__ifi_pad = 0;
892 	r->ifi_type = dev->type;
893 	r->ifi_index = dev->ifindex;
894 	r->ifi_flags = dev->flags;
895 	r->ifi_change = 0;	/* Wireless changes don't affect those flags */
896 
897 	/* Add the wireless events in the netlink packet */
898 	RTA_PUT(skb, IFLA_WIRELESS,
899 		event_len, event);
900 
901 	nlh->nlmsg_len = skb->tail - b;
902 	return skb->len;
903 
904 nlmsg_failure:
905 rtattr_failure:
906 	skb_trim(skb, b - skb->data);
907 	return -1;
908 }
909 
910 /* ---------------------------------------------------------------- */
911 /*
912  * Create and broadcast and send it on the standard rtnetlink socket
913  * This is a pure clone rtmsg_ifinfo() in net/core/rtnetlink.c
914  * Andrzej Krzysztofowicz mandated that I used a IFLA_XXX field
915  * within a RTM_NEWLINK event.
916  */
rtmsg_iwinfo(struct net_device * dev,char * event,int event_len)917 static inline void rtmsg_iwinfo(struct net_device *	dev,
918 				char *			event,
919 				int			event_len)
920 {
921 	struct sk_buff *skb;
922 	int size = NLMSG_GOODSIZE;
923 
924 	skb = alloc_skb(size, GFP_ATOMIC);
925 	if (!skb)
926 		return;
927 
928 	if (rtnetlink_fill_iwinfo(skb, dev, RTM_NEWLINK,
929 				  event, event_len) < 0) {
930 		kfree_skb(skb);
931 		return;
932 	}
933 	NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
934 	netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_ATOMIC);
935 }
936 #endif	/* WE_EVENT_NETLINK */
937 
938 /* ---------------------------------------------------------------- */
939 /*
940  * Main event dispatcher. Called from other parts and drivers.
941  * Send the event on the apropriate channels.
942  * May be called from interrupt context.
943  */
wireless_send_event(struct net_device * dev,unsigned int cmd,union iwreq_data * wrqu,char * extra)944 void wireless_send_event(struct net_device *	dev,
945 			 unsigned int		cmd,
946 			 union iwreq_data *	wrqu,
947 			 char *			extra)
948 {
949 	const struct iw_ioctl_description *	descr = NULL;
950 	int extra_len = 0;
951 	struct iw_event  *event;		/* Mallocated whole event */
952 	int event_len;				/* Its size */
953 	int hdr_len;				/* Size of the event header */
954 	/* Don't "optimise" the following variable, it will crash */
955 	unsigned	cmd_index;		/* *MUST* be unsigned */
956 
957 	/* Get the description of the IOCTL */
958 	if(cmd <= SIOCIWLAST) {
959 		cmd_index = cmd - SIOCIWFIRST;
960 		if(cmd_index < standard_ioctl_num)
961 			descr = &(standard_ioctl[cmd_index]);
962 	} else {
963 		cmd_index = cmd - IWEVFIRST;
964 		if(cmd_index < standard_event_num)
965 			descr = &(standard_event[cmd_index]);
966 	}
967 	/* Don't accept unknown events */
968 	if(descr == NULL) {
969 		/* Note : we don't return an error to the driver, because
970 		 * the driver would not know what to do about it. It can't
971 		 * return an error to the user, because the event is not
972 		 * initiated by a user request.
973 		 * The best the driver could do is to log an error message.
974 		 * We will do it ourselves instead...
975 		 */
976 	  	printk(KERN_ERR "%s (WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
977 		       dev->name, cmd);
978 		return;
979 	}
980 #ifdef WE_EVENT_DEBUG
981 	printk(KERN_DEBUG "%s (WE) : Got event 0x%04X\n",
982 	       dev->name, cmd);
983 	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);
984 #endif	/* WE_EVENT_DEBUG */
985 
986 	/* Check extra parameters and set extra_len */
987 	if(descr->header_type == IW_HEADER_TYPE_POINT) {
988 		/* Check if number of token fits within bounds */
989 		if(wrqu->data.length > descr->max_tokens) {
990 		  	printk(KERN_ERR "%s (WE) : Wireless Event too big (%d)\n", dev->name, wrqu->data.length);
991 			return;
992 		}
993 		if(wrqu->data.length < descr->min_tokens) {
994 		  	printk(KERN_ERR "%s (WE) : Wireless Event too small (%d)\n", dev->name, wrqu->data.length);
995 			return;
996 		}
997 		/* Calculate extra_len - extra is NULL for restricted events */
998 		if(extra != NULL)
999 			extra_len = wrqu->data.length * descr->token_size;
1000 #ifdef WE_EVENT_DEBUG
1001 		printk(KERN_DEBUG "%s (WE) : Event 0x%04X, tokens %d, extra_len %d\n", dev->name, cmd, wrqu->data.length, extra_len);
1002 #endif	/* WE_EVENT_DEBUG */
1003 	}
1004 
1005 	/* Total length of the event */
1006 	hdr_len = event_type_size[descr->header_type];
1007 	event_len = hdr_len + extra_len;
1008 
1009 #ifdef WE_EVENT_DEBUG
1010 	printk(KERN_DEBUG "%s (WE) : Event 0x%04X, hdr_len %d, event_len %d\n", dev->name, cmd, hdr_len, event_len);
1011 #endif	/* WE_EVENT_DEBUG */
1012 
1013 	/* Create temporary buffer to hold the event */
1014 	event = kmalloc(event_len, GFP_ATOMIC);
1015 	if(event == NULL)
1016 		return;
1017 
1018 	/* Fill event */
1019 	event->len = event_len;
1020 	event->cmd = cmd;
1021 	memcpy(&event->u, wrqu, hdr_len - IW_EV_LCP_LEN);
1022 	if(extra != NULL)
1023 		memcpy(((char *) event) + hdr_len, extra, extra_len);
1024 
1025 #ifdef WE_EVENT_NETLINK
1026 	/* rtnetlink event channel */
1027 	rtmsg_iwinfo(dev, (char *) event, event_len);
1028 #endif	/* WE_EVENT_NETLINK */
1029 
1030 	/* Cleanup */
1031 	kfree(event);
1032 
1033 	return;		/* Always success, I guess ;-) */
1034 }
1035 
1036 /********************** ENHANCED IWSPY SUPPORT **********************/
1037 /*
1038  * In the old days, the driver was handling spy support all by itself.
1039  * Now, the driver can delegate this task to Wireless Extensions.
1040  * It needs to use those standard spy iw_handler in struct iw_handler_def,
1041  * push data to us via XXX and include struct iw_spy_data in its
1042  * private part.
1043  * One of the main advantage of centralising spy support here is that
1044  * it becomes much easier to improve and extend it without having to touch
1045  * the drivers. One example is the addition of the Spy-Threshold events.
1046  * Note : IW_WIRELESS_SPY is defined in iw_handler.h
1047  */
1048 
1049 /*------------------------------------------------------------------*/
1050 /*
1051  * Standard Wireless Handler : set Spy List
1052  */
iw_handler_set_spy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1053 int iw_handler_set_spy(struct net_device *	dev,
1054 		       struct iw_request_info *	info,
1055 		       union iwreq_data *	wrqu,
1056 		       char *			extra)
1057 {
1058 #ifdef IW_WIRELESS_SPY
1059 	struct iw_spy_data *	spydata = (dev->priv +
1060 					   dev->wireless_handlers->spy_offset);
1061 	struct sockaddr *	address = (struct sockaddr *) extra;
1062 
1063 	/* Disable spy collection while we copy the addresses.
1064 	 * As we don't disable interrupts, we need to do this to avoid races.
1065 	 * As we are the only writer, this is good enough. */
1066 	spydata->spy_number = 0;
1067 
1068 	/* Are there are addresses to copy? */
1069 	if(wrqu->data.length > 0) {
1070 		int i;
1071 
1072 		/* Copy addresses */
1073 		for(i = 0; i < wrqu->data.length; i++)
1074 			memcpy(spydata->spy_address[i], address[i].sa_data,
1075 			       ETH_ALEN);
1076 		/* Reset stats */
1077 		memset(spydata->spy_stat, 0,
1078 		       sizeof(struct iw_quality) * IW_MAX_SPY);
1079 
1080 #ifdef WE_SPY_DEBUG
1081 		printk(KERN_DEBUG "iw_handler_set_spy() :  offset %ld, spydata %p, num %d\n", dev->wireless_handlers->spy_offset, spydata, wrqu->data.length);
1082 		for (i = 0; i < wrqu->data.length; i++)
1083 			printk(KERN_DEBUG
1084 			       "%02X:%02X:%02X:%02X:%02X:%02X \n",
1085 			       spydata->spy_address[i][0],
1086 			       spydata->spy_address[i][1],
1087 			       spydata->spy_address[i][2],
1088 			       spydata->spy_address[i][3],
1089 			       spydata->spy_address[i][4],
1090 			       spydata->spy_address[i][5]);
1091 #endif	/* WE_SPY_DEBUG */
1092 	}
1093 	/* Enable addresses */
1094 	spydata->spy_number = wrqu->data.length;
1095 
1096 	return 0;
1097 #else /* IW_WIRELESS_SPY */
1098 	return -EOPNOTSUPP;
1099 #endif /* IW_WIRELESS_SPY */
1100 }
1101 
1102 /*------------------------------------------------------------------*/
1103 /*
1104  * Standard Wireless Handler : get Spy List
1105  */
iw_handler_get_spy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1106 int iw_handler_get_spy(struct net_device *	dev,
1107 		       struct iw_request_info *	info,
1108 		       union iwreq_data *	wrqu,
1109 		       char *			extra)
1110 {
1111 #ifdef IW_WIRELESS_SPY
1112 	struct iw_spy_data *	spydata = (dev->priv +
1113 					   dev->wireless_handlers->spy_offset);
1114 	struct sockaddr *	address = (struct sockaddr *) extra;
1115 	int			i;
1116 
1117 	wrqu->data.length = spydata->spy_number;
1118 
1119 	/* Copy addresses. */
1120 	for(i = 0; i < spydata->spy_number; i++) 	{
1121 		memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
1122 		address[i].sa_family = AF_UNIX;
1123 	}
1124 	/* Copy stats to the user buffer (just after). */
1125 	if(spydata->spy_number > 0)
1126 		memcpy(extra  + (sizeof(struct sockaddr) *spydata->spy_number),
1127 		       spydata->spy_stat,
1128 		       sizeof(struct iw_quality) * spydata->spy_number);
1129 	/* Reset updated flags. */
1130 	for(i = 0; i < spydata->spy_number; i++)
1131 		spydata->spy_stat[i].updated = 0;
1132 	return 0;
1133 #else /* IW_WIRELESS_SPY */
1134 	return -EOPNOTSUPP;
1135 #endif /* IW_WIRELESS_SPY */
1136 }
1137 
1138 /*------------------------------------------------------------------*/
1139 /*
1140  * Standard Wireless Handler : set spy threshold
1141  */
iw_handler_set_thrspy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1142 int iw_handler_set_thrspy(struct net_device *	dev,
1143 			  struct iw_request_info *info,
1144 			  union iwreq_data *	wrqu,
1145 			  char *		extra)
1146 {
1147 #ifdef IW_WIRELESS_THRSPY
1148 	struct iw_spy_data *	spydata = (dev->priv +
1149 					   dev->wireless_handlers->spy_offset);
1150 	struct iw_thrspy *	threshold = (struct iw_thrspy *) extra;
1151 
1152 	/* Just do it */
1153 	memcpy(&(spydata->spy_thr_low), &(threshold->low),
1154 	       2 * sizeof(struct iw_quality));
1155 
1156 	/* Clear flag */
1157 	memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1158 
1159 #ifdef WE_SPY_DEBUG
1160 	printk(KERN_DEBUG "iw_handler_set_thrspy() :  low %d ; high %d\n", spydata->spy_thr_low.level, spydata->spy_thr_high.level);
1161 #endif	/* WE_SPY_DEBUG */
1162 
1163 	return 0;
1164 #else /* IW_WIRELESS_THRSPY */
1165 	return -EOPNOTSUPP;
1166 #endif /* IW_WIRELESS_THRSPY */
1167 }
1168 
1169 /*------------------------------------------------------------------*/
1170 /*
1171  * Standard Wireless Handler : get spy threshold
1172  */
iw_handler_get_thrspy(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1173 int iw_handler_get_thrspy(struct net_device *	dev,
1174 			  struct iw_request_info *info,
1175 			  union iwreq_data *	wrqu,
1176 			  char *		extra)
1177 {
1178 #ifdef IW_WIRELESS_THRSPY
1179 	struct iw_spy_data *	spydata = (dev->priv +
1180 					   dev->wireless_handlers->spy_offset);
1181 	struct iw_thrspy *	threshold = (struct iw_thrspy *) extra;
1182 
1183 	/* Just do it */
1184 	memcpy(&(threshold->low), &(spydata->spy_thr_low),
1185 	       2 * sizeof(struct iw_quality));
1186 
1187 	return 0;
1188 #else /* IW_WIRELESS_THRSPY */
1189 	return -EOPNOTSUPP;
1190 #endif /* IW_WIRELESS_THRSPY */
1191 }
1192 
1193 #ifdef IW_WIRELESS_THRSPY
1194 /*------------------------------------------------------------------*/
1195 /*
1196  * Prepare and send a Spy Threshold event
1197  */
iw_send_thrspy_event(struct net_device * dev,struct iw_spy_data * spydata,unsigned char * address,struct iw_quality * wstats)1198 static void iw_send_thrspy_event(struct net_device *	dev,
1199 				 struct iw_spy_data *	spydata,
1200 				 unsigned char *	address,
1201 				 struct iw_quality *	wstats)
1202 {
1203 	union iwreq_data	wrqu;
1204 	struct iw_thrspy	threshold;
1205 
1206 	/* Init */
1207 	wrqu.data.length = 1;
1208 	wrqu.data.flags = 0;
1209 	/* Copy address */
1210 	memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1211 	threshold.addr.sa_family = ARPHRD_ETHER;
1212 	/* Copy stats */
1213 	memcpy(&(threshold.qual), wstats, sizeof(struct iw_quality));
1214 	/* Copy also thresholds */
1215 	memcpy(&(threshold.low), &(spydata->spy_thr_low),
1216 	       2 * sizeof(struct iw_quality));
1217 
1218 #ifdef WE_SPY_DEBUG
1219 	printk(KERN_DEBUG "iw_send_thrspy_event() : address %02X:%02X:%02X:%02X:%02X:%02X, level %d, up = %d\n",
1220 	       threshold.addr.sa_data[0],
1221 	       threshold.addr.sa_data[1],
1222 	       threshold.addr.sa_data[2],
1223 	       threshold.addr.sa_data[3],
1224 	       threshold.addr.sa_data[4],
1225 	       threshold.addr.sa_data[5], threshold.qual.level);
1226 #endif	/* WE_SPY_DEBUG */
1227 
1228 	/* Send event to user space */
1229 	wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1230 }
1231 #endif /* IW_WIRELESS_THRSPY */
1232 
1233 /* ---------------------------------------------------------------- */
1234 /*
1235  * Call for the driver to update the spy data.
1236  * For now, the spy data is a simple array. As the size of the array is
1237  * small, this is good enough. If we wanted to support larger number of
1238  * spy addresses, we should use something more efficient...
1239  */
wireless_spy_update(struct net_device * dev,unsigned char * address,struct iw_quality * wstats)1240 void wireless_spy_update(struct net_device *	dev,
1241 			 unsigned char *	address,
1242 			 struct iw_quality *	wstats)
1243 {
1244 #ifdef IW_WIRELESS_SPY
1245 	struct iw_spy_data *	spydata = (dev->priv +
1246 					   dev->wireless_handlers->spy_offset);
1247 	int			i;
1248 	int			match = -1;
1249 
1250 #ifdef WE_SPY_DEBUG
1251 	printk(KERN_DEBUG "wireless_spy_update() :  offset %ld, spydata %p, address %02X:%02X:%02X:%02X:%02X:%02X\n", dev->wireless_handlers->spy_offset, spydata, address[0], address[1], address[2], address[3], address[4], address[5]);
1252 #endif	/* WE_SPY_DEBUG */
1253 
1254 	/* Update all records that match */
1255 	for(i = 0; i < spydata->spy_number; i++)
1256 		if(!memcmp(address, spydata->spy_address[i], ETH_ALEN)) {
1257 			memcpy(&(spydata->spy_stat[i]), wstats,
1258 			       sizeof(struct iw_quality));
1259 			match = i;
1260 		}
1261 #ifdef IW_WIRELESS_THRSPY
1262 	/* Generate an event if we cross the spy threshold.
1263 	 * To avoid event storms, we have a simple hysteresis : we generate
1264 	 * event only when we go under the low threshold or above the
1265 	 * high threshold. */
1266 	if(match >= 0) {
1267 		if(spydata->spy_thr_under[match]) {
1268 			if(wstats->level > spydata->spy_thr_high.level) {
1269 				spydata->spy_thr_under[match] = 0;
1270 				iw_send_thrspy_event(dev, spydata,
1271 						     address, wstats);
1272 			}
1273 		} else {
1274 			if(wstats->level < spydata->spy_thr_low.level) {
1275 				spydata->spy_thr_under[match] = 1;
1276 				iw_send_thrspy_event(dev, spydata,
1277 						     address, wstats);
1278 			}
1279 		}
1280 	}
1281 #endif /* IW_WIRELESS_THRSPY */
1282 #endif /* IW_WIRELESS_SPY */
1283 }
1284