2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/endian.h>
41 #include <sys/socket.h>
44 #include <net/ethernet.h>
46 #include <net/if_var.h>
47 #include <net/if_llc.h>
48 #include <net/if_media.h>
49 #include <net/vlan/if_vlan_var.h>
51 #if defined(__DragonFly__)
52 #include <net/ifq_var.h>
55 #include <netproto/802_11/ieee80211_var.h>
56 #include <netproto/802_11/ieee80211_regdomain.h>
57 #ifdef IEEE80211_SUPPORT_SUPERG
58 #include <netproto/802_11/ieee80211_superg.h>
60 #ifdef IEEE80211_SUPPORT_TDMA
61 #include <netproto/802_11/ieee80211_tdma.h>
63 #include <netproto/802_11/ieee80211_wds.h>
64 #include <netproto/802_11/ieee80211_mesh.h>
66 #if defined(INET) || defined(INET6)
67 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
76 #include <netinet/ip6.h>
79 #if defined(__DragonFly__)
81 #include <security/mac/mac_framework.h>
84 #define ETHER_HEADER_COPY(dst, src) \
85 memcpy(dst, src, sizeof(struct ether_header))
87 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
88 u_int hdrsize, u_int ciphdrsize, u_int mtu);
89 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
91 #ifdef IEEE80211_DEBUG
93 * Decide if an outbound management frame should be
94 * printed when debugging is enabled. This filters some
95 * of the less interesting frames that come frequently
99 doprint(struct ieee80211vap *vap, int subtype)
102 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
103 return (vap->iv_opmode == IEEE80211_M_IBSS);
110 * Transmit a frame to the given destination on the given VAP.
112 * It's up to the caller to figure out the details of who this
113 * is going to and resolving the node.
115 * This routine takes care of queuing it for power save,
116 * A-MPDU state stuff, fast-frames state stuff, encapsulation
117 * if required, then passing it up to the driver layer.
119 * This routine (for now) consumes the mbuf and frees the node
120 * reference; it ideally will return a TX status which reflects
121 * whether the mbuf was consumed or not, so the caller can
122 * free the mbuf (if appropriate) and the node reference (again,
126 ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
127 struct ieee80211_node *ni)
129 struct ieee80211com *ic = vap->iv_ic;
130 struct ifnet *ifp = vap->iv_ifp;
133 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
134 (m->m_flags & M_PWR_SAV) == 0) {
136 * Station in power save mode; pass the frame
137 * to the 802.11 layer and continue. We'll get
138 * the frame back when the time is right.
139 * XXX lose WDS vap linkage?
141 if (ieee80211_pwrsave(ni, m) != 0)
142 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
143 ieee80211_free_node(ni);
146 * We queued it fine, so tell the upper layer
147 * that we consumed it.
151 /* calculate priority so drivers can find the tx queue */
152 if (ieee80211_classify(ni, m)) {
153 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
154 ni->ni_macaddr, NULL,
155 "%s", "classification failure");
156 vap->iv_stats.is_tx_classify++;
157 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
159 ieee80211_free_node(ni);
161 /* XXX better status? */
165 * Stash the node pointer. Note that we do this after
166 * any call to ieee80211_dwds_mcast because that code
167 * uses any existing value for rcvif to identify the
168 * interface it (might have been) received on.
170 m->m_pkthdr.rcvif = (void *)ni;
171 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
172 len = m->m_pkthdr.len;
174 BPF_MTAP(ifp, m); /* 802.3 tx */
177 * Check if A-MPDU tx aggregation is setup or if we
178 * should try to enable it. The sta must be associated
179 * with HT and A-MPDU enabled for use. When the policy
180 * routine decides we should enable A-MPDU we issue an
181 * ADDBA request and wait for a reply. The frame being
182 * encapsulated will go out w/o using A-MPDU, or possibly
183 * it might be collected by the driver and held/retransmit.
184 * The default ic_ampdu_enable routine handles staggering
185 * ADDBA requests in case the receiver NAK's us or we are
186 * otherwise unable to establish a BA stream.
188 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
189 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
190 if ((m->m_flags & M_EAPOL) == 0) {
191 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
192 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
194 ieee80211_txampdu_count_packet(tap);
195 if (IEEE80211_AMPDU_RUNNING(tap)) {
197 * Operational, mark frame for aggregation.
199 * XXX do tx aggregation here
201 m->m_flags |= M_AMPDU_MPDU;
202 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
203 ic->ic_ampdu_enable(ni, tap)) {
205 * Not negotiated yet, request service.
207 ieee80211_ampdu_request(ni, tap);
208 /* XXX hold frame for reply? */
213 #ifdef IEEE80211_SUPPORT_SUPERG
215 * Check for AMSDU/FF; queue for aggregation
217 * Note: we don't bother trying to do fast frames or
218 * A-MSDU encapsulation for 802.3 drivers. Now, we
219 * likely could do it for FF (because it's a magic
220 * atheros tunnel LLC type) but I don't think we're going
221 * to really need to. For A-MSDU we'd have to set the
222 * A-MSDU QoS bit in the wifi header, so we just plain
225 * Strictly speaking, we could actually /do/ A-MSDU / FF
226 * with A-MPDU together which for certain circumstances
227 * is beneficial (eg A-MSDU of TCK ACKs.) However,
228 * I'll ignore that for now so existing behaviour is maintained.
229 * Later on it would be good to make "amsdu + ampdu" configurable.
231 else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
232 if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
233 m = ieee80211_amsdu_check(ni, m);
235 /* NB: any ni ref held on stageq */
236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
237 "%s: amsdu_check queued frame\n",
241 } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
242 IEEE80211_NODE_FF)) {
243 m = ieee80211_ff_check(ni, m);
245 /* NB: any ni ref held on stageq */
246 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
247 "%s: ff_check queued frame\n",
253 #endif /* IEEE80211_SUPPORT_SUPERG */
256 * Grab the TX lock - serialise the TX process from this
257 * point (where TX state is being checked/modified)
258 * through to driver queue.
260 IEEE80211_TX_LOCK(ic);
263 * XXX make the encap and transmit code a separate function
264 * so things like the FF (and later A-MSDU) path can just call
265 * it for flushed frames.
267 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
269 * Encapsulate the packet in prep for transmission.
271 m = ieee80211_encap(vap, ni, m);
273 /* NB: stat+msg handled in ieee80211_encap */
274 IEEE80211_TX_UNLOCK(ic);
275 ieee80211_free_node(ni);
276 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
280 (void) ieee80211_parent_xmitpkt(ic, m);
283 * Unlock at this point - no need to hold it across
284 * ieee80211_free_node() (ie, the comlock)
286 IEEE80211_TX_UNLOCK(ic);
287 ic->ic_lastdata = ticks;
295 * Send the given mbuf through the given vap.
297 * This consumes the mbuf regardless of whether the transmit
298 * was successful or not.
300 * This does none of the initial checks that ieee80211_start()
301 * does (eg CAC timeout, interface wakeup) - the caller must
305 ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
307 #define IS_DWDS(vap) \
308 (vap->iv_opmode == IEEE80211_M_WDS && \
309 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
310 struct ieee80211com *ic = vap->iv_ic;
311 struct ifnet *ifp = vap->iv_ifp;
312 struct ieee80211_node *ni;
313 struct ether_header *eh;
316 * Cancel any background scan.
318 if (ic->ic_flags & IEEE80211_F_SCAN)
319 ieee80211_cancel_anyscan(vap);
321 * Find the node for the destination so we can do
322 * things like power save and fast frames aggregation.
324 * NB: past this point various code assumes the first
325 * mbuf has the 802.3 header present (and contiguous).
328 if (m->m_len < sizeof(struct ether_header) &&
329 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
330 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
331 "discard frame, %s\n", "m_pullup failed");
332 vap->iv_stats.is_tx_nobuf++; /* XXX */
333 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
336 eh = mtod(m, struct ether_header *);
337 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
340 * Only unicast frames from the above go out
341 * DWDS vaps; multicast frames are handled by
342 * dispatching the frame as it comes through
343 * the AP vap (see below).
345 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
346 eh->ether_dhost, "mcast", "%s", "on DWDS");
347 vap->iv_stats.is_dwds_mcast++;
349 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
350 /* XXX better status? */
353 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
355 * Spam DWDS vap's w/ multicast traffic.
357 /* XXX only if dwds in use? */
358 ieee80211_dwds_mcast(vap, m);
361 #ifdef IEEE80211_SUPPORT_MESH
362 if (vap->iv_opmode != IEEE80211_M_MBSS) {
364 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
366 /* NB: ieee80211_find_txnode does stat+msg */
367 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
369 /* XXX better status? */
372 if (ni->ni_associd == 0 &&
373 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
374 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
375 eh->ether_dhost, NULL,
376 "sta not associated (type 0x%04x)",
377 htons(eh->ether_type));
378 vap->iv_stats.is_tx_notassoc++;
379 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
381 ieee80211_free_node(ni);
382 /* XXX better status? */
385 #ifdef IEEE80211_SUPPORT_MESH
387 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
389 * Proxy station only if configured.
391 if (!ieee80211_mesh_isproxyena(vap)) {
392 IEEE80211_DISCARD_MAC(vap,
393 IEEE80211_MSG_OUTPUT |
395 eh->ether_dhost, NULL,
396 "%s", "proxy not enabled");
397 vap->iv_stats.is_mesh_notproxy++;
398 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
400 /* XXX better status? */
403 #if defined(__DragonFly__)
404 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
405 "forward frame from DS SA(%s), DA(%s)\n",
406 ether_sprintf(eh->ether_shost),
407 ether_sprintf(eh->ether_dhost));
409 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
410 "forward frame from DS SA(%6D), DA(%6D)\n",
411 eh->ether_shost, ":",
412 eh->ether_dhost, ":");
414 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
416 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
419 * NB: ieee80211_mesh_discover holds/disposes
420 * frame (e.g. queueing on path discovery).
422 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
423 /* XXX better status? */
430 * We've resolved the sender, so attempt to transmit it.
433 if (vap->iv_state == IEEE80211_S_SLEEP) {
435 * In power save; queue frame and then wakeup device
438 ic->ic_lastdata = ticks;
439 if (ieee80211_pwrsave(ni, m) != 0)
440 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
441 ieee80211_free_node(ni);
442 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
446 if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
453 * Start method for vap's. All packets from the stack come
454 * through here. We handle common processing of the packets
455 * before dispatching them to the underlying device.
457 * if_transmit() requires that the mbuf be consumed by this call
458 * regardless of the return condition.
461 #if defined(__DragonFly__)
464 ieee80211_vap_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
466 struct ieee80211vap *vap = ifp->if_softc;
467 struct ieee80211com *ic = vap->iv_ic;
468 struct ifnet *parent = vap->iv_ifp;
469 struct mbuf *m = NULL;
471 /* NB: parent must be up and running */
472 if (!IFNET_IS_UP_RUNNING(parent)) {
473 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
474 "%s: ignore queue, parent %s not up+running\n",
475 __func__, parent->if_xname);
482 wlan_assert_serialized();
483 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
486 * No data frames go out unless we're running.
487 * Note in particular this covers CAC and CSA
488 * states (though maybe we should check muting
491 if (vap->iv_state != IEEE80211_S_RUN &&
492 vap->iv_state != IEEE80211_S_SLEEP) {
494 /* re-check under the com lock to avoid races */
495 if (vap->iv_state != IEEE80211_S_RUN &&
496 vap->iv_state != IEEE80211_S_SLEEP) {
497 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
498 "%s: ignore queue, in %s state\n",
499 __func__, ieee80211_state_name[vap->iv_state]);
500 vap->iv_stats.is_tx_badstate++;
501 IEEE80211_UNLOCK(ic);
502 ifsq_set_oactive(ifsq);
504 /* return (EINVAL); */
507 IEEE80211_UNLOCK(ic);
510 wlan_serialize_exit();
512 m = ifsq_dequeue(ifsq);
517 * Sanitize mbuf flags for net80211 use. We cannot
518 * clear M_PWR_SAV or M_MORE_DATA because these may
519 * be set for frames that are re-submitted from the
522 * NB: This must be done before ieee80211_classify as
523 * it marks EAPOL in frames with M_EAPOL.
525 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
528 * Bump to the packet transmission path.
529 * The mbuf will be consumed here.
531 ieee80211_start_pkt(vap, m);
533 wlan_serialize_enter();
539 ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
541 struct ieee80211vap *vap = ifp->if_softc;
542 struct ieee80211com *ic = vap->iv_ic;
545 * No data frames go out unless we're running.
546 * Note in particular this covers CAC and CSA
547 * states (though maybe we should check muting
550 if (vap->iv_state != IEEE80211_S_RUN &&
551 vap->iv_state != IEEE80211_S_SLEEP) {
553 /* re-check under the com lock to avoid races */
554 if (vap->iv_state != IEEE80211_S_RUN &&
555 vap->iv_state != IEEE80211_S_SLEEP) {
556 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
557 "%s: ignore queue, in %s state\n",
558 __func__, ieee80211_state_name[vap->iv_state]);
559 vap->iv_stats.is_tx_badstate++;
560 IEEE80211_UNLOCK(ic);
561 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
563 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
566 IEEE80211_UNLOCK(ic);
570 * Sanitize mbuf flags for net80211 use. We cannot
571 * clear M_PWR_SAV or M_MORE_DATA because these may
572 * be set for frames that are re-submitted from the
575 * NB: This must be done before ieee80211_classify as
576 * it marks EAPOL in frames with M_EAPOL.
578 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
581 * Bump to the packet transmission path.
582 * The mbuf will be consumed here.
584 return (ieee80211_start_pkt(vap, m));
588 ieee80211_vap_qflush(struct ifnet *ifp)
597 * 802.11 raw output routine.
599 * XXX TODO: this (and other send routines) should correctly
600 * XXX keep the pwr mgmt bit set if it decides to call into the
601 * XXX driver to send a frame whilst the state is SLEEP.
603 * Otherwise the peer may decide that we're awake and flood us
604 * with traffic we are still too asleep to receive!
607 ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
608 struct mbuf *m, const struct ieee80211_bpf_params *params)
610 struct ieee80211com *ic = vap->iv_ic;
614 * Set node - the caller has taken a reference, so ensure
615 * that the mbuf has the same node value that
616 * it would if it were going via the normal path.
618 m->m_pkthdr.rcvif = (void *)ni;
621 * Attempt to add bpf transmit parameters.
623 * For now it's ok to fail; the raw_xmit api still takes
626 * Later on when ic_raw_xmit() has params removed,
627 * they'll have to be added - so fail the transmit if
631 (void) ieee80211_add_xmit_params(m, params);
633 error = ic->ic_raw_xmit(ni, m, params);
635 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
636 ieee80211_free_node(ni);
642 * 802.11 output routine. This is (currently) used only to
643 * connect bpf write calls to the 802.11 layer for injecting
646 #if defined(__DragonFly__)
648 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
649 struct sockaddr *dst, struct rtentry *rt)
652 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
653 const struct sockaddr *dst, struct route *ro)
656 #define senderr(e) do { error = (e); goto bad;} while (0)
657 struct ieee80211_node *ni = NULL;
658 struct ieee80211vap *vap;
659 struct ieee80211_frame *wh;
660 struct ieee80211com *ic = NULL;
664 #if defined(__DragonFly__)
665 struct ifaltq_subque *ifsq;
666 ifsq = ifq_get_subq_default(&ifp->if_snd);
667 if (ifsq_is_oactive(ifsq)) {
669 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
672 * Short-circuit requests if the vap is marked OACTIVE
673 * as this can happen because a packet came down through
674 * ieee80211_start before the vap entered RUN state in
675 * which case it's ok to just drop the frame. This
676 * should not be necessary but callers of if_output don't
684 * Hand to the 802.3 code if not tagged as
685 * a raw 802.11 frame.
687 #if defined(__DragonFly__)
688 if (dst->sa_family != AF_IEEE80211)
689 return vap->iv_output(ifp, m, dst, rt);
691 if (dst->sa_family != AF_IEEE80211)
692 return vap->iv_output(ifp, m, dst, ro);
695 error = mac_ifnet_check_transmit(ifp, m);
699 if (ifp->if_flags & IFF_MONITOR)
701 if (!IFNET_IS_UP_RUNNING(ifp))
703 if (vap->iv_state == IEEE80211_S_CAC) {
704 IEEE80211_DPRINTF(vap,
705 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
706 "block %s frame in CAC state\n", "raw data");
707 vap->iv_stats.is_tx_badstate++;
708 senderr(EIO); /* XXX */
709 } else if (vap->iv_state == IEEE80211_S_SCAN)
711 /* XXX bypass bridge, pfil, carp, etc. */
713 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
714 senderr(EIO); /* XXX */
715 wh = mtod(m, struct ieee80211_frame *);
716 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
717 IEEE80211_FC0_VERSION_0)
718 senderr(EIO); /* XXX */
720 /* locate destination node */
721 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
722 case IEEE80211_FC1_DIR_NODS:
723 case IEEE80211_FC1_DIR_FROMDS:
724 ni = ieee80211_find_txnode(vap, wh->i_addr1);
726 case IEEE80211_FC1_DIR_TODS:
727 case IEEE80211_FC1_DIR_DSTODS:
728 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
729 senderr(EIO); /* XXX */
730 ni = ieee80211_find_txnode(vap, wh->i_addr3);
733 senderr(EIO); /* XXX */
737 * Permit packets w/ bpf params through regardless
738 * (see below about sa_len).
740 if (dst->sa_len == 0)
741 senderr(EHOSTUNREACH);
742 ni = ieee80211_ref_node(vap->iv_bss);
746 * Sanitize mbuf for net80211 flags leaked from above.
748 * NB: This must be done before ieee80211_classify as
749 * it marks EAPOL in frames with M_EAPOL.
751 m->m_flags &= ~M_80211_TX;
753 /* calculate priority so drivers can find the tx queue */
754 /* XXX assumes an 802.3 frame */
755 if (ieee80211_classify(ni, m))
756 senderr(EIO); /* XXX */
758 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
759 IEEE80211_NODE_STAT(ni, tx_data);
760 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
761 IEEE80211_NODE_STAT(ni, tx_mcast);
762 m->m_flags |= M_MCAST;
764 IEEE80211_NODE_STAT(ni, tx_ucast);
765 /* NB: ieee80211_encap does not include 802.11 header */
766 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
768 IEEE80211_TX_LOCK(ic);
771 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
772 * present by setting the sa_len field of the sockaddr (yes,
774 * NB: we assume sa_data is suitably aligned to cast.
776 ret = ieee80211_raw_output(vap, ni, m,
777 (const struct ieee80211_bpf_params *)(dst->sa_len ?
778 dst->sa_data : NULL));
779 IEEE80211_TX_UNLOCK(ic);
785 ieee80211_free_node(ni);
786 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
792 * Set the direction field and address fields of an outgoing
793 * frame. Note this should be called early on in constructing
794 * a frame as it sets i_fc[1]; other bits can then be or'd in.
797 ieee80211_send_setup(
798 struct ieee80211_node *ni,
801 const uint8_t sa[IEEE80211_ADDR_LEN],
802 const uint8_t da[IEEE80211_ADDR_LEN],
803 const uint8_t bssid[IEEE80211_ADDR_LEN])
805 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
806 struct ieee80211vap *vap = ni->ni_vap;
807 struct ieee80211_tx_ampdu *tap;
808 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
811 IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
813 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
814 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
815 switch (vap->iv_opmode) {
816 case IEEE80211_M_STA:
817 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
818 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
819 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
820 IEEE80211_ADDR_COPY(wh->i_addr3, da);
822 case IEEE80211_M_IBSS:
823 case IEEE80211_M_AHDEMO:
824 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
825 IEEE80211_ADDR_COPY(wh->i_addr1, da);
826 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
827 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
829 case IEEE80211_M_HOSTAP:
830 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
831 IEEE80211_ADDR_COPY(wh->i_addr1, da);
832 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
833 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
835 case IEEE80211_M_WDS:
836 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
837 IEEE80211_ADDR_COPY(wh->i_addr1, da);
838 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
839 IEEE80211_ADDR_COPY(wh->i_addr3, da);
840 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
842 case IEEE80211_M_MBSS:
843 #ifdef IEEE80211_SUPPORT_MESH
844 if (IEEE80211_IS_MULTICAST(da)) {
845 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
847 IEEE80211_ADDR_COPY(wh->i_addr1, da);
848 IEEE80211_ADDR_COPY(wh->i_addr2,
851 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
852 IEEE80211_ADDR_COPY(wh->i_addr1, da);
853 IEEE80211_ADDR_COPY(wh->i_addr2,
855 IEEE80211_ADDR_COPY(wh->i_addr3, da);
856 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
860 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
864 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
865 IEEE80211_ADDR_COPY(wh->i_addr1, da);
866 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
867 #ifdef IEEE80211_SUPPORT_MESH
868 if (vap->iv_opmode == IEEE80211_M_MBSS)
869 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
872 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
874 *(uint16_t *)&wh->i_dur[0] = 0;
876 tap = &ni->ni_tx_ampdu[tid];
877 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
878 m->m_flags |= M_AMPDU_MPDU;
880 if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
881 type & IEEE80211_FC0_SUBTYPE_MASK))
882 seqno = ni->ni_txseqs[tid]++;
886 *(uint16_t *)&wh->i_seq[0] =
887 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
888 M_SEQNO_SET(m, seqno);
891 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
892 m->m_flags |= M_MCAST;
897 * Send a management frame to the specified node. The node pointer
898 * must have a reference as the pointer will be passed to the driver
899 * and potentially held for a long time. If the frame is successfully
900 * dispatched to the driver, then it is responsible forkfreeing the
901 * reference (and potentiallykfree'ing up any associated storage);
902 * otherwise deal with reclaiming any reference (on error).
905 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
906 struct ieee80211_bpf_params *params)
908 struct ieee80211vap *vap = ni->ni_vap;
909 struct ieee80211com *ic = ni->ni_ic;
910 struct ieee80211_frame *wh;
913 KASSERT(ni != NULL, ("null node"));
915 if (vap->iv_state == IEEE80211_S_CAC) {
916 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
917 ni, "block %s frame in CAC state",
918 ieee80211_mgt_subtype_name(type));
919 vap->iv_stats.is_tx_badstate++;
920 ieee80211_free_node(ni);
922 return EIO; /* XXX */
925 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
927 ieee80211_free_node(ni);
931 IEEE80211_TX_LOCK(ic);
933 wh = mtod(m, struct ieee80211_frame *);
934 ieee80211_send_setup(ni, m,
935 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
936 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
937 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
938 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
939 "encrypting frame (%s)", __func__);
940 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
942 m->m_flags |= M_ENCAP; /* mark encapsulated */
944 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
945 M_WME_SETAC(m, params->ibp_pri);
947 #ifdef IEEE80211_DEBUG
948 /* avoid printing too many frames */
949 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
950 ieee80211_msg_dumppkts(vap)) {
951 kprintf("[%s] send %s on channel %u\n",
952 ether_sprintf(wh->i_addr1),
953 ieee80211_mgt_subtype_name(type),
954 ieee80211_chan2ieee(ic, ic->ic_curchan));
957 IEEE80211_NODE_STAT(ni, tx_mgmt);
959 ret = ieee80211_raw_output(vap, ni, m, params);
960 IEEE80211_TX_UNLOCK(ic);
965 ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
968 struct ieee80211vap *vap = ni->ni_vap;
974 * Send a null data frame to the specified node. If the station
975 * is setup for QoS then a QoS Null Data frame is constructed.
976 * If this is a WDS station then a 4-address frame is constructed.
978 * NB: the caller is assumed to have setup a node reference
979 * for use; this is necessary to deal with a race condition
980 * when probing for inactive stations. Like ieee80211_mgmt_output
981 * we must cleanup any node reference on error; however we
982 * can safely just unref it as we know it will never be the
983 * last reference to the node.
986 ieee80211_send_nulldata(struct ieee80211_node *ni)
988 struct ieee80211vap *vap = ni->ni_vap;
989 struct ieee80211com *ic = ni->ni_ic;
991 struct ieee80211_frame *wh;
996 if (vap->iv_state == IEEE80211_S_CAC) {
997 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
998 ni, "block %s frame in CAC state", "null data");
999 ieee80211_unref_node(&ni);
1000 vap->iv_stats.is_tx_badstate++;
1001 return EIO; /* XXX */
1004 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
1005 hdrlen = sizeof(struct ieee80211_qosframe);
1007 hdrlen = sizeof(struct ieee80211_frame);
1008 /* NB: only WDS vap's get 4-address frames */
1009 if (vap->iv_opmode == IEEE80211_M_WDS)
1010 hdrlen += IEEE80211_ADDR_LEN;
1011 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1012 hdrlen = roundup(hdrlen, sizeof(uint32_t));
1014 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1017 ieee80211_unref_node(&ni);
1018 vap->iv_stats.is_tx_nobuf++;
1021 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
1022 ("leading space %zd", M_LEADINGSPACE(m)));
1023 M_PREPEND(m, hdrlen, M_NOWAIT);
1025 /* NB: cannot happen */
1026 ieee80211_free_node(ni);
1030 IEEE80211_TX_LOCK(ic);
1032 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
1033 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1034 const int tid = WME_AC_TO_TID(WME_AC_BE);
1037 ieee80211_send_setup(ni, m,
1038 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
1039 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1041 if (vap->iv_opmode == IEEE80211_M_WDS)
1042 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1044 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1045 qos[0] = tid & IEEE80211_QOS_TID;
1046 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
1047 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1050 ieee80211_send_setup(ni, m,
1051 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
1052 IEEE80211_NONQOS_TID,
1053 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
1055 if (vap->iv_opmode != IEEE80211_M_WDS) {
1056 /* NB: power management bit is never sent by an AP */
1057 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1058 vap->iv_opmode != IEEE80211_M_HOSTAP)
1059 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
1061 if ((ic->ic_flags & IEEE80211_F_SCAN) &&
1062 (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
1063 ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
1066 m->m_len = m->m_pkthdr.len = hdrlen;
1067 m->m_flags |= M_ENCAP; /* mark encapsulated */
1069 M_WME_SETAC(m, WME_AC_BE);
1071 IEEE80211_NODE_STAT(ni, tx_data);
1073 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
1074 "send %snull data frame on channel %u, pwr mgt %s",
1075 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1076 ieee80211_chan2ieee(ic, ic->ic_curchan),
1077 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1079 ret = ieee80211_raw_output(vap, ni, m, NULL);
1080 IEEE80211_TX_UNLOCK(ic);
1085 * Assign priority to a frame based on any vlan tag assigned
1086 * to the station and/or any Diffserv setting in an IP header.
1087 * Finally, if an ACM policy is setup (in station mode) it's
1091 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1093 const struct ether_header *eh = mtod(m, struct ether_header *);
1094 int v_wme_ac, d_wme_ac, ac;
1097 * Always promote PAE/EAPOL frames to high priority.
1099 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
1100 /* NB: mark so others don't need to check header */
1101 m->m_flags |= M_EAPOL;
1106 * Non-qos traffic goes to BE.
1108 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1114 * If node has a vlan tag then all traffic
1115 * to it must have a matching tag.
1118 if (ni->ni_vlan != 0) {
1119 if ((m->m_flags & M_VLANTAG) == 0) {
1120 IEEE80211_NODE_STAT(ni, tx_novlantag);
1123 #if defined(__DragonFly__)
1124 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
1125 EVL_VLANOFTAG(ni->ni_vlan)) {
1126 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1130 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1131 EVL_VLANOFTAG(ni->ni_vlan)) {
1132 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1136 /* map vlan priority to AC */
1137 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1140 /* XXX m_copydata may be too slow for fast path */
1142 if (eh->ether_type == htons(ETHERTYPE_IP)) {
1145 * IP frame, map the DSCP bits from the TOS field.
1147 /* NB: ip header may not be in first mbuf */
1148 m_copydata(m, sizeof(struct ether_header) +
1149 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
1150 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1151 d_wme_ac = TID_TO_WME_AC(tos);
1155 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
1159 * IPv6 frame, map the DSCP bits from the traffic class field.
1161 m_copydata(m, sizeof(struct ether_header) +
1162 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
1164 tos = (uint8_t)(ntohl(flow) >> 20);
1165 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
1166 d_wme_ac = TID_TO_WME_AC(tos);
1169 d_wme_ac = WME_AC_BE;
1177 * Use highest priority AC.
1179 if (v_wme_ac > d_wme_ac)
1187 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1188 static const int acmap[4] = {
1189 WME_AC_BK, /* WME_AC_BE */
1190 WME_AC_BK, /* WME_AC_BK */
1191 WME_AC_BE, /* WME_AC_VI */
1192 WME_AC_VI, /* WME_AC_VO */
1194 struct ieee80211com *ic = ni->ni_ic;
1196 while (ac != WME_AC_BK &&
1197 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1206 * Insure there is sufficient contiguous space to encapsulate the
1207 * 802.11 data frame. If room isn't already there, arrange for it.
1208 * Drivers and cipher modules assume we have done the necessary work
1209 * and fail rudely if they don't find the space they need.
1212 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1213 struct ieee80211_key *key, struct mbuf *m)
1215 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
1216 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1219 /* XXX belongs in crypto code? */
1220 needed_space += key->wk_cipher->ic_header;
1223 * When crypto is being done in the host we must insure
1224 * the data are writable for the cipher routines; clone
1225 * a writable mbuf chain.
1226 * XXX handle SWMIC specially
1228 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1229 m = m_unshare(m, M_NOWAIT);
1231 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1232 "%s: cannot get writable mbuf\n", __func__);
1233 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1239 * We know we are called just before stripping an Ethernet
1240 * header and prepending an LLC header. This means we know
1242 * sizeof(struct ether_header) - sizeof(struct llc)
1243 * bytes recovered to which we need additional space for the
1244 * 802.11 header and any crypto header.
1246 /* XXX check trailing space and copy instead? */
1247 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1248 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1250 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1251 "%s: cannot expand storage\n", __func__);
1252 vap->iv_stats.is_tx_nobuf++;
1256 #if defined(__DragonFly__)
1257 KASSERT(needed_space <= MHLEN,
1258 ("not enough room, need %u got %zd\n", needed_space, MHLEN));
1260 KASSERT(needed_space <= MHLEN,
1261 ("not enough room, need %u got %d\n", needed_space, MHLEN));
1264 * Setup new mbuf to have leading space to prepend the
1265 * 802.11 header and any crypto header bits that are
1266 * required (the latter are added when the driver calls
1267 * back to ieee80211_crypto_encap to do crypto encapsulation).
1269 /* NB: must be first 'cuz it clobbers m_data */
1270 m_move_pkthdr(n, m);
1271 n->m_len = 0; /* NB: m_gethdr does not set */
1272 n->m_data += needed_space;
1274 * Pull up Ethernet header to create the expected layout.
1275 * We could use m_pullup but that's overkill (i.e. we don't
1276 * need the actual data) and it cannot fail so do it inline
1279 /* NB: struct ether_header is known to be contiguous */
1280 n->m_len += sizeof(struct ether_header);
1281 m->m_len -= sizeof(struct ether_header);
1282 m->m_data += sizeof(struct ether_header);
1284 * Replace the head of the chain.
1290 #undef TO_BE_RECLAIMED
1294 * Return the transmit key to use in sending a unicast frame.
1295 * If a unicast key is set we use that. When no unicast key is set
1296 * we fall back to the default transmit key.
1298 static __inline struct ieee80211_key *
1299 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
1300 struct ieee80211_node *ni)
1302 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
1303 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1304 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1306 return &vap->iv_nw_keys[vap->iv_def_txkey];
1308 return &ni->ni_ucastkey;
1313 * Return the transmit key to use in sending a multicast frame.
1314 * Multicast traffic always uses the group key which is installed as
1315 * the default tx key.
1317 static __inline struct ieee80211_key *
1318 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
1319 struct ieee80211_node *ni)
1321 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
1322 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1324 return &vap->iv_nw_keys[vap->iv_def_txkey];
1328 * Encapsulate an outbound data frame. The mbuf chain is updated.
1329 * If an error is encountered NULL is returned. The caller is required
1330 * to provide a node reference and pullup the ethernet header in the
1333 * NB: Packet is assumed to be processed by ieee80211_classify which
1334 * marked EAPOL frames w/ M_EAPOL.
1337 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1340 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1341 #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
1342 struct ieee80211com *ic = ni->ni_ic;
1343 #ifdef IEEE80211_SUPPORT_MESH
1344 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1345 struct ieee80211_meshcntl_ae10 *mc;
1346 struct ieee80211_mesh_route *rt = NULL;
1349 struct ether_header eh;
1350 struct ieee80211_frame *wh;
1351 struct ieee80211_key *key;
1353 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1354 ieee80211_seq seqno;
1355 int meshhdrsize, meshae;
1359 IEEE80211_TX_LOCK_ASSERT(ic);
1362 * Copy existing Ethernet header to a safe place. The
1363 * rest of the code assumes it's ok to strip it when
1364 * reorganizing state for the final encapsulation.
1366 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1367 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1370 * Insure space for additional headers. First identify
1371 * transmit key to use in calculating any buffer adjustments
1372 * required. This is also used below to do privacy
1373 * encapsulation work. Then calculate the 802.11 header
1374 * size and any padding required by the driver.
1376 * Note key may be NULL if we fall back to the default
1377 * transmit key and that is not set. In that case the
1378 * buffer may not be expanded as needed by the cipher
1379 * routines, but they will/should discard it.
1381 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1382 if (vap->iv_opmode == IEEE80211_M_STA ||
1383 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1384 (vap->iv_opmode == IEEE80211_M_WDS &&
1385 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1386 key = ieee80211_crypto_getucastkey(vap, ni);
1388 key = ieee80211_crypto_getmcastkey(vap, ni);
1389 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1390 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1392 "no default transmit key (%s) deftxkey %u",
1393 __func__, vap->iv_def_txkey);
1394 vap->iv_stats.is_tx_nodefkey++;
1400 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1401 * frames so suppress use. This may be an issue if other
1402 * ap's require all data frames to be QoS-encapsulated
1403 * once negotiated in which case we'll need to make this
1405 * NB: mesh data frames are QoS.
1407 addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1408 (vap->iv_opmode == IEEE80211_M_MBSS)) &&
1409 (m->m_flags & M_EAPOL) == 0;
1411 hdrsize = sizeof(struct ieee80211_qosframe);
1413 hdrsize = sizeof(struct ieee80211_frame);
1414 #ifdef IEEE80211_SUPPORT_MESH
1415 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1417 * Mesh data frames are encapsulated according to the
1418 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1419 * o Group Addressed data (aka multicast) originating
1420 * at the local sta are sent w/ 3-address format and
1421 * address extension mode 00
1422 * o Individually Addressed data (aka unicast) originating
1423 * at the local sta are sent w/ 4-address format and
1424 * address extension mode 00
1425 * o Group Addressed data forwarded from a non-mesh sta are
1426 * sent w/ 3-address format and address extension mode 01
1427 * o Individually Address data from another sta are sent
1428 * w/ 4-address format and address extension mode 10
1430 is4addr = 0; /* NB: don't use, disable */
1431 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1432 rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1433 KASSERT(rt != NULL, ("route is NULL"));
1434 dir = IEEE80211_FC1_DIR_DSTODS;
1435 hdrsize += IEEE80211_ADDR_LEN;
1436 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1437 if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1439 IEEE80211_NOTE_MAC(vap,
1442 "%s", "trying to send to ourself");
1445 meshae = IEEE80211_MESH_AE_10;
1447 sizeof(struct ieee80211_meshcntl_ae10);
1449 meshae = IEEE80211_MESH_AE_00;
1451 sizeof(struct ieee80211_meshcntl);
1454 dir = IEEE80211_FC1_DIR_FROMDS;
1455 if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1457 meshae = IEEE80211_MESH_AE_01;
1459 sizeof(struct ieee80211_meshcntl_ae01);
1462 meshae = IEEE80211_MESH_AE_00;
1463 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1469 * 4-address frames need to be generated for:
1470 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1471 * o packets sent through a vap marked for relaying
1472 * (e.g. a station operating with dynamic WDS)
1474 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1475 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1476 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1478 hdrsize += IEEE80211_ADDR_LEN;
1479 meshhdrsize = meshae = 0;
1480 #ifdef IEEE80211_SUPPORT_MESH
1484 * Honor driver DATAPAD requirement.
1486 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1487 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1491 if (__predict_true((m->m_flags & M_FF) == 0)) {
1495 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1497 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1500 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1501 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1502 llc = mtod(m, struct llc *);
1503 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1504 llc->llc_control = LLC_UI;
1505 llc->llc_snap.org_code[0] = 0;
1506 llc->llc_snap.org_code[1] = 0;
1507 llc->llc_snap.org_code[2] = 0;
1508 llc->llc_snap.ether_type = eh.ether_type;
1510 #ifdef IEEE80211_SUPPORT_SUPERG
1512 * Aggregated frame. Check if it's for AMSDU or FF.
1514 * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
1515 * anywhere for some reason. But, since 11n requires
1516 * AMSDU RX, we can just assume "11n" == "AMSDU".
1518 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
1519 if (ieee80211_amsdu_tx_ok(ni)) {
1520 m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
1523 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1529 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1531 M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1533 vap->iv_stats.is_tx_nobuf++;
1536 wh = mtod(m, struct ieee80211_frame *);
1537 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1538 *(uint16_t *)wh->i_dur = 0;
1539 qos = NULL; /* NB: quiet compiler */
1541 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1542 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1543 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1544 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1545 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1546 } else switch (vap->iv_opmode) {
1547 case IEEE80211_M_STA:
1548 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1549 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1550 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1551 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1553 case IEEE80211_M_IBSS:
1554 case IEEE80211_M_AHDEMO:
1555 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1556 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1557 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1559 * NB: always use the bssid from iv_bss as the
1560 * neighbor's may be stale after an ibss merge
1562 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1564 case IEEE80211_M_HOSTAP:
1565 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1566 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1567 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1568 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1570 #ifdef IEEE80211_SUPPORT_MESH
1571 case IEEE80211_M_MBSS:
1572 /* NB: offset by hdrspace to deal with DATAPAD */
1573 mc = (struct ieee80211_meshcntl_ae10 *)
1574 (mtod(m, uint8_t *) + hdrspace);
1577 case IEEE80211_MESH_AE_00: /* no proxy */
1579 if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1580 IEEE80211_ADDR_COPY(wh->i_addr1,
1582 IEEE80211_ADDR_COPY(wh->i_addr2,
1584 IEEE80211_ADDR_COPY(wh->i_addr3,
1586 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1588 qos =((struct ieee80211_qosframe_addr4 *)
1590 } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1592 IEEE80211_ADDR_COPY(wh->i_addr1,
1594 IEEE80211_ADDR_COPY(wh->i_addr2,
1596 IEEE80211_ADDR_COPY(wh->i_addr3,
1598 qos = ((struct ieee80211_qosframe *)
1602 case IEEE80211_MESH_AE_01: /* mcast, proxy */
1603 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1604 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1605 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1606 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1608 IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1610 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1612 case IEEE80211_MESH_AE_10: /* ucast, proxy */
1613 KASSERT(rt != NULL, ("route is NULL"));
1614 IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
1615 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1616 IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
1617 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1618 mc->mc_flags = IEEE80211_MESH_AE_10;
1619 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1620 IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
1621 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1624 KASSERT(0, ("meshae %d", meshae));
1627 mc->mc_ttl = ms->ms_ttl;
1629 le32enc(mc->mc_seq, ms->ms_seq);
1632 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1636 if (m->m_flags & M_MORE_DATA)
1637 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1642 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1643 /* NB: mesh case handled earlier */
1644 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1645 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1646 ac = M_WME_GETAC(m);
1647 /* map from access class/queue to 11e header priorty value */
1648 tid = WME_AC_TO_TID(ac);
1649 qos[0] = tid & IEEE80211_QOS_TID;
1650 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1651 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1652 #ifdef IEEE80211_SUPPORT_MESH
1653 if (vap->iv_opmode == IEEE80211_M_MBSS)
1654 qos[1] = IEEE80211_QOS_MC;
1658 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1661 * If this is an A-MSDU then ensure we set the
1665 qos[0] |= IEEE80211_QOS_AMSDU;
1667 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1669 * NB: don't assign a sequence # to potential
1670 * aggregates; we expect this happens at the
1671 * point the frame comes off any aggregation q
1672 * as otherwise we may introduce holes in the
1673 * BA sequence space and/or make window accouting
1676 * XXX may want to control this with a driver
1677 * capability; this may also change when we pull
1678 * aggregation up into net80211
1680 seqno = ni->ni_txseqs[tid]++;
1681 *(uint16_t *)wh->i_seq =
1682 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1683 M_SEQNO_SET(m, seqno);
1686 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1687 *(uint16_t *)wh->i_seq =
1688 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1689 M_SEQNO_SET(m, seqno);
1692 * XXX TODO: we shouldn't allow EAPOL, etc that would
1693 * be forced to be non-QoS traffic to be A-MSDU encapsulated.
1696 kprintf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
1701 /* check if xmit fragmentation is required */
1702 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1703 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1704 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1705 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1708 * IEEE 802.1X: send EAPOL frames always in the clear.
1709 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1711 if ((m->m_flags & M_EAPOL) == 0 ||
1712 ((vap->iv_flags & IEEE80211_F_WPA) &&
1713 (vap->iv_opmode == IEEE80211_M_STA ?
1714 !IEEE80211_KEY_UNDEFINED(key) :
1715 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1716 wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
1717 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1718 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1720 "%s", "enmic failed, discard frame");
1721 vap->iv_stats.is_crypto_enmicfail++;
1726 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1727 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1730 m->m_flags |= M_ENCAP; /* mark encapsulated */
1732 IEEE80211_NODE_STAT(ni, tx_data);
1733 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1734 IEEE80211_NODE_STAT(ni, tx_mcast);
1735 m->m_flags |= M_MCAST;
1737 IEEE80211_NODE_STAT(ni, tx_ucast);
1738 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1750 ieee80211_free_mbuf(struct mbuf *m)
1758 next = m->m_nextpkt;
1759 m->m_nextpkt = NULL;
1761 } while ((m = next) != NULL);
1765 * Fragment the frame according to the specified mtu.
1766 * The size of the 802.11 header (w/o padding) is provided
1767 * so we don't need to recalculate it. We create a new
1768 * mbuf for each fragment and chain it through m_nextpkt;
1769 * we might be able to optimize this by reusing the original
1770 * packet's mbufs but that is significantly more complicated.
1773 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1774 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1776 struct ieee80211com *ic = vap->iv_ic;
1777 struct ieee80211_frame *wh, *whf;
1778 struct mbuf *m, *prev;
1779 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1782 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1783 KASSERT(m0->m_pkthdr.len > mtu,
1784 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1787 * Honor driver DATAPAD requirement.
1789 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1790 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1794 wh = mtod(m0, struct ieee80211_frame *);
1795 /* NB: mark the first frag; it will be propagated below */
1796 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1797 totalhdrsize = hdrspace + ciphdrsize;
1799 off = mtu - ciphdrsize;
1800 remainder = m0->m_pkthdr.len - off;
1803 fragsize = totalhdrsize + remainder;
1806 /* XXX fragsize can be >2048! */
1807 KASSERT(fragsize < MCLBYTES,
1808 ("fragment size %u too big!", fragsize));
1809 if (fragsize > MHLEN)
1810 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1812 m = m_gethdr(M_NOWAIT, MT_DATA);
1815 /* leave room to prepend any cipher header */
1816 m_align(m, fragsize - ciphdrsize);
1819 * Form the header in the fragment. Note that since
1820 * we mark the first fragment with the MORE_FRAG bit
1821 * it automatically is propagated to each fragment; we
1822 * need only clear it on the last fragment (done below).
1823 * NB: frag 1+ dont have Mesh Control field present.
1825 whf = mtod(m, struct ieee80211_frame *);
1826 memcpy(whf, wh, hdrsize);
1827 #ifdef IEEE80211_SUPPORT_MESH
1828 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1829 if (IEEE80211_IS_DSTODS(wh))
1830 ((struct ieee80211_qosframe_addr4 *)
1831 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1833 ((struct ieee80211_qosframe *)
1834 whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1837 *(uint16_t *)&whf->i_seq[0] |= htole16(
1838 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1839 IEEE80211_SEQ_FRAG_SHIFT);
1842 payload = fragsize - totalhdrsize;
1843 /* NB: destination is known to be contiguous */
1845 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1846 m->m_len = hdrspace + payload;
1847 m->m_pkthdr.len = hdrspace + payload;
1848 m->m_flags |= M_FRAG;
1850 /* chain up the fragment */
1851 prev->m_nextpkt = m;
1854 /* deduct fragment just formed */
1855 remainder -= payload;
1857 } while (remainder != 0);
1859 /* set the last fragment */
1860 m->m_flags |= M_LASTFRAG;
1861 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1863 /* strip first mbuf now that everything has been copied */
1864 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1865 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1867 vap->iv_stats.is_tx_fragframes++;
1868 vap->iv_stats.is_tx_frags += fragno-1;
1872 /* reclaim fragments but leave original frame for caller to free */
1873 ieee80211_free_mbuf(m0->m_nextpkt);
1874 m0->m_nextpkt = NULL;
1879 * Add a supported rates element id to a frame.
1882 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1886 *frm++ = IEEE80211_ELEMID_RATES;
1887 nrates = rs->rs_nrates;
1888 if (nrates > IEEE80211_RATE_SIZE)
1889 nrates = IEEE80211_RATE_SIZE;
1891 memcpy(frm, rs->rs_rates, nrates);
1892 return frm + nrates;
1896 * Add an extended supported rates element id to a frame.
1899 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1902 * Add an extended supported rates element if operating in 11g mode.
1904 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1905 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1906 *frm++ = IEEE80211_ELEMID_XRATES;
1908 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1915 * Add an ssid element to a frame.
1918 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1920 *frm++ = IEEE80211_ELEMID_SSID;
1922 memcpy(frm, ssid, len);
1927 * Add an erp element to a frame.
1930 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1934 *frm++ = IEEE80211_ELEMID_ERP;
1937 if (ic->ic_nonerpsta != 0)
1938 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1939 if (ic->ic_flags & IEEE80211_F_USEPROT)
1940 erp |= IEEE80211_ERP_USE_PROTECTION;
1941 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1942 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1948 * Add a CFParams element to a frame.
1951 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1953 #define ADDSHORT(frm, v) do { \
1957 *frm++ = IEEE80211_ELEMID_CFPARMS;
1959 *frm++ = 0; /* CFP count */
1960 *frm++ = 2; /* CFP period */
1961 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1962 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1967 static __inline uint8_t *
1968 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1970 memcpy(frm, ie->ie_data, ie->ie_len);
1971 return frm + ie->ie_len;
1974 static __inline uint8_t *
1975 add_ie(uint8_t *frm, const uint8_t *ie)
1977 memcpy(frm, ie, 2 + ie[1]);
1978 return frm + 2 + ie[1];
1981 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1983 * Add a WME information element to a frame.
1986 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1988 static const struct ieee80211_wme_info info = {
1989 .wme_id = IEEE80211_ELEMID_VENDOR,
1990 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1991 .wme_oui = { WME_OUI_BYTES },
1992 .wme_type = WME_OUI_TYPE,
1993 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1994 .wme_version = WME_VERSION,
1997 memcpy(frm, &info, sizeof(info));
1998 return frm + sizeof(info);
2002 * Add a WME parameters element to a frame.
2005 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
2007 #define SM(_v, _f) (((_v) << _f##_S) & _f)
2008 #define ADDSHORT(frm, v) do { \
2012 /* NB: this works 'cuz a param has an info at the front */
2013 static const struct ieee80211_wme_info param = {
2014 .wme_id = IEEE80211_ELEMID_VENDOR,
2015 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2016 .wme_oui = { WME_OUI_BYTES },
2017 .wme_type = WME_OUI_TYPE,
2018 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2019 .wme_version = WME_VERSION,
2023 memcpy(frm, ¶m, sizeof(param));
2024 frm += __offsetof(struct ieee80211_wme_info, wme_info);
2025 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
2026 *frm++ = 0; /* reserved field */
2027 for (i = 0; i < WME_NUM_AC; i++) {
2028 const struct wmeParams *ac =
2029 &wme->wme_bssChanParams.cap_wmeParams[i];
2030 *frm++ = SM(i, WME_PARAM_ACI)
2031 | SM(ac->wmep_acm, WME_PARAM_ACM)
2032 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
2034 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
2035 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
2037 ADDSHORT(frm, ac->wmep_txopLimit);
2043 #undef WME_OUI_BYTES
2046 * Add an 11h Power Constraint element to a frame.
2049 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
2051 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
2052 /* XXX per-vap tx power limit? */
2053 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
2055 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
2057 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
2062 * Add an 11h Power Capability element to a frame.
2065 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
2067 frm[0] = IEEE80211_ELEMID_PWRCAP;
2069 frm[2] = c->ic_minpower;
2070 frm[3] = c->ic_maxpower;
2075 * Add an 11h Supported Channels element to a frame.
2078 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
2080 static const int ielen = 26;
2082 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
2084 /* XXX not correct */
2085 memcpy(frm+2, ic->ic_chan_avail, ielen);
2086 return frm + 2 + ielen;
2090 * Add an 11h Quiet time element to a frame.
2093 ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
2095 struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2097 quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2099 if (vap->iv_quiet_count_value == 1)
2100 vap->iv_quiet_count_value = vap->iv_quiet_count;
2101 else if (vap->iv_quiet_count_value > 1)
2102 vap->iv_quiet_count_value--;
2104 if (vap->iv_quiet_count_value == 0) {
2105 /* value 0 is reserved as per 802.11h standerd */
2106 vap->iv_quiet_count_value = 1;
2109 quiet->tbttcount = vap->iv_quiet_count_value;
2110 quiet->period = vap->iv_quiet_period;
2111 quiet->duration = htole16(vap->iv_quiet_duration);
2112 quiet->offset = htole16(vap->iv_quiet_offset);
2113 return frm + sizeof(*quiet);
2117 * Add an 11h Channel Switch Announcement element to a frame.
2118 * Note that we use the per-vap CSA count to adjust the global
2119 * counter so we can use this routine to form probe response
2120 * frames and get the current count.
2123 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
2125 struct ieee80211com *ic = vap->iv_ic;
2126 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
2128 csa->csa_ie = IEEE80211_ELEMID_CSA;
2130 csa->csa_mode = 1; /* XXX force quiet on channel */
2131 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
2132 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
2133 return frm + sizeof(*csa);
2137 * Add an 11h country information element to a frame.
2140 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
2143 if (ic->ic_countryie == NULL ||
2144 ic->ic_countryie_chan != ic->ic_bsschan) {
2146 * Handle lazy construction of ie. This is done on
2147 * first use and after a channel change that requires
2150 if (ic->ic_countryie != NULL)
2151 IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
2152 ic->ic_countryie = ieee80211_alloc_countryie(ic);
2153 if (ic->ic_countryie == NULL)
2155 ic->ic_countryie_chan = ic->ic_bsschan;
2157 return add_appie(frm, ic->ic_countryie);
2161 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2163 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2164 return (add_ie(frm, vap->iv_wpa_ie));
2166 /* XXX else complain? */
2172 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2174 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2175 return (add_ie(frm, vap->iv_rsn_ie));
2177 /* XXX else complain? */
2183 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2185 if (ni->ni_flags & IEEE80211_NODE_QOS) {
2186 *frm++ = IEEE80211_ELEMID_QOS;
2195 * Send a probe request frame with the specified ssid
2196 * and any optional information element data.
2199 ieee80211_send_probereq(struct ieee80211_node *ni,
2200 const uint8_t sa[IEEE80211_ADDR_LEN],
2201 const uint8_t da[IEEE80211_ADDR_LEN],
2202 const uint8_t bssid[IEEE80211_ADDR_LEN],
2203 const uint8_t *ssid, size_t ssidlen)
2205 struct ieee80211vap *vap = ni->ni_vap;
2206 struct ieee80211com *ic = ni->ni_ic;
2207 const struct ieee80211_txparam *tp;
2208 struct ieee80211_bpf_params params;
2209 struct ieee80211_frame *wh;
2210 const struct ieee80211_rateset *rs;
2215 if (vap->iv_state == IEEE80211_S_CAC) {
2216 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2217 "block %s frame in CAC state", "probe request");
2218 vap->iv_stats.is_tx_badstate++;
2219 return EIO; /* XXX */
2223 * Hold a reference on the node so it doesn't go away until after
2224 * the xmit is complete all the way in the driver. On error we
2225 * will remove our reference.
2227 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2228 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2230 ni, ether_sprintf(ni->ni_macaddr),
2231 ieee80211_node_refcnt(ni)+1);
2232 ieee80211_ref_node(ni);
2235 * prreq frame format
2237 * [tlv] supported rates
2238 * [tlv] RSN (optional)
2239 * [tlv] extended supported rates
2240 * [tlv] WPA (optional)
2241 * [tlv] user-specified ie's
2243 m = ieee80211_getmgtframe(&frm,
2244 ic->ic_headroom + sizeof(struct ieee80211_frame),
2245 2 + IEEE80211_NWID_LEN
2246 + 2 + IEEE80211_RATE_SIZE
2247 + sizeof(struct ieee80211_ie_wpa)
2248 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2249 + sizeof(struct ieee80211_ie_wpa)
2250 + (vap->iv_appie_probereq != NULL ?
2251 vap->iv_appie_probereq->ie_len : 0)
2254 vap->iv_stats.is_tx_nobuf++;
2255 ieee80211_free_node(ni);
2259 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
2260 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2261 frm = ieee80211_add_rates(frm, rs);
2262 frm = ieee80211_add_rsn(frm, vap);
2263 frm = ieee80211_add_xrates(frm, rs);
2264 frm = ieee80211_add_wpa(frm, vap);
2265 if (vap->iv_appie_probereq != NULL)
2266 frm = add_appie(frm, vap->iv_appie_probereq);
2267 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2269 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
2270 ("leading space %zd", M_LEADINGSPACE(m)));
2271 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2273 /* NB: cannot happen */
2274 ieee80211_free_node(ni);
2278 IEEE80211_TX_LOCK(ic);
2279 wh = mtod(m, struct ieee80211_frame *);
2280 ieee80211_send_setup(ni, m,
2281 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
2282 IEEE80211_NONQOS_TID, sa, da, bssid);
2283 /* XXX power management? */
2284 m->m_flags |= M_ENCAP; /* mark encapsulated */
2286 M_WME_SETAC(m, WME_AC_BE);
2288 IEEE80211_NODE_STAT(ni, tx_probereq);
2289 IEEE80211_NODE_STAT(ni, tx_mgmt);
2291 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2292 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2293 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2294 (int)ssidlen, ssid);
2296 memset(¶ms, 0, sizeof(params));
2297 params.ibp_pri = M_WME_GETAC(m);
2298 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2299 params.ibp_rate0 = tp->mgmtrate;
2300 if (IEEE80211_IS_MULTICAST(da)) {
2301 params.ibp_flags |= IEEE80211_BPF_NOACK;
2302 params.ibp_try0 = 1;
2304 params.ibp_try0 = tp->maxretry;
2305 params.ibp_power = ni->ni_txpower;
2306 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2307 IEEE80211_TX_UNLOCK(ic);
2312 * Calculate capability information for mgt frames.
2315 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2317 struct ieee80211com *ic = vap->iv_ic;
2320 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2322 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2323 capinfo = IEEE80211_CAPINFO_ESS;
2324 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2325 capinfo = IEEE80211_CAPINFO_IBSS;
2328 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2329 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2330 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2331 IEEE80211_IS_CHAN_2GHZ(chan))
2332 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2333 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2334 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2335 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
2336 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2341 * Send a management frame. The node is for the destination (or ic_bss
2342 * when in station mode). Nodes other than ic_bss have their reference
2343 * count bumped to reflect our use for an indeterminant time.
2346 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2348 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
2349 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
2350 struct ieee80211vap *vap = ni->ni_vap;
2351 struct ieee80211com *ic = ni->ni_ic;
2352 struct ieee80211_node *bss = vap->iv_bss;
2353 struct ieee80211_bpf_params params;
2357 int has_challenge, is_shared_key, ret, status;
2359 KASSERT(ni != NULL, ("null node"));
2362 * Hold a reference on the node so it doesn't go away until after
2363 * the xmit is complete all the way in the driver. On error we
2364 * will remove our reference.
2366 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2367 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2369 ni, ether_sprintf(ni->ni_macaddr),
2370 ieee80211_node_refcnt(ni)+1);
2371 ieee80211_ref_node(ni);
2373 memset(¶ms, 0, sizeof(params));
2376 case IEEE80211_FC0_SUBTYPE_AUTH:
2379 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2380 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2381 ni->ni_challenge != NULL);
2384 * Deduce whether we're doing open authentication or
2385 * shared key authentication. We do the latter if
2386 * we're in the middle of a shared key authentication
2387 * handshake or if we're initiating an authentication
2388 * request and configured to use shared key.
2390 is_shared_key = has_challenge ||
2391 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2392 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
2393 bss->ni_authmode == IEEE80211_AUTH_SHARED);
2395 m = ieee80211_getmgtframe(&frm,
2396 ic->ic_headroom + sizeof(struct ieee80211_frame),
2397 3 * sizeof(uint16_t)
2398 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2399 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2402 senderr(ENOMEM, is_tx_nobuf);
2404 ((uint16_t *)frm)[0] =
2405 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2406 : htole16(IEEE80211_AUTH_ALG_OPEN);
2407 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2408 ((uint16_t *)frm)[2] = htole16(status);/* status */
2410 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2411 ((uint16_t *)frm)[3] =
2412 htole16((IEEE80211_CHALLENGE_LEN << 8) |
2413 IEEE80211_ELEMID_CHALLENGE);
2414 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2415 IEEE80211_CHALLENGE_LEN);
2416 m->m_pkthdr.len = m->m_len =
2417 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2418 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
2419 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2420 "request encrypt frame (%s)", __func__);
2421 /* mark frame for encryption */
2422 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2425 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2427 /* XXX not right for shared key */
2428 if (status == IEEE80211_STATUS_SUCCESS)
2429 IEEE80211_NODE_STAT(ni, tx_auth);
2431 IEEE80211_NODE_STAT(ni, tx_auth_fail);
2433 if (vap->iv_opmode == IEEE80211_M_STA)
2434 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2435 (void *) vap->iv_state);
2438 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2439 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
2440 "send station deauthenticate (reason: %d (%s))", arg,
2441 ieee80211_reason_to_string(arg));
2442 m = ieee80211_getmgtframe(&frm,
2443 ic->ic_headroom + sizeof(struct ieee80211_frame),
2446 senderr(ENOMEM, is_tx_nobuf);
2447 *(uint16_t *)frm = htole16(arg); /* reason */
2448 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2450 IEEE80211_NODE_STAT(ni, tx_deauth);
2451 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2453 ieee80211_node_unauthorize(ni); /* port closed */
2456 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2457 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2459 * asreq frame format
2460 * [2] capability information
2461 * [2] listen interval
2462 * [6*] current AP address (reassoc only)
2464 * [tlv] supported rates
2465 * [tlv] extended supported rates
2466 * [4] power capability (optional)
2467 * [28] supported channels (optional)
2468 * [tlv] HT capabilities
2469 * [tlv] WME (optional)
2470 * [tlv] Vendor OUI HT capabilities (optional)
2471 * [tlv] Atheros capabilities (if negotiated)
2472 * [tlv] AppIE's (optional)
2474 m = ieee80211_getmgtframe(&frm,
2475 ic->ic_headroom + sizeof(struct ieee80211_frame),
2478 + IEEE80211_ADDR_LEN
2479 + 2 + IEEE80211_NWID_LEN
2480 + 2 + IEEE80211_RATE_SIZE
2481 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2484 + sizeof(struct ieee80211_wme_info)
2485 + sizeof(struct ieee80211_ie_htcap)
2486 + 4 + sizeof(struct ieee80211_ie_htcap)
2487 #ifdef IEEE80211_SUPPORT_SUPERG
2488 + sizeof(struct ieee80211_ath_ie)
2490 + (vap->iv_appie_wpa != NULL ?
2491 vap->iv_appie_wpa->ie_len : 0)
2492 + (vap->iv_appie_assocreq != NULL ?
2493 vap->iv_appie_assocreq->ie_len : 0)
2496 senderr(ENOMEM, is_tx_nobuf);
2498 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2499 ("wrong mode %u", vap->iv_opmode));
2500 capinfo = IEEE80211_CAPINFO_ESS;
2501 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2502 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2504 * NB: Some 11a AP's reject the request when
2505 * short premable is set.
2507 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2508 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2509 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2510 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2511 (ic->ic_caps & IEEE80211_C_SHSLOT))
2512 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2513 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2514 (vap->iv_flags & IEEE80211_F_DOTH))
2515 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2516 *(uint16_t *)frm = htole16(capinfo);
2519 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2520 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2524 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2525 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2526 frm += IEEE80211_ADDR_LEN;
2529 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2530 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2531 frm = ieee80211_add_rsn(frm, vap);
2532 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2533 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2534 frm = ieee80211_add_powercapability(frm,
2536 frm = ieee80211_add_supportedchannels(frm, ic);
2540 * Check the channel - we may be using an 11n NIC with an
2541 * 11n capable station, but we're configured to be an 11b
2544 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2545 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2546 ni->ni_ies.htcap_ie != NULL &&
2547 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
2548 frm = ieee80211_add_htcap(frm, ni);
2550 frm = ieee80211_add_wpa(frm, vap);
2551 if ((ic->ic_flags & IEEE80211_F_WME) &&
2552 ni->ni_ies.wme_ie != NULL)
2553 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2556 * Same deal - only send HT info if we're on an 11n
2559 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2560 IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2561 ni->ni_ies.htcap_ie != NULL &&
2562 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
2563 frm = ieee80211_add_htcap_vendor(frm, ni);
2565 #ifdef IEEE80211_SUPPORT_SUPERG
2566 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2567 frm = ieee80211_add_ath(frm,
2568 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2569 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2570 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2571 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2573 #endif /* IEEE80211_SUPPORT_SUPERG */
2574 if (vap->iv_appie_assocreq != NULL)
2575 frm = add_appie(frm, vap->iv_appie_assocreq);
2576 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2578 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2579 (void *) vap->iv_state);
2582 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2583 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2585 * asresp frame format
2586 * [2] capability information
2588 * [2] association ID
2589 * [tlv] supported rates
2590 * [tlv] extended supported rates
2591 * [tlv] HT capabilities (standard, if STA enabled)
2592 * [tlv] HT information (standard, if STA enabled)
2593 * [tlv] WME (if configured and STA enabled)
2594 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2595 * [tlv] HT information (vendor OUI, if STA enabled)
2596 * [tlv] Atheros capabilities (if STA enabled)
2597 * [tlv] AppIE's (optional)
2599 m = ieee80211_getmgtframe(&frm,
2600 ic->ic_headroom + sizeof(struct ieee80211_frame),
2604 + 2 + IEEE80211_RATE_SIZE
2605 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2606 + sizeof(struct ieee80211_ie_htcap) + 4
2607 + sizeof(struct ieee80211_ie_htinfo) + 4
2608 + sizeof(struct ieee80211_wme_param)
2609 #ifdef IEEE80211_SUPPORT_SUPERG
2610 + sizeof(struct ieee80211_ath_ie)
2612 + (vap->iv_appie_assocresp != NULL ?
2613 vap->iv_appie_assocresp->ie_len : 0)
2616 senderr(ENOMEM, is_tx_nobuf);
2618 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2619 *(uint16_t *)frm = htole16(capinfo);
2622 *(uint16_t *)frm = htole16(arg); /* status */
2625 if (arg == IEEE80211_STATUS_SUCCESS) {
2626 *(uint16_t *)frm = htole16(ni->ni_associd);
2627 IEEE80211_NODE_STAT(ni, tx_assoc);
2629 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2632 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2633 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2634 /* NB: respond according to what we received */
2635 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2636 frm = ieee80211_add_htcap(frm, ni);
2637 frm = ieee80211_add_htinfo(frm, ni);
2639 if ((vap->iv_flags & IEEE80211_F_WME) &&
2640 ni->ni_ies.wme_ie != NULL)
2641 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2642 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2643 frm = ieee80211_add_htcap_vendor(frm, ni);
2644 frm = ieee80211_add_htinfo_vendor(frm, ni);
2646 #ifdef IEEE80211_SUPPORT_SUPERG
2647 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2648 frm = ieee80211_add_ath(frm,
2649 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2650 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2651 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2652 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2653 #endif /* IEEE80211_SUPPORT_SUPERG */
2654 if (vap->iv_appie_assocresp != NULL)
2655 frm = add_appie(frm, vap->iv_appie_assocresp);
2656 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2659 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2660 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2661 "send station disassociate (reason: %d (%s))", arg,
2662 ieee80211_reason_to_string(arg));
2663 m = ieee80211_getmgtframe(&frm,
2664 ic->ic_headroom + sizeof(struct ieee80211_frame),
2667 senderr(ENOMEM, is_tx_nobuf);
2668 *(uint16_t *)frm = htole16(arg); /* reason */
2669 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2671 IEEE80211_NODE_STAT(ni, tx_disassoc);
2672 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2676 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2677 "invalid mgmt frame type %u", type);
2678 senderr(EINVAL, is_tx_unknownmgt);
2682 /* NB: force non-ProbeResp frames to the highest queue */
2683 params.ibp_pri = WME_AC_VO;
2684 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2685 /* NB: we know all frames are unicast */
2686 params.ibp_try0 = bss->ni_txparms->maxretry;
2687 params.ibp_power = bss->ni_txpower;
2688 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2690 ieee80211_free_node(ni);
2697 * Return an mbuf with a probe response frame in it.
2698 * Space is left to prepend and 802.11 header at the
2699 * front but it's left to the caller to fill in.
2702 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2704 struct ieee80211vap *vap = bss->ni_vap;
2705 struct ieee80211com *ic = bss->ni_ic;
2706 const struct ieee80211_rateset *rs;
2712 * probe response frame format
2714 * [2] beacon interval
2715 * [2] cabability information
2717 * [tlv] supported rates
2718 * [tlv] parameter set (FH/DS)
2719 * [tlv] parameter set (IBSS)
2720 * [tlv] country (optional)
2721 * [3] power control (optional)
2722 * [5] channel switch announcement (CSA) (optional)
2723 * [tlv] extended rate phy (ERP)
2724 * [tlv] extended supported rates
2725 * [tlv] RSN (optional)
2726 * [tlv] HT capabilities
2727 * [tlv] HT information
2728 * [tlv] WPA (optional)
2729 * [tlv] WME (optional)
2730 * [tlv] Vendor OUI HT capabilities (optional)
2731 * [tlv] Vendor OUI HT information (optional)
2732 * [tlv] Atheros capabilities
2733 * [tlv] AppIE's (optional)
2734 * [tlv] Mesh ID (MBSS)
2735 * [tlv] Mesh Conf (MBSS)
2737 m = ieee80211_getmgtframe(&frm,
2738 ic->ic_headroom + sizeof(struct ieee80211_frame),
2742 + 2 + IEEE80211_NWID_LEN
2743 + 2 + IEEE80211_RATE_SIZE
2745 + IEEE80211_COUNTRY_MAX_SIZE
2747 + sizeof(struct ieee80211_csa_ie)
2748 + sizeof(struct ieee80211_quiet_ie)
2750 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2751 + sizeof(struct ieee80211_ie_wpa)
2752 + sizeof(struct ieee80211_ie_htcap)
2753 + sizeof(struct ieee80211_ie_htinfo)
2754 + sizeof(struct ieee80211_ie_wpa)
2755 + sizeof(struct ieee80211_wme_param)
2756 + 4 + sizeof(struct ieee80211_ie_htcap)
2757 + 4 + sizeof(struct ieee80211_ie_htinfo)
2758 #ifdef IEEE80211_SUPPORT_SUPERG
2759 + sizeof(struct ieee80211_ath_ie)
2761 #ifdef IEEE80211_SUPPORT_MESH
2762 + 2 + IEEE80211_MESHID_LEN
2763 + sizeof(struct ieee80211_meshconf_ie)
2765 + (vap->iv_appie_proberesp != NULL ?
2766 vap->iv_appie_proberesp->ie_len : 0)
2769 vap->iv_stats.is_tx_nobuf++;
2773 memset(frm, 0, 8); /* timestamp should be filled later */
2775 *(uint16_t *)frm = htole16(bss->ni_intval);
2777 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2778 *(uint16_t *)frm = htole16(capinfo);
2781 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2782 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2783 frm = ieee80211_add_rates(frm, rs);
2785 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2786 *frm++ = IEEE80211_ELEMID_FHPARMS;
2788 *frm++ = bss->ni_fhdwell & 0x00ff;
2789 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2790 *frm++ = IEEE80211_FH_CHANSET(
2791 ieee80211_chan2ieee(ic, bss->ni_chan));
2792 *frm++ = IEEE80211_FH_CHANPAT(
2793 ieee80211_chan2ieee(ic, bss->ni_chan));
2794 *frm++ = bss->ni_fhindex;
2796 *frm++ = IEEE80211_ELEMID_DSPARMS;
2798 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2801 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2802 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2804 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2806 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2807 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2808 frm = ieee80211_add_countryie(frm, ic);
2809 if (vap->iv_flags & IEEE80211_F_DOTH) {
2810 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2811 frm = ieee80211_add_powerconstraint(frm, vap);
2812 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2813 frm = ieee80211_add_csa(frm, vap);
2815 if (vap->iv_flags & IEEE80211_F_DOTH) {
2816 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2817 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2819 frm = ieee80211_add_quiet(frm, vap);
2822 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2823 frm = ieee80211_add_erp(frm, ic);
2824 frm = ieee80211_add_xrates(frm, rs);
2825 frm = ieee80211_add_rsn(frm, vap);
2827 * NB: legacy 11b clients do not get certain ie's.
2828 * The caller identifies such clients by passing
2829 * a token in legacy to us. Could expand this to be
2830 * any legacy client for stuff like HT ie's.
2832 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2833 legacy != IEEE80211_SEND_LEGACY_11B) {
2834 frm = ieee80211_add_htcap(frm, bss);
2835 frm = ieee80211_add_htinfo(frm, bss);
2837 frm = ieee80211_add_wpa(frm, vap);
2838 if (vap->iv_flags & IEEE80211_F_WME)
2839 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2840 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2841 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2842 legacy != IEEE80211_SEND_LEGACY_11B) {
2843 frm = ieee80211_add_htcap_vendor(frm, bss);
2844 frm = ieee80211_add_htinfo_vendor(frm, bss);
2846 #ifdef IEEE80211_SUPPORT_SUPERG
2847 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2848 legacy != IEEE80211_SEND_LEGACY_11B)
2849 frm = ieee80211_add_athcaps(frm, bss);
2851 if (vap->iv_appie_proberesp != NULL)
2852 frm = add_appie(frm, vap->iv_appie_proberesp);
2853 #ifdef IEEE80211_SUPPORT_MESH
2854 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2855 frm = ieee80211_add_meshid(frm, vap);
2856 frm = ieee80211_add_meshconf(frm, vap);
2859 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2865 * Send a probe response frame to the specified mac address.
2866 * This does not go through the normal mgt frame api so we
2867 * can specify the destination address and re-use the bss node
2868 * for the sta reference.
2871 ieee80211_send_proberesp(struct ieee80211vap *vap,
2872 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2874 struct ieee80211_node *bss = vap->iv_bss;
2875 struct ieee80211com *ic = vap->iv_ic;
2876 struct ieee80211_frame *wh;
2880 if (vap->iv_state == IEEE80211_S_CAC) {
2881 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2882 "block %s frame in CAC state", "probe response");
2883 vap->iv_stats.is_tx_badstate++;
2884 return EIO; /* XXX */
2888 * Hold a reference on the node so it doesn't go away until after
2889 * the xmit is complete all the way in the driver. On error we
2890 * will remove our reference.
2892 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2893 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2894 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2895 ieee80211_node_refcnt(bss)+1);
2896 ieee80211_ref_node(bss);
2898 m = ieee80211_alloc_proberesp(bss, legacy);
2900 ieee80211_free_node(bss);
2904 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2905 KASSERT(m != NULL, ("no room for header"));
2907 IEEE80211_TX_LOCK(ic);
2908 wh = mtod(m, struct ieee80211_frame *);
2909 ieee80211_send_setup(bss, m,
2910 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2911 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2912 /* XXX power management? */
2913 m->m_flags |= M_ENCAP; /* mark encapsulated */
2915 M_WME_SETAC(m, WME_AC_BE);
2917 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2918 "send probe resp on channel %u to %s%s\n",
2919 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2920 legacy ? " <legacy>" : "");
2921 IEEE80211_NODE_STAT(bss, tx_mgmt);
2923 ret = ieee80211_raw_output(vap, bss, m, NULL);
2924 IEEE80211_TX_UNLOCK(ic);
2929 * Allocate and build a RTS (Request To Send) control frame.
2932 ieee80211_alloc_rts(struct ieee80211com *ic,
2933 const uint8_t ra[IEEE80211_ADDR_LEN],
2934 const uint8_t ta[IEEE80211_ADDR_LEN],
2937 struct ieee80211_frame_rts *rts;
2940 /* XXX honor ic_headroom */
2941 m = m_gethdr(M_NOWAIT, MT_DATA);
2943 rts = mtod(m, struct ieee80211_frame_rts *);
2944 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2945 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2946 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2947 *(u_int16_t *)rts->i_dur = htole16(dur);
2948 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2949 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2951 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2957 * Allocate and build a CTS (Clear To Send) control frame.
2960 ieee80211_alloc_cts(struct ieee80211com *ic,
2961 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2963 struct ieee80211_frame_cts *cts;
2966 /* XXX honor ic_headroom */
2967 m = m_gethdr(M_NOWAIT, MT_DATA);
2969 cts = mtod(m, struct ieee80211_frame_cts *);
2970 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2971 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2972 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2973 *(u_int16_t *)cts->i_dur = htole16(dur);
2974 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2976 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2982 ieee80211_tx_mgt_timeout(void *arg)
2984 struct ieee80211vap *vap = arg;
2986 IEEE80211_LOCK(vap->iv_ic);
2987 if (vap->iv_state != IEEE80211_S_INIT &&
2988 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2990 * NB: it's safe to specify a timeout as the reason here;
2991 * it'll only be used in the right state.
2993 ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
2994 IEEE80211_SCAN_FAIL_TIMEOUT);
2996 IEEE80211_UNLOCK(vap->iv_ic);
3000 * This is the callback set on net80211-sourced transmitted
3001 * authentication request frames.
3003 * This does a couple of things:
3005 * + If the frame transmitted was a success, it schedules a future
3006 * event which will transition the interface to scan.
3007 * If a state transition _then_ occurs before that event occurs,
3008 * said state transition will cancel this callout.
3010 * + If the frame transmit was a failure, it immediately schedules
3011 * the transition back to scan.
3014 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
3016 struct ieee80211vap *vap = ni->ni_vap;
3017 enum ieee80211_state ostate = (enum ieee80211_state) arg;
3020 * Frame transmit completed; arrange timer callback. If
3021 * transmit was successfully we wait for response. Otherwise
3022 * we arrange an immediate callback instead of doing the
3023 * callback directly since we don't know what state the driver
3024 * is in (e.g. what locks it is holding). This work should
3025 * not be too time-critical and not happen too often so the
3026 * added overhead is acceptable.
3028 * XXX what happens if !acked but response shows up before callback?
3030 if (vap->iv_state == ostate) {
3031 callout_reset(&vap->iv_mgtsend,
3032 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3033 ieee80211_tx_mgt_timeout, vap);
3038 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
3039 struct ieee80211_node *ni)
3041 struct ieee80211vap *vap = ni->ni_vap;
3042 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3043 struct ieee80211com *ic = ni->ni_ic;
3044 struct ieee80211_rateset *rs = &ni->ni_rates;
3048 * beacon frame format
3050 * [2] beacon interval
3051 * [2] cabability information
3053 * [tlv] supported rates
3054 * [3] parameter set (DS)
3055 * [8] CF parameter set (optional)
3056 * [tlv] parameter set (IBSS/TIM)
3057 * [tlv] country (optional)
3058 * [3] power control (optional)
3059 * [5] channel switch announcement (CSA) (optional)
3060 * [tlv] extended rate phy (ERP)
3061 * [tlv] extended supported rates
3062 * [tlv] RSN parameters
3063 * [tlv] HT capabilities
3064 * [tlv] HT information
3065 * XXX Vendor-specific OIDs (e.g. Atheros)
3066 * [tlv] WPA parameters
3067 * [tlv] WME parameters
3068 * [tlv] Vendor OUI HT capabilities (optional)
3069 * [tlv] Vendor OUI HT information (optional)
3070 * [tlv] Atheros capabilities (optional)
3071 * [tlv] TDMA parameters (optional)
3072 * [tlv] Mesh ID (MBSS)
3073 * [tlv] Mesh Conf (MBSS)
3074 * [tlv] application data (optional)
3077 memset(bo, 0, sizeof(*bo));
3079 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3081 *(uint16_t *)frm = htole16(ni->ni_intval);
3083 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3084 bo->bo_caps = (uint16_t *)frm;
3085 *(uint16_t *)frm = htole16(capinfo);
3087 *frm++ = IEEE80211_ELEMID_SSID;
3088 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3089 *frm++ = ni->ni_esslen;
3090 memcpy(frm, ni->ni_essid, ni->ni_esslen);
3091 frm += ni->ni_esslen;
3094 frm = ieee80211_add_rates(frm, rs);
3095 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3096 *frm++ = IEEE80211_ELEMID_DSPARMS;
3098 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3100 if (ic->ic_flags & IEEE80211_F_PCF) {
3102 frm = ieee80211_add_cfparms(frm, ic);
3105 if (vap->iv_opmode == IEEE80211_M_IBSS) {
3106 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3108 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3110 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3111 vap->iv_opmode == IEEE80211_M_MBSS) {
3112 /* TIM IE is the same for Mesh and Hostap */
3113 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3115 tie->tim_ie = IEEE80211_ELEMID_TIM;
3116 tie->tim_len = 4; /* length */
3117 tie->tim_count = 0; /* DTIM count */
3118 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3119 tie->tim_bitctl = 0; /* bitmap control */
3120 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3121 frm += sizeof(struct ieee80211_tim_ie);
3124 bo->bo_tim_trailer = frm;
3125 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
3126 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
3127 frm = ieee80211_add_countryie(frm, ic);
3128 if (vap->iv_flags & IEEE80211_F_DOTH) {
3129 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
3130 frm = ieee80211_add_powerconstraint(frm, vap);
3132 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
3133 frm = ieee80211_add_csa(frm, vap);
3137 if (vap->iv_flags & IEEE80211_F_DOTH) {
3139 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3140 (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3142 frm = ieee80211_add_quiet(frm,vap);
3147 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3149 frm = ieee80211_add_erp(frm, ic);
3151 frm = ieee80211_add_xrates(frm, rs);
3152 frm = ieee80211_add_rsn(frm, vap);
3153 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
3154 frm = ieee80211_add_htcap(frm, ni);
3155 bo->bo_htinfo = frm;
3156 frm = ieee80211_add_htinfo(frm, ni);
3158 frm = ieee80211_add_wpa(frm, vap);
3159 if (vap->iv_flags & IEEE80211_F_WME) {
3161 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
3163 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
3164 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
3165 frm = ieee80211_add_htcap_vendor(frm, ni);
3166 frm = ieee80211_add_htinfo_vendor(frm, ni);
3168 #ifdef IEEE80211_SUPPORT_SUPERG
3169 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
3171 frm = ieee80211_add_athcaps(frm, ni);
3174 #ifdef IEEE80211_SUPPORT_TDMA
3175 if (vap->iv_caps & IEEE80211_C_TDMA) {
3177 frm = ieee80211_add_tdma(frm, vap);
3180 if (vap->iv_appie_beacon != NULL) {
3182 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
3183 frm = add_appie(frm, vap->iv_appie_beacon);
3185 #ifdef IEEE80211_SUPPORT_MESH
3186 if (vap->iv_opmode == IEEE80211_M_MBSS) {
3187 frm = ieee80211_add_meshid(frm, vap);
3188 bo->bo_meshconf = frm;
3189 frm = ieee80211_add_meshconf(frm, vap);
3192 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
3193 bo->bo_csa_trailer_len = frm - bo->bo_csa;
3194 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3198 * Allocate a beacon frame and fillin the appropriate bits.
3201 ieee80211_beacon_alloc(struct ieee80211_node *ni)
3203 struct ieee80211vap *vap = ni->ni_vap;
3204 struct ieee80211com *ic = ni->ni_ic;
3205 struct ifnet *ifp = vap->iv_ifp;
3206 struct ieee80211_frame *wh;
3212 * beacon frame format
3214 * [2] beacon interval
3215 * [2] cabability information
3217 * [tlv] supported rates
3218 * [3] parameter set (DS)
3219 * [8] CF parameter set (optional)
3220 * [tlv] parameter set (IBSS/TIM)
3221 * [tlv] country (optional)
3222 * [3] power control (optional)
3223 * [5] channel switch announcement (CSA) (optional)
3224 * [tlv] extended rate phy (ERP)
3225 * [tlv] extended supported rates
3226 * [tlv] RSN parameters
3227 * [tlv] HT capabilities
3228 * [tlv] HT information
3229 * [tlv] Vendor OUI HT capabilities (optional)
3230 * [tlv] Vendor OUI HT information (optional)
3231 * XXX Vendor-specific OIDs (e.g. Atheros)
3232 * [tlv] WPA parameters
3233 * [tlv] WME parameters
3234 * [tlv] TDMA parameters (optional)
3235 * [tlv] Mesh ID (MBSS)
3236 * [tlv] Mesh Conf (MBSS)
3237 * [tlv] application data (optional)
3238 * NB: we allocate the max space required for the TIM bitmap.
3239 * XXX how big is this?
3241 pktlen = 8 /* time stamp */
3242 + sizeof(uint16_t) /* beacon interval */
3243 + sizeof(uint16_t) /* capabilities */
3244 + 2 + ni->ni_esslen /* ssid */
3245 + 2 + IEEE80211_RATE_SIZE /* supported rates */
3246 + 2 + 1 /* DS parameters */
3247 + 2 + 6 /* CF parameters */
3248 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
3249 + IEEE80211_COUNTRY_MAX_SIZE /* country */
3250 + 2 + 1 /* power control */
3251 + sizeof(struct ieee80211_csa_ie) /* CSA */
3252 + sizeof(struct ieee80211_quiet_ie) /* Quiet */
3254 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
3255 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
3256 2*sizeof(struct ieee80211_ie_wpa) : 0)
3257 /* XXX conditional? */
3258 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
3259 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
3260 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
3261 sizeof(struct ieee80211_wme_param) : 0)
3262 #ifdef IEEE80211_SUPPORT_SUPERG
3263 + sizeof(struct ieee80211_ath_ie) /* ATH */
3265 #ifdef IEEE80211_SUPPORT_TDMA
3266 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
3267 sizeof(struct ieee80211_tdma_param) : 0)
3269 #ifdef IEEE80211_SUPPORT_MESH
3270 + 2 + ni->ni_meshidlen
3271 + sizeof(struct ieee80211_meshconf_ie)
3273 + IEEE80211_MAX_APPIE
3275 m = ieee80211_getmgtframe(&frm,
3276 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
3278 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
3279 "%s: cannot get buf; size %u\n", __func__, pktlen);
3280 vap->iv_stats.is_tx_nobuf++;
3283 ieee80211_beacon_construct(m, frm, ni);
3285 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3286 KASSERT(m != NULL, ("no space for 802.11 header?"));
3287 wh = mtod(m, struct ieee80211_frame *);
3288 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3289 IEEE80211_FC0_SUBTYPE_BEACON;
3290 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3291 *(uint16_t *)wh->i_dur = 0;
3292 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3293 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3294 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3295 *(uint16_t *)wh->i_seq = 0;
3301 * Update the dynamic parts of a beacon frame based on the current state.
3304 ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3306 struct ieee80211vap *vap = ni->ni_vap;
3307 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
3308 struct ieee80211com *ic = ni->ni_ic;
3309 int len_changed = 0;
3311 struct ieee80211_frame *wh;
3312 ieee80211_seq seqno;
3316 * Handle 11h channel change when we've reached the count.
3317 * We must recalculate the beacon frame contents to account
3318 * for the new channel. Note we do this only for the first
3319 * vap that reaches this point; subsequent vaps just update
3320 * their beacon state to reflect the recalculated channel.
3322 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
3323 vap->iv_csa_count == ic->ic_csa_count) {
3324 vap->iv_csa_count = 0;
3326 * Effect channel change before reconstructing the beacon
3327 * frame contents as many places reference ni_chan.
3329 if (ic->ic_csa_newchan != NULL)
3330 ieee80211_csa_completeswitch(ic);
3332 * NB: ieee80211_beacon_construct clears all pending
3333 * updates in bo_flags so we don't need to explicitly
3334 * clear IEEE80211_BEACON_CSA.
3336 ieee80211_beacon_construct(m,
3337 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
3339 /* XXX do WME aggressive mode processing? */
3340 IEEE80211_UNLOCK(ic);
3341 return 1; /* just assume length changed */
3344 wh = mtod(m, struct ieee80211_frame *);
3345 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3346 *(uint16_t *)&wh->i_seq[0] =
3347 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3348 M_SEQNO_SET(m, seqno);
3350 /* XXX faster to recalculate entirely or just changes? */
3351 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3352 *bo->bo_caps = htole16(capinfo);
3354 if (vap->iv_flags & IEEE80211_F_WME) {
3355 struct ieee80211_wme_state *wme = &ic->ic_wme;
3358 * Check for aggressive mode change. When there is
3359 * significant high priority traffic in the BSS
3360 * throttle back BE traffic by using conservative
3361 * parameters. Otherwise BE uses aggressive params
3362 * to optimize performance of legacy/non-QoS traffic.
3364 if (wme->wme_flags & WME_F_AGGRMODE) {
3365 if (wme->wme_hipri_traffic >
3366 wme->wme_hipri_switch_thresh) {
3367 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3368 "%s: traffic %u, disable aggressive mode\n",
3369 __func__, wme->wme_hipri_traffic);
3370 wme->wme_flags &= ~WME_F_AGGRMODE;
3371 ieee80211_wme_updateparams_locked(vap);
3372 wme->wme_hipri_traffic =
3373 wme->wme_hipri_switch_hysteresis;
3375 wme->wme_hipri_traffic = 0;
3377 if (wme->wme_hipri_traffic <=
3378 wme->wme_hipri_switch_thresh) {
3379 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3380 "%s: traffic %u, enable aggressive mode\n",
3381 __func__, wme->wme_hipri_traffic);
3382 wme->wme_flags |= WME_F_AGGRMODE;
3383 ieee80211_wme_updateparams_locked(vap);
3384 wme->wme_hipri_traffic = 0;
3386 wme->wme_hipri_traffic =
3387 wme->wme_hipri_switch_hysteresis;
3389 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3390 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
3391 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3395 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
3396 ieee80211_ht_update_beacon(vap, bo);
3397 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
3399 #ifdef IEEE80211_SUPPORT_TDMA
3400 if (vap->iv_caps & IEEE80211_C_TDMA) {
3402 * NB: the beacon is potentially updated every TBTT.
3404 ieee80211_tdma_update_beacon(vap, bo);
3407 #ifdef IEEE80211_SUPPORT_MESH
3408 if (vap->iv_opmode == IEEE80211_M_MBSS)
3409 ieee80211_mesh_update_beacon(vap, bo);
3412 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
3413 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3414 struct ieee80211_tim_ie *tie =
3415 (struct ieee80211_tim_ie *) bo->bo_tim;
3416 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3417 u_int timlen, timoff, i;
3419 * ATIM/DTIM needs updating. If it fits in the
3420 * current space allocated then just copy in the
3421 * new bits. Otherwise we need to move any trailing
3422 * data to make room. Note that we know there is
3423 * contiguous space because ieee80211_beacon_allocate
3424 * insures there is space in the mbuf to write a
3425 * maximal-size virtual bitmap (based on iv_max_aid).
3428 * Calculate the bitmap size and offset, copy any
3429 * trailer out of the way, and then copy in the
3430 * new bitmap and update the information element.
3431 * Note that the tim bitmap must contain at least
3432 * one byte and any offset must be even.
3434 if (vap->iv_ps_pending != 0) {
3435 timoff = 128; /* impossibly large */
3436 for (i = 0; i < vap->iv_tim_len; i++)
3437 if (vap->iv_tim_bitmap[i]) {
3441 KASSERT(timoff != 128, ("tim bitmap empty!"));
3442 for (i = vap->iv_tim_len-1; i >= timoff; i--)
3443 if (vap->iv_tim_bitmap[i])
3445 timlen = 1 + (i - timoff);
3450 if (timlen != bo->bo_tim_len) {
3451 /* copy up/down trailer */
3452 int adjust = tie->tim_bitmap+timlen
3453 - bo->bo_tim_trailer;
3454 ovbcopy(bo->bo_tim_trailer,
3455 bo->bo_tim_trailer+adjust,
3456 bo->bo_tim_trailer_len);
3457 bo->bo_tim_trailer += adjust;
3458 bo->bo_erp += adjust;
3459 bo->bo_htinfo += adjust;
3460 #ifdef IEEE80211_SUPPORT_SUPERG
3461 bo->bo_ath += adjust;
3463 #ifdef IEEE80211_SUPPORT_TDMA
3464 bo->bo_tdma += adjust;
3466 #ifdef IEEE80211_SUPPORT_MESH
3467 bo->bo_meshconf += adjust;
3469 bo->bo_appie += adjust;
3470 bo->bo_wme += adjust;
3471 bo->bo_csa += adjust;
3472 bo->bo_quiet += adjust;
3473 bo->bo_tim_len = timlen;
3475 /* update information element */
3476 tie->tim_len = 3 + timlen;
3477 tie->tim_bitctl = timoff;
3480 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3483 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3485 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3486 "%s: TIM updated, pending %u, off %u, len %u\n",
3487 __func__, vap->iv_ps_pending, timoff, timlen);
3489 /* count down DTIM period */
3490 if (tie->tim_count == 0)
3491 tie->tim_count = tie->tim_period - 1;
3494 /* update state for buffered multicast frames on DTIM */
3495 if (mcast && tie->tim_count == 0)
3496 tie->tim_bitctl |= 1;
3498 tie->tim_bitctl &= ~1;
3499 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
3500 struct ieee80211_csa_ie *csa =
3501 (struct ieee80211_csa_ie *) bo->bo_csa;
3504 * Insert or update CSA ie. If we're just starting
3505 * to count down to the channel switch then we need
3506 * to insert the CSA ie. Otherwise we just need to
3507 * drop the count. The actual change happens above
3508 * when the vap's count reaches the target count.
3510 if (vap->iv_csa_count == 0) {
3511 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
3512 bo->bo_erp += sizeof(*csa);
3513 bo->bo_htinfo += sizeof(*csa);
3514 bo->bo_wme += sizeof(*csa);
3515 #ifdef IEEE80211_SUPPORT_SUPERG
3516 bo->bo_ath += sizeof(*csa);
3518 #ifdef IEEE80211_SUPPORT_TDMA
3519 bo->bo_tdma += sizeof(*csa);
3521 #ifdef IEEE80211_SUPPORT_MESH
3522 bo->bo_meshconf += sizeof(*csa);
3524 bo->bo_appie += sizeof(*csa);
3525 bo->bo_csa_trailer_len += sizeof(*csa);
3526 bo->bo_quiet += sizeof(*csa);
3527 bo->bo_tim_trailer_len += sizeof(*csa);
3528 m->m_len += sizeof(*csa);
3529 m->m_pkthdr.len += sizeof(*csa);
3531 ieee80211_add_csa(bo->bo_csa, vap);
3534 vap->iv_csa_count++;
3535 /* NB: don't clear IEEE80211_BEACON_CSA */
3537 if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3538 (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3540 ieee80211_add_quiet(bo->bo_quiet, vap);
3542 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3544 * ERP element needs updating.
3546 (void) ieee80211_add_erp(bo->bo_erp, ic);
3547 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3549 #ifdef IEEE80211_SUPPORT_SUPERG
3550 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3551 ieee80211_add_athcaps(bo->bo_ath, ni);
3552 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3556 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3557 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3563 aielen += aie->ie_len;
3564 if (aielen != bo->bo_appie_len) {
3565 /* copy up/down trailer */
3566 int adjust = aielen - bo->bo_appie_len;
3567 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3568 bo->bo_tim_trailer_len);
3569 bo->bo_tim_trailer += adjust;
3570 bo->bo_appie += adjust;
3571 bo->bo_appie_len = aielen;
3577 frm = add_appie(frm, aie);
3578 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3580 IEEE80211_UNLOCK(ic);
3586 * Do Ethernet-LLC encapsulation for each payload in a fast frame
3587 * tunnel encapsulation. The frame is assumed to have an Ethernet
3588 * header at the front that must be stripped before prepending the
3589 * LLC followed by the Ethernet header passed in (with an Ethernet
3590 * type that specifies the payload size).
3593 ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3594 const struct ether_header *eh)
3599 /* XXX optimize by combining m_adj+M_PREPEND */
3600 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3601 llc = mtod(m, struct llc *);
3602 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3603 llc->llc_control = LLC_UI;
3604 llc->llc_snap.org_code[0] = 0;
3605 llc->llc_snap.org_code[1] = 0;
3606 llc->llc_snap.org_code[2] = 0;
3607 llc->llc_snap.ether_type = eh->ether_type;
3608 payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3610 M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3611 if (m == NULL) { /* XXX cannot happen */
3612 IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3613 "%s: no space for ether_header\n", __func__);
3614 vap->iv_stats.is_tx_nobuf++;
3617 ETHER_HEADER_COPY(mtod(m, void *), eh);
3618 mtod(m, struct ether_header *)->ether_type = htons(payload);
3623 * Complete an mbuf transmission.
3625 * For now, this simply processes a completed frame after the
3626 * driver has completed it's transmission and/or retransmission.
3627 * It assumes the frame is an 802.11 encapsulated frame.
3629 * Later on it will grow to become the exit path for a given frame
3630 * from the driver and, depending upon how it's been encapsulated
3631 * and already transmitted, it may end up doing A-MPDU retransmission,
3632 * power save requeuing, etc.
3634 * In order for the above to work, the driver entry point to this
3635 * must not hold any driver locks. Thus, the driver needs to delay
3636 * any actual mbuf completion until it can release said locks.
3638 * This frees the mbuf and if the mbuf has a node reference,
3639 * the node reference will be freed.
3642 ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3646 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3649 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3650 #if defined(__DragonFly__)
3652 * On DragonFly, IFCOUNTER_OBYTES and
3653 * IFCOUNTER_OMCASTS increments are currently done
3654 * by ifq_dispatch() already.
3657 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
3658 if (m->m_flags & M_MCAST)
3659 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3662 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3663 if (m->m_flags & M_TXCB)
3664 ieee80211_process_callback(ni, m, status);
3665 ieee80211_free_node(ni);