1 /******************************************************************************
2  * rtl871x_ioctl_set.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>
25  * Larry Finger <Larry.Finger@lwfinger.net>
26  *
27  ******************************************************************************/
28 
29 #define _RTL871X_IOCTL_SET_C_
30 
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "rtl871x_ioctl_set.h"
34 #include "usb_osintf.h"
35 #include "usb_ops.h"
36 
37 
validate_ssid(struct ndis_802_11_ssid * ssid)38 static u8 validate_ssid(struct ndis_802_11_ssid *ssid)
39 {
40 	u8 i;
41 
42 	if (ssid->SsidLength > 32)
43 		return false;
44 	for (i = 0; i < ssid->SsidLength; i++) {
45 		/* wifi, printable ascii code must be supported */
46 		if (!((ssid->Ssid[i] >= 0x20) && (ssid->Ssid[i] <= 0x7e)))
47 			return false;
48 	}
49 	return true;
50 }
51 
do_join(struct _adapter * padapter)52 static u8 do_join(struct _adapter *padapter)
53 {
54 	struct list_head *plist, *phead;
55 	u8 *pibss = NULL;
56 	struct	mlme_priv	*pmlmepriv = &(padapter->mlmepriv);
57 	struct  __queue	*queue	= &(pmlmepriv->scanned_queue);
58 	int ret;
59 
60 	phead = &queue->queue;
61 	plist = phead->next;
62 	pmlmepriv->cur_network.join_res = -2;
63 	pmlmepriv->fw_state |= _FW_UNDER_LINKING;
64 	pmlmepriv->pscanned = plist;
65 	pmlmepriv->to_join = true;
66 
67 	/* adhoc mode will start with an empty queue, but skip checking */
68 	if (!check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) &&
69 	    list_empty(&queue->queue)) {
70 		if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
71 			pmlmepriv->fw_state ^= _FW_UNDER_LINKING;
72 		/* when set_ssid/set_bssid for do_join(), but scanning queue
73 		 * is empty we try to issue sitesurvey firstly
74 		 */
75 		if (!pmlmepriv->sitesurveyctrl.traffic_busy)
76 			r8712_sitesurvey_cmd(padapter, &pmlmepriv->assoc_ssid);
77 		return true;
78 	}
79 
80 	ret = r8712_select_and_join_from_scan(pmlmepriv);
81 	if (ret == _SUCCESS) {
82 		mod_timer(&pmlmepriv->assoc_timer,
83 			  jiffies + msecs_to_jiffies(MAX_JOIN_TIMEOUT));
84 	} else {
85 		if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
86 			/* submit r8712_createbss_cmd to change to an
87 			 * ADHOC_MASTER pmlmepriv->lock has been
88 			 * acquired by caller...
89 			 */
90 			struct wlan_bssid_ex *pdev_network =
91 				&(padapter->registrypriv.dev_network);
92 			pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
93 			pibss = padapter->registrypriv.dev_network.MacAddress;
94 			memcpy(&pdev_network->Ssid,
95 			       &pmlmepriv->assoc_ssid,
96 			       sizeof(struct ndis_802_11_ssid));
97 			r8712_update_registrypriv_dev_network(padapter);
98 			r8712_generate_random_ibss(pibss);
99 			if (r8712_createbss_cmd(padapter) != _SUCCESS)
100 				return false;
101 			pmlmepriv->to_join = false;
102 		} else {
103 			/* can't associate ; reset under-linking */
104 			if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
105 				pmlmepriv->fw_state ^=
106 					_FW_UNDER_LINKING;
107 			/* when set_ssid/set_bssid for do_join(), but
108 			 * there are no desired bss in scanning queue
109 			 * we try to issue sitesurvey first
110 			 */
111 			if (!pmlmepriv->sitesurveyctrl.traffic_busy)
112 				r8712_sitesurvey_cmd(padapter,
113 						     &pmlmepriv->assoc_ssid);
114 		}
115 	}
116 	return true;
117 }
118 
r8712_set_802_11_bssid(struct _adapter * padapter,u8 * bssid)119 u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
120 {
121 	unsigned long irqL;
122 	u8 status = true;
123 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
124 
125 	if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) {
126 		status = false;
127 		return status;
128 	}
129 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
130 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
131 	    _FW_UNDER_LINKING)) {
132 		status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
133 		goto _Abort_Set_BSSID;
134 	}
135 	if (check_fwstate(pmlmepriv,
136 	    _FW_LINKED | WIFI_ADHOC_MASTER_STATE)) {
137 		if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
138 		    ETH_ALEN)) {
139 			if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE))
140 				/* driver is in
141 				 * WIFI_ADHOC_MASTER_STATE
142 				 */
143 				goto _Abort_Set_BSSID;
144 		} else {
145 			r8712_disassoc_cmd(padapter);
146 			if (check_fwstate(pmlmepriv, _FW_LINKED))
147 				r8712_ind_disconnect(padapter);
148 			r8712_free_assoc_resources(padapter);
149 			if ((check_fwstate(pmlmepriv,
150 			     WIFI_ADHOC_MASTER_STATE))) {
151 				_clr_fwstate_(pmlmepriv,
152 					      WIFI_ADHOC_MASTER_STATE);
153 				set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
154 			}
155 		}
156 	}
157 	memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
158 	pmlmepriv->assoc_by_bssid = true;
159 	status = do_join(padapter);
160 	goto done;
161 _Abort_Set_BSSID:
162 done:
163 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
164 	return status;
165 }
166 
r8712_set_802_11_ssid(struct _adapter * padapter,struct ndis_802_11_ssid * ssid)167 void r8712_set_802_11_ssid(struct _adapter *padapter,
168 			   struct ndis_802_11_ssid *ssid)
169 {
170 	unsigned long irqL;
171 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
172 	struct wlan_network *pnetwork = &pmlmepriv->cur_network;
173 
174 	if (!padapter->hw_init_completed)
175 		return;
176 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
177 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING)) {
178 		check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
179 		goto _Abort_Set_SSID;
180 	}
181 	if (check_fwstate(pmlmepriv, _FW_LINKED | WIFI_ADHOC_MASTER_STATE)) {
182 		if ((pmlmepriv->assoc_ssid.SsidLength == ssid->SsidLength) &&
183 		    (!memcmp(&pmlmepriv->assoc_ssid.Ssid, ssid->Ssid,
184 		    ssid->SsidLength))) {
185 			if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
186 				if (!r8712_is_same_ibss(padapter,
187 				     pnetwork)) {
188 					/* if in WIFI_ADHOC_MASTER_STATE or
189 					 *  WIFI_ADHOC_STATE, create bss or
190 					 * rejoin again
191 					 */
192 					r8712_disassoc_cmd(padapter);
193 					if (check_fwstate(pmlmepriv,
194 					    _FW_LINKED))
195 						r8712_ind_disconnect(padapter);
196 					r8712_free_assoc_resources(padapter);
197 					if (check_fwstate(pmlmepriv,
198 					     WIFI_ADHOC_MASTER_STATE)) {
199 						_clr_fwstate_(pmlmepriv,
200 						    WIFI_ADHOC_MASTER_STATE);
201 						set_fwstate(pmlmepriv,
202 							    WIFI_ADHOC_STATE);
203 					}
204 				} else {
205 					/* driver is in
206 					 * WIFI_ADHOC_MASTER_STATE
207 					 */
208 					goto _Abort_Set_SSID;
209 				}
210 			}
211 		} else {
212 			r8712_disassoc_cmd(padapter);
213 			if (check_fwstate(pmlmepriv, _FW_LINKED))
214 				r8712_ind_disconnect(padapter);
215 			r8712_free_assoc_resources(padapter);
216 			if (check_fwstate(pmlmepriv,
217 			    WIFI_ADHOC_MASTER_STATE)) {
218 				_clr_fwstate_(pmlmepriv,
219 					      WIFI_ADHOC_MASTER_STATE);
220 				set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
221 			}
222 		}
223 	}
224 	if (padapter->securitypriv.btkip_countermeasure)
225 		goto _Abort_Set_SSID;
226 	if (!validate_ssid(ssid))
227 		goto _Abort_Set_SSID;
228 	memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
229 	pmlmepriv->assoc_by_bssid = false;
230 	do_join(padapter);
231 	goto done;
232 _Abort_Set_SSID:
233 done:
234 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
235 }
236 
r8712_set_802_11_infrastructure_mode(struct _adapter * padapter,enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)237 void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
238 	enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
239 {
240 	unsigned long irqL;
241 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
242 	struct wlan_network	*cur_network = &pmlmepriv->cur_network;
243 	enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
244 				&(cur_network->network.InfrastructureMode);
245 
246 	if (*pold_state != networktype) {
247 		spin_lock_irqsave(&pmlmepriv->lock, irqL);
248 		if (check_fwstate(pmlmepriv, _FW_LINKED) ||
249 		    (*pold_state == Ndis802_11IBSS))
250 			r8712_disassoc_cmd(padapter);
251 		if (check_fwstate(pmlmepriv,
252 		    _FW_LINKED | WIFI_ADHOC_MASTER_STATE))
253 			r8712_free_assoc_resources(padapter);
254 		if (check_fwstate(pmlmepriv, _FW_LINKED) ||
255 		    (*pold_state == Ndis802_11Infrastructure) ||
256 		    (*pold_state == Ndis802_11IBSS)) {
257 			/* will clr Linked_state before this function,
258 			 * we must have checked whether issue dis-assoc_cmd or
259 			 * not
260 			 */
261 			r8712_ind_disconnect(padapter);
262 		}
263 		*pold_state = networktype;
264 		/* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
265 		 * WIFI_ADHOC_MASTER_STATE
266 		 */
267 		_clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
268 			      WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE);
269 		switch (networktype) {
270 		case Ndis802_11IBSS:
271 			set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
272 			break;
273 		case Ndis802_11Infrastructure:
274 			set_fwstate(pmlmepriv, WIFI_STATION_STATE);
275 			break;
276 		case Ndis802_11APMode:
277 			set_fwstate(pmlmepriv, WIFI_AP_STATE);
278 			break;
279 		case Ndis802_11AutoUnknown:
280 		case Ndis802_11InfrastructureMax:
281 			break;
282 		}
283 		spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
284 	}
285 }
286 
r8712_set_802_11_disassociate(struct _adapter * padapter)287 u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
288 {
289 	unsigned long irqL;
290 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
291 
292 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
293 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
294 		r8712_disassoc_cmd(padapter);
295 		r8712_ind_disconnect(padapter);
296 		r8712_free_assoc_resources(padapter);
297 	}
298 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
299 	return true;
300 }
301 
r8712_set_802_11_bssid_list_scan(struct _adapter * padapter)302 u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
303 {
304 	struct mlme_priv *pmlmepriv = NULL;
305 	unsigned long irqL;
306 	u8 ret = true;
307 
308 	if (!padapter)
309 		return false;
310 	pmlmepriv = &padapter->mlmepriv;
311 	if (!padapter->hw_init_completed)
312 		return false;
313 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
314 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING) ||
315 	    pmlmepriv->sitesurveyctrl.traffic_busy) {
316 		/* Scan or linking is in progress, do nothing. */
317 		ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
318 	} else {
319 		r8712_free_network_queue(padapter);
320 		ret = r8712_sitesurvey_cmd(padapter, NULL);
321 	}
322 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
323 	return ret;
324 }
325 
r8712_set_802_11_authentication_mode(struct _adapter * padapter,enum NDIS_802_11_AUTHENTICATION_MODE authmode)326 u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
327 				enum NDIS_802_11_AUTHENTICATION_MODE authmode)
328 {
329 	struct security_priv *psecuritypriv = &padapter->securitypriv;
330 	u8 ret;
331 
332 	psecuritypriv->ndisauthtype = authmode;
333 	if (psecuritypriv->ndisauthtype > 3)
334 		psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
335 	if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
336 		ret = true;
337 	else
338 		ret = false;
339 	return ret;
340 }
341 
r8712_set_802_11_add_wep(struct _adapter * padapter,struct NDIS_802_11_WEP * wep)342 u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
343 			    struct NDIS_802_11_WEP *wep)
344 {
345 	sint	keyid;
346 	struct security_priv *psecuritypriv = &padapter->securitypriv;
347 
348 	keyid = wep->KeyIndex & 0x3fffffff;
349 	if (keyid >= WEP_KEYS)
350 		return false;
351 	switch (wep->KeyLength) {
352 	case 5:
353 		psecuritypriv->PrivacyAlgrthm = _WEP40_;
354 		break;
355 	case 13:
356 		psecuritypriv->PrivacyAlgrthm = _WEP104_;
357 		break;
358 	default:
359 		psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
360 		break;
361 	}
362 	memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
363 		wep->KeyLength);
364 	psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
365 	psecuritypriv->PrivacyKeyIndex = keyid;
366 	if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)
367 		return false;
368 	return _SUCCESS;
369 }
370