1 /* Host AP driver Info Frame processing (part of hostap.o module) */
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"
10 #include "hostap_ap.h"
12 /* Called only as a tasklet (software IRQ) */
13 static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
16 struct hfa384x_comm_tallies *tallies;
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);
24 tallies = (struct hfa384x_comm_tallies *) buf;
25 #define ADD_COMM_TALLIES(name) \
26 local->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
52 /* Called only as a tasklet (software IRQ) */
53 static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
56 struct hfa384x_comm_tallies32 *tallies;
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);
64 tallies = (struct hfa384x_comm_tallies32 *) buf;
65 #define ADD_COMM_TALLIES(name) \
66 local->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
92 /* Called only as a tasklet (software IRQ) */
93 static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
97 prism2_info_commtallies32(local, buf, left);
99 prism2_info_commtallies16(local, buf, left);
103 #ifndef PRISM2_NO_STATION_MODES
104 #ifndef PRISM2_NO_DEBUG
105 static const char* hfa384x_linkstatus_str(u16 linkstatus)
107 switch (linkstatus) {
108 case HFA384X_LINKSTATUS_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";
124 #endif /* PRISM2_NO_DEBUG */
127 /* Called only as a tasklet (software IRQ) */
128 static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
134 /* Alloc new JoinRequests to occur since LinkStatus for the previous
135 * has been received */
136 local->last_join_time = 0;
139 printk(KERN_DEBUG "%s: invalid linkstatus info frame "
140 "length %d\n", local->dev->name, left);
144 non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
145 local->iw_mode == IW_MODE_REPEAT ||
146 local->iw_mode == IW_MODE_MONITOR;
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));
155 netif_carrier_on(local->dev);
156 netif_carrier_on(local->ddev);
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);
167 static void prism2_host_roaming(local_info_t *local)
169 struct hfa384x_join_request req;
170 struct net_device *dev = local->dev;
171 struct hfa384x_hostscan_result *selected, *entry;
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",
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.
189 * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
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",
200 selected = &local->last_scan_results[0];
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)
212 PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
213 "selection\n", dev->name);
220 memcpy(req.bssid, selected->bssid, ETH_ALEN);
221 req.channel = selected->chid;
222 spin_unlock_irqrestore(&local->lock, flags);
224 PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
226 dev->name, req.bssid, le16_to_cpu(req.channel));
227 if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
229 printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
231 local->last_join_time = jiffies;
235 static void hostap_report_scan_complete(local_info_t *local)
237 union iwreq_data wrqu;
239 /* Inform user space about new scan results (just empty event,
240 * SIOCGIWSCAN can be used to fetch data */
241 wrqu.data.length = 0;
243 wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
245 /* Allow SIOCGIWSCAN handling to occur since we have received
247 local->scan_timestamp = 0;
251 /* Called only as a tasklet (software IRQ) */
252 static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
258 struct hfa384x_scan_result *res;
259 struct hfa384x_hostscan_result *results, *prev;
262 printk(KERN_DEBUG "%s: invalid scanresult info frame "
263 "length %d\n", local->dev->name, left);
272 new_count = left / sizeof(struct hfa384x_scan_result);
273 results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
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));
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);
294 hostap_report_scan_complete(local);
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);
302 /* Called only as a tasklet (software IRQ) */
303 static void prism2_info_hostscanresults(local_info_t *local,
304 unsigned char *buf, int left)
306 int i, result_size, copy_len, new_count;
307 struct hfa384x_hostscan_result *results, *prev;
312 wake_up_interruptible(&local->hostscan_wq);
315 printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
316 "length %d\n", local->dev->name, left);
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);
327 if (copy_len > sizeof(struct hfa384x_hostscan_result))
328 copy_len = sizeof(struct hfa384x_hostscan_result);
335 new_count = left / result_size;
336 results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
341 for (i = 0; i < new_count; i++) {
342 memcpy(&results[i], ptr, copy_len);
348 printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
349 local->dev->name, left, result_size);
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);
360 hostap_report_scan_complete(local);
362 #endif /* PRISM2_NO_STATION_MODES */
365 /* Called only as a tasklet (software IRQ) */
366 void hostap_info_process(local_info_t *local, struct sk_buff *skb)
368 struct hfa384x_info_frame *info;
371 #ifndef PRISM2_NO_DEBUG
373 #endif /* PRISM2_NO_DEBUG */
375 info = (struct hfa384x_info_frame *) skb->data;
376 buf = skb->data + sizeof(*info);
377 left = skb->len - sizeof(*info);
379 switch (le16_to_cpu(info->type)) {
380 case HFA384X_INFO_COMMTALLIES:
381 prism2_info_commtallies(local, buf, left);
384 #ifndef PRISM2_NO_STATION_MODES
385 case HFA384X_INFO_LINKSTATUS:
386 prism2_info_linkstatus(local, buf, left);
389 case HFA384X_INFO_SCANRESULTS:
390 prism2_info_scanresults(local, buf, left);
393 case HFA384X_INFO_HOSTSCANRESULTS:
394 prism2_info_hostscanresults(local, buf, left);
396 #endif /* PRISM2_NO_STATION_MODES */
398 #ifndef PRISM2_NO_DEBUG
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");
408 #endif /* PRISM2_NO_DEBUG */
413 #ifndef PRISM2_NO_STATION_MODES
414 static void handle_info_queue_linkstatus(local_info_t *local)
416 int val = local->prev_link_status;
418 union iwreq_data wrqu;
421 val == HFA384X_LINKSTATUS_CONNECTED ||
422 val == HFA384X_LINKSTATUS_AP_CHANGE ||
423 val == HFA384X_LINKSTATUS_AP_IN_RANGE;
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);
430 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
432 (unsigned char *) local->bssid);
433 if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
434 hostap_add_sta(local->ap, local->bssid);
437 /* Get BSSID if we have a valid AP address */
439 netif_carrier_on(local->dev);
440 netif_carrier_on(local->ddev);
441 memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
443 netif_carrier_off(local->dev);
444 netif_carrier_off(local->ddev);
445 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
447 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
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
455 if (connected || local->prev_linkstatus_connected)
456 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
457 local->prev_linkstatus_connected = connected;
461 static void handle_info_queue_scanresults(local_info_t *local)
463 if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
464 prism2_host_roaming(local);
466 if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
467 !is_zero_ether_addr(local->preferred_ap)) {
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.
475 prism2_host_roaming(local);
480 /* Called only as scheduled task after receiving info frames (used to avoid
481 * pending too much time in HW IRQ handler). */
482 static void handle_info_queue(struct work_struct *work)
484 local_info_t *local = container_of(work, local_info_t, info_queue);
486 if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
487 &local->pending_info))
488 handle_info_queue_linkstatus(local);
490 if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
491 &local->pending_info))
492 handle_info_queue_scanresults(local);
494 #endif /* PRISM2_NO_STATION_MODES */
497 void hostap_info_init(local_info_t *local)
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 */
506 EXPORT_SYMBOL(hostap_info_init);
507 EXPORT_SYMBOL(hostap_info_process);