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