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