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 #define IS_MAC_ADDRESS_BROADCAST(addr) \
38 ( \
39 	((addr[0] == 0xff) && (addr[1] == 0xff) && \
40 	 (addr[2] == 0xff) && (addr[3] == 0xff) && \
41 	 (addr[4] == 0xff) && (addr[5] == 0xff)) ? true : false \
42 )
43 
validate_ssid(struct ndis_802_11_ssid * ssid)44 static u8 validate_ssid(struct ndis_802_11_ssid *ssid)
45 {
46 	u8 i;
47 
48 	if (ssid->SsidLength > 32)
49 		return false;
50 	for (i = 0; i < ssid->SsidLength; i++) {
51 		/* wifi, printable ascii code must be supported */
52 		if (!((ssid->Ssid[i] >= 0x20) && (ssid->Ssid[i] <= 0x7e)))
53 			return false;
54 	}
55 	return true;
56 }
57 
do_join(struct _adapter * padapter)58 static u8 do_join(struct _adapter *padapter)
59 {
60 	struct list_head *plist, *phead;
61 	u8 *pibss = NULL;
62 	struct	mlme_priv	*pmlmepriv = &(padapter->mlmepriv);
63 	struct  __queue	*queue	= &(pmlmepriv->scanned_queue);
64 
65 	phead = get_list_head(queue);
66 	plist = get_next(phead);
67 	pmlmepriv->cur_network.join_res = -2;
68 	pmlmepriv->fw_state |= _FW_UNDER_LINKING;
69 	pmlmepriv->pscanned = plist;
70 	pmlmepriv->to_join = true;
71 
72 	/* adhoc mode will start with an empty queue, but skip checking */
73 	if (!check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) &&
74 	    _queue_empty(queue)) {
75 		if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
76 			pmlmepriv->fw_state ^= _FW_UNDER_LINKING;
77 		/* when set_ssid/set_bssid for do_join(), but scanning queue
78 		 * is empty we try to issue sitesurvey firstly
79 		 */
80 		if (pmlmepriv->sitesurveyctrl.traffic_busy == false)
81 			r8712_sitesurvey_cmd(padapter, &pmlmepriv->assoc_ssid);
82 		return true;
83 	} else {
84 		int ret;
85 
86 		ret = r8712_select_and_join_from_scan(pmlmepriv);
87 		if (ret == _SUCCESS)
88 			_set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
89 		else {
90 			if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
91 				/* submit r8712_createbss_cmd to change to an
92 				 * ADHOC_MASTER pmlmepriv->lock has been
93 				 * acquired by caller...
94 				 */
95 				struct wlan_bssid_ex *pdev_network =
96 					&(padapter->registrypriv.dev_network);
97 				pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
98 				pibss = padapter->registrypriv.dev_network.
99 					MacAddress;
100 				memset(&pdev_network->Ssid, 0,
101 					sizeof(struct ndis_802_11_ssid));
102 				memcpy(&pdev_network->Ssid,
103 					&pmlmepriv->assoc_ssid,
104 					sizeof(struct ndis_802_11_ssid));
105 				r8712_update_registrypriv_dev_network(padapter);
106 				r8712_generate_random_ibss(pibss);
107 				if (r8712_createbss_cmd(padapter) != _SUCCESS)
108 					return false;
109 				pmlmepriv->to_join = false;
110 			} else {
111 				/* can't associate ; reset under-linking */
112 				if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
113 					pmlmepriv->fw_state ^=
114 							     _FW_UNDER_LINKING;
115 				/* when set_ssid/set_bssid for do_join(), but
116 				 * there are no desired bss in scanning queue
117 				 * we try to issue sitesurvey first
118 				 */
119 				if (!pmlmepriv->sitesurveyctrl.traffic_busy)
120 					r8712_sitesurvey_cmd(padapter,
121 						       &pmlmepriv->assoc_ssid);
122 			}
123 		}
124 	}
125 	return true;
126 }
127 
r8712_set_802_11_bssid(struct _adapter * padapter,u8 * bssid)128 u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
129 {
130 	unsigned long irqL;
131 	u8 status = true;
132 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
133 
134 	if ((bssid[0] == 0x00 && bssid[1] == 0x00 && bssid[2] == 0x00 &&
135 	     bssid[3] == 0x00 && bssid[4] == 0x00 && bssid[5] == 0x00) ||
136 	    (bssid[0] == 0xFF && bssid[1] == 0xFF && bssid[2] == 0xFF &&
137 	     bssid[3] == 0xFF && bssid[4] == 0xFF && bssid[5] == 0xFF)) {
138 		status = false;
139 		return status;
140 	}
141 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
142 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
143 	    _FW_UNDER_LINKING) == true) {
144 		status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
145 		goto _Abort_Set_BSSID;
146 	}
147 	if (check_fwstate(pmlmepriv,
148 	    _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true) {
149 		if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
150 		    ETH_ALEN)) {
151 			if (check_fwstate(pmlmepriv,
152 			    WIFI_STATION_STATE) == false)
153 				goto _Abort_Set_BSSID; /* driver is in
154 						* WIFI_ADHOC_MASTER_STATE */
155 		} else {
156 			r8712_disassoc_cmd(padapter);
157 			if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
158 				r8712_ind_disconnect(padapter);
159 			r8712_free_assoc_resources(padapter);
160 			if ((check_fwstate(pmlmepriv,
161 			     WIFI_ADHOC_MASTER_STATE))) {
162 				_clr_fwstate_(pmlmepriv,
163 					      WIFI_ADHOC_MASTER_STATE);
164 				set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
165 			}
166 		}
167 	}
168 	memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
169 	pmlmepriv->assoc_by_bssid = true;
170 	status = do_join(padapter);
171 	goto done;
172 _Abort_Set_BSSID:
173 done:
174 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
175 	return status;
176 }
177 
r8712_set_802_11_ssid(struct _adapter * padapter,struct ndis_802_11_ssid * ssid)178 void r8712_set_802_11_ssid(struct _adapter *padapter,
179 			   struct ndis_802_11_ssid *ssid)
180 {
181 	unsigned long irqL;
182 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
183 	struct wlan_network *pnetwork = &pmlmepriv->cur_network;
184 
185 	if (padapter->hw_init_completed == false)
186 		return;
187 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
188 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) {
189 		check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
190 		goto _Abort_Set_SSID;
191 	}
192 	if (check_fwstate(pmlmepriv, _FW_LINKED|WIFI_ADHOC_MASTER_STATE)) {
193 		if ((pmlmepriv->assoc_ssid.SsidLength == ssid->SsidLength) &&
194 		    (!memcmp(&pmlmepriv->assoc_ssid.Ssid, ssid->Ssid,
195 		    ssid->SsidLength))) {
196 			if ((check_fwstate(pmlmepriv,
197 			     WIFI_STATION_STATE) == false)) {
198 				if (r8712_is_same_ibss(padapter,
199 				     pnetwork) == false) {
200 					/* if in WIFI_ADHOC_MASTER_STATE or
201 					 *  WIFI_ADHOC_STATE, create bss or
202 					 * rejoin again
203 					 */
204 					r8712_disassoc_cmd(padapter);
205 					if (check_fwstate(pmlmepriv,
206 					    _FW_LINKED) == true)
207 						r8712_ind_disconnect(padapter);
208 					r8712_free_assoc_resources(padapter);
209 					if (check_fwstate(pmlmepriv,
210 					     WIFI_ADHOC_MASTER_STATE)) {
211 						_clr_fwstate_(pmlmepriv,
212 						    WIFI_ADHOC_MASTER_STATE);
213 						set_fwstate(pmlmepriv,
214 							    WIFI_ADHOC_STATE);
215 					}
216 				} else
217 					goto _Abort_Set_SSID; /* driver is in
218 						  * WIFI_ADHOC_MASTER_STATE */
219 			}
220 		} else {
221 			r8712_disassoc_cmd(padapter);
222 			if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
223 				r8712_ind_disconnect(padapter);
224 			r8712_free_assoc_resources(padapter);
225 			if (check_fwstate(pmlmepriv,
226 			    WIFI_ADHOC_MASTER_STATE) == true) {
227 				_clr_fwstate_(pmlmepriv,
228 					      WIFI_ADHOC_MASTER_STATE);
229 				set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
230 			}
231 		}
232 	}
233 	if (padapter->securitypriv.btkip_countermeasure == true)
234 		goto _Abort_Set_SSID;
235 	if (validate_ssid(ssid) == false)
236 		goto _Abort_Set_SSID;
237 	memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
238 	pmlmepriv->assoc_by_bssid = false;
239 	do_join(padapter);
240 	goto done;
241 _Abort_Set_SSID:
242 done:
243 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
244 }
245 
r8712_set_802_11_infrastructure_mode(struct _adapter * padapter,enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)246 void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
247 	enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
248 {
249 	unsigned long irqL;
250 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
251 	struct wlan_network	*cur_network = &pmlmepriv->cur_network;
252 	enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
253 				&(cur_network->network.InfrastructureMode);
254 
255 	if (*pold_state != networktype) {
256 		spin_lock_irqsave(&pmlmepriv->lock, irqL);
257 		if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
258 		    (*pold_state == Ndis802_11IBSS))
259 			r8712_disassoc_cmd(padapter);
260 		if (check_fwstate(pmlmepriv,
261 		    _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true)
262 			r8712_free_assoc_resources(padapter);
263 		if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
264 		    (*pold_state == Ndis802_11Infrastructure) ||
265 		    (*pold_state == Ndis802_11IBSS)) {
266 			/* will clr Linked_state before this function,
267 			 * we must have chked whether issue dis-assoc_cmd or
268 			 * not */
269 			r8712_ind_disconnect(padapter);
270 		}
271 		*pold_state = networktype;
272 		/* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
273 		 * WIFI_ADHOC_MASTER_STATE */
274 		_clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
275 			      WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE |
276 			      WIFI_AP_STATE);
277 		switch (networktype) {
278 		case Ndis802_11IBSS:
279 			set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
280 			break;
281 		case Ndis802_11Infrastructure:
282 			set_fwstate(pmlmepriv, WIFI_STATION_STATE);
283 			break;
284 		case Ndis802_11APMode:
285 			set_fwstate(pmlmepriv, WIFI_AP_STATE);
286 			break;
287 		case Ndis802_11AutoUnknown:
288 		case Ndis802_11InfrastructureMax:
289 			break;
290 		}
291 		spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
292 	}
293 }
294 
r8712_set_802_11_disassociate(struct _adapter * padapter)295 u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
296 {
297 	unsigned long irqL;
298 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
299 
300 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
301 	if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
302 		r8712_disassoc_cmd(padapter);
303 		r8712_ind_disconnect(padapter);
304 		r8712_free_assoc_resources(padapter);
305 	}
306 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
307 	return true;
308 }
309 
r8712_set_802_11_bssid_list_scan(struct _adapter * padapter)310 u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
311 {
312 	struct mlme_priv *pmlmepriv = NULL;
313 	unsigned long irqL;
314 	u8 ret = true;
315 
316 	if (padapter == NULL)
317 		return false;
318 	pmlmepriv = &padapter->mlmepriv;
319 	if (padapter->hw_init_completed == false)
320 		return false;
321 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
322 	if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) ||
323 	    (pmlmepriv->sitesurveyctrl.traffic_busy == true)) {
324 		/* Scan or linking is in progress, do nothing. */
325 		ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
326 	} else {
327 		r8712_free_network_queue(padapter);
328 		ret = r8712_sitesurvey_cmd(padapter, NULL);
329 	}
330 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
331 	return ret;
332 }
333 
r8712_set_802_11_authentication_mode(struct _adapter * padapter,enum NDIS_802_11_AUTHENTICATION_MODE authmode)334 u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
335 				enum NDIS_802_11_AUTHENTICATION_MODE authmode)
336 {
337 	struct security_priv *psecuritypriv = &padapter->securitypriv;
338 	u8 ret;
339 
340 	psecuritypriv->ndisauthtype = authmode;
341 	if (psecuritypriv->ndisauthtype > 3)
342 		psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
343 	if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
344 		ret = true;
345 	else
346 		ret = false;
347 	return ret;
348 }
349 
r8712_set_802_11_add_wep(struct _adapter * padapter,struct NDIS_802_11_WEP * wep)350 u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
351 			    struct NDIS_802_11_WEP *wep)
352 {
353 	u8	bdefaultkey;
354 	u8	btransmitkey;
355 	sint	keyid;
356 	struct security_priv *psecuritypriv = &padapter->securitypriv;
357 
358 	bdefaultkey = (wep->KeyIndex & 0x40000000) > 0 ? false : true;
359 	btransmitkey = (wep->KeyIndex & 0x80000000) > 0 ? true : false;
360 	keyid = wep->KeyIndex & 0x3fffffff;
361 	if (keyid >= WEP_KEYS)
362 		return false;
363 	switch (wep->KeyLength) {
364 	case 5:
365 		psecuritypriv->PrivacyAlgrthm = _WEP40_;
366 		break;
367 	case 13:
368 		psecuritypriv->PrivacyAlgrthm = _WEP104_;
369 		break;
370 	default:
371 		psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
372 		break;
373 	}
374 	memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
375 		wep->KeyLength);
376 	psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
377 	psecuritypriv->PrivacyKeyIndex = keyid;
378 	if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)
379 		return false;
380 	return _SUCCESS;
381 }
382