2 * This file implement the Wireless Extensions core API.
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>
8 * (As all part of the Linux kernel, this file is GPL)
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>
20 #include <net/net_namespace.h>
22 typedef int (*wext_ioctl_func
)(struct net_device
*, struct iwreq
*,
23 unsigned int, struct iw_request_info
*,
28 * Meta-data about all the standard Wireless Extension request we
31 static const struct iw_ioctl_description standard_ioctl
[] = {
32 [IW_IOCTL_IDX(SIOCSIWCOMMIT
)] = {
33 .header_type
= IW_HEADER_TYPE_NULL
,
35 [IW_IOCTL_IDX(SIOCGIWNAME
)] = {
36 .header_type
= IW_HEADER_TYPE_CHAR
,
37 .flags
= IW_DESCR_FLAG_DUMP
,
39 [IW_IOCTL_IDX(SIOCSIWNWID
)] = {
40 .header_type
= IW_HEADER_TYPE_PARAM
,
41 .flags
= IW_DESCR_FLAG_EVENT
,
43 [IW_IOCTL_IDX(SIOCGIWNWID
)] = {
44 .header_type
= IW_HEADER_TYPE_PARAM
,
45 .flags
= IW_DESCR_FLAG_DUMP
,
47 [IW_IOCTL_IDX(SIOCSIWFREQ
)] = {
48 .header_type
= IW_HEADER_TYPE_FREQ
,
49 .flags
= IW_DESCR_FLAG_EVENT
,
51 [IW_IOCTL_IDX(SIOCGIWFREQ
)] = {
52 .header_type
= IW_HEADER_TYPE_FREQ
,
53 .flags
= IW_DESCR_FLAG_DUMP
,
55 [IW_IOCTL_IDX(SIOCSIWMODE
)] = {
56 .header_type
= IW_HEADER_TYPE_UINT
,
57 .flags
= IW_DESCR_FLAG_EVENT
,
59 [IW_IOCTL_IDX(SIOCGIWMODE
)] = {
60 .header_type
= IW_HEADER_TYPE_UINT
,
61 .flags
= IW_DESCR_FLAG_DUMP
,
63 [IW_IOCTL_IDX(SIOCSIWSENS
)] = {
64 .header_type
= IW_HEADER_TYPE_PARAM
,
66 [IW_IOCTL_IDX(SIOCGIWSENS
)] = {
67 .header_type
= IW_HEADER_TYPE_PARAM
,
69 [IW_IOCTL_IDX(SIOCSIWRANGE
)] = {
70 .header_type
= IW_HEADER_TYPE_NULL
,
72 [IW_IOCTL_IDX(SIOCGIWRANGE
)] = {
73 .header_type
= IW_HEADER_TYPE_POINT
,
75 .max_tokens
= sizeof(struct iw_range
),
76 .flags
= IW_DESCR_FLAG_DUMP
,
78 [IW_IOCTL_IDX(SIOCSIWPRIV
)] = {
79 .header_type
= IW_HEADER_TYPE_NULL
,
81 [IW_IOCTL_IDX(SIOCGIWPRIV
)] = { /* (handled directly by us) */
82 .header_type
= IW_HEADER_TYPE_POINT
,
83 .token_size
= sizeof(struct iw_priv_args
),
85 .flags
= IW_DESCR_FLAG_NOMAX
,
87 [IW_IOCTL_IDX(SIOCSIWSTATS
)] = {
88 .header_type
= IW_HEADER_TYPE_NULL
,
90 [IW_IOCTL_IDX(SIOCGIWSTATS
)] = { /* (handled directly by us) */
91 .header_type
= IW_HEADER_TYPE_POINT
,
93 .max_tokens
= sizeof(struct iw_statistics
),
94 .flags
= IW_DESCR_FLAG_DUMP
,
96 [IW_IOCTL_IDX(SIOCSIWSPY
)] = {
97 .header_type
= IW_HEADER_TYPE_POINT
,
98 .token_size
= sizeof(struct sockaddr
),
99 .max_tokens
= IW_MAX_SPY
,
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
,
107 [IW_IOCTL_IDX(SIOCSIWTHRSPY
)] = {
108 .header_type
= IW_HEADER_TYPE_POINT
,
109 .token_size
= sizeof(struct iw_thrspy
),
113 [IW_IOCTL_IDX(SIOCGIWTHRSPY
)] = {
114 .header_type
= IW_HEADER_TYPE_POINT
,
115 .token_size
= sizeof(struct iw_thrspy
),
119 [IW_IOCTL_IDX(SIOCSIWAP
)] = {
120 .header_type
= IW_HEADER_TYPE_ADDR
,
122 [IW_IOCTL_IDX(SIOCGIWAP
)] = {
123 .header_type
= IW_HEADER_TYPE_ADDR
,
124 .flags
= IW_DESCR_FLAG_DUMP
,
126 [IW_IOCTL_IDX(SIOCSIWMLME
)] = {
127 .header_type
= IW_HEADER_TYPE_POINT
,
129 .min_tokens
= sizeof(struct iw_mlme
),
130 .max_tokens
= sizeof(struct iw_mlme
),
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
,
139 [IW_IOCTL_IDX(SIOCSIWSCAN
)] = {
140 .header_type
= IW_HEADER_TYPE_POINT
,
143 .max_tokens
= sizeof(struct iw_scan_req
),
145 [IW_IOCTL_IDX(SIOCGIWSCAN
)] = {
146 .header_type
= IW_HEADER_TYPE_POINT
,
148 .max_tokens
= IW_SCAN_MAX_DATA
,
149 .flags
= IW_DESCR_FLAG_NOMAX
,
151 [IW_IOCTL_IDX(SIOCSIWESSID
)] = {
152 .header_type
= IW_HEADER_TYPE_POINT
,
154 .max_tokens
= IW_ESSID_MAX_SIZE
,
155 .flags
= IW_DESCR_FLAG_EVENT
,
157 [IW_IOCTL_IDX(SIOCGIWESSID
)] = {
158 .header_type
= IW_HEADER_TYPE_POINT
,
160 .max_tokens
= IW_ESSID_MAX_SIZE
,
161 .flags
= IW_DESCR_FLAG_DUMP
,
163 [IW_IOCTL_IDX(SIOCSIWNICKN
)] = {
164 .header_type
= IW_HEADER_TYPE_POINT
,
166 .max_tokens
= IW_ESSID_MAX_SIZE
,
168 [IW_IOCTL_IDX(SIOCGIWNICKN
)] = {
169 .header_type
= IW_HEADER_TYPE_POINT
,
171 .max_tokens
= IW_ESSID_MAX_SIZE
,
173 [IW_IOCTL_IDX(SIOCSIWRATE
)] = {
174 .header_type
= IW_HEADER_TYPE_PARAM
,
176 [IW_IOCTL_IDX(SIOCGIWRATE
)] = {
177 .header_type
= IW_HEADER_TYPE_PARAM
,
179 [IW_IOCTL_IDX(SIOCSIWRTS
)] = {
180 .header_type
= IW_HEADER_TYPE_PARAM
,
182 [IW_IOCTL_IDX(SIOCGIWRTS
)] = {
183 .header_type
= IW_HEADER_TYPE_PARAM
,
185 [IW_IOCTL_IDX(SIOCSIWFRAG
)] = {
186 .header_type
= IW_HEADER_TYPE_PARAM
,
188 [IW_IOCTL_IDX(SIOCGIWFRAG
)] = {
189 .header_type
= IW_HEADER_TYPE_PARAM
,
191 [IW_IOCTL_IDX(SIOCSIWTXPOW
)] = {
192 .header_type
= IW_HEADER_TYPE_PARAM
,
194 [IW_IOCTL_IDX(SIOCGIWTXPOW
)] = {
195 .header_type
= IW_HEADER_TYPE_PARAM
,
197 [IW_IOCTL_IDX(SIOCSIWRETRY
)] = {
198 .header_type
= IW_HEADER_TYPE_PARAM
,
200 [IW_IOCTL_IDX(SIOCGIWRETRY
)] = {
201 .header_type
= IW_HEADER_TYPE_PARAM
,
203 [IW_IOCTL_IDX(SIOCSIWENCODE
)] = {
204 .header_type
= IW_HEADER_TYPE_POINT
,
206 .max_tokens
= IW_ENCODING_TOKEN_MAX
,
207 .flags
= IW_DESCR_FLAG_EVENT
| IW_DESCR_FLAG_RESTRICT
,
209 [IW_IOCTL_IDX(SIOCGIWENCODE
)] = {
210 .header_type
= IW_HEADER_TYPE_POINT
,
212 .max_tokens
= IW_ENCODING_TOKEN_MAX
,
213 .flags
= IW_DESCR_FLAG_DUMP
| IW_DESCR_FLAG_RESTRICT
,
215 [IW_IOCTL_IDX(SIOCSIWPOWER
)] = {
216 .header_type
= IW_HEADER_TYPE_PARAM
,
218 [IW_IOCTL_IDX(SIOCGIWPOWER
)] = {
219 .header_type
= IW_HEADER_TYPE_PARAM
,
221 [IW_IOCTL_IDX(SIOCSIWGENIE
)] = {
222 .header_type
= IW_HEADER_TYPE_POINT
,
224 .max_tokens
= IW_GENERIC_IE_MAX
,
226 [IW_IOCTL_IDX(SIOCGIWGENIE
)] = {
227 .header_type
= IW_HEADER_TYPE_POINT
,
229 .max_tokens
= IW_GENERIC_IE_MAX
,
231 [IW_IOCTL_IDX(SIOCSIWAUTH
)] = {
232 .header_type
= IW_HEADER_TYPE_PARAM
,
234 [IW_IOCTL_IDX(SIOCGIWAUTH
)] = {
235 .header_type
= IW_HEADER_TYPE_PARAM
,
237 [IW_IOCTL_IDX(SIOCSIWENCODEEXT
)] = {
238 .header_type
= IW_HEADER_TYPE_POINT
,
240 .min_tokens
= sizeof(struct iw_encode_ext
),
241 .max_tokens
= sizeof(struct iw_encode_ext
) +
242 IW_ENCODING_TOKEN_MAX
,
244 [IW_IOCTL_IDX(SIOCGIWENCODEEXT
)] = {
245 .header_type
= IW_HEADER_TYPE_POINT
,
247 .min_tokens
= sizeof(struct iw_encode_ext
),
248 .max_tokens
= sizeof(struct iw_encode_ext
) +
249 IW_ENCODING_TOKEN_MAX
,
251 [IW_IOCTL_IDX(SIOCSIWPMKSA
)] = {
252 .header_type
= IW_HEADER_TYPE_POINT
,
254 .min_tokens
= sizeof(struct iw_pmksa
),
255 .max_tokens
= sizeof(struct iw_pmksa
),
258 static const unsigned standard_ioctl_num
= ARRAY_SIZE(standard_ioctl
);
261 * Meta-data about all the additional standard Wireless Extension events
264 static const struct iw_ioctl_description standard_event
[] = {
265 [IW_EVENT_IDX(IWEVTXDROP
)] = {
266 .header_type
= IW_HEADER_TYPE_ADDR
,
268 [IW_EVENT_IDX(IWEVQUAL
)] = {
269 .header_type
= IW_HEADER_TYPE_QUAL
,
271 [IW_EVENT_IDX(IWEVCUSTOM
)] = {
272 .header_type
= IW_HEADER_TYPE_POINT
,
274 .max_tokens
= IW_CUSTOM_MAX
,
276 [IW_EVENT_IDX(IWEVREGISTERED
)] = {
277 .header_type
= IW_HEADER_TYPE_ADDR
,
279 [IW_EVENT_IDX(IWEVEXPIRED
)] = {
280 .header_type
= IW_HEADER_TYPE_ADDR
,
282 [IW_EVENT_IDX(IWEVGENIE
)] = {
283 .header_type
= IW_HEADER_TYPE_POINT
,
285 .max_tokens
= IW_GENERIC_IE_MAX
,
287 [IW_EVENT_IDX(IWEVMICHAELMICFAILURE
)] = {
288 .header_type
= IW_HEADER_TYPE_POINT
,
290 .max_tokens
= sizeof(struct iw_michaelmicfailure
),
292 [IW_EVENT_IDX(IWEVASSOCREQIE
)] = {
293 .header_type
= IW_HEADER_TYPE_POINT
,
295 .max_tokens
= IW_GENERIC_IE_MAX
,
297 [IW_EVENT_IDX(IWEVASSOCRESPIE
)] = {
298 .header_type
= IW_HEADER_TYPE_POINT
,
300 .max_tokens
= IW_GENERIC_IE_MAX
,
302 [IW_EVENT_IDX(IWEVPMKIDCAND
)] = {
303 .header_type
= IW_HEADER_TYPE_POINT
,
305 .max_tokens
= sizeof(struct iw_pmkid_cand
),
308 static const unsigned standard_event_num
= ARRAY_SIZE(standard_event
);
310 /* Size (in bytes) of various events */
311 static const int event_type_size
[] = {
312 IW_EV_LCP_LEN
, /* IW_HEADER_TYPE_NULL */
314 IW_EV_CHAR_LEN
, /* IW_HEADER_TYPE_CHAR */
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 */
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 */
326 static const int compat_event_type_size
[] = {
327 IW_EV_COMPAT_LCP_LEN
, /* IW_HEADER_TYPE_NULL */
329 IW_EV_COMPAT_CHAR_LEN
, /* IW_HEADER_TYPE_CHAR */
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 */
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 */
344 static int __net_init
wext_pernet_init(struct net
*net
)
346 skb_queue_head_init(&net
->wext_nlevents
);
350 static void __net_exit
wext_pernet_exit(struct net
*net
)
352 skb_queue_purge(&net
->wext_nlevents
);
355 static struct pernet_operations wext_pernet_ops
= {
356 .init
= wext_pernet_init
,
357 .exit
= wext_pernet_exit
,
360 static int __init
wireless_nlevent_init(void)
362 return register_pernet_subsys(&wext_pernet_ops
);
365 subsys_initcall(wireless_nlevent_init
);
367 /* Process events generated by the wireless layer or the driver. */
368 static void wireless_nlevent_process(struct work_struct
*work
)
376 while ((skb
= skb_dequeue(&net
->wext_nlevents
)))
377 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
,
384 static DECLARE_WORK(wireless_nlevent_work
, wireless_nlevent_process
);
386 static struct nlmsghdr
*rtnetlink_ifinfo_prep(struct net_device
*dev
,
390 struct nlmsghdr
*nlh
;
392 nlh
= nlmsg_put(skb
, 0, 0, RTM_NEWLINK
, sizeof(*r
), 0);
397 r
->ifi_family
= AF_UNSPEC
;
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 */
404 NLA_PUT_STRING(skb
, IFLA_IFNAME
, dev
->name
);
408 nlmsg_cancel(skb
, nlh
);
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.
418 void wireless_send_event(struct net_device
* dev
,
420 union iwreq_data
* wrqu
,
423 const struct iw_ioctl_description
* descr
= NULL
;
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 */
432 struct nlmsghdr
*nlh
;
435 struct __compat_iw_event
*compat_event
;
436 struct compat_iw_point compat_wrqu
;
437 struct sk_buff
*compskb
;
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
448 if (WARN_ON(cmd
== SIOCGIWSCAN
&& extra
))
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
]);
457 cmd_index
= IW_EVENT_IDX(cmd
);
458 if (cmd_index
< standard_event_num
)
459 descr
= &(standard_event
[cmd_index
]);
461 /* Don't accept unknown events */
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...
470 netdev_err(dev
, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
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",
483 if (wrqu
->data
.length
< descr
->min_tokens
) {
484 netdev_err(dev
, "(WE) : Wireless Event too small (%d)\n",
488 /* Calculate extra_len - extra is NULL for restricted events */
490 extra_len
= wrqu
->data
.length
* descr
->token_size
;
491 /* Always at an offset in wrqu */
492 wrqu_off
= IW_EV_POINT_OFF
;
495 /* Total length of the event */
496 hdr_len
= event_type_size
[descr
->header_type
];
497 event_len
= hdr_len
+ extra_len
;
500 * The problem for 64/32 bit.
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) |
507 * This padding exists because we manipulate event->u,
508 * and 'event' is not packed.
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 |
516 * The second padding exists because struct iw_point is extended,
517 * but this depends on the platform...
519 * On 32-bit, all the padding shouldn't be there.
522 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
526 /* Send via the RtNetlink event channel */
527 nlh
= rtnetlink_ifinfo_prep(dev
, skb
);
533 /* Add the wireless events in the netlink packet */
534 nla
= nla_reserve(skb
, IFLA_WIRELESS
, event_len
);
539 event
= nla_data(nla
);
541 /* Fill event - first clear to avoid data leaking */
542 memset(event
, 0, hdr_len
);
543 event
->len
= event_len
;
545 memcpy(&event
->u
, ((char *) wrqu
) + wrqu_off
, hdr_len
- IW_EV_LCP_LEN
);
547 memcpy(((char *) event
) + hdr_len
, extra
, extra_len
);
551 hdr_len
= compat_event_type_size
[descr
->header_type
];
552 event_len
= hdr_len
+ extra_len
;
554 compskb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
560 /* Send via the RtNetlink event channel */
561 nlh
= rtnetlink_ifinfo_prep(dev
, compskb
);
568 /* Add the wireless events in the netlink packet */
569 nla
= nla_reserve(compskb
, IFLA_WIRELESS
, event_len
);
575 compat_event
= nla_data(nla
);
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
);
586 memcpy(((char *) compat_event
) + hdr_len
,
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
);
594 nlmsg_end(compskb
, nlh
);
596 skb_shinfo(skb
)->frag_list
= compskb
;
598 skb_queue_tail(&dev_net(dev
)->wext_nlevents
, skb
);
599 schedule_work(&wireless_nlevent_work
);
601 EXPORT_SYMBOL(wireless_send_event
);
607 struct iw_statistics
*get_wireless_stats(struct net_device
*dev
)
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
);
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
);
627 static int iw_handler_get_iwstats(struct net_device
* dev
,
628 struct iw_request_info
* info
,
629 union iwreq_data
* wrqu
,
632 /* Get stats from the driver */
633 struct iw_statistics
*stats
;
635 stats
= get_wireless_stats(dev
);
637 /* Copy statistics to extra */
638 memcpy(extra
, stats
, sizeof(struct iw_statistics
));
639 wrqu
->data
.length
= sizeof(struct iw_statistics
);
641 /* Check if we need to clear the updated flag */
642 if (wrqu
->data
.flags
!= 0)
643 stats
->qual
.updated
&= ~IW_QUAL_ALL_UPDATED
;
649 static iw_handler
get_handler(struct net_device
*dev
, unsigned int cmd
)
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
;
655 #ifdef CONFIG_CFG80211_WEXT
656 if (dev
->ieee80211_ptr
&& dev
->ieee80211_ptr
->wiphy
)
657 handlers
= dev
->ieee80211_ptr
->wiphy
->wext
;
659 #ifdef CONFIG_WIRELESS_EXT
660 if (dev
->wireless_handlers
)
661 handlers
= dev
->wireless_handlers
;
667 /* Try as a standard command */
668 index
= IW_IOCTL_IDX(cmd
);
669 if (index
< handlers
->num_standard
)
670 return handlers
->standard
[index
];
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
];
683 static 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
)
688 int err
, extra_size
, user_length
= 0, essid_compat
= 0;
691 /* Calculate space needed by arguments. Always allocate
694 extra_size
= descr
->max_tokens
* descr
->token_size
;
696 /* Check need for ESSID compatibility for WE < 21 */
702 if (iwp
->length
== descr
->max_tokens
+ 1)
704 else if (IW_IS_SET(cmd
) && (iwp
->length
!= 0)) {
705 char essid
[IW_ESSID_MAX_SIZE
+ 1];
707 len
= iwp
->length
* descr
->token_size
;
709 if (len
> IW_ESSID_MAX_SIZE
)
712 err
= copy_from_user(essid
, iwp
->pointer
, len
);
716 if (essid
[iwp
->length
- 1] == '\0')
724 iwp
->length
-= essid_compat
;
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)
731 /* Check if number of token fits within bounds */
732 if (iwp
->length
> descr
->max_tokens
)
734 if (iwp
->length
< descr
->min_tokens
)
737 /* Check NULL pointer */
740 /* Save user space buffer size for checking */
741 user_length
= iwp
->length
;
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.
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.
755 extra_size
= user_length
* descr
->token_size
;
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
765 /* kzalloc() ensures NULL-termination for essid_compat. */
766 extra
= kzalloc(extra_size
, GFP_KERNEL
);
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
,
774 descr
->token_size
)) {
779 if (cmd
== SIOCSIWENCODEEXT
) {
780 struct iw_encode_ext
*ee
= (void *) extra
;
782 if (iwp
->length
< sizeof(*ee
) + ee
->key_len
)
787 if (IW_IS_GET(cmd
) && !(descr
->flags
& IW_DESCR_FLAG_NOMAX
)) {
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
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.
800 iwp
->length
= descr
->max_tokens
;
803 err
= handler(dev
, info
, (union iwreq_data
*) iwp
, extra
);
805 iwp
->length
+= essid_compat
;
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
) {
815 if (copy_to_user(iwp
->pointer
, extra
,
817 descr
->token_size
)) {
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
;
828 if (descr
->flags
& IW_DESCR_FLAG_RESTRICT
)
829 /* If the event is restricted, don't
830 * export the payload.
832 wireless_send_event(dev
, cmd
, data
, NULL
);
834 wireless_send_event(dev
, cmd
, data
, extra
);
843 * Call the commit handler in the driver
844 * (if exist and if conditions are right)
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.
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()" ;-)
860 int call_commit_handler(struct net_device
*dev
)
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
,
869 return 0; /* Command completed successfully */
871 /* cfg80211 has no commit */
877 * Main IOCTl dispatcher.
878 * Check the type of IOCTL and call the appropriate wrapper...
880 static int wireless_process_ioctl(struct net
*net
, struct ifreq
*ifr
,
882 struct iw_request_info
*info
,
883 wext_ioctl_func standard
,
884 wext_ioctl_func
private)
886 struct iwreq
*iwr
= (struct iwreq
*) ifr
;
887 struct net_device
*dev
;
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 */
893 /* Make sure the device exist */
894 if ((dev
= __dev_get_by_name(net
, ifr
->ifr_name
)) == NULL
)
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
);
904 #ifdef CONFIG_WEXT_PRIV
905 if (cmd
== SIOCGIWPRIV
&& dev
->wireless_handlers
)
906 return standard(dev
, iwr
, cmd
, info
,
907 iw_handler_get_private
);
911 if (!netif_device_present(dev
))
914 /* New driver API : try to find the handler */
915 handler
= get_handler(dev
, cmd
);
917 /* Standard and private are not the same */
918 if (cmd
< SIOCIWFIRSTPRIV
)
919 return standard(dev
, iwr
, cmd
, info
, handler
);
921 return private(dev
, iwr
, cmd
, info
, handler
);
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
);
929 /* If command is `set a parameter', or `get the encoding parameters',
930 * check if the user has the right to do it.
932 static int wext_permission_check(unsigned int cmd
)
934 if ((IW_IS_SET(cmd
) || cmd
== SIOCGIWENCODE
||
935 cmd
== SIOCGIWENCODEEXT
) &&
936 !capable(CAP_NET_ADMIN
))
942 /* entry point from dev ioctl */
943 static 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)
948 int ret
= wext_permission_check(cmd
);
953 dev_load(net
, ifr
->ifr_name
);
955 ret
= wireless_process_ioctl(net
, ifr
, cmd
, info
, standard
, private);
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.
966 static int ioctl_standard_call(struct net_device
* dev
,
969 struct iw_request_info
*info
,
972 const struct iw_ioctl_description
* descr
;
975 /* Get the description of the IOCTL */
976 if (IW_IOCTL_IDX(cmd
) >= standard_ioctl_num
)
978 descr
= &(standard_ioctl
[IW_IOCTL_IDX(cmd
)]);
980 /* Check if we have a pointer to user space data or not */
981 if (descr
->header_type
!= IW_HEADER_TYPE_POINT
) {
983 /* No extra arguments. Trivial to handle */
984 ret
= handler(dev
, info
, &(iwr
->u
), NULL
);
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
);
991 ret
= ioctl_standard_iw_point(&iwr
->u
.data
, cmd
, descr
,
995 /* Call commit handler if needed and defined */
996 if (ret
== -EIWCOMMIT
)
997 ret
= call_commit_handler(dev
);
999 /* Here, we will generate the appropriate event if needed */
1005 int wext_handle_ioctl(struct net
*net
, struct ifreq
*ifr
, unsigned int cmd
,
1008 struct iw_request_info info
= { .cmd
= cmd
, .flags
= 0 };
1011 ret
= wext_ioctl_dispatch(net
, ifr
, cmd
, &info
,
1012 ioctl_standard_call
,
1013 ioctl_private_call
);
1016 copy_to_user(arg
, ifr
, sizeof(struct iwreq
)))
1022 #ifdef CONFIG_COMPAT
1023 static int compat_standard_call(struct net_device
*dev
,
1026 struct iw_request_info
*info
,
1029 const struct iw_ioctl_description
*descr
;
1030 struct compat_iw_point
*iwp_compat
;
1031 struct iw_point iwp
;
1034 descr
= standard_ioctl
+ IW_IOCTL_IDX(cmd
);
1036 if (descr
->header_type
!= IW_HEADER_TYPE_POINT
)
1037 return ioctl_standard_call(dev
, iwr
, cmd
, info
, handler
);
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
;
1044 err
= ioctl_standard_iw_point(&iwp
, cmd
, descr
, handler
, dev
, info
);
1046 iwp_compat
->pointer
= ptr_to_compat(iwp
.pointer
);
1047 iwp_compat
->length
= iwp
.length
;
1048 iwp_compat
->flags
= iwp
.flags
;
1053 int compat_wext_handle_ioctl(struct net
*net
, unsigned int cmd
,
1056 void __user
*argp
= (void __user
*)arg
;
1057 struct iw_request_info info
;
1062 if (copy_from_user(&iwr
, argp
, sizeof(struct iwreq
)))
1065 iwr
.ifr_name
[IFNAMSIZ
-1] = 0;
1066 colon
= strchr(iwr
.ifr_name
, ':');
1071 info
.flags
= IW_REQUEST_FLAG_COMPAT
;
1073 ret
= wext_ioctl_dispatch(net
, (struct ifreq
*) &iwr
, cmd
, &info
,
1074 compat_standard_call
,
1075 compat_private_call
);
1079 copy_to_user(argp
, &iwr
, sizeof(struct iwreq
)))