xref: /freebsd/sys/net80211/ieee80211_output.c (revision d365f9c76094de4e2c66f4a916999de432e50628)
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,
1451a1e1d21SSam Leffler     struct mbuf *m, int type)
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],
1908a1b9b6aSSam Leffler 		    ieee80211_chan2ieee(ic, ni->ni_chan));
1918a1b9b6aSSam Leffler 	}
1928a1b9b6aSSam Leffler #endif
1938a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1941a1e1d21SSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);
1951a1e1d21SSam Leffler 	ifp->if_timer = 1;
196af5e59bfSRobert Watson 	if_start(ifp);
1971a1e1d21SSam Leffler 	return 0;
1981a1e1d21SSam Leffler }
1991a1e1d21SSam Leffler 
2000a915fadSSam Leffler /*
2018a1b9b6aSSam Leffler  * Send a null data frame to the specified node.
20219ad2dd7SSam Leffler  *
20319ad2dd7SSam Leffler  * NB: the caller is assumed to have setup a node reference
20419ad2dd7SSam Leffler  *     for use; this is necessary to deal with a race condition
20519ad2dd7SSam Leffler  *     when probing for inactive stations.
2068a1b9b6aSSam Leffler  */
2078a1b9b6aSSam Leffler int
208f62121ceSSam Leffler ieee80211_send_nulldata(struct ieee80211_node *ni)
2098a1b9b6aSSam Leffler {
210f62121ceSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2118a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
2128a1b9b6aSSam Leffler 	struct mbuf *m;
2138a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
2148a1b9b6aSSam Leffler 
2158a1b9b6aSSam Leffler 	MGETHDR(m, M_NOWAIT, MT_HEADER);
2168a1b9b6aSSam Leffler 	if (m == NULL) {
2178a1b9b6aSSam Leffler 		/* XXX debug msg */
2188a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
21919ad2dd7SSam Leffler 		ieee80211_unref_node(&ni);
2208a1b9b6aSSam Leffler 		return ENOMEM;
2218a1b9b6aSSam Leffler 	}
22219ad2dd7SSam Leffler 	m->m_pkthdr.rcvif = (void *) ni;
2238a1b9b6aSSam Leffler 
2248a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
225add59d08SSam Leffler 	ieee80211_send_setup(ic, ni, wh,
226add59d08SSam Leffler 		IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
227add59d08SSam Leffler 		ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
228add59d08SSam Leffler 	/* NB: power management bit is never sent by an AP */
229add59d08SSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
230add59d08SSam Leffler 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
231add59d08SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
2328a1b9b6aSSam Leffler 	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
2338a1b9b6aSSam Leffler 
2348a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
2358a1b9b6aSSam Leffler 
236add59d08SSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
237add59d08SSam Leffler 	    "[%s] send null data frame on channel %u, pwr mgt %s\n",
238add59d08SSam Leffler 	    ether_sprintf(ni->ni_macaddr),
239add59d08SSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan),
240add59d08SSam Leffler 	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
241add59d08SSam Leffler 
2428a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
2438a1b9b6aSSam Leffler 	if_start(ifp);
2448a1b9b6aSSam Leffler 
2458a1b9b6aSSam Leffler 	return 0;
2468a1b9b6aSSam Leffler }
2478a1b9b6aSSam Leffler 
2488a1b9b6aSSam Leffler /*
2498a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
2508a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
2518a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
2528a1b9b6aSSam Leffler  * applied.
2538a1b9b6aSSam Leffler  */
2548a1b9b6aSSam Leffler int
2558a1b9b6aSSam Leffler ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
2568a1b9b6aSSam Leffler {
2578a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
2588a1b9b6aSSam Leffler #ifdef INET
2598a1b9b6aSSam Leffler 	struct ether_header *eh;
2608a1b9b6aSSam Leffler #endif
2618a1b9b6aSSam Leffler 
2628a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
2638a1b9b6aSSam Leffler 		ac = WME_AC_BE;
2648a1b9b6aSSam Leffler 		goto done;
2658a1b9b6aSSam Leffler 	}
2668a1b9b6aSSam Leffler 
2678a1b9b6aSSam Leffler 	/*
2688a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
2698a1b9b6aSSam Leffler 	 * to it must have a matching tag.
2708a1b9b6aSSam Leffler 	 */
2718a1b9b6aSSam Leffler 	v_wme_ac = 0;
2728a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
2738a1b9b6aSSam Leffler 		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
274bd40429eSSam Leffler 		 if (mtag == NULL) {
2758a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
2768a1b9b6aSSam Leffler 			return 1;
2778a1b9b6aSSam Leffler 		}
2788a1b9b6aSSam Leffler 		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
2798a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
2808a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
2818a1b9b6aSSam Leffler 			return 1;
2828a1b9b6aSSam Leffler 		}
2838a1b9b6aSSam Leffler 		/* map vlan priority to AC */
2848a1b9b6aSSam Leffler 		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
2858a1b9b6aSSam Leffler 		case 1:
2868a1b9b6aSSam Leffler 		case 2:
2878a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BK;
2888a1b9b6aSSam Leffler 			break;
2898a1b9b6aSSam Leffler 		case 0:
2908a1b9b6aSSam Leffler 		case 3:
2918a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BE;
2928a1b9b6aSSam Leffler 			break;
2938a1b9b6aSSam Leffler 		case 4:
2948a1b9b6aSSam Leffler 		case 5:
2958a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VI;
2968a1b9b6aSSam Leffler 			break;
2978a1b9b6aSSam Leffler 		case 6:
2988a1b9b6aSSam Leffler 		case 7:
2998a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VO;
3008a1b9b6aSSam Leffler 			break;
3018a1b9b6aSSam Leffler 		}
3028a1b9b6aSSam Leffler 	}
3038a1b9b6aSSam Leffler 
3048a1b9b6aSSam Leffler #ifdef INET
3058a1b9b6aSSam Leffler 	eh = mtod(m, struct ether_header *);
3068a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
3078a1b9b6aSSam Leffler 		const struct ip *ip = (struct ip *)
3088a1b9b6aSSam Leffler 			(mtod(m, u_int8_t *) + sizeof (*eh));
3098a1b9b6aSSam Leffler 		/*
3108a1b9b6aSSam Leffler 		 * IP frame, map the TOS field.
3118a1b9b6aSSam Leffler 		 */
3128a1b9b6aSSam Leffler 		switch (ip->ip_tos) {
3138a1b9b6aSSam Leffler 		case 0x08:
3148a1b9b6aSSam Leffler 		case 0x20:
3158a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BK;	/* background */
3168a1b9b6aSSam Leffler 			break;
3178a1b9b6aSSam Leffler 		case 0x28:
3188a1b9b6aSSam Leffler 		case 0xa0:
3198a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VI;	/* video */
3208a1b9b6aSSam Leffler 			break;
3218a1b9b6aSSam Leffler 		case 0x30:			/* voice */
3228a1b9b6aSSam Leffler 		case 0xe0:
3238a1b9b6aSSam Leffler 		case 0x88:			/* XXX UPSD */
3248a1b9b6aSSam Leffler 		case 0xb8:
3258a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VO;
3268a1b9b6aSSam Leffler 			break;
3278a1b9b6aSSam Leffler 		default:
3288a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BE;
3298a1b9b6aSSam Leffler 			break;
3308a1b9b6aSSam Leffler 		}
3318a1b9b6aSSam Leffler 	} else {
3328a1b9b6aSSam Leffler #endif /* INET */
3338a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
3348a1b9b6aSSam Leffler #ifdef INET
3358a1b9b6aSSam Leffler 	}
3368a1b9b6aSSam Leffler #endif
3378a1b9b6aSSam Leffler 	/*
3388a1b9b6aSSam Leffler 	 * Use highest priority AC.
3398a1b9b6aSSam Leffler 	 */
3408a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
3418a1b9b6aSSam Leffler 		ac = v_wme_ac;
3428a1b9b6aSSam Leffler 	else
3438a1b9b6aSSam Leffler 		ac = d_wme_ac;
3448a1b9b6aSSam Leffler 
3458a1b9b6aSSam Leffler 	/*
3468a1b9b6aSSam Leffler 	 * Apply ACM policy.
3478a1b9b6aSSam Leffler 	 */
3488a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
3498a1b9b6aSSam Leffler 		static const int acmap[4] = {
3508a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
3518a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
3528a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
3538a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
3548a1b9b6aSSam Leffler 		};
3558a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
3568a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
3578a1b9b6aSSam Leffler 			ac = acmap[ac];
3588a1b9b6aSSam Leffler 	}
3598a1b9b6aSSam Leffler done:
3608a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
3618a1b9b6aSSam Leffler 	return 0;
3628a1b9b6aSSam Leffler }
3638a1b9b6aSSam Leffler 
3648a1b9b6aSSam Leffler /*
3655e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
3665e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
3675e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
3685e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
3695e923d2eSSam Leffler  */
3705e923d2eSSam Leffler static struct mbuf *
3715e923d2eSSam Leffler ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
3725e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
3735e923d2eSSam Leffler {
3745e923d2eSSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
3755e923d2eSSam Leffler 	int needed_space = hdrsize;
3765e923d2eSSam Leffler 
3775e923d2eSSam Leffler 	if (key != NULL) {
3785e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
3795e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
3805e923d2eSSam Leffler 		/* XXX frags */
3815e923d2eSSam Leffler 	}
3825e923d2eSSam Leffler 	/*
3835e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
3845e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
3855e923d2eSSam Leffler 	 * there will be
3865e923d2eSSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
3875e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
3885e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
3895e923d2eSSam Leffler 	 */
3905e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
3915e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
3925e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
3935e923d2eSSam Leffler 		if (n == NULL) {
3945e923d2eSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
3955e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
3965e923d2eSSam Leffler 			ic->ic_stats.is_tx_nobuf++;
3975e923d2eSSam Leffler 			m_freem(m);
3985e923d2eSSam Leffler 			return NULL;
3995e923d2eSSam Leffler 		}
4005e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
4015e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
4025e923d2eSSam Leffler 		/*
4035e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
4045e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
4055e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
4065e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
4075e923d2eSSam Leffler 		 */
4085e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
4095e923d2eSSam Leffler 		m_move_pkthdr(n, m);
4105e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
4115e923d2eSSam Leffler 		n->m_data += needed_space;
4125e923d2eSSam Leffler 		/*
4135e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
4145e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
4155e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
4165e923d2eSSam Leffler 		 * for speed.
4175e923d2eSSam Leffler 		 */
4185e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
4195e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
4205e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
4215e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
4225e923d2eSSam Leffler 		/*
4235e923d2eSSam Leffler 		 * Replace the head of the chain.
4245e923d2eSSam Leffler 		 */
4255e923d2eSSam Leffler 		n->m_next = m;
4265e923d2eSSam Leffler 		m = n;
4275e923d2eSSam Leffler 	}
4285e923d2eSSam Leffler 	return m;
4295e923d2eSSam Leffler #undef TO_BE_RECLAIMED
4305e923d2eSSam Leffler }
4315e923d2eSSam Leffler 
4325e923d2eSSam Leffler #define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
4335e923d2eSSam Leffler /*
4345e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
4355e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
4365e923d2eSSam Leffler  * we fall back to the default transmit key.
4378a1b9b6aSSam Leffler  */
4388a1b9b6aSSam Leffler static __inline struct ieee80211_key *
4395e923d2eSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
4408a1b9b6aSSam Leffler {
4415e923d2eSSam Leffler 	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
4428a1b9b6aSSam Leffler 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4438a1b9b6aSSam Leffler 		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4448a1b9b6aSSam Leffler 			return NULL;
4458a1b9b6aSSam Leffler 		return &ic->ic_nw_keys[ic->ic_def_txkey];
4468a1b9b6aSSam Leffler 	} else {
4478a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
4488a1b9b6aSSam Leffler 	}
4495e923d2eSSam Leffler }
4505e923d2eSSam Leffler 
4515e923d2eSSam Leffler /*
4525e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
4535e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
4545e923d2eSSam Leffler  * the default tx key.
4555e923d2eSSam Leffler  */
4565e923d2eSSam Leffler static __inline struct ieee80211_key *
4575e923d2eSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
4585e923d2eSSam Leffler {
4595e923d2eSSam Leffler 	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4605e923d2eSSam Leffler 	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4615e923d2eSSam Leffler 		return NULL;
4625e923d2eSSam Leffler 	return &ic->ic_nw_keys[ic->ic_def_txkey];
4638a1b9b6aSSam Leffler }
4648a1b9b6aSSam Leffler 
4658a1b9b6aSSam Leffler /*
4668a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
4678a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
4688a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
4698a1b9b6aSSam Leffler  * first mbuf.
4700a915fadSSam Leffler  */
4711a1e1d21SSam Leffler struct mbuf *
4728a1b9b6aSSam Leffler ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
4738a1b9b6aSSam Leffler 	struct ieee80211_node *ni)
4741a1e1d21SSam Leffler {
4751a1e1d21SSam Leffler 	struct ether_header eh;
4761a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
4778a1b9b6aSSam Leffler 	struct ieee80211_key *key;
4781a1e1d21SSam Leffler 	struct llc *llc;
4795e923d2eSSam Leffler 	int hdrsize, datalen, addqos;
4801a1e1d21SSam Leffler 
4818a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
4821a1e1d21SSam Leffler 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
4831a1e1d21SSam Leffler 
4848a1b9b6aSSam Leffler 	/*
4858a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
4868a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
4878a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
4888a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
4898a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
4908a1b9b6aSSam Leffler 	 *
4918a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
4928a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
4938a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
4948a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
4958a1b9b6aSSam Leffler 	 */
4968a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
4975e923d2eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
4985e923d2eSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
4995e923d2eSSam Leffler 			key = ieee80211_crypto_getucastkey(ic, ni);
5005e923d2eSSam Leffler 		else
5015e923d2eSSam Leffler 			key = ieee80211_crypto_getmcastkey(ic, ni);
5028a1b9b6aSSam Leffler 		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
5038a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
5045e923d2eSSam Leffler 			    "[%s] no default transmit key (%s) deftxkey %u\n",
5055e923d2eSSam Leffler 			    ether_sprintf(eh.ether_dhost), __func__,
5065e923d2eSSam Leffler 			    ic->ic_def_txkey);
5075e923d2eSSam Leffler 			ic->ic_stats.is_tx_nodefkey++;
5088a1b9b6aSSam Leffler 		}
5098a1b9b6aSSam Leffler 	} else
5108a1b9b6aSSam Leffler 		key = NULL;
5118a1b9b6aSSam Leffler 	/* XXX 4-address format */
5125e923d2eSSam Leffler 	/*
5135e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
5145e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
5155e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
5165e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
5175e923d2eSSam Leffler 	 * configurable.
5185e923d2eSSam Leffler 	 */
5195e923d2eSSam Leffler 	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
5205e923d2eSSam Leffler 		 eh.ether_type != htons(ETHERTYPE_PAE);
5215e923d2eSSam Leffler 	if (addqos)
5228a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
5238a1b9b6aSSam Leffler 	else
5248a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
5258a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
5268a1b9b6aSSam Leffler 		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
5278a1b9b6aSSam Leffler 	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
5288a1b9b6aSSam Leffler 	if (m == NULL) {
5298a1b9b6aSSam Leffler 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
5300a915fadSSam Leffler 		goto bad;
5310a915fadSSam Leffler 	}
5321a1e1d21SSam Leffler 
5338a1b9b6aSSam Leffler 	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
5341a1e1d21SSam Leffler 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
5351a1e1d21SSam Leffler 	llc = mtod(m, struct llc *);
5361a1e1d21SSam Leffler 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
5371a1e1d21SSam Leffler 	llc->llc_control = LLC_UI;
5381a1e1d21SSam Leffler 	llc->llc_snap.org_code[0] = 0;
5391a1e1d21SSam Leffler 	llc->llc_snap.org_code[1] = 0;
5401a1e1d21SSam Leffler 	llc->llc_snap.org_code[2] = 0;
5411a1e1d21SSam Leffler 	llc->llc_snap.ether_type = eh.ether_type;
5428a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
5438a1b9b6aSSam Leffler 
5448a1b9b6aSSam Leffler 	M_PREPEND(m, hdrsize, M_DONTWAIT);
5451be50176SSam Leffler 	if (m == NULL) {
5468a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
5470a915fadSSam Leffler 		goto bad;
5481be50176SSam Leffler 	}
5491a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5501a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
5511a1e1d21SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
5521a1e1d21SSam Leffler 	switch (ic->ic_opmode) {
5531a1e1d21SSam Leffler 	case IEEE80211_M_STA:
5541a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5551a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
5561a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
5571a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
5581a1e1d21SSam Leffler 		break;
5591a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
5601a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
5611a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
5621a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5631a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
564a8b16e87SSam Leffler 		/*
565a8b16e87SSam Leffler 		 * NB: always use the bssid from ic_bss as the
566a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
567a8b16e87SSam Leffler 		 */
568a8b16e87SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
5691a1e1d21SSam Leffler 		break;
5701a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
5711a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
5721a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5731a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
5741a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
5751a1e1d21SSam Leffler 		break;
5762bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
5770a915fadSSam Leffler 		goto bad;
5781a1e1d21SSam Leffler 	}
579bc5627d9SSam Leffler 	if (m->m_flags & M_MORE_DATA)
580bc5627d9SSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
5815e923d2eSSam Leffler 	if (addqos) {
5828a1b9b6aSSam Leffler 		struct ieee80211_qosframe *qwh =
5838a1b9b6aSSam Leffler 			(struct ieee80211_qosframe *) wh;
5848a1b9b6aSSam Leffler 		int ac, tid;
5858a1b9b6aSSam Leffler 
5868a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
5878a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
5888a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
5898a1b9b6aSSam Leffler 		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
5908a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
5918a1b9b6aSSam Leffler 			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
5928a1b9b6aSSam Leffler 		qwh->i_qos[1] = 0;
5938a1b9b6aSSam Leffler 		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
5948a1b9b6aSSam Leffler 
5958a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
5968a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
5978a1b9b6aSSam Leffler 		ni->ni_txseqs[tid]++;
5988a1b9b6aSSam Leffler 	} else {
5998a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
6008a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
6018a1b9b6aSSam Leffler 		ni->ni_txseqs[0]++;
6028a1b9b6aSSam Leffler 	}
6035e923d2eSSam Leffler 	if (key != NULL) {
6045e923d2eSSam Leffler 		/*
6055e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
6065e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
6075e923d2eSSam Leffler 		 */
6085e923d2eSSam Leffler 		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
6095e923d2eSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_WPA) &&
610b4b64678SSam Leffler 		     (ic->ic_opmode == IEEE80211_M_STA ?
611b4b64678SSam Leffler 		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
6125e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
6135e923d2eSSam Leffler 			/* XXX do fragmentation */
61496d88463SSam Leffler 			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
6155e923d2eSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
6165e923d2eSSam Leffler 				    "[%s] enmic failed, discard frame\n",
6175e923d2eSSam Leffler 				    ether_sprintf(eh.ether_dhost));
6185e923d2eSSam Leffler 				ic->ic_stats.is_crypto_enmicfail++;
6195e923d2eSSam Leffler 				goto bad;
6205e923d2eSSam Leffler 			}
6215e923d2eSSam Leffler 		}
6225e923d2eSSam Leffler 	}
6238a1b9b6aSSam Leffler 
6248a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
6258a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
6268a1b9b6aSSam Leffler 
6271a1e1d21SSam Leffler 	return m;
6280a915fadSSam Leffler bad:
6290a915fadSSam Leffler 	if (m != NULL)
6300a915fadSSam Leffler 		m_freem(m);
6310a915fadSSam Leffler 	return NULL;
6321a1e1d21SSam Leffler }
6331a1e1d21SSam Leffler 
6341a1e1d21SSam Leffler /*
6351a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
6361a1e1d21SSam Leffler  */
6378a1b9b6aSSam Leffler static u_int8_t *
6381a1e1d21SSam Leffler ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
6391a1e1d21SSam Leffler {
6401a1e1d21SSam Leffler 	int nrates;
6411a1e1d21SSam Leffler 
6421a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
6431a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
6441a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
6451a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
6461a1e1d21SSam Leffler 	*frm++ = nrates;
6471a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
6481a1e1d21SSam Leffler 	return frm + nrates;
6491a1e1d21SSam Leffler }
6501a1e1d21SSam Leffler 
6511a1e1d21SSam Leffler /*
6521a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
6531a1e1d21SSam Leffler  */
6548a1b9b6aSSam Leffler static u_int8_t *
6551a1e1d21SSam Leffler ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
6561a1e1d21SSam Leffler {
6571a1e1d21SSam Leffler 	/*
6581a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
6591a1e1d21SSam Leffler 	 */
6601a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
6611a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
6621a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
6631a1e1d21SSam Leffler 		*frm++ = nrates;
6641a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
6651a1e1d21SSam Leffler 		frm += nrates;
6661a1e1d21SSam Leffler 	}
6671a1e1d21SSam Leffler 	return frm;
6681a1e1d21SSam Leffler }
6691a1e1d21SSam Leffler 
6701a1e1d21SSam Leffler /*
6711a1e1d21SSam Leffler  * Add an ssid elemet to a frame.
6721a1e1d21SSam Leffler  */
6731a1e1d21SSam Leffler static u_int8_t *
6741a1e1d21SSam Leffler ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
6751a1e1d21SSam Leffler {
6761a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
6771a1e1d21SSam Leffler 	*frm++ = len;
6781a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
6791a1e1d21SSam Leffler 	return frm + len;
6801a1e1d21SSam Leffler }
6811a1e1d21SSam Leffler 
6828a1b9b6aSSam Leffler /*
6838a1b9b6aSSam Leffler  * Add an erp element to a frame.
6848a1b9b6aSSam Leffler  */
6858a1b9b6aSSam Leffler static u_int8_t *
6868a1b9b6aSSam Leffler ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
6871a1e1d21SSam Leffler {
6888a1b9b6aSSam Leffler 	u_int8_t erp;
6891a1e1d21SSam Leffler 
6908a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
6918a1b9b6aSSam Leffler 	*frm++ = 1;
6928a1b9b6aSSam Leffler 	erp = 0;
6938a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
6948a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
6958a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
6968a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
6978a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
6988a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
6998a1b9b6aSSam Leffler 	*frm++ = erp;
7008a1b9b6aSSam Leffler 	return frm;
7011a1e1d21SSam Leffler }
7021a1e1d21SSam Leffler 
7038a1b9b6aSSam Leffler static u_int8_t *
7048a1b9b6aSSam Leffler ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
7058a1b9b6aSSam Leffler {
7068a1b9b6aSSam Leffler #define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
7078a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
7088a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
7098a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
7108a1b9b6aSSam Leffler 	frm += 2;				\
7118a1b9b6aSSam Leffler } while (0)
7128a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
7138a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
7148a1b9b6aSSam Leffler 	frm += 4;				\
7158a1b9b6aSSam Leffler } while (0)
7168a1b9b6aSSam Leffler 	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
7178a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
7188a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
7198a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
7208a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
7218a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
7228a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
7238a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_NULL },
7248a1b9b6aSSam Leffler 	};
7258a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
7268a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
7278a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
7288a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
7298a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
7308a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
7318a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
7328a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
7338a1b9b6aSSam Leffler 	u_int8_t *selcnt;
7348a1b9b6aSSam Leffler 
7358a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_VENDOR;
7368a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
7378a1b9b6aSSam Leffler 	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
7388a1b9b6aSSam Leffler 	frm += sizeof(oui);
7398a1b9b6aSSam Leffler 	ADDSHORT(frm, WPA_VERSION);
7408a1b9b6aSSam Leffler 
7418a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
7428a1b9b6aSSam Leffler 
7438a1b9b6aSSam Leffler 	/* multicast cipher */
7448a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
7458a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
7468a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
7478a1b9b6aSSam Leffler 	else
7488a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
7498a1b9b6aSSam Leffler 
7508a1b9b6aSSam Leffler 	/* unicast cipher list */
7518a1b9b6aSSam Leffler 	selcnt = frm;
7528a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7538a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
7548a1b9b6aSSam Leffler 		selcnt[0]++;
7558a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
7568a1b9b6aSSam Leffler 	}
7578a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
7588a1b9b6aSSam Leffler 		selcnt[0]++;
7598a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
7608a1b9b6aSSam Leffler 	}
7618a1b9b6aSSam Leffler 
7628a1b9b6aSSam Leffler 	/* authenticator selector list */
7638a1b9b6aSSam Leffler 	selcnt = frm;
7648a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7658a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
7668a1b9b6aSSam Leffler 		selcnt[0]++;
7678a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
7688a1b9b6aSSam Leffler 	}
7698a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
7708a1b9b6aSSam Leffler 		selcnt[0]++;
7718a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
7728a1b9b6aSSam Leffler 	}
7738a1b9b6aSSam Leffler 
7748a1b9b6aSSam Leffler 	/* optional capabilities */
7756aa57182SSam Leffler 	if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
7768a1b9b6aSSam Leffler 		ADDSHORT(frm, rsn->rsn_caps);
7778a1b9b6aSSam Leffler 
7788a1b9b6aSSam Leffler 	/* calculate element length */
7798a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
7808a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
78116574882SSam Leffler 		("WPA IE too big, %u > %zu",
7828a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
7838a1b9b6aSSam Leffler 	return frm;
7848a1b9b6aSSam Leffler #undef ADDSHORT
7858a1b9b6aSSam Leffler #undef ADDSELECTOR
7868a1b9b6aSSam Leffler #undef WPA_OUI_BYTES
7878a1b9b6aSSam Leffler }
7888a1b9b6aSSam Leffler 
7898a1b9b6aSSam Leffler static u_int8_t *
7908a1b9b6aSSam Leffler ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
7918a1b9b6aSSam Leffler {
7928a1b9b6aSSam Leffler #define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
7938a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
7948a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
7958a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
7968a1b9b6aSSam Leffler 	frm += 2;				\
7978a1b9b6aSSam Leffler } while (0)
7988a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
7998a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
8008a1b9b6aSSam Leffler 	frm += 4;				\
8018a1b9b6aSSam Leffler } while (0)
8028a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
8038a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
8048a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
8058a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
8068a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
8078a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
8088a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_NULL },
8098a1b9b6aSSam Leffler 	};
8108a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
8118a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
8128a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
8138a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
8148a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
8158a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
8168a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
8178a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
8188a1b9b6aSSam Leffler 	u_int8_t *selcnt;
8198a1b9b6aSSam Leffler 
8208a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_RSN;
8218a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
8228a1b9b6aSSam Leffler 	ADDSHORT(frm, RSN_VERSION);
8238a1b9b6aSSam Leffler 
8248a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
8258a1b9b6aSSam Leffler 
8268a1b9b6aSSam Leffler 	/* multicast cipher */
8278a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
8288a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
8298a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
8308a1b9b6aSSam Leffler 	else
8318a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
8328a1b9b6aSSam Leffler 
8338a1b9b6aSSam Leffler 	/* unicast cipher list */
8348a1b9b6aSSam Leffler 	selcnt = frm;
8358a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
8368a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
8378a1b9b6aSSam Leffler 		selcnt[0]++;
8388a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
8398a1b9b6aSSam Leffler 	}
8408a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
8418a1b9b6aSSam Leffler 		selcnt[0]++;
8428a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
8438a1b9b6aSSam Leffler 	}
8448a1b9b6aSSam Leffler 
8458a1b9b6aSSam Leffler 	/* authenticator selector list */
8468a1b9b6aSSam Leffler 	selcnt = frm;
8478a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
8488a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
8498a1b9b6aSSam Leffler 		selcnt[0]++;
8508a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
8518a1b9b6aSSam Leffler 	}
8528a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
8538a1b9b6aSSam Leffler 		selcnt[0]++;
8548a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
8558a1b9b6aSSam Leffler 	}
8568a1b9b6aSSam Leffler 
8578a1b9b6aSSam Leffler 	/* optional capabilities */
8588a1b9b6aSSam Leffler 	ADDSHORT(frm, rsn->rsn_caps);
8598a1b9b6aSSam Leffler 	/* XXX PMKID */
8608a1b9b6aSSam Leffler 
8618a1b9b6aSSam Leffler 	/* calculate element length */
8628a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
8638a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
86416574882SSam Leffler 		("RSN IE too big, %u > %zu",
8658a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
8668a1b9b6aSSam Leffler 	return frm;
8678a1b9b6aSSam Leffler #undef ADDSELECTOR
8688a1b9b6aSSam Leffler #undef ADDSHORT
8698a1b9b6aSSam Leffler #undef RSN_OUI_BYTES
8708a1b9b6aSSam Leffler }
8718a1b9b6aSSam Leffler 
8728a1b9b6aSSam Leffler /*
8738a1b9b6aSSam Leffler  * Add a WPA/RSN element to a frame.
8748a1b9b6aSSam Leffler  */
8758a1b9b6aSSam Leffler static u_int8_t *
8768a1b9b6aSSam Leffler ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
8778a1b9b6aSSam Leffler {
8788a1b9b6aSSam Leffler 
8798a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
8808a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA2)
8818a1b9b6aSSam Leffler 		frm = ieee80211_setup_rsn_ie(ic, frm);
8828a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA1)
8838a1b9b6aSSam Leffler 		frm = ieee80211_setup_wpa_ie(ic, frm);
8848a1b9b6aSSam Leffler 	return frm;
8858a1b9b6aSSam Leffler }
8868a1b9b6aSSam Leffler 
8878a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
8888a1b9b6aSSam Leffler /*
8898a1b9b6aSSam Leffler  * Add a WME information element to a frame.
8908a1b9b6aSSam Leffler  */
8918a1b9b6aSSam Leffler static u_int8_t *
8928a1b9b6aSSam Leffler ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
8938a1b9b6aSSam Leffler {
8948a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
8958a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
8968a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
8978a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
8988a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
8998a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
9008a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
9018a1b9b6aSSam Leffler 		.wme_info	= 0,
9028a1b9b6aSSam Leffler 	};
9038a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
9048a1b9b6aSSam Leffler 	return frm + sizeof(info);
9058a1b9b6aSSam Leffler }
9068a1b9b6aSSam Leffler 
9078a1b9b6aSSam Leffler /*
9088a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
9098a1b9b6aSSam Leffler  */
9108a1b9b6aSSam Leffler static u_int8_t *
9118a1b9b6aSSam Leffler ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
9128a1b9b6aSSam Leffler {
9138a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
9148a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
9158a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
9168a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
9178a1b9b6aSSam Leffler 	frm += 2;				\
9188a1b9b6aSSam Leffler } while (0)
9198a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
9208a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
9218a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
9228a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
9238a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
9248a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
9258a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
9268a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
9278a1b9b6aSSam Leffler 	};
9288a1b9b6aSSam Leffler 	int i;
9298a1b9b6aSSam Leffler 
9308a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
9318a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
9328a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
9338a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
9348a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
9358a1b9b6aSSam Leffler 		const struct wmeParams *ac =
9368a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
9378a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
9388a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
9398a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
9408a1b9b6aSSam Leffler 		       ;
9418a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
9428a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
9438a1b9b6aSSam Leffler 		       ;
9448a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
9458a1b9b6aSSam Leffler 	}
9468a1b9b6aSSam Leffler 	return frm;
9478a1b9b6aSSam Leffler #undef SM
9488a1b9b6aSSam Leffler #undef ADDSHORT
9498a1b9b6aSSam Leffler }
9508a1b9b6aSSam Leffler #undef WME_OUI_BYTES
9518a1b9b6aSSam Leffler 
9520a915fadSSam Leffler /*
953af8418dcSSam Leffler  * Send a probe request frame with the specified ssid
954af8418dcSSam Leffler  * and any optional information element data.
955af8418dcSSam Leffler  */
956af8418dcSSam Leffler int
957af8418dcSSam Leffler ieee80211_send_probereq(struct ieee80211_node *ni,
958af8418dcSSam Leffler 	const u_int8_t sa[IEEE80211_ADDR_LEN],
959af8418dcSSam Leffler 	const u_int8_t da[IEEE80211_ADDR_LEN],
960af8418dcSSam Leffler 	const u_int8_t bssid[IEEE80211_ADDR_LEN],
961af8418dcSSam Leffler 	const u_int8_t *ssid, size_t ssidlen,
962af8418dcSSam Leffler 	const void *optie, size_t optielen)
963af8418dcSSam Leffler {
964af8418dcSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
965af8418dcSSam Leffler 	enum ieee80211_phymode mode;
966af8418dcSSam Leffler 	struct ieee80211_frame *wh;
967af8418dcSSam Leffler 	struct mbuf *m;
968af8418dcSSam Leffler 	u_int8_t *frm;
969af8418dcSSam Leffler 
970af8418dcSSam Leffler 	/*
971af8418dcSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
972af8418dcSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
973af8418dcSSam Leffler 	 * will remove our reference.
974af8418dcSSam Leffler 	 */
975af8418dcSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
976af8418dcSSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
977af8418dcSSam Leffler 		__func__, __LINE__,
978af8418dcSSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
979af8418dcSSam Leffler 		ieee80211_node_refcnt(ni)+1);
980af8418dcSSam Leffler 	ieee80211_ref_node(ni);
981af8418dcSSam Leffler 
982af8418dcSSam Leffler 	/*
983af8418dcSSam Leffler 	 * prreq frame format
984af8418dcSSam Leffler 	 *	[tlv] ssid
985af8418dcSSam Leffler 	 *	[tlv] supported rates
986af8418dcSSam Leffler 	 *	[tlv] extended supported rates
987af8418dcSSam Leffler 	 *	[tlv] user-specified ie's
988af8418dcSSam Leffler 	 */
989af8418dcSSam Leffler 	m = ieee80211_getmgtframe(&frm,
990af8418dcSSam Leffler 		 2 + IEEE80211_NWID_LEN
991af8418dcSSam Leffler 	       + 2 + IEEE80211_RATE_SIZE
992af8418dcSSam Leffler 	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
993af8418dcSSam Leffler 	       + (optie != NULL ? optielen : 0)
994af8418dcSSam Leffler 	);
995af8418dcSSam Leffler 	if (m == NULL) {
996af8418dcSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
997af8418dcSSam Leffler 		ieee80211_free_node(ni);
998af8418dcSSam Leffler 		return ENOMEM;
999af8418dcSSam Leffler 	}
1000af8418dcSSam Leffler 
1001af8418dcSSam Leffler 	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1002af8418dcSSam Leffler 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
1003af8418dcSSam Leffler 	frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1004af8418dcSSam Leffler 	frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1005af8418dcSSam Leffler 
1006af8418dcSSam Leffler 	if (optie != NULL) {
1007af8418dcSSam Leffler 		memcpy(frm, optie, optielen);
1008af8418dcSSam Leffler 		frm += optielen;
1009af8418dcSSam Leffler 	}
1010af8418dcSSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1011af8418dcSSam Leffler 
1012af8418dcSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1013af8418dcSSam Leffler 	if (m == NULL)
1014af8418dcSSam Leffler 		return ENOMEM;
1015af8418dcSSam Leffler 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1016af8418dcSSam Leffler 	m->m_pkthdr.rcvif = (void *)ni;
1017af8418dcSSam Leffler 
1018af8418dcSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1019af8418dcSSam Leffler 	ieee80211_send_setup(ic, ni, wh,
1020af8418dcSSam Leffler 		IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1021af8418dcSSam Leffler 		sa, da, bssid);
1022af8418dcSSam Leffler 	/* XXX power management? */
1023af8418dcSSam Leffler 
1024af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_probereq);
1025af8418dcSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1026af8418dcSSam Leffler 
1027af8418dcSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1028af8418dcSSam Leffler 	    "[%s] send probe req on channel %u\n",
1029af8418dcSSam Leffler 	    ether_sprintf(wh->i_addr1),
1030af8418dcSSam Leffler 	    ieee80211_chan2ieee(ic, ni->ni_chan));
1031af8418dcSSam Leffler 
1032af8418dcSSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);
1033af8418dcSSam Leffler 	if_start(ic->ic_ifp);
1034af8418dcSSam Leffler 	return 0;
1035af8418dcSSam Leffler }
1036af8418dcSSam Leffler 
1037af8418dcSSam Leffler /*
10380a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
10390a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
10400a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
10410a915fadSSam Leffler  */
10421a1e1d21SSam Leffler int
10431a1e1d21SSam Leffler ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
10441a1e1d21SSam Leffler 	int type, int arg)
10451a1e1d21SSam Leffler {
10461be50176SSam Leffler #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
10471a1e1d21SSam Leffler 	struct mbuf *m;
10481a1e1d21SSam Leffler 	u_int8_t *frm;
10491a1e1d21SSam Leffler 	u_int16_t capinfo;
10508a1b9b6aSSam Leffler 	int has_challenge, is_shared_key, ret, timer, status;
10511a1e1d21SSam Leffler 
10520a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
10530a915fadSSam Leffler 
10540a915fadSSam Leffler 	/*
10550a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
10560a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
10570a915fadSSam Leffler 	 * will remove our reference.
10580a915fadSSam Leffler 	 */
10598a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
106049a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
10618a1b9b6aSSam Leffler 		__func__, __LINE__,
106249a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
10638a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
10640a915fadSSam Leffler 	ieee80211_ref_node(ni);
10658a1b9b6aSSam Leffler 
10661a1e1d21SSam Leffler 	timer = 0;
10671a1e1d21SSam Leffler 	switch (type) {
10681a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
10691a1e1d21SSam Leffler 		/*
10701a1e1d21SSam Leffler 		 * probe response frame format
10711a1e1d21SSam Leffler 		 *	[8] time stamp
10721a1e1d21SSam Leffler 		 *	[2] beacon interval
10731a1e1d21SSam Leffler 		 *	[2] cabability information
10741a1e1d21SSam Leffler 		 *	[tlv] ssid
10751a1e1d21SSam Leffler 		 *	[tlv] supported rates
1076b83d2112SSam Leffler 		 *	[tlv] parameter set (FH/DS)
10771a1e1d21SSam Leffler 		 *	[tlv] parameter set (IBSS)
10788a1b9b6aSSam Leffler 		 *	[tlv] extended rate phy (ERP)
10791a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
10808a1b9b6aSSam Leffler 		 *	[tlv] WPA
1081d6ec172cSSam Leffler 		 *	[tlv] WME (optional)
10821a1e1d21SSam Leffler 		 */
10838a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
10848a1b9b6aSSam Leffler 			 8
10858a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
10868a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
10878a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
10881a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
10898a1b9b6aSSam Leffler 		       + 7	/* max(7,3) */
10901a1e1d21SSam Leffler 		       + 6
10918a1b9b6aSSam Leffler 		       + 3
10928a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
10938a1b9b6aSSam Leffler 		       /* XXX !WPA1+WPA2 fits w/o a cluster */
10948a1b9b6aSSam Leffler 		       + (ic->ic_flags & IEEE80211_F_WPA ?
10958a1b9b6aSSam Leffler 				2*sizeof(struct ieee80211_ie_wpa) : 0)
1096d6ec172cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
10978a1b9b6aSSam Leffler 		);
10981a1e1d21SSam Leffler 		if (m == NULL)
10998a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11001a1e1d21SSam Leffler 
11011a1e1d21SSam Leffler 		memset(frm, 0, 8);	/* timestamp should be filled later */
11021a1e1d21SSam Leffler 		frm += 8;
11031a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
11041a1e1d21SSam Leffler 		frm += 2;
11051a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
11061a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_IBSS;
11071a1e1d21SSam Leffler 		else
11081a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_ESS;
11098a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
11101a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
11114f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
11124f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
11134f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
11148a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
11158a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
11161a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
11171a1e1d21SSam Leffler 		frm += 2;
11181a1e1d21SSam Leffler 
11191a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
11201a1e1d21SSam Leffler 				ic->ic_bss->ni_esslen);
11218a1b9b6aSSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
11221a1e1d21SSam Leffler 
1123b83d2112SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_FH) {
1124b83d2112SSam Leffler                         *frm++ = IEEE80211_ELEMID_FHPARMS;
1125b83d2112SSam Leffler                         *frm++ = 5;
1126b83d2112SSam Leffler                         *frm++ = ni->ni_fhdwell & 0x00ff;
1127b83d2112SSam Leffler                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1128b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANSET(
1129b83d2112SSam Leffler 			    ieee80211_chan2ieee(ic, ni->ni_chan));
1130b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANPAT(
1131b83d2112SSam Leffler 			    ieee80211_chan2ieee(ic, ni->ni_chan));
1132b83d2112SSam Leffler                         *frm++ = ni->ni_fhindex;
1133b83d2112SSam Leffler 		} else {
1134b83d2112SSam Leffler 			*frm++ = IEEE80211_ELEMID_DSPARMS;
1135b83d2112SSam Leffler 			*frm++ = 1;
1136b83d2112SSam Leffler 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1137b83d2112SSam Leffler 		}
1138b83d2112SSam Leffler 
11391a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
11401a1e1d21SSam Leffler 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
11411a1e1d21SSam Leffler 			*frm++ = 2;
11421a1e1d21SSam Leffler 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
11431a1e1d21SSam Leffler 		}
11448a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
11458a1b9b6aSSam Leffler 			frm = ieee80211_add_wpa(frm, ic);
11468a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
11478a1b9b6aSSam Leffler 			frm = ieee80211_add_erp(frm, ic);
11488a1b9b6aSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1149d6ec172cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME)
1150d6ec172cSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
11511a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
11521a1e1d21SSam Leffler 		break;
11531a1e1d21SSam Leffler 
11541a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
11558a1b9b6aSSam Leffler 		status = arg >> 16;
11568a1b9b6aSSam Leffler 		arg &= 0xffff;
11578a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
11588a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
11598a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
11608a1b9b6aSSam Leffler 
11618a1b9b6aSSam Leffler 		/*
11628a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
11638a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
11648a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
11658a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
11668a1b9b6aSSam Leffler 		 * request and configured to use shared key.
11678a1b9b6aSSam Leffler 		 */
11688a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
11698a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
11708a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
11718a1b9b6aSSam Leffler 		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
11728a1b9b6aSSam Leffler 
11738a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
11748a1b9b6aSSam Leffler 			  3 * sizeof(u_int16_t)
11758a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
11768a1b9b6aSSam Leffler 				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
11778a1b9b6aSSam Leffler 		);
11781a1e1d21SSam Leffler 		if (m == NULL)
11798a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11808a1b9b6aSSam Leffler 
11818a1b9b6aSSam Leffler 		((u_int16_t *)frm)[0] =
11828a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
11838a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
11841a1e1d21SSam Leffler 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
11858a1b9b6aSSam Leffler 		((u_int16_t *)frm)[2] = htole16(status);/* status */
11868a1b9b6aSSam Leffler 
11878a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
11888a1b9b6aSSam Leffler 			((u_int16_t *)frm)[3] =
11898a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
11908a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
11918a1b9b6aSSam Leffler 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
11928a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
11938a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
11948a1b9b6aSSam Leffler 				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
11958a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
11968a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
11978a1b9b6aSSam Leffler 				    "[%s] request encrypt frame (%s)\n",
11988a1b9b6aSSam Leffler 				    ether_sprintf(ni->ni_macaddr), __func__);
11998a1b9b6aSSam Leffler 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
12008a1b9b6aSSam Leffler 			}
12018a1b9b6aSSam Leffler 		} else
12028a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
12038a1b9b6aSSam Leffler 
12048a1b9b6aSSam Leffler 		/* XXX not right for shared key */
12058a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
12068a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
12078a1b9b6aSSam Leffler 		else
12088a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
12098a1b9b6aSSam Leffler 
12101a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
12111a1e1d21SSam Leffler 			timer = IEEE80211_TRANS_WAIT;
12121a1e1d21SSam Leffler 		break;
12131a1e1d21SSam Leffler 
12141a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
12158a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
12168a1b9b6aSSam Leffler 			"[%s] send station deauthenticate (reason %d)\n",
12171a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
12188a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
12191a1e1d21SSam Leffler 		if (m == NULL)
12208a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12218a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
12228a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
12238a1b9b6aSSam Leffler 
12248a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
12258a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
12268a1b9b6aSSam Leffler 
1227e4918ecdSSam Leffler 		ieee80211_node_unauthorize(ni);		/* port closed */
12281a1e1d21SSam Leffler 		break;
12291a1e1d21SSam Leffler 
12301a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
12311a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
12321a1e1d21SSam Leffler 		/*
12331a1e1d21SSam Leffler 		 * asreq frame format
12341a1e1d21SSam Leffler 		 *	[2] capability information
12351a1e1d21SSam Leffler 		 *	[2] listen interval
12361a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
12371a1e1d21SSam Leffler 		 *	[tlv] ssid
12381a1e1d21SSam Leffler 		 *	[tlv] supported rates
12391a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
12408a1b9b6aSSam Leffler 		 *	[tlv] WME
12418a1b9b6aSSam Leffler 		 *	[tlv] user-specified ie's
12421a1e1d21SSam Leffler 		 */
12438a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
12448a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
12451a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
12461a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
12478a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
12481a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
12498a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
12508a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
12518a1b9b6aSSam Leffler 		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
12528a1b9b6aSSam Leffler 		);
12531a1e1d21SSam Leffler 		if (m == NULL)
12548a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12551a1e1d21SSam Leffler 
12561a1e1d21SSam Leffler 		capinfo = 0;
12571a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
12581a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_IBSS;
12591a1e1d21SSam Leffler 		else		/* IEEE80211_M_STA */
12601a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_ESS;
12618a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
12621a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
12634f2e09c4SSam Leffler 		/*
12644f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
12654f2e09c4SSam Leffler 		 *     short premable is set.
12664f2e09c4SSam Leffler 		 */
12674f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
12684f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
12691a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
12708a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
12718a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
12721a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
12731a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
12741a1e1d21SSam Leffler 		frm += 2;
12751a1e1d21SSam Leffler 
12761a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
12771a1e1d21SSam Leffler 		frm += 2;
12781a1e1d21SSam Leffler 
12791a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
12801a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
12811a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
12821a1e1d21SSam Leffler 		}
12831a1e1d21SSam Leffler 
12841a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
12851a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
12861a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
12878a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
12888a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
12898a1b9b6aSSam Leffler 		if (ic->ic_opt_ie != NULL) {
12908a1b9b6aSSam Leffler 			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
12918a1b9b6aSSam Leffler 			frm += ic->ic_opt_ie_len;
12928a1b9b6aSSam Leffler 		}
12931a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
12941a1e1d21SSam Leffler 
12951a1e1d21SSam Leffler 		timer = IEEE80211_TRANS_WAIT;
12961a1e1d21SSam Leffler 		break;
12971a1e1d21SSam Leffler 
12981a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
12991a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
13001a1e1d21SSam Leffler 		/*
13011a1e1d21SSam Leffler 		 * asreq frame format
13021a1e1d21SSam Leffler 		 *	[2] capability information
13031a1e1d21SSam Leffler 		 *	[2] status
13041a1e1d21SSam Leffler 		 *	[2] association ID
13051a1e1d21SSam Leffler 		 *	[tlv] supported rates
13061a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
13078a1b9b6aSSam Leffler 		 *	[tlv] WME (if enabled and STA enabled)
13081a1e1d21SSam Leffler 		 */
13098a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
13108a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
13111a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
13121a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
13131a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
13148a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
13158a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
13168a1b9b6aSSam Leffler 		);
13171a1e1d21SSam Leffler 		if (m == NULL)
13188a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
13191a1e1d21SSam Leffler 
13201a1e1d21SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
13218a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
13221a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
13234f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
13244f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
13254f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
13268a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
13278a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
13281a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
13291a1e1d21SSam Leffler 		frm += 2;
13301a1e1d21SSam Leffler 
13311a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* status */
13321a1e1d21SSam Leffler 		frm += 2;
13331a1e1d21SSam Leffler 
13348a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
13351a1e1d21SSam Leffler 			*(u_int16_t *)frm = htole16(ni->ni_associd);
13368a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
13378a1b9b6aSSam Leffler 		} else
13388a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
13391a1e1d21SSam Leffler 		frm += 2;
13401a1e1d21SSam Leffler 
13411a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
13421a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
13438a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
13448a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
13451a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
13461a1e1d21SSam Leffler 		break;
13471a1e1d21SSam Leffler 
13481a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
13498a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
13508a1b9b6aSSam Leffler 			"[%s] send station disassociate (reason %d)\n",
13511a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
13528a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
13531a1e1d21SSam Leffler 		if (m == NULL)
13548a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
13558a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
13568a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
13578a1b9b6aSSam Leffler 
13588a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
13598a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
13601a1e1d21SSam Leffler 		break;
13611a1e1d21SSam Leffler 
13621a1e1d21SSam Leffler 	default:
13638a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
13648a1b9b6aSSam Leffler 			"[%s] invalid mgmt frame type %u\n",
13658a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr), type);
13661be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
13670a915fadSSam Leffler 		/* NOTREACHED */
13681a1e1d21SSam Leffler 	}
13691a1e1d21SSam Leffler 
13708a1b9b6aSSam Leffler 	ret = ieee80211_mgmt_output(ic, ni, m, type);
13710a915fadSSam Leffler 	if (ret == 0) {
13720a915fadSSam Leffler 		if (timer)
13731a1e1d21SSam Leffler 			ic->ic_mgt_timer = timer;
13740a915fadSSam Leffler 	} else {
13750a915fadSSam Leffler bad:
13768a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
13770a915fadSSam Leffler 	}
13781a1e1d21SSam Leffler 	return ret;
13790a915fadSSam Leffler #undef senderr
13801a1e1d21SSam Leffler }
13818a1b9b6aSSam Leffler 
13828a1b9b6aSSam Leffler /*
13838a1b9b6aSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
13848a1b9b6aSSam Leffler  */
13858a1b9b6aSSam Leffler struct mbuf *
13868a1b9b6aSSam Leffler ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
13878a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo)
13888a1b9b6aSSam Leffler {
13898a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
13908a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
13918a1b9b6aSSam Leffler 	struct mbuf *m;
13928a1b9b6aSSam Leffler 	int pktlen;
13938a1b9b6aSSam Leffler 	u_int8_t *frm, *efrm;
13948a1b9b6aSSam Leffler 	u_int16_t capinfo;
13958a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
13968a1b9b6aSSam Leffler 
13978a1b9b6aSSam Leffler 	/*
13988a1b9b6aSSam Leffler 	 * beacon frame format
13998a1b9b6aSSam Leffler 	 *	[8] time stamp
14008a1b9b6aSSam Leffler 	 *	[2] beacon interval
14018a1b9b6aSSam Leffler 	 *	[2] cabability information
14028a1b9b6aSSam Leffler 	 *	[tlv] ssid
14038a1b9b6aSSam Leffler 	 *	[tlv] supported rates
14048a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
14058a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
14068a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
14078a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
14088a1b9b6aSSam Leffler 	 *	[tlv] WME parameters
14098a1b9b6aSSam Leffler 	 *	[tlv] WPA/RSN parameters
14108a1b9b6aSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
14118a1b9b6aSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
14128a1b9b6aSSam Leffler 	 */
14138a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
14148a1b9b6aSSam Leffler 	pktlen =   8					/* time stamp */
14158a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* beacon interval */
14168a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* capabilities */
14178a1b9b6aSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
14188a1b9b6aSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
14198a1b9b6aSSam Leffler 	         + 2 + 1				/* DS parameters */
14208a1b9b6aSSam Leffler 		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
14218a1b9b6aSSam Leffler 		 + 2 + 1				/* ERP */
14228a1b9b6aSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
14238a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
14248a1b9b6aSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
14258a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
14268a1b9b6aSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
14278a1b9b6aSSam Leffler 		 ;
14288a1b9b6aSSam Leffler 	m = ieee80211_getmgtframe(&frm, pktlen);
14298a1b9b6aSSam Leffler 	if (m == NULL) {
14308a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
14318a1b9b6aSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
14328a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
14338a1b9b6aSSam Leffler 		return NULL;
14348a1b9b6aSSam Leffler 	}
14358a1b9b6aSSam Leffler 
14368a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
14378a1b9b6aSSam Leffler 	frm += 8;
14388a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(ni->ni_intval);
14398a1b9b6aSSam Leffler 	frm += 2;
14408a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
14418a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
14428a1b9b6aSSam Leffler 	else
14438a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
14448a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
14458a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
14468a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
14478a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
14488a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
14498a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
14508a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
14518a1b9b6aSSam Leffler 	bo->bo_caps = (u_int16_t *)frm;
14528a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(capinfo);
14538a1b9b6aSSam Leffler 	frm += 2;
14548a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
14558a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
14568a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
14578a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
14588a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
14598a1b9b6aSSam Leffler 	} else
14608a1b9b6aSSam Leffler 		*frm++ = 0;
14618a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
14628a1b9b6aSSam Leffler 	if (ic->ic_curmode != IEEE80211_MODE_FH) {
14638a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
14648a1b9b6aSSam Leffler 		*frm++ = 1;
14658a1b9b6aSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
14668a1b9b6aSSam Leffler 	}
14678a1b9b6aSSam Leffler 	bo->bo_tim = frm;
14688a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
14698a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
14708a1b9b6aSSam Leffler 		*frm++ = 2;
14718a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
14728a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
14738a1b9b6aSSam Leffler 	} else {
14748a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
14758a1b9b6aSSam Leffler 
14768a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
14778a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
14788a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
14798a1b9b6aSSam Leffler 		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
14808a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
14818a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
14828a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
14838a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
14848a1b9b6aSSam Leffler 	}
14858a1b9b6aSSam Leffler 	bo->bo_trailer = frm;
14868a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
14878a1b9b6aSSam Leffler 		bo->bo_wme = frm;
14888a1b9b6aSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
14895aa17efaSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
14908a1b9b6aSSam Leffler 	}
14918a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA)
14928a1b9b6aSSam Leffler 		frm = ieee80211_add_wpa(frm, ic);
14938a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G)
14948a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
14958a1b9b6aSSam Leffler 	efrm = ieee80211_add_xrates(frm, rs);
14968a1b9b6aSSam Leffler 	bo->bo_trailer_len = efrm - bo->bo_trailer;
14978a1b9b6aSSam Leffler 	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
14988a1b9b6aSSam Leffler 
14998a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
15008a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
15018a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
15028a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
15038a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
15048a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
15058a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
15068a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
15078a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
15088a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
15098a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_seq = 0;
15108a1b9b6aSSam Leffler 
15118a1b9b6aSSam Leffler 	return m;
15128a1b9b6aSSam Leffler }
15138a1b9b6aSSam Leffler 
15148a1b9b6aSSam Leffler /*
15158a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
15168a1b9b6aSSam Leffler  */
15178a1b9b6aSSam Leffler int
15188a1b9b6aSSam Leffler ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
15198a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
15208a1b9b6aSSam Leffler {
15218a1b9b6aSSam Leffler 	int len_changed = 0;
15228a1b9b6aSSam Leffler 	u_int16_t capinfo;
15238a1b9b6aSSam Leffler 
15248a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
15258a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
15268a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
15278a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
15288a1b9b6aSSam Leffler 	else
15298a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
15308a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
15318a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
15328a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
15338a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
15348a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
15358a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
15368a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
15378a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
15388a1b9b6aSSam Leffler 
15398a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
15408a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
15418a1b9b6aSSam Leffler 
15428a1b9b6aSSam Leffler 		/*
15438a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
15448a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
15458a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
15468a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
15478a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
15488a1b9b6aSSam Leffler 		 */
15498a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
15508a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
15518a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
15528a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
15538a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
15548a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
15558a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
15568a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
15578a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
15588a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
15598a1b9b6aSSam Leffler 			} else
15608a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
15618a1b9b6aSSam Leffler 		} else {
15628a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
15638a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
15648a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
15658a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
15668a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
15678a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
15688a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
15698a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
15708a1b9b6aSSam Leffler 			} else
15718a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
15728a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
15738a1b9b6aSSam Leffler 		}
15748a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
15758a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
15768a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
15778a1b9b6aSSam Leffler 		}
15788a1b9b6aSSam Leffler 	}
15798a1b9b6aSSam Leffler 
15808a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
15818a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
15828a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
15838a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
15848a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
15858a1b9b6aSSam Leffler 			/*
15868a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
15878a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
15888a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
15898a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
15908a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
15918a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
15928a1b9b6aSSam Leffler 			 * maximal-size virtual bitmap (based on ic_max_aid).
15938a1b9b6aSSam Leffler 			 */
15948a1b9b6aSSam Leffler 			/*
15958a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
15968a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
15978a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
15988a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
15998a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
16008a1b9b6aSSam Leffler 			 */
16018a1b9b6aSSam Leffler 			if (ic->ic_ps_pending != 0) {
16028a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
16038a1b9b6aSSam Leffler 				for (i = 0; i < ic->ic_tim_len; i++)
16048a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i]) {
16058a1b9b6aSSam Leffler 						timoff = i &~ 1;
16068a1b9b6aSSam Leffler 						break;
16078a1b9b6aSSam Leffler 					}
16088a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
16098a1b9b6aSSam Leffler 				for (i = ic->ic_tim_len-1; i >= timoff; i--)
16108a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i])
16118a1b9b6aSSam Leffler 						break;
16128a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
16138a1b9b6aSSam Leffler 			} else {
16148a1b9b6aSSam Leffler 				timoff = 0;
16158a1b9b6aSSam Leffler 				timlen = 1;
16168a1b9b6aSSam Leffler 			}
16178a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
16188a1b9b6aSSam Leffler 				/* copy up/down trailer */
16198a1b9b6aSSam Leffler 				ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
16208a1b9b6aSSam Leffler 					bo->bo_trailer_len);
16218a1b9b6aSSam Leffler 				bo->bo_trailer = tie->tim_bitmap+timlen;
16228a1b9b6aSSam Leffler 				bo->bo_wme = bo->bo_trailer;
16238a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
16248a1b9b6aSSam Leffler 
16258a1b9b6aSSam Leffler 				/* update information element */
16268a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
16278a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
16288a1b9b6aSSam Leffler 				len_changed = 1;
16298a1b9b6aSSam Leffler 			}
16308a1b9b6aSSam Leffler 			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
16318a1b9b6aSSam Leffler 				bo->bo_tim_len);
16328a1b9b6aSSam Leffler 
16338a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
16348a1b9b6aSSam Leffler 
16358a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
16368a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
16378a1b9b6aSSam Leffler 				__func__, ic->ic_ps_pending, timoff, timlen);
16388a1b9b6aSSam Leffler 		}
16398a1b9b6aSSam Leffler 		/* count down DTIM period */
16408a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
16418a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
16428a1b9b6aSSam Leffler 		else
16438a1b9b6aSSam Leffler 			tie->tim_count--;
16448a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
16458a1b9b6aSSam Leffler 		if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
16468a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
16478a1b9b6aSSam Leffler 		else
16488a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
16498a1b9b6aSSam Leffler 	}
16508a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
16518a1b9b6aSSam Leffler 
16528a1b9b6aSSam Leffler 	return len_changed;
16538a1b9b6aSSam Leffler }
16548a1b9b6aSSam Leffler 
16558a1b9b6aSSam Leffler /*
16568a1b9b6aSSam Leffler  * Save an outbound packet for a node in power-save sleep state.
16578a1b9b6aSSam Leffler  * The new packet is placed on the node's saved queue, and the TIM
16588a1b9b6aSSam Leffler  * is changed, if necessary.
16598a1b9b6aSSam Leffler  */
16608a1b9b6aSSam Leffler void
16618a1b9b6aSSam Leffler ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
16628a1b9b6aSSam Leffler 		  struct mbuf *m)
16638a1b9b6aSSam Leffler {
16648a1b9b6aSSam Leffler 	int qlen, age;
16658a1b9b6aSSam Leffler 
16668a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_LOCK(ni);
16678a1b9b6aSSam Leffler 	if (_IF_QFULL(&ni->ni_savedq)) {
16688a1b9b6aSSam Leffler 		_IF_DROP(&ni->ni_savedq);
16698a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
16708a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
16718a1b9b6aSSam Leffler 			"[%s] pwr save q overflow, drops %d (size %d)\n",
16728a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr),
16738a1b9b6aSSam Leffler 			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
16748a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
16758a1b9b6aSSam Leffler 		if (ieee80211_msg_dumppkts(ic))
16768a1b9b6aSSam Leffler 			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
16778a1b9b6aSSam Leffler #endif
16788a1b9b6aSSam Leffler 		m_freem(m);
16798a1b9b6aSSam Leffler 		return;
16808a1b9b6aSSam Leffler 	}
16818a1b9b6aSSam Leffler 	/*
16828a1b9b6aSSam Leffler 	 * Tag the frame with it's expiry time and insert
16838a1b9b6aSSam Leffler 	 * it in the queue.  The aging interval is 4 times
16848a1b9b6aSSam Leffler 	 * the listen interval specified by the station.
16858a1b9b6aSSam Leffler 	 * Frames that sit around too long are reclaimed
16868a1b9b6aSSam Leffler 	 * using this information.
16878a1b9b6aSSam Leffler 	 */
16888a1b9b6aSSam Leffler 	/* XXX handle overflow? */
1689d365f9c7SSam Leffler 	age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
16908a1b9b6aSSam Leffler 	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
16918a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
16928a1b9b6aSSam Leffler 
16938a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1694d365f9c7SSam Leffler 		"[%s] save frame with age %d, %u now queued\n",
1695d365f9c7SSam Leffler 		ether_sprintf(ni->ni_macaddr), age, qlen);
16968a1b9b6aSSam Leffler 
16978a1b9b6aSSam Leffler 	if (qlen == 1)
1698edfa57d0SSam Leffler 		ic->ic_set_tim(ni, 1);
16998a1b9b6aSSam Leffler }
1700