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