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