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