1
2
3
4
5
6
7
8
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 <linux/export.h>
17#include <net/cfg80211.h>
18#include <net/iw_handler.h>
19#include <net/netlink.h>
20#include <net/wext.h>
21#include <net/net_namespace.h>
22
23typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
24 unsigned int, struct iw_request_info *,
25 iw_handler);
26
27
28
29
30
31
32static const struct iw_ioctl_description standard_ioctl[] = {
33 [IW_IOCTL_IDX(SIOCSIWCOMMIT)] = {
34 .header_type = IW_HEADER_TYPE_NULL,
35 },
36 [IW_IOCTL_IDX(SIOCGIWNAME)] = {
37 .header_type = IW_HEADER_TYPE_CHAR,
38 .flags = IW_DESCR_FLAG_DUMP,
39 },
40 [IW_IOCTL_IDX(SIOCSIWNWID)] = {
41 .header_type = IW_HEADER_TYPE_PARAM,
42 .flags = IW_DESCR_FLAG_EVENT,
43 },
44 [IW_IOCTL_IDX(SIOCGIWNWID)] = {
45 .header_type = IW_HEADER_TYPE_PARAM,
46 .flags = IW_DESCR_FLAG_DUMP,
47 },
48 [IW_IOCTL_IDX(SIOCSIWFREQ)] = {
49 .header_type = IW_HEADER_TYPE_FREQ,
50 .flags = IW_DESCR_FLAG_EVENT,
51 },
52 [IW_IOCTL_IDX(SIOCGIWFREQ)] = {
53 .header_type = IW_HEADER_TYPE_FREQ,
54 .flags = IW_DESCR_FLAG_DUMP,
55 },
56 [IW_IOCTL_IDX(SIOCSIWMODE)] = {
57 .header_type = IW_HEADER_TYPE_UINT,
58 .flags = IW_DESCR_FLAG_EVENT,
59 },
60 [IW_IOCTL_IDX(SIOCGIWMODE)] = {
61 .header_type = IW_HEADER_TYPE_UINT,
62 .flags = IW_DESCR_FLAG_DUMP,
63 },
64 [IW_IOCTL_IDX(SIOCSIWSENS)] = {
65 .header_type = IW_HEADER_TYPE_PARAM,
66 },
67 [IW_IOCTL_IDX(SIOCGIWSENS)] = {
68 .header_type = IW_HEADER_TYPE_PARAM,
69 },
70 [IW_IOCTL_IDX(SIOCSIWRANGE)] = {
71 .header_type = IW_HEADER_TYPE_NULL,
72 },
73 [IW_IOCTL_IDX(SIOCGIWRANGE)] = {
74 .header_type = IW_HEADER_TYPE_POINT,
75 .token_size = 1,
76 .max_tokens = sizeof(struct iw_range),
77 .flags = IW_DESCR_FLAG_DUMP,
78 },
79 [IW_IOCTL_IDX(SIOCSIWPRIV)] = {
80 .header_type = IW_HEADER_TYPE_NULL,
81 },
82 [IW_IOCTL_IDX(SIOCGIWPRIV)] = {
83 .header_type = IW_HEADER_TYPE_POINT,
84 .token_size = sizeof(struct iw_priv_args),
85 .max_tokens = 16,
86 .flags = IW_DESCR_FLAG_NOMAX,
87 },
88 [IW_IOCTL_IDX(SIOCSIWSTATS)] = {
89 .header_type = IW_HEADER_TYPE_NULL,
90 },
91 [IW_IOCTL_IDX(SIOCGIWSTATS)] = {
92 .header_type = IW_HEADER_TYPE_POINT,
93 .token_size = 1,
94 .max_tokens = sizeof(struct iw_statistics),
95 .flags = IW_DESCR_FLAG_DUMP,
96 },
97 [IW_IOCTL_IDX(SIOCSIWSPY)] = {
98 .header_type = IW_HEADER_TYPE_POINT,
99 .token_size = sizeof(struct sockaddr),
100 .max_tokens = IW_MAX_SPY,
101 },
102 [IW_IOCTL_IDX(SIOCGIWSPY)] = {
103 .header_type = IW_HEADER_TYPE_POINT,
104 .token_size = sizeof(struct sockaddr) +
105 sizeof(struct iw_quality),
106 .max_tokens = IW_MAX_SPY,
107 },
108 [IW_IOCTL_IDX(SIOCSIWTHRSPY)] = {
109 .header_type = IW_HEADER_TYPE_POINT,
110 .token_size = sizeof(struct iw_thrspy),
111 .min_tokens = 1,
112 .max_tokens = 1,
113 },
114 [IW_IOCTL_IDX(SIOCGIWTHRSPY)] = {
115 .header_type = IW_HEADER_TYPE_POINT,
116 .token_size = sizeof(struct iw_thrspy),
117 .min_tokens = 1,
118 .max_tokens = 1,
119 },
120 [IW_IOCTL_IDX(SIOCSIWAP)] = {
121 .header_type = IW_HEADER_TYPE_ADDR,
122 },
123 [IW_IOCTL_IDX(SIOCGIWAP)] = {
124 .header_type = IW_HEADER_TYPE_ADDR,
125 .flags = IW_DESCR_FLAG_DUMP,
126 },
127 [IW_IOCTL_IDX(SIOCSIWMLME)] = {
128 .header_type = IW_HEADER_TYPE_POINT,
129 .token_size = 1,
130 .min_tokens = sizeof(struct iw_mlme),
131 .max_tokens = sizeof(struct iw_mlme),
132 },
133 [IW_IOCTL_IDX(SIOCGIWAPLIST)] = {
134 .header_type = IW_HEADER_TYPE_POINT,
135 .token_size = sizeof(struct sockaddr) +
136 sizeof(struct iw_quality),
137 .max_tokens = IW_MAX_AP,
138 .flags = IW_DESCR_FLAG_NOMAX,
139 },
140 [IW_IOCTL_IDX(SIOCSIWSCAN)] = {
141 .header_type = IW_HEADER_TYPE_POINT,
142 .token_size = 1,
143 .min_tokens = 0,
144 .max_tokens = sizeof(struct iw_scan_req),
145 },
146 [IW_IOCTL_IDX(SIOCGIWSCAN)] = {
147 .header_type = IW_HEADER_TYPE_POINT,
148 .token_size = 1,
149 .max_tokens = IW_SCAN_MAX_DATA,
150 .flags = IW_DESCR_FLAG_NOMAX,
151 },
152 [IW_IOCTL_IDX(SIOCSIWESSID)] = {
153 .header_type = IW_HEADER_TYPE_POINT,
154 .token_size = 1,
155 .max_tokens = IW_ESSID_MAX_SIZE,
156 .flags = IW_DESCR_FLAG_EVENT,
157 },
158 [IW_IOCTL_IDX(SIOCGIWESSID)] = {
159 .header_type = IW_HEADER_TYPE_POINT,
160 .token_size = 1,
161 .max_tokens = IW_ESSID_MAX_SIZE,
162 .flags = IW_DESCR_FLAG_DUMP,
163 },
164 [IW_IOCTL_IDX(SIOCSIWNICKN)] = {
165 .header_type = IW_HEADER_TYPE_POINT,
166 .token_size = 1,
167 .max_tokens = IW_ESSID_MAX_SIZE,
168 },
169 [IW_IOCTL_IDX(SIOCGIWNICKN)] = {
170 .header_type = IW_HEADER_TYPE_POINT,
171 .token_size = 1,
172 .max_tokens = IW_ESSID_MAX_SIZE,
173 },
174 [IW_IOCTL_IDX(SIOCSIWRATE)] = {
175 .header_type = IW_HEADER_TYPE_PARAM,
176 },
177 [IW_IOCTL_IDX(SIOCGIWRATE)] = {
178 .header_type = IW_HEADER_TYPE_PARAM,
179 },
180 [IW_IOCTL_IDX(SIOCSIWRTS)] = {
181 .header_type = IW_HEADER_TYPE_PARAM,
182 },
183 [IW_IOCTL_IDX(SIOCGIWRTS)] = {
184 .header_type = IW_HEADER_TYPE_PARAM,
185 },
186 [IW_IOCTL_IDX(SIOCSIWFRAG)] = {
187 .header_type = IW_HEADER_TYPE_PARAM,
188 },
189 [IW_IOCTL_IDX(SIOCGIWFRAG)] = {
190 .header_type = IW_HEADER_TYPE_PARAM,
191 },
192 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = {
193 .header_type = IW_HEADER_TYPE_PARAM,
194 },
195 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = {
196 .header_type = IW_HEADER_TYPE_PARAM,
197 },
198 [IW_IOCTL_IDX(SIOCSIWRETRY)] = {
199 .header_type = IW_HEADER_TYPE_PARAM,
200 },
201 [IW_IOCTL_IDX(SIOCGIWRETRY)] = {
202 .header_type = IW_HEADER_TYPE_PARAM,
203 },
204 [IW_IOCTL_IDX(SIOCSIWENCODE)] = {
205 .header_type = IW_HEADER_TYPE_POINT,
206 .token_size = 1,
207 .max_tokens = IW_ENCODING_TOKEN_MAX,
208 .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
209 },
210 [IW_IOCTL_IDX(SIOCGIWENCODE)] = {
211 .header_type = IW_HEADER_TYPE_POINT,
212 .token_size = 1,
213 .max_tokens = IW_ENCODING_TOKEN_MAX,
214 .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
215 },
216 [IW_IOCTL_IDX(SIOCSIWPOWER)] = {
217 .header_type = IW_HEADER_TYPE_PARAM,
218 },
219 [IW_IOCTL_IDX(SIOCGIWPOWER)] = {
220 .header_type = IW_HEADER_TYPE_PARAM,
221 },
222 [IW_IOCTL_IDX(SIOCSIWGENIE)] = {
223 .header_type = IW_HEADER_TYPE_POINT,
224 .token_size = 1,
225 .max_tokens = IW_GENERIC_IE_MAX,
226 },
227 [IW_IOCTL_IDX(SIOCGIWGENIE)] = {
228 .header_type = IW_HEADER_TYPE_POINT,
229 .token_size = 1,
230 .max_tokens = IW_GENERIC_IE_MAX,
231 },
232 [IW_IOCTL_IDX(SIOCSIWAUTH)] = {
233 .header_type = IW_HEADER_TYPE_PARAM,
234 },
235 [IW_IOCTL_IDX(SIOCGIWAUTH)] = {
236 .header_type = IW_HEADER_TYPE_PARAM,
237 },
238 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)] = {
239 .header_type = IW_HEADER_TYPE_POINT,
240 .token_size = 1,
241 .min_tokens = sizeof(struct iw_encode_ext),
242 .max_tokens = sizeof(struct iw_encode_ext) +
243 IW_ENCODING_TOKEN_MAX,
244 },
245 [IW_IOCTL_IDX(SIOCGIWENCODEEXT)] = {
246 .header_type = IW_HEADER_TYPE_POINT,
247 .token_size = 1,
248 .min_tokens = sizeof(struct iw_encode_ext),
249 .max_tokens = sizeof(struct iw_encode_ext) +
250 IW_ENCODING_TOKEN_MAX,
251 },
252 [IW_IOCTL_IDX(SIOCSIWPMKSA)] = {
253 .header_type = IW_HEADER_TYPE_POINT,
254 .token_size = 1,
255 .min_tokens = sizeof(struct iw_pmksa),
256 .max_tokens = sizeof(struct iw_pmksa),
257 },
258};
259static const unsigned int standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
260
261
262
263
264
265static const struct iw_ioctl_description standard_event[] = {
266 [IW_EVENT_IDX(IWEVTXDROP)] = {
267 .header_type = IW_HEADER_TYPE_ADDR,
268 },
269 [IW_EVENT_IDX(IWEVQUAL)] = {
270 .header_type = IW_HEADER_TYPE_QUAL,
271 },
272 [IW_EVENT_IDX(IWEVCUSTOM)] = {
273 .header_type = IW_HEADER_TYPE_POINT,
274 .token_size = 1,
275 .max_tokens = IW_CUSTOM_MAX,
276 },
277 [IW_EVENT_IDX(IWEVREGISTERED)] = {
278 .header_type = IW_HEADER_TYPE_ADDR,
279 },
280 [IW_EVENT_IDX(IWEVEXPIRED)] = {
281 .header_type = IW_HEADER_TYPE_ADDR,
282 },
283 [IW_EVENT_IDX(IWEVGENIE)] = {
284 .header_type = IW_HEADER_TYPE_POINT,
285 .token_size = 1,
286 .max_tokens = IW_GENERIC_IE_MAX,
287 },
288 [IW_EVENT_IDX(IWEVMICHAELMICFAILURE)] = {
289 .header_type = IW_HEADER_TYPE_POINT,
290 .token_size = 1,
291 .max_tokens = sizeof(struct iw_michaelmicfailure),
292 },
293 [IW_EVENT_IDX(IWEVASSOCREQIE)] = {
294 .header_type = IW_HEADER_TYPE_POINT,
295 .token_size = 1,
296 .max_tokens = IW_GENERIC_IE_MAX,
297 },
298 [IW_EVENT_IDX(IWEVASSOCRESPIE)] = {
299 .header_type = IW_HEADER_TYPE_POINT,
300 .token_size = 1,
301 .max_tokens = IW_GENERIC_IE_MAX,
302 },
303 [IW_EVENT_IDX(IWEVPMKIDCAND)] = {
304 .header_type = IW_HEADER_TYPE_POINT,
305 .token_size = 1,
306 .max_tokens = sizeof(struct iw_pmkid_cand),
307 },
308};
309static const unsigned int standard_event_num = ARRAY_SIZE(standard_event);
310
311
312static const int event_type_size[] = {
313 IW_EV_LCP_LEN,
314 0,
315 IW_EV_CHAR_LEN,
316 0,
317 IW_EV_UINT_LEN,
318 IW_EV_FREQ_LEN,
319 IW_EV_ADDR_LEN,
320 0,
321 IW_EV_POINT_LEN,
322 IW_EV_PARAM_LEN,
323 IW_EV_QUAL_LEN,
324};
325
326#ifdef CONFIG_COMPAT
327static const int compat_event_type_size[] = {
328 IW_EV_COMPAT_LCP_LEN,
329 0,
330 IW_EV_COMPAT_CHAR_LEN,
331 0,
332 IW_EV_COMPAT_UINT_LEN,
333 IW_EV_COMPAT_FREQ_LEN,
334 IW_EV_COMPAT_ADDR_LEN,
335 0,
336 IW_EV_COMPAT_POINT_LEN,
337 IW_EV_COMPAT_PARAM_LEN,
338 IW_EV_COMPAT_QUAL_LEN,
339};
340#endif
341
342
343
344
345void wireless_nlevent_flush(void)
346{
347 struct sk_buff *skb;
348 struct net *net;
349
350 down_read(&net_rwsem);
351 for_each_net(net) {
352 while ((skb = skb_dequeue(&net->wext_nlevents)))
353 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
354 GFP_KERNEL);
355 }
356 up_read(&net_rwsem);
357}
358EXPORT_SYMBOL_GPL(wireless_nlevent_flush);
359
360static int wext_netdev_notifier_call(struct notifier_block *nb,
361 unsigned long state, void *ptr)
362{
363
364
365
366
367
368
369
370 wireless_nlevent_flush();
371
372 return NOTIFY_OK;
373}
374
375static struct notifier_block wext_netdev_notifier = {
376 .notifier_call = wext_netdev_notifier_call,
377};
378
379static int __net_init wext_pernet_init(struct net *net)
380{
381 skb_queue_head_init(&net->wext_nlevents);
382 return 0;
383}
384
385static void __net_exit wext_pernet_exit(struct net *net)
386{
387 skb_queue_purge(&net->wext_nlevents);
388}
389
390static struct pernet_operations wext_pernet_ops = {
391 .init = wext_pernet_init,
392 .exit = wext_pernet_exit,
393};
394
395static int __init wireless_nlevent_init(void)
396{
397 int err = register_pernet_subsys(&wext_pernet_ops);
398
399 if (err)
400 return err;
401
402 err = register_netdevice_notifier(&wext_netdev_notifier);
403 if (err)
404 unregister_pernet_subsys(&wext_pernet_ops);
405 return err;
406}
407
408subsys_initcall(wireless_nlevent_init);
409
410
411static void wireless_nlevent_process(struct work_struct *work)
412{
413 wireless_nlevent_flush();
414}
415
416static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
417
418static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
419 struct sk_buff *skb)
420{
421 struct ifinfomsg *r;
422 struct nlmsghdr *nlh;
423
424 nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
425 if (!nlh)
426 return NULL;
427
428 r = nlmsg_data(nlh);
429 r->ifi_family = AF_UNSPEC;
430 r->__ifi_pad = 0;
431 r->ifi_type = dev->type;
432 r->ifi_index = dev->ifindex;
433 r->ifi_flags = dev_get_flags(dev);
434 r->ifi_change = 0;
435
436 if (nla_put_string(skb, IFLA_IFNAME, dev->name))
437 goto nla_put_failure;
438
439 return nlh;
440 nla_put_failure:
441 nlmsg_cancel(skb, nlh);
442 return NULL;
443}
444
445
446
447
448
449
450
451void wireless_send_event(struct net_device * dev,
452 unsigned int cmd,
453 union iwreq_data * wrqu,
454 const char * extra)
455{
456 const struct iw_ioctl_description * descr = NULL;
457 int extra_len = 0;
458 struct iw_event *event;
459 int event_len;
460 int hdr_len;
461 int wrqu_off = 0;
462
463 unsigned int cmd_index;
464 struct sk_buff *skb;
465 struct nlmsghdr *nlh;
466 struct nlattr *nla;
467#ifdef CONFIG_COMPAT
468 struct __compat_iw_event *compat_event;
469 struct compat_iw_point compat_wrqu;
470 struct sk_buff *compskb;
471#endif
472
473
474
475
476
477
478
479
480
481 if (WARN_ON(cmd == SIOCGIWSCAN && extra))
482 extra = NULL;
483
484
485 if (cmd <= SIOCIWLAST) {
486 cmd_index = IW_IOCTL_IDX(cmd);
487 if (cmd_index < standard_ioctl_num)
488 descr = &(standard_ioctl[cmd_index]);
489 } else {
490 cmd_index = IW_EVENT_IDX(cmd);
491 if (cmd_index < standard_event_num)
492 descr = &(standard_event[cmd_index]);
493 }
494
495 if (descr == NULL) {
496
497
498
499
500
501
502
503 netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
504 cmd);
505 return;
506 }
507
508
509 if (descr->header_type == IW_HEADER_TYPE_POINT) {
510
511 if (wrqu->data.length > descr->max_tokens) {
512 netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too big (%d)\n",
513 cmd, wrqu->data.length);
514 return;
515 }
516 if (wrqu->data.length < descr->min_tokens) {
517 netdev_err(dev, "(WE) : Wireless Event (cmd=0x%04X) too small (%d)\n",
518 cmd, wrqu->data.length);
519 return;
520 }
521
522 if (extra != NULL)
523 extra_len = wrqu->data.length * descr->token_size;
524
525 wrqu_off = IW_EV_POINT_OFF;
526 }
527
528
529 hdr_len = event_type_size[descr->header_type];
530 event_len = hdr_len + extra_len;
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
556 if (!skb)
557 return;
558
559
560 nlh = rtnetlink_ifinfo_prep(dev, skb);
561 if (WARN_ON(!nlh)) {
562 kfree_skb(skb);
563 return;
564 }
565
566
567 nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
568 if (!nla) {
569 kfree_skb(skb);
570 return;
571 }
572 event = nla_data(nla);
573
574
575 memset(event, 0, hdr_len);
576 event->len = event_len;
577 event->cmd = cmd;
578 memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
579 if (extra_len)
580 memcpy(((char *) event) + hdr_len, extra, extra_len);
581
582 nlmsg_end(skb, nlh);
583#ifdef CONFIG_COMPAT
584 hdr_len = compat_event_type_size[descr->header_type];
585 event_len = hdr_len + extra_len;
586
587 compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
588 if (!compskb) {
589 kfree_skb(skb);
590 return;
591 }
592
593
594 nlh = rtnetlink_ifinfo_prep(dev, compskb);
595 if (WARN_ON(!nlh)) {
596 kfree_skb(skb);
597 kfree_skb(compskb);
598 return;
599 }
600
601
602 nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
603 if (!nla) {
604 kfree_skb(skb);
605 kfree_skb(compskb);
606 return;
607 }
608 compat_event = nla_data(nla);
609
610 compat_event->len = event_len;
611 compat_event->cmd = cmd;
612 if (descr->header_type == IW_HEADER_TYPE_POINT) {
613 compat_wrqu.length = wrqu->data.length;
614 compat_wrqu.flags = wrqu->data.flags;
615 memcpy(&compat_event->pointer,
616 ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
617 hdr_len - IW_EV_COMPAT_LCP_LEN);
618 if (extra_len)
619 memcpy(((char *) compat_event) + hdr_len,
620 extra, extra_len);
621 } else {
622
623 memcpy(&compat_event->pointer, wrqu,
624 hdr_len - IW_EV_COMPAT_LCP_LEN);
625 }
626
627 nlmsg_end(compskb, nlh);
628
629 skb_shinfo(skb)->frag_list = compskb;
630#endif
631 skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
632 schedule_work(&wireless_nlevent_work);
633}
634EXPORT_SYMBOL(wireless_send_event);
635
636
637
638
639
640struct iw_statistics *get_wireless_stats(struct net_device *dev)
641{
642#ifdef CONFIG_WIRELESS_EXT
643 if ((dev->wireless_handlers != NULL) &&
644 (dev->wireless_handlers->get_wireless_stats != NULL))
645 return dev->wireless_handlers->get_wireless_stats(dev);
646#endif
647
648#ifdef CONFIG_CFG80211_WEXT
649 if (dev->ieee80211_ptr &&
650 dev->ieee80211_ptr->wiphy &&
651 dev->ieee80211_ptr->wiphy->wext &&
652 dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
653 return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
654#endif
655
656
657 return NULL;
658}
659
660static int iw_handler_get_iwstats(struct net_device * dev,
661 struct iw_request_info * info,
662 union iwreq_data * wrqu,
663 char * extra)
664{
665
666 struct iw_statistics *stats;
667
668 stats = get_wireless_stats(dev);
669 if (stats) {
670
671 memcpy(extra, stats, sizeof(struct iw_statistics));
672 wrqu->data.length = sizeof(struct iw_statistics);
673
674
675 if (wrqu->data.flags != 0)
676 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
677 return 0;
678 } else
679 return -EOPNOTSUPP;
680}
681
682static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
683{
684
685 unsigned int index;
686 const struct iw_handler_def *handlers = NULL;
687
688#ifdef CONFIG_CFG80211_WEXT
689 if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
690 handlers = dev->ieee80211_ptr->wiphy->wext;
691#endif
692#ifdef CONFIG_WIRELESS_EXT
693 if (dev->wireless_handlers)
694 handlers = dev->wireless_handlers;
695#endif
696
697 if (!handlers)
698 return NULL;
699
700
701 index = IW_IOCTL_IDX(cmd);
702 if (index < handlers->num_standard)
703 return handlers->standard[index];
704
705#ifdef CONFIG_WEXT_PRIV
706
707 index = cmd - SIOCIWFIRSTPRIV;
708 if (index < handlers->num_private)
709 return handlers->private[index];
710#endif
711
712
713 return NULL;
714}
715
716static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
717 const struct iw_ioctl_description *descr,
718 iw_handler handler, struct net_device *dev,
719 struct iw_request_info *info)
720{
721 int err, extra_size, user_length = 0, essid_compat = 0;
722 char *extra;
723
724
725
726
727 extra_size = descr->max_tokens * descr->token_size;
728
729
730 switch (cmd) {
731 case SIOCSIWESSID:
732 case SIOCGIWESSID:
733 case SIOCSIWNICKN:
734 case SIOCGIWNICKN:
735 if (iwp->length == descr->max_tokens + 1)
736 essid_compat = 1;
737 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
738 char essid[IW_ESSID_MAX_SIZE + 1];
739 unsigned int len;
740 len = iwp->length * descr->token_size;
741
742 if (len > IW_ESSID_MAX_SIZE)
743 return -EFAULT;
744
745 err = copy_from_user(essid, iwp->pointer, len);
746 if (err)
747 return -EFAULT;
748
749 if (essid[iwp->length - 1] == '\0')
750 essid_compat = 1;
751 }
752 break;
753 default:
754 break;
755 }
756
757 iwp->length -= essid_compat;
758
759
760 if (IW_IS_SET(cmd)) {
761
762 if (!iwp->pointer && iwp->length != 0)
763 return -EFAULT;
764
765 if (iwp->length > descr->max_tokens)
766 return -E2BIG;
767 if (iwp->length < descr->min_tokens)
768 return -EINVAL;
769 } else {
770
771 if (!iwp->pointer)
772 return -EFAULT;
773
774 user_length = iwp->length;
775
776
777
778
779
780
781
782 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
783 (user_length > descr->max_tokens)) {
784
785
786
787
788 extra_size = user_length * descr->token_size;
789
790
791
792
793
794
795 }
796 }
797
798
799 extra = kzalloc(extra_size, GFP_KERNEL);
800 if (!extra)
801 return -ENOMEM;
802
803
804 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
805 if (copy_from_user(extra, iwp->pointer,
806 iwp->length *
807 descr->token_size)) {
808 err = -EFAULT;
809 goto out;
810 }
811
812 if (cmd == SIOCSIWENCODEEXT) {
813 struct iw_encode_ext *ee = (void *) extra;
814
815 if (iwp->length < sizeof(*ee) + ee->key_len) {
816 err = -EFAULT;
817 goto out;
818 }
819 }
820 }
821
822 if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
823
824
825
826
827
828
829
830
831
832
833
834
835 iwp->length = descr->max_tokens;
836 }
837
838 err = handler(dev, info, (union iwreq_data *) iwp, extra);
839
840 iwp->length += essid_compat;
841
842
843 if (!err && IW_IS_GET(cmd)) {
844
845 if (user_length < iwp->length) {
846 err = -E2BIG;
847 goto out;
848 }
849
850 if (copy_to_user(iwp->pointer, extra,
851 iwp->length *
852 descr->token_size)) {
853 err = -EFAULT;
854 goto out;
855 }
856 }
857
858
859 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
860 ((err == 0) || (err == -EIWCOMMIT))) {
861 union iwreq_data *data = (union iwreq_data *) iwp;
862
863 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
864
865
866
867 wireless_send_event(dev, cmd, data, NULL);
868 else
869 wireless_send_event(dev, cmd, data, extra);
870 }
871
872out:
873 kfree(extra);
874 return err;
875}
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895int call_commit_handler(struct net_device *dev)
896{
897#ifdef CONFIG_WIRELESS_EXT
898 if ((netif_running(dev)) &&
899 (dev->wireless_handlers->standard[0] != NULL))
900
901 return dev->wireless_handlers->standard[0](dev, NULL,
902 NULL, NULL);
903 else
904 return 0;
905#else
906
907 return 0;
908#endif
909}
910
911
912
913
914
915static int wireless_process_ioctl(struct net *net, struct iwreq *iwr,
916 unsigned int cmd,
917 struct iw_request_info *info,
918 wext_ioctl_func standard,
919 wext_ioctl_func private)
920{
921 struct net_device *dev;
922 iw_handler handler;
923
924
925
926
927
928 if ((dev = __dev_get_by_name(net, iwr->ifr_name)) == NULL)
929 return -ENODEV;
930
931
932
933
934 if (cmd == SIOCGIWSTATS)
935 return standard(dev, iwr, cmd, info,
936 &iw_handler_get_iwstats);
937
938#ifdef CONFIG_WEXT_PRIV
939 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
940 return standard(dev, iwr, cmd, info,
941 iw_handler_get_private);
942#endif
943
944
945 if (!netif_device_present(dev))
946 return -ENODEV;
947
948
949 handler = get_handler(dev, cmd);
950 if (handler) {
951
952 if (cmd < SIOCIWFIRSTPRIV)
953 return standard(dev, iwr, cmd, info, handler);
954 else if (private)
955 return private(dev, iwr, cmd, info, handler);
956 }
957 return -EOPNOTSUPP;
958}
959
960
961
962
963static int wext_permission_check(unsigned int cmd)
964{
965 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
966 cmd == SIOCGIWENCODEEXT) &&
967 !capable(CAP_NET_ADMIN))
968 return -EPERM;
969
970 return 0;
971}
972
973
974static int wext_ioctl_dispatch(struct net *net, struct iwreq *iwr,
975 unsigned int cmd, struct iw_request_info *info,
976 wext_ioctl_func standard,
977 wext_ioctl_func private)
978{
979 int ret = wext_permission_check(cmd);
980
981 if (ret)
982 return ret;
983
984 dev_load(net, iwr->ifr_name);
985 rtnl_lock();
986 ret = wireless_process_ioctl(net, iwr, cmd, info, standard, private);
987 rtnl_unlock();
988
989 return ret;
990}
991
992
993
994
995
996
997static int ioctl_standard_call(struct net_device * dev,
998 struct iwreq *iwr,
999 unsigned int cmd,
1000 struct iw_request_info *info,
1001 iw_handler handler)
1002{
1003 const struct iw_ioctl_description * descr;
1004 int ret = -EINVAL;
1005
1006
1007 if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
1008 return -EOPNOTSUPP;
1009 descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
1010
1011
1012 if (descr->header_type != IW_HEADER_TYPE_POINT) {
1013
1014
1015 ret = handler(dev, info, &(iwr->u), NULL);
1016
1017
1018 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
1019 ((ret == 0) || (ret == -EIWCOMMIT)))
1020 wireless_send_event(dev, cmd, &(iwr->u), NULL);
1021 } else {
1022 ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
1023 handler, dev, info);
1024 }
1025
1026
1027 if (ret == -EIWCOMMIT)
1028 ret = call_commit_handler(dev);
1029
1030
1031
1032 return ret;
1033}
1034
1035
1036int wext_handle_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1037{
1038 struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1039 struct iwreq iwr;
1040 int ret;
1041
1042 if (copy_from_user(&iwr, arg, sizeof(iwr)))
1043 return -EFAULT;
1044
1045 iwr.ifr_name[sizeof(iwr.ifr_name) - 1] = 0;
1046
1047 ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
1048 ioctl_standard_call,
1049 ioctl_private_call);
1050 if (ret >= 0 &&
1051 IW_IS_GET(cmd) &&
1052 copy_to_user(arg, &iwr, sizeof(struct iwreq)))
1053 return -EFAULT;
1054
1055 return ret;
1056}
1057
1058#ifdef CONFIG_COMPAT
1059static int compat_standard_call(struct net_device *dev,
1060 struct iwreq *iwr,
1061 unsigned int cmd,
1062 struct iw_request_info *info,
1063 iw_handler handler)
1064{
1065 const struct iw_ioctl_description *descr;
1066 struct compat_iw_point *iwp_compat;
1067 struct iw_point iwp;
1068 int err;
1069
1070 descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1071
1072 if (descr->header_type != IW_HEADER_TYPE_POINT)
1073 return ioctl_standard_call(dev, iwr, cmd, info, handler);
1074
1075 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1076 iwp.pointer = compat_ptr(iwp_compat->pointer);
1077 iwp.length = iwp_compat->length;
1078 iwp.flags = iwp_compat->flags;
1079
1080 err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1081
1082 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1083 iwp_compat->length = iwp.length;
1084 iwp_compat->flags = iwp.flags;
1085
1086 return err;
1087}
1088
1089int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1090 unsigned long arg)
1091{
1092 void __user *argp = (void __user *)arg;
1093 struct iw_request_info info;
1094 struct iwreq iwr;
1095 char *colon;
1096 int ret;
1097
1098 if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1099 return -EFAULT;
1100
1101 iwr.ifr_name[IFNAMSIZ-1] = 0;
1102 colon = strchr(iwr.ifr_name, ':');
1103 if (colon)
1104 *colon = 0;
1105
1106 info.cmd = cmd;
1107 info.flags = IW_REQUEST_FLAG_COMPAT;
1108
1109 ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
1110 compat_standard_call,
1111 compat_private_call);
1112
1113 if (ret >= 0 &&
1114 IW_IS_GET(cmd) &&
1115 copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1116 return -EFAULT;
1117
1118 return ret;
1119}
1120#endif
1121
1122char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
1123 char *ends, struct iw_event *iwe, int event_len)
1124{
1125 int lcp_len = iwe_stream_lcp_len(info);
1126
1127 event_len = iwe_stream_event_len_adjust(info, event_len);
1128
1129
1130 if (likely((stream + event_len) < ends)) {
1131 iwe->len = event_len;
1132
1133 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1134 memcpy(stream + lcp_len, &iwe->u,
1135 event_len - lcp_len);
1136 stream += event_len;
1137 }
1138
1139 return stream;
1140}
1141EXPORT_SYMBOL(iwe_stream_add_event);
1142
1143char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
1144 char *ends, struct iw_event *iwe, char *extra)
1145{
1146 int event_len = iwe_stream_point_len(info) + iwe->u.data.length;
1147 int point_len = iwe_stream_point_len(info);
1148 int lcp_len = iwe_stream_lcp_len(info);
1149
1150
1151 if (likely((stream + event_len) < ends)) {
1152 iwe->len = event_len;
1153 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1154 memcpy(stream + lcp_len,
1155 ((char *) &iwe->u) + IW_EV_POINT_OFF,
1156 IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1157 if (iwe->u.data.length && extra)
1158 memcpy(stream + point_len, extra, iwe->u.data.length);
1159 stream += event_len;
1160 }
1161
1162 return stream;
1163}
1164EXPORT_SYMBOL(iwe_stream_add_point);
1165
1166char *iwe_stream_add_value(struct iw_request_info *info, char *event,
1167 char *value, char *ends, struct iw_event *iwe,
1168 int event_len)
1169{
1170 int lcp_len = iwe_stream_lcp_len(info);
1171
1172
1173 event_len -= IW_EV_LCP_LEN;
1174
1175
1176 if (likely((value + event_len) < ends)) {
1177
1178 memcpy(value, &iwe->u, event_len);
1179 value += event_len;
1180
1181 iwe->len = value - event;
1182 memcpy(event, (char *) iwe, lcp_len);
1183 }
1184
1185 return value;
1186}
1187EXPORT_SYMBOL(iwe_stream_add_value);
1188