1 // SPDX-License-Identifier: GPL-2.0
2 /* Host AP driver Info Frame processing (part of hostap.o module) */
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"
11 #include "hostap_ap.h"
13 /* Called only as a tasklet (software IRQ) */
14 static void prism2_info_commtallies16(local_info_t
*local
, unsigned char *buf
,
17 struct hfa384x_comm_tallies
*tallies
;
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
);
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
53 /* Called only as a tasklet (software IRQ) */
54 static void prism2_info_commtallies32(local_info_t
*local
, unsigned char *buf
,
57 struct hfa384x_comm_tallies32
*tallies
;
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
);
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
93 /* Called only as a tasklet (software IRQ) */
94 static void prism2_info_commtallies(local_info_t
*local
, unsigned char *buf
,
98 prism2_info_commtallies32(local
, buf
, left
);
100 prism2_info_commtallies16(local
, buf
, left
);
104 #ifndef PRISM2_NO_STATION_MODES
105 #ifndef PRISM2_NO_DEBUG
106 static const char* hfa384x_linkstatus_str(u16 linkstatus
)
108 switch (linkstatus
) {
109 case HFA384X_LINKSTATUS_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";
125 #endif /* PRISM2_NO_DEBUG */
128 /* Called only as a tasklet (software IRQ) */
129 static void prism2_info_linkstatus(local_info_t
*local
, unsigned char *buf
,
135 /* Alloc new JoinRequests to occur since LinkStatus for the previous
136 * has been received */
137 local
->last_join_time
= 0;
140 printk(KERN_DEBUG
"%s: invalid linkstatus info frame "
141 "length %d\n", local
->dev
->name
, left
);
145 non_sta_mode
= local
->iw_mode
== IW_MODE_MASTER
||
146 local
->iw_mode
== IW_MODE_REPEAT
||
147 local
->iw_mode
== IW_MODE_MONITOR
;
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
));
156 netif_carrier_on(local
->dev
);
157 netif_carrier_on(local
->ddev
);
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
);
168 static void prism2_host_roaming(local_info_t
*local
)
170 struct hfa384x_join_request req
;
171 struct net_device
*dev
= local
->dev
;
172 struct hfa384x_hostscan_result
*selected
, *entry
;
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",
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.
190 * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
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",
201 selected
= &local
->last_scan_results
[0];
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)
213 PDEBUG(DEBUG_EXTRA
, "%s: using preferred AP "
214 "selection\n", dev
->name
);
221 memcpy(req
.bssid
, selected
->bssid
, ETH_ALEN
);
222 req
.channel
= selected
->chid
;
223 spin_unlock_irqrestore(&local
->lock
, flags
);
225 PDEBUG(DEBUG_EXTRA
, "%s: JoinRequest: BSSID=%pM"
227 dev
->name
, req
.bssid
, le16_to_cpu(req
.channel
));
228 if (local
->func
->set_rid(dev
, HFA384X_RID_JOINREQUEST
, &req
,
230 printk(KERN_DEBUG
"%s: JoinRequest failed\n", dev
->name
);
232 local
->last_join_time
= jiffies
;
236 static void hostap_report_scan_complete(local_info_t
*local
)
238 union iwreq_data wrqu
;
240 /* Inform user space about new scan results (just empty event,
241 * SIOCGIWSCAN can be used to fetch data */
242 wrqu
.data
.length
= 0;
244 wireless_send_event(local
->dev
, SIOCGIWSCAN
, &wrqu
, NULL
);
246 /* Allow SIOCGIWSCAN handling to occur since we have received
248 local
->scan_timestamp
= 0;
252 /* Called only as a tasklet (software IRQ) */
253 static void prism2_info_scanresults(local_info_t
*local
, unsigned char *buf
,
259 struct hfa384x_scan_result
*res
;
260 struct hfa384x_hostscan_result
*results
, *prev
;
263 printk(KERN_DEBUG
"%s: invalid scanresult info frame "
264 "length %d\n", local
->dev
->name
, left
);
273 new_count
= left
/ sizeof(struct hfa384x_scan_result
);
274 results
= kmalloc(new_count
* sizeof(struct hfa384x_hostscan_result
),
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
));
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
);
295 hostap_report_scan_complete(local
);
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
);
303 /* Called only as a tasklet (software IRQ) */
304 static void prism2_info_hostscanresults(local_info_t
*local
,
305 unsigned char *buf
, int left
)
307 int i
, result_size
, copy_len
, new_count
;
308 struct hfa384x_hostscan_result
*results
, *prev
;
313 wake_up_interruptible(&local
->hostscan_wq
);
316 printk(KERN_DEBUG
"%s: invalid hostscanresult info frame "
317 "length %d\n", local
->dev
->name
, left
);
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
);
328 if (copy_len
> sizeof(struct hfa384x_hostscan_result
))
329 copy_len
= sizeof(struct hfa384x_hostscan_result
);
336 new_count
= left
/ result_size
;
337 results
= kcalloc(new_count
, sizeof(struct hfa384x_hostscan_result
),
342 for (i
= 0; i
< new_count
; i
++) {
343 memcpy(&results
[i
], ptr
, copy_len
);
349 printk(KERN_DEBUG
"%s: short HostScan result entry (%d/%d)\n",
350 local
->dev
->name
, left
, result_size
);
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
);
361 hostap_report_scan_complete(local
);
363 #endif /* PRISM2_NO_STATION_MODES */
366 /* Called only as a tasklet (software IRQ) */
367 void hostap_info_process(local_info_t
*local
, struct sk_buff
*skb
)
369 struct hfa384x_info_frame
*info
;
372 #ifndef PRISM2_NO_DEBUG
374 #endif /* PRISM2_NO_DEBUG */
376 info
= (struct hfa384x_info_frame
*) skb
->data
;
377 buf
= skb
->data
+ sizeof(*info
);
378 left
= skb
->len
- sizeof(*info
);
380 switch (le16_to_cpu(info
->type
)) {
381 case HFA384X_INFO_COMMTALLIES
:
382 prism2_info_commtallies(local
, buf
, left
);
385 #ifndef PRISM2_NO_STATION_MODES
386 case HFA384X_INFO_LINKSTATUS
:
387 prism2_info_linkstatus(local
, buf
, left
);
390 case HFA384X_INFO_SCANRESULTS
:
391 prism2_info_scanresults(local
, buf
, left
);
394 case HFA384X_INFO_HOSTSCANRESULTS
:
395 prism2_info_hostscanresults(local
, buf
, left
);
397 #endif /* PRISM2_NO_STATION_MODES */
399 #ifndef PRISM2_NO_DEBUG
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");
409 #endif /* PRISM2_NO_DEBUG */
414 #ifndef PRISM2_NO_STATION_MODES
415 static void handle_info_queue_linkstatus(local_info_t
*local
)
417 int val
= local
->prev_link_status
;
419 union iwreq_data wrqu
;
422 val
== HFA384X_LINKSTATUS_CONNECTED
||
423 val
== HFA384X_LINKSTATUS_AP_CHANGE
||
424 val
== HFA384X_LINKSTATUS_AP_IN_RANGE
;
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
);
431 PDEBUG(DEBUG_EXTRA
, "%s: LinkStatus: BSSID=%pM\n",
433 (unsigned char *) local
->bssid
);
434 if (local
->wds_type
& HOSTAP_WDS_AP_CLIENT
)
435 hostap_add_sta(local
->ap
, local
->bssid
);
438 /* Get BSSID if we have a valid AP address */
440 netif_carrier_on(local
->dev
);
441 netif_carrier_on(local
->ddev
);
442 memcpy(wrqu
.ap_addr
.sa_data
, local
->bssid
, ETH_ALEN
);
444 netif_carrier_off(local
->dev
);
445 netif_carrier_off(local
->ddev
);
446 eth_zero_addr(wrqu
.ap_addr
.sa_data
);
448 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
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
456 if (connected
|| local
->prev_linkstatus_connected
)
457 wireless_send_event(local
->dev
, SIOCGIWAP
, &wrqu
, NULL
);
458 local
->prev_linkstatus_connected
= connected
;
462 static void handle_info_queue_scanresults(local_info_t
*local
)
464 if (local
->host_roaming
== 1 && local
->iw_mode
== IW_MODE_INFRA
)
465 prism2_host_roaming(local
);
467 if (local
->host_roaming
== 2 && local
->iw_mode
== IW_MODE_INFRA
&&
468 !is_zero_ether_addr(local
->preferred_ap
)) {
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.
476 prism2_host_roaming(local
);
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
)
485 local_info_t
*local
= container_of(work
, local_info_t
, info_queue
);
487 if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS
,
488 &local
->pending_info
))
489 handle_info_queue_linkstatus(local
);
491 if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS
,
492 &local
->pending_info
))
493 handle_info_queue_scanresults(local
);
495 #endif /* PRISM2_NO_STATION_MODES */
498 void hostap_info_init(local_info_t
*local
)
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 */
507 EXPORT_SYMBOL(hostap_info_init
);
508 EXPORT_SYMBOL(hostap_info_process
);