ieee80211_output.c revision 147223
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2 as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 147223 2005-06-10 05:04:42Z sam $");
35
36#include "opt_inet.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/mbuf.h>
41#include <sys/kernel.h>
42#include <sys/endian.h>
43
44#include <sys/socket.h>
45
46#include <net/bpf.h>
47#include <net/ethernet.h>
48#include <net/if.h>
49#include <net/if_llc.h>
50#include <net/if_media.h>
51#include <net/if_vlan_var.h>
52
53#include <net80211/ieee80211_var.h>
54
55#ifdef INET
56#include <netinet/in.h>
57#include <netinet/if_ether.h>
58#include <netinet/in_systm.h>
59#include <netinet/ip.h>
60#endif
61
62#ifdef IEEE80211_DEBUG
63/*
64 * Decide if an outbound management frame should be
65 * printed when debugging is enabled.  This filters some
66 * of the less interesting frames that come frequently
67 * (e.g. beacons).
68 */
69static __inline int
70doprint(struct ieee80211com *ic, int subtype)
71{
72	switch (subtype) {
73	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
74		return (ic->ic_opmode == IEEE80211_M_IBSS);
75	}
76	return 1;
77}
78#endif
79
80/*
81 * Send a management frame to the specified node.  The node pointer
82 * must have a reference as the pointer will be passed to the driver
83 * and potentially held for a long time.  If the frame is successfully
84 * dispatched to the driver, then it is responsible for freeing the
85 * reference (and potentially free'ing up any associated storage).
86 */
87static int
88ieee80211_mgmt_output(struct ieee80211com *ic, struct ieee80211_node *ni,
89    struct mbuf *m, int type)
90{
91	struct ifnet *ifp = ic->ic_ifp;
92	struct ieee80211_frame *wh;
93
94	KASSERT(ni != NULL, ("null node"));
95
96	/*
97	 * Yech, hack alert!  We want to pass the node down to the
98	 * driver's start routine.  If we don't do so then the start
99	 * routine must immediately look it up again and that can
100	 * cause a lock order reversal if, for example, this frame
101	 * is being sent because the station is being timedout and
102	 * the frame being sent is a DEAUTH message.  We could stick
103	 * this in an m_tag and tack that on to the mbuf.  However
104	 * that's rather expensive to do for every frame so instead
105	 * we stuff it in the rcvif field since outbound frames do
106	 * not (presently) use this.
107	 */
108	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
109	if (m == NULL)
110		return ENOMEM;
111	KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null"));
112	m->m_pkthdr.rcvif = (void *)ni;
113
114	wh = mtod(m, struct ieee80211_frame *);
115	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
116	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
117	*(u_int16_t *)wh->i_dur = 0;
118	*(u_int16_t *)wh->i_seq =
119	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
120	ni->ni_txseqs[0]++;
121	/*
122	 * Hack.  When sending PROBE_REQ frames while scanning we
123	 * explicitly force a broadcast rather than (as before) clobber
124	 * ni_macaddr and ni_bssid.  This is stopgap, we need a way
125	 * to communicate this directly rather than do something
126	 * implicit based on surrounding state.
127	 */
128	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ &&
129	    (ic->ic_flags & IEEE80211_F_SCAN)) {
130		IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
131		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
132		IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
133	} else {
134		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
135		IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
136		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
137	}
138
139	if ((m->m_flags & M_LINK0) != 0 && ni->ni_challenge != NULL) {
140		m->m_flags &= ~M_LINK0;
141		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
142			"[%s] encrypting frame (%s)\n",
143			ether_sprintf(wh->i_addr1), __func__);
144		wh->i_fc[1] |= IEEE80211_FC1_WEP;
145	}
146#ifdef IEEE80211_DEBUG
147	/* avoid printing too many frames */
148	if ((ieee80211_msg_debug(ic) && doprint(ic, type)) ||
149	    ieee80211_msg_dumppkts(ic)) {
150		printf("[%s] send %s on channel %u\n",
151		    ether_sprintf(wh->i_addr1),
152		    ieee80211_mgt_subtype_name[
153			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
154				IEEE80211_FC0_SUBTYPE_SHIFT],
155		    ieee80211_chan2ieee(ic, ni->ni_chan));
156	}
157#endif
158	IEEE80211_NODE_STAT(ni, tx_mgmt);
159	IF_ENQUEUE(&ic->ic_mgtq, m);
160	ifp->if_timer = 1;
161	if_start(ifp);
162	return 0;
163}
164
165/*
166 * Send a null data frame to the specified node.
167 */
168int
169ieee80211_send_nulldata(struct ieee80211com *ic, struct ieee80211_node *ni)
170{
171	struct ifnet *ifp = ic->ic_ifp;
172	struct mbuf *m;
173	struct ieee80211_frame *wh;
174
175	MGETHDR(m, M_NOWAIT, MT_HEADER);
176	if (m == NULL) {
177		/* XXX debug msg */
178		ic->ic_stats.is_tx_nobuf++;
179		return ENOMEM;
180	}
181	m->m_pkthdr.rcvif = (void *) ieee80211_ref_node(ni);
182
183	wh = mtod(m, struct ieee80211_frame *);
184	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
185		IEEE80211_FC0_SUBTYPE_NODATA;
186	*(u_int16_t *)wh->i_dur = 0;
187	*(u_int16_t *)wh->i_seq =
188	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
189	ni->ni_txseqs[0]++;
190
191	/* XXX WDS */
192	wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
193	IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
194	IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
195	IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
196	m->m_len = m->m_pkthdr.len = sizeof(struct ieee80211_frame);
197
198	IEEE80211_NODE_STAT(ni, tx_data);
199
200	IF_ENQUEUE(&ic->ic_mgtq, m);		/* cheat */
201	if_start(ifp);
202
203	return 0;
204}
205
206/*
207 * Assign priority to a frame based on any vlan tag assigned
208 * to the station and/or any Diffserv setting in an IP header.
209 * Finally, if an ACM policy is setup (in station mode) it's
210 * applied.
211 */
212int
213ieee80211_classify(struct ieee80211com *ic, struct mbuf *m, struct ieee80211_node *ni)
214{
215	int v_wme_ac, d_wme_ac, ac;
216#ifdef INET
217	struct ether_header *eh;
218#endif
219
220	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
221		ac = WME_AC_BE;
222		goto done;
223	}
224
225	/*
226	 * If node has a vlan tag then all traffic
227	 * to it must have a matching tag.
228	 */
229	v_wme_ac = 0;
230	if (ni->ni_vlan != 0) {
231		 struct m_tag *mtag = VLAN_OUTPUT_TAG(ic->ic_ifp, m);
232		 if (mtag == NULL) {
233			IEEE80211_NODE_STAT(ni, tx_novlantag);
234			return 1;
235		}
236		if (EVL_VLANOFTAG(VLAN_TAG_VALUE(mtag)) !=
237		    EVL_VLANOFTAG(ni->ni_vlan)) {
238			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
239			return 1;
240		}
241		/* map vlan priority to AC */
242		switch (EVL_PRIOFTAG(ni->ni_vlan)) {
243		case 1:
244		case 2:
245			v_wme_ac = WME_AC_BK;
246			break;
247		case 0:
248		case 3:
249			v_wme_ac = WME_AC_BE;
250			break;
251		case 4:
252		case 5:
253			v_wme_ac = WME_AC_VI;
254			break;
255		case 6:
256		case 7:
257			v_wme_ac = WME_AC_VO;
258			break;
259		}
260	}
261
262#ifdef INET
263	eh = mtod(m, struct ether_header *);
264	if (eh->ether_type == htons(ETHERTYPE_IP)) {
265		const struct ip *ip = (struct ip *)
266			(mtod(m, u_int8_t *) + sizeof (*eh));
267		/*
268		 * IP frame, map the TOS field.
269		 */
270		switch (ip->ip_tos) {
271		case 0x08:
272		case 0x20:
273			d_wme_ac = WME_AC_BK;	/* background */
274			break;
275		case 0x28:
276		case 0xa0:
277			d_wme_ac = WME_AC_VI;	/* video */
278			break;
279		case 0x30:			/* voice */
280		case 0xe0:
281		case 0x88:			/* XXX UPSD */
282		case 0xb8:
283			d_wme_ac = WME_AC_VO;
284			break;
285		default:
286			d_wme_ac = WME_AC_BE;
287			break;
288		}
289	} else {
290#endif /* INET */
291		d_wme_ac = WME_AC_BE;
292#ifdef INET
293	}
294#endif
295	/*
296	 * Use highest priority AC.
297	 */
298	if (v_wme_ac > d_wme_ac)
299		ac = v_wme_ac;
300	else
301		ac = d_wme_ac;
302
303	/*
304	 * Apply ACM policy.
305	 */
306	if (ic->ic_opmode == IEEE80211_M_STA) {
307		static const int acmap[4] = {
308			WME_AC_BK,	/* WME_AC_BE */
309			WME_AC_BK,	/* WME_AC_BK */
310			WME_AC_BE,	/* WME_AC_VI */
311			WME_AC_VI,	/* WME_AC_VO */
312		};
313		while (ac != WME_AC_BK &&
314		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
315			ac = acmap[ac];
316	}
317done:
318	M_WME_SETAC(m, ac);
319	return 0;
320}
321
322/*
323 * Insure there is sufficient contiguous space to encapsulate the
324 * 802.11 data frame.  If room isn't already there, arrange for it.
325 * Drivers and cipher modules assume we have done the necessary work
326 * and fail rudely if they don't find the space they need.
327 */
328static struct mbuf *
329ieee80211_mbuf_adjust(struct ieee80211com *ic, int hdrsize,
330	struct ieee80211_key *key, struct mbuf *m)
331{
332#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
333	int needed_space = hdrsize;
334
335	if (key != NULL) {
336		/* XXX belongs in crypto code? */
337		needed_space += key->wk_cipher->ic_header;
338		/* XXX frags */
339	}
340	/*
341	 * We know we are called just before stripping an Ethernet
342	 * header and prepending an LLC header.  This means we know
343	 * there will be
344	 *	sizeof(struct ether_header) - sizeof(struct llc)
345	 * bytes recovered to which we need additional space for the
346	 * 802.11 header and any crypto header.
347	 */
348	/* XXX check trailing space and copy instead? */
349	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
350		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
351		if (n == NULL) {
352			IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
353			    "%s: cannot expand storage\n", __func__);
354			ic->ic_stats.is_tx_nobuf++;
355			m_freem(m);
356			return NULL;
357		}
358		KASSERT(needed_space <= MHLEN,
359		    ("not enough room, need %u got %zu\n", needed_space, MHLEN));
360		/*
361		 * Setup new mbuf to have leading space to prepend the
362		 * 802.11 header and any crypto header bits that are
363		 * required (the latter are added when the driver calls
364		 * back to ieee80211_crypto_encap to do crypto encapsulation).
365		 */
366		/* NB: must be first 'cuz it clobbers m_data */
367		m_move_pkthdr(n, m);
368		n->m_len = 0;			/* NB: m_gethdr does not set */
369		n->m_data += needed_space;
370		/*
371		 * Pull up Ethernet header to create the expected layout.
372		 * We could use m_pullup but that's overkill (i.e. we don't
373		 * need the actual data) and it cannot fail so do it inline
374		 * for speed.
375		 */
376		/* NB: struct ether_header is known to be contiguous */
377		n->m_len += sizeof(struct ether_header);
378		m->m_len -= sizeof(struct ether_header);
379		m->m_data += sizeof(struct ether_header);
380		/*
381		 * Replace the head of the chain.
382		 */
383		n->m_next = m;
384		m = n;
385	}
386	return m;
387#undef TO_BE_RECLAIMED
388}
389
390#define	KEY_UNDEFINED(k)	((k).wk_cipher == &ieee80211_cipher_none)
391/*
392 * Return the transmit key to use in sending a unicast frame.
393 * If a unicast key is set we use that.  When no unicast key is set
394 * we fall back to the default transmit key.
395 */
396static __inline struct ieee80211_key *
397ieee80211_crypto_getucastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
398{
399	if (KEY_UNDEFINED(ni->ni_ucastkey)) {
400		if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
401		    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
402			return NULL;
403		return &ic->ic_nw_keys[ic->ic_def_txkey];
404	} else {
405		return &ni->ni_ucastkey;
406	}
407}
408
409/*
410 * Return the transmit key to use in sending a multicast frame.
411 * Multicast traffic always uses the group key which is installed as
412 * the default tx key.
413 */
414static __inline struct ieee80211_key *
415ieee80211_crypto_getmcastkey(struct ieee80211com *ic, struct ieee80211_node *ni)
416{
417	if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
418	    KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
419		return NULL;
420	return &ic->ic_nw_keys[ic->ic_def_txkey];
421}
422
423/*
424 * Encapsulate an outbound data frame.  The mbuf chain is updated.
425 * If an error is encountered NULL is returned.  The caller is required
426 * to provide a node reference and pullup the ethernet header in the
427 * first mbuf.
428 */
429struct mbuf *
430ieee80211_encap(struct ieee80211com *ic, struct mbuf *m,
431	struct ieee80211_node *ni)
432{
433	struct ether_header eh;
434	struct ieee80211_frame *wh;
435	struct ieee80211_key *key;
436	struct llc *llc;
437	int hdrsize, datalen, addqos;
438
439	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
440	memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
441
442	/*
443	 * Insure space for additional headers.  First identify
444	 * transmit key to use in calculating any buffer adjustments
445	 * required.  This is also used below to do privacy
446	 * encapsulation work.  Then calculate the 802.11 header
447	 * size and any padding required by the driver.
448	 *
449	 * Note key may be NULL if we fall back to the default
450	 * transmit key and that is not set.  In that case the
451	 * buffer may not be expanded as needed by the cipher
452	 * routines, but they will/should discard it.
453	 */
454	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
455		if (ic->ic_opmode == IEEE80211_M_STA ||
456		    !IEEE80211_IS_MULTICAST(eh.ether_dhost))
457			key = ieee80211_crypto_getucastkey(ic, ni);
458		else
459			key = ieee80211_crypto_getmcastkey(ic, ni);
460		if (key == NULL && eh.ether_type != htons(ETHERTYPE_PAE)) {
461			IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
462			    "[%s] no default transmit key (%s) deftxkey %u\n",
463			    ether_sprintf(eh.ether_dhost), __func__,
464			    ic->ic_def_txkey);
465			ic->ic_stats.is_tx_nodefkey++;
466		}
467	} else
468		key = NULL;
469	/* XXX 4-address format */
470	/*
471	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
472	 * frames so suppress use.  This may be an issue if other
473	 * ap's require all data frames to be QoS-encapsulated
474	 * once negotiated in which case we'll need to make this
475	 * configurable.
476	 */
477	addqos = (ni->ni_flags & IEEE80211_NODE_QOS) &&
478		 eh.ether_type != htons(ETHERTYPE_PAE);
479	if (addqos)
480		hdrsize = sizeof(struct ieee80211_qosframe);
481	else
482		hdrsize = sizeof(struct ieee80211_frame);
483	if (ic->ic_flags & IEEE80211_F_DATAPAD)
484		hdrsize = roundup(hdrsize, sizeof(u_int32_t));
485	m = ieee80211_mbuf_adjust(ic, hdrsize, key, m);
486	if (m == NULL) {
487		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
488		goto bad;
489	}
490
491	/* NB: this could be optimized because of ieee80211_mbuf_adjust */
492	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
493	llc = mtod(m, struct llc *);
494	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
495	llc->llc_control = LLC_UI;
496	llc->llc_snap.org_code[0] = 0;
497	llc->llc_snap.org_code[1] = 0;
498	llc->llc_snap.org_code[2] = 0;
499	llc->llc_snap.ether_type = eh.ether_type;
500	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
501
502	M_PREPEND(m, hdrsize, M_DONTWAIT);
503	if (m == NULL) {
504		ic->ic_stats.is_tx_nobuf++;
505		goto bad;
506	}
507	wh = mtod(m, struct ieee80211_frame *);
508	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
509	*(u_int16_t *)wh->i_dur = 0;
510	switch (ic->ic_opmode) {
511	case IEEE80211_M_STA:
512		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
513		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
514		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
515		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
516		break;
517	case IEEE80211_M_IBSS:
518	case IEEE80211_M_AHDEMO:
519		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
520		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
521		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
522		/*
523		 * NB: always use the bssid from ic_bss as the
524		 *     neighbor's may be stale after an ibss merge
525		 */
526		IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
527		break;
528	case IEEE80211_M_HOSTAP:
529		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
530		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
531		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
532		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
533		break;
534	case IEEE80211_M_MONITOR:
535		goto bad;
536	}
537	if (addqos) {
538		struct ieee80211_qosframe *qwh =
539			(struct ieee80211_qosframe *) wh;
540		int ac, tid;
541
542		ac = M_WME_GETAC(m);
543		/* map from access class/queue to 11e header priorty value */
544		tid = WME_AC_TO_TID(ac);
545		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
546		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
547			qwh->i_qos[0] |= 1 << IEEE80211_QOS_ACKPOLICY_S;
548		qwh->i_qos[1] = 0;
549		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
550
551		*(u_int16_t *)wh->i_seq =
552		    htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
553		ni->ni_txseqs[tid]++;
554	} else {
555		*(u_int16_t *)wh->i_seq =
556		    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
557		ni->ni_txseqs[0]++;
558	}
559	if (key != NULL) {
560		/*
561		 * IEEE 802.1X: send EAPOL frames always in the clear.
562		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
563		 */
564		if (eh.ether_type != htons(ETHERTYPE_PAE) ||
565		    ((ic->ic_flags & IEEE80211_F_WPA) &&
566		     (ic->ic_opmode == IEEE80211_M_STA ?
567		      !KEY_UNDEFINED(*key) : !KEY_UNDEFINED(ni->ni_ucastkey)))) {
568			wh->i_fc[1] |= IEEE80211_FC1_WEP;
569			/* XXX do fragmentation */
570			if (!ieee80211_crypto_enmic(ic, key, m, 0)) {
571				IEEE80211_DPRINTF(ic, IEEE80211_MSG_OUTPUT,
572				    "[%s] enmic failed, discard frame\n",
573				    ether_sprintf(eh.ether_dhost));
574				ic->ic_stats.is_crypto_enmicfail++;
575				goto bad;
576			}
577		}
578	}
579
580	IEEE80211_NODE_STAT(ni, tx_data);
581	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
582
583	return m;
584bad:
585	if (m != NULL)
586		m_freem(m);
587	return NULL;
588}
589
590/*
591 * Add a supported rates element id to a frame.
592 */
593static u_int8_t *
594ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
595{
596	int nrates;
597
598	*frm++ = IEEE80211_ELEMID_RATES;
599	nrates = rs->rs_nrates;
600	if (nrates > IEEE80211_RATE_SIZE)
601		nrates = IEEE80211_RATE_SIZE;
602	*frm++ = nrates;
603	memcpy(frm, rs->rs_rates, nrates);
604	return frm + nrates;
605}
606
607/*
608 * Add an extended supported rates element id to a frame.
609 */
610static u_int8_t *
611ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
612{
613	/*
614	 * Add an extended supported rates element if operating in 11g mode.
615	 */
616	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
617		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
618		*frm++ = IEEE80211_ELEMID_XRATES;
619		*frm++ = nrates;
620		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
621		frm += nrates;
622	}
623	return frm;
624}
625
626/*
627 * Add an ssid elemet to a frame.
628 */
629static u_int8_t *
630ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
631{
632	*frm++ = IEEE80211_ELEMID_SSID;
633	*frm++ = len;
634	memcpy(frm, ssid, len);
635	return frm + len;
636}
637
638/*
639 * Add an erp element to a frame.
640 */
641static u_int8_t *
642ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
643{
644	u_int8_t erp;
645
646	*frm++ = IEEE80211_ELEMID_ERP;
647	*frm++ = 1;
648	erp = 0;
649	if (ic->ic_nonerpsta != 0)
650		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
651	if (ic->ic_flags & IEEE80211_F_USEPROT)
652		erp |= IEEE80211_ERP_USE_PROTECTION;
653	if (ic->ic_flags & IEEE80211_F_USEBARKER)
654		erp |= IEEE80211_ERP_LONG_PREAMBLE;
655	*frm++ = erp;
656	return frm;
657}
658
659static u_int8_t *
660ieee80211_setup_wpa_ie(struct ieee80211com *ic, u_int8_t *ie)
661{
662#define	WPA_OUI_BYTES		0x00, 0x50, 0xf2
663#define	ADDSHORT(frm, v) do {			\
664	frm[0] = (v) & 0xff;			\
665	frm[1] = (v) >> 8;			\
666	frm += 2;				\
667} while (0)
668#define	ADDSELECTOR(frm, sel) do {		\
669	memcpy(frm, sel, 4);			\
670	frm += 4;				\
671} while (0)
672	static const u_int8_t oui[4] = { WPA_OUI_BYTES, WPA_OUI_TYPE };
673	static const u_int8_t cipher_suite[][4] = {
674		{ WPA_OUI_BYTES, WPA_CSE_WEP40 },	/* NB: 40-bit */
675		{ WPA_OUI_BYTES, WPA_CSE_TKIP },
676		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX WRAP */
677		{ WPA_OUI_BYTES, WPA_CSE_CCMP },
678		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
679		{ WPA_OUI_BYTES, WPA_CSE_NULL },
680	};
681	static const u_int8_t wep104_suite[4] =
682		{ WPA_OUI_BYTES, WPA_CSE_WEP104 };
683	static const u_int8_t key_mgt_unspec[4] =
684		{ WPA_OUI_BYTES, WPA_ASE_8021X_UNSPEC };
685	static const u_int8_t key_mgt_psk[4] =
686		{ WPA_OUI_BYTES, WPA_ASE_8021X_PSK };
687	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
688	u_int8_t *frm = ie;
689	u_int8_t *selcnt;
690
691	*frm++ = IEEE80211_ELEMID_VENDOR;
692	*frm++ = 0;				/* length filled in below */
693	memcpy(frm, oui, sizeof(oui));		/* WPA OUI */
694	frm += sizeof(oui);
695	ADDSHORT(frm, WPA_VERSION);
696
697	/* XXX filter out CKIP */
698
699	/* multicast cipher */
700	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
701	    rsn->rsn_mcastkeylen >= 13)
702		ADDSELECTOR(frm, wep104_suite);
703	else
704		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
705
706	/* unicast cipher list */
707	selcnt = frm;
708	ADDSHORT(frm, 0);			/* selector count */
709	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
710		selcnt[0]++;
711		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
712	}
713	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
714		selcnt[0]++;
715		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
716	}
717
718	/* authenticator selector list */
719	selcnt = frm;
720	ADDSHORT(frm, 0);			/* selector count */
721	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
722		selcnt[0]++;
723		ADDSELECTOR(frm, key_mgt_unspec);
724	}
725	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
726		selcnt[0]++;
727		ADDSELECTOR(frm, key_mgt_psk);
728	}
729
730	/* optional capabilities */
731	if (rsn->rsn_caps != 0 && rsn->rsn_caps != RSN_CAP_PREAUTH)
732		ADDSHORT(frm, rsn->rsn_caps);
733
734	/* calculate element length */
735	ie[1] = frm - ie - 2;
736	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
737		("WPA IE too big, %u > %zu",
738		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
739	return frm;
740#undef ADDSHORT
741#undef ADDSELECTOR
742#undef WPA_OUI_BYTES
743}
744
745static u_int8_t *
746ieee80211_setup_rsn_ie(struct ieee80211com *ic, u_int8_t *ie)
747{
748#define	RSN_OUI_BYTES		0x00, 0x0f, 0xac
749#define	ADDSHORT(frm, v) do {			\
750	frm[0] = (v) & 0xff;			\
751	frm[1] = (v) >> 8;			\
752	frm += 2;				\
753} while (0)
754#define	ADDSELECTOR(frm, sel) do {		\
755	memcpy(frm, sel, 4);			\
756	frm += 4;				\
757} while (0)
758	static const u_int8_t cipher_suite[][4] = {
759		{ RSN_OUI_BYTES, RSN_CSE_WEP40 },	/* NB: 40-bit */
760		{ RSN_OUI_BYTES, RSN_CSE_TKIP },
761		{ RSN_OUI_BYTES, RSN_CSE_WRAP },
762		{ RSN_OUI_BYTES, RSN_CSE_CCMP },
763		{ 0x00, 0x00, 0x00, 0x00 },		/* XXX CKIP */
764		{ RSN_OUI_BYTES, RSN_CSE_NULL },
765	};
766	static const u_int8_t wep104_suite[4] =
767		{ RSN_OUI_BYTES, RSN_CSE_WEP104 };
768	static const u_int8_t key_mgt_unspec[4] =
769		{ RSN_OUI_BYTES, RSN_ASE_8021X_UNSPEC };
770	static const u_int8_t key_mgt_psk[4] =
771		{ RSN_OUI_BYTES, RSN_ASE_8021X_PSK };
772	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
773	u_int8_t *frm = ie;
774	u_int8_t *selcnt;
775
776	*frm++ = IEEE80211_ELEMID_RSN;
777	*frm++ = 0;				/* length filled in below */
778	ADDSHORT(frm, RSN_VERSION);
779
780	/* XXX filter out CKIP */
781
782	/* multicast cipher */
783	if (rsn->rsn_mcastcipher == IEEE80211_CIPHER_WEP &&
784	    rsn->rsn_mcastkeylen >= 13)
785		ADDSELECTOR(frm, wep104_suite);
786	else
787		ADDSELECTOR(frm, cipher_suite[rsn->rsn_mcastcipher]);
788
789	/* unicast cipher list */
790	selcnt = frm;
791	ADDSHORT(frm, 0);			/* selector count */
792	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_AES_CCM)) {
793		selcnt[0]++;
794		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_AES_CCM]);
795	}
796	if (rsn->rsn_ucastcipherset & (1<<IEEE80211_CIPHER_TKIP)) {
797		selcnt[0]++;
798		ADDSELECTOR(frm, cipher_suite[IEEE80211_CIPHER_TKIP]);
799	}
800
801	/* authenticator selector list */
802	selcnt = frm;
803	ADDSHORT(frm, 0);			/* selector count */
804	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_UNSPEC) {
805		selcnt[0]++;
806		ADDSELECTOR(frm, key_mgt_unspec);
807	}
808	if (rsn->rsn_keymgmtset & WPA_ASE_8021X_PSK) {
809		selcnt[0]++;
810		ADDSELECTOR(frm, key_mgt_psk);
811	}
812
813	/* optional capabilities */
814	ADDSHORT(frm, rsn->rsn_caps);
815	/* XXX PMKID */
816
817	/* calculate element length */
818	ie[1] = frm - ie - 2;
819	KASSERT(ie[1]+2 <= sizeof(struct ieee80211_ie_wpa),
820		("RSN IE too big, %u > %zu",
821		ie[1]+2, sizeof(struct ieee80211_ie_wpa)));
822	return frm;
823#undef ADDSELECTOR
824#undef ADDSHORT
825#undef RSN_OUI_BYTES
826}
827
828/*
829 * Add a WPA/RSN element to a frame.
830 */
831static u_int8_t *
832ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic)
833{
834
835	KASSERT(ic->ic_flags & IEEE80211_F_WPA, ("no WPA/RSN!"));
836	if (ic->ic_flags & IEEE80211_F_WPA2)
837		frm = ieee80211_setup_rsn_ie(ic, frm);
838	if (ic->ic_flags & IEEE80211_F_WPA1)
839		frm = ieee80211_setup_wpa_ie(ic, frm);
840	return frm;
841}
842
843#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
844/*
845 * Add a WME information element to a frame.
846 */
847static u_int8_t *
848ieee80211_add_wme_info(u_int8_t *frm, struct ieee80211_wme_state *wme)
849{
850	static const struct ieee80211_wme_info info = {
851		.wme_id		= IEEE80211_ELEMID_VENDOR,
852		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
853		.wme_oui	= { WME_OUI_BYTES },
854		.wme_type	= WME_OUI_TYPE,
855		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
856		.wme_version	= WME_VERSION,
857		.wme_info	= 0,
858	};
859	memcpy(frm, &info, sizeof(info));
860	return frm + sizeof(info);
861}
862
863/*
864 * Add a WME parameters element to a frame.
865 */
866static u_int8_t *
867ieee80211_add_wme_param(u_int8_t *frm, struct ieee80211_wme_state *wme)
868{
869#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
870#define	ADDSHORT(frm, v) do {			\
871	frm[0] = (v) & 0xff;			\
872	frm[1] = (v) >> 8;			\
873	frm += 2;				\
874} while (0)
875	/* NB: this works 'cuz a param has an info at the front */
876	static const struct ieee80211_wme_info param = {
877		.wme_id		= IEEE80211_ELEMID_VENDOR,
878		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
879		.wme_oui	= { WME_OUI_BYTES },
880		.wme_type	= WME_OUI_TYPE,
881		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
882		.wme_version	= WME_VERSION,
883	};
884	int i;
885
886	memcpy(frm, &param, sizeof(param));
887	frm += __offsetof(struct ieee80211_wme_info, wme_info);
888	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
889	*frm++ = 0;					/* reserved field */
890	for (i = 0; i < WME_NUM_AC; i++) {
891		const struct wmeParams *ac =
892		       &wme->wme_bssChanParams.cap_wmeParams[i];
893		*frm++ = SM(i, WME_PARAM_ACI)
894		       | SM(ac->wmep_acm, WME_PARAM_ACM)
895		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
896		       ;
897		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
898		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
899		       ;
900		ADDSHORT(frm, ac->wmep_txopLimit);
901	}
902	return frm;
903#undef SM
904#undef ADDSHORT
905}
906#undef WME_OUI_BYTES
907
908/*
909 * Send a management frame.  The node is for the destination (or ic_bss
910 * when in station mode).  Nodes other than ic_bss have their reference
911 * count bumped to reflect our use for an indeterminant time.
912 */
913int
914ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
915	int type, int arg)
916{
917#define	senderr(_x, _v)	do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
918	struct mbuf *m;
919	u_int8_t *frm;
920	enum ieee80211_phymode mode;
921	u_int16_t capinfo;
922	int has_challenge, is_shared_key, ret, timer, status;
923
924	KASSERT(ni != NULL, ("null node"));
925
926	/*
927	 * Hold a reference on the node so it doesn't go away until after
928	 * the xmit is complete all the way in the driver.  On error we
929	 * will remove our reference.
930	 */
931	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE,
932		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
933		__func__, __LINE__,
934		ni, ether_sprintf(ni->ni_macaddr),
935		ieee80211_node_refcnt(ni)+1);
936	ieee80211_ref_node(ni);
937
938	timer = 0;
939	switch (type) {
940	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
941		/*
942		 * prreq frame format
943		 *	[tlv] ssid
944		 *	[tlv] supported rates
945		 *	[tlv] extended supported rates
946		 *	[tlv] user-specified ie's
947		 */
948		m = ieee80211_getmgtframe(&frm,
949			 2 + IEEE80211_NWID_LEN
950		       + 2 + IEEE80211_RATE_SIZE
951		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
952		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
953		);
954		if (m == NULL)
955			senderr(ENOMEM, is_tx_nobuf);
956
957		frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
958		mode = ieee80211_chan2mode(ic, ni->ni_chan);
959		frm = ieee80211_add_rates(frm, &ic->ic_sup_rates[mode]);
960		frm = ieee80211_add_xrates(frm, &ic->ic_sup_rates[mode]);
961		if (ic->ic_opt_ie != NULL) {
962			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
963			frm += ic->ic_opt_ie_len;
964		}
965		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
966
967		IEEE80211_NODE_STAT(ni, tx_probereq);
968		if (ic->ic_opmode == IEEE80211_M_STA)
969			timer = IEEE80211_TRANS_WAIT;
970		break;
971
972	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
973		/*
974		 * probe response frame format
975		 *	[8] time stamp
976		 *	[2] beacon interval
977		 *	[2] cabability information
978		 *	[tlv] ssid
979		 *	[tlv] supported rates
980		 *	[tlv] parameter set (FH/DS)
981		 *	[tlv] parameter set (IBSS)
982		 *	[tlv] extended rate phy (ERP)
983		 *	[tlv] extended supported rates
984		 *	[tlv] WPA
985		 *	[tlv] WME (optional)
986		 */
987		m = ieee80211_getmgtframe(&frm,
988			 8
989		       + sizeof(u_int16_t)
990		       + sizeof(u_int16_t)
991		       + 2 + IEEE80211_NWID_LEN
992		       + 2 + IEEE80211_RATE_SIZE
993		       + 7	/* max(7,3) */
994		       + 6
995		       + 3
996		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
997		       /* XXX !WPA1+WPA2 fits w/o a cluster */
998		       + (ic->ic_flags & IEEE80211_F_WPA ?
999				2*sizeof(struct ieee80211_ie_wpa) : 0)
1000		       + sizeof(struct ieee80211_wme_param)
1001		);
1002		if (m == NULL)
1003			senderr(ENOMEM, is_tx_nobuf);
1004
1005		memset(frm, 0, 8);	/* timestamp should be filled later */
1006		frm += 8;
1007		*(u_int16_t *)frm = htole16(ic->ic_bss->ni_intval);
1008		frm += 2;
1009		if (ic->ic_opmode == IEEE80211_M_IBSS)
1010			capinfo = IEEE80211_CAPINFO_IBSS;
1011		else
1012			capinfo = IEEE80211_CAPINFO_ESS;
1013		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1014			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1015		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1016		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1017			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1018		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1019			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1020		*(u_int16_t *)frm = htole16(capinfo);
1021		frm += 2;
1022
1023		frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
1024				ic->ic_bss->ni_esslen);
1025		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1026
1027		if (ic->ic_phytype == IEEE80211_T_FH) {
1028                        *frm++ = IEEE80211_ELEMID_FHPARMS;
1029                        *frm++ = 5;
1030                        *frm++ = ni->ni_fhdwell & 0x00ff;
1031                        *frm++ = (ni->ni_fhdwell >> 8) & 0x00ff;
1032                        *frm++ = IEEE80211_FH_CHANSET(
1033			    ieee80211_chan2ieee(ic, ni->ni_chan));
1034                        *frm++ = IEEE80211_FH_CHANPAT(
1035			    ieee80211_chan2ieee(ic, ni->ni_chan));
1036                        *frm++ = ni->ni_fhindex;
1037		} else {
1038			*frm++ = IEEE80211_ELEMID_DSPARMS;
1039			*frm++ = 1;
1040			*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1041		}
1042
1043		if (ic->ic_opmode == IEEE80211_M_IBSS) {
1044			*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1045			*frm++ = 2;
1046			*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1047		}
1048		if (ic->ic_flags & IEEE80211_F_WPA)
1049			frm = ieee80211_add_wpa(frm, ic);
1050		if (ic->ic_curmode == IEEE80211_MODE_11G)
1051			frm = ieee80211_add_erp(frm, ic);
1052		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1053		if (ic->ic_flags & IEEE80211_F_WME)
1054			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1055		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1056		break;
1057
1058	case IEEE80211_FC0_SUBTYPE_AUTH:
1059		status = arg >> 16;
1060		arg &= 0xffff;
1061		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1062		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1063		    ni->ni_challenge != NULL);
1064
1065		/*
1066		 * Deduce whether we're doing open authentication or
1067		 * shared key authentication.  We do the latter if
1068		 * we're in the middle of a shared key authentication
1069		 * handshake or if we're initiating an authentication
1070		 * request and configured to use shared key.
1071		 */
1072		is_shared_key = has_challenge ||
1073		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1074		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1075		      ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED);
1076
1077		m = ieee80211_getmgtframe(&frm,
1078			  3 * sizeof(u_int16_t)
1079			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1080				sizeof(u_int16_t)+IEEE80211_CHALLENGE_LEN : 0)
1081		);
1082		if (m == NULL)
1083			senderr(ENOMEM, is_tx_nobuf);
1084
1085		((u_int16_t *)frm)[0] =
1086		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1087		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
1088		((u_int16_t *)frm)[1] = htole16(arg);	/* sequence number */
1089		((u_int16_t *)frm)[2] = htole16(status);/* status */
1090
1091		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1092			((u_int16_t *)frm)[3] =
1093			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
1094			    IEEE80211_ELEMID_CHALLENGE);
1095			memcpy(&((u_int16_t *)frm)[4], ni->ni_challenge,
1096			    IEEE80211_CHALLENGE_LEN);
1097			m->m_pkthdr.len = m->m_len =
1098				4 * sizeof(u_int16_t) + IEEE80211_CHALLENGE_LEN;
1099			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1100				IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1101				    "[%s] request encrypt frame (%s)\n",
1102				    ether_sprintf(ni->ni_macaddr), __func__);
1103				m->m_flags |= M_LINK0; /* WEP-encrypt, please */
1104			}
1105		} else
1106			m->m_pkthdr.len = m->m_len = 3 * sizeof(u_int16_t);
1107
1108		/* XXX not right for shared key */
1109		if (status == IEEE80211_STATUS_SUCCESS)
1110			IEEE80211_NODE_STAT(ni, tx_auth);
1111		else
1112			IEEE80211_NODE_STAT(ni, tx_auth_fail);
1113
1114		if (ic->ic_opmode == IEEE80211_M_STA)
1115			timer = IEEE80211_TRANS_WAIT;
1116		break;
1117
1118	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1119		IEEE80211_DPRINTF(ic, IEEE80211_MSG_AUTH,
1120			"[%s] send station deauthenticate (reason %d)\n",
1121			ether_sprintf(ni->ni_macaddr), arg);
1122		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1123		if (m == NULL)
1124			senderr(ENOMEM, is_tx_nobuf);
1125		*(u_int16_t *)frm = htole16(arg);	/* reason */
1126		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1127
1128		IEEE80211_NODE_STAT(ni, tx_deauth);
1129		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1130
1131		ieee80211_node_unauthorize(ic, ni);	/* port closed */
1132		break;
1133
1134	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1135	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1136		/*
1137		 * asreq frame format
1138		 *	[2] capability information
1139		 *	[2] listen interval
1140		 *	[6*] current AP address (reassoc only)
1141		 *	[tlv] ssid
1142		 *	[tlv] supported rates
1143		 *	[tlv] extended supported rates
1144		 *	[tlv] WME
1145		 *	[tlv] user-specified ie's
1146		 */
1147		m = ieee80211_getmgtframe(&frm,
1148			 sizeof(u_int16_t)
1149		       + sizeof(u_int16_t)
1150		       + IEEE80211_ADDR_LEN
1151		       + 2 + IEEE80211_NWID_LEN
1152		       + 2 + IEEE80211_RATE_SIZE
1153		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1154		       + sizeof(struct ieee80211_wme_info)
1155		       + (ic->ic_opt_ie != NULL ? ic->ic_opt_ie_len : 0)
1156		);
1157		if (m == NULL)
1158			senderr(ENOMEM, is_tx_nobuf);
1159
1160		capinfo = 0;
1161		if (ic->ic_opmode == IEEE80211_M_IBSS)
1162			capinfo |= IEEE80211_CAPINFO_IBSS;
1163		else		/* IEEE80211_M_STA */
1164			capinfo |= IEEE80211_CAPINFO_ESS;
1165		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1166			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1167		/*
1168		 * NB: Some 11a AP's reject the request when
1169		 *     short premable is set.
1170		 */
1171		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1172		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1173			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1174		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) &&
1175		    (ic->ic_caps & IEEE80211_C_SHSLOT))
1176			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1177		*(u_int16_t *)frm = htole16(capinfo);
1178		frm += 2;
1179
1180		*(u_int16_t *)frm = htole16(ic->ic_lintval);
1181		frm += 2;
1182
1183		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
1184			IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
1185			frm += IEEE80211_ADDR_LEN;
1186		}
1187
1188		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
1189		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1190		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1191		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1192			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1193		if (ic->ic_opt_ie != NULL) {
1194			memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
1195			frm += ic->ic_opt_ie_len;
1196		}
1197		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1198
1199		timer = IEEE80211_TRANS_WAIT;
1200		break;
1201
1202	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1203	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1204		/*
1205		 * asreq frame format
1206		 *	[2] capability information
1207		 *	[2] status
1208		 *	[2] association ID
1209		 *	[tlv] supported rates
1210		 *	[tlv] extended supported rates
1211		 *	[tlv] WME (if enabled and STA enabled)
1212		 */
1213		m = ieee80211_getmgtframe(&frm,
1214			 sizeof(u_int16_t)
1215		       + sizeof(u_int16_t)
1216		       + sizeof(u_int16_t)
1217		       + 2 + IEEE80211_RATE_SIZE
1218		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1219		       + sizeof(struct ieee80211_wme_param)
1220		);
1221		if (m == NULL)
1222			senderr(ENOMEM, is_tx_nobuf);
1223
1224		capinfo = IEEE80211_CAPINFO_ESS;
1225		if (ic->ic_flags & IEEE80211_F_PRIVACY)
1226			capinfo |= IEEE80211_CAPINFO_PRIVACY;
1227		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1228		    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1229			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1230		if (ic->ic_flags & IEEE80211_F_SHSLOT)
1231			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1232		*(u_int16_t *)frm = htole16(capinfo);
1233		frm += 2;
1234
1235		*(u_int16_t *)frm = htole16(arg);	/* status */
1236		frm += 2;
1237
1238		if (arg == IEEE80211_STATUS_SUCCESS) {
1239			*(u_int16_t *)frm = htole16(ni->ni_associd);
1240			IEEE80211_NODE_STAT(ni, tx_assoc);
1241		} else
1242			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
1243		frm += 2;
1244
1245		frm = ieee80211_add_rates(frm, &ni->ni_rates);
1246		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
1247		if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
1248			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1249		m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1250		break;
1251
1252	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1253		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ASSOC,
1254			"[%s] send station disassociate (reason %d)\n",
1255			ether_sprintf(ni->ni_macaddr), arg);
1256		m = ieee80211_getmgtframe(&frm, sizeof(u_int16_t));
1257		if (m == NULL)
1258			senderr(ENOMEM, is_tx_nobuf);
1259		*(u_int16_t *)frm = htole16(arg);	/* reason */
1260		m->m_pkthdr.len = m->m_len = sizeof(u_int16_t);
1261
1262		IEEE80211_NODE_STAT(ni, tx_disassoc);
1263		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
1264		break;
1265
1266	default:
1267		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1268			"[%s] invalid mgmt frame type %u\n",
1269			ether_sprintf(ni->ni_macaddr), type);
1270		senderr(EINVAL, is_tx_unknownmgt);
1271		/* NOTREACHED */
1272	}
1273
1274	ret = ieee80211_mgmt_output(ic, ni, m, type);
1275	if (ret == 0) {
1276		if (timer)
1277			ic->ic_mgt_timer = timer;
1278	} else {
1279bad:
1280		ieee80211_free_node(ni);
1281	}
1282	return ret;
1283#undef senderr
1284}
1285
1286/*
1287 * Allocate a beacon frame and fillin the appropriate bits.
1288 */
1289struct mbuf *
1290ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni,
1291	struct ieee80211_beacon_offsets *bo)
1292{
1293	struct ifnet *ifp = ic->ic_ifp;
1294	struct ieee80211_frame *wh;
1295	struct mbuf *m;
1296	int pktlen;
1297	u_int8_t *frm, *efrm;
1298	u_int16_t capinfo;
1299	struct ieee80211_rateset *rs;
1300
1301	/*
1302	 * beacon frame format
1303	 *	[8] time stamp
1304	 *	[2] beacon interval
1305	 *	[2] cabability information
1306	 *	[tlv] ssid
1307	 *	[tlv] supported rates
1308	 *	[3] parameter set (DS)
1309	 *	[tlv] parameter set (IBSS/TIM)
1310	 *	[tlv] extended rate phy (ERP)
1311	 *	[tlv] extended supported rates
1312	 *	[tlv] WME parameters
1313	 *	[tlv] WPA/RSN parameters
1314	 * XXX Vendor-specific OIDs (e.g. Atheros)
1315	 * NB: we allocate the max space required for the TIM bitmap.
1316	 */
1317	rs = &ni->ni_rates;
1318	pktlen =   8					/* time stamp */
1319		 + sizeof(u_int16_t)			/* beacon interval */
1320		 + sizeof(u_int16_t)			/* capabilities */
1321		 + 2 + ni->ni_esslen			/* ssid */
1322	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
1323	         + 2 + 1				/* DS parameters */
1324		 + 2 + 4 + ic->ic_tim_len		/* DTIM/IBSSPARMS */
1325		 + 2 + 1				/* ERP */
1326	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1327		 + (ic->ic_caps & IEEE80211_C_WME ?	/* WME */
1328			sizeof(struct ieee80211_wme_param) : 0)
1329		 + (ic->ic_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
1330			2*sizeof(struct ieee80211_ie_wpa) : 0)
1331		 ;
1332	m = ieee80211_getmgtframe(&frm, pktlen);
1333	if (m == NULL) {
1334		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1335			"%s: cannot get buf; size %u\n", __func__, pktlen);
1336		ic->ic_stats.is_tx_nobuf++;
1337		return NULL;
1338	}
1339
1340	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
1341	frm += 8;
1342	*(u_int16_t *)frm = htole16(ni->ni_intval);
1343	frm += 2;
1344	if (ic->ic_opmode == IEEE80211_M_IBSS)
1345		capinfo = IEEE80211_CAPINFO_IBSS;
1346	else
1347		capinfo = IEEE80211_CAPINFO_ESS;
1348	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1349		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1350	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1351	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1352		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1353	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1354		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1355	bo->bo_caps = (u_int16_t *)frm;
1356	*(u_int16_t *)frm = htole16(capinfo);
1357	frm += 2;
1358	*frm++ = IEEE80211_ELEMID_SSID;
1359	if ((ic->ic_flags & IEEE80211_F_HIDESSID) == 0) {
1360		*frm++ = ni->ni_esslen;
1361		memcpy(frm, ni->ni_essid, ni->ni_esslen);
1362		frm += ni->ni_esslen;
1363	} else
1364		*frm++ = 0;
1365	frm = ieee80211_add_rates(frm, rs);
1366	if (ic->ic_curmode != IEEE80211_MODE_FH) {
1367		*frm++ = IEEE80211_ELEMID_DSPARMS;
1368		*frm++ = 1;
1369		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
1370	}
1371	bo->bo_tim = frm;
1372	if (ic->ic_opmode == IEEE80211_M_IBSS) {
1373		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
1374		*frm++ = 2;
1375		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
1376		bo->bo_tim_len = 0;
1377	} else {
1378		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
1379
1380		tie->tim_ie = IEEE80211_ELEMID_TIM;
1381		tie->tim_len = 4;	/* length */
1382		tie->tim_count = 0;	/* DTIM count */
1383		tie->tim_period = ic->ic_dtim_period;	/* DTIM period */
1384		tie->tim_bitctl = 0;	/* bitmap control */
1385		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
1386		frm += sizeof(struct ieee80211_tim_ie);
1387		bo->bo_tim_len = 1;
1388	}
1389	bo->bo_trailer = frm;
1390	if (ic->ic_flags & IEEE80211_F_WME) {
1391		bo->bo_wme = frm;
1392		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
1393		ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1394	}
1395	if (ic->ic_flags & IEEE80211_F_WPA)
1396		frm = ieee80211_add_wpa(frm, ic);
1397	if (ic->ic_curmode == IEEE80211_MODE_11G)
1398		frm = ieee80211_add_erp(frm, ic);
1399	efrm = ieee80211_add_xrates(frm, rs);
1400	bo->bo_trailer_len = efrm - bo->bo_trailer;
1401	m->m_pkthdr.len = m->m_len = efrm - mtod(m, u_int8_t *);
1402
1403	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1404	KASSERT(m != NULL, ("no space for 802.11 header?"));
1405	wh = mtod(m, struct ieee80211_frame *);
1406	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1407	    IEEE80211_FC0_SUBTYPE_BEACON;
1408	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1409	*(u_int16_t *)wh->i_dur = 0;
1410	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1411	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
1412	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
1413	*(u_int16_t *)wh->i_seq = 0;
1414
1415	return m;
1416}
1417
1418/*
1419 * Update the dynamic parts of a beacon frame based on the current state.
1420 */
1421int
1422ieee80211_beacon_update(struct ieee80211com *ic, struct ieee80211_node *ni,
1423	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
1424{
1425	int len_changed = 0;
1426	u_int16_t capinfo;
1427
1428	IEEE80211_BEACON_LOCK(ic);
1429	/* XXX faster to recalculate entirely or just changes? */
1430	if (ic->ic_opmode == IEEE80211_M_IBSS)
1431		capinfo = IEEE80211_CAPINFO_IBSS;
1432	else
1433		capinfo = IEEE80211_CAPINFO_ESS;
1434	if (ic->ic_flags & IEEE80211_F_PRIVACY)
1435		capinfo |= IEEE80211_CAPINFO_PRIVACY;
1436	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1437	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
1438		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1439	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1440		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1441	*bo->bo_caps = htole16(capinfo);
1442
1443	if (ic->ic_flags & IEEE80211_F_WME) {
1444		struct ieee80211_wme_state *wme = &ic->ic_wme;
1445
1446		/*
1447		 * Check for agressive mode change.  When there is
1448		 * significant high priority traffic in the BSS
1449		 * throttle back BE traffic by using conservative
1450		 * parameters.  Otherwise BE uses agressive params
1451		 * to optimize performance of legacy/non-QoS traffic.
1452		 */
1453		if (wme->wme_flags & WME_F_AGGRMODE) {
1454			if (wme->wme_hipri_traffic >
1455			    wme->wme_hipri_switch_thresh) {
1456				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1457				    "%s: traffic %u, disable aggressive mode\n",
1458				    __func__, wme->wme_hipri_traffic);
1459				wme->wme_flags &= ~WME_F_AGGRMODE;
1460				ieee80211_wme_updateparams_locked(ic);
1461				wme->wme_hipri_traffic =
1462					wme->wme_hipri_switch_hysteresis;
1463			} else
1464				wme->wme_hipri_traffic = 0;
1465		} else {
1466			if (wme->wme_hipri_traffic <=
1467			    wme->wme_hipri_switch_thresh) {
1468				IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
1469				    "%s: traffic %u, enable aggressive mode\n",
1470				    __func__, wme->wme_hipri_traffic);
1471				wme->wme_flags |= WME_F_AGGRMODE;
1472				ieee80211_wme_updateparams_locked(ic);
1473				wme->wme_hipri_traffic = 0;
1474			} else
1475				wme->wme_hipri_traffic =
1476					wme->wme_hipri_switch_hysteresis;
1477		}
1478		if (ic->ic_flags & IEEE80211_F_WMEUPDATE) {
1479			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
1480			ic->ic_flags &= ~IEEE80211_F_WMEUPDATE;
1481		}
1482	}
1483
1484	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* NB: no IBSS support*/
1485		struct ieee80211_tim_ie *tie =
1486			(struct ieee80211_tim_ie *) bo->bo_tim;
1487		if (ic->ic_flags & IEEE80211_F_TIMUPDATE) {
1488			u_int timlen, timoff, i;
1489			/*
1490			 * ATIM/DTIM needs updating.  If it fits in the
1491			 * current space allocated then just copy in the
1492			 * new bits.  Otherwise we need to move any trailing
1493			 * data to make room.  Note that we know there is
1494			 * contiguous space because ieee80211_beacon_allocate
1495			 * insures there is space in the mbuf to write a
1496			 * maximal-size virtual bitmap (based on ic_max_aid).
1497			 */
1498			/*
1499			 * Calculate the bitmap size and offset, copy any
1500			 * trailer out of the way, and then copy in the
1501			 * new bitmap and update the information element.
1502			 * Note that the tim bitmap must contain at least
1503			 * one byte and any offset must be even.
1504			 */
1505			if (ic->ic_ps_pending != 0) {
1506				timoff = 128;		/* impossibly large */
1507				for (i = 0; i < ic->ic_tim_len; i++)
1508					if (ic->ic_tim_bitmap[i]) {
1509						timoff = i &~ 1;
1510						break;
1511					}
1512				KASSERT(timoff != 128, ("tim bitmap empty!"));
1513				for (i = ic->ic_tim_len-1; i >= timoff; i--)
1514					if (ic->ic_tim_bitmap[i])
1515						break;
1516				timlen = 1 + (i - timoff);
1517			} else {
1518				timoff = 0;
1519				timlen = 1;
1520			}
1521			if (timlen != bo->bo_tim_len) {
1522				/* copy up/down trailer */
1523				ovbcopy(bo->bo_trailer, tie->tim_bitmap+timlen,
1524					bo->bo_trailer_len);
1525				bo->bo_trailer = tie->tim_bitmap+timlen;
1526				bo->bo_wme = bo->bo_trailer;
1527				bo->bo_tim_len = timlen;
1528
1529				/* update information element */
1530				tie->tim_len = 3 + timlen;
1531				tie->tim_bitctl = timoff;
1532				len_changed = 1;
1533			}
1534			memcpy(tie->tim_bitmap, ic->ic_tim_bitmap + timoff,
1535				bo->bo_tim_len);
1536
1537			ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
1538
1539			IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1540				"%s: TIM updated, pending %u, off %u, len %u\n",
1541				__func__, ic->ic_ps_pending, timoff, timlen);
1542		}
1543		/* count down DTIM period */
1544		if (tie->tim_count == 0)
1545			tie->tim_count = tie->tim_period - 1;
1546		else
1547			tie->tim_count--;
1548		/* update state for buffered multicast frames on DTIM */
1549		if (mcast && (tie->tim_count == 1 || tie->tim_period == 1))
1550			tie->tim_bitctl |= 1;
1551		else
1552			tie->tim_bitctl &= ~1;
1553	}
1554	IEEE80211_BEACON_UNLOCK(ic);
1555
1556	return len_changed;
1557}
1558
1559/*
1560 * Save an outbound packet for a node in power-save sleep state.
1561 * The new packet is placed on the node's saved queue, and the TIM
1562 * is changed, if necessary.
1563 */
1564void
1565ieee80211_pwrsave(struct ieee80211com *ic, struct ieee80211_node *ni,
1566		  struct mbuf *m)
1567{
1568	int qlen, age;
1569
1570	IEEE80211_NODE_SAVEQ_LOCK(ni);
1571	if (_IF_QFULL(&ni->ni_savedq)) {
1572		_IF_DROP(&ni->ni_savedq);
1573		IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1574		IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
1575			"[%s] pwr save q overflow, drops %d (size %d)\n",
1576			ether_sprintf(ni->ni_macaddr),
1577			ni->ni_savedq.ifq_drops, IEEE80211_PS_MAX_QUEUE);
1578#ifdef IEEE80211_DEBUG
1579		if (ieee80211_msg_dumppkts(ic))
1580			ieee80211_dump_pkt(mtod(m, caddr_t), m->m_len, -1, -1);
1581#endif
1582		m_freem(m);
1583		return;
1584	}
1585	/*
1586	 * Tag the frame with it's expiry time and insert
1587	 * it in the queue.  The aging interval is 4 times
1588	 * the listen interval specified by the station.
1589	 * Frames that sit around too long are reclaimed
1590	 * using this information.
1591	 */
1592	/* XXX handle overflow? */
1593	age = ((ni->ni_intval * ic->ic_lintval) << 2) / 1024; /* TU -> secs */
1594	_IEEE80211_NODE_SAVEQ_ENQUEUE(ni, m, qlen, age);
1595	IEEE80211_NODE_SAVEQ_UNLOCK(ni);
1596
1597	IEEE80211_DPRINTF(ic, IEEE80211_MSG_POWER,
1598		"[%s] save frame, %u now queued\n",
1599		ether_sprintf(ni->ni_macaddr), qlen);
1600
1601	if (qlen == 1)
1602		ic->ic_set_tim(ic, ni, 1);
1603}
1604