Add snort package, with experimental -custom package for build-time package customiza...
[openwrt.git] / package / openwrt / wlcompat.c
1 /*
2  * wlcompat.c
3  *
4  * Copyright (C) 2005 Mike Baker,
5  *                    Felix Fietkau <nbd@vd-s.ath.cx>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * $Id$
22  */
23
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/if_arp.h>
29 #include <asm/uaccess.h>
30 #include <linux/wireless.h>
31
32 #include <net/iw_handler.h>
33 #include <wlioctl.h>
34
35 static struct net_device *dev;
36 char buf[WLC_IOCTL_MAXLEN];
37
38 /* The frequency of each channel in MHz */
39 const long channel_frequency[] = {
40         2412, 2417, 2422, 2427, 2432, 2437, 2442,
41         2447, 2452, 2457, 2462, 2467, 2472, 2484
42 };
43 #define NUM_CHANNELS ( sizeof(channel_frequency) / sizeof(channel_frequency[0]) )
44
45 static int wl_ioctl(struct net_device *dev, int cmd, void *buf, int len)
46 {
47         mm_segment_t old_fs = get_fs();
48         struct ifreq ifr;
49         int ret;
50         wl_ioctl_t ioc;
51         ioc.cmd = cmd;
52         ioc.buf = buf;
53         ioc.len = len;
54         strncpy(ifr.ifr_name, dev->name, IFNAMSIZ);
55         ifr.ifr_data = (caddr_t) &ioc;
56         set_fs(KERNEL_DS);
57         ret = dev->do_ioctl(dev,&ifr,SIOCDEVPRIVATE);
58         set_fs (old_fs);
59         return ret;
60 }
61
62 static int wlcompat_ioctl_getiwrange(struct net_device *dev,
63                                     char *extra)
64 {
65         int i, k;
66         struct iw_range *range;
67
68         range = (struct iw_range *) extra;
69
70         range->we_version_compiled = WIRELESS_EXT;
71         range->we_version_source = WIRELESS_EXT;
72         
73         range->min_nwid = range->max_nwid = 0;
74         
75         range->num_channels = NUM_CHANNELS;
76         k = 0;
77         for (i = 0; i < NUM_CHANNELS; i++) {
78                 range->freq[k].i = i + 1;
79                 range->freq[k].m = channel_frequency[i] * 100000;
80                 range->freq[k].e = 1;
81                 k++;
82                 if (k >= IW_MAX_FREQUENCIES)
83                         break;
84         }
85         range->num_frequency = k;
86         range->sensitivity = 3;
87
88         /* nbd: don't know what this means, but other drivers set it this way */
89         range->pmp_flags = IW_POWER_PERIOD;
90         range->pmt_flags = IW_POWER_TIMEOUT;
91         range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_UNICAST_R;
92
93         range->min_pmp = 0;
94         range->max_pmp = 65535000;
95         range->min_pmt = 0;
96         range->max_pmt = 65535 * 1000;
97
98         range->min_rts = 0;
99         if (wl_ioctl(dev, WLC_GET_RTS, &range->max_rts, sizeof(int)) < 0)
100                 range->max_rts = 2347;
101
102         range->min_frag = 256;
103         
104         if (wl_ioctl(dev, WLC_GET_FRAG, &range->max_frag, sizeof(int)) < 0)
105                 range->max_frag = 2346;
106
107         range->txpower_capa = IW_TXPOW_MWATT;
108
109         return 0;
110 }
111
112
113 static int wlcompat_set_scan(struct net_device *dev,
114                          struct iw_request_info *info,
115                          union iwreq_data *wrqu,
116                          char *extra)
117 {
118         int ap = 0, oldap = 0;
119         wl_scan_params_t params;
120
121         memset(&params, 0, sizeof(params));
122
123         /* use defaults (same parameters as wl scan) */
124         memset(&params.bssid, 0xff, sizeof(params.bssid));
125         params.bss_type = DOT11_BSSTYPE_ANY;
126         params.scan_type = -1;
127         params.nprobes = -1;
128         params.active_time = -1;
129         params.passive_time = -1;
130         params.home_time = -1;
131         
132         /* can only scan in STA mode */
133         wl_ioctl(dev, WLC_GET_AP, &oldap, sizeof(oldap));
134         if (oldap > 0)
135                 wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
136         
137         if (wl_ioctl(dev, WLC_SCAN, &params, 64) < 0)
138                 return -EINVAL;
139         
140         if (oldap > 0)
141                 wl_ioctl(dev, WLC_SET_AP, &oldap, sizeof(oldap));
142
143         return 0;
144 }
145
146
147 static int wlcompat_get_scan(struct net_device *dev,
148                          struct iw_request_info *info,
149                          union iwreq_data *wrqu,
150                          char *extra)
151 {
152         wl_scan_results_t *results = (wl_scan_results_t *) buf;
153         wl_bss_info_t *bss_info;
154         char *info_ptr;
155         char *current_ev = extra;
156         char *current_val;
157         char *end_buf = extra + IW_SCAN_MAX_DATA;
158         struct iw_event iwe;
159         int i, j;
160
161         if (wl_ioctl(dev, WLC_SCAN_RESULTS, buf, WLC_IOCTL_MAXLEN) < 0)
162                 return -EAGAIN;
163         
164         bss_info = &(results->bss_info[0]);
165         info_ptr = (char *) bss_info;
166         for (i = 0; i < results->count; i++) {
167
168                 /* send the cell address (must be sent first) */
169                 iwe.cmd = SIOCGIWAP;
170                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
171                 memcpy(&iwe.u.ap_addr.sa_data, &bss_info->BSSID, sizeof(bss_info->BSSID));
172                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
173                 
174                 /* send the ESSID */
175                 iwe.cmd = SIOCGIWESSID;
176                 iwe.u.data.length = bss_info->SSID_len;
177                 if (iwe.u.data.length > IW_ESSID_MAX_SIZE)
178                         iwe.u.data.length = IW_ESSID_MAX_SIZE;
179                 iwe.u.data.flags = 1;
180                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, bss_info->SSID);
181
182                 /* send frequency/channel info */
183                 iwe.cmd = SIOCGIWFREQ;
184                 iwe.u.freq.e = 0;
185                 iwe.u.freq.m = bss_info->channel;
186                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
187
188                 /* add quality statistics */
189                 iwe.cmd = IWEVQUAL;
190                 iwe.u.qual.level = bss_info->RSSI;
191                 iwe.u.qual.noise = bss_info->phy_noise;
192                 iwe.u.qual.qual = 0;
193                 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
194         
195                 /* send rate information */
196                 iwe.cmd = SIOCGIWRATE;
197                 current_val = current_ev + IW_EV_LCP_LEN;
198                 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
199                 
200                 for(j = 0 ; j < bss_info->rateset.count ; j++) {
201                         iwe.u.bitrate.value = ((bss_info->rateset.rates[j] & 0x7f) * 500000);
202                         current_val = iwe_stream_add_value(current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
203                 }
204                 if((current_val - current_ev) > IW_EV_LCP_LEN)
205                         current_ev = current_val;
206
207                 info_ptr += sizeof(wl_bss_info_t);
208                 if (bss_info->ie_length % 4)
209                         info_ptr += bss_info->ie_length + 4 - (bss_info->ie_length % 4);
210                 else
211                         info_ptr += bss_info->ie_length;
212                 bss_info = (wl_bss_info_t *) info_ptr;
213         }
214         
215         wrqu->data.length = (current_ev - extra);
216         wrqu->data.flags = 0;
217
218         return 0;
219 }
220
221 static int wlcompat_ioctl(struct net_device *dev,
222                          struct iw_request_info *info,
223                          union iwreq_data *wrqu,
224                          char *extra)
225 {
226         switch (info->cmd) {
227                 case SIOCGIWNAME:
228                         strcpy(wrqu->name, "IEEE 802.11-DS");
229                         break;
230                 case SIOCGIWFREQ:
231                 {
232                         channel_info_t ci;
233
234                         if (wl_ioctl(dev,WLC_GET_CHANNEL, &ci, sizeof(ci)) < 0)
235                                 return -EINVAL;
236
237                         wrqu->freq.m = ci.target_channel;
238                         wrqu->freq.e = 0;
239                         break;
240                 }
241                 case SIOCSIWFREQ:
242                 {
243                         if (wrqu->freq.e == 1) {
244                                 int channel = 0;
245                                 int f = wrqu->freq.m / 100000;
246                                 while ((channel < NUM_CHANNELS + 1) && (f != channel_frequency[channel]))
247                                         channel++;
248                                 
249                                 if (channel == NUM_CHANNELS) // channel not found
250                                         return -EINVAL;
251
252                                 wrqu->freq.e = 0;
253                                 wrqu->freq.m = channel + 1;
254                         }
255                         if ((wrqu->freq.e == 0) && (wrqu->freq.m < 1000)) {
256                                 if (wl_ioctl(dev, WLC_SET_CHANNEL, &wrqu->freq.m, sizeof(int)) < 0)
257                                         return -EINVAL;
258                         } else {
259                                 return -EINVAL;
260                         }
261                         break;
262                 }
263                 case SIOCGIWAP:
264                 {
265                         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
266                         if (wl_ioctl(dev,WLC_GET_BSSID,wrqu->ap_addr.sa_data,6) < 0)
267                                 return -EINVAL;
268                         break;
269                 }
270                 case SIOCGIWESSID:
271                 {
272                         wlc_ssid_t ssid;
273                         
274                         if (wl_ioctl(dev,WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)) < 0)
275                                 return -EINVAL;
276
277                         wrqu->essid.flags = wrqu->data.flags = 1;
278                         wrqu->essid.length = wrqu->data.length = ssid.SSID_len + 1;
279                         memcpy(extra,ssid.SSID,ssid.SSID_len + 1);
280                         break;
281                 }
282                 case SIOCSIWESSID:
283                 {
284                         wlc_ssid_t ssid;
285                         memset(&ssid, 0, sizeof(ssid));
286                         ssid.SSID_len = strlen(extra);
287                         if (ssid.SSID_len > WLC_ESSID_MAX_SIZE)
288                                 ssid.SSID_len = WLC_ESSID_MAX_SIZE;
289                         memcpy(ssid.SSID, extra, ssid.SSID_len);
290                         if (wl_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)) < 0)
291                                 return -EINVAL;
292                         break;
293                 }
294                 case SIOCGIWRTS:
295                 {
296                         if (wl_ioctl(dev,WLC_GET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
297                                 return -EINVAL;
298                         break;
299                 }
300                 case SIOCSIWRTS:
301                 {
302                         if (wl_ioctl(dev,WLC_SET_RTS,&(wrqu->rts.value),sizeof(int)) < 0) 
303                                 return -EINVAL;
304                         break;
305                 }
306                 case SIOCGIWFRAG:
307                 {
308                         if (wl_ioctl(dev,WLC_GET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
309                                 return -EINVAL;
310                         break;
311                 }
312                 case SIOCSIWFRAG:
313                 {
314                         if (wl_ioctl(dev,WLC_SET_FRAG,&(wrqu->frag.value),sizeof(int)) < 0)
315                                 return -EINVAL;
316                         break;
317                 }
318                 case SIOCGIWTXPOW:
319                 {
320                         wrqu->txpower.value = 0;
321                         if (wl_ioctl(dev,WLC_GET_TXPWR, &(wrqu->txpower.value), sizeof(int)) < 0)
322                                 return -EINVAL;
323                         wrqu->txpower.fixed = 0;
324                         wrqu->txpower.disabled = 0;
325                         wrqu->txpower.flags = IW_TXPOW_MWATT;
326                         break;
327                 }
328                 case SIOCSIWTXPOW:
329                 {
330                         if (wrqu->txpower.flags != IW_TXPOW_MWATT)
331                                 return -EINVAL;
332
333                         if (wl_ioctl(dev, WLC_SET_TXPWR, &wrqu->txpower.value, sizeof(int)) < 0)
334                                 return -EINVAL;
335                 }
336                 case SIOCGIWENCODE:
337                 {
338                         int val = 0;
339                         if (wl_ioctl(dev, WLC_GET_WEP, &val, sizeof(val)) < 0)
340                                 return -EINVAL;
341                         
342                         if (val > 0) {
343                                 wrqu->data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
344                         } else {
345                                 wrqu->data.flags = IW_ENCODE_DISABLED;
346                         }
347                                 
348                                 
349                         
350                         break;
351                 }
352                 case SIOCGIWRANGE:
353                 {
354                         return wlcompat_ioctl_getiwrange(dev, extra);
355                         break;
356                 }
357                 case SIOCSIWMODE:
358                 {
359                         int ap = -1, infra = -1, passive = 0, wet = 0;
360                         
361                         switch (wrqu->mode) {
362                                 case IW_MODE_MONITOR:
363                                         passive = 1;
364                                         break;
365                                 case IW_MODE_ADHOC:
366                                         infra = 0;
367                                         ap = 0;
368                                         break;
369                                 case IW_MODE_MASTER:
370                                         infra = 1;
371                                         ap = 1;
372                                         break;
373                                 case IW_MODE_INFRA:
374                                         infra = 1;
375                                         ap = 0;
376                                         break;
377                                 case IW_MODE_REPEAT:
378                                         infra = 1;
379                                         ap = 0;
380                                         wet = 1;
381                                         break;
382                                 default:
383                                         return -EINVAL;
384                         }
385                         
386                         if (wl_ioctl(dev, WLC_SET_PASSIVE, &passive, sizeof(passive)) < 0)
387                                 return -EINVAL;
388                         if (wl_ioctl(dev, WLC_SET_MONITOR, &passive, sizeof(passive)) < 0)
389                                 return -EINVAL;
390                         if (wl_ioctl(dev, WLC_SET_WET, &wet, sizeof(wet)) < 0)
391                                 return -EINVAL;
392                         if (ap >= 0)
393                                 if (wl_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap)) < 0)
394                                         return -EINVAL;
395                         if (infra >= 0)
396                                 if (wl_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra)) < 0)
397                                         return -EINVAL;
398
399                         break;
400                                                 
401                 }
402                 case SIOCGIWMODE:
403                 {
404                         int ap, infra, wet, passive;
405
406                         if (wl_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap)) < 0)
407                                 return -EINVAL;
408                         if (wl_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra)) < 0)
409                                 return -EINVAL;
410                         if (wl_ioctl(dev, WLC_GET_PASSIVE, &passive, sizeof(passive)) < 0)
411                                 return -EINVAL;
412                         if (wl_ioctl(dev, WLC_GET_WET, &wet, sizeof(wet)) < 0)
413                                 return -EINVAL;
414
415                         if (passive) {
416                                 wrqu->mode = IW_MODE_MONITOR;
417                         } else if (!infra) {
418                                 wrqu->mode = IW_MODE_ADHOC;
419                         } else {
420                                 if (ap) {
421                                         wrqu->mode = IW_MODE_MASTER;
422                                 } else {
423                                         if (wet) {
424                                                 wrqu->mode = IW_MODE_REPEAT;
425                                         } else {
426                                                 wrqu->mode = IW_MODE_INFRA;
427                                         }
428                                 }
429                         }
430                         break;
431                 }
432                 default:
433                 {
434                         return -EINVAL;
435                 }
436         }
437         
438         return 0;
439 }
440
441 static const iw_handler  wlcompat_handler[] = {
442         NULL,                   /* SIOCSIWCOMMIT */
443         wlcompat_ioctl,         /* SIOCGIWNAME */
444         NULL,                   /* SIOCSIWNWID */
445         NULL,                   /* SIOCGIWNWID */
446         wlcompat_ioctl,         /* SIOCSIWFREQ */
447         wlcompat_ioctl,         /* SIOCGIWFREQ */
448         wlcompat_ioctl,         /* SIOCSIWMODE */
449         wlcompat_ioctl,         /* SIOCGIWMODE */
450         NULL,                   /* SIOCSIWSENS */
451         NULL,                   /* SIOCGIWSENS */
452         NULL,                   /* SIOCSIWRANGE, unused */
453         wlcompat_ioctl,         /* SIOCGIWRANGE */
454         NULL,                   /* SIOCSIWPRIV */
455         NULL,                   /* SIOCGIWPRIV */
456         NULL,                   /* SIOCSIWSTATS */
457         NULL,                   /* SIOCGIWSTATS */
458         iw_handler_set_spy,     /* SIOCSIWSPY */
459         iw_handler_get_spy,     /* SIOCGIWSPY */
460         iw_handler_set_thrspy,  /* SIOCSIWTHRSPY */
461         iw_handler_get_thrspy,  /* SIOCGIWTHRSPY */
462         NULL,                   /* SIOCSIWAP */
463         wlcompat_ioctl,         /* SIOCGIWAP */
464         NULL,                   /* -- hole -- */
465         NULL,                   /* SIOCGIWAPLIST */
466         wlcompat_set_scan,      /* SIOCSIWSCAN */
467         wlcompat_get_scan,      /* SIOCGIWSCAN */
468         wlcompat_ioctl,         /* SIOCSIWESSID */
469         wlcompat_ioctl,         /* SIOCGIWESSID */
470         NULL,                   /* SIOCSIWNICKN */
471         NULL,                   /* SIOCGIWNICKN */
472         NULL,                   /* -- hole -- */
473         NULL,                   /* -- hole -- */
474         NULL,                   /* SIOCSIWRATE */
475         NULL,                   /* SIOCGIWRATE */
476         wlcompat_ioctl,         /* SIOCSIWRTS */
477         wlcompat_ioctl,         /* SIOCGIWRTS */
478         wlcompat_ioctl,         /* SIOCSIWFRAG */
479         wlcompat_ioctl,         /* SIOCGIWFRAG */
480         wlcompat_ioctl,         /* SIOCSIWTXPOW */
481         wlcompat_ioctl,         /* SIOCGIWTXPOW */
482         NULL,                   /* SIOCSIWRETRY */
483         NULL,                   /* SIOCGIWRETRY */
484         NULL,                   /* SIOCSIWENCODE */
485         wlcompat_ioctl,         /* SIOCGIWENCODE */
486 };
487
488 static const struct iw_handler_def      wlcompat_handler_def =
489 {
490         .standard       = (iw_handler *) wlcompat_handler,
491         .num_standard   = sizeof(wlcompat_handler)/sizeof(iw_handler),
492         .private        = NULL,
493         .num_private    = 0,
494         .private_args   = NULL, 
495         .num_private_args = 0,
496 };
497
498 #ifdef DEBUG
499 static int (*old_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
500 void print_buffer(int len, unsigned char *buf) {
501         int x;
502         if (buf != NULL) {
503                 for (x=0;x<len && x<180 ;x++) {
504                         if ((x % 4) == 0)
505                                 printk(" ");
506                         printk("%02X",buf[x]);
507                 }
508         } else {
509                 printk(" NULL");
510         }
511         printk("\n");
512
513 }
514 static int new_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) {
515         int ret = 0;
516         printk("dev: %s ioctl: 0x%04x\n",dev->name,cmd);
517         if (cmd==SIOCDEVPRIVATE) {
518                 wl_ioctl_t *ioc = (wl_ioctl_t *)ifr->ifr_data;
519                 unsigned char *buf = ioc->buf;
520                 printk("   cmd: %d buf: 0x%08x len: %d\n",ioc->cmd,&(ioc->buf),ioc->len);
521                 printk("   send: ->");
522                 print_buffer(ioc->len, buf);
523                 ret = old_ioctl(dev,ifr,cmd);
524                 printk("   recv: ->");
525                 print_buffer(ioc->len, buf);
526                 printk("   ret: %d\n", ret);
527         } else {
528                 ret = old_ioctl(dev,ifr,cmd);
529         }
530         return ret;
531 }
532 #endif
533
534 static int __init wlcompat_init()
535 {
536         dev = dev_get_by_name("eth1");
537 #ifdef DEBUG
538         old_ioctl = dev->do_ioctl;
539         dev->do_ioctl = new_ioctl;
540 #endif
541         dev->wireless_handlers = (struct iw_handler_def *)&wlcompat_handler_def;
542         return 0;
543 }
544
545 static void __exit wlcompat_exit()
546 {
547         dev->wireless_handlers = NULL;
548 #ifdef DEBUG
549         dev->do_ioctl = old_ioctl;
550 #endif
551         return;
552 }
553
554 EXPORT_NO_SYMBOLS;
555 MODULE_AUTHOR("openwrt.org");
556 MODULE_LICENSE("GPL");
557
558 module_init(wlcompat_init);
559 module_exit(wlcompat_exit);