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