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>
37 #include <sys/kernel.h>
38 #include <sys/endian.h>
40 #include <sys/socket.h>
43 #include <net/ethernet.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/if_vlan_var.h>
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <net80211/ieee80211_superg.h>
54 #ifdef IEEE80211_SUPPORT_TDMA
55 #include <net80211/ieee80211_tdma.h>
57 #include <net80211/ieee80211_wds.h>
58 #include <net80211/ieee80211_mesh.h>
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
73 #include <security/mac/mac_framework.h>
75 #define ETHER_HEADER_COPY(dst, src) \
76 memcpy(dst, src, sizeof(struct ether_header))
78 /* unalligned little endian access */
79 #define LE_WRITE_2(p, v) do { \
80 ((uint8_t *)(p))[0] = (v) & 0xff; \
81 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
83 #define LE_WRITE_4(p, v) do { \
84 ((uint8_t *)(p))[0] = (v) & 0xff; \
85 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
86 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
87 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
90 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
91 u_int hdrsize, u_int ciphdrsize, u_int mtu);
92 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
94 #ifdef IEEE80211_DEBUG
96 * Decide if an outbound management frame should be
97 * printed when debugging is enabled. This filters some
98 * of the less interesting frames that come frequently
102 doprint(struct ieee80211vap *vap, int subtype)
105 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
106 return (vap->iv_opmode == IEEE80211_M_IBSS);
113 * Transmit a frame to the given destination on the given VAP.
115 * It's up to the caller to figure out the details of who this
116 * is going to and resolving the node.
118 * This routine takes care of queuing it for power save,
119 * A-MPDU state stuff, fast-frames state stuff, encapsulation
120 * if required, then passing it up to the driver layer.
122 * This routine (for now) consumes the mbuf and frees the node
123 * reference; it ideally will return a TX status which reflects
124 * whether the mbuf was consumed or not, so the caller can
125 * free the mbuf (if appropriate) and the node reference (again,
129 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
130 struct ieee80211_node *ni)
132 struct ieee80211com *ic = vap->iv_ic;
133 struct ifnet *ifp = vap->iv_ifp;
136 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
137 (m->m_flags & M_PWR_SAV) == 0) {
139 * Station in power save mode; pass the frame
140 * to the 802.11 layer and continue. We'll get
141 * the frame back when the time is right.
142 * XXX lose WDS vap linkage?
144 (void) ieee80211_pwrsave(ni, m);
145 ieee80211_free_node(ni);
148 * We queued it fine, so tell the upper layer
149 * that we consumed it.
153 /* calculate priority so drivers can find the tx queue */
154 if (ieee80211_classify(ni, m)) {
155 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
156 ni->ni_macaddr, NULL,
157 "%s", "classification failure");
158 vap->iv_stats.is_tx_classify++;
161 ieee80211_free_node(ni);
163 /* XXX better status? */
167 * Stash the node pointer. Note that we do this after
168 * any call to ieee80211_dwds_mcast because that code
169 * uses any existing value for rcvif to identify the
170 * interface it (might have been) received on.
172 m->m_pkthdr.rcvif = (void *)ni;
174 BPF_MTAP(ifp, m); /* 802.3 tx */
177 * Check if A-MPDU tx aggregation is setup or if we
178 * should try to enable it. The sta must be associated
179 * with HT and A-MPDU enabled for use. When the policy
180 * routine decides we should enable A-MPDU we issue an
181 * ADDBA request and wait for a reply. The frame being
182 * encapsulated will go out w/o using A-MPDU, or possibly
183 * it might be collected by the driver and held/retransmit.
184 * The default ic_ampdu_enable routine handles staggering
185 * ADDBA requests in case the receiver NAK's us or we are
186 * otherwise unable to establish a BA stream.
188 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
189 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
190 (m->m_flags & M_EAPOL) == 0) {
191 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
192 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
194 ieee80211_txampdu_count_packet(tap);
195 if (IEEE80211_AMPDU_RUNNING(tap)) {
197 * Operational, mark frame for aggregation.
199 * XXX do tx aggregation here
201 m->m_flags |= M_AMPDU_MPDU;
202 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
203 ic->ic_ampdu_enable(ni, tap)) {
205 * Not negotiated yet, request service.
207 ieee80211_ampdu_request(ni, tap);
208 /* XXX hold frame for reply? */
212 #ifdef IEEE80211_SUPPORT_SUPERG
213 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
214 m = ieee80211_ff_check(ni, m);
216 /* NB: any ni ref held on stageq */
220 #endif /* IEEE80211_SUPPORT_SUPERG */
223 * Grab the TX lock - serialise the TX process from this
224 * point (where TX state is being checked/modified)
225 * through to driver queue.
227 IEEE80211_TX_LOCK(ic);
229 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
231 * Encapsulate the packet in prep for transmission.
233 m = ieee80211_encap(vap, ni, m);
235 /* NB: stat+msg handled in ieee80211_encap */
236 IEEE80211_TX_UNLOCK(ic);
237 ieee80211_free_node(ni);
238 /* XXX better status? */
242 error = ieee80211_parent_xmitpkt(ic, m);
245 * Unlock at this point - no need to hold it across
246 * ieee80211_free_node() (ie, the comlock)
248 IEEE80211_TX_UNLOCK(ic);
250 /* NB: IFQ_HANDOFF reclaims mbuf */
251 ieee80211_free_node(ni);
255 ic->ic_lastdata = ticks;
263 * Send the given mbuf through the given vap.
265 * This consumes the mbuf regardless of whether the transmit
266 * was successful or not.
268 * This does none of the initial checks that ieee80211_start()
269 * does (eg CAC timeout, interface wakeup) - the caller must
273 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
275 #define IS_DWDS(vap) \
276 (vap->iv_opmode == IEEE80211_M_WDS && \
277 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
278 struct ieee80211com *ic = vap->iv_ic;
279 struct ifnet *ifp = vap->iv_ifp;
280 struct ieee80211_node *ni;
281 struct ether_header *eh;
284 * Cancel any background scan.
286 if (ic->ic_flags & IEEE80211_F_SCAN)
287 ieee80211_cancel_anyscan(vap);
289 * Find the node for the destination so we can do
290 * things like power save and fast frames aggregation.
292 * NB: past this point various code assumes the first
293 * mbuf has the 802.3 header present (and contiguous).
296 if (m->m_len < sizeof(struct ether_header) &&
297 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
298 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
299 "discard frame, %s\n", "m_pullup failed");
300 vap->iv_stats.is_tx_nobuf++; /* XXX */
304 eh = mtod(m, struct ether_header *);
305 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
308 * Only unicast frames from the above go out
309 * DWDS vaps; multicast frames are handled by
310 * dispatching the frame as it comes through
311 * the AP vap (see below).
313 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
314 eh->ether_dhost, "mcast", "%s", "on DWDS");
315 vap->iv_stats.is_dwds_mcast++;
317 /* XXX better status? */
320 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
322 * Spam DWDS vap's w/ multicast traffic.
324 /* XXX only if dwds in use? */
325 ieee80211_dwds_mcast(vap, m);
328 #ifdef IEEE80211_SUPPORT_MESH
329 if (vap->iv_opmode != IEEE80211_M_MBSS) {
331 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
333 /* NB: ieee80211_find_txnode does stat+msg */
336 /* XXX better status? */
339 if (ni->ni_associd == 0 &&
340 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
341 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
342 eh->ether_dhost, NULL,
343 "sta not associated (type 0x%04x)",
344 htons(eh->ether_type));
345 vap->iv_stats.is_tx_notassoc++;
348 ieee80211_free_node(ni);
349 /* XXX better status? */
352 #ifdef IEEE80211_SUPPORT_MESH
354 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
356 * Proxy station only if configured.
358 if (!ieee80211_mesh_isproxyena(vap)) {
359 IEEE80211_DISCARD_MAC(vap,
360 IEEE80211_MSG_OUTPUT |
362 eh->ether_dhost, NULL,
363 "%s", "proxy not enabled");
364 vap->iv_stats.is_mesh_notproxy++;
367 /* XXX better status? */
370 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
371 "forward frame from DS SA(%6D), DA(%6D)\n",
372 eh->ether_shost, ":",
373 eh->ether_dhost, ":");
374 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
376 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
379 * NB: ieee80211_mesh_discover holds/disposes
380 * frame (e.g. queueing on path discovery).
383 /* XXX better status? */
390 * We've resolved the sender, so attempt to transmit it.
392 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
399 * Start method for vap's. All packets from the stack come
400 * through here. We handle common processing of the packets
401 * before dispatching them to the underlying device.
403 * if_transmit() requires that the mbuf be consumed by this call
404 * regardless of the return condition.
407 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
409 struct ieee80211vap *vap = ifp->if_softc;
410 struct ieee80211com *ic = vap->iv_ic;
411 struct ifnet *parent = ic->ic_ifp;
413 /* NB: parent must be up and running */
414 if (!IFNET_IS_UP_RUNNING(parent)) {
415 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
416 "%s: ignore queue, parent %s not up+running\n",
417 __func__, parent->if_xname);
422 if (vap->iv_state == IEEE80211_S_SLEEP) {
424 * In power save, wakeup device for transmit.
426 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
431 * No data frames go out unless we're running.
432 * Note in particular this covers CAC and CSA
433 * states (though maybe we should check muting
436 if (vap->iv_state != IEEE80211_S_RUN) {
438 /* re-check under the com lock to avoid races */
439 if (vap->iv_state != IEEE80211_S_RUN) {
440 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
441 "%s: ignore queue, in %s state\n",
442 __func__, ieee80211_state_name[vap->iv_state]);
443 vap->iv_stats.is_tx_badstate++;
444 IEEE80211_UNLOCK(ic);
445 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
449 IEEE80211_UNLOCK(ic);
453 * Sanitize mbuf flags for net80211 use. We cannot
454 * clear M_PWR_SAV or M_MORE_DATA because these may
455 * be set for frames that are re-submitted from the
458 * NB: This must be done before ieee80211_classify as
459 * it marks EAPOL in frames with M_EAPOL.
461 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
464 * Bump to the packet transmission path.
465 * The mbuf will be consumed here.
467 return (ieee80211_start_pkt(vap, m));
471 ieee80211_vap_qflush(struct ifnet *ifp)
478 * 802.11 raw output routine.
481 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
482 struct mbuf *m, const struct ieee80211_bpf_params *params)
484 struct ieee80211com *ic = vap->iv_ic;
486 return (ic->ic_raw_xmit(ni, m, params));
490 * 802.11 output routine. This is (currently) used only to
491 * connect bpf write calls to the 802.11 layer for injecting
494 #if __FreeBSD_version >= 1000031
496 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
497 const struct sockaddr *dst, struct route *ro)
500 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
501 struct sockaddr *dst, struct route *ro)
504 #define senderr(e) do { error = (e); goto bad;} while (0)
505 struct ieee80211_node *ni = NULL;
506 struct ieee80211vap *vap;
507 struct ieee80211_frame *wh;
508 struct ieee80211com *ic = NULL;
512 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
514 * Short-circuit requests if the vap is marked OACTIVE
515 * as this can happen because a packet came down through
516 * ieee80211_start before the vap entered RUN state in
517 * which case it's ok to just drop the frame. This
518 * should not be necessary but callers of if_output don't
526 * Hand to the 802.3 code if not tagged as
527 * a raw 802.11 frame.
529 if (dst->sa_family != AF_IEEE80211)
530 return vap->iv_output(ifp, m, dst, ro);
532 error = mac_ifnet_check_transmit(ifp, m);
536 if (ifp->if_flags & IFF_MONITOR)
538 if (!IFNET_IS_UP_RUNNING(ifp))
540 if (vap->iv_state == IEEE80211_S_CAC) {
541 IEEE80211_DPRINTF(vap,
542 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
543 "block %s frame in CAC state\n", "raw data");
544 vap->iv_stats.is_tx_badstate++;
545 senderr(EIO); /* XXX */
546 } else if (vap->iv_state == IEEE80211_S_SCAN)
548 /* XXX bypass bridge, pfil, carp, etc. */
550 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
551 senderr(EIO); /* XXX */
552 wh = mtod(m, struct ieee80211_frame *);
553 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
554 IEEE80211_FC0_VERSION_0)
555 senderr(EIO); /* XXX */
557 /* locate destination node */
558 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
559 case IEEE80211_FC1_DIR_NODS:
560 case IEEE80211_FC1_DIR_FROMDS:
561 ni = ieee80211_find_txnode(vap, wh->i_addr1);
563 case IEEE80211_FC1_DIR_TODS:
564 case IEEE80211_FC1_DIR_DSTODS:
565 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
566 senderr(EIO); /* XXX */
567 ni = ieee80211_find_txnode(vap, wh->i_addr3);
570 senderr(EIO); /* XXX */
574 * Permit packets w/ bpf params through regardless
575 * (see below about sa_len).
577 if (dst->sa_len == 0)
578 senderr(EHOSTUNREACH);
579 ni = ieee80211_ref_node(vap->iv_bss);
583 * Sanitize mbuf for net80211 flags leaked from above.
585 * NB: This must be done before ieee80211_classify as
586 * it marks EAPOL in frames with M_EAPOL.
588 m->m_flags &= ~M_80211_TX;
590 /* calculate priority so drivers can find the tx queue */
591 /* XXX assumes an 802.3 frame */
592 if (ieee80211_classify(ni, m))
593 senderr(EIO); /* XXX */
596 IEEE80211_NODE_STAT(ni, tx_data);
597 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
598 IEEE80211_NODE_STAT(ni, tx_mcast);
599 m->m_flags |= M_MCAST;
601 IEEE80211_NODE_STAT(ni, tx_ucast);
602 /* NB: ieee80211_encap does not include 802.11 header */
603 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
605 IEEE80211_TX_LOCK(ic);
608 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
609 * present by setting the sa_len field of the sockaddr (yes,
611 * NB: we assume sa_data is suitably aligned to cast.
613 ret = ieee80211_raw_output(vap, ni, m,
614 (const struct ieee80211_bpf_params *)(dst->sa_len ?
615 dst->sa_data : NULL));
616 IEEE80211_TX_UNLOCK(ic);
622 ieee80211_free_node(ni);
629 * Set the direction field and address fields of an outgoing
630 * frame. Note this should be called early on in constructing
631 * a frame as it sets i_fc[1]; other bits can then be or'd in.
634 ieee80211_send_setup(
635 struct ieee80211_node *ni,
638 const uint8_t sa[IEEE80211_ADDR_LEN],
639 const uint8_t da[IEEE80211_ADDR_LEN],
640 const uint8_t bssid[IEEE80211_ADDR_LEN])
642 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
643 struct ieee80211vap *vap = ni->ni_vap;
644 struct ieee80211_tx_ampdu *tap;
645 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
648 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
650 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
651 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
652 switch (vap->iv_opmode) {
653 case IEEE80211_M_STA:
654 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
655 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
656 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
657 IEEE80211_ADDR_COPY(wh->i_addr3, da);
659 case IEEE80211_M_IBSS:
660 case IEEE80211_M_AHDEMO:
661 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
662 IEEE80211_ADDR_COPY(wh->i_addr1, da);
663 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
664 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
666 case IEEE80211_M_HOSTAP:
667 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
668 IEEE80211_ADDR_COPY(wh->i_addr1, da);
669 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
670 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
672 case IEEE80211_M_WDS:
673 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
674 IEEE80211_ADDR_COPY(wh->i_addr1, da);
675 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
676 IEEE80211_ADDR_COPY(wh->i_addr3, da);
677 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
679 case IEEE80211_M_MBSS:
680 #ifdef IEEE80211_SUPPORT_MESH
681 if (IEEE80211_IS_MULTICAST(da)) {
682 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
684 IEEE80211_ADDR_COPY(wh->i_addr1, da);
685 IEEE80211_ADDR_COPY(wh->i_addr2,
688 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
689 IEEE80211_ADDR_COPY(wh->i_addr1, da);
690 IEEE80211_ADDR_COPY(wh->i_addr2,
692 IEEE80211_ADDR_COPY(wh->i_addr3, da);
693 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
697 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
701 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
702 IEEE80211_ADDR_COPY(wh->i_addr1, da);
703 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
704 #ifdef IEEE80211_SUPPORT_MESH
705 if (vap->iv_opmode == IEEE80211_M_MBSS)
706 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
709 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
711 *(uint16_t *)&wh->i_dur[0] = 0;
713 tap = &ni->ni_tx_ampdu[tid];
714 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
715 m->m_flags |= M_AMPDU_MPDU;
717 seqno = ni->ni_txseqs[tid]++;
718 *(uint16_t *)&wh->i_seq[0] =
719 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
720 M_SEQNO_SET(m, seqno);
723 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
724 m->m_flags |= M_MCAST;
729 * Send a management frame to the specified node. The node pointer
730 * must have a reference as the pointer will be passed to the driver
731 * and potentially held for a long time. If the frame is successfully
732 * dispatched to the driver, then it is responsible for freeing the
733 * reference (and potentially free'ing up any associated storage);
734 * otherwise deal with reclaiming any reference (on error).
737 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
738 struct ieee80211_bpf_params *params)
740 struct ieee80211vap *vap = ni->ni_vap;
741 struct ieee80211com *ic = ni->ni_ic;
742 struct ieee80211_frame *wh;
745 KASSERT(ni != NULL, ("null node"));
747 if (vap->iv_state == IEEE80211_S_CAC) {
748 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
749 ni, "block %s frame in CAC state",
750 ieee80211_mgt_subtype_name[
751 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
752 IEEE80211_FC0_SUBTYPE_SHIFT]);
753 vap->iv_stats.is_tx_badstate++;
754 ieee80211_free_node(ni);
756 return EIO; /* XXX */
759 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
761 ieee80211_free_node(ni);
765 IEEE80211_TX_LOCK(ic);
767 wh = mtod(m, struct ieee80211_frame *);
768 ieee80211_send_setup(ni, m,
769 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
770 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
771 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
772 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
773 "encrypting frame (%s)", __func__);
774 wh->i_fc[1] |= IEEE80211_FC1_WEP;
776 m->m_flags |= M_ENCAP; /* mark encapsulated */
778 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
779 M_WME_SETAC(m, params->ibp_pri);
781 #ifdef IEEE80211_DEBUG
782 /* avoid printing too many frames */
783 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
784 ieee80211_msg_dumppkts(vap)) {
785 printf("[%s] send %s on channel %u\n",
786 ether_sprintf(wh->i_addr1),
787 ieee80211_mgt_subtype_name[
788 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
789 IEEE80211_FC0_SUBTYPE_SHIFT],
790 ieee80211_chan2ieee(ic, ic->ic_curchan));
793 IEEE80211_NODE_STAT(ni, tx_mgmt);
795 ret = ieee80211_raw_output(vap, ni, m, params);
796 IEEE80211_TX_UNLOCK(ic);
801 * Send a null data frame to the specified node. If the station
802 * is setup for QoS then a QoS Null Data frame is constructed.
803 * If this is a WDS station then a 4-address frame is constructed.
805 * NB: the caller is assumed to have setup a node reference
806 * for use; this is necessary to deal with a race condition
807 * when probing for inactive stations. Like ieee80211_mgmt_output
808 * we must cleanup any node reference on error; however we
809 * can safely just unref it as we know it will never be the
810 * last reference to the node.
813 ieee80211_send_nulldata(struct ieee80211_node *ni)
815 struct ieee80211vap *vap = ni->ni_vap;
816 struct ieee80211com *ic = ni->ni_ic;
818 struct ieee80211_frame *wh;
823 if (vap->iv_state == IEEE80211_S_CAC) {
824 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
825 ni, "block %s frame in CAC state", "null data");
826 ieee80211_unref_node(&ni);
827 vap->iv_stats.is_tx_badstate++;
828 return EIO; /* XXX */
831 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
832 hdrlen = sizeof(struct ieee80211_qosframe);
834 hdrlen = sizeof(struct ieee80211_frame);
835 /* NB: only WDS vap's get 4-address frames */
836 if (vap->iv_opmode == IEEE80211_M_WDS)
837 hdrlen += IEEE80211_ADDR_LEN;
838 if (ic->ic_flags & IEEE80211_F_DATAPAD)
839 hdrlen = roundup(hdrlen, sizeof(uint32_t));
841 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
844 ieee80211_unref_node(&ni);
845 vap->iv_stats.is_tx_nobuf++;
848 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
849 ("leading space %zd", M_LEADINGSPACE(m)));
850 M_PREPEND(m, hdrlen, M_NOWAIT);
852 /* NB: cannot happen */
853 ieee80211_free_node(ni);
857 IEEE80211_TX_LOCK(ic);
859 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
860 if (ni->ni_flags & IEEE80211_NODE_QOS) {
861 const int tid = WME_AC_TO_TID(WME_AC_BE);
864 ieee80211_send_setup(ni, m,
865 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
866 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
868 if (vap->iv_opmode == IEEE80211_M_WDS)
869 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
871 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
872 qos[0] = tid & IEEE80211_QOS_TID;
873 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
874 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
877 ieee80211_send_setup(ni, m,
878 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
879 IEEE80211_NONQOS_TID,
880 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
882 if (vap->iv_opmode != IEEE80211_M_WDS) {
883 /* NB: power management bit is never sent by an AP */
884 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
885 vap->iv_opmode != IEEE80211_M_HOSTAP)
886 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
888 m->m_len = m->m_pkthdr.len = hdrlen;
889 m->m_flags |= M_ENCAP; /* mark encapsulated */
891 M_WME_SETAC(m, WME_AC_BE);
893 IEEE80211_NODE_STAT(ni, tx_data);
895 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
896 "send %snull data frame on channel %u, pwr mgt %s",
897 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
898 ieee80211_chan2ieee(ic, ic->ic_curchan),
899 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
901 ret = ieee80211_raw_output(vap, ni, m, NULL);
902 IEEE80211_TX_UNLOCK(ic);
907 * Assign priority to a frame based on any vlan tag assigned
908 * to the station and/or any Diffserv setting in an IP header.
909 * Finally, if an ACM policy is setup (in station mode) it's
913 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
915 const struct ether_header *eh = mtod(m, struct ether_header *);
916 int v_wme_ac, d_wme_ac, ac;
919 * Always promote PAE/EAPOL frames to high priority.
921 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
922 /* NB: mark so others don't need to check header */
923 m->m_flags |= M_EAPOL;
928 * Non-qos traffic goes to BE.
930 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
936 * If node has a vlan tag then all traffic
937 * to it must have a matching tag.
940 if (ni->ni_vlan != 0) {
941 if ((m->m_flags & M_VLANTAG) == 0) {
942 IEEE80211_NODE_STAT(ni, tx_novlantag);
945 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
946 EVL_VLANOFTAG(ni->ni_vlan)) {
947 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
950 /* map vlan priority to AC */
951 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
954 /* XXX m_copydata may be too slow for fast path */
956 if (eh->ether_type == htons(ETHERTYPE_IP)) {
959 * IP frame, map the DSCP bits from the TOS field.
961 /* NB: ip header may not be in first mbuf */
962 m_copydata(m, sizeof(struct ether_header) +
963 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
964 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
965 d_wme_ac = TID_TO_WME_AC(tos);
969 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
973 * IPv6 frame, map the DSCP bits from the traffic class field.
975 m_copydata(m, sizeof(struct ether_header) +
976 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
978 tos = (uint8_t)(ntohl(flow) >> 20);
979 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
980 d_wme_ac = TID_TO_WME_AC(tos);
983 d_wme_ac = WME_AC_BE;
991 * Use highest priority AC.
993 if (v_wme_ac > d_wme_ac)
1001 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1002 static const int acmap[4] = {
1003 WME_AC_BK, /* WME_AC_BE */
1004 WME_AC_BK, /* WME_AC_BK */
1005 WME_AC_BE, /* WME_AC_VI */
1006 WME_AC_VI, /* WME_AC_VO */
1008 struct ieee80211com *ic = ni->ni_ic;
1010 while (ac != WME_AC_BK &&
1011 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1020 * Insure there is sufficient contiguous space to encapsulate the
1021 * 802.11 data frame. If room isn't already there, arrange for it.
1022 * Drivers and cipher modules assume we have done the necessary work
1023 * and fail rudely if they don't find the space they need.
1026 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1027 struct ieee80211_key *key, struct mbuf *m)
1029 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1030 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1033 /* XXX belongs in crypto code? */
1034 needed_space += key->wk_cipher->ic_header;
1037 * When crypto is being done in the host we must insure
1038 * the data are writable for the cipher routines; clone
1039 * a writable mbuf chain.
1040 * XXX handle SWMIC specially
1042 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1043 m = m_unshare(m, M_NOWAIT);
1045 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1046 "%s: cannot get writable mbuf\n", __func__);
1047 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1053 * We know we are called just before stripping an Ethernet
1054 * header and prepending an LLC header. This means we know
1056 * sizeof(struct ether_header) - sizeof(struct llc)
1057 * bytes recovered to which we need additional space for the
1058 * 802.11 header and any crypto header.
1060 /* XXX check trailing space and copy instead? */
1061 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1062 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1064 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1065 "%s: cannot expand storage\n", __func__);
1066 vap->iv_stats.is_tx_nobuf++;
1070 KASSERT(needed_space <= MHLEN,
1071 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1073 * Setup new mbuf to have leading space to prepend the
1074 * 802.11 header and any crypto header bits that are
1075 * required (the latter are added when the driver calls
1076 * back to ieee80211_crypto_encap to do crypto encapsulation).
1078 /* NB: must be first 'cuz it clobbers m_data */
1079 m_move_pkthdr(n, m);
1080 n->m_len = 0; /* NB: m_gethdr does not set */
1081 n->m_data += needed_space;
1083 * Pull up Ethernet header to create the expected layout.
1084 * We could use m_pullup but that's overkill (i.e. we don't
1085 * need the actual data) and it cannot fail so do it inline
1088 /* NB: struct ether_header is known to be contiguous */
1089 n->m_len += sizeof(struct ether_header);
1090 m->m_len -= sizeof(struct ether_header);
1091 m->m_data += sizeof(struct ether_header);
1093 * Replace the head of the chain.
1099 #undef TO_BE_RECLAIMED
1103 * Return the transmit key to use in sending a unicast frame.
1104 * If a unicast key is set we use that. When no unicast key is set
1105 * we fall back to the default transmit key.
1107 static __inline struct ieee80211_key *
1108 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1109 struct ieee80211_node *ni)
1111 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1112 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1113 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1115 return &vap->iv_nw_keys[vap->iv_def_txkey];
1117 return &ni->ni_ucastkey;
1122 * Return the transmit key to use in sending a multicast frame.
1123 * Multicast traffic always uses the group key which is installed as
1124 * the default tx key.
1126 static __inline struct ieee80211_key *
1127 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1128 struct ieee80211_node *ni)
1130 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1131 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1133 return &vap->iv_nw_keys[vap->iv_def_txkey];
1137 * Encapsulate an outbound data frame. The mbuf chain is updated.
1138 * If an error is encountered NULL is returned. The caller is required
1139 * to provide a node reference and pullup the ethernet header in the
1142 * NB: Packet is assumed to be processed by ieee80211_classify which
1143 * marked EAPOL frames w/ M_EAPOL.
1146 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1149 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1150 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1151 struct ieee80211com *ic = ni->ni_ic;
1152 #ifdef IEEE80211_SUPPORT_MESH
1153 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1154 struct ieee80211_meshcntl_ae10 *mc;
1155 struct ieee80211_mesh_route *rt = NULL;
1158 struct ether_header eh;
1159 struct ieee80211_frame *wh;
1160 struct ieee80211_key *key;
1162 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1163 ieee80211_seq seqno;
1164 int meshhdrsize, meshae;
1167 IEEE80211_TX_LOCK_ASSERT(ic);
1170 * Copy existing Ethernet header to a safe place. The
1171 * rest of the code assumes it's ok to strip it when
1172 * reorganizing state for the final encapsulation.
1174 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1175 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1178 * Insure space for additional headers. First identify
1179 * transmit key to use in calculating any buffer adjustments
1180 * required. This is also used below to do privacy
1181 * encapsulation work. Then calculate the 802.11 header
1182 * size and any padding required by the driver.
1184 * Note key may be NULL if we fall back to the default
1185 * transmit key and that is not set. In that case the
1186 * buffer may not be expanded as needed by the cipher
1187 * routines, but they will/should discard it.
1189 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1190 if (vap->iv_opmode == IEEE80211_M_STA ||
1191 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1192 (vap->iv_opmode == IEEE80211_M_WDS &&
1193 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1194 key = ieee80211_crypto_getucastkey(vap, ni);
1196 key = ieee80211_crypto_getmcastkey(vap, ni);
1197 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1198 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1200 "no default transmit key (%s) deftxkey %u",
1201 __func__, vap->iv_def_txkey);
1202 vap->iv_stats.is_tx_nodefkey++;
1208 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1209 * frames so suppress use. This may be an issue if other
1210 * ap's require all data frames to be QoS-encapsulated
1211 * once negotiated in which case we'll need to make this
1213 * NB: mesh data frames are QoS.
1215 addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1216 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1217 (m->m_flags & M_EAPOL) == 0;
1219 hdrsize = sizeof(struct ieee80211_qosframe);
1221 hdrsize = sizeof(struct ieee80211_frame);
1222 #ifdef IEEE80211_SUPPORT_MESH
1223 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1225 * Mesh data frames are encapsulated according to the
1226 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1227 * o Group Addressed data (aka multicast) originating
1228 * at the local sta are sent w/ 3-address format and
1229 * address extension mode 00
1230 * o Individually Addressed data (aka unicast) originating
1231 * at the local sta are sent w/ 4-address format and
1232 * address extension mode 00
1233 * o Group Addressed data forwarded from a non-mesh sta are
1234 * sent w/ 3-address format and address extension mode 01
1235 * o Individually Address data from another sta are sent
1236 * w/ 4-address format and address extension mode 10
1238 is4addr = 0; /* NB: don't use, disable */
1239 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1240 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1241 KASSERT(rt != NULL, ("route is NULL"));
1242 dir = IEEE80211_FC1_DIR_DSTODS;
1243 hdrsize += IEEE80211_ADDR_LEN;
1244 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1245 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1247 IEEE80211_NOTE_MAC(vap,
1250 "%s", "trying to send to ourself");
1253 meshae = IEEE80211_MESH_AE_10;
1255 sizeof(struct ieee80211_meshcntl_ae10);
1257 meshae = IEEE80211_MESH_AE_00;
1259 sizeof(struct ieee80211_meshcntl);
1262 dir = IEEE80211_FC1_DIR_FROMDS;
1263 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1265 meshae = IEEE80211_MESH_AE_01;
1267 sizeof(struct ieee80211_meshcntl_ae01);
1270 meshae = IEEE80211_MESH_AE_00;
1271 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1277 * 4-address frames need to be generated for:
1278 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1279 * o packets sent through a vap marked for relaying
1280 * (e.g. a station operating with dynamic WDS)
1282 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1283 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1284 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1286 hdrsize += IEEE80211_ADDR_LEN;
1287 meshhdrsize = meshae = 0;
1288 #ifdef IEEE80211_SUPPORT_MESH
1292 * Honor driver DATAPAD requirement.
1294 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1295 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1299 if (__predict_true((m->m_flags & M_FF) == 0)) {
1303 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1305 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1308 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1309 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1310 llc = mtod(m, struct llc *);
1311 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1312 llc->llc_control = LLC_UI;
1313 llc->llc_snap.org_code[0] = 0;
1314 llc->llc_snap.org_code[1] = 0;
1315 llc->llc_snap.org_code[2] = 0;
1316 llc->llc_snap.ether_type = eh.ether_type;
1318 #ifdef IEEE80211_SUPPORT_SUPERG
1322 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1327 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1329 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1331 vap->iv_stats.is_tx_nobuf++;
1334 wh = mtod(m, struct ieee80211_frame *);
1335 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1336 *(uint16_t *)wh->i_dur = 0;
1337 qos = NULL; /* NB: quiet compiler */
1339 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1340 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1341 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1342 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1343 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1344 } else switch (vap->iv_opmode) {
1345 case IEEE80211_M_STA:
1346 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1347 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1348 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1349 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1351 case IEEE80211_M_IBSS:
1352 case IEEE80211_M_AHDEMO:
1353 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1354 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1355 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1357 * NB: always use the bssid from iv_bss as the
1358 * neighbor's may be stale after an ibss merge
1360 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1362 case IEEE80211_M_HOSTAP:
1363 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1364 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1365 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1366 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1368 #ifdef IEEE80211_SUPPORT_MESH
1369 case IEEE80211_M_MBSS:
1370 /* NB: offset by hdrspace to deal with DATAPAD */
1371 mc = (struct ieee80211_meshcntl_ae10 *)
1372 (mtod(m, uint8_t *) + hdrspace);
1375 case IEEE80211_MESH_AE_00: /* no proxy */
1377 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1378 IEEE80211_ADDR_COPY(wh->i_addr1,
1380 IEEE80211_ADDR_COPY(wh->i_addr2,
1382 IEEE80211_ADDR_COPY(wh->i_addr3,
1384 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1386 qos =((struct ieee80211_qosframe_addr4 *)
1388 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1390 IEEE80211_ADDR_COPY(wh->i_addr1,
1392 IEEE80211_ADDR_COPY(wh->i_addr2,
1394 IEEE80211_ADDR_COPY(wh->i_addr3,
1396 qos = ((struct ieee80211_qosframe *)
1400 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1401 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1402 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1403 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1404 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1406 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1408 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1410 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1411 KASSERT(rt != NULL, ("route is NULL"));
1412 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1413 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1414 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1415 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1416 mc->mc_flags = IEEE80211_MESH_AE_10;
1417 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1418 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1419 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1422 KASSERT(0, ("meshae %d", meshae));
1425 mc->mc_ttl = ms->ms_ttl;
1427 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1430 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1434 if (m->m_flags & M_MORE_DATA)
1435 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1440 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1441 /* NB: mesh case handled earlier */
1442 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1443 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1444 ac = M_WME_GETAC(m);
1445 /* map from access class/queue to 11e header priorty value */
1446 tid = WME_AC_TO_TID(ac);
1447 qos[0] = tid & IEEE80211_QOS_TID;
1448 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1449 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1450 #ifdef IEEE80211_SUPPORT_MESH
1451 if (vap->iv_opmode == IEEE80211_M_MBSS)
1452 qos[1] = IEEE80211_QOS_MC;
1456 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1458 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1460 * NB: don't assign a sequence # to potential
1461 * aggregates; we expect this happens at the
1462 * point the frame comes off any aggregation q
1463 * as otherwise we may introduce holes in the
1464 * BA sequence space and/or make window accouting
1467 * XXX may want to control this with a driver
1468 * capability; this may also change when we pull
1469 * aggregation up into net80211
1471 seqno = ni->ni_txseqs[tid]++;
1472 *(uint16_t *)wh->i_seq =
1473 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1474 M_SEQNO_SET(m, seqno);
1477 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1478 *(uint16_t *)wh->i_seq =
1479 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1480 M_SEQNO_SET(m, seqno);
1484 /* check if xmit fragmentation is required */
1485 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1486 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1487 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1488 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1491 * IEEE 802.1X: send EAPOL frames always in the clear.
1492 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1494 if ((m->m_flags & M_EAPOL) == 0 ||
1495 ((vap->iv_flags & IEEE80211_F_WPA) &&
1496 (vap->iv_opmode == IEEE80211_M_STA ?
1497 !IEEE80211_KEY_UNDEFINED(key) :
1498 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1499 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1500 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1501 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1503 "%s", "enmic failed, discard frame");
1504 vap->iv_stats.is_crypto_enmicfail++;
1509 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1510 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1513 m->m_flags |= M_ENCAP; /* mark encapsulated */
1515 IEEE80211_NODE_STAT(ni, tx_data);
1516 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1517 IEEE80211_NODE_STAT(ni, tx_mcast);
1518 m->m_flags |= M_MCAST;
1520 IEEE80211_NODE_STAT(ni, tx_ucast);
1521 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1533 * Fragment the frame according to the specified mtu.
1534 * The size of the 802.11 header (w/o padding) is provided
1535 * so we don't need to recalculate it. We create a new
1536 * mbuf for each fragment and chain it through m_nextpkt;
1537 * we might be able to optimize this by reusing the original
1538 * packet's mbufs but that is significantly more complicated.
1541 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1542 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1544 struct ieee80211com *ic = vap->iv_ic;
1545 struct ieee80211_frame *wh, *whf;
1546 struct mbuf *m, *prev, *next;
1547 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1550 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1551 KASSERT(m0->m_pkthdr.len > mtu,
1552 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1555 * Honor driver DATAPAD requirement.
1557 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1558 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1562 wh = mtod(m0, struct ieee80211_frame *);
1563 /* NB: mark the first frag; it will be propagated below */
1564 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1565 totalhdrsize = hdrspace + ciphdrsize;
1567 off = mtu - ciphdrsize;
1568 remainder = m0->m_pkthdr.len - off;
1571 fragsize = totalhdrsize + remainder;
1574 /* XXX fragsize can be >2048! */
1575 KASSERT(fragsize < MCLBYTES,
1576 ("fragment size %u too big!", fragsize));
1577 if (fragsize > MHLEN)
1578 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1580 m = m_gethdr(M_NOWAIT, MT_DATA);
1583 /* leave room to prepend any cipher header */
1584 m_align(m, fragsize - ciphdrsize);
1587 * Form the header in the fragment. Note that since
1588 * we mark the first fragment with the MORE_FRAG bit
1589 * it automatically is propagated to each fragment; we
1590 * need only clear it on the last fragment (done below).
1591 * NB: frag 1+ dont have Mesh Control field present.
1593 whf = mtod(m, struct ieee80211_frame *);
1594 memcpy(whf, wh, hdrsize);
1595 #ifdef IEEE80211_SUPPORT_MESH
1596 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1597 if (IEEE80211_IS_DSTODS(wh))
1598 ((struct ieee80211_qosframe_addr4 *)
1599 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1601 ((struct ieee80211_qosframe *)
1602 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1605 *(uint16_t *)&whf->i_seq[0] |= htole16(
1606 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1607 IEEE80211_SEQ_FRAG_SHIFT);
1610 payload = fragsize - totalhdrsize;
1611 /* NB: destination is known to be contiguous */
1613 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1614 m->m_len = hdrspace + payload;
1615 m->m_pkthdr.len = hdrspace + payload;
1616 m->m_flags |= M_FRAG;
1618 /* chain up the fragment */
1619 prev->m_nextpkt = m;
1622 /* deduct fragment just formed */
1623 remainder -= payload;
1625 } while (remainder != 0);
1627 /* set the last fragment */
1628 m->m_flags |= M_LASTFRAG;
1629 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1631 /* strip first mbuf now that everything has been copied */
1632 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1633 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1635 vap->iv_stats.is_tx_fragframes++;
1636 vap->iv_stats.is_tx_frags += fragno-1;
1640 /* reclaim fragments but leave original frame for caller to free */
1641 for (m = m0->m_nextpkt; m != NULL; m = next) {
1642 next = m->m_nextpkt;
1643 m->m_nextpkt = NULL; /* XXX paranoid */
1646 m0->m_nextpkt = NULL;
1651 * Add a supported rates element id to a frame.
1654 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1658 *frm++ = IEEE80211_ELEMID_RATES;
1659 nrates = rs->rs_nrates;
1660 if (nrates > IEEE80211_RATE_SIZE)
1661 nrates = IEEE80211_RATE_SIZE;
1663 memcpy(frm, rs->rs_rates, nrates);
1664 return frm + nrates;
1668 * Add an extended supported rates element id to a frame.
1671 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1674 * Add an extended supported rates element if operating in 11g mode.
1676 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1677 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1678 *frm++ = IEEE80211_ELEMID_XRATES;
1680 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1687 * Add an ssid element to a frame.
1690 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1692 *frm++ = IEEE80211_ELEMID_SSID;
1694 memcpy(frm, ssid, len);
1699 * Add an erp element to a frame.
1702 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1706 *frm++ = IEEE80211_ELEMID_ERP;
1709 if (ic->ic_nonerpsta != 0)
1710 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1711 if (ic->ic_flags & IEEE80211_F_USEPROT)
1712 erp |= IEEE80211_ERP_USE_PROTECTION;
1713 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1714 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1720 * Add a CFParams element to a frame.
1723 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1725 #define ADDSHORT(frm, v) do { \
1726 LE_WRITE_2(frm, v); \
1729 *frm++ = IEEE80211_ELEMID_CFPARMS;
1731 *frm++ = 0; /* CFP count */
1732 *frm++ = 2; /* CFP period */
1733 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1734 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1739 static __inline uint8_t *
1740 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1742 memcpy(frm, ie->ie_data, ie->ie_len);
1743 return frm + ie->ie_len;
1746 static __inline uint8_t *
1747 add_ie(uint8_t *frm, const uint8_t *ie)
1749 memcpy(frm, ie, 2 + ie[1]);
1750 return frm + 2 + ie[1];
1753 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1755 * Add a WME information element to a frame.
1758 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1760 static const struct ieee80211_wme_info info = {
1761 .wme_id = IEEE80211_ELEMID_VENDOR,
1762 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1763 .wme_oui = { WME_OUI_BYTES },
1764 .wme_type = WME_OUI_TYPE,
1765 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1766 .wme_version = WME_VERSION,
1769 memcpy(frm, &info, sizeof(info));
1770 return frm + sizeof(info);
1774 * Add a WME parameters element to a frame.
1777 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1779 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1780 #define ADDSHORT(frm, v) do { \
1781 LE_WRITE_2(frm, v); \
1784 /* NB: this works 'cuz a param has an info at the front */
1785 static const struct ieee80211_wme_info param = {
1786 .wme_id = IEEE80211_ELEMID_VENDOR,
1787 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1788 .wme_oui = { WME_OUI_BYTES },
1789 .wme_type = WME_OUI_TYPE,
1790 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1791 .wme_version = WME_VERSION,
1795 memcpy(frm, ¶m, sizeof(param));
1796 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1797 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1798 *frm++ = 0; /* reserved field */
1799 for (i = 0; i < WME_NUM_AC; i++) {
1800 const struct wmeParams *ac =
1801 &wme->wme_bssChanParams.cap_wmeParams[i];
1802 *frm++ = SM(i, WME_PARAM_ACI)
1803 | SM(ac->wmep_acm, WME_PARAM_ACM)
1804 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1806 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1807 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1809 ADDSHORT(frm, ac->wmep_txopLimit);
1815 #undef WME_OUI_BYTES
1818 * Add an 11h Power Constraint element to a frame.
1821 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1823 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1824 /* XXX per-vap tx power limit? */
1825 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1827 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1829 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1834 * Add an 11h Power Capability element to a frame.
1837 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1839 frm[0] = IEEE80211_ELEMID_PWRCAP;
1841 frm[2] = c->ic_minpower;
1842 frm[3] = c->ic_maxpower;
1847 * Add an 11h Supported Channels element to a frame.
1850 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1852 static const int ielen = 26;
1854 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1856 /* XXX not correct */
1857 memcpy(frm+2, ic->ic_chan_avail, ielen);
1858 return frm + 2 + ielen;
1862 * Add an 11h Quiet time element to a frame.
1865 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1867 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1869 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1871 if (vap->iv_quiet_count_value == 1)
1872 vap->iv_quiet_count_value = vap->iv_quiet_count;
1873 else if (vap->iv_quiet_count_value > 1)
1874 vap->iv_quiet_count_value--;
1876 if (vap->iv_quiet_count_value == 0) {
1877 /* value 0 is reserved as per 802.11h standerd */
1878 vap->iv_quiet_count_value = 1;
1881 quiet->tbttcount = vap->iv_quiet_count_value;
1882 quiet->period = vap->iv_quiet_period;
1883 quiet->duration = htole16(vap->iv_quiet_duration);
1884 quiet->offset = htole16(vap->iv_quiet_offset);
1885 return frm + sizeof(*quiet);
1889 * Add an 11h Channel Switch Announcement element to a frame.
1890 * Note that we use the per-vap CSA count to adjust the global
1891 * counter so we can use this routine to form probe response
1892 * frames and get the current count.
1895 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1897 struct ieee80211com *ic = vap->iv_ic;
1898 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1900 csa->csa_ie = IEEE80211_ELEMID_CSA;
1902 csa->csa_mode = 1; /* XXX force quiet on channel */
1903 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1904 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1905 return frm + sizeof(*csa);
1909 * Add an 11h country information element to a frame.
1912 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1915 if (ic->ic_countryie == NULL ||
1916 ic->ic_countryie_chan != ic->ic_bsschan) {
1918 * Handle lazy construction of ie. This is done on
1919 * first use and after a channel change that requires
1922 if (ic->ic_countryie != NULL)
1923 free(ic->ic_countryie, M_80211_NODE_IE);
1924 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1925 if (ic->ic_countryie == NULL)
1927 ic->ic_countryie_chan = ic->ic_bsschan;
1929 return add_appie(frm, ic->ic_countryie);
1933 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
1935 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
1936 return (add_ie(frm, vap->iv_wpa_ie));
1938 /* XXX else complain? */
1944 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
1946 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
1947 return (add_ie(frm, vap->iv_rsn_ie));
1949 /* XXX else complain? */
1955 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
1957 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1958 *frm++ = IEEE80211_ELEMID_QOS;
1967 * Send a probe request frame with the specified ssid
1968 * and any optional information element data.
1971 ieee80211_send_probereq(struct ieee80211_node *ni,
1972 const uint8_t sa[IEEE80211_ADDR_LEN],
1973 const uint8_t da[IEEE80211_ADDR_LEN],
1974 const uint8_t bssid[IEEE80211_ADDR_LEN],
1975 const uint8_t *ssid, size_t ssidlen)
1977 struct ieee80211vap *vap = ni->ni_vap;
1978 struct ieee80211com *ic = ni->ni_ic;
1979 const struct ieee80211_txparam *tp;
1980 struct ieee80211_bpf_params params;
1981 struct ieee80211_frame *wh;
1982 const struct ieee80211_rateset *rs;
1987 if (vap->iv_state == IEEE80211_S_CAC) {
1988 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1989 "block %s frame in CAC state", "probe request");
1990 vap->iv_stats.is_tx_badstate++;
1991 return EIO; /* XXX */
1995 * Hold a reference on the node so it doesn't go away until after
1996 * the xmit is complete all the way in the driver. On error we
1997 * will remove our reference.
1999 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2000 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2002 ni, ether_sprintf(ni->ni_macaddr),
2003 ieee80211_node_refcnt(ni)+1);
2004 ieee80211_ref_node(ni);
2007 * prreq frame format
2009 * [tlv] supported rates
2010 * [tlv] RSN (optional)
2011 * [tlv] extended supported rates
2012 * [tlv] WPA (optional)
2013 * [tlv] user-specified ie's
2015 m = ieee80211_getmgtframe(&frm,
2016 ic->ic_headroom + sizeof(struct ieee80211_frame),
2017 2 + IEEE80211_NWID_LEN
2018 + 2 + IEEE80211_RATE_SIZE
2019 + sizeof(struct ieee80211_ie_wpa)
2020 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2021 + sizeof(struct ieee80211_ie_wpa)
2022 + (vap->iv_appie_probereq != NULL ?
2023 vap->iv_appie_probereq->ie_len : 0)
2026 vap->iv_stats.is_tx_nobuf++;
2027 ieee80211_free_node(ni);
2031 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2032 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2033 frm = ieee80211_add_rates(frm, rs);
2034 frm = ieee80211_add_rsn(frm, vap);
2035 frm = ieee80211_add_xrates(frm, rs);
2036 frm = ieee80211_add_wpa(frm, vap);
2037 if (vap->iv_appie_probereq != NULL)
2038 frm = add_appie(frm, vap->iv_appie_probereq);
2039 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2041 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2042 ("leading space %zd", M_LEADINGSPACE(m)));
2043 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2045 /* NB: cannot happen */
2046 ieee80211_free_node(ni);
2050 IEEE80211_TX_LOCK(ic);
2051 wh = mtod(m, struct ieee80211_frame *);
2052 ieee80211_send_setup(ni, m,
2053 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2054 IEEE80211_NONQOS_TID, sa, da, bssid);
2055 /* XXX power management? */
2056 m->m_flags |= M_ENCAP; /* mark encapsulated */
2058 M_WME_SETAC(m, WME_AC_BE);
2060 IEEE80211_NODE_STAT(ni, tx_probereq);
2061 IEEE80211_NODE_STAT(ni, tx_mgmt);
2063 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2064 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2065 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2068 memset(¶ms, 0, sizeof(params));
2069 params.ibp_pri = M_WME_GETAC(m);
2070 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2071 params.ibp_rate0 = tp->mgmtrate;
2072 if (IEEE80211_IS_MULTICAST(da)) {
2073 params.ibp_flags |= IEEE80211_BPF_NOACK;
2074 params.ibp_try0 = 1;
2076 params.ibp_try0 = tp->maxretry;
2077 params.ibp_power = ni->ni_txpower;
2078 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2079 IEEE80211_TX_UNLOCK(ic);
2084 * Calculate capability information for mgt frames.
2087 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2089 struct ieee80211com *ic = vap->iv_ic;
2092 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2094 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2095 capinfo = IEEE80211_CAPINFO_ESS;
2096 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2097 capinfo = IEEE80211_CAPINFO_IBSS;
2100 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2101 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2102 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2103 IEEE80211_IS_CHAN_2GHZ(chan))
2104 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2105 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2106 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2107 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2108 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2113 * Send a management frame. The node is for the destination (or ic_bss
2114 * when in station mode). Nodes other than ic_bss have their reference
2115 * count bumped to reflect our use for an indeterminant time.
2118 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2120 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2121 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2122 struct ieee80211vap *vap = ni->ni_vap;
2123 struct ieee80211com *ic = ni->ni_ic;
2124 struct ieee80211_node *bss = vap->iv_bss;
2125 struct ieee80211_bpf_params params;
2129 int has_challenge, is_shared_key, ret, status;
2131 KASSERT(ni != NULL, ("null node"));
2134 * Hold a reference on the node so it doesn't go away until after
2135 * the xmit is complete all the way in the driver. On error we
2136 * will remove our reference.
2138 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2139 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2141 ni, ether_sprintf(ni->ni_macaddr),
2142 ieee80211_node_refcnt(ni)+1);
2143 ieee80211_ref_node(ni);
2145 memset(¶ms, 0, sizeof(params));
2148 case IEEE80211_FC0_SUBTYPE_AUTH:
2151 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2152 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2153 ni->ni_challenge != NULL);
2156 * Deduce whether we're doing open authentication or
2157 * shared key authentication. We do the latter if
2158 * we're in the middle of a shared key authentication
2159 * handshake or if we're initiating an authentication
2160 * request and configured to use shared key.
2162 is_shared_key = has_challenge ||
2163 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2164 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2165 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2167 m = ieee80211_getmgtframe(&frm,
2168 ic->ic_headroom + sizeof(struct ieee80211_frame),
2169 3 * sizeof(uint16_t)
2170 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2171 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2174 senderr(ENOMEM, is_tx_nobuf);
2176 ((uint16_t *)frm)[0] =
2177 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2178 : htole16(IEEE80211_AUTH_ALG_OPEN);
2179 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2180 ((uint16_t *)frm)[2] = htole16(status);/* status */
2182 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2183 ((uint16_t *)frm)[3] =
2184 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2185 IEEE80211_ELEMID_CHALLENGE);
2186 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2187 IEEE80211_CHALLENGE_LEN);
2188 m->m_pkthdr.len = m->m_len =
2189 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2190 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2191 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2192 "request encrypt frame (%s)", __func__);
2193 /* mark frame for encryption */
2194 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2197 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2199 /* XXX not right for shared key */
2200 if (status == IEEE80211_STATUS_SUCCESS)
2201 IEEE80211_NODE_STAT(ni, tx_auth);
2203 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2205 if (vap->iv_opmode == IEEE80211_M_STA)
2206 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2207 (void *) vap->iv_state);
2210 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2211 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2212 "send station deauthenticate (reason %d)", arg);
2213 m = ieee80211_getmgtframe(&frm,
2214 ic->ic_headroom + sizeof(struct ieee80211_frame),
2217 senderr(ENOMEM, is_tx_nobuf);
2218 *(uint16_t *)frm = htole16(arg); /* reason */
2219 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2221 IEEE80211_NODE_STAT(ni, tx_deauth);
2222 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2224 ieee80211_node_unauthorize(ni); /* port closed */
2227 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2228 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2230 * asreq frame format
2231 * [2] capability information
2232 * [2] listen interval
2233 * [6*] current AP address (reassoc only)
2235 * [tlv] supported rates
2236 * [tlv] extended supported rates
2237 * [4] power capability (optional)
2238 * [28] supported channels (optional)
2239 * [tlv] HT capabilities
2240 * [tlv] WME (optional)
2241 * [tlv] Vendor OUI HT capabilities (optional)
2242 * [tlv] Atheros capabilities (if negotiated)
2243 * [tlv] AppIE's (optional)
2245 m = ieee80211_getmgtframe(&frm,
2246 ic->ic_headroom + sizeof(struct ieee80211_frame),
2249 + IEEE80211_ADDR_LEN
2250 + 2 + IEEE80211_NWID_LEN
2251 + 2 + IEEE80211_RATE_SIZE
2252 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2255 + sizeof(struct ieee80211_wme_info)
2256 + sizeof(struct ieee80211_ie_htcap)
2257 + 4 + sizeof(struct ieee80211_ie_htcap)
2258 #ifdef IEEE80211_SUPPORT_SUPERG
2259 + sizeof(struct ieee80211_ath_ie)
2261 + (vap->iv_appie_wpa != NULL ?
2262 vap->iv_appie_wpa->ie_len : 0)
2263 + (vap->iv_appie_assocreq != NULL ?
2264 vap->iv_appie_assocreq->ie_len : 0)
2267 senderr(ENOMEM, is_tx_nobuf);
2269 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2270 ("wrong mode %u", vap->iv_opmode));
2271 capinfo = IEEE80211_CAPINFO_ESS;
2272 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2273 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2275 * NB: Some 11a AP's reject the request when
2276 * short premable is set.
2278 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2279 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2280 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2281 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2282 (ic->ic_caps & IEEE80211_C_SHSLOT))
2283 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2284 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2285 (vap->iv_flags & IEEE80211_F_DOTH))
2286 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2287 *(uint16_t *)frm = htole16(capinfo);
2290 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2291 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2295 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2296 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2297 frm += IEEE80211_ADDR_LEN;
2300 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2301 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2302 frm = ieee80211_add_rsn(frm, vap);
2303 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2304 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2305 frm = ieee80211_add_powercapability(frm,
2307 frm = ieee80211_add_supportedchannels(frm, ic);
2309 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2310 ni->ni_ies.htcap_ie != NULL &&
2311 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2312 frm = ieee80211_add_htcap(frm, ni);
2313 frm = ieee80211_add_wpa(frm, vap);
2314 if ((ic->ic_flags & IEEE80211_F_WME) &&
2315 ni->ni_ies.wme_ie != NULL)
2316 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2317 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2318 ni->ni_ies.htcap_ie != NULL &&
2319 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2320 frm = ieee80211_add_htcap_vendor(frm, ni);
2321 #ifdef IEEE80211_SUPPORT_SUPERG
2322 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2323 frm = ieee80211_add_ath(frm,
2324 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2325 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2326 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2327 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2329 #endif /* IEEE80211_SUPPORT_SUPERG */
2330 if (vap->iv_appie_assocreq != NULL)
2331 frm = add_appie(frm, vap->iv_appie_assocreq);
2332 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2334 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2335 (void *) vap->iv_state);
2338 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2339 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2341 * asresp frame format
2342 * [2] capability information
2344 * [2] association ID
2345 * [tlv] supported rates
2346 * [tlv] extended supported rates
2347 * [tlv] HT capabilities (standard, if STA enabled)
2348 * [tlv] HT information (standard, if STA enabled)
2349 * [tlv] WME (if configured and STA enabled)
2350 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2351 * [tlv] HT information (vendor OUI, if STA enabled)
2352 * [tlv] Atheros capabilities (if STA enabled)
2353 * [tlv] AppIE's (optional)
2355 m = ieee80211_getmgtframe(&frm,
2356 ic->ic_headroom + sizeof(struct ieee80211_frame),
2360 + 2 + IEEE80211_RATE_SIZE
2361 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2362 + sizeof(struct ieee80211_ie_htcap) + 4
2363 + sizeof(struct ieee80211_ie_htinfo) + 4
2364 + sizeof(struct ieee80211_wme_param)
2365 #ifdef IEEE80211_SUPPORT_SUPERG
2366 + sizeof(struct ieee80211_ath_ie)
2368 + (vap->iv_appie_assocresp != NULL ?
2369 vap->iv_appie_assocresp->ie_len : 0)
2372 senderr(ENOMEM, is_tx_nobuf);
2374 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2375 *(uint16_t *)frm = htole16(capinfo);
2378 *(uint16_t *)frm = htole16(arg); /* status */
2381 if (arg == IEEE80211_STATUS_SUCCESS) {
2382 *(uint16_t *)frm = htole16(ni->ni_associd);
2383 IEEE80211_NODE_STAT(ni, tx_assoc);
2385 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2388 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2389 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2390 /* NB: respond according to what we received */
2391 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2392 frm = ieee80211_add_htcap(frm, ni);
2393 frm = ieee80211_add_htinfo(frm, ni);
2395 if ((vap->iv_flags & IEEE80211_F_WME) &&
2396 ni->ni_ies.wme_ie != NULL)
2397 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2398 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2399 frm = ieee80211_add_htcap_vendor(frm, ni);
2400 frm = ieee80211_add_htinfo_vendor(frm, ni);
2402 #ifdef IEEE80211_SUPPORT_SUPERG
2403 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2404 frm = ieee80211_add_ath(frm,
2405 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2406 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2407 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2408 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2409 #endif /* IEEE80211_SUPPORT_SUPERG */
2410 if (vap->iv_appie_assocresp != NULL)
2411 frm = add_appie(frm, vap->iv_appie_assocresp);
2412 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2415 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2416 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2417 "send station disassociate (reason %d)", arg);
2418 m = ieee80211_getmgtframe(&frm,
2419 ic->ic_headroom + sizeof(struct ieee80211_frame),
2422 senderr(ENOMEM, is_tx_nobuf);
2423 *(uint16_t *)frm = htole16(arg); /* reason */
2424 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2426 IEEE80211_NODE_STAT(ni, tx_disassoc);
2427 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2431 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2432 "invalid mgmt frame type %u", type);
2433 senderr(EINVAL, is_tx_unknownmgt);
2437 /* NB: force non-ProbeResp frames to the highest queue */
2438 params.ibp_pri = WME_AC_VO;
2439 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2440 /* NB: we know all frames are unicast */
2441 params.ibp_try0 = bss->ni_txparms->maxretry;
2442 params.ibp_power = bss->ni_txpower;
2443 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2445 ieee80211_free_node(ni);
2452 * Return an mbuf with a probe response frame in it.
2453 * Space is left to prepend and 802.11 header at the
2454 * front but it's left to the caller to fill in.
2457 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2459 struct ieee80211vap *vap = bss->ni_vap;
2460 struct ieee80211com *ic = bss->ni_ic;
2461 const struct ieee80211_rateset *rs;
2467 * probe response frame format
2469 * [2] beacon interval
2470 * [2] cabability information
2472 * [tlv] supported rates
2473 * [tlv] parameter set (FH/DS)
2474 * [tlv] parameter set (IBSS)
2475 * [tlv] country (optional)
2476 * [3] power control (optional)
2477 * [5] channel switch announcement (CSA) (optional)
2478 * [tlv] extended rate phy (ERP)
2479 * [tlv] extended supported rates
2480 * [tlv] RSN (optional)
2481 * [tlv] HT capabilities
2482 * [tlv] HT information
2483 * [tlv] WPA (optional)
2484 * [tlv] WME (optional)
2485 * [tlv] Vendor OUI HT capabilities (optional)
2486 * [tlv] Vendor OUI HT information (optional)
2487 * [tlv] Atheros capabilities
2488 * [tlv] AppIE's (optional)
2489 * [tlv] Mesh ID (MBSS)
2490 * [tlv] Mesh Conf (MBSS)
2492 m = ieee80211_getmgtframe(&frm,
2493 ic->ic_headroom + sizeof(struct ieee80211_frame),
2497 + 2 + IEEE80211_NWID_LEN
2498 + 2 + IEEE80211_RATE_SIZE
2500 + IEEE80211_COUNTRY_MAX_SIZE
2502 + sizeof(struct ieee80211_csa_ie)
2503 + sizeof(struct ieee80211_quiet_ie)
2505 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2506 + sizeof(struct ieee80211_ie_wpa)
2507 + sizeof(struct ieee80211_ie_htcap)
2508 + sizeof(struct ieee80211_ie_htinfo)
2509 + sizeof(struct ieee80211_ie_wpa)
2510 + sizeof(struct ieee80211_wme_param)
2511 + 4 + sizeof(struct ieee80211_ie_htcap)
2512 + 4 + sizeof(struct ieee80211_ie_htinfo)
2513 #ifdef IEEE80211_SUPPORT_SUPERG
2514 + sizeof(struct ieee80211_ath_ie)
2516 #ifdef IEEE80211_SUPPORT_MESH
2517 + 2 + IEEE80211_MESHID_LEN
2518 + sizeof(struct ieee80211_meshconf_ie)
2520 + (vap->iv_appie_proberesp != NULL ?
2521 vap->iv_appie_proberesp->ie_len : 0)
2524 vap->iv_stats.is_tx_nobuf++;
2528 memset(frm, 0, 8); /* timestamp should be filled later */
2530 *(uint16_t *)frm = htole16(bss->ni_intval);
2532 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2533 *(uint16_t *)frm = htole16(capinfo);
2536 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2537 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2538 frm = ieee80211_add_rates(frm, rs);
2540 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2541 *frm++ = IEEE80211_ELEMID_FHPARMS;
2543 *frm++ = bss->ni_fhdwell & 0x00ff;
2544 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2545 *frm++ = IEEE80211_FH_CHANSET(
2546 ieee80211_chan2ieee(ic, bss->ni_chan));
2547 *frm++ = IEEE80211_FH_CHANPAT(
2548 ieee80211_chan2ieee(ic, bss->ni_chan));
2549 *frm++ = bss->ni_fhindex;
2551 *frm++ = IEEE80211_ELEMID_DSPARMS;
2553 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2556 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2557 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2559 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2561 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2562 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2563 frm = ieee80211_add_countryie(frm, ic);
2564 if (vap->iv_flags & IEEE80211_F_DOTH) {
2565 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2566 frm = ieee80211_add_powerconstraint(frm, vap);
2567 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2568 frm = ieee80211_add_csa(frm, vap);
2570 if (vap->iv_flags & IEEE80211_F_DOTH) {
2571 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2572 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2574 frm = ieee80211_add_quiet(frm, vap);
2577 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2578 frm = ieee80211_add_erp(frm, ic);
2579 frm = ieee80211_add_xrates(frm, rs);
2580 frm = ieee80211_add_rsn(frm, vap);
2582 * NB: legacy 11b clients do not get certain ie's.
2583 * The caller identifies such clients by passing
2584 * a token in legacy to us. Could expand this to be
2585 * any legacy client for stuff like HT ie's.
2587 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2588 legacy != IEEE80211_SEND_LEGACY_11B) {
2589 frm = ieee80211_add_htcap(frm, bss);
2590 frm = ieee80211_add_htinfo(frm, bss);
2592 frm = ieee80211_add_wpa(frm, vap);
2593 if (vap->iv_flags & IEEE80211_F_WME)
2594 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2595 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2596 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2597 legacy != IEEE80211_SEND_LEGACY_11B) {
2598 frm = ieee80211_add_htcap_vendor(frm, bss);
2599 frm = ieee80211_add_htinfo_vendor(frm, bss);
2601 #ifdef IEEE80211_SUPPORT_SUPERG
2602 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2603 legacy != IEEE80211_SEND_LEGACY_11B)
2604 frm = ieee80211_add_athcaps(frm, bss);
2606 if (vap->iv_appie_proberesp != NULL)
2607 frm = add_appie(frm, vap->iv_appie_proberesp);
2608 #ifdef IEEE80211_SUPPORT_MESH
2609 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2610 frm = ieee80211_add_meshid(frm, vap);
2611 frm = ieee80211_add_meshconf(frm, vap);
2614 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2620 * Send a probe response frame to the specified mac address.
2621 * This does not go through the normal mgt frame api so we
2622 * can specify the destination address and re-use the bss node
2623 * for the sta reference.
2626 ieee80211_send_proberesp(struct ieee80211vap *vap,
2627 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2629 struct ieee80211_node *bss = vap->iv_bss;
2630 struct ieee80211com *ic = vap->iv_ic;
2631 struct ieee80211_frame *wh;
2635 if (vap->iv_state == IEEE80211_S_CAC) {
2636 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2637 "block %s frame in CAC state", "probe response");
2638 vap->iv_stats.is_tx_badstate++;
2639 return EIO; /* XXX */
2643 * Hold a reference on the node so it doesn't go away until after
2644 * the xmit is complete all the way in the driver. On error we
2645 * will remove our reference.
2647 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2648 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2649 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2650 ieee80211_node_refcnt(bss)+1);
2651 ieee80211_ref_node(bss);
2653 m = ieee80211_alloc_proberesp(bss, legacy);
2655 ieee80211_free_node(bss);
2659 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2660 KASSERT(m != NULL, ("no room for header"));
2662 IEEE80211_TX_LOCK(ic);
2663 wh = mtod(m, struct ieee80211_frame *);
2664 ieee80211_send_setup(bss, m,
2665 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2666 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2667 /* XXX power management? */
2668 m->m_flags |= M_ENCAP; /* mark encapsulated */
2670 M_WME_SETAC(m, WME_AC_BE);
2672 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2673 "send probe resp on channel %u to %s%s\n",
2674 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2675 legacy ? " <legacy>" : "");
2676 IEEE80211_NODE_STAT(bss, tx_mgmt);
2678 ret = ieee80211_raw_output(vap, bss, m, NULL);
2679 IEEE80211_TX_UNLOCK(ic);
2684 * Allocate and build a RTS (Request To Send) control frame.
2687 ieee80211_alloc_rts(struct ieee80211com *ic,
2688 const uint8_t ra[IEEE80211_ADDR_LEN],
2689 const uint8_t ta[IEEE80211_ADDR_LEN],
2692 struct ieee80211_frame_rts *rts;
2695 /* XXX honor ic_headroom */
2696 m = m_gethdr(M_NOWAIT, MT_DATA);
2698 rts = mtod(m, struct ieee80211_frame_rts *);
2699 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2700 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2701 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2702 *(u_int16_t *)rts->i_dur = htole16(dur);
2703 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2704 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2706 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2712 * Allocate and build a CTS (Clear To Send) control frame.
2715 ieee80211_alloc_cts(struct ieee80211com *ic,
2716 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2718 struct ieee80211_frame_cts *cts;
2721 /* XXX honor ic_headroom */
2722 m = m_gethdr(M_NOWAIT, MT_DATA);
2724 cts = mtod(m, struct ieee80211_frame_cts *);
2725 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2726 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2727 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2728 *(u_int16_t *)cts->i_dur = htole16(dur);
2729 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2731 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2737 ieee80211_tx_mgt_timeout(void *arg)
2739 struct ieee80211vap *vap = arg;
2741 IEEE80211_LOCK(vap->iv_ic);
2742 if (vap->iv_state != IEEE80211_S_INIT &&
2743 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2745 * NB: it's safe to specify a timeout as the reason here;
2746 * it'll only be used in the right state.
2748 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
2749 IEEE80211_SCAN_FAIL_TIMEOUT);
2751 IEEE80211_UNLOCK(vap->iv_ic);
2755 * This is the callback set on net80211-sourced transmitted
2756 * authentication request frames.
2758 * This does a couple of things:
2760 * + If the frame transmitted was a success, it schedules a future
2761 * event which will transition the interface to scan.
2762 * If a state transition _then_ occurs before that event occurs,
2763 * said state transition will cancel this callout.
2765 * + If the frame transmit was a failure, it immediately schedules
2766 * the transition back to scan.
2769 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2771 struct ieee80211vap *vap = ni->ni_vap;
2772 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2775 * Frame transmit completed; arrange timer callback. If
2776 * transmit was successfuly we wait for response. Otherwise
2777 * we arrange an immediate callback instead of doing the
2778 * callback directly since we don't know what state the driver
2779 * is in (e.g. what locks it is holding). This work should
2780 * not be too time-critical and not happen too often so the
2781 * added overhead is acceptable.
2783 * XXX what happens if !acked but response shows up before callback?
2785 if (vap->iv_state == ostate) {
2786 callout_reset(&vap->iv_mgtsend,
2787 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2788 ieee80211_tx_mgt_timeout, vap);
2793 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2794 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2796 struct ieee80211vap *vap = ni->ni_vap;
2797 struct ieee80211com *ic = ni->ni_ic;
2798 struct ieee80211_rateset *rs = &ni->ni_rates;
2802 * beacon frame format
2804 * [2] beacon interval
2805 * [2] cabability information
2807 * [tlv] supported rates
2808 * [3] parameter set (DS)
2809 * [8] CF parameter set (optional)
2810 * [tlv] parameter set (IBSS/TIM)
2811 * [tlv] country (optional)
2812 * [3] power control (optional)
2813 * [5] channel switch announcement (CSA) (optional)
2814 * [tlv] extended rate phy (ERP)
2815 * [tlv] extended supported rates
2816 * [tlv] RSN parameters
2817 * [tlv] HT capabilities
2818 * [tlv] HT information
2819 * XXX Vendor-specific OIDs (e.g. Atheros)
2820 * [tlv] WPA parameters
2821 * [tlv] WME parameters
2822 * [tlv] Vendor OUI HT capabilities (optional)
2823 * [tlv] Vendor OUI HT information (optional)
2824 * [tlv] Atheros capabilities (optional)
2825 * [tlv] TDMA parameters (optional)
2826 * [tlv] Mesh ID (MBSS)
2827 * [tlv] Mesh Conf (MBSS)
2828 * [tlv] application data (optional)
2831 memset(bo, 0, sizeof(*bo));
2833 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2835 *(uint16_t *)frm = htole16(ni->ni_intval);
2837 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2838 bo->bo_caps = (uint16_t *)frm;
2839 *(uint16_t *)frm = htole16(capinfo);
2841 *frm++ = IEEE80211_ELEMID_SSID;
2842 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2843 *frm++ = ni->ni_esslen;
2844 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2845 frm += ni->ni_esslen;
2848 frm = ieee80211_add_rates(frm, rs);
2849 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2850 *frm++ = IEEE80211_ELEMID_DSPARMS;
2852 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2854 if (ic->ic_flags & IEEE80211_F_PCF) {
2856 frm = ieee80211_add_cfparms(frm, ic);
2859 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2860 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2862 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2864 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2865 vap->iv_opmode == IEEE80211_M_MBSS) {
2866 /* TIM IE is the same for Mesh and Hostap */
2867 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2869 tie->tim_ie = IEEE80211_ELEMID_TIM;
2870 tie->tim_len = 4; /* length */
2871 tie->tim_count = 0; /* DTIM count */
2872 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2873 tie->tim_bitctl = 0; /* bitmap control */
2874 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2875 frm += sizeof(struct ieee80211_tim_ie);
2878 bo->bo_tim_trailer = frm;
2879 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2880 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2881 frm = ieee80211_add_countryie(frm, ic);
2882 if (vap->iv_flags & IEEE80211_F_DOTH) {
2883 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2884 frm = ieee80211_add_powerconstraint(frm, vap);
2886 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2887 frm = ieee80211_add_csa(frm, vap);
2891 if (vap->iv_flags & IEEE80211_F_DOTH) {
2893 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2894 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2896 frm = ieee80211_add_quiet(frm,vap);
2901 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2903 frm = ieee80211_add_erp(frm, ic);
2905 frm = ieee80211_add_xrates(frm, rs);
2906 frm = ieee80211_add_rsn(frm, vap);
2907 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2908 frm = ieee80211_add_htcap(frm, ni);
2909 bo->bo_htinfo = frm;
2910 frm = ieee80211_add_htinfo(frm, ni);
2912 frm = ieee80211_add_wpa(frm, vap);
2913 if (vap->iv_flags & IEEE80211_F_WME) {
2915 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2917 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2918 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2919 frm = ieee80211_add_htcap_vendor(frm, ni);
2920 frm = ieee80211_add_htinfo_vendor(frm, ni);
2922 #ifdef IEEE80211_SUPPORT_SUPERG
2923 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2925 frm = ieee80211_add_athcaps(frm, ni);
2928 #ifdef IEEE80211_SUPPORT_TDMA
2929 if (vap->iv_caps & IEEE80211_C_TDMA) {
2931 frm = ieee80211_add_tdma(frm, vap);
2934 if (vap->iv_appie_beacon != NULL) {
2936 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2937 frm = add_appie(frm, vap->iv_appie_beacon);
2939 #ifdef IEEE80211_SUPPORT_MESH
2940 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2941 frm = ieee80211_add_meshid(frm, vap);
2942 bo->bo_meshconf = frm;
2943 frm = ieee80211_add_meshconf(frm, vap);
2946 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2947 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2948 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2952 * Allocate a beacon frame and fillin the appropriate bits.
2955 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2956 struct ieee80211_beacon_offsets *bo)
2958 struct ieee80211vap *vap = ni->ni_vap;
2959 struct ieee80211com *ic = ni->ni_ic;
2960 struct ifnet *ifp = vap->iv_ifp;
2961 struct ieee80211_frame *wh;
2967 * beacon frame format
2969 * [2] beacon interval
2970 * [2] cabability information
2972 * [tlv] supported rates
2973 * [3] parameter set (DS)
2974 * [8] CF parameter set (optional)
2975 * [tlv] parameter set (IBSS/TIM)
2976 * [tlv] country (optional)
2977 * [3] power control (optional)
2978 * [5] channel switch announcement (CSA) (optional)
2979 * [tlv] extended rate phy (ERP)
2980 * [tlv] extended supported rates
2981 * [tlv] RSN parameters
2982 * [tlv] HT capabilities
2983 * [tlv] HT information
2984 * [tlv] Vendor OUI HT capabilities (optional)
2985 * [tlv] Vendor OUI HT information (optional)
2986 * XXX Vendor-specific OIDs (e.g. Atheros)
2987 * [tlv] WPA parameters
2988 * [tlv] WME parameters
2989 * [tlv] TDMA parameters (optional)
2990 * [tlv] Mesh ID (MBSS)
2991 * [tlv] Mesh Conf (MBSS)
2992 * [tlv] application data (optional)
2993 * NB: we allocate the max space required for the TIM bitmap.
2994 * XXX how big is this?
2996 pktlen = 8 /* time stamp */
2997 + sizeof(uint16_t) /* beacon interval */
2998 + sizeof(uint16_t) /* capabilities */
2999 + 2 + ni->ni_esslen /* ssid */
3000 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3001 + 2 + 1 /* DS parameters */
3002 + 2 + 6 /* CF parameters */
3003 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3004 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3005 + 2 + 1 /* power control */
3006 + sizeof(struct ieee80211_csa_ie) /* CSA */
3007 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3009 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3010 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3011 2*sizeof(struct ieee80211_ie_wpa) : 0)
3012 /* XXX conditional? */
3013 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3014 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3015 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3016 sizeof(struct ieee80211_wme_param) : 0)
3017 #ifdef IEEE80211_SUPPORT_SUPERG
3018 + sizeof(struct ieee80211_ath_ie) /* ATH */
3020 #ifdef IEEE80211_SUPPORT_TDMA
3021 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3022 sizeof(struct ieee80211_tdma_param) : 0)
3024 #ifdef IEEE80211_SUPPORT_MESH
3025 + 2 + ni->ni_meshidlen
3026 + sizeof(struct ieee80211_meshconf_ie)
3028 + IEEE80211_MAX_APPIE
3030 m = ieee80211_getmgtframe(&frm,
3031 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3033 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3034 "%s: cannot get buf; size %u\n", __func__, pktlen);
3035 vap->iv_stats.is_tx_nobuf++;
3038 ieee80211_beacon_construct(m, frm, bo, ni);
3040 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3041 KASSERT(m != NULL, ("no space for 802.11 header?"));
3042 wh = mtod(m, struct ieee80211_frame *);
3043 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3044 IEEE80211_FC0_SUBTYPE_BEACON;
3045 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3046 *(uint16_t *)wh->i_dur = 0;
3047 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3048 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3049 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3050 *(uint16_t *)wh->i_seq = 0;
3056 * Update the dynamic parts of a beacon frame based on the current state.
3059 ieee80211_beacon_update(struct ieee80211_node *ni,
3060 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
3062 struct ieee80211vap *vap = ni->ni_vap;
3063 struct ieee80211com *ic = ni->ni_ic;
3064 int len_changed = 0;
3066 struct ieee80211_frame *wh;
3067 ieee80211_seq seqno;
3071 * Handle 11h channel change when we've reached the count.
3072 * We must recalculate the beacon frame contents to account
3073 * for the new channel. Note we do this only for the first
3074 * vap that reaches this point; subsequent vaps just update
3075 * their beacon state to reflect the recalculated channel.
3077 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3078 vap->iv_csa_count == ic->ic_csa_count) {
3079 vap->iv_csa_count = 0;
3081 * Effect channel change before reconstructing the beacon
3082 * frame contents as many places reference ni_chan.
3084 if (ic->ic_csa_newchan != NULL)
3085 ieee80211_csa_completeswitch(ic);
3087 * NB: ieee80211_beacon_construct clears all pending
3088 * updates in bo_flags so we don't need to explicitly
3089 * clear IEEE80211_BEACON_CSA.
3091 ieee80211_beacon_construct(m,
3092 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
3094 /* XXX do WME aggressive mode processing? */
3095 IEEE80211_UNLOCK(ic);
3096 return 1; /* just assume length changed */
3099 wh = mtod(m, struct ieee80211_frame *);
3100 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3101 *(uint16_t *)&wh->i_seq[0] =
3102 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3103 M_SEQNO_SET(m, seqno);
3105 /* XXX faster to recalculate entirely or just changes? */
3106 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3107 *bo->bo_caps = htole16(capinfo);
3109 if (vap->iv_flags & IEEE80211_F_WME) {
3110 struct ieee80211_wme_state *wme = &ic->ic_wme;
3113 * Check for agressive mode change. When there is
3114 * significant high priority traffic in the BSS
3115 * throttle back BE traffic by using conservative
3116 * parameters. Otherwise BE uses agressive params
3117 * to optimize performance of legacy/non-QoS traffic.
3119 if (wme->wme_flags & WME_F_AGGRMODE) {
3120 if (wme->wme_hipri_traffic >
3121 wme->wme_hipri_switch_thresh) {
3122 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3123 "%s: traffic %u, disable aggressive mode\n",
3124 __func__, wme->wme_hipri_traffic);
3125 wme->wme_flags &= ~WME_F_AGGRMODE;
3126 ieee80211_wme_updateparams_locked(vap);
3127 wme->wme_hipri_traffic =
3128 wme->wme_hipri_switch_hysteresis;
3130 wme->wme_hipri_traffic = 0;
3132 if (wme->wme_hipri_traffic <=
3133 wme->wme_hipri_switch_thresh) {
3134 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3135 "%s: traffic %u, enable aggressive mode\n",
3136 __func__, wme->wme_hipri_traffic);
3137 wme->wme_flags |= WME_F_AGGRMODE;
3138 ieee80211_wme_updateparams_locked(vap);
3139 wme->wme_hipri_traffic = 0;
3141 wme->wme_hipri_traffic =
3142 wme->wme_hipri_switch_hysteresis;
3144 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3145 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
3146 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3150 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3151 ieee80211_ht_update_beacon(vap, bo);
3152 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3154 #ifdef IEEE80211_SUPPORT_TDMA
3155 if (vap->iv_caps & IEEE80211_C_TDMA) {
3157 * NB: the beacon is potentially updated every TBTT.
3159 ieee80211_tdma_update_beacon(vap, bo);
3162 #ifdef IEEE80211_SUPPORT_MESH
3163 if (vap->iv_opmode == IEEE80211_M_MBSS)
3164 ieee80211_mesh_update_beacon(vap, bo);
3167 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3168 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3169 struct ieee80211_tim_ie *tie =
3170 (struct ieee80211_tim_ie *) bo->bo_tim;
3171 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3172 u_int timlen, timoff, i;
3174 * ATIM/DTIM needs updating. If it fits in the
3175 * current space allocated then just copy in the
3176 * new bits. Otherwise we need to move any trailing
3177 * data to make room. Note that we know there is
3178 * contiguous space because ieee80211_beacon_allocate
3179 * insures there is space in the mbuf to write a
3180 * maximal-size virtual bitmap (based on iv_max_aid).
3183 * Calculate the bitmap size and offset, copy any
3184 * trailer out of the way, and then copy in the
3185 * new bitmap and update the information element.
3186 * Note that the tim bitmap must contain at least
3187 * one byte and any offset must be even.
3189 if (vap->iv_ps_pending != 0) {
3190 timoff = 128; /* impossibly large */
3191 for (i = 0; i < vap->iv_tim_len; i++)
3192 if (vap->iv_tim_bitmap[i]) {
3196 KASSERT(timoff != 128, ("tim bitmap empty!"));
3197 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3198 if (vap->iv_tim_bitmap[i])
3200 timlen = 1 + (i - timoff);
3205 if (timlen != bo->bo_tim_len) {
3206 /* copy up/down trailer */
3207 int adjust = tie->tim_bitmap+timlen
3208 - bo->bo_tim_trailer;
3209 ovbcopy(bo->bo_tim_trailer,
3210 bo->bo_tim_trailer+adjust,
3211 bo->bo_tim_trailer_len);
3212 bo->bo_tim_trailer += adjust;
3213 bo->bo_erp += adjust;
3214 bo->bo_htinfo += adjust;
3215 #ifdef IEEE80211_SUPPORT_SUPERG
3216 bo->bo_ath += adjust;
3218 #ifdef IEEE80211_SUPPORT_TDMA
3219 bo->bo_tdma += adjust;
3221 #ifdef IEEE80211_SUPPORT_MESH
3222 bo->bo_meshconf += adjust;
3224 bo->bo_appie += adjust;
3225 bo->bo_wme += adjust;
3226 bo->bo_csa += adjust;
3227 bo->bo_quiet += adjust;
3228 bo->bo_tim_len = timlen;
3230 /* update information element */
3231 tie->tim_len = 3 + timlen;
3232 tie->tim_bitctl = timoff;
3235 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3238 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3240 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3241 "%s: TIM updated, pending %u, off %u, len %u\n",
3242 __func__, vap->iv_ps_pending, timoff, timlen);
3244 /* count down DTIM period */
3245 if (tie->tim_count == 0)
3246 tie->tim_count = tie->tim_period - 1;
3249 /* update state for buffered multicast frames on DTIM */
3250 if (mcast && tie->tim_count == 0)
3251 tie->tim_bitctl |= 1;
3253 tie->tim_bitctl &= ~1;
3254 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3255 struct ieee80211_csa_ie *csa =
3256 (struct ieee80211_csa_ie *) bo->bo_csa;
3259 * Insert or update CSA ie. If we're just starting
3260 * to count down to the channel switch then we need
3261 * to insert the CSA ie. Otherwise we just need to
3262 * drop the count. The actual change happens above
3263 * when the vap's count reaches the target count.
3265 if (vap->iv_csa_count == 0) {
3266 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3267 bo->bo_erp += sizeof(*csa);
3268 bo->bo_htinfo += sizeof(*csa);
3269 bo->bo_wme += sizeof(*csa);
3270 #ifdef IEEE80211_SUPPORT_SUPERG
3271 bo->bo_ath += sizeof(*csa);
3273 #ifdef IEEE80211_SUPPORT_TDMA
3274 bo->bo_tdma += sizeof(*csa);
3276 #ifdef IEEE80211_SUPPORT_MESH
3277 bo->bo_meshconf += sizeof(*csa);
3279 bo->bo_appie += sizeof(*csa);
3280 bo->bo_csa_trailer_len += sizeof(*csa);
3281 bo->bo_quiet += sizeof(*csa);
3282 bo->bo_tim_trailer_len += sizeof(*csa);
3283 m->m_len += sizeof(*csa);
3284 m->m_pkthdr.len += sizeof(*csa);
3286 ieee80211_add_csa(bo->bo_csa, vap);
3289 vap->iv_csa_count++;
3290 /* NB: don't clear IEEE80211_BEACON_CSA */
3292 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3293 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3295 ieee80211_add_quiet(bo->bo_quiet, vap);
3297 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3299 * ERP element needs updating.
3301 (void) ieee80211_add_erp(bo->bo_erp, ic);
3302 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3304 #ifdef IEEE80211_SUPPORT_SUPERG
3305 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3306 ieee80211_add_athcaps(bo->bo_ath, ni);
3307 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3311 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3312 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3318 aielen += aie->ie_len;
3319 if (aielen != bo->bo_appie_len) {
3320 /* copy up/down trailer */
3321 int adjust = aielen - bo->bo_appie_len;
3322 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3323 bo->bo_tim_trailer_len);
3324 bo->bo_tim_trailer += adjust;
3325 bo->bo_appie += adjust;
3326 bo->bo_appie_len = aielen;
3332 frm = add_appie(frm, aie);
3333 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3335 IEEE80211_UNLOCK(ic);
3341 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3342 * tunnel encapsulation. The frame is assumed to have an Ethernet
3343 * header at the front that must be stripped before prepending the
3344 * LLC followed by the Ethernet header passed in (with an Ethernet
3345 * type that specifies the payload size).
3348 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3349 const struct ether_header *eh)
3354 /* XXX optimize by combining m_adj+M_PREPEND */
3355 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3356 llc = mtod(m, struct llc *);
3357 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3358 llc->llc_control = LLC_UI;
3359 llc->llc_snap.org_code[0] = 0;
3360 llc->llc_snap.org_code[1] = 0;
3361 llc->llc_snap.org_code[2] = 0;
3362 llc->llc_snap.ether_type = eh->ether_type;
3363 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3365 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3366 if (m == NULL) { /* XXX cannot happen */
3367 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3368 "%s: no space for ether_header\n", __func__);
3369 vap->iv_stats.is_tx_nobuf++;
3372 ETHER_HEADER_COPY(mtod(m, void *), eh);
3373 mtod(m, struct ether_header *)->ether_type = htons(payload);
3378 * Complete an mbuf transmission.
3380 * For now, this simply processes a completed frame after the
3381 * driver has completed it's transmission and/or retransmission.
3382 * It assumes the frame is an 802.11 encapsulated frame.
3384 * Later on it will grow to become the exit path for a given frame
3385 * from the driver and, depending upon how it's been encapsulated
3386 * and already transmitted, it may end up doing A-MPDU retransmission,
3387 * power save requeuing, etc.
3389 * In order for the above to work, the driver entry point to this
3390 * must not hold any driver locks. Thus, the driver needs to delay
3391 * any actual mbuf completion until it can release said locks.
3393 * This frees the mbuf and if the mbuf has a node reference,
3394 * the node reference will be freed.
3397 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3401 if (m->m_flags & M_TXCB)
3402 ieee80211_process_callback(ni, m, status);
3403 ieee80211_free_node(ni);