ieee80211_output.c revision 147789
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 147789 2005-07-06 01:55:17Z 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/*
81119150Ssam * Send a management frame to the specified node.  The node pointer
82119150Ssam * must have a reference as the pointer will be passed to the driver
83119150Ssam * and potentially held for a long time.  If the frame is successfully
84119150Ssam * dispatched to the driver, then it is responsible for freeing the
85119150Ssam * reference (and potentially free'ing up any associated storage).
86119150Ssam */
87119150Ssamstatic int
88138568Ssamieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
89116742Ssam    struct mbuf *m, int type)
90116742Ssam{
91138568Ssam	struct ifnet *ifp = ic->ic_ifp;
92116742Ssam	struct ieee80211_frame *wh;
93116742Ssam
94119150Ssam	KASSERT(ni != NULL, ("null node"));
95116742Ssam
96119150Ssam	/*
97119150Ssam	 * Yech, hack alert!  We want to pass the node down to the
98119150Ssam	 * driver's start routine.  If we don't do so then the start
99119150Ssam	 * routine must immediately look it up again and that can
100119150Ssam	 * cause a lock order reversal if, for example, this frame
101119150Ssam	 * is being sent because the station is being timedout and
102119150Ssam	 * the frame being sent is a DEAUTH message.  We could stick
103119150Ssam	 * this in an m_tag and tack that on to the mbuf.  However
104119150Ssam	 * that's rather expensive to do for every frame so instead
105119150Ssam	 * we stuff it in the rcvif field since outbound frames do
106119150Ssam	 * not (presently) use this.
107119150Ssam	 */
108116742Ssam	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
109116742Ssam	if (m == NULL)
110116742Ssam		return ENOMEM;
111119150Ssam	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
112119150Ssam	m->m_pkthdr.rcvif = (void *)ni;
113119150Ssam
114116742Ssam	wh = mtod(m, struct ieee80211_frame *);
115116742Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
116116742Ssam	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
117116742Ssam	*(u_int16_t *)wh->i_dur = 0;
118116742Ssam	*(u_int16_t *)wh->i_seq =
119138568Ssam	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
120138568Ssam	ni->ni_txseqs[0]++;
121138568Ssam	/*
122138568Ssam	 * Hack.  When sending PROBE_REQ frames while scanning we
123138568Ssam	 * explicitly force a broadcast rather than (as before) clobber
124138568Ssam	 * ni_macaddr and ni_bssid.  This is stopgap, we need a way
125138568Ssam	 * to communicate this directly rather than do something
126138568Ssam	 * implicit based on surrounding state.
127138568Ssam	 */
128138568Ssam	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ &&
129138568Ssam	    (ic->ic_flags & IEEE80211_F_SCAN)) {
130138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
131138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
132138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
133138568Ssam	} else {
134138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
135138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
136138568Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
137138568Ssam	}
138116742Ssam
139138568Ssam	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
140138568Ssam		m->m_flags &= ~M_LINK0;
141138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
142138568Ssam			"[%s] encrypting frame (%s)\n",
143138568Ssam			ether_sprintf(wh->i_addr1), __func__);
144138568Ssam		wh->i_fc[1] |= IEEE80211_FC1_WEP;
145138568Ssam	}
146116742Ssam#ifdef IEEE80211_DEBUG
147138568Ssam	/* avoid printing too many frames */
148138568Ssam	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
149138568Ssam	    ieee80211_msg_dumppkts(ic)) {
150138568Ssam		printf("[%s] send %s on channel %u\n",
151138568Ssam		    ether_sprintf(wh->i_addr1),
152138568Ssam		    ieee80211_mgt_subtype_name[
153138568Ssam			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
154138568Ssam				IEEE80211_FC0_SUBTYPE_SHIFT],
155138568Ssam		    ieee80211_chan2ieee(ic, ni->ni_chan));
156138568Ssam	}
157116742Ssam#endif
158138568Ssam	IEEE80211_NODE_STAT(ni, tx_mgmt);
159116742Ssam	IF_ENQUEUE(&ic->ic_mgtq, m);
160116742Ssam	ifp->if_timer = 1;
161132712Srwatson	if_start(ifp);
162116742Ssam	return 0;
163116742Ssam}
164116742Ssam
165119150Ssam/*
166138568Ssam * Send a null data frame to the specified node.
167119150Ssam */
168138568Ssamint
169138568Ssamieee80211_send_nulldata(struct ieee80211com *ic, struct ieee80211_node *ni)
170138568Ssam{
171138568Ssam	struct ifnet *ifp = ic->ic_ifp;
172138568Ssam	struct mbuf *m;
173138568Ssam	struct ieee80211_frame *wh;
174138568Ssam
175138568Ssam	MGETHDR(m, M_NOWAIT, MT_HEADER);
176138568Ssam	if (m == NULL) {
177138568Ssam		/* XXX debug msg */
178138568Ssam		ic->ic_stats.is_tx_nobuf++;
179138568Ssam		return ENOMEM;
180138568Ssam	}
181138568Ssam	m->m_pkthdr.rcvif = (void *) ieee80211_ref_node(ni);
182138568Ssam
183138568Ssam	wh = mtod(m, struct ieee80211_frame *);
184138568Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
185138568Ssam		IEEE80211_FC0_SUBTYPE_NODATA;
186138568Ssam	*(u_int16_t *)wh->i_dur = 0;
187138568Ssam	*(u_int16_t *)wh->i_seq =
188138568Ssam	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
189138568Ssam	ni->ni_txseqs[0]++;
190138568Ssam
191138568Ssam	/* XXX WDS */
192138568Ssam	wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
193138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
194138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
195138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
196138568Ssam	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
197138568Ssam
198138568Ssam	IEEE80211_NODE_STAT(ni, tx_data);
199138568Ssam
200138568Ssam	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
201138568Ssam	if_start(ifp);
202138568Ssam
203138568Ssam	return 0;
204138568Ssam}
205138568Ssam
206138568Ssam/*
207138568Ssam * Assign priority to a frame based on any vlan tag assigned
208138568Ssam * to the station and/or any Diffserv setting in an IP header.
209138568Ssam * Finally, if an ACM policy is setup (in station mode) it's
210138568Ssam * applied.
211138568Ssam */
212138568Ssamint
213138568Ssamieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
214138568Ssam{
215138568Ssam	int v_wme_ac, d_wme_ac, ac;
216138568Ssam#ifdef INET
217138568Ssam	struct ether_header *eh;
218138568Ssam#endif
219138568Ssam
220138568Ssam	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
221138568Ssam		ac = WME_AC_BE;
222138568Ssam		goto done;
223138568Ssam	}
224138568Ssam
225138568Ssam	/*
226138568Ssam	 * If node has a vlan tag then all traffic
227138568Ssam	 * to it must have a matching tag.
228138568Ssam	 */
229138568Ssam	v_wme_ac = 0;
230138568Ssam	if (ni->ni_vlan != 0) {
231138568Ssam		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
232143716Ssam		 if (mtag == NULL) {
233138568Ssam			IEEE80211_NODE_STAT(ni, tx_novlantag);
234138568Ssam			return 1;
235138568Ssam		}
236138568Ssam		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
237138568Ssam		    EVL_VLANOFTAG(ni->ni_vlan)) {
238138568Ssam			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
239138568Ssam			return 1;
240138568Ssam		}
241138568Ssam		/* map vlan priority to AC */
242138568Ssam		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
243138568Ssam		case 1:
244138568Ssam		case 2:
245138568Ssam			v_wme_ac = WME_AC_BK;
246138568Ssam			break;
247138568Ssam		case 0:
248138568Ssam		case 3:
249138568Ssam			v_wme_ac = WME_AC_BE;
250138568Ssam			break;
251138568Ssam		case 4:
252138568Ssam		case 5:
253138568Ssam			v_wme_ac = WME_AC_VI;
254138568Ssam			break;
255138568Ssam		case 6:
256138568Ssam		case 7:
257138568Ssam			v_wme_ac = WME_AC_VO;
258138568Ssam			break;
259138568Ssam		}
260138568Ssam	}
261138568Ssam
262138568Ssam#ifdef INET
263138568Ssam	eh = mtod(m, struct ether_header *);
264138568Ssam	if (eh->ether_type == htons(ETHERTYPE_IP)) {
265138568Ssam		const struct ip *ip = (struct ip *)
266138568Ssam			(mtod(m, u_int8_t *) + sizeof (*eh));
267138568Ssam		/*
268138568Ssam		 * IP frame, map the TOS field.
269138568Ssam		 */
270138568Ssam		switch (ip->ip_tos) {
271138568Ssam		case 0x08:
272138568Ssam		case 0x20:
273138568Ssam			d_wme_ac = WME_AC_BK;	/* background */
274138568Ssam			break;
275138568Ssam		case 0x28:
276138568Ssam		case 0xa0:
277138568Ssam			d_wme_ac = WME_AC_VI;	/* video */
278138568Ssam			break;
279138568Ssam		case 0x30:			/* voice */
280138568Ssam		case 0xe0:
281138568Ssam		case 0x88:			/* XXX UPSD */
282138568Ssam		case 0xb8:
283138568Ssam			d_wme_ac = WME_AC_VO;
284138568Ssam			break;
285138568Ssam		default:
286138568Ssam			d_wme_ac = WME_AC_BE;
287138568Ssam			break;
288138568Ssam		}
289138568Ssam	} else {
290138568Ssam#endif /* INET */
291138568Ssam		d_wme_ac = WME_AC_BE;
292138568Ssam#ifdef INET
293138568Ssam	}
294138568Ssam#endif
295138568Ssam	/*
296138568Ssam	 * Use highest priority AC.
297138568Ssam	 */
298138568Ssam	if (v_wme_ac > d_wme_ac)
299138568Ssam		ac = v_wme_ac;
300138568Ssam	else
301138568Ssam		ac = d_wme_ac;
302138568Ssam
303138568Ssam	/*
304138568Ssam	 * Apply ACM policy.
305138568Ssam	 */
306138568Ssam	if (ic->ic_opmode == IEEE80211_M_STA) {
307138568Ssam		static const int acmap[4] = {
308138568Ssam			WME_AC_BK,	/* WME_AC_BE */
309138568Ssam			WME_AC_BK,	/* WME_AC_BK */
310138568Ssam			WME_AC_BE,	/* WME_AC_VI */
311138568Ssam			WME_AC_VI,	/* WME_AC_VO */
312138568Ssam		};
313138568Ssam		while (ac != WME_AC_BK &&
314138568Ssam		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
315138568Ssam			ac = acmap[ac];
316138568Ssam	}
317138568Ssamdone:
318138568Ssam	M_WME_SETAC(m, ac);
319138568Ssam	return 0;
320138568Ssam}
321138568Ssam
322138568Ssam/*
323139527Ssam * Insure there is sufficient contiguous space to encapsulate the
324139527Ssam * 802.11 data frame.  If room isn't already there, arrange for it.
325139527Ssam * Drivers and cipher modules assume we have done the necessary work
326139527Ssam * and fail rudely if they don't find the space they need.
327139527Ssam */
328139527Ssamstatic struct mbuf *
329139527Ssamieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
330139527Ssam	struct ieee80211_key *key, struct mbuf *m)
331139527Ssam{
332139527Ssam#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
333139527Ssam	int needed_space = hdrsize;
334139527Ssam
335139527Ssam	if (key != NULL) {
336139527Ssam		/* XXX belongs in crypto code? */
337139527Ssam		needed_space += key->wk_cipher->ic_header;
338139527Ssam		/* XXX frags */
339139527Ssam	}
340139527Ssam	/*
341139527Ssam	 * We know we are called just before stripping an Ethernet
342139527Ssam	 * header and prepending an LLC header.  This means we know
343139527Ssam	 * there will be
344139527Ssam	 *	sizeof(struct ether_header) - sizeof(struct llc)
345139527Ssam	 * bytes recovered to which we need additional space for the
346139527Ssam	 * 802.11 header and any crypto header.
347139527Ssam	 */
348139527Ssam	/* XXX check trailing space and copy instead? */
349139527Ssam	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
350139527Ssam		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
351139527Ssam		if (n == NULL) {
352139527Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
353139527Ssam			    "%s: cannot expand storage\n", __func__);
354139527Ssam			ic->ic_stats.is_tx_nobuf++;
355139527Ssam			m_freem(m);
356139527Ssam			return NULL;
357139527Ssam		}
358139527Ssam		KASSERT(needed_space <= MHLEN,
359139527Ssam		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
360139527Ssam		/*
361139527Ssam		 * Setup new mbuf to have leading space to prepend the
362139527Ssam		 * 802.11 header and any crypto header bits that are
363139527Ssam		 * required (the latter are added when the driver calls
364139527Ssam		 * back to ieee80211_crypto_encap to do crypto encapsulation).
365139527Ssam		 */
366139527Ssam		/* NB: must be first 'cuz it clobbers m_data */
367139527Ssam		m_move_pkthdr(n, m);
368139527Ssam		n->m_len = 0;			/* NB: m_gethdr does not set */
369139527Ssam		n->m_data += needed_space;
370139527Ssam		/*
371139527Ssam		 * Pull up Ethernet header to create the expected layout.
372139527Ssam		 * We could use m_pullup but that's overkill (i.e. we don't
373139527Ssam		 * need the actual data) and it cannot fail so do it inline
374139527Ssam		 * for speed.
375139527Ssam		 */
376139527Ssam		/* NB: struct ether_header is known to be contiguous */
377139527Ssam		n->m_len += sizeof(struct ether_header);
378139527Ssam		m->m_len -= sizeof(struct ether_header);
379139527Ssam		m->m_data += sizeof(struct ether_header);
380139527Ssam		/*
381139527Ssam		 * Replace the head of the chain.
382139527Ssam		 */
383139527Ssam		n->m_next = m;
384139527Ssam		m = n;
385139527Ssam	}
386139527Ssam	return m;
387139527Ssam#undef TO_BE_RECLAIMED
388139527Ssam}
389139527Ssam
390139527Ssam#define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
391139527Ssam/*
392139527Ssam * Return the transmit key to use in sending a unicast frame.
393139527Ssam * If a unicast key is set we use that.  When no unicast key is set
394139527Ssam * we fall back to the default transmit key.
395138568Ssam */
396138568Ssamstatic __inline struct ieee80211_key *
397139527Ssamieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
398138568Ssam{
399139527Ssam	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
400138568Ssam		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
401138568Ssam		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
402138568Ssam			return NULL;
403138568Ssam		return &ic->ic_nw_keys[ic->ic_def_txkey];
404138568Ssam	} else {
405138568Ssam		return &ni->ni_ucastkey;
406138568Ssam	}
407138568Ssam}
408138568Ssam
409138568Ssam/*
410139527Ssam * Return the transmit key to use in sending a multicast frame.
411139527Ssam * Multicast traffic always uses the group key which is installed as
412139527Ssam * the default tx key.
413139527Ssam */
414139527Ssamstatic __inline struct ieee80211_key *
415139527Ssamieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
416139527Ssam{
417139527Ssam	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
418139527Ssam	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
419139527Ssam		return NULL;
420139527Ssam	return &ic->ic_nw_keys[ic->ic_def_txkey];
421139527Ssam}
422139527Ssam
423139527Ssam/*
424138568Ssam * Encapsulate an outbound data frame.  The mbuf chain is updated.
425138568Ssam * If an error is encountered NULL is returned.  The caller is required
426138568Ssam * to provide a node reference and pullup the ethernet header in the
427138568Ssam * first mbuf.
428138568Ssam */
429116742Ssamstruct mbuf *
430138568Ssamieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
431138568Ssam	struct ieee80211_node *ni)
432116742Ssam{
433116742Ssam	struct ether_header eh;
434116742Ssam	struct ieee80211_frame *wh;
435138568Ssam	struct ieee80211_key *key;
436116742Ssam	struct llc *llc;
437139527Ssam	int hdrsize, datalen, addqos;
438116742Ssam
439138568Ssam	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
440116742Ssam	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
441116742Ssam
442138568Ssam	/*
443138568Ssam	 * Insure space for additional headers.  First identify
444138568Ssam	 * transmit key to use in calculating any buffer adjustments
445138568Ssam	 * required.  This is also used below to do privacy
446138568Ssam	 * encapsulation work.  Then calculate the 802.11 header
447138568Ssam	 * size and any padding required by the driver.
448138568Ssam	 *
449138568Ssam	 * Note key may be NULL if we fall back to the default
450138568Ssam	 * transmit key and that is not set.  In that case the
451138568Ssam	 * buffer may not be expanded as needed by the cipher
452138568Ssam	 * routines, but they will/should discard it.
453138568Ssam	 */
454138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
455139527Ssam		if (ic->ic_opmode == IEEE80211_M_STA ||
456139527Ssam		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
457139527Ssam			key = ieee80211_crypto_getucastkey(ic, ni);
458139527Ssam		else
459139527Ssam			key = ieee80211_crypto_getmcastkey(ic, ni);
460138568Ssam		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
461138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
462139527Ssam			    "[%s] no default transmit key (%s) deftxkey %u\n",
463139527Ssam			    ether_sprintf(eh.ether_dhost), __func__,
464139527Ssam			    ic->ic_def_txkey);
465139527Ssam			ic->ic_stats.is_tx_nodefkey++;
466138568Ssam		}
467138568Ssam	} else
468138568Ssam		key = NULL;
469138568Ssam	/* XXX 4-address format */
470139527Ssam	/*
471139527Ssam	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
472139527Ssam	 * frames so suppress use.  This may be an issue if other
473139527Ssam	 * ap's require all data frames to be QoS-encapsulated
474139527Ssam	 * once negotiated in which case we'll need to make this
475139527Ssam	 * configurable.
476139527Ssam	 */
477139527Ssam	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
478139527Ssam		 eh.ether_type != htons(ETHERTYPE_PAE);
479139527Ssam	if (addqos)
480138568Ssam		hdrsize = sizeof(struct ieee80211_qosframe);
481138568Ssam	else
482138568Ssam		hdrsize = sizeof(struct ieee80211_frame);
483138568Ssam	if (ic->ic_flags & IEEE80211_F_DATAPAD)
484138568Ssam		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
485138568Ssam	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
486138568Ssam	if (m == NULL) {
487138568Ssam		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
488127772Ssam		goto bad;
489127772Ssam	}
490116742Ssam
491138568Ssam	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
492116742Ssam	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
493116742Ssam	llc = mtod(m, struct llc *);
494116742Ssam	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
495116742Ssam	llc->llc_control = LLC_UI;
496116742Ssam	llc->llc_snap.org_code[0] = 0;
497116742Ssam	llc->llc_snap.org_code[1] = 0;
498116742Ssam	llc->llc_snap.org_code[2] = 0;
499116742Ssam	llc->llc_snap.ether_type = eh.ether_type;
500138568Ssam	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
501138568Ssam
502138568Ssam	M_PREPEND(m, hdrsize, M_DONTWAIT);
503121180Ssam	if (m == NULL) {
504138568Ssam		ic->ic_stats.is_tx_nobuf++;
505119150Ssam		goto bad;
506121180Ssam	}
507116742Ssam	wh = mtod(m, struct ieee80211_frame *);
508116742Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
509116742Ssam	*(u_int16_t *)wh->i_dur = 0;
510116742Ssam	switch (ic->ic_opmode) {
511116742Ssam	case IEEE80211_M_STA:
512116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
513116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
514116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
515116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
516116742Ssam		break;
517116742Ssam	case IEEE80211_M_IBSS:
518116742Ssam	case IEEE80211_M_AHDEMO:
519116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
520116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
521116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
522140636Ssam		/*
523140636Ssam		 * NB: always use the bssid from ic_bss as the
524140636Ssam		 *     neighbor's may be stale after an ibss merge
525140636Ssam		 */
526140636Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
527116742Ssam		break;
528116742Ssam	case IEEE80211_M_HOSTAP:
529116742Ssam		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
530116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
531116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
532116742Ssam		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
533116742Ssam		break;
534117817Ssam	case IEEE80211_M_MONITOR:
535119150Ssam		goto bad;
536116742Ssam	}
537147789Ssam	if (m->m_flags & M_MORE_DATA)
538147789Ssam		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
539139527Ssam	if (addqos) {
540138568Ssam		struct ieee80211_qosframe *qwh =
541138568Ssam			(struct ieee80211_qosframe *) wh;
542138568Ssam		int ac, tid;
543138568Ssam
544138568Ssam		ac = M_WME_GETAC(m);
545138568Ssam		/* map from access class/queue to 11e header priorty value */
546138568Ssam		tid = WME_AC_TO_TID(ac);
547138568Ssam		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
548138568Ssam		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
549138568Ssam			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
550138568Ssam		qwh->i_qos[1] = 0;
551138568Ssam		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
552138568Ssam
553138568Ssam		*(u_int16_t *)wh->i_seq =
554138568Ssam		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
555138568Ssam		ni->ni_txseqs[tid]++;
556138568Ssam	} else {
557138568Ssam		*(u_int16_t *)wh->i_seq =
558138568Ssam		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
559138568Ssam		ni->ni_txseqs[0]++;
560138568Ssam	}
561139527Ssam	if (key != NULL) {
562139527Ssam		/*
563139527Ssam		 * IEEE 802.1X: send EAPOL frames always in the clear.
564139527Ssam		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
565139527Ssam		 */
566139527Ssam		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
567139527Ssam		    ((ic->ic_flags & IEEE80211_F_WPA) &&
568141660Ssam		     (ic->ic_opmode == IEEE80211_M_STA ?
569141660Ssam		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
570139527Ssam			wh->i_fc[1] |= IEEE80211_FC1_WEP;
571139527Ssam			/* XXX do fragmentation */
572147045Ssam			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
573139527Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
574139527Ssam				    "[%s] enmic failed, discard frame\n",
575139527Ssam				    ether_sprintf(eh.ether_dhost));
576139527Ssam				ic->ic_stats.is_crypto_enmicfail++;
577139527Ssam				goto bad;
578139527Ssam			}
579139527Ssam		}
580139527Ssam	}
581138568Ssam
582138568Ssam	IEEE80211_NODE_STAT(ni, tx_data);
583138568Ssam	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
584138568Ssam
585116742Ssam	return m;
586119150Ssambad:
587119150Ssam	if (m != NULL)
588119150Ssam		m_freem(m);
589119150Ssam	return NULL;
590116742Ssam}
591116742Ssam
592116742Ssam/*
593116742Ssam * Add a supported rates element id to a frame.
594116742Ssam */
595138568Ssamstatic u_int8_t *
596116742Ssamieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
597116742Ssam{
598116742Ssam	int nrates;
599116742Ssam
600116742Ssam	*frm++ = IEEE80211_ELEMID_RATES;
601116742Ssam	nrates = rs->rs_nrates;
602116742Ssam	if (nrates > IEEE80211_RATE_SIZE)
603116742Ssam		nrates = IEEE80211_RATE_SIZE;
604116742Ssam	*frm++ = nrates;
605116742Ssam	memcpy(frm, rs->rs_rates, nrates);
606116742Ssam	return frm + nrates;
607116742Ssam}
608116742Ssam
609116742Ssam/*
610116742Ssam * Add an extended supported rates element id to a frame.
611116742Ssam */
612138568Ssamstatic u_int8_t *
613116742Ssamieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
614116742Ssam{
615116742Ssam	/*
616116742Ssam	 * Add an extended supported rates element if operating in 11g mode.
617116742Ssam	 */
618116742Ssam	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
619116742Ssam		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
620116742Ssam		*frm++ = IEEE80211_ELEMID_XRATES;
621116742Ssam		*frm++ = nrates;
622116742Ssam		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
623116742Ssam		frm += nrates;
624116742Ssam	}
625116742Ssam	return frm;
626116742Ssam}
627116742Ssam
628116742Ssam/*
629116742Ssam * Add an ssid elemet to a frame.
630116742Ssam */
631116742Ssamstatic u_int8_t *
632116742Ssamieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
633116742Ssam{
634116742Ssam	*frm++ = IEEE80211_ELEMID_SSID;
635116742Ssam	*frm++ = len;
636116742Ssam	memcpy(frm, ssid, len);
637116742Ssam	return frm + len;
638116742Ssam}
639116742Ssam
640138568Ssam/*
641138568Ssam * Add an erp element to a frame.
642138568Ssam */
643138568Ssamstatic u_int8_t *
644138568Ssamieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
645116742Ssam{
646138568Ssam	u_int8_t erp;
647116742Ssam
648138568Ssam	*frm++ = IEEE80211_ELEMID_ERP;
649138568Ssam	*frm++ = 1;
650138568Ssam	erp = 0;
651138568Ssam	if (ic->ic_nonerpsta != 0)
652138568Ssam		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
653138568Ssam	if (ic->ic_flags & IEEE80211_F_USEPROT)
654138568Ssam		erp |= IEEE80211_ERP_USE_PROTECTION;
655138568Ssam	if (ic->ic_flags & IEEE80211_F_USEBARKER)
656138568Ssam		erp |= IEEE80211_ERP_LONG_PREAMBLE;
657138568Ssam	*frm++ = erp;
658138568Ssam	return frm;
659138568Ssam}
660138568Ssam
661138568Ssamstatic u_int8_t *
662138568Ssamieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
663138568Ssam{
664138568Ssam#define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
665138568Ssam#define	ADDSHORT(frm, v) do {			\
666138568Ssam	frm[0] = (v) & 0xff;			\
667138568Ssam	frm[1] = (v) >> 8;			\
668138568Ssam	frm += 2;				\
669138568Ssam} while (0)
670138568Ssam#define	ADDSELECTOR(frm, sel) do {		\
671138568Ssam	memcpy(frm, sel, 4);			\
672138568Ssam	frm += 4;				\
673138568Ssam} while (0)
674138568Ssam	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
675138568Ssam	static const u_int8_t cipher_suite[][4] = {
676138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
677138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
678138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
679138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
680138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
681138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_NULL },
682138568Ssam	};
683138568Ssam	static const u_int8_t wep104_suite[4] =
684138568Ssam		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
685138568Ssam	static const u_int8_t key_mgt_unspec[4] =
686138568Ssam		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
687138568Ssam	static const u_int8_t key_mgt_psk[4] =
688138568Ssam		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
689138568Ssam	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
690138568Ssam	u_int8_t *frm = ie;
691138568Ssam	u_int8_t *selcnt;
692138568Ssam
693138568Ssam	*frm++ = IEEE80211_ELEMID_VENDOR;
694138568Ssam	*frm++ = 0;				/* length filled in below */
695138568Ssam	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
696138568Ssam	frm += sizeof(oui);
697138568Ssam	ADDSHORT(frm, WPA_VERSION);
698138568Ssam
699138568Ssam	/* XXX filter out CKIP */
700138568Ssam
701138568Ssam	/* multicast cipher */
702138568Ssam	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
703138568Ssam	    rsn->rsn_mcastkeylen >= 13)
704138568Ssam		ADDSELECTOR(frm, wep104_suite);
705116742Ssam	else
706138568Ssam		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
707138568Ssam
708138568Ssam	/* unicast cipher list */
709138568Ssam	selcnt = frm;
710138568Ssam	ADDSHORT(frm, 0);			/* selector count */
711138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
712138568Ssam		selcnt[0]++;
713138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
714138568Ssam	}
715138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
716138568Ssam		selcnt[0]++;
717138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
718138568Ssam	}
719138568Ssam
720138568Ssam	/* authenticator selector list */
721138568Ssam	selcnt = frm;
722138568Ssam	ADDSHORT(frm, 0);			/* selector count */
723138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
724138568Ssam		selcnt[0]++;
725138568Ssam		ADDSELECTOR(frm, key_mgt_unspec);
726138568Ssam	}
727138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
728138568Ssam		selcnt[0]++;
729138568Ssam		ADDSELECTOR(frm, key_mgt_psk);
730138568Ssam	}
731138568Ssam
732138568Ssam	/* optional capabilities */
733147066Ssam	if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
734138568Ssam		ADDSHORT(frm, rsn->rsn_caps);
735138568Ssam
736138568Ssam	/* calculate element length */
737138568Ssam	ie[1] = frm - ie - 2;
738138568Ssam	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
739138609Ssam		("WPA IE too big, %u > %zu",
740138568Ssam		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
741138568Ssam	return frm;
742138568Ssam#undef ADDSHORT
743138568Ssam#undef ADDSELECTOR
744138568Ssam#undef WPA_OUI_BYTES
745116742Ssam}
746116742Ssam
747138568Ssamstatic u_int8_t *
748138568Ssamieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
749138568Ssam{
750138568Ssam#define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
751138568Ssam#define	ADDSHORT(frm, v) do {			\
752138568Ssam	frm[0] = (v) & 0xff;			\
753138568Ssam	frm[1] = (v) >> 8;			\
754138568Ssam	frm += 2;				\
755138568Ssam} while (0)
756138568Ssam#define	ADDSELECTOR(frm, sel) do {		\
757138568Ssam	memcpy(frm, sel, 4);			\
758138568Ssam	frm += 4;				\
759138568Ssam} while (0)
760138568Ssam	static const u_int8_t cipher_suite[][4] = {
761138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
762138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
763138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
764138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
765138568Ssam		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
766138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_NULL },
767138568Ssam	};
768138568Ssam	static const u_int8_t wep104_suite[4] =
769138568Ssam		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
770138568Ssam	static const u_int8_t key_mgt_unspec[4] =
771138568Ssam		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
772138568Ssam	static const u_int8_t key_mgt_psk[4] =
773138568Ssam		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
774138568Ssam	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
775138568Ssam	u_int8_t *frm = ie;
776138568Ssam	u_int8_t *selcnt;
777138568Ssam
778138568Ssam	*frm++ = IEEE80211_ELEMID_RSN;
779138568Ssam	*frm++ = 0;				/* length filled in below */
780138568Ssam	ADDSHORT(frm, RSN_VERSION);
781138568Ssam
782138568Ssam	/* XXX filter out CKIP */
783138568Ssam
784138568Ssam	/* multicast cipher */
785138568Ssam	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
786138568Ssam	    rsn->rsn_mcastkeylen >= 13)
787138568Ssam		ADDSELECTOR(frm, wep104_suite);
788138568Ssam	else
789138568Ssam		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
790138568Ssam
791138568Ssam	/* unicast cipher list */
792138568Ssam	selcnt = frm;
793138568Ssam	ADDSHORT(frm, 0);			/* selector count */
794138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
795138568Ssam		selcnt[0]++;
796138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
797138568Ssam	}
798138568Ssam	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
799138568Ssam		selcnt[0]++;
800138568Ssam		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
801138568Ssam	}
802138568Ssam
803138568Ssam	/* authenticator selector list */
804138568Ssam	selcnt = frm;
805138568Ssam	ADDSHORT(frm, 0);			/* selector count */
806138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
807138568Ssam		selcnt[0]++;
808138568Ssam		ADDSELECTOR(frm, key_mgt_unspec);
809138568Ssam	}
810138568Ssam	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
811138568Ssam		selcnt[0]++;
812138568Ssam		ADDSELECTOR(frm, key_mgt_psk);
813138568Ssam	}
814138568Ssam
815138568Ssam	/* optional capabilities */
816147066Ssam	ADDSHORT(frm, rsn->rsn_caps);
817138568Ssam	/* XXX PMKID */
818138568Ssam
819138568Ssam	/* calculate element length */
820138568Ssam	ie[1] = frm - ie - 2;
821138568Ssam	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
822138609Ssam		("RSN IE too big, %u > %zu",
823138568Ssam		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
824138568Ssam	return frm;
825138568Ssam#undef ADDSELECTOR
826138568Ssam#undef ADDSHORT
827138568Ssam#undef RSN_OUI_BYTES
828138568Ssam}
829138568Ssam
830119150Ssam/*
831138568Ssam * Add a WPA/RSN element to a frame.
832138568Ssam */
833138568Ssamstatic u_int8_t *
834138568Ssamieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
835138568Ssam{
836138568Ssam
837138568Ssam	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
838138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA2)
839138568Ssam		frm = ieee80211_setup_rsn_ie(ic, frm);
840138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA1)
841138568Ssam		frm = ieee80211_setup_wpa_ie(ic, frm);
842138568Ssam	return frm;
843138568Ssam}
844138568Ssam
845138568Ssam#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
846138568Ssam/*
847138568Ssam * Add a WME information element to a frame.
848138568Ssam */
849138568Ssamstatic u_int8_t *
850138568Ssamieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
851138568Ssam{
852138568Ssam	static const struct ieee80211_wme_info info = {
853138568Ssam		.wme_id		= IEEE80211_ELEMID_VENDOR,
854138568Ssam		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
855138568Ssam		.wme_oui	= { WME_OUI_BYTES },
856138568Ssam		.wme_type	= WME_OUI_TYPE,
857138568Ssam		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
858138568Ssam		.wme_version	= WME_VERSION,
859138568Ssam		.wme_info	= 0,
860138568Ssam	};
861138568Ssam	memcpy(frm, &info, sizeof(info));
862138568Ssam	return frm + sizeof(info);
863138568Ssam}
864138568Ssam
865138568Ssam/*
866138568Ssam * Add a WME parameters element to a frame.
867138568Ssam */
868138568Ssamstatic u_int8_t *
869138568Ssamieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
870138568Ssam{
871138568Ssam#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
872138568Ssam#define	ADDSHORT(frm, v) do {			\
873138568Ssam	frm[0] = (v) & 0xff;			\
874138568Ssam	frm[1] = (v) >> 8;			\
875138568Ssam	frm += 2;				\
876138568Ssam} while (0)
877138568Ssam	/* NB: this works 'cuz a param has an info at the front */
878138568Ssam	static const struct ieee80211_wme_info param = {
879138568Ssam		.wme_id		= IEEE80211_ELEMID_VENDOR,
880138568Ssam		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
881138568Ssam		.wme_oui	= { WME_OUI_BYTES },
882138568Ssam		.wme_type	= WME_OUI_TYPE,
883138568Ssam		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
884138568Ssam		.wme_version	= WME_VERSION,
885138568Ssam	};
886138568Ssam	int i;
887138568Ssam
888138568Ssam	memcpy(frm, &param, sizeof(param));
889138568Ssam	frm += __offsetof(struct ieee80211_wme_info, wme_info);
890138568Ssam	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
891138568Ssam	*frm++ = 0;					/* reserved field */
892138568Ssam	for (i = 0; i < WME_NUM_AC; i++) {
893138568Ssam		const struct wmeParams *ac =
894138568Ssam		       &wme->wme_bssChanParams.cap_wmeParams[i];
895138568Ssam		*frm++ = SM(i, WME_PARAM_ACI)
896138568Ssam		       | SM(ac->wmep_acm, WME_PARAM_ACM)
897138568Ssam		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
898138568Ssam		       ;
899138568Ssam		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
900138568Ssam		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
901138568Ssam		       ;
902138568Ssam		ADDSHORT(frm, ac->wmep_txopLimit);
903138568Ssam	}
904138568Ssam	return frm;
905138568Ssam#undef SM
906138568Ssam#undef ADDSHORT
907138568Ssam}
908138568Ssam#undef WME_OUI_BYTES
909138568Ssam
910138568Ssam/*
911119150Ssam * Send a management frame.  The node is for the destination (or ic_bss
912119150Ssam * when in station mode).  Nodes other than ic_bss have their reference
913119150Ssam * count bumped to reflect our use for an indeterminant time.
914119150Ssam */
915116742Ssamint
916116742Ssamieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
917116742Ssam	int type, int arg)
918116742Ssam{
919121180Ssam#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
920116742Ssam	struct mbuf *m;
921116742Ssam	u_int8_t *frm;
922116742Ssam	enum ieee80211_phymode mode;
923116742Ssam	u_int16_t capinfo;
924138568Ssam	int has_challenge, is_shared_key, ret, timer, status;
925116742Ssam
926119150Ssam	KASSERT(ni != NULL, ("null node"));
927119150Ssam
928119150Ssam	/*
929119150Ssam	 * Hold a reference on the node so it doesn't go away until after
930119150Ssam	 * the xmit is complete all the way in the driver.  On error we
931119150Ssam	 * will remove our reference.
932119150Ssam	 */
933138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
934140766Ssam		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
935138568Ssam		__func__, __LINE__,
936140766Ssam		ni, ether_sprintf(ni->ni_macaddr),
937140766Ssam		ieee80211_node_refcnt(ni)+1);
938138568Ssam	ieee80211_ref_node(ni);
939138568Ssam
940116742Ssam	timer = 0;
941116742Ssam	switch (type) {
942116742Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
943116742Ssam		/*
944116742Ssam		 * prreq frame format
945116742Ssam		 *	[tlv] ssid
946116742Ssam		 *	[tlv] supported rates
947116742Ssam		 *	[tlv] extended supported rates
948138568Ssam		 *	[tlv] user-specified ie's
949116742Ssam		 */
950138568Ssam		m = ieee80211_getmgtframe(&frm,
951138568Ssam			 2 + IEEE80211_NWID_LEN
952116742Ssam		       + 2 + IEEE80211_RATE_SIZE
953138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
954138568Ssam		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
955138568Ssam		);
956116742Ssam		if (m == NULL)
957138568Ssam			senderr(ENOMEM, is_tx_nobuf);
958138568Ssam
959116742Ssam		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
960116742Ssam		mode = ieee80211_chan2mode(ic, ni->ni_chan);
961116742Ssam		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
962116742Ssam		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
963138568Ssam		if (ic->ic_opt_ie != NULL) {
964138568Ssam			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
965138568Ssam			frm += ic->ic_opt_ie_len;
966138568Ssam		}
967116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
968116742Ssam
969138568Ssam		IEEE80211_NODE_STAT(ni, tx_probereq);
970138568Ssam		if (ic->ic_opmode == IEEE80211_M_STA)
971138568Ssam			timer = IEEE80211_TRANS_WAIT;
972116742Ssam		break;
973116742Ssam
974116742Ssam	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
975116742Ssam		/*
976116742Ssam		 * probe response frame format
977116742Ssam		 *	[8] time stamp
978116742Ssam		 *	[2] beacon interval
979116742Ssam		 *	[2] cabability information
980116742Ssam		 *	[tlv] ssid
981116742Ssam		 *	[tlv] supported rates
982121178Ssam		 *	[tlv] parameter set (FH/DS)
983116742Ssam		 *	[tlv] parameter set (IBSS)
984138568Ssam		 *	[tlv] extended rate phy (ERP)
985116742Ssam		 *	[tlv] extended supported rates
986138568Ssam		 *	[tlv] WPA
987144136Ssam		 *	[tlv] WME (optional)
988116742Ssam		 */
989138568Ssam		m = ieee80211_getmgtframe(&frm,
990138568Ssam			 8
991138568Ssam		       + sizeof(u_int16_t)
992138568Ssam		       + sizeof(u_int16_t)
993138568Ssam		       + 2 + IEEE80211_NWID_LEN
994116742Ssam		       + 2 + IEEE80211_RATE_SIZE
995138568Ssam		       + 7	/* max(7,3) */
996116742Ssam		       + 6
997138568Ssam		       + 3
998138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
999138568Ssam		       /* XXX !WPA1+WPA2 fits w/o a cluster */
1000138568Ssam		       + (ic->ic_flags & IEEE80211_F_WPA ?
1001138568Ssam				2*sizeof(struct ieee80211_ie_wpa) : 0)
1002144136Ssam		       + sizeof(struct ieee80211_wme_param)
1003138568Ssam		);
1004116742Ssam		if (m == NULL)
1005138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1006116742Ssam
1007116742Ssam		memset(frm, 0, 8);	/* timestamp should be filled later */
1008116742Ssam		frm += 8;
1009116742Ssam		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1010116742Ssam		frm += 2;
1011116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS)
1012116742Ssam			capinfo = IEEE80211_CAPINFO_IBSS;
1013116742Ssam		else
1014116742Ssam			capinfo = IEEE80211_CAPINFO_ESS;
1015138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1016116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1017120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1018120070Ssam		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1019120070Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1020138568Ssam		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1021138568Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1022116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1023116742Ssam		frm += 2;
1024116742Ssam
1025116742Ssam		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1026116742Ssam				ic->ic_bss->ni_esslen);
1027138568Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1028116742Ssam
1029121178Ssam		if (ic->ic_phytype == IEEE80211_T_FH) {
1030121178Ssam                        *frm++ = IEEE80211_ELEMID_FHPARMS;
1031121178Ssam                        *frm++ = 5;
1032121178Ssam                        *frm++ = ni->ni_fhdwell & 0x00ff;
1033121178Ssam                        *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1034121178Ssam                        *frm++ = IEEE80211_FH_CHANSET(
1035121178Ssam			    ieee80211_chan2ieee(ic, ni->ni_chan));
1036121178Ssam                        *frm++ = IEEE80211_FH_CHANPAT(
1037121178Ssam			    ieee80211_chan2ieee(ic, ni->ni_chan));
1038121178Ssam                        *frm++ = ni->ni_fhindex;
1039121178Ssam		} else {
1040121178Ssam			*frm++ = IEEE80211_ELEMID_DSPARMS;
1041121178Ssam			*frm++ = 1;
1042121178Ssam			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1043121178Ssam		}
1044121178Ssam
1045116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS) {
1046116742Ssam			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1047116742Ssam			*frm++ = 2;
1048116742Ssam			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1049116742Ssam		}
1050138568Ssam		if (ic->ic_flags & IEEE80211_F_WPA)
1051138568Ssam			frm = ieee80211_add_wpa(frm, ic);
1052138568Ssam		if (ic->ic_curmode == IEEE80211_MODE_11G)
1053138568Ssam			frm = ieee80211_add_erp(frm, ic);
1054138568Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1055144136Ssam		if (ic->ic_flags & IEEE80211_F_WME)
1056144136Ssam			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1057116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1058116742Ssam		break;
1059116742Ssam
1060116742Ssam	case IEEE80211_FC0_SUBTYPE_AUTH:
1061138568Ssam		status = arg >> 16;
1062138568Ssam		arg &= 0xffff;
1063138568Ssam		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1064138568Ssam		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1065138568Ssam		    ni->ni_challenge != NULL);
1066138568Ssam
1067138568Ssam		/*
1068138568Ssam		 * Deduce whether we're doing open authentication or
1069138568Ssam		 * shared key authentication.  We do the latter if
1070138568Ssam		 * we're in the middle of a shared key authentication
1071138568Ssam		 * handshake or if we're initiating an authentication
1072138568Ssam		 * request and configured to use shared key.
1073138568Ssam		 */
1074138568Ssam		is_shared_key = has_challenge ||
1075138568Ssam		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1076138568Ssam		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1077138568Ssam		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1078138568Ssam
1079138568Ssam		m = ieee80211_getmgtframe(&frm,
1080138568Ssam			  3 * sizeof(u_int16_t)
1081138568Ssam			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1082138568Ssam				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1083138568Ssam		);
1084116742Ssam		if (m == NULL)
1085138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1086138568Ssam
1087138568Ssam		((u_int16_t *)frm)[0] =
1088138568Ssam		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1089138568Ssam		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
1090116742Ssam		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
1091138568Ssam		((u_int16_t *)frm)[2] = htole16(status);/* status */
1092138568Ssam
1093138568Ssam		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1094138568Ssam			((u_int16_t *)frm)[3] =
1095138568Ssam			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
1096138568Ssam			    IEEE80211_ELEMID_CHALLENGE);
1097138568Ssam			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1098138568Ssam			    IEEE80211_CHALLENGE_LEN);
1099138568Ssam			m->m_pkthdr.len = m->m_len =
1100138568Ssam				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1101138568Ssam			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1102138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1103138568Ssam				    "[%s] request encrypt frame (%s)\n",
1104138568Ssam				    ether_sprintf(ni->ni_macaddr), __func__);
1105138568Ssam				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1106138568Ssam			}
1107138568Ssam		} else
1108138568Ssam			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1109138568Ssam
1110138568Ssam		/* XXX not right for shared key */
1111138568Ssam		if (status == IEEE80211_STATUS_SUCCESS)
1112138568Ssam			IEEE80211_NODE_STAT(ni, tx_auth);
1113138568Ssam		else
1114138568Ssam			IEEE80211_NODE_STAT(ni, tx_auth_fail);
1115138568Ssam
1116116742Ssam		if (ic->ic_opmode == IEEE80211_M_STA)
1117116742Ssam			timer = IEEE80211_TRANS_WAIT;
1118116742Ssam		break;
1119116742Ssam
1120116742Ssam	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1121138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1122138568Ssam			"[%s] send station deauthenticate (reason %d)\n",
1123138568Ssam			ether_sprintf(ni->ni_macaddr), arg);
1124138568Ssam		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1125116742Ssam		if (m == NULL)
1126138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1127138568Ssam		*(u_int16_t *)frm = htole16(arg);	/* reason */
1128138568Ssam		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1129138568Ssam
1130138568Ssam		IEEE80211_NODE_STAT(ni, tx_deauth);
1131138568Ssam		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1132138568Ssam
1133138568Ssam		ieee80211_node_unauthorize(ic, ni);	/* port closed */
1134116742Ssam		break;
1135116742Ssam
1136116742Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1137116742Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1138116742Ssam		/*
1139116742Ssam		 * asreq frame format
1140116742Ssam		 *	[2] capability information
1141116742Ssam		 *	[2] listen interval
1142116742Ssam		 *	[6*] current AP address (reassoc only)
1143116742Ssam		 *	[tlv] ssid
1144116742Ssam		 *	[tlv] supported rates
1145116742Ssam		 *	[tlv] extended supported rates
1146138568Ssam		 *	[tlv] WME
1147138568Ssam		 *	[tlv] user-specified ie's
1148116742Ssam		 */
1149138568Ssam		m = ieee80211_getmgtframe(&frm,
1150138568Ssam			 sizeof(u_int16_t)
1151116742Ssam		       + sizeof(u_int16_t)
1152116742Ssam		       + IEEE80211_ADDR_LEN
1153138568Ssam		       + 2 + IEEE80211_NWID_LEN
1154116742Ssam		       + 2 + IEEE80211_RATE_SIZE
1155138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1156138568Ssam		       + sizeof(struct ieee80211_wme_info)
1157138568Ssam		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1158138568Ssam		);
1159116742Ssam		if (m == NULL)
1160138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1161116742Ssam
1162116742Ssam		capinfo = 0;
1163116742Ssam		if (ic->ic_opmode == IEEE80211_M_IBSS)
1164116742Ssam			capinfo |= IEEE80211_CAPINFO_IBSS;
1165116742Ssam		else		/* IEEE80211_M_STA */
1166116742Ssam			capinfo |= IEEE80211_CAPINFO_ESS;
1167138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1168116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1169120070Ssam		/*
1170120070Ssam		 * NB: Some 11a AP's reject the request when
1171120070Ssam		 *     short premable is set.
1172120070Ssam		 */
1173120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1174120070Ssam		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1175116742Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1176138568Ssam		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1177138568Ssam		    (ic->ic_caps & IEEE80211_C_SHSLOT))
1178116742Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1179116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1180116742Ssam		frm += 2;
1181116742Ssam
1182116742Ssam		*(u_int16_t *)frm = htole16(ic->ic_lintval);
1183116742Ssam		frm += 2;
1184116742Ssam
1185116742Ssam		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1186116742Ssam			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1187116742Ssam			frm += IEEE80211_ADDR_LEN;
1188116742Ssam		}
1189116742Ssam
1190116742Ssam		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1191116742Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1192116742Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1193138568Ssam		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1194138568Ssam			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1195138568Ssam		if (ic->ic_opt_ie != NULL) {
1196138568Ssam			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1197138568Ssam			frm += ic->ic_opt_ie_len;
1198138568Ssam		}
1199116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1200116742Ssam
1201116742Ssam		timer = IEEE80211_TRANS_WAIT;
1202116742Ssam		break;
1203116742Ssam
1204116742Ssam	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1205116742Ssam	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1206116742Ssam		/*
1207116742Ssam		 * asreq frame format
1208116742Ssam		 *	[2] capability information
1209116742Ssam		 *	[2] status
1210116742Ssam		 *	[2] association ID
1211116742Ssam		 *	[tlv] supported rates
1212116742Ssam		 *	[tlv] extended supported rates
1213138568Ssam		 *	[tlv] WME (if enabled and STA enabled)
1214116742Ssam		 */
1215138568Ssam		m = ieee80211_getmgtframe(&frm,
1216138568Ssam			 sizeof(u_int16_t)
1217116742Ssam		       + sizeof(u_int16_t)
1218116742Ssam		       + sizeof(u_int16_t)
1219116742Ssam		       + 2 + IEEE80211_RATE_SIZE
1220138568Ssam		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1221138568Ssam		       + sizeof(struct ieee80211_wme_param)
1222138568Ssam		);
1223116742Ssam		if (m == NULL)
1224138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1225116742Ssam
1226116742Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1227138568Ssam		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1228116742Ssam			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1229120070Ssam		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1230120070Ssam		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1231120070Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1232138568Ssam		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1233138568Ssam			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1234116742Ssam		*(u_int16_t *)frm = htole16(capinfo);
1235116742Ssam		frm += 2;
1236116742Ssam
1237116742Ssam		*(u_int16_t *)frm = htole16(arg);	/* status */
1238116742Ssam		frm += 2;
1239116742Ssam
1240138568Ssam		if (arg == IEEE80211_STATUS_SUCCESS) {
1241116742Ssam			*(u_int16_t *)frm = htole16(ni->ni_associd);
1242138568Ssam			IEEE80211_NODE_STAT(ni, tx_assoc);
1243138568Ssam		} else
1244138568Ssam			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1245116742Ssam		frm += 2;
1246116742Ssam
1247119150Ssam		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1248119150Ssam		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1249138568Ssam		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1250138568Ssam			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1251116742Ssam		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1252116742Ssam		break;
1253116742Ssam
1254116742Ssam	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1255138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1256138568Ssam			"[%s] send station disassociate (reason %d)\n",
1257138568Ssam			ether_sprintf(ni->ni_macaddr), arg);
1258138568Ssam		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1259116742Ssam		if (m == NULL)
1260138568Ssam			senderr(ENOMEM, is_tx_nobuf);
1261138568Ssam		*(u_int16_t *)frm = htole16(arg);	/* reason */
1262138568Ssam		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1263138568Ssam
1264138568Ssam		IEEE80211_NODE_STAT(ni, tx_disassoc);
1265138568Ssam		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1266116742Ssam		break;
1267116742Ssam
1268116742Ssam	default:
1269138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1270138568Ssam			"[%s] invalid mgmt frame type %u\n",
1271138568Ssam			ether_sprintf(ni->ni_macaddr), type);
1272121180Ssam		senderr(EINVAL, is_tx_unknownmgt);
1273119150Ssam		/* NOTREACHED */
1274116742Ssam	}
1275116742Ssam
1276138568Ssam	ret = ieee80211_mgmt_output(ic, ni, m, type);
1277119150Ssam	if (ret == 0) {
1278119150Ssam		if (timer)
1279119150Ssam			ic->ic_mgt_timer = timer;
1280119150Ssam	} else {
1281119150Ssambad:
1282138568Ssam		ieee80211_free_node(ni);
1283119150Ssam	}
1284116742Ssam	return ret;
1285119150Ssam#undef senderr
1286116742Ssam}
1287138568Ssam
1288138568Ssam/*
1289138568Ssam * Allocate a beacon frame and fillin the appropriate bits.
1290138568Ssam */
1291138568Ssamstruct mbuf *
1292138568Ssamieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1293138568Ssam	struct ieee80211_beacon_offsets *bo)
1294138568Ssam{
1295138568Ssam	struct ifnet *ifp = ic->ic_ifp;
1296138568Ssam	struct ieee80211_frame *wh;
1297138568Ssam	struct mbuf *m;
1298138568Ssam	int pktlen;
1299138568Ssam	u_int8_t *frm, *efrm;
1300138568Ssam	u_int16_t capinfo;
1301138568Ssam	struct ieee80211_rateset *rs;
1302138568Ssam
1303138568Ssam	/*
1304138568Ssam	 * beacon frame format
1305138568Ssam	 *	[8] time stamp
1306138568Ssam	 *	[2] beacon interval
1307138568Ssam	 *	[2] cabability information
1308138568Ssam	 *	[tlv] ssid
1309138568Ssam	 *	[tlv] supported rates
1310138568Ssam	 *	[3] parameter set (DS)
1311138568Ssam	 *	[tlv] parameter set (IBSS/TIM)
1312138568Ssam	 *	[tlv] extended rate phy (ERP)
1313138568Ssam	 *	[tlv] extended supported rates
1314138568Ssam	 *	[tlv] WME parameters
1315138568Ssam	 *	[tlv] WPA/RSN parameters
1316138568Ssam	 * XXX Vendor-specific OIDs (e.g. Atheros)
1317138568Ssam	 * NB: we allocate the max space required for the TIM bitmap.
1318138568Ssam	 */
1319138568Ssam	rs = &ni->ni_rates;
1320138568Ssam	pktlen =   8					/* time stamp */
1321138568Ssam		 + sizeof(u_int16_t)			/* beacon interval */
1322138568Ssam		 + sizeof(u_int16_t)			/* capabilities */
1323138568Ssam		 + 2 + ni->ni_esslen			/* ssid */
1324138568Ssam	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
1325138568Ssam	         + 2 + 1				/* DS parameters */
1326138568Ssam		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
1327138568Ssam		 + 2 + 1				/* ERP */
1328138568Ssam	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1329138568Ssam		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
1330138568Ssam			sizeof(struct ieee80211_wme_param) : 0)
1331138568Ssam		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
1332138568Ssam			2*sizeof(struct ieee80211_ie_wpa) : 0)
1333138568Ssam		 ;
1334138568Ssam	m = ieee80211_getmgtframe(&frm, pktlen);
1335138568Ssam	if (m == NULL) {
1336138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1337138568Ssam			"%s: cannot get buf; size %u\n", __func__, pktlen);
1338138568Ssam		ic->ic_stats.is_tx_nobuf++;
1339138568Ssam		return NULL;
1340138568Ssam	}
1341138568Ssam
1342138568Ssam	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
1343138568Ssam	frm += 8;
1344138568Ssam	*(u_int16_t *)frm = htole16(ni->ni_intval);
1345138568Ssam	frm += 2;
1346138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
1347138568Ssam		capinfo = IEEE80211_CAPINFO_IBSS;
1348138568Ssam	else
1349138568Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1350138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1351138568Ssam		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1352138568Ssam	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1353138568Ssam	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1354138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1355138568Ssam	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1356138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1357138568Ssam	bo->bo_caps = (u_int16_t *)frm;
1358138568Ssam	*(u_int16_t *)frm = htole16(capinfo);
1359138568Ssam	frm += 2;
1360138568Ssam	*frm++ = IEEE80211_ELEMID_SSID;
1361138568Ssam	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1362138568Ssam		*frm++ = ni->ni_esslen;
1363138568Ssam		memcpy(frm, ni->ni_essid, ni->ni_esslen);
1364138568Ssam		frm += ni->ni_esslen;
1365138568Ssam	} else
1366138568Ssam		*frm++ = 0;
1367138568Ssam	frm = ieee80211_add_rates(frm, rs);
1368138568Ssam	if (ic->ic_curmode != IEEE80211_MODE_FH) {
1369138568Ssam		*frm++ = IEEE80211_ELEMID_DSPARMS;
1370138568Ssam		*frm++ = 1;
1371138568Ssam		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1372138568Ssam	}
1373138568Ssam	bo->bo_tim = frm;
1374138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
1375138568Ssam		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1376138568Ssam		*frm++ = 2;
1377138568Ssam		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1378138568Ssam		bo->bo_tim_len = 0;
1379138568Ssam	} else {
1380138568Ssam		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1381138568Ssam
1382138568Ssam		tie->tim_ie = IEEE80211_ELEMID_TIM;
1383138568Ssam		tie->tim_len = 4;	/* length */
1384138568Ssam		tie->tim_count = 0;	/* DTIM count */
1385138568Ssam		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
1386138568Ssam		tie->tim_bitctl = 0;	/* bitmap control */
1387138568Ssam		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
1388138568Ssam		frm += sizeof(struct ieee80211_tim_ie);
1389138568Ssam		bo->bo_tim_len = 1;
1390138568Ssam	}
1391138568Ssam	bo->bo_trailer = frm;
1392138568Ssam	if (ic->ic_flags & IEEE80211_F_WME) {
1393138568Ssam		bo->bo_wme = frm;
1394138568Ssam		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1395140764Ssam		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1396138568Ssam	}
1397138568Ssam	if (ic->ic_flags & IEEE80211_F_WPA)
1398138568Ssam		frm = ieee80211_add_wpa(frm, ic);
1399138568Ssam	if (ic->ic_curmode == IEEE80211_MODE_11G)
1400138568Ssam		frm = ieee80211_add_erp(frm, ic);
1401138568Ssam	efrm = ieee80211_add_xrates(frm, rs);
1402138568Ssam	bo->bo_trailer_len = efrm - bo->bo_trailer;
1403138568Ssam	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1404138568Ssam
1405138568Ssam	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1406138568Ssam	KASSERT(m != NULL, ("no space for 802.11 header?"));
1407138568Ssam	wh = mtod(m, struct ieee80211_frame *);
1408138568Ssam	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1409138568Ssam	    IEEE80211_FC0_SUBTYPE_BEACON;
1410138568Ssam	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1411138568Ssam	*(u_int16_t *)wh->i_dur = 0;
1412138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1413138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1414138568Ssam	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1415138568Ssam	*(u_int16_t *)wh->i_seq = 0;
1416138568Ssam
1417138568Ssam	return m;
1418138568Ssam}
1419138568Ssam
1420138568Ssam/*
1421138568Ssam * Update the dynamic parts of a beacon frame based on the current state.
1422138568Ssam */
1423138568Ssamint
1424138568Ssamieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1425138568Ssam	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1426138568Ssam{
1427138568Ssam	int len_changed = 0;
1428138568Ssam	u_int16_t capinfo;
1429138568Ssam
1430138568Ssam	IEEE80211_BEACON_LOCK(ic);
1431138568Ssam	/* XXX faster to recalculate entirely or just changes? */
1432138568Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
1433138568Ssam		capinfo = IEEE80211_CAPINFO_IBSS;
1434138568Ssam	else
1435138568Ssam		capinfo = IEEE80211_CAPINFO_ESS;
1436138568Ssam	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1437138568Ssam		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1438138568Ssam	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1439138568Ssam	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1440138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1441138568Ssam	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1442138568Ssam		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1443138568Ssam	*bo->bo_caps = htole16(capinfo);
1444138568Ssam
1445138568Ssam	if (ic->ic_flags & IEEE80211_F_WME) {
1446138568Ssam		struct ieee80211_wme_state *wme = &ic->ic_wme;
1447138568Ssam
1448138568Ssam		/*
1449138568Ssam		 * Check for agressive mode change.  When there is
1450138568Ssam		 * significant high priority traffic in the BSS
1451138568Ssam		 * throttle back BE traffic by using conservative
1452138568Ssam		 * parameters.  Otherwise BE uses agressive params
1453138568Ssam		 * to optimize performance of legacy/non-QoS traffic.
1454138568Ssam		 */
1455138568Ssam		if (wme->wme_flags & WME_F_AGGRMODE) {
1456138568Ssam			if (wme->wme_hipri_traffic >
1457138568Ssam			    wme->wme_hipri_switch_thresh) {
1458138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1459138568Ssam				    "%s: traffic %u, disable aggressive mode\n",
1460138568Ssam				    __func__, wme->wme_hipri_traffic);
1461138568Ssam				wme->wme_flags &= ~WME_F_AGGRMODE;
1462138568Ssam				ieee80211_wme_updateparams_locked(ic);
1463138568Ssam				wme->wme_hipri_traffic =
1464138568Ssam					wme->wme_hipri_switch_hysteresis;
1465138568Ssam			} else
1466138568Ssam				wme->wme_hipri_traffic = 0;
1467138568Ssam		} else {
1468138568Ssam			if (wme->wme_hipri_traffic <=
1469138568Ssam			    wme->wme_hipri_switch_thresh) {
1470138568Ssam				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1471138568Ssam				    "%s: traffic %u, enable aggressive mode\n",
1472138568Ssam				    __func__, wme->wme_hipri_traffic);
1473138568Ssam				wme->wme_flags |= WME_F_AGGRMODE;
1474138568Ssam				ieee80211_wme_updateparams_locked(ic);
1475138568Ssam				wme->wme_hipri_traffic = 0;
1476138568Ssam			} else
1477138568Ssam				wme->wme_hipri_traffic =
1478138568Ssam					wme->wme_hipri_switch_hysteresis;
1479138568Ssam		}
1480138568Ssam		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1481138568Ssam			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
1482138568Ssam			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1483138568Ssam		}
1484138568Ssam	}
1485138568Ssam
1486138568Ssam	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
1487138568Ssam		struct ieee80211_tim_ie *tie =
1488138568Ssam			(struct ieee80211_tim_ie *) bo->bo_tim;
1489138568Ssam		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1490138568Ssam			u_int timlen, timoff, i;
1491138568Ssam			/*
1492138568Ssam			 * ATIM/DTIM needs updating.  If it fits in the
1493138568Ssam			 * current space allocated then just copy in the
1494138568Ssam			 * new bits.  Otherwise we need to move any trailing
1495138568Ssam			 * data to make room.  Note that we know there is
1496138568Ssam			 * contiguous space because ieee80211_beacon_allocate
1497138568Ssam			 * insures there is space in the mbuf to write a
1498138568Ssam			 * maximal-size virtual bitmap (based on ic_max_aid).
1499138568Ssam			 */
1500138568Ssam			/*
1501138568Ssam			 * Calculate the bitmap size and offset, copy any
1502138568Ssam			 * trailer out of the way, and then copy in the
1503138568Ssam			 * new bitmap and update the information element.
1504138568Ssam			 * Note that the tim bitmap must contain at least
1505138568Ssam			 * one byte and any offset must be even.
1506138568Ssam			 */
1507138568Ssam			if (ic->ic_ps_pending != 0) {
1508138568Ssam				timoff = 128;		/* impossibly large */
1509138568Ssam				for (i = 0; i < ic->ic_tim_len; i++)
1510138568Ssam					if (ic->ic_tim_bitmap[i]) {
1511138568Ssam						timoff = i &~ 1;
1512138568Ssam						break;
1513138568Ssam					}
1514138568Ssam				KASSERT(timoff != 128, ("tim bitmap empty!"));
1515138568Ssam				for (i = ic->ic_tim_len-1; i >= timoff; i--)
1516138568Ssam					if (ic->ic_tim_bitmap[i])
1517138568Ssam						break;
1518138568Ssam				timlen = 1 + (i - timoff);
1519138568Ssam			} else {
1520138568Ssam				timoff = 0;
1521138568Ssam				timlen = 1;
1522138568Ssam			}
1523138568Ssam			if (timlen != bo->bo_tim_len) {
1524138568Ssam				/* copy up/down trailer */
1525138568Ssam				ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
1526138568Ssam					bo->bo_trailer_len);
1527138568Ssam				bo->bo_trailer = tie->tim_bitmap+timlen;
1528138568Ssam				bo->bo_wme = bo->bo_trailer;
1529138568Ssam				bo->bo_tim_len = timlen;
1530138568Ssam
1531138568Ssam				/* update information element */
1532138568Ssam				tie->tim_len = 3 + timlen;
1533138568Ssam				tie->tim_bitctl = timoff;
1534138568Ssam				len_changed = 1;
1535138568Ssam			}
1536138568Ssam			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1537138568Ssam				bo->bo_tim_len);
1538138568Ssam
1539138568Ssam			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1540138568Ssam
1541138568Ssam			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1542138568Ssam				"%s: TIM updated, pending %u, off %u, len %u\n",
1543138568Ssam				__func__, ic->ic_ps_pending, timoff, timlen);
1544138568Ssam		}
1545138568Ssam		/* count down DTIM period */
1546138568Ssam		if (tie->tim_count == 0)
1547138568Ssam			tie->tim_count = tie->tim_period - 1;
1548138568Ssam		else
1549138568Ssam			tie->tim_count--;
1550138568Ssam		/* update state for buffered multicast frames on DTIM */
1551138568Ssam		if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
1552138568Ssam			tie->tim_bitctl |= 1;
1553138568Ssam		else
1554138568Ssam			tie->tim_bitctl &= ~1;
1555138568Ssam	}
1556138568Ssam	IEEE80211_BEACON_UNLOCK(ic);
1557138568Ssam
1558138568Ssam	return len_changed;
1559138568Ssam}
1560138568Ssam
1561138568Ssam/*
1562138568Ssam * Save an outbound packet for a node in power-save sleep state.
1563138568Ssam * The new packet is placed on the node's saved queue, and the TIM
1564138568Ssam * is changed, if necessary.
1565138568Ssam */
1566138568Ssamvoid
1567138568Ssamieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1568138568Ssam		  struct mbuf *m)
1569138568Ssam{
1570138568Ssam	int qlen, age;
1571138568Ssam
1572138568Ssam	IEEE80211_NODE_SAVEQ_LOCK(ni);
1573138568Ssam	if (_IF_QFULL(&ni->ni_savedq)) {
1574138568Ssam		_IF_DROP(&ni->ni_savedq);
1575138568Ssam		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1576138568Ssam		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1577138568Ssam			"[%s] pwr save q overflow, drops %d (size %d)\n",
1578138568Ssam			ether_sprintf(ni->ni_macaddr),
1579138568Ssam			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1580138568Ssam#ifdef IEEE80211_DEBUG
1581138568Ssam		if (ieee80211_msg_dumppkts(ic))
1582138568Ssam			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1583138568Ssam#endif
1584138568Ssam		m_freem(m);
1585138568Ssam		return;
1586138568Ssam	}
1587138568Ssam	/*
1588138568Ssam	 * Tag the frame with it's expiry time and insert
1589138568Ssam	 * it in the queue.  The aging interval is 4 times
1590138568Ssam	 * the listen interval specified by the station.
1591138568Ssam	 * Frames that sit around too long are reclaimed
1592138568Ssam	 * using this information.
1593138568Ssam	 */
1594138568Ssam	/* XXX handle overflow? */
1595138568Ssam	age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
1596138568Ssam	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1597138568Ssam	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1598138568Ssam
1599138568Ssam	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1600138568Ssam		"[%s] save frame, %u now queued\n",
1601138568Ssam		ether_sprintf(ni->ni_macaddr), qlen);
1602138568Ssam
1603138568Ssam	if (qlen == 1)
1604138568Ssam		ic->ic_set_tim(ic, ni, 1);
1605138568Ssam}
1606