Home | History | Annotate | Line # | Download | only in net80211
ieee80211_output.c revision 1.13
      1 /*	$NetBSD: ieee80211_output.c,v 1.13 2004/05/31 11:02:55 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.13 2004/05/31 11:02:55 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 	IASSERT(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 			ic->ic_stats.is_tx_nombuf++;
    192 			goto bad;
    193 		}
    194 	}
    195 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
    196 
    197 	ni = ieee80211_find_txnode(ic, eh.ether_dhost);
    198 	if (ni == NULL) {
    199 		IEEE80211_DPRINTF(("%s: no node for dst %s, discard frame\n",
    200 			__func__, ether_sprintf(eh.ether_dhost)));
    201 		ic->ic_stats.is_tx_nonode++;
    202 		goto bad;
    203 	}
    204 	ni->ni_inact = 0;
    205 
    206 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
    207 	llc = mtod(m, struct llc *);
    208 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
    209 	llc->llc_control = LLC_UI;
    210 	llc->llc_snap.org_code[0] = 0;
    211 	llc->llc_snap.org_code[1] = 0;
    212 	llc->llc_snap.org_code[2] = 0;
    213 	llc->llc_snap.ether_type = eh.ether_type;
    214 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
    215 	if (m == NULL) {
    216 		ic->ic_stats.is_tx_nombuf++;
    217 		goto bad;
    218 	}
    219 	wh = mtod(m, struct ieee80211_frame *);
    220 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
    221 	*(u_int16_t *)&wh->i_dur[0] = 0;
    222 	*(u_int16_t *)&wh->i_seq[0] =
    223 	    htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
    224 	ni->ni_txseq++;
    225 	switch (ic->ic_opmode) {
    226 	case IEEE80211_M_STA:
    227 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
    228 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
    229 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    230 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
    231 		break;
    232 	case IEEE80211_M_IBSS:
    233 	case IEEE80211_M_AHDEMO:
    234 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
    235 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    236 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
    237 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
    238 		break;
    239 	case IEEE80211_M_HOSTAP:
    240 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
    241 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
    242 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
    243 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
    244 		break;
    245 	case IEEE80211_M_MONITOR:
    246 		goto bad;
    247 	}
    248 	if (ic->ic_flags & IEEE80211_F_WEPON)
    249 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
    250 	*pni = ni;
    251 	return m;
    252 bad:
    253 	if (m != NULL)
    254 		m_freem(m);
    255 	if (ni && ni != ic->ic_bss)
    256 		ieee80211_free_node(ic, ni);
    257 	*pni = NULL;
    258 	return NULL;
    259 }
    260 
    261 /*
    262  * Add a supported rates element id to a frame.
    263  */
    264 u_int8_t *
    265 ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    266 {
    267 	int nrates;
    268 
    269 	*frm++ = IEEE80211_ELEMID_RATES;
    270 	nrates = rs->rs_nrates;
    271 	if (nrates > IEEE80211_RATE_SIZE)
    272 		nrates = IEEE80211_RATE_SIZE;
    273 	*frm++ = nrates;
    274 	memcpy(frm, rs->rs_rates, nrates);
    275 	return frm + nrates;
    276 }
    277 
    278 /*
    279  * Add an extended supported rates element id to a frame.
    280  */
    281 u_int8_t *
    282 ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
    283 {
    284 	/*
    285 	 * Add an extended supported rates element if operating in 11g mode.
    286 	 */
    287 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
    288 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
    289 		*frm++ = IEEE80211_ELEMID_XRATES;
    290 		*frm++ = nrates;
    291 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
    292 		frm += nrates;
    293 	}
    294 	return frm;
    295 }
    296 
    297 /*
    298  * Add an ssid elemet to a frame.
    299  */
    300 static u_int8_t *
    301 ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
    302 {
    303 	*frm++ = IEEE80211_ELEMID_SSID;
    304 	*frm++ = len;
    305 	memcpy(frm, ssid, len);
    306 	return frm + len;
    307 }
    308 
    309 static struct mbuf *
    310 ieee80211_getmbuf(int flags, int type, u_int pktlen)
    311 {
    312 	struct mbuf *m;
    313 
    314 	IASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
    315 #ifdef __FreeBSD__
    316 	if (pktlen <= MHLEN)
    317 		MGETHDR(m, flags, type);
    318 	else
    319 		m = m_getcl(flags, type, M_PKTHDR);
    320 #else
    321 	MGETHDR(m, flags, type);
    322 	if (m != NULL && pktlen > MHLEN)
    323 		MCLGET(m, flags);
    324 #endif
    325 	return m;
    326 }
    327 
    328 /*
    329  * Send a management frame.  The node is for the destination (or ic_bss
    330  * when in station mode).  Nodes other than ic_bss have their reference
    331  * count bumped to reflect our use for an indeterminant time.
    332  */
    333 int
    334 ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
    335 	int type, int arg)
    336 {
    337 #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
    338 	struct ifnet *ifp = &ic->ic_if;
    339 	struct mbuf *m;
    340 	u_int8_t *frm;
    341 	enum ieee80211_phymode mode;
    342 	u_int16_t capinfo;
    343 	int has_challenge, is_shared_key, ret, timer;
    344 
    345 	IASSERT(ni != NULL, ("null node"));
    346 
    347 	/*
    348 	 * Hold a reference on the node so it doesn't go away until after
    349 	 * the xmit is complete all the way in the driver.  On error we
    350 	 * will remove our reference.
    351 	 */
    352 	if (ni != ic->ic_bss)
    353 		ieee80211_ref_node(ni);
    354 	timer = 0;
    355 	switch (type) {
    356 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    357 		/*
    358 		 * prreq frame format
    359 		 *	[tlv] ssid
    360 		 *	[tlv] supported rates
    361 		 *	[tlv] extended supported rates
    362 		 */
    363 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    364 			 2 + ic->ic_des_esslen
    365 		       + 2 + IEEE80211_RATE_SIZE
    366 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    367 		if (m == NULL)
    368 			senderr(ENOMEM, is_tx_nombuf);
    369 		m->m_data += sizeof(struct ieee80211_frame);
    370 		frm = mtod(m, u_int8_t *);
    371 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
    372 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
    373 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
    374 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
    375 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    376 
    377 		timer = IEEE80211_TRANS_WAIT;
    378 		break;
    379 
    380 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    381 		/*
    382 		 * probe response frame format
    383 		 *	[8] time stamp
    384 		 *	[2] beacon interval
    385 		 *	[2] cabability information
    386 		 *	[tlv] ssid
    387 		 *	[tlv] supported rates
    388 		 *	[tlv] parameter set (FH/DS)
    389 		 *	[tlv] parameter set (IBSS)
    390 		 *	[tlv] extended supported rates
    391 		 */
    392 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    393 			 8 + 2 + 2 + 2
    394 		       + 2 + ni->ni_esslen
    395 		       + 2 + IEEE80211_RATE_SIZE
    396 		       + (ic->ic_phytype == IEEE80211_T_FH ? 7 : 3)
    397 		       + 6
    398 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    399 		if (m == NULL)
    400 			senderr(ENOMEM, is_tx_nombuf);
    401 		m->m_data += sizeof(struct ieee80211_frame);
    402 		frm = mtod(m, u_int8_t *);
    403 
    404 		memset(frm, 0, 8);	/* timestamp should be filled later */
    405 		frm += 8;
    406 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
    407 		frm += 2;
    408 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    409 			capinfo = IEEE80211_CAPINFO_IBSS;
    410 		else
    411 			capinfo = IEEE80211_CAPINFO_ESS;
    412 		if (ic->ic_flags & IEEE80211_F_WEPON)
    413 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    414 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    415 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    416 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    417 		*(u_int16_t *)frm = htole16(capinfo);
    418 		frm += 2;
    419 
    420 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
    421 				ic->ic_bss->ni_esslen);
    422 		frm = ieee80211_add_rates(frm, &ic->ic_bss->ni_rates);
    423 
    424 		if (ic->ic_phytype == IEEE80211_T_FH) {
    425                         *frm++ = IEEE80211_ELEMID_FHPARMS;
    426                         *frm++ = 5;
    427                         *frm++ = ni->ni_fhdwell & 0x00ff;
    428                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
    429                         *frm++ = IEEE80211_FH_CHANSET(
    430 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    431                         *frm++ = IEEE80211_FH_CHANPAT(
    432 			    ieee80211_chan2ieee(ic, ni->ni_chan));
    433                         *frm++ = ni->ni_fhindex;
    434 		} else {
    435 			*frm++ = IEEE80211_ELEMID_DSPARMS;
    436 			*frm++ = 1;
    437 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
    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, is_tx_nombuf);
    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, is_tx_nombuf);
    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, is_tx_nombuf);
    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 		/*
    543 		 * NB: Some 11a AP's reject the request when
    544 		 *     short premable is set.
    545 		 */
    546 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    547 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    548 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    549 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    550 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
    551 		*(u_int16_t *)frm = htole16(capinfo);
    552 		frm += 2;
    553 
    554 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
    555 		frm += 2;
    556 
    557 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
    558 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
    559 			frm += IEEE80211_ADDR_LEN;
    560 		}
    561 
    562 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
    563 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    564 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    565 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    566 
    567 		timer = IEEE80211_TRANS_WAIT;
    568 		break;
    569 
    570 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    571 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
    572 		/*
    573 		 * asreq frame format
    574 		 *	[2] capability information
    575 		 *	[2] status
    576 		 *	[2] association ID
    577 		 *	[tlv] supported rates
    578 		 *	[tlv] extended supported rates
    579 		 */
    580 		m = ieee80211_getmbuf(M_DONTWAIT, MT_DATA,
    581 			 sizeof(capinfo)
    582 		       + sizeof(u_int16_t)
    583 		       + sizeof(u_int16_t)
    584 		       + 2 + IEEE80211_RATE_SIZE
    585 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE));
    586 		if (m == NULL)
    587 			senderr(ENOMEM, is_tx_nombuf);
    588 		m->m_data += sizeof(struct ieee80211_frame);
    589 		frm = mtod(m, u_int8_t *);
    590 
    591 		capinfo = IEEE80211_CAPINFO_ESS;
    592 		if (ic->ic_flags & IEEE80211_F_WEPON)
    593 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
    594 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
    595 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
    596 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
    597 		*(u_int16_t *)frm = htole16(capinfo);
    598 		frm += 2;
    599 
    600 		*(u_int16_t *)frm = htole16(arg);	/* status */
    601 		frm += 2;
    602 
    603 		if (arg == IEEE80211_STATUS_SUCCESS)
    604 			*(u_int16_t *)frm = htole16(ni->ni_associd);
    605 		frm += 2;
    606 
    607 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
    608 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
    609 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
    610 		break;
    611 
    612 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
    613 		if (ifp->if_flags & IFF_DEBUG)
    614 			if_printf(ifp, "station %s disassociate (reason %d)\n",
    615 			    ether_sprintf(ni->ni_macaddr), arg);
    616 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    617 		if (m == NULL)
    618 			senderr(ENOMEM, is_tx_nombuf);
    619 		MH_ALIGN(m, 2);
    620 		m->m_pkthdr.len = m->m_len = 2;
    621 		*mtod(m, u_int16_t *) = htole16(arg);	/* reason */
    622 		break;
    623 
    624 	default:
    625 		IEEE80211_DPRINTF(("%s: invalid mgmt frame type %u\n",
    626 			__func__, type));
    627 		senderr(EINVAL, is_tx_unknownmgt);
    628 		/* NOTREACHED */
    629 	}
    630 
    631 	ret = ieee80211_mgmt_output(ifp, ni, m, type);
    632 	if (ret == 0) {
    633 		if (timer)
    634 			ic->ic_mgt_timer = timer;
    635 	} else {
    636 bad:
    637 		if (ni != ic->ic_bss)		/* remove ref we added */
    638 			ieee80211_free_node(ic, ni);
    639 	}
    640 	return ret;
    641 #undef senderr
    642 }
    643 
    644 void
    645 ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
    646 		  struct mbuf *m)
    647 {
    648 	/* Store the new packet on our queue, changing the TIM if necessary */
    649 
    650 	if (IF_IS_EMPTY(&ni->ni_savedq)) {
    651 		ic->ic_set_tim(ic, ni->ni_associd, 1);
    652 	}
    653 	if (ni->ni_savedq.ifq_len >= IEEE80211_PS_MAX_QUEUE) {
    654 		IF_DROP(&ni->ni_savedq);
    655 		m_freem(m);
    656 		if (ic->ic_if.if_flags & IFF_DEBUG)
    657 			printf("%s: station %s power save queue overflow"
    658 			       " of size %d drops %d\n",
    659 			       ic->ic_if.if_xname,
    660 			       ether_sprintf(ni->ni_macaddr),
    661 			       IEEE80211_PS_MAX_QUEUE,
    662 			       ni->ni_savedq.ifq_drops);
    663 	} else {
    664 		/* Similar to ieee80211_mgmt_output, store the node in
    665 		 * the rcvif field.
    666 		 */
    667 		IF_ENQUEUE(&ni->ni_savedq, m);
    668 		m->m_pkthdr.rcvif = (void *)ni;
    669 	}
    670 }
    671 
    672