2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/endian.h>
40 #include <sys/socket.h>
43 #include <net/ethernet.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/if_vlan_var.h>
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <net80211/ieee80211_superg.h>
54 #ifdef IEEE80211_SUPPORT_TDMA
55 #include <net80211/ieee80211_tdma.h>
57 #include <net80211/ieee80211_wds.h>
58 #include <net80211/ieee80211_mesh.h>
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
73 #include <security/mac/mac_framework.h>
75 #define ETHER_HEADER_COPY(dst, src) \
76 memcpy(dst, src, sizeof(struct ether_header))
78 /* unalligned little endian access */
79 #define LE_WRITE_2(p, v) do { \
80 ((uint8_t *)(p))[0] = (v) & 0xff; \
81 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
83 #define LE_WRITE_4(p, v) do { \
84 ((uint8_t *)(p))[0] = (v) & 0xff; \
85 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
86 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
87 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
90 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
91 u_int hdrsize, u_int ciphdrsize, u_int mtu);
92 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
94 #ifdef IEEE80211_DEBUG
96 * Decide if an outbound management frame should be
97 * printed when debugging is enabled. This filters some
98 * of the less interesting frames that come frequently
102 doprint(struct ieee80211vap *vap, int subtype)
105 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
106 return (vap->iv_opmode == IEEE80211_M_IBSS);
113 * Start method for vap's. All packets from the stack come
114 * through here. We handle common processing of the packets
115 * before dispatching them to the underlying device.
118 ieee80211_start(struct ifnet *ifp)
120 #define IS_DWDS(vap) \
121 (vap->iv_opmode == IEEE80211_M_WDS && \
122 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
123 struct ieee80211vap *vap = ifp->if_softc;
124 struct ieee80211com *ic = vap->iv_ic;
125 struct ifnet *parent = ic->ic_ifp;
126 struct ieee80211_node *ni;
128 struct ether_header *eh;
131 /* NB: parent must be up and running */
132 if (!IFNET_IS_UP_RUNNING(parent)) {
133 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
134 "%s: ignore queue, parent %s not up+running\n",
135 __func__, parent->if_xname);
139 if (vap->iv_state == IEEE80211_S_SLEEP) {
141 * In power save, wakeup device for transmit.
143 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
147 * No data frames go out unless we're running.
148 * Note in particular this covers CAC and CSA
149 * states (though maybe we should check muting
152 if (vap->iv_state != IEEE80211_S_RUN) {
154 /* re-check under the com lock to avoid races */
155 if (vap->iv_state != IEEE80211_S_RUN) {
156 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
157 "%s: ignore queue, in %s state\n",
158 __func__, ieee80211_state_name[vap->iv_state]);
159 vap->iv_stats.is_tx_badstate++;
160 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
161 IEEE80211_UNLOCK(ic);
164 IEEE80211_UNLOCK(ic);
167 IFQ_DEQUEUE(&ifp->if_snd, m);
171 * Sanitize mbuf flags for net80211 use. We cannot
172 * clear M_PWR_SAV or M_MORE_DATA because these may
173 * be set for frames that are re-submitted from the
176 * NB: This must be done before ieee80211_classify as
177 * it marks EAPOL in frames with M_EAPOL.
179 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
181 * Cancel any background scan.
183 if (ic->ic_flags & IEEE80211_F_SCAN)
184 ieee80211_cancel_anyscan(vap);
186 * Find the node for the destination so we can do
187 * things like power save and fast frames aggregation.
189 * NB: past this point various code assumes the first
190 * mbuf has the 802.3 header present (and contiguous).
193 if (m->m_len < sizeof(struct ether_header) &&
194 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
195 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
196 "discard frame, %s\n", "m_pullup failed");
197 vap->iv_stats.is_tx_nobuf++; /* XXX */
201 eh = mtod(m, struct ether_header *);
202 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
205 * Only unicast frames from the above go out
206 * DWDS vaps; multicast frames are handled by
207 * dispatching the frame as it comes through
208 * the AP vap (see below).
210 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
211 eh->ether_dhost, "mcast", "%s", "on DWDS");
212 vap->iv_stats.is_dwds_mcast++;
216 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
218 * Spam DWDS vap's w/ multicast traffic.
220 /* XXX only if dwds in use? */
221 ieee80211_dwds_mcast(vap, m);
224 #ifdef IEEE80211_SUPPORT_MESH
225 if (vap->iv_opmode != IEEE80211_M_MBSS) {
227 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
229 /* NB: ieee80211_find_txnode does stat+msg */
234 if (ni->ni_associd == 0 &&
235 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
236 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
237 eh->ether_dhost, NULL,
238 "sta not associated (type 0x%04x)",
239 htons(eh->ether_type));
240 vap->iv_stats.is_tx_notassoc++;
243 ieee80211_free_node(ni);
246 #ifdef IEEE80211_SUPPORT_MESH
248 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
250 * Proxy station only if configured.
252 if (!ieee80211_mesh_isproxyena(vap)) {
253 IEEE80211_DISCARD_MAC(vap,
254 IEEE80211_MSG_OUTPUT |
256 eh->ether_dhost, NULL,
257 "%s", "proxy not enabled");
258 vap->iv_stats.is_mesh_notproxy++;
263 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
265 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
268 * NB: ieee80211_mesh_discover holds/disposes
269 * frame (e.g. queueing on path discovery).
276 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
277 (m->m_flags & M_PWR_SAV) == 0) {
279 * Station in power save mode; pass the frame
280 * to the 802.11 layer and continue. We'll get
281 * the frame back when the time is right.
282 * XXX lose WDS vap linkage?
284 (void) ieee80211_pwrsave(ni, m);
285 ieee80211_free_node(ni);
288 /* calculate priority so drivers can find the tx queue */
289 if (ieee80211_classify(ni, m)) {
290 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
291 eh->ether_dhost, NULL,
292 "%s", "classification failure");
293 vap->iv_stats.is_tx_classify++;
296 ieee80211_free_node(ni);
300 * Stash the node pointer. Note that we do this after
301 * any call to ieee80211_dwds_mcast because that code
302 * uses any existing value for rcvif to identify the
303 * interface it (might have been) received on.
305 m->m_pkthdr.rcvif = (void *)ni;
307 BPF_MTAP(ifp, m); /* 802.3 tx */
310 * Check if A-MPDU tx aggregation is setup or if we
311 * should try to enable it. The sta must be associated
312 * with HT and A-MPDU enabled for use. When the policy
313 * routine decides we should enable A-MPDU we issue an
314 * ADDBA request and wait for a reply. The frame being
315 * encapsulated will go out w/o using A-MPDU, or possibly
316 * it might be collected by the driver and held/retransmit.
317 * The default ic_ampdu_enable routine handles staggering
318 * ADDBA requests in case the receiver NAK's us or we are
319 * otherwise unable to establish a BA stream.
321 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
322 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
323 (m->m_flags & M_EAPOL) == 0) {
324 const int ac = M_WME_GETAC(m);
325 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
327 ieee80211_txampdu_count_packet(tap);
328 if (IEEE80211_AMPDU_RUNNING(tap)) {
330 * Operational, mark frame for aggregation.
332 * XXX do tx aggregation here
334 m->m_flags |= M_AMPDU_MPDU;
335 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
336 ic->ic_ampdu_enable(ni, tap)) {
338 * Not negotiated yet, request service.
340 ieee80211_ampdu_request(ni, tap);
341 /* XXX hold frame for reply? */
344 #ifdef IEEE80211_SUPPORT_SUPERG
345 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
346 m = ieee80211_ff_check(ni, m);
348 /* NB: any ni ref held on stageq */
352 #endif /* IEEE80211_SUPPORT_SUPERG */
353 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
355 * Encapsulate the packet in prep for transmission.
357 m = ieee80211_encap(vap, ni, m);
359 /* NB: stat+msg handled in ieee80211_encap */
360 ieee80211_free_node(ni);
365 error = parent->if_transmit(parent, m);
367 /* NB: IFQ_HANDOFF reclaims mbuf */
368 ieee80211_free_node(ni);
372 ic->ic_lastdata = ticks;
378 * 802.11 output routine. This is (currently) used only to
379 * connect bpf write calls to the 802.11 layer for injecting
383 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
384 struct sockaddr *dst, struct route *ro)
386 #define senderr(e) do { error = (e); goto bad;} while (0)
387 struct ieee80211_node *ni = NULL;
388 struct ieee80211vap *vap;
389 struct ieee80211_frame *wh;
392 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
394 * Short-circuit requests if the vap is marked OACTIVE
395 * as this can happen because a packet came down through
396 * ieee80211_start before the vap entered RUN state in
397 * which case it's ok to just drop the frame. This
398 * should not be necessary but callers of if_output don't
405 * Hand to the 802.3 code if not tagged as
406 * a raw 802.11 frame.
408 if (dst->sa_family != AF_IEEE80211)
409 return vap->iv_output(ifp, m, dst, ro);
411 error = mac_ifnet_check_transmit(ifp, m);
415 if (ifp->if_flags & IFF_MONITOR)
417 if (!IFNET_IS_UP_RUNNING(ifp))
419 if (vap->iv_state == IEEE80211_S_CAC) {
420 IEEE80211_DPRINTF(vap,
421 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
422 "block %s frame in CAC state\n", "raw data");
423 vap->iv_stats.is_tx_badstate++;
424 senderr(EIO); /* XXX */
425 } else if (vap->iv_state == IEEE80211_S_SCAN)
427 /* XXX bypass bridge, pfil, carp, etc. */
429 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
430 senderr(EIO); /* XXX */
431 wh = mtod(m, struct ieee80211_frame *);
432 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
433 IEEE80211_FC0_VERSION_0)
434 senderr(EIO); /* XXX */
436 /* locate destination node */
437 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
438 case IEEE80211_FC1_DIR_NODS:
439 case IEEE80211_FC1_DIR_FROMDS:
440 ni = ieee80211_find_txnode(vap, wh->i_addr1);
442 case IEEE80211_FC1_DIR_TODS:
443 case IEEE80211_FC1_DIR_DSTODS:
444 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
445 senderr(EIO); /* XXX */
446 ni = ieee80211_find_txnode(vap, wh->i_addr3);
449 senderr(EIO); /* XXX */
453 * Permit packets w/ bpf params through regardless
454 * (see below about sa_len).
456 if (dst->sa_len == 0)
457 senderr(EHOSTUNREACH);
458 ni = ieee80211_ref_node(vap->iv_bss);
462 * Sanitize mbuf for net80211 flags leaked from above.
464 * NB: This must be done before ieee80211_classify as
465 * it marks EAPOL in frames with M_EAPOL.
467 m->m_flags &= ~M_80211_TX;
469 /* calculate priority so drivers can find the tx queue */
470 /* XXX assumes an 802.3 frame */
471 if (ieee80211_classify(ni, m))
472 senderr(EIO); /* XXX */
475 IEEE80211_NODE_STAT(ni, tx_data);
476 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
477 IEEE80211_NODE_STAT(ni, tx_mcast);
478 m->m_flags |= M_MCAST;
480 IEEE80211_NODE_STAT(ni, tx_ucast);
481 /* NB: ieee80211_encap does not include 802.11 header */
482 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
485 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
486 * present by setting the sa_len field of the sockaddr (yes,
488 * NB: we assume sa_data is suitably aligned to cast.
490 return vap->iv_ic->ic_raw_xmit(ni, m,
491 (const struct ieee80211_bpf_params *)(dst->sa_len ?
492 dst->sa_data : NULL));
497 ieee80211_free_node(ni);
504 * Set the direction field and address fields of an outgoing
505 * frame. Note this should be called early on in constructing
506 * a frame as it sets i_fc[1]; other bits can then be or'd in.
509 ieee80211_send_setup(
510 struct ieee80211_node *ni,
513 const uint8_t sa[IEEE80211_ADDR_LEN],
514 const uint8_t da[IEEE80211_ADDR_LEN],
515 const uint8_t bssid[IEEE80211_ADDR_LEN])
517 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
518 struct ieee80211vap *vap = ni->ni_vap;
519 struct ieee80211_tx_ampdu *tap;
520 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
523 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
524 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
525 switch (vap->iv_opmode) {
526 case IEEE80211_M_STA:
527 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
528 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
529 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
530 IEEE80211_ADDR_COPY(wh->i_addr3, da);
532 case IEEE80211_M_IBSS:
533 case IEEE80211_M_AHDEMO:
534 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
535 IEEE80211_ADDR_COPY(wh->i_addr1, da);
536 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
537 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
539 case IEEE80211_M_HOSTAP:
540 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
541 IEEE80211_ADDR_COPY(wh->i_addr1, da);
542 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
543 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
545 case IEEE80211_M_WDS:
546 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
547 IEEE80211_ADDR_COPY(wh->i_addr1, da);
548 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
549 IEEE80211_ADDR_COPY(wh->i_addr3, da);
550 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
552 case IEEE80211_M_MBSS:
553 #ifdef IEEE80211_SUPPORT_MESH
554 /* XXX add support for proxied addresses */
555 if (IEEE80211_IS_MULTICAST(da)) {
556 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
558 IEEE80211_ADDR_COPY(wh->i_addr1, da);
559 IEEE80211_ADDR_COPY(wh->i_addr2,
562 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
563 IEEE80211_ADDR_COPY(wh->i_addr1, da);
564 IEEE80211_ADDR_COPY(wh->i_addr2,
566 IEEE80211_ADDR_COPY(wh->i_addr3, da);
567 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
571 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
575 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
576 IEEE80211_ADDR_COPY(wh->i_addr1, da);
577 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
578 #ifdef IEEE80211_SUPPORT_MESH
579 if (vap->iv_opmode == IEEE80211_M_MBSS)
580 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
583 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
585 *(uint16_t *)&wh->i_dur[0] = 0;
587 tap = &ni->ni_tx_ampdu[TID_TO_WME_AC(tid)];
588 if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
589 m->m_flags |= M_AMPDU_MPDU;
591 seqno = ni->ni_txseqs[tid]++;
592 *(uint16_t *)&wh->i_seq[0] =
593 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
594 M_SEQNO_SET(m, seqno);
597 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
598 m->m_flags |= M_MCAST;
603 * Send a management frame to the specified node. The node pointer
604 * must have a reference as the pointer will be passed to the driver
605 * and potentially held for a long time. If the frame is successfully
606 * dispatched to the driver, then it is responsible for freeing the
607 * reference (and potentially free'ing up any associated storage);
608 * otherwise deal with reclaiming any reference (on error).
611 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
612 struct ieee80211_bpf_params *params)
614 struct ieee80211vap *vap = ni->ni_vap;
615 struct ieee80211com *ic = ni->ni_ic;
616 struct ieee80211_frame *wh;
618 KASSERT(ni != NULL, ("null node"));
620 if (vap->iv_state == IEEE80211_S_CAC) {
621 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
622 ni, "block %s frame in CAC state",
623 ieee80211_mgt_subtype_name[
624 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
625 IEEE80211_FC0_SUBTYPE_SHIFT]);
626 vap->iv_stats.is_tx_badstate++;
627 ieee80211_free_node(ni);
629 return EIO; /* XXX */
632 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
634 ieee80211_free_node(ni);
638 wh = mtod(m, struct ieee80211_frame *);
639 ieee80211_send_setup(ni, m,
640 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
641 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
642 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
643 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
644 "encrypting frame (%s)", __func__);
645 wh->i_fc[1] |= IEEE80211_FC1_WEP;
647 m->m_flags |= M_ENCAP; /* mark encapsulated */
649 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
650 M_WME_SETAC(m, params->ibp_pri);
652 #ifdef IEEE80211_DEBUG
653 /* avoid printing too many frames */
654 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
655 ieee80211_msg_dumppkts(vap)) {
656 printf("[%s] send %s on channel %u\n",
657 ether_sprintf(wh->i_addr1),
658 ieee80211_mgt_subtype_name[
659 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
660 IEEE80211_FC0_SUBTYPE_SHIFT],
661 ieee80211_chan2ieee(ic, ic->ic_curchan));
664 IEEE80211_NODE_STAT(ni, tx_mgmt);
666 return ic->ic_raw_xmit(ni, m, params);
670 * Send a null data frame to the specified node. If the station
671 * is setup for QoS then a QoS Null Data frame is constructed.
672 * If this is a WDS station then a 4-address frame is constructed.
674 * NB: the caller is assumed to have setup a node reference
675 * for use; this is necessary to deal with a race condition
676 * when probing for inactive stations. Like ieee80211_mgmt_output
677 * we must cleanup any node reference on error; however we
678 * can safely just unref it as we know it will never be the
679 * last reference to the node.
682 ieee80211_send_nulldata(struct ieee80211_node *ni)
684 struct ieee80211vap *vap = ni->ni_vap;
685 struct ieee80211com *ic = ni->ni_ic;
687 struct ieee80211_frame *wh;
691 if (vap->iv_state == IEEE80211_S_CAC) {
692 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
693 ni, "block %s frame in CAC state", "null data");
694 ieee80211_unref_node(&ni);
695 vap->iv_stats.is_tx_badstate++;
696 return EIO; /* XXX */
699 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
700 hdrlen = sizeof(struct ieee80211_qosframe);
702 hdrlen = sizeof(struct ieee80211_frame);
703 /* NB: only WDS vap's get 4-address frames */
704 if (vap->iv_opmode == IEEE80211_M_WDS)
705 hdrlen += IEEE80211_ADDR_LEN;
706 if (ic->ic_flags & IEEE80211_F_DATAPAD)
707 hdrlen = roundup(hdrlen, sizeof(uint32_t));
709 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
712 ieee80211_unref_node(&ni);
713 vap->iv_stats.is_tx_nobuf++;
716 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
717 ("leading space %zd", M_LEADINGSPACE(m)));
718 M_PREPEND(m, hdrlen, M_DONTWAIT);
720 /* NB: cannot happen */
721 ieee80211_free_node(ni);
725 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
726 if (ni->ni_flags & IEEE80211_NODE_QOS) {
727 const int tid = WME_AC_TO_TID(WME_AC_BE);
730 ieee80211_send_setup(ni, m,
731 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
732 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
734 if (vap->iv_opmode == IEEE80211_M_WDS)
735 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
737 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
738 qos[0] = tid & IEEE80211_QOS_TID;
739 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
740 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
743 ieee80211_send_setup(ni, m,
744 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
745 IEEE80211_NONQOS_TID,
746 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
748 if (vap->iv_opmode != IEEE80211_M_WDS) {
749 /* NB: power management bit is never sent by an AP */
750 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
751 vap->iv_opmode != IEEE80211_M_HOSTAP)
752 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
754 m->m_len = m->m_pkthdr.len = hdrlen;
755 m->m_flags |= M_ENCAP; /* mark encapsulated */
757 M_WME_SETAC(m, WME_AC_BE);
759 IEEE80211_NODE_STAT(ni, tx_data);
761 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
762 "send %snull data frame on channel %u, pwr mgt %s",
763 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
764 ieee80211_chan2ieee(ic, ic->ic_curchan),
765 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
767 return ic->ic_raw_xmit(ni, m, NULL);
771 * Assign priority to a frame based on any vlan tag assigned
772 * to the station and/or any Diffserv setting in an IP header.
773 * Finally, if an ACM policy is setup (in station mode) it's
777 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
779 const struct ether_header *eh = mtod(m, struct ether_header *);
780 int v_wme_ac, d_wme_ac, ac;
783 * Always promote PAE/EAPOL frames to high priority.
785 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
786 /* NB: mark so others don't need to check header */
787 m->m_flags |= M_EAPOL;
792 * Non-qos traffic goes to BE.
794 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
800 * If node has a vlan tag then all traffic
801 * to it must have a matching tag.
804 if (ni->ni_vlan != 0) {
805 if ((m->m_flags & M_VLANTAG) == 0) {
806 IEEE80211_NODE_STAT(ni, tx_novlantag);
809 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
810 EVL_VLANOFTAG(ni->ni_vlan)) {
811 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
814 /* map vlan priority to AC */
815 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
818 /* XXX m_copydata may be too slow for fast path */
820 if (eh->ether_type == htons(ETHERTYPE_IP)) {
823 * IP frame, map the DSCP bits from the TOS field.
825 /* NB: ip header may not be in first mbuf */
826 m_copydata(m, sizeof(struct ether_header) +
827 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
828 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
829 d_wme_ac = TID_TO_WME_AC(tos);
833 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
837 * IPv6 frame, map the DSCP bits from the traffic class field.
839 m_copydata(m, sizeof(struct ether_header) +
840 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
842 tos = (uint8_t)(ntohl(flow) >> 20);
843 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
844 d_wme_ac = TID_TO_WME_AC(tos);
847 d_wme_ac = WME_AC_BE;
855 * Use highest priority AC.
857 if (v_wme_ac > d_wme_ac)
865 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
866 static const int acmap[4] = {
867 WME_AC_BK, /* WME_AC_BE */
868 WME_AC_BK, /* WME_AC_BK */
869 WME_AC_BE, /* WME_AC_VI */
870 WME_AC_VI, /* WME_AC_VO */
872 struct ieee80211com *ic = ni->ni_ic;
874 while (ac != WME_AC_BK &&
875 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
884 * Insure there is sufficient contiguous space to encapsulate the
885 * 802.11 data frame. If room isn't already there, arrange for it.
886 * Drivers and cipher modules assume we have done the necessary work
887 * and fail rudely if they don't find the space they need.
890 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
891 struct ieee80211_key *key, struct mbuf *m)
893 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
894 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
897 /* XXX belongs in crypto code? */
898 needed_space += key->wk_cipher->ic_header;
901 * When crypto is being done in the host we must insure
902 * the data are writable for the cipher routines; clone
903 * a writable mbuf chain.
904 * XXX handle SWMIC specially
906 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
907 m = m_unshare(m, M_NOWAIT);
909 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
910 "%s: cannot get writable mbuf\n", __func__);
911 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
917 * We know we are called just before stripping an Ethernet
918 * header and prepending an LLC header. This means we know
920 * sizeof(struct ether_header) - sizeof(struct llc)
921 * bytes recovered to which we need additional space for the
922 * 802.11 header and any crypto header.
924 /* XXX check trailing space and copy instead? */
925 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
926 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
928 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
929 "%s: cannot expand storage\n", __func__);
930 vap->iv_stats.is_tx_nobuf++;
934 KASSERT(needed_space <= MHLEN,
935 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
937 * Setup new mbuf to have leading space to prepend the
938 * 802.11 header and any crypto header bits that are
939 * required (the latter are added when the driver calls
940 * back to ieee80211_crypto_encap to do crypto encapsulation).
942 /* NB: must be first 'cuz it clobbers m_data */
944 n->m_len = 0; /* NB: m_gethdr does not set */
945 n->m_data += needed_space;
947 * Pull up Ethernet header to create the expected layout.
948 * We could use m_pullup but that's overkill (i.e. we don't
949 * need the actual data) and it cannot fail so do it inline
952 /* NB: struct ether_header is known to be contiguous */
953 n->m_len += sizeof(struct ether_header);
954 m->m_len -= sizeof(struct ether_header);
955 m->m_data += sizeof(struct ether_header);
957 * Replace the head of the chain.
963 #undef TO_BE_RECLAIMED
967 * Return the transmit key to use in sending a unicast frame.
968 * If a unicast key is set we use that. When no unicast key is set
969 * we fall back to the default transmit key.
971 static __inline struct ieee80211_key *
972 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
973 struct ieee80211_node *ni)
975 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
976 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
977 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
979 return &vap->iv_nw_keys[vap->iv_def_txkey];
981 return &ni->ni_ucastkey;
986 * Return the transmit key to use in sending a multicast frame.
987 * Multicast traffic always uses the group key which is installed as
988 * the default tx key.
990 static __inline struct ieee80211_key *
991 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
992 struct ieee80211_node *ni)
994 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
995 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
997 return &vap->iv_nw_keys[vap->iv_def_txkey];
1001 * Encapsulate an outbound data frame. The mbuf chain is updated.
1002 * If an error is encountered NULL is returned. The caller is required
1003 * to provide a node reference and pullup the ethernet header in the
1006 * NB: Packet is assumed to be processed by ieee80211_classify which
1007 * marked EAPOL frames w/ M_EAPOL.
1010 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1013 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1014 struct ieee80211com *ic = ni->ni_ic;
1015 #ifdef IEEE80211_SUPPORT_MESH
1016 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1017 struct ieee80211_meshcntl_ae10 *mc;
1019 struct ether_header eh;
1020 struct ieee80211_frame *wh;
1021 struct ieee80211_key *key;
1023 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1024 ieee80211_seq seqno;
1025 int meshhdrsize, meshae;
1029 * Copy existing Ethernet header to a safe place. The
1030 * rest of the code assumes it's ok to strip it when
1031 * reorganizing state for the final encapsulation.
1033 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1034 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1037 * Insure space for additional headers. First identify
1038 * transmit key to use in calculating any buffer adjustments
1039 * required. This is also used below to do privacy
1040 * encapsulation work. Then calculate the 802.11 header
1041 * size and any padding required by the driver.
1043 * Note key may be NULL if we fall back to the default
1044 * transmit key and that is not set. In that case the
1045 * buffer may not be expanded as needed by the cipher
1046 * routines, but they will/should discard it.
1048 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1049 if (vap->iv_opmode == IEEE80211_M_STA ||
1050 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1051 (vap->iv_opmode == IEEE80211_M_WDS &&
1052 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1053 key = ieee80211_crypto_getucastkey(vap, ni);
1055 key = ieee80211_crypto_getmcastkey(vap, ni);
1056 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1057 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1059 "no default transmit key (%s) deftxkey %u",
1060 __func__, vap->iv_def_txkey);
1061 vap->iv_stats.is_tx_nodefkey++;
1067 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1068 * frames so suppress use. This may be an issue if other
1069 * ap's require all data frames to be QoS-encapsulated
1070 * once negotiated in which case we'll need to make this
1073 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1074 (m->m_flags & M_EAPOL) == 0;
1076 hdrsize = sizeof(struct ieee80211_qosframe);
1078 hdrsize = sizeof(struct ieee80211_frame);
1079 #ifdef IEEE80211_SUPPORT_MESH
1080 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1082 * Mesh data frames are encapsulated according to the
1083 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1084 * o Group Addressed data (aka multicast) originating
1085 * at the local sta are sent w/ 3-address format and
1086 * address extension mode 00
1087 * o Individually Addressed data (aka unicast) originating
1088 * at the local sta are sent w/ 4-address format and
1089 * address extension mode 00
1090 * o Group Addressed data forwarded from a non-mesh sta are
1091 * sent w/ 3-address format and address extension mode 01
1092 * o Individually Address data from another sta are sent
1093 * w/ 4-address format and address extension mode 10
1095 is4addr = 0; /* NB: don't use, disable */
1096 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1097 hdrsize += IEEE80211_ADDR_LEN; /* unicast are 4-addr */
1098 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1099 /* XXX defines for AE modes */
1100 if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1101 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1104 meshae = 4; /* NB: pseudo */
1105 } else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1107 meshhdrsize += 1*IEEE80211_ADDR_LEN;
1110 meshhdrsize += 2*IEEE80211_ADDR_LEN;
1115 * 4-address frames need to be generated for:
1116 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1117 * o packets sent through a vap marked for relaying
1118 * (e.g. a station operating with dynamic WDS)
1120 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1121 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1122 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1124 hdrsize += IEEE80211_ADDR_LEN;
1125 meshhdrsize = meshae = 0;
1126 #ifdef IEEE80211_SUPPORT_MESH
1130 * Honor driver DATAPAD requirement.
1132 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1133 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1137 if (__predict_true((m->m_flags & M_FF) == 0)) {
1141 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1143 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1146 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1147 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1148 llc = mtod(m, struct llc *);
1149 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1150 llc->llc_control = LLC_UI;
1151 llc->llc_snap.org_code[0] = 0;
1152 llc->llc_snap.org_code[1] = 0;
1153 llc->llc_snap.org_code[2] = 0;
1154 llc->llc_snap.ether_type = eh.ether_type;
1156 #ifdef IEEE80211_SUPPORT_SUPERG
1160 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1165 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1167 M_PREPEND(m, hdrspace + meshhdrsize, M_DONTWAIT);
1169 vap->iv_stats.is_tx_nobuf++;
1172 wh = mtod(m, struct ieee80211_frame *);
1173 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1174 *(uint16_t *)wh->i_dur = 0;
1175 qos = NULL; /* NB: quiet compiler */
1177 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1178 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1179 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1180 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1181 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1182 } else switch (vap->iv_opmode) {
1183 case IEEE80211_M_STA:
1184 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1185 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1186 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1187 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1189 case IEEE80211_M_IBSS:
1190 case IEEE80211_M_AHDEMO:
1191 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1192 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1193 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1195 * NB: always use the bssid from iv_bss as the
1196 * neighbor's may be stale after an ibss merge
1198 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1200 case IEEE80211_M_HOSTAP:
1201 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1202 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1203 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1204 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1206 #ifdef IEEE80211_SUPPORT_MESH
1207 case IEEE80211_M_MBSS:
1208 /* NB: offset by hdrspace to deal with DATAPAD */
1209 mc = (struct ieee80211_meshcntl_ae10 *)
1210 (mtod(m, uint8_t *) + hdrspace);
1212 case 0: /* ucast, no proxy */
1213 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1214 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1215 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1216 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1217 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1219 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1221 case 4: /* mcast, no proxy */
1222 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1223 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1224 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1225 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1226 mc->mc_flags = 0; /* NB: AE is really 0 */
1227 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1229 case 1: /* mcast, proxy */
1230 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1231 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1232 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1233 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1235 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1236 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1238 case 2: /* ucast, proxy */
1239 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1240 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1241 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1242 /* XXX not right, need MeshDA */
1243 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1244 /* XXX assume are MeshSA */
1245 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1247 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1248 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1249 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1252 KASSERT(0, ("meshae %d", meshae));
1255 mc->mc_ttl = ms->ms_ttl;
1257 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1260 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1264 if (m->m_flags & M_MORE_DATA)
1265 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1270 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1271 /* NB: mesh case handled earlier */
1272 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1273 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1274 ac = M_WME_GETAC(m);
1275 /* map from access class/queue to 11e header priorty value */
1276 tid = WME_AC_TO_TID(ac);
1277 qos[0] = tid & IEEE80211_QOS_TID;
1278 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1279 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1281 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1283 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1285 * NB: don't assign a sequence # to potential
1286 * aggregates; we expect this happens at the
1287 * point the frame comes off any aggregation q
1288 * as otherwise we may introduce holes in the
1289 * BA sequence space and/or make window accouting
1292 * XXX may want to control this with a driver
1293 * capability; this may also change when we pull
1294 * aggregation up into net80211
1296 seqno = ni->ni_txseqs[tid]++;
1297 *(uint16_t *)wh->i_seq =
1298 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1299 M_SEQNO_SET(m, seqno);
1302 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1303 *(uint16_t *)wh->i_seq =
1304 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1305 M_SEQNO_SET(m, seqno);
1309 /* check if xmit fragmentation is required */
1310 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1311 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1312 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1313 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1316 * IEEE 802.1X: send EAPOL frames always in the clear.
1317 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1319 if ((m->m_flags & M_EAPOL) == 0 ||
1320 ((vap->iv_flags & IEEE80211_F_WPA) &&
1321 (vap->iv_opmode == IEEE80211_M_STA ?
1322 !IEEE80211_KEY_UNDEFINED(key) :
1323 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1324 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1325 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1326 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1328 "%s", "enmic failed, discard frame");
1329 vap->iv_stats.is_crypto_enmicfail++;
1334 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1335 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1338 m->m_flags |= M_ENCAP; /* mark encapsulated */
1340 IEEE80211_NODE_STAT(ni, tx_data);
1341 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1342 IEEE80211_NODE_STAT(ni, tx_mcast);
1343 m->m_flags |= M_MCAST;
1345 IEEE80211_NODE_STAT(ni, tx_ucast);
1346 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1357 * Fragment the frame according to the specified mtu.
1358 * The size of the 802.11 header (w/o padding) is provided
1359 * so we don't need to recalculate it. We create a new
1360 * mbuf for each fragment and chain it through m_nextpkt;
1361 * we might be able to optimize this by reusing the original
1362 * packet's mbufs but that is significantly more complicated.
1365 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1366 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1368 struct ieee80211_frame *wh, *whf;
1369 struct mbuf *m, *prev, *next;
1370 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1372 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1373 KASSERT(m0->m_pkthdr.len > mtu,
1374 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1376 wh = mtod(m0, struct ieee80211_frame *);
1377 /* NB: mark the first frag; it will be propagated below */
1378 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1379 totalhdrsize = hdrsize + ciphdrsize;
1381 off = mtu - ciphdrsize;
1382 remainder = m0->m_pkthdr.len - off;
1385 fragsize = totalhdrsize + remainder;
1388 /* XXX fragsize can be >2048! */
1389 KASSERT(fragsize < MCLBYTES,
1390 ("fragment size %u too big!", fragsize));
1391 if (fragsize > MHLEN)
1392 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1394 m = m_gethdr(M_DONTWAIT, MT_DATA);
1397 /* leave room to prepend any cipher header */
1398 m_align(m, fragsize - ciphdrsize);
1401 * Form the header in the fragment. Note that since
1402 * we mark the first fragment with the MORE_FRAG bit
1403 * it automatically is propagated to each fragment; we
1404 * need only clear it on the last fragment (done below).
1406 whf = mtod(m, struct ieee80211_frame *);
1407 memcpy(whf, wh, hdrsize);
1408 *(uint16_t *)&whf->i_seq[0] |= htole16(
1409 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1410 IEEE80211_SEQ_FRAG_SHIFT);
1413 payload = fragsize - totalhdrsize;
1414 /* NB: destination is known to be contiguous */
1415 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1416 m->m_len = hdrsize + payload;
1417 m->m_pkthdr.len = hdrsize + payload;
1418 m->m_flags |= M_FRAG;
1420 /* chain up the fragment */
1421 prev->m_nextpkt = m;
1424 /* deduct fragment just formed */
1425 remainder -= payload;
1427 } while (remainder != 0);
1429 /* set the last fragment */
1430 m->m_flags |= M_LASTFRAG;
1431 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1433 /* strip first mbuf now that everything has been copied */
1434 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1435 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1437 vap->iv_stats.is_tx_fragframes++;
1438 vap->iv_stats.is_tx_frags += fragno-1;
1442 /* reclaim fragments but leave original frame for caller to free */
1443 for (m = m0->m_nextpkt; m != NULL; m = next) {
1444 next = m->m_nextpkt;
1445 m->m_nextpkt = NULL; /* XXX paranoid */
1448 m0->m_nextpkt = NULL;
1453 * Add a supported rates element id to a frame.
1456 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1460 *frm++ = IEEE80211_ELEMID_RATES;
1461 nrates = rs->rs_nrates;
1462 if (nrates > IEEE80211_RATE_SIZE)
1463 nrates = IEEE80211_RATE_SIZE;
1465 memcpy(frm, rs->rs_rates, nrates);
1466 return frm + nrates;
1470 * Add an extended supported rates element id to a frame.
1473 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1476 * Add an extended supported rates element if operating in 11g mode.
1478 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1479 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1480 *frm++ = IEEE80211_ELEMID_XRATES;
1482 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1489 * Add an ssid element to a frame.
1492 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1494 *frm++ = IEEE80211_ELEMID_SSID;
1496 memcpy(frm, ssid, len);
1501 * Add an erp element to a frame.
1504 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1508 *frm++ = IEEE80211_ELEMID_ERP;
1511 if (ic->ic_nonerpsta != 0)
1512 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1513 if (ic->ic_flags & IEEE80211_F_USEPROT)
1514 erp |= IEEE80211_ERP_USE_PROTECTION;
1515 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1516 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1522 * Add a CFParams element to a frame.
1525 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1527 #define ADDSHORT(frm, v) do { \
1528 LE_WRITE_2(frm, v); \
1531 *frm++ = IEEE80211_ELEMID_CFPARMS;
1533 *frm++ = 0; /* CFP count */
1534 *frm++ = 2; /* CFP period */
1535 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1536 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1541 static __inline uint8_t *
1542 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1544 memcpy(frm, ie->ie_data, ie->ie_len);
1545 return frm + ie->ie_len;
1548 static __inline uint8_t *
1549 add_ie(uint8_t *frm, const uint8_t *ie)
1551 memcpy(frm, ie, 2 + ie[1]);
1552 return frm + 2 + ie[1];
1555 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1557 * Add a WME information element to a frame.
1560 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1562 static const struct ieee80211_wme_info info = {
1563 .wme_id = IEEE80211_ELEMID_VENDOR,
1564 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1565 .wme_oui = { WME_OUI_BYTES },
1566 .wme_type = WME_OUI_TYPE,
1567 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1568 .wme_version = WME_VERSION,
1571 memcpy(frm, &info, sizeof(info));
1572 return frm + sizeof(info);
1576 * Add a WME parameters element to a frame.
1579 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1581 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1582 #define ADDSHORT(frm, v) do { \
1583 LE_WRITE_2(frm, v); \
1586 /* NB: this works 'cuz a param has an info at the front */
1587 static const struct ieee80211_wme_info param = {
1588 .wme_id = IEEE80211_ELEMID_VENDOR,
1589 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1590 .wme_oui = { WME_OUI_BYTES },
1591 .wme_type = WME_OUI_TYPE,
1592 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1593 .wme_version = WME_VERSION,
1597 memcpy(frm, ¶m, sizeof(param));
1598 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1599 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1600 *frm++ = 0; /* reserved field */
1601 for (i = 0; i < WME_NUM_AC; i++) {
1602 const struct wmeParams *ac =
1603 &wme->wme_bssChanParams.cap_wmeParams[i];
1604 *frm++ = SM(i, WME_PARAM_ACI)
1605 | SM(ac->wmep_acm, WME_PARAM_ACM)
1606 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1608 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1609 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1611 ADDSHORT(frm, ac->wmep_txopLimit);
1617 #undef WME_OUI_BYTES
1620 * Add an 11h Power Constraint element to a frame.
1623 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1625 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1626 /* XXX per-vap tx power limit? */
1627 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1629 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1631 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1636 * Add an 11h Power Capability element to a frame.
1639 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1641 frm[0] = IEEE80211_ELEMID_PWRCAP;
1643 frm[2] = c->ic_minpower;
1644 frm[3] = c->ic_maxpower;
1649 * Add an 11h Supported Channels element to a frame.
1652 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1654 static const int ielen = 26;
1656 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1658 /* XXX not correct */
1659 memcpy(frm+2, ic->ic_chan_avail, ielen);
1660 return frm + 2 + ielen;
1664 * Add an 11h Channel Switch Announcement element to a frame.
1665 * Note that we use the per-vap CSA count to adjust the global
1666 * counter so we can use this routine to form probe response
1667 * frames and get the current count.
1670 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1672 struct ieee80211com *ic = vap->iv_ic;
1673 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1675 csa->csa_ie = IEEE80211_ELEMID_CSA;
1677 csa->csa_mode = 1; /* XXX force quiet on channel */
1678 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1679 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1680 return frm + sizeof(*csa);
1684 * Add an 11h country information element to a frame.
1687 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1690 if (ic->ic_countryie == NULL ||
1691 ic->ic_countryie_chan != ic->ic_bsschan) {
1693 * Handle lazy construction of ie. This is done on
1694 * first use and after a channel change that requires
1697 if (ic->ic_countryie != NULL)
1698 free(ic->ic_countryie, M_80211_NODE_IE);
1699 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1700 if (ic->ic_countryie == NULL)
1702 ic->ic_countryie_chan = ic->ic_bsschan;
1704 return add_appie(frm, ic->ic_countryie);
1708 ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
1710 if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
1711 return (add_ie(frm, vap->iv_wpa_ie));
1713 /* XXX else complain? */
1719 ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
1721 if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
1722 return (add_ie(frm, vap->iv_rsn_ie));
1724 /* XXX else complain? */
1730 ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
1732 if (ni->ni_flags & IEEE80211_NODE_QOS) {
1733 *frm++ = IEEE80211_ELEMID_QOS;
1742 * Send a probe request frame with the specified ssid
1743 * and any optional information element data.
1746 ieee80211_send_probereq(struct ieee80211_node *ni,
1747 const uint8_t sa[IEEE80211_ADDR_LEN],
1748 const uint8_t da[IEEE80211_ADDR_LEN],
1749 const uint8_t bssid[IEEE80211_ADDR_LEN],
1750 const uint8_t *ssid, size_t ssidlen)
1752 struct ieee80211vap *vap = ni->ni_vap;
1753 struct ieee80211com *ic = ni->ni_ic;
1754 const struct ieee80211_txparam *tp;
1755 struct ieee80211_bpf_params params;
1756 struct ieee80211_frame *wh;
1757 const struct ieee80211_rateset *rs;
1761 if (vap->iv_state == IEEE80211_S_CAC) {
1762 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1763 "block %s frame in CAC state", "probe request");
1764 vap->iv_stats.is_tx_badstate++;
1765 return EIO; /* XXX */
1769 * Hold a reference on the node so it doesn't go away until after
1770 * the xmit is complete all the way in the driver. On error we
1771 * will remove our reference.
1773 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1774 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1776 ni, ether_sprintf(ni->ni_macaddr),
1777 ieee80211_node_refcnt(ni)+1);
1778 ieee80211_ref_node(ni);
1781 * prreq frame format
1783 * [tlv] supported rates
1784 * [tlv] RSN (optional)
1785 * [tlv] extended supported rates
1786 * [tlv] WPA (optional)
1787 * [tlv] user-specified ie's
1789 m = ieee80211_getmgtframe(&frm,
1790 ic->ic_headroom + sizeof(struct ieee80211_frame),
1791 2 + IEEE80211_NWID_LEN
1792 + 2 + IEEE80211_RATE_SIZE
1793 + sizeof(struct ieee80211_ie_wpa)
1794 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1795 + sizeof(struct ieee80211_ie_wpa)
1796 + (vap->iv_appie_probereq != NULL ?
1797 vap->iv_appie_probereq->ie_len : 0)
1800 vap->iv_stats.is_tx_nobuf++;
1801 ieee80211_free_node(ni);
1805 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1806 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1807 frm = ieee80211_add_rates(frm, rs);
1808 frm = ieee80211_add_rsn(frm, vap);
1809 frm = ieee80211_add_xrates(frm, rs);
1810 frm = ieee80211_add_wpa(frm, vap);
1811 if (vap->iv_appie_probereq != NULL)
1812 frm = add_appie(frm, vap->iv_appie_probereq);
1813 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1815 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1816 ("leading space %zd", M_LEADINGSPACE(m)));
1817 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1819 /* NB: cannot happen */
1820 ieee80211_free_node(ni);
1824 wh = mtod(m, struct ieee80211_frame *);
1825 ieee80211_send_setup(ni, m,
1826 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1827 IEEE80211_NONQOS_TID, sa, da, bssid);
1828 /* XXX power management? */
1829 m->m_flags |= M_ENCAP; /* mark encapsulated */
1831 M_WME_SETAC(m, WME_AC_BE);
1833 IEEE80211_NODE_STAT(ni, tx_probereq);
1834 IEEE80211_NODE_STAT(ni, tx_mgmt);
1836 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1837 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1838 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1841 memset(¶ms, 0, sizeof(params));
1842 params.ibp_pri = M_WME_GETAC(m);
1843 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1844 params.ibp_rate0 = tp->mgmtrate;
1845 if (IEEE80211_IS_MULTICAST(da)) {
1846 params.ibp_flags |= IEEE80211_BPF_NOACK;
1847 params.ibp_try0 = 1;
1849 params.ibp_try0 = tp->maxretry;
1850 params.ibp_power = ni->ni_txpower;
1851 return ic->ic_raw_xmit(ni, m, ¶ms);
1855 * Calculate capability information for mgt frames.
1858 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1860 struct ieee80211com *ic = vap->iv_ic;
1863 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1865 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1866 capinfo = IEEE80211_CAPINFO_ESS;
1867 else if (vap->iv_opmode == IEEE80211_M_IBSS)
1868 capinfo = IEEE80211_CAPINFO_IBSS;
1871 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1872 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1873 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1874 IEEE80211_IS_CHAN_2GHZ(chan))
1875 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1876 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1877 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1878 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1879 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1884 * Send a management frame. The node is for the destination (or ic_bss
1885 * when in station mode). Nodes other than ic_bss have their reference
1886 * count bumped to reflect our use for an indeterminant time.
1889 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1891 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1892 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1893 struct ieee80211vap *vap = ni->ni_vap;
1894 struct ieee80211com *ic = ni->ni_ic;
1895 struct ieee80211_node *bss = vap->iv_bss;
1896 struct ieee80211_bpf_params params;
1900 int has_challenge, is_shared_key, ret, status;
1902 KASSERT(ni != NULL, ("null node"));
1905 * Hold a reference on the node so it doesn't go away until after
1906 * the xmit is complete all the way in the driver. On error we
1907 * will remove our reference.
1909 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1910 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1912 ni, ether_sprintf(ni->ni_macaddr),
1913 ieee80211_node_refcnt(ni)+1);
1914 ieee80211_ref_node(ni);
1916 memset(¶ms, 0, sizeof(params));
1919 case IEEE80211_FC0_SUBTYPE_AUTH:
1922 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1923 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1924 ni->ni_challenge != NULL);
1927 * Deduce whether we're doing open authentication or
1928 * shared key authentication. We do the latter if
1929 * we're in the middle of a shared key authentication
1930 * handshake or if we're initiating an authentication
1931 * request and configured to use shared key.
1933 is_shared_key = has_challenge ||
1934 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1935 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1936 bss->ni_authmode == IEEE80211_AUTH_SHARED);
1938 m = ieee80211_getmgtframe(&frm,
1939 ic->ic_headroom + sizeof(struct ieee80211_frame),
1940 3 * sizeof(uint16_t)
1941 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1942 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1945 senderr(ENOMEM, is_tx_nobuf);
1947 ((uint16_t *)frm)[0] =
1948 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1949 : htole16(IEEE80211_AUTH_ALG_OPEN);
1950 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1951 ((uint16_t *)frm)[2] = htole16(status);/* status */
1953 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1954 ((uint16_t *)frm)[3] =
1955 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1956 IEEE80211_ELEMID_CHALLENGE);
1957 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1958 IEEE80211_CHALLENGE_LEN);
1959 m->m_pkthdr.len = m->m_len =
1960 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1961 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1962 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1963 "request encrypt frame (%s)", __func__);
1964 /* mark frame for encryption */
1965 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1968 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1970 /* XXX not right for shared key */
1971 if (status == IEEE80211_STATUS_SUCCESS)
1972 IEEE80211_NODE_STAT(ni, tx_auth);
1974 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1976 if (vap->iv_opmode == IEEE80211_M_STA)
1977 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1978 (void *) vap->iv_state);
1981 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1982 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1983 "send station deauthenticate (reason %d)", arg);
1984 m = ieee80211_getmgtframe(&frm,
1985 ic->ic_headroom + sizeof(struct ieee80211_frame),
1988 senderr(ENOMEM, is_tx_nobuf);
1989 *(uint16_t *)frm = htole16(arg); /* reason */
1990 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1992 IEEE80211_NODE_STAT(ni, tx_deauth);
1993 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1995 ieee80211_node_unauthorize(ni); /* port closed */
1998 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1999 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2001 * asreq frame format
2002 * [2] capability information
2003 * [2] listen interval
2004 * [6*] current AP address (reassoc only)
2006 * [tlv] supported rates
2007 * [tlv] extended supported rates
2008 * [4] power capability (optional)
2009 * [28] supported channels (optional)
2010 * [tlv] HT capabilities
2011 * [tlv] WME (optional)
2012 * [tlv] Vendor OUI HT capabilities (optional)
2013 * [tlv] Atheros capabilities (if negotiated)
2014 * [tlv] AppIE's (optional)
2016 m = ieee80211_getmgtframe(&frm,
2017 ic->ic_headroom + sizeof(struct ieee80211_frame),
2020 + IEEE80211_ADDR_LEN
2021 + 2 + IEEE80211_NWID_LEN
2022 + 2 + IEEE80211_RATE_SIZE
2023 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2026 + sizeof(struct ieee80211_wme_info)
2027 + sizeof(struct ieee80211_ie_htcap)
2028 + 4 + sizeof(struct ieee80211_ie_htcap)
2029 #ifdef IEEE80211_SUPPORT_SUPERG
2030 + sizeof(struct ieee80211_ath_ie)
2032 + (vap->iv_appie_wpa != NULL ?
2033 vap->iv_appie_wpa->ie_len : 0)
2034 + (vap->iv_appie_assocreq != NULL ?
2035 vap->iv_appie_assocreq->ie_len : 0)
2038 senderr(ENOMEM, is_tx_nobuf);
2040 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2041 ("wrong mode %u", vap->iv_opmode));
2042 capinfo = IEEE80211_CAPINFO_ESS;
2043 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2044 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2046 * NB: Some 11a AP's reject the request when
2047 * short premable is set.
2049 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2050 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2051 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2052 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2053 (ic->ic_caps & IEEE80211_C_SHSLOT))
2054 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2055 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2056 (vap->iv_flags & IEEE80211_F_DOTH))
2057 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2058 *(uint16_t *)frm = htole16(capinfo);
2061 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2062 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2066 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2067 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2068 frm += IEEE80211_ADDR_LEN;
2071 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2072 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2073 frm = ieee80211_add_rsn(frm, vap);
2074 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2075 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2076 frm = ieee80211_add_powercapability(frm,
2078 frm = ieee80211_add_supportedchannels(frm, ic);
2080 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2081 ni->ni_ies.htcap_ie != NULL &&
2082 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2083 frm = ieee80211_add_htcap(frm, ni);
2084 frm = ieee80211_add_wpa(frm, vap);
2085 if ((ic->ic_flags & IEEE80211_F_WME) &&
2086 ni->ni_ies.wme_ie != NULL)
2087 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2088 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2089 ni->ni_ies.htcap_ie != NULL &&
2090 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2091 frm = ieee80211_add_htcap_vendor(frm, ni);
2092 #ifdef IEEE80211_SUPPORT_SUPERG
2093 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2094 frm = ieee80211_add_ath(frm,
2095 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2096 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2097 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2098 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2100 #endif /* IEEE80211_SUPPORT_SUPERG */
2101 if (vap->iv_appie_assocreq != NULL)
2102 frm = add_appie(frm, vap->iv_appie_assocreq);
2103 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2105 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2106 (void *) vap->iv_state);
2109 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2110 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2112 * asresp frame format
2113 * [2] capability information
2115 * [2] association ID
2116 * [tlv] supported rates
2117 * [tlv] extended supported rates
2118 * [tlv] HT capabilities (standard, if STA enabled)
2119 * [tlv] HT information (standard, if STA enabled)
2120 * [tlv] WME (if configured and STA enabled)
2121 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2122 * [tlv] HT information (vendor OUI, if STA enabled)
2123 * [tlv] Atheros capabilities (if STA enabled)
2124 * [tlv] AppIE's (optional)
2126 m = ieee80211_getmgtframe(&frm,
2127 ic->ic_headroom + sizeof(struct ieee80211_frame),
2131 + 2 + IEEE80211_RATE_SIZE
2132 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2133 + sizeof(struct ieee80211_ie_htcap) + 4
2134 + sizeof(struct ieee80211_ie_htinfo) + 4
2135 + sizeof(struct ieee80211_wme_param)
2136 #ifdef IEEE80211_SUPPORT_SUPERG
2137 + sizeof(struct ieee80211_ath_ie)
2139 + (vap->iv_appie_assocresp != NULL ?
2140 vap->iv_appie_assocresp->ie_len : 0)
2143 senderr(ENOMEM, is_tx_nobuf);
2145 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2146 *(uint16_t *)frm = htole16(capinfo);
2149 *(uint16_t *)frm = htole16(arg); /* status */
2152 if (arg == IEEE80211_STATUS_SUCCESS) {
2153 *(uint16_t *)frm = htole16(ni->ni_associd);
2154 IEEE80211_NODE_STAT(ni, tx_assoc);
2156 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2159 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2160 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2161 /* NB: respond according to what we received */
2162 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2163 frm = ieee80211_add_htcap(frm, ni);
2164 frm = ieee80211_add_htinfo(frm, ni);
2166 if ((vap->iv_flags & IEEE80211_F_WME) &&
2167 ni->ni_ies.wme_ie != NULL)
2168 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2169 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2170 frm = ieee80211_add_htcap_vendor(frm, ni);
2171 frm = ieee80211_add_htinfo_vendor(frm, ni);
2173 #ifdef IEEE80211_SUPPORT_SUPERG
2174 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2175 frm = ieee80211_add_ath(frm,
2176 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2177 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2178 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2179 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2180 #endif /* IEEE80211_SUPPORT_SUPERG */
2181 if (vap->iv_appie_assocresp != NULL)
2182 frm = add_appie(frm, vap->iv_appie_assocresp);
2183 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2186 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2187 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2188 "send station disassociate (reason %d)", arg);
2189 m = ieee80211_getmgtframe(&frm,
2190 ic->ic_headroom + sizeof(struct ieee80211_frame),
2193 senderr(ENOMEM, is_tx_nobuf);
2194 *(uint16_t *)frm = htole16(arg); /* reason */
2195 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2197 IEEE80211_NODE_STAT(ni, tx_disassoc);
2198 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2202 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2203 "invalid mgmt frame type %u", type);
2204 senderr(EINVAL, is_tx_unknownmgt);
2208 /* NB: force non-ProbeResp frames to the highest queue */
2209 params.ibp_pri = WME_AC_VO;
2210 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2211 /* NB: we know all frames are unicast */
2212 params.ibp_try0 = bss->ni_txparms->maxretry;
2213 params.ibp_power = bss->ni_txpower;
2214 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2216 ieee80211_free_node(ni);
2223 * Return an mbuf with a probe response frame in it.
2224 * Space is left to prepend and 802.11 header at the
2225 * front but it's left to the caller to fill in.
2228 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2230 struct ieee80211vap *vap = bss->ni_vap;
2231 struct ieee80211com *ic = bss->ni_ic;
2232 const struct ieee80211_rateset *rs;
2238 * probe response frame format
2240 * [2] beacon interval
2241 * [2] cabability information
2243 * [tlv] supported rates
2244 * [tlv] parameter set (FH/DS)
2245 * [tlv] parameter set (IBSS)
2246 * [tlv] country (optional)
2247 * [3] power control (optional)
2248 * [5] channel switch announcement (CSA) (optional)
2249 * [tlv] extended rate phy (ERP)
2250 * [tlv] extended supported rates
2251 * [tlv] RSN (optional)
2252 * [tlv] HT capabilities
2253 * [tlv] HT information
2254 * [tlv] WPA (optional)
2255 * [tlv] WME (optional)
2256 * [tlv] Vendor OUI HT capabilities (optional)
2257 * [tlv] Vendor OUI HT information (optional)
2258 * [tlv] Atheros capabilities
2259 * [tlv] AppIE's (optional)
2260 * [tlv] Mesh ID (MBSS)
2261 * [tlv] Mesh Conf (MBSS)
2263 m = ieee80211_getmgtframe(&frm,
2264 ic->ic_headroom + sizeof(struct ieee80211_frame),
2268 + 2 + IEEE80211_NWID_LEN
2269 + 2 + IEEE80211_RATE_SIZE
2271 + IEEE80211_COUNTRY_MAX_SIZE
2273 + sizeof(struct ieee80211_csa_ie)
2275 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2276 + sizeof(struct ieee80211_ie_wpa)
2277 + sizeof(struct ieee80211_ie_htcap)
2278 + sizeof(struct ieee80211_ie_htinfo)
2279 + sizeof(struct ieee80211_ie_wpa)
2280 + sizeof(struct ieee80211_wme_param)
2281 + 4 + sizeof(struct ieee80211_ie_htcap)
2282 + 4 + sizeof(struct ieee80211_ie_htinfo)
2283 #ifdef IEEE80211_SUPPORT_SUPERG
2284 + sizeof(struct ieee80211_ath_ie)
2286 #ifdef IEEE80211_SUPPORT_MESH
2287 + 2 + IEEE80211_MESHID_LEN
2288 + sizeof(struct ieee80211_meshconf_ie)
2290 + (vap->iv_appie_proberesp != NULL ?
2291 vap->iv_appie_proberesp->ie_len : 0)
2294 vap->iv_stats.is_tx_nobuf++;
2298 memset(frm, 0, 8); /* timestamp should be filled later */
2300 *(uint16_t *)frm = htole16(bss->ni_intval);
2302 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2303 *(uint16_t *)frm = htole16(capinfo);
2306 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2307 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2308 frm = ieee80211_add_rates(frm, rs);
2310 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2311 *frm++ = IEEE80211_ELEMID_FHPARMS;
2313 *frm++ = bss->ni_fhdwell & 0x00ff;
2314 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2315 *frm++ = IEEE80211_FH_CHANSET(
2316 ieee80211_chan2ieee(ic, bss->ni_chan));
2317 *frm++ = IEEE80211_FH_CHANPAT(
2318 ieee80211_chan2ieee(ic, bss->ni_chan));
2319 *frm++ = bss->ni_fhindex;
2321 *frm++ = IEEE80211_ELEMID_DSPARMS;
2323 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2326 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2327 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2329 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2331 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2332 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2333 frm = ieee80211_add_countryie(frm, ic);
2334 if (vap->iv_flags & IEEE80211_F_DOTH) {
2335 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2336 frm = ieee80211_add_powerconstraint(frm, vap);
2337 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2338 frm = ieee80211_add_csa(frm, vap);
2340 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2341 frm = ieee80211_add_erp(frm, ic);
2342 frm = ieee80211_add_xrates(frm, rs);
2343 frm = ieee80211_add_rsn(frm, vap);
2345 * NB: legacy 11b clients do not get certain ie's.
2346 * The caller identifies such clients by passing
2347 * a token in legacy to us. Could expand this to be
2348 * any legacy client for stuff like HT ie's.
2350 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2351 legacy != IEEE80211_SEND_LEGACY_11B) {
2352 frm = ieee80211_add_htcap(frm, bss);
2353 frm = ieee80211_add_htinfo(frm, bss);
2355 frm = ieee80211_add_wpa(frm, vap);
2356 if (vap->iv_flags & IEEE80211_F_WME)
2357 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2358 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2359 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2360 legacy != IEEE80211_SEND_LEGACY_11B) {
2361 frm = ieee80211_add_htcap_vendor(frm, bss);
2362 frm = ieee80211_add_htinfo_vendor(frm, bss);
2364 #ifdef IEEE80211_SUPPORT_SUPERG
2365 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2366 legacy != IEEE80211_SEND_LEGACY_11B)
2367 frm = ieee80211_add_athcaps(frm, bss);
2369 if (vap->iv_appie_proberesp != NULL)
2370 frm = add_appie(frm, vap->iv_appie_proberesp);
2371 #ifdef IEEE80211_SUPPORT_MESH
2372 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2373 frm = ieee80211_add_meshid(frm, vap);
2374 frm = ieee80211_add_meshconf(frm, vap);
2377 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2383 * Send a probe response frame to the specified mac address.
2384 * This does not go through the normal mgt frame api so we
2385 * can specify the destination address and re-use the bss node
2386 * for the sta reference.
2389 ieee80211_send_proberesp(struct ieee80211vap *vap,
2390 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2392 struct ieee80211_node *bss = vap->iv_bss;
2393 struct ieee80211com *ic = vap->iv_ic;
2394 struct ieee80211_frame *wh;
2397 if (vap->iv_state == IEEE80211_S_CAC) {
2398 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2399 "block %s frame in CAC state", "probe response");
2400 vap->iv_stats.is_tx_badstate++;
2401 return EIO; /* XXX */
2405 * Hold a reference on the node so it doesn't go away until after
2406 * the xmit is complete all the way in the driver. On error we
2407 * will remove our reference.
2409 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2410 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2411 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2412 ieee80211_node_refcnt(bss)+1);
2413 ieee80211_ref_node(bss);
2415 m = ieee80211_alloc_proberesp(bss, legacy);
2417 ieee80211_free_node(bss);
2421 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2422 KASSERT(m != NULL, ("no room for header"));
2424 wh = mtod(m, struct ieee80211_frame *);
2425 ieee80211_send_setup(bss, m,
2426 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2427 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2428 /* XXX power management? */
2429 m->m_flags |= M_ENCAP; /* mark encapsulated */
2431 M_WME_SETAC(m, WME_AC_BE);
2433 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2434 "send probe resp on channel %u to %s%s\n",
2435 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2436 legacy ? " <legacy>" : "");
2437 IEEE80211_NODE_STAT(bss, tx_mgmt);
2439 return ic->ic_raw_xmit(bss, m, NULL);
2443 * Allocate and build a RTS (Request To Send) control frame.
2446 ieee80211_alloc_rts(struct ieee80211com *ic,
2447 const uint8_t ra[IEEE80211_ADDR_LEN],
2448 const uint8_t ta[IEEE80211_ADDR_LEN],
2451 struct ieee80211_frame_rts *rts;
2454 /* XXX honor ic_headroom */
2455 m = m_gethdr(M_DONTWAIT, MT_DATA);
2457 rts = mtod(m, struct ieee80211_frame_rts *);
2458 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2459 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2460 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2461 *(u_int16_t *)rts->i_dur = htole16(dur);
2462 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2463 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2465 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2471 * Allocate and build a CTS (Clear To Send) control frame.
2474 ieee80211_alloc_cts(struct ieee80211com *ic,
2475 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2477 struct ieee80211_frame_cts *cts;
2480 /* XXX honor ic_headroom */
2481 m = m_gethdr(M_DONTWAIT, MT_DATA);
2483 cts = mtod(m, struct ieee80211_frame_cts *);
2484 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2485 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2486 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2487 *(u_int16_t *)cts->i_dur = htole16(dur);
2488 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2490 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2496 ieee80211_tx_mgt_timeout(void *arg)
2498 struct ieee80211_node *ni = arg;
2499 struct ieee80211vap *vap = ni->ni_vap;
2501 if (vap->iv_state != IEEE80211_S_INIT &&
2502 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2504 * NB: it's safe to specify a timeout as the reason here;
2505 * it'll only be used in the right state.
2507 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2508 IEEE80211_SCAN_FAIL_TIMEOUT);
2513 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2515 struct ieee80211vap *vap = ni->ni_vap;
2516 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2519 * Frame transmit completed; arrange timer callback. If
2520 * transmit was successfuly we wait for response. Otherwise
2521 * we arrange an immediate callback instead of doing the
2522 * callback directly since we don't know what state the driver
2523 * is in (e.g. what locks it is holding). This work should
2524 * not be too time-critical and not happen too often so the
2525 * added overhead is acceptable.
2527 * XXX what happens if !acked but response shows up before callback?
2529 if (vap->iv_state == ostate)
2530 callout_reset(&vap->iv_mgtsend,
2531 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2532 ieee80211_tx_mgt_timeout, ni);
2536 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2537 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2539 struct ieee80211vap *vap = ni->ni_vap;
2540 struct ieee80211com *ic = ni->ni_ic;
2541 struct ieee80211_rateset *rs = &ni->ni_rates;
2545 * beacon frame format
2547 * [2] beacon interval
2548 * [2] cabability information
2550 * [tlv] supported rates
2551 * [3] parameter set (DS)
2552 * [8] CF parameter set (optional)
2553 * [tlv] parameter set (IBSS/TIM)
2554 * [tlv] country (optional)
2555 * [3] power control (optional)
2556 * [5] channel switch announcement (CSA) (optional)
2557 * [tlv] extended rate phy (ERP)
2558 * [tlv] extended supported rates
2559 * [tlv] RSN parameters
2560 * [tlv] HT capabilities
2561 * [tlv] HT information
2562 * XXX Vendor-specific OIDs (e.g. Atheros)
2563 * [tlv] WPA parameters
2564 * [tlv] WME parameters
2565 * [tlv] Vendor OUI HT capabilities (optional)
2566 * [tlv] Vendor OUI HT information (optional)
2567 * [tlv] Atheros capabilities (optional)
2568 * [tlv] TDMA parameters (optional)
2569 * [tlv] Mesh ID (MBSS)
2570 * [tlv] Mesh Conf (MBSS)
2571 * [tlv] application data (optional)
2574 memset(bo, 0, sizeof(*bo));
2576 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2578 *(uint16_t *)frm = htole16(ni->ni_intval);
2580 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2581 bo->bo_caps = (uint16_t *)frm;
2582 *(uint16_t *)frm = htole16(capinfo);
2584 *frm++ = IEEE80211_ELEMID_SSID;
2585 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2586 *frm++ = ni->ni_esslen;
2587 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2588 frm += ni->ni_esslen;
2591 frm = ieee80211_add_rates(frm, rs);
2592 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2593 *frm++ = IEEE80211_ELEMID_DSPARMS;
2595 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2597 if (ic->ic_flags & IEEE80211_F_PCF) {
2599 frm = ieee80211_add_cfparms(frm, ic);
2602 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2603 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2605 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2607 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2608 vap->iv_opmode == IEEE80211_M_MBSS) {
2609 /* TIM IE is the same for Mesh and Hostap */
2610 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2612 tie->tim_ie = IEEE80211_ELEMID_TIM;
2613 tie->tim_len = 4; /* length */
2614 tie->tim_count = 0; /* DTIM count */
2615 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2616 tie->tim_bitctl = 0; /* bitmap control */
2617 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2618 frm += sizeof(struct ieee80211_tim_ie);
2621 bo->bo_tim_trailer = frm;
2622 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2623 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2624 frm = ieee80211_add_countryie(frm, ic);
2625 if (vap->iv_flags & IEEE80211_F_DOTH) {
2626 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2627 frm = ieee80211_add_powerconstraint(frm, vap);
2629 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2630 frm = ieee80211_add_csa(frm, vap);
2633 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2635 frm = ieee80211_add_erp(frm, ic);
2637 frm = ieee80211_add_xrates(frm, rs);
2638 frm = ieee80211_add_rsn(frm, vap);
2639 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2640 frm = ieee80211_add_htcap(frm, ni);
2641 bo->bo_htinfo = frm;
2642 frm = ieee80211_add_htinfo(frm, ni);
2644 frm = ieee80211_add_wpa(frm, vap);
2645 if (vap->iv_flags & IEEE80211_F_WME) {
2647 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2649 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2650 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2651 frm = ieee80211_add_htcap_vendor(frm, ni);
2652 frm = ieee80211_add_htinfo_vendor(frm, ni);
2654 #ifdef IEEE80211_SUPPORT_SUPERG
2655 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2657 frm = ieee80211_add_athcaps(frm, ni);
2660 #ifdef IEEE80211_SUPPORT_TDMA
2661 if (vap->iv_caps & IEEE80211_C_TDMA) {
2663 frm = ieee80211_add_tdma(frm, vap);
2666 if (vap->iv_appie_beacon != NULL) {
2668 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2669 frm = add_appie(frm, vap->iv_appie_beacon);
2671 #ifdef IEEE80211_SUPPORT_MESH
2672 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2673 frm = ieee80211_add_meshid(frm, vap);
2674 bo->bo_meshconf = frm;
2675 frm = ieee80211_add_meshconf(frm, vap);
2678 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2679 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2680 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2684 * Allocate a beacon frame and fillin the appropriate bits.
2687 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2688 struct ieee80211_beacon_offsets *bo)
2690 struct ieee80211vap *vap = ni->ni_vap;
2691 struct ieee80211com *ic = ni->ni_ic;
2692 struct ifnet *ifp = vap->iv_ifp;
2693 struct ieee80211_frame *wh;
2699 * beacon frame format
2701 * [2] beacon interval
2702 * [2] cabability information
2704 * [tlv] supported rates
2705 * [3] parameter set (DS)
2706 * [8] CF parameter set (optional)
2707 * [tlv] parameter set (IBSS/TIM)
2708 * [tlv] country (optional)
2709 * [3] power control (optional)
2710 * [5] channel switch announcement (CSA) (optional)
2711 * [tlv] extended rate phy (ERP)
2712 * [tlv] extended supported rates
2713 * [tlv] RSN parameters
2714 * [tlv] HT capabilities
2715 * [tlv] HT information
2716 * [tlv] Vendor OUI HT capabilities (optional)
2717 * [tlv] Vendor OUI HT information (optional)
2718 * XXX Vendor-specific OIDs (e.g. Atheros)
2719 * [tlv] WPA parameters
2720 * [tlv] WME parameters
2721 * [tlv] TDMA parameters (optional)
2722 * [tlv] Mesh ID (MBSS)
2723 * [tlv] Mesh Conf (MBSS)
2724 * [tlv] application data (optional)
2725 * NB: we allocate the max space required for the TIM bitmap.
2726 * XXX how big is this?
2728 pktlen = 8 /* time stamp */
2729 + sizeof(uint16_t) /* beacon interval */
2730 + sizeof(uint16_t) /* capabilities */
2731 + 2 + ni->ni_esslen /* ssid */
2732 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2733 + 2 + 1 /* DS parameters */
2734 + 2 + 6 /* CF parameters */
2735 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2736 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2737 + 2 + 1 /* power control */
2738 + sizeof(struct ieee80211_csa_ie) /* CSA */
2740 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2741 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2742 2*sizeof(struct ieee80211_ie_wpa) : 0)
2743 /* XXX conditional? */
2744 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2745 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2746 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2747 sizeof(struct ieee80211_wme_param) : 0)
2748 #ifdef IEEE80211_SUPPORT_SUPERG
2749 + sizeof(struct ieee80211_ath_ie) /* ATH */
2751 #ifdef IEEE80211_SUPPORT_TDMA
2752 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2753 sizeof(struct ieee80211_tdma_param) : 0)
2755 #ifdef IEEE80211_SUPPORT_MESH
2756 + 2 + ni->ni_meshidlen
2757 + sizeof(struct ieee80211_meshconf_ie)
2759 + IEEE80211_MAX_APPIE
2761 m = ieee80211_getmgtframe(&frm,
2762 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2764 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2765 "%s: cannot get buf; size %u\n", __func__, pktlen);
2766 vap->iv_stats.is_tx_nobuf++;
2769 ieee80211_beacon_construct(m, frm, bo, ni);
2771 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2772 KASSERT(m != NULL, ("no space for 802.11 header?"));
2773 wh = mtod(m, struct ieee80211_frame *);
2774 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2775 IEEE80211_FC0_SUBTYPE_BEACON;
2776 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2777 *(uint16_t *)wh->i_dur = 0;
2778 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2779 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2780 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2781 *(uint16_t *)wh->i_seq = 0;
2787 * Update the dynamic parts of a beacon frame based on the current state.
2790 ieee80211_beacon_update(struct ieee80211_node *ni,
2791 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2793 struct ieee80211vap *vap = ni->ni_vap;
2794 struct ieee80211com *ic = ni->ni_ic;
2795 int len_changed = 0;
2797 struct ieee80211_frame *wh;
2798 ieee80211_seq seqno;
2802 * Handle 11h channel change when we've reached the count.
2803 * We must recalculate the beacon frame contents to account
2804 * for the new channel. Note we do this only for the first
2805 * vap that reaches this point; subsequent vaps just update
2806 * their beacon state to reflect the recalculated channel.
2808 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2809 vap->iv_csa_count == ic->ic_csa_count) {
2810 vap->iv_csa_count = 0;
2812 * Effect channel change before reconstructing the beacon
2813 * frame contents as many places reference ni_chan.
2815 if (ic->ic_csa_newchan != NULL)
2816 ieee80211_csa_completeswitch(ic);
2818 * NB: ieee80211_beacon_construct clears all pending
2819 * updates in bo_flags so we don't need to explicitly
2820 * clear IEEE80211_BEACON_CSA.
2822 ieee80211_beacon_construct(m,
2823 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2825 /* XXX do WME aggressive mode processing? */
2826 IEEE80211_UNLOCK(ic);
2827 return 1; /* just assume length changed */
2830 wh = mtod(m, struct ieee80211_frame *);
2831 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
2832 *(uint16_t *)&wh->i_seq[0] =
2833 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2834 M_SEQNO_SET(m, seqno);
2836 /* XXX faster to recalculate entirely or just changes? */
2837 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2838 *bo->bo_caps = htole16(capinfo);
2840 if (vap->iv_flags & IEEE80211_F_WME) {
2841 struct ieee80211_wme_state *wme = &ic->ic_wme;
2844 * Check for agressive mode change. When there is
2845 * significant high priority traffic in the BSS
2846 * throttle back BE traffic by using conservative
2847 * parameters. Otherwise BE uses agressive params
2848 * to optimize performance of legacy/non-QoS traffic.
2850 if (wme->wme_flags & WME_F_AGGRMODE) {
2851 if (wme->wme_hipri_traffic >
2852 wme->wme_hipri_switch_thresh) {
2853 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2854 "%s: traffic %u, disable aggressive mode\n",
2855 __func__, wme->wme_hipri_traffic);
2856 wme->wme_flags &= ~WME_F_AGGRMODE;
2857 ieee80211_wme_updateparams_locked(vap);
2858 wme->wme_hipri_traffic =
2859 wme->wme_hipri_switch_hysteresis;
2861 wme->wme_hipri_traffic = 0;
2863 if (wme->wme_hipri_traffic <=
2864 wme->wme_hipri_switch_thresh) {
2865 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2866 "%s: traffic %u, enable aggressive mode\n",
2867 __func__, wme->wme_hipri_traffic);
2868 wme->wme_flags |= WME_F_AGGRMODE;
2869 ieee80211_wme_updateparams_locked(vap);
2870 wme->wme_hipri_traffic = 0;
2872 wme->wme_hipri_traffic =
2873 wme->wme_hipri_switch_hysteresis;
2875 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2876 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
2877 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2881 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
2882 ieee80211_ht_update_beacon(vap, bo);
2883 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2885 #ifdef IEEE80211_SUPPORT_TDMA
2886 if (vap->iv_caps & IEEE80211_C_TDMA) {
2888 * NB: the beacon is potentially updated every TBTT.
2890 ieee80211_tdma_update_beacon(vap, bo);
2893 #ifdef IEEE80211_SUPPORT_MESH
2894 if (vap->iv_opmode == IEEE80211_M_MBSS)
2895 ieee80211_mesh_update_beacon(vap, bo);
2898 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2899 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
2900 struct ieee80211_tim_ie *tie =
2901 (struct ieee80211_tim_ie *) bo->bo_tim;
2902 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2903 u_int timlen, timoff, i;
2905 * ATIM/DTIM needs updating. If it fits in the
2906 * current space allocated then just copy in the
2907 * new bits. Otherwise we need to move any trailing
2908 * data to make room. Note that we know there is
2909 * contiguous space because ieee80211_beacon_allocate
2910 * insures there is space in the mbuf to write a
2911 * maximal-size virtual bitmap (based on iv_max_aid).
2914 * Calculate the bitmap size and offset, copy any
2915 * trailer out of the way, and then copy in the
2916 * new bitmap and update the information element.
2917 * Note that the tim bitmap must contain at least
2918 * one byte and any offset must be even.
2920 if (vap->iv_ps_pending != 0) {
2921 timoff = 128; /* impossibly large */
2922 for (i = 0; i < vap->iv_tim_len; i++)
2923 if (vap->iv_tim_bitmap[i]) {
2927 KASSERT(timoff != 128, ("tim bitmap empty!"));
2928 for (i = vap->iv_tim_len-1; i >= timoff; i--)
2929 if (vap->iv_tim_bitmap[i])
2931 timlen = 1 + (i - timoff);
2936 if (timlen != bo->bo_tim_len) {
2937 /* copy up/down trailer */
2938 int adjust = tie->tim_bitmap+timlen
2939 - bo->bo_tim_trailer;
2940 ovbcopy(bo->bo_tim_trailer,
2941 bo->bo_tim_trailer+adjust,
2942 bo->bo_tim_trailer_len);
2943 bo->bo_tim_trailer += adjust;
2944 bo->bo_erp += adjust;
2945 bo->bo_htinfo += adjust;
2946 #ifdef IEEE80211_SUPPORT_SUPERG
2947 bo->bo_ath += adjust;
2949 #ifdef IEEE80211_SUPPORT_TDMA
2950 bo->bo_tdma += adjust;
2952 #ifdef IEEE80211_SUPPORT_MESH
2953 bo->bo_meshconf += adjust;
2955 bo->bo_appie += adjust;
2956 bo->bo_wme += adjust;
2957 bo->bo_csa += adjust;
2958 bo->bo_tim_len = timlen;
2960 /* update information element */
2961 tie->tim_len = 3 + timlen;
2962 tie->tim_bitctl = timoff;
2965 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2968 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2970 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2971 "%s: TIM updated, pending %u, off %u, len %u\n",
2972 __func__, vap->iv_ps_pending, timoff, timlen);
2974 /* count down DTIM period */
2975 if (tie->tim_count == 0)
2976 tie->tim_count = tie->tim_period - 1;
2979 /* update state for buffered multicast frames on DTIM */
2980 if (mcast && tie->tim_count == 0)
2981 tie->tim_bitctl |= 1;
2983 tie->tim_bitctl &= ~1;
2984 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2985 struct ieee80211_csa_ie *csa =
2986 (struct ieee80211_csa_ie *) bo->bo_csa;
2989 * Insert or update CSA ie. If we're just starting
2990 * to count down to the channel switch then we need
2991 * to insert the CSA ie. Otherwise we just need to
2992 * drop the count. The actual change happens above
2993 * when the vap's count reaches the target count.
2995 if (vap->iv_csa_count == 0) {
2996 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2997 bo->bo_erp += sizeof(*csa);
2998 bo->bo_htinfo += sizeof(*csa);
2999 bo->bo_wme += sizeof(*csa);
3000 #ifdef IEEE80211_SUPPORT_SUPERG
3001 bo->bo_ath += sizeof(*csa);
3003 #ifdef IEEE80211_SUPPORT_TDMA
3004 bo->bo_tdma += sizeof(*csa);
3006 #ifdef IEEE80211_SUPPORT_MESH
3007 bo->bo_meshconf += sizeof(*csa);
3009 bo->bo_appie += sizeof(*csa);
3010 bo->bo_csa_trailer_len += sizeof(*csa);
3011 bo->bo_tim_trailer_len += sizeof(*csa);
3012 m->m_len += sizeof(*csa);
3013 m->m_pkthdr.len += sizeof(*csa);
3015 ieee80211_add_csa(bo->bo_csa, vap);
3018 vap->iv_csa_count++;
3019 /* NB: don't clear IEEE80211_BEACON_CSA */
3021 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3023 * ERP element needs updating.
3025 (void) ieee80211_add_erp(bo->bo_erp, ic);
3026 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3028 #ifdef IEEE80211_SUPPORT_SUPERG
3029 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3030 ieee80211_add_athcaps(bo->bo_ath, ni);
3031 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3035 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3036 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3042 aielen += aie->ie_len;
3043 if (aielen != bo->bo_appie_len) {
3044 /* copy up/down trailer */
3045 int adjust = aielen - bo->bo_appie_len;
3046 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3047 bo->bo_tim_trailer_len);
3048 bo->bo_tim_trailer += adjust;
3049 bo->bo_appie += adjust;
3050 bo->bo_appie_len = aielen;
3056 frm = add_appie(frm, aie);
3057 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3059 IEEE80211_UNLOCK(ic);