timeout/untimeout ==> callout_*
[dragonfly.git] / sys / dev / netif / owi / owi_hostap.c
1 /*
2  * Copyright (c) 2002
3  *      Thomas Skibo <skibo@pacbell.net>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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.
31  *
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 $
34  */
35
36 /* This is experimental Host AP software for Prism 2 802.11b interfaces.
37  *
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>.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #if defined(__FreeBSD__) && __FreeBSD_version >= 500033
45 #include <sys/endian.h>
46 #endif
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/ucred.h>
53 #include <sys/socket.h>
54 #include <sys/module.h>
55 #include <sys/queue.h>
56 #include <sys/bus.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59
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>
65 #include <sys/rman.h>
66
67 #include <net/if.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>
76
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>
82
83 #include "wi_hostap.h"
84 #include "if_wivar.h"
85 #include "if_wireg.h"
86
87 static MALLOC_DEFINE(M_HAP_STA, "hostap_sta", "if_wi host AP mode station entry");
88
89 static void wihap_sta_timeout(void *v);
90 static struct wihap_sta_info *wihap_sta_alloc(struct wi_softc *sc,
91     u_int8_t *addr);
92 static void wihap_sta_delete(struct wihap_sta_info *sta);
93 static struct wihap_sta_info *wihap_sta_find(struct wihap_info *whi,
94     u_int8_t *addr);
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[],
99     u_int16_t reason);
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[],
105     u_int16_t reason);
106 static void wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
107     caddr_t pkt, int len);
108
109 /*
110  * Spl use in this driver.
111  *
112  * splnet is used everywhere here to block timeouts when we need to do
113  * so.
114  */
115
116 /*
117  * take_hword()
118  *
119  *      Used for parsing management frames.  The pkt pointer and length
120  *      variables are updated after the value is removed.
121  */
122 static __inline u_int16_t
123 take_hword(caddr_t *ppkt, int *plen)
124 {
125         u_int16_t s = le16toh(* (u_int16_t *) *ppkt);
126         *ppkt += sizeof(u_int16_t);
127         *plen -= sizeof(u_int16_t);
128         return s;
129 }
130
131 /* take_tlv()
132  *
133  *      Parse out TLV element from a packet, check for underflow of packet
134  *      or overflow of buffer, update pkt/len.
135  */
136 static int
137 take_tlv(caddr_t *ppkt, int *plen, int id_expect, void *dst, int maxlen)
138 {
139         u_int8_t id, len;
140
141         if (*plen < 2)
142                 return -1;
143
144         id = ((u_int8_t *)*ppkt)[0];
145         len = ((u_int8_t *)*ppkt)[1];
146
147         if (id != id_expect || *plen < len+2 || maxlen < len)
148                 return -1;
149
150         bcopy(*ppkt + 2, dst, len);
151         *plen -= 2 + len;
152         *ppkt += 2 + len;
153
154         return (len);
155 }
156
157 /* put_hword()
158  *      Put half-word element into management frames.
159  */
160 static __inline void
161 put_hword(caddr_t *ppkt, u_int16_t s)
162 {
163         * (u_int16_t *) *ppkt = htole16(s);
164         *ppkt += sizeof(u_int16_t);
165 }
166
167 /* put_tlv()
168  *      Put TLV elements into management frames.
169  */
170 static void
171 put_tlv(caddr_t *ppkt, u_int8_t id, void *src, u_int8_t len)
172 {
173         (*ppkt)[0] = id;
174         (*ppkt)[1] = len;
175         bcopy(src, (*ppkt) + 2, len);
176         *ppkt += 2 + len;
177 }
178
179 static int
180 put_rates(caddr_t *ppkt, u_int16_t rates)
181 {
182         u_int8_t ratebuf[8];
183         int len = 0;
184
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;
193
194         put_tlv(ppkt, IEEE80211_ELEMID_RATES, ratebuf, len);
195         return len;
196 }
197
198 /* wihap_init()
199  *
200  *      Initialize host AP data structures.  Called even if port type is
201  *      not AP.
202  */
203 void
204 owihap_init(struct wi_softc *sc)
205 {
206         int i;
207         struct wihap_info *whi = &sc->wi_hostap_info;
208
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);
211
212         bzero(whi, sizeof(struct wihap_info));
213
214         if (sc->wi_ptype != WI_PORTTYPE_AP)
215                 return;
216
217         whi->apflags = WIHAPFL_ACTIVE;
218
219         LIST_INIT(&whi->sta_list);
220         for (i = 0; i < WI_STA_HASH_SIZE; i++)
221                 LIST_INIT(&whi->sta_hash[i]);
222
223         whi->inactivity_time = WIHAP_DFLT_INACTIVITY_TIME;
224 }
225
226 /* wihap_sta_disassoc()
227  *
228  *      Send a disassociation frame to a specified station.
229  */
230 static void
231 wihap_sta_disassoc(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
232 {
233         struct wi_80211_hdr     *resp_hdr;
234         caddr_t                 pkt;
235
236         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
237                 printf("Sending disassoc to sta %6D\n", sta_addr, ":");
238
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);
244
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);
248
249         put_hword(&pkt, reason);
250
251         owi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
252 }
253
254 /* wihap_sta_deauth()
255  *
256  *      Send a deauthentication message to a specified station.
257  */
258 static void
259 wihap_sta_deauth(struct wi_softc *sc, u_int8_t sta_addr[], u_int16_t reason)
260 {
261         struct wi_80211_hdr     *resp_hdr;
262         caddr_t                 pkt;
263
264         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
265                 printf("Sending deauth to sta %6D\n", sta_addr, ":");
266
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);
272
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);
276
277         put_hword(&pkt, reason);
278
279         owi_mgmt_xmit(sc, sc->wi_txbuf, 2 + sizeof(struct wi_80211_hdr));
280 }
281
282 /* wihap_shutdown()
283  *
284  *      Disassociate all stations and free up data structures.
285  */
286 void
287 owihap_shutdown(struct wi_softc *sc)
288 {
289         struct wihap_info       *whi = &sc->wi_hostap_info;
290         struct wihap_sta_info   *sta, *next;
291         int s;
292
293         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
294                 printf("wihap_shutdown: sc=0x%x whi=0x%x\n",
295                     (int)sc, (int)whi);
296
297         if (!(whi->apflags & WIHAPFL_ACTIVE))
298                 return;
299
300         /* XXX: I read somewhere you can deauth all the stations with
301          * a single broadcast.  Maybe try that someday.
302          */
303
304         s = splnet();
305         sta = LIST_FIRST(&whi->sta_list);
306         while (sta) {
307                 callout_stop(&sta->tmo);
308                 if (!sc->wi_gone) {
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);
317                 }
318
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);
324                 sta = next;
325         }
326
327         whi->apflags = 0;
328         splx(s);
329 }
330
331 /* sta_hash_func()
332  * Hash function for finding stations from ethernet address.
333  */
334 static __inline int
335 sta_hash_func(u_int8_t addr[])
336 {
337         return ((addr[3] + addr[4] + addr[5]) % WI_STA_HASH_SIZE);
338 }
339
340 /* addr_cmp():  Maybe this is a faster way to compare addresses? */
341 static __inline int
342 addr_cmp(u_int8_t a[], u_int8_t b[])
343 {
344         return (*(u_int16_t *)(a + 4) == *(u_int16_t *)(b + 4) &&
345                 *(u_int32_t *)(a    ) == *(u_int32_t *)(b));
346 }
347
348 static void
349 wihap_sta_timeout(void *v)
350 {
351         struct wihap_sta_info   *sta = v;
352         struct wi_softc         *sc = sta->sc;
353         struct wihap_info       *whi = &sc->wi_hostap_info;
354         int     s;
355
356         s = splnet();
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",
360                             sta->addr, ":");
361
362                 /* Disassoc station. */
363                 wihap_sta_disassoc(sc, sta->addr,
364                     IEEE80211_REASON_ASSOC_EXPIRE);
365                 sta->flags &= ~WI_SIFLAGS_ASSOC;
366
367                 callout_reset(&sta->tmo, hz * whi->inactivity_time,
368                               wihap_sta_timeout, sta);
369
370         } else if (sta->flags & WI_SIFLAGS_AUTHEN) {
371
372                 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
373                         device_printf(sc->dev, "inactivity disassoc: %6D\n",
374                             sta->addr, ":");
375
376                 /* Deauthenticate station. */
377                 wihap_sta_deauth(sc, sta->addr, IEEE80211_REASON_AUTH_EXPIRE);
378                 sta->flags &= ~WI_SIFLAGS_AUTHEN;
379
380                 /* Delete the station if it's not permanent. */
381                 if (!(sta->flags & WI_SIFLAGS_PERM))
382                         wihap_sta_delete(sta);
383         }
384         splx(s);
385 }
386
387 /* wihap_sta_delete()
388  * Delete a single station and free up its data structure.
389  */
390 static void
391 wihap_sta_delete(struct wihap_sta_info *sta)
392 {
393         struct wi_softc         *sc = sta->sc;
394         struct wihap_info       *whi = &sc->wi_hostap_info;
395         int i = sta->asid - 0xc001;
396
397         callout_stop(&sta->tmo);
398
399         whi->asid_inuse_mask[i >> 4] &= ~(1UL << (i & 0xf));
400
401         LIST_REMOVE(sta, list);
402         LIST_REMOVE(sta, hash);
403         if (sta->challenge)
404                 FREE(sta->challenge, M_TEMP);
405         FREE(sta, M_HAP_STA);
406         whi->n_stations--;
407 }
408
409 /* wihap_sta_alloc()
410  *
411  *      Create a new station data structure and put it in the list
412  *      and hash table.
413  */
414 static struct wihap_sta_info *
415 wihap_sta_alloc(struct wi_softc *sc, u_int8_t *addr)
416 {
417         struct wihap_info       *whi = &sc->wi_hostap_info;
418         struct wihap_sta_info   *sta;
419         int i, hash = sta_hash_func(addr);
420
421         /* Allocate structure. */
422         MALLOC(sta, struct wihap_sta_info *, sizeof(struct wihap_sta_info),
423             M_HAP_STA, M_INTWAIT);
424         if (sta == NULL)
425                 return(NULL);
426
427         bzero(sta, sizeof(struct wihap_sta_info));
428         callout_init(&sta->tmo);
429
430         /* Allocate an ASID. */
431         i=hash<<4;
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;
436
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);
440
441         sta->sc = sc;
442         whi->n_stations++;
443         bcopy(addr, &sta->addr, ETHER_ADDR_LEN);
444
445         return(sta);
446 }
447
448 /* wihap_sta_find()
449  *
450  *      Find station structure given address.
451  */
452 static struct wihap_sta_info *
453 wihap_sta_find(struct wihap_info *whi, u_int8_t *addr)
454 {
455         int i;
456         struct wihap_sta_info *sta;
457
458         i = sta_hash_func(addr);
459         LIST_FOREACH(sta, &whi->sta_hash[i], hash)
460                 if (addr_cmp(addr,sta->addr))
461                         return sta;
462
463         return (NULL);
464 }
465
466 static int
467 wihap_check_rates(struct wihap_sta_info *sta, u_int8_t rates[], int rates_len)
468 {
469         struct wi_softc *sc = sta->sc;
470         int     i;
471
472         sta->rates = 0;
473         sta->tx_max_rate = 0;
474         for (i=0; i<rates_len; i++)
475                 switch (rates[i] & 0x7f) {
476                 case 0x02:
477                         sta->rates |= WI_SUPPRATES_1M;
478                         break;
479                 case 0x04:
480                         sta->rates |= WI_SUPPRATES_2M;
481                         if (sta->tx_max_rate<1)
482                                 sta->tx_max_rate = 1;
483                         break;
484                 case 0x0b:
485                         sta->rates |= WI_SUPPRATES_5M;
486                         if (sta->tx_max_rate<2)
487                                 sta->tx_max_rate = 2;
488                         break;
489                 case 0x16:
490                         sta->rates |= WI_SUPPRATES_11M;
491                         sta->tx_max_rate = 3;
492                         break;
493                 }
494
495         sta->rates &= sc->wi_supprates;
496         sta->tx_curr_rate = sta->tx_max_rate;
497
498         return (sta->rates == 0 ? -1 : 0);
499 }
500
501
502 /* wihap_auth_req()
503  *
504  *      Handle incoming authentication request.  Only handle OPEN
505  *      requests.
506  */
507 static void
508 wihap_auth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
509     caddr_t pkt, int len)
510 {
511         struct wihap_info       *whi = &sc->wi_hostap_info;
512         struct wihap_sta_info   *sta;
513
514         u_int16_t               algo;
515         u_int16_t               seq;
516         u_int16_t               status;
517         int                     i, challenge_len;
518         u_int32_t               challenge[32];
519
520         struct wi_80211_hdr     *resp_hdr;
521
522         if (len < 6)
523                 return;
524
525         /* Break open packet. */
526         algo = take_hword(&pkt, &len);
527         seq = take_hword(&pkt, &len);
528         status = take_hword(&pkt, &len);
529         challenge_len = 0;
530         if (len > 0 && (challenge_len = take_tlv(&pkt, &len,
531             IEEE80211_ELEMID_CHALLENGE, challenge, sizeof(challenge))) < 0)
532                 return;
533
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);
537
538         /* Find or create station info. */
539         sta = wihap_sta_find(whi, rxfrm->wi_addr2);
540         if (sta == NULL) {
541
542                 /* Are we allowing new stations?
543                  */
544                 if (whi->apflags & WIHAPFL_MAC_FILT) {
545                         status = IEEE80211_STATUS_OTHER; /* XXX */
546                         goto fail;
547                 }
548
549                 /* Check for too many stations.
550                  */
551                 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
552                         status = IEEE80211_STATUS_TOO_MANY_STATIONS;
553                         goto fail;
554                 }
555
556                 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
557                         printf("wihap_auth_req: new station\n");
558
559                 /* Create new station. */
560                 sta = wihap_sta_alloc(sc, rxfrm->wi_addr2);
561                 if (sta == NULL) {
562                         /* Out of memory! */
563                         status = IEEE80211_STATUS_TOO_MANY_STATIONS;
564                         goto fail;
565                 }
566         }
567
568         /* Note: it's okay to leave the station info structure around
569          * if the authen fails.  It'll be timed out eventually.
570          */
571         switch (algo) {
572         case IEEE80211_AUTH_ALG_OPEN:
573                 if (sc->wi_authmode != IEEE80211_AUTH_OPEN) {
574                         seq = 2;
575                         status = IEEE80211_STATUS_ALG;
576                         goto fail;
577                 }
578                 if (seq != 1) {
579                         seq = 2;
580                         status = IEEE80211_STATUS_SEQUENCE;
581                         goto fail;
582                 }
583                 challenge_len = 0;
584                 seq = 2;
585                 sta->flags |= WI_SIFLAGS_AUTHEN;
586                 break;
587         case IEEE80211_AUTH_ALG_SHARED:
588                 if (sc->wi_authmode != IEEE80211_AUTH_SHARED) {
589                         seq = 2;
590                         status = IEEE80211_STATUS_ALG;
591                         goto fail;
592                 }
593                 switch (seq) {
594                 case 1:
595                         /* Create a challenge frame. */
596                         if (!sta->challenge) {
597                                 MALLOC(sta->challenge, u_int32_t *, 128,
598                                        M_TEMP, M_INTWAIT);
599                                 if (!sta->challenge)
600                                         return;
601                         }
602                         for (i = 0; i < 32; i++)
603                                 challenge[i] = sta->challenge[i] =
604                                         arc4random();
605                         challenge_len = 128;
606                         seq = 2;
607                         break;
608                 case 3:
609                         if (challenge_len != 128 || !sta->challenge ||
610                             !(le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_WEP)) {
611                                 status = IEEE80211_STATUS_CHALLENGE;
612                                 goto fail;
613                         }
614                         challenge_len = 0;
615                         seq = 4;
616
617                         /* Check the challenge text.  (Was decrypted by
618                          * the adapter.)
619                          */
620                         for (i=0; i<32; i++)
621                                 if (sta->challenge[i] != challenge[i]) {
622                                         status = IEEE80211_STATUS_CHALLENGE;
623                                         FREE(sta->challenge, M_TEMP);
624                                         sta->challenge = NULL;
625                                         goto fail;
626                                 }
627
628                         sta->flags |= WI_SIFLAGS_AUTHEN;
629                         FREE(sta->challenge, M_TEMP);
630                         sta->challenge = NULL;
631                         break;
632                 default:
633                         seq = 2;
634                         status = IEEE80211_STATUS_SEQUENCE;
635                         goto fail;
636                 } /* switch (seq) */
637                 break;
638         default:
639                 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
640                         printf("wihap_auth_req: algorithm unsupported: 0x%x\n",
641                                algo);
642                 status = IEEE80211_STATUS_ALG;
643                 goto fail;
644         } /* switch (algo) */
645
646         status = IEEE80211_STATUS_SUCCESS;
647
648 fail:
649         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
650                 printf("wihap_auth_req: returns status=0x%x\n", status);
651
652         /* Send response. */
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);
667 }
668
669 /* wihap_assoc_req()
670  *
671  *      Handle incoming association and reassociation requests.
672  */
673 static void
674 wihap_assoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
675                 caddr_t pkt, int len)
676 {
677         struct wihap_info       *whi = &sc->wi_hostap_info;
678         struct wihap_sta_info   *sta;
679         struct wi_80211_hdr     *resp_hdr;
680         u_int16_t               capinfo;
681         u_int16_t               lstintvl;
682         u_int8_t                rates[8];
683         int                     ssid_len, rates_len;
684         char                    ssid[33];
685         u_int16_t               status;
686         u_int16_t               asid = 0;
687
688         if (len < 8)
689                 return;
690
691         /* Pull out request parameters. */
692         capinfo = take_hword(&pkt, &len);
693         lstintvl = take_hword(&pkt, &len);
694
695         if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_STYPE)) ==
696             htole16(WI_STYPE_MGMT_REASREQ)) {
697                 if (len < 6)
698                         return;
699                 /* Eat the MAC address of the current AP */
700                 take_hword(&pkt, &len);
701                 take_hword(&pkt, &len);
702                 take_hword(&pkt, &len);
703         }
704
705         if ((ssid_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_SSID,
706             ssid, sizeof(ssid) - 1))<0)
707                 return;
708         ssid[ssid_len] = '\0';
709         if ((rates_len = take_tlv(&pkt, &len, IEEE80211_ELEMID_RATES,
710             rates, sizeof(rates)))<0)
711                 return;
712
713         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
714                 printf("wihap_assoc_req: from station %6D\n",
715                     rxfrm->wi_addr2, ":");
716
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);
722                 return;
723         }
724
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);
730                 return;
731         }
732
733         /* Check supported rates against ours. */
734         if (wihap_check_rates(sta, rates, rates_len) < 0) {
735                 status = IEEE80211_STATUS_RATES;
736                 goto fail;
737         }
738
739         /* Check capinfo.
740          * Check for ESS, not IBSS.
741          * Check WEP/PRIVACY flags match.
742          * Refuse stations requesting to be put on CF-polling list.
743          */
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");
751                 goto fail;
752
753         }
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 " : "");
759                 goto fail;
760         }
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");
766                 goto fail;
767         }
768
769         /* Use ASID is allocated by whi_sta_alloc(). */
770         asid = sta->asid;
771
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");
775         }
776
777         sta->flags |= WI_SIFLAGS_ASSOC;
778         sta->inactivity_timer = whi->inactivity_time;
779         status = IEEE80211_STATUS_SUCCESS;
780
781 fail:
782         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
783                 printf("wihap_assoc_req: returns status=0x%x\n", status);
784
785         /* Send response. */
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);
790
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);
794
795         put_hword(&pkt, capinfo);
796         put_hword(&pkt, status);
797         put_hword(&pkt, asid);
798         rates_len = put_rates(&pkt, sc->wi_supprates);
799
800         owi_mgmt_xmit(sc, sc->wi_txbuf,
801             8 + rates_len + sizeof(struct wi_80211_hdr));
802 }
803
804 /* wihap_deauth_req()
805  *
806  *      Handle deauthentication requests.  Delete the station.
807  */
808 static void
809 wihap_deauth_req(struct wi_softc *sc, struct wi_frame *rxfrm,
810                  caddr_t pkt, int len)
811 {
812         struct wihap_info       *whi = &sc->wi_hostap_info;
813         struct wihap_sta_info   *sta;
814         u_int16_t               reason;
815
816         if (len<2)
817                 return;
818
819         reason = take_hword(&pkt, &len);
820
821         sta = wihap_sta_find(whi, rxfrm->wi_addr2);
822         if (sta == NULL) {
823                 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
824                         printf("wihap_deauth_req: unknown station: %6D\n",
825                             rxfrm->wi_addr2, ":");
826         }
827         else
828                 wihap_sta_delete(sta);
829 }
830
831 /* wihap_disassoc_req()
832  *
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.
836  */
837 static void
838 wihap_disassoc_req(struct wi_softc *sc, struct wi_frame *rxfrm,
839     caddr_t pkt, int len)
840 {
841         struct wihap_info       *whi = &sc->wi_hostap_info;
842         struct wihap_sta_info   *sta;
843         u_int16_t               reason;
844
845         if (len < 2)
846                 return;
847
848         reason = take_hword(&pkt, &len);
849
850         sta = wihap_sta_find(whi, rxfrm->wi_addr2);
851         if (sta == NULL) {
852                 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
853                         printf("wihap_disassoc_req: unknown station: %6D\n",
854                             rxfrm->wi_addr2, ":");
855         }
856         else if (!(sta->flags & WI_SIFLAGS_AUTHEN)) {
857                 /*
858                  * If station is not authenticated, send deauthentication
859                  * frame.
860                  */
861                 wihap_sta_deauth(sc, rxfrm->wi_addr2,
862                     IEEE80211_REASON_NOT_AUTHED);
863                 return;
864         }
865         else
866                 sta->flags &= ~WI_SIFLAGS_ASSOC;
867 }
868
869 /* wihap_debug_frame_type()
870  *
871  * Print out frame type.  Used in early debugging.
872  */
873 static __inline void
874 wihap_debug_frame_type(struct wi_frame *rxfrm)
875 {
876         printf("wihap_mgmt_input: len=%d ", le16toh(rxfrm->wi_dat_len));
877
878         if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
879             htole16(WI_FTYPE_MGMT)) {
880
881                 printf("MGMT: ");
882
883                 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
884                 case WI_STYPE_MGMT_ASREQ:
885                         printf("assoc req: \n");
886                         break;
887                 case WI_STYPE_MGMT_ASRESP:
888                         printf("assoc resp: \n");
889                         break;
890                 case WI_STYPE_MGMT_REASREQ:
891                         printf("reassoc req: \n");
892                         break;
893                 case WI_STYPE_MGMT_REASRESP:
894                         printf("reassoc resp: \n");
895                         break;
896                 case WI_STYPE_MGMT_PROBEREQ:
897                         printf("probe req: \n");
898                         break;
899                 case WI_STYPE_MGMT_PROBERESP:
900                         printf("probe resp: \n");
901                         break;
902                 case WI_STYPE_MGMT_BEACON:
903                         printf("beacon: \n");
904                         break;
905                 case WI_STYPE_MGMT_ATIM:
906                         printf("ann traf ind \n");
907                         break;
908                 case WI_STYPE_MGMT_DISAS:
909                         printf("disassociation: \n");
910                         break;
911                 case WI_STYPE_MGMT_AUTH:
912                         printf("auth: \n");
913                         break;
914                 case WI_STYPE_MGMT_DEAUTH:
915                         printf("deauth: \n");
916                         break;
917                 default:
918                         printf("unknown (stype=0x%x)\n",
919                             le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE);
920                 }
921
922         }
923         else {
924                 printf("ftype=0x%x (ctl=0x%x)\n",
925                     le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_FTYPE,
926                     le16toh(rxfrm->wi_frame_ctl));
927         }
928 }
929
930 /* wihap_mgmt_input:
931  *
932  *      Called for each management frame received in host ap mode.
933  *      wihap_mgmt_input() is expected to free the mbuf.
934  */
935 void
936 owihap_mgmt_input(struct wi_softc *sc, struct wi_frame *rxfrm, struct mbuf *m)
937 {
938         caddr_t pkt;
939         int     s, len;
940
941         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
942                 wihap_debug_frame_type(rxfrm);
943
944         pkt = mtod(m, caddr_t) + WI_802_11_OFFSET_RAW;
945         len = m->m_len - WI_802_11_OFFSET_RAW;
946
947         if ((rxfrm->wi_frame_ctl & htole16(WI_FCTL_FTYPE)) ==
948             htole16(WI_FTYPE_MGMT)) {
949
950                 /* any of the following will mess w/ the station list */
951                 s = splnet();
952                 switch (le16toh(rxfrm->wi_frame_ctl) & WI_FCTL_STYPE) {
953                 case WI_STYPE_MGMT_ASREQ:
954                         wihap_assoc_req(sc, rxfrm, pkt, len);
955                         break;
956                 case WI_STYPE_MGMT_ASRESP:
957                         break;
958                 case WI_STYPE_MGMT_REASREQ:
959                         wihap_assoc_req(sc, rxfrm, pkt, len);
960                         break;
961                 case WI_STYPE_MGMT_REASRESP:
962                         break;
963                 case WI_STYPE_MGMT_PROBEREQ:
964                         break;
965                 case WI_STYPE_MGMT_PROBERESP:
966                         break;
967                 case WI_STYPE_MGMT_BEACON:
968                         break;
969                 case WI_STYPE_MGMT_ATIM:
970                         break;
971                 case WI_STYPE_MGMT_DISAS:
972                         wihap_disassoc_req(sc, rxfrm, pkt, len);
973                         break;
974                 case WI_STYPE_MGMT_AUTH:
975                         wihap_auth_req(sc, rxfrm, pkt, len);
976                         break;
977                 case WI_STYPE_MGMT_DEAUTH:
978                         wihap_deauth_req(sc, rxfrm, pkt, len);
979                         break;
980                 }
981                 splx(s);
982         }
983
984         m_freem(m);
985 }
986
987 /* wihap_sta_is_assoc()
988  *
989  *      Determine if a station is assoc'ed.  Update its activity
990  *      counter as a side-effect.
991  */
992 static int
993 wihap_sta_is_assoc(struct wihap_info *whi, u_int8_t addr[])
994 {
995         struct wihap_sta_info *sta;
996         int retval, s;
997
998         s = splnet();
999         retval = 0;
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);
1005                 retval = 1;
1006         }
1007         splx(s);
1008         return (retval);
1009 }
1010
1011 /* wihap_check_tx()
1012  *
1013  *      Determine if a station is assoc'ed, get its tx rate, and update
1014  *      its activity.
1015  */
1016 int
1017 owihap_check_tx(struct wihap_info *whi, u_int8_t addr[], u_int8_t *txrate)
1018 {
1019         struct wihap_sta_info *sta;
1020         static u_int8_t txratetable[] = { 10, 20, 55, 110 };
1021         int s;
1022
1023         if (addr[0] & 0x01) {
1024                 *txrate = 0; /* XXX: multicast rate? */
1025                 return(1);
1026         }
1027         s = splnet();
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 ];
1034                 splx(s);
1035                 return(1);
1036         }
1037         splx(s);
1038
1039         return(0);
1040 }
1041
1042 /*
1043  * wihap_data_input()
1044  *
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.
1048  *
1049  *      If wihap_data_input() returns a non-zero, it has processed
1050  *      the packet and will free the mbuf.
1051  */
1052 int
1053 owihap_data_input(struct wi_softc *sc, struct wi_frame *rxfrm, struct mbuf *m)
1054 {
1055         struct ifnet            *ifp = &sc->arpcom.ac_if;
1056         struct wihap_info       *whi = &sc->wi_hostap_info;
1057         struct wihap_sta_info   *sta;
1058         int                     mcast, s;
1059
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, ":");
1065                 m_freem(m);
1066                 return(1);
1067         }
1068
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, ":");
1074                 m_freem(m);
1075                 return (1);
1076         }
1077
1078         s = splnet();
1079
1080         /* Find source station. */
1081         sta = wihap_sta_find(whi, rxfrm->wi_addr2);
1082
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);
1090                 splx(s);
1091                 m_freem(m);
1092                 return(1);
1093         }
1094
1095         callout_reset(&sta->tmo, hz * whi->inactivity_time,
1096                       wihap_sta_timeout, sta);
1097         sta->sig_info = le16toh(rxfrm->wi_q_info);
1098
1099         splx(s);
1100
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)) {
1104
1105                 /* If it's multicast, make a copy.
1106                  */
1107                 if (mcast) {
1108                         m = m_copym(m, 0, M_COPYALL, MB_DONTWAIT);
1109                         if (m == NULL)
1110                                 return(0);
1111                         m->m_flags |= M_MCAST; /* XXX */
1112                 }
1113
1114                 /* Queue up for repeating.
1115                  */
1116                 IF_HANDOFF(&ifp->if_snd, m, ifp);
1117                 return (!mcast);
1118         }
1119
1120         return(0);
1121 }
1122
1123 /* wihap_ioctl()
1124  *
1125  *      Handle Host AP specific ioctls.  Called from wi_ioctl().
1126  */
1127 int
1128 owihap_ioctl(struct wi_softc *sc, u_long command, caddr_t data)
1129 {
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;
1138
1139         if (!(sc->arpcom.ac_if.if_flags & IFF_RUNNING))
1140                 return ENODEV;
1141
1142         switch (command) {
1143         case SIOCHOSTAP_DEL:
1144                 if ((error = suser(td)))
1145                         break;
1146                 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1147                         break;
1148                 s = splnet();
1149                 sta = wihap_sta_find(whi, reqsta.addr);
1150                 if (sta == NULL)
1151                         error = ENOENT;
1152                 else {
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);
1161
1162                         wihap_sta_delete(sta);
1163                 }
1164                 splx(s);
1165                 break;
1166
1167         case SIOCHOSTAP_GET:
1168                 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1169                         break;
1170                 s = splnet();
1171                 sta = wihap_sta_find(whi, reqsta.addr);
1172                 if (sta == NULL) {
1173                         error = ENOENT;
1174                         splx(s);
1175                 } else {
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;
1181                         splx(s);
1182                         error = copyout(&reqsta, ifr->ifr_data,
1183                             sizeof(reqsta));
1184                 }
1185                 break;
1186
1187         case SIOCHOSTAP_ADD:
1188                 if ((error = suser(td)))
1189                         break;
1190                 if ((error = copyin(ifr->ifr_data, &reqsta, sizeof(reqsta))))
1191                         break;
1192                 s = splnet();
1193                 sta = wihap_sta_find(whi, reqsta.addr);
1194                 if (sta != NULL) {
1195                         error = EEXIST;
1196                         splx(s);
1197                         break;
1198                 }
1199                 if (whi->n_stations >= WIHAP_MAX_STATIONS) {
1200                         error = ENOSPC;
1201                         splx(s);
1202                         break;
1203                 }
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);
1208                 splx(s);
1209                 break;
1210
1211         case SIOCHOSTAP_SFLAGS:
1212                 if ((error = suser(td)))
1213                         break;
1214                 if ((error = copyin(ifr->ifr_data, &flag, sizeof(int))))
1215                         break;
1216
1217                 whi->apflags = (whi->apflags & WIHAPFL_CANTCHANGE) |
1218                     (flag & ~WIHAPFL_CANTCHANGE);
1219                 break;
1220
1221         case SIOCHOSTAP_GFLAGS:
1222                 flag = (int) whi->apflags;
1223                 error = copyout(&flag, ifr->ifr_data, sizeof(int));
1224                 break;
1225
1226         case SIOCHOSTAP_GETALL:
1227                 if ((error = copyin(ifr->ifr_data, &reqall, sizeof(reqall))))
1228                         break;
1229
1230                 reqall.nstations = whi->n_stations;
1231                 n = 0;
1232                 s = splnet();
1233                 sta = LIST_FIRST(&whi->sta_list);
1234                 while (sta && reqall.size >= n+sizeof(struct hostap_sta)) {
1235
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;
1242
1243                         error = copyout(&stabuf, (caddr_t) reqall.addr + n,
1244                             sizeof(struct hostap_sta));
1245                         if (error)
1246                                 break;
1247
1248                         sta = LIST_NEXT(sta, list);
1249                         n += sizeof(struct hostap_sta);
1250                 }
1251                 splx(s);
1252
1253                 if (!error)
1254                         error = copyout(&reqall, ifr->ifr_data,
1255                             sizeof(reqall));
1256                 break;
1257         default:
1258                 printf("wihap_ioctl: i shouldn't get other ioctls!\n");
1259                 error = EINVAL;
1260         }
1261
1262         return(error);
1263 }