1/* Host AP driver Info Frame processing (part of hostap.o module) */
2
3#include <linux/if_arp.h>
4#include <linux/sched.h>
5#include <linux/slab.h>
6#include <linux/export.h>
7#include <linux/etherdevice.h>
8#include "hostap_wlan.h"
9#include "hostap.h"
10#include "hostap_ap.h"
11
12/* Called only as a tasklet (software IRQ) */
13static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
14				      int left)
15{
16	struct hfa384x_comm_tallies *tallies;
17
18	if (left < sizeof(struct hfa384x_comm_tallies)) {
19		printk(KERN_DEBUG "%s: too short (len=%d) commtallies "
20		       "info frame\n", local->dev->name, left);
21		return;
22	}
23
24	tallies = (struct hfa384x_comm_tallies *) buf;
25#define ADD_COMM_TALLIES(name) \
26local->comm_tallies.name += le16_to_cpu(tallies->name)
27	ADD_COMM_TALLIES(tx_unicast_frames);
28	ADD_COMM_TALLIES(tx_multicast_frames);
29	ADD_COMM_TALLIES(tx_fragments);
30	ADD_COMM_TALLIES(tx_unicast_octets);
31	ADD_COMM_TALLIES(tx_multicast_octets);
32	ADD_COMM_TALLIES(tx_deferred_transmissions);
33	ADD_COMM_TALLIES(tx_single_retry_frames);
34	ADD_COMM_TALLIES(tx_multiple_retry_frames);
35	ADD_COMM_TALLIES(tx_retry_limit_exceeded);
36	ADD_COMM_TALLIES(tx_discards);
37	ADD_COMM_TALLIES(rx_unicast_frames);
38	ADD_COMM_TALLIES(rx_multicast_frames);
39	ADD_COMM_TALLIES(rx_fragments);
40	ADD_COMM_TALLIES(rx_unicast_octets);
41	ADD_COMM_TALLIES(rx_multicast_octets);
42	ADD_COMM_TALLIES(rx_fcs_errors);
43	ADD_COMM_TALLIES(rx_discards_no_buffer);
44	ADD_COMM_TALLIES(tx_discards_wrong_sa);
45	ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
46	ADD_COMM_TALLIES(rx_message_in_msg_fragments);
47	ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
48#undef ADD_COMM_TALLIES
49}
50
51
52/* Called only as a tasklet (software IRQ) */
53static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
54				      int left)
55{
56	struct hfa384x_comm_tallies32 *tallies;
57
58	if (left < sizeof(struct hfa384x_comm_tallies32)) {
59		printk(KERN_DEBUG "%s: too short (len=%d) commtallies32 "
60		       "info frame\n", local->dev->name, left);
61		return;
62	}
63
64	tallies = (struct hfa384x_comm_tallies32 *) buf;
65#define ADD_COMM_TALLIES(name) \
66local->comm_tallies.name += le32_to_cpu(tallies->name)
67	ADD_COMM_TALLIES(tx_unicast_frames);
68	ADD_COMM_TALLIES(tx_multicast_frames);
69	ADD_COMM_TALLIES(tx_fragments);
70	ADD_COMM_TALLIES(tx_unicast_octets);
71	ADD_COMM_TALLIES(tx_multicast_octets);
72	ADD_COMM_TALLIES(tx_deferred_transmissions);
73	ADD_COMM_TALLIES(tx_single_retry_frames);
74	ADD_COMM_TALLIES(tx_multiple_retry_frames);
75	ADD_COMM_TALLIES(tx_retry_limit_exceeded);
76	ADD_COMM_TALLIES(tx_discards);
77	ADD_COMM_TALLIES(rx_unicast_frames);
78	ADD_COMM_TALLIES(rx_multicast_frames);
79	ADD_COMM_TALLIES(rx_fragments);
80	ADD_COMM_TALLIES(rx_unicast_octets);
81	ADD_COMM_TALLIES(rx_multicast_octets);
82	ADD_COMM_TALLIES(rx_fcs_errors);
83	ADD_COMM_TALLIES(rx_discards_no_buffer);
84	ADD_COMM_TALLIES(tx_discards_wrong_sa);
85	ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
86	ADD_COMM_TALLIES(rx_message_in_msg_fragments);
87	ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
88#undef ADD_COMM_TALLIES
89}
90
91
92/* Called only as a tasklet (software IRQ) */
93static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
94				    int left)
95{
96	if (local->tallies32)
97		prism2_info_commtallies32(local, buf, left);
98	else
99		prism2_info_commtallies16(local, buf, left);
100}
101
102
103#ifndef PRISM2_NO_STATION_MODES
104#ifndef PRISM2_NO_DEBUG
105static const char* hfa384x_linkstatus_str(u16 linkstatus)
106{
107	switch (linkstatus) {
108	case HFA384X_LINKSTATUS_CONNECTED:
109		return "Connected";
110	case HFA384X_LINKSTATUS_DISCONNECTED:
111		return "Disconnected";
112	case HFA384X_LINKSTATUS_AP_CHANGE:
113		return "Access point change";
114	case HFA384X_LINKSTATUS_AP_OUT_OF_RANGE:
115		return "Access point out of range";
116	case HFA384X_LINKSTATUS_AP_IN_RANGE:
117		return "Access point in range";
118	case HFA384X_LINKSTATUS_ASSOC_FAILED:
119		return "Association failed";
120	default:
121		return "Unknown";
122	}
123}
124#endif /* PRISM2_NO_DEBUG */
125
126
127/* Called only as a tasklet (software IRQ) */
128static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
129				    int left)
130{
131	u16 val;
132	int non_sta_mode;
133
134	/* Alloc new JoinRequests to occur since LinkStatus for the previous
135	 * has been received */
136	local->last_join_time = 0;
137
138	if (left != 2) {
139		printk(KERN_DEBUG "%s: invalid linkstatus info frame "
140		       "length %d\n", local->dev->name, left);
141		return;
142	}
143
144	non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
145		local->iw_mode == IW_MODE_REPEAT ||
146		local->iw_mode == IW_MODE_MONITOR;
147
148	val = buf[0] | (buf[1] << 8);
149	if (!non_sta_mode || val != HFA384X_LINKSTATUS_DISCONNECTED) {
150		PDEBUG(DEBUG_EXTRA, "%s: LinkStatus=%d (%s)\n",
151		       local->dev->name, val, hfa384x_linkstatus_str(val));
152	}
153
154	if (non_sta_mode) {
155		netif_carrier_on(local->dev);
156		netif_carrier_on(local->ddev);
157		return;
158	}
159
160	/* Get current BSSID later in scheduled task */
161	set_bit(PRISM2_INFO_PENDING_LINKSTATUS, &local->pending_info);
162	local->prev_link_status = val;
163	schedule_work(&local->info_queue);
164}
165
166
167static void prism2_host_roaming(local_info_t *local)
168{
169	struct hfa384x_join_request req;
170	struct net_device *dev = local->dev;
171	struct hfa384x_hostscan_result *selected, *entry;
172	int i;
173	unsigned long flags;
174
175	if (local->last_join_time &&
176	    time_before(jiffies, local->last_join_time + 10 * HZ)) {
177		PDEBUG(DEBUG_EXTRA, "%s: last join request has not yet been "
178		       "completed - waiting for it before issuing new one\n",
179		       dev->name);
180		return;
181	}
182
183	/* ScanResults are sorted: first ESS results in decreasing signal
184	 * quality then IBSS results in similar order.
185	 * Trivial roaming policy: just select the first entry.
186	 * This could probably be improved by adding hysteresis to limit
187	 * number of handoffs, etc.
188	 *
189	 * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
190	 * ScanResults */
191	spin_lock_irqsave(&local->lock, flags);
192	if (local->last_scan_results == NULL ||
193	    local->last_scan_results_count == 0) {
194		spin_unlock_irqrestore(&local->lock, flags);
195		PDEBUG(DEBUG_EXTRA, "%s: no scan results for host roaming\n",
196		       dev->name);
197		return;
198	}
199
200	selected = &local->last_scan_results[0];
201
202	if (local->preferred_ap[0] || local->preferred_ap[1] ||
203	    local->preferred_ap[2] || local->preferred_ap[3] ||
204	    local->preferred_ap[4] || local->preferred_ap[5]) {
205		/* Try to find preferred AP */
206		PDEBUG(DEBUG_EXTRA, "%s: Preferred AP BSSID %pM\n",
207		       dev->name, local->preferred_ap);
208		for (i = 0; i < local->last_scan_results_count; i++) {
209			entry = &local->last_scan_results[i];
210			if (memcmp(local->preferred_ap, entry->bssid, 6) == 0)
211			{
212				PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
213				       "selection\n", dev->name);
214				selected = entry;
215				break;
216			}
217		}
218	}
219
220	memcpy(req.bssid, selected->bssid, ETH_ALEN);
221	req.channel = selected->chid;
222	spin_unlock_irqrestore(&local->lock, flags);
223
224	PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
225	       " channel=%d\n",
226	       dev->name, req.bssid, le16_to_cpu(req.channel));
227	if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
228				 sizeof(req))) {
229		printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
230	}
231	local->last_join_time = jiffies;
232}
233
234
235static void hostap_report_scan_complete(local_info_t *local)
236{
237	union iwreq_data wrqu;
238
239	/* Inform user space about new scan results (just empty event,
240	 * SIOCGIWSCAN can be used to fetch data */
241	wrqu.data.length = 0;
242	wrqu.data.flags = 0;
243	wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
244
245	/* Allow SIOCGIWSCAN handling to occur since we have received
246	 * scanning result */
247	local->scan_timestamp = 0;
248}
249
250
251/* Called only as a tasklet (software IRQ) */
252static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
253				    int left)
254{
255	u16 *pos;
256	int new_count, i;
257	unsigned long flags;
258	struct hfa384x_scan_result *res;
259	struct hfa384x_hostscan_result *results, *prev;
260
261	if (left < 4) {
262		printk(KERN_DEBUG "%s: invalid scanresult info frame "
263		       "length %d\n", local->dev->name, left);
264		return;
265	}
266
267	pos = (u16 *) buf;
268	pos++;
269	pos++;
270	left -= 4;
271
272	new_count = left / sizeof(struct hfa384x_scan_result);
273	results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
274			  GFP_ATOMIC);
275	if (results == NULL)
276		return;
277
278	/* Convert to hostscan result format. */
279	res = (struct hfa384x_scan_result *) pos;
280	for (i = 0; i < new_count; i++) {
281		memcpy(&results[i], &res[i],
282		       sizeof(struct hfa384x_scan_result));
283		results[i].atim = 0;
284	}
285
286	spin_lock_irqsave(&local->lock, flags);
287	local->last_scan_type = PRISM2_SCAN;
288	prev = local->last_scan_results;
289	local->last_scan_results = results;
290	local->last_scan_results_count = new_count;
291	spin_unlock_irqrestore(&local->lock, flags);
292	kfree(prev);
293
294	hostap_report_scan_complete(local);
295
296	/* Perform rest of ScanResults handling later in scheduled task */
297	set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
298	schedule_work(&local->info_queue);
299}
300
301
302/* Called only as a tasklet (software IRQ) */
303static void prism2_info_hostscanresults(local_info_t *local,
304					unsigned char *buf, int left)
305{
306	int i, result_size, copy_len, new_count;
307	struct hfa384x_hostscan_result *results, *prev;
308	unsigned long flags;
309	__le16 *pos;
310	u8 *ptr;
311
312	wake_up_interruptible(&local->hostscan_wq);
313
314	if (left < 4) {
315		printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
316		       "length %d\n", local->dev->name, left);
317		return;
318	}
319
320	pos = (__le16 *) buf;
321	copy_len = result_size = le16_to_cpu(*pos);
322	if (result_size == 0) {
323		printk(KERN_DEBUG "%s: invalid result_size (0) in "
324		       "hostscanresults\n", local->dev->name);
325		return;
326	}
327	if (copy_len > sizeof(struct hfa384x_hostscan_result))
328		copy_len = sizeof(struct hfa384x_hostscan_result);
329
330	pos++;
331	pos++;
332	left -= 4;
333	ptr = (u8 *) pos;
334
335	new_count = left / result_size;
336	results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
337			  GFP_ATOMIC);
338	if (results == NULL)
339		return;
340
341	for (i = 0; i < new_count; i++) {
342		memcpy(&results[i], ptr, copy_len);
343		ptr += result_size;
344		left -= result_size;
345	}
346
347	if (left) {
348		printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
349		       local->dev->name, left, result_size);
350	}
351
352	spin_lock_irqsave(&local->lock, flags);
353	local->last_scan_type = PRISM2_HOSTSCAN;
354	prev = local->last_scan_results;
355	local->last_scan_results = results;
356	local->last_scan_results_count = new_count;
357	spin_unlock_irqrestore(&local->lock, flags);
358	kfree(prev);
359
360	hostap_report_scan_complete(local);
361}
362#endif /* PRISM2_NO_STATION_MODES */
363
364
365/* Called only as a tasklet (software IRQ) */
366void hostap_info_process(local_info_t *local, struct sk_buff *skb)
367{
368	struct hfa384x_info_frame *info;
369	unsigned char *buf;
370	int left;
371#ifndef PRISM2_NO_DEBUG
372	int i;
373#endif /* PRISM2_NO_DEBUG */
374
375	info = (struct hfa384x_info_frame *) skb->data;
376	buf = skb->data + sizeof(*info);
377	left = skb->len - sizeof(*info);
378
379	switch (le16_to_cpu(info->type)) {
380	case HFA384X_INFO_COMMTALLIES:
381		prism2_info_commtallies(local, buf, left);
382		break;
383
384#ifndef PRISM2_NO_STATION_MODES
385	case HFA384X_INFO_LINKSTATUS:
386		prism2_info_linkstatus(local, buf, left);
387		break;
388
389	case HFA384X_INFO_SCANRESULTS:
390		prism2_info_scanresults(local, buf, left);
391		break;
392
393	case HFA384X_INFO_HOSTSCANRESULTS:
394		prism2_info_hostscanresults(local, buf, left);
395		break;
396#endif /* PRISM2_NO_STATION_MODES */
397
398#ifndef PRISM2_NO_DEBUG
399	default:
400		PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
401		       local->dev->name, le16_to_cpu(info->len),
402		       le16_to_cpu(info->type));
403		PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
404		for (i = 0; i < (left < 100 ? left : 100); i++)
405			PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
406		PDEBUG2(DEBUG_EXTRA, "\n");
407		break;
408#endif /* PRISM2_NO_DEBUG */
409	}
410}
411
412
413#ifndef PRISM2_NO_STATION_MODES
414static void handle_info_queue_linkstatus(local_info_t *local)
415{
416	int val = local->prev_link_status;
417	int connected;
418	union iwreq_data wrqu;
419
420	connected =
421		val == HFA384X_LINKSTATUS_CONNECTED ||
422		val == HFA384X_LINKSTATUS_AP_CHANGE ||
423		val == HFA384X_LINKSTATUS_AP_IN_RANGE;
424
425	if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
426				 local->bssid, ETH_ALEN, 1) < 0) {
427		printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
428		       "LinkStatus event\n", local->dev->name);
429	} else {
430		PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
431		       local->dev->name,
432		       (unsigned char *) local->bssid);
433		if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
434			hostap_add_sta(local->ap, local->bssid);
435	}
436
437	/* Get BSSID if we have a valid AP address */
438	if (connected) {
439		netif_carrier_on(local->dev);
440		netif_carrier_on(local->ddev);
441		memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
442	} else {
443		netif_carrier_off(local->dev);
444		netif_carrier_off(local->ddev);
445		eth_zero_addr(wrqu.ap_addr.sa_data);
446	}
447	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
448
449	/*
450	 * Filter out sequential disconnect events in order not to cause a
451	 * flood of SIOCGIWAP events that have a race condition with EAPOL
452	 * frames and can confuse wpa_supplicant about the current association
453	 * status.
454	 */
455	if (connected || local->prev_linkstatus_connected)
456		wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
457	local->prev_linkstatus_connected = connected;
458}
459
460
461static void handle_info_queue_scanresults(local_info_t *local)
462{
463	if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
464		prism2_host_roaming(local);
465
466	if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
467	    !is_zero_ether_addr(local->preferred_ap)) {
468		/*
469		 * Firmware seems to be getting into odd state in host_roaming
470		 * mode 2 when hostscan is used without join command, so try
471		 * to fix this by re-joining the current AP. This does not
472		 * actually trigger a new association if the current AP is
473		 * still in the scan results.
474		 */
475		prism2_host_roaming(local);
476	}
477}
478
479
480/* Called only as scheduled task after receiving info frames (used to avoid
481 * pending too much time in HW IRQ handler). */
482static void handle_info_queue(struct work_struct *work)
483{
484	local_info_t *local = container_of(work, local_info_t, info_queue);
485
486	if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
487			       &local->pending_info))
488		handle_info_queue_linkstatus(local);
489
490	if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
491			       &local->pending_info))
492		handle_info_queue_scanresults(local);
493}
494#endif /* PRISM2_NO_STATION_MODES */
495
496
497void hostap_info_init(local_info_t *local)
498{
499	skb_queue_head_init(&local->info_list);
500#ifndef PRISM2_NO_STATION_MODES
501	INIT_WORK(&local->info_queue, handle_info_queue);
502#endif /* PRISM2_NO_STATION_MODES */
503}
504
505
506EXPORT_SYMBOL(hostap_info_init);
507EXPORT_SYMBOL(hostap_info_process);
508