ieee80211_output.c revision 254076
1/*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_output.c 254076 2013-08-07 22:01:43Z adrian $");
29
30#include "opt_inet.h"
31#include "opt_inet6.h"
32#include "opt_wlan.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/mbuf.h>
37#include <sys/kernel.h>
38#include <sys/endian.h>
39
40#include <sys/socket.h>
41
42#include <net/bpf.h>
43#include <net/ethernet.h>
44#include <net/if.h>
45#include <net/if_llc.h>
46#include <net/if_media.h>
47#include <net/if_vlan_var.h>
48
49#include <net80211/ieee80211_var.h>
50#include <net80211/ieee80211_regdomain.h>
51#ifdef IEEE80211_SUPPORT_SUPERG
52#include <net80211/ieee80211_superg.h>
53#endif
54#ifdef IEEE80211_SUPPORT_TDMA
55#include <net80211/ieee80211_tdma.h>
56#endif
57#include <net80211/ieee80211_wds.h>
58#include <net80211/ieee80211_mesh.h>
59
60#if defined(INET) || defined(INET6)
61#include <netinet/in.h>
62#endif
63
64#ifdef INET
65#include <netinet/if_ether.h>
66#include <netinet/in_systm.h>
67#include <netinet/ip.h>
68#endif
69#ifdef INET6
70#include <netinet/ip6.h>
71#endif
72
73#include <security/mac/mac_framework.h>
74
75#define	ETHER_HEADER_COPY(dst, src) \
76	memcpy(dst, src, sizeof(struct ether_header))
77
78/* unalligned little endian access */
79#define LE_WRITE_2(p, v) do {				\
80	((uint8_t *)(p))[0] = (v) & 0xff;		\
81	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
82} while (0)
83#define LE_WRITE_4(p, v) do {				\
84	((uint8_t *)(p))[0] = (v) & 0xff;		\
85	((uint8_t *)(p))[1] = ((v) >> 8) & 0xff;	\
86	((uint8_t *)(p))[2] = ((v) >> 16) & 0xff;	\
87	((uint8_t *)(p))[3] = ((v) >> 24) & 0xff;	\
88} while (0)
89
90static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
91	u_int hdrsize, u_int ciphdrsize, u_int mtu);
92static	void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
93
94#ifdef IEEE80211_DEBUG
95/*
96 * Decide if an outbound management frame should be
97 * printed when debugging is enabled.  This filters some
98 * of the less interesting frames that come frequently
99 * (e.g. beacons).
100 */
101static __inline int
102doprint(struct ieee80211vap *vap, int subtype)
103{
104	switch (subtype) {
105	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
106		return (vap->iv_opmode == IEEE80211_M_IBSS);
107	}
108	return 1;
109}
110#endif
111
112/*
113 * Transmit a frame to the given destination on the given VAP.
114 *
115 * It's up to the caller to figure out the details of who this
116 * is going to and resolving the node.
117 *
118 * This routine takes care of queuing it for power save,
119 * A-MPDU state stuff, fast-frames state stuff, encapsulation
120 * if required, then passing it up to the driver layer.
121 *
122 * This routine (for now) consumes the mbuf and frees the node
123 * reference; it ideally will return a TX status which reflects
124 * whether the mbuf was consumed or not, so the caller can
125 * free the mbuf (if appropriate) and the node reference (again,
126 * if appropriate.)
127 */
128int
129ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
130    struct ieee80211_node *ni)
131{
132	struct ieee80211com *ic = vap->iv_ic;
133	struct ifnet *ifp = vap->iv_ifp;
134	int error;
135
136	if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
137	    (m->m_flags & M_PWR_SAV) == 0) {
138		/*
139		 * Station in power save mode; pass the frame
140		 * to the 802.11 layer and continue.  We'll get
141		 * the frame back when the time is right.
142		 * XXX lose WDS vap linkage?
143		 */
144		(void) ieee80211_pwrsave(ni, m);
145		ieee80211_free_node(ni);
146		/* XXX better status? */
147		return (ENOBUFS);
148	}
149	/* calculate priority so drivers can find the tx queue */
150	if (ieee80211_classify(ni, m)) {
151		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
152		    ni->ni_macaddr, NULL,
153		    "%s", "classification failure");
154		vap->iv_stats.is_tx_classify++;
155		ifp->if_oerrors++;
156		m_freem(m);
157		ieee80211_free_node(ni);
158		/* XXX better status? */
159		return (ENOBUFS);
160	}
161	/*
162	 * Stash the node pointer.  Note that we do this after
163	 * any call to ieee80211_dwds_mcast because that code
164	 * uses any existing value for rcvif to identify the
165	 * interface it (might have been) received on.
166	 */
167	m->m_pkthdr.rcvif = (void *)ni;
168
169	BPF_MTAP(ifp, m);		/* 802.3 tx */
170
171
172	/*
173	 * Check if A-MPDU tx aggregation is setup or if we
174	 * should try to enable it.  The sta must be associated
175	 * with HT and A-MPDU enabled for use.  When the policy
176	 * routine decides we should enable A-MPDU we issue an
177	 * ADDBA request and wait for a reply.  The frame being
178	 * encapsulated will go out w/o using A-MPDU, or possibly
179	 * it might be collected by the driver and held/retransmit.
180	 * The default ic_ampdu_enable routine handles staggering
181	 * ADDBA requests in case the receiver NAK's us or we are
182	 * otherwise unable to establish a BA stream.
183	 */
184	if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
185	    (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
186	    (m->m_flags & M_EAPOL) == 0) {
187		int tid = WME_AC_TO_TID(M_WME_GETAC(m));
188		struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
189
190		ieee80211_txampdu_count_packet(tap);
191		if (IEEE80211_AMPDU_RUNNING(tap)) {
192			/*
193			 * Operational, mark frame for aggregation.
194			 *
195			 * XXX do tx aggregation here
196			 */
197			m->m_flags |= M_AMPDU_MPDU;
198		} else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
199		    ic->ic_ampdu_enable(ni, tap)) {
200			/*
201			 * Not negotiated yet, request service.
202			 */
203			ieee80211_ampdu_request(ni, tap);
204			/* XXX hold frame for reply? */
205		}
206	}
207
208#ifdef IEEE80211_SUPPORT_SUPERG
209	else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
210		m = ieee80211_ff_check(ni, m);
211		if (m == NULL) {
212			/* NB: any ni ref held on stageq */
213			/* XXX better status? */
214			return (ENOBUFS);
215		}
216	}
217#endif /* IEEE80211_SUPPORT_SUPERG */
218
219	/*
220	 * Grab the TX lock - serialise the TX process from this
221	 * point (where TX state is being checked/modified)
222	 * through to driver queue.
223	 */
224	IEEE80211_TX_LOCK(ic);
225
226	if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
227		/*
228		 * Encapsulate the packet in prep for transmission.
229		 */
230		m = ieee80211_encap(vap, ni, m);
231		if (m == NULL) {
232			/* NB: stat+msg handled in ieee80211_encap */
233			IEEE80211_TX_UNLOCK(ic);
234			ieee80211_free_node(ni);
235			/* XXX better status? */
236			return (ENOBUFS);
237		}
238	}
239	error = ieee80211_parent_transmit(ic, m);
240
241	/*
242	 * Unlock at this point - no need to hold it across
243	 * ieee80211_free_node() (ie, the comlock)
244	 */
245	IEEE80211_TX_UNLOCK(ic);
246	if (error != 0) {
247		/* NB: IFQ_HANDOFF reclaims mbuf */
248		ieee80211_free_node(ni);
249	} else {
250		ifp->if_opackets++;
251	}
252	ic->ic_lastdata = ticks;
253
254	return (0);
255}
256
257
258
259/*
260 * Send the given mbuf through the given vap.
261 *
262 * This consumes the mbuf regardless of whether the transmit
263 * was successful or not.
264 *
265 * This does none of the initial checks that ieee80211_start()
266 * does (eg CAC timeout, interface wakeup) - the caller must
267 * do this first.
268 */
269static int
270ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
271{
272#define	IS_DWDS(vap) \
273	(vap->iv_opmode == IEEE80211_M_WDS && \
274	 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
275	struct ieee80211com *ic = vap->iv_ic;
276	struct ifnet *ifp = vap->iv_ifp;
277	struct ieee80211_node *ni;
278	struct ether_header *eh;
279
280	/*
281	 * Cancel any background scan.
282	 */
283	if (ic->ic_flags & IEEE80211_F_SCAN)
284		ieee80211_cancel_anyscan(vap);
285	/*
286	 * Find the node for the destination so we can do
287	 * things like power save and fast frames aggregation.
288	 *
289	 * NB: past this point various code assumes the first
290	 *     mbuf has the 802.3 header present (and contiguous).
291	 */
292	ni = NULL;
293	if (m->m_len < sizeof(struct ether_header) &&
294	   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
295		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
296		    "discard frame, %s\n", "m_pullup failed");
297		vap->iv_stats.is_tx_nobuf++;	/* XXX */
298		ifp->if_oerrors++;
299		return (ENOBUFS);
300	}
301	eh = mtod(m, struct ether_header *);
302	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
303		if (IS_DWDS(vap)) {
304			/*
305			 * Only unicast frames from the above go out
306			 * DWDS vaps; multicast frames are handled by
307			 * dispatching the frame as it comes through
308			 * the AP vap (see below).
309			 */
310			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
311			    eh->ether_dhost, "mcast", "%s", "on DWDS");
312			vap->iv_stats.is_dwds_mcast++;
313			m_freem(m);
314			/* XXX better status? */
315			return (ENOBUFS);
316		}
317		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
318			/*
319			 * Spam DWDS vap's w/ multicast traffic.
320			 */
321			/* XXX only if dwds in use? */
322			ieee80211_dwds_mcast(vap, m);
323		}
324	}
325#ifdef IEEE80211_SUPPORT_MESH
326	if (vap->iv_opmode != IEEE80211_M_MBSS) {
327#endif
328		ni = ieee80211_find_txnode(vap, eh->ether_dhost);
329		if (ni == NULL) {
330			/* NB: ieee80211_find_txnode does stat+msg */
331			ifp->if_oerrors++;
332			m_freem(m);
333			/* XXX better status? */
334			return (ENOBUFS);
335		}
336		if (ni->ni_associd == 0 &&
337		    (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
338			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
339			    eh->ether_dhost, NULL,
340			    "sta not associated (type 0x%04x)",
341			    htons(eh->ether_type));
342			vap->iv_stats.is_tx_notassoc++;
343			ifp->if_oerrors++;
344			m_freem(m);
345			ieee80211_free_node(ni);
346			/* XXX better status? */
347			return (ENOBUFS);
348		}
349#ifdef IEEE80211_SUPPORT_MESH
350	} else {
351		if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
352			/*
353			 * Proxy station only if configured.
354			 */
355			if (!ieee80211_mesh_isproxyena(vap)) {
356				IEEE80211_DISCARD_MAC(vap,
357				    IEEE80211_MSG_OUTPUT |
358				    IEEE80211_MSG_MESH,
359				    eh->ether_dhost, NULL,
360				    "%s", "proxy not enabled");
361				vap->iv_stats.is_mesh_notproxy++;
362				ifp->if_oerrors++;
363				m_freem(m);
364				/* XXX better status? */
365				return (ENOBUFS);
366			}
367			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
368			    "forward frame from DS SA(%6D), DA(%6D)\n",
369			    eh->ether_shost, ":",
370			    eh->ether_dhost, ":");
371			ieee80211_mesh_proxy_check(vap, eh->ether_shost);
372		}
373		ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
374		if (ni == NULL) {
375			/*
376			 * NB: ieee80211_mesh_discover holds/disposes
377			 * frame (e.g. queueing on path discovery).
378			 */
379			ifp->if_oerrors++;
380			/* XXX better status? */
381			return (ENOBUFS);
382		}
383	}
384#endif
385
386	/*
387	 * We've resolved the sender, so attempt to transmit it.
388	 */
389	if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
390		return (ENOBUFS);
391	return (0);
392#undef	IS_DWDS
393}
394
395/*
396 * Start method for vap's.  All packets from the stack come
397 * through here.  We handle common processing of the packets
398 * before dispatching them to the underlying device.
399 */
400void
401ieee80211_start(struct ifnet *ifp)
402{
403	struct ieee80211vap *vap = ifp->if_softc;
404	struct ieee80211com *ic = vap->iv_ic;
405	struct ifnet *parent = ic->ic_ifp;
406	struct mbuf *m;
407
408	/* NB: parent must be up and running */
409	if (!IFNET_IS_UP_RUNNING(parent)) {
410		IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
411		    "%s: ignore queue, parent %s not up+running\n",
412		    __func__, parent->if_xname);
413		/* XXX stat */
414		return;
415	}
416	if (vap->iv_state == IEEE80211_S_SLEEP) {
417		/*
418		 * In power save, wakeup device for transmit.
419		 */
420		ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
421		return;
422	}
423	/*
424	 * No data frames go out unless we're running.
425	 * Note in particular this covers CAC and CSA
426	 * states (though maybe we should check muting
427	 * for CSA).
428	 */
429	if (vap->iv_state != IEEE80211_S_RUN) {
430		IEEE80211_LOCK(ic);
431		/* re-check under the com lock to avoid races */
432		if (vap->iv_state != IEEE80211_S_RUN) {
433			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
434			    "%s: ignore queue, in %s state\n",
435			    __func__, ieee80211_state_name[vap->iv_state]);
436			vap->iv_stats.is_tx_badstate++;
437			IEEE80211_UNLOCK(ic);
438			IFQ_LOCK(&ifp->if_snd);
439			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
440			IFQ_UNLOCK(&ifp->if_snd);
441			return;
442		}
443		IEEE80211_UNLOCK(ic);
444	}
445
446	for (;;) {
447		IFQ_DEQUEUE(&ifp->if_snd, m);
448		if (m == NULL)
449			break;
450		/*
451		 * Sanitize mbuf flags for net80211 use.  We cannot
452		 * clear M_PWR_SAV or M_MORE_DATA because these may
453		 * be set for frames that are re-submitted from the
454		 * power save queue.
455		 *
456		 * NB: This must be done before ieee80211_classify as
457		 *     it marks EAPOL in frames with M_EAPOL.
458		 */
459		m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
460		/*
461		 * Bump to the packet transmission path.
462		 */
463		(void) ieee80211_start_pkt(vap, m);
464		/* mbuf is consumed here */
465	}
466}
467
468/*
469 * 802.11 raw output routine.
470 */
471int
472ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
473    struct mbuf *m, const struct ieee80211_bpf_params *params)
474{
475	struct ieee80211com *ic = vap->iv_ic;
476
477	return (ic->ic_raw_xmit(ni, m, params));
478}
479
480/*
481 * 802.11 output routine. This is (currently) used only to
482 * connect bpf write calls to the 802.11 layer for injecting
483 * raw 802.11 frames.
484 */
485#if __FreeBSD_version >= 1000031
486int
487ieee80211_output(struct ifnet *ifp, struct mbuf *m,
488	const struct sockaddr *dst, struct route *ro)
489#else
490int
491ieee80211_output(struct ifnet *ifp, struct mbuf *m,
492	struct sockaddr *dst, struct route *ro)
493#endif
494{
495#define senderr(e) do { error = (e); goto bad;} while (0)
496	struct ieee80211_node *ni = NULL;
497	struct ieee80211vap *vap;
498	struct ieee80211_frame *wh;
499	struct ieee80211com *ic = NULL;
500	int error;
501	int ret;
502
503	IFQ_LOCK(&ifp->if_snd);
504	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
505		IFQ_UNLOCK(&ifp->if_snd);
506		/*
507		 * Short-circuit requests if the vap is marked OACTIVE
508		 * as this can happen because a packet came down through
509		 * ieee80211_start before the vap entered RUN state in
510		 * which case it's ok to just drop the frame.  This
511		 * should not be necessary but callers of if_output don't
512		 * check OACTIVE.
513		 */
514		senderr(ENETDOWN);
515	}
516	IFQ_UNLOCK(&ifp->if_snd);
517	vap = ifp->if_softc;
518	ic = vap->iv_ic;
519	/*
520	 * Hand to the 802.3 code if not tagged as
521	 * a raw 802.11 frame.
522	 */
523	if (dst->sa_family != AF_IEEE80211)
524		return vap->iv_output(ifp, m, dst, ro);
525#ifdef MAC
526	error = mac_ifnet_check_transmit(ifp, m);
527	if (error)
528		senderr(error);
529#endif
530	if (ifp->if_flags & IFF_MONITOR)
531		senderr(ENETDOWN);
532	if (!IFNET_IS_UP_RUNNING(ifp))
533		senderr(ENETDOWN);
534	if (vap->iv_state == IEEE80211_S_CAC) {
535		IEEE80211_DPRINTF(vap,
536		    IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
537		    "block %s frame in CAC state\n", "raw data");
538		vap->iv_stats.is_tx_badstate++;
539		senderr(EIO);		/* XXX */
540	} else if (vap->iv_state == IEEE80211_S_SCAN)
541		senderr(EIO);
542	/* XXX bypass bridge, pfil, carp, etc. */
543
544	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
545		senderr(EIO);	/* XXX */
546	wh = mtod(m, struct ieee80211_frame *);
547	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
548	    IEEE80211_FC0_VERSION_0)
549		senderr(EIO);	/* XXX */
550
551	/* locate destination node */
552	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
553	case IEEE80211_FC1_DIR_NODS:
554	case IEEE80211_FC1_DIR_FROMDS:
555		ni = ieee80211_find_txnode(vap, wh->i_addr1);
556		break;
557	case IEEE80211_FC1_DIR_TODS:
558	case IEEE80211_FC1_DIR_DSTODS:
559		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
560			senderr(EIO);	/* XXX */
561		ni = ieee80211_find_txnode(vap, wh->i_addr3);
562		break;
563	default:
564		senderr(EIO);	/* XXX */
565	}
566	if (ni == NULL) {
567		/*
568		 * Permit packets w/ bpf params through regardless
569		 * (see below about sa_len).
570		 */
571		if (dst->sa_len == 0)
572			senderr(EHOSTUNREACH);
573		ni = ieee80211_ref_node(vap->iv_bss);
574	}
575
576	/*
577	 * Sanitize mbuf for net80211 flags leaked from above.
578	 *
579	 * NB: This must be done before ieee80211_classify as
580	 *     it marks EAPOL in frames with M_EAPOL.
581	 */
582	m->m_flags &= ~M_80211_TX;
583
584	/* calculate priority so drivers can find the tx queue */
585	/* XXX assumes an 802.3 frame */
586	if (ieee80211_classify(ni, m))
587		senderr(EIO);		/* XXX */
588
589	ifp->if_opackets++;
590	IEEE80211_NODE_STAT(ni, tx_data);
591	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
592		IEEE80211_NODE_STAT(ni, tx_mcast);
593		m->m_flags |= M_MCAST;
594	} else
595		IEEE80211_NODE_STAT(ni, tx_ucast);
596	/* NB: ieee80211_encap does not include 802.11 header */
597	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
598
599	IEEE80211_TX_LOCK(ic);
600
601	/*
602	 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
603	 * present by setting the sa_len field of the sockaddr (yes,
604	 * this is a hack).
605	 * NB: we assume sa_data is suitably aligned to cast.
606	 */
607	ret = ieee80211_raw_output(vap, ni, m,
608	    (const struct ieee80211_bpf_params *)(dst->sa_len ?
609		dst->sa_data : NULL));
610	IEEE80211_TX_UNLOCK(ic);
611	return (ret);
612bad:
613	if (m != NULL)
614		m_freem(m);
615	if (ni != NULL)
616		ieee80211_free_node(ni);
617	ifp->if_oerrors++;
618	return error;
619#undef senderr
620}
621
622/*
623 * Set the direction field and address fields of an outgoing
624 * frame.  Note this should be called early on in constructing
625 * a frame as it sets i_fc[1]; other bits can then be or'd in.
626 */
627void
628ieee80211_send_setup(
629	struct ieee80211_node *ni,
630	struct mbuf *m,
631	int type, int tid,
632	const uint8_t sa[IEEE80211_ADDR_LEN],
633	const uint8_t da[IEEE80211_ADDR_LEN],
634	const uint8_t bssid[IEEE80211_ADDR_LEN])
635{
636#define	WH4(wh)	((struct ieee80211_frame_addr4 *)wh)
637	struct ieee80211vap *vap = ni->ni_vap;
638	struct ieee80211_tx_ampdu *tap;
639	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
640	ieee80211_seq seqno;
641
642	IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
643
644	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
645	if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
646		switch (vap->iv_opmode) {
647		case IEEE80211_M_STA:
648			wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
649			IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
650			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
651			IEEE80211_ADDR_COPY(wh->i_addr3, da);
652			break;
653		case IEEE80211_M_IBSS:
654		case IEEE80211_M_AHDEMO:
655			wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
656			IEEE80211_ADDR_COPY(wh->i_addr1, da);
657			IEEE80211_ADDR_COPY(wh->i_addr2, sa);
658			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
659			break;
660		case IEEE80211_M_HOSTAP:
661			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
662			IEEE80211_ADDR_COPY(wh->i_addr1, da);
663			IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
664			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
665			break;
666		case IEEE80211_M_WDS:
667			wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
668			IEEE80211_ADDR_COPY(wh->i_addr1, da);
669			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
670			IEEE80211_ADDR_COPY(wh->i_addr3, da);
671			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
672			break;
673		case IEEE80211_M_MBSS:
674#ifdef IEEE80211_SUPPORT_MESH
675			if (IEEE80211_IS_MULTICAST(da)) {
676				wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
677				/* XXX next hop */
678				IEEE80211_ADDR_COPY(wh->i_addr1, da);
679				IEEE80211_ADDR_COPY(wh->i_addr2,
680				    vap->iv_myaddr);
681			} else {
682				wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
683				IEEE80211_ADDR_COPY(wh->i_addr1, da);
684				IEEE80211_ADDR_COPY(wh->i_addr2,
685				    vap->iv_myaddr);
686				IEEE80211_ADDR_COPY(wh->i_addr3, da);
687				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
688			}
689#endif
690			break;
691		case IEEE80211_M_MONITOR:	/* NB: to quiet compiler */
692			break;
693		}
694	} else {
695		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
696		IEEE80211_ADDR_COPY(wh->i_addr1, da);
697		IEEE80211_ADDR_COPY(wh->i_addr2, sa);
698#ifdef IEEE80211_SUPPORT_MESH
699		if (vap->iv_opmode == IEEE80211_M_MBSS)
700			IEEE80211_ADDR_COPY(wh->i_addr3, sa);
701		else
702#endif
703			IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
704	}
705	*(uint16_t *)&wh->i_dur[0] = 0;
706
707	tap = &ni->ni_tx_ampdu[tid];
708	if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
709		m->m_flags |= M_AMPDU_MPDU;
710	else {
711		seqno = ni->ni_txseqs[tid]++;
712		*(uint16_t *)&wh->i_seq[0] =
713		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
714		M_SEQNO_SET(m, seqno);
715	}
716
717	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
718		m->m_flags |= M_MCAST;
719#undef WH4
720}
721
722/*
723 * Send a management frame to the specified node.  The node pointer
724 * must have a reference as the pointer will be passed to the driver
725 * and potentially held for a long time.  If the frame is successfully
726 * dispatched to the driver, then it is responsible for freeing the
727 * reference (and potentially free'ing up any associated storage);
728 * otherwise deal with reclaiming any reference (on error).
729 */
730int
731ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
732	struct ieee80211_bpf_params *params)
733{
734	struct ieee80211vap *vap = ni->ni_vap;
735	struct ieee80211com *ic = ni->ni_ic;
736	struct ieee80211_frame *wh;
737	int ret;
738
739	KASSERT(ni != NULL, ("null node"));
740
741	if (vap->iv_state == IEEE80211_S_CAC) {
742		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
743		    ni, "block %s frame in CAC state",
744			ieee80211_mgt_subtype_name[
745			    (type & IEEE80211_FC0_SUBTYPE_MASK) >>
746				IEEE80211_FC0_SUBTYPE_SHIFT]);
747		vap->iv_stats.is_tx_badstate++;
748		ieee80211_free_node(ni);
749		m_freem(m);
750		return EIO;		/* XXX */
751	}
752
753	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
754	if (m == NULL) {
755		ieee80211_free_node(ni);
756		return ENOMEM;
757	}
758
759	IEEE80211_TX_LOCK(ic);
760
761	wh = mtod(m, struct ieee80211_frame *);
762	ieee80211_send_setup(ni, m,
763	     IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
764	     vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
765	if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
766		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
767		    "encrypting frame (%s)", __func__);
768		wh->i_fc[1] |= IEEE80211_FC1_WEP;
769	}
770	m->m_flags |= M_ENCAP;		/* mark encapsulated */
771
772	KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
773	M_WME_SETAC(m, params->ibp_pri);
774
775#ifdef IEEE80211_DEBUG
776	/* avoid printing too many frames */
777	if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
778	    ieee80211_msg_dumppkts(vap)) {
779		printf("[%s] send %s on channel %u\n",
780		    ether_sprintf(wh->i_addr1),
781		    ieee80211_mgt_subtype_name[
782			(type & IEEE80211_FC0_SUBTYPE_MASK) >>
783				IEEE80211_FC0_SUBTYPE_SHIFT],
784		    ieee80211_chan2ieee(ic, ic->ic_curchan));
785	}
786#endif
787	IEEE80211_NODE_STAT(ni, tx_mgmt);
788
789	ret = ieee80211_raw_output(vap, ni, m, params);
790	IEEE80211_TX_UNLOCK(ic);
791	return (ret);
792}
793
794/*
795 * Send a null data frame to the specified node.  If the station
796 * is setup for QoS then a QoS Null Data frame is constructed.
797 * If this is a WDS station then a 4-address frame is constructed.
798 *
799 * NB: the caller is assumed to have setup a node reference
800 *     for use; this is necessary to deal with a race condition
801 *     when probing for inactive stations.  Like ieee80211_mgmt_output
802 *     we must cleanup any node reference on error;  however we
803 *     can safely just unref it as we know it will never be the
804 *     last reference to the node.
805 */
806int
807ieee80211_send_nulldata(struct ieee80211_node *ni)
808{
809	struct ieee80211vap *vap = ni->ni_vap;
810	struct ieee80211com *ic = ni->ni_ic;
811	struct mbuf *m;
812	struct ieee80211_frame *wh;
813	int hdrlen;
814	uint8_t *frm;
815	int ret;
816
817	if (vap->iv_state == IEEE80211_S_CAC) {
818		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
819		    ni, "block %s frame in CAC state", "null data");
820		ieee80211_unref_node(&ni);
821		vap->iv_stats.is_tx_badstate++;
822		return EIO;		/* XXX */
823	}
824
825	if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
826		hdrlen = sizeof(struct ieee80211_qosframe);
827	else
828		hdrlen = sizeof(struct ieee80211_frame);
829	/* NB: only WDS vap's get 4-address frames */
830	if (vap->iv_opmode == IEEE80211_M_WDS)
831		hdrlen += IEEE80211_ADDR_LEN;
832	if (ic->ic_flags & IEEE80211_F_DATAPAD)
833		hdrlen = roundup(hdrlen, sizeof(uint32_t));
834
835	m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
836	if (m == NULL) {
837		/* XXX debug msg */
838		ieee80211_unref_node(&ni);
839		vap->iv_stats.is_tx_nobuf++;
840		return ENOMEM;
841	}
842	KASSERT(M_LEADINGSPACE(m) >= hdrlen,
843	    ("leading space %zd", M_LEADINGSPACE(m)));
844	M_PREPEND(m, hdrlen, M_NOWAIT);
845	if (m == NULL) {
846		/* NB: cannot happen */
847		ieee80211_free_node(ni);
848		return ENOMEM;
849	}
850
851	IEEE80211_TX_LOCK(ic);
852
853	wh = mtod(m, struct ieee80211_frame *);		/* NB: a little lie */
854	if (ni->ni_flags & IEEE80211_NODE_QOS) {
855		const int tid = WME_AC_TO_TID(WME_AC_BE);
856		uint8_t *qos;
857
858		ieee80211_send_setup(ni, m,
859		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
860		    tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
861
862		if (vap->iv_opmode == IEEE80211_M_WDS)
863			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
864		else
865			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
866		qos[0] = tid & IEEE80211_QOS_TID;
867		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
868			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
869		qos[1] = 0;
870	} else {
871		ieee80211_send_setup(ni, m,
872		    IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
873		    IEEE80211_NONQOS_TID,
874		    vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
875	}
876	if (vap->iv_opmode != IEEE80211_M_WDS) {
877		/* NB: power management bit is never sent by an AP */
878		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
879		    vap->iv_opmode != IEEE80211_M_HOSTAP)
880			wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
881	}
882	m->m_len = m->m_pkthdr.len = hdrlen;
883	m->m_flags |= M_ENCAP;		/* mark encapsulated */
884
885	M_WME_SETAC(m, WME_AC_BE);
886
887	IEEE80211_NODE_STAT(ni, tx_data);
888
889	IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
890	    "send %snull data frame on channel %u, pwr mgt %s",
891	    ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
892	    ieee80211_chan2ieee(ic, ic->ic_curchan),
893	    wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
894
895	ret = ieee80211_raw_output(vap, ni, m, NULL);
896	IEEE80211_TX_UNLOCK(ic);
897	return (ret);
898}
899
900/*
901 * Assign priority to a frame based on any vlan tag assigned
902 * to the station and/or any Diffserv setting in an IP header.
903 * Finally, if an ACM policy is setup (in station mode) it's
904 * applied.
905 */
906int
907ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
908{
909	const struct ether_header *eh = mtod(m, struct ether_header *);
910	int v_wme_ac, d_wme_ac, ac;
911
912	/*
913	 * Always promote PAE/EAPOL frames to high priority.
914	 */
915	if (eh->ether_type == htons(ETHERTYPE_PAE)) {
916		/* NB: mark so others don't need to check header */
917		m->m_flags |= M_EAPOL;
918		ac = WME_AC_VO;
919		goto done;
920	}
921	/*
922	 * Non-qos traffic goes to BE.
923	 */
924	if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
925		ac = WME_AC_BE;
926		goto done;
927	}
928
929	/*
930	 * If node has a vlan tag then all traffic
931	 * to it must have a matching tag.
932	 */
933	v_wme_ac = 0;
934	if (ni->ni_vlan != 0) {
935		 if ((m->m_flags & M_VLANTAG) == 0) {
936			IEEE80211_NODE_STAT(ni, tx_novlantag);
937			return 1;
938		}
939		if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
940		    EVL_VLANOFTAG(ni->ni_vlan)) {
941			IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
942			return 1;
943		}
944		/* map vlan priority to AC */
945		v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
946	}
947
948	/* XXX m_copydata may be too slow for fast path */
949#ifdef INET
950	if (eh->ether_type == htons(ETHERTYPE_IP)) {
951		uint8_t tos;
952		/*
953		 * IP frame, map the DSCP bits from the TOS field.
954		 */
955		/* NB: ip header may not be in first mbuf */
956		m_copydata(m, sizeof(struct ether_header) +
957		    offsetof(struct ip, ip_tos), sizeof(tos), &tos);
958		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
959		d_wme_ac = TID_TO_WME_AC(tos);
960	} else {
961#endif /* INET */
962#ifdef INET6
963	if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
964		uint32_t flow;
965		uint8_t tos;
966		/*
967		 * IPv6 frame, map the DSCP bits from the traffic class field.
968		 */
969		m_copydata(m, sizeof(struct ether_header) +
970		    offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
971		    (caddr_t) &flow);
972		tos = (uint8_t)(ntohl(flow) >> 20);
973		tos >>= 5;		/* NB: ECN + low 3 bits of DSCP */
974		d_wme_ac = TID_TO_WME_AC(tos);
975	} else {
976#endif /* INET6 */
977		d_wme_ac = WME_AC_BE;
978#ifdef INET6
979	}
980#endif
981#ifdef INET
982	}
983#endif
984	/*
985	 * Use highest priority AC.
986	 */
987	if (v_wme_ac > d_wme_ac)
988		ac = v_wme_ac;
989	else
990		ac = d_wme_ac;
991
992	/*
993	 * Apply ACM policy.
994	 */
995	if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
996		static const int acmap[4] = {
997			WME_AC_BK,	/* WME_AC_BE */
998			WME_AC_BK,	/* WME_AC_BK */
999			WME_AC_BE,	/* WME_AC_VI */
1000			WME_AC_VI,	/* WME_AC_VO */
1001		};
1002		struct ieee80211com *ic = ni->ni_ic;
1003
1004		while (ac != WME_AC_BK &&
1005		    ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1006			ac = acmap[ac];
1007	}
1008done:
1009	M_WME_SETAC(m, ac);
1010	return 0;
1011}
1012
1013/*
1014 * Insure there is sufficient contiguous space to encapsulate the
1015 * 802.11 data frame.  If room isn't already there, arrange for it.
1016 * Drivers and cipher modules assume we have done the necessary work
1017 * and fail rudely if they don't find the space they need.
1018 */
1019struct mbuf *
1020ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1021	struct ieee80211_key *key, struct mbuf *m)
1022{
1023#define	TO_BE_RECLAIMED	(sizeof(struct ether_header) - sizeof(struct llc))
1024	int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1025
1026	if (key != NULL) {
1027		/* XXX belongs in crypto code? */
1028		needed_space += key->wk_cipher->ic_header;
1029		/* XXX frags */
1030		/*
1031		 * When crypto is being done in the host we must insure
1032		 * the data are writable for the cipher routines; clone
1033		 * a writable mbuf chain.
1034		 * XXX handle SWMIC specially
1035		 */
1036		if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1037			m = m_unshare(m, M_NOWAIT);
1038			if (m == NULL) {
1039				IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1040				    "%s: cannot get writable mbuf\n", __func__);
1041				vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1042				return NULL;
1043			}
1044		}
1045	}
1046	/*
1047	 * We know we are called just before stripping an Ethernet
1048	 * header and prepending an LLC header.  This means we know
1049	 * there will be
1050	 *	sizeof(struct ether_header) - sizeof(struct llc)
1051	 * bytes recovered to which we need additional space for the
1052	 * 802.11 header and any crypto header.
1053	 */
1054	/* XXX check trailing space and copy instead? */
1055	if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1056		struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1057		if (n == NULL) {
1058			IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1059			    "%s: cannot expand storage\n", __func__);
1060			vap->iv_stats.is_tx_nobuf++;
1061			m_freem(m);
1062			return NULL;
1063		}
1064		KASSERT(needed_space <= MHLEN,
1065		    ("not enough room, need %u got %d\n", needed_space, MHLEN));
1066		/*
1067		 * Setup new mbuf to have leading space to prepend the
1068		 * 802.11 header and any crypto header bits that are
1069		 * required (the latter are added when the driver calls
1070		 * back to ieee80211_crypto_encap to do crypto encapsulation).
1071		 */
1072		/* NB: must be first 'cuz it clobbers m_data */
1073		m_move_pkthdr(n, m);
1074		n->m_len = 0;			/* NB: m_gethdr does not set */
1075		n->m_data += needed_space;
1076		/*
1077		 * Pull up Ethernet header to create the expected layout.
1078		 * We could use m_pullup but that's overkill (i.e. we don't
1079		 * need the actual data) and it cannot fail so do it inline
1080		 * for speed.
1081		 */
1082		/* NB: struct ether_header is known to be contiguous */
1083		n->m_len += sizeof(struct ether_header);
1084		m->m_len -= sizeof(struct ether_header);
1085		m->m_data += sizeof(struct ether_header);
1086		/*
1087		 * Replace the head of the chain.
1088		 */
1089		n->m_next = m;
1090		m = n;
1091	}
1092	return m;
1093#undef TO_BE_RECLAIMED
1094}
1095
1096/*
1097 * Return the transmit key to use in sending a unicast frame.
1098 * If a unicast key is set we use that.  When no unicast key is set
1099 * we fall back to the default transmit key.
1100 */
1101static __inline struct ieee80211_key *
1102ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1103	struct ieee80211_node *ni)
1104{
1105	if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1106		if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1107		    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1108			return NULL;
1109		return &vap->iv_nw_keys[vap->iv_def_txkey];
1110	} else {
1111		return &ni->ni_ucastkey;
1112	}
1113}
1114
1115/*
1116 * Return the transmit key to use in sending a multicast frame.
1117 * Multicast traffic always uses the group key which is installed as
1118 * the default tx key.
1119 */
1120static __inline struct ieee80211_key *
1121ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1122	struct ieee80211_node *ni)
1123{
1124	if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1125	    IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1126		return NULL;
1127	return &vap->iv_nw_keys[vap->iv_def_txkey];
1128}
1129
1130/*
1131 * Encapsulate an outbound data frame.  The mbuf chain is updated.
1132 * If an error is encountered NULL is returned.  The caller is required
1133 * to provide a node reference and pullup the ethernet header in the
1134 * first mbuf.
1135 *
1136 * NB: Packet is assumed to be processed by ieee80211_classify which
1137 *     marked EAPOL frames w/ M_EAPOL.
1138 */
1139struct mbuf *
1140ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1141    struct mbuf *m)
1142{
1143#define	WH4(wh)	((struct ieee80211_frame_addr4 *)(wh))
1144#define MC01(mc)	((struct ieee80211_meshcntl_ae01 *)mc)
1145	struct ieee80211com *ic = ni->ni_ic;
1146#ifdef IEEE80211_SUPPORT_MESH
1147	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1148	struct ieee80211_meshcntl_ae10 *mc;
1149	struct ieee80211_mesh_route *rt = NULL;
1150	int dir = -1;
1151#endif
1152	struct ether_header eh;
1153	struct ieee80211_frame *wh;
1154	struct ieee80211_key *key;
1155	struct llc *llc;
1156	int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1157	ieee80211_seq seqno;
1158	int meshhdrsize, meshae;
1159	uint8_t *qos;
1160
1161	IEEE80211_TX_LOCK_ASSERT(ic);
1162
1163	/*
1164	 * Copy existing Ethernet header to a safe place.  The
1165	 * rest of the code assumes it's ok to strip it when
1166	 * reorganizing state for the final encapsulation.
1167	 */
1168	KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1169	ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1170
1171	/*
1172	 * Insure space for additional headers.  First identify
1173	 * transmit key to use in calculating any buffer adjustments
1174	 * required.  This is also used below to do privacy
1175	 * encapsulation work.  Then calculate the 802.11 header
1176	 * size and any padding required by the driver.
1177	 *
1178	 * Note key may be NULL if we fall back to the default
1179	 * transmit key and that is not set.  In that case the
1180	 * buffer may not be expanded as needed by the cipher
1181	 * routines, but they will/should discard it.
1182	 */
1183	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1184		if (vap->iv_opmode == IEEE80211_M_STA ||
1185		    !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1186		    (vap->iv_opmode == IEEE80211_M_WDS &&
1187		     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1188			key = ieee80211_crypto_getucastkey(vap, ni);
1189		else
1190			key = ieee80211_crypto_getmcastkey(vap, ni);
1191		if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1192			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1193			    eh.ether_dhost,
1194			    "no default transmit key (%s) deftxkey %u",
1195			    __func__, vap->iv_def_txkey);
1196			vap->iv_stats.is_tx_nodefkey++;
1197			goto bad;
1198		}
1199	} else
1200		key = NULL;
1201	/*
1202	 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1203	 * frames so suppress use.  This may be an issue if other
1204	 * ap's require all data frames to be QoS-encapsulated
1205	 * once negotiated in which case we'll need to make this
1206	 * configurable.
1207	 * NB: mesh data frames are QoS.
1208	 */
1209	addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1210	    (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1211	    (m->m_flags & M_EAPOL) == 0;
1212	if (addqos)
1213		hdrsize = sizeof(struct ieee80211_qosframe);
1214	else
1215		hdrsize = sizeof(struct ieee80211_frame);
1216#ifdef IEEE80211_SUPPORT_MESH
1217	if (vap->iv_opmode == IEEE80211_M_MBSS) {
1218		/*
1219		 * Mesh data frames are encapsulated according to the
1220		 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1221		 * o Group Addressed data (aka multicast) originating
1222		 *   at the local sta are sent w/ 3-address format and
1223		 *   address extension mode 00
1224		 * o Individually Addressed data (aka unicast) originating
1225		 *   at the local sta are sent w/ 4-address format and
1226		 *   address extension mode 00
1227		 * o Group Addressed data forwarded from a non-mesh sta are
1228		 *   sent w/ 3-address format and address extension mode 01
1229		 * o Individually Address data from another sta are sent
1230		 *   w/ 4-address format and address extension mode 10
1231		 */
1232		is4addr = 0;		/* NB: don't use, disable */
1233		if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1234			rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1235			KASSERT(rt != NULL, ("route is NULL"));
1236			dir = IEEE80211_FC1_DIR_DSTODS;
1237			hdrsize += IEEE80211_ADDR_LEN;
1238			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1239				if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1240				    vap->iv_myaddr)) {
1241					IEEE80211_NOTE_MAC(vap,
1242					    IEEE80211_MSG_MESH,
1243					    eh.ether_dhost,
1244					    "%s", "trying to send to ourself");
1245					goto bad;
1246				}
1247				meshae = IEEE80211_MESH_AE_10;
1248				meshhdrsize =
1249				    sizeof(struct ieee80211_meshcntl_ae10);
1250			} else {
1251				meshae = IEEE80211_MESH_AE_00;
1252				meshhdrsize =
1253				    sizeof(struct ieee80211_meshcntl);
1254			}
1255		} else {
1256			dir = IEEE80211_FC1_DIR_FROMDS;
1257			if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1258				/* proxy group */
1259				meshae = IEEE80211_MESH_AE_01;
1260				meshhdrsize =
1261				    sizeof(struct ieee80211_meshcntl_ae01);
1262			} else {
1263				/* group */
1264				meshae = IEEE80211_MESH_AE_00;
1265				meshhdrsize = sizeof(struct ieee80211_meshcntl);
1266			}
1267		}
1268	} else {
1269#endif
1270		/*
1271		 * 4-address frames need to be generated for:
1272		 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1273		 * o packets sent through a vap marked for relaying
1274		 *   (e.g. a station operating with dynamic WDS)
1275		 */
1276		is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1277		    ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1278		     !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1279		if (is4addr)
1280			hdrsize += IEEE80211_ADDR_LEN;
1281		meshhdrsize = meshae = 0;
1282#ifdef IEEE80211_SUPPORT_MESH
1283	}
1284#endif
1285	/*
1286	 * Honor driver DATAPAD requirement.
1287	 */
1288	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1289		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1290	else
1291		hdrspace = hdrsize;
1292
1293	if (__predict_true((m->m_flags & M_FF) == 0)) {
1294		/*
1295		 * Normal frame.
1296		 */
1297		m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1298		if (m == NULL) {
1299			/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1300			goto bad;
1301		}
1302		/* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1303		m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1304		llc = mtod(m, struct llc *);
1305		llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1306		llc->llc_control = LLC_UI;
1307		llc->llc_snap.org_code[0] = 0;
1308		llc->llc_snap.org_code[1] = 0;
1309		llc->llc_snap.org_code[2] = 0;
1310		llc->llc_snap.ether_type = eh.ether_type;
1311	} else {
1312#ifdef IEEE80211_SUPPORT_SUPERG
1313		/*
1314		 * Aggregated frame.
1315		 */
1316		m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1317		if (m == NULL)
1318#endif
1319			goto bad;
1320	}
1321	datalen = m->m_pkthdr.len;		/* NB: w/o 802.11 header */
1322
1323	M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1324	if (m == NULL) {
1325		vap->iv_stats.is_tx_nobuf++;
1326		goto bad;
1327	}
1328	wh = mtod(m, struct ieee80211_frame *);
1329	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1330	*(uint16_t *)wh->i_dur = 0;
1331	qos = NULL;	/* NB: quiet compiler */
1332	if (is4addr) {
1333		wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1334		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1335		IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1336		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1337		IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1338	} else switch (vap->iv_opmode) {
1339	case IEEE80211_M_STA:
1340		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1341		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1342		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1343		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1344		break;
1345	case IEEE80211_M_IBSS:
1346	case IEEE80211_M_AHDEMO:
1347		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1348		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1349		IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1350		/*
1351		 * NB: always use the bssid from iv_bss as the
1352		 *     neighbor's may be stale after an ibss merge
1353		 */
1354		IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1355		break;
1356	case IEEE80211_M_HOSTAP:
1357		wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1358		IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1359		IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1360		IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1361		break;
1362#ifdef IEEE80211_SUPPORT_MESH
1363	case IEEE80211_M_MBSS:
1364		/* NB: offset by hdrspace to deal with DATAPAD */
1365		mc = (struct ieee80211_meshcntl_ae10 *)
1366		     (mtod(m, uint8_t *) + hdrspace);
1367		wh->i_fc[1] = dir;
1368		switch (meshae) {
1369		case IEEE80211_MESH_AE_00:	/* no proxy */
1370			mc->mc_flags = 0;
1371			if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1372				IEEE80211_ADDR_COPY(wh->i_addr1,
1373				    ni->ni_macaddr);
1374				IEEE80211_ADDR_COPY(wh->i_addr2,
1375				    vap->iv_myaddr);
1376				IEEE80211_ADDR_COPY(wh->i_addr3,
1377				    eh.ether_dhost);
1378				IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1379				    eh.ether_shost);
1380				qos =((struct ieee80211_qosframe_addr4 *)
1381				    wh)->i_qos;
1382			} else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1383				 /* mcast */
1384				IEEE80211_ADDR_COPY(wh->i_addr1,
1385				    eh.ether_dhost);
1386				IEEE80211_ADDR_COPY(wh->i_addr2,
1387				    vap->iv_myaddr);
1388				IEEE80211_ADDR_COPY(wh->i_addr3,
1389				    eh.ether_shost);
1390				qos = ((struct ieee80211_qosframe *)
1391				    wh)->i_qos;
1392			}
1393			break;
1394		case IEEE80211_MESH_AE_01:	/* mcast, proxy */
1395			wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1396			IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1397			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1398			IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1399			mc->mc_flags = 1;
1400			IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1401			    eh.ether_shost);
1402			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1403			break;
1404		case IEEE80211_MESH_AE_10:	/* ucast, proxy */
1405			KASSERT(rt != NULL, ("route is NULL"));
1406			IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1407			IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1408			IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1409			IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1410			mc->mc_flags = IEEE80211_MESH_AE_10;
1411			IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1412			IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1413			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1414			break;
1415		default:
1416			KASSERT(0, ("meshae %d", meshae));
1417			break;
1418		}
1419		mc->mc_ttl = ms->ms_ttl;
1420		ms->ms_seq++;
1421		LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1422		break;
1423#endif
1424	case IEEE80211_M_WDS:		/* NB: is4addr should always be true */
1425	default:
1426		goto bad;
1427	}
1428	if (m->m_flags & M_MORE_DATA)
1429		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1430	if (addqos) {
1431		int ac, tid;
1432
1433		if (is4addr) {
1434			qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1435		/* NB: mesh case handled earlier */
1436		} else if (vap->iv_opmode != IEEE80211_M_MBSS)
1437			qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1438		ac = M_WME_GETAC(m);
1439		/* map from access class/queue to 11e header priorty value */
1440		tid = WME_AC_TO_TID(ac);
1441		qos[0] = tid & IEEE80211_QOS_TID;
1442		if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1443			qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1444#ifdef IEEE80211_SUPPORT_MESH
1445		if (vap->iv_opmode == IEEE80211_M_MBSS)
1446			qos[1] = IEEE80211_QOS_MC;
1447		else
1448#endif
1449			qos[1] = 0;
1450		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1451
1452		if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1453			/*
1454			 * NB: don't assign a sequence # to potential
1455			 * aggregates; we expect this happens at the
1456			 * point the frame comes off any aggregation q
1457			 * as otherwise we may introduce holes in the
1458			 * BA sequence space and/or make window accouting
1459			 * more difficult.
1460			 *
1461			 * XXX may want to control this with a driver
1462			 * capability; this may also change when we pull
1463			 * aggregation up into net80211
1464			 */
1465			seqno = ni->ni_txseqs[tid]++;
1466			*(uint16_t *)wh->i_seq =
1467			    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1468			M_SEQNO_SET(m, seqno);
1469		}
1470	} else {
1471		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1472		*(uint16_t *)wh->i_seq =
1473		    htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1474		M_SEQNO_SET(m, seqno);
1475	}
1476
1477
1478	/* check if xmit fragmentation is required */
1479	txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1480	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1481	    (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1482	    (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1483	if (key != NULL) {
1484		/*
1485		 * IEEE 802.1X: send EAPOL frames always in the clear.
1486		 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1487		 */
1488		if ((m->m_flags & M_EAPOL) == 0 ||
1489		    ((vap->iv_flags & IEEE80211_F_WPA) &&
1490		     (vap->iv_opmode == IEEE80211_M_STA ?
1491		      !IEEE80211_KEY_UNDEFINED(key) :
1492		      !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1493			wh->i_fc[1] |= IEEE80211_FC1_WEP;
1494			if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1495				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1496				    eh.ether_dhost,
1497				    "%s", "enmic failed, discard frame");
1498				vap->iv_stats.is_crypto_enmicfail++;
1499				goto bad;
1500			}
1501		}
1502	}
1503	if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1504	    key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1505		goto bad;
1506
1507	m->m_flags |= M_ENCAP;		/* mark encapsulated */
1508
1509	IEEE80211_NODE_STAT(ni, tx_data);
1510	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1511		IEEE80211_NODE_STAT(ni, tx_mcast);
1512		m->m_flags |= M_MCAST;
1513	} else
1514		IEEE80211_NODE_STAT(ni, tx_ucast);
1515	IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1516
1517	return m;
1518bad:
1519	if (m != NULL)
1520		m_freem(m);
1521	return NULL;
1522#undef WH4
1523#undef MC01
1524}
1525
1526/*
1527 * Fragment the frame according to the specified mtu.
1528 * The size of the 802.11 header (w/o padding) is provided
1529 * so we don't need to recalculate it.  We create a new
1530 * mbuf for each fragment and chain it through m_nextpkt;
1531 * we might be able to optimize this by reusing the original
1532 * packet's mbufs but that is significantly more complicated.
1533 */
1534static int
1535ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1536	u_int hdrsize, u_int ciphdrsize, u_int mtu)
1537{
1538	struct ieee80211com *ic = vap->iv_ic;
1539	struct ieee80211_frame *wh, *whf;
1540	struct mbuf *m, *prev, *next;
1541	u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1542	u_int hdrspace;
1543
1544	KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1545	KASSERT(m0->m_pkthdr.len > mtu,
1546		("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1547
1548	/*
1549	 * Honor driver DATAPAD requirement.
1550	 */
1551	if (ic->ic_flags & IEEE80211_F_DATAPAD)
1552		hdrspace = roundup(hdrsize, sizeof(uint32_t));
1553	else
1554		hdrspace = hdrsize;
1555
1556	wh = mtod(m0, struct ieee80211_frame *);
1557	/* NB: mark the first frag; it will be propagated below */
1558	wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1559	totalhdrsize = hdrspace + ciphdrsize;
1560	fragno = 1;
1561	off = mtu - ciphdrsize;
1562	remainder = m0->m_pkthdr.len - off;
1563	prev = m0;
1564	do {
1565		fragsize = totalhdrsize + remainder;
1566		if (fragsize > mtu)
1567			fragsize = mtu;
1568		/* XXX fragsize can be >2048! */
1569		KASSERT(fragsize < MCLBYTES,
1570			("fragment size %u too big!", fragsize));
1571		if (fragsize > MHLEN)
1572			m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1573		else
1574			m = m_gethdr(M_NOWAIT, MT_DATA);
1575		if (m == NULL)
1576			goto bad;
1577		/* leave room to prepend any cipher header */
1578		m_align(m, fragsize - ciphdrsize);
1579
1580		/*
1581		 * Form the header in the fragment.  Note that since
1582		 * we mark the first fragment with the MORE_FRAG bit
1583		 * it automatically is propagated to each fragment; we
1584		 * need only clear it on the last fragment (done below).
1585		 * NB: frag 1+ dont have Mesh Control field present.
1586		 */
1587		whf = mtod(m, struct ieee80211_frame *);
1588		memcpy(whf, wh, hdrsize);
1589#ifdef IEEE80211_SUPPORT_MESH
1590		if (vap->iv_opmode == IEEE80211_M_MBSS) {
1591			if (IEEE80211_IS_DSTODS(wh))
1592				((struct ieee80211_qosframe_addr4 *)
1593				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1594			else
1595				((struct ieee80211_qosframe *)
1596				    whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1597		}
1598#endif
1599		*(uint16_t *)&whf->i_seq[0] |= htole16(
1600			(fragno & IEEE80211_SEQ_FRAG_MASK) <<
1601				IEEE80211_SEQ_FRAG_SHIFT);
1602		fragno++;
1603
1604		payload = fragsize - totalhdrsize;
1605		/* NB: destination is known to be contiguous */
1606
1607		m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1608		m->m_len = hdrspace + payload;
1609		m->m_pkthdr.len = hdrspace + payload;
1610		m->m_flags |= M_FRAG;
1611
1612		/* chain up the fragment */
1613		prev->m_nextpkt = m;
1614		prev = m;
1615
1616		/* deduct fragment just formed */
1617		remainder -= payload;
1618		off += payload;
1619	} while (remainder != 0);
1620
1621	/* set the last fragment */
1622	m->m_flags |= M_LASTFRAG;
1623	whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1624
1625	/* strip first mbuf now that everything has been copied */
1626	m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1627	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1628
1629	vap->iv_stats.is_tx_fragframes++;
1630	vap->iv_stats.is_tx_frags += fragno-1;
1631
1632	return 1;
1633bad:
1634	/* reclaim fragments but leave original frame for caller to free */
1635	for (m = m0->m_nextpkt; m != NULL; m = next) {
1636		next = m->m_nextpkt;
1637		m->m_nextpkt = NULL;		/* XXX paranoid */
1638		m_freem(m);
1639	}
1640	m0->m_nextpkt = NULL;
1641	return 0;
1642}
1643
1644/*
1645 * Add a supported rates element id to a frame.
1646 */
1647uint8_t *
1648ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1649{
1650	int nrates;
1651
1652	*frm++ = IEEE80211_ELEMID_RATES;
1653	nrates = rs->rs_nrates;
1654	if (nrates > IEEE80211_RATE_SIZE)
1655		nrates = IEEE80211_RATE_SIZE;
1656	*frm++ = nrates;
1657	memcpy(frm, rs->rs_rates, nrates);
1658	return frm + nrates;
1659}
1660
1661/*
1662 * Add an extended supported rates element id to a frame.
1663 */
1664uint8_t *
1665ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1666{
1667	/*
1668	 * Add an extended supported rates element if operating in 11g mode.
1669	 */
1670	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1671		int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1672		*frm++ = IEEE80211_ELEMID_XRATES;
1673		*frm++ = nrates;
1674		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1675		frm += nrates;
1676	}
1677	return frm;
1678}
1679
1680/*
1681 * Add an ssid element to a frame.
1682 */
1683static uint8_t *
1684ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1685{
1686	*frm++ = IEEE80211_ELEMID_SSID;
1687	*frm++ = len;
1688	memcpy(frm, ssid, len);
1689	return frm + len;
1690}
1691
1692/*
1693 * Add an erp element to a frame.
1694 */
1695static uint8_t *
1696ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1697{
1698	uint8_t erp;
1699
1700	*frm++ = IEEE80211_ELEMID_ERP;
1701	*frm++ = 1;
1702	erp = 0;
1703	if (ic->ic_nonerpsta != 0)
1704		erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1705	if (ic->ic_flags & IEEE80211_F_USEPROT)
1706		erp |= IEEE80211_ERP_USE_PROTECTION;
1707	if (ic->ic_flags & IEEE80211_F_USEBARKER)
1708		erp |= IEEE80211_ERP_LONG_PREAMBLE;
1709	*frm++ = erp;
1710	return frm;
1711}
1712
1713/*
1714 * Add a CFParams element to a frame.
1715 */
1716static uint8_t *
1717ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1718{
1719#define	ADDSHORT(frm, v) do {	\
1720	LE_WRITE_2(frm, v);	\
1721	frm += 2;		\
1722} while (0)
1723	*frm++ = IEEE80211_ELEMID_CFPARMS;
1724	*frm++ = 6;
1725	*frm++ = 0;		/* CFP count */
1726	*frm++ = 2;		/* CFP period */
1727	ADDSHORT(frm, 0);	/* CFP MaxDuration (TU) */
1728	ADDSHORT(frm, 0);	/* CFP CurRemaining (TU) */
1729	return frm;
1730#undef ADDSHORT
1731}
1732
1733static __inline uint8_t *
1734add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1735{
1736	memcpy(frm, ie->ie_data, ie->ie_len);
1737	return frm + ie->ie_len;
1738}
1739
1740static __inline uint8_t *
1741add_ie(uint8_t *frm, const uint8_t *ie)
1742{
1743	memcpy(frm, ie, 2 + ie[1]);
1744	return frm + 2 + ie[1];
1745}
1746
1747#define	WME_OUI_BYTES		0x00, 0x50, 0xf2
1748/*
1749 * Add a WME information element to a frame.
1750 */
1751static uint8_t *
1752ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1753{
1754	static const struct ieee80211_wme_info info = {
1755		.wme_id		= IEEE80211_ELEMID_VENDOR,
1756		.wme_len	= sizeof(struct ieee80211_wme_info) - 2,
1757		.wme_oui	= { WME_OUI_BYTES },
1758		.wme_type	= WME_OUI_TYPE,
1759		.wme_subtype	= WME_INFO_OUI_SUBTYPE,
1760		.wme_version	= WME_VERSION,
1761		.wme_info	= 0,
1762	};
1763	memcpy(frm, &info, sizeof(info));
1764	return frm + sizeof(info);
1765}
1766
1767/*
1768 * Add a WME parameters element to a frame.
1769 */
1770static uint8_t *
1771ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1772{
1773#define	SM(_v, _f)	(((_v) << _f##_S) & _f)
1774#define	ADDSHORT(frm, v) do {	\
1775	LE_WRITE_2(frm, v);	\
1776	frm += 2;		\
1777} while (0)
1778	/* NB: this works 'cuz a param has an info at the front */
1779	static const struct ieee80211_wme_info param = {
1780		.wme_id		= IEEE80211_ELEMID_VENDOR,
1781		.wme_len	= sizeof(struct ieee80211_wme_param) - 2,
1782		.wme_oui	= { WME_OUI_BYTES },
1783		.wme_type	= WME_OUI_TYPE,
1784		.wme_subtype	= WME_PARAM_OUI_SUBTYPE,
1785		.wme_version	= WME_VERSION,
1786	};
1787	int i;
1788
1789	memcpy(frm, &param, sizeof(param));
1790	frm += __offsetof(struct ieee80211_wme_info, wme_info);
1791	*frm++ = wme->wme_bssChanParams.cap_info;	/* AC info */
1792	*frm++ = 0;					/* reserved field */
1793	for (i = 0; i < WME_NUM_AC; i++) {
1794		const struct wmeParams *ac =
1795		       &wme->wme_bssChanParams.cap_wmeParams[i];
1796		*frm++ = SM(i, WME_PARAM_ACI)
1797		       | SM(ac->wmep_acm, WME_PARAM_ACM)
1798		       | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1799		       ;
1800		*frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1801		       | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1802		       ;
1803		ADDSHORT(frm, ac->wmep_txopLimit);
1804	}
1805	return frm;
1806#undef SM
1807#undef ADDSHORT
1808}
1809#undef WME_OUI_BYTES
1810
1811/*
1812 * Add an 11h Power Constraint element to a frame.
1813 */
1814static uint8_t *
1815ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1816{
1817	const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1818	/* XXX per-vap tx power limit? */
1819	int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1820
1821	frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1822	frm[1] = 1;
1823	frm[2] = c->ic_maxregpower > limit ?  c->ic_maxregpower - limit : 0;
1824	return frm + 3;
1825}
1826
1827/*
1828 * Add an 11h Power Capability element to a frame.
1829 */
1830static uint8_t *
1831ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1832{
1833	frm[0] = IEEE80211_ELEMID_PWRCAP;
1834	frm[1] = 2;
1835	frm[2] = c->ic_minpower;
1836	frm[3] = c->ic_maxpower;
1837	return frm + 4;
1838}
1839
1840/*
1841 * Add an 11h Supported Channels element to a frame.
1842 */
1843static uint8_t *
1844ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1845{
1846	static const int ielen = 26;
1847
1848	frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1849	frm[1] = ielen;
1850	/* XXX not correct */
1851	memcpy(frm+2, ic->ic_chan_avail, ielen);
1852	return frm + 2 + ielen;
1853}
1854
1855/*
1856 * Add an 11h Quiet time element to a frame.
1857 */
1858static uint8_t *
1859ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1860{
1861	struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1862
1863	quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1864	quiet->len = 6;
1865	if (vap->iv_quiet_count_value == 1)
1866		vap->iv_quiet_count_value = vap->iv_quiet_count;
1867	else if (vap->iv_quiet_count_value > 1)
1868		vap->iv_quiet_count_value--;
1869
1870	if (vap->iv_quiet_count_value == 0) {
1871		/* value 0 is reserved as per 802.11h standerd */
1872		vap->iv_quiet_count_value = 1;
1873	}
1874
1875	quiet->tbttcount = vap->iv_quiet_count_value;
1876	quiet->period = vap->iv_quiet_period;
1877	quiet->duration = htole16(vap->iv_quiet_duration);
1878	quiet->offset = htole16(vap->iv_quiet_offset);
1879	return frm + sizeof(*quiet);
1880}
1881
1882/*
1883 * Add an 11h Channel Switch Announcement element to a frame.
1884 * Note that we use the per-vap CSA count to adjust the global
1885 * counter so we can use this routine to form probe response
1886 * frames and get the current count.
1887 */
1888static uint8_t *
1889ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1890{
1891	struct ieee80211com *ic = vap->iv_ic;
1892	struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1893
1894	csa->csa_ie = IEEE80211_ELEMID_CSA;
1895	csa->csa_len = 3;
1896	csa->csa_mode = 1;		/* XXX force quiet on channel */
1897	csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1898	csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1899	return frm + sizeof(*csa);
1900}
1901
1902/*
1903 * Add an 11h country information element to a frame.
1904 */
1905static uint8_t *
1906ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1907{
1908
1909	if (ic->ic_countryie == NULL ||
1910	    ic->ic_countryie_chan != ic->ic_bsschan) {
1911		/*
1912		 * Handle lazy construction of ie.  This is done on
1913		 * first use and after a channel change that requires
1914		 * re-calculation.
1915		 */
1916		if (ic->ic_countryie != NULL)
1917			free(ic->ic_countryie, M_80211_NODE_IE);
1918		ic->ic_countryie = ieee80211_alloc_countryie(ic);
1919		if (ic->ic_countryie == NULL)
1920			return frm;
1921		ic->ic_countryie_chan = ic->ic_bsschan;
1922	}
1923	return add_appie(frm, ic->ic_countryie);
1924}
1925
1926uint8_t *
1927ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
1928{
1929	if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
1930		return (add_ie(frm, vap->iv_wpa_ie));
1931	else {
1932		/* XXX else complain? */
1933		return (frm);
1934	}
1935}
1936
1937uint8_t *
1938ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
1939{
1940	if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
1941		return (add_ie(frm, vap->iv_rsn_ie));
1942	else {
1943		/* XXX else complain? */
1944		return (frm);
1945	}
1946}
1947
1948uint8_t *
1949ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
1950{
1951	if (ni->ni_flags & IEEE80211_NODE_QOS) {
1952		*frm++ = IEEE80211_ELEMID_QOS;
1953		*frm++ = 1;
1954		*frm++ = 0;
1955	}
1956
1957	return (frm);
1958}
1959
1960/*
1961 * Send a probe request frame with the specified ssid
1962 * and any optional information element data.
1963 */
1964int
1965ieee80211_send_probereq(struct ieee80211_node *ni,
1966	const uint8_t sa[IEEE80211_ADDR_LEN],
1967	const uint8_t da[IEEE80211_ADDR_LEN],
1968	const uint8_t bssid[IEEE80211_ADDR_LEN],
1969	const uint8_t *ssid, size_t ssidlen)
1970{
1971	struct ieee80211vap *vap = ni->ni_vap;
1972	struct ieee80211com *ic = ni->ni_ic;
1973	const struct ieee80211_txparam *tp;
1974	struct ieee80211_bpf_params params;
1975	struct ieee80211_frame *wh;
1976	const struct ieee80211_rateset *rs;
1977	struct mbuf *m;
1978	uint8_t *frm;
1979	int ret;
1980
1981	if (vap->iv_state == IEEE80211_S_CAC) {
1982		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1983		    "block %s frame in CAC state", "probe request");
1984		vap->iv_stats.is_tx_badstate++;
1985		return EIO;		/* XXX */
1986	}
1987
1988	/*
1989	 * Hold a reference on the node so it doesn't go away until after
1990	 * the xmit is complete all the way in the driver.  On error we
1991	 * will remove our reference.
1992	 */
1993	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1994		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1995		__func__, __LINE__,
1996		ni, ether_sprintf(ni->ni_macaddr),
1997		ieee80211_node_refcnt(ni)+1);
1998	ieee80211_ref_node(ni);
1999
2000	/*
2001	 * prreq frame format
2002	 *	[tlv] ssid
2003	 *	[tlv] supported rates
2004	 *	[tlv] RSN (optional)
2005	 *	[tlv] extended supported rates
2006	 *	[tlv] WPA (optional)
2007	 *	[tlv] user-specified ie's
2008	 */
2009	m = ieee80211_getmgtframe(&frm,
2010		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2011	       	 2 + IEEE80211_NWID_LEN
2012	       + 2 + IEEE80211_RATE_SIZE
2013	       + sizeof(struct ieee80211_ie_wpa)
2014	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2015	       + sizeof(struct ieee80211_ie_wpa)
2016	       + (vap->iv_appie_probereq != NULL ?
2017		   vap->iv_appie_probereq->ie_len : 0)
2018	);
2019	if (m == NULL) {
2020		vap->iv_stats.is_tx_nobuf++;
2021		ieee80211_free_node(ni);
2022		return ENOMEM;
2023	}
2024
2025	frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2026	rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2027	frm = ieee80211_add_rates(frm, rs);
2028	frm = ieee80211_add_rsn(frm, vap);
2029	frm = ieee80211_add_xrates(frm, rs);
2030	frm = ieee80211_add_wpa(frm, vap);
2031	if (vap->iv_appie_probereq != NULL)
2032		frm = add_appie(frm, vap->iv_appie_probereq);
2033	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2034
2035	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2036	    ("leading space %zd", M_LEADINGSPACE(m)));
2037	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2038	if (m == NULL) {
2039		/* NB: cannot happen */
2040		ieee80211_free_node(ni);
2041		return ENOMEM;
2042	}
2043
2044	IEEE80211_TX_LOCK(ic);
2045	wh = mtod(m, struct ieee80211_frame *);
2046	ieee80211_send_setup(ni, m,
2047	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2048	     IEEE80211_NONQOS_TID, sa, da, bssid);
2049	/* XXX power management? */
2050	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2051
2052	M_WME_SETAC(m, WME_AC_BE);
2053
2054	IEEE80211_NODE_STAT(ni, tx_probereq);
2055	IEEE80211_NODE_STAT(ni, tx_mgmt);
2056
2057	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2058	    "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2059	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2060	    ssidlen, ssid);
2061
2062	memset(&params, 0, sizeof(params));
2063	params.ibp_pri = M_WME_GETAC(m);
2064	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2065	params.ibp_rate0 = tp->mgmtrate;
2066	if (IEEE80211_IS_MULTICAST(da)) {
2067		params.ibp_flags |= IEEE80211_BPF_NOACK;
2068		params.ibp_try0 = 1;
2069	} else
2070		params.ibp_try0 = tp->maxretry;
2071	params.ibp_power = ni->ni_txpower;
2072	ret = ieee80211_raw_output(vap, ni, m, &params);
2073	IEEE80211_TX_UNLOCK(ic);
2074	return (ret);
2075}
2076
2077/*
2078 * Calculate capability information for mgt frames.
2079 */
2080uint16_t
2081ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2082{
2083	struct ieee80211com *ic = vap->iv_ic;
2084	uint16_t capinfo;
2085
2086	KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2087
2088	if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2089		capinfo = IEEE80211_CAPINFO_ESS;
2090	else if (vap->iv_opmode == IEEE80211_M_IBSS)
2091		capinfo = IEEE80211_CAPINFO_IBSS;
2092	else
2093		capinfo = 0;
2094	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2095		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2096	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2097	    IEEE80211_IS_CHAN_2GHZ(chan))
2098		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2099	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2100		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2101	if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2102		capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2103	return capinfo;
2104}
2105
2106/*
2107 * Send a management frame.  The node is for the destination (or ic_bss
2108 * when in station mode).  Nodes other than ic_bss have their reference
2109 * count bumped to reflect our use for an indeterminant time.
2110 */
2111int
2112ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2113{
2114#define	HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2115#define	senderr(_x, _v)	do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2116	struct ieee80211vap *vap = ni->ni_vap;
2117	struct ieee80211com *ic = ni->ni_ic;
2118	struct ieee80211_node *bss = vap->iv_bss;
2119	struct ieee80211_bpf_params params;
2120	struct mbuf *m;
2121	uint8_t *frm;
2122	uint16_t capinfo;
2123	int has_challenge, is_shared_key, ret, status;
2124
2125	KASSERT(ni != NULL, ("null node"));
2126
2127	/*
2128	 * Hold a reference on the node so it doesn't go away until after
2129	 * the xmit is complete all the way in the driver.  On error we
2130	 * will remove our reference.
2131	 */
2132	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2133		"ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2134		__func__, __LINE__,
2135		ni, ether_sprintf(ni->ni_macaddr),
2136		ieee80211_node_refcnt(ni)+1);
2137	ieee80211_ref_node(ni);
2138
2139	memset(&params, 0, sizeof(params));
2140	switch (type) {
2141
2142	case IEEE80211_FC0_SUBTYPE_AUTH:
2143		status = arg >> 16;
2144		arg &= 0xffff;
2145		has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2146		    arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2147		    ni->ni_challenge != NULL);
2148
2149		/*
2150		 * Deduce whether we're doing open authentication or
2151		 * shared key authentication.  We do the latter if
2152		 * we're in the middle of a shared key authentication
2153		 * handshake or if we're initiating an authentication
2154		 * request and configured to use shared key.
2155		 */
2156		is_shared_key = has_challenge ||
2157		     arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2158		     (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2159		      bss->ni_authmode == IEEE80211_AUTH_SHARED);
2160
2161		m = ieee80211_getmgtframe(&frm,
2162			  ic->ic_headroom + sizeof(struct ieee80211_frame),
2163			  3 * sizeof(uint16_t)
2164			+ (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2165				sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2166		);
2167		if (m == NULL)
2168			senderr(ENOMEM, is_tx_nobuf);
2169
2170		((uint16_t *)frm)[0] =
2171		    (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2172		                    : htole16(IEEE80211_AUTH_ALG_OPEN);
2173		((uint16_t *)frm)[1] = htole16(arg);	/* sequence number */
2174		((uint16_t *)frm)[2] = htole16(status);/* status */
2175
2176		if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2177			((uint16_t *)frm)[3] =
2178			    htole16((IEEE80211_CHALLENGE_LEN << 8) |
2179			    IEEE80211_ELEMID_CHALLENGE);
2180			memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2181			    IEEE80211_CHALLENGE_LEN);
2182			m->m_pkthdr.len = m->m_len =
2183				4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2184			if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2185				IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2186				    "request encrypt frame (%s)", __func__);
2187				/* mark frame for encryption */
2188				params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2189			}
2190		} else
2191			m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2192
2193		/* XXX not right for shared key */
2194		if (status == IEEE80211_STATUS_SUCCESS)
2195			IEEE80211_NODE_STAT(ni, tx_auth);
2196		else
2197			IEEE80211_NODE_STAT(ni, tx_auth_fail);
2198
2199		if (vap->iv_opmode == IEEE80211_M_STA)
2200			ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2201				(void *) vap->iv_state);
2202		break;
2203
2204	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2205		IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2206		    "send station deauthenticate (reason %d)", arg);
2207		m = ieee80211_getmgtframe(&frm,
2208			ic->ic_headroom + sizeof(struct ieee80211_frame),
2209			sizeof(uint16_t));
2210		if (m == NULL)
2211			senderr(ENOMEM, is_tx_nobuf);
2212		*(uint16_t *)frm = htole16(arg);	/* reason */
2213		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2214
2215		IEEE80211_NODE_STAT(ni, tx_deauth);
2216		IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2217
2218		ieee80211_node_unauthorize(ni);		/* port closed */
2219		break;
2220
2221	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2222	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2223		/*
2224		 * asreq frame format
2225		 *	[2] capability information
2226		 *	[2] listen interval
2227		 *	[6*] current AP address (reassoc only)
2228		 *	[tlv] ssid
2229		 *	[tlv] supported rates
2230		 *	[tlv] extended supported rates
2231		 *	[4] power capability (optional)
2232		 *	[28] supported channels (optional)
2233		 *	[tlv] HT capabilities
2234		 *	[tlv] WME (optional)
2235		 *	[tlv] Vendor OUI HT capabilities (optional)
2236		 *	[tlv] Atheros capabilities (if negotiated)
2237		 *	[tlv] AppIE's (optional)
2238		 */
2239		m = ieee80211_getmgtframe(&frm,
2240			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2241			 sizeof(uint16_t)
2242		       + sizeof(uint16_t)
2243		       + IEEE80211_ADDR_LEN
2244		       + 2 + IEEE80211_NWID_LEN
2245		       + 2 + IEEE80211_RATE_SIZE
2246		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2247		       + 4
2248		       + 2 + 26
2249		       + sizeof(struct ieee80211_wme_info)
2250		       + sizeof(struct ieee80211_ie_htcap)
2251		       + 4 + sizeof(struct ieee80211_ie_htcap)
2252#ifdef IEEE80211_SUPPORT_SUPERG
2253		       + sizeof(struct ieee80211_ath_ie)
2254#endif
2255		       + (vap->iv_appie_wpa != NULL ?
2256				vap->iv_appie_wpa->ie_len : 0)
2257		       + (vap->iv_appie_assocreq != NULL ?
2258				vap->iv_appie_assocreq->ie_len : 0)
2259		);
2260		if (m == NULL)
2261			senderr(ENOMEM, is_tx_nobuf);
2262
2263		KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2264		    ("wrong mode %u", vap->iv_opmode));
2265		capinfo = IEEE80211_CAPINFO_ESS;
2266		if (vap->iv_flags & IEEE80211_F_PRIVACY)
2267			capinfo |= IEEE80211_CAPINFO_PRIVACY;
2268		/*
2269		 * NB: Some 11a AP's reject the request when
2270		 *     short premable is set.
2271		 */
2272		if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2273		    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2274			capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2275		if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2276		    (ic->ic_caps & IEEE80211_C_SHSLOT))
2277			capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2278		if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2279		    (vap->iv_flags & IEEE80211_F_DOTH))
2280			capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2281		*(uint16_t *)frm = htole16(capinfo);
2282		frm += 2;
2283
2284		KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2285		*(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2286						    bss->ni_intval));
2287		frm += 2;
2288
2289		if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2290			IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2291			frm += IEEE80211_ADDR_LEN;
2292		}
2293
2294		frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2295		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2296		frm = ieee80211_add_rsn(frm, vap);
2297		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2298		if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2299			frm = ieee80211_add_powercapability(frm,
2300			    ic->ic_curchan);
2301			frm = ieee80211_add_supportedchannels(frm, ic);
2302		}
2303		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2304		    ni->ni_ies.htcap_ie != NULL &&
2305		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2306			frm = ieee80211_add_htcap(frm, ni);
2307		frm = ieee80211_add_wpa(frm, vap);
2308		if ((ic->ic_flags & IEEE80211_F_WME) &&
2309		    ni->ni_ies.wme_ie != NULL)
2310			frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2311		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2312		    ni->ni_ies.htcap_ie != NULL &&
2313		    ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2314			frm = ieee80211_add_htcap_vendor(frm, ni);
2315#ifdef IEEE80211_SUPPORT_SUPERG
2316		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2317			frm = ieee80211_add_ath(frm,
2318				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2319				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2320				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2321				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2322		}
2323#endif /* IEEE80211_SUPPORT_SUPERG */
2324		if (vap->iv_appie_assocreq != NULL)
2325			frm = add_appie(frm, vap->iv_appie_assocreq);
2326		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2327
2328		ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2329			(void *) vap->iv_state);
2330		break;
2331
2332	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2333	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2334		/*
2335		 * asresp frame format
2336		 *	[2] capability information
2337		 *	[2] status
2338		 *	[2] association ID
2339		 *	[tlv] supported rates
2340		 *	[tlv] extended supported rates
2341		 *	[tlv] HT capabilities (standard, if STA enabled)
2342		 *	[tlv] HT information (standard, if STA enabled)
2343		 *	[tlv] WME (if configured and STA enabled)
2344		 *	[tlv] HT capabilities (vendor OUI, if STA enabled)
2345		 *	[tlv] HT information (vendor OUI, if STA enabled)
2346		 *	[tlv] Atheros capabilities (if STA enabled)
2347		 *	[tlv] AppIE's (optional)
2348		 */
2349		m = ieee80211_getmgtframe(&frm,
2350			 ic->ic_headroom + sizeof(struct ieee80211_frame),
2351			 sizeof(uint16_t)
2352		       + sizeof(uint16_t)
2353		       + sizeof(uint16_t)
2354		       + 2 + IEEE80211_RATE_SIZE
2355		       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2356		       + sizeof(struct ieee80211_ie_htcap) + 4
2357		       + sizeof(struct ieee80211_ie_htinfo) + 4
2358		       + sizeof(struct ieee80211_wme_param)
2359#ifdef IEEE80211_SUPPORT_SUPERG
2360		       + sizeof(struct ieee80211_ath_ie)
2361#endif
2362		       + (vap->iv_appie_assocresp != NULL ?
2363				vap->iv_appie_assocresp->ie_len : 0)
2364		);
2365		if (m == NULL)
2366			senderr(ENOMEM, is_tx_nobuf);
2367
2368		capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2369		*(uint16_t *)frm = htole16(capinfo);
2370		frm += 2;
2371
2372		*(uint16_t *)frm = htole16(arg);	/* status */
2373		frm += 2;
2374
2375		if (arg == IEEE80211_STATUS_SUCCESS) {
2376			*(uint16_t *)frm = htole16(ni->ni_associd);
2377			IEEE80211_NODE_STAT(ni, tx_assoc);
2378		} else
2379			IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2380		frm += 2;
2381
2382		frm = ieee80211_add_rates(frm, &ni->ni_rates);
2383		frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2384		/* NB: respond according to what we received */
2385		if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2386			frm = ieee80211_add_htcap(frm, ni);
2387			frm = ieee80211_add_htinfo(frm, ni);
2388		}
2389		if ((vap->iv_flags & IEEE80211_F_WME) &&
2390		    ni->ni_ies.wme_ie != NULL)
2391			frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2392		if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2393			frm = ieee80211_add_htcap_vendor(frm, ni);
2394			frm = ieee80211_add_htinfo_vendor(frm, ni);
2395		}
2396#ifdef IEEE80211_SUPPORT_SUPERG
2397		if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2398			frm = ieee80211_add_ath(frm,
2399				IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2400				((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2401				 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2402				vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2403#endif /* IEEE80211_SUPPORT_SUPERG */
2404		if (vap->iv_appie_assocresp != NULL)
2405			frm = add_appie(frm, vap->iv_appie_assocresp);
2406		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2407		break;
2408
2409	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2410		IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2411		    "send station disassociate (reason %d)", arg);
2412		m = ieee80211_getmgtframe(&frm,
2413			ic->ic_headroom + sizeof(struct ieee80211_frame),
2414			sizeof(uint16_t));
2415		if (m == NULL)
2416			senderr(ENOMEM, is_tx_nobuf);
2417		*(uint16_t *)frm = htole16(arg);	/* reason */
2418		m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2419
2420		IEEE80211_NODE_STAT(ni, tx_disassoc);
2421		IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2422		break;
2423
2424	default:
2425		IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2426		    "invalid mgmt frame type %u", type);
2427		senderr(EINVAL, is_tx_unknownmgt);
2428		/* NOTREACHED */
2429	}
2430
2431	/* NB: force non-ProbeResp frames to the highest queue */
2432	params.ibp_pri = WME_AC_VO;
2433	params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2434	/* NB: we know all frames are unicast */
2435	params.ibp_try0 = bss->ni_txparms->maxretry;
2436	params.ibp_power = bss->ni_txpower;
2437	return ieee80211_mgmt_output(ni, m, type, &params);
2438bad:
2439	ieee80211_free_node(ni);
2440	return ret;
2441#undef senderr
2442#undef HTFLAGS
2443}
2444
2445/*
2446 * Return an mbuf with a probe response frame in it.
2447 * Space is left to prepend and 802.11 header at the
2448 * front but it's left to the caller to fill in.
2449 */
2450struct mbuf *
2451ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2452{
2453	struct ieee80211vap *vap = bss->ni_vap;
2454	struct ieee80211com *ic = bss->ni_ic;
2455	const struct ieee80211_rateset *rs;
2456	struct mbuf *m;
2457	uint16_t capinfo;
2458	uint8_t *frm;
2459
2460	/*
2461	 * probe response frame format
2462	 *	[8] time stamp
2463	 *	[2] beacon interval
2464	 *	[2] cabability information
2465	 *	[tlv] ssid
2466	 *	[tlv] supported rates
2467	 *	[tlv] parameter set (FH/DS)
2468	 *	[tlv] parameter set (IBSS)
2469	 *	[tlv] country (optional)
2470	 *	[3] power control (optional)
2471	 *	[5] channel switch announcement (CSA) (optional)
2472	 *	[tlv] extended rate phy (ERP)
2473	 *	[tlv] extended supported rates
2474	 *	[tlv] RSN (optional)
2475	 *	[tlv] HT capabilities
2476	 *	[tlv] HT information
2477	 *	[tlv] WPA (optional)
2478	 *	[tlv] WME (optional)
2479	 *	[tlv] Vendor OUI HT capabilities (optional)
2480	 *	[tlv] Vendor OUI HT information (optional)
2481	 *	[tlv] Atheros capabilities
2482	 *	[tlv] AppIE's (optional)
2483	 *	[tlv] Mesh ID (MBSS)
2484	 *	[tlv] Mesh Conf (MBSS)
2485	 */
2486	m = ieee80211_getmgtframe(&frm,
2487		 ic->ic_headroom + sizeof(struct ieee80211_frame),
2488		 8
2489	       + sizeof(uint16_t)
2490	       + sizeof(uint16_t)
2491	       + 2 + IEEE80211_NWID_LEN
2492	       + 2 + IEEE80211_RATE_SIZE
2493	       + 7	/* max(7,3) */
2494	       + IEEE80211_COUNTRY_MAX_SIZE
2495	       + 3
2496	       + sizeof(struct ieee80211_csa_ie)
2497	       + sizeof(struct ieee80211_quiet_ie)
2498	       + 3
2499	       + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2500	       + sizeof(struct ieee80211_ie_wpa)
2501	       + sizeof(struct ieee80211_ie_htcap)
2502	       + sizeof(struct ieee80211_ie_htinfo)
2503	       + sizeof(struct ieee80211_ie_wpa)
2504	       + sizeof(struct ieee80211_wme_param)
2505	       + 4 + sizeof(struct ieee80211_ie_htcap)
2506	       + 4 + sizeof(struct ieee80211_ie_htinfo)
2507#ifdef IEEE80211_SUPPORT_SUPERG
2508	       + sizeof(struct ieee80211_ath_ie)
2509#endif
2510#ifdef IEEE80211_SUPPORT_MESH
2511	       + 2 + IEEE80211_MESHID_LEN
2512	       + sizeof(struct ieee80211_meshconf_ie)
2513#endif
2514	       + (vap->iv_appie_proberesp != NULL ?
2515			vap->iv_appie_proberesp->ie_len : 0)
2516	);
2517	if (m == NULL) {
2518		vap->iv_stats.is_tx_nobuf++;
2519		return NULL;
2520	}
2521
2522	memset(frm, 0, 8);	/* timestamp should be filled later */
2523	frm += 8;
2524	*(uint16_t *)frm = htole16(bss->ni_intval);
2525	frm += 2;
2526	capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2527	*(uint16_t *)frm = htole16(capinfo);
2528	frm += 2;
2529
2530	frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2531	rs = ieee80211_get_suprates(ic, bss->ni_chan);
2532	frm = ieee80211_add_rates(frm, rs);
2533
2534	if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2535		*frm++ = IEEE80211_ELEMID_FHPARMS;
2536		*frm++ = 5;
2537		*frm++ = bss->ni_fhdwell & 0x00ff;
2538		*frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2539		*frm++ = IEEE80211_FH_CHANSET(
2540		    ieee80211_chan2ieee(ic, bss->ni_chan));
2541		*frm++ = IEEE80211_FH_CHANPAT(
2542		    ieee80211_chan2ieee(ic, bss->ni_chan));
2543		*frm++ = bss->ni_fhindex;
2544	} else {
2545		*frm++ = IEEE80211_ELEMID_DSPARMS;
2546		*frm++ = 1;
2547		*frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2548	}
2549
2550	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2551		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2552		*frm++ = 2;
2553		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2554	}
2555	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2556	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2557		frm = ieee80211_add_countryie(frm, ic);
2558	if (vap->iv_flags & IEEE80211_F_DOTH) {
2559		if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2560			frm = ieee80211_add_powerconstraint(frm, vap);
2561		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2562			frm = ieee80211_add_csa(frm, vap);
2563	}
2564	if (vap->iv_flags & IEEE80211_F_DOTH) {
2565		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2566		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2567			if (vap->iv_quiet)
2568				frm = ieee80211_add_quiet(frm, vap);
2569		}
2570	}
2571	if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2572		frm = ieee80211_add_erp(frm, ic);
2573	frm = ieee80211_add_xrates(frm, rs);
2574	frm = ieee80211_add_rsn(frm, vap);
2575	/*
2576	 * NB: legacy 11b clients do not get certain ie's.
2577	 *     The caller identifies such clients by passing
2578	 *     a token in legacy to us.  Could expand this to be
2579	 *     any legacy client for stuff like HT ie's.
2580	 */
2581	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2582	    legacy != IEEE80211_SEND_LEGACY_11B) {
2583		frm = ieee80211_add_htcap(frm, bss);
2584		frm = ieee80211_add_htinfo(frm, bss);
2585	}
2586	frm = ieee80211_add_wpa(frm, vap);
2587	if (vap->iv_flags & IEEE80211_F_WME)
2588		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2589	if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2590	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2591	    legacy != IEEE80211_SEND_LEGACY_11B) {
2592		frm = ieee80211_add_htcap_vendor(frm, bss);
2593		frm = ieee80211_add_htinfo_vendor(frm, bss);
2594	}
2595#ifdef IEEE80211_SUPPORT_SUPERG
2596	if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2597	    legacy != IEEE80211_SEND_LEGACY_11B)
2598		frm = ieee80211_add_athcaps(frm, bss);
2599#endif
2600	if (vap->iv_appie_proberesp != NULL)
2601		frm = add_appie(frm, vap->iv_appie_proberesp);
2602#ifdef IEEE80211_SUPPORT_MESH
2603	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2604		frm = ieee80211_add_meshid(frm, vap);
2605		frm = ieee80211_add_meshconf(frm, vap);
2606	}
2607#endif
2608	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2609
2610	return m;
2611}
2612
2613/*
2614 * Send a probe response frame to the specified mac address.
2615 * This does not go through the normal mgt frame api so we
2616 * can specify the destination address and re-use the bss node
2617 * for the sta reference.
2618 */
2619int
2620ieee80211_send_proberesp(struct ieee80211vap *vap,
2621	const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2622{
2623	struct ieee80211_node *bss = vap->iv_bss;
2624	struct ieee80211com *ic = vap->iv_ic;
2625	struct ieee80211_frame *wh;
2626	struct mbuf *m;
2627	int ret;
2628
2629	if (vap->iv_state == IEEE80211_S_CAC) {
2630		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2631		    "block %s frame in CAC state", "probe response");
2632		vap->iv_stats.is_tx_badstate++;
2633		return EIO;		/* XXX */
2634	}
2635
2636	/*
2637	 * Hold a reference on the node so it doesn't go away until after
2638	 * the xmit is complete all the way in the driver.  On error we
2639	 * will remove our reference.
2640	 */
2641	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2642	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2643	    __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2644	    ieee80211_node_refcnt(bss)+1);
2645	ieee80211_ref_node(bss);
2646
2647	m = ieee80211_alloc_proberesp(bss, legacy);
2648	if (m == NULL) {
2649		ieee80211_free_node(bss);
2650		return ENOMEM;
2651	}
2652
2653	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2654	KASSERT(m != NULL, ("no room for header"));
2655
2656	IEEE80211_TX_LOCK(ic);
2657	wh = mtod(m, struct ieee80211_frame *);
2658	ieee80211_send_setup(bss, m,
2659	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2660	     IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2661	/* XXX power management? */
2662	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2663
2664	M_WME_SETAC(m, WME_AC_BE);
2665
2666	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2667	    "send probe resp on channel %u to %s%s\n",
2668	    ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2669	    legacy ? " <legacy>" : "");
2670	IEEE80211_NODE_STAT(bss, tx_mgmt);
2671
2672	ret = ieee80211_raw_output(vap, bss, m, NULL);
2673	IEEE80211_TX_UNLOCK(ic);
2674	return (ret);
2675}
2676
2677/*
2678 * Allocate and build a RTS (Request To Send) control frame.
2679 */
2680struct mbuf *
2681ieee80211_alloc_rts(struct ieee80211com *ic,
2682	const uint8_t ra[IEEE80211_ADDR_LEN],
2683	const uint8_t ta[IEEE80211_ADDR_LEN],
2684	uint16_t dur)
2685{
2686	struct ieee80211_frame_rts *rts;
2687	struct mbuf *m;
2688
2689	/* XXX honor ic_headroom */
2690	m = m_gethdr(M_NOWAIT, MT_DATA);
2691	if (m != NULL) {
2692		rts = mtod(m, struct ieee80211_frame_rts *);
2693		rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2694			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2695		rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2696		*(u_int16_t *)rts->i_dur = htole16(dur);
2697		IEEE80211_ADDR_COPY(rts->i_ra, ra);
2698		IEEE80211_ADDR_COPY(rts->i_ta, ta);
2699
2700		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2701	}
2702	return m;
2703}
2704
2705/*
2706 * Allocate and build a CTS (Clear To Send) control frame.
2707 */
2708struct mbuf *
2709ieee80211_alloc_cts(struct ieee80211com *ic,
2710	const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2711{
2712	struct ieee80211_frame_cts *cts;
2713	struct mbuf *m;
2714
2715	/* XXX honor ic_headroom */
2716	m = m_gethdr(M_NOWAIT, MT_DATA);
2717	if (m != NULL) {
2718		cts = mtod(m, struct ieee80211_frame_cts *);
2719		cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2720			IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2721		cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2722		*(u_int16_t *)cts->i_dur = htole16(dur);
2723		IEEE80211_ADDR_COPY(cts->i_ra, ra);
2724
2725		m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2726	}
2727	return m;
2728}
2729
2730static void
2731ieee80211_tx_mgt_timeout(void *arg)
2732{
2733	struct ieee80211_node *ni = arg;
2734	struct ieee80211vap *vap = ni->ni_vap;
2735
2736	if (vap->iv_state != IEEE80211_S_INIT &&
2737	    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2738		/*
2739		 * NB: it's safe to specify a timeout as the reason here;
2740		 *     it'll only be used in the right state.
2741		 */
2742		ieee80211_new_state(vap, IEEE80211_S_SCAN,
2743			IEEE80211_SCAN_FAIL_TIMEOUT);
2744	}
2745}
2746
2747static void
2748ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2749{
2750	struct ieee80211vap *vap = ni->ni_vap;
2751	enum ieee80211_state ostate = (enum ieee80211_state) arg;
2752
2753	/*
2754	 * Frame transmit completed; arrange timer callback.  If
2755	 * transmit was successfuly we wait for response.  Otherwise
2756	 * we arrange an immediate callback instead of doing the
2757	 * callback directly since we don't know what state the driver
2758	 * is in (e.g. what locks it is holding).  This work should
2759	 * not be too time-critical and not happen too often so the
2760	 * added overhead is acceptable.
2761	 *
2762	 * XXX what happens if !acked but response shows up before callback?
2763	 */
2764	if (vap->iv_state == ostate)
2765		callout_reset(&vap->iv_mgtsend,
2766			status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2767			ieee80211_tx_mgt_timeout, ni);
2768}
2769
2770static void
2771ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2772	struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2773{
2774	struct ieee80211vap *vap = ni->ni_vap;
2775	struct ieee80211com *ic = ni->ni_ic;
2776	struct ieee80211_rateset *rs = &ni->ni_rates;
2777	uint16_t capinfo;
2778
2779	/*
2780	 * beacon frame format
2781	 *	[8] time stamp
2782	 *	[2] beacon interval
2783	 *	[2] cabability information
2784	 *	[tlv] ssid
2785	 *	[tlv] supported rates
2786	 *	[3] parameter set (DS)
2787	 *	[8] CF parameter set (optional)
2788	 *	[tlv] parameter set (IBSS/TIM)
2789	 *	[tlv] country (optional)
2790	 *	[3] power control (optional)
2791	 *	[5] channel switch announcement (CSA) (optional)
2792	 *	[tlv] extended rate phy (ERP)
2793	 *	[tlv] extended supported rates
2794	 *	[tlv] RSN parameters
2795	 *	[tlv] HT capabilities
2796	 *	[tlv] HT information
2797	 * XXX Vendor-specific OIDs (e.g. Atheros)
2798	 *	[tlv] WPA parameters
2799	 *	[tlv] WME parameters
2800	 *	[tlv] Vendor OUI HT capabilities (optional)
2801	 *	[tlv] Vendor OUI HT information (optional)
2802	 *	[tlv] Atheros capabilities (optional)
2803	 *	[tlv] TDMA parameters (optional)
2804	 *	[tlv] Mesh ID (MBSS)
2805	 *	[tlv] Mesh Conf (MBSS)
2806	 *	[tlv] application data (optional)
2807	 */
2808
2809	memset(bo, 0, sizeof(*bo));
2810
2811	memset(frm, 0, 8);	/* XXX timestamp is set by hardware/driver */
2812	frm += 8;
2813	*(uint16_t *)frm = htole16(ni->ni_intval);
2814	frm += 2;
2815	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2816	bo->bo_caps = (uint16_t *)frm;
2817	*(uint16_t *)frm = htole16(capinfo);
2818	frm += 2;
2819	*frm++ = IEEE80211_ELEMID_SSID;
2820	if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2821		*frm++ = ni->ni_esslen;
2822		memcpy(frm, ni->ni_essid, ni->ni_esslen);
2823		frm += ni->ni_esslen;
2824	} else
2825		*frm++ = 0;
2826	frm = ieee80211_add_rates(frm, rs);
2827	if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2828		*frm++ = IEEE80211_ELEMID_DSPARMS;
2829		*frm++ = 1;
2830		*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2831	}
2832	if (ic->ic_flags & IEEE80211_F_PCF) {
2833		bo->bo_cfp = frm;
2834		frm = ieee80211_add_cfparms(frm, ic);
2835	}
2836	bo->bo_tim = frm;
2837	if (vap->iv_opmode == IEEE80211_M_IBSS) {
2838		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
2839		*frm++ = 2;
2840		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
2841		bo->bo_tim_len = 0;
2842	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2843	    vap->iv_opmode == IEEE80211_M_MBSS) {
2844		/* TIM IE is the same for Mesh and Hostap */
2845		struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2846
2847		tie->tim_ie = IEEE80211_ELEMID_TIM;
2848		tie->tim_len = 4;	/* length */
2849		tie->tim_count = 0;	/* DTIM count */
2850		tie->tim_period = vap->iv_dtim_period;	/* DTIM period */
2851		tie->tim_bitctl = 0;	/* bitmap control */
2852		tie->tim_bitmap[0] = 0;	/* Partial Virtual Bitmap */
2853		frm += sizeof(struct ieee80211_tim_ie);
2854		bo->bo_tim_len = 1;
2855	}
2856	bo->bo_tim_trailer = frm;
2857	if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2858	    (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2859		frm = ieee80211_add_countryie(frm, ic);
2860	if (vap->iv_flags & IEEE80211_F_DOTH) {
2861		if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2862			frm = ieee80211_add_powerconstraint(frm, vap);
2863		bo->bo_csa = frm;
2864		if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2865			frm = ieee80211_add_csa(frm, vap);
2866	} else
2867		bo->bo_csa = frm;
2868
2869	if (vap->iv_flags & IEEE80211_F_DOTH) {
2870		bo->bo_quiet = frm;
2871		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2872		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2873			if (vap->iv_quiet)
2874				frm = ieee80211_add_quiet(frm,vap);
2875		}
2876	} else
2877		bo->bo_quiet = frm;
2878
2879	if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2880		bo->bo_erp = frm;
2881		frm = ieee80211_add_erp(frm, ic);
2882	}
2883	frm = ieee80211_add_xrates(frm, rs);
2884	frm = ieee80211_add_rsn(frm, vap);
2885	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2886		frm = ieee80211_add_htcap(frm, ni);
2887		bo->bo_htinfo = frm;
2888		frm = ieee80211_add_htinfo(frm, ni);
2889	}
2890	frm = ieee80211_add_wpa(frm, vap);
2891	if (vap->iv_flags & IEEE80211_F_WME) {
2892		bo->bo_wme = frm;
2893		frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2894	}
2895	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2896	    (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2897		frm = ieee80211_add_htcap_vendor(frm, ni);
2898		frm = ieee80211_add_htinfo_vendor(frm, ni);
2899	}
2900#ifdef IEEE80211_SUPPORT_SUPERG
2901	if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2902		bo->bo_ath = frm;
2903		frm = ieee80211_add_athcaps(frm, ni);
2904	}
2905#endif
2906#ifdef IEEE80211_SUPPORT_TDMA
2907	if (vap->iv_caps & IEEE80211_C_TDMA) {
2908		bo->bo_tdma = frm;
2909		frm = ieee80211_add_tdma(frm, vap);
2910	}
2911#endif
2912	if (vap->iv_appie_beacon != NULL) {
2913		bo->bo_appie = frm;
2914		bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2915		frm = add_appie(frm, vap->iv_appie_beacon);
2916	}
2917#ifdef IEEE80211_SUPPORT_MESH
2918	if (vap->iv_opmode == IEEE80211_M_MBSS) {
2919		frm = ieee80211_add_meshid(frm, vap);
2920		bo->bo_meshconf = frm;
2921		frm = ieee80211_add_meshconf(frm, vap);
2922	}
2923#endif
2924	bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2925	bo->bo_csa_trailer_len = frm - bo->bo_csa;
2926	m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2927}
2928
2929/*
2930 * Allocate a beacon frame and fillin the appropriate bits.
2931 */
2932struct mbuf *
2933ieee80211_beacon_alloc(struct ieee80211_node *ni,
2934	struct ieee80211_beacon_offsets *bo)
2935{
2936	struct ieee80211vap *vap = ni->ni_vap;
2937	struct ieee80211com *ic = ni->ni_ic;
2938	struct ifnet *ifp = vap->iv_ifp;
2939	struct ieee80211_frame *wh;
2940	struct mbuf *m;
2941	int pktlen;
2942	uint8_t *frm;
2943
2944	/*
2945	 * beacon frame format
2946	 *	[8] time stamp
2947	 *	[2] beacon interval
2948	 *	[2] cabability information
2949	 *	[tlv] ssid
2950	 *	[tlv] supported rates
2951	 *	[3] parameter set (DS)
2952	 *	[8] CF parameter set (optional)
2953	 *	[tlv] parameter set (IBSS/TIM)
2954	 *	[tlv] country (optional)
2955	 *	[3] power control (optional)
2956	 *	[5] channel switch announcement (CSA) (optional)
2957	 *	[tlv] extended rate phy (ERP)
2958	 *	[tlv] extended supported rates
2959	 *	[tlv] RSN parameters
2960	 *	[tlv] HT capabilities
2961	 *	[tlv] HT information
2962	 *	[tlv] Vendor OUI HT capabilities (optional)
2963	 *	[tlv] Vendor OUI HT information (optional)
2964	 * XXX Vendor-specific OIDs (e.g. Atheros)
2965	 *	[tlv] WPA parameters
2966	 *	[tlv] WME parameters
2967	 *	[tlv] TDMA parameters (optional)
2968	 *	[tlv] Mesh ID (MBSS)
2969	 *	[tlv] Mesh Conf (MBSS)
2970	 *	[tlv] application data (optional)
2971	 * NB: we allocate the max space required for the TIM bitmap.
2972	 * XXX how big is this?
2973	 */
2974	pktlen =   8					/* time stamp */
2975		 + sizeof(uint16_t)			/* beacon interval */
2976		 + sizeof(uint16_t)			/* capabilities */
2977		 + 2 + ni->ni_esslen			/* ssid */
2978	         + 2 + IEEE80211_RATE_SIZE		/* supported rates */
2979	         + 2 + 1				/* DS parameters */
2980		 + 2 + 6				/* CF parameters */
2981		 + 2 + 4 + vap->iv_tim_len		/* DTIM/IBSSPARMS */
2982		 + IEEE80211_COUNTRY_MAX_SIZE		/* country */
2983		 + 2 + 1				/* power control */
2984		 + sizeof(struct ieee80211_csa_ie)	/* CSA */
2985		 + sizeof(struct ieee80211_quiet_ie)	/* Quiet */
2986		 + 2 + 1				/* ERP */
2987	         + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2988		 + (vap->iv_caps & IEEE80211_C_WPA ?	/* WPA 1+2 */
2989			2*sizeof(struct ieee80211_ie_wpa) : 0)
2990		 /* XXX conditional? */
2991		 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2992		 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2993		 + (vap->iv_caps & IEEE80211_C_WME ?	/* WME */
2994			sizeof(struct ieee80211_wme_param) : 0)
2995#ifdef IEEE80211_SUPPORT_SUPERG
2996		 + sizeof(struct ieee80211_ath_ie)	/* ATH */
2997#endif
2998#ifdef IEEE80211_SUPPORT_TDMA
2999		 + (vap->iv_caps & IEEE80211_C_TDMA ?	/* TDMA */
3000			sizeof(struct ieee80211_tdma_param) : 0)
3001#endif
3002#ifdef IEEE80211_SUPPORT_MESH
3003		 + 2 + ni->ni_meshidlen
3004		 + sizeof(struct ieee80211_meshconf_ie)
3005#endif
3006		 + IEEE80211_MAX_APPIE
3007		 ;
3008	m = ieee80211_getmgtframe(&frm,
3009		ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3010	if (m == NULL) {
3011		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3012			"%s: cannot get buf; size %u\n", __func__, pktlen);
3013		vap->iv_stats.is_tx_nobuf++;
3014		return NULL;
3015	}
3016	ieee80211_beacon_construct(m, frm, bo, ni);
3017
3018	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3019	KASSERT(m != NULL, ("no space for 802.11 header?"));
3020	wh = mtod(m, struct ieee80211_frame *);
3021	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3022	    IEEE80211_FC0_SUBTYPE_BEACON;
3023	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3024	*(uint16_t *)wh->i_dur = 0;
3025	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3026	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3027	IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3028	*(uint16_t *)wh->i_seq = 0;
3029
3030	return m;
3031}
3032
3033/*
3034 * Update the dynamic parts of a beacon frame based on the current state.
3035 */
3036int
3037ieee80211_beacon_update(struct ieee80211_node *ni,
3038	struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
3039{
3040	struct ieee80211vap *vap = ni->ni_vap;
3041	struct ieee80211com *ic = ni->ni_ic;
3042	int len_changed = 0;
3043	uint16_t capinfo;
3044	struct ieee80211_frame *wh;
3045	ieee80211_seq seqno;
3046
3047	IEEE80211_LOCK(ic);
3048	/*
3049	 * Handle 11h channel change when we've reached the count.
3050	 * We must recalculate the beacon frame contents to account
3051	 * for the new channel.  Note we do this only for the first
3052	 * vap that reaches this point; subsequent vaps just update
3053	 * their beacon state to reflect the recalculated channel.
3054	 */
3055	if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3056	    vap->iv_csa_count == ic->ic_csa_count) {
3057		vap->iv_csa_count = 0;
3058		/*
3059		 * Effect channel change before reconstructing the beacon
3060		 * frame contents as many places reference ni_chan.
3061		 */
3062		if (ic->ic_csa_newchan != NULL)
3063			ieee80211_csa_completeswitch(ic);
3064		/*
3065		 * NB: ieee80211_beacon_construct clears all pending
3066		 * updates in bo_flags so we don't need to explicitly
3067		 * clear IEEE80211_BEACON_CSA.
3068		 */
3069		ieee80211_beacon_construct(m,
3070		    mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
3071
3072		/* XXX do WME aggressive mode processing? */
3073		IEEE80211_UNLOCK(ic);
3074		return 1;		/* just assume length changed */
3075	}
3076
3077	wh = mtod(m, struct ieee80211_frame *);
3078	seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3079	*(uint16_t *)&wh->i_seq[0] =
3080		htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3081	M_SEQNO_SET(m, seqno);
3082
3083	/* XXX faster to recalculate entirely or just changes? */
3084	capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3085	*bo->bo_caps = htole16(capinfo);
3086
3087	if (vap->iv_flags & IEEE80211_F_WME) {
3088		struct ieee80211_wme_state *wme = &ic->ic_wme;
3089
3090		/*
3091		 * Check for agressive mode change.  When there is
3092		 * significant high priority traffic in the BSS
3093		 * throttle back BE traffic by using conservative
3094		 * parameters.  Otherwise BE uses agressive params
3095		 * to optimize performance of legacy/non-QoS traffic.
3096		 */
3097		if (wme->wme_flags & WME_F_AGGRMODE) {
3098			if (wme->wme_hipri_traffic >
3099			    wme->wme_hipri_switch_thresh) {
3100				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3101				    "%s: traffic %u, disable aggressive mode\n",
3102				    __func__, wme->wme_hipri_traffic);
3103				wme->wme_flags &= ~WME_F_AGGRMODE;
3104				ieee80211_wme_updateparams_locked(vap);
3105				wme->wme_hipri_traffic =
3106					wme->wme_hipri_switch_hysteresis;
3107			} else
3108				wme->wme_hipri_traffic = 0;
3109		} else {
3110			if (wme->wme_hipri_traffic <=
3111			    wme->wme_hipri_switch_thresh) {
3112				IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3113				    "%s: traffic %u, enable aggressive mode\n",
3114				    __func__, wme->wme_hipri_traffic);
3115				wme->wme_flags |= WME_F_AGGRMODE;
3116				ieee80211_wme_updateparams_locked(vap);
3117				wme->wme_hipri_traffic = 0;
3118			} else
3119				wme->wme_hipri_traffic =
3120					wme->wme_hipri_switch_hysteresis;
3121		}
3122		if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3123			(void) ieee80211_add_wme_param(bo->bo_wme, wme);
3124			clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3125		}
3126	}
3127
3128	if (isset(bo->bo_flags,  IEEE80211_BEACON_HTINFO)) {
3129		ieee80211_ht_update_beacon(vap, bo);
3130		clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3131	}
3132#ifdef IEEE80211_SUPPORT_TDMA
3133	if (vap->iv_caps & IEEE80211_C_TDMA) {
3134		/*
3135		 * NB: the beacon is potentially updated every TBTT.
3136		 */
3137		ieee80211_tdma_update_beacon(vap, bo);
3138	}
3139#endif
3140#ifdef IEEE80211_SUPPORT_MESH
3141	if (vap->iv_opmode == IEEE80211_M_MBSS)
3142		ieee80211_mesh_update_beacon(vap, bo);
3143#endif
3144
3145	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3146	    vap->iv_opmode == IEEE80211_M_MBSS) {	/* NB: no IBSS support*/
3147		struct ieee80211_tim_ie *tie =
3148			(struct ieee80211_tim_ie *) bo->bo_tim;
3149		if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3150			u_int timlen, timoff, i;
3151			/*
3152			 * ATIM/DTIM needs updating.  If it fits in the
3153			 * current space allocated then just copy in the
3154			 * new bits.  Otherwise we need to move any trailing
3155			 * data to make room.  Note that we know there is
3156			 * contiguous space because ieee80211_beacon_allocate
3157			 * insures there is space in the mbuf to write a
3158			 * maximal-size virtual bitmap (based on iv_max_aid).
3159			 */
3160			/*
3161			 * Calculate the bitmap size and offset, copy any
3162			 * trailer out of the way, and then copy in the
3163			 * new bitmap and update the information element.
3164			 * Note that the tim bitmap must contain at least
3165			 * one byte and any offset must be even.
3166			 */
3167			if (vap->iv_ps_pending != 0) {
3168				timoff = 128;		/* impossibly large */
3169				for (i = 0; i < vap->iv_tim_len; i++)
3170					if (vap->iv_tim_bitmap[i]) {
3171						timoff = i &~ 1;
3172						break;
3173					}
3174				KASSERT(timoff != 128, ("tim bitmap empty!"));
3175				for (i = vap->iv_tim_len-1; i >= timoff; i--)
3176					if (vap->iv_tim_bitmap[i])
3177						break;
3178				timlen = 1 + (i - timoff);
3179			} else {
3180				timoff = 0;
3181				timlen = 1;
3182			}
3183			if (timlen != bo->bo_tim_len) {
3184				/* copy up/down trailer */
3185				int adjust = tie->tim_bitmap+timlen
3186					   - bo->bo_tim_trailer;
3187				ovbcopy(bo->bo_tim_trailer,
3188				    bo->bo_tim_trailer+adjust,
3189				    bo->bo_tim_trailer_len);
3190				bo->bo_tim_trailer += adjust;
3191				bo->bo_erp += adjust;
3192				bo->bo_htinfo += adjust;
3193#ifdef IEEE80211_SUPPORT_SUPERG
3194				bo->bo_ath += adjust;
3195#endif
3196#ifdef IEEE80211_SUPPORT_TDMA
3197				bo->bo_tdma += adjust;
3198#endif
3199#ifdef IEEE80211_SUPPORT_MESH
3200				bo->bo_meshconf += adjust;
3201#endif
3202				bo->bo_appie += adjust;
3203				bo->bo_wme += adjust;
3204				bo->bo_csa += adjust;
3205				bo->bo_quiet += adjust;
3206				bo->bo_tim_len = timlen;
3207
3208				/* update information element */
3209				tie->tim_len = 3 + timlen;
3210				tie->tim_bitctl = timoff;
3211				len_changed = 1;
3212			}
3213			memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3214				bo->bo_tim_len);
3215
3216			clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3217
3218			IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3219				"%s: TIM updated, pending %u, off %u, len %u\n",
3220				__func__, vap->iv_ps_pending, timoff, timlen);
3221		}
3222		/* count down DTIM period */
3223		if (tie->tim_count == 0)
3224			tie->tim_count = tie->tim_period - 1;
3225		else
3226			tie->tim_count--;
3227		/* update state for buffered multicast frames on DTIM */
3228		if (mcast && tie->tim_count == 0)
3229			tie->tim_bitctl |= 1;
3230		else
3231			tie->tim_bitctl &= ~1;
3232		if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3233			struct ieee80211_csa_ie *csa =
3234			    (struct ieee80211_csa_ie *) bo->bo_csa;
3235
3236			/*
3237			 * Insert or update CSA ie.  If we're just starting
3238			 * to count down to the channel switch then we need
3239			 * to insert the CSA ie.  Otherwise we just need to
3240			 * drop the count.  The actual change happens above
3241			 * when the vap's count reaches the target count.
3242			 */
3243			if (vap->iv_csa_count == 0) {
3244				memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3245				bo->bo_erp += sizeof(*csa);
3246				bo->bo_htinfo += sizeof(*csa);
3247				bo->bo_wme += sizeof(*csa);
3248#ifdef IEEE80211_SUPPORT_SUPERG
3249				bo->bo_ath += sizeof(*csa);
3250#endif
3251#ifdef IEEE80211_SUPPORT_TDMA
3252				bo->bo_tdma += sizeof(*csa);
3253#endif
3254#ifdef IEEE80211_SUPPORT_MESH
3255				bo->bo_meshconf += sizeof(*csa);
3256#endif
3257				bo->bo_appie += sizeof(*csa);
3258				bo->bo_csa_trailer_len += sizeof(*csa);
3259				bo->bo_quiet += sizeof(*csa);
3260				bo->bo_tim_trailer_len += sizeof(*csa);
3261				m->m_len += sizeof(*csa);
3262				m->m_pkthdr.len += sizeof(*csa);
3263
3264				ieee80211_add_csa(bo->bo_csa, vap);
3265			} else
3266				csa->csa_count--;
3267			vap->iv_csa_count++;
3268			/* NB: don't clear IEEE80211_BEACON_CSA */
3269		}
3270		if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3271		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3272			if (vap->iv_quiet)
3273				ieee80211_add_quiet(bo->bo_quiet, vap);
3274		}
3275		if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3276			/*
3277			 * ERP element needs updating.
3278			 */
3279			(void) ieee80211_add_erp(bo->bo_erp, ic);
3280			clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3281		}
3282#ifdef IEEE80211_SUPPORT_SUPERG
3283		if (isset(bo->bo_flags,  IEEE80211_BEACON_ATH)) {
3284			ieee80211_add_athcaps(bo->bo_ath, ni);
3285			clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3286		}
3287#endif
3288	}
3289	if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3290		const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3291		int aielen;
3292		uint8_t *frm;
3293
3294		aielen = 0;
3295		if (aie != NULL)
3296			aielen += aie->ie_len;
3297		if (aielen != bo->bo_appie_len) {
3298			/* copy up/down trailer */
3299			int adjust = aielen - bo->bo_appie_len;
3300			ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3301				bo->bo_tim_trailer_len);
3302			bo->bo_tim_trailer += adjust;
3303			bo->bo_appie += adjust;
3304			bo->bo_appie_len = aielen;
3305
3306			len_changed = 1;
3307		}
3308		frm = bo->bo_appie;
3309		if (aie != NULL)
3310			frm  = add_appie(frm, aie);
3311		clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3312	}
3313	IEEE80211_UNLOCK(ic);
3314
3315	return len_changed;
3316}
3317