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