1 /*
2  * Driver interaction with extended Linux CFG8021
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Alternatively, this software may be distributed under the terms of BSD
9  * license.
10  *
11  */
12 
13 #include <sys/cdefs.h>
14 #include "hardware_legacy/driver_nl80211.h"
15 #include "wpa_supplicant_i.h"
16 #include "config.h"
17 #ifdef ANDROID
18 #include "android_drv.h"
19 #endif
20 
21 
22 typedef struct android_wifi_priv_cmd {
23 #ifdef BCMDHD_64_BIT_IPC
24 	u64 bufaddr;
25 #else
26 	char *bufaddr;
27 #endif
28 	int used_len;
29 	int total_len;
30 } android_wifi_priv_cmd;
31 
32 static int drv_errors = 0;
33 
wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data * drv)34 static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
35 {
36 	drv_errors++;
37 	if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
38 		drv_errors = 0;
39 		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
40 	}
41 }
42 
wpa_driver_notify_country_change(void * ctx,char * cmd)43 static void wpa_driver_notify_country_change(void *ctx, char *cmd)
44 {
45 	if ((os_strncasecmp(cmd, "COUNTRY", 7) == 0) ||
46 	    (os_strncasecmp(cmd, "SETBAND", 7) == 0)) {
47 		union wpa_event_data event;
48 
49 		os_memset(&event, 0, sizeof(event));
50 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
51 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
52 			event.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
53 			if (os_strlen(cmd) > 9) {
54 				event.channel_list_changed.alpha2[0] = cmd[8];
55 				event.channel_list_changed.alpha2[1] = cmd[9];
56 			}
57 		} else {
58 			event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
59 		}
60 		wpa_supplicant_event(ctx, EVENT_CHANNEL_LIST_CHANGED, &event);
61 	}
62 }
63 
wpa_driver_nl80211_driver_cmd(void * priv,char * cmd,char * buf,size_t buf_len)64 int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
65 				  size_t buf_len )
66 {
67 	struct i802_bss *bss = priv;
68 	struct wpa_driver_nl80211_data *drv = bss->drv;
69 	struct ifreq ifr;
70 	android_wifi_priv_cmd priv_cmd;
71 	int ret = 0;
72 
73 	if (bss->ifindex <= 0 && bss->wdev_id > 0) {
74 		/* DRIVER CMD received on the DEDICATED P2P Interface which doesn't
75 		 * have an NETDEVICE associated with it. So we have to re-route the
76 		 * command to the parent NETDEVICE
77 		 */
78 		struct wpa_supplicant *wpa_s = (struct wpa_supplicant *)(drv->ctx);
79 
80 		wpa_printf(MSG_DEBUG, "Re-routing DRIVER cmd to parent iface");
81 		if (wpa_s && wpa_s->parent) {
82 			/* Update the nl80211 pointers corresponding to parent iface */
83 			bss = wpa_s->parent->drv_priv;
84 			drv = bss->drv;
85 			wpa_printf(MSG_DEBUG, "Re-routing command to iface: %s"
86 					      " cmd (%s)", bss->ifname, cmd);
87 		}
88 	}
89 
90 	if (os_strcasecmp(cmd, "STOP") == 0) {
91 		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
92 		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
93 	} else if (os_strcasecmp(cmd, "START") == 0) {
94 		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
95 		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
96 	} else if (os_strcasecmp(cmd, "MACADDR") == 0) {
97 		u8 macaddr[ETH_ALEN] = {};
98 
99 		ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, macaddr);
100 		if (!ret)
101 			ret = os_snprintf(buf, buf_len,
102 					  "Macaddr = " MACSTR "\n", MAC2STR(macaddr));
103 	} else { /* Use private command */
104 		os_memcpy(buf, cmd, strlen(cmd) + 1);
105 		memset(&ifr, 0, sizeof(ifr));
106 		memset(&priv_cmd, 0, sizeof(priv_cmd));
107 		os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
108 
109 #ifdef BCMDHD_64_BIT_IPC
110 		priv_cmd.bufaddr = (u64)(uintptr_t)buf;
111 #else
112 		priv_cmd.bufaddr = buf;
113 #endif
114 		priv_cmd.used_len = buf_len;
115 		priv_cmd.total_len = buf_len;
116 		ifr.ifr_data = &priv_cmd;
117 
118 		if ((ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr)) < 0) {
119 			wpa_printf(MSG_ERROR, "%s: failed to issue private command: %s", __func__, cmd);
120 			wpa_driver_send_hang_msg(drv);
121 		} else {
122 			drv_errors = 0;
123 			ret = 0;
124 			if ((os_strcasecmp(cmd, "LINKSPEED") == 0) ||
125 			    (os_strcasecmp(cmd, "RSSI") == 0) ||
126 			    (os_strcasecmp(cmd, "GETBAND") == 0) ||
127 			    (os_strncasecmp(cmd, "WLS_BATCHING", 12) == 0))
128 				ret = strlen(buf);
129 			wpa_driver_notify_country_change(drv->ctx, cmd);
130 			wpa_printf(MSG_DEBUG, "%s %s len = %d, %zu", __func__, buf, ret, strlen(buf));
131 		}
132 	}
133 	return ret;
134 }
135 
wpa_driver_set_p2p_noa(void * priv,u8 count,int start,int duration)136 int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration)
137 {
138 	char buf[MAX_DRV_CMD_SIZE];
139 
140 	memset(buf, 0, sizeof(buf));
141 	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
142 	snprintf(buf, sizeof(buf), "P2P_SET_NOA %d %d %d", count, start, duration);
143 	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf)+1);
144 }
145 
wpa_driver_get_p2p_noa(void * priv __unused,u8 * buf __unused,size_t len __unused)146 int wpa_driver_get_p2p_noa(void *priv __unused, u8 *buf __unused, size_t len __unused)
147 {
148 	/* Return 0 till we handle p2p_presence request completely in the driver */
149 	return 0;
150 }
151 
wpa_driver_set_p2p_ps(void * priv,int legacy_ps,int opp_ps,int ctwindow)152 int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow)
153 {
154 	char buf[MAX_DRV_CMD_SIZE];
155 
156 	memset(buf, 0, sizeof(buf));
157 	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
158 	snprintf(buf, sizeof(buf), "P2P_SET_PS %d %d %d", legacy_ps, opp_ps, ctwindow);
159 	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
160 }
161 
wpa_driver_set_ap_wps_p2p_ie(void * priv,const struct wpabuf * beacon,const struct wpabuf * proberesp,const struct wpabuf * assocresp)162 int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
163 				 const struct wpabuf *proberesp,
164 				 const struct wpabuf *assocresp)
165 {
166 	char *buf;
167 	const struct wpabuf *ap_wps_p2p_ie = NULL;
168 
169 	char *_cmd = "SET_AP_WPS_P2P_IE";
170 	char *pbuf;
171 	int ret = 0;
172 	int i, buf_len;
173 	struct cmd_desc {
174 		int cmd;
175 		const struct wpabuf *src;
176 	} cmd_arr[] = {
177 		{0x1, beacon},
178 		{0x2, proberesp},
179 		{0x4, assocresp},
180 		{-1, NULL}
181 	};
182 
183 	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
184 	for (i = 0; cmd_arr[i].cmd != -1; i++) {
185 		ap_wps_p2p_ie = cmd_arr[i].src;
186 		if (ap_wps_p2p_ie) {
187 			buf_len = strlen(_cmd) + 3 + wpabuf_len(ap_wps_p2p_ie);
188 			buf = os_zalloc(buf_len);
189 			if (NULL == buf) {
190 				wpa_printf(MSG_ERROR, "%s: Out of memory",
191 					   __func__);
192 				ret = -1;
193 				break;
194 			}
195 		} else {
196 			continue;
197 		}
198 		pbuf = buf;
199 		pbuf += snprintf(pbuf, buf_len - wpabuf_len(ap_wps_p2p_ie),
200 				 "%s %d",_cmd, cmd_arr[i].cmd);
201 		*pbuf++ = '\0';
202 		os_memcpy(pbuf, wpabuf_head(ap_wps_p2p_ie), wpabuf_len(ap_wps_p2p_ie));
203 		ret = wpa_driver_nl80211_driver_cmd(priv, buf, buf, buf_len);
204 		os_free(buf);
205 		if (ret < 0)
206 			break;
207 	}
208 
209 	return ret;
210 }
211