xref: /freebsd/sys/net80211/ieee80211_output.c (revision 96d8846397806b9fbd4ced09b5dddcc8f4e7373d)
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 /*
810a915fadSSam Leffler  * Send a management frame to the specified node.  The node pointer
820a915fadSSam Leffler  * must have a reference as the pointer will be passed to the driver
830a915fadSSam Leffler  * and potentially held for a long time.  If the frame is successfully
840a915fadSSam Leffler  * dispatched to the driver, then it is responsible for freeing the
850a915fadSSam Leffler  * reference (and potentially free'ing up any associated storage).
860a915fadSSam Leffler  */
870a915fadSSam Leffler static int
888a1b9b6aSSam Leffler ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
891a1e1d21SSam Leffler     struct mbuf *m, int type)
901a1e1d21SSam Leffler {
918a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
921a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
931a1e1d21SSam Leffler 
940a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
951a1e1d21SSam Leffler 
960a915fadSSam Leffler 	/*
970a915fadSSam Leffler 	 * Yech, hack alert!  We want to pass the node down to the
980a915fadSSam Leffler 	 * driver's start routine.  If we don't do so then the start
990a915fadSSam Leffler 	 * routine must immediately look it up again and that can
1000a915fadSSam Leffler 	 * cause a lock order reversal if, for example, this frame
1010a915fadSSam Leffler 	 * is being sent because the station is being timedout and
1020a915fadSSam Leffler 	 * the frame being sent is a DEAUTH message.  We could stick
1030a915fadSSam Leffler 	 * this in an m_tag and tack that on to the mbuf.  However
1040a915fadSSam Leffler 	 * that's rather expensive to do for every frame so instead
1050a915fadSSam Leffler 	 * we stuff it in the rcvif field since outbound frames do
1060a915fadSSam Leffler 	 * not (presently) use this.
1070a915fadSSam Leffler 	 */
1081a1e1d21SSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1091a1e1d21SSam Leffler 	if (m == NULL)
1101a1e1d21SSam Leffler 		return ENOMEM;
1110a915fadSSam Leffler 	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1120a915fadSSam Leffler 	m->m_pkthdr.rcvif = (void *)ni;
1130a915fadSSam Leffler 
1141a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1151a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
1161a1e1d21SSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1171a1e1d21SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
1181a1e1d21SSam Leffler 	*(u_int16_t *)wh->i_seq =
1198a1b9b6aSSam Leffler 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
1208a1b9b6aSSam Leffler 	ni->ni_txseqs[0]++;
1218a1b9b6aSSam Leffler 	/*
1228a1b9b6aSSam Leffler 	 * Hack.  When sending PROBE_REQ frames while scanning we
1238a1b9b6aSSam Leffler 	 * explicitly force a broadcast rather than (as before) clobber
1248a1b9b6aSSam Leffler 	 * ni_macaddr and ni_bssid.  This is stopgap, we need a way
1258a1b9b6aSSam Leffler 	 * to communicate this directly rather than do something
1268a1b9b6aSSam Leffler 	 * implicit based on surrounding state.
1278a1b9b6aSSam Leffler 	 */
1288a1b9b6aSSam Leffler 	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ &&
1298a1b9b6aSSam Leffler 	    (ic->ic_flags & IEEE80211_F_SCAN)) {
1308a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1318a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1328a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
1338a1b9b6aSSam Leffler 	} else {
1341a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1351a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1361a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1371a1e1d21SSam Leffler 	}
1380a915fadSSam Leffler 
1398a1b9b6aSSam Leffler 	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
1408a1b9b6aSSam Leffler 		m->m_flags &= ~M_LINK0;
1418a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1428a1b9b6aSSam Leffler 			"[%s] encrypting frame (%s)\n",
1438a1b9b6aSSam Leffler 			ether_sprintf(wh->i_addr1), __func__);
1448a1b9b6aSSam Leffler 		wh->i_fc[1] |= IEEE80211_FC1_WEP;
1458a1b9b6aSSam Leffler 	}
1468a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
1478a1b9b6aSSam Leffler 	/* avoid printing too many frames */
1488a1b9b6aSSam Leffler 	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
1498a1b9b6aSSam Leffler 	    ieee80211_msg_dumppkts(ic)) {
1508a1b9b6aSSam Leffler 		printf("[%s] send %s on channel %u\n",
1518a1b9b6aSSam Leffler 		    ether_sprintf(wh->i_addr1),
1528a1b9b6aSSam Leffler 		    ieee80211_mgt_subtype_name[
1538a1b9b6aSSam Leffler 			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
1548a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_SHIFT],
1558a1b9b6aSSam Leffler 		    ieee80211_chan2ieee(ic, ni->ni_chan));
1568a1b9b6aSSam Leffler 	}
1578a1b9b6aSSam Leffler #endif
1588a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_mgmt);
1591a1e1d21SSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);
1601a1e1d21SSam Leffler 	ifp->if_timer = 1;
161af5e59bfSRobert Watson 	if_start(ifp);
1621a1e1d21SSam Leffler 	return 0;
1631a1e1d21SSam Leffler }
1641a1e1d21SSam Leffler 
1650a915fadSSam Leffler /*
1668a1b9b6aSSam Leffler  * Send a null data frame to the specified node.
1678a1b9b6aSSam Leffler  */
1688a1b9b6aSSam Leffler int
1698a1b9b6aSSam Leffler ieee80211_send_nulldata(struct ieee80211com *ic, struct ieee80211_node *ni)
1708a1b9b6aSSam Leffler {
1718a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1728a1b9b6aSSam Leffler 	struct mbuf *m;
1738a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
1748a1b9b6aSSam Leffler 
1758a1b9b6aSSam Leffler 	MGETHDR(m, M_NOWAIT, MT_HEADER);
1768a1b9b6aSSam Leffler 	if (m == NULL) {
1778a1b9b6aSSam Leffler 		/* XXX debug msg */
1788a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
1798a1b9b6aSSam Leffler 		return ENOMEM;
1808a1b9b6aSSam Leffler 	}
1818a1b9b6aSSam Leffler 	m->m_pkthdr.rcvif = (void *) ieee80211_ref_node(ni);
1828a1b9b6aSSam Leffler 
1838a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
1848a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
1858a1b9b6aSSam Leffler 		IEEE80211_FC0_SUBTYPE_NODATA;
1868a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
1878a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_seq =
1888a1b9b6aSSam Leffler 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
1898a1b9b6aSSam Leffler 	ni->ni_txseqs[0]++;
1908a1b9b6aSSam Leffler 
1918a1b9b6aSSam Leffler 	/* XXX WDS */
1928a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1938a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1948a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1958a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
1968a1b9b6aSSam Leffler 	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
1978a1b9b6aSSam Leffler 
1988a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
1998a1b9b6aSSam Leffler 
2008a1b9b6aSSam Leffler 	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
2018a1b9b6aSSam Leffler 	if_start(ifp);
2028a1b9b6aSSam Leffler 
2038a1b9b6aSSam Leffler 	return 0;
2048a1b9b6aSSam Leffler }
2058a1b9b6aSSam Leffler 
2068a1b9b6aSSam Leffler /*
2078a1b9b6aSSam Leffler  * Assign priority to a frame based on any vlan tag assigned
2088a1b9b6aSSam Leffler  * to the station and/or any Diffserv setting in an IP header.
2098a1b9b6aSSam Leffler  * Finally, if an ACM policy is setup (in station mode) it's
2108a1b9b6aSSam Leffler  * applied.
2118a1b9b6aSSam Leffler  */
2128a1b9b6aSSam Leffler int
2138a1b9b6aSSam Leffler ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
2148a1b9b6aSSam Leffler {
2158a1b9b6aSSam Leffler 	int v_wme_ac, d_wme_ac, ac;
2168a1b9b6aSSam Leffler #ifdef INET
2178a1b9b6aSSam Leffler 	struct ether_header *eh;
2188a1b9b6aSSam Leffler #endif
2198a1b9b6aSSam Leffler 
2208a1b9b6aSSam Leffler 	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
2218a1b9b6aSSam Leffler 		ac = WME_AC_BE;
2228a1b9b6aSSam Leffler 		goto done;
2238a1b9b6aSSam Leffler 	}
2248a1b9b6aSSam Leffler 
2258a1b9b6aSSam Leffler 	/*
2268a1b9b6aSSam Leffler 	 * If node has a vlan tag then all traffic
2278a1b9b6aSSam Leffler 	 * to it must have a matching tag.
2288a1b9b6aSSam Leffler 	 */
2298a1b9b6aSSam Leffler 	v_wme_ac = 0;
2308a1b9b6aSSam Leffler 	if (ni->ni_vlan != 0) {
2318a1b9b6aSSam Leffler 		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
232bd40429eSSam Leffler 		 if (mtag == NULL) {
2338a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_novlantag);
2348a1b9b6aSSam Leffler 			return 1;
2358a1b9b6aSSam Leffler 		}
2368a1b9b6aSSam Leffler 		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
2378a1b9b6aSSam Leffler 		    EVL_VLANOFTAG(ni->ni_vlan)) {
2388a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
2398a1b9b6aSSam Leffler 			return 1;
2408a1b9b6aSSam Leffler 		}
2418a1b9b6aSSam Leffler 		/* map vlan priority to AC */
2428a1b9b6aSSam Leffler 		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
2438a1b9b6aSSam Leffler 		case 1:
2448a1b9b6aSSam Leffler 		case 2:
2458a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BK;
2468a1b9b6aSSam Leffler 			break;
2478a1b9b6aSSam Leffler 		case 0:
2488a1b9b6aSSam Leffler 		case 3:
2498a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_BE;
2508a1b9b6aSSam Leffler 			break;
2518a1b9b6aSSam Leffler 		case 4:
2528a1b9b6aSSam Leffler 		case 5:
2538a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VI;
2548a1b9b6aSSam Leffler 			break;
2558a1b9b6aSSam Leffler 		case 6:
2568a1b9b6aSSam Leffler 		case 7:
2578a1b9b6aSSam Leffler 			v_wme_ac = WME_AC_VO;
2588a1b9b6aSSam Leffler 			break;
2598a1b9b6aSSam Leffler 		}
2608a1b9b6aSSam Leffler 	}
2618a1b9b6aSSam Leffler 
2628a1b9b6aSSam Leffler #ifdef INET
2638a1b9b6aSSam Leffler 	eh = mtod(m, struct ether_header *);
2648a1b9b6aSSam Leffler 	if (eh->ether_type == htons(ETHERTYPE_IP)) {
2658a1b9b6aSSam Leffler 		const struct ip *ip = (struct ip *)
2668a1b9b6aSSam Leffler 			(mtod(m, u_int8_t *) + sizeof (*eh));
2678a1b9b6aSSam Leffler 		/*
2688a1b9b6aSSam Leffler 		 * IP frame, map the TOS field.
2698a1b9b6aSSam Leffler 		 */
2708a1b9b6aSSam Leffler 		switch (ip->ip_tos) {
2718a1b9b6aSSam Leffler 		case 0x08:
2728a1b9b6aSSam Leffler 		case 0x20:
2738a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BK;	/* background */
2748a1b9b6aSSam Leffler 			break;
2758a1b9b6aSSam Leffler 		case 0x28:
2768a1b9b6aSSam Leffler 		case 0xa0:
2778a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VI;	/* video */
2788a1b9b6aSSam Leffler 			break;
2798a1b9b6aSSam Leffler 		case 0x30:			/* voice */
2808a1b9b6aSSam Leffler 		case 0xe0:
2818a1b9b6aSSam Leffler 		case 0x88:			/* XXX UPSD */
2828a1b9b6aSSam Leffler 		case 0xb8:
2838a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_VO;
2848a1b9b6aSSam Leffler 			break;
2858a1b9b6aSSam Leffler 		default:
2868a1b9b6aSSam Leffler 			d_wme_ac = WME_AC_BE;
2878a1b9b6aSSam Leffler 			break;
2888a1b9b6aSSam Leffler 		}
2898a1b9b6aSSam Leffler 	} else {
2908a1b9b6aSSam Leffler #endif /* INET */
2918a1b9b6aSSam Leffler 		d_wme_ac = WME_AC_BE;
2928a1b9b6aSSam Leffler #ifdef INET
2938a1b9b6aSSam Leffler 	}
2948a1b9b6aSSam Leffler #endif
2958a1b9b6aSSam Leffler 	/*
2968a1b9b6aSSam Leffler 	 * Use highest priority AC.
2978a1b9b6aSSam Leffler 	 */
2988a1b9b6aSSam Leffler 	if (v_wme_ac > d_wme_ac)
2998a1b9b6aSSam Leffler 		ac = v_wme_ac;
3008a1b9b6aSSam Leffler 	else
3018a1b9b6aSSam Leffler 		ac = d_wme_ac;
3028a1b9b6aSSam Leffler 
3038a1b9b6aSSam Leffler 	/*
3048a1b9b6aSSam Leffler 	 * Apply ACM policy.
3058a1b9b6aSSam Leffler 	 */
3068a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
3078a1b9b6aSSam Leffler 		static const int acmap[4] = {
3088a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BE */
3098a1b9b6aSSam Leffler 			WME_AC_BK,	/* WME_AC_BK */
3108a1b9b6aSSam Leffler 			WME_AC_BE,	/* WME_AC_VI */
3118a1b9b6aSSam Leffler 			WME_AC_VI,	/* WME_AC_VO */
3128a1b9b6aSSam Leffler 		};
3138a1b9b6aSSam Leffler 		while (ac != WME_AC_BK &&
3148a1b9b6aSSam Leffler 		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
3158a1b9b6aSSam Leffler 			ac = acmap[ac];
3168a1b9b6aSSam Leffler 	}
3178a1b9b6aSSam Leffler done:
3188a1b9b6aSSam Leffler 	M_WME_SETAC(m, ac);
3198a1b9b6aSSam Leffler 	return 0;
3208a1b9b6aSSam Leffler }
3218a1b9b6aSSam Leffler 
3228a1b9b6aSSam Leffler /*
3235e923d2eSSam Leffler  * Insure there is sufficient contiguous space to encapsulate the
3245e923d2eSSam Leffler  * 802.11 data frame.  If room isn't already there, arrange for it.
3255e923d2eSSam Leffler  * Drivers and cipher modules assume we have done the necessary work
3265e923d2eSSam Leffler  * and fail rudely if they don't find the space they need.
3275e923d2eSSam Leffler  */
3285e923d2eSSam Leffler static struct mbuf *
3295e923d2eSSam Leffler ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
3305e923d2eSSam Leffler 	struct ieee80211_key *key, struct mbuf *m)
3315e923d2eSSam Leffler {
3325e923d2eSSam Leffler #define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
3335e923d2eSSam Leffler 	int needed_space = hdrsize;
3345e923d2eSSam Leffler 
3355e923d2eSSam Leffler 	if (key != NULL) {
3365e923d2eSSam Leffler 		/* XXX belongs in crypto code? */
3375e923d2eSSam Leffler 		needed_space += key->wk_cipher->ic_header;
3385e923d2eSSam Leffler 		/* XXX frags */
3395e923d2eSSam Leffler 	}
3405e923d2eSSam Leffler 	/*
3415e923d2eSSam Leffler 	 * We know we are called just before stripping an Ethernet
3425e923d2eSSam Leffler 	 * header and prepending an LLC header.  This means we know
3435e923d2eSSam Leffler 	 * there will be
3445e923d2eSSam Leffler 	 *	sizeof(struct ether_header) - sizeof(struct llc)
3455e923d2eSSam Leffler 	 * bytes recovered to which we need additional space for the
3465e923d2eSSam Leffler 	 * 802.11 header and any crypto header.
3475e923d2eSSam Leffler 	 */
3485e923d2eSSam Leffler 	/* XXX check trailing space and copy instead? */
3495e923d2eSSam Leffler 	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
3505e923d2eSSam Leffler 		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
3515e923d2eSSam Leffler 		if (n == NULL) {
3525e923d2eSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
3535e923d2eSSam Leffler 			    "%s: cannot expand storage\n", __func__);
3545e923d2eSSam Leffler 			ic->ic_stats.is_tx_nobuf++;
3555e923d2eSSam Leffler 			m_freem(m);
3565e923d2eSSam Leffler 			return NULL;
3575e923d2eSSam Leffler 		}
3585e923d2eSSam Leffler 		KASSERT(needed_space <= MHLEN,
3595e923d2eSSam Leffler 		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
3605e923d2eSSam Leffler 		/*
3615e923d2eSSam Leffler 		 * Setup new mbuf to have leading space to prepend the
3625e923d2eSSam Leffler 		 * 802.11 header and any crypto header bits that are
3635e923d2eSSam Leffler 		 * required (the latter are added when the driver calls
3645e923d2eSSam Leffler 		 * back to ieee80211_crypto_encap to do crypto encapsulation).
3655e923d2eSSam Leffler 		 */
3665e923d2eSSam Leffler 		/* NB: must be first 'cuz it clobbers m_data */
3675e923d2eSSam Leffler 		m_move_pkthdr(n, m);
3685e923d2eSSam Leffler 		n->m_len = 0;			/* NB: m_gethdr does not set */
3695e923d2eSSam Leffler 		n->m_data += needed_space;
3705e923d2eSSam Leffler 		/*
3715e923d2eSSam Leffler 		 * Pull up Ethernet header to create the expected layout.
3725e923d2eSSam Leffler 		 * We could use m_pullup but that's overkill (i.e. we don't
3735e923d2eSSam Leffler 		 * need the actual data) and it cannot fail so do it inline
3745e923d2eSSam Leffler 		 * for speed.
3755e923d2eSSam Leffler 		 */
3765e923d2eSSam Leffler 		/* NB: struct ether_header is known to be contiguous */
3775e923d2eSSam Leffler 		n->m_len += sizeof(struct ether_header);
3785e923d2eSSam Leffler 		m->m_len -= sizeof(struct ether_header);
3795e923d2eSSam Leffler 		m->m_data += sizeof(struct ether_header);
3805e923d2eSSam Leffler 		/*
3815e923d2eSSam Leffler 		 * Replace the head of the chain.
3825e923d2eSSam Leffler 		 */
3835e923d2eSSam Leffler 		n->m_next = m;
3845e923d2eSSam Leffler 		m = n;
3855e923d2eSSam Leffler 	}
3865e923d2eSSam Leffler 	return m;
3875e923d2eSSam Leffler #undef TO_BE_RECLAIMED
3885e923d2eSSam Leffler }
3895e923d2eSSam Leffler 
3905e923d2eSSam Leffler #define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
3915e923d2eSSam Leffler /*
3925e923d2eSSam Leffler  * Return the transmit key to use in sending a unicast frame.
3935e923d2eSSam Leffler  * If a unicast key is set we use that.  When no unicast key is set
3945e923d2eSSam Leffler  * we fall back to the default transmit key.
3958a1b9b6aSSam Leffler  */
3968a1b9b6aSSam Leffler static __inline struct ieee80211_key *
3975e923d2eSSam Leffler ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
3988a1b9b6aSSam Leffler {
3995e923d2eSSam Leffler 	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
4008a1b9b6aSSam Leffler 		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4018a1b9b6aSSam Leffler 		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4028a1b9b6aSSam Leffler 			return NULL;
4038a1b9b6aSSam Leffler 		return &ic->ic_nw_keys[ic->ic_def_txkey];
4048a1b9b6aSSam Leffler 	} else {
4058a1b9b6aSSam Leffler 		return &ni->ni_ucastkey;
4068a1b9b6aSSam Leffler 	}
4075e923d2eSSam Leffler }
4085e923d2eSSam Leffler 
4095e923d2eSSam Leffler /*
4105e923d2eSSam Leffler  * Return the transmit key to use in sending a multicast frame.
4115e923d2eSSam Leffler  * Multicast traffic always uses the group key which is installed as
4125e923d2eSSam Leffler  * the default tx key.
4135e923d2eSSam Leffler  */
4145e923d2eSSam Leffler static __inline struct ieee80211_key *
4155e923d2eSSam Leffler ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
4165e923d2eSSam Leffler {
4175e923d2eSSam Leffler 	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
4185e923d2eSSam Leffler 	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
4195e923d2eSSam Leffler 		return NULL;
4205e923d2eSSam Leffler 	return &ic->ic_nw_keys[ic->ic_def_txkey];
4218a1b9b6aSSam Leffler }
4228a1b9b6aSSam Leffler 
4238a1b9b6aSSam Leffler /*
4248a1b9b6aSSam Leffler  * Encapsulate an outbound data frame.  The mbuf chain is updated.
4258a1b9b6aSSam Leffler  * If an error is encountered NULL is returned.  The caller is required
4268a1b9b6aSSam Leffler  * to provide a node reference and pullup the ethernet header in the
4278a1b9b6aSSam Leffler  * first mbuf.
4280a915fadSSam Leffler  */
4291a1e1d21SSam Leffler struct mbuf *
4308a1b9b6aSSam Leffler ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
4318a1b9b6aSSam Leffler 	struct ieee80211_node *ni)
4321a1e1d21SSam Leffler {
4331a1e1d21SSam Leffler 	struct ether_header eh;
4341a1e1d21SSam Leffler 	struct ieee80211_frame *wh;
4358a1b9b6aSSam Leffler 	struct ieee80211_key *key;
4361a1e1d21SSam Leffler 	struct llc *llc;
4375e923d2eSSam Leffler 	int hdrsize, datalen, addqos;
4381a1e1d21SSam Leffler 
4398a1b9b6aSSam Leffler 	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
4401a1e1d21SSam Leffler 	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
4411a1e1d21SSam Leffler 
4428a1b9b6aSSam Leffler 	/*
4438a1b9b6aSSam Leffler 	 * Insure space for additional headers.  First identify
4448a1b9b6aSSam Leffler 	 * transmit key to use in calculating any buffer adjustments
4458a1b9b6aSSam Leffler 	 * required.  This is also used below to do privacy
4468a1b9b6aSSam Leffler 	 * encapsulation work.  Then calculate the 802.11 header
4478a1b9b6aSSam Leffler 	 * size and any padding required by the driver.
4488a1b9b6aSSam Leffler 	 *
4498a1b9b6aSSam Leffler 	 * Note key may be NULL if we fall back to the default
4508a1b9b6aSSam Leffler 	 * transmit key and that is not set.  In that case the
4518a1b9b6aSSam Leffler 	 * buffer may not be expanded as needed by the cipher
4528a1b9b6aSSam Leffler 	 * routines, but they will/should discard it.
4538a1b9b6aSSam Leffler 	 */
4548a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
4555e923d2eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA ||
4565e923d2eSSam Leffler 		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
4575e923d2eSSam Leffler 			key = ieee80211_crypto_getucastkey(ic, ni);
4585e923d2eSSam Leffler 		else
4595e923d2eSSam Leffler 			key = ieee80211_crypto_getmcastkey(ic, ni);
4608a1b9b6aSSam Leffler 		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
4618a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
4625e923d2eSSam Leffler 			    "[%s] no default transmit key (%s) deftxkey %u\n",
4635e923d2eSSam Leffler 			    ether_sprintf(eh.ether_dhost), __func__,
4645e923d2eSSam Leffler 			    ic->ic_def_txkey);
4655e923d2eSSam Leffler 			ic->ic_stats.is_tx_nodefkey++;
4668a1b9b6aSSam Leffler 		}
4678a1b9b6aSSam Leffler 	} else
4688a1b9b6aSSam Leffler 		key = NULL;
4698a1b9b6aSSam Leffler 	/* XXX 4-address format */
4705e923d2eSSam Leffler 	/*
4715e923d2eSSam Leffler 	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
4725e923d2eSSam Leffler 	 * frames so suppress use.  This may be an issue if other
4735e923d2eSSam Leffler 	 * ap's require all data frames to be QoS-encapsulated
4745e923d2eSSam Leffler 	 * once negotiated in which case we'll need to make this
4755e923d2eSSam Leffler 	 * configurable.
4765e923d2eSSam Leffler 	 */
4775e923d2eSSam Leffler 	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
4785e923d2eSSam Leffler 		 eh.ether_type != htons(ETHERTYPE_PAE);
4795e923d2eSSam Leffler 	if (addqos)
4808a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_qosframe);
4818a1b9b6aSSam Leffler 	else
4828a1b9b6aSSam Leffler 		hdrsize = sizeof(struct ieee80211_frame);
4838a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_DATAPAD)
4848a1b9b6aSSam Leffler 		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
4858a1b9b6aSSam Leffler 	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
4868a1b9b6aSSam Leffler 	if (m == NULL) {
4878a1b9b6aSSam Leffler 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
4880a915fadSSam Leffler 		goto bad;
4890a915fadSSam Leffler 	}
4901a1e1d21SSam Leffler 
4918a1b9b6aSSam Leffler 	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
4921a1e1d21SSam Leffler 	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
4931a1e1d21SSam Leffler 	llc = mtod(m, struct llc *);
4941a1e1d21SSam Leffler 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
4951a1e1d21SSam Leffler 	llc->llc_control = LLC_UI;
4961a1e1d21SSam Leffler 	llc->llc_snap.org_code[0] = 0;
4971a1e1d21SSam Leffler 	llc->llc_snap.org_code[1] = 0;
4981a1e1d21SSam Leffler 	llc->llc_snap.org_code[2] = 0;
4991a1e1d21SSam Leffler 	llc->llc_snap.ether_type = eh.ether_type;
5008a1b9b6aSSam Leffler 	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
5018a1b9b6aSSam Leffler 
5028a1b9b6aSSam Leffler 	M_PREPEND(m, hdrsize, M_DONTWAIT);
5031be50176SSam Leffler 	if (m == NULL) {
5048a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
5050a915fadSSam Leffler 		goto bad;
5061be50176SSam Leffler 	}
5071a1e1d21SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
5081a1e1d21SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
5091a1e1d21SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
5101a1e1d21SSam Leffler 	switch (ic->ic_opmode) {
5111a1e1d21SSam Leffler 	case IEEE80211_M_STA:
5121a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
5131a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
5141a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
5151a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
5161a1e1d21SSam Leffler 		break;
5171a1e1d21SSam Leffler 	case IEEE80211_M_IBSS:
5181a1e1d21SSam Leffler 	case IEEE80211_M_AHDEMO:
5191a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
5201a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5211a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
522a8b16e87SSam Leffler 		/*
523a8b16e87SSam Leffler 		 * NB: always use the bssid from ic_bss as the
524a8b16e87SSam Leffler 		 *     neighbor's may be stale after an ibss merge
525a8b16e87SSam Leffler 		 */
526a8b16e87SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
5271a1e1d21SSam Leffler 		break;
5281a1e1d21SSam Leffler 	case IEEE80211_M_HOSTAP:
5291a1e1d21SSam Leffler 		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
5301a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
5311a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
5321a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
5331a1e1d21SSam Leffler 		break;
5342bbe529dSSam Leffler 	case IEEE80211_M_MONITOR:
5350a915fadSSam Leffler 		goto bad;
5361a1e1d21SSam Leffler 	}
5375e923d2eSSam Leffler 	if (addqos) {
5388a1b9b6aSSam Leffler 		struct ieee80211_qosframe *qwh =
5398a1b9b6aSSam Leffler 			(struct ieee80211_qosframe *) wh;
5408a1b9b6aSSam Leffler 		int ac, tid;
5418a1b9b6aSSam Leffler 
5428a1b9b6aSSam Leffler 		ac = M_WME_GETAC(m);
5438a1b9b6aSSam Leffler 		/* map from access class/queue to 11e header priorty value */
5448a1b9b6aSSam Leffler 		tid = WME_AC_TO_TID(ac);
5458a1b9b6aSSam Leffler 		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
5468a1b9b6aSSam Leffler 		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
5478a1b9b6aSSam Leffler 			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
5488a1b9b6aSSam Leffler 		qwh->i_qos[1] = 0;
5498a1b9b6aSSam Leffler 		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
5508a1b9b6aSSam Leffler 
5518a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
5528a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
5538a1b9b6aSSam Leffler 		ni->ni_txseqs[tid]++;
5548a1b9b6aSSam Leffler 	} else {
5558a1b9b6aSSam Leffler 		*(u_int16_t *)wh->i_seq =
5568a1b9b6aSSam Leffler 		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
5578a1b9b6aSSam Leffler 		ni->ni_txseqs[0]++;
5588a1b9b6aSSam Leffler 	}
5595e923d2eSSam Leffler 	if (key != NULL) {
5605e923d2eSSam Leffler 		/*
5615e923d2eSSam Leffler 		 * IEEE 802.1X: send EAPOL frames always in the clear.
5625e923d2eSSam Leffler 		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
5635e923d2eSSam Leffler 		 */
5645e923d2eSSam Leffler 		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
5655e923d2eSSam Leffler 		    ((ic->ic_flags & IEEE80211_F_WPA) &&
566b4b64678SSam Leffler 		     (ic->ic_opmode == IEEE80211_M_STA ?
567b4b64678SSam Leffler 		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
5685e923d2eSSam Leffler 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
5695e923d2eSSam Leffler 			/* XXX do fragmentation */
57096d88463SSam Leffler 			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
5715e923d2eSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
5725e923d2eSSam Leffler 				    "[%s] enmic failed, discard frame\n",
5735e923d2eSSam Leffler 				    ether_sprintf(eh.ether_dhost));
5745e923d2eSSam Leffler 				ic->ic_stats.is_crypto_enmicfail++;
5755e923d2eSSam Leffler 				goto bad;
5765e923d2eSSam Leffler 			}
5775e923d2eSSam Leffler 		}
5785e923d2eSSam Leffler 	}
5798a1b9b6aSSam Leffler 
5808a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT(ni, tx_data);
5818a1b9b6aSSam Leffler 	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
5828a1b9b6aSSam Leffler 
5831a1e1d21SSam Leffler 	return m;
5840a915fadSSam Leffler bad:
5850a915fadSSam Leffler 	if (m != NULL)
5860a915fadSSam Leffler 		m_freem(m);
5870a915fadSSam Leffler 	return NULL;
5881a1e1d21SSam Leffler }
5891a1e1d21SSam Leffler 
5901a1e1d21SSam Leffler /*
5911a1e1d21SSam Leffler  * Add a supported rates element id to a frame.
5921a1e1d21SSam Leffler  */
5938a1b9b6aSSam Leffler static u_int8_t *
5941a1e1d21SSam Leffler ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
5951a1e1d21SSam Leffler {
5961a1e1d21SSam Leffler 	int nrates;
5971a1e1d21SSam Leffler 
5981a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_RATES;
5991a1e1d21SSam Leffler 	nrates = rs->rs_nrates;
6001a1e1d21SSam Leffler 	if (nrates > IEEE80211_RATE_SIZE)
6011a1e1d21SSam Leffler 		nrates = IEEE80211_RATE_SIZE;
6021a1e1d21SSam Leffler 	*frm++ = nrates;
6031a1e1d21SSam Leffler 	memcpy(frm, rs->rs_rates, nrates);
6041a1e1d21SSam Leffler 	return frm + nrates;
6051a1e1d21SSam Leffler }
6061a1e1d21SSam Leffler 
6071a1e1d21SSam Leffler /*
6081a1e1d21SSam Leffler  * Add an extended supported rates element id to a frame.
6091a1e1d21SSam Leffler  */
6108a1b9b6aSSam Leffler static u_int8_t *
6111a1e1d21SSam Leffler ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
6121a1e1d21SSam Leffler {
6131a1e1d21SSam Leffler 	/*
6141a1e1d21SSam Leffler 	 * Add an extended supported rates element if operating in 11g mode.
6151a1e1d21SSam Leffler 	 */
6161a1e1d21SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
6171a1e1d21SSam Leffler 		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
6181a1e1d21SSam Leffler 		*frm++ = IEEE80211_ELEMID_XRATES;
6191a1e1d21SSam Leffler 		*frm++ = nrates;
6201a1e1d21SSam Leffler 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
6211a1e1d21SSam Leffler 		frm += nrates;
6221a1e1d21SSam Leffler 	}
6231a1e1d21SSam Leffler 	return frm;
6241a1e1d21SSam Leffler }
6251a1e1d21SSam Leffler 
6261a1e1d21SSam Leffler /*
6271a1e1d21SSam Leffler  * Add an ssid elemet to a frame.
6281a1e1d21SSam Leffler  */
6291a1e1d21SSam Leffler static u_int8_t *
6301a1e1d21SSam Leffler ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
6311a1e1d21SSam Leffler {
6321a1e1d21SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
6331a1e1d21SSam Leffler 	*frm++ = len;
6341a1e1d21SSam Leffler 	memcpy(frm, ssid, len);
6351a1e1d21SSam Leffler 	return frm + len;
6361a1e1d21SSam Leffler }
6371a1e1d21SSam Leffler 
6388a1b9b6aSSam Leffler /*
6398a1b9b6aSSam Leffler  * Add an erp element to a frame.
6408a1b9b6aSSam Leffler  */
6418a1b9b6aSSam Leffler static u_int8_t *
6428a1b9b6aSSam Leffler ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
6431a1e1d21SSam Leffler {
6448a1b9b6aSSam Leffler 	u_int8_t erp;
6451a1e1d21SSam Leffler 
6468a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_ERP;
6478a1b9b6aSSam Leffler 	*frm++ = 1;
6488a1b9b6aSSam Leffler 	erp = 0;
6498a1b9b6aSSam Leffler 	if (ic->ic_nonerpsta != 0)
6508a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
6518a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEPROT)
6528a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_USE_PROTECTION;
6538a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_USEBARKER)
6548a1b9b6aSSam Leffler 		erp |= IEEE80211_ERP_LONG_PREAMBLE;
6558a1b9b6aSSam Leffler 	*frm++ = erp;
6568a1b9b6aSSam Leffler 	return frm;
6571a1e1d21SSam Leffler }
6581a1e1d21SSam Leffler 
6598a1b9b6aSSam Leffler static u_int8_t *
6608a1b9b6aSSam Leffler ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
6618a1b9b6aSSam Leffler {
6628a1b9b6aSSam Leffler #define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
6638a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
6648a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
6658a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
6668a1b9b6aSSam Leffler 	frm += 2;				\
6678a1b9b6aSSam Leffler } while (0)
6688a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
6698a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
6708a1b9b6aSSam Leffler 	frm += 4;				\
6718a1b9b6aSSam Leffler } while (0)
6728a1b9b6aSSam Leffler 	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
6738a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
6748a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
6758a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
6768a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
6778a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
6788a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
6798a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_NULL },
6808a1b9b6aSSam Leffler 	};
6818a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
6828a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
6838a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
6848a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
6858a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
6868a1b9b6aSSam Leffler 		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
6878a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
6888a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
6898a1b9b6aSSam Leffler 	u_int8_t *selcnt;
6908a1b9b6aSSam Leffler 
6918a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_VENDOR;
6928a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
6938a1b9b6aSSam Leffler 	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
6948a1b9b6aSSam Leffler 	frm += sizeof(oui);
6958a1b9b6aSSam Leffler 	ADDSHORT(frm, WPA_VERSION);
6968a1b9b6aSSam Leffler 
6978a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
6988a1b9b6aSSam Leffler 
6998a1b9b6aSSam Leffler 	/* multicast cipher */
7008a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
7018a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
7028a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
7038a1b9b6aSSam Leffler 	else
7048a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
7058a1b9b6aSSam Leffler 
7068a1b9b6aSSam Leffler 	/* unicast cipher list */
7078a1b9b6aSSam Leffler 	selcnt = frm;
7088a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7098a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
7108a1b9b6aSSam Leffler 		selcnt[0]++;
7118a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
7128a1b9b6aSSam Leffler 	}
7138a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
7148a1b9b6aSSam Leffler 		selcnt[0]++;
7158a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
7168a1b9b6aSSam Leffler 	}
7178a1b9b6aSSam Leffler 
7188a1b9b6aSSam Leffler 	/* authenticator selector list */
7198a1b9b6aSSam Leffler 	selcnt = frm;
7208a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7218a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
7228a1b9b6aSSam Leffler 		selcnt[0]++;
7238a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
7248a1b9b6aSSam Leffler 	}
7258a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
7268a1b9b6aSSam Leffler 		selcnt[0]++;
7278a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
7288a1b9b6aSSam Leffler 	}
7298a1b9b6aSSam Leffler 
7308a1b9b6aSSam Leffler 	/* optional capabilities */
7318a1b9b6aSSam Leffler 	if (rsn->rsn_caps != 0)
7328a1b9b6aSSam Leffler 		ADDSHORT(frm, rsn->rsn_caps);
7338a1b9b6aSSam Leffler 
7348a1b9b6aSSam Leffler 	/* calculate element length */
7358a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
7368a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
73716574882SSam Leffler 		("WPA IE too big, %u > %zu",
7388a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
7398a1b9b6aSSam Leffler 	return frm;
7408a1b9b6aSSam Leffler #undef ADDSHORT
7418a1b9b6aSSam Leffler #undef ADDSELECTOR
7428a1b9b6aSSam Leffler #undef WPA_OUI_BYTES
7438a1b9b6aSSam Leffler }
7448a1b9b6aSSam Leffler 
7458a1b9b6aSSam Leffler static u_int8_t *
7468a1b9b6aSSam Leffler ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
7478a1b9b6aSSam Leffler {
7488a1b9b6aSSam Leffler #define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
7498a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
7508a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
7518a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
7528a1b9b6aSSam Leffler 	frm += 2;				\
7538a1b9b6aSSam Leffler } while (0)
7548a1b9b6aSSam Leffler #define	ADDSELECTOR(frm, sel) do {		\
7558a1b9b6aSSam Leffler 	memcpy(frm, sel, 4);			\
7568a1b9b6aSSam Leffler 	frm += 4;				\
7578a1b9b6aSSam Leffler } while (0)
7588a1b9b6aSSam Leffler 	static const u_int8_t cipher_suite[][4] = {
7598a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
7608a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
7618a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
7628a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
7638a1b9b6aSSam Leffler 		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
7648a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_NULL },
7658a1b9b6aSSam Leffler 	};
7668a1b9b6aSSam Leffler 	static const u_int8_t wep104_suite[4] =
7678a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
7688a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_unspec[4] =
7698a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
7708a1b9b6aSSam Leffler 	static const u_int8_t key_mgt_psk[4] =
7718a1b9b6aSSam Leffler 		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
7728a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
7738a1b9b6aSSam Leffler 	u_int8_t *frm = ie;
7748a1b9b6aSSam Leffler 	u_int8_t *selcnt;
7758a1b9b6aSSam Leffler 
7768a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_RSN;
7778a1b9b6aSSam Leffler 	*frm++ = 0;				/* length filled in below */
7788a1b9b6aSSam Leffler 	ADDSHORT(frm, RSN_VERSION);
7798a1b9b6aSSam Leffler 
7808a1b9b6aSSam Leffler 	/* XXX filter out CKIP */
7818a1b9b6aSSam Leffler 
7828a1b9b6aSSam Leffler 	/* multicast cipher */
7838a1b9b6aSSam Leffler 	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
7848a1b9b6aSSam Leffler 	    rsn->rsn_mcastkeylen >= 13)
7858a1b9b6aSSam Leffler 		ADDSELECTOR(frm, wep104_suite);
7868a1b9b6aSSam Leffler 	else
7878a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
7888a1b9b6aSSam Leffler 
7898a1b9b6aSSam Leffler 	/* unicast cipher list */
7908a1b9b6aSSam Leffler 	selcnt = frm;
7918a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
7928a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
7938a1b9b6aSSam Leffler 		selcnt[0]++;
7948a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
7958a1b9b6aSSam Leffler 	}
7968a1b9b6aSSam Leffler 	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
7978a1b9b6aSSam Leffler 		selcnt[0]++;
7988a1b9b6aSSam Leffler 		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
7998a1b9b6aSSam Leffler 	}
8008a1b9b6aSSam Leffler 
8018a1b9b6aSSam Leffler 	/* authenticator selector list */
8028a1b9b6aSSam Leffler 	selcnt = frm;
8038a1b9b6aSSam Leffler 	ADDSHORT(frm, 0);			/* selector count */
8048a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
8058a1b9b6aSSam Leffler 		selcnt[0]++;
8068a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_unspec);
8078a1b9b6aSSam Leffler 	}
8088a1b9b6aSSam Leffler 	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
8098a1b9b6aSSam Leffler 		selcnt[0]++;
8108a1b9b6aSSam Leffler 		ADDSELECTOR(frm, key_mgt_psk);
8118a1b9b6aSSam Leffler 	}
8128a1b9b6aSSam Leffler 
8138a1b9b6aSSam Leffler 	/* optional capabilities */
8148a1b9b6aSSam Leffler 	if (rsn->rsn_caps != 0)
8158a1b9b6aSSam Leffler 		ADDSHORT(frm, rsn->rsn_caps);
8168a1b9b6aSSam Leffler 	/* XXX PMKID */
8178a1b9b6aSSam Leffler 
8188a1b9b6aSSam Leffler 	/* calculate element length */
8198a1b9b6aSSam Leffler 	ie[1] = frm - ie - 2;
8208a1b9b6aSSam Leffler 	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
82116574882SSam Leffler 		("RSN IE too big, %u > %zu",
8228a1b9b6aSSam Leffler 		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
8238a1b9b6aSSam Leffler 	return frm;
8248a1b9b6aSSam Leffler #undef ADDSELECTOR
8258a1b9b6aSSam Leffler #undef ADDSHORT
8268a1b9b6aSSam Leffler #undef RSN_OUI_BYTES
8278a1b9b6aSSam Leffler }
8288a1b9b6aSSam Leffler 
8298a1b9b6aSSam Leffler /*
8308a1b9b6aSSam Leffler  * Add a WPA/RSN element to a frame.
8318a1b9b6aSSam Leffler  */
8328a1b9b6aSSam Leffler static u_int8_t *
8338a1b9b6aSSam Leffler ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
8348a1b9b6aSSam Leffler {
8358a1b9b6aSSam Leffler 
8368a1b9b6aSSam Leffler 	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
8378a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA2)
8388a1b9b6aSSam Leffler 		frm = ieee80211_setup_rsn_ie(ic, frm);
8398a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA1)
8408a1b9b6aSSam Leffler 		frm = ieee80211_setup_wpa_ie(ic, frm);
8418a1b9b6aSSam Leffler 	return frm;
8428a1b9b6aSSam Leffler }
8438a1b9b6aSSam Leffler 
8448a1b9b6aSSam Leffler #define	WME_OUI_BYTES		0x00, 0x50, 0xf2
8458a1b9b6aSSam Leffler /*
8468a1b9b6aSSam Leffler  * Add a WME information element to a frame.
8478a1b9b6aSSam Leffler  */
8488a1b9b6aSSam Leffler static u_int8_t *
8498a1b9b6aSSam Leffler ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
8508a1b9b6aSSam Leffler {
8518a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info info = {
8528a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
8538a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
8548a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
8558a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
8568a1b9b6aSSam Leffler 		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
8578a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
8588a1b9b6aSSam Leffler 		.wme_info	= 0,
8598a1b9b6aSSam Leffler 	};
8608a1b9b6aSSam Leffler 	memcpy(frm, &info, sizeof(info));
8618a1b9b6aSSam Leffler 	return frm + sizeof(info);
8628a1b9b6aSSam Leffler }
8638a1b9b6aSSam Leffler 
8648a1b9b6aSSam Leffler /*
8658a1b9b6aSSam Leffler  * Add a WME parameters element to a frame.
8668a1b9b6aSSam Leffler  */
8678a1b9b6aSSam Leffler static u_int8_t *
8688a1b9b6aSSam Leffler ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
8698a1b9b6aSSam Leffler {
8708a1b9b6aSSam Leffler #define	SM(_v, _f)	(((_v) << _f##_S) & _f)
8718a1b9b6aSSam Leffler #define	ADDSHORT(frm, v) do {			\
8728a1b9b6aSSam Leffler 	frm[0] = (v) & 0xff;			\
8738a1b9b6aSSam Leffler 	frm[1] = (v) >> 8;			\
8748a1b9b6aSSam Leffler 	frm += 2;				\
8758a1b9b6aSSam Leffler } while (0)
8768a1b9b6aSSam Leffler 	/* NB: this works 'cuz a param has an info at the front */
8778a1b9b6aSSam Leffler 	static const struct ieee80211_wme_info param = {
8788a1b9b6aSSam Leffler 		.wme_id		= IEEE80211_ELEMID_VENDOR,
8798a1b9b6aSSam Leffler 		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
8808a1b9b6aSSam Leffler 		.wme_oui	= { WME_OUI_BYTES },
8818a1b9b6aSSam Leffler 		.wme_type	= WME_OUI_TYPE,
8828a1b9b6aSSam Leffler 		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
8838a1b9b6aSSam Leffler 		.wme_version	= WME_VERSION,
8848a1b9b6aSSam Leffler 	};
8858a1b9b6aSSam Leffler 	int i;
8868a1b9b6aSSam Leffler 
8878a1b9b6aSSam Leffler 	memcpy(frm, &param, sizeof(param));
8888a1b9b6aSSam Leffler 	frm += __offsetof(struct ieee80211_wme_info, wme_info);
8898a1b9b6aSSam Leffler 	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
8908a1b9b6aSSam Leffler 	*frm++ = 0;					/* reserved field */
8918a1b9b6aSSam Leffler 	for (i = 0; i < WME_NUM_AC; i++) {
8928a1b9b6aSSam Leffler 		const struct wmeParams *ac =
8938a1b9b6aSSam Leffler 		       &wme->wme_bssChanParams.cap_wmeParams[i];
8948a1b9b6aSSam Leffler 		*frm++ = SM(i, WME_PARAM_ACI)
8958a1b9b6aSSam Leffler 		       | SM(ac->wmep_acm, WME_PARAM_ACM)
8968a1b9b6aSSam Leffler 		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
8978a1b9b6aSSam Leffler 		       ;
8988a1b9b6aSSam Leffler 		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
8998a1b9b6aSSam Leffler 		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
9008a1b9b6aSSam Leffler 		       ;
9018a1b9b6aSSam Leffler 		ADDSHORT(frm, ac->wmep_txopLimit);
9028a1b9b6aSSam Leffler 	}
9038a1b9b6aSSam Leffler 	return frm;
9048a1b9b6aSSam Leffler #undef SM
9058a1b9b6aSSam Leffler #undef ADDSHORT
9068a1b9b6aSSam Leffler }
9078a1b9b6aSSam Leffler #undef WME_OUI_BYTES
9088a1b9b6aSSam Leffler 
9090a915fadSSam Leffler /*
9100a915fadSSam Leffler  * Send a management frame.  The node is for the destination (or ic_bss
9110a915fadSSam Leffler  * when in station mode).  Nodes other than ic_bss have their reference
9120a915fadSSam Leffler  * count bumped to reflect our use for an indeterminant time.
9130a915fadSSam Leffler  */
9141a1e1d21SSam Leffler int
9151a1e1d21SSam Leffler ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
9161a1e1d21SSam Leffler 	int type, int arg)
9171a1e1d21SSam Leffler {
9181be50176SSam Leffler #define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
9191a1e1d21SSam Leffler 	struct mbuf *m;
9201a1e1d21SSam Leffler 	u_int8_t *frm;
9211a1e1d21SSam Leffler 	enum ieee80211_phymode mode;
9221a1e1d21SSam Leffler 	u_int16_t capinfo;
9238a1b9b6aSSam Leffler 	int has_challenge, is_shared_key, ret, timer, status;
9241a1e1d21SSam Leffler 
9250a915fadSSam Leffler 	KASSERT(ni != NULL, ("null node"));
9260a915fadSSam Leffler 
9270a915fadSSam Leffler 	/*
9280a915fadSSam Leffler 	 * Hold a reference on the node so it doesn't go away until after
9290a915fadSSam Leffler 	 * the xmit is complete all the way in the driver.  On error we
9300a915fadSSam Leffler 	 * will remove our reference.
9310a915fadSSam Leffler 	 */
9328a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
93349a15236SSam Leffler 		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
9348a1b9b6aSSam Leffler 		__func__, __LINE__,
93549a15236SSam Leffler 		ni, ether_sprintf(ni->ni_macaddr),
9368a1b9b6aSSam Leffler 		ieee80211_node_refcnt(ni)+1);
9370a915fadSSam Leffler 	ieee80211_ref_node(ni);
9388a1b9b6aSSam Leffler 
9391a1e1d21SSam Leffler 	timer = 0;
9401a1e1d21SSam Leffler 	switch (type) {
9411a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
9421a1e1d21SSam Leffler 		/*
9431a1e1d21SSam Leffler 		 * prreq frame format
9441a1e1d21SSam Leffler 		 *	[tlv] ssid
9451a1e1d21SSam Leffler 		 *	[tlv] supported rates
9461a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
9478a1b9b6aSSam Leffler 		 *	[tlv] user-specified ie's
9481a1e1d21SSam Leffler 		 */
9498a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
9508a1b9b6aSSam Leffler 			 2 + IEEE80211_NWID_LEN
9511a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
9528a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
9538a1b9b6aSSam Leffler 		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
9548a1b9b6aSSam Leffler 		);
9551a1e1d21SSam Leffler 		if (m == NULL)
9568a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
9578a1b9b6aSSam Leffler 
9581a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
9591a1e1d21SSam Leffler 		mode = ieee80211_chan2mode(ic, ni->ni_chan);
9601a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
9611a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
9628a1b9b6aSSam Leffler 		if (ic->ic_opt_ie != NULL) {
9638a1b9b6aSSam Leffler 			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
9648a1b9b6aSSam Leffler 			frm += ic->ic_opt_ie_len;
9658a1b9b6aSSam Leffler 		}
9661a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
9671a1e1d21SSam Leffler 
9688a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_probereq);
9698a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
9701a1e1d21SSam Leffler 			timer = IEEE80211_TRANS_WAIT;
9711a1e1d21SSam Leffler 		break;
9721a1e1d21SSam Leffler 
9731a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
9741a1e1d21SSam Leffler 		/*
9751a1e1d21SSam Leffler 		 * probe response frame format
9761a1e1d21SSam Leffler 		 *	[8] time stamp
9771a1e1d21SSam Leffler 		 *	[2] beacon interval
9781a1e1d21SSam Leffler 		 *	[2] cabability information
9791a1e1d21SSam Leffler 		 *	[tlv] ssid
9801a1e1d21SSam Leffler 		 *	[tlv] supported rates
981b83d2112SSam Leffler 		 *	[tlv] parameter set (FH/DS)
9821a1e1d21SSam Leffler 		 *	[tlv] parameter set (IBSS)
9838a1b9b6aSSam Leffler 		 *	[tlv] extended rate phy (ERP)
9841a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
9858a1b9b6aSSam Leffler 		 *	[tlv] WPA
986d6ec172cSSam Leffler 		 *	[tlv] WME (optional)
9871a1e1d21SSam Leffler 		 */
9888a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
9898a1b9b6aSSam Leffler 			 8
9908a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
9918a1b9b6aSSam Leffler 		       + sizeof(u_int16_t)
9928a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
9931a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
9948a1b9b6aSSam Leffler 		       + 7	/* max(7,3) */
9951a1e1d21SSam Leffler 		       + 6
9968a1b9b6aSSam Leffler 		       + 3
9978a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
9988a1b9b6aSSam Leffler 		       /* XXX !WPA1+WPA2 fits w/o a cluster */
9998a1b9b6aSSam Leffler 		       + (ic->ic_flags & IEEE80211_F_WPA ?
10008a1b9b6aSSam Leffler 				2*sizeof(struct ieee80211_ie_wpa) : 0)
1001d6ec172cSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
10028a1b9b6aSSam Leffler 		);
10031a1e1d21SSam Leffler 		if (m == NULL)
10048a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
10051a1e1d21SSam Leffler 
10061a1e1d21SSam Leffler 		memset(frm, 0, 8);	/* timestamp should be filled later */
10071a1e1d21SSam Leffler 		frm += 8;
10081a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
10091a1e1d21SSam Leffler 		frm += 2;
10101a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
10111a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_IBSS;
10121a1e1d21SSam Leffler 		else
10131a1e1d21SSam Leffler 			capinfo = IEEE80211_CAPINFO_ESS;
10148a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
10151a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
10164f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
10174f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
10184f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
10198a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
10208a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
10211a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
10221a1e1d21SSam Leffler 		frm += 2;
10231a1e1d21SSam Leffler 
10241a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
10251a1e1d21SSam Leffler 				ic->ic_bss->ni_esslen);
10268a1b9b6aSSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
10271a1e1d21SSam Leffler 
1028b83d2112SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_FH) {
1029b83d2112SSam Leffler                         *frm++ = IEEE80211_ELEMID_FHPARMS;
1030b83d2112SSam Leffler                         *frm++ = 5;
1031b83d2112SSam Leffler                         *frm++ = ni->ni_fhdwell & 0x00ff;
1032b83d2112SSam Leffler                         *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1033b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANSET(
1034b83d2112SSam Leffler 			    ieee80211_chan2ieee(ic, ni->ni_chan));
1035b83d2112SSam Leffler                         *frm++ = IEEE80211_FH_CHANPAT(
1036b83d2112SSam Leffler 			    ieee80211_chan2ieee(ic, ni->ni_chan));
1037b83d2112SSam Leffler                         *frm++ = ni->ni_fhindex;
1038b83d2112SSam Leffler 		} else {
1039b83d2112SSam Leffler 			*frm++ = IEEE80211_ELEMID_DSPARMS;
1040b83d2112SSam Leffler 			*frm++ = 1;
1041b83d2112SSam Leffler 			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1042b83d2112SSam Leffler 		}
1043b83d2112SSam Leffler 
10441a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
10451a1e1d21SSam Leffler 			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
10461a1e1d21SSam Leffler 			*frm++ = 2;
10471a1e1d21SSam Leffler 			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
10481a1e1d21SSam Leffler 		}
10498a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
10508a1b9b6aSSam Leffler 			frm = ieee80211_add_wpa(frm, ic);
10518a1b9b6aSSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
10528a1b9b6aSSam Leffler 			frm = ieee80211_add_erp(frm, ic);
10538a1b9b6aSSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1054d6ec172cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME)
1055d6ec172cSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
10561a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
10571a1e1d21SSam Leffler 		break;
10581a1e1d21SSam Leffler 
10591a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_AUTH:
10608a1b9b6aSSam Leffler 		status = arg >> 16;
10618a1b9b6aSSam Leffler 		arg &= 0xffff;
10628a1b9b6aSSam Leffler 		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
10638a1b9b6aSSam Leffler 		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
10648a1b9b6aSSam Leffler 		    ni->ni_challenge != NULL);
10658a1b9b6aSSam Leffler 
10668a1b9b6aSSam Leffler 		/*
10678a1b9b6aSSam Leffler 		 * Deduce whether we're doing open authentication or
10688a1b9b6aSSam Leffler 		 * shared key authentication.  We do the latter if
10698a1b9b6aSSam Leffler 		 * we're in the middle of a shared key authentication
10708a1b9b6aSSam Leffler 		 * handshake or if we're initiating an authentication
10718a1b9b6aSSam Leffler 		 * request and configured to use shared key.
10728a1b9b6aSSam Leffler 		 */
10738a1b9b6aSSam Leffler 		is_shared_key = has_challenge ||
10748a1b9b6aSSam Leffler 		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
10758a1b9b6aSSam Leffler 		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
10768a1b9b6aSSam Leffler 		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
10778a1b9b6aSSam Leffler 
10788a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
10798a1b9b6aSSam Leffler 			  3 * sizeof(u_int16_t)
10808a1b9b6aSSam Leffler 			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
10818a1b9b6aSSam Leffler 				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
10828a1b9b6aSSam Leffler 		);
10831a1e1d21SSam Leffler 		if (m == NULL)
10848a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
10858a1b9b6aSSam Leffler 
10868a1b9b6aSSam Leffler 		((u_int16_t *)frm)[0] =
10878a1b9b6aSSam Leffler 		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
10888a1b9b6aSSam Leffler 		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
10891a1e1d21SSam Leffler 		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
10908a1b9b6aSSam Leffler 		((u_int16_t *)frm)[2] = htole16(status);/* status */
10918a1b9b6aSSam Leffler 
10928a1b9b6aSSam Leffler 		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
10938a1b9b6aSSam Leffler 			((u_int16_t *)frm)[3] =
10948a1b9b6aSSam Leffler 			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
10958a1b9b6aSSam Leffler 			    IEEE80211_ELEMID_CHALLENGE);
10968a1b9b6aSSam Leffler 			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
10978a1b9b6aSSam Leffler 			    IEEE80211_CHALLENGE_LEN);
10988a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len =
10998a1b9b6aSSam Leffler 				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
11008a1b9b6aSSam Leffler 			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
11018a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
11028a1b9b6aSSam Leffler 				    "[%s] request encrypt frame (%s)\n",
11038a1b9b6aSSam Leffler 				    ether_sprintf(ni->ni_macaddr), __func__);
11048a1b9b6aSSam Leffler 				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
11058a1b9b6aSSam Leffler 			}
11068a1b9b6aSSam Leffler 		} else
11078a1b9b6aSSam Leffler 			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
11088a1b9b6aSSam Leffler 
11098a1b9b6aSSam Leffler 		/* XXX not right for shared key */
11108a1b9b6aSSam Leffler 		if (status == IEEE80211_STATUS_SUCCESS)
11118a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth);
11128a1b9b6aSSam Leffler 		else
11138a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_auth_fail);
11148a1b9b6aSSam Leffler 
11158a1b9b6aSSam Leffler 		/*
11168a1b9b6aSSam Leffler 		 * When 802.1x is not in use mark the port
11178a1b9b6aSSam Leffler 		 * authorized at this point so traffic can flow.
11188a1b9b6aSSam Leffler 		 */
11198a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
11208a1b9b6aSSam Leffler 		    status == IEEE80211_STATUS_SUCCESS &&
11218a1b9b6aSSam Leffler 		    ni->ni_authmode != IEEE80211_AUTH_8021X)
11228a1b9b6aSSam Leffler 			ieee80211_node_authorize(ic, ni);
11231a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
11241a1e1d21SSam Leffler 			timer = IEEE80211_TRANS_WAIT;
11251a1e1d21SSam Leffler 		break;
11261a1e1d21SSam Leffler 
11271a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
11288a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
11298a1b9b6aSSam Leffler 			"[%s] send station deauthenticate (reason %d)\n",
11301a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
11318a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
11321a1e1d21SSam Leffler 		if (m == NULL)
11338a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11348a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
11358a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
11368a1b9b6aSSam Leffler 
11378a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_deauth);
11388a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
11398a1b9b6aSSam Leffler 
11408a1b9b6aSSam Leffler 		ieee80211_node_unauthorize(ic, ni);	/* port closed */
11411a1e1d21SSam Leffler 		break;
11421a1e1d21SSam Leffler 
11431a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
11441a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
11451a1e1d21SSam Leffler 		/*
11461a1e1d21SSam Leffler 		 * asreq frame format
11471a1e1d21SSam Leffler 		 *	[2] capability information
11481a1e1d21SSam Leffler 		 *	[2] listen interval
11491a1e1d21SSam Leffler 		 *	[6*] current AP address (reassoc only)
11501a1e1d21SSam Leffler 		 *	[tlv] ssid
11511a1e1d21SSam Leffler 		 *	[tlv] supported rates
11521a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
11538a1b9b6aSSam Leffler 		 *	[tlv] WME
11548a1b9b6aSSam Leffler 		 *	[tlv] user-specified ie's
11551a1e1d21SSam Leffler 		 */
11568a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
11578a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
11581a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
11591a1e1d21SSam Leffler 		       + IEEE80211_ADDR_LEN
11608a1b9b6aSSam Leffler 		       + 2 + IEEE80211_NWID_LEN
11611a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
11628a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
11638a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_info)
11648a1b9b6aSSam Leffler 		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
11658a1b9b6aSSam Leffler 		);
11661a1e1d21SSam Leffler 		if (m == NULL)
11678a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
11681a1e1d21SSam Leffler 
11691a1e1d21SSam Leffler 		capinfo = 0;
11701a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS)
11711a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_IBSS;
11721a1e1d21SSam Leffler 		else		/* IEEE80211_M_STA */
11731a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_ESS;
11748a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
11751a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
11764f2e09c4SSam Leffler 		/*
11774f2e09c4SSam Leffler 		 * NB: Some 11a AP's reject the request when
11784f2e09c4SSam Leffler 		 *     short premable is set.
11794f2e09c4SSam Leffler 		 */
11804f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
11814f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
11821a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
11838a1b9b6aSSam Leffler 		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
11848a1b9b6aSSam Leffler 		    (ic->ic_caps & IEEE80211_C_SHSLOT))
11851a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
11861a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
11871a1e1d21SSam Leffler 		frm += 2;
11881a1e1d21SSam Leffler 
11891a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(ic->ic_lintval);
11901a1e1d21SSam Leffler 		frm += 2;
11911a1e1d21SSam Leffler 
11921a1e1d21SSam Leffler 		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
11931a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
11941a1e1d21SSam Leffler 			frm += IEEE80211_ADDR_LEN;
11951a1e1d21SSam Leffler 		}
11961a1e1d21SSam Leffler 
11971a1e1d21SSam Leffler 		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
11981a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
11991a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
12008a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
12018a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
12028a1b9b6aSSam Leffler 		if (ic->ic_opt_ie != NULL) {
12038a1b9b6aSSam Leffler 			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
12048a1b9b6aSSam Leffler 			frm += ic->ic_opt_ie_len;
12058a1b9b6aSSam Leffler 		}
12061a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
12071a1e1d21SSam Leffler 
12081a1e1d21SSam Leffler 		timer = IEEE80211_TRANS_WAIT;
12091a1e1d21SSam Leffler 		break;
12101a1e1d21SSam Leffler 
12111a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
12121a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
12131a1e1d21SSam Leffler 		/*
12141a1e1d21SSam Leffler 		 * asreq frame format
12151a1e1d21SSam Leffler 		 *	[2] capability information
12161a1e1d21SSam Leffler 		 *	[2] status
12171a1e1d21SSam Leffler 		 *	[2] association ID
12181a1e1d21SSam Leffler 		 *	[tlv] supported rates
12191a1e1d21SSam Leffler 		 *	[tlv] extended supported rates
12208a1b9b6aSSam Leffler 		 *	[tlv] WME (if enabled and STA enabled)
12211a1e1d21SSam Leffler 		 */
12228a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm,
12238a1b9b6aSSam Leffler 			 sizeof(u_int16_t)
12241a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
12251a1e1d21SSam Leffler 		       + sizeof(u_int16_t)
12261a1e1d21SSam Leffler 		       + 2 + IEEE80211_RATE_SIZE
12278a1b9b6aSSam Leffler 		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
12288a1b9b6aSSam Leffler 		       + sizeof(struct ieee80211_wme_param)
12298a1b9b6aSSam Leffler 		);
12301a1e1d21SSam Leffler 		if (m == NULL)
12318a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12321a1e1d21SSam Leffler 
12331a1e1d21SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
12348a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
12351a1e1d21SSam Leffler 			capinfo |= IEEE80211_CAPINFO_PRIVACY;
12364f2e09c4SSam Leffler 		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
12374f2e09c4SSam Leffler 		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
12384f2e09c4SSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
12398a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
12408a1b9b6aSSam Leffler 			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
12411a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(capinfo);
12421a1e1d21SSam Leffler 		frm += 2;
12431a1e1d21SSam Leffler 
12441a1e1d21SSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* status */
12451a1e1d21SSam Leffler 		frm += 2;
12461a1e1d21SSam Leffler 
12478a1b9b6aSSam Leffler 		if (arg == IEEE80211_STATUS_SUCCESS) {
12481a1e1d21SSam Leffler 			*(u_int16_t *)frm = htole16(ni->ni_associd);
12498a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc);
12508a1b9b6aSSam Leffler 		} else
12518a1b9b6aSSam Leffler 			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
12521a1e1d21SSam Leffler 		frm += 2;
12531a1e1d21SSam Leffler 
12541a1e1d21SSam Leffler 		frm = ieee80211_add_rates(frm, &ni->ni_rates);
12551a1e1d21SSam Leffler 		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
12568a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
12578a1b9b6aSSam Leffler 			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
12581a1e1d21SSam Leffler 		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
12591a1e1d21SSam Leffler 		break;
12601a1e1d21SSam Leffler 
12611a1e1d21SSam Leffler 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
12628a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
12638a1b9b6aSSam Leffler 			"[%s] send station disassociate (reason %d)\n",
12641a1e1d21SSam Leffler 			ether_sprintf(ni->ni_macaddr), arg);
12658a1b9b6aSSam Leffler 		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
12661a1e1d21SSam Leffler 		if (m == NULL)
12678a1b9b6aSSam Leffler 			senderr(ENOMEM, is_tx_nobuf);
12688a1b9b6aSSam Leffler 		*(u_int16_t *)frm = htole16(arg);	/* reason */
12698a1b9b6aSSam Leffler 		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
12708a1b9b6aSSam Leffler 
12718a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT(ni, tx_disassoc);
12728a1b9b6aSSam Leffler 		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
12731a1e1d21SSam Leffler 		break;
12741a1e1d21SSam Leffler 
12751a1e1d21SSam Leffler 	default:
12768a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
12778a1b9b6aSSam Leffler 			"[%s] invalid mgmt frame type %u\n",
12788a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr), type);
12791be50176SSam Leffler 		senderr(EINVAL, is_tx_unknownmgt);
12800a915fadSSam Leffler 		/* NOTREACHED */
12811a1e1d21SSam Leffler 	}
12821a1e1d21SSam Leffler 
12838a1b9b6aSSam Leffler 	ret = ieee80211_mgmt_output(ic, ni, m, type);
12840a915fadSSam Leffler 	if (ret == 0) {
12850a915fadSSam Leffler 		if (timer)
12861a1e1d21SSam Leffler 			ic->ic_mgt_timer = timer;
12870a915fadSSam Leffler 	} else {
12880a915fadSSam Leffler bad:
12898a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
12900a915fadSSam Leffler 	}
12911a1e1d21SSam Leffler 	return ret;
12920a915fadSSam Leffler #undef senderr
12931a1e1d21SSam Leffler }
12948a1b9b6aSSam Leffler 
12958a1b9b6aSSam Leffler /*
12968a1b9b6aSSam Leffler  * Allocate a beacon frame and fillin the appropriate bits.
12978a1b9b6aSSam Leffler  */
12988a1b9b6aSSam Leffler struct mbuf *
12998a1b9b6aSSam Leffler ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
13008a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo)
13018a1b9b6aSSam Leffler {
13028a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
13038a1b9b6aSSam Leffler 	struct ieee80211_frame *wh;
13048a1b9b6aSSam Leffler 	struct mbuf *m;
13058a1b9b6aSSam Leffler 	int pktlen;
13068a1b9b6aSSam Leffler 	u_int8_t *frm, *efrm;
13078a1b9b6aSSam Leffler 	u_int16_t capinfo;
13088a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
13098a1b9b6aSSam Leffler 
13108a1b9b6aSSam Leffler 	/*
13118a1b9b6aSSam Leffler 	 * beacon frame format
13128a1b9b6aSSam Leffler 	 *	[8] time stamp
13138a1b9b6aSSam Leffler 	 *	[2] beacon interval
13148a1b9b6aSSam Leffler 	 *	[2] cabability information
13158a1b9b6aSSam Leffler 	 *	[tlv] ssid
13168a1b9b6aSSam Leffler 	 *	[tlv] supported rates
13178a1b9b6aSSam Leffler 	 *	[3] parameter set (DS)
13188a1b9b6aSSam Leffler 	 *	[tlv] parameter set (IBSS/TIM)
13198a1b9b6aSSam Leffler 	 *	[tlv] extended rate phy (ERP)
13208a1b9b6aSSam Leffler 	 *	[tlv] extended supported rates
13218a1b9b6aSSam Leffler 	 *	[tlv] WME parameters
13228a1b9b6aSSam Leffler 	 *	[tlv] WPA/RSN parameters
13238a1b9b6aSSam Leffler 	 * XXX Vendor-specific OIDs (e.g. Atheros)
13248a1b9b6aSSam Leffler 	 * NB: we allocate the max space required for the TIM bitmap.
13258a1b9b6aSSam Leffler 	 */
13268a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
13278a1b9b6aSSam Leffler 	pktlen =   8					/* time stamp */
13288a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* beacon interval */
13298a1b9b6aSSam Leffler 		 + sizeof(u_int16_t)			/* capabilities */
13308a1b9b6aSSam Leffler 		 + 2 + ni->ni_esslen			/* ssid */
13318a1b9b6aSSam Leffler 	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
13328a1b9b6aSSam Leffler 	         + 2 + 1				/* DS parameters */
13338a1b9b6aSSam Leffler 		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
13348a1b9b6aSSam Leffler 		 + 2 + 1				/* ERP */
13358a1b9b6aSSam Leffler 	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
13368a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
13378a1b9b6aSSam Leffler 			sizeof(struct ieee80211_wme_param) : 0)
13388a1b9b6aSSam Leffler 		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
13398a1b9b6aSSam Leffler 			2*sizeof(struct ieee80211_ie_wpa) : 0)
13408a1b9b6aSSam Leffler 		 ;
13418a1b9b6aSSam Leffler 	m = ieee80211_getmgtframe(&frm, pktlen);
13428a1b9b6aSSam Leffler 	if (m == NULL) {
13438a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
13448a1b9b6aSSam Leffler 			"%s: cannot get buf; size %u\n", __func__, pktlen);
13458a1b9b6aSSam Leffler 		ic->ic_stats.is_tx_nobuf++;
13468a1b9b6aSSam Leffler 		return NULL;
13478a1b9b6aSSam Leffler 	}
13488a1b9b6aSSam Leffler 
13498a1b9b6aSSam Leffler 	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
13508a1b9b6aSSam Leffler 	frm += 8;
13518a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(ni->ni_intval);
13528a1b9b6aSSam Leffler 	frm += 2;
13538a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
13548a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
13558a1b9b6aSSam Leffler 	else
13568a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
13578a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
13588a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
13598a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
13608a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
13618a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
13628a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
13638a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
13648a1b9b6aSSam Leffler 	bo->bo_caps = (u_int16_t *)frm;
13658a1b9b6aSSam Leffler 	*(u_int16_t *)frm = htole16(capinfo);
13668a1b9b6aSSam Leffler 	frm += 2;
13678a1b9b6aSSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
13688a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
13698a1b9b6aSSam Leffler 		*frm++ = ni->ni_esslen;
13708a1b9b6aSSam Leffler 		memcpy(frm, ni->ni_essid, ni->ni_esslen);
13718a1b9b6aSSam Leffler 		frm += ni->ni_esslen;
13728a1b9b6aSSam Leffler 	} else
13738a1b9b6aSSam Leffler 		*frm++ = 0;
13748a1b9b6aSSam Leffler 	frm = ieee80211_add_rates(frm, rs);
13758a1b9b6aSSam Leffler 	if (ic->ic_curmode != IEEE80211_MODE_FH) {
13768a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_DSPARMS;
13778a1b9b6aSSam Leffler 		*frm++ = 1;
13788a1b9b6aSSam Leffler 		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
13798a1b9b6aSSam Leffler 	}
13808a1b9b6aSSam Leffler 	bo->bo_tim = frm;
13818a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
13828a1b9b6aSSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
13838a1b9b6aSSam Leffler 		*frm++ = 2;
13848a1b9b6aSSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
13858a1b9b6aSSam Leffler 		bo->bo_tim_len = 0;
13868a1b9b6aSSam Leffler 	} else {
13878a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
13888a1b9b6aSSam Leffler 
13898a1b9b6aSSam Leffler 		tie->tim_ie = IEEE80211_ELEMID_TIM;
13908a1b9b6aSSam Leffler 		tie->tim_len = 4;	/* length */
13918a1b9b6aSSam Leffler 		tie->tim_count = 0;	/* DTIM count */
13928a1b9b6aSSam Leffler 		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
13938a1b9b6aSSam Leffler 		tie->tim_bitctl = 0;	/* bitmap control */
13948a1b9b6aSSam Leffler 		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
13958a1b9b6aSSam Leffler 		frm += sizeof(struct ieee80211_tim_ie);
13968a1b9b6aSSam Leffler 		bo->bo_tim_len = 1;
13978a1b9b6aSSam Leffler 	}
13988a1b9b6aSSam Leffler 	bo->bo_trailer = frm;
13998a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
14008a1b9b6aSSam Leffler 		bo->bo_wme = frm;
14018a1b9b6aSSam Leffler 		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
14025aa17efaSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
14038a1b9b6aSSam Leffler 	}
14048a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WPA)
14058a1b9b6aSSam Leffler 		frm = ieee80211_add_wpa(frm, ic);
14068a1b9b6aSSam Leffler 	if (ic->ic_curmode == IEEE80211_MODE_11G)
14078a1b9b6aSSam Leffler 		frm = ieee80211_add_erp(frm, ic);
14088a1b9b6aSSam Leffler 	efrm = ieee80211_add_xrates(frm, rs);
14098a1b9b6aSSam Leffler 	bo->bo_trailer_len = efrm - bo->bo_trailer;
14108a1b9b6aSSam Leffler 	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
14118a1b9b6aSSam Leffler 
14128a1b9b6aSSam Leffler 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
14138a1b9b6aSSam Leffler 	KASSERT(m != NULL, ("no space for 802.11 header?"));
14148a1b9b6aSSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
14158a1b9b6aSSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
14168a1b9b6aSSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
14178a1b9b6aSSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
14188a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
14198a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
14208a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
14218a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
14228a1b9b6aSSam Leffler 	*(u_int16_t *)wh->i_seq = 0;
14238a1b9b6aSSam Leffler 
14248a1b9b6aSSam Leffler 	return m;
14258a1b9b6aSSam Leffler }
14268a1b9b6aSSam Leffler 
14278a1b9b6aSSam Leffler /*
14288a1b9b6aSSam Leffler  * Update the dynamic parts of a beacon frame based on the current state.
14298a1b9b6aSSam Leffler  */
14308a1b9b6aSSam Leffler int
14318a1b9b6aSSam Leffler ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
14328a1b9b6aSSam Leffler 	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
14338a1b9b6aSSam Leffler {
14348a1b9b6aSSam Leffler 	int len_changed = 0;
14358a1b9b6aSSam Leffler 	u_int16_t capinfo;
14368a1b9b6aSSam Leffler 
14378a1b9b6aSSam Leffler 	IEEE80211_BEACON_LOCK(ic);
14388a1b9b6aSSam Leffler 	/* XXX faster to recalculate entirely or just changes? */
14398a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
14408a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
14418a1b9b6aSSam Leffler 	else
14428a1b9b6aSSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
14438a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
14448a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
14458a1b9b6aSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
14468a1b9b6aSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
14478a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
14488a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
14498a1b9b6aSSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
14508a1b9b6aSSam Leffler 	*bo->bo_caps = htole16(capinfo);
14518a1b9b6aSSam Leffler 
14528a1b9b6aSSam Leffler 	if (ic->ic_flags & IEEE80211_F_WME) {
14538a1b9b6aSSam Leffler 		struct ieee80211_wme_state *wme = &ic->ic_wme;
14548a1b9b6aSSam Leffler 
14558a1b9b6aSSam Leffler 		/*
14568a1b9b6aSSam Leffler 		 * Check for agressive mode change.  When there is
14578a1b9b6aSSam Leffler 		 * significant high priority traffic in the BSS
14588a1b9b6aSSam Leffler 		 * throttle back BE traffic by using conservative
14598a1b9b6aSSam Leffler 		 * parameters.  Otherwise BE uses agressive params
14608a1b9b6aSSam Leffler 		 * to optimize performance of legacy/non-QoS traffic.
14618a1b9b6aSSam Leffler 		 */
14628a1b9b6aSSam Leffler 		if (wme->wme_flags & WME_F_AGGRMODE) {
14638a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic >
14648a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
14658a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
14668a1b9b6aSSam Leffler 				    "%s: traffic %u, disable aggressive mode\n",
14678a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
14688a1b9b6aSSam Leffler 				wme->wme_flags &= ~WME_F_AGGRMODE;
14698a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
14708a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
14718a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
14728a1b9b6aSSam Leffler 			} else
14738a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
14748a1b9b6aSSam Leffler 		} else {
14758a1b9b6aSSam Leffler 			if (wme->wme_hipri_traffic <=
14768a1b9b6aSSam Leffler 			    wme->wme_hipri_switch_thresh) {
14778a1b9b6aSSam Leffler 				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
14788a1b9b6aSSam Leffler 				    "%s: traffic %u, enable aggressive mode\n",
14798a1b9b6aSSam Leffler 				    __func__, wme->wme_hipri_traffic);
14808a1b9b6aSSam Leffler 				wme->wme_flags |= WME_F_AGGRMODE;
14818a1b9b6aSSam Leffler 				ieee80211_wme_updateparams_locked(ic);
14828a1b9b6aSSam Leffler 				wme->wme_hipri_traffic = 0;
14838a1b9b6aSSam Leffler 			} else
14848a1b9b6aSSam Leffler 				wme->wme_hipri_traffic =
14858a1b9b6aSSam Leffler 					wme->wme_hipri_switch_hysteresis;
14868a1b9b6aSSam Leffler 		}
14878a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
14888a1b9b6aSSam Leffler 			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
14898a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
14908a1b9b6aSSam Leffler 		}
14918a1b9b6aSSam Leffler 	}
14928a1b9b6aSSam Leffler 
14938a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
14948a1b9b6aSSam Leffler 		struct ieee80211_tim_ie *tie =
14958a1b9b6aSSam Leffler 			(struct ieee80211_tim_ie *) bo->bo_tim;
14968a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
14978a1b9b6aSSam Leffler 			u_int timlen, timoff, i;
14988a1b9b6aSSam Leffler 			/*
14998a1b9b6aSSam Leffler 			 * ATIM/DTIM needs updating.  If it fits in the
15008a1b9b6aSSam Leffler 			 * current space allocated then just copy in the
15018a1b9b6aSSam Leffler 			 * new bits.  Otherwise we need to move any trailing
15028a1b9b6aSSam Leffler 			 * data to make room.  Note that we know there is
15038a1b9b6aSSam Leffler 			 * contiguous space because ieee80211_beacon_allocate
15048a1b9b6aSSam Leffler 			 * insures there is space in the mbuf to write a
15058a1b9b6aSSam Leffler 			 * maximal-size virtual bitmap (based on ic_max_aid).
15068a1b9b6aSSam Leffler 			 */
15078a1b9b6aSSam Leffler 			/*
15088a1b9b6aSSam Leffler 			 * Calculate the bitmap size and offset, copy any
15098a1b9b6aSSam Leffler 			 * trailer out of the way, and then copy in the
15108a1b9b6aSSam Leffler 			 * new bitmap and update the information element.
15118a1b9b6aSSam Leffler 			 * Note that the tim bitmap must contain at least
15128a1b9b6aSSam Leffler 			 * one byte and any offset must be even.
15138a1b9b6aSSam Leffler 			 */
15148a1b9b6aSSam Leffler 			if (ic->ic_ps_pending != 0) {
15158a1b9b6aSSam Leffler 				timoff = 128;		/* impossibly large */
15168a1b9b6aSSam Leffler 				for (i = 0; i < ic->ic_tim_len; i++)
15178a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i]) {
15188a1b9b6aSSam Leffler 						timoff = i &~ 1;
15198a1b9b6aSSam Leffler 						break;
15208a1b9b6aSSam Leffler 					}
15218a1b9b6aSSam Leffler 				KASSERT(timoff != 128, ("tim bitmap empty!"));
15228a1b9b6aSSam Leffler 				for (i = ic->ic_tim_len-1; i >= timoff; i--)
15238a1b9b6aSSam Leffler 					if (ic->ic_tim_bitmap[i])
15248a1b9b6aSSam Leffler 						break;
15258a1b9b6aSSam Leffler 				timlen = 1 + (i - timoff);
15268a1b9b6aSSam Leffler 			} else {
15278a1b9b6aSSam Leffler 				timoff = 0;
15288a1b9b6aSSam Leffler 				timlen = 1;
15298a1b9b6aSSam Leffler 			}
15308a1b9b6aSSam Leffler 			if (timlen != bo->bo_tim_len) {
15318a1b9b6aSSam Leffler 				/* copy up/down trailer */
15328a1b9b6aSSam Leffler 				ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
15338a1b9b6aSSam Leffler 					bo->bo_trailer_len);
15348a1b9b6aSSam Leffler 				bo->bo_trailer = tie->tim_bitmap+timlen;
15358a1b9b6aSSam Leffler 				bo->bo_wme = bo->bo_trailer;
15368a1b9b6aSSam Leffler 				bo->bo_tim_len = timlen;
15378a1b9b6aSSam Leffler 
15388a1b9b6aSSam Leffler 				/* update information element */
15398a1b9b6aSSam Leffler 				tie->tim_len = 3 + timlen;
15408a1b9b6aSSam Leffler 				tie->tim_bitctl = timoff;
15418a1b9b6aSSam Leffler 				len_changed = 1;
15428a1b9b6aSSam Leffler 			}
15438a1b9b6aSSam Leffler 			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
15448a1b9b6aSSam Leffler 				bo->bo_tim_len);
15458a1b9b6aSSam Leffler 
15468a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
15478a1b9b6aSSam Leffler 
15488a1b9b6aSSam Leffler 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
15498a1b9b6aSSam Leffler 				"%s: TIM updated, pending %u, off %u, len %u\n",
15508a1b9b6aSSam Leffler 				__func__, ic->ic_ps_pending, timoff, timlen);
15518a1b9b6aSSam Leffler 		}
15528a1b9b6aSSam Leffler 		/* count down DTIM period */
15538a1b9b6aSSam Leffler 		if (tie->tim_count == 0)
15548a1b9b6aSSam Leffler 			tie->tim_count = tie->tim_period - 1;
15558a1b9b6aSSam Leffler 		else
15568a1b9b6aSSam Leffler 			tie->tim_count--;
15578a1b9b6aSSam Leffler 		/* update state for buffered multicast frames on DTIM */
15588a1b9b6aSSam Leffler 		if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
15598a1b9b6aSSam Leffler 			tie->tim_bitctl |= 1;
15608a1b9b6aSSam Leffler 		else
15618a1b9b6aSSam Leffler 			tie->tim_bitctl &= ~1;
15628a1b9b6aSSam Leffler 	}
15638a1b9b6aSSam Leffler 	IEEE80211_BEACON_UNLOCK(ic);
15648a1b9b6aSSam Leffler 
15658a1b9b6aSSam Leffler 	return len_changed;
15668a1b9b6aSSam Leffler }
15678a1b9b6aSSam Leffler 
15688a1b9b6aSSam Leffler /*
15698a1b9b6aSSam Leffler  * Save an outbound packet for a node in power-save sleep state.
15708a1b9b6aSSam Leffler  * The new packet is placed on the node's saved queue, and the TIM
15718a1b9b6aSSam Leffler  * is changed, if necessary.
15728a1b9b6aSSam Leffler  */
15738a1b9b6aSSam Leffler void
15748a1b9b6aSSam Leffler ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
15758a1b9b6aSSam Leffler 		  struct mbuf *m)
15768a1b9b6aSSam Leffler {
15778a1b9b6aSSam Leffler 	int qlen, age;
15788a1b9b6aSSam Leffler 
15798a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_LOCK(ni);
15808a1b9b6aSSam Leffler 	if (_IF_QFULL(&ni->ni_savedq)) {
15818a1b9b6aSSam Leffler 		_IF_DROP(&ni->ni_savedq);
15828a1b9b6aSSam Leffler 		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
15838a1b9b6aSSam Leffler 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
15848a1b9b6aSSam Leffler 			"[%s] pwr save q overflow, drops %d (size %d)\n",
15858a1b9b6aSSam Leffler 			ether_sprintf(ni->ni_macaddr),
15868a1b9b6aSSam Leffler 			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
15878a1b9b6aSSam Leffler #ifdef IEEE80211_DEBUG
15888a1b9b6aSSam Leffler 		if (ieee80211_msg_dumppkts(ic))
15898a1b9b6aSSam Leffler 			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
15908a1b9b6aSSam Leffler #endif
15918a1b9b6aSSam Leffler 		m_freem(m);
15928a1b9b6aSSam Leffler 		return;
15938a1b9b6aSSam Leffler 	}
15948a1b9b6aSSam Leffler 	/*
15958a1b9b6aSSam Leffler 	 * Tag the frame with it's expiry time and insert
15968a1b9b6aSSam Leffler 	 * it in the queue.  The aging interval is 4 times
15978a1b9b6aSSam Leffler 	 * the listen interval specified by the station.
15988a1b9b6aSSam Leffler 	 * Frames that sit around too long are reclaimed
15998a1b9b6aSSam Leffler 	 * using this information.
16008a1b9b6aSSam Leffler 	 */
16018a1b9b6aSSam Leffler 	/* XXX handle overflow? */
16028a1b9b6aSSam Leffler 	age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
16038a1b9b6aSSam Leffler 	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
16048a1b9b6aSSam Leffler 	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
16058a1b9b6aSSam Leffler 
16068a1b9b6aSSam Leffler 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
16078a1b9b6aSSam Leffler 		"[%s] save frame, %u now queued\n",
16088a1b9b6aSSam Leffler 		ether_sprintf(ni->ni_macaddr), qlen);
16098a1b9b6aSSam Leffler 
16108a1b9b6aSSam Leffler 	if (qlen == 1)
16118a1b9b6aSSam Leffler 		ic->ic_set_tim(ic, ni, 1);
16128a1b9b6aSSam Leffler }
1613