2 * SPDX-License-Identifier: BSD-2-Clause
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_private.h>
52 #include <net/if_vlan_var.h>
54 #include <net80211/ieee80211_var.h>
55 #include <net80211/ieee80211_regdomain.h>
56 #ifdef IEEE80211_SUPPORT_SUPERG
57 #include <net80211/ieee80211_superg.h>
59 #ifdef IEEE80211_SUPPORT_TDMA
60 #include <net80211/ieee80211_tdma.h>
62 #include <net80211/ieee80211_wds.h>
63 #include <net80211/ieee80211_mesh.h>
64 #include <net80211/ieee80211_vht.h>
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
76 #include <netinet/ip6.h>
79 #include <security/mac/mac_framework.h>
81 #define ETHER_HEADER_COPY(dst, src) \
82 memcpy(dst, src, sizeof(struct ether_header))
84 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
85 u_int hdrsize, u_int ciphdrsize, u_int mtu);
86 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
88 #ifdef IEEE80211_DEBUG
90 * Decide if an outbound management frame should be
91 * printed when debugging is enabled. This filters some
92 * of the less interesting frames that come frequently
96 doprint(struct ieee80211vap *vap, int subtype)
99 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
100 return (vap->iv_opmode == IEEE80211_M_IBSS);
107 * Transmit a frame to the given destination on the given VAP.
109 * It's up to the caller to figure out the details of who this
110 * is going to and resolving the node.
112 * This routine takes care of queuing it for power save,
113 * A-MPDU state stuff, fast-frames state stuff, encapsulation
114 * if required, then passing it up to the driver layer.
116 * This routine (for now) consumes the mbuf and frees the node
117 * reference; it ideally will return a TX status which reflects
118 * whether the mbuf was consumed or not, so the caller can
119 * free the mbuf (if appropriate) and the node reference (again,
123 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
124 struct ieee80211_node *ni)
126 struct ieee80211com *ic = vap->iv_ic;
127 struct ifnet *ifp = vap->iv_ifp;
130 #ifdef IEEE80211_SUPPORT_SUPERG
132 int do_ampdu_amsdu = 0;
133 int no_ampdu = 1; /* Will be set to 0 if ampdu is active */
137 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
138 (m->m_flags & M_PWR_SAV) == 0) {
140 * Station in power save mode; pass the frame
141 * to the 802.11 layer and continue. We'll get
142 * the frame back when the time is right.
143 * XXX lose WDS vap linkage?
145 if (ieee80211_pwrsave(ni, m) != 0)
146 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
147 ieee80211_free_node(ni);
150 * We queued it fine, so tell the upper layer
151 * that we consumed it.
155 /* calculate priority so drivers can find the tx queue */
156 if (ieee80211_classify(ni, m)) {
157 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
158 ni->ni_macaddr, NULL,
159 "%s", "classification failure");
160 vap->iv_stats.is_tx_classify++;
161 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
163 ieee80211_free_node(ni);
165 /* XXX better status? */
169 * Stash the node pointer. Note that we do this after
170 * any call to ieee80211_dwds_mcast because that code
171 * uses any existing value for rcvif to identify the
172 * interface it (might have been) received on.
174 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
175 m->m_pkthdr.rcvif = (void *)ni;
176 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
178 BPF_MTAP(ifp, m); /* 802.3 tx */
181 * Figure out if we can do A-MPDU, A-MSDU or FF.
183 * A-MPDU depends upon vap/node config.
184 * A-MSDU depends upon vap/node config.
185 * FF depends upon vap config, IE and whether
186 * it's 11abg (and not 11n/11ac/etc.)
188 * Note that these flags indiciate whether we can do
189 * it at all, rather than the situation (eg traffic type.)
191 do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
192 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX));
193 #ifdef IEEE80211_SUPPORT_SUPERG
194 do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
195 (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX));
197 ((ni->ni_flags & IEEE80211_NODE_HT) == 0) &&
198 ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) &&
199 (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF));
203 * Check if A-MPDU tx aggregation is setup or if we
204 * should try to enable it. The sta must be associated
205 * with HT and A-MPDU enabled for use. When the policy
206 * routine decides we should enable A-MPDU we issue an
207 * ADDBA request and wait for a reply. The frame being
208 * encapsulated will go out w/o using A-MPDU, or possibly
209 * it might be collected by the driver and held/retransmit.
210 * The default ic_ampdu_enable routine handles staggering
211 * ADDBA requests in case the receiver NAK's us or we are
212 * otherwise unable to establish a BA stream.
214 * Don't treat group-addressed frames as candidates for aggregation;
215 * net80211 doesn't support 802.11aa-2012 and so group addressed
216 * frames will always have sequence numbers allocated from the NON_QOS
220 if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
221 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
222 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
224 ieee80211_txampdu_count_packet(tap);
225 if (IEEE80211_AMPDU_RUNNING(tap)) {
227 * Operational, mark frame for aggregation.
229 * XXX do tx aggregation here
231 m->m_flags |= M_AMPDU_MPDU;
232 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
233 ic->ic_ampdu_enable(ni, tap)) {
235 * Not negotiated yet, request service.
237 ieee80211_ampdu_request(ni, tap);
238 /* XXX hold frame for reply? */
241 * Now update the no-ampdu flag. A-MPDU may have been
242 * started or administratively disabled above; so now we
243 * know whether we're running yet or not.
245 * This will let us know whether we should be doing A-MSDU
246 * at this point. We only do A-MSDU if we're either not
247 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU
250 * Whilst here, update the amsdu-ampdu flag. The above may
251 * have also set or cleared the amsdu-in-ampdu txa_flags
252 * combination so we can correctly do A-MPDU + A-MSDU.
254 #ifdef IEEE80211_SUPPORT_SUPERG
255 no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap)
256 || (IEEE80211_AMPDU_NACKED(tap)));
257 do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap);
262 #ifdef IEEE80211_SUPPORT_SUPERG
264 * Check for AMSDU/FF; queue for aggregation
266 * Note: we don't bother trying to do fast frames or
267 * A-MSDU encapsulation for 802.3 drivers. Now, we
268 * likely could do it for FF (because it's a magic
269 * atheros tunnel LLC type) but I don't think we're going
270 * to really need to. For A-MSDU we'd have to set the
271 * A-MSDU QoS bit in the wifi header, so we just plain
274 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
276 (do_ampdu_amsdu || (no_ampdu && do_amsdu)) &&
277 ieee80211_amsdu_tx_ok(ni)) {
278 m = ieee80211_amsdu_check(ni, m);
280 /* NB: any ni ref held on stageq */
281 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
282 "%s: amsdu_check queued frame\n",
286 } else if ((! mcast) && do_ff) {
287 m = ieee80211_ff_check(ni, m);
289 /* NB: any ni ref held on stageq */
290 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
291 "%s: ff_check queued frame\n",
297 #endif /* IEEE80211_SUPPORT_SUPERG */
300 * Grab the TX lock - serialise the TX process from this
301 * point (where TX state is being checked/modified)
302 * through to driver queue.
304 IEEE80211_TX_LOCK(ic);
307 * XXX make the encap and transmit code a separate function
308 * so things like the FF (and later A-MSDU) path can just call
309 * it for flushed frames.
311 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
313 * Encapsulate the packet in prep for transmission.
315 m = ieee80211_encap(vap, ni, m);
317 /* NB: stat+msg handled in ieee80211_encap */
318 IEEE80211_TX_UNLOCK(ic);
319 ieee80211_free_node(ni);
320 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
324 (void) ieee80211_parent_xmitpkt(ic, m);
327 * Unlock at this point - no need to hold it across
328 * ieee80211_free_node() (ie, the comlock)
330 IEEE80211_TX_UNLOCK(ic);
331 ic->ic_lastdata = ticks;
337 * Send the given mbuf through the given vap.
339 * This consumes the mbuf regardless of whether the transmit
340 * was successful or not.
342 * This does none of the initial checks that ieee80211_start()
343 * does (eg CAC timeout, interface wakeup) - the caller must
347 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
349 #define IS_DWDS(vap) \
350 (vap->iv_opmode == IEEE80211_M_WDS && \
351 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
352 struct ieee80211com *ic = vap->iv_ic;
353 struct ifnet *ifp = vap->iv_ifp;
354 struct ieee80211_node *ni;
355 struct ether_header *eh;
358 * Cancel any background scan.
360 if (ic->ic_flags & IEEE80211_F_SCAN)
361 ieee80211_cancel_anyscan(vap);
363 * Find the node for the destination so we can do
364 * things like power save and fast frames aggregation.
366 * NB: past this point various code assumes the first
367 * mbuf has the 802.3 header present (and contiguous).
370 if (m->m_len < sizeof(struct ether_header) &&
371 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
372 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
373 "discard frame, %s\n", "m_pullup failed");
374 vap->iv_stats.is_tx_nobuf++; /* XXX */
375 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
378 eh = mtod(m, struct ether_header *);
379 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
382 * Only unicast frames from the above go out
383 * DWDS vaps; multicast frames are handled by
384 * dispatching the frame as it comes through
385 * the AP vap (see below).
387 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
388 eh->ether_dhost, "mcast", "%s", "on DWDS");
389 vap->iv_stats.is_dwds_mcast++;
391 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
392 /* XXX better status? */
395 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
397 * Spam DWDS vap's w/ multicast traffic.
399 /* XXX only if dwds in use? */
400 ieee80211_dwds_mcast(vap, m);
403 #ifdef IEEE80211_SUPPORT_MESH
404 if (vap->iv_opmode != IEEE80211_M_MBSS) {
406 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
408 /* NB: ieee80211_find_txnode does stat+msg */
409 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
411 /* XXX better status? */
414 if (ni->ni_associd == 0 &&
415 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
416 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
417 eh->ether_dhost, NULL,
418 "sta not associated (type 0x%04x)",
419 htons(eh->ether_type));
420 vap->iv_stats.is_tx_notassoc++;
421 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
423 ieee80211_free_node(ni);
424 /* XXX better status? */
427 #ifdef IEEE80211_SUPPORT_MESH
429 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
431 * Proxy station only if configured.
433 if (!ieee80211_mesh_isproxyena(vap)) {
434 IEEE80211_DISCARD_MAC(vap,
435 IEEE80211_MSG_OUTPUT |
437 eh->ether_dhost, NULL,
438 "%s", "proxy not enabled");
439 vap->iv_stats.is_mesh_notproxy++;
440 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
442 /* XXX better status? */
445 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
446 "forward frame from DS SA(%6D), DA(%6D)\n",
447 eh->ether_shost, ":",
448 eh->ether_dhost, ":");
449 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
451 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
454 * NB: ieee80211_mesh_discover holds/disposes
455 * frame (e.g. queueing on path discovery).
457 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
458 /* XXX better status? */
465 * We've resolved the sender, so attempt to transmit it.
468 if (vap->iv_state == IEEE80211_S_SLEEP) {
470 * In power save; queue frame and then wakeup device
473 ic->ic_lastdata = ticks;
474 if (ieee80211_pwrsave(ni, m) != 0)
475 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
476 ieee80211_free_node(ni);
477 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
481 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
488 * Start method for vap's. All packets from the stack come
489 * through here. We handle common processing of the packets
490 * before dispatching them to the underlying device.
492 * if_transmit() requires that the mbuf be consumed by this call
493 * regardless of the return condition.
496 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
498 struct ieee80211vap *vap = ifp->if_softc;
499 struct ieee80211com *ic = vap->iv_ic;
502 * No data frames go out unless we're running.
503 * Note in particular this covers CAC and CSA
504 * states (though maybe we should check muting
507 if (vap->iv_state != IEEE80211_S_RUN &&
508 vap->iv_state != IEEE80211_S_SLEEP) {
510 /* re-check under the com lock to avoid races */
511 if (vap->iv_state != IEEE80211_S_RUN &&
512 vap->iv_state != IEEE80211_S_SLEEP) {
513 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
514 "%s: ignore queue, in %s state\n",
515 __func__, ieee80211_state_name[vap->iv_state]);
516 vap->iv_stats.is_tx_badstate++;
517 IEEE80211_UNLOCK(ic);
518 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
520 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
523 IEEE80211_UNLOCK(ic);
527 * Sanitize mbuf flags for net80211 use. We cannot
528 * clear M_PWR_SAV or M_MORE_DATA because these may
529 * be set for frames that are re-submitted from the
532 * NB: This must be done before ieee80211_classify as
533 * it marks EAPOL in frames with M_EAPOL.
535 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
538 * Bump to the packet transmission path.
539 * The mbuf will be consumed here.
541 return (ieee80211_start_pkt(vap, m));
545 ieee80211_vap_qflush(struct ifnet *ifp)
552 * 802.11 raw output routine.
554 * XXX TODO: this (and other send routines) should correctly
555 * XXX keep the pwr mgmt bit set if it decides to call into the
556 * XXX driver to send a frame whilst the state is SLEEP.
558 * Otherwise the peer may decide that we're awake and flood us
559 * with traffic we are still too asleep to receive!
562 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
563 struct mbuf *m, const struct ieee80211_bpf_params *params)
565 struct ieee80211com *ic = vap->iv_ic;
569 * Set node - the caller has taken a reference, so ensure
570 * that the mbuf has the same node value that
571 * it would if it were going via the normal path.
573 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
574 m->m_pkthdr.rcvif = (void *)ni;
577 * Attempt to add bpf transmit parameters.
579 * For now it's ok to fail; the raw_xmit api still takes
582 * Later on when ic_raw_xmit() has params removed,
583 * they'll have to be added - so fail the transmit if
587 (void) ieee80211_add_xmit_params(m, params);
589 error = ic->ic_raw_xmit(ni, m, params);
591 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
592 ieee80211_free_node(ni);
598 ieee80211_validate_frame(struct mbuf *m,
599 const struct ieee80211_bpf_params *params)
601 struct ieee80211_frame *wh;
604 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
607 wh = mtod(m, struct ieee80211_frame *);
608 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
609 IEEE80211_FC0_VERSION_0)
612 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
613 if (type != IEEE80211_FC0_TYPE_DATA) {
614 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
615 IEEE80211_FC1_DIR_NODS)
618 if (type != IEEE80211_FC0_TYPE_MGT &&
619 (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
622 /* XXX skip other field checks? */
625 if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
626 (IEEE80211_IS_PROTECTED(wh))) {
629 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
632 * See IEEE Std 802.11-2012,
633 * 8.2.4.1.9 'Protected Frame field'
635 /* XXX no support for robust management frames yet. */
636 if (!(type == IEEE80211_FC0_TYPE_DATA ||
637 (type == IEEE80211_FC0_TYPE_MGT &&
638 subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
641 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
644 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
651 ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
653 struct ieee80211com *ic = ni->ni_ic;
655 if (IEEE80211_IS_HT_RATE(rate)) {
656 if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
659 rate = IEEE80211_RV(rate);
661 if (rate > ic->ic_txstream * 8 - 1)
668 if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
674 if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
677 switch (ic->ic_txstream) {
700 if (!ieee80211_isratevalid(ic->ic_rt, rate))
707 ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
708 const struct ieee80211_bpf_params *params)
713 return (0); /* nothing to do */
715 /* NB: most drivers assume that ibp_rate0 is set (!= 0). */
716 if (params->ibp_rate0 != 0) {
717 error = ieee80211_validate_rate(ni, params->ibp_rate0);
721 /* XXX pre-setup some default (e.g., mgmt / mcast) rate */
726 if (params->ibp_rate1 != 0 &&
727 (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
730 if (params->ibp_rate2 != 0 &&
731 (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
734 if (params->ibp_rate3 != 0 &&
735 (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
742 * 802.11 output routine. This is (currently) used only to
743 * connect bpf write calls to the 802.11 layer for injecting
747 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
748 const struct sockaddr *dst, struct route *ro)
750 #define senderr(e) do { error = (e); goto bad;} while (0)
751 const struct ieee80211_bpf_params *params = NULL;
752 struct ieee80211_node *ni = NULL;
753 struct ieee80211vap *vap;
754 struct ieee80211_frame *wh;
755 struct ieee80211com *ic = NULL;
759 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
761 * Short-circuit requests if the vap is marked OACTIVE
762 * as this can happen because a packet came down through
763 * ieee80211_start before the vap entered RUN state in
764 * which case it's ok to just drop the frame. This
765 * should not be necessary but callers of if_output don't
773 * Hand to the 802.3 code if not tagged as
774 * a raw 802.11 frame.
776 if (dst->sa_family != AF_IEEE80211)
777 return vap->iv_output(ifp, m, dst, ro);
779 error = mac_ifnet_check_transmit(ifp, m);
783 if (ifp->if_flags & IFF_MONITOR)
785 if (!IFNET_IS_UP_RUNNING(ifp))
787 if (vap->iv_state == IEEE80211_S_CAC) {
788 IEEE80211_DPRINTF(vap,
789 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
790 "block %s frame in CAC state\n", "raw data");
791 vap->iv_stats.is_tx_badstate++;
792 senderr(EIO); /* XXX */
793 } else if (vap->iv_state == IEEE80211_S_SCAN)
795 /* XXX bypass bridge, pfil, carp, etc. */
798 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
799 * present by setting the sa_len field of the sockaddr (yes,
801 * NB: we assume sa_data is suitably aligned to cast.
803 if (dst->sa_len != 0)
804 params = (const struct ieee80211_bpf_params *)dst->sa_data;
806 error = ieee80211_validate_frame(m, params);
810 wh = mtod(m, struct ieee80211_frame *);
812 /* locate destination node */
813 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
814 case IEEE80211_FC1_DIR_NODS:
815 case IEEE80211_FC1_DIR_FROMDS:
816 ni = ieee80211_find_txnode(vap, wh->i_addr1);
818 case IEEE80211_FC1_DIR_TODS:
819 case IEEE80211_FC1_DIR_DSTODS:
820 ni = ieee80211_find_txnode(vap, wh->i_addr3);
827 * Permit packets w/ bpf params through regardless
828 * (see below about sa_len).
830 if (dst->sa_len == 0)
831 senderr(EHOSTUNREACH);
832 ni = ieee80211_ref_node(vap->iv_bss);
836 * Sanitize mbuf for net80211 flags leaked from above.
838 * NB: This must be done before ieee80211_classify as
839 * it marks EAPOL in frames with M_EAPOL.
841 m->m_flags &= ~M_80211_TX;
842 m->m_flags |= M_ENCAP; /* mark encapsulated */
844 if (IEEE80211_IS_DATA(wh)) {
845 /* calculate priority so drivers can find the tx queue */
846 if (ieee80211_classify(ni, m))
847 senderr(EIO); /* XXX */
849 /* NB: ieee80211_encap does not include 802.11 header */
850 IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
851 m->m_pkthdr.len - ieee80211_hdrsize(wh));
853 M_WME_SETAC(m, WME_AC_BE);
855 error = ieee80211_sanitize_rates(ni, m, params);
859 IEEE80211_NODE_STAT(ni, tx_data);
860 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
861 IEEE80211_NODE_STAT(ni, tx_mcast);
862 m->m_flags |= M_MCAST;
864 IEEE80211_NODE_STAT(ni, tx_ucast);
866 IEEE80211_TX_LOCK(ic);
867 ret = ieee80211_raw_output(vap, ni, m, params);
868 IEEE80211_TX_UNLOCK(ic);
874 ieee80211_free_node(ni);
875 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
881 * Set the direction field and address fields of an outgoing
882 * frame. Note this should be called early on in constructing
883 * a frame as it sets i_fc[1]; other bits can then be or'd in.
886 ieee80211_send_setup(
887 struct ieee80211_node *ni,
890 const uint8_t sa[IEEE80211_ADDR_LEN],
891 const uint8_t da[IEEE80211_ADDR_LEN],
892 const uint8_t bssid[IEEE80211_ADDR_LEN])
894 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
895 struct ieee80211vap *vap = ni->ni_vap;
896 struct ieee80211_tx_ampdu *tap;
897 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
900 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
902 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
903 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
904 switch (vap->iv_opmode) {
905 case IEEE80211_M_STA:
906 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
907 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
908 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
909 IEEE80211_ADDR_COPY(wh->i_addr3, da);
911 case IEEE80211_M_IBSS:
912 case IEEE80211_M_AHDEMO:
913 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
914 IEEE80211_ADDR_COPY(wh->i_addr1, da);
915 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
916 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
918 case IEEE80211_M_HOSTAP:
919 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
920 IEEE80211_ADDR_COPY(wh->i_addr1, da);
921 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
922 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
924 case IEEE80211_M_WDS:
925 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
926 IEEE80211_ADDR_COPY(wh->i_addr1, da);
927 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
928 IEEE80211_ADDR_COPY(wh->i_addr3, da);
929 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
931 case IEEE80211_M_MBSS:
932 #ifdef IEEE80211_SUPPORT_MESH
933 if (IEEE80211_IS_MULTICAST(da)) {
934 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
936 IEEE80211_ADDR_COPY(wh->i_addr1, da);
937 IEEE80211_ADDR_COPY(wh->i_addr2,
940 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
941 IEEE80211_ADDR_COPY(wh->i_addr1, da);
942 IEEE80211_ADDR_COPY(wh->i_addr2,
944 IEEE80211_ADDR_COPY(wh->i_addr3, da);
945 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
949 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
953 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
954 IEEE80211_ADDR_COPY(wh->i_addr1, da);
955 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
956 #ifdef IEEE80211_SUPPORT_MESH
957 if (vap->iv_opmode == IEEE80211_M_MBSS)
958 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
961 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
963 *(uint16_t *)&wh->i_dur[0] = 0;
966 * XXX TODO: this is what the TX lock is for.
967 * Here we're incrementing sequence numbers, and they
968 * need to be in lock-step with what the driver is doing
969 * both in TX ordering and crypto encap (IV increment.)
971 * If the driver does seqno itself, then we can skip
972 * assigning sequence numbers here, and we can avoid
973 * requiring the TX lock.
975 tap = &ni->ni_tx_ampdu[tid];
976 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
977 m->m_flags |= M_AMPDU_MPDU;
979 /* NB: zero out i_seq field (for s/w encryption etc) */
980 *(uint16_t *)&wh->i_seq[0] = 0;
982 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
983 type & IEEE80211_FC0_SUBTYPE_MASK))
985 * 802.11-2012 9.3.2.10 - QoS multicast frames
986 * come out of a different seqno space.
988 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
989 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
991 seqno = ni->ni_txseqs[tid]++;
996 *(uint16_t *)&wh->i_seq[0] =
997 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
998 M_SEQNO_SET(m, seqno);
1001 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1002 m->m_flags |= M_MCAST;
1007 * Send a management frame to the specified node. The node pointer
1008 * must have a reference as the pointer will be passed to the driver
1009 * and potentially held for a long time. If the frame is successfully
1010 * dispatched to the driver, then it is responsible for freeing the
1011 * reference (and potentially free'ing up any associated storage);
1012 * otherwise deal with reclaiming any reference (on error).
1015 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
1016 struct ieee80211_bpf_params *params)
1018 struct ieee80211vap *vap = ni->ni_vap;
1019 struct ieee80211com *ic = ni->ni_ic;
1020 struct ieee80211_frame *wh;
1023 KASSERT(ni != NULL, ("null node"));
1025 if (vap->iv_state == IEEE80211_S_CAC) {
1026 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1027 ni, "block %s frame in CAC state",
1028 ieee80211_mgt_subtype_name(type));
1029 vap->iv_stats.is_tx_badstate++;
1030 ieee80211_free_node(ni);
1032 return EIO; /* XXX */
1035 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
1037 ieee80211_free_node(ni);
1041 IEEE80211_TX_LOCK(ic);
1043 wh = mtod(m, struct ieee80211_frame *);
1044 ieee80211_send_setup(ni, m,
1045 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1046 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1047 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1048 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1049 "encrypting frame (%s)", __func__);
1050 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1052 m->m_flags |= M_ENCAP; /* mark encapsulated */
1054 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
1055 M_WME_SETAC(m, params->ibp_pri);
1057 #ifdef IEEE80211_DEBUG
1058 /* avoid printing too many frames */
1059 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1060 ieee80211_msg_dumppkts(vap)) {
1061 ieee80211_note(vap, "[%s] send %s on channel %u\n",
1062 ether_sprintf(wh->i_addr1),
1063 ieee80211_mgt_subtype_name(type),
1064 ieee80211_chan2ieee(ic, ic->ic_curchan));
1067 IEEE80211_NODE_STAT(ni, tx_mgmt);
1069 ret = ieee80211_raw_output(vap, ni, m, params);
1070 IEEE80211_TX_UNLOCK(ic);
1075 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
1078 struct ieee80211vap *vap = ni->ni_vap;
1084 * Send a null data frame to the specified node. If the station
1085 * is setup for QoS then a QoS Null Data frame is constructed.
1086 * If this is a WDS station then a 4-address frame is constructed.
1088 * NB: the caller is assumed to have setup a node reference
1089 * for use; this is necessary to deal with a race condition
1090 * when probing for inactive stations. Like ieee80211_mgmt_output
1091 * we must cleanup any node reference on error; however we
1092 * can safely just unref it as we know it will never be the
1093 * last reference to the node.
1096 ieee80211_send_nulldata(struct ieee80211_node *ni)
1098 struct ieee80211vap *vap = ni->ni_vap;
1099 struct ieee80211com *ic = ni->ni_ic;
1101 struct ieee80211_frame *wh;
1106 if (vap->iv_state == IEEE80211_S_CAC) {
1107 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1108 ni, "block %s frame in CAC state", "null data");
1109 ieee80211_unref_node(&ni);
1110 vap->iv_stats.is_tx_badstate++;
1111 return EIO; /* XXX */
1114 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1115 hdrlen = sizeof(struct ieee80211_qosframe);
1117 hdrlen = sizeof(struct ieee80211_frame);
1118 /* NB: only WDS vap's get 4-address frames */
1119 if (vap->iv_opmode == IEEE80211_M_WDS)
1120 hdrlen += IEEE80211_ADDR_LEN;
1121 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1122 hdrlen = roundup(hdrlen, sizeof(uint32_t));
1124 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1127 ieee80211_unref_node(&ni);
1128 vap->iv_stats.is_tx_nobuf++;
1131 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1132 ("leading space %zd", M_LEADINGSPACE(m)));
1133 M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT);
1135 /* NB: cannot happen */
1136 ieee80211_free_node(ni);
1140 IEEE80211_TX_LOCK(ic);
1142 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
1143 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1144 const int tid = WME_AC_TO_TID(WME_AC_BE);
1147 ieee80211_send_setup(ni, m,
1148 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1149 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1151 if (vap->iv_opmode == IEEE80211_M_WDS)
1152 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1154 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1155 qos[0] = tid & IEEE80211_QOS_TID;
1156 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1157 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1160 ieee80211_send_setup(ni, m,
1161 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1162 IEEE80211_NONQOS_TID,
1163 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1165 if (vap->iv_opmode != IEEE80211_M_WDS) {
1166 /* NB: power management bit is never sent by an AP */
1167 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1168 vap->iv_opmode != IEEE80211_M_HOSTAP)
1169 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1171 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1172 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1173 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1176 m->m_len = m->m_pkthdr.len = hdrlen;
1177 m->m_flags |= M_ENCAP; /* mark encapsulated */
1179 M_WME_SETAC(m, WME_AC_BE);
1181 IEEE80211_NODE_STAT(ni, tx_data);
1183 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1184 "send %snull data frame on channel %u, pwr mgt %s",
1185 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1186 ieee80211_chan2ieee(ic, ic->ic_curchan),
1187 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1189 ret = ieee80211_raw_output(vap, ni, m, NULL);
1190 IEEE80211_TX_UNLOCK(ic);
1195 * Assign priority to a frame based on any vlan tag assigned
1196 * to the station and/or any Diffserv setting in an IP header.
1197 * Finally, if an ACM policy is setup (in station mode) it's
1201 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1203 const struct ether_header *eh = NULL;
1204 uint16_t ether_type;
1205 int v_wme_ac, d_wme_ac, ac;
1207 if (__predict_false(m->m_flags & M_ENCAP)) {
1208 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1210 int hdrlen, subtype;
1212 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1213 if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1218 hdrlen = ieee80211_hdrsize(wh);
1219 if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1222 llc = (struct llc *)mtodo(m, hdrlen);
1223 if (llc->llc_dsap != LLC_SNAP_LSAP ||
1224 llc->llc_ssap != LLC_SNAP_LSAP ||
1225 llc->llc_control != LLC_UI ||
1226 llc->llc_snap.org_code[0] != 0 ||
1227 llc->llc_snap.org_code[1] != 0 ||
1228 llc->llc_snap.org_code[2] != 0)
1231 ether_type = llc->llc_snap.ether_type;
1233 eh = mtod(m, struct ether_header *);
1234 ether_type = eh->ether_type;
1238 * Always promote PAE/EAPOL frames to high priority.
1240 if (ether_type == htons(ETHERTYPE_PAE)) {
1241 /* NB: mark so others don't need to check header */
1242 m->m_flags |= M_EAPOL;
1247 * Non-qos traffic goes to BE.
1249 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1255 * If node has a vlan tag then all traffic
1256 * to it must have a matching tag.
1259 if (ni->ni_vlan != 0) {
1260 if ((m->m_flags & M_VLANTAG) == 0) {
1261 IEEE80211_NODE_STAT(ni, tx_novlantag);
1264 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1265 EVL_VLANOFTAG(ni->ni_vlan)) {
1266 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1269 /* map vlan priority to AC */
1270 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1276 /* XXX m_copydata may be too slow for fast path */
1277 switch (ntohs(eh->ether_type)) {
1283 * IP frame, map the DSCP bits from the TOS field.
1285 /* NB: ip header may not be in first mbuf */
1286 m_copydata(m, sizeof(struct ether_header) +
1287 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1288 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1289 d_wme_ac = TID_TO_WME_AC(tos);
1294 case ETHERTYPE_IPV6:
1299 * IPv6 frame, map the DSCP bits from the traffic class field.
1301 m_copydata(m, sizeof(struct ether_header) +
1302 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1304 tos = (uint8_t)(ntohl(flow) >> 20);
1305 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1306 d_wme_ac = TID_TO_WME_AC(tos);
1312 d_wme_ac = WME_AC_BE;
1317 * Use highest priority AC.
1319 if (v_wme_ac > d_wme_ac)
1327 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1328 static const int acmap[4] = {
1329 WME_AC_BK, /* WME_AC_BE */
1330 WME_AC_BK, /* WME_AC_BK */
1331 WME_AC_BE, /* WME_AC_VI */
1332 WME_AC_VI, /* WME_AC_VO */
1334 struct ieee80211com *ic = ni->ni_ic;
1336 while (ac != WME_AC_BK &&
1337 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1346 * Insure there is sufficient contiguous space to encapsulate the
1347 * 802.11 data frame. If room isn't already there, arrange for it.
1348 * Drivers and cipher modules assume we have done the necessary work
1349 * and fail rudely if they don't find the space they need.
1352 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1353 struct ieee80211_key *key, struct mbuf *m)
1355 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1356 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1359 /* XXX belongs in crypto code? */
1360 needed_space += key->wk_cipher->ic_header;
1363 * When crypto is being done in the host we must insure
1364 * the data are writable for the cipher routines; clone
1365 * a writable mbuf chain.
1366 * XXX handle SWMIC specially
1368 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1369 m = m_unshare(m, IEEE80211_M_NOWAIT);
1371 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1372 "%s: cannot get writable mbuf\n", __func__);
1373 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1379 * We know we are called just before stripping an Ethernet
1380 * header and prepending an LLC header. This means we know
1382 * sizeof(struct ether_header) - sizeof(struct llc)
1383 * bytes recovered to which we need additional space for the
1384 * 802.11 header and any crypto header.
1386 /* XXX check trailing space and copy instead? */
1387 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1388 struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type);
1390 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1391 "%s: cannot expand storage\n", __func__);
1392 vap->iv_stats.is_tx_nobuf++;
1396 KASSERT(needed_space <= MHLEN,
1397 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1399 * Setup new mbuf to have leading space to prepend the
1400 * 802.11 header and any crypto header bits that are
1401 * required (the latter are added when the driver calls
1402 * back to ieee80211_crypto_encap to do crypto encapsulation).
1404 /* NB: must be first 'cuz it clobbers m_data */
1405 m_move_pkthdr(n, m);
1406 n->m_len = 0; /* NB: m_gethdr does not set */
1407 n->m_data += needed_space;
1409 * Pull up Ethernet header to create the expected layout.
1410 * We could use m_pullup but that's overkill (i.e. we don't
1411 * need the actual data) and it cannot fail so do it inline
1414 /* NB: struct ether_header is known to be contiguous */
1415 n->m_len += sizeof(struct ether_header);
1416 m->m_len -= sizeof(struct ether_header);
1417 m->m_data += sizeof(struct ether_header);
1419 * Replace the head of the chain.
1425 #undef TO_BE_RECLAIMED
1429 * Return the transmit key to use in sending a unicast frame.
1430 * If a unicast key is set we use that. When no unicast key is set
1431 * we fall back to the default transmit key.
1433 static __inline struct ieee80211_key *
1434 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1435 struct ieee80211_node *ni)
1437 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1438 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1439 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1441 return &vap->iv_nw_keys[vap->iv_def_txkey];
1443 return &ni->ni_ucastkey;
1448 * Return the transmit key to use in sending a multicast frame.
1449 * Multicast traffic always uses the group key which is installed as
1450 * the default tx key.
1452 static __inline struct ieee80211_key *
1453 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1454 struct ieee80211_node *ni)
1456 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1457 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1459 return &vap->iv_nw_keys[vap->iv_def_txkey];
1463 * Encapsulate an outbound data frame. The mbuf chain is updated.
1464 * If an error is encountered NULL is returned. The caller is required
1465 * to provide a node reference and pullup the ethernet header in the
1468 * NB: Packet is assumed to be processed by ieee80211_classify which
1469 * marked EAPOL frames w/ M_EAPOL.
1472 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1475 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1476 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1477 struct ieee80211com *ic = ni->ni_ic;
1478 #ifdef IEEE80211_SUPPORT_MESH
1479 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1480 struct ieee80211_meshcntl_ae10 *mc;
1481 struct ieee80211_mesh_route *rt = NULL;
1484 struct ether_header eh;
1485 struct ieee80211_frame *wh;
1486 struct ieee80211_key *key;
1488 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
1489 ieee80211_seq seqno;
1490 int meshhdrsize, meshae;
1494 IEEE80211_TX_LOCK_ASSERT(ic);
1496 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
1499 * Copy existing Ethernet header to a safe place. The
1500 * rest of the code assumes it's ok to strip it when
1501 * reorganizing state for the final encapsulation.
1503 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1504 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1507 * Insure space for additional headers. First identify
1508 * transmit key to use in calculating any buffer adjustments
1509 * required. This is also used below to do privacy
1510 * encapsulation work. Then calculate the 802.11 header
1511 * size and any padding required by the driver.
1513 * Note key may be NULL if we fall back to the default
1514 * transmit key and that is not set. In that case the
1515 * buffer may not be expanded as needed by the cipher
1516 * routines, but they will/should discard it.
1518 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1519 if (vap->iv_opmode == IEEE80211_M_STA ||
1520 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1521 (vap->iv_opmode == IEEE80211_M_WDS &&
1522 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1523 key = ieee80211_crypto_getucastkey(vap, ni);
1524 } else if ((vap->iv_opmode == IEEE80211_M_WDS) &&
1525 (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1527 * Use ucastkey for DWDS transmit nodes, multicast
1530 * This is required to ensure that multicast frames
1531 * from a DWDS AP to a DWDS STA is encrypted with
1532 * a key that can actually work.
1534 * There's no default key for multicast traffic
1535 * on a DWDS WDS VAP node (note NOT the DWDS enabled
1536 * AP VAP, the dynamically created per-STA WDS node)
1537 * so encap fails and transmit fails.
1539 key = ieee80211_crypto_getucastkey(vap, ni);
1541 key = ieee80211_crypto_getmcastkey(vap, ni);
1543 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1544 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1546 "no default transmit key (%s) deftxkey %u",
1547 __func__, vap->iv_def_txkey);
1548 vap->iv_stats.is_tx_nodefkey++;
1554 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1555 * frames so suppress use. This may be an issue if other
1556 * ap's require all data frames to be QoS-encapsulated
1557 * once negotiated in which case we'll need to make this
1560 * Don't send multicast QoS frames.
1561 * Technically multicast frames can be QoS if all stations in the
1564 * NB: mesh data frames are QoS, including multicast frames.
1567 (((is_mcast == 0) && (ni->ni_flags &
1568 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
1569 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1570 (m->m_flags & M_EAPOL) == 0;
1573 hdrsize = sizeof(struct ieee80211_qosframe);
1575 hdrsize = sizeof(struct ieee80211_frame);
1576 #ifdef IEEE80211_SUPPORT_MESH
1577 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1579 * Mesh data frames are encapsulated according to the
1580 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1581 * o Group Addressed data (aka multicast) originating
1582 * at the local sta are sent w/ 3-address format and
1583 * address extension mode 00
1584 * o Individually Addressed data (aka unicast) originating
1585 * at the local sta are sent w/ 4-address format and
1586 * address extension mode 00
1587 * o Group Addressed data forwarded from a non-mesh sta are
1588 * sent w/ 3-address format and address extension mode 01
1589 * o Individually Address data from another sta are sent
1590 * w/ 4-address format and address extension mode 10
1592 is4addr = 0; /* NB: don't use, disable */
1593 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1594 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1595 KASSERT(rt != NULL, ("route is NULL"));
1596 dir = IEEE80211_FC1_DIR_DSTODS;
1597 hdrsize += IEEE80211_ADDR_LEN;
1598 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1599 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1601 IEEE80211_NOTE_MAC(vap,
1604 "%s", "trying to send to ourself");
1607 meshae = IEEE80211_MESH_AE_10;
1609 sizeof(struct ieee80211_meshcntl_ae10);
1611 meshae = IEEE80211_MESH_AE_00;
1613 sizeof(struct ieee80211_meshcntl);
1616 dir = IEEE80211_FC1_DIR_FROMDS;
1617 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1619 meshae = IEEE80211_MESH_AE_01;
1621 sizeof(struct ieee80211_meshcntl_ae01);
1624 meshae = IEEE80211_MESH_AE_00;
1625 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1631 * 4-address frames need to be generated for:
1632 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1633 * o packets sent through a vap marked for relaying
1634 * (e.g. a station operating with dynamic WDS)
1636 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1637 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1638 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1640 hdrsize += IEEE80211_ADDR_LEN;
1641 meshhdrsize = meshae = 0;
1642 #ifdef IEEE80211_SUPPORT_MESH
1646 * Honor driver DATAPAD requirement.
1648 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1649 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1653 if (__predict_true((m->m_flags & M_FF) == 0)) {
1657 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1659 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1662 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1663 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1664 llc = mtod(m, struct llc *);
1665 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1666 llc->llc_control = LLC_UI;
1667 llc->llc_snap.org_code[0] = 0;
1668 llc->llc_snap.org_code[1] = 0;
1669 llc->llc_snap.org_code[2] = 0;
1670 llc->llc_snap.ether_type = eh.ether_type;
1672 #ifdef IEEE80211_SUPPORT_SUPERG
1674 * Aggregated frame. Check if it's for AMSDU or FF.
1676 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1677 * anywhere for some reason. But, since 11n requires
1678 * AMSDU RX, we can just assume "11n" == "AMSDU".
1680 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1681 if (ieee80211_amsdu_tx_ok(ni)) {
1682 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1685 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1691 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1693 M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT);
1695 vap->iv_stats.is_tx_nobuf++;
1698 wh = mtod(m, struct ieee80211_frame *);
1699 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1700 *(uint16_t *)wh->i_dur = 0;
1701 qos = NULL; /* NB: quiet compiler */
1703 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1704 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1705 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1706 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1707 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1708 } else switch (vap->iv_opmode) {
1709 case IEEE80211_M_STA:
1710 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1711 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1712 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1713 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1715 case IEEE80211_M_IBSS:
1716 case IEEE80211_M_AHDEMO:
1717 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1718 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1719 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1721 * NB: always use the bssid from iv_bss as the
1722 * neighbor's may be stale after an ibss merge
1724 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1726 case IEEE80211_M_HOSTAP:
1727 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1728 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1729 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1730 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1732 #ifdef IEEE80211_SUPPORT_MESH
1733 case IEEE80211_M_MBSS:
1734 /* NB: offset by hdrspace to deal with DATAPAD */
1735 mc = (struct ieee80211_meshcntl_ae10 *)
1736 (mtod(m, uint8_t *) + hdrspace);
1739 case IEEE80211_MESH_AE_00: /* no proxy */
1741 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1742 IEEE80211_ADDR_COPY(wh->i_addr1,
1744 IEEE80211_ADDR_COPY(wh->i_addr2,
1746 IEEE80211_ADDR_COPY(wh->i_addr3,
1748 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1750 qos =((struct ieee80211_qosframe_addr4 *)
1752 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1754 IEEE80211_ADDR_COPY(wh->i_addr1,
1756 IEEE80211_ADDR_COPY(wh->i_addr2,
1758 IEEE80211_ADDR_COPY(wh->i_addr3,
1760 qos = ((struct ieee80211_qosframe *)
1764 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1765 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1766 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1767 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1768 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1770 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1772 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1774 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1775 KASSERT(rt != NULL, ("route is NULL"));
1776 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1777 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1778 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1779 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1780 mc->mc_flags = IEEE80211_MESH_AE_10;
1781 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1782 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1783 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1786 KASSERT(0, ("meshae %d", meshae));
1789 mc->mc_ttl = ms->ms_ttl;
1791 le32enc(mc->mc_seq, ms->ms_seq);
1794 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1798 if (m->m_flags & M_MORE_DATA)
1799 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1804 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1805 /* NB: mesh case handled earlier */
1806 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1807 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1808 ac = M_WME_GETAC(m);
1809 /* map from access class/queue to 11e header priorty value */
1810 tid = WME_AC_TO_TID(ac);
1811 qos[0] = tid & IEEE80211_QOS_TID;
1812 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1813 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1814 #ifdef IEEE80211_SUPPORT_MESH
1815 if (vap->iv_opmode == IEEE80211_M_MBSS)
1816 qos[1] = IEEE80211_QOS_MC;
1820 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA;
1823 * If this is an A-MSDU then ensure we set the
1827 qos[0] |= IEEE80211_QOS_AMSDU;
1830 * XXX TODO TX lock is needed for atomic updates of sequence
1831 * numbers. If the driver does it, then don't do it here;
1832 * and we don't need the TX lock held.
1834 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1836 * 802.11-2012 9.3.2.10 -
1838 * If this is a multicast frame then we need
1839 * to ensure that the sequence number comes from
1840 * a separate seqno space and not the TID space.
1842 * Otherwise multicast frames may actually cause
1843 * holes in the TX blockack window space and
1844 * upset various things.
1846 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1847 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1849 seqno = ni->ni_txseqs[tid]++;
1852 * NB: don't assign a sequence # to potential
1853 * aggregates; we expect this happens at the
1854 * point the frame comes off any aggregation q
1855 * as otherwise we may introduce holes in the
1856 * BA sequence space and/or make window accouting
1859 * XXX may want to control this with a driver
1860 * capability; this may also change when we pull
1861 * aggregation up into net80211
1863 *(uint16_t *)wh->i_seq =
1864 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1865 M_SEQNO_SET(m, seqno);
1867 /* NB: zero out i_seq field (for s/w encryption etc) */
1868 *(uint16_t *)wh->i_seq = 0;
1872 * XXX TODO TX lock is needed for atomic updates of sequence
1873 * numbers. If the driver does it, then don't do it here;
1874 * and we don't need the TX lock held.
1876 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1877 *(uint16_t *)wh->i_seq =
1878 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1879 M_SEQNO_SET(m, seqno);
1882 * XXX TODO: we shouldn't allow EAPOL, etc that would
1883 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1886 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1891 * Check if xmit fragmentation is required.
1893 * If the hardware does fragmentation offload, then don't bother
1896 if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
1899 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1900 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1901 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1902 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1906 * IEEE 802.1X: send EAPOL frames always in the clear.
1907 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1909 if ((m->m_flags & M_EAPOL) == 0 ||
1910 ((vap->iv_flags & IEEE80211_F_WPA) &&
1911 (vap->iv_opmode == IEEE80211_M_STA ?
1912 !IEEE80211_KEY_UNDEFINED(key) :
1913 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1914 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1915 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1916 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1918 "%s", "enmic failed, discard frame");
1919 vap->iv_stats.is_crypto_enmicfail++;
1924 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1925 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1928 m->m_flags |= M_ENCAP; /* mark encapsulated */
1930 IEEE80211_NODE_STAT(ni, tx_data);
1931 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1932 IEEE80211_NODE_STAT(ni, tx_mcast);
1933 m->m_flags |= M_MCAST;
1935 IEEE80211_NODE_STAT(ni, tx_ucast);
1936 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1948 ieee80211_free_mbuf(struct mbuf *m)
1956 next = m->m_nextpkt;
1957 m->m_nextpkt = NULL;
1959 } while ((m = next) != NULL);
1963 * Fragment the frame according to the specified mtu.
1964 * The size of the 802.11 header (w/o padding) is provided
1965 * so we don't need to recalculate it. We create a new
1966 * mbuf for each fragment and chain it through m_nextpkt;
1967 * we might be able to optimize this by reusing the original
1968 * packet's mbufs but that is significantly more complicated.
1971 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1972 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1974 struct ieee80211com *ic = vap->iv_ic;
1975 struct ieee80211_frame *wh, *whf;
1976 struct mbuf *m, *prev;
1977 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1980 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1981 KASSERT(m0->m_pkthdr.len > mtu,
1982 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1985 * Honor driver DATAPAD requirement.
1987 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1988 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1992 wh = mtod(m0, struct ieee80211_frame *);
1993 /* NB: mark the first frag; it will be propagated below */
1994 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1995 totalhdrsize = hdrspace + ciphdrsize;
1997 off = mtu - ciphdrsize;
1998 remainder = m0->m_pkthdr.len - off;
2001 fragsize = MIN(totalhdrsize + remainder, mtu);
2002 m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
2005 /* leave room to prepend any cipher header */
2006 m_align(m, fragsize - ciphdrsize);
2009 * Form the header in the fragment. Note that since
2010 * we mark the first fragment with the MORE_FRAG bit
2011 * it automatically is propagated to each fragment; we
2012 * need only clear it on the last fragment (done below).
2013 * NB: frag 1+ dont have Mesh Control field present.
2015 whf = mtod(m, struct ieee80211_frame *);
2016 memcpy(whf, wh, hdrsize);
2017 #ifdef IEEE80211_SUPPORT_MESH
2018 if (vap->iv_opmode == IEEE80211_M_MBSS)
2019 ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
2021 *(uint16_t *)&whf->i_seq[0] |= htole16(
2022 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
2023 IEEE80211_SEQ_FRAG_SHIFT);
2026 payload = fragsize - totalhdrsize;
2027 /* NB: destination is known to be contiguous */
2029 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
2030 m->m_len = hdrspace + payload;
2031 m->m_pkthdr.len = hdrspace + payload;
2032 m->m_flags |= M_FRAG;
2034 /* chain up the fragment */
2035 prev->m_nextpkt = m;
2038 /* deduct fragment just formed */
2039 remainder -= payload;
2041 } while (remainder != 0);
2043 /* set the last fragment */
2044 m->m_flags |= M_LASTFRAG;
2045 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
2047 /* strip first mbuf now that everything has been copied */
2048 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
2049 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
2051 vap->iv_stats.is_tx_fragframes++;
2052 vap->iv_stats.is_tx_frags += fragno-1;
2056 /* reclaim fragments but leave original frame for caller to free */
2057 ieee80211_free_mbuf(m0->m_nextpkt);
2058 m0->m_nextpkt = NULL;
2063 * Add a supported rates element id to a frame.
2066 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
2070 *frm++ = IEEE80211_ELEMID_RATES;
2071 nrates = rs->rs_nrates;
2072 if (nrates > IEEE80211_RATE_SIZE)
2073 nrates = IEEE80211_RATE_SIZE;
2075 memcpy(frm, rs->rs_rates, nrates);
2076 return frm + nrates;
2080 * Add an extended supported rates element id to a frame.
2083 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
2086 * Add an extended supported rates element if operating in 11g mode.
2088 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2089 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2090 *frm++ = IEEE80211_ELEMID_XRATES;
2092 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2099 * Add an ssid element to a frame.
2102 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
2104 *frm++ = IEEE80211_ELEMID_SSID;
2106 memcpy(frm, ssid, len);
2111 * Add an erp element to a frame.
2114 ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap)
2116 struct ieee80211com *ic = vap->iv_ic;
2119 *frm++ = IEEE80211_ELEMID_ERP;
2124 * TODO: This uses the global flags for now because
2125 * the per-VAP flags are fine for per-VAP, but don't
2126 * take into account which VAPs share the same channel
2127 * and which are on different channels.
2129 * ERP and HT/VHT protection mode is a function of
2130 * how many stations are on a channel, not specifically
2131 * the VAP or global. But, until we grow that status,
2132 * the global flag will have to do.
2134 if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR)
2135 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2138 * TODO: same as above; these should be based not
2139 * on the vap or ic flags, but instead on a combination
2140 * of per-VAP and channels.
2142 if (ic->ic_flags & IEEE80211_F_USEPROT)
2143 erp |= IEEE80211_ERP_USE_PROTECTION;
2144 if (ic->ic_flags & IEEE80211_F_USEBARKER)
2145 erp |= IEEE80211_ERP_LONG_PREAMBLE;
2151 * Add a CFParams element to a frame.
2154 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
2156 #define ADDSHORT(frm, v) do { \
2160 *frm++ = IEEE80211_ELEMID_CFPARMS;
2162 *frm++ = 0; /* CFP count */
2163 *frm++ = 2; /* CFP period */
2164 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
2165 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
2170 static __inline uint8_t *
2171 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2173 memcpy(frm, ie->ie_data, ie->ie_len);
2174 return frm + ie->ie_len;
2177 static __inline uint8_t *
2178 add_ie(uint8_t *frm, const uint8_t *ie)
2180 memcpy(frm, ie, 2 + ie[1]);
2181 return frm + 2 + ie[1];
2184 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
2186 * Add a WME information element to a frame.
2189 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme,
2190 struct ieee80211_node *ni)
2192 static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE };
2193 struct ieee80211vap *vap = ni->ni_vap;
2195 *frm++ = IEEE80211_ELEMID_VENDOR;
2196 *frm++ = sizeof(struct ieee80211_wme_info) - 2;
2197 memcpy(frm, oui, sizeof(oui));
2199 *frm++ = WME_INFO_OUI_SUBTYPE;
2200 *frm++ = WME_VERSION;
2202 /* QoS info field depends upon operating mode */
2203 switch (vap->iv_opmode) {
2204 case IEEE80211_M_HOSTAP:
2205 *frm = wme->wme_bssChanParams.cap_info;
2206 if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
2207 *frm |= WME_CAPINFO_UAPSD_EN;
2210 case IEEE80211_M_STA:
2212 * NB: UAPSD drivers must set this up in their
2213 * VAP creation method.
2215 *frm++ = vap->iv_uapsdinfo;
2226 * Add a WME parameters element to a frame.
2229 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme,
2232 #define ADDSHORT(frm, v) do { \
2236 /* NB: this works 'cuz a param has an info at the front */
2237 static const struct ieee80211_wme_info param = {
2238 .wme_id = IEEE80211_ELEMID_VENDOR,
2239 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2240 .wme_oui = { WME_OUI_BYTES },
2241 .wme_type = WME_OUI_TYPE,
2242 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2243 .wme_version = WME_VERSION,
2247 memcpy(frm, ¶m, sizeof(param));
2248 frm += __offsetof(struct ieee80211_wme_info, wme_info);
2249 *frm = wme->wme_bssChanParams.cap_info; /* AC info */
2251 *frm |= WME_CAPINFO_UAPSD_EN;
2253 *frm++ = 0; /* reserved field */
2254 /* XXX TODO - U-APSD bits - SP, flags below */
2255 for (i = 0; i < WME_NUM_AC; i++) {
2256 const struct wmeParams *ac =
2257 &wme->wme_bssChanParams.cap_wmeParams[i];
2258 *frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI)
2259 | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM)
2260 | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN)
2262 *frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax,
2264 | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin,
2267 ADDSHORT(frm, ac->wmep_txopLimit);
2272 #undef WME_OUI_BYTES
2275 * Add an 11h Power Constraint element to a frame.
2278 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2280 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2281 /* XXX per-vap tx power limit? */
2282 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2284 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2286 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
2291 * Add an 11h Power Capability element to a frame.
2294 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2296 frm[0] = IEEE80211_ELEMID_PWRCAP;
2298 frm[2] = c->ic_minpower;
2299 frm[3] = c->ic_maxpower;
2304 * Add an 11h Supported Channels element to a frame.
2307 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2309 static const int ielen = 26;
2311 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2313 /* XXX not correct */
2314 memcpy(frm+2, ic->ic_chan_avail, ielen);
2315 return frm + 2 + ielen;
2319 * Add an 11h Quiet time element to a frame.
2322 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
2324 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2326 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2330 * Only update every beacon interval - otherwise probe responses
2331 * would update the quiet count value.
2334 if (vap->iv_quiet_count_value == 1)
2335 vap->iv_quiet_count_value = vap->iv_quiet_count;
2336 else if (vap->iv_quiet_count_value > 1)
2337 vap->iv_quiet_count_value--;
2340 if (vap->iv_quiet_count_value == 0) {
2341 /* value 0 is reserved as per 802.11h standerd */
2342 vap->iv_quiet_count_value = 1;
2345 quiet->tbttcount = vap->iv_quiet_count_value;
2346 quiet->period = vap->iv_quiet_period;
2347 quiet->duration = htole16(vap->iv_quiet_duration);
2348 quiet->offset = htole16(vap->iv_quiet_offset);
2349 return frm + sizeof(*quiet);
2353 * Add an 11h Channel Switch Announcement element to a frame.
2354 * Note that we use the per-vap CSA count to adjust the global
2355 * counter so we can use this routine to form probe response
2356 * frames and get the current count.
2359 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2361 struct ieee80211com *ic = vap->iv_ic;
2362 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2364 csa->csa_ie = IEEE80211_ELEMID_CSA;
2366 csa->csa_mode = 1; /* XXX force quiet on channel */
2367 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2368 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2369 return frm + sizeof(*csa);
2373 * Add an 11h country information element to a frame.
2376 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2379 if (ic->ic_countryie == NULL ||
2380 ic->ic_countryie_chan != ic->ic_bsschan) {
2382 * Handle lazy construction of ie. This is done on
2383 * first use and after a channel change that requires
2386 if (ic->ic_countryie != NULL)
2387 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2388 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2389 if (ic->ic_countryie == NULL)
2391 ic->ic_countryie_chan = ic->ic_bsschan;
2393 return add_appie(frm, ic->ic_countryie);
2397 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2399 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2400 return (add_ie(frm, vap->iv_wpa_ie));
2402 /* XXX else complain? */
2408 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2410 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2411 return (add_ie(frm, vap->iv_rsn_ie));
2413 /* XXX else complain? */
2419 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2421 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2422 *frm++ = IEEE80211_ELEMID_QOS;
2431 * ieee80211_send_probereq(): send a probe request frame with the specified ssid
2432 * and any optional information element data; some helper functions as FW based
2433 * HW scans need some of that information passed too.
2436 ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic)
2438 const struct ieee80211_rateset *rs;
2440 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2443 * prreq frame format
2445 * [tlv] supported rates
2446 * [tlv] extended supported rates (if needed)
2447 * [tlv] HT cap (optional)
2448 * [tlv] VHT cap (optional)
2449 * [tlv] WPA (optional)
2450 * [tlv] user-specified ie's
2452 return ( 2 + IEEE80211_NWID_LEN
2453 + 2 + IEEE80211_RATE_SIZE
2454 + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
2455 2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0)
2456 + (((vap->iv_opmode == IEEE80211_M_IBSS) &&
2457 (vap->iv_flags_ht & IEEE80211_FHT_HT)) ?
2458 sizeof(struct ieee80211_ie_htcap) : 0)
2460 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */
2461 + sizeof(struct ieee80211_ie_vhtcap)
2463 + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ?
2464 vap->iv_wpa_ie[1] : 0)
2465 + (vap->iv_appie_probereq != NULL ?
2466 vap->iv_appie_probereq->ie_len : 0)
2471 ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic,
2472 uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen,
2475 const struct ieee80211_rateset *rs;
2479 if (!alloc && (frmp == NULL || frmlen == NULL))
2482 len = ieee80211_probereq_ie_len(vap, ic);
2483 if (!alloc && len > *frmlen)
2486 /* For HW scans we usually do not pass in the SSID as IE. */
2488 len -= (2 + IEEE80211_NWID_LEN);
2491 frm = IEEE80211_MALLOC(len, M_80211_VAP,
2492 IEEE80211_M_WAITOK | IEEE80211_M_ZERO);
2499 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2500 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2501 frm = ieee80211_add_rates(frm, rs);
2502 frm = ieee80211_add_xrates(frm, rs);
2505 * Note: we can't use bss; we don't have one yet.
2507 * So, we should announce our capabilities
2508 * in this channel mode (2g/5g), not the
2509 * channel details itself.
2511 if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2512 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2513 struct ieee80211_channel *c;
2516 * Get the HT channel that we should try upgrading to.
2517 * If we can do 40MHz then this'll upgrade it appropriately.
2519 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2521 frm = ieee80211_add_htcap_ch(frm, vap, c);
2525 * XXX TODO: need to figure out what/how to update the
2529 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
2530 struct ieee80211_channel *c;
2532 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2534 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
2535 frm = ieee80211_add_vhtcap_ch(frm, vap, c);
2539 frm = ieee80211_add_wpa(frm, vap);
2540 if (vap->iv_appie_probereq != NULL)
2541 frm = add_appie(frm, vap->iv_appie_probereq);
2552 ieee80211_send_probereq(struct ieee80211_node *ni,
2553 const uint8_t sa[IEEE80211_ADDR_LEN],
2554 const uint8_t da[IEEE80211_ADDR_LEN],
2555 const uint8_t bssid[IEEE80211_ADDR_LEN],
2556 const uint8_t *ssid, size_t ssidlen)
2558 struct ieee80211vap *vap = ni->ni_vap;
2559 struct ieee80211com *ic = ni->ni_ic;
2560 struct ieee80211_node *bss;
2561 const struct ieee80211_txparam *tp;
2562 struct ieee80211_bpf_params params;
2568 bss = ieee80211_ref_node(vap->iv_bss);
2570 if (vap->iv_state == IEEE80211_S_CAC) {
2571 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2572 "block %s frame in CAC state", "probe request");
2573 vap->iv_stats.is_tx_badstate++;
2574 ieee80211_free_node(bss);
2575 return EIO; /* XXX */
2579 * Hold a reference on the node so it doesn't go away until after
2580 * the xmit is complete all the way in the driver. On error we
2581 * will remove our reference.
2583 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2584 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2586 ni, ether_sprintf(ni->ni_macaddr),
2587 ieee80211_node_refcnt(ni)+1);
2588 ieee80211_ref_node(ni);
2590 /* See comments above for entire frame format. */
2591 frmlen = ieee80211_probereq_ie_len(vap, ic);
2592 m = ieee80211_getmgtframe(&frm,
2593 ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen);
2595 vap->iv_stats.is_tx_nobuf++;
2596 ieee80211_free_node(ni);
2597 ieee80211_free_node(bss);
2601 ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen,
2604 ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret));
2606 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2607 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2608 ("leading space %zd", M_LEADINGSPACE(m)));
2609 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
2611 /* NB: cannot happen */
2612 ieee80211_free_node(ni);
2613 ieee80211_free_node(bss);
2617 IEEE80211_TX_LOCK(ic);
2618 ieee80211_send_setup(ni, m,
2619 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2620 IEEE80211_NONQOS_TID, sa, da, bssid);
2621 /* XXX power management? */
2622 m->m_flags |= M_ENCAP; /* mark encapsulated */
2624 M_WME_SETAC(m, WME_AC_BE);
2626 IEEE80211_NODE_STAT(ni, tx_probereq);
2627 IEEE80211_NODE_STAT(ni, tx_mgmt);
2629 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2630 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2631 ieee80211_chan2ieee(ic, ic->ic_curchan),
2632 ether_sprintf(bssid),
2637 memset(¶ms, 0, sizeof(params));
2638 params.ibp_pri = M_WME_GETAC(m);
2639 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2640 params.ibp_rate0 = tp->mgmtrate;
2641 if (IEEE80211_IS_MULTICAST(da)) {
2642 params.ibp_flags |= IEEE80211_BPF_NOACK;
2643 params.ibp_try0 = 1;
2645 params.ibp_try0 = tp->maxretry;
2646 params.ibp_power = ni->ni_txpower;
2647 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2648 IEEE80211_TX_UNLOCK(ic);
2649 ieee80211_free_node(bss);
2654 * Calculate capability information for mgt frames.
2657 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2661 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2663 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2664 capinfo = IEEE80211_CAPINFO_ESS;
2665 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2666 capinfo = IEEE80211_CAPINFO_IBSS;
2669 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2670 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2671 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2672 IEEE80211_IS_CHAN_2GHZ(chan))
2673 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2674 if (vap->iv_flags & IEEE80211_F_SHSLOT)
2675 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2676 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2677 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2682 * Send a management frame. The node is for the destination (or ic_bss
2683 * when in station mode). Nodes other than ic_bss have their reference
2684 * count bumped to reflect our use for an indeterminant time.
2687 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2689 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2690 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2691 struct ieee80211vap *vap = ni->ni_vap;
2692 struct ieee80211com *ic = ni->ni_ic;
2693 struct ieee80211_node *bss = vap->iv_bss;
2694 struct ieee80211_bpf_params params;
2698 int has_challenge, is_shared_key, ret, status;
2700 KASSERT(ni != NULL, ("null node"));
2703 * Hold a reference on the node so it doesn't go away until after
2704 * the xmit is complete all the way in the driver. On error we
2705 * will remove our reference.
2707 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2708 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2710 ni, ether_sprintf(ni->ni_macaddr),
2711 ieee80211_node_refcnt(ni)+1);
2712 ieee80211_ref_node(ni);
2714 memset(¶ms, 0, sizeof(params));
2716 case IEEE80211_FC0_SUBTYPE_AUTH:
2719 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2720 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2721 ni->ni_challenge != NULL);
2724 * Deduce whether we're doing open authentication or
2725 * shared key authentication. We do the latter if
2726 * we're in the middle of a shared key authentication
2727 * handshake or if we're initiating an authentication
2728 * request and configured to use shared key.
2730 is_shared_key = has_challenge ||
2731 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2732 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2733 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2735 m = ieee80211_getmgtframe(&frm,
2736 ic->ic_headroom + sizeof(struct ieee80211_frame),
2737 3 * sizeof(uint16_t)
2738 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2739 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0));
2741 senderr(ENOMEM, is_tx_nobuf);
2743 ((uint16_t *)frm)[0] =
2744 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2745 : htole16(IEEE80211_AUTH_ALG_OPEN);
2746 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2747 ((uint16_t *)frm)[2] = htole16(status);/* status */
2749 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2750 ((uint16_t *)frm)[3] =
2751 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2752 IEEE80211_ELEMID_CHALLENGE);
2753 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2754 IEEE80211_CHALLENGE_LEN);
2755 m->m_pkthdr.len = m->m_len =
2756 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2757 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2758 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2759 "request encrypt frame (%s)", __func__);
2760 /* mark frame for encryption */
2761 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2764 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2766 /* XXX not right for shared key */
2767 if (status == IEEE80211_STATUS_SUCCESS)
2768 IEEE80211_NODE_STAT(ni, tx_auth);
2770 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2772 if (vap->iv_opmode == IEEE80211_M_STA)
2773 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2774 (void *) vap->iv_state);
2777 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2778 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2779 "send station deauthenticate (reason: %d (%s))", arg,
2780 ieee80211_reason_to_string(arg));
2781 m = ieee80211_getmgtframe(&frm,
2782 ic->ic_headroom + sizeof(struct ieee80211_frame),
2785 senderr(ENOMEM, is_tx_nobuf);
2786 *(uint16_t *)frm = htole16(arg); /* reason */
2787 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2789 IEEE80211_NODE_STAT(ni, tx_deauth);
2790 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2792 ieee80211_node_unauthorize(ni); /* port closed */
2795 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2796 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2798 * asreq frame format
2799 * [2] capability information
2800 * [2] listen interval
2801 * [6*] current AP address (reassoc only)
2803 * [tlv] supported rates
2804 * [tlv] extended supported rates
2805 * [4] power capability (optional)
2806 * [28] supported channels (optional)
2807 * [tlv] HT capabilities
2808 * [tlv] VHT capabilities
2809 * [tlv] WME (optional)
2810 * [tlv] Vendor OUI HT capabilities (optional)
2811 * [tlv] Atheros capabilities (if negotiated)
2812 * [tlv] AppIE's (optional)
2814 m = ieee80211_getmgtframe(&frm,
2815 ic->ic_headroom + sizeof(struct ieee80211_frame),
2818 + IEEE80211_ADDR_LEN
2819 + 2 + IEEE80211_NWID_LEN
2820 + 2 + IEEE80211_RATE_SIZE
2821 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2824 + sizeof(struct ieee80211_wme_info)
2825 + sizeof(struct ieee80211_ie_htcap)
2826 + sizeof(struct ieee80211_ie_vhtcap)
2827 + 4 + sizeof(struct ieee80211_ie_htcap)
2828 #ifdef IEEE80211_SUPPORT_SUPERG
2829 + sizeof(struct ieee80211_ath_ie)
2831 + (vap->iv_appie_wpa != NULL ?
2832 vap->iv_appie_wpa->ie_len : 0)
2833 + (vap->iv_appie_assocreq != NULL ?
2834 vap->iv_appie_assocreq->ie_len : 0)
2837 senderr(ENOMEM, is_tx_nobuf);
2839 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2840 ("wrong mode %u", vap->iv_opmode));
2841 capinfo = IEEE80211_CAPINFO_ESS;
2842 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2843 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2845 * NB: Some 11a AP's reject the request when
2846 * short preamble is set.
2848 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2849 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2850 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2851 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2852 (ic->ic_caps & IEEE80211_C_SHSLOT))
2853 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2854 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2855 (vap->iv_flags & IEEE80211_F_DOTH))
2856 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2857 *(uint16_t *)frm = htole16(capinfo);
2860 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2861 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2865 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2866 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2867 frm += IEEE80211_ADDR_LEN;
2870 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2871 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2872 frm = ieee80211_add_rsn(frm, vap);
2873 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2874 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2875 frm = ieee80211_add_powercapability(frm,
2877 frm = ieee80211_add_supportedchannels(frm, ic);
2881 * Check the channel - we may be using an 11n NIC with an
2882 * 11n capable station, but we're configured to be an 11b
2885 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2886 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2887 ni->ni_ies.htcap_ie != NULL &&
2888 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2889 frm = ieee80211_add_htcap(frm, ni);
2892 if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
2893 IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
2894 ni->ni_ies.vhtcap_ie != NULL &&
2895 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
2896 frm = ieee80211_add_vhtcap(frm, ni);
2899 frm = ieee80211_add_wpa(frm, vap);
2900 if ((vap->iv_flags & IEEE80211_F_WME) &&
2901 ni->ni_ies.wme_ie != NULL)
2902 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
2905 * Same deal - only send HT info if we're on an 11n
2908 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2909 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2910 ni->ni_ies.htcap_ie != NULL &&
2911 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2912 frm = ieee80211_add_htcap_vendor(frm, ni);
2914 #ifdef IEEE80211_SUPPORT_SUPERG
2915 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2916 frm = ieee80211_add_ath(frm,
2917 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2918 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2919 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2920 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2922 #endif /* IEEE80211_SUPPORT_SUPERG */
2923 if (vap->iv_appie_assocreq != NULL)
2924 frm = add_appie(frm, vap->iv_appie_assocreq);
2925 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2927 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2928 (void *) vap->iv_state);
2931 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2932 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2934 * asresp frame format
2935 * [2] capability information
2937 * [2] association ID
2938 * [tlv] supported rates
2939 * [tlv] extended supported rates
2940 * [tlv] HT capabilities (standard, if STA enabled)
2941 * [tlv] HT information (standard, if STA enabled)
2942 * [tlv] VHT capabilities (standard, if STA enabled)
2943 * [tlv] VHT information (standard, if STA enabled)
2944 * [tlv] WME (if configured and STA enabled)
2945 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2946 * [tlv] HT information (vendor OUI, if STA enabled)
2947 * [tlv] Atheros capabilities (if STA enabled)
2948 * [tlv] AppIE's (optional)
2950 m = ieee80211_getmgtframe(&frm,
2951 ic->ic_headroom + sizeof(struct ieee80211_frame),
2955 + 2 + IEEE80211_RATE_SIZE
2956 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2957 + sizeof(struct ieee80211_ie_htcap) + 4
2958 + sizeof(struct ieee80211_ie_htinfo) + 4
2959 + sizeof(struct ieee80211_ie_vhtcap)
2960 + sizeof(struct ieee80211_ie_vht_operation)
2961 + sizeof(struct ieee80211_wme_param)
2962 #ifdef IEEE80211_SUPPORT_SUPERG
2963 + sizeof(struct ieee80211_ath_ie)
2965 + (vap->iv_appie_assocresp != NULL ?
2966 vap->iv_appie_assocresp->ie_len : 0)
2969 senderr(ENOMEM, is_tx_nobuf);
2971 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2972 *(uint16_t *)frm = htole16(capinfo);
2975 *(uint16_t *)frm = htole16(arg); /* status */
2978 if (arg == IEEE80211_STATUS_SUCCESS) {
2979 *(uint16_t *)frm = htole16(ni->ni_associd);
2980 IEEE80211_NODE_STAT(ni, tx_assoc);
2982 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2985 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2986 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2987 /* NB: respond according to what we received */
2988 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2989 frm = ieee80211_add_htcap(frm, ni);
2990 frm = ieee80211_add_htinfo(frm, ni);
2992 if ((vap->iv_flags & IEEE80211_F_WME) &&
2993 ni->ni_ies.wme_ie != NULL)
2994 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
2995 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
2996 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2997 frm = ieee80211_add_htcap_vendor(frm, ni);
2998 frm = ieee80211_add_htinfo_vendor(frm, ni);
3000 if (ni->ni_flags & IEEE80211_NODE_VHT) {
3001 frm = ieee80211_add_vhtcap(frm, ni);
3002 frm = ieee80211_add_vhtinfo(frm, ni);
3004 #ifdef IEEE80211_SUPPORT_SUPERG
3005 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
3006 frm = ieee80211_add_ath(frm,
3007 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
3008 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
3009 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
3010 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
3011 #endif /* IEEE80211_SUPPORT_SUPERG */
3012 if (vap->iv_appie_assocresp != NULL)
3013 frm = add_appie(frm, vap->iv_appie_assocresp);
3014 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3017 case IEEE80211_FC0_SUBTYPE_DISASSOC:
3018 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
3019 "send station disassociate (reason: %d (%s))", arg,
3020 ieee80211_reason_to_string(arg));
3021 m = ieee80211_getmgtframe(&frm,
3022 ic->ic_headroom + sizeof(struct ieee80211_frame),
3025 senderr(ENOMEM, is_tx_nobuf);
3026 *(uint16_t *)frm = htole16(arg); /* reason */
3027 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
3029 IEEE80211_NODE_STAT(ni, tx_disassoc);
3030 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
3034 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
3035 "invalid mgmt frame type %u", type);
3036 senderr(EINVAL, is_tx_unknownmgt);
3040 /* NB: force non-ProbeResp frames to the highest queue */
3041 params.ibp_pri = WME_AC_VO;
3042 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
3043 /* NB: we know all frames are unicast */
3044 params.ibp_try0 = bss->ni_txparms->maxretry;
3045 params.ibp_power = bss->ni_txpower;
3046 return ieee80211_mgmt_output(ni, m, type, ¶ms);
3048 ieee80211_free_node(ni);
3055 * Return an mbuf with a probe response frame in it.
3056 * Space is left to prepend and 802.11 header at the
3057 * front but it's left to the caller to fill in.
3060 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3062 struct ieee80211vap *vap = bss->ni_vap;
3063 struct ieee80211com *ic = bss->ni_ic;
3064 const struct ieee80211_rateset *rs;
3070 * probe response frame format
3072 * [2] beacon interval
3073 * [2] cabability information
3075 * [tlv] supported rates
3076 * [tlv] parameter set (FH/DS)
3077 * [tlv] parameter set (IBSS)
3078 * [tlv] country (optional)
3079 * [3] power control (optional)
3080 * [5] channel switch announcement (CSA) (optional)
3081 * [tlv] extended rate phy (ERP)
3082 * [tlv] extended supported rates
3083 * [tlv] RSN (optional)
3084 * [tlv] HT capabilities
3085 * [tlv] HT information
3086 * [tlv] VHT capabilities
3087 * [tlv] VHT information
3088 * [tlv] WPA (optional)
3089 * [tlv] WME (optional)
3090 * [tlv] Vendor OUI HT capabilities (optional)
3091 * [tlv] Vendor OUI HT information (optional)
3092 * [tlv] Atheros capabilities
3093 * [tlv] AppIE's (optional)
3094 * [tlv] Mesh ID (MBSS)
3095 * [tlv] Mesh Conf (MBSS)
3097 m = ieee80211_getmgtframe(&frm,
3098 ic->ic_headroom + sizeof(struct ieee80211_frame),
3102 + 2 + IEEE80211_NWID_LEN
3103 + 2 + IEEE80211_RATE_SIZE
3105 + IEEE80211_COUNTRY_MAX_SIZE
3107 + sizeof(struct ieee80211_csa_ie)
3108 + sizeof(struct ieee80211_quiet_ie)
3110 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3111 + sizeof(struct ieee80211_ie_wpa)
3112 + sizeof(struct ieee80211_ie_htcap)
3113 + sizeof(struct ieee80211_ie_htinfo)
3114 + sizeof(struct ieee80211_ie_wpa)
3115 + sizeof(struct ieee80211_wme_param)
3116 + 4 + sizeof(struct ieee80211_ie_htcap)
3117 + 4 + sizeof(struct ieee80211_ie_htinfo)
3118 + sizeof(struct ieee80211_ie_vhtcap)
3119 + sizeof(struct ieee80211_ie_vht_operation)
3120 #ifdef IEEE80211_SUPPORT_SUPERG
3121 + sizeof(struct ieee80211_ath_ie)
3123 #ifdef IEEE80211_SUPPORT_MESH
3124 + 2 + IEEE80211_MESHID_LEN
3125 + sizeof(struct ieee80211_meshconf_ie)
3127 + (vap->iv_appie_proberesp != NULL ?
3128 vap->iv_appie_proberesp->ie_len : 0)
3131 vap->iv_stats.is_tx_nobuf++;
3135 memset(frm, 0, 8); /* timestamp should be filled later */
3137 *(uint16_t *)frm = htole16(bss->ni_intval);
3139 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
3140 *(uint16_t *)frm = htole16(capinfo);
3143 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
3144 rs = ieee80211_get_suprates(ic, bss->ni_chan);
3145 frm = ieee80211_add_rates(frm, rs);
3147 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
3148 *frm++ = IEEE80211_ELEMID_FHPARMS;
3150 *frm++ = bss->ni_fhdwell & 0x00ff;
3151 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
3152 *frm++ = IEEE80211_FH_CHANSET(
3153 ieee80211_chan2ieee(ic, bss->ni_chan));
3154 *frm++ = IEEE80211_FH_CHANPAT(
3155 ieee80211_chan2ieee(ic, bss->ni_chan));
3156 *frm++ = bss->ni_fhindex;
3158 *frm++ = IEEE80211_ELEMID_DSPARMS;
3160 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
3163 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3164 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3166 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3168 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3169 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3170 frm = ieee80211_add_countryie(frm, ic);
3171 if (vap->iv_flags & IEEE80211_F_DOTH) {
3172 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3173 frm = ieee80211_add_powerconstraint(frm, vap);
3174 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3175 frm = ieee80211_add_csa(frm, vap);
3177 if (vap->iv_flags & IEEE80211_F_DOTH) {
3178 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3179 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3181 frm = ieee80211_add_quiet(frm, vap, 0);
3184 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3185 frm = ieee80211_add_erp(frm, vap);
3186 frm = ieee80211_add_xrates(frm, rs);
3187 frm = ieee80211_add_rsn(frm, vap);
3189 * NB: legacy 11b clients do not get certain ie's.
3190 * The caller identifies such clients by passing
3191 * a token in legacy to us. Could expand this to be
3192 * any legacy client for stuff like HT ie's.
3194 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3195 legacy != IEEE80211_SEND_LEGACY_11B) {
3196 frm = ieee80211_add_htcap(frm, bss);
3197 frm = ieee80211_add_htinfo(frm, bss);
3199 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3200 legacy != IEEE80211_SEND_LEGACY_11B) {
3201 frm = ieee80211_add_vhtcap(frm, bss);
3202 frm = ieee80211_add_vhtinfo(frm, bss);
3204 frm = ieee80211_add_wpa(frm, vap);
3205 if (vap->iv_flags & IEEE80211_F_WME)
3206 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3207 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3208 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3209 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3210 legacy != IEEE80211_SEND_LEGACY_11B) {
3211 frm = ieee80211_add_htcap_vendor(frm, bss);
3212 frm = ieee80211_add_htinfo_vendor(frm, bss);
3214 #ifdef IEEE80211_SUPPORT_SUPERG
3215 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
3216 legacy != IEEE80211_SEND_LEGACY_11B)
3217 frm = ieee80211_add_athcaps(frm, bss);
3219 if (vap->iv_appie_proberesp != NULL)
3220 frm = add_appie(frm, vap->iv_appie_proberesp);
3221 #ifdef IEEE80211_SUPPORT_MESH
3222 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3223 frm = ieee80211_add_meshid(frm, vap);
3224 frm = ieee80211_add_meshconf(frm, vap);
3227 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3233 * Send a probe response frame to the specified mac address.
3234 * This does not go through the normal mgt frame api so we
3235 * can specify the destination address and re-use the bss node
3236 * for the sta reference.
3239 ieee80211_send_proberesp(struct ieee80211vap *vap,
3240 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3242 struct ieee80211_node *bss = vap->iv_bss;
3243 struct ieee80211com *ic = vap->iv_ic;
3247 if (vap->iv_state == IEEE80211_S_CAC) {
3248 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3249 "block %s frame in CAC state", "probe response");
3250 vap->iv_stats.is_tx_badstate++;
3251 return EIO; /* XXX */
3255 * Hold a reference on the node so it doesn't go away until after
3256 * the xmit is complete all the way in the driver. On error we
3257 * will remove our reference.
3259 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3260 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3261 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3262 ieee80211_node_refcnt(bss)+1);
3263 ieee80211_ref_node(bss);
3265 m = ieee80211_alloc_proberesp(bss, legacy);
3267 ieee80211_free_node(bss);
3271 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3272 KASSERT(m != NULL, ("no room for header"));
3274 IEEE80211_TX_LOCK(ic);
3275 ieee80211_send_setup(bss, m,
3276 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
3277 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3278 /* XXX power management? */
3279 m->m_flags |= M_ENCAP; /* mark encapsulated */
3281 M_WME_SETAC(m, WME_AC_BE);
3283 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3284 "send probe resp on channel %u to %s%s\n",
3285 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3286 legacy ? " <legacy>" : "");
3287 IEEE80211_NODE_STAT(bss, tx_mgmt);
3289 ret = ieee80211_raw_output(vap, bss, m, NULL);
3290 IEEE80211_TX_UNLOCK(ic);
3295 * Allocate and build a RTS (Request To Send) control frame.
3298 ieee80211_alloc_rts(struct ieee80211com *ic,
3299 const uint8_t ra[IEEE80211_ADDR_LEN],
3300 const uint8_t ta[IEEE80211_ADDR_LEN],
3303 struct ieee80211_frame_rts *rts;
3306 /* XXX honor ic_headroom */
3307 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3309 rts = mtod(m, struct ieee80211_frame_rts *);
3310 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3311 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3312 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3313 *(u_int16_t *)rts->i_dur = htole16(dur);
3314 IEEE80211_ADDR_COPY(rts->i_ra, ra);
3315 IEEE80211_ADDR_COPY(rts->i_ta, ta);
3317 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3323 * Allocate and build a CTS (Clear To Send) control frame.
3326 ieee80211_alloc_cts(struct ieee80211com *ic,
3327 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3329 struct ieee80211_frame_cts *cts;
3332 /* XXX honor ic_headroom */
3333 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3335 cts = mtod(m, struct ieee80211_frame_cts *);
3336 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3337 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3338 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3339 *(u_int16_t *)cts->i_dur = htole16(dur);
3340 IEEE80211_ADDR_COPY(cts->i_ra, ra);
3342 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3348 * Wrapper for CTS/RTS frame allocation.
3351 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3352 uint8_t rate, int prot)
3354 struct ieee80211com *ic = ni->ni_ic;
3355 struct ieee80211vap *vap = ni->ni_vap;
3356 const struct ieee80211_frame *wh;
3359 int pktlen, isshort;
3361 KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3362 prot == IEEE80211_PROT_CTSONLY,
3363 ("wrong protection type %d", prot));
3365 wh = mtod(m, const struct ieee80211_frame *);
3366 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3367 isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0;
3368 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3369 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3371 if (prot == IEEE80211_PROT_RTSCTS) {
3372 /* NB: CTS is the same size as an ACK */
3373 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3374 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3376 mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur);
3382 ieee80211_tx_mgt_timeout(void *arg)
3384 struct ieee80211vap *vap = arg;
3386 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3387 "vap %p mode %s state %s flags %#x & %#x\n", vap,
3388 ieee80211_opmode_name[vap->iv_opmode],
3389 ieee80211_state_name[vap->iv_state],
3390 vap->iv_ic->ic_flags, IEEE80211_F_SCAN);
3392 IEEE80211_LOCK(vap->iv_ic);
3393 if (vap->iv_state != IEEE80211_S_INIT &&
3394 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3396 * NB: it's safe to specify a timeout as the reason here;
3397 * it'll only be used in the right state.
3399 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
3400 IEEE80211_SCAN_FAIL_TIMEOUT);
3402 IEEE80211_UNLOCK(vap->iv_ic);
3406 * This is the callback set on net80211-sourced transmitted
3407 * authentication request frames.
3409 * This does a couple of things:
3411 * + If the frame transmitted was a success, it schedules a future
3412 * event which will transition the interface to scan.
3413 * If a state transition _then_ occurs before that event occurs,
3414 * said state transition will cancel this callout.
3416 * + If the frame transmit was a failure, it immediately schedules
3417 * the transition back to scan.
3420 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3422 struct ieee80211vap *vap = ni->ni_vap;
3423 enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
3426 * Frame transmit completed; arrange timer callback. If
3427 * transmit was successfully we wait for response. Otherwise
3428 * we arrange an immediate callback instead of doing the
3429 * callback directly since we don't know what state the driver
3430 * is in (e.g. what locks it is holding). This work should
3431 * not be too time-critical and not happen too often so the
3432 * added overhead is acceptable.
3434 * XXX what happens if !acked but response shows up before callback?
3436 if (vap->iv_state == ostate) {
3437 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3438 "ni %p mode %s state %s arg %p status %d\n", ni,
3439 ieee80211_opmode_name[vap->iv_opmode],
3440 ieee80211_state_name[vap->iv_state], arg, status);
3442 callout_reset(&vap->iv_mgtsend,
3443 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3444 ieee80211_tx_mgt_timeout, vap);
3449 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3450 struct ieee80211_node *ni)
3452 struct ieee80211vap *vap = ni->ni_vap;
3453 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3454 struct ieee80211com *ic = ni->ni_ic;
3455 struct ieee80211_rateset *rs = &ni->ni_rates;
3459 * beacon frame format
3461 * TODO: update to 802.11-2012; a lot of stuff has changed;
3462 * vendor extensions should be at the end, etc.
3465 * [2] beacon interval
3466 * [2] cabability information
3468 * [tlv] supported rates
3469 * [3] parameter set (DS)
3470 * [8] CF parameter set (optional)
3471 * [tlv] parameter set (IBSS/TIM)
3472 * [tlv] country (optional)
3473 * [3] power control (optional)
3474 * [5] channel switch announcement (CSA) (optional)
3476 * XXX TODO: IBSS DFS
3477 * XXX TODO: TPC report
3478 * [tlv] extended rate phy (ERP)
3479 * [tlv] extended supported rates
3480 * [tlv] RSN parameters
3482 * (XXX EDCA parameter set, QoS capability?)
3483 * XXX TODO: AP channel report
3485 * [tlv] HT capabilities
3486 * [tlv] HT information
3487 * XXX TODO: 20/40 BSS coexistence
3490 * XXX TODO: mesh config
3491 * XXX TODO: mesh awake window
3492 * XXX TODO: beacon timing (mesh, etc)
3493 * XXX TODO: MCCAOP Advertisement Overview
3494 * XXX TODO: MCCAOP Advertisement
3495 * XXX TODO: Mesh channel switch parameters
3497 * XXX TODO: VHT capabilities
3498 * XXX TODO: VHT operation
3499 * XXX TODO: VHT transmit power envelope
3500 * XXX TODO: channel switch wrapper element
3501 * XXX TODO: extended BSS load element
3503 * XXX Vendor-specific OIDs (e.g. Atheros)
3504 * [tlv] WPA parameters
3505 * [tlv] WME parameters
3506 * [tlv] Vendor OUI HT capabilities (optional)
3507 * [tlv] Vendor OUI HT information (optional)
3508 * [tlv] Atheros capabilities (optional)
3509 * [tlv] TDMA parameters (optional)
3510 * [tlv] Mesh ID (MBSS)
3511 * [tlv] Mesh Conf (MBSS)
3512 * [tlv] application data (optional)
3515 memset(bo, 0, sizeof(*bo));
3517 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3519 *(uint16_t *)frm = htole16(ni->ni_intval);
3521 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3522 bo->bo_caps = (uint16_t *)frm;
3523 *(uint16_t *)frm = htole16(capinfo);
3525 *frm++ = IEEE80211_ELEMID_SSID;
3526 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3527 *frm++ = ni->ni_esslen;
3528 memcpy(frm, ni->ni_essid, ni->ni_esslen);
3529 frm += ni->ni_esslen;
3532 frm = ieee80211_add_rates(frm, rs);
3533 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3534 *frm++ = IEEE80211_ELEMID_DSPARMS;
3536 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3538 if (ic->ic_flags & IEEE80211_F_PCF) {
3540 frm = ieee80211_add_cfparms(frm, ic);
3543 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3544 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3546 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3548 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3549 vap->iv_opmode == IEEE80211_M_MBSS) {
3550 /* TIM IE is the same for Mesh and Hostap */
3551 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3553 tie->tim_ie = IEEE80211_ELEMID_TIM;
3554 tie->tim_len = 4; /* length */
3555 tie->tim_count = 0; /* DTIM count */
3556 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3557 tie->tim_bitctl = 0; /* bitmap control */
3558 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3559 frm += sizeof(struct ieee80211_tim_ie);
3562 bo->bo_tim_trailer = frm;
3563 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3564 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3565 frm = ieee80211_add_countryie(frm, ic);
3566 if (vap->iv_flags & IEEE80211_F_DOTH) {
3567 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3568 frm = ieee80211_add_powerconstraint(frm, vap);
3570 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3571 frm = ieee80211_add_csa(frm, vap);
3575 bo->bo_quiet = NULL;
3576 if (vap->iv_flags & IEEE80211_F_DOTH) {
3577 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3578 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
3579 (vap->iv_quiet == 1)) {
3581 * We only insert the quiet IE offset if
3582 * the quiet IE is enabled. Otherwise don't
3583 * put it here or we'll just overwrite
3584 * some other beacon contents.
3586 if (vap->iv_quiet) {
3588 frm = ieee80211_add_quiet(frm,vap, 0);
3593 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3595 frm = ieee80211_add_erp(frm, vap);
3597 frm = ieee80211_add_xrates(frm, rs);
3598 frm = ieee80211_add_rsn(frm, vap);
3599 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3600 frm = ieee80211_add_htcap(frm, ni);
3601 bo->bo_htinfo = frm;
3602 frm = ieee80211_add_htinfo(frm, ni);
3605 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
3606 frm = ieee80211_add_vhtcap(frm, ni);
3607 bo->bo_vhtinfo = frm;
3608 frm = ieee80211_add_vhtinfo(frm, ni);
3609 /* Transmit power envelope */
3610 /* Channel switch wrapper element */
3611 /* Extended bss load element */
3614 frm = ieee80211_add_wpa(frm, vap);
3615 if (vap->iv_flags & IEEE80211_F_WME) {
3617 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3618 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3620 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3621 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3622 frm = ieee80211_add_htcap_vendor(frm, ni);
3623 frm = ieee80211_add_htinfo_vendor(frm, ni);
3626 #ifdef IEEE80211_SUPPORT_SUPERG
3627 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3629 frm = ieee80211_add_athcaps(frm, ni);
3632 #ifdef IEEE80211_SUPPORT_TDMA
3633 if (vap->iv_caps & IEEE80211_C_TDMA) {
3635 frm = ieee80211_add_tdma(frm, vap);
3638 if (vap->iv_appie_beacon != NULL) {
3640 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3641 frm = add_appie(frm, vap->iv_appie_beacon);
3644 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */
3645 #ifdef IEEE80211_SUPPORT_MESH
3646 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3647 frm = ieee80211_add_meshid(frm, vap);
3648 bo->bo_meshconf = frm;
3649 frm = ieee80211_add_meshconf(frm, vap);
3652 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3653 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3654 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3658 * Allocate a beacon frame and fillin the appropriate bits.
3661 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3663 struct ieee80211vap *vap = ni->ni_vap;
3664 struct ieee80211com *ic = ni->ni_ic;
3665 struct ifnet *ifp = vap->iv_ifp;
3666 struct ieee80211_frame *wh;
3672 * Update the "We're putting the quiet IE in the beacon" state.
3674 if (vap->iv_quiet == 1)
3675 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3676 else if (vap->iv_quiet == 0)
3677 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3680 * beacon frame format
3682 * Note: This needs updating for 802.11-2012.
3685 * [2] beacon interval
3686 * [2] cabability information
3688 * [tlv] supported rates
3689 * [3] parameter set (DS)
3690 * [8] CF parameter set (optional)
3691 * [tlv] parameter set (IBSS/TIM)
3692 * [tlv] country (optional)
3693 * [3] power control (optional)
3694 * [5] channel switch announcement (CSA) (optional)
3695 * [tlv] extended rate phy (ERP)
3696 * [tlv] extended supported rates
3697 * [tlv] RSN parameters
3698 * [tlv] HT capabilities
3699 * [tlv] HT information
3700 * [tlv] VHT capabilities
3701 * [tlv] VHT operation
3702 * [tlv] Vendor OUI HT capabilities (optional)
3703 * [tlv] Vendor OUI HT information (optional)
3704 * XXX Vendor-specific OIDs (e.g. Atheros)
3705 * [tlv] WPA parameters
3706 * [tlv] WME parameters
3707 * [tlv] TDMA parameters (optional)
3708 * [tlv] Mesh ID (MBSS)
3709 * [tlv] Mesh Conf (MBSS)
3710 * [tlv] application data (optional)
3711 * NB: we allocate the max space required for the TIM bitmap.
3712 * XXX how big is this?
3714 pktlen = 8 /* time stamp */
3715 + sizeof(uint16_t) /* beacon interval */
3716 + sizeof(uint16_t) /* capabilities */
3717 + 2 + ni->ni_esslen /* ssid */
3718 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3719 + 2 + 1 /* DS parameters */
3720 + 2 + 6 /* CF parameters */
3721 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3722 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3723 + 2 + 1 /* power control */
3724 + sizeof(struct ieee80211_csa_ie) /* CSA */
3725 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3727 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3728 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3729 2*sizeof(struct ieee80211_ie_wpa) : 0)
3730 /* XXX conditional? */
3731 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3732 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3733 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
3734 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
3735 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3736 sizeof(struct ieee80211_wme_param) : 0)
3737 #ifdef IEEE80211_SUPPORT_SUPERG
3738 + sizeof(struct ieee80211_ath_ie) /* ATH */
3740 #ifdef IEEE80211_SUPPORT_TDMA
3741 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3742 sizeof(struct ieee80211_tdma_param) : 0)
3744 #ifdef IEEE80211_SUPPORT_MESH
3745 + 2 + ni->ni_meshidlen
3746 + sizeof(struct ieee80211_meshconf_ie)
3748 + IEEE80211_MAX_APPIE
3750 m = ieee80211_getmgtframe(&frm,
3751 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3753 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3754 "%s: cannot get buf; size %u\n", __func__, pktlen);
3755 vap->iv_stats.is_tx_nobuf++;
3758 ieee80211_beacon_construct(m, frm, ni);
3760 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3761 KASSERT(m != NULL, ("no space for 802.11 header?"));
3762 wh = mtod(m, struct ieee80211_frame *);
3763 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3764 IEEE80211_FC0_SUBTYPE_BEACON;
3765 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3766 *(uint16_t *)wh->i_dur = 0;
3767 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3768 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3769 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3770 *(uint16_t *)wh->i_seq = 0;
3776 * Update the dynamic parts of a beacon frame based on the current state.
3779 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3781 struct ieee80211vap *vap = ni->ni_vap;
3782 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3783 struct ieee80211com *ic = ni->ni_ic;
3784 int len_changed = 0;
3786 struct ieee80211_frame *wh;
3787 ieee80211_seq seqno;
3791 * Handle 11h channel change when we've reached the count.
3792 * We must recalculate the beacon frame contents to account
3793 * for the new channel. Note we do this only for the first
3794 * vap that reaches this point; subsequent vaps just update
3795 * their beacon state to reflect the recalculated channel.
3797 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3798 vap->iv_csa_count == ic->ic_csa_count) {
3799 vap->iv_csa_count = 0;
3801 * Effect channel change before reconstructing the beacon
3802 * frame contents as many places reference ni_chan.
3804 if (ic->ic_csa_newchan != NULL)
3805 ieee80211_csa_completeswitch(ic);
3807 * NB: ieee80211_beacon_construct clears all pending
3808 * updates in bo_flags so we don't need to explicitly
3809 * clear IEEE80211_BEACON_CSA.
3811 ieee80211_beacon_construct(m,
3812 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3814 /* XXX do WME aggressive mode processing? */
3815 IEEE80211_UNLOCK(ic);
3816 return 1; /* just assume length changed */
3820 * Handle the quiet time element being added and removed.
3821 * Again, for now we just cheat and reconstruct the whole
3822 * beacon - that way the gap is provided as appropriate.
3824 * So, track whether we have already added the IE versus
3825 * whether we want to be adding the IE.
3827 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
3828 (vap->iv_quiet == 0)) {
3830 * Quiet time beacon IE enabled, but it's disabled;
3833 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3834 ieee80211_beacon_construct(m,
3835 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3836 /* XXX do WME aggressive mode processing? */
3837 IEEE80211_UNLOCK(ic);
3838 return 1; /* just assume length changed */
3841 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
3842 (vap->iv_quiet == 1)) {
3844 * Quiet time beacon IE disabled, but it's now enabled;
3847 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3848 ieee80211_beacon_construct(m,
3849 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3850 /* XXX do WME aggressive mode processing? */
3851 IEEE80211_UNLOCK(ic);
3852 return 1; /* just assume length changed */
3855 wh = mtod(m, struct ieee80211_frame *);
3858 * XXX TODO Strictly speaking this should be incremented with the TX
3859 * lock held so as to serialise access to the non-qos TID sequence
3862 * If the driver identifies it does its own TX seqno management then
3863 * we can skip this (and still not do the TX seqno.)
3865 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3866 *(uint16_t *)&wh->i_seq[0] =
3867 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3868 M_SEQNO_SET(m, seqno);
3870 /* XXX faster to recalculate entirely or just changes? */
3871 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3872 *bo->bo_caps = htole16(capinfo);
3874 if (vap->iv_flags & IEEE80211_F_WME) {
3875 struct ieee80211_wme_state *wme = &ic->ic_wme;
3878 * Check for aggressive mode change. When there is
3879 * significant high priority traffic in the BSS
3880 * throttle back BE traffic by using conservative
3881 * parameters. Otherwise BE uses aggressive params
3882 * to optimize performance of legacy/non-QoS traffic.
3884 if (wme->wme_flags & WME_F_AGGRMODE) {
3885 if (wme->wme_hipri_traffic >
3886 wme->wme_hipri_switch_thresh) {
3887 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3888 "%s: traffic %u, disable aggressive mode\n",
3889 __func__, wme->wme_hipri_traffic);
3890 wme->wme_flags &= ~WME_F_AGGRMODE;
3891 ieee80211_wme_updateparams_locked(vap);
3892 wme->wme_hipri_traffic =
3893 wme->wme_hipri_switch_hysteresis;
3895 wme->wme_hipri_traffic = 0;
3897 if (wme->wme_hipri_traffic <=
3898 wme->wme_hipri_switch_thresh) {
3899 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3900 "%s: traffic %u, enable aggressive mode\n",
3901 __func__, wme->wme_hipri_traffic);
3902 wme->wme_flags |= WME_F_AGGRMODE;
3903 ieee80211_wme_updateparams_locked(vap);
3904 wme->wme_hipri_traffic = 0;
3906 wme->wme_hipri_traffic =
3907 wme->wme_hipri_switch_hysteresis;
3909 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3910 (void) ieee80211_add_wme_param(bo->bo_wme, wme,
3911 vap->iv_flags_ext & IEEE80211_FEXT_UAPSD);
3912 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3916 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3917 ieee80211_ht_update_beacon(vap, bo);
3918 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3920 #ifdef IEEE80211_SUPPORT_TDMA
3921 if (vap->iv_caps & IEEE80211_C_TDMA) {
3923 * NB: the beacon is potentially updated every TBTT.
3925 ieee80211_tdma_update_beacon(vap, bo);
3928 #ifdef IEEE80211_SUPPORT_MESH
3929 if (vap->iv_opmode == IEEE80211_M_MBSS)
3930 ieee80211_mesh_update_beacon(vap, bo);
3933 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3934 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3935 struct ieee80211_tim_ie *tie =
3936 (struct ieee80211_tim_ie *) bo->bo_tim;
3937 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3938 u_int timlen, timoff, i;
3940 * ATIM/DTIM needs updating. If it fits in the
3941 * current space allocated then just copy in the
3942 * new bits. Otherwise we need to move any trailing
3943 * data to make room. Note that we know there is
3944 * contiguous space because ieee80211_beacon_allocate
3945 * insures there is space in the mbuf to write a
3946 * maximal-size virtual bitmap (based on iv_max_aid).
3949 * Calculate the bitmap size and offset, copy any
3950 * trailer out of the way, and then copy in the
3951 * new bitmap and update the information element.
3952 * Note that the tim bitmap must contain at least
3953 * one byte and any offset must be even.
3955 if (vap->iv_ps_pending != 0) {
3956 timoff = 128; /* impossibly large */
3957 for (i = 0; i < vap->iv_tim_len; i++)
3958 if (vap->iv_tim_bitmap[i]) {
3962 KASSERT(timoff != 128, ("tim bitmap empty!"));
3963 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3964 if (vap->iv_tim_bitmap[i])
3966 timlen = 1 + (i - timoff);
3973 * TODO: validate this!
3975 if (timlen != bo->bo_tim_len) {
3976 /* copy up/down trailer */
3977 int adjust = tie->tim_bitmap+timlen
3978 - bo->bo_tim_trailer;
3979 ovbcopy(bo->bo_tim_trailer,
3980 bo->bo_tim_trailer+adjust,
3981 bo->bo_tim_trailer_len);
3982 bo->bo_tim_trailer += adjust;
3983 bo->bo_erp += adjust;
3984 bo->bo_htinfo += adjust;
3985 bo->bo_vhtinfo += adjust;
3986 #ifdef IEEE80211_SUPPORT_SUPERG
3987 bo->bo_ath += adjust;
3989 #ifdef IEEE80211_SUPPORT_TDMA
3990 bo->bo_tdma += adjust;
3992 #ifdef IEEE80211_SUPPORT_MESH
3993 bo->bo_meshconf += adjust;
3995 bo->bo_appie += adjust;
3996 bo->bo_wme += adjust;
3997 bo->bo_csa += adjust;
3998 bo->bo_quiet += adjust;
3999 bo->bo_tim_len = timlen;
4001 /* update information element */
4002 tie->tim_len = 3 + timlen;
4003 tie->tim_bitctl = timoff;
4006 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
4009 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
4011 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
4012 "%s: TIM updated, pending %u, off %u, len %u\n",
4013 __func__, vap->iv_ps_pending, timoff, timlen);
4015 /* count down DTIM period */
4016 if (tie->tim_count == 0)
4017 tie->tim_count = tie->tim_period - 1;
4020 /* update state for buffered multicast frames on DTIM */
4021 if (mcast && tie->tim_count == 0)
4022 tie->tim_bitctl |= 1;
4024 tie->tim_bitctl &= ~1;
4025 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
4026 struct ieee80211_csa_ie *csa =
4027 (struct ieee80211_csa_ie *) bo->bo_csa;
4030 * Insert or update CSA ie. If we're just starting
4031 * to count down to the channel switch then we need
4032 * to insert the CSA ie. Otherwise we just need to
4033 * drop the count. The actual change happens above
4034 * when the vap's count reaches the target count.
4036 if (vap->iv_csa_count == 0) {
4037 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
4038 bo->bo_erp += sizeof(*csa);
4039 bo->bo_htinfo += sizeof(*csa);
4040 bo->bo_vhtinfo += sizeof(*csa);
4041 bo->bo_wme += sizeof(*csa);
4042 #ifdef IEEE80211_SUPPORT_SUPERG
4043 bo->bo_ath += sizeof(*csa);
4045 #ifdef IEEE80211_SUPPORT_TDMA
4046 bo->bo_tdma += sizeof(*csa);
4048 #ifdef IEEE80211_SUPPORT_MESH
4049 bo->bo_meshconf += sizeof(*csa);
4051 bo->bo_appie += sizeof(*csa);
4052 bo->bo_csa_trailer_len += sizeof(*csa);
4053 bo->bo_quiet += sizeof(*csa);
4054 bo->bo_tim_trailer_len += sizeof(*csa);
4055 m->m_len += sizeof(*csa);
4056 m->m_pkthdr.len += sizeof(*csa);
4058 ieee80211_add_csa(bo->bo_csa, vap);
4061 vap->iv_csa_count++;
4062 /* NB: don't clear IEEE80211_BEACON_CSA */
4066 * Only add the quiet time IE if we've enabled it
4069 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
4070 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
4071 if (vap->iv_quiet &&
4072 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
4073 ieee80211_add_quiet(bo->bo_quiet, vap, 1);
4076 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
4078 * ERP element needs updating.
4080 (void) ieee80211_add_erp(bo->bo_erp, vap);
4081 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
4083 #ifdef IEEE80211_SUPPORT_SUPERG
4084 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
4085 ieee80211_add_athcaps(bo->bo_ath, ni);
4086 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
4090 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
4091 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
4097 aielen += aie->ie_len;
4098 if (aielen != bo->bo_appie_len) {
4099 /* copy up/down trailer */
4100 int adjust = aielen - bo->bo_appie_len;
4101 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
4102 bo->bo_tim_trailer_len);
4103 bo->bo_tim_trailer += adjust;
4104 bo->bo_appie += adjust;
4105 bo->bo_appie_len = aielen;
4111 frm = add_appie(frm, aie);
4112 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
4114 IEEE80211_UNLOCK(ic);
4120 * Do Ethernet-LLC encapsulation for each payload in a fast frame
4121 * tunnel encapsulation. The frame is assumed to have an Ethernet
4122 * header at the front that must be stripped before prepending the
4123 * LLC followed by the Ethernet header passed in (with an Ethernet
4124 * type that specifies the payload size).
4127 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
4128 const struct ether_header *eh)
4133 /* XXX optimize by combining m_adj+M_PREPEND */
4134 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
4135 llc = mtod(m, struct llc *);
4136 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
4137 llc->llc_control = LLC_UI;
4138 llc->llc_snap.org_code[0] = 0;
4139 llc->llc_snap.org_code[1] = 0;
4140 llc->llc_snap.org_code[2] = 0;
4141 llc->llc_snap.ether_type = eh->ether_type;
4142 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
4144 M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT);
4145 if (m == NULL) { /* XXX cannot happen */
4146 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
4147 "%s: no space for ether_header\n", __func__);
4148 vap->iv_stats.is_tx_nobuf++;
4151 ETHER_HEADER_COPY(mtod(m, void *), eh);
4152 mtod(m, struct ether_header *)->ether_type = htons(payload);
4157 * Complete an mbuf transmission.
4159 * For now, this simply processes a completed frame after the
4160 * driver has completed it's transmission and/or retransmission.
4161 * It assumes the frame is an 802.11 encapsulated frame.
4163 * Later on it will grow to become the exit path for a given frame
4164 * from the driver and, depending upon how it's been encapsulated
4165 * and already transmitted, it may end up doing A-MPDU retransmission,
4166 * power save requeuing, etc.
4168 * In order for the above to work, the driver entry point to this
4169 * must not hold any driver locks. Thus, the driver needs to delay
4170 * any actual mbuf completion until it can release said locks.
4172 * This frees the mbuf and if the mbuf has a node reference,
4173 * the node reference will be freed.
4176 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
4180 struct ifnet *ifp = ni->ni_vap->iv_ifp;
4183 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
4184 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
4185 if (m->m_flags & M_MCAST)
4186 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4188 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4189 if (m->m_flags & M_TXCB) {
4190 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
4191 "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap,
4192 ieee80211_opmode_name[ni->ni_vap->iv_opmode],
4193 ieee80211_state_name[ni->ni_vap->iv_state], m, status);
4194 ieee80211_process_callback(ni, m, status);
4196 ieee80211_free_node(ni);