2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Atsushi Onoe
5 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/endian.h>
43 #include <sys/socket.h>
46 #include <net/ethernet.h>
48 #include <net/if_var.h>
49 #include <net/if_llc.h>
50 #include <net/if_media.h>
51 #include <net/if_vlan_var.h>
53 #include <net80211/ieee80211_var.h>
54 #include <net80211/ieee80211_regdomain.h>
55 #ifdef IEEE80211_SUPPORT_SUPERG
56 #include <net80211/ieee80211_superg.h>
58 #ifdef IEEE80211_SUPPORT_TDMA
59 #include <net80211/ieee80211_tdma.h>
61 #include <net80211/ieee80211_wds.h>
62 #include <net80211/ieee80211_mesh.h>
63 #include <net80211/ieee80211_vht.h>
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
75 #include <netinet/ip6.h>
78 #include <security/mac/mac_framework.h>
80 #define ETHER_HEADER_COPY(dst, src) \
81 memcpy(dst, src, sizeof(struct ether_header))
83 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
84 u_int hdrsize, u_int ciphdrsize, u_int mtu);
85 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
87 #ifdef IEEE80211_DEBUG
89 * Decide if an outbound management frame should be
90 * printed when debugging is enabled. This filters some
91 * of the less interesting frames that come frequently
95 doprint(struct ieee80211vap *vap, int subtype)
98 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
99 return (vap->iv_opmode == IEEE80211_M_IBSS);
106 * Transmit a frame to the given destination on the given VAP.
108 * It's up to the caller to figure out the details of who this
109 * is going to and resolving the node.
111 * This routine takes care of queuing it for power save,
112 * A-MPDU state stuff, fast-frames state stuff, encapsulation
113 * if required, then passing it up to the driver layer.
115 * This routine (for now) consumes the mbuf and frees the node
116 * reference; it ideally will return a TX status which reflects
117 * whether the mbuf was consumed or not, so the caller can
118 * free the mbuf (if appropriate) and the node reference (again,
122 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
123 struct ieee80211_node *ni)
125 struct ieee80211com *ic = vap->iv_ic;
126 struct ifnet *ifp = vap->iv_ifp;
129 #ifdef IEEE80211_SUPPORT_SUPERG
131 int do_ampdu_amsdu = 0;
132 int no_ampdu = 1; /* Will be set to 0 if ampdu is active */
136 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
137 (m->m_flags & M_PWR_SAV) == 0) {
139 * Station in power save mode; pass the frame
140 * to the 802.11 layer and continue. We'll get
141 * the frame back when the time is right.
142 * XXX lose WDS vap linkage?
144 if (ieee80211_pwrsave(ni, m) != 0)
145 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
146 ieee80211_free_node(ni);
149 * We queued it fine, so tell the upper layer
150 * that we consumed it.
154 /* calculate priority so drivers can find the tx queue */
155 if (ieee80211_classify(ni, m)) {
156 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
157 ni->ni_macaddr, NULL,
158 "%s", "classification failure");
159 vap->iv_stats.is_tx_classify++;
160 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
162 ieee80211_free_node(ni);
164 /* XXX better status? */
168 * Stash the node pointer. Note that we do this after
169 * any call to ieee80211_dwds_mcast because that code
170 * uses any existing value for rcvif to identify the
171 * interface it (might have been) received on.
173 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
174 m->m_pkthdr.rcvif = (void *)ni;
175 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
177 BPF_MTAP(ifp, m); /* 802.3 tx */
180 * Figure out if we can do A-MPDU, A-MSDU or FF.
182 * A-MPDU depends upon vap/node config.
183 * A-MSDU depends upon vap/node config.
184 * FF depends upon vap config, IE and whether
185 * it's 11abg (and not 11n/11ac/etc.)
187 * Note that these flags indiciate whether we can do
188 * it at all, rather than the situation (eg traffic type.)
190 do_ampdu = ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
191 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX));
192 #ifdef IEEE80211_SUPPORT_SUPERG
193 do_amsdu = ((ni->ni_flags & IEEE80211_NODE_AMSDU_TX) &&
194 (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX));
196 ((ni->ni_flags & IEEE80211_NODE_HT) == 0) &&
197 ((ni->ni_flags & IEEE80211_NODE_VHT) == 0) &&
198 (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF));
202 * Check if A-MPDU tx aggregation is setup or if we
203 * should try to enable it. The sta must be associated
204 * with HT and A-MPDU enabled for use. When the policy
205 * routine decides we should enable A-MPDU we issue an
206 * ADDBA request and wait for a reply. The frame being
207 * encapsulated will go out w/o using A-MPDU, or possibly
208 * it might be collected by the driver and held/retransmit.
209 * The default ic_ampdu_enable routine handles staggering
210 * ADDBA requests in case the receiver NAK's us or we are
211 * otherwise unable to establish a BA stream.
213 * Don't treat group-addressed frames as candidates for aggregation;
214 * net80211 doesn't support 802.11aa-2012 and so group addressed
215 * frames will always have sequence numbers allocated from the NON_QOS
219 if ((m->m_flags & M_EAPOL) == 0 && (! mcast)) {
220 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
221 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
223 ieee80211_txampdu_count_packet(tap);
224 if (IEEE80211_AMPDU_RUNNING(tap)) {
226 * Operational, mark frame for aggregation.
228 * XXX do tx aggregation here
230 m->m_flags |= M_AMPDU_MPDU;
231 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
232 ic->ic_ampdu_enable(ni, tap)) {
234 * Not negotiated yet, request service.
236 ieee80211_ampdu_request(ni, tap);
237 /* XXX hold frame for reply? */
240 * Now update the no-ampdu flag. A-MPDU may have been
241 * started or administratively disabled above; so now we
242 * know whether we're running yet or not.
244 * This will let us know whether we should be doing A-MSDU
245 * at this point. We only do A-MSDU if we're either not
246 * doing A-MPDU, or A-MPDU is NACKed, or A-MPDU + A-MSDU
249 * Whilst here, update the amsdu-ampdu flag. The above may
250 * have also set or cleared the amsdu-in-ampdu txa_flags
251 * combination so we can correctly do A-MPDU + A-MSDU.
253 #ifdef IEEE80211_SUPPORT_SUPERG
254 no_ampdu = (! IEEE80211_AMPDU_RUNNING(tap)
255 || (IEEE80211_AMPDU_NACKED(tap)));
256 do_ampdu_amsdu = IEEE80211_AMPDU_RUNNING_AMSDU(tap);
261 #ifdef IEEE80211_SUPPORT_SUPERG
263 * Check for AMSDU/FF; queue for aggregation
265 * Note: we don't bother trying to do fast frames or
266 * A-MSDU encapsulation for 802.3 drivers. Now, we
267 * likely could do it for FF (because it's a magic
268 * atheros tunnel LLC type) but I don't think we're going
269 * to really need to. For A-MSDU we'd have to set the
270 * A-MSDU QoS bit in the wifi header, so we just plain
273 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
275 (do_ampdu_amsdu || (no_ampdu && do_amsdu)) &&
276 ieee80211_amsdu_tx_ok(ni)) {
277 m = ieee80211_amsdu_check(ni, m);
279 /* NB: any ni ref held on stageq */
280 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
281 "%s: amsdu_check queued frame\n",
285 } else if ((! mcast) && do_ff) {
286 m = ieee80211_ff_check(ni, m);
288 /* NB: any ni ref held on stageq */
289 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
290 "%s: ff_check queued frame\n",
296 #endif /* IEEE80211_SUPPORT_SUPERG */
299 * Grab the TX lock - serialise the TX process from this
300 * point (where TX state is being checked/modified)
301 * through to driver queue.
303 IEEE80211_TX_LOCK(ic);
306 * XXX make the encap and transmit code a separate function
307 * so things like the FF (and later A-MSDU) path can just call
308 * it for flushed frames.
310 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
312 * Encapsulate the packet in prep for transmission.
314 m = ieee80211_encap(vap, ni, m);
316 /* NB: stat+msg handled in ieee80211_encap */
317 IEEE80211_TX_UNLOCK(ic);
318 ieee80211_free_node(ni);
319 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
323 (void) ieee80211_parent_xmitpkt(ic, m);
326 * Unlock at this point - no need to hold it across
327 * ieee80211_free_node() (ie, the comlock)
329 IEEE80211_TX_UNLOCK(ic);
330 ic->ic_lastdata = ticks;
336 * Send the given mbuf through the given vap.
338 * This consumes the mbuf regardless of whether the transmit
339 * was successful or not.
341 * This does none of the initial checks that ieee80211_start()
342 * does (eg CAC timeout, interface wakeup) - the caller must
346 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
348 #define IS_DWDS(vap) \
349 (vap->iv_opmode == IEEE80211_M_WDS && \
350 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
351 struct ieee80211com *ic = vap->iv_ic;
352 struct ifnet *ifp = vap->iv_ifp;
353 struct ieee80211_node *ni;
354 struct ether_header *eh;
357 * Cancel any background scan.
359 if (ic->ic_flags & IEEE80211_F_SCAN)
360 ieee80211_cancel_anyscan(vap);
362 * Find the node for the destination so we can do
363 * things like power save and fast frames aggregation.
365 * NB: past this point various code assumes the first
366 * mbuf has the 802.3 header present (and contiguous).
369 if (m->m_len < sizeof(struct ether_header) &&
370 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
371 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
372 "discard frame, %s\n", "m_pullup failed");
373 vap->iv_stats.is_tx_nobuf++; /* XXX */
374 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
377 eh = mtod(m, struct ether_header *);
378 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
381 * Only unicast frames from the above go out
382 * DWDS vaps; multicast frames are handled by
383 * dispatching the frame as it comes through
384 * the AP vap (see below).
386 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
387 eh->ether_dhost, "mcast", "%s", "on DWDS");
388 vap->iv_stats.is_dwds_mcast++;
390 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
391 /* XXX better status? */
394 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
396 * Spam DWDS vap's w/ multicast traffic.
398 /* XXX only if dwds in use? */
399 ieee80211_dwds_mcast(vap, m);
402 #ifdef IEEE80211_SUPPORT_MESH
403 if (vap->iv_opmode != IEEE80211_M_MBSS) {
405 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
407 /* NB: ieee80211_find_txnode does stat+msg */
408 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
410 /* XXX better status? */
413 if (ni->ni_associd == 0 &&
414 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
415 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
416 eh->ether_dhost, NULL,
417 "sta not associated (type 0x%04x)",
418 htons(eh->ether_type));
419 vap->iv_stats.is_tx_notassoc++;
420 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
422 ieee80211_free_node(ni);
423 /* XXX better status? */
426 #ifdef IEEE80211_SUPPORT_MESH
428 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
430 * Proxy station only if configured.
432 if (!ieee80211_mesh_isproxyena(vap)) {
433 IEEE80211_DISCARD_MAC(vap,
434 IEEE80211_MSG_OUTPUT |
436 eh->ether_dhost, NULL,
437 "%s", "proxy not enabled");
438 vap->iv_stats.is_mesh_notproxy++;
439 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
441 /* XXX better status? */
444 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
445 "forward frame from DS SA(%6D), DA(%6D)\n",
446 eh->ether_shost, ":",
447 eh->ether_dhost, ":");
448 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
450 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
453 * NB: ieee80211_mesh_discover holds/disposes
454 * frame (e.g. queueing on path discovery).
456 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
457 /* XXX better status? */
464 * We've resolved the sender, so attempt to transmit it.
467 if (vap->iv_state == IEEE80211_S_SLEEP) {
469 * In power save; queue frame and then wakeup device
472 ic->ic_lastdata = ticks;
473 if (ieee80211_pwrsave(ni, m) != 0)
474 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
475 ieee80211_free_node(ni);
476 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
480 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
487 * Start method for vap's. All packets from the stack come
488 * through here. We handle common processing of the packets
489 * before dispatching them to the underlying device.
491 * if_transmit() requires that the mbuf be consumed by this call
492 * regardless of the return condition.
495 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
497 struct ieee80211vap *vap = ifp->if_softc;
498 struct ieee80211com *ic = vap->iv_ic;
501 * No data frames go out unless we're running.
502 * Note in particular this covers CAC and CSA
503 * states (though maybe we should check muting
506 if (vap->iv_state != IEEE80211_S_RUN &&
507 vap->iv_state != IEEE80211_S_SLEEP) {
509 /* re-check under the com lock to avoid races */
510 if (vap->iv_state != IEEE80211_S_RUN &&
511 vap->iv_state != IEEE80211_S_SLEEP) {
512 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
513 "%s: ignore queue, in %s state\n",
514 __func__, ieee80211_state_name[vap->iv_state]);
515 vap->iv_stats.is_tx_badstate++;
516 IEEE80211_UNLOCK(ic);
517 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
519 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
522 IEEE80211_UNLOCK(ic);
526 * Sanitize mbuf flags for net80211 use. We cannot
527 * clear M_PWR_SAV or M_MORE_DATA because these may
528 * be set for frames that are re-submitted from the
531 * NB: This must be done before ieee80211_classify as
532 * it marks EAPOL in frames with M_EAPOL.
534 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
537 * Bump to the packet transmission path.
538 * The mbuf will be consumed here.
540 return (ieee80211_start_pkt(vap, m));
544 ieee80211_vap_qflush(struct ifnet *ifp)
551 * 802.11 raw output routine.
553 * XXX TODO: this (and other send routines) should correctly
554 * XXX keep the pwr mgmt bit set if it decides to call into the
555 * XXX driver to send a frame whilst the state is SLEEP.
557 * Otherwise the peer may decide that we're awake and flood us
558 * with traffic we are still too asleep to receive!
561 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
562 struct mbuf *m, const struct ieee80211_bpf_params *params)
564 struct ieee80211com *ic = vap->iv_ic;
568 * Set node - the caller has taken a reference, so ensure
569 * that the mbuf has the same node value that
570 * it would if it were going via the normal path.
572 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
573 m->m_pkthdr.rcvif = (void *)ni;
576 * Attempt to add bpf transmit parameters.
578 * For now it's ok to fail; the raw_xmit api still takes
581 * Later on when ic_raw_xmit() has params removed,
582 * they'll have to be added - so fail the transmit if
586 (void) ieee80211_add_xmit_params(m, params);
588 error = ic->ic_raw_xmit(ni, m, params);
590 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
591 ieee80211_free_node(ni);
597 ieee80211_validate_frame(struct mbuf *m,
598 const struct ieee80211_bpf_params *params)
600 struct ieee80211_frame *wh;
603 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
606 wh = mtod(m, struct ieee80211_frame *);
607 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
608 IEEE80211_FC0_VERSION_0)
611 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
612 if (type != IEEE80211_FC0_TYPE_DATA) {
613 if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
614 IEEE80211_FC1_DIR_NODS)
617 if (type != IEEE80211_FC0_TYPE_MGT &&
618 (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) != 0)
621 /* XXX skip other field checks? */
624 if ((params && (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0) ||
625 (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) != 0) {
628 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
631 * See IEEE Std 802.11-2012,
632 * 8.2.4.1.9 'Protected Frame field'
634 /* XXX no support for robust management frames yet. */
635 if (!(type == IEEE80211_FC0_TYPE_DATA ||
636 (type == IEEE80211_FC0_TYPE_MGT &&
637 subtype == IEEE80211_FC0_SUBTYPE_AUTH)))
640 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
643 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
650 ieee80211_validate_rate(struct ieee80211_node *ni, uint8_t rate)
652 struct ieee80211com *ic = ni->ni_ic;
654 if (IEEE80211_IS_HT_RATE(rate)) {
655 if ((ic->ic_htcaps & IEEE80211_HTC_HT) == 0)
658 rate = IEEE80211_RV(rate);
660 if (rate > ic->ic_txstream * 8 - 1)
667 if ((ic->ic_htcaps & IEEE80211_HTC_TXMCS32) == 0)
673 if ((ic->ic_htcaps & IEEE80211_HTC_TXUNEQUAL) == 0)
676 switch (ic->ic_txstream) {
699 if (!ieee80211_isratevalid(ic->ic_rt, rate))
706 ieee80211_sanitize_rates(struct ieee80211_node *ni, struct mbuf *m,
707 const struct ieee80211_bpf_params *params)
712 return (0); /* nothing to do */
714 /* NB: most drivers assume that ibp_rate0 is set (!= 0). */
715 if (params->ibp_rate0 != 0) {
716 error = ieee80211_validate_rate(ni, params->ibp_rate0);
720 /* XXX pre-setup some default (e.g., mgmt / mcast) rate */
725 if (params->ibp_rate1 != 0 &&
726 (error = ieee80211_validate_rate(ni, params->ibp_rate1)) != 0)
729 if (params->ibp_rate2 != 0 &&
730 (error = ieee80211_validate_rate(ni, params->ibp_rate2)) != 0)
733 if (params->ibp_rate3 != 0 &&
734 (error = ieee80211_validate_rate(ni, params->ibp_rate3)) != 0)
741 * 802.11 output routine. This is (currently) used only to
742 * connect bpf write calls to the 802.11 layer for injecting
746 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
747 const struct sockaddr *dst, struct route *ro)
749 #define senderr(e) do { error = (e); goto bad;} while (0)
750 const struct ieee80211_bpf_params *params = NULL;
751 struct ieee80211_node *ni = NULL;
752 struct ieee80211vap *vap;
753 struct ieee80211_frame *wh;
754 struct ieee80211com *ic = NULL;
758 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
760 * Short-circuit requests if the vap is marked OACTIVE
761 * as this can happen because a packet came down through
762 * ieee80211_start before the vap entered RUN state in
763 * which case it's ok to just drop the frame. This
764 * should not be necessary but callers of if_output don't
772 * Hand to the 802.3 code if not tagged as
773 * a raw 802.11 frame.
775 if (dst->sa_family != AF_IEEE80211)
776 return vap->iv_output(ifp, m, dst, ro);
778 error = mac_ifnet_check_transmit(ifp, m);
782 if (ifp->if_flags & IFF_MONITOR)
784 if (!IFNET_IS_UP_RUNNING(ifp))
786 if (vap->iv_state == IEEE80211_S_CAC) {
787 IEEE80211_DPRINTF(vap,
788 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
789 "block %s frame in CAC state\n", "raw data");
790 vap->iv_stats.is_tx_badstate++;
791 senderr(EIO); /* XXX */
792 } else if (vap->iv_state == IEEE80211_S_SCAN)
794 /* XXX bypass bridge, pfil, carp, etc. */
797 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
798 * present by setting the sa_len field of the sockaddr (yes,
800 * NB: we assume sa_data is suitably aligned to cast.
802 if (dst->sa_len != 0)
803 params = (const struct ieee80211_bpf_params *)dst->sa_data;
805 error = ieee80211_validate_frame(m, params);
809 wh = mtod(m, struct ieee80211_frame *);
811 /* locate destination node */
812 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
813 case IEEE80211_FC1_DIR_NODS:
814 case IEEE80211_FC1_DIR_FROMDS:
815 ni = ieee80211_find_txnode(vap, wh->i_addr1);
817 case IEEE80211_FC1_DIR_TODS:
818 case IEEE80211_FC1_DIR_DSTODS:
819 ni = ieee80211_find_txnode(vap, wh->i_addr3);
826 * Permit packets w/ bpf params through regardless
827 * (see below about sa_len).
829 if (dst->sa_len == 0)
830 senderr(EHOSTUNREACH);
831 ni = ieee80211_ref_node(vap->iv_bss);
835 * Sanitize mbuf for net80211 flags leaked from above.
837 * NB: This must be done before ieee80211_classify as
838 * it marks EAPOL in frames with M_EAPOL.
840 m->m_flags &= ~M_80211_TX;
841 m->m_flags |= M_ENCAP; /* mark encapsulated */
843 if (IEEE80211_IS_DATA(wh)) {
844 /* calculate priority so drivers can find the tx queue */
845 if (ieee80211_classify(ni, m))
846 senderr(EIO); /* XXX */
848 /* NB: ieee80211_encap does not include 802.11 header */
849 IEEE80211_NODE_STAT_ADD(ni, tx_bytes,
850 m->m_pkthdr.len - ieee80211_hdrsize(wh));
852 M_WME_SETAC(m, WME_AC_BE);
854 error = ieee80211_sanitize_rates(ni, m, params);
858 IEEE80211_NODE_STAT(ni, tx_data);
859 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
860 IEEE80211_NODE_STAT(ni, tx_mcast);
861 m->m_flags |= M_MCAST;
863 IEEE80211_NODE_STAT(ni, tx_ucast);
865 IEEE80211_TX_LOCK(ic);
866 ret = ieee80211_raw_output(vap, ni, m, params);
867 IEEE80211_TX_UNLOCK(ic);
873 ieee80211_free_node(ni);
874 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
880 * Set the direction field and address fields of an outgoing
881 * frame. Note this should be called early on in constructing
882 * a frame as it sets i_fc[1]; other bits can then be or'd in.
885 ieee80211_send_setup(
886 struct ieee80211_node *ni,
889 const uint8_t sa[IEEE80211_ADDR_LEN],
890 const uint8_t da[IEEE80211_ADDR_LEN],
891 const uint8_t bssid[IEEE80211_ADDR_LEN])
893 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
894 struct ieee80211vap *vap = ni->ni_vap;
895 struct ieee80211_tx_ampdu *tap;
896 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
899 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
901 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
902 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
903 switch (vap->iv_opmode) {
904 case IEEE80211_M_STA:
905 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
906 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
907 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
908 IEEE80211_ADDR_COPY(wh->i_addr3, da);
910 case IEEE80211_M_IBSS:
911 case IEEE80211_M_AHDEMO:
912 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
913 IEEE80211_ADDR_COPY(wh->i_addr1, da);
914 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
915 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
917 case IEEE80211_M_HOSTAP:
918 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
919 IEEE80211_ADDR_COPY(wh->i_addr1, da);
920 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
921 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
923 case IEEE80211_M_WDS:
924 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
925 IEEE80211_ADDR_COPY(wh->i_addr1, da);
926 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
927 IEEE80211_ADDR_COPY(wh->i_addr3, da);
928 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
930 case IEEE80211_M_MBSS:
931 #ifdef IEEE80211_SUPPORT_MESH
932 if (IEEE80211_IS_MULTICAST(da)) {
933 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
935 IEEE80211_ADDR_COPY(wh->i_addr1, da);
936 IEEE80211_ADDR_COPY(wh->i_addr2,
939 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
940 IEEE80211_ADDR_COPY(wh->i_addr1, da);
941 IEEE80211_ADDR_COPY(wh->i_addr2,
943 IEEE80211_ADDR_COPY(wh->i_addr3, da);
944 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
948 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
952 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
953 IEEE80211_ADDR_COPY(wh->i_addr1, da);
954 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
955 #ifdef IEEE80211_SUPPORT_MESH
956 if (vap->iv_opmode == IEEE80211_M_MBSS)
957 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
960 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
962 *(uint16_t *)&wh->i_dur[0] = 0;
965 * XXX TODO: this is what the TX lock is for.
966 * Here we're incrementing sequence numbers, and they
967 * need to be in lock-step with what the driver is doing
968 * both in TX ordering and crypto encap (IV increment.)
970 * If the driver does seqno itself, then we can skip
971 * assigning sequence numbers here, and we can avoid
972 * requiring the TX lock.
974 tap = &ni->ni_tx_ampdu[tid];
975 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap)) {
976 m->m_flags |= M_AMPDU_MPDU;
978 /* NB: zero out i_seq field (for s/w encryption etc) */
979 *(uint16_t *)&wh->i_seq[0] = 0;
981 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
982 type & IEEE80211_FC0_SUBTYPE_MASK))
984 * 802.11-2012 9.3.2.10 - QoS multicast frames
985 * come out of a different seqno space.
987 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
988 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
990 seqno = ni->ni_txseqs[tid]++;
995 *(uint16_t *)&wh->i_seq[0] =
996 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
997 M_SEQNO_SET(m, seqno);
1000 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1001 m->m_flags |= M_MCAST;
1006 * Send a management frame to the specified node. The node pointer
1007 * must have a reference as the pointer will be passed to the driver
1008 * and potentially held for a long time. If the frame is successfully
1009 * dispatched to the driver, then it is responsible for freeing the
1010 * reference (and potentially free'ing up any associated storage);
1011 * otherwise deal with reclaiming any reference (on error).
1014 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
1015 struct ieee80211_bpf_params *params)
1017 struct ieee80211vap *vap = ni->ni_vap;
1018 struct ieee80211com *ic = ni->ni_ic;
1019 struct ieee80211_frame *wh;
1022 KASSERT(ni != NULL, ("null node"));
1024 if (vap->iv_state == IEEE80211_S_CAC) {
1025 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1026 ni, "block %s frame in CAC state",
1027 ieee80211_mgt_subtype_name(type));
1028 vap->iv_stats.is_tx_badstate++;
1029 ieee80211_free_node(ni);
1031 return EIO; /* XXX */
1034 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
1036 ieee80211_free_node(ni);
1040 IEEE80211_TX_LOCK(ic);
1042 wh = mtod(m, struct ieee80211_frame *);
1043 ieee80211_send_setup(ni, m,
1044 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
1045 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1046 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
1047 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
1048 "encrypting frame (%s)", __func__);
1049 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1051 m->m_flags |= M_ENCAP; /* mark encapsulated */
1053 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
1054 M_WME_SETAC(m, params->ibp_pri);
1056 #ifdef IEEE80211_DEBUG
1057 /* avoid printing too many frames */
1058 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
1059 ieee80211_msg_dumppkts(vap)) {
1060 ieee80211_note(vap, "[%s] send %s on channel %u\n",
1061 ether_sprintf(wh->i_addr1),
1062 ieee80211_mgt_subtype_name(type),
1063 ieee80211_chan2ieee(ic, ic->ic_curchan));
1066 IEEE80211_NODE_STAT(ni, tx_mgmt);
1068 ret = ieee80211_raw_output(vap, ni, m, params);
1069 IEEE80211_TX_UNLOCK(ic);
1074 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
1077 struct ieee80211vap *vap = ni->ni_vap;
1083 * Send a null data frame to the specified node. If the station
1084 * is setup for QoS then a QoS Null Data frame is constructed.
1085 * If this is a WDS station then a 4-address frame is constructed.
1087 * NB: the caller is assumed to have setup a node reference
1088 * for use; this is necessary to deal with a race condition
1089 * when probing for inactive stations. Like ieee80211_mgmt_output
1090 * we must cleanup any node reference on error; however we
1091 * can safely just unref it as we know it will never be the
1092 * last reference to the node.
1095 ieee80211_send_nulldata(struct ieee80211_node *ni)
1097 struct ieee80211vap *vap = ni->ni_vap;
1098 struct ieee80211com *ic = ni->ni_ic;
1100 struct ieee80211_frame *wh;
1105 if (vap->iv_state == IEEE80211_S_CAC) {
1106 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
1107 ni, "block %s frame in CAC state", "null data");
1108 ieee80211_unref_node(&ni);
1109 vap->iv_stats.is_tx_badstate++;
1110 return EIO; /* XXX */
1113 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1114 hdrlen = sizeof(struct ieee80211_qosframe);
1116 hdrlen = sizeof(struct ieee80211_frame);
1117 /* NB: only WDS vap's get 4-address frames */
1118 if (vap->iv_opmode == IEEE80211_M_WDS)
1119 hdrlen += IEEE80211_ADDR_LEN;
1120 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1121 hdrlen = roundup(hdrlen, sizeof(uint32_t));
1123 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1126 ieee80211_unref_node(&ni);
1127 vap->iv_stats.is_tx_nobuf++;
1130 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1131 ("leading space %zd", M_LEADINGSPACE(m)));
1132 M_PREPEND(m, hdrlen, IEEE80211_M_NOWAIT);
1134 /* NB: cannot happen */
1135 ieee80211_free_node(ni);
1139 IEEE80211_TX_LOCK(ic);
1141 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
1142 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1143 const int tid = WME_AC_TO_TID(WME_AC_BE);
1146 ieee80211_send_setup(ni, m,
1147 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1148 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1150 if (vap->iv_opmode == IEEE80211_M_WDS)
1151 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1153 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1154 qos[0] = tid & IEEE80211_QOS_TID;
1155 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1156 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1159 ieee80211_send_setup(ni, m,
1160 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1161 IEEE80211_NONQOS_TID,
1162 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1164 if (vap->iv_opmode != IEEE80211_M_WDS) {
1165 /* NB: power management bit is never sent by an AP */
1166 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1167 vap->iv_opmode != IEEE80211_M_HOSTAP)
1168 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1170 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1171 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1172 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1175 m->m_len = m->m_pkthdr.len = hdrlen;
1176 m->m_flags |= M_ENCAP; /* mark encapsulated */
1178 M_WME_SETAC(m, WME_AC_BE);
1180 IEEE80211_NODE_STAT(ni, tx_data);
1182 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1183 "send %snull data frame on channel %u, pwr mgt %s",
1184 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1185 ieee80211_chan2ieee(ic, ic->ic_curchan),
1186 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1188 ret = ieee80211_raw_output(vap, ni, m, NULL);
1189 IEEE80211_TX_UNLOCK(ic);
1194 * Assign priority to a frame based on any vlan tag assigned
1195 * to the station and/or any Diffserv setting in an IP header.
1196 * Finally, if an ACM policy is setup (in station mode) it's
1200 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1202 const struct ether_header *eh = NULL;
1203 uint16_t ether_type;
1204 int v_wme_ac, d_wme_ac, ac;
1206 if (__predict_false(m->m_flags & M_ENCAP)) {
1207 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
1209 int hdrlen, subtype;
1211 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1212 if (subtype & IEEE80211_FC0_SUBTYPE_NODATA) {
1217 hdrlen = ieee80211_hdrsize(wh);
1218 if (m->m_pkthdr.len < hdrlen + sizeof(*llc))
1221 llc = (struct llc *)mtodo(m, hdrlen);
1222 if (llc->llc_dsap != LLC_SNAP_LSAP ||
1223 llc->llc_ssap != LLC_SNAP_LSAP ||
1224 llc->llc_control != LLC_UI ||
1225 llc->llc_snap.org_code[0] != 0 ||
1226 llc->llc_snap.org_code[1] != 0 ||
1227 llc->llc_snap.org_code[2] != 0)
1230 ether_type = llc->llc_snap.ether_type;
1232 eh = mtod(m, struct ether_header *);
1233 ether_type = eh->ether_type;
1237 * Always promote PAE/EAPOL frames to high priority.
1239 if (ether_type == htons(ETHERTYPE_PAE)) {
1240 /* NB: mark so others don't need to check header */
1241 m->m_flags |= M_EAPOL;
1246 * Non-qos traffic goes to BE.
1248 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1254 * If node has a vlan tag then all traffic
1255 * to it must have a matching tag.
1258 if (ni->ni_vlan != 0) {
1259 if ((m->m_flags & M_VLANTAG) == 0) {
1260 IEEE80211_NODE_STAT(ni, tx_novlantag);
1263 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1264 EVL_VLANOFTAG(ni->ni_vlan)) {
1265 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1268 /* map vlan priority to AC */
1269 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1275 /* XXX m_copydata may be too slow for fast path */
1276 switch (ntohs(eh->ether_type)) {
1282 * IP frame, map the DSCP bits from the TOS field.
1284 /* NB: ip header may not be in first mbuf */
1285 m_copydata(m, sizeof(struct ether_header) +
1286 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1287 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1288 d_wme_ac = TID_TO_WME_AC(tos);
1293 case ETHERTYPE_IPV6:
1298 * IPv6 frame, map the DSCP bits from the traffic class field.
1300 m_copydata(m, sizeof(struct ether_header) +
1301 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1303 tos = (uint8_t)(ntohl(flow) >> 20);
1304 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1305 d_wme_ac = TID_TO_WME_AC(tos);
1311 d_wme_ac = WME_AC_BE;
1316 * Use highest priority AC.
1318 if (v_wme_ac > d_wme_ac)
1326 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1327 static const int acmap[4] = {
1328 WME_AC_BK, /* WME_AC_BE */
1329 WME_AC_BK, /* WME_AC_BK */
1330 WME_AC_BE, /* WME_AC_VI */
1331 WME_AC_VI, /* WME_AC_VO */
1333 struct ieee80211com *ic = ni->ni_ic;
1335 while (ac != WME_AC_BK &&
1336 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1345 * Insure there is sufficient contiguous space to encapsulate the
1346 * 802.11 data frame. If room isn't already there, arrange for it.
1347 * Drivers and cipher modules assume we have done the necessary work
1348 * and fail rudely if they don't find the space they need.
1351 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1352 struct ieee80211_key *key, struct mbuf *m)
1354 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1355 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1358 /* XXX belongs in crypto code? */
1359 needed_space += key->wk_cipher->ic_header;
1362 * When crypto is being done in the host we must insure
1363 * the data are writable for the cipher routines; clone
1364 * a writable mbuf chain.
1365 * XXX handle SWMIC specially
1367 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1368 m = m_unshare(m, IEEE80211_M_NOWAIT);
1370 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1371 "%s: cannot get writable mbuf\n", __func__);
1372 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1378 * We know we are called just before stripping an Ethernet
1379 * header and prepending an LLC header. This means we know
1381 * sizeof(struct ether_header) - sizeof(struct llc)
1382 * bytes recovered to which we need additional space for the
1383 * 802.11 header and any crypto header.
1385 /* XXX check trailing space and copy instead? */
1386 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1387 struct mbuf *n = m_gethdr(IEEE80211_M_NOWAIT, m->m_type);
1389 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1390 "%s: cannot expand storage\n", __func__);
1391 vap->iv_stats.is_tx_nobuf++;
1395 KASSERT(needed_space <= MHLEN,
1396 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1398 * Setup new mbuf to have leading space to prepend the
1399 * 802.11 header and any crypto header bits that are
1400 * required (the latter are added when the driver calls
1401 * back to ieee80211_crypto_encap to do crypto encapsulation).
1403 /* NB: must be first 'cuz it clobbers m_data */
1404 m_move_pkthdr(n, m);
1405 n->m_len = 0; /* NB: m_gethdr does not set */
1406 n->m_data += needed_space;
1408 * Pull up Ethernet header to create the expected layout.
1409 * We could use m_pullup but that's overkill (i.e. we don't
1410 * need the actual data) and it cannot fail so do it inline
1413 /* NB: struct ether_header is known to be contiguous */
1414 n->m_len += sizeof(struct ether_header);
1415 m->m_len -= sizeof(struct ether_header);
1416 m->m_data += sizeof(struct ether_header);
1418 * Replace the head of the chain.
1424 #undef TO_BE_RECLAIMED
1428 * Return the transmit key to use in sending a unicast frame.
1429 * If a unicast key is set we use that. When no unicast key is set
1430 * we fall back to the default transmit key.
1432 static __inline struct ieee80211_key *
1433 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1434 struct ieee80211_node *ni)
1436 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1437 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1438 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1440 return &vap->iv_nw_keys[vap->iv_def_txkey];
1442 return &ni->ni_ucastkey;
1447 * Return the transmit key to use in sending a multicast frame.
1448 * Multicast traffic always uses the group key which is installed as
1449 * the default tx key.
1451 static __inline struct ieee80211_key *
1452 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1453 struct ieee80211_node *ni)
1455 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1456 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1458 return &vap->iv_nw_keys[vap->iv_def_txkey];
1462 * Encapsulate an outbound data frame. The mbuf chain is updated.
1463 * If an error is encountered NULL is returned. The caller is required
1464 * to provide a node reference and pullup the ethernet header in the
1467 * NB: Packet is assumed to be processed by ieee80211_classify which
1468 * marked EAPOL frames w/ M_EAPOL.
1471 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1474 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1475 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1476 struct ieee80211com *ic = ni->ni_ic;
1477 #ifdef IEEE80211_SUPPORT_MESH
1478 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1479 struct ieee80211_meshcntl_ae10 *mc;
1480 struct ieee80211_mesh_route *rt = NULL;
1483 struct ether_header eh;
1484 struct ieee80211_frame *wh;
1485 struct ieee80211_key *key;
1487 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr, is_mcast;
1488 ieee80211_seq seqno;
1489 int meshhdrsize, meshae;
1493 IEEE80211_TX_LOCK_ASSERT(ic);
1495 is_mcast = !! (m->m_flags & (M_MCAST | M_BCAST));
1498 * Copy existing Ethernet header to a safe place. The
1499 * rest of the code assumes it's ok to strip it when
1500 * reorganizing state for the final encapsulation.
1502 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1503 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1506 * Insure space for additional headers. First identify
1507 * transmit key to use in calculating any buffer adjustments
1508 * required. This is also used below to do privacy
1509 * encapsulation work. Then calculate the 802.11 header
1510 * size and any padding required by the driver.
1512 * Note key may be NULL if we fall back to the default
1513 * transmit key and that is not set. In that case the
1514 * buffer may not be expanded as needed by the cipher
1515 * routines, but they will/should discard it.
1517 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1518 if (vap->iv_opmode == IEEE80211_M_STA ||
1519 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1520 (vap->iv_opmode == IEEE80211_M_WDS &&
1521 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1522 key = ieee80211_crypto_getucastkey(vap, ni);
1523 } else if ((vap->iv_opmode == IEEE80211_M_WDS) &&
1524 (! (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY))) {
1526 * Use ucastkey for DWDS transmit nodes, multicast
1529 * This is required to ensure that multicast frames
1530 * from a DWDS AP to a DWDS STA is encrypted with
1531 * a key that can actually work.
1533 * There's no default key for multicast traffic
1534 * on a DWDS WDS VAP node (note NOT the DWDS enabled
1535 * AP VAP, the dynamically created per-STA WDS node)
1536 * so encap fails and transmit fails.
1538 key = ieee80211_crypto_getucastkey(vap, ni);
1540 key = ieee80211_crypto_getmcastkey(vap, ni);
1542 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1543 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1545 "no default transmit key (%s) deftxkey %u",
1546 __func__, vap->iv_def_txkey);
1547 vap->iv_stats.is_tx_nodefkey++;
1553 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1554 * frames so suppress use. This may be an issue if other
1555 * ap's require all data frames to be QoS-encapsulated
1556 * once negotiated in which case we'll need to make this
1559 * Don't send multicast QoS frames.
1560 * Technically multicast frames can be QoS if all stations in the
1563 * NB: mesh data frames are QoS, including multicast frames.
1566 (((is_mcast == 0) && (ni->ni_flags &
1567 (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))) ||
1568 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1569 (m->m_flags & M_EAPOL) == 0;
1572 hdrsize = sizeof(struct ieee80211_qosframe);
1574 hdrsize = sizeof(struct ieee80211_frame);
1575 #ifdef IEEE80211_SUPPORT_MESH
1576 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1578 * Mesh data frames are encapsulated according to the
1579 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1580 * o Group Addressed data (aka multicast) originating
1581 * at the local sta are sent w/ 3-address format and
1582 * address extension mode 00
1583 * o Individually Addressed data (aka unicast) originating
1584 * at the local sta are sent w/ 4-address format and
1585 * address extension mode 00
1586 * o Group Addressed data forwarded from a non-mesh sta are
1587 * sent w/ 3-address format and address extension mode 01
1588 * o Individually Address data from another sta are sent
1589 * w/ 4-address format and address extension mode 10
1591 is4addr = 0; /* NB: don't use, disable */
1592 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1593 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1594 KASSERT(rt != NULL, ("route is NULL"));
1595 dir = IEEE80211_FC1_DIR_DSTODS;
1596 hdrsize += IEEE80211_ADDR_LEN;
1597 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1598 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1600 IEEE80211_NOTE_MAC(vap,
1603 "%s", "trying to send to ourself");
1606 meshae = IEEE80211_MESH_AE_10;
1608 sizeof(struct ieee80211_meshcntl_ae10);
1610 meshae = IEEE80211_MESH_AE_00;
1612 sizeof(struct ieee80211_meshcntl);
1615 dir = IEEE80211_FC1_DIR_FROMDS;
1616 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1618 meshae = IEEE80211_MESH_AE_01;
1620 sizeof(struct ieee80211_meshcntl_ae01);
1623 meshae = IEEE80211_MESH_AE_00;
1624 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1630 * 4-address frames need to be generated for:
1631 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1632 * o packets sent through a vap marked for relaying
1633 * (e.g. a station operating with dynamic WDS)
1635 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1636 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1637 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1639 hdrsize += IEEE80211_ADDR_LEN;
1640 meshhdrsize = meshae = 0;
1641 #ifdef IEEE80211_SUPPORT_MESH
1645 * Honor driver DATAPAD requirement.
1647 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1648 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1652 if (__predict_true((m->m_flags & M_FF) == 0)) {
1656 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1658 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1661 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1662 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1663 llc = mtod(m, struct llc *);
1664 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1665 llc->llc_control = LLC_UI;
1666 llc->llc_snap.org_code[0] = 0;
1667 llc->llc_snap.org_code[1] = 0;
1668 llc->llc_snap.org_code[2] = 0;
1669 llc->llc_snap.ether_type = eh.ether_type;
1671 #ifdef IEEE80211_SUPPORT_SUPERG
1673 * Aggregated frame. Check if it's for AMSDU or FF.
1675 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1676 * anywhere for some reason. But, since 11n requires
1677 * AMSDU RX, we can just assume "11n" == "AMSDU".
1679 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1680 if (ieee80211_amsdu_tx_ok(ni)) {
1681 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1684 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1690 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1692 M_PREPEND(m, hdrspace + meshhdrsize, IEEE80211_M_NOWAIT);
1694 vap->iv_stats.is_tx_nobuf++;
1697 wh = mtod(m, struct ieee80211_frame *);
1698 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1699 *(uint16_t *)wh->i_dur = 0;
1700 qos = NULL; /* NB: quiet compiler */
1702 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1703 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1704 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1705 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1706 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1707 } else switch (vap->iv_opmode) {
1708 case IEEE80211_M_STA:
1709 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1710 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1711 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1712 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1714 case IEEE80211_M_IBSS:
1715 case IEEE80211_M_AHDEMO:
1716 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1717 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1718 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1720 * NB: always use the bssid from iv_bss as the
1721 * neighbor's may be stale after an ibss merge
1723 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1725 case IEEE80211_M_HOSTAP:
1726 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1727 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1728 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1729 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1731 #ifdef IEEE80211_SUPPORT_MESH
1732 case IEEE80211_M_MBSS:
1733 /* NB: offset by hdrspace to deal with DATAPAD */
1734 mc = (struct ieee80211_meshcntl_ae10 *)
1735 (mtod(m, uint8_t *) + hdrspace);
1738 case IEEE80211_MESH_AE_00: /* no proxy */
1740 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1741 IEEE80211_ADDR_COPY(wh->i_addr1,
1743 IEEE80211_ADDR_COPY(wh->i_addr2,
1745 IEEE80211_ADDR_COPY(wh->i_addr3,
1747 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1749 qos =((struct ieee80211_qosframe_addr4 *)
1751 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1753 IEEE80211_ADDR_COPY(wh->i_addr1,
1755 IEEE80211_ADDR_COPY(wh->i_addr2,
1757 IEEE80211_ADDR_COPY(wh->i_addr3,
1759 qos = ((struct ieee80211_qosframe *)
1763 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1764 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1765 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1766 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1767 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1769 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1771 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1773 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1774 KASSERT(rt != NULL, ("route is NULL"));
1775 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1776 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1777 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1778 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1779 mc->mc_flags = IEEE80211_MESH_AE_10;
1780 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1781 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1782 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1785 KASSERT(0, ("meshae %d", meshae));
1788 mc->mc_ttl = ms->ms_ttl;
1790 le32enc(mc->mc_seq, ms->ms_seq);
1793 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1797 if (m->m_flags & M_MORE_DATA)
1798 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1803 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1804 /* NB: mesh case handled earlier */
1805 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1806 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1807 ac = M_WME_GETAC(m);
1808 /* map from access class/queue to 11e header priorty value */
1809 tid = WME_AC_TO_TID(ac);
1810 qos[0] = tid & IEEE80211_QOS_TID;
1811 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1812 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1813 #ifdef IEEE80211_SUPPORT_MESH
1814 if (vap->iv_opmode == IEEE80211_M_MBSS)
1815 qos[1] = IEEE80211_QOS_MC;
1819 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_DATA;
1822 * If this is an A-MSDU then ensure we set the
1826 qos[0] |= IEEE80211_QOS_AMSDU;
1829 * XXX TODO TX lock is needed for atomic updates of sequence
1830 * numbers. If the driver does it, then don't do it here;
1831 * and we don't need the TX lock held.
1833 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1835 * 802.11-2012 9.3.2.10 -
1837 * If this is a multicast frame then we need
1838 * to ensure that the sequence number comes from
1839 * a separate seqno space and not the TID space.
1841 * Otherwise multicast frames may actually cause
1842 * holes in the TX blockack window space and
1843 * upset various things.
1845 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1846 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1848 seqno = ni->ni_txseqs[tid]++;
1851 * NB: don't assign a sequence # to potential
1852 * aggregates; we expect this happens at the
1853 * point the frame comes off any aggregation q
1854 * as otherwise we may introduce holes in the
1855 * BA sequence space and/or make window accouting
1858 * XXX may want to control this with a driver
1859 * capability; this may also change when we pull
1860 * aggregation up into net80211
1862 *(uint16_t *)wh->i_seq =
1863 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1864 M_SEQNO_SET(m, seqno);
1866 /* NB: zero out i_seq field (for s/w encryption etc) */
1867 *(uint16_t *)wh->i_seq = 0;
1871 * XXX TODO TX lock is needed for atomic updates of sequence
1872 * numbers. If the driver does it, then don't do it here;
1873 * and we don't need the TX lock held.
1875 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1876 *(uint16_t *)wh->i_seq =
1877 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1878 M_SEQNO_SET(m, seqno);
1881 * XXX TODO: we shouldn't allow EAPOL, etc that would
1882 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1885 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1890 * Check if xmit fragmentation is required.
1892 * If the hardware does fragmentation offload, then don't bother
1895 if (IEEE80211_CONF_FRAG_OFFLOAD(ic))
1898 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1899 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1900 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1901 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1905 * IEEE 802.1X: send EAPOL frames always in the clear.
1906 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1908 if ((m->m_flags & M_EAPOL) == 0 ||
1909 ((vap->iv_flags & IEEE80211_F_WPA) &&
1910 (vap->iv_opmode == IEEE80211_M_STA ?
1911 !IEEE80211_KEY_UNDEFINED(key) :
1912 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1913 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1914 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1915 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1917 "%s", "enmic failed, discard frame");
1918 vap->iv_stats.is_crypto_enmicfail++;
1923 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1924 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1927 m->m_flags |= M_ENCAP; /* mark encapsulated */
1929 IEEE80211_NODE_STAT(ni, tx_data);
1930 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1931 IEEE80211_NODE_STAT(ni, tx_mcast);
1932 m->m_flags |= M_MCAST;
1934 IEEE80211_NODE_STAT(ni, tx_ucast);
1935 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1947 ieee80211_free_mbuf(struct mbuf *m)
1955 next = m->m_nextpkt;
1956 m->m_nextpkt = NULL;
1958 } while ((m = next) != NULL);
1962 * Fragment the frame according to the specified mtu.
1963 * The size of the 802.11 header (w/o padding) is provided
1964 * so we don't need to recalculate it. We create a new
1965 * mbuf for each fragment and chain it through m_nextpkt;
1966 * we might be able to optimize this by reusing the original
1967 * packet's mbufs but that is significantly more complicated.
1970 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1971 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1973 struct ieee80211com *ic = vap->iv_ic;
1974 struct ieee80211_frame *wh, *whf;
1975 struct mbuf *m, *prev;
1976 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1979 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1980 KASSERT(m0->m_pkthdr.len > mtu,
1981 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1984 * Honor driver DATAPAD requirement.
1986 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1987 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1991 wh = mtod(m0, struct ieee80211_frame *);
1992 /* NB: mark the first frag; it will be propagated below */
1993 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1994 totalhdrsize = hdrspace + ciphdrsize;
1996 off = mtu - ciphdrsize;
1997 remainder = m0->m_pkthdr.len - off;
2000 fragsize = MIN(totalhdrsize + remainder, mtu);
2001 m = m_get2(fragsize, IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
2004 /* leave room to prepend any cipher header */
2005 m_align(m, fragsize - ciphdrsize);
2008 * Form the header in the fragment. Note that since
2009 * we mark the first fragment with the MORE_FRAG bit
2010 * it automatically is propagated to each fragment; we
2011 * need only clear it on the last fragment (done below).
2012 * NB: frag 1+ dont have Mesh Control field present.
2014 whf = mtod(m, struct ieee80211_frame *);
2015 memcpy(whf, wh, hdrsize);
2016 #ifdef IEEE80211_SUPPORT_MESH
2017 if (vap->iv_opmode == IEEE80211_M_MBSS)
2018 ieee80211_getqos(wh)[1] &= ~IEEE80211_QOS_MC;
2020 *(uint16_t *)&whf->i_seq[0] |= htole16(
2021 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
2022 IEEE80211_SEQ_FRAG_SHIFT);
2025 payload = fragsize - totalhdrsize;
2026 /* NB: destination is known to be contiguous */
2028 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
2029 m->m_len = hdrspace + payload;
2030 m->m_pkthdr.len = hdrspace + payload;
2031 m->m_flags |= M_FRAG;
2033 /* chain up the fragment */
2034 prev->m_nextpkt = m;
2037 /* deduct fragment just formed */
2038 remainder -= payload;
2040 } while (remainder != 0);
2042 /* set the last fragment */
2043 m->m_flags |= M_LASTFRAG;
2044 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
2046 /* strip first mbuf now that everything has been copied */
2047 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
2048 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
2050 vap->iv_stats.is_tx_fragframes++;
2051 vap->iv_stats.is_tx_frags += fragno-1;
2055 /* reclaim fragments but leave original frame for caller to free */
2056 ieee80211_free_mbuf(m0->m_nextpkt);
2057 m0->m_nextpkt = NULL;
2062 * Add a supported rates element id to a frame.
2065 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
2069 *frm++ = IEEE80211_ELEMID_RATES;
2070 nrates = rs->rs_nrates;
2071 if (nrates > IEEE80211_RATE_SIZE)
2072 nrates = IEEE80211_RATE_SIZE;
2074 memcpy(frm, rs->rs_rates, nrates);
2075 return frm + nrates;
2079 * Add an extended supported rates element id to a frame.
2082 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
2085 * Add an extended supported rates element if operating in 11g mode.
2087 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2088 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2089 *frm++ = IEEE80211_ELEMID_XRATES;
2091 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2098 * Add an ssid element to a frame.
2101 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
2103 *frm++ = IEEE80211_ELEMID_SSID;
2105 memcpy(frm, ssid, len);
2110 * Add an erp element to a frame.
2113 ieee80211_add_erp(uint8_t *frm, struct ieee80211vap *vap)
2115 struct ieee80211com *ic = vap->iv_ic;
2118 *frm++ = IEEE80211_ELEMID_ERP;
2123 * TODO: This uses the global flags for now because
2124 * the per-VAP flags are fine for per-VAP, but don't
2125 * take into account which VAPs share the same channel
2126 * and which are on different channels.
2128 * ERP and HT/VHT protection mode is a function of
2129 * how many stations are on a channel, not specifically
2130 * the VAP or global. But, until we grow that status,
2131 * the global flag will have to do.
2133 if (ic->ic_flags_ext & IEEE80211_FEXT_NONERP_PR)
2134 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
2137 * TODO: same as above; these should be based not
2138 * on the vap or ic flags, but instead on a combination
2139 * of per-VAP and channels.
2141 if (ic->ic_flags & IEEE80211_F_USEPROT)
2142 erp |= IEEE80211_ERP_USE_PROTECTION;
2143 if (ic->ic_flags & IEEE80211_F_USEBARKER)
2144 erp |= IEEE80211_ERP_LONG_PREAMBLE;
2150 * Add a CFParams element to a frame.
2153 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
2155 #define ADDSHORT(frm, v) do { \
2159 *frm++ = IEEE80211_ELEMID_CFPARMS;
2161 *frm++ = 0; /* CFP count */
2162 *frm++ = 2; /* CFP period */
2163 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
2164 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
2169 static __inline uint8_t *
2170 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
2172 memcpy(frm, ie->ie_data, ie->ie_len);
2173 return frm + ie->ie_len;
2176 static __inline uint8_t *
2177 add_ie(uint8_t *frm, const uint8_t *ie)
2179 memcpy(frm, ie, 2 + ie[1]);
2180 return frm + 2 + ie[1];
2183 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
2185 * Add a WME information element to a frame.
2188 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme,
2189 struct ieee80211_node *ni)
2191 static const uint8_t oui[4] = { WME_OUI_BYTES, WME_OUI_TYPE };
2192 struct ieee80211vap *vap = ni->ni_vap;
2194 *frm++ = IEEE80211_ELEMID_VENDOR;
2195 *frm++ = sizeof(struct ieee80211_wme_info) - 2;
2196 memcpy(frm, oui, sizeof(oui));
2198 *frm++ = WME_INFO_OUI_SUBTYPE;
2199 *frm++ = WME_VERSION;
2201 /* QoS info field depends upon operating mode */
2202 switch (vap->iv_opmode) {
2203 case IEEE80211_M_HOSTAP:
2204 *frm = wme->wme_bssChanParams.cap_info;
2205 if (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD)
2206 *frm |= WME_CAPINFO_UAPSD_EN;
2209 case IEEE80211_M_STA:
2211 * NB: UAPSD drivers must set this up in their
2212 * VAP creation method.
2214 *frm++ = vap->iv_uapsdinfo;
2225 * Add a WME parameters element to a frame.
2228 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme,
2231 #define ADDSHORT(frm, v) do { \
2235 /* NB: this works 'cuz a param has an info at the front */
2236 static const struct ieee80211_wme_info param = {
2237 .wme_id = IEEE80211_ELEMID_VENDOR,
2238 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2239 .wme_oui = { WME_OUI_BYTES },
2240 .wme_type = WME_OUI_TYPE,
2241 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2242 .wme_version = WME_VERSION,
2246 memcpy(frm, ¶m, sizeof(param));
2247 frm += __offsetof(struct ieee80211_wme_info, wme_info);
2248 *frm = wme->wme_bssChanParams.cap_info; /* AC info */
2250 *frm |= WME_CAPINFO_UAPSD_EN;
2252 *frm++ = 0; /* reserved field */
2253 /* XXX TODO - U-APSD bits - SP, flags below */
2254 for (i = 0; i < WME_NUM_AC; i++) {
2255 const struct wmeParams *ac =
2256 &wme->wme_bssChanParams.cap_wmeParams[i];
2257 *frm++ = _IEEE80211_SHIFTMASK(i, WME_PARAM_ACI)
2258 | _IEEE80211_SHIFTMASK(ac->wmep_acm, WME_PARAM_ACM)
2259 | _IEEE80211_SHIFTMASK(ac->wmep_aifsn, WME_PARAM_AIFSN)
2261 *frm++ = _IEEE80211_SHIFTMASK(ac->wmep_logcwmax,
2263 | _IEEE80211_SHIFTMASK(ac->wmep_logcwmin,
2266 ADDSHORT(frm, ac->wmep_txopLimit);
2271 #undef WME_OUI_BYTES
2274 * Add an 11h Power Constraint element to a frame.
2277 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2279 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2280 /* XXX per-vap tx power limit? */
2281 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2283 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2285 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
2290 * Add an 11h Power Capability element to a frame.
2293 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2295 frm[0] = IEEE80211_ELEMID_PWRCAP;
2297 frm[2] = c->ic_minpower;
2298 frm[3] = c->ic_maxpower;
2303 * Add an 11h Supported Channels element to a frame.
2306 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2308 static const int ielen = 26;
2310 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2312 /* XXX not correct */
2313 memcpy(frm+2, ic->ic_chan_avail, ielen);
2314 return frm + 2 + ielen;
2318 * Add an 11h Quiet time element to a frame.
2321 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap, int update)
2323 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2325 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2329 * Only update every beacon interval - otherwise probe responses
2330 * would update the quiet count value.
2333 if (vap->iv_quiet_count_value == 1)
2334 vap->iv_quiet_count_value = vap->iv_quiet_count;
2335 else if (vap->iv_quiet_count_value > 1)
2336 vap->iv_quiet_count_value--;
2339 if (vap->iv_quiet_count_value == 0) {
2340 /* value 0 is reserved as per 802.11h standerd */
2341 vap->iv_quiet_count_value = 1;
2344 quiet->tbttcount = vap->iv_quiet_count_value;
2345 quiet->period = vap->iv_quiet_period;
2346 quiet->duration = htole16(vap->iv_quiet_duration);
2347 quiet->offset = htole16(vap->iv_quiet_offset);
2348 return frm + sizeof(*quiet);
2352 * Add an 11h Channel Switch Announcement element to a frame.
2353 * Note that we use the per-vap CSA count to adjust the global
2354 * counter so we can use this routine to form probe response
2355 * frames and get the current count.
2358 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2360 struct ieee80211com *ic = vap->iv_ic;
2361 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2363 csa->csa_ie = IEEE80211_ELEMID_CSA;
2365 csa->csa_mode = 1; /* XXX force quiet on channel */
2366 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2367 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2368 return frm + sizeof(*csa);
2372 * Add an 11h country information element to a frame.
2375 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2378 if (ic->ic_countryie == NULL ||
2379 ic->ic_countryie_chan != ic->ic_bsschan) {
2381 * Handle lazy construction of ie. This is done on
2382 * first use and after a channel change that requires
2385 if (ic->ic_countryie != NULL)
2386 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2387 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2388 if (ic->ic_countryie == NULL)
2390 ic->ic_countryie_chan = ic->ic_bsschan;
2392 return add_appie(frm, ic->ic_countryie);
2396 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2398 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2399 return (add_ie(frm, vap->iv_wpa_ie));
2401 /* XXX else complain? */
2407 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2409 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2410 return (add_ie(frm, vap->iv_rsn_ie));
2412 /* XXX else complain? */
2418 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2420 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2421 *frm++ = IEEE80211_ELEMID_QOS;
2430 * ieee80211_send_probereq(): send a probe request frame with the specified ssid
2431 * and any optional information element data; some helper functions as FW based
2432 * HW scans need some of that information passed too.
2435 ieee80211_probereq_ie_len(struct ieee80211vap *vap, struct ieee80211com *ic)
2437 const struct ieee80211_rateset *rs;
2439 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2442 * prreq frame format
2444 * [tlv] supported rates
2445 * [tlv] extended supported rates (if needed)
2446 * [tlv] HT cap (optional)
2447 * [tlv] VHT cap (optional)
2448 * [tlv] WPA (optional)
2449 * [tlv] user-specified ie's
2451 return ( 2 + IEEE80211_NWID_LEN
2452 + 2 + IEEE80211_RATE_SIZE
2453 + ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
2454 2 + (rs->rs_nrates - IEEE80211_RATE_SIZE) : 0)
2455 + (((vap->iv_opmode == IEEE80211_M_IBSS) &&
2456 (vap->iv_flags_ht & IEEE80211_FHT_HT)) ?
2457 sizeof(struct ieee80211_ie_htcap) : 0)
2459 + sizeof(struct ieee80211_ie_htinfo) /* XXX not needed? */
2460 + sizeof(struct ieee80211_ie_vhtcap)
2462 + ((vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL) ?
2463 vap->iv_wpa_ie[1] : 0)
2464 + (vap->iv_appie_probereq != NULL ?
2465 vap->iv_appie_probereq->ie_len : 0)
2470 ieee80211_probereq_ie(struct ieee80211vap *vap, struct ieee80211com *ic,
2471 uint8_t **frmp, uint32_t *frmlen, const uint8_t *ssid, size_t ssidlen,
2474 const struct ieee80211_rateset *rs;
2478 if (!alloc && (frmp == NULL || frmlen == NULL))
2481 len = ieee80211_probereq_ie_len(vap, ic);
2482 if (!alloc && len > *frmlen)
2485 /* For HW scans we usually do not pass in the SSID as IE. */
2487 len -= (2 + IEEE80211_NWID_LEN);
2490 frm = IEEE80211_MALLOC(len, M_80211_VAP,
2491 IEEE80211_M_WAITOK | IEEE80211_M_ZERO);
2498 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2499 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2500 frm = ieee80211_add_rates(frm, rs);
2501 frm = ieee80211_add_xrates(frm, rs);
2504 * Note: we can't use bss; we don't have one yet.
2506 * So, we should announce our capabilities
2507 * in this channel mode (2g/5g), not the
2508 * channel details itself.
2510 if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2511 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2512 struct ieee80211_channel *c;
2515 * Get the HT channel that we should try upgrading to.
2516 * If we can do 40MHz then this'll upgrade it appropriately.
2518 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2520 frm = ieee80211_add_htcap_ch(frm, vap, c);
2524 * XXX TODO: need to figure out what/how to update the
2528 if (vap->iv_flags_vht & IEEE80211_FVHT_VHT) {
2529 struct ieee80211_channel *c;
2531 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2533 c = ieee80211_vht_adjust_channel(ic, c, vap->iv_flags_vht);
2534 frm = ieee80211_add_vhtcap_ch(frm, vap, c);
2538 frm = ieee80211_add_wpa(frm, vap);
2539 if (vap->iv_appie_probereq != NULL)
2540 frm = add_appie(frm, vap->iv_appie_probereq);
2551 ieee80211_send_probereq(struct ieee80211_node *ni,
2552 const uint8_t sa[IEEE80211_ADDR_LEN],
2553 const uint8_t da[IEEE80211_ADDR_LEN],
2554 const uint8_t bssid[IEEE80211_ADDR_LEN],
2555 const uint8_t *ssid, size_t ssidlen)
2557 struct ieee80211vap *vap = ni->ni_vap;
2558 struct ieee80211com *ic = ni->ni_ic;
2559 struct ieee80211_node *bss;
2560 const struct ieee80211_txparam *tp;
2561 struct ieee80211_bpf_params params;
2567 bss = ieee80211_ref_node(vap->iv_bss);
2569 if (vap->iv_state == IEEE80211_S_CAC) {
2570 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2571 "block %s frame in CAC state", "probe request");
2572 vap->iv_stats.is_tx_badstate++;
2573 ieee80211_free_node(bss);
2574 return EIO; /* XXX */
2578 * Hold a reference on the node so it doesn't go away until after
2579 * the xmit is complete all the way in the driver. On error we
2580 * will remove our reference.
2582 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2583 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2585 ni, ether_sprintf(ni->ni_macaddr),
2586 ieee80211_node_refcnt(ni)+1);
2587 ieee80211_ref_node(ni);
2589 /* See comments above for entire frame format. */
2590 frmlen = ieee80211_probereq_ie_len(vap, ic);
2591 m = ieee80211_getmgtframe(&frm,
2592 ic->ic_headroom + sizeof(struct ieee80211_frame), frmlen);
2594 vap->iv_stats.is_tx_nobuf++;
2595 ieee80211_free_node(ni);
2596 ieee80211_free_node(bss);
2600 ret = ieee80211_probereq_ie(vap, ic, &frm, &frmlen, ssid, ssidlen,
2603 ("%s: ieee80211_probereq_ie failed: %d\n", __func__, ret));
2605 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2606 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2607 ("leading space %zd", M_LEADINGSPACE(m)));
2608 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
2610 /* NB: cannot happen */
2611 ieee80211_free_node(ni);
2612 ieee80211_free_node(bss);
2616 IEEE80211_TX_LOCK(ic);
2617 ieee80211_send_setup(ni, m,
2618 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2619 IEEE80211_NONQOS_TID, sa, da, bssid);
2620 /* XXX power management? */
2621 m->m_flags |= M_ENCAP; /* mark encapsulated */
2623 M_WME_SETAC(m, WME_AC_BE);
2625 IEEE80211_NODE_STAT(ni, tx_probereq);
2626 IEEE80211_NODE_STAT(ni, tx_mgmt);
2628 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2629 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2630 ieee80211_chan2ieee(ic, ic->ic_curchan),
2631 ether_sprintf(bssid),
2636 memset(¶ms, 0, sizeof(params));
2637 params.ibp_pri = M_WME_GETAC(m);
2638 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2639 params.ibp_rate0 = tp->mgmtrate;
2640 if (IEEE80211_IS_MULTICAST(da)) {
2641 params.ibp_flags |= IEEE80211_BPF_NOACK;
2642 params.ibp_try0 = 1;
2644 params.ibp_try0 = tp->maxretry;
2645 params.ibp_power = ni->ni_txpower;
2646 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2647 IEEE80211_TX_UNLOCK(ic);
2648 ieee80211_free_node(bss);
2653 * Calculate capability information for mgt frames.
2656 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2660 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2662 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2663 capinfo = IEEE80211_CAPINFO_ESS;
2664 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2665 capinfo = IEEE80211_CAPINFO_IBSS;
2668 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2669 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2670 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2671 IEEE80211_IS_CHAN_2GHZ(chan))
2672 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2673 if (vap->iv_flags & IEEE80211_F_SHSLOT)
2674 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2675 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2676 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2681 * Send a management frame. The node is for the destination (or ic_bss
2682 * when in station mode). Nodes other than ic_bss have their reference
2683 * count bumped to reflect our use for an indeterminant time.
2686 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2688 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2689 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2690 struct ieee80211vap *vap = ni->ni_vap;
2691 struct ieee80211com *ic = ni->ni_ic;
2692 struct ieee80211_node *bss = vap->iv_bss;
2693 struct ieee80211_bpf_params params;
2697 int has_challenge, is_shared_key, ret, status;
2699 KASSERT(ni != NULL, ("null node"));
2702 * Hold a reference on the node so it doesn't go away until after
2703 * the xmit is complete all the way in the driver. On error we
2704 * will remove our reference.
2706 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2707 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2709 ni, ether_sprintf(ni->ni_macaddr),
2710 ieee80211_node_refcnt(ni)+1);
2711 ieee80211_ref_node(ni);
2713 memset(¶ms, 0, sizeof(params));
2715 case IEEE80211_FC0_SUBTYPE_AUTH:
2718 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2719 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2720 ni->ni_challenge != NULL);
2723 * Deduce whether we're doing open authentication or
2724 * shared key authentication. We do the latter if
2725 * we're in the middle of a shared key authentication
2726 * handshake or if we're initiating an authentication
2727 * request and configured to use shared key.
2729 is_shared_key = has_challenge ||
2730 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2731 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2732 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2734 m = ieee80211_getmgtframe(&frm,
2735 ic->ic_headroom + sizeof(struct ieee80211_frame),
2736 3 * sizeof(uint16_t)
2737 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2738 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0));
2740 senderr(ENOMEM, is_tx_nobuf);
2742 ((uint16_t *)frm)[0] =
2743 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2744 : htole16(IEEE80211_AUTH_ALG_OPEN);
2745 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2746 ((uint16_t *)frm)[2] = htole16(status);/* status */
2748 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2749 ((uint16_t *)frm)[3] =
2750 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2751 IEEE80211_ELEMID_CHALLENGE);
2752 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2753 IEEE80211_CHALLENGE_LEN);
2754 m->m_pkthdr.len = m->m_len =
2755 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2756 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2757 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2758 "request encrypt frame (%s)", __func__);
2759 /* mark frame for encryption */
2760 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2763 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2765 /* XXX not right for shared key */
2766 if (status == IEEE80211_STATUS_SUCCESS)
2767 IEEE80211_NODE_STAT(ni, tx_auth);
2769 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2771 if (vap->iv_opmode == IEEE80211_M_STA)
2772 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2773 (void *) vap->iv_state);
2776 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2777 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2778 "send station deauthenticate (reason: %d (%s))", arg,
2779 ieee80211_reason_to_string(arg));
2780 m = ieee80211_getmgtframe(&frm,
2781 ic->ic_headroom + sizeof(struct ieee80211_frame),
2784 senderr(ENOMEM, is_tx_nobuf);
2785 *(uint16_t *)frm = htole16(arg); /* reason */
2786 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2788 IEEE80211_NODE_STAT(ni, tx_deauth);
2789 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2791 ieee80211_node_unauthorize(ni); /* port closed */
2794 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2795 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2797 * asreq frame format
2798 * [2] capability information
2799 * [2] listen interval
2800 * [6*] current AP address (reassoc only)
2802 * [tlv] supported rates
2803 * [tlv] extended supported rates
2804 * [4] power capability (optional)
2805 * [28] supported channels (optional)
2806 * [tlv] HT capabilities
2807 * [tlv] VHT capabilities
2808 * [tlv] WME (optional)
2809 * [tlv] Vendor OUI HT capabilities (optional)
2810 * [tlv] Atheros capabilities (if negotiated)
2811 * [tlv] AppIE's (optional)
2813 m = ieee80211_getmgtframe(&frm,
2814 ic->ic_headroom + sizeof(struct ieee80211_frame),
2817 + IEEE80211_ADDR_LEN
2818 + 2 + IEEE80211_NWID_LEN
2819 + 2 + IEEE80211_RATE_SIZE
2820 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2823 + sizeof(struct ieee80211_wme_info)
2824 + sizeof(struct ieee80211_ie_htcap)
2825 + sizeof(struct ieee80211_ie_vhtcap)
2826 + 4 + sizeof(struct ieee80211_ie_htcap)
2827 #ifdef IEEE80211_SUPPORT_SUPERG
2828 + sizeof(struct ieee80211_ath_ie)
2830 + (vap->iv_appie_wpa != NULL ?
2831 vap->iv_appie_wpa->ie_len : 0)
2832 + (vap->iv_appie_assocreq != NULL ?
2833 vap->iv_appie_assocreq->ie_len : 0)
2836 senderr(ENOMEM, is_tx_nobuf);
2838 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2839 ("wrong mode %u", vap->iv_opmode));
2840 capinfo = IEEE80211_CAPINFO_ESS;
2841 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2842 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2844 * NB: Some 11a AP's reject the request when
2845 * short preamble is set.
2847 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) &&
2848 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2849 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2850 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2851 (ic->ic_caps & IEEE80211_C_SHSLOT))
2852 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2853 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2854 (vap->iv_flags & IEEE80211_F_DOTH))
2855 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2856 *(uint16_t *)frm = htole16(capinfo);
2859 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2860 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2864 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2865 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2866 frm += IEEE80211_ADDR_LEN;
2869 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2870 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2871 frm = ieee80211_add_rsn(frm, vap);
2872 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2873 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2874 frm = ieee80211_add_powercapability(frm,
2876 frm = ieee80211_add_supportedchannels(frm, ic);
2880 * Check the channel - we may be using an 11n NIC with an
2881 * 11n capable station, but we're configured to be an 11b
2884 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2885 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2886 ni->ni_ies.htcap_ie != NULL &&
2887 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2888 frm = ieee80211_add_htcap(frm, ni);
2891 if ((vap->iv_flags_vht & IEEE80211_FVHT_VHT) &&
2892 IEEE80211_IS_CHAN_VHT(ni->ni_chan) &&
2893 ni->ni_ies.vhtcap_ie != NULL &&
2894 ni->ni_ies.vhtcap_ie[0] == IEEE80211_ELEMID_VHT_CAP) {
2895 frm = ieee80211_add_vhtcap(frm, ni);
2898 frm = ieee80211_add_wpa(frm, vap);
2899 if ((vap->iv_flags & IEEE80211_F_WME) &&
2900 ni->ni_ies.wme_ie != NULL)
2901 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
2904 * Same deal - only send HT info if we're on an 11n
2907 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2908 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2909 ni->ni_ies.htcap_ie != NULL &&
2910 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2911 frm = ieee80211_add_htcap_vendor(frm, ni);
2913 #ifdef IEEE80211_SUPPORT_SUPERG
2914 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2915 frm = ieee80211_add_ath(frm,
2916 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2917 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2918 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2919 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2921 #endif /* IEEE80211_SUPPORT_SUPERG */
2922 if (vap->iv_appie_assocreq != NULL)
2923 frm = add_appie(frm, vap->iv_appie_assocreq);
2924 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2926 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2927 (void *) vap->iv_state);
2930 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2931 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2933 * asresp frame format
2934 * [2] capability information
2936 * [2] association ID
2937 * [tlv] supported rates
2938 * [tlv] extended supported rates
2939 * [tlv] HT capabilities (standard, if STA enabled)
2940 * [tlv] HT information (standard, if STA enabled)
2941 * [tlv] VHT capabilities (standard, if STA enabled)
2942 * [tlv] VHT information (standard, if STA enabled)
2943 * [tlv] WME (if configured and STA enabled)
2944 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2945 * [tlv] HT information (vendor OUI, if STA enabled)
2946 * [tlv] Atheros capabilities (if STA enabled)
2947 * [tlv] AppIE's (optional)
2949 m = ieee80211_getmgtframe(&frm,
2950 ic->ic_headroom + sizeof(struct ieee80211_frame),
2954 + 2 + IEEE80211_RATE_SIZE
2955 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2956 + sizeof(struct ieee80211_ie_htcap) + 4
2957 + sizeof(struct ieee80211_ie_htinfo) + 4
2958 + sizeof(struct ieee80211_ie_vhtcap)
2959 + sizeof(struct ieee80211_ie_vht_operation)
2960 + sizeof(struct ieee80211_wme_param)
2961 #ifdef IEEE80211_SUPPORT_SUPERG
2962 + sizeof(struct ieee80211_ath_ie)
2964 + (vap->iv_appie_assocresp != NULL ?
2965 vap->iv_appie_assocresp->ie_len : 0)
2968 senderr(ENOMEM, is_tx_nobuf);
2970 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2971 *(uint16_t *)frm = htole16(capinfo);
2974 *(uint16_t *)frm = htole16(arg); /* status */
2977 if (arg == IEEE80211_STATUS_SUCCESS) {
2978 *(uint16_t *)frm = htole16(ni->ni_associd);
2979 IEEE80211_NODE_STAT(ni, tx_assoc);
2981 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2984 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2985 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2986 /* NB: respond according to what we received */
2987 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2988 frm = ieee80211_add_htcap(frm, ni);
2989 frm = ieee80211_add_htinfo(frm, ni);
2991 if ((vap->iv_flags & IEEE80211_F_WME) &&
2992 ni->ni_ies.wme_ie != NULL)
2993 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
2994 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
2995 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2996 frm = ieee80211_add_htcap_vendor(frm, ni);
2997 frm = ieee80211_add_htinfo_vendor(frm, ni);
2999 if (ni->ni_flags & IEEE80211_NODE_VHT) {
3000 frm = ieee80211_add_vhtcap(frm, ni);
3001 frm = ieee80211_add_vhtinfo(frm, ni);
3003 #ifdef IEEE80211_SUPPORT_SUPERG
3004 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
3005 frm = ieee80211_add_ath(frm,
3006 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
3007 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
3008 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
3009 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
3010 #endif /* IEEE80211_SUPPORT_SUPERG */
3011 if (vap->iv_appie_assocresp != NULL)
3012 frm = add_appie(frm, vap->iv_appie_assocresp);
3013 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3016 case IEEE80211_FC0_SUBTYPE_DISASSOC:
3017 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
3018 "send station disassociate (reason: %d (%s))", arg,
3019 ieee80211_reason_to_string(arg));
3020 m = ieee80211_getmgtframe(&frm,
3021 ic->ic_headroom + sizeof(struct ieee80211_frame),
3024 senderr(ENOMEM, is_tx_nobuf);
3025 *(uint16_t *)frm = htole16(arg); /* reason */
3026 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
3028 IEEE80211_NODE_STAT(ni, tx_disassoc);
3029 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
3033 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
3034 "invalid mgmt frame type %u", type);
3035 senderr(EINVAL, is_tx_unknownmgt);
3039 /* NB: force non-ProbeResp frames to the highest queue */
3040 params.ibp_pri = WME_AC_VO;
3041 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
3042 /* NB: we know all frames are unicast */
3043 params.ibp_try0 = bss->ni_txparms->maxretry;
3044 params.ibp_power = bss->ni_txpower;
3045 return ieee80211_mgmt_output(ni, m, type, ¶ms);
3047 ieee80211_free_node(ni);
3054 * Return an mbuf with a probe response frame in it.
3055 * Space is left to prepend and 802.11 header at the
3056 * front but it's left to the caller to fill in.
3059 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
3061 struct ieee80211vap *vap = bss->ni_vap;
3062 struct ieee80211com *ic = bss->ni_ic;
3063 const struct ieee80211_rateset *rs;
3069 * probe response frame format
3071 * [2] beacon interval
3072 * [2] cabability information
3074 * [tlv] supported rates
3075 * [tlv] parameter set (FH/DS)
3076 * [tlv] parameter set (IBSS)
3077 * [tlv] country (optional)
3078 * [3] power control (optional)
3079 * [5] channel switch announcement (CSA) (optional)
3080 * [tlv] extended rate phy (ERP)
3081 * [tlv] extended supported rates
3082 * [tlv] RSN (optional)
3083 * [tlv] HT capabilities
3084 * [tlv] HT information
3085 * [tlv] VHT capabilities
3086 * [tlv] VHT information
3087 * [tlv] WPA (optional)
3088 * [tlv] WME (optional)
3089 * [tlv] Vendor OUI HT capabilities (optional)
3090 * [tlv] Vendor OUI HT information (optional)
3091 * [tlv] Atheros capabilities
3092 * [tlv] AppIE's (optional)
3093 * [tlv] Mesh ID (MBSS)
3094 * [tlv] Mesh Conf (MBSS)
3096 m = ieee80211_getmgtframe(&frm,
3097 ic->ic_headroom + sizeof(struct ieee80211_frame),
3101 + 2 + IEEE80211_NWID_LEN
3102 + 2 + IEEE80211_RATE_SIZE
3104 + IEEE80211_COUNTRY_MAX_SIZE
3106 + sizeof(struct ieee80211_csa_ie)
3107 + sizeof(struct ieee80211_quiet_ie)
3109 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3110 + sizeof(struct ieee80211_ie_wpa)
3111 + sizeof(struct ieee80211_ie_htcap)
3112 + sizeof(struct ieee80211_ie_htinfo)
3113 + sizeof(struct ieee80211_ie_wpa)
3114 + sizeof(struct ieee80211_wme_param)
3115 + 4 + sizeof(struct ieee80211_ie_htcap)
3116 + 4 + sizeof(struct ieee80211_ie_htinfo)
3117 + sizeof(struct ieee80211_ie_vhtcap)
3118 + sizeof(struct ieee80211_ie_vht_operation)
3119 #ifdef IEEE80211_SUPPORT_SUPERG
3120 + sizeof(struct ieee80211_ath_ie)
3122 #ifdef IEEE80211_SUPPORT_MESH
3123 + 2 + IEEE80211_MESHID_LEN
3124 + sizeof(struct ieee80211_meshconf_ie)
3126 + (vap->iv_appie_proberesp != NULL ?
3127 vap->iv_appie_proberesp->ie_len : 0)
3130 vap->iv_stats.is_tx_nobuf++;
3134 memset(frm, 0, 8); /* timestamp should be filled later */
3136 *(uint16_t *)frm = htole16(bss->ni_intval);
3138 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
3139 *(uint16_t *)frm = htole16(capinfo);
3142 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
3143 rs = ieee80211_get_suprates(ic, bss->ni_chan);
3144 frm = ieee80211_add_rates(frm, rs);
3146 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
3147 *frm++ = IEEE80211_ELEMID_FHPARMS;
3149 *frm++ = bss->ni_fhdwell & 0x00ff;
3150 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
3151 *frm++ = IEEE80211_FH_CHANSET(
3152 ieee80211_chan2ieee(ic, bss->ni_chan));
3153 *frm++ = IEEE80211_FH_CHANPAT(
3154 ieee80211_chan2ieee(ic, bss->ni_chan));
3155 *frm++ = bss->ni_fhindex;
3157 *frm++ = IEEE80211_ELEMID_DSPARMS;
3159 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
3162 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3163 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3165 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3167 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3168 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3169 frm = ieee80211_add_countryie(frm, ic);
3170 if (vap->iv_flags & IEEE80211_F_DOTH) {
3171 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
3172 frm = ieee80211_add_powerconstraint(frm, vap);
3173 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3174 frm = ieee80211_add_csa(frm, vap);
3176 if (vap->iv_flags & IEEE80211_F_DOTH) {
3177 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3178 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3180 frm = ieee80211_add_quiet(frm, vap, 0);
3183 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
3184 frm = ieee80211_add_erp(frm, vap);
3185 frm = ieee80211_add_xrates(frm, rs);
3186 frm = ieee80211_add_rsn(frm, vap);
3188 * NB: legacy 11b clients do not get certain ie's.
3189 * The caller identifies such clients by passing
3190 * a token in legacy to us. Could expand this to be
3191 * any legacy client for stuff like HT ie's.
3193 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3194 legacy != IEEE80211_SEND_LEGACY_11B) {
3195 frm = ieee80211_add_htcap(frm, bss);
3196 frm = ieee80211_add_htinfo(frm, bss);
3198 if (IEEE80211_IS_CHAN_VHT(bss->ni_chan) &&
3199 legacy != IEEE80211_SEND_LEGACY_11B) {
3200 frm = ieee80211_add_vhtcap(frm, bss);
3201 frm = ieee80211_add_vhtinfo(frm, bss);
3203 frm = ieee80211_add_wpa(frm, vap);
3204 if (vap->iv_flags & IEEE80211_F_WME)
3205 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3206 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3207 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
3208 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
3209 legacy != IEEE80211_SEND_LEGACY_11B) {
3210 frm = ieee80211_add_htcap_vendor(frm, bss);
3211 frm = ieee80211_add_htinfo_vendor(frm, bss);
3213 #ifdef IEEE80211_SUPPORT_SUPERG
3214 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
3215 legacy != IEEE80211_SEND_LEGACY_11B)
3216 frm = ieee80211_add_athcaps(frm, bss);
3218 if (vap->iv_appie_proberesp != NULL)
3219 frm = add_appie(frm, vap->iv_appie_proberesp);
3220 #ifdef IEEE80211_SUPPORT_MESH
3221 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3222 frm = ieee80211_add_meshid(frm, vap);
3223 frm = ieee80211_add_meshconf(frm, vap);
3226 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3232 * Send a probe response frame to the specified mac address.
3233 * This does not go through the normal mgt frame api so we
3234 * can specify the destination address and re-use the bss node
3235 * for the sta reference.
3238 ieee80211_send_proberesp(struct ieee80211vap *vap,
3239 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
3241 struct ieee80211_node *bss = vap->iv_bss;
3242 struct ieee80211com *ic = vap->iv_ic;
3246 if (vap->iv_state == IEEE80211_S_CAC) {
3247 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
3248 "block %s frame in CAC state", "probe response");
3249 vap->iv_stats.is_tx_badstate++;
3250 return EIO; /* XXX */
3254 * Hold a reference on the node so it doesn't go away until after
3255 * the xmit is complete all the way in the driver. On error we
3256 * will remove our reference.
3258 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3259 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
3260 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
3261 ieee80211_node_refcnt(bss)+1);
3262 ieee80211_ref_node(bss);
3264 m = ieee80211_alloc_proberesp(bss, legacy);
3266 ieee80211_free_node(bss);
3270 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3271 KASSERT(m != NULL, ("no room for header"));
3273 IEEE80211_TX_LOCK(ic);
3274 ieee80211_send_setup(bss, m,
3275 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
3276 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
3277 /* XXX power management? */
3278 m->m_flags |= M_ENCAP; /* mark encapsulated */
3280 M_WME_SETAC(m, WME_AC_BE);
3282 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
3283 "send probe resp on channel %u to %s%s\n",
3284 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
3285 legacy ? " <legacy>" : "");
3286 IEEE80211_NODE_STAT(bss, tx_mgmt);
3288 ret = ieee80211_raw_output(vap, bss, m, NULL);
3289 IEEE80211_TX_UNLOCK(ic);
3294 * Allocate and build a RTS (Request To Send) control frame.
3297 ieee80211_alloc_rts(struct ieee80211com *ic,
3298 const uint8_t ra[IEEE80211_ADDR_LEN],
3299 const uint8_t ta[IEEE80211_ADDR_LEN],
3302 struct ieee80211_frame_rts *rts;
3305 /* XXX honor ic_headroom */
3306 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3308 rts = mtod(m, struct ieee80211_frame_rts *);
3309 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3310 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
3311 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3312 *(u_int16_t *)rts->i_dur = htole16(dur);
3313 IEEE80211_ADDR_COPY(rts->i_ra, ra);
3314 IEEE80211_ADDR_COPY(rts->i_ta, ta);
3316 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
3322 * Allocate and build a CTS (Clear To Send) control frame.
3325 ieee80211_alloc_cts(struct ieee80211com *ic,
3326 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
3328 struct ieee80211_frame_cts *cts;
3331 /* XXX honor ic_headroom */
3332 m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
3334 cts = mtod(m, struct ieee80211_frame_cts *);
3335 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
3336 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
3337 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3338 *(u_int16_t *)cts->i_dur = htole16(dur);
3339 IEEE80211_ADDR_COPY(cts->i_ra, ra);
3341 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
3347 * Wrapper for CTS/RTS frame allocation.
3350 ieee80211_alloc_prot(struct ieee80211_node *ni, const struct mbuf *m,
3351 uint8_t rate, int prot)
3353 struct ieee80211com *ic = ni->ni_ic;
3354 struct ieee80211vap *vap = ni->ni_vap;
3355 const struct ieee80211_frame *wh;
3358 int pktlen, isshort;
3360 KASSERT(prot == IEEE80211_PROT_RTSCTS ||
3361 prot == IEEE80211_PROT_CTSONLY,
3362 ("wrong protection type %d", prot));
3364 wh = mtod(m, const struct ieee80211_frame *);
3365 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3366 isshort = (vap->iv_flags & IEEE80211_F_SHPREAMBLE) != 0;
3367 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3368 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3370 if (prot == IEEE80211_PROT_RTSCTS) {
3371 /* NB: CTS is the same size as an ACK */
3372 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3373 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3375 mprot = ieee80211_alloc_cts(ic, vap->iv_myaddr, dur);
3381 ieee80211_tx_mgt_timeout(void *arg)
3383 struct ieee80211vap *vap = arg;
3385 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3386 "vap %p mode %s state %s flags %#x & %#x\n", vap,
3387 ieee80211_opmode_name[vap->iv_opmode],
3388 ieee80211_state_name[vap->iv_state],
3389 vap->iv_ic->ic_flags, IEEE80211_F_SCAN);
3391 IEEE80211_LOCK(vap->iv_ic);
3392 if (vap->iv_state != IEEE80211_S_INIT &&
3393 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3395 * NB: it's safe to specify a timeout as the reason here;
3396 * it'll only be used in the right state.
3398 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
3399 IEEE80211_SCAN_FAIL_TIMEOUT);
3401 IEEE80211_UNLOCK(vap->iv_ic);
3405 * This is the callback set on net80211-sourced transmitted
3406 * authentication request frames.
3408 * This does a couple of things:
3410 * + If the frame transmitted was a success, it schedules a future
3411 * event which will transition the interface to scan.
3412 * If a state transition _then_ occurs before that event occurs,
3413 * said state transition will cancel this callout.
3415 * + If the frame transmit was a failure, it immediately schedules
3416 * the transition back to scan.
3419 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3421 struct ieee80211vap *vap = ni->ni_vap;
3422 enum ieee80211_state ostate = (enum ieee80211_state)(uintptr_t)arg;
3425 * Frame transmit completed; arrange timer callback. If
3426 * transmit was successfully we wait for response. Otherwise
3427 * we arrange an immediate callback instead of doing the
3428 * callback directly since we don't know what state the driver
3429 * is in (e.g. what locks it is holding). This work should
3430 * not be too time-critical and not happen too often so the
3431 * added overhead is acceptable.
3433 * XXX what happens if !acked but response shows up before callback?
3435 if (vap->iv_state == ostate) {
3436 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
3437 "ni %p mode %s state %s arg %p status %d\n", ni,
3438 ieee80211_opmode_name[vap->iv_opmode],
3439 ieee80211_state_name[vap->iv_state], arg, status);
3441 callout_reset(&vap->iv_mgtsend,
3442 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3443 ieee80211_tx_mgt_timeout, vap);
3448 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3449 struct ieee80211_node *ni)
3451 struct ieee80211vap *vap = ni->ni_vap;
3452 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3453 struct ieee80211com *ic = ni->ni_ic;
3454 struct ieee80211_rateset *rs = &ni->ni_rates;
3458 * beacon frame format
3460 * TODO: update to 802.11-2012; a lot of stuff has changed;
3461 * vendor extensions should be at the end, etc.
3464 * [2] beacon interval
3465 * [2] cabability information
3467 * [tlv] supported rates
3468 * [3] parameter set (DS)
3469 * [8] CF parameter set (optional)
3470 * [tlv] parameter set (IBSS/TIM)
3471 * [tlv] country (optional)
3472 * [3] power control (optional)
3473 * [5] channel switch announcement (CSA) (optional)
3475 * XXX TODO: IBSS DFS
3476 * XXX TODO: TPC report
3477 * [tlv] extended rate phy (ERP)
3478 * [tlv] extended supported rates
3479 * [tlv] RSN parameters
3481 * (XXX EDCA parameter set, QoS capability?)
3482 * XXX TODO: AP channel report
3484 * [tlv] HT capabilities
3485 * [tlv] HT information
3486 * XXX TODO: 20/40 BSS coexistence
3489 * XXX TODO: mesh config
3490 * XXX TODO: mesh awake window
3491 * XXX TODO: beacon timing (mesh, etc)
3492 * XXX TODO: MCCAOP Advertisement Overview
3493 * XXX TODO: MCCAOP Advertisement
3494 * XXX TODO: Mesh channel switch parameters
3496 * XXX TODO: VHT capabilities
3497 * XXX TODO: VHT operation
3498 * XXX TODO: VHT transmit power envelope
3499 * XXX TODO: channel switch wrapper element
3500 * XXX TODO: extended BSS load element
3502 * XXX Vendor-specific OIDs (e.g. Atheros)
3503 * [tlv] WPA parameters
3504 * [tlv] WME parameters
3505 * [tlv] Vendor OUI HT capabilities (optional)
3506 * [tlv] Vendor OUI HT information (optional)
3507 * [tlv] Atheros capabilities (optional)
3508 * [tlv] TDMA parameters (optional)
3509 * [tlv] Mesh ID (MBSS)
3510 * [tlv] Mesh Conf (MBSS)
3511 * [tlv] application data (optional)
3514 memset(bo, 0, sizeof(*bo));
3516 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3518 *(uint16_t *)frm = htole16(ni->ni_intval);
3520 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3521 bo->bo_caps = (uint16_t *)frm;
3522 *(uint16_t *)frm = htole16(capinfo);
3524 *frm++ = IEEE80211_ELEMID_SSID;
3525 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3526 *frm++ = ni->ni_esslen;
3527 memcpy(frm, ni->ni_essid, ni->ni_esslen);
3528 frm += ni->ni_esslen;
3531 frm = ieee80211_add_rates(frm, rs);
3532 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3533 *frm++ = IEEE80211_ELEMID_DSPARMS;
3535 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3537 if (ic->ic_flags & IEEE80211_F_PCF) {
3539 frm = ieee80211_add_cfparms(frm, ic);
3542 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3543 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3545 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3547 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3548 vap->iv_opmode == IEEE80211_M_MBSS) {
3549 /* TIM IE is the same for Mesh and Hostap */
3550 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3552 tie->tim_ie = IEEE80211_ELEMID_TIM;
3553 tie->tim_len = 4; /* length */
3554 tie->tim_count = 0; /* DTIM count */
3555 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3556 tie->tim_bitctl = 0; /* bitmap control */
3557 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3558 frm += sizeof(struct ieee80211_tim_ie);
3561 bo->bo_tim_trailer = frm;
3562 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3563 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3564 frm = ieee80211_add_countryie(frm, ic);
3565 if (vap->iv_flags & IEEE80211_F_DOTH) {
3566 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3567 frm = ieee80211_add_powerconstraint(frm, vap);
3569 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3570 frm = ieee80211_add_csa(frm, vap);
3574 bo->bo_quiet = NULL;
3575 if (vap->iv_flags & IEEE80211_F_DOTH) {
3576 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3577 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
3578 (vap->iv_quiet == 1)) {
3580 * We only insert the quiet IE offset if
3581 * the quiet IE is enabled. Otherwise don't
3582 * put it here or we'll just overwrite
3583 * some other beacon contents.
3585 if (vap->iv_quiet) {
3587 frm = ieee80211_add_quiet(frm,vap, 0);
3592 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3594 frm = ieee80211_add_erp(frm, vap);
3596 frm = ieee80211_add_xrates(frm, rs);
3597 frm = ieee80211_add_rsn(frm, vap);
3598 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3599 frm = ieee80211_add_htcap(frm, ni);
3600 bo->bo_htinfo = frm;
3601 frm = ieee80211_add_htinfo(frm, ni);
3604 if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
3605 frm = ieee80211_add_vhtcap(frm, ni);
3606 bo->bo_vhtinfo = frm;
3607 frm = ieee80211_add_vhtinfo(frm, ni);
3608 /* Transmit power envelope */
3609 /* Channel switch wrapper element */
3610 /* Extended bss load element */
3613 frm = ieee80211_add_wpa(frm, vap);
3614 if (vap->iv_flags & IEEE80211_F_WME) {
3616 frm = ieee80211_add_wme_param(frm, &ic->ic_wme,
3617 !! (vap->iv_flags_ext & IEEE80211_FEXT_UAPSD));
3619 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3620 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3621 frm = ieee80211_add_htcap_vendor(frm, ni);
3622 frm = ieee80211_add_htinfo_vendor(frm, ni);
3625 #ifdef IEEE80211_SUPPORT_SUPERG
3626 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3628 frm = ieee80211_add_athcaps(frm, ni);
3631 #ifdef IEEE80211_SUPPORT_TDMA
3632 if (vap->iv_caps & IEEE80211_C_TDMA) {
3634 frm = ieee80211_add_tdma(frm, vap);
3637 if (vap->iv_appie_beacon != NULL) {
3639 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3640 frm = add_appie(frm, vap->iv_appie_beacon);
3643 /* XXX TODO: move meshid/meshconf up to before vendor extensions? */
3644 #ifdef IEEE80211_SUPPORT_MESH
3645 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3646 frm = ieee80211_add_meshid(frm, vap);
3647 bo->bo_meshconf = frm;
3648 frm = ieee80211_add_meshconf(frm, vap);
3651 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3652 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3653 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3657 * Allocate a beacon frame and fillin the appropriate bits.
3660 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3662 struct ieee80211vap *vap = ni->ni_vap;
3663 struct ieee80211com *ic = ni->ni_ic;
3664 struct ifnet *ifp = vap->iv_ifp;
3665 struct ieee80211_frame *wh;
3671 * Update the "We're putting the quiet IE in the beacon" state.
3673 if (vap->iv_quiet == 1)
3674 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3675 else if (vap->iv_quiet == 0)
3676 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3679 * beacon frame format
3681 * Note: This needs updating for 802.11-2012.
3684 * [2] beacon interval
3685 * [2] cabability information
3687 * [tlv] supported rates
3688 * [3] parameter set (DS)
3689 * [8] CF parameter set (optional)
3690 * [tlv] parameter set (IBSS/TIM)
3691 * [tlv] country (optional)
3692 * [3] power control (optional)
3693 * [5] channel switch announcement (CSA) (optional)
3694 * [tlv] extended rate phy (ERP)
3695 * [tlv] extended supported rates
3696 * [tlv] RSN parameters
3697 * [tlv] HT capabilities
3698 * [tlv] HT information
3699 * [tlv] VHT capabilities
3700 * [tlv] VHT operation
3701 * [tlv] Vendor OUI HT capabilities (optional)
3702 * [tlv] Vendor OUI HT information (optional)
3703 * XXX Vendor-specific OIDs (e.g. Atheros)
3704 * [tlv] WPA parameters
3705 * [tlv] WME parameters
3706 * [tlv] TDMA parameters (optional)
3707 * [tlv] Mesh ID (MBSS)
3708 * [tlv] Mesh Conf (MBSS)
3709 * [tlv] application data (optional)
3710 * NB: we allocate the max space required for the TIM bitmap.
3711 * XXX how big is this?
3713 pktlen = 8 /* time stamp */
3714 + sizeof(uint16_t) /* beacon interval */
3715 + sizeof(uint16_t) /* capabilities */
3716 + 2 + ni->ni_esslen /* ssid */
3717 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3718 + 2 + 1 /* DS parameters */
3719 + 2 + 6 /* CF parameters */
3720 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3721 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3722 + 2 + 1 /* power control */
3723 + sizeof(struct ieee80211_csa_ie) /* CSA */
3724 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3726 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3727 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3728 2*sizeof(struct ieee80211_ie_wpa) : 0)
3729 /* XXX conditional? */
3730 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3731 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3732 + sizeof(struct ieee80211_ie_vhtcap)/* VHT caps */
3733 + sizeof(struct ieee80211_ie_vht_operation)/* VHT info */
3734 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3735 sizeof(struct ieee80211_wme_param) : 0)
3736 #ifdef IEEE80211_SUPPORT_SUPERG
3737 + sizeof(struct ieee80211_ath_ie) /* ATH */
3739 #ifdef IEEE80211_SUPPORT_TDMA
3740 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3741 sizeof(struct ieee80211_tdma_param) : 0)
3743 #ifdef IEEE80211_SUPPORT_MESH
3744 + 2 + ni->ni_meshidlen
3745 + sizeof(struct ieee80211_meshconf_ie)
3747 + IEEE80211_MAX_APPIE
3749 m = ieee80211_getmgtframe(&frm,
3750 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3752 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3753 "%s: cannot get buf; size %u\n", __func__, pktlen);
3754 vap->iv_stats.is_tx_nobuf++;
3757 ieee80211_beacon_construct(m, frm, ni);
3759 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
3760 KASSERT(m != NULL, ("no space for 802.11 header?"));
3761 wh = mtod(m, struct ieee80211_frame *);
3762 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3763 IEEE80211_FC0_SUBTYPE_BEACON;
3764 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3765 *(uint16_t *)wh->i_dur = 0;
3766 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3767 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3768 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3769 *(uint16_t *)wh->i_seq = 0;
3775 * Update the dynamic parts of a beacon frame based on the current state.
3778 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3780 struct ieee80211vap *vap = ni->ni_vap;
3781 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3782 struct ieee80211com *ic = ni->ni_ic;
3783 int len_changed = 0;
3785 struct ieee80211_frame *wh;
3786 ieee80211_seq seqno;
3790 * Handle 11h channel change when we've reached the count.
3791 * We must recalculate the beacon frame contents to account
3792 * for the new channel. Note we do this only for the first
3793 * vap that reaches this point; subsequent vaps just update
3794 * their beacon state to reflect the recalculated channel.
3796 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3797 vap->iv_csa_count == ic->ic_csa_count) {
3798 vap->iv_csa_count = 0;
3800 * Effect channel change before reconstructing the beacon
3801 * frame contents as many places reference ni_chan.
3803 if (ic->ic_csa_newchan != NULL)
3804 ieee80211_csa_completeswitch(ic);
3806 * NB: ieee80211_beacon_construct clears all pending
3807 * updates in bo_flags so we don't need to explicitly
3808 * clear IEEE80211_BEACON_CSA.
3810 ieee80211_beacon_construct(m,
3811 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3813 /* XXX do WME aggressive mode processing? */
3814 IEEE80211_UNLOCK(ic);
3815 return 1; /* just assume length changed */
3819 * Handle the quiet time element being added and removed.
3820 * Again, for now we just cheat and reconstruct the whole
3821 * beacon - that way the gap is provided as appropriate.
3823 * So, track whether we have already added the IE versus
3824 * whether we want to be adding the IE.
3826 if ((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) &&
3827 (vap->iv_quiet == 0)) {
3829 * Quiet time beacon IE enabled, but it's disabled;
3832 vap->iv_flags_ext &= ~IEEE80211_FEXT_QUIET_IE;
3833 ieee80211_beacon_construct(m,
3834 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3835 /* XXX do WME aggressive mode processing? */
3836 IEEE80211_UNLOCK(ic);
3837 return 1; /* just assume length changed */
3840 if (((vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE) == 0) &&
3841 (vap->iv_quiet == 1)) {
3843 * Quiet time beacon IE disabled, but it's now enabled;
3846 vap->iv_flags_ext |= IEEE80211_FEXT_QUIET_IE;
3847 ieee80211_beacon_construct(m,
3848 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3849 /* XXX do WME aggressive mode processing? */
3850 IEEE80211_UNLOCK(ic);
3851 return 1; /* just assume length changed */
3854 wh = mtod(m, struct ieee80211_frame *);
3857 * XXX TODO Strictly speaking this should be incremented with the TX
3858 * lock held so as to serialise access to the non-qos TID sequence
3861 * If the driver identifies it does its own TX seqno management then
3862 * we can skip this (and still not do the TX seqno.)
3864 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3865 *(uint16_t *)&wh->i_seq[0] =
3866 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3867 M_SEQNO_SET(m, seqno);
3869 /* XXX faster to recalculate entirely or just changes? */
3870 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3871 *bo->bo_caps = htole16(capinfo);
3873 if (vap->iv_flags & IEEE80211_F_WME) {
3874 struct ieee80211_wme_state *wme = &ic->ic_wme;
3877 * Check for aggressive mode change. When there is
3878 * significant high priority traffic in the BSS
3879 * throttle back BE traffic by using conservative
3880 * parameters. Otherwise BE uses aggressive params
3881 * to optimize performance of legacy/non-QoS traffic.
3883 if (wme->wme_flags & WME_F_AGGRMODE) {
3884 if (wme->wme_hipri_traffic >
3885 wme->wme_hipri_switch_thresh) {
3886 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3887 "%s: traffic %u, disable aggressive mode\n",
3888 __func__, wme->wme_hipri_traffic);
3889 wme->wme_flags &= ~WME_F_AGGRMODE;
3890 ieee80211_wme_updateparams_locked(vap);
3891 wme->wme_hipri_traffic =
3892 wme->wme_hipri_switch_hysteresis;
3894 wme->wme_hipri_traffic = 0;
3896 if (wme->wme_hipri_traffic <=
3897 wme->wme_hipri_switch_thresh) {
3898 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3899 "%s: traffic %u, enable aggressive mode\n",
3900 __func__, wme->wme_hipri_traffic);
3901 wme->wme_flags |= WME_F_AGGRMODE;
3902 ieee80211_wme_updateparams_locked(vap);
3903 wme->wme_hipri_traffic = 0;
3905 wme->wme_hipri_traffic =
3906 wme->wme_hipri_switch_hysteresis;
3908 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3909 (void) ieee80211_add_wme_param(bo->bo_wme, wme,
3910 vap->iv_flags_ext & IEEE80211_FEXT_UAPSD);
3911 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3915 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3916 ieee80211_ht_update_beacon(vap, bo);
3917 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3919 #ifdef IEEE80211_SUPPORT_TDMA
3920 if (vap->iv_caps & IEEE80211_C_TDMA) {
3922 * NB: the beacon is potentially updated every TBTT.
3924 ieee80211_tdma_update_beacon(vap, bo);
3927 #ifdef IEEE80211_SUPPORT_MESH
3928 if (vap->iv_opmode == IEEE80211_M_MBSS)
3929 ieee80211_mesh_update_beacon(vap, bo);
3932 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3933 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3934 struct ieee80211_tim_ie *tie =
3935 (struct ieee80211_tim_ie *) bo->bo_tim;
3936 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3937 u_int timlen, timoff, i;
3939 * ATIM/DTIM needs updating. If it fits in the
3940 * current space allocated then just copy in the
3941 * new bits. Otherwise we need to move any trailing
3942 * data to make room. Note that we know there is
3943 * contiguous space because ieee80211_beacon_allocate
3944 * insures there is space in the mbuf to write a
3945 * maximal-size virtual bitmap (based on iv_max_aid).
3948 * Calculate the bitmap size and offset, copy any
3949 * trailer out of the way, and then copy in the
3950 * new bitmap and update the information element.
3951 * Note that the tim bitmap must contain at least
3952 * one byte and any offset must be even.
3954 if (vap->iv_ps_pending != 0) {
3955 timoff = 128; /* impossibly large */
3956 for (i = 0; i < vap->iv_tim_len; i++)
3957 if (vap->iv_tim_bitmap[i]) {
3961 KASSERT(timoff != 128, ("tim bitmap empty!"));
3962 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3963 if (vap->iv_tim_bitmap[i])
3965 timlen = 1 + (i - timoff);
3972 * TODO: validate this!
3974 if (timlen != bo->bo_tim_len) {
3975 /* copy up/down trailer */
3976 int adjust = tie->tim_bitmap+timlen
3977 - bo->bo_tim_trailer;
3978 ovbcopy(bo->bo_tim_trailer,
3979 bo->bo_tim_trailer+adjust,
3980 bo->bo_tim_trailer_len);
3981 bo->bo_tim_trailer += adjust;
3982 bo->bo_erp += adjust;
3983 bo->bo_htinfo += adjust;
3984 bo->bo_vhtinfo += adjust;
3985 #ifdef IEEE80211_SUPPORT_SUPERG
3986 bo->bo_ath += adjust;
3988 #ifdef IEEE80211_SUPPORT_TDMA
3989 bo->bo_tdma += adjust;
3991 #ifdef IEEE80211_SUPPORT_MESH
3992 bo->bo_meshconf += adjust;
3994 bo->bo_appie += adjust;
3995 bo->bo_wme += adjust;
3996 bo->bo_csa += adjust;
3997 bo->bo_quiet += adjust;
3998 bo->bo_tim_len = timlen;
4000 /* update information element */
4001 tie->tim_len = 3 + timlen;
4002 tie->tim_bitctl = timoff;
4005 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
4008 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
4010 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
4011 "%s: TIM updated, pending %u, off %u, len %u\n",
4012 __func__, vap->iv_ps_pending, timoff, timlen);
4014 /* count down DTIM period */
4015 if (tie->tim_count == 0)
4016 tie->tim_count = tie->tim_period - 1;
4019 /* update state for buffered multicast frames on DTIM */
4020 if (mcast && tie->tim_count == 0)
4021 tie->tim_bitctl |= 1;
4023 tie->tim_bitctl &= ~1;
4024 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
4025 struct ieee80211_csa_ie *csa =
4026 (struct ieee80211_csa_ie *) bo->bo_csa;
4029 * Insert or update CSA ie. If we're just starting
4030 * to count down to the channel switch then we need
4031 * to insert the CSA ie. Otherwise we just need to
4032 * drop the count. The actual change happens above
4033 * when the vap's count reaches the target count.
4035 if (vap->iv_csa_count == 0) {
4036 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
4037 bo->bo_erp += sizeof(*csa);
4038 bo->bo_htinfo += sizeof(*csa);
4039 bo->bo_vhtinfo += sizeof(*csa);
4040 bo->bo_wme += sizeof(*csa);
4041 #ifdef IEEE80211_SUPPORT_SUPERG
4042 bo->bo_ath += sizeof(*csa);
4044 #ifdef IEEE80211_SUPPORT_TDMA
4045 bo->bo_tdma += sizeof(*csa);
4047 #ifdef IEEE80211_SUPPORT_MESH
4048 bo->bo_meshconf += sizeof(*csa);
4050 bo->bo_appie += sizeof(*csa);
4051 bo->bo_csa_trailer_len += sizeof(*csa);
4052 bo->bo_quiet += sizeof(*csa);
4053 bo->bo_tim_trailer_len += sizeof(*csa);
4054 m->m_len += sizeof(*csa);
4055 m->m_pkthdr.len += sizeof(*csa);
4057 ieee80211_add_csa(bo->bo_csa, vap);
4060 vap->iv_csa_count++;
4061 /* NB: don't clear IEEE80211_BEACON_CSA */
4065 * Only add the quiet time IE if we've enabled it
4068 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
4069 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
4070 if (vap->iv_quiet &&
4071 (vap->iv_flags_ext & IEEE80211_FEXT_QUIET_IE)) {
4072 ieee80211_add_quiet(bo->bo_quiet, vap, 1);
4075 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
4077 * ERP element needs updating.
4079 (void) ieee80211_add_erp(bo->bo_erp, vap);
4080 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
4082 #ifdef IEEE80211_SUPPORT_SUPERG
4083 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
4084 ieee80211_add_athcaps(bo->bo_ath, ni);
4085 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
4089 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
4090 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
4096 aielen += aie->ie_len;
4097 if (aielen != bo->bo_appie_len) {
4098 /* copy up/down trailer */
4099 int adjust = aielen - bo->bo_appie_len;
4100 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
4101 bo->bo_tim_trailer_len);
4102 bo->bo_tim_trailer += adjust;
4103 bo->bo_appie += adjust;
4104 bo->bo_appie_len = aielen;
4110 frm = add_appie(frm, aie);
4111 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
4113 IEEE80211_UNLOCK(ic);
4119 * Do Ethernet-LLC encapsulation for each payload in a fast frame
4120 * tunnel encapsulation. The frame is assumed to have an Ethernet
4121 * header at the front that must be stripped before prepending the
4122 * LLC followed by the Ethernet header passed in (with an Ethernet
4123 * type that specifies the payload size).
4126 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
4127 const struct ether_header *eh)
4132 /* XXX optimize by combining m_adj+M_PREPEND */
4133 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
4134 llc = mtod(m, struct llc *);
4135 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
4136 llc->llc_control = LLC_UI;
4137 llc->llc_snap.org_code[0] = 0;
4138 llc->llc_snap.org_code[1] = 0;
4139 llc->llc_snap.org_code[2] = 0;
4140 llc->llc_snap.ether_type = eh->ether_type;
4141 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
4143 M_PREPEND(m, sizeof(struct ether_header), IEEE80211_M_NOWAIT);
4144 if (m == NULL) { /* XXX cannot happen */
4145 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
4146 "%s: no space for ether_header\n", __func__);
4147 vap->iv_stats.is_tx_nobuf++;
4150 ETHER_HEADER_COPY(mtod(m, void *), eh);
4151 mtod(m, struct ether_header *)->ether_type = htons(payload);
4156 * Complete an mbuf transmission.
4158 * For now, this simply processes a completed frame after the
4159 * driver has completed it's transmission and/or retransmission.
4160 * It assumes the frame is an 802.11 encapsulated frame.
4162 * Later on it will grow to become the exit path for a given frame
4163 * from the driver and, depending upon how it's been encapsulated
4164 * and already transmitted, it may end up doing A-MPDU retransmission,
4165 * power save requeuing, etc.
4167 * In order for the above to work, the driver entry point to this
4168 * must not hold any driver locks. Thus, the driver needs to delay
4169 * any actual mbuf completion until it can release said locks.
4171 * This frees the mbuf and if the mbuf has a node reference,
4172 * the node reference will be freed.
4175 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
4179 struct ifnet *ifp = ni->ni_vap->iv_ifp;
4182 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
4183 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
4184 if (m->m_flags & M_MCAST)
4185 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
4187 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4188 if (m->m_flags & M_TXCB) {
4189 IEEE80211_DPRINTF(ni->ni_vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
4190 "ni %p vap %p mode %s state %s m %p status %d\n", ni, ni->ni_vap,
4191 ieee80211_opmode_name[ni->ni_vap->iv_opmode],
4192 ieee80211_state_name[ni->ni_vap->iv_state], m, status);
4193 ieee80211_process_callback(ni, m, status);
4195 ieee80211_free_node(ni);