xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd_indep_power/wl_iw.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Linux Wireless Extensions support
4  *
5  * Copyright (C) 1999-2017, Broadcom Corporation
6  *
7  *      Unless you and Broadcom execute a separate written software license
8  * agreement governing use of this software, this software is licensed to you
9  * under the terms of the GNU General Public License version 2 (the "GPL"),
10  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11  * following added to such license:
12  *
13  *      As a special exception, the copyright holders of this software give you
14  * permission to link this software with independent modules, and to copy and
15  * distribute the resulting executable under terms of your choice, provided that
16  * you also meet, for each linked independent module, the terms and conditions of
17  * the license of that module.  An independent module is a module which is not
18  * derived from this software.  The special exception does not apply to any
19  * modifications of the software.
20  *
21  *      Notwithstanding the above, under no circumstances may you combine this
22  * software in any way with any other Broadcom software provided under a license
23  * other than the GPL, without Broadcom's express prior written consent.
24  *
25  *
26  * <<Broadcom-WL-IPTag/Open:>>
27  *
28  * $Id: wl_iw.c 616333 2016-02-01 05:30:29Z $
29  */
30 
31 #if defined(USE_IW)
32 #define LINUX_PORT
33 
34 #include <typedefs.h>
35 #include <linuxver.h>
36 #include <osl.h>
37 
38 #include <bcmutils.h>
39 #include <bcmendian.h>
40 #include <ethernet.h>
41 
42 #include <linux/if_arp.h>
43 #include <linux/uaccess.h>
44 #include <wlioctl.h>
45 #ifdef WL_NAN
46 #include <wlioctl_utils.h>
47 #endif
48 #include <wl_iw.h>
49 #include <wl_android.h>
50 #ifdef WL_ESCAN
51 #include <wl_escan.h>
52 #endif
53 #include <dhd_config.h>
54 
55 uint iw_msg_level = WL_ERROR_LEVEL;
56 
57 #define WL_ERROR_MSG(x, args...) \
58 	do { \
59 		if (iw_msg_level & WL_ERROR_LEVEL) { \
60 			printk(KERN_ERR "[dhd] WEXT-ERROR) %s : " x, __func__, ## args); \
61 		} \
62 	} while (0)
63 #define WL_TRACE_MSG(x, args...) \
64 	do { \
65 		if (iw_msg_level & WL_TRACE_LEVEL) { \
66 			printk(KERN_INFO "[dhd] WEXT-TRACE) %s : " x, __func__, ## args); \
67 		} \
68 	} while (0)
69 #define WL_SCAN_MSG(x, args...) \
70 	do { \
71 		if (iw_msg_level & WL_SCAN_LEVEL) { \
72 			printk(KERN_INFO "[dhd] WEXT-SCAN) %s : " x, __func__, ## args); \
73 		} \
74 	} while (0)
75 #define WL_WSEC_MSG(x, args...) \
76 	do { \
77 		if (iw_msg_level & WL_WSEC_LEVEL) { \
78 			printk(KERN_INFO "[dhd] WEXT-WSEC) %s : " x, __func__, ## args); \
79 		} \
80 	} while (0)
81 #define WL_ERROR(x) WL_ERROR_MSG x
82 #define WL_TRACE(x) WL_TRACE_MSG x
83 #define WL_SCAN(x) WL_SCAN_MSG x
84 #define WL_WSEC(x) WL_WSEC_MSG x
85 
86 #ifdef BCMWAPI_WPI
87 /* these items should evetually go into wireless.h of the linux system headfile dir */
88 #ifndef IW_ENCODE_ALG_SM4
89 #define IW_ENCODE_ALG_SM4 0x20
90 #endif
91 
92 #ifndef IW_AUTH_WAPI_ENABLED
93 #define IW_AUTH_WAPI_ENABLED 0x20
94 #endif
95 
96 #ifndef IW_AUTH_WAPI_VERSION_1
97 #define IW_AUTH_WAPI_VERSION_1	0x00000008
98 #endif
99 
100 #ifndef IW_AUTH_CIPHER_SMS4
101 #define IW_AUTH_CIPHER_SMS4	0x00000020
102 #endif
103 
104 #ifndef IW_AUTH_KEY_MGMT_WAPI_PSK
105 #define IW_AUTH_KEY_MGMT_WAPI_PSK 4
106 #endif
107 
108 #ifndef IW_AUTH_KEY_MGMT_WAPI_CERT
109 #define IW_AUTH_KEY_MGMT_WAPI_CERT 8
110 #endif
111 #endif /* BCMWAPI_WPI */
112 
113 /* Broadcom extensions to WEXT, linux upstream has obsoleted WEXT */
114 #ifndef IW_AUTH_KEY_MGMT_FT_802_1X
115 #define IW_AUTH_KEY_MGMT_FT_802_1X 0x04
116 #endif
117 
118 #ifndef IW_AUTH_KEY_MGMT_FT_PSK
119 #define IW_AUTH_KEY_MGMT_FT_PSK 0x08
120 #endif
121 
122 #ifndef IW_ENC_CAPA_FW_ROAM_ENABLE
123 #define IW_ENC_CAPA_FW_ROAM_ENABLE	0x00000020
124 #endif
125 
126 
127 /* FC9: wireless.h 2.6.25-14.fc9.i686 is missing these, even though WIRELESS_EXT is set to latest
128  * version 22.
129  */
130 #ifndef IW_ENCODE_ALG_PMK
131 #define IW_ENCODE_ALG_PMK 4
132 #endif
133 #ifndef IW_ENC_CAPA_4WAY_HANDSHAKE
134 #define IW_ENC_CAPA_4WAY_HANDSHAKE 0x00000010
135 #endif
136 /* End FC9. */
137 
138 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
139 #include <linux/rtnetlink.h>
140 #endif
141 
142 extern bool wl_iw_conn_status_str(uint32 event_type, uint32 status,
143 	uint32 reason, char* stringBuf, uint buflen);
144 
145 uint wl_msg_level = WL_ERROR_VAL;
146 
147 #define MAX_WLIW_IOCTL_LEN WLC_IOCTL_MEDLEN
148 
149 /* IOCTL swapping mode for Big Endian host with Little Endian dongle.  Default to off */
150 #define htod32(i) (i)
151 #define htod16(i) (i)
152 #define dtoh32(i) (i)
153 #define dtoh16(i) (i)
154 #define htodchanspec(i) (i)
155 #define dtohchanspec(i) (i)
156 
157 extern struct iw_statistics *dhd_get_wireless_stats(struct net_device *dev);
158 extern int dhd_wait_pend8021x(struct net_device *dev);
159 
160 #if WIRELESS_EXT < 19
161 #define IW_IOCTL_IDX(cmd)	((cmd) - SIOCIWFIRST)
162 #define IW_EVENT_IDX(cmd)	((cmd) - IWEVFIRST)
163 #endif /* WIRELESS_EXT < 19 */
164 
165 
166 #ifndef WL_ESCAN
167 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0))
168 #define DAEMONIZE(a)	do { \
169 		allow_signal(SIGKILL);	\
170 		allow_signal(SIGTERM);	\
171 	} while (0)
172 #elif ((LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)) && \
173 	(LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)))
174 #define DAEMONIZE(a) daemonize(a); \
175 	allow_signal(SIGKILL); \
176 	allow_signal(SIGTERM);
177 #else /* Linux 2.4 (w/o preemption patch) */
178 #define RAISE_RX_SOFTIRQ() \
179 	cpu_raise_softirq(smp_processor_id(), NET_RX_SOFTIRQ)
180 #define DAEMONIZE(a) daemonize(); \
181 	do { if (a) \
182 		strncpy(current->comm, a, MIN(sizeof(current->comm), (strlen(a) + 1))); \
183 	} while (0);
184 #endif /* LINUX_VERSION_CODE  */
185 
186 #define ISCAN_STATE_IDLE   0
187 #define ISCAN_STATE_SCANING 1
188 
189 /* the buf lengh can be WLC_IOCTL_MAXLEN (8K) to reduce iteration */
190 #define WLC_IW_ISCAN_MAXLEN   2048
191 typedef struct iscan_buf {
192 	struct iscan_buf * next;
193 	char   iscan_buf[WLC_IW_ISCAN_MAXLEN];
194 } iscan_buf_t;
195 
196 typedef struct iscan_info {
197 	struct net_device *dev;
198 	timer_list_compat_t timer;
199 	uint32 timer_ms;
200 	uint32 timer_on;
201 	int    iscan_state;
202 	iscan_buf_t * list_hdr;
203 	iscan_buf_t * list_cur;
204 
205 	/* Thread to work on iscan */
206 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
207 	struct task_struct *kthread;
208 #endif
209 	long sysioc_pid;
210 	struct semaphore sysioc_sem;
211 	struct completion sysioc_exited;
212 	char ioctlbuf[WLC_IOCTL_SMLEN];
213 } iscan_info_t;
214 static void wl_iw_timerfunc(ulong data);
215 static void wl_iw_set_event_mask(struct net_device *dev);
216 static int wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, uint16 action);
217 #endif /* !WL_ESCAN */
218 
219 struct pmk_list {
220 	pmkid_list_t pmkids;
221 	pmkid_t foo[MAXPMKID - 1];
222 };
223 
224 typedef struct wl_wext_info {
225 	struct net_device *dev;
226 	dhd_pub_t *dhd;
227 	struct delayed_work pm_enable_work;
228 	struct mutex pm_sync;
229 	struct wl_conn_info conn_info;
230 	struct pmk_list pmk_list;
231 #ifndef WL_ESCAN
232 	struct iscan_info iscan;
233 #endif
234 } wl_wext_info_t;
235 
236 /* priv_link becomes netdev->priv and is the link between netdev and wlif struct */
237 typedef struct priv_link {
238 	wl_iw_t *wliw;
239 } priv_link_t;
240 
241 /* dev to priv_link */
242 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
243 #define WL_DEV_LINK(dev)       (priv_link_t*)(dev->priv)
244 #else
245 #define WL_DEV_LINK(dev)       (priv_link_t*)netdev_priv(dev)
246 #endif
247 
248 /* dev to wl_iw_t */
249 #define IW_DEV_IF(dev)          ((wl_iw_t*)(WL_DEV_LINK(dev))->wliw)
250 
swap_key_from_BE(wl_wsec_key_t * key)251 static void swap_key_from_BE(
252 	        wl_wsec_key_t *key
253 )
254 {
255 	key->index = htod32(key->index);
256 	key->len = htod32(key->len);
257 	key->algo = htod32(key->algo);
258 	key->flags = htod32(key->flags);
259 	key->rxiv.hi = htod32(key->rxiv.hi);
260 	key->rxiv.lo = htod16(key->rxiv.lo);
261 	key->iv_initialized = htod32(key->iv_initialized);
262 }
263 
swap_key_to_BE(wl_wsec_key_t * key)264 static void swap_key_to_BE(
265 	        wl_wsec_key_t *key
266 )
267 {
268 	key->index = dtoh32(key->index);
269 	key->len = dtoh32(key->len);
270 	key->algo = dtoh32(key->algo);
271 	key->flags = dtoh32(key->flags);
272 	key->rxiv.hi = dtoh32(key->rxiv.hi);
273 	key->rxiv.lo = dtoh16(key->rxiv.lo);
274 	key->iv_initialized = dtoh32(key->iv_initialized);
275 }
276 
277 static int
dev_wlc_ioctl(struct net_device * dev,int cmd,void * arg,int len)278 dev_wlc_ioctl(
279 	struct net_device *dev,
280 	int cmd,
281 	void *arg,
282 	int len
283 )
284 {
285 	struct ifreq ifr;
286 	wl_ioctl_t ioc;
287 	mm_segment_t fs;
288 	int ret;
289 
290 	memset(&ioc, 0, sizeof(ioc));
291 #ifdef CONFIG_COMPAT
292 	ioc.cmd = cmd | WLC_SPEC_FLAG;
293 #else
294 	ioc.cmd = cmd;
295 #endif
296 	ioc.buf = arg;
297 	ioc.len = len;
298 
299 	strncpy(ifr.ifr_name, dev->name, sizeof(ifr.ifr_name));
300 	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
301 	ifr.ifr_data = (caddr_t) &ioc;
302 
303 	fs = get_fs();
304 	set_fs(KERNEL_DS);
305 #if defined(WL_USE_NETDEV_OPS)
306 	ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, SIOCDEVPRIVATE);
307 #else
308 	ret = dev->do_ioctl(dev, &ifr, SIOCDEVPRIVATE);
309 #endif
310 	set_fs(fs);
311 
312 	return ret;
313 }
314 
315 /*
316 set named driver variable to int value and return error indication
317 calling example: dev_wlc_intvar_set(dev, "arate", rate)
318 */
319 
320 static int
dev_wlc_intvar_set(struct net_device * dev,char * name,int val)321 dev_wlc_intvar_set(
322 	struct net_device *dev,
323 	char *name,
324 	int val)
325 {
326 	char buf[WLC_IOCTL_SMLEN];
327 	uint len;
328 
329 	val = htod32(val);
330 	len = bcm_mkiovar(name, (char *)(&val), sizeof(val), buf, sizeof(buf));
331 	ASSERT(len);
332 
333 	return (dev_wlc_ioctl(dev, WLC_SET_VAR, buf, len));
334 }
335 
336 #ifndef WL_ESCAN
337 static int
dev_iw_iovar_setbuf(struct net_device * dev,char * iovar,void * param,int paramlen,void * bufptr,int buflen)338 dev_iw_iovar_setbuf(
339 	struct net_device *dev,
340 	char *iovar,
341 	void *param,
342 	int paramlen,
343 	void *bufptr,
344 	int buflen)
345 {
346 	int iolen;
347 
348 	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
349 	ASSERT(iolen);
350 	BCM_REFERENCE(iolen);
351 
352 	return (dev_wlc_ioctl(dev, WLC_SET_VAR, bufptr, iolen));
353 }
354 
355 static int
dev_iw_iovar_getbuf(struct net_device * dev,char * iovar,void * param,int paramlen,void * bufptr,int buflen)356 dev_iw_iovar_getbuf(
357 	struct net_device *dev,
358 	char *iovar,
359 	void *param,
360 	int paramlen,
361 	void *bufptr,
362 	int buflen)
363 {
364 	int iolen;
365 
366 	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
367 	ASSERT(iolen);
368 	BCM_REFERENCE(iolen);
369 
370 	return (dev_wlc_ioctl(dev, WLC_GET_VAR, bufptr, buflen));
371 }
372 #endif
373 
374 #if WIRELESS_EXT > 17
375 static int
dev_wlc_bufvar_set(struct net_device * dev,char * name,char * buf,int len)376 dev_wlc_bufvar_set(
377 	struct net_device *dev,
378 	char *name,
379 	char *buf, int len)
380 {
381 	char *ioctlbuf;
382 	uint buflen;
383 	int error;
384 
385 	ioctlbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
386 	if (!ioctlbuf)
387 		return -ENOMEM;
388 
389 	buflen = bcm_mkiovar(name, buf, len, ioctlbuf, MAX_WLIW_IOCTL_LEN);
390 	ASSERT(buflen);
391 	error = dev_wlc_ioctl(dev, WLC_SET_VAR, ioctlbuf, buflen);
392 
393 	kfree(ioctlbuf);
394 	return error;
395 }
396 #endif /* WIRELESS_EXT > 17 */
397 
398 /*
399 get named driver variable to int value and return error indication
400 calling example: dev_wlc_bufvar_get(dev, "arate", &rate)
401 */
402 
403 static int
dev_wlc_bufvar_get(struct net_device * dev,char * name,char * buf,int buflen)404 dev_wlc_bufvar_get(
405 	struct net_device *dev,
406 	char *name,
407 	char *buf, int buflen)
408 {
409 	char *ioctlbuf;
410 	int error;
411 
412 	uint len;
413 
414 	ioctlbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
415 	if (!ioctlbuf)
416 		return -ENOMEM;
417 	len = bcm_mkiovar(name, NULL, 0, ioctlbuf, MAX_WLIW_IOCTL_LEN);
418 	ASSERT(len);
419 	BCM_REFERENCE(len);
420 	error = dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)ioctlbuf, MAX_WLIW_IOCTL_LEN);
421 	if (!error)
422 		bcopy(ioctlbuf, buf, buflen);
423 
424 	kfree(ioctlbuf);
425 	return (error);
426 }
427 
428 /*
429 get named driver variable to int value and return error indication
430 calling example: dev_wlc_intvar_get(dev, "arate", &rate)
431 */
432 
433 static int
dev_wlc_intvar_get(struct net_device * dev,char * name,int * retval)434 dev_wlc_intvar_get(
435 	struct net_device *dev,
436 	char *name,
437 	int *retval)
438 {
439 	union {
440 		char buf[WLC_IOCTL_SMLEN];
441 		int val;
442 	} var;
443 	int error;
444 
445 	uint len;
446 	uint data_null;
447 
448 	len = bcm_mkiovar(name, (char *)(&data_null), 0, (char *)(&var), sizeof(var.buf));
449 	ASSERT(len);
450 	error = dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)&var, len);
451 
452 	*retval = dtoh32(var.val);
453 
454 	return (error);
455 }
456 
457 /* Maintain backward compatibility */
458 #if WIRELESS_EXT < 13
459 struct iw_request_info
460 {
461 	__u16		cmd;		/* Wireless Extension command */
462 	__u16		flags;		/* More to come ;-) */
463 };
464 
465 typedef int (*iw_handler)(struct net_device *dev, struct iw_request_info *info,
466 	void *wrqu, char *extra);
467 #endif /* WIRELESS_EXT < 13 */
468 
469 #if WIRELESS_EXT > 12
470 static int
wl_iw_set_leddc(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)471 wl_iw_set_leddc(
472 	struct net_device *dev,
473 	struct iw_request_info *info,
474 	union iwreq_data *wrqu,
475 	char *extra
476 )
477 {
478 	int dc = *(int *)extra;
479 	int error;
480 
481 	error = dev_wlc_intvar_set(dev, "leddc", dc);
482 	return error;
483 }
484 
485 static int
wl_iw_set_vlanmode(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)486 wl_iw_set_vlanmode(
487 	struct net_device *dev,
488 	struct iw_request_info *info,
489 	union iwreq_data *wrqu,
490 	char *extra
491 )
492 {
493 	int mode = *(int *)extra;
494 	int error;
495 
496 	mode = htod32(mode);
497 	error = dev_wlc_intvar_set(dev, "vlan_mode", mode);
498 	return error;
499 }
500 
501 static int
wl_iw_set_pm(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)502 wl_iw_set_pm(
503 	struct net_device *dev,
504 	struct iw_request_info *info,
505 	union iwreq_data *wrqu,
506 	char *extra
507 )
508 {
509 	int pm = *(int *)extra;
510 	int error;
511 
512 	pm = htod32(pm);
513 	error = dev_wlc_ioctl(dev, WLC_SET_PM, &pm, sizeof(pm));
514 	return error;
515 }
516 #endif /* WIRELESS_EXT > 12 */
517 
518 int
wl_iw_send_priv_event(struct net_device * dev,char * flag)519 wl_iw_send_priv_event(
520 	struct net_device *dev,
521 	char *flag
522 )
523 {
524 	union iwreq_data wrqu;
525 	char extra[IW_CUSTOM_MAX + 1];
526 	int cmd;
527 
528 	cmd = IWEVCUSTOM;
529 	memset(&wrqu, 0, sizeof(wrqu));
530 	if (strlen(flag) > sizeof(extra))
531 		return -1;
532 
533 	strncpy(extra, flag, sizeof(extra));
534 	extra[sizeof(extra) - 1] = '\0';
535 	wrqu.data.length = strlen(extra);
536 	wireless_send_event(dev, cmd, &wrqu, extra);
537 	WL_TRACE(("Send IWEVCUSTOM Event as %s\n", extra));
538 
539 	return 0;
540 }
541 
542 static int
wl_iw_config_commit(struct net_device * dev,struct iw_request_info * info,void * zwrq,char * extra)543 wl_iw_config_commit(
544 	struct net_device *dev,
545 	struct iw_request_info *info,
546 	void *zwrq,
547 	char *extra
548 )
549 {
550 	wlc_ssid_t ssid;
551 	int error;
552 	struct sockaddr bssid;
553 
554 	WL_TRACE(("%s: SIOCSIWCOMMIT\n", dev->name));
555 
556 	if ((error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid))))
557 		return error;
558 
559 	ssid.SSID_len = dtoh32(ssid.SSID_len);
560 
561 	if (!ssid.SSID_len)
562 		return 0;
563 
564 	bzero(&bssid, sizeof(struct sockaddr));
565 	if ((error = dev_wlc_ioctl(dev, WLC_REASSOC, &bssid, ETHER_ADDR_LEN))) {
566 		WL_ERROR(("WLC_REASSOC failed (%d)\n", error));
567 		return error;
568 	}
569 
570 	return 0;
571 }
572 
573 static int
wl_iw_get_name(struct net_device * dev,struct iw_request_info * info,union iwreq_data * cwrq,char * extra)574 wl_iw_get_name(
575 	struct net_device *dev,
576 	struct iw_request_info *info,
577 	union iwreq_data *cwrq,
578 	char *extra
579 )
580 {
581 	int phytype, err;
582 	uint band[3];
583 	char cap[5];
584 
585 	WL_TRACE(("%s: SIOCGIWNAME\n", dev->name));
586 
587 	cap[0] = 0;
588 	if ((err = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &phytype, sizeof(phytype))) < 0)
589 		goto done;
590 	if ((err = dev_wlc_ioctl(dev, WLC_GET_BANDLIST, band, sizeof(band))) < 0)
591 		goto done;
592 
593 	band[0] = dtoh32(band[0]);
594 	switch (phytype) {
595 		case WLC_PHY_TYPE_A:
596 			strncpy(cap, "a", sizeof(cap));
597 			break;
598 		case WLC_PHY_TYPE_B:
599 			strncpy(cap, "b", sizeof(cap));
600 			break;
601 		case WLC_PHY_TYPE_G:
602 			if (band[0] >= 2)
603 				strncpy(cap, "abg", sizeof(cap));
604 			else
605 				strncpy(cap, "bg", sizeof(cap));
606 			break;
607 		case WLC_PHY_TYPE_N:
608 			if (band[0] >= 2)
609 				strncpy(cap, "abgn", sizeof(cap));
610 			else
611 				strncpy(cap, "bgn", sizeof(cap));
612 			break;
613 	}
614 done:
615 	(void)snprintf(cwrq->name, IFNAMSIZ, "IEEE 802.11%s", cap);
616 
617 	return 0;
618 }
619 
620 #define DHD_CHECK(dhd, dev) \
621  	if (!dhd) { \
622 		WL_ERROR (("[dhd-%s] %s: dhd is NULL\n", dev->name, __FUNCTION__)); \
623 		return -ENODEV; \
624 	} \
625 
626 static int
wl_iw_set_freq(struct net_device * dev,struct iw_request_info * info,struct iw_freq * fwrq,char * extra)627 wl_iw_set_freq(
628 	struct net_device *dev,
629 	struct iw_request_info *info,
630 	struct iw_freq *fwrq,
631 	char *extra
632 )
633 {
634 	int error, chan;
635 	uint sf = 0;
636 	struct dhd_pub *dhd = dhd_get_pub(dev);
637 	wl_wext_info_t *wext_info = NULL;
638 
639 	WL_TRACE(("%s: SIOCSIWFREQ\n", dev->name));
640 	DHD_CHECK(dhd, dev);
641 	wext_info = dhd->wext_info;
642 
643 	/* Setting by channel number */
644 	if (fwrq->e == 0 && fwrq->m < MAXCHANNEL) {
645 		chan = fwrq->m;
646 	}
647 
648 	/* Setting by frequency */
649 	else {
650 		/* Convert to MHz as best we can */
651 		if (fwrq->e >= 6) {
652 			fwrq->e -= 6;
653 			while (fwrq->e--)
654 				fwrq->m *= 10;
655 		} else if (fwrq->e < 6) {
656 			while (fwrq->e++ < 6)
657 				fwrq->m /= 10;
658 		}
659 	/* handle 4.9GHz frequencies as Japan 4 GHz based channelization */
660 		if (fwrq->m > 4000 && fwrq->m < 5000) {
661 			sf = WF_CHAN_FACTOR_4_G; /* start factor for 4 GHz */
662 		}
663 		chan = wf_mhz2channel(fwrq->m, sf);
664 	}
665 	if (wext_info)
666 		wext_info->conn_info.channel = chan;
667 	WL_MSG(dev->name, "chan=%d\n", chan);
668 	chan = htod32(chan);
669 	if ((error = dev_wlc_ioctl(dev, WLC_SET_CHANNEL, &chan, sizeof(chan)))) {
670 		WL_ERROR(("WLC_SET_CHANNEL failed (%d).\n", error));
671 		return error;
672 	}
673 
674 	/* -EINPROGRESS: Call commit handler */
675 	return -EINPROGRESS;
676 }
677 
678 static int
wl_iw_get_freq(struct net_device * dev,struct iw_request_info * info,struct iw_freq * fwrq,char * extra)679 wl_iw_get_freq(
680 	struct net_device *dev,
681 	struct iw_request_info *info,
682 	struct iw_freq *fwrq,
683 	char *extra
684 )
685 {
686 	int error;
687 	u32 chanspec = 0;
688 	int ctl_chan;
689 
690 	WL_TRACE(("%s: SIOCGIWFREQ\n", dev->name));
691 
692 	if ((error = dev_wlc_intvar_get(dev, "chanspec", &chanspec)))
693 		return error;
694 	ctl_chan = wf_chspec_ctlchan(chanspec);
695 
696 	/* Return radio channel in channel form */
697 	fwrq->m = ctl_chan;
698 	fwrq->e = dtoh32(0);
699 	return 0;
700 }
701 
702 static int
wl_iw_set_mode(struct net_device * dev,struct iw_request_info * info,__u32 * uwrq,char * extra)703 wl_iw_set_mode(
704 	struct net_device *dev,
705 	struct iw_request_info *info,
706 	__u32 *uwrq,
707 	char *extra
708 )
709 {
710 	int infra = 0, ap = 0, error = 0;
711 	struct dhd_pub *dhd = dhd_get_pub(dev);
712 	wl_wext_info_t *wext_info = NULL;
713 
714 	WL_TRACE(("%s: SIOCSIWMODE\n", dev->name));
715 	DHD_CHECK(dhd, dev);
716 	wext_info = dhd->wext_info;
717 	if (wext_info) {
718 		memset(&wext_info->conn_info.ssid, 0, sizeof(wlc_ssid_t));
719 		memset(&wext_info->conn_info.bssid, 0, sizeof(struct ether_addr));
720 		wext_info->conn_info.channel = 0;
721 	}
722 
723 	switch (*uwrq) {
724 	case IW_MODE_MASTER:
725 		infra = ap = 1;
726 		break;
727 	case IW_MODE_ADHOC:
728 	case IW_MODE_AUTO:
729 		break;
730 	case IW_MODE_INFRA:
731 		infra = 1;
732 		break;
733 	default:
734 		return -EINVAL;
735 	}
736 	infra = htod32(infra);
737 	ap = htod32(ap);
738 
739 	if ((error = dev_wlc_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra))) ||
740 	    (error = dev_wlc_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap))))
741 		return error;
742 
743 	/* -EINPROGRESS: Call commit handler */
744 	return -EINPROGRESS;
745 }
746 
747 static int
wl_iw_get_mode(struct net_device * dev,struct iw_request_info * info,__u32 * uwrq,char * extra)748 wl_iw_get_mode(
749 	struct net_device *dev,
750 	struct iw_request_info *info,
751 	__u32 *uwrq,
752 	char *extra
753 )
754 {
755 	int error, infra = 0, ap = 0;
756 
757 	WL_TRACE(("%s: SIOCGIWMODE\n", dev->name));
758 
759 	if ((error = dev_wlc_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra))) ||
760 	    (error = dev_wlc_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap))))
761 		return error;
762 
763 	infra = dtoh32(infra);
764 	ap = dtoh32(ap);
765 	*uwrq = infra ? ap ? IW_MODE_MASTER : IW_MODE_INFRA : IW_MODE_ADHOC;
766 
767 	return 0;
768 }
769 
770 static int
wl_iw_get_range(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)771 wl_iw_get_range(
772 	struct net_device *dev,
773 	struct iw_request_info *info,
774 	struct iw_point *dwrq,
775 	char *extra
776 )
777 {
778 	struct iw_range *range = (struct iw_range *) extra;
779 	static int channels[MAXCHANNEL+1];
780 	wl_uint32_list_t *list = (wl_uint32_list_t *) channels;
781 	wl_rateset_t rateset;
782 	int error, i, k;
783 	uint sf, ch;
784 
785 	int phytype;
786 	int bw_cap = 0, sgi_tx = 0, nmode = 0;
787 	channel_info_t ci;
788 	uint8 nrate_list2copy = 0;
789 	uint16 nrate_list[4][8] = { {13, 26, 39, 52, 78, 104, 117, 130},
790 		{14, 29, 43, 58, 87, 116, 130, 144},
791 		{27, 54, 81, 108, 162, 216, 243, 270},
792 		{30, 60, 90, 120, 180, 240, 270, 300}};
793 	int fbt_cap = 0;
794 
795 	WL_TRACE(("%s: SIOCGIWRANGE\n", dev->name));
796 
797 	if (!extra)
798 		return -EINVAL;
799 
800 	dwrq->length = sizeof(struct iw_range);
801 	memset(range, 0, sizeof(*range));
802 
803 	/* We don't use nwids */
804 	range->min_nwid = range->max_nwid = 0;
805 
806 	/* Set available channels/frequencies */
807 	list->count = htod32(MAXCHANNEL);
808 	if ((error = dev_wlc_ioctl(dev, WLC_GET_VALID_CHANNELS, channels, sizeof(channels))))
809 		return error;
810 	for (i = 0; i < dtoh32(list->count) && i < IW_MAX_FREQUENCIES; i++) {
811 		range->freq[i].i = dtoh32(list->element[i]);
812 
813 		ch = dtoh32(list->element[i]);
814 		if (ch <= CH_MAX_2G_CHANNEL)
815 			sf = WF_CHAN_FACTOR_2_4_G;
816 		else
817 			sf = WF_CHAN_FACTOR_5_G;
818 
819 		range->freq[i].m = wf_channel2mhz(ch, sf);
820 		range->freq[i].e = 6;
821 	}
822 	range->num_frequency = range->num_channels = i;
823 
824 	/* Link quality (use NDIS cutoffs) */
825 	range->max_qual.qual = 5;
826 	/* Signal level (use RSSI) */
827 	range->max_qual.level = 0x100 - 200;	/* -200 dBm */
828 	/* Noise level (use noise) */
829 	range->max_qual.noise = 0x100 - 200;	/* -200 dBm */
830 	/* Signal level threshold range (?) */
831 	range->sensitivity = 65535;
832 
833 #if WIRELESS_EXT > 11
834 	/* Link quality (use NDIS cutoffs) */
835 	range->avg_qual.qual = 3;
836 	/* Signal level (use RSSI) */
837 	range->avg_qual.level = 0x100 + WL_IW_RSSI_GOOD;
838 	/* Noise level (use noise) */
839 	range->avg_qual.noise = 0x100 - 75;	/* -75 dBm */
840 #endif /* WIRELESS_EXT > 11 */
841 
842 	/* Set available bitrates */
843 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset, sizeof(rateset))))
844 		return error;
845 	rateset.count = dtoh32(rateset.count);
846 	range->num_bitrates = rateset.count;
847 	for (i = 0; i < rateset.count && i < IW_MAX_BITRATES; i++)
848 		range->bitrate[i] = (rateset.rates[i] & 0x7f) * 500000; /* convert to bps */
849 	if ((error = dev_wlc_intvar_get(dev, "nmode", &nmode)))
850 		return error;
851 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &phytype, sizeof(phytype))))
852 		return error;
853 	if (nmode == 1 && (((phytype == WLC_PHY_TYPE_LCN) ||
854 	                    (phytype == WLC_PHY_TYPE_LCN40)))) {
855 		if ((error = dev_wlc_intvar_get(dev, "mimo_bw_cap", &bw_cap)))
856 			return error;
857 		if ((error = dev_wlc_intvar_get(dev, "sgi_tx", &sgi_tx)))
858 			return error;
859 		if ((error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(channel_info_t))))
860 			return error;
861 		ci.hw_channel = dtoh32(ci.hw_channel);
862 
863 		if (bw_cap == 0 ||
864 			(bw_cap == 2 && ci.hw_channel <= 14)) {
865 			if (sgi_tx == 0)
866 				nrate_list2copy = 0;
867 			else
868 				nrate_list2copy = 1;
869 		}
870 		if (bw_cap == 1 ||
871 			(bw_cap == 2 && ci.hw_channel >= 36)) {
872 			if (sgi_tx == 0)
873 				nrate_list2copy = 2;
874 			else
875 				nrate_list2copy = 3;
876 		}
877 		range->num_bitrates += 8;
878 		ASSERT(range->num_bitrates < IW_MAX_BITRATES);
879 		for (k = 0; i < range->num_bitrates; k++, i++) {
880 			/* convert to bps */
881 			range->bitrate[i] = (nrate_list[nrate_list2copy][k]) * 500000;
882 		}
883 	}
884 
885 	/* Set an indication of the max TCP throughput
886 	 * in bit/s that we can expect using this interface.
887 	 * May be use for QoS stuff... Jean II
888 	 */
889 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &i, sizeof(i))))
890 		return error;
891 	i = dtoh32(i);
892 	if (i == WLC_PHY_TYPE_A)
893 		range->throughput = 24000000;	/* 24 Mbits/s */
894 	else
895 		range->throughput = 1500000;	/* 1.5 Mbits/s */
896 
897 	/* RTS and fragmentation thresholds */
898 	range->min_rts = 0;
899 	range->max_rts = 2347;
900 	range->min_frag = 256;
901 	range->max_frag = 2346;
902 
903 	range->max_encoding_tokens = DOT11_MAX_DEFAULT_KEYS;
904 	range->num_encoding_sizes = 4;
905 	range->encoding_size[0] = WEP1_KEY_SIZE;
906 	range->encoding_size[1] = WEP128_KEY_SIZE;
907 #if WIRELESS_EXT > 17
908 	range->encoding_size[2] = TKIP_KEY_SIZE;
909 #else
910 	range->encoding_size[2] = 0;
911 #endif
912 	range->encoding_size[3] = AES_KEY_SIZE;
913 
914 	/* Do not support power micro-management */
915 	range->min_pmp = 0;
916 	range->max_pmp = 0;
917 	range->min_pmt = 0;
918 	range->max_pmt = 0;
919 	range->pmp_flags = 0;
920 	range->pm_capa = 0;
921 
922 	/* Transmit Power - values are in mW */
923 	range->num_txpower = 2;
924 	range->txpower[0] = 1;
925 	range->txpower[1] = 255;
926 	range->txpower_capa = IW_TXPOW_MWATT;
927 
928 #if WIRELESS_EXT > 10
929 	range->we_version_compiled = WIRELESS_EXT;
930 	range->we_version_source = 19;
931 
932 	/* Only support retry limits */
933 	range->retry_capa = IW_RETRY_LIMIT;
934 	range->retry_flags = IW_RETRY_LIMIT;
935 	range->r_time_flags = 0;
936 	/* SRL and LRL limits */
937 	range->min_retry = 1;
938 	range->max_retry = 255;
939 	/* Retry lifetime limits unsupported */
940 	range->min_r_time = 0;
941 	range->max_r_time = 0;
942 #endif /* WIRELESS_EXT > 10 */
943 
944 #if WIRELESS_EXT > 17
945 	range->enc_capa = IW_ENC_CAPA_WPA;
946 	range->enc_capa |= IW_ENC_CAPA_CIPHER_TKIP;
947 	range->enc_capa |= IW_ENC_CAPA_CIPHER_CCMP;
948 	range->enc_capa |= IW_ENC_CAPA_WPA2;
949 
950 	/* Determine driver FBT capability. */
951 	if (dev_wlc_intvar_get(dev, "fbt_cap", &fbt_cap) == 0) {
952 		if (fbt_cap == WLC_FBT_CAP_DRV_4WAY_AND_REASSOC) {
953 			/* Tell the host (e.g. wpa_supplicant) to let driver do the handshake */
954 			range->enc_capa |= IW_ENC_CAPA_4WAY_HANDSHAKE;
955 		}
956 	}
957 
958 #ifdef BCMFW_ROAM_ENABLE_WEXT
959 	/* Advertise firmware roam capability to the external supplicant */
960 	range->enc_capa |= IW_ENC_CAPA_FW_ROAM_ENABLE;
961 #endif /* BCMFW_ROAM_ENABLE_WEXT */
962 
963 	/* Event capability (kernel) */
964 	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
965 	/* Event capability (driver) */
966 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
967 	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
968 	IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
969 	IW_EVENT_CAPA_SET(range->event_capa, IWEVMICHAELMICFAILURE);
970 	IW_EVENT_CAPA_SET(range->event_capa, IWEVASSOCREQIE);
971 	IW_EVENT_CAPA_SET(range->event_capa, IWEVASSOCRESPIE);
972 	IW_EVENT_CAPA_SET(range->event_capa, IWEVPMKIDCAND);
973 
974 #if WIRELESS_EXT >= 22 && defined(IW_SCAN_CAPA_ESSID)
975 	/* FC7 wireless.h defines EXT 22 but doesn't define scan_capa bits */
976 	range->scan_capa = IW_SCAN_CAPA_ESSID;
977 #endif
978 #endif /* WIRELESS_EXT > 17 */
979 
980 	return 0;
981 }
982 
983 #ifndef WL_ESCAN
984 static int
rssi_to_qual(int rssi)985 rssi_to_qual(int rssi)
986 {
987 	if (rssi <= WL_IW_RSSI_NO_SIGNAL)
988 		return 0;
989 	else if (rssi <= WL_IW_RSSI_VERY_LOW)
990 		return 1;
991 	else if (rssi <= WL_IW_RSSI_LOW)
992 		return 2;
993 	else if (rssi <= WL_IW_RSSI_GOOD)
994 		return 3;
995 	else if (rssi <= WL_IW_RSSI_VERY_GOOD)
996 		return 4;
997 	else
998 		return 5;
999 }
1000 #endif /* WL_ESCAN */
1001 
1002 static int
wl_iw_set_spy(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1003 wl_iw_set_spy(
1004 	struct net_device *dev,
1005 	struct iw_request_info *info,
1006 	struct iw_point *dwrq,
1007 	char *extra
1008 )
1009 {
1010 	wl_iw_t *iw = IW_DEV_IF(dev);
1011 	struct sockaddr *addr = (struct sockaddr *) extra;
1012 	int i;
1013 
1014 	WL_TRACE(("%s: SIOCSIWSPY\n", dev->name));
1015 
1016 	if (!extra)
1017 		return -EINVAL;
1018 
1019 	iw->spy_num = MIN(ARRAYSIZE(iw->spy_addr), dwrq->length);
1020 	for (i = 0; i < iw->spy_num; i++)
1021 		memcpy(&iw->spy_addr[i], addr[i].sa_data, ETHER_ADDR_LEN);
1022 	memset(iw->spy_qual, 0, sizeof(iw->spy_qual));
1023 
1024 	return 0;
1025 }
1026 
1027 static int
wl_iw_get_spy(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1028 wl_iw_get_spy(
1029 	struct net_device *dev,
1030 	struct iw_request_info *info,
1031 	struct iw_point *dwrq,
1032 	char *extra
1033 )
1034 {
1035 	wl_iw_t *iw = IW_DEV_IF(dev);
1036 	struct sockaddr *addr = (struct sockaddr *) extra;
1037 	struct iw_quality *qual = (struct iw_quality *) &addr[iw->spy_num];
1038 	int i;
1039 
1040 	WL_TRACE(("%s: SIOCGIWSPY\n", dev->name));
1041 
1042 	if (!extra)
1043 		return -EINVAL;
1044 
1045 	dwrq->length = iw->spy_num;
1046 	for (i = 0; i < iw->spy_num; i++) {
1047 		memcpy(addr[i].sa_data, &iw->spy_addr[i], ETHER_ADDR_LEN);
1048 		addr[i].sa_family = AF_UNIX;
1049 		memcpy(&qual[i], &iw->spy_qual[i], sizeof(struct iw_quality));
1050 		iw->spy_qual[i].updated = 0;
1051 	}
1052 
1053 	return 0;
1054 }
1055 
1056 static int
wl_iw_set_wap(struct net_device * dev,struct iw_request_info * info,struct sockaddr * awrq,char * extra)1057 wl_iw_set_wap(
1058 	struct net_device *dev,
1059 	struct iw_request_info *info,
1060 	struct sockaddr *awrq,
1061 	char *extra
1062 )
1063 {
1064 	int error = -EINVAL;
1065 	struct dhd_pub *dhd = dhd_get_pub(dev);
1066 	wl_wext_info_t *wext_info = NULL;
1067 
1068 	WL_TRACE(("%s: SIOCSIWAP\n", dev->name));
1069 	DHD_CHECK(dhd, dev);
1070  	wext_info = dhd->wext_info;
1071 	if (awrq->sa_family != ARPHRD_ETHER) {
1072 		WL_ERROR(("Invalid Header...sa_family\n"));
1073 		return -EINVAL;
1074 	}
1075 
1076 	/* Ignore "auto" or "off" */
1077 	if (ETHER_ISBCAST(awrq->sa_data) || ETHER_ISNULLADDR(awrq->sa_data)) {
1078 		scb_val_t scbval;
1079 		bzero(&scbval, sizeof(scb_val_t));
1080 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1081 		if ((error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t)))) {
1082 			WL_ERROR(("WLC_DISASSOC failed (%d).\n", error));
1083 		}
1084 		wl_ext_in4way_sync_wext(dev,
1085 			STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
1086 			WL_EXT_STATUS_DISCONNECTING, NULL);
1087 		return 0;
1088 	}
1089 	/* WL_ASSOC(("Assoc to %s\n", bcm_ether_ntoa((struct ether_addr *)&(awrq->sa_data),
1090 	 * eabuf)));
1091 	 */
1092 	/* Reassociate to the specified AP */
1093 	if (wext_info)
1094 		memcpy(&wext_info->conn_info.bssid, awrq->sa_data, ETHER_ADDR_LEN);
1095 	if (wext_info && wext_info->conn_info.ssid.SSID_len) {
1096 		if ((error = wl_ext_connect(dev, &wext_info->conn_info)))
1097 			return error;
1098 	} else {
1099 		if ((error = dev_wlc_ioctl(dev, WLC_REASSOC, awrq->sa_data, ETHER_ADDR_LEN))) {
1100 			WL_ERROR(("WLC_REASSOC failed (%d).\n", error));
1101 			return error;
1102 		}
1103 		WL_MSG(dev->name, "join BSSID="MACSTR"\n", MAC2STR((u8 *)awrq->sa_data));
1104 	}
1105 	wl_ext_in4way_sync_wext(dev, STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY,
1106 		WL_EXT_STATUS_CONNECTING, NULL);
1107 
1108 	return 0;
1109 }
1110 
1111 static int
wl_iw_get_wap(struct net_device * dev,struct iw_request_info * info,struct sockaddr * awrq,char * extra)1112 wl_iw_get_wap(
1113 	struct net_device *dev,
1114 	struct iw_request_info *info,
1115 	struct sockaddr *awrq,
1116 	char *extra
1117 )
1118 {
1119 	WL_TRACE(("%s: SIOCGIWAP\n", dev->name));
1120 
1121 	awrq->sa_family = ARPHRD_ETHER;
1122 	memset(awrq->sa_data, 0, ETHER_ADDR_LEN);
1123 
1124 	/* Ignore error (may be down or disassociated) */
1125 	(void) dev_wlc_ioctl(dev, WLC_GET_BSSID, awrq->sa_data, ETHER_ADDR_LEN);
1126 
1127 	return 0;
1128 }
1129 
1130 #if WIRELESS_EXT > 17
1131 static int
wl_iw_mlme(struct net_device * dev,struct iw_request_info * info,struct sockaddr * awrq,char * extra)1132 wl_iw_mlme(
1133 	struct net_device *dev,
1134 	struct iw_request_info *info,
1135 	struct sockaddr *awrq,
1136 	char *extra
1137 )
1138 {
1139 	struct iw_mlme *mlme;
1140 	scb_val_t scbval;
1141 	int error  = -EINVAL;
1142 
1143 	WL_TRACE(("%s: SIOCSIWMLME\n", dev->name));
1144 
1145 	mlme = (struct iw_mlme *)extra;
1146 	if (mlme == NULL) {
1147 		WL_ERROR(("Invalid ioctl data.\n"));
1148 		return error;
1149 	}
1150 
1151 	scbval.val = mlme->reason_code;
1152 	bcopy(&mlme->addr.sa_data, &scbval.ea, ETHER_ADDR_LEN);
1153 
1154 	if (mlme->cmd == IW_MLME_DISASSOC) {
1155 		scbval.val = htod32(scbval.val);
1156 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1157 		error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t));
1158 	}
1159 	else if (mlme->cmd == IW_MLME_DEAUTH) {
1160 		scbval.val = htod32(scbval.val);
1161 		WL_MSG(dev->name, "WLC_SCB_DEAUTHENTICATE_FOR_REASON\n");
1162 		error = dev_wlc_ioctl(dev, WLC_SCB_DEAUTHENTICATE_FOR_REASON, &scbval,
1163 			sizeof(scb_val_t));
1164 	}
1165 	else {
1166 		WL_ERROR(("Invalid ioctl data.\n"));
1167 		return error;
1168 	}
1169 	wl_ext_in4way_sync_wext(dev,
1170 			STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
1171 			WL_EXT_STATUS_DISCONNECTING, NULL);
1172 
1173 	return error;
1174 }
1175 #endif /* WIRELESS_EXT > 17 */
1176 
1177 #ifndef WL_ESCAN
1178 static int
wl_iw_get_aplist(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1179 wl_iw_get_aplist(
1180 	struct net_device *dev,
1181 	struct iw_request_info *info,
1182 	struct iw_point *dwrq,
1183 	char *extra
1184 )
1185 {
1186 	wl_scan_results_t *list;
1187 	struct sockaddr *addr = (struct sockaddr *) extra;
1188 	struct iw_quality qual[IW_MAX_AP];
1189 	wl_bss_info_t *bi = NULL;
1190 	int error, i;
1191 	uint buflen = dwrq->length;
1192 	int16 rssi;
1193 
1194 	WL_TRACE(("%s: SIOCGIWAPLIST\n", dev->name));
1195 
1196 	if (!extra)
1197 		return -EINVAL;
1198 
1199 	/* Get scan results (too large to put on the stack) */
1200 	list = kmalloc(buflen, GFP_KERNEL);
1201 	if (!list)
1202 		return -ENOMEM;
1203 	memset(list, 0, buflen);
1204 	list->buflen = htod32(buflen);
1205 	if ((error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, buflen))) {
1206 		WL_ERROR(("%d: Scan results error %d\n", __LINE__, error));
1207 		kfree(list);
1208 		return error;
1209 	}
1210 	list->buflen = dtoh32(list->buflen);
1211 	list->version = dtoh32(list->version);
1212 	list->count = dtoh32(list->count);
1213 	ASSERT(list->version == WL_BSS_INFO_VERSION);
1214 
1215 	for (i = 0, dwrq->length = 0; i < list->count && dwrq->length < IW_MAX_AP; i++) {
1216 		bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1217 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1218 			buflen));
1219 
1220 		/* Infrastructure only */
1221 		if (!(dtoh16(bi->capability) & DOT11_CAP_ESS))
1222 			continue;
1223 
1224 		/* BSSID */
1225 		memcpy(addr[dwrq->length].sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1226 		addr[dwrq->length].sa_family = ARPHRD_ETHER;
1227 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1228 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1229 		qual[dwrq->length].qual = rssi_to_qual(rssi);
1230 		qual[dwrq->length].level = 0x100 + rssi;
1231 		qual[dwrq->length].noise = 0x100 + bi->phy_noise;
1232 
1233 		/* Updated qual, level, and noise */
1234 #if WIRELESS_EXT > 18
1235 		qual[dwrq->length].updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1236 #else
1237 		qual[dwrq->length].updated = 7;
1238 #endif /* WIRELESS_EXT > 18 */
1239 
1240 		dwrq->length++;
1241 	}
1242 
1243 	kfree(list);
1244 
1245 	if (dwrq->length) {
1246 		memcpy(&addr[dwrq->length], qual, sizeof(struct iw_quality) * dwrq->length);
1247 		/* Provided qual */
1248 		dwrq->flags = 1;
1249 	}
1250 
1251 	return 0;
1252 }
1253 
1254 static int
wl_iw_iscan_get_aplist(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1255 wl_iw_iscan_get_aplist(
1256 	struct net_device *dev,
1257 	struct iw_request_info *info,
1258 	struct iw_point *dwrq,
1259 	char *extra
1260 )
1261 {
1262 	wl_scan_results_t *list;
1263 	iscan_buf_t * buf;
1264 	iscan_info_t *iscan;
1265 
1266 	struct sockaddr *addr = (struct sockaddr *) extra;
1267 	struct iw_quality qual[IW_MAX_AP];
1268 	wl_bss_info_t *bi = NULL;
1269 	int i;
1270 	int16 rssi;
1271 	struct dhd_pub *dhd = dhd_get_pub(dev);
1272 	wl_wext_info_t *wext_info = NULL;
1273 
1274 	WL_TRACE(("%s: SIOCGIWAPLIST\n", dev->name));
1275 	DHD_CHECK(dhd, dev);
1276  	wext_info = dhd->wext_info;
1277 	iscan = &wext_info->iscan;
1278 
1279 	if (!extra)
1280 		return -EINVAL;
1281 
1282 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1283 		return wl_iw_get_aplist(dev, info, dwrq, extra);
1284 	}
1285 
1286 	buf = iscan->list_hdr;
1287 	/* Get scan results (too large to put on the stack) */
1288 	while (buf) {
1289 	    list = &((wl_iscan_results_t*)buf->iscan_buf)->results;
1290 	    ASSERT(list->version == WL_BSS_INFO_VERSION);
1291 
1292 	    bi = NULL;
1293 	for (i = 0, dwrq->length = 0; i < list->count && dwrq->length < IW_MAX_AP; i++) {
1294 		bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1295 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1296 			WLC_IW_ISCAN_MAXLEN));
1297 
1298 		/* Infrastructure only */
1299 		if (!(dtoh16(bi->capability) & DOT11_CAP_ESS))
1300 			continue;
1301 
1302 		/* BSSID */
1303 		memcpy(addr[dwrq->length].sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1304 		addr[dwrq->length].sa_family = ARPHRD_ETHER;
1305 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1306 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1307 		qual[dwrq->length].qual = rssi_to_qual(rssi);
1308 		qual[dwrq->length].level = 0x100 + rssi;
1309 		qual[dwrq->length].noise = 0x100 + bi->phy_noise;
1310 
1311 		/* Updated qual, level, and noise */
1312 #if WIRELESS_EXT > 18
1313 		qual[dwrq->length].updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1314 #else
1315 		qual[dwrq->length].updated = 7;
1316 #endif /* WIRELESS_EXT > 18 */
1317 
1318 		dwrq->length++;
1319 	    }
1320 	    buf = buf->next;
1321 	}
1322 	if (dwrq->length) {
1323 		memcpy(&addr[dwrq->length], qual, sizeof(struct iw_quality) * dwrq->length);
1324 		/* Provided qual */
1325 		dwrq->flags = 1;
1326 	}
1327 
1328 	return 0;
1329 }
1330 #endif
1331 
1332 #if WIRELESS_EXT > 13
1333 #ifndef WL_ESCAN
1334 static int
wl_iw_set_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1335 wl_iw_set_scan(
1336 	struct net_device *dev,
1337 	struct iw_request_info *info,
1338 	union iwreq_data *wrqu,
1339 	char *extra
1340 )
1341 {
1342 	wlc_ssid_t ssid;
1343 
1344 	WL_TRACE(("%s: SIOCSIWSCAN\n", dev->name));
1345 
1346 	/* default Broadcast scan */
1347 	memset(&ssid, 0, sizeof(ssid));
1348 
1349 #if WIRELESS_EXT > 17
1350 	/* check for given essid */
1351 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1352 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1353 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1354 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1355 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1356 			ssid.SSID_len = htod32(ssid.SSID_len);
1357 		}
1358 	}
1359 #endif
1360 	/* Ignore error (most likely scan in progress) */
1361 	(void) dev_wlc_ioctl(dev, WLC_SCAN, &ssid, sizeof(ssid));
1362 
1363 	return 0;
1364 }
1365 #endif
1366 
1367 static int
wl_iw_iscan_set_scan(struct net_device * dev,struct iw_request_info * info,union iwreq_data * wrqu,char * extra)1368 wl_iw_iscan_set_scan(
1369 	struct net_device *dev,
1370 	struct iw_request_info *info,
1371 	union iwreq_data *wrqu,
1372 	char *extra
1373 )
1374 {
1375 	struct dhd_pub *dhd = dhd_get_pub(dev);
1376 	wl_wext_info_t *wext_info = NULL;
1377 	wlc_ssid_t ssid;
1378 	int err;
1379 #ifndef WL_ESCAN
1380 	iscan_info_t *iscan;
1381 #endif
1382 
1383 	DHD_CHECK(dhd, dev);
1384 	wext_info = dhd->wext_info;
1385 	err = wl_ext_in4way_sync_wext(dev, STA_NO_SCAN_IN4WAY, WL_EXT_STATUS_SCAN, NULL);
1386 	if (err)
1387 		return err;
1388 #ifdef WL_ESCAN
1389 	/* default Broadcast scan */
1390 	memset(&ssid, 0, sizeof(ssid));
1391 #if WIRELESS_EXT > 17
1392 	/* check for given essid */
1393 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1394 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1395 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1396 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1397 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1398 			ssid.SSID_len = htod32(ssid.SSID_len);
1399 		}
1400 	}
1401 #endif
1402 	return wl_escan_set_scan(dev, dhd, &ssid, 0, TRUE);
1403 #else
1404 	iscan = &wext_info->iscan;
1405 	WL_TRACE(("%s: SIOCSIWSCAN iscan=%p\n", dev->name, iscan));
1406 
1407 	/* use backup if our thread is not successful */
1408 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1409 		return wl_iw_set_scan(dev, info, wrqu, extra);
1410 	}
1411 	if (iscan->iscan_state == ISCAN_STATE_SCANING) {
1412 		return 0;
1413 	}
1414 
1415 	/* default Broadcast scan */
1416 	memset(&ssid, 0, sizeof(ssid));
1417 
1418 #if WIRELESS_EXT > 17
1419 	/* check for given essid */
1420 	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1421 		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1422 			struct iw_scan_req *req = (struct iw_scan_req *)extra;
1423 			ssid.SSID_len = MIN(sizeof(ssid.SSID), req->essid_len);
1424 			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
1425 			ssid.SSID_len = htod32(ssid.SSID_len);
1426 		}
1427 	}
1428 #endif
1429 
1430 	iscan->list_cur = iscan->list_hdr;
1431 	iscan->iscan_state = ISCAN_STATE_SCANING;
1432 
1433 
1434 	wl_iw_set_event_mask(dev);
1435 	wl_iw_iscan(iscan, &ssid, WL_SCAN_ACTION_START);
1436 
1437 	iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
1438 	add_timer(&iscan->timer);
1439 	iscan->timer_on = 1;
1440 
1441 	return 0;
1442 #endif
1443 }
1444 
1445 #if WIRELESS_EXT > 17
1446 static bool
ie_is_wpa_ie(uint8 ** wpaie,uint8 ** tlvs,int * tlvs_len)1447 ie_is_wpa_ie(uint8 **wpaie, uint8 **tlvs, int *tlvs_len)
1448 {
1449 /* Is this body of this tlvs entry a WPA entry? If */
1450 /* not update the tlvs buffer pointer/length */
1451 	uint8 *ie = *wpaie;
1452 
1453 	/* If the contents match the WPA_OUI and type=1 */
1454 	if ((ie[1] >= 6) &&
1455 		!bcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x01"), 4)) {
1456 		return TRUE;
1457 	}
1458 
1459 	/* point to the next ie */
1460 	ie += ie[1] + 2;
1461 	/* calculate the length of the rest of the buffer */
1462 	*tlvs_len -= (int)(ie - *tlvs);
1463 	/* update the pointer to the start of the buffer */
1464 	*tlvs = ie;
1465 	return FALSE;
1466 }
1467 
1468 static bool
ie_is_wps_ie(uint8 ** wpsie,uint8 ** tlvs,int * tlvs_len)1469 ie_is_wps_ie(uint8 **wpsie, uint8 **tlvs, int *tlvs_len)
1470 {
1471 /* Is this body of this tlvs entry a WPS entry? If */
1472 /* not update the tlvs buffer pointer/length */
1473 	uint8 *ie = *wpsie;
1474 
1475 	/* If the contents match the WPA_OUI and type=4 */
1476 	if ((ie[1] >= 4) &&
1477 		!bcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x04"), 4)) {
1478 		return TRUE;
1479 	}
1480 
1481 	/* point to the next ie */
1482 	ie += ie[1] + 2;
1483 	/* calculate the length of the rest of the buffer */
1484 	*tlvs_len -= (int)(ie - *tlvs);
1485 	/* update the pointer to the start of the buffer */
1486 	*tlvs = ie;
1487 	return FALSE;
1488 }
1489 #endif /* WIRELESS_EXT > 17 */
1490 
1491 #ifdef BCMWAPI_WPI
_wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len,int uppercase)1492 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
1493 	size_t len, int uppercase)
1494 {
1495 	size_t i;
1496 	char *pos = buf, *end = buf + buf_size;
1497 	int ret;
1498 	if (buf_size == 0)
1499 		return 0;
1500 	for (i = 0; i < len; i++) {
1501 		ret = snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
1502 			data[i]);
1503 		if (ret < 0 || ret >= end - pos) {
1504 			end[-1] = '\0';
1505 			return pos - buf;
1506 		}
1507 		pos += ret;
1508 	}
1509 	end[-1] = '\0';
1510 	return pos - buf;
1511 }
1512 
1513 /**
1514  * wpa_snprintf_hex - Print data as a hex string into a buffer
1515  * @buf: Memory area to use as the output buffer
1516  * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
1517  * @data: Data to be printed
1518  * @len: Length of data in bytes
1519  * Returns: Number of bytes written
1520  */
1521 static int
wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len)1522 wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
1523 {
1524 	return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
1525 }
1526 #endif /* BCMWAPI_WPI */
1527 
1528 #ifndef WL_ESCAN
1529 static
1530 #endif
1531 int
wl_iw_handle_scanresults_ies(char ** event_p,char * end,struct iw_request_info * info,wl_bss_info_t * bi)1532 wl_iw_handle_scanresults_ies(char **event_p, char *end,
1533 	struct iw_request_info *info, wl_bss_info_t *bi)
1534 {
1535 #if WIRELESS_EXT > 17
1536 	struct iw_event	iwe;
1537 	char *event;
1538 #ifdef BCMWAPI_WPI
1539 	char *buf;
1540 	int custom_event_len;
1541 #endif
1542 
1543 	event = *event_p;
1544 	if (bi->ie_length) {
1545 		/* look for wpa/rsn ies in the ie list... */
1546 		bcm_tlv_t *ie;
1547 		uint8 *ptr = ((uint8 *)bi) + bi->ie_offset;
1548 		int ptr_len = bi->ie_length;
1549 
1550 		/* OSEN IE */
1551 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_VS_ID)) &&
1552 			ie->len > WFA_OUI_LEN + 1 &&
1553 			!bcmp((const void *)&ie->data[0], (const void *)WFA_OUI, WFA_OUI_LEN) &&
1554 			ie->data[WFA_OUI_LEN] == WFA_OUI_TYPE_OSEN) {
1555 			iwe.cmd = IWEVGENIE;
1556 			iwe.u.data.length = ie->len + 2;
1557 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1558 		}
1559 		ptr = ((uint8 *)bi) + bi->ie_offset;
1560 
1561 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_RSN_ID))) {
1562 			iwe.cmd = IWEVGENIE;
1563 			iwe.u.data.length = ie->len + 2;
1564 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1565 		}
1566 		ptr = ((uint8 *)bi) + bi->ie_offset;
1567 
1568 		if ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_MDIE_ID))) {
1569 			iwe.cmd = IWEVGENIE;
1570 			iwe.u.data.length = ie->len + 2;
1571 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1572 		}
1573 		ptr = ((uint8 *)bi) + bi->ie_offset;
1574 
1575 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
1576 			/* look for WPS IE */
1577 			if (ie_is_wps_ie(((uint8 **)&ie), &ptr, &ptr_len)) {
1578 				iwe.cmd = IWEVGENIE;
1579 				iwe.u.data.length = ie->len + 2;
1580 				event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1581 				break;
1582 			}
1583 		}
1584 
1585 		ptr = ((uint8 *)bi) + bi->ie_offset;
1586 		ptr_len = bi->ie_length;
1587 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
1588 			if (ie_is_wpa_ie(((uint8 **)&ie), &ptr, &ptr_len)) {
1589 				iwe.cmd = IWEVGENIE;
1590 				iwe.u.data.length = ie->len + 2;
1591 				event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1592 				break;
1593 			}
1594 		}
1595 
1596 #ifdef BCMWAPI_WPI
1597 		ptr = ((uint8 *)bi) + sizeof(wl_bss_info_t);
1598 		ptr_len = bi->ie_length;
1599 
1600 		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WAPI_ID))) {
1601 			WL_TRACE(("found a WAPI IE...\n"));
1602 #ifdef WAPI_IE_USE_GENIE
1603 			iwe.cmd = IWEVGENIE;
1604 			iwe.u.data.length = ie->len + 2;
1605 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)ie);
1606 #else /* using CUSTOM event */
1607 			iwe.cmd = IWEVCUSTOM;
1608 			custom_event_len = strlen("wapi_ie=") + 2*(ie->len + 2);
1609 			iwe.u.data.length = custom_event_len;
1610 
1611 			buf = kmalloc(custom_event_len+1, GFP_KERNEL);
1612 			if (buf == NULL)
1613 			{
1614 				WL_ERROR(("malloc(%d) returned NULL...\n", custom_event_len));
1615 				break;
1616 			}
1617 
1618 			memcpy(buf, "wapi_ie=", 8);
1619 			wpa_snprintf_hex(buf + 8, 2+1, &(ie->id), 1);
1620 			wpa_snprintf_hex(buf + 10, 2+1, &(ie->len), 1);
1621 			wpa_snprintf_hex(buf + 12, 2*ie->len+1, ie->data, ie->len);
1622 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, buf);
1623 			kfree(buf);
1624 #endif /* WAPI_IE_USE_GENIE */
1625 			break;
1626 		}
1627 #endif /* BCMWAPI_WPI */
1628 		*event_p = event;
1629 	}
1630 
1631 #endif /* WIRELESS_EXT > 17 */
1632 	return 0;
1633 }
1634 
1635 #ifndef WL_ESCAN
1636 static int
wl_iw_get_scan(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1637 wl_iw_get_scan(
1638 	struct net_device *dev,
1639 	struct iw_request_info *info,
1640 	struct iw_point *dwrq,
1641 	char *extra
1642 )
1643 {
1644 	channel_info_t ci;
1645 	wl_scan_results_t *list;
1646 	struct iw_event	iwe;
1647 	wl_bss_info_t *bi = NULL;
1648 	int error, i, j;
1649 	char *event = extra, *end = extra + dwrq->length, *value;
1650 	uint buflen = dwrq->length;
1651 	int16 rssi;
1652 	int channel;
1653 
1654 	WL_TRACE(("%s SIOCGIWSCAN\n", dev->name));
1655 
1656 	if (!extra)
1657 		return -EINVAL;
1658 
1659 	/* Check for scan in progress */
1660 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(ci))))
1661 		return error;
1662 	ci.scan_channel = dtoh32(ci.scan_channel);
1663 	if (ci.scan_channel)
1664 		return -EAGAIN;
1665 
1666 	/* Get scan results (too large to put on the stack) */
1667 	list = kmalloc(buflen, GFP_KERNEL);
1668 	if (!list)
1669 		return -ENOMEM;
1670 	memset(list, 0, buflen);
1671 	list->buflen = htod32(buflen);
1672 	if ((error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, buflen))) {
1673 		kfree(list);
1674 		return error;
1675 	}
1676 	list->buflen = dtoh32(list->buflen);
1677 	list->version = dtoh32(list->version);
1678 	list->count = dtoh32(list->count);
1679 
1680 	ASSERT(list->version == WL_BSS_INFO_VERSION);
1681 
1682 	for (i = 0; i < list->count && i < IW_MAX_AP; i++) {
1683 		bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1684 		ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1685 			buflen));
1686 
1687 		// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1688 		rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1689 		channel = (bi->ctl_ch == 0) ? CHSPEC_CHANNEL(bi->chanspec) : bi->ctl_ch;
1690 		WL_SCAN(("BSSID="MACSTR", channel=%d, RSSI=%d, SSID=\"%s\"\n",
1691 			MAC2STR(bi->BSSID.octet), channel, rssi, bi->SSID));
1692 
1693 		/* First entry must be the BSSID */
1694 		iwe.cmd = SIOCGIWAP;
1695 		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1696 		memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1697 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_ADDR_LEN);
1698 
1699 		/* SSID */
1700 		iwe.u.data.length = dtoh32(bi->SSID_len);
1701 		iwe.cmd = SIOCGIWESSID;
1702 		iwe.u.data.flags = 1;
1703 		event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, bi->SSID);
1704 
1705 		/* Mode */
1706 		if (dtoh16(bi->capability) & (DOT11_CAP_ESS | DOT11_CAP_IBSS)) {
1707 			iwe.cmd = SIOCGIWMODE;
1708 			if (dtoh16(bi->capability) & DOT11_CAP_ESS)
1709 				iwe.u.mode = IW_MODE_INFRA;
1710 			else
1711 				iwe.u.mode = IW_MODE_ADHOC;
1712 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_UINT_LEN);
1713 		}
1714 
1715 		/* Channel */
1716 		iwe.cmd = SIOCGIWFREQ;
1717 
1718 		iwe.u.freq.m = wf_channel2mhz(CHSPEC_CHANNEL(bi->chanspec),
1719 			(CHSPEC_IS2G(bi->chanspec)) ?
1720 			WF_CHAN_FACTOR_2_4_G : WF_CHAN_FACTOR_5_G);
1721 		iwe.u.freq.e = 6;
1722 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_FREQ_LEN);
1723 
1724 		/* Channel quality */
1725 		iwe.cmd = IWEVQUAL;
1726 		iwe.u.qual.qual = rssi_to_qual(rssi);
1727 		iwe.u.qual.level = 0x100 + rssi;
1728 		iwe.u.qual.noise = 0x100 + bi->phy_noise;
1729 		event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_QUAL_LEN);
1730 
1731 		 wl_iw_handle_scanresults_ies(&event, end, info, bi);
1732 
1733 		/* Encryption */
1734 		iwe.cmd = SIOCGIWENCODE;
1735 		if (dtoh16(bi->capability) & DOT11_CAP_PRIVACY)
1736 			iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1737 		else
1738 			iwe.u.data.flags = IW_ENCODE_DISABLED;
1739 		iwe.u.data.length = 0;
1740 		event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)event);
1741 
1742 		/* Rates */
1743 		if (bi->rateset.count) {
1744 			value = event + IW_EV_LCP_LEN;
1745 			iwe.cmd = SIOCGIWRATE;
1746 			/* Those two flags are ignored... */
1747 			iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1748 			for (j = 0; j < bi->rateset.count && j < IW_MAX_BITRATES; j++) {
1749 				iwe.u.bitrate.value = (bi->rateset.rates[j] & 0x7f) * 500000;
1750 				value = IWE_STREAM_ADD_VALUE(info, event, value, end, &iwe,
1751 					IW_EV_PARAM_LEN);
1752 			}
1753 			event = value;
1754 		}
1755 	}
1756 
1757 	kfree(list);
1758 
1759 	dwrq->length = event - extra;
1760 	dwrq->flags = 0;	/* todo */
1761 
1762 	return 0;
1763 }
1764 #endif /* WL_ESCAN */
1765 
1766 static int
wl_iw_iscan_get_scan(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1767 wl_iw_iscan_get_scan(
1768 	struct net_device *dev,
1769 	struct iw_request_info *info,
1770 	struct iw_point *dwrq,
1771 	char *extra
1772 )
1773 {
1774 	struct dhd_pub *dhd = dhd_get_pub(dev);
1775 	wl_wext_info_t *wext_info = NULL;
1776 #ifndef WL_ESCAN
1777 	wl_scan_results_t *list;
1778 	struct iw_event	iwe;
1779 	wl_bss_info_t *bi = NULL;
1780 	int ii, j;
1781 	int apcnt;
1782 	char *event = extra, *end = extra + dwrq->length, *value;
1783 	iscan_buf_t * p_buf;
1784 	int16 rssi;
1785 	int channel;
1786 	iscan_info_t *iscan;
1787 #endif
1788 
1789 	DHD_CHECK(dhd, dev);
1790 	wext_info = dhd->wext_info;
1791 #ifdef WL_ESCAN
1792 	return wl_escan_get_scan(dev, dhd, info, dwrq, extra);
1793 #else
1794 	WL_TRACE(("%s SIOCGIWSCAN\n", dev->name));
1795 
1796 	if (!extra)
1797 		return -EINVAL;
1798 
1799 	/* use backup if our thread is not successful */
1800 	iscan = &wext_info->iscan;
1801 	if ((!iscan) || (iscan->sysioc_pid < 0)) {
1802 		return wl_iw_get_scan(dev, info, dwrq, extra);
1803 	}
1804 
1805 	/* Check for scan in progress */
1806 	if (iscan->iscan_state == ISCAN_STATE_SCANING) {
1807 		WL_TRACE(("%s: SIOCGIWSCAN GET still scanning\n", dev->name));
1808 		return -EAGAIN;
1809 	}
1810 
1811 	apcnt = 0;
1812 	p_buf = iscan->list_hdr;
1813 	/* Get scan results */
1814 	while (p_buf != iscan->list_cur) {
1815 		list = &((wl_iscan_results_t*)p_buf->iscan_buf)->results;
1816 
1817 		if (list->version != WL_BSS_INFO_VERSION) {
1818 			WL_ERROR(("list->version %d != WL_BSS_INFO_VERSION\n", list->version));
1819 		}
1820 
1821 		bi = NULL;
1822 		for (ii = 0; ii < list->count && apcnt < IW_MAX_AP; apcnt++, ii++) {
1823 			bi = bi ? (wl_bss_info_t *)((uintptr)bi + dtoh32(bi->length)) : list->bss_info;
1824 			ASSERT(((uintptr)bi + dtoh32(bi->length)) <= ((uintptr)list +
1825 				WLC_IW_ISCAN_MAXLEN));
1826 
1827 			/* overflow check cover fields before wpa IEs */
1828 			if (event + ETHER_ADDR_LEN + bi->SSID_len + IW_EV_UINT_LEN + IW_EV_FREQ_LEN +
1829 				IW_EV_QUAL_LEN >= end)
1830 				return -E2BIG;
1831 
1832 			// terence 20150419: limit the max. rssi to -2 or the bss will be filtered out in android OS
1833 			rssi = MIN(dtoh16(bi->RSSI), RSSI_MAXVAL);
1834 			channel = (bi->ctl_ch == 0) ? CHSPEC_CHANNEL(bi->chanspec) : bi->ctl_ch;
1835 			WL_SCAN(("BSSID="MACSTR", channel=%d, RSSI=%d, SSID=\"%s\"\n",
1836 				MAC2STR(bi->BSSID.octet), channel, rssi, bi->SSID));
1837 
1838 			/* First entry must be the BSSID */
1839 			iwe.cmd = SIOCGIWAP;
1840 			iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1841 			memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID, ETHER_ADDR_LEN);
1842 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_ADDR_LEN);
1843 
1844 			/* SSID */
1845 			iwe.u.data.length = dtoh32(bi->SSID_len);
1846 			iwe.cmd = SIOCGIWESSID;
1847 			iwe.u.data.flags = 1;
1848 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, bi->SSID);
1849 
1850 			/* Mode */
1851 			if (dtoh16(bi->capability) & (DOT11_CAP_ESS | DOT11_CAP_IBSS)) {
1852 				iwe.cmd = SIOCGIWMODE;
1853 				if (dtoh16(bi->capability) & DOT11_CAP_ESS)
1854 					iwe.u.mode = IW_MODE_INFRA;
1855 				else
1856 					iwe.u.mode = IW_MODE_ADHOC;
1857 				event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_UINT_LEN);
1858 			}
1859 
1860 			/* Channel */
1861 			iwe.cmd = SIOCGIWFREQ;
1862 			iwe.u.freq.m = wf_channel2mhz(CHSPEC_CHANNEL(bi->chanspec),
1863 				(CHSPEC_IS2G(bi->chanspec)) ?
1864 				WF_CHAN_FACTOR_2_4_G : WF_CHAN_FACTOR_5_G);
1865 			iwe.u.freq.e = 6;
1866 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_FREQ_LEN);
1867 
1868 			/* Channel quality */
1869 			iwe.cmd = IWEVQUAL;
1870 			iwe.u.qual.qual = rssi_to_qual(rssi);
1871 			iwe.u.qual.level = 0x100 + rssi;
1872 			iwe.u.qual.noise = 0x100 + bi->phy_noise;
1873 			event = IWE_STREAM_ADD_EVENT(info, event, end, &iwe, IW_EV_QUAL_LEN);
1874 
1875 			wl_iw_handle_scanresults_ies(&event, end, info, bi);
1876 
1877 			/* Encryption */
1878 			iwe.cmd = SIOCGIWENCODE;
1879 			if (dtoh16(bi->capability) & DOT11_CAP_PRIVACY)
1880 				iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1881 			else
1882 				iwe.u.data.flags = IW_ENCODE_DISABLED;
1883 			iwe.u.data.length = 0;
1884 			event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)event);
1885 
1886 			/* Rates */
1887 			if (bi->rateset.count <= sizeof(bi->rateset.rates)) {
1888 				if (event + IW_MAX_BITRATES*IW_EV_PARAM_LEN >= end)
1889 					return -E2BIG;
1890 
1891 				value = event + IW_EV_LCP_LEN;
1892 				iwe.cmd = SIOCGIWRATE;
1893 				/* Those two flags are ignored... */
1894 				iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1895 				for (j = 0; j < bi->rateset.count && j < IW_MAX_BITRATES; j++) {
1896 					iwe.u.bitrate.value = (bi->rateset.rates[j] & 0x7f) * 500000;
1897 					value = IWE_STREAM_ADD_VALUE(info, event, value, end, &iwe,
1898 						IW_EV_PARAM_LEN);
1899 				}
1900 				event = value;
1901 			}
1902 		}
1903 		p_buf = p_buf->next;
1904 	} /* while (p_buf) */
1905 
1906 	dwrq->length = event - extra;
1907 	dwrq->flags = 0;	/* todo */
1908 	WL_SCAN(("apcnt=%d\n", apcnt));
1909 
1910 	return 0;
1911 #endif
1912 }
1913 #endif /* WIRELESS_EXT > 13 */
1914 
1915 
1916 static int
wl_iw_set_essid(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1917 wl_iw_set_essid(
1918 	struct net_device *dev,
1919 	struct iw_request_info *info,
1920 	struct iw_point *dwrq,
1921 	char *extra
1922 )
1923 {
1924 	wlc_ssid_t ssid;
1925 	int error;
1926 	struct dhd_pub *dhd = dhd_get_pub(dev);
1927 	wl_wext_info_t *wext_info = NULL;
1928 
1929 	WL_TRACE(("%s: SIOCSIWESSID\n", dev->name));
1930 	DHD_CHECK(dhd, dev);
1931 	wext_info = dhd->wext_info;
1932 
1933 	/* default Broadcast SSID */
1934 	memset(&ssid, 0, sizeof(ssid));
1935 	if (dwrq->length && extra) {
1936 #if WIRELESS_EXT > 20
1937 		ssid.SSID_len = MIN(sizeof(ssid.SSID), dwrq->length);
1938 #else
1939 		ssid.SSID_len = MIN(sizeof(ssid.SSID), dwrq->length-1);
1940 #endif
1941 		memcpy(ssid.SSID, extra, ssid.SSID_len);
1942 		ssid.SSID_len = htod32(ssid.SSID_len);
1943 
1944 		if (wext_info) {
1945 			memcpy(wext_info->conn_info.ssid.SSID, ssid.SSID, ssid.SSID_len);
1946 			wext_info->conn_info.ssid.SSID_len = ssid.SSID_len;
1947 		}
1948 		if (wext_info && memcmp(&ether_null, &wext_info->conn_info.bssid, ETHER_ADDR_LEN)) {
1949 			if ((error = wl_ext_connect(dev, &wext_info->conn_info)))
1950 				return error;
1951 		} else {
1952 			if ((error = dev_wlc_ioctl(dev, WLC_SET_SSID, &ssid, sizeof(ssid)))) {
1953 				WL_ERROR(("WLC_SET_SSID failed (%d).\n", error));
1954 				return error;
1955 			}
1956 			WL_MSG(dev->name, "join SSID=\"%s\"\n", ssid.SSID);
1957 		}
1958 		wl_ext_in4way_sync_wext(dev, STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY,
1959 			WL_EXT_STATUS_CONNECTING, NULL);
1960 	}
1961 	/* If essid null then it is "iwconfig <interface> essid off" command */
1962 	else {
1963 		scb_val_t scbval;
1964 		bzero(&scbval, sizeof(scb_val_t));
1965 		WL_MSG(dev->name, "WLC_DISASSOC\n");
1966 		if ((error = dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval, sizeof(scb_val_t)))) {
1967 			WL_ERROR(("WLC_DISASSOC failed (%d).\n", error));
1968 			return error;
1969 		}
1970 		wl_ext_in4way_sync_wext(dev,
1971 			STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
1972 			WL_EXT_STATUS_DISCONNECTING, NULL);
1973 	}
1974 	return 0;
1975 }
1976 
1977 static int
wl_iw_get_essid(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)1978 wl_iw_get_essid(
1979 	struct net_device *dev,
1980 	struct iw_request_info *info,
1981 	struct iw_point *dwrq,
1982 	char *extra
1983 )
1984 {
1985 	wlc_ssid_t ssid;
1986 	int error;
1987 
1988 	WL_TRACE(("%s: SIOCGIWESSID\n", dev->name));
1989 
1990 	if (!extra)
1991 		return -EINVAL;
1992 
1993 	if ((error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid)))) {
1994 		WL_ERROR(("Error getting the SSID %d\n", error));
1995 		return error;
1996 	}
1997 
1998 	ssid.SSID_len = dtoh32(ssid.SSID_len);
1999 
2000 	/* Max SSID length check */
2001 	if (ssid.SSID_len > IW_ESSID_MAX_SIZE) {
2002 		ssid.SSID_len = IW_ESSID_MAX_SIZE;
2003 	}
2004 
2005 	/* Get the current SSID */
2006 	memcpy(extra, ssid.SSID, ssid.SSID_len);
2007 
2008 	/* NULL terminating as length of extra buffer is IW_ESSID_MAX_SIZE ie 32 */
2009 	extra[IW_ESSID_MAX_SIZE] = '\0';
2010 
2011 	dwrq->length = ssid.SSID_len;
2012 
2013 	dwrq->flags = 1; /* active */
2014 
2015 	return 0;
2016 }
2017 
2018 static int
wl_iw_set_nick(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)2019 wl_iw_set_nick(
2020 	struct net_device *dev,
2021 	struct iw_request_info *info,
2022 	struct iw_point *dwrq,
2023 	char *extra
2024 )
2025 {
2026 	wl_iw_t *iw = IW_DEV_IF(dev);
2027 	WL_TRACE(("%s: SIOCSIWNICKN\n", dev->name));
2028 
2029 	if (!extra)
2030 		return -EINVAL;
2031 
2032 	/* Check the size of the string */
2033 	if (dwrq->length > sizeof(iw->nickname))
2034 		return -E2BIG;
2035 
2036 	memcpy(iw->nickname, extra, dwrq->length);
2037 	iw->nickname[dwrq->length - 1] = '\0';
2038 
2039 	return 0;
2040 }
2041 
2042 static int
wl_iw_get_nick(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)2043 wl_iw_get_nick(
2044 	struct net_device *dev,
2045 	struct iw_request_info *info,
2046 	struct iw_point *dwrq,
2047 	char *extra
2048 )
2049 {
2050 	wl_iw_t *iw = IW_DEV_IF(dev);
2051 	WL_TRACE(("%s: SIOCGIWNICKN\n", dev->name));
2052 
2053 	if (!extra)
2054 		return -EINVAL;
2055 
2056 	strcpy(extra, iw->nickname);
2057 	dwrq->length = strlen(extra) + 1;
2058 
2059 	return 0;
2060 }
2061 
wl_iw_set_rate(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2062 static int wl_iw_set_rate(
2063 	struct net_device *dev,
2064 	struct iw_request_info *info,
2065 	struct iw_param *vwrq,
2066 	char *extra
2067 )
2068 {
2069 	wl_rateset_t rateset;
2070 	int error, rate, i, error_bg, error_a;
2071 
2072 	WL_TRACE(("%s: SIOCSIWRATE\n", dev->name));
2073 
2074 	/* Get current rateset */
2075 	if ((error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset, sizeof(rateset))))
2076 		return error;
2077 
2078 	rateset.count = dtoh32(rateset.count);
2079 
2080 	if (vwrq->value < 0) {
2081 		/* Select maximum rate */
2082 		rate = rateset.rates[rateset.count - 1] & 0x7f;
2083 	} else if (vwrq->value < rateset.count) {
2084 		/* Select rate by rateset index */
2085 		rate = rateset.rates[vwrq->value] & 0x7f;
2086 	} else {
2087 		/* Specified rate in bps */
2088 		rate = vwrq->value / 500000;
2089 	}
2090 
2091 	if (vwrq->fixed) {
2092 		/*
2093 			Set rate override,
2094 			Since the is a/b/g-blind, both a/bg_rate are enforced.
2095 		*/
2096 		error_bg = dev_wlc_intvar_set(dev, "bg_rate", rate);
2097 		error_a = dev_wlc_intvar_set(dev, "a_rate", rate);
2098 
2099 		if (error_bg && error_a)
2100 			return (error_bg | error_a);
2101 	} else {
2102 		/*
2103 			clear rate override
2104 			Since the is a/b/g-blind, both a/bg_rate are enforced.
2105 		*/
2106 		/* 0 is for clearing rate override */
2107 		error_bg = dev_wlc_intvar_set(dev, "bg_rate", 0);
2108 		/* 0 is for clearing rate override */
2109 		error_a = dev_wlc_intvar_set(dev, "a_rate", 0);
2110 
2111 		if (error_bg && error_a)
2112 			return (error_bg | error_a);
2113 
2114 		/* Remove rates above selected rate */
2115 		for (i = 0; i < rateset.count; i++)
2116 			if ((rateset.rates[i] & 0x7f) > rate)
2117 				break;
2118 		rateset.count = htod32(i);
2119 
2120 		/* Set current rateset */
2121 		if ((error = dev_wlc_ioctl(dev, WLC_SET_RATESET, &rateset, sizeof(rateset))))
2122 			return error;
2123 	}
2124 
2125 	return 0;
2126 }
2127 
wl_iw_get_rate(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2128 static int wl_iw_get_rate(
2129 	struct net_device *dev,
2130 	struct iw_request_info *info,
2131 	struct iw_param *vwrq,
2132 	char *extra
2133 )
2134 {
2135 	int error, rate;
2136 
2137 	WL_TRACE(("%s: SIOCGIWRATE\n", dev->name));
2138 
2139 	/* Report the current tx rate */
2140 	if ((error = dev_wlc_ioctl(dev, WLC_GET_RATE, &rate, sizeof(rate))))
2141 		return error;
2142 	rate = dtoh32(rate);
2143 	vwrq->value = rate * 500000;
2144 
2145 	return 0;
2146 }
2147 
2148 static int
wl_iw_set_rts(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2149 wl_iw_set_rts(
2150 	struct net_device *dev,
2151 	struct iw_request_info *info,
2152 	struct iw_param *vwrq,
2153 	char *extra
2154 )
2155 {
2156 	int error, rts;
2157 
2158 	WL_TRACE(("%s: SIOCSIWRTS\n", dev->name));
2159 
2160 	if (vwrq->disabled)
2161 		rts = DOT11_DEFAULT_RTS_LEN;
2162 	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_RTS_LEN)
2163 		return -EINVAL;
2164 	else
2165 		rts = vwrq->value;
2166 
2167 	if ((error = dev_wlc_intvar_set(dev, "rtsthresh", rts)))
2168 		return error;
2169 
2170 	return 0;
2171 }
2172 
2173 static int
wl_iw_get_rts(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2174 wl_iw_get_rts(
2175 	struct net_device *dev,
2176 	struct iw_request_info *info,
2177 	struct iw_param *vwrq,
2178 	char *extra
2179 )
2180 {
2181 	int error, rts;
2182 
2183 	WL_TRACE(("%s: SIOCGIWRTS\n", dev->name));
2184 
2185 	if ((error = dev_wlc_intvar_get(dev, "rtsthresh", &rts)))
2186 		return error;
2187 
2188 	vwrq->value = rts;
2189 	vwrq->disabled = (rts >= DOT11_DEFAULT_RTS_LEN);
2190 	vwrq->fixed = 1;
2191 
2192 	return 0;
2193 }
2194 
2195 static int
wl_iw_set_frag(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2196 wl_iw_set_frag(
2197 	struct net_device *dev,
2198 	struct iw_request_info *info,
2199 	struct iw_param *vwrq,
2200 	char *extra
2201 )
2202 {
2203 	int error, frag;
2204 
2205 	WL_TRACE(("%s: SIOCSIWFRAG\n", dev->name));
2206 
2207 	if (vwrq->disabled)
2208 		frag = DOT11_DEFAULT_FRAG_LEN;
2209 	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_FRAG_LEN)
2210 		return -EINVAL;
2211 	else
2212 		frag = vwrq->value;
2213 
2214 	if ((error = dev_wlc_intvar_set(dev, "fragthresh", frag)))
2215 		return error;
2216 
2217 	return 0;
2218 }
2219 
2220 static int
wl_iw_get_frag(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2221 wl_iw_get_frag(
2222 	struct net_device *dev,
2223 	struct iw_request_info *info,
2224 	struct iw_param *vwrq,
2225 	char *extra
2226 )
2227 {
2228 	int error, fragthreshold;
2229 
2230 	WL_TRACE(("%s: SIOCGIWFRAG\n", dev->name));
2231 
2232 	if ((error = dev_wlc_intvar_get(dev, "fragthresh", &fragthreshold)))
2233 		return error;
2234 
2235 	vwrq->value = fragthreshold;
2236 	vwrq->disabled = (fragthreshold >= DOT11_DEFAULT_FRAG_LEN);
2237 	vwrq->fixed = 1;
2238 
2239 	return 0;
2240 }
2241 
2242 static int
wl_iw_set_txpow(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2243 wl_iw_set_txpow(
2244 	struct net_device *dev,
2245 	struct iw_request_info *info,
2246 	struct iw_param *vwrq,
2247 	char *extra
2248 )
2249 {
2250 	int error, disable;
2251 	uint16 txpwrmw;
2252 	WL_TRACE(("%s: SIOCSIWTXPOW\n", dev->name));
2253 
2254 	/* Make sure radio is off or on as far as software is concerned */
2255 	disable = vwrq->disabled ? WL_RADIO_SW_DISABLE : 0;
2256 	disable += WL_RADIO_SW_DISABLE << 16;
2257 
2258 	disable = htod32(disable);
2259 	if ((error = dev_wlc_ioctl(dev, WLC_SET_RADIO, &disable, sizeof(disable))))
2260 		return error;
2261 
2262 	/* If Radio is off, nothing more to do */
2263 	if (disable & WL_RADIO_SW_DISABLE)
2264 		return 0;
2265 
2266 	/* Only handle mW */
2267 	if (!(vwrq->flags & IW_TXPOW_MWATT))
2268 		return -EINVAL;
2269 
2270 	/* Value < 0 means just "on" or "off" */
2271 	if (vwrq->value < 0)
2272 		return 0;
2273 
2274 	if (vwrq->value > 0xffff) txpwrmw = 0xffff;
2275 	else txpwrmw = (uint16)vwrq->value;
2276 
2277 
2278 	error = dev_wlc_intvar_set(dev, "qtxpower", (int)(bcm_mw_to_qdbm(txpwrmw)));
2279 	return error;
2280 }
2281 
2282 static int
wl_iw_get_txpow(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2283 wl_iw_get_txpow(
2284 	struct net_device *dev,
2285 	struct iw_request_info *info,
2286 	struct iw_param *vwrq,
2287 	char *extra
2288 )
2289 {
2290 	int error, disable, txpwrdbm;
2291 	uint8 result;
2292 
2293 	WL_TRACE(("%s: SIOCGIWTXPOW\n", dev->name));
2294 
2295 	if ((error = dev_wlc_ioctl(dev, WLC_GET_RADIO, &disable, sizeof(disable))) ||
2296 	    (error = dev_wlc_intvar_get(dev, "qtxpower", &txpwrdbm)))
2297 		return error;
2298 
2299 	disable = dtoh32(disable);
2300 	result = (uint8)(txpwrdbm & ~WL_TXPWR_OVERRIDE);
2301 	vwrq->value = (int32)bcm_qdbm_to_mw(result);
2302 	vwrq->fixed = 0;
2303 	vwrq->disabled = (disable & (WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE)) ? 1 : 0;
2304 	vwrq->flags = IW_TXPOW_MWATT;
2305 
2306 	return 0;
2307 }
2308 
2309 #if WIRELESS_EXT > 10
2310 static int
wl_iw_set_retry(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2311 wl_iw_set_retry(
2312 	struct net_device *dev,
2313 	struct iw_request_info *info,
2314 	struct iw_param *vwrq,
2315 	char *extra
2316 )
2317 {
2318 	int error, lrl, srl;
2319 
2320 	WL_TRACE(("%s: SIOCSIWRETRY\n", dev->name));
2321 
2322 	/* Do not handle "off" or "lifetime" */
2323 	if (vwrq->disabled || (vwrq->flags & IW_RETRY_LIFETIME))
2324 		return -EINVAL;
2325 
2326 	/* Handle "[min|max] limit" */
2327 	if (vwrq->flags & IW_RETRY_LIMIT) {
2328 		/* "max limit" or just "limit" */
2329 #if WIRELESS_EXT > 20
2330 		if ((vwrq->flags & IW_RETRY_LONG) ||(vwrq->flags & IW_RETRY_MAX) ||
2331 			!((vwrq->flags & IW_RETRY_SHORT) || (vwrq->flags & IW_RETRY_MIN)))
2332 #else
2333 		if ((vwrq->flags & IW_RETRY_MAX) || !(vwrq->flags & IW_RETRY_MIN))
2334 #endif /* WIRELESS_EXT > 20 */
2335 		{
2336 			lrl = htod32(vwrq->value);
2337 			if ((error = dev_wlc_ioctl(dev, WLC_SET_LRL, &lrl, sizeof(lrl))))
2338 				return error;
2339 		}
2340 		/* "min limit" or just "limit" */
2341 #if WIRELESS_EXT > 20
2342 		if ((vwrq->flags & IW_RETRY_SHORT) ||(vwrq->flags & IW_RETRY_MIN) ||
2343 			!((vwrq->flags & IW_RETRY_LONG) || (vwrq->flags & IW_RETRY_MAX)))
2344 #else
2345 		if ((vwrq->flags & IW_RETRY_MIN) || !(vwrq->flags & IW_RETRY_MAX))
2346 #endif /* WIRELESS_EXT > 20 */
2347 		{
2348 			srl = htod32(vwrq->value);
2349 			if ((error = dev_wlc_ioctl(dev, WLC_SET_SRL, &srl, sizeof(srl))))
2350 				return error;
2351 		}
2352 	}
2353 
2354 	return 0;
2355 }
2356 
2357 static int
wl_iw_get_retry(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2358 wl_iw_get_retry(
2359 	struct net_device *dev,
2360 	struct iw_request_info *info,
2361 	struct iw_param *vwrq,
2362 	char *extra
2363 )
2364 {
2365 	int error, lrl, srl;
2366 
2367 	WL_TRACE(("%s: SIOCGIWRETRY\n", dev->name));
2368 
2369 	vwrq->disabled = 0;      /* Can't be disabled */
2370 
2371 	/* Do not handle lifetime queries */
2372 	if ((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME)
2373 		return -EINVAL;
2374 
2375 	/* Get retry limits */
2376 	if ((error = dev_wlc_ioctl(dev, WLC_GET_LRL, &lrl, sizeof(lrl))) ||
2377 	    (error = dev_wlc_ioctl(dev, WLC_GET_SRL, &srl, sizeof(srl))))
2378 		return error;
2379 
2380 	lrl = dtoh32(lrl);
2381 	srl = dtoh32(srl);
2382 
2383 	/* Note : by default, display the min retry number */
2384 	if (vwrq->flags & IW_RETRY_MAX) {
2385 		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
2386 		vwrq->value = lrl;
2387 	} else {
2388 		vwrq->flags = IW_RETRY_LIMIT;
2389 		vwrq->value = srl;
2390 		if (srl != lrl)
2391 			vwrq->flags |= IW_RETRY_MIN;
2392 	}
2393 
2394 	return 0;
2395 }
2396 #endif /* WIRELESS_EXT > 10 */
2397 
2398 static int
wl_iw_set_encode(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)2399 wl_iw_set_encode(
2400 	struct net_device *dev,
2401 	struct iw_request_info *info,
2402 	struct iw_point *dwrq,
2403 	char *extra
2404 )
2405 {
2406 	wl_wsec_key_t key;
2407 	int error, val, wsec;
2408 
2409 	WL_TRACE(("%s: SIOCSIWENCODE\n", dev->name));
2410 
2411 	memset(&key, 0, sizeof(key));
2412 
2413 	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
2414 		/* Find the current key */
2415 		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS; key.index++) {
2416 			val = htod32(key.index);
2417 			if ((error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val))))
2418 				return error;
2419 			val = dtoh32(val);
2420 			if (val)
2421 				break;
2422 		}
2423 		/* Default to 0 */
2424 		if (key.index == DOT11_MAX_DEFAULT_KEYS)
2425 			key.index = 0;
2426 	} else {
2427 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2428 		if (key.index >= DOT11_MAX_DEFAULT_KEYS)
2429 			return -EINVAL;
2430 	}
2431 
2432 	/* Interpret "off" to mean no encryption */
2433 	wsec = (dwrq->flags & IW_ENCODE_DISABLED) ? 0 : WEP_ENABLED;
2434 
2435 	if ((error = dev_wlc_intvar_set(dev, "wsec", wsec)))
2436 		return error;
2437 
2438 	/* Old API used to pass a NULL pointer instead of IW_ENCODE_NOKEY */
2439 	if (!extra || !dwrq->length || (dwrq->flags & IW_ENCODE_NOKEY)) {
2440 		/* Just select a new current key */
2441 		val = htod32(key.index);
2442 		if ((error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY, &val, sizeof(val))))
2443 			return error;
2444 	} else {
2445 		key.len = dwrq->length;
2446 
2447 		if (dwrq->length > sizeof(key.data))
2448 			return -EINVAL;
2449 
2450 		memcpy(key.data, extra, dwrq->length);
2451 
2452 		key.flags = WL_PRIMARY_KEY;
2453 		switch (key.len) {
2454 		case WEP1_KEY_SIZE:
2455 			key.algo = CRYPTO_ALGO_WEP1;
2456 			break;
2457 		case WEP128_KEY_SIZE:
2458 			key.algo = CRYPTO_ALGO_WEP128;
2459 			break;
2460 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
2461 		case TKIP_KEY_SIZE:
2462 			key.algo = CRYPTO_ALGO_TKIP;
2463 			break;
2464 #endif
2465 		case AES_KEY_SIZE:
2466 			key.algo = CRYPTO_ALGO_AES_CCM;
2467 			break;
2468 		default:
2469 			return -EINVAL;
2470 		}
2471 
2472 		/* Set the new key/index */
2473 		swap_key_from_BE(&key);
2474 		if ((error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key))))
2475 			return error;
2476 	}
2477 
2478 	/* Interpret "restricted" to mean shared key authentication */
2479 	val = (dwrq->flags & IW_ENCODE_RESTRICTED) ? 1 : 0;
2480 	val = htod32(val);
2481 	if ((error = dev_wlc_ioctl(dev, WLC_SET_AUTH, &val, sizeof(val))))
2482 		return error;
2483 
2484 	return 0;
2485 }
2486 
2487 static int
wl_iw_get_encode(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)2488 wl_iw_get_encode(
2489 	struct net_device *dev,
2490 	struct iw_request_info *info,
2491 	struct iw_point *dwrq,
2492 	char *extra
2493 )
2494 {
2495 	wl_wsec_key_t key;
2496 	int error, val, wsec, auth;
2497 
2498 	WL_TRACE(("%s: SIOCGIWENCODE\n", dev->name));
2499 
2500 	/* assure default values of zero for things we don't touch */
2501 	bzero(&key, sizeof(wl_wsec_key_t));
2502 
2503 	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
2504 		/* Find the current key */
2505 		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS; key.index++) {
2506 			val = key.index;
2507 			if ((error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val, sizeof(val))))
2508 				return error;
2509 			val = dtoh32(val);
2510 			if (val)
2511 				break;
2512 		}
2513 	} else
2514 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2515 
2516 	if (key.index >= DOT11_MAX_DEFAULT_KEYS)
2517 		key.index = 0;
2518 
2519 	/* Get info */
2520 
2521 	if ((error = dev_wlc_ioctl(dev, WLC_GET_WSEC, &wsec, sizeof(wsec))) ||
2522 	    (error = dev_wlc_ioctl(dev, WLC_GET_AUTH, &auth, sizeof(auth))))
2523 		return error;
2524 
2525 	swap_key_to_BE(&key);
2526 
2527 	wsec = dtoh32(wsec);
2528 	auth = dtoh32(auth);
2529 	/* Get key length */
2530 	dwrq->length = MIN(IW_ENCODING_TOKEN_MAX, key.len);
2531 
2532 	/* Get flags */
2533 	dwrq->flags = key.index + 1;
2534 	if (!(wsec & (WEP_ENABLED | TKIP_ENABLED | AES_ENABLED))) {
2535 		/* Interpret "off" to mean no encryption */
2536 		dwrq->flags |= IW_ENCODE_DISABLED;
2537 	}
2538 	if (auth) {
2539 		/* Interpret "restricted" to mean shared key authentication */
2540 		dwrq->flags |= IW_ENCODE_RESTRICTED;
2541 	}
2542 
2543 	/* Get key */
2544 	if (dwrq->length && extra)
2545 		memcpy(extra, key.data, dwrq->length);
2546 
2547 	return 0;
2548 }
2549 
2550 static int
wl_iw_set_power(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2551 wl_iw_set_power(
2552 	struct net_device *dev,
2553 	struct iw_request_info *info,
2554 	struct iw_param *vwrq,
2555 	char *extra
2556 )
2557 {
2558 	int error, pm;
2559 
2560 	WL_TRACE(("%s: SIOCSIWPOWER\n", dev->name));
2561 
2562 	pm = vwrq->disabled ? PM_OFF : PM_MAX;
2563 
2564 	pm = htod32(pm);
2565 	if ((error = dev_wlc_ioctl(dev, WLC_SET_PM, &pm, sizeof(pm))))
2566 		return error;
2567 
2568 	return 0;
2569 }
2570 
2571 static int
wl_iw_get_power(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2572 wl_iw_get_power(
2573 	struct net_device *dev,
2574 	struct iw_request_info *info,
2575 	struct iw_param *vwrq,
2576 	char *extra
2577 )
2578 {
2579 	int error, pm;
2580 
2581 	WL_TRACE(("%s: SIOCGIWPOWER\n", dev->name));
2582 
2583 	if ((error = dev_wlc_ioctl(dev, WLC_GET_PM, &pm, sizeof(pm))))
2584 		return error;
2585 
2586 	pm = dtoh32(pm);
2587 	vwrq->disabled = pm ? 0 : 1;
2588 	vwrq->flags = IW_POWER_ALL_R;
2589 
2590 	return 0;
2591 }
2592 
2593 #if WIRELESS_EXT > 17
2594 static int
wl_iw_set_wpaie(struct net_device * dev,struct iw_request_info * info,struct iw_point * iwp,char * extra)2595 wl_iw_set_wpaie(
2596 	struct net_device *dev,
2597 	struct iw_request_info *info,
2598 	struct iw_point *iwp,
2599 	char *extra
2600 )
2601 {
2602 #if defined(BCMWAPI_WPI)
2603 	uchar buf[WLC_IOCTL_SMLEN] = {0};
2604 	uchar *p = buf;
2605 	int wapi_ie_size;
2606 
2607 	WL_TRACE(("%s: SIOCSIWGENIE\n", dev->name));
2608 
2609 	if (extra[0] == DOT11_MNG_WAPI_ID)
2610 	{
2611 		wapi_ie_size = iwp->length;
2612 		memcpy(p, extra, iwp->length);
2613 		dev_wlc_bufvar_set(dev, "wapiie", buf, wapi_ie_size);
2614 	}
2615 	else
2616 #endif
2617 		dev_wlc_bufvar_set(dev, "wpaie", extra, iwp->length);
2618 
2619 	return 0;
2620 }
2621 
2622 static int
wl_iw_get_wpaie(struct net_device * dev,struct iw_request_info * info,struct iw_point * iwp,char * extra)2623 wl_iw_get_wpaie(
2624 	struct net_device *dev,
2625 	struct iw_request_info *info,
2626 	struct iw_point *iwp,
2627 	char *extra
2628 )
2629 {
2630 	WL_TRACE(("%s: SIOCGIWGENIE\n", dev->name));
2631 	iwp->length = 64;
2632 	dev_wlc_bufvar_get(dev, "wpaie", extra, iwp->length);
2633 	return 0;
2634 }
2635 
2636 static int
wl_iw_set_encodeext(struct net_device * dev,struct iw_request_info * info,struct iw_point * dwrq,char * extra)2637 wl_iw_set_encodeext(
2638 	struct net_device *dev,
2639 	struct iw_request_info *info,
2640 	struct iw_point *dwrq,
2641 	char *extra
2642 )
2643 {
2644 	wl_wsec_key_t key;
2645 	int error;
2646 	struct iw_encode_ext *iwe;
2647 
2648 	WL_TRACE(("%s: SIOCSIWENCODEEXT\n", dev->name));
2649 
2650 	memset(&key, 0, sizeof(key));
2651 	iwe = (struct iw_encode_ext *)extra;
2652 
2653 	/* disable encryption completely  */
2654 	if (dwrq->flags & IW_ENCODE_DISABLED) {
2655 
2656 	}
2657 
2658 	/* get the key index */
2659 	key.index = 0;
2660 	if (dwrq->flags & IW_ENCODE_INDEX)
2661 		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
2662 
2663 	key.len = iwe->key_len;
2664 
2665 	/* Instead of bcast for ea address for default wep keys, driver needs it to be Null */
2666 	if (!ETHER_ISMULTI(iwe->addr.sa_data))
2667 		bcopy((void *)&iwe->addr.sa_data, (char *)&key.ea, ETHER_ADDR_LEN);
2668 
2669 	/* check for key index change */
2670 	if (key.len == 0) {
2671 		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
2672 			WL_WSEC(("Changing the the primary Key to %d\n", key.index));
2673 			/* change the key index .... */
2674 			key.index = htod32(key.index);
2675 			error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY,
2676 				&key.index, sizeof(key.index));
2677 			if (error)
2678 				return error;
2679 		}
2680 		/* key delete */
2681 		else {
2682 			swap_key_from_BE(&key);
2683 			error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
2684 			if (error)
2685 				return error;
2686 		}
2687 	}
2688 	/* This case is used to allow an external 802.1x supplicant
2689 	 * to pass the PMK to the in-driver supplicant for use in
2690 	 * the 4-way handshake.
2691 	 */
2692 	else if (iwe->alg == IW_ENCODE_ALG_PMK) {
2693 		int j;
2694 		wsec_pmk_t pmk;
2695 		char keystring[WSEC_MAX_PSK_LEN + 1];
2696 		char* charptr = keystring;
2697 		uint len;
2698 
2699 		/* copy the raw hex key to the appropriate format */
2700 		for (j = 0; j < (WSEC_MAX_PSK_LEN / 2); j++) {
2701 			(void)snprintf(charptr, 3, "%02x", iwe->key[j]);
2702 			charptr += 2;
2703 		}
2704 		len = strlen(keystring);
2705 		pmk.key_len = htod16(len);
2706 		bcopy(keystring, pmk.key, len);
2707 		pmk.flags = htod16(WSEC_PASSPHRASE);
2708 
2709 		WL_WSEC(("set key %s\n", keystring));
2710 		error = dev_wlc_ioctl(dev, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
2711 		if (error) {
2712 			WL_ERROR(("WLC_SET_WSEC_PMK error %d\n", error));
2713 			return error;
2714 		}
2715 	}
2716 
2717 	else {
2718 		if (iwe->key_len > sizeof(key.data))
2719 			return -EINVAL;
2720 
2721 		WL_WSEC(("Setting the key index %d\n", key.index));
2722 		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
2723 			WL_WSEC(("key is a Primary Key\n"));
2724 			key.flags = WL_PRIMARY_KEY;
2725 		}
2726 
2727 		bcopy((void *)iwe->key, key.data, iwe->key_len);
2728 
2729 		if (iwe->alg == IW_ENCODE_ALG_TKIP) {
2730 			uint8 keybuf[8];
2731 			bcopy(&key.data[24], keybuf, sizeof(keybuf));
2732 			bcopy(&key.data[16], &key.data[24], sizeof(keybuf));
2733 			bcopy(keybuf, &key.data[16], sizeof(keybuf));
2734 		}
2735 
2736 		/* rx iv */
2737 		if (iwe->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
2738 			uchar *ivptr;
2739 			ivptr = (uchar *)iwe->rx_seq;
2740 			key.rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
2741 				(ivptr[3] << 8) | ivptr[2];
2742 			key.rxiv.lo = (ivptr[1] << 8) | ivptr[0];
2743 			key.iv_initialized = TRUE;
2744 		}
2745 
2746 		switch (iwe->alg) {
2747 			case IW_ENCODE_ALG_NONE:
2748 				key.algo = CRYPTO_ALGO_OFF;
2749 				break;
2750 			case IW_ENCODE_ALG_WEP:
2751 				if (iwe->key_len == WEP1_KEY_SIZE)
2752 					key.algo = CRYPTO_ALGO_WEP1;
2753 				else
2754 					key.algo = CRYPTO_ALGO_WEP128;
2755 				break;
2756 			case IW_ENCODE_ALG_TKIP:
2757 				key.algo = CRYPTO_ALGO_TKIP;
2758 				break;
2759 			case IW_ENCODE_ALG_CCMP:
2760 				key.algo = CRYPTO_ALGO_AES_CCM;
2761 				break;
2762 #ifdef BCMWAPI_WPI
2763 			case IW_ENCODE_ALG_SM4:
2764 				key.algo = CRYPTO_ALGO_SMS4;
2765 				if (iwe->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
2766 					key.flags &= ~WL_PRIMARY_KEY;
2767 				}
2768 				break;
2769 #endif
2770 			default:
2771 				break;
2772 		}
2773 		swap_key_from_BE(&key);
2774 
2775 		dhd_wait_pend8021x(dev);
2776 
2777 		error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
2778 		if (error)
2779 			return error;
2780 		wl_ext_in4way_sync_wext(dev, STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY,
2781 			WL_EXT_STATUS_ADD_KEY, NULL);
2782 	}
2783 	return 0;
2784 }
2785 
2786 /* wpa2 pmk list */
2787 static int
wl_iw_set_pmksa(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2788 wl_iw_set_pmksa(
2789 	struct net_device *dev,
2790 	struct iw_request_info *info,
2791 	struct iw_param *vwrq,
2792 	char *extra
2793 )
2794 {
2795 	struct pmk_list *pmk_list = NULL;
2796 	struct iw_pmksa *iwpmksa;
2797 	uint i;
2798 	char eabuf[ETHER_ADDR_STR_LEN];
2799 	pmkid_t *pmkid_array = NULL;
2800 	struct dhd_pub *dhd = dhd_get_pub(dev);
2801 	wl_wext_info_t *wext_info = NULL;
2802 
2803 	WL_TRACE(("%s: SIOCSIWPMKSA\n", dev->name));
2804 	DHD_CHECK(dhd, dev);
2805 	wext_info = dhd->wext_info;
2806  	pmk_list = &wext_info->pmk_list;
2807 	if (pmk_list)
2808 		pmkid_array = pmk_list->pmkids.pmkid;
2809 	iwpmksa = (struct iw_pmksa *)extra;
2810 	bzero((char *)eabuf, ETHER_ADDR_STR_LEN);
2811 	if (iwpmksa->cmd == IW_PMKSA_FLUSH) {
2812 		WL_TRACE(("wl_iw_set_pmksa - IW_PMKSA_FLUSH\n"));
2813 		bzero((char *)pmk_list, sizeof(struct pmk_list));
2814 	}
2815 	if (iwpmksa->cmd == IW_PMKSA_REMOVE) {
2816 		pmkid_list_t pmkid, *pmkidptr;
2817 		pmkidptr = &pmkid;
2818 		bcopy(&iwpmksa->bssid.sa_data[0], &pmkidptr->pmkid[0].BSSID, ETHER_ADDR_LEN);
2819 		bcopy(&iwpmksa->pmkid[0], &pmkidptr->pmkid[0].PMKID, WPA2_PMKID_LEN);
2820 		{
2821 			uint j;
2822 			WL_TRACE(("wl_iw_set_pmksa,IW_PMKSA_REMOVE - PMKID: %s = ",
2823 				bcm_ether_ntoa(&pmkidptr->pmkid[0].BSSID,
2824 				eabuf)));
2825 			for (j = 0; j < WPA2_PMKID_LEN; j++)
2826 				WL_TRACE(("%02x ", pmkidptr->pmkid[0].PMKID[j]));
2827 			WL_TRACE(("\n"));
2828 		}
2829 		for (i = 0; i < pmk_list->pmkids.npmkid; i++)
2830 			if (!bcmp(&iwpmksa->bssid.sa_data[0], &pmkid_array[i].BSSID,
2831 				ETHER_ADDR_LEN))
2832 				break;
2833 		for (; i < pmk_list->pmkids.npmkid; i++) {
2834 			bcopy(&pmkid_array[i+1].BSSID,
2835 				&pmkid_array[i].BSSID,
2836 				ETHER_ADDR_LEN);
2837 			bcopy(&pmkid_array[i+1].PMKID,
2838 				&pmkid_array[i].PMKID,
2839 				WPA2_PMKID_LEN);
2840 		}
2841 		pmk_list->pmkids.npmkid--;
2842 	}
2843 	if (iwpmksa->cmd == IW_PMKSA_ADD) {
2844 		bcopy(&iwpmksa->bssid.sa_data[0],
2845 			&pmkid_array[pmk_list->pmkids.npmkid].BSSID,
2846 			ETHER_ADDR_LEN);
2847 		bcopy(&iwpmksa->pmkid[0], &pmkid_array[pmk_list->pmkids.npmkid].PMKID,
2848 			WPA2_PMKID_LEN);
2849 		{
2850 			uint j;
2851 			uint k;
2852 			k = pmk_list->pmkids.npmkid;
2853 			BCM_REFERENCE(k);
2854 			WL_TRACE(("wl_iw_set_pmksa,IW_PMKSA_ADD - PMKID: %s = ",
2855 				bcm_ether_ntoa(&pmkid_array[k].BSSID,
2856 				eabuf)));
2857 			for (j = 0; j < WPA2_PMKID_LEN; j++)
2858 				WL_TRACE(("%02x ", pmkid_array[k].PMKID[j]));
2859 			WL_TRACE(("\n"));
2860 		}
2861 		pmk_list->pmkids.npmkid++;
2862 	}
2863 	WL_TRACE(("PRINTING pmkid LIST - No of elements %d\n", pmk_list->pmkids.npmkid));
2864 	for (i = 0; i < pmk_list->pmkids.npmkid; i++) {
2865 		uint j;
2866 		WL_TRACE(("PMKID[%d]: %s = ", i,
2867 			bcm_ether_ntoa(&pmkid_array[i].BSSID,
2868 			eabuf)));
2869 		for (j = 0; j < WPA2_PMKID_LEN; j++)
2870 			WL_TRACE(("%02x ", pmkid_array[i].PMKID[j]));
2871 		printf("\n");
2872 	}
2873 	dev_wlc_bufvar_set(dev, "pmkid_info", (char *)pmk_list, sizeof(struct pmk_list));
2874 	return 0;
2875 }
2876 
2877 static int
wl_iw_get_encodeext(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2878 wl_iw_get_encodeext(
2879 	struct net_device *dev,
2880 	struct iw_request_info *info,
2881 	struct iw_param *vwrq,
2882 	char *extra
2883 )
2884 {
2885 	WL_TRACE(("%s: SIOCGIWENCODEEXT\n", dev->name));
2886 	return 0;
2887 }
2888 
2889 static int
wl_iw_set_wpaauth(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)2890 wl_iw_set_wpaauth(
2891 	struct net_device *dev,
2892 	struct iw_request_info *info,
2893 	struct iw_param *vwrq,
2894 	char *extra
2895 )
2896 {
2897 	int error = 0;
2898 	int paramid;
2899 	int paramval;
2900 	uint32 cipher_combined;
2901 	int val = 0;
2902 	wl_iw_t *iw = IW_DEV_IF(dev);
2903 
2904 	WL_TRACE(("%s: SIOCSIWAUTH\n", dev->name));
2905 
2906 	paramid = vwrq->flags & IW_AUTH_INDEX;
2907 	paramval = vwrq->value;
2908 
2909 	WL_TRACE(("%s: SIOCSIWAUTH, paramid = 0x%0x, paramval = 0x%0x\n",
2910 		dev->name, paramid, paramval));
2911 
2912 	switch (paramid) {
2913 
2914 	case IW_AUTH_WPA_VERSION:
2915 		/* supported wpa version disabled or wpa or wpa2 */
2916 		if (paramval & IW_AUTH_WPA_VERSION_DISABLED)
2917 			val = WPA_AUTH_DISABLED;
2918 		else if (paramval & (IW_AUTH_WPA_VERSION_WPA))
2919 			val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
2920 		else if (paramval & IW_AUTH_WPA_VERSION_WPA2)
2921 			val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
2922 #ifdef BCMWAPI_WPI
2923 		else if (paramval & IW_AUTH_WAPI_VERSION_1)
2924 			val = WAPI_AUTH_UNSPECIFIED;
2925 #endif
2926 		WL_TRACE(("%d: setting wpa_auth to 0x%0x\n", __LINE__, val));
2927 		if ((error = dev_wlc_intvar_set(dev, "wpa_auth", val)))
2928 			return error;
2929 		break;
2930 
2931 	case IW_AUTH_CIPHER_PAIRWISE:
2932 	case IW_AUTH_CIPHER_GROUP: {
2933 		int fbt_cap = 0;
2934 
2935 		if (paramid == IW_AUTH_CIPHER_PAIRWISE) {
2936 			iw->pwsec = paramval;
2937 		}
2938 		else {
2939 			iw->gwsec = paramval;
2940 		}
2941 
2942 		if ((error = dev_wlc_intvar_get(dev, "wsec", &val))) {
2943 			WL_ERROR(("wsec error %d\n", error));
2944 			return error;
2945 		}
2946 		WL_WSEC(("get wsec=0x%x\n", val));
2947 
2948 		cipher_combined = iw->gwsec | iw->pwsec;
2949 		val &= ~(WEP_ENABLED | TKIP_ENABLED | AES_ENABLED);
2950 		if (cipher_combined & (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
2951 			val |= WEP_ENABLED;
2952 		if (cipher_combined & IW_AUTH_CIPHER_TKIP)
2953 			val |= TKIP_ENABLED;
2954 		if (cipher_combined & IW_AUTH_CIPHER_CCMP)
2955 			val |= AES_ENABLED;
2956 #ifdef BCMWAPI_WPI
2957 		val &= ~SMS4_ENABLED;
2958 		if (cipher_combined & IW_AUTH_CIPHER_SMS4)
2959 			val |= SMS4_ENABLED;
2960 #endif
2961 
2962 		if (iw->privacy_invoked && !val) {
2963 			WL_WSEC(("%s: 'Privacy invoked' TRUE but clearing wsec, assuming "
2964 			         "we're a WPS enrollee\n", dev->name));
2965 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", TRUE))) {
2966 				WL_WSEC(("Failed to set iovar is_WPS_enrollee\n"));
2967 				return error;
2968 			}
2969 		} else if (val) {
2970 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
2971 				WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
2972 				return error;
2973 			}
2974 		}
2975 
2976 		WL_WSEC(("set wsec=0x%x\n", val));
2977 		if ((error = dev_wlc_intvar_set(dev, "wsec", val))) {
2978 			WL_ERROR(("wsec error %d\n", error));
2979 			return error;
2980 		}
2981 
2982 		/* Ensure in-dongle supplicant is turned on when FBT wants to do the 4-way
2983 		 * handshake.
2984 		 */
2985 		if (dev_wlc_intvar_get(dev, "fbt_cap", &fbt_cap) == 0) {
2986 			WL_WSEC(("get fbt_cap=0x%x\n", fbt_cap));
2987 			if (fbt_cap == WLC_FBT_CAP_DRV_4WAY_AND_REASSOC) {
2988 				if ((paramid == IW_AUTH_CIPHER_PAIRWISE) && (val & AES_ENABLED)) {
2989 					if ((error = dev_wlc_intvar_set(dev, "sup_wpa", 1))) {
2990 						WL_ERROR(("sup_wpa 1 error %d\n", error));
2991 						return error;
2992 					}
2993 				}
2994 				else if (val == 0) {
2995 					if ((error = dev_wlc_intvar_set(dev, "sup_wpa", 0))) {
2996 						WL_ERROR(("sup_wpa 0 error %d\n", error));
2997 						return error;
2998 					}
2999 				}
3000 			}
3001 		}
3002 		break;
3003 	}
3004 
3005 	case IW_AUTH_KEY_MGMT:
3006 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val))) {
3007 			WL_ERROR(("wpa_auth error %d\n", error));
3008 			return error;
3009 		}
3010 		WL_WSEC(("get wpa_auth to %d\n", val));
3011 
3012 		if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
3013 			if (paramval & (IW_AUTH_KEY_MGMT_FT_PSK | IW_AUTH_KEY_MGMT_PSK))
3014 				val = WPA_AUTH_PSK;
3015 			else
3016 				val = WPA_AUTH_UNSPECIFIED;
3017 			if (paramval & (IW_AUTH_KEY_MGMT_FT_802_1X | IW_AUTH_KEY_MGMT_FT_PSK))
3018 				val |= WPA2_AUTH_FT;
3019 		}
3020 		else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
3021 			if (paramval & (IW_AUTH_KEY_MGMT_FT_PSK | IW_AUTH_KEY_MGMT_PSK))
3022 				val = WPA2_AUTH_PSK;
3023 			else
3024 				val = WPA2_AUTH_UNSPECIFIED;
3025 			if (paramval & (IW_AUTH_KEY_MGMT_FT_802_1X | IW_AUTH_KEY_MGMT_FT_PSK))
3026 				val |= WPA2_AUTH_FT;
3027 		}
3028 #ifdef BCMWAPI_WPI
3029 		if (paramval & (IW_AUTH_KEY_MGMT_WAPI_PSK | IW_AUTH_KEY_MGMT_WAPI_CERT))
3030 			val = WAPI_AUTH_UNSPECIFIED;
3031 #endif
3032 		WL_TRACE(("%d: setting wpa_auth to %d\n", __LINE__, val));
3033 		if ((error = dev_wlc_intvar_set(dev, "wpa_auth", val)))
3034 			return error;
3035 		break;
3036 
3037 	case IW_AUTH_TKIP_COUNTERMEASURES:
3038 		dev_wlc_bufvar_set(dev, "tkip_countermeasures", (char *)&paramval, 1);
3039 		break;
3040 
3041 	case IW_AUTH_80211_AUTH_ALG:
3042 		/* open shared */
3043 		WL_MSG(dev->name, "Setting the D11auth %d\n", paramval);
3044 		if (paramval & IW_AUTH_ALG_OPEN_SYSTEM)
3045 			val = 0;
3046 		else if (paramval & IW_AUTH_ALG_SHARED_KEY)
3047 			val = 1;
3048 		else
3049 			error = 1;
3050 		if (!error && (error = dev_wlc_intvar_set(dev, "auth", val)))
3051 			return error;
3052 		break;
3053 
3054 	case IW_AUTH_WPA_ENABLED:
3055 		if (paramval == 0) {
3056 			val = 0;
3057 			WL_TRACE(("%d: setting wpa_auth to %d\n", __LINE__, val));
3058 			error = dev_wlc_intvar_set(dev, "wpa_auth", val);
3059 			return error;
3060 		}
3061 		else {
3062 			/* If WPA is enabled, wpa_auth is set elsewhere */
3063 		}
3064 		break;
3065 
3066 	case IW_AUTH_DROP_UNENCRYPTED:
3067 		dev_wlc_bufvar_set(dev, "wsec_restrict", (char *)&paramval, 1);
3068 		break;
3069 
3070 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3071 		dev_wlc_bufvar_set(dev, "rx_unencrypted_eapol", (char *)&paramval, 1);
3072 		break;
3073 
3074 #if WIRELESS_EXT > 17
3075 
3076 	case IW_AUTH_ROAMING_CONTROL:
3077 		WL_TRACE(("IW_AUTH_ROAMING_CONTROL\n"));
3078 		/* driver control or user space app control */
3079 		break;
3080 
3081 	case IW_AUTH_PRIVACY_INVOKED: {
3082 		int wsec;
3083 
3084 		if (paramval == 0) {
3085 			iw->privacy_invoked = FALSE;
3086 			if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
3087 				WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
3088 				return error;
3089 			}
3090 		} else {
3091 			iw->privacy_invoked = TRUE;
3092 			if ((error = dev_wlc_intvar_get(dev, "wsec", &wsec)))
3093 				return error;
3094 
3095 			if (!WSEC_ENABLED(wsec)) {
3096 				/* if privacy is true, but wsec is false, we are a WPS enrollee */
3097 				if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", TRUE))) {
3098 					WL_WSEC(("Failed to set iovar is_WPS_enrollee\n"));
3099 					return error;
3100 				}
3101 			} else {
3102 				if ((error = dev_wlc_intvar_set(dev, "is_WPS_enrollee", FALSE))) {
3103 					WL_WSEC(("Failed to clear iovar is_WPS_enrollee\n"));
3104 					return error;
3105 				}
3106 			}
3107 		}
3108 		break;
3109 	}
3110 
3111 
3112 #endif /* WIRELESS_EXT > 17 */
3113 
3114 #ifdef BCMWAPI_WPI
3115 
3116 	case IW_AUTH_WAPI_ENABLED:
3117 		if ((error = dev_wlc_intvar_get(dev, "wsec", &val)))
3118 			return error;
3119 		if (paramval) {
3120 			val |= SMS4_ENABLED;
3121 			if ((error = dev_wlc_intvar_set(dev, "wsec", val))) {
3122 				WL_ERROR(("setting wsec to 0x%0x returned error %d\n",
3123 					val, error));
3124 				return error;
3125 			}
3126 			if ((error = dev_wlc_intvar_set(dev, "wpa_auth", WAPI_AUTH_UNSPECIFIED))) {
3127 				WL_ERROR(("setting wpa_auth(%d) returned %d\n",
3128 					WAPI_AUTH_UNSPECIFIED,
3129 					error));
3130 				return error;
3131 			}
3132 		}
3133 
3134 		break;
3135 
3136 #endif /* BCMWAPI_WPI */
3137 
3138 	default:
3139 		break;
3140 	}
3141 	return 0;
3142 }
3143 #define VAL_PSK(_val) (((_val) & WPA_AUTH_PSK) || ((_val) & WPA2_AUTH_PSK))
3144 
3145 static int
wl_iw_get_wpaauth(struct net_device * dev,struct iw_request_info * info,struct iw_param * vwrq,char * extra)3146 wl_iw_get_wpaauth(
3147 	struct net_device *dev,
3148 	struct iw_request_info *info,
3149 	struct iw_param *vwrq,
3150 	char *extra
3151 )
3152 {
3153 	int error;
3154 	int paramid;
3155 	int paramval = 0;
3156 	int val;
3157 	wl_iw_t *iw = IW_DEV_IF(dev);
3158 
3159 	WL_TRACE(("%s: SIOCGIWAUTH\n", dev->name));
3160 
3161 	paramid = vwrq->flags & IW_AUTH_INDEX;
3162 
3163 	switch (paramid) {
3164 	case IW_AUTH_WPA_VERSION:
3165 		/* supported wpa version disabled or wpa or wpa2 */
3166 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3167 			return error;
3168 		if (val & (WPA_AUTH_NONE | WPA_AUTH_DISABLED))
3169 			paramval = IW_AUTH_WPA_VERSION_DISABLED;
3170 		else if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED))
3171 			paramval = IW_AUTH_WPA_VERSION_WPA;
3172 		else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED))
3173 			paramval = IW_AUTH_WPA_VERSION_WPA2;
3174 		break;
3175 
3176 	case IW_AUTH_CIPHER_PAIRWISE:
3177 		paramval = iw->pwsec;
3178 		break;
3179 
3180 	case IW_AUTH_CIPHER_GROUP:
3181 		paramval = iw->gwsec;
3182 		break;
3183 
3184 	case IW_AUTH_KEY_MGMT:
3185 		/* psk, 1x */
3186 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3187 			return error;
3188 		if (VAL_PSK(val))
3189 			paramval = IW_AUTH_KEY_MGMT_PSK;
3190 		else
3191 			paramval = IW_AUTH_KEY_MGMT_802_1X;
3192 
3193 		break;
3194 	case IW_AUTH_TKIP_COUNTERMEASURES:
3195 		dev_wlc_bufvar_get(dev, "tkip_countermeasures", (char *)&paramval, 1);
3196 		break;
3197 
3198 	case IW_AUTH_DROP_UNENCRYPTED:
3199 		dev_wlc_bufvar_get(dev, "wsec_restrict", (char *)&paramval, 1);
3200 		break;
3201 
3202 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
3203 		dev_wlc_bufvar_get(dev, "rx_unencrypted_eapol", (char *)&paramval, 1);
3204 		break;
3205 
3206 	case IW_AUTH_80211_AUTH_ALG:
3207 		/* open, shared, leap */
3208 		if ((error = dev_wlc_intvar_get(dev, "auth", &val)))
3209 			return error;
3210 		if (!val)
3211 			paramval = IW_AUTH_ALG_OPEN_SYSTEM;
3212 		else
3213 			paramval = IW_AUTH_ALG_SHARED_KEY;
3214 		break;
3215 	case IW_AUTH_WPA_ENABLED:
3216 		if ((error = dev_wlc_intvar_get(dev, "wpa_auth", &val)))
3217 			return error;
3218 		if (val)
3219 			paramval = TRUE;
3220 		else
3221 			paramval = FALSE;
3222 		break;
3223 
3224 #if WIRELESS_EXT > 17
3225 
3226 	case IW_AUTH_ROAMING_CONTROL:
3227 		WL_ERROR(("IW_AUTH_ROAMING_CONTROL\n"));
3228 		/* driver control or user space app control */
3229 		break;
3230 
3231 	case IW_AUTH_PRIVACY_INVOKED:
3232 		paramval = iw->privacy_invoked;
3233 		break;
3234 
3235 #endif /* WIRELESS_EXT > 17 */
3236 	}
3237 	vwrq->value = paramval;
3238 	return 0;
3239 }
3240 #endif /* WIRELESS_EXT > 17 */
3241 
3242 static const iw_handler wl_iw_handler[] =
3243 {
3244 	(iw_handler) wl_iw_config_commit,	/* SIOCSIWCOMMIT */
3245 	(iw_handler) wl_iw_get_name,		/* SIOCGIWNAME */
3246 	(iw_handler) NULL,			/* SIOCSIWNWID */
3247 	(iw_handler) NULL,			/* SIOCGIWNWID */
3248 	(iw_handler) wl_iw_set_freq,		/* SIOCSIWFREQ */
3249 	(iw_handler) wl_iw_get_freq,		/* SIOCGIWFREQ */
3250 	(iw_handler) wl_iw_set_mode,		/* SIOCSIWMODE */
3251 	(iw_handler) wl_iw_get_mode,		/* SIOCGIWMODE */
3252 	(iw_handler) NULL,			/* SIOCSIWSENS */
3253 	(iw_handler) NULL,			/* SIOCGIWSENS */
3254 	(iw_handler) NULL,			/* SIOCSIWRANGE */
3255 	(iw_handler) wl_iw_get_range,		/* SIOCGIWRANGE */
3256 	(iw_handler) NULL,			/* SIOCSIWPRIV */
3257 	(iw_handler) NULL,			/* SIOCGIWPRIV */
3258 	(iw_handler) NULL,			/* SIOCSIWSTATS */
3259 	(iw_handler) NULL,			/* SIOCGIWSTATS */
3260 	(iw_handler) wl_iw_set_spy,		/* SIOCSIWSPY */
3261 	(iw_handler) wl_iw_get_spy,		/* SIOCGIWSPY */
3262 	(iw_handler) NULL,			/* -- hole -- */
3263 	(iw_handler) NULL,			/* -- hole -- */
3264 	(iw_handler) wl_iw_set_wap,		/* SIOCSIWAP */
3265 	(iw_handler) wl_iw_get_wap,		/* SIOCGIWAP */
3266 #if WIRELESS_EXT > 17
3267 	(iw_handler) wl_iw_mlme,		/* SIOCSIWMLME */
3268 #else
3269 	(iw_handler) NULL,			/* -- hole -- */
3270 #endif
3271 #ifdef WL_ESCAN
3272 	(iw_handler) NULL,			/* SIOCGIWAPLIST */
3273 #else
3274 	(iw_handler) wl_iw_iscan_get_aplist,	/* SIOCGIWAPLIST */
3275 #endif
3276 #if WIRELESS_EXT > 13
3277 	(iw_handler) wl_iw_iscan_set_scan,	/* SIOCSIWSCAN */
3278 	(iw_handler) wl_iw_iscan_get_scan,	/* SIOCGIWSCAN */
3279 #else	/* WIRELESS_EXT > 13 */
3280 	(iw_handler) NULL,			/* SIOCSIWSCAN */
3281 	(iw_handler) NULL,			/* SIOCGIWSCAN */
3282 #endif	/* WIRELESS_EXT > 13 */
3283 	(iw_handler) wl_iw_set_essid,		/* SIOCSIWESSID */
3284 	(iw_handler) wl_iw_get_essid,		/* SIOCGIWESSID */
3285 	(iw_handler) wl_iw_set_nick,		/* SIOCSIWNICKN */
3286 	(iw_handler) wl_iw_get_nick,		/* SIOCGIWNICKN */
3287 	(iw_handler) NULL,			/* -- hole -- */
3288 	(iw_handler) NULL,			/* -- hole -- */
3289 	(iw_handler) wl_iw_set_rate,		/* SIOCSIWRATE */
3290 	(iw_handler) wl_iw_get_rate,		/* SIOCGIWRATE */
3291 	(iw_handler) wl_iw_set_rts,		/* SIOCSIWRTS */
3292 	(iw_handler) wl_iw_get_rts,		/* SIOCGIWRTS */
3293 	(iw_handler) wl_iw_set_frag,		/* SIOCSIWFRAG */
3294 	(iw_handler) wl_iw_get_frag,		/* SIOCGIWFRAG */
3295 	(iw_handler) wl_iw_set_txpow,		/* SIOCSIWTXPOW */
3296 	(iw_handler) wl_iw_get_txpow,		/* SIOCGIWTXPOW */
3297 #if WIRELESS_EXT > 10
3298 	(iw_handler) wl_iw_set_retry,		/* SIOCSIWRETRY */
3299 	(iw_handler) wl_iw_get_retry,		/* SIOCGIWRETRY */
3300 #endif /* WIRELESS_EXT > 10 */
3301 	(iw_handler) wl_iw_set_encode,		/* SIOCSIWENCODE */
3302 	(iw_handler) wl_iw_get_encode,		/* SIOCGIWENCODE */
3303 	(iw_handler) wl_iw_set_power,		/* SIOCSIWPOWER */
3304 	(iw_handler) wl_iw_get_power,		/* SIOCGIWPOWER */
3305 #if WIRELESS_EXT > 17
3306 	(iw_handler) NULL,			/* -- hole -- */
3307 	(iw_handler) NULL,			/* -- hole -- */
3308 	(iw_handler) wl_iw_set_wpaie,		/* SIOCSIWGENIE */
3309 	(iw_handler) wl_iw_get_wpaie,		/* SIOCGIWGENIE */
3310 	(iw_handler) wl_iw_set_wpaauth,		/* SIOCSIWAUTH */
3311 	(iw_handler) wl_iw_get_wpaauth,		/* SIOCGIWAUTH */
3312 	(iw_handler) wl_iw_set_encodeext,	/* SIOCSIWENCODEEXT */
3313 	(iw_handler) wl_iw_get_encodeext,	/* SIOCGIWENCODEEXT */
3314 	(iw_handler) wl_iw_set_pmksa,		/* SIOCSIWPMKSA */
3315 #endif /* WIRELESS_EXT > 17 */
3316 };
3317 
3318 #if WIRELESS_EXT > 12
3319 enum {
3320 	WL_IW_SET_LEDDC = SIOCIWFIRSTPRIV,
3321 	WL_IW_SET_VLANMODE,
3322 	WL_IW_SET_PM,
3323 	WL_IW_SET_LAST
3324 };
3325 
3326 static iw_handler wl_iw_priv_handler[] = {
3327 	wl_iw_set_leddc,
3328 	wl_iw_set_vlanmode,
3329 	wl_iw_set_pm,
3330 	NULL
3331 };
3332 
3333 static struct iw_priv_args wl_iw_priv_args[] = {
3334 	{
3335 		WL_IW_SET_LEDDC,
3336 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3337 		0,
3338 		"set_leddc"
3339 	},
3340 	{
3341 		WL_IW_SET_VLANMODE,
3342 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3343 		0,
3344 		"set_vlanmode"
3345 	},
3346 	{
3347 		WL_IW_SET_PM,
3348 		IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
3349 		0,
3350 		"set_pm"
3351 	},
3352 	{ 0, 0, 0, { 0 } }
3353 };
3354 
3355 const struct iw_handler_def wl_iw_handler_def =
3356 {
3357 	.num_standard = ARRAYSIZE(wl_iw_handler),
3358 	.num_private = ARRAY_SIZE(wl_iw_priv_handler),
3359 	.num_private_args = ARRAY_SIZE(wl_iw_priv_args),
3360 	.standard = (const iw_handler *) wl_iw_handler,
3361 	.private = wl_iw_priv_handler,
3362 	.private_args = wl_iw_priv_args,
3363 #if WIRELESS_EXT >= 19
3364 	get_wireless_stats: dhd_get_wireless_stats,
3365 #endif /* WIRELESS_EXT >= 19 */
3366 	};
3367 #endif /* WIRELESS_EXT > 12 */
3368 
3369 int
wl_iw_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)3370 wl_iw_ioctl(
3371 	struct net_device *dev,
3372 	struct ifreq *rq,
3373 	int cmd
3374 )
3375 {
3376 	struct iwreq *wrq = (struct iwreq *) rq;
3377 	struct iw_request_info info;
3378 	iw_handler handler;
3379 	char *extra = NULL;
3380 	size_t token_size = 1;
3381 	int max_tokens = 0, ret = 0;
3382 #ifndef WL_ESCAN
3383 	struct dhd_pub *dhd = dhd_get_pub(dev);
3384 	wl_wext_info_t *wext_info = NULL;
3385 	iscan_info_t *iscan;
3386 
3387 	DHD_CHECK(dhd, dev);
3388 	wext_info = dhd->wext_info;
3389 	iscan = &wext_info->iscan;
3390 #endif
3391 
3392 	if (cmd < SIOCIWFIRST ||
3393 		IW_IOCTL_IDX(cmd) >= ARRAYSIZE(wl_iw_handler) ||
3394 		!(handler = wl_iw_handler[IW_IOCTL_IDX(cmd)]))
3395 		return -EOPNOTSUPP;
3396 
3397 	switch (cmd) {
3398 
3399 	case SIOCSIWESSID:
3400 	case SIOCGIWESSID:
3401 	case SIOCSIWNICKN:
3402 	case SIOCGIWNICKN:
3403 		max_tokens = IW_ESSID_MAX_SIZE + 1;
3404 		break;
3405 
3406 	case SIOCSIWENCODE:
3407 	case SIOCGIWENCODE:
3408 #if WIRELESS_EXT > 17
3409 	case SIOCSIWENCODEEXT:
3410 	case SIOCGIWENCODEEXT:
3411 #endif
3412 		max_tokens = IW_ENCODING_TOKEN_MAX;
3413 		break;
3414 
3415 	case SIOCGIWRANGE:
3416 		max_tokens = sizeof(struct iw_range);
3417 		break;
3418 
3419 	case SIOCGIWAPLIST:
3420 		token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality);
3421 		max_tokens = IW_MAX_AP;
3422 		break;
3423 
3424 #if WIRELESS_EXT > 13
3425 	case SIOCGIWSCAN:
3426 #ifndef WL_ESCAN
3427 	if (iscan)
3428 		max_tokens = wrq->u.data.length;
3429 	else
3430 #endif
3431 		max_tokens = IW_SCAN_MAX_DATA;
3432 		break;
3433 #endif /* WIRELESS_EXT > 13 */
3434 
3435 	case SIOCSIWSPY:
3436 		token_size = sizeof(struct sockaddr);
3437 		max_tokens = IW_MAX_SPY;
3438 		break;
3439 
3440 	case SIOCGIWSPY:
3441 		token_size = sizeof(struct sockaddr) + sizeof(struct iw_quality);
3442 		max_tokens = IW_MAX_SPY;
3443 		break;
3444 	default:
3445 		break;
3446 	}
3447 
3448 	if (max_tokens && wrq->u.data.pointer) {
3449 		if (wrq->u.data.length > max_tokens)
3450 			return -E2BIG;
3451 
3452 		if (!(extra = kmalloc(max_tokens * token_size, GFP_KERNEL)))
3453 			return -ENOMEM;
3454 
3455 		if (copy_from_user(extra, wrq->u.data.pointer, wrq->u.data.length * token_size)) {
3456 			kfree(extra);
3457 			return -EFAULT;
3458 		}
3459 	}
3460 
3461 	info.cmd = cmd;
3462 	info.flags = 0;
3463 
3464 	ret = handler(dev, &info, &wrq->u, extra);
3465 
3466 	if (extra) {
3467 		if (copy_to_user(wrq->u.data.pointer, extra, wrq->u.data.length * token_size)) {
3468 			kfree(extra);
3469 			return -EFAULT;
3470 		}
3471 
3472 		kfree(extra);
3473 	}
3474 
3475 	return ret;
3476 }
3477 
3478 /* Convert a connection status event into a connection status string.
3479  * Returns TRUE if a matching connection status string was found.
3480  */
3481 bool
wl_iw_conn_status_str(uint32 event_type,uint32 status,uint32 reason,char * stringBuf,uint buflen)3482 wl_iw_conn_status_str(uint32 event_type, uint32 status, uint32 reason,
3483 	char* stringBuf, uint buflen)
3484 {
3485 	typedef struct conn_fail_event_map_t {
3486 		uint32 inEvent;			/* input: event type to match */
3487 		uint32 inStatus;		/* input: event status code to match */
3488 		uint32 inReason;		/* input: event reason code to match */
3489 		const char* outName;	/* output: failure type */
3490 		const char* outCause;	/* output: failure cause */
3491 	} conn_fail_event_map_t;
3492 
3493 	/* Map of WLC_E events to connection failure strings */
3494 #	define WL_IW_DONT_CARE	9999
3495 	const conn_fail_event_map_t event_map [] = {
3496 		/* inEvent           inStatus                inReason         */
3497 		/* outName outCause                                           */
3498 		{WLC_E_SET_SSID,     WLC_E_STATUS_SUCCESS,   WL_IW_DONT_CARE,
3499 		"Conn", "Success"},
3500 		{WLC_E_SET_SSID,     WLC_E_STATUS_NO_NETWORKS, WL_IW_DONT_CARE,
3501 		"Conn", "NoNetworks"},
3502 		{WLC_E_SET_SSID,     WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3503 		"Conn", "ConfigMismatch"},
3504 		{WLC_E_PRUNE,        WL_IW_DONT_CARE,        WLC_E_PRUNE_ENCR_MISMATCH,
3505 		"Conn", "EncrypMismatch"},
3506 		{WLC_E_PRUNE,        WL_IW_DONT_CARE,        WLC_E_RSN_MISMATCH,
3507 		"Conn", "RsnMismatch"},
3508 		{WLC_E_AUTH,         WLC_E_STATUS_TIMEOUT,   WL_IW_DONT_CARE,
3509 		"Conn", "AuthTimeout"},
3510 		{WLC_E_AUTH,         WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3511 		"Conn", "AuthFail"},
3512 		{WLC_E_AUTH,         WLC_E_STATUS_NO_ACK,    WL_IW_DONT_CARE,
3513 		"Conn", "AuthNoAck"},
3514 		{WLC_E_REASSOC,      WLC_E_STATUS_FAIL,      WL_IW_DONT_CARE,
3515 		"Conn", "ReassocFail"},
3516 		{WLC_E_REASSOC,      WLC_E_STATUS_TIMEOUT,   WL_IW_DONT_CARE,
3517 		"Conn", "ReassocTimeout"},
3518 		{WLC_E_REASSOC,      WLC_E_STATUS_ABORT,     WL_IW_DONT_CARE,
3519 		"Conn", "ReassocAbort"},
3520 		{WLC_E_PSK_SUP,      WLC_SUP_KEYED,          WL_IW_DONT_CARE,
3521 		"Sup", "ConnSuccess"},
3522 		{WLC_E_PSK_SUP,      WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3523 		"Sup", "WpaHandshakeFail"},
3524 		{WLC_E_DEAUTH_IND,   WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3525 		"Conn", "Deauth"},
3526 		{WLC_E_DISASSOC_IND, WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3527 		"Conn", "DisassocInd"},
3528 		{WLC_E_DISASSOC,     WL_IW_DONT_CARE,        WL_IW_DONT_CARE,
3529 		"Conn", "Disassoc"}
3530 	};
3531 
3532 	const char* name = "";
3533 	const char* cause = NULL;
3534 	int i;
3535 
3536 	/* Search the event map table for a matching event */
3537 	for (i = 0;  i < sizeof(event_map)/sizeof(event_map[0]);  i++) {
3538 		const conn_fail_event_map_t* row = &event_map[i];
3539 		if (row->inEvent == event_type &&
3540 		    (row->inStatus == status || row->inStatus == WL_IW_DONT_CARE) &&
3541 		    (row->inReason == reason || row->inReason == WL_IW_DONT_CARE)) {
3542 			name = row->outName;
3543 			cause = row->outCause;
3544 			break;
3545 		}
3546 	}
3547 
3548 	/* If found, generate a connection failure string and return TRUE */
3549 	if (cause) {
3550 		memset(stringBuf, 0, buflen);
3551 		(void)snprintf(stringBuf, buflen, "%s %s %02d %02d", name, cause, status, reason);
3552 		WL_TRACE(("Connection status: %s\n", stringBuf));
3553 		return TRUE;
3554 	} else {
3555 		return FALSE;
3556 	}
3557 }
3558 
3559 #if (WIRELESS_EXT > 14)
3560 /* Check if we have received an event that indicates connection failure
3561  * If so, generate a connection failure report string.
3562  * The caller supplies a buffer to hold the generated string.
3563  */
3564 static bool
wl_iw_check_conn_fail(wl_event_msg_t * e,char * stringBuf,uint buflen)3565 wl_iw_check_conn_fail(wl_event_msg_t *e, char* stringBuf, uint buflen)
3566 {
3567 	uint32 event = ntoh32(e->event_type);
3568 	uint32 status =  ntoh32(e->status);
3569 	uint32 reason =  ntoh32(e->reason);
3570 
3571 	if (wl_iw_conn_status_str(event, status, reason, stringBuf, buflen)) {
3572 		return TRUE;
3573 	} else
3574 	{
3575 		return FALSE;
3576 	}
3577 }
3578 #endif /* WIRELESS_EXT > 14 */
3579 
3580 #ifndef IW_CUSTOM_MAX
3581 #define IW_CUSTOM_MAX 256 /* size of extra buffer used for translation of events */
3582 #endif /* IW_CUSTOM_MAX */
3583 
3584 void
wl_iw_event(struct net_device * dev,struct wl_wext_info * wext_info,wl_event_msg_t * e,void * data)3585 wl_iw_event(struct net_device *dev, struct wl_wext_info *wext_info,
3586 	wl_event_msg_t *e, void* data)
3587 {
3588 #if WIRELESS_EXT > 13
3589 	union iwreq_data wrqu;
3590 	char extra[IW_CUSTOM_MAX + 1];
3591 	int cmd = 0;
3592 	uint32 event_type = ntoh32(e->event_type);
3593 	uint16 flags =  ntoh16(e->flags);
3594 	uint32 datalen = ntoh32(e->datalen);
3595 	uint32 status =  ntoh32(e->status);
3596 	uint32 reason =  ntoh32(e->reason);
3597 #ifndef WL_ESCAN
3598 	iscan_info_t *iscan = &wext_info->iscan;
3599 #endif
3600 
3601 	memset(&wrqu, 0, sizeof(wrqu));
3602 	memset(extra, 0, sizeof(extra));
3603 
3604 	memcpy(wrqu.addr.sa_data, &e->addr, ETHER_ADDR_LEN);
3605 	wrqu.addr.sa_family = ARPHRD_ETHER;
3606 
3607 	switch (event_type) {
3608 	case WLC_E_TXFAIL:
3609 		cmd = IWEVTXDROP;
3610 		break;
3611 #if WIRELESS_EXT > 14
3612 	case WLC_E_JOIN:
3613 	case WLC_E_ASSOC_IND:
3614 	case WLC_E_REASSOC_IND:
3615 		cmd = IWEVREGISTERED;
3616 		break;
3617 	case WLC_E_DEAUTH:
3618 	case WLC_E_DISASSOC:
3619 		wl_ext_in4way_sync_wext(dev,
3620 			STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
3621 			WL_EXT_STATUS_DISCONNECTED, NULL);
3622 		WL_MSG_RLMT(dev->name, &e->addr, ETHER_ADDR_LEN,
3623 			"disconnected with "MACSTR", event %d, reason %d\n",
3624 			MAC2STR((u8 *)wrqu.addr.sa_data), event_type, reason);
3625 		break;
3626 	case WLC_E_DEAUTH_IND:
3627 	case WLC_E_DISASSOC_IND:
3628 		cmd = SIOCGIWAP;
3629 		WL_MSG(dev->name, "disconnected with "MACSTR", event %d, reason %d\n",
3630 			MAC2STR((u8 *)wrqu.addr.sa_data), event_type, reason);
3631 		bzero(wrqu.addr.sa_data, ETHER_ADDR_LEN);
3632 		bzero(&extra, ETHER_ADDR_LEN);
3633 		wl_ext_in4way_sync_wext(dev,
3634 			STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
3635 			WL_EXT_STATUS_DISCONNECTED, NULL);
3636 		break;
3637 
3638 	case WLC_E_LINK:
3639 		cmd = SIOCGIWAP;
3640 		if (!(flags & WLC_EVENT_MSG_LINK)) {
3641 			WL_MSG(dev->name, "Link Down with "MACSTR", reason=%d\n",
3642 				MAC2STR((u8 *)wrqu.addr.sa_data), reason);
3643 			bzero(wrqu.addr.sa_data, ETHER_ADDR_LEN);
3644 			bzero(&extra, ETHER_ADDR_LEN);
3645 			wl_ext_in4way_sync_wext(dev,
3646 				STA_NO_SCAN_IN4WAY|STA_NO_BTC_IN4WAY|STA_WAIT_DISCONNECTED,
3647 				WL_EXT_STATUS_DISCONNECTED, NULL);
3648 		} else {
3649 			WL_MSG(dev->name, "Link UP with "MACSTR"\n",
3650 				MAC2STR((u8 *)wrqu.addr.sa_data));
3651 		}
3652 		break;
3653 	case WLC_E_ACTION_FRAME:
3654 		cmd = IWEVCUSTOM;
3655 		if (datalen + 1 <= sizeof(extra)) {
3656 			wrqu.data.length = datalen + 1;
3657 			extra[0] = WLC_E_ACTION_FRAME;
3658 			memcpy(&extra[1], data, datalen);
3659 			WL_TRACE(("WLC_E_ACTION_FRAME len %d \n", wrqu.data.length));
3660 		}
3661 		break;
3662 
3663 	case WLC_E_ACTION_FRAME_COMPLETE:
3664 		cmd = IWEVCUSTOM;
3665 		if (sizeof(status) + 1 <= sizeof(extra)) {
3666 			wrqu.data.length = sizeof(status) + 1;
3667 			extra[0] = WLC_E_ACTION_FRAME_COMPLETE;
3668 			memcpy(&extra[1], &status, sizeof(status));
3669 			WL_TRACE(("wl_iw_event status %d  \n", status));
3670 		}
3671 		break;
3672 #endif /* WIRELESS_EXT > 14 */
3673 #if WIRELESS_EXT > 17
3674 	case WLC_E_MIC_ERROR: {
3675 		struct	iw_michaelmicfailure  *micerrevt = (struct  iw_michaelmicfailure  *)&extra;
3676 		cmd = IWEVMICHAELMICFAILURE;
3677 		wrqu.data.length = sizeof(struct iw_michaelmicfailure);
3678 		if (flags & WLC_EVENT_MSG_GROUP)
3679 			micerrevt->flags |= IW_MICFAILURE_GROUP;
3680 		else
3681 			micerrevt->flags |= IW_MICFAILURE_PAIRWISE;
3682 		memcpy(micerrevt->src_addr.sa_data, &e->addr, ETHER_ADDR_LEN);
3683 		micerrevt->src_addr.sa_family = ARPHRD_ETHER;
3684 
3685 		break;
3686 	}
3687 
3688 	case WLC_E_ASSOC_REQ_IE:
3689 		cmd = IWEVASSOCREQIE;
3690 		wrqu.data.length = datalen;
3691 		if (datalen < sizeof(extra))
3692 			memcpy(extra, data, datalen);
3693 		break;
3694 
3695 	case WLC_E_ASSOC_RESP_IE:
3696 		cmd = IWEVASSOCRESPIE;
3697 		wrqu.data.length = datalen;
3698 		if (datalen < sizeof(extra))
3699 			memcpy(extra, data, datalen);
3700 		break;
3701 
3702 	case WLC_E_PMKID_CACHE: {
3703 		struct iw_pmkid_cand *iwpmkidcand = (struct iw_pmkid_cand *)&extra;
3704 		pmkid_cand_list_t *pmkcandlist;
3705 		pmkid_cand_t	*pmkidcand;
3706 		int count;
3707 
3708 		if (data == NULL)
3709 			break;
3710 
3711 		cmd = IWEVPMKIDCAND;
3712 		pmkcandlist = data;
3713 		count = ntoh32_ua((uint8 *)&pmkcandlist->npmkid_cand);
3714 		wrqu.data.length = sizeof(struct iw_pmkid_cand);
3715 		pmkidcand = pmkcandlist->pmkid_cand;
3716 		while (count) {
3717 			bzero(iwpmkidcand, sizeof(struct iw_pmkid_cand));
3718 			if (pmkidcand->preauth)
3719 				iwpmkidcand->flags |= IW_PMKID_CAND_PREAUTH;
3720 			bcopy(&pmkidcand->BSSID, &iwpmkidcand->bssid.sa_data,
3721 			      ETHER_ADDR_LEN);
3722 			wireless_send_event(dev, cmd, &wrqu, extra);
3723 			pmkidcand++;
3724 			count--;
3725 		}
3726 		break;
3727 	}
3728 #endif /* WIRELESS_EXT > 17 */
3729 
3730 #ifndef WL_ESCAN
3731 	case WLC_E_SCAN_COMPLETE:
3732 #if WIRELESS_EXT > 14
3733 		cmd = SIOCGIWSCAN;
3734 #endif
3735 		WL_TRACE(("event WLC_E_SCAN_COMPLETE\n"));
3736 		// terence 20150224: fix "wlan0: (WE) : Wireless Event too big (65306)"
3737 		memset(&wrqu, 0, sizeof(wrqu));
3738 		if ((iscan) && (iscan->sysioc_pid >= 0) &&
3739 			(iscan->iscan_state != ISCAN_STATE_IDLE))
3740 			up(&iscan->sysioc_sem);
3741 		break;
3742 #endif
3743 
3744 	default:
3745 		/* Cannot translate event */
3746 		break;
3747 	}
3748 
3749 	if (cmd) {
3750 #ifndef WL_ESCAN
3751 		if (cmd == SIOCGIWSCAN) {
3752 			if ((!iscan) || (iscan->sysioc_pid < 0)) {
3753 				wireless_send_event(dev, cmd, &wrqu, NULL);
3754 			};
3755 		} else
3756 #endif
3757 			wireless_send_event(dev, cmd, &wrqu, extra);
3758 	}
3759 
3760 #if WIRELESS_EXT > 14
3761 	/* Look for WLC events that indicate a connection failure.
3762 	 * If found, generate an IWEVCUSTOM event.
3763 	 */
3764 	memset(extra, 0, sizeof(extra));
3765 	if (wl_iw_check_conn_fail(e, extra, sizeof(extra))) {
3766 		cmd = IWEVCUSTOM;
3767 		wrqu.data.length = strlen(extra);
3768 		wireless_send_event(dev, cmd, &wrqu, extra);
3769 	}
3770 #endif /* WIRELESS_EXT > 14 */
3771 
3772 #endif /* WIRELESS_EXT > 13 */
3773 }
3774 
3775 #ifdef WL_NAN
wl_iw_get_wireless_stats_cbfn(void * ctx,uint8 * data,uint16 type,uint16 len)3776 static int wl_iw_get_wireless_stats_cbfn(void *ctx, uint8 *data, uint16 type, uint16 len)
3777 {
3778 	struct iw_statistics *wstats = ctx;
3779 	int res = BCME_OK;
3780 
3781 	switch (type) {
3782 		case WL_CNT_XTLV_WLC: {
3783 			wl_cnt_wlc_t *cnt = (wl_cnt_wlc_t *)data;
3784 			if (len > sizeof(wl_cnt_wlc_t)) {
3785 				printf("counter structure length invalid! %d > %d\n",
3786 					len, (int)sizeof(wl_cnt_wlc_t));
3787 			}
3788 			wstats->discard.nwid = 0;
3789 			wstats->discard.code = dtoh32(cnt->rxundec);
3790 			wstats->discard.fragment = dtoh32(cnt->rxfragerr);
3791 			wstats->discard.retries = dtoh32(cnt->txfail);
3792 			wstats->discard.misc = dtoh32(cnt->rxrunt) + dtoh32(cnt->rxgiant);
3793 			wstats->miss.beacon = 0;
3794 			WL_TRACE(("wl_iw_get_wireless_stats counters txframe=%d txbyte=%d\n",
3795 				dtoh32(cnt->txframe), dtoh32(cnt->txbyte)));
3796 			WL_TRACE(("wl_iw_get_wireless_stats counters rxundec=%d\n",
3797 				dtoh32(cnt->rxundec)));
3798 			WL_TRACE(("wl_iw_get_wireless_stats counters txfail=%d\n",
3799 				dtoh32(cnt->txfail)));
3800 			WL_TRACE(("wl_iw_get_wireless_stats counters rxfragerr=%d\n",
3801 				dtoh32(cnt->rxfragerr)));
3802 			WL_TRACE(("wl_iw_get_wireless_stats counters rxrunt=%d\n",
3803 				dtoh32(cnt->rxrunt)));
3804 			WL_TRACE(("wl_iw_get_wireless_stats counters rxgiant=%d\n",
3805 				dtoh32(cnt->rxgiant)));
3806 			break;
3807 		}
3808 		case WL_CNT_XTLV_CNTV_LE10_UCODE:
3809 		case WL_CNT_XTLV_LT40_UCODE_V1:
3810 		case WL_CNT_XTLV_GE40_UCODE_V1:
3811 		{
3812 			/* Offsets of rxfrmtoolong and rxbadplcp are the same in
3813 			 * wl_cnt_v_le10_mcst_t, wl_cnt_lt40mcst_v1_t, and wl_cnt_ge40mcst_v1_t.
3814 			 * So we can just cast to wl_cnt_v_le10_mcst_t here.
3815 			 */
3816 			wl_cnt_v_le10_mcst_t *cnt = (wl_cnt_v_le10_mcst_t *)data;
3817 			if (len != WL_CNT_MCST_STRUCT_SZ) {
3818 				printf("counter structure length mismatch! %d != %d\n",
3819 					len, WL_CNT_MCST_STRUCT_SZ);
3820 			}
3821 			WL_TRACE(("wl_iw_get_wireless_stats counters rxfrmtoolong=%d\n",
3822 				dtoh32(cnt->rxfrmtoolong)));
3823 			WL_TRACE(("wl_iw_get_wireless_stats counters rxbadplcp=%d\n",
3824 				dtoh32(cnt->rxbadplcp)));
3825 			BCM_REFERENCE(cnt);
3826 			break;
3827 		}
3828 		default:
3829 			WL_ERROR(("%d: Unsupported type %d\n", __LINE__, type));
3830 			break;
3831 	}
3832 	return res;
3833 }
3834 #endif
3835 
wl_iw_get_wireless_stats(struct net_device * dev,struct iw_statistics * wstats)3836 int wl_iw_get_wireless_stats(struct net_device *dev, struct iw_statistics *wstats)
3837 {
3838 	int res = 0;
3839 	int phy_noise;
3840 	int rssi;
3841 	scb_val_t scb_val;
3842 #if WIRELESS_EXT > 11
3843 	char *cntbuf = NULL;
3844 	wl_cnt_info_t *cntinfo;
3845 	uint16 ver;
3846 	uint32 corerev = 0;
3847 #endif /* WIRELESS_EXT > 11 */
3848 
3849 	phy_noise = 0;
3850 	if ((res = dev_wlc_ioctl(dev, WLC_GET_PHY_NOISE, &phy_noise, sizeof(phy_noise)))) {
3851 		WL_TRACE(("WLC_GET_PHY_NOISE error=%d\n", res));
3852 		goto done;
3853 	}
3854 
3855 	phy_noise = dtoh32(phy_noise);
3856 	WL_TRACE(("wl_iw_get_wireless_stats phy noise=%d\n *****", phy_noise));
3857 
3858 	memset(&scb_val, 0, sizeof(scb_val));
3859 	if ((res = dev_wlc_ioctl(dev, WLC_GET_RSSI, &scb_val, sizeof(scb_val_t)))) {
3860 		WL_TRACE(("WLC_GET_RSSI error=%d\n", res));
3861 		goto done;
3862 	}
3863 
3864 	rssi = dtoh32(scb_val.val);
3865 	rssi = MIN(rssi, RSSI_MAXVAL);
3866 	WL_TRACE(("wl_iw_get_wireless_stats rssi=%d ****** \n", rssi));
3867 	if (rssi <= WL_IW_RSSI_NO_SIGNAL)
3868 		wstats->qual.qual = 0;
3869 	else if (rssi <= WL_IW_RSSI_VERY_LOW)
3870 		wstats->qual.qual = 1;
3871 	else if (rssi <= WL_IW_RSSI_LOW)
3872 		wstats->qual.qual = 2;
3873 	else if (rssi <= WL_IW_RSSI_GOOD)
3874 		wstats->qual.qual = 3;
3875 	else if (rssi <= WL_IW_RSSI_VERY_GOOD)
3876 		wstats->qual.qual = 4;
3877 	else
3878 		wstats->qual.qual = 5;
3879 
3880 	/* Wraps to 0 if RSSI is 0 */
3881 	wstats->qual.level = 0x100 + rssi;
3882 	wstats->qual.noise = 0x100 + phy_noise;
3883 #if WIRELESS_EXT > 18
3884 	wstats->qual.updated |= (IW_QUAL_ALL_UPDATED | IW_QUAL_DBM);
3885 #else
3886 	wstats->qual.updated |= 7;
3887 #endif /* WIRELESS_EXT > 18 */
3888 
3889 #if WIRELESS_EXT > 11
3890 	WL_TRACE(("wl_iw_get_wireless_stats counters\n *****"));
3891 
3892 	cntbuf = kmalloc(MAX_WLIW_IOCTL_LEN, GFP_KERNEL);
3893 	if (!cntbuf) {
3894 		res = BCME_NOMEM;
3895 		goto done;
3896 	}
3897 
3898 	memset(cntbuf, 0, MAX_WLIW_IOCTL_LEN);
3899 	res = dev_wlc_bufvar_get(dev, "counters", cntbuf, MAX_WLIW_IOCTL_LEN);
3900 	if (res)
3901 	{
3902 		WL_ERROR(("wl_iw_get_wireless_stats counters failed error=%d ****** \n", res));
3903 		goto done;
3904 	}
3905 
3906 	cntinfo = (wl_cnt_info_t *)cntbuf;
3907 	cntinfo->version = dtoh16(cntinfo->version);
3908 	cntinfo->datalen = dtoh16(cntinfo->datalen);
3909 	ver = cntinfo->version;
3910 #ifdef WL_NAN
3911 	CHK_CNTBUF_DATALEN(cntbuf, MAX_WLIW_IOCTL_LEN);
3912 #endif
3913 	if (ver > WL_CNT_T_VERSION) {
3914 		WL_TRACE(("\tIncorrect version of counters struct: expected %d; got %d\n",
3915 			WL_CNT_T_VERSION, ver));
3916 		res = BCME_VERSION;
3917 		goto done;
3918 	}
3919 
3920 	if (ver == WL_CNT_VERSION_11) {
3921 		wlc_rev_info_t revinfo;
3922 		memset(&revinfo, 0, sizeof(revinfo));
3923 		res = dev_wlc_ioctl(dev, WLC_GET_REVINFO, &revinfo, sizeof(revinfo));
3924 		if (res) {
3925 			WL_ERROR(("WLC_GET_REVINFO failed %d\n", res));
3926 			goto done;
3927 		}
3928 		corerev = dtoh32(revinfo.corerev);
3929 	}
3930 
3931 #ifdef WL_NAN
3932 	res = wl_cntbuf_to_xtlv_format(NULL, cntinfo, MAX_WLIW_IOCTL_LEN, corerev);
3933 	if (res) {
3934 		WL_ERROR(("wl_cntbuf_to_xtlv_format failed %d\n", res));
3935 		goto done;
3936 	}
3937 
3938 	if ((res = bcm_unpack_xtlv_buf(wstats, cntinfo->data, cntinfo->datalen,
3939 		BCM_XTLV_OPTION_ALIGN32, wl_iw_get_wireless_stats_cbfn))) {
3940 		goto done;
3941 	}
3942 #endif
3943 #endif /* WIRELESS_EXT > 11 */
3944 
3945 done:
3946 #if WIRELESS_EXT > 11
3947 	if (cntbuf) {
3948 		kfree(cntbuf);
3949 	}
3950 #endif /* WIRELESS_EXT > 11 */
3951 	return res;
3952 }
3953 
3954 #ifndef WL_ESCAN
3955 static void
wl_iw_timerfunc(ulong data)3956 wl_iw_timerfunc(ulong data)
3957 {
3958 	iscan_info_t *iscan = (iscan_info_t *)data;
3959 	iscan->timer_on = 0;
3960 	if (iscan->iscan_state != ISCAN_STATE_IDLE) {
3961 		WL_TRACE(("timer trigger\n"));
3962 		up(&iscan->sysioc_sem);
3963 	}
3964 }
3965 
3966 static void
wl_iw_set_event_mask(struct net_device * dev)3967 wl_iw_set_event_mask(struct net_device *dev)
3968 {
3969 	char eventmask[WL_EVENTING_MASK_LEN];
3970 	char iovbuf[WL_EVENTING_MASK_LEN + 12];	/* Room for "event_msgs" + '\0' + bitvec */
3971 
3972 	dev_iw_iovar_getbuf(dev, "event_msgs", "", 0, iovbuf, sizeof(iovbuf));
3973 	bcopy(iovbuf, eventmask, WL_EVENTING_MASK_LEN);
3974 	setbit(eventmask, WLC_E_SCAN_COMPLETE);
3975 	dev_iw_iovar_setbuf(dev, "event_msgs", eventmask, WL_EVENTING_MASK_LEN,
3976 		iovbuf, sizeof(iovbuf));
3977 
3978 }
3979 
3980 static int
wl_iw_iscan_prep(wl_scan_params_t * params,wlc_ssid_t * ssid)3981 wl_iw_iscan_prep(wl_scan_params_t *params, wlc_ssid_t *ssid)
3982 {
3983 	int err = 0;
3984 
3985 	memcpy(&params->bssid, &ether_bcast, ETHER_ADDR_LEN);
3986 	params->bss_type = DOT11_BSSTYPE_ANY;
3987 	params->scan_type = 0;
3988 	params->nprobes = -1;
3989 	params->active_time = -1;
3990 	params->passive_time = -1;
3991 	params->home_time = -1;
3992 	params->channel_num = 0;
3993 
3994 	params->nprobes = htod32(params->nprobes);
3995 	params->active_time = htod32(params->active_time);
3996 	params->passive_time = htod32(params->passive_time);
3997 	params->home_time = htod32(params->home_time);
3998 	if (ssid && ssid->SSID_len)
3999 		memcpy(&params->ssid, ssid, sizeof(wlc_ssid_t));
4000 
4001 	return err;
4002 }
4003 
4004 static int
wl_iw_iscan(iscan_info_t * iscan,wlc_ssid_t * ssid,uint16 action)4005 wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, uint16 action)
4006 {
4007 	int params_size = (WL_SCAN_PARAMS_FIXED_SIZE + OFFSETOF(wl_iscan_params_t, params));
4008 	wl_iscan_params_t *params;
4009 	int err = 0;
4010 
4011 	if (ssid && ssid->SSID_len) {
4012 		params_size += sizeof(wlc_ssid_t);
4013 	}
4014 	params = (wl_iscan_params_t*)kmalloc(params_size, GFP_KERNEL);
4015 	if (params == NULL) {
4016 		return -ENOMEM;
4017 	}
4018 	memset(params, 0, params_size);
4019 	ASSERT(params_size < WLC_IOCTL_SMLEN);
4020 
4021 	err = wl_iw_iscan_prep(&params->params, ssid);
4022 
4023 	if (!err) {
4024 		params->version = htod32(ISCAN_REQ_VERSION);
4025 		params->action = htod16(action);
4026 		params->scan_duration = htod16(0);
4027 
4028 		/* params_size += OFFSETOF(wl_iscan_params_t, params); */
4029 		(void) dev_iw_iovar_setbuf(iscan->dev, "iscan", params, params_size,
4030 			iscan->ioctlbuf, WLC_IOCTL_SMLEN);
4031 	}
4032 
4033 	kfree(params);
4034 	return err;
4035 }
4036 
4037 static uint32
wl_iw_iscan_get(iscan_info_t * iscan)4038 wl_iw_iscan_get(iscan_info_t *iscan)
4039 {
4040 	iscan_buf_t * buf;
4041 	iscan_buf_t * ptr;
4042 	wl_iscan_results_t * list_buf;
4043 	wl_iscan_results_t list;
4044 	wl_scan_results_t *results;
4045 	uint32 status;
4046 
4047 	/* buffers are allocated on demand */
4048 	if (iscan->list_cur) {
4049 		buf = iscan->list_cur;
4050 		iscan->list_cur = buf->next;
4051 	}
4052 	else {
4053 		buf = kmalloc(sizeof(iscan_buf_t), GFP_KERNEL);
4054 		if (!buf)
4055 			return WL_SCAN_RESULTS_ABORTED;
4056 		buf->next = NULL;
4057 		if (!iscan->list_hdr)
4058 			iscan->list_hdr = buf;
4059 		else {
4060 			ptr = iscan->list_hdr;
4061 			while (ptr->next) {
4062 				ptr = ptr->next;
4063 			}
4064 			ptr->next = buf;
4065 		}
4066 	}
4067 	memset(buf->iscan_buf, 0, WLC_IW_ISCAN_MAXLEN);
4068 	list_buf = (wl_iscan_results_t*)buf->iscan_buf;
4069 	results = &list_buf->results;
4070 	results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
4071 	results->version = 0;
4072 	results->count = 0;
4073 
4074 	memset(&list, 0, sizeof(list));
4075 	list.results.buflen = htod32(WLC_IW_ISCAN_MAXLEN);
4076 	(void) dev_iw_iovar_getbuf(
4077 		iscan->dev,
4078 		"iscanresults",
4079 		&list,
4080 		WL_ISCAN_RESULTS_FIXED_SIZE,
4081 		buf->iscan_buf,
4082 		WLC_IW_ISCAN_MAXLEN);
4083 	results->buflen = dtoh32(results->buflen);
4084 	results->version = dtoh32(results->version);
4085 	results->count = dtoh32(results->count);
4086 	WL_TRACE(("results->count = %d\n", results->count));
4087 
4088 	WL_TRACE(("results->buflen = %d\n", results->buflen));
4089 	status = dtoh32(list_buf->status);
4090 	return status;
4091 }
4092 
wl_iw_send_scan_complete(iscan_info_t * iscan)4093 static void wl_iw_send_scan_complete(iscan_info_t *iscan)
4094 {
4095 	union iwreq_data wrqu;
4096 
4097 	memset(&wrqu, 0, sizeof(wrqu));
4098 
4099 	/* wext expects to get no data for SIOCGIWSCAN Event  */
4100 	wireless_send_event(iscan->dev, SIOCGIWSCAN, &wrqu, NULL);
4101 }
4102 
4103 static int
_iscan_sysioc_thread(void * data)4104 _iscan_sysioc_thread(void *data)
4105 {
4106 	uint32 status;
4107 	iscan_info_t *iscan = (iscan_info_t *)data;
4108 
4109 	WL_MSG("wlan", "thread Enter\n");
4110 	DAEMONIZE("iscan_sysioc");
4111 
4112 	status = WL_SCAN_RESULTS_PARTIAL;
4113 	while (down_interruptible(&iscan->sysioc_sem) == 0) {
4114 		if (iscan->timer_on) {
4115 			del_timer(&iscan->timer);
4116 			iscan->timer_on = 0;
4117 		}
4118 
4119 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4120 		rtnl_lock();
4121 #endif
4122 		status = wl_iw_iscan_get(iscan);
4123 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4124 		rtnl_unlock();
4125 #endif
4126 
4127 		switch (status) {
4128 			case WL_SCAN_RESULTS_PARTIAL:
4129 				WL_TRACE(("iscanresults incomplete\n"));
4130 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4131 				rtnl_lock();
4132 #endif
4133 				/* make sure our buffer size is enough before going next round */
4134 				wl_iw_iscan(iscan, NULL, WL_SCAN_ACTION_CONTINUE);
4135 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
4136 				rtnl_unlock();
4137 #endif
4138 				/* Reschedule the timer */
4139 				iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
4140 				add_timer(&iscan->timer);
4141 				iscan->timer_on = 1;
4142 				break;
4143 			case WL_SCAN_RESULTS_SUCCESS:
4144 				WL_TRACE(("iscanresults complete\n"));
4145 				iscan->iscan_state = ISCAN_STATE_IDLE;
4146 				wl_iw_send_scan_complete(iscan);
4147 				break;
4148 			case WL_SCAN_RESULTS_PENDING:
4149 				WL_TRACE(("iscanresults pending\n"));
4150 				/* Reschedule the timer */
4151 				iscan->timer.expires = jiffies + msecs_to_jiffies(iscan->timer_ms);
4152 				add_timer(&iscan->timer);
4153 				iscan->timer_on = 1;
4154 				break;
4155 			case WL_SCAN_RESULTS_ABORTED:
4156 				WL_TRACE(("iscanresults aborted\n"));
4157 				iscan->iscan_state = ISCAN_STATE_IDLE;
4158 				wl_iw_send_scan_complete(iscan);
4159 				break;
4160 			default:
4161 				WL_TRACE(("iscanresults returned unknown status %d\n", status));
4162 				break;
4163 		 }
4164 	}
4165 	WL_MSG("wlan", "was terminated\n");
4166 	complete_and_exit(&iscan->sysioc_exited, 0);
4167 }
4168 #endif /* !WL_ESCAN */
4169 
4170 void
wl_iw_detach(struct net_device * dev,dhd_pub_t * dhdp)4171 wl_iw_detach(struct net_device *dev, dhd_pub_t *dhdp)
4172 {
4173 	wl_wext_info_t *wext_info = dhdp->wext_info;
4174 #ifndef WL_ESCAN
4175 	iscan_buf_t  *buf;
4176 	iscan_info_t *iscan;
4177 #endif
4178 	if (!wext_info)
4179 		return;
4180 
4181 #ifndef WL_ESCAN
4182 	iscan = &wext_info->iscan;
4183 	if (iscan->sysioc_pid >= 0) {
4184 		KILL_PROC(iscan->sysioc_pid, SIGTERM);
4185 		wait_for_completion(&iscan->sysioc_exited);
4186 	}
4187 
4188 	while (iscan->list_hdr) {
4189 		buf = iscan->list_hdr->next;
4190 		kfree(iscan->list_hdr);
4191 		iscan->list_hdr = buf;
4192 	}
4193 #endif
4194 	wl_ext_event_deregister(dev, dhdp, WLC_E_LAST, wl_iw_event);
4195 	if (wext_info) {
4196 		kfree(wext_info);
4197 		dhdp->wext_info = NULL;
4198 	}
4199 }
4200 
4201 int
wl_iw_attach(struct net_device * dev,dhd_pub_t * dhdp)4202 wl_iw_attach(struct net_device *dev, dhd_pub_t *dhdp)
4203 {
4204 	wl_wext_info_t *wext_info = NULL;
4205 	int ret = 0;
4206 #ifndef WL_ESCAN
4207 	iscan_info_t *iscan = NULL;
4208 #endif
4209 
4210 	if (!dev)
4211 		return 0;
4212 	WL_TRACE(("Enter\n"));
4213 
4214 	wext_info = (void *)kzalloc(sizeof(struct wl_wext_info), GFP_KERNEL);
4215 	if (!wext_info)
4216 		return -ENOMEM;
4217 	memset(wext_info, 0, sizeof(wl_wext_info_t));
4218 	wext_info->dev = dev;
4219 	wext_info->dhd = dhdp;
4220 	wext_info->conn_info.bssidx = 0;
4221 	dhdp->wext_info = (void *)wext_info;
4222 
4223 #ifndef WL_ESCAN
4224 	iscan = &wext_info->iscan;
4225 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
4226 	iscan->kthread = NULL;
4227 #endif
4228 	iscan->sysioc_pid = -1;
4229 	/* we only care about main interface so save a global here */
4230 	iscan->dev = dev;
4231 	iscan->iscan_state = ISCAN_STATE_IDLE;
4232 
4233 	/* Set up the timer */
4234 	iscan->timer_ms    = 2000;
4235 	init_timer_compat(&iscan->timer, wl_iw_timerfunc, iscan);
4236 
4237 	sema_init(&iscan->sysioc_sem, 0);
4238 	init_completion(&iscan->sysioc_exited);
4239 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
4240 	iscan->kthread = kthread_run(_iscan_sysioc_thread, iscan, "iscan_sysioc");
4241 	iscan->sysioc_pid = iscan->kthread->pid;
4242 #else
4243 	iscan->sysioc_pid = kernel_thread(_iscan_sysioc_thread, iscan, 0);
4244 #endif
4245 	if (iscan->sysioc_pid < 0) {
4246 		ret = -ENOMEM;
4247 		goto exit;
4248 	}
4249 #endif
4250 	ret = wl_ext_event_register(dev, dhdp, WLC_E_LAST, wl_iw_event, dhdp->wext_info,
4251 		PRIO_EVENT_WEXT);
4252 	if (ret) {
4253 		WL_ERROR(("wl_ext_event_register err %d\n", ret));
4254 		goto exit;
4255 	}
4256 
4257 	return ret;
4258 exit:
4259 	wl_iw_detach(dev, dhdp);
4260 	return ret;
4261 }
4262 
4263 void
wl_iw_down(struct net_device * dev,dhd_pub_t * dhdp)4264 wl_iw_down(struct net_device *dev, dhd_pub_t *dhdp)
4265 {
4266 	wl_wext_info_t *wext_info = NULL;
4267 
4268 	if (dhdp) {
4269 		wext_info = dhdp->wext_info;
4270  	} else {
4271 		WL_ERROR (("dhd is NULL\n"));
4272 		return;
4273 	}
4274 }
4275 
4276 int
wl_iw_up(struct net_device * dev,dhd_pub_t * dhdp)4277 wl_iw_up(struct net_device *dev, dhd_pub_t *dhdp)
4278 {
4279 	wl_wext_info_t *wext_info = NULL;
4280 	int ret = 0;
4281 
4282 	if (dhdp) {
4283 		wext_info = dhdp->wext_info;
4284  	} else {
4285 		WL_ERROR (("dhd is NULL\n"));
4286 		return -ENODEV;
4287 	}
4288 
4289 	return ret;
4290 }
4291 
4292 s32
wl_iw_autochannel(struct net_device * dev,char * command,int total_len)4293 wl_iw_autochannel(struct net_device *dev, char* command, int total_len)
4294 {
4295 	struct dhd_pub *dhd = dhd_get_pub(dev);
4296 	wl_wext_info_t *wext_info = NULL;
4297 	int ret = 0;
4298 #ifdef WL_ESCAN
4299 	int bytes_written = -1;
4300 #endif
4301 
4302 	DHD_CHECK(dhd, dev);
4303 	wext_info = dhd->wext_info;
4304 #ifdef WL_ESCAN
4305 	sscanf(command, "%*s %d", &dhd->escan->autochannel);
4306 	if (dhd->escan->autochannel == 0) {
4307 		dhd->escan->best_2g_ch = 0;
4308 		dhd->escan->best_5g_ch = 0;
4309 	} else if (dhd->escan->autochannel == 2) {
4310 		bytes_written = snprintf(command, total_len, "2g=%d 5g=%d",
4311 			dhd->escan->best_2g_ch, dhd->escan->best_5g_ch);
4312 		WL_TRACE(("command result is %s\n", command));
4313 		ret = bytes_written;
4314 	}
4315 #endif
4316 
4317 	return ret;
4318 }
4319 
4320 #endif /* USE_IW */
4321