1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
5 #include <linux/etherdevice.h>
15 static const u8 bssid_any
[ETH_ALEN
] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17 static const u8 bssid_off
[ETH_ALEN
] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
21 static int assoc_helper_essid(struct lbs_private
*priv
,
22 struct assoc_request
* assoc_req
)
25 struct bss_descriptor
* bss
;
28 lbs_deb_enter(LBS_DEB_ASSOC
);
30 /* FIXME: take channel into account when picking SSIDs if a channel
34 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
))
35 channel
= assoc_req
->channel
;
37 lbs_deb_assoc("SSID '%s' requested\n",
38 escape_essid(assoc_req
->ssid
, assoc_req
->ssid_len
));
39 if (assoc_req
->mode
== IW_MODE_INFRA
) {
40 lbs_send_specific_ssid_scan(priv
, assoc_req
->ssid
,
43 bss
= lbs_find_ssid_in_list(priv
, assoc_req
->ssid
,
44 assoc_req
->ssid_len
, NULL
, IW_MODE_INFRA
, channel
);
46 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
47 ret
= lbs_associate(priv
, assoc_req
);
49 lbs_deb_assoc("SSID not found; cannot associate\n");
51 } else if (assoc_req
->mode
== IW_MODE_ADHOC
) {
52 /* Scan for the network, do not save previous results. Stale
53 * scan data will cause us to join a non-existant adhoc network
55 lbs_send_specific_ssid_scan(priv
, assoc_req
->ssid
,
58 /* Search for the requested SSID in the scan table */
59 bss
= lbs_find_ssid_in_list(priv
, assoc_req
->ssid
,
60 assoc_req
->ssid_len
, NULL
, IW_MODE_ADHOC
, channel
);
62 lbs_deb_assoc("SSID found, will join\n");
63 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
64 lbs_join_adhoc_network(priv
, assoc_req
);
66 /* else send START command */
67 lbs_deb_assoc("SSID not found, creating adhoc network\n");
68 memcpy(&assoc_req
->bss
.ssid
, &assoc_req
->ssid
,
70 assoc_req
->bss
.ssid_len
= assoc_req
->ssid_len
;
71 lbs_start_adhoc_network(priv
, assoc_req
);
75 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
80 static int assoc_helper_bssid(struct lbs_private
*priv
,
81 struct assoc_request
* assoc_req
)
84 struct bss_descriptor
* bss
;
87 lbs_deb_enter_args(LBS_DEB_ASSOC
, "BSSID %s",
88 print_mac(mac
, assoc_req
->bssid
));
90 /* Search for index position in list for requested MAC */
91 bss
= lbs_find_bssid_in_list(priv
, assoc_req
->bssid
,
94 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
95 "cannot associate.\n", print_mac(mac
, assoc_req
->bssid
));
99 memcpy(&assoc_req
->bss
, bss
, sizeof(struct bss_descriptor
));
100 if (assoc_req
->mode
== IW_MODE_INFRA
) {
101 ret
= lbs_associate(priv
, assoc_req
);
102 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret
);
103 } else if (assoc_req
->mode
== IW_MODE_ADHOC
) {
104 lbs_join_adhoc_network(priv
, assoc_req
);
108 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
113 static int assoc_helper_associate(struct lbs_private
*priv
,
114 struct assoc_request
* assoc_req
)
116 int ret
= 0, done
= 0;
118 lbs_deb_enter(LBS_DEB_ASSOC
);
120 /* If we're given and 'any' BSSID, try associating based on SSID */
122 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
123 if (compare_ether_addr(bssid_any
, assoc_req
->bssid
)
124 && compare_ether_addr(bssid_off
, assoc_req
->bssid
)) {
125 ret
= assoc_helper_bssid(priv
, assoc_req
);
130 if (!done
&& test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
131 ret
= assoc_helper_essid(priv
, assoc_req
);
134 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
139 static int assoc_helper_mode(struct lbs_private
*priv
,
140 struct assoc_request
* assoc_req
)
144 lbs_deb_enter(LBS_DEB_ASSOC
);
146 if (assoc_req
->mode
== priv
->mode
)
149 if (assoc_req
->mode
== IW_MODE_INFRA
) {
150 if (priv
->psstate
!= PS_STATE_FULL_POWER
)
151 lbs_ps_wakeup(priv
, CMD_OPTION_WAITFORRSP
);
152 priv
->psmode
= LBS802_11POWERMODECAM
;
155 priv
->mode
= assoc_req
->mode
;
156 ret
= lbs_prepare_and_send_command(priv
,
158 0, CMD_OPTION_WAITFORRSP
,
159 OID_802_11_INFRASTRUCTURE_MODE
,
160 /* Shoot me now */ (void *) (size_t) assoc_req
->mode
);
163 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
168 int lbs_update_channel(struct lbs_private
*priv
)
172 /* the channel in f/w could be out of sync; get the current channel */
173 lbs_deb_enter(LBS_DEB_ASSOC
);
175 ret
= lbs_get_channel(priv
);
177 priv
->curbssparams
.channel
= ret
;
180 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
184 static int assoc_helper_channel(struct lbs_private
*priv
,
185 struct assoc_request
* assoc_req
)
189 lbs_deb_enter(LBS_DEB_ASSOC
);
191 ret
= lbs_update_channel(priv
);
193 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
197 if (assoc_req
->channel
== priv
->curbssparams
.channel
)
200 if (priv
->mesh_dev
) {
201 /* Change mesh channel first; 21.p21 firmware won't let
202 you change channel otherwise (even though it'll return
204 lbs_mesh_config(priv
, 0, assoc_req
->channel
);
207 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
208 priv
->curbssparams
.channel
, assoc_req
->channel
);
210 ret
= lbs_set_channel(priv
, assoc_req
->channel
);
212 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
214 /* FIXME: shouldn't need to grab the channel _again_ after setting
215 * it since the firmware is supposed to return the new channel, but
217 ret
= lbs_update_channel(priv
);
219 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
223 if (assoc_req
->channel
!= priv
->curbssparams
.channel
) {
224 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
229 if ( assoc_req
->secinfo
.wep_enabled
230 && (assoc_req
->wep_keys
[0].len
231 || assoc_req
->wep_keys
[1].len
232 || assoc_req
->wep_keys
[2].len
233 || assoc_req
->wep_keys
[3].len
)) {
234 /* Make sure WEP keys are re-sent to firmware */
235 set_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
);
238 /* Must restart/rejoin adhoc networks after channel change */
239 set_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
);
243 lbs_mesh_config(priv
, 1, priv
->curbssparams
.channel
);
246 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
251 static int assoc_helper_wep_keys(struct lbs_private
*priv
,
252 struct assoc_request
*assoc_req
)
257 lbs_deb_enter(LBS_DEB_ASSOC
);
259 /* Set or remove WEP keys */
260 if (assoc_req
->wep_keys
[0].len
|| assoc_req
->wep_keys
[1].len
||
261 assoc_req
->wep_keys
[2].len
|| assoc_req
->wep_keys
[3].len
)
262 ret
= lbs_cmd_802_11_set_wep(priv
, CMD_ACT_ADD
, assoc_req
);
264 ret
= lbs_cmd_802_11_set_wep(priv
, CMD_ACT_REMOVE
, assoc_req
);
269 /* enable/disable the MAC's WEP packet filter */
270 if (assoc_req
->secinfo
.wep_enabled
)
271 priv
->currentpacketfilter
|= CMD_ACT_MAC_WEP_ENABLE
;
273 priv
->currentpacketfilter
&= ~CMD_ACT_MAC_WEP_ENABLE
;
275 ret
= lbs_set_mac_packet_filter(priv
);
279 mutex_lock(&priv
->lock
);
281 /* Copy WEP keys into priv wep key fields */
282 for (i
= 0; i
< 4; i
++) {
283 memcpy(&priv
->wep_keys
[i
], &assoc_req
->wep_keys
[i
],
284 sizeof(struct enc_key
));
286 priv
->wep_tx_keyidx
= assoc_req
->wep_tx_keyidx
;
288 mutex_unlock(&priv
->lock
);
291 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
295 static int assoc_helper_secinfo(struct lbs_private
*priv
,
296 struct assoc_request
* assoc_req
)
302 lbs_deb_enter(LBS_DEB_ASSOC
);
304 memcpy(&priv
->secinfo
, &assoc_req
->secinfo
,
305 sizeof(struct lbs_802_11_security
));
307 ret
= lbs_set_mac_packet_filter(priv
);
311 /* If RSN is already enabled, don't try to enable it again, since
312 * ENABLE_RSN resets internal state machines and will clobber the
313 * 4-way WPA handshake.
316 /* Get RSN enabled/disabled */
317 ret
= lbs_cmd_802_11_enable_rsn(priv
, CMD_ACT_GET
, &rsn
);
319 lbs_deb_assoc("Failed to get RSN status: %d\n", ret
);
323 /* Don't re-enable RSN if it's already enabled */
324 do_wpa
= assoc_req
->secinfo
.WPAenabled
|| assoc_req
->secinfo
.WPA2enabled
;
328 /* Set RSN enabled/disabled */
329 ret
= lbs_cmd_802_11_enable_rsn(priv
, CMD_ACT_SET
, &do_wpa
);
332 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
337 static int assoc_helper_wpa_keys(struct lbs_private
*priv
,
338 struct assoc_request
* assoc_req
)
341 unsigned int flags
= assoc_req
->flags
;
343 lbs_deb_enter(LBS_DEB_ASSOC
);
345 /* Work around older firmware bug where WPA unicast and multicast
346 * keys must be set independently. Seen in SDIO parts with firmware
350 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
)) {
351 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
);
352 ret
= lbs_cmd_802_11_key_material(priv
, CMD_ACT_SET
, assoc_req
);
353 assoc_req
->flags
= flags
;
359 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
)) {
360 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
);
362 ret
= lbs_cmd_802_11_key_material(priv
, CMD_ACT_SET
, assoc_req
);
363 assoc_req
->flags
= flags
;
367 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
372 static int assoc_helper_wpa_ie(struct lbs_private
*priv
,
373 struct assoc_request
* assoc_req
)
377 lbs_deb_enter(LBS_DEB_ASSOC
);
379 if (assoc_req
->secinfo
.WPAenabled
|| assoc_req
->secinfo
.WPA2enabled
) {
380 memcpy(&priv
->wpa_ie
, &assoc_req
->wpa_ie
, assoc_req
->wpa_ie_len
);
381 priv
->wpa_ie_len
= assoc_req
->wpa_ie_len
;
383 memset(&priv
->wpa_ie
, 0, MAX_WPA_IE_LEN
);
384 priv
->wpa_ie_len
= 0;
387 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
392 static int should_deauth_infrastructure(struct lbs_private
*priv
,
393 struct assoc_request
* assoc_req
)
397 if (priv
->connect_status
!= LBS_CONNECTED
)
400 lbs_deb_enter(LBS_DEB_ASSOC
);
401 if (test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
402 lbs_deb_assoc("Deauthenticating due to new SSID\n");
407 if (test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
408 if (priv
->secinfo
.auth_mode
!= assoc_req
->secinfo
.auth_mode
) {
409 lbs_deb_assoc("Deauthenticating due to new security\n");
415 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
416 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
421 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
422 lbs_deb_assoc("Deauthenticating due to channel switch\n");
427 /* FIXME: deal with 'auto' mode somehow */
428 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
429 if (assoc_req
->mode
!= IW_MODE_INFRA
) {
430 lbs_deb_assoc("Deauthenticating due to leaving "
438 lbs_deb_leave_args(LBS_DEB_ASSOC
, "ret %d", ret
);
443 static int should_stop_adhoc(struct lbs_private
*priv
,
444 struct assoc_request
* assoc_req
)
446 lbs_deb_enter(LBS_DEB_ASSOC
);
448 if (priv
->connect_status
!= LBS_CONNECTED
)
451 if (lbs_ssid_cmp(priv
->curbssparams
.ssid
,
452 priv
->curbssparams
.ssid_len
,
453 assoc_req
->ssid
, assoc_req
->ssid_len
) != 0)
456 /* FIXME: deal with 'auto' mode somehow */
457 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
458 if (assoc_req
->mode
!= IW_MODE_ADHOC
)
462 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
463 if (assoc_req
->channel
!= priv
->curbssparams
.channel
)
467 lbs_deb_leave(LBS_DEB_ASSOC
);
472 void lbs_association_worker(struct work_struct
*work
)
474 struct lbs_private
*priv
= container_of(work
, struct lbs_private
,
476 struct assoc_request
* assoc_req
= NULL
;
478 int find_any_ssid
= 0;
479 DECLARE_MAC_BUF(mac
);
481 lbs_deb_enter(LBS_DEB_ASSOC
);
483 mutex_lock(&priv
->lock
);
484 assoc_req
= priv
->pending_assoc_req
;
485 priv
->pending_assoc_req
= NULL
;
486 priv
->in_progress_assoc_req
= assoc_req
;
487 mutex_unlock(&priv
->lock
);
493 "Association Request:\n"
503 escape_essid(assoc_req
->ssid
, assoc_req
->ssid_len
),
504 assoc_req
->channel
, assoc_req
->band
, assoc_req
->mode
,
505 print_mac(mac
, assoc_req
->bssid
),
506 assoc_req
->secinfo
.WPAenabled
? " WPA" : "",
507 assoc_req
->secinfo
.WPA2enabled
? " WPA2" : "",
508 assoc_req
->secinfo
.wep_enabled
? " WEP" : "",
509 assoc_req
->secinfo
.auth_mode
);
511 /* If 'any' SSID was specified, find an SSID to associate with */
512 if (test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)
513 && !assoc_req
->ssid_len
)
516 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
517 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
518 if (compare_ether_addr(assoc_req
->bssid
, bssid_any
)
519 && compare_ether_addr(assoc_req
->bssid
, bssid_off
))
526 ret
= lbs_find_best_network_ssid(priv
, assoc_req
->ssid
,
527 &assoc_req
->ssid_len
, assoc_req
->mode
, &new_mode
);
529 lbs_deb_assoc("Could not find best network\n");
534 /* Ensure we switch to the mode of the AP */
535 if (assoc_req
->mode
== IW_MODE_AUTO
) {
536 set_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
);
537 assoc_req
->mode
= new_mode
;
542 * Check if the attributes being changing require deauthentication
543 * from the currently associated infrastructure access point.
545 if (priv
->mode
== IW_MODE_INFRA
) {
546 if (should_deauth_infrastructure(priv
, assoc_req
)) {
547 ret
= lbs_send_deauthentication(priv
);
549 lbs_deb_assoc("Deauthentication due to new "
550 "configuration request failed: %d\n",
554 } else if (priv
->mode
== IW_MODE_ADHOC
) {
555 if (should_stop_adhoc(priv
, assoc_req
)) {
556 ret
= lbs_stop_adhoc_network(priv
);
558 lbs_deb_assoc("Teardown of AdHoc network due to "
559 "new configuration request failed: %d\n",
566 /* Send the various configuration bits to the firmware */
567 if (test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
)) {
568 ret
= assoc_helper_mode(priv
, assoc_req
);
573 if (test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
)) {
574 ret
= assoc_helper_channel(priv
, assoc_req
);
579 if ( test_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
)
580 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX
, &assoc_req
->flags
)) {
581 ret
= assoc_helper_wep_keys(priv
, assoc_req
);
586 if (test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
587 ret
= assoc_helper_secinfo(priv
, assoc_req
);
592 if (test_bit(ASSOC_FLAG_WPA_IE
, &assoc_req
->flags
)) {
593 ret
= assoc_helper_wpa_ie(priv
, assoc_req
);
598 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
)
599 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
)) {
600 ret
= assoc_helper_wpa_keys(priv
, assoc_req
);
605 /* SSID/BSSID should be the _last_ config option set, because they
606 * trigger the association attempt.
608 if (test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)
609 || test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
612 ret
= assoc_helper_associate(priv
, assoc_req
);
614 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
619 if (priv
->connect_status
!= LBS_CONNECTED
) {
620 lbs_deb_assoc("ASSOC: association unsuccessful, "
626 lbs_deb_assoc("associated to %s\n",
627 print_mac(mac
, priv
->curbssparams
.bssid
));
628 lbs_prepare_and_send_command(priv
,
630 0, CMD_OPTION_WAITFORRSP
, 0, NULL
);
632 lbs_prepare_and_send_command(priv
,
634 0, CMD_OPTION_WAITFORRSP
, 0, NULL
);
642 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
646 mutex_lock(&priv
->lock
);
647 priv
->in_progress_assoc_req
= NULL
;
648 mutex_unlock(&priv
->lock
);
652 lbs_deb_leave(LBS_DEB_ASSOC
);
657 * Caller MUST hold any necessary locks
659 struct assoc_request
*lbs_get_association_request(struct lbs_private
*priv
)
661 struct assoc_request
* assoc_req
;
663 lbs_deb_enter(LBS_DEB_ASSOC
);
664 if (!priv
->pending_assoc_req
) {
665 priv
->pending_assoc_req
= kzalloc(sizeof(struct assoc_request
),
667 if (!priv
->pending_assoc_req
) {
668 lbs_pr_info("Not enough memory to allocate association"
674 /* Copy current configuration attributes to the association request,
675 * but don't overwrite any that are already set.
677 assoc_req
= priv
->pending_assoc_req
;
678 if (!test_bit(ASSOC_FLAG_SSID
, &assoc_req
->flags
)) {
679 memcpy(&assoc_req
->ssid
, &priv
->curbssparams
.ssid
,
681 assoc_req
->ssid_len
= priv
->curbssparams
.ssid_len
;
684 if (!test_bit(ASSOC_FLAG_CHANNEL
, &assoc_req
->flags
))
685 assoc_req
->channel
= priv
->curbssparams
.channel
;
687 if (!test_bit(ASSOC_FLAG_BAND
, &assoc_req
->flags
))
688 assoc_req
->band
= priv
->curbssparams
.band
;
690 if (!test_bit(ASSOC_FLAG_MODE
, &assoc_req
->flags
))
691 assoc_req
->mode
= priv
->mode
;
693 if (!test_bit(ASSOC_FLAG_BSSID
, &assoc_req
->flags
)) {
694 memcpy(&assoc_req
->bssid
, priv
->curbssparams
.bssid
,
698 if (!test_bit(ASSOC_FLAG_WEP_KEYS
, &assoc_req
->flags
)) {
700 for (i
= 0; i
< 4; i
++) {
701 memcpy(&assoc_req
->wep_keys
[i
], &priv
->wep_keys
[i
],
702 sizeof(struct enc_key
));
706 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX
, &assoc_req
->flags
))
707 assoc_req
->wep_tx_keyidx
= priv
->wep_tx_keyidx
;
709 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY
, &assoc_req
->flags
)) {
710 memcpy(&assoc_req
->wpa_mcast_key
, &priv
->wpa_mcast_key
,
711 sizeof(struct enc_key
));
714 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY
, &assoc_req
->flags
)) {
715 memcpy(&assoc_req
->wpa_unicast_key
, &priv
->wpa_unicast_key
,
716 sizeof(struct enc_key
));
719 if (!test_bit(ASSOC_FLAG_SECINFO
, &assoc_req
->flags
)) {
720 memcpy(&assoc_req
->secinfo
, &priv
->secinfo
,
721 sizeof(struct lbs_802_11_security
));
724 if (!test_bit(ASSOC_FLAG_WPA_IE
, &assoc_req
->flags
)) {
725 memcpy(&assoc_req
->wpa_ie
, &priv
->wpa_ie
,
727 assoc_req
->wpa_ie_len
= priv
->wpa_ie_len
;
730 lbs_deb_leave(LBS_DEB_ASSOC
);