2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/endian.h>
43 #include <sys/socket.h>
46 #include <net/ethernet.h>
48 #include <net/if_var.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_vlan_var.h>
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_regdomain.h>
55 #ifdef IEEE80211_SUPPORT_SUPERG
56 #include <net80211/ieee80211_superg.h>
58 #ifdef IEEE80211_SUPPORT_TDMA
59 #include <net80211/ieee80211_tdma.h>
61 #include <net80211/ieee80211_wds.h>
62 #include <net80211/ieee80211_mesh.h>
63 #include <net80211/ieee80211_vht.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
75 #include <netinet/ip6.h>
78 #include <security/mac/mac_framework.h>
80 #define ETHER_HEADER_COPY(dst, src) \
81 memcpy(dst, src, sizeof(struct ether_header))
83 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
84 u_int hdrsize, u_int ciphdrsize, u_int mtu);
85 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
87 #ifdef IEEE80211_DEBUG
89 * Decide if an outbound management frame should be
90 * printed when debugging is enabled. This filters some
91 * of the less interesting frames that come frequently
95 doprint(struct ieee80211vap *vap, int subtype)
98 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
99 return (vap->iv_opmode == IEEE80211_M_IBSS);
106 * Transmit a frame to the given destination on the given VAP.
108 * It's up to the caller to figure out the details of who this
109 * is going to and resolving the node.
111 * This routine takes care of queuing it for power save,
112 * A-MPDU state stuff, fast-frames state stuff, encapsulation
113 * if required, then passing it up to the driver layer.
115 * This routine (for now) consumes the mbuf and frees the node
116 * reference; it ideally will return a TX status which reflects
117 * whether the mbuf was consumed or not, so the caller can
118 * free the mbuf (if appropriate) and the node reference (again,
122 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
123 struct ieee80211_node *ni)
125 struct ieee80211com *ic = vap->iv_ic;
126 struct ifnet *ifp = vap->iv_ifp;
129 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
130 (m->m_flags & M_PWR_SAV) == 0) {
132 * Station in power save mode; pass the frame
133 * to the 802.11 layer and continue. We'll get
134 * the frame back when the time is right.
135 * XXX lose WDS vap linkage?
137 if (ieee80211_pwrsave(ni, m) != 0)
138 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
139 ieee80211_free_node(ni);
142 * We queued it fine, so tell the upper layer
143 * that we consumed it.
147 /* calculate priority so drivers can find the tx queue */
148 if (ieee80211_classify(ni, m)) {
149 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
150 ni->ni_macaddr, NULL,
151 "%s", "classification failure");
152 vap->iv_stats.is_tx_classify++;
153 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
155 ieee80211_free_node(ni);
157 /* XXX better status? */
161 * Stash the node pointer. Note that we do this after
162 * any call to ieee80211_dwds_mcast because that code
163 * uses any existing value for rcvif to identify the
164 * interface it (might have been) received on.
166 m->m_pkthdr.rcvif = (void *)ni;
167 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
169 BPF_MTAP(ifp, m); /* 802.3 tx */
172 * Check if A-MPDU tx aggregation is setup or if we
173 * should try to enable it. The sta must be associated
174 * with HT and A-MPDU enabled for use. When the policy
175 * routine decides we should enable A-MPDU we issue an
176 * ADDBA request and wait for a reply. The frame being
177 * encapsulated will go out w/o using A-MPDU, or possibly
178 * it might be collected by the driver and held/retransmit.
179 * The default ic_ampdu_enable routine handles staggering
180 * ADDBA requests in case the receiver NAK's us or we are
181 * otherwise unable to establish a BA stream.
183 * Don't treat group-addressed frames as candidates for aggregation;
184 * net80211 doesn't support 802.11aa-2012 and so group addressed
185 * frames will always have sequence numbers allocated from the NON_QOS
188 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
189 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
190 if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
191 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
192 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
194 ieee80211_txampdu_count_packet(tap);
195 if (IEEE80211_AMPDU_RUNNING(tap)) {
197 * Operational, mark frame for aggregation.
199 * XXX do tx aggregation here
201 m->m_flags |= M_AMPDU_MPDU;
202 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
203 ic->ic_ampdu_enable(ni, tap)) {
205 * Not negotiated yet, request service.
207 ieee80211_ampdu_request(ni, tap);
208 /* XXX hold frame for reply? */
213 #ifdef IEEE80211_SUPPORT_SUPERG
215 * Check for AMSDU/FF; queue for aggregation
217 * Note: we don't bother trying to do fast frames or
218 * A-MSDU encapsulation for 802.3 drivers. Now, we
219 * likely could do it for FF (because it's a magic
220 * atheros tunnel LLC type) but I don't think we're going
221 * to really need to. For A-MSDU we'd have to set the
222 * A-MSDU QoS bit in the wifi header, so we just plain
225 * Strictly speaking, we could actually /do/ A-MSDU / FF
226 * with A-MPDU together which for certain circumstances
227 * is beneficial (eg A-MSDU of TCK ACKs.) However,
228 * I'll ignore that for now so existing behaviour is maintained.
229 * Later on it would be good to make "amsdu + ampdu" configurable.
231 else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
232 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
233 m = ieee80211_amsdu_check(ni, m);
235 /* NB: any ni ref held on stageq */
236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
237 "%s: amsdu_check queued frame\n",
241 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
242 IEEE80211_NODE_FF)) {
243 m = ieee80211_ff_check(ni, m);
245 /* NB: any ni ref held on stageq */
246 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
247 "%s: ff_check queued frame\n",
253 #endif /* IEEE80211_SUPPORT_SUPERG */
256 * Grab the TX lock - serialise the TX process from this
257 * point (where TX state is being checked/modified)
258 * through to driver queue.
260 IEEE80211_TX_LOCK(ic);
263 * XXX make the encap and transmit code a separate function
264 * so things like the FF (and later A-MSDU) path can just call
265 * it for flushed frames.
267 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
269 * Encapsulate the packet in prep for transmission.
271 m = ieee80211_encap(vap, ni, m);
273 /* NB: stat+msg handled in ieee80211_encap */
274 IEEE80211_TX_UNLOCK(ic);
275 ieee80211_free_node(ni);
276 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
280 (void) ieee80211_parent_xmitpkt(ic, m);
283 * Unlock at this point - no need to hold it across
284 * ieee80211_free_node() (ie, the comlock)
286 IEEE80211_TX_UNLOCK(ic);
287 ic->ic_lastdata = ticks;
295 * Send the given mbuf through the given vap.
297 * This consumes the mbuf regardless of whether the transmit
298 * was successful or not.
300 * This does none of the initial checks that ieee80211_start()
301 * does (eg CAC timeout, interface wakeup) - the caller must
305 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
307 #define IS_DWDS(vap) \
308 (vap->iv_opmode == IEEE80211_M_WDS && \
309 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
310 struct ieee80211com *ic = vap->iv_ic;
311 struct ifnet *ifp = vap->iv_ifp;
312 struct ieee80211_node *ni;
313 struct ether_header *eh;
316 * Cancel any background scan.
318 if (ic->ic_flags & IEEE80211_F_SCAN)
319 ieee80211_cancel_anyscan(vap);
321 * Find the node for the destination so we can do
322 * things like power save and fast frames aggregation.
324 * NB: past this point various code assumes the first
325 * mbuf has the 802.3 header present (and contiguous).
328 if (m->m_len < sizeof(struct ether_header) &&
329 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
330 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
331 "discard frame, %s\n", "m_pullup failed");
332 vap->iv_stats.is_tx_nobuf++; /* XXX */
333 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
336 eh = mtod(m, struct ether_header *);
337 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
340 * Only unicast frames from the above go out
341 * DWDS vaps; multicast frames are handled by
342 * dispatching the frame as it comes through
343 * the AP vap (see below).
345 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
346 eh->ether_dhost, "mcast", "%s", "on DWDS");
347 vap->iv_stats.is_dwds_mcast++;
349 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
350 /* XXX better status? */
353 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
355 * Spam DWDS vap's w/ multicast traffic.
357 /* XXX only if dwds in use? */
358 ieee80211_dwds_mcast(vap, m);
361 #ifdef IEEE80211_SUPPORT_MESH
362 if (vap->iv_opmode != IEEE80211_M_MBSS) {
364 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
366 /* NB: ieee80211_find_txnode does stat+msg */
367 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
369 /* XXX better status? */
372 if (ni->ni_associd == 0 &&
373 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
374 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
375 eh->ether_dhost, NULL,
376 "sta not associated (type 0x%04x)",
377 htons(eh->ether_type));
378 vap->iv_stats.is_tx_notassoc++;
379 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
381 ieee80211_free_node(ni);
382 /* XXX better status? */
385 #ifdef IEEE80211_SUPPORT_MESH
387 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
389 * Proxy station only if configured.
391 if (!ieee80211_mesh_isproxyena(vap)) {
392 IEEE80211_DISCARD_MAC(vap,
393 IEEE80211_MSG_OUTPUT |
395 eh->ether_dhost, NULL,
396 "%s", "proxy not enabled");
397 vap->iv_stats.is_mesh_notproxy++;
398 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
400 /* XXX better status? */
403 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
404 "forward frame from DS SA(%6D), DA(%6D)\n",
405 eh->ether_shost, ":",
406 eh->ether_dhost, ":");
407 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
409 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
412 * NB: ieee80211_mesh_discover holds/disposes
413 * frame (e.g. queueing on path discovery).
415 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
416 /* XXX better status? */
423 * We've resolved the sender, so attempt to transmit it.
426 if (vap->iv_state == IEEE80211_S_SLEEP) {
428 * In power save; queue frame and then wakeup device
431 ic->ic_lastdata = ticks;
432 if (ieee80211_pwrsave(ni, m) != 0)
433 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
434 ieee80211_free_node(ni);
435 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
439 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
446 * Start method for vap's. All packets from the stack come
447 * through here. We handle common processing of the packets
448 * before dispatching them to the underlying device.
450 * if_transmit() requires that the mbuf be consumed by this call
451 * regardless of the return condition.
454 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
456 struct ieee80211vap *vap = ifp->if_softc;
457 struct ieee80211com *ic = vap->iv_ic;
460 * No data frames go out unless we're running.
461 * Note in particular this covers CAC and CSA
462 * states (though maybe we should check muting
465 if (vap->iv_state != IEEE80211_S_RUN &&
466 vap->iv_state != IEEE80211_S_SLEEP) {
468 /* re-check under the com lock to avoid races */
469 if (vap->iv_state != IEEE80211_S_RUN &&
470 vap->iv_state != IEEE80211_S_SLEEP) {
471 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
472 "%s: ignore queue, in %s state\n",
473 __func__, ieee80211_state_name[vap->iv_state]);
474 vap->iv_stats.is_tx_badstate++;
475 IEEE80211_UNLOCK(ic);
476 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
478 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
481 IEEE80211_UNLOCK(ic);
485 * Sanitize mbuf flags for net80211 use. We cannot
486 * clear M_PWR_SAV or M_MORE_DATA because these may
487 * be set for frames that are re-submitted from the
490 * NB: This must be done before ieee80211_classify as
491 * it marks EAPOL in frames with M_EAPOL.
493 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
496 * Bump to the packet transmission path.
497 * The mbuf will be consumed here.
499 return (ieee80211_start_pkt(vap, m));
503 ieee80211_vap_qflush(struct ifnet *ifp)
510 * 802.11 raw output routine.
512 * XXX TODO: this (and other send routines) should correctly
513 * XXX keep the pwr mgmt bit set if it decides to call into the
514 * XXX driver to send a frame whilst the state is SLEEP.
516 * Otherwise the peer may decide that we're awake and flood us
517 * with traffic we are still too asleep to receive!
520 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
521 struct mbuf *m, const struct ieee80211_bpf_params *params)
523 struct ieee80211com *ic = vap->iv_ic;
527 * Set node - the caller has taken a reference, so ensure
528 * that the mbuf has the same node value that
529 * it would if it were going via the normal path.
531 m->m_pkthdr.rcvif = (void *)ni;
534 * Attempt to add bpf transmit parameters.
536 * For now it's ok to fail; the raw_xmit api still takes
539 * Later on when ic_raw_xmit() has params removed,
540 * they'll have to be added - so fail the transmit if
544 (void) ieee80211_add_xmit_params(m, params);
546 error = ic->ic_raw_xmit(ni, m, params);
548 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
549 ieee80211_free_node(ni);
555 ieee80211_validate_frame(struct mbuf *m,
556 const struct ieee80211_bpf_params *params)
558 struct ieee80211_frame *wh;
561 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
564 wh = mtod(m, struct ieee80211_frame *);
565 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
566 IEEE80211_FC0_VERSION_0)
569 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
570 if (type != IEEE80211_FC0_TYPE_DATA) {
571 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
572 IEEE80211_FC1_DIR_NODS)
575 if (type != IEEE80211_FC0_TYPE_MGT &&
576 (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
579 /* XXX skip other field checks? */
582 if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
583 (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0) {
586 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
589 * See IEEE Std 802.11-2012,
590 * 8.2.4.1.9 'Protected Frame field'
592 /* XXX no support for robust management frames yet. */
593 if (!(type == IEEE80211_FC0_TYPE_DATA ||
594 (type == IEEE80211_FC0_TYPE_MGT &&
595 subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
598 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
601 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
608 ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
610 struct ieee80211com *ic = ni->ni_ic;
612 if (IEEE80211_IS_HT_RATE(rate)) {
613 if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
616 rate = IEEE80211_RV(rate);
618 if (rate > ic->ic_txstream * 8 - 1)
625 if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
631 if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
634 switch (ic->ic_txstream) {
657 if (!ieee80211_isratevalid(ic->ic_rt, rate))
664 ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
665 const struct ieee80211_bpf_params *params)
670 return (0); /* nothing to do */
672 /* NB: most drivers assume that ibp_rate0 is set (!= 0). */
673 if (params->ibp_rate0 != 0) {
674 error = ieee80211_validate_rate(ni, params->ibp_rate0);
678 /* XXX pre-setup some default (e.g., mgmt / mcast) rate */
683 if (params->ibp_rate1 != 0 &&
684 (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
687 if (params->ibp_rate2 != 0 &&
688 (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
691 if (params->ibp_rate3 != 0 &&
692 (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
699 * 802.11 output routine. This is (currently) used only to
700 * connect bpf write calls to the 802.11 layer for injecting
704 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
705 const struct sockaddr *dst, struct route *ro)
707 #define senderr(e) do { error = (e); goto bad;} while (0)
708 const struct ieee80211_bpf_params *params = NULL;
709 struct ieee80211_node *ni = NULL;
710 struct ieee80211vap *vap;
711 struct ieee80211_frame *wh;
712 struct ieee80211com *ic = NULL;
716 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
718 * Short-circuit requests if the vap is marked OACTIVE
719 * as this can happen because a packet came down through
720 * ieee80211_start before the vap entered RUN state in
721 * which case it's ok to just drop the frame. This
722 * should not be necessary but callers of if_output don't
730 * Hand to the 802.3 code if not tagged as
731 * a raw 802.11 frame.
733 if (dst->sa_family != AF_IEEE80211)
734 return vap->iv_output(ifp, m, dst, ro);
736 error = mac_ifnet_check_transmit(ifp, m);
740 if (ifp->if_flags & IFF_MONITOR)
742 if (!IFNET_IS_UP_RUNNING(ifp))
744 if (vap->iv_state == IEEE80211_S_CAC) {
745 IEEE80211_DPRINTF(vap,
746 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
747 "block %s frame in CAC state\n", "raw data");
748 vap->iv_stats.is_tx_badstate++;
749 senderr(EIO); /* XXX */
750 } else if (vap->iv_state == IEEE80211_S_SCAN)
752 /* XXX bypass bridge, pfil, carp, etc. */
755 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
756 * present by setting the sa_len field of the sockaddr (yes,
758 * NB: we assume sa_data is suitably aligned to cast.
760 if (dst->sa_len != 0)
761 params = (const struct ieee80211_bpf_params *)dst->sa_data;
763 error = ieee80211_validate_frame(m, params);
767 wh = mtod(m, struct ieee80211_frame *);
769 /* locate destination node */
770 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
771 case IEEE80211_FC1_DIR_NODS:
772 case IEEE80211_FC1_DIR_FROMDS:
773 ni = ieee80211_find_txnode(vap, wh->i_addr1);
775 case IEEE80211_FC1_DIR_TODS:
776 case IEEE80211_FC1_DIR_DSTODS:
777 ni = ieee80211_find_txnode(vap, wh->i_addr3);
784 * Permit packets w/ bpf params through regardless
785 * (see below about sa_len).
787 if (dst->sa_len == 0)
788 senderr(EHOSTUNREACH);
789 ni = ieee80211_ref_node(vap->iv_bss);
793 * Sanitize mbuf for net80211 flags leaked from above.
795 * NB: This must be done before ieee80211_classify as
796 * it marks EAPOL in frames with M_EAPOL.
798 m->m_flags &= ~M_80211_TX;
799 m->m_flags |= M_ENCAP; /* mark encapsulated */
801 if (IEEE80211_IS_DATA(wh)) {
802 /* calculate priority so drivers can find the tx queue */
803 if (ieee80211_classify(ni, m))
804 senderr(EIO); /* XXX */
806 /* NB: ieee80211_encap does not include 802.11 header */
807 IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
808 m->m_pkthdr.len - ieee80211_hdrsize(wh));
810 M_WME_SETAC(m, WME_AC_BE);
812 error = ieee80211_sanitize_rates(ni, m, params);
816 IEEE80211_NODE_STAT(ni, tx_data);
817 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
818 IEEE80211_NODE_STAT(ni, tx_mcast);
819 m->m_flags |= M_MCAST;
821 IEEE80211_NODE_STAT(ni, tx_ucast);
823 IEEE80211_TX_LOCK(ic);
824 ret = ieee80211_raw_output(vap, ni, m, params);
825 IEEE80211_TX_UNLOCK(ic);
831 ieee80211_free_node(ni);
832 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
838 * Set the direction field and address fields of an outgoing
839 * frame. Note this should be called early on in constructing
840 * a frame as it sets i_fc[1]; other bits can then be or'd in.
843 ieee80211_send_setup(
844 struct ieee80211_node *ni,
847 const uint8_t sa[IEEE80211_ADDR_LEN],
848 const uint8_t da[IEEE80211_ADDR_LEN],
849 const uint8_t bssid[IEEE80211_ADDR_LEN])
851 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
852 struct ieee80211vap *vap = ni->ni_vap;
853 struct ieee80211_tx_ampdu *tap;
854 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
857 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
859 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
860 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
861 switch (vap->iv_opmode) {
862 case IEEE80211_M_STA:
863 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
864 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
865 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
866 IEEE80211_ADDR_COPY(wh->i_addr3, da);
868 case IEEE80211_M_IBSS:
869 case IEEE80211_M_AHDEMO:
870 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
871 IEEE80211_ADDR_COPY(wh->i_addr1, da);
872 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
873 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
875 case IEEE80211_M_HOSTAP:
876 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
877 IEEE80211_ADDR_COPY(wh->i_addr1, da);
878 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
879 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
881 case IEEE80211_M_WDS:
882 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
883 IEEE80211_ADDR_COPY(wh->i_addr1, da);
884 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
885 IEEE80211_ADDR_COPY(wh->i_addr3, da);
886 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
888 case IEEE80211_M_MBSS:
889 #ifdef IEEE80211_SUPPORT_MESH
890 if (IEEE80211_IS_MULTICAST(da)) {
891 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
893 IEEE80211_ADDR_COPY(wh->i_addr1, da);
894 IEEE80211_ADDR_COPY(wh->i_addr2,
897 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
898 IEEE80211_ADDR_COPY(wh->i_addr1, da);
899 IEEE80211_ADDR_COPY(wh->i_addr2,
901 IEEE80211_ADDR_COPY(wh->i_addr3, da);
902 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
906 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
910 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
911 IEEE80211_ADDR_COPY(wh->i_addr1, da);
912 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
913 #ifdef IEEE80211_SUPPORT_MESH
914 if (vap->iv_opmode == IEEE80211_M_MBSS)
915 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
918 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
920 *(uint16_t *)&wh->i_dur[0] = 0;
923 * XXX TODO: this is what the TX lock is for.
924 * Here we're incrementing sequence numbers, and they
925 * need to be in lock-step with what the driver is doing
926 * both in TX ordering and crypto encap (IV increment.)
928 * If the driver does seqno itself, then we can skip
929 * assigning sequence numbers here, and we can avoid
930 * requiring the TX lock.
932 tap = &ni->ni_tx_ampdu[tid];
933 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
934 m->m_flags |= M_AMPDU_MPDU;
936 /* NB: zero out i_seq field (for s/w encryption etc) */
937 *(uint16_t *)&wh->i_seq[0] = 0;
939 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
940 type & IEEE80211_FC0_SUBTYPE_MASK))
942 * 802.11-2012 9.3.2.10 - QoS multicast frames
943 * come out of a different seqno space.
945 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
946 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
948 seqno = ni->ni_txseqs[tid]++;
953 *(uint16_t *)&wh->i_seq[0] =
954 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
955 M_SEQNO_SET(m, seqno);
958 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
959 m->m_flags |= M_MCAST;
964 * Send a management frame to the specified node. The node pointer
965 * must have a reference as the pointer will be passed to the driver
966 * and potentially held for a long time. If the frame is successfully
967 * dispatched to the driver, then it is responsible for freeing the
968 * reference (and potentially free'ing up any associated storage);
969 * otherwise deal with reclaiming any reference (on error).
972 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
973 struct ieee80211_bpf_params *params)
975 struct ieee80211vap *vap = ni->ni_vap;
976 struct ieee80211com *ic = ni->ni_ic;
977 struct ieee80211_frame *wh;
980 KASSERT(ni != NULL, ("null node"));
982 if (vap->iv_state == IEEE80211_S_CAC) {
983 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
984 ni, "block %s frame in CAC state",
985 ieee80211_mgt_subtype_name(type));
986 vap->iv_stats.is_tx_badstate++;
987 ieee80211_free_node(ni);
989 return EIO; /* XXX */
992 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
994 ieee80211_free_node(ni);
998 IEEE80211_TX_LOCK(ic);
1000 wh = mtod(m, struct ieee80211_frame *);
1001 ieee80211_send_setup(ni, m,
1002 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1003 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1004 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1005 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1006 "encrypting frame (%s)", __func__);
1007 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1009 m->m_flags |= M_ENCAP; /* mark encapsulated */
1011 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
1012 M_WME_SETAC(m, params->ibp_pri);
1014 #ifdef IEEE80211_DEBUG
1015 /* avoid printing too many frames */
1016 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1017 ieee80211_msg_dumppkts(vap)) {
1018 printf("[%s] send %s on channel %u\n",
1019 ether_sprintf(wh->i_addr1),
1020 ieee80211_mgt_subtype_name(type),
1021 ieee80211_chan2ieee(ic, ic->ic_curchan));
1024 IEEE80211_NODE_STAT(ni, tx_mgmt);
1026 ret = ieee80211_raw_output(vap, ni, m, params);
1027 IEEE80211_TX_UNLOCK(ic);
1032 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
1035 struct ieee80211vap *vap = ni->ni_vap;
1041 * Send a null data frame to the specified node. If the station
1042 * is setup for QoS then a QoS Null Data frame is constructed.
1043 * If this is a WDS station then a 4-address frame is constructed.
1045 * NB: the caller is assumed to have setup a node reference
1046 * for use; this is necessary to deal with a race condition
1047 * when probing for inactive stations. Like ieee80211_mgmt_output
1048 * we must cleanup any node reference on error; however we
1049 * can safely just unref it as we know it will never be the
1050 * last reference to the node.
1053 ieee80211_send_nulldata(struct ieee80211_node *ni)
1055 struct ieee80211vap *vap = ni->ni_vap;
1056 struct ieee80211com *ic = ni->ni_ic;
1058 struct ieee80211_frame *wh;
1063 if (vap->iv_state == IEEE80211_S_CAC) {
1064 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1065 ni, "block %s frame in CAC state", "null data");
1066 ieee80211_unref_node(&ni);
1067 vap->iv_stats.is_tx_badstate++;
1068 return EIO; /* XXX */
1071 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1072 hdrlen = sizeof(struct ieee80211_qosframe);
1074 hdrlen = sizeof(struct ieee80211_frame);
1075 /* NB: only WDS vap's get 4-address frames */
1076 if (vap->iv_opmode == IEEE80211_M_WDS)
1077 hdrlen += IEEE80211_ADDR_LEN;
1078 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1079 hdrlen = roundup(hdrlen, sizeof(uint32_t));
1081 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1084 ieee80211_unref_node(&ni);
1085 vap->iv_stats.is_tx_nobuf++;
1088 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1089 ("leading space %zd", M_LEADINGSPACE(m)));
1090 M_PREPEND(m, hdrlen, M_NOWAIT);
1092 /* NB: cannot happen */
1093 ieee80211_free_node(ni);
1097 IEEE80211_TX_LOCK(ic);
1099 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
1100 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1101 const int tid = WME_AC_TO_TID(WME_AC_BE);
1104 ieee80211_send_setup(ni, m,
1105 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1106 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1108 if (vap->iv_opmode == IEEE80211_M_WDS)
1109 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1111 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1112 qos[0] = tid & IEEE80211_QOS_TID;
1113 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1114 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1117 ieee80211_send_setup(ni, m,
1118 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1119 IEEE80211_NONQOS_TID,
1120 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1122 if (vap->iv_opmode != IEEE80211_M_WDS) {
1123 /* NB: power management bit is never sent by an AP */
1124 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1125 vap->iv_opmode != IEEE80211_M_HOSTAP)
1126 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1128 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1129 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1130 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1133 m->m_len = m->m_pkthdr.len = hdrlen;
1134 m->m_flags |= M_ENCAP; /* mark encapsulated */
1136 M_WME_SETAC(m, WME_AC_BE);
1138 IEEE80211_NODE_STAT(ni, tx_data);
1140 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1141 "send %snull data frame on channel %u, pwr mgt %s",
1142 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1143 ieee80211_chan2ieee(ic, ic->ic_curchan),
1144 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1146 ret = ieee80211_raw_output(vap, ni, m, NULL);
1147 IEEE80211_TX_UNLOCK(ic);
1152 * Assign priority to a frame based on any vlan tag assigned
1153 * to the station and/or any Diffserv setting in an IP header.
1154 * Finally, if an ACM policy is setup (in station mode) it's
1158 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1160 const struct ether_header *eh = NULL;
1161 uint16_t ether_type;
1162 int v_wme_ac, d_wme_ac, ac;
1164 if (__predict_false(m->m_flags & M_ENCAP)) {
1165 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1167 int hdrlen, subtype;
1169 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1170 if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1175 hdrlen = ieee80211_hdrsize(wh);
1176 if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1179 llc = (struct llc *)mtodo(m, hdrlen);
1180 if (llc->llc_dsap != LLC_SNAP_LSAP ||
1181 llc->llc_ssap != LLC_SNAP_LSAP ||
1182 llc->llc_control != LLC_UI ||
1183 llc->llc_snap.org_code[0] != 0 ||
1184 llc->llc_snap.org_code[1] != 0 ||
1185 llc->llc_snap.org_code[2] != 0)
1188 ether_type = llc->llc_snap.ether_type;
1190 eh = mtod(m, struct ether_header *);
1191 ether_type = eh->ether_type;
1195 * Always promote PAE/EAPOL frames to high priority.
1197 if (ether_type == htons(ETHERTYPE_PAE)) {
1198 /* NB: mark so others don't need to check header */
1199 m->m_flags |= M_EAPOL;
1204 * Non-qos traffic goes to BE.
1206 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1212 * If node has a vlan tag then all traffic
1213 * to it must have a matching tag.
1216 if (ni->ni_vlan != 0) {
1217 if ((m->m_flags & M_VLANTAG) == 0) {
1218 IEEE80211_NODE_STAT(ni, tx_novlantag);
1221 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1222 EVL_VLANOFTAG(ni->ni_vlan)) {
1223 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1226 /* map vlan priority to AC */
1227 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1230 /* XXX m_copydata may be too slow for fast path */
1232 if (eh && eh->ether_type == htons(ETHERTYPE_IP)) {
1235 * IP frame, map the DSCP bits from the TOS field.
1237 /* NB: ip header may not be in first mbuf */
1238 m_copydata(m, sizeof(struct ether_header) +
1239 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1240 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1241 d_wme_ac = TID_TO_WME_AC(tos);
1245 if (eh && eh->ether_type == htons(ETHERTYPE_IPV6)) {
1249 * IPv6 frame, map the DSCP bits from the traffic class field.
1251 m_copydata(m, sizeof(struct ether_header) +
1252 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1254 tos = (uint8_t)(ntohl(flow) >> 20);
1255 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1256 d_wme_ac = TID_TO_WME_AC(tos);
1259 d_wme_ac = WME_AC_BE;
1267 * Use highest priority AC.
1269 if (v_wme_ac > d_wme_ac)
1277 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1278 static const int acmap[4] = {
1279 WME_AC_BK, /* WME_AC_BE */
1280 WME_AC_BK, /* WME_AC_BK */
1281 WME_AC_BE, /* WME_AC_VI */
1282 WME_AC_VI, /* WME_AC_VO */
1284 struct ieee80211com *ic = ni->ni_ic;
1286 while (ac != WME_AC_BK &&
1287 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1296 * Insure there is sufficient contiguous space to encapsulate the
1297 * 802.11 data frame. If room isn't already there, arrange for it.
1298 * Drivers and cipher modules assume we have done the necessary work
1299 * and fail rudely if they don't find the space they need.
1302 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1303 struct ieee80211_key *key, struct mbuf *m)
1305 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1306 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1309 /* XXX belongs in crypto code? */
1310 needed_space += key->wk_cipher->ic_header;
1313 * When crypto is being done in the host we must insure
1314 * the data are writable for the cipher routines; clone
1315 * a writable mbuf chain.
1316 * XXX handle SWMIC specially
1318 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1319 m = m_unshare(m, M_NOWAIT);
1321 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1322 "%s: cannot get writable mbuf\n", __func__);
1323 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1329 * We know we are called just before stripping an Ethernet
1330 * header and prepending an LLC header. This means we know
1332 * sizeof(struct ether_header) - sizeof(struct llc)
1333 * bytes recovered to which we need additional space for the
1334 * 802.11 header and any crypto header.
1336 /* XXX check trailing space and copy instead? */
1337 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1338 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1340 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1341 "%s: cannot expand storage\n", __func__);
1342 vap->iv_stats.is_tx_nobuf++;
1346 KASSERT(needed_space <= MHLEN,
1347 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1349 * Setup new mbuf to have leading space to prepend the
1350 * 802.11 header and any crypto header bits that are
1351 * required (the latter are added when the driver calls
1352 * back to ieee80211_crypto_encap to do crypto encapsulation).
1354 /* NB: must be first 'cuz it clobbers m_data */
1355 m_move_pkthdr(n, m);
1356 n->m_len = 0; /* NB: m_gethdr does not set */
1357 n->m_data += needed_space;
1359 * Pull up Ethernet header to create the expected layout.
1360 * We could use m_pullup but that's overkill (i.e. we don't
1361 * need the actual data) and it cannot fail so do it inline
1364 /* NB: struct ether_header is known to be contiguous */
1365 n->m_len += sizeof(struct ether_header);
1366 m->m_len -= sizeof(struct ether_header);
1367 m->m_data += sizeof(struct ether_header);
1369 * Replace the head of the chain.
1375 #undef TO_BE_RECLAIMED
1379 * Return the transmit key to use in sending a unicast frame.
1380 * If a unicast key is set we use that. When no unicast key is set
1381 * we fall back to the default transmit key.
1383 static __inline struct ieee80211_key *
1384 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1385 struct ieee80211_node *ni)
1387 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1388 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1389 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1391 return &vap->iv_nw_keys[vap->iv_def_txkey];
1393 return &ni->ni_ucastkey;
1398 * Return the transmit key to use in sending a multicast frame.
1399 * Multicast traffic always uses the group key which is installed as
1400 * the default tx key.
1402 static __inline struct ieee80211_key *
1403 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1404 struct ieee80211_node *ni)
1406 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1407 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1409 return &vap->iv_nw_keys[vap->iv_def_txkey];
1413 * Encapsulate an outbound data frame. The mbuf chain is updated.
1414 * If an error is encountered NULL is returned. The caller is required
1415 * to provide a node reference and pullup the ethernet header in the
1418 * NB: Packet is assumed to be processed by ieee80211_classify which
1419 * marked EAPOL frames w/ M_EAPOL.
1422 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1425 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1426 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1427 struct ieee80211com *ic = ni->ni_ic;
1428 #ifdef IEEE80211_SUPPORT_MESH
1429 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1430 struct ieee80211_meshcntl_ae10 *mc;
1431 struct ieee80211_mesh_route *rt = NULL;
1434 struct ether_header eh;
1435 struct ieee80211_frame *wh;
1436 struct ieee80211_key *key;
1438 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
1439 ieee80211_seq seqno;
1440 int meshhdrsize, meshae;
1444 IEEE80211_TX_LOCK_ASSERT(ic);
1446 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
1449 * Copy existing Ethernet header to a safe place. The
1450 * rest of the code assumes it's ok to strip it when
1451 * reorganizing state for the final encapsulation.
1453 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1454 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1457 * Insure space for additional headers. First identify
1458 * transmit key to use in calculating any buffer adjustments
1459 * required. This is also used below to do privacy
1460 * encapsulation work. Then calculate the 802.11 header
1461 * size and any padding required by the driver.
1463 * Note key may be NULL if we fall back to the default
1464 * transmit key and that is not set. In that case the
1465 * buffer may not be expanded as needed by the cipher
1466 * routines, but they will/should discard it.
1468 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1469 if (vap->iv_opmode == IEEE80211_M_STA ||
1470 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1471 (vap->iv_opmode == IEEE80211_M_WDS &&
1472 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1473 key = ieee80211_crypto_getucastkey(vap, ni);
1475 key = ieee80211_crypto_getmcastkey(vap, ni);
1476 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1477 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1479 "no default transmit key (%s) deftxkey %u",
1480 __func__, vap->iv_def_txkey);
1481 vap->iv_stats.is_tx_nodefkey++;
1487 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1488 * frames so suppress use. This may be an issue if other
1489 * ap's require all data frames to be QoS-encapsulated
1490 * once negotiated in which case we'll need to make this
1493 * Don't send multicast QoS frames.
1494 * Technically multicast frames can be QoS if all stations in the
1497 * NB: mesh data frames are QoS, including multicast frames.
1500 (((is_mcast == 0) && (ni->ni_flags &
1501 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
1502 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1503 (m->m_flags & M_EAPOL) == 0;
1506 hdrsize = sizeof(struct ieee80211_qosframe);
1508 hdrsize = sizeof(struct ieee80211_frame);
1509 #ifdef IEEE80211_SUPPORT_MESH
1510 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1512 * Mesh data frames are encapsulated according to the
1513 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1514 * o Group Addressed data (aka multicast) originating
1515 * at the local sta are sent w/ 3-address format and
1516 * address extension mode 00
1517 * o Individually Addressed data (aka unicast) originating
1518 * at the local sta are sent w/ 4-address format and
1519 * address extension mode 00
1520 * o Group Addressed data forwarded from a non-mesh sta are
1521 * sent w/ 3-address format and address extension mode 01
1522 * o Individually Address data from another sta are sent
1523 * w/ 4-address format and address extension mode 10
1525 is4addr = 0; /* NB: don't use, disable */
1526 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1527 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1528 KASSERT(rt != NULL, ("route is NULL"));
1529 dir = IEEE80211_FC1_DIR_DSTODS;
1530 hdrsize += IEEE80211_ADDR_LEN;
1531 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1532 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1534 IEEE80211_NOTE_MAC(vap,
1537 "%s", "trying to send to ourself");
1540 meshae = IEEE80211_MESH_AE_10;
1542 sizeof(struct ieee80211_meshcntl_ae10);
1544 meshae = IEEE80211_MESH_AE_00;
1546 sizeof(struct ieee80211_meshcntl);
1549 dir = IEEE80211_FC1_DIR_FROMDS;
1550 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1552 meshae = IEEE80211_MESH_AE_01;
1554 sizeof(struct ieee80211_meshcntl_ae01);
1557 meshae = IEEE80211_MESH_AE_00;
1558 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1564 * 4-address frames need to be generated for:
1565 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1566 * o packets sent through a vap marked for relaying
1567 * (e.g. a station operating with dynamic WDS)
1569 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1570 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1571 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1573 hdrsize += IEEE80211_ADDR_LEN;
1574 meshhdrsize = meshae = 0;
1575 #ifdef IEEE80211_SUPPORT_MESH
1579 * Honor driver DATAPAD requirement.
1581 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1582 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1586 if (__predict_true((m->m_flags & M_FF) == 0)) {
1590 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1592 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1595 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1596 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1597 llc = mtod(m, struct llc *);
1598 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1599 llc->llc_control = LLC_UI;
1600 llc->llc_snap.org_code[0] = 0;
1601 llc->llc_snap.org_code[1] = 0;
1602 llc->llc_snap.org_code[2] = 0;
1603 llc->llc_snap.ether_type = eh.ether_type;
1605 #ifdef IEEE80211_SUPPORT_SUPERG
1607 * Aggregated frame. Check if it's for AMSDU or FF.
1609 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1610 * anywhere for some reason. But, since 11n requires
1611 * AMSDU RX, we can just assume "11n" == "AMSDU".
1613 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1614 if (ieee80211_amsdu_tx_ok(ni)) {
1615 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1618 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1624 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1626 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1628 vap->iv_stats.is_tx_nobuf++;
1631 wh = mtod(m, struct ieee80211_frame *);
1632 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1633 *(uint16_t *)wh->i_dur = 0;
1634 qos = NULL; /* NB: quiet compiler */
1636 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1637 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1638 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1639 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1640 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1641 } else switch (vap->iv_opmode) {
1642 case IEEE80211_M_STA:
1643 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1644 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1645 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1646 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1648 case IEEE80211_M_IBSS:
1649 case IEEE80211_M_AHDEMO:
1650 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1651 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1652 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1654 * NB: always use the bssid from iv_bss as the
1655 * neighbor's may be stale after an ibss merge
1657 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1659 case IEEE80211_M_HOSTAP:
1660 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1661 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1662 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1663 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1665 #ifdef IEEE80211_SUPPORT_MESH
1666 case IEEE80211_M_MBSS:
1667 /* NB: offset by hdrspace to deal with DATAPAD */
1668 mc = (struct ieee80211_meshcntl_ae10 *)
1669 (mtod(m, uint8_t *) + hdrspace);
1672 case IEEE80211_MESH_AE_00: /* no proxy */
1674 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1675 IEEE80211_ADDR_COPY(wh->i_addr1,
1677 IEEE80211_ADDR_COPY(wh->i_addr2,
1679 IEEE80211_ADDR_COPY(wh->i_addr3,
1681 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1683 qos =((struct ieee80211_qosframe_addr4 *)
1685 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1687 IEEE80211_ADDR_COPY(wh->i_addr1,
1689 IEEE80211_ADDR_COPY(wh->i_addr2,
1691 IEEE80211_ADDR_COPY(wh->i_addr3,
1693 qos = ((struct ieee80211_qosframe *)
1697 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1698 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1699 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1700 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1701 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1703 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1705 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1707 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1708 KASSERT(rt != NULL, ("route is NULL"));
1709 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1710 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1711 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1712 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1713 mc->mc_flags = IEEE80211_MESH_AE_10;
1714 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1715 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1716 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1719 KASSERT(0, ("meshae %d", meshae));
1722 mc->mc_ttl = ms->ms_ttl;
1724 le32enc(mc->mc_seq, ms->ms_seq);
1727 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1731 if (m->m_flags & M_MORE_DATA)
1732 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1737 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1738 /* NB: mesh case handled earlier */
1739 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1740 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1741 ac = M_WME_GETAC(m);
1742 /* map from access class/queue to 11e header priorty value */
1743 tid = WME_AC_TO_TID(ac);
1744 qos[0] = tid & IEEE80211_QOS_TID;
1745 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1746 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1747 #ifdef IEEE80211_SUPPORT_MESH
1748 if (vap->iv_opmode == IEEE80211_M_MBSS)
1749 qos[1] = IEEE80211_QOS_MC;
1753 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1756 * If this is an A-MSDU then ensure we set the
1760 qos[0] |= IEEE80211_QOS_AMSDU;
1763 * XXX TODO TX lock is needed for atomic updates of sequence
1764 * numbers. If the driver does it, then don't do it here;
1765 * and we don't need the TX lock held.
1767 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1769 * 802.11-2012 9.3.2.10 -
1771 * If this is a multicast frame then we need
1772 * to ensure that the sequence number comes from
1773 * a separate seqno space and not the TID space.
1775 * Otherwise multicast frames may actually cause
1776 * holes in the TX blockack window space and
1777 * upset various things.
1779 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1780 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1782 seqno = ni->ni_txseqs[tid]++;
1785 * NB: don't assign a sequence # to potential
1786 * aggregates; we expect this happens at the
1787 * point the frame comes off any aggregation q
1788 * as otherwise we may introduce holes in the
1789 * BA sequence space and/or make window accouting
1792 * XXX may want to control this with a driver
1793 * capability; this may also change when we pull
1794 * aggregation up into net80211
1796 *(uint16_t *)wh->i_seq =
1797 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1798 M_SEQNO_SET(m, seqno);
1800 /* NB: zero out i_seq field (for s/w encryption etc) */
1801 *(uint16_t *)wh->i_seq = 0;
1805 * XXX TODO TX lock is needed for atomic updates of sequence
1806 * numbers. If the driver does it, then don't do it here;
1807 * and we don't need the TX lock held.
1809 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1810 *(uint16_t *)wh->i_seq =
1811 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1812 M_SEQNO_SET(m, seqno);
1815 * XXX TODO: we shouldn't allow EAPOL, etc that would
1816 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1819 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1824 * Check if xmit fragmentation is required.
1826 * If the hardware does fragmentation offload, then don't bother
1829 if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
1832 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1833 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1834 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1835 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1839 * IEEE 802.1X: send EAPOL frames always in the clear.
1840 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1842 if ((m->m_flags & M_EAPOL) == 0 ||
1843 ((vap->iv_flags & IEEE80211_F_WPA) &&
1844 (vap->iv_opmode == IEEE80211_M_STA ?
1845 !IEEE80211_KEY_UNDEFINED(key) :
1846 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1847 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1848 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1849 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1851 "%s", "enmic failed, discard frame");
1852 vap->iv_stats.is_crypto_enmicfail++;
1857 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1858 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1861 m->m_flags |= M_ENCAP; /* mark encapsulated */
1863 IEEE80211_NODE_STAT(ni, tx_data);
1864 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1865 IEEE80211_NODE_STAT(ni, tx_mcast);
1866 m->m_flags |= M_MCAST;
1868 IEEE80211_NODE_STAT(ni, tx_ucast);
1869 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1881 ieee80211_free_mbuf(struct mbuf *m)
1889 next = m->m_nextpkt;
1890 m->m_nextpkt = NULL;
1892 } while ((m = next) != NULL);
1896 * Fragment the frame according to the specified mtu.
1897 * The size of the 802.11 header (w/o padding) is provided
1898 * so we don't need to recalculate it. We create a new
1899 * mbuf for each fragment and chain it through m_nextpkt;
1900 * we might be able to optimize this by reusing the original
1901 * packet's mbufs but that is significantly more complicated.
1904 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1905 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1907 struct ieee80211com *ic = vap->iv_ic;
1908 struct ieee80211_frame *wh, *whf;
1909 struct mbuf *m, *prev;
1910 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1913 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1914 KASSERT(m0->m_pkthdr.len > mtu,
1915 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1918 * Honor driver DATAPAD requirement.
1920 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1921 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1925 wh = mtod(m0, struct ieee80211_frame *);
1926 /* NB: mark the first frag; it will be propagated below */
1927 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1928 totalhdrsize = hdrspace + ciphdrsize;
1930 off = mtu - ciphdrsize;
1931 remainder = m0->m_pkthdr.len - off;
1934 fragsize = MIN(totalhdrsize + remainder, mtu);
1935 m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR);
1938 /* leave room to prepend any cipher header */
1939 m_align(m, fragsize - ciphdrsize);
1942 * Form the header in the fragment. Note that since
1943 * we mark the first fragment with the MORE_FRAG bit
1944 * it automatically is propagated to each fragment; we
1945 * need only clear it on the last fragment (done below).
1946 * NB: frag 1+ dont have Mesh Control field present.
1948 whf = mtod(m, struct ieee80211_frame *);
1949 memcpy(whf, wh, hdrsize);
1950 #ifdef IEEE80211_SUPPORT_MESH
1951 if (vap->iv_opmode == IEEE80211_M_MBSS)
1952 ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
1954 *(uint16_t *)&whf->i_seq[0] |= htole16(
1955 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1956 IEEE80211_SEQ_FRAG_SHIFT);
1959 payload = fragsize - totalhdrsize;
1960 /* NB: destination is known to be contiguous */
1962 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1963 m->m_len = hdrspace + payload;
1964 m->m_pkthdr.len = hdrspace + payload;
1965 m->m_flags |= M_FRAG;
1967 /* chain up the fragment */
1968 prev->m_nextpkt = m;
1971 /* deduct fragment just formed */
1972 remainder -= payload;
1974 } while (remainder != 0);
1976 /* set the last fragment */
1977 m->m_flags |= M_LASTFRAG;
1978 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1980 /* strip first mbuf now that everything has been copied */
1981 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1982 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1984 vap->iv_stats.is_tx_fragframes++;
1985 vap->iv_stats.is_tx_frags += fragno-1;
1989 /* reclaim fragments but leave original frame for caller to free */
1990 ieee80211_free_mbuf(m0->m_nextpkt);
1991 m0->m_nextpkt = NULL;
1996 * Add a supported rates element id to a frame.
1999 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
2003 *frm++ = IEEE80211_ELEMID_RATES;
2004 nrates = rs->rs_nrates;
2005 if (nrates > IEEE80211_RATE_SIZE)
2006 nrates = IEEE80211_RATE_SIZE;
2008 memcpy(frm, rs->rs_rates, nrates);
2009 return frm + nrates;
2013 * Add an extended supported rates element id to a frame.
2016 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
2019 * Add an extended supported rates element if operating in 11g mode.
2021 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2022 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2023 *frm++ = IEEE80211_ELEMID_XRATES;
2025 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2032 * Add an ssid element to a frame.
2035 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
2037 *frm++ = IEEE80211_ELEMID_SSID;
2039 memcpy(frm, ssid, len);
2044 * Add an erp element to a frame.
2047 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
2051 *frm++ = IEEE80211_ELEMID_ERP;
2054 if (ic->ic_nonerpsta != 0)
2055 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2056 if (ic->ic_flags & IEEE80211_F_USEPROT)
2057 erp |= IEEE80211_ERP_USE_PROTECTION;
2058 if (ic->ic_flags & IEEE80211_F_USEBARKER)
2059 erp |= IEEE80211_ERP_LONG_PREAMBLE;
2065 * Add a CFParams element to a frame.
2068 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
2070 #define ADDSHORT(frm, v) do { \
2074 *frm++ = IEEE80211_ELEMID_CFPARMS;
2076 *frm++ = 0; /* CFP count */
2077 *frm++ = 2; /* CFP period */
2078 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
2079 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
2084 static __inline uint8_t *
2085 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2087 memcpy(frm, ie->ie_data, ie->ie_len);
2088 return frm + ie->ie_len;
2091 static __inline uint8_t *
2092 add_ie(uint8_t *frm, const uint8_t *ie)
2094 memcpy(frm, ie, 2 + ie[1]);
2095 return frm + 2 + ie[1];
2098 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
2100 * Add a WME information element to a frame.
2103 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
2105 static const struct ieee80211_wme_info info = {
2106 .wme_id = IEEE80211_ELEMID_VENDOR,
2107 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
2108 .wme_oui = { WME_OUI_BYTES },
2109 .wme_type = WME_OUI_TYPE,
2110 .wme_subtype = WME_INFO_OUI_SUBTYPE,
2111 .wme_version = WME_VERSION,
2114 memcpy(frm, &info, sizeof(info));
2115 return frm + sizeof(info);
2119 * Add a WME parameters element to a frame.
2122 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
2124 #define SM(_v, _f) (((_v) << _f##_S) & _f)
2125 #define ADDSHORT(frm, v) do { \
2129 /* NB: this works 'cuz a param has an info at the front */
2130 static const struct ieee80211_wme_info param = {
2131 .wme_id = IEEE80211_ELEMID_VENDOR,
2132 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2133 .wme_oui = { WME_OUI_BYTES },
2134 .wme_type = WME_OUI_TYPE,
2135 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2136 .wme_version = WME_VERSION,
2140 memcpy(frm, ¶m, sizeof(param));
2141 frm += __offsetof(struct ieee80211_wme_info, wme_info);
2142 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
2143 *frm++ = 0; /* reserved field */
2144 for (i = 0; i < WME_NUM_AC; i++) {
2145 const struct wmeParams *ac =
2146 &wme->wme_bssChanParams.cap_wmeParams[i];
2147 *frm++ = SM(i, WME_PARAM_ACI)
2148 | SM(ac->wmep_acm, WME_PARAM_ACM)
2149 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
2151 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
2152 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
2154 ADDSHORT(frm, ac->wmep_txopLimit);
2160 #undef WME_OUI_BYTES
2163 * Add an 11h Power Constraint element to a frame.
2166 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2168 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2169 /* XXX per-vap tx power limit? */
2170 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2172 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2174 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
2179 * Add an 11h Power Capability element to a frame.
2182 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2184 frm[0] = IEEE80211_ELEMID_PWRCAP;
2186 frm[2] = c->ic_minpower;
2187 frm[3] = c->ic_maxpower;
2192 * Add an 11h Supported Channels element to a frame.
2195 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2197 static const int ielen = 26;
2199 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2201 /* XXX not correct */
2202 memcpy(frm+2, ic->ic_chan_avail, ielen);
2203 return frm + 2 + ielen;
2207 * Add an 11h Quiet time element to a frame.
2210 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
2212 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2214 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2218 * Only update every beacon interval - otherwise probe responses
2219 * would update the quiet count value.
2222 if (vap->iv_quiet_count_value == 1)
2223 vap->iv_quiet_count_value = vap->iv_quiet_count;
2224 else if (vap->iv_quiet_count_value > 1)
2225 vap->iv_quiet_count_value--;
2228 if (vap->iv_quiet_count_value == 0) {
2229 /* value 0 is reserved as per 802.11h standerd */
2230 vap->iv_quiet_count_value = 1;
2233 quiet->tbttcount = vap->iv_quiet_count_value;
2234 quiet->period = vap->iv_quiet_period;
2235 quiet->duration = htole16(vap->iv_quiet_duration);
2236 quiet->offset = htole16(vap->iv_quiet_offset);
2237 return frm + sizeof(*quiet);
2241 * Add an 11h Channel Switch Announcement element to a frame.
2242 * Note that we use the per-vap CSA count to adjust the global
2243 * counter so we can use this routine to form probe response
2244 * frames and get the current count.
2247 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2249 struct ieee80211com *ic = vap->iv_ic;
2250 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2252 csa->csa_ie = IEEE80211_ELEMID_CSA;
2254 csa->csa_mode = 1; /* XXX force quiet on channel */
2255 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2256 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2257 return frm + sizeof(*csa);
2261 * Add an 11h country information element to a frame.
2264 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2267 if (ic->ic_countryie == NULL ||
2268 ic->ic_countryie_chan != ic->ic_bsschan) {
2270 * Handle lazy construction of ie. This is done on
2271 * first use and after a channel change that requires
2274 if (ic->ic_countryie != NULL)
2275 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2276 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2277 if (ic->ic_countryie == NULL)
2279 ic->ic_countryie_chan = ic->ic_bsschan;
2281 return add_appie(frm, ic->ic_countryie);
2285 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2287 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2288 return (add_ie(frm, vap->iv_wpa_ie));
2290 /* XXX else complain? */
2296 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2298 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2299 return (add_ie(frm, vap->iv_rsn_ie));
2301 /* XXX else complain? */
2307 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2309 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2310 *frm++ = IEEE80211_ELEMID_QOS;
2319 * Send a probe request frame with the specified ssid
2320 * and any optional information element data.
2323 ieee80211_send_probereq(struct ieee80211_node *ni,
2324 const uint8_t sa[IEEE80211_ADDR_LEN],
2325 const uint8_t da[IEEE80211_ADDR_LEN],
2326 const uint8_t bssid[IEEE80211_ADDR_LEN],
2327 const uint8_t *ssid, size_t ssidlen)
2329 struct ieee80211vap *vap = ni->ni_vap;
2330 struct ieee80211com *ic = ni->ni_ic;
2331 struct ieee80211_node *bss;
2332 const struct ieee80211_txparam *tp;
2333 struct ieee80211_bpf_params params;
2334 const struct ieee80211_rateset *rs;
2339 bss = ieee80211_ref_node(vap->iv_bss);
2341 if (vap->iv_state == IEEE80211_S_CAC) {
2342 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2343 "block %s frame in CAC state", "probe request");
2344 vap->iv_stats.is_tx_badstate++;
2345 ieee80211_free_node(bss);
2346 return EIO; /* XXX */
2350 * Hold a reference on the node so it doesn't go away until after
2351 * the xmit is complete all the way in the driver. On error we
2352 * will remove our reference.
2354 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2355 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2357 ni, ether_sprintf(ni->ni_macaddr),
2358 ieee80211_node_refcnt(ni)+1);
2359 ieee80211_ref_node(ni);
2362 * prreq frame format
2364 * [tlv] supported rates
2365 * [tlv] RSN (optional)
2366 * [tlv] extended supported rates
2367 * [tlv] HT cap (optional)
2368 * [tlv] VHT cap (optional)
2369 * [tlv] WPA (optional)
2370 * [tlv] user-specified ie's
2372 m = ieee80211_getmgtframe(&frm,
2373 ic->ic_headroom + sizeof(struct ieee80211_frame),
2374 2 + IEEE80211_NWID_LEN
2375 + 2 + IEEE80211_RATE_SIZE
2376 + sizeof(struct ieee80211_ie_htcap)
2377 + sizeof(struct ieee80211_ie_vhtcap)
2378 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */
2379 + sizeof(struct ieee80211_ie_wpa)
2380 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2381 + sizeof(struct ieee80211_ie_wpa)
2382 + (vap->iv_appie_probereq != NULL ?
2383 vap->iv_appie_probereq->ie_len : 0)
2386 vap->iv_stats.is_tx_nobuf++;
2387 ieee80211_free_node(ni);
2388 ieee80211_free_node(bss);
2392 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2393 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2394 frm = ieee80211_add_rates(frm, rs);
2395 frm = ieee80211_add_rsn(frm, vap);
2396 frm = ieee80211_add_xrates(frm, rs);
2399 * Note: we can't use bss; we don't have one yet.
2401 * So, we should announce our capabilities
2402 * in this channel mode (2g/5g), not the
2403 * channel details itself.
2405 if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2406 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2407 struct ieee80211_channel *c;
2410 * Get the HT channel that we should try upgrading to.
2411 * If we can do 40MHz then this'll upgrade it appropriately.
2413 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2415 frm = ieee80211_add_htcap_ch(frm, vap, c);
2419 * XXX TODO: need to figure out what/how to update the
2423 (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
2424 struct ieee80211_channel *c;
2426 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2428 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
2429 frm = ieee80211_add_vhtcap_ch(frm, vap, c);
2433 frm = ieee80211_add_wpa(frm, vap);
2434 if (vap->iv_appie_probereq != NULL)
2435 frm = add_appie(frm, vap->iv_appie_probereq);
2436 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2438 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2439 ("leading space %zd", M_LEADINGSPACE(m)));
2440 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2442 /* NB: cannot happen */
2443 ieee80211_free_node(ni);
2444 ieee80211_free_node(bss);
2448 IEEE80211_TX_LOCK(ic);
2449 ieee80211_send_setup(ni, m,
2450 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2451 IEEE80211_NONQOS_TID, sa, da, bssid);
2452 /* XXX power management? */
2453 m->m_flags |= M_ENCAP; /* mark encapsulated */
2455 M_WME_SETAC(m, WME_AC_BE);
2457 IEEE80211_NODE_STAT(ni, tx_probereq);
2458 IEEE80211_NODE_STAT(ni, tx_mgmt);
2460 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2461 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2462 ieee80211_chan2ieee(ic, ic->ic_curchan),
2463 ether_sprintf(bssid),
2468 memset(¶ms, 0, sizeof(params));
2469 params.ibp_pri = M_WME_GETAC(m);
2470 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2471 params.ibp_rate0 = tp->mgmtrate;
2472 if (IEEE80211_IS_MULTICAST(da)) {
2473 params.ibp_flags |= IEEE80211_BPF_NOACK;
2474 params.ibp_try0 = 1;
2476 params.ibp_try0 = tp->maxretry;
2477 params.ibp_power = ni->ni_txpower;
2478 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2479 IEEE80211_TX_UNLOCK(ic);
2480 ieee80211_free_node(bss);
2485 * Calculate capability information for mgt frames.
2488 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2490 struct ieee80211com *ic = vap->iv_ic;
2493 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2495 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2496 capinfo = IEEE80211_CAPINFO_ESS;
2497 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2498 capinfo = IEEE80211_CAPINFO_IBSS;
2501 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2502 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2503 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2504 IEEE80211_IS_CHAN_2GHZ(chan))
2505 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2506 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2507 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2508 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2509 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2514 * Send a management frame. The node is for the destination (or ic_bss
2515 * when in station mode). Nodes other than ic_bss have their reference
2516 * count bumped to reflect our use for an indeterminant time.
2519 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2521 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2522 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2523 struct ieee80211vap *vap = ni->ni_vap;
2524 struct ieee80211com *ic = ni->ni_ic;
2525 struct ieee80211_node *bss = vap->iv_bss;
2526 struct ieee80211_bpf_params params;
2530 int has_challenge, is_shared_key, ret, status;
2532 KASSERT(ni != NULL, ("null node"));
2535 * Hold a reference on the node so it doesn't go away until after
2536 * the xmit is complete all the way in the driver. On error we
2537 * will remove our reference.
2539 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2540 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2542 ni, ether_sprintf(ni->ni_macaddr),
2543 ieee80211_node_refcnt(ni)+1);
2544 ieee80211_ref_node(ni);
2546 memset(¶ms, 0, sizeof(params));
2549 case IEEE80211_FC0_SUBTYPE_AUTH:
2552 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2553 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2554 ni->ni_challenge != NULL);
2557 * Deduce whether we're doing open authentication or
2558 * shared key authentication. We do the latter if
2559 * we're in the middle of a shared key authentication
2560 * handshake or if we're initiating an authentication
2561 * request and configured to use shared key.
2563 is_shared_key = has_challenge ||
2564 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2565 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2566 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2568 m = ieee80211_getmgtframe(&frm,
2569 ic->ic_headroom + sizeof(struct ieee80211_frame),
2570 3 * sizeof(uint16_t)
2571 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2572 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2575 senderr(ENOMEM, is_tx_nobuf);
2577 ((uint16_t *)frm)[0] =
2578 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2579 : htole16(IEEE80211_AUTH_ALG_OPEN);
2580 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2581 ((uint16_t *)frm)[2] = htole16(status);/* status */
2583 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2584 ((uint16_t *)frm)[3] =
2585 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2586 IEEE80211_ELEMID_CHALLENGE);
2587 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2588 IEEE80211_CHALLENGE_LEN);
2589 m->m_pkthdr.len = m->m_len =
2590 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2591 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2592 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2593 "request encrypt frame (%s)", __func__);
2594 /* mark frame for encryption */
2595 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2598 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2600 /* XXX not right for shared key */
2601 if (status == IEEE80211_STATUS_SUCCESS)
2602 IEEE80211_NODE_STAT(ni, tx_auth);
2604 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2606 if (vap->iv_opmode == IEEE80211_M_STA)
2607 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2608 (void *) vap->iv_state);
2611 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2612 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2613 "send station deauthenticate (reason: %d (%s))", arg,
2614 ieee80211_reason_to_string(arg));
2615 m = ieee80211_getmgtframe(&frm,
2616 ic->ic_headroom + sizeof(struct ieee80211_frame),
2619 senderr(ENOMEM, is_tx_nobuf);
2620 *(uint16_t *)frm = htole16(arg); /* reason */
2621 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2623 IEEE80211_NODE_STAT(ni, tx_deauth);
2624 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2626 ieee80211_node_unauthorize(ni); /* port closed */
2629 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2630 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2632 * asreq frame format
2633 * [2] capability information
2634 * [2] listen interval
2635 * [6*] current AP address (reassoc only)
2637 * [tlv] supported rates
2638 * [tlv] extended supported rates
2639 * [4] power capability (optional)
2640 * [28] supported channels (optional)
2641 * [tlv] HT capabilities
2642 * [tlv] VHT capabilities
2643 * [tlv] WME (optional)
2644 * [tlv] Vendor OUI HT capabilities (optional)
2645 * [tlv] Atheros capabilities (if negotiated)
2646 * [tlv] AppIE's (optional)
2648 m = ieee80211_getmgtframe(&frm,
2649 ic->ic_headroom + sizeof(struct ieee80211_frame),
2652 + IEEE80211_ADDR_LEN
2653 + 2 + IEEE80211_NWID_LEN
2654 + 2 + IEEE80211_RATE_SIZE
2655 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2658 + sizeof(struct ieee80211_wme_info)
2659 + sizeof(struct ieee80211_ie_htcap)
2660 + sizeof(struct ieee80211_ie_vhtcap)
2661 + 4 + sizeof(struct ieee80211_ie_htcap)
2662 #ifdef IEEE80211_SUPPORT_SUPERG
2663 + sizeof(struct ieee80211_ath_ie)
2665 + (vap->iv_appie_wpa != NULL ?
2666 vap->iv_appie_wpa->ie_len : 0)
2667 + (vap->iv_appie_assocreq != NULL ?
2668 vap->iv_appie_assocreq->ie_len : 0)
2671 senderr(ENOMEM, is_tx_nobuf);
2673 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2674 ("wrong mode %u", vap->iv_opmode));
2675 capinfo = IEEE80211_CAPINFO_ESS;
2676 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2677 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2679 * NB: Some 11a AP's reject the request when
2680 * short preamble is set.
2682 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2683 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2684 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2685 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2686 (ic->ic_caps & IEEE80211_C_SHSLOT))
2687 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2688 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2689 (vap->iv_flags & IEEE80211_F_DOTH))
2690 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2691 *(uint16_t *)frm = htole16(capinfo);
2694 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2695 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2699 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2700 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2701 frm += IEEE80211_ADDR_LEN;
2704 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2705 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2706 frm = ieee80211_add_rsn(frm, vap);
2707 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2708 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2709 frm = ieee80211_add_powercapability(frm,
2711 frm = ieee80211_add_supportedchannels(frm, ic);
2715 * Check the channel - we may be using an 11n NIC with an
2716 * 11n capable station, but we're configured to be an 11b
2719 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2720 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2721 ni->ni_ies.htcap_ie != NULL &&
2722 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2723 frm = ieee80211_add_htcap(frm, ni);
2726 if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
2727 IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
2728 ni->ni_ies.vhtcap_ie != NULL &&
2729 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
2730 frm = ieee80211_add_vhtcap(frm, ni);
2733 frm = ieee80211_add_wpa(frm, vap);
2734 if ((ic->ic_flags & IEEE80211_F_WME) &&
2735 ni->ni_ies.wme_ie != NULL)
2736 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2739 * Same deal - only send HT info if we're on an 11n
2742 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2743 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2744 ni->ni_ies.htcap_ie != NULL &&
2745 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2746 frm = ieee80211_add_htcap_vendor(frm, ni);
2748 #ifdef IEEE80211_SUPPORT_SUPERG
2749 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2750 frm = ieee80211_add_ath(frm,
2751 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2752 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2753 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2754 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2756 #endif /* IEEE80211_SUPPORT_SUPERG */
2757 if (vap->iv_appie_assocreq != NULL)
2758 frm = add_appie(frm, vap->iv_appie_assocreq);
2759 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2761 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2762 (void *) vap->iv_state);
2765 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2766 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2768 * asresp frame format
2769 * [2] capability information
2771 * [2] association ID
2772 * [tlv] supported rates
2773 * [tlv] extended supported rates
2774 * [tlv] HT capabilities (standard, if STA enabled)
2775 * [tlv] HT information (standard, if STA enabled)
2776 * [tlv] VHT capabilities (standard, if STA enabled)
2777 * [tlv] VHT information (standard, if STA enabled)
2778 * [tlv] WME (if configured and STA enabled)
2779 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2780 * [tlv] HT information (vendor OUI, if STA enabled)
2781 * [tlv] Atheros capabilities (if STA enabled)
2782 * [tlv] AppIE's (optional)
2784 m = ieee80211_getmgtframe(&frm,
2785 ic->ic_headroom + sizeof(struct ieee80211_frame),
2789 + 2 + IEEE80211_RATE_SIZE
2790 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2791 + sizeof(struct ieee80211_ie_htcap) + 4
2792 + sizeof(struct ieee80211_ie_htinfo) + 4
2793 + sizeof(struct ieee80211_ie_vhtcap)
2794 + sizeof(struct ieee80211_ie_vht_operation)
2795 + sizeof(struct ieee80211_wme_param)
2796 #ifdef IEEE80211_SUPPORT_SUPERG
2797 + sizeof(struct ieee80211_ath_ie)
2799 + (vap->iv_appie_assocresp != NULL ?
2800 vap->iv_appie_assocresp->ie_len : 0)
2803 senderr(ENOMEM, is_tx_nobuf);
2805 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2806 *(uint16_t *)frm = htole16(capinfo);
2809 *(uint16_t *)frm = htole16(arg); /* status */
2812 if (arg == IEEE80211_STATUS_SUCCESS) {
2813 *(uint16_t *)frm = htole16(ni->ni_associd);
2814 IEEE80211_NODE_STAT(ni, tx_assoc);
2816 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2819 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2820 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2821 /* NB: respond according to what we received */
2822 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2823 frm = ieee80211_add_htcap(frm, ni);
2824 frm = ieee80211_add_htinfo(frm, ni);
2826 if ((vap->iv_flags & IEEE80211_F_WME) &&
2827 ni->ni_ies.wme_ie != NULL)
2828 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2829 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2830 frm = ieee80211_add_htcap_vendor(frm, ni);
2831 frm = ieee80211_add_htinfo_vendor(frm, ni);
2833 if (ni->ni_flags & IEEE80211_NODE_VHT) {
2834 frm = ieee80211_add_vhtcap(frm, ni);
2835 frm = ieee80211_add_vhtinfo(frm, ni);
2837 #ifdef IEEE80211_SUPPORT_SUPERG
2838 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2839 frm = ieee80211_add_ath(frm,
2840 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2841 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2842 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2843 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2844 #endif /* IEEE80211_SUPPORT_SUPERG */
2845 if (vap->iv_appie_assocresp != NULL)
2846 frm = add_appie(frm, vap->iv_appie_assocresp);
2847 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2850 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2851 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2852 "send station disassociate (reason: %d (%s))", arg,
2853 ieee80211_reason_to_string(arg));
2854 m = ieee80211_getmgtframe(&frm,
2855 ic->ic_headroom + sizeof(struct ieee80211_frame),
2858 senderr(ENOMEM, is_tx_nobuf);
2859 *(uint16_t *)frm = htole16(arg); /* reason */
2860 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2862 IEEE80211_NODE_STAT(ni, tx_disassoc);
2863 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2867 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2868 "invalid mgmt frame type %u", type);
2869 senderr(EINVAL, is_tx_unknownmgt);
2873 /* NB: force non-ProbeResp frames to the highest queue */
2874 params.ibp_pri = WME_AC_VO;
2875 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2876 /* NB: we know all frames are unicast */
2877 params.ibp_try0 = bss->ni_txparms->maxretry;
2878 params.ibp_power = bss->ni_txpower;
2879 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2881 ieee80211_free_node(ni);
2888 * Return an mbuf with a probe response frame in it.
2889 * Space is left to prepend and 802.11 header at the
2890 * front but it's left to the caller to fill in.
2893 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2895 struct ieee80211vap *vap = bss->ni_vap;
2896 struct ieee80211com *ic = bss->ni_ic;
2897 const struct ieee80211_rateset *rs;
2903 * probe response frame format
2905 * [2] beacon interval
2906 * [2] cabability information
2908 * [tlv] supported rates
2909 * [tlv] parameter set (FH/DS)
2910 * [tlv] parameter set (IBSS)
2911 * [tlv] country (optional)
2912 * [3] power control (optional)
2913 * [5] channel switch announcement (CSA) (optional)
2914 * [tlv] extended rate phy (ERP)
2915 * [tlv] extended supported rates
2916 * [tlv] RSN (optional)
2917 * [tlv] HT capabilities
2918 * [tlv] HT information
2919 * [tlv] VHT capabilities
2920 * [tlv] VHT information
2921 * [tlv] WPA (optional)
2922 * [tlv] WME (optional)
2923 * [tlv] Vendor OUI HT capabilities (optional)
2924 * [tlv] Vendor OUI HT information (optional)
2925 * [tlv] Atheros capabilities
2926 * [tlv] AppIE's (optional)
2927 * [tlv] Mesh ID (MBSS)
2928 * [tlv] Mesh Conf (MBSS)
2930 m = ieee80211_getmgtframe(&frm,
2931 ic->ic_headroom + sizeof(struct ieee80211_frame),
2935 + 2 + IEEE80211_NWID_LEN
2936 + 2 + IEEE80211_RATE_SIZE
2938 + IEEE80211_COUNTRY_MAX_SIZE
2940 + sizeof(struct ieee80211_csa_ie)
2941 + sizeof(struct ieee80211_quiet_ie)
2943 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2944 + sizeof(struct ieee80211_ie_wpa)
2945 + sizeof(struct ieee80211_ie_htcap)
2946 + sizeof(struct ieee80211_ie_htinfo)
2947 + sizeof(struct ieee80211_ie_wpa)
2948 + sizeof(struct ieee80211_wme_param)
2949 + 4 + sizeof(struct ieee80211_ie_htcap)
2950 + 4 + sizeof(struct ieee80211_ie_htinfo)
2951 + sizeof(struct ieee80211_ie_vhtcap)
2952 + sizeof(struct ieee80211_ie_vht_operation)
2953 #ifdef IEEE80211_SUPPORT_SUPERG
2954 + sizeof(struct ieee80211_ath_ie)
2956 #ifdef IEEE80211_SUPPORT_MESH
2957 + 2 + IEEE80211_MESHID_LEN
2958 + sizeof(struct ieee80211_meshconf_ie)
2960 + (vap->iv_appie_proberesp != NULL ?
2961 vap->iv_appie_proberesp->ie_len : 0)
2964 vap->iv_stats.is_tx_nobuf++;
2968 memset(frm, 0, 8); /* timestamp should be filled later */
2970 *(uint16_t *)frm = htole16(bss->ni_intval);
2972 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2973 *(uint16_t *)frm = htole16(capinfo);
2976 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2977 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2978 frm = ieee80211_add_rates(frm, rs);
2980 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2981 *frm++ = IEEE80211_ELEMID_FHPARMS;
2983 *frm++ = bss->ni_fhdwell & 0x00ff;
2984 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2985 *frm++ = IEEE80211_FH_CHANSET(
2986 ieee80211_chan2ieee(ic, bss->ni_chan));
2987 *frm++ = IEEE80211_FH_CHANPAT(
2988 ieee80211_chan2ieee(ic, bss->ni_chan));
2989 *frm++ = bss->ni_fhindex;
2991 *frm++ = IEEE80211_ELEMID_DSPARMS;
2993 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2996 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2997 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2999 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3001 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3002 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3003 frm = ieee80211_add_countryie(frm, ic);
3004 if (vap->iv_flags & IEEE80211_F_DOTH) {
3005 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3006 frm = ieee80211_add_powerconstraint(frm, vap);
3007 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3008 frm = ieee80211_add_csa(frm, vap);
3010 if (vap->iv_flags & IEEE80211_F_DOTH) {
3011 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3012 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3014 frm = ieee80211_add_quiet(frm, vap, 0);
3017 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3018 frm = ieee80211_add_erp(frm, ic);
3019 frm = ieee80211_add_xrates(frm, rs);
3020 frm = ieee80211_add_rsn(frm, vap);
3022 * NB: legacy 11b clients do not get certain ie's.
3023 * The caller identifies such clients by passing
3024 * a token in legacy to us. Could expand this to be
3025 * any legacy client for stuff like HT ie's.
3027 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3028 legacy != IEEE80211_SEND_LEGACY_11B) {
3029 frm = ieee80211_add_htcap(frm, bss);
3030 frm = ieee80211_add_htinfo(frm, bss);
3032 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3033 legacy != IEEE80211_SEND_LEGACY_11B) {
3034 frm = ieee80211_add_vhtcap(frm, bss);
3035 frm = ieee80211_add_vhtinfo(frm, bss);
3037 frm = ieee80211_add_wpa(frm, vap);
3038 if (vap->iv_flags & IEEE80211_F_WME)
3039 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
3040 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3041 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3042 legacy != IEEE80211_SEND_LEGACY_11B) {
3043 frm = ieee80211_add_htcap_vendor(frm, bss);
3044 frm = ieee80211_add_htinfo_vendor(frm, bss);
3046 #ifdef IEEE80211_SUPPORT_SUPERG
3047 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
3048 legacy != IEEE80211_SEND_LEGACY_11B)
3049 frm = ieee80211_add_athcaps(frm, bss);
3051 if (vap->iv_appie_proberesp != NULL)
3052 frm = add_appie(frm, vap->iv_appie_proberesp);
3053 #ifdef IEEE80211_SUPPORT_MESH
3054 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3055 frm = ieee80211_add_meshid(frm, vap);
3056 frm = ieee80211_add_meshconf(frm, vap);
3059 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3065 * Send a probe response frame to the specified mac address.
3066 * This does not go through the normal mgt frame api so we
3067 * can specify the destination address and re-use the bss node
3068 * for the sta reference.
3071 ieee80211_send_proberesp(struct ieee80211vap *vap,
3072 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3074 struct ieee80211_node *bss = vap->iv_bss;
3075 struct ieee80211com *ic = vap->iv_ic;
3079 if (vap->iv_state == IEEE80211_S_CAC) {
3080 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3081 "block %s frame in CAC state", "probe response");
3082 vap->iv_stats.is_tx_badstate++;
3083 return EIO; /* XXX */
3087 * Hold a reference on the node so it doesn't go away until after
3088 * the xmit is complete all the way in the driver. On error we
3089 * will remove our reference.
3091 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3092 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3093 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3094 ieee80211_node_refcnt(bss)+1);
3095 ieee80211_ref_node(bss);
3097 m = ieee80211_alloc_proberesp(bss, legacy);
3099 ieee80211_free_node(bss);
3103 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3104 KASSERT(m != NULL, ("no room for header"));
3106 IEEE80211_TX_LOCK(ic);
3107 ieee80211_send_setup(bss, m,
3108 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
3109 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3110 /* XXX power management? */
3111 m->m_flags |= M_ENCAP; /* mark encapsulated */
3113 M_WME_SETAC(m, WME_AC_BE);
3115 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3116 "send probe resp on channel %u to %s%s\n",
3117 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3118 legacy ? " <legacy>" : "");
3119 IEEE80211_NODE_STAT(bss, tx_mgmt);
3121 ret = ieee80211_raw_output(vap, bss, m, NULL);
3122 IEEE80211_TX_UNLOCK(ic);
3127 * Allocate and build a RTS (Request To Send) control frame.
3130 ieee80211_alloc_rts(struct ieee80211com *ic,
3131 const uint8_t ra[IEEE80211_ADDR_LEN],
3132 const uint8_t ta[IEEE80211_ADDR_LEN],
3135 struct ieee80211_frame_rts *rts;
3138 /* XXX honor ic_headroom */
3139 m = m_gethdr(M_NOWAIT, MT_DATA);
3141 rts = mtod(m, struct ieee80211_frame_rts *);
3142 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3143 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3144 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3145 *(u_int16_t *)rts->i_dur = htole16(dur);
3146 IEEE80211_ADDR_COPY(rts->i_ra, ra);
3147 IEEE80211_ADDR_COPY(rts->i_ta, ta);
3149 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3155 * Allocate and build a CTS (Clear To Send) control frame.
3158 ieee80211_alloc_cts(struct ieee80211com *ic,
3159 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3161 struct ieee80211_frame_cts *cts;
3164 /* XXX honor ic_headroom */
3165 m = m_gethdr(M_NOWAIT, MT_DATA);
3167 cts = mtod(m, struct ieee80211_frame_cts *);
3168 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3169 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3170 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3171 *(u_int16_t *)cts->i_dur = htole16(dur);
3172 IEEE80211_ADDR_COPY(cts->i_ra, ra);
3174 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3180 * Wrapper for CTS/RTS frame allocation.
3183 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3184 uint8_t rate, int prot)
3186 struct ieee80211com *ic = ni->ni_ic;
3187 const struct ieee80211_frame *wh;
3190 int pktlen, isshort;
3192 KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3193 prot == IEEE80211_PROT_CTSONLY,
3194 ("wrong protection type %d", prot));
3196 wh = mtod(m, const struct ieee80211_frame *);
3197 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3198 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3199 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3200 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3202 if (prot == IEEE80211_PROT_RTSCTS) {
3203 /* NB: CTS is the same size as an ACK */
3204 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3205 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3207 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3213 ieee80211_tx_mgt_timeout(void *arg)
3215 struct ieee80211vap *vap = arg;
3217 IEEE80211_LOCK(vap->iv_ic);
3218 if (vap->iv_state != IEEE80211_S_INIT &&
3219 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3221 * NB: it's safe to specify a timeout as the reason here;
3222 * it'll only be used in the right state.
3224 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
3225 IEEE80211_SCAN_FAIL_TIMEOUT);
3227 IEEE80211_UNLOCK(vap->iv_ic);
3231 * This is the callback set on net80211-sourced transmitted
3232 * authentication request frames.
3234 * This does a couple of things:
3236 * + If the frame transmitted was a success, it schedules a future
3237 * event which will transition the interface to scan.
3238 * If a state transition _then_ occurs before that event occurs,
3239 * said state transition will cancel this callout.
3241 * + If the frame transmit was a failure, it immediately schedules
3242 * the transition back to scan.
3245 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3247 struct ieee80211vap *vap = ni->ni_vap;
3248 enum ieee80211_state ostate = (enum ieee80211_state) arg;
3251 * Frame transmit completed; arrange timer callback. If
3252 * transmit was successfully we wait for response. Otherwise
3253 * we arrange an immediate callback instead of doing the
3254 * callback directly since we don't know what state the driver
3255 * is in (e.g. what locks it is holding). This work should
3256 * not be too time-critical and not happen too often so the
3257 * added overhead is acceptable.
3259 * XXX what happens if !acked but response shows up before callback?
3261 if (vap->iv_state == ostate) {
3262 callout_reset(&vap->iv_mgtsend,
3263 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3264 ieee80211_tx_mgt_timeout, vap);
3269 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3270 struct ieee80211_node *ni)
3272 struct ieee80211vap *vap = ni->ni_vap;
3273 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3274 struct ieee80211com *ic = ni->ni_ic;
3275 struct ieee80211_rateset *rs = &ni->ni_rates;
3279 * beacon frame format
3281 * TODO: update to 802.11-2012; a lot of stuff has changed;
3282 * vendor extensions should be at the end, etc.
3285 * [2] beacon interval
3286 * [2] cabability information
3288 * [tlv] supported rates
3289 * [3] parameter set (DS)
3290 * [8] CF parameter set (optional)
3291 * [tlv] parameter set (IBSS/TIM)
3292 * [tlv] country (optional)
3293 * [3] power control (optional)
3294 * [5] channel switch announcement (CSA) (optional)
3296 * XXX TODO: IBSS DFS
3297 * XXX TODO: TPC report
3298 * [tlv] extended rate phy (ERP)
3299 * [tlv] extended supported rates
3300 * [tlv] RSN parameters
3302 * (XXX EDCA parameter set, QoS capability?)
3303 * XXX TODO: AP channel report
3305 * [tlv] HT capabilities
3306 * [tlv] HT information
3307 * XXX TODO: 20/40 BSS coexistence
3310 * XXX TODO: mesh config
3311 * XXX TODO: mesh awake window
3312 * XXX TODO: beacon timing (mesh, etc)
3313 * XXX TODO: MCCAOP Advertisement Overview
3314 * XXX TODO: MCCAOP Advertisement
3315 * XXX TODO: Mesh channel switch parameters
3317 * XXX TODO: VHT capabilities
3318 * XXX TODO: VHT operation
3319 * XXX TODO: VHT transmit power envelope
3320 * XXX TODO: channel switch wrapper element
3321 * XXX TODO: extended BSS load element
3323 * XXX Vendor-specific OIDs (e.g. Atheros)
3324 * [tlv] WPA parameters
3325 * [tlv] WME parameters
3326 * [tlv] Vendor OUI HT capabilities (optional)
3327 * [tlv] Vendor OUI HT information (optional)
3328 * [tlv] Atheros capabilities (optional)
3329 * [tlv] TDMA parameters (optional)
3330 * [tlv] Mesh ID (MBSS)
3331 * [tlv] Mesh Conf (MBSS)
3332 * [tlv] application data (optional)
3335 memset(bo, 0, sizeof(*bo));
3337 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3339 *(uint16_t *)frm = htole16(ni->ni_intval);
3341 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3342 bo->bo_caps = (uint16_t *)frm;
3343 *(uint16_t *)frm = htole16(capinfo);
3345 *frm++ = IEEE80211_ELEMID_SSID;
3346 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3347 *frm++ = ni->ni_esslen;
3348 memcpy(frm, ni->ni_essid, ni->ni_esslen);
3349 frm += ni->ni_esslen;
3352 frm = ieee80211_add_rates(frm, rs);
3353 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3354 *frm++ = IEEE80211_ELEMID_DSPARMS;
3356 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3358 if (ic->ic_flags & IEEE80211_F_PCF) {
3360 frm = ieee80211_add_cfparms(frm, ic);
3363 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3364 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3366 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3368 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3369 vap->iv_opmode == IEEE80211_M_MBSS) {
3370 /* TIM IE is the same for Mesh and Hostap */
3371 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3373 tie->tim_ie = IEEE80211_ELEMID_TIM;
3374 tie->tim_len = 4; /* length */
3375 tie->tim_count = 0; /* DTIM count */
3376 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3377 tie->tim_bitctl = 0; /* bitmap control */
3378 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3379 frm += sizeof(struct ieee80211_tim_ie);
3382 bo->bo_tim_trailer = frm;
3383 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3384 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3385 frm = ieee80211_add_countryie(frm, ic);
3386 if (vap->iv_flags & IEEE80211_F_DOTH) {
3387 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3388 frm = ieee80211_add_powerconstraint(frm, vap);
3390 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3391 frm = ieee80211_add_csa(frm, vap);
3395 bo->bo_quiet = NULL;
3396 if (vap->iv_flags & IEEE80211_F_DOTH) {
3397 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3398 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
3399 (vap->iv_quiet == 1)) {
3401 * We only insert the quiet IE offset if
3402 * the quiet IE is enabled. Otherwise don't
3403 * put it here or we'll just overwrite
3404 * some other beacon contents.
3406 if (vap->iv_quiet) {
3408 frm = ieee80211_add_quiet(frm,vap, 0);
3413 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3415 frm = ieee80211_add_erp(frm, ic);
3417 frm = ieee80211_add_xrates(frm, rs);
3418 frm = ieee80211_add_rsn(frm, vap);
3419 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3420 frm = ieee80211_add_htcap(frm, ni);
3421 bo->bo_htinfo = frm;
3422 frm = ieee80211_add_htinfo(frm, ni);
3425 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
3426 frm = ieee80211_add_vhtcap(frm, ni);
3427 bo->bo_vhtinfo = frm;
3428 frm = ieee80211_add_vhtinfo(frm, ni);
3429 /* Transmit power envelope */
3430 /* Channel switch wrapper element */
3431 /* Extended bss load element */
3434 frm = ieee80211_add_wpa(frm, vap);
3435 if (vap->iv_flags & IEEE80211_F_WME) {
3437 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
3439 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3440 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3441 frm = ieee80211_add_htcap_vendor(frm, ni);
3442 frm = ieee80211_add_htinfo_vendor(frm, ni);
3445 #ifdef IEEE80211_SUPPORT_SUPERG
3446 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3448 frm = ieee80211_add_athcaps(frm, ni);
3451 #ifdef IEEE80211_SUPPORT_TDMA
3452 if (vap->iv_caps & IEEE80211_C_TDMA) {
3454 frm = ieee80211_add_tdma(frm, vap);
3457 if (vap->iv_appie_beacon != NULL) {
3459 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3460 frm = add_appie(frm, vap->iv_appie_beacon);
3463 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */
3464 #ifdef IEEE80211_SUPPORT_MESH
3465 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3466 frm = ieee80211_add_meshid(frm, vap);
3467 bo->bo_meshconf = frm;
3468 frm = ieee80211_add_meshconf(frm, vap);
3471 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3472 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3473 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3477 * Allocate a beacon frame and fillin the appropriate bits.
3480 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3482 struct ieee80211vap *vap = ni->ni_vap;
3483 struct ieee80211com *ic = ni->ni_ic;
3484 struct ifnet *ifp = vap->iv_ifp;
3485 struct ieee80211_frame *wh;
3491 * Update the "We're putting the quiet IE in the beacon" state.
3493 if (vap->iv_quiet == 1)
3494 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3495 else if (vap->iv_quiet == 0)
3496 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3499 * beacon frame format
3501 * Note: This needs updating for 802.11-2012.
3504 * [2] beacon interval
3505 * [2] cabability information
3507 * [tlv] supported rates
3508 * [3] parameter set (DS)
3509 * [8] CF parameter set (optional)
3510 * [tlv] parameter set (IBSS/TIM)
3511 * [tlv] country (optional)
3512 * [3] power control (optional)
3513 * [5] channel switch announcement (CSA) (optional)
3514 * [tlv] extended rate phy (ERP)
3515 * [tlv] extended supported rates
3516 * [tlv] RSN parameters
3517 * [tlv] HT capabilities
3518 * [tlv] HT information
3519 * [tlv] VHT capabilities
3520 * [tlv] VHT operation
3521 * [tlv] Vendor OUI HT capabilities (optional)
3522 * [tlv] Vendor OUI HT information (optional)
3523 * XXX Vendor-specific OIDs (e.g. Atheros)
3524 * [tlv] WPA parameters
3525 * [tlv] WME parameters
3526 * [tlv] TDMA parameters (optional)
3527 * [tlv] Mesh ID (MBSS)
3528 * [tlv] Mesh Conf (MBSS)
3529 * [tlv] application data (optional)
3530 * NB: we allocate the max space required for the TIM bitmap.
3531 * XXX how big is this?
3533 pktlen = 8 /* time stamp */
3534 + sizeof(uint16_t) /* beacon interval */
3535 + sizeof(uint16_t) /* capabilities */
3536 + 2 + ni->ni_esslen /* ssid */
3537 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3538 + 2 + 1 /* DS parameters */
3539 + 2 + 6 /* CF parameters */
3540 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3541 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3542 + 2 + 1 /* power control */
3543 + sizeof(struct ieee80211_csa_ie) /* CSA */
3544 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3546 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3547 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3548 2*sizeof(struct ieee80211_ie_wpa) : 0)
3549 /* XXX conditional? */
3550 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3551 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3552 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
3553 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
3554 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3555 sizeof(struct ieee80211_wme_param) : 0)
3556 #ifdef IEEE80211_SUPPORT_SUPERG
3557 + sizeof(struct ieee80211_ath_ie) /* ATH */
3559 #ifdef IEEE80211_SUPPORT_TDMA
3560 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3561 sizeof(struct ieee80211_tdma_param) : 0)
3563 #ifdef IEEE80211_SUPPORT_MESH
3564 + 2 + ni->ni_meshidlen
3565 + sizeof(struct ieee80211_meshconf_ie)
3567 + IEEE80211_MAX_APPIE
3569 m = ieee80211_getmgtframe(&frm,
3570 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3572 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3573 "%s: cannot get buf; size %u\n", __func__, pktlen);
3574 vap->iv_stats.is_tx_nobuf++;
3577 ieee80211_beacon_construct(m, frm, ni);
3579 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3580 KASSERT(m != NULL, ("no space for 802.11 header?"));
3581 wh = mtod(m, struct ieee80211_frame *);
3582 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3583 IEEE80211_FC0_SUBTYPE_BEACON;
3584 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3585 *(uint16_t *)wh->i_dur = 0;
3586 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3587 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3588 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3589 *(uint16_t *)wh->i_seq = 0;
3595 * Update the dynamic parts of a beacon frame based on the current state.
3598 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3600 struct ieee80211vap *vap = ni->ni_vap;
3601 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3602 struct ieee80211com *ic = ni->ni_ic;
3603 int len_changed = 0;
3605 struct ieee80211_frame *wh;
3606 ieee80211_seq seqno;
3610 * Handle 11h channel change when we've reached the count.
3611 * We must recalculate the beacon frame contents to account
3612 * for the new channel. Note we do this only for the first
3613 * vap that reaches this point; subsequent vaps just update
3614 * their beacon state to reflect the recalculated channel.
3616 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3617 vap->iv_csa_count == ic->ic_csa_count) {
3618 vap->iv_csa_count = 0;
3620 * Effect channel change before reconstructing the beacon
3621 * frame contents as many places reference ni_chan.
3623 if (ic->ic_csa_newchan != NULL)
3624 ieee80211_csa_completeswitch(ic);
3626 * NB: ieee80211_beacon_construct clears all pending
3627 * updates in bo_flags so we don't need to explicitly
3628 * clear IEEE80211_BEACON_CSA.
3630 ieee80211_beacon_construct(m,
3631 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3633 /* XXX do WME aggressive mode processing? */
3634 IEEE80211_UNLOCK(ic);
3635 return 1; /* just assume length changed */
3639 * Handle the quiet time element being added and removed.
3640 * Again, for now we just cheat and reconstruct the whole
3641 * beacon - that way the gap is provided as appropriate.
3643 * So, track whether we have already added the IE versus
3644 * whether we want to be adding the IE.
3646 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
3647 (vap->iv_quiet == 0)) {
3649 * Quiet time beacon IE enabled, but it's disabled;
3652 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3653 ieee80211_beacon_construct(m,
3654 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3655 /* XXX do WME aggressive mode processing? */
3656 IEEE80211_UNLOCK(ic);
3657 return 1; /* just assume length changed */
3660 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
3661 (vap->iv_quiet == 1)) {
3663 * Quiet time beacon IE disabled, but it's now enabled;
3666 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3667 ieee80211_beacon_construct(m,
3668 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3669 /* XXX do WME aggressive mode processing? */
3670 IEEE80211_UNLOCK(ic);
3671 return 1; /* just assume length changed */
3674 wh = mtod(m, struct ieee80211_frame *);
3677 * XXX TODO Strictly speaking this should be incremented with the TX
3678 * lock held so as to serialise access to the non-qos TID sequence
3681 * If the driver identifies it does its own TX seqno management then
3682 * we can skip this (and still not do the TX seqno.)
3684 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3685 *(uint16_t *)&wh->i_seq[0] =
3686 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3687 M_SEQNO_SET(m, seqno);
3689 /* XXX faster to recalculate entirely or just changes? */
3690 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3691 *bo->bo_caps = htole16(capinfo);
3693 if (vap->iv_flags & IEEE80211_F_WME) {
3694 struct ieee80211_wme_state *wme = &ic->ic_wme;
3697 * Check for aggressive mode change. When there is
3698 * significant high priority traffic in the BSS
3699 * throttle back BE traffic by using conservative
3700 * parameters. Otherwise BE uses aggressive params
3701 * to optimize performance of legacy/non-QoS traffic.
3703 if (wme->wme_flags & WME_F_AGGRMODE) {
3704 if (wme->wme_hipri_traffic >
3705 wme->wme_hipri_switch_thresh) {
3706 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3707 "%s: traffic %u, disable aggressive mode\n",
3708 __func__, wme->wme_hipri_traffic);
3709 wme->wme_flags &= ~WME_F_AGGRMODE;
3710 ieee80211_wme_updateparams_locked(vap);
3711 wme->wme_hipri_traffic =
3712 wme->wme_hipri_switch_hysteresis;
3714 wme->wme_hipri_traffic = 0;
3716 if (wme->wme_hipri_traffic <=
3717 wme->wme_hipri_switch_thresh) {
3718 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3719 "%s: traffic %u, enable aggressive mode\n",
3720 __func__, wme->wme_hipri_traffic);
3721 wme->wme_flags |= WME_F_AGGRMODE;
3722 ieee80211_wme_updateparams_locked(vap);
3723 wme->wme_hipri_traffic = 0;
3725 wme->wme_hipri_traffic =
3726 wme->wme_hipri_switch_hysteresis;
3728 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3729 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
3730 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3734 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3735 ieee80211_ht_update_beacon(vap, bo);
3736 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3738 #ifdef IEEE80211_SUPPORT_TDMA
3739 if (vap->iv_caps & IEEE80211_C_TDMA) {
3741 * NB: the beacon is potentially updated every TBTT.
3743 ieee80211_tdma_update_beacon(vap, bo);
3746 #ifdef IEEE80211_SUPPORT_MESH
3747 if (vap->iv_opmode == IEEE80211_M_MBSS)
3748 ieee80211_mesh_update_beacon(vap, bo);
3751 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3752 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3753 struct ieee80211_tim_ie *tie =
3754 (struct ieee80211_tim_ie *) bo->bo_tim;
3755 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3756 u_int timlen, timoff, i;
3758 * ATIM/DTIM needs updating. If it fits in the
3759 * current space allocated then just copy in the
3760 * new bits. Otherwise we need to move any trailing
3761 * data to make room. Note that we know there is
3762 * contiguous space because ieee80211_beacon_allocate
3763 * insures there is space in the mbuf to write a
3764 * maximal-size virtual bitmap (based on iv_max_aid).
3767 * Calculate the bitmap size and offset, copy any
3768 * trailer out of the way, and then copy in the
3769 * new bitmap and update the information element.
3770 * Note that the tim bitmap must contain at least
3771 * one byte and any offset must be even.
3773 if (vap->iv_ps_pending != 0) {
3774 timoff = 128; /* impossibly large */
3775 for (i = 0; i < vap->iv_tim_len; i++)
3776 if (vap->iv_tim_bitmap[i]) {
3780 KASSERT(timoff != 128, ("tim bitmap empty!"));
3781 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3782 if (vap->iv_tim_bitmap[i])
3784 timlen = 1 + (i - timoff);
3791 * TODO: validate this!
3793 if (timlen != bo->bo_tim_len) {
3794 /* copy up/down trailer */
3795 int adjust = tie->tim_bitmap+timlen
3796 - bo->bo_tim_trailer;
3797 ovbcopy(bo->bo_tim_trailer,
3798 bo->bo_tim_trailer+adjust,
3799 bo->bo_tim_trailer_len);
3800 bo->bo_tim_trailer += adjust;
3801 bo->bo_erp += adjust;
3802 bo->bo_htinfo += adjust;
3803 bo->bo_vhtinfo += adjust;
3804 #ifdef IEEE80211_SUPPORT_SUPERG
3805 bo->bo_ath += adjust;
3807 #ifdef IEEE80211_SUPPORT_TDMA
3808 bo->bo_tdma += adjust;
3810 #ifdef IEEE80211_SUPPORT_MESH
3811 bo->bo_meshconf += adjust;
3813 bo->bo_appie += adjust;
3814 bo->bo_wme += adjust;
3815 bo->bo_csa += adjust;
3816 bo->bo_quiet += adjust;
3817 bo->bo_tim_len = timlen;
3819 /* update information element */
3820 tie->tim_len = 3 + timlen;
3821 tie->tim_bitctl = timoff;
3824 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3827 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3829 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3830 "%s: TIM updated, pending %u, off %u, len %u\n",
3831 __func__, vap->iv_ps_pending, timoff, timlen);
3833 /* count down DTIM period */
3834 if (tie->tim_count == 0)
3835 tie->tim_count = tie->tim_period - 1;
3838 /* update state for buffered multicast frames on DTIM */
3839 if (mcast && tie->tim_count == 0)
3840 tie->tim_bitctl |= 1;
3842 tie->tim_bitctl &= ~1;
3843 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3844 struct ieee80211_csa_ie *csa =
3845 (struct ieee80211_csa_ie *) bo->bo_csa;
3848 * Insert or update CSA ie. If we're just starting
3849 * to count down to the channel switch then we need
3850 * to insert the CSA ie. Otherwise we just need to
3851 * drop the count. The actual change happens above
3852 * when the vap's count reaches the target count.
3854 if (vap->iv_csa_count == 0) {
3855 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3856 bo->bo_erp += sizeof(*csa);
3857 bo->bo_htinfo += sizeof(*csa);
3858 bo->bo_vhtinfo += sizeof(*csa);
3859 bo->bo_wme += sizeof(*csa);
3860 #ifdef IEEE80211_SUPPORT_SUPERG
3861 bo->bo_ath += sizeof(*csa);
3863 #ifdef IEEE80211_SUPPORT_TDMA
3864 bo->bo_tdma += sizeof(*csa);
3866 #ifdef IEEE80211_SUPPORT_MESH
3867 bo->bo_meshconf += sizeof(*csa);
3869 bo->bo_appie += sizeof(*csa);
3870 bo->bo_csa_trailer_len += sizeof(*csa);
3871 bo->bo_quiet += sizeof(*csa);
3872 bo->bo_tim_trailer_len += sizeof(*csa);
3873 m->m_len += sizeof(*csa);
3874 m->m_pkthdr.len += sizeof(*csa);
3876 ieee80211_add_csa(bo->bo_csa, vap);
3879 vap->iv_csa_count++;
3880 /* NB: don't clear IEEE80211_BEACON_CSA */
3884 * Only add the quiet time IE if we've enabled it
3887 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3888 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3889 if (vap->iv_quiet &&
3890 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
3891 ieee80211_add_quiet(bo->bo_quiet, vap, 1);
3894 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3896 * ERP element needs updating.
3898 (void) ieee80211_add_erp(bo->bo_erp, ic);
3899 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3901 #ifdef IEEE80211_SUPPORT_SUPERG
3902 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3903 ieee80211_add_athcaps(bo->bo_ath, ni);
3904 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3908 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3909 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3915 aielen += aie->ie_len;
3916 if (aielen != bo->bo_appie_len) {
3917 /* copy up/down trailer */
3918 int adjust = aielen - bo->bo_appie_len;
3919 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3920 bo->bo_tim_trailer_len);
3921 bo->bo_tim_trailer += adjust;
3922 bo->bo_appie += adjust;
3923 bo->bo_appie_len = aielen;
3929 frm = add_appie(frm, aie);
3930 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3932 IEEE80211_UNLOCK(ic);
3938 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3939 * tunnel encapsulation. The frame is assumed to have an Ethernet
3940 * header at the front that must be stripped before prepending the
3941 * LLC followed by the Ethernet header passed in (with an Ethernet
3942 * type that specifies the payload size).
3945 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3946 const struct ether_header *eh)
3951 /* XXX optimize by combining m_adj+M_PREPEND */
3952 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3953 llc = mtod(m, struct llc *);
3954 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3955 llc->llc_control = LLC_UI;
3956 llc->llc_snap.org_code[0] = 0;
3957 llc->llc_snap.org_code[1] = 0;
3958 llc->llc_snap.org_code[2] = 0;
3959 llc->llc_snap.ether_type = eh->ether_type;
3960 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3962 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3963 if (m == NULL) { /* XXX cannot happen */
3964 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3965 "%s: no space for ether_header\n", __func__);
3966 vap->iv_stats.is_tx_nobuf++;
3969 ETHER_HEADER_COPY(mtod(m, void *), eh);
3970 mtod(m, struct ether_header *)->ether_type = htons(payload);
3975 * Complete an mbuf transmission.
3977 * For now, this simply processes a completed frame after the
3978 * driver has completed it's transmission and/or retransmission.
3979 * It assumes the frame is an 802.11 encapsulated frame.
3981 * Later on it will grow to become the exit path for a given frame
3982 * from the driver and, depending upon how it's been encapsulated
3983 * and already transmitted, it may end up doing A-MPDU retransmission,
3984 * power save requeuing, etc.
3986 * In order for the above to work, the driver entry point to this
3987 * must not hold any driver locks. Thus, the driver needs to delay
3988 * any actual mbuf completion until it can release said locks.
3990 * This frees the mbuf and if the mbuf has a node reference,
3991 * the node reference will be freed.
3994 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3998 struct ifnet *ifp = ni->ni_vap->iv_ifp;
4001 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
4002 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
4003 if (m->m_flags & M_MCAST)
4004 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4006 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4007 if (m->m_flags & M_TXCB)
4008 ieee80211_process_callback(ni, m, status);
4009 ieee80211_free_node(ni);