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