Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.9
      1 /*	$NetBSD: ieee80211_output.c,v 1.9 2003/11/02 00:17:27 dyoung Exp $	*/
      2 /*-
      3  * Copyright (c) 2001 Atsushi Onoe
      4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * Alternatively, this software may be distributed under the terms of the
     19  * GNU General Public License ("GPL") version 2 as published by the Free
     20  * Software Foundation.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifdef __FreeBSD__
     36 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_output.c,v 1.5 2003/09/01 02:55:09 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.9 2003/11/02 00:17:27 dyoung Exp $");
     39 #endif
     40 
     41 #include "opt_inet.h"
     42 
     43 #ifdef __NetBSD__
     44 #include "bpfilter.h"
     45 #endif /* __NetBSD__ */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/socket.h>
     53 #include <sys/sockio.h>
     54 #include <sys/endian.h>
     55 #include <sys/errno.h>
     56 #ifdef __FreeBSD__
     57 #include <sys/bus.h>
     58 #endif
     59 #include <sys/proc.h>
     60 #include <sys/sysctl.h>
     61 
     62 #ifdef __FreeBSD__
     63 #include <machine/atomic.h>
     64 #endif
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_media.h>
     69 #include <net/if_arp.h>
     70 #ifdef __FreeBSD__
     71 #include <net/ethernet.h>
     72 #else
     73 #include <net/if_ether.h>
     74 #endif
     75 #include <net/if_llc.h>
     76 
     77 #include <net80211/ieee80211_var.h>
     78 #include <net80211/ieee80211_compat.h>
     79 
     80 #if NBPFILTER > 0
     81 #include <net/bpf.h>
     82 #endif
     83 
     84 #ifdef INET
     85 #include <netinet/in.h>
     86 #ifdef __FreeBSD__
     87 #include <netinet/if_ether.h>
     88 #else
     89 #include <net/if_ether.h>
     90 #endif
     91 #endif
     92 
     93 /*
     94  * Send a management frame to the specified node.  The node pointer
     95  * must have a reference as the pointer will be passed to the driver
     96  * and potentially held for a long time.  If the frame is successfully
     97  * dispatched to the driver, then it is responsible for freeing the
     98  * reference (and potentially free'ing up any associated storage).
     99  */
    100 static int
    101 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
    102     struct mbuf *m, int type)
    103 {
    104 	struct ieee80211com *ic = (void *)ifp;
    105 	struct ieee80211_frame *wh;
    106 
    107 	KASSERT(ni != NULL, ("null node"));
    108 	ni->ni_inact = 0;
    109 
    110 	/*
    111 	 * Yech, hack alert!  We want to pass the node down to the
    112 	 * driver's start routine.  If we don't do so then the start
    113 	 * routine must immediately look it up again and that can
    114 	 * cause a lock order reversal if, for example, this frame
    115 	 * is being sent because the station is being timedout and
    116 	 * the frame being sent is a DEAUTH message.  We could stick
    117 	 * this in an m_tag and tack that on to the mbuf.  However
    118 	 * that's rather expensive to do for every frame so instead
    119 	 * we stuff it in the rcvif field since outbound frames do
    120 	 * not (presently) use this.
    121 	 */
    122 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    123 	if (m == NULL)
    124 		return ENOMEM;
    125 #ifdef __FreeBSD__
    126 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
    127 #endif
    128 	m->m_pkthdr.rcvif = (void *)ni;
    129 
    130 	wh = mtod(m, struct ieee80211_frame *);
    131 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
    132 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    133 	*(u_int16_t *)&wh->i_dur[0] = 0;
    134 	*(u_int16_t *)&wh->i_seq[0] =
    135 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    136 	ni->ni_txseq++;
    137 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
    138 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
    139 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    140 
    141 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
    142 		m->m_flags &= ~M_LINK0;
    143 		IEEE80211_DPRINTF(("%s: encrypting frame for %s\n", __func__,
    144 		    ether_sprintf(wh->i_addr1)));
    145 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    146 	}
    147 
    148 	if (ifp->if_flags & IFF_DEBUG) {
    149 		/* avoid to print too many frames */
    150 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
    151 #ifdef IEEE80211_DEBUG
    152 		    ieee80211_debug > 1 ||
    153 #endif
    154 		    (type & IEEE80211_FC0_SUBTYPE_MASK) !=
    155 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    156 			if_printf(ifp, "sending %s to %s on channel %u\n",
    157 			    ieee80211_mgt_subtype_name[
    158 			    (type & IEEE80211_FC0_SUBTYPE_MASK)
    159 			    >> IEEE80211_FC0_SUBTYPE_SHIFT],
    160 			    ether_sprintf(ni->ni_macaddr),
    161 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    162 	}
    163 
    164 	IF_ENQUEUE(&ic->ic_mgtq, m);
    165 	ifp->if_timer = 1;
    166 	(*ifp->if_start)(ifp);
    167 	return 0;
    168 }
    169 
    170 /*
    171  * Encapsulate an outbound data frame.  The mbuf chain is updated and
    172  * a reference to the destination node is returned.  If an error is
    173  * encountered NULL is returned and the node reference will also be NULL.
    174  *
    175  * NB: The caller is responsible for free'ing a returned node reference.
    176  *     The convention is ic_bss is not reference counted; the caller must
    177  *     maintain that.
    178  */
    179 struct mbuf *
    180 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
    181 {
    182 	struct ieee80211com *ic = (void *)ifp;
    183 	struct ether_header eh;
    184 	struct ieee80211_frame *wh;
    185 	struct ieee80211_node *ni = NULL;
    186 	struct llc *llc;
    187 
    188 	if (m->m_len < sizeof(struct ether_header)) {
    189 		m = m_pullup(m, sizeof(struct ether_header));
    190 		if (m == NULL) {
    191 			/* XXX statistic */
    192 			goto bad;
    193 		}
    194 	}
    195 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
    196 
    197 	if ((ni = ieee80211_find_txnode(ic, eh.ether_dhost)) == NULL)
    198 		goto bad;
    199 
    200 	ni->ni_inact = 0;
    201 
    202 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
    203 	llc = mtod(m, struct llc *);
    204 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    205 	llc->llc_control = LLC_UI;
    206 	llc->llc_snap.org_code[0] = 0;
    207 	llc->llc_snap.org_code[1] = 0;
    208 	llc->llc_snap.org_code[2] = 0;
    209 	llc->llc_snap.ether_type = eh.ether_type;
    210 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    211 	if (m == NULL)
    212 		goto bad;
    213 	wh = mtod(m, struct ieee80211_frame *);
    214 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
    215 	*(u_int16_t *)&wh->i_dur[0] = 0;
    216 	*(u_int16_t *)&wh->i_seq[0] =
    217 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    218 	ni->ni_txseq++;
    219 	switch (ic->ic_opmode) {
    220 	case IEEE80211_M_STA:
    221 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    222 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
    223 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    224 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
    225 		break;
    226 	case IEEE80211_M_IBSS:
    227 	case IEEE80211_M_AHDEMO:
    228 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    229 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    230 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    231 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    232 		break;
    233 	case IEEE80211_M_HOSTAP:
    234 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    235 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    236 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
    237 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
    238 		break;
    239 	case IEEE80211_M_MONITOR:
    240 		goto bad;
    241 	}
    242 	*pni = ni;
    243 	return m;
    244 bad:
    245 	if (m != NULL)
    246 		m_freem(m);
    247 	if (ni && ni != ic->ic_bss)
    248 		ieee80211_free_node(ic, ni);
    249 	*pni = NULL;
    250 	return NULL;
    251 }
    252 
    253 /*
    254  * Add a supported rates element id to a frame.
    255  */
    256 u_int8_t *
    257 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    258 {
    259 	int nrates;
    260 
    261 	*frm++ = IEEE80211_ELEMID_RATES;
    262 	nrates = rs->rs_nrates;
    263 	if (nrates > IEEE80211_RATE_SIZE)
    264 		nrates = IEEE80211_RATE_SIZE;
    265 	*frm++ = nrates;
    266 	memcpy(frm, rs->rs_rates, nrates);
    267 	return frm + nrates;
    268 }
    269 
    270 /*
    271  * Add an extended supported rates element id to a frame.
    272  */
    273 u_int8_t *
    274 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    275 {
    276 	/*
    277 	 * Add an extended supported rates element if operating in 11g mode.
    278 	 */
    279 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    280 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    281 		*frm++ = IEEE80211_ELEMID_XRATES;
    282 		*frm++ = nrates;
    283 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    284 		frm += nrates;
    285 	}
    286 	return frm;
    287 }
    288 
    289 /*
    290  * Add an ssid elemet to a frame.
    291  */
    292 static u_int8_t *
    293 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    294 {
    295 	*frm++ = IEEE80211_ELEMID_SSID;
    296 	*frm++ = len;
    297 	memcpy(frm, ssid, len);
    298 	return frm + len;
    299 }
    300 
    301 static struct mbuf *
    302 ieee80211_getmbuf(int flags, int type, u_int pktlen)
    303 {
    304 	struct mbuf *m;
    305 
    306 	KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    307 #ifdef __FreeBSD__
    308 	if (pktlen <= MHLEN)
    309 		MGETHDR(m, flags, type);
    310 	else
    311 		m = m_getcl(flags, type, M_PKTHDR);
    312 #else
    313 	MGETHDR(m, flags, type);
    314 	if (m != NULL && pktlen > MHLEN)
    315 		MCLGET(m, flags);
    316 #endif
    317 	return m;
    318 }
    319 
    320 /*
    321  * Send a management frame.  The node is for the destination (or ic_bss
    322  * when in station mode).  Nodes other than ic_bss have their reference
    323  * count bumped to reflect our use for an indeterminant time.
    324  */
    325 int
    326 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    327 	int type, int arg)
    328 {
    329 #define	senderr(_x)	do { ret = _x; goto bad; } while (0)
    330 	struct ifnet *ifp = &ic->ic_if;
    331 	struct mbuf *m;
    332 	u_int8_t *frm;
    333 	enum ieee80211_phymode mode;
    334 	u_int16_t capinfo;
    335 	int has_challenge, is_shared_key, ret, timer;
    336 
    337 	KASSERT(ni != NULL, ("null node"));
    338 
    339 	/*
    340 	 * Hold a reference on the node so it doesn't go away until after
    341 	 * the xmit is complete all the way in the driver.  On error we
    342 	 * will remove our reference.
    343 	 */
    344 	if (ni != ic->ic_bss)
    345 		ieee80211_ref_node(ni);
    346 	timer = 0;
    347 	switch (type) {
    348 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    349 		/*
    350 		 * prreq frame format
    351 		 *	[tlv] ssid
    352 		 *	[tlv] supported rates
    353 		 *	[tlv] extended supported rates
    354 		 */
    355 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    356 			 2 + ic->ic_des_esslen
    357 		       + 2 + IEEE80211_RATE_SIZE
    358 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    359 		if (m == NULL)
    360 			senderr(ENOMEM);
    361 		m->m_data += sizeof(struct ieee80211_frame);
    362 		frm = mtod(m, u_int8_t *);
    363 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    364 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    365 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    366 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    367 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    368 
    369 		timer = IEEE80211_TRANS_WAIT;
    370 		break;
    371 
    372 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    373 		/*
    374 		 * probe response frame format
    375 		 *	[8] time stamp
    376 		 *	[2] beacon interval
    377 		 *	[2] cabability information
    378 		 *	[tlv] ssid
    379 		 *	[tlv] supported rates
    380 		 *	[tlv] parameter set (IBSS)
    381 		 *	[tlv] extended supported rates
    382 		 */
    383 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    384 			 8 + 2 + 2 + 2
    385 		       + 2 + ni->ni_esslen
    386 		       + 2 + IEEE80211_RATE_SIZE
    387 		       + 6
    388 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    389 		if (m == NULL)
    390 			senderr(ENOMEM);
    391 		m->m_data += sizeof(struct ieee80211_frame);
    392 		frm = mtod(m, u_int8_t *);
    393 
    394 		memset(frm, 0, 8);	/* timestamp should be filled later */
    395 		frm += 8;
    396 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    397 		frm += 2;
    398 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    399 			capinfo = IEEE80211_CAPINFO_IBSS;
    400 		else
    401 			capinfo = IEEE80211_CAPINFO_ESS;
    402 		if (ic->ic_flags & IEEE80211_F_WEPON)
    403 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    404 		*(u_int16_t *)frm = htole16(capinfo);
    405 		frm += 2;
    406 
    407 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    408 				ic->ic_bss->ni_esslen);
    409 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    410 
    411                 switch (ic->ic_phytype) {
    412                 case IEEE80211_T_FH:
    413                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    414                         *frm++ = 5;
    415                         *frm++ = ni->ni_fhdwell & 0x00ff;
    416                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    417                         *frm++ = IEEE80211_FH_CHANSET(
    418 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    419                         *frm++ = IEEE80211_FH_CHANPAT(
    420 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    421                         *frm++ = ni->ni_fhindex;
    422                         break;
    423 		case IEEE80211_T_OFDM:	/* probably multi-mode */
    424 		default:
    425 			if (ieee80211_chan2mode(ic, ni->ni_chan) !=
    426 			    IEEE80211_MODE_11B) {
    427 				if_printf(ifp, "unhandled mode %d for %s\n",
    428 				    ieee80211_chan2mode(ic, ni->ni_chan),
    429 				    ether_sprintf(ni->ni_macaddr));
    430 				break;
    431 			}
    432 			/*FALLTHROUGH*/
    433                 case IEEE80211_T_DS:
    434                         *frm++ = IEEE80211_ELEMID_DSPARMS;
    435                         *frm++ = 1;
    436                         *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    437                         break;
    438                 }
    439 
    440 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    441 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    442 			*frm++ = 2;
    443 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    444 		} else {	/* IEEE80211_M_HOSTAP */
    445 			/* TODO: TIM */
    446 			*frm++ = IEEE80211_ELEMID_TIM;
    447 			*frm++ = 4;	/* length */
    448 			*frm++ = 0;	/* DTIM count */
    449 			*frm++ = 1;	/* DTIM period */
    450 			*frm++ = 0;	/* bitmap control */
    451 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    452 		}
    453 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    454 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    455 		break;
    456 
    457 	case IEEE80211_FC0_SUBTYPE_AUTH:
    458 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    459 		if (m == NULL)
    460 			senderr(ENOMEM);
    461 
    462 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
    463 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
    464 		    ni->ni_challenge != NULL);
    465 
    466 		is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
    467 		    arg == IEEE80211_AUTH_SHARED_PASS);
    468 
    469 		if (has_challenge) {
    470 			MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
    471 			m->m_pkthdr.len = m->m_len =
    472 			    2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
    473 		} else {
    474 			MH_ALIGN(m, 2 * 3);
    475 			m->m_pkthdr.len = m->m_len = 2 * 3;
    476 		}
    477 		frm = mtod(m, u_int8_t *);
    478 		((u_int16_t *)frm)[0] =
    479 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
    480 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
    481 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    482 		((u_int16_t *)frm)[2] = 0;		/* status */
    483 
    484 		if (has_challenge) {
    485 			((u_int16_t *)frm)[3] =
    486 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
    487 			    IEEE80211_ELEMID_CHALLENGE);
    488 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
    489 			    IEEE80211_CHALLENGE_LEN);
    490 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
    491 				IEEE80211_DPRINTF((
    492 				    "%s: request encrypt frame\n", __func__));
    493 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
    494 			}
    495 		}
    496 		if (ic->ic_opmode == IEEE80211_M_STA)
    497 			timer = IEEE80211_TRANS_WAIT;
    498 		break;
    499 
    500 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    501 		if (ifp->if_flags & IFF_DEBUG)
    502 			if_printf(ifp, "station %s deauthenticate (reason %d)\n",
    503 			    ether_sprintf(ni->ni_macaddr), arg);
    504 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    505 		if (m == NULL)
    506 			senderr(ENOMEM);
    507 		MH_ALIGN(m, 2);
    508 		m->m_pkthdr.len = m->m_len = 2;
    509 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    510 		break;
    511 
    512 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    513 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    514 		/*
    515 		 * asreq frame format
    516 		 *	[2] capability information
    517 		 *	[2] listen interval
    518 		 *	[6*] current AP address (reassoc only)
    519 		 *	[tlv] ssid
    520 		 *	[tlv] supported rates
    521 		 *	[tlv] extended supported rates
    522 		 */
    523 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    524 			 sizeof(capinfo)
    525 		       + sizeof(u_int16_t)
    526 		       + IEEE80211_ADDR_LEN
    527 		       + 2 + ni->ni_esslen
    528 		       + 2 + IEEE80211_RATE_SIZE
    529 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    530 		if (m == NULL)
    531 			senderr(ENOMEM);
    532 		m->m_data += sizeof(struct ieee80211_frame);
    533 		frm = mtod(m, u_int8_t *);
    534 
    535 		capinfo = 0;
    536 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    537 			capinfo |= IEEE80211_CAPINFO_IBSS;
    538 		else		/* IEEE80211_M_STA */
    539 			capinfo |= IEEE80211_CAPINFO_ESS;
    540 		if (ic->ic_flags & IEEE80211_F_WEPON)
    541 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    542 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
    543 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    544 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    545 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    546 		*(u_int16_t *)frm = htole16(capinfo);
    547 		frm += 2;
    548 
    549 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    550 		frm += 2;
    551 
    552 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    553 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    554 			frm += IEEE80211_ADDR_LEN;
    555 		}
    556 
    557 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    558 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    559 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    560 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    561 
    562 		timer = IEEE80211_TRANS_WAIT;
    563 		break;
    564 
    565 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    566 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    567 		/*
    568 		 * asreq frame format
    569 		 *	[2] capability information
    570 		 *	[2] status
    571 		 *	[2] association ID
    572 		 *	[tlv] supported rates
    573 		 *	[tlv] extended supported rates
    574 		 */
    575 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    576 			 sizeof(capinfo)
    577 		       + sizeof(u_int16_t)
    578 		       + sizeof(u_int16_t)
    579 		       + 2 + IEEE80211_RATE_SIZE
    580 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    581 		if (m == NULL)
    582 			senderr(ENOMEM);
    583 		m->m_data += sizeof(struct ieee80211_frame);
    584 		frm = mtod(m, u_int8_t *);
    585 
    586 		capinfo = IEEE80211_CAPINFO_ESS;
    587 		if (ic->ic_flags & IEEE80211_F_WEPON)
    588 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    589 		*(u_int16_t *)frm = htole16(capinfo);
    590 		frm += 2;
    591 
    592 		*(u_int16_t *)frm = htole16(arg);	/* status */
    593 		frm += 2;
    594 
    595 		if (arg == IEEE80211_STATUS_SUCCESS)
    596 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    597 		frm += 2;
    598 
    599 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    600 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    601 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    602 		break;
    603 
    604 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    605 		if (ifp->if_flags & IFF_DEBUG)
    606 			if_printf(ifp, "station %s disassociate (reason %d)\n",
    607 			    ether_sprintf(ni->ni_macaddr), arg);
    608 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    609 		if (m == NULL)
    610 			senderr(ENOMEM);
    611 		MH_ALIGN(m, 2);
    612 		m->m_pkthdr.len = m->m_len = 2;
    613 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    614 		break;
    615 
    616 	default:
    617 		IEEE80211_DPRINTF(("%s: invalid mgmt frame type %u\n",
    618 			__func__, type));
    619 		senderr(EINVAL);
    620 		/* NOTREACHED */
    621 	}
    622 
    623 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    624 	if (ret == 0) {
    625 		if (timer)
    626 			ic->ic_mgt_timer = timer;
    627 	} else {
    628 bad:
    629 		if (ni != ic->ic_bss)		/* remove ref we added */
    630 			ieee80211_free_node(ic, ni);
    631 	}
    632 	return ret;
    633 #undef senderr
    634 }
    635 
    636 void
    637 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    638 		  struct mbuf *m)
    639 {
    640 	/* Store the new packet on our queue, changing the TIM if necessary */
    641 
    642 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    643 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    644 	}
    645 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    646 		IF_DROP(&ni->ni_savedq);
    647 		m_freem(m);
    648 		if (ic->ic_if.if_flags & IFF_DEBUG)
    649 			printf("%s: station %s power save queue overflow"
    650 			       " of size %d drops %d\n",
    651 			       ic->ic_if.if_xname,
    652 			       ether_sprintf(ni->ni_macaddr),
    653 			       IEEE80211_PS_MAX_QUEUE,
    654 			       ni->ni_savedq.ifq_drops);
    655 	} else {
    656 		/* Similar to ieee80211_mgmt_output, store the node in
    657 		 * the rcvif field.
    658 		 */
    659 		IF_ENQUEUE(&ni->ni_savedq, m);
    660 		m->m_pkthdr.rcvif = (void *)ni;
    661 	}
    662 }
    663 
    664