]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/hostap/hostap.c
366cefbaca5918170221efcbfdaef43a81e99f71
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / hostap / hostap.c
1 /*
2  * Host AP (software wireless LAN access point) driver for
3  * Intersil Prism2/2.5/3 - hostap.o module, common routines
4  *
5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6  * <jkmaline@cc.hut.fi>
7  * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation. See README and COPYING for
12  * more details.
13  */
14
15 #include <linux/config.h>
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/proc_fs.h>
21 #include <linux/if_arp.h>
22 #include <linux/delay.h>
23 #include <linux/random.h>
24 #include <linux/workqueue.h>
25 #include <linux/kmod.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/wireless.h>
28 #include <net/iw_handler.h>
29 #include <asm/uaccess.h>
30
31 #include "hostap_wlan.h"
32 #include "hostap_80211.h"
33 #include "hostap_ap.h"
34 #include "hostap.h"
35 #include "hostap_crypt.h"
36
37 MODULE_AUTHOR("Jouni Malinen");
38 MODULE_DESCRIPTION("Host AP common routines");
39 MODULE_LICENSE("GPL");
40
41 /* Old hostap_crypt module is now part of hostap module. */
42 #include "hostap_crypt.c"
43
44 #define TX_TIMEOUT (2 * HZ)
45
46 #define PRISM2_MAX_FRAME_SIZE 2304
47 #define PRISM2_MIN_MTU 256
48 /* FIX: */
49 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
50
51
52 /* hostap.c */
53 static int prism2_wds_add(local_info_t *local, u8 *remote_addr,
54                           int rtnl_locked);
55 static int prism2_wds_del(local_info_t *local, u8 *remote_addr,
56                           int rtnl_locked, int do_not_remove);
57
58 /* hostap_ap.c */
59 static int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
60                                   struct iw_quality qual[], int buf_size,
61                                   int aplist);
62 static int prism2_ap_translate_scan(struct net_device *dev, char *buffer);
63 static int prism2_hostapd(struct ap_data *ap,
64                           struct prism2_hostapd_param *param);
65 static void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
66                                 struct prism2_crypt_data ***crypt);
67 static void ap_control_kickall(struct ap_data *ap);
68 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
69 static int ap_control_add_mac(struct mac_restrictions *mac_restrictions,
70                               u8 *mac);
71 static int ap_control_del_mac(struct mac_restrictions *mac_restrictions,
72                               u8 *mac);
73 static void ap_control_flush_macs(struct mac_restrictions *mac_restrictions);
74 static int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev,
75                                u8 *mac);
76 #endif /* !PRISM2_NO_KERNEL_IEEE80211_MGMT */
77
78
79 static const long freq_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
80                                   2447, 2452, 2457, 2462, 2467, 2472, 2484 };
81 #define FREQ_COUNT (sizeof(freq_list) / sizeof(freq_list[0]))
82
83
84 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
85 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
86 static unsigned char rfc1042_header[] =
87 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
88 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
89 static unsigned char bridge_tunnel_header[] =
90 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
91 /* No encapsulation header if EtherType < 0x600 (=length) */
92
93
94 /* FIX: these could be compiled separately and linked together to hostap.o */
95 #include "hostap_ap.c"
96 #include "hostap_info.c"
97 #include "hostap_ioctl.c"
98 #include "hostap_proc.c"
99 #include "hostap_80211_rx.c"
100 #include "hostap_80211_tx.c"
101
102
103 struct net_device * hostap_add_interface(struct local_info *local,
104                                          int type, int rtnl_locked,
105                                          const char *prefix,
106                                          const char *name)
107 {
108         struct net_device *dev, *mdev;
109         struct hostap_interface *iface;
110         int ret;
111
112         dev = alloc_etherdev(sizeof(struct hostap_interface));
113         if (dev == NULL)
114                 return NULL;
115
116         iface = netdev_priv(dev);
117         iface->dev = dev;
118         iface->local = local;
119         iface->type = type;
120         list_add(&iface->list, &local->hostap_interfaces);
121
122         mdev = local->dev;
123         memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
124         dev->base_addr = mdev->base_addr;
125         dev->irq = mdev->irq;
126         dev->mem_start = mdev->mem_start;
127         dev->mem_end = mdev->mem_end;
128
129         hostap_setup_dev(dev, local, 0);
130         dev->destructor = free_netdev;
131
132         sprintf(dev->name, "%s%s", prefix, name);
133         if (!rtnl_locked)
134                 rtnl_lock();
135
136         ret = 0;
137         if (strchr(dev->name, '%'))
138                 ret = dev_alloc_name(dev, dev->name);
139
140         if (ret >= 0)
141                 ret = register_netdevice(dev);
142
143         if (!rtnl_locked)
144                 rtnl_unlock();
145
146         if (ret < 0) {
147                 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
148                        dev->name);
149                 free_netdev(dev);
150                 return NULL;
151         }
152
153         printk(KERN_DEBUG "%s: registered netdevice %s\n",
154                mdev->name, dev->name);
155
156         return dev;
157 }
158
159
160 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
161                              int remove_from_list)
162 {
163         struct hostap_interface *iface;
164
165         if (!dev)
166                 return;
167
168         iface = netdev_priv(dev);
169
170         if (remove_from_list) {
171                 list_del(&iface->list);
172         }
173
174         if (dev == iface->local->ddev)
175                 iface->local->ddev = NULL;
176         else if (dev == iface->local->apdev)
177                 iface->local->apdev = NULL;
178         else if (dev == iface->local->stadev)
179                 iface->local->stadev = NULL;
180
181         if (rtnl_locked)
182                 unregister_netdevice(dev);
183         else
184                 unregister_netdev(dev);
185
186         /* dev->destructor = free_netdev() will free the device data, including
187          * private data, when removing the device */
188 }
189
190
191 static inline int prism2_wds_special_addr(u8 *addr)
192 {
193         if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
194                 return 0;
195
196         return 1;
197 }
198
199
200 static int prism2_wds_add(local_info_t *local, u8 *remote_addr,
201                           int rtnl_locked)
202 {
203         struct net_device *dev;
204         struct list_head *ptr;
205         struct hostap_interface *iface, *empty, *match;
206
207         empty = match = NULL;
208         read_lock_bh(&local->iface_lock);
209         list_for_each(ptr, &local->hostap_interfaces) {
210                 iface = list_entry(ptr, struct hostap_interface, list);
211                 if (iface->type != HOSTAP_INTERFACE_WDS)
212                         continue;
213
214                 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
215                         empty = iface;
216                 else if (memcmp(iface->u.wds.remote_addr, remote_addr,
217                                 ETH_ALEN) == 0) {
218                         match = iface;
219                         break;
220                 }
221         }
222         if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
223                 /* take pre-allocated entry into use */
224                 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
225                 read_unlock_bh(&local->iface_lock);
226                 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
227                        local->dev->name, empty->dev->name);
228                 return 0;
229         }
230         read_unlock_bh(&local->iface_lock);
231
232         if (!prism2_wds_special_addr(remote_addr)) {
233                 if (match)
234                         return -EEXIST;
235                 hostap_add_sta(local->ap, remote_addr);
236         }
237
238         if (local->wds_connections >= local->wds_max_connections)
239                 return -ENOBUFS;
240
241         /* verify that there is room for wds# postfix in the interface name */
242         if (strlen(local->dev->name) > IFNAMSIZ - 5) {
243                 printk(KERN_DEBUG "'%s' too long base device name\n",
244                        local->dev->name);
245                 return -EINVAL;
246         }
247
248         dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
249                                    local->ddev->name, "wds%d");
250         if (dev == NULL)
251                 return -ENOMEM;
252
253         iface = netdev_priv(dev);
254         memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
255
256         local->wds_connections++;
257
258         return 0;
259 }
260
261
262 static int prism2_wds_del(local_info_t *local, u8 *remote_addr,
263                           int rtnl_locked, int do_not_remove)
264 {
265         unsigned long flags;
266         struct list_head *ptr;
267         struct hostap_interface *iface, *selected = NULL;
268
269         write_lock_irqsave(&local->iface_lock, flags);
270         list_for_each(ptr, &local->hostap_interfaces) {
271                 iface = list_entry(ptr, struct hostap_interface, list);
272                 if (iface->type != HOSTAP_INTERFACE_WDS)
273                         continue;
274
275                 if (memcmp(iface->u.wds.remote_addr, remote_addr,
276                            ETH_ALEN) == 0) {
277                         selected = iface;
278                         break;
279                 }
280         }
281         if (selected && !do_not_remove)
282                 list_del(&selected->list);
283         write_unlock_irqrestore(&local->iface_lock, flags);
284
285         if (selected) {
286                 if (do_not_remove)
287                         memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
288                 else {
289                         hostap_remove_interface(selected->dev, rtnl_locked, 0);
290                         local->wds_connections--;
291                 }
292         }
293
294         return selected ? 0 : -ENODEV;
295 }
296
297
298 u16 hostap_tx_callback_register(local_info_t *local,
299                                 void (*func)(struct sk_buff *, int ok, void *),
300                                 void *data)
301 {
302         unsigned long flags;
303         struct hostap_tx_callback_info *entry;
304
305         entry = (struct hostap_tx_callback_info *) kmalloc(sizeof(*entry),
306                                                            GFP_ATOMIC);
307         if (entry == NULL)
308                 return 0;
309
310         entry->func = func;
311         entry->data = data;
312
313         spin_lock_irqsave(&local->lock, flags);
314         entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
315         entry->next = local->tx_callback;
316         local->tx_callback = entry;
317         spin_unlock_irqrestore(&local->lock, flags);
318
319         return entry->idx;
320 }
321
322
323 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
324 {
325         unsigned long flags;
326         struct hostap_tx_callback_info *cb, *prev = NULL;
327
328         spin_lock_irqsave(&local->lock, flags);
329         cb = local->tx_callback;
330         while (cb != NULL && cb->idx != idx) {
331                 prev = cb;
332                 cb = cb->next;
333         }
334         if (cb) {
335                 if (prev == NULL)
336                         local->tx_callback = cb->next;
337                 else
338                         prev->next = cb->next;
339                 kfree(cb);
340         }
341         spin_unlock_irqrestore(&local->lock, flags);
342
343         return cb ? 0 : -1;
344 }
345
346
347 /* val is in host byte order */
348 int hostap_set_word(struct net_device *dev, int rid, u16 val)
349 {
350         struct hostap_interface *iface;
351         u16 tmp = cpu_to_le16(val);
352         iface = netdev_priv(dev);
353         return iface->local->func->set_rid(dev, rid, &tmp, 2);
354 }
355
356
357 int hostap_set_string(struct net_device *dev, int rid, const char *val)
358 {
359         struct hostap_interface *iface;
360         char buf[MAX_SSID_LEN + 2];
361         int len;
362
363         iface = netdev_priv(dev);
364         len = strlen(val);
365         if (len > MAX_SSID_LEN)
366                 return -1;
367         memset(buf, 0, sizeof(buf));
368         buf[0] = len; /* little endian 16 bit word */
369         memcpy(buf + 2, val, len);
370
371         return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
372 }
373
374
375 u16 hostap_get_porttype(local_info_t *local)
376 {
377         if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
378                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
379         if (local->iw_mode == IW_MODE_ADHOC)
380                 return HFA384X_PORTTYPE_IBSS;
381         if (local->iw_mode == IW_MODE_INFRA)
382                 return HFA384X_PORTTYPE_BSS;
383         if (local->iw_mode == IW_MODE_REPEAT)
384                 return HFA384X_PORTTYPE_WDS;
385         if (local->iw_mode == IW_MODE_MONITOR)
386                 return HFA384X_PORTTYPE_PSEUDO_IBSS;
387         return HFA384X_PORTTYPE_HOSTAP;
388 }
389
390
391 int hostap_set_encryption(local_info_t *local)
392 {
393         u16 val, old_val;
394         int i, keylen, len, idx;
395         char keybuf[WEP_KEY_LEN + 1];
396         enum { NONE, WEP, OTHER } encrypt_type;
397
398         idx = local->tx_keyidx;
399         if (local->crypt[idx] == NULL || local->crypt[idx]->ops == NULL)
400                 encrypt_type = NONE;
401         else if (strcmp(local->crypt[idx]->ops->name, "WEP") == 0)
402                 encrypt_type = WEP;
403         else
404                 encrypt_type = OTHER;
405
406         if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
407                                  1) < 0) {
408                 printk(KERN_DEBUG "Could not read current WEP flags.\n");
409                 goto fail;
410         }
411         le16_to_cpus(&val);
412         old_val = val;
413
414         if (encrypt_type != NONE || local->privacy_invoked)
415                 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
416         else
417                 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
418
419         if (local->open_wep || encrypt_type == NONE ||
420             ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
421                 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
422         else
423                 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
424
425         if ((encrypt_type != NONE || local->privacy_invoked) &&
426             (encrypt_type == OTHER || local->host_encrypt))
427                 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
428         else
429                 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
430         if ((encrypt_type != NONE || local->privacy_invoked) &&
431             (encrypt_type == OTHER || local->host_decrypt))
432                 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
433         else
434                 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
435
436         if (val != old_val &&
437             hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
438                 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
439                        val);
440                 goto fail;
441         }
442
443         if (encrypt_type != WEP)
444                 return 0;
445
446         /* 104-bit support seems to require that all the keys are set to the
447          * same keylen */
448         keylen = 6; /* first 5 octets */
449         len = local->crypt[idx]->ops->get_key(keybuf, sizeof(keybuf),
450                                               NULL, local->crypt[idx]->priv);
451         if (idx >= 0 && idx < WEP_KEYS && len > 5)
452                 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
453
454         for (i = 0; i < WEP_KEYS; i++) {
455                 memset(keybuf, 0, sizeof(keybuf));
456                 if (local->crypt[i]) {
457                         (void) local->crypt[i]->ops->get_key(
458                                 keybuf, sizeof(keybuf),
459                                 NULL, local->crypt[i]->priv);
460                 }
461                 if (local->func->set_rid(local->dev,
462                                          HFA384X_RID_CNFDEFAULTKEY0 + i,
463                                          keybuf, keylen)) {
464                         printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
465                                i, keylen);
466                         goto fail;
467                 }
468         }
469         if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
470                 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
471                 goto fail;
472         }
473
474         return 0;
475
476  fail:
477         printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
478         return -1;
479 }
480
481
482 int hostap_set_antsel(local_info_t *local)
483 {
484         u16 val;
485         int ret = 0;
486
487         if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
488             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
489                              HFA386X_CR_TX_CONFIGURE,
490                              NULL, &val) == 0) {
491                 val &= ~(BIT(2) | BIT(1));
492                 switch (local->antsel_tx) {
493                 case HOSTAP_ANTSEL_DIVERSITY:
494                         val |= BIT(1);
495                         break;
496                 case HOSTAP_ANTSEL_LOW:
497                         break;
498                 case HOSTAP_ANTSEL_HIGH:
499                         val |= BIT(2);
500                         break;
501                 }
502
503                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
504                                      HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
505                         printk(KERN_INFO "%s: setting TX AntSel failed\n",
506                                local->dev->name);
507                         ret = -1;
508                 }
509         }
510
511         if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
512             local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
513                              HFA386X_CR_RX_CONFIGURE,
514                              NULL, &val) == 0) {
515                 val &= ~(BIT(1) | BIT(0));
516                 switch (local->antsel_rx) {
517                 case HOSTAP_ANTSEL_DIVERSITY:
518                         break;
519                 case HOSTAP_ANTSEL_LOW:
520                         val |= BIT(0);
521                         break;
522                 case HOSTAP_ANTSEL_HIGH:
523                         val |= BIT(0) | BIT(1);
524                         break;
525                 }
526
527                 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
528                                      HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
529                         printk(KERN_INFO "%s: setting RX AntSel failed\n",
530                                local->dev->name);
531                         ret = -1;
532                 }
533         }
534
535         return ret;
536 }
537
538
539 int hostap_set_roaming(local_info_t *local)
540 {
541         u16 val;
542
543         switch (local->host_roaming) {
544         case 1:
545                 val = HFA384X_ROAMING_HOST;
546                 break;
547         case 2:
548                 val = HFA384X_ROAMING_DISABLED;
549                 break;
550         case 0:
551         default:
552                 val = HFA384X_ROAMING_FIRMWARE;
553                 break;
554         }
555
556         return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
557 }
558
559
560 int hostap_set_auth_algs(local_info_t *local)
561 {
562         int val = local->auth_algs;
563         /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
564          * set to include both Open and Shared Key flags. It tries to use
565          * Shared Key authentication in that case even if WEP keys are not
566          * configured.. STA f/w v0.7.6 is able to handle such configuration,
567          * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
568         if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
569             val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
570                 val = PRISM2_AUTH_OPEN;
571
572         if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
573                 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
574                        "failed\n", local->dev->name, local->auth_algs);
575                 return -EINVAL;
576         }
577
578         return 0;
579 }
580
581
582 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
583 {
584         u16 status, fc;
585
586         status = __le16_to_cpu(rx->status);
587
588         printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
589                "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
590                "jiffies=%ld\n",
591                name, status, (status >> 8) & 0x07, status >> 13, status & 1,
592                rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
593
594         fc = __le16_to_cpu(rx->frame_control);
595         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
596                "data_len=%d%s%s\n",
597                fc, WLAN_FC_GET_TYPE(fc), WLAN_FC_GET_STYPE(fc),
598                __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
599                __le16_to_cpu(rx->data_len),
600                fc & WLAN_FC_TODS ? " [ToDS]" : "",
601                fc & WLAN_FC_FROMDS ? " [FromDS]" : "");
602
603         printk(KERN_DEBUG "   A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
604                MACSTR "\n",
605                MAC2STR(rx->addr1), MAC2STR(rx->addr2), MAC2STR(rx->addr3),
606                MAC2STR(rx->addr4));
607
608         printk(KERN_DEBUG "   dst=" MACSTR " src=" MACSTR " len=%d\n",
609                MAC2STR(rx->dst_addr), MAC2STR(rx->src_addr),
610                __be16_to_cpu(rx->len));
611 }
612
613
614 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
615 {
616         u16 fc;
617
618         printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
619                "tx_control=0x%04x; jiffies=%ld\n",
620                name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
621                __le16_to_cpu(tx->tx_control), jiffies);
622
623         fc = __le16_to_cpu(tx->frame_control);
624         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
625                "data_len=%d%s%s\n",
626                fc, WLAN_FC_GET_TYPE(fc), WLAN_FC_GET_STYPE(fc),
627                __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
628                __le16_to_cpu(tx->data_len),
629                fc & WLAN_FC_TODS ? " [ToDS]" : "",
630                fc & WLAN_FC_FROMDS ? " [FromDS]" : "");
631
632         printk(KERN_DEBUG "   A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
633                MACSTR "\n",
634                MAC2STR(tx->addr1), MAC2STR(tx->addr2), MAC2STR(tx->addr3),
635                MAC2STR(tx->addr4));
636
637         printk(KERN_DEBUG "   dst=" MACSTR " src=" MACSTR " len=%d\n",
638                MAC2STR(tx->dst_addr), MAC2STR(tx->src_addr),
639                __be16_to_cpu(tx->len));
640 }
641
642
643 int hostap_80211_header_parse(struct sk_buff *skb, unsigned char *haddr)
644 {
645         memcpy(haddr, skb->mac.raw + 10, ETH_ALEN); /* addr2 */
646         return ETH_ALEN;
647 }
648
649
650 int hostap_80211_prism_header_parse(struct sk_buff *skb, unsigned char *haddr)
651 {
652         if (*(u32 *)skb->mac.raw == LWNG_CAP_DID_BASE) {
653                 memcpy(haddr, skb->mac.raw +
654                        sizeof(struct linux_wlan_ng_prism_hdr) + 10,
655                        ETH_ALEN); /* addr2 */
656         } else { /* (*(u32 *)skb->mac.raw == htonl(LWNG_CAPHDR_VERSION)) */
657                 memcpy(haddr, skb->mac.raw +
658                        sizeof(struct linux_wlan_ng_cap_hdr) + 10,
659                        ETH_ALEN); /* addr2 */
660         }
661         return ETH_ALEN;
662 }
663
664
665 int hostap_80211_get_hdrlen(u16 fc)
666 {
667         int hdrlen = 24;
668
669         switch (WLAN_FC_GET_TYPE(fc)) {
670         case WLAN_FC_TYPE_DATA:
671                 if ((fc & WLAN_FC_FROMDS) && (fc & WLAN_FC_TODS))
672                         hdrlen = 30; /* Addr4 */
673                 break;
674         case WLAN_FC_TYPE_CTRL:
675                 switch (WLAN_FC_GET_STYPE(fc)) {
676                 case WLAN_FC_STYPE_CTS:
677                 case WLAN_FC_STYPE_ACK:
678                         hdrlen = 10;
679                         break;
680                 default:
681                         hdrlen = 16;
682                         break;
683                 }
684                 break;
685         }
686
687         return hdrlen;
688 }
689
690
691 struct net_device_stats *hostap_get_stats(struct net_device *dev)
692 {
693         struct hostap_interface *iface;
694         iface = netdev_priv(dev);
695         return &iface->stats;
696 }
697
698
699 static int prism2_close(struct net_device *dev)
700 {
701         struct hostap_interface *iface;
702         local_info_t *local;
703
704         PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
705
706         iface = netdev_priv(dev);
707         local = iface->local;
708
709         if (dev == local->ddev) {
710                 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
711         }
712 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
713         if (!local->hostapd && dev == local->dev &&
714             (!local->func->card_present || local->func->card_present(local)) &&
715             local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
716                 hostap_deauth_all_stas(dev, local->ap, 1);
717 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
718
719         if (local->func->dev_close && local->func->dev_close(local))
720                 return 0;
721
722         if (dev == local->dev) {
723                 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
724         }
725
726         if (netif_running(dev)) {
727                 netif_stop_queue(dev);
728                 netif_device_detach(dev);
729         }
730
731         flush_scheduled_work();
732
733         module_put(local->hw_module);
734
735         local->num_dev_open--;
736
737         if (dev != local->dev && local->dev->flags & IFF_UP &&
738             local->master_dev_auto_open && local->num_dev_open == 1) {
739                 /* Close master radio interface automatically if it was also
740                  * opened automatically and we are now closing the last
741                  * remaining non-master device. */
742                 dev_close(local->dev);
743         }
744
745         return 0;
746 }
747
748
749 static int prism2_open(struct net_device *dev)
750 {
751         struct hostap_interface *iface;
752         local_info_t *local;
753
754         PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
755
756         iface = netdev_priv(dev);
757         local = iface->local;
758
759         if (local->no_pri) {
760                 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
761                        "f/w\n", dev->name);
762                 return 1;
763         }
764
765         if ((local->func->card_present && !local->func->card_present(local)) ||
766             local->hw_downloading)
767                 return -ENODEV;
768
769         if (local->func->dev_open && local->func->dev_open(local))
770                 return 1;
771
772         if (!try_module_get(local->hw_module))
773                 return -ENODEV;
774         local->num_dev_open++;
775
776         if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
777                 printk(KERN_WARNING "%s: could not enable MAC port\n",
778                        dev->name);
779                 prism2_close(dev);
780                 return 1;
781         }
782         if (!local->dev_enabled)
783                 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
784         local->dev_enabled = 1;
785
786         if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
787                 /* Master radio interface is needed for all operation, so open
788                  * it automatically when any virtual net_device is opened. */
789                 local->master_dev_auto_open = 1;
790                 dev_open(local->dev);
791         }
792
793         netif_device_attach(dev);
794         netif_start_queue(dev);
795
796         return 0;
797 }
798
799
800 static int prism2_set_mac_address(struct net_device *dev, void *p)
801 {
802         struct hostap_interface *iface;
803         local_info_t *local;
804         struct list_head *ptr;
805         struct sockaddr *addr = p;
806
807         iface = netdev_priv(dev);
808         local = iface->local;
809
810         if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
811                                  ETH_ALEN) < 0 || local->func->reset_port(dev))
812                 return -EINVAL;
813
814         read_lock_bh(&local->iface_lock);
815         list_for_each(ptr, &local->hostap_interfaces) {
816                 iface = list_entry(ptr, struct hostap_interface, list);
817                 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
818         }
819         memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
820         read_unlock_bh(&local->iface_lock);
821
822         return 0;
823 }
824
825
826 /* TODO: to be further implemented as soon as Prism2 fully supports
827  *       GroupAddresses and correct documentation is available */
828 void hostap_set_multicast_list_queue(void *data)
829 {
830         struct net_device *dev = (struct net_device *) data;
831         struct hostap_interface *iface;
832         local_info_t *local;
833
834         iface = netdev_priv(dev);
835         local = iface->local;
836         if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
837                             local->is_promisc)) {
838                 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
839                        dev->name, local->is_promisc ? "en" : "dis");
840         }
841 }
842
843
844 static void hostap_set_multicast_list(struct net_device *dev)
845 {
846 #if 0
847         /* FIX: promiscuous mode seems to be causing a lot of problems with
848          * some station firmware versions (FCSErr frames, invalid MACPort, etc.
849          * corrupted incoming frames). This code is now commented out while the
850          * problems are investigated. */
851         struct hostap_interface *iface;
852         local_info_t *local;
853
854         iface = netdev_priv(dev);
855         local = iface->local;
856         if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
857                 local->is_promisc = 1;
858         } else {
859                 local->is_promisc = 0;
860         }
861
862         schedule_work(&local->set_multicast_list_queue);
863 #endif
864 }
865
866
867 static int prism2_change_mtu(struct net_device *dev, int new_mtu)
868 {
869         if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
870                 return -EINVAL;
871
872         dev->mtu = new_mtu;
873         return 0;
874 }
875
876
877 static void prism2_tx_timeout(struct net_device *dev)
878 {
879         struct hostap_interface *iface;
880         local_info_t *local;
881         struct hfa384x_regs regs;
882
883         iface = netdev_priv(dev);
884         local = iface->local;
885
886         printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
887         netif_stop_queue(local->dev);
888
889         local->func->read_regs(dev, &regs);
890         printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
891                "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
892                dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
893                regs.swsupport0);
894
895         local->func->schedule_reset(local);
896 }
897
898
899 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
900                       int main_dev)
901 {
902         struct hostap_interface *iface;
903
904         iface = netdev_priv(dev);
905         ether_setup(dev);
906
907         /* kernel callbacks */
908         dev->get_stats = hostap_get_stats;
909         if (iface) {
910                 /* Currently, we point to the proper spy_data only on
911                  * the main_dev. This could be fixed. Jean II */
912                 iface->wireless_data.spy_data = &iface->spy_data;
913                 dev->wireless_data = &iface->wireless_data;
914         }
915         dev->wireless_handlers =
916                 (struct iw_handler_def *) &hostap_iw_handler_def;
917         dev->do_ioctl = hostap_ioctl;
918         dev->open = prism2_open;
919         dev->stop = prism2_close;
920         dev->hard_start_xmit = hostap_data_start_xmit;
921         dev->set_mac_address = prism2_set_mac_address;
922         dev->set_multicast_list = hostap_set_multicast_list;
923         dev->change_mtu = prism2_change_mtu;
924         dev->tx_timeout = prism2_tx_timeout;
925         dev->watchdog_timeo = TX_TIMEOUT;
926
927         dev->mtu = local->mtu;
928         if (!main_dev) {
929                 /* use main radio device queue */
930                 dev->tx_queue_len = 0;
931         }
932
933         SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
934
935         netif_stop_queue(dev);
936 }
937
938
939 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
940 {
941         struct net_device *dev = local->dev;
942
943         if (local->apdev)
944                 return -EEXIST;
945
946         printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
947
948         local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
949                                             rtnl_locked, local->ddev->name,
950                                             "ap");
951         if (local->apdev == NULL)
952                 return -ENOMEM;
953
954         local->apdev->hard_start_xmit = hostap_mgmt_start_xmit;
955         local->apdev->type = ARPHRD_IEEE80211;
956         local->apdev->hard_header_parse = hostap_80211_header_parse;
957
958         return 0;
959 }
960
961
962 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
963 {
964         struct net_device *dev = local->dev;
965
966         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
967
968         hostap_remove_interface(local->apdev, rtnl_locked, 1);
969         local->apdev = NULL;
970
971         return 0;
972 }
973
974
975 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
976 {
977         struct net_device *dev = local->dev;
978
979         if (local->stadev)
980                 return -EEXIST;
981
982         printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
983
984         local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
985                                              rtnl_locked, local->ddev->name,
986                                              "sta");
987         if (local->stadev == NULL)
988                 return -ENOMEM;
989
990         return 0;
991 }
992
993
994 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
995 {
996         struct net_device *dev = local->dev;
997
998         printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
999
1000         hostap_remove_interface(local->stadev, rtnl_locked, 1);
1001         local->stadev = NULL;
1002
1003         return 0;
1004 }
1005
1006
1007 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
1008 {
1009         int ret;
1010
1011         if (val < 0 || val > 1)
1012                 return -EINVAL;
1013
1014         if (local->hostapd == val)
1015                 return 0;
1016
1017         if (val) {
1018                 ret = hostap_enable_hostapd(local, rtnl_locked);
1019                 if (ret == 0)
1020                         local->hostapd = 1;
1021         } else {
1022                 local->hostapd = 0;
1023                 ret = hostap_disable_hostapd(local, rtnl_locked);
1024                 if (ret != 0)
1025                         local->hostapd = 1;
1026         }
1027
1028         return ret;
1029 }
1030
1031
1032 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
1033 {
1034         int ret;
1035
1036         if (val < 0 || val > 1)
1037                 return -EINVAL;
1038
1039         if (local->hostapd_sta == val)
1040                 return 0;
1041
1042         if (val) {
1043                 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
1044                 if (ret == 0)
1045                         local->hostapd_sta = 1;
1046         } else {
1047                 local->hostapd_sta = 0;
1048                 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
1049                 if (ret != 0)
1050                         local->hostapd_sta = 1;
1051         }
1052
1053
1054         return ret;
1055 }
1056
1057
1058 int prism2_update_comms_qual(struct net_device *dev)
1059 {
1060         struct hostap_interface *iface;
1061         local_info_t *local;
1062         int ret = 0;
1063         struct hfa384x_comms_quality sq;
1064
1065         iface = netdev_priv(dev);
1066         local = iface->local;
1067         if (!local->sta_fw_ver)
1068                 ret = -1;
1069         else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1070                 if (local->func->get_rid(local->dev,
1071                                          HFA384X_RID_DBMCOMMSQUALITY,
1072                                          &sq, sizeof(sq), 1) >= 0) {
1073                         local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1074                         local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1075                         local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1076                         local->last_comms_qual_update = jiffies;
1077                 } else
1078                         ret = -1;
1079         } else {
1080                 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1081                                          &sq, sizeof(sq), 1) >= 0) {
1082                         local->comms_qual = le16_to_cpu(sq.comm_qual);
1083                         local->avg_signal = HFA384X_LEVEL_TO_dBm(
1084                                 le16_to_cpu(sq.signal_level));
1085                         local->avg_noise = HFA384X_LEVEL_TO_dBm(
1086                                 le16_to_cpu(sq.noise_level));
1087                         local->last_comms_qual_update = jiffies;
1088                 } else
1089                         ret = -1;
1090         }
1091
1092         return ret;
1093 }
1094
1095
1096 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u8 stype,
1097                          u8 *body, size_t bodylen)
1098 {
1099         struct sk_buff *skb;
1100         struct hostap_ieee80211_mgmt *mgmt;
1101         struct hostap_skb_tx_data *meta;
1102         struct net_device *dev = local->dev;
1103
1104         skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1105         if (skb == NULL)
1106                 return -ENOMEM;
1107
1108         mgmt = (struct hostap_ieee80211_mgmt *)
1109                 skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1110         memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1111         mgmt->frame_control =
1112                 cpu_to_le16((WLAN_FC_TYPE_MGMT << 2) | (stype << 4));
1113         memcpy(mgmt->da, dst, ETH_ALEN);
1114         memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1115         memcpy(mgmt->bssid, dst, ETH_ALEN);
1116         if (body)
1117                 memcpy(skb_put(skb, bodylen), body, bodylen);
1118
1119         meta = (struct hostap_skb_tx_data *) skb->cb;
1120         memset(meta, 0, sizeof(*meta));
1121         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1122         meta->iface = netdev_priv(dev);
1123
1124         skb->dev = dev;
1125         skb->mac.raw = skb->nh.raw = skb->data;
1126         dev_queue_xmit(skb);
1127
1128         return 0;
1129 }
1130
1131
1132 int prism2_sta_deauth(local_info_t *local, u16 reason)
1133 {
1134         union iwreq_data wrqu;
1135         int ret;
1136
1137         if (local->iw_mode != IW_MODE_INFRA ||
1138             memcmp(local->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 ||
1139             memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
1140                 return 0;
1141
1142         reason = cpu_to_le16(reason);
1143         ret = prism2_sta_send_mgmt(local, local->bssid, WLAN_FC_STYPE_DEAUTH,
1144                                    (u8 *) &reason, 2);
1145         memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1146         wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1147         return ret;
1148 }
1149
1150
1151 struct proc_dir_entry *hostap_proc;
1152
1153 static int __init hostap_init(void)
1154 {
1155         hostap_crypto_init();
1156
1157         if (proc_net != NULL) {
1158                 hostap_proc = proc_mkdir("hostap", proc_net);
1159                 if (!hostap_proc)
1160                         printk(KERN_WARNING "Failed to mkdir "
1161                                "/proc/net/hostap\n");
1162         } else
1163                 hostap_proc = NULL;
1164
1165         return 0;
1166 }
1167
1168
1169 static void __exit hostap_exit(void)
1170 {
1171         if (hostap_proc != NULL) {
1172                 hostap_proc = NULL;
1173                 remove_proc_entry("hostap", proc_net);
1174         }
1175
1176         hostap_crypto_deinit();
1177 }
1178
1179
1180 EXPORT_SYMBOL(hostap_set_word);
1181 EXPORT_SYMBOL(hostap_set_string);
1182 EXPORT_SYMBOL(hostap_get_porttype);
1183 EXPORT_SYMBOL(hostap_set_encryption);
1184 EXPORT_SYMBOL(hostap_set_antsel);
1185 EXPORT_SYMBOL(hostap_set_roaming);
1186 EXPORT_SYMBOL(hostap_set_auth_algs);
1187 EXPORT_SYMBOL(hostap_dump_rx_header);
1188 EXPORT_SYMBOL(hostap_dump_tx_header);
1189 EXPORT_SYMBOL(hostap_80211_header_parse);
1190 EXPORT_SYMBOL(hostap_80211_prism_header_parse);
1191 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1192 EXPORT_SYMBOL(hostap_get_stats);
1193 EXPORT_SYMBOL(hostap_setup_dev);
1194 EXPORT_SYMBOL(hostap_proc);
1195 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1196 EXPORT_SYMBOL(hostap_set_hostapd);
1197 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1198 EXPORT_SYMBOL(hostap_add_interface);
1199 EXPORT_SYMBOL(hostap_remove_interface);
1200 EXPORT_SYMBOL(prism2_update_comms_qual);
1201
1202 module_init(hostap_init);
1203 module_exit(hostap_exit);