1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
5 #include <linux/etherdevice.h>
14 static const u8 bssid_any
[ETH_ALEN
] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
15 static const u8 bssid_off
[ETH_ALEN
] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17 static void print_assoc_req(const char * extra
, struct assoc_request
* assoc_req
)
20 "#### Association Request: %s\n"
26 " BSSID: " MAC_FMT
"\n"
27 " Encryption:%s%s%s\n"
29 extra
, assoc_req
->flags
,
30 escape_essid(assoc_req
->ssid
, assoc_req
->ssid_len
),
31 assoc_req
->channel
, assoc_req
->band
, assoc_req
->mode
,
32 MAC_ARG(assoc_req
->bssid
),
33 assoc_req
->secinfo
.WPAenabled
? " WPA" : "",
34 assoc_req
->secinfo
.WPA2enabled
? " WPA2" : "",
35 assoc_req
->secinfo
.wep_enabled
? " WEP" : "",
36 assoc_req
->secinfo
.auth_mode
);
40 static int assoc_helper_essid(wlan_private
*priv
,
41 struct assoc_request
* assoc_req
)
43 wlan_adapter
*adapter
= priv
->adapter
;
45 struct bss_descriptor
* bss
;
48 lbs_deb_enter(LBS_DEB_ASSOC
);
50 /* FIXME: take channel into account when picking SSIDs if a channel
54 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
))
55 channel
= assoc_req
->channel
;
57 lbs_deb_assoc("New SSID requested: '%s'\n",
58 escape_essid(assoc_req
->ssid
, assoc_req
->ssid_len
));
59 if (assoc_req
->mode
== IW_MODE_INFRA
) {
60 if (adapter
->prescan
) {
61 libertas_send_specific_ssid_scan(priv
, assoc_req
->ssid
,
62 assoc_req
->ssid_len
, 0);
65 bss
= libertas_find_ssid_in_list(adapter
, assoc_req
->ssid
,
66 assoc_req
->ssid_len
, NULL
, IW_MODE_INFRA
, channel
);
68 lbs_deb_assoc("SSID found in scan list, associating\n");
69 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
70 ret
= wlan_associate(priv
, assoc_req
);
72 lbs_deb_assoc("SSID not found; cannot associate\n");
74 } else if (assoc_req
->mode
== IW_MODE_ADHOC
) {
75 /* Scan for the network, do not save previous results. Stale
76 * scan data will cause us to join a non-existant adhoc network
78 libertas_send_specific_ssid_scan(priv
, assoc_req
->ssid
,
79 assoc_req
->ssid_len
, 1);
81 /* Search for the requested SSID in the scan table */
82 bss
= libertas_find_ssid_in_list(adapter
, assoc_req
->ssid
,
83 assoc_req
->ssid_len
, NULL
, IW_MODE_ADHOC
, channel
);
85 lbs_deb_assoc("SSID found, will join\n");
86 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
87 libertas_join_adhoc_network(priv
, assoc_req
);
89 /* else send START command */
90 lbs_deb_assoc("SSID not found, creating adhoc network\n");
91 memcpy(&assoc_req
->bss
.ssid
, &assoc_req
->ssid
,
93 assoc_req
->bss
.ssid_len
= assoc_req
->ssid_len
;
94 libertas_start_adhoc_network(priv
, assoc_req
);
98 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
103 static int assoc_helper_bssid(wlan_private
*priv
,
104 struct assoc_request
* assoc_req
)
106 wlan_adapter
*adapter
= priv
->adapter
;
108 struct bss_descriptor
* bss
;
110 lbs_deb_enter_args(LBS_DEB_ASSOC
, "BSSID " MAC_FMT
,
111 MAC_ARG(assoc_req
->bssid
));
113 /* Search for index position in list for requested MAC */
114 bss
= libertas_find_bssid_in_list(adapter
, assoc_req
->bssid
,
117 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT
" not found, "
118 "cannot associate.\n", MAC_ARG(assoc_req
->bssid
));
122 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
123 if (assoc_req
->mode
== IW_MODE_INFRA
) {
124 ret
= wlan_associate(priv
, assoc_req
);
125 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret
);
126 } else if (assoc_req
->mode
== IW_MODE_ADHOC
) {
127 libertas_join_adhoc_network(priv
, assoc_req
);
131 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
136 static int assoc_helper_associate(wlan_private
*priv
,
137 struct assoc_request
* assoc_req
)
139 int ret
= 0, done
= 0;
141 /* If we're given and 'any' BSSID, try associating based on SSID */
143 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
144 if (compare_ether_addr(bssid_any
, assoc_req
->bssid
)
145 && compare_ether_addr(bssid_off
, assoc_req
->bssid
)) {
146 ret
= assoc_helper_bssid(priv
, assoc_req
);
149 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret
);
154 if (!done
&& test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
155 ret
= assoc_helper_essid(priv
, assoc_req
);
157 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret
);
165 static int assoc_helper_mode(wlan_private
*priv
,
166 struct assoc_request
* assoc_req
)
168 wlan_adapter
*adapter
= priv
->adapter
;
171 lbs_deb_enter(LBS_DEB_ASSOC
);
173 if (assoc_req
->mode
== adapter
->mode
)
176 if (assoc_req
->mode
== IW_MODE_INFRA
) {
177 if (adapter
->psstate
!= PS_STATE_FULL_POWER
)
178 libertas_ps_wakeup(priv
, cmd_option_waitforrsp
);
179 adapter
->psmode
= wlan802_11powermodecam
;
182 adapter
->mode
= assoc_req
->mode
;
183 ret
= libertas_prepare_and_send_command(priv
,
185 0, cmd_option_waitforrsp
,
186 OID_802_11_INFRASTRUCTURE_MODE
,
187 /* Shoot me now */ (void *) (size_t) assoc_req
->mode
);
190 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
195 static int update_channel(wlan_private
* priv
)
197 /* the channel in f/w could be out of sync, get the current channel */
198 return libertas_prepare_and_send_command(priv
, cmd_802_11_rf_channel
,
199 cmd_opt_802_11_rf_channel_get
,
200 cmd_option_waitforrsp
, 0, NULL
);
203 void libertas_sync_channel(struct work_struct
*work
)
205 wlan_private
*priv
= container_of(work
, wlan_private
, sync_channel
);
207 if (update_channel(priv
) != 0)
208 lbs_pr_info("Channel synchronization failed.");
211 static int assoc_helper_channel(wlan_private
*priv
,
212 struct assoc_request
* assoc_req
)
214 wlan_adapter
*adapter
= priv
->adapter
;
217 lbs_deb_enter(LBS_DEB_ASSOC
);
219 ret
= update_channel(priv
);
221 lbs_deb_assoc("ASSOC: channel: error getting channel.");
224 if (assoc_req
->channel
== adapter
->curbssparams
.channel
)
227 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
228 adapter
->curbssparams
.channel
, assoc_req
->channel
);
230 ret
= libertas_prepare_and_send_command(priv
, cmd_802_11_rf_channel
,
231 cmd_opt_802_11_rf_channel_set
,
232 cmd_option_waitforrsp
, 0, &assoc_req
->channel
);
234 lbs_deb_assoc("ASSOC: channel: error setting channel.");
237 ret
= update_channel(priv
);
239 lbs_deb_assoc("ASSOC: channel: error getting channel.");
242 if (assoc_req
->channel
!= adapter
->curbssparams
.channel
) {
243 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
248 if ( assoc_req
->secinfo
.wep_enabled
249 && (assoc_req
->wep_keys
[0].len
250 || assoc_req
->wep_keys
[1].len
251 || assoc_req
->wep_keys
[2].len
252 || assoc_req
->wep_keys
[3].len
)) {
253 /* Make sure WEP keys are re-sent to firmware */
254 set_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
);
257 /* Must restart/rejoin adhoc networks after channel change */
258 set_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
);
261 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
266 static int assoc_helper_wep_keys(wlan_private
*priv
,
267 struct assoc_request
* assoc_req
)
269 wlan_adapter
*adapter
= priv
->adapter
;
273 lbs_deb_enter(LBS_DEB_ASSOC
);
275 /* Set or remove WEP keys */
276 if ( assoc_req
->wep_keys
[0].len
277 || assoc_req
->wep_keys
[1].len
278 || assoc_req
->wep_keys
[2].len
279 || assoc_req
->wep_keys
[3].len
) {
280 ret
= libertas_prepare_and_send_command(priv
,
283 cmd_option_waitforrsp
,
286 ret
= libertas_prepare_and_send_command(priv
,
289 cmd_option_waitforrsp
,
296 /* enable/disable the MAC's WEP packet filter */
297 if (assoc_req
->secinfo
.wep_enabled
)
298 adapter
->currentpacketfilter
|= cmd_act_mac_wep_enable
;
300 adapter
->currentpacketfilter
&= ~cmd_act_mac_wep_enable
;
301 ret
= libertas_set_mac_packet_filter(priv
);
305 mutex_lock(&adapter
->lock
);
307 /* Copy WEP keys into adapter wep key fields */
308 for (i
= 0; i
< 4; i
++) {
309 memcpy(&adapter
->wep_keys
[i
], &assoc_req
->wep_keys
[i
],
310 sizeof(struct WLAN_802_11_KEY
));
312 adapter
->wep_tx_keyidx
= assoc_req
->wep_tx_keyidx
;
314 mutex_unlock(&adapter
->lock
);
317 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
321 static int assoc_helper_secinfo(wlan_private
*priv
,
322 struct assoc_request
* assoc_req
)
324 wlan_adapter
*adapter
= priv
->adapter
;
327 lbs_deb_enter(LBS_DEB_ASSOC
);
329 memcpy(&adapter
->secinfo
, &assoc_req
->secinfo
,
330 sizeof(struct wlan_802_11_security
));
332 ret
= libertas_set_mac_packet_filter(priv
);
336 /* enable/disable RSN */
337 ret
= libertas_prepare_and_send_command(priv
,
338 cmd_802_11_enable_rsn
,
340 cmd_option_waitforrsp
,
344 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
349 static int assoc_helper_wpa_keys(wlan_private
*priv
,
350 struct assoc_request
* assoc_req
)
354 lbs_deb_enter(LBS_DEB_ASSOC
);
356 ret
= libertas_prepare_and_send_command(priv
,
357 cmd_802_11_key_material
,
359 cmd_option_waitforrsp
,
362 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
367 static int assoc_helper_wpa_ie(wlan_private
*priv
,
368 struct assoc_request
* assoc_req
)
370 wlan_adapter
*adapter
= priv
->adapter
;
373 lbs_deb_enter(LBS_DEB_ASSOC
);
375 if (assoc_req
->secinfo
.WPAenabled
|| assoc_req
->secinfo
.WPA2enabled
) {
376 memcpy(&adapter
->wpa_ie
, &assoc_req
->wpa_ie
, assoc_req
->wpa_ie_len
);
377 adapter
->wpa_ie_len
= assoc_req
->wpa_ie_len
;
379 memset(&adapter
->wpa_ie
, 0, MAX_WPA_IE_LEN
);
380 adapter
->wpa_ie_len
= 0;
383 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
388 static int should_deauth_infrastructure(wlan_adapter
*adapter
,
389 struct assoc_request
* assoc_req
)
391 if (adapter
->connect_status
!= libertas_connected
)
394 if (test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
395 lbs_deb_assoc("Deauthenticating due to new SSID in "
396 " configuration request.\n");
400 if (test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
401 if (adapter
->secinfo
.auth_mode
!= assoc_req
->secinfo
.auth_mode
) {
402 lbs_deb_assoc("Deauthenticating due to updated security "
403 "info in configuration request.\n");
408 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
409 lbs_deb_assoc("Deauthenticating due to new BSSID in "
410 " configuration request.\n");
414 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
415 lbs_deb_assoc("Deauthenticating due to channel switch.\n");
419 /* FIXME: deal with 'auto' mode somehow */
420 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
421 if (assoc_req
->mode
!= IW_MODE_INFRA
)
429 static int should_stop_adhoc(wlan_adapter
*adapter
,
430 struct assoc_request
* assoc_req
)
432 if (adapter
->connect_status
!= libertas_connected
)
435 if (libertas_ssid_cmp(adapter
->curbssparams
.ssid
,
436 adapter
->curbssparams
.ssid_len
,
437 assoc_req
->ssid
, assoc_req
->ssid_len
) != 0)
440 /* FIXME: deal with 'auto' mode somehow */
441 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
442 if (assoc_req
->mode
!= IW_MODE_ADHOC
)
446 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
447 if (assoc_req
->channel
!= adapter
->curbssparams
.channel
)
455 void libertas_association_worker(struct work_struct
*work
)
457 wlan_private
*priv
= container_of(work
, wlan_private
, assoc_work
.work
);
458 wlan_adapter
*adapter
= priv
->adapter
;
459 struct assoc_request
* assoc_req
= NULL
;
461 int find_any_ssid
= 0;
463 lbs_deb_enter(LBS_DEB_ASSOC
);
465 mutex_lock(&adapter
->lock
);
466 assoc_req
= adapter
->pending_assoc_req
;
467 adapter
->pending_assoc_req
= NULL
;
468 adapter
->in_progress_assoc_req
= assoc_req
;
469 mutex_unlock(&adapter
->lock
);
474 print_assoc_req(__func__
, assoc_req
);
476 /* If 'any' SSID was specified, find an SSID to associate with */
477 if (test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)
478 && !assoc_req
->ssid_len
)
481 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
482 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
483 if (compare_ether_addr(assoc_req
->bssid
, bssid_any
)
484 && compare_ether_addr(assoc_req
->bssid
, bssid_off
))
491 ret
= libertas_find_best_network_ssid(priv
, assoc_req
->ssid
,
492 &assoc_req
->ssid_len
, assoc_req
->mode
, &new_mode
);
494 lbs_deb_assoc("Could not find best network\n");
499 /* Ensure we switch to the mode of the AP */
500 if (assoc_req
->mode
== IW_MODE_AUTO
) {
501 set_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
);
502 assoc_req
->mode
= new_mode
;
507 * Check if the attributes being changing require deauthentication
508 * from the currently associated infrastructure access point.
510 if (adapter
->mode
== IW_MODE_INFRA
) {
511 if (should_deauth_infrastructure(adapter
, assoc_req
)) {
512 ret
= libertas_send_deauthentication(priv
);
514 lbs_deb_assoc("Deauthentication due to new "
515 "configuration request failed: %d\n",
519 } else if (adapter
->mode
== IW_MODE_ADHOC
) {
520 if (should_stop_adhoc(adapter
, assoc_req
)) {
521 ret
= libertas_stop_adhoc_network(priv
);
523 lbs_deb_assoc("Teardown of AdHoc network due to "
524 "new configuration request failed: %d\n",
531 /* Send the various configuration bits to the firmware */
532 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
533 ret
= assoc_helper_mode(priv
, assoc_req
);
535 lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__
, ret
);
540 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
541 ret
= assoc_helper_channel(priv
, assoc_req
);
543 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
549 if ( test_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
)
550 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX
, &assoc_req
->flags
)) {
551 ret
= assoc_helper_wep_keys(priv
, assoc_req
);
553 lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__
, ret
);
558 if (test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
559 ret
= assoc_helper_secinfo(priv
, assoc_req
);
561 lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__
, ret
);
566 if (test_bit(ASSOC_FLAG_WPA_IE
, &assoc_req
->flags
)) {
567 ret
= assoc_helper_wpa_ie(priv
, assoc_req
);
569 lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__
, ret
);
574 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
)
575 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
)) {
576 ret
= assoc_helper_wpa_keys(priv
, assoc_req
);
578 lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__
, ret
);
583 /* SSID/BSSID should be the _last_ config option set, because they
584 * trigger the association attempt.
586 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)
587 || test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
590 ret
= assoc_helper_associate(priv
, assoc_req
);
592 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
597 if (adapter
->connect_status
!= libertas_connected
) {
598 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
604 lbs_deb_assoc("ASSOC: association attempt successful. "
605 "Associated to '%s' (" MAC_FMT
")\n",
606 escape_essid(adapter
->curbssparams
.ssid
,
607 adapter
->curbssparams
.ssid_len
),
608 MAC_ARG(adapter
->curbssparams
.bssid
));
609 libertas_prepare_and_send_command(priv
,
611 0, cmd_option_waitforrsp
, 0, NULL
);
613 libertas_prepare_and_send_command(priv
,
615 0, cmd_option_waitforrsp
, 0, NULL
);
623 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
627 mutex_lock(&adapter
->lock
);
628 adapter
->in_progress_assoc_req
= NULL
;
629 mutex_unlock(&adapter
->lock
);
633 lbs_deb_leave(LBS_DEB_ASSOC
);
638 * Caller MUST hold any necessary locks
640 struct assoc_request
* wlan_get_association_request(wlan_adapter
*adapter
)
642 struct assoc_request
* assoc_req
;
644 if (!adapter
->pending_assoc_req
) {
645 adapter
->pending_assoc_req
= kzalloc(sizeof(struct assoc_request
),
647 if (!adapter
->pending_assoc_req
) {
648 lbs_pr_info("Not enough memory to allocate association"
654 /* Copy current configuration attributes to the association request,
655 * but don't overwrite any that are already set.
657 assoc_req
= adapter
->pending_assoc_req
;
658 if (!test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
659 memcpy(&assoc_req
->ssid
, &adapter
->curbssparams
.ssid
,
661 assoc_req
->ssid_len
= adapter
->curbssparams
.ssid_len
;
664 if (!test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
))
665 assoc_req
->channel
= adapter
->curbssparams
.channel
;
667 if (!test_bit(ASSOC_FLAG_BAND
, &assoc_req
->flags
))
668 assoc_req
->band
= adapter
->curbssparams
.band
;
670 if (!test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
))
671 assoc_req
->mode
= adapter
->mode
;
673 if (!test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
674 memcpy(&assoc_req
->bssid
, adapter
->curbssparams
.bssid
,
678 if (!test_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
)) {
680 for (i
= 0; i
< 4; i
++) {
681 memcpy(&assoc_req
->wep_keys
[i
], &adapter
->wep_keys
[i
],
682 sizeof(struct WLAN_802_11_KEY
));
686 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX
, &assoc_req
->flags
))
687 assoc_req
->wep_tx_keyidx
= adapter
->wep_tx_keyidx
;
689 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
)) {
690 memcpy(&assoc_req
->wpa_mcast_key
, &adapter
->wpa_mcast_key
,
691 sizeof(struct WLAN_802_11_KEY
));
694 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
)) {
695 memcpy(&assoc_req
->wpa_unicast_key
, &adapter
->wpa_unicast_key
,
696 sizeof(struct WLAN_802_11_KEY
));
699 if (!test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
700 memcpy(&assoc_req
->secinfo
, &adapter
->secinfo
,
701 sizeof(struct wlan_802_11_security
));
704 if (!test_bit(ASSOC_FLAG_WPA_IE
, &assoc_req
->flags
)) {
705 memcpy(&assoc_req
->wpa_ie
, &adapter
->wpa_ie
,
707 assoc_req
->wpa_ie_len
= adapter
->wpa_ie_len
;
710 print_assoc_req(__func__
, assoc_req
);