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