xref: /freebsd/sys/net80211/ieee80211_output.c (revision 6d049ab343c232d88119bf8528edfb0aa5273e20)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler #include "opt_inet.h"
371a1e1d21SSam Leffler 
381a1e1d21SSam Leffler #include <sys/param.h>
391a1e1d21SSam Leffler #include <sys/systm.h>
401a1e1d21SSam Leffler #include <sys/mbuf.h>
411a1e1d21SSam Leffler #include <sys/kernel.h>
421a1e1d21SSam Leffler #include <sys/endian.h>
431a1e1d21SSam Leffler 
448a1b9b6aSSam Leffler #include <sys/socket.h>
451a1e1d21SSam Leffler 
461a1e1d21SSam Leffler #include <net/bpf.h>
478a1b9b6aSSam Leffler #include <net/ethernet.h>
488a1b9b6aSSam Leffler #include <net/if.h>
498a1b9b6aSSam Leffler #include <net/if_llc.h>
508a1b9b6aSSam Leffler #include <net/if_media.h>
518a1b9b6aSSam Leffler #include <net/if_vlan_var.h>
528a1b9b6aSSam Leffler 
538a1b9b6aSSam Leffler #include <net80211/ieee80211_var.h>
541a1e1d21SSam Leffler 
551a1e1d21SSam Leffler #ifdef INET
561a1e1d21SSam Leffler #include <netinet/in.h>
571a1e1d21SSam Leffler #include <netinet/if_ether.h>
588a1b9b6aSSam Leffler #include <netinet/in_systm.h>
598a1b9b6aSSam Leffler #include <netinet/ip.h>
608a1b9b6aSSam Leffler #endif
618a1b9b6aSSam Leffler 
628a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
638a1b9b6aSSam Leffler /*
648a1b9b6aSSam Leffler  * Decide if an outbound management frame should be
658a1b9b6aSSam Leffler  * printed when debugging is enabled.  This filters some
668a1b9b6aSSam Leffler  * of the less interesting frames that come frequently
678a1b9b6aSSam Leffler  * (e.g. beacons).
688a1b9b6aSSam Leffler  */
698a1b9b6aSSam Leffler static __inline int
708a1b9b6aSSam Leffler doprint(struct ieee80211com *ic, int subtype)
718a1b9b6aSSam Leffler {
728a1b9b6aSSam Leffler 	switch (subtype) {
738a1b9b6aSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
748a1b9b6aSSam Leffler 		return (ic->ic_opmode == IEEE80211_M_IBSS);
758a1b9b6aSSam Leffler 	}
768a1b9b6aSSam Leffler 	return 1;
778a1b9b6aSSam Leffler }
781a1e1d21SSam Leffler #endif
791a1e1d21SSam Leffler 
800a915fadSSam Leffler /*
81add59d08SSam Leffler  * Set the direction field and address fields of an outgoing
82add59d08SSam Leffler  * non-QoS frame.  Note this should be called early on in
83add59d08SSam Leffler  * constructing a frame as it sets i_fc[1]; other bits can
84add59d08SSam Leffler  * then be or'd in.
85add59d08SSam Leffler  */
86add59d08SSam Leffler static void
87add59d08SSam Leffler ieee80211_send_setup(struct ieee80211com *ic,
88add59d08SSam Leffler 	struct ieee80211_node *ni,
89add59d08SSam Leffler 	struct ieee80211_frame *wh,
90add59d08SSam Leffler 	int type,
91add59d08SSam Leffler 	const u_int8_t sa[IEEE80211_ADDR_LEN],
92add59d08SSam Leffler 	const u_int8_t da[IEEE80211_ADDR_LEN],
93add59d08SSam Leffler 	const u_int8_t bssid[IEEE80211_ADDR_LEN])
94add59d08SSam Leffler {
95add59d08SSam Leffler #define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
96add59d08SSam Leffler 
97add59d08SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
98add59d08SSam Leffler 	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
99add59d08SSam Leffler 		switch (ic->ic_opmode) {
100add59d08SSam Leffler 		case IEEE80211_M_STA:
101add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
102add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
103add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
104add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, da);
105add59d08SSam Leffler 			break;
106add59d08SSam Leffler 		case IEEE80211_M_IBSS:
107add59d08SSam Leffler 		case IEEE80211_M_AHDEMO:
108add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
109add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
110add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
111add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
112add59d08SSam Leffler 			break;
113add59d08SSam Leffler 		case IEEE80211_M_HOSTAP:
114add59d08SSam Leffler 			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
115add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr1, da);
116add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
117add59d08SSam Leffler 			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
118add59d08SSam Leffler 			break;
119add59d08SSam Leffler 		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
120add59d08SSam Leffler 			break;
121add59d08SSam Leffler 		}
122add59d08SSam Leffler 	} else {
123add59d08SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
124add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, da);
125add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
126add59d08SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
127add59d08SSam Leffler 	}
128add59d08SSam Leffler 	*(u_int16_t *)&wh->i_dur[0] = 0;
129add59d08SSam Leffler 	/* NB: use non-QoS tid */
130add59d08SSam Leffler 	*(u_int16_t *)&wh->i_seq[0] =
131add59d08SSam Leffler 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
132add59d08SSam Leffler 	ni->ni_txseqs[0]++;
133add59d08SSam Leffler #undef WH4
134add59d08SSam Leffler }
135add59d08SSam Leffler 
136add59d08SSam Leffler /*
1370a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
1380a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
1390a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
1400a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
1410a915fadSSam Leffler  * reference (and potentially free'ing up any associated storage).
1420a915fadSSam Leffler  */
1430a915fadSSam Leffler static int
1448a1b9b6aSSam Leffler ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
1456d049ab3SSam Leffler     struct mbuf *m, int type, int timer)
1461a1e1d21SSam Leffler {
1478a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1481a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
1491a1e1d21SSam Leffler 
1500a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
1511a1e1d21SSam Leffler 
1520a915fadSSam Leffler 	/*
1530a915fadSSam Leffler 	 * Yech, hack alert!  We want to pass the node down to the
1540a915fadSSam Leffler 	 * driver's start routine.  If we don't do so then the start
1550a915fadSSam Leffler 	 * routine must immediately look it up again and that can
1560a915fadSSam Leffler 	 * cause a lock order reversal if, for example, this frame
1570a915fadSSam Leffler 	 * is being sent because the station is being timedout and
1580a915fadSSam Leffler 	 * the frame being sent is a DEAUTH message.  We could stick
1590a915fadSSam Leffler 	 * this in an m_tag and tack that on to the mbuf.  However
1600a915fadSSam Leffler 	 * that's rather expensive to do for every frame so instead
1610a915fadSSam Leffler 	 * we stuff it in the rcvif field since outbound frames do
1620a915fadSSam Leffler 	 * not (presently) use this.
1630a915fadSSam Leffler 	 */
1641a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1651a1e1d21SSam Leffler 	if (m == NULL)
1661a1e1d21SSam Leffler 		return ENOMEM;
1670a915fadSSam Leffler 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1680a915fadSSam Leffler 	m->m_pkthdr.rcvif = (void *)ni;
1690a915fadSSam Leffler 
1701a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
171add59d08SSam Leffler 	ieee80211_send_setup(ic, ni, wh,
172add59d08SSam Leffler 		IEEE80211_FC0_TYPE_MGT | type,
173add59d08SSam Leffler 		ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
1748a1b9b6aSSam Leffler 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
1758a1b9b6aSSam Leffler 		m->m_flags &= ~M_LINK0;
1768a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1778a1b9b6aSSam Leffler 			"[%s] encrypting frame (%s)\n",
1788a1b9b6aSSam Leffler 			ether_sprintf(wh->i_addr1), __func__);
1798a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
1808a1b9b6aSSam Leffler 	}
1818a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
1828a1b9b6aSSam Leffler 	/* avoid printing too many frames */
1838a1b9b6aSSam Leffler 	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
1848a1b9b6aSSam Leffler 	    ieee80211_msg_dumppkts(ic)) {
1858a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
1868a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
1878a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
1888a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
1898a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
190b5c99415SSam Leffler 		    ieee80211_chan2ieee(ic, ic->ic_curchan));
1918a1b9b6aSSam Leffler 	}
1928a1b9b6aSSam Leffler #endif
1938a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1941a1e1d21SSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);
1956d049ab3SSam Leffler 	if (timer) {
1966d049ab3SSam Leffler 		/*
1976d049ab3SSam Leffler 		 * Set the mgt frame timeout.
1986d049ab3SSam Leffler 		 */
1996d049ab3SSam Leffler 		ic->ic_mgt_timer = timer;
2001a1e1d21SSam Leffler 		ifp->if_timer = 1;
2016d049ab3SSam Leffler 	}
202af5e59bfSRobert Watson 	if_start(ifp);
2031a1e1d21SSam Leffler 	return 0;
2041a1e1d21SSam Leffler }
2051a1e1d21SSam Leffler 
2060a915fadSSam Leffler /*
2078a1b9b6aSSam Leffler  * Send a null data frame to the specified node.
20819ad2dd7SSam Leffler  *
20919ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
21019ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
21119ad2dd7SSam Leffler  *     when probing for inactive stations.
2128a1b9b6aSSam Leffler  */
2138a1b9b6aSSam Leffler int
214f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
2158a1b9b6aSSam Leffler {
216f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2178a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
2188a1b9b6aSSam Leffler 	struct mbuf *m;
2198a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
2208a1b9b6aSSam Leffler 
22134333b16SAndre Oppermann 	MGETHDR(m, M_NOWAIT, MT_DATA);
2228a1b9b6aSSam Leffler 	if (m == NULL) {
2238a1b9b6aSSam Leffler 		/* XXX debug msg */
2248a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
22519ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
2268a1b9b6aSSam Leffler 		return ENOMEM;
2278a1b9b6aSSam Leffler 	}
22819ad2dd7SSam Leffler 	m->m_pkthdr.rcvif = (void *) ni;
2298a1b9b6aSSam Leffler 
2308a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
231add59d08SSam Leffler 	ieee80211_send_setup(ic, ni, wh,
232add59d08SSam Leffler 		IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
233add59d08SSam Leffler 		ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
234add59d08SSam Leffler 	/* NB: power management bit is never sent by an AP */
235add59d08SSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
236add59d08SSam Leffler 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
237add59d08SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
2388a1b9b6aSSam Leffler 	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
2398a1b9b6aSSam Leffler 
2408a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
2418a1b9b6aSSam Leffler 
242add59d08SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
243add59d08SSam Leffler 	    "[%s] send null data frame on channel %u, pwr mgt %s\n",
244add59d08SSam Leffler 	    ether_sprintf(ni->ni_macaddr),
245b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
246add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
247add59d08SSam Leffler 
2488a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
2498a1b9b6aSSam Leffler 	if_start(ifp);
2508a1b9b6aSSam Leffler 
2518a1b9b6aSSam Leffler 	return 0;
2528a1b9b6aSSam Leffler }
2538a1b9b6aSSam Leffler 
2548a1b9b6aSSam Leffler /*
2558a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
2568a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
2578a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
2588a1b9b6aSSam Leffler  * applied.
2598a1b9b6aSSam Leffler  */
2608a1b9b6aSSam Leffler int
2618a1b9b6aSSam Leffler ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
2628a1b9b6aSSam Leffler {
2638a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
2648a1b9b6aSSam Leffler #ifdef INET
2658a1b9b6aSSam Leffler 	struct ether_header *eh;
2668a1b9b6aSSam Leffler #endif
2678a1b9b6aSSam Leffler 
2688a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
2698a1b9b6aSSam Leffler 		ac = WME_AC_BE;
2708a1b9b6aSSam Leffler 		goto done;
2718a1b9b6aSSam Leffler 	}
2728a1b9b6aSSam Leffler 
2738a1b9b6aSSam Leffler 	/*
2748a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
2758a1b9b6aSSam Leffler 	 * to it must have a matching tag.
2768a1b9b6aSSam Leffler 	 */
2778a1b9b6aSSam Leffler 	v_wme_ac = 0;
2788a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
2798a1b9b6aSSam Leffler 		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
280bd40429eSSam Leffler 		 if (mtag == NULL) {
2818a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
2828a1b9b6aSSam Leffler 			return 1;
2838a1b9b6aSSam Leffler 		}
2848a1b9b6aSSam Leffler 		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
2858a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
2868a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
2878a1b9b6aSSam Leffler 			return 1;
2888a1b9b6aSSam Leffler 		}
2898a1b9b6aSSam Leffler 		/* map vlan priority to AC */
2908a1b9b6aSSam Leffler 		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
2918a1b9b6aSSam Leffler 		case 1:
2928a1b9b6aSSam Leffler 		case 2:
2938a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BK;
2948a1b9b6aSSam Leffler 			break;
2958a1b9b6aSSam Leffler 		case 0:
2968a1b9b6aSSam Leffler 		case 3:
2978a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BE;
2988a1b9b6aSSam Leffler 			break;
2998a1b9b6aSSam Leffler 		case 4:
3008a1b9b6aSSam Leffler 		case 5:
3018a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VI;
3028a1b9b6aSSam Leffler 			break;
3038a1b9b6aSSam Leffler 		case 6:
3048a1b9b6aSSam Leffler 		case 7:
3058a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VO;
3068a1b9b6aSSam Leffler 			break;
3078a1b9b6aSSam Leffler 		}
3088a1b9b6aSSam Leffler 	}
3098a1b9b6aSSam Leffler 
3108a1b9b6aSSam Leffler #ifdef INET
3118a1b9b6aSSam Leffler 	eh = mtod(m, struct ether_header *);
3128a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
3138a1b9b6aSSam Leffler 		const struct ip *ip = (struct ip *)
3148a1b9b6aSSam Leffler 			(mtod(m, u_int8_t *) + sizeof (*eh));
3158a1b9b6aSSam Leffler 		/*
3168a1b9b6aSSam Leffler 		 * IP frame, map the TOS field.
3178a1b9b6aSSam Leffler 		 */
3188a1b9b6aSSam Leffler 		switch (ip->ip_tos) {
3198a1b9b6aSSam Leffler 		case 0x08:
3208a1b9b6aSSam Leffler 		case 0x20:
3218a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BK;	/* background */
3228a1b9b6aSSam Leffler 			break;
3238a1b9b6aSSam Leffler 		case 0x28:
3248a1b9b6aSSam Leffler 		case 0xa0:
3258a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VI;	/* video */
3268a1b9b6aSSam Leffler 			break;
3278a1b9b6aSSam Leffler 		case 0x30:			/* voice */
3288a1b9b6aSSam Leffler 		case 0xe0:
3298a1b9b6aSSam Leffler 		case 0x88:			/* XXX UPSD */
3308a1b9b6aSSam Leffler 		case 0xb8:
3318a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VO;
3328a1b9b6aSSam Leffler 			break;
3338a1b9b6aSSam Leffler 		default:
3348a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BE;
3358a1b9b6aSSam Leffler 			break;
3368a1b9b6aSSam Leffler 		}
3378a1b9b6aSSam Leffler 	} else {
3388a1b9b6aSSam Leffler #endif /* INET */
3398a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
3408a1b9b6aSSam Leffler #ifdef INET
3418a1b9b6aSSam Leffler 	}
3428a1b9b6aSSam Leffler #endif
3438a1b9b6aSSam Leffler 	/*
3448a1b9b6aSSam Leffler 	 * Use highest priority AC.
3458a1b9b6aSSam Leffler 	 */
3468a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
3478a1b9b6aSSam Leffler 		ac = v_wme_ac;
3488a1b9b6aSSam Leffler 	else
3498a1b9b6aSSam Leffler 		ac = d_wme_ac;
3508a1b9b6aSSam Leffler 
3518a1b9b6aSSam Leffler 	/*
3528a1b9b6aSSam Leffler 	 * Apply ACM policy.
3538a1b9b6aSSam Leffler 	 */
3548a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
3558a1b9b6aSSam Leffler 		static const int acmap[4] = {
3568a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
3578a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
3588a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
3598a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
3608a1b9b6aSSam Leffler 		};
3618a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
3628a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
3638a1b9b6aSSam Leffler 			ac = acmap[ac];
3648a1b9b6aSSam Leffler 	}
3658a1b9b6aSSam Leffler done:
3668a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
3678a1b9b6aSSam Leffler 	return 0;
3688a1b9b6aSSam Leffler }
3698a1b9b6aSSam Leffler 
3708a1b9b6aSSam Leffler /*
3715e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
3725e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
3735e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
3745e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
3755e923d2eSSam Leffler  */
3765e923d2eSSam Leffler static struct mbuf *
3775e923d2eSSam Leffler ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
3785e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
3795e923d2eSSam Leffler {
3805e923d2eSSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
3815e923d2eSSam Leffler 	int needed_space = hdrsize;
3825e923d2eSSam Leffler 
3835e923d2eSSam Leffler 	if (key != NULL) {
3845e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
3855e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
3865e923d2eSSam Leffler 		/* XXX frags */
3875e923d2eSSam Leffler 	}
3885e923d2eSSam Leffler 	/*
3895e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
3905e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
3915e923d2eSSam Leffler 	 * there will be
3925e923d2eSSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
3935e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
3945e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
3955e923d2eSSam Leffler 	 */
3965e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
3975e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
3985e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
3995e923d2eSSam Leffler 		if (n == NULL) {
4005e923d2eSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
4015e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
4025e923d2eSSam Leffler 			ic->ic_stats.is_tx_nobuf++;
4035e923d2eSSam Leffler 			m_freem(m);
4045e923d2eSSam Leffler 			return NULL;
4055e923d2eSSam Leffler 		}
4065e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
4075e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
4085e923d2eSSam Leffler 		/*
4095e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
4105e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
4115e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
4125e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
4135e923d2eSSam Leffler 		 */
4145e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
4155e923d2eSSam Leffler 		m_move_pkthdr(n, m);
4165e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
4175e923d2eSSam Leffler 		n->m_data += needed_space;
4185e923d2eSSam Leffler 		/*
4195e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
4205e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
4215e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
4225e923d2eSSam Leffler 		 * for speed.
4235e923d2eSSam Leffler 		 */
4245e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
4255e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
4265e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
4275e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
4285e923d2eSSam Leffler 		/*
4295e923d2eSSam Leffler 		 * Replace the head of the chain.
4305e923d2eSSam Leffler 		 */
4315e923d2eSSam Leffler 		n->m_next = m;
4325e923d2eSSam Leffler 		m = n;
4335e923d2eSSam Leffler 	}
4345e923d2eSSam Leffler 	return m;
4355e923d2eSSam Leffler #undef TO_BE_RECLAIMED
4365e923d2eSSam Leffler }
4375e923d2eSSam Leffler 
4385e923d2eSSam Leffler #define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
4395e923d2eSSam Leffler /*
4405e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
4415e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
4425e923d2eSSam Leffler  * we fall back to the default transmit key.
4438a1b9b6aSSam Leffler  */
4448a1b9b6aSSam Leffler static __inline struct ieee80211_key *
4455e923d2eSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
4468a1b9b6aSSam Leffler {
4475e923d2eSSam Leffler 	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
4488a1b9b6aSSam Leffler 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4498a1b9b6aSSam Leffler 		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4508a1b9b6aSSam Leffler 			return NULL;
4518a1b9b6aSSam Leffler 		return &ic->ic_nw_keys[ic->ic_def_txkey];
4528a1b9b6aSSam Leffler 	} else {
4538a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
4548a1b9b6aSSam Leffler 	}
4555e923d2eSSam Leffler }
4565e923d2eSSam Leffler 
4575e923d2eSSam Leffler /*
4585e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
4595e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
4605e923d2eSSam Leffler  * the default tx key.
4615e923d2eSSam Leffler  */
4625e923d2eSSam Leffler static __inline struct ieee80211_key *
4635e923d2eSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
4645e923d2eSSam Leffler {
4655e923d2eSSam Leffler 	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4665e923d2eSSam Leffler 	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4675e923d2eSSam Leffler 		return NULL;
4685e923d2eSSam Leffler 	return &ic->ic_nw_keys[ic->ic_def_txkey];
4698a1b9b6aSSam Leffler }
4708a1b9b6aSSam Leffler 
4718a1b9b6aSSam Leffler /*
4728a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
4738a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
4748a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
4758a1b9b6aSSam Leffler  * first mbuf.
4760a915fadSSam Leffler  */
4771a1e1d21SSam Leffler struct mbuf *
4788a1b9b6aSSam Leffler ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
4798a1b9b6aSSam Leffler 	struct ieee80211_node *ni)
4801a1e1d21SSam Leffler {
4811a1e1d21SSam Leffler 	struct ether_header eh;
4821a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
4838a1b9b6aSSam Leffler 	struct ieee80211_key *key;
4841a1e1d21SSam Leffler 	struct llc *llc;
4855e923d2eSSam Leffler 	int hdrsize, datalen, addqos;
4861a1e1d21SSam Leffler 
4878a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
4881a1e1d21SSam Leffler 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
4891a1e1d21SSam Leffler 
4908a1b9b6aSSam Leffler 	/*
4918a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
4928a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
4938a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
4948a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
4958a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
4968a1b9b6aSSam Leffler 	 *
4978a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
4988a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
4998a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
5008a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
5018a1b9b6aSSam Leffler 	 */
5028a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
5035e923d2eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
5045e923d2eSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
5055e923d2eSSam Leffler 			key = ieee80211_crypto_getucastkey(ic, ni);
5065e923d2eSSam Leffler 		else
5075e923d2eSSam Leffler 			key = ieee80211_crypto_getmcastkey(ic, ni);
5088a1b9b6aSSam Leffler 		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
5098a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
5105e923d2eSSam Leffler 			    "[%s] no default transmit key (%s) deftxkey %u\n",
5115e923d2eSSam Leffler 			    ether_sprintf(eh.ether_dhost), __func__,
5125e923d2eSSam Leffler 			    ic->ic_def_txkey);
5135e923d2eSSam Leffler 			ic->ic_stats.is_tx_nodefkey++;
5148a1b9b6aSSam Leffler 		}
5158a1b9b6aSSam Leffler 	} else
5168a1b9b6aSSam Leffler 		key = NULL;
5178a1b9b6aSSam Leffler 	/* XXX 4-address format */
5185e923d2eSSam Leffler 	/*
5195e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
5205e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
5215e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
5225e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
5235e923d2eSSam Leffler 	 * configurable.
5245e923d2eSSam Leffler 	 */
5255e923d2eSSam Leffler 	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
5265e923d2eSSam Leffler 		 eh.ether_type != htons(ETHERTYPE_PAE);
5275e923d2eSSam Leffler 	if (addqos)
5288a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
5298a1b9b6aSSam Leffler 	else
5308a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
5318a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
5328a1b9b6aSSam Leffler 		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
5338a1b9b6aSSam Leffler 	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
5348a1b9b6aSSam Leffler 	if (m == NULL) {
5358a1b9b6aSSam Leffler 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
5360a915fadSSam Leffler 		goto bad;
5370a915fadSSam Leffler 	}
5381a1e1d21SSam Leffler 
5398a1b9b6aSSam Leffler 	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
5401a1e1d21SSam Leffler 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
5411a1e1d21SSam Leffler 	llc = mtod(m, struct llc *);
5421a1e1d21SSam Leffler 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
5431a1e1d21SSam Leffler 	llc->llc_control = LLC_UI;
5441a1e1d21SSam Leffler 	llc->llc_snap.org_code[0] = 0;
5451a1e1d21SSam Leffler 	llc->llc_snap.org_code[1] = 0;
5461a1e1d21SSam Leffler 	llc->llc_snap.org_code[2] = 0;
5471a1e1d21SSam Leffler 	llc->llc_snap.ether_type = eh.ether_type;
5488a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
5498a1b9b6aSSam Leffler 
5508a1b9b6aSSam Leffler 	M_PREPEND(m, hdrsize, M_DONTWAIT);
5511be50176SSam Leffler 	if (m == NULL) {
5528a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
5530a915fadSSam Leffler 		goto bad;
5541be50176SSam Leffler 	}
5551a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5561a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
5571a1e1d21SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
5581a1e1d21SSam Leffler 	switch (ic->ic_opmode) {
5591a1e1d21SSam Leffler 	case IEEE80211_M_STA:
5601a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5611a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
5621a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
5631a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
5641a1e1d21SSam Leffler 		break;
5651a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
5661a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
5671a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
5681a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5691a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
570a8b16e87SSam Leffler 		/*
571a8b16e87SSam Leffler 		 * NB: always use the bssid from ic_bss as the
572a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
573a8b16e87SSam Leffler 		 */
574a8b16e87SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
5751a1e1d21SSam Leffler 		break;
5761a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
5771a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
5781a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5791a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
5801a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
5811a1e1d21SSam Leffler 		break;
5822bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
5830a915fadSSam Leffler 		goto bad;
5841a1e1d21SSam Leffler 	}
585bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
586bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
5875e923d2eSSam Leffler 	if (addqos) {
5888a1b9b6aSSam Leffler 		struct ieee80211_qosframe *qwh =
5898a1b9b6aSSam Leffler 			(struct ieee80211_qosframe *) wh;
5908a1b9b6aSSam Leffler 		int ac, tid;
5918a1b9b6aSSam Leffler 
5928a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
5938a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
5948a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
5958a1b9b6aSSam Leffler 		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
5968a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
5978a1b9b6aSSam Leffler 			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
5988a1b9b6aSSam Leffler 		qwh->i_qos[1] = 0;
5998a1b9b6aSSam Leffler 		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
6008a1b9b6aSSam Leffler 
6018a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
6028a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
6038a1b9b6aSSam Leffler 		ni->ni_txseqs[tid]++;
6048a1b9b6aSSam Leffler 	} else {
6058a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
6068a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
6078a1b9b6aSSam Leffler 		ni->ni_txseqs[0]++;
6088a1b9b6aSSam Leffler 	}
6095e923d2eSSam Leffler 	if (key != NULL) {
6105e923d2eSSam Leffler 		/*
6115e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
6125e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
6135e923d2eSSam Leffler 		 */
6145e923d2eSSam Leffler 		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
6155e923d2eSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_WPA) &&
616b4b64678SSam Leffler 		     (ic->ic_opmode == IEEE80211_M_STA ?
617b4b64678SSam Leffler 		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
6185e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
6195e923d2eSSam Leffler 			/* XXX do fragmentation */
62096d88463SSam Leffler 			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
6215e923d2eSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
6225e923d2eSSam Leffler 				    "[%s] enmic failed, discard frame\n",
6235e923d2eSSam Leffler 				    ether_sprintf(eh.ether_dhost));
6245e923d2eSSam Leffler 				ic->ic_stats.is_crypto_enmicfail++;
6255e923d2eSSam Leffler 				goto bad;
6265e923d2eSSam Leffler 			}
6275e923d2eSSam Leffler 		}
6285e923d2eSSam Leffler 	}
6298a1b9b6aSSam Leffler 
6308a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
6318a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
6328a1b9b6aSSam Leffler 
6331a1e1d21SSam Leffler 	return m;
6340a915fadSSam Leffler bad:
6350a915fadSSam Leffler 	if (m != NULL)
6360a915fadSSam Leffler 		m_freem(m);
6370a915fadSSam Leffler 	return NULL;
6381a1e1d21SSam Leffler }
6391a1e1d21SSam Leffler 
6401a1e1d21SSam Leffler /*
6411a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
6421a1e1d21SSam Leffler  */
6438a1b9b6aSSam Leffler static u_int8_t *
6441a1e1d21SSam Leffler ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
6451a1e1d21SSam Leffler {
6461a1e1d21SSam Leffler 	int nrates;
6471a1e1d21SSam Leffler 
6481a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
6491a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
6501a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
6511a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
6521a1e1d21SSam Leffler 	*frm++ = nrates;
6531a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
6541a1e1d21SSam Leffler 	return frm + nrates;
6551a1e1d21SSam Leffler }
6561a1e1d21SSam Leffler 
6571a1e1d21SSam Leffler /*
6581a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
6591a1e1d21SSam Leffler  */
6608a1b9b6aSSam Leffler static u_int8_t *
6611a1e1d21SSam Leffler ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
6621a1e1d21SSam Leffler {
6631a1e1d21SSam Leffler 	/*
6641a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
6651a1e1d21SSam Leffler 	 */
6661a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
6671a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
6681a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
6691a1e1d21SSam Leffler 		*frm++ = nrates;
6701a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
6711a1e1d21SSam Leffler 		frm += nrates;
6721a1e1d21SSam Leffler 	}
6731a1e1d21SSam Leffler 	return frm;
6741a1e1d21SSam Leffler }
6751a1e1d21SSam Leffler 
6761a1e1d21SSam Leffler /*
6771a1e1d21SSam Leffler  * Add an ssid elemet to a frame.
6781a1e1d21SSam Leffler  */
6791a1e1d21SSam Leffler static u_int8_t *
6801a1e1d21SSam Leffler ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
6811a1e1d21SSam Leffler {
6821a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
6831a1e1d21SSam Leffler 	*frm++ = len;
6841a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
6851a1e1d21SSam Leffler 	return frm + len;
6861a1e1d21SSam Leffler }
6871a1e1d21SSam Leffler 
6888a1b9b6aSSam Leffler /*
6898a1b9b6aSSam Leffler  * Add an erp element to a frame.
6908a1b9b6aSSam Leffler  */
6918a1b9b6aSSam Leffler static u_int8_t *
6928a1b9b6aSSam Leffler ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
6931a1e1d21SSam Leffler {
6948a1b9b6aSSam Leffler 	u_int8_t erp;
6951a1e1d21SSam Leffler 
6968a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
6978a1b9b6aSSam Leffler 	*frm++ = 1;
6988a1b9b6aSSam Leffler 	erp = 0;
6998a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
7008a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
7018a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
7028a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
7038a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
7048a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
7058a1b9b6aSSam Leffler 	*frm++ = erp;
7068a1b9b6aSSam Leffler 	return frm;
7071a1e1d21SSam Leffler }
7081a1e1d21SSam Leffler 
7098a1b9b6aSSam Leffler static u_int8_t *
7108a1b9b6aSSam Leffler ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
7118a1b9b6aSSam Leffler {
7128a1b9b6aSSam Leffler #define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
7138a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
7148a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
7158a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
7168a1b9b6aSSam Leffler 	frm += 2;				\
7178a1b9b6aSSam Leffler } while (0)
7188a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
7198a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
7208a1b9b6aSSam Leffler 	frm += 4;				\
7218a1b9b6aSSam Leffler } while (0)
7228a1b9b6aSSam Leffler 	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
7238a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
7248a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
7258a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
7268a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
7278a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
7288a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
7298a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_NULL },
7308a1b9b6aSSam Leffler 	};
7318a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
7328a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
7338a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
7348a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
7358a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
7368a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
7378a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
7388a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
7398a1b9b6aSSam Leffler 	u_int8_t *selcnt;
7408a1b9b6aSSam Leffler 
7418a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_VENDOR;
7428a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
7438a1b9b6aSSam Leffler 	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
7448a1b9b6aSSam Leffler 	frm += sizeof(oui);
7458a1b9b6aSSam Leffler 	ADDSHORT(frm, WPA_VERSION);
7468a1b9b6aSSam Leffler 
7478a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
7488a1b9b6aSSam Leffler 
7498a1b9b6aSSam Leffler 	/* multicast cipher */
7508a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
7518a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
7528a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
7538a1b9b6aSSam Leffler 	else
7548a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
7558a1b9b6aSSam Leffler 
7568a1b9b6aSSam Leffler 	/* unicast cipher list */
7578a1b9b6aSSam Leffler 	selcnt = frm;
7588a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7598a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
7608a1b9b6aSSam Leffler 		selcnt[0]++;
7618a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
7628a1b9b6aSSam Leffler 	}
7638a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
7648a1b9b6aSSam Leffler 		selcnt[0]++;
7658a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
7668a1b9b6aSSam Leffler 	}
7678a1b9b6aSSam Leffler 
7688a1b9b6aSSam Leffler 	/* authenticator selector list */
7698a1b9b6aSSam Leffler 	selcnt = frm;
7708a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7718a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
7728a1b9b6aSSam Leffler 		selcnt[0]++;
7738a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
7748a1b9b6aSSam Leffler 	}
7758a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
7768a1b9b6aSSam Leffler 		selcnt[0]++;
7778a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
7788a1b9b6aSSam Leffler 	}
7798a1b9b6aSSam Leffler 
7808a1b9b6aSSam Leffler 	/* optional capabilities */
7816aa57182SSam Leffler 	if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
7828a1b9b6aSSam Leffler 		ADDSHORT(frm, rsn->rsn_caps);
7838a1b9b6aSSam Leffler 
7848a1b9b6aSSam Leffler 	/* calculate element length */
7858a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
7868a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
78716574882SSam Leffler 		("WPA IE too big, %u > %zu",
7888a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
7898a1b9b6aSSam Leffler 	return frm;
7908a1b9b6aSSam Leffler #undef ADDSHORT
7918a1b9b6aSSam Leffler #undef ADDSELECTOR
7928a1b9b6aSSam Leffler #undef WPA_OUI_BYTES
7938a1b9b6aSSam Leffler }
7948a1b9b6aSSam Leffler 
7958a1b9b6aSSam Leffler static u_int8_t *
7968a1b9b6aSSam Leffler ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
7978a1b9b6aSSam Leffler {
7988a1b9b6aSSam Leffler #define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
7998a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
8008a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
8018a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
8028a1b9b6aSSam Leffler 	frm += 2;				\
8038a1b9b6aSSam Leffler } while (0)
8048a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
8058a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
8068a1b9b6aSSam Leffler 	frm += 4;				\
8078a1b9b6aSSam Leffler } while (0)
8088a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
8098a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
8108a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
8118a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
8128a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
8138a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
8148a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_NULL },
8158a1b9b6aSSam Leffler 	};
8168a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
8178a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
8188a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
8198a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
8208a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
8218a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
8228a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
8238a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
8248a1b9b6aSSam Leffler 	u_int8_t *selcnt;
8258a1b9b6aSSam Leffler 
8268a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_RSN;
8278a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
8288a1b9b6aSSam Leffler 	ADDSHORT(frm, RSN_VERSION);
8298a1b9b6aSSam Leffler 
8308a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
8318a1b9b6aSSam Leffler 
8328a1b9b6aSSam Leffler 	/* multicast cipher */
8338a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
8348a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
8358a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
8368a1b9b6aSSam Leffler 	else
8378a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
8388a1b9b6aSSam Leffler 
8398a1b9b6aSSam Leffler 	/* unicast cipher list */
8408a1b9b6aSSam Leffler 	selcnt = frm;
8418a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
8428a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
8438a1b9b6aSSam Leffler 		selcnt[0]++;
8448a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
8458a1b9b6aSSam Leffler 	}
8468a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
8478a1b9b6aSSam Leffler 		selcnt[0]++;
8488a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
8498a1b9b6aSSam Leffler 	}
8508a1b9b6aSSam Leffler 
8518a1b9b6aSSam Leffler 	/* authenticator selector list */
8528a1b9b6aSSam Leffler 	selcnt = frm;
8538a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
8548a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
8558a1b9b6aSSam Leffler 		selcnt[0]++;
8568a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
8578a1b9b6aSSam Leffler 	}
8588a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
8598a1b9b6aSSam Leffler 		selcnt[0]++;
8608a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
8618a1b9b6aSSam Leffler 	}
8628a1b9b6aSSam Leffler 
8638a1b9b6aSSam Leffler 	/* optional capabilities */
8648a1b9b6aSSam Leffler 	ADDSHORT(frm, rsn->rsn_caps);
8658a1b9b6aSSam Leffler 	/* XXX PMKID */
8668a1b9b6aSSam Leffler 
8678a1b9b6aSSam Leffler 	/* calculate element length */
8688a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
8698a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
87016574882SSam Leffler 		("RSN IE too big, %u > %zu",
8718a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
8728a1b9b6aSSam Leffler 	return frm;
8738a1b9b6aSSam Leffler #undef ADDSELECTOR
8748a1b9b6aSSam Leffler #undef ADDSHORT
8758a1b9b6aSSam Leffler #undef RSN_OUI_BYTES
8768a1b9b6aSSam Leffler }
8778a1b9b6aSSam Leffler 
8788a1b9b6aSSam Leffler /*
8798a1b9b6aSSam Leffler  * Add a WPA/RSN element to a frame.
8808a1b9b6aSSam Leffler  */
8818a1b9b6aSSam Leffler static u_int8_t *
8828a1b9b6aSSam Leffler ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
8838a1b9b6aSSam Leffler {
8848a1b9b6aSSam Leffler 
8858a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
8868a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA2)
8878a1b9b6aSSam Leffler 		frm = ieee80211_setup_rsn_ie(ic, frm);
8888a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA1)
8898a1b9b6aSSam Leffler 		frm = ieee80211_setup_wpa_ie(ic, frm);
8908a1b9b6aSSam Leffler 	return frm;
8918a1b9b6aSSam Leffler }
8928a1b9b6aSSam Leffler 
8938a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
8948a1b9b6aSSam Leffler /*
8958a1b9b6aSSam Leffler  * Add a WME information element to a frame.
8968a1b9b6aSSam Leffler  */
8978a1b9b6aSSam Leffler static u_int8_t *
8988a1b9b6aSSam Leffler ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
8998a1b9b6aSSam Leffler {
9008a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
9018a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
9028a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
9038a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
9048a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
9058a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
9068a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
9078a1b9b6aSSam Leffler 		.wme_info	= 0,
9088a1b9b6aSSam Leffler 	};
9098a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
9108a1b9b6aSSam Leffler 	return frm + sizeof(info);
9118a1b9b6aSSam Leffler }
9128a1b9b6aSSam Leffler 
9138a1b9b6aSSam Leffler /*
9148a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
9158a1b9b6aSSam Leffler  */
9168a1b9b6aSSam Leffler static u_int8_t *
9178a1b9b6aSSam Leffler ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
9188a1b9b6aSSam Leffler {
9198a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
9208a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
9218a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
9228a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
9238a1b9b6aSSam Leffler 	frm += 2;				\
9248a1b9b6aSSam Leffler } while (0)
9258a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
9268a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
9278a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
9288a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
9298a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
9308a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
9318a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
9328a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
9338a1b9b6aSSam Leffler 	};
9348a1b9b6aSSam Leffler 	int i;
9358a1b9b6aSSam Leffler 
9368a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
9378a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
9388a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
9398a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
9408a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
9418a1b9b6aSSam Leffler 		const struct wmeParams *ac =
9428a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
9438a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
9448a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
9458a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
9468a1b9b6aSSam Leffler 		       ;
9478a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
9488a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
9498a1b9b6aSSam Leffler 		       ;
9508a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
9518a1b9b6aSSam Leffler 	}
9528a1b9b6aSSam Leffler 	return frm;
9538a1b9b6aSSam Leffler #undef SM
9548a1b9b6aSSam Leffler #undef ADDSHORT
9558a1b9b6aSSam Leffler }
9568a1b9b6aSSam Leffler #undef WME_OUI_BYTES
9578a1b9b6aSSam Leffler 
9580a915fadSSam Leffler /*
959af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
960af8418dcSSam Leffler  * and any optional information element data.
961af8418dcSSam Leffler  */
962af8418dcSSam Leffler int
963af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
964af8418dcSSam Leffler 	const u_int8_t sa[IEEE80211_ADDR_LEN],
965af8418dcSSam Leffler 	const u_int8_t da[IEEE80211_ADDR_LEN],
966af8418dcSSam Leffler 	const u_int8_t bssid[IEEE80211_ADDR_LEN],
967af8418dcSSam Leffler 	const u_int8_t *ssid, size_t ssidlen,
968af8418dcSSam Leffler 	const void *optie, size_t optielen)
969af8418dcSSam Leffler {
970af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
971af8418dcSSam Leffler 	enum ieee80211_phymode mode;
972af8418dcSSam Leffler 	struct ieee80211_frame *wh;
973af8418dcSSam Leffler 	struct mbuf *m;
974af8418dcSSam Leffler 	u_int8_t *frm;
975af8418dcSSam Leffler 
976af8418dcSSam Leffler 	/*
977af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
978af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
979af8418dcSSam Leffler 	 * will remove our reference.
980af8418dcSSam Leffler 	 */
981af8418dcSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
982af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
983af8418dcSSam Leffler 		__func__, __LINE__,
984af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
985af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
986af8418dcSSam Leffler 	ieee80211_ref_node(ni);
987af8418dcSSam Leffler 
988af8418dcSSam Leffler 	/*
989af8418dcSSam Leffler 	 * prreq frame format
990af8418dcSSam Leffler 	 *	[tlv] ssid
991af8418dcSSam Leffler 	 *	[tlv] supported rates
992af8418dcSSam Leffler 	 *	[tlv] extended supported rates
993af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
994af8418dcSSam Leffler 	 */
995af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
996af8418dcSSam Leffler 		 2 + IEEE80211_NWID_LEN
997af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
998af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
999af8418dcSSam Leffler 	       + (optie != NULL ? optielen : 0)
1000af8418dcSSam Leffler 	);
1001af8418dcSSam Leffler 	if (m == NULL) {
1002af8418dcSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
1003af8418dcSSam Leffler 		ieee80211_free_node(ni);
1004af8418dcSSam Leffler 		return ENOMEM;
1005af8418dcSSam Leffler 	}
1006af8418dcSSam Leffler 
1007af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1008b5c99415SSam Leffler 	mode = ieee80211_chan2mode(ic, ic->ic_curchan);
1009af8418dcSSam Leffler 	frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1010af8418dcSSam Leffler 	frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1011af8418dcSSam Leffler 
1012af8418dcSSam Leffler 	if (optie != NULL) {
1013af8418dcSSam Leffler 		memcpy(frm, optie, optielen);
1014af8418dcSSam Leffler 		frm += optielen;
1015af8418dcSSam Leffler 	}
1016af8418dcSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1017af8418dcSSam Leffler 
1018af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1019af8418dcSSam Leffler 	if (m == NULL)
1020af8418dcSSam Leffler 		return ENOMEM;
1021af8418dcSSam Leffler 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1022af8418dcSSam Leffler 	m->m_pkthdr.rcvif = (void *)ni;
1023af8418dcSSam Leffler 
1024af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1025af8418dcSSam Leffler 	ieee80211_send_setup(ic, ni, wh,
1026af8418dcSSam Leffler 		IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1027af8418dcSSam Leffler 		sa, da, bssid);
1028af8418dcSSam Leffler 	/* XXX power management? */
1029af8418dcSSam Leffler 
1030af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1031af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1032af8418dcSSam Leffler 
1033af8418dcSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1034af8418dcSSam Leffler 	    "[%s] send probe req on channel %u\n",
1035af8418dcSSam Leffler 	    ether_sprintf(wh->i_addr1),
1036b5c99415SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_curchan));
1037af8418dcSSam Leffler 
1038af8418dcSSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);
1039af8418dcSSam Leffler 	if_start(ic->ic_ifp);
1040af8418dcSSam Leffler 	return 0;
1041af8418dcSSam Leffler }
1042af8418dcSSam Leffler 
1043af8418dcSSam Leffler /*
10440a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
10450a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
10460a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
10470a915fadSSam Leffler  */
10481a1e1d21SSam Leffler int
10491a1e1d21SSam Leffler ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
10501a1e1d21SSam Leffler 	int type, int arg)
10511a1e1d21SSam Leffler {
10521be50176SSam Leffler #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
10531a1e1d21SSam Leffler 	struct mbuf *m;
10541a1e1d21SSam Leffler 	u_int8_t *frm;
10551a1e1d21SSam Leffler 	u_int16_t capinfo;
10568a1b9b6aSSam Leffler 	int has_challenge, is_shared_key, ret, timer, status;
10571a1e1d21SSam Leffler 
10580a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
10590a915fadSSam Leffler 
10600a915fadSSam Leffler 	/*
10610a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
10620a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
10630a915fadSSam Leffler 	 * will remove our reference.
10640a915fadSSam Leffler 	 */
10658a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
106649a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
10678a1b9b6aSSam Leffler 		__func__, __LINE__,
106849a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
10698a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
10700a915fadSSam Leffler 	ieee80211_ref_node(ni);
10718a1b9b6aSSam Leffler 
10721a1e1d21SSam Leffler 	timer = 0;
10731a1e1d21SSam Leffler 	switch (type) {
10741a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
10751a1e1d21SSam Leffler 		/*
10761a1e1d21SSam Leffler 		 * probe response frame format
10771a1e1d21SSam Leffler 		 *	[8] time stamp
10781a1e1d21SSam Leffler 		 *	[2] beacon interval
10791a1e1d21SSam Leffler 		 *	[2] cabability information
10801a1e1d21SSam Leffler 		 *	[tlv] ssid
10811a1e1d21SSam Leffler 		 *	[tlv] supported rates
1082b83d2112SSam Leffler 		 *	[tlv] parameter set (FH/DS)
10831a1e1d21SSam Leffler 		 *	[tlv] parameter set (IBSS)
10848a1b9b6aSSam Leffler 		 *	[tlv] extended rate phy (ERP)
10851a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
10868a1b9b6aSSam Leffler 		 *	[tlv] WPA
1087d6ec172cSSam Leffler 		 *	[tlv] WME (optional)
10881a1e1d21SSam Leffler 		 */
10898a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
10908a1b9b6aSSam Leffler 			 8
10918a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
10928a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
10938a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
10941a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
10958a1b9b6aSSam Leffler 		       + 7	/* max(7,3) */
10961a1e1d21SSam Leffler 		       + 6
10978a1b9b6aSSam Leffler 		       + 3
10988a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
10998a1b9b6aSSam Leffler 		       /* XXX !WPA1+WPA2 fits w/o a cluster */
11008a1b9b6aSSam Leffler 		       + (ic->ic_flags & IEEE80211_F_WPA ?
11018a1b9b6aSSam Leffler 				2*sizeof(struct ieee80211_ie_wpa) : 0)
1102d6ec172cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
11038a1b9b6aSSam Leffler 		);
11041a1e1d21SSam Leffler 		if (m == NULL)
11058a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11061a1e1d21SSam Leffler 
11071a1e1d21SSam Leffler 		memset(frm, 0, 8);	/* timestamp should be filled later */
11081a1e1d21SSam Leffler 		frm += 8;
11091a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
11101a1e1d21SSam Leffler 		frm += 2;
11111a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
11121a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_IBSS;
11131a1e1d21SSam Leffler 		else
11141a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_ESS;
11158a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
11161a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
11174f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1118b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
11194f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
11208a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
11218a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
11221a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
11231a1e1d21SSam Leffler 		frm += 2;
11241a1e1d21SSam Leffler 
11251a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
11261a1e1d21SSam Leffler 				ic->ic_bss->ni_esslen);
11278a1b9b6aSSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
11281a1e1d21SSam Leffler 
1129b83d2112SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_FH) {
1130b83d2112SSam Leffler                         *frm++ = IEEE80211_ELEMID_FHPARMS;
1131b83d2112SSam Leffler                         *frm++ = 5;
1132b83d2112SSam Leffler                         *frm++ = ni->ni_fhdwell & 0x00ff;
1133b83d2112SSam Leffler                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1134b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANSET(
1135b5c99415SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_curchan));
1136b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANPAT(
1137b5c99415SSam Leffler 			    ieee80211_chan2ieee(ic, ic->ic_curchan));
1138b83d2112SSam Leffler                         *frm++ = ni->ni_fhindex;
1139b83d2112SSam Leffler 		} else {
1140b83d2112SSam Leffler 			*frm++ = IEEE80211_ELEMID_DSPARMS;
1141b83d2112SSam Leffler 			*frm++ = 1;
1142b5c99415SSam Leffler 			*frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan);
1143b83d2112SSam Leffler 		}
1144b83d2112SSam Leffler 
11451a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
11461a1e1d21SSam Leffler 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
11471a1e1d21SSam Leffler 			*frm++ = 2;
11481a1e1d21SSam Leffler 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
11491a1e1d21SSam Leffler 		}
11508a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
11518a1b9b6aSSam Leffler 			frm = ieee80211_add_wpa(frm, ic);
11528a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
11538a1b9b6aSSam Leffler 			frm = ieee80211_add_erp(frm, ic);
11548a1b9b6aSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1155d6ec172cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME)
1156d6ec172cSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
11571a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
11581a1e1d21SSam Leffler 		break;
11591a1e1d21SSam Leffler 
11601a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
11618a1b9b6aSSam Leffler 		status = arg >> 16;
11628a1b9b6aSSam Leffler 		arg &= 0xffff;
11638a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
11648a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
11658a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
11668a1b9b6aSSam Leffler 
11678a1b9b6aSSam Leffler 		/*
11688a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
11698a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
11708a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
11718a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
11728a1b9b6aSSam Leffler 		 * request and configured to use shared key.
11738a1b9b6aSSam Leffler 		 */
11748a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
11758a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
11768a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
11778a1b9b6aSSam Leffler 		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
11788a1b9b6aSSam Leffler 
11798a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
11808a1b9b6aSSam Leffler 			  3 * sizeof(u_int16_t)
11818a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
11828a1b9b6aSSam Leffler 				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
11838a1b9b6aSSam Leffler 		);
11841a1e1d21SSam Leffler 		if (m == NULL)
11858a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11868a1b9b6aSSam Leffler 
11878a1b9b6aSSam Leffler 		((u_int16_t *)frm)[0] =
11888a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
11898a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
11901a1e1d21SSam Leffler 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
11918a1b9b6aSSam Leffler 		((u_int16_t *)frm)[2] = htole16(status);/* status */
11928a1b9b6aSSam Leffler 
11938a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
11948a1b9b6aSSam Leffler 			((u_int16_t *)frm)[3] =
11958a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
11968a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
11978a1b9b6aSSam Leffler 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
11988a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
11998a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
12008a1b9b6aSSam Leffler 				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
12018a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
12028a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
12038a1b9b6aSSam Leffler 				    "[%s] request encrypt frame (%s)\n",
12048a1b9b6aSSam Leffler 				    ether_sprintf(ni->ni_macaddr), __func__);
12058a1b9b6aSSam Leffler 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
12068a1b9b6aSSam Leffler 			}
12078a1b9b6aSSam Leffler 		} else
12088a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
12098a1b9b6aSSam Leffler 
12108a1b9b6aSSam Leffler 		/* XXX not right for shared key */
12118a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
12128a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
12138a1b9b6aSSam Leffler 		else
12148a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
12158a1b9b6aSSam Leffler 
12161a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
12171a1e1d21SSam Leffler 			timer = IEEE80211_TRANS_WAIT;
12181a1e1d21SSam Leffler 		break;
12191a1e1d21SSam Leffler 
12201a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
12218a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
12228a1b9b6aSSam Leffler 			"[%s] send station deauthenticate (reason %d)\n",
12231a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
12248a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
12251a1e1d21SSam Leffler 		if (m == NULL)
12268a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12278a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
12288a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
12298a1b9b6aSSam Leffler 
12308a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
12318a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
12328a1b9b6aSSam Leffler 
1233e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
12341a1e1d21SSam Leffler 		break;
12351a1e1d21SSam Leffler 
12361a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
12371a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
12381a1e1d21SSam Leffler 		/*
12391a1e1d21SSam Leffler 		 * asreq frame format
12401a1e1d21SSam Leffler 		 *	[2] capability information
12411a1e1d21SSam Leffler 		 *	[2] listen interval
12421a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
12431a1e1d21SSam Leffler 		 *	[tlv] ssid
12441a1e1d21SSam Leffler 		 *	[tlv] supported rates
12451a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
12468a1b9b6aSSam Leffler 		 *	[tlv] WME
12478a1b9b6aSSam Leffler 		 *	[tlv] user-specified ie's
12481a1e1d21SSam Leffler 		 */
12498a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
12508a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
12511a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
12521a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
12538a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
12541a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
12558a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
12568a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
12578a1b9b6aSSam Leffler 		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
12588a1b9b6aSSam Leffler 		);
12591a1e1d21SSam Leffler 		if (m == NULL)
12608a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12611a1e1d21SSam Leffler 
12621a1e1d21SSam Leffler 		capinfo = 0;
12631a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
12641a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_IBSS;
12651a1e1d21SSam Leffler 		else		/* IEEE80211_M_STA */
12661a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_ESS;
12678a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
12681a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
12694f2e09c4SSam Leffler 		/*
12704f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
12714f2e09c4SSam Leffler 		 *     short premable is set.
12724f2e09c4SSam Leffler 		 */
12734f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1274b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
12751a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
12768a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
12778a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
12781a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
12791a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
12801a1e1d21SSam Leffler 		frm += 2;
12811a1e1d21SSam Leffler 
12821a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
12831a1e1d21SSam Leffler 		frm += 2;
12841a1e1d21SSam Leffler 
12851a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
12861a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
12871a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
12881a1e1d21SSam Leffler 		}
12891a1e1d21SSam Leffler 
12901a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
12911a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
12921a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
12938a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
12948a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
12958a1b9b6aSSam Leffler 		if (ic->ic_opt_ie != NULL) {
12968a1b9b6aSSam Leffler 			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
12978a1b9b6aSSam Leffler 			frm += ic->ic_opt_ie_len;
12988a1b9b6aSSam Leffler 		}
12991a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
13001a1e1d21SSam Leffler 
13011a1e1d21SSam Leffler 		timer = IEEE80211_TRANS_WAIT;
13021a1e1d21SSam Leffler 		break;
13031a1e1d21SSam Leffler 
13041a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
13051a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
13061a1e1d21SSam Leffler 		/*
13071a1e1d21SSam Leffler 		 * asreq frame format
13081a1e1d21SSam Leffler 		 *	[2] capability information
13091a1e1d21SSam Leffler 		 *	[2] status
13101a1e1d21SSam Leffler 		 *	[2] association ID
13111a1e1d21SSam Leffler 		 *	[tlv] supported rates
13121a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
13138a1b9b6aSSam Leffler 		 *	[tlv] WME (if enabled and STA enabled)
13141a1e1d21SSam Leffler 		 */
13158a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
13168a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
13171a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
13181a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
13191a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
13208a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
13218a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
13228a1b9b6aSSam Leffler 		);
13231a1e1d21SSam Leffler 		if (m == NULL)
13248a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
13251a1e1d21SSam Leffler 
13261a1e1d21SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
13278a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
13281a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
13294f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1330b5c99415SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
13314f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
13328a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
13338a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
13341a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
13351a1e1d21SSam Leffler 		frm += 2;
13361a1e1d21SSam Leffler 
13371a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* status */
13381a1e1d21SSam Leffler 		frm += 2;
13391a1e1d21SSam Leffler 
13408a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
13411a1e1d21SSam Leffler 			*(u_int16_t *)frm = htole16(ni->ni_associd);
13428a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
13438a1b9b6aSSam Leffler 		} else
13448a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
13451a1e1d21SSam Leffler 		frm += 2;
13461a1e1d21SSam Leffler 
13471a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
13481a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
13498a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
13508a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
13511a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
13521a1e1d21SSam Leffler 		break;
13531a1e1d21SSam Leffler 
13541a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
13558a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
13568a1b9b6aSSam Leffler 			"[%s] send station disassociate (reason %d)\n",
13571a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
13588a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
13591a1e1d21SSam Leffler 		if (m == NULL)
13608a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
13618a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
13628a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
13638a1b9b6aSSam Leffler 
13648a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
13658a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
13661a1e1d21SSam Leffler 		break;
13671a1e1d21SSam Leffler 
13681a1e1d21SSam Leffler 	default:
13698a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
13708a1b9b6aSSam Leffler 			"[%s] invalid mgmt frame type %u\n",
13718a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr), type);
13721be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
13730a915fadSSam Leffler 		/* NOTREACHED */
13741a1e1d21SSam Leffler 	}
13756d049ab3SSam Leffler 	ret = ieee80211_mgmt_output(ic, ni, m, type, timer);
13766d049ab3SSam Leffler 	if (ret != 0) {
13770a915fadSSam Leffler bad:
13788a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
13790a915fadSSam Leffler 	}
13801a1e1d21SSam Leffler 	return ret;
13810a915fadSSam Leffler #undef senderr
13821a1e1d21SSam Leffler }
13838a1b9b6aSSam Leffler 
13848a1b9b6aSSam Leffler /*
13858a1b9b6aSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
13868a1b9b6aSSam Leffler  */
13878a1b9b6aSSam Leffler struct mbuf *
13888a1b9b6aSSam Leffler ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
13898a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo)
13908a1b9b6aSSam Leffler {
13918a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
13928a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
13938a1b9b6aSSam Leffler 	struct mbuf *m;
13948a1b9b6aSSam Leffler 	int pktlen;
13958a1b9b6aSSam Leffler 	u_int8_t *frm, *efrm;
13968a1b9b6aSSam Leffler 	u_int16_t capinfo;
13978a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
13988a1b9b6aSSam Leffler 
13998a1b9b6aSSam Leffler 	/*
14008a1b9b6aSSam Leffler 	 * beacon frame format
14018a1b9b6aSSam Leffler 	 *	[8] time stamp
14028a1b9b6aSSam Leffler 	 *	[2] beacon interval
14038a1b9b6aSSam Leffler 	 *	[2] cabability information
14048a1b9b6aSSam Leffler 	 *	[tlv] ssid
14058a1b9b6aSSam Leffler 	 *	[tlv] supported rates
14068a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
14078a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
14088a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
14098a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
14108a1b9b6aSSam Leffler 	 *	[tlv] WME parameters
14118a1b9b6aSSam Leffler 	 *	[tlv] WPA/RSN parameters
14128a1b9b6aSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
14138a1b9b6aSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
14148a1b9b6aSSam Leffler 	 */
14158a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
14168a1b9b6aSSam Leffler 	pktlen =   8					/* time stamp */
14178a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* beacon interval */
14188a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* capabilities */
14198a1b9b6aSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
14208a1b9b6aSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
14218a1b9b6aSSam Leffler 	         + 2 + 1				/* DS parameters */
14228a1b9b6aSSam Leffler 		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
14238a1b9b6aSSam Leffler 		 + 2 + 1				/* ERP */
14248a1b9b6aSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
14258a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
14268a1b9b6aSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
14278a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
14288a1b9b6aSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
14298a1b9b6aSSam Leffler 		 ;
14308a1b9b6aSSam Leffler 	m = ieee80211_getmgtframe(&frm, pktlen);
14318a1b9b6aSSam Leffler 	if (m == NULL) {
14328a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
14338a1b9b6aSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
14348a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
14358a1b9b6aSSam Leffler 		return NULL;
14368a1b9b6aSSam Leffler 	}
14378a1b9b6aSSam Leffler 
14388a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
14398a1b9b6aSSam Leffler 	frm += 8;
14408a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(ni->ni_intval);
14418a1b9b6aSSam Leffler 	frm += 2;
14428a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
14438a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
14448a1b9b6aSSam Leffler 	else
14458a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
14468a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
14478a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
14488a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
14498a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
14508a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
14518a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
14528a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
14538a1b9b6aSSam Leffler 	bo->bo_caps = (u_int16_t *)frm;
14548a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(capinfo);
14558a1b9b6aSSam Leffler 	frm += 2;
14568a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
14578a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
14588a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
14598a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
14608a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
14618a1b9b6aSSam Leffler 	} else
14628a1b9b6aSSam Leffler 		*frm++ = 0;
14638a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
14648a1b9b6aSSam Leffler 	if (ic->ic_curmode != IEEE80211_MODE_FH) {
14658a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
14668a1b9b6aSSam Leffler 		*frm++ = 1;
14678a1b9b6aSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
14688a1b9b6aSSam Leffler 	}
14698a1b9b6aSSam Leffler 	bo->bo_tim = frm;
14708a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
14718a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
14728a1b9b6aSSam Leffler 		*frm++ = 2;
14738a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
14748a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
14758a1b9b6aSSam Leffler 	} else {
14768a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
14778a1b9b6aSSam Leffler 
14788a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
14798a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
14808a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
14818a1b9b6aSSam Leffler 		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
14828a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
14838a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
14848a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
14858a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
14868a1b9b6aSSam Leffler 	}
14878a1b9b6aSSam Leffler 	bo->bo_trailer = frm;
14888a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
14898a1b9b6aSSam Leffler 		bo->bo_wme = frm;
14908a1b9b6aSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
14915aa17efaSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
14928a1b9b6aSSam Leffler 	}
14938a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA)
14948a1b9b6aSSam Leffler 		frm = ieee80211_add_wpa(frm, ic);
14950912bac9SSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G) {
14960912bac9SSam Leffler 		bo->bo_erp = frm;
14978a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
14980912bac9SSam Leffler 	}
14998a1b9b6aSSam Leffler 	efrm = ieee80211_add_xrates(frm, rs);
15008a1b9b6aSSam Leffler 	bo->bo_trailer_len = efrm - bo->bo_trailer;
15018a1b9b6aSSam Leffler 	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
15028a1b9b6aSSam Leffler 
15038a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
15048a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
15058a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
15068a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
15078a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
15088a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
15098a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
15108a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
15118a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
15128a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
15138a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_seq = 0;
15148a1b9b6aSSam Leffler 
15158a1b9b6aSSam Leffler 	return m;
15168a1b9b6aSSam Leffler }
15178a1b9b6aSSam Leffler 
15188a1b9b6aSSam Leffler /*
15198a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
15208a1b9b6aSSam Leffler  */
15218a1b9b6aSSam Leffler int
15228a1b9b6aSSam Leffler ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
15238a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
15248a1b9b6aSSam Leffler {
15258a1b9b6aSSam Leffler 	int len_changed = 0;
15268a1b9b6aSSam Leffler 	u_int16_t capinfo;
15278a1b9b6aSSam Leffler 
15288a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
15298a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
15308a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
15318a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
15328a1b9b6aSSam Leffler 	else
15338a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
15348a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
15358a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
15368a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
15378a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
15388a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
15398a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
15408a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
15418a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
15428a1b9b6aSSam Leffler 
15438a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
15448a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
15458a1b9b6aSSam Leffler 
15468a1b9b6aSSam Leffler 		/*
15478a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
15488a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
15498a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
15508a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
15518a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
15528a1b9b6aSSam Leffler 		 */
15538a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
15548a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
15558a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
15568a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
15578a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
15588a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
15598a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
15608a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
15618a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
15628a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
15638a1b9b6aSSam Leffler 			} else
15648a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
15658a1b9b6aSSam Leffler 		} else {
15668a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
15678a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
15688a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
15698a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
15708a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
15718a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
15728a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
15738a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
15748a1b9b6aSSam Leffler 			} else
15758a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
15768a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
15778a1b9b6aSSam Leffler 		}
15788a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
15798a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
15808a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
15818a1b9b6aSSam Leffler 		}
15828a1b9b6aSSam Leffler 	}
15838a1b9b6aSSam Leffler 
15848a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
15858a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
15868a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
15878a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
15888a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
15898a1b9b6aSSam Leffler 			/*
15908a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
15918a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
15928a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
15938a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
15948a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
15958a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
15968a1b9b6aSSam Leffler 			 * maximal-size virtual bitmap (based on ic_max_aid).
15978a1b9b6aSSam Leffler 			 */
15988a1b9b6aSSam Leffler 			/*
15998a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
16008a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
16018a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
16028a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
16038a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
16048a1b9b6aSSam Leffler 			 */
16058a1b9b6aSSam Leffler 			if (ic->ic_ps_pending != 0) {
16068a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
16078a1b9b6aSSam Leffler 				for (i = 0; i < ic->ic_tim_len; i++)
16088a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i]) {
16098a1b9b6aSSam Leffler 						timoff = i &~ 1;
16108a1b9b6aSSam Leffler 						break;
16118a1b9b6aSSam Leffler 					}
16128a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
16138a1b9b6aSSam Leffler 				for (i = ic->ic_tim_len-1; i >= timoff; i--)
16148a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i])
16158a1b9b6aSSam Leffler 						break;
16168a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
16178a1b9b6aSSam Leffler 			} else {
16188a1b9b6aSSam Leffler 				timoff = 0;
16198a1b9b6aSSam Leffler 				timlen = 1;
16208a1b9b6aSSam Leffler 			}
16218a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
16228a1b9b6aSSam Leffler 				/* copy up/down trailer */
16230912bac9SSam Leffler 				int adjust = tie->tim_bitmap+timlen
16240912bac9SSam Leffler 					   - bo->bo_trailer;
16250912bac9SSam Leffler 				ovbcopy(bo->bo_trailer, bo->bo_trailer+adjust,
16268a1b9b6aSSam Leffler 					bo->bo_trailer_len);
16270912bac9SSam Leffler 				bo->bo_trailer += adjust;
16280912bac9SSam Leffler 				bo->bo_wme += adjust;
16290912bac9SSam Leffler 				bo->bo_erp += adjust;
16308a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
16318a1b9b6aSSam Leffler 
16328a1b9b6aSSam Leffler 				/* update information element */
16338a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
16348a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
16358a1b9b6aSSam Leffler 				len_changed = 1;
16368a1b9b6aSSam Leffler 			}
16378a1b9b6aSSam Leffler 			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
16388a1b9b6aSSam Leffler 				bo->bo_tim_len);
16398a1b9b6aSSam Leffler 
16408a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
16418a1b9b6aSSam Leffler 
16428a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
16438a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
16448a1b9b6aSSam Leffler 				__func__, ic->ic_ps_pending, timoff, timlen);
16458a1b9b6aSSam Leffler 		}
16468a1b9b6aSSam Leffler 		/* count down DTIM period */
16478a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
16488a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
16498a1b9b6aSSam Leffler 		else
16508a1b9b6aSSam Leffler 			tie->tim_count--;
16518a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
1652a196b35fSSam Leffler 		if (mcast && tie->tim_count == 0)
16538a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
16548a1b9b6aSSam Leffler 		else
16558a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
16560912bac9SSam Leffler 		if (ic->ic_flags_ext & IEEE80211_FEXT_ERPUPDATE) {
16570912bac9SSam Leffler 			/*
16580912bac9SSam Leffler 			 * ERP element needs updating.
16590912bac9SSam Leffler 			 */
16600912bac9SSam Leffler 			(void) ieee80211_add_erp(bo->bo_erp, ic);
16610912bac9SSam Leffler 			ic->ic_flags_ext &= ~IEEE80211_FEXT_ERPUPDATE;
16620912bac9SSam Leffler 		}
16638a1b9b6aSSam Leffler 	}
16648a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
16658a1b9b6aSSam Leffler 
16668a1b9b6aSSam Leffler 	return len_changed;
16678a1b9b6aSSam Leffler }
16688a1b9b6aSSam Leffler 
16698a1b9b6aSSam Leffler /*
16708a1b9b6aSSam Leffler  * Save an outbound packet for a node in power-save sleep state.
16718a1b9b6aSSam Leffler  * The new packet is placed on the node's saved queue, and the TIM
16728a1b9b6aSSam Leffler  * is changed, if necessary.
16738a1b9b6aSSam Leffler  */
16748a1b9b6aSSam Leffler void
16758a1b9b6aSSam Leffler ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
16768a1b9b6aSSam Leffler 		  struct mbuf *m)
16778a1b9b6aSSam Leffler {
16788a1b9b6aSSam Leffler 	int qlen, age;
16798a1b9b6aSSam Leffler 
16808a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_LOCK(ni);
16818a1b9b6aSSam Leffler 	if (_IF_QFULL(&ni->ni_savedq)) {
16828a1b9b6aSSam Leffler 		_IF_DROP(&ni->ni_savedq);
16838a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
16848a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
16858a1b9b6aSSam Leffler 			"[%s] pwr save q overflow, drops %d (size %d)\n",
16868a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr),
16878a1b9b6aSSam Leffler 			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
16888a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
16898a1b9b6aSSam Leffler 		if (ieee80211_msg_dumppkts(ic))
16908a1b9b6aSSam Leffler 			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
16918a1b9b6aSSam Leffler #endif
16928a1b9b6aSSam Leffler 		m_freem(m);
16938a1b9b6aSSam Leffler 		return;
16948a1b9b6aSSam Leffler 	}
16958a1b9b6aSSam Leffler 	/*
16968a1b9b6aSSam Leffler 	 * Tag the frame with it's expiry time and insert
16978a1b9b6aSSam Leffler 	 * it in the queue.  The aging interval is 4 times
16988a1b9b6aSSam Leffler 	 * the listen interval specified by the station.
16998a1b9b6aSSam Leffler 	 * Frames that sit around too long are reclaimed
17008a1b9b6aSSam Leffler 	 * using this information.
17018a1b9b6aSSam Leffler 	 */
17028a1b9b6aSSam Leffler 	/* XXX handle overflow? */
1703d365f9c7SSam Leffler 	age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
17048a1b9b6aSSam Leffler 	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
17058a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
17068a1b9b6aSSam Leffler 
17078a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1708d365f9c7SSam Leffler 		"[%s] save frame with age %d, %u now queued\n",
1709d365f9c7SSam Leffler 		ether_sprintf(ni->ni_macaddr), age, qlen);
17108a1b9b6aSSam Leffler 
17118a1b9b6aSSam Leffler 	if (qlen == 1)
1712edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 1);
17138a1b9b6aSSam Leffler }
1714