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