1/*
2 * Driver interaction with Linux Host AP driver
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include <sys/ioctl.h>
11
12#include "linux_wext.h"
13#include "common.h"
14#include "driver.h"
15#include "driver_wext.h"
16#include "eloop.h"
17#include "driver_hostap.h"
18
19
20#include <net/if_arp.h>
21#include <netpacket/packet.h>
22
23#include "priv_netlink.h"
24#include "netlink.h"
25#include "linux_ioctl.h"
26#include "common/ieee802_11_defs.h"
27#include "common/ieee802_11_common.h"
28
29
30/* MTU to be set for the wlan#ap device; this is mainly needed for IEEE 802.1X
31 * frames that might be longer than normal default MTU and they are not
32 * fragmented */
33#define HOSTAPD_MTU 2290
34
35static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
36
37struct hostap_driver_data {
38	struct hostapd_data *hapd;
39
40	char iface[IFNAMSIZ + 1];
41	int sock; /* raw packet socket for driver access */
42	int ioctl_sock; /* socket for ioctl() use */
43	struct netlink_data *netlink;
44
45	int we_version;
46
47	u8 *generic_ie;
48	size_t generic_ie_len;
49	u8 *wps_ie;
50	size_t wps_ie_len;
51};
52
53
54static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
55			 int len);
56static int hostap_set_iface_flags(void *priv, int dev_up);
57
58static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
59			u16 stype)
60{
61	struct ieee80211_hdr *hdr;
62	u16 fc, ethertype;
63	u8 *pos, *sa;
64	size_t left;
65	union wpa_event_data event;
66
67	if (len < sizeof(struct ieee80211_hdr))
68		return;
69
70	hdr = (struct ieee80211_hdr *) buf;
71	fc = le_to_host16(hdr->frame_control);
72
73	if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
74		printf("Not ToDS data frame (fc=0x%04x)\n", fc);
75		return;
76	}
77
78	sa = hdr->addr2;
79	os_memset(&event, 0, sizeof(event));
80	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
81	event.rx_from_unknown.addr = sa;
82	wpa_supplicant_event(drv->hapd, EVENT_RX_FROM_UNKNOWN, &event);
83
84	pos = (u8 *) (hdr + 1);
85	left = len - sizeof(*hdr);
86
87	if (left < sizeof(rfc1042_header)) {
88		printf("Too short data frame\n");
89		return;
90	}
91
92	if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
93		printf("Data frame with no RFC1042 header\n");
94		return;
95	}
96	pos += sizeof(rfc1042_header);
97	left -= sizeof(rfc1042_header);
98
99	if (left < 2) {
100		printf("No ethertype in data frame\n");
101		return;
102	}
103
104	ethertype = WPA_GET_BE16(pos);
105	pos += 2;
106	left -= 2;
107	switch (ethertype) {
108	case ETH_P_PAE:
109		drv_event_eapol_rx(drv->hapd, sa, pos, left);
110		break;
111
112	default:
113		printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
114		break;
115	}
116}
117
118
119static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
120			       size_t len, int ok)
121{
122	struct ieee80211_hdr *hdr;
123	u16 fc;
124	union wpa_event_data event;
125
126	hdr = (struct ieee80211_hdr *) buf;
127	fc = le_to_host16(hdr->frame_control);
128
129	os_memset(&event, 0, sizeof(event));
130	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
131	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
132	event.tx_status.dst = hdr->addr1;
133	event.tx_status.data = buf;
134	event.tx_status.data_len = len;
135	event.tx_status.ack = ok;
136	wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
137}
138
139
140static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
141{
142	struct ieee80211_hdr *hdr;
143	u16 fc, extra_len, type, stype;
144	size_t data_len = len;
145	int ver;
146	union wpa_event_data event;
147
148	/* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
149	 * these to user space */
150	if (len < 24) {
151		wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
152			   (unsigned long) len);
153		return;
154	}
155
156	hdr = (struct ieee80211_hdr *) buf;
157	fc = le_to_host16(hdr->frame_control);
158	type = WLAN_FC_GET_TYPE(fc);
159	stype = WLAN_FC_GET_STYPE(fc);
160
161	if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
162		wpa_hexdump(MSG_MSGDUMP, "Received management frame",
163			    buf, len);
164	}
165
166	ver = fc & WLAN_FC_PVER;
167
168	/* protocol version 3 is reserved for indicating extra data after the
169	 * payload, version 2 for indicating ACKed frame (TX callbacks), and
170	 * version 1 for indicating failed frame (no ACK, TX callbacks) */
171	if (ver == 3) {
172		u8 *pos = buf + len - 2;
173		extra_len = WPA_GET_LE16(pos);
174		printf("extra data in frame (elen=%d)\n", extra_len);
175		if ((size_t) extra_len + 2 > len) {
176			printf("  extra data overflow\n");
177			return;
178		}
179		len -= extra_len + 2;
180	} else if (ver == 1 || ver == 2) {
181		handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
182		return;
183	} else if (ver != 0) {
184		printf("unknown protocol version %d\n", ver);
185		return;
186	}
187
188	switch (type) {
189	case WLAN_FC_TYPE_MGMT:
190		os_memset(&event, 0, sizeof(event));
191		event.rx_mgmt.frame = buf;
192		event.rx_mgmt.frame_len = data_len;
193		wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
194		break;
195	case WLAN_FC_TYPE_CTRL:
196		wpa_printf(MSG_DEBUG, "CTRL");
197		break;
198	case WLAN_FC_TYPE_DATA:
199		wpa_printf(MSG_DEBUG, "DATA");
200		handle_data(drv, buf, data_len, stype);
201		break;
202	default:
203		wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
204		break;
205	}
206}
207
208
209static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
210{
211	struct hostap_driver_data *drv = eloop_ctx;
212	int len;
213	unsigned char buf[3000];
214
215	len = recv(sock, buf, sizeof(buf), 0);
216	if (len < 0) {
217		perror("recv");
218		return;
219	}
220
221	handle_frame(drv, buf, len);
222}
223
224
225static int hostap_init_sockets(struct hostap_driver_data *drv, u8 *own_addr)
226{
227	struct ifreq ifr;
228	struct sockaddr_ll addr;
229
230	drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
231	if (drv->sock < 0) {
232		perror("socket[PF_PACKET,SOCK_RAW]");
233		return -1;
234	}
235
236	if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
237		printf("Could not register read socket\n");
238		return -1;
239	}
240
241        memset(&ifr, 0, sizeof(ifr));
242        snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
243        if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
244		perror("ioctl(SIOCGIFINDEX)");
245		return -1;
246        }
247
248	if (hostap_set_iface_flags(drv, 1)) {
249		return -1;
250	}
251
252	memset(&addr, 0, sizeof(addr));
253	addr.sll_family = AF_PACKET;
254	addr.sll_ifindex = ifr.ifr_ifindex;
255	wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
256		   addr.sll_ifindex);
257
258	if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
259		perror("bind");
260		return -1;
261	}
262
263	return linux_get_ifhwaddr(drv->sock, drv->iface, own_addr);
264}
265
266
267static int hostap_send_mlme(void *priv, const u8 *msg, size_t len, int noack)
268{
269	struct hostap_driver_data *drv = priv;
270	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
271	int res;
272
273	/* Request TX callback */
274	hdr->frame_control |= host_to_le16(BIT(1));
275	res = send(drv->sock, msg, len, 0);
276	hdr->frame_control &= ~host_to_le16(BIT(1));
277
278	return res;
279}
280
281
282static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
283			     size_t data_len, int encrypt, const u8 *own_addr,
284			     u32 flags)
285{
286	struct hostap_driver_data *drv = priv;
287	struct ieee80211_hdr *hdr;
288	size_t len;
289	u8 *pos;
290	int res;
291
292	len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
293	hdr = os_zalloc(len);
294	if (hdr == NULL) {
295		printf("malloc() failed for hostapd_send_data(len=%lu)\n",
296		       (unsigned long) len);
297		return -1;
298	}
299
300	hdr->frame_control =
301		IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
302	hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
303	if (encrypt)
304		hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
305	memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
306	memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
307	memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
308
309	pos = (u8 *) (hdr + 1);
310	memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
311	pos += sizeof(rfc1042_header);
312	*((u16 *) pos) = htons(ETH_P_PAE);
313	pos += 2;
314	memcpy(pos, data, data_len);
315
316	res = hostap_send_mlme(drv, (u8 *) hdr, len, 0);
317	if (res < 0) {
318		wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
319			   "failed: %d (%s)",
320			   (unsigned long) len, errno, strerror(errno));
321	}
322	free(hdr);
323
324	return res;
325}
326
327
328static int hostap_sta_set_flags(void *priv, const u8 *addr,
329				int total_flags, int flags_or, int flags_and)
330{
331	struct hostap_driver_data *drv = priv;
332	struct prism2_hostapd_param param;
333
334	if (flags_or & WPA_STA_AUTHORIZED)
335		flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
336	if (!(flags_and & WPA_STA_AUTHORIZED))
337		flags_and = ~BIT(5);
338	else
339		flags_and = ~0;
340	memset(&param, 0, sizeof(param));
341	param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
342	memcpy(param.sta_addr, addr, ETH_ALEN);
343	param.u.set_flags_sta.flags_or = flags_or;
344	param.u.set_flags_sta.flags_and = flags_and;
345	return hostapd_ioctl(drv, &param, sizeof(param));
346}
347
348
349static int hostap_set_iface_flags(void *priv, int dev_up)
350{
351	struct hostap_driver_data *drv = priv;
352	struct ifreq ifr;
353	char ifname[IFNAMSIZ];
354
355	os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface);
356	if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
357		return -1;
358
359	if (dev_up) {
360		memset(&ifr, 0, sizeof(ifr));
361		os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
362		ifr.ifr_mtu = HOSTAPD_MTU;
363		if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
364			perror("ioctl[SIOCSIFMTU]");
365			printf("Setting MTU failed - trying to survive with "
366			       "current value\n");
367		}
368	}
369
370	return 0;
371}
372
373
374static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
375			 int len)
376{
377	struct hostap_driver_data *drv = priv;
378	struct iwreq iwr;
379
380	memset(&iwr, 0, sizeof(iwr));
381	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
382	iwr.u.data.pointer = (caddr_t) param;
383	iwr.u.data.length = len;
384
385	if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
386		perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
387		return -1;
388	}
389
390	return 0;
391}
392
393
394static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
395				     enum wpa_alg alg, const u8 *addr,
396				     int key_idx, int set_tx,
397				     const u8 *seq, size_t seq_len,
398				     const u8 *key, size_t key_len)
399{
400	struct hostap_driver_data *drv = priv;
401	struct prism2_hostapd_param *param;
402	u8 *buf;
403	size_t blen;
404	int ret = 0;
405
406	blen = sizeof(*param) + key_len;
407	buf = os_zalloc(blen);
408	if (buf == NULL)
409		return -1;
410
411	param = (struct prism2_hostapd_param *) buf;
412	param->cmd = PRISM2_SET_ENCRYPTION;
413	if (addr == NULL)
414		memset(param->sta_addr, 0xff, ETH_ALEN);
415	else
416		memcpy(param->sta_addr, addr, ETH_ALEN);
417	switch (alg) {
418	case WPA_ALG_NONE:
419		os_strlcpy((char *) param->u.crypt.alg, "NONE",
420			   HOSTAP_CRYPT_ALG_NAME_LEN);
421		break;
422	case WPA_ALG_WEP:
423		os_strlcpy((char *) param->u.crypt.alg, "WEP",
424			   HOSTAP_CRYPT_ALG_NAME_LEN);
425		break;
426	case WPA_ALG_TKIP:
427		os_strlcpy((char *) param->u.crypt.alg, "TKIP",
428			   HOSTAP_CRYPT_ALG_NAME_LEN);
429		break;
430	case WPA_ALG_CCMP:
431		os_strlcpy((char *) param->u.crypt.alg, "CCMP",
432			   HOSTAP_CRYPT_ALG_NAME_LEN);
433		break;
434	default:
435		os_free(buf);
436		return -1;
437	}
438	param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
439	param->u.crypt.idx = key_idx;
440	param->u.crypt.key_len = key_len;
441	memcpy((u8 *) (param + 1), key, key_len);
442
443	if (hostapd_ioctl(drv, param, blen)) {
444		printf("Failed to set encryption.\n");
445		ret = -1;
446	}
447	free(buf);
448
449	return ret;
450}
451
452
453static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
454			     int idx, u8 *seq)
455{
456	struct hostap_driver_data *drv = priv;
457	struct prism2_hostapd_param *param;
458	u8 *buf;
459	size_t blen;
460	int ret = 0;
461
462	blen = sizeof(*param) + 32;
463	buf = os_zalloc(blen);
464	if (buf == NULL)
465		return -1;
466
467	param = (struct prism2_hostapd_param *) buf;
468	param->cmd = PRISM2_GET_ENCRYPTION;
469	if (addr == NULL)
470		memset(param->sta_addr, 0xff, ETH_ALEN);
471	else
472		memcpy(param->sta_addr, addr, ETH_ALEN);
473	param->u.crypt.idx = idx;
474
475	if (hostapd_ioctl(drv, param, blen)) {
476		printf("Failed to get encryption.\n");
477		ret = -1;
478	} else {
479		memcpy(seq, param->u.crypt.seq, 8);
480	}
481	free(buf);
482
483	return ret;
484}
485
486
487static int hostap_ioctl_prism2param(void *priv, int param, int value)
488{
489	struct hostap_driver_data *drv = priv;
490	struct iwreq iwr;
491	int *i;
492
493	memset(&iwr, 0, sizeof(iwr));
494	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
495	i = (int *) iwr.u.name;
496	*i++ = param;
497	*i++ = value;
498
499	if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
500		perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
501		return -1;
502	}
503
504	return 0;
505}
506
507
508static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
509{
510	struct hostap_driver_data *drv = priv;
511	int enabled = params->enabled;
512
513	/* enable kernel driver support for IEEE 802.1X */
514	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
515		printf("Could not setup IEEE 802.1X support in kernel driver."
516		       "\n");
517		return -1;
518	}
519
520	if (!enabled)
521		return 0;
522
523	/* use host driver implementation of encryption to allow
524	 * individual keys and passing plaintext EAPOL frames */
525	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
526	    hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
527		printf("Could not setup host-based encryption in kernel "
528		       "driver.\n");
529		return -1;
530	}
531
532	return 0;
533}
534
535
536static int hostap_set_privacy(void *priv, int enabled)
537{
538	struct hostap_drvier_data *drv = priv;
539
540	return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
541					enabled);
542}
543
544
545static int hostap_set_ssid(void *priv, const u8 *buf, int len)
546{
547	struct hostap_driver_data *drv = priv;
548	struct iwreq iwr;
549
550	memset(&iwr, 0, sizeof(iwr));
551	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
552	iwr.u.essid.flags = 1; /* SSID active */
553	iwr.u.essid.pointer = (caddr_t) buf;
554	iwr.u.essid.length = len + 1;
555
556	if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
557		perror("ioctl[SIOCSIWESSID]");
558		printf("len=%d\n", len);
559		return -1;
560	}
561
562	return 0;
563}
564
565
566static int hostap_flush(void *priv)
567{
568	struct hostap_driver_data *drv = priv;
569	struct prism2_hostapd_param param;
570
571	memset(&param, 0, sizeof(param));
572	param.cmd = PRISM2_HOSTAPD_FLUSH;
573	return hostapd_ioctl(drv, &param, sizeof(param));
574}
575
576
577static int hostap_read_sta_data(void *priv,
578				struct hostap_sta_driver_data *data,
579				const u8 *addr)
580{
581	struct hostap_driver_data *drv = priv;
582	char buf[1024], line[128], *pos;
583	FILE *f;
584	unsigned long val;
585
586	memset(data, 0, sizeof(*data));
587	snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
588		 drv->iface, MAC2STR(addr));
589
590	f = fopen(buf, "r");
591	if (!f)
592		return -1;
593	/* Need to read proc file with in one piece, so use large enough
594	 * buffer. */
595	setbuffer(f, buf, sizeof(buf));
596
597	while (fgets(line, sizeof(line), f)) {
598		pos = strchr(line, '=');
599		if (!pos)
600			continue;
601		*pos++ = '\0';
602		val = strtoul(pos, NULL, 10);
603		if (strcmp(line, "rx_packets") == 0)
604			data->rx_packets = val;
605		else if (strcmp(line, "tx_packets") == 0)
606			data->tx_packets = val;
607		else if (strcmp(line, "rx_bytes") == 0)
608			data->rx_bytes = val;
609		else if (strcmp(line, "tx_bytes") == 0)
610			data->tx_bytes = val;
611	}
612
613	fclose(f);
614
615	return 0;
616}
617
618
619static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
620{
621	struct hostap_driver_data *drv = priv;
622	struct prism2_hostapd_param param;
623	int tx_supp_rates = 0;
624	size_t i;
625
626#define WLAN_RATE_1M BIT(0)
627#define WLAN_RATE_2M BIT(1)
628#define WLAN_RATE_5M5 BIT(2)
629#define WLAN_RATE_11M BIT(3)
630
631	for (i = 0; i < params->supp_rates_len; i++) {
632		if ((params->supp_rates[i] & 0x7f) == 2)
633			tx_supp_rates |= WLAN_RATE_1M;
634		if ((params->supp_rates[i] & 0x7f) == 4)
635			tx_supp_rates |= WLAN_RATE_2M;
636		if ((params->supp_rates[i] & 0x7f) == 11)
637			tx_supp_rates |= WLAN_RATE_5M5;
638		if ((params->supp_rates[i] & 0x7f) == 22)
639			tx_supp_rates |= WLAN_RATE_11M;
640	}
641
642	memset(&param, 0, sizeof(param));
643	param.cmd = PRISM2_HOSTAPD_ADD_STA;
644	memcpy(param.sta_addr, params->addr, ETH_ALEN);
645	param.u.add_sta.aid = params->aid;
646	param.u.add_sta.capability = params->capability;
647	param.u.add_sta.tx_supp_rates = tx_supp_rates;
648	return hostapd_ioctl(drv, &param, sizeof(param));
649}
650
651
652static int hostap_sta_remove(void *priv, const u8 *addr)
653{
654	struct hostap_driver_data *drv = priv;
655	struct prism2_hostapd_param param;
656
657	hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
658
659	memset(&param, 0, sizeof(param));
660	param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
661	memcpy(param.sta_addr, addr, ETH_ALEN);
662	if (hostapd_ioctl(drv, &param, sizeof(param))) {
663		printf("Could not remove station from kernel driver.\n");
664		return -1;
665	}
666	return 0;
667}
668
669
670static int hostap_get_inact_sec(void *priv, const u8 *addr)
671{
672	struct hostap_driver_data *drv = priv;
673	struct prism2_hostapd_param param;
674
675	memset(&param, 0, sizeof(param));
676	param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
677	memcpy(param.sta_addr, addr, ETH_ALEN);
678	if (hostapd_ioctl(drv, &param, sizeof(param))) {
679		return -1;
680	}
681
682	return param.u.get_info_sta.inactive_sec;
683}
684
685
686static int hostap_sta_clear_stats(void *priv, const u8 *addr)
687{
688	struct hostap_driver_data *drv = priv;
689	struct prism2_hostapd_param param;
690
691	memset(&param, 0, sizeof(param));
692	param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
693	memcpy(param.sta_addr, addr, ETH_ALEN);
694	if (hostapd_ioctl(drv, &param, sizeof(param))) {
695		return -1;
696	}
697
698	return 0;
699}
700
701
702static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
703{
704	struct prism2_hostapd_param *param;
705	int res;
706	size_t blen, elem_len;
707
708	elem_len = drv->generic_ie_len + drv->wps_ie_len;
709	blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
710	if (blen < sizeof(*param))
711		blen = sizeof(*param);
712
713	param = os_zalloc(blen);
714	if (param == NULL)
715		return -1;
716
717	param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
718	param->u.generic_elem.len = elem_len;
719	if (drv->generic_ie) {
720		os_memcpy(param->u.generic_elem.data, drv->generic_ie,
721			  drv->generic_ie_len);
722	}
723	if (drv->wps_ie) {
724		os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
725			  drv->wps_ie, drv->wps_ie_len);
726	}
727	wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
728		    param->u.generic_elem.data, elem_len);
729	res = hostapd_ioctl(drv, param, blen);
730
731	os_free(param);
732
733	return res;
734}
735
736
737static int hostap_set_generic_elem(void *priv,
738				   const u8 *elem, size_t elem_len)
739{
740	struct hostap_driver_data *drv = priv;
741
742	os_free(drv->generic_ie);
743	drv->generic_ie = NULL;
744	drv->generic_ie_len = 0;
745	if (elem) {
746		drv->generic_ie = os_malloc(elem_len);
747		if (drv->generic_ie == NULL)
748			return -1;
749		os_memcpy(drv->generic_ie, elem, elem_len);
750		drv->generic_ie_len = elem_len;
751	}
752
753	return hostapd_ioctl_set_generic_elem(drv);
754}
755
756
757static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
758				const struct wpabuf *proberesp,
759				const struct wpabuf *assocresp)
760{
761	struct hostap_driver_data *drv = priv;
762
763	/*
764	 * Host AP driver supports only one set of extra IEs, so we need to
765	 * use the Probe Response IEs also for Beacon frames since they include
766	 * more information.
767	 */
768
769	os_free(drv->wps_ie);
770	drv->wps_ie = NULL;
771	drv->wps_ie_len = 0;
772	if (proberesp) {
773		drv->wps_ie = os_malloc(wpabuf_len(proberesp));
774		if (drv->wps_ie == NULL)
775			return -1;
776		os_memcpy(drv->wps_ie, wpabuf_head(proberesp),
777			  wpabuf_len(proberesp));
778		drv->wps_ie_len = wpabuf_len(proberesp);
779	}
780
781	return hostapd_ioctl_set_generic_elem(drv);
782}
783
784
785static void
786hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
787				       char *custom)
788{
789	wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
790
791	if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
792		char *pos;
793		u8 addr[ETH_ALEN];
794		pos = strstr(custom, "addr=");
795		if (pos == NULL) {
796			wpa_printf(MSG_DEBUG,
797				   "MLME-MICHAELMICFAILURE.indication "
798				   "without sender address ignored");
799			return;
800		}
801		pos += 5;
802		if (hwaddr_aton(pos, addr) == 0) {
803			union wpa_event_data data;
804			os_memset(&data, 0, sizeof(data));
805			data.michael_mic_failure.unicast = 1;
806			data.michael_mic_failure.src = addr;
807			wpa_supplicant_event(drv->hapd,
808					     EVENT_MICHAEL_MIC_FAILURE, &data);
809		} else {
810			wpa_printf(MSG_DEBUG,
811				   "MLME-MICHAELMICFAILURE.indication "
812				   "with invalid MAC address");
813		}
814	}
815}
816
817
818static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
819					    char *data, int len)
820{
821	struct iw_event iwe_buf, *iwe = &iwe_buf;
822	char *pos, *end, *custom, *buf;
823
824	pos = data;
825	end = data + len;
826
827	while (pos + IW_EV_LCP_LEN <= end) {
828		/* Event data may be unaligned, so make a local, aligned copy
829		 * before processing. */
830		memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
831		wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
832			   iwe->cmd, iwe->len);
833		if (iwe->len <= IW_EV_LCP_LEN)
834			return;
835
836		custom = pos + IW_EV_POINT_LEN;
837		if (drv->we_version > 18 &&
838		    (iwe->cmd == IWEVMICHAELMICFAILURE ||
839		     iwe->cmd == IWEVCUSTOM)) {
840			/* WE-19 removed the pointer from struct iw_point */
841			char *dpos = (char *) &iwe_buf.u.data.length;
842			int dlen = dpos - (char *) &iwe_buf;
843			memcpy(dpos, pos + IW_EV_LCP_LEN,
844			       sizeof(struct iw_event) - dlen);
845		} else {
846			memcpy(&iwe_buf, pos, sizeof(struct iw_event));
847			custom += IW_EV_POINT_OFF;
848		}
849
850		switch (iwe->cmd) {
851		case IWEVCUSTOM:
852			if (custom + iwe->u.data.length > end)
853				return;
854			buf = malloc(iwe->u.data.length + 1);
855			if (buf == NULL)
856				return;
857			memcpy(buf, custom, iwe->u.data.length);
858			buf[iwe->u.data.length] = '\0';
859			hostapd_wireless_event_wireless_custom(drv, buf);
860			free(buf);
861			break;
862		}
863
864		pos += iwe->len;
865	}
866}
867
868
869static void hostapd_wireless_event_rtm_newlink(void *ctx,
870					       struct ifinfomsg *ifi,
871					       u8 *buf, size_t len)
872{
873	struct hostap_driver_data *drv = ctx;
874	int attrlen, rta_len;
875	struct rtattr *attr;
876
877	/* TODO: use ifi->ifi_index to filter out wireless events from other
878	 * interfaces */
879
880	attrlen = len;
881	attr = (struct rtattr *) buf;
882
883	rta_len = RTA_ALIGN(sizeof(struct rtattr));
884	while (RTA_OK(attr, attrlen)) {
885		if (attr->rta_type == IFLA_WIRELESS) {
886			hostapd_wireless_event_wireless(
887				drv, ((char *) attr) + rta_len,
888				attr->rta_len - rta_len);
889		}
890		attr = RTA_NEXT(attr, attrlen);
891	}
892}
893
894
895static int hostap_get_we_version(struct hostap_driver_data *drv)
896{
897	struct iw_range *range;
898	struct iwreq iwr;
899	int minlen;
900	size_t buflen;
901
902	drv->we_version = 0;
903
904	/*
905	 * Use larger buffer than struct iw_range in order to allow the
906	 * structure to grow in the future.
907	 */
908	buflen = sizeof(struct iw_range) + 500;
909	range = os_zalloc(buflen);
910	if (range == NULL)
911		return -1;
912
913	memset(&iwr, 0, sizeof(iwr));
914	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
915	iwr.u.data.pointer = (caddr_t) range;
916	iwr.u.data.length = buflen;
917
918	minlen = ((char *) &range->enc_capa) - (char *) range +
919		sizeof(range->enc_capa);
920
921	if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
922		perror("ioctl[SIOCGIWRANGE]");
923		free(range);
924		return -1;
925	} else if (iwr.u.data.length >= minlen &&
926		   range->we_version_compiled >= 18) {
927		wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
928			   "WE(source)=%d enc_capa=0x%x",
929			   range->we_version_compiled,
930			   range->we_version_source,
931			   range->enc_capa);
932		drv->we_version = range->we_version_compiled;
933	}
934
935	free(range);
936	return 0;
937}
938
939
940static int hostap_wireless_event_init(struct hostap_driver_data *drv)
941{
942	struct netlink_config *cfg;
943
944	hostap_get_we_version(drv);
945
946	cfg = os_zalloc(sizeof(*cfg));
947	if (cfg == NULL)
948		return -1;
949	cfg->ctx = drv;
950	cfg->newlink_cb = hostapd_wireless_event_rtm_newlink;
951	drv->netlink = netlink_init(cfg);
952	if (drv->netlink == NULL) {
953		os_free(cfg);
954		return -1;
955	}
956
957	return 0;
958}
959
960
961static void * hostap_init(struct hostapd_data *hapd,
962			  struct wpa_init_params *params)
963{
964	struct hostap_driver_data *drv;
965
966	drv = os_zalloc(sizeof(struct hostap_driver_data));
967	if (drv == NULL) {
968		printf("Could not allocate memory for hostapd driver data\n");
969		return NULL;
970	}
971
972	drv->hapd = hapd;
973	drv->ioctl_sock = drv->sock = -1;
974	memcpy(drv->iface, params->ifname, sizeof(drv->iface));
975
976	drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
977	if (drv->ioctl_sock < 0) {
978		perror("socket[PF_INET,SOCK_DGRAM]");
979		free(drv);
980		return NULL;
981	}
982
983	if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
984		printf("Could not enable hostapd mode for interface %s\n",
985		       drv->iface);
986		close(drv->ioctl_sock);
987		free(drv);
988		return NULL;
989	}
990
991	if (hostap_init_sockets(drv, params->own_addr) ||
992	    hostap_wireless_event_init(drv)) {
993		close(drv->ioctl_sock);
994		free(drv);
995		return NULL;
996	}
997
998	return drv;
999}
1000
1001
1002static void hostap_driver_deinit(void *priv)
1003{
1004	struct hostap_driver_data *drv = priv;
1005
1006	netlink_deinit(drv->netlink);
1007	(void) hostap_set_iface_flags(drv, 0);
1008	(void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1009	(void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1010
1011	if (drv->ioctl_sock >= 0)
1012		close(drv->ioctl_sock);
1013
1014	if (drv->sock >= 0)
1015		close(drv->sock);
1016
1017	os_free(drv->generic_ie);
1018	os_free(drv->wps_ie);
1019
1020	free(drv);
1021}
1022
1023
1024static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
1025			     int reason)
1026{
1027	struct hostap_driver_data *drv = priv;
1028	struct ieee80211_mgmt mgmt;
1029
1030	if (is_broadcast_ether_addr(addr)) {
1031		/*
1032		 * New Prism2.5/3 STA firmware versions seem to have issues
1033		 * with this broadcast deauth frame. This gets the firmware in
1034		 * odd state where nothing works correctly, so let's skip
1035		 * sending this for the hostap driver.
1036		 */
1037		return 0;
1038	}
1039
1040	memset(&mgmt, 0, sizeof(mgmt));
1041	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1042					  WLAN_FC_STYPE_DEAUTH);
1043	memcpy(mgmt.da, addr, ETH_ALEN);
1044	memcpy(mgmt.sa, own_addr, ETH_ALEN);
1045	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1046	mgmt.u.deauth.reason_code = host_to_le16(reason);
1047	return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1048				sizeof(mgmt.u.deauth), 0);
1049}
1050
1051
1052static int hostap_set_freq(void *priv, struct hostapd_freq_params *freq)
1053{
1054	struct hostap_driver_data *drv = priv;
1055	struct iwreq iwr;
1056
1057	os_memset(&iwr, 0, sizeof(iwr));
1058	os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1059	iwr.u.freq.m = freq->channel;
1060	iwr.u.freq.e = 0;
1061
1062	if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
1063		perror("ioctl[SIOCSIWFREQ]");
1064		return -1;
1065	}
1066
1067	return 0;
1068}
1069
1070
1071static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
1072			       int reason)
1073{
1074	struct hostap_driver_data *drv = priv;
1075	struct ieee80211_mgmt mgmt;
1076
1077	memset(&mgmt, 0, sizeof(mgmt));
1078	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1079					  WLAN_FC_STYPE_DISASSOC);
1080	memcpy(mgmt.da, addr, ETH_ALEN);
1081	memcpy(mgmt.sa, own_addr, ETH_ALEN);
1082	memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1083	mgmt.u.disassoc.reason_code = host_to_le16(reason);
1084	return  hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
1085				 sizeof(mgmt.u.disassoc), 0);
1086}
1087
1088
1089static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1090							    u16 *num_modes,
1091							    u16 *flags)
1092{
1093	struct hostapd_hw_modes *mode;
1094	int i, clen, rlen;
1095	const short chan2freq[14] = {
1096		2412, 2417, 2422, 2427, 2432, 2437, 2442,
1097		2447, 2452, 2457, 2462, 2467, 2472, 2484
1098	};
1099
1100	mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1101	if (mode == NULL)
1102		return NULL;
1103
1104	*num_modes = 1;
1105	*flags = 0;
1106
1107	mode->mode = HOSTAPD_MODE_IEEE80211B;
1108	mode->num_channels = 14;
1109	mode->num_rates = 4;
1110
1111	clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1112	rlen = mode->num_rates * sizeof(int);
1113
1114	mode->channels = os_zalloc(clen);
1115	mode->rates = os_zalloc(rlen);
1116	if (mode->channels == NULL || mode->rates == NULL) {
1117		os_free(mode->channels);
1118		os_free(mode->rates);
1119		os_free(mode);
1120		return NULL;
1121	}
1122
1123	for (i = 0; i < 14; i++) {
1124		mode->channels[i].chan = i + 1;
1125		mode->channels[i].freq = chan2freq[i];
1126		/* TODO: Get allowed channel list from the driver */
1127		if (i >= 11)
1128			mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1129	}
1130
1131	mode->rates[0] = 10;
1132	mode->rates[1] = 20;
1133	mode->rates[2] = 55;
1134	mode->rates[3] = 110;
1135
1136	return mode;
1137}
1138
1139
1140static void wpa_driver_hostap_poll_client(void *priv, const u8 *own_addr,
1141					  const u8 *addr, int qos)
1142{
1143	struct ieee80211_hdr hdr;
1144
1145	os_memset(&hdr, 0, sizeof(hdr));
1146
1147	/*
1148	 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
1149	 * but it is apparently not retried so TX Exc events
1150	 * are not received for it.
1151	 * This is the reason the driver overrides the default
1152	 * handling.
1153	 */
1154	hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA,
1155					 WLAN_FC_STYPE_DATA);
1156
1157	hdr.frame_control |=
1158		host_to_le16(WLAN_FC_FROMDS);
1159	os_memcpy(hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
1160	os_memcpy(hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
1161	os_memcpy(hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
1162
1163	hostap_send_mlme(priv, (u8 *)&hdr, sizeof(hdr), 0);
1164}
1165
1166
1167const struct wpa_driver_ops wpa_driver_hostap_ops = {
1168	.name = "hostap",
1169	.desc = "Host AP driver (Intersil Prism2/2.5/3)",
1170	.set_key = wpa_driver_hostap_set_key,
1171	.hapd_init = hostap_init,
1172	.hapd_deinit = hostap_driver_deinit,
1173	.set_ieee8021x = hostap_set_ieee8021x,
1174	.set_privacy = hostap_set_privacy,
1175	.get_seqnum = hostap_get_seqnum,
1176	.flush = hostap_flush,
1177	.set_generic_elem = hostap_set_generic_elem,
1178	.read_sta_data = hostap_read_sta_data,
1179	.hapd_send_eapol = hostap_send_eapol,
1180	.sta_set_flags = hostap_sta_set_flags,
1181	.sta_deauth = hostap_sta_deauth,
1182	.sta_disassoc = hostap_sta_disassoc,
1183	.sta_remove = hostap_sta_remove,
1184	.hapd_set_ssid = hostap_set_ssid,
1185	.send_mlme = hostap_send_mlme,
1186	.sta_add = hostap_sta_add,
1187	.get_inact_sec = hostap_get_inact_sec,
1188	.sta_clear_stats = hostap_sta_clear_stats,
1189	.get_hw_feature_data = hostap_get_hw_feature_data,
1190	.set_ap_wps_ie = hostap_set_ap_wps_ie,
1191	.set_freq = hostap_set_freq,
1192	.poll_client = wpa_driver_hostap_poll_client,
1193};
1194