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>
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
67 #include <netinet/ip6.h>
70 #include <security/mac/mac_framework.h>
72 #define ETHER_HEADER_COPY(dst, src) \
73 memcpy(dst, src, sizeof(struct ether_header))
75 /* unalligned little endian access */
76 #define LE_WRITE_2(p, v) do { \
77 ((uint8_t *)(p))[0] = (v) & 0xff; \
78 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
80 #define LE_WRITE_4(p, v) do { \
81 ((uint8_t *)(p))[0] = (v) & 0xff; \
82 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
83 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
84 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
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 * Start method for vap's. All packets from the stack come
111 * through here. We handle common processing of the packets
112 * before dispatching them to the underlying device.
115 ieee80211_start(struct ifnet *ifp)
117 #define IS_DWDS(vap) \
118 (vap->iv_opmode == IEEE80211_M_WDS && \
119 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
120 struct ieee80211vap *vap = ifp->if_softc;
121 struct ieee80211com *ic = vap->iv_ic;
122 struct ifnet *parent = ic->ic_ifp;
123 struct ieee80211_node *ni;
125 struct ether_header *eh;
128 /* NB: parent must be up and running */
129 if (!IFNET_IS_UP_RUNNING(parent)) {
130 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
131 "%s: ignore queue, parent %s not up+running\n",
132 __func__, parent->if_xname);
136 if (vap->iv_state == IEEE80211_S_SLEEP) {
138 * In power save, wakeup device for transmit.
140 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
144 * No data frames go out unless we're running.
145 * Note in particular this covers CAC and CSA
146 * states (though maybe we should check muting
149 if (vap->iv_state != IEEE80211_S_RUN) {
151 /* re-check under the com lock to avoid races */
152 if (vap->iv_state != IEEE80211_S_RUN) {
153 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
154 "%s: ignore queue, in %s state\n",
155 __func__, ieee80211_state_name[vap->iv_state]);
156 vap->iv_stats.is_tx_badstate++;
157 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
158 IEEE80211_UNLOCK(ic);
161 IEEE80211_UNLOCK(ic);
164 IFQ_DEQUEUE(&ifp->if_snd, m);
168 * Sanitize mbuf flags for net80211 use. We cannot
169 * clear M_PWR_SAV or M_MORE_DATA because these may
170 * be set for frames that are re-submitted from the
173 * NB: This must be done before ieee80211_classify as
174 * it marks EAPOL in frames with M_EAPOL.
176 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
178 * Cancel any background scan.
180 if (ic->ic_flags & IEEE80211_F_SCAN)
181 ieee80211_cancel_anyscan(vap);
183 * Find the node for the destination so we can do
184 * things like power save and fast frames aggregation.
186 * NB: past this point various code assumes the first
187 * mbuf has the 802.3 header present (and contiguous).
190 if (m->m_len < sizeof(struct ether_header) &&
191 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
192 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
193 "discard frame, %s\n", "m_pullup failed");
194 vap->iv_stats.is_tx_nobuf++; /* XXX */
198 eh = mtod(m, struct ether_header *);
199 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
202 * Only unicast frames from the above go out
203 * DWDS vaps; multicast frames are handled by
204 * dispatching the frame as it comes through
205 * the AP vap (see below).
207 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
208 eh->ether_dhost, "mcast", "%s", "on DWDS");
209 vap->iv_stats.is_dwds_mcast++;
213 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
215 * Spam DWDS vap's w/ multicast traffic.
217 /* XXX only if dwds in use? */
218 ieee80211_dwds_mcast(vap, m);
221 #ifdef IEEE80211_SUPPORT_MESH
222 if (vap->iv_opmode != IEEE80211_M_MBSS) {
224 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
226 /* NB: ieee80211_find_txnode does stat+msg */
231 if (ni->ni_associd == 0 &&
232 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
233 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
234 eh->ether_dhost, NULL,
235 "sta not associated (type 0x%04x)",
236 htons(eh->ether_type));
237 vap->iv_stats.is_tx_notassoc++;
240 ieee80211_free_node(ni);
243 #ifdef IEEE80211_SUPPORT_MESH
245 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
247 * Proxy station only if configured.
249 if (!ieee80211_mesh_isproxyena(vap)) {
250 IEEE80211_DISCARD_MAC(vap,
251 IEEE80211_MSG_OUTPUT |
253 eh->ether_dhost, NULL,
254 "%s", "proxy not enabled");
255 vap->iv_stats.is_mesh_notproxy++;
260 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
262 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
265 * NB: ieee80211_mesh_discover holds/disposes
266 * frame (e.g. queueing on path discovery).
273 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
274 (m->m_flags & M_PWR_SAV) == 0) {
276 * Station in power save mode; pass the frame
277 * to the 802.11 layer and continue. We'll get
278 * the frame back when the time is right.
279 * XXX lose WDS vap linkage?
281 (void) ieee80211_pwrsave(ni, m);
282 ieee80211_free_node(ni);
285 /* calculate priority so drivers can find the tx queue */
286 if (ieee80211_classify(ni, m)) {
287 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
288 eh->ether_dhost, NULL,
289 "%s", "classification failure");
290 vap->iv_stats.is_tx_classify++;
293 ieee80211_free_node(ni);
297 * Stash the node pointer. Note that we do this after
298 * any call to ieee80211_dwds_mcast because that code
299 * uses any existing value for rcvif to identify the
300 * interface it (might have been) received on.
302 m->m_pkthdr.rcvif = (void *)ni;
304 BPF_MTAP(ifp, m); /* 802.3 tx */
307 * Check if A-MPDU tx aggregation is setup or if we
308 * should try to enable it. The sta must be associated
309 * with HT and A-MPDU enabled for use. When the policy
310 * routine decides we should enable A-MPDU we issue an
311 * ADDBA request and wait for a reply. The frame being
312 * encapsulated will go out w/o using A-MPDU, or possibly
313 * it might be collected by the driver and held/retransmit.
314 * The default ic_ampdu_enable routine handles staggering
315 * ADDBA requests in case the receiver NAK's us or we are
316 * otherwise unable to establish a BA stream.
318 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
319 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
320 (m->m_flags & M_EAPOL) == 0) {
321 const int ac = M_WME_GETAC(m);
322 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
324 ieee80211_txampdu_count_packet(tap);
325 if (IEEE80211_AMPDU_RUNNING(tap)) {
327 * Operational, mark frame for aggregation.
329 * XXX do tx aggregation here
331 m->m_flags |= M_AMPDU_MPDU;
332 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
333 ic->ic_ampdu_enable(ni, tap)) {
335 * Not negotiated yet, request service.
337 ieee80211_ampdu_request(ni, tap);
338 /* XXX hold frame for reply? */
341 #ifdef IEEE80211_SUPPORT_SUPERG
342 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
343 m = ieee80211_ff_check(ni, m);
345 /* NB: any ni ref held on stageq */
349 #endif /* IEEE80211_SUPPORT_SUPERG */
350 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
352 * Encapsulate the packet in prep for transmission.
354 m = ieee80211_encap(vap, ni, m);
356 /* NB: stat+msg handled in ieee80211_encap */
357 ieee80211_free_node(ni);
362 error = parent->if_transmit(parent, m);
364 /* NB: IFQ_HANDOFF reclaims mbuf */
365 ieee80211_free_node(ni);
369 ic->ic_lastdata = ticks;
375 * 802.11 output routine. This is (currently) used only to
376 * connect bpf write calls to the 802.11 layer for injecting
380 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
381 struct sockaddr *dst, struct route *ro)
383 #define senderr(e) do { error = (e); goto bad;} while (0)
384 struct ieee80211_node *ni = NULL;
385 struct ieee80211vap *vap;
386 struct ieee80211_frame *wh;
389 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
391 * Short-circuit requests if the vap is marked OACTIVE
392 * as this can happen because a packet came down through
393 * ieee80211_start before the vap entered RUN state in
394 * which case it's ok to just drop the frame. This
395 * should not be necessary but callers of if_output don't
402 * Hand to the 802.3 code if not tagged as
403 * a raw 802.11 frame.
405 if (dst->sa_family != AF_IEEE80211)
406 return vap->iv_output(ifp, m, dst, ro);
408 error = mac_ifnet_check_transmit(ifp, m);
412 if (ifp->if_flags & IFF_MONITOR)
414 if (!IFNET_IS_UP_RUNNING(ifp))
416 if (vap->iv_state == IEEE80211_S_CAC) {
417 IEEE80211_DPRINTF(vap,
418 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
419 "block %s frame in CAC state\n", "raw data");
420 vap->iv_stats.is_tx_badstate++;
421 senderr(EIO); /* XXX */
423 /* XXX bypass bridge, pfil, carp, etc. */
425 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
426 senderr(EIO); /* XXX */
427 wh = mtod(m, struct ieee80211_frame *);
428 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
429 IEEE80211_FC0_VERSION_0)
430 senderr(EIO); /* XXX */
432 /* locate destination node */
433 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
434 case IEEE80211_FC1_DIR_NODS:
435 case IEEE80211_FC1_DIR_FROMDS:
436 ni = ieee80211_find_txnode(vap, wh->i_addr1);
438 case IEEE80211_FC1_DIR_TODS:
439 case IEEE80211_FC1_DIR_DSTODS:
440 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
441 senderr(EIO); /* XXX */
442 ni = ieee80211_find_txnode(vap, wh->i_addr3);
445 senderr(EIO); /* XXX */
449 * Permit packets w/ bpf params through regardless
450 * (see below about sa_len).
452 if (dst->sa_len == 0)
453 senderr(EHOSTUNREACH);
454 ni = ieee80211_ref_node(vap->iv_bss);
458 * Sanitize mbuf for net80211 flags leaked from above.
460 * NB: This must be done before ieee80211_classify as
461 * it marks EAPOL in frames with M_EAPOL.
463 m->m_flags &= ~M_80211_TX;
465 /* calculate priority so drivers can find the tx queue */
466 /* XXX assumes an 802.3 frame */
467 if (ieee80211_classify(ni, m))
468 senderr(EIO); /* XXX */
471 IEEE80211_NODE_STAT(ni, tx_data);
472 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
473 IEEE80211_NODE_STAT(ni, tx_mcast);
474 m->m_flags |= M_MCAST;
476 IEEE80211_NODE_STAT(ni, tx_ucast);
477 /* NB: ieee80211_encap does not include 802.11 header */
478 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
481 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
482 * present by setting the sa_len field of the sockaddr (yes,
484 * NB: we assume sa_data is suitably aligned to cast.
486 return vap->iv_ic->ic_raw_xmit(ni, m,
487 (const struct ieee80211_bpf_params *)(dst->sa_len ?
488 dst->sa_data : NULL));
493 ieee80211_free_node(ni);
500 * Set the direction field and address fields of an outgoing
501 * frame. Note this should be called early on in constructing
502 * a frame as it sets i_fc[1]; other bits can then be or'd in.
505 ieee80211_send_setup(
506 struct ieee80211_node *ni,
509 const uint8_t sa[IEEE80211_ADDR_LEN],
510 const uint8_t da[IEEE80211_ADDR_LEN],
511 const uint8_t bssid[IEEE80211_ADDR_LEN])
513 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
514 struct ieee80211vap *vap = ni->ni_vap;
515 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
518 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
519 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
520 switch (vap->iv_opmode) {
521 case IEEE80211_M_STA:
522 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
523 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
524 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
525 IEEE80211_ADDR_COPY(wh->i_addr3, da);
527 case IEEE80211_M_IBSS:
528 case IEEE80211_M_AHDEMO:
529 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
530 IEEE80211_ADDR_COPY(wh->i_addr1, da);
531 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
532 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
534 case IEEE80211_M_HOSTAP:
535 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
536 IEEE80211_ADDR_COPY(wh->i_addr1, da);
537 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
538 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
540 case IEEE80211_M_WDS:
541 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
542 IEEE80211_ADDR_COPY(wh->i_addr1, da);
543 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
544 IEEE80211_ADDR_COPY(wh->i_addr3, da);
545 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
547 case IEEE80211_M_MBSS:
548 #ifdef IEEE80211_SUPPORT_MESH
549 /* XXX add support for proxied addresses */
550 if (IEEE80211_IS_MULTICAST(da)) {
551 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
553 IEEE80211_ADDR_COPY(wh->i_addr1, da);
554 IEEE80211_ADDR_COPY(wh->i_addr2,
557 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
558 IEEE80211_ADDR_COPY(wh->i_addr1, da);
559 IEEE80211_ADDR_COPY(wh->i_addr2,
561 IEEE80211_ADDR_COPY(wh->i_addr3, da);
562 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
566 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
570 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
571 IEEE80211_ADDR_COPY(wh->i_addr1, da);
572 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
573 #ifdef IEEE80211_SUPPORT_MESH
574 if (vap->iv_opmode == IEEE80211_M_MBSS)
575 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
578 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
580 *(uint16_t *)&wh->i_dur[0] = 0;
582 seqno = ni->ni_txseqs[tid]++;
583 *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
584 M_SEQNO_SET(m, seqno);
586 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
587 m->m_flags |= M_MCAST;
592 * Send a management frame to the specified node. The node pointer
593 * must have a reference as the pointer will be passed to the driver
594 * and potentially held for a long time. If the frame is successfully
595 * dispatched to the driver, then it is responsible for freeing the
596 * reference (and potentially free'ing up any associated storage);
597 * otherwise deal with reclaiming any reference (on error).
600 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
601 struct ieee80211_bpf_params *params)
603 struct ieee80211vap *vap = ni->ni_vap;
604 struct ieee80211com *ic = ni->ni_ic;
605 struct ieee80211_frame *wh;
607 KASSERT(ni != NULL, ("null node"));
609 if (vap->iv_state == IEEE80211_S_CAC) {
610 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
611 ni, "block %s frame in CAC state",
612 ieee80211_mgt_subtype_name[
613 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
614 IEEE80211_FC0_SUBTYPE_SHIFT]);
615 vap->iv_stats.is_tx_badstate++;
616 ieee80211_free_node(ni);
618 return EIO; /* XXX */
621 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
623 ieee80211_free_node(ni);
627 wh = mtod(m, struct ieee80211_frame *);
628 ieee80211_send_setup(ni, m,
629 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
630 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
631 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
632 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
633 "encrypting frame (%s)", __func__);
634 wh->i_fc[1] |= IEEE80211_FC1_WEP;
636 m->m_flags |= M_ENCAP; /* mark encapsulated */
638 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
639 M_WME_SETAC(m, params->ibp_pri);
641 #ifdef IEEE80211_DEBUG
642 /* avoid printing too many frames */
643 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
644 ieee80211_msg_dumppkts(vap)) {
645 printf("[%s] send %s on channel %u\n",
646 ether_sprintf(wh->i_addr1),
647 ieee80211_mgt_subtype_name[
648 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
649 IEEE80211_FC0_SUBTYPE_SHIFT],
650 ieee80211_chan2ieee(ic, ic->ic_curchan));
653 IEEE80211_NODE_STAT(ni, tx_mgmt);
655 return ic->ic_raw_xmit(ni, m, params);
659 * Send a null data frame to the specified node. If the station
660 * is setup for QoS then a QoS Null Data frame is constructed.
661 * If this is a WDS station then a 4-address frame is constructed.
663 * NB: the caller is assumed to have setup a node reference
664 * for use; this is necessary to deal with a race condition
665 * when probing for inactive stations. Like ieee80211_mgmt_output
666 * we must cleanup any node reference on error; however we
667 * can safely just unref it as we know it will never be the
668 * last reference to the node.
671 ieee80211_send_nulldata(struct ieee80211_node *ni)
673 struct ieee80211vap *vap = ni->ni_vap;
674 struct ieee80211com *ic = ni->ni_ic;
676 struct ieee80211_frame *wh;
680 if (vap->iv_state == IEEE80211_S_CAC) {
681 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
682 ni, "block %s frame in CAC state", "null data");
683 ieee80211_unref_node(&ni);
684 vap->iv_stats.is_tx_badstate++;
685 return EIO; /* XXX */
688 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
689 hdrlen = sizeof(struct ieee80211_qosframe);
691 hdrlen = sizeof(struct ieee80211_frame);
692 /* NB: only WDS vap's get 4-address frames */
693 if (vap->iv_opmode == IEEE80211_M_WDS)
694 hdrlen += IEEE80211_ADDR_LEN;
695 if (ic->ic_flags & IEEE80211_F_DATAPAD)
696 hdrlen = roundup(hdrlen, sizeof(uint32_t));
698 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
701 ieee80211_unref_node(&ni);
702 vap->iv_stats.is_tx_nobuf++;
705 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
706 ("leading space %zd", M_LEADINGSPACE(m)));
707 M_PREPEND(m, hdrlen, M_DONTWAIT);
709 /* NB: cannot happen */
710 ieee80211_free_node(ni);
714 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
715 if (ni->ni_flags & IEEE80211_NODE_QOS) {
716 const int tid = WME_AC_TO_TID(WME_AC_BE);
719 ieee80211_send_setup(ni, m,
720 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
721 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
723 if (vap->iv_opmode == IEEE80211_M_WDS)
724 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
726 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
727 qos[0] = tid & IEEE80211_QOS_TID;
728 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
729 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
732 ieee80211_send_setup(ni, m,
733 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
734 IEEE80211_NONQOS_TID,
735 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
737 if (vap->iv_opmode != IEEE80211_M_WDS) {
738 /* NB: power management bit is never sent by an AP */
739 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
740 vap->iv_opmode != IEEE80211_M_HOSTAP)
741 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
743 m->m_len = m->m_pkthdr.len = hdrlen;
744 m->m_flags |= M_ENCAP; /* mark encapsulated */
746 M_WME_SETAC(m, WME_AC_BE);
748 IEEE80211_NODE_STAT(ni, tx_data);
750 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
751 "send %snull data frame on channel %u, pwr mgt %s",
752 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
753 ieee80211_chan2ieee(ic, ic->ic_curchan),
754 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
756 return ic->ic_raw_xmit(ni, m, NULL);
760 * Assign priority to a frame based on any vlan tag assigned
761 * to the station and/or any Diffserv setting in an IP header.
762 * Finally, if an ACM policy is setup (in station mode) it's
766 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
768 const struct ether_header *eh = mtod(m, struct ether_header *);
769 int v_wme_ac, d_wme_ac, ac;
772 * Always promote PAE/EAPOL frames to high priority.
774 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
775 /* NB: mark so others don't need to check header */
776 m->m_flags |= M_EAPOL;
781 * Non-qos traffic goes to BE.
783 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
789 * If node has a vlan tag then all traffic
790 * to it must have a matching tag.
793 if (ni->ni_vlan != 0) {
794 if ((m->m_flags & M_VLANTAG) == 0) {
795 IEEE80211_NODE_STAT(ni, tx_novlantag);
798 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
799 EVL_VLANOFTAG(ni->ni_vlan)) {
800 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
803 /* map vlan priority to AC */
804 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
807 /* XXX m_copydata may be too slow for fast path */
809 if (eh->ether_type == htons(ETHERTYPE_IP)) {
812 * IP frame, map the DSCP bits from the TOS field.
814 /* NB: ip header may not be in first mbuf */
815 m_copydata(m, sizeof(struct ether_header) +
816 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
817 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
818 d_wme_ac = TID_TO_WME_AC(tos);
822 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
826 * IPv6 frame, map the DSCP bits from the TOS field.
828 m_copydata(m, sizeof(struct ether_header) +
829 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
831 tos = (uint8_t)(ntohl(flow) >> 20);
832 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
833 d_wme_ac = TID_TO_WME_AC(tos);
836 d_wme_ac = WME_AC_BE;
844 * Use highest priority AC.
846 if (v_wme_ac > d_wme_ac)
854 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
855 static const int acmap[4] = {
856 WME_AC_BK, /* WME_AC_BE */
857 WME_AC_BK, /* WME_AC_BK */
858 WME_AC_BE, /* WME_AC_VI */
859 WME_AC_VI, /* WME_AC_VO */
861 struct ieee80211com *ic = ni->ni_ic;
863 while (ac != WME_AC_BK &&
864 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
873 * Insure there is sufficient contiguous space to encapsulate the
874 * 802.11 data frame. If room isn't already there, arrange for it.
875 * Drivers and cipher modules assume we have done the necessary work
876 * and fail rudely if they don't find the space they need.
879 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
880 struct ieee80211_key *key, struct mbuf *m)
882 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
883 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
886 /* XXX belongs in crypto code? */
887 needed_space += key->wk_cipher->ic_header;
890 * When crypto is being done in the host we must insure
891 * the data are writable for the cipher routines; clone
892 * a writable mbuf chain.
893 * XXX handle SWMIC specially
895 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
896 m = m_unshare(m, M_NOWAIT);
898 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
899 "%s: cannot get writable mbuf\n", __func__);
900 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
906 * We know we are called just before stripping an Ethernet
907 * header and prepending an LLC header. This means we know
909 * sizeof(struct ether_header) - sizeof(struct llc)
910 * bytes recovered to which we need additional space for the
911 * 802.11 header and any crypto header.
913 /* XXX check trailing space and copy instead? */
914 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
915 struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
917 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
918 "%s: cannot expand storage\n", __func__);
919 vap->iv_stats.is_tx_nobuf++;
923 KASSERT(needed_space <= MHLEN,
924 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
926 * Setup new mbuf to have leading space to prepend the
927 * 802.11 header and any crypto header bits that are
928 * required (the latter are added when the driver calls
929 * back to ieee80211_crypto_encap to do crypto encapsulation).
931 /* NB: must be first 'cuz it clobbers m_data */
933 n->m_len = 0; /* NB: m_gethdr does not set */
934 n->m_data += needed_space;
936 * Pull up Ethernet header to create the expected layout.
937 * We could use m_pullup but that's overkill (i.e. we don't
938 * need the actual data) and it cannot fail so do it inline
941 /* NB: struct ether_header is known to be contiguous */
942 n->m_len += sizeof(struct ether_header);
943 m->m_len -= sizeof(struct ether_header);
944 m->m_data += sizeof(struct ether_header);
946 * Replace the head of the chain.
952 #undef TO_BE_RECLAIMED
956 * Return the transmit key to use in sending a unicast frame.
957 * If a unicast key is set we use that. When no unicast key is set
958 * we fall back to the default transmit key.
960 static __inline struct ieee80211_key *
961 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
962 struct ieee80211_node *ni)
964 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
965 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
966 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
968 return &vap->iv_nw_keys[vap->iv_def_txkey];
970 return &ni->ni_ucastkey;
975 * Return the transmit key to use in sending a multicast frame.
976 * Multicast traffic always uses the group key which is installed as
977 * the default tx key.
979 static __inline struct ieee80211_key *
980 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
981 struct ieee80211_node *ni)
983 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
984 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
986 return &vap->iv_nw_keys[vap->iv_def_txkey];
990 * Encapsulate an outbound data frame. The mbuf chain is updated.
991 * If an error is encountered NULL is returned. The caller is required
992 * to provide a node reference and pullup the ethernet header in the
995 * NB: Packet is assumed to be processed by ieee80211_classify which
996 * marked EAPOL frames w/ M_EAPOL.
999 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1002 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1003 struct ieee80211com *ic = ni->ni_ic;
1004 #ifdef IEEE80211_SUPPORT_MESH
1005 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1006 struct ieee80211_meshcntl_ae10 *mc;
1008 struct ether_header eh;
1009 struct ieee80211_frame *wh;
1010 struct ieee80211_key *key;
1012 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1013 ieee80211_seq seqno;
1014 int meshhdrsize, meshae;
1018 * Copy existing Ethernet header to a safe place. The
1019 * rest of the code assumes it's ok to strip it when
1020 * reorganizing state for the final encapsulation.
1022 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1023 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1026 * Insure space for additional headers. First identify
1027 * transmit key to use in calculating any buffer adjustments
1028 * required. This is also used below to do privacy
1029 * encapsulation work. Then calculate the 802.11 header
1030 * size and any padding required by the driver.
1032 * Note key may be NULL if we fall back to the default
1033 * transmit key and that is not set. In that case the
1034 * buffer may not be expanded as needed by the cipher
1035 * routines, but they will/should discard it.
1037 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1038 if (vap->iv_opmode == IEEE80211_M_STA ||
1039 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1040 (vap->iv_opmode == IEEE80211_M_WDS &&
1041 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1042 key = ieee80211_crypto_getucastkey(vap, ni);
1044 key = ieee80211_crypto_getmcastkey(vap, ni);
1045 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1046 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1048 "no default transmit key (%s) deftxkey %u",
1049 __func__, vap->iv_def_txkey);
1050 vap->iv_stats.is_tx_nodefkey++;
1056 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1057 * frames so suppress use. This may be an issue if other
1058 * ap's require all data frames to be QoS-encapsulated
1059 * once negotiated in which case we'll need to make this
1062 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1063 (m->m_flags & M_EAPOL) == 0;
1065 hdrsize = sizeof(struct ieee80211_qosframe);
1067 hdrsize = sizeof(struct ieee80211_frame);
1068 #ifdef IEEE80211_SUPPORT_MESH
1069 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1071 * Mesh data frames are encapsulated according to the
1072 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1073 * o Group Addressed data (aka multicast) originating
1074 * at the local sta are sent w/ 3-address format and
1075 * address extension mode 00
1076 * o Individually Addressed data (aka unicast) originating
1077 * at the local sta are sent w/ 4-address format and
1078 * address extension mode 00
1079 * o Group Addressed data forwarded from a non-mesh sta are
1080 * sent w/ 3-address format and address extension mode 01
1081 * o Individually Address data from another sta are sent
1082 * w/ 4-address format and address extension mode 10
1084 is4addr = 0; /* NB: don't use, disable */
1085 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1086 hdrsize += IEEE80211_ADDR_LEN; /* unicast are 4-addr */
1087 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1088 /* XXX defines for AE modes */
1089 if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1090 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1093 meshae = 4; /* NB: pseudo */
1094 } else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1096 meshhdrsize += 1*IEEE80211_ADDR_LEN;
1099 meshhdrsize += 2*IEEE80211_ADDR_LEN;
1104 * 4-address frames need to be generated for:
1105 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1106 * o packets sent through a vap marked for relaying
1107 * (e.g. a station operating with dynamic WDS)
1109 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1110 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1111 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1113 hdrsize += IEEE80211_ADDR_LEN;
1114 meshhdrsize = meshae = 0;
1115 #ifdef IEEE80211_SUPPORT_MESH
1119 * Honor driver DATAPAD requirement.
1121 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1122 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1126 if (__predict_true((m->m_flags & M_FF) == 0)) {
1130 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1132 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1135 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1136 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1137 llc = mtod(m, struct llc *);
1138 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1139 llc->llc_control = LLC_UI;
1140 llc->llc_snap.org_code[0] = 0;
1141 llc->llc_snap.org_code[1] = 0;
1142 llc->llc_snap.org_code[2] = 0;
1143 llc->llc_snap.ether_type = eh.ether_type;
1145 #ifdef IEEE80211_SUPPORT_SUPERG
1149 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1154 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1156 M_PREPEND(m, hdrspace + meshhdrsize, M_DONTWAIT);
1158 vap->iv_stats.is_tx_nobuf++;
1161 wh = mtod(m, struct ieee80211_frame *);
1162 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1163 *(uint16_t *)wh->i_dur = 0;
1164 qos = NULL; /* NB: quiet compiler */
1166 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1167 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1168 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1169 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1170 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1171 } else switch (vap->iv_opmode) {
1172 case IEEE80211_M_STA:
1173 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1174 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1175 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1176 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1178 case IEEE80211_M_IBSS:
1179 case IEEE80211_M_AHDEMO:
1180 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1181 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1182 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1184 * NB: always use the bssid from iv_bss as the
1185 * neighbor's may be stale after an ibss merge
1187 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1189 case IEEE80211_M_HOSTAP:
1190 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1191 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1192 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1193 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1195 #ifdef IEEE80211_SUPPORT_MESH
1196 case IEEE80211_M_MBSS:
1197 /* NB: offset by hdrspace to deal with DATAPAD */
1198 mc = (struct ieee80211_meshcntl_ae10 *)
1199 (mtod(m, uint8_t *) + hdrspace);
1201 case 0: /* ucast, no proxy */
1202 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1203 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1204 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1205 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1206 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1208 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1210 case 4: /* mcast, no proxy */
1211 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1212 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1213 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1214 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1215 mc->mc_flags = 0; /* NB: AE is really 0 */
1216 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1218 case 1: /* mcast, proxy */
1219 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1220 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1221 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1222 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1224 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1225 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1227 case 2: /* ucast, proxy */
1228 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1229 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1230 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1231 /* XXX not right, need MeshDA */
1232 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1233 /* XXX assume are MeshSA */
1234 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1236 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1237 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1238 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1241 KASSERT(0, ("meshae %d", meshae));
1244 mc->mc_ttl = ms->ms_ttl;
1246 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1249 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1253 if (m->m_flags & M_MORE_DATA)
1254 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1259 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1260 /* NB: mesh case handled earlier */
1261 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1262 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1263 ac = M_WME_GETAC(m);
1264 /* map from access class/queue to 11e header priorty value */
1265 tid = WME_AC_TO_TID(ac);
1266 qos[0] = tid & IEEE80211_QOS_TID;
1267 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1268 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1270 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1272 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1274 * NB: don't assign a sequence # to potential
1275 * aggregates; we expect this happens at the
1276 * point the frame comes off any aggregation q
1277 * as otherwise we may introduce holes in the
1278 * BA sequence space and/or make window accouting
1281 * XXX may want to control this with a driver
1282 * capability; this may also change when we pull
1283 * aggregation up into net80211
1285 seqno = ni->ni_txseqs[tid]++;
1286 *(uint16_t *)wh->i_seq =
1287 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1288 M_SEQNO_SET(m, seqno);
1291 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1292 *(uint16_t *)wh->i_seq =
1293 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1294 M_SEQNO_SET(m, seqno);
1298 /* check if xmit fragmentation is required */
1299 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1300 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1301 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1302 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1305 * IEEE 802.1X: send EAPOL frames always in the clear.
1306 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1308 if ((m->m_flags & M_EAPOL) == 0 ||
1309 ((vap->iv_flags & IEEE80211_F_WPA) &&
1310 (vap->iv_opmode == IEEE80211_M_STA ?
1311 !IEEE80211_KEY_UNDEFINED(key) :
1312 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1313 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1314 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1315 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1317 "%s", "enmic failed, discard frame");
1318 vap->iv_stats.is_crypto_enmicfail++;
1323 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1324 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1327 m->m_flags |= M_ENCAP; /* mark encapsulated */
1329 IEEE80211_NODE_STAT(ni, tx_data);
1330 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1331 IEEE80211_NODE_STAT(ni, tx_mcast);
1332 m->m_flags |= M_MCAST;
1334 IEEE80211_NODE_STAT(ni, tx_ucast);
1335 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1346 * Fragment the frame according to the specified mtu.
1347 * The size of the 802.11 header (w/o padding) is provided
1348 * so we don't need to recalculate it. We create a new
1349 * mbuf for each fragment and chain it through m_nextpkt;
1350 * we might be able to optimize this by reusing the original
1351 * packet's mbufs but that is significantly more complicated.
1354 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1355 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1357 struct ieee80211_frame *wh, *whf;
1358 struct mbuf *m, *prev, *next;
1359 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1361 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1362 KASSERT(m0->m_pkthdr.len > mtu,
1363 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1365 wh = mtod(m0, struct ieee80211_frame *);
1366 /* NB: mark the first frag; it will be propagated below */
1367 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1368 totalhdrsize = hdrsize + ciphdrsize;
1370 off = mtu - ciphdrsize;
1371 remainder = m0->m_pkthdr.len - off;
1374 fragsize = totalhdrsize + remainder;
1377 /* XXX fragsize can be >2048! */
1378 KASSERT(fragsize < MCLBYTES,
1379 ("fragment size %u too big!", fragsize));
1380 if (fragsize > MHLEN)
1381 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1383 m = m_gethdr(M_DONTWAIT, MT_DATA);
1386 /* leave room to prepend any cipher header */
1387 m_align(m, fragsize - ciphdrsize);
1390 * Form the header in the fragment. Note that since
1391 * we mark the first fragment with the MORE_FRAG bit
1392 * it automatically is propagated to each fragment; we
1393 * need only clear it on the last fragment (done below).
1395 whf = mtod(m, struct ieee80211_frame *);
1396 memcpy(whf, wh, hdrsize);
1397 *(uint16_t *)&whf->i_seq[0] |= htole16(
1398 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1399 IEEE80211_SEQ_FRAG_SHIFT);
1402 payload = fragsize - totalhdrsize;
1403 /* NB: destination is known to be contiguous */
1404 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1405 m->m_len = hdrsize + payload;
1406 m->m_pkthdr.len = hdrsize + payload;
1407 m->m_flags |= M_FRAG;
1409 /* chain up the fragment */
1410 prev->m_nextpkt = m;
1413 /* deduct fragment just formed */
1414 remainder -= payload;
1416 } while (remainder != 0);
1418 /* set the last fragment */
1419 m->m_flags |= M_LASTFRAG;
1420 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1422 /* strip first mbuf now that everything has been copied */
1423 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1424 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1426 vap->iv_stats.is_tx_fragframes++;
1427 vap->iv_stats.is_tx_frags += fragno-1;
1431 /* reclaim fragments but leave original frame for caller to free */
1432 for (m = m0->m_nextpkt; m != NULL; m = next) {
1433 next = m->m_nextpkt;
1434 m->m_nextpkt = NULL; /* XXX paranoid */
1437 m0->m_nextpkt = NULL;
1442 * Add a supported rates element id to a frame.
1445 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1449 *frm++ = IEEE80211_ELEMID_RATES;
1450 nrates = rs->rs_nrates;
1451 if (nrates > IEEE80211_RATE_SIZE)
1452 nrates = IEEE80211_RATE_SIZE;
1454 memcpy(frm, rs->rs_rates, nrates);
1455 return frm + nrates;
1459 * Add an extended supported rates element id to a frame.
1462 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1465 * Add an extended supported rates element if operating in 11g mode.
1467 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1468 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1469 *frm++ = IEEE80211_ELEMID_XRATES;
1471 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1478 * Add an ssid element to a frame.
1481 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1483 *frm++ = IEEE80211_ELEMID_SSID;
1485 memcpy(frm, ssid, len);
1490 * Add an erp element to a frame.
1493 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1497 *frm++ = IEEE80211_ELEMID_ERP;
1500 if (ic->ic_nonerpsta != 0)
1501 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1502 if (ic->ic_flags & IEEE80211_F_USEPROT)
1503 erp |= IEEE80211_ERP_USE_PROTECTION;
1504 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1505 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1511 * Add a CFParams element to a frame.
1514 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1516 #define ADDSHORT(frm, v) do { \
1517 LE_WRITE_2(frm, v); \
1520 *frm++ = IEEE80211_ELEMID_CFPARMS;
1522 *frm++ = 0; /* CFP count */
1523 *frm++ = 2; /* CFP period */
1524 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1525 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1530 static __inline uint8_t *
1531 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1533 memcpy(frm, ie->ie_data, ie->ie_len);
1534 return frm + ie->ie_len;
1537 static __inline uint8_t *
1538 add_ie(uint8_t *frm, const uint8_t *ie)
1540 memcpy(frm, ie, 2 + ie[1]);
1541 return frm + 2 + ie[1];
1544 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1546 * Add a WME information element to a frame.
1549 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1551 static const struct ieee80211_wme_info info = {
1552 .wme_id = IEEE80211_ELEMID_VENDOR,
1553 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1554 .wme_oui = { WME_OUI_BYTES },
1555 .wme_type = WME_OUI_TYPE,
1556 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1557 .wme_version = WME_VERSION,
1560 memcpy(frm, &info, sizeof(info));
1561 return frm + sizeof(info);
1565 * Add a WME parameters element to a frame.
1568 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1570 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1571 #define ADDSHORT(frm, v) do { \
1572 LE_WRITE_2(frm, v); \
1575 /* NB: this works 'cuz a param has an info at the front */
1576 static const struct ieee80211_wme_info param = {
1577 .wme_id = IEEE80211_ELEMID_VENDOR,
1578 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1579 .wme_oui = { WME_OUI_BYTES },
1580 .wme_type = WME_OUI_TYPE,
1581 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1582 .wme_version = WME_VERSION,
1586 memcpy(frm, ¶m, sizeof(param));
1587 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1588 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1589 *frm++ = 0; /* reserved field */
1590 for (i = 0; i < WME_NUM_AC; i++) {
1591 const struct wmeParams *ac =
1592 &wme->wme_bssChanParams.cap_wmeParams[i];
1593 *frm++ = SM(i, WME_PARAM_ACI)
1594 | SM(ac->wmep_acm, WME_PARAM_ACM)
1595 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1597 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1598 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1600 ADDSHORT(frm, ac->wmep_txopLimit);
1606 #undef WME_OUI_BYTES
1609 * Add an 11h Power Constraint element to a frame.
1612 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1614 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1615 /* XXX per-vap tx power limit? */
1616 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1618 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1620 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1625 * Add an 11h Power Capability element to a frame.
1628 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1630 frm[0] = IEEE80211_ELEMID_PWRCAP;
1632 frm[2] = c->ic_minpower;
1633 frm[3] = c->ic_maxpower;
1638 * Add an 11h Supported Channels element to a frame.
1641 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1643 static const int ielen = 26;
1645 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1647 /* XXX not correct */
1648 memcpy(frm+2, ic->ic_chan_avail, ielen);
1649 return frm + 2 + ielen;
1653 * Add an 11h Channel Switch Announcement element to a frame.
1654 * Note that we use the per-vap CSA count to adjust the global
1655 * counter so we can use this routine to form probe response
1656 * frames and get the current count.
1659 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1661 struct ieee80211com *ic = vap->iv_ic;
1662 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1664 csa->csa_ie = IEEE80211_ELEMID_CSA;
1666 csa->csa_mode = 1; /* XXX force quiet on channel */
1667 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1668 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1669 return frm + sizeof(*csa);
1673 * Add an 11h country information element to a frame.
1676 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1679 if (ic->ic_countryie == NULL ||
1680 ic->ic_countryie_chan != ic->ic_bsschan) {
1682 * Handle lazy construction of ie. This is done on
1683 * first use and after a channel change that requires
1686 if (ic->ic_countryie != NULL)
1687 free(ic->ic_countryie, M_80211_NODE_IE);
1688 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1689 if (ic->ic_countryie == NULL)
1691 ic->ic_countryie_chan = ic->ic_bsschan;
1693 return add_appie(frm, ic->ic_countryie);
1697 * Send a probe request frame with the specified ssid
1698 * and any optional information element data.
1701 ieee80211_send_probereq(struct ieee80211_node *ni,
1702 const uint8_t sa[IEEE80211_ADDR_LEN],
1703 const uint8_t da[IEEE80211_ADDR_LEN],
1704 const uint8_t bssid[IEEE80211_ADDR_LEN],
1705 const uint8_t *ssid, size_t ssidlen)
1707 struct ieee80211vap *vap = ni->ni_vap;
1708 struct ieee80211com *ic = ni->ni_ic;
1709 const struct ieee80211_txparam *tp;
1710 struct ieee80211_bpf_params params;
1711 struct ieee80211_frame *wh;
1712 const struct ieee80211_rateset *rs;
1716 if (vap->iv_state == IEEE80211_S_CAC) {
1717 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1718 "block %s frame in CAC state", "probe request");
1719 vap->iv_stats.is_tx_badstate++;
1720 return EIO; /* XXX */
1724 * Hold a reference on the node so it doesn't go away until after
1725 * the xmit is complete all the way in the driver. On error we
1726 * will remove our reference.
1728 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1729 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1731 ni, ether_sprintf(ni->ni_macaddr),
1732 ieee80211_node_refcnt(ni)+1);
1733 ieee80211_ref_node(ni);
1736 * prreq frame format
1738 * [tlv] supported rates
1739 * [tlv] RSN (optional)
1740 * [tlv] extended supported rates
1741 * [tlv] WPA (optional)
1742 * [tlv] user-specified ie's
1744 m = ieee80211_getmgtframe(&frm,
1745 ic->ic_headroom + sizeof(struct ieee80211_frame),
1746 2 + IEEE80211_NWID_LEN
1747 + 2 + IEEE80211_RATE_SIZE
1748 + sizeof(struct ieee80211_ie_wpa)
1749 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1750 + sizeof(struct ieee80211_ie_wpa)
1751 + (vap->iv_appie_probereq != NULL ?
1752 vap->iv_appie_probereq->ie_len : 0)
1755 vap->iv_stats.is_tx_nobuf++;
1756 ieee80211_free_node(ni);
1760 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1761 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1762 frm = ieee80211_add_rates(frm, rs);
1763 if (vap->iv_flags & IEEE80211_F_WPA2) {
1764 if (vap->iv_rsn_ie != NULL)
1765 frm = add_ie(frm, vap->iv_rsn_ie);
1766 /* XXX else complain? */
1768 frm = ieee80211_add_xrates(frm, rs);
1769 if (vap->iv_flags & IEEE80211_F_WPA1) {
1770 if (vap->iv_wpa_ie != NULL)
1771 frm = add_ie(frm, vap->iv_wpa_ie);
1772 /* XXX else complain? */
1774 if (vap->iv_appie_probereq != NULL)
1775 frm = add_appie(frm, vap->iv_appie_probereq);
1776 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1778 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1779 ("leading space %zd", M_LEADINGSPACE(m)));
1780 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1782 /* NB: cannot happen */
1783 ieee80211_free_node(ni);
1787 wh = mtod(m, struct ieee80211_frame *);
1788 ieee80211_send_setup(ni, m,
1789 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1790 IEEE80211_NONQOS_TID, sa, da, bssid);
1791 /* XXX power management? */
1792 m->m_flags |= M_ENCAP; /* mark encapsulated */
1794 M_WME_SETAC(m, WME_AC_BE);
1796 IEEE80211_NODE_STAT(ni, tx_probereq);
1797 IEEE80211_NODE_STAT(ni, tx_mgmt);
1799 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1800 "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
1801 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
1804 memset(¶ms, 0, sizeof(params));
1805 params.ibp_pri = M_WME_GETAC(m);
1806 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1807 params.ibp_rate0 = tp->mgmtrate;
1808 if (IEEE80211_IS_MULTICAST(da)) {
1809 params.ibp_flags |= IEEE80211_BPF_NOACK;
1810 params.ibp_try0 = 1;
1812 params.ibp_try0 = tp->maxretry;
1813 params.ibp_power = ni->ni_txpower;
1814 return ic->ic_raw_xmit(ni, m, ¶ms);
1818 * Calculate capability information for mgt frames.
1821 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1823 struct ieee80211com *ic = vap->iv_ic;
1826 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1828 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1829 capinfo = IEEE80211_CAPINFO_ESS;
1830 else if (vap->iv_opmode == IEEE80211_M_IBSS)
1831 capinfo = IEEE80211_CAPINFO_IBSS;
1834 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1835 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1836 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1837 IEEE80211_IS_CHAN_2GHZ(chan))
1838 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1839 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1840 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1841 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1842 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1847 * Send a management frame. The node is for the destination (or ic_bss
1848 * when in station mode). Nodes other than ic_bss have their reference
1849 * count bumped to reflect our use for an indeterminant time.
1852 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1854 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1855 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1856 struct ieee80211vap *vap = ni->ni_vap;
1857 struct ieee80211com *ic = ni->ni_ic;
1858 struct ieee80211_node *bss = vap->iv_bss;
1859 struct ieee80211_bpf_params params;
1863 int has_challenge, is_shared_key, ret, status;
1865 KASSERT(ni != NULL, ("null node"));
1868 * Hold a reference on the node so it doesn't go away until after
1869 * the xmit is complete all the way in the driver. On error we
1870 * will remove our reference.
1872 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1873 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
1875 ni, ether_sprintf(ni->ni_macaddr),
1876 ieee80211_node_refcnt(ni)+1);
1877 ieee80211_ref_node(ni);
1879 memset(¶ms, 0, sizeof(params));
1882 case IEEE80211_FC0_SUBTYPE_AUTH:
1885 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1886 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1887 ni->ni_challenge != NULL);
1890 * Deduce whether we're doing open authentication or
1891 * shared key authentication. We do the latter if
1892 * we're in the middle of a shared key authentication
1893 * handshake or if we're initiating an authentication
1894 * request and configured to use shared key.
1896 is_shared_key = has_challenge ||
1897 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1898 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1899 bss->ni_authmode == IEEE80211_AUTH_SHARED);
1901 m = ieee80211_getmgtframe(&frm,
1902 ic->ic_headroom + sizeof(struct ieee80211_frame),
1903 3 * sizeof(uint16_t)
1904 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1905 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1908 senderr(ENOMEM, is_tx_nobuf);
1910 ((uint16_t *)frm)[0] =
1911 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1912 : htole16(IEEE80211_AUTH_ALG_OPEN);
1913 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1914 ((uint16_t *)frm)[2] = htole16(status);/* status */
1916 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1917 ((uint16_t *)frm)[3] =
1918 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1919 IEEE80211_ELEMID_CHALLENGE);
1920 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1921 IEEE80211_CHALLENGE_LEN);
1922 m->m_pkthdr.len = m->m_len =
1923 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1924 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1925 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1926 "request encrypt frame (%s)", __func__);
1927 /* mark frame for encryption */
1928 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1931 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1933 /* XXX not right for shared key */
1934 if (status == IEEE80211_STATUS_SUCCESS)
1935 IEEE80211_NODE_STAT(ni, tx_auth);
1937 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1939 if (vap->iv_opmode == IEEE80211_M_STA)
1940 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1941 (void *) vap->iv_state);
1944 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1945 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1946 "send station deauthenticate (reason %d)", arg);
1947 m = ieee80211_getmgtframe(&frm,
1948 ic->ic_headroom + sizeof(struct ieee80211_frame),
1951 senderr(ENOMEM, is_tx_nobuf);
1952 *(uint16_t *)frm = htole16(arg); /* reason */
1953 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1955 IEEE80211_NODE_STAT(ni, tx_deauth);
1956 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1958 ieee80211_node_unauthorize(ni); /* port closed */
1961 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1962 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1964 * asreq frame format
1965 * [2] capability information
1966 * [2] listen interval
1967 * [6*] current AP address (reassoc only)
1969 * [tlv] supported rates
1970 * [tlv] extended supported rates
1971 * [4] power capability (optional)
1972 * [28] supported channels (optional)
1973 * [tlv] HT capabilities
1974 * [tlv] WME (optional)
1975 * [tlv] Vendor OUI HT capabilities (optional)
1976 * [tlv] Atheros capabilities (if negotiated)
1977 * [tlv] AppIE's (optional)
1979 m = ieee80211_getmgtframe(&frm,
1980 ic->ic_headroom + sizeof(struct ieee80211_frame),
1983 + IEEE80211_ADDR_LEN
1984 + 2 + IEEE80211_NWID_LEN
1985 + 2 + IEEE80211_RATE_SIZE
1986 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1989 + sizeof(struct ieee80211_wme_info)
1990 + sizeof(struct ieee80211_ie_htcap)
1991 + 4 + sizeof(struct ieee80211_ie_htcap)
1992 #ifdef IEEE80211_SUPPORT_SUPERG
1993 + sizeof(struct ieee80211_ath_ie)
1995 + (vap->iv_appie_wpa != NULL ?
1996 vap->iv_appie_wpa->ie_len : 0)
1997 + (vap->iv_appie_assocreq != NULL ?
1998 vap->iv_appie_assocreq->ie_len : 0)
2001 senderr(ENOMEM, is_tx_nobuf);
2003 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2004 ("wrong mode %u", vap->iv_opmode));
2005 capinfo = IEEE80211_CAPINFO_ESS;
2006 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2007 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2009 * NB: Some 11a AP's reject the request when
2010 * short premable is set.
2012 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2013 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2014 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2015 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2016 (ic->ic_caps & IEEE80211_C_SHSLOT))
2017 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2018 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2019 (vap->iv_flags & IEEE80211_F_DOTH))
2020 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2021 *(uint16_t *)frm = htole16(capinfo);
2024 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2025 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2029 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2030 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2031 frm += IEEE80211_ADDR_LEN;
2034 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2035 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2036 if (vap->iv_flags & IEEE80211_F_WPA2) {
2037 if (vap->iv_rsn_ie != NULL)
2038 frm = add_ie(frm, vap->iv_rsn_ie);
2039 /* XXX else complain? */
2041 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2042 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2043 frm = ieee80211_add_powercapability(frm,
2045 frm = ieee80211_add_supportedchannels(frm, ic);
2047 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2048 ni->ni_ies.htcap_ie != NULL &&
2049 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2050 frm = ieee80211_add_htcap(frm, ni);
2051 if (vap->iv_flags & IEEE80211_F_WPA1) {
2052 if (vap->iv_wpa_ie != NULL)
2053 frm = add_ie(frm, vap->iv_wpa_ie);
2054 /* XXX else complain */
2056 if ((ic->ic_flags & IEEE80211_F_WME) &&
2057 ni->ni_ies.wme_ie != NULL)
2058 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2059 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2060 ni->ni_ies.htcap_ie != NULL &&
2061 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2062 frm = ieee80211_add_htcap_vendor(frm, ni);
2063 #ifdef IEEE80211_SUPPORT_SUPERG
2064 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2065 frm = ieee80211_add_ath(frm,
2066 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2067 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2068 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2069 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2071 #endif /* IEEE80211_SUPPORT_SUPERG */
2072 if (vap->iv_appie_assocreq != NULL)
2073 frm = add_appie(frm, vap->iv_appie_assocreq);
2074 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2076 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2077 (void *) vap->iv_state);
2080 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2081 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2083 * asresp frame format
2084 * [2] capability information
2086 * [2] association ID
2087 * [tlv] supported rates
2088 * [tlv] extended supported rates
2089 * [tlv] HT capabilities (standard, if STA enabled)
2090 * [tlv] HT information (standard, if STA enabled)
2091 * [tlv] WME (if configured and STA enabled)
2092 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2093 * [tlv] HT information (vendor OUI, if STA enabled)
2094 * [tlv] Atheros capabilities (if STA enabled)
2095 * [tlv] AppIE's (optional)
2097 m = ieee80211_getmgtframe(&frm,
2098 ic->ic_headroom + sizeof(struct ieee80211_frame),
2102 + 2 + IEEE80211_RATE_SIZE
2103 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2104 + sizeof(struct ieee80211_ie_htcap) + 4
2105 + sizeof(struct ieee80211_ie_htinfo) + 4
2106 + sizeof(struct ieee80211_wme_param)
2107 #ifdef IEEE80211_SUPPORT_SUPERG
2108 + sizeof(struct ieee80211_ath_ie)
2110 + (vap->iv_appie_assocresp != NULL ?
2111 vap->iv_appie_assocresp->ie_len : 0)
2114 senderr(ENOMEM, is_tx_nobuf);
2116 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2117 *(uint16_t *)frm = htole16(capinfo);
2120 *(uint16_t *)frm = htole16(arg); /* status */
2123 if (arg == IEEE80211_STATUS_SUCCESS) {
2124 *(uint16_t *)frm = htole16(ni->ni_associd);
2125 IEEE80211_NODE_STAT(ni, tx_assoc);
2127 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2130 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2131 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2132 /* NB: respond according to what we received */
2133 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2134 frm = ieee80211_add_htcap(frm, ni);
2135 frm = ieee80211_add_htinfo(frm, ni);
2137 if ((vap->iv_flags & IEEE80211_F_WME) &&
2138 ni->ni_ies.wme_ie != NULL)
2139 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2140 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2141 frm = ieee80211_add_htcap_vendor(frm, ni);
2142 frm = ieee80211_add_htinfo_vendor(frm, ni);
2144 #ifdef IEEE80211_SUPPORT_SUPERG
2145 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2146 frm = ieee80211_add_ath(frm,
2147 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2148 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2149 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2150 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2151 #endif /* IEEE80211_SUPPORT_SUPERG */
2152 if (vap->iv_appie_assocresp != NULL)
2153 frm = add_appie(frm, vap->iv_appie_assocresp);
2154 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2157 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2158 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2159 "send station disassociate (reason %d)", arg);
2160 m = ieee80211_getmgtframe(&frm,
2161 ic->ic_headroom + sizeof(struct ieee80211_frame),
2164 senderr(ENOMEM, is_tx_nobuf);
2165 *(uint16_t *)frm = htole16(arg); /* reason */
2166 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2168 IEEE80211_NODE_STAT(ni, tx_disassoc);
2169 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2173 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2174 "invalid mgmt frame type %u", type);
2175 senderr(EINVAL, is_tx_unknownmgt);
2179 /* NB: force non-ProbeResp frames to the highest queue */
2180 params.ibp_pri = WME_AC_VO;
2181 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2182 /* NB: we know all frames are unicast */
2183 params.ibp_try0 = bss->ni_txparms->maxretry;
2184 params.ibp_power = bss->ni_txpower;
2185 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2187 ieee80211_free_node(ni);
2194 * Return an mbuf with a probe response frame in it.
2195 * Space is left to prepend and 802.11 header at the
2196 * front but it's left to the caller to fill in.
2199 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2201 struct ieee80211vap *vap = bss->ni_vap;
2202 struct ieee80211com *ic = bss->ni_ic;
2203 const struct ieee80211_rateset *rs;
2209 * probe response frame format
2211 * [2] beacon interval
2212 * [2] cabability information
2214 * [tlv] supported rates
2215 * [tlv] parameter set (FH/DS)
2216 * [tlv] parameter set (IBSS)
2217 * [tlv] country (optional)
2218 * [3] power control (optional)
2219 * [5] channel switch announcement (CSA) (optional)
2220 * [tlv] extended rate phy (ERP)
2221 * [tlv] extended supported rates
2222 * [tlv] RSN (optional)
2223 * [tlv] HT capabilities
2224 * [tlv] HT information
2225 * [tlv] WPA (optional)
2226 * [tlv] WME (optional)
2227 * [tlv] Vendor OUI HT capabilities (optional)
2228 * [tlv] Vendor OUI HT information (optional)
2229 * [tlv] Atheros capabilities
2230 * [tlv] AppIE's (optional)
2231 * [tlv] Mesh ID (MBSS)
2232 * [tlv] Mesh Conf (MBSS)
2234 m = ieee80211_getmgtframe(&frm,
2235 ic->ic_headroom + sizeof(struct ieee80211_frame),
2239 + 2 + IEEE80211_NWID_LEN
2240 + 2 + IEEE80211_RATE_SIZE
2242 + IEEE80211_COUNTRY_MAX_SIZE
2244 + sizeof(struct ieee80211_csa_ie)
2246 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2247 + sizeof(struct ieee80211_ie_wpa)
2248 + sizeof(struct ieee80211_ie_htcap)
2249 + sizeof(struct ieee80211_ie_htinfo)
2250 + sizeof(struct ieee80211_ie_wpa)
2251 + sizeof(struct ieee80211_wme_param)
2252 + 4 + sizeof(struct ieee80211_ie_htcap)
2253 + 4 + sizeof(struct ieee80211_ie_htinfo)
2254 #ifdef IEEE80211_SUPPORT_SUPERG
2255 + sizeof(struct ieee80211_ath_ie)
2257 #ifdef IEEE80211_SUPPORT_MESH
2258 + 2 + IEEE80211_MESHID_LEN
2259 + sizeof(struct ieee80211_meshconf_ie)
2261 + (vap->iv_appie_proberesp != NULL ?
2262 vap->iv_appie_proberesp->ie_len : 0)
2265 vap->iv_stats.is_tx_nobuf++;
2269 memset(frm, 0, 8); /* timestamp should be filled later */
2271 *(uint16_t *)frm = htole16(bss->ni_intval);
2273 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2274 *(uint16_t *)frm = htole16(capinfo);
2277 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2278 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2279 frm = ieee80211_add_rates(frm, rs);
2281 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2282 *frm++ = IEEE80211_ELEMID_FHPARMS;
2284 *frm++ = bss->ni_fhdwell & 0x00ff;
2285 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2286 *frm++ = IEEE80211_FH_CHANSET(
2287 ieee80211_chan2ieee(ic, bss->ni_chan));
2288 *frm++ = IEEE80211_FH_CHANPAT(
2289 ieee80211_chan2ieee(ic, bss->ni_chan));
2290 *frm++ = bss->ni_fhindex;
2292 *frm++ = IEEE80211_ELEMID_DSPARMS;
2294 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2297 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2298 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2300 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2302 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2303 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2304 frm = ieee80211_add_countryie(frm, ic);
2305 if (vap->iv_flags & IEEE80211_F_DOTH) {
2306 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2307 frm = ieee80211_add_powerconstraint(frm, vap);
2308 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2309 frm = ieee80211_add_csa(frm, vap);
2311 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2312 frm = ieee80211_add_erp(frm, ic);
2313 frm = ieee80211_add_xrates(frm, rs);
2314 if (vap->iv_flags & IEEE80211_F_WPA2) {
2315 if (vap->iv_rsn_ie != NULL)
2316 frm = add_ie(frm, vap->iv_rsn_ie);
2317 /* XXX else complain? */
2320 * NB: legacy 11b clients do not get certain ie's.
2321 * The caller identifies such clients by passing
2322 * a token in legacy to us. Could expand this to be
2323 * any legacy client for stuff like HT ie's.
2325 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2326 legacy != IEEE80211_SEND_LEGACY_11B) {
2327 frm = ieee80211_add_htcap(frm, bss);
2328 frm = ieee80211_add_htinfo(frm, bss);
2330 if (vap->iv_flags & IEEE80211_F_WPA1) {
2331 if (vap->iv_wpa_ie != NULL)
2332 frm = add_ie(frm, vap->iv_wpa_ie);
2333 /* XXX else complain? */
2335 if (vap->iv_flags & IEEE80211_F_WME)
2336 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2337 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2338 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2339 legacy != IEEE80211_SEND_LEGACY_11B) {
2340 frm = ieee80211_add_htcap_vendor(frm, bss);
2341 frm = ieee80211_add_htinfo_vendor(frm, bss);
2343 #ifdef IEEE80211_SUPPORT_SUPERG
2344 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2345 legacy != IEEE80211_SEND_LEGACY_11B)
2346 frm = ieee80211_add_athcaps(frm, bss);
2348 if (vap->iv_appie_proberesp != NULL)
2349 frm = add_appie(frm, vap->iv_appie_proberesp);
2350 #ifdef IEEE80211_SUPPORT_MESH
2351 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2352 frm = ieee80211_add_meshid(frm, vap);
2353 frm = ieee80211_add_meshconf(frm, vap);
2356 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2362 * Send a probe response frame to the specified mac address.
2363 * This does not go through the normal mgt frame api so we
2364 * can specify the destination address and re-use the bss node
2365 * for the sta reference.
2368 ieee80211_send_proberesp(struct ieee80211vap *vap,
2369 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2371 struct ieee80211_node *bss = vap->iv_bss;
2372 struct ieee80211com *ic = vap->iv_ic;
2373 struct ieee80211_frame *wh;
2376 if (vap->iv_state == IEEE80211_S_CAC) {
2377 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2378 "block %s frame in CAC state", "probe response");
2379 vap->iv_stats.is_tx_badstate++;
2380 return EIO; /* XXX */
2384 * Hold a reference on the node so it doesn't go away until after
2385 * the xmit is complete all the way in the driver. On error we
2386 * will remove our reference.
2388 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2389 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2390 __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
2391 ieee80211_node_refcnt(bss)+1);
2392 ieee80211_ref_node(bss);
2394 m = ieee80211_alloc_proberesp(bss, legacy);
2396 ieee80211_free_node(bss);
2400 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2401 KASSERT(m != NULL, ("no room for header"));
2403 wh = mtod(m, struct ieee80211_frame *);
2404 ieee80211_send_setup(bss, m,
2405 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2406 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2407 /* XXX power management? */
2408 m->m_flags |= M_ENCAP; /* mark encapsulated */
2410 M_WME_SETAC(m, WME_AC_BE);
2412 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2413 "send probe resp on channel %u to %s%s\n",
2414 ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
2415 legacy ? " <legacy>" : "");
2416 IEEE80211_NODE_STAT(bss, tx_mgmt);
2418 return ic->ic_raw_xmit(bss, m, NULL);
2422 * Allocate and build a RTS (Request To Send) control frame.
2425 ieee80211_alloc_rts(struct ieee80211com *ic,
2426 const uint8_t ra[IEEE80211_ADDR_LEN],
2427 const uint8_t ta[IEEE80211_ADDR_LEN],
2430 struct ieee80211_frame_rts *rts;
2433 /* XXX honor ic_headroom */
2434 m = m_gethdr(M_DONTWAIT, MT_DATA);
2436 rts = mtod(m, struct ieee80211_frame_rts *);
2437 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2438 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2439 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2440 *(u_int16_t *)rts->i_dur = htole16(dur);
2441 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2442 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2444 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2450 * Allocate and build a CTS (Clear To Send) control frame.
2453 ieee80211_alloc_cts(struct ieee80211com *ic,
2454 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2456 struct ieee80211_frame_cts *cts;
2459 /* XXX honor ic_headroom */
2460 m = m_gethdr(M_DONTWAIT, MT_DATA);
2462 cts = mtod(m, struct ieee80211_frame_cts *);
2463 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2464 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2465 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2466 *(u_int16_t *)cts->i_dur = htole16(dur);
2467 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2469 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2475 ieee80211_tx_mgt_timeout(void *arg)
2477 struct ieee80211_node *ni = arg;
2478 struct ieee80211vap *vap = ni->ni_vap;
2480 if (vap->iv_state != IEEE80211_S_INIT &&
2481 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2483 * NB: it's safe to specify a timeout as the reason here;
2484 * it'll only be used in the right state.
2486 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2487 IEEE80211_SCAN_FAIL_TIMEOUT);
2492 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2494 struct ieee80211vap *vap = ni->ni_vap;
2495 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2498 * Frame transmit completed; arrange timer callback. If
2499 * transmit was successfuly we wait for response. Otherwise
2500 * we arrange an immediate callback instead of doing the
2501 * callback directly since we don't know what state the driver
2502 * is in (e.g. what locks it is holding). This work should
2503 * not be too time-critical and not happen too often so the
2504 * added overhead is acceptable.
2506 * XXX what happens if !acked but response shows up before callback?
2508 if (vap->iv_state == ostate)
2509 callout_reset(&vap->iv_mgtsend,
2510 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2511 ieee80211_tx_mgt_timeout, ni);
2515 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2516 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2518 struct ieee80211vap *vap = ni->ni_vap;
2519 struct ieee80211com *ic = ni->ni_ic;
2520 struct ieee80211_rateset *rs = &ni->ni_rates;
2524 * beacon frame format
2526 * [2] beacon interval
2527 * [2] cabability information
2529 * [tlv] supported rates
2530 * [3] parameter set (DS)
2531 * [8] CF parameter set (optional)
2532 * [tlv] parameter set (IBSS/TIM)
2533 * [tlv] country (optional)
2534 * [3] power control (optional)
2535 * [5] channel switch announcement (CSA) (optional)
2536 * [tlv] extended rate phy (ERP)
2537 * [tlv] extended supported rates
2538 * [tlv] RSN parameters
2539 * [tlv] HT capabilities
2540 * [tlv] HT information
2541 * XXX Vendor-specific OIDs (e.g. Atheros)
2542 * [tlv] WPA parameters
2543 * [tlv] WME parameters
2544 * [tlv] Vendor OUI HT capabilities (optional)
2545 * [tlv] Vendor OUI HT information (optional)
2546 * [tlv] Atheros capabilities (optional)
2547 * [tlv] TDMA parameters (optional)
2548 * [tlv] Mesh ID (MBSS)
2549 * [tlv] Mesh Conf (MBSS)
2550 * [tlv] application data (optional)
2553 memset(bo, 0, sizeof(*bo));
2555 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2557 *(uint16_t *)frm = htole16(ni->ni_intval);
2559 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2560 bo->bo_caps = (uint16_t *)frm;
2561 *(uint16_t *)frm = htole16(capinfo);
2563 *frm++ = IEEE80211_ELEMID_SSID;
2564 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2565 *frm++ = ni->ni_esslen;
2566 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2567 frm += ni->ni_esslen;
2570 frm = ieee80211_add_rates(frm, rs);
2571 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2572 *frm++ = IEEE80211_ELEMID_DSPARMS;
2574 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2576 if (ic->ic_flags & IEEE80211_F_PCF) {
2578 frm = ieee80211_add_cfparms(frm, ic);
2581 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2582 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2584 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2586 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2587 vap->iv_opmode == IEEE80211_M_MBSS) {
2588 /* TIM IE is the same for Mesh and Hostap */
2589 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2591 tie->tim_ie = IEEE80211_ELEMID_TIM;
2592 tie->tim_len = 4; /* length */
2593 tie->tim_count = 0; /* DTIM count */
2594 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2595 tie->tim_bitctl = 0; /* bitmap control */
2596 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2597 frm += sizeof(struct ieee80211_tim_ie);
2600 bo->bo_tim_trailer = frm;
2601 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2602 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2603 frm = ieee80211_add_countryie(frm, ic);
2604 if (vap->iv_flags & IEEE80211_F_DOTH) {
2605 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2606 frm = ieee80211_add_powerconstraint(frm, vap);
2608 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2609 frm = ieee80211_add_csa(frm, vap);
2612 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2614 frm = ieee80211_add_erp(frm, ic);
2616 frm = ieee80211_add_xrates(frm, rs);
2617 if (vap->iv_flags & IEEE80211_F_WPA2) {
2618 if (vap->iv_rsn_ie != NULL)
2619 frm = add_ie(frm, vap->iv_rsn_ie);
2620 /* XXX else complain */
2622 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2623 frm = ieee80211_add_htcap(frm, ni);
2624 bo->bo_htinfo = frm;
2625 frm = ieee80211_add_htinfo(frm, ni);
2627 if (vap->iv_flags & IEEE80211_F_WPA1) {
2628 if (vap->iv_wpa_ie != NULL)
2629 frm = add_ie(frm, vap->iv_wpa_ie);
2630 /* XXX else complain */
2632 if (vap->iv_flags & IEEE80211_F_WME) {
2634 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2636 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2637 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2638 frm = ieee80211_add_htcap_vendor(frm, ni);
2639 frm = ieee80211_add_htinfo_vendor(frm, ni);
2641 #ifdef IEEE80211_SUPPORT_SUPERG
2642 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2644 frm = ieee80211_add_athcaps(frm, ni);
2647 #ifdef IEEE80211_SUPPORT_TDMA
2648 if (vap->iv_caps & IEEE80211_C_TDMA) {
2650 frm = ieee80211_add_tdma(frm, vap);
2653 if (vap->iv_appie_beacon != NULL) {
2655 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2656 frm = add_appie(frm, vap->iv_appie_beacon);
2658 #ifdef IEEE80211_SUPPORT_MESH
2659 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2660 frm = ieee80211_add_meshid(frm, vap);
2661 bo->bo_meshconf = frm;
2662 frm = ieee80211_add_meshconf(frm, vap);
2665 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2666 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2667 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2671 * Allocate a beacon frame and fillin the appropriate bits.
2674 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2675 struct ieee80211_beacon_offsets *bo)
2677 struct ieee80211vap *vap = ni->ni_vap;
2678 struct ieee80211com *ic = ni->ni_ic;
2679 struct ifnet *ifp = vap->iv_ifp;
2680 struct ieee80211_frame *wh;
2686 * beacon frame format
2688 * [2] beacon interval
2689 * [2] cabability information
2691 * [tlv] supported rates
2692 * [3] parameter set (DS)
2693 * [8] CF parameter set (optional)
2694 * [tlv] parameter set (IBSS/TIM)
2695 * [tlv] country (optional)
2696 * [3] power control (optional)
2697 * [5] channel switch announcement (CSA) (optional)
2698 * [tlv] extended rate phy (ERP)
2699 * [tlv] extended supported rates
2700 * [tlv] RSN parameters
2701 * [tlv] HT capabilities
2702 * [tlv] HT information
2703 * [tlv] Vendor OUI HT capabilities (optional)
2704 * [tlv] Vendor OUI HT information (optional)
2705 * XXX Vendor-specific OIDs (e.g. Atheros)
2706 * [tlv] WPA parameters
2707 * [tlv] WME parameters
2708 * [tlv] TDMA parameters (optional)
2709 * [tlv] Mesh ID (MBSS)
2710 * [tlv] Mesh Conf (MBSS)
2711 * [tlv] application data (optional)
2712 * NB: we allocate the max space required for the TIM bitmap.
2713 * XXX how big is this?
2715 pktlen = 8 /* time stamp */
2716 + sizeof(uint16_t) /* beacon interval */
2717 + sizeof(uint16_t) /* capabilities */
2718 + 2 + ni->ni_esslen /* ssid */
2719 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2720 + 2 + 1 /* DS parameters */
2721 + 2 + 6 /* CF parameters */
2722 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2723 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2724 + 2 + 1 /* power control */
2725 + sizeof(struct ieee80211_csa_ie) /* CSA */
2727 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2728 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2729 2*sizeof(struct ieee80211_ie_wpa) : 0)
2730 /* XXX conditional? */
2731 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2732 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2733 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2734 sizeof(struct ieee80211_wme_param) : 0)
2735 #ifdef IEEE80211_SUPPORT_SUPERG
2736 + sizeof(struct ieee80211_ath_ie) /* ATH */
2738 #ifdef IEEE80211_SUPPORT_TDMA
2739 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2740 sizeof(struct ieee80211_tdma_param) : 0)
2742 #ifdef IEEE80211_SUPPORT_MESH
2743 + 2 + ni->ni_meshidlen
2744 + sizeof(struct ieee80211_meshconf_ie)
2746 + IEEE80211_MAX_APPIE
2748 m = ieee80211_getmgtframe(&frm,
2749 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2751 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2752 "%s: cannot get buf; size %u\n", __func__, pktlen);
2753 vap->iv_stats.is_tx_nobuf++;
2756 ieee80211_beacon_construct(m, frm, bo, ni);
2758 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2759 KASSERT(m != NULL, ("no space for 802.11 header?"));
2760 wh = mtod(m, struct ieee80211_frame *);
2761 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2762 IEEE80211_FC0_SUBTYPE_BEACON;
2763 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2764 *(uint16_t *)wh->i_dur = 0;
2765 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2766 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2767 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2768 *(uint16_t *)wh->i_seq = 0;
2774 * Update the dynamic parts of a beacon frame based on the current state.
2777 ieee80211_beacon_update(struct ieee80211_node *ni,
2778 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2780 struct ieee80211vap *vap = ni->ni_vap;
2781 struct ieee80211com *ic = ni->ni_ic;
2782 int len_changed = 0;
2787 * Handle 11h channel change when we've reached the count.
2788 * We must recalculate the beacon frame contents to account
2789 * for the new channel. Note we do this only for the first
2790 * vap that reaches this point; subsequent vaps just update
2791 * their beacon state to reflect the recalculated channel.
2793 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2794 vap->iv_csa_count == ic->ic_csa_count) {
2795 vap->iv_csa_count = 0;
2797 * Effect channel change before reconstructing the beacon
2798 * frame contents as many places reference ni_chan.
2800 if (ic->ic_csa_newchan != NULL)
2801 ieee80211_csa_completeswitch(ic);
2803 * NB: ieee80211_beacon_construct clears all pending
2804 * updates in bo_flags so we don't need to explicitly
2805 * clear IEEE80211_BEACON_CSA.
2807 ieee80211_beacon_construct(m,
2808 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2810 /* XXX do WME aggressive mode processing? */
2811 IEEE80211_UNLOCK(ic);
2812 return 1; /* just assume length changed */
2815 /* XXX faster to recalculate entirely or just changes? */
2816 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2817 *bo->bo_caps = htole16(capinfo);
2819 if (vap->iv_flags & IEEE80211_F_WME) {
2820 struct ieee80211_wme_state *wme = &ic->ic_wme;
2823 * Check for agressive mode change. When there is
2824 * significant high priority traffic in the BSS
2825 * throttle back BE traffic by using conservative
2826 * parameters. Otherwise BE uses agressive params
2827 * to optimize performance of legacy/non-QoS traffic.
2829 if (wme->wme_flags & WME_F_AGGRMODE) {
2830 if (wme->wme_hipri_traffic >
2831 wme->wme_hipri_switch_thresh) {
2832 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2833 "%s: traffic %u, disable aggressive mode\n",
2834 __func__, wme->wme_hipri_traffic);
2835 wme->wme_flags &= ~WME_F_AGGRMODE;
2836 ieee80211_wme_updateparams_locked(vap);
2837 wme->wme_hipri_traffic =
2838 wme->wme_hipri_switch_hysteresis;
2840 wme->wme_hipri_traffic = 0;
2842 if (wme->wme_hipri_traffic <=
2843 wme->wme_hipri_switch_thresh) {
2844 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2845 "%s: traffic %u, enable aggressive mode\n",
2846 __func__, wme->wme_hipri_traffic);
2847 wme->wme_flags |= WME_F_AGGRMODE;
2848 ieee80211_wme_updateparams_locked(vap);
2849 wme->wme_hipri_traffic = 0;
2851 wme->wme_hipri_traffic =
2852 wme->wme_hipri_switch_hysteresis;
2854 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2855 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
2856 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2860 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
2861 ieee80211_ht_update_beacon(vap, bo);
2862 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2864 #ifdef IEEE80211_SUPPORT_TDMA
2865 if (vap->iv_caps & IEEE80211_C_TDMA) {
2867 * NB: the beacon is potentially updated every TBTT.
2869 ieee80211_tdma_update_beacon(vap, bo);
2872 #ifdef IEEE80211_SUPPORT_MESH
2873 if (vap->iv_opmode == IEEE80211_M_MBSS)
2874 ieee80211_mesh_update_beacon(vap, bo);
2877 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2878 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
2879 struct ieee80211_tim_ie *tie =
2880 (struct ieee80211_tim_ie *) bo->bo_tim;
2881 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2882 u_int timlen, timoff, i;
2884 * ATIM/DTIM needs updating. If it fits in the
2885 * current space allocated then just copy in the
2886 * new bits. Otherwise we need to move any trailing
2887 * data to make room. Note that we know there is
2888 * contiguous space because ieee80211_beacon_allocate
2889 * insures there is space in the mbuf to write a
2890 * maximal-size virtual bitmap (based on iv_max_aid).
2893 * Calculate the bitmap size and offset, copy any
2894 * trailer out of the way, and then copy in the
2895 * new bitmap and update the information element.
2896 * Note that the tim bitmap must contain at least
2897 * one byte and any offset must be even.
2899 if (vap->iv_ps_pending != 0) {
2900 timoff = 128; /* impossibly large */
2901 for (i = 0; i < vap->iv_tim_len; i++)
2902 if (vap->iv_tim_bitmap[i]) {
2906 KASSERT(timoff != 128, ("tim bitmap empty!"));
2907 for (i = vap->iv_tim_len-1; i >= timoff; i--)
2908 if (vap->iv_tim_bitmap[i])
2910 timlen = 1 + (i - timoff);
2915 if (timlen != bo->bo_tim_len) {
2916 /* copy up/down trailer */
2917 int adjust = tie->tim_bitmap+timlen
2918 - bo->bo_tim_trailer;
2919 ovbcopy(bo->bo_tim_trailer,
2920 bo->bo_tim_trailer+adjust,
2921 bo->bo_tim_trailer_len);
2922 bo->bo_tim_trailer += adjust;
2923 bo->bo_erp += adjust;
2924 bo->bo_htinfo += adjust;
2925 #ifdef IEEE80211_SUPERG_SUPPORT
2926 bo->bo_ath += adjust;
2928 #ifdef IEEE80211_TDMA_SUPPORT
2929 bo->bo_tdma += adjust;
2931 #ifdef IEEE80211_MESH_SUPPORT
2932 bo->bo_meshconf += adjust;
2934 bo->bo_appie += adjust;
2935 bo->bo_wme += adjust;
2936 bo->bo_csa += adjust;
2937 bo->bo_tim_len = timlen;
2939 /* update information element */
2940 tie->tim_len = 3 + timlen;
2941 tie->tim_bitctl = timoff;
2944 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2947 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2949 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2950 "%s: TIM updated, pending %u, off %u, len %u\n",
2951 __func__, vap->iv_ps_pending, timoff, timlen);
2953 /* count down DTIM period */
2954 if (tie->tim_count == 0)
2955 tie->tim_count = tie->tim_period - 1;
2958 /* update state for buffered multicast frames on DTIM */
2959 if (mcast && tie->tim_count == 0)
2960 tie->tim_bitctl |= 1;
2962 tie->tim_bitctl &= ~1;
2963 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2964 struct ieee80211_csa_ie *csa =
2965 (struct ieee80211_csa_ie *) bo->bo_csa;
2968 * Insert or update CSA ie. If we're just starting
2969 * to count down to the channel switch then we need
2970 * to insert the CSA ie. Otherwise we just need to
2971 * drop the count. The actual change happens above
2972 * when the vap's count reaches the target count.
2974 if (vap->iv_csa_count == 0) {
2975 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2976 bo->bo_erp += sizeof(*csa);
2977 bo->bo_htinfo += sizeof(*csa);
2978 bo->bo_wme += sizeof(*csa);
2979 #ifdef IEEE80211_SUPERG_SUPPORT
2980 bo->bo_ath += sizeof(*csa);
2982 #ifdef IEEE80211_TDMA_SUPPORT
2983 bo->bo_tdma += sizeof(*csa);
2985 #ifdef IEEE80211_MESH_SUPPORT
2986 bo->bo_meshconf += sizeof(*csa);
2988 bo->bo_appie += sizeof(*csa);
2989 bo->bo_csa_trailer_len += sizeof(*csa);
2990 bo->bo_tim_trailer_len += sizeof(*csa);
2991 m->m_len += sizeof(*csa);
2992 m->m_pkthdr.len += sizeof(*csa);
2994 ieee80211_add_csa(bo->bo_csa, vap);
2997 vap->iv_csa_count++;
2998 /* NB: don't clear IEEE80211_BEACON_CSA */
3000 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3002 * ERP element needs updating.
3004 (void) ieee80211_add_erp(bo->bo_erp, ic);
3005 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3007 #ifdef IEEE80211_SUPPORT_SUPERG
3008 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3009 ieee80211_add_athcaps(bo->bo_ath, ni);
3010 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3014 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3015 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3021 aielen += aie->ie_len;
3022 if (aielen != bo->bo_appie_len) {
3023 /* copy up/down trailer */
3024 int adjust = aielen - bo->bo_appie_len;
3025 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3026 bo->bo_tim_trailer_len);
3027 bo->bo_tim_trailer += adjust;
3028 bo->bo_appie += adjust;
3029 bo->bo_appie_len = aielen;
3035 frm = add_appie(frm, aie);
3036 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3038 IEEE80211_UNLOCK(ic);