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