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