2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/endian.h>
41 #include <sys/socket.h>
44 #include <net/ethernet.h>
46 #include <net/if_var.h>
47 #include <net/if_llc.h>
48 #include <net/if_media.h>
49 #include <net/if_vlan_var.h>
51 #include <net80211/ieee80211_var.h>
52 #include <net80211/ieee80211_regdomain.h>
53 #ifdef IEEE80211_SUPPORT_SUPERG
54 #include <net80211/ieee80211_superg.h>
56 #ifdef IEEE80211_SUPPORT_TDMA
57 #include <net80211/ieee80211_tdma.h>
59 #include <net80211/ieee80211_wds.h>
60 #include <net80211/ieee80211_mesh.h>
62 #if defined(INET) || defined(INET6)
63 #include <netinet/in.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
72 #include <netinet/ip6.h>
75 #include <security/mac/mac_framework.h>
77 #define ETHER_HEADER_COPY(dst, src) \
78 memcpy(dst, src, sizeof(struct ether_header))
80 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
81 u_int hdrsize, u_int ciphdrsize, u_int mtu);
82 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
84 #ifdef IEEE80211_DEBUG
86 * Decide if an outbound management frame should be
87 * printed when debugging is enabled. This filters some
88 * of the less interesting frames that come frequently
92 doprint(struct ieee80211vap *vap, int subtype)
95 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
96 return (vap->iv_opmode == IEEE80211_M_IBSS);
103 * Transmit a frame to the given destination on the given VAP.
105 * It's up to the caller to figure out the details of who this
106 * is going to and resolving the node.
108 * This routine takes care of queuing it for power save,
109 * A-MPDU state stuff, fast-frames state stuff, encapsulation
110 * if required, then passing it up to the driver layer.
112 * This routine (for now) consumes the mbuf and frees the node
113 * reference; it ideally will return a TX status which reflects
114 * whether the mbuf was consumed or not, so the caller can
115 * free the mbuf (if appropriate) and the node reference (again,
119 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
120 struct ieee80211_node *ni)
122 struct ieee80211com *ic = vap->iv_ic;
123 struct ifnet *ifp = vap->iv_ifp;
124 #ifdef IEEE80211_SUPPORT_SUPERG
128 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
129 (m->m_flags & M_PWR_SAV) == 0) {
131 * Station in power save mode; pass the frame
132 * to the 802.11 layer and continue. We'll get
133 * the frame back when the time is right.
134 * XXX lose WDS vap linkage?
136 if (ieee80211_pwrsave(ni, m) != 0)
137 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
138 ieee80211_free_node(ni);
141 * We queued it fine, so tell the upper layer
142 * that we consumed it.
146 /* calculate priority so drivers can find the tx queue */
147 if (ieee80211_classify(ni, m)) {
148 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
149 ni->ni_macaddr, NULL,
150 "%s", "classification failure");
151 vap->iv_stats.is_tx_classify++;
152 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
154 ieee80211_free_node(ni);
156 /* XXX better status? */
160 * Stash the node pointer. Note that we do this after
161 * any call to ieee80211_dwds_mcast because that code
162 * uses any existing value for rcvif to identify the
163 * interface it (might have been) received on.
165 m->m_pkthdr.rcvif = (void *)ni;
166 #ifdef IEEE80211_SUPPORT_SUPERG
167 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
170 BPF_MTAP(ifp, m); /* 802.3 tx */
173 * Check if A-MPDU tx aggregation is setup or if we
174 * should try to enable it. The sta must be associated
175 * with HT and A-MPDU enabled for use. When the policy
176 * routine decides we should enable A-MPDU we issue an
177 * ADDBA request and wait for a reply. The frame being
178 * encapsulated will go out w/o using A-MPDU, or possibly
179 * it might be collected by the driver and held/retransmit.
180 * The default ic_ampdu_enable routine handles staggering
181 * ADDBA requests in case the receiver NAK's us or we are
182 * otherwise unable to establish a BA stream.
184 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
185 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
186 if ((m->m_flags & M_EAPOL) == 0) {
187 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
188 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
190 ieee80211_txampdu_count_packet(tap);
191 if (IEEE80211_AMPDU_RUNNING(tap)) {
193 * Operational, mark frame for aggregation.
195 * XXX do tx aggregation here
197 m->m_flags |= M_AMPDU_MPDU;
198 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
199 ic->ic_ampdu_enable(ni, tap)) {
201 * Not negotiated yet, request service.
203 ieee80211_ampdu_request(ni, tap);
204 /* XXX hold frame for reply? */
209 #ifdef IEEE80211_SUPPORT_SUPERG
211 * Check for AMSDU/FF; queue for aggregation
213 * Note: we don't bother trying to do fast frames or
214 * A-MSDU encapsulation for 802.3 drivers. Now, we
215 * likely could do it for FF (because it's a magic
216 * atheros tunnel LLC type) but I don't think we're going
217 * to really need to. For A-MSDU we'd have to set the
218 * A-MSDU QoS bit in the wifi header, so we just plain
221 * Strictly speaking, we could actually /do/ A-MSDU / FF
222 * with A-MPDU together which for certain circumstances
223 * is beneficial (eg A-MSDU of TCK ACKs.) However,
224 * I'll ignore that for now so existing behaviour is maintained.
225 * Later on it would be good to make "amsdu + ampdu" configurable.
227 else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
228 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
229 m = ieee80211_amsdu_check(ni, m);
231 /* NB: any ni ref held on stageq */
232 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
233 "%s: amsdu_check queued frame\n",
237 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
238 IEEE80211_NODE_FF)) {
239 m = ieee80211_ff_check(ni, m);
241 /* NB: any ni ref held on stageq */
242 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
243 "%s: ff_check queued frame\n",
249 #endif /* IEEE80211_SUPPORT_SUPERG */
252 * Grab the TX lock - serialise the TX process from this
253 * point (where TX state is being checked/modified)
254 * through to driver queue.
256 IEEE80211_TX_LOCK(ic);
259 * XXX make the encap and transmit code a separate function
260 * so things like the FF (and later A-MSDU) path can just call
261 * it for flushed frames.
263 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
265 * Encapsulate the packet in prep for transmission.
267 m = ieee80211_encap(vap, ni, m);
269 /* NB: stat+msg handled in ieee80211_encap */
270 IEEE80211_TX_UNLOCK(ic);
271 ieee80211_free_node(ni);
272 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
276 (void) ieee80211_parent_xmitpkt(ic, m);
279 * Unlock at this point - no need to hold it across
280 * ieee80211_free_node() (ie, the comlock)
282 IEEE80211_TX_UNLOCK(ic);
283 ic->ic_lastdata = ticks;
291 * Send the given mbuf through the given vap.
293 * This consumes the mbuf regardless of whether the transmit
294 * was successful or not.
296 * This does none of the initial checks that ieee80211_start()
297 * does (eg CAC timeout, interface wakeup) - the caller must
301 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
303 #define IS_DWDS(vap) \
304 (vap->iv_opmode == IEEE80211_M_WDS && \
305 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
306 struct ieee80211com *ic = vap->iv_ic;
307 struct ifnet *ifp = vap->iv_ifp;
308 struct ieee80211_node *ni;
309 struct ether_header *eh;
312 * Cancel any background scan.
314 if (ic->ic_flags & IEEE80211_F_SCAN)
315 ieee80211_cancel_anyscan(vap);
317 * Find the node for the destination so we can do
318 * things like power save and fast frames aggregation.
320 * NB: past this point various code assumes the first
321 * mbuf has the 802.3 header present (and contiguous).
324 if (m->m_len < sizeof(struct ether_header) &&
325 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
326 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
327 "discard frame, %s\n", "m_pullup failed");
328 vap->iv_stats.is_tx_nobuf++; /* XXX */
329 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
332 eh = mtod(m, struct ether_header *);
333 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
336 * Only unicast frames from the above go out
337 * DWDS vaps; multicast frames are handled by
338 * dispatching the frame as it comes through
339 * the AP vap (see below).
341 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
342 eh->ether_dhost, "mcast", "%s", "on DWDS");
343 vap->iv_stats.is_dwds_mcast++;
345 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
346 /* XXX better status? */
349 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
351 * Spam DWDS vap's w/ multicast traffic.
353 /* XXX only if dwds in use? */
354 ieee80211_dwds_mcast(vap, m);
357 #ifdef IEEE80211_SUPPORT_MESH
358 if (vap->iv_opmode != IEEE80211_M_MBSS) {
360 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
362 /* NB: ieee80211_find_txnode does stat+msg */
363 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
365 /* XXX better status? */
368 if (ni->ni_associd == 0 &&
369 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
370 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
371 eh->ether_dhost, NULL,
372 "sta not associated (type 0x%04x)",
373 htons(eh->ether_type));
374 vap->iv_stats.is_tx_notassoc++;
375 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
377 ieee80211_free_node(ni);
378 /* XXX better status? */
381 #ifdef IEEE80211_SUPPORT_MESH
383 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
385 * Proxy station only if configured.
387 if (!ieee80211_mesh_isproxyena(vap)) {
388 IEEE80211_DISCARD_MAC(vap,
389 IEEE80211_MSG_OUTPUT |
391 eh->ether_dhost, NULL,
392 "%s", "proxy not enabled");
393 vap->iv_stats.is_mesh_notproxy++;
394 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
396 /* XXX better status? */
399 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
400 "forward frame from DS SA(%6D), DA(%6D)\n",
401 eh->ether_shost, ":",
402 eh->ether_dhost, ":");
403 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
405 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
408 * NB: ieee80211_mesh_discover holds/disposes
409 * frame (e.g. queueing on path discovery).
411 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
412 /* XXX better status? */
419 * We've resolved the sender, so attempt to transmit it.
422 if (vap->iv_state == IEEE80211_S_SLEEP) {
424 * In power save; queue frame and then wakeup device
427 ic->ic_lastdata = ticks;
428 if (ieee80211_pwrsave(ni, m) != 0)
429 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
430 ieee80211_free_node(ni);
431 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
435 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
442 * Start method for vap's. All packets from the stack come
443 * through here. We handle common processing of the packets
444 * before dispatching them to the underlying device.
446 * if_transmit() requires that the mbuf be consumed by this call
447 * regardless of the return condition.
450 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
452 struct ieee80211vap *vap = ifp->if_softc;
453 struct ieee80211com *ic = vap->iv_ic;
456 * No data frames go out unless we're running.
457 * Note in particular this covers CAC and CSA
458 * states (though maybe we should check muting
461 if (vap->iv_state != IEEE80211_S_RUN &&
462 vap->iv_state != IEEE80211_S_SLEEP) {
464 /* re-check under the com lock to avoid races */
465 if (vap->iv_state != IEEE80211_S_RUN &&
466 vap->iv_state != IEEE80211_S_SLEEP) {
467 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
468 "%s: ignore queue, in %s state\n",
469 __func__, ieee80211_state_name[vap->iv_state]);
470 vap->iv_stats.is_tx_badstate++;
471 IEEE80211_UNLOCK(ic);
472 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
474 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
477 IEEE80211_UNLOCK(ic);
481 * Sanitize mbuf flags for net80211 use. We cannot
482 * clear M_PWR_SAV or M_MORE_DATA because these may
483 * be set for frames that are re-submitted from the
486 * NB: This must be done before ieee80211_classify as
487 * it marks EAPOL in frames with M_EAPOL.
489 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
492 * Bump to the packet transmission path.
493 * The mbuf will be consumed here.
495 return (ieee80211_start_pkt(vap, m));
499 ieee80211_vap_qflush(struct ifnet *ifp)
506 * 802.11 raw output routine.
508 * XXX TODO: this (and other send routines) should correctly
509 * XXX keep the pwr mgmt bit set if it decides to call into the
510 * XXX driver to send a frame whilst the state is SLEEP.
512 * Otherwise the peer may decide that we're awake and flood us
513 * with traffic we are still too asleep to receive!
516 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
517 struct mbuf *m, const struct ieee80211_bpf_params *params)
519 struct ieee80211com *ic = vap->iv_ic;
523 * Set node - the caller has taken a reference, so ensure
524 * that the mbuf has the same node value that
525 * it would if it were going via the normal path.
527 m->m_pkthdr.rcvif = (void *)ni;
530 * Attempt to add bpf transmit parameters.
532 * For now it's ok to fail; the raw_xmit api still takes
535 * Later on when ic_raw_xmit() has params removed,
536 * they'll have to be added - so fail the transmit if
540 (void) ieee80211_add_xmit_params(m, params);
542 error = ic->ic_raw_xmit(ni, m, params);
544 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
545 ieee80211_free_node(ni);
551 * 802.11 output routine. This is (currently) used only to
552 * connect bpf write calls to the 802.11 layer for injecting
556 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
557 const struct sockaddr *dst, struct route *ro)
559 #define senderr(e) do { error = (e); goto bad;} while (0)
560 struct ieee80211_node *ni = NULL;
561 struct ieee80211vap *vap;
562 struct ieee80211_frame *wh;
563 struct ieee80211com *ic = NULL;
567 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
569 * Short-circuit requests if the vap is marked OACTIVE
570 * as this can happen because a packet came down through
571 * ieee80211_start before the vap entered RUN state in
572 * which case it's ok to just drop the frame. This
573 * should not be necessary but callers of if_output don't
581 * Hand to the 802.3 code if not tagged as
582 * a raw 802.11 frame.
584 if (dst->sa_family != AF_IEEE80211)
585 return vap->iv_output(ifp, m, dst, ro);
587 error = mac_ifnet_check_transmit(ifp, m);
591 if (ifp->if_flags & IFF_MONITOR)
593 if (!IFNET_IS_UP_RUNNING(ifp))
595 if (vap->iv_state == IEEE80211_S_CAC) {
596 IEEE80211_DPRINTF(vap,
597 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
598 "block %s frame in CAC state\n", "raw data");
599 vap->iv_stats.is_tx_badstate++;
600 senderr(EIO); /* XXX */
601 } else if (vap->iv_state == IEEE80211_S_SCAN)
603 /* XXX bypass bridge, pfil, carp, etc. */
605 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
606 senderr(EIO); /* XXX */
607 wh = mtod(m, struct ieee80211_frame *);
608 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
609 IEEE80211_FC0_VERSION_0)
610 senderr(EIO); /* XXX */
611 if (m->m_pkthdr.len < ieee80211_anyhdrsize(wh))
612 senderr(EIO); /* XXX */
614 /* locate destination node */
615 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
616 case IEEE80211_FC1_DIR_NODS:
617 case IEEE80211_FC1_DIR_FROMDS:
618 ni = ieee80211_find_txnode(vap, wh->i_addr1);
620 case IEEE80211_FC1_DIR_TODS:
621 case IEEE80211_FC1_DIR_DSTODS:
622 ni = ieee80211_find_txnode(vap, wh->i_addr3);
625 senderr(EIO); /* XXX */
629 * Permit packets w/ bpf params through regardless
630 * (see below about sa_len).
632 if (dst->sa_len == 0)
633 senderr(EHOSTUNREACH);
634 ni = ieee80211_ref_node(vap->iv_bss);
638 * Sanitize mbuf for net80211 flags leaked from above.
640 * NB: This must be done before ieee80211_classify as
641 * it marks EAPOL in frames with M_EAPOL.
643 m->m_flags &= ~M_80211_TX;
645 /* calculate priority so drivers can find the tx queue */
646 /* XXX assumes an 802.3 frame */
647 if (ieee80211_classify(ni, m))
648 senderr(EIO); /* XXX */
650 IEEE80211_NODE_STAT(ni, tx_data);
651 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
652 IEEE80211_NODE_STAT(ni, tx_mcast);
653 m->m_flags |= M_MCAST;
655 IEEE80211_NODE_STAT(ni, tx_ucast);
656 /* NB: ieee80211_encap does not include 802.11 header */
657 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
659 IEEE80211_TX_LOCK(ic);
662 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
663 * present by setting the sa_len field of the sockaddr (yes,
665 * NB: we assume sa_data is suitably aligned to cast.
667 ret = ieee80211_raw_output(vap, ni, m,
668 (const struct ieee80211_bpf_params *)(dst->sa_len ?
669 dst->sa_data : NULL));
670 IEEE80211_TX_UNLOCK(ic);
676 ieee80211_free_node(ni);
677 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
683 * Set the direction field and address fields of an outgoing
684 * frame. Note this should be called early on in constructing
685 * a frame as it sets i_fc[1]; other bits can then be or'd in.
688 ieee80211_send_setup(
689 struct ieee80211_node *ni,
692 const uint8_t sa[IEEE80211_ADDR_LEN],
693 const uint8_t da[IEEE80211_ADDR_LEN],
694 const uint8_t bssid[IEEE80211_ADDR_LEN])
696 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
697 struct ieee80211vap *vap = ni->ni_vap;
698 struct ieee80211_tx_ampdu *tap;
699 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
702 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
704 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
705 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
706 switch (vap->iv_opmode) {
707 case IEEE80211_M_STA:
708 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
709 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
710 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
711 IEEE80211_ADDR_COPY(wh->i_addr3, da);
713 case IEEE80211_M_IBSS:
714 case IEEE80211_M_AHDEMO:
715 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
716 IEEE80211_ADDR_COPY(wh->i_addr1, da);
717 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
718 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
720 case IEEE80211_M_HOSTAP:
721 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
722 IEEE80211_ADDR_COPY(wh->i_addr1, da);
723 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
724 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
726 case IEEE80211_M_WDS:
727 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
728 IEEE80211_ADDR_COPY(wh->i_addr1, da);
729 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
730 IEEE80211_ADDR_COPY(wh->i_addr3, da);
731 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
733 case IEEE80211_M_MBSS:
734 #ifdef IEEE80211_SUPPORT_MESH
735 if (IEEE80211_IS_MULTICAST(da)) {
736 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
738 IEEE80211_ADDR_COPY(wh->i_addr1, da);
739 IEEE80211_ADDR_COPY(wh->i_addr2,
742 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
743 IEEE80211_ADDR_COPY(wh->i_addr1, da);
744 IEEE80211_ADDR_COPY(wh->i_addr2,
746 IEEE80211_ADDR_COPY(wh->i_addr3, da);
747 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
751 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
755 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
756 IEEE80211_ADDR_COPY(wh->i_addr1, da);
757 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
758 #ifdef IEEE80211_SUPPORT_MESH
759 if (vap->iv_opmode == IEEE80211_M_MBSS)
760 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
763 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
765 *(uint16_t *)&wh->i_dur[0] = 0;
767 tap = &ni->ni_tx_ampdu[tid];
768 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
769 m->m_flags |= M_AMPDU_MPDU;
771 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
772 type & IEEE80211_FC0_SUBTYPE_MASK))
773 seqno = ni->ni_txseqs[tid]++;
777 *(uint16_t *)&wh->i_seq[0] =
778 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
779 M_SEQNO_SET(m, seqno);
782 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
783 m->m_flags |= M_MCAST;
788 * Send a management frame to the specified node. The node pointer
789 * must have a reference as the pointer will be passed to the driver
790 * and potentially held for a long time. If the frame is successfully
791 * dispatched to the driver, then it is responsible for freeing the
792 * reference (and potentially free'ing up any associated storage);
793 * otherwise deal with reclaiming any reference (on error).
796 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
797 struct ieee80211_bpf_params *params)
799 struct ieee80211vap *vap = ni->ni_vap;
800 struct ieee80211com *ic = ni->ni_ic;
801 struct ieee80211_frame *wh;
804 KASSERT(ni != NULL, ("null node"));
806 if (vap->iv_state == IEEE80211_S_CAC) {
807 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
808 ni, "block %s frame in CAC state",
809 ieee80211_mgt_subtype_name(type));
810 vap->iv_stats.is_tx_badstate++;
811 ieee80211_free_node(ni);
813 return EIO; /* XXX */
816 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
818 ieee80211_free_node(ni);
822 IEEE80211_TX_LOCK(ic);
824 wh = mtod(m, struct ieee80211_frame *);
825 ieee80211_send_setup(ni, m,
826 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
827 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
828 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
829 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
830 "encrypting frame (%s)", __func__);
831 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
833 m->m_flags |= M_ENCAP; /* mark encapsulated */
835 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
836 M_WME_SETAC(m, params->ibp_pri);
838 #ifdef IEEE80211_DEBUG
839 /* avoid printing too many frames */
840 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
841 ieee80211_msg_dumppkts(vap)) {
842 printf("[%s] send %s on channel %u\n",
843 ether_sprintf(wh->i_addr1),
844 ieee80211_mgt_subtype_name(type),
845 ieee80211_chan2ieee(ic, ic->ic_curchan));
848 IEEE80211_NODE_STAT(ni, tx_mgmt);
850 ret = ieee80211_raw_output(vap, ni, m, params);
851 IEEE80211_TX_UNLOCK(ic);
856 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
859 struct ieee80211vap *vap = ni->ni_vap;
865 * Send a null data frame to the specified node. If the station
866 * is setup for QoS then a QoS Null Data frame is constructed.
867 * If this is a WDS station then a 4-address frame is constructed.
869 * NB: the caller is assumed to have setup a node reference
870 * for use; this is necessary to deal with a race condition
871 * when probing for inactive stations. Like ieee80211_mgmt_output
872 * we must cleanup any node reference on error; however we
873 * can safely just unref it as we know it will never be the
874 * last reference to the node.
877 ieee80211_send_nulldata(struct ieee80211_node *ni)
879 struct ieee80211vap *vap = ni->ni_vap;
880 struct ieee80211com *ic = ni->ni_ic;
882 struct ieee80211_frame *wh;
887 if (vap->iv_state == IEEE80211_S_CAC) {
888 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
889 ni, "block %s frame in CAC state", "null data");
890 ieee80211_unref_node(&ni);
891 vap->iv_stats.is_tx_badstate++;
892 return EIO; /* XXX */
895 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
896 hdrlen = sizeof(struct ieee80211_qosframe);
898 hdrlen = sizeof(struct ieee80211_frame);
899 /* NB: only WDS vap's get 4-address frames */
900 if (vap->iv_opmode == IEEE80211_M_WDS)
901 hdrlen += IEEE80211_ADDR_LEN;
902 if (ic->ic_flags & IEEE80211_F_DATAPAD)
903 hdrlen = roundup(hdrlen, sizeof(uint32_t));
905 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
908 ieee80211_unref_node(&ni);
909 vap->iv_stats.is_tx_nobuf++;
912 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
913 ("leading space %zd", M_LEADINGSPACE(m)));
914 M_PREPEND(m, hdrlen, M_NOWAIT);
916 /* NB: cannot happen */
917 ieee80211_free_node(ni);
921 IEEE80211_TX_LOCK(ic);
923 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
924 if (ni->ni_flags & IEEE80211_NODE_QOS) {
925 const int tid = WME_AC_TO_TID(WME_AC_BE);
928 ieee80211_send_setup(ni, m,
929 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
930 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
932 if (vap->iv_opmode == IEEE80211_M_WDS)
933 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
935 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
936 qos[0] = tid & IEEE80211_QOS_TID;
937 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
938 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
941 ieee80211_send_setup(ni, m,
942 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
943 IEEE80211_NONQOS_TID,
944 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
946 if (vap->iv_opmode != IEEE80211_M_WDS) {
947 /* NB: power management bit is never sent by an AP */
948 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
949 vap->iv_opmode != IEEE80211_M_HOSTAP)
950 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
952 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
953 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
954 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
957 m->m_len = m->m_pkthdr.len = hdrlen;
958 m->m_flags |= M_ENCAP; /* mark encapsulated */
960 M_WME_SETAC(m, WME_AC_BE);
962 IEEE80211_NODE_STAT(ni, tx_data);
964 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
965 "send %snull data frame on channel %u, pwr mgt %s",
966 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
967 ieee80211_chan2ieee(ic, ic->ic_curchan),
968 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
970 ret = ieee80211_raw_output(vap, ni, m, NULL);
971 IEEE80211_TX_UNLOCK(ic);
976 * Assign priority to a frame based on any vlan tag assigned
977 * to the station and/or any Diffserv setting in an IP header.
978 * Finally, if an ACM policy is setup (in station mode) it's
982 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
984 const struct ether_header *eh = mtod(m, struct ether_header *);
985 int v_wme_ac, d_wme_ac, ac;
988 * Always promote PAE/EAPOL frames to high priority.
990 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
991 /* NB: mark so others don't need to check header */
992 m->m_flags |= M_EAPOL;
997 * Non-qos traffic goes to BE.
999 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1005 * If node has a vlan tag then all traffic
1006 * to it must have a matching tag.
1009 if (ni->ni_vlan != 0) {
1010 if ((m->m_flags & M_VLANTAG) == 0) {
1011 IEEE80211_NODE_STAT(ni, tx_novlantag);
1014 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1015 EVL_VLANOFTAG(ni->ni_vlan)) {
1016 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1019 /* map vlan priority to AC */
1020 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1023 /* XXX m_copydata may be too slow for fast path */
1025 if (eh->ether_type == htons(ETHERTYPE_IP)) {
1028 * IP frame, map the DSCP bits from the TOS field.
1030 /* NB: ip header may not be in first mbuf */
1031 m_copydata(m, sizeof(struct ether_header) +
1032 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1033 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1034 d_wme_ac = TID_TO_WME_AC(tos);
1038 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
1042 * IPv6 frame, map the DSCP bits from the traffic class field.
1044 m_copydata(m, sizeof(struct ether_header) +
1045 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1047 tos = (uint8_t)(ntohl(flow) >> 20);
1048 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1049 d_wme_ac = TID_TO_WME_AC(tos);
1052 d_wme_ac = WME_AC_BE;
1060 * Use highest priority AC.
1062 if (v_wme_ac > d_wme_ac)
1070 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1071 static const int acmap[4] = {
1072 WME_AC_BK, /* WME_AC_BE */
1073 WME_AC_BK, /* WME_AC_BK */
1074 WME_AC_BE, /* WME_AC_VI */
1075 WME_AC_VI, /* WME_AC_VO */
1077 struct ieee80211com *ic = ni->ni_ic;
1079 while (ac != WME_AC_BK &&
1080 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1089 * Insure there is sufficient contiguous space to encapsulate the
1090 * 802.11 data frame. If room isn't already there, arrange for it.
1091 * Drivers and cipher modules assume we have done the necessary work
1092 * and fail rudely if they don't find the space they need.
1095 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1096 struct ieee80211_key *key, struct mbuf *m)
1098 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1099 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1102 /* XXX belongs in crypto code? */
1103 needed_space += key->wk_cipher->ic_header;
1106 * When crypto is being done in the host we must insure
1107 * the data are writable for the cipher routines; clone
1108 * a writable mbuf chain.
1109 * XXX handle SWMIC specially
1111 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1112 m = m_unshare(m, M_NOWAIT);
1114 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1115 "%s: cannot get writable mbuf\n", __func__);
1116 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1122 * We know we are called just before stripping an Ethernet
1123 * header and prepending an LLC header. This means we know
1125 * sizeof(struct ether_header) - sizeof(struct llc)
1126 * bytes recovered to which we need additional space for the
1127 * 802.11 header and any crypto header.
1129 /* XXX check trailing space and copy instead? */
1130 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1131 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1133 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1134 "%s: cannot expand storage\n", __func__);
1135 vap->iv_stats.is_tx_nobuf++;
1139 KASSERT(needed_space <= MHLEN,
1140 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1142 * Setup new mbuf to have leading space to prepend the
1143 * 802.11 header and any crypto header bits that are
1144 * required (the latter are added when the driver calls
1145 * back to ieee80211_crypto_encap to do crypto encapsulation).
1147 /* NB: must be first 'cuz it clobbers m_data */
1148 m_move_pkthdr(n, m);
1149 n->m_len = 0; /* NB: m_gethdr does not set */
1150 n->m_data += needed_space;
1152 * Pull up Ethernet header to create the expected layout.
1153 * We could use m_pullup but that's overkill (i.e. we don't
1154 * need the actual data) and it cannot fail so do it inline
1157 /* NB: struct ether_header is known to be contiguous */
1158 n->m_len += sizeof(struct ether_header);
1159 m->m_len -= sizeof(struct ether_header);
1160 m->m_data += sizeof(struct ether_header);
1162 * Replace the head of the chain.
1168 #undef TO_BE_RECLAIMED
1172 * Return the transmit key to use in sending a unicast frame.
1173 * If a unicast key is set we use that. When no unicast key is set
1174 * we fall back to the default transmit key.
1176 static __inline struct ieee80211_key *
1177 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1178 struct ieee80211_node *ni)
1180 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1181 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1182 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1184 return &vap->iv_nw_keys[vap->iv_def_txkey];
1186 return &ni->ni_ucastkey;
1191 * Return the transmit key to use in sending a multicast frame.
1192 * Multicast traffic always uses the group key which is installed as
1193 * the default tx key.
1195 static __inline struct ieee80211_key *
1196 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1197 struct ieee80211_node *ni)
1199 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1200 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1202 return &vap->iv_nw_keys[vap->iv_def_txkey];
1206 * Encapsulate an outbound data frame. The mbuf chain is updated.
1207 * If an error is encountered NULL is returned. The caller is required
1208 * to provide a node reference and pullup the ethernet header in the
1211 * NB: Packet is assumed to be processed by ieee80211_classify which
1212 * marked EAPOL frames w/ M_EAPOL.
1215 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1218 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1219 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1220 struct ieee80211com *ic = ni->ni_ic;
1221 #ifdef IEEE80211_SUPPORT_MESH
1222 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1223 struct ieee80211_meshcntl_ae10 *mc;
1224 struct ieee80211_mesh_route *rt = NULL;
1227 struct ether_header eh;
1228 struct ieee80211_frame *wh;
1229 struct ieee80211_key *key;
1231 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1232 ieee80211_seq seqno;
1233 int meshhdrsize, meshae;
1237 IEEE80211_TX_LOCK_ASSERT(ic);
1240 * Copy existing Ethernet header to a safe place. The
1241 * rest of the code assumes it's ok to strip it when
1242 * reorganizing state for the final encapsulation.
1244 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1245 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1248 * Insure space for additional headers. First identify
1249 * transmit key to use in calculating any buffer adjustments
1250 * required. This is also used below to do privacy
1251 * encapsulation work. Then calculate the 802.11 header
1252 * size and any padding required by the driver.
1254 * Note key may be NULL if we fall back to the default
1255 * transmit key and that is not set. In that case the
1256 * buffer may not be expanded as needed by the cipher
1257 * routines, but they will/should discard it.
1259 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1260 if (vap->iv_opmode == IEEE80211_M_STA ||
1261 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1262 (vap->iv_opmode == IEEE80211_M_WDS &&
1263 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1264 key = ieee80211_crypto_getucastkey(vap, ni);
1266 key = ieee80211_crypto_getmcastkey(vap, ni);
1267 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1268 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1270 "no default transmit key (%s) deftxkey %u",
1271 __func__, vap->iv_def_txkey);
1272 vap->iv_stats.is_tx_nodefkey++;
1278 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1279 * frames so suppress use. This may be an issue if other
1280 * ap's require all data frames to be QoS-encapsulated
1281 * once negotiated in which case we'll need to make this
1283 * NB: mesh data frames are QoS.
1285 addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1286 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1287 (m->m_flags & M_EAPOL) == 0;
1289 hdrsize = sizeof(struct ieee80211_qosframe);
1291 hdrsize = sizeof(struct ieee80211_frame);
1292 #ifdef IEEE80211_SUPPORT_MESH
1293 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1295 * Mesh data frames are encapsulated according to the
1296 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1297 * o Group Addressed data (aka multicast) originating
1298 * at the local sta are sent w/ 3-address format and
1299 * address extension mode 00
1300 * o Individually Addressed data (aka unicast) originating
1301 * at the local sta are sent w/ 4-address format and
1302 * address extension mode 00
1303 * o Group Addressed data forwarded from a non-mesh sta are
1304 * sent w/ 3-address format and address extension mode 01
1305 * o Individually Address data from another sta are sent
1306 * w/ 4-address format and address extension mode 10
1308 is4addr = 0; /* NB: don't use, disable */
1309 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1310 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1311 KASSERT(rt != NULL, ("route is NULL"));
1312 dir = IEEE80211_FC1_DIR_DSTODS;
1313 hdrsize += IEEE80211_ADDR_LEN;
1314 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1315 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1317 IEEE80211_NOTE_MAC(vap,
1320 "%s", "trying to send to ourself");
1323 meshae = IEEE80211_MESH_AE_10;
1325 sizeof(struct ieee80211_meshcntl_ae10);
1327 meshae = IEEE80211_MESH_AE_00;
1329 sizeof(struct ieee80211_meshcntl);
1332 dir = IEEE80211_FC1_DIR_FROMDS;
1333 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1335 meshae = IEEE80211_MESH_AE_01;
1337 sizeof(struct ieee80211_meshcntl_ae01);
1340 meshae = IEEE80211_MESH_AE_00;
1341 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1347 * 4-address frames need to be generated for:
1348 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1349 * o packets sent through a vap marked for relaying
1350 * (e.g. a station operating with dynamic WDS)
1352 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1353 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1354 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1356 hdrsize += IEEE80211_ADDR_LEN;
1357 meshhdrsize = meshae = 0;
1358 #ifdef IEEE80211_SUPPORT_MESH
1362 * Honor driver DATAPAD requirement.
1364 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1365 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1369 if (__predict_true((m->m_flags & M_FF) == 0)) {
1373 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1375 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1378 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1379 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1380 llc = mtod(m, struct llc *);
1381 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1382 llc->llc_control = LLC_UI;
1383 llc->llc_snap.org_code[0] = 0;
1384 llc->llc_snap.org_code[1] = 0;
1385 llc->llc_snap.org_code[2] = 0;
1386 llc->llc_snap.ether_type = eh.ether_type;
1388 #ifdef IEEE80211_SUPPORT_SUPERG
1390 * Aggregated frame. Check if it's for AMSDU or FF.
1392 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1393 * anywhere for some reason. But, since 11n requires
1394 * AMSDU RX, we can just assume "11n" == "AMSDU".
1396 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1397 if (ieee80211_amsdu_tx_ok(ni)) {
1398 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1401 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1407 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1409 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1411 vap->iv_stats.is_tx_nobuf++;
1414 wh = mtod(m, struct ieee80211_frame *);
1415 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1416 *(uint16_t *)wh->i_dur = 0;
1417 qos = NULL; /* NB: quiet compiler */
1419 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1420 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1421 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1422 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1423 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1424 } else switch (vap->iv_opmode) {
1425 case IEEE80211_M_STA:
1426 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1427 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1428 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1429 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1431 case IEEE80211_M_IBSS:
1432 case IEEE80211_M_AHDEMO:
1433 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1434 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1435 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1437 * NB: always use the bssid from iv_bss as the
1438 * neighbor's may be stale after an ibss merge
1440 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1442 case IEEE80211_M_HOSTAP:
1443 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1444 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1445 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1446 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1448 #ifdef IEEE80211_SUPPORT_MESH
1449 case IEEE80211_M_MBSS:
1450 /* NB: offset by hdrspace to deal with DATAPAD */
1451 mc = (struct ieee80211_meshcntl_ae10 *)
1452 (mtod(m, uint8_t *) + hdrspace);
1455 case IEEE80211_MESH_AE_00: /* no proxy */
1457 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1458 IEEE80211_ADDR_COPY(wh->i_addr1,
1460 IEEE80211_ADDR_COPY(wh->i_addr2,
1462 IEEE80211_ADDR_COPY(wh->i_addr3,
1464 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1466 qos =((struct ieee80211_qosframe_addr4 *)
1468 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1470 IEEE80211_ADDR_COPY(wh->i_addr1,
1472 IEEE80211_ADDR_COPY(wh->i_addr2,
1474 IEEE80211_ADDR_COPY(wh->i_addr3,
1476 qos = ((struct ieee80211_qosframe *)
1480 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1481 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1482 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1483 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1484 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1486 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1488 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1490 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1491 KASSERT(rt != NULL, ("route is NULL"));
1492 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1493 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1494 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1495 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1496 mc->mc_flags = IEEE80211_MESH_AE_10;
1497 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1498 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1499 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1502 KASSERT(0, ("meshae %d", meshae));
1505 mc->mc_ttl = ms->ms_ttl;
1507 le32enc(mc->mc_seq, ms->ms_seq);
1510 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1514 if (m->m_flags & M_MORE_DATA)
1515 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1520 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1521 /* NB: mesh case handled earlier */
1522 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1523 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1524 ac = M_WME_GETAC(m);
1525 /* map from access class/queue to 11e header priorty value */
1526 tid = WME_AC_TO_TID(ac);
1527 qos[0] = tid & IEEE80211_QOS_TID;
1528 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1529 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1530 #ifdef IEEE80211_SUPPORT_MESH
1531 if (vap->iv_opmode == IEEE80211_M_MBSS)
1532 qos[1] = IEEE80211_QOS_MC;
1536 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1539 * If this is an A-MSDU then ensure we set the
1543 qos[0] |= IEEE80211_QOS_AMSDU;
1545 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1547 * NB: don't assign a sequence # to potential
1548 * aggregates; we expect this happens at the
1549 * point the frame comes off any aggregation q
1550 * as otherwise we may introduce holes in the
1551 * BA sequence space and/or make window accouting
1554 * XXX may want to control this with a driver
1555 * capability; this may also change when we pull
1556 * aggregation up into net80211
1558 seqno = ni->ni_txseqs[tid]++;
1559 *(uint16_t *)wh->i_seq =
1560 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1561 M_SEQNO_SET(m, seqno);
1564 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1565 *(uint16_t *)wh->i_seq =
1566 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1567 M_SEQNO_SET(m, seqno);
1570 * XXX TODO: we shouldn't allow EAPOL, etc that would
1571 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1574 printf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1579 /* check if xmit fragmentation is required */
1580 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1581 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1582 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1583 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1586 * IEEE 802.1X: send EAPOL frames always in the clear.
1587 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1589 if ((m->m_flags & M_EAPOL) == 0 ||
1590 ((vap->iv_flags & IEEE80211_F_WPA) &&
1591 (vap->iv_opmode == IEEE80211_M_STA ?
1592 !IEEE80211_KEY_UNDEFINED(key) :
1593 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1594 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1595 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1596 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1598 "%s", "enmic failed, discard frame");
1599 vap->iv_stats.is_crypto_enmicfail++;
1604 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1605 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1608 m->m_flags |= M_ENCAP; /* mark encapsulated */
1610 IEEE80211_NODE_STAT(ni, tx_data);
1611 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1612 IEEE80211_NODE_STAT(ni, tx_mcast);
1613 m->m_flags |= M_MCAST;
1615 IEEE80211_NODE_STAT(ni, tx_ucast);
1616 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1628 ieee80211_free_mbuf(struct mbuf *m)
1636 next = m->m_nextpkt;
1637 m->m_nextpkt = NULL;
1639 } while ((m = next) != NULL);
1643 * Fragment the frame according to the specified mtu.
1644 * The size of the 802.11 header (w/o padding) is provided
1645 * so we don't need to recalculate it. We create a new
1646 * mbuf for each fragment and chain it through m_nextpkt;
1647 * we might be able to optimize this by reusing the original
1648 * packet's mbufs but that is significantly more complicated.
1651 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1652 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1654 struct ieee80211com *ic = vap->iv_ic;
1655 struct ieee80211_frame *wh, *whf;
1656 struct mbuf *m, *prev;
1657 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1660 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1661 KASSERT(m0->m_pkthdr.len > mtu,
1662 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1665 * Honor driver DATAPAD requirement.
1667 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1668 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1672 wh = mtod(m0, struct ieee80211_frame *);
1673 /* NB: mark the first frag; it will be propagated below */
1674 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1675 totalhdrsize = hdrspace + ciphdrsize;
1677 off = mtu - ciphdrsize;
1678 remainder = m0->m_pkthdr.len - off;
1681 fragsize = MIN(totalhdrsize + remainder, mtu);
1682 m = m_get2(fragsize, M_NOWAIT, MT_DATA, M_PKTHDR);
1685 /* leave room to prepend any cipher header */
1686 m_align(m, fragsize - ciphdrsize);
1689 * Form the header in the fragment. Note that since
1690 * we mark the first fragment with the MORE_FRAG bit
1691 * it automatically is propagated to each fragment; we
1692 * need only clear it on the last fragment (done below).
1693 * NB: frag 1+ dont have Mesh Control field present.
1695 whf = mtod(m, struct ieee80211_frame *);
1696 memcpy(whf, wh, hdrsize);
1697 #ifdef IEEE80211_SUPPORT_MESH
1698 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1699 if (IEEE80211_IS_DSTODS(wh))
1700 ((struct ieee80211_qosframe_addr4 *)
1701 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1703 ((struct ieee80211_qosframe *)
1704 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1707 *(uint16_t *)&whf->i_seq[0] |= htole16(
1708 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1709 IEEE80211_SEQ_FRAG_SHIFT);
1712 payload = fragsize - totalhdrsize;
1713 /* NB: destination is known to be contiguous */
1715 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1716 m->m_len = hdrspace + payload;
1717 m->m_pkthdr.len = hdrspace + payload;
1718 m->m_flags |= M_FRAG;
1720 /* chain up the fragment */
1721 prev->m_nextpkt = m;
1724 /* deduct fragment just formed */
1725 remainder -= payload;
1727 } while (remainder != 0);
1729 /* set the last fragment */
1730 m->m_flags |= M_LASTFRAG;
1731 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1733 /* strip first mbuf now that everything has been copied */
1734 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1735 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1737 vap->iv_stats.is_tx_fragframes++;
1738 vap->iv_stats.is_tx_frags += fragno-1;
1742 /* reclaim fragments but leave original frame for caller to free */
1743 ieee80211_free_mbuf(m0->m_nextpkt);
1744 m0->m_nextpkt = NULL;
1749 * Add a supported rates element id to a frame.
1752 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1756 *frm++ = IEEE80211_ELEMID_RATES;
1757 nrates = rs->rs_nrates;
1758 if (nrates > IEEE80211_RATE_SIZE)
1759 nrates = IEEE80211_RATE_SIZE;
1761 memcpy(frm, rs->rs_rates, nrates);
1762 return frm + nrates;
1766 * Add an extended supported rates element id to a frame.
1769 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1772 * Add an extended supported rates element if operating in 11g mode.
1774 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1775 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1776 *frm++ = IEEE80211_ELEMID_XRATES;
1778 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1785 * Add an ssid element to a frame.
1788 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1790 *frm++ = IEEE80211_ELEMID_SSID;
1792 memcpy(frm, ssid, len);
1797 * Add an erp element to a frame.
1800 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1804 *frm++ = IEEE80211_ELEMID_ERP;
1807 if (ic->ic_nonerpsta != 0)
1808 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1809 if (ic->ic_flags & IEEE80211_F_USEPROT)
1810 erp |= IEEE80211_ERP_USE_PROTECTION;
1811 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1812 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1818 * Add a CFParams element to a frame.
1821 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1823 #define ADDSHORT(frm, v) do { \
1827 *frm++ = IEEE80211_ELEMID_CFPARMS;
1829 *frm++ = 0; /* CFP count */
1830 *frm++ = 2; /* CFP period */
1831 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1832 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1837 static __inline uint8_t *
1838 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1840 memcpy(frm, ie->ie_data, ie->ie_len);
1841 return frm + ie->ie_len;
1844 static __inline uint8_t *
1845 add_ie(uint8_t *frm, const uint8_t *ie)
1847 memcpy(frm, ie, 2 + ie[1]);
1848 return frm + 2 + ie[1];
1851 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1853 * Add a WME information element to a frame.
1856 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1858 static const struct ieee80211_wme_info info = {
1859 .wme_id = IEEE80211_ELEMID_VENDOR,
1860 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1861 .wme_oui = { WME_OUI_BYTES },
1862 .wme_type = WME_OUI_TYPE,
1863 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1864 .wme_version = WME_VERSION,
1867 memcpy(frm, &info, sizeof(info));
1868 return frm + sizeof(info);
1872 * Add a WME parameters element to a frame.
1875 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1877 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1878 #define ADDSHORT(frm, v) do { \
1882 /* NB: this works 'cuz a param has an info at the front */
1883 static const struct ieee80211_wme_info param = {
1884 .wme_id = IEEE80211_ELEMID_VENDOR,
1885 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1886 .wme_oui = { WME_OUI_BYTES },
1887 .wme_type = WME_OUI_TYPE,
1888 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1889 .wme_version = WME_VERSION,
1893 memcpy(frm, ¶m, sizeof(param));
1894 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1895 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1896 *frm++ = 0; /* reserved field */
1897 for (i = 0; i < WME_NUM_AC; i++) {
1898 const struct wmeParams *ac =
1899 &wme->wme_bssChanParams.cap_wmeParams[i];
1900 *frm++ = SM(i, WME_PARAM_ACI)
1901 | SM(ac->wmep_acm, WME_PARAM_ACM)
1902 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1904 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1905 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1907 ADDSHORT(frm, ac->wmep_txopLimit);
1913 #undef WME_OUI_BYTES
1916 * Add an 11h Power Constraint element to a frame.
1919 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1921 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1922 /* XXX per-vap tx power limit? */
1923 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1925 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1927 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1932 * Add an 11h Power Capability element to a frame.
1935 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1937 frm[0] = IEEE80211_ELEMID_PWRCAP;
1939 frm[2] = c->ic_minpower;
1940 frm[3] = c->ic_maxpower;
1945 * Add an 11h Supported Channels element to a frame.
1948 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1950 static const int ielen = 26;
1952 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1954 /* XXX not correct */
1955 memcpy(frm+2, ic->ic_chan_avail, ielen);
1956 return frm + 2 + ielen;
1960 * Add an 11h Quiet time element to a frame.
1963 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1965 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1967 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1969 if (vap->iv_quiet_count_value == 1)
1970 vap->iv_quiet_count_value = vap->iv_quiet_count;
1971 else if (vap->iv_quiet_count_value > 1)
1972 vap->iv_quiet_count_value--;
1974 if (vap->iv_quiet_count_value == 0) {
1975 /* value 0 is reserved as per 802.11h standerd */
1976 vap->iv_quiet_count_value = 1;
1979 quiet->tbttcount = vap->iv_quiet_count_value;
1980 quiet->period = vap->iv_quiet_period;
1981 quiet->duration = htole16(vap->iv_quiet_duration);
1982 quiet->offset = htole16(vap->iv_quiet_offset);
1983 return frm + sizeof(*quiet);
1987 * Add an 11h Channel Switch Announcement element to a frame.
1988 * Note that we use the per-vap CSA count to adjust the global
1989 * counter so we can use this routine to form probe response
1990 * frames and get the current count.
1993 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1995 struct ieee80211com *ic = vap->iv_ic;
1996 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1998 csa->csa_ie = IEEE80211_ELEMID_CSA;
2000 csa->csa_mode = 1; /* XXX force quiet on channel */
2001 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2002 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2003 return frm + sizeof(*csa);
2007 * Add an 11h country information element to a frame.
2010 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2013 if (ic->ic_countryie == NULL ||
2014 ic->ic_countryie_chan != ic->ic_bsschan) {
2016 * Handle lazy construction of ie. This is done on
2017 * first use and after a channel change that requires
2020 if (ic->ic_countryie != NULL)
2021 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2022 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2023 if (ic->ic_countryie == NULL)
2025 ic->ic_countryie_chan = ic->ic_bsschan;
2027 return add_appie(frm, ic->ic_countryie);
2031 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2033 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2034 return (add_ie(frm, vap->iv_wpa_ie));
2036 /* XXX else complain? */
2042 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2044 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2045 return (add_ie(frm, vap->iv_rsn_ie));
2047 /* XXX else complain? */
2053 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2055 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2056 *frm++ = IEEE80211_ELEMID_QOS;
2065 * Send a probe request frame with the specified ssid
2066 * and any optional information element data.
2069 ieee80211_send_probereq(struct ieee80211_node *ni,
2070 const uint8_t sa[IEEE80211_ADDR_LEN],
2071 const uint8_t da[IEEE80211_ADDR_LEN],
2072 const uint8_t bssid[IEEE80211_ADDR_LEN],
2073 const uint8_t *ssid, size_t ssidlen)
2075 struct ieee80211vap *vap = ni->ni_vap;
2076 struct ieee80211com *ic = ni->ni_ic;
2077 struct ieee80211_node *bss;
2078 const struct ieee80211_txparam *tp;
2079 struct ieee80211_bpf_params params;
2080 const struct ieee80211_rateset *rs;
2085 bss = ieee80211_ref_node(vap->iv_bss);
2087 if (vap->iv_state == IEEE80211_S_CAC) {
2088 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2089 "block %s frame in CAC state", "probe request");
2090 vap->iv_stats.is_tx_badstate++;
2091 ieee80211_free_node(bss);
2092 return EIO; /* XXX */
2096 * Hold a reference on the node so it doesn't go away until after
2097 * the xmit is complete all the way in the driver. On error we
2098 * will remove our reference.
2100 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2101 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2103 ni, ether_sprintf(ni->ni_macaddr),
2104 ieee80211_node_refcnt(ni)+1);
2105 ieee80211_ref_node(ni);
2108 * prreq frame format
2110 * [tlv] supported rates
2111 * [tlv] RSN (optional)
2112 * [tlv] extended supported rates
2113 * [tlv] HT cap (optional)
2114 * [tlv] WPA (optional)
2115 * [tlv] user-specified ie's
2117 m = ieee80211_getmgtframe(&frm,
2118 ic->ic_headroom + sizeof(struct ieee80211_frame),
2119 2 + IEEE80211_NWID_LEN
2120 + 2 + IEEE80211_RATE_SIZE
2121 + sizeof(struct ieee80211_ie_htcap)
2122 + sizeof(struct ieee80211_ie_htinfo)
2123 + sizeof(struct ieee80211_ie_wpa)
2124 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2125 + sizeof(struct ieee80211_ie_wpa)
2126 + (vap->iv_appie_probereq != NULL ?
2127 vap->iv_appie_probereq->ie_len : 0)
2130 vap->iv_stats.is_tx_nobuf++;
2131 ieee80211_free_node(ni);
2132 ieee80211_free_node(bss);
2136 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2137 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2138 frm = ieee80211_add_rates(frm, rs);
2139 frm = ieee80211_add_rsn(frm, vap);
2140 frm = ieee80211_add_xrates(frm, rs);
2143 * Note: we can't use bss; we don't have one yet.
2145 * So, we should announce our capabilities
2146 * in this channel mode (2g/5g), not the
2147 * channel details itself.
2149 if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
2150 (vap->iv_flags_ht & IEEE80211_FHT_HT)) {
2151 struct ieee80211_channel *c;
2154 * Get the HT channel that we should try upgrading to.
2155 * If we can do 40MHz then this'll upgrade it appropriately.
2157 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
2159 frm = ieee80211_add_htcap_ch(frm, vap, c);
2162 frm = ieee80211_add_wpa(frm, vap);
2163 if (vap->iv_appie_probereq != NULL)
2164 frm = add_appie(frm, vap->iv_appie_probereq);
2165 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2167 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2168 ("leading space %zd", M_LEADINGSPACE(m)));
2169 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2171 /* NB: cannot happen */
2172 ieee80211_free_node(ni);
2173 ieee80211_free_node(bss);
2177 IEEE80211_TX_LOCK(ic);
2178 ieee80211_send_setup(ni, m,
2179 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2180 IEEE80211_NONQOS_TID, sa, da, bssid);
2181 /* XXX power management? */
2182 m->m_flags |= M_ENCAP; /* mark encapsulated */
2184 M_WME_SETAC(m, WME_AC_BE);
2186 IEEE80211_NODE_STAT(ni, tx_probereq);
2187 IEEE80211_NODE_STAT(ni, tx_mgmt);
2189 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2190 "send probe req on channel %u bssid %s sa %6D da %6D ssid \"%.*s\"\n",
2191 ieee80211_chan2ieee(ic, ic->ic_curchan),
2192 ether_sprintf(bssid),
2197 memset(¶ms, 0, sizeof(params));
2198 params.ibp_pri = M_WME_GETAC(m);
2199 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2200 params.ibp_rate0 = tp->mgmtrate;
2201 if (IEEE80211_IS_MULTICAST(da)) {
2202 params.ibp_flags |= IEEE80211_BPF_NOACK;
2203 params.ibp_try0 = 1;
2205 params.ibp_try0 = tp->maxretry;
2206 params.ibp_power = ni->ni_txpower;
2207 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2208 IEEE80211_TX_UNLOCK(ic);
2209 ieee80211_free_node(bss);
2214 * Calculate capability information for mgt frames.
2217 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2219 struct ieee80211com *ic = vap->iv_ic;
2222 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2224 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2225 capinfo = IEEE80211_CAPINFO_ESS;
2226 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2227 capinfo = IEEE80211_CAPINFO_IBSS;
2230 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2231 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2232 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2233 IEEE80211_IS_CHAN_2GHZ(chan))
2234 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2235 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2236 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2237 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2238 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2243 * Send a management frame. The node is for the destination (or ic_bss
2244 * when in station mode). Nodes other than ic_bss have their reference
2245 * count bumped to reflect our use for an indeterminant time.
2248 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2250 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2251 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2252 struct ieee80211vap *vap = ni->ni_vap;
2253 struct ieee80211com *ic = ni->ni_ic;
2254 struct ieee80211_node *bss = vap->iv_bss;
2255 struct ieee80211_bpf_params params;
2259 int has_challenge, is_shared_key, ret, status;
2261 KASSERT(ni != NULL, ("null node"));
2264 * Hold a reference on the node so it doesn't go away until after
2265 * the xmit is complete all the way in the driver. On error we
2266 * will remove our reference.
2268 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2269 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2271 ni, ether_sprintf(ni->ni_macaddr),
2272 ieee80211_node_refcnt(ni)+1);
2273 ieee80211_ref_node(ni);
2275 memset(¶ms, 0, sizeof(params));
2278 case IEEE80211_FC0_SUBTYPE_AUTH:
2281 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2282 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2283 ni->ni_challenge != NULL);
2286 * Deduce whether we're doing open authentication or
2287 * shared key authentication. We do the latter if
2288 * we're in the middle of a shared key authentication
2289 * handshake or if we're initiating an authentication
2290 * request and configured to use shared key.
2292 is_shared_key = has_challenge ||
2293 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2294 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2295 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2297 m = ieee80211_getmgtframe(&frm,
2298 ic->ic_headroom + sizeof(struct ieee80211_frame),
2299 3 * sizeof(uint16_t)
2300 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2301 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2304 senderr(ENOMEM, is_tx_nobuf);
2306 ((uint16_t *)frm)[0] =
2307 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2308 : htole16(IEEE80211_AUTH_ALG_OPEN);
2309 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2310 ((uint16_t *)frm)[2] = htole16(status);/* status */
2312 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2313 ((uint16_t *)frm)[3] =
2314 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2315 IEEE80211_ELEMID_CHALLENGE);
2316 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2317 IEEE80211_CHALLENGE_LEN);
2318 m->m_pkthdr.len = m->m_len =
2319 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2320 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2321 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2322 "request encrypt frame (%s)", __func__);
2323 /* mark frame for encryption */
2324 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2327 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2329 /* XXX not right for shared key */
2330 if (status == IEEE80211_STATUS_SUCCESS)
2331 IEEE80211_NODE_STAT(ni, tx_auth);
2333 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2335 if (vap->iv_opmode == IEEE80211_M_STA)
2336 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2337 (void *) vap->iv_state);
2340 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2341 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2342 "send station deauthenticate (reason: %d (%s))", arg,
2343 ieee80211_reason_to_string(arg));
2344 m = ieee80211_getmgtframe(&frm,
2345 ic->ic_headroom + sizeof(struct ieee80211_frame),
2348 senderr(ENOMEM, is_tx_nobuf);
2349 *(uint16_t *)frm = htole16(arg); /* reason */
2350 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2352 IEEE80211_NODE_STAT(ni, tx_deauth);
2353 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2355 ieee80211_node_unauthorize(ni); /* port closed */
2358 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2359 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2361 * asreq frame format
2362 * [2] capability information
2363 * [2] listen interval
2364 * [6*] current AP address (reassoc only)
2366 * [tlv] supported rates
2367 * [tlv] extended supported rates
2368 * [4] power capability (optional)
2369 * [28] supported channels (optional)
2370 * [tlv] HT capabilities
2371 * [tlv] WME (optional)
2372 * [tlv] Vendor OUI HT capabilities (optional)
2373 * [tlv] Atheros capabilities (if negotiated)
2374 * [tlv] AppIE's (optional)
2376 m = ieee80211_getmgtframe(&frm,
2377 ic->ic_headroom + sizeof(struct ieee80211_frame),
2380 + IEEE80211_ADDR_LEN
2381 + 2 + IEEE80211_NWID_LEN
2382 + 2 + IEEE80211_RATE_SIZE
2383 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2386 + sizeof(struct ieee80211_wme_info)
2387 + sizeof(struct ieee80211_ie_htcap)
2388 + 4 + sizeof(struct ieee80211_ie_htcap)
2389 #ifdef IEEE80211_SUPPORT_SUPERG
2390 + sizeof(struct ieee80211_ath_ie)
2392 + (vap->iv_appie_wpa != NULL ?
2393 vap->iv_appie_wpa->ie_len : 0)
2394 + (vap->iv_appie_assocreq != NULL ?
2395 vap->iv_appie_assocreq->ie_len : 0)
2398 senderr(ENOMEM, is_tx_nobuf);
2400 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2401 ("wrong mode %u", vap->iv_opmode));
2402 capinfo = IEEE80211_CAPINFO_ESS;
2403 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2404 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2406 * NB: Some 11a AP's reject the request when
2407 * short premable is set.
2409 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2410 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2411 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2412 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2413 (ic->ic_caps & IEEE80211_C_SHSLOT))
2414 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2415 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2416 (vap->iv_flags & IEEE80211_F_DOTH))
2417 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2418 *(uint16_t *)frm = htole16(capinfo);
2421 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2422 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2426 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2427 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2428 frm += IEEE80211_ADDR_LEN;
2431 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2432 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2433 frm = ieee80211_add_rsn(frm, vap);
2434 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2435 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2436 frm = ieee80211_add_powercapability(frm,
2438 frm = ieee80211_add_supportedchannels(frm, ic);
2442 * Check the channel - we may be using an 11n NIC with an
2443 * 11n capable station, but we're configured to be an 11b
2446 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2447 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2448 ni->ni_ies.htcap_ie != NULL &&
2449 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2450 frm = ieee80211_add_htcap(frm, ni);
2452 frm = ieee80211_add_wpa(frm, vap);
2453 if ((ic->ic_flags & IEEE80211_F_WME) &&
2454 ni->ni_ies.wme_ie != NULL)
2455 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2458 * Same deal - only send HT info if we're on an 11n
2461 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2462 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2463 ni->ni_ies.htcap_ie != NULL &&
2464 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2465 frm = ieee80211_add_htcap_vendor(frm, ni);
2467 #ifdef IEEE80211_SUPPORT_SUPERG
2468 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2469 frm = ieee80211_add_ath(frm,
2470 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2471 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2472 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2473 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2475 #endif /* IEEE80211_SUPPORT_SUPERG */
2476 if (vap->iv_appie_assocreq != NULL)
2477 frm = add_appie(frm, vap->iv_appie_assocreq);
2478 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2480 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2481 (void *) vap->iv_state);
2484 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2485 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2487 * asresp frame format
2488 * [2] capability information
2490 * [2] association ID
2491 * [tlv] supported rates
2492 * [tlv] extended supported rates
2493 * [tlv] HT capabilities (standard, if STA enabled)
2494 * [tlv] HT information (standard, if STA enabled)
2495 * [tlv] WME (if configured and STA enabled)
2496 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2497 * [tlv] HT information (vendor OUI, if STA enabled)
2498 * [tlv] Atheros capabilities (if STA enabled)
2499 * [tlv] AppIE's (optional)
2501 m = ieee80211_getmgtframe(&frm,
2502 ic->ic_headroom + sizeof(struct ieee80211_frame),
2506 + 2 + IEEE80211_RATE_SIZE
2507 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2508 + sizeof(struct ieee80211_ie_htcap) + 4
2509 + sizeof(struct ieee80211_ie_htinfo) + 4
2510 + sizeof(struct ieee80211_wme_param)
2511 #ifdef IEEE80211_SUPPORT_SUPERG
2512 + sizeof(struct ieee80211_ath_ie)
2514 + (vap->iv_appie_assocresp != NULL ?
2515 vap->iv_appie_assocresp->ie_len : 0)
2518 senderr(ENOMEM, is_tx_nobuf);
2520 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2521 *(uint16_t *)frm = htole16(capinfo);
2524 *(uint16_t *)frm = htole16(arg); /* status */
2527 if (arg == IEEE80211_STATUS_SUCCESS) {
2528 *(uint16_t *)frm = htole16(ni->ni_associd);
2529 IEEE80211_NODE_STAT(ni, tx_assoc);
2531 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2534 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2535 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2536 /* NB: respond according to what we received */
2537 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2538 frm = ieee80211_add_htcap(frm, ni);
2539 frm = ieee80211_add_htinfo(frm, ni);
2541 if ((vap->iv_flags & IEEE80211_F_WME) &&
2542 ni->ni_ies.wme_ie != NULL)
2543 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2544 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2545 frm = ieee80211_add_htcap_vendor(frm, ni);
2546 frm = ieee80211_add_htinfo_vendor(frm, ni);
2548 #ifdef IEEE80211_SUPPORT_SUPERG
2549 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2550 frm = ieee80211_add_ath(frm,
2551 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2552 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2553 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2554 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2555 #endif /* IEEE80211_SUPPORT_SUPERG */
2556 if (vap->iv_appie_assocresp != NULL)
2557 frm = add_appie(frm, vap->iv_appie_assocresp);
2558 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2561 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2562 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2563 "send station disassociate (reason: %d (%s))", arg,
2564 ieee80211_reason_to_string(arg));
2565 m = ieee80211_getmgtframe(&frm,
2566 ic->ic_headroom + sizeof(struct ieee80211_frame),
2569 senderr(ENOMEM, is_tx_nobuf);
2570 *(uint16_t *)frm = htole16(arg); /* reason */
2571 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2573 IEEE80211_NODE_STAT(ni, tx_disassoc);
2574 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2578 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2579 "invalid mgmt frame type %u", type);
2580 senderr(EINVAL, is_tx_unknownmgt);
2584 /* NB: force non-ProbeResp frames to the highest queue */
2585 params.ibp_pri = WME_AC_VO;
2586 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2587 /* NB: we know all frames are unicast */
2588 params.ibp_try0 = bss->ni_txparms->maxretry;
2589 params.ibp_power = bss->ni_txpower;
2590 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2592 ieee80211_free_node(ni);
2599 * Return an mbuf with a probe response frame in it.
2600 * Space is left to prepend and 802.11 header at the
2601 * front but it's left to the caller to fill in.
2604 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2606 struct ieee80211vap *vap = bss->ni_vap;
2607 struct ieee80211com *ic = bss->ni_ic;
2608 const struct ieee80211_rateset *rs;
2614 * probe response frame format
2616 * [2] beacon interval
2617 * [2] cabability information
2619 * [tlv] supported rates
2620 * [tlv] parameter set (FH/DS)
2621 * [tlv] parameter set (IBSS)
2622 * [tlv] country (optional)
2623 * [3] power control (optional)
2624 * [5] channel switch announcement (CSA) (optional)
2625 * [tlv] extended rate phy (ERP)
2626 * [tlv] extended supported rates
2627 * [tlv] RSN (optional)
2628 * [tlv] HT capabilities
2629 * [tlv] HT information
2630 * [tlv] WPA (optional)
2631 * [tlv] WME (optional)
2632 * [tlv] Vendor OUI HT capabilities (optional)
2633 * [tlv] Vendor OUI HT information (optional)
2634 * [tlv] Atheros capabilities
2635 * [tlv] AppIE's (optional)
2636 * [tlv] Mesh ID (MBSS)
2637 * [tlv] Mesh Conf (MBSS)
2639 m = ieee80211_getmgtframe(&frm,
2640 ic->ic_headroom + sizeof(struct ieee80211_frame),
2644 + 2 + IEEE80211_NWID_LEN
2645 + 2 + IEEE80211_RATE_SIZE
2647 + IEEE80211_COUNTRY_MAX_SIZE
2649 + sizeof(struct ieee80211_csa_ie)
2650 + sizeof(struct ieee80211_quiet_ie)
2652 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2653 + sizeof(struct ieee80211_ie_wpa)
2654 + sizeof(struct ieee80211_ie_htcap)
2655 + sizeof(struct ieee80211_ie_htinfo)
2656 + sizeof(struct ieee80211_ie_wpa)
2657 + sizeof(struct ieee80211_wme_param)
2658 + 4 + sizeof(struct ieee80211_ie_htcap)
2659 + 4 + sizeof(struct ieee80211_ie_htinfo)
2660 #ifdef IEEE80211_SUPPORT_SUPERG
2661 + sizeof(struct ieee80211_ath_ie)
2663 #ifdef IEEE80211_SUPPORT_MESH
2664 + 2 + IEEE80211_MESHID_LEN
2665 + sizeof(struct ieee80211_meshconf_ie)
2667 + (vap->iv_appie_proberesp != NULL ?
2668 vap->iv_appie_proberesp->ie_len : 0)
2671 vap->iv_stats.is_tx_nobuf++;
2675 memset(frm, 0, 8); /* timestamp should be filled later */
2677 *(uint16_t *)frm = htole16(bss->ni_intval);
2679 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2680 *(uint16_t *)frm = htole16(capinfo);
2683 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2684 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2685 frm = ieee80211_add_rates(frm, rs);
2687 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2688 *frm++ = IEEE80211_ELEMID_FHPARMS;
2690 *frm++ = bss->ni_fhdwell & 0x00ff;
2691 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2692 *frm++ = IEEE80211_FH_CHANSET(
2693 ieee80211_chan2ieee(ic, bss->ni_chan));
2694 *frm++ = IEEE80211_FH_CHANPAT(
2695 ieee80211_chan2ieee(ic, bss->ni_chan));
2696 *frm++ = bss->ni_fhindex;
2698 *frm++ = IEEE80211_ELEMID_DSPARMS;
2700 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2703 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2704 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2706 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2708 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2709 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2710 frm = ieee80211_add_countryie(frm, ic);
2711 if (vap->iv_flags & IEEE80211_F_DOTH) {
2712 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2713 frm = ieee80211_add_powerconstraint(frm, vap);
2714 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2715 frm = ieee80211_add_csa(frm, vap);
2717 if (vap->iv_flags & IEEE80211_F_DOTH) {
2718 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2719 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2721 frm = ieee80211_add_quiet(frm, vap);
2724 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2725 frm = ieee80211_add_erp(frm, ic);
2726 frm = ieee80211_add_xrates(frm, rs);
2727 frm = ieee80211_add_rsn(frm, vap);
2729 * NB: legacy 11b clients do not get certain ie's.
2730 * The caller identifies such clients by passing
2731 * a token in legacy to us. Could expand this to be
2732 * any legacy client for stuff like HT ie's.
2734 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2735 legacy != IEEE80211_SEND_LEGACY_11B) {
2736 frm = ieee80211_add_htcap(frm, bss);
2737 frm = ieee80211_add_htinfo(frm, bss);
2739 frm = ieee80211_add_wpa(frm, vap);
2740 if (vap->iv_flags & IEEE80211_F_WME)
2741 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2742 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2743 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2744 legacy != IEEE80211_SEND_LEGACY_11B) {
2745 frm = ieee80211_add_htcap_vendor(frm, bss);
2746 frm = ieee80211_add_htinfo_vendor(frm, bss);
2748 #ifdef IEEE80211_SUPPORT_SUPERG
2749 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2750 legacy != IEEE80211_SEND_LEGACY_11B)
2751 frm = ieee80211_add_athcaps(frm, bss);
2753 if (vap->iv_appie_proberesp != NULL)
2754 frm = add_appie(frm, vap->iv_appie_proberesp);
2755 #ifdef IEEE80211_SUPPORT_MESH
2756 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2757 frm = ieee80211_add_meshid(frm, vap);
2758 frm = ieee80211_add_meshconf(frm, vap);
2761 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2767 * Send a probe response frame to the specified mac address.
2768 * This does not go through the normal mgt frame api so we
2769 * can specify the destination address and re-use the bss node
2770 * for the sta reference.
2773 ieee80211_send_proberesp(struct ieee80211vap *vap,
2774 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2776 struct ieee80211_node *bss = vap->iv_bss;
2777 struct ieee80211com *ic = vap->iv_ic;
2781 if (vap->iv_state == IEEE80211_S_CAC) {
2782 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2783 "block %s frame in CAC state", "probe response");
2784 vap->iv_stats.is_tx_badstate++;
2785 return EIO; /* XXX */
2789 * Hold a reference on the node so it doesn't go away until after
2790 * the xmit is complete all the way in the driver. On error we
2791 * will remove our reference.
2793 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2794 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2795 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2796 ieee80211_node_refcnt(bss)+1);
2797 ieee80211_ref_node(bss);
2799 m = ieee80211_alloc_proberesp(bss, legacy);
2801 ieee80211_free_node(bss);
2805 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2806 KASSERT(m != NULL, ("no room for header"));
2808 IEEE80211_TX_LOCK(ic);
2809 ieee80211_send_setup(bss, m,
2810 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2811 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2812 /* XXX power management? */
2813 m->m_flags |= M_ENCAP; /* mark encapsulated */
2815 M_WME_SETAC(m, WME_AC_BE);
2817 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2818 "send probe resp on channel %u to %s%s\n",
2819 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2820 legacy ? " <legacy>" : "");
2821 IEEE80211_NODE_STAT(bss, tx_mgmt);
2823 ret = ieee80211_raw_output(vap, bss, m, NULL);
2824 IEEE80211_TX_UNLOCK(ic);
2829 * Allocate and build a RTS (Request To Send) control frame.
2832 ieee80211_alloc_rts(struct ieee80211com *ic,
2833 const uint8_t ra[IEEE80211_ADDR_LEN],
2834 const uint8_t ta[IEEE80211_ADDR_LEN],
2837 struct ieee80211_frame_rts *rts;
2840 /* XXX honor ic_headroom */
2841 m = m_gethdr(M_NOWAIT, MT_DATA);
2843 rts = mtod(m, struct ieee80211_frame_rts *);
2844 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2845 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2846 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2847 *(u_int16_t *)rts->i_dur = htole16(dur);
2848 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2849 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2851 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2857 * Allocate and build a CTS (Clear To Send) control frame.
2860 ieee80211_alloc_cts(struct ieee80211com *ic,
2861 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2863 struct ieee80211_frame_cts *cts;
2866 /* XXX honor ic_headroom */
2867 m = m_gethdr(M_NOWAIT, MT_DATA);
2869 cts = mtod(m, struct ieee80211_frame_cts *);
2870 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2871 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2872 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2873 *(u_int16_t *)cts->i_dur = htole16(dur);
2874 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2876 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2882 ieee80211_tx_mgt_timeout(void *arg)
2884 struct ieee80211vap *vap = arg;
2886 IEEE80211_LOCK(vap->iv_ic);
2887 if (vap->iv_state != IEEE80211_S_INIT &&
2888 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2890 * NB: it's safe to specify a timeout as the reason here;
2891 * it'll only be used in the right state.
2893 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
2894 IEEE80211_SCAN_FAIL_TIMEOUT);
2896 IEEE80211_UNLOCK(vap->iv_ic);
2900 * This is the callback set on net80211-sourced transmitted
2901 * authentication request frames.
2903 * This does a couple of things:
2905 * + If the frame transmitted was a success, it schedules a future
2906 * event which will transition the interface to scan.
2907 * If a state transition _then_ occurs before that event occurs,
2908 * said state transition will cancel this callout.
2910 * + If the frame transmit was a failure, it immediately schedules
2911 * the transition back to scan.
2914 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2916 struct ieee80211vap *vap = ni->ni_vap;
2917 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2920 * Frame transmit completed; arrange timer callback. If
2921 * transmit was successfully we wait for response. Otherwise
2922 * we arrange an immediate callback instead of doing the
2923 * callback directly since we don't know what state the driver
2924 * is in (e.g. what locks it is holding). This work should
2925 * not be too time-critical and not happen too often so the
2926 * added overhead is acceptable.
2928 * XXX what happens if !acked but response shows up before callback?
2930 if (vap->iv_state == ostate) {
2931 callout_reset(&vap->iv_mgtsend,
2932 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2933 ieee80211_tx_mgt_timeout, vap);
2938 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2939 struct ieee80211_node *ni)
2941 struct ieee80211vap *vap = ni->ni_vap;
2942 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2943 struct ieee80211com *ic = ni->ni_ic;
2944 struct ieee80211_rateset *rs = &ni->ni_rates;
2948 * beacon frame format
2950 * [2] beacon interval
2951 * [2] cabability information
2953 * [tlv] supported rates
2954 * [3] parameter set (DS)
2955 * [8] CF parameter set (optional)
2956 * [tlv] parameter set (IBSS/TIM)
2957 * [tlv] country (optional)
2958 * [3] power control (optional)
2959 * [5] channel switch announcement (CSA) (optional)
2960 * [tlv] extended rate phy (ERP)
2961 * [tlv] extended supported rates
2962 * [tlv] RSN parameters
2963 * [tlv] HT capabilities
2964 * [tlv] HT information
2965 * XXX Vendor-specific OIDs (e.g. Atheros)
2966 * [tlv] WPA parameters
2967 * [tlv] WME parameters
2968 * [tlv] Vendor OUI HT capabilities (optional)
2969 * [tlv] Vendor OUI HT information (optional)
2970 * [tlv] Atheros capabilities (optional)
2971 * [tlv] TDMA parameters (optional)
2972 * [tlv] Mesh ID (MBSS)
2973 * [tlv] Mesh Conf (MBSS)
2974 * [tlv] application data (optional)
2977 memset(bo, 0, sizeof(*bo));
2979 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2981 *(uint16_t *)frm = htole16(ni->ni_intval);
2983 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2984 bo->bo_caps = (uint16_t *)frm;
2985 *(uint16_t *)frm = htole16(capinfo);
2987 *frm++ = IEEE80211_ELEMID_SSID;
2988 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2989 *frm++ = ni->ni_esslen;
2990 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2991 frm += ni->ni_esslen;
2994 frm = ieee80211_add_rates(frm, rs);
2995 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2996 *frm++ = IEEE80211_ELEMID_DSPARMS;
2998 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3000 if (ic->ic_flags & IEEE80211_F_PCF) {
3002 frm = ieee80211_add_cfparms(frm, ic);
3005 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3006 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3008 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3010 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3011 vap->iv_opmode == IEEE80211_M_MBSS) {
3012 /* TIM IE is the same for Mesh and Hostap */
3013 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3015 tie->tim_ie = IEEE80211_ELEMID_TIM;
3016 tie->tim_len = 4; /* length */
3017 tie->tim_count = 0; /* DTIM count */
3018 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3019 tie->tim_bitctl = 0; /* bitmap control */
3020 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3021 frm += sizeof(struct ieee80211_tim_ie);
3024 bo->bo_tim_trailer = frm;
3025 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3026 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3027 frm = ieee80211_add_countryie(frm, ic);
3028 if (vap->iv_flags & IEEE80211_F_DOTH) {
3029 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3030 frm = ieee80211_add_powerconstraint(frm, vap);
3032 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3033 frm = ieee80211_add_csa(frm, vap);
3037 if (vap->iv_flags & IEEE80211_F_DOTH) {
3039 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3040 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3042 frm = ieee80211_add_quiet(frm,vap);
3047 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3049 frm = ieee80211_add_erp(frm, ic);
3051 frm = ieee80211_add_xrates(frm, rs);
3052 frm = ieee80211_add_rsn(frm, vap);
3053 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3054 frm = ieee80211_add_htcap(frm, ni);
3055 bo->bo_htinfo = frm;
3056 frm = ieee80211_add_htinfo(frm, ni);
3058 frm = ieee80211_add_wpa(frm, vap);
3059 if (vap->iv_flags & IEEE80211_F_WME) {
3061 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
3063 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3064 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3065 frm = ieee80211_add_htcap_vendor(frm, ni);
3066 frm = ieee80211_add_htinfo_vendor(frm, ni);
3068 #ifdef IEEE80211_SUPPORT_SUPERG
3069 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3071 frm = ieee80211_add_athcaps(frm, ni);
3074 #ifdef IEEE80211_SUPPORT_TDMA
3075 if (vap->iv_caps & IEEE80211_C_TDMA) {
3077 frm = ieee80211_add_tdma(frm, vap);
3080 if (vap->iv_appie_beacon != NULL) {
3082 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3083 frm = add_appie(frm, vap->iv_appie_beacon);
3085 #ifdef IEEE80211_SUPPORT_MESH
3086 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3087 frm = ieee80211_add_meshid(frm, vap);
3088 bo->bo_meshconf = frm;
3089 frm = ieee80211_add_meshconf(frm, vap);
3092 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3093 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3094 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3098 * Allocate a beacon frame and fillin the appropriate bits.
3101 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3103 struct ieee80211vap *vap = ni->ni_vap;
3104 struct ieee80211com *ic = ni->ni_ic;
3105 struct ifnet *ifp = vap->iv_ifp;
3106 struct ieee80211_frame *wh;
3112 * beacon frame format
3114 * [2] beacon interval
3115 * [2] cabability information
3117 * [tlv] supported rates
3118 * [3] parameter set (DS)
3119 * [8] CF parameter set (optional)
3120 * [tlv] parameter set (IBSS/TIM)
3121 * [tlv] country (optional)
3122 * [3] power control (optional)
3123 * [5] channel switch announcement (CSA) (optional)
3124 * [tlv] extended rate phy (ERP)
3125 * [tlv] extended supported rates
3126 * [tlv] RSN parameters
3127 * [tlv] HT capabilities
3128 * [tlv] HT information
3129 * [tlv] Vendor OUI HT capabilities (optional)
3130 * [tlv] Vendor OUI HT information (optional)
3131 * XXX Vendor-specific OIDs (e.g. Atheros)
3132 * [tlv] WPA parameters
3133 * [tlv] WME parameters
3134 * [tlv] TDMA parameters (optional)
3135 * [tlv] Mesh ID (MBSS)
3136 * [tlv] Mesh Conf (MBSS)
3137 * [tlv] application data (optional)
3138 * NB: we allocate the max space required for the TIM bitmap.
3139 * XXX how big is this?
3141 pktlen = 8 /* time stamp */
3142 + sizeof(uint16_t) /* beacon interval */
3143 + sizeof(uint16_t) /* capabilities */
3144 + 2 + ni->ni_esslen /* ssid */
3145 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3146 + 2 + 1 /* DS parameters */
3147 + 2 + 6 /* CF parameters */
3148 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3149 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3150 + 2 + 1 /* power control */
3151 + sizeof(struct ieee80211_csa_ie) /* CSA */
3152 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3154 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3155 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3156 2*sizeof(struct ieee80211_ie_wpa) : 0)
3157 /* XXX conditional? */
3158 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3159 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3160 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3161 sizeof(struct ieee80211_wme_param) : 0)
3162 #ifdef IEEE80211_SUPPORT_SUPERG
3163 + sizeof(struct ieee80211_ath_ie) /* ATH */
3165 #ifdef IEEE80211_SUPPORT_TDMA
3166 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3167 sizeof(struct ieee80211_tdma_param) : 0)
3169 #ifdef IEEE80211_SUPPORT_MESH
3170 + 2 + ni->ni_meshidlen
3171 + sizeof(struct ieee80211_meshconf_ie)
3173 + IEEE80211_MAX_APPIE
3175 m = ieee80211_getmgtframe(&frm,
3176 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3178 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3179 "%s: cannot get buf; size %u\n", __func__, pktlen);
3180 vap->iv_stats.is_tx_nobuf++;
3183 ieee80211_beacon_construct(m, frm, ni);
3185 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3186 KASSERT(m != NULL, ("no space for 802.11 header?"));
3187 wh = mtod(m, struct ieee80211_frame *);
3188 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3189 IEEE80211_FC0_SUBTYPE_BEACON;
3190 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3191 *(uint16_t *)wh->i_dur = 0;
3192 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3193 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3194 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3195 *(uint16_t *)wh->i_seq = 0;
3201 * Update the dynamic parts of a beacon frame based on the current state.
3204 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3206 struct ieee80211vap *vap = ni->ni_vap;
3207 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3208 struct ieee80211com *ic = ni->ni_ic;
3209 int len_changed = 0;
3211 struct ieee80211_frame *wh;
3212 ieee80211_seq seqno;
3216 * Handle 11h channel change when we've reached the count.
3217 * We must recalculate the beacon frame contents to account
3218 * for the new channel. Note we do this only for the first
3219 * vap that reaches this point; subsequent vaps just update
3220 * their beacon state to reflect the recalculated channel.
3222 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3223 vap->iv_csa_count == ic->ic_csa_count) {
3224 vap->iv_csa_count = 0;
3226 * Effect channel change before reconstructing the beacon
3227 * frame contents as many places reference ni_chan.
3229 if (ic->ic_csa_newchan != NULL)
3230 ieee80211_csa_completeswitch(ic);
3232 * NB: ieee80211_beacon_construct clears all pending
3233 * updates in bo_flags so we don't need to explicitly
3234 * clear IEEE80211_BEACON_CSA.
3236 ieee80211_beacon_construct(m,
3237 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3239 /* XXX do WME aggressive mode processing? */
3240 IEEE80211_UNLOCK(ic);
3241 return 1; /* just assume length changed */
3244 wh = mtod(m, struct ieee80211_frame *);
3245 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3246 *(uint16_t *)&wh->i_seq[0] =
3247 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3248 M_SEQNO_SET(m, seqno);
3250 /* XXX faster to recalculate entirely or just changes? */
3251 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3252 *bo->bo_caps = htole16(capinfo);
3254 if (vap->iv_flags & IEEE80211_F_WME) {
3255 struct ieee80211_wme_state *wme = &ic->ic_wme;
3258 * Check for aggressive mode change. When there is
3259 * significant high priority traffic in the BSS
3260 * throttle back BE traffic by using conservative
3261 * parameters. Otherwise BE uses aggressive params
3262 * to optimize performance of legacy/non-QoS traffic.
3264 if (wme->wme_flags & WME_F_AGGRMODE) {
3265 if (wme->wme_hipri_traffic >
3266 wme->wme_hipri_switch_thresh) {
3267 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3268 "%s: traffic %u, disable aggressive mode\n",
3269 __func__, wme->wme_hipri_traffic);
3270 wme->wme_flags &= ~WME_F_AGGRMODE;
3271 ieee80211_wme_updateparams_locked(vap);
3272 wme->wme_hipri_traffic =
3273 wme->wme_hipri_switch_hysteresis;
3275 wme->wme_hipri_traffic = 0;
3277 if (wme->wme_hipri_traffic <=
3278 wme->wme_hipri_switch_thresh) {
3279 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3280 "%s: traffic %u, enable aggressive mode\n",
3281 __func__, wme->wme_hipri_traffic);
3282 wme->wme_flags |= WME_F_AGGRMODE;
3283 ieee80211_wme_updateparams_locked(vap);
3284 wme->wme_hipri_traffic = 0;
3286 wme->wme_hipri_traffic =
3287 wme->wme_hipri_switch_hysteresis;
3289 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3290 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
3291 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3295 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3296 ieee80211_ht_update_beacon(vap, bo);
3297 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3299 #ifdef IEEE80211_SUPPORT_TDMA
3300 if (vap->iv_caps & IEEE80211_C_TDMA) {
3302 * NB: the beacon is potentially updated every TBTT.
3304 ieee80211_tdma_update_beacon(vap, bo);
3307 #ifdef IEEE80211_SUPPORT_MESH
3308 if (vap->iv_opmode == IEEE80211_M_MBSS)
3309 ieee80211_mesh_update_beacon(vap, bo);
3312 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3313 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3314 struct ieee80211_tim_ie *tie =
3315 (struct ieee80211_tim_ie *) bo->bo_tim;
3316 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3317 u_int timlen, timoff, i;
3319 * ATIM/DTIM needs updating. If it fits in the
3320 * current space allocated then just copy in the
3321 * new bits. Otherwise we need to move any trailing
3322 * data to make room. Note that we know there is
3323 * contiguous space because ieee80211_beacon_allocate
3324 * insures there is space in the mbuf to write a
3325 * maximal-size virtual bitmap (based on iv_max_aid).
3328 * Calculate the bitmap size and offset, copy any
3329 * trailer out of the way, and then copy in the
3330 * new bitmap and update the information element.
3331 * Note that the tim bitmap must contain at least
3332 * one byte and any offset must be even.
3334 if (vap->iv_ps_pending != 0) {
3335 timoff = 128; /* impossibly large */
3336 for (i = 0; i < vap->iv_tim_len; i++)
3337 if (vap->iv_tim_bitmap[i]) {
3341 KASSERT(timoff != 128, ("tim bitmap empty!"));
3342 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3343 if (vap->iv_tim_bitmap[i])
3345 timlen = 1 + (i - timoff);
3350 if (timlen != bo->bo_tim_len) {
3351 /* copy up/down trailer */
3352 int adjust = tie->tim_bitmap+timlen
3353 - bo->bo_tim_trailer;
3354 ovbcopy(bo->bo_tim_trailer,
3355 bo->bo_tim_trailer+adjust,
3356 bo->bo_tim_trailer_len);
3357 bo->bo_tim_trailer += adjust;
3358 bo->bo_erp += adjust;
3359 bo->bo_htinfo += adjust;
3360 #ifdef IEEE80211_SUPPORT_SUPERG
3361 bo->bo_ath += adjust;
3363 #ifdef IEEE80211_SUPPORT_TDMA
3364 bo->bo_tdma += adjust;
3366 #ifdef IEEE80211_SUPPORT_MESH
3367 bo->bo_meshconf += adjust;
3369 bo->bo_appie += adjust;
3370 bo->bo_wme += adjust;
3371 bo->bo_csa += adjust;
3372 bo->bo_quiet += adjust;
3373 bo->bo_tim_len = timlen;
3375 /* update information element */
3376 tie->tim_len = 3 + timlen;
3377 tie->tim_bitctl = timoff;
3380 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3383 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3385 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3386 "%s: TIM updated, pending %u, off %u, len %u\n",
3387 __func__, vap->iv_ps_pending, timoff, timlen);
3389 /* count down DTIM period */
3390 if (tie->tim_count == 0)
3391 tie->tim_count = tie->tim_period - 1;
3394 /* update state for buffered multicast frames on DTIM */
3395 if (mcast && tie->tim_count == 0)
3396 tie->tim_bitctl |= 1;
3398 tie->tim_bitctl &= ~1;
3399 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3400 struct ieee80211_csa_ie *csa =
3401 (struct ieee80211_csa_ie *) bo->bo_csa;
3404 * Insert or update CSA ie. If we're just starting
3405 * to count down to the channel switch then we need
3406 * to insert the CSA ie. Otherwise we just need to
3407 * drop the count. The actual change happens above
3408 * when the vap's count reaches the target count.
3410 if (vap->iv_csa_count == 0) {
3411 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3412 bo->bo_erp += sizeof(*csa);
3413 bo->bo_htinfo += sizeof(*csa);
3414 bo->bo_wme += sizeof(*csa);
3415 #ifdef IEEE80211_SUPPORT_SUPERG
3416 bo->bo_ath += sizeof(*csa);
3418 #ifdef IEEE80211_SUPPORT_TDMA
3419 bo->bo_tdma += sizeof(*csa);
3421 #ifdef IEEE80211_SUPPORT_MESH
3422 bo->bo_meshconf += sizeof(*csa);
3424 bo->bo_appie += sizeof(*csa);
3425 bo->bo_csa_trailer_len += sizeof(*csa);
3426 bo->bo_quiet += sizeof(*csa);
3427 bo->bo_tim_trailer_len += sizeof(*csa);
3428 m->m_len += sizeof(*csa);
3429 m->m_pkthdr.len += sizeof(*csa);
3431 ieee80211_add_csa(bo->bo_csa, vap);
3434 vap->iv_csa_count++;
3435 /* NB: don't clear IEEE80211_BEACON_CSA */
3437 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3438 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3440 ieee80211_add_quiet(bo->bo_quiet, vap);
3442 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3444 * ERP element needs updating.
3446 (void) ieee80211_add_erp(bo->bo_erp, ic);
3447 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3449 #ifdef IEEE80211_SUPPORT_SUPERG
3450 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3451 ieee80211_add_athcaps(bo->bo_ath, ni);
3452 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3456 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3457 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3463 aielen += aie->ie_len;
3464 if (aielen != bo->bo_appie_len) {
3465 /* copy up/down trailer */
3466 int adjust = aielen - bo->bo_appie_len;
3467 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3468 bo->bo_tim_trailer_len);
3469 bo->bo_tim_trailer += adjust;
3470 bo->bo_appie += adjust;
3471 bo->bo_appie_len = aielen;
3477 frm = add_appie(frm, aie);
3478 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3480 IEEE80211_UNLOCK(ic);
3486 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3487 * tunnel encapsulation. The frame is assumed to have an Ethernet
3488 * header at the front that must be stripped before prepending the
3489 * LLC followed by the Ethernet header passed in (with an Ethernet
3490 * type that specifies the payload size).
3493 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3494 const struct ether_header *eh)
3499 /* XXX optimize by combining m_adj+M_PREPEND */
3500 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3501 llc = mtod(m, struct llc *);
3502 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3503 llc->llc_control = LLC_UI;
3504 llc->llc_snap.org_code[0] = 0;
3505 llc->llc_snap.org_code[1] = 0;
3506 llc->llc_snap.org_code[2] = 0;
3507 llc->llc_snap.ether_type = eh->ether_type;
3508 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3510 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3511 if (m == NULL) { /* XXX cannot happen */
3512 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3513 "%s: no space for ether_header\n", __func__);
3514 vap->iv_stats.is_tx_nobuf++;
3517 ETHER_HEADER_COPY(mtod(m, void *), eh);
3518 mtod(m, struct ether_header *)->ether_type = htons(payload);
3523 * Complete an mbuf transmission.
3525 * For now, this simply processes a completed frame after the
3526 * driver has completed it's transmission and/or retransmission.
3527 * It assumes the frame is an 802.11 encapsulated frame.
3529 * Later on it will grow to become the exit path for a given frame
3530 * from the driver and, depending upon how it's been encapsulated
3531 * and already transmitted, it may end up doing A-MPDU retransmission,
3532 * power save requeuing, etc.
3534 * In order for the above to work, the driver entry point to this
3535 * must not hold any driver locks. Thus, the driver needs to delay
3536 * any actual mbuf completion until it can release said locks.
3538 * This frees the mbuf and if the mbuf has a node reference,
3539 * the node reference will be freed.
3542 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3546 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3549 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
3550 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3551 if (m->m_flags & M_MCAST)
3552 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3554 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3555 if (m->m_flags & M_TXCB)
3556 ieee80211_process_callback(ni, m, status);
3557 ieee80211_free_node(ni);