Merge branch 'linus' into perfcounters/core
[linux-2.6] / drivers / net / wireless / hostap / hostap_info.c
1 /* Host AP driver Info Frame processing (part of hostap.o module) */
2
3 #include <linux/if_arp.h>
4 #include "hostap_wlan.h"
5 #include "hostap.h"
6 #include "hostap_ap.h"
7
8 /* Called only as a tasklet (software IRQ) */
9 static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
10                                       int left)
11 {
12         struct hfa384x_comm_tallies *tallies;
13
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);
17                 return;
18         }
19
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
45 }
46
47
48 /* Called only as a tasklet (software IRQ) */
49 static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
50                                       int left)
51 {
52         struct hfa384x_comm_tallies32 *tallies;
53
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);
57                 return;
58         }
59
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
85 }
86
87
88 /* Called only as a tasklet (software IRQ) */
89 static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
90                                     int left)
91 {
92         if (local->tallies32)
93                 prism2_info_commtallies32(local, buf, left);
94         else
95                 prism2_info_commtallies16(local, buf, left);
96 }
97
98
99 #ifndef PRISM2_NO_STATION_MODES
100 #ifndef PRISM2_NO_DEBUG
101 static const char* hfa384x_linkstatus_str(u16 linkstatus)
102 {
103         switch (linkstatus) {
104         case HFA384X_LINKSTATUS_CONNECTED:
105                 return "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";
116         default:
117                 return "Unknown";
118         }
119 }
120 #endif /* PRISM2_NO_DEBUG */
121
122
123 /* Called only as a tasklet (software IRQ) */
124 static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
125                                     int left)
126 {
127         u16 val;
128         int non_sta_mode;
129
130         /* Alloc new JoinRequests to occur since LinkStatus for the previous
131          * has been received */
132         local->last_join_time = 0;
133
134         if (left != 2) {
135                 printk(KERN_DEBUG "%s: invalid linkstatus info frame "
136                        "length %d\n", local->dev->name, left);
137                 return;
138         }
139
140         non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
141                 local->iw_mode == IW_MODE_REPEAT ||
142                 local->iw_mode == IW_MODE_MONITOR;
143
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));
148         }
149
150         if (non_sta_mode) {
151                 netif_carrier_on(local->dev);
152                 netif_carrier_on(local->ddev);
153                 return;
154         }
155
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);
160 }
161
162
163 static void prism2_host_roaming(local_info_t *local)
164 {
165         struct hfa384x_join_request req;
166         struct net_device *dev = local->dev;
167         struct hfa384x_hostscan_result *selected, *entry;
168         int i;
169         unsigned long flags;
170
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",
175                        dev->name);
176                 return;
177         }
178
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.
184          *
185          * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
186          * ScanResults */
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",
192                        dev->name);
193                 return;
194         }
195
196         selected = &local->last_scan_results[0];
197
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)
207                         {
208                                 PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
209                                        "selection\n", dev->name);
210                                 selected = entry;
211                                 break;
212                         }
213                 }
214         }
215
216         memcpy(req.bssid, selected->bssid, 6);
217         req.channel = selected->chid;
218         spin_unlock_irqrestore(&local->lock, flags);
219
220         PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
221                " channel=%d\n",
222                dev->name, req.bssid, le16_to_cpu(req.channel));
223         if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
224                                  sizeof(req))) {
225                 printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
226         }
227         local->last_join_time = jiffies;
228 }
229
230
231 static void hostap_report_scan_complete(local_info_t *local)
232 {
233         union iwreq_data wrqu;
234
235         /* Inform user space about new scan results (just empty event,
236          * SIOCGIWSCAN can be used to fetch data */
237         wrqu.data.length = 0;
238         wrqu.data.flags = 0;
239         wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
240
241         /* Allow SIOCGIWSCAN handling to occur since we have received
242          * scanning result */
243         local->scan_timestamp = 0;
244 }
245
246
247 /* Called only as a tasklet (software IRQ) */
248 static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
249                                     int left)
250 {
251         u16 *pos;
252         int new_count, i;
253         unsigned long flags;
254         struct hfa384x_scan_result *res;
255         struct hfa384x_hostscan_result *results, *prev;
256
257         if (left < 4) {
258                 printk(KERN_DEBUG "%s: invalid scanresult info frame "
259                        "length %d\n", local->dev->name, left);
260                 return;
261         }
262
263         pos = (u16 *) buf;
264         pos++;
265         pos++;
266         left -= 4;
267
268         new_count = left / sizeof(struct hfa384x_scan_result);
269         results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
270                           GFP_ATOMIC);
271         if (results == NULL)
272                 return;
273
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));
279                 results[i].atim = 0;
280         }
281
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);
288         kfree(prev);
289
290         hostap_report_scan_complete(local);
291
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);
295 }
296
297
298 /* Called only as a tasklet (software IRQ) */
299 static void prism2_info_hostscanresults(local_info_t *local,
300                                         unsigned char *buf, int left)
301 {
302         int i, result_size, copy_len, new_count;
303         struct hfa384x_hostscan_result *results, *prev;
304         unsigned long flags;
305         __le16 *pos;
306         u8 *ptr;
307
308         wake_up_interruptible(&local->hostscan_wq);
309
310         if (left < 4) {
311                 printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
312                        "length %d\n", local->dev->name, left);
313                 return;
314         }
315
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);
321                 return;
322         }
323         if (copy_len > sizeof(struct hfa384x_hostscan_result))
324                 copy_len = sizeof(struct hfa384x_hostscan_result);
325
326         pos++;
327         pos++;
328         left -= 4;
329         ptr = (u8 *) pos;
330
331         new_count = left / result_size;
332         results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
333                           GFP_ATOMIC);
334         if (results == NULL)
335                 return;
336
337         for (i = 0; i < new_count; i++) {
338                 memcpy(&results[i], ptr, copy_len);
339                 ptr += result_size;
340                 left -= result_size;
341         }
342
343         if (left) {
344                 printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
345                        local->dev->name, left, result_size);
346         }
347
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);
354         kfree(prev);
355
356         hostap_report_scan_complete(local);
357 }
358 #endif /* PRISM2_NO_STATION_MODES */
359
360
361 /* Called only as a tasklet (software IRQ) */
362 void hostap_info_process(local_info_t *local, struct sk_buff *skb)
363 {
364         struct hfa384x_info_frame *info;
365         unsigned char *buf;
366         int left;
367 #ifndef PRISM2_NO_DEBUG
368         int i;
369 #endif /* PRISM2_NO_DEBUG */
370
371         info = (struct hfa384x_info_frame *) skb->data;
372         buf = skb->data + sizeof(*info);
373         left = skb->len - sizeof(*info);
374
375         switch (le16_to_cpu(info->type)) {
376         case HFA384X_INFO_COMMTALLIES:
377                 prism2_info_commtallies(local, buf, left);
378                 break;
379
380 #ifndef PRISM2_NO_STATION_MODES
381         case HFA384X_INFO_LINKSTATUS:
382                 prism2_info_linkstatus(local, buf, left);
383                 break;
384
385         case HFA384X_INFO_SCANRESULTS:
386                 prism2_info_scanresults(local, buf, left);
387                 break;
388
389         case HFA384X_INFO_HOSTSCANRESULTS:
390                 prism2_info_hostscanresults(local, buf, left);
391                 break;
392 #endif /* PRISM2_NO_STATION_MODES */
393
394 #ifndef PRISM2_NO_DEBUG
395         default:
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");
403                 break;
404 #endif /* PRISM2_NO_DEBUG */
405         }
406 }
407
408
409 #ifndef PRISM2_NO_STATION_MODES
410 static void handle_info_queue_linkstatus(local_info_t *local)
411 {
412         int val = local->prev_link_status;
413         int connected;
414         union iwreq_data wrqu;
415
416         connected =
417                 val == HFA384X_LINKSTATUS_CONNECTED ||
418                 val == HFA384X_LINKSTATUS_AP_CHANGE ||
419                 val == HFA384X_LINKSTATUS_AP_IN_RANGE;
420
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);
425         } else {
426                 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
427                        local->dev->name,
428                        (unsigned char *) local->bssid);
429                 if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
430                         hostap_add_sta(local->ap, local->bssid);
431         }
432
433         /* Get BSSID if we have a valid AP address */
434         if (connected) {
435                 netif_carrier_on(local->dev);
436                 netif_carrier_on(local->ddev);
437                 memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
438         } else {
439                 netif_carrier_off(local->dev);
440                 netif_carrier_off(local->ddev);
441                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
442         }
443         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
444
445         /*
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
449          * status.
450          */
451         if (connected || local->prev_linkstatus_connected)
452                 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
453         local->prev_linkstatus_connected = connected;
454 }
455
456
457 static void handle_info_queue_scanresults(local_info_t *local)
458 {
459         if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
460                 prism2_host_roaming(local);
461
462         if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
463             memcmp(local->preferred_ap, "\x00\x00\x00\x00\x00\x00",
464                    ETH_ALEN) != 0) {
465                 /*
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.
471                  */
472                 prism2_host_roaming(local);
473         }
474 }
475
476
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)
480 {
481         local_info_t *local = container_of(work, local_info_t, info_queue);
482
483         if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
484                                &local->pending_info))
485                 handle_info_queue_linkstatus(local);
486
487         if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
488                                &local->pending_info))
489                 handle_info_queue_scanresults(local);
490 }
491 #endif /* PRISM2_NO_STATION_MODES */
492
493
494 void hostap_info_init(local_info_t *local)
495 {
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 */
500 }
501
502
503 EXPORT_SYMBOL(hostap_info_init);
504 EXPORT_SYMBOL(hostap_info_process);