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/owi/Attic/owi_hostap.c,v 1.2 2004/09/15 00:21:09 joerg 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 defined(__FreeBSD__) && __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 <netproto/802_11/ieee80211.h>
74 #include <netproto/802_11/ieee80211_ioctl.h>
75 #include <netproto/802_11/if_wavelan_ieee.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/if_ether.h>
83 #include "wi_hostap.h"
87 static MALLOC_DEFINE(M_HAP_STA, "hostap_sta", "if_wi host AP mode station entry");
89 static void wihap_sta_timeout(void *v);
90 static struct wihap_sta_info *wihap_sta_alloc(struct wi_softc *sc,
92 static void wihap_sta_delete(struct wihap_sta_info *sta);
93 static struct wihap_sta_info *wihap_sta_find(struct wihap_info *whi,
95 static int wihap_sta_is_assoc(struct wihap_info *whi, u_int8_t addr[]);
96 static void wihap_auth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
97 caddr_t pkt, int len);
98 static void wihap_sta_deauth(struct wi_softc *sc, u_int8_t sta_addr[],
100 static void wihap_deauth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
101 caddr_t pkt, int len);
102 static void wihap_assoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
103 caddr_t pkt, int len);
104 static void wihap_sta_disassoc(struct wi_softc *sc, u_int8_t sta_addr[],
106 static void wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
107 caddr_t pkt, int len);
110 * Spl use in this driver.
112 * splnet is used everywhere here to block timeouts when we need to do
119 * Used for parsing management frames. The pkt pointer and length
120 * variables are updated after the value is removed.
122 static __inline u_int16_t
123 take_hword(caddr_t *ppkt, int *plen)
125 u_int16_t s = le16toh(* (u_int16_t *) *ppkt);
126 *ppkt += sizeof(u_int16_t);
127 *plen -= sizeof(u_int16_t);
133 * Parse out TLV element from a packet, check for underflow of packet
134 * or overflow of buffer, update pkt/len.
137 take_tlv(caddr_t *ppkt, int *plen, int id_expect, void *dst, int maxlen)
144 id = ((u_int8_t *)*ppkt)[0];
145 len = ((u_int8_t *)*ppkt)[1];
147 if (id != id_expect || *plen < len+2 || maxlen < len)
150 bcopy(*ppkt + 2, dst, len);
158 * Put half-word element into management frames.
161 put_hword(caddr_t *ppkt, u_int16_t s)
163 * (u_int16_t *) *ppkt = htole16(s);
164 *ppkt += sizeof(u_int16_t);
168 * Put TLV elements into management frames.
171 put_tlv(caddr_t *ppkt, u_int8_t id, void *src, u_int8_t len)
175 bcopy(src, (*ppkt) + 2, len);
180 put_rates(caddr_t *ppkt, u_int16_t rates)
185 if (rates & WI_SUPPRATES_1M)
186 ratebuf[len++] = 0x82;
187 if (rates & WI_SUPPRATES_2M)
188 ratebuf[len++] = 0x84;
189 if (rates & WI_SUPPRATES_5M)
190 ratebuf[len++] = 0x8b;
191 if (rates & WI_SUPPRATES_11M)
192 ratebuf[len++] = 0x96;
194 put_tlv(ppkt, IEEE80211_ELEMID_RATES, ratebuf, len);
200 * Initialize host AP data structures. Called even if port type is
204 owihap_init(struct wi_softc *sc)
207 struct wihap_info *whi = &sc->wi_hostap_info;
209 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
210 printf("wihap_init: sc=0x%x whi=0x%x\n", (int)sc, (int)whi);
212 bzero(whi, sizeof(struct wihap_info));
214 if (sc->wi_ptype != WI_PORTTYPE_AP)
217 whi->apflags = WIHAPFL_ACTIVE;
219 LIST_INIT(&whi->sta_list);
220 for (i = 0; i < WI_STA_HASH_SIZE; i++)
221 LIST_INIT(&whi->sta_hash[i]);
223 whi->inactivity_time = WIHAP_DFLT_INACTIVITY_TIME;
226 /* wihap_sta_disassoc()
228 * Send a disassociation frame to a specified station.
231 wihap_sta_disassoc(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
233 struct wi_80211_hdr *resp_hdr;
236 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
237 printf("Sending disassoc to sta %6D\n", sta_addr, ":");
239 /* Send disassoc packet. */
240 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
241 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
242 resp_hdr->frame_ctl = WI_FTYPE_MGMT | WI_STYPE_MGMT_DISAS;
243 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
245 bcopy(sta_addr, resp_hdr->addr1, ETHER_ADDR_LEN);
246 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
247 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
249 put_hword(&pkt, reason);
251 owi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
254 /* wihap_sta_deauth()
256 * Send a deauthentication message to a specified station.
259 wihap_sta_deauth(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
261 struct wi_80211_hdr *resp_hdr;
264 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
265 printf("Sending deauth to sta %6D\n", sta_addr, ":");
267 /* Send deauth packet. */
268 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
269 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
270 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_DEAUTH);
271 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
273 bcopy(sta_addr, resp_hdr->addr1, ETHER_ADDR_LEN);
274 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
275 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
277 put_hword(&pkt, reason);
279 owi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
284 * Disassociate all stations and free up data structures.
287 owihap_shutdown(struct wi_softc *sc)
289 struct wihap_info *whi = &sc->wi_hostap_info;
290 struct wihap_sta_info *sta, *next;
293 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
294 printf("wihap_shutdown: sc=0x%x whi=0x%x\n",
297 if (!(whi->apflags & WIHAPFL_ACTIVE))
300 /* XXX: I read somewhere you can deauth all the stations with
301 * a single broadcast. Maybe try that someday.
305 sta = LIST_FIRST(&whi->sta_list);
307 callout_stop(&sta->tmo);
309 /* Disassociate station. */
310 if (sta->flags & WI_SIFLAGS_ASSOC)
311 wihap_sta_disassoc(sc, sta->addr,
312 IEEE80211_REASON_ASSOC_LEAVE);
313 /* Deauth station. */
314 if (sta->flags & WI_SIFLAGS_AUTHEN)
315 wihap_sta_deauth(sc, sta->addr,
316 IEEE80211_REASON_AUTH_LEAVE);
319 /* Delete the structure. */
320 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
321 printf("wihap_shutdown: FREE(sta=0x%x)\n", (int)sta);
322 next = LIST_NEXT(sta, list);
323 FREE(sta, M_HAP_STA);
332 * Hash function for finding stations from ethernet address.
335 sta_hash_func(u_int8_t addr[])
337 return ((addr[3] + addr[4] + addr[5]) % WI_STA_HASH_SIZE);
340 /* addr_cmp(): Maybe this is a faster way to compare addresses? */
342 addr_cmp(u_int8_t a[], u_int8_t b[])
344 return (*(u_int16_t *)(a + 4) == *(u_int16_t *)(b + 4) &&
345 *(u_int32_t *)(a ) == *(u_int32_t *)(b));
349 wihap_sta_timeout(void *v)
351 struct wihap_sta_info *sta = v;
352 struct wi_softc *sc = sta->sc;
353 struct wihap_info *whi = &sc->wi_hostap_info;
357 if (sta->flags & WI_SIFLAGS_ASSOC) {
358 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
359 device_printf(sc->dev, "inactivity disassoc: %6D\n",
362 /* Disassoc station. */
363 wihap_sta_disassoc(sc, sta->addr,
364 IEEE80211_REASON_ASSOC_EXPIRE);
365 sta->flags &= ~WI_SIFLAGS_ASSOC;
367 callout_reset(&sta->tmo, hz * whi->inactivity_time,
368 wihap_sta_timeout, sta);
370 } else if (sta->flags & WI_SIFLAGS_AUTHEN) {
372 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
373 device_printf(sc->dev, "inactivity disassoc: %6D\n",
376 /* Deauthenticate station. */
377 wihap_sta_deauth(sc, sta->addr, IEEE80211_REASON_AUTH_EXPIRE);
378 sta->flags &= ~WI_SIFLAGS_AUTHEN;
380 /* Delete the station if it's not permanent. */
381 if (!(sta->flags & WI_SIFLAGS_PERM))
382 wihap_sta_delete(sta);
387 /* wihap_sta_delete()
388 * Delete a single station and free up its data structure.
391 wihap_sta_delete(struct wihap_sta_info *sta)
393 struct wi_softc *sc = sta->sc;
394 struct wihap_info *whi = &sc->wi_hostap_info;
395 int i = sta->asid - 0xc001;
397 callout_stop(&sta->tmo);
399 whi->asid_inuse_mask[i >> 4] &= ~(1UL << (i & 0xf));
401 LIST_REMOVE(sta, list);
402 LIST_REMOVE(sta, hash);
404 FREE(sta->challenge, M_TEMP);
405 FREE(sta, M_HAP_STA);
411 * Create a new station data structure and put it in the list
414 static struct wihap_sta_info *
415 wihap_sta_alloc(struct wi_softc *sc, u_int8_t *addr)
417 struct wihap_info *whi = &sc->wi_hostap_info;
418 struct wihap_sta_info *sta;
419 int i, hash = sta_hash_func(addr);
421 /* Allocate structure. */
422 MALLOC(sta, struct wihap_sta_info *, sizeof(struct wihap_sta_info),
423 M_HAP_STA, M_INTWAIT);
427 bzero(sta, sizeof(struct wihap_sta_info));
428 callout_init(&sta->tmo);
430 /* Allocate an ASID. */
432 while (whi->asid_inuse_mask[i >> 4] & (1UL << (i & 0xf)))
433 i = (i == (WI_STA_HASH_SIZE << 4) - 1) ? 0 : (i + 1);
434 whi->asid_inuse_mask[i >> 4] |= (1UL << (i & 0xf));
435 sta->asid = 0xc001 + i;
437 /* Insert in list and hash list. */
438 LIST_INSERT_HEAD(&whi->sta_list, sta, list);
439 LIST_INSERT_HEAD(&whi->sta_hash[hash], sta, hash);
443 bcopy(addr, &sta->addr, ETHER_ADDR_LEN);
450 * Find station structure given address.
452 static struct wihap_sta_info *
453 wihap_sta_find(struct wihap_info *whi, u_int8_t *addr)
456 struct wihap_sta_info *sta;
458 i = sta_hash_func(addr);
459 LIST_FOREACH(sta, &whi->sta_hash[i], hash)
460 if (addr_cmp(addr,sta->addr))
467 wihap_check_rates(struct wihap_sta_info *sta, u_int8_t rates[], int rates_len)
469 struct wi_softc *sc = sta->sc;
473 sta->tx_max_rate = 0;
474 for (i=0; i<rates_len; i++)
475 switch (rates[i] & 0x7f) {
477 sta->rates |= WI_SUPPRATES_1M;
480 sta->rates |= WI_SUPPRATES_2M;
481 if (sta->tx_max_rate<1)
482 sta->tx_max_rate = 1;
485 sta->rates |= WI_SUPPRATES_5M;
486 if (sta->tx_max_rate<2)
487 sta->tx_max_rate = 2;
490 sta->rates |= WI_SUPPRATES_11M;
491 sta->tx_max_rate = 3;
495 sta->rates &= sc->wi_supprates;
496 sta->tx_curr_rate = sta->tx_max_rate;
498 return (sta->rates == 0 ? -1 : 0);
504 * Handle incoming authentication request. Only handle OPEN
508 wihap_auth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
509 caddr_t pkt, int len)
511 struct wihap_info *whi = &sc->wi_hostap_info;
512 struct wihap_sta_info *sta;
517 int i, challenge_len;
518 u_int32_t challenge[32];
520 struct wi_80211_hdr *resp_hdr;
525 /* Break open packet. */
526 algo = take_hword(&pkt, &len);
527 seq = take_hword(&pkt, &len);
528 status = take_hword(&pkt, &len);
530 if (len > 0 && (challenge_len = take_tlv(&pkt, &len,
531 IEEE80211_ELEMID_CHALLENGE, challenge, sizeof(challenge))) < 0)
534 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
535 printf("wihap_auth_req: station %6D algo=0x%x seq=0x%x\n",
536 rxfrm->wi_addr2, ":", algo, seq);
538 /* Find or create station info. */
539 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
542 /* Are we allowing new stations?
544 if (whi->apflags & WIHAPFL_MAC_FILT) {
545 status = IEEE80211_STATUS_OTHER; /* XXX */
549 /* Check for too many stations.
551 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
552 status = IEEE80211_STATUS_TOO_MANY_STATIONS;
556 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
557 printf("wihap_auth_req: new station\n");
559 /* Create new station. */
560 sta = wihap_sta_alloc(sc, rxfrm->wi_addr2);
563 status = IEEE80211_STATUS_TOO_MANY_STATIONS;
568 /* Note: it's okay to leave the station info structure around
569 * if the authen fails. It'll be timed out eventually.
572 case IEEE80211_AUTH_ALG_OPEN:
573 if (sc->wi_authmode != IEEE80211_AUTH_OPEN) {
575 status = IEEE80211_STATUS_ALG;
580 status = IEEE80211_STATUS_SEQUENCE;
585 sta->flags |= WI_SIFLAGS_AUTHEN;
587 case IEEE80211_AUTH_ALG_SHARED:
588 if (sc->wi_authmode != IEEE80211_AUTH_SHARED) {
590 status = IEEE80211_STATUS_ALG;
595 /* Create a challenge frame. */
596 if (!sta->challenge) {
597 MALLOC(sta->challenge, u_int32_t *, 128,
602 for (i = 0; i < 32; i++)
603 challenge[i] = sta->challenge[i] =
609 if (challenge_len != 128 || !sta->challenge ||
610 !(le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_WEP)) {
611 status = IEEE80211_STATUS_CHALLENGE;
617 /* Check the challenge text. (Was decrypted by
621 if (sta->challenge[i] != challenge[i]) {
622 status = IEEE80211_STATUS_CHALLENGE;
623 FREE(sta->challenge, M_TEMP);
624 sta->challenge = NULL;
628 sta->flags |= WI_SIFLAGS_AUTHEN;
629 FREE(sta->challenge, M_TEMP);
630 sta->challenge = NULL;
634 status = IEEE80211_STATUS_SEQUENCE;
639 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
640 printf("wihap_auth_req: algorithm unsupported: 0x%x\n",
642 status = IEEE80211_STATUS_ALG;
644 } /* switch (algo) */
646 status = IEEE80211_STATUS_SUCCESS;
649 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
650 printf("wihap_auth_req: returns status=0x%x\n", status);
653 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
654 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
655 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_AUTH);
656 bcopy(rxfrm->wi_addr2, resp_hdr->addr1, ETHER_ADDR_LEN);
657 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
658 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
659 pkt = &sc->wi_txbuf[sizeof(struct wi_80211_hdr)];
660 put_hword(&pkt, algo);
661 put_hword(&pkt, seq);
662 put_hword(&pkt, status);
663 if (challenge_len > 0)
664 put_tlv(&pkt, IEEE80211_ELEMID_CHALLENGE,
665 challenge, challenge_len);
666 owi_mgmt_xmit(sc, sc->wi_txbuf, (char *) pkt - (char *) sc->wi_txbuf);
671 * Handle incoming association and reassociation requests.
674 wihap_assoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
675 caddr_t pkt, int len)
677 struct wihap_info *whi = &sc->wi_hostap_info;
678 struct wihap_sta_info *sta;
679 struct wi_80211_hdr *resp_hdr;
683 int ssid_len, rates_len;
691 /* Pull out request parameters. */
692 capinfo = take_hword(&pkt, &len);
693 lstintvl = take_hword(&pkt, &len);
695 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_STYPE)) ==
696 htole16(WI_STYPE_MGMT_REASREQ)) {
699 /* Eat the MAC address of the current AP */
700 take_hword(&pkt, &len);
701 take_hword(&pkt, &len);
702 take_hword(&pkt, &len);
705 if ((ssid_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_SSID,
706 ssid, sizeof(ssid) - 1))<0)
708 ssid[ssid_len] = '\0';
709 if ((rates_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_RATES,
710 rates, sizeof(rates)))<0)
713 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
714 printf("wihap_assoc_req: from station %6D\n",
715 rxfrm->wi_addr2, ":");
717 /* If SSID doesn't match, simply drop. */
718 if (strcmp(sc->wi_net_name, ssid) != 0) {
719 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
720 printf("wihap_assoc_req: bad ssid: '%s' != '%s'\n",
721 ssid, sc->wi_net_name);
725 /* Is this station authenticated yet? */
726 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
727 if (sta == NULL || !(sta->flags & WI_SIFLAGS_AUTHEN)) {
728 wihap_sta_deauth(sc, rxfrm->wi_addr2,
729 IEEE80211_REASON_NOT_AUTHED);
733 /* Check supported rates against ours. */
734 if (wihap_check_rates(sta, rates, rates_len) < 0) {
735 status = IEEE80211_STATUS_RATES;
740 * Check for ESS, not IBSS.
741 * Check WEP/PRIVACY flags match.
742 * Refuse stations requesting to be put on CF-polling list.
744 sta->capinfo = capinfo;
745 status = IEEE80211_STATUS_CAPINFO;
746 if ((capinfo & (IEEE80211_CAPINFO_ESS | IEEE80211_CAPINFO_IBSS)) !=
747 IEEE80211_CAPINFO_ESS) {
748 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
749 printf("wihap_assoc_req: capinfo mismatch: "
750 "client using IBSS mode\n");
754 if ((sc->wi_use_wep && !(capinfo & IEEE80211_CAPINFO_PRIVACY)) ||
755 (!sc->wi_use_wep && (capinfo & IEEE80211_CAPINFO_PRIVACY))) {
756 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
757 printf("wihap_assoc_req: capinfo mismatch: client "
758 "%susing WEP\n", sc->wi_use_wep ? "not " : "");
761 if ((capinfo & (IEEE80211_CAPINFO_CF_POLLABLE |
762 IEEE80211_CAPINFO_CF_POLLREQ)) == IEEE80211_CAPINFO_CF_POLLABLE) {
763 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
764 printf("wihap_assoc_req: capinfo mismatch: "
765 "client requested CF polling\n");
769 /* Use ASID is allocated by whi_sta_alloc(). */
772 if (sta->flags & WI_SIFLAGS_ASSOC) {
773 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
774 printf("wihap_assoc_req: already assoc'ed?\n");
777 sta->flags |= WI_SIFLAGS_ASSOC;
778 sta->inactivity_timer = whi->inactivity_time;
779 status = IEEE80211_STATUS_SUCCESS;
782 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
783 printf("wihap_assoc_req: returns status=0x%x\n", status);
786 resp_hdr = (struct wi_80211_hdr *) sc->wi_txbuf;
787 bzero(resp_hdr, sizeof(struct wi_80211_hdr));
788 resp_hdr->frame_ctl = htole16(WI_FTYPE_MGMT | WI_STYPE_MGMT_ASRESP);
789 pkt = sc->wi_txbuf + sizeof(struct wi_80211_hdr);
791 bcopy(rxfrm->wi_addr2, resp_hdr->addr1, ETHER_ADDR_LEN);
792 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr2, ETHER_ADDR_LEN);
793 bcopy(sc->arpcom.ac_enaddr, resp_hdr->addr3, ETHER_ADDR_LEN);
795 put_hword(&pkt, capinfo);
796 put_hword(&pkt, status);
797 put_hword(&pkt, asid);
798 rates_len = put_rates(&pkt, sc->wi_supprates);
800 owi_mgmt_xmit(sc, sc->wi_txbuf,
801 8 + rates_len + sizeof(struct wi_80211_hdr));
804 /* wihap_deauth_req()
806 * Handle deauthentication requests. Delete the station.
809 wihap_deauth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
810 caddr_t pkt, int len)
812 struct wihap_info *whi = &sc->wi_hostap_info;
813 struct wihap_sta_info *sta;
819 reason = take_hword(&pkt, &len);
821 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
823 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
824 printf("wihap_deauth_req: unknown station: %6D\n",
825 rxfrm->wi_addr2, ":");
828 wihap_sta_delete(sta);
831 /* wihap_disassoc_req()
833 * Handle disassociation requests. Just reset the assoc flag.
834 * We'll free up the station resources when we get a deauth
835 * request or when it times out.
838 wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
839 caddr_t pkt, int len)
841 struct wihap_info *whi = &sc->wi_hostap_info;
842 struct wihap_sta_info *sta;
848 reason = take_hword(&pkt, &len);
850 sta = wihap_sta_find(whi, rxfrm->wi_addr2);
852 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
853 printf("wihap_disassoc_req: unknown station: %6D\n",
854 rxfrm->wi_addr2, ":");
856 else if (!(sta->flags & WI_SIFLAGS_AUTHEN)) {
858 * If station is not authenticated, send deauthentication
861 wihap_sta_deauth(sc, rxfrm->wi_addr2,
862 IEEE80211_REASON_NOT_AUTHED);
866 sta->flags &= ~WI_SIFLAGS_ASSOC;
869 /* wihap_debug_frame_type()
871 * Print out frame type. Used in early debugging.
874 wihap_debug_frame_type(struct wi_frame *rxfrm)
876 printf("wihap_mgmt_input: len=%d ", le16toh(rxfrm->wi_dat_len));
878 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
879 htole16(WI_FTYPE_MGMT)) {
883 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
884 case WI_STYPE_MGMT_ASREQ:
885 printf("assoc req: \n");
887 case WI_STYPE_MGMT_ASRESP:
888 printf("assoc resp: \n");
890 case WI_STYPE_MGMT_REASREQ:
891 printf("reassoc req: \n");
893 case WI_STYPE_MGMT_REASRESP:
894 printf("reassoc resp: \n");
896 case WI_STYPE_MGMT_PROBEREQ:
897 printf("probe req: \n");
899 case WI_STYPE_MGMT_PROBERESP:
900 printf("probe resp: \n");
902 case WI_STYPE_MGMT_BEACON:
903 printf("beacon: \n");
905 case WI_STYPE_MGMT_ATIM:
906 printf("ann traf ind \n");
908 case WI_STYPE_MGMT_DISAS:
909 printf("disassociation: \n");
911 case WI_STYPE_MGMT_AUTH:
914 case WI_STYPE_MGMT_DEAUTH:
915 printf("deauth: \n");
918 printf("unknown (stype=0x%x)\n",
919 le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE);
924 printf("ftype=0x%x (ctl=0x%x)\n",
925 le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_FTYPE,
926 le16toh(rxfrm->wi_frame_ctl));
932 * Called for each management frame received in host ap mode.
933 * wihap_mgmt_input() is expected to free the mbuf.
936 owihap_mgmt_input(struct wi_softc *sc, struct wi_frame *rxfrm, struct mbuf *m)
941 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
942 wihap_debug_frame_type(rxfrm);
944 pkt = mtod(m, caddr_t) + WI_802_11_OFFSET_RAW;
945 len = m->m_len - WI_802_11_OFFSET_RAW;
947 if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
948 htole16(WI_FTYPE_MGMT)) {
950 /* any of the following will mess w/ the station list */
952 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
953 case WI_STYPE_MGMT_ASREQ:
954 wihap_assoc_req(sc, rxfrm, pkt, len);
956 case WI_STYPE_MGMT_ASRESP:
958 case WI_STYPE_MGMT_REASREQ:
959 wihap_assoc_req(sc, rxfrm, pkt, len);
961 case WI_STYPE_MGMT_REASRESP:
963 case WI_STYPE_MGMT_PROBEREQ:
965 case WI_STYPE_MGMT_PROBERESP:
967 case WI_STYPE_MGMT_BEACON:
969 case WI_STYPE_MGMT_ATIM:
971 case WI_STYPE_MGMT_DISAS:
972 wihap_disassoc_req(sc, rxfrm, pkt, len);
974 case WI_STYPE_MGMT_AUTH:
975 wihap_auth_req(sc, rxfrm, pkt, len);
977 case WI_STYPE_MGMT_DEAUTH:
978 wihap_deauth_req(sc, rxfrm, pkt, len);
987 /* wihap_sta_is_assoc()
989 * Determine if a station is assoc'ed. Update its activity
990 * counter as a side-effect.
993 wihap_sta_is_assoc(struct wihap_info *whi, u_int8_t addr[])
995 struct wihap_sta_info *sta;
1000 sta = wihap_sta_find(whi, addr);
1001 if (sta != NULL && (sta->flags & WI_SIFLAGS_ASSOC)) {
1002 /* Keep it active. */
1003 callout_reset(&sta->tmo, hz * whi->inactivity_time,
1004 wihap_sta_timeout, sta);
1013 * Determine if a station is assoc'ed, get its tx rate, and update
1017 owihap_check_tx(struct wihap_info *whi, u_int8_t addr[], u_int8_t *txrate)
1019 struct wihap_sta_info *sta;
1020 static u_int8_t txratetable[] = { 10, 20, 55, 110 };
1023 if (addr[0] & 0x01) {
1024 *txrate = 0; /* XXX: multicast rate? */
1028 sta = wihap_sta_find(whi, addr);
1029 if (sta != NULL && (sta->flags & WI_SIFLAGS_ASSOC)) {
1030 /* Keep it active. */
1031 callout_reset(&sta->tmo, hz * whi->inactivity_time,
1032 wihap_sta_timeout, sta);
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 owihap_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 callout_reset(&sta->tmo, hz * whi->inactivity_time,
1096 wihap_sta_timeout, sta);
1097 sta->sig_info = le16toh(rxfrm->wi_q_info);
1101 /* Repeat this packet to BSS? */
1102 mcast = (rxfrm->wi_addr3[0] & 0x01) != 0;
1103 if (mcast || wihap_sta_is_assoc(whi, rxfrm->wi_addr3)) {
1105 /* If it's multicast, make a copy.
1108 m = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
1111 m->m_flags |= M_MCAST; /* XXX */
1114 /* Queue up for repeating.
1116 IF_HANDOFF(&ifp->if_snd, m, ifp);
1125 * Handle Host AP specific ioctls. Called from wi_ioctl().
1128 owihap_ioctl(struct wi_softc *sc, u_long command, caddr_t data)
1130 struct ifreq *ifr = (struct ifreq *) data;
1131 struct wihap_info *whi = &sc->wi_hostap_info;
1132 struct wihap_sta_info *sta;
1133 struct hostap_getall reqall;
1134 struct hostap_sta reqsta;
1135 struct hostap_sta stabuf;
1136 int s, error = 0, n, flag;
1137 struct thread *td = curthread;
1139 if (!(sc->arpcom.ac_if.if_flags & IFF_RUNNING))
1143 case SIOCHOSTAP_DEL:
1144 if ((error = suser(td)))
1146 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1149 sta = wihap_sta_find(whi, reqsta.addr);
1153 /* Disassociate station. */
1154 if (sta->flags & WI_SIFLAGS_ASSOC)
1155 wihap_sta_disassoc(sc, sta->addr,
1156 IEEE80211_REASON_ASSOC_LEAVE);
1157 /* Deauth station. */
1158 if (sta->flags & WI_SIFLAGS_AUTHEN)
1159 wihap_sta_deauth(sc, sta->addr,
1160 IEEE80211_REASON_AUTH_LEAVE);
1162 wihap_sta_delete(sta);
1167 case SIOCHOSTAP_GET:
1168 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1171 sta = wihap_sta_find(whi, reqsta.addr);
1176 reqsta.flags = sta->flags;
1177 reqsta.asid = sta->asid;
1178 reqsta.capinfo = sta->capinfo;
1179 reqsta.sig_info = sta->sig_info;
1180 reqsta.rates = sta->rates;
1182 error = copyout(&reqsta, ifr->ifr_data,
1187 case SIOCHOSTAP_ADD:
1188 if ((error = suser(td)))
1190 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1193 sta = wihap_sta_find(whi, reqsta.addr);
1199 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
1204 sta = wihap_sta_alloc(sc, reqsta.addr);
1205 sta->flags = reqsta.flags;
1206 callout_reset(&sta->tmo, hz * whi->inactivity_time,
1207 wihap_sta_timeout, sta);
1211 case SIOCHOSTAP_SFLAGS:
1212 if ((error = suser(td)))
1214 if ((error = copyin(ifr->ifr_data, &flag, sizeof(int))))
1217 whi->apflags = (whi->apflags & WIHAPFL_CANTCHANGE) |
1218 (flag & ~WIHAPFL_CANTCHANGE);
1221 case SIOCHOSTAP_GFLAGS:
1222 flag = (int) whi->apflags;
1223 error = copyout(&flag, ifr->ifr_data, sizeof(int));
1226 case SIOCHOSTAP_GETALL:
1227 if ((error = copyin(ifr->ifr_data, &reqall, sizeof(reqall))))
1230 reqall.nstations = whi->n_stations;
1233 sta = LIST_FIRST(&whi->sta_list);
1234 while (sta && reqall.size >= n+sizeof(struct hostap_sta)) {
1236 bcopy(sta->addr, stabuf.addr, ETHER_ADDR_LEN);
1237 stabuf.asid = sta->asid;
1238 stabuf.flags = sta->flags;
1239 stabuf.capinfo = sta->capinfo;
1240 stabuf.sig_info = sta->sig_info;
1241 stabuf.rates = sta->rates;
1243 error = copyout(&stabuf, (caddr_t) reqall.addr + n,
1244 sizeof(struct hostap_sta));
1248 sta = LIST_NEXT(sta, list);
1249 n += sizeof(struct hostap_sta);
1254 error = copyout(&reqall, ifr->ifr_data,
1258 printf("wihap_ioctl: i shouldn't get other ioctls!\n");