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