Linux Audio

Check our new training course

Loading...
v6.13.7
   1/*
   2 * This file implement the Wireless Extensions core API.
   3 *
   4 * Authors :	Jean Tourrilhes - HPL - <jt@hpl.hp.com>
   5 * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
   6 * Copyright	2009 Johannes Berg <johannes@sipsolutions.net>
   7 * Copyright (C) 2024 Intel Corporation
   8 *
   9 * (As all part of the Linux kernel, this file is GPL)
  10 */
  11#include <linux/kernel.h>
  12#include <linux/netdevice.h>
  13#include <linux/rtnetlink.h>
  14#include <linux/slab.h>
  15#include <linux/wireless.h>
  16#include <linux/uaccess.h>
  17#include <linux/export.h>
  18#include <net/cfg80211.h>
  19#include <net/iw_handler.h>
  20#include <net/netlink.h>
  21#include <net/wext.h>
  22#include <net/net_namespace.h>
  23
  24typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  25			       unsigned int, struct iw_request_info *,
  26			       iw_handler);
  27
  28
  29/*
  30 * Meta-data about all the standard Wireless Extension request we
  31 * know about.
  32 */
  33static const struct iw_ioctl_description standard_ioctl[] = {
  34	[IW_IOCTL_IDX(SIOCSIWCOMMIT)] = {
  35		.header_type	= IW_HEADER_TYPE_NULL,
  36	},
  37	[IW_IOCTL_IDX(SIOCGIWNAME)] = {
  38		.header_type	= IW_HEADER_TYPE_CHAR,
  39		.flags		= IW_DESCR_FLAG_DUMP,
  40	},
  41	[IW_IOCTL_IDX(SIOCSIWNWID)] = {
  42		.header_type	= IW_HEADER_TYPE_PARAM,
  43		.flags		= IW_DESCR_FLAG_EVENT,
  44	},
  45	[IW_IOCTL_IDX(SIOCGIWNWID)] = {
  46		.header_type	= IW_HEADER_TYPE_PARAM,
  47		.flags		= IW_DESCR_FLAG_DUMP,
  48	},
  49	[IW_IOCTL_IDX(SIOCSIWFREQ)] = {
  50		.header_type	= IW_HEADER_TYPE_FREQ,
  51		.flags		= IW_DESCR_FLAG_EVENT,
  52	},
  53	[IW_IOCTL_IDX(SIOCGIWFREQ)] = {
  54		.header_type	= IW_HEADER_TYPE_FREQ,
  55		.flags		= IW_DESCR_FLAG_DUMP,
  56	},
  57	[IW_IOCTL_IDX(SIOCSIWMODE)] = {
  58		.header_type	= IW_HEADER_TYPE_UINT,
  59		.flags		= IW_DESCR_FLAG_EVENT,
  60	},
  61	[IW_IOCTL_IDX(SIOCGIWMODE)] = {
  62		.header_type	= IW_HEADER_TYPE_UINT,
  63		.flags		= IW_DESCR_FLAG_DUMP,
  64	},
  65	[IW_IOCTL_IDX(SIOCSIWSENS)] = {
  66		.header_type	= IW_HEADER_TYPE_PARAM,
  67	},
  68	[IW_IOCTL_IDX(SIOCGIWSENS)] = {
  69		.header_type	= IW_HEADER_TYPE_PARAM,
  70	},
  71	[IW_IOCTL_IDX(SIOCSIWRANGE)] = {
  72		.header_type	= IW_HEADER_TYPE_NULL,
  73	},
  74	[IW_IOCTL_IDX(SIOCGIWRANGE)] = {
  75		.header_type	= IW_HEADER_TYPE_POINT,
  76		.token_size	= 1,
  77		.max_tokens	= sizeof(struct iw_range),
  78		.flags		= IW_DESCR_FLAG_DUMP,
  79	},
  80	[IW_IOCTL_IDX(SIOCSIWPRIV)] = {
  81		.header_type	= IW_HEADER_TYPE_NULL,
  82	},
  83	[IW_IOCTL_IDX(SIOCGIWPRIV)] = { /* (handled directly by us) */
  84		.header_type	= IW_HEADER_TYPE_POINT,
  85		.token_size	= sizeof(struct iw_priv_args),
  86		.max_tokens	= 16,
  87		.flags		= IW_DESCR_FLAG_NOMAX,
  88	},
  89	[IW_IOCTL_IDX(SIOCSIWSTATS)] = {
  90		.header_type	= IW_HEADER_TYPE_NULL,
  91	},
  92	[IW_IOCTL_IDX(SIOCGIWSTATS)] = { /* (handled directly by us) */
  93		.header_type	= IW_HEADER_TYPE_POINT,
  94		.token_size	= 1,
  95		.max_tokens	= sizeof(struct iw_statistics),
  96		.flags		= IW_DESCR_FLAG_DUMP,
  97	},
  98	[IW_IOCTL_IDX(SIOCSIWSPY)] = {
  99		.header_type	= IW_HEADER_TYPE_POINT,
 100		.token_size	= sizeof(struct sockaddr),
 101		.max_tokens	= IW_MAX_SPY,
 102	},
 103	[IW_IOCTL_IDX(SIOCGIWSPY)] = {
 104		.header_type	= IW_HEADER_TYPE_POINT,
 105		.token_size	= sizeof(struct sockaddr) +
 106				  sizeof(struct iw_quality),
 107		.max_tokens	= IW_MAX_SPY,
 108	},
 109	[IW_IOCTL_IDX(SIOCSIWTHRSPY)] = {
 110		.header_type	= IW_HEADER_TYPE_POINT,
 111		.token_size	= sizeof(struct iw_thrspy),
 112		.min_tokens	= 1,
 113		.max_tokens	= 1,
 114	},
 115	[IW_IOCTL_IDX(SIOCGIWTHRSPY)] = {
 116		.header_type	= IW_HEADER_TYPE_POINT,
 117		.token_size	= sizeof(struct iw_thrspy),
 118		.min_tokens	= 1,
 119		.max_tokens	= 1,
 120	},
 121	[IW_IOCTL_IDX(SIOCSIWAP)] = {
 122		.header_type	= IW_HEADER_TYPE_ADDR,
 123	},
 124	[IW_IOCTL_IDX(SIOCGIWAP)] = {
 125		.header_type	= IW_HEADER_TYPE_ADDR,
 126		.flags		= IW_DESCR_FLAG_DUMP,
 127	},
 128	[IW_IOCTL_IDX(SIOCSIWMLME)] = {
 129		.header_type	= IW_HEADER_TYPE_POINT,
 130		.token_size	= 1,
 131		.min_tokens	= sizeof(struct iw_mlme),
 132		.max_tokens	= sizeof(struct iw_mlme),
 133	},
 134	[IW_IOCTL_IDX(SIOCGIWAPLIST)] = {
 135		.header_type	= IW_HEADER_TYPE_POINT,
 136		.token_size	= sizeof(struct sockaddr) +
 137				  sizeof(struct iw_quality),
 138		.max_tokens	= IW_MAX_AP,
 139		.flags		= IW_DESCR_FLAG_NOMAX,
 140	},
 141	[IW_IOCTL_IDX(SIOCSIWSCAN)] = {
 142		.header_type	= IW_HEADER_TYPE_POINT,
 143		.token_size	= 1,
 144		.min_tokens	= 0,
 145		.max_tokens	= sizeof(struct iw_scan_req),
 146	},
 147	[IW_IOCTL_IDX(SIOCGIWSCAN)] = {
 148		.header_type	= IW_HEADER_TYPE_POINT,
 149		.token_size	= 1,
 150		.max_tokens	= IW_SCAN_MAX_DATA,
 151		.flags		= IW_DESCR_FLAG_NOMAX,
 152	},
 153	[IW_IOCTL_IDX(SIOCSIWESSID)] = {
 154		.header_type	= IW_HEADER_TYPE_POINT,
 155		.token_size	= 1,
 156		.max_tokens	= IW_ESSID_MAX_SIZE,
 157		.flags		= IW_DESCR_FLAG_EVENT,
 158	},
 159	[IW_IOCTL_IDX(SIOCGIWESSID)] = {
 160		.header_type	= IW_HEADER_TYPE_POINT,
 161		.token_size	= 1,
 162		.max_tokens	= IW_ESSID_MAX_SIZE,
 163		.flags		= IW_DESCR_FLAG_DUMP,
 164	},
 165	[IW_IOCTL_IDX(SIOCSIWNICKN)] = {
 166		.header_type	= IW_HEADER_TYPE_POINT,
 167		.token_size	= 1,
 168		.max_tokens	= IW_ESSID_MAX_SIZE,
 169	},
 170	[IW_IOCTL_IDX(SIOCGIWNICKN)] = {
 171		.header_type	= IW_HEADER_TYPE_POINT,
 172		.token_size	= 1,
 173		.max_tokens	= IW_ESSID_MAX_SIZE,
 174	},
 175	[IW_IOCTL_IDX(SIOCSIWRATE)] = {
 176		.header_type	= IW_HEADER_TYPE_PARAM,
 177	},
 178	[IW_IOCTL_IDX(SIOCGIWRATE)] = {
 179		.header_type	= IW_HEADER_TYPE_PARAM,
 180	},
 181	[IW_IOCTL_IDX(SIOCSIWRTS)] = {
 182		.header_type	= IW_HEADER_TYPE_PARAM,
 183	},
 184	[IW_IOCTL_IDX(SIOCGIWRTS)] = {
 185		.header_type	= IW_HEADER_TYPE_PARAM,
 186	},
 187	[IW_IOCTL_IDX(SIOCSIWFRAG)] = {
 188		.header_type	= IW_HEADER_TYPE_PARAM,
 189	},
 190	[IW_IOCTL_IDX(SIOCGIWFRAG)] = {
 191		.header_type	= IW_HEADER_TYPE_PARAM,
 192	},
 193	[IW_IOCTL_IDX(SIOCSIWTXPOW)] = {
 194		.header_type	= IW_HEADER_TYPE_PARAM,
 195	},
 196	[IW_IOCTL_IDX(SIOCGIWTXPOW)] = {
 197		.header_type	= IW_HEADER_TYPE_PARAM,
 198	},
 199	[IW_IOCTL_IDX(SIOCSIWRETRY)] = {
 200		.header_type	= IW_HEADER_TYPE_PARAM,
 201	},
 202	[IW_IOCTL_IDX(SIOCGIWRETRY)] = {
 203		.header_type	= IW_HEADER_TYPE_PARAM,
 204	},
 205	[IW_IOCTL_IDX(SIOCSIWENCODE)] = {
 206		.header_type	= IW_HEADER_TYPE_POINT,
 207		.token_size	= 1,
 208		.max_tokens	= IW_ENCODING_TOKEN_MAX,
 209		.flags		= IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
 210	},
 211	[IW_IOCTL_IDX(SIOCGIWENCODE)] = {
 212		.header_type	= IW_HEADER_TYPE_POINT,
 213		.token_size	= 1,
 214		.max_tokens	= IW_ENCODING_TOKEN_MAX,
 215		.flags		= IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
 216	},
 217	[IW_IOCTL_IDX(SIOCSIWPOWER)] = {
 218		.header_type	= IW_HEADER_TYPE_PARAM,
 219	},
 220	[IW_IOCTL_IDX(SIOCGIWPOWER)] = {
 221		.header_type	= IW_HEADER_TYPE_PARAM,
 222	},
 223	[IW_IOCTL_IDX(SIOCSIWGENIE)] = {
 224		.header_type	= IW_HEADER_TYPE_POINT,
 225		.token_size	= 1,
 226		.max_tokens	= IW_GENERIC_IE_MAX,
 227	},
 228	[IW_IOCTL_IDX(SIOCGIWGENIE)] = {
 229		.header_type	= IW_HEADER_TYPE_POINT,
 230		.token_size	= 1,
 231		.max_tokens	= IW_GENERIC_IE_MAX,
 232	},
 233	[IW_IOCTL_IDX(SIOCSIWAUTH)] = {
 234		.header_type	= IW_HEADER_TYPE_PARAM,
 235	},
 236	[IW_IOCTL_IDX(SIOCGIWAUTH)] = {
 237		.header_type	= IW_HEADER_TYPE_PARAM,
 238	},
 239	[IW_IOCTL_IDX(SIOCSIWENCODEEXT)] = {
 240		.header_type	= IW_HEADER_TYPE_POINT,
 241		.token_size	= 1,
 242		.min_tokens	= sizeof(struct iw_encode_ext),
 243		.max_tokens	= sizeof(struct iw_encode_ext) +
 244				  IW_ENCODING_TOKEN_MAX,
 245	},
 246	[IW_IOCTL_IDX(SIOCGIWENCODEEXT)] = {
 247		.header_type	= IW_HEADER_TYPE_POINT,
 248		.token_size	= 1,
 249		.min_tokens	= sizeof(struct iw_encode_ext),
 250		.max_tokens	= sizeof(struct iw_encode_ext) +
 251				  IW_ENCODING_TOKEN_MAX,
 252	},
 253	[IW_IOCTL_IDX(SIOCSIWPMKSA)] = {
 254		.header_type	= IW_HEADER_TYPE_POINT,
 255		.token_size	= 1,
 256		.min_tokens	= sizeof(struct iw_pmksa),
 257		.max_tokens	= sizeof(struct iw_pmksa),
 258	},
 259};
 260static const unsigned int standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
 261
 262/*
 263 * Meta-data about all the additional standard Wireless Extension events
 264 * we know about.
 265 */
 266static const struct iw_ioctl_description standard_event[] = {
 267	[IW_EVENT_IDX(IWEVTXDROP)] = {
 268		.header_type	= IW_HEADER_TYPE_ADDR,
 269	},
 270	[IW_EVENT_IDX(IWEVQUAL)] = {
 271		.header_type	= IW_HEADER_TYPE_QUAL,
 272	},
 273	[IW_EVENT_IDX(IWEVCUSTOM)] = {
 274		.header_type	= IW_HEADER_TYPE_POINT,
 275		.token_size	= 1,
 276		.max_tokens	= IW_CUSTOM_MAX,
 277	},
 278	[IW_EVENT_IDX(IWEVREGISTERED)] = {
 279		.header_type	= IW_HEADER_TYPE_ADDR,
 280	},
 281	[IW_EVENT_IDX(IWEVEXPIRED)] = {
 282		.header_type	= IW_HEADER_TYPE_ADDR,
 283	},
 284	[IW_EVENT_IDX(IWEVGENIE)] = {
 285		.header_type	= IW_HEADER_TYPE_POINT,
 286		.token_size	= 1,
 287		.max_tokens	= IW_GENERIC_IE_MAX,
 288	},
 289	[IW_EVENT_IDX(IWEVMICHAELMICFAILURE)] = {
 290		.header_type	= IW_HEADER_TYPE_POINT,
 291		.token_size	= 1,
 292		.max_tokens	= sizeof(struct iw_michaelmicfailure),
 293	},
 294	[IW_EVENT_IDX(IWEVASSOCREQIE)] = {
 295		.header_type	= IW_HEADER_TYPE_POINT,
 296		.token_size	= 1,
 297		.max_tokens	= IW_GENERIC_IE_MAX,
 298	},
 299	[IW_EVENT_IDX(IWEVASSOCRESPIE)] = {
 300		.header_type	= IW_HEADER_TYPE_POINT,
 301		.token_size	= 1,
 302		.max_tokens	= IW_GENERIC_IE_MAX,
 303	},
 304	[IW_EVENT_IDX(IWEVPMKIDCAND)] = {
 305		.header_type	= IW_HEADER_TYPE_POINT,
 306		.token_size	= 1,
 307		.max_tokens	= sizeof(struct iw_pmkid_cand),
 308	},
 309};
 310static const unsigned int standard_event_num = ARRAY_SIZE(standard_event);
 311
 312/* Size (in bytes) of various events */
 313static const int event_type_size[] = {
 314	IW_EV_LCP_LEN,			/* IW_HEADER_TYPE_NULL */
 315	0,
 316	IW_EV_CHAR_LEN,			/* IW_HEADER_TYPE_CHAR */
 317	0,
 318	IW_EV_UINT_LEN,			/* IW_HEADER_TYPE_UINT */
 319	IW_EV_FREQ_LEN,			/* IW_HEADER_TYPE_FREQ */
 320	IW_EV_ADDR_LEN,			/* IW_HEADER_TYPE_ADDR */
 321	0,
 322	IW_EV_POINT_LEN,		/* Without variable payload */
 323	IW_EV_PARAM_LEN,		/* IW_HEADER_TYPE_PARAM */
 324	IW_EV_QUAL_LEN,			/* IW_HEADER_TYPE_QUAL */
 325};
 326
 327#ifdef CONFIG_COMPAT
 328static const int compat_event_type_size[] = {
 329	IW_EV_COMPAT_LCP_LEN,		/* IW_HEADER_TYPE_NULL */
 330	0,
 331	IW_EV_COMPAT_CHAR_LEN,		/* IW_HEADER_TYPE_CHAR */
 332	0,
 333	IW_EV_COMPAT_UINT_LEN,		/* IW_HEADER_TYPE_UINT */
 334	IW_EV_COMPAT_FREQ_LEN,		/* IW_HEADER_TYPE_FREQ */
 335	IW_EV_COMPAT_ADDR_LEN,		/* IW_HEADER_TYPE_ADDR */
 336	0,
 337	IW_EV_COMPAT_POINT_LEN,		/* Without variable payload */
 338	IW_EV_COMPAT_PARAM_LEN,		/* IW_HEADER_TYPE_PARAM */
 339	IW_EV_COMPAT_QUAL_LEN,		/* IW_HEADER_TYPE_QUAL */
 340};
 341#endif
 342
 343
 344/* IW event code */
 345
 346void wireless_nlevent_flush(void)
 347{
 348	struct sk_buff *skb;
 349	struct net *net;
 350
 351	down_read(&net_rwsem);
 352	for_each_net(net) {
 353		while ((skb = skb_dequeue(&net->wext_nlevents)))
 354			rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
 355				    GFP_KERNEL);
 356	}
 357	up_read(&net_rwsem);
 358}
 359EXPORT_SYMBOL_GPL(wireless_nlevent_flush);
 360
 361static int wext_netdev_notifier_call(struct notifier_block *nb,
 362				     unsigned long state, void *ptr)
 363{
 364	/*
 365	 * When a netdev changes state in any way, flush all pending messages
 366	 * to avoid them going out in a strange order, e.g. RTM_NEWLINK after
 367	 * RTM_DELLINK, or with IFF_UP after without IFF_UP during dev_close()
 368	 * or similar - all of which could otherwise happen due to delays from
 369	 * schedule_work().
 370	 */
 371	wireless_nlevent_flush();
 372
 373	return NOTIFY_OK;
 374}
 375
 376static struct notifier_block wext_netdev_notifier = {
 377	.notifier_call = wext_netdev_notifier_call,
 378};
 379
 380static int __net_init wext_pernet_init(struct net *net)
 381{
 382	skb_queue_head_init(&net->wext_nlevents);
 383	return 0;
 384}
 385
 386static void __net_exit wext_pernet_exit(struct net *net)
 387{
 388	skb_queue_purge(&net->wext_nlevents);
 389}
 390
 391static struct pernet_operations wext_pernet_ops = {
 392	.init = wext_pernet_init,
 393	.exit = wext_pernet_exit,
 394};
 395
 396static int __init wireless_nlevent_init(void)
 397{
 398	int err = register_pernet_subsys(&wext_pernet_ops);
 399
 400	if (err)
 401		return err;
 402
 403	err = register_netdevice_notifier(&wext_netdev_notifier);
 404	if (err)
 405		unregister_pernet_subsys(&wext_pernet_ops);
 406	return err;
 407}
 408
 409subsys_initcall(wireless_nlevent_init);
 410
 411/* Process events generated by the wireless layer or the driver. */
 412static void wireless_nlevent_process(struct work_struct *work)
 413{
 414	wireless_nlevent_flush();
 
 
 
 
 
 
 
 
 
 
 
 415}
 416
 417static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
 418
 419static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
 420					      struct sk_buff *skb)
 421{
 422	struct ifinfomsg *r;
 423	struct nlmsghdr  *nlh;
 424
 425	nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
 426	if (!nlh)
 427		return NULL;
 428
 429	r = nlmsg_data(nlh);
 430	r->ifi_family = AF_UNSPEC;
 431	r->__ifi_pad = 0;
 432	r->ifi_type = dev->type;
 433	r->ifi_index = dev->ifindex;
 434	r->ifi_flags = dev_get_flags(dev);
 435	r->ifi_change = 0;	/* Wireless changes don't affect those flags */
 436
 437	if (nla_put_string(skb, IFLA_IFNAME, dev->name))
 438		goto nla_put_failure;
 439
 440	return nlh;
 441 nla_put_failure:
 442	nlmsg_cancel(skb, nlh);
 443	return NULL;
 444}
 445
 446
 447/*
 448 * Main event dispatcher. Called from other parts and drivers.
 449 * Send the event on the appropriate channels.
 450 * May be called from interrupt context.
 451 */
 452void wireless_send_event(struct net_device *	dev,
 453			 unsigned int		cmd,
 454			 union iwreq_data *	wrqu,
 455			 const char *		extra)
 456{
 457	const struct iw_ioctl_description *	descr = NULL;
 458	int extra_len = 0;
 459	struct iw_event  *event;		/* Mallocated whole event */
 460	int event_len;				/* Its size */
 461	int hdr_len;				/* Size of the event header */
 462	int wrqu_off = 0;			/* Offset in wrqu */
 463	/* Don't "optimise" the following variable, it will crash */
 464	unsigned int	cmd_index;		/* *MUST* be unsigned */
 465	struct sk_buff *skb;
 466	struct nlmsghdr *nlh;
 467	struct nlattr *nla;
 468#ifdef CONFIG_COMPAT
 469	struct __compat_iw_event *compat_event;
 470	struct compat_iw_point compat_wrqu;
 471	struct sk_buff *compskb;
 472	int ptr_len;
 473#endif
 474
 475	/*
 476	 * Nothing in the kernel sends scan events with data, be safe.
 477	 * This is necessary because we cannot fix up scan event data
 478	 * for compat, due to being contained in 'extra', but normally
 479	 * applications are required to retrieve the scan data anyway
 480	 * and no data is included in the event, this codifies that
 481	 * practice.
 482	 */
 483	if (WARN_ON(cmd == SIOCGIWSCAN && extra))
 484		extra = NULL;
 485
 486	/* Get the description of the Event */
 487	if (cmd <= SIOCIWLAST) {
 488		cmd_index = IW_IOCTL_IDX(cmd);
 489		if (cmd_index < standard_ioctl_num)
 490			descr = &(standard_ioctl[cmd_index]);
 491	} else {
 492		cmd_index = IW_EVENT_IDX(cmd);
 493		if (cmd_index < standard_event_num)
 494			descr = &(standard_event[cmd_index]);
 495	}
 496	/* Don't accept unknown events */
 497	if (descr == NULL) {
 498		/* Note : we don't return an error to the driver, because
 499		 * the driver would not know what to do about it. It can't
 500		 * return an error to the user, because the event is not
 501		 * initiated by a user request.
 502		 * The best the driver could do is to log an error message.
 503		 * We will do it ourselves instead...
 504		 */
 505		netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
 506			   cmd);
 507		return;
 508	}
 509
 510	/* Check extra parameters and set extra_len */
 511	if (descr->header_type == IW_HEADER_TYPE_POINT) {
 512		/* Check if number of token fits within bounds */
 513		if (wrqu->data.length > descr->max_tokens) {
 514			netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too big (%d)\n",
 515				   cmd, wrqu->data.length);
 516			return;
 517		}
 518		if (wrqu->data.length < descr->min_tokens) {
 519			netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too small (%d)\n",
 520				   cmd, wrqu->data.length);
 521			return;
 522		}
 523		/* Calculate extra_len - extra is NULL for restricted events */
 524		if (extra != NULL)
 525			extra_len = wrqu->data.length * descr->token_size;
 526		/* Always at an offset in wrqu */
 527		wrqu_off = IW_EV_POINT_OFF;
 528	}
 529
 530	/* Total length of the event */
 531	hdr_len = event_type_size[descr->header_type];
 532	event_len = hdr_len + extra_len;
 533
 534	/*
 535	 * The problem for 64/32 bit.
 536	 *
 537	 * On 64-bit, a regular event is laid out as follows:
 538	 *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
 539	 *      | event.len | event.cmd |     p a d d i n g     |
 540	 *      | wrqu data ... (with the correct size)         |
 541	 *
 542	 * This padding exists because we manipulate event->u,
 543	 * and 'event' is not packed.
 544	 *
 545	 * An iw_point event is laid out like this instead:
 546	 *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
 547	 *      | event.len | event.cmd |     p a d d i n g     |
 548	 *      | iwpnt.len | iwpnt.flg |     p a d d i n g     |
 549	 *      | extra data  ...
 550	 *
 551	 * The second padding exists because struct iw_point is extended,
 552	 * but this depends on the platform...
 553	 *
 554	 * On 32-bit, all the padding shouldn't be there.
 555	 */
 556
 557	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 558	if (!skb)
 559		return;
 560
 561	/* Send via the RtNetlink event channel */
 562	nlh = rtnetlink_ifinfo_prep(dev, skb);
 563	if (WARN_ON(!nlh)) {
 564		kfree_skb(skb);
 565		return;
 566	}
 567
 568	/* Add the wireless events in the netlink packet */
 569	nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
 570	if (!nla) {
 571		kfree_skb(skb);
 572		return;
 573	}
 574	event = nla_data(nla);
 575
 576	/* Fill event - first clear to avoid data leaking */
 577	memset(event, 0, hdr_len);
 578	event->len = event_len;
 579	event->cmd = cmd;
 580	memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
 581	if (extra_len)
 582		memcpy(((char *) event) + hdr_len, extra, extra_len);
 583
 584	nlmsg_end(skb, nlh);
 585#ifdef CONFIG_COMPAT
 586	hdr_len = compat_event_type_size[descr->header_type];
 587
 588	/* ptr_len is remaining size in event header apart from LCP */
 589	ptr_len = hdr_len - IW_EV_COMPAT_LCP_LEN;
 590	event_len = hdr_len + extra_len;
 591
 592	compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 593	if (!compskb) {
 594		kfree_skb(skb);
 595		return;
 596	}
 597
 598	/* Send via the RtNetlink event channel */
 599	nlh = rtnetlink_ifinfo_prep(dev, compskb);
 600	if (WARN_ON(!nlh)) {
 601		kfree_skb(skb);
 602		kfree_skb(compskb);
 603		return;
 604	}
 605
 606	/* Add the wireless events in the netlink packet */
 607	nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
 608	if (!nla) {
 609		kfree_skb(skb);
 610		kfree_skb(compskb);
 611		return;
 612	}
 613	compat_event = nla_data(nla);
 614
 615	compat_event->len = event_len;
 616	compat_event->cmd = cmd;
 617	if (descr->header_type == IW_HEADER_TYPE_POINT) {
 618		compat_wrqu.length = wrqu->data.length;
 619		compat_wrqu.flags = wrqu->data.flags;
 620		memcpy(compat_event->ptr_bytes,
 621		       ((char *)&compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
 622			ptr_len);
 623		if (extra_len)
 624			memcpy(&compat_event->ptr_bytes[ptr_len],
 625			       extra, extra_len);
 626	} else {
 627		/* extra_len must be zero, so no if (extra) needed */
 628		memcpy(compat_event->ptr_bytes, wrqu, ptr_len);
 
 629	}
 630
 631	nlmsg_end(compskb, nlh);
 632
 633	skb_shinfo(skb)->frag_list = compskb;
 634#endif
 635	skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
 636	schedule_work(&wireless_nlevent_work);
 637}
 638EXPORT_SYMBOL(wireless_send_event);
 639
 640#ifdef CONFIG_CFG80211_WEXT
 641static void wireless_warn_cfg80211_wext(void)
 642{
 643	char name[sizeof(current->comm)];
 644
 645	pr_warn_once("warning: `%s' uses wireless extensions which will stop working for Wi-Fi 7 hardware; use nl80211\n",
 646		     get_task_comm(name, current));
 647}
 648#endif
 649
 650/* IW handlers */
 651
 652struct iw_statistics *get_wireless_stats(struct net_device *dev)
 653{
 654#ifdef CONFIG_WIRELESS_EXT
 655	if ((dev->wireless_handlers != NULL) &&
 656	   (dev->wireless_handlers->get_wireless_stats != NULL))
 657		return dev->wireless_handlers->get_wireless_stats(dev);
 658#endif
 659
 660#ifdef CONFIG_CFG80211_WEXT
 661	if (dev->ieee80211_ptr &&
 662	    dev->ieee80211_ptr->wiphy &&
 663	    dev->ieee80211_ptr->wiphy->wext &&
 664	    dev->ieee80211_ptr->wiphy->wext->get_wireless_stats) {
 665		wireless_warn_cfg80211_wext();
 666		if (dev->ieee80211_ptr->wiphy->flags & (WIPHY_FLAG_SUPPORTS_MLO |
 667							WIPHY_FLAG_DISABLE_WEXT))
 668			return NULL;
 669		return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
 670	}
 671#endif
 672
 673	/* not found */
 674	return NULL;
 675}
 676
 677/* noinline to avoid a bogus warning with -O3 */
 678static noinline int iw_handler_get_iwstats(struct net_device *	dev,
 679				  struct iw_request_info *	info,
 680				  union iwreq_data *		wrqu,
 681				  char *			extra)
 682{
 683	/* Get stats from the driver */
 684	struct iw_statistics *stats;
 685
 686	stats = get_wireless_stats(dev);
 687	if (stats) {
 688		/* Copy statistics to extra */
 689		memcpy(extra, stats, sizeof(struct iw_statistics));
 690		wrqu->data.length = sizeof(struct iw_statistics);
 691
 692		/* Check if we need to clear the updated flag */
 693		if (wrqu->data.flags != 0)
 694			stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
 695		return 0;
 696	} else
 697		return -EOPNOTSUPP;
 698}
 699
 700static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
 701{
 702	/* Don't "optimise" the following variable, it will crash */
 703	unsigned int	index;		/* *MUST* be unsigned */
 704	const struct iw_handler_def *handlers = NULL;
 705
 706#ifdef CONFIG_CFG80211_WEXT
 707	if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy) {
 708		wireless_warn_cfg80211_wext();
 709		if (dev->ieee80211_ptr->wiphy->flags & (WIPHY_FLAG_SUPPORTS_MLO |
 710							WIPHY_FLAG_DISABLE_WEXT))
 711			return NULL;
 712		handlers = dev->ieee80211_ptr->wiphy->wext;
 713	}
 714#endif
 715#ifdef CONFIG_WIRELESS_EXT
 716	if (dev->wireless_handlers)
 717		handlers = dev->wireless_handlers;
 718#endif
 719
 720	if (!handlers)
 721		return NULL;
 722
 723	/* Try as a standard command */
 724	index = IW_IOCTL_IDX(cmd);
 725	if (index < handlers->num_standard)
 726		return handlers->standard[index];
 727
 728#ifdef CONFIG_WEXT_PRIV
 729	/* Try as a private command */
 730	index = cmd - SIOCIWFIRSTPRIV;
 731	if (index < handlers->num_private)
 732		return handlers->private[index];
 733#endif
 734
 735	/* Not found */
 736	return NULL;
 737}
 738
 739static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
 740				   const struct iw_ioctl_description *descr,
 741				   iw_handler handler, struct net_device *dev,
 742				   struct iw_request_info *info)
 743{
 744	int err, extra_size, user_length = 0, essid_compat = 0;
 745	char *extra;
 746
 747	/* Calculate space needed by arguments. Always allocate
 748	 * for max space.
 749	 */
 750	extra_size = descr->max_tokens * descr->token_size;
 751
 752	/* Check need for ESSID compatibility for WE < 21 */
 753	switch (cmd) {
 754	case SIOCSIWESSID:
 755	case SIOCGIWESSID:
 756	case SIOCSIWNICKN:
 757	case SIOCGIWNICKN:
 758		if (iwp->length == descr->max_tokens + 1)
 759			essid_compat = 1;
 760		else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
 761			char essid[IW_ESSID_MAX_SIZE + 1];
 762			unsigned int len;
 763			len = iwp->length * descr->token_size;
 764
 765			if (len > IW_ESSID_MAX_SIZE)
 766				return -EFAULT;
 767
 768			err = copy_from_user(essid, iwp->pointer, len);
 769			if (err)
 770				return -EFAULT;
 771
 772			if (essid[iwp->length - 1] == '\0')
 773				essid_compat = 1;
 774		}
 775		break;
 776	default:
 777		break;
 778	}
 779
 780	iwp->length -= essid_compat;
 781
 782	/* Check what user space is giving us */
 783	if (IW_IS_SET(cmd)) {
 784		/* Check NULL pointer */
 785		if (!iwp->pointer && iwp->length != 0)
 786			return -EFAULT;
 787		/* Check if number of token fits within bounds */
 788		if (iwp->length > descr->max_tokens)
 789			return -E2BIG;
 790		if (iwp->length < descr->min_tokens)
 791			return -EINVAL;
 792	} else {
 793		/* Check NULL pointer */
 794		if (!iwp->pointer)
 795			return -EFAULT;
 796		/* Save user space buffer size for checking */
 797		user_length = iwp->length;
 798
 799		/* Don't check if user_length > max to allow forward
 800		 * compatibility. The test user_length < min is
 801		 * implied by the test at the end.
 802		 */
 803
 804		/* Support for very large requests */
 805		if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
 806		    (user_length > descr->max_tokens)) {
 807			/* Allow userspace to GET more than max so
 808			 * we can support any size GET requests.
 809			 * There is still a limit : -ENOMEM.
 810			 */
 811			extra_size = user_length * descr->token_size;
 812
 813			/* Note : user_length is originally a __u16,
 814			 * and token_size is controlled by us,
 815			 * so extra_size won't get negative and
 816			 * won't overflow...
 817			 */
 818		}
 819	}
 820
 821	/* Sanity-check to ensure we never end up _allocating_ zero
 822	 * bytes of data for extra.
 823	 */
 824	if (extra_size <= 0)
 825		return -EFAULT;
 826
 827	/* kzalloc() ensures NULL-termination for essid_compat. */
 828	extra = kzalloc(extra_size, GFP_KERNEL);
 829	if (!extra)
 830		return -ENOMEM;
 831
 832	/* If it is a SET, get all the extra data in here */
 833	if (IW_IS_SET(cmd) && (iwp->length != 0)) {
 834		if (copy_from_user(extra, iwp->pointer,
 835				   iwp->length *
 836				   descr->token_size)) {
 837			err = -EFAULT;
 838			goto out;
 839		}
 840
 841		if (cmd == SIOCSIWENCODEEXT) {
 842			struct iw_encode_ext *ee = (void *) extra;
 843
 844			if (iwp->length < sizeof(*ee) + ee->key_len) {
 845				err = -EFAULT;
 846				goto out;
 847			}
 848		}
 849	}
 850
 851	if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
 852		/*
 853		 * If this is a GET, but not NOMAX, it means that the extra
 854		 * data is not bounded by userspace, but by max_tokens. Thus
 855		 * set the length to max_tokens. This matches the extra data
 856		 * allocation.
 857		 * The driver should fill it with the number of tokens it
 858		 * provided, and it may check iwp->length rather than having
 859		 * knowledge of max_tokens. If the driver doesn't change the
 860		 * iwp->length, this ioctl just copies back max_token tokens
 861		 * filled with zeroes. Hopefully the driver isn't claiming
 862		 * them to be valid data.
 863		 */
 864		iwp->length = descr->max_tokens;
 865	}
 866
 867	err = handler(dev, info, (union iwreq_data *) iwp, extra);
 868
 869	iwp->length += essid_compat;
 870
 871	/* If we have something to return to the user */
 872	if (!err && IW_IS_GET(cmd)) {
 873		/* Check if there is enough buffer up there */
 874		if (user_length < iwp->length) {
 875			err = -E2BIG;
 876			goto out;
 877		}
 878
 879		if (copy_to_user(iwp->pointer, extra,
 880				 iwp->length *
 881				 descr->token_size)) {
 882			err = -EFAULT;
 883			goto out;
 884		}
 885	}
 886
 887	/* Generate an event to notify listeners of the change */
 888	if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
 889	    ((err == 0) || (err == -EIWCOMMIT))) {
 890		union iwreq_data *data = (union iwreq_data *) iwp;
 891
 892		if (descr->flags & IW_DESCR_FLAG_RESTRICT)
 893			/* If the event is restricted, don't
 894			 * export the payload.
 895			 */
 896			wireless_send_event(dev, cmd, data, NULL);
 897		else
 898			wireless_send_event(dev, cmd, data, extra);
 899	}
 900
 901out:
 902	kfree(extra);
 903	return err;
 904}
 905
 906/*
 907 * Call the commit handler in the driver
 908 * (if exist and if conditions are right)
 909 *
 910 * Note : our current commit strategy is currently pretty dumb,
 911 * but we will be able to improve on that...
 912 * The goal is to try to agreagate as many changes as possible
 913 * before doing the commit. Drivers that will define a commit handler
 914 * are usually those that need a reset after changing parameters, so
 915 * we want to minimise the number of reset.
 916 * A cool idea is to use a timer : at each "set" command, we re-set the
 917 * timer, when the timer eventually fires, we call the driver.
 918 * Hopefully, more on that later.
 919 *
 920 * Also, I'm waiting to see how many people will complain about the
 921 * netif_running(dev) test. I'm open on that one...
 922 * Hopefully, the driver will remember to do a commit in "open()" ;-)
 923 */
 924int call_commit_handler(struct net_device *dev)
 925{
 926#ifdef CONFIG_WIRELESS_EXT
 927	if (netif_running(dev) &&
 928	    dev->wireless_handlers &&
 929	    dev->wireless_handlers->standard[0])
 930		/* Call the commit handler on the driver */
 931		return dev->wireless_handlers->standard[0](dev, NULL,
 932							   NULL, NULL);
 933	else
 934		return 0;		/* Command completed successfully */
 935#else
 936	/* cfg80211 has no commit */
 937	return 0;
 938#endif
 939}
 940
 941/*
 942 * Main IOCTl dispatcher.
 943 * Check the type of IOCTL and call the appropriate wrapper...
 944 */
 945static int wireless_process_ioctl(struct net *net, struct iwreq *iwr,
 946				  unsigned int cmd,
 947				  struct iw_request_info *info,
 948				  wext_ioctl_func standard,
 949				  wext_ioctl_func private)
 950{
 
 951	struct net_device *dev;
 952	iw_handler	handler;
 953
 954	/* Permissions are already checked in dev_ioctl() before calling us.
 955	 * The copy_to/from_user() of ifr is also dealt with in there */
 956
 957	/* Make sure the device exist */
 958	if ((dev = __dev_get_by_name(net, iwr->ifr_name)) == NULL)
 959		return -ENODEV;
 960
 961	/* A bunch of special cases, then the generic case...
 962	 * Note that 'cmd' is already filtered in dev_ioctl() with
 963	 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
 964	if (cmd == SIOCGIWSTATS)
 965		return standard(dev, iwr, cmd, info,
 966				&iw_handler_get_iwstats);
 967
 968#ifdef CONFIG_WEXT_PRIV
 969	if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
 970		return standard(dev, iwr, cmd, info,
 971				iw_handler_get_private);
 972#endif
 973
 974	/* Basic check */
 975	if (!netif_device_present(dev))
 976		return -ENODEV;
 977
 978	/* New driver API : try to find the handler */
 979	handler = get_handler(dev, cmd);
 980	if (handler) {
 981		/* Standard and private are not the same */
 982		if (cmd < SIOCIWFIRSTPRIV)
 983			return standard(dev, iwr, cmd, info, handler);
 984		else if (private)
 985			return private(dev, iwr, cmd, info, handler);
 986	}
 
 
 
 987	return -EOPNOTSUPP;
 988}
 989
 990/* If command is `set a parameter', or `get the encoding parameters',
 991 * check if the user has the right to do it.
 992 */
 993static int wext_permission_check(unsigned int cmd)
 994{
 995	if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
 996	     cmd == SIOCGIWENCODEEXT) &&
 997	    !capable(CAP_NET_ADMIN))
 998		return -EPERM;
 999
1000	return 0;
1001}
1002
1003/* entry point from dev ioctl */
1004static int wext_ioctl_dispatch(struct net *net, struct iwreq *iwr,
1005			       unsigned int cmd, struct iw_request_info *info,
1006			       wext_ioctl_func standard,
1007			       wext_ioctl_func private)
1008{
1009	int ret = wext_permission_check(cmd);
1010
1011	if (ret)
1012		return ret;
1013
1014	dev_load(net, iwr->ifr_name);
1015	rtnl_lock();
1016	ret = wireless_process_ioctl(net, iwr, cmd, info, standard, private);
1017	rtnl_unlock();
1018
1019	return ret;
1020}
1021
1022/*
1023 * Wrapper to call a standard Wireless Extension handler.
1024 * We do various checks and also take care of moving data between
1025 * user space and kernel space.
1026 */
1027static int ioctl_standard_call(struct net_device *	dev,
1028			       struct iwreq		*iwr,
1029			       unsigned int		cmd,
1030			       struct iw_request_info	*info,
1031			       iw_handler		handler)
1032{
1033	const struct iw_ioctl_description *	descr;
1034	int					ret = -EINVAL;
1035
1036	/* Get the description of the IOCTL */
1037	if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
1038		return -EOPNOTSUPP;
1039	descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
1040
1041	/* Check if we have a pointer to user space data or not */
1042	if (descr->header_type != IW_HEADER_TYPE_POINT) {
1043
1044		/* No extra arguments. Trivial to handle */
1045		ret = handler(dev, info, &(iwr->u), NULL);
1046
1047		/* Generate an event to notify listeners of the change */
1048		if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
1049		   ((ret == 0) || (ret == -EIWCOMMIT)))
1050			wireless_send_event(dev, cmd, &(iwr->u), NULL);
1051	} else {
1052		ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
1053					      handler, dev, info);
1054	}
1055
1056	/* Call commit handler if needed and defined */
1057	if (ret == -EIWCOMMIT)
1058		ret = call_commit_handler(dev);
1059
1060	/* Here, we will generate the appropriate event if needed */
1061
1062	return ret;
1063}
1064
1065
1066int wext_handle_ioctl(struct net *net, unsigned int cmd, void __user *arg)
 
1067{
1068	struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1069	struct iwreq iwr;
1070	int ret;
1071
1072	if (copy_from_user(&iwr, arg, sizeof(iwr)))
1073		return -EFAULT;
1074
1075	iwr.ifr_name[sizeof(iwr.ifr_name) - 1] = 0;
1076
1077	ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
1078				  ioctl_standard_call,
1079				  ioctl_private_call);
1080	if (ret >= 0 &&
1081	    IW_IS_GET(cmd) &&
1082	    copy_to_user(arg, &iwr, sizeof(struct iwreq)))
1083		return -EFAULT;
1084
1085	return ret;
1086}
1087
1088#ifdef CONFIG_COMPAT
1089static int compat_standard_call(struct net_device	*dev,
1090				struct iwreq		*iwr,
1091				unsigned int		cmd,
1092				struct iw_request_info	*info,
1093				iw_handler		handler)
1094{
1095	const struct iw_ioctl_description *descr;
1096	struct compat_iw_point *iwp_compat;
1097	struct iw_point iwp;
1098	int err;
1099
1100	descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1101
1102	if (descr->header_type != IW_HEADER_TYPE_POINT)
1103		return ioctl_standard_call(dev, iwr, cmd, info, handler);
1104
1105	iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1106	iwp.pointer = compat_ptr(iwp_compat->pointer);
1107	iwp.length = iwp_compat->length;
1108	iwp.flags = iwp_compat->flags;
1109
1110	err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1111
1112	iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1113	iwp_compat->length = iwp.length;
1114	iwp_compat->flags = iwp.flags;
1115
1116	return err;
1117}
1118
1119int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1120			     unsigned long arg)
1121{
1122	void __user *argp = (void __user *)arg;
1123	struct iw_request_info info;
1124	struct iwreq iwr;
1125	char *colon;
1126	int ret;
1127
1128	if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1129		return -EFAULT;
1130
1131	iwr.ifr_name[IFNAMSIZ-1] = 0;
1132	colon = strchr(iwr.ifr_name, ':');
1133	if (colon)
1134		*colon = 0;
1135
1136	info.cmd = cmd;
1137	info.flags = IW_REQUEST_FLAG_COMPAT;
1138
1139	ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
1140				  compat_standard_call,
1141				  compat_private_call);
1142
1143	if (ret >= 0 &&
1144	    IW_IS_GET(cmd) &&
1145	    copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1146		return -EFAULT;
1147
1148	return ret;
1149}
1150#endif
1151
1152char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
1153			   char *ends, struct iw_event *iwe, int event_len)
1154{
1155	int lcp_len = iwe_stream_lcp_len(info);
1156
1157	event_len = iwe_stream_event_len_adjust(info, event_len);
1158
1159	/* Check if it's possible */
1160	if (likely((stream + event_len) < ends)) {
1161		iwe->len = event_len;
1162		/* Beware of alignment issues on 64 bits */
1163		memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1164		memcpy(stream + lcp_len, &iwe->u,
1165		       event_len - lcp_len);
1166		stream += event_len;
1167	}
1168
1169	return stream;
1170}
1171EXPORT_SYMBOL(iwe_stream_add_event);
1172
1173char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
1174			   char *ends, struct iw_event *iwe, char *extra)
1175{
1176	int event_len = iwe_stream_point_len(info) + iwe->u.data.length;
1177	int point_len = iwe_stream_point_len(info);
1178	int lcp_len   = iwe_stream_lcp_len(info);
1179
1180	/* Check if it's possible */
1181	if (likely((stream + event_len) < ends)) {
1182		iwe->len = event_len;
1183		memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1184		memcpy(stream + lcp_len,
1185		       ((char *) &iwe->u) + IW_EV_POINT_OFF,
1186		       IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1187		if (iwe->u.data.length && extra)
1188			memcpy(stream + point_len, extra, iwe->u.data.length);
1189		stream += event_len;
1190	}
1191
1192	return stream;
1193}
1194EXPORT_SYMBOL(iwe_stream_add_point);
1195
1196char *iwe_stream_add_value(struct iw_request_info *info, char *event,
1197			   char *value, char *ends, struct iw_event *iwe,
1198			   int event_len)
1199{
1200	int lcp_len = iwe_stream_lcp_len(info);
1201
1202	/* Don't duplicate LCP */
1203	event_len -= IW_EV_LCP_LEN;
1204
1205	/* Check if it's possible */
1206	if (likely((value + event_len) < ends)) {
1207		/* Add new value */
1208		memcpy(value, &iwe->u, event_len);
1209		value += event_len;
1210		/* Patch LCP */
1211		iwe->len = value - event;
1212		memcpy(event, (char *) iwe, lcp_len);
1213	}
1214
1215	return value;
1216}
1217EXPORT_SYMBOL(iwe_stream_add_value);
v3.1
   1/*
   2 * This file implement the Wireless Extensions core API.
   3 *
   4 * Authors :	Jean Tourrilhes - HPL - <jt@hpl.hp.com>
   5 * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
   6 * Copyright	2009 Johannes Berg <johannes@sipsolutions.net>
 
   7 *
   8 * (As all part of the Linux kernel, this file is GPL)
   9 */
  10#include <linux/kernel.h>
  11#include <linux/netdevice.h>
  12#include <linux/rtnetlink.h>
  13#include <linux/slab.h>
  14#include <linux/wireless.h>
  15#include <linux/uaccess.h>
 
  16#include <net/cfg80211.h>
  17#include <net/iw_handler.h>
  18#include <net/netlink.h>
  19#include <net/wext.h>
  20#include <net/net_namespace.h>
  21
  22typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  23			       unsigned int, struct iw_request_info *,
  24			       iw_handler);
  25
  26
  27/*
  28 * Meta-data about all the standard Wireless Extension request we
  29 * know about.
  30 */
  31static const struct iw_ioctl_description standard_ioctl[] = {
  32	[IW_IOCTL_IDX(SIOCSIWCOMMIT)] = {
  33		.header_type	= IW_HEADER_TYPE_NULL,
  34	},
  35	[IW_IOCTL_IDX(SIOCGIWNAME)] = {
  36		.header_type	= IW_HEADER_TYPE_CHAR,
  37		.flags		= IW_DESCR_FLAG_DUMP,
  38	},
  39	[IW_IOCTL_IDX(SIOCSIWNWID)] = {
  40		.header_type	= IW_HEADER_TYPE_PARAM,
  41		.flags		= IW_DESCR_FLAG_EVENT,
  42	},
  43	[IW_IOCTL_IDX(SIOCGIWNWID)] = {
  44		.header_type	= IW_HEADER_TYPE_PARAM,
  45		.flags		= IW_DESCR_FLAG_DUMP,
  46	},
  47	[IW_IOCTL_IDX(SIOCSIWFREQ)] = {
  48		.header_type	= IW_HEADER_TYPE_FREQ,
  49		.flags		= IW_DESCR_FLAG_EVENT,
  50	},
  51	[IW_IOCTL_IDX(SIOCGIWFREQ)] = {
  52		.header_type	= IW_HEADER_TYPE_FREQ,
  53		.flags		= IW_DESCR_FLAG_DUMP,
  54	},
  55	[IW_IOCTL_IDX(SIOCSIWMODE)] = {
  56		.header_type	= IW_HEADER_TYPE_UINT,
  57		.flags		= IW_DESCR_FLAG_EVENT,
  58	},
  59	[IW_IOCTL_IDX(SIOCGIWMODE)] = {
  60		.header_type	= IW_HEADER_TYPE_UINT,
  61		.flags		= IW_DESCR_FLAG_DUMP,
  62	},
  63	[IW_IOCTL_IDX(SIOCSIWSENS)] = {
  64		.header_type	= IW_HEADER_TYPE_PARAM,
  65	},
  66	[IW_IOCTL_IDX(SIOCGIWSENS)] = {
  67		.header_type	= IW_HEADER_TYPE_PARAM,
  68	},
  69	[IW_IOCTL_IDX(SIOCSIWRANGE)] = {
  70		.header_type	= IW_HEADER_TYPE_NULL,
  71	},
  72	[IW_IOCTL_IDX(SIOCGIWRANGE)] = {
  73		.header_type	= IW_HEADER_TYPE_POINT,
  74		.token_size	= 1,
  75		.max_tokens	= sizeof(struct iw_range),
  76		.flags		= IW_DESCR_FLAG_DUMP,
  77	},
  78	[IW_IOCTL_IDX(SIOCSIWPRIV)] = {
  79		.header_type	= IW_HEADER_TYPE_NULL,
  80	},
  81	[IW_IOCTL_IDX(SIOCGIWPRIV)] = { /* (handled directly by us) */
  82		.header_type	= IW_HEADER_TYPE_POINT,
  83		.token_size	= sizeof(struct iw_priv_args),
  84		.max_tokens	= 16,
  85		.flags		= IW_DESCR_FLAG_NOMAX,
  86	},
  87	[IW_IOCTL_IDX(SIOCSIWSTATS)] = {
  88		.header_type	= IW_HEADER_TYPE_NULL,
  89	},
  90	[IW_IOCTL_IDX(SIOCGIWSTATS)] = { /* (handled directly by us) */
  91		.header_type	= IW_HEADER_TYPE_POINT,
  92		.token_size	= 1,
  93		.max_tokens	= sizeof(struct iw_statistics),
  94		.flags		= IW_DESCR_FLAG_DUMP,
  95	},
  96	[IW_IOCTL_IDX(SIOCSIWSPY)] = {
  97		.header_type	= IW_HEADER_TYPE_POINT,
  98		.token_size	= sizeof(struct sockaddr),
  99		.max_tokens	= IW_MAX_SPY,
 100	},
 101	[IW_IOCTL_IDX(SIOCGIWSPY)] = {
 102		.header_type	= IW_HEADER_TYPE_POINT,
 103		.token_size	= sizeof(struct sockaddr) +
 104				  sizeof(struct iw_quality),
 105		.max_tokens	= IW_MAX_SPY,
 106	},
 107	[IW_IOCTL_IDX(SIOCSIWTHRSPY)] = {
 108		.header_type	= IW_HEADER_TYPE_POINT,
 109		.token_size	= sizeof(struct iw_thrspy),
 110		.min_tokens	= 1,
 111		.max_tokens	= 1,
 112	},
 113	[IW_IOCTL_IDX(SIOCGIWTHRSPY)] = {
 114		.header_type	= IW_HEADER_TYPE_POINT,
 115		.token_size	= sizeof(struct iw_thrspy),
 116		.min_tokens	= 1,
 117		.max_tokens	= 1,
 118	},
 119	[IW_IOCTL_IDX(SIOCSIWAP)] = {
 120		.header_type	= IW_HEADER_TYPE_ADDR,
 121	},
 122	[IW_IOCTL_IDX(SIOCGIWAP)] = {
 123		.header_type	= IW_HEADER_TYPE_ADDR,
 124		.flags		= IW_DESCR_FLAG_DUMP,
 125	},
 126	[IW_IOCTL_IDX(SIOCSIWMLME)] = {
 127		.header_type	= IW_HEADER_TYPE_POINT,
 128		.token_size	= 1,
 129		.min_tokens	= sizeof(struct iw_mlme),
 130		.max_tokens	= sizeof(struct iw_mlme),
 131	},
 132	[IW_IOCTL_IDX(SIOCGIWAPLIST)] = {
 133		.header_type	= IW_HEADER_TYPE_POINT,
 134		.token_size	= sizeof(struct sockaddr) +
 135				  sizeof(struct iw_quality),
 136		.max_tokens	= IW_MAX_AP,
 137		.flags		= IW_DESCR_FLAG_NOMAX,
 138	},
 139	[IW_IOCTL_IDX(SIOCSIWSCAN)] = {
 140		.header_type	= IW_HEADER_TYPE_POINT,
 141		.token_size	= 1,
 142		.min_tokens	= 0,
 143		.max_tokens	= sizeof(struct iw_scan_req),
 144	},
 145	[IW_IOCTL_IDX(SIOCGIWSCAN)] = {
 146		.header_type	= IW_HEADER_TYPE_POINT,
 147		.token_size	= 1,
 148		.max_tokens	= IW_SCAN_MAX_DATA,
 149		.flags		= IW_DESCR_FLAG_NOMAX,
 150	},
 151	[IW_IOCTL_IDX(SIOCSIWESSID)] = {
 152		.header_type	= IW_HEADER_TYPE_POINT,
 153		.token_size	= 1,
 154		.max_tokens	= IW_ESSID_MAX_SIZE,
 155		.flags		= IW_DESCR_FLAG_EVENT,
 156	},
 157	[IW_IOCTL_IDX(SIOCGIWESSID)] = {
 158		.header_type	= IW_HEADER_TYPE_POINT,
 159		.token_size	= 1,
 160		.max_tokens	= IW_ESSID_MAX_SIZE,
 161		.flags		= IW_DESCR_FLAG_DUMP,
 162	},
 163	[IW_IOCTL_IDX(SIOCSIWNICKN)] = {
 164		.header_type	= IW_HEADER_TYPE_POINT,
 165		.token_size	= 1,
 166		.max_tokens	= IW_ESSID_MAX_SIZE,
 167	},
 168	[IW_IOCTL_IDX(SIOCGIWNICKN)] = {
 169		.header_type	= IW_HEADER_TYPE_POINT,
 170		.token_size	= 1,
 171		.max_tokens	= IW_ESSID_MAX_SIZE,
 172	},
 173	[IW_IOCTL_IDX(SIOCSIWRATE)] = {
 174		.header_type	= IW_HEADER_TYPE_PARAM,
 175	},
 176	[IW_IOCTL_IDX(SIOCGIWRATE)] = {
 177		.header_type	= IW_HEADER_TYPE_PARAM,
 178	},
 179	[IW_IOCTL_IDX(SIOCSIWRTS)] = {
 180		.header_type	= IW_HEADER_TYPE_PARAM,
 181	},
 182	[IW_IOCTL_IDX(SIOCGIWRTS)] = {
 183		.header_type	= IW_HEADER_TYPE_PARAM,
 184	},
 185	[IW_IOCTL_IDX(SIOCSIWFRAG)] = {
 186		.header_type	= IW_HEADER_TYPE_PARAM,
 187	},
 188	[IW_IOCTL_IDX(SIOCGIWFRAG)] = {
 189		.header_type	= IW_HEADER_TYPE_PARAM,
 190	},
 191	[IW_IOCTL_IDX(SIOCSIWTXPOW)] = {
 192		.header_type	= IW_HEADER_TYPE_PARAM,
 193	},
 194	[IW_IOCTL_IDX(SIOCGIWTXPOW)] = {
 195		.header_type	= IW_HEADER_TYPE_PARAM,
 196	},
 197	[IW_IOCTL_IDX(SIOCSIWRETRY)] = {
 198		.header_type	= IW_HEADER_TYPE_PARAM,
 199	},
 200	[IW_IOCTL_IDX(SIOCGIWRETRY)] = {
 201		.header_type	= IW_HEADER_TYPE_PARAM,
 202	},
 203	[IW_IOCTL_IDX(SIOCSIWENCODE)] = {
 204		.header_type	= IW_HEADER_TYPE_POINT,
 205		.token_size	= 1,
 206		.max_tokens	= IW_ENCODING_TOKEN_MAX,
 207		.flags		= IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
 208	},
 209	[IW_IOCTL_IDX(SIOCGIWENCODE)] = {
 210		.header_type	= IW_HEADER_TYPE_POINT,
 211		.token_size	= 1,
 212		.max_tokens	= IW_ENCODING_TOKEN_MAX,
 213		.flags		= IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
 214	},
 215	[IW_IOCTL_IDX(SIOCSIWPOWER)] = {
 216		.header_type	= IW_HEADER_TYPE_PARAM,
 217	},
 218	[IW_IOCTL_IDX(SIOCGIWPOWER)] = {
 219		.header_type	= IW_HEADER_TYPE_PARAM,
 220	},
 221	[IW_IOCTL_IDX(SIOCSIWGENIE)] = {
 222		.header_type	= IW_HEADER_TYPE_POINT,
 223		.token_size	= 1,
 224		.max_tokens	= IW_GENERIC_IE_MAX,
 225	},
 226	[IW_IOCTL_IDX(SIOCGIWGENIE)] = {
 227		.header_type	= IW_HEADER_TYPE_POINT,
 228		.token_size	= 1,
 229		.max_tokens	= IW_GENERIC_IE_MAX,
 230	},
 231	[IW_IOCTL_IDX(SIOCSIWAUTH)] = {
 232		.header_type	= IW_HEADER_TYPE_PARAM,
 233	},
 234	[IW_IOCTL_IDX(SIOCGIWAUTH)] = {
 235		.header_type	= IW_HEADER_TYPE_PARAM,
 236	},
 237	[IW_IOCTL_IDX(SIOCSIWENCODEEXT)] = {
 238		.header_type	= IW_HEADER_TYPE_POINT,
 239		.token_size	= 1,
 240		.min_tokens	= sizeof(struct iw_encode_ext),
 241		.max_tokens	= sizeof(struct iw_encode_ext) +
 242				  IW_ENCODING_TOKEN_MAX,
 243	},
 244	[IW_IOCTL_IDX(SIOCGIWENCODEEXT)] = {
 245		.header_type	= IW_HEADER_TYPE_POINT,
 246		.token_size	= 1,
 247		.min_tokens	= sizeof(struct iw_encode_ext),
 248		.max_tokens	= sizeof(struct iw_encode_ext) +
 249				  IW_ENCODING_TOKEN_MAX,
 250	},
 251	[IW_IOCTL_IDX(SIOCSIWPMKSA)] = {
 252		.header_type	= IW_HEADER_TYPE_POINT,
 253		.token_size	= 1,
 254		.min_tokens	= sizeof(struct iw_pmksa),
 255		.max_tokens	= sizeof(struct iw_pmksa),
 256	},
 257};
 258static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
 259
 260/*
 261 * Meta-data about all the additional standard Wireless Extension events
 262 * we know about.
 263 */
 264static const struct iw_ioctl_description standard_event[] = {
 265	[IW_EVENT_IDX(IWEVTXDROP)] = {
 266		.header_type	= IW_HEADER_TYPE_ADDR,
 267	},
 268	[IW_EVENT_IDX(IWEVQUAL)] = {
 269		.header_type	= IW_HEADER_TYPE_QUAL,
 270	},
 271	[IW_EVENT_IDX(IWEVCUSTOM)] = {
 272		.header_type	= IW_HEADER_TYPE_POINT,
 273		.token_size	= 1,
 274		.max_tokens	= IW_CUSTOM_MAX,
 275	},
 276	[IW_EVENT_IDX(IWEVREGISTERED)] = {
 277		.header_type	= IW_HEADER_TYPE_ADDR,
 278	},
 279	[IW_EVENT_IDX(IWEVEXPIRED)] = {
 280		.header_type	= IW_HEADER_TYPE_ADDR,
 281	},
 282	[IW_EVENT_IDX(IWEVGENIE)] = {
 283		.header_type	= IW_HEADER_TYPE_POINT,
 284		.token_size	= 1,
 285		.max_tokens	= IW_GENERIC_IE_MAX,
 286	},
 287	[IW_EVENT_IDX(IWEVMICHAELMICFAILURE)] = {
 288		.header_type	= IW_HEADER_TYPE_POINT,
 289		.token_size	= 1,
 290		.max_tokens	= sizeof(struct iw_michaelmicfailure),
 291	},
 292	[IW_EVENT_IDX(IWEVASSOCREQIE)] = {
 293		.header_type	= IW_HEADER_TYPE_POINT,
 294		.token_size	= 1,
 295		.max_tokens	= IW_GENERIC_IE_MAX,
 296	},
 297	[IW_EVENT_IDX(IWEVASSOCRESPIE)] = {
 298		.header_type	= IW_HEADER_TYPE_POINT,
 299		.token_size	= 1,
 300		.max_tokens	= IW_GENERIC_IE_MAX,
 301	},
 302	[IW_EVENT_IDX(IWEVPMKIDCAND)] = {
 303		.header_type	= IW_HEADER_TYPE_POINT,
 304		.token_size	= 1,
 305		.max_tokens	= sizeof(struct iw_pmkid_cand),
 306	},
 307};
 308static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
 309
 310/* Size (in bytes) of various events */
 311static const int event_type_size[] = {
 312	IW_EV_LCP_LEN,			/* IW_HEADER_TYPE_NULL */
 313	0,
 314	IW_EV_CHAR_LEN,			/* IW_HEADER_TYPE_CHAR */
 315	0,
 316	IW_EV_UINT_LEN,			/* IW_HEADER_TYPE_UINT */
 317	IW_EV_FREQ_LEN,			/* IW_HEADER_TYPE_FREQ */
 318	IW_EV_ADDR_LEN,			/* IW_HEADER_TYPE_ADDR */
 319	0,
 320	IW_EV_POINT_LEN,		/* Without variable payload */
 321	IW_EV_PARAM_LEN,		/* IW_HEADER_TYPE_PARAM */
 322	IW_EV_QUAL_LEN,			/* IW_HEADER_TYPE_QUAL */
 323};
 324
 325#ifdef CONFIG_COMPAT
 326static const int compat_event_type_size[] = {
 327	IW_EV_COMPAT_LCP_LEN,		/* IW_HEADER_TYPE_NULL */
 328	0,
 329	IW_EV_COMPAT_CHAR_LEN,		/* IW_HEADER_TYPE_CHAR */
 330	0,
 331	IW_EV_COMPAT_UINT_LEN,		/* IW_HEADER_TYPE_UINT */
 332	IW_EV_COMPAT_FREQ_LEN,		/* IW_HEADER_TYPE_FREQ */
 333	IW_EV_COMPAT_ADDR_LEN,		/* IW_HEADER_TYPE_ADDR */
 334	0,
 335	IW_EV_COMPAT_POINT_LEN,		/* Without variable payload */
 336	IW_EV_COMPAT_PARAM_LEN,		/* IW_HEADER_TYPE_PARAM */
 337	IW_EV_COMPAT_QUAL_LEN,		/* IW_HEADER_TYPE_QUAL */
 338};
 339#endif
 340
 341
 342/* IW event code */
 343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 344static int __net_init wext_pernet_init(struct net *net)
 345{
 346	skb_queue_head_init(&net->wext_nlevents);
 347	return 0;
 348}
 349
 350static void __net_exit wext_pernet_exit(struct net *net)
 351{
 352	skb_queue_purge(&net->wext_nlevents);
 353}
 354
 355static struct pernet_operations wext_pernet_ops = {
 356	.init = wext_pernet_init,
 357	.exit = wext_pernet_exit,
 358};
 359
 360static int __init wireless_nlevent_init(void)
 361{
 362	return register_pernet_subsys(&wext_pernet_ops);
 
 
 
 
 
 
 
 
 363}
 364
 365subsys_initcall(wireless_nlevent_init);
 366
 367/* Process events generated by the wireless layer or the driver. */
 368static void wireless_nlevent_process(struct work_struct *work)
 369{
 370	struct sk_buff *skb;
 371	struct net *net;
 372
 373	rtnl_lock();
 374
 375	for_each_net(net) {
 376		while ((skb = skb_dequeue(&net->wext_nlevents)))
 377			rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
 378				    GFP_KERNEL);
 379	}
 380
 381	rtnl_unlock();
 382}
 383
 384static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
 385
 386static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
 387					      struct sk_buff *skb)
 388{
 389	struct ifinfomsg *r;
 390	struct nlmsghdr  *nlh;
 391
 392	nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
 393	if (!nlh)
 394		return NULL;
 395
 396	r = nlmsg_data(nlh);
 397	r->ifi_family = AF_UNSPEC;
 398	r->__ifi_pad = 0;
 399	r->ifi_type = dev->type;
 400	r->ifi_index = dev->ifindex;
 401	r->ifi_flags = dev_get_flags(dev);
 402	r->ifi_change = 0;	/* Wireless changes don't affect those flags */
 403
 404	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
 
 405
 406	return nlh;
 407 nla_put_failure:
 408	nlmsg_cancel(skb, nlh);
 409	return NULL;
 410}
 411
 412
 413/*
 414 * Main event dispatcher. Called from other parts and drivers.
 415 * Send the event on the appropriate channels.
 416 * May be called from interrupt context.
 417 */
 418void wireless_send_event(struct net_device *	dev,
 419			 unsigned int		cmd,
 420			 union iwreq_data *	wrqu,
 421			 const char *		extra)
 422{
 423	const struct iw_ioctl_description *	descr = NULL;
 424	int extra_len = 0;
 425	struct iw_event  *event;		/* Mallocated whole event */
 426	int event_len;				/* Its size */
 427	int hdr_len;				/* Size of the event header */
 428	int wrqu_off = 0;			/* Offset in wrqu */
 429	/* Don't "optimise" the following variable, it will crash */
 430	unsigned	cmd_index;		/* *MUST* be unsigned */
 431	struct sk_buff *skb;
 432	struct nlmsghdr *nlh;
 433	struct nlattr *nla;
 434#ifdef CONFIG_COMPAT
 435	struct __compat_iw_event *compat_event;
 436	struct compat_iw_point compat_wrqu;
 437	struct sk_buff *compskb;
 
 438#endif
 439
 440	/*
 441	 * Nothing in the kernel sends scan events with data, be safe.
 442	 * This is necessary because we cannot fix up scan event data
 443	 * for compat, due to being contained in 'extra', but normally
 444	 * applications are required to retrieve the scan data anyway
 445	 * and no data is included in the event, this codifies that
 446	 * practice.
 447	 */
 448	if (WARN_ON(cmd == SIOCGIWSCAN && extra))
 449		extra = NULL;
 450
 451	/* Get the description of the Event */
 452	if (cmd <= SIOCIWLAST) {
 453		cmd_index = IW_IOCTL_IDX(cmd);
 454		if (cmd_index < standard_ioctl_num)
 455			descr = &(standard_ioctl[cmd_index]);
 456	} else {
 457		cmd_index = IW_EVENT_IDX(cmd);
 458		if (cmd_index < standard_event_num)
 459			descr = &(standard_event[cmd_index]);
 460	}
 461	/* Don't accept unknown events */
 462	if (descr == NULL) {
 463		/* Note : we don't return an error to the driver, because
 464		 * the driver would not know what to do about it. It can't
 465		 * return an error to the user, because the event is not
 466		 * initiated by a user request.
 467		 * The best the driver could do is to log an error message.
 468		 * We will do it ourselves instead...
 469		 */
 470		netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
 471			   cmd);
 472		return;
 473	}
 474
 475	/* Check extra parameters and set extra_len */
 476	if (descr->header_type == IW_HEADER_TYPE_POINT) {
 477		/* Check if number of token fits within bounds */
 478		if (wrqu->data.length > descr->max_tokens) {
 479			netdev_err(dev, "(WE) : Wireless Event too big (%d)\n",
 480				   wrqu->data.length);
 481			return;
 482		}
 483		if (wrqu->data.length < descr->min_tokens) {
 484			netdev_err(dev, "(WE) : Wireless Event too small (%d)\n",
 485				   wrqu->data.length);
 486			return;
 487		}
 488		/* Calculate extra_len - extra is NULL for restricted events */
 489		if (extra != NULL)
 490			extra_len = wrqu->data.length * descr->token_size;
 491		/* Always at an offset in wrqu */
 492		wrqu_off = IW_EV_POINT_OFF;
 493	}
 494
 495	/* Total length of the event */
 496	hdr_len = event_type_size[descr->header_type];
 497	event_len = hdr_len + extra_len;
 498
 499	/*
 500	 * The problem for 64/32 bit.
 501	 *
 502	 * On 64-bit, a regular event is laid out as follows:
 503	 *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
 504	 *      | event.len | event.cmd |     p a d d i n g     |
 505	 *      | wrqu data ... (with the correct size)         |
 506	 *
 507	 * This padding exists because we manipulate event->u,
 508	 * and 'event' is not packed.
 509	 *
 510	 * An iw_point event is laid out like this instead:
 511	 *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
 512	 *      | event.len | event.cmd |     p a d d i n g     |
 513	 *      | iwpnt.len | iwpnt.flg |     p a d d i n g     |
 514	 *      | extra data  ...
 515	 *
 516	 * The second padding exists because struct iw_point is extended,
 517	 * but this depends on the platform...
 518	 *
 519	 * On 32-bit, all the padding shouldn't be there.
 520	 */
 521
 522	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 523	if (!skb)
 524		return;
 525
 526	/* Send via the RtNetlink event channel */
 527	nlh = rtnetlink_ifinfo_prep(dev, skb);
 528	if (WARN_ON(!nlh)) {
 529		kfree_skb(skb);
 530		return;
 531	}
 532
 533	/* Add the wireless events in the netlink packet */
 534	nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
 535	if (!nla) {
 536		kfree_skb(skb);
 537		return;
 538	}
 539	event = nla_data(nla);
 540
 541	/* Fill event - first clear to avoid data leaking */
 542	memset(event, 0, hdr_len);
 543	event->len = event_len;
 544	event->cmd = cmd;
 545	memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
 546	if (extra_len)
 547		memcpy(((char *) event) + hdr_len, extra, extra_len);
 548
 549	nlmsg_end(skb, nlh);
 550#ifdef CONFIG_COMPAT
 551	hdr_len = compat_event_type_size[descr->header_type];
 
 
 
 552	event_len = hdr_len + extra_len;
 553
 554	compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
 555	if (!compskb) {
 556		kfree_skb(skb);
 557		return;
 558	}
 559
 560	/* Send via the RtNetlink event channel */
 561	nlh = rtnetlink_ifinfo_prep(dev, compskb);
 562	if (WARN_ON(!nlh)) {
 563		kfree_skb(skb);
 564		kfree_skb(compskb);
 565		return;
 566	}
 567
 568	/* Add the wireless events in the netlink packet */
 569	nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
 570	if (!nla) {
 571		kfree_skb(skb);
 572		kfree_skb(compskb);
 573		return;
 574	}
 575	compat_event = nla_data(nla);
 576
 577	compat_event->len = event_len;
 578	compat_event->cmd = cmd;
 579	if (descr->header_type == IW_HEADER_TYPE_POINT) {
 580		compat_wrqu.length = wrqu->data.length;
 581		compat_wrqu.flags = wrqu->data.flags;
 582		memcpy(&compat_event->pointer,
 583			((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
 584			hdr_len - IW_EV_COMPAT_LCP_LEN);
 585		if (extra_len)
 586			memcpy(((char *) compat_event) + hdr_len,
 587				extra, extra_len);
 588	} else {
 589		/* extra_len must be zero, so no if (extra) needed */
 590		memcpy(&compat_event->pointer, wrqu,
 591			hdr_len - IW_EV_COMPAT_LCP_LEN);
 592	}
 593
 594	nlmsg_end(compskb, nlh);
 595
 596	skb_shinfo(skb)->frag_list = compskb;
 597#endif
 598	skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
 599	schedule_work(&wireless_nlevent_work);
 600}
 601EXPORT_SYMBOL(wireless_send_event);
 602
 
 
 
 
 603
 
 
 
 
 604
 605/* IW handlers */
 606
 607struct iw_statistics *get_wireless_stats(struct net_device *dev)
 608{
 609#ifdef CONFIG_WIRELESS_EXT
 610	if ((dev->wireless_handlers != NULL) &&
 611	   (dev->wireless_handlers->get_wireless_stats != NULL))
 612		return dev->wireless_handlers->get_wireless_stats(dev);
 613#endif
 614
 615#ifdef CONFIG_CFG80211_WEXT
 616	if (dev->ieee80211_ptr &&
 617	    dev->ieee80211_ptr->wiphy &&
 618	    dev->ieee80211_ptr->wiphy->wext &&
 619	    dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
 
 
 
 
 620		return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
 
 621#endif
 622
 623	/* not found */
 624	return NULL;
 625}
 626
 627static int iw_handler_get_iwstats(struct net_device *		dev,
 
 628				  struct iw_request_info *	info,
 629				  union iwreq_data *		wrqu,
 630				  char *			extra)
 631{
 632	/* Get stats from the driver */
 633	struct iw_statistics *stats;
 634
 635	stats = get_wireless_stats(dev);
 636	if (stats) {
 637		/* Copy statistics to extra */
 638		memcpy(extra, stats, sizeof(struct iw_statistics));
 639		wrqu->data.length = sizeof(struct iw_statistics);
 640
 641		/* Check if we need to clear the updated flag */
 642		if (wrqu->data.flags != 0)
 643			stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
 644		return 0;
 645	} else
 646		return -EOPNOTSUPP;
 647}
 648
 649static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
 650{
 651	/* Don't "optimise" the following variable, it will crash */
 652	unsigned int	index;		/* *MUST* be unsigned */
 653	const struct iw_handler_def *handlers = NULL;
 654
 655#ifdef CONFIG_CFG80211_WEXT
 656	if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
 
 
 
 
 657		handlers = dev->ieee80211_ptr->wiphy->wext;
 
 658#endif
 659#ifdef CONFIG_WIRELESS_EXT
 660	if (dev->wireless_handlers)
 661		handlers = dev->wireless_handlers;
 662#endif
 663
 664	if (!handlers)
 665		return NULL;
 666
 667	/* Try as a standard command */
 668	index = IW_IOCTL_IDX(cmd);
 669	if (index < handlers->num_standard)
 670		return handlers->standard[index];
 671
 672#ifdef CONFIG_WEXT_PRIV
 673	/* Try as a private command */
 674	index = cmd - SIOCIWFIRSTPRIV;
 675	if (index < handlers->num_private)
 676		return handlers->private[index];
 677#endif
 678
 679	/* Not found */
 680	return NULL;
 681}
 682
 683static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
 684				   const struct iw_ioctl_description *descr,
 685				   iw_handler handler, struct net_device *dev,
 686				   struct iw_request_info *info)
 687{
 688	int err, extra_size, user_length = 0, essid_compat = 0;
 689	char *extra;
 690
 691	/* Calculate space needed by arguments. Always allocate
 692	 * for max space.
 693	 */
 694	extra_size = descr->max_tokens * descr->token_size;
 695
 696	/* Check need for ESSID compatibility for WE < 21 */
 697	switch (cmd) {
 698	case SIOCSIWESSID:
 699	case SIOCGIWESSID:
 700	case SIOCSIWNICKN:
 701	case SIOCGIWNICKN:
 702		if (iwp->length == descr->max_tokens + 1)
 703			essid_compat = 1;
 704		else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
 705			char essid[IW_ESSID_MAX_SIZE + 1];
 706			unsigned int len;
 707			len = iwp->length * descr->token_size;
 708
 709			if (len > IW_ESSID_MAX_SIZE)
 710				return -EFAULT;
 711
 712			err = copy_from_user(essid, iwp->pointer, len);
 713			if (err)
 714				return -EFAULT;
 715
 716			if (essid[iwp->length - 1] == '\0')
 717				essid_compat = 1;
 718		}
 719		break;
 720	default:
 721		break;
 722	}
 723
 724	iwp->length -= essid_compat;
 725
 726	/* Check what user space is giving us */
 727	if (IW_IS_SET(cmd)) {
 728		/* Check NULL pointer */
 729		if (!iwp->pointer && iwp->length != 0)
 730			return -EFAULT;
 731		/* Check if number of token fits within bounds */
 732		if (iwp->length > descr->max_tokens)
 733			return -E2BIG;
 734		if (iwp->length < descr->min_tokens)
 735			return -EINVAL;
 736	} else {
 737		/* Check NULL pointer */
 738		if (!iwp->pointer)
 739			return -EFAULT;
 740		/* Save user space buffer size for checking */
 741		user_length = iwp->length;
 742
 743		/* Don't check if user_length > max to allow forward
 744		 * compatibility. The test user_length < min is
 745		 * implied by the test at the end.
 746		 */
 747
 748		/* Support for very large requests */
 749		if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
 750		    (user_length > descr->max_tokens)) {
 751			/* Allow userspace to GET more than max so
 752			 * we can support any size GET requests.
 753			 * There is still a limit : -ENOMEM.
 754			 */
 755			extra_size = user_length * descr->token_size;
 756
 757			/* Note : user_length is originally a __u16,
 758			 * and token_size is controlled by us,
 759			 * so extra_size won't get negative and
 760			 * won't overflow...
 761			 */
 762		}
 763	}
 764
 
 
 
 
 
 
 765	/* kzalloc() ensures NULL-termination for essid_compat. */
 766	extra = kzalloc(extra_size, GFP_KERNEL);
 767	if (!extra)
 768		return -ENOMEM;
 769
 770	/* If it is a SET, get all the extra data in here */
 771	if (IW_IS_SET(cmd) && (iwp->length != 0)) {
 772		if (copy_from_user(extra, iwp->pointer,
 773				   iwp->length *
 774				   descr->token_size)) {
 775			err = -EFAULT;
 776			goto out;
 777		}
 778
 779		if (cmd == SIOCSIWENCODEEXT) {
 780			struct iw_encode_ext *ee = (void *) extra;
 781
 782			if (iwp->length < sizeof(*ee) + ee->key_len)
 783				return -EFAULT;
 
 
 784		}
 785	}
 786
 787	if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
 788		/*
 789		 * If this is a GET, but not NOMAX, it means that the extra
 790		 * data is not bounded by userspace, but by max_tokens. Thus
 791		 * set the length to max_tokens. This matches the extra data
 792		 * allocation.
 793		 * The driver should fill it with the number of tokens it
 794		 * provided, and it may check iwp->length rather than having
 795		 * knowledge of max_tokens. If the driver doesn't change the
 796		 * iwp->length, this ioctl just copies back max_token tokens
 797		 * filled with zeroes. Hopefully the driver isn't claiming
 798		 * them to be valid data.
 799		 */
 800		iwp->length = descr->max_tokens;
 801	}
 802
 803	err = handler(dev, info, (union iwreq_data *) iwp, extra);
 804
 805	iwp->length += essid_compat;
 806
 807	/* If we have something to return to the user */
 808	if (!err && IW_IS_GET(cmd)) {
 809		/* Check if there is enough buffer up there */
 810		if (user_length < iwp->length) {
 811			err = -E2BIG;
 812			goto out;
 813		}
 814
 815		if (copy_to_user(iwp->pointer, extra,
 816				 iwp->length *
 817				 descr->token_size)) {
 818			err = -EFAULT;
 819			goto out;
 820		}
 821	}
 822
 823	/* Generate an event to notify listeners of the change */
 824	if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
 825	    ((err == 0) || (err == -EIWCOMMIT))) {
 826		union iwreq_data *data = (union iwreq_data *) iwp;
 827
 828		if (descr->flags & IW_DESCR_FLAG_RESTRICT)
 829			/* If the event is restricted, don't
 830			 * export the payload.
 831			 */
 832			wireless_send_event(dev, cmd, data, NULL);
 833		else
 834			wireless_send_event(dev, cmd, data, extra);
 835	}
 836
 837out:
 838	kfree(extra);
 839	return err;
 840}
 841
 842/*
 843 * Call the commit handler in the driver
 844 * (if exist and if conditions are right)
 845 *
 846 * Note : our current commit strategy is currently pretty dumb,
 847 * but we will be able to improve on that...
 848 * The goal is to try to agreagate as many changes as possible
 849 * before doing the commit. Drivers that will define a commit handler
 850 * are usually those that need a reset after changing parameters, so
 851 * we want to minimise the number of reset.
 852 * A cool idea is to use a timer : at each "set" command, we re-set the
 853 * timer, when the timer eventually fires, we call the driver.
 854 * Hopefully, more on that later.
 855 *
 856 * Also, I'm waiting to see how many people will complain about the
 857 * netif_running(dev) test. I'm open on that one...
 858 * Hopefully, the driver will remember to do a commit in "open()" ;-)
 859 */
 860int call_commit_handler(struct net_device *dev)
 861{
 862#ifdef CONFIG_WIRELESS_EXT
 863	if ((netif_running(dev)) &&
 864	   (dev->wireless_handlers->standard[0] != NULL))
 
 865		/* Call the commit handler on the driver */
 866		return dev->wireless_handlers->standard[0](dev, NULL,
 867							   NULL, NULL);
 868	else
 869		return 0;		/* Command completed successfully */
 870#else
 871	/* cfg80211 has no commit */
 872	return 0;
 873#endif
 874}
 875
 876/*
 877 * Main IOCTl dispatcher.
 878 * Check the type of IOCTL and call the appropriate wrapper...
 879 */
 880static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
 881				  unsigned int cmd,
 882				  struct iw_request_info *info,
 883				  wext_ioctl_func standard,
 884				  wext_ioctl_func private)
 885{
 886	struct iwreq *iwr = (struct iwreq *) ifr;
 887	struct net_device *dev;
 888	iw_handler	handler;
 889
 890	/* Permissions are already checked in dev_ioctl() before calling us.
 891	 * The copy_to/from_user() of ifr is also dealt with in there */
 892
 893	/* Make sure the device exist */
 894	if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
 895		return -ENODEV;
 896
 897	/* A bunch of special cases, then the generic case...
 898	 * Note that 'cmd' is already filtered in dev_ioctl() with
 899	 * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
 900	if (cmd == SIOCGIWSTATS)
 901		return standard(dev, iwr, cmd, info,
 902				&iw_handler_get_iwstats);
 903
 904#ifdef CONFIG_WEXT_PRIV
 905	if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
 906		return standard(dev, iwr, cmd, info,
 907				iw_handler_get_private);
 908#endif
 909
 910	/* Basic check */
 911	if (!netif_device_present(dev))
 912		return -ENODEV;
 913
 914	/* New driver API : try to find the handler */
 915	handler = get_handler(dev, cmd);
 916	if (handler) {
 917		/* Standard and private are not the same */
 918		if (cmd < SIOCIWFIRSTPRIV)
 919			return standard(dev, iwr, cmd, info, handler);
 920		else if (private)
 921			return private(dev, iwr, cmd, info, handler);
 922	}
 923	/* Old driver API : call driver ioctl handler */
 924	if (dev->netdev_ops->ndo_do_ioctl)
 925		return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
 926	return -EOPNOTSUPP;
 927}
 928
 929/* If command is `set a parameter', or `get the encoding parameters',
 930 * check if the user has the right to do it.
 931 */
 932static int wext_permission_check(unsigned int cmd)
 933{
 934	if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
 935	     cmd == SIOCGIWENCODEEXT) &&
 936	    !capable(CAP_NET_ADMIN))
 937		return -EPERM;
 938
 939	return 0;
 940}
 941
 942/* entry point from dev ioctl */
 943static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
 944			       unsigned int cmd, struct iw_request_info *info,
 945			       wext_ioctl_func standard,
 946			       wext_ioctl_func private)
 947{
 948	int ret = wext_permission_check(cmd);
 949
 950	if (ret)
 951		return ret;
 952
 953	dev_load(net, ifr->ifr_name);
 954	rtnl_lock();
 955	ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
 956	rtnl_unlock();
 957
 958	return ret;
 959}
 960
 961/*
 962 * Wrapper to call a standard Wireless Extension handler.
 963 * We do various checks and also take care of moving data between
 964 * user space and kernel space.
 965 */
 966static int ioctl_standard_call(struct net_device *	dev,
 967			       struct iwreq		*iwr,
 968			       unsigned int		cmd,
 969			       struct iw_request_info	*info,
 970			       iw_handler		handler)
 971{
 972	const struct iw_ioctl_description *	descr;
 973	int					ret = -EINVAL;
 974
 975	/* Get the description of the IOCTL */
 976	if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
 977		return -EOPNOTSUPP;
 978	descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
 979
 980	/* Check if we have a pointer to user space data or not */
 981	if (descr->header_type != IW_HEADER_TYPE_POINT) {
 982
 983		/* No extra arguments. Trivial to handle */
 984		ret = handler(dev, info, &(iwr->u), NULL);
 985
 986		/* Generate an event to notify listeners of the change */
 987		if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
 988		   ((ret == 0) || (ret == -EIWCOMMIT)))
 989			wireless_send_event(dev, cmd, &(iwr->u), NULL);
 990	} else {
 991		ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
 992					      handler, dev, info);
 993	}
 994
 995	/* Call commit handler if needed and defined */
 996	if (ret == -EIWCOMMIT)
 997		ret = call_commit_handler(dev);
 998
 999	/* Here, we will generate the appropriate event if needed */
1000
1001	return ret;
1002}
1003
1004
1005int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1006		      void __user *arg)
1007{
1008	struct iw_request_info info = { .cmd = cmd, .flags = 0 };
 
1009	int ret;
1010
1011	ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
 
 
 
 
 
1012				  ioctl_standard_call,
1013				  ioctl_private_call);
1014	if (ret >= 0 &&
1015	    IW_IS_GET(cmd) &&
1016	    copy_to_user(arg, ifr, sizeof(struct iwreq)))
1017		return -EFAULT;
1018
1019	return ret;
1020}
1021
1022#ifdef CONFIG_COMPAT
1023static int compat_standard_call(struct net_device	*dev,
1024				struct iwreq		*iwr,
1025				unsigned int		cmd,
1026				struct iw_request_info	*info,
1027				iw_handler		handler)
1028{
1029	const struct iw_ioctl_description *descr;
1030	struct compat_iw_point *iwp_compat;
1031	struct iw_point iwp;
1032	int err;
1033
1034	descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1035
1036	if (descr->header_type != IW_HEADER_TYPE_POINT)
1037		return ioctl_standard_call(dev, iwr, cmd, info, handler);
1038
1039	iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1040	iwp.pointer = compat_ptr(iwp_compat->pointer);
1041	iwp.length = iwp_compat->length;
1042	iwp.flags = iwp_compat->flags;
1043
1044	err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1045
1046	iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1047	iwp_compat->length = iwp.length;
1048	iwp_compat->flags = iwp.flags;
1049
1050	return err;
1051}
1052
1053int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1054			     unsigned long arg)
1055{
1056	void __user *argp = (void __user *)arg;
1057	struct iw_request_info info;
1058	struct iwreq iwr;
1059	char *colon;
1060	int ret;
1061
1062	if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1063		return -EFAULT;
1064
1065	iwr.ifr_name[IFNAMSIZ-1] = 0;
1066	colon = strchr(iwr.ifr_name, ':');
1067	if (colon)
1068		*colon = 0;
1069
1070	info.cmd = cmd;
1071	info.flags = IW_REQUEST_FLAG_COMPAT;
1072
1073	ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
1074				  compat_standard_call,
1075				  compat_private_call);
1076
1077	if (ret >= 0 &&
1078	    IW_IS_GET(cmd) &&
1079	    copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1080		return -EFAULT;
1081
1082	return ret;
1083}
1084#endif