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.
26 * $FreeBSD: head/sys/net80211/ieee80211_output.c 198384 2009-10-23 11:13:08Z rpaulo $
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>
44 #include <net/route.h>
46 #include <net/if_llc.h>
47 #include <net/if_media.h>
48 #include <net/ifq_var.h>
50 #include <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_regdomain.h>
52 #ifdef IEEE80211_SUPPORT_SUPERG
53 #include <netproto/802_11/ieee80211_superg.h>
55 #ifdef IEEE80211_SUPPORT_TDMA
56 #include <netproto/802_11/ieee80211_tdma.h>
58 #include <netproto/802_11/ieee80211_wds.h>
59 #include <netproto/802_11/ieee80211_mesh.h>
62 #include <netinet/in.h>
63 #include <netinet/if_ether.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
71 #define ETHER_HEADER_COPY(dst, src) \
72 memcpy(dst, src, sizeof(struct ether_header))
74 /* unalligned little endian access */
75 #define LE_WRITE_2(p, v) do { \
76 ((uint8_t *)(p))[0] = (v) & 0xff; \
77 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
79 #define LE_WRITE_4(p, v) do { \
80 ((uint8_t *)(p))[0] = (v) & 0xff; \
81 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
82 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
83 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
86 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
87 u_int hdrsize, u_int ciphdrsize, u_int mtu);
88 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
90 #ifdef IEEE80211_DEBUG
92 * Decide if an outbound management frame should be
93 * printed when debugging is enabled. This filters some
94 * of the less interesting frames that come frequently
98 doprint(struct ieee80211vap *vap, int subtype)
101 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
102 return (vap->iv_opmode == IEEE80211_M_IBSS);
109 * Start method for vap's. All packets from the stack come
110 * through here. We handle common processing of the packets
111 * before dispatching them to the underlying device.
114 ieee80211_start(struct ifnet *ifp)
116 #define IS_DWDS(vap) \
117 (vap->iv_opmode == IEEE80211_M_WDS && \
118 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
119 struct ieee80211vap *vap = ifp->if_softc;
120 struct ieee80211com *ic = vap->iv_ic;
121 struct ifnet *parent = ic->ic_ifp;
122 struct ieee80211_node *ni;
123 struct mbuf *m = NULL;
124 struct ether_header *eh;
127 /* NB: parent must be up and running */
128 if (!IFNET_IS_UP_RUNNING(parent)) {
129 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
130 "%s: ignore queue, parent %s not up+running\n",
131 __func__, parent->if_xname);
133 ifq_purge(&ifp->if_snd);
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);
141 ifq_purge(&ifp->if_snd);
145 * No data frames go out unless we're running.
146 * Note in particular this covers CAC and CSA
147 * states (though maybe we should check muting
150 if (vap->iv_state != IEEE80211_S_RUN) {
152 /* re-check under the com lock to avoid races */
153 if (vap->iv_state != IEEE80211_S_RUN) {
154 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
155 "%s: ignore queue, in %s state\n",
156 __func__, ieee80211_state_name[vap->iv_state]);
157 vap->iv_stats.is_tx_badstate++;
158 ifp->if_flags |= IFF_OACTIVE;
159 IEEE80211_UNLOCK(ic);
162 IEEE80211_UNLOCK(ic);
165 m = ifq_dequeue(&ifp->if_snd, NULL);
169 * Sanitize mbuf flags for net80211 use. We cannot
170 * clear M_PWR_SAV or M_MORE_DATA because these may
171 * be set for frames that are re-submitted from the
174 * NB: This must be done before ieee80211_classify as
175 * it marks EAPOL in frames with M_EAPOL.
177 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
179 * Cancel any background scan.
181 if (ic->ic_flags & IEEE80211_F_SCAN)
182 ieee80211_cancel_anyscan(vap);
184 * Find the node for the destination so we can do
185 * things like power save and fast frames aggregation.
187 * NB: past this point various code assumes the first
188 * mbuf has the 802.3 header present (and contiguous).
191 if (m->m_len < sizeof(struct ether_header) &&
192 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
193 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
194 "discard frame, %s\n", "m_pullup failed");
195 vap->iv_stats.is_tx_nobuf++; /* XXX */
199 eh = mtod(m, struct ether_header *);
200 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
203 * Only unicast frames from the above go out
204 * DWDS vaps; multicast frames are handled by
205 * dispatching the frame as it comes through
206 * the AP vap (see below).
208 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
209 eh->ether_dhost, "mcast", "%s", "on DWDS");
210 vap->iv_stats.is_dwds_mcast++;
214 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
216 * Spam DWDS vap's w/ multicast traffic.
218 /* XXX only if dwds in use? */
219 ieee80211_dwds_mcast(vap, m);
222 #ifdef IEEE80211_SUPPORT_MESH
223 if (vap->iv_opmode != IEEE80211_M_MBSS) {
225 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
227 /* NB: ieee80211_find_txnode does stat+msg */
232 if (ni->ni_associd == 0 &&
233 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
234 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
235 eh->ether_dhost, NULL,
236 "sta not associated (type 0x%04x)",
237 htons(eh->ether_type));
238 vap->iv_stats.is_tx_notassoc++;
241 ieee80211_free_node(ni);
244 #ifdef IEEE80211_SUPPORT_MESH
246 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
248 * Proxy station only if configured.
250 if (!ieee80211_mesh_isproxyena(vap)) {
251 IEEE80211_DISCARD_MAC(vap,
252 IEEE80211_MSG_OUTPUT |
254 eh->ether_dhost, NULL,
255 "%s", "proxy not enabled");
256 vap->iv_stats.is_mesh_notproxy++;
261 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
263 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
266 * NB: ieee80211_mesh_discover holds/disposes
267 * frame (e.g. queueing on path discovery).
274 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
275 (m->m_flags & M_PWR_SAV) == 0) {
277 * Station in power save mode; pass the frame
278 * to the 802.11 layer and continue. We'll get
279 * the frame back when the time is right.
280 * XXX lose WDS vap linkage?
282 (void) ieee80211_pwrsave(ni, m);
283 ieee80211_free_node(ni);
286 /* calculate priority so drivers can find the tx queue */
287 if (ieee80211_classify(ni, m)) {
288 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
289 eh->ether_dhost, NULL,
290 "%s", "classification failure");
291 vap->iv_stats.is_tx_classify++;
294 ieee80211_free_node(ni);
298 * Stash the node pointer. Note that we do this after
299 * any call to ieee80211_dwds_mcast because that code
300 * uses any existing value for rcvif to identify the
301 * interface it (might have been) received on.
303 m->m_pkthdr.rcvif = (void *)ni;
305 BPF_MTAP(ifp, m); /* 802.3 tx */
308 * Check if A-MPDU tx aggregation is setup or if we
309 * should try to enable it. The sta must be associated
310 * with HT and A-MPDU enabled for use. When the policy
311 * routine decides we should enable A-MPDU we issue an
312 * ADDBA request and wait for a reply. The frame being
313 * encapsulated will go out w/o using A-MPDU, or possibly
314 * it might be collected by the driver and held/retransmit.
315 * The default ic_ampdu_enable routine handles staggering
316 * ADDBA requests in case the receiver NAK's us or we are
317 * otherwise unable to establish a BA stream.
319 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
320 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
321 (m->m_flags & M_EAPOL) == 0) {
322 const int ac = M_WME_GETAC(m);
323 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
325 ieee80211_txampdu_count_packet(tap);
326 if (IEEE80211_AMPDU_RUNNING(tap)) {
328 * Operational, mark frame for aggregation.
330 * XXX do tx aggregation here
332 m->m_flags |= M_AMPDU_MPDU;
333 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
334 ic->ic_ampdu_enable(ni, tap)) {
336 * Not negotiated yet, request service.
338 ieee80211_ampdu_request(ni, tap);
339 /* XXX hold frame for reply? */
342 #ifdef IEEE80211_SUPPORT_SUPERG
343 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
344 m = ieee80211_ff_check(ni, m);
346 /* NB: any ni ref held on stageq */
350 #endif /* IEEE80211_SUPPORT_SUPERG */
351 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
353 * Encapsulate the packet in prep for transmission.
355 m = ieee80211_encap(vap, ni, m);
357 /* NB: stat+msg handled in ieee80211_encap */
358 ieee80211_free_node(ni);
363 error = ieee80211_handoff(parent, m);
365 /* NB: IFQ_HANDOFF reclaims mbuf */
366 ieee80211_free_node(ni);
370 ic->ic_lastdata = ticks;
377 * 802.11 output routine. This is (currently) used only to
378 * connect bpf write calls to the 802.11 layer for injecting
382 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
383 struct sockaddr *dst, struct rtentry *rt)
385 #define senderr(e) do { error = (e); goto bad;} while (0)
386 struct ieee80211_node *ni = NULL;
387 struct ieee80211vap *vap;
388 struct ieee80211_frame *wh;
391 if (ifp->if_flags & IFF_OACTIVE) {
393 * Short-circuit requests if the vap is marked OACTIVE
394 * as this can happen because a packet came down through
395 * ieee80211_start before the vap entered RUN state in
396 * which case it's ok to just drop the frame. This
397 * should not be necessary but callers of if_output don't
404 * Hand to the 802.3 code if not tagged as
405 * a raw 802.11 frame.
407 if (dst->sa_family != AF_IEEE80211)
408 return vap->iv_output(ifp, m, dst, rt);
410 error = mac_ifnet_check_transmit(ifp, m);
414 if (ifp->if_flags & IFF_MONITOR)
416 if (!IFNET_IS_UP_RUNNING(ifp))
418 if (vap->iv_state == IEEE80211_S_CAC) {
419 IEEE80211_DPRINTF(vap,
420 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
421 "block %s frame in CAC state\n", "raw data");
422 vap->iv_stats.is_tx_badstate++;
423 senderr(EIO); /* XXX */
425 /* XXX bypass bridge, pfil, carp, etc. */
427 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
428 senderr(EIO); /* XXX */
429 wh = mtod(m, struct ieee80211_frame *);
430 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
431 IEEE80211_FC0_VERSION_0)
432 senderr(EIO); /* XXX */
434 /* locate destination node */
435 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
436 case IEEE80211_FC1_DIR_NODS:
437 case IEEE80211_FC1_DIR_FROMDS:
438 ni = ieee80211_find_txnode(vap, wh->i_addr1);
440 case IEEE80211_FC1_DIR_TODS:
441 case IEEE80211_FC1_DIR_DSTODS:
442 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
443 senderr(EIO); /* XXX */
444 ni = ieee80211_find_txnode(vap, wh->i_addr3);
447 senderr(EIO); /* XXX */
451 * Permit packets w/ bpf params through regardless
452 * (see below about sa_len).
454 if (dst->sa_len == 0)
455 senderr(EHOSTUNREACH);
456 ni = ieee80211_ref_node(vap->iv_bss);
460 * Sanitize mbuf for net80211 flags leaked from above.
462 * NB: This must be done before ieee80211_classify as
463 * it marks EAPOL in frames with M_EAPOL.
465 m->m_flags &= ~M_80211_TX;
467 /* calculate priority so drivers can find the tx queue */
468 /* XXX assumes an 802.3 frame */
469 if (ieee80211_classify(ni, m))
470 senderr(EIO); /* XXX */
473 IEEE80211_NODE_STAT(ni, tx_data);
474 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
475 IEEE80211_NODE_STAT(ni, tx_mcast);
476 m->m_flags |= M_MCAST;
478 IEEE80211_NODE_STAT(ni, tx_ucast);
479 /* NB: ieee80211_encap does not include 802.11 header */
480 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
483 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
484 * present by setting the sa_len field of the sockaddr (yes,
486 * NB: we assume sa_data is suitably aligned to cast.
488 return vap->iv_ic->ic_raw_xmit(ni, m,
489 (const struct ieee80211_bpf_params *)(dst->sa_len ?
490 dst->sa_data : NULL));
495 ieee80211_free_node(ni);
502 * Set the direction field and address fields of an outgoing
503 * frame. Note this should be called early on in constructing
504 * a frame as it sets i_fc[1]; other bits can then be or'd in.
507 ieee80211_send_setup(
508 struct ieee80211_node *ni,
511 const uint8_t sa[IEEE80211_ADDR_LEN],
512 const uint8_t da[IEEE80211_ADDR_LEN],
513 const uint8_t bssid[IEEE80211_ADDR_LEN])
515 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
516 struct ieee80211vap *vap = ni->ni_vap;
517 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
520 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
521 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
522 switch (vap->iv_opmode) {
523 case IEEE80211_M_STA:
524 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
525 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
526 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
527 IEEE80211_ADDR_COPY(wh->i_addr3, da);
529 case IEEE80211_M_IBSS:
530 case IEEE80211_M_AHDEMO:
531 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
532 IEEE80211_ADDR_COPY(wh->i_addr1, da);
533 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
534 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
536 case IEEE80211_M_HOSTAP:
537 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
538 IEEE80211_ADDR_COPY(wh->i_addr1, da);
539 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
540 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
542 case IEEE80211_M_WDS:
543 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
544 IEEE80211_ADDR_COPY(wh->i_addr1, da);
545 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
546 IEEE80211_ADDR_COPY(wh->i_addr3, da);
547 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
549 case IEEE80211_M_MBSS:
550 #ifdef IEEE80211_SUPPORT_MESH
551 /* XXX add support for proxied addresses */
552 if (IEEE80211_IS_MULTICAST(da)) {
553 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
555 IEEE80211_ADDR_COPY(wh->i_addr1, da);
556 IEEE80211_ADDR_COPY(wh->i_addr2,
559 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
560 IEEE80211_ADDR_COPY(wh->i_addr1, da);
561 IEEE80211_ADDR_COPY(wh->i_addr2,
563 IEEE80211_ADDR_COPY(wh->i_addr3, da);
564 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
568 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
572 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
573 IEEE80211_ADDR_COPY(wh->i_addr1, da);
574 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
575 #ifdef IEEE80211_SUPPORT_MESH
576 if (vap->iv_opmode == IEEE80211_M_MBSS)
577 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
580 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
582 *(uint16_t *)&wh->i_dur[0] = 0;
584 seqno = ni->ni_txseqs[tid]++;
585 *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
586 M_SEQNO_SET(m, seqno);
588 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
589 m->m_flags |= M_MCAST;
594 * Send a management frame to the specified node. The node pointer
595 * must have a reference as the pointer will be passed to the driver
596 * and potentially held for a long time. If the frame is successfully
597 * dispatched to the driver, then it is responsible for freeing the
598 * reference (and potentially free'ing up any associated storage);
599 * otherwise deal with reclaiming any reference (on error).
602 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
603 struct ieee80211_bpf_params *params)
605 struct ieee80211vap *vap = ni->ni_vap;
606 struct ieee80211com *ic = ni->ni_ic;
607 struct ieee80211_frame *wh;
609 KASSERT(ni != NULL, ("null node"));
611 if (vap->iv_state == IEEE80211_S_CAC) {
612 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
613 ni, "block %s frame in CAC state",
614 ieee80211_mgt_subtype_name[
615 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
616 IEEE80211_FC0_SUBTYPE_SHIFT]);
617 vap->iv_stats.is_tx_badstate++;
618 ieee80211_free_node(ni);
620 return EIO; /* XXX */
623 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
625 ieee80211_free_node(ni);
629 wh = mtod(m, struct ieee80211_frame *);
630 ieee80211_send_setup(ni, m,
631 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
632 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
633 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
634 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
635 "encrypting frame (%s)", __func__);
636 wh->i_fc[1] |= IEEE80211_FC1_WEP;
638 m->m_flags |= M_ENCAP; /* mark encapsulated */
640 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
641 M_WME_SETAC(m, params->ibp_pri);
643 #ifdef IEEE80211_DEBUG
644 /* avoid printing too many frames */
645 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
646 ieee80211_msg_dumppkts(vap)) {
647 kprintf("[%6D] send %s on channel %u\n",
649 ieee80211_mgt_subtype_name[
650 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
651 IEEE80211_FC0_SUBTYPE_SHIFT],
652 ieee80211_chan2ieee(ic, ic->ic_curchan));
655 IEEE80211_NODE_STAT(ni, tx_mgmt);
657 return ic->ic_raw_xmit(ni, m, params);
661 * Send a null data frame to the specified node. If the station
662 * is setup for QoS then a QoS Null Data frame is constructed.
663 * If this is a WDS station then a 4-address frame is constructed.
665 * NB: the caller is assumed to have setup a node reference
666 * for use; this is necessary to deal with a race condition
667 * when probing for inactive stations. Like ieee80211_mgmt_output
668 * we must cleanup any node reference on error; however we
669 * can safely just unref it as we know it will never be the
670 * last reference to the node.
673 ieee80211_send_nulldata(struct ieee80211_node *ni)
675 struct ieee80211vap *vap = ni->ni_vap;
676 struct ieee80211com *ic = ni->ni_ic;
678 struct ieee80211_frame *wh;
682 if (vap->iv_state == IEEE80211_S_CAC) {
683 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
684 ni, "block %s frame in CAC state", "null data");
685 ieee80211_unref_node(&ni);
686 vap->iv_stats.is_tx_badstate++;
687 return EIO; /* XXX */
690 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
691 hdrlen = sizeof(struct ieee80211_qosframe);
693 hdrlen = sizeof(struct ieee80211_frame);
694 /* NB: only WDS vap's get 4-address frames */
695 if (vap->iv_opmode == IEEE80211_M_WDS)
696 hdrlen += IEEE80211_ADDR_LEN;
697 if (ic->ic_flags & IEEE80211_F_DATAPAD)
698 hdrlen = roundup(hdrlen, sizeof(uint32_t));
700 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
703 ieee80211_unref_node(&ni);
704 vap->iv_stats.is_tx_nobuf++;
707 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
708 ("leading space %zd", M_LEADINGSPACE(m)));
709 M_PREPEND(m, hdrlen, MB_DONTWAIT);
711 /* NB: cannot happen */
712 ieee80211_free_node(ni);
716 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
717 if (ni->ni_flags & IEEE80211_NODE_QOS) {
718 const int tid = WME_AC_TO_TID(WME_AC_BE);
721 ieee80211_send_setup(ni, m,
722 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
723 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
725 if (vap->iv_opmode == IEEE80211_M_WDS)
726 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
728 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
729 qos[0] = tid & IEEE80211_QOS_TID;
730 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
731 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
734 ieee80211_send_setup(ni, m,
735 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
736 IEEE80211_NONQOS_TID,
737 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
739 if (vap->iv_opmode != IEEE80211_M_WDS) {
740 /* NB: power management bit is never sent by an AP */
741 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
742 vap->iv_opmode != IEEE80211_M_HOSTAP)
743 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
745 m->m_len = m->m_pkthdr.len = hdrlen;
746 m->m_flags |= M_ENCAP; /* mark encapsulated */
748 M_WME_SETAC(m, WME_AC_BE);
750 IEEE80211_NODE_STAT(ni, tx_data);
752 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
753 "send %snull data frame on channel %u, pwr mgt %s",
754 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
755 ieee80211_chan2ieee(ic, ic->ic_curchan),
756 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
758 return ic->ic_raw_xmit(ni, m, NULL);
762 * Assign priority to a frame based on any vlan tag assigned
763 * to the station and/or any Diffserv setting in an IP header.
764 * Finally, if an ACM policy is setup (in station mode) it's
768 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
770 const struct ether_header *eh = mtod(m, struct ether_header *);
771 int v_wme_ac, d_wme_ac, ac;
774 * Always promote PAE/EAPOL frames to high priority.
776 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
777 /* NB: mark so others don't need to check header */
778 m->m_flags |= M_EAPOL;
783 * Non-qos traffic goes to BE.
785 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
791 * If node has a vlan tag then all traffic
792 * to it must have a matching tag.
795 if (ni->ni_vlan != 0) {
796 if ((m->m_flags & M_VLANTAG) == 0) {
797 IEEE80211_NODE_STAT(ni, tx_novlantag);
801 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
802 EVL_VLANOFTAG(ni->ni_vlan)) {
803 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
806 /* map vlan priority to AC */
807 v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
811 /* XXX m_copydata may be too slow for fast path */
813 if (eh->ether_type == htons(ETHERTYPE_IP)) {
816 * IP frame, map the DSCP bits from the TOS field.
818 /* NB: ip header may not be in first mbuf */
819 m_copydata(m, sizeof(struct ether_header) +
820 offsetof(struct ip, ip_tos), sizeof(tos), &tos);
821 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
822 d_wme_ac = TID_TO_WME_AC(tos);
826 if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
830 * IPv6 frame, map the DSCP bits from the TOS field.
832 m_copydata(m, sizeof(struct ether_header) +
833 offsetof(struct ip6_hdr, ip6_flow), sizeof(flow),
835 tos = (uint8_t)(ntohl(flow) >> 20);
836 tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
837 d_wme_ac = TID_TO_WME_AC(tos);
840 d_wme_ac = WME_AC_BE;
848 * Use highest priority AC.
850 if (v_wme_ac > d_wme_ac)
858 if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
859 static const int acmap[4] = {
860 WME_AC_BK, /* WME_AC_BE */
861 WME_AC_BK, /* WME_AC_BK */
862 WME_AC_BE, /* WME_AC_VI */
863 WME_AC_VI, /* WME_AC_VO */
865 struct ieee80211com *ic = ni->ni_ic;
867 while (ac != WME_AC_BK &&
868 ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
877 * Insure there is sufficient contiguous space to encapsulate the
878 * 802.11 data frame. If room isn't already there, arrange for it.
879 * Drivers and cipher modules assume we have done the necessary work
880 * and fail rudely if they don't find the space they need.
883 ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
884 struct ieee80211_key *key, struct mbuf *m)
886 #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
887 struct mbuf *mnew = NULL;
888 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
891 /* XXX belongs in crypto code? */
892 needed_space += key->wk_cipher->ic_header;
895 * When crypto is being done in the host we must insure
896 * the data are writable for the cipher routines; clone
897 * a writable mbuf chain.
898 * XXX handle SWMIC specially
900 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
901 mnew = m_dup(m, MB_DONTWAIT);
903 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
904 "%s: cannot get writable mbuf\n", __func__);
905 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
913 * We know we are called just before stripping an Ethernet
914 * header and prepending an LLC header. This means we know
916 * sizeof(struct ether_header) - sizeof(struct llc)
917 * bytes recovered to which we need additional space for the
918 * 802.11 header and any crypto header.
920 /* XXX check trailing space and copy instead? */
921 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
922 struct mbuf *n = m_gethdr(MB_DONTWAIT, m->m_type);
924 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
925 "%s: cannot expand storage\n", __func__);
926 vap->iv_stats.is_tx_nobuf++;
930 KASSERT(needed_space <= MHLEN,
931 ("not enough room, need %u got %zu\n", needed_space, MHLEN));
933 * Setup new mbuf to have leading space to prepend the
934 * 802.11 header and any crypto header bits that are
935 * required (the latter are added when the driver calls
936 * back to ieee80211_crypto_encap to do crypto encapsulation).
938 /* NB: must be first 'cuz it clobbers m_data */
940 n->m_len = 0; /* NB: m_gethdr does not set */
941 n->m_data += needed_space;
943 * Pull up Ethernet header to create the expected layout.
944 * We could use m_pullup but that's overkill (i.e. we don't
945 * need the actual data) and it cannot fail so do it inline
948 /* NB: struct ether_header is known to be contiguous */
949 n->m_len += sizeof(struct ether_header);
950 m->m_len -= sizeof(struct ether_header);
951 m->m_data += sizeof(struct ether_header);
953 * Replace the head of the chain.
959 #undef TO_BE_RECLAIMED
963 * Return the transmit key to use in sending a unicast frame.
964 * If a unicast key is set we use that. When no unicast key is set
965 * we fall back to the default transmit key.
967 static __inline struct ieee80211_key *
968 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
969 struct ieee80211_node *ni)
971 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
972 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
973 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
975 return &vap->iv_nw_keys[vap->iv_def_txkey];
977 return &ni->ni_ucastkey;
982 * Return the transmit key to use in sending a multicast frame.
983 * Multicast traffic always uses the group key which is installed as
984 * the default tx key.
986 static __inline struct ieee80211_key *
987 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
988 struct ieee80211_node *ni)
990 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
991 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
993 return &vap->iv_nw_keys[vap->iv_def_txkey];
997 * Encapsulate an outbound data frame. The mbuf chain is updated.
998 * If an error is encountered NULL is returned. The caller is required
999 * to provide a node reference and pullup the ethernet header in the
1002 * NB: Packet is assumed to be processed by ieee80211_classify which
1003 * marked EAPOL frames w/ M_EAPOL.
1006 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1009 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1010 struct ieee80211com *ic = ni->ni_ic;
1011 #ifdef IEEE80211_SUPPORT_MESH
1012 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1013 struct ieee80211_meshcntl_ae10 *mc;
1015 struct ether_header eh;
1016 struct ieee80211_frame *wh;
1017 struct ieee80211_key *key;
1019 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1020 ieee80211_seq seqno;
1021 int meshhdrsize, meshae;
1025 * Copy existing Ethernet header to a safe place. The
1026 * rest of the code assumes it's ok to strip it when
1027 * reorganizing state for the final encapsulation.
1029 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1030 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1033 * Insure space for additional headers. First identify
1034 * transmit key to use in calculating any buffer adjustments
1035 * required. This is also used below to do privacy
1036 * encapsulation work. Then calculate the 802.11 header
1037 * size and any padding required by the driver.
1039 * Note key may be NULL if we fall back to the default
1040 * transmit key and that is not set. In that case the
1041 * buffer may not be expanded as needed by the cipher
1042 * routines, but they will/should discard it.
1044 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1045 if (vap->iv_opmode == IEEE80211_M_STA ||
1046 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1047 (vap->iv_opmode == IEEE80211_M_WDS &&
1048 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1049 key = ieee80211_crypto_getucastkey(vap, ni);
1051 key = ieee80211_crypto_getmcastkey(vap, ni);
1052 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1053 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1055 "no default transmit key (%s) deftxkey %u",
1056 __func__, vap->iv_def_txkey);
1057 vap->iv_stats.is_tx_nodefkey++;
1063 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1064 * frames so suppress use. This may be an issue if other
1065 * ap's require all data frames to be QoS-encapsulated
1066 * once negotiated in which case we'll need to make this
1069 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1070 (m->m_flags & M_EAPOL) == 0;
1072 hdrsize = sizeof(struct ieee80211_qosframe);
1074 hdrsize = sizeof(struct ieee80211_frame);
1075 #ifdef IEEE80211_SUPPORT_MESH
1076 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1078 * Mesh data frames are encapsulated according to the
1079 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1080 * o Group Addressed data (aka multicast) originating
1081 * at the local sta are sent w/ 3-address format and
1082 * address extension mode 00
1083 * o Individually Addressed data (aka unicast) originating
1084 * at the local sta are sent w/ 4-address format and
1085 * address extension mode 00
1086 * o Group Addressed data forwarded from a non-mesh sta are
1087 * sent w/ 3-address format and address extension mode 01
1088 * o Individually Address data from another sta are sent
1089 * w/ 4-address format and address extension mode 10
1091 is4addr = 0; /* NB: don't use, disable */
1092 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1093 hdrsize += IEEE80211_ADDR_LEN; /* unicast are 4-addr */
1094 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1095 /* XXX defines for AE modes */
1096 if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1097 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1100 meshae = 4; /* NB: pseudo */
1101 } else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1103 meshhdrsize += 1*IEEE80211_ADDR_LEN;
1106 meshhdrsize += 2*IEEE80211_ADDR_LEN;
1111 * 4-address frames need to be generated for:
1112 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1113 * o packets sent through a vap marked for relaying
1114 * (e.g. a station operating with dynamic WDS)
1116 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1117 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1118 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1120 hdrsize += IEEE80211_ADDR_LEN;
1121 meshhdrsize = meshae = 0;
1122 #ifdef IEEE80211_SUPPORT_MESH
1126 * Honor driver DATAPAD requirement.
1128 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1129 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1133 if (__predict_true((m->m_flags & M_FF) == 0)) {
1137 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1139 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1142 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1143 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1144 llc = mtod(m, struct llc *);
1145 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1146 llc->llc_control = LLC_UI;
1147 llc->llc_snap.org_code[0] = 0;
1148 llc->llc_snap.org_code[1] = 0;
1149 llc->llc_snap.org_code[2] = 0;
1150 llc->llc_snap.ether_type = eh.ether_type;
1152 #ifdef IEEE80211_SUPPORT_SUPERG
1156 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1161 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1163 M_PREPEND(m, hdrspace + meshhdrsize, MB_DONTWAIT);
1165 vap->iv_stats.is_tx_nobuf++;
1168 wh = mtod(m, struct ieee80211_frame *);
1169 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1170 *(uint16_t *)wh->i_dur = 0;
1171 qos = NULL; /* NB: quiet compiler */
1173 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1174 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1175 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1176 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1177 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1178 } else switch (vap->iv_opmode) {
1179 case IEEE80211_M_STA:
1180 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1181 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1182 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1183 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1185 case IEEE80211_M_IBSS:
1186 case IEEE80211_M_AHDEMO:
1187 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1188 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1189 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1191 * NB: always use the bssid from iv_bss as the
1192 * neighbor's may be stale after an ibss merge
1194 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1196 case IEEE80211_M_HOSTAP:
1197 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1198 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1199 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1200 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1202 #ifdef IEEE80211_SUPPORT_MESH
1203 case IEEE80211_M_MBSS:
1204 /* NB: offset by hdrspace to deal with DATAPAD */
1205 mc = (struct ieee80211_meshcntl_ae10 *)
1206 (mtod(m, uint8_t *) + hdrspace);
1208 case 0: /* ucast, no proxy */
1209 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1210 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1211 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1212 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1213 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1215 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1217 case 4: /* mcast, no proxy */
1218 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1219 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1220 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1221 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1222 mc->mc_flags = 0; /* NB: AE is really 0 */
1223 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1225 case 1: /* mcast, proxy */
1226 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1227 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1228 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1229 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1231 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1232 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1234 case 2: /* ucast, proxy */
1235 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1236 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1237 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1238 /* XXX not right, need MeshDA */
1239 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1240 /* XXX assume are MeshSA */
1241 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1243 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1244 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1245 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1248 KASSERT(0, ("meshae %d", meshae));
1251 mc->mc_ttl = ms->ms_ttl;
1253 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1256 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1260 if (m->m_flags & M_MORE_DATA)
1261 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1266 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1267 /* NB: mesh case handled earlier */
1268 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1269 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1270 ac = M_WME_GETAC(m);
1271 /* map from access class/queue to 11e header priorty value */
1272 tid = WME_AC_TO_TID(ac);
1273 qos[0] = tid & IEEE80211_QOS_TID;
1274 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1275 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1277 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1279 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1281 * NB: don't assign a sequence # to potential
1282 * aggregates; we expect this happens at the
1283 * point the frame comes off any aggregation q
1284 * as otherwise we may introduce holes in the
1285 * BA sequence space and/or make window accouting
1288 * XXX may want to control this with a driver
1289 * capability; this may also change when we pull
1290 * aggregation up into net80211
1292 seqno = ni->ni_txseqs[tid]++;
1293 *(uint16_t *)wh->i_seq =
1294 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1295 M_SEQNO_SET(m, seqno);
1298 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1299 *(uint16_t *)wh->i_seq =
1300 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1301 M_SEQNO_SET(m, seqno);
1305 /* check if xmit fragmentation is required */
1306 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1307 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1308 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1309 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1312 * IEEE 802.1X: send EAPOL frames always in the clear.
1313 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1315 if ((m->m_flags & M_EAPOL) == 0 ||
1316 ((vap->iv_flags & IEEE80211_F_WPA) &&
1317 (vap->iv_opmode == IEEE80211_M_STA ?
1318 !IEEE80211_KEY_UNDEFINED(key) :
1319 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1320 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1321 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1322 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1324 "%s", "enmic failed, discard frame");
1325 vap->iv_stats.is_crypto_enmicfail++;
1330 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1331 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1334 m->m_flags |= M_ENCAP; /* mark encapsulated */
1336 IEEE80211_NODE_STAT(ni, tx_data);
1337 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1338 IEEE80211_NODE_STAT(ni, tx_mcast);
1339 m->m_flags |= M_MCAST;
1341 IEEE80211_NODE_STAT(ni, tx_ucast);
1342 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1353 * Fragment the frame according to the specified mtu.
1354 * The size of the 802.11 header (w/o padding) is provided
1355 * so we don't need to recalculate it. We create a new
1356 * mbuf for each fragment and chain it through m_nextpkt;
1357 * we might be able to optimize this by reusing the original
1358 * packet's mbufs but that is significantly more complicated.
1361 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1362 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1364 struct ieee80211_frame *wh, *whf;
1365 struct mbuf *m, *prev, *next;
1366 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1368 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1369 KASSERT(m0->m_pkthdr.len > mtu,
1370 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1372 wh = mtod(m0, struct ieee80211_frame *);
1373 /* NB: mark the first frag; it will be propagated below */
1374 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1375 totalhdrsize = hdrsize + ciphdrsize;
1377 off = mtu - ciphdrsize;
1378 remainder = m0->m_pkthdr.len - off;
1381 fragsize = totalhdrsize + remainder;
1384 /* XXX fragsize can be >2048! */
1385 KASSERT(fragsize < MCLBYTES,
1386 ("fragment size %u too big!", fragsize));
1387 if (fragsize > MHLEN)
1388 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1390 m = m_gethdr(MB_DONTWAIT, MT_DATA);
1393 /* leave room to prepend any cipher header */
1394 m_align(m, fragsize - ciphdrsize);
1397 * Form the header in the fragment. Note that since
1398 * we mark the first fragment with the MORE_FRAG bit
1399 * it automatically is propagated to each fragment; we
1400 * need only clear it on the last fragment (done below).
1402 whf = mtod(m, struct ieee80211_frame *);
1403 memcpy(whf, wh, hdrsize);
1404 *(uint16_t *)&whf->i_seq[0] |= htole16(
1405 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1406 IEEE80211_SEQ_FRAG_SHIFT);
1409 payload = fragsize - totalhdrsize;
1410 /* NB: destination is known to be contiguous */
1411 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1412 m->m_len = hdrsize + payload;
1413 m->m_pkthdr.len = hdrsize + payload;
1414 m->m_flags |= M_FRAG;
1416 /* chain up the fragment */
1417 prev->m_nextpkt = m;
1420 /* deduct fragment just formed */
1421 remainder -= payload;
1423 } while (remainder != 0);
1425 /* set the last fragment */
1426 m->m_flags |= M_LASTFRAG;
1427 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1429 /* strip first mbuf now that everything has been copied */
1430 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1431 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1433 vap->iv_stats.is_tx_fragframes++;
1434 vap->iv_stats.is_tx_frags += fragno-1;
1438 /* reclaim fragments but leave original frame for caller to free */
1439 for (m = m0->m_nextpkt; m != NULL; m = next) {
1440 next = m->m_nextpkt;
1441 m->m_nextpkt = NULL; /* XXX paranoid */
1444 m0->m_nextpkt = NULL;
1449 * Add a supported rates element id to a frame.
1452 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1456 *frm++ = IEEE80211_ELEMID_RATES;
1457 nrates = rs->rs_nrates;
1458 if (nrates > IEEE80211_RATE_SIZE)
1459 nrates = IEEE80211_RATE_SIZE;
1461 memcpy(frm, rs->rs_rates, nrates);
1462 return frm + nrates;
1466 * Add an extended supported rates element id to a frame.
1469 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1472 * Add an extended supported rates element if operating in 11g mode.
1474 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1475 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1476 *frm++ = IEEE80211_ELEMID_XRATES;
1478 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1485 * Add an ssid element to a frame.
1488 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1490 *frm++ = IEEE80211_ELEMID_SSID;
1492 memcpy(frm, ssid, len);
1497 * Add an erp element to a frame.
1500 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1504 *frm++ = IEEE80211_ELEMID_ERP;
1507 if (ic->ic_nonerpsta != 0)
1508 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1509 if (ic->ic_flags & IEEE80211_F_USEPROT)
1510 erp |= IEEE80211_ERP_USE_PROTECTION;
1511 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1512 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1518 * Add a CFParams element to a frame.
1521 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1523 #define ADDSHORT(frm, v) do { \
1524 LE_WRITE_2(frm, v); \
1527 *frm++ = IEEE80211_ELEMID_CFPARMS;
1529 *frm++ = 0; /* CFP count */
1530 *frm++ = 2; /* CFP period */
1531 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1532 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1537 static __inline uint8_t *
1538 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1540 memcpy(frm, ie->ie_data, ie->ie_len);
1541 return frm + ie->ie_len;
1544 static __inline uint8_t *
1545 add_ie(uint8_t *frm, const uint8_t *ie)
1547 memcpy(frm, ie, 2 + ie[1]);
1548 return frm + 2 + ie[1];
1551 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1553 * Add a WME information element to a frame.
1556 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1558 static const struct ieee80211_wme_info info = {
1559 .wme_id = IEEE80211_ELEMID_VENDOR,
1560 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1561 .wme_oui = { WME_OUI_BYTES },
1562 .wme_type = WME_OUI_TYPE,
1563 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1564 .wme_version = WME_VERSION,
1567 memcpy(frm, &info, sizeof(info));
1568 return frm + sizeof(info);
1572 * Add a WME parameters element to a frame.
1575 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1577 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1578 #define ADDSHORT(frm, v) do { \
1579 LE_WRITE_2(frm, v); \
1582 /* NB: this works 'cuz a param has an info at the front */
1583 static const struct ieee80211_wme_info param = {
1584 .wme_id = IEEE80211_ELEMID_VENDOR,
1585 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1586 .wme_oui = { WME_OUI_BYTES },
1587 .wme_type = WME_OUI_TYPE,
1588 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1589 .wme_version = WME_VERSION,
1593 memcpy(frm, ¶m, sizeof(param));
1594 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1595 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1596 *frm++ = 0; /* reserved field */
1597 for (i = 0; i < WME_NUM_AC; i++) {
1598 const struct wmeParams *ac =
1599 &wme->wme_bssChanParams.cap_wmeParams[i];
1600 *frm++ = SM(i, WME_PARAM_ACI)
1601 | SM(ac->wmep_acm, WME_PARAM_ACM)
1602 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1604 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1605 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1607 ADDSHORT(frm, ac->wmep_txopLimit);
1613 #undef WME_OUI_BYTES
1616 * Add an 11h Power Constraint element to a frame.
1619 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1621 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1622 /* XXX per-vap tx power limit? */
1623 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1625 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1627 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1632 * Add an 11h Power Capability element to a frame.
1635 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1637 frm[0] = IEEE80211_ELEMID_PWRCAP;
1639 frm[2] = c->ic_minpower;
1640 frm[3] = c->ic_maxpower;
1645 * Add an 11h Supported Channels element to a frame.
1648 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1650 static const int ielen = 26;
1652 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1654 /* XXX not correct */
1655 memcpy(frm+2, ic->ic_chan_avail, ielen);
1656 return frm + 2 + ielen;
1660 * Add an 11h Channel Switch Announcement element to a frame.
1661 * Note that we use the per-vap CSA count to adjust the global
1662 * counter so we can use this routine to form probe response
1663 * frames and get the current count.
1666 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1668 struct ieee80211com *ic = vap->iv_ic;
1669 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1671 csa->csa_ie = IEEE80211_ELEMID_CSA;
1673 csa->csa_mode = 1; /* XXX force quiet on channel */
1674 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1675 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1676 return frm + sizeof(*csa);
1680 * Add an 11h country information element to a frame.
1683 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1686 if (ic->ic_countryie == NULL ||
1687 ic->ic_countryie_chan != ic->ic_bsschan) {
1689 * Handle lazy construction of ie. This is done on
1690 * first use and after a channel change that requires
1693 if (ic->ic_countryie != NULL)
1694 kfree(ic->ic_countryie, M_80211_NODE_IE);
1695 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1696 if (ic->ic_countryie == NULL)
1698 ic->ic_countryie_chan = ic->ic_bsschan;
1700 return add_appie(frm, ic->ic_countryie);
1704 * Send a probe request frame with the specified ssid
1705 * and any optional information element data.
1708 ieee80211_send_probereq(struct ieee80211_node *ni,
1709 const uint8_t sa[IEEE80211_ADDR_LEN],
1710 const uint8_t da[IEEE80211_ADDR_LEN],
1711 const uint8_t bssid[IEEE80211_ADDR_LEN],
1712 const uint8_t *ssid, size_t ssidlen)
1714 struct ieee80211vap *vap = ni->ni_vap;
1715 struct ieee80211com *ic = ni->ni_ic;
1716 const struct ieee80211_txparam *tp;
1717 struct ieee80211_bpf_params params;
1718 struct ieee80211_frame *wh;
1719 const struct ieee80211_rateset *rs;
1723 if (vap->iv_state == IEEE80211_S_CAC) {
1724 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1725 "block %s frame in CAC state", "probe request");
1726 vap->iv_stats.is_tx_badstate++;
1727 return EIO; /* XXX */
1731 * Hold a reference on the node so it doesn't go away until after
1732 * the xmit is complete all the way in the driver. On error we
1733 * will remove our reference.
1735 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1736 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1738 ni, ni->ni_macaddr, ":",
1739 ieee80211_node_refcnt(ni)+1);
1740 ieee80211_ref_node(ni);
1743 * prreq frame format
1745 * [tlv] supported rates
1746 * [tlv] RSN (optional)
1747 * [tlv] extended supported rates
1748 * [tlv] WPA (optional)
1749 * [tlv] user-specified ie's
1751 m = ieee80211_getmgtframe(&frm,
1752 ic->ic_headroom + sizeof(struct ieee80211_frame),
1753 2 + IEEE80211_NWID_LEN
1754 + 2 + IEEE80211_RATE_SIZE
1755 + sizeof(struct ieee80211_ie_wpa)
1756 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1757 + sizeof(struct ieee80211_ie_wpa)
1758 + (vap->iv_appie_probereq != NULL ?
1759 vap->iv_appie_probereq->ie_len : 0)
1762 vap->iv_stats.is_tx_nobuf++;
1763 ieee80211_free_node(ni);
1767 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1768 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1769 frm = ieee80211_add_rates(frm, rs);
1770 if (vap->iv_flags & IEEE80211_F_WPA2) {
1771 if (vap->iv_rsn_ie != NULL)
1772 frm = add_ie(frm, vap->iv_rsn_ie);
1773 /* XXX else complain? */
1775 frm = ieee80211_add_xrates(frm, rs);
1776 if (vap->iv_flags & IEEE80211_F_WPA1) {
1777 if (vap->iv_wpa_ie != NULL)
1778 frm = add_ie(frm, vap->iv_wpa_ie);
1779 /* XXX else complain? */
1781 if (vap->iv_appie_probereq != NULL)
1782 frm = add_appie(frm, vap->iv_appie_probereq);
1783 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1785 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1786 ("leading space %zd", M_LEADINGSPACE(m)));
1787 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1789 /* NB: cannot happen */
1790 ieee80211_free_node(ni);
1794 wh = mtod(m, struct ieee80211_frame *);
1795 ieee80211_send_setup(ni, m,
1796 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1797 IEEE80211_NONQOS_TID, sa, da, bssid);
1798 /* XXX power management? */
1799 m->m_flags |= M_ENCAP; /* mark encapsulated */
1801 M_WME_SETAC(m, WME_AC_BE);
1803 IEEE80211_NODE_STAT(ni, tx_probereq);
1804 IEEE80211_NODE_STAT(ni, tx_mgmt);
1806 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1807 "send probe req on channel %u bssid %6D ssid \"%.*s\"\n",
1808 ieee80211_chan2ieee(ic, ic->ic_curchan), bssid, ":",
1811 memset(¶ms, 0, sizeof(params));
1812 params.ibp_pri = M_WME_GETAC(m);
1813 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1814 params.ibp_rate0 = tp->mgmtrate;
1815 if (IEEE80211_IS_MULTICAST(da)) {
1816 params.ibp_flags |= IEEE80211_BPF_NOACK;
1817 params.ibp_try0 = 1;
1819 params.ibp_try0 = tp->maxretry;
1820 params.ibp_power = ni->ni_txpower;
1821 return ic->ic_raw_xmit(ni, m, ¶ms);
1825 * Calculate capability information for mgt frames.
1828 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1830 struct ieee80211com *ic = vap->iv_ic;
1833 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1835 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1836 capinfo = IEEE80211_CAPINFO_ESS;
1837 else if (vap->iv_opmode == IEEE80211_M_IBSS)
1838 capinfo = IEEE80211_CAPINFO_IBSS;
1841 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1842 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1843 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1844 IEEE80211_IS_CHAN_2GHZ(chan))
1845 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1846 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1847 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1848 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1849 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1854 * Send a management frame. The node is for the destination (or ic_bss
1855 * when in station mode). Nodes other than ic_bss have their reference
1856 * count bumped to reflect our use for an indeterminant time.
1859 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1861 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1862 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1863 struct ieee80211vap *vap = ni->ni_vap;
1864 struct ieee80211com *ic = ni->ni_ic;
1865 struct ieee80211_node *bss = vap->iv_bss;
1866 struct ieee80211_bpf_params params;
1870 int has_challenge, is_shared_key, ret, status;
1872 KASSERT(ni != NULL, ("null node"));
1875 * Hold a reference on the node so it doesn't go away until after
1876 * the xmit is complete all the way in the driver. On error we
1877 * will remove our reference.
1879 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1880 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1882 ni, ni->ni_macaddr, ":",
1883 ieee80211_node_refcnt(ni)+1);
1884 ieee80211_ref_node(ni);
1886 memset(¶ms, 0, sizeof(params));
1889 case IEEE80211_FC0_SUBTYPE_AUTH:
1892 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1893 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1894 ni->ni_challenge != NULL);
1897 * Deduce whether we're doing open authentication or
1898 * shared key authentication. We do the latter if
1899 * we're in the middle of a shared key authentication
1900 * handshake or if we're initiating an authentication
1901 * request and configured to use shared key.
1903 is_shared_key = has_challenge ||
1904 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1905 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1906 bss->ni_authmode == IEEE80211_AUTH_SHARED);
1908 m = ieee80211_getmgtframe(&frm,
1909 ic->ic_headroom + sizeof(struct ieee80211_frame),
1910 3 * sizeof(uint16_t)
1911 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1912 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1915 senderr(ENOMEM, is_tx_nobuf);
1917 ((uint16_t *)frm)[0] =
1918 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1919 : htole16(IEEE80211_AUTH_ALG_OPEN);
1920 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1921 ((uint16_t *)frm)[2] = htole16(status);/* status */
1923 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1924 ((uint16_t *)frm)[3] =
1925 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1926 IEEE80211_ELEMID_CHALLENGE);
1927 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1928 IEEE80211_CHALLENGE_LEN);
1929 m->m_pkthdr.len = m->m_len =
1930 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1931 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1932 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1933 "request encrypt frame (%s)", __func__);
1934 /* mark frame for encryption */
1935 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1938 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1940 /* XXX not right for shared key */
1941 if (status == IEEE80211_STATUS_SUCCESS)
1942 IEEE80211_NODE_STAT(ni, tx_auth);
1944 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1946 if (vap->iv_opmode == IEEE80211_M_STA)
1947 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1948 (void *) vap->iv_state);
1951 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1952 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1953 "send station deauthenticate (reason %d)", arg);
1954 m = ieee80211_getmgtframe(&frm,
1955 ic->ic_headroom + sizeof(struct ieee80211_frame),
1958 senderr(ENOMEM, is_tx_nobuf);
1959 *(uint16_t *)frm = htole16(arg); /* reason */
1960 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1962 IEEE80211_NODE_STAT(ni, tx_deauth);
1963 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1965 ieee80211_node_unauthorize(ni); /* port closed */
1968 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1969 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1971 * asreq frame format
1972 * [2] capability information
1973 * [2] listen interval
1974 * [6*] current AP address (reassoc only)
1976 * [tlv] supported rates
1977 * [tlv] extended supported rates
1978 * [4] power capability (optional)
1979 * [28] supported channels (optional)
1980 * [tlv] HT capabilities
1981 * [tlv] WME (optional)
1982 * [tlv] Vendor OUI HT capabilities (optional)
1983 * [tlv] Atheros capabilities (if negotiated)
1984 * [tlv] AppIE's (optional)
1986 m = ieee80211_getmgtframe(&frm,
1987 ic->ic_headroom + sizeof(struct ieee80211_frame),
1990 + IEEE80211_ADDR_LEN
1991 + 2 + IEEE80211_NWID_LEN
1992 + 2 + IEEE80211_RATE_SIZE
1993 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1996 + sizeof(struct ieee80211_wme_info)
1997 + sizeof(struct ieee80211_ie_htcap)
1998 + 4 + sizeof(struct ieee80211_ie_htcap)
1999 #ifdef IEEE80211_SUPPORT_SUPERG
2000 + sizeof(struct ieee80211_ath_ie)
2002 + (vap->iv_appie_wpa != NULL ?
2003 vap->iv_appie_wpa->ie_len : 0)
2004 + (vap->iv_appie_assocreq != NULL ?
2005 vap->iv_appie_assocreq->ie_len : 0)
2008 senderr(ENOMEM, is_tx_nobuf);
2010 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2011 ("wrong mode %u", vap->iv_opmode));
2012 capinfo = IEEE80211_CAPINFO_ESS;
2013 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2014 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2016 * NB: Some 11a AP's reject the request when
2017 * short premable is set.
2019 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2020 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2021 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2022 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2023 (ic->ic_caps & IEEE80211_C_SHSLOT))
2024 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2025 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2026 (vap->iv_flags & IEEE80211_F_DOTH))
2027 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2028 *(uint16_t *)frm = htole16(capinfo);
2031 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2032 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2036 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2037 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2038 frm += IEEE80211_ADDR_LEN;
2041 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2042 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2043 if (vap->iv_flags & IEEE80211_F_WPA2) {
2044 if (vap->iv_rsn_ie != NULL)
2045 frm = add_ie(frm, vap->iv_rsn_ie);
2046 /* XXX else complain? */
2048 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2049 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2050 frm = ieee80211_add_powercapability(frm,
2052 frm = ieee80211_add_supportedchannels(frm, ic);
2054 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2055 ni->ni_ies.htcap_ie != NULL &&
2056 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2057 frm = ieee80211_add_htcap(frm, ni);
2058 if (vap->iv_flags & IEEE80211_F_WPA1) {
2059 if (vap->iv_wpa_ie != NULL)
2060 frm = add_ie(frm, vap->iv_wpa_ie);
2061 /* XXX else complain */
2063 if ((ic->ic_flags & IEEE80211_F_WME) &&
2064 ni->ni_ies.wme_ie != NULL)
2065 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2066 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2067 ni->ni_ies.htcap_ie != NULL &&
2068 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2069 frm = ieee80211_add_htcap_vendor(frm, ni);
2070 #ifdef IEEE80211_SUPPORT_SUPERG
2071 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2072 frm = ieee80211_add_ath(frm,
2073 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2074 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2075 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2076 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2078 #endif /* IEEE80211_SUPPORT_SUPERG */
2079 if (vap->iv_appie_assocreq != NULL)
2080 frm = add_appie(frm, vap->iv_appie_assocreq);
2081 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2083 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2084 (void *) vap->iv_state);
2087 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2088 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2090 * asresp frame format
2091 * [2] capability information
2093 * [2] association ID
2094 * [tlv] supported rates
2095 * [tlv] extended supported rates
2096 * [tlv] HT capabilities (standard, if STA enabled)
2097 * [tlv] HT information (standard, if STA enabled)
2098 * [tlv] WME (if configured and STA enabled)
2099 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2100 * [tlv] HT information (vendor OUI, if STA enabled)
2101 * [tlv] Atheros capabilities (if STA enabled)
2102 * [tlv] AppIE's (optional)
2104 m = ieee80211_getmgtframe(&frm,
2105 ic->ic_headroom + sizeof(struct ieee80211_frame),
2109 + 2 + IEEE80211_RATE_SIZE
2110 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2111 + sizeof(struct ieee80211_ie_htcap) + 4
2112 + sizeof(struct ieee80211_ie_htinfo) + 4
2113 + sizeof(struct ieee80211_wme_param)
2114 #ifdef IEEE80211_SUPPORT_SUPERG
2115 + sizeof(struct ieee80211_ath_ie)
2117 + (vap->iv_appie_assocresp != NULL ?
2118 vap->iv_appie_assocresp->ie_len : 0)
2121 senderr(ENOMEM, is_tx_nobuf);
2123 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2124 *(uint16_t *)frm = htole16(capinfo);
2127 *(uint16_t *)frm = htole16(arg); /* status */
2130 if (arg == IEEE80211_STATUS_SUCCESS) {
2131 *(uint16_t *)frm = htole16(ni->ni_associd);
2132 IEEE80211_NODE_STAT(ni, tx_assoc);
2134 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2137 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2138 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2139 /* NB: respond according to what we received */
2140 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2141 frm = ieee80211_add_htcap(frm, ni);
2142 frm = ieee80211_add_htinfo(frm, ni);
2144 if ((vap->iv_flags & IEEE80211_F_WME) &&
2145 ni->ni_ies.wme_ie != NULL)
2146 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2147 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2148 frm = ieee80211_add_htcap_vendor(frm, ni);
2149 frm = ieee80211_add_htinfo_vendor(frm, ni);
2151 #ifdef IEEE80211_SUPPORT_SUPERG
2152 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2153 frm = ieee80211_add_ath(frm,
2154 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2155 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2156 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2157 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2158 #endif /* IEEE80211_SUPPORT_SUPERG */
2159 if (vap->iv_appie_assocresp != NULL)
2160 frm = add_appie(frm, vap->iv_appie_assocresp);
2161 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2164 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2165 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2166 "send station disassociate (reason %d)", arg);
2167 m = ieee80211_getmgtframe(&frm,
2168 ic->ic_headroom + sizeof(struct ieee80211_frame),
2171 senderr(ENOMEM, is_tx_nobuf);
2172 *(uint16_t *)frm = htole16(arg); /* reason */
2173 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2175 IEEE80211_NODE_STAT(ni, tx_disassoc);
2176 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2180 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2181 "invalid mgmt frame type %u", type);
2182 senderr(EINVAL, is_tx_unknownmgt);
2186 /* NB: force non-ProbeResp frames to the highest queue */
2187 params.ibp_pri = WME_AC_VO;
2188 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2189 /* NB: we know all frames are unicast */
2190 params.ibp_try0 = bss->ni_txparms->maxretry;
2191 params.ibp_power = bss->ni_txpower;
2192 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2194 ieee80211_free_node(ni);
2201 * Return an mbuf with a probe response frame in it.
2202 * Space is left to prepend and 802.11 header at the
2203 * front but it's left to the caller to fill in.
2206 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2208 struct ieee80211vap *vap = bss->ni_vap;
2209 struct ieee80211com *ic = bss->ni_ic;
2210 const struct ieee80211_rateset *rs;
2216 * probe response frame format
2218 * [2] beacon interval
2219 * [2] cabability information
2221 * [tlv] supported rates
2222 * [tlv] parameter set (FH/DS)
2223 * [tlv] parameter set (IBSS)
2224 * [tlv] country (optional)
2225 * [3] power control (optional)
2226 * [5] channel switch announcement (CSA) (optional)
2227 * [tlv] extended rate phy (ERP)
2228 * [tlv] extended supported rates
2229 * [tlv] RSN (optional)
2230 * [tlv] HT capabilities
2231 * [tlv] HT information
2232 * [tlv] WPA (optional)
2233 * [tlv] WME (optional)
2234 * [tlv] Vendor OUI HT capabilities (optional)
2235 * [tlv] Vendor OUI HT information (optional)
2236 * [tlv] Atheros capabilities
2237 * [tlv] AppIE's (optional)
2238 * [tlv] Mesh ID (MBSS)
2239 * [tlv] Mesh Conf (MBSS)
2241 m = ieee80211_getmgtframe(&frm,
2242 ic->ic_headroom + sizeof(struct ieee80211_frame),
2246 + 2 + IEEE80211_NWID_LEN
2247 + 2 + IEEE80211_RATE_SIZE
2249 + IEEE80211_COUNTRY_MAX_SIZE
2251 + sizeof(struct ieee80211_csa_ie)
2253 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2254 + sizeof(struct ieee80211_ie_wpa)
2255 + sizeof(struct ieee80211_ie_htcap)
2256 + sizeof(struct ieee80211_ie_htinfo)
2257 + sizeof(struct ieee80211_ie_wpa)
2258 + sizeof(struct ieee80211_wme_param)
2259 + 4 + sizeof(struct ieee80211_ie_htcap)
2260 + 4 + sizeof(struct ieee80211_ie_htinfo)
2261 #ifdef IEEE80211_SUPPORT_SUPERG
2262 + sizeof(struct ieee80211_ath_ie)
2264 #ifdef IEEE80211_SUPPORT_MESH
2265 + 2 + IEEE80211_MESHID_LEN
2266 + sizeof(struct ieee80211_meshconf_ie)
2268 + (vap->iv_appie_proberesp != NULL ?
2269 vap->iv_appie_proberesp->ie_len : 0)
2272 vap->iv_stats.is_tx_nobuf++;
2276 memset(frm, 0, 8); /* timestamp should be filled later */
2278 *(uint16_t *)frm = htole16(bss->ni_intval);
2280 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2281 *(uint16_t *)frm = htole16(capinfo);
2284 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2285 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2286 frm = ieee80211_add_rates(frm, rs);
2288 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2289 *frm++ = IEEE80211_ELEMID_FHPARMS;
2291 *frm++ = bss->ni_fhdwell & 0x00ff;
2292 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2293 *frm++ = IEEE80211_FH_CHANSET(
2294 ieee80211_chan2ieee(ic, bss->ni_chan));
2295 *frm++ = IEEE80211_FH_CHANPAT(
2296 ieee80211_chan2ieee(ic, bss->ni_chan));
2297 *frm++ = bss->ni_fhindex;
2299 *frm++ = IEEE80211_ELEMID_DSPARMS;
2301 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2304 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2305 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2307 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2309 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2310 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2311 frm = ieee80211_add_countryie(frm, ic);
2312 if (vap->iv_flags & IEEE80211_F_DOTH) {
2313 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2314 frm = ieee80211_add_powerconstraint(frm, vap);
2315 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2316 frm = ieee80211_add_csa(frm, vap);
2318 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2319 frm = ieee80211_add_erp(frm, ic);
2320 frm = ieee80211_add_xrates(frm, rs);
2321 if (vap->iv_flags & IEEE80211_F_WPA2) {
2322 if (vap->iv_rsn_ie != NULL)
2323 frm = add_ie(frm, vap->iv_rsn_ie);
2324 /* XXX else complain? */
2327 * NB: legacy 11b clients do not get certain ie's.
2328 * The caller identifies such clients by passing
2329 * a token in legacy to us. Could expand this to be
2330 * any legacy client for stuff like HT ie's.
2332 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2333 legacy != IEEE80211_SEND_LEGACY_11B) {
2334 frm = ieee80211_add_htcap(frm, bss);
2335 frm = ieee80211_add_htinfo(frm, bss);
2337 if (vap->iv_flags & IEEE80211_F_WPA1) {
2338 if (vap->iv_wpa_ie != NULL)
2339 frm = add_ie(frm, vap->iv_wpa_ie);
2340 /* XXX else complain? */
2342 if (vap->iv_flags & IEEE80211_F_WME)
2343 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2344 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2345 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2346 legacy != IEEE80211_SEND_LEGACY_11B) {
2347 frm = ieee80211_add_htcap_vendor(frm, bss);
2348 frm = ieee80211_add_htinfo_vendor(frm, bss);
2350 #ifdef IEEE80211_SUPPORT_SUPERG
2351 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2352 legacy != IEEE80211_SEND_LEGACY_11B)
2353 frm = ieee80211_add_athcaps(frm, bss);
2355 if (vap->iv_appie_proberesp != NULL)
2356 frm = add_appie(frm, vap->iv_appie_proberesp);
2357 #ifdef IEEE80211_SUPPORT_MESH
2358 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2359 frm = ieee80211_add_meshid(frm, vap);
2360 frm = ieee80211_add_meshconf(frm, vap);
2363 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2369 * Send a probe response frame to the specified mac address.
2370 * This does not go through the normal mgt frame api so we
2371 * can specify the destination address and re-use the bss node
2372 * for the sta reference.
2375 ieee80211_send_proberesp(struct ieee80211vap *vap,
2376 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2378 struct ieee80211_node *bss = vap->iv_bss;
2379 struct ieee80211com *ic = vap->iv_ic;
2380 struct ieee80211_frame *wh;
2383 if (vap->iv_state == IEEE80211_S_CAC) {
2384 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2385 "block %s frame in CAC state", "probe response");
2386 vap->iv_stats.is_tx_badstate++;
2387 return EIO; /* XXX */
2391 * Hold a reference on the node so it doesn't go away until after
2392 * the xmit is complete all the way in the driver. On error we
2393 * will remove our reference.
2395 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2396 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
2397 __func__, __LINE__, bss, bss->ni_macaddr, ":",
2398 ieee80211_node_refcnt(bss)+1);
2399 ieee80211_ref_node(bss);
2401 m = ieee80211_alloc_proberesp(bss, legacy);
2403 ieee80211_free_node(bss);
2407 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2408 KASSERT(m != NULL, ("no room for header"));
2410 wh = mtod(m, struct ieee80211_frame *);
2411 ieee80211_send_setup(bss, m,
2412 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
2413 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
2414 /* XXX power management? */
2415 m->m_flags |= M_ENCAP; /* mark encapsulated */
2417 M_WME_SETAC(m, WME_AC_BE);
2419 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
2420 "send probe resp on channel %u to %6D%s\n",
2421 ieee80211_chan2ieee(ic, ic->ic_curchan), da, ":",
2422 legacy ? " <legacy>" : "");
2423 IEEE80211_NODE_STAT(bss, tx_mgmt);
2425 return ic->ic_raw_xmit(bss, m, NULL);
2429 * Allocate and build a RTS (Request To Send) control frame.
2432 ieee80211_alloc_rts(struct ieee80211com *ic,
2433 const uint8_t ra[IEEE80211_ADDR_LEN],
2434 const uint8_t ta[IEEE80211_ADDR_LEN],
2437 struct ieee80211_frame_rts *rts;
2440 /* XXX honor ic_headroom */
2441 m = m_gethdr(MB_DONTWAIT, MT_DATA);
2443 rts = mtod(m, struct ieee80211_frame_rts *);
2444 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2445 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
2446 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2447 *(u_int16_t *)rts->i_dur = htole16(dur);
2448 IEEE80211_ADDR_COPY(rts->i_ra, ra);
2449 IEEE80211_ADDR_COPY(rts->i_ta, ta);
2451 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
2457 * Allocate and build a CTS (Clear To Send) control frame.
2460 ieee80211_alloc_cts(struct ieee80211com *ic,
2461 const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
2463 struct ieee80211_frame_cts *cts;
2466 /* XXX honor ic_headroom */
2467 m = m_gethdr(MB_DONTWAIT, MT_DATA);
2469 cts = mtod(m, struct ieee80211_frame_cts *);
2470 cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
2471 IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
2472 cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2473 *(u_int16_t *)cts->i_dur = htole16(dur);
2474 IEEE80211_ADDR_COPY(cts->i_ra, ra);
2476 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
2482 ieee80211_tx_mgt_timeout(void *arg)
2484 struct ieee80211_node *ni = arg;
2485 struct ieee80211vap *vap = ni->ni_vap;
2487 if (vap->iv_state != IEEE80211_S_INIT &&
2488 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2490 * NB: it's safe to specify a timeout as the reason here;
2491 * it'll only be used in the right state.
2493 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2494 IEEE80211_SCAN_FAIL_TIMEOUT);
2499 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2501 struct ieee80211vap *vap = ni->ni_vap;
2502 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2505 * Frame transmit completed; arrange timer callback. If
2506 * transmit was successfuly we wait for response. Otherwise
2507 * we arrange an immediate callback instead of doing the
2508 * callback directly since we don't know what state the driver
2509 * is in (e.g. what locks it is holding). This work should
2510 * not be too time-critical and not happen too often so the
2511 * added overhead is acceptable.
2513 * XXX what happens if !acked but response shows up before callback?
2515 if (vap->iv_state == ostate)
2516 callout_reset(&vap->iv_mgtsend,
2517 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2518 ieee80211_tx_mgt_timeout, ni);
2522 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2523 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2525 struct ieee80211vap *vap = ni->ni_vap;
2526 struct ieee80211com *ic = ni->ni_ic;
2527 struct ieee80211_rateset *rs = &ni->ni_rates;
2531 * beacon frame format
2533 * [2] beacon interval
2534 * [2] cabability information
2536 * [tlv] supported rates
2537 * [3] parameter set (DS)
2538 * [8] CF parameter set (optional)
2539 * [tlv] parameter set (IBSS/TIM)
2540 * [tlv] country (optional)
2541 * [3] power control (optional)
2542 * [5] channel switch announcement (CSA) (optional)
2543 * [tlv] extended rate phy (ERP)
2544 * [tlv] extended supported rates
2545 * [tlv] RSN parameters
2546 * [tlv] HT capabilities
2547 * [tlv] HT information
2548 * XXX Vendor-specific OIDs (e.g. Atheros)
2549 * [tlv] WPA parameters
2550 * [tlv] WME parameters
2551 * [tlv] Vendor OUI HT capabilities (optional)
2552 * [tlv] Vendor OUI HT information (optional)
2553 * [tlv] Atheros capabilities (optional)
2554 * [tlv] TDMA parameters (optional)
2555 * [tlv] Mesh ID (MBSS)
2556 * [tlv] Mesh Conf (MBSS)
2557 * [tlv] application data (optional)
2560 memset(bo, 0, sizeof(*bo));
2562 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2564 *(uint16_t *)frm = htole16(ni->ni_intval);
2566 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2567 bo->bo_caps = (uint16_t *)frm;
2568 *(uint16_t *)frm = htole16(capinfo);
2570 *frm++ = IEEE80211_ELEMID_SSID;
2571 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2572 *frm++ = ni->ni_esslen;
2573 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2574 frm += ni->ni_esslen;
2577 frm = ieee80211_add_rates(frm, rs);
2578 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2579 *frm++ = IEEE80211_ELEMID_DSPARMS;
2581 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2583 if (ic->ic_flags & IEEE80211_F_PCF) {
2585 frm = ieee80211_add_cfparms(frm, ic);
2588 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2589 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2591 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2593 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2594 vap->iv_opmode == IEEE80211_M_MBSS) {
2595 /* TIM IE is the same for Mesh and Hostap */
2596 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2598 tie->tim_ie = IEEE80211_ELEMID_TIM;
2599 tie->tim_len = 4; /* length */
2600 tie->tim_count = 0; /* DTIM count */
2601 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2602 tie->tim_bitctl = 0; /* bitmap control */
2603 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2604 frm += sizeof(struct ieee80211_tim_ie);
2607 bo->bo_tim_trailer = frm;
2608 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2609 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2610 frm = ieee80211_add_countryie(frm, ic);
2611 if (vap->iv_flags & IEEE80211_F_DOTH) {
2612 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2613 frm = ieee80211_add_powerconstraint(frm, vap);
2615 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2616 frm = ieee80211_add_csa(frm, vap);
2619 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2621 frm = ieee80211_add_erp(frm, ic);
2623 frm = ieee80211_add_xrates(frm, rs);
2624 if (vap->iv_flags & IEEE80211_F_WPA2) {
2625 if (vap->iv_rsn_ie != NULL)
2626 frm = add_ie(frm, vap->iv_rsn_ie);
2627 /* XXX else complain */
2629 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2630 frm = ieee80211_add_htcap(frm, ni);
2631 bo->bo_htinfo = frm;
2632 frm = ieee80211_add_htinfo(frm, ni);
2634 if (vap->iv_flags & IEEE80211_F_WPA1) {
2635 if (vap->iv_wpa_ie != NULL)
2636 frm = add_ie(frm, vap->iv_wpa_ie);
2637 /* XXX else complain */
2639 if (vap->iv_flags & IEEE80211_F_WME) {
2641 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2643 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2644 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2645 frm = ieee80211_add_htcap_vendor(frm, ni);
2646 frm = ieee80211_add_htinfo_vendor(frm, ni);
2648 #ifdef IEEE80211_SUPPORT_SUPERG
2649 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2651 frm = ieee80211_add_athcaps(frm, ni);
2654 #ifdef IEEE80211_SUPPORT_TDMA
2655 if (vap->iv_caps & IEEE80211_C_TDMA) {
2657 frm = ieee80211_add_tdma(frm, vap);
2660 if (vap->iv_appie_beacon != NULL) {
2662 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2663 frm = add_appie(frm, vap->iv_appie_beacon);
2665 #ifdef IEEE80211_SUPPORT_MESH
2666 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2667 frm = ieee80211_add_meshid(frm, vap);
2668 bo->bo_meshconf = frm;
2669 frm = ieee80211_add_meshconf(frm, vap);
2672 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2673 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2674 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2678 * Allocate a beacon frame and fillin the appropriate bits.
2681 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2682 struct ieee80211_beacon_offsets *bo)
2684 struct ieee80211vap *vap = ni->ni_vap;
2685 struct ieee80211com *ic = ni->ni_ic;
2686 struct ifnet *ifp = vap->iv_ifp;
2687 struct ieee80211_frame *wh;
2693 * beacon frame format
2695 * [2] beacon interval
2696 * [2] cabability information
2698 * [tlv] supported rates
2699 * [3] parameter set (DS)
2700 * [8] CF parameter set (optional)
2701 * [tlv] parameter set (IBSS/TIM)
2702 * [tlv] country (optional)
2703 * [3] power control (optional)
2704 * [5] channel switch announcement (CSA) (optional)
2705 * [tlv] extended rate phy (ERP)
2706 * [tlv] extended supported rates
2707 * [tlv] RSN parameters
2708 * [tlv] HT capabilities
2709 * [tlv] HT information
2710 * [tlv] Vendor OUI HT capabilities (optional)
2711 * [tlv] Vendor OUI HT information (optional)
2712 * XXX Vendor-specific OIDs (e.g. Atheros)
2713 * [tlv] WPA parameters
2714 * [tlv] WME parameters
2715 * [tlv] TDMA parameters (optional)
2716 * [tlv] Mesh ID (MBSS)
2717 * [tlv] Mesh Conf (MBSS)
2718 * [tlv] application data (optional)
2719 * NB: we allocate the max space required for the TIM bitmap.
2720 * XXX how big is this?
2722 pktlen = 8 /* time stamp */
2723 + sizeof(uint16_t) /* beacon interval */
2724 + sizeof(uint16_t) /* capabilities */
2725 + 2 + ni->ni_esslen /* ssid */
2726 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2727 + 2 + 1 /* DS parameters */
2728 + 2 + 6 /* CF parameters */
2729 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2730 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2731 + 2 + 1 /* power control */
2732 + sizeof(struct ieee80211_csa_ie) /* CSA */
2734 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2735 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2736 2*sizeof(struct ieee80211_ie_wpa) : 0)
2737 /* XXX conditional? */
2738 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2739 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2740 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2741 sizeof(struct ieee80211_wme_param) : 0)
2742 #ifdef IEEE80211_SUPPORT_SUPERG
2743 + sizeof(struct ieee80211_ath_ie) /* ATH */
2745 #ifdef IEEE80211_SUPPORT_TDMA
2746 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2747 sizeof(struct ieee80211_tdma_param) : 0)
2749 #ifdef IEEE80211_SUPPORT_MESH
2750 + 2 + ni->ni_meshidlen
2751 + sizeof(struct ieee80211_meshconf_ie)
2753 + IEEE80211_MAX_APPIE
2755 m = ieee80211_getmgtframe(&frm,
2756 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2758 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2759 "%s: cannot get buf; size %u\n", __func__, pktlen);
2760 vap->iv_stats.is_tx_nobuf++;
2763 ieee80211_beacon_construct(m, frm, bo, ni);
2765 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2766 KASSERT(m != NULL, ("no space for 802.11 header?"));
2767 wh = mtod(m, struct ieee80211_frame *);
2768 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2769 IEEE80211_FC0_SUBTYPE_BEACON;
2770 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2771 *(uint16_t *)wh->i_dur = 0;
2772 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2773 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2774 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2775 *(uint16_t *)wh->i_seq = 0;
2781 * Update the dynamic parts of a beacon frame based on the current state.
2784 ieee80211_beacon_update(struct ieee80211_node *ni,
2785 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2787 struct ieee80211vap *vap = ni->ni_vap;
2788 struct ieee80211com *ic = ni->ni_ic;
2789 int len_changed = 0;
2794 * Handle 11h channel change when we've reached the count.
2795 * We must recalculate the beacon frame contents to account
2796 * for the new channel. Note we do this only for the first
2797 * vap that reaches this point; subsequent vaps just update
2798 * their beacon state to reflect the recalculated channel.
2800 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2801 vap->iv_csa_count == ic->ic_csa_count) {
2802 vap->iv_csa_count = 0;
2804 * Effect channel change before reconstructing the beacon
2805 * frame contents as many places reference ni_chan.
2807 if (ic->ic_csa_newchan != NULL)
2808 ieee80211_csa_completeswitch(ic);
2810 * NB: ieee80211_beacon_construct clears all pending
2811 * updates in bo_flags so we don't need to explicitly
2812 * clear IEEE80211_BEACON_CSA.
2814 ieee80211_beacon_construct(m,
2815 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2817 /* XXX do WME aggressive mode processing? */
2818 IEEE80211_UNLOCK(ic);
2819 return 1; /* just assume length changed */
2822 /* XXX faster to recalculate entirely or just changes? */
2823 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2824 *bo->bo_caps = htole16(capinfo);
2826 if (vap->iv_flags & IEEE80211_F_WME) {
2827 struct ieee80211_wme_state *wme = &ic->ic_wme;
2830 * Check for agressive mode change. When there is
2831 * significant high priority traffic in the BSS
2832 * throttle back BE traffic by using conservative
2833 * parameters. Otherwise BE uses agressive params
2834 * to optimize performance of legacy/non-QoS traffic.
2836 if (wme->wme_flags & WME_F_AGGRMODE) {
2837 if (wme->wme_hipri_traffic >
2838 wme->wme_hipri_switch_thresh) {
2839 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2840 "%s: traffic %u, disable aggressive mode\n",
2841 __func__, wme->wme_hipri_traffic);
2842 wme->wme_flags &= ~WME_F_AGGRMODE;
2843 ieee80211_wme_updateparams_locked(vap);
2844 wme->wme_hipri_traffic =
2845 wme->wme_hipri_switch_hysteresis;
2847 wme->wme_hipri_traffic = 0;
2849 if (wme->wme_hipri_traffic <=
2850 wme->wme_hipri_switch_thresh) {
2851 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2852 "%s: traffic %u, enable aggressive mode\n",
2853 __func__, wme->wme_hipri_traffic);
2854 wme->wme_flags |= WME_F_AGGRMODE;
2855 ieee80211_wme_updateparams_locked(vap);
2856 wme->wme_hipri_traffic = 0;
2858 wme->wme_hipri_traffic =
2859 wme->wme_hipri_switch_hysteresis;
2861 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2862 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
2863 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2867 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
2868 ieee80211_ht_update_beacon(vap, bo);
2869 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2871 #ifdef IEEE80211_SUPPORT_TDMA
2872 if (vap->iv_caps & IEEE80211_C_TDMA) {
2874 * NB: the beacon is potentially updated every TBTT.
2876 ieee80211_tdma_update_beacon(vap, bo);
2879 #ifdef IEEE80211_SUPPORT_MESH
2880 if (vap->iv_opmode == IEEE80211_M_MBSS)
2881 ieee80211_mesh_update_beacon(vap, bo);
2884 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2885 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
2886 struct ieee80211_tim_ie *tie =
2887 (struct ieee80211_tim_ie *) bo->bo_tim;
2888 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2889 u_int timlen, timoff, i;
2891 * ATIM/DTIM needs updating. If it fits in the
2892 * current space allocated then just copy in the
2893 * new bits. Otherwise we need to move any trailing
2894 * data to make room. Note that we know there is
2895 * contiguous space because ieee80211_beacon_allocate
2896 * insures there is space in the mbuf to write a
2897 * maximal-size virtual bitmap (based on iv_max_aid).
2900 * Calculate the bitmap size and offset, copy any
2901 * trailer out of the way, and then copy in the
2902 * new bitmap and update the information element.
2903 * Note that the tim bitmap must contain at least
2904 * one byte and any offset must be even.
2906 if (vap->iv_ps_pending != 0) {
2907 timoff = 128; /* impossibly large */
2908 for (i = 0; i < vap->iv_tim_len; i++)
2909 if (vap->iv_tim_bitmap[i]) {
2913 KASSERT(timoff != 128, ("tim bitmap empty!"));
2914 for (i = vap->iv_tim_len-1; i >= timoff; i--)
2915 if (vap->iv_tim_bitmap[i])
2917 timlen = 1 + (i - timoff);
2922 if (timlen != bo->bo_tim_len) {
2923 /* copy up/down trailer */
2924 int adjust = tie->tim_bitmap+timlen
2925 - bo->bo_tim_trailer;
2926 ovbcopy(bo->bo_tim_trailer,
2927 bo->bo_tim_trailer+adjust,
2928 bo->bo_tim_trailer_len);
2929 bo->bo_tim_trailer += adjust;
2930 bo->bo_erp += adjust;
2931 bo->bo_htinfo += adjust;
2932 #ifdef IEEE80211_SUPERG_SUPPORT
2933 bo->bo_ath += adjust;
2935 #ifdef IEEE80211_TDMA_SUPPORT
2936 bo->bo_tdma += adjust;
2938 #ifdef IEEE80211_MESH_SUPPORT
2939 bo->bo_meshconf += adjust;
2941 bo->bo_appie += adjust;
2942 bo->bo_wme += adjust;
2943 bo->bo_csa += adjust;
2944 bo->bo_tim_len = timlen;
2946 /* update information element */
2947 tie->tim_len = 3 + timlen;
2948 tie->tim_bitctl = timoff;
2951 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2954 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2956 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2957 "%s: TIM updated, pending %u, off %u, len %u\n",
2958 __func__, vap->iv_ps_pending, timoff, timlen);
2960 /* count down DTIM period */
2961 if (tie->tim_count == 0)
2962 tie->tim_count = tie->tim_period - 1;
2965 /* update state for buffered multicast frames on DTIM */
2966 if (mcast && tie->tim_count == 0)
2967 tie->tim_bitctl |= 1;
2969 tie->tim_bitctl &= ~1;
2970 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2971 struct ieee80211_csa_ie *csa =
2972 (struct ieee80211_csa_ie *) bo->bo_csa;
2975 * Insert or update CSA ie. If we're just starting
2976 * to count down to the channel switch then we need
2977 * to insert the CSA ie. Otherwise we just need to
2978 * drop the count. The actual change happens above
2979 * when the vap's count reaches the target count.
2981 if (vap->iv_csa_count == 0) {
2982 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2983 bo->bo_erp += sizeof(*csa);
2984 bo->bo_htinfo += sizeof(*csa);
2985 bo->bo_wme += sizeof(*csa);
2986 #ifdef IEEE80211_SUPERG_SUPPORT
2987 bo->bo_ath += sizeof(*csa);
2989 #ifdef IEEE80211_TDMA_SUPPORT
2990 bo->bo_tdma += sizeof(*csa);
2992 #ifdef IEEE80211_MESH_SUPPORT
2993 bo->bo_meshconf += sizeof(*csa);
2995 bo->bo_appie += sizeof(*csa);
2996 bo->bo_csa_trailer_len += sizeof(*csa);
2997 bo->bo_tim_trailer_len += sizeof(*csa);
2998 m->m_len += sizeof(*csa);
2999 m->m_pkthdr.len += sizeof(*csa);
3001 ieee80211_add_csa(bo->bo_csa, vap);
3004 vap->iv_csa_count++;
3005 /* NB: don't clear IEEE80211_BEACON_CSA */
3007 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3009 * ERP element needs updating.
3011 (void) ieee80211_add_erp(bo->bo_erp, ic);
3012 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3014 #ifdef IEEE80211_SUPPORT_SUPERG
3015 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3016 ieee80211_add_athcaps(bo->bo_ath, ni);
3017 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3021 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3022 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3028 aielen += aie->ie_len;
3029 if (aielen != bo->bo_appie_len) {
3030 /* copy up/down trailer */
3031 int adjust = aielen - bo->bo_appie_len;
3032 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3033 bo->bo_tim_trailer_len);
3034 bo->bo_tim_trailer += adjust;
3035 bo->bo_appie += adjust;
3036 bo->bo_appie_len = aielen;
3042 frm = add_appie(frm, aie);
3043 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
3045 IEEE80211_UNLOCK(ic);