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 ASSERT_RTNL();
351
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}
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 rtnl_lock();
414 wireless_nlevent_flush();
415 rtnl_unlock();
416}
417
418static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
419
420static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
421 struct sk_buff *skb)
422{
423 struct ifinfomsg *r;
424 struct nlmsghdr *nlh;
425
426 nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
427 if (!nlh)
428 return NULL;
429
430 r = nlmsg_data(nlh);
431 r->ifi_family = AF_UNSPEC;
432 r->__ifi_pad = 0;
433 r->ifi_type = dev->type;
434 r->ifi_index = dev->ifindex;
435 r->ifi_flags = dev_get_flags(dev);
436 r->ifi_change = 0;
437
438 if (nla_put_string(skb, IFLA_IFNAME, dev->name))
439 goto nla_put_failure;
440
441 return nlh;
442 nla_put_failure:
443 nlmsg_cancel(skb, nlh);
444 return NULL;
445}
446
447
448
449
450
451
452
453void wireless_send_event(struct net_device * dev,
454 unsigned int cmd,
455 union iwreq_data * wrqu,
456 const char * extra)
457{
458 const struct iw_ioctl_description * descr = NULL;
459 int extra_len = 0;
460 struct iw_event *event;
461 int event_len;
462 int hdr_len;
463 int wrqu_off = 0;
464
465 unsigned int cmd_index;
466 struct sk_buff *skb;
467 struct nlmsghdr *nlh;
468 struct nlattr *nla;
469#ifdef CONFIG_COMPAT
470 struct __compat_iw_event *compat_event;
471 struct compat_iw_point compat_wrqu;
472 struct sk_buff *compskb;
473#endif
474
475
476
477
478
479
480
481
482
483 if (WARN_ON(cmd == SIOCGIWSCAN && extra))
484 extra = NULL;
485
486
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
497 if (descr == NULL) {
498
499
500
501
502
503
504
505 netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
506 cmd);
507 return;
508 }
509
510
511 if (descr->header_type == IW_HEADER_TYPE_POINT) {
512
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
524 if (extra != NULL)
525 extra_len = wrqu->data.length * descr->token_size;
526
527 wrqu_off = IW_EV_POINT_OFF;
528 }
529
530
531 hdr_len = event_type_size[descr->header_type];
532 event_len = hdr_len + extra_len;
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
558 if (!skb)
559 return;
560
561
562 nlh = rtnetlink_ifinfo_prep(dev, skb);
563 if (WARN_ON(!nlh)) {
564 kfree_skb(skb);
565 return;
566 }
567
568
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
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 event_len = hdr_len + extra_len;
588
589 compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
590 if (!compskb) {
591 kfree_skb(skb);
592 return;
593 }
594
595
596 nlh = rtnetlink_ifinfo_prep(dev, compskb);
597 if (WARN_ON(!nlh)) {
598 kfree_skb(skb);
599 kfree_skb(compskb);
600 return;
601 }
602
603
604 nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
605 if (!nla) {
606 kfree_skb(skb);
607 kfree_skb(compskb);
608 return;
609 }
610 compat_event = nla_data(nla);
611
612 compat_event->len = event_len;
613 compat_event->cmd = cmd;
614 if (descr->header_type == IW_HEADER_TYPE_POINT) {
615 compat_wrqu.length = wrqu->data.length;
616 compat_wrqu.flags = wrqu->data.flags;
617 memcpy(&compat_event->pointer,
618 ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
619 hdr_len - IW_EV_COMPAT_LCP_LEN);
620 if (extra_len)
621 memcpy(((char *) compat_event) + hdr_len,
622 extra, extra_len);
623 } else {
624
625 memcpy(&compat_event->pointer, wrqu,
626 hdr_len - IW_EV_COMPAT_LCP_LEN);
627 }
628
629 nlmsg_end(compskb, nlh);
630
631 skb_shinfo(skb)->frag_list = compskb;
632#endif
633 skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
634 schedule_work(&wireless_nlevent_work);
635}
636EXPORT_SYMBOL(wireless_send_event);
637
638
639
640
641
642struct iw_statistics *get_wireless_stats(struct net_device *dev)
643{
644#ifdef CONFIG_WIRELESS_EXT
645 if ((dev->wireless_handlers != NULL) &&
646 (dev->wireless_handlers->get_wireless_stats != NULL))
647 return dev->wireless_handlers->get_wireless_stats(dev);
648#endif
649
650#ifdef CONFIG_CFG80211_WEXT
651 if (dev->ieee80211_ptr &&
652 dev->ieee80211_ptr->wiphy &&
653 dev->ieee80211_ptr->wiphy->wext &&
654 dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
655 return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
656#endif
657
658
659 return NULL;
660}
661
662static int iw_handler_get_iwstats(struct net_device * dev,
663 struct iw_request_info * info,
664 union iwreq_data * wrqu,
665 char * extra)
666{
667
668 struct iw_statistics *stats;
669
670 stats = get_wireless_stats(dev);
671 if (stats) {
672
673 memcpy(extra, stats, sizeof(struct iw_statistics));
674 wrqu->data.length = sizeof(struct iw_statistics);
675
676
677 if (wrqu->data.flags != 0)
678 stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
679 return 0;
680 } else
681 return -EOPNOTSUPP;
682}
683
684static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
685{
686
687 unsigned int index;
688 const struct iw_handler_def *handlers = NULL;
689
690#ifdef CONFIG_CFG80211_WEXT
691 if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
692 handlers = dev->ieee80211_ptr->wiphy->wext;
693#endif
694#ifdef CONFIG_WIRELESS_EXT
695 if (dev->wireless_handlers)
696 handlers = dev->wireless_handlers;
697#endif
698
699 if (!handlers)
700 return NULL;
701
702
703 index = IW_IOCTL_IDX(cmd);
704 if (index < handlers->num_standard)
705 return handlers->standard[index];
706
707#ifdef CONFIG_WEXT_PRIV
708
709 index = cmd - SIOCIWFIRSTPRIV;
710 if (index < handlers->num_private)
711 return handlers->private[index];
712#endif
713
714
715 return NULL;
716}
717
718static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
719 const struct iw_ioctl_description *descr,
720 iw_handler handler, struct net_device *dev,
721 struct iw_request_info *info)
722{
723 int err, extra_size, user_length = 0, essid_compat = 0;
724 char *extra;
725
726
727
728
729 extra_size = descr->max_tokens * descr->token_size;
730
731
732 switch (cmd) {
733 case SIOCSIWESSID:
734 case SIOCGIWESSID:
735 case SIOCSIWNICKN:
736 case SIOCGIWNICKN:
737 if (iwp->length == descr->max_tokens + 1)
738 essid_compat = 1;
739 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
740 char essid[IW_ESSID_MAX_SIZE + 1];
741 unsigned int len;
742 len = iwp->length * descr->token_size;
743
744 if (len > IW_ESSID_MAX_SIZE)
745 return -EFAULT;
746
747 err = copy_from_user(essid, iwp->pointer, len);
748 if (err)
749 return -EFAULT;
750
751 if (essid[iwp->length - 1] == '\0')
752 essid_compat = 1;
753 }
754 break;
755 default:
756 break;
757 }
758
759 iwp->length -= essid_compat;
760
761
762 if (IW_IS_SET(cmd)) {
763
764 if (!iwp->pointer && iwp->length != 0)
765 return -EFAULT;
766
767 if (iwp->length > descr->max_tokens)
768 return -E2BIG;
769 if (iwp->length < descr->min_tokens)
770 return -EINVAL;
771 } else {
772
773 if (!iwp->pointer)
774 return -EFAULT;
775
776 user_length = iwp->length;
777
778
779
780
781
782
783
784 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
785 (user_length > descr->max_tokens)) {
786
787
788
789
790 extra_size = user_length * descr->token_size;
791
792
793
794
795
796
797 }
798 }
799
800
801 extra = kzalloc(extra_size, GFP_KERNEL);
802 if (!extra)
803 return -ENOMEM;
804
805
806 if (IW_IS_SET(cmd) && (iwp->length != 0)) {
807 if (copy_from_user(extra, iwp->pointer,
808 iwp->length *
809 descr->token_size)) {
810 err = -EFAULT;
811 goto out;
812 }
813
814 if (cmd == SIOCSIWENCODEEXT) {
815 struct iw_encode_ext *ee = (void *) extra;
816
817 if (iwp->length < sizeof(*ee) + ee->key_len) {
818 err = -EFAULT;
819 goto out;
820 }
821 }
822 }
823
824 if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
825
826
827
828
829
830
831
832
833
834
835
836
837 iwp->length = descr->max_tokens;
838 }
839
840 err = handler(dev, info, (union iwreq_data *) iwp, extra);
841
842 iwp->length += essid_compat;
843
844
845 if (!err && IW_IS_GET(cmd)) {
846
847 if (user_length < iwp->length) {
848 err = -E2BIG;
849 goto out;
850 }
851
852 if (copy_to_user(iwp->pointer, extra,
853 iwp->length *
854 descr->token_size)) {
855 err = -EFAULT;
856 goto out;
857 }
858 }
859
860
861 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
862 ((err == 0) || (err == -EIWCOMMIT))) {
863 union iwreq_data *data = (union iwreq_data *) iwp;
864
865 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
866
867
868
869 wireless_send_event(dev, cmd, data, NULL);
870 else
871 wireless_send_event(dev, cmd, data, extra);
872 }
873
874out:
875 kfree(extra);
876 return err;
877}
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897int call_commit_handler(struct net_device *dev)
898{
899#ifdef CONFIG_WIRELESS_EXT
900 if ((netif_running(dev)) &&
901 (dev->wireless_handlers->standard[0] != NULL))
902
903 return dev->wireless_handlers->standard[0](dev, NULL,
904 NULL, NULL);
905 else
906 return 0;
907#else
908
909 return 0;
910#endif
911}
912
913
914
915
916
917static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
918 unsigned int cmd,
919 struct iw_request_info *info,
920 wext_ioctl_func standard,
921 wext_ioctl_func private)
922{
923 struct iwreq *iwr = (struct iwreq *) ifr;
924 struct net_device *dev;
925 iw_handler handler;
926
927
928
929
930
931 if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
932 return -ENODEV;
933
934
935
936
937 if (cmd == SIOCGIWSTATS)
938 return standard(dev, iwr, cmd, info,
939 &iw_handler_get_iwstats);
940
941#ifdef CONFIG_WEXT_PRIV
942 if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
943 return standard(dev, iwr, cmd, info,
944 iw_handler_get_private);
945#endif
946
947
948 if (!netif_device_present(dev))
949 return -ENODEV;
950
951
952 handler = get_handler(dev, cmd);
953 if (handler) {
954
955 if (cmd < SIOCIWFIRSTPRIV)
956 return standard(dev, iwr, cmd, info, handler);
957 else if (private)
958 return private(dev, iwr, cmd, info, handler);
959 }
960
961 if (dev->netdev_ops->ndo_do_ioctl)
962 return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
963 return -EOPNOTSUPP;
964}
965
966
967
968
969static int wext_permission_check(unsigned int cmd)
970{
971 if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
972 cmd == SIOCGIWENCODEEXT) &&
973 !capable(CAP_NET_ADMIN))
974 return -EPERM;
975
976 return 0;
977}
978
979
980static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
981 unsigned int cmd, struct iw_request_info *info,
982 wext_ioctl_func standard,
983 wext_ioctl_func private)
984{
985 int ret = wext_permission_check(cmd);
986
987 if (ret)
988 return ret;
989
990 dev_load(net, ifr->ifr_name);
991 rtnl_lock();
992 ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
993 rtnl_unlock();
994
995 return ret;
996}
997
998
999
1000
1001
1002
1003static int ioctl_standard_call(struct net_device * dev,
1004 struct iwreq *iwr,
1005 unsigned int cmd,
1006 struct iw_request_info *info,
1007 iw_handler handler)
1008{
1009 const struct iw_ioctl_description * descr;
1010 int ret = -EINVAL;
1011
1012
1013 if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
1014 return -EOPNOTSUPP;
1015 descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
1016
1017
1018 if (descr->header_type != IW_HEADER_TYPE_POINT) {
1019
1020
1021 ret = handler(dev, info, &(iwr->u), NULL);
1022
1023
1024 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
1025 ((ret == 0) || (ret == -EIWCOMMIT)))
1026 wireless_send_event(dev, cmd, &(iwr->u), NULL);
1027 } else {
1028 ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
1029 handler, dev, info);
1030 }
1031
1032
1033 if (ret == -EIWCOMMIT)
1034 ret = call_commit_handler(dev);
1035
1036
1037
1038 return ret;
1039}
1040
1041
1042int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1043 void __user *arg)
1044{
1045 struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1046 int ret;
1047
1048 ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
1049 ioctl_standard_call,
1050 ioctl_private_call);
1051 if (ret >= 0 &&
1052 IW_IS_GET(cmd) &&
1053 copy_to_user(arg, ifr, sizeof(struct iwreq)))
1054 return -EFAULT;
1055
1056 return ret;
1057}
1058
1059#ifdef CONFIG_COMPAT
1060static int compat_standard_call(struct net_device *dev,
1061 struct iwreq *iwr,
1062 unsigned int cmd,
1063 struct iw_request_info *info,
1064 iw_handler handler)
1065{
1066 const struct iw_ioctl_description *descr;
1067 struct compat_iw_point *iwp_compat;
1068 struct iw_point iwp;
1069 int err;
1070
1071 descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1072
1073 if (descr->header_type != IW_HEADER_TYPE_POINT)
1074 return ioctl_standard_call(dev, iwr, cmd, info, handler);
1075
1076 iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1077 iwp.pointer = compat_ptr(iwp_compat->pointer);
1078 iwp.length = iwp_compat->length;
1079 iwp.flags = iwp_compat->flags;
1080
1081 err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1082
1083 iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1084 iwp_compat->length = iwp.length;
1085 iwp_compat->flags = iwp.flags;
1086
1087 return err;
1088}
1089
1090int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1091 unsigned long arg)
1092{
1093 void __user *argp = (void __user *)arg;
1094 struct iw_request_info info;
1095 struct iwreq iwr;
1096 char *colon;
1097 int ret;
1098
1099 if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1100 return -EFAULT;
1101
1102 iwr.ifr_name[IFNAMSIZ-1] = 0;
1103 colon = strchr(iwr.ifr_name, ':');
1104 if (colon)
1105 *colon = 0;
1106
1107 info.cmd = cmd;
1108 info.flags = IW_REQUEST_FLAG_COMPAT;
1109
1110 ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
1111 compat_standard_call,
1112 compat_private_call);
1113
1114 if (ret >= 0 &&
1115 IW_IS_GET(cmd) &&
1116 copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1117 return -EFAULT;
1118
1119 return ret;
1120}
1121#endif
1122
1123char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
1124 char *ends, struct iw_event *iwe, int event_len)
1125{
1126 int lcp_len = iwe_stream_lcp_len(info);
1127
1128 event_len = iwe_stream_event_len_adjust(info, event_len);
1129
1130
1131 if (likely((stream + event_len) < ends)) {
1132 iwe->len = event_len;
1133
1134 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1135 memcpy(stream + lcp_len, &iwe->u,
1136 event_len - lcp_len);
1137 stream += event_len;
1138 }
1139
1140 return stream;
1141}
1142EXPORT_SYMBOL(iwe_stream_add_event);
1143
1144char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
1145 char *ends, struct iw_event *iwe, char *extra)
1146{
1147 int event_len = iwe_stream_point_len(info) + iwe->u.data.length;
1148 int point_len = iwe_stream_point_len(info);
1149 int lcp_len = iwe_stream_lcp_len(info);
1150
1151
1152 if (likely((stream + event_len) < ends)) {
1153 iwe->len = event_len;
1154 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1155 memcpy(stream + lcp_len,
1156 ((char *) &iwe->u) + IW_EV_POINT_OFF,
1157 IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1158 if (iwe->u.data.length && extra)
1159 memcpy(stream + point_len, extra, iwe->u.data.length);
1160 stream += event_len;
1161 }
1162
1163 return stream;
1164}
1165EXPORT_SYMBOL(iwe_stream_add_point);
1166
1167char *iwe_stream_add_value(struct iw_request_info *info, char *event,
1168 char *value, char *ends, struct iw_event *iwe,
1169 int event_len)
1170{
1171 int lcp_len = iwe_stream_lcp_len(info);
1172
1173
1174 event_len -= IW_EV_LCP_LEN;
1175
1176
1177 if (likely((value + event_len) < ends)) {
1178
1179 memcpy(value, &iwe->u, event_len);
1180 value += event_len;
1181
1182 iwe->len = value - event;
1183 memcpy(event, (char *) iwe, lcp_len);
1184 }
1185
1186 return value;
1187}
1188EXPORT_SYMBOL(iwe_stream_add_value);
1189