Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.24
      1 /*	$NetBSD: ieee80211_output.c,v 1.24 2004/12/27 10:47:57 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.24 2004/12/27 10:47:57 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  * Arguments in:
    278  *
    279  * paylen:  payload length (no FCS, no WEP header)
    280  *
    281  * hdrlen:  header length
    282  *
    283  * rate:    MSDU speed, units 500kb/s
    284  *
    285  * flags:   IEEE80211_F_SHPREAMBLE (use short preamble),
    286  *          IEEE80211_F_SHSLOT (use short slot length)
    287  *
    288  * Arguments out:
    289  *
    290  * d:       802.11 Duration field for RTS,
    291  *          802.11 Duration field for data frame,
    292  *          PLCP Length for data frame,
    293  *          residual octets at end of data slot
    294  */
    295 static int
    296 ieee80211_compute_duration1(int len, int use_ack, uint32_t flags, int rate,
    297     struct ieee80211_duration *d)
    298 {
    299 	int pre, ctsrate;
    300 	int ack, bitlen, data_dur, remainder;
    301 
    302 	/* RTS reserves medium for SIFS | CTS | SIFS | (DATA) | SIFS | ACK
    303 	 * DATA reserves medium for SIFS | ACK
    304 	 *
    305 	 * XXXMYC: no ACK on multicast/broadcast or control packets
    306 	 */
    307 
    308 	bitlen = len * 8;
    309 
    310 	pre = IEEE80211_DUR_DS_SIFS;
    311 	if ((flags & IEEE80211_F_SHPREAMBLE) != 0)
    312 		pre += IEEE80211_DUR_DS_SHORT_PREAMBLE + IEEE80211_DUR_DS_FAST_PLCPHDR;
    313 	else
    314 		pre += IEEE80211_DUR_DS_LONG_PREAMBLE + IEEE80211_DUR_DS_SLOW_PLCPHDR;
    315 
    316 	d->d_residue = 0;
    317 	data_dur = (bitlen * 2) / rate;
    318 	remainder = (bitlen * 2) % rate;
    319 	if (remainder != 0) {
    320 		d->d_residue = (rate - remainder) / 16;
    321 		data_dur++;
    322 	}
    323 
    324 	switch (rate) {
    325 	case 2:		/* 1 Mb/s */
    326 	case 4:		/* 2 Mb/s */
    327 		/* 1 - 2 Mb/s WLAN: send ACK/CTS at 1 Mb/s */
    328 		ctsrate = 2;
    329 		break;
    330 	case 11:	/* 5.5 Mb/s */
    331 	case 22:	/* 11  Mb/s */
    332 	case 44:	/* 22  Mb/s */
    333 		/* 5.5 - 11 Mb/s WLAN: send ACK/CTS at 2 Mb/s */
    334 		ctsrate = 4;
    335 		break;
    336 	default:
    337 		/* TBD */
    338 		return -1;
    339 	}
    340 
    341 	d->d_plcp_len = data_dur;
    342 
    343 	ack = (use_ack) ? pre + (IEEE80211_DUR_DS_SLOW_ACK * 2) / ctsrate : 0;
    344 
    345 	d->d_rts_dur =
    346 	    pre + (IEEE80211_DUR_DS_SLOW_CTS * 2) / ctsrate +
    347 	    pre + data_dur +
    348 	    ack;
    349 
    350 	d->d_data_dur = ack;
    351 
    352 	return 0;
    353 }
    354 
    355 /*
    356  * Arguments in:
    357  *
    358  * wh:      802.11 header
    359  *
    360  * paylen:  payload length (no FCS, no WEP header)
    361  *
    362  * rate:    MSDU speed, units 500kb/s
    363  *
    364  * fraglen: fragment length, set to maximum (or higher) for no
    365  *          fragmentation
    366  *
    367  * flags:   IEEE80211_F_PRIVACY (hardware adds WEP),
    368  *          IEEE80211_F_SHPREAMBLE (use short preamble),
    369  *          IEEE80211_F_SHSLOT (use short slot length)
    370  *
    371  * Arguments out:
    372  *
    373  * d0: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
    374  *     of first/only fragment
    375  *
    376  * dn: 802.11 Duration fields (RTS/Data), PLCP Length, Service fields
    377  *     of first/only fragment
    378  */
    379 int
    380 ieee80211_compute_duration(struct ieee80211_frame *wh, int len,
    381     uint32_t flags, int fraglen, int rate, struct ieee80211_duration *d0,
    382     struct ieee80211_duration *dn, int *npktp, int debug)
    383 {
    384 	int ack, rc;
    385 	int firstlen, hdrlen, lastlen, lastlen0, npkt, overlen, paylen;
    386 
    387 	if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
    388 		hdrlen = sizeof(struct ieee80211_frame_addr4);
    389 	else
    390 		hdrlen = sizeof(struct ieee80211_frame);
    391 
    392 	paylen = len - hdrlen;
    393 
    394 	if ((flags & IEEE80211_F_PRIVACY) != 0)
    395 		overlen = IEEE80211_WEP_TOTLEN + IEEE80211_CRC_LEN;
    396 	else
    397 		overlen = IEEE80211_CRC_LEN;
    398 
    399 	npkt = paylen / fraglen;
    400 	lastlen0 = paylen % fraglen;
    401 
    402 	if (npkt == 0)			/* no fragments */
    403 		lastlen = paylen + overlen;
    404 	else if (lastlen0 != 0) {	/* a short "tail" fragment */
    405 		lastlen = lastlen0 + overlen;
    406 		npkt++;
    407 	} else				/* full-length "tail" fragment */
    408 		lastlen = fraglen + overlen;
    409 
    410 	if (npktp != NULL)
    411 		*npktp = npkt;
    412 
    413 	if (npkt > 1)
    414 		firstlen = fraglen + overlen;
    415 	else
    416 		firstlen = paylen + overlen;
    417 
    418 	if (debug) {
    419 		printf("%s: npkt %d firstlen %d lastlen0 %d lastlen %d "
    420 		    "fraglen %d overlen %d len %d rate %d flags %08x\n",
    421 		    __func__, npkt, firstlen, lastlen0, lastlen, fraglen,
    422 		    overlen, len, rate, flags);
    423 	}
    424 
    425 	ack = !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    426 	    (wh->i_fc[1] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL;
    427 
    428 	rc = ieee80211_compute_duration1(firstlen + hdrlen,
    429 	    ack, flags, rate, d0);
    430 	if (rc == -1)
    431 		return rc;
    432 
    433 	if (npkt <= 1) {
    434 		*dn = *d0;
    435 		return 0;
    436 	}
    437 	return ieee80211_compute_duration1(lastlen + hdrlen, ack, flags, rate,
    438 	    dn);
    439 }
    440 
    441 /*
    442  * Add a supported rates element id to a frame.
    443  */
    444 u_int8_t *
    445 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    446 {
    447 	int nrates;
    448 
    449 	*frm++ = IEEE80211_ELEMID_RATES;
    450 	nrates = rs->rs_nrates;
    451 	if (nrates > IEEE80211_RATE_SIZE)
    452 		nrates = IEEE80211_RATE_SIZE;
    453 	*frm++ = nrates;
    454 	memcpy(frm, rs->rs_rates, nrates);
    455 	return frm + nrates;
    456 }
    457 
    458 /*
    459  * Add an extended supported rates element id to a frame.
    460  */
    461 u_int8_t *
    462 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    463 {
    464 	/*
    465 	 * Add an extended supported rates element if operating in 11g mode.
    466 	 */
    467 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    468 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    469 		*frm++ = IEEE80211_ELEMID_XRATES;
    470 		*frm++ = nrates;
    471 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    472 		frm += nrates;
    473 	}
    474 	return frm;
    475 }
    476 
    477 /*
    478  * Add an ssid elemet to a frame.
    479  */
    480 static u_int8_t *
    481 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    482 {
    483 	*frm++ = IEEE80211_ELEMID_SSID;
    484 	*frm++ = len;
    485 	memcpy(frm, ssid, len);
    486 	return frm + len;
    487 }
    488 
    489 static struct mbuf *
    490 ieee80211_getmbuf(int flags, int type, u_int pktlen)
    491 {
    492 	struct mbuf *m;
    493 
    494 	IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    495 #ifdef __FreeBSD__
    496 	if (pktlen <= MHLEN)
    497 		MGETHDR(m, flags, type);
    498 	else
    499 		m = m_getcl(flags, type, M_PKTHDR);
    500 #else
    501 	MGETHDR(m, flags, type);
    502 	if (m != NULL && pktlen > MHLEN)
    503 		MCLGET(m, flags);
    504 #endif
    505 	return m;
    506 }
    507 
    508 /*
    509  * Send a management frame.  The node is for the destination (or ic_bss
    510  * when in station mode).  Nodes other than ic_bss have their reference
    511  * count bumped to reflect our use for an indeterminant time.
    512  */
    513 int
    514 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    515 	int type, int arg)
    516 {
    517 #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
    518 	struct ifnet *ifp = &ic->ic_if;
    519 	struct mbuf *m;
    520 	u_int8_t *frm;
    521 	enum ieee80211_phymode mode;
    522 	u_int16_t capinfo;
    523 	int has_challenge, is_shared_key, ret, timer;
    524 
    525 	IASSERT(ni != NULL, ("null node"));
    526 
    527 	/*
    528 	 * Hold a reference on the node so it doesn't go away until after
    529 	 * the xmit is complete all the way in the driver.  On error we
    530 	 * will remove our reference.
    531 	 */
    532 	ieee80211_ref_node(ni);
    533 	timer = 0;
    534 	switch (type) {
    535 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    536 		/*
    537 		 * prreq frame format
    538 		 *	[tlv] ssid
    539 		 *	[tlv] supported rates
    540 		 *	[tlv] extended supported rates
    541 		 */
    542 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    543 			 2 + ic->ic_des_esslen
    544 		       + 2 + IEEE80211_RATE_SIZE
    545 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    546 		if (m == NULL)
    547 			senderr(ENOMEM, is_tx_nombuf);
    548 		m->m_data += sizeof(struct ieee80211_frame);
    549 		frm = mtod(m, u_int8_t *);
    550 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    551 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    552 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    553 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    554 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    555 
    556 		timer = IEEE80211_TRANS_WAIT;
    557 		break;
    558 
    559 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    560 		/*
    561 		 * probe response frame format
    562 		 *	[8] time stamp
    563 		 *	[2] beacon interval
    564 		 *	[2] cabability information
    565 		 *	[tlv] ssid
    566 		 *	[tlv] supported rates
    567 		 *	[tlv] parameter set (FH/DS)
    568 		 *	[tlv] parameter set (IBSS)
    569 		 *	[tlv] extended supported rates
    570 		 */
    571 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    572 			 8 + 2 + 2 + 2
    573 		       + 2 + ni->ni_esslen
    574 		       + 2 + IEEE80211_RATE_SIZE
    575 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    576 		       + 6
    577 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    578 		if (m == NULL)
    579 			senderr(ENOMEM, is_tx_nombuf);
    580 		m->m_data += sizeof(struct ieee80211_frame);
    581 		frm = mtod(m, u_int8_t *);
    582 
    583 		memset(frm, 0, 8);	/* timestamp should be filled later */
    584 		frm += 8;
    585 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    586 		frm += 2;
    587 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    588 			capinfo = IEEE80211_CAPINFO_IBSS;
    589 		else
    590 			capinfo = IEEE80211_CAPINFO_ESS;
    591 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    592 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    593 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    594 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    595 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    596 		*(u_int16_t *)frm = htole16(capinfo);
    597 		frm += 2;
    598 
    599 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    600 				ic->ic_bss->ni_esslen);
    601 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    602 
    603 		if (ic->ic_phytype == IEEE80211_T_FH) {
    604                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    605                         *frm++ = 5;
    606                         *frm++ = ni->ni_fhdwell & 0x00ff;
    607                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    608                         *frm++ = IEEE80211_FH_CHANSET(
    609 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    610                         *frm++ = IEEE80211_FH_CHANPAT(
    611 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    612                         *frm++ = ni->ni_fhindex;
    613 		} else {
    614 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    615 			*frm++ = 1;
    616 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    617 		}
    618 
    619 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    620 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    621 			*frm++ = 2;
    622 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    623 		} else {	/* IEEE80211_M_HOSTAP */
    624 			/* TODO: TIM */
    625 			*frm++ = IEEE80211_ELEMID_TIM;
    626 			*frm++ = 4;	/* length */
    627 			*frm++ = 0;	/* DTIM count */
    628 			*frm++ = 1;	/* DTIM period */
    629 			*frm++ = 0;	/* bitmap control */
    630 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    631 		}
    632 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    633 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    634 		break;
    635 
    636 	case IEEE80211_FC0_SUBTYPE_AUTH:
    637 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    638 		if (m == NULL)
    639 			senderr(ENOMEM, is_tx_nombuf);
    640 
    641 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
    642 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
    643 		    ni->ni_challenge != NULL);
    644 
    645 		is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
    646 		    arg == IEEE80211_AUTH_SHARED_PASS);
    647 
    648 		if (has_challenge) {
    649 			MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
    650 			m->m_pkthdr.len = m->m_len =
    651 			    2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
    652 		} else {
    653 			MH_ALIGN(m, 2 * 3);
    654 			m->m_pkthdr.len = m->m_len = 2 * 3;
    655 		}
    656 		frm = mtod(m, u_int8_t *);
    657 		((u_int16_t *)frm)[0] =
    658 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
    659 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
    660 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    661 		((u_int16_t *)frm)[2] = 0;		/* status */
    662 
    663 		if (has_challenge) {
    664 			((u_int16_t *)frm)[3] =
    665 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
    666 			    IEEE80211_ELEMID_CHALLENGE);
    667 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
    668 			    IEEE80211_CHALLENGE_LEN);
    669 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
    670 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    671 				    ("%s: request encrypt frame\n", __func__));
    672 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
    673 			}
    674 		}
    675 		if (ic->ic_opmode == IEEE80211_M_STA)
    676 			timer = IEEE80211_TRANS_WAIT;
    677 		break;
    678 
    679 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    680 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    681 			("send station %s deauthenticate (reason %d)\n",
    682 			ether_sprintf(ni->ni_macaddr), arg));
    683 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    684 		if (m == NULL)
    685 			senderr(ENOMEM, is_tx_nombuf);
    686 		MH_ALIGN(m, 2);
    687 		m->m_pkthdr.len = m->m_len = 2;
    688 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    689 		break;
    690 
    691 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    692 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    693 		/*
    694 		 * asreq frame format
    695 		 *	[2] capability information
    696 		 *	[2] listen interval
    697 		 *	[6*] current AP address (reassoc only)
    698 		 *	[tlv] ssid
    699 		 *	[tlv] supported rates
    700 		 *	[tlv] extended supported rates
    701 		 */
    702 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    703 			 sizeof(capinfo)
    704 		       + sizeof(u_int16_t)
    705 		       + IEEE80211_ADDR_LEN
    706 		       + 2 + ni->ni_esslen
    707 		       + 2 + IEEE80211_RATE_SIZE
    708 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    709 		if (m == NULL)
    710 			senderr(ENOMEM, is_tx_nombuf);
    711 		m->m_data += sizeof(struct ieee80211_frame);
    712 		frm = mtod(m, u_int8_t *);
    713 
    714 		capinfo = 0;
    715 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    716 			capinfo |= IEEE80211_CAPINFO_IBSS;
    717 		else		/* IEEE80211_M_STA */
    718 			capinfo |= IEEE80211_CAPINFO_ESS;
    719 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    720 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    721 		/*
    722 		 * NB: Some 11a AP's reject the request when
    723 		 *     short premable is set.
    724 		 */
    725 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    726 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    727 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    728 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    729 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    730 		*(u_int16_t *)frm = htole16(capinfo);
    731 		frm += 2;
    732 
    733 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    734 		frm += 2;
    735 
    736 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    737 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    738 			frm += IEEE80211_ADDR_LEN;
    739 		}
    740 
    741 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    742 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    743 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    744 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    745 
    746 		timer = IEEE80211_TRANS_WAIT;
    747 		break;
    748 
    749 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    750 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    751 		/*
    752 		 * asreq frame format
    753 		 *	[2] capability information
    754 		 *	[2] status
    755 		 *	[2] association ID
    756 		 *	[tlv] supported rates
    757 		 *	[tlv] extended supported rates
    758 		 */
    759 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    760 			 sizeof(capinfo)
    761 		       + sizeof(u_int16_t)
    762 		       + sizeof(u_int16_t)
    763 		       + 2 + IEEE80211_RATE_SIZE
    764 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    765 		if (m == NULL)
    766 			senderr(ENOMEM, is_tx_nombuf);
    767 		m->m_data += sizeof(struct ieee80211_frame);
    768 		frm = mtod(m, u_int8_t *);
    769 
    770 		capinfo = IEEE80211_CAPINFO_ESS;
    771 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
    772 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    773 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    774 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    775 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    776 		*(u_int16_t *)frm = htole16(capinfo);
    777 		frm += 2;
    778 
    779 		*(u_int16_t *)frm = htole16(arg);	/* status */
    780 		frm += 2;
    781 
    782 		if (arg == IEEE80211_STATUS_SUCCESS)
    783 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    784 		frm += 2;
    785 
    786 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    787 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    788 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    789 		break;
    790 
    791 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    792 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    793 			("send station %s disassociate (reason %d)\n",
    794 			ether_sprintf(ni->ni_macaddr), arg));
    795 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    796 		if (m == NULL)
    797 			senderr(ENOMEM, is_tx_nombuf);
    798 		MH_ALIGN(m, 2);
    799 		m->m_pkthdr.len = m->m_len = 2;
    800 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    801 		break;
    802 
    803 	default:
    804 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    805 			("%s: invalid mgmt frame type %u\n", __func__, type));
    806 		senderr(EINVAL, is_tx_unknownmgt);
    807 		/* NOTREACHED */
    808 	}
    809 
    810 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    811 	if (ret == 0) {
    812 		if (timer)
    813 			ic->ic_mgt_timer = timer;
    814 	} else {
    815 bad:
    816 		ieee80211_release_node(ic, ni);
    817 	}
    818 	return ret;
    819 #undef senderr
    820 }
    821 
    822 void
    823 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    824 		  struct mbuf *m)
    825 {
    826 	/* Store the new packet on our queue, changing the TIM if necessary */
    827 
    828 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    829 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    830 	}
    831 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    832 		IF_DROP(&ni->ni_savedq);
    833 		m_freem(m);
    834 		if (ic->ic_if.if_flags & IFF_DEBUG)
    835 			printf("%s: station %s power save queue overflow"
    836 			       " of size %d drops %d\n",
    837 			       ic->ic_if.if_xname,
    838 			       ether_sprintf(ni->ni_macaddr),
    839 			       IEEE80211_PS_MAX_QUEUE,
    840 			       ni->ni_savedq.ifq_drops);
    841 	} else {
    842 		/* Similar to ieee80211_mgmt_output, store the node in
    843 		 * the rcvif field.
    844 		 */
    845 		IF_ENQUEUE(&ni->ni_savedq, m);
    846 		m->m_pkthdr.rcvif = (void *)ni;
    847 	}
    848 }
    849 
    850