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