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