18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file implement the Wireless Extensions spy API.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Authors :	Jean Tourrilhes - HPL - <jt@hpl.hp.com>
58c2ecf20Sopenharmony_ci * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * (As all part of the Linux kernel, this file is GPL)
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/wireless.h>
118c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
128c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
138c2ecf20Sopenharmony_ci#include <linux/export.h>
148c2ecf20Sopenharmony_ci#include <net/iw_handler.h>
158c2ecf20Sopenharmony_ci#include <net/arp.h>
168c2ecf20Sopenharmony_ci#include <net/wext.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic inline struct iw_spy_data *get_spydata(struct net_device *dev)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	/* This is the new way */
218c2ecf20Sopenharmony_ci	if (dev->wireless_data)
228c2ecf20Sopenharmony_ci		return dev->wireless_data->spy_data;
238c2ecf20Sopenharmony_ci	return NULL;
248c2ecf20Sopenharmony_ci}
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciint iw_handler_set_spy(struct net_device *	dev,
278c2ecf20Sopenharmony_ci		       struct iw_request_info *	info,
288c2ecf20Sopenharmony_ci		       union iwreq_data *	wrqu,
298c2ecf20Sopenharmony_ci		       char *			extra)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct iw_spy_data *	spydata = get_spydata(dev);
328c2ecf20Sopenharmony_ci	struct sockaddr *	address = (struct sockaddr *) extra;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	/* Make sure driver is not buggy or using the old API */
358c2ecf20Sopenharmony_ci	if (!spydata)
368c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* Disable spy collection while we copy the addresses.
398c2ecf20Sopenharmony_ci	 * While we copy addresses, any call to wireless_spy_update()
408c2ecf20Sopenharmony_ci	 * will NOP. This is OK, as anyway the addresses are changing. */
418c2ecf20Sopenharmony_ci	spydata->spy_number = 0;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* We want to operate without locking, because wireless_spy_update()
448c2ecf20Sopenharmony_ci	 * most likely will happen in the interrupt handler, and therefore
458c2ecf20Sopenharmony_ci	 * have its own locking constraints and needs performance.
468c2ecf20Sopenharmony_ci	 * The rtnl_lock() make sure we don't race with the other iw_handlers.
478c2ecf20Sopenharmony_ci	 * This make sure wireless_spy_update() "see" that the spy list
488c2ecf20Sopenharmony_ci	 * is temporarily disabled. */
498c2ecf20Sopenharmony_ci	smp_wmb();
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* Are there are addresses to copy? */
528c2ecf20Sopenharmony_ci	if (wrqu->data.length > 0) {
538c2ecf20Sopenharmony_ci		int i;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		/* Copy addresses */
568c2ecf20Sopenharmony_ci		for (i = 0; i < wrqu->data.length; i++)
578c2ecf20Sopenharmony_ci			memcpy(spydata->spy_address[i], address[i].sa_data,
588c2ecf20Sopenharmony_ci			       ETH_ALEN);
598c2ecf20Sopenharmony_ci		/* Reset stats */
608c2ecf20Sopenharmony_ci		memset(spydata->spy_stat, 0,
618c2ecf20Sopenharmony_ci		       sizeof(struct iw_quality) * IW_MAX_SPY);
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Make sure above is updated before re-enabling */
658c2ecf20Sopenharmony_ci	smp_wmb();
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	/* Enable addresses */
688c2ecf20Sopenharmony_ci	spydata->spy_number = wrqu->data.length;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iw_handler_set_spy);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciint iw_handler_get_spy(struct net_device *	dev,
758c2ecf20Sopenharmony_ci		       struct iw_request_info *	info,
768c2ecf20Sopenharmony_ci		       union iwreq_data *	wrqu,
778c2ecf20Sopenharmony_ci		       char *			extra)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct iw_spy_data *	spydata = get_spydata(dev);
808c2ecf20Sopenharmony_ci	struct sockaddr *	address = (struct sockaddr *) extra;
818c2ecf20Sopenharmony_ci	int			i;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Make sure driver is not buggy or using the old API */
848c2ecf20Sopenharmony_ci	if (!spydata)
858c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	wrqu->data.length = spydata->spy_number;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Copy addresses. */
908c2ecf20Sopenharmony_ci	for (i = 0; i < spydata->spy_number; i++) 	{
918c2ecf20Sopenharmony_ci		memcpy(address[i].sa_data, spydata->spy_address[i], ETH_ALEN);
928c2ecf20Sopenharmony_ci		address[i].sa_family = AF_UNIX;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci	/* Copy stats to the user buffer (just after). */
958c2ecf20Sopenharmony_ci	if (spydata->spy_number > 0)
968c2ecf20Sopenharmony_ci		memcpy(extra  + (sizeof(struct sockaddr) *spydata->spy_number),
978c2ecf20Sopenharmony_ci		       spydata->spy_stat,
988c2ecf20Sopenharmony_ci		       sizeof(struct iw_quality) * spydata->spy_number);
998c2ecf20Sopenharmony_ci	/* Reset updated flags. */
1008c2ecf20Sopenharmony_ci	for (i = 0; i < spydata->spy_number; i++)
1018c2ecf20Sopenharmony_ci		spydata->spy_stat[i].updated &= ~IW_QUAL_ALL_UPDATED;
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iw_handler_get_spy);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/*------------------------------------------------------------------*/
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * Standard Wireless Handler : set spy threshold
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_ciint iw_handler_set_thrspy(struct net_device *	dev,
1118c2ecf20Sopenharmony_ci			  struct iw_request_info *info,
1128c2ecf20Sopenharmony_ci			  union iwreq_data *	wrqu,
1138c2ecf20Sopenharmony_ci			  char *		extra)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct iw_spy_data *	spydata = get_spydata(dev);
1168c2ecf20Sopenharmony_ci	struct iw_thrspy *	threshold = (struct iw_thrspy *) extra;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/* Make sure driver is not buggy or using the old API */
1198c2ecf20Sopenharmony_ci	if (!spydata)
1208c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Just do it */
1238c2ecf20Sopenharmony_ci	spydata->spy_thr_low = threshold->low;
1248c2ecf20Sopenharmony_ci	spydata->spy_thr_high = threshold->high;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* Clear flag */
1278c2ecf20Sopenharmony_ci	memset(spydata->spy_thr_under, '\0', sizeof(spydata->spy_thr_under));
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iw_handler_set_thrspy);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*------------------------------------------------------------------*/
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * Standard Wireless Handler : get spy threshold
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_ciint iw_handler_get_thrspy(struct net_device *	dev,
1388c2ecf20Sopenharmony_ci			  struct iw_request_info *info,
1398c2ecf20Sopenharmony_ci			  union iwreq_data *	wrqu,
1408c2ecf20Sopenharmony_ci			  char *		extra)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct iw_spy_data *	spydata = get_spydata(dev);
1438c2ecf20Sopenharmony_ci	struct iw_thrspy *	threshold = (struct iw_thrspy *) extra;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* Make sure driver is not buggy or using the old API */
1468c2ecf20Sopenharmony_ci	if (!spydata)
1478c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* Just do it */
1508c2ecf20Sopenharmony_ci	threshold->low = spydata->spy_thr_low;
1518c2ecf20Sopenharmony_ci	threshold->high = spydata->spy_thr_high;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iw_handler_get_thrspy);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/*------------------------------------------------------------------*/
1588c2ecf20Sopenharmony_ci/*
1598c2ecf20Sopenharmony_ci * Prepare and send a Spy Threshold event
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_cistatic void iw_send_thrspy_event(struct net_device *	dev,
1628c2ecf20Sopenharmony_ci				 struct iw_spy_data *	spydata,
1638c2ecf20Sopenharmony_ci				 unsigned char *	address,
1648c2ecf20Sopenharmony_ci				 struct iw_quality *	wstats)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	union iwreq_data	wrqu;
1678c2ecf20Sopenharmony_ci	struct iw_thrspy	threshold;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* Init */
1708c2ecf20Sopenharmony_ci	wrqu.data.length = 1;
1718c2ecf20Sopenharmony_ci	wrqu.data.flags = 0;
1728c2ecf20Sopenharmony_ci	/* Copy address */
1738c2ecf20Sopenharmony_ci	memcpy(threshold.addr.sa_data, address, ETH_ALEN);
1748c2ecf20Sopenharmony_ci	threshold.addr.sa_family = ARPHRD_ETHER;
1758c2ecf20Sopenharmony_ci	/* Copy stats */
1768c2ecf20Sopenharmony_ci	threshold.qual = *wstats;
1778c2ecf20Sopenharmony_ci	/* Copy also thresholds */
1788c2ecf20Sopenharmony_ci	threshold.low = spydata->spy_thr_low;
1798c2ecf20Sopenharmony_ci	threshold.high = spydata->spy_thr_high;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* Send event to user space */
1828c2ecf20Sopenharmony_ci	wireless_send_event(dev, SIOCGIWTHRSPY, &wrqu, (char *) &threshold);
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------- */
1868c2ecf20Sopenharmony_ci/*
1878c2ecf20Sopenharmony_ci * Call for the driver to update the spy data.
1888c2ecf20Sopenharmony_ci * For now, the spy data is a simple array. As the size of the array is
1898c2ecf20Sopenharmony_ci * small, this is good enough. If we wanted to support larger number of
1908c2ecf20Sopenharmony_ci * spy addresses, we should use something more efficient...
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_civoid wireless_spy_update(struct net_device *	dev,
1938c2ecf20Sopenharmony_ci			 unsigned char *	address,
1948c2ecf20Sopenharmony_ci			 struct iw_quality *	wstats)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct iw_spy_data *	spydata = get_spydata(dev);
1978c2ecf20Sopenharmony_ci	int			i;
1988c2ecf20Sopenharmony_ci	int			match = -1;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Make sure driver is not buggy or using the old API */
2018c2ecf20Sopenharmony_ci	if (!spydata)
2028c2ecf20Sopenharmony_ci		return;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* Update all records that match */
2058c2ecf20Sopenharmony_ci	for (i = 0; i < spydata->spy_number; i++)
2068c2ecf20Sopenharmony_ci		if (ether_addr_equal(address, spydata->spy_address[i])) {
2078c2ecf20Sopenharmony_ci			memcpy(&(spydata->spy_stat[i]), wstats,
2088c2ecf20Sopenharmony_ci			       sizeof(struct iw_quality));
2098c2ecf20Sopenharmony_ci			match = i;
2108c2ecf20Sopenharmony_ci		}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* Generate an event if we cross the spy threshold.
2138c2ecf20Sopenharmony_ci	 * To avoid event storms, we have a simple hysteresis : we generate
2148c2ecf20Sopenharmony_ci	 * event only when we go under the low threshold or above the
2158c2ecf20Sopenharmony_ci	 * high threshold. */
2168c2ecf20Sopenharmony_ci	if (match >= 0) {
2178c2ecf20Sopenharmony_ci		if (spydata->spy_thr_under[match]) {
2188c2ecf20Sopenharmony_ci			if (wstats->level > spydata->spy_thr_high.level) {
2198c2ecf20Sopenharmony_ci				spydata->spy_thr_under[match] = 0;
2208c2ecf20Sopenharmony_ci				iw_send_thrspy_event(dev, spydata,
2218c2ecf20Sopenharmony_ci						     address, wstats);
2228c2ecf20Sopenharmony_ci			}
2238c2ecf20Sopenharmony_ci		} else {
2248c2ecf20Sopenharmony_ci			if (wstats->level < spydata->spy_thr_low.level) {
2258c2ecf20Sopenharmony_ci				spydata->spy_thr_under[match] = 1;
2268c2ecf20Sopenharmony_ci				iw_send_thrspy_event(dev, spydata,
2278c2ecf20Sopenharmony_ci						     address, wstats);
2288c2ecf20Sopenharmony_ci			}
2298c2ecf20Sopenharmony_ci		}
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(wireless_spy_update);
233