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