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