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#define WPA_PS_ENABLED		0
28#define WPA_PS_DISABLED		1
29#define UNUSED(x)	(void)(x)
30
31
32/* Return type for setBand*/
33enum {
34	SEND_CHANNEL_CHANGE_EVENT = 0,
35	DO_NOT_SEND_CHANNEL_CHANGE_EVENT,
36};
37
38typedef struct android_wifi_priv_cmd {
39	char *buf;
40	int used_len;
41	int total_len;
42} android_wifi_priv_cmd;
43
44static int drv_errors = 0;
45
46static void wpa_driver_notify_country_change(void *ctx, char *cmd)
47{
48	if ((os_strncasecmp(cmd, "COUNTRY", 7) == 0) ||
49	    (os_strncasecmp(cmd, "SETBAND", 7) == 0)) {
50		union wpa_event_data event;
51
52		os_memset(&event, 0, sizeof(event));
53		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
54		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
55			event.channel_list_changed.type = REGDOM_TYPE_COUNTRY;
56			if (os_strlen(cmd) > 9) {
57				event.channel_list_changed.alpha2[0] = cmd[8];
58				event.channel_list_changed.alpha2[1] = cmd[9];
59			}
60		} else {
61			event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
62		}
63		wpa_supplicant_event(ctx, EVENT_CHANNEL_LIST_CHANGED, &event);
64	}
65}
66
67int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
68				  size_t buf_len )
69{
70	struct i802_bss *bss = priv;
71	struct wpa_driver_nl80211_data *drv = bss->drv;
72	struct wpa_driver_nl80211_data *driver;
73	struct ifreq ifr;
74	android_wifi_priv_cmd priv_cmd;
75	int ret = 0;
76
77	if (os_strcasecmp(cmd, "START") == 0) {
78		dl_list_for_each(driver, &drv->global->interfaces, struct wpa_driver_nl80211_data, list) {
79			linux_set_iface_flags(drv->global->ioctl_sock, driver->first_bss->ifname, 1);
80			wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
81		}
82	} else if (os_strcasecmp(cmd, "MACADDR") == 0) {
83		u8 macaddr[ETH_ALEN] = {};
84
85		ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname, macaddr);
86		if (!ret)
87			ret = os_snprintf(buf, buf_len,
88					  "Macaddr = " MACSTR "\n", MAC2STR(macaddr));
89	} else { /* Use private command */
90		memset(&ifr, 0, sizeof(ifr));
91		memset(&priv_cmd, 0, sizeof(priv_cmd));
92		os_memcpy(buf, cmd, strlen(cmd) + 1);
93		os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
94
95		priv_cmd.buf = buf;
96		priv_cmd.used_len = buf_len;
97		priv_cmd.total_len = buf_len;
98		ifr.ifr_data = &priv_cmd;
99
100		if ((ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr)) < 0) {
101			wpa_printf(MSG_ERROR, "%s: failed to issue private commands\n", __func__);
102		} else {
103			drv_errors = 0;
104			if((os_strncasecmp(cmd, "SETBAND", 7) == 0) &&
105				ret == DO_NOT_SEND_CHANNEL_CHANGE_EVENT) {
106				return 0;
107			}
108
109			ret = 0;
110			if ((os_strcasecmp(cmd, "LINKSPEED") == 0) ||
111			    (os_strcasecmp(cmd, "RSSI") == 0) ||
112			    (os_strcasecmp(cmd, "GETBAND") == 0) )
113				ret = strlen(buf);
114			else if (os_strcasecmp(cmd, "P2P_DEV_ADDR") == 0)
115				wpa_printf(MSG_DEBUG, "%s: P2P: Device address ("MACSTR")",
116					__func__, MAC2STR(buf));
117			else if (os_strcasecmp(cmd, "P2P_SET_PS") == 0)
118				wpa_printf(MSG_DEBUG, "%s: P2P: %s ", __func__, buf);
119			else if (os_strcasecmp(cmd, "P2P_SET_NOA") == 0)
120				wpa_printf(MSG_DEBUG, "%s: P2P: %s ", __func__, buf);
121			else if (os_strcasecmp(cmd, "STOP") == 0) {
122				wpa_printf(MSG_DEBUG, "%s: %s ", __func__, buf);
123				dl_list_for_each(driver, &drv->global->interfaces, struct wpa_driver_nl80211_data, list) {
124					linux_set_iface_flags(drv->global->ioctl_sock, driver->first_bss->ifname, 0);
125					wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
126				}
127			}
128			else
129				wpa_printf(MSG_DEBUG, "%s %s len = %d, %zu", __func__, buf, ret, buf_len);
130			wpa_driver_notify_country_change(drv->ctx, cmd);
131		}
132	}
133	return ret;
134}
135
136int 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
146int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len)
147{
148	UNUSED(priv), UNUSED(buf), UNUSED(len);
149	/* Return 0 till we handle p2p_presence request completely in the driver */
150	return 0;
151}
152
153int wpa_driver_set_p2p_ps(void *priv, int legacy_ps, int opp_ps, int ctwindow)
154{
155	char buf[MAX_DRV_CMD_SIZE];
156
157	memset(buf, 0, sizeof(buf));
158	wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
159	snprintf(buf, sizeof(buf), "P2P_SET_PS %d %d %d", legacy_ps, opp_ps, ctwindow);
160	return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
161}
162
163int wpa_driver_set_ap_wps_p2p_ie(void *priv, const struct wpabuf *beacon,
164				 const struct wpabuf *proberesp,
165				 const struct wpabuf *assocresp)
166{
167	UNUSED(priv), UNUSED(beacon), UNUSED(proberesp), UNUSED(assocresp);
168	return 0;
169}
170