sctp: fix a type cast warnings that causes a_rwnd gets the wrong value
[pandora-kernel.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 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 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         return register_netdevice_notifier(&wext_netdev_notifier);
403 }
404
405 subsys_initcall(wireless_nlevent_init);
406
407 /* Process events generated by the wireless layer or the driver. */
408 static void wireless_nlevent_process(struct work_struct *work)
409 {
410         rtnl_lock();
411         wireless_nlevent_flush();
412         rtnl_unlock();
413 }
414
415 static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
416
417 static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
418                                               struct sk_buff *skb)
419 {
420         struct ifinfomsg *r;
421         struct nlmsghdr  *nlh;
422
423         nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
424         if (!nlh)
425                 return NULL;
426
427         r = nlmsg_data(nlh);
428         r->ifi_family = AF_UNSPEC;
429         r->__ifi_pad = 0;
430         r->ifi_type = dev->type;
431         r->ifi_index = dev->ifindex;
432         r->ifi_flags = dev_get_flags(dev);
433         r->ifi_change = 0;      /* Wireless changes don't affect those flags */
434
435         NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
436
437         return nlh;
438  nla_put_failure:
439         nlmsg_cancel(skb, nlh);
440         return NULL;
441 }
442
443
444 /*
445  * Main event dispatcher. Called from other parts and drivers.
446  * Send the event on the appropriate channels.
447  * May be called from interrupt context.
448  */
449 void wireless_send_event(struct net_device *    dev,
450                          unsigned int           cmd,
451                          union iwreq_data *     wrqu,
452                          const char *           extra)
453 {
454         const struct iw_ioctl_description *     descr = NULL;
455         int extra_len = 0;
456         struct iw_event  *event;                /* Mallocated whole event */
457         int event_len;                          /* Its size */
458         int hdr_len;                            /* Size of the event header */
459         int wrqu_off = 0;                       /* Offset in wrqu */
460         /* Don't "optimise" the following variable, it will crash */
461         unsigned        cmd_index;              /* *MUST* be unsigned */
462         struct sk_buff *skb;
463         struct nlmsghdr *nlh;
464         struct nlattr *nla;
465 #ifdef CONFIG_COMPAT
466         struct __compat_iw_event *compat_event;
467         struct compat_iw_point compat_wrqu;
468         struct sk_buff *compskb;
469 #endif
470
471         /*
472          * Nothing in the kernel sends scan events with data, be safe.
473          * This is necessary because we cannot fix up scan event data
474          * for compat, due to being contained in 'extra', but normally
475          * applications are required to retrieve the scan data anyway
476          * and no data is included in the event, this codifies that
477          * practice.
478          */
479         if (WARN_ON(cmd == SIOCGIWSCAN && extra))
480                 extra = NULL;
481
482         /* Get the description of the Event */
483         if (cmd <= SIOCIWLAST) {
484                 cmd_index = IW_IOCTL_IDX(cmd);
485                 if (cmd_index < standard_ioctl_num)
486                         descr = &(standard_ioctl[cmd_index]);
487         } else {
488                 cmd_index = IW_EVENT_IDX(cmd);
489                 if (cmd_index < standard_event_num)
490                         descr = &(standard_event[cmd_index]);
491         }
492         /* Don't accept unknown events */
493         if (descr == NULL) {
494                 /* Note : we don't return an error to the driver, because
495                  * the driver would not know what to do about it. It can't
496                  * return an error to the user, because the event is not
497                  * initiated by a user request.
498                  * The best the driver could do is to log an error message.
499                  * We will do it ourselves instead...
500                  */
501                 netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
502                            cmd);
503                 return;
504         }
505
506         /* Check extra parameters and set extra_len */
507         if (descr->header_type == IW_HEADER_TYPE_POINT) {
508                 /* Check if number of token fits within bounds */
509                 if (wrqu->data.length > descr->max_tokens) {
510                         netdev_err(dev, "(WE) : Wireless Event too big (%d)\n",
511                                    wrqu->data.length);
512                         return;
513                 }
514                 if (wrqu->data.length < descr->min_tokens) {
515                         netdev_err(dev, "(WE) : Wireless Event too small (%d)\n",
516                                    wrqu->data.length);
517                         return;
518                 }
519                 /* Calculate extra_len - extra is NULL for restricted events */
520                 if (extra != NULL)
521                         extra_len = wrqu->data.length * descr->token_size;
522                 /* Always at an offset in wrqu */
523                 wrqu_off = IW_EV_POINT_OFF;
524         }
525
526         /* Total length of the event */
527         hdr_len = event_type_size[descr->header_type];
528         event_len = hdr_len + extra_len;
529
530         /*
531          * The problem for 64/32 bit.
532          *
533          * On 64-bit, a regular event is laid out as follows:
534          *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
535          *      | event.len | event.cmd |     p a d d i n g     |
536          *      | wrqu data ... (with the correct size)         |
537          *
538          * This padding exists because we manipulate event->u,
539          * and 'event' is not packed.
540          *
541          * An iw_point event is laid out like this instead:
542          *      |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
543          *      | event.len | event.cmd |     p a d d i n g     |
544          *      | iwpnt.len | iwpnt.flg |     p a d d i n g     |
545          *      | extra data  ...
546          *
547          * The second padding exists because struct iw_point is extended,
548          * but this depends on the platform...
549          *
550          * On 32-bit, all the padding shouldn't be there.
551          */
552
553         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
554         if (!skb)
555                 return;
556
557         /* Send via the RtNetlink event channel */
558         nlh = rtnetlink_ifinfo_prep(dev, skb);
559         if (WARN_ON(!nlh)) {
560                 kfree_skb(skb);
561                 return;
562         }
563
564         /* Add the wireless events in the netlink packet */
565         nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
566         if (!nla) {
567                 kfree_skb(skb);
568                 return;
569         }
570         event = nla_data(nla);
571
572         /* Fill event - first clear to avoid data leaking */
573         memset(event, 0, hdr_len);
574         event->len = event_len;
575         event->cmd = cmd;
576         memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
577         if (extra_len)
578                 memcpy(((char *) event) + hdr_len, extra, extra_len);
579
580         nlmsg_end(skb, nlh);
581 #ifdef CONFIG_COMPAT
582         hdr_len = compat_event_type_size[descr->header_type];
583         event_len = hdr_len + extra_len;
584
585         compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
586         if (!compskb) {
587                 kfree_skb(skb);
588                 return;
589         }
590
591         /* Send via the RtNetlink event channel */
592         nlh = rtnetlink_ifinfo_prep(dev, compskb);
593         if (WARN_ON(!nlh)) {
594                 kfree_skb(skb);
595                 kfree_skb(compskb);
596                 return;
597         }
598
599         /* Add the wireless events in the netlink packet */
600         nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
601         if (!nla) {
602                 kfree_skb(skb);
603                 kfree_skb(compskb);
604                 return;
605         }
606         compat_event = nla_data(nla);
607
608         compat_event->len = event_len;
609         compat_event->cmd = cmd;
610         if (descr->header_type == IW_HEADER_TYPE_POINT) {
611                 compat_wrqu.length = wrqu->data.length;
612                 compat_wrqu.flags = wrqu->data.flags;
613                 memcpy(&compat_event->pointer,
614                         ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
615                         hdr_len - IW_EV_COMPAT_LCP_LEN);
616                 if (extra_len)
617                         memcpy(((char *) compat_event) + hdr_len,
618                                 extra, extra_len);
619         } else {
620                 /* extra_len must be zero, so no if (extra) needed */
621                 memcpy(&compat_event->pointer, wrqu,
622                         hdr_len - IW_EV_COMPAT_LCP_LEN);
623         }
624
625         nlmsg_end(compskb, nlh);
626
627         skb_shinfo(skb)->frag_list = compskb;
628 #endif
629         skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
630         schedule_work(&wireless_nlevent_work);
631 }
632 EXPORT_SYMBOL(wireless_send_event);
633
634
635
636 /* IW handlers */
637
638 struct iw_statistics *get_wireless_stats(struct net_device *dev)
639 {
640 #ifdef CONFIG_WIRELESS_EXT
641         if ((dev->wireless_handlers != NULL) &&
642            (dev->wireless_handlers->get_wireless_stats != NULL))
643                 return dev->wireless_handlers->get_wireless_stats(dev);
644 #endif
645
646 #ifdef CONFIG_CFG80211_WEXT
647         if (dev->ieee80211_ptr &&
648             dev->ieee80211_ptr->wiphy &&
649             dev->ieee80211_ptr->wiphy->wext &&
650             dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
651                 return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
652 #endif
653
654         /* not found */
655         return NULL;
656 }
657
658 static int iw_handler_get_iwstats(struct net_device *           dev,
659                                   struct iw_request_info *      info,
660                                   union iwreq_data *            wrqu,
661                                   char *                        extra)
662 {
663         /* Get stats from the driver */
664         struct iw_statistics *stats;
665
666         stats = get_wireless_stats(dev);
667         if (stats) {
668                 /* Copy statistics to extra */
669                 memcpy(extra, stats, sizeof(struct iw_statistics));
670                 wrqu->data.length = sizeof(struct iw_statistics);
671
672                 /* Check if we need to clear the updated flag */
673                 if (wrqu->data.flags != 0)
674                         stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
675                 return 0;
676         } else
677                 return -EOPNOTSUPP;
678 }
679
680 static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
681 {
682         /* Don't "optimise" the following variable, it will crash */
683         unsigned int    index;          /* *MUST* be unsigned */
684         const struct iw_handler_def *handlers = NULL;
685
686 #ifdef CONFIG_CFG80211_WEXT
687         if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
688                 handlers = dev->ieee80211_ptr->wiphy->wext;
689 #endif
690 #ifdef CONFIG_WIRELESS_EXT
691         if (dev->wireless_handlers)
692                 handlers = dev->wireless_handlers;
693 #endif
694
695         if (!handlers)
696                 return NULL;
697
698         /* Try as a standard command */
699         index = IW_IOCTL_IDX(cmd);
700         if (index < handlers->num_standard)
701                 return handlers->standard[index];
702
703 #ifdef CONFIG_WEXT_PRIV
704         /* Try as a private command */
705         index = cmd - SIOCIWFIRSTPRIV;
706         if (index < handlers->num_private)
707                 return handlers->private[index];
708 #endif
709
710         /* Not found */
711         return NULL;
712 }
713
714 static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
715                                    const struct iw_ioctl_description *descr,
716                                    iw_handler handler, struct net_device *dev,
717                                    struct iw_request_info *info)
718 {
719         int err, extra_size, user_length = 0, essid_compat = 0;
720         char *extra;
721
722         /* Calculate space needed by arguments. Always allocate
723          * for max space.
724          */
725         extra_size = descr->max_tokens * descr->token_size;
726
727         /* Check need for ESSID compatibility for WE < 21 */
728         switch (cmd) {
729         case SIOCSIWESSID:
730         case SIOCGIWESSID:
731         case SIOCSIWNICKN:
732         case SIOCGIWNICKN:
733                 if (iwp->length == descr->max_tokens + 1)
734                         essid_compat = 1;
735                 else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
736                         char essid[IW_ESSID_MAX_SIZE + 1];
737                         unsigned int len;
738                         len = iwp->length * descr->token_size;
739
740                         if (len > IW_ESSID_MAX_SIZE)
741                                 return -EFAULT;
742
743                         err = copy_from_user(essid, iwp->pointer, len);
744                         if (err)
745                                 return -EFAULT;
746
747                         if (essid[iwp->length - 1] == '\0')
748                                 essid_compat = 1;
749                 }
750                 break;
751         default:
752                 break;
753         }
754
755         iwp->length -= essid_compat;
756
757         /* Check what user space is giving us */
758         if (IW_IS_SET(cmd)) {
759                 /* Check NULL pointer */
760                 if (!iwp->pointer && iwp->length != 0)
761                         return -EFAULT;
762                 /* Check if number of token fits within bounds */
763                 if (iwp->length > descr->max_tokens)
764                         return -E2BIG;
765                 if (iwp->length < descr->min_tokens)
766                         return -EINVAL;
767         } else {
768                 /* Check NULL pointer */
769                 if (!iwp->pointer)
770                         return -EFAULT;
771                 /* Save user space buffer size for checking */
772                 user_length = iwp->length;
773
774                 /* Don't check if user_length > max to allow forward
775                  * compatibility. The test user_length < min is
776                  * implied by the test at the end.
777                  */
778
779                 /* Support for very large requests */
780                 if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
781                     (user_length > descr->max_tokens)) {
782                         /* Allow userspace to GET more than max so
783                          * we can support any size GET requests.
784                          * There is still a limit : -ENOMEM.
785                          */
786                         extra_size = user_length * descr->token_size;
787
788                         /* Note : user_length is originally a __u16,
789                          * and token_size is controlled by us,
790                          * so extra_size won't get negative and
791                          * won't overflow...
792                          */
793                 }
794         }
795
796         /* kzalloc() ensures NULL-termination for essid_compat. */
797         extra = kzalloc(extra_size, GFP_KERNEL);
798         if (!extra)
799                 return -ENOMEM;
800
801         /* If it is a SET, get all the extra data in here */
802         if (IW_IS_SET(cmd) && (iwp->length != 0)) {
803                 if (copy_from_user(extra, iwp->pointer,
804                                    iwp->length *
805                                    descr->token_size)) {
806                         err = -EFAULT;
807                         goto out;
808                 }
809
810                 if (cmd == SIOCSIWENCODEEXT) {
811                         struct iw_encode_ext *ee = (void *) extra;
812
813                         if (iwp->length < sizeof(*ee) + ee->key_len)
814                                 return -EFAULT;
815                 }
816         }
817
818         if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
819                 /*
820                  * If this is a GET, but not NOMAX, it means that the extra
821                  * data is not bounded by userspace, but by max_tokens. Thus
822                  * set the length to max_tokens. This matches the extra data
823                  * allocation.
824                  * The driver should fill it with the number of tokens it
825                  * provided, and it may check iwp->length rather than having
826                  * knowledge of max_tokens. If the driver doesn't change the
827                  * iwp->length, this ioctl just copies back max_token tokens
828                  * filled with zeroes. Hopefully the driver isn't claiming
829                  * them to be valid data.
830                  */
831                 iwp->length = descr->max_tokens;
832         }
833
834         err = handler(dev, info, (union iwreq_data *) iwp, extra);
835
836         iwp->length += essid_compat;
837
838         /* If we have something to return to the user */
839         if (!err && IW_IS_GET(cmd)) {
840                 /* Check if there is enough buffer up there */
841                 if (user_length < iwp->length) {
842                         err = -E2BIG;
843                         goto out;
844                 }
845
846                 if (copy_to_user(iwp->pointer, extra,
847                                  iwp->length *
848                                  descr->token_size)) {
849                         err = -EFAULT;
850                         goto out;
851                 }
852         }
853
854         /* Generate an event to notify listeners of the change */
855         if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
856             ((err == 0) || (err == -EIWCOMMIT))) {
857                 union iwreq_data *data = (union iwreq_data *) iwp;
858
859                 if (descr->flags & IW_DESCR_FLAG_RESTRICT)
860                         /* If the event is restricted, don't
861                          * export the payload.
862                          */
863                         wireless_send_event(dev, cmd, data, NULL);
864                 else
865                         wireless_send_event(dev, cmd, data, extra);
866         }
867
868 out:
869         kfree(extra);
870         return err;
871 }
872
873 /*
874  * Call the commit handler in the driver
875  * (if exist and if conditions are right)
876  *
877  * Note : our current commit strategy is currently pretty dumb,
878  * but we will be able to improve on that...
879  * The goal is to try to agreagate as many changes as possible
880  * before doing the commit. Drivers that will define a commit handler
881  * are usually those that need a reset after changing parameters, so
882  * we want to minimise the number of reset.
883  * A cool idea is to use a timer : at each "set" command, we re-set the
884  * timer, when the timer eventually fires, we call the driver.
885  * Hopefully, more on that later.
886  *
887  * Also, I'm waiting to see how many people will complain about the
888  * netif_running(dev) test. I'm open on that one...
889  * Hopefully, the driver will remember to do a commit in "open()" ;-)
890  */
891 int call_commit_handler(struct net_device *dev)
892 {
893 #ifdef CONFIG_WIRELESS_EXT
894         if ((netif_running(dev)) &&
895            (dev->wireless_handlers->standard[0] != NULL))
896                 /* Call the commit handler on the driver */
897                 return dev->wireless_handlers->standard[0](dev, NULL,
898                                                            NULL, NULL);
899         else
900                 return 0;               /* Command completed successfully */
901 #else
902         /* cfg80211 has no commit */
903         return 0;
904 #endif
905 }
906
907 /*
908  * Main IOCTl dispatcher.
909  * Check the type of IOCTL and call the appropriate wrapper...
910  */
911 static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
912                                   unsigned int cmd,
913                                   struct iw_request_info *info,
914                                   wext_ioctl_func standard,
915                                   wext_ioctl_func private)
916 {
917         struct iwreq *iwr = (struct iwreq *) ifr;
918         struct net_device *dev;
919         iw_handler      handler;
920
921         /* Permissions are already checked in dev_ioctl() before calling us.
922          * The copy_to/from_user() of ifr is also dealt with in there */
923
924         /* Make sure the device exist */
925         if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
926                 return -ENODEV;
927
928         /* A bunch of special cases, then the generic case...
929          * Note that 'cmd' is already filtered in dev_ioctl() with
930          * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
931         if (cmd == SIOCGIWSTATS)
932                 return standard(dev, iwr, cmd, info,
933                                 &iw_handler_get_iwstats);
934
935 #ifdef CONFIG_WEXT_PRIV
936         if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
937                 return standard(dev, iwr, cmd, info,
938                                 iw_handler_get_private);
939 #endif
940
941         /* Basic check */
942         if (!netif_device_present(dev))
943                 return -ENODEV;
944
945         /* New driver API : try to find the handler */
946         handler = get_handler(dev, cmd);
947         if (handler) {
948                 /* Standard and private are not the same */
949                 if (cmd < SIOCIWFIRSTPRIV)
950                         return standard(dev, iwr, cmd, info, handler);
951                 else if (private)
952                         return private(dev, iwr, cmd, info, handler);
953         }
954         /* Old driver API : call driver ioctl handler */
955         if (dev->netdev_ops->ndo_do_ioctl)
956                 return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
957         return -EOPNOTSUPP;
958 }
959
960 /* If command is `set a parameter', or `get the encoding parameters',
961  * check if the user has the right to do it.
962  */
963 static 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 /* entry point from dev ioctl */
974 static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
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, ifr->ifr_name);
985         rtnl_lock();
986         ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
987         rtnl_unlock();
988
989         return ret;
990 }
991
992 /*
993  * Wrapper to call a standard Wireless Extension handler.
994  * We do various checks and also take care of moving data between
995  * user space and kernel space.
996  */
997 static 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         /* Get the description of the IOCTL */
1007         if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
1008                 return -EOPNOTSUPP;
1009         descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
1010
1011         /* Check if we have a pointer to user space data or not */
1012         if (descr->header_type != IW_HEADER_TYPE_POINT) {
1013
1014                 /* No extra arguments. Trivial to handle */
1015                 ret = handler(dev, info, &(iwr->u), NULL);
1016
1017                 /* Generate an event to notify listeners of the change */
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         /* Call commit handler if needed and defined */
1027         if (ret == -EIWCOMMIT)
1028                 ret = call_commit_handler(dev);
1029
1030         /* Here, we will generate the appropriate event if needed */
1031
1032         return ret;
1033 }
1034
1035
1036 int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
1037                       void __user *arg)
1038 {
1039         struct iw_request_info info = { .cmd = cmd, .flags = 0 };
1040         int ret;
1041
1042         ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
1043                                   ioctl_standard_call,
1044                                   ioctl_private_call);
1045         if (ret >= 0 &&
1046             IW_IS_GET(cmd) &&
1047             copy_to_user(arg, ifr, sizeof(struct iwreq)))
1048                 return -EFAULT;
1049
1050         return ret;
1051 }
1052
1053 #ifdef CONFIG_COMPAT
1054 static int compat_standard_call(struct net_device       *dev,
1055                                 struct iwreq            *iwr,
1056                                 unsigned int            cmd,
1057                                 struct iw_request_info  *info,
1058                                 iw_handler              handler)
1059 {
1060         const struct iw_ioctl_description *descr;
1061         struct compat_iw_point *iwp_compat;
1062         struct iw_point iwp;
1063         int err;
1064
1065         descr = standard_ioctl + IW_IOCTL_IDX(cmd);
1066
1067         if (descr->header_type != IW_HEADER_TYPE_POINT)
1068                 return ioctl_standard_call(dev, iwr, cmd, info, handler);
1069
1070         iwp_compat = (struct compat_iw_point *) &iwr->u.data;
1071         iwp.pointer = compat_ptr(iwp_compat->pointer);
1072         iwp.length = iwp_compat->length;
1073         iwp.flags = iwp_compat->flags;
1074
1075         err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
1076
1077         iwp_compat->pointer = ptr_to_compat(iwp.pointer);
1078         iwp_compat->length = iwp.length;
1079         iwp_compat->flags = iwp.flags;
1080
1081         return err;
1082 }
1083
1084 int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
1085                              unsigned long arg)
1086 {
1087         void __user *argp = (void __user *)arg;
1088         struct iw_request_info info;
1089         struct iwreq iwr;
1090         char *colon;
1091         int ret;
1092
1093         if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
1094                 return -EFAULT;
1095
1096         iwr.ifr_name[IFNAMSIZ-1] = 0;
1097         colon = strchr(iwr.ifr_name, ':');
1098         if (colon)
1099                 *colon = 0;
1100
1101         info.cmd = cmd;
1102         info.flags = IW_REQUEST_FLAG_COMPAT;
1103
1104         ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
1105                                   compat_standard_call,
1106                                   compat_private_call);
1107
1108         if (ret >= 0 &&
1109             IW_IS_GET(cmd) &&
1110             copy_to_user(argp, &iwr, sizeof(struct iwreq)))
1111                 return -EFAULT;
1112
1113         return ret;
1114 }
1115 #endif