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
27typedef 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
37static int drv_errors = 0;
38
39static 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
48static 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
69int 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
141int 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
151int 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
157int 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
167int 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        return 0;
172}
173