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