3 * Thomas Skibo <skibo@pacbell.net>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Thomas Skibo.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Thomas Skibo AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Thomas Skibo OR HIS DRINKING PALS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/dev/wi/wi_hostap.c,v 1.7.2.4 2002/08/02 07:11:34 imp Exp $
33 * $DragonFly: src/sys/dev/netif/wi/Attic/wi_hostap.c,v 1.3 2003/06/23 17:55:37 dillon Exp $
36 /* This is experimental Host AP software for Prism 2 802.11b interfaces.
38 * Much of this is based upon the "Linux Host AP driver Host AP driver
39 * for Intersil Prism2" by Jouni Malinen <jkm@ssh.com> or <jkmaline@cc.hut.fi>.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #if __FreeBSD_version >= 500033
45 #include <sys/endian.h>
47 #include <sys/sockio.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
52 #include <sys/ucred.h>
53 #include <sys/socket.h>
54 #include <sys/module.h>
55 #include <sys/queue.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
62 #include <machine/clock.h>
63 #include <machine/md_var.h>
64 #include <machine/bus_pio.h>
68 #include <net/if_arp.h>
69 #include <net/ethernet.h>
70 #include <net/if_dl.h>
71 #include <net/if_media.h>
72 #include <net/if_types.h>
73 #include <net/if_ieee80211.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_ether.h>
81 #include <dev/wi/if_wavelan_ieee.h>
82 #include <dev/wi/wi_hostap.h>
83 #include <dev/wi/if_wivar.h>
84 #include <dev/wi/if_wireg.h>
86 MALLOC_DEFINE(M_HAP_STA, "hostap_sta", "if_wi host AP mode station entry");
88 static void wihap_sta_timeout(void *v);
89 static struct wihap_sta_info *wihap_sta_alloc(struct wi_softc *sc,
91 static void wihap_sta_delete(struct wihap_sta_info *sta);
92 static struct wihap_sta_info *wihap_sta_find(struct wihap_info *whi,
94 static int wihap_sta_is_assoc(struct wihap_info *whi, u_int8_t addr[]);
95 static void wihap_auth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
96 caddr_t pkt, int len);
97 static void wihap_sta_deauth(struct wi_softc *sc, u_int8_t sta_addr[],
99 static void wihap_deauth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
100 caddr_t pkt, int len);
101 static void wihap_assoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
102 caddr_t pkt, int len);
103 static void wihap_sta_disassoc(struct wi_softc *sc, u_int8_t sta_addr[],
105 static void wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
106 caddr_t pkt, int len);
109 * Spl use in this driver.
111 * splnet is used everywhere here to block timeouts when we need to do
118 * Used for parsing management frames. The pkt pointer and length
119 * variables are updated after the value is removed.
121 static __inline u_int16_t
122 take_hword(caddr_t *ppkt, int *plen)
124 u_int16_t s = le16toh(* (u_int16_t *) *ppkt);
125 *ppkt += sizeof(u_int16_t);
126 *plen -= sizeof(u_int16_t);
132 * Parse out TLV element from a packet, check for underflow of packet
133 * or overflow of buffer, update pkt/len.
136 take_tlv(caddr_t *ppkt, int *plen, int id_expect, void *dst, int maxlen)
143 id = ((u_int8_t *)*ppkt)[0];
144 len = ((u_int8_t *)*ppkt)[1];
146 if (id != id_expect || *plen < len+2 || maxlen < len)
149 bcopy(*ppkt + 2, dst, len);
157 * Put half-word element into management frames.
160 put_hword(caddr_t *ppkt, u_int16_t s)
162 * (u_int16_t *) *ppkt = htole16(s);
163 *ppkt += sizeof(u_int16_t);
167 * Put TLV elements into management frames.
170 put_tlv(caddr_t *ppkt, u_int8_t id, void *src, u_int8_t len)
174 bcopy(src, (*ppkt) + 2, len);
179 put_rates(caddr_t *ppkt, u_int16_t rates)
184 if (rates & WI_SUPPRATES_1M)
185 ratebuf[len++] = 0x82;
186 if (rates & WI_SUPPRATES_2M)
187 ratebuf[len++] = 0x84;
188 if (rates & WI_SUPPRATES_5M)
189 ratebuf[len++] = 0x8b;
190 if (rates & WI_SUPPRATES_11M)
191 ratebuf[len++] = 0x96;
193 put_tlv(ppkt, IEEE80211_ELEMID_RATES, ratebuf, len);
199 * Initialize host AP data structures. Called even if port type is
203 wihap_init(struct wi_softc *sc)
206 struct wihap_info *whi = &sc->wi_hostap_info;
208 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
209 printf("wihap_init: sc=0x%x whi=0x%x\n", (int)sc, (int)whi);
211 bzero(whi, sizeof(struct wihap_info));
213 if (sc->wi_ptype != WI_PORTTYPE_AP)
216 whi->apflags = WIHAPFL_ACTIVE;
218 LIST_INIT(&whi->sta_list);
219 for (i = 0; i < WI_STA_HASH_SIZE; i++)
220 LIST_INIT(&whi->sta_hash[i]);
222 whi->inactivity_time = WIHAP_DFLT_INACTIVITY_TIME;
225 /* wihap_sta_disassoc()
227 * Send a disassociation frame to a specified station.
230 wihap_sta_disassoc(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
232 struct wi_80211_hdr *resp_hdr;
235 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
236 printf("Sending disassoc to sta %6D\n", sta_addr, ":");
238 /* Send disassoc packet. */
239 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
240 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
241 resp_hdr->frame_ctl = WI_FTYPE_MGMT | WI_STYPE_MGMT_DISAS;
242 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
244 bcopy(sta_addr, resp_hdr->addr1, ETHER_ADDR_LEN);
245 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
246 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
248 put_hword(&pkt, reason);
250 wi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
253 /* wihap_sta_deauth()
255 * Send a deauthentication message to a specified station.
258 wihap_sta_deauth(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
260 struct wi_80211_hdr *resp_hdr;
263 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
264 printf("Sending deauth to sta %6D\n", sta_addr, ":");
266 /* Send deauth packet. */
267 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
268 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
269 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_DEAUTH);
270 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
272 bcopy(sta_addr, resp_hdr->addr1, ETHER_ADDR_LEN);
273 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
274 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
276 put_hword(&pkt, reason);
278 wi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
283 * Disassociate all stations and free up data structures.
286 wihap_shutdown(struct wi_softc *sc)
288 struct wihap_info *whi = &sc->wi_hostap_info;
289 struct wihap_sta_info *sta, *next;
292 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
293 printf("wihap_shutdown: sc=0x%x whi=0x%x\n",
296 if (!(whi->apflags & WIHAPFL_ACTIVE))
299 /* XXX: I read somewhere you can deauth all the stations with
300 * a single broadcast. Maybe try that someday.
304 sta = LIST_FIRST(&whi->sta_list);
306 untimeout(wihap_sta_timeout, sta, sta->tmo);
308 /* Disassociate station. */
309 if (sta->flags & WI_SIFLAGS_ASSOC)
310 wihap_sta_disassoc(sc, sta->addr,
311 IEEE80211_REASON_ASSOC_LEAVE);
312 /* Deauth station. */
313 if (sta->flags & WI_SIFLAGS_AUTHEN)
314 wihap_sta_deauth(sc, sta->addr,
315 IEEE80211_REASON_AUTH_LEAVE);
318 /* Delete the structure. */
319 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
320 printf("wihap_shutdown: FREE(sta=0x%x)\n", (int)sta);
321 next = LIST_NEXT(sta, list);
322 FREE(sta, M_HAP_STA);
331 * Hash function for finding stations from ethernet address.
334 sta_hash_func(u_int8_t addr[])
336 return ((addr[3] + addr[4] + addr[5]) % WI_STA_HASH_SIZE);
339 /* addr_cmp(): Maybe this is a faster way to compare addresses? */
341 addr_cmp(u_int8_t a[], u_int8_t b[])
343 return (*(u_int16_t *)(a + 4) == *(u_int16_t *)(b + 4) &&
344 *(u_int32_t *)(a ) == *(u_int32_t *)(b));
348 wihap_sta_timeout(void *v)
350 struct wihap_sta_info *sta = v;
351 struct wi_softc *sc = sta->sc;
352 struct wihap_info *whi = &sc->wi_hostap_info;
356 if (sta->flags & WI_SIFLAGS_ASSOC) {
357 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
358 device_printf(sc->dev, "inactivity disassoc: %6D\n",
361 /* Disassoc station. */
362 wihap_sta_disassoc(sc, sta->addr,
363 IEEE80211_REASON_ASSOC_EXPIRE);
364 sta->flags &= ~WI_SIFLAGS_ASSOC;
366 sta->tmo = timeout(wihap_sta_timeout, sta,
367 hz * whi->inactivity_time);
369 } else if (sta->flags & WI_SIFLAGS_AUTHEN) {
371 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
372 device_printf(sc->dev, "inactivity disassoc: %6D\n",
375 /* Deauthenticate station. */
376 wihap_sta_deauth(sc, sta->addr, IEEE80211_REASON_AUTH_EXPIRE);
377 sta->flags &= ~WI_SIFLAGS_AUTHEN;
379 /* Delete the station if it's not permanent. */
380 if (!(sta->flags & WI_SIFLAGS_PERM))
381 wihap_sta_delete(sta);
386 /* wihap_sta_delete()
387 * Delete a single station and free up its data structure.
390 wihap_sta_delete(struct wihap_sta_info *sta)
392 struct wi_softc *sc = sta->sc;
393 struct wihap_info *whi = &sc->wi_hostap_info;
394 int i = sta->asid - 0xc001;
396 untimeout(wihap_sta_timeout, sta, sta->tmo);
398 whi->asid_inuse_mask[i >> 4] &= ~(1UL << (i & 0xf));
400 LIST_REMOVE(sta, list);
401 LIST_REMOVE(sta, hash);
403 FREE(sta->challenge, M_TEMP);
404 FREE(sta, M_HAP_STA);
410 * Create a new station data structure and put it in the list
413 static struct wihap_sta_info *
414 wihap_sta_alloc(struct wi_softc *sc, u_int8_t *addr)
416 struct wihap_info *whi = &sc->wi_hostap_info;
417 struct wihap_sta_info *sta;
418 int i, hash = sta_hash_func(addr);
420 /* Allocate structure. */
421 MALLOC(sta, struct wihap_sta_info *, sizeof(struct wihap_sta_info),
422 M_HAP_STA, M_NOWAIT);
426 bzero(sta, sizeof(struct wihap_sta_info));
428 /* Allocate an ASID. */
430 while (whi->asid_inuse_mask[i >> 4] & (1UL << (i & 0xf)))
431 i = (i == (WI_STA_HASH_SIZE << 4) - 1) ? 0 : (i + 1);
432 whi->asid_inuse_mask[i >> 4] |= (1UL << (i & 0xf));
433 sta->asid = 0xc001 + i;
435 /* Insert in list and hash list. */
436 LIST_INSERT_HEAD(&whi->sta_list, sta, list);
437 LIST_INSERT_HEAD(&whi->sta_hash[hash], sta, hash);
441 bcopy(addr, &sta->addr, ETHER_ADDR_LEN);
448 * Find station structure given address.
450 static struct wihap_sta_info *
451 wihap_sta_find(struct wihap_info *whi, u_int8_t *addr)
454 struct wihap_sta_info *sta;
456 i = sta_hash_func(addr);
457 LIST_FOREACH(sta, &whi->sta_hash[i], hash)
458 if (addr_cmp(addr,sta->addr))
465 wihap_check_rates(struct wihap_sta_info *sta, u_int8_t rates[], int rates_len)
467 struct wi_softc *sc = sta->sc;
471 sta->tx_max_rate = 0;
472 for (i=0; i<rates_len; i++)
473 switch (rates[i] & 0x7f) {
475 sta->rates |= WI_SUPPRATES_1M;
478 sta->rates |= WI_SUPPRATES_2M;
479 if (sta->tx_max_rate<1)
480 sta->tx_max_rate = 1;
483 sta->rates |= WI_SUPPRATES_5M;
484 if (sta->tx_max_rate<2)
485 sta->tx_max_rate = 2;
488 sta->rates |= WI_SUPPRATES_11M;
489 sta->tx_max_rate = 3;
493 sta->rates &= sc->wi_supprates;
494 sta->tx_curr_rate = sta->tx_max_rate;
496 return (sta->rates == 0 ? -1 : 0);
502 * Handle incoming authentication request. Only handle OPEN
506 wihap_auth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
507 caddr_t pkt, int len)
509 struct wihap_info *whi = &sc->wi_hostap_info;
510 struct wihap_sta_info *sta;
515 int i, challenge_len;
516 u_int32_t challenge[32];
518 struct wi_80211_hdr *resp_hdr;
523 /* Break open packet. */
524 algo = take_hword(&pkt, &len);
525 seq = take_hword(&pkt, &len);
526 status = take_hword(&pkt, &len);
528 if (len > 0 && (challenge_len = take_tlv(&pkt, &len,
529 IEEE80211_ELEMID_CHALLENGE, challenge, sizeof(challenge))) < 0)
532 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
533 printf("wihap_auth_req: station %6D algo=0x%x seq=0x%x\n",
534 rxfrm->wi_addr2, ":", algo, seq);
536 /* Find or create station info. */
537 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
540 /* Are we allowing new stations?
542 if (whi->apflags & WIHAPFL_MAC_FILT) {
543 status = IEEE80211_STATUS_OTHER; /* XXX */
547 /* Check for too many stations.
549 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
550 status = IEEE80211_STATUS_TOO_MANY_STATIONS;
554 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
555 printf("wihap_auth_req: new station\n");
557 /* Create new station. */
558 sta = wihap_sta_alloc(sc, rxfrm->wi_addr2);
561 status = IEEE80211_STATUS_TOO_MANY_STATIONS;
566 /* Note: it's okay to leave the station info structure around
567 * if the authen fails. It'll be timed out eventually.
570 case IEEE80211_AUTH_ALG_OPEN:
571 if (sc->wi_authmode != IEEE80211_AUTH_OPEN) {
573 status = IEEE80211_STATUS_ALG;
578 status = IEEE80211_STATUS_SEQUENCE;
583 sta->flags |= WI_SIFLAGS_AUTHEN;
585 case IEEE80211_AUTH_ALG_SHARED:
586 if (sc->wi_authmode != IEEE80211_AUTH_SHARED) {
588 status = IEEE80211_STATUS_ALG;
593 /* Create a challenge frame. */
594 if (!sta->challenge) {
595 MALLOC(sta->challenge, u_int32_t *, 128,
600 for (i = 0; i < 32; i++)
601 challenge[i] = sta->challenge[i] =
607 if (challenge_len != 128 || !sta->challenge ||
608 !(le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_WEP)) {
609 status = IEEE80211_STATUS_CHALLENGE;
615 /* Check the challenge text. (Was decrypted by
619 if (sta->challenge[i] != challenge[i]) {
620 status = IEEE80211_STATUS_CHALLENGE;
621 FREE(sta->challenge, M_TEMP);
622 sta->challenge = NULL;
626 sta->flags |= WI_SIFLAGS_AUTHEN;
627 FREE(sta->challenge, M_TEMP);
628 sta->challenge = NULL;
632 status = IEEE80211_STATUS_SEQUENCE;
637 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
638 printf("wihap_auth_req: algorithm unsupported: 0x%x\n",
640 status = IEEE80211_STATUS_ALG;
642 } /* switch (algo) */
644 status = IEEE80211_STATUS_SUCCESS;
647 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
648 printf("wihap_auth_req: returns status=0x%x\n", status);
651 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
652 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
653 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_AUTH);
654 bcopy(rxfrm->wi_addr2, resp_hdr->addr1, ETHER_ADDR_LEN);
655 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
656 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
657 pkt = &sc->wi_txbuf[sizeof(struct wi_80211_hdr)];
658 put_hword(&pkt, algo);
659 put_hword(&pkt, seq);
660 put_hword(&pkt, status);
661 if (challenge_len > 0)
662 put_tlv(&pkt, IEEE80211_ELEMID_CHALLENGE,
663 challenge, challenge_len);
664 wi_mgmt_xmit(sc, sc->wi_txbuf, (char *) pkt - (char *) sc->wi_txbuf);
669 * Handle incoming association and reassociation requests.
672 wihap_assoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
673 caddr_t pkt, int len)
675 struct wihap_info *whi = &sc->wi_hostap_info;
676 struct wihap_sta_info *sta;
677 struct wi_80211_hdr *resp_hdr;
681 int ssid_len, rates_len;
689 /* Pull out request parameters. */
690 capinfo = take_hword(&pkt, &len);
691 lstintvl = take_hword(&pkt, &len);
693 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_STYPE)) ==
694 htole16(WI_STYPE_MGMT_REASREQ)) {
697 /* Eat the MAC address of the current AP */
698 take_hword(&pkt, &len);
699 take_hword(&pkt, &len);
700 take_hword(&pkt, &len);
703 if ((ssid_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_SSID,
704 ssid, sizeof(ssid) - 1))<0)
706 ssid[ssid_len] = '\0';
707 if ((rates_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_RATES,
708 rates, sizeof(rates)))<0)
711 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
712 printf("wihap_assoc_req: from station %6D\n",
713 rxfrm->wi_addr2, ":");
715 /* If SSID doesn't match, simply drop. */
716 if (strcmp(sc->wi_net_name, ssid) != 0) {
717 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
718 printf("wihap_assoc_req: bad ssid: '%s' != '%s'\n",
719 ssid, sc->wi_net_name);
723 /* Is this station authenticated yet? */
724 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
725 if (sta == NULL || !(sta->flags & WI_SIFLAGS_AUTHEN)) {
726 wihap_sta_deauth(sc, rxfrm->wi_addr2,
727 IEEE80211_REASON_NOT_AUTHED);
731 /* Check supported rates against ours. */
732 if (wihap_check_rates(sta, rates, rates_len) < 0) {
733 status = IEEE80211_STATUS_RATES;
738 * Check for ESS, not IBSS.
739 * Check WEP/PRIVACY flags match.
740 * Refuse stations requesting to be put on CF-polling list.
742 sta->capinfo = capinfo;
743 status = IEEE80211_STATUS_CAPINFO;
744 if ((capinfo & (IEEE80211_CAPINFO_ESS | IEEE80211_CAPINFO_IBSS)) !=
745 IEEE80211_CAPINFO_ESS) {
746 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
747 printf("wihap_assoc_req: capinfo mismatch: "
748 "client using IBSS mode\n");
752 if ((sc->wi_use_wep && !(capinfo & IEEE80211_CAPINFO_PRIVACY)) ||
753 (!sc->wi_use_wep && (capinfo & IEEE80211_CAPINFO_PRIVACY))) {
754 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
755 printf("wihap_assoc_req: capinfo mismatch: client "
756 "%susing WEP\n", sc->wi_use_wep ? "not " : "");
759 if ((capinfo & (IEEE80211_CAPINFO_CF_POLLABLE |
760 IEEE80211_CAPINFO_CF_POLLREQ)) == IEEE80211_CAPINFO_CF_POLLABLE) {
761 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
762 printf("wihap_assoc_req: capinfo mismatch: "
763 "client requested CF polling\n");
767 /* Use ASID is allocated by whi_sta_alloc(). */
770 if (sta->flags & WI_SIFLAGS_ASSOC) {
771 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
772 printf("wihap_assoc_req: already assoc'ed?\n");
775 sta->flags |= WI_SIFLAGS_ASSOC;
776 sta->inactivity_timer = whi->inactivity_time;
777 status = IEEE80211_STATUS_SUCCESS;
780 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
781 printf("wihap_assoc_req: returns status=0x%x\n", status);
784 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
785 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
786 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_ASRESP);
787 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
789 bcopy(rxfrm->wi_addr2, resp_hdr->addr1, ETHER_ADDR_LEN);
790 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
791 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
793 put_hword(&pkt, capinfo);
794 put_hword(&pkt, status);
795 put_hword(&pkt, asid);
796 rates_len = put_rates(&pkt, sc->wi_supprates);
798 wi_mgmt_xmit(sc, sc->wi_txbuf,
799 8 + rates_len + sizeof(struct wi_80211_hdr));
802 /* wihap_deauth_req()
804 * Handle deauthentication requests. Delete the station.
807 wihap_deauth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
808 caddr_t pkt, int len)
810 struct wihap_info *whi = &sc->wi_hostap_info;
811 struct wihap_sta_info *sta;
817 reason = take_hword(&pkt, &len);
819 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
821 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
822 printf("wihap_deauth_req: unknown station: %6D\n",
823 rxfrm->wi_addr2, ":");
826 wihap_sta_delete(sta);
829 /* wihap_disassoc_req()
831 * Handle disassociation requests. Just reset the assoc flag.
832 * We'll free up the station resources when we get a deauth
833 * request or when it times out.
836 wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
837 caddr_t pkt, int len)
839 struct wihap_info *whi = &sc->wi_hostap_info;
840 struct wihap_sta_info *sta;
846 reason = take_hword(&pkt, &len);
848 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
850 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
851 printf("wihap_disassoc_req: unknown station: %6D\n",
852 rxfrm->wi_addr2, ":");
854 else if (!(sta->flags & WI_SIFLAGS_AUTHEN)) {
856 * If station is not authenticated, send deauthentication
859 wihap_sta_deauth(sc, rxfrm->wi_addr2,
860 IEEE80211_REASON_NOT_AUTHED);
864 sta->flags &= ~WI_SIFLAGS_ASSOC;
867 /* wihap_debug_frame_type()
869 * Print out frame type. Used in early debugging.
872 wihap_debug_frame_type(struct wi_frame *rxfrm)
874 printf("wihap_mgmt_input: len=%d ", le16toh(rxfrm->wi_dat_len));
876 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
877 htole16(WI_FTYPE_MGMT)) {
881 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
882 case WI_STYPE_MGMT_ASREQ:
883 printf("assoc req: \n");
885 case WI_STYPE_MGMT_ASRESP:
886 printf("assoc resp: \n");
888 case WI_STYPE_MGMT_REASREQ:
889 printf("reassoc req: \n");
891 case WI_STYPE_MGMT_REASRESP:
892 printf("reassoc resp: \n");
894 case WI_STYPE_MGMT_PROBEREQ:
895 printf("probe req: \n");
897 case WI_STYPE_MGMT_PROBERESP:
898 printf("probe resp: \n");
900 case WI_STYPE_MGMT_BEACON:
901 printf("beacon: \n");
903 case WI_STYPE_MGMT_ATIM:
904 printf("ann traf ind \n");
906 case WI_STYPE_MGMT_DISAS:
907 printf("disassociation: \n");
909 case WI_STYPE_MGMT_AUTH:
912 case WI_STYPE_MGMT_DEAUTH:
913 printf("deauth: \n");
916 printf("unknown (stype=0x%x)\n",
917 le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE);
922 printf("ftype=0x%x (ctl=0x%x)\n",
923 le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_FTYPE,
924 le16toh(rxfrm->wi_frame_ctl));
930 * Called for each management frame received in host ap mode.
931 * wihap_mgmt_input() is expected to free the mbuf.
934 wihap_mgmt_input(struct wi_softc *sc, struct wi_frame *rxfrm, struct mbuf *m)
939 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
940 wihap_debug_frame_type(rxfrm);
942 pkt = mtod(m, caddr_t) + WI_802_11_OFFSET_RAW;
943 len = m->m_len - WI_802_11_OFFSET_RAW;
945 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
946 htole16(WI_FTYPE_MGMT)) {
948 /* any of the following will mess w/ the station list */
950 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
951 case WI_STYPE_MGMT_ASREQ:
952 wihap_assoc_req(sc, rxfrm, pkt, len);
954 case WI_STYPE_MGMT_ASRESP:
956 case WI_STYPE_MGMT_REASREQ:
957 wihap_assoc_req(sc, rxfrm, pkt, len);
959 case WI_STYPE_MGMT_REASRESP:
961 case WI_STYPE_MGMT_PROBEREQ:
963 case WI_STYPE_MGMT_PROBERESP:
965 case WI_STYPE_MGMT_BEACON:
967 case WI_STYPE_MGMT_ATIM:
969 case WI_STYPE_MGMT_DISAS:
970 wihap_disassoc_req(sc, rxfrm, pkt, len);
972 case WI_STYPE_MGMT_AUTH:
973 wihap_auth_req(sc, rxfrm, pkt, len);
975 case WI_STYPE_MGMT_DEAUTH:
976 wihap_deauth_req(sc, rxfrm, pkt, len);
985 /* wihap_sta_is_assoc()
987 * Determine if a station is assoc'ed. Update its activity
988 * counter as a side-effect.
991 wihap_sta_is_assoc(struct wihap_info *whi, u_int8_t addr[])
993 struct wihap_sta_info *sta;
998 sta = wihap_sta_find(whi, addr);
999 if (sta != NULL && (sta->flags & WI_SIFLAGS_ASSOC)) {
1000 /* Keep it active. */
1001 untimeout(wihap_sta_timeout, sta, sta->tmo);
1002 sta->tmo = timeout(wihap_sta_timeout, sta,
1003 hz * whi->inactivity_time);
1012 * Determine if a station is assoc'ed, get its tx rate, and update
1016 wihap_check_tx(struct wihap_info *whi, u_int8_t addr[], u_int8_t *txrate)
1018 struct wihap_sta_info *sta;
1019 static u_int8_t txratetable[] = { 10, 20, 55, 110 };
1022 if (addr[0] & 0x01) {
1023 *txrate = 0; /* XXX: multicast rate? */
1027 sta = wihap_sta_find(whi, addr);
1028 if (sta != NULL && (sta->flags & WI_SIFLAGS_ASSOC)) {
1029 /* Keep it active. */
1030 untimeout(wihap_sta_timeout, sta, sta->tmo);
1031 sta->tmo = timeout(wihap_sta_timeout, sta,
1032 hz * whi->inactivity_time);
1033 *txrate = txratetable[ sta->tx_curr_rate ];
1043 * wihap_data_input()
1045 * Handle all data input on interface when in Host AP mode.
1046 * Some packets are destined for this machine, others are
1047 * repeated to other stations.
1049 * If wihap_data_input() returns a non-zero, it has processed
1050 * the packet and will free the mbuf.
1053 wihap_data_input(struct wi_softc *sc, struct wi_frame *rxfrm, struct mbuf *m)
1055 struct ifnet *ifp = &sc->arpcom.ac_if;
1056 struct wihap_info *whi = &sc->wi_hostap_info;
1057 struct wihap_sta_info *sta;
1060 /* TODS flag must be set. */
1061 if (!(rxfrm->wi_frame_ctl & htole16(WI_FCTL_TODS))) {
1062 if (ifp->if_flags & IFF_DEBUG)
1063 printf("wihap_data_input: no TODS src=%6D\n",
1064 rxfrm->wi_addr2, ":");
1069 /* Check BSSID. (Is this necessary?) */
1070 if (!addr_cmp(rxfrm->wi_addr1, sc->arpcom.ac_enaddr)) {
1071 if (ifp->if_flags & IFF_DEBUG)
1072 printf("wihap_data_input: incorrect bss: %6D\n",
1073 rxfrm->wi_addr1, ":");
1080 /* Find source station. */
1081 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
1083 /* Source station must be associated. */
1084 if (sta == NULL || !(sta->flags & WI_SIFLAGS_ASSOC)) {
1085 if (ifp->if_flags & IFF_DEBUG)
1086 printf("wihap_data_input: dropping unassoc src %6D\n",
1087 rxfrm->wi_addr2, ":");
1088 wihap_sta_disassoc(sc, rxfrm->wi_addr2,
1089 IEEE80211_REASON_ASSOC_LEAVE);
1095 untimeout(wihap_sta_timeout, sta, sta->tmo);
1096 sta->tmo = timeout(wihap_sta_timeout, sta,
1097 hz * whi->inactivity_time);
1098 sta->sig_info = le16toh(rxfrm->wi_q_info);
1102 /* Repeat this packet to BSS? */
1103 mcast = (rxfrm->wi_addr3[0] & 0x01) != 0;
1104 if (mcast || wihap_sta_is_assoc(whi, rxfrm->wi_addr3)) {
1106 /* If it's multicast, make a copy.
1109 m = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1112 m->m_flags |= M_MCAST; /* XXX */
1115 /* Queue up for repeating.
1117 IF_HANDOFF(&ifp->if_snd, m, ifp);
1126 * Handle Host AP specific ioctls. Called from wi_ioctl().
1129 wihap_ioctl(struct wi_softc *sc, u_long command, caddr_t data)
1131 struct ifreq *ifr = (struct ifreq *) data;
1132 struct wihap_info *whi = &sc->wi_hostap_info;
1133 struct wihap_sta_info *sta;
1134 struct hostap_getall reqall;
1135 struct hostap_sta reqsta;
1136 struct hostap_sta stabuf;
1137 int s, error = 0, n, flag;
1138 #if __FreeBSD_version >= 500000
1139 struct thread *td = curthread;
1141 struct proc *proc = curproc; /* Little white lie */
1144 if (!(sc->arpcom.ac_if.if_flags & IFF_RUNNING))
1148 case SIOCHOSTAP_DEL:
1149 if ((error = suser_xxx(proc->p_ucred, 0)))
1151 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1154 sta = wihap_sta_find(whi, reqsta.addr);
1158 /* Disassociate station. */
1159 if (sta->flags & WI_SIFLAGS_ASSOC)
1160 wihap_sta_disassoc(sc, sta->addr,
1161 IEEE80211_REASON_ASSOC_LEAVE);
1162 /* Deauth station. */
1163 if (sta->flags & WI_SIFLAGS_AUTHEN)
1164 wihap_sta_deauth(sc, sta->addr,
1165 IEEE80211_REASON_AUTH_LEAVE);
1167 wihap_sta_delete(sta);
1172 case SIOCHOSTAP_GET:
1173 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1176 sta = wihap_sta_find(whi, reqsta.addr);
1181 reqsta.flags = sta->flags;
1182 reqsta.asid = sta->asid;
1183 reqsta.capinfo = sta->capinfo;
1184 reqsta.sig_info = sta->sig_info;
1185 reqsta.rates = sta->rates;
1187 error = copyout(&reqsta, ifr->ifr_data,
1192 case SIOCHOSTAP_ADD:
1193 if ((error = suser_xxx(proc->p_ucred, 0)))
1195 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1198 sta = wihap_sta_find(whi, reqsta.addr);
1204 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
1209 sta = wihap_sta_alloc(sc, reqsta.addr);
1210 sta->flags = reqsta.flags;
1211 sta->tmo = timeout(wihap_sta_timeout, sta,
1212 hz * whi->inactivity_time);
1216 case SIOCHOSTAP_SFLAGS:
1217 if ((error = suser_xxx(proc->p_ucred, 0)))
1219 if ((error = copyin(ifr->ifr_data, &flag, sizeof(int))))
1222 whi->apflags = (whi->apflags & WIHAPFL_CANTCHANGE) |
1223 (flag & ~WIHAPFL_CANTCHANGE);
1226 case SIOCHOSTAP_GFLAGS:
1227 flag = (int) whi->apflags;
1228 error = copyout(&flag, ifr->ifr_data, sizeof(int));
1231 case SIOCHOSTAP_GETALL:
1232 if ((error = copyin(ifr->ifr_data, &reqall, sizeof(reqall))))
1235 reqall.nstations = whi->n_stations;
1238 sta = LIST_FIRST(&whi->sta_list);
1239 while (sta && reqall.size >= n+sizeof(struct hostap_sta)) {
1241 bcopy(sta->addr, stabuf.addr, ETHER_ADDR_LEN);
1242 stabuf.asid = sta->asid;
1243 stabuf.flags = sta->flags;
1244 stabuf.capinfo = sta->capinfo;
1245 stabuf.sig_info = sta->sig_info;
1246 stabuf.rates = sta->rates;
1248 error = copyout(&stabuf, (caddr_t) reqall.addr + n,
1249 sizeof(struct hostap_sta));
1253 sta = LIST_NEXT(sta, list);
1254 n += sizeof(struct hostap_sta);
1259 error = copyout(&reqall, ifr->ifr_data,
1263 printf("wihap_ioctl: i shouldn't get other ioctls!\n");