Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.17
      1 /*	$NetBSD: ieee80211_output.c,v 1.17 2004/08/10 00:57:22 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.10 2004/04/02 23:25:39 sam Exp $");
     37 #else
     38 __KERNEL_RCSID(0, "$NetBSD: ieee80211_output.c,v 1.17 2004/08/10 00:57:22 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 #ifdef IEEE80211_DEBUG
     94 /*
     95  * Decide if an outbound management frame should be
     96  * printed when debugging is enabled.  This filters some
     97  * of the less interesting frames that come frequently
     98  * (e.g. beacons).
     99  */
    100 static __inline int
    101 doprint(struct ieee80211com *ic, int subtype)
    102 {
    103 	switch (subtype) {
    104 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    105 		return (ic->ic_opmode == IEEE80211_M_IBSS);
    106 	}
    107 	return 1;
    108 }
    109 #endif
    110 
    111 /*
    112  * Send a management frame to the specified node.  The node pointer
    113  * must have a reference as the pointer will be passed to the driver
    114  * and potentially held for a long time.  If the frame is successfully
    115  * dispatched to the driver, then it is responsible for freeing the
    116  * reference (and potentially free'ing up any associated storage).
    117  */
    118 static int
    119 ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
    120     struct mbuf *m, int type)
    121 {
    122 	struct ieee80211com *ic = (void *)ifp;
    123 	struct ieee80211_frame *wh;
    124 
    125 	IASSERT(ni != NULL, ("null node"));
    126 	ni->ni_inact = 0;
    127 
    128 	/*
    129 	 * Yech, hack alert!  We want to pass the node down to the
    130 	 * driver's start routine.  If we don't do so then the start
    131 	 * routine must immediately look it up again and that can
    132 	 * cause a lock order reversal if, for example, this frame
    133 	 * is being sent because the station is being timedout and
    134 	 * the frame being sent is a DEAUTH message.  We could stick
    135 	 * this in an m_tag and tack that on to the mbuf.  However
    136 	 * that's rather expensive to do for every frame so instead
    137 	 * we stuff it in the rcvif field since outbound frames do
    138 	 * not (presently) use this.
    139 	 */
    140 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    141 	if (m == NULL)
    142 		return ENOMEM;
    143 #ifdef __FreeBSD__
    144 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
    145 #endif
    146 	m->m_pkthdr.rcvif = (void *)ni;
    147 
    148 	wh = mtod(m, struct ieee80211_frame *);
    149 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
    150 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    151 	*(u_int16_t *)&wh->i_dur[0] = 0;
    152 	*(u_int16_t *)&wh->i_seq[0] =
    153 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    154 	ni->ni_txseq++;
    155 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
    156 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
    157 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    158 
    159 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
    160 		m->m_flags &= ~M_LINK0;
    161 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    162 			("%s: encrypting frame for %s\n",
    163 			__func__, ether_sprintf(wh->i_addr1)));
    164 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    165 	}
    166 #ifdef IEEE80211_DEBUG
    167 	/* avoid printing too many frames */
    168 	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
    169 	    ieee80211_msg_dumppkts(ic)) {
    170 		if_printf(ifp, "sending %s to %s on channel %u\n",
    171 		    ieee80211_mgt_subtype_name[
    172 		    (type & IEEE80211_FC0_SUBTYPE_MASK)
    173 		    >> IEEE80211_FC0_SUBTYPE_SHIFT],
    174 		    ether_sprintf(ni->ni_macaddr),
    175 		    ieee80211_chan2ieee(ic, ni->ni_chan));
    176 	}
    177 #endif
    178 	IF_ENQUEUE(&ic->ic_mgtq, m);
    179 	ifp->if_timer = 1;
    180 	(*ifp->if_start)(ifp);
    181 	return 0;
    182 }
    183 
    184 /*
    185  * Encapsulate an outbound data frame.  The mbuf chain is updated and
    186  * a reference to the destination node is returned.  If an error is
    187  * encountered NULL is returned and the node reference will also be NULL.
    188  *
    189  * NB: The caller is responsible for free'ing a returned node reference.
    190  *     The convention is ic_bss is not reference counted; the caller must
    191  *     maintain that.
    192  */
    193 struct mbuf *
    194 ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
    195 {
    196 	struct ieee80211com *ic = (void *)ifp;
    197 	struct ether_header eh;
    198 	struct ieee80211_frame *wh;
    199 	struct ieee80211_node *ni = NULL;
    200 	struct llc *llc;
    201 
    202 	if (m->m_len < sizeof(struct ether_header)) {
    203 		m = m_pullup(m, sizeof(struct ether_header));
    204 		if (m == NULL) {
    205 			ic->ic_stats.is_tx_nombuf++;
    206 			goto bad;
    207 		}
    208 	}
    209 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
    210 
    211 	ni = ieee80211_find_txnode(ic, eh.ether_dhost);
    212 	if (ni == NULL) {
    213 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
    214 			("%s: no node for dst %s, discard frame\n",
    215 			__func__, ether_sprintf(eh.ether_dhost)));
    216 		ic->ic_stats.is_tx_nonode++;
    217 		goto bad;
    218 	}
    219 	ni->ni_inact = 0;
    220 
    221 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
    222 	llc = mtod(m, struct llc *);
    223 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    224 	llc->llc_control = LLC_UI;
    225 	llc->llc_snap.org_code[0] = 0;
    226 	llc->llc_snap.org_code[1] = 0;
    227 	llc->llc_snap.org_code[2] = 0;
    228 	llc->llc_snap.ether_type = eh.ether_type;
    229 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    230 	if (m == NULL) {
    231 		ic->ic_stats.is_tx_nombuf++;
    232 		goto bad;
    233 	}
    234 	wh = mtod(m, struct ieee80211_frame *);
    235 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
    236 	*(u_int16_t *)&wh->i_dur[0] = 0;
    237 	*(u_int16_t *)&wh->i_seq[0] =
    238 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    239 	ni->ni_txseq++;
    240 	switch (ic->ic_opmode) {
    241 	case IEEE80211_M_STA:
    242 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    243 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
    244 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    245 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
    246 		break;
    247 	case IEEE80211_M_IBSS:
    248 	case IEEE80211_M_AHDEMO:
    249 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    250 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    251 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    252 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    253 		break;
    254 	case IEEE80211_M_HOSTAP:
    255 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    256 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    257 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
    258 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
    259 		break;
    260 	case IEEE80211_M_MONITOR:
    261 		goto bad;
    262 	}
    263 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
    264 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    265 	*pni = ni;
    266 	return m;
    267 bad:
    268 	if (m != NULL)
    269 		m_freem(m);
    270 	if (ni != NULL)
    271 		ieee80211_release_node(ic, ni);
    272 	*pni = NULL;
    273 	return NULL;
    274 }
    275 
    276 /*
    277  * Add a supported rates element id to a frame.
    278  */
    279 u_int8_t *
    280 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    281 {
    282 	int nrates;
    283 
    284 	*frm++ = IEEE80211_ELEMID_RATES;
    285 	nrates = rs->rs_nrates;
    286 	if (nrates > IEEE80211_RATE_SIZE)
    287 		nrates = IEEE80211_RATE_SIZE;
    288 	*frm++ = nrates;
    289 	memcpy(frm, rs->rs_rates, nrates);
    290 	return frm + nrates;
    291 }
    292 
    293 /*
    294  * Add an extended supported rates element id to a frame.
    295  */
    296 u_int8_t *
    297 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    298 {
    299 	/*
    300 	 * Add an extended supported rates element if operating in 11g mode.
    301 	 */
    302 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    303 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    304 		*frm++ = IEEE80211_ELEMID_XRATES;
    305 		*frm++ = nrates;
    306 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    307 		frm += nrates;
    308 	}
    309 	return frm;
    310 }
    311 
    312 /*
    313  * Add an ssid elemet to a frame.
    314  */
    315 static u_int8_t *
    316 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    317 {
    318 	*frm++ = IEEE80211_ELEMID_SSID;
    319 	*frm++ = len;
    320 	memcpy(frm, ssid, len);
    321 	return frm + len;
    322 }
    323 
    324 static struct mbuf *
    325 ieee80211_getmbuf(int flags, int type, u_int pktlen)
    326 {
    327 	struct mbuf *m;
    328 
    329 	IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    330 #ifdef __FreeBSD__
    331 	if (pktlen <= MHLEN)
    332 		MGETHDR(m, flags, type);
    333 	else
    334 		m = m_getcl(flags, type, M_PKTHDR);
    335 #else
    336 	MGETHDR(m, flags, type);
    337 	if (m != NULL && pktlen > MHLEN)
    338 		MCLGET(m, flags);
    339 #endif
    340 	return m;
    341 }
    342 
    343 /*
    344  * Send a management frame.  The node is for the destination (or ic_bss
    345  * when in station mode).  Nodes other than ic_bss have their reference
    346  * count bumped to reflect our use for an indeterminant time.
    347  */
    348 int
    349 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    350 	int type, int arg)
    351 {
    352 #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
    353 	struct ifnet *ifp = &ic->ic_if;
    354 	struct mbuf *m;
    355 	u_int8_t *frm;
    356 	enum ieee80211_phymode mode;
    357 	u_int16_t capinfo;
    358 	int has_challenge, is_shared_key, ret, timer;
    359 
    360 	IASSERT(ni != NULL, ("null node"));
    361 
    362 	/*
    363 	 * Hold a reference on the node so it doesn't go away until after
    364 	 * the xmit is complete all the way in the driver.  On error we
    365 	 * will remove our reference.
    366 	 */
    367 	ieee80211_ref_node(ni);
    368 	timer = 0;
    369 	switch (type) {
    370 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    371 		/*
    372 		 * prreq frame format
    373 		 *	[tlv] ssid
    374 		 *	[tlv] supported rates
    375 		 *	[tlv] extended supported rates
    376 		 */
    377 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    378 			 2 + ic->ic_des_esslen
    379 		       + 2 + IEEE80211_RATE_SIZE
    380 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    381 		if (m == NULL)
    382 			senderr(ENOMEM, is_tx_nombuf);
    383 		m->m_data += sizeof(struct ieee80211_frame);
    384 		frm = mtod(m, u_int8_t *);
    385 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    386 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    387 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    388 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    389 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    390 
    391 		timer = IEEE80211_TRANS_WAIT;
    392 		break;
    393 
    394 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    395 		/*
    396 		 * probe response frame format
    397 		 *	[8] time stamp
    398 		 *	[2] beacon interval
    399 		 *	[2] cabability information
    400 		 *	[tlv] ssid
    401 		 *	[tlv] supported rates
    402 		 *	[tlv] parameter set (FH/DS)
    403 		 *	[tlv] parameter set (IBSS)
    404 		 *	[tlv] extended supported rates
    405 		 */
    406 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    407 			 8 + 2 + 2 + 2
    408 		       + 2 + ni->ni_esslen
    409 		       + 2 + IEEE80211_RATE_SIZE
    410 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    411 		       + 6
    412 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    413 		if (m == NULL)
    414 			senderr(ENOMEM, is_tx_nombuf);
    415 		m->m_data += sizeof(struct ieee80211_frame);
    416 		frm = mtod(m, u_int8_t *);
    417 
    418 		memset(frm, 0, 8);	/* timestamp should be filled later */
    419 		frm += 8;
    420 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    421 		frm += 2;
    422 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    423 			capinfo = IEEE80211_CAPINFO_IBSS;
    424 		else
    425 			capinfo = IEEE80211_CAPINFO_ESS;
    426 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    427 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    428 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    429 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    430 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    431 		*(u_int16_t *)frm = htole16(capinfo);
    432 		frm += 2;
    433 
    434 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    435 				ic->ic_bss->ni_esslen);
    436 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    437 
    438 		if (ic->ic_phytype == IEEE80211_T_FH) {
    439                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    440                         *frm++ = 5;
    441                         *frm++ = ni->ni_fhdwell & 0x00ff;
    442                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    443                         *frm++ = IEEE80211_FH_CHANSET(
    444 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    445                         *frm++ = IEEE80211_FH_CHANPAT(
    446 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    447                         *frm++ = ni->ni_fhindex;
    448 		} else {
    449 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    450 			*frm++ = 1;
    451 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    452 		}
    453 
    454 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    455 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    456 			*frm++ = 2;
    457 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    458 		} else {	/* IEEE80211_M_HOSTAP */
    459 			/* TODO: TIM */
    460 			*frm++ = IEEE80211_ELEMID_TIM;
    461 			*frm++ = 4;	/* length */
    462 			*frm++ = 0;	/* DTIM count */
    463 			*frm++ = 1;	/* DTIM period */
    464 			*frm++ = 0;	/* bitmap control */
    465 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    466 		}
    467 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    468 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    469 		break;
    470 
    471 	case IEEE80211_FC0_SUBTYPE_AUTH:
    472 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    473 		if (m == NULL)
    474 			senderr(ENOMEM, is_tx_nombuf);
    475 
    476 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
    477 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
    478 		    ni->ni_challenge != NULL);
    479 
    480 		is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
    481 		    arg == IEEE80211_AUTH_SHARED_PASS);
    482 
    483 		if (has_challenge) {
    484 			MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
    485 			m->m_pkthdr.len = m->m_len =
    486 			    2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
    487 		} else {
    488 			MH_ALIGN(m, 2 * 3);
    489 			m->m_pkthdr.len = m->m_len = 2 * 3;
    490 		}
    491 		frm = mtod(m, u_int8_t *);
    492 		((u_int16_t *)frm)[0] =
    493 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
    494 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
    495 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    496 		((u_int16_t *)frm)[2] = 0;		/* status */
    497 
    498 		if (has_challenge) {
    499 			((u_int16_t *)frm)[3] =
    500 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
    501 			    IEEE80211_ELEMID_CHALLENGE);
    502 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
    503 			    IEEE80211_CHALLENGE_LEN);
    504 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
    505 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    506 				    ("%s: request encrypt frame\n", __func__));
    507 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
    508 			}
    509 		}
    510 		if (ic->ic_opmode == IEEE80211_M_STA)
    511 			timer = IEEE80211_TRANS_WAIT;
    512 		break;
    513 
    514 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    515 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    516 			("send station %s deauthenticate (reason %d)\n",
    517 			ether_sprintf(ni->ni_macaddr), arg));
    518 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    519 		if (m == NULL)
    520 			senderr(ENOMEM, is_tx_nombuf);
    521 		MH_ALIGN(m, 2);
    522 		m->m_pkthdr.len = m->m_len = 2;
    523 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    524 		break;
    525 
    526 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    527 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    528 		/*
    529 		 * asreq frame format
    530 		 *	[2] capability information
    531 		 *	[2] listen interval
    532 		 *	[6*] current AP address (reassoc only)
    533 		 *	[tlv] ssid
    534 		 *	[tlv] supported rates
    535 		 *	[tlv] extended supported rates
    536 		 */
    537 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    538 			 sizeof(capinfo)
    539 		       + sizeof(u_int16_t)
    540 		       + IEEE80211_ADDR_LEN
    541 		       + 2 + ni->ni_esslen
    542 		       + 2 + IEEE80211_RATE_SIZE
    543 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    544 		if (m == NULL)
    545 			senderr(ENOMEM, is_tx_nombuf);
    546 		m->m_data += sizeof(struct ieee80211_frame);
    547 		frm = mtod(m, u_int8_t *);
    548 
    549 		capinfo = 0;
    550 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    551 			capinfo |= IEEE80211_CAPINFO_IBSS;
    552 		else		/* IEEE80211_M_STA */
    553 			capinfo |= IEEE80211_CAPINFO_ESS;
    554 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    555 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    556 		/*
    557 		 * NB: Some 11a AP's reject the request when
    558 		 *     short premable is set.
    559 		 */
    560 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    561 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    562 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    563 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    564 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    565 		*(u_int16_t *)frm = htole16(capinfo);
    566 		frm += 2;
    567 
    568 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    569 		frm += 2;
    570 
    571 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    572 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    573 			frm += IEEE80211_ADDR_LEN;
    574 		}
    575 
    576 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    577 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    578 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    579 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    580 
    581 		timer = IEEE80211_TRANS_WAIT;
    582 		break;
    583 
    584 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    585 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    586 		/*
    587 		 * asreq frame format
    588 		 *	[2] capability information
    589 		 *	[2] status
    590 		 *	[2] association ID
    591 		 *	[tlv] supported rates
    592 		 *	[tlv] extended supported rates
    593 		 */
    594 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    595 			 sizeof(capinfo)
    596 		       + sizeof(u_int16_t)
    597 		       + sizeof(u_int16_t)
    598 		       + 2 + IEEE80211_RATE_SIZE
    599 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    600 		if (m == NULL)
    601 			senderr(ENOMEM, is_tx_nombuf);
    602 		m->m_data += sizeof(struct ieee80211_frame);
    603 		frm = mtod(m, u_int8_t *);
    604 
    605 		capinfo = IEEE80211_CAPINFO_ESS;
    606 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    607 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    608 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    609 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    610 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    611 		*(u_int16_t *)frm = htole16(capinfo);
    612 		frm += 2;
    613 
    614 		*(u_int16_t *)frm = htole16(arg);	/* status */
    615 		frm += 2;
    616 
    617 		if (arg == IEEE80211_STATUS_SUCCESS)
    618 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    619 		frm += 2;
    620 
    621 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    622 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    623 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    624 		break;
    625 
    626 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    627 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    628 			("send station %s disassociate (reason %d)\n",
    629 			ether_sprintf(ni->ni_macaddr), arg));
    630 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    631 		if (m == NULL)
    632 			senderr(ENOMEM, is_tx_nombuf);
    633 		MH_ALIGN(m, 2);
    634 		m->m_pkthdr.len = m->m_len = 2;
    635 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    636 		break;
    637 
    638 	default:
    639 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    640 			("%s: invalid mgmt frame type %u\n", __func__, type));
    641 		senderr(EINVAL, is_tx_unknownmgt);
    642 		/* NOTREACHED */
    643 	}
    644 
    645 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    646 	if (ret == 0) {
    647 		if (timer)
    648 			ic->ic_mgt_timer = timer;
    649 	} else {
    650 bad:
    651 		ieee80211_release_node(ic, ni);
    652 	}
    653 	return ret;
    654 #undef senderr
    655 }
    656 
    657 void
    658 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    659 		  struct mbuf *m)
    660 {
    661 	/* Store the new packet on our queue, changing the TIM if necessary */
    662 
    663 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    664 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    665 	}
    666 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    667 		IF_DROP(&ni->ni_savedq);
    668 		m_freem(m);
    669 		if (ic->ic_if.if_flags & IFF_DEBUG)
    670 			printf("%s: station %s power save queue overflow"
    671 			       " of size %d drops %d\n",
    672 			       ic->ic_if.if_xname,
    673 			       ether_sprintf(ni->ni_macaddr),
    674 			       IEEE80211_PS_MAX_QUEUE,
    675 			       ni->ni_savedq.ifq_drops);
    676 	} else {
    677 		/* Similar to ieee80211_mgmt_output, store the node in
    678 		 * the rcvif field.
    679 		 */
    680 		IF_ENQUEUE(&ni->ni_savedq, m);
    681 		m->m_pkthdr.rcvif = (void *)ni;
    682 	}
    683 }
    684 
    685