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 $
30 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
39 #include <sys/socket.h>
42 #include <net/ethernet.h>
43 #include <net/route.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/ifq_var.h>
49 #include <netproto/802_11/ieee80211_var.h>
50 #include <netproto/802_11/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <netproto/802_11/ieee80211_superg.h>
54 #ifdef IEEE80211_SUPPORT_TDMA
55 #include <netproto/802_11/ieee80211_tdma.h>
57 #include <netproto/802_11/ieee80211_wds.h>
58 #include <netproto/802_11/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 #define ETHER_HEADER_COPY(dst, src) \
71 memcpy(dst, src, sizeof(struct ether_header))
73 /* unalligned little endian access */
74 #define LE_WRITE_2(p, v) do { \
75 ((uint8_t *)(p))[0] = (v) & 0xff; \
76 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
78 #define LE_WRITE_4(p, v) do { \
79 ((uint8_t *)(p))[0] = (v) & 0xff; \
80 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \
81 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \
82 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \
85 static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
86 u_int hdrsize, u_int ciphdrsize, u_int mtu);
87 static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
89 #ifdef IEEE80211_DEBUG
91 * Decide if an outbound management frame should be
92 * printed when debugging is enabled. This filters some
93 * of the less interesting frames that come frequently
97 doprint(struct ieee80211vap *vap, int subtype)
100 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
101 return (vap->iv_opmode == IEEE80211_M_IBSS);
108 * Start method for vap's. All packets from the stack come
109 * through here. We handle common processing of the packets
110 * before dispatching them to the underlying device.
113 ieee80211_start(struct ifnet *ifp)
115 #define IS_DWDS(vap) \
116 (vap->iv_opmode == IEEE80211_M_WDS && \
117 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
118 struct ieee80211vap *vap = ifp->if_softc;
119 struct ieee80211com *ic = vap->iv_ic;
120 struct ifnet *parent = ic->ic_ifp;
121 struct ieee80211_node *ni;
122 struct mbuf *m = NULL;
123 struct ether_header *eh;
126 /* NB: parent must be up and running */
127 if (!IFNET_IS_UP_RUNNING(parent)) {
128 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
129 "%s: ignore queue, parent %s not up+running\n",
130 __func__, parent->if_xname);
132 ifq_purge(&ifp->if_snd);
135 if (vap->iv_state == IEEE80211_S_SLEEP) {
137 * In power save, wakeup device for transmit.
139 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
140 ifq_purge(&ifp->if_snd);
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) {
150 /* re-check under the com lock to avoid races */
151 if (vap->iv_state != IEEE80211_S_RUN) {
152 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
153 "%s: ignore queue, in %s state\n",
154 __func__, ieee80211_state_name[vap->iv_state]);
155 vap->iv_stats.is_tx_badstate++;
156 ifq_set_oactive(&ifp->if_snd);
161 m = ifq_dequeue(&ifp->if_snd, NULL);
165 * Sanitize mbuf flags for net80211 use. We cannot
166 * clear M_PWR_SAV or M_MORE_DATA because these may
167 * be set for frames that are re-submitted from the
170 * NB: This must be done before ieee80211_classify as
171 * it marks EAPOL in frames with M_EAPOL.
173 m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
175 * Cancel any background scan.
177 if (ic->ic_flags & IEEE80211_F_SCAN)
178 ieee80211_cancel_anyscan(vap);
180 * Find the node for the destination so we can do
181 * things like power save and fast frames aggregation.
183 * NB: past this point various code assumes the first
184 * mbuf has the 802.3 header present (and contiguous).
187 if (m->m_len < sizeof(struct ether_header) &&
188 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
189 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
190 "discard frame, %s\n", "m_pullup failed");
191 vap->iv_stats.is_tx_nobuf++; /* XXX */
195 eh = mtod(m, struct ether_header *);
196 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
199 * Only unicast frames from the above go out
200 * DWDS vaps; multicast frames are handled by
201 * dispatching the frame as it comes through
202 * the AP vap (see below).
204 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
205 eh->ether_dhost, "mcast", "%s", "on DWDS");
206 vap->iv_stats.is_dwds_mcast++;
210 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
212 * Spam DWDS vap's w/ multicast traffic.
214 /* XXX only if dwds in use? */
215 ieee80211_dwds_mcast(vap, m);
218 #ifdef IEEE80211_SUPPORT_MESH
219 if (vap->iv_opmode != IEEE80211_M_MBSS) {
221 ni = ieee80211_find_txnode(vap, eh->ether_dhost);
223 /* NB: ieee80211_find_txnode does stat+msg */
228 if (ni->ni_associd == 0 &&
229 (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
230 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
231 eh->ether_dhost, NULL,
232 "sta not associated (type 0x%04x)",
233 htons(eh->ether_type));
234 vap->iv_stats.is_tx_notassoc++;
237 ieee80211_free_node(ni);
240 #ifdef IEEE80211_SUPPORT_MESH
242 if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
244 * Proxy station only if configured.
246 if (!ieee80211_mesh_isproxyena(vap)) {
247 IEEE80211_DISCARD_MAC(vap,
248 IEEE80211_MSG_OUTPUT |
250 eh->ether_dhost, NULL,
251 "%s", "proxy not enabled");
252 vap->iv_stats.is_mesh_notproxy++;
257 ieee80211_mesh_proxy_check(vap, eh->ether_shost);
259 ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
262 * NB: ieee80211_mesh_discover holds/disposes
263 * frame (e.g. queueing on path discovery).
270 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
271 (m->m_flags & M_PWR_SAV) == 0) {
273 * Station in power save mode; pass the frame
274 * to the 802.11 layer and continue. We'll get
275 * the frame back when the time is right.
276 * XXX lose WDS vap linkage?
278 (void) ieee80211_pwrsave(ni, m);
279 ieee80211_free_node(ni);
282 /* calculate priority so drivers can find the tx queue */
283 if (ieee80211_classify(ni, m)) {
284 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
285 eh->ether_dhost, NULL,
286 "%s", "classification failure");
287 vap->iv_stats.is_tx_classify++;
290 ieee80211_free_node(ni);
294 * Stash the node pointer. Note that we do this after
295 * any call to ieee80211_dwds_mcast because that code
296 * uses any existing value for rcvif to identify the
297 * interface it (might have been) received on.
299 m->m_pkthdr.rcvif = (void *)ni;
301 BPF_MTAP(ifp, m); /* 802.3 tx */
304 * Check if A-MPDU tx aggregation is setup or if we
305 * should try to enable it. The sta must be associated
306 * with HT and A-MPDU enabled for use. When the policy
307 * routine decides we should enable A-MPDU we issue an
308 * ADDBA request and wait for a reply. The frame being
309 * encapsulated will go out w/o using A-MPDU, or possibly
310 * it might be collected by the driver and held/retransmit.
311 * The default ic_ampdu_enable routine handles staggering
312 * ADDBA requests in case the receiver NAK's us or we are
313 * otherwise unable to establish a BA stream.
315 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
316 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
317 (m->m_flags & M_EAPOL) == 0) {
318 const int ac = M_WME_GETAC(m);
319 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac];
321 ieee80211_txampdu_count_packet(tap);
322 if (IEEE80211_AMPDU_RUNNING(tap)) {
324 * Operational, mark frame for aggregation.
326 * XXX do tx aggregation here
328 m->m_flags |= M_AMPDU_MPDU;
329 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
330 ic->ic_ampdu_enable(ni, tap)) {
332 * Not negotiated yet, request service.
334 ieee80211_ampdu_request(ni, tap);
335 /* XXX hold frame for reply? */
338 #ifdef IEEE80211_SUPPORT_SUPERG
339 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
340 m = ieee80211_ff_check(ni, m);
342 /* NB: any ni ref held on stageq */
346 #endif /* IEEE80211_SUPPORT_SUPERG */
347 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
349 * Encapsulate the packet in prep for transmission.
351 m = ieee80211_encap(vap, ni, m);
353 /* NB: stat+msg handled in ieee80211_encap */
354 ieee80211_free_node(ni);
359 error = ieee80211_handoff(parent, m);
361 /* NB: IFQ_HANDOFF reclaims mbuf */
362 ieee80211_free_node(ni);
366 ic->ic_lastdata = ticks;
373 * 802.11 output routine. This is (currently) used only to
374 * connect bpf write calls to the 802.11 layer for injecting
378 ieee80211_output(struct ifnet *ifp, struct mbuf *m,
379 struct sockaddr *dst, struct rtentry *rt)
381 #define senderr(e) do { error = (e); goto bad;} while (0)
382 struct ieee80211_node *ni = NULL;
383 struct ieee80211vap *vap;
384 struct ieee80211_frame *wh;
387 if (ifq_is_oactive(&ifp->if_snd)) {
389 * Short-circuit requests if the vap is marked OACTIVE
390 * as this can happen because a packet came down through
391 * ieee80211_start before the vap entered RUN state in
392 * which case it's ok to just drop the frame. This
393 * should not be necessary but callers of if_output don't
400 * Hand to the 802.3 code if not tagged as
401 * a raw 802.11 frame.
403 if (dst->sa_family != AF_IEEE80211)
404 return vap->iv_output(ifp, m, dst, rt);
406 error = mac_ifnet_check_transmit(ifp, m);
410 if (ifp->if_flags & IFF_MONITOR)
412 if (!IFNET_IS_UP_RUNNING(ifp))
414 if (vap->iv_state == IEEE80211_S_CAC) {
415 IEEE80211_DPRINTF(vap,
416 IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
417 "block %s frame in CAC state\n", "raw data");
418 vap->iv_stats.is_tx_badstate++;
419 senderr(EIO); /* XXX */
421 /* XXX bypass bridge, pfil, carp, etc. */
423 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
424 senderr(EIO); /* XXX */
425 wh = mtod(m, struct ieee80211_frame *);
426 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
427 IEEE80211_FC0_VERSION_0)
428 senderr(EIO); /* XXX */
430 /* locate destination node */
431 switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
432 case IEEE80211_FC1_DIR_NODS:
433 case IEEE80211_FC1_DIR_FROMDS:
434 ni = ieee80211_find_txnode(vap, wh->i_addr1);
436 case IEEE80211_FC1_DIR_TODS:
437 case IEEE80211_FC1_DIR_DSTODS:
438 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
439 senderr(EIO); /* XXX */
440 ni = ieee80211_find_txnode(vap, wh->i_addr3);
443 senderr(EIO); /* XXX */
447 * Permit packets w/ bpf params through regardless
448 * (see below about sa_len).
450 if (dst->sa_len == 0)
451 senderr(EHOSTUNREACH);
452 ni = ieee80211_ref_node(vap->iv_bss);
456 * Sanitize mbuf for net80211 flags leaked from above.
458 * NB: This must be done before ieee80211_classify as
459 * it marks EAPOL in frames with M_EAPOL.
461 m->m_flags &= ~M_80211_TX;
463 /* calculate priority so drivers can find the tx queue */
464 /* XXX assumes an 802.3 frame */
465 if (ieee80211_classify(ni, m))
466 senderr(EIO); /* XXX */
469 IEEE80211_NODE_STAT(ni, tx_data);
470 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
471 IEEE80211_NODE_STAT(ni, tx_mcast);
472 m->m_flags |= M_MCAST;
474 IEEE80211_NODE_STAT(ni, tx_ucast);
475 /* NB: ieee80211_encap does not include 802.11 header */
476 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
479 * NB: DLT_IEEE802_11_RADIO identifies the parameters are
480 * present by setting the sa_len field of the sockaddr (yes,
482 * NB: we assume sa_data is suitably aligned to cast.
484 return vap->iv_ic->ic_raw_xmit(ni, m,
485 (const struct ieee80211_bpf_params *)(dst->sa_len ?
486 dst->sa_data : NULL));
491 ieee80211_free_node(ni);
498 * Set the direction field and address fields of an outgoing
499 * frame. Note this should be called early on in constructing
500 * a frame as it sets i_fc[1]; other bits can then be or'd in.
503 ieee80211_send_setup(
504 struct ieee80211_node *ni,
507 const uint8_t sa[IEEE80211_ADDR_LEN],
508 const uint8_t da[IEEE80211_ADDR_LEN],
509 const uint8_t bssid[IEEE80211_ADDR_LEN])
511 #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
512 struct ieee80211vap *vap = ni->ni_vap;
513 struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
516 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
517 if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
518 switch (vap->iv_opmode) {
519 case IEEE80211_M_STA:
520 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
521 IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
522 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
523 IEEE80211_ADDR_COPY(wh->i_addr3, da);
525 case IEEE80211_M_IBSS:
526 case IEEE80211_M_AHDEMO:
527 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
528 IEEE80211_ADDR_COPY(wh->i_addr1, da);
529 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
530 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
532 case IEEE80211_M_HOSTAP:
533 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
534 IEEE80211_ADDR_COPY(wh->i_addr1, da);
535 IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
536 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
538 case IEEE80211_M_WDS:
539 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
540 IEEE80211_ADDR_COPY(wh->i_addr1, da);
541 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
542 IEEE80211_ADDR_COPY(wh->i_addr3, da);
543 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
545 case IEEE80211_M_MBSS:
546 #ifdef IEEE80211_SUPPORT_MESH
547 /* XXX add support for proxied addresses */
548 if (IEEE80211_IS_MULTICAST(da)) {
549 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
551 IEEE80211_ADDR_COPY(wh->i_addr1, da);
552 IEEE80211_ADDR_COPY(wh->i_addr2,
555 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
556 IEEE80211_ADDR_COPY(wh->i_addr1, da);
557 IEEE80211_ADDR_COPY(wh->i_addr2,
559 IEEE80211_ADDR_COPY(wh->i_addr3, da);
560 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
564 case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
568 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
569 IEEE80211_ADDR_COPY(wh->i_addr1, da);
570 IEEE80211_ADDR_COPY(wh->i_addr2, sa);
571 #ifdef IEEE80211_SUPPORT_MESH
572 if (vap->iv_opmode == IEEE80211_M_MBSS)
573 IEEE80211_ADDR_COPY(wh->i_addr3, sa);
576 IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
578 *(uint16_t *)&wh->i_dur[0] = 0;
580 seqno = ni->ni_txseqs[tid]++;
581 *(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
582 M_SEQNO_SET(m, seqno);
584 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
585 m->m_flags |= M_MCAST;
590 * Send a management frame to the specified node. The node pointer
591 * must have a reference as the pointer will be passed to the driver
592 * and potentially held for a long time. If the frame is successfully
593 * dispatched to the driver, then it is responsible for freeing the
594 * reference (and potentially free'ing up any associated storage);
595 * otherwise deal with reclaiming any reference (on error).
598 ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
599 struct ieee80211_bpf_params *params)
601 struct ieee80211vap *vap = ni->ni_vap;
602 struct ieee80211com *ic = ni->ni_ic;
603 struct ieee80211_frame *wh;
605 KASSERT(ni != NULL, ("null node"));
607 if (vap->iv_state == IEEE80211_S_CAC) {
608 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
609 ni, "block %s frame in CAC state",
610 ieee80211_mgt_subtype_name[
611 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
612 IEEE80211_FC0_SUBTYPE_SHIFT]);
613 vap->iv_stats.is_tx_badstate++;
614 ieee80211_free_node(ni);
616 return EIO; /* XXX */
619 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
621 ieee80211_free_node(ni);
625 wh = mtod(m, struct ieee80211_frame *);
626 ieee80211_send_setup(ni, m,
627 IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
628 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
629 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
630 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
631 "encrypting frame (%s)", __func__);
632 wh->i_fc[1] |= IEEE80211_FC1_WEP;
634 m->m_flags |= M_ENCAP; /* mark encapsulated */
636 KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
637 M_WME_SETAC(m, params->ibp_pri);
639 #ifdef IEEE80211_DEBUG
640 /* avoid printing too many frames */
641 if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
642 ieee80211_msg_dumppkts(vap)) {
643 kprintf("[%6D] send %s on channel %u\n",
645 ieee80211_mgt_subtype_name[
646 (type & IEEE80211_FC0_SUBTYPE_MASK) >>
647 IEEE80211_FC0_SUBTYPE_SHIFT],
648 ieee80211_chan2ieee(ic, ic->ic_curchan));
651 IEEE80211_NODE_STAT(ni, tx_mgmt);
653 return ic->ic_raw_xmit(ni, m, params);
657 * Send a null data frame to the specified node. If the station
658 * is setup for QoS then a QoS Null Data frame is constructed.
659 * If this is a WDS station then a 4-address frame is constructed.
661 * NB: the caller is assumed to have setup a node reference
662 * for use; this is necessary to deal with a race condition
663 * when probing for inactive stations. Like ieee80211_mgmt_output
664 * we must cleanup any node reference on error; however we
665 * can safely just unref it as we know it will never be the
666 * last reference to the node.
669 ieee80211_send_nulldata(struct ieee80211_node *ni)
671 struct ieee80211vap *vap = ni->ni_vap;
672 struct ieee80211com *ic = ni->ni_ic;
674 struct ieee80211_frame *wh;
678 if (vap->iv_state == IEEE80211_S_CAC) {
679 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
680 ni, "block %s frame in CAC state", "null data");
681 ieee80211_unref_node(&ni);
682 vap->iv_stats.is_tx_badstate++;
683 return EIO; /* XXX */
686 if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
687 hdrlen = sizeof(struct ieee80211_qosframe);
689 hdrlen = sizeof(struct ieee80211_frame);
690 /* NB: only WDS vap's get 4-address frames */
691 if (vap->iv_opmode == IEEE80211_M_WDS)
692 hdrlen += IEEE80211_ADDR_LEN;
693 if (ic->ic_flags & IEEE80211_F_DATAPAD)
694 hdrlen = roundup(hdrlen, sizeof(uint32_t));
696 m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
699 ieee80211_unref_node(&ni);
700 vap->iv_stats.is_tx_nobuf++;
703 KASSERT(M_LEADINGSPACE(m) >= hdrlen,
704 ("leading space %zd", M_LEADINGSPACE(m)));
705 M_PREPEND(m, hdrlen, MB_DONTWAIT);
707 /* NB: cannot happen */
708 ieee80211_free_node(ni);
712 wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
713 if (ni->ni_flags & IEEE80211_NODE_QOS) {
714 const int tid = WME_AC_TO_TID(WME_AC_BE);
717 ieee80211_send_setup(ni, m,
718 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
719 tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
721 if (vap->iv_opmode == IEEE80211_M_WDS)
722 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
724 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
725 qos[0] = tid & IEEE80211_QOS_TID;
726 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
727 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
730 ieee80211_send_setup(ni, m,
731 IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
732 IEEE80211_NONQOS_TID,
733 vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
735 if (vap->iv_opmode != IEEE80211_M_WDS) {
736 /* NB: power management bit is never sent by an AP */
737 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
738 vap->iv_opmode != IEEE80211_M_HOSTAP)
739 wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
741 m->m_len = m->m_pkthdr.len = hdrlen;
742 m->m_flags |= M_ENCAP; /* mark encapsulated */
744 M_WME_SETAC(m, WME_AC_BE);
746 IEEE80211_NODE_STAT(ni, tx_data);
748 IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
749 "send %snull data frame on channel %u, pwr mgt %s",
750 ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
751 ieee80211_chan2ieee(ic, ic->ic_curchan),
752 wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
754 return ic->ic_raw_xmit(ni, m, NULL);
758 * Assign priority to a frame based on any vlan tag assigned
759 * to the station and/or any Diffserv setting in an IP header.
760 * Finally, if an ACM policy is setup (in station mode) it's
764 ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
766 const struct ether_header *eh = mtod(m, struct ether_header *);
767 int v_wme_ac, d_wme_ac, ac;
770 * Always promote PAE/EAPOL frames to high priority.
772 if (eh->ether_type == htons(ETHERTYPE_PAE)) {
773 /* NB: mark so others don't need to check header */
774 m->m_flags |= M_EAPOL;
779 * Non-qos traffic goes to BE.
781 if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
787 * If node has a vlan tag then all traffic
788 * to it must have a matching tag.
791 if (ni->ni_vlan != 0) {
792 if ((m->m_flags & M_VLANTAG) == 0) {
793 IEEE80211_NODE_STAT(ni, tx_novlantag);
797 if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
798 EVL_VLANOFTAG(ni->ni_vlan)) {
799 IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
802 /* map vlan priority to AC */
803 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 struct mbuf *mnew = NULL;
884 int needed_space = vap->iv_ic->ic_headroom + hdrsize;
887 /* XXX belongs in crypto code? */
888 needed_space += key->wk_cipher->ic_header;
891 * When crypto is being done in the host we must insure
892 * the data are writable for the cipher routines; clone
893 * a writable mbuf chain.
894 * XXX handle SWMIC specially
896 if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
897 mnew = m_dup(m, MB_DONTWAIT);
899 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
900 "%s: cannot get writable mbuf\n", __func__);
901 vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
909 * We know we are called just before stripping an Ethernet
910 * header and prepending an LLC header. This means we know
912 * sizeof(struct ether_header) - sizeof(struct llc)
913 * bytes recovered to which we need additional space for the
914 * 802.11 header and any crypto header.
916 /* XXX check trailing space and copy instead? */
917 if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
918 struct mbuf *n = m_gethdr(MB_DONTWAIT, m->m_type);
920 IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
921 "%s: cannot expand storage\n", __func__);
922 vap->iv_stats.is_tx_nobuf++;
926 KASSERT(needed_space <= MHLEN,
927 ("not enough room, need %u got %zu", needed_space, MHLEN));
929 * Setup new mbuf to have leading space to prepend the
930 * 802.11 header and any crypto header bits that are
931 * required (the latter are added when the driver calls
932 * back to ieee80211_crypto_encap to do crypto encapsulation).
934 /* NB: must be first 'cuz it clobbers m_data */
936 n->m_len = 0; /* NB: m_gethdr does not set */
937 n->m_data += needed_space;
939 * Pull up Ethernet header to create the expected layout.
940 * We could use m_pullup but that's overkill (i.e. we don't
941 * need the actual data) and it cannot fail so do it inline
944 /* NB: struct ether_header is known to be contiguous */
945 n->m_len += sizeof(struct ether_header);
946 m->m_len -= sizeof(struct ether_header);
947 m->m_data += sizeof(struct ether_header);
949 * Replace the head of the chain.
955 #undef TO_BE_RECLAIMED
959 * Return the transmit key to use in sending a unicast frame.
960 * If a unicast key is set we use that. When no unicast key is set
961 * we fall back to the default transmit key.
963 static __inline struct ieee80211_key *
964 ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
965 struct ieee80211_node *ni)
967 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
968 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
969 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
971 return &vap->iv_nw_keys[vap->iv_def_txkey];
973 return &ni->ni_ucastkey;
978 * Return the transmit key to use in sending a multicast frame.
979 * Multicast traffic always uses the group key which is installed as
980 * the default tx key.
982 static __inline struct ieee80211_key *
983 ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
984 struct ieee80211_node *ni)
986 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
987 IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
989 return &vap->iv_nw_keys[vap->iv_def_txkey];
993 * Encapsulate an outbound data frame. The mbuf chain is updated.
994 * If an error is encountered NULL is returned. The caller is required
995 * to provide a node reference and pullup the ethernet header in the
998 * NB: Packet is assumed to be processed by ieee80211_classify which
999 * marked EAPOL frames w/ M_EAPOL.
1002 ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
1005 #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1006 struct ieee80211com *ic = ni->ni_ic;
1007 #ifdef IEEE80211_SUPPORT_MESH
1008 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1009 struct ieee80211_meshcntl_ae10 *mc;
1011 struct ether_header eh;
1012 struct ieee80211_frame *wh;
1013 struct ieee80211_key *key;
1015 int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
1016 ieee80211_seq seqno;
1017 int meshhdrsize, meshae;
1021 * Copy existing Ethernet header to a safe place. The
1022 * rest of the code assumes it's ok to strip it when
1023 * reorganizing state for the final encapsulation.
1025 KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
1026 ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1029 * Insure space for additional headers. First identify
1030 * transmit key to use in calculating any buffer adjustments
1031 * required. This is also used below to do privacy
1032 * encapsulation work. Then calculate the 802.11 header
1033 * size and any padding required by the driver.
1035 * Note key may be NULL if we fall back to the default
1036 * transmit key and that is not set. In that case the
1037 * buffer may not be expanded as needed by the cipher
1038 * routines, but they will/should discard it.
1040 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1041 if (vap->iv_opmode == IEEE80211_M_STA ||
1042 !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
1043 (vap->iv_opmode == IEEE80211_M_WDS &&
1044 (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
1045 key = ieee80211_crypto_getucastkey(vap, ni);
1047 key = ieee80211_crypto_getmcastkey(vap, ni);
1048 if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
1049 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
1051 "no default transmit key (%s) deftxkey %u",
1052 __func__, vap->iv_def_txkey);
1053 vap->iv_stats.is_tx_nodefkey++;
1059 * XXX Some ap's don't handle QoS-encapsulated EAPOL
1060 * frames so suppress use. This may be an issue if other
1061 * ap's require all data frames to be QoS-encapsulated
1062 * once negotiated in which case we'll need to make this
1065 addqos = (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) &&
1066 (m->m_flags & M_EAPOL) == 0;
1068 hdrsize = sizeof(struct ieee80211_qosframe);
1070 hdrsize = sizeof(struct ieee80211_frame);
1071 #ifdef IEEE80211_SUPPORT_MESH
1072 if (vap->iv_opmode == IEEE80211_M_MBSS) {
1074 * Mesh data frames are encapsulated according to the
1075 * rules of Section 11B.8.5 (p.139 of D3.0 spec).
1076 * o Group Addressed data (aka multicast) originating
1077 * at the local sta are sent w/ 3-address format and
1078 * address extension mode 00
1079 * o Individually Addressed data (aka unicast) originating
1080 * at the local sta are sent w/ 4-address format and
1081 * address extension mode 00
1082 * o Group Addressed data forwarded from a non-mesh sta are
1083 * sent w/ 3-address format and address extension mode 01
1084 * o Individually Address data from another sta are sent
1085 * w/ 4-address format and address extension mode 10
1087 is4addr = 0; /* NB: don't use, disable */
1088 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1089 hdrsize += IEEE80211_ADDR_LEN; /* unicast are 4-addr */
1090 meshhdrsize = sizeof(struct ieee80211_meshcntl);
1091 /* XXX defines for AE modes */
1092 if (IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1093 if (!IEEE80211_IS_MULTICAST(eh.ether_dhost))
1096 meshae = 4; /* NB: pseudo */
1097 } else if (IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1099 meshhdrsize += 1*IEEE80211_ADDR_LEN;
1102 meshhdrsize += 2*IEEE80211_ADDR_LEN;
1107 * 4-address frames need to be generated for:
1108 * o packets sent through a WDS vap (IEEE80211_M_WDS)
1109 * o packets sent through a vap marked for relaying
1110 * (e.g. a station operating with dynamic WDS)
1112 is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
1113 ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
1114 !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
1116 hdrsize += IEEE80211_ADDR_LEN;
1117 meshhdrsize = meshae = 0;
1118 #ifdef IEEE80211_SUPPORT_MESH
1122 * Honor driver DATAPAD requirement.
1124 if (ic->ic_flags & IEEE80211_F_DATAPAD)
1125 hdrspace = roundup(hdrsize, sizeof(uint32_t));
1129 if (__predict_true((m->m_flags & M_FF) == 0)) {
1133 m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1135 /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1138 /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1139 m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1140 llc = mtod(m, struct llc *);
1141 llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1142 llc->llc_control = LLC_UI;
1143 llc->llc_snap.org_code[0] = 0;
1144 llc->llc_snap.org_code[1] = 0;
1145 llc->llc_snap.org_code[2] = 0;
1146 llc->llc_snap.ether_type = eh.ether_type;
1148 #ifdef IEEE80211_SUPPORT_SUPERG
1152 m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
1157 datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1159 M_PREPEND(m, hdrspace + meshhdrsize, MB_DONTWAIT);
1161 vap->iv_stats.is_tx_nobuf++;
1164 wh = mtod(m, struct ieee80211_frame *);
1165 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1166 *(uint16_t *)wh->i_dur = 0;
1167 qos = NULL; /* NB: quiet compiler */
1169 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1170 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1171 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1172 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1173 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1174 } else switch (vap->iv_opmode) {
1175 case IEEE80211_M_STA:
1176 wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1177 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1178 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1179 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1181 case IEEE80211_M_IBSS:
1182 case IEEE80211_M_AHDEMO:
1183 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1184 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1185 IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1187 * NB: always use the bssid from iv_bss as the
1188 * neighbor's may be stale after an ibss merge
1190 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1192 case IEEE80211_M_HOSTAP:
1193 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1194 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1195 IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1196 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1198 #ifdef IEEE80211_SUPPORT_MESH
1199 case IEEE80211_M_MBSS:
1200 /* NB: offset by hdrspace to deal with DATAPAD */
1201 mc = (struct ieee80211_meshcntl_ae10 *)
1202 (mtod(m, uint8_t *) + hdrspace);
1204 case 0: /* ucast, no proxy */
1205 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1206 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1207 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1208 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1209 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
1211 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1213 case 4: /* mcast, no proxy */
1214 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1215 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1216 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1217 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1218 mc->mc_flags = 0; /* NB: AE is really 0 */
1219 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1221 case 1: /* mcast, proxy */
1222 wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1223 IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1224 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1225 IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
1227 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_shost);
1228 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1230 case 2: /* ucast, proxy */
1231 wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
1232 IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
1233 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1234 /* XXX not right, need MeshDA */
1235 IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1236 /* XXX assume are MeshSA */
1237 IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1239 IEEE80211_ADDR_COPY(mc->mc_addr4, eh.ether_dhost);
1240 IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_shost);
1241 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1244 KASSERT(0, ("meshae %d", meshae));
1247 mc->mc_ttl = ms->ms_ttl;
1249 LE_WRITE_4(mc->mc_seq, ms->ms_seq);
1252 case IEEE80211_M_WDS: /* NB: is4addr should always be true */
1256 if (m->m_flags & M_MORE_DATA)
1257 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1262 qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
1263 /* NB: mesh case handled earlier */
1264 } else if (vap->iv_opmode != IEEE80211_M_MBSS)
1265 qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1266 ac = M_WME_GETAC(m);
1267 /* map from access class/queue to 11e header priorty value */
1268 tid = WME_AC_TO_TID(ac);
1269 qos[0] = tid & IEEE80211_QOS_TID;
1270 if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
1271 qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1273 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1275 if ((m->m_flags & M_AMPDU_MPDU) == 0) {
1277 * NB: don't assign a sequence # to potential
1278 * aggregates; we expect this happens at the
1279 * point the frame comes off any aggregation q
1280 * as otherwise we may introduce holes in the
1281 * BA sequence space and/or make window accouting
1284 * XXX may want to control this with a driver
1285 * capability; this may also change when we pull
1286 * aggregation up into net80211
1288 seqno = ni->ni_txseqs[tid]++;
1289 *(uint16_t *)wh->i_seq =
1290 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1291 M_SEQNO_SET(m, seqno);
1294 seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
1295 *(uint16_t *)wh->i_seq =
1296 htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
1297 M_SEQNO_SET(m, seqno);
1301 /* check if xmit fragmentation is required */
1302 txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
1303 !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1304 (vap->iv_caps & IEEE80211_C_TXFRAG) &&
1305 (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1308 * IEEE 802.1X: send EAPOL frames always in the clear.
1309 * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1311 if ((m->m_flags & M_EAPOL) == 0 ||
1312 ((vap->iv_flags & IEEE80211_F_WPA) &&
1313 (vap->iv_opmode == IEEE80211_M_STA ?
1314 !IEEE80211_KEY_UNDEFINED(key) :
1315 !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1316 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1317 if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
1318 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
1320 "%s", "enmic failed, discard frame");
1321 vap->iv_stats.is_crypto_enmicfail++;
1326 if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
1327 key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
1330 m->m_flags |= M_ENCAP; /* mark encapsulated */
1332 IEEE80211_NODE_STAT(ni, tx_data);
1333 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1334 IEEE80211_NODE_STAT(ni, tx_mcast);
1335 m->m_flags |= M_MCAST;
1337 IEEE80211_NODE_STAT(ni, tx_ucast);
1338 IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1349 * Fragment the frame according to the specified mtu.
1350 * The size of the 802.11 header (w/o padding) is provided
1351 * so we don't need to recalculate it. We create a new
1352 * mbuf for each fragment and chain it through m_nextpkt;
1353 * we might be able to optimize this by reusing the original
1354 * packet's mbufs but that is significantly more complicated.
1357 ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
1358 u_int hdrsize, u_int ciphdrsize, u_int mtu)
1360 struct ieee80211_frame *wh, *whf;
1361 struct mbuf *m, *prev, *next;
1362 u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1364 KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
1365 KASSERT(m0->m_pkthdr.len > mtu,
1366 ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
1368 wh = mtod(m0, struct ieee80211_frame *);
1369 /* NB: mark the first frag; it will be propagated below */
1370 wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1371 totalhdrsize = hdrsize + ciphdrsize;
1373 off = mtu - ciphdrsize;
1374 remainder = m0->m_pkthdr.len - off;
1377 fragsize = totalhdrsize + remainder;
1380 /* XXX fragsize can be >2048! */
1381 KASSERT(fragsize < MCLBYTES,
1382 ("fragment size %u too big!", fragsize));
1383 if (fragsize > MHLEN)
1384 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1386 m = m_gethdr(MB_DONTWAIT, MT_DATA);
1389 /* leave room to prepend any cipher header */
1390 m_align(m, fragsize - ciphdrsize);
1393 * Form the header in the fragment. Note that since
1394 * we mark the first fragment with the MORE_FRAG bit
1395 * it automatically is propagated to each fragment; we
1396 * need only clear it on the last fragment (done below).
1398 whf = mtod(m, struct ieee80211_frame *);
1399 memcpy(whf, wh, hdrsize);
1400 *(uint16_t *)&whf->i_seq[0] |= htole16(
1401 (fragno & IEEE80211_SEQ_FRAG_MASK) <<
1402 IEEE80211_SEQ_FRAG_SHIFT);
1405 payload = fragsize - totalhdrsize;
1406 /* NB: destination is known to be contiguous */
1407 m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrsize);
1408 m->m_len = hdrsize + payload;
1409 m->m_pkthdr.len = hdrsize + payload;
1410 m->m_flags |= M_FRAG;
1411 m->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1413 /* chain up the fragment */
1414 prev->m_nextpkt = m;
1417 /* deduct fragment just formed */
1418 remainder -= payload;
1420 } while (remainder != 0);
1422 /* set the last fragment */
1423 m->m_flags |= M_LASTFRAG;
1424 whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
1426 /* strip first mbuf now that everything has been copied */
1427 m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
1428 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1430 vap->iv_stats.is_tx_fragframes++;
1431 vap->iv_stats.is_tx_frags += fragno-1;
1435 /* reclaim fragments but leave original frame for caller to free */
1436 for (m = m0->m_nextpkt; m != NULL; m = next) {
1437 next = m->m_nextpkt;
1438 m->m_nextpkt = NULL; /* XXX paranoid */
1441 m0->m_nextpkt = NULL;
1446 * Add a supported rates element id to a frame.
1449 ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1453 *frm++ = IEEE80211_ELEMID_RATES;
1454 nrates = rs->rs_nrates;
1455 if (nrates > IEEE80211_RATE_SIZE)
1456 nrates = IEEE80211_RATE_SIZE;
1458 memcpy(frm, rs->rs_rates, nrates);
1459 return frm + nrates;
1463 * Add an extended supported rates element id to a frame.
1466 ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1469 * Add an extended supported rates element if operating in 11g mode.
1471 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1472 int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1473 *frm++ = IEEE80211_ELEMID_XRATES;
1475 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1482 * Add an ssid element to a frame.
1485 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1487 *frm++ = IEEE80211_ELEMID_SSID;
1489 memcpy(frm, ssid, len);
1494 * Add an erp element to a frame.
1497 ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1501 *frm++ = IEEE80211_ELEMID_ERP;
1504 if (ic->ic_nonerpsta != 0)
1505 erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1506 if (ic->ic_flags & IEEE80211_F_USEPROT)
1507 erp |= IEEE80211_ERP_USE_PROTECTION;
1508 if (ic->ic_flags & IEEE80211_F_USEBARKER)
1509 erp |= IEEE80211_ERP_LONG_PREAMBLE;
1515 * Add a CFParams element to a frame.
1518 ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1520 #define ADDSHORT(frm, v) do { \
1521 LE_WRITE_2(frm, v); \
1524 *frm++ = IEEE80211_ELEMID_CFPARMS;
1526 *frm++ = 0; /* CFP count */
1527 *frm++ = 2; /* CFP period */
1528 ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
1529 ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1534 static __inline uint8_t *
1535 add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
1537 memcpy(frm, ie->ie_data, ie->ie_len);
1538 return frm + ie->ie_len;
1541 static __inline uint8_t *
1542 add_ie(uint8_t *frm, const uint8_t *ie)
1544 memcpy(frm, ie, 2 + ie[1]);
1545 return frm + 2 + ie[1];
1548 #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1550 * Add a WME information element to a frame.
1553 ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1555 static const struct ieee80211_wme_info info = {
1556 .wme_id = IEEE80211_ELEMID_VENDOR,
1557 .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1558 .wme_oui = { WME_OUI_BYTES },
1559 .wme_type = WME_OUI_TYPE,
1560 .wme_subtype = WME_INFO_OUI_SUBTYPE,
1561 .wme_version = WME_VERSION,
1564 memcpy(frm, &info, sizeof(info));
1565 return frm + sizeof(info);
1569 * Add a WME parameters element to a frame.
1572 ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
1574 #define SM(_v, _f) (((_v) << _f##_S) & _f)
1575 #define ADDSHORT(frm, v) do { \
1576 LE_WRITE_2(frm, v); \
1579 /* NB: this works 'cuz a param has an info at the front */
1580 static const struct ieee80211_wme_info param = {
1581 .wme_id = IEEE80211_ELEMID_VENDOR,
1582 .wme_len = sizeof(struct ieee80211_wme_param) - 2,
1583 .wme_oui = { WME_OUI_BYTES },
1584 .wme_type = WME_OUI_TYPE,
1585 .wme_subtype = WME_PARAM_OUI_SUBTYPE,
1586 .wme_version = WME_VERSION,
1590 memcpy(frm, ¶m, sizeof(param));
1591 frm += __offsetof(struct ieee80211_wme_info, wme_info);
1592 *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
1593 *frm++ = 0; /* reserved field */
1594 for (i = 0; i < WME_NUM_AC; i++) {
1595 const struct wmeParams *ac =
1596 &wme->wme_bssChanParams.cap_wmeParams[i];
1597 *frm++ = SM(i, WME_PARAM_ACI)
1598 | SM(ac->wmep_acm, WME_PARAM_ACM)
1599 | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
1601 *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
1602 | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
1604 ADDSHORT(frm, ac->wmep_txopLimit);
1610 #undef WME_OUI_BYTES
1613 * Add an 11h Power Constraint element to a frame.
1616 ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
1618 const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
1619 /* XXX per-vap tx power limit? */
1620 int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
1622 frm[0] = IEEE80211_ELEMID_PWRCNSTR;
1624 frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
1629 * Add an 11h Power Capability element to a frame.
1632 ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
1634 frm[0] = IEEE80211_ELEMID_PWRCAP;
1636 frm[2] = c->ic_minpower;
1637 frm[3] = c->ic_maxpower;
1642 * Add an 11h Supported Channels element to a frame.
1645 ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
1647 static const int ielen = 26;
1649 frm[0] = IEEE80211_ELEMID_SUPPCHAN;
1651 /* XXX not correct */
1652 memcpy(frm+2, ic->ic_chan_avail, ielen);
1653 return frm + 2 + ielen;
1657 * Add an 11h Channel Switch Announcement element to a frame.
1658 * Note that we use the per-vap CSA count to adjust the global
1659 * counter so we can use this routine to form probe response
1660 * frames and get the current count.
1663 ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
1665 struct ieee80211com *ic = vap->iv_ic;
1666 struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
1668 csa->csa_ie = IEEE80211_ELEMID_CSA;
1670 csa->csa_mode = 1; /* XXX force quiet on channel */
1671 csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
1672 csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
1673 return frm + sizeof(*csa);
1677 * Add an 11h country information element to a frame.
1680 ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
1683 if (ic->ic_countryie == NULL ||
1684 ic->ic_countryie_chan != ic->ic_bsschan) {
1686 * Handle lazy construction of ie. This is done on
1687 * first use and after a channel change that requires
1690 if (ic->ic_countryie != NULL)
1691 kfree(ic->ic_countryie, M_80211_NODE_IE);
1692 ic->ic_countryie = ieee80211_alloc_countryie(ic);
1693 if (ic->ic_countryie == NULL)
1695 ic->ic_countryie_chan = ic->ic_bsschan;
1697 return add_appie(frm, ic->ic_countryie);
1701 * Send a probe request frame with the specified ssid
1702 * and any optional information element data.
1705 ieee80211_send_probereq(struct ieee80211_node *ni,
1706 const uint8_t sa[IEEE80211_ADDR_LEN],
1707 const uint8_t da[IEEE80211_ADDR_LEN],
1708 const uint8_t bssid[IEEE80211_ADDR_LEN],
1709 const uint8_t *ssid, size_t ssidlen)
1711 struct ieee80211vap *vap = ni->ni_vap;
1712 struct ieee80211com *ic = ni->ni_ic;
1713 const struct ieee80211_txparam *tp;
1714 struct ieee80211_bpf_params params;
1715 const struct ieee80211_rateset *rs;
1719 if (vap->iv_state == IEEE80211_S_CAC) {
1720 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
1721 "block %s frame in CAC state", "probe request");
1722 vap->iv_stats.is_tx_badstate++;
1723 return EIO; /* XXX */
1727 * Hold a reference on the node so it doesn't go away until after
1728 * the xmit is complete all the way in the driver. On error we
1729 * will remove our reference.
1731 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1732 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1734 ni, ni->ni_macaddr, ":",
1735 ieee80211_node_refcnt(ni)+1);
1736 ieee80211_ref_node(ni);
1739 * prreq frame format
1741 * [tlv] supported rates
1742 * [tlv] RSN (optional)
1743 * [tlv] extended supported rates
1744 * [tlv] WPA (optional)
1745 * [tlv] user-specified ie's
1747 m = ieee80211_getmgtframe(&frm,
1748 ic->ic_headroom + sizeof(struct ieee80211_frame),
1749 2 + IEEE80211_NWID_LEN
1750 + 2 + IEEE80211_RATE_SIZE
1751 + sizeof(struct ieee80211_ie_wpa)
1752 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1753 + sizeof(struct ieee80211_ie_wpa)
1754 + (vap->iv_appie_probereq != NULL ?
1755 vap->iv_appie_probereq->ie_len : 0)
1758 vap->iv_stats.is_tx_nobuf++;
1759 ieee80211_free_node(ni);
1763 frm = ieee80211_add_ssid(frm, ssid, ssidlen);
1764 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1765 frm = ieee80211_add_rates(frm, rs);
1766 if (vap->iv_flags & IEEE80211_F_WPA2) {
1767 if (vap->iv_rsn_ie != NULL)
1768 frm = add_ie(frm, vap->iv_rsn_ie);
1769 /* XXX else complain? */
1771 frm = ieee80211_add_xrates(frm, rs);
1772 if (vap->iv_flags & IEEE80211_F_WPA1) {
1773 if (vap->iv_wpa_ie != NULL)
1774 frm = add_ie(frm, vap->iv_wpa_ie);
1775 /* XXX else complain? */
1777 if (vap->iv_appie_probereq != NULL)
1778 frm = add_appie(frm, vap->iv_appie_probereq);
1779 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1781 KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
1782 ("leading space %zd", M_LEADINGSPACE(m)));
1783 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
1785 /* NB: cannot happen */
1786 ieee80211_free_node(ni);
1790 ieee80211_send_setup(ni, m,
1791 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
1792 IEEE80211_NONQOS_TID, sa, da, bssid);
1793 /* XXX power management? */
1794 m->m_flags |= M_ENCAP; /* mark encapsulated */
1796 M_WME_SETAC(m, WME_AC_BE);
1798 IEEE80211_NODE_STAT(ni, tx_probereq);
1799 IEEE80211_NODE_STAT(ni, tx_mgmt);
1801 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
1802 "send probe req on channel %u bssid %6D ssid \"%.*s\"\n",
1803 ieee80211_chan2ieee(ic, ic->ic_curchan), bssid, ":",
1804 (int)ssidlen, ssid);
1806 memset(¶ms, 0, sizeof(params));
1807 params.ibp_pri = M_WME_GETAC(m);
1808 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1809 params.ibp_rate0 = tp->mgmtrate;
1810 if (IEEE80211_IS_MULTICAST(da)) {
1811 params.ibp_flags |= IEEE80211_BPF_NOACK;
1812 params.ibp_try0 = 1;
1814 params.ibp_try0 = tp->maxretry;
1815 params.ibp_power = ni->ni_txpower;
1816 return ic->ic_raw_xmit(ni, m, ¶ms);
1820 * Calculate capability information for mgt frames.
1823 ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
1825 struct ieee80211com *ic = vap->iv_ic;
1828 KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
1830 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1831 capinfo = IEEE80211_CAPINFO_ESS;
1832 else if (vap->iv_opmode == IEEE80211_M_IBSS)
1833 capinfo = IEEE80211_CAPINFO_IBSS;
1836 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1837 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1838 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1839 IEEE80211_IS_CHAN_2GHZ(chan))
1840 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1841 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1842 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1843 if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
1844 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
1849 * Send a management frame. The node is for the destination (or ic_bss
1850 * when in station mode). Nodes other than ic_bss have their reference
1851 * count bumped to reflect our use for an indeterminant time.
1854 ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
1856 #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
1857 #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
1858 struct ieee80211vap *vap = ni->ni_vap;
1859 struct ieee80211com *ic = ni->ni_ic;
1860 struct ieee80211_node *bss = vap->iv_bss;
1861 struct ieee80211_bpf_params params;
1865 int has_challenge, is_shared_key, ret, status;
1867 KASSERT(ni != NULL, ("null node"));
1870 * Hold a reference on the node so it doesn't go away until after
1871 * the xmit is complete all the way in the driver. On error we
1872 * will remove our reference.
1874 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1875 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
1877 ni, ni->ni_macaddr, ":",
1878 ieee80211_node_refcnt(ni)+1);
1879 ieee80211_ref_node(ni);
1881 memset(¶ms, 0, sizeof(params));
1884 case IEEE80211_FC0_SUBTYPE_AUTH:
1887 has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
1888 arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
1889 ni->ni_challenge != NULL);
1892 * Deduce whether we're doing open authentication or
1893 * shared key authentication. We do the latter if
1894 * we're in the middle of a shared key authentication
1895 * handshake or if we're initiating an authentication
1896 * request and configured to use shared key.
1898 is_shared_key = has_challenge ||
1899 arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
1900 (arg == IEEE80211_AUTH_SHARED_REQUEST &&
1901 bss->ni_authmode == IEEE80211_AUTH_SHARED);
1903 m = ieee80211_getmgtframe(&frm,
1904 ic->ic_headroom + sizeof(struct ieee80211_frame),
1905 3 * sizeof(uint16_t)
1906 + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
1907 sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
1910 senderr(ENOMEM, is_tx_nobuf);
1912 ((uint16_t *)frm)[0] =
1913 (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
1914 : htole16(IEEE80211_AUTH_ALG_OPEN);
1915 ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
1916 ((uint16_t *)frm)[2] = htole16(status);/* status */
1918 if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
1919 ((uint16_t *)frm)[3] =
1920 htole16((IEEE80211_CHALLENGE_LEN << 8) |
1921 IEEE80211_ELEMID_CHALLENGE);
1922 memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
1923 IEEE80211_CHALLENGE_LEN);
1924 m->m_pkthdr.len = m->m_len =
1925 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
1926 if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
1927 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1928 "request encrypt frame (%s)", __func__);
1929 /* mark frame for encryption */
1930 params.ibp_flags |= IEEE80211_BPF_CRYPTO;
1933 m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
1935 /* XXX not right for shared key */
1936 if (status == IEEE80211_STATUS_SUCCESS)
1937 IEEE80211_NODE_STAT(ni, tx_auth);
1939 IEEE80211_NODE_STAT(ni, tx_auth_fail);
1941 if (vap->iv_opmode == IEEE80211_M_STA)
1942 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
1943 (void *) vap->iv_state);
1946 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1947 IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
1948 "send station deauthenticate (reason %d)", arg);
1949 m = ieee80211_getmgtframe(&frm,
1950 ic->ic_headroom + sizeof(struct ieee80211_frame),
1953 senderr(ENOMEM, is_tx_nobuf);
1954 *(uint16_t *)frm = htole16(arg); /* reason */
1955 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
1957 IEEE80211_NODE_STAT(ni, tx_deauth);
1958 IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
1960 ieee80211_node_unauthorize(ni); /* port closed */
1963 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1964 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1966 * asreq frame format
1967 * [2] capability information
1968 * [2] listen interval
1969 * [6*] current AP address (reassoc only)
1971 * [tlv] supported rates
1972 * [tlv] extended supported rates
1973 * [4] power capability (optional)
1974 * [28] supported channels (optional)
1975 * [tlv] HT capabilities
1976 * [tlv] WME (optional)
1977 * [tlv] Vendor OUI HT capabilities (optional)
1978 * [tlv] Atheros capabilities (if negotiated)
1979 * [tlv] AppIE's (optional)
1981 m = ieee80211_getmgtframe(&frm,
1982 ic->ic_headroom + sizeof(struct ieee80211_frame),
1985 + IEEE80211_ADDR_LEN
1986 + 2 + IEEE80211_NWID_LEN
1987 + 2 + IEEE80211_RATE_SIZE
1988 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1991 + sizeof(struct ieee80211_wme_info)
1992 + sizeof(struct ieee80211_ie_htcap)
1993 + 4 + sizeof(struct ieee80211_ie_htcap)
1994 #ifdef IEEE80211_SUPPORT_SUPERG
1995 + sizeof(struct ieee80211_ath_ie)
1997 + (vap->iv_appie_wpa != NULL ?
1998 vap->iv_appie_wpa->ie_len : 0)
1999 + (vap->iv_appie_assocreq != NULL ?
2000 vap->iv_appie_assocreq->ie_len : 0)
2003 senderr(ENOMEM, is_tx_nobuf);
2005 KASSERT(vap->iv_opmode == IEEE80211_M_STA,
2006 ("wrong mode %u", vap->iv_opmode));
2007 capinfo = IEEE80211_CAPINFO_ESS;
2008 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2009 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2011 * NB: Some 11a AP's reject the request when
2012 * short premable is set.
2014 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2015 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2016 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2017 if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2018 (ic->ic_caps & IEEE80211_C_SHSLOT))
2019 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2020 if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
2021 (vap->iv_flags & IEEE80211_F_DOTH))
2022 capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2023 *(uint16_t *)frm = htole16(capinfo);
2026 KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
2027 *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
2031 if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
2032 IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2033 frm += IEEE80211_ADDR_LEN;
2036 frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
2037 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2038 if (vap->iv_flags & IEEE80211_F_WPA2) {
2039 if (vap->iv_rsn_ie != NULL)
2040 frm = add_ie(frm, vap->iv_rsn_ie);
2041 /* XXX else complain? */
2043 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2044 if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
2045 frm = ieee80211_add_powercapability(frm,
2047 frm = ieee80211_add_supportedchannels(frm, ic);
2049 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2050 ni->ni_ies.htcap_ie != NULL &&
2051 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP)
2052 frm = ieee80211_add_htcap(frm, ni);
2053 if (vap->iv_flags & IEEE80211_F_WPA1) {
2054 if (vap->iv_wpa_ie != NULL)
2055 frm = add_ie(frm, vap->iv_wpa_ie);
2056 /* XXX else complain */
2058 if ((ic->ic_flags & IEEE80211_F_WME) &&
2059 ni->ni_ies.wme_ie != NULL)
2060 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
2061 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
2062 ni->ni_ies.htcap_ie != NULL &&
2063 ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR)
2064 frm = ieee80211_add_htcap_vendor(frm, ni);
2065 #ifdef IEEE80211_SUPPORT_SUPERG
2066 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
2067 frm = ieee80211_add_ath(frm,
2068 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2069 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2070 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2071 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2073 #endif /* IEEE80211_SUPPORT_SUPERG */
2074 if (vap->iv_appie_assocreq != NULL)
2075 frm = add_appie(frm, vap->iv_appie_assocreq);
2076 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2078 ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
2079 (void *) vap->iv_state);
2082 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2083 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2085 * asresp frame format
2086 * [2] capability information
2088 * [2] association ID
2089 * [tlv] supported rates
2090 * [tlv] extended supported rates
2091 * [tlv] HT capabilities (standard, if STA enabled)
2092 * [tlv] HT information (standard, if STA enabled)
2093 * [tlv] WME (if configured and STA enabled)
2094 * [tlv] HT capabilities (vendor OUI, if STA enabled)
2095 * [tlv] HT information (vendor OUI, if STA enabled)
2096 * [tlv] Atheros capabilities (if STA enabled)
2097 * [tlv] AppIE's (optional)
2099 m = ieee80211_getmgtframe(&frm,
2100 ic->ic_headroom + sizeof(struct ieee80211_frame),
2104 + 2 + IEEE80211_RATE_SIZE
2105 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2106 + sizeof(struct ieee80211_ie_htcap) + 4
2107 + sizeof(struct ieee80211_ie_htinfo) + 4
2108 + sizeof(struct ieee80211_wme_param)
2109 #ifdef IEEE80211_SUPPORT_SUPERG
2110 + sizeof(struct ieee80211_ath_ie)
2112 + (vap->iv_appie_assocresp != NULL ?
2113 vap->iv_appie_assocresp->ie_len : 0)
2116 senderr(ENOMEM, is_tx_nobuf);
2118 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2119 *(uint16_t *)frm = htole16(capinfo);
2122 *(uint16_t *)frm = htole16(arg); /* status */
2125 if (arg == IEEE80211_STATUS_SUCCESS) {
2126 *(uint16_t *)frm = htole16(ni->ni_associd);
2127 IEEE80211_NODE_STAT(ni, tx_assoc);
2129 IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2132 frm = ieee80211_add_rates(frm, &ni->ni_rates);
2133 frm = ieee80211_add_xrates(frm, &ni->ni_rates);
2134 /* NB: respond according to what we received */
2135 if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
2136 frm = ieee80211_add_htcap(frm, ni);
2137 frm = ieee80211_add_htinfo(frm, ni);
2139 if ((vap->iv_flags & IEEE80211_F_WME) &&
2140 ni->ni_ies.wme_ie != NULL)
2141 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2142 if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
2143 frm = ieee80211_add_htcap_vendor(frm, ni);
2144 frm = ieee80211_add_htinfo_vendor(frm, ni);
2146 #ifdef IEEE80211_SUPPORT_SUPERG
2147 if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
2148 frm = ieee80211_add_ath(frm,
2149 IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
2150 ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
2151 ni->ni_authmode != IEEE80211_AUTH_8021X) ?
2152 vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
2153 #endif /* IEEE80211_SUPPORT_SUPERG */
2154 if (vap->iv_appie_assocresp != NULL)
2155 frm = add_appie(frm, vap->iv_appie_assocresp);
2156 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2159 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2160 IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
2161 "send station disassociate (reason %d)", arg);
2162 m = ieee80211_getmgtframe(&frm,
2163 ic->ic_headroom + sizeof(struct ieee80211_frame),
2166 senderr(ENOMEM, is_tx_nobuf);
2167 *(uint16_t *)frm = htole16(arg); /* reason */
2168 m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2170 IEEE80211_NODE_STAT(ni, tx_disassoc);
2171 IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2175 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
2176 "invalid mgmt frame type %u", type);
2177 senderr(EINVAL, is_tx_unknownmgt);
2181 /* NB: force non-ProbeResp frames to the highest queue */
2182 params.ibp_pri = WME_AC_VO;
2183 params.ibp_rate0 = bss->ni_txparms->mgmtrate;
2184 /* NB: we know all frames are unicast */
2185 params.ibp_try0 = bss->ni_txparms->maxretry;
2186 params.ibp_power = bss->ni_txpower;
2187 return ieee80211_mgmt_output(ni, m, type, ¶ms);
2189 ieee80211_free_node(ni);
2196 * Return an mbuf with a probe response frame in it.
2197 * Space is left to prepend and 802.11 header at the
2198 * front but it's left to the caller to fill in.
2201 ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
2203 struct ieee80211vap *vap = bss->ni_vap;
2204 struct ieee80211com *ic = bss->ni_ic;
2205 const struct ieee80211_rateset *rs;
2211 * probe response frame format
2213 * [2] beacon interval
2214 * [2] cabability information
2216 * [tlv] supported rates
2217 * [tlv] parameter set (FH/DS)
2218 * [tlv] parameter set (IBSS)
2219 * [tlv] country (optional)
2220 * [3] power control (optional)
2221 * [5] channel switch announcement (CSA) (optional)
2222 * [tlv] extended rate phy (ERP)
2223 * [tlv] extended supported rates
2224 * [tlv] RSN (optional)
2225 * [tlv] HT capabilities
2226 * [tlv] HT information
2227 * [tlv] WPA (optional)
2228 * [tlv] WME (optional)
2229 * [tlv] Vendor OUI HT capabilities (optional)
2230 * [tlv] Vendor OUI HT information (optional)
2231 * [tlv] Atheros capabilities
2232 * [tlv] AppIE's (optional)
2233 * [tlv] Mesh ID (MBSS)
2234 * [tlv] Mesh Conf (MBSS)
2236 m = ieee80211_getmgtframe(&frm,
2237 ic->ic_headroom + sizeof(struct ieee80211_frame),
2241 + 2 + IEEE80211_NWID_LEN
2242 + 2 + IEEE80211_RATE_SIZE
2244 + IEEE80211_COUNTRY_MAX_SIZE
2246 + sizeof(struct ieee80211_csa_ie)
2248 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2249 + sizeof(struct ieee80211_ie_wpa)
2250 + sizeof(struct ieee80211_ie_htcap)
2251 + sizeof(struct ieee80211_ie_htinfo)
2252 + sizeof(struct ieee80211_ie_wpa)
2253 + sizeof(struct ieee80211_wme_param)
2254 + 4 + sizeof(struct ieee80211_ie_htcap)
2255 + 4 + sizeof(struct ieee80211_ie_htinfo)
2256 #ifdef IEEE80211_SUPPORT_SUPERG
2257 + sizeof(struct ieee80211_ath_ie)
2259 #ifdef IEEE80211_SUPPORT_MESH
2260 + 2 + IEEE80211_MESHID_LEN
2261 + sizeof(struct ieee80211_meshconf_ie)
2263 + (vap->iv_appie_proberesp != NULL ?
2264 vap->iv_appie_proberesp->ie_len : 0)
2267 vap->iv_stats.is_tx_nobuf++;
2271 memset(frm, 0, 8); /* timestamp should be filled later */
2273 *(uint16_t *)frm = htole16(bss->ni_intval);
2275 capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2276 *(uint16_t *)frm = htole16(capinfo);
2279 frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
2280 rs = ieee80211_get_suprates(ic, bss->ni_chan);
2281 frm = ieee80211_add_rates(frm, rs);
2283 if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
2284 *frm++ = IEEE80211_ELEMID_FHPARMS;
2286 *frm++ = bss->ni_fhdwell & 0x00ff;
2287 *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
2288 *frm++ = IEEE80211_FH_CHANSET(
2289 ieee80211_chan2ieee(ic, bss->ni_chan));
2290 *frm++ = IEEE80211_FH_CHANPAT(
2291 ieee80211_chan2ieee(ic, bss->ni_chan));
2292 *frm++ = bss->ni_fhindex;
2294 *frm++ = IEEE80211_ELEMID_DSPARMS;
2296 *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
2299 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2300 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2302 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2304 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2305 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2306 frm = ieee80211_add_countryie(frm, ic);
2307 if (vap->iv_flags & IEEE80211_F_DOTH) {
2308 if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
2309 frm = ieee80211_add_powerconstraint(frm, vap);
2310 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2311 frm = ieee80211_add_csa(frm, vap);
2313 if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
2314 frm = ieee80211_add_erp(frm, ic);
2315 frm = ieee80211_add_xrates(frm, rs);
2316 if (vap->iv_flags & IEEE80211_F_WPA2) {
2317 if (vap->iv_rsn_ie != NULL)
2318 frm = add_ie(frm, vap->iv_rsn_ie);
2319 /* XXX else complain? */
2322 * NB: legacy 11b clients do not get certain ie's.
2323 * The caller identifies such clients by passing
2324 * a token in legacy to us. Could expand this to be
2325 * any legacy client for stuff like HT ie's.
2327 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2328 legacy != IEEE80211_SEND_LEGACY_11B) {
2329 frm = ieee80211_add_htcap(frm, bss);
2330 frm = ieee80211_add_htinfo(frm, bss);
2332 if (vap->iv_flags & IEEE80211_F_WPA1) {
2333 if (vap->iv_wpa_ie != NULL)
2334 frm = add_ie(frm, vap->iv_wpa_ie);
2335 /* XXX else complain? */
2337 if (vap->iv_flags & IEEE80211_F_WME)
2338 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2339 if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
2340 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
2341 legacy != IEEE80211_SEND_LEGACY_11B) {
2342 frm = ieee80211_add_htcap_vendor(frm, bss);
2343 frm = ieee80211_add_htinfo_vendor(frm, bss);
2345 #ifdef IEEE80211_SUPPORT_SUPERG
2346 if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
2347 legacy != IEEE80211_SEND_LEGACY_11B)
2348 frm = ieee80211_add_athcaps(frm, bss);
2350 if (vap->iv_appie_proberesp != NULL)
2351 frm = add_appie(frm, vap->iv_appie_proberesp);
2352 #ifdef IEEE80211_SUPPORT_MESH
2353 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2354 frm = ieee80211_add_meshid(frm, vap);
2355 frm = ieee80211_add_meshconf(frm, vap);
2358 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2364 * Send a probe response frame to the specified mac address.
2365 * This does not go through the normal mgt frame api so we
2366 * can specify the destination address and re-use the bss node
2367 * for the sta reference.
2370 ieee80211_send_proberesp(struct ieee80211vap *vap,
2371 const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2373 struct ieee80211_node *bss = vap->iv_bss;
2374 struct ieee80211com *ic = vap->iv_ic;
2377 if (vap->iv_state == IEEE80211_S_CAC) {
2378 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
2379 "block %s frame in CAC state", "probe response");
2380 vap->iv_stats.is_tx_badstate++;
2381 return EIO; /* XXX */
2385 * Hold a reference on the node so it doesn't go away until after
2386 * the xmit is complete all the way in the driver. On error we
2387 * will remove our reference.
2389 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2390 "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n",
2391 __func__, __LINE__, bss, bss->ni_macaddr, ":",
2392 ieee80211_node_refcnt(bss)+1);
2393 ieee80211_ref_node(bss);
2395 m = ieee80211_alloc_proberesp(bss, legacy);
2397 ieee80211_free_node(bss);
2401 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2402 KASSERT(m != NULL, ("no room for header"));
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 %6D%s\n",
2414 ieee80211_chan2ieee(ic, ic->ic_curchan), 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(MB_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(MB_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_callout(void *arg)
2477 struct ieee80211_node *ni = arg;
2478 struct ieee80211vap *vap;
2480 wlan_serialize_enter();
2482 if (vap->iv_state != IEEE80211_S_INIT &&
2483 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2485 * NB: it's safe to specify a timeout as the reason here;
2486 * it'll only be used in the right state.
2488 ieee80211_new_state(vap, IEEE80211_S_SCAN,
2489 IEEE80211_SCAN_FAIL_TIMEOUT);
2491 wlan_serialize_exit();
2495 ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
2497 struct ieee80211vap *vap = ni->ni_vap;
2498 enum ieee80211_state ostate = (enum ieee80211_state) arg;
2501 * Frame transmit completed; arrange timer callback. If
2502 * transmit was successfuly we wait for response. Otherwise
2503 * we arrange an immediate callback instead of doing the
2504 * callback directly since we don't know what state the driver
2505 * is in (e.g. what locks it is holding). This work should
2506 * not be too time-critical and not happen too often so the
2507 * added overhead is acceptable.
2509 * XXX what happens if !acked but response shows up before callback?
2511 if (vap->iv_state == ostate)
2512 callout_reset(&vap->iv_mgtsend,
2513 status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
2514 ieee80211_tx_mgt_timeout_callout, ni);
2518 ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
2519 struct ieee80211_beacon_offsets *bo, struct ieee80211_node *ni)
2521 struct ieee80211vap *vap = ni->ni_vap;
2522 struct ieee80211com *ic = ni->ni_ic;
2523 struct ieee80211_rateset *rs = &ni->ni_rates;
2527 * beacon frame format
2529 * [2] beacon interval
2530 * [2] cabability information
2532 * [tlv] supported rates
2533 * [3] parameter set (DS)
2534 * [8] CF parameter set (optional)
2535 * [tlv] parameter set (IBSS/TIM)
2536 * [tlv] country (optional)
2537 * [3] power control (optional)
2538 * [5] channel switch announcement (CSA) (optional)
2539 * [tlv] extended rate phy (ERP)
2540 * [tlv] extended supported rates
2541 * [tlv] RSN parameters
2542 * [tlv] HT capabilities
2543 * [tlv] HT information
2544 * XXX Vendor-specific OIDs (e.g. Atheros)
2545 * [tlv] WPA parameters
2546 * [tlv] WME parameters
2547 * [tlv] Vendor OUI HT capabilities (optional)
2548 * [tlv] Vendor OUI HT information (optional)
2549 * [tlv] Atheros capabilities (optional)
2550 * [tlv] TDMA parameters (optional)
2551 * [tlv] Mesh ID (MBSS)
2552 * [tlv] Mesh Conf (MBSS)
2553 * [tlv] application data (optional)
2556 memset(bo, 0, sizeof(*bo));
2558 memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
2560 *(uint16_t *)frm = htole16(ni->ni_intval);
2562 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2563 bo->bo_caps = (uint16_t *)frm;
2564 *(uint16_t *)frm = htole16(capinfo);
2566 *frm++ = IEEE80211_ELEMID_SSID;
2567 if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
2568 *frm++ = ni->ni_esslen;
2569 memcpy(frm, ni->ni_essid, ni->ni_esslen);
2570 frm += ni->ni_esslen;
2573 frm = ieee80211_add_rates(frm, rs);
2574 if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
2575 *frm++ = IEEE80211_ELEMID_DSPARMS;
2577 *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
2579 if (ic->ic_flags & IEEE80211_F_PCF) {
2581 frm = ieee80211_add_cfparms(frm, ic);
2584 if (vap->iv_opmode == IEEE80211_M_IBSS) {
2585 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
2587 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
2589 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2590 vap->iv_opmode == IEEE80211_M_MBSS) {
2591 /* TIM IE is the same for Mesh and Hostap */
2592 struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
2594 tie->tim_ie = IEEE80211_ELEMID_TIM;
2595 tie->tim_len = 4; /* length */
2596 tie->tim_count = 0; /* DTIM count */
2597 tie->tim_period = vap->iv_dtim_period; /* DTIM period */
2598 tie->tim_bitctl = 0; /* bitmap control */
2599 tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
2600 frm += sizeof(struct ieee80211_tim_ie);
2603 bo->bo_tim_trailer = frm;
2604 if ((vap->iv_flags & IEEE80211_F_DOTH) ||
2605 (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
2606 frm = ieee80211_add_countryie(frm, ic);
2607 if (vap->iv_flags & IEEE80211_F_DOTH) {
2608 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
2609 frm = ieee80211_add_powerconstraint(frm, vap);
2611 if (ic->ic_flags & IEEE80211_F_CSAPENDING)
2612 frm = ieee80211_add_csa(frm, vap);
2615 if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
2617 frm = ieee80211_add_erp(frm, ic);
2619 frm = ieee80211_add_xrates(frm, rs);
2620 if (vap->iv_flags & IEEE80211_F_WPA2) {
2621 if (vap->iv_rsn_ie != NULL)
2622 frm = add_ie(frm, vap->iv_rsn_ie);
2623 /* XXX else complain */
2625 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
2626 frm = ieee80211_add_htcap(frm, ni);
2627 bo->bo_htinfo = frm;
2628 frm = ieee80211_add_htinfo(frm, ni);
2630 if (vap->iv_flags & IEEE80211_F_WPA1) {
2631 if (vap->iv_wpa_ie != NULL)
2632 frm = add_ie(frm, vap->iv_wpa_ie);
2633 /* XXX else complain */
2635 if (vap->iv_flags & IEEE80211_F_WME) {
2637 frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
2639 if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
2640 (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
2641 frm = ieee80211_add_htcap_vendor(frm, ni);
2642 frm = ieee80211_add_htinfo_vendor(frm, ni);
2644 #ifdef IEEE80211_SUPPORT_SUPERG
2645 if (vap->iv_flags & IEEE80211_F_ATHEROS) {
2647 frm = ieee80211_add_athcaps(frm, ni);
2650 #ifdef IEEE80211_SUPPORT_TDMA
2651 if (vap->iv_caps & IEEE80211_C_TDMA) {
2653 frm = ieee80211_add_tdma(frm, vap);
2656 if (vap->iv_appie_beacon != NULL) {
2658 bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
2659 frm = add_appie(frm, vap->iv_appie_beacon);
2661 #ifdef IEEE80211_SUPPORT_MESH
2662 if (vap->iv_opmode == IEEE80211_M_MBSS) {
2663 frm = ieee80211_add_meshid(frm, vap);
2664 bo->bo_meshconf = frm;
2665 frm = ieee80211_add_meshconf(frm, vap);
2668 bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
2669 bo->bo_csa_trailer_len = frm - bo->bo_csa;
2670 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2674 * Allocate a beacon frame and fillin the appropriate bits.
2677 ieee80211_beacon_alloc(struct ieee80211_node *ni,
2678 struct ieee80211_beacon_offsets *bo)
2680 struct ieee80211vap *vap = ni->ni_vap;
2681 struct ieee80211com *ic = ni->ni_ic;
2682 struct ifnet *ifp = vap->iv_ifp;
2683 struct ieee80211_frame *wh;
2689 * beacon frame format
2691 * [2] beacon interval
2692 * [2] cabability information
2694 * [tlv] supported rates
2695 * [3] parameter set (DS)
2696 * [8] CF parameter set (optional)
2697 * [tlv] parameter set (IBSS/TIM)
2698 * [tlv] country (optional)
2699 * [3] power control (optional)
2700 * [5] channel switch announcement (CSA) (optional)
2701 * [tlv] extended rate phy (ERP)
2702 * [tlv] extended supported rates
2703 * [tlv] RSN parameters
2704 * [tlv] HT capabilities
2705 * [tlv] HT information
2706 * [tlv] Vendor OUI HT capabilities (optional)
2707 * [tlv] Vendor OUI HT information (optional)
2708 * XXX Vendor-specific OIDs (e.g. Atheros)
2709 * [tlv] WPA parameters
2710 * [tlv] WME parameters
2711 * [tlv] TDMA parameters (optional)
2712 * [tlv] Mesh ID (MBSS)
2713 * [tlv] Mesh Conf (MBSS)
2714 * [tlv] application data (optional)
2715 * NB: we allocate the max space required for the TIM bitmap.
2716 * XXX how big is this?
2718 pktlen = 8 /* time stamp */
2719 + sizeof(uint16_t) /* beacon interval */
2720 + sizeof(uint16_t) /* capabilities */
2721 + 2 + ni->ni_esslen /* ssid */
2722 + 2 + IEEE80211_RATE_SIZE /* supported rates */
2723 + 2 + 1 /* DS parameters */
2724 + 2 + 6 /* CF parameters */
2725 + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
2726 + IEEE80211_COUNTRY_MAX_SIZE /* country */
2727 + 2 + 1 /* power control */
2728 + sizeof(struct ieee80211_csa_ie) /* CSA */
2730 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2731 + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
2732 2*sizeof(struct ieee80211_ie_wpa) : 0)
2733 /* XXX conditional? */
2734 + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
2735 + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
2736 + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
2737 sizeof(struct ieee80211_wme_param) : 0)
2738 #ifdef IEEE80211_SUPPORT_SUPERG
2739 + sizeof(struct ieee80211_ath_ie) /* ATH */
2741 #ifdef IEEE80211_SUPPORT_TDMA
2742 + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
2743 sizeof(struct ieee80211_tdma_param) : 0)
2745 #ifdef IEEE80211_SUPPORT_MESH
2746 + 2 + ni->ni_meshidlen
2747 + sizeof(struct ieee80211_meshconf_ie)
2749 + IEEE80211_MAX_APPIE
2751 m = ieee80211_getmgtframe(&frm,
2752 ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
2754 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
2755 "%s: cannot get buf; size %u\n", __func__, pktlen);
2756 vap->iv_stats.is_tx_nobuf++;
2759 ieee80211_beacon_construct(m, frm, bo, ni);
2761 M_PREPEND(m, sizeof(struct ieee80211_frame), MB_DONTWAIT);
2762 KASSERT(m != NULL, ("no space for 802.11 header?"));
2763 wh = mtod(m, struct ieee80211_frame *);
2764 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2765 IEEE80211_FC0_SUBTYPE_BEACON;
2766 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2767 *(uint16_t *)wh->i_dur = 0;
2768 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2769 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
2770 IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2771 *(uint16_t *)wh->i_seq = 0;
2777 * Update the dynamic parts of a beacon frame based on the current state.
2780 ieee80211_beacon_update(struct ieee80211_node *ni,
2781 struct ieee80211_beacon_offsets *bo, struct mbuf *m, int mcast)
2783 struct ieee80211vap *vap = ni->ni_vap;
2784 struct ieee80211com *ic = ni->ni_ic;
2785 int len_changed = 0;
2789 * Handle 11h channel change when we've reached the count.
2790 * We must recalculate the beacon frame contents to account
2791 * for the new channel. Note we do this only for the first
2792 * vap that reaches this point; subsequent vaps just update
2793 * their beacon state to reflect the recalculated channel.
2795 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
2796 vap->iv_csa_count == ic->ic_csa_count) {
2797 vap->iv_csa_count = 0;
2799 * Effect channel change before reconstructing the beacon
2800 * frame contents as many places reference ni_chan.
2802 if (ic->ic_csa_newchan != NULL)
2803 ieee80211_csa_completeswitch(ic);
2805 * NB: ieee80211_beacon_construct clears all pending
2806 * updates in bo_flags so we don't need to explicitly
2807 * clear IEEE80211_BEACON_CSA.
2809 ieee80211_beacon_construct(m,
2810 mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), bo, ni);
2812 /* XXX do WME aggressive mode processing? */
2813 return 1; /* just assume length changed */
2816 /* XXX faster to recalculate entirely or just changes? */
2817 capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
2818 *bo->bo_caps = htole16(capinfo);
2820 if (vap->iv_flags & IEEE80211_F_WME) {
2821 struct ieee80211_wme_state *wme = &ic->ic_wme;
2824 * Check for agressive mode change. When there is
2825 * significant high priority traffic in the BSS
2826 * throttle back BE traffic by using conservative
2827 * parameters. Otherwise BE uses agressive params
2828 * to optimize performance of legacy/non-QoS traffic.
2830 if (wme->wme_flags & WME_F_AGGRMODE) {
2831 if (wme->wme_hipri_traffic >
2832 wme->wme_hipri_switch_thresh) {
2833 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2834 "%s: traffic %u, disable aggressive mode\n",
2835 __func__, wme->wme_hipri_traffic);
2836 wme->wme_flags &= ~WME_F_AGGRMODE;
2837 ieee80211_wme_updateparams_locked(vap);
2838 wme->wme_hipri_traffic =
2839 wme->wme_hipri_switch_hysteresis;
2841 wme->wme_hipri_traffic = 0;
2843 if (wme->wme_hipri_traffic <=
2844 wme->wme_hipri_switch_thresh) {
2845 IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
2846 "%s: traffic %u, enable aggressive mode\n",
2847 __func__, wme->wme_hipri_traffic);
2848 wme->wme_flags |= WME_F_AGGRMODE;
2849 ieee80211_wme_updateparams_locked(vap);
2850 wme->wme_hipri_traffic = 0;
2852 wme->wme_hipri_traffic =
2853 wme->wme_hipri_switch_hysteresis;
2855 if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
2856 (void) ieee80211_add_wme_param(bo->bo_wme, wme);
2857 clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
2861 if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
2862 ieee80211_ht_update_beacon(vap, bo);
2863 clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
2865 #ifdef IEEE80211_SUPPORT_TDMA
2866 if (vap->iv_caps & IEEE80211_C_TDMA) {
2868 * NB: the beacon is potentially updated every TBTT.
2870 ieee80211_tdma_update_beacon(vap, bo);
2873 #ifdef IEEE80211_SUPPORT_MESH
2874 if (vap->iv_opmode == IEEE80211_M_MBSS)
2875 ieee80211_mesh_update_beacon(vap, bo);
2878 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2879 vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
2880 struct ieee80211_tim_ie *tie =
2881 (struct ieee80211_tim_ie *) bo->bo_tim;
2882 if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
2883 u_int timlen, timoff, i;
2885 * ATIM/DTIM needs updating. If it fits in the
2886 * current space allocated then just copy in the
2887 * new bits. Otherwise we need to move any trailing
2888 * data to make room. Note that we know there is
2889 * contiguous space because ieee80211_beacon_allocate
2890 * insures there is space in the mbuf to write a
2891 * maximal-size virtual bitmap (based on iv_max_aid).
2894 * Calculate the bitmap size and offset, copy any
2895 * trailer out of the way, and then copy in the
2896 * new bitmap and update the information element.
2897 * Note that the tim bitmap must contain at least
2898 * one byte and any offset must be even.
2900 if (vap->iv_ps_pending != 0) {
2901 timoff = 128; /* impossibly large */
2902 for (i = 0; i < vap->iv_tim_len; i++)
2903 if (vap->iv_tim_bitmap[i]) {
2907 KASSERT(timoff != 128, ("tim bitmap empty!"));
2908 for (i = vap->iv_tim_len-1; i >= timoff; i--)
2909 if (vap->iv_tim_bitmap[i])
2911 timlen = 1 + (i - timoff);
2916 if (timlen != bo->bo_tim_len) {
2917 /* copy up/down trailer */
2918 int adjust = tie->tim_bitmap+timlen
2919 - bo->bo_tim_trailer;
2920 ovbcopy(bo->bo_tim_trailer,
2921 bo->bo_tim_trailer+adjust,
2922 bo->bo_tim_trailer_len);
2923 bo->bo_tim_trailer += adjust;
2924 bo->bo_erp += adjust;
2925 bo->bo_htinfo += adjust;
2926 #ifdef IEEE80211_SUPERG_SUPPORT
2927 bo->bo_ath += adjust;
2929 #ifdef IEEE80211_TDMA_SUPPORT
2930 bo->bo_tdma += adjust;
2932 #ifdef IEEE80211_MESH_SUPPORT
2933 bo->bo_meshconf += adjust;
2935 bo->bo_appie += adjust;
2936 bo->bo_wme += adjust;
2937 bo->bo_csa += adjust;
2938 bo->bo_tim_len = timlen;
2940 /* update information element */
2941 tie->tim_len = 3 + timlen;
2942 tie->tim_bitctl = timoff;
2945 memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
2948 clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
2950 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
2951 "%s: TIM updated, pending %u, off %u, len %u\n",
2952 __func__, vap->iv_ps_pending, timoff, timlen);
2954 /* count down DTIM period */
2955 if (tie->tim_count == 0)
2956 tie->tim_count = tie->tim_period - 1;
2959 /* update state for buffered multicast frames on DTIM */
2960 if (mcast && tie->tim_count == 0)
2961 tie->tim_bitctl |= 1;
2963 tie->tim_bitctl &= ~1;
2964 if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
2965 struct ieee80211_csa_ie *csa =
2966 (struct ieee80211_csa_ie *) bo->bo_csa;
2969 * Insert or update CSA ie. If we're just starting
2970 * to count down to the channel switch then we need
2971 * to insert the CSA ie. Otherwise we just need to
2972 * drop the count. The actual change happens above
2973 * when the vap's count reaches the target count.
2975 if (vap->iv_csa_count == 0) {
2976 memmove(&csa[1], csa, bo->bo_csa_trailer_len);
2977 bo->bo_erp += sizeof(*csa);
2978 bo->bo_htinfo += sizeof(*csa);
2979 bo->bo_wme += sizeof(*csa);
2980 #ifdef IEEE80211_SUPERG_SUPPORT
2981 bo->bo_ath += sizeof(*csa);
2983 #ifdef IEEE80211_TDMA_SUPPORT
2984 bo->bo_tdma += sizeof(*csa);
2986 #ifdef IEEE80211_MESH_SUPPORT
2987 bo->bo_meshconf += sizeof(*csa);
2989 bo->bo_appie += sizeof(*csa);
2990 bo->bo_csa_trailer_len += sizeof(*csa);
2991 bo->bo_tim_trailer_len += sizeof(*csa);
2992 m->m_len += sizeof(*csa);
2993 m->m_pkthdr.len += sizeof(*csa);
2995 ieee80211_add_csa(bo->bo_csa, vap);
2998 vap->iv_csa_count++;
2999 /* NB: don't clear IEEE80211_BEACON_CSA */
3001 if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3003 * ERP element needs updating.
3005 (void) ieee80211_add_erp(bo->bo_erp, ic);
3006 clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3008 #ifdef IEEE80211_SUPPORT_SUPERG
3009 if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
3010 ieee80211_add_athcaps(bo->bo_ath, ni);
3011 clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
3015 if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
3016 const struct ieee80211_appie *aie = vap->iv_appie_beacon;
3022 aielen += aie->ie_len;
3023 if (aielen != bo->bo_appie_len) {
3024 /* copy up/down trailer */
3025 int adjust = aielen - bo->bo_appie_len;
3026 ovbcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
3027 bo->bo_tim_trailer_len);
3028 bo->bo_tim_trailer += adjust;
3029 bo->bo_appie += adjust;
3030 bo->bo_appie_len = aielen;
3036 frm = add_appie(frm, aie);
3037 clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);