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