1206b73d0SCy Schubert /*
2206b73d0SCy Schubert  * Driver interaction with Linux Host AP driver
3206b73d0SCy Schubert  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4206b73d0SCy Schubert  *
5206b73d0SCy Schubert  * This software may be distributed under the terms of the BSD license.
6206b73d0SCy Schubert  * See README for more details.
7206b73d0SCy Schubert  */
8206b73d0SCy Schubert 
9206b73d0SCy Schubert #include "includes.h"
10206b73d0SCy Schubert #include <sys/ioctl.h>
11206b73d0SCy Schubert 
12206b73d0SCy Schubert #include "linux_wext.h"
13206b73d0SCy Schubert #include "common.h"
14206b73d0SCy Schubert #include "driver.h"
15206b73d0SCy Schubert #include "driver_wext.h"
16206b73d0SCy Schubert #include "eloop.h"
17206b73d0SCy Schubert #include "driver_hostap.h"
18206b73d0SCy Schubert 
19206b73d0SCy Schubert 
20206b73d0SCy Schubert #include <net/if_arp.h>
21206b73d0SCy Schubert #include <netpacket/packet.h>
22206b73d0SCy Schubert 
23206b73d0SCy Schubert #include "priv_netlink.h"
24206b73d0SCy Schubert #include "netlink.h"
25206b73d0SCy Schubert #include "linux_ioctl.h"
26206b73d0SCy Schubert #include "common/ieee802_11_defs.h"
27206b73d0SCy Schubert #include "common/ieee802_11_common.h"
28206b73d0SCy Schubert 
29206b73d0SCy Schubert 
30206b73d0SCy Schubert /* MTU to be set for the wlan#ap device; this is mainly needed for IEEE 802.1X
31206b73d0SCy Schubert  * frames that might be longer than normal default MTU and they are not
32206b73d0SCy Schubert  * fragmented */
33206b73d0SCy Schubert #define HOSTAPD_MTU 2290
34206b73d0SCy Schubert 
35206b73d0SCy Schubert static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
36206b73d0SCy Schubert 
37206b73d0SCy Schubert struct hostap_driver_data {
38206b73d0SCy Schubert 	struct hostapd_data *hapd;
39206b73d0SCy Schubert 
40206b73d0SCy Schubert 	char iface[IFNAMSIZ + 1];
41206b73d0SCy Schubert 	int sock; /* raw packet socket for driver access */
42206b73d0SCy Schubert 	int ioctl_sock; /* socket for ioctl() use */
43206b73d0SCy Schubert 	struct netlink_data *netlink;
44206b73d0SCy Schubert 
45206b73d0SCy Schubert 	int we_version;
46206b73d0SCy Schubert 
47206b73d0SCy Schubert 	u8 *generic_ie;
48206b73d0SCy Schubert 	size_t generic_ie_len;
49206b73d0SCy Schubert 	u8 *wps_ie;
50206b73d0SCy Schubert 	size_t wps_ie_len;
51206b73d0SCy Schubert };
52206b73d0SCy Schubert 
53206b73d0SCy Schubert 
54206b73d0SCy Schubert static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
55206b73d0SCy Schubert 			 int len);
56206b73d0SCy Schubert static int hostap_set_iface_flags(void *priv, int dev_up);
57206b73d0SCy Schubert 
handle_data(struct hostap_driver_data * drv,u8 * buf,size_t len,u16 stype)58206b73d0SCy Schubert static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
59206b73d0SCy Schubert 			u16 stype)
60206b73d0SCy Schubert {
61206b73d0SCy Schubert 	struct ieee80211_hdr *hdr;
62206b73d0SCy Schubert 	u16 fc, ethertype;
63206b73d0SCy Schubert 	u8 *pos, *sa;
64206b73d0SCy Schubert 	size_t left;
65206b73d0SCy Schubert 	union wpa_event_data event;
66206b73d0SCy Schubert 
67206b73d0SCy Schubert 	if (len < sizeof(struct ieee80211_hdr))
68206b73d0SCy Schubert 		return;
69206b73d0SCy Schubert 
70206b73d0SCy Schubert 	hdr = (struct ieee80211_hdr *) buf;
71206b73d0SCy Schubert 	fc = le_to_host16(hdr->frame_control);
72206b73d0SCy Schubert 
73206b73d0SCy Schubert 	if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
74206b73d0SCy Schubert 		printf("Not ToDS data frame (fc=0x%04x)\n", fc);
75206b73d0SCy Schubert 		return;
76206b73d0SCy Schubert 	}
77206b73d0SCy Schubert 
78206b73d0SCy Schubert 	sa = hdr->addr2;
79206b73d0SCy Schubert 	os_memset(&event, 0, sizeof(event));
80206b73d0SCy Schubert 	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
81206b73d0SCy Schubert 	event.rx_from_unknown.addr = sa;
82206b73d0SCy Schubert 	wpa_supplicant_event(drv->hapd, EVENT_RX_FROM_UNKNOWN, &event);
83206b73d0SCy Schubert 
84206b73d0SCy Schubert 	pos = (u8 *) (hdr + 1);
85206b73d0SCy Schubert 	left = len - sizeof(*hdr);
86206b73d0SCy Schubert 
87206b73d0SCy Schubert 	if (left < sizeof(rfc1042_header)) {
88206b73d0SCy Schubert 		printf("Too short data frame\n");
89206b73d0SCy Schubert 		return;
90206b73d0SCy Schubert 	}
91206b73d0SCy Schubert 
92206b73d0SCy Schubert 	if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
93206b73d0SCy Schubert 		printf("Data frame with no RFC1042 header\n");
94206b73d0SCy Schubert 		return;
95206b73d0SCy Schubert 	}
96206b73d0SCy Schubert 	pos += sizeof(rfc1042_header);
97206b73d0SCy Schubert 	left -= sizeof(rfc1042_header);
98206b73d0SCy Schubert 
99206b73d0SCy Schubert 	if (left < 2) {
100206b73d0SCy Schubert 		printf("No ethertype in data frame\n");
101206b73d0SCy Schubert 		return;
102206b73d0SCy Schubert 	}
103206b73d0SCy Schubert 
104206b73d0SCy Schubert 	ethertype = WPA_GET_BE16(pos);
105206b73d0SCy Schubert 	pos += 2;
106206b73d0SCy Schubert 	left -= 2;
107206b73d0SCy Schubert 	switch (ethertype) {
108206b73d0SCy Schubert 	case ETH_P_PAE:
109206b73d0SCy Schubert 		drv_event_eapol_rx(drv->hapd, sa, pos, left);
110206b73d0SCy Schubert 		break;
111206b73d0SCy Schubert 
112206b73d0SCy Schubert 	default:
113206b73d0SCy Schubert 		printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
114206b73d0SCy Schubert 		break;
115206b73d0SCy Schubert 	}
116206b73d0SCy Schubert }
117206b73d0SCy Schubert 
118206b73d0SCy Schubert 
handle_tx_callback(struct hostap_driver_data * drv,u8 * buf,size_t len,int ok)119206b73d0SCy Schubert static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
120206b73d0SCy Schubert 			       size_t len, int ok)
121206b73d0SCy Schubert {
122206b73d0SCy Schubert 	struct ieee80211_hdr *hdr;
123206b73d0SCy Schubert 	u16 fc;
124206b73d0SCy Schubert 	union wpa_event_data event;
125206b73d0SCy Schubert 
126206b73d0SCy Schubert 	hdr = (struct ieee80211_hdr *) buf;
127206b73d0SCy Schubert 	fc = le_to_host16(hdr->frame_control);
128206b73d0SCy Schubert 
129206b73d0SCy Schubert 	os_memset(&event, 0, sizeof(event));
130206b73d0SCy Schubert 	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
131206b73d0SCy Schubert 	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
132206b73d0SCy Schubert 	event.tx_status.dst = hdr->addr1;
133206b73d0SCy Schubert 	event.tx_status.data = buf;
134206b73d0SCy Schubert 	event.tx_status.data_len = len;
135206b73d0SCy Schubert 	event.tx_status.ack = ok;
136206b73d0SCy Schubert 	wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
137206b73d0SCy Schubert }
138206b73d0SCy Schubert 
139206b73d0SCy Schubert 
handle_frame(struct hostap_driver_data * drv,u8 * buf,size_t len)140206b73d0SCy Schubert static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
141206b73d0SCy Schubert {
142206b73d0SCy Schubert 	struct ieee80211_hdr *hdr;
143206b73d0SCy Schubert 	u16 fc, type, stype;
144206b73d0SCy Schubert 	size_t data_len = len;
145206b73d0SCy Schubert 	int ver;
146206b73d0SCy Schubert 	union wpa_event_data event;
147206b73d0SCy Schubert 
148206b73d0SCy Schubert 	/* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
149206b73d0SCy Schubert 	 * these to user space */
150206b73d0SCy Schubert 	if (len < 24) {
151206b73d0SCy Schubert 		wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
152206b73d0SCy Schubert 			   (unsigned long) len);
153206b73d0SCy Schubert 		return;
154206b73d0SCy Schubert 	}
155206b73d0SCy Schubert 
156206b73d0SCy Schubert 	hdr = (struct ieee80211_hdr *) buf;
157206b73d0SCy Schubert 	fc = le_to_host16(hdr->frame_control);
158206b73d0SCy Schubert 	type = WLAN_FC_GET_TYPE(fc);
159206b73d0SCy Schubert 	stype = WLAN_FC_GET_STYPE(fc);
160206b73d0SCy Schubert 
161206b73d0SCy Schubert 	if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
162206b73d0SCy Schubert 		wpa_hexdump(MSG_MSGDUMP, "Received management frame",
163206b73d0SCy Schubert 			    buf, len);
164206b73d0SCy Schubert 	}
165206b73d0SCy Schubert 
166206b73d0SCy Schubert 	ver = fc & WLAN_FC_PVER;
167206b73d0SCy Schubert 
168206b73d0SCy Schubert 	/* protocol version 2 is reserved for indicating ACKed frame (TX
169206b73d0SCy Schubert 	 * callbacks), and version 1 for indicating failed frame (no ACK, TX
170206b73d0SCy Schubert 	 * callbacks) */
171206b73d0SCy Schubert 	if (ver == 1 || ver == 2) {
172206b73d0SCy Schubert 		handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
173206b73d0SCy Schubert 		return;
174206b73d0SCy Schubert 	} else if (ver != 0) {
175206b73d0SCy Schubert 		printf("unknown protocol version %d\n", ver);
176206b73d0SCy Schubert 		return;
177206b73d0SCy Schubert 	}
178206b73d0SCy Schubert 
179206b73d0SCy Schubert 	switch (type) {
180206b73d0SCy Schubert 	case WLAN_FC_TYPE_MGMT:
181206b73d0SCy Schubert 		os_memset(&event, 0, sizeof(event));
182206b73d0SCy Schubert 		event.rx_mgmt.frame = buf;
183206b73d0SCy Schubert 		event.rx_mgmt.frame_len = data_len;
184206b73d0SCy Schubert 		wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
185206b73d0SCy Schubert 		break;
186206b73d0SCy Schubert 	case WLAN_FC_TYPE_CTRL:
187206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "CTRL");
188206b73d0SCy Schubert 		break;
189206b73d0SCy Schubert 	case WLAN_FC_TYPE_DATA:
190206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "DATA");
191206b73d0SCy Schubert 		handle_data(drv, buf, data_len, stype);
192206b73d0SCy Schubert 		break;
193206b73d0SCy Schubert 	default:
194206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
195206b73d0SCy Schubert 		break;
196206b73d0SCy Schubert 	}
197206b73d0SCy Schubert }
198206b73d0SCy Schubert 
199206b73d0SCy Schubert 
handle_read(int sock,void * eloop_ctx,void * sock_ctx)200206b73d0SCy Schubert static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
201206b73d0SCy Schubert {
202206b73d0SCy Schubert 	struct hostap_driver_data *drv = eloop_ctx;
203206b73d0SCy Schubert 	int len;
204206b73d0SCy Schubert 	unsigned char buf[3000];
205206b73d0SCy Schubert 
206206b73d0SCy Schubert 	len = recv(sock, buf, sizeof(buf), 0);
207206b73d0SCy Schubert 	if (len < 0) {
208206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "recv: %s", strerror(errno));
209206b73d0SCy Schubert 		return;
210206b73d0SCy Schubert 	}
211206b73d0SCy Schubert 
212206b73d0SCy Schubert 	handle_frame(drv, buf, len);
213206b73d0SCy Schubert }
214206b73d0SCy Schubert 
215206b73d0SCy Schubert 
hostap_init_sockets(struct hostap_driver_data * drv,u8 * own_addr)216206b73d0SCy Schubert static int hostap_init_sockets(struct hostap_driver_data *drv, u8 *own_addr)
217206b73d0SCy Schubert {
218206b73d0SCy Schubert 	struct ifreq ifr;
219206b73d0SCy Schubert 	struct sockaddr_ll addr;
220206b73d0SCy Schubert 
221206b73d0SCy Schubert 	drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
222206b73d0SCy Schubert 	if (drv->sock < 0) {
223206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "socket[PF_PACKET,SOCK_RAW]: %s",
224206b73d0SCy Schubert 			   strerror(errno));
225206b73d0SCy Schubert 		return -1;
226206b73d0SCy Schubert 	}
227206b73d0SCy Schubert 
228206b73d0SCy Schubert 	if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
229206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "Could not register read socket");
230206b73d0SCy Schubert 		return -1;
231206b73d0SCy Schubert 	}
232206b73d0SCy Schubert 
233206b73d0SCy Schubert         memset(&ifr, 0, sizeof(ifr));
234206b73d0SCy Schubert 	if (os_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap",
235206b73d0SCy Schubert 			drv->iface) >= (int) sizeof(ifr.ifr_name)) {
236206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "hostap: AP interface name truncated");
237206b73d0SCy Schubert 		return -1;
238206b73d0SCy Schubert 	}
239206b73d0SCy Schubert         if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
240206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl(SIOCGIFINDEX): %s",
241206b73d0SCy Schubert 			   strerror(errno));
242206b73d0SCy Schubert 		return -1;
243206b73d0SCy Schubert         }
244206b73d0SCy Schubert 
245206b73d0SCy Schubert 	if (hostap_set_iface_flags(drv, 1)) {
246206b73d0SCy Schubert 		return -1;
247206b73d0SCy Schubert 	}
248206b73d0SCy Schubert 
249206b73d0SCy Schubert 	memset(&addr, 0, sizeof(addr));
250206b73d0SCy Schubert 	addr.sll_family = AF_PACKET;
251206b73d0SCy Schubert 	addr.sll_ifindex = ifr.ifr_ifindex;
252206b73d0SCy Schubert 	wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
253206b73d0SCy Schubert 		   addr.sll_ifindex);
254206b73d0SCy Schubert 
255206b73d0SCy Schubert 	if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
256206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "bind: %s", strerror(errno));
257206b73d0SCy Schubert 		return -1;
258206b73d0SCy Schubert 	}
259206b73d0SCy Schubert 
260206b73d0SCy Schubert 	return linux_get_ifhwaddr(drv->sock, drv->iface, own_addr);
261206b73d0SCy Schubert }
262206b73d0SCy Schubert 
263206b73d0SCy Schubert 
hostap_send_mlme(void * priv,const u8 * msg,size_t len,int noack,unsigned int freq,const u16 * csa_offs,size_t csa_offs_len,int no_encrypt,unsigned int wait)264206b73d0SCy Schubert static int hostap_send_mlme(void *priv, const u8 *msg, size_t len, int noack,
265206b73d0SCy Schubert 			    unsigned int freq,
266*c1d255d3SCy Schubert 			    const u16 *csa_offs, size_t csa_offs_len,
267*c1d255d3SCy Schubert 			    int no_encrypt, unsigned int wait)
268206b73d0SCy Schubert {
269206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
270206b73d0SCy Schubert 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
271206b73d0SCy Schubert 	int res;
272206b73d0SCy Schubert 
273206b73d0SCy Schubert 	/* Request TX callback */
274206b73d0SCy Schubert 	hdr->frame_control |= host_to_le16(BIT(1));
275206b73d0SCy Schubert 	res = send(drv->sock, msg, len, 0);
276206b73d0SCy Schubert 	hdr->frame_control &= ~host_to_le16(BIT(1));
277206b73d0SCy Schubert 
278206b73d0SCy Schubert 	return res;
279206b73d0SCy Schubert }
280206b73d0SCy Schubert 
281206b73d0SCy Schubert 
hostap_send_eapol(void * priv,const u8 * addr,const u8 * data,size_t data_len,int encrypt,const u8 * own_addr,u32 flags)282206b73d0SCy Schubert static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
283206b73d0SCy Schubert 			     size_t data_len, int encrypt, const u8 *own_addr,
284206b73d0SCy Schubert 			     u32 flags)
285206b73d0SCy Schubert {
286206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
287206b73d0SCy Schubert 	struct ieee80211_hdr *hdr;
288206b73d0SCy Schubert 	size_t len;
289206b73d0SCy Schubert 	u8 *pos;
290206b73d0SCy Schubert 	int res;
291206b73d0SCy Schubert 
292206b73d0SCy Schubert 	len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
293206b73d0SCy Schubert 	hdr = os_zalloc(len);
294206b73d0SCy Schubert 	if (hdr == NULL) {
295206b73d0SCy Schubert 		printf("malloc() failed for hostapd_send_data(len=%lu)\n",
296206b73d0SCy Schubert 		       (unsigned long) len);
297206b73d0SCy Schubert 		return -1;
298206b73d0SCy Schubert 	}
299206b73d0SCy Schubert 
300206b73d0SCy Schubert 	hdr->frame_control =
301206b73d0SCy Schubert 		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
302206b73d0SCy Schubert 	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
303206b73d0SCy Schubert 	if (encrypt)
304206b73d0SCy Schubert 		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
305206b73d0SCy Schubert 	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
306206b73d0SCy Schubert 	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
307206b73d0SCy Schubert 	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
308206b73d0SCy Schubert 
309206b73d0SCy Schubert 	pos = (u8 *) (hdr + 1);
310206b73d0SCy Schubert 	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
311206b73d0SCy Schubert 	pos += sizeof(rfc1042_header);
312206b73d0SCy Schubert 	*((u16 *) pos) = htons(ETH_P_PAE);
313206b73d0SCy Schubert 	pos += 2;
314206b73d0SCy Schubert 	memcpy(pos, data, data_len);
315206b73d0SCy Schubert 
316*c1d255d3SCy Schubert 	res = hostap_send_mlme(drv, (u8 *) hdr, len, 0, 0, NULL, 0, 0, 0);
317206b73d0SCy Schubert 	if (res < 0) {
318206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
319206b73d0SCy Schubert 			   "failed: %d (%s)",
320206b73d0SCy Schubert 			   (unsigned long) len, errno, strerror(errno));
321206b73d0SCy Schubert 	}
322206b73d0SCy Schubert 	os_free(hdr);
323206b73d0SCy Schubert 
324206b73d0SCy Schubert 	return res;
325206b73d0SCy Schubert }
326206b73d0SCy Schubert 
327206b73d0SCy Schubert 
hostap_sta_set_flags(void * priv,const u8 * addr,unsigned int total_flags,unsigned int flags_or,unsigned int flags_and)328206b73d0SCy Schubert static int hostap_sta_set_flags(void *priv, const u8 *addr,
329206b73d0SCy Schubert 				unsigned int total_flags, unsigned int flags_or,
330206b73d0SCy Schubert 				unsigned int flags_and)
331206b73d0SCy Schubert {
332206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
333206b73d0SCy Schubert 	struct prism2_hostapd_param param;
334206b73d0SCy Schubert 
335206b73d0SCy Schubert 	if (flags_or & WPA_STA_AUTHORIZED)
336206b73d0SCy Schubert 		flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
337206b73d0SCy Schubert 	if (!(flags_and & WPA_STA_AUTHORIZED))
338206b73d0SCy Schubert 		flags_and = ~BIT(5);
339206b73d0SCy Schubert 	else
340206b73d0SCy Schubert 		flags_and = ~0;
341206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
342206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
343206b73d0SCy Schubert 	memcpy(param.sta_addr, addr, ETH_ALEN);
344206b73d0SCy Schubert 	param.u.set_flags_sta.flags_or = flags_or;
345206b73d0SCy Schubert 	param.u.set_flags_sta.flags_and = flags_and;
346206b73d0SCy Schubert 	return hostapd_ioctl(drv, &param, sizeof(param));
347206b73d0SCy Schubert }
348206b73d0SCy Schubert 
349206b73d0SCy Schubert 
hostap_set_iface_flags(void * priv,int dev_up)350206b73d0SCy Schubert static int hostap_set_iface_flags(void *priv, int dev_up)
351206b73d0SCy Schubert {
352206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
353206b73d0SCy Schubert 	struct ifreq ifr;
354206b73d0SCy Schubert 	char ifname[IFNAMSIZ];
355206b73d0SCy Schubert 
356206b73d0SCy Schubert 	if (os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface) >= IFNAMSIZ) {
357206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "hostap: AP interface name truncated");
358206b73d0SCy Schubert 		return -1;
359206b73d0SCy Schubert 	}
360206b73d0SCy Schubert 	if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
361206b73d0SCy Schubert 		return -1;
362206b73d0SCy Schubert 
363206b73d0SCy Schubert 	if (dev_up) {
364206b73d0SCy Schubert 		memset(&ifr, 0, sizeof(ifr));
365206b73d0SCy Schubert 		os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
366206b73d0SCy Schubert 		ifr.ifr_mtu = HOSTAPD_MTU;
367206b73d0SCy Schubert 		if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
368206b73d0SCy Schubert 			wpa_printf(MSG_INFO,
369206b73d0SCy Schubert 				   "Setting MTU failed - trying to survive with current value: ioctl[SIOCSIFMTU]: %s",
370206b73d0SCy Schubert 				   strerror(errno));
371206b73d0SCy Schubert 		}
372206b73d0SCy Schubert 	}
373206b73d0SCy Schubert 
374206b73d0SCy Schubert 	return 0;
375206b73d0SCy Schubert }
376206b73d0SCy Schubert 
377206b73d0SCy Schubert 
hostapd_ioctl(void * priv,struct prism2_hostapd_param * param,int len)378206b73d0SCy Schubert static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
379206b73d0SCy Schubert 			 int len)
380206b73d0SCy Schubert {
381206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
382206b73d0SCy Schubert 	struct iwreq iwr;
383206b73d0SCy Schubert 
384206b73d0SCy Schubert 	memset(&iwr, 0, sizeof(iwr));
385206b73d0SCy Schubert 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
386206b73d0SCy Schubert 	iwr.u.data.pointer = (caddr_t) param;
387206b73d0SCy Schubert 	iwr.u.data.length = len;
388206b73d0SCy Schubert 
389206b73d0SCy Schubert 	if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
390206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_HOSTAPD]: %s",
391206b73d0SCy Schubert 			   strerror(errno));
392206b73d0SCy Schubert 		return -1;
393206b73d0SCy Schubert 	}
394206b73d0SCy Schubert 
395206b73d0SCy Schubert 	return 0;
396206b73d0SCy Schubert }
397206b73d0SCy Schubert 
398206b73d0SCy Schubert 
wpa_driver_hostap_set_key(void * priv,struct wpa_driver_set_key_params * params)399*c1d255d3SCy Schubert static int wpa_driver_hostap_set_key(void *priv,
400*c1d255d3SCy Schubert 				     struct wpa_driver_set_key_params *params)
401206b73d0SCy Schubert {
402206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
403206b73d0SCy Schubert 	struct prism2_hostapd_param *param;
404206b73d0SCy Schubert 	u8 *buf;
405206b73d0SCy Schubert 	size_t blen;
406206b73d0SCy Schubert 	int ret = 0;
407*c1d255d3SCy Schubert 	enum wpa_alg alg = params->alg;
408*c1d255d3SCy Schubert 	const u8 *addr = params->addr;
409*c1d255d3SCy Schubert 	int key_idx = params->key_idx;
410*c1d255d3SCy Schubert 	int set_tx = params->set_tx;
411*c1d255d3SCy Schubert 	const u8 *key = params->key;
412*c1d255d3SCy Schubert 	size_t key_len = params->key_len;
413206b73d0SCy Schubert 
414206b73d0SCy Schubert 	blen = sizeof(*param) + key_len;
415206b73d0SCy Schubert 	buf = os_zalloc(blen);
416206b73d0SCy Schubert 	if (buf == NULL)
417206b73d0SCy Schubert 		return -1;
418206b73d0SCy Schubert 
419206b73d0SCy Schubert 	param = (struct prism2_hostapd_param *) buf;
420206b73d0SCy Schubert 	param->cmd = PRISM2_SET_ENCRYPTION;
421206b73d0SCy Schubert 	if (addr == NULL)
422206b73d0SCy Schubert 		memset(param->sta_addr, 0xff, ETH_ALEN);
423206b73d0SCy Schubert 	else
424206b73d0SCy Schubert 		memcpy(param->sta_addr, addr, ETH_ALEN);
425206b73d0SCy Schubert 	switch (alg) {
426206b73d0SCy Schubert 	case WPA_ALG_NONE:
427206b73d0SCy Schubert 		os_strlcpy((char *) param->u.crypt.alg, "NONE",
428206b73d0SCy Schubert 			   HOSTAP_CRYPT_ALG_NAME_LEN);
429206b73d0SCy Schubert 		break;
430206b73d0SCy Schubert 	case WPA_ALG_WEP:
431206b73d0SCy Schubert 		os_strlcpy((char *) param->u.crypt.alg, "WEP",
432206b73d0SCy Schubert 			   HOSTAP_CRYPT_ALG_NAME_LEN);
433206b73d0SCy Schubert 		break;
434206b73d0SCy Schubert 	case WPA_ALG_TKIP:
435206b73d0SCy Schubert 		os_strlcpy((char *) param->u.crypt.alg, "TKIP",
436206b73d0SCy Schubert 			   HOSTAP_CRYPT_ALG_NAME_LEN);
437206b73d0SCy Schubert 		break;
438206b73d0SCy Schubert 	case WPA_ALG_CCMP:
439206b73d0SCy Schubert 		os_strlcpy((char *) param->u.crypt.alg, "CCMP",
440206b73d0SCy Schubert 			   HOSTAP_CRYPT_ALG_NAME_LEN);
441206b73d0SCy Schubert 		break;
442206b73d0SCy Schubert 	default:
443206b73d0SCy Schubert 		os_free(buf);
444206b73d0SCy Schubert 		return -1;
445206b73d0SCy Schubert 	}
446206b73d0SCy Schubert 	param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
447206b73d0SCy Schubert 	param->u.crypt.idx = key_idx;
448206b73d0SCy Schubert 	param->u.crypt.key_len = key_len;
449206b73d0SCy Schubert 	memcpy((u8 *) (param + 1), key, key_len);
450206b73d0SCy Schubert 
451206b73d0SCy Schubert 	if (hostapd_ioctl(drv, param, blen)) {
452206b73d0SCy Schubert 		printf("Failed to set encryption.\n");
453206b73d0SCy Schubert 		ret = -1;
454206b73d0SCy Schubert 	}
455206b73d0SCy Schubert 	free(buf);
456206b73d0SCy Schubert 
457206b73d0SCy Schubert 	return ret;
458206b73d0SCy Schubert }
459206b73d0SCy Schubert 
460206b73d0SCy Schubert 
hostap_get_seqnum(const char * ifname,void * priv,const u8 * addr,int idx,u8 * seq)461206b73d0SCy Schubert static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
462206b73d0SCy Schubert 			     int idx, u8 *seq)
463206b73d0SCy Schubert {
464206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
465206b73d0SCy Schubert 	struct prism2_hostapd_param *param;
466206b73d0SCy Schubert 	u8 *buf;
467206b73d0SCy Schubert 	size_t blen;
468206b73d0SCy Schubert 	int ret = 0;
469206b73d0SCy Schubert 
470206b73d0SCy Schubert 	blen = sizeof(*param) + 32;
471206b73d0SCy Schubert 	buf = os_zalloc(blen);
472206b73d0SCy Schubert 	if (buf == NULL)
473206b73d0SCy Schubert 		return -1;
474206b73d0SCy Schubert 
475206b73d0SCy Schubert 	param = (struct prism2_hostapd_param *) buf;
476206b73d0SCy Schubert 	param->cmd = PRISM2_GET_ENCRYPTION;
477206b73d0SCy Schubert 	if (addr == NULL)
478206b73d0SCy Schubert 		os_memset(param->sta_addr, 0xff, ETH_ALEN);
479206b73d0SCy Schubert 	else
480206b73d0SCy Schubert 		os_memcpy(param->sta_addr, addr, ETH_ALEN);
481206b73d0SCy Schubert 	param->u.crypt.idx = idx;
482206b73d0SCy Schubert 
483206b73d0SCy Schubert 	if (hostapd_ioctl(drv, param, blen)) {
484206b73d0SCy Schubert 		printf("Failed to get encryption.\n");
485206b73d0SCy Schubert 		ret = -1;
486206b73d0SCy Schubert 	} else {
487206b73d0SCy Schubert 		os_memcpy(seq, param->u.crypt.seq, 8);
488206b73d0SCy Schubert 	}
489206b73d0SCy Schubert 	os_free(buf);
490206b73d0SCy Schubert 
491206b73d0SCy Schubert 	return ret;
492206b73d0SCy Schubert }
493206b73d0SCy Schubert 
494206b73d0SCy Schubert 
hostap_ioctl_prism2param(void * priv,int param,int value)495206b73d0SCy Schubert static int hostap_ioctl_prism2param(void *priv, int param, int value)
496206b73d0SCy Schubert {
497206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
498206b73d0SCy Schubert 	struct iwreq iwr;
499206b73d0SCy Schubert 	int *i;
500206b73d0SCy Schubert 
501206b73d0SCy Schubert 	memset(&iwr, 0, sizeof(iwr));
502206b73d0SCy Schubert 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
503206b73d0SCy Schubert 	i = (int *) iwr.u.name;
504206b73d0SCy Schubert 	*i++ = param;
505206b73d0SCy Schubert 	*i++ = value;
506206b73d0SCy Schubert 
507206b73d0SCy Schubert 	if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
508206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_PRISM2_PARAM]: %s",
509206b73d0SCy Schubert 			   strerror(errno));
510206b73d0SCy Schubert 		return -1;
511206b73d0SCy Schubert 	}
512206b73d0SCy Schubert 
513206b73d0SCy Schubert 	return 0;
514206b73d0SCy Schubert }
515206b73d0SCy Schubert 
516206b73d0SCy Schubert 
hostap_set_ieee8021x(void * priv,struct wpa_bss_params * params)517206b73d0SCy Schubert static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
518206b73d0SCy Schubert {
519206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
520206b73d0SCy Schubert 	int enabled = params->enabled;
521206b73d0SCy Schubert 
522206b73d0SCy Schubert 	/* enable kernel driver support for IEEE 802.1X */
523206b73d0SCy Schubert 	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
524206b73d0SCy Schubert 		printf("Could not setup IEEE 802.1X support in kernel driver."
525206b73d0SCy Schubert 		       "\n");
526206b73d0SCy Schubert 		return -1;
527206b73d0SCy Schubert 	}
528206b73d0SCy Schubert 
529206b73d0SCy Schubert 	if (!enabled)
530206b73d0SCy Schubert 		return 0;
531206b73d0SCy Schubert 
532206b73d0SCy Schubert 	/* use host driver implementation of encryption to allow
533206b73d0SCy Schubert 	 * individual keys and passing plaintext EAPOL frames */
534206b73d0SCy Schubert 	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
535206b73d0SCy Schubert 	    hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
536206b73d0SCy Schubert 		printf("Could not setup host-based encryption in kernel "
537206b73d0SCy Schubert 		       "driver.\n");
538206b73d0SCy Schubert 		return -1;
539206b73d0SCy Schubert 	}
540206b73d0SCy Schubert 
541206b73d0SCy Schubert 	return 0;
542206b73d0SCy Schubert }
543206b73d0SCy Schubert 
544206b73d0SCy Schubert 
hostap_set_privacy(void * priv,int enabled)545206b73d0SCy Schubert static int hostap_set_privacy(void *priv, int enabled)
546206b73d0SCy Schubert {
547206b73d0SCy Schubert 	struct hostap_drvier_data *drv = priv;
548206b73d0SCy Schubert 
549206b73d0SCy Schubert 	return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
550206b73d0SCy Schubert 					enabled);
551206b73d0SCy Schubert }
552206b73d0SCy Schubert 
553206b73d0SCy Schubert 
hostap_set_ssid(void * priv,const u8 * buf,int len)554206b73d0SCy Schubert static int hostap_set_ssid(void *priv, const u8 *buf, int len)
555206b73d0SCy Schubert {
556206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
557206b73d0SCy Schubert 	struct iwreq iwr;
558206b73d0SCy Schubert 
559206b73d0SCy Schubert 	memset(&iwr, 0, sizeof(iwr));
560206b73d0SCy Schubert 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
561206b73d0SCy Schubert 	iwr.u.essid.flags = 1; /* SSID active */
562206b73d0SCy Schubert 	iwr.u.essid.pointer = (caddr_t) buf;
563206b73d0SCy Schubert 	iwr.u.essid.length = len + 1;
564206b73d0SCy Schubert 
565206b73d0SCy Schubert 	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
566206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
567206b73d0SCy Schubert 			   len, strerror(errno));
568206b73d0SCy Schubert 		return -1;
569206b73d0SCy Schubert 	}
570206b73d0SCy Schubert 
571206b73d0SCy Schubert 	return 0;
572206b73d0SCy Schubert }
573206b73d0SCy Schubert 
574206b73d0SCy Schubert 
hostap_flush(void * priv)575206b73d0SCy Schubert static int hostap_flush(void *priv)
576206b73d0SCy Schubert {
577206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
578206b73d0SCy Schubert 	struct prism2_hostapd_param param;
579206b73d0SCy Schubert 
580206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
581206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_FLUSH;
582206b73d0SCy Schubert 	return hostapd_ioctl(drv, &param, sizeof(param));
583206b73d0SCy Schubert }
584206b73d0SCy Schubert 
585206b73d0SCy Schubert 
hostap_read_sta_data(void * priv,struct hostap_sta_driver_data * data,const u8 * addr)586206b73d0SCy Schubert static int hostap_read_sta_data(void *priv,
587206b73d0SCy Schubert 				struct hostap_sta_driver_data *data,
588206b73d0SCy Schubert 				const u8 *addr)
589206b73d0SCy Schubert {
590206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
591206b73d0SCy Schubert 	char buf[1024], line[128], *pos;
592206b73d0SCy Schubert 	FILE *f;
593206b73d0SCy Schubert 	unsigned long val;
594206b73d0SCy Schubert 
595206b73d0SCy Schubert 	memset(data, 0, sizeof(*data));
596206b73d0SCy Schubert 	snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
597206b73d0SCy Schubert 		 drv->iface, MAC2STR(addr));
598206b73d0SCy Schubert 
599206b73d0SCy Schubert 	f = fopen(buf, "r");
600206b73d0SCy Schubert 	if (!f)
601206b73d0SCy Schubert 		return -1;
602206b73d0SCy Schubert 	/* Need to read proc file with in one piece, so use large enough
603206b73d0SCy Schubert 	 * buffer. */
604206b73d0SCy Schubert 	setbuffer(f, buf, sizeof(buf));
605206b73d0SCy Schubert 
606206b73d0SCy Schubert 	while (fgets(line, sizeof(line), f)) {
607206b73d0SCy Schubert 		pos = strchr(line, '=');
608206b73d0SCy Schubert 		if (!pos)
609206b73d0SCy Schubert 			continue;
610206b73d0SCy Schubert 		*pos++ = '\0';
611206b73d0SCy Schubert 		val = strtoul(pos, NULL, 10);
612206b73d0SCy Schubert 		if (strcmp(line, "rx_packets") == 0)
613206b73d0SCy Schubert 			data->rx_packets = val;
614206b73d0SCy Schubert 		else if (strcmp(line, "tx_packets") == 0)
615206b73d0SCy Schubert 			data->tx_packets = val;
616206b73d0SCy Schubert 		else if (strcmp(line, "rx_bytes") == 0)
617206b73d0SCy Schubert 			data->rx_bytes = val;
618206b73d0SCy Schubert 		else if (strcmp(line, "tx_bytes") == 0)
619206b73d0SCy Schubert 			data->tx_bytes = val;
620206b73d0SCy Schubert 	}
621206b73d0SCy Schubert 
622206b73d0SCy Schubert 	fclose(f);
623206b73d0SCy Schubert 
624206b73d0SCy Schubert 	return 0;
625206b73d0SCy Schubert }
626206b73d0SCy Schubert 
627206b73d0SCy Schubert 
hostap_sta_add(void * priv,struct hostapd_sta_add_params * params)628206b73d0SCy Schubert static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
629206b73d0SCy Schubert {
630206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
631206b73d0SCy Schubert 	struct prism2_hostapd_param param;
632206b73d0SCy Schubert 	int tx_supp_rates = 0;
633206b73d0SCy Schubert 	size_t i;
634206b73d0SCy Schubert 
635206b73d0SCy Schubert #define WLAN_RATE_1M BIT(0)
636206b73d0SCy Schubert #define WLAN_RATE_2M BIT(1)
637206b73d0SCy Schubert #define WLAN_RATE_5M5 BIT(2)
638206b73d0SCy Schubert #define WLAN_RATE_11M BIT(3)
639206b73d0SCy Schubert 
640206b73d0SCy Schubert 	for (i = 0; i < params->supp_rates_len; i++) {
641206b73d0SCy Schubert 		if ((params->supp_rates[i] & 0x7f) == 2)
642206b73d0SCy Schubert 			tx_supp_rates |= WLAN_RATE_1M;
643206b73d0SCy Schubert 		if ((params->supp_rates[i] & 0x7f) == 4)
644206b73d0SCy Schubert 			tx_supp_rates |= WLAN_RATE_2M;
645206b73d0SCy Schubert 		if ((params->supp_rates[i] & 0x7f) == 11)
646206b73d0SCy Schubert 			tx_supp_rates |= WLAN_RATE_5M5;
647206b73d0SCy Schubert 		if ((params->supp_rates[i] & 0x7f) == 22)
648206b73d0SCy Schubert 			tx_supp_rates |= WLAN_RATE_11M;
649206b73d0SCy Schubert 	}
650206b73d0SCy Schubert 
651206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
652206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_ADD_STA;
653206b73d0SCy Schubert 	memcpy(param.sta_addr, params->addr, ETH_ALEN);
654206b73d0SCy Schubert 	param.u.add_sta.aid = params->aid;
655206b73d0SCy Schubert 	param.u.add_sta.capability = params->capability;
656206b73d0SCy Schubert 	param.u.add_sta.tx_supp_rates = tx_supp_rates;
657206b73d0SCy Schubert 	return hostapd_ioctl(drv, &param, sizeof(param));
658206b73d0SCy Schubert }
659206b73d0SCy Schubert 
660206b73d0SCy Schubert 
hostap_sta_remove(void * priv,const u8 * addr)661206b73d0SCy Schubert static int hostap_sta_remove(void *priv, const u8 *addr)
662206b73d0SCy Schubert {
663206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
664206b73d0SCy Schubert 	struct prism2_hostapd_param param;
665206b73d0SCy Schubert 
666206b73d0SCy Schubert 	hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
667206b73d0SCy Schubert 
668206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
669206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
670206b73d0SCy Schubert 	memcpy(param.sta_addr, addr, ETH_ALEN);
671206b73d0SCy Schubert 	if (hostapd_ioctl(drv, &param, sizeof(param))) {
672206b73d0SCy Schubert 		printf("Could not remove station from kernel driver.\n");
673206b73d0SCy Schubert 		return -1;
674206b73d0SCy Schubert 	}
675206b73d0SCy Schubert 	return 0;
676206b73d0SCy Schubert }
677206b73d0SCy Schubert 
678206b73d0SCy Schubert 
hostap_get_inact_sec(void * priv,const u8 * addr)679206b73d0SCy Schubert static int hostap_get_inact_sec(void *priv, const u8 *addr)
680206b73d0SCy Schubert {
681206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
682206b73d0SCy Schubert 	struct prism2_hostapd_param param;
683206b73d0SCy Schubert 
684206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
685206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
686206b73d0SCy Schubert 	memcpy(param.sta_addr, addr, ETH_ALEN);
687206b73d0SCy Schubert 	if (hostapd_ioctl(drv, &param, sizeof(param))) {
688206b73d0SCy Schubert 		return -1;
689206b73d0SCy Schubert 	}
690206b73d0SCy Schubert 
691206b73d0SCy Schubert 	return param.u.get_info_sta.inactive_sec;
692206b73d0SCy Schubert }
693206b73d0SCy Schubert 
694206b73d0SCy Schubert 
hostap_sta_clear_stats(void * priv,const u8 * addr)695206b73d0SCy Schubert static int hostap_sta_clear_stats(void *priv, const u8 *addr)
696206b73d0SCy Schubert {
697206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
698206b73d0SCy Schubert 	struct prism2_hostapd_param param;
699206b73d0SCy Schubert 
700206b73d0SCy Schubert 	memset(&param, 0, sizeof(param));
701206b73d0SCy Schubert 	param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
702206b73d0SCy Schubert 	memcpy(param.sta_addr, addr, ETH_ALEN);
703206b73d0SCy Schubert 	if (hostapd_ioctl(drv, &param, sizeof(param))) {
704206b73d0SCy Schubert 		return -1;
705206b73d0SCy Schubert 	}
706206b73d0SCy Schubert 
707206b73d0SCy Schubert 	return 0;
708206b73d0SCy Schubert }
709206b73d0SCy Schubert 
710206b73d0SCy Schubert 
hostapd_ioctl_set_generic_elem(struct hostap_driver_data * drv)711206b73d0SCy Schubert static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
712206b73d0SCy Schubert {
713206b73d0SCy Schubert 	struct prism2_hostapd_param *param;
714206b73d0SCy Schubert 	int res;
715206b73d0SCy Schubert 	size_t blen, elem_len;
716206b73d0SCy Schubert 
717206b73d0SCy Schubert 	elem_len = drv->generic_ie_len + drv->wps_ie_len;
718206b73d0SCy Schubert 	blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
719206b73d0SCy Schubert 	if (blen < sizeof(*param))
720206b73d0SCy Schubert 		blen = sizeof(*param);
721206b73d0SCy Schubert 
722206b73d0SCy Schubert 	param = os_zalloc(blen);
723206b73d0SCy Schubert 	if (param == NULL)
724206b73d0SCy Schubert 		return -1;
725206b73d0SCy Schubert 
726206b73d0SCy Schubert 	param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
727206b73d0SCy Schubert 	param->u.generic_elem.len = elem_len;
728206b73d0SCy Schubert 	if (drv->generic_ie) {
729206b73d0SCy Schubert 		os_memcpy(param->u.generic_elem.data, drv->generic_ie,
730206b73d0SCy Schubert 			  drv->generic_ie_len);
731206b73d0SCy Schubert 	}
732206b73d0SCy Schubert 	if (drv->wps_ie) {
733206b73d0SCy Schubert 		os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
734206b73d0SCy Schubert 			  drv->wps_ie, drv->wps_ie_len);
735206b73d0SCy Schubert 	}
736206b73d0SCy Schubert 	wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
737206b73d0SCy Schubert 		    param->u.generic_elem.data, elem_len);
738206b73d0SCy Schubert 	res = hostapd_ioctl(drv, param, blen);
739206b73d0SCy Schubert 
740206b73d0SCy Schubert 	os_free(param);
741206b73d0SCy Schubert 
742206b73d0SCy Schubert 	return res;
743206b73d0SCy Schubert }
744206b73d0SCy Schubert 
745206b73d0SCy Schubert 
hostap_set_generic_elem(void * priv,const u8 * elem,size_t elem_len)746206b73d0SCy Schubert static int hostap_set_generic_elem(void *priv,
747206b73d0SCy Schubert 				   const u8 *elem, size_t elem_len)
748206b73d0SCy Schubert {
749206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
750206b73d0SCy Schubert 
751206b73d0SCy Schubert 	os_free(drv->generic_ie);
752206b73d0SCy Schubert 	drv->generic_ie = NULL;
753206b73d0SCy Schubert 	drv->generic_ie_len = 0;
754206b73d0SCy Schubert 	if (elem) {
755206b73d0SCy Schubert 		drv->generic_ie = os_memdup(elem, elem_len);
756206b73d0SCy Schubert 		if (drv->generic_ie == NULL)
757206b73d0SCy Schubert 			return -1;
758206b73d0SCy Schubert 		drv->generic_ie_len = elem_len;
759206b73d0SCy Schubert 	}
760206b73d0SCy Schubert 
761206b73d0SCy Schubert 	return hostapd_ioctl_set_generic_elem(drv);
762206b73d0SCy Schubert }
763206b73d0SCy Schubert 
764206b73d0SCy Schubert 
hostap_set_ap_wps_ie(void * priv,const struct wpabuf * beacon,const struct wpabuf * proberesp,const struct wpabuf * assocresp)765206b73d0SCy Schubert static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
766206b73d0SCy Schubert 				const struct wpabuf *proberesp,
767206b73d0SCy Schubert 				const struct wpabuf *assocresp)
768206b73d0SCy Schubert {
769206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
770206b73d0SCy Schubert 
771206b73d0SCy Schubert 	/*
772206b73d0SCy Schubert 	 * Host AP driver supports only one set of extra IEs, so we need to
773206b73d0SCy Schubert 	 * use the Probe Response IEs also for Beacon frames since they include
774206b73d0SCy Schubert 	 * more information.
775206b73d0SCy Schubert 	 */
776206b73d0SCy Schubert 
777206b73d0SCy Schubert 	os_free(drv->wps_ie);
778206b73d0SCy Schubert 	drv->wps_ie = NULL;
779206b73d0SCy Schubert 	drv->wps_ie_len = 0;
780206b73d0SCy Schubert 	if (proberesp) {
781206b73d0SCy Schubert 		drv->wps_ie = os_memdup(wpabuf_head(proberesp),
782206b73d0SCy Schubert 					wpabuf_len(proberesp));
783206b73d0SCy Schubert 		if (drv->wps_ie == NULL)
784206b73d0SCy Schubert 			return -1;
785206b73d0SCy Schubert 		drv->wps_ie_len = wpabuf_len(proberesp);
786206b73d0SCy Schubert 	}
787206b73d0SCy Schubert 
788206b73d0SCy Schubert 	return hostapd_ioctl_set_generic_elem(drv);
789206b73d0SCy Schubert }
790206b73d0SCy Schubert 
791206b73d0SCy Schubert 
792206b73d0SCy Schubert static void
hostapd_wireless_event_wireless_custom(struct hostap_driver_data * drv,char * custom)793206b73d0SCy Schubert hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
794206b73d0SCy Schubert 				       char *custom)
795206b73d0SCy Schubert {
796206b73d0SCy Schubert 	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
797206b73d0SCy Schubert 
798206b73d0SCy Schubert 	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
799206b73d0SCy Schubert 		char *pos;
800206b73d0SCy Schubert 		u8 addr[ETH_ALEN];
801206b73d0SCy Schubert 		pos = strstr(custom, "addr=");
802206b73d0SCy Schubert 		if (pos == NULL) {
803206b73d0SCy Schubert 			wpa_printf(MSG_DEBUG,
804206b73d0SCy Schubert 				   "MLME-MICHAELMICFAILURE.indication "
805206b73d0SCy Schubert 				   "without sender address ignored");
806206b73d0SCy Schubert 			return;
807206b73d0SCy Schubert 		}
808206b73d0SCy Schubert 		pos += 5;
809206b73d0SCy Schubert 		if (hwaddr_aton(pos, addr) == 0) {
810206b73d0SCy Schubert 			union wpa_event_data data;
811206b73d0SCy Schubert 			os_memset(&data, 0, sizeof(data));
812206b73d0SCy Schubert 			data.michael_mic_failure.unicast = 1;
813206b73d0SCy Schubert 			data.michael_mic_failure.src = addr;
814206b73d0SCy Schubert 			wpa_supplicant_event(drv->hapd,
815206b73d0SCy Schubert 					     EVENT_MICHAEL_MIC_FAILURE, &data);
816206b73d0SCy Schubert 		} else {
817206b73d0SCy Schubert 			wpa_printf(MSG_DEBUG,
818206b73d0SCy Schubert 				   "MLME-MICHAELMICFAILURE.indication "
819206b73d0SCy Schubert 				   "with invalid MAC address");
820206b73d0SCy Schubert 		}
821206b73d0SCy Schubert 	}
822206b73d0SCy Schubert }
823206b73d0SCy Schubert 
824206b73d0SCy Schubert 
hostapd_wireless_event_wireless(struct hostap_driver_data * drv,char * data,unsigned int len)825206b73d0SCy Schubert static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
826206b73d0SCy Schubert 					    char *data, unsigned int len)
827206b73d0SCy Schubert {
828206b73d0SCy Schubert 	struct iw_event iwe_buf, *iwe = &iwe_buf;
829206b73d0SCy Schubert 	char *pos, *end, *custom, *buf;
830206b73d0SCy Schubert 
831206b73d0SCy Schubert 	pos = data;
832206b73d0SCy Schubert 	end = data + len;
833206b73d0SCy Schubert 
834206b73d0SCy Schubert 	while ((size_t) (end - pos) >= IW_EV_LCP_LEN) {
835206b73d0SCy Schubert 		/* Event data may be unaligned, so make a local, aligned copy
836206b73d0SCy Schubert 		 * before processing. */
837206b73d0SCy Schubert 		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
838206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
839206b73d0SCy Schubert 			   iwe->cmd, iwe->len);
840206b73d0SCy Schubert 		if (iwe->len <= IW_EV_LCP_LEN || iwe->len > end - pos)
841206b73d0SCy Schubert 			return;
842206b73d0SCy Schubert 
843206b73d0SCy Schubert 		custom = pos + IW_EV_POINT_LEN;
844206b73d0SCy Schubert 		if (drv->we_version > 18 &&
845206b73d0SCy Schubert 		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
846206b73d0SCy Schubert 		     iwe->cmd == IWEVCUSTOM)) {
847206b73d0SCy Schubert 			/* WE-19 removed the pointer from struct iw_point */
848206b73d0SCy Schubert 			char *dpos = (char *) &iwe_buf.u.data.length;
849206b73d0SCy Schubert 			int dlen = dpos - (char *) &iwe_buf;
850206b73d0SCy Schubert 			memcpy(dpos, pos + IW_EV_LCP_LEN,
851206b73d0SCy Schubert 			       sizeof(struct iw_event) - dlen);
852206b73d0SCy Schubert 		} else {
853206b73d0SCy Schubert 			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
854206b73d0SCy Schubert 			custom += IW_EV_POINT_OFF;
855206b73d0SCy Schubert 		}
856206b73d0SCy Schubert 
857206b73d0SCy Schubert 		switch (iwe->cmd) {
858206b73d0SCy Schubert 		case IWEVCUSTOM:
859206b73d0SCy Schubert 			if (iwe->u.data.length > end - custom)
860206b73d0SCy Schubert 				return;
861206b73d0SCy Schubert 			buf = malloc(iwe->u.data.length + 1);
862206b73d0SCy Schubert 			if (buf == NULL)
863206b73d0SCy Schubert 				return;
864206b73d0SCy Schubert 			memcpy(buf, custom, iwe->u.data.length);
865206b73d0SCy Schubert 			buf[iwe->u.data.length] = '\0';
866206b73d0SCy Schubert 			hostapd_wireless_event_wireless_custom(drv, buf);
867206b73d0SCy Schubert 			free(buf);
868206b73d0SCy Schubert 			break;
869206b73d0SCy Schubert 		}
870206b73d0SCy Schubert 
871206b73d0SCy Schubert 		pos += iwe->len;
872206b73d0SCy Schubert 	}
873206b73d0SCy Schubert }
874206b73d0SCy Schubert 
875206b73d0SCy Schubert 
hostapd_wireless_event_rtm_newlink(void * ctx,struct ifinfomsg * ifi,u8 * buf,size_t len)876206b73d0SCy Schubert static void hostapd_wireless_event_rtm_newlink(void *ctx,
877206b73d0SCy Schubert 					       struct ifinfomsg *ifi,
878206b73d0SCy Schubert 					       u8 *buf, size_t len)
879206b73d0SCy Schubert {
880206b73d0SCy Schubert 	struct hostap_driver_data *drv = ctx;
881206b73d0SCy Schubert 	int attrlen, rta_len;
882206b73d0SCy Schubert 	struct rtattr *attr;
883206b73d0SCy Schubert 
884206b73d0SCy Schubert 	/* TODO: use ifi->ifi_index to filter out wireless events from other
885206b73d0SCy Schubert 	 * interfaces */
886206b73d0SCy Schubert 
887206b73d0SCy Schubert 	attrlen = len;
888206b73d0SCy Schubert 	attr = (struct rtattr *) buf;
889206b73d0SCy Schubert 
890206b73d0SCy Schubert 	rta_len = RTA_ALIGN(sizeof(struct rtattr));
891206b73d0SCy Schubert 	while (RTA_OK(attr, attrlen)) {
892206b73d0SCy Schubert 		if (attr->rta_type == IFLA_WIRELESS) {
893206b73d0SCy Schubert 			hostapd_wireless_event_wireless(
894206b73d0SCy Schubert 				drv, ((char *) attr) + rta_len,
895206b73d0SCy Schubert 				attr->rta_len - rta_len);
896206b73d0SCy Schubert 		}
897206b73d0SCy Schubert 		attr = RTA_NEXT(attr, attrlen);
898206b73d0SCy Schubert 	}
899206b73d0SCy Schubert }
900206b73d0SCy Schubert 
901206b73d0SCy Schubert 
hostap_get_we_version(struct hostap_driver_data * drv)902206b73d0SCy Schubert static int hostap_get_we_version(struct hostap_driver_data *drv)
903206b73d0SCy Schubert {
904206b73d0SCy Schubert 	struct iw_range *range;
905206b73d0SCy Schubert 	struct iwreq iwr;
906206b73d0SCy Schubert 	int minlen;
907206b73d0SCy Schubert 	size_t buflen;
908206b73d0SCy Schubert 
909206b73d0SCy Schubert 	drv->we_version = 0;
910206b73d0SCy Schubert 
911206b73d0SCy Schubert 	/*
912206b73d0SCy Schubert 	 * Use larger buffer than struct iw_range in order to allow the
913206b73d0SCy Schubert 	 * structure to grow in the future.
914206b73d0SCy Schubert 	 */
915206b73d0SCy Schubert 	buflen = sizeof(struct iw_range) + 500;
916206b73d0SCy Schubert 	range = os_zalloc(buflen);
917206b73d0SCy Schubert 	if (range == NULL)
918206b73d0SCy Schubert 		return -1;
919206b73d0SCy Schubert 
920206b73d0SCy Schubert 	memset(&iwr, 0, sizeof(iwr));
921206b73d0SCy Schubert 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
922206b73d0SCy Schubert 	iwr.u.data.pointer = (caddr_t) range;
923206b73d0SCy Schubert 	iwr.u.data.length = buflen;
924206b73d0SCy Schubert 
925206b73d0SCy Schubert 	minlen = ((char *) &range->enc_capa) - (char *) range +
926206b73d0SCy Schubert 		sizeof(range->enc_capa);
927206b73d0SCy Schubert 
928206b73d0SCy Schubert 	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
929206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
930206b73d0SCy Schubert 			   strerror(errno));
931206b73d0SCy Schubert 		os_free(range);
932206b73d0SCy Schubert 		return -1;
933206b73d0SCy Schubert 	} else if (iwr.u.data.length >= minlen &&
934206b73d0SCy Schubert 		   range->we_version_compiled >= 18) {
935206b73d0SCy Schubert 		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
936206b73d0SCy Schubert 			   "WE(source)=%d enc_capa=0x%x",
937206b73d0SCy Schubert 			   range->we_version_compiled,
938206b73d0SCy Schubert 			   range->we_version_source,
939206b73d0SCy Schubert 			   range->enc_capa);
940206b73d0SCy Schubert 		drv->we_version = range->we_version_compiled;
941206b73d0SCy Schubert 	}
942206b73d0SCy Schubert 
943206b73d0SCy Schubert 	free(range);
944206b73d0SCy Schubert 	return 0;
945206b73d0SCy Schubert }
946206b73d0SCy Schubert 
947206b73d0SCy Schubert 
hostap_wireless_event_init(struct hostap_driver_data * drv)948206b73d0SCy Schubert static int hostap_wireless_event_init(struct hostap_driver_data *drv)
949206b73d0SCy Schubert {
950206b73d0SCy Schubert 	struct netlink_config *cfg;
951206b73d0SCy Schubert 
952206b73d0SCy Schubert 	hostap_get_we_version(drv);
953206b73d0SCy Schubert 
954206b73d0SCy Schubert 	cfg = os_zalloc(sizeof(*cfg));
955206b73d0SCy Schubert 	if (cfg == NULL)
956206b73d0SCy Schubert 		return -1;
957206b73d0SCy Schubert 	cfg->ctx = drv;
958206b73d0SCy Schubert 	cfg->newlink_cb = hostapd_wireless_event_rtm_newlink;
959206b73d0SCy Schubert 	drv->netlink = netlink_init(cfg);
960206b73d0SCy Schubert 	if (drv->netlink == NULL) {
961206b73d0SCy Schubert 		os_free(cfg);
962206b73d0SCy Schubert 		return -1;
963206b73d0SCy Schubert 	}
964206b73d0SCy Schubert 
965206b73d0SCy Schubert 	return 0;
966206b73d0SCy Schubert }
967206b73d0SCy Schubert 
968206b73d0SCy Schubert 
hostap_init(struct hostapd_data * hapd,struct wpa_init_params * params)969206b73d0SCy Schubert static void * hostap_init(struct hostapd_data *hapd,
970206b73d0SCy Schubert 			  struct wpa_init_params *params)
971206b73d0SCy Schubert {
972206b73d0SCy Schubert 	struct hostap_driver_data *drv;
973206b73d0SCy Schubert 
974206b73d0SCy Schubert 	drv = os_zalloc(sizeof(struct hostap_driver_data));
975206b73d0SCy Schubert 	if (drv == NULL) {
976206b73d0SCy Schubert 		printf("Could not allocate memory for hostapd driver data\n");
977206b73d0SCy Schubert 		return NULL;
978206b73d0SCy Schubert 	}
979206b73d0SCy Schubert 
980206b73d0SCy Schubert 	drv->hapd = hapd;
981206b73d0SCy Schubert 	drv->ioctl_sock = drv->sock = -1;
982206b73d0SCy Schubert 	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
983206b73d0SCy Schubert 
984206b73d0SCy Schubert 	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
985206b73d0SCy Schubert 	if (drv->ioctl_sock < 0) {
986206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
987206b73d0SCy Schubert 			   strerror(errno));
988206b73d0SCy Schubert 		os_free(drv);
989206b73d0SCy Schubert 		return NULL;
990206b73d0SCy Schubert 	}
991206b73d0SCy Schubert 
992206b73d0SCy Schubert 	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
993206b73d0SCy Schubert 		wpa_printf(MSG_ERROR,
994206b73d0SCy Schubert 			   "Could not enable hostapd mode for interface %s",
995206b73d0SCy Schubert 			   drv->iface);
996206b73d0SCy Schubert 		close(drv->ioctl_sock);
997206b73d0SCy Schubert 		os_free(drv);
998206b73d0SCy Schubert 		return NULL;
999206b73d0SCy Schubert 	}
1000206b73d0SCy Schubert 
1001206b73d0SCy Schubert 	if (hostap_init_sockets(drv, params->own_addr) ||
1002206b73d0SCy Schubert 	    hostap_wireless_event_init(drv)) {
1003206b73d0SCy Schubert 		close(drv->ioctl_sock);
1004206b73d0SCy Schubert 		os_free(drv);
1005206b73d0SCy Schubert 		return NULL;
1006206b73d0SCy Schubert 	}
1007206b73d0SCy Schubert 
1008206b73d0SCy Schubert 	return drv;
1009206b73d0SCy Schubert }
1010206b73d0SCy Schubert 
1011206b73d0SCy Schubert 
hostap_driver_deinit(void * priv)1012206b73d0SCy Schubert static void hostap_driver_deinit(void *priv)
1013206b73d0SCy Schubert {
1014206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
1015206b73d0SCy Schubert 
1016206b73d0SCy Schubert 	netlink_deinit(drv->netlink);
1017206b73d0SCy Schubert 	(void) hostap_set_iface_flags(drv, 0);
1018206b73d0SCy Schubert 	(void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1019206b73d0SCy Schubert 	(void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1020206b73d0SCy Schubert 
1021206b73d0SCy Schubert 	if (drv->ioctl_sock >= 0)
1022206b73d0SCy Schubert 		close(drv->ioctl_sock);
1023206b73d0SCy Schubert 
1024206b73d0SCy Schubert 	if (drv->sock >= 0)
1025206b73d0SCy Schubert 		close(drv->sock);
1026206b73d0SCy Schubert 
1027206b73d0SCy Schubert 	os_free(drv->generic_ie);
1028206b73d0SCy Schubert 	os_free(drv->wps_ie);
1029206b73d0SCy Schubert 
1030206b73d0SCy Schubert 	free(drv);
1031206b73d0SCy Schubert }
1032206b73d0SCy Schubert 
1033206b73d0SCy Schubert 
hostap_sta_deauth(void * priv,const u8 * own_addr,const u8 * addr,u16 reason)1034206b73d0SCy Schubert static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
1035206b73d0SCy Schubert 			     u16 reason)
1036206b73d0SCy Schubert {
1037206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
1038206b73d0SCy Schubert 	struct ieee80211_mgmt mgmt;
1039206b73d0SCy Schubert 
1040206b73d0SCy Schubert 	if (is_broadcast_ether_addr(addr)) {
1041206b73d0SCy Schubert 		/*
1042206b73d0SCy Schubert 		 * New Prism2.5/3 STA firmware versions seem to have issues
1043206b73d0SCy Schubert 		 * with this broadcast deauth frame. This gets the firmware in
1044206b73d0SCy Schubert 		 * odd state where nothing works correctly, so let's skip
1045206b73d0SCy Schubert 		 * sending this for the hostap driver.
1046206b73d0SCy Schubert 		 */
1047206b73d0SCy Schubert 		return 0;
1048206b73d0SCy Schubert 	}
1049206b73d0SCy Schubert 
1050206b73d0SCy Schubert 	memset(&mgmt, 0, sizeof(mgmt));
1051206b73d0SCy Schubert 	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1052206b73d0SCy Schubert 					  WLAN_FC_STYPE_DEAUTH);
1053206b73d0SCy Schubert 	memcpy(mgmt.da, addr, ETH_ALEN);
1054206b73d0SCy Schubert 	memcpy(mgmt.sa, own_addr, ETH_ALEN);
1055206b73d0SCy Schubert 	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1056206b73d0SCy Schubert 	mgmt.u.deauth.reason_code = host_to_le16(reason);
1057206b73d0SCy Schubert 	return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1058*c1d255d3SCy Schubert 				sizeof(mgmt.u.deauth), 0, 0, NULL, 0, 0, 0);
1059206b73d0SCy Schubert }
1060206b73d0SCy Schubert 
1061206b73d0SCy Schubert 
hostap_set_freq(void * priv,struct hostapd_freq_params * freq)1062206b73d0SCy Schubert static int hostap_set_freq(void *priv, struct hostapd_freq_params *freq)
1063206b73d0SCy Schubert {
1064206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
1065206b73d0SCy Schubert 	struct iwreq iwr;
1066206b73d0SCy Schubert 
1067206b73d0SCy Schubert 	os_memset(&iwr, 0, sizeof(iwr));
1068206b73d0SCy Schubert 	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1069206b73d0SCy Schubert 	iwr.u.freq.m = freq->channel;
1070206b73d0SCy Schubert 	iwr.u.freq.e = 0;
1071206b73d0SCy Schubert 
1072206b73d0SCy Schubert 	if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
1073206b73d0SCy Schubert 		wpa_printf(MSG_ERROR, "ioctl[SIOCSIWFREQ]: %s",
1074206b73d0SCy Schubert 			   strerror(errno));
1075206b73d0SCy Schubert 		return -1;
1076206b73d0SCy Schubert 	}
1077206b73d0SCy Schubert 
1078206b73d0SCy Schubert 	return 0;
1079206b73d0SCy Schubert }
1080206b73d0SCy Schubert 
1081206b73d0SCy Schubert 
hostap_sta_disassoc(void * priv,const u8 * own_addr,const u8 * addr,u16 reason)1082206b73d0SCy Schubert static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
1083206b73d0SCy Schubert 			       u16 reason)
1084206b73d0SCy Schubert {
1085206b73d0SCy Schubert 	struct hostap_driver_data *drv = priv;
1086206b73d0SCy Schubert 	struct ieee80211_mgmt mgmt;
1087206b73d0SCy Schubert 
1088206b73d0SCy Schubert 	memset(&mgmt, 0, sizeof(mgmt));
1089206b73d0SCy Schubert 	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1090206b73d0SCy Schubert 					  WLAN_FC_STYPE_DISASSOC);
1091206b73d0SCy Schubert 	memcpy(mgmt.da, addr, ETH_ALEN);
1092206b73d0SCy Schubert 	memcpy(mgmt.sa, own_addr, ETH_ALEN);
1093206b73d0SCy Schubert 	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1094206b73d0SCy Schubert 	mgmt.u.disassoc.reason_code = host_to_le16(reason);
1095206b73d0SCy Schubert 	return  hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1096*c1d255d3SCy Schubert 				 sizeof(mgmt.u.disassoc), 0, 0, NULL, 0, 0, 0);
1097206b73d0SCy Schubert }
1098206b73d0SCy Schubert 
1099206b73d0SCy Schubert 
hostap_get_hw_feature_data(void * priv,u16 * num_modes,u16 * flags,u8 * dfs)1100206b73d0SCy Schubert static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1101206b73d0SCy Schubert 							    u16 *num_modes,
1102206b73d0SCy Schubert 							    u16 *flags, u8 *dfs)
1103206b73d0SCy Schubert {
1104206b73d0SCy Schubert 	struct hostapd_hw_modes *mode;
1105206b73d0SCy Schubert 	int i, clen, rlen;
1106206b73d0SCy Schubert 	const short chan2freq[14] = {
1107206b73d0SCy Schubert 		2412, 2417, 2422, 2427, 2432, 2437, 2442,
1108206b73d0SCy Schubert 		2447, 2452, 2457, 2462, 2467, 2472, 2484
1109206b73d0SCy Schubert 	};
1110206b73d0SCy Schubert 
1111206b73d0SCy Schubert 	mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1112206b73d0SCy Schubert 	if (mode == NULL)
1113206b73d0SCy Schubert 		return NULL;
1114206b73d0SCy Schubert 
1115206b73d0SCy Schubert 	*num_modes = 1;
1116206b73d0SCy Schubert 	*flags = 0;
1117206b73d0SCy Schubert 	*dfs = 0;
1118206b73d0SCy Schubert 
1119206b73d0SCy Schubert 	mode->mode = HOSTAPD_MODE_IEEE80211B;
1120206b73d0SCy Schubert 	mode->num_channels = 14;
1121206b73d0SCy Schubert 	mode->num_rates = 4;
1122206b73d0SCy Schubert 
1123206b73d0SCy Schubert 	clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1124206b73d0SCy Schubert 	rlen = mode->num_rates * sizeof(int);
1125206b73d0SCy Schubert 
1126206b73d0SCy Schubert 	mode->channels = os_zalloc(clen);
1127206b73d0SCy Schubert 	mode->rates = os_zalloc(rlen);
1128206b73d0SCy Schubert 	if (mode->channels == NULL || mode->rates == NULL) {
1129206b73d0SCy Schubert 		os_free(mode->channels);
1130206b73d0SCy Schubert 		os_free(mode->rates);
1131206b73d0SCy Schubert 		os_free(mode);
1132206b73d0SCy Schubert 		return NULL;
1133206b73d0SCy Schubert 	}
1134206b73d0SCy Schubert 
1135206b73d0SCy Schubert 	for (i = 0; i < 14; i++) {
1136206b73d0SCy Schubert 		mode->channels[i].chan = i + 1;
1137206b73d0SCy Schubert 		mode->channels[i].freq = chan2freq[i];
1138206b73d0SCy Schubert 		mode->channels[i].allowed_bw = HOSTAPD_CHAN_WIDTH_20;
1139206b73d0SCy Schubert 		/* TODO: Get allowed channel list from the driver */
1140206b73d0SCy Schubert 		if (i >= 11)
1141206b73d0SCy Schubert 			mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1142206b73d0SCy Schubert 	}
1143206b73d0SCy Schubert 
1144206b73d0SCy Schubert 	mode->rates[0] = 10;
1145206b73d0SCy Schubert 	mode->rates[1] = 20;
1146206b73d0SCy Schubert 	mode->rates[2] = 55;
1147206b73d0SCy Schubert 	mode->rates[3] = 110;
1148206b73d0SCy Schubert 
1149206b73d0SCy Schubert 	return mode;
1150206b73d0SCy Schubert }
1151206b73d0SCy Schubert 
1152206b73d0SCy Schubert 
wpa_driver_hostap_poll_client(void * priv,const u8 * own_addr,const u8 * addr,int qos)1153206b73d0SCy Schubert static void wpa_driver_hostap_poll_client(void *priv, const u8 *own_addr,
1154206b73d0SCy Schubert 					  const u8 *addr, int qos)
1155206b73d0SCy Schubert {
1156206b73d0SCy Schubert 	struct ieee80211_hdr hdr;
1157206b73d0SCy Schubert 
1158206b73d0SCy Schubert 	os_memset(&hdr, 0, sizeof(hdr));
1159206b73d0SCy Schubert 
1160206b73d0SCy Schubert 	/*
1161206b73d0SCy Schubert 	 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
1162206b73d0SCy Schubert 	 * but it is apparently not retried so TX Exc events
1163206b73d0SCy Schubert 	 * are not received for it.
1164206b73d0SCy Schubert 	 * This is the reason the driver overrides the default
1165206b73d0SCy Schubert 	 * handling.
1166206b73d0SCy Schubert 	 */
1167206b73d0SCy Schubert 	hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA,
1168206b73d0SCy Schubert 					 WLAN_FC_STYPE_DATA);
1169206b73d0SCy Schubert 
1170206b73d0SCy Schubert 	hdr.frame_control |=
1171206b73d0SCy Schubert 		host_to_le16(WLAN_FC_FROMDS);
1172206b73d0SCy Schubert 	os_memcpy(hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
1173206b73d0SCy Schubert 	os_memcpy(hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
1174206b73d0SCy Schubert 	os_memcpy(hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
1175206b73d0SCy Schubert 
1176*c1d255d3SCy Schubert 	hostap_send_mlme(priv, (u8 *)&hdr, sizeof(hdr), 0, 0, NULL, 0, 0, 0);
1177206b73d0SCy Schubert }
1178206b73d0SCy Schubert 
1179206b73d0SCy Schubert 
1180206b73d0SCy Schubert const struct wpa_driver_ops wpa_driver_hostap_ops = {
1181206b73d0SCy Schubert 	.name = "hostap",
1182206b73d0SCy Schubert 	.desc = "Host AP driver (Intersil Prism2/2.5/3)",
1183206b73d0SCy Schubert 	.set_key = wpa_driver_hostap_set_key,
1184206b73d0SCy Schubert 	.hapd_init = hostap_init,
1185206b73d0SCy Schubert 	.hapd_deinit = hostap_driver_deinit,
1186206b73d0SCy Schubert 	.set_ieee8021x = hostap_set_ieee8021x,
1187206b73d0SCy Schubert 	.set_privacy = hostap_set_privacy,
1188206b73d0SCy Schubert 	.get_seqnum = hostap_get_seqnum,
1189206b73d0SCy Schubert 	.flush = hostap_flush,
1190206b73d0SCy Schubert 	.set_generic_elem = hostap_set_generic_elem,
1191206b73d0SCy Schubert 	.read_sta_data = hostap_read_sta_data,
1192206b73d0SCy Schubert 	.hapd_send_eapol = hostap_send_eapol,
1193206b73d0SCy Schubert 	.sta_set_flags = hostap_sta_set_flags,
1194206b73d0SCy Schubert 	.sta_deauth = hostap_sta_deauth,
1195206b73d0SCy Schubert 	.sta_disassoc = hostap_sta_disassoc,
1196206b73d0SCy Schubert 	.sta_remove = hostap_sta_remove,
1197206b73d0SCy Schubert 	.hapd_set_ssid = hostap_set_ssid,
1198206b73d0SCy Schubert 	.send_mlme = hostap_send_mlme,
1199206b73d0SCy Schubert 	.sta_add = hostap_sta_add,
1200206b73d0SCy Schubert 	.get_inact_sec = hostap_get_inact_sec,
1201206b73d0SCy Schubert 	.sta_clear_stats = hostap_sta_clear_stats,
1202206b73d0SCy Schubert 	.get_hw_feature_data = hostap_get_hw_feature_data,
1203206b73d0SCy Schubert 	.set_ap_wps_ie = hostap_set_ap_wps_ie,
1204206b73d0SCy Schubert 	.set_freq = hostap_set_freq,
1205206b73d0SCy Schubert 	.poll_client = wpa_driver_hostap_poll_client,
1206206b73d0SCy Schubert };
1207