GNU Linux-libre 4.14.257-gnu1
[releases.git] / net / wireless / wext-core.c
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 <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
23 typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
24                                unsigned int, struct iw_request_info *,
25                                iw_handler);
26
27
28 /*
29  * Meta-data about all the standard Wireless Extension request we
30  * know about.
31  */
32 static 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)] = { /* (handled directly by us) */
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)] = { /* (handled directly by us) */
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 };
259 static const unsigned int standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
260
261 /*
262  * Meta-data about all the additional standard Wireless Extension events
263  * we know about.
264  */
265 static 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 };
309 static const unsigned int standard_event_num = ARRAY_SIZE(standard_event);
310
311 /* Size (in bytes) of various events */
312 static const int event_type_size[] = {
313         IW_EV_LCP_LEN,                  /* IW_HEADER_TYPE_NULL */
314         0,
315         IW_EV_CHAR_LEN,                 /* IW_HEADER_TYPE_CHAR */
316         0,
317         IW_EV_UINT_LEN,                 /* IW_HEADER_TYPE_UINT */
318         IW_EV_FREQ_LEN,                 /* IW_HEADER_TYPE_FREQ */
319         IW_EV_ADDR_LEN,                 /* IW_HEADER_TYPE_ADDR */
320         0,
321         IW_EV_POINT_LEN,                /* Without variable payload */
322         IW_EV_PARAM_LEN,                /* IW_HEADER_TYPE_PARAM */
323         IW_EV_QUAL_LEN,                 /* IW_HEADER_TYPE_QUAL */
324 };
325
326 #ifdef CONFIG_COMPAT
327 static const int compat_event_type_size[] = {
328         IW_EV_COMPAT_LCP_LEN,           /* IW_HEADER_TYPE_NULL */
329         0,
330         IW_EV_COMPAT_CHAR_LEN,          /* IW_HEADER_TYPE_CHAR */
331         0,
332         IW_EV_COMPAT_UINT_LEN,          /* IW_HEADER_TYPE_UINT */
333         IW_EV_COMPAT_FREQ_LEN,          /* IW_HEADER_TYPE_FREQ */
334         IW_EV_COMPAT_ADDR_LEN,          /* IW_HEADER_TYPE_ADDR */
335         0,
336         IW_EV_COMPAT_POINT_LEN,         /* Without variable payload */
337         IW_EV_COMPAT_PARAM_LEN,         /* IW_HEADER_TYPE_PARAM */
338         IW_EV_COMPAT_QUAL_LEN,          /* IW_HEADER_TYPE_QUAL */
339 };
340 #endif
341
342
343 /* IW event code */
344
345 void 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 }
358 EXPORT_SYMBOL_GPL(wireless_nlevent_flush);
359
360 static int wext_netdev_notifier_call(struct notifier_block *nb,
361                                      unsigned long state, void *ptr)
362 {
363         /*
364          * When a netdev changes state in any way, flush all pending messages
365          * to avoid them going out in a strange order, e.g. RTM_NEWLINK after
366          * RTM_DELLINK, or with IFF_UP after without IFF_UP during dev_close()
367          * or similar - all of which could otherwise happen due to delays from
368          * schedule_work().
369          */
370         wireless_nlevent_flush();
371
372         return NOTIFY_OK;
373 }
374
375 static struct notifier_block wext_netdev_notifier = {
376         .notifier_call = wext_netdev_notifier_call,
377 };
378
379 static int __net_init wext_pernet_init(struct net *net)
380 {
381         skb_queue_head_init(&net->wext_nlevents);
382         return 0;
383 }
384
385 static void __net_exit wext_pernet_exit(struct net *net)
386 {
387         skb_queue_purge(&net->wext_nlevents);
388 }
389
390 static struct pernet_operations wext_pernet_ops = {
391         .init = wext_pernet_init,
392         .exit = wext_pernet_exit,
393 };
394
395 static 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
408 subsys_initcall(wireless_nlevent_init);
409
410 /* Process events generated by the wireless layer or the driver. */
411 static void wireless_nlevent_process(struct work_struct *work)
412 {
413         rtnl_lock();
414         wireless_nlevent_flush();
415         rtnl_unlock();
416 }
417
418 static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
419
420 static 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;      /* Wireless changes don't affect those flags */
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  * Main event dispatcher. Called from other parts and drivers.
450  * Send the event on the appropriate channels.
451  * May be called from interrupt context.
452  */
453 void 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;                /* Mallocated whole event */
461         int event_len;                          /* Its size */
462         int hdr_len;                            /* Size of the event header */
463         int wrqu_off = 0;                       /* Offset in wrqu */
464         /* Don't "optimise" the following variable, it will crash */
465         unsigned int    cmd_index;              /* *MUST* be unsigned */
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          * 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         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         /* Send via the RtNetlink event channel */
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         /* Add the wireless events in the netlink packet */
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                 /* extra_len must be zero, so no if (extra) needed */
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 }
636 EXPORT_SYMBOL(wireless_send_event);
637
638
639
640 /* IW handlers */
641
642 struct 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         /* not found */
659         return NULL;
660 }
661
662 /* noinline to avoid a bogus warning with -O3 */
663 static noinline int iw_handler_get_iwstats(struct net_device *  dev,
664                                   struct iw_request_info *      info,
665                                   union iwreq_data *            wrqu,
666                                   char *                        extra)
667 {
668         /* Get stats from the driver */
669         struct iw_statistics *stats;
670
671         stats = get_wireless_stats(dev);
672         if (stats) {
673                 /* Copy statistics to extra */
674                 memcpy(extra, stats, sizeof(struct iw_statistics));
675                 wrqu->data.length = sizeof(struct iw_statistics);
676
677                 /* Check if we need to clear the updated flag */
678                 if (wrqu->data.flags != 0)
679                         stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
680                 return 0;
681         } else
682                 return -EOPNOTSUPP;
683 }
684
685 static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
686 {
687         /* Don't "optimise" the following variable, it will crash */
688         unsigned int    index;          /* *MUST* be unsigned */
689         const struct iw_handler_def *handlers = NULL;
690
691 #ifdef CONFIG_CFG80211_WEXT
692         if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
693                 handlers = dev->ieee80211_ptr->wiphy->wext;
694 #endif
695 #ifdef CONFIG_WIRELESS_EXT
696         if (dev->wireless_handlers)
697                 handlers = dev->wireless_handlers;
698 #endif
699
700         if (!handlers)
701                 return NULL;
702
703         /* Try as a standard command */
704         index = IW_IOCTL_IDX(cmd);
705         if (index < handlers->num_standard)
706                 return handlers->standard[index];
707
708 #ifdef CONFIG_WEXT_PRIV
709         /* Try as a private command */
710         index = cmd - SIOCIWFIRSTPRIV;
711         if (index < handlers->num_private)
712                 return handlers->private[index];
713 #endif
714
715         /* Not found */
716         return NULL;
717 }
718
719 static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
720                                    const struct iw_ioctl_description *descr,
721                                    iw_handler handler, struct net_device *dev,
722                                    struct iw_request_info *info)
723 {
724         int err, extra_size, user_length = 0, essid_compat = 0;
725         char *extra;
726
727         /* Calculate space needed by arguments. Always allocate
728          * for max space.
729          */
730         extra_size = descr->max_tokens * descr->token_size;
731
732         /* Check need for ESSID compatibility for WE < 21 */
733         switch (cmd) {
734         case SIOCSIWESSID:
735         case SIOCGIWESSID:
736         case SIOCSIWNICKN:
737         case SIOCGIWNICKN:
738                 if (iwp->length == descr->max_tokens + 1)
739                         essid_compat = 1;
740                 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
741                         char essid[IW_ESSID_MAX_SIZE + 1];
742                         unsigned int len;
743                         len = iwp->length * descr->token_size;
744
745                         if (len > IW_ESSID_MAX_SIZE)
746                                 return -EFAULT;
747
748                         err = copy_from_user(essid, iwp->pointer, len);
749                         if (err)
750                                 return -EFAULT;
751
752                         if (essid[iwp->length - 1] == '\0')
753                                 essid_compat = 1;
754                 }
755                 break;
756         default:
757                 break;
758         }
759
760         iwp->length -= essid_compat;
761
762         /* Check what user space is giving us */
763         if (IW_IS_SET(cmd)) {
764                 /* Check NULL pointer */
765                 if (!iwp->pointer && iwp->length != 0)
766                         return -EFAULT;
767                 /* Check if number of token fits within bounds */
768                 if (iwp->length > descr->max_tokens)
769                         return -E2BIG;
770                 if (iwp->length < descr->min_tokens)
771                         return -EINVAL;
772         } else {
773                 /* Check NULL pointer */
774                 if (!iwp->pointer)
775                         return -EFAULT;
776                 /* Save user space buffer size for checking */
777                 user_length = iwp->length;
778
779                 /* Don't check if user_length > max to allow forward
780                  * compatibility. The test user_length < min is
781                  * implied by the test at the end.
782                  */
783
784                 /* Support for very large requests */
785                 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
786                     (user_length > descr->max_tokens)) {
787                         /* Allow userspace to GET more than max so
788                          * we can support any size GET requests.
789                          * There is still a limit : -ENOMEM.
790                          */
791                         extra_size = user_length * descr->token_size;
792
793                         /* Note : user_length is originally a __u16,
794                          * and token_size is controlled by us,
795                          * so extra_size won't get negative and
796                          * won't overflow...
797                          */
798                 }
799         }
800
801         /* kzalloc() ensures NULL-termination for essid_compat. */
802         extra = kzalloc(extra_size, GFP_KERNEL);
803         if (!extra)
804                 return -ENOMEM;
805
806         /* If it is a SET, get all the extra data in here */
807         if (IW_IS_SET(cmd) && (iwp->length != 0)) {
808                 if (copy_from_user(extra, iwp->pointer,
809                                    iwp->length *
810                                    descr->token_size)) {
811                         err = -EFAULT;
812                         goto out;
813                 }
814
815                 if (cmd == SIOCSIWENCODEEXT) {
816                         struct iw_encode_ext *ee = (void *) extra;
817
818                         if (iwp->length < sizeof(*ee) + ee->key_len) {
819                                 err = -EFAULT;
820                                 goto out;
821                         }
822                 }
823         }
824
825         if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
826                 /*
827                  * If this is a GET, but not NOMAX, it means that the extra
828                  * data is not bounded by userspace, but by max_tokens. Thus
829                  * set the length to max_tokens. This matches the extra data
830                  * allocation.
831                  * The driver should fill it with the number of tokens it
832                  * provided, and it may check iwp->length rather than having
833                  * knowledge of max_tokens. If the driver doesn't change the
834                  * iwp->length, this ioctl just copies back max_token tokens
835                  * filled with zeroes. Hopefully the driver isn't claiming
836                  * them to be valid data.
837                  */
838                 iwp->length = descr->max_tokens;
839         }
840
841         err = handler(dev, info, (union iwreq_data *) iwp, extra);
842
843         iwp->length += essid_compat;
844
845         /* If we have something to return to the user */
846         if (!err && IW_IS_GET(cmd)) {
847                 /* Check if there is enough buffer up there */
848                 if (user_length < iwp->length) {
849                         err = -E2BIG;
850                         goto out;
851                 }
852
853                 if (copy_to_user(iwp->pointer, extra,
854                                  iwp->length *
855                                  descr->token_size)) {
856                         err = -EFAULT;
857                         goto out;
858                 }
859         }
860
861         /* Generate an event to notify listeners of the change */
862         if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
863             ((err == 0) || (err == -EIWCOMMIT))) {
864                 union iwreq_data *data = (union iwreq_data *) iwp;
865
866                 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
867                         /* If the event is restricted, don't
868                          * export the payload.
869                          */
870                         wireless_send_event(dev, cmd, data, NULL);
871                 else
872                         wireless_send_event(dev, cmd, data, extra);
873         }
874
875 out:
876         kfree(extra);
877         return err;
878 }
879
880 /*
881  * Call the commit handler in the driver
882  * (if exist and if conditions are right)
883  *
884  * Note : our current commit strategy is currently pretty dumb,
885  * but we will be able to improve on that...
886  * The goal is to try to agreagate as many changes as possible
887  * before doing the commit. Drivers that will define a commit handler
888  * are usually those that need a reset after changing parameters, so
889  * we want to minimise the number of reset.
890  * A cool idea is to use a timer : at each "set" command, we re-set the
891  * timer, when the timer eventually fires, we call the driver.
892  * Hopefully, more on that later.
893  *
894  * Also, I'm waiting to see how many people will complain about the
895  * netif_running(dev) test. I'm open on that one...
896  * Hopefully, the driver will remember to do a commit in "open()" ;-)
897  */
898 int call_commit_handler(struct net_device *dev)
899 {
900 #ifdef CONFIG_WIRELESS_EXT
901         if (netif_running(dev) &&
902             dev->wireless_handlers &&
903             dev->wireless_handlers->standard[0])
904                 /* Call the commit handler on the driver */
905                 return dev->wireless_handlers->standard[0](dev, NULL,
906                                                            NULL, NULL);
907         else
908                 return 0;               /* Command completed successfully */
909 #else
910         /* cfg80211 has no commit */
911         return 0;
912 #endif
913 }
914
915 /*
916  * Main IOCTl dispatcher.
917  * Check the type of IOCTL and call the appropriate wrapper...
918  */
919 static int wireless_process_ioctl(struct net *net, struct iwreq *iwr,
920                                   unsigned int cmd,
921                                   struct iw_request_info *info,
922                                   wext_ioctl_func standard,
923                                   wext_ioctl_func private)
924 {
925         struct net_device *dev;
926         iw_handler      handler;
927
928         /* Permissions are already checked in dev_ioctl() before calling us.
929          * The copy_to/from_user() of ifr is also dealt with in there */
930
931         /* Make sure the device exist */
932         if ((dev = __dev_get_by_name(net, iwr->ifr_name)) == NULL)
933                 return -ENODEV;
934
935         /* A bunch of special cases, then the generic case...
936          * Note that 'cmd' is already filtered in dev_ioctl() with
937          * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
938         if (cmd == SIOCGIWSTATS)
939                 return standard(dev, iwr, cmd, info,
940                                 &iw_handler_get_iwstats);
941
942 #ifdef CONFIG_WEXT_PRIV
943         if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
944                 return standard(dev, iwr, cmd, info,
945                                 iw_handler_get_private);
946 #endif
947
948         /* Basic check */
949         if (!netif_device_present(dev))
950                 return -ENODEV;
951
952         /* New driver API : try to find the handler */
953         handler = get_handler(dev, cmd);
954         if (handler) {
955                 /* Standard and private are not the same */
956                 if (cmd < SIOCIWFIRSTPRIV)
957                         return standard(dev, iwr, cmd, info, handler);
958                 else if (private)
959                         return private(dev, iwr, cmd, info, handler);
960         }
961         return -EOPNOTSUPP;
962 }
963
964 /* If command is `set a parameter', or `get the encoding parameters',
965  * check if the user has the right to do it.
966  */
967 static int wext_permission_check(unsigned int cmd)
968 {
969         if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
970              cmd == SIOCGIWENCODEEXT) &&
971             !capable(CAP_NET_ADMIN))
972                 return -EPERM;
973
974         return 0;
975 }
976
977 /* entry point from dev ioctl */
978 static int wext_ioctl_dispatch(struct net *net, struct iwreq *iwr,
979                                unsigned int cmd, struct iw_request_info *info,
980                                wext_ioctl_func standard,
981                                wext_ioctl_func private)
982 {
983         int ret = wext_permission_check(cmd);
984
985         if (ret)
986                 return ret;
987
988         dev_load(net, iwr->ifr_name);
989         rtnl_lock();
990         ret = wireless_process_ioctl(net, iwr, cmd, info, standard, private);
991         rtnl_unlock();
992
993         return ret;
994 }
995
996 /*
997  * Wrapper to call a standard Wireless Extension handler.
998  * We do various checks and also take care of moving data between
999  * user space and kernel space.
1000  */
1001 static int ioctl_standard_call(struct net_device *      dev,
1002                                struct iwreq             *iwr,
1003                                unsigned int             cmd,
1004                                struct iw_request_info   *info,
1005                                iw_handler               handler)
1006 {
1007         const struct iw_ioctl_description *     descr;
1008         int                                     ret = -EINVAL;
1009
1010         /* Get the description of the IOCTL */
1011         if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
1012                 return -EOPNOTSUPP;
1013         descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
1014
1015         /* Check if we have a pointer to user space data or not */
1016         if (descr->header_type != IW_HEADER_TYPE_POINT) {
1017
1018                 /* No extra arguments. Trivial to handle */
1019                 ret = handler(dev, info, &(iwr->u), NULL);
1020
1021                 /* Generate an event to notify listeners of the change */
1022                 if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
1023                    ((ret == 0) || (ret == -EIWCOMMIT)))
1024                         wireless_send_event(dev, cmd, &(iwr->u), NULL);
1025         } else {
1026                 ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
1027                                               handler, dev, info);
1028         }
1029
1030         /* Call commit handler if needed and defined */
1031         if (ret == -EIWCOMMIT)
1032                 ret = call_commit_handler(dev);
1033
1034         /* Here, we will generate the appropriate event if needed */
1035
1036         return ret;
1037 }
1038
1039
1040 int wext_handle_ioctl(struct net *net, struct iwreq *iwr, unsigned int cmd,
1041                       void __user *arg)
1042 {
1043         struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1044         int ret;
1045
1046         ret = wext_ioctl_dispatch(net, iwr, cmd, &info,
1047                                   ioctl_standard_call,
1048                                   ioctl_private_call);
1049         if (ret >= 0 &&
1050             IW_IS_GET(cmd) &&
1051             copy_to_user(arg, iwr, sizeof(struct iwreq)))
1052                 return -EFAULT;
1053
1054         return ret;
1055 }
1056
1057 #ifdef CONFIG_COMPAT
1058 static int compat_standard_call(struct net_device       *dev,
1059                                 struct iwreq            *iwr,
1060                                 unsigned int            cmd,
1061                                 struct iw_request_info  *info,
1062                                 iw_handler              handler)
1063 {
1064         const struct iw_ioctl_description *descr;
1065         struct compat_iw_point *iwp_compat;
1066         struct iw_point iwp;
1067         int err;
1068
1069         descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1070
1071         if (descr->header_type != IW_HEADER_TYPE_POINT)
1072                 return ioctl_standard_call(dev, iwr, cmd, info, handler);
1073
1074         iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1075         iwp.pointer = compat_ptr(iwp_compat->pointer);
1076         iwp.length = iwp_compat->length;
1077         iwp.flags = iwp_compat->flags;
1078
1079         err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1080
1081         iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1082         iwp_compat->length = iwp.length;
1083         iwp_compat->flags = iwp.flags;
1084
1085         return err;
1086 }
1087
1088 int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1089                              unsigned long arg)
1090 {
1091         void __user *argp = (void __user *)arg;
1092         struct iw_request_info info;
1093         struct iwreq iwr;
1094         char *colon;
1095         int ret;
1096
1097         if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1098                 return -EFAULT;
1099
1100         iwr.ifr_name[IFNAMSIZ-1] = 0;
1101         colon = strchr(iwr.ifr_name, ':');
1102         if (colon)
1103                 *colon = 0;
1104
1105         info.cmd = cmd;
1106         info.flags = IW_REQUEST_FLAG_COMPAT;
1107
1108         ret = wext_ioctl_dispatch(net, &iwr, cmd, &info,
1109                                   compat_standard_call,
1110                                   compat_private_call);
1111
1112         if (ret >= 0 &&
1113             IW_IS_GET(cmd) &&
1114             copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1115                 return -EFAULT;
1116
1117         return ret;
1118 }
1119 #endif
1120
1121 char *iwe_stream_add_event(struct iw_request_info *info, char *stream,
1122                            char *ends, struct iw_event *iwe, int event_len)
1123 {
1124         int lcp_len = iwe_stream_lcp_len(info);
1125
1126         event_len = iwe_stream_event_len_adjust(info, event_len);
1127
1128         /* Check if it's possible */
1129         if (likely((stream + event_len) < ends)) {
1130                 iwe->len = event_len;
1131                 /* Beware of alignement issues on 64 bits */
1132                 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1133                 memcpy(stream + lcp_len, &iwe->u,
1134                        event_len - lcp_len);
1135                 stream += event_len;
1136         }
1137
1138         return stream;
1139 }
1140 EXPORT_SYMBOL(iwe_stream_add_event);
1141
1142 char *iwe_stream_add_point(struct iw_request_info *info, char *stream,
1143                            char *ends, struct iw_event *iwe, char *extra)
1144 {
1145         int event_len = iwe_stream_point_len(info) + iwe->u.data.length;
1146         int point_len = iwe_stream_point_len(info);
1147         int lcp_len   = iwe_stream_lcp_len(info);
1148
1149         /* Check if it's possible */
1150         if (likely((stream + event_len) < ends)) {
1151                 iwe->len = event_len;
1152                 memcpy(stream, (char *) iwe, IW_EV_LCP_PK_LEN);
1153                 memcpy(stream + lcp_len,
1154                        ((char *) &iwe->u) + IW_EV_POINT_OFF,
1155                        IW_EV_POINT_PK_LEN - IW_EV_LCP_PK_LEN);
1156                 if (iwe->u.data.length && extra)
1157                         memcpy(stream + point_len, extra, iwe->u.data.length);
1158                 stream += event_len;
1159         }
1160
1161         return stream;
1162 }
1163 EXPORT_SYMBOL(iwe_stream_add_point);
1164
1165 char *iwe_stream_add_value(struct iw_request_info *info, char *event,
1166                            char *value, char *ends, struct iw_event *iwe,
1167                            int event_len)
1168 {
1169         int lcp_len = iwe_stream_lcp_len(info);
1170
1171         /* Don't duplicate LCP */
1172         event_len -= IW_EV_LCP_LEN;
1173
1174         /* Check if it's possible */
1175         if (likely((value + event_len) < ends)) {
1176                 /* Add new value */
1177                 memcpy(value, &iwe->u, event_len);
1178                 value += event_len;
1179                 /* Patch LCP */
1180                 iwe->len = value - event;
1181                 memcpy(event, (char *) iwe, lcp_len);
1182         }
1183
1184         return value;
1185 }
1186 EXPORT_SYMBOL(iwe_stream_add_value);