1 /* Host AP driver Info Frame processing (part of hostap.o module) */
3 #include <linux/if_arp.h>
4 #include "hostap_wlan.h"
8 /* Called only as a tasklet (software IRQ) */
9 static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
12 struct hfa384x_comm_tallies *tallies;
14 if (left < sizeof(struct hfa384x_comm_tallies)) {
15 printk(KERN_DEBUG "%s: too short (len=%d) commtallies "
16 "info frame\n", local->dev->name, left);
20 tallies = (struct hfa384x_comm_tallies *) buf;
21 #define ADD_COMM_TALLIES(name) \
22 local->comm_tallies.name += le16_to_cpu(tallies->name)
23 ADD_COMM_TALLIES(tx_unicast_frames);
24 ADD_COMM_TALLIES(tx_multicast_frames);
25 ADD_COMM_TALLIES(tx_fragments);
26 ADD_COMM_TALLIES(tx_unicast_octets);
27 ADD_COMM_TALLIES(tx_multicast_octets);
28 ADD_COMM_TALLIES(tx_deferred_transmissions);
29 ADD_COMM_TALLIES(tx_single_retry_frames);
30 ADD_COMM_TALLIES(tx_multiple_retry_frames);
31 ADD_COMM_TALLIES(tx_retry_limit_exceeded);
32 ADD_COMM_TALLIES(tx_discards);
33 ADD_COMM_TALLIES(rx_unicast_frames);
34 ADD_COMM_TALLIES(rx_multicast_frames);
35 ADD_COMM_TALLIES(rx_fragments);
36 ADD_COMM_TALLIES(rx_unicast_octets);
37 ADD_COMM_TALLIES(rx_multicast_octets);
38 ADD_COMM_TALLIES(rx_fcs_errors);
39 ADD_COMM_TALLIES(rx_discards_no_buffer);
40 ADD_COMM_TALLIES(tx_discards_wrong_sa);
41 ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
42 ADD_COMM_TALLIES(rx_message_in_msg_fragments);
43 ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
44 #undef ADD_COMM_TALLIES
48 /* Called only as a tasklet (software IRQ) */
49 static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
52 struct hfa384x_comm_tallies32 *tallies;
54 if (left < sizeof(struct hfa384x_comm_tallies32)) {
55 printk(KERN_DEBUG "%s: too short (len=%d) commtallies32 "
56 "info frame\n", local->dev->name, left);
60 tallies = (struct hfa384x_comm_tallies32 *) buf;
61 #define ADD_COMM_TALLIES(name) \
62 local->comm_tallies.name += le32_to_cpu(tallies->name)
63 ADD_COMM_TALLIES(tx_unicast_frames);
64 ADD_COMM_TALLIES(tx_multicast_frames);
65 ADD_COMM_TALLIES(tx_fragments);
66 ADD_COMM_TALLIES(tx_unicast_octets);
67 ADD_COMM_TALLIES(tx_multicast_octets);
68 ADD_COMM_TALLIES(tx_deferred_transmissions);
69 ADD_COMM_TALLIES(tx_single_retry_frames);
70 ADD_COMM_TALLIES(tx_multiple_retry_frames);
71 ADD_COMM_TALLIES(tx_retry_limit_exceeded);
72 ADD_COMM_TALLIES(tx_discards);
73 ADD_COMM_TALLIES(rx_unicast_frames);
74 ADD_COMM_TALLIES(rx_multicast_frames);
75 ADD_COMM_TALLIES(rx_fragments);
76 ADD_COMM_TALLIES(rx_unicast_octets);
77 ADD_COMM_TALLIES(rx_multicast_octets);
78 ADD_COMM_TALLIES(rx_fcs_errors);
79 ADD_COMM_TALLIES(rx_discards_no_buffer);
80 ADD_COMM_TALLIES(tx_discards_wrong_sa);
81 ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
82 ADD_COMM_TALLIES(rx_message_in_msg_fragments);
83 ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
84 #undef ADD_COMM_TALLIES
88 /* Called only as a tasklet (software IRQ) */
89 static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
93 prism2_info_commtallies32(local, buf, left);
95 prism2_info_commtallies16(local, buf, left);
99 #ifndef PRISM2_NO_STATION_MODES
100 #ifndef PRISM2_NO_DEBUG
101 static const char* hfa384x_linkstatus_str(u16 linkstatus)
103 switch (linkstatus) {
104 case HFA384X_LINKSTATUS_CONNECTED:
106 case HFA384X_LINKSTATUS_DISCONNECTED:
107 return "Disconnected";
108 case HFA384X_LINKSTATUS_AP_CHANGE:
109 return "Access point change";
110 case HFA384X_LINKSTATUS_AP_OUT_OF_RANGE:
111 return "Access point out of range";
112 case HFA384X_LINKSTATUS_AP_IN_RANGE:
113 return "Access point in range";
114 case HFA384X_LINKSTATUS_ASSOC_FAILED:
115 return "Association failed";
120 #endif /* PRISM2_NO_DEBUG */
123 /* Called only as a tasklet (software IRQ) */
124 static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
130 /* Alloc new JoinRequests to occur since LinkStatus for the previous
131 * has been received */
132 local->last_join_time = 0;
135 printk(KERN_DEBUG "%s: invalid linkstatus info frame "
136 "length %d\n", local->dev->name, left);
140 non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
141 local->iw_mode == IW_MODE_REPEAT ||
142 local->iw_mode == IW_MODE_MONITOR;
144 val = buf[0] | (buf[1] << 8);
145 if (!non_sta_mode || val != HFA384X_LINKSTATUS_DISCONNECTED) {
146 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus=%d (%s)\n",
147 local->dev->name, val, hfa384x_linkstatus_str(val));
151 netif_carrier_on(local->dev);
152 netif_carrier_on(local->ddev);
156 /* Get current BSSID later in scheduled task */
157 set_bit(PRISM2_INFO_PENDING_LINKSTATUS, &local->pending_info);
158 local->prev_link_status = val;
159 schedule_work(&local->info_queue);
163 static void prism2_host_roaming(local_info_t *local)
165 struct hfa384x_join_request req;
166 struct net_device *dev = local->dev;
167 struct hfa384x_hostscan_result *selected, *entry;
171 if (local->last_join_time &&
172 time_before(jiffies, local->last_join_time + 10 * HZ)) {
173 PDEBUG(DEBUG_EXTRA, "%s: last join request has not yet been "
174 "completed - waiting for it before issuing new one\n",
179 /* ScanResults are sorted: first ESS results in decreasing signal
180 * quality then IBSS results in similar order.
181 * Trivial roaming policy: just select the first entry.
182 * This could probably be improved by adding hysteresis to limit
183 * number of handoffs, etc.
185 * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
187 spin_lock_irqsave(&local->lock, flags);
188 if (local->last_scan_results == NULL ||
189 local->last_scan_results_count == 0) {
190 spin_unlock_irqrestore(&local->lock, flags);
191 PDEBUG(DEBUG_EXTRA, "%s: no scan results for host roaming\n",
196 selected = &local->last_scan_results[0];
198 if (local->preferred_ap[0] || local->preferred_ap[1] ||
199 local->preferred_ap[2] || local->preferred_ap[3] ||
200 local->preferred_ap[4] || local->preferred_ap[5]) {
201 /* Try to find preferred AP */
202 PDEBUG(DEBUG_EXTRA, "%s: Preferred AP BSSID %pM\n",
203 dev->name, local->preferred_ap);
204 for (i = 0; i < local->last_scan_results_count; i++) {
205 entry = &local->last_scan_results[i];
206 if (memcmp(local->preferred_ap, entry->bssid, 6) == 0)
208 PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
209 "selection\n", dev->name);
216 memcpy(req.bssid, selected->bssid, 6);
217 req.channel = selected->chid;
218 spin_unlock_irqrestore(&local->lock, flags);
220 PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
222 dev->name, req.bssid, le16_to_cpu(req.channel));
223 if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
225 printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
227 local->last_join_time = jiffies;
231 static void hostap_report_scan_complete(local_info_t *local)
233 union iwreq_data wrqu;
235 /* Inform user space about new scan results (just empty event,
236 * SIOCGIWSCAN can be used to fetch data */
237 wrqu.data.length = 0;
239 wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
241 /* Allow SIOCGIWSCAN handling to occur since we have received
243 local->scan_timestamp = 0;
247 /* Called only as a tasklet (software IRQ) */
248 static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
254 struct hfa384x_scan_result *res;
255 struct hfa384x_hostscan_result *results, *prev;
258 printk(KERN_DEBUG "%s: invalid scanresult info frame "
259 "length %d\n", local->dev->name, left);
268 new_count = left / sizeof(struct hfa384x_scan_result);
269 results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
274 /* Convert to hostscan result format. */
275 res = (struct hfa384x_scan_result *) pos;
276 for (i = 0; i < new_count; i++) {
277 memcpy(&results[i], &res[i],
278 sizeof(struct hfa384x_scan_result));
282 spin_lock_irqsave(&local->lock, flags);
283 local->last_scan_type = PRISM2_SCAN;
284 prev = local->last_scan_results;
285 local->last_scan_results = results;
286 local->last_scan_results_count = new_count;
287 spin_unlock_irqrestore(&local->lock, flags);
290 hostap_report_scan_complete(local);
292 /* Perform rest of ScanResults handling later in scheduled task */
293 set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
294 schedule_work(&local->info_queue);
298 /* Called only as a tasklet (software IRQ) */
299 static void prism2_info_hostscanresults(local_info_t *local,
300 unsigned char *buf, int left)
302 int i, result_size, copy_len, new_count;
303 struct hfa384x_hostscan_result *results, *prev;
308 wake_up_interruptible(&local->hostscan_wq);
311 printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
312 "length %d\n", local->dev->name, left);
316 pos = (__le16 *) buf;
317 copy_len = result_size = le16_to_cpu(*pos);
318 if (result_size == 0) {
319 printk(KERN_DEBUG "%s: invalid result_size (0) in "
320 "hostscanresults\n", local->dev->name);
323 if (copy_len > sizeof(struct hfa384x_hostscan_result))
324 copy_len = sizeof(struct hfa384x_hostscan_result);
331 new_count = left / result_size;
332 results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
337 for (i = 0; i < new_count; i++) {
338 memcpy(&results[i], ptr, copy_len);
344 printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
345 local->dev->name, left, result_size);
348 spin_lock_irqsave(&local->lock, flags);
349 local->last_scan_type = PRISM2_HOSTSCAN;
350 prev = local->last_scan_results;
351 local->last_scan_results = results;
352 local->last_scan_results_count = new_count;
353 spin_unlock_irqrestore(&local->lock, flags);
356 hostap_report_scan_complete(local);
358 #endif /* PRISM2_NO_STATION_MODES */
361 /* Called only as a tasklet (software IRQ) */
362 void hostap_info_process(local_info_t *local, struct sk_buff *skb)
364 struct hfa384x_info_frame *info;
367 #ifndef PRISM2_NO_DEBUG
369 #endif /* PRISM2_NO_DEBUG */
371 info = (struct hfa384x_info_frame *) skb->data;
372 buf = skb->data + sizeof(*info);
373 left = skb->len - sizeof(*info);
375 switch (le16_to_cpu(info->type)) {
376 case HFA384X_INFO_COMMTALLIES:
377 prism2_info_commtallies(local, buf, left);
380 #ifndef PRISM2_NO_STATION_MODES
381 case HFA384X_INFO_LINKSTATUS:
382 prism2_info_linkstatus(local, buf, left);
385 case HFA384X_INFO_SCANRESULTS:
386 prism2_info_scanresults(local, buf, left);
389 case HFA384X_INFO_HOSTSCANRESULTS:
390 prism2_info_hostscanresults(local, buf, left);
392 #endif /* PRISM2_NO_STATION_MODES */
394 #ifndef PRISM2_NO_DEBUG
396 PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
397 local->dev->name, le16_to_cpu(info->len),
398 le16_to_cpu(info->type));
399 PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
400 for (i = 0; i < (left < 100 ? left : 100); i++)
401 PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
402 PDEBUG2(DEBUG_EXTRA, "\n");
404 #endif /* PRISM2_NO_DEBUG */
409 #ifndef PRISM2_NO_STATION_MODES
410 static void handle_info_queue_linkstatus(local_info_t *local)
412 int val = local->prev_link_status;
414 union iwreq_data wrqu;
417 val == HFA384X_LINKSTATUS_CONNECTED ||
418 val == HFA384X_LINKSTATUS_AP_CHANGE ||
419 val == HFA384X_LINKSTATUS_AP_IN_RANGE;
421 if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
422 local->bssid, ETH_ALEN, 1) < 0) {
423 printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
424 "LinkStatus event\n", local->dev->name);
426 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
428 (unsigned char *) local->bssid);
429 if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
430 hostap_add_sta(local->ap, local->bssid);
433 /* Get BSSID if we have a valid AP address */
435 netif_carrier_on(local->dev);
436 netif_carrier_on(local->ddev);
437 memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
439 netif_carrier_off(local->dev);
440 netif_carrier_off(local->ddev);
441 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
443 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
446 * Filter out sequential disconnect events in order not to cause a
447 * flood of SIOCGIWAP events that have a race condition with EAPOL
448 * frames and can confuse wpa_supplicant about the current association
451 if (connected || local->prev_linkstatus_connected)
452 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
453 local->prev_linkstatus_connected = connected;
457 static void handle_info_queue_scanresults(local_info_t *local)
459 if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
460 prism2_host_roaming(local);
462 if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
463 memcmp(local->preferred_ap, "\x00\x00\x00\x00\x00\x00",
466 * Firmware seems to be getting into odd state in host_roaming
467 * mode 2 when hostscan is used without join command, so try
468 * to fix this by re-joining the current AP. This does not
469 * actually trigger a new association if the current AP is
470 * still in the scan results.
472 prism2_host_roaming(local);
477 /* Called only as scheduled task after receiving info frames (used to avoid
478 * pending too much time in HW IRQ handler). */
479 static void handle_info_queue(struct work_struct *work)
481 local_info_t *local = container_of(work, local_info_t, info_queue);
483 if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
484 &local->pending_info))
485 handle_info_queue_linkstatus(local);
487 if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
488 &local->pending_info))
489 handle_info_queue_scanresults(local);
491 #endif /* PRISM2_NO_STATION_MODES */
494 void hostap_info_init(local_info_t *local)
496 skb_queue_head_init(&local->info_list);
497 #ifndef PRISM2_NO_STATION_MODES
498 INIT_WORK(&local->info_queue, handle_info_queue);
499 #endif /* PRISM2_NO_STATION_MODES */
503 EXPORT_SYMBOL(hostap_info_init);
504 EXPORT_SYMBOL(hostap_info_process);