Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.15
      1 /*	$NetBSD: ieee80211_output.c,v 1.15 2004/07/23 08:25:25 mycroft 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.15 2004/07/23 08:25:25 mycroft 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_WEPON)
    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 && ni != ic->ic_bss)
    271 		ieee80211_free_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 	if (ni != ic->ic_bss)
    368 		ieee80211_ref_node(ni);
    369 	timer = 0;
    370 	switch (type) {
    371 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    372 		/*
    373 		 * prreq frame format
    374 		 *	[tlv] ssid
    375 		 *	[tlv] supported rates
    376 		 *	[tlv] extended supported rates
    377 		 */
    378 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    379 			 2 + ic->ic_des_esslen
    380 		       + 2 + IEEE80211_RATE_SIZE
    381 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    382 		if (m == NULL)
    383 			senderr(ENOMEM, is_tx_nombuf);
    384 		m->m_data += sizeof(struct ieee80211_frame);
    385 		frm = mtod(m, u_int8_t *);
    386 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    387 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    388 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    389 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    390 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    391 
    392 		timer = IEEE80211_TRANS_WAIT;
    393 		break;
    394 
    395 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    396 		/*
    397 		 * probe response frame format
    398 		 *	[8] time stamp
    399 		 *	[2] beacon interval
    400 		 *	[2] cabability information
    401 		 *	[tlv] ssid
    402 		 *	[tlv] supported rates
    403 		 *	[tlv] parameter set (FH/DS)
    404 		 *	[tlv] parameter set (IBSS)
    405 		 *	[tlv] extended supported rates
    406 		 */
    407 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    408 			 8 + 2 + 2 + 2
    409 		       + 2 + ni->ni_esslen
    410 		       + 2 + IEEE80211_RATE_SIZE
    411 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    412 		       + 6
    413 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    414 		if (m == NULL)
    415 			senderr(ENOMEM, is_tx_nombuf);
    416 		m->m_data += sizeof(struct ieee80211_frame);
    417 		frm = mtod(m, u_int8_t *);
    418 
    419 		memset(frm, 0, 8);	/* timestamp should be filled later */
    420 		frm += 8;
    421 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    422 		frm += 2;
    423 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    424 			capinfo = IEEE80211_CAPINFO_IBSS;
    425 		else
    426 			capinfo = IEEE80211_CAPINFO_ESS;
    427 		if (ic->ic_flags & IEEE80211_F_WEPON)
    428 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    429 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    430 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    431 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    432 		*(u_int16_t *)frm = htole16(capinfo);
    433 		frm += 2;
    434 
    435 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    436 				ic->ic_bss->ni_esslen);
    437 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    438 
    439 		if (ic->ic_phytype == IEEE80211_T_FH) {
    440                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    441                         *frm++ = 5;
    442                         *frm++ = ni->ni_fhdwell & 0x00ff;
    443                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    444                         *frm++ = IEEE80211_FH_CHANSET(
    445 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    446                         *frm++ = IEEE80211_FH_CHANPAT(
    447 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    448                         *frm++ = ni->ni_fhindex;
    449 		} else {
    450 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    451 			*frm++ = 1;
    452 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    453 		}
    454 
    455 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
    456 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
    457 			*frm++ = 2;
    458 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
    459 		} else {	/* IEEE80211_M_HOSTAP */
    460 			/* TODO: TIM */
    461 			*frm++ = IEEE80211_ELEMID_TIM;
    462 			*frm++ = 4;	/* length */
    463 			*frm++ = 0;	/* DTIM count */
    464 			*frm++ = 1;	/* DTIM period */
    465 			*frm++ = 0;	/* bitmap control */
    466 			*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
    467 		}
    468 		frm = ieee80211_add_xrates(frm, &ic->ic_bss->ni_rates);
    469 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    470 		break;
    471 
    472 	case IEEE80211_FC0_SUBTYPE_AUTH:
    473 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    474 		if (m == NULL)
    475 			senderr(ENOMEM, is_tx_nombuf);
    476 
    477 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
    478 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
    479 		    ni->ni_challenge != NULL);
    480 
    481 		is_shared_key = has_challenge || (ni->ni_challenge != NULL &&
    482 		    arg == IEEE80211_AUTH_SHARED_PASS);
    483 
    484 		if (has_challenge) {
    485 			MH_ALIGN(m, 2 * 3 + 2 + IEEE80211_CHALLENGE_LEN);
    486 			m->m_pkthdr.len = m->m_len =
    487 			    2 * 3 + 2 + IEEE80211_CHALLENGE_LEN;
    488 		} else {
    489 			MH_ALIGN(m, 2 * 3);
    490 			m->m_pkthdr.len = m->m_len = 2 * 3;
    491 		}
    492 		frm = mtod(m, u_int8_t *);
    493 		((u_int16_t *)frm)[0] =
    494 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
    495 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
    496 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
    497 		((u_int16_t *)frm)[2] = 0;		/* status */
    498 
    499 		if (has_challenge) {
    500 			((u_int16_t *)frm)[3] =
    501 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
    502 			    IEEE80211_ELEMID_CHALLENGE);
    503 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
    504 			    IEEE80211_CHALLENGE_LEN);
    505 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
    506 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    507 				    ("%s: request encrypt frame\n", __func__));
    508 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
    509 			}
    510 		}
    511 		if (ic->ic_opmode == IEEE80211_M_STA)
    512 			timer = IEEE80211_TRANS_WAIT;
    513 		break;
    514 
    515 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
    516 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
    517 			("send station %s deauthenticate (reason %d)\n",
    518 			ether_sprintf(ni->ni_macaddr), arg));
    519 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    520 		if (m == NULL)
    521 			senderr(ENOMEM, is_tx_nombuf);
    522 		MH_ALIGN(m, 2);
    523 		m->m_pkthdr.len = m->m_len = 2;
    524 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    525 		break;
    526 
    527 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    528 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
    529 		/*
    530 		 * asreq frame format
    531 		 *	[2] capability information
    532 		 *	[2] listen interval
    533 		 *	[6*] current AP address (reassoc only)
    534 		 *	[tlv] ssid
    535 		 *	[tlv] supported rates
    536 		 *	[tlv] extended supported rates
    537 		 */
    538 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    539 			 sizeof(capinfo)
    540 		       + sizeof(u_int16_t)
    541 		       + IEEE80211_ADDR_LEN
    542 		       + 2 + ni->ni_esslen
    543 		       + 2 + IEEE80211_RATE_SIZE
    544 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    545 		if (m == NULL)
    546 			senderr(ENOMEM, is_tx_nombuf);
    547 		m->m_data += sizeof(struct ieee80211_frame);
    548 		frm = mtod(m, u_int8_t *);
    549 
    550 		capinfo = 0;
    551 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    552 			capinfo |= IEEE80211_CAPINFO_IBSS;
    553 		else		/* IEEE80211_M_STA */
    554 			capinfo |= IEEE80211_CAPINFO_ESS;
    555 		if (ic->ic_flags & IEEE80211_F_WEPON)
    556 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    557 		/*
    558 		 * NB: Some 11a AP's reject the request when
    559 		 *     short premable is set.
    560 		 */
    561 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    562 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    563 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    564 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    565 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    566 		*(u_int16_t *)frm = htole16(capinfo);
    567 		frm += 2;
    568 
    569 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    570 		frm += 2;
    571 
    572 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    573 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    574 			frm += IEEE80211_ADDR_LEN;
    575 		}
    576 
    577 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    578 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    579 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    580 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    581 
    582 		timer = IEEE80211_TRANS_WAIT;
    583 		break;
    584 
    585 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    586 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    587 		/*
    588 		 * asreq frame format
    589 		 *	[2] capability information
    590 		 *	[2] status
    591 		 *	[2] association ID
    592 		 *	[tlv] supported rates
    593 		 *	[tlv] extended supported rates
    594 		 */
    595 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    596 			 sizeof(capinfo)
    597 		       + sizeof(u_int16_t)
    598 		       + sizeof(u_int16_t)
    599 		       + 2 + IEEE80211_RATE_SIZE
    600 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    601 		if (m == NULL)
    602 			senderr(ENOMEM, is_tx_nombuf);
    603 		m->m_data += sizeof(struct ieee80211_frame);
    604 		frm = mtod(m, u_int8_t *);
    605 
    606 		capinfo = IEEE80211_CAPINFO_ESS;
    607 		if (ic->ic_flags & IEEE80211_F_WEPON)
    608 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    609 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    610 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    611 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    612 		*(u_int16_t *)frm = htole16(capinfo);
    613 		frm += 2;
    614 
    615 		*(u_int16_t *)frm = htole16(arg);	/* status */
    616 		frm += 2;
    617 
    618 		if (arg == IEEE80211_STATUS_SUCCESS)
    619 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    620 		frm += 2;
    621 
    622 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    623 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    624 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    625 		break;
    626 
    627 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    628 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
    629 			("send station %s disassociate (reason %d)\n",
    630 			ether_sprintf(ni->ni_macaddr), arg));
    631 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    632 		if (m == NULL)
    633 			senderr(ENOMEM, is_tx_nombuf);
    634 		MH_ALIGN(m, 2);
    635 		m->m_pkthdr.len = m->m_len = 2;
    636 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    637 		break;
    638 
    639 	default:
    640 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
    641 			("%s: invalid mgmt frame type %u\n", __func__, type));
    642 		senderr(EINVAL, is_tx_unknownmgt);
    643 		/* NOTREACHED */
    644 	}
    645 
    646 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    647 	if (ret == 0) {
    648 		if (timer)
    649 			ic->ic_mgt_timer = timer;
    650 	} else {
    651 bad:
    652 		if (ni != ic->ic_bss)		/* remove ref we added */
    653 			ieee80211_free_node(ic, ni);
    654 	}
    655 	return ret;
    656 #undef senderr
    657 }
    658 
    659 void
    660 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    661 		  struct mbuf *m)
    662 {
    663 	/* Store the new packet on our queue, changing the TIM if necessary */
    664 
    665 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    666 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    667 	}
    668 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    669 		IF_DROP(&ni->ni_savedq);
    670 		m_freem(m);
    671 		if (ic->ic_if.if_flags & IFF_DEBUG)
    672 			printf("%s: station %s power save queue overflow"
    673 			       " of size %d drops %d\n",
    674 			       ic->ic_if.if_xname,
    675 			       ether_sprintf(ni->ni_macaddr),
    676 			       IEEE80211_PS_MAX_QUEUE,
    677 			       ni->ni_savedq.ifq_drops);
    678 	} else {
    679 		/* Similar to ieee80211_mgmt_output, store the node in
    680 		 * the rcvif field.
    681 		 */
    682 		IF_ENQUEUE(&ni->ni_savedq, m);
    683 		m->m_pkthdr.rcvif = (void *)ni;
    684 	}
    685 }
    686 
    687