2 * hostapd / Driver interaction with Prism54 PIMFOR interface
3 * Copyright (c) 2004, Bell Kin <bell_kin@pek.com.tw>
4 * based on hostap driver.c, ieee802_11.c
5 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Alternatively, this software may be distributed under the terms of BSD
14 * See README and COPYING for more details.
18 #include <sys/ioctl.h>
19 #include <sys/select.h>
21 #ifdef USE_KERNEL_HEADERS
22 /* compat-wireless does not include linux/compiler.h to define __user, so
27 #include <asm/types.h>
28 #include <linux/if_packet.h>
29 #include <linux/if_ether.h> /* The L2 protocols */
30 #include <linux/if_arp.h>
31 #include <linux/wireless.h>
32 #else /* USE_KERNEL_HEADERS */
33 #include <net/if_arp.h>
34 #include <netpacket/packet.h>
35 #include "wireless_copy.h"
36 #endif /* USE_KERNEL_HEADERS */
41 #include "ieee802_1x.h"
43 #include "ieee802_11.h"
46 #include "radius/radius.h"
48 #include "accounting.h"
50 const int PIM_BUF_SIZE = 4096;
52 struct prism54_driver_data {
53 struct hostapd_data *hapd;
54 char iface[IFNAMSIZ + 1];
55 int sock; /* raw packet socket for 802.3 access */
56 int pim_sock; /* socket for pimfor packet */
61 static int mac_id_refresh(struct prism54_driver_data *data, int id, char *mac)
63 if (id < 0 || id > 2006) {
66 memcpy(&data->macs[id][0], mac, ETH_ALEN);
71 static char * mac_id_get(struct prism54_driver_data *data, int id)
73 if (id < 0 || id > 2006) {
76 return &data->macs[id][0];
80 /* wait for a specific pimfor, timeout in 10ms resolution */
81 /* pim_sock must be non-block to prevent dead lock from no response */
82 /* or same response type in series */
83 static int prism54_waitpim(void *priv, unsigned long oid, void *buf, int len,
86 struct prism54_driver_data *drv = priv;
87 struct timeval tv, stv, ctv;
97 gettimeofday(&stv, NULL);
99 FD_SET(drv->pim_sock, &pfd);
102 if (select(drv->pim_sock + 1, &pfd, NULL, NULL, &tv)) {
103 rlen = recv(drv->pim_sock, pkt, 8192, 0);
105 if (pkt->oid == htonl(oid)) {
108 memcpy(buf, pkt, rlen);
113 printf("buffer too small\n");
118 gettimeofday(&ctv, NULL);
123 gettimeofday(&ctv, NULL);
124 } while (((ctv.tv_sec - stv.tv_sec) * 100 +
125 (ctv.tv_usec - stv.tv_usec) / 10000) > timeout);
131 /* send an eapol packet */
132 static int prism54_send_eapol(void *priv, const u8 *addr,
133 const u8 *data, size_t data_len, int encrypt,
136 struct prism54_driver_data *drv = priv;
142 len = sizeof(*hdr) + data_len;
143 hdr = os_zalloc(len);
145 printf("malloc() failed for prism54_send_data(len=%lu)\n",
146 (unsigned long) len);
150 memcpy(&hdr->da[0], addr, ETH_ALEN);
151 memcpy(&hdr->sa[0], own_addr, ETH_ALEN);
152 hdr->type = htons(ETH_P_PAE);
153 pos = (u8 *) (hdr + 1);
154 memcpy(pos, data, data_len);
156 res = send(drv->sock, hdr, len, 0);
160 perror("hostapd_send_eapol: send");
161 printf("hostapd_send_eapol - packet len: %lu - failed\n",
162 (unsigned long) len);
169 /* open data channel(auth-1) or eapol only(unauth-0) */
170 static int prism54_set_sta_authorized(void *priv, const u8 *addr,
173 struct prism54_driver_data *drv = priv;
177 hdr = malloc(sizeof(*hdr) + ETH_ALEN);
180 hdr->op = htonl(PIMOP_SET);
182 hdr->oid = htonl(DOT11_OID_EAPAUTHSTA);
184 hdr->oid = htonl(DOT11_OID_EAPUNAUTHSTA);
186 pos = (char *) (hdr + 1);
187 memcpy(pos, addr, ETH_ALEN);
188 send(drv->pim_sock, hdr, sizeof(*hdr) + ETH_ALEN, 0);
189 prism54_waitpim(priv, hdr->oid, hdr, sizeof(*hdr) + ETH_ALEN, 10);
196 prism54_sta_set_flags(void *priv, const u8 *addr, int total_flags,
197 int flags_or, int flags_and)
199 /* For now, only support setting Authorized flag */
200 if (flags_or & WLAN_STA_AUTHORIZED)
201 return prism54_set_sta_authorized(priv, addr, 1);
202 if (flags_and & WLAN_STA_AUTHORIZED)
203 return prism54_set_sta_authorized(priv, addr, 0);
208 /* set per station key */
209 static int prism54_set_encryption(const char *ifname, void *priv,
210 const char *alg, const u8 *addr,
211 int idx, const u8 *key, size_t key_len,
214 struct prism54_driver_data *drv = priv;
216 struct obj_stakey *keys;
221 blen = sizeof(struct obj_stakey) + sizeof(pimdev_hdr);
224 printf("memory low\n");
227 keys = (struct obj_stakey *) &hdr[1];
229 memset(&keys->address[0], 0xff, ETH_ALEN);
231 memcpy(&keys->address[0], addr, ETH_ALEN);
233 if (!strcmp(alg, "WEP")) {
234 keys->type = DOT11_PRIV_WEP;
235 } else if (!strcmp(alg, "TKIP")) {
236 keys->type = DOT11_PRIV_TKIP;
237 } else if (!strcmp(alg, "none")) {
238 /* the only way to clear the key is to deauth it */
239 /* and prism54 is capable to receive unencrypted packet */
240 /* so we do nothing here */
244 printf("bad auth type: %s\n", alg);
246 buf = (u8 *) &keys->key[0];
247 keys->length = key_len;
249 keys->options = htons(DOT11_STAKEY_OPTION_DEFAULTKEY);
252 hdr->op = htonl(PIMOP_SET);
253 hdr->oid = htonl(DOT11_OID_STAKEY);
255 memcpy(buf, key, key_len);
257 ret = send(drv->pim_sock, hdr, blen, 0);
262 prism54_waitpim(priv, hdr->oid, hdr, blen, 10);
270 /* get TKIP station sequence counter, prism54 is only 6 bytes */
271 static int prism54_get_seqnum(const char *ifname, void *priv, const u8 *addr,
274 struct prism54_driver_data *drv = priv;
275 struct obj_stasc *stasc;
280 blen = sizeof(*stasc) + sizeof(*hdr);
285 stasc = (struct obj_stasc *) &hdr[1];
288 memset(&stasc->address[0], 0xff, ETH_ALEN);
290 memcpy(&stasc->address[0], addr, ETH_ALEN);
292 hdr->oid = htonl(DOT11_OID_STASC);
293 hdr->op = htonl(PIMOP_GET);
295 if (send(drv->pim_sock,hdr,blen,0) <= 0) {
299 if (prism54_waitpim(priv, DOT11_OID_STASC, hdr, blen, 10) <= 0) {
302 if (hdr->op == (int) htonl(PIMOP_RESPONSE)) {
303 memcpy(seq + 2, &stasc->sc_high, ETH_ALEN);
315 /* include unencrypted, set mlme autolevel to extended */
316 static int prism54_init_1x(void *priv)
318 struct prism54_driver_data *drv = priv;
321 int blen = sizeof(*hdr) + sizeof(*ul);
327 ul = (unsigned long *) &hdr[1];
328 hdr->op = htonl(PIMOP_SET);
329 hdr->oid = htonl(DOT11_OID_EXUNENCRYPTED);
330 *ul = htonl(DOT11_BOOL_TRUE); /* not accept */
331 send(drv->pim_sock, hdr, blen, 0);
332 prism54_waitpim(priv, DOT11_OID_EXUNENCRYPTED, hdr, blen, 10);
333 hdr->op = htonl(PIMOP_SET);
334 hdr->oid = htonl(DOT11_OID_MLMEAUTOLEVEL);
335 *ul = htonl(DOT11_MLME_EXTENDED);
336 send(drv->pim_sock, hdr, blen, 0);
337 prism54_waitpim(priv, DOT11_OID_MLMEAUTOLEVEL, hdr, blen, 10);
338 hdr->op = htonl(PIMOP_SET);
339 hdr->oid = htonl(DOT11_OID_DOT1XENABLE);
340 *ul = htonl(DOT11_BOOL_TRUE);
341 send(drv->pim_sock, hdr, blen, 0);
342 prism54_waitpim(priv, DOT11_OID_DOT1XENABLE, hdr, blen, 10);
343 hdr->op = htonl(PIMOP_SET);
344 hdr->oid = htonl(DOT11_OID_AUTHENABLE);
345 *ul = htonl(DOT11_AUTH_OS); /* OS */
346 send(drv->pim_sock, hdr, blen, 0);
347 prism54_waitpim(priv, DOT11_OID_AUTHENABLE, hdr, blen, 10);
353 static int prism54_set_privacy_invoked(const char *ifname, void *priv,
356 struct prism54_driver_data *drv = priv;
360 int blen = sizeof(*hdr) + sizeof(*ul);
364 ul = (unsigned long *) &hdr[1];
365 hdr->op = htonl(PIMOP_SET);
366 hdr->oid = htonl(DOT11_OID_PRIVACYINVOKED);
368 *ul = htonl(DOT11_BOOL_TRUE); /* has privacy */
372 ret = send(drv->pim_sock, hdr, blen, 0);
374 ret = prism54_waitpim(priv, DOT11_OID_PRIVACYINVOKED, hdr,
382 static int prism54_ioctl_setiwessid(const char *ifname, void *priv,
383 const u8 *buf, int len)
386 struct prism54_driver_data *drv = priv;
389 memset(&iwr, 0, sizeof(iwr));
390 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
391 iwr.u.essid.flags = 1; /* SSID active */
392 iwr.u.essid.pointer = (caddr_t) buf;
393 iwr.u.essid.length = len + 1;
395 if (ioctl(drv->pim_sock, SIOCSIWESSID, &iwr) < 0) {
396 perror("ioctl[SIOCSIWESSID]");
397 printf("len=%d\n", len);
405 /* kick all stations */
406 /* does not work during init, but at least it won't crash firmware */
407 static int prism54_flush(void *priv)
409 struct prism54_driver_data *drv = priv;
410 struct obj_mlmeex *mlme;
415 int blen = sizeof(*hdr) + sizeof(*mlme);
418 hdr = os_zalloc(blen);
422 mlme = (struct obj_mlmeex *) &hdr[1];
423 nsta = (long *) &hdr[1];
424 hdr->op = htonl(PIMOP_GET);
425 hdr->oid = htonl(DOT11_OID_CLIENTS);
426 ret = send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(long), 0);
427 ret = prism54_waitpim(priv, DOT11_OID_CLIENTS, hdr, blen, 10);
428 if ((ret < 0) || (hdr->op != (int) htonl(PIMOP_RESPONSE)) ||
429 (le_to_host32(*nsta) > 2007)) {
433 for (i = 0; i < le_to_host32(*nsta); i++) {
435 mac_id = mac_id_get(drv, i);
437 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
438 mlme->code = host_to_le16(WLAN_REASON_UNSPECIFIED);
439 mlme->state = htons(DOT11_STATE_NONE);
441 hdr->op = htonl(PIMOP_SET);
442 hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
443 ret = send(drv->pim_sock, hdr, blen, 0);
444 prism54_waitpim(priv, DOT11_OID_DISASSOCIATEEX, hdr, blen,
447 for (i = 0; i < le_to_host32(*nsta); i++) {
449 mac_id = mac_id_get(drv, i);
451 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
452 mlme->code = host_to_le16(WLAN_REASON_UNSPECIFIED);
453 mlme->state = htons(DOT11_STATE_NONE);
455 hdr->op = htonl(PIMOP_SET);
456 hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
457 ret = send(drv->pim_sock, hdr, blen, 0);
458 prism54_waitpim(priv, DOT11_OID_DEAUTHENTICATEEX, hdr, blen,
466 static int prism54_sta_deauth(void *priv, const u8 *addr, int reason)
468 struct prism54_driver_data *drv = priv;
470 struct obj_mlmeex *mlme;
472 int blen = sizeof(*hdr) + sizeof(*mlme);
476 mlme = (struct obj_mlmeex *) &hdr[1];
477 hdr->op = htonl(PIMOP_SET);
478 hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
479 memcpy(&mlme->address[0], addr, ETH_ALEN);
481 mlme->state = htons(DOT11_STATE_NONE);
482 mlme->code = host_to_le16(reason);
484 ret = send(drv->pim_sock, hdr, blen, 0);
485 prism54_waitpim(priv, DOT11_OID_DEAUTHENTICATEEX, hdr, blen, 10);
491 static int prism54_sta_disassoc(void *priv, const u8 *addr, int reason)
493 struct prism54_driver_data *drv = priv;
495 struct obj_mlmeex *mlme;
497 int blen = sizeof(*hdr) + sizeof(*mlme);
501 mlme = (struct obj_mlmeex *) &hdr[1];
502 hdr->op = htonl(PIMOP_SET);
503 hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
504 memcpy(&mlme->address[0], addr, ETH_ALEN);
506 mlme->state = htons(DOT11_STATE_NONE);
507 mlme->code = host_to_le16(reason);
509 ret = send(drv->pim_sock, hdr, blen, 0);
510 prism54_waitpim(priv, DOT11_OID_DISASSOCIATEEX, hdr, blen, 10);
516 static int prism54_get_inact_sec(void *priv, const u8 *addr)
518 struct prism54_driver_data *drv = priv;
521 int blen = sizeof(*hdr) + sizeof(*sta);
527 hdr->op = htonl(PIMOP_GET);
528 hdr->oid = htonl(DOT11_OID_CLIENTFIND);
529 sta = (struct obj_sta *) &hdr[1];
530 memcpy(&sta->address[0], addr, ETH_ALEN);
531 ret = send(drv->pim_sock, hdr, blen, 0);
532 ret = prism54_waitpim(priv, DOT11_OID_CLIENTFIND, hdr, blen, 10);
534 printf("get_inact_sec: bad return %d\n", ret);
538 if (hdr->op != (int) htonl(PIMOP_RESPONSE)) {
539 printf("get_inact_sec: bad resp\n");
544 return le_to_host16(sta->age);
548 /* set attachments */
549 static int prism54_set_generic_elem(const char *ifname, void *priv,
550 const u8 *elem, size_t elem_len)
552 struct prism54_driver_data *drv = priv;
555 struct obj_attachment_hdr *attach;
556 size_t blen = sizeof(*hdr) + sizeof(*attach) + elem_len;
557 hdr = os_zalloc(blen);
559 printf("%s: memory low\n", __func__);
562 hdr->op = htonl(PIMOP_SET);
563 hdr->oid = htonl(DOT11_OID_ATTACHMENT);
564 attach = (struct obj_attachment_hdr *)&hdr[1];
565 attach->type = DOT11_PKT_BEACON;
567 attach->size = host_to_le16((short)elem_len);
568 pos = ((char*) attach) + sizeof(*attach);
570 memcpy(pos, elem, elem_len);
571 send(drv->pim_sock, hdr, blen, 0);
572 attach->type = DOT11_PKT_PROBE_RESP;
573 send(drv->pim_sock, hdr, blen, 0);
579 /* tell the card to auth the sta */
580 static void prism54_handle_probe(struct prism54_driver_data *drv,
581 void *buf, size_t len)
583 struct obj_mlmeex *mlme;
585 struct sta_info *sta;
586 hdr = (pimdev_hdr *)buf;
587 mlme = (struct obj_mlmeex *) &hdr[1];
588 sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
590 if (sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC))
593 if (len < sizeof(*mlme)) {
594 printf("bad probe packet\n");
597 mlme->state = htons(DOT11_STATE_AUTHING);
599 hdr->op = htonl(PIMOP_SET);
600 hdr->oid = htonl(DOT11_OID_AUTHENTICATEEX);
602 send(drv->pim_sock, hdr, sizeof(*hdr)+sizeof(*mlme), 0);
606 static void prism54_handle_deauth(struct prism54_driver_data *drv,
607 void *buf, size_t len)
609 struct obj_mlme *mlme;
611 struct sta_info *sta;
614 hdr = (pimdev_hdr *) buf;
615 mlme = (struct obj_mlme *) &hdr[1];
616 sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
617 mac_id = mac_id_get(drv, mlme->id);
618 if (sta == NULL || mac_id == NULL)
620 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
621 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
622 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
623 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
624 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
625 ap_free_sta(drv->hapd, sta);
629 static void prism54_handle_disassoc(struct prism54_driver_data *drv,
630 void *buf, size_t len)
632 struct obj_mlme *mlme;
634 struct sta_info *sta;
637 hdr = (pimdev_hdr *) buf;
638 mlme = (struct obj_mlme *) &hdr[1];
639 mac_id = mac_id_get(drv, mlme->id);
642 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
643 sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
647 sta->flags &= ~WLAN_STA_ASSOC;
648 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
649 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
650 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
651 accounting_sta_stop(drv->hapd, sta);
652 ieee802_1x_free_station(sta);
656 /* to auth it, just allow it now, later for os/sk */
657 static void prism54_handle_auth(struct prism54_driver_data *drv,
658 void *buf, size_t len)
660 struct obj_mlmeex *mlme;
662 struct sta_info *sta;
665 hdr = (pimdev_hdr *) buf;
666 mlme = (struct obj_mlmeex *) &hdr[1];
667 if (len < sizeof(*mlme)) {
668 printf("bad auth packet\n");
672 if (mlme->state == htons(DOT11_STATE_AUTHING)) {
673 sta = ap_sta_add(drv->hapd, (u8 *) &mlme->address[0]);
674 if (drv->hapd->tkip_countermeasures) {
675 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
678 mac_id_refresh(drv, mlme->id, &mlme->address[0]);
680 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
683 sta->flags &= ~WLAN_STA_PREAUTH;
685 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
686 sta->flags |= WLAN_STA_AUTH;
687 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
689 mlme->state=htons(DOT11_STATE_AUTH);
690 hdr->op = htonl(PIMOP_SET);
691 hdr->oid = htonl(DOT11_OID_AUTHENTICATEEX);
693 sta->timeout_next = STA_NULLFUNC;
694 send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
699 printf("auth fail: %x\n", resp);
700 mlme->code = host_to_le16(resp);
703 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
704 hdr->oid = htonl(DOT11_OID_DEAUTHENTICATEEX);
705 hdr->op = htonl(PIMOP_SET);
706 send(drv->pim_sock, hdr, sizeof(*hdr)+sizeof(*mlme), 0);
710 /* do the wpa thing */
711 static void prism54_handle_assoc(struct prism54_driver_data *drv,
712 void *buf, size_t len)
715 struct obj_mlmeex *mlme;
716 struct ieee802_11_elems elems;
717 struct sta_info *sta;
726 hdr = (pimdev_hdr *) buf;
727 mlme = (struct obj_mlmeex *) &hdr[1];
728 switch (ntohl(hdr->oid)) {
729 case DOT11_OID_ASSOCIATE:
730 case DOT11_OID_REASSOCIATE:
735 if ((mlme->state == (int) htonl(DOT11_STATE_ASSOCING)) ||
736 (mlme->state == (int) htonl(DOT11_STATE_REASSOCING))) {
737 if (len < sizeof(pimdev_hdr) + sizeof(struct obj_mlme)) {
738 printf("bad assoc packet\n");
741 mac_id = mac_id_get(drv, mlme->id);
744 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
745 sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
747 printf("cannot get sta\n");
750 cb = (u8 *) &mlme->data[0];
751 if (hdr->oid == htonl(DOT11_OID_ASSOCIATEEX)) {
753 } else if (hdr->oid == htonl(DOT11_OID_REASSOCIATEEX)) {
756 if (le_to_host16(mlme->size) <= ieofs) {
757 printf("attach too small\n");
758 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
761 if (ieee802_11_parse_elems(cb + ieofs,
762 le_to_host16(mlme->size) - ieofs,
763 &elems, 1) == ParseFailed) {
764 printf("STA " MACSTR " sent invalid association "
765 "request\n", MAC2STR(sta->addr));
766 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
769 if ((drv->hapd->conf->wpa & WPA_PROTO_RSN) &&
771 wpa_ie = elems.rsn_ie;
772 wpa_ie_len = elems.rsn_ie_len;
773 } else if ((drv->hapd->conf->wpa & WPA_PROTO_WPA) &&
775 wpa_ie = elems.wpa_ie;
776 wpa_ie_len = elems.wpa_ie_len;
781 if (drv->hapd->conf->wpa && wpa_ie == NULL) {
782 printf("STA " MACSTR ": No WPA/RSN IE in association "
783 "request\n", MAC2STR(sta->addr));
784 resp = WLAN_STATUS_INVALID_IE;
787 if (drv->hapd->conf->wpa) {
791 if (sta->wpa_sm == NULL)
792 sta->wpa_sm = wpa_auth_sta_init(
793 drv->hapd->wpa_auth, sta->addr);
794 if (sta->wpa_sm == NULL) {
795 printf("Failed to initialize WPA state "
797 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
800 res = wpa_validate_wpa_ie(drv->hapd->wpa_auth,
804 if (res == WPA_INVALID_GROUP)
805 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
806 else if (res == WPA_INVALID_PAIRWISE)
807 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
808 else if (res == WPA_INVALID_AKMP)
809 resp = WLAN_STATUS_AKMP_NOT_VALID;
810 else if (res == WPA_ALLOC_FAIL)
811 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
812 else if (res != WPA_IE_OK)
813 resp = WLAN_STATUS_INVALID_IE;
814 if (resp != WLAN_STATUS_SUCCESS)
817 hdr->oid = (hdr->oid == htonl(DOT11_OID_ASSOCIATEEX)) ?
818 htonl(DOT11_OID_ASSOCIATEEX) :
819 htonl(DOT11_OID_REASSOCIATEEX);
820 hdr->op = htonl(PIMOP_SET);
822 mlme->state = htons(DOT11_STATE_ASSOC);
824 send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
826 } else if (mlme->state==htons(DOT11_STATE_ASSOC)) {
827 if (len < sizeof(pimdev_hdr) + sizeof(struct obj_mlme)) {
828 printf("bad assoc packet\n");
831 mac_id = mac_id_get(drv, mlme->id);
834 memcpy(&mlme->address[0], mac_id, ETH_ALEN);
835 sta = ap_get_sta(drv->hapd, (u8 *) &mlme->address[0]);
837 printf("cannot get sta\n");
840 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
841 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
842 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
843 hostapd_new_assoc_sta(drv->hapd, sta, !new_assoc);
844 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
845 sta->timeout_next = STA_NULLFUNC;
851 printf("Prism54: assoc fail: %x\n", resp);
852 mlme->code = host_to_le16(resp);
854 mlme->state = htons(DOT11_STATE_ASSOCING);
855 hdr->oid = htonl(DOT11_OID_DISASSOCIATEEX);
856 hdr->op = htonl(PIMOP_SET);
857 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
858 send(drv->pim_sock, hdr, sizeof(*hdr) + sizeof(*mlme), 0);
862 static void handle_pim(int sock, void *eloop_ctx, void *sock_ctx)
864 struct prism54_driver_data *drv = eloop_ctx;
868 hdr = malloc(PIM_BUF_SIZE);
871 len = recv(sock, hdr, PIM_BUF_SIZE, 0);
878 printf("handle_pim: too short (%d)\n", len);
883 if (hdr->op != (int) htonl(PIMOP_TRAP)) {
887 switch (ntohl(hdr->oid)) {
888 case DOT11_OID_PROBE:
889 prism54_handle_probe(drv, hdr, len);
891 case DOT11_OID_DEAUTHENTICATEEX:
892 case DOT11_OID_DEAUTHENTICATE:
893 prism54_handle_deauth(drv, hdr, len);
895 case DOT11_OID_DISASSOCIATEEX:
896 case DOT11_OID_DISASSOCIATE:
897 prism54_handle_disassoc(drv, hdr, len);
899 case DOT11_OID_AUTHENTICATEEX:
900 case DOT11_OID_AUTHENTICATE:
901 prism54_handle_auth(drv, hdr, len);
903 case DOT11_OID_ASSOCIATEEX:
904 case DOT11_OID_REASSOCIATEEX:
905 case DOT11_OID_ASSOCIATE:
906 case DOT11_OID_REASSOCIATE:
907 prism54_handle_assoc(drv, hdr, len);
916 static void handle_802_3(int sock, void *eloop_ctx, void *sock_ctx)
918 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
922 hdr = malloc(PIM_BUF_SIZE);
925 len = recv(sock, hdr, PIM_BUF_SIZE, 0);
932 wpa_printf(MSG_MSGDUMP, "handle_802_3: too short (%d)", len);
936 if (hdr->type == htons(ETH_P_PAE)) {
937 hostapd_eapol_receive(hapd, (u8 *) &hdr->sa[0], (u8 *) &hdr[1],
944 static int prism54_init_sockets(struct prism54_driver_data *drv)
946 struct hostapd_data *hapd = drv->hapd;
948 struct sockaddr_ll addr;
950 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
952 perror("socket[PF_PACKET,SOCK_RAW]");
956 if (eloop_register_read_sock(drv->sock, handle_802_3, drv->hapd, NULL))
958 printf("Could not register read socket\n");
962 memset(&ifr, 0, sizeof(ifr));
963 if (hapd->conf->bridge[0] != '\0') {
964 printf("opening bridge: %s\n", hapd->conf->bridge);
965 os_strlcpy(ifr.ifr_name, hapd->conf->bridge,
966 sizeof(ifr.ifr_name));
968 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
970 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
971 perror("ioctl(SIOCGIFINDEX)");
975 memset(&addr, 0, sizeof(addr));
976 addr.sll_family = AF_PACKET;
977 addr.sll_ifindex = ifr.ifr_ifindex;
978 addr.sll_protocol = htons(ETH_P_PAE);
979 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
982 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
987 memset(&ifr, 0, sizeof(ifr));
988 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
989 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
990 perror("ioctl(SIOCGIFHWADDR)");
994 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
995 printf("Invalid HW-addr family 0x%04x\n",
996 ifr.ifr_hwaddr.sa_family);
999 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
1001 drv->pim_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1002 if (drv->pim_sock < 0) {
1003 perror("socket[PF_PACKET,SOCK_RAW]");
1007 if (eloop_register_read_sock(drv->pim_sock, handle_pim, drv, NULL)) {
1008 printf("Could not register read socket\n");
1012 memset(&ifr, 0, sizeof(ifr));
1013 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
1014 if (ioctl(drv->pim_sock, SIOCGIFINDEX, &ifr) != 0) {
1015 perror("ioctl(SIOCGIFINDEX)");
1019 memset(&addr, 0, sizeof(addr));
1020 addr.sll_family = AF_PACKET;
1021 addr.sll_ifindex = ifr.ifr_ifindex;
1022 addr.sll_protocol = htons(ETH_P_ALL);
1023 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
1026 if (bind(drv->pim_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1035 static void * prism54_driver_init(struct hostapd_data *hapd)
1037 struct prism54_driver_data *drv;
1039 drv = os_zalloc(sizeof(struct prism54_driver_data));
1041 printf("Could not allocate memory for hostapd Prism54 driver "
1047 drv->pim_sock = drv->sock = -1;
1048 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1050 if (prism54_init_sockets(drv)) {
1054 prism54_init_1x(drv);
1055 /* must clean previous elems */
1056 prism54_set_generic_elem(drv->iface, drv, NULL, 0);
1062 static void prism54_driver_deinit(void *priv)
1064 struct prism54_driver_data *drv = priv;
1066 if (drv->pim_sock >= 0)
1067 close(drv->pim_sock);
1076 const struct wpa_driver_ops wpa_driver_prism54_ops = {
1078 .init = prism54_driver_init,
1079 .deinit = prism54_driver_deinit,
1080 /* .set_ieee8021x = prism54_init_1x, */
1081 .set_privacy = prism54_set_privacy_invoked,
1082 .set_encryption = prism54_set_encryption,
1083 .get_seqnum = prism54_get_seqnum,
1084 .flush = prism54_flush,
1085 .set_generic_elem = prism54_set_generic_elem,
1086 .send_eapol = prism54_send_eapol,
1087 .sta_set_flags = prism54_sta_set_flags,
1088 .sta_deauth = prism54_sta_deauth,
1089 .sta_disassoc = prism54_sta_disassoc,
1090 .set_ssid = prism54_ioctl_setiwessid,
1091 .get_inact_sec = prism54_get_inact_sec,