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 * Send the given mbuf through the given vap.
115 * This consumes the mbuf regardless of whether the transmit
116 * was successful or not.
118 * This does none of the initial checks that ieee80211_start()
119 * does (eg CAC timeout, interface wakeup) - the caller must
123 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
125 #define IS_DWDS(vap) \
126 (vap->iv_opmode == IEEE80211_M_WDS && \
127 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
128 struct ieee80211com *ic = vap->iv_ic;
129 struct ifnet *ifp = vap->iv_ifp;
130 struct ieee80211_node *ni;
131 struct ether_header *eh;
135 * Cancel any background scan.
137 if (ic->ic_flags & IEEE80211_F_SCAN)
138 ieee80211_cancel_anyscan(vap);
140 * Find the node for the destination so we can do
141 * things like power save and fast frames aggregation.
143 * NB: past this point various code assumes the first
144 * mbuf has the 802.3 header present (and contiguous).
147 if (m->m_len < sizeof(struct ether_header) &&
148 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
149 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
150 "discard frame, %s\n", "m_pullup failed");
151 vap->iv_stats.is_tx_nobuf++; /* XXX */
155 eh = mtod(m, struct ether_header *);
156 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
159 * Only unicast frames from the above go out
160 * DWDS vaps; multicast frames are handled by
161 * dispatching the frame as it comes through
162 * the AP vap (see below).
164 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
165 eh->ether_dhost, "mcast", "%s", "on DWDS");
166 vap->iv_stats.is_dwds_mcast++;
168 /* XXX better status? */
171 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
173 * Spam DWDS vap's w/ multicast traffic.
175 /* XXX only if dwds in use? */
176 ieee80211_dwds_mcast(vap, m);
179 #ifdef IEEE80211_SUPPORT_MESH
180 if (vap->iv_opmode != IEEE80211_M_MBSS) {
182 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
184 /* NB: ieee80211_find_txnode does stat+msg */
187 /* XXX better status? */
190 if (ni->ni_associd == 0 &&
191 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
192 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
193 eh->ether_dhost, NULL,
194 "sta not associated (type 0x%04x)",
195 htons(eh->ether_type));
196 vap->iv_stats.is_tx_notassoc++;
199 ieee80211_free_node(ni);
200 /* XXX better status? */
203 #ifdef IEEE80211_SUPPORT_MESH
205 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
207 * Proxy station only if configured.
209 if (!ieee80211_mesh_isproxyena(vap)) {
210 IEEE80211_DISCARD_MAC(vap,
211 IEEE80211_MSG_OUTPUT |
213 eh->ether_dhost, NULL,
214 "%s", "proxy not enabled");
215 vap->iv_stats.is_mesh_notproxy++;
218 /* XXX better status? */
221 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
222 "forward frame from DS SA(%6D), DA(%6D)\n",
223 eh->ether_shost, ":",
224 eh->ether_dhost, ":");
225 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
227 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
230 * NB: ieee80211_mesh_discover holds/disposes
231 * frame (e.g. queueing on path discovery).
234 /* XXX better status? */
239 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
240 (m->m_flags & M_PWR_SAV) == 0) {
242 * Station in power save mode; pass the frame
243 * to the 802.11 layer and continue. We'll get
244 * the frame back when the time is right.
245 * XXX lose WDS vap linkage?
247 (void) ieee80211_pwrsave(ni, m);
248 ieee80211_free_node(ni);
249 /* XXX better status? */
252 /* calculate priority so drivers can find the tx queue */
253 if (ieee80211_classify(ni, m)) {
254 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
255 eh->ether_dhost, NULL,
256 "%s", "classification failure");
257 vap->iv_stats.is_tx_classify++;
260 ieee80211_free_node(ni);
261 /* XXX better status? */
265 * Stash the node pointer. Note that we do this after
266 * any call to ieee80211_dwds_mcast because that code
267 * uses any existing value for rcvif to identify the
268 * interface it (might have been) received on.
270 m->m_pkthdr.rcvif = (void *)ni;
272 BPF_MTAP(ifp, m); /* 802.3 tx */
276 * Check if A-MPDU tx aggregation is setup or if we
277 * should try to enable it. The sta must be associated
278 * with HT and A-MPDU enabled for use. When the policy
279 * routine decides we should enable A-MPDU we issue an
280 * ADDBA request and wait for a reply. The frame being
281 * encapsulated will go out w/o using A-MPDU, or possibly
282 * it might be collected by the driver and held/retransmit.
283 * The default ic_ampdu_enable routine handles staggering
284 * ADDBA requests in case the receiver NAK's us or we are
285 * otherwise unable to establish a BA stream.
287 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
288 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
289 (m->m_flags & M_EAPOL) == 0) {
290 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
291 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
293 ieee80211_txampdu_count_packet(tap);
294 if (IEEE80211_AMPDU_RUNNING(tap)) {
296 * Operational, mark frame for aggregation.
298 * XXX do tx aggregation here
300 m->m_flags |= M_AMPDU_MPDU;
301 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
302 ic->ic_ampdu_enable(ni, tap)) {
304 * Not negotiated yet, request service.
306 ieee80211_ampdu_request(ni, tap);
307 /* XXX hold frame for reply? */
311 #ifdef IEEE80211_SUPPORT_SUPERG
312 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
313 m = ieee80211_ff_check(ni, m);
315 /* NB: any ni ref held on stageq */
316 /* XXX better status? */
320 #endif /* IEEE80211_SUPPORT_SUPERG */
323 * Grab the TX lock - serialise the TX process from this
324 * point (where TX state is being checked/modified)
325 * through to driver queue.
327 IEEE80211_TX_LOCK(ic);
329 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
331 * Encapsulate the packet in prep for transmission.
333 m = ieee80211_encap(vap, ni, m);
335 /* NB: stat+msg handled in ieee80211_encap */
336 IEEE80211_TX_UNLOCK(ic);
337 ieee80211_free_node(ni);
338 /* XXX better status? */
342 error = ieee80211_parent_transmit(ic, m);
345 * Unlock at this point - no need to hold it across
346 * ieee80211_free_node() (ie, the comlock)
348 IEEE80211_TX_UNLOCK(ic);
350 /* NB: IFQ_HANDOFF reclaims mbuf */
351 ieee80211_free_node(ni);
355 ic->ic_lastdata = ticks;
362 * Start method for vap's. All packets from the stack come
363 * through here. We handle common processing of the packets
364 * before dispatching them to the underlying device.
367 ieee80211_start(struct ifnet *ifp)
369 struct ieee80211vap *vap = ifp->if_softc;
370 struct ieee80211com *ic = vap->iv_ic;
371 struct ifnet *parent = ic->ic_ifp;
374 /* NB: parent must be up and running */
375 if (!IFNET_IS_UP_RUNNING(parent)) {
376 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
377 "%s: ignore queue, parent %s not up+running\n",
378 __func__, parent->if_xname);
382 if (vap->iv_state == IEEE80211_S_SLEEP) {
384 * In power save, wakeup device for transmit.
386 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
390 * No data frames go out unless we're running.
391 * Note in particular this covers CAC and CSA
392 * states (though maybe we should check muting
395 if (vap->iv_state != IEEE80211_S_RUN) {
397 /* re-check under the com lock to avoid races */
398 if (vap->iv_state != IEEE80211_S_RUN) {
399 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
400 "%s: ignore queue, in %s state\n",
401 __func__, ieee80211_state_name[vap->iv_state]);
402 vap->iv_stats.is_tx_badstate++;
403 IEEE80211_UNLOCK(ic);
404 IFQ_LOCK(&ifp->if_snd);
405 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
406 IFQ_UNLOCK(&ifp->if_snd);
409 IEEE80211_UNLOCK(ic);
413 IFQ_DEQUEUE(&ifp->if_snd, m);
417 * Sanitize mbuf flags for net80211 use. We cannot
418 * clear M_PWR_SAV or M_MORE_DATA because these may
419 * be set for frames that are re-submitted from the
422 * NB: This must be done before ieee80211_classify as
423 * it marks EAPOL in frames with M_EAPOL.
425 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
427 * Bump to the packet transmission path.
429 (void) ieee80211_start_pkt(vap, m);
430 /* mbuf is consumed here */
435 * 802.11 raw output routine.
438 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
439 struct mbuf *m, const struct ieee80211_bpf_params *params)
441 struct ieee80211com *ic = vap->iv_ic;
443 return (ic->ic_raw_xmit(ni, m, params));
447 * 802.11 output routine. This is (currently) used only to
448 * connect bpf write calls to the 802.11 layer for injecting
452 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
453 const struct sockaddr *dst, struct route *ro)
455 #define senderr(e) do { error = (e); goto bad;} while (0)
456 struct ieee80211_node *ni = NULL;
457 struct ieee80211vap *vap;
458 struct ieee80211_frame *wh;
459 struct ieee80211com *ic = NULL;
463 IFQ_LOCK(&ifp->if_snd);
464 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
465 IFQ_UNLOCK(&ifp->if_snd);
467 * Short-circuit requests if the vap is marked OACTIVE
468 * as this can happen because a packet came down through
469 * ieee80211_start before the vap entered RUN state in
470 * which case it's ok to just drop the frame. This
471 * should not be necessary but callers of if_output don't
476 IFQ_UNLOCK(&ifp->if_snd);
480 * Hand to the 802.3 code if not tagged as
481 * a raw 802.11 frame.
483 if (dst->sa_family != AF_IEEE80211)
484 return vap->iv_output(ifp, m, dst, ro);
486 error = mac_ifnet_check_transmit(ifp, m);
490 if (ifp->if_flags & IFF_MONITOR)
492 if (!IFNET_IS_UP_RUNNING(ifp))
494 if (vap->iv_state == IEEE80211_S_CAC) {
495 IEEE80211_DPRINTF(vap,
496 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
497 "block %s frame in CAC state\n", "raw data");
498 vap->iv_stats.is_tx_badstate++;
499 senderr(EIO); /* XXX */
500 } else if (vap->iv_state == IEEE80211_S_SCAN)
502 /* XXX bypass bridge, pfil, carp, etc. */
504 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
505 senderr(EIO); /* XXX */
506 wh = mtod(m, struct ieee80211_frame *);
507 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
508 IEEE80211_FC0_VERSION_0)
509 senderr(EIO); /* XXX */
511 /* locate destination node */
512 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
513 case IEEE80211_FC1_DIR_NODS:
514 case IEEE80211_FC1_DIR_FROMDS:
515 ni = ieee80211_find_txnode(vap, wh->i_addr1);
517 case IEEE80211_FC1_DIR_TODS:
518 case IEEE80211_FC1_DIR_DSTODS:
519 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
520 senderr(EIO); /* XXX */
521 ni = ieee80211_find_txnode(vap, wh->i_addr3);
524 senderr(EIO); /* XXX */
528 * Permit packets w/ bpf params through regardless
529 * (see below about sa_len).
531 if (dst->sa_len == 0)
532 senderr(EHOSTUNREACH);
533 ni = ieee80211_ref_node(vap->iv_bss);
537 * Sanitize mbuf for net80211 flags leaked from above.
539 * NB: This must be done before ieee80211_classify as
540 * it marks EAPOL in frames with M_EAPOL.
542 m->m_flags &= ~M_80211_TX;
544 /* calculate priority so drivers can find the tx queue */
545 /* XXX assumes an 802.3 frame */
546 if (ieee80211_classify(ni, m))
547 senderr(EIO); /* XXX */
550 IEEE80211_NODE_STAT(ni, tx_data);
551 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
552 IEEE80211_NODE_STAT(ni, tx_mcast);
553 m->m_flags |= M_MCAST;
555 IEEE80211_NODE_STAT(ni, tx_ucast);
556 /* NB: ieee80211_encap does not include 802.11 header */
557 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
559 IEEE80211_TX_LOCK(ic);
562 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
563 * present by setting the sa_len field of the sockaddr (yes,
565 * NB: we assume sa_data is suitably aligned to cast.
567 ret = ieee80211_raw_output(vap, ni, m,
568 (const struct ieee80211_bpf_params *)(dst->sa_len ?
569 dst->sa_data : NULL));
570 IEEE80211_TX_UNLOCK(ic);
576 ieee80211_free_node(ni);
583 * Set the direction field and address fields of an outgoing
584 * frame. Note this should be called early on in constructing
585 * a frame as it sets i_fc[1]; other bits can then be or'd in.
588 ieee80211_send_setup(
589 struct ieee80211_node *ni,
592 const uint8_t sa[IEEE80211_ADDR_LEN],
593 const uint8_t da[IEEE80211_ADDR_LEN],
594 const uint8_t bssid[IEEE80211_ADDR_LEN])
596 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
597 struct ieee80211vap *vap = ni->ni_vap;
598 struct ieee80211_tx_ampdu *tap;
599 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
602 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
604 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
605 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
606 switch (vap->iv_opmode) {
607 case IEEE80211_M_STA:
608 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
609 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
610 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
611 IEEE80211_ADDR_COPY(wh->i_addr3, da);
613 case IEEE80211_M_IBSS:
614 case IEEE80211_M_AHDEMO:
615 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
616 IEEE80211_ADDR_COPY(wh->i_addr1, da);
617 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
618 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
620 case IEEE80211_M_HOSTAP:
621 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
622 IEEE80211_ADDR_COPY(wh->i_addr1, da);
623 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
624 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
626 case IEEE80211_M_WDS:
627 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
628 IEEE80211_ADDR_COPY(wh->i_addr1, da);
629 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
630 IEEE80211_ADDR_COPY(wh->i_addr3, da);
631 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
633 case IEEE80211_M_MBSS:
634 #ifdef IEEE80211_SUPPORT_MESH
635 if (IEEE80211_IS_MULTICAST(da)) {
636 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
638 IEEE80211_ADDR_COPY(wh->i_addr1, da);
639 IEEE80211_ADDR_COPY(wh->i_addr2,
642 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
643 IEEE80211_ADDR_COPY(wh->i_addr1, da);
644 IEEE80211_ADDR_COPY(wh->i_addr2,
646 IEEE80211_ADDR_COPY(wh->i_addr3, da);
647 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
651 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
655 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
656 IEEE80211_ADDR_COPY(wh->i_addr1, da);
657 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
658 #ifdef IEEE80211_SUPPORT_MESH
659 if (vap->iv_opmode == IEEE80211_M_MBSS)
660 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
663 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
665 *(uint16_t *)&wh->i_dur[0] = 0;
667 tap = &ni->ni_tx_ampdu[tid];
668 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
669 m->m_flags |= M_AMPDU_MPDU;
671 seqno = ni->ni_txseqs[tid]++;
672 *(uint16_t *)&wh->i_seq[0] =
673 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
674 M_SEQNO_SET(m, seqno);
677 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
678 m->m_flags |= M_MCAST;
683 * Send a management frame to the specified node. The node pointer
684 * must have a reference as the pointer will be passed to the driver
685 * and potentially held for a long time. If the frame is successfully
686 * dispatched to the driver, then it is responsible for freeing the
687 * reference (and potentially free'ing up any associated storage);
688 * otherwise deal with reclaiming any reference (on error).
691 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
692 struct ieee80211_bpf_params *params)
694 struct ieee80211vap *vap = ni->ni_vap;
695 struct ieee80211com *ic = ni->ni_ic;
696 struct ieee80211_frame *wh;
699 KASSERT(ni != NULL, ("null node"));
701 if (vap->iv_state == IEEE80211_S_CAC) {
702 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
703 ni, "block %s frame in CAC state",
704 ieee80211_mgt_subtype_name[
705 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
706 IEEE80211_FC0_SUBTYPE_SHIFT]);
707 vap->iv_stats.is_tx_badstate++;
708 ieee80211_free_node(ni);
710 return EIO; /* XXX */
713 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
715 ieee80211_free_node(ni);
719 IEEE80211_TX_LOCK(ic);
721 wh = mtod(m, struct ieee80211_frame *);
722 ieee80211_send_setup(ni, m,
723 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
724 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
725 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
726 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
727 "encrypting frame (%s)", __func__);
728 wh->i_fc[1] |= IEEE80211_FC1_WEP;
730 m->m_flags |= M_ENCAP; /* mark encapsulated */
732 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
733 M_WME_SETAC(m, params->ibp_pri);
735 #ifdef IEEE80211_DEBUG
736 /* avoid printing too many frames */
737 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
738 ieee80211_msg_dumppkts(vap)) {
739 printf("[%s] send %s on channel %u\n",
740 ether_sprintf(wh->i_addr1),
741 ieee80211_mgt_subtype_name[
742 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
743 IEEE80211_FC0_SUBTYPE_SHIFT],
744 ieee80211_chan2ieee(ic, ic->ic_curchan));
747 IEEE80211_NODE_STAT(ni, tx_mgmt);
749 ret = ieee80211_raw_output(vap, ni, m, params);
750 IEEE80211_TX_UNLOCK(ic);
755 * Send a null data frame to the specified node. If the station
756 * is setup for QoS then a QoS Null Data frame is constructed.
757 * If this is a WDS station then a 4-address frame is constructed.
759 * NB: the caller is assumed to have setup a node reference
760 * for use; this is necessary to deal with a race condition
761 * when probing for inactive stations. Like ieee80211_mgmt_output
762 * we must cleanup any node reference on error; however we
763 * can safely just unref it as we know it will never be the
764 * last reference to the node.
767 ieee80211_send_nulldata(struct ieee80211_node *ni)
769 struct ieee80211vap *vap = ni->ni_vap;
770 struct ieee80211com *ic = ni->ni_ic;
772 struct ieee80211_frame *wh;
777 if (vap->iv_state == IEEE80211_S_CAC) {
778 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
779 ni, "block %s frame in CAC state", "null data");
780 ieee80211_unref_node(&ni);
781 vap->iv_stats.is_tx_badstate++;
782 return EIO; /* XXX */
785 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
786 hdrlen = sizeof(struct ieee80211_qosframe);
788 hdrlen = sizeof(struct ieee80211_frame);
789 /* NB: only WDS vap's get 4-address frames */
790 if (vap->iv_opmode == IEEE80211_M_WDS)
791 hdrlen += IEEE80211_ADDR_LEN;
792 if (ic->ic_flags & IEEE80211_F_DATAPAD)
793 hdrlen = roundup(hdrlen, sizeof(uint32_t));
795 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
798 ieee80211_unref_node(&ni);
799 vap->iv_stats.is_tx_nobuf++;
802 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
803 ("leading space %zd", M_LEADINGSPACE(m)));
804 M_PREPEND(m, hdrlen, M_NOWAIT);
806 /* NB: cannot happen */
807 ieee80211_free_node(ni);
811 IEEE80211_TX_LOCK(ic);
813 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
814 if (ni->ni_flags & IEEE80211_NODE_QOS) {
815 const int tid = WME_AC_TO_TID(WME_AC_BE);
818 ieee80211_send_setup(ni, m,
819 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
820 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
822 if (vap->iv_opmode == IEEE80211_M_WDS)
823 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
825 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
826 qos[0] = tid & IEEE80211_QOS_TID;
827 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
828 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
831 ieee80211_send_setup(ni, m,
832 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
833 IEEE80211_NONQOS_TID,
834 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
836 if (vap->iv_opmode != IEEE80211_M_WDS) {
837 /* NB: power management bit is never sent by an AP */
838 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
839 vap->iv_opmode != IEEE80211_M_HOSTAP)
840 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
842 m->m_len = m->m_pkthdr.len = hdrlen;
843 m->m_flags |= M_ENCAP; /* mark encapsulated */
845 M_WME_SETAC(m, WME_AC_BE);
847 IEEE80211_NODE_STAT(ni, tx_data);
849 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
850 "send %snull data frame on channel %u, pwr mgt %s",
851 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
852 ieee80211_chan2ieee(ic, ic->ic_curchan),
853 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
855 ret = ieee80211_raw_output(vap, ni, m, NULL);
856 IEEE80211_TX_UNLOCK(ic);
861 * Assign priority to a frame based on any vlan tag assigned
862 * to the station and/or any Diffserv setting in an IP header.
863 * Finally, if an ACM policy is setup (in station mode) it's
867 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
869 const struct ether_header *eh = mtod(m, struct ether_header *);
870 int v_wme_ac, d_wme_ac, ac;
873 * Always promote PAE/EAPOL frames to high priority.
875 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
876 /* NB: mark so others don't need to check header */
877 m->m_flags |= M_EAPOL;
882 * Non-qos traffic goes to BE.
884 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
890 * If node has a vlan tag then all traffic
891 * to it must have a matching tag.
894 if (ni->ni_vlan != 0) {
895 if ((m->m_flags & M_VLANTAG) == 0) {
896 IEEE80211_NODE_STAT(ni, tx_novlantag);
899 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
900 EVL_VLANOFTAG(ni->ni_vlan)) {
901 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
904 /* map vlan priority to AC */
905 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
908 /* XXX m_copydata may be too slow for fast path */
910 if (eh->ether_type == htons(ETHERTYPE_IP)) {
913 * IP frame, map the DSCP bits from the TOS field.
915 /* NB: ip header may not be in first mbuf */
916 m_copydata(m, sizeof(struct ether_header) +
917 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
918 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
919 d_wme_ac = TID_TO_WME_AC(tos);
923 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
927 * IPv6 frame, map the DSCP bits from the traffic class field.
929 m_copydata(m, sizeof(struct ether_header) +
930 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
932 tos = (uint8_t)(ntohl(flow) >> 20);
933 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
934 d_wme_ac = TID_TO_WME_AC(tos);
937 d_wme_ac = WME_AC_BE;
945 * Use highest priority AC.
947 if (v_wme_ac > d_wme_ac)
955 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
956 static const int acmap[4] = {
957 WME_AC_BK, /* WME_AC_BE */
958 WME_AC_BK, /* WME_AC_BK */
959 WME_AC_BE, /* WME_AC_VI */
960 WME_AC_VI, /* WME_AC_VO */
962 struct ieee80211com *ic = ni->ni_ic;
964 while (ac != WME_AC_BK &&
965 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
974 * Insure there is sufficient contiguous space to encapsulate the
975 * 802.11 data frame. If room isn't already there, arrange for it.
976 * Drivers and cipher modules assume we have done the necessary work
977 * and fail rudely if they don't find the space they need.
980 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
981 struct ieee80211_key *key, struct mbuf *m)
983 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
984 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
987 /* XXX belongs in crypto code? */
988 needed_space += key->wk_cipher->ic_header;
991 * When crypto is being done in the host we must insure
992 * the data are writable for the cipher routines; clone
993 * a writable mbuf chain.
994 * XXX handle SWMIC specially
996 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
997 m = m_unshare(m, M_NOWAIT);
999 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1000 "%s: cannot get writable mbuf\n", __func__);
1001 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1007 * We know we are called just before stripping an Ethernet
1008 * header and prepending an LLC header. This means we know
1010 * sizeof(struct ether_header) - sizeof(struct llc)
1011 * bytes recovered to which we need additional space for the
1012 * 802.11 header and any crypto header.
1014 /* XXX check trailing space and copy instead? */
1015 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1016 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1018 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1019 "%s: cannot expand storage\n", __func__);
1020 vap->iv_stats.is_tx_nobuf++;
1024 KASSERT(needed_space <= MHLEN,
1025 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
1027 * Setup new mbuf to have leading space to prepend the
1028 * 802.11 header and any crypto header bits that are
1029 * required (the latter are added when the driver calls
1030 * back to ieee80211_crypto_encap to do crypto encapsulation).
1032 /* NB: must be first 'cuz it clobbers m_data */
1033 m_move_pkthdr(n, m);
1034 n->m_len = 0; /* NB: m_gethdr does not set */
1035 n->m_data += needed_space;
1037 * Pull up Ethernet header to create the expected layout.
1038 * We could use m_pullup but that's overkill (i.e. we don't
1039 * need the actual data) and it cannot fail so do it inline
1042 /* NB: struct ether_header is known to be contiguous */
1043 n->m_len += sizeof(struct ether_header);
1044 m->m_len -= sizeof(struct ether_header);
1045 m->m_data += sizeof(struct ether_header);
1047 * Replace the head of the chain.
1053 #undef TO_BE_RECLAIMED
1057 * Return the transmit key to use in sending a unicast frame.
1058 * If a unicast key is set we use that. When no unicast key is set
1059 * we fall back to the default transmit key.
1061 static __inline struct ieee80211_key *
1062 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1063 struct ieee80211_node *ni)
1065 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1066 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1067 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1069 return &vap->iv_nw_keys[vap->iv_def_txkey];
1071 return &ni->ni_ucastkey;
1076 * Return the transmit key to use in sending a multicast frame.
1077 * Multicast traffic always uses the group key which is installed as
1078 * the default tx key.
1080 static __inline struct ieee80211_key *
1081 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1082 struct ieee80211_node *ni)
1084 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1085 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1087 return &vap->iv_nw_keys[vap->iv_def_txkey];
1091 * Encapsulate an outbound data frame. The mbuf chain is updated.
1092 * If an error is encountered NULL is returned. The caller is required
1093 * to provide a node reference and pullup the ethernet header in the
1096 * NB: Packet is assumed to be processed by ieee80211_classify which
1097 * marked EAPOL frames w/ M_EAPOL.
1100 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1103 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1104 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1105 struct ieee80211com *ic = ni->ni_ic;
1106 #ifdef IEEE80211_SUPPORT_MESH
1107 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1108 struct ieee80211_meshcntl_ae10 *mc;
1109 struct ieee80211_mesh_route *rt = NULL;
1112 struct ether_header eh;
1113 struct ieee80211_frame *wh;
1114 struct ieee80211_key *key;
1116 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1117 ieee80211_seq seqno;
1118 int meshhdrsize, meshae;
1121 IEEE80211_TX_LOCK_ASSERT(ic);
1124 * Copy existing Ethernet header to a safe place. The
1125 * rest of the code assumes it's ok to strip it when
1126 * reorganizing state for the final encapsulation.
1128 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1129 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1132 * Insure space for additional headers. First identify
1133 * transmit key to use in calculating any buffer adjustments
1134 * required. This is also used below to do privacy
1135 * encapsulation work. Then calculate the 802.11 header
1136 * size and any padding required by the driver.
1138 * Note key may be NULL if we fall back to the default
1139 * transmit key and that is not set. In that case the
1140 * buffer may not be expanded as needed by the cipher
1141 * routines, but they will/should discard it.
1143 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1144 if (vap->iv_opmode == IEEE80211_M_STA ||
1145 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1146 (vap->iv_opmode == IEEE80211_M_WDS &&
1147 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1148 key = ieee80211_crypto_getucastkey(vap, ni);
1150 key = ieee80211_crypto_getmcastkey(vap, ni);
1151 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1152 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1154 "no default transmit key (%s) deftxkey %u",
1155 __func__, vap->iv_def_txkey);
1156 vap->iv_stats.is_tx_nodefkey++;
1162 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1163 * frames so suppress use. This may be an issue if other
1164 * ap's require all data frames to be QoS-encapsulated
1165 * once negotiated in which case we'll need to make this
1167 * NB: mesh data frames are QoS.
1169 addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1170 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1171 (m->m_flags & M_EAPOL) == 0;
1173 hdrsize = sizeof(struct ieee80211_qosframe);
1175 hdrsize = sizeof(struct ieee80211_frame);
1176 #ifdef IEEE80211_SUPPORT_MESH
1177 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1179 * Mesh data frames are encapsulated according to the
1180 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1181 * o Group Addressed data (aka multicast) originating
1182 * at the local sta are sent w/ 3-address format and
1183 * address extension mode 00
1184 * o Individually Addressed data (aka unicast) originating
1185 * at the local sta are sent w/ 4-address format and
1186 * address extension mode 00
1187 * o Group Addressed data forwarded from a non-mesh sta are
1188 * sent w/ 3-address format and address extension mode 01
1189 * o Individually Address data from another sta are sent
1190 * w/ 4-address format and address extension mode 10
1192 is4addr = 0; /* NB: don't use, disable */
1193 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1194 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1195 KASSERT(rt != NULL, ("route is NULL"));
1196 dir = IEEE80211_FC1_DIR_DSTODS;
1197 hdrsize += IEEE80211_ADDR_LEN;
1198 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1199 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1201 IEEE80211_NOTE_MAC(vap,
1204 "%s", "trying to send to ourself");
1207 meshae = IEEE80211_MESH_AE_10;
1209 sizeof(struct ieee80211_meshcntl_ae10);
1211 meshae = IEEE80211_MESH_AE_00;
1213 sizeof(struct ieee80211_meshcntl);
1216 dir = IEEE80211_FC1_DIR_FROMDS;
1217 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1219 meshae = IEEE80211_MESH_AE_01;
1221 sizeof(struct ieee80211_meshcntl_ae01);
1224 meshae = IEEE80211_MESH_AE_00;
1225 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1231 * 4-address frames need to be generated for:
1232 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1233 * o packets sent through a vap marked for relaying
1234 * (e.g. a station operating with dynamic WDS)
1236 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1237 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1238 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1240 hdrsize += IEEE80211_ADDR_LEN;
1241 meshhdrsize = meshae = 0;
1242 #ifdef IEEE80211_SUPPORT_MESH
1246 * Honor driver DATAPAD requirement.
1248 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1249 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1253 if (__predict_true((m->m_flags & M_FF) == 0)) {
1257 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1259 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1262 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1263 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1264 llc = mtod(m, struct llc *);
1265 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1266 llc->llc_control = LLC_UI;
1267 llc->llc_snap.org_code[0] = 0;
1268 llc->llc_snap.org_code[1] = 0;
1269 llc->llc_snap.org_code[2] = 0;
1270 llc->llc_snap.ether_type = eh.ether_type;
1272 #ifdef IEEE80211_SUPPORT_SUPERG
1276 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1281 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1283 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1285 vap->iv_stats.is_tx_nobuf++;
1288 wh = mtod(m, struct ieee80211_frame *);
1289 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1290 *(uint16_t *)wh->i_dur = 0;
1291 qos = NULL; /* NB: quiet compiler */
1293 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1294 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1295 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1296 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1297 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1298 } else switch (vap->iv_opmode) {
1299 case IEEE80211_M_STA:
1300 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1301 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1302 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1303 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1305 case IEEE80211_M_IBSS:
1306 case IEEE80211_M_AHDEMO:
1307 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1308 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1309 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1311 * NB: always use the bssid from iv_bss as the
1312 * neighbor's may be stale after an ibss merge
1314 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1316 case IEEE80211_M_HOSTAP:
1317 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1318 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1319 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1320 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1322 #ifdef IEEE80211_SUPPORT_MESH
1323 case IEEE80211_M_MBSS:
1324 /* NB: offset by hdrspace to deal with DATAPAD */
1325 mc = (struct ieee80211_meshcntl_ae10 *)
1326 (mtod(m, uint8_t *) + hdrspace);
1329 case IEEE80211_MESH_AE_00: /* no proxy */
1331 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1332 IEEE80211_ADDR_COPY(wh->i_addr1,
1334 IEEE80211_ADDR_COPY(wh->i_addr2,
1336 IEEE80211_ADDR_COPY(wh->i_addr3,
1338 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1340 qos =((struct ieee80211_qosframe_addr4 *)
1342 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1344 IEEE80211_ADDR_COPY(wh->i_addr1,
1346 IEEE80211_ADDR_COPY(wh->i_addr2,
1348 IEEE80211_ADDR_COPY(wh->i_addr3,
1350 qos = ((struct ieee80211_qosframe *)
1354 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1355 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1356 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1357 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1358 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1360 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1362 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1364 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1365 KASSERT(rt != NULL, ("route is NULL"));
1366 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1367 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1368 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1369 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1370 mc->mc_flags = IEEE80211_MESH_AE_10;
1371 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1372 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1373 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1376 KASSERT(0, ("meshae %d", meshae));
1379 mc->mc_ttl = ms->ms_ttl;
1381 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1384 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1388 if (m->m_flags & M_MORE_DATA)
1389 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1394 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1395 /* NB: mesh case handled earlier */
1396 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1397 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1398 ac = M_WME_GETAC(m);
1399 /* map from access class/queue to 11e header priorty value */
1400 tid = WME_AC_TO_TID(ac);
1401 qos[0] = tid & IEEE80211_QOS_TID;
1402 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1403 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1404 #ifdef IEEE80211_SUPPORT_MESH
1405 if (vap->iv_opmode == IEEE80211_M_MBSS)
1406 qos[1] = IEEE80211_QOS_MC;
1410 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1412 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1414 * NB: don't assign a sequence # to potential
1415 * aggregates; we expect this happens at the
1416 * point the frame comes off any aggregation q
1417 * as otherwise we may introduce holes in the
1418 * BA sequence space and/or make window accouting
1421 * XXX may want to control this with a driver
1422 * capability; this may also change when we pull
1423 * aggregation up into net80211
1425 seqno = ni->ni_txseqs[tid]++;
1426 *(uint16_t *)wh->i_seq =
1427 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1428 M_SEQNO_SET(m, seqno);
1431 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1432 *(uint16_t *)wh->i_seq =
1433 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1434 M_SEQNO_SET(m, seqno);
1438 /* check if xmit fragmentation is required */
1439 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1440 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1441 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1442 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1445 * IEEE 802.1X: send EAPOL frames always in the clear.
1446 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1448 if ((m->m_flags & M_EAPOL) == 0 ||
1449 ((vap->iv_flags & IEEE80211_F_WPA) &&
1450 (vap->iv_opmode == IEEE80211_M_STA ?
1451 !IEEE80211_KEY_UNDEFINED(key) :
1452 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1453 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1454 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1455 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1457 "%s", "enmic failed, discard frame");
1458 vap->iv_stats.is_crypto_enmicfail++;
1463 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1464 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1467 m->m_flags |= M_ENCAP; /* mark encapsulated */
1469 IEEE80211_NODE_STAT(ni, tx_data);
1470 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1471 IEEE80211_NODE_STAT(ni, tx_mcast);
1472 m->m_flags |= M_MCAST;
1474 IEEE80211_NODE_STAT(ni, tx_ucast);
1475 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1487 * Fragment the frame according to the specified mtu.
1488 * The size of the 802.11 header (w/o padding) is provided
1489 * so we don't need to recalculate it. We create a new
1490 * mbuf for each fragment and chain it through m_nextpkt;
1491 * we might be able to optimize this by reusing the original
1492 * packet's mbufs but that is significantly more complicated.
1495 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1496 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1498 struct ieee80211_frame *wh, *whf;
1499 struct mbuf *m, *prev, *next;
1500 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1502 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1503 KASSERT(m0->m_pkthdr.len > mtu,
1504 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1506 wh = mtod(m0, struct ieee80211_frame *);
1507 /* NB: mark the first frag; it will be propagated below */
1508 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1509 totalhdrsize = hdrsize + ciphdrsize;
1511 off = mtu - ciphdrsize;
1512 remainder = m0->m_pkthdr.len - off;
1515 fragsize = totalhdrsize + remainder;
1518 /* XXX fragsize can be >2048! */
1519 KASSERT(fragsize < MCLBYTES,
1520 ("fragment size %u too big!", fragsize));
1521 if (fragsize > MHLEN)
1522 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1524 m = m_gethdr(M_NOWAIT, MT_DATA);
1527 /* leave room to prepend any cipher header */
1528 m_align(m, fragsize - ciphdrsize);
1531 * Form the header in the fragment. Note that since
1532 * we mark the first fragment with the MORE_FRAG bit
1533 * it automatically is propagated to each fragment; we
1534 * need only clear it on the last fragment (done below).
1535 * NB: frag 1+ dont have Mesh Control field present.
1537 whf = mtod(m, struct ieee80211_frame *);
1538 memcpy(whf, wh, hdrsize);
1539 #ifdef IEEE80211_SUPPORT_MESH
1540 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1541 if (IEEE80211_IS_DSTODS(wh))
1542 ((struct ieee80211_qosframe_addr4 *)
1543 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1545 ((struct ieee80211_qosframe *)
1546 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1549 *(uint16_t *)&whf->i_seq[0] |= htole16(
1550 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1551 IEEE80211_SEQ_FRAG_SHIFT);
1554 payload = fragsize - totalhdrsize;
1555 /* NB: destination is known to be contiguous */
1556 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1557 m->m_len = hdrsize + payload;
1558 m->m_pkthdr.len = hdrsize + payload;
1559 m->m_flags |= M_FRAG;
1561 /* chain up the fragment */
1562 prev->m_nextpkt = m;
1565 /* deduct fragment just formed */
1566 remainder -= payload;
1568 } while (remainder != 0);
1570 /* set the last fragment */
1571 m->m_flags |= M_LASTFRAG;
1572 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1574 /* strip first mbuf now that everything has been copied */
1575 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1576 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1578 vap->iv_stats.is_tx_fragframes++;
1579 vap->iv_stats.is_tx_frags += fragno-1;
1583 /* reclaim fragments but leave original frame for caller to free */
1584 for (m = m0->m_nextpkt; m != NULL; m = next) {
1585 next = m->m_nextpkt;
1586 m->m_nextpkt = NULL; /* XXX paranoid */
1589 m0->m_nextpkt = NULL;
1594 * Add a supported rates element id to a frame.
1597 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1601 *frm++ = IEEE80211_ELEMID_RATES;
1602 nrates = rs->rs_nrates;
1603 if (nrates > IEEE80211_RATE_SIZE)
1604 nrates = IEEE80211_RATE_SIZE;
1606 memcpy(frm, rs->rs_rates, nrates);
1607 return frm + nrates;
1611 * Add an extended supported rates element id to a frame.
1614 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1617 * Add an extended supported rates element if operating in 11g mode.
1619 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1620 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1621 *frm++ = IEEE80211_ELEMID_XRATES;
1623 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1630 * Add an ssid element to a frame.
1633 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1635 *frm++ = IEEE80211_ELEMID_SSID;
1637 memcpy(frm, ssid, len);
1642 * Add an erp element to a frame.
1645 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1649 *frm++ = IEEE80211_ELEMID_ERP;
1652 if (ic->ic_nonerpsta != 0)
1653 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1654 if (ic->ic_flags & IEEE80211_F_USEPROT)
1655 erp |= IEEE80211_ERP_USE_PROTECTION;
1656 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1657 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1663 * Add a CFParams element to a frame.
1666 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1668 #define ADDSHORT(frm, v) do { \
1669 LE_WRITE_2(frm, v); \
1672 *frm++ = IEEE80211_ELEMID_CFPARMS;
1674 *frm++ = 0; /* CFP count */
1675 *frm++ = 2; /* CFP period */
1676 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1677 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1682 static __inline uint8_t *
1683 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1685 memcpy(frm, ie->ie_data, ie->ie_len);
1686 return frm + ie->ie_len;
1689 static __inline uint8_t *
1690 add_ie(uint8_t *frm, const uint8_t *ie)
1692 memcpy(frm, ie, 2 + ie[1]);
1693 return frm + 2 + ie[1];
1696 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1698 * Add a WME information element to a frame.
1701 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1703 static const struct ieee80211_wme_info info = {
1704 .wme_id = IEEE80211_ELEMID_VENDOR,
1705 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1706 .wme_oui = { WME_OUI_BYTES },
1707 .wme_type = WME_OUI_TYPE,
1708 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1709 .wme_version = WME_VERSION,
1712 memcpy(frm, &info, sizeof(info));
1713 return frm + sizeof(info);
1717 * Add a WME parameters element to a frame.
1720 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1722 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1723 #define ADDSHORT(frm, v) do { \
1724 LE_WRITE_2(frm, v); \
1727 /* NB: this works 'cuz a param has an info at the front */
1728 static const struct ieee80211_wme_info param = {
1729 .wme_id = IEEE80211_ELEMID_VENDOR,
1730 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1731 .wme_oui = { WME_OUI_BYTES },
1732 .wme_type = WME_OUI_TYPE,
1733 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1734 .wme_version = WME_VERSION,
1738 memcpy(frm, ¶m, sizeof(param));
1739 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1740 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1741 *frm++ = 0; /* reserved field */
1742 for (i = 0; i < WME_NUM_AC; i++) {
1743 const struct wmeParams *ac =
1744 &wme->wme_bssChanParams.cap_wmeParams[i];
1745 *frm++ = SM(i, WME_PARAM_ACI)
1746 | SM(ac->wmep_acm, WME_PARAM_ACM)
1747 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1749 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1750 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1752 ADDSHORT(frm, ac->wmep_txopLimit);
1758 #undef WME_OUI_BYTES
1761 * Add an 11h Power Constraint element to a frame.
1764 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1766 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1767 /* XXX per-vap tx power limit? */
1768 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1770 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1772 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1777 * Add an 11h Power Capability element to a frame.
1780 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1782 frm[0] = IEEE80211_ELEMID_PWRCAP;
1784 frm[2] = c->ic_minpower;
1785 frm[3] = c->ic_maxpower;
1790 * Add an 11h Supported Channels element to a frame.
1793 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1795 static const int ielen = 26;
1797 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1799 /* XXX not correct */
1800 memcpy(frm+2, ic->ic_chan_avail, ielen);
1801 return frm + 2 + ielen;
1805 * Add an 11h Quiet time element to a frame.
1808 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
1810 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
1812 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
1814 if (vap->iv_quiet_count_value == 1)
1815 vap->iv_quiet_count_value = vap->iv_quiet_count;
1816 else if (vap->iv_quiet_count_value > 1)
1817 vap->iv_quiet_count_value--;
1819 if (vap->iv_quiet_count_value == 0) {
1820 /* value 0 is reserved as per 802.11h standerd */
1821 vap->iv_quiet_count_value = 1;
1824 quiet->tbttcount = vap->iv_quiet_count_value;
1825 quiet->period = vap->iv_quiet_period;
1826 quiet->duration = htole16(vap->iv_quiet_duration);
1827 quiet->offset = htole16(vap->iv_quiet_offset);
1828 return frm + sizeof(*quiet);
1832 * Add an 11h Channel Switch Announcement element to a frame.
1833 * Note that we use the per-vap CSA count to adjust the global
1834 * counter so we can use this routine to form probe response
1835 * frames and get the current count.
1838 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1840 struct ieee80211com *ic = vap->iv_ic;
1841 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1843 csa->csa_ie = IEEE80211_ELEMID_CSA;
1845 csa->csa_mode = 1; /* XXX force quiet on channel */
1846 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1847 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1848 return frm + sizeof(*csa);
1852 * Add an 11h country information element to a frame.
1855 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1858 if (ic->ic_countryie == NULL ||
1859 ic->ic_countryie_chan != ic->ic_bsschan) {
1861 * Handle lazy construction of ie. This is done on
1862 * first use and after a channel change that requires
1865 if (ic->ic_countryie != NULL)
1866 free(ic->ic_countryie, M_80211_NODE_IE);
1867 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1868 if (ic->ic_countryie == NULL)
1870 ic->ic_countryie_chan = ic->ic_bsschan;
1872 return add_appie(frm, ic->ic_countryie);
1876 * Send a probe request frame with the specified ssid
1877 * and any optional information element data.
1880 ieee80211_send_probereq(struct ieee80211_node *ni,
1881 const uint8_t sa[IEEE80211_ADDR_LEN],
1882 const uint8_t da[IEEE80211_ADDR_LEN],
1883 const uint8_t bssid[IEEE80211_ADDR_LEN],
1884 const uint8_t *ssid, size_t ssidlen)
1886 struct ieee80211vap *vap = ni->ni_vap;
1887 struct ieee80211com *ic = ni->ni_ic;
1888 const struct ieee80211_txparam *tp;
1889 struct ieee80211_bpf_params params;
1890 struct ieee80211_frame *wh;
1891 const struct ieee80211_rateset *rs;
1896 if (vap->iv_state == IEEE80211_S_CAC) {
1897 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1898 "block %s frame in CAC state", "probe request");
1899 vap->iv_stats.is_tx_badstate++;
1900 return EIO; /* XXX */
1904 * Hold a reference on the node so it doesn't go away until after
1905 * the xmit is complete all the way in the driver. On error we
1906 * will remove our reference.
1908 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1909 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1911 ni, ether_sprintf(ni->ni_macaddr),
1912 ieee80211_node_refcnt(ni)+1);
1913 ieee80211_ref_node(ni);
1916 * prreq frame format
1918 * [tlv] supported rates
1919 * [tlv] RSN (optional)
1920 * [tlv] extended supported rates
1921 * [tlv] WPA (optional)
1922 * [tlv] user-specified ie's
1924 m = ieee80211_getmgtframe(&frm,
1925 ic->ic_headroom + sizeof(struct ieee80211_frame),
1926 2 + IEEE80211_NWID_LEN
1927 + 2 + IEEE80211_RATE_SIZE
1928 + sizeof(struct ieee80211_ie_wpa)
1929 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1930 + sizeof(struct ieee80211_ie_wpa)
1931 + (vap->iv_appie_probereq != NULL ?
1932 vap->iv_appie_probereq->ie_len : 0)
1935 vap->iv_stats.is_tx_nobuf++;
1936 ieee80211_free_node(ni);
1940 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1941 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1942 frm = ieee80211_add_rates(frm, rs);
1943 if (vap->iv_flags & IEEE80211_F_WPA2) {
1944 if (vap->iv_rsn_ie != NULL)
1945 frm = add_ie(frm, vap->iv_rsn_ie);
1946 /* XXX else complain? */
1948 frm = ieee80211_add_xrates(frm, rs);
1949 if (vap->iv_flags & IEEE80211_F_WPA1) {
1950 if (vap->iv_wpa_ie != NULL)
1951 frm = add_ie(frm, vap->iv_wpa_ie);
1952 /* XXX else complain? */
1954 if (vap->iv_appie_probereq != NULL)
1955 frm = add_appie(frm, vap->iv_appie_probereq);
1956 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1958 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1959 ("leading space %zd", M_LEADINGSPACE(m)));
1960 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
1962 /* NB: cannot happen */
1963 ieee80211_free_node(ni);
1967 IEEE80211_TX_LOCK(ic);
1968 wh = mtod(m, struct ieee80211_frame *);
1969 ieee80211_send_setup(ni, m,
1970 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1971 IEEE80211_NONQOS_TID, sa, da, bssid);
1972 /* XXX power management? */
1973 m->m_flags |= M_ENCAP; /* mark encapsulated */
1975 M_WME_SETAC(m, WME_AC_BE);
1977 IEEE80211_NODE_STAT(ni, tx_probereq);
1978 IEEE80211_NODE_STAT(ni, tx_mgmt);
1980 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1981 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1982 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1985 memset(¶ms, 0, sizeof(params));
1986 params.ibp_pri = M_WME_GETAC(m);
1987 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1988 params.ibp_rate0 = tp->mgmtrate;
1989 if (IEEE80211_IS_MULTICAST(da)) {
1990 params.ibp_flags |= IEEE80211_BPF_NOACK;
1991 params.ibp_try0 = 1;
1993 params.ibp_try0 = tp->maxretry;
1994 params.ibp_power = ni->ni_txpower;
1995 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
1996 IEEE80211_TX_UNLOCK(ic);
2001 * Calculate capability information for mgt frames.
2004 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2006 struct ieee80211com *ic = vap->iv_ic;
2009 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2011 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2012 capinfo = IEEE80211_CAPINFO_ESS;
2013 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2014 capinfo = IEEE80211_CAPINFO_IBSS;
2017 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2018 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2019 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2020 IEEE80211_IS_CHAN_2GHZ(chan))
2021 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2022 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2023 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2024 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2025 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2030 * Send a management frame. The node is for the destination (or ic_bss
2031 * when in station mode). Nodes other than ic_bss have their reference
2032 * count bumped to reflect our use for an indeterminant time.
2035 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2037 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2038 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2039 struct ieee80211vap *vap = ni->ni_vap;
2040 struct ieee80211com *ic = ni->ni_ic;
2041 struct ieee80211_node *bss = vap->iv_bss;
2042 struct ieee80211_bpf_params params;
2046 int has_challenge, is_shared_key, ret, status;
2048 KASSERT(ni != NULL, ("null node"));
2051 * Hold a reference on the node so it doesn't go away until after
2052 * the xmit is complete all the way in the driver. On error we
2053 * will remove our reference.
2055 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2056 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2058 ni, ether_sprintf(ni->ni_macaddr),
2059 ieee80211_node_refcnt(ni)+1);
2060 ieee80211_ref_node(ni);
2062 memset(¶ms, 0, sizeof(params));
2065 case IEEE80211_FC0_SUBTYPE_AUTH:
2068 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2069 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2070 ni->ni_challenge != NULL);
2073 * Deduce whether we're doing open authentication or
2074 * shared key authentication. We do the latter if
2075 * we're in the middle of a shared key authentication
2076 * handshake or if we're initiating an authentication
2077 * request and configured to use shared key.
2079 is_shared_key = has_challenge ||
2080 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2081 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2082 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2084 m = ieee80211_getmgtframe(&frm,
2085 ic->ic_headroom + sizeof(struct ieee80211_frame),
2086 3 * sizeof(uint16_t)
2087 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2088 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2091 senderr(ENOMEM, is_tx_nobuf);
2093 ((uint16_t *)frm)[0] =
2094 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2095 : htole16(IEEE80211_AUTH_ALG_OPEN);
2096 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2097 ((uint16_t *)frm)[2] = htole16(status);/* status */
2099 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2100 ((uint16_t *)frm)[3] =
2101 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2102 IEEE80211_ELEMID_CHALLENGE);
2103 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2104 IEEE80211_CHALLENGE_LEN);
2105 m->m_pkthdr.len = m->m_len =
2106 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2107 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2108 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2109 "request encrypt frame (%s)", __func__);
2110 /* mark frame for encryption */
2111 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2114 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2116 /* XXX not right for shared key */
2117 if (status == IEEE80211_STATUS_SUCCESS)
2118 IEEE80211_NODE_STAT(ni, tx_auth);
2120 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2122 if (vap->iv_opmode == IEEE80211_M_STA)
2123 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2124 (void *) vap->iv_state);
2127 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2128 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2129 "send station deauthenticate (reason %d)", arg);
2130 m = ieee80211_getmgtframe(&frm,
2131 ic->ic_headroom + sizeof(struct ieee80211_frame),
2134 senderr(ENOMEM, is_tx_nobuf);
2135 *(uint16_t *)frm = htole16(arg); /* reason */
2136 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2138 IEEE80211_NODE_STAT(ni, tx_deauth);
2139 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2141 ieee80211_node_unauthorize(ni); /* port closed */
2144 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2145 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2147 * asreq frame format
2148 * [2] capability information
2149 * [2] listen interval
2150 * [6*] current AP address (reassoc only)
2152 * [tlv] supported rates
2153 * [tlv] extended supported rates
2154 * [4] power capability (optional)
2155 * [28] supported channels (optional)
2156 * [tlv] HT capabilities
2157 * [tlv] WME (optional)
2158 * [tlv] Vendor OUI HT capabilities (optional)
2159 * [tlv] Atheros capabilities (if negotiated)
2160 * [tlv] AppIE's (optional)
2162 m = ieee80211_getmgtframe(&frm,
2163 ic->ic_headroom + sizeof(struct ieee80211_frame),
2166 + IEEE80211_ADDR_LEN
2167 + 2 + IEEE80211_NWID_LEN
2168 + 2 + IEEE80211_RATE_SIZE
2169 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2172 + sizeof(struct ieee80211_wme_info)
2173 + sizeof(struct ieee80211_ie_htcap)
2174 + 4 + sizeof(struct ieee80211_ie_htcap)
2175 #ifdef IEEE80211_SUPPORT_SUPERG
2176 + sizeof(struct ieee80211_ath_ie)
2178 + (vap->iv_appie_wpa != NULL ?
2179 vap->iv_appie_wpa->ie_len : 0)
2180 + (vap->iv_appie_assocreq != NULL ?
2181 vap->iv_appie_assocreq->ie_len : 0)
2184 senderr(ENOMEM, is_tx_nobuf);
2186 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2187 ("wrong mode %u", vap->iv_opmode));
2188 capinfo = IEEE80211_CAPINFO_ESS;
2189 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2190 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2192 * NB: Some 11a AP's reject the request when
2193 * short premable is set.
2195 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2196 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2197 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2198 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2199 (ic->ic_caps & IEEE80211_C_SHSLOT))
2200 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2201 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2202 (vap->iv_flags & IEEE80211_F_DOTH))
2203 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2204 *(uint16_t *)frm = htole16(capinfo);
2207 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2208 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2212 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2213 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2214 frm += IEEE80211_ADDR_LEN;
2217 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2218 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2219 if (vap->iv_flags & IEEE80211_F_WPA2) {
2220 if (vap->iv_rsn_ie != NULL)
2221 frm = add_ie(frm, vap->iv_rsn_ie);
2222 /* XXX else complain? */
2224 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2225 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2226 frm = ieee80211_add_powercapability(frm,
2228 frm = ieee80211_add_supportedchannels(frm, ic);
2230 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2231 ni->ni_ies.htcap_ie != NULL &&
2232 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2233 frm = ieee80211_add_htcap(frm, ni);
2234 if (vap->iv_flags & IEEE80211_F_WPA1) {
2235 if (vap->iv_wpa_ie != NULL)
2236 frm = add_ie(frm, vap->iv_wpa_ie);
2237 /* XXX else complain */
2239 if ((ic->ic_flags & IEEE80211_F_WME) &&
2240 ni->ni_ies.wme_ie != NULL)
2241 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2242 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2243 ni->ni_ies.htcap_ie != NULL &&
2244 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2245 frm = ieee80211_add_htcap_vendor(frm, ni);
2246 #ifdef IEEE80211_SUPPORT_SUPERG
2247 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2248 frm = ieee80211_add_ath(frm,
2249 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2250 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2251 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2252 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2254 #endif /* IEEE80211_SUPPORT_SUPERG */
2255 if (vap->iv_appie_assocreq != NULL)
2256 frm = add_appie(frm, vap->iv_appie_assocreq);
2257 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2259 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2260 (void *) vap->iv_state);
2263 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2264 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2266 * asresp frame format
2267 * [2] capability information
2269 * [2] association ID
2270 * [tlv] supported rates
2271 * [tlv] extended supported rates
2272 * [tlv] HT capabilities (standard, if STA enabled)
2273 * [tlv] HT information (standard, if STA enabled)
2274 * [tlv] WME (if configured and STA enabled)
2275 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2276 * [tlv] HT information (vendor OUI, if STA enabled)
2277 * [tlv] Atheros capabilities (if STA enabled)
2278 * [tlv] AppIE's (optional)
2280 m = ieee80211_getmgtframe(&frm,
2281 ic->ic_headroom + sizeof(struct ieee80211_frame),
2285 + 2 + IEEE80211_RATE_SIZE
2286 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2287 + sizeof(struct ieee80211_ie_htcap) + 4
2288 + sizeof(struct ieee80211_ie_htinfo) + 4
2289 + sizeof(struct ieee80211_wme_param)
2290 #ifdef IEEE80211_SUPPORT_SUPERG
2291 + sizeof(struct ieee80211_ath_ie)
2293 + (vap->iv_appie_assocresp != NULL ?
2294 vap->iv_appie_assocresp->ie_len : 0)
2297 senderr(ENOMEM, is_tx_nobuf);
2299 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2300 *(uint16_t *)frm = htole16(capinfo);
2303 *(uint16_t *)frm = htole16(arg); /* status */
2306 if (arg == IEEE80211_STATUS_SUCCESS) {
2307 *(uint16_t *)frm = htole16(ni->ni_associd);
2308 IEEE80211_NODE_STAT(ni, tx_assoc);
2310 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2313 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2314 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2315 /* NB: respond according to what we received */
2316 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2317 frm = ieee80211_add_htcap(frm, ni);
2318 frm = ieee80211_add_htinfo(frm, ni);
2320 if ((vap->iv_flags & IEEE80211_F_WME) &&
2321 ni->ni_ies.wme_ie != NULL)
2322 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2323 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2324 frm = ieee80211_add_htcap_vendor(frm, ni);
2325 frm = ieee80211_add_htinfo_vendor(frm, ni);
2327 #ifdef IEEE80211_SUPPORT_SUPERG
2328 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2329 frm = ieee80211_add_ath(frm,
2330 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2331 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2332 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2333 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2334 #endif /* IEEE80211_SUPPORT_SUPERG */
2335 if (vap->iv_appie_assocresp != NULL)
2336 frm = add_appie(frm, vap->iv_appie_assocresp);
2337 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2340 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2341 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2342 "send station disassociate (reason %d)", arg);
2343 m = ieee80211_getmgtframe(&frm,
2344 ic->ic_headroom + sizeof(struct ieee80211_frame),
2347 senderr(ENOMEM, is_tx_nobuf);
2348 *(uint16_t *)frm = htole16(arg); /* reason */
2349 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2351 IEEE80211_NODE_STAT(ni, tx_disassoc);
2352 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2356 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2357 "invalid mgmt frame type %u", type);
2358 senderr(EINVAL, is_tx_unknownmgt);
2362 /* NB: force non-ProbeResp frames to the highest queue */
2363 params.ibp_pri = WME_AC_VO;
2364 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2365 /* NB: we know all frames are unicast */
2366 params.ibp_try0 = bss->ni_txparms->maxretry;
2367 params.ibp_power = bss->ni_txpower;
2368 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2370 ieee80211_free_node(ni);
2377 * Return an mbuf with a probe response frame in it.
2378 * Space is left to prepend and 802.11 header at the
2379 * front but it's left to the caller to fill in.
2382 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2384 struct ieee80211vap *vap = bss->ni_vap;
2385 struct ieee80211com *ic = bss->ni_ic;
2386 const struct ieee80211_rateset *rs;
2392 * probe response frame format
2394 * [2] beacon interval
2395 * [2] cabability information
2397 * [tlv] supported rates
2398 * [tlv] parameter set (FH/DS)
2399 * [tlv] parameter set (IBSS)
2400 * [tlv] country (optional)
2401 * [3] power control (optional)
2402 * [5] channel switch announcement (CSA) (optional)
2403 * [tlv] extended rate phy (ERP)
2404 * [tlv] extended supported rates
2405 * [tlv] RSN (optional)
2406 * [tlv] HT capabilities
2407 * [tlv] HT information
2408 * [tlv] WPA (optional)
2409 * [tlv] WME (optional)
2410 * [tlv] Vendor OUI HT capabilities (optional)
2411 * [tlv] Vendor OUI HT information (optional)
2412 * [tlv] Atheros capabilities
2413 * [tlv] AppIE's (optional)
2414 * [tlv] Mesh ID (MBSS)
2415 * [tlv] Mesh Conf (MBSS)
2417 m = ieee80211_getmgtframe(&frm,
2418 ic->ic_headroom + sizeof(struct ieee80211_frame),
2422 + 2 + IEEE80211_NWID_LEN
2423 + 2 + IEEE80211_RATE_SIZE
2425 + IEEE80211_COUNTRY_MAX_SIZE
2427 + sizeof(struct ieee80211_csa_ie)
2428 + sizeof(struct ieee80211_quiet_ie)
2430 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2431 + sizeof(struct ieee80211_ie_wpa)
2432 + sizeof(struct ieee80211_ie_htcap)
2433 + sizeof(struct ieee80211_ie_htinfo)
2434 + sizeof(struct ieee80211_ie_wpa)
2435 + sizeof(struct ieee80211_wme_param)
2436 + 4 + sizeof(struct ieee80211_ie_htcap)
2437 + 4 + sizeof(struct ieee80211_ie_htinfo)
2438 #ifdef IEEE80211_SUPPORT_SUPERG
2439 + sizeof(struct ieee80211_ath_ie)
2441 #ifdef IEEE80211_SUPPORT_MESH
2442 + 2 + IEEE80211_MESHID_LEN
2443 + sizeof(struct ieee80211_meshconf_ie)
2445 + (vap->iv_appie_proberesp != NULL ?
2446 vap->iv_appie_proberesp->ie_len : 0)
2449 vap->iv_stats.is_tx_nobuf++;
2453 memset(frm, 0, 8); /* timestamp should be filled later */
2455 *(uint16_t *)frm = htole16(bss->ni_intval);
2457 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2458 *(uint16_t *)frm = htole16(capinfo);
2461 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2462 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2463 frm = ieee80211_add_rates(frm, rs);
2465 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2466 *frm++ = IEEE80211_ELEMID_FHPARMS;
2468 *frm++ = bss->ni_fhdwell & 0x00ff;
2469 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2470 *frm++ = IEEE80211_FH_CHANSET(
2471 ieee80211_chan2ieee(ic, bss->ni_chan));
2472 *frm++ = IEEE80211_FH_CHANPAT(
2473 ieee80211_chan2ieee(ic, bss->ni_chan));
2474 *frm++ = bss->ni_fhindex;
2476 *frm++ = IEEE80211_ELEMID_DSPARMS;
2478 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2481 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2482 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2484 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2486 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2487 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2488 frm = ieee80211_add_countryie(frm, ic);
2489 if (vap->iv_flags & IEEE80211_F_DOTH) {
2490 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2491 frm = ieee80211_add_powerconstraint(frm, vap);
2492 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2493 frm = ieee80211_add_csa(frm, vap);
2495 if (vap->iv_flags & IEEE80211_F_DOTH) {
2496 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2497 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2499 frm = ieee80211_add_quiet(frm, vap);
2502 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2503 frm = ieee80211_add_erp(frm, ic);
2504 frm = ieee80211_add_xrates(frm, rs);
2505 if (vap->iv_flags & IEEE80211_F_WPA2) {
2506 if (vap->iv_rsn_ie != NULL)
2507 frm = add_ie(frm, vap->iv_rsn_ie);
2508 /* XXX else complain? */
2511 * NB: legacy 11b clients do not get certain ie's.
2512 * The caller identifies such clients by passing
2513 * a token in legacy to us. Could expand this to be
2514 * any legacy client for stuff like HT ie's.
2516 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2517 legacy != IEEE80211_SEND_LEGACY_11B) {
2518 frm = ieee80211_add_htcap(frm, bss);
2519 frm = ieee80211_add_htinfo(frm, bss);
2521 if (vap->iv_flags & IEEE80211_F_WPA1) {
2522 if (vap->iv_wpa_ie != NULL)
2523 frm = add_ie(frm, vap->iv_wpa_ie);
2524 /* XXX else complain? */
2526 if (vap->iv_flags & IEEE80211_F_WME)
2527 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2528 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2529 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2530 legacy != IEEE80211_SEND_LEGACY_11B) {
2531 frm = ieee80211_add_htcap_vendor(frm, bss);
2532 frm = ieee80211_add_htinfo_vendor(frm, bss);
2534 #ifdef IEEE80211_SUPPORT_SUPERG
2535 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2536 legacy != IEEE80211_SEND_LEGACY_11B)
2537 frm = ieee80211_add_athcaps(frm, bss);
2539 if (vap->iv_appie_proberesp != NULL)
2540 frm = add_appie(frm, vap->iv_appie_proberesp);
2541 #ifdef IEEE80211_SUPPORT_MESH
2542 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2543 frm = ieee80211_add_meshid(frm, vap);
2544 frm = ieee80211_add_meshconf(frm, vap);
2547 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2553 * Send a probe response frame to the specified mac address.
2554 * This does not go through the normal mgt frame api so we
2555 * can specify the destination address and re-use the bss node
2556 * for the sta reference.
2559 ieee80211_send_proberesp(struct ieee80211vap *vap,
2560 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2562 struct ieee80211_node *bss = vap->iv_bss;
2563 struct ieee80211com *ic = vap->iv_ic;
2564 struct ieee80211_frame *wh;
2568 if (vap->iv_state == IEEE80211_S_CAC) {
2569 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2570 "block %s frame in CAC state", "probe response");
2571 vap->iv_stats.is_tx_badstate++;
2572 return EIO; /* XXX */
2576 * Hold a reference on the node so it doesn't go away until after
2577 * the xmit is complete all the way in the driver. On error we
2578 * will remove our reference.
2580 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2581 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2582 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2583 ieee80211_node_refcnt(bss)+1);
2584 ieee80211_ref_node(bss);
2586 m = ieee80211_alloc_proberesp(bss, legacy);
2588 ieee80211_free_node(bss);
2592 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2593 KASSERT(m != NULL, ("no room for header"));
2595 IEEE80211_TX_LOCK(ic);
2596 wh = mtod(m, struct ieee80211_frame *);
2597 ieee80211_send_setup(bss, m,
2598 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2599 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2600 /* XXX power management? */
2601 m->m_flags |= M_ENCAP; /* mark encapsulated */
2603 M_WME_SETAC(m, WME_AC_BE);
2605 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2606 "send probe resp on channel %u to %s%s\n",
2607 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2608 legacy ? " <legacy>" : "");
2609 IEEE80211_NODE_STAT(bss, tx_mgmt);
2611 ret = ieee80211_raw_output(vap, bss, m, NULL);
2612 IEEE80211_TX_UNLOCK(ic);
2617 * Allocate and build a RTS (Request To Send) control frame.
2620 ieee80211_alloc_rts(struct ieee80211com *ic,
2621 const uint8_t ra[IEEE80211_ADDR_LEN],
2622 const uint8_t ta[IEEE80211_ADDR_LEN],
2625 struct ieee80211_frame_rts *rts;
2628 /* XXX honor ic_headroom */
2629 m = m_gethdr(M_NOWAIT, MT_DATA);
2631 rts = mtod(m, struct ieee80211_frame_rts *);
2632 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2633 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2634 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2635 *(u_int16_t *)rts->i_dur = htole16(dur);
2636 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2637 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2639 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2645 * Allocate and build a CTS (Clear To Send) control frame.
2648 ieee80211_alloc_cts(struct ieee80211com *ic,
2649 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2651 struct ieee80211_frame_cts *cts;
2654 /* XXX honor ic_headroom */
2655 m = m_gethdr(M_NOWAIT, MT_DATA);
2657 cts = mtod(m, struct ieee80211_frame_cts *);
2658 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2659 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2660 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2661 *(u_int16_t *)cts->i_dur = htole16(dur);
2662 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2664 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2670 ieee80211_tx_mgt_timeout(void *arg)
2672 struct ieee80211_node *ni = arg;
2673 struct ieee80211vap *vap = ni->ni_vap;
2675 if (vap->iv_state != IEEE80211_S_INIT &&
2676 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2678 * NB: it's safe to specify a timeout as the reason here;
2679 * it'll only be used in the right state.
2681 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2682 IEEE80211_SCAN_FAIL_TIMEOUT);
2687 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2689 struct ieee80211vap *vap = ni->ni_vap;
2690 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2693 * Frame transmit completed; arrange timer callback. If
2694 * transmit was successfuly we wait for response. Otherwise
2695 * we arrange an immediate callback instead of doing the
2696 * callback directly since we don't know what state the driver
2697 * is in (e.g. what locks it is holding). This work should
2698 * not be too time-critical and not happen too often so the
2699 * added overhead is acceptable.
2701 * XXX what happens if !acked but response shows up before callback?
2703 if (vap->iv_state == ostate)
2704 callout_reset(&vap->iv_mgtsend,
2705 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2706 ieee80211_tx_mgt_timeout, ni);
2710 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2711 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2713 struct ieee80211vap *vap = ni->ni_vap;
2714 struct ieee80211com *ic = ni->ni_ic;
2715 struct ieee80211_rateset *rs = &ni->ni_rates;
2719 * beacon frame format
2721 * [2] beacon interval
2722 * [2] cabability information
2724 * [tlv] supported rates
2725 * [3] parameter set (DS)
2726 * [8] CF parameter set (optional)
2727 * [tlv] parameter set (IBSS/TIM)
2728 * [tlv] country (optional)
2729 * [3] power control (optional)
2730 * [5] channel switch announcement (CSA) (optional)
2731 * [tlv] extended rate phy (ERP)
2732 * [tlv] extended supported rates
2733 * [tlv] RSN parameters
2734 * [tlv] HT capabilities
2735 * [tlv] HT information
2736 * XXX Vendor-specific OIDs (e.g. Atheros)
2737 * [tlv] WPA parameters
2738 * [tlv] WME parameters
2739 * [tlv] Vendor OUI HT capabilities (optional)
2740 * [tlv] Vendor OUI HT information (optional)
2741 * [tlv] Atheros capabilities (optional)
2742 * [tlv] TDMA parameters (optional)
2743 * [tlv] Mesh ID (MBSS)
2744 * [tlv] Mesh Conf (MBSS)
2745 * [tlv] application data (optional)
2748 memset(bo, 0, sizeof(*bo));
2750 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2752 *(uint16_t *)frm = htole16(ni->ni_intval);
2754 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2755 bo->bo_caps = (uint16_t *)frm;
2756 *(uint16_t *)frm = htole16(capinfo);
2758 *frm++ = IEEE80211_ELEMID_SSID;
2759 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2760 *frm++ = ni->ni_esslen;
2761 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2762 frm += ni->ni_esslen;
2765 frm = ieee80211_add_rates(frm, rs);
2766 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2767 *frm++ = IEEE80211_ELEMID_DSPARMS;
2769 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2771 if (ic->ic_flags & IEEE80211_F_PCF) {
2773 frm = ieee80211_add_cfparms(frm, ic);
2776 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2777 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2779 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2781 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2782 vap->iv_opmode == IEEE80211_M_MBSS) {
2783 /* TIM IE is the same for Mesh and Hostap */
2784 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2786 tie->tim_ie = IEEE80211_ELEMID_TIM;
2787 tie->tim_len = 4; /* length */
2788 tie->tim_count = 0; /* DTIM count */
2789 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2790 tie->tim_bitctl = 0; /* bitmap control */
2791 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2792 frm += sizeof(struct ieee80211_tim_ie);
2795 bo->bo_tim_trailer = frm;
2796 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2797 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2798 frm = ieee80211_add_countryie(frm, ic);
2799 if (vap->iv_flags & IEEE80211_F_DOTH) {
2800 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2801 frm = ieee80211_add_powerconstraint(frm, vap);
2803 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2804 frm = ieee80211_add_csa(frm, vap);
2808 if (vap->iv_flags & IEEE80211_F_DOTH) {
2810 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2811 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2813 frm = ieee80211_add_quiet(frm,vap);
2818 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2820 frm = ieee80211_add_erp(frm, ic);
2822 frm = ieee80211_add_xrates(frm, rs);
2823 if (vap->iv_flags & IEEE80211_F_WPA2) {
2824 if (vap->iv_rsn_ie != NULL)
2825 frm = add_ie(frm, vap->iv_rsn_ie);
2826 /* XXX else complain */
2828 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2829 frm = ieee80211_add_htcap(frm, ni);
2830 bo->bo_htinfo = frm;
2831 frm = ieee80211_add_htinfo(frm, ni);
2833 if (vap->iv_flags & IEEE80211_F_WPA1) {
2834 if (vap->iv_wpa_ie != NULL)
2835 frm = add_ie(frm, vap->iv_wpa_ie);
2836 /* XXX else complain */
2838 if (vap->iv_flags & IEEE80211_F_WME) {
2840 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2842 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2843 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2844 frm = ieee80211_add_htcap_vendor(frm, ni);
2845 frm = ieee80211_add_htinfo_vendor(frm, ni);
2847 #ifdef IEEE80211_SUPPORT_SUPERG
2848 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2850 frm = ieee80211_add_athcaps(frm, ni);
2853 #ifdef IEEE80211_SUPPORT_TDMA
2854 if (vap->iv_caps & IEEE80211_C_TDMA) {
2856 frm = ieee80211_add_tdma(frm, vap);
2859 if (vap->iv_appie_beacon != NULL) {
2861 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2862 frm = add_appie(frm, vap->iv_appie_beacon);
2864 #ifdef IEEE80211_SUPPORT_MESH
2865 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2866 frm = ieee80211_add_meshid(frm, vap);
2867 bo->bo_meshconf = frm;
2868 frm = ieee80211_add_meshconf(frm, vap);
2871 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2872 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2873 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2877 * Allocate a beacon frame and fillin the appropriate bits.
2880 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2881 struct ieee80211_beacon_offsets *bo)
2883 struct ieee80211vap *vap = ni->ni_vap;
2884 struct ieee80211com *ic = ni->ni_ic;
2885 struct ifnet *ifp = vap->iv_ifp;
2886 struct ieee80211_frame *wh;
2892 * beacon frame format
2894 * [2] beacon interval
2895 * [2] cabability information
2897 * [tlv] supported rates
2898 * [3] parameter set (DS)
2899 * [8] CF parameter set (optional)
2900 * [tlv] parameter set (IBSS/TIM)
2901 * [tlv] country (optional)
2902 * [3] power control (optional)
2903 * [5] channel switch announcement (CSA) (optional)
2904 * [tlv] extended rate phy (ERP)
2905 * [tlv] extended supported rates
2906 * [tlv] RSN parameters
2907 * [tlv] HT capabilities
2908 * [tlv] HT information
2909 * [tlv] Vendor OUI HT capabilities (optional)
2910 * [tlv] Vendor OUI HT information (optional)
2911 * XXX Vendor-specific OIDs (e.g. Atheros)
2912 * [tlv] WPA parameters
2913 * [tlv] WME parameters
2914 * [tlv] TDMA parameters (optional)
2915 * [tlv] Mesh ID (MBSS)
2916 * [tlv] Mesh Conf (MBSS)
2917 * [tlv] application data (optional)
2918 * NB: we allocate the max space required for the TIM bitmap.
2919 * XXX how big is this?
2921 pktlen = 8 /* time stamp */
2922 + sizeof(uint16_t) /* beacon interval */
2923 + sizeof(uint16_t) /* capabilities */
2924 + 2 + ni->ni_esslen /* ssid */
2925 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2926 + 2 + 1 /* DS parameters */
2927 + 2 + 6 /* CF parameters */
2928 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2929 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2930 + 2 + 1 /* power control */
2931 + sizeof(struct ieee80211_csa_ie) /* CSA */
2932 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
2934 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2935 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2936 2*sizeof(struct ieee80211_ie_wpa) : 0)
2937 /* XXX conditional? */
2938 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2939 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2940 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2941 sizeof(struct ieee80211_wme_param) : 0)
2942 #ifdef IEEE80211_SUPPORT_SUPERG
2943 + sizeof(struct ieee80211_ath_ie) /* ATH */
2945 #ifdef IEEE80211_SUPPORT_TDMA
2946 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2947 sizeof(struct ieee80211_tdma_param) : 0)
2949 #ifdef IEEE80211_SUPPORT_MESH
2950 + 2 + ni->ni_meshidlen
2951 + sizeof(struct ieee80211_meshconf_ie)
2953 + IEEE80211_MAX_APPIE
2955 m = ieee80211_getmgtframe(&frm,
2956 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2958 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2959 "%s: cannot get buf; size %u\n", __func__, pktlen);
2960 vap->iv_stats.is_tx_nobuf++;
2963 ieee80211_beacon_construct(m, frm, bo, ni);
2965 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2966 KASSERT(m != NULL, ("no space for 802.11 header?"));
2967 wh = mtod(m, struct ieee80211_frame *);
2968 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2969 IEEE80211_FC0_SUBTYPE_BEACON;
2970 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2971 *(uint16_t *)wh->i_dur = 0;
2972 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2973 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2974 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2975 *(uint16_t *)wh->i_seq = 0;
2981 * Update the dynamic parts of a beacon frame based on the current state.
2984 ieee80211_beacon_update(struct ieee80211_node *ni,
2985 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2987 struct ieee80211vap *vap = ni->ni_vap;
2988 struct ieee80211com *ic = ni->ni_ic;
2989 int len_changed = 0;
2991 struct ieee80211_frame *wh;
2992 ieee80211_seq seqno;
2996 * Handle 11h channel change when we've reached the count.
2997 * We must recalculate the beacon frame contents to account
2998 * for the new channel. Note we do this only for the first
2999 * vap that reaches this point; subsequent vaps just update
3000 * their beacon state to reflect the recalculated channel.
3002 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3003 vap->iv_csa_count == ic->ic_csa_count) {
3004 vap->iv_csa_count = 0;
3006 * Effect channel change before reconstructing the beacon
3007 * frame contents as many places reference ni_chan.
3009 if (ic->ic_csa_newchan != NULL)
3010 ieee80211_csa_completeswitch(ic);
3012 * NB: ieee80211_beacon_construct clears all pending
3013 * updates in bo_flags so we don't need to explicitly
3014 * clear IEEE80211_BEACON_CSA.
3016 ieee80211_beacon_construct(m,
3017 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
3019 /* XXX do WME aggressive mode processing? */
3020 IEEE80211_UNLOCK(ic);
3021 return 1; /* just assume length changed */
3024 wh = mtod(m, struct ieee80211_frame *);
3025 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3026 *(uint16_t *)&wh->i_seq[0] =
3027 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3028 M_SEQNO_SET(m, seqno);
3030 /* XXX faster to recalculate entirely or just changes? */
3031 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3032 *bo->bo_caps = htole16(capinfo);
3034 if (vap->iv_flags & IEEE80211_F_WME) {
3035 struct ieee80211_wme_state *wme = &ic->ic_wme;
3038 * Check for agressive mode change. When there is
3039 * significant high priority traffic in the BSS
3040 * throttle back BE traffic by using conservative
3041 * parameters. Otherwise BE uses agressive params
3042 * to optimize performance of legacy/non-QoS traffic.
3044 if (wme->wme_flags & WME_F_AGGRMODE) {
3045 if (wme->wme_hipri_traffic >
3046 wme->wme_hipri_switch_thresh) {
3047 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3048 "%s: traffic %u, disable aggressive mode\n",
3049 __func__, wme->wme_hipri_traffic);
3050 wme->wme_flags &= ~WME_F_AGGRMODE;
3051 ieee80211_wme_updateparams_locked(vap);
3052 wme->wme_hipri_traffic =
3053 wme->wme_hipri_switch_hysteresis;
3055 wme->wme_hipri_traffic = 0;
3057 if (wme->wme_hipri_traffic <=
3058 wme->wme_hipri_switch_thresh) {
3059 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3060 "%s: traffic %u, enable aggressive mode\n",
3061 __func__, wme->wme_hipri_traffic);
3062 wme->wme_flags |= WME_F_AGGRMODE;
3063 ieee80211_wme_updateparams_locked(vap);
3064 wme->wme_hipri_traffic = 0;
3066 wme->wme_hipri_traffic =
3067 wme->wme_hipri_switch_hysteresis;
3069 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3070 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
3071 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3075 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3076 ieee80211_ht_update_beacon(vap, bo);
3077 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3079 #ifdef IEEE80211_SUPPORT_TDMA
3080 if (vap->iv_caps & IEEE80211_C_TDMA) {
3082 * NB: the beacon is potentially updated every TBTT.
3084 ieee80211_tdma_update_beacon(vap, bo);
3087 #ifdef IEEE80211_SUPPORT_MESH
3088 if (vap->iv_opmode == IEEE80211_M_MBSS)
3089 ieee80211_mesh_update_beacon(vap, bo);
3092 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3093 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3094 struct ieee80211_tim_ie *tie =
3095 (struct ieee80211_tim_ie *) bo->bo_tim;
3096 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3097 u_int timlen, timoff, i;
3099 * ATIM/DTIM needs updating. If it fits in the
3100 * current space allocated then just copy in the
3101 * new bits. Otherwise we need to move any trailing
3102 * data to make room. Note that we know there is
3103 * contiguous space because ieee80211_beacon_allocate
3104 * insures there is space in the mbuf to write a
3105 * maximal-size virtual bitmap (based on iv_max_aid).
3108 * Calculate the bitmap size and offset, copy any
3109 * trailer out of the way, and then copy in the
3110 * new bitmap and update the information element.
3111 * Note that the tim bitmap must contain at least
3112 * one byte and any offset must be even.
3114 if (vap->iv_ps_pending != 0) {
3115 timoff = 128; /* impossibly large */
3116 for (i = 0; i < vap->iv_tim_len; i++)
3117 if (vap->iv_tim_bitmap[i]) {
3121 KASSERT(timoff != 128, ("tim bitmap empty!"));
3122 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3123 if (vap->iv_tim_bitmap[i])
3125 timlen = 1 + (i - timoff);
3130 if (timlen != bo->bo_tim_len) {
3131 /* copy up/down trailer */
3132 int adjust = tie->tim_bitmap+timlen
3133 - bo->bo_tim_trailer;
3134 ovbcopy(bo->bo_tim_trailer,
3135 bo->bo_tim_trailer+adjust,
3136 bo->bo_tim_trailer_len);
3137 bo->bo_tim_trailer += adjust;
3138 bo->bo_erp += adjust;
3139 bo->bo_htinfo += adjust;
3140 #ifdef IEEE80211_SUPPORT_SUPERG
3141 bo->bo_ath += adjust;
3143 #ifdef IEEE80211_SUPPORT_TDMA
3144 bo->bo_tdma += adjust;
3146 #ifdef IEEE80211_SUPPORT_MESH
3147 bo->bo_meshconf += adjust;
3149 bo->bo_appie += adjust;
3150 bo->bo_wme += adjust;
3151 bo->bo_csa += adjust;
3152 bo->bo_quiet += adjust;
3153 bo->bo_tim_len = timlen;
3155 /* update information element */
3156 tie->tim_len = 3 + timlen;
3157 tie->tim_bitctl = timoff;
3160 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3163 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3165 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3166 "%s: TIM updated, pending %u, off %u, len %u\n",
3167 __func__, vap->iv_ps_pending, timoff, timlen);
3169 /* count down DTIM period */
3170 if (tie->tim_count == 0)
3171 tie->tim_count = tie->tim_period - 1;
3174 /* update state for buffered multicast frames on DTIM */
3175 if (mcast && tie->tim_count == 0)
3176 tie->tim_bitctl |= 1;
3178 tie->tim_bitctl &= ~1;
3179 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3180 struct ieee80211_csa_ie *csa =
3181 (struct ieee80211_csa_ie *) bo->bo_csa;
3184 * Insert or update CSA ie. If we're just starting
3185 * to count down to the channel switch then we need
3186 * to insert the CSA ie. Otherwise we just need to
3187 * drop the count. The actual change happens above
3188 * when the vap's count reaches the target count.
3190 if (vap->iv_csa_count == 0) {
3191 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3192 bo->bo_erp += sizeof(*csa);
3193 bo->bo_htinfo += sizeof(*csa);
3194 bo->bo_wme += sizeof(*csa);
3195 #ifdef IEEE80211_SUPPORT_SUPERG
3196 bo->bo_ath += sizeof(*csa);
3198 #ifdef IEEE80211_SUPPORT_TDMA
3199 bo->bo_tdma += sizeof(*csa);
3201 #ifdef IEEE80211_SUPPORT_MESH
3202 bo->bo_meshconf += sizeof(*csa);
3204 bo->bo_appie += sizeof(*csa);
3205 bo->bo_csa_trailer_len += sizeof(*csa);
3206 bo->bo_quiet += sizeof(*csa);
3207 bo->bo_tim_trailer_len += sizeof(*csa);
3208 m->m_len += sizeof(*csa);
3209 m->m_pkthdr.len += sizeof(*csa);
3211 ieee80211_add_csa(bo->bo_csa, vap);
3214 vap->iv_csa_count++;
3215 /* NB: don't clear IEEE80211_BEACON_CSA */
3217 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3218 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3220 ieee80211_add_quiet(bo->bo_quiet, vap);
3222 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3224 * ERP element needs updating.
3226 (void) ieee80211_add_erp(bo->bo_erp, ic);
3227 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3229 #ifdef IEEE80211_SUPPORT_SUPERG
3230 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3231 ieee80211_add_athcaps(bo->bo_ath, ni);
3232 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3236 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3237 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3243 aielen += aie->ie_len;
3244 if (aielen != bo->bo_appie_len) {
3245 /* copy up/down trailer */
3246 int adjust = aielen - bo->bo_appie_len;
3247 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3248 bo->bo_tim_trailer_len);
3249 bo->bo_tim_trailer += adjust;
3250 bo->bo_appie += adjust;
3251 bo->bo_appie_len = aielen;
3257 frm = add_appie(frm, aie);
3258 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3260 IEEE80211_UNLOCK(ic);