ieee80211_output.c revision 153973
1116742Ssam/*-
2116904Ssam * Copyright (c) 2001 Atsushi Onoe
3139530Ssam * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4116742Ssam * All rights reserved.
5116742Ssam *
6116742Ssam * Redistribution and use in source and binary forms, with or without
7116742Ssam * modification, are permitted provided that the following conditions
8116742Ssam * are met:
9116742Ssam * 1. Redistributions of source code must retain the above copyright
10116904Ssam *    notice, this list of conditions and the following disclaimer.
11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright
12116904Ssam *    notice, this list of conditions and the following disclaimer in the
13116904Ssam *    documentation and/or other materials provided with the distribution.
14116904Ssam * 3. The name of the author may not be used to endorse or promote products
15116904Ssam *    derived from this software without specific prior written permission.
16116742Ssam *
17116742Ssam * Alternatively, this software may be distributed under the terms of the
18116742Ssam * GNU General Public License ("GPL") version 2 as published by the Free
19116742Ssam * Software Foundation.
20116742Ssam *
21116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31116742Ssam */
32116742Ssam
33116742Ssam#include <sys/cdefs.h>
34116742Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 153973 2006-01-02 16:57:20Z sam $");
35116742Ssam
36116742Ssam#include "opt_inet.h"
37116742Ssam
38116742Ssam#include <sys/param.h>
39116742Ssam#include <sys/systm.h>
40116742Ssam#include <sys/mbuf.h>
41116742Ssam#include <sys/kernel.h>
42116742Ssam#include <sys/endian.h>
43116742Ssam
44138568Ssam#include <sys/socket.h>
45116742Ssam
46138568Ssam#include <net/bpf.h>
47138568Ssam#include <net/ethernet.h>
48116742Ssam#include <net/if.h>
49138568Ssam#include <net/if_llc.h>
50116742Ssam#include <net/if_media.h>
51138568Ssam#include <net/if_vlan_var.h>
52116742Ssam
53116742Ssam#include <net80211/ieee80211_var.h>
54116742Ssam
55116742Ssam#ifdef INET
56116742Ssam#include <netinet/in.h>
57116742Ssam#include <netinet/if_ether.h>
58138568Ssam#include <netinet/in_systm.h>
59138568Ssam#include <netinet/ip.h>
60116742Ssam#endif
61116742Ssam
62138568Ssam#ifdef IEEE80211_DEBUG
63119150Ssam/*
64138568Ssam * Decide if an outbound management frame should be
65138568Ssam * printed when debugging is enabled.  This filters some
66138568Ssam * of the less interesting frames that come frequently
67138568Ssam * (e.g. beacons).
68138568Ssam */
69138568Ssamstatic __inline int
70138568Ssamdoprint(struct ieee80211com *ic, int subtype)
71138568Ssam{
72138568Ssam	switch (subtype) {
73138568Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
74138568Ssam		return (ic->ic_opmode == IEEE80211_M_IBSS);
75138568Ssam	}
76138568Ssam	return 1;
77138568Ssam}
78138568Ssam#endif
79138568Ssam
80138568Ssam/*
81148314Ssam * Set the direction field and address fields of an outgoing
82148314Ssam * non-QoS frame.  Note this should be called early on in
83148314Ssam * constructing a frame as it sets i_fc[1]; other bits can
84148314Ssam * then be or'd in.
85148314Ssam */
86148314Ssamstatic void
87148314Ssamieee80211_send_setup(struct ieee80211com *ic,
88148314Ssam	struct ieee80211_node *ni,
89148314Ssam	struct ieee80211_frame *wh,
90148314Ssam	int type,
91148314Ssam	const u_int8_t sa[IEEE80211_ADDR_LEN],
92148314Ssam	const u_int8_t da[IEEE80211_ADDR_LEN],
93148314Ssam	const u_int8_t bssid[IEEE80211_ADDR_LEN])
94148314Ssam{
95148314Ssam#define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
96148314Ssam
97148314Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
98148314Ssam	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
99148314Ssam		switch (ic->ic_opmode) {
100148314Ssam		case IEEE80211_M_STA:
101148314Ssam			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
102148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
103148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
104148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr3, da);
105148314Ssam			break;
106148314Ssam		case IEEE80211_M_IBSS:
107148314Ssam		case IEEE80211_M_AHDEMO:
108148314Ssam			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
109148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr1, da);
110148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
111148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
112148314Ssam			break;
113148314Ssam		case IEEE80211_M_HOSTAP:
114148314Ssam			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
115148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr1, da);
116148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
117148314Ssam			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
118148314Ssam			break;
119148314Ssam		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
120148314Ssam			break;
121148314Ssam		}
122148314Ssam	} else {
123148314Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
124148314Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, da);
125148314Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
126148314Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
127148314Ssam	}
128148314Ssam	*(u_int16_t *)&wh->i_dur[0] = 0;
129148314Ssam	/* NB: use non-QoS tid */
130148314Ssam	*(u_int16_t *)&wh->i_seq[0] =
131148314Ssam	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
132148314Ssam	ni->ni_txseqs[0]++;
133148314Ssam#undef WH4
134148314Ssam}
135148314Ssam
136148314Ssam/*
137119150Ssam * Send a management frame to the specified node.  The node pointer
138119150Ssam * must have a reference as the pointer will be passed to the driver
139119150Ssam * and potentially held for a long time.  If the frame is successfully
140119150Ssam * dispatched to the driver, then it is responsible for freeing the
141119150Ssam * reference (and potentially free'ing up any associated storage).
142119150Ssam */
143119150Ssamstatic int
144138568Ssamieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
145116742Ssam    struct mbuf *m, int type)
146116742Ssam{
147138568Ssam	struct ifnet *ifp = ic->ic_ifp;
148116742Ssam	struct ieee80211_frame *wh;
149116742Ssam
150119150Ssam	KASSERT(ni != NULL, ("null node"));
151116742Ssam
152119150Ssam	/*
153119150Ssam	 * Yech, hack alert!  We want to pass the node down to the
154119150Ssam	 * driver's start routine.  If we don't do so then the start
155119150Ssam	 * routine must immediately look it up again and that can
156119150Ssam	 * cause a lock order reversal if, for example, this frame
157119150Ssam	 * is being sent because the station is being timedout and
158119150Ssam	 * the frame being sent is a DEAUTH message.  We could stick
159119150Ssam	 * this in an m_tag and tack that on to the mbuf.  However
160119150Ssam	 * that's rather expensive to do for every frame so instead
161119150Ssam	 * we stuff it in the rcvif field since outbound frames do
162119150Ssam	 * not (presently) use this.
163119150Ssam	 */
164116742Ssam	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
165116742Ssam	if (m == NULL)
166116742Ssam		return ENOMEM;
167119150Ssam	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
168119150Ssam	m->m_pkthdr.rcvif = (void *)ni;
169119150Ssam
170116742Ssam	wh = mtod(m, struct ieee80211_frame *);
171148314Ssam	ieee80211_send_setup(ic, ni, wh,
172148314Ssam		IEEE80211_FC0_TYPE_MGT | type,
173148314Ssam		ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
174138568Ssam	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
175138568Ssam		m->m_flags &= ~M_LINK0;
176138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
177138568Ssam			"[%s] encrypting frame (%s)\n",
178138568Ssam			ether_sprintf(wh->i_addr1), __func__);
179138568Ssam		wh->i_fc[1] |= IEEE80211_FC1_WEP;
180138568Ssam	}
181116742Ssam#ifdef IEEE80211_DEBUG
182138568Ssam	/* avoid printing too many frames */
183138568Ssam	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
184138568Ssam	    ieee80211_msg_dumppkts(ic)) {
185138568Ssam		printf("[%s] send %s on channel %u\n",
186138568Ssam		    ether_sprintf(wh->i_addr1),
187138568Ssam		    ieee80211_mgt_subtype_name[
188138568Ssam			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
189138568Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
190148936Ssam		    ieee80211_chan2ieee(ic, ic->ic_curchan));
191138568Ssam	}
192116742Ssam#endif
193138568Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);
194116742Ssam	IF_ENQUEUE(&ic->ic_mgtq, m);
195116742Ssam	ifp->if_timer = 1;
196132712Srwatson	if_start(ifp);
197116742Ssam	return 0;
198116742Ssam}
199116742Ssam
200119150Ssam/*
201138568Ssam * Send a null data frame to the specified node.
202148582Ssam *
203148582Ssam * NB: the caller is assumed to have setup a node reference
204148582Ssam *     for use; this is necessary to deal with a race condition
205148582Ssam *     when probing for inactive stations.
206119150Ssam */
207138568Ssamint
208148301Ssamieee80211_send_nulldata(struct ieee80211_node *ni)
209138568Ssam{
210148301Ssam	struct ieee80211com *ic = ni->ni_ic;
211138568Ssam	struct ifnet *ifp = ic->ic_ifp;
212138568Ssam	struct mbuf *m;
213138568Ssam	struct ieee80211_frame *wh;
214138568Ssam
215151967Sandre	MGETHDR(m, M_NOWAIT, MT_DATA);
216138568Ssam	if (m == NULL) {
217138568Ssam		/* XXX debug msg */
218138568Ssam		ic->ic_stats.is_tx_nobuf++;
219148582Ssam		ieee80211_unref_node(&ni);
220138568Ssam		return ENOMEM;
221138568Ssam	}
222148582Ssam	m->m_pkthdr.rcvif = (void *) ni;
223138568Ssam
224138568Ssam	wh = mtod(m, struct ieee80211_frame *);
225148314Ssam	ieee80211_send_setup(ic, ni, wh,
226148314Ssam		IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
227148314Ssam		ic->ic_myaddr, ni->ni_macaddr, ni->ni_bssid);
228148314Ssam	/* NB: power management bit is never sent by an AP */
229148314Ssam	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
230148314Ssam	    ic->ic_opmode != IEEE80211_M_HOSTAP)
231148314Ssam		wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
232138568Ssam	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
233138568Ssam
234138568Ssam	IEEE80211_NODE_STAT(ni, tx_data);
235138568Ssam
236148314Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
237148314Ssam	    "[%s] send null data frame on channel %u, pwr mgt %s\n",
238148314Ssam	    ether_sprintf(ni->ni_macaddr),
239148936Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan),
240148314Ssam	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
241148314Ssam
242138568Ssam	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
243138568Ssam	if_start(ifp);
244138568Ssam
245138568Ssam	return 0;
246138568Ssam}
247138568Ssam
248138568Ssam/*
249138568Ssam * Assign priority to a frame based on any vlan tag assigned
250138568Ssam * to the station and/or any Diffserv setting in an IP header.
251138568Ssam * Finally, if an ACM policy is setup (in station mode) it's
252138568Ssam * applied.
253138568Ssam */
254138568Ssamint
255138568Ssamieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
256138568Ssam{
257138568Ssam	int v_wme_ac, d_wme_ac, ac;
258138568Ssam#ifdef INET
259138568Ssam	struct ether_header *eh;
260138568Ssam#endif
261138568Ssam
262138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
263138568Ssam		ac = WME_AC_BE;
264138568Ssam		goto done;
265138568Ssam	}
266138568Ssam
267138568Ssam	/*
268138568Ssam	 * If node has a vlan tag then all traffic
269138568Ssam	 * to it must have a matching tag.
270138568Ssam	 */
271138568Ssam	v_wme_ac = 0;
272138568Ssam	if (ni->ni_vlan != 0) {
273138568Ssam		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
274143716Ssam		 if (mtag == NULL) {
275138568Ssam			IEEE80211_NODE_STAT(ni, tx_novlantag);
276138568Ssam			return 1;
277138568Ssam		}
278138568Ssam		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
279138568Ssam		    EVL_VLANOFTAG(ni->ni_vlan)) {
280138568Ssam			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
281138568Ssam			return 1;
282138568Ssam		}
283138568Ssam		/* map vlan priority to AC */
284138568Ssam		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
285138568Ssam		case 1:
286138568Ssam		case 2:
287138568Ssam			v_wme_ac = WME_AC_BK;
288138568Ssam			break;
289138568Ssam		case 0:
290138568Ssam		case 3:
291138568Ssam			v_wme_ac = WME_AC_BE;
292138568Ssam			break;
293138568Ssam		case 4:
294138568Ssam		case 5:
295138568Ssam			v_wme_ac = WME_AC_VI;
296138568Ssam			break;
297138568Ssam		case 6:
298138568Ssam		case 7:
299138568Ssam			v_wme_ac = WME_AC_VO;
300138568Ssam			break;
301138568Ssam		}
302138568Ssam	}
303138568Ssam
304138568Ssam#ifdef INET
305138568Ssam	eh = mtod(m, struct ether_header *);
306138568Ssam	if (eh->ether_type == htons(ETHERTYPE_IP)) {
307138568Ssam		const struct ip *ip = (struct ip *)
308138568Ssam			(mtod(m, u_int8_t *) + sizeof (*eh));
309138568Ssam		/*
310138568Ssam		 * IP frame, map the TOS field.
311138568Ssam		 */
312138568Ssam		switch (ip->ip_tos) {
313138568Ssam		case 0x08:
314138568Ssam		case 0x20:
315138568Ssam			d_wme_ac = WME_AC_BK;	/* background */
316138568Ssam			break;
317138568Ssam		case 0x28:
318138568Ssam		case 0xa0:
319138568Ssam			d_wme_ac = WME_AC_VI;	/* video */
320138568Ssam			break;
321138568Ssam		case 0x30:			/* voice */
322138568Ssam		case 0xe0:
323138568Ssam		case 0x88:			/* XXX UPSD */
324138568Ssam		case 0xb8:
325138568Ssam			d_wme_ac = WME_AC_VO;
326138568Ssam			break;
327138568Ssam		default:
328138568Ssam			d_wme_ac = WME_AC_BE;
329138568Ssam			break;
330138568Ssam		}
331138568Ssam	} else {
332138568Ssam#endif /* INET */
333138568Ssam		d_wme_ac = WME_AC_BE;
334138568Ssam#ifdef INET
335138568Ssam	}
336138568Ssam#endif
337138568Ssam	/*
338138568Ssam	 * Use highest priority AC.
339138568Ssam	 */
340138568Ssam	if (v_wme_ac > d_wme_ac)
341138568Ssam		ac = v_wme_ac;
342138568Ssam	else
343138568Ssam		ac = d_wme_ac;
344138568Ssam
345138568Ssam	/*
346138568Ssam	 * Apply ACM policy.
347138568Ssam	 */
348138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA) {
349138568Ssam		static const int acmap[4] = {
350138568Ssam			WME_AC_BK,	/* WME_AC_BE */
351138568Ssam			WME_AC_BK,	/* WME_AC_BK */
352138568Ssam			WME_AC_BE,	/* WME_AC_VI */
353138568Ssam			WME_AC_VI,	/* WME_AC_VO */
354138568Ssam		};
355138568Ssam		while (ac != WME_AC_BK &&
356138568Ssam		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
357138568Ssam			ac = acmap[ac];
358138568Ssam	}
359138568Ssamdone:
360138568Ssam	M_WME_SETAC(m, ac);
361138568Ssam	return 0;
362138568Ssam}
363138568Ssam
364138568Ssam/*
365139527Ssam * Insure there is sufficient contiguous space to encapsulate the
366139527Ssam * 802.11 data frame.  If room isn't already there, arrange for it.
367139527Ssam * Drivers and cipher modules assume we have done the necessary work
368139527Ssam * and fail rudely if they don't find the space they need.
369139527Ssam */
370139527Ssamstatic struct mbuf *
371139527Ssamieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
372139527Ssam	struct ieee80211_key *key, struct mbuf *m)
373139527Ssam{
374139527Ssam#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
375139527Ssam	int needed_space = hdrsize;
376139527Ssam
377139527Ssam	if (key != NULL) {
378139527Ssam		/* XXX belongs in crypto code? */
379139527Ssam		needed_space += key->wk_cipher->ic_header;
380139527Ssam		/* XXX frags */
381139527Ssam	}
382139527Ssam	/*
383139527Ssam	 * We know we are called just before stripping an Ethernet
384139527Ssam	 * header and prepending an LLC header.  This means we know
385139527Ssam	 * there will be
386139527Ssam	 *	sizeof(struct ether_header) - sizeof(struct llc)
387139527Ssam	 * bytes recovered to which we need additional space for the
388139527Ssam	 * 802.11 header and any crypto header.
389139527Ssam	 */
390139527Ssam	/* XXX check trailing space and copy instead? */
391139527Ssam	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
392139527Ssam		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
393139527Ssam		if (n == NULL) {
394139527Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
395139527Ssam			    "%s: cannot expand storage\n", __func__);
396139527Ssam			ic->ic_stats.is_tx_nobuf++;
397139527Ssam			m_freem(m);
398139527Ssam			return NULL;
399139527Ssam		}
400139527Ssam		KASSERT(needed_space <= MHLEN,
401139527Ssam		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
402139527Ssam		/*
403139527Ssam		 * Setup new mbuf to have leading space to prepend the
404139527Ssam		 * 802.11 header and any crypto header bits that are
405139527Ssam		 * required (the latter are added when the driver calls
406139527Ssam		 * back to ieee80211_crypto_encap to do crypto encapsulation).
407139527Ssam		 */
408139527Ssam		/* NB: must be first 'cuz it clobbers m_data */
409139527Ssam		m_move_pkthdr(n, m);
410139527Ssam		n->m_len = 0;			/* NB: m_gethdr does not set */
411139527Ssam		n->m_data += needed_space;
412139527Ssam		/*
413139527Ssam		 * Pull up Ethernet header to create the expected layout.
414139527Ssam		 * We could use m_pullup but that's overkill (i.e. we don't
415139527Ssam		 * need the actual data) and it cannot fail so do it inline
416139527Ssam		 * for speed.
417139527Ssam		 */
418139527Ssam		/* NB: struct ether_header is known to be contiguous */
419139527Ssam		n->m_len += sizeof(struct ether_header);
420139527Ssam		m->m_len -= sizeof(struct ether_header);
421139527Ssam		m->m_data += sizeof(struct ether_header);
422139527Ssam		/*
423139527Ssam		 * Replace the head of the chain.
424139527Ssam		 */
425139527Ssam		n->m_next = m;
426139527Ssam		m = n;
427139527Ssam	}
428139527Ssam	return m;
429139527Ssam#undef TO_BE_RECLAIMED
430139527Ssam}
431139527Ssam
432139527Ssam#define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
433139527Ssam/*
434139527Ssam * Return the transmit key to use in sending a unicast frame.
435139527Ssam * If a unicast key is set we use that.  When no unicast key is set
436139527Ssam * we fall back to the default transmit key.
437138568Ssam */
438138568Ssamstatic __inline struct ieee80211_key *
439139527Ssamieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
440138568Ssam{
441139527Ssam	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
442138568Ssam		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
443138568Ssam		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
444138568Ssam			return NULL;
445138568Ssam		return &ic->ic_nw_keys[ic->ic_def_txkey];
446138568Ssam	} else {
447138568Ssam		return &ni->ni_ucastkey;
448138568Ssam	}
449138568Ssam}
450138568Ssam
451138568Ssam/*
452139527Ssam * Return the transmit key to use in sending a multicast frame.
453139527Ssam * Multicast traffic always uses the group key which is installed as
454139527Ssam * the default tx key.
455139527Ssam */
456139527Ssamstatic __inline struct ieee80211_key *
457139527Ssamieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
458139527Ssam{
459139527Ssam	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
460139527Ssam	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
461139527Ssam		return NULL;
462139527Ssam	return &ic->ic_nw_keys[ic->ic_def_txkey];
463139527Ssam}
464139527Ssam
465139527Ssam/*
466138568Ssam * Encapsulate an outbound data frame.  The mbuf chain is updated.
467138568Ssam * If an error is encountered NULL is returned.  The caller is required
468138568Ssam * to provide a node reference and pullup the ethernet header in the
469138568Ssam * first mbuf.
470138568Ssam */
471116742Ssamstruct mbuf *
472138568Ssamieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
473138568Ssam	struct ieee80211_node *ni)
474116742Ssam{
475116742Ssam	struct ether_header eh;
476116742Ssam	struct ieee80211_frame *wh;
477138568Ssam	struct ieee80211_key *key;
478116742Ssam	struct llc *llc;
479139527Ssam	int hdrsize, datalen, addqos;
480116742Ssam
481138568Ssam	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
482116742Ssam	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
483116742Ssam
484138568Ssam	/*
485138568Ssam	 * Insure space for additional headers.  First identify
486138568Ssam	 * transmit key to use in calculating any buffer adjustments
487138568Ssam	 * required.  This is also used below to do privacy
488138568Ssam	 * encapsulation work.  Then calculate the 802.11 header
489138568Ssam	 * size and any padding required by the driver.
490138568Ssam	 *
491138568Ssam	 * Note key may be NULL if we fall back to the default
492138568Ssam	 * transmit key and that is not set.  In that case the
493138568Ssam	 * buffer may not be expanded as needed by the cipher
494138568Ssam	 * routines, but they will/should discard it.
495138568Ssam	 */
496138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
497139527Ssam		if (ic->ic_opmode == IEEE80211_M_STA ||
498139527Ssam		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
499139527Ssam			key = ieee80211_crypto_getucastkey(ic, ni);
500139527Ssam		else
501139527Ssam			key = ieee80211_crypto_getmcastkey(ic, ni);
502138568Ssam		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
503138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
504139527Ssam			    "[%s] no default transmit key (%s) deftxkey %u\n",
505139527Ssam			    ether_sprintf(eh.ether_dhost), __func__,
506139527Ssam			    ic->ic_def_txkey);
507139527Ssam			ic->ic_stats.is_tx_nodefkey++;
508138568Ssam		}
509138568Ssam	} else
510138568Ssam		key = NULL;
511138568Ssam	/* XXX 4-address format */
512139527Ssam	/*
513139527Ssam	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
514139527Ssam	 * frames so suppress use.  This may be an issue if other
515139527Ssam	 * ap's require all data frames to be QoS-encapsulated
516139527Ssam	 * once negotiated in which case we'll need to make this
517139527Ssam	 * configurable.
518139527Ssam	 */
519139527Ssam	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
520139527Ssam		 eh.ether_type != htons(ETHERTYPE_PAE);
521139527Ssam	if (addqos)
522138568Ssam		hdrsize = sizeof(struct ieee80211_qosframe);
523138568Ssam	else
524138568Ssam		hdrsize = sizeof(struct ieee80211_frame);
525138568Ssam	if (ic->ic_flags & IEEE80211_F_DATAPAD)
526138568Ssam		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
527138568Ssam	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
528138568Ssam	if (m == NULL) {
529138568Ssam		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
530127772Ssam		goto bad;
531127772Ssam	}
532116742Ssam
533138568Ssam	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
534116742Ssam	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
535116742Ssam	llc = mtod(m, struct llc *);
536116742Ssam	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
537116742Ssam	llc->llc_control = LLC_UI;
538116742Ssam	llc->llc_snap.org_code[0] = 0;
539116742Ssam	llc->llc_snap.org_code[1] = 0;
540116742Ssam	llc->llc_snap.org_code[2] = 0;
541116742Ssam	llc->llc_snap.ether_type = eh.ether_type;
542138568Ssam	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
543138568Ssam
544138568Ssam	M_PREPEND(m, hdrsize, M_DONTWAIT);
545121180Ssam	if (m == NULL) {
546138568Ssam		ic->ic_stats.is_tx_nobuf++;
547119150Ssam		goto bad;
548121180Ssam	}
549116742Ssam	wh = mtod(m, struct ieee80211_frame *);
550116742Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
551116742Ssam	*(u_int16_t *)wh->i_dur = 0;
552116742Ssam	switch (ic->ic_opmode) {
553116742Ssam	case IEEE80211_M_STA:
554116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
555116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
556116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
557116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
558116742Ssam		break;
559116742Ssam	case IEEE80211_M_IBSS:
560116742Ssam	case IEEE80211_M_AHDEMO:
561116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
562116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
563116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
564140636Ssam		/*
565140636Ssam		 * NB: always use the bssid from ic_bss as the
566140636Ssam		 *     neighbor's may be stale after an ibss merge
567140636Ssam		 */
568140636Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
569116742Ssam		break;
570116742Ssam	case IEEE80211_M_HOSTAP:
571116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
572116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
573116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
574116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
575116742Ssam		break;
576117817Ssam	case IEEE80211_M_MONITOR:
577119150Ssam		goto bad;
578116742Ssam	}
579147789Ssam	if (m->m_flags & M_MORE_DATA)
580147789Ssam		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
581139527Ssam	if (addqos) {
582138568Ssam		struct ieee80211_qosframe *qwh =
583138568Ssam			(struct ieee80211_qosframe *) wh;
584138568Ssam		int ac, tid;
585138568Ssam
586138568Ssam		ac = M_WME_GETAC(m);
587138568Ssam		/* map from access class/queue to 11e header priorty value */
588138568Ssam		tid = WME_AC_TO_TID(ac);
589138568Ssam		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
590138568Ssam		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
591138568Ssam			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
592138568Ssam		qwh->i_qos[1] = 0;
593138568Ssam		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
594138568Ssam
595138568Ssam		*(u_int16_t *)wh->i_seq =
596138568Ssam		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
597138568Ssam		ni->ni_txseqs[tid]++;
598138568Ssam	} else {
599138568Ssam		*(u_int16_t *)wh->i_seq =
600138568Ssam		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
601138568Ssam		ni->ni_txseqs[0]++;
602138568Ssam	}
603139527Ssam	if (key != NULL) {
604139527Ssam		/*
605139527Ssam		 * IEEE 802.1X: send EAPOL frames always in the clear.
606139527Ssam		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
607139527Ssam		 */
608139527Ssam		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
609139527Ssam		    ((ic->ic_flags & IEEE80211_F_WPA) &&
610141660Ssam		     (ic->ic_opmode == IEEE80211_M_STA ?
611141660Ssam		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
612139527Ssam			wh->i_fc[1] |= IEEE80211_FC1_WEP;
613139527Ssam			/* XXX do fragmentation */
614147045Ssam			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
615139527Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
616139527Ssam				    "[%s] enmic failed, discard frame\n",
617139527Ssam				    ether_sprintf(eh.ether_dhost));
618139527Ssam				ic->ic_stats.is_crypto_enmicfail++;
619139527Ssam				goto bad;
620139527Ssam			}
621139527Ssam		}
622139527Ssam	}
623138568Ssam
624138568Ssam	IEEE80211_NODE_STAT(ni, tx_data);
625138568Ssam	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
626138568Ssam
627116742Ssam	return m;
628119150Ssambad:
629119150Ssam	if (m != NULL)
630119150Ssam		m_freem(m);
631119150Ssam	return NULL;
632116742Ssam}
633116742Ssam
634116742Ssam/*
635116742Ssam * Add a supported rates element id to a frame.
636116742Ssam */
637138568Ssamstatic u_int8_t *
638116742Ssamieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
639116742Ssam{
640116742Ssam	int nrates;
641116742Ssam
642116742Ssam	*frm++ = IEEE80211_ELEMID_RATES;
643116742Ssam	nrates = rs->rs_nrates;
644116742Ssam	if (nrates > IEEE80211_RATE_SIZE)
645116742Ssam		nrates = IEEE80211_RATE_SIZE;
646116742Ssam	*frm++ = nrates;
647116742Ssam	memcpy(frm, rs->rs_rates, nrates);
648116742Ssam	return frm + nrates;
649116742Ssam}
650116742Ssam
651116742Ssam/*
652116742Ssam * Add an extended supported rates element id to a frame.
653116742Ssam */
654138568Ssamstatic u_int8_t *
655116742Ssamieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
656116742Ssam{
657116742Ssam	/*
658116742Ssam	 * Add an extended supported rates element if operating in 11g mode.
659116742Ssam	 */
660116742Ssam	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
661116742Ssam		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
662116742Ssam		*frm++ = IEEE80211_ELEMID_XRATES;
663116742Ssam		*frm++ = nrates;
664116742Ssam		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
665116742Ssam		frm += nrates;
666116742Ssam	}
667116742Ssam	return frm;
668116742Ssam}
669116742Ssam
670116742Ssam/*
671116742Ssam * Add an ssid elemet to a frame.
672116742Ssam */
673116742Ssamstatic u_int8_t *
674116742Ssamieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
675116742Ssam{
676116742Ssam	*frm++ = IEEE80211_ELEMID_SSID;
677116742Ssam	*frm++ = len;
678116742Ssam	memcpy(frm, ssid, len);
679116742Ssam	return frm + len;
680116742Ssam}
681116742Ssam
682138568Ssam/*
683138568Ssam * Add an erp element to a frame.
684138568Ssam */
685138568Ssamstatic u_int8_t *
686138568Ssamieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
687116742Ssam{
688138568Ssam	u_int8_t erp;
689116742Ssam
690138568Ssam	*frm++ = IEEE80211_ELEMID_ERP;
691138568Ssam	*frm++ = 1;
692138568Ssam	erp = 0;
693138568Ssam	if (ic->ic_nonerpsta != 0)
694138568Ssam		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
695138568Ssam	if (ic->ic_flags & IEEE80211_F_USEPROT)
696138568Ssam		erp |= IEEE80211_ERP_USE_PROTECTION;
697138568Ssam	if (ic->ic_flags & IEEE80211_F_USEBARKER)
698138568Ssam		erp |= IEEE80211_ERP_LONG_PREAMBLE;
699138568Ssam	*frm++ = erp;
700138568Ssam	return frm;
701138568Ssam}
702138568Ssam
703138568Ssamstatic u_int8_t *
704138568Ssamieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
705138568Ssam{
706138568Ssam#define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
707138568Ssam#define	ADDSHORT(frm, v) do {			\
708138568Ssam	frm[0] = (v) & 0xff;			\
709138568Ssam	frm[1] = (v) >> 8;			\
710138568Ssam	frm += 2;				\
711138568Ssam} while (0)
712138568Ssam#define	ADDSELECTOR(frm, sel) do {		\
713138568Ssam	memcpy(frm, sel, 4);			\
714138568Ssam	frm += 4;				\
715138568Ssam} while (0)
716138568Ssam	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
717138568Ssam	static const u_int8_t cipher_suite[][4] = {
718138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
719138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
720138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
721138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
722138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
723138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_NULL },
724138568Ssam	};
725138568Ssam	static const u_int8_t wep104_suite[4] =
726138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
727138568Ssam	static const u_int8_t key_mgt_unspec[4] =
728138568Ssam		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
729138568Ssam	static const u_int8_t key_mgt_psk[4] =
730138568Ssam		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
731138568Ssam	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
732138568Ssam	u_int8_t *frm = ie;
733138568Ssam	u_int8_t *selcnt;
734138568Ssam
735138568Ssam	*frm++ = IEEE80211_ELEMID_VENDOR;
736138568Ssam	*frm++ = 0;				/* length filled in below */
737138568Ssam	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
738138568Ssam	frm += sizeof(oui);
739138568Ssam	ADDSHORT(frm, WPA_VERSION);
740138568Ssam
741138568Ssam	/* XXX filter out CKIP */
742138568Ssam
743138568Ssam	/* multicast cipher */
744138568Ssam	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
745138568Ssam	    rsn->rsn_mcastkeylen >= 13)
746138568Ssam		ADDSELECTOR(frm, wep104_suite);
747116742Ssam	else
748138568Ssam		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
749138568Ssam
750138568Ssam	/* unicast cipher list */
751138568Ssam	selcnt = frm;
752138568Ssam	ADDSHORT(frm, 0);			/* selector count */
753138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
754138568Ssam		selcnt[0]++;
755138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
756138568Ssam	}
757138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
758138568Ssam		selcnt[0]++;
759138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
760138568Ssam	}
761138568Ssam
762138568Ssam	/* authenticator selector list */
763138568Ssam	selcnt = frm;
764138568Ssam	ADDSHORT(frm, 0);			/* selector count */
765138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
766138568Ssam		selcnt[0]++;
767138568Ssam		ADDSELECTOR(frm, key_mgt_unspec);
768138568Ssam	}
769138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
770138568Ssam		selcnt[0]++;
771138568Ssam		ADDSELECTOR(frm, key_mgt_psk);
772138568Ssam	}
773138568Ssam
774138568Ssam	/* optional capabilities */
775147066Ssam	if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
776138568Ssam		ADDSHORT(frm, rsn->rsn_caps);
777138568Ssam
778138568Ssam	/* calculate element length */
779138568Ssam	ie[1] = frm - ie - 2;
780138568Ssam	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
781138609Ssam		("WPA IE too big, %u > %zu",
782138568Ssam		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
783138568Ssam	return frm;
784138568Ssam#undef ADDSHORT
785138568Ssam#undef ADDSELECTOR
786138568Ssam#undef WPA_OUI_BYTES
787116742Ssam}
788116742Ssam
789138568Ssamstatic u_int8_t *
790138568Ssamieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
791138568Ssam{
792138568Ssam#define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
793138568Ssam#define	ADDSHORT(frm, v) do {			\
794138568Ssam	frm[0] = (v) & 0xff;			\
795138568Ssam	frm[1] = (v) >> 8;			\
796138568Ssam	frm += 2;				\
797138568Ssam} while (0)
798138568Ssam#define	ADDSELECTOR(frm, sel) do {		\
799138568Ssam	memcpy(frm, sel, 4);			\
800138568Ssam	frm += 4;				\
801138568Ssam} while (0)
802138568Ssam	static const u_int8_t cipher_suite[][4] = {
803138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
804138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
805138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
806138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
807138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
808138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_NULL },
809138568Ssam	};
810138568Ssam	static const u_int8_t wep104_suite[4] =
811138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
812138568Ssam	static const u_int8_t key_mgt_unspec[4] =
813138568Ssam		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
814138568Ssam	static const u_int8_t key_mgt_psk[4] =
815138568Ssam		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
816138568Ssam	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
817138568Ssam	u_int8_t *frm = ie;
818138568Ssam	u_int8_t *selcnt;
819138568Ssam
820138568Ssam	*frm++ = IEEE80211_ELEMID_RSN;
821138568Ssam	*frm++ = 0;				/* length filled in below */
822138568Ssam	ADDSHORT(frm, RSN_VERSION);
823138568Ssam
824138568Ssam	/* XXX filter out CKIP */
825138568Ssam
826138568Ssam	/* multicast cipher */
827138568Ssam	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
828138568Ssam	    rsn->rsn_mcastkeylen >= 13)
829138568Ssam		ADDSELECTOR(frm, wep104_suite);
830138568Ssam	else
831138568Ssam		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
832138568Ssam
833138568Ssam	/* unicast cipher list */
834138568Ssam	selcnt = frm;
835138568Ssam	ADDSHORT(frm, 0);			/* selector count */
836138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
837138568Ssam		selcnt[0]++;
838138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
839138568Ssam	}
840138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
841138568Ssam		selcnt[0]++;
842138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
843138568Ssam	}
844138568Ssam
845138568Ssam	/* authenticator selector list */
846138568Ssam	selcnt = frm;
847138568Ssam	ADDSHORT(frm, 0);			/* selector count */
848138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
849138568Ssam		selcnt[0]++;
850138568Ssam		ADDSELECTOR(frm, key_mgt_unspec);
851138568Ssam	}
852138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
853138568Ssam		selcnt[0]++;
854138568Ssam		ADDSELECTOR(frm, key_mgt_psk);
855138568Ssam	}
856138568Ssam
857138568Ssam	/* optional capabilities */
858147066Ssam	ADDSHORT(frm, rsn->rsn_caps);
859138568Ssam	/* XXX PMKID */
860138568Ssam
861138568Ssam	/* calculate element length */
862138568Ssam	ie[1] = frm - ie - 2;
863138568Ssam	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
864138609Ssam		("RSN IE too big, %u > %zu",
865138568Ssam		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
866138568Ssam	return frm;
867138568Ssam#undef ADDSELECTOR
868138568Ssam#undef ADDSHORT
869138568Ssam#undef RSN_OUI_BYTES
870138568Ssam}
871138568Ssam
872119150Ssam/*
873138568Ssam * Add a WPA/RSN element to a frame.
874138568Ssam */
875138568Ssamstatic u_int8_t *
876138568Ssamieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
877138568Ssam{
878138568Ssam
879138568Ssam	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
880138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA2)
881138568Ssam		frm = ieee80211_setup_rsn_ie(ic, frm);
882138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA1)
883138568Ssam		frm = ieee80211_setup_wpa_ie(ic, frm);
884138568Ssam	return frm;
885138568Ssam}
886138568Ssam
887138568Ssam#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
888138568Ssam/*
889138568Ssam * Add a WME information element to a frame.
890138568Ssam */
891138568Ssamstatic u_int8_t *
892138568Ssamieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
893138568Ssam{
894138568Ssam	static const struct ieee80211_wme_info info = {
895138568Ssam		.wme_id		= IEEE80211_ELEMID_VENDOR,
896138568Ssam		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
897138568Ssam		.wme_oui	= { WME_OUI_BYTES },
898138568Ssam		.wme_type	= WME_OUI_TYPE,
899138568Ssam		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
900138568Ssam		.wme_version	= WME_VERSION,
901138568Ssam		.wme_info	= 0,
902138568Ssam	};
903138568Ssam	memcpy(frm, &info, sizeof(info));
904138568Ssam	return frm + sizeof(info);
905138568Ssam}
906138568Ssam
907138568Ssam/*
908138568Ssam * Add a WME parameters element to a frame.
909138568Ssam */
910138568Ssamstatic u_int8_t *
911138568Ssamieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
912138568Ssam{
913138568Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
914138568Ssam#define	ADDSHORT(frm, v) do {			\
915138568Ssam	frm[0] = (v) & 0xff;			\
916138568Ssam	frm[1] = (v) >> 8;			\
917138568Ssam	frm += 2;				\
918138568Ssam} while (0)
919138568Ssam	/* NB: this works 'cuz a param has an info at the front */
920138568Ssam	static const struct ieee80211_wme_info param = {
921138568Ssam		.wme_id		= IEEE80211_ELEMID_VENDOR,
922138568Ssam		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
923138568Ssam		.wme_oui	= { WME_OUI_BYTES },
924138568Ssam		.wme_type	= WME_OUI_TYPE,
925138568Ssam		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
926138568Ssam		.wme_version	= WME_VERSION,
927138568Ssam	};
928138568Ssam	int i;
929138568Ssam
930138568Ssam	memcpy(frm, &param, sizeof(param));
931138568Ssam	frm += __offsetof(struct ieee80211_wme_info, wme_info);
932138568Ssam	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
933138568Ssam	*frm++ = 0;					/* reserved field */
934138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
935138568Ssam		const struct wmeParams *ac =
936138568Ssam		       &wme->wme_bssChanParams.cap_wmeParams[i];
937138568Ssam		*frm++ = SM(i, WME_PARAM_ACI)
938138568Ssam		       | SM(ac->wmep_acm, WME_PARAM_ACM)
939138568Ssam		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
940138568Ssam		       ;
941138568Ssam		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
942138568Ssam		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
943138568Ssam		       ;
944138568Ssam		ADDSHORT(frm, ac->wmep_txopLimit);
945138568Ssam	}
946138568Ssam	return frm;
947138568Ssam#undef SM
948138568Ssam#undef ADDSHORT
949138568Ssam}
950138568Ssam#undef WME_OUI_BYTES
951138568Ssam
952138568Ssam/*
953148315Ssam * Send a probe request frame with the specified ssid
954148315Ssam * and any optional information element data.
955148315Ssam */
956148315Ssamint
957148315Ssamieee80211_send_probereq(struct ieee80211_node *ni,
958148315Ssam	const u_int8_t sa[IEEE80211_ADDR_LEN],
959148315Ssam	const u_int8_t da[IEEE80211_ADDR_LEN],
960148315Ssam	const u_int8_t bssid[IEEE80211_ADDR_LEN],
961148315Ssam	const u_int8_t *ssid, size_t ssidlen,
962148315Ssam	const void *optie, size_t optielen)
963148315Ssam{
964148315Ssam	struct ieee80211com *ic = ni->ni_ic;
965148315Ssam	enum ieee80211_phymode mode;
966148315Ssam	struct ieee80211_frame *wh;
967148315Ssam	struct mbuf *m;
968148315Ssam	u_int8_t *frm;
969148315Ssam
970148315Ssam	/*
971148315Ssam	 * Hold a reference on the node so it doesn't go away until after
972148315Ssam	 * the xmit is complete all the way in the driver.  On error we
973148315Ssam	 * will remove our reference.
974148315Ssam	 */
975148315Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
976148315Ssam		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
977148315Ssam		__func__, __LINE__,
978148315Ssam		ni, ether_sprintf(ni->ni_macaddr),
979148315Ssam		ieee80211_node_refcnt(ni)+1);
980148315Ssam	ieee80211_ref_node(ni);
981148315Ssam
982148315Ssam	/*
983148315Ssam	 * prreq frame format
984148315Ssam	 *	[tlv] ssid
985148315Ssam	 *	[tlv] supported rates
986148315Ssam	 *	[tlv] extended supported rates
987148315Ssam	 *	[tlv] user-specified ie's
988148315Ssam	 */
989148315Ssam	m = ieee80211_getmgtframe(&frm,
990148315Ssam		 2 + IEEE80211_NWID_LEN
991148315Ssam	       + 2 + IEEE80211_RATE_SIZE
992148315Ssam	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
993148315Ssam	       + (optie != NULL ? optielen : 0)
994148315Ssam	);
995148315Ssam	if (m == NULL) {
996148315Ssam		ic->ic_stats.is_tx_nobuf++;
997148315Ssam		ieee80211_free_node(ni);
998148315Ssam		return ENOMEM;
999148315Ssam	}
1000148315Ssam
1001148315Ssam	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1002148936Ssam	mode = ieee80211_chan2mode(ic, ic->ic_curchan);
1003148315Ssam	frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
1004148315Ssam	frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
1005148315Ssam
1006148315Ssam	if (optie != NULL) {
1007148315Ssam		memcpy(frm, optie, optielen);
1008148315Ssam		frm += optielen;
1009148315Ssam	}
1010148315Ssam	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1011148315Ssam
1012148315Ssam	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1013148315Ssam	if (m == NULL)
1014148315Ssam		return ENOMEM;
1015148315Ssam	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
1016148315Ssam	m->m_pkthdr.rcvif = (void *)ni;
1017148315Ssam
1018148315Ssam	wh = mtod(m, struct ieee80211_frame *);
1019148315Ssam	ieee80211_send_setup(ic, ni, wh,
1020148315Ssam		IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1021148315Ssam		sa, da, bssid);
1022148315Ssam	/* XXX power management? */
1023148315Ssam
1024148315Ssam	IEEE80211_NODE_STAT(ni, tx_probereq);
1025148315Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);
1026148315Ssam
1027148315Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1028148315Ssam	    "[%s] send probe req on channel %u\n",
1029148315Ssam	    ether_sprintf(wh->i_addr1),
1030148936Ssam	    ieee80211_chan2ieee(ic, ic->ic_curchan));
1031148315Ssam
1032148315Ssam	IF_ENQUEUE(&ic->ic_mgtq, m);
1033148315Ssam	if_start(ic->ic_ifp);
1034148315Ssam	return 0;
1035148315Ssam}
1036148315Ssam
1037148315Ssam/*
1038119150Ssam * Send a management frame.  The node is for the destination (or ic_bss
1039119150Ssam * when in station mode).  Nodes other than ic_bss have their reference
1040119150Ssam * count bumped to reflect our use for an indeterminant time.
1041119150Ssam */
1042116742Ssamint
1043116742Ssamieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
1044116742Ssam	int type, int arg)
1045116742Ssam{
1046121180Ssam#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
1047116742Ssam	struct mbuf *m;
1048116742Ssam	u_int8_t *frm;
1049116742Ssam	u_int16_t capinfo;
1050138568Ssam	int has_challenge, is_shared_key, ret, timer, status;
1051116742Ssam
1052119150Ssam	KASSERT(ni != NULL, ("null node"));
1053119150Ssam
1054119150Ssam	/*
1055119150Ssam	 * Hold a reference on the node so it doesn't go away until after
1056119150Ssam	 * the xmit is complete all the way in the driver.  On error we
1057119150Ssam	 * will remove our reference.
1058119150Ssam	 */
1059138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
1060140766Ssam		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1061138568Ssam		__func__, __LINE__,
1062140766Ssam		ni, ether_sprintf(ni->ni_macaddr),
1063140766Ssam		ieee80211_node_refcnt(ni)+1);
1064138568Ssam	ieee80211_ref_node(ni);
1065138568Ssam
1066116742Ssam	timer = 0;
1067116742Ssam	switch (type) {
1068116742Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1069116742Ssam		/*
1070116742Ssam		 * probe response frame format
1071116742Ssam		 *	[8] time stamp
1072116742Ssam		 *	[2] beacon interval
1073116742Ssam		 *	[2] cabability information
1074116742Ssam		 *	[tlv] ssid
1075116742Ssam		 *	[tlv] supported rates
1076121178Ssam		 *	[tlv] parameter set (FH/DS)
1077116742Ssam		 *	[tlv] parameter set (IBSS)
1078138568Ssam		 *	[tlv] extended rate phy (ERP)
1079116742Ssam		 *	[tlv] extended supported rates
1080138568Ssam		 *	[tlv] WPA
1081144136Ssam		 *	[tlv] WME (optional)
1082116742Ssam		 */
1083138568Ssam		m = ieee80211_getmgtframe(&frm,
1084138568Ssam			 8
1085138568Ssam		       + sizeof(u_int16_t)
1086138568Ssam		       + sizeof(u_int16_t)
1087138568Ssam		       + 2 + IEEE80211_NWID_LEN
1088116742Ssam		       + 2 + IEEE80211_RATE_SIZE
1089138568Ssam		       + 7	/* max(7,3) */
1090116742Ssam		       + 6
1091138568Ssam		       + 3
1092138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1093138568Ssam		       /* XXX !WPA1+WPA2 fits w/o a cluster */
1094138568Ssam		       + (ic->ic_flags & IEEE80211_F_WPA ?
1095138568Ssam				2*sizeof(struct ieee80211_ie_wpa) : 0)
1096144136Ssam		       + sizeof(struct ieee80211_wme_param)
1097138568Ssam		);
1098116742Ssam		if (m == NULL)
1099138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1100116742Ssam
1101116742Ssam		memset(frm, 0, 8);	/* timestamp should be filled later */
1102116742Ssam		frm += 8;
1103116742Ssam		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1104116742Ssam		frm += 2;
1105116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS)
1106116742Ssam			capinfo = IEEE80211_CAPINFO_IBSS;
1107116742Ssam		else
1108116742Ssam			capinfo = IEEE80211_CAPINFO_ESS;
1109138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1110116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1111120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1112148936Ssam		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1113120070Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1114138568Ssam		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1115138568Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1116116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1117116742Ssam		frm += 2;
1118116742Ssam
1119116742Ssam		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1120116742Ssam				ic->ic_bss->ni_esslen);
1121138568Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1122116742Ssam
1123121178Ssam		if (ic->ic_phytype == IEEE80211_T_FH) {
1124121178Ssam                        *frm++ = IEEE80211_ELEMID_FHPARMS;
1125121178Ssam                        *frm++ = 5;
1126121178Ssam                        *frm++ = ni->ni_fhdwell & 0x00ff;
1127121178Ssam                        *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1128121178Ssam                        *frm++ = IEEE80211_FH_CHANSET(
1129148936Ssam			    ieee80211_chan2ieee(ic, ic->ic_curchan));
1130121178Ssam                        *frm++ = IEEE80211_FH_CHANPAT(
1131148936Ssam			    ieee80211_chan2ieee(ic, ic->ic_curchan));
1132121178Ssam                        *frm++ = ni->ni_fhindex;
1133121178Ssam		} else {
1134121178Ssam			*frm++ = IEEE80211_ELEMID_DSPARMS;
1135121178Ssam			*frm++ = 1;
1136148936Ssam			*frm++ = ieee80211_chan2ieee(ic, ic->ic_curchan);
1137121178Ssam		}
1138121178Ssam
1139116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS) {
1140116742Ssam			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1141116742Ssam			*frm++ = 2;
1142116742Ssam			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1143116742Ssam		}
1144138568Ssam		if (ic->ic_flags & IEEE80211_F_WPA)
1145138568Ssam			frm = ieee80211_add_wpa(frm, ic);
1146138568Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G)
1147138568Ssam			frm = ieee80211_add_erp(frm, ic);
1148138568Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1149144136Ssam		if (ic->ic_flags & IEEE80211_F_WME)
1150144136Ssam			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1151116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1152116742Ssam		break;
1153116742Ssam
1154116742Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
1155138568Ssam		status = arg >> 16;
1156138568Ssam		arg &= 0xffff;
1157138568Ssam		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1158138568Ssam		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1159138568Ssam		    ni->ni_challenge != NULL);
1160138568Ssam
1161138568Ssam		/*
1162138568Ssam		 * Deduce whether we're doing open authentication or
1163138568Ssam		 * shared key authentication.  We do the latter if
1164138568Ssam		 * we're in the middle of a shared key authentication
1165138568Ssam		 * handshake or if we're initiating an authentication
1166138568Ssam		 * request and configured to use shared key.
1167138568Ssam		 */
1168138568Ssam		is_shared_key = has_challenge ||
1169138568Ssam		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1170138568Ssam		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1171138568Ssam		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1172138568Ssam
1173138568Ssam		m = ieee80211_getmgtframe(&frm,
1174138568Ssam			  3 * sizeof(u_int16_t)
1175138568Ssam			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1176138568Ssam				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1177138568Ssam		);
1178116742Ssam		if (m == NULL)
1179138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1180138568Ssam
1181138568Ssam		((u_int16_t *)frm)[0] =
1182138568Ssam		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1183138568Ssam		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
1184116742Ssam		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
1185138568Ssam		((u_int16_t *)frm)[2] = htole16(status);/* status */
1186138568Ssam
1187138568Ssam		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1188138568Ssam			((u_int16_t *)frm)[3] =
1189138568Ssam			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
1190138568Ssam			    IEEE80211_ELEMID_CHALLENGE);
1191138568Ssam			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1192138568Ssam			    IEEE80211_CHALLENGE_LEN);
1193138568Ssam			m->m_pkthdr.len = m->m_len =
1194138568Ssam				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1195138568Ssam			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1196138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1197138568Ssam				    "[%s] request encrypt frame (%s)\n",
1198138568Ssam				    ether_sprintf(ni->ni_macaddr), __func__);
1199138568Ssam				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1200138568Ssam			}
1201138568Ssam		} else
1202138568Ssam			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1203138568Ssam
1204138568Ssam		/* XXX not right for shared key */
1205138568Ssam		if (status == IEEE80211_STATUS_SUCCESS)
1206138568Ssam			IEEE80211_NODE_STAT(ni, tx_auth);
1207138568Ssam		else
1208138568Ssam			IEEE80211_NODE_STAT(ni, tx_auth_fail);
1209138568Ssam
1210116742Ssam		if (ic->ic_opmode == IEEE80211_M_STA)
1211116742Ssam			timer = IEEE80211_TRANS_WAIT;
1212116742Ssam		break;
1213116742Ssam
1214116742Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1215138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1216138568Ssam			"[%s] send station deauthenticate (reason %d)\n",
1217138568Ssam			ether_sprintf(ni->ni_macaddr), arg);
1218138568Ssam		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1219116742Ssam		if (m == NULL)
1220138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1221138568Ssam		*(u_int16_t *)frm = htole16(arg);	/* reason */
1222138568Ssam		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1223138568Ssam
1224138568Ssam		IEEE80211_NODE_STAT(ni, tx_deauth);
1225138568Ssam		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1226138568Ssam
1227148302Ssam		ieee80211_node_unauthorize(ni);		/* port closed */
1228116742Ssam		break;
1229116742Ssam
1230116742Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1231116742Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1232116742Ssam		/*
1233116742Ssam		 * asreq frame format
1234116742Ssam		 *	[2] capability information
1235116742Ssam		 *	[2] listen interval
1236116742Ssam		 *	[6*] current AP address (reassoc only)
1237116742Ssam		 *	[tlv] ssid
1238116742Ssam		 *	[tlv] supported rates
1239116742Ssam		 *	[tlv] extended supported rates
1240138568Ssam		 *	[tlv] WME
1241138568Ssam		 *	[tlv] user-specified ie's
1242116742Ssam		 */
1243138568Ssam		m = ieee80211_getmgtframe(&frm,
1244138568Ssam			 sizeof(u_int16_t)
1245116742Ssam		       + sizeof(u_int16_t)
1246116742Ssam		       + IEEE80211_ADDR_LEN
1247138568Ssam		       + 2 + IEEE80211_NWID_LEN
1248116742Ssam		       + 2 + IEEE80211_RATE_SIZE
1249138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1250138568Ssam		       + sizeof(struct ieee80211_wme_info)
1251138568Ssam		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1252138568Ssam		);
1253116742Ssam		if (m == NULL)
1254138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1255116742Ssam
1256116742Ssam		capinfo = 0;
1257116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS)
1258116742Ssam			capinfo |= IEEE80211_CAPINFO_IBSS;
1259116742Ssam		else		/* IEEE80211_M_STA */
1260116742Ssam			capinfo |= IEEE80211_CAPINFO_ESS;
1261138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1262116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1263120070Ssam		/*
1264120070Ssam		 * NB: Some 11a AP's reject the request when
1265120070Ssam		 *     short premable is set.
1266120070Ssam		 */
1267120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1268148936Ssam		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1269116742Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1270138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1271138568Ssam		    (ic->ic_caps & IEEE80211_C_SHSLOT))
1272116742Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1273116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1274116742Ssam		frm += 2;
1275116742Ssam
1276116742Ssam		*(u_int16_t *)frm = htole16(ic->ic_lintval);
1277116742Ssam		frm += 2;
1278116742Ssam
1279116742Ssam		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1280116742Ssam			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1281116742Ssam			frm += IEEE80211_ADDR_LEN;
1282116742Ssam		}
1283116742Ssam
1284116742Ssam		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1285116742Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1286116742Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1287138568Ssam		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1288138568Ssam			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1289138568Ssam		if (ic->ic_opt_ie != NULL) {
1290138568Ssam			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1291138568Ssam			frm += ic->ic_opt_ie_len;
1292138568Ssam		}
1293116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1294116742Ssam
1295116742Ssam		timer = IEEE80211_TRANS_WAIT;
1296116742Ssam		break;
1297116742Ssam
1298116742Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1299116742Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1300116742Ssam		/*
1301116742Ssam		 * asreq frame format
1302116742Ssam		 *	[2] capability information
1303116742Ssam		 *	[2] status
1304116742Ssam		 *	[2] association ID
1305116742Ssam		 *	[tlv] supported rates
1306116742Ssam		 *	[tlv] extended supported rates
1307138568Ssam		 *	[tlv] WME (if enabled and STA enabled)
1308116742Ssam		 */
1309138568Ssam		m = ieee80211_getmgtframe(&frm,
1310138568Ssam			 sizeof(u_int16_t)
1311116742Ssam		       + sizeof(u_int16_t)
1312116742Ssam		       + sizeof(u_int16_t)
1313116742Ssam		       + 2 + IEEE80211_RATE_SIZE
1314138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1315138568Ssam		       + sizeof(struct ieee80211_wme_param)
1316138568Ssam		);
1317116742Ssam		if (m == NULL)
1318138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1319116742Ssam
1320116742Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1321138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1322116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1323120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1324148936Ssam		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
1325120070Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1326138568Ssam		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1327138568Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1328116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1329116742Ssam		frm += 2;
1330116742Ssam
1331116742Ssam		*(u_int16_t *)frm = htole16(arg);	/* status */
1332116742Ssam		frm += 2;
1333116742Ssam
1334138568Ssam		if (arg == IEEE80211_STATUS_SUCCESS) {
1335116742Ssam			*(u_int16_t *)frm = htole16(ni->ni_associd);
1336138568Ssam			IEEE80211_NODE_STAT(ni, tx_assoc);
1337138568Ssam		} else
1338138568Ssam			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1339116742Ssam		frm += 2;
1340116742Ssam
1341119150Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1342119150Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1343138568Ssam		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1344138568Ssam			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1345116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1346116742Ssam		break;
1347116742Ssam
1348116742Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1349138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1350138568Ssam			"[%s] send station disassociate (reason %d)\n",
1351138568Ssam			ether_sprintf(ni->ni_macaddr), arg);
1352138568Ssam		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1353116742Ssam		if (m == NULL)
1354138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1355138568Ssam		*(u_int16_t *)frm = htole16(arg);	/* reason */
1356138568Ssam		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1357138568Ssam
1358138568Ssam		IEEE80211_NODE_STAT(ni, tx_disassoc);
1359138568Ssam		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1360116742Ssam		break;
1361116742Ssam
1362116742Ssam	default:
1363138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1364138568Ssam			"[%s] invalid mgmt frame type %u\n",
1365138568Ssam			ether_sprintf(ni->ni_macaddr), type);
1366121180Ssam		senderr(EINVAL, is_tx_unknownmgt);
1367119150Ssam		/* NOTREACHED */
1368116742Ssam	}
1369116742Ssam
1370138568Ssam	ret = ieee80211_mgmt_output(ic, ni, m, type);
1371119150Ssam	if (ret == 0) {
1372119150Ssam		if (timer)
1373119150Ssam			ic->ic_mgt_timer = timer;
1374119150Ssam	} else {
1375119150Ssambad:
1376138568Ssam		ieee80211_free_node(ni);
1377119150Ssam	}
1378116742Ssam	return ret;
1379119150Ssam#undef senderr
1380116742Ssam}
1381138568Ssam
1382138568Ssam/*
1383138568Ssam * Allocate a beacon frame and fillin the appropriate bits.
1384138568Ssam */
1385138568Ssamstruct mbuf *
1386138568Ssamieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1387138568Ssam	struct ieee80211_beacon_offsets *bo)
1388138568Ssam{
1389138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1390138568Ssam	struct ieee80211_frame *wh;
1391138568Ssam	struct mbuf *m;
1392138568Ssam	int pktlen;
1393138568Ssam	u_int8_t *frm, *efrm;
1394138568Ssam	u_int16_t capinfo;
1395138568Ssam	struct ieee80211_rateset *rs;
1396138568Ssam
1397138568Ssam	/*
1398138568Ssam	 * beacon frame format
1399138568Ssam	 *	[8] time stamp
1400138568Ssam	 *	[2] beacon interval
1401138568Ssam	 *	[2] cabability information
1402138568Ssam	 *	[tlv] ssid
1403138568Ssam	 *	[tlv] supported rates
1404138568Ssam	 *	[3] parameter set (DS)
1405138568Ssam	 *	[tlv] parameter set (IBSS/TIM)
1406138568Ssam	 *	[tlv] extended rate phy (ERP)
1407138568Ssam	 *	[tlv] extended supported rates
1408138568Ssam	 *	[tlv] WME parameters
1409138568Ssam	 *	[tlv] WPA/RSN parameters
1410138568Ssam	 * XXX Vendor-specific OIDs (e.g. Atheros)
1411138568Ssam	 * NB: we allocate the max space required for the TIM bitmap.
1412138568Ssam	 */
1413138568Ssam	rs = &ni->ni_rates;
1414138568Ssam	pktlen =   8					/* time stamp */
1415138568Ssam		 + sizeof(u_int16_t)			/* beacon interval */
1416138568Ssam		 + sizeof(u_int16_t)			/* capabilities */
1417138568Ssam		 + 2 + ni->ni_esslen			/* ssid */
1418138568Ssam	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
1419138568Ssam	         + 2 + 1				/* DS parameters */
1420138568Ssam		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
1421138568Ssam		 + 2 + 1				/* ERP */
1422138568Ssam	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1423138568Ssam		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
1424138568Ssam			sizeof(struct ieee80211_wme_param) : 0)
1425138568Ssam		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
1426138568Ssam			2*sizeof(struct ieee80211_ie_wpa) : 0)
1427138568Ssam		 ;
1428138568Ssam	m = ieee80211_getmgtframe(&frm, pktlen);
1429138568Ssam	if (m == NULL) {
1430138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1431138568Ssam			"%s: cannot get buf; size %u\n", __func__, pktlen);
1432138568Ssam		ic->ic_stats.is_tx_nobuf++;
1433138568Ssam		return NULL;
1434138568Ssam	}
1435138568Ssam
1436138568Ssam	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
1437138568Ssam	frm += 8;
1438138568Ssam	*(u_int16_t *)frm = htole16(ni->ni_intval);
1439138568Ssam	frm += 2;
1440138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
1441138568Ssam		capinfo = IEEE80211_CAPINFO_IBSS;
1442138568Ssam	else
1443138568Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1444138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1445138568Ssam		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1446138568Ssam	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1447138568Ssam	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1448138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1449138568Ssam	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1450138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1451138568Ssam	bo->bo_caps = (u_int16_t *)frm;
1452138568Ssam	*(u_int16_t *)frm = htole16(capinfo);
1453138568Ssam	frm += 2;
1454138568Ssam	*frm++ = IEEE80211_ELEMID_SSID;
1455138568Ssam	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1456138568Ssam		*frm++ = ni->ni_esslen;
1457138568Ssam		memcpy(frm, ni->ni_essid, ni->ni_esslen);
1458138568Ssam		frm += ni->ni_esslen;
1459138568Ssam	} else
1460138568Ssam		*frm++ = 0;
1461138568Ssam	frm = ieee80211_add_rates(frm, rs);
1462138568Ssam	if (ic->ic_curmode != IEEE80211_MODE_FH) {
1463138568Ssam		*frm++ = IEEE80211_ELEMID_DSPARMS;
1464138568Ssam		*frm++ = 1;
1465138568Ssam		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1466138568Ssam	}
1467138568Ssam	bo->bo_tim = frm;
1468138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
1469138568Ssam		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1470138568Ssam		*frm++ = 2;
1471138568Ssam		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1472138568Ssam		bo->bo_tim_len = 0;
1473138568Ssam	} else {
1474138568Ssam		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1475138568Ssam
1476138568Ssam		tie->tim_ie = IEEE80211_ELEMID_TIM;
1477138568Ssam		tie->tim_len = 4;	/* length */
1478138568Ssam		tie->tim_count = 0;	/* DTIM count */
1479138568Ssam		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
1480138568Ssam		tie->tim_bitctl = 0;	/* bitmap control */
1481138568Ssam		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
1482138568Ssam		frm += sizeof(struct ieee80211_tim_ie);
1483138568Ssam		bo->bo_tim_len = 1;
1484138568Ssam	}
1485138568Ssam	bo->bo_trailer = frm;
1486138568Ssam	if (ic->ic_flags & IEEE80211_F_WME) {
1487138568Ssam		bo->bo_wme = frm;
1488138568Ssam		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1489140764Ssam		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1490138568Ssam	}
1491138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA)
1492138568Ssam		frm = ieee80211_add_wpa(frm, ic);
1493153973Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G) {
1494153973Ssam		bo->bo_erp = frm;
1495138568Ssam		frm = ieee80211_add_erp(frm, ic);
1496153973Ssam	}
1497138568Ssam	efrm = ieee80211_add_xrates(frm, rs);
1498138568Ssam	bo->bo_trailer_len = efrm - bo->bo_trailer;
1499138568Ssam	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1500138568Ssam
1501138568Ssam	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1502138568Ssam	KASSERT(m != NULL, ("no space for 802.11 header?"));
1503138568Ssam	wh = mtod(m, struct ieee80211_frame *);
1504138568Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1505138568Ssam	    IEEE80211_FC0_SUBTYPE_BEACON;
1506138568Ssam	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1507138568Ssam	*(u_int16_t *)wh->i_dur = 0;
1508138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1509138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1510138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1511138568Ssam	*(u_int16_t *)wh->i_seq = 0;
1512138568Ssam
1513138568Ssam	return m;
1514138568Ssam}
1515138568Ssam
1516138568Ssam/*
1517138568Ssam * Update the dynamic parts of a beacon frame based on the current state.
1518138568Ssam */
1519138568Ssamint
1520138568Ssamieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1521138568Ssam	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1522138568Ssam{
1523138568Ssam	int len_changed = 0;
1524138568Ssam	u_int16_t capinfo;
1525138568Ssam
1526138568Ssam	IEEE80211_BEACON_LOCK(ic);
1527138568Ssam	/* XXX faster to recalculate entirely or just changes? */
1528138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
1529138568Ssam		capinfo = IEEE80211_CAPINFO_IBSS;
1530138568Ssam	else
1531138568Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1532138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1533138568Ssam		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1534138568Ssam	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1535138568Ssam	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1536138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1537138568Ssam	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1538138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1539138568Ssam	*bo->bo_caps = htole16(capinfo);
1540138568Ssam
1541138568Ssam	if (ic->ic_flags & IEEE80211_F_WME) {
1542138568Ssam		struct ieee80211_wme_state *wme = &ic->ic_wme;
1543138568Ssam
1544138568Ssam		/*
1545138568Ssam		 * Check for agressive mode change.  When there is
1546138568Ssam		 * significant high priority traffic in the BSS
1547138568Ssam		 * throttle back BE traffic by using conservative
1548138568Ssam		 * parameters.  Otherwise BE uses agressive params
1549138568Ssam		 * to optimize performance of legacy/non-QoS traffic.
1550138568Ssam		 */
1551138568Ssam		if (wme->wme_flags & WME_F_AGGRMODE) {
1552138568Ssam			if (wme->wme_hipri_traffic >
1553138568Ssam			    wme->wme_hipri_switch_thresh) {
1554138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1555138568Ssam				    "%s: traffic %u, disable aggressive mode\n",
1556138568Ssam				    __func__, wme->wme_hipri_traffic);
1557138568Ssam				wme->wme_flags &= ~WME_F_AGGRMODE;
1558138568Ssam				ieee80211_wme_updateparams_locked(ic);
1559138568Ssam				wme->wme_hipri_traffic =
1560138568Ssam					wme->wme_hipri_switch_hysteresis;
1561138568Ssam			} else
1562138568Ssam				wme->wme_hipri_traffic = 0;
1563138568Ssam		} else {
1564138568Ssam			if (wme->wme_hipri_traffic <=
1565138568Ssam			    wme->wme_hipri_switch_thresh) {
1566138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1567138568Ssam				    "%s: traffic %u, enable aggressive mode\n",
1568138568Ssam				    __func__, wme->wme_hipri_traffic);
1569138568Ssam				wme->wme_flags |= WME_F_AGGRMODE;
1570138568Ssam				ieee80211_wme_updateparams_locked(ic);
1571138568Ssam				wme->wme_hipri_traffic = 0;
1572138568Ssam			} else
1573138568Ssam				wme->wme_hipri_traffic =
1574138568Ssam					wme->wme_hipri_switch_hysteresis;
1575138568Ssam		}
1576138568Ssam		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1577138568Ssam			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
1578138568Ssam			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1579138568Ssam		}
1580138568Ssam	}
1581138568Ssam
1582138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
1583138568Ssam		struct ieee80211_tim_ie *tie =
1584138568Ssam			(struct ieee80211_tim_ie *) bo->bo_tim;
1585138568Ssam		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1586138568Ssam			u_int timlen, timoff, i;
1587138568Ssam			/*
1588138568Ssam			 * ATIM/DTIM needs updating.  If it fits in the
1589138568Ssam			 * current space allocated then just copy in the
1590138568Ssam			 * new bits.  Otherwise we need to move any trailing
1591138568Ssam			 * data to make room.  Note that we know there is
1592138568Ssam			 * contiguous space because ieee80211_beacon_allocate
1593138568Ssam			 * insures there is space in the mbuf to write a
1594138568Ssam			 * maximal-size virtual bitmap (based on ic_max_aid).
1595138568Ssam			 */
1596138568Ssam			/*
1597138568Ssam			 * Calculate the bitmap size and offset, copy any
1598138568Ssam			 * trailer out of the way, and then copy in the
1599138568Ssam			 * new bitmap and update the information element.
1600138568Ssam			 * Note that the tim bitmap must contain at least
1601138568Ssam			 * one byte and any offset must be even.
1602138568Ssam			 */
1603138568Ssam			if (ic->ic_ps_pending != 0) {
1604138568Ssam				timoff = 128;		/* impossibly large */
1605138568Ssam				for (i = 0; i < ic->ic_tim_len; i++)
1606138568Ssam					if (ic->ic_tim_bitmap[i]) {
1607138568Ssam						timoff = i &~ 1;
1608138568Ssam						break;
1609138568Ssam					}
1610138568Ssam				KASSERT(timoff != 128, ("tim bitmap empty!"));
1611138568Ssam				for (i = ic->ic_tim_len-1; i >= timoff; i--)
1612138568Ssam					if (ic->ic_tim_bitmap[i])
1613138568Ssam						break;
1614138568Ssam				timlen = 1 + (i - timoff);
1615138568Ssam			} else {
1616138568Ssam				timoff = 0;
1617138568Ssam				timlen = 1;
1618138568Ssam			}
1619138568Ssam			if (timlen != bo->bo_tim_len) {
1620138568Ssam				/* copy up/down trailer */
1621153973Ssam				int adjust = tie->tim_bitmap+timlen
1622153973Ssam					   - bo->bo_trailer;
1623153973Ssam				ovbcopy(bo->bo_trailer, bo->bo_trailer+adjust,
1624138568Ssam					bo->bo_trailer_len);
1625153973Ssam				bo->bo_trailer += adjust;
1626153973Ssam				bo->bo_wme += adjust;
1627153973Ssam				bo->bo_erp += adjust;
1628138568Ssam				bo->bo_tim_len = timlen;
1629138568Ssam
1630138568Ssam				/* update information element */
1631138568Ssam				tie->tim_len = 3 + timlen;
1632138568Ssam				tie->tim_bitctl = timoff;
1633138568Ssam				len_changed = 1;
1634138568Ssam			}
1635138568Ssam			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1636138568Ssam				bo->bo_tim_len);
1637138568Ssam
1638138568Ssam			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1639138568Ssam
1640138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1641138568Ssam				"%s: TIM updated, pending %u, off %u, len %u\n",
1642138568Ssam				__func__, ic->ic_ps_pending, timoff, timlen);
1643138568Ssam		}
1644138568Ssam		/* count down DTIM period */
1645138568Ssam		if (tie->tim_count == 0)
1646138568Ssam			tie->tim_count = tie->tim_period - 1;
1647138568Ssam		else
1648138568Ssam			tie->tim_count--;
1649138568Ssam		/* update state for buffered multicast frames on DTIM */
1650153139Ssam		if (mcast && tie->tim_count == 0)
1651138568Ssam			tie->tim_bitctl |= 1;
1652138568Ssam		else
1653138568Ssam			tie->tim_bitctl &= ~1;
1654153973Ssam		if (ic->ic_flags_ext & IEEE80211_FEXT_ERPUPDATE) {
1655153973Ssam			/*
1656153973Ssam			 * ERP element needs updating.
1657153973Ssam			 */
1658153973Ssam			(void) ieee80211_add_erp(bo->bo_erp, ic);
1659153973Ssam			ic->ic_flags_ext &= ~IEEE80211_FEXT_ERPUPDATE;
1660153973Ssam		}
1661138568Ssam	}
1662138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1663138568Ssam
1664138568Ssam	return len_changed;
1665138568Ssam}
1666138568Ssam
1667138568Ssam/*
1668138568Ssam * Save an outbound packet for a node in power-save sleep state.
1669138568Ssam * The new packet is placed on the node's saved queue, and the TIM
1670138568Ssam * is changed, if necessary.
1671138568Ssam */
1672138568Ssamvoid
1673138568Ssamieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1674138568Ssam		  struct mbuf *m)
1675138568Ssam{
1676138568Ssam	int qlen, age;
1677138568Ssam
1678138568Ssam	IEEE80211_NODE_SAVEQ_LOCK(ni);
1679138568Ssam	if (_IF_QFULL(&ni->ni_savedq)) {
1680138568Ssam		_IF_DROP(&ni->ni_savedq);
1681138568Ssam		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1682138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1683138568Ssam			"[%s] pwr save q overflow, drops %d (size %d)\n",
1684138568Ssam			ether_sprintf(ni->ni_macaddr),
1685138568Ssam			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1686138568Ssam#ifdef IEEE80211_DEBUG
1687138568Ssam		if (ieee80211_msg_dumppkts(ic))
1688138568Ssam			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1689138568Ssam#endif
1690138568Ssam		m_freem(m);
1691138568Ssam		return;
1692138568Ssam	}
1693138568Ssam	/*
1694138568Ssam	 * Tag the frame with it's expiry time and insert
1695138568Ssam	 * it in the queue.  The aging interval is 4 times
1696138568Ssam	 * the listen interval specified by the station.
1697138568Ssam	 * Frames that sit around too long are reclaimed
1698138568Ssam	 * using this information.
1699138568Ssam	 */
1700138568Ssam	/* XXX handle overflow? */
1701148843Ssam	age = ((ni->ni_intval * ic->ic_bintval) << 2) / 1024; /* TU -> secs */
1702138568Ssam	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1703138568Ssam	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1704138568Ssam
1705138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1706148843Ssam		"[%s] save frame with age %d, %u now queued\n",
1707148843Ssam		ether_sprintf(ni->ni_macaddr), age, qlen);
1708138568Ssam
1709138568Ssam	if (qlen == 1)
1710148304Ssam		ic->ic_set_tim(ni, 1);
1711138568Ssam}
1712