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 "hardware_legacy/driver_nl80211.h"
14#include "wpa_supplicant_i.h"
15#include "config.h"
16#ifdef ANDROID
17#include "android_drv.h"
18#endif
19
20#define MAX_WPSP2PIE_CMD_SIZE		512
21
22typedef struct android_wifi_priv_cmd {
23	char *buf;
24	int used_len;
25	int total_len;
26} android_wifi_priv_cmd;
27
28static int drv_errors = 0;
29
30static 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
39static 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
60int 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 (os_strcasecmp(cmd, "STOP") == 0) {
70		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
71		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
72	} else if (os_strcasecmp(cmd, "START") == 0) {
73		linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
74		wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
75	} else if (os_strcasecmp(cmd, "MACADDR") == 0) {
76		u8 macaddr[ETH_ALEN] = {};
77
78		ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, macaddr);
79		if (!ret)
80			ret = os_snprintf(buf, buf_len,
81					  "Macaddr = " MACSTR "\n", MAC2STR(macaddr));
82	} else { /* Use private command */
83		memset(&ifr, 0, sizeof(ifr));
84		memset(&priv_cmd, 0, sizeof(priv_cmd));
85		os_memcpy(buf, cmd, strlen(cmd) + 1);
86		os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
87
88		priv_cmd.buf = buf;
89		priv_cmd.used_len = buf_len;
90		priv_cmd.total_len = buf_len;
91		ifr.ifr_data = &priv_cmd;
92
93		if ((ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr)) < 0) {
94			wpa_printf(MSG_ERROR, "%s: failed to issue private commands\n", __func__);
95		} else {
96			drv_errors = 0;
97			ret = 0;
98			if ((os_strcasecmp(cmd, "LINKSPEED") == 0) ||
99			    (os_strcasecmp(cmd, "RSSI") == 0) ||
100			    (os_strcasecmp(cmd, "GETBAND") == 0) )
101				ret = strlen(buf);
102			else if (os_strcasecmp(cmd, "P2P_DEV_ADDR") == 0)
103				wpa_printf(MSG_DEBUG, "%s: P2P: Device address ("MACSTR")",
104					__func__, MAC2STR(buf));
105			else if (os_strcasecmp(cmd, "P2P_SET_PS") == 0)
106				wpa_printf(MSG_DEBUG, "%s: P2P: %s ", __func__, buf);
107			else if (os_strcasecmp(cmd, "P2P_SET_NOA") == 0)
108				wpa_printf(MSG_DEBUG, "%s: P2P: %s ", __func__, buf);
109			else
110				wpa_printf(MSG_DEBUG, "%s %s len = %d, %d", __func__, buf, ret, strlen(buf));
111			wpa_driver_notify_country_change(drv->ctx, cmd);
112		}
113	}
114	return ret;
115}
116
117int wpa_driver_set_p2p_noa(void *priv, u8 count, int start, int duration)
118{
119	char buf[MAX_DRV_CMD_SIZE];
120
121	memset(buf, 0, sizeof(buf));
122	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
123	snprintf(buf, sizeof(buf), "P2P_SET_NOA %d %d %d", count, start, duration);
124	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf)+1);
125}
126
127int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len)
128{
129	/* Return 0 till we handle p2p_presence request completely in the driver */
130	return 0;
131}
132
133int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow)
134{
135	char buf[MAX_DRV_CMD_SIZE];
136
137	memset(buf, 0, sizeof(buf));
138	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
139	snprintf(buf, sizeof(buf), "P2P_SET_PS %d %d %d", legacy_ps, opp_ps, ctwindow);
140	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
141}
142
143int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
144				 const struct wpabuf *proberesp,
145				 const struct wpabuf *assocresp)
146{
147	char buf[MAX_WPSP2PIE_CMD_SIZE];
148	struct wpabuf *ap_wps_p2p_ie = NULL;
149	char *_cmd = "SET_AP_WPS_P2P_IE";
150	char *pbuf;
151	int ret = 0;
152	int i;
153	struct cmd_desc {
154		int cmd;
155		const struct wpabuf *src;
156	} cmd_arr[] = {
157		{0x1, beacon},
158		{0x2, proberesp},
159		{0x4, assocresp},
160		{-1, NULL}
161	};
162
163	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
164	for (i = 0; cmd_arr[i].cmd != -1; i++) {
165		os_memset(buf, 0, sizeof(buf));
166		pbuf = buf;
167		pbuf += sprintf(pbuf, "%s %d", _cmd, cmd_arr[i].cmd);
168		*pbuf++ = '\0';
169		ap_wps_p2p_ie = cmd_arr[i].src ?
170			wpabuf_dup(cmd_arr[i].src) : NULL;
171		if (ap_wps_p2p_ie) {
172			os_memcpy(pbuf, wpabuf_head(ap_wps_p2p_ie), wpabuf_len(ap_wps_p2p_ie));
173			ret = wpa_driver_nl80211_driver_cmd(priv, buf, buf,
174				strlen(_cmd) + 3 + wpabuf_len(ap_wps_p2p_ie));
175			wpabuf_free(ap_wps_p2p_ie);
176			if (ret < 0)
177				break;
178		}
179	}
180
181	return ret;
182}
183