132176cfdSRui Paulo /*-
2f186073cSJoerg Sonnenberger * Copyright (c) 2001 Atsushi Onoe
332176cfdSRui Paulo * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4f186073cSJoerg Sonnenberger * All rights reserved.
5f186073cSJoerg Sonnenberger *
6f186073cSJoerg Sonnenberger * Redistribution and use in source and binary forms, with or without
7f186073cSJoerg Sonnenberger * modification, are permitted provided that the following conditions
8f186073cSJoerg Sonnenberger * are met:
9f186073cSJoerg Sonnenberger * 1. Redistributions of source code must retain the above copyright
10f186073cSJoerg Sonnenberger * notice, this list of conditions and the following disclaimer.
11f186073cSJoerg Sonnenberger * 2. Redistributions in binary form must reproduce the above copyright
12f186073cSJoerg Sonnenberger * notice, this list of conditions and the following disclaimer in the
13f186073cSJoerg Sonnenberger * documentation and/or other materials provided with the distribution.
14f186073cSJoerg Sonnenberger *
15f186073cSJoerg Sonnenberger * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f186073cSJoerg Sonnenberger * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f186073cSJoerg Sonnenberger * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f186073cSJoerg Sonnenberger * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19f186073cSJoerg Sonnenberger * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f186073cSJoerg Sonnenberger * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f186073cSJoerg Sonnenberger * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f186073cSJoerg Sonnenberger * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f186073cSJoerg Sonnenberger * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f186073cSJoerg Sonnenberger * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f186073cSJoerg Sonnenberger */
26f186073cSJoerg Sonnenberger
27085ff963SMatthew Dillon #include <sys/cdefs.h>
28085ff963SMatthew Dillon __FBSDID("$FreeBSD$");
29085ff963SMatthew Dillon
30f186073cSJoerg Sonnenberger #include "opt_inet.h"
3132176cfdSRui Paulo #include "opt_inet6.h"
3232176cfdSRui Paulo #include "opt_wlan.h"
33f186073cSJoerg Sonnenberger
34f186073cSJoerg Sonnenberger #include <sys/param.h>
35f186073cSJoerg Sonnenberger #include <sys/systm.h>
36f186073cSJoerg Sonnenberger #include <sys/kernel.h>
374f655ef5SMatthew Dillon #include <sys/malloc.h>
384f655ef5SMatthew Dillon #include <sys/mbuf.h>
39f186073cSJoerg Sonnenberger #include <sys/endian.h>
40f186073cSJoerg Sonnenberger
41841ab66cSSepherosa Ziehau #include <sys/socket.h>
42f186073cSJoerg Sonnenberger
43f186073cSJoerg Sonnenberger #include <net/bpf.h>
44841ab66cSSepherosa Ziehau #include <net/ethernet.h>
45841ab66cSSepherosa Ziehau #include <net/if.h>
46085ff963SMatthew Dillon #include <net/if_var.h>
47841ab66cSSepherosa Ziehau #include <net/if_llc.h>
48841ab66cSSepherosa Ziehau #include <net/if_media.h>
49085ff963SMatthew Dillon #include <net/vlan/if_vlan_var.h>
50085ff963SMatthew Dillon
51085ff963SMatthew Dillon #if defined(__DragonFly__)
5234a60cf6SRui Paulo #include <net/ifq_var.h>
53085ff963SMatthew Dillon #endif
54841ab66cSSepherosa Ziehau
55841ab66cSSepherosa Ziehau #include <netproto/802_11/ieee80211_var.h>
5632176cfdSRui Paulo #include <netproto/802_11/ieee80211_regdomain.h>
5732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
5832176cfdSRui Paulo #include <netproto/802_11/ieee80211_superg.h>
5932176cfdSRui Paulo #endif
6032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA
6132176cfdSRui Paulo #include <netproto/802_11/ieee80211_tdma.h>
6232176cfdSRui Paulo #endif
6332176cfdSRui Paulo #include <netproto/802_11/ieee80211_wds.h>
6432176cfdSRui Paulo #include <netproto/802_11/ieee80211_mesh.h>
65f186073cSJoerg Sonnenberger
66085ff963SMatthew Dillon #if defined(INET) || defined(INET6)
67f186073cSJoerg Sonnenberger #include <netinet/in.h>
68085ff963SMatthew Dillon #endif
69085ff963SMatthew Dillon
70085ff963SMatthew Dillon #ifdef INET
71f186073cSJoerg Sonnenberger #include <netinet/if_ether.h>
72841ab66cSSepherosa Ziehau #include <netinet/in_systm.h>
73841ab66cSSepherosa Ziehau #include <netinet/ip.h>
74f186073cSJoerg Sonnenberger #endif
7532176cfdSRui Paulo #ifdef INET6
7632176cfdSRui Paulo #include <netinet/ip6.h>
7732176cfdSRui Paulo #endif
7832176cfdSRui Paulo
794f655ef5SMatthew Dillon #if defined(__DragonFly__)
804f655ef5SMatthew Dillon #else
814f655ef5SMatthew Dillon #include <security/mac/mac_framework.h>
824f655ef5SMatthew Dillon #endif
83085ff963SMatthew Dillon
8432176cfdSRui Paulo #define ETHER_HEADER_COPY(dst, src) \
8532176cfdSRui Paulo memcpy(dst, src, sizeof(struct ether_header))
8632176cfdSRui Paulo
8732176cfdSRui Paulo static int ieee80211_fragment(struct ieee80211vap *, struct mbuf *,
8832176cfdSRui Paulo u_int hdrsize, u_int ciphdrsize, u_int mtu);
8932176cfdSRui Paulo static void ieee80211_tx_mgt_cb(struct ieee80211_node *, void *, int);
90f186073cSJoerg Sonnenberger
91841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG
92841ab66cSSepherosa Ziehau /*
93841ab66cSSepherosa Ziehau * Decide if an outbound management frame should be
94841ab66cSSepherosa Ziehau * printed when debugging is enabled. This filters some
95841ab66cSSepherosa Ziehau * of the less interesting frames that come frequently
96841ab66cSSepherosa Ziehau * (e.g. beacons).
97841ab66cSSepherosa Ziehau */
98841ab66cSSepherosa Ziehau static __inline int
doprint(struct ieee80211vap * vap,int subtype)9932176cfdSRui Paulo doprint(struct ieee80211vap *vap, int subtype)
100841ab66cSSepherosa Ziehau {
101841ab66cSSepherosa Ziehau switch (subtype) {
102841ab66cSSepherosa Ziehau case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
10332176cfdSRui Paulo return (vap->iv_opmode == IEEE80211_M_IBSS);
104841ab66cSSepherosa Ziehau }
105841ab66cSSepherosa Ziehau return 1;
106841ab66cSSepherosa Ziehau }
107841ab66cSSepherosa Ziehau #endif
108841ab66cSSepherosa Ziehau
109841ab66cSSepherosa Ziehau /*
110085ff963SMatthew Dillon * Transmit a frame to the given destination on the given VAP.
111085ff963SMatthew Dillon *
112085ff963SMatthew Dillon * It's up to the caller to figure out the details of who this
113085ff963SMatthew Dillon * is going to and resolving the node.
114085ff963SMatthew Dillon *
115085ff963SMatthew Dillon * This routine takes care of queuing it for power save,
116085ff963SMatthew Dillon * A-MPDU state stuff, fast-frames state stuff, encapsulation
117085ff963SMatthew Dillon * if required, then passing it up to the driver layer.
118085ff963SMatthew Dillon *
119085ff963SMatthew Dillon * This routine (for now) consumes the mbuf and frees the node
120085ff963SMatthew Dillon * reference; it ideally will return a TX status which reflects
121085ff963SMatthew Dillon * whether the mbuf was consumed or not, so the caller can
122085ff963SMatthew Dillon * free the mbuf (if appropriate) and the node reference (again,
123085ff963SMatthew Dillon * if appropriate.)
124841ab66cSSepherosa Ziehau */
125085ff963SMatthew Dillon int
ieee80211_vap_pkt_send_dest(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_node * ni)126085ff963SMatthew Dillon ieee80211_vap_pkt_send_dest(struct ieee80211vap *vap, struct mbuf *m,
127085ff963SMatthew Dillon struct ieee80211_node *ni)
12832176cfdSRui Paulo {
12932176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic;
130085ff963SMatthew Dillon struct ifnet *ifp = vap->iv_ifp;
1314f655ef5SMatthew Dillon int len, mcast;
13232176cfdSRui Paulo
13332176cfdSRui Paulo if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
13432176cfdSRui Paulo (m->m_flags & M_PWR_SAV) == 0) {
13532176cfdSRui Paulo /*
13632176cfdSRui Paulo * Station in power save mode; pass the frame
13732176cfdSRui Paulo * to the 802.11 layer and continue. We'll get
13832176cfdSRui Paulo * the frame back when the time is right.
13932176cfdSRui Paulo * XXX lose WDS vap linkage?
14032176cfdSRui Paulo */
1414f898719SImre Vadász if (ieee80211_pwrsave(ni, m) != 0)
1424f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
14332176cfdSRui Paulo ieee80211_free_node(ni);
144085ff963SMatthew Dillon
145085ff963SMatthew Dillon /*
146085ff963SMatthew Dillon * We queued it fine, so tell the upper layer
147085ff963SMatthew Dillon * that we consumed it.
148085ff963SMatthew Dillon */
149085ff963SMatthew Dillon return (0);
15032176cfdSRui Paulo }
15132176cfdSRui Paulo /* calculate priority so drivers can find the tx queue */
15232176cfdSRui Paulo if (ieee80211_classify(ni, m)) {
15332176cfdSRui Paulo IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
154085ff963SMatthew Dillon ni->ni_macaddr, NULL,
15532176cfdSRui Paulo "%s", "classification failure");
15632176cfdSRui Paulo vap->iv_stats.is_tx_classify++;
1574f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
15832176cfdSRui Paulo m_freem(m);
15932176cfdSRui Paulo ieee80211_free_node(ni);
160085ff963SMatthew Dillon
161085ff963SMatthew Dillon /* XXX better status? */
162085ff963SMatthew Dillon return (0);
16332176cfdSRui Paulo }
16432176cfdSRui Paulo /*
16532176cfdSRui Paulo * Stash the node pointer. Note that we do this after
16632176cfdSRui Paulo * any call to ieee80211_dwds_mcast because that code
16732176cfdSRui Paulo * uses any existing value for rcvif to identify the
16832176cfdSRui Paulo * interface it (might have been) received on.
16932176cfdSRui Paulo */
17032176cfdSRui Paulo m->m_pkthdr.rcvif = (void *)ni;
1714f898719SImre Vadász mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1: 0;
1724f898719SImre Vadász len = m->m_pkthdr.len;
17332176cfdSRui Paulo
17432176cfdSRui Paulo BPF_MTAP(ifp, m); /* 802.3 tx */
17532176cfdSRui Paulo
17632176cfdSRui Paulo /*
17732176cfdSRui Paulo * Check if A-MPDU tx aggregation is setup or if we
17832176cfdSRui Paulo * should try to enable it. The sta must be associated
17932176cfdSRui Paulo * with HT and A-MPDU enabled for use. When the policy
18032176cfdSRui Paulo * routine decides we should enable A-MPDU we issue an
18132176cfdSRui Paulo * ADDBA request and wait for a reply. The frame being
18232176cfdSRui Paulo * encapsulated will go out w/o using A-MPDU, or possibly
18332176cfdSRui Paulo * it might be collected by the driver and held/retransmit.
18432176cfdSRui Paulo * The default ic_ampdu_enable routine handles staggering
18532176cfdSRui Paulo * ADDBA requests in case the receiver NAK's us or we are
18632176cfdSRui Paulo * otherwise unable to establish a BA stream.
18732176cfdSRui Paulo */
18832176cfdSRui Paulo if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
1894f655ef5SMatthew Dillon (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)) {
1904f655ef5SMatthew Dillon if ((m->m_flags & M_EAPOL) == 0) {
191085ff963SMatthew Dillon int tid = WME_AC_TO_TID(M_WME_GETAC(m));
192085ff963SMatthew Dillon struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
19332176cfdSRui Paulo
19432176cfdSRui Paulo ieee80211_txampdu_count_packet(tap);
19532176cfdSRui Paulo if (IEEE80211_AMPDU_RUNNING(tap)) {
19632176cfdSRui Paulo /*
19732176cfdSRui Paulo * Operational, mark frame for aggregation.
19832176cfdSRui Paulo *
19932176cfdSRui Paulo * XXX do tx aggregation here
20032176cfdSRui Paulo */
20132176cfdSRui Paulo m->m_flags |= M_AMPDU_MPDU;
20232176cfdSRui Paulo } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
20332176cfdSRui Paulo ic->ic_ampdu_enable(ni, tap)) {
20432176cfdSRui Paulo /*
20532176cfdSRui Paulo * Not negotiated yet, request service.
20632176cfdSRui Paulo */
20732176cfdSRui Paulo ieee80211_ampdu_request(ni, tap);
20832176cfdSRui Paulo /* XXX hold frame for reply? */
20932176cfdSRui Paulo }
21032176cfdSRui Paulo }
2114f655ef5SMatthew Dillon }
212085ff963SMatthew Dillon
21332176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
2144f655ef5SMatthew Dillon /*
2154f655ef5SMatthew Dillon * Check for AMSDU/FF; queue for aggregation
2164f655ef5SMatthew Dillon *
2174f655ef5SMatthew Dillon * Note: we don't bother trying to do fast frames or
2184f655ef5SMatthew Dillon * A-MSDU encapsulation for 802.3 drivers. Now, we
2194f655ef5SMatthew Dillon * likely could do it for FF (because it's a magic
2204f655ef5SMatthew Dillon * atheros tunnel LLC type) but I don't think we're going
2214f655ef5SMatthew Dillon * to really need to. For A-MSDU we'd have to set the
2224f655ef5SMatthew Dillon * A-MSDU QoS bit in the wifi header, so we just plain
2234f655ef5SMatthew Dillon * can't do it.
2244f655ef5SMatthew Dillon *
2254f655ef5SMatthew Dillon * Strictly speaking, we could actually /do/ A-MSDU / FF
2264f655ef5SMatthew Dillon * with A-MPDU together which for certain circumstances
2274f655ef5SMatthew Dillon * is beneficial (eg A-MSDU of TCK ACKs.) However,
2284f655ef5SMatthew Dillon * I'll ignore that for now so existing behaviour is maintained.
2294f655ef5SMatthew Dillon * Later on it would be good to make "amsdu + ampdu" configurable.
2304f655ef5SMatthew Dillon */
2314f655ef5SMatthew Dillon else if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
2324f655ef5SMatthew Dillon if ((! mcast) && ieee80211_amsdu_tx_ok(ni)) {
2334f655ef5SMatthew Dillon m = ieee80211_amsdu_check(ni, m);
2344f655ef5SMatthew Dillon if (m == NULL) {
2354f655ef5SMatthew Dillon /* NB: any ni ref held on stageq */
2364f655ef5SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2374f655ef5SMatthew Dillon "%s: amsdu_check queued frame\n",
2384f655ef5SMatthew Dillon __func__);
2394f655ef5SMatthew Dillon return (0);
2404f655ef5SMatthew Dillon }
2414f655ef5SMatthew Dillon } else if ((! mcast) && IEEE80211_ATH_CAP(vap, ni,
2424f655ef5SMatthew Dillon IEEE80211_NODE_FF)) {
24332176cfdSRui Paulo m = ieee80211_ff_check(ni, m);
24432176cfdSRui Paulo if (m == NULL) {
24532176cfdSRui Paulo /* NB: any ni ref held on stageq */
2464f655ef5SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
2474f655ef5SMatthew Dillon "%s: ff_check queued frame\n",
2484f655ef5SMatthew Dillon __func__);
249085ff963SMatthew Dillon return (0);
25032176cfdSRui Paulo }
25132176cfdSRui Paulo }
2524f655ef5SMatthew Dillon }
25332176cfdSRui Paulo #endif /* IEEE80211_SUPPORT_SUPERG */
254085ff963SMatthew Dillon
255085ff963SMatthew Dillon /*
256085ff963SMatthew Dillon * Grab the TX lock - serialise the TX process from this
257085ff963SMatthew Dillon * point (where TX state is being checked/modified)
258085ff963SMatthew Dillon * through to driver queue.
259085ff963SMatthew Dillon */
260085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
261085ff963SMatthew Dillon
2624f655ef5SMatthew Dillon /*
2634f655ef5SMatthew Dillon * XXX make the encap and transmit code a separate function
2644f655ef5SMatthew Dillon * so things like the FF (and later A-MSDU) path can just call
2654f655ef5SMatthew Dillon * it for flushed frames.
2664f655ef5SMatthew Dillon */
26732176cfdSRui Paulo if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
26832176cfdSRui Paulo /*
26932176cfdSRui Paulo * Encapsulate the packet in prep for transmission.
27032176cfdSRui Paulo */
27132176cfdSRui Paulo m = ieee80211_encap(vap, ni, m);
27232176cfdSRui Paulo if (m == NULL) {
27332176cfdSRui Paulo /* NB: stat+msg handled in ieee80211_encap */
274085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
27532176cfdSRui Paulo ieee80211_free_node(ni);
2764f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
277085ff963SMatthew Dillon return (ENOBUFS);
27832176cfdSRui Paulo }
27932176cfdSRui Paulo }
280c75fa8b8SMatthew Dillon /* HACK - added by DragonFly, mbuf could lose rcvif assignment above */
281c75fa8b8SMatthew Dillon m->m_pkthdr.rcvif = (void *)ni;
2824f655ef5SMatthew Dillon (void) ieee80211_parent_xmitpkt(ic, m);
28332176cfdSRui Paulo
284085ff963SMatthew Dillon /*
285085ff963SMatthew Dillon * Unlock at this point - no need to hold it across
286085ff963SMatthew Dillon * ieee80211_free_node() (ie, the comlock)
287085ff963SMatthew Dillon */
288085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
28932176cfdSRui Paulo ic->ic_lastdata = ticks;
290085ff963SMatthew Dillon
291085ff963SMatthew Dillon return (0);
29232176cfdSRui Paulo }
293085ff963SMatthew Dillon
294085ff963SMatthew Dillon
295085ff963SMatthew Dillon
296085ff963SMatthew Dillon /*
297085ff963SMatthew Dillon * Send the given mbuf through the given vap.
298085ff963SMatthew Dillon *
299085ff963SMatthew Dillon * This consumes the mbuf regardless of whether the transmit
300085ff963SMatthew Dillon * was successful or not.
301085ff963SMatthew Dillon *
302085ff963SMatthew Dillon * This does none of the initial checks that ieee80211_start()
303085ff963SMatthew Dillon * does (eg CAC timeout, interface wakeup) - the caller must
304085ff963SMatthew Dillon * do this first.
305085ff963SMatthew Dillon */
306085ff963SMatthew Dillon static int
ieee80211_start_pkt(struct ieee80211vap * vap,struct mbuf * m)307085ff963SMatthew Dillon ieee80211_start_pkt(struct ieee80211vap *vap, struct mbuf *m)
308085ff963SMatthew Dillon {
309085ff963SMatthew Dillon #define IS_DWDS(vap) \
310085ff963SMatthew Dillon (vap->iv_opmode == IEEE80211_M_WDS && \
311085ff963SMatthew Dillon (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) == 0)
312085ff963SMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
313085ff963SMatthew Dillon struct ifnet *ifp = vap->iv_ifp;
314085ff963SMatthew Dillon struct ieee80211_node *ni;
315085ff963SMatthew Dillon struct ether_header *eh;
316085ff963SMatthew Dillon
317085ff963SMatthew Dillon /*
318085ff963SMatthew Dillon * Cancel any background scan.
319085ff963SMatthew Dillon */
320085ff963SMatthew Dillon if (ic->ic_flags & IEEE80211_F_SCAN)
321085ff963SMatthew Dillon ieee80211_cancel_anyscan(vap);
322085ff963SMatthew Dillon /*
323085ff963SMatthew Dillon * Find the node for the destination so we can do
324085ff963SMatthew Dillon * things like power save and fast frames aggregation.
325085ff963SMatthew Dillon *
326085ff963SMatthew Dillon * NB: past this point various code assumes the first
327085ff963SMatthew Dillon * mbuf has the 802.3 header present (and contiguous).
328085ff963SMatthew Dillon */
329085ff963SMatthew Dillon ni = NULL;
330085ff963SMatthew Dillon if (m->m_len < sizeof(struct ether_header) &&
331085ff963SMatthew Dillon (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
332085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
333085ff963SMatthew Dillon "discard frame, %s\n", "m_pullup failed");
334085ff963SMatthew Dillon vap->iv_stats.is_tx_nobuf++; /* XXX */
3354f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
336085ff963SMatthew Dillon return (ENOBUFS);
337085ff963SMatthew Dillon }
338085ff963SMatthew Dillon eh = mtod(m, struct ether_header *);
339085ff963SMatthew Dillon if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
340085ff963SMatthew Dillon if (IS_DWDS(vap)) {
341085ff963SMatthew Dillon /*
342085ff963SMatthew Dillon * Only unicast frames from the above go out
343085ff963SMatthew Dillon * DWDS vaps; multicast frames are handled by
344085ff963SMatthew Dillon * dispatching the frame as it comes through
345085ff963SMatthew Dillon * the AP vap (see below).
346085ff963SMatthew Dillon */
347085ff963SMatthew Dillon IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_WDS,
348085ff963SMatthew Dillon eh->ether_dhost, "mcast", "%s", "on DWDS");
349085ff963SMatthew Dillon vap->iv_stats.is_dwds_mcast++;
350085ff963SMatthew Dillon m_freem(m);
3514f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
352085ff963SMatthew Dillon /* XXX better status? */
353085ff963SMatthew Dillon return (ENOBUFS);
354085ff963SMatthew Dillon }
355085ff963SMatthew Dillon if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
356085ff963SMatthew Dillon /*
357085ff963SMatthew Dillon * Spam DWDS vap's w/ multicast traffic.
358085ff963SMatthew Dillon */
359085ff963SMatthew Dillon /* XXX only if dwds in use? */
360085ff963SMatthew Dillon ieee80211_dwds_mcast(vap, m);
361085ff963SMatthew Dillon }
362085ff963SMatthew Dillon }
363085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
364085ff963SMatthew Dillon if (vap->iv_opmode != IEEE80211_M_MBSS) {
365085ff963SMatthew Dillon #endif
366085ff963SMatthew Dillon ni = ieee80211_find_txnode(vap, eh->ether_dhost);
367085ff963SMatthew Dillon if (ni == NULL) {
368085ff963SMatthew Dillon /* NB: ieee80211_find_txnode does stat+msg */
3694f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
370085ff963SMatthew Dillon m_freem(m);
371085ff963SMatthew Dillon /* XXX better status? */
372085ff963SMatthew Dillon return (ENOBUFS);
373085ff963SMatthew Dillon }
374085ff963SMatthew Dillon if (ni->ni_associd == 0 &&
375085ff963SMatthew Dillon (ni->ni_flags & IEEE80211_NODE_ASSOCID)) {
376085ff963SMatthew Dillon IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
377085ff963SMatthew Dillon eh->ether_dhost, NULL,
378085ff963SMatthew Dillon "sta not associated (type 0x%04x)",
379085ff963SMatthew Dillon htons(eh->ether_type));
380085ff963SMatthew Dillon vap->iv_stats.is_tx_notassoc++;
3814f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
382085ff963SMatthew Dillon m_freem(m);
383085ff963SMatthew Dillon ieee80211_free_node(ni);
384085ff963SMatthew Dillon /* XXX better status? */
385085ff963SMatthew Dillon return (ENOBUFS);
386085ff963SMatthew Dillon }
387085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
388085ff963SMatthew Dillon } else {
389085ff963SMatthew Dillon if (!IEEE80211_ADDR_EQ(eh->ether_shost, vap->iv_myaddr)) {
390085ff963SMatthew Dillon /*
391085ff963SMatthew Dillon * Proxy station only if configured.
392085ff963SMatthew Dillon */
393085ff963SMatthew Dillon if (!ieee80211_mesh_isproxyena(vap)) {
394085ff963SMatthew Dillon IEEE80211_DISCARD_MAC(vap,
395085ff963SMatthew Dillon IEEE80211_MSG_OUTPUT |
396085ff963SMatthew Dillon IEEE80211_MSG_MESH,
397085ff963SMatthew Dillon eh->ether_dhost, NULL,
398085ff963SMatthew Dillon "%s", "proxy not enabled");
399085ff963SMatthew Dillon vap->iv_stats.is_mesh_notproxy++;
4004f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
401085ff963SMatthew Dillon m_freem(m);
402085ff963SMatthew Dillon /* XXX better status? */
403085ff963SMatthew Dillon return (ENOBUFS);
404085ff963SMatthew Dillon }
4054f655ef5SMatthew Dillon #if defined(__DragonFly__)
406085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
407f92fae3fSSascha Wildner "forward frame from DS SA(%s), DA(%s)\n",
408f92fae3fSSascha Wildner ether_sprintf(eh->ether_shost),
409f92fae3fSSascha Wildner ether_sprintf(eh->ether_dhost));
4104f655ef5SMatthew Dillon #else
4114f655ef5SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
4124f655ef5SMatthew Dillon "forward frame from DS SA(%6D), DA(%6D)\n",
4134f655ef5SMatthew Dillon eh->ether_shost, ":",
4144f655ef5SMatthew Dillon eh->ether_dhost, ":");
4154f655ef5SMatthew Dillon #endif
416085ff963SMatthew Dillon ieee80211_mesh_proxy_check(vap, eh->ether_shost);
417085ff963SMatthew Dillon }
418085ff963SMatthew Dillon ni = ieee80211_mesh_discover(vap, eh->ether_dhost, m);
419085ff963SMatthew Dillon if (ni == NULL) {
420085ff963SMatthew Dillon /*
421085ff963SMatthew Dillon * NB: ieee80211_mesh_discover holds/disposes
422085ff963SMatthew Dillon * frame (e.g. queueing on path discovery).
423085ff963SMatthew Dillon */
4244f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
425085ff963SMatthew Dillon /* XXX better status? */
426085ff963SMatthew Dillon return (ENOBUFS);
427085ff963SMatthew Dillon }
428085ff963SMatthew Dillon }
429085ff963SMatthew Dillon #endif
430085ff963SMatthew Dillon
431085ff963SMatthew Dillon /*
432085ff963SMatthew Dillon * We've resolved the sender, so attempt to transmit it.
433085ff963SMatthew Dillon */
434085ff963SMatthew Dillon
435085ff963SMatthew Dillon if (vap->iv_state == IEEE80211_S_SLEEP) {
436085ff963SMatthew Dillon /*
437085ff963SMatthew Dillon * In power save; queue frame and then wakeup device
438085ff963SMatthew Dillon * for transmit.
439085ff963SMatthew Dillon */
440085ff963SMatthew Dillon ic->ic_lastdata = ticks;
4414f898719SImre Vadász if (ieee80211_pwrsave(ni, m) != 0)
4424f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
443085ff963SMatthew Dillon ieee80211_free_node(ni);
444085ff963SMatthew Dillon ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
445085ff963SMatthew Dillon return (0);
446085ff963SMatthew Dillon }
447085ff963SMatthew Dillon
448085ff963SMatthew Dillon if (ieee80211_vap_pkt_send_dest(vap, m, ni) != 0)
449085ff963SMatthew Dillon return (ENOBUFS);
450085ff963SMatthew Dillon return (0);
45132176cfdSRui Paulo #undef IS_DWDS
45232176cfdSRui Paulo }
45332176cfdSRui Paulo
454085ff963SMatthew Dillon /*
455085ff963SMatthew Dillon * Start method for vap's. All packets from the stack come
456085ff963SMatthew Dillon * through here. We handle common processing of the packets
457085ff963SMatthew Dillon * before dispatching them to the underlying device.
458085ff963SMatthew Dillon *
459085ff963SMatthew Dillon * if_transmit() requires that the mbuf be consumed by this call
460085ff963SMatthew Dillon * regardless of the return condition.
461085ff963SMatthew Dillon */
462085ff963SMatthew Dillon
463085ff963SMatthew Dillon #if defined(__DragonFly__)
464085ff963SMatthew Dillon
465085ff963SMatthew Dillon void
ieee80211_vap_start(struct ifnet * ifp,struct ifaltq_subque * ifsq)466085ff963SMatthew Dillon ieee80211_vap_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
467085ff963SMatthew Dillon {
468085ff963SMatthew Dillon struct ieee80211vap *vap = ifp->if_softc;
469085ff963SMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
4704f655ef5SMatthew Dillon struct ifnet *parent = vap->iv_ifp;
471085ff963SMatthew Dillon struct mbuf *m = NULL;
472085ff963SMatthew Dillon
473085ff963SMatthew Dillon /* NB: parent must be up and running */
474085ff963SMatthew Dillon if (!IFNET_IS_UP_RUNNING(parent)) {
475085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
476085ff963SMatthew Dillon "%s: ignore queue, parent %s not up+running\n",
477085ff963SMatthew Dillon __func__, parent->if_xname);
478085ff963SMatthew Dillon /* XXX stat */
479085ff963SMatthew Dillon /*m_freem(m);*/
480085ff963SMatthew Dillon /*return (EINVAL);*/
481085ff963SMatthew Dillon return;
482085ff963SMatthew Dillon }
483085ff963SMatthew Dillon
484085ff963SMatthew Dillon wlan_assert_serialized();
485085ff963SMatthew Dillon ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
486085ff963SMatthew Dillon
487085ff963SMatthew Dillon /*
488085ff963SMatthew Dillon * No data frames go out unless we're running.
489085ff963SMatthew Dillon * Note in particular this covers CAC and CSA
490085ff963SMatthew Dillon * states (though maybe we should check muting
491085ff963SMatthew Dillon * for CSA).
492085ff963SMatthew Dillon */
493085ff963SMatthew Dillon if (vap->iv_state != IEEE80211_S_RUN &&
494085ff963SMatthew Dillon vap->iv_state != IEEE80211_S_SLEEP) {
495085ff963SMatthew Dillon IEEE80211_LOCK(ic);
496085ff963SMatthew Dillon /* re-check under the com lock to avoid races */
497085ff963SMatthew Dillon if (vap->iv_state != IEEE80211_S_RUN &&
498085ff963SMatthew Dillon vap->iv_state != IEEE80211_S_SLEEP) {
499085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
500085ff963SMatthew Dillon "%s: ignore queue, in %s state\n",
501085ff963SMatthew Dillon __func__, ieee80211_state_name[vap->iv_state]);
502085ff963SMatthew Dillon vap->iv_stats.is_tx_badstate++;
503085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
504085ff963SMatthew Dillon ifsq_set_oactive(ifsq);
505085ff963SMatthew Dillon /*m_freem(m);*/
506085ff963SMatthew Dillon /* return (EINVAL); */
507085ff963SMatthew Dillon return;
508085ff963SMatthew Dillon }
509085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
510085ff963SMatthew Dillon }
511085ff963SMatthew Dillon
512085ff963SMatthew Dillon wlan_serialize_exit();
513085ff963SMatthew Dillon for (;;) {
514085ff963SMatthew Dillon m = ifsq_dequeue(ifsq);
515085ff963SMatthew Dillon if (m == NULL)
516085ff963SMatthew Dillon break;
517085ff963SMatthew Dillon
518085ff963SMatthew Dillon /*
519085ff963SMatthew Dillon * Sanitize mbuf flags for net80211 use. We cannot
520085ff963SMatthew Dillon * clear M_PWR_SAV or M_MORE_DATA because these may
521085ff963SMatthew Dillon * be set for frames that are re-submitted from the
522085ff963SMatthew Dillon * power save queue.
523085ff963SMatthew Dillon *
524085ff963SMatthew Dillon * NB: This must be done before ieee80211_classify as
525085ff963SMatthew Dillon * it marks EAPOL in frames with M_EAPOL.
526085ff963SMatthew Dillon */
527085ff963SMatthew Dillon m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
528085ff963SMatthew Dillon
529085ff963SMatthew Dillon /*
530085ff963SMatthew Dillon * Bump to the packet transmission path.
531085ff963SMatthew Dillon * The mbuf will be consumed here.
532085ff963SMatthew Dillon */
533085ff963SMatthew Dillon ieee80211_start_pkt(vap, m);
534085ff963SMatthew Dillon }
535085ff963SMatthew Dillon wlan_serialize_enter();
536085ff963SMatthew Dillon }
537085ff963SMatthew Dillon
538085ff963SMatthew Dillon #else
539085ff963SMatthew Dillon
540085ff963SMatthew Dillon int
ieee80211_vap_transmit(struct ifnet * ifp,struct mbuf * m)541085ff963SMatthew Dillon ieee80211_vap_transmit(struct ifnet *ifp, struct mbuf *m)
542085ff963SMatthew Dillon {
543085ff963SMatthew Dillon struct ieee80211vap *vap = ifp->if_softc;
544085ff963SMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
545085ff963SMatthew Dillon
546085ff963SMatthew Dillon /*
547085ff963SMatthew Dillon * No data frames go out unless we're running.
548085ff963SMatthew Dillon * Note in particular this covers CAC and CSA
549085ff963SMatthew Dillon * states (though maybe we should check muting
550085ff963SMatthew Dillon * for CSA).
551085ff963SMatthew Dillon */
552085ff963SMatthew Dillon if (vap->iv_state != IEEE80211_S_RUN &&
553085ff963SMatthew Dillon vap->iv_state != IEEE80211_S_SLEEP) {
554085ff963SMatthew Dillon IEEE80211_LOCK(ic);
555085ff963SMatthew Dillon /* re-check under the com lock to avoid races */
556085ff963SMatthew Dillon if (vap->iv_state != IEEE80211_S_RUN &&
557085ff963SMatthew Dillon vap->iv_state != IEEE80211_S_SLEEP) {
558085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
559085ff963SMatthew Dillon "%s: ignore queue, in %s state\n",
560085ff963SMatthew Dillon __func__, ieee80211_state_name[vap->iv_state]);
561085ff963SMatthew Dillon vap->iv_stats.is_tx_badstate++;
562085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
563085ff963SMatthew Dillon ifp->if_drv_flags |= IFF_DRV_OACTIVE;
564085ff963SMatthew Dillon m_freem(m);
5654f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
5664f898719SImre Vadász return (ENETDOWN);
567085ff963SMatthew Dillon }
568085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
569085ff963SMatthew Dillon }
570085ff963SMatthew Dillon
571085ff963SMatthew Dillon /*
572085ff963SMatthew Dillon * Sanitize mbuf flags for net80211 use. We cannot
573085ff963SMatthew Dillon * clear M_PWR_SAV or M_MORE_DATA because these may
574085ff963SMatthew Dillon * be set for frames that are re-submitted from the
575085ff963SMatthew Dillon * power save queue.
576085ff963SMatthew Dillon *
577085ff963SMatthew Dillon * NB: This must be done before ieee80211_classify as
578085ff963SMatthew Dillon * it marks EAPOL in frames with M_EAPOL.
579085ff963SMatthew Dillon */
580085ff963SMatthew Dillon m->m_flags &= ~(M_80211_TX - M_PWR_SAV - M_MORE_DATA);
581085ff963SMatthew Dillon
582085ff963SMatthew Dillon /*
583085ff963SMatthew Dillon * Bump to the packet transmission path.
584085ff963SMatthew Dillon * The mbuf will be consumed here.
585085ff963SMatthew Dillon */
586085ff963SMatthew Dillon return (ieee80211_start_pkt(vap, m));
587085ff963SMatthew Dillon }
588085ff963SMatthew Dillon
589085ff963SMatthew Dillon void
ieee80211_vap_qflush(struct ifnet * ifp)590085ff963SMatthew Dillon ieee80211_vap_qflush(struct ifnet *ifp)
591085ff963SMatthew Dillon {
592085ff963SMatthew Dillon
593085ff963SMatthew Dillon /* Empty for now */
594085ff963SMatthew Dillon }
595085ff963SMatthew Dillon
596085ff963SMatthew Dillon #endif
597085ff963SMatthew Dillon
598085ff963SMatthew Dillon /*
599085ff963SMatthew Dillon * 802.11 raw output routine.
600085ff963SMatthew Dillon *
601085ff963SMatthew Dillon * XXX TODO: this (and other send routines) should correctly
602085ff963SMatthew Dillon * XXX keep the pwr mgmt bit set if it decides to call into the
603085ff963SMatthew Dillon * XXX driver to send a frame whilst the state is SLEEP.
604085ff963SMatthew Dillon *
605085ff963SMatthew Dillon * Otherwise the peer may decide that we're awake and flood us
606085ff963SMatthew Dillon * with traffic we are still too asleep to receive!
607085ff963SMatthew Dillon */
608085ff963SMatthew Dillon int
ieee80211_raw_output(struct ieee80211vap * vap,struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)609085ff963SMatthew Dillon ieee80211_raw_output(struct ieee80211vap *vap, struct ieee80211_node *ni,
610085ff963SMatthew Dillon struct mbuf *m, const struct ieee80211_bpf_params *params)
611085ff963SMatthew Dillon {
612085ff963SMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
6134f655ef5SMatthew Dillon int error;
614085ff963SMatthew Dillon
6154f898719SImre Vadász /*
6164f898719SImre Vadász * Set node - the caller has taken a reference, so ensure
6174f898719SImre Vadász * that the mbuf has the same node value that
6184f898719SImre Vadász * it would if it were going via the normal path.
6194f898719SImre Vadász */
6204f898719SImre Vadász m->m_pkthdr.rcvif = (void *)ni;
6214f898719SImre Vadász
6224f898719SImre Vadász /*
6234f898719SImre Vadász * Attempt to add bpf transmit parameters.
6244f898719SImre Vadász *
6254f898719SImre Vadász * For now it's ok to fail; the raw_xmit api still takes
6264f898719SImre Vadász * them as an option.
6274f898719SImre Vadász *
6284f898719SImre Vadász * Later on when ic_raw_xmit() has params removed,
6294f898719SImre Vadász * they'll have to be added - so fail the transmit if
6304f898719SImre Vadász * they can't be.
6314f898719SImre Vadász */
6324f898719SImre Vadász if (params)
6334f898719SImre Vadász (void) ieee80211_add_xmit_params(m, params);
6344f898719SImre Vadász
6354f655ef5SMatthew Dillon error = ic->ic_raw_xmit(ni, m, params);
6364f655ef5SMatthew Dillon if (error) {
6374f655ef5SMatthew Dillon if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, 1);
6384f655ef5SMatthew Dillon ieee80211_free_node(ni);
6394f655ef5SMatthew Dillon }
6404f655ef5SMatthew Dillon return (error);
641085ff963SMatthew Dillon }
642cc0d8938SRui Paulo
64332176cfdSRui Paulo /*
64432176cfdSRui Paulo * 802.11 output routine. This is (currently) used only to
64532176cfdSRui Paulo * connect bpf write calls to the 802.11 layer for injecting
64632176cfdSRui Paulo * raw 802.11 frames.
64732176cfdSRui Paulo */
648085ff963SMatthew Dillon #if defined(__DragonFly__)
64932176cfdSRui Paulo int
ieee80211_output(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct rtentry * rt)65032176cfdSRui Paulo ieee80211_output(struct ifnet *ifp, struct mbuf *m,
65134a60cf6SRui Paulo struct sockaddr *dst, struct rtentry *rt)
652085ff963SMatthew Dillon #else
653085ff963SMatthew Dillon int
654085ff963SMatthew Dillon ieee80211_output(struct ifnet *ifp, struct mbuf *m,
655294727bfSImre Vadász const struct sockaddr *dst, struct route *ro)
656085ff963SMatthew Dillon #endif
65732176cfdSRui Paulo {
65832176cfdSRui Paulo #define senderr(e) do { error = (e); goto bad;} while (0)
65932176cfdSRui Paulo struct ieee80211_node *ni = NULL;
66032176cfdSRui Paulo struct ieee80211vap *vap;
66132176cfdSRui Paulo struct ieee80211_frame *wh;
662085ff963SMatthew Dillon struct ieee80211com *ic = NULL;
66332176cfdSRui Paulo int error;
664085ff963SMatthew Dillon int ret;
66532176cfdSRui Paulo
666085ff963SMatthew Dillon #if defined(__DragonFly__)
667085ff963SMatthew Dillon struct ifaltq_subque *ifsq;
668f0a26983SSepherosa Ziehau ifsq = ifq_get_subq_default(&ifp->if_snd);
6694f655ef5SMatthew Dillon if (ifsq_is_oactive(ifsq)) {
670085ff963SMatthew Dillon #else
6714f655ef5SMatthew Dillon if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
672085ff963SMatthew Dillon #endif
67332176cfdSRui Paulo /*
67432176cfdSRui Paulo * Short-circuit requests if the vap is marked OACTIVE
67532176cfdSRui Paulo * as this can happen because a packet came down through
67632176cfdSRui Paulo * ieee80211_start before the vap entered RUN state in
67732176cfdSRui Paulo * which case it's ok to just drop the frame. This
67832176cfdSRui Paulo * should not be necessary but callers of if_output don't
67932176cfdSRui Paulo * check OACTIVE.
68032176cfdSRui Paulo */
68132176cfdSRui Paulo senderr(ENETDOWN);
68232176cfdSRui Paulo }
68332176cfdSRui Paulo vap = ifp->if_softc;
684085ff963SMatthew Dillon ic = vap->iv_ic;
68532176cfdSRui Paulo /*
68632176cfdSRui Paulo * Hand to the 802.3 code if not tagged as
68732176cfdSRui Paulo * a raw 802.11 frame.
68832176cfdSRui Paulo */
689085ff963SMatthew Dillon #if defined(__DragonFly__)
69032176cfdSRui Paulo if (dst->sa_family != AF_IEEE80211)
69134a60cf6SRui Paulo return vap->iv_output(ifp, m, dst, rt);
692085ff963SMatthew Dillon #else
693085ff963SMatthew Dillon if (dst->sa_family != AF_IEEE80211)
694085ff963SMatthew Dillon return vap->iv_output(ifp, m, dst, ro);
695085ff963SMatthew Dillon #endif
69632176cfdSRui Paulo #ifdef MAC
69732176cfdSRui Paulo error = mac_ifnet_check_transmit(ifp, m);
69832176cfdSRui Paulo if (error)
69932176cfdSRui Paulo senderr(error);
70032176cfdSRui Paulo #endif
70132176cfdSRui Paulo if (ifp->if_flags & IFF_MONITOR)
70232176cfdSRui Paulo senderr(ENETDOWN);
70332176cfdSRui Paulo if (!IFNET_IS_UP_RUNNING(ifp))
70432176cfdSRui Paulo senderr(ENETDOWN);
70532176cfdSRui Paulo if (vap->iv_state == IEEE80211_S_CAC) {
70632176cfdSRui Paulo IEEE80211_DPRINTF(vap,
70732176cfdSRui Paulo IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
70832176cfdSRui Paulo "block %s frame in CAC state\n", "raw data");
70932176cfdSRui Paulo vap->iv_stats.is_tx_badstate++;
71032176cfdSRui Paulo senderr(EIO); /* XXX */
711085ff963SMatthew Dillon } else if (vap->iv_state == IEEE80211_S_SCAN)
712085ff963SMatthew Dillon senderr(EIO);
71332176cfdSRui Paulo /* XXX bypass bridge, pfil, carp, etc. */
71432176cfdSRui Paulo
71532176cfdSRui Paulo if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_ack))
71632176cfdSRui Paulo senderr(EIO); /* XXX */
71732176cfdSRui Paulo wh = mtod(m, struct ieee80211_frame *);
71832176cfdSRui Paulo if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
71932176cfdSRui Paulo IEEE80211_FC0_VERSION_0)
72032176cfdSRui Paulo senderr(EIO); /* XXX */
72132176cfdSRui Paulo
72232176cfdSRui Paulo /* locate destination node */
72332176cfdSRui Paulo switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
72432176cfdSRui Paulo case IEEE80211_FC1_DIR_NODS:
72532176cfdSRui Paulo case IEEE80211_FC1_DIR_FROMDS:
72632176cfdSRui Paulo ni = ieee80211_find_txnode(vap, wh->i_addr1);
72732176cfdSRui Paulo break;
72832176cfdSRui Paulo case IEEE80211_FC1_DIR_TODS:
72932176cfdSRui Paulo case IEEE80211_FC1_DIR_DSTODS:
73032176cfdSRui Paulo if (m->m_pkthdr.len < sizeof(struct ieee80211_frame))
73132176cfdSRui Paulo senderr(EIO); /* XXX */
73232176cfdSRui Paulo ni = ieee80211_find_txnode(vap, wh->i_addr3);
73332176cfdSRui Paulo break;
73432176cfdSRui Paulo default:
73532176cfdSRui Paulo senderr(EIO); /* XXX */
73632176cfdSRui Paulo }
73732176cfdSRui Paulo if (ni == NULL) {
73832176cfdSRui Paulo /*
73932176cfdSRui Paulo * Permit packets w/ bpf params through regardless
74032176cfdSRui Paulo * (see below about sa_len).
74132176cfdSRui Paulo */
74232176cfdSRui Paulo if (dst->sa_len == 0)
74332176cfdSRui Paulo senderr(EHOSTUNREACH);
74432176cfdSRui Paulo ni = ieee80211_ref_node(vap->iv_bss);
74532176cfdSRui Paulo }
74632176cfdSRui Paulo
74732176cfdSRui Paulo /*
74832176cfdSRui Paulo * Sanitize mbuf for net80211 flags leaked from above.
74932176cfdSRui Paulo *
75032176cfdSRui Paulo * NB: This must be done before ieee80211_classify as
75132176cfdSRui Paulo * it marks EAPOL in frames with M_EAPOL.
75232176cfdSRui Paulo */
75332176cfdSRui Paulo m->m_flags &= ~M_80211_TX;
75432176cfdSRui Paulo
75532176cfdSRui Paulo /* calculate priority so drivers can find the tx queue */
75632176cfdSRui Paulo /* XXX assumes an 802.3 frame */
75732176cfdSRui Paulo if (ieee80211_classify(ni, m))
75832176cfdSRui Paulo senderr(EIO); /* XXX */
75932176cfdSRui Paulo
7604f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
76132176cfdSRui Paulo IEEE80211_NODE_STAT(ni, tx_data);
76232176cfdSRui Paulo if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
76332176cfdSRui Paulo IEEE80211_NODE_STAT(ni, tx_mcast);
76432176cfdSRui Paulo m->m_flags |= M_MCAST;
76532176cfdSRui Paulo } else
76632176cfdSRui Paulo IEEE80211_NODE_STAT(ni, tx_ucast);
76732176cfdSRui Paulo /* NB: ieee80211_encap does not include 802.11 header */
76832176cfdSRui Paulo IEEE80211_NODE_STAT_ADD(ni, tx_bytes, m->m_pkthdr.len);
76932176cfdSRui Paulo
770085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
771085ff963SMatthew Dillon
77232176cfdSRui Paulo /*
77332176cfdSRui Paulo * NB: DLT_IEEE802_11_RADIO identifies the parameters are
77432176cfdSRui Paulo * present by setting the sa_len field of the sockaddr (yes,
77532176cfdSRui Paulo * this is a hack).
77632176cfdSRui Paulo * NB: we assume sa_data is suitably aligned to cast.
77732176cfdSRui Paulo */
778085ff963SMatthew Dillon ret = ieee80211_raw_output(vap, ni, m,
77932176cfdSRui Paulo (const struct ieee80211_bpf_params *)(dst->sa_len ?
78032176cfdSRui Paulo dst->sa_data : NULL));
781085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
782085ff963SMatthew Dillon return (ret);
78332176cfdSRui Paulo bad:
78432176cfdSRui Paulo if (m != NULL)
78532176cfdSRui Paulo m_freem(m);
78632176cfdSRui Paulo if (ni != NULL)
78732176cfdSRui Paulo ieee80211_free_node(ni);
7884f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
78932176cfdSRui Paulo return error;
79032176cfdSRui Paulo #undef senderr
79132176cfdSRui Paulo }
79232176cfdSRui Paulo
79332176cfdSRui Paulo /*
79432176cfdSRui Paulo * Set the direction field and address fields of an outgoing
79532176cfdSRui Paulo * frame. Note this should be called early on in constructing
79632176cfdSRui Paulo * a frame as it sets i_fc[1]; other bits can then be or'd in.
79732176cfdSRui Paulo */
79832176cfdSRui Paulo void
79932176cfdSRui Paulo ieee80211_send_setup(
800841ab66cSSepherosa Ziehau struct ieee80211_node *ni,
80132176cfdSRui Paulo struct mbuf *m,
80232176cfdSRui Paulo int type, int tid,
803841ab66cSSepherosa Ziehau const uint8_t sa[IEEE80211_ADDR_LEN],
804841ab66cSSepherosa Ziehau const uint8_t da[IEEE80211_ADDR_LEN],
805841ab66cSSepherosa Ziehau const uint8_t bssid[IEEE80211_ADDR_LEN])
806841ab66cSSepherosa Ziehau {
807841ab66cSSepherosa Ziehau #define WH4(wh) ((struct ieee80211_frame_addr4 *)wh)
80832176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
809085ff963SMatthew Dillon struct ieee80211_tx_ampdu *tap;
81032176cfdSRui Paulo struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
81132176cfdSRui Paulo ieee80211_seq seqno;
812841ab66cSSepherosa Ziehau
813085ff963SMatthew Dillon IEEE80211_TX_LOCK_ASSERT(ni->ni_ic);
814085ff963SMatthew Dillon
815841ab66cSSepherosa Ziehau wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | type;
816841ab66cSSepherosa Ziehau if ((type & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_DATA) {
81732176cfdSRui Paulo switch (vap->iv_opmode) {
818841ab66cSSepherosa Ziehau case IEEE80211_M_STA:
819841ab66cSSepherosa Ziehau wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
820841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr1, bssid);
821841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr2, sa);
822841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr3, da);
823841ab66cSSepherosa Ziehau break;
824841ab66cSSepherosa Ziehau case IEEE80211_M_IBSS:
825841ab66cSSepherosa Ziehau case IEEE80211_M_AHDEMO:
826841ab66cSSepherosa Ziehau wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
827841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr1, da);
828841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr2, sa);
829841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
830841ab66cSSepherosa Ziehau break;
831841ab66cSSepherosa Ziehau case IEEE80211_M_HOSTAP:
832841ab66cSSepherosa Ziehau wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
833841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr1, da);
834841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr2, bssid);
835841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr3, sa);
836841ab66cSSepherosa Ziehau break;
83732176cfdSRui Paulo case IEEE80211_M_WDS:
83832176cfdSRui Paulo wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
83932176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr1, da);
84032176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
84132176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, da);
84232176cfdSRui Paulo IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
84332176cfdSRui Paulo break;
84432176cfdSRui Paulo case IEEE80211_M_MBSS:
84532176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
84632176cfdSRui Paulo if (IEEE80211_IS_MULTICAST(da)) {
84732176cfdSRui Paulo wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
84832176cfdSRui Paulo /* XXX next hop */
84932176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr1, da);
85032176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2,
85132176cfdSRui Paulo vap->iv_myaddr);
85232176cfdSRui Paulo } else {
85332176cfdSRui Paulo wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
85432176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr1, da);
85532176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2,
85632176cfdSRui Paulo vap->iv_myaddr);
85732176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, da);
85832176cfdSRui Paulo IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, sa);
85932176cfdSRui Paulo }
86032176cfdSRui Paulo #endif
86132176cfdSRui Paulo break;
862841ab66cSSepherosa Ziehau case IEEE80211_M_MONITOR: /* NB: to quiet compiler */
863841ab66cSSepherosa Ziehau break;
864841ab66cSSepherosa Ziehau }
865841ab66cSSepherosa Ziehau } else {
866841ab66cSSepherosa Ziehau wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
867841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr1, da);
868841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr2, sa);
86932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
87032176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS)
87132176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, sa);
87232176cfdSRui Paulo else
87332176cfdSRui Paulo #endif
874841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr3, bssid);
875841ab66cSSepherosa Ziehau }
876841ab66cSSepherosa Ziehau *(uint16_t *)&wh->i_dur[0] = 0;
87732176cfdSRui Paulo
878085ff963SMatthew Dillon tap = &ni->ni_tx_ampdu[tid];
879085ff963SMatthew Dillon if (tid != IEEE80211_NONQOS_TID && IEEE80211_AMPDU_RUNNING(tap))
880085ff963SMatthew Dillon m->m_flags |= M_AMPDU_MPDU;
881085ff963SMatthew Dillon else {
8824f898719SImre Vadász if (IEEE80211_HAS_SEQ(type & IEEE80211_FC0_TYPE_MASK,
8834f898719SImre Vadász type & IEEE80211_FC0_SUBTYPE_MASK))
88432176cfdSRui Paulo seqno = ni->ni_txseqs[tid]++;
8854f898719SImre Vadász else
8864f898719SImre Vadász seqno = 0;
8874f655ef5SMatthew Dillon
888085ff963SMatthew Dillon *(uint16_t *)&wh->i_seq[0] =
889085ff963SMatthew Dillon htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
89032176cfdSRui Paulo M_SEQNO_SET(m, seqno);
891085ff963SMatthew Dillon }
89232176cfdSRui Paulo
89332176cfdSRui Paulo if (IEEE80211_IS_MULTICAST(wh->i_addr1))
89432176cfdSRui Paulo m->m_flags |= M_MCAST;
895841ab66cSSepherosa Ziehau #undef WH4
896841ab66cSSepherosa Ziehau }
897841ab66cSSepherosa Ziehau
898f186073cSJoerg Sonnenberger /*
899f186073cSJoerg Sonnenberger * Send a management frame to the specified node. The node pointer
900f186073cSJoerg Sonnenberger * must have a reference as the pointer will be passed to the driver
901f186073cSJoerg Sonnenberger * and potentially held for a long time. If the frame is successfully
9024f655ef5SMatthew Dillon * dispatched to the driver, then it is responsible forkfreeing the
9034f655ef5SMatthew Dillon * reference (and potentiallykfree'ing up any associated storage);
90432176cfdSRui Paulo * otherwise deal with reclaiming any reference (on error).
905f186073cSJoerg Sonnenberger */
90632176cfdSRui Paulo int
90732176cfdSRui Paulo ieee80211_mgmt_output(struct ieee80211_node *ni, struct mbuf *m, int type,
90832176cfdSRui Paulo struct ieee80211_bpf_params *params)
909f186073cSJoerg Sonnenberger {
91032176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
91132176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
912f186073cSJoerg Sonnenberger struct ieee80211_frame *wh;
913085ff963SMatthew Dillon int ret;
914085ff963SMatthew Dillon
915f186073cSJoerg Sonnenberger KASSERT(ni != NULL, ("null node"));
916f186073cSJoerg Sonnenberger
91732176cfdSRui Paulo if (vap->iv_state == IEEE80211_S_CAC) {
91832176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
91932176cfdSRui Paulo ni, "block %s frame in CAC state",
9204f655ef5SMatthew Dillon ieee80211_mgt_subtype_name(type));
92132176cfdSRui Paulo vap->iv_stats.is_tx_badstate++;
92232176cfdSRui Paulo ieee80211_free_node(ni);
92332176cfdSRui Paulo m_freem(m);
92432176cfdSRui Paulo return EIO; /* XXX */
92532176cfdSRui Paulo }
92632176cfdSRui Paulo
927b5523eacSSascha Wildner M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
92832176cfdSRui Paulo if (m == NULL) {
92932176cfdSRui Paulo ieee80211_free_node(ni);
930f186073cSJoerg Sonnenberger return ENOMEM;
93132176cfdSRui Paulo }
932f186073cSJoerg Sonnenberger
933085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
934085ff963SMatthew Dillon
935f186073cSJoerg Sonnenberger wh = mtod(m, struct ieee80211_frame *);
93632176cfdSRui Paulo ieee80211_send_setup(ni, m,
93732176cfdSRui Paulo IEEE80211_FC0_TYPE_MGT | type, IEEE80211_NONQOS_TID,
93832176cfdSRui Paulo vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
93932176cfdSRui Paulo if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
94032176cfdSRui Paulo IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_AUTH, wh->i_addr1,
94132176cfdSRui Paulo "encrypting frame (%s)", __func__);
942085ff963SMatthew Dillon wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
943f186073cSJoerg Sonnenberger }
94432176cfdSRui Paulo m->m_flags |= M_ENCAP; /* mark encapsulated */
94532176cfdSRui Paulo
94632176cfdSRui Paulo KASSERT(type != IEEE80211_FC0_SUBTYPE_PROBE_RESP, ("probe response?"));
94732176cfdSRui Paulo M_WME_SETAC(m, params->ibp_pri);
94832176cfdSRui Paulo
949841ab66cSSepherosa Ziehau #ifdef IEEE80211_DEBUG
950841ab66cSSepherosa Ziehau /* avoid printing too many frames */
95132176cfdSRui Paulo if ((ieee80211_msg_debug(vap) && doprint(vap, type)) ||
95232176cfdSRui Paulo ieee80211_msg_dumppkts(vap)) {
9531e290df3SAntonio Huete Jimenez kprintf("[%s] send %s on channel %u\n",
954085ff963SMatthew Dillon ether_sprintf(wh->i_addr1),
9554f655ef5SMatthew Dillon ieee80211_mgt_subtype_name(type),
956841ab66cSSepherosa Ziehau ieee80211_chan2ieee(ic, ic->ic_curchan));
957841ab66cSSepherosa Ziehau }
958841ab66cSSepherosa Ziehau #endif
959841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_mgmt);
96032176cfdSRui Paulo
961085ff963SMatthew Dillon ret = ieee80211_raw_output(vap, ni, m, params);
962085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
963085ff963SMatthew Dillon return (ret);
964f186073cSJoerg Sonnenberger }
965f186073cSJoerg Sonnenberger
9664f655ef5SMatthew Dillon static void
9674f655ef5SMatthew Dillon ieee80211_nulldata_transmitted(struct ieee80211_node *ni, void *arg,
9684f655ef5SMatthew Dillon int status)
9694f655ef5SMatthew Dillon {
9704f655ef5SMatthew Dillon struct ieee80211vap *vap = ni->ni_vap;
9714f655ef5SMatthew Dillon
9724f655ef5SMatthew Dillon wakeup(vap);
9734f655ef5SMatthew Dillon }
9744f655ef5SMatthew Dillon
975f186073cSJoerg Sonnenberger /*
97632176cfdSRui Paulo * Send a null data frame to the specified node. If the station
97732176cfdSRui Paulo * is setup for QoS then a QoS Null Data frame is constructed.
97832176cfdSRui Paulo * If this is a WDS station then a 4-address frame is constructed.
979f186073cSJoerg Sonnenberger *
980841ab66cSSepherosa Ziehau * NB: the caller is assumed to have setup a node reference
981841ab66cSSepherosa Ziehau * for use; this is necessary to deal with a race condition
98232176cfdSRui Paulo * when probing for inactive stations. Like ieee80211_mgmt_output
98332176cfdSRui Paulo * we must cleanup any node reference on error; however we
98432176cfdSRui Paulo * can safely just unref it as we know it will never be the
98532176cfdSRui Paulo * last reference to the node.
986841ab66cSSepherosa Ziehau */
987841ab66cSSepherosa Ziehau int
988841ab66cSSepherosa Ziehau ieee80211_send_nulldata(struct ieee80211_node *ni)
989841ab66cSSepherosa Ziehau {
99032176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
991841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic;
992841ab66cSSepherosa Ziehau struct mbuf *m;
993841ab66cSSepherosa Ziehau struct ieee80211_frame *wh;
99432176cfdSRui Paulo int hdrlen;
99532176cfdSRui Paulo uint8_t *frm;
996085ff963SMatthew Dillon int ret;
997841ab66cSSepherosa Ziehau
99832176cfdSRui Paulo if (vap->iv_state == IEEE80211_S_CAC) {
99932176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT | IEEE80211_MSG_DOTH,
100032176cfdSRui Paulo ni, "block %s frame in CAC state", "null data");
100132176cfdSRui Paulo ieee80211_unref_node(&ni);
100232176cfdSRui Paulo vap->iv_stats.is_tx_badstate++;
100332176cfdSRui Paulo return EIO; /* XXX */
100432176cfdSRui Paulo }
100532176cfdSRui Paulo
100632176cfdSRui Paulo if (ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT))
100732176cfdSRui Paulo hdrlen = sizeof(struct ieee80211_qosframe);
100832176cfdSRui Paulo else
100932176cfdSRui Paulo hdrlen = sizeof(struct ieee80211_frame);
101032176cfdSRui Paulo /* NB: only WDS vap's get 4-address frames */
101132176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_WDS)
101232176cfdSRui Paulo hdrlen += IEEE80211_ADDR_LEN;
101332176cfdSRui Paulo if (ic->ic_flags & IEEE80211_F_DATAPAD)
101432176cfdSRui Paulo hdrlen = roundup(hdrlen, sizeof(uint32_t));
101532176cfdSRui Paulo
101632176cfdSRui Paulo m = ieee80211_getmgtframe(&frm, ic->ic_headroom + hdrlen, 0);
1017841ab66cSSepherosa Ziehau if (m == NULL) {
1018841ab66cSSepherosa Ziehau /* XXX debug msg */
1019841ab66cSSepherosa Ziehau ieee80211_unref_node(&ni);
102032176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
1021841ab66cSSepherosa Ziehau return ENOMEM;
1022841ab66cSSepherosa Ziehau }
102332176cfdSRui Paulo KASSERT(M_LEADINGSPACE(m) >= hdrlen,
102432176cfdSRui Paulo ("leading space %zd", M_LEADINGSPACE(m)));
1025b5523eacSSascha Wildner M_PREPEND(m, hdrlen, M_NOWAIT);
102632176cfdSRui Paulo if (m == NULL) {
102732176cfdSRui Paulo /* NB: cannot happen */
102832176cfdSRui Paulo ieee80211_free_node(ni);
102932176cfdSRui Paulo return ENOMEM;
103032176cfdSRui Paulo }
1031841ab66cSSepherosa Ziehau
1032085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
1033085ff963SMatthew Dillon
103432176cfdSRui Paulo wh = mtod(m, struct ieee80211_frame *); /* NB: a little lie */
103532176cfdSRui Paulo if (ni->ni_flags & IEEE80211_NODE_QOS) {
103632176cfdSRui Paulo const int tid = WME_AC_TO_TID(WME_AC_BE);
103732176cfdSRui Paulo uint8_t *qos;
103832176cfdSRui Paulo
103932176cfdSRui Paulo ieee80211_send_setup(ni, m,
104032176cfdSRui Paulo IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS_NULL,
104132176cfdSRui Paulo tid, vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
104232176cfdSRui Paulo
104332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_WDS)
104432176cfdSRui Paulo qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
104532176cfdSRui Paulo else
104632176cfdSRui Paulo qos = ((struct ieee80211_qosframe *) wh)->i_qos;
104732176cfdSRui Paulo qos[0] = tid & IEEE80211_QOS_TID;
104832176cfdSRui Paulo if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[WME_AC_BE].wmep_noackPolicy)
104932176cfdSRui Paulo qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
105032176cfdSRui Paulo qos[1] = 0;
105132176cfdSRui Paulo } else {
105232176cfdSRui Paulo ieee80211_send_setup(ni, m,
1053841ab66cSSepherosa Ziehau IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA,
105432176cfdSRui Paulo IEEE80211_NONQOS_TID,
105532176cfdSRui Paulo vap->iv_myaddr, ni->ni_macaddr, ni->ni_bssid);
105632176cfdSRui Paulo }
105732176cfdSRui Paulo if (vap->iv_opmode != IEEE80211_M_WDS) {
1058841ab66cSSepherosa Ziehau /* NB: power management bit is never sent by an AP */
1059841ab66cSSepherosa Ziehau if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
106032176cfdSRui Paulo vap->iv_opmode != IEEE80211_M_HOSTAP)
1061841ab66cSSepherosa Ziehau wh->i_fc[1] |= IEEE80211_FC1_PWR_MGT;
106232176cfdSRui Paulo }
10634f655ef5SMatthew Dillon if ((ic->ic_flags & IEEE80211_F_SCAN) &&
10644f655ef5SMatthew Dillon (ni->ni_flags & IEEE80211_NODE_PWR_MGT)) {
10654f655ef5SMatthew Dillon ieee80211_add_callback(m, ieee80211_nulldata_transmitted,
10664f655ef5SMatthew Dillon NULL);
10674f655ef5SMatthew Dillon }
106832176cfdSRui Paulo m->m_len = m->m_pkthdr.len = hdrlen;
106932176cfdSRui Paulo m->m_flags |= M_ENCAP; /* mark encapsulated */
107032176cfdSRui Paulo
107132176cfdSRui Paulo M_WME_SETAC(m, WME_AC_BE);
1072841ab66cSSepherosa Ziehau
1073841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_data);
1074841ab66cSSepherosa Ziehau
107532176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS, ni,
107632176cfdSRui Paulo "send %snull data frame on channel %u, pwr mgt %s",
107732176cfdSRui Paulo ni->ni_flags & IEEE80211_NODE_QOS ? "QoS " : "",
1078841ab66cSSepherosa Ziehau ieee80211_chan2ieee(ic, ic->ic_curchan),
1079841ab66cSSepherosa Ziehau wh->i_fc[1] & IEEE80211_FC1_PWR_MGT ? "ena" : "dis");
1080841ab66cSSepherosa Ziehau
1081085ff963SMatthew Dillon ret = ieee80211_raw_output(vap, ni, m, NULL);
1082085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
1083085ff963SMatthew Dillon return (ret);
1084841ab66cSSepherosa Ziehau }
1085841ab66cSSepherosa Ziehau
1086841ab66cSSepherosa Ziehau /*
1087841ab66cSSepherosa Ziehau * Assign priority to a frame based on any vlan tag assigned
1088841ab66cSSepherosa Ziehau * to the station and/or any Diffserv setting in an IP header.
1089841ab66cSSepherosa Ziehau * Finally, if an ACM policy is setup (in station mode) it's
1090841ab66cSSepherosa Ziehau * applied.
1091841ab66cSSepherosa Ziehau */
1092841ab66cSSepherosa Ziehau int
109332176cfdSRui Paulo ieee80211_classify(struct ieee80211_node *ni, struct mbuf *m)
1094841ab66cSSepherosa Ziehau {
109532176cfdSRui Paulo const struct ether_header *eh = mtod(m, struct ether_header *);
109632176cfdSRui Paulo int v_wme_ac, d_wme_ac, ac;
1097841ab66cSSepherosa Ziehau
109832176cfdSRui Paulo /*
109932176cfdSRui Paulo * Always promote PAE/EAPOL frames to high priority.
110032176cfdSRui Paulo */
110132176cfdSRui Paulo if (eh->ether_type == htons(ETHERTYPE_PAE)) {
110232176cfdSRui Paulo /* NB: mark so others don't need to check header */
110332176cfdSRui Paulo m->m_flags |= M_EAPOL;
110432176cfdSRui Paulo ac = WME_AC_VO;
110532176cfdSRui Paulo goto done;
110632176cfdSRui Paulo }
110732176cfdSRui Paulo /*
110832176cfdSRui Paulo * Non-qos traffic goes to BE.
110932176cfdSRui Paulo */
1110841ab66cSSepherosa Ziehau if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0) {
1111841ab66cSSepherosa Ziehau ac = WME_AC_BE;
1112841ab66cSSepherosa Ziehau goto done;
1113841ab66cSSepherosa Ziehau }
1114841ab66cSSepherosa Ziehau
1115841ab66cSSepherosa Ziehau /*
1116841ab66cSSepherosa Ziehau * If node has a vlan tag then all traffic
1117841ab66cSSepherosa Ziehau * to it must have a matching tag.
1118841ab66cSSepherosa Ziehau */
1119841ab66cSSepherosa Ziehau v_wme_ac = 0;
1120841ab66cSSepherosa Ziehau if (ni->ni_vlan != 0) {
112132176cfdSRui Paulo if ((m->m_flags & M_VLANTAG) == 0) {
1122841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_novlantag);
1123841ab66cSSepherosa Ziehau return 1;
1124841ab66cSSepherosa Ziehau }
1125085ff963SMatthew Dillon #if defined(__DragonFly__)
112632176cfdSRui Paulo if (EVL_VLANOFTAG(m->m_pkthdr.ether_vlantag) !=
1127841ab66cSSepherosa Ziehau EVL_VLANOFTAG(ni->ni_vlan)) {
1128841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1129841ab66cSSepherosa Ziehau return 1;
1130841ab66cSSepherosa Ziehau }
1131085ff963SMatthew Dillon #else
1132085ff963SMatthew Dillon if (EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) !=
1133085ff963SMatthew Dillon EVL_VLANOFTAG(ni->ni_vlan)) {
1134085ff963SMatthew Dillon IEEE80211_NODE_STAT(ni, tx_vlanmismatch);
1135085ff963SMatthew Dillon return 1;
1136085ff963SMatthew Dillon }
1137085ff963SMatthew Dillon #endif
1138841ab66cSSepherosa Ziehau /* map vlan priority to AC */
113932176cfdSRui Paulo v_wme_ac = TID_TO_WME_AC(EVL_PRIOFTAG(ni->ni_vlan));
1140841ab66cSSepherosa Ziehau }
1141841ab66cSSepherosa Ziehau
114232176cfdSRui Paulo /* XXX m_copydata may be too slow for fast path */
1143841ab66cSSepherosa Ziehau #ifdef INET
1144841ab66cSSepherosa Ziehau if (eh->ether_type == htons(ETHERTYPE_IP)) {
114532176cfdSRui Paulo uint8_t tos;
1146841ab66cSSepherosa Ziehau /*
114732176cfdSRui Paulo * IP frame, map the DSCP bits from the TOS field.
1148841ab66cSSepherosa Ziehau */
114932176cfdSRui Paulo /* NB: ip header may not be in first mbuf */
115032176cfdSRui Paulo m_copydata(m, sizeof(struct ether_header) +
115132176cfdSRui Paulo offsetof(struct ip, ip_tos), sizeof(tos), &tos);
115232176cfdSRui Paulo tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
115332176cfdSRui Paulo d_wme_ac = TID_TO_WME_AC(tos);
1154841ab66cSSepherosa Ziehau } else {
1155841ab66cSSepherosa Ziehau #endif /* INET */
115632176cfdSRui Paulo #ifdef INET6
115732176cfdSRui Paulo if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
115832176cfdSRui Paulo uint32_t flow;
115932176cfdSRui Paulo uint8_t tos;
116032176cfdSRui Paulo /*
1161085ff963SMatthew Dillon * IPv6 frame, map the DSCP bits from the traffic class field.
116232176cfdSRui Paulo */
116332176cfdSRui Paulo m_copydata(m, sizeof(struct ether_header) +
1164*05d02a38SAaron LI offsetof(struct ip6_hdr, ip6_flow), sizeof(flow), &flow);
116532176cfdSRui Paulo tos = (uint8_t)(ntohl(flow) >> 20);
116632176cfdSRui Paulo tos >>= 5; /* NB: ECN + low 3 bits of DSCP */
116732176cfdSRui Paulo d_wme_ac = TID_TO_WME_AC(tos);
116832176cfdSRui Paulo } else {
116932176cfdSRui Paulo #endif /* INET6 */
1170841ab66cSSepherosa Ziehau d_wme_ac = WME_AC_BE;
117132176cfdSRui Paulo #ifdef INET6
117232176cfdSRui Paulo }
117332176cfdSRui Paulo #endif
1174841ab66cSSepherosa Ziehau #ifdef INET
1175841ab66cSSepherosa Ziehau }
1176841ab66cSSepherosa Ziehau #endif
1177841ab66cSSepherosa Ziehau /*
1178841ab66cSSepherosa Ziehau * Use highest priority AC.
1179841ab66cSSepherosa Ziehau */
1180841ab66cSSepherosa Ziehau if (v_wme_ac > d_wme_ac)
1181841ab66cSSepherosa Ziehau ac = v_wme_ac;
1182841ab66cSSepherosa Ziehau else
1183841ab66cSSepherosa Ziehau ac = d_wme_ac;
1184841ab66cSSepherosa Ziehau
1185841ab66cSSepherosa Ziehau /*
1186841ab66cSSepherosa Ziehau * Apply ACM policy.
1187841ab66cSSepherosa Ziehau */
118832176cfdSRui Paulo if (ni->ni_vap->iv_opmode == IEEE80211_M_STA) {
1189841ab66cSSepherosa Ziehau static const int acmap[4] = {
1190841ab66cSSepherosa Ziehau WME_AC_BK, /* WME_AC_BE */
1191841ab66cSSepherosa Ziehau WME_AC_BK, /* WME_AC_BK */
1192841ab66cSSepherosa Ziehau WME_AC_BE, /* WME_AC_VI */
1193841ab66cSSepherosa Ziehau WME_AC_VI, /* WME_AC_VO */
1194841ab66cSSepherosa Ziehau };
119532176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
119632176cfdSRui Paulo
1197841ab66cSSepherosa Ziehau while (ac != WME_AC_BK &&
1198841ab66cSSepherosa Ziehau ic->ic_wme.wme_wmeBssChanParams.cap_wmeParams[ac].wmep_acm)
1199841ab66cSSepherosa Ziehau ac = acmap[ac];
1200841ab66cSSepherosa Ziehau }
1201841ab66cSSepherosa Ziehau done:
1202841ab66cSSepherosa Ziehau M_WME_SETAC(m, ac);
1203841ab66cSSepherosa Ziehau return 0;
1204841ab66cSSepherosa Ziehau }
1205841ab66cSSepherosa Ziehau
1206841ab66cSSepherosa Ziehau /*
1207841ab66cSSepherosa Ziehau * Insure there is sufficient contiguous space to encapsulate the
1208841ab66cSSepherosa Ziehau * 802.11 data frame. If room isn't already there, arrange for it.
1209841ab66cSSepherosa Ziehau * Drivers and cipher modules assume we have done the necessary work
1210841ab66cSSepherosa Ziehau * and fail rudely if they don't find the space they need.
1211841ab66cSSepherosa Ziehau */
121232176cfdSRui Paulo struct mbuf *
121332176cfdSRui Paulo ieee80211_mbuf_adjust(struct ieee80211vap *vap, int hdrsize,
1214841ab66cSSepherosa Ziehau struct ieee80211_key *key, struct mbuf *m)
1215841ab66cSSepherosa Ziehau {
1216841ab66cSSepherosa Ziehau #define TO_BE_RECLAIMED (sizeof(struct ether_header) - sizeof(struct llc))
121732176cfdSRui Paulo int needed_space = vap->iv_ic->ic_headroom + hdrsize;
1218841ab66cSSepherosa Ziehau
1219841ab66cSSepherosa Ziehau if (key != NULL) {
1220841ab66cSSepherosa Ziehau /* XXX belongs in crypto code? */
1221841ab66cSSepherosa Ziehau needed_space += key->wk_cipher->ic_header;
1222841ab66cSSepherosa Ziehau /* XXX frags */
1223841ab66cSSepherosa Ziehau /*
1224841ab66cSSepherosa Ziehau * When crypto is being done in the host we must insure
1225841ab66cSSepherosa Ziehau * the data are writable for the cipher routines; clone
1226841ab66cSSepherosa Ziehau * a writable mbuf chain.
1227841ab66cSSepherosa Ziehau * XXX handle SWMIC specially
1228841ab66cSSepherosa Ziehau */
122932176cfdSRui Paulo if (key->wk_flags & (IEEE80211_KEY_SWENCRYPT|IEEE80211_KEY_SWENMIC)) {
1230b5523eacSSascha Wildner m = m_unshare(m, M_NOWAIT);
1231841ab66cSSepherosa Ziehau if (m == NULL) {
123232176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1233841ab66cSSepherosa Ziehau "%s: cannot get writable mbuf\n", __func__);
123432176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++; /* XXX new stat */
1235841ab66cSSepherosa Ziehau return NULL;
1236841ab66cSSepherosa Ziehau }
1237841ab66cSSepherosa Ziehau }
1238841ab66cSSepherosa Ziehau }
1239841ab66cSSepherosa Ziehau /*
1240841ab66cSSepherosa Ziehau * We know we are called just before stripping an Ethernet
1241841ab66cSSepherosa Ziehau * header and prepending an LLC header. This means we know
1242841ab66cSSepherosa Ziehau * there will be
1243841ab66cSSepherosa Ziehau * sizeof(struct ether_header) - sizeof(struct llc)
1244841ab66cSSepherosa Ziehau * bytes recovered to which we need additional space for the
1245841ab66cSSepherosa Ziehau * 802.11 header and any crypto header.
1246841ab66cSSepherosa Ziehau */
1247841ab66cSSepherosa Ziehau /* XXX check trailing space and copy instead? */
1248841ab66cSSepherosa Ziehau if (M_LEADINGSPACE(m) < needed_space - TO_BE_RECLAIMED) {
1249b5523eacSSascha Wildner struct mbuf *n = m_gethdr(M_NOWAIT, m->m_type);
1250841ab66cSSepherosa Ziehau if (n == NULL) {
125132176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_OUTPUT,
1252841ab66cSSepherosa Ziehau "%s: cannot expand storage\n", __func__);
125332176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
1254841ab66cSSepherosa Ziehau m_freem(m);
1255841ab66cSSepherosa Ziehau return NULL;
1256841ab66cSSepherosa Ziehau }
1257085ff963SMatthew Dillon #if defined(__DragonFly__)
1258841ab66cSSepherosa Ziehau KASSERT(needed_space <= MHLEN,
1259085ff963SMatthew Dillon ("not enough room, need %u got %zd\n", needed_space, MHLEN));
1260085ff963SMatthew Dillon #else
1261085ff963SMatthew Dillon KASSERT(needed_space <= MHLEN,
1262085ff963SMatthew Dillon ("not enough room, need %u got %d\n", needed_space, MHLEN));
1263085ff963SMatthew Dillon #endif
1264841ab66cSSepherosa Ziehau /*
1265841ab66cSSepherosa Ziehau * Setup new mbuf to have leading space to prepend the
1266841ab66cSSepherosa Ziehau * 802.11 header and any crypto header bits that are
1267841ab66cSSepherosa Ziehau * required (the latter are added when the driver calls
1268841ab66cSSepherosa Ziehau * back to ieee80211_crypto_encap to do crypto encapsulation).
1269841ab66cSSepherosa Ziehau */
1270841ab66cSSepherosa Ziehau /* NB: must be first 'cuz it clobbers m_data */
1271841ab66cSSepherosa Ziehau m_move_pkthdr(n, m);
1272841ab66cSSepherosa Ziehau n->m_len = 0; /* NB: m_gethdr does not set */
1273841ab66cSSepherosa Ziehau n->m_data += needed_space;
1274841ab66cSSepherosa Ziehau /*
1275841ab66cSSepherosa Ziehau * Pull up Ethernet header to create the expected layout.
1276841ab66cSSepherosa Ziehau * We could use m_pullup but that's overkill (i.e. we don't
1277841ab66cSSepherosa Ziehau * need the actual data) and it cannot fail so do it inline
1278841ab66cSSepherosa Ziehau * for speed.
1279841ab66cSSepherosa Ziehau */
1280841ab66cSSepherosa Ziehau /* NB: struct ether_header is known to be contiguous */
1281841ab66cSSepherosa Ziehau n->m_len += sizeof(struct ether_header);
1282841ab66cSSepherosa Ziehau m->m_len -= sizeof(struct ether_header);
1283841ab66cSSepherosa Ziehau m->m_data += sizeof(struct ether_header);
1284841ab66cSSepherosa Ziehau /*
1285841ab66cSSepherosa Ziehau * Replace the head of the chain.
1286841ab66cSSepherosa Ziehau */
1287841ab66cSSepherosa Ziehau n->m_next = m;
1288841ab66cSSepherosa Ziehau m = n;
1289841ab66cSSepherosa Ziehau }
1290841ab66cSSepherosa Ziehau return m;
1291841ab66cSSepherosa Ziehau #undef TO_BE_RECLAIMED
1292841ab66cSSepherosa Ziehau }
1293841ab66cSSepherosa Ziehau
1294841ab66cSSepherosa Ziehau /*
1295841ab66cSSepherosa Ziehau * Return the transmit key to use in sending a unicast frame.
1296841ab66cSSepherosa Ziehau * If a unicast key is set we use that. When no unicast key is set
1297841ab66cSSepherosa Ziehau * we fall back to the default transmit key.
1298841ab66cSSepherosa Ziehau */
1299841ab66cSSepherosa Ziehau static __inline struct ieee80211_key *
130032176cfdSRui Paulo ieee80211_crypto_getucastkey(struct ieee80211vap *vap,
130132176cfdSRui Paulo struct ieee80211_node *ni)
1302841ab66cSSepherosa Ziehau {
130332176cfdSRui Paulo if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
130432176cfdSRui Paulo if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
130532176cfdSRui Paulo IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1306841ab66cSSepherosa Ziehau return NULL;
130732176cfdSRui Paulo return &vap->iv_nw_keys[vap->iv_def_txkey];
1308841ab66cSSepherosa Ziehau } else {
1309841ab66cSSepherosa Ziehau return &ni->ni_ucastkey;
1310841ab66cSSepherosa Ziehau }
1311841ab66cSSepherosa Ziehau }
1312841ab66cSSepherosa Ziehau
1313841ab66cSSepherosa Ziehau /*
1314841ab66cSSepherosa Ziehau * Return the transmit key to use in sending a multicast frame.
1315841ab66cSSepherosa Ziehau * Multicast traffic always uses the group key which is installed as
1316841ab66cSSepherosa Ziehau * the default tx key.
1317841ab66cSSepherosa Ziehau */
1318841ab66cSSepherosa Ziehau static __inline struct ieee80211_key *
131932176cfdSRui Paulo ieee80211_crypto_getmcastkey(struct ieee80211vap *vap,
132032176cfdSRui Paulo struct ieee80211_node *ni)
1321841ab66cSSepherosa Ziehau {
132232176cfdSRui Paulo if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE ||
132332176cfdSRui Paulo IEEE80211_KEY_UNDEFINED(&vap->iv_nw_keys[vap->iv_def_txkey]))
1324841ab66cSSepherosa Ziehau return NULL;
132532176cfdSRui Paulo return &vap->iv_nw_keys[vap->iv_def_txkey];
1326841ab66cSSepherosa Ziehau }
1327841ab66cSSepherosa Ziehau
1328841ab66cSSepherosa Ziehau /*
1329841ab66cSSepherosa Ziehau * Encapsulate an outbound data frame. The mbuf chain is updated.
1330841ab66cSSepherosa Ziehau * If an error is encountered NULL is returned. The caller is required
1331841ab66cSSepherosa Ziehau * to provide a node reference and pullup the ethernet header in the
1332841ab66cSSepherosa Ziehau * first mbuf.
133332176cfdSRui Paulo *
133432176cfdSRui Paulo * NB: Packet is assumed to be processed by ieee80211_classify which
133532176cfdSRui Paulo * marked EAPOL frames w/ M_EAPOL.
1336f186073cSJoerg Sonnenberger */
1337f186073cSJoerg Sonnenberger struct mbuf *
133832176cfdSRui Paulo ieee80211_encap(struct ieee80211vap *vap, struct ieee80211_node *ni,
133932176cfdSRui Paulo struct mbuf *m)
1340f186073cSJoerg Sonnenberger {
134132176cfdSRui Paulo #define WH4(wh) ((struct ieee80211_frame_addr4 *)(wh))
1342085ff963SMatthew Dillon #define MC01(mc) ((struct ieee80211_meshcntl_ae01 *)mc)
134332176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
134432176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
134532176cfdSRui Paulo struct ieee80211_mesh_state *ms = vap->iv_mesh;
134632176cfdSRui Paulo struct ieee80211_meshcntl_ae10 *mc;
1347085ff963SMatthew Dillon struct ieee80211_mesh_route *rt = NULL;
1348085ff963SMatthew Dillon int dir = -1;
134932176cfdSRui Paulo #endif
1350f186073cSJoerg Sonnenberger struct ether_header eh;
1351f186073cSJoerg Sonnenberger struct ieee80211_frame *wh;
1352841ab66cSSepherosa Ziehau struct ieee80211_key *key;
1353f186073cSJoerg Sonnenberger struct llc *llc;
135432176cfdSRui Paulo int hdrsize, hdrspace, datalen, addqos, txfrag, is4addr;
135532176cfdSRui Paulo ieee80211_seq seqno;
135632176cfdSRui Paulo int meshhdrsize, meshae;
135732176cfdSRui Paulo uint8_t *qos;
13584f655ef5SMatthew Dillon int is_amsdu = 0;
1359f186073cSJoerg Sonnenberger
1360085ff963SMatthew Dillon IEEE80211_TX_LOCK_ASSERT(ic);
1361085ff963SMatthew Dillon
136232176cfdSRui Paulo /*
136332176cfdSRui Paulo * Copy existing Ethernet header to a safe place. The
136432176cfdSRui Paulo * rest of the code assumes it's ok to strip it when
136532176cfdSRui Paulo * reorganizing state for the final encapsulation.
136632176cfdSRui Paulo */
1367841ab66cSSepherosa Ziehau KASSERT(m->m_len >= sizeof(eh), ("no ethernet header!"));
136832176cfdSRui Paulo ETHER_HEADER_COPY(&eh, mtod(m, caddr_t));
1369f186073cSJoerg Sonnenberger
1370841ab66cSSepherosa Ziehau /*
1371841ab66cSSepherosa Ziehau * Insure space for additional headers. First identify
1372841ab66cSSepherosa Ziehau * transmit key to use in calculating any buffer adjustments
1373841ab66cSSepherosa Ziehau * required. This is also used below to do privacy
1374841ab66cSSepherosa Ziehau * encapsulation work. Then calculate the 802.11 header
1375841ab66cSSepherosa Ziehau * size and any padding required by the driver.
1376841ab66cSSepherosa Ziehau *
1377841ab66cSSepherosa Ziehau * Note key may be NULL if we fall back to the default
1378841ab66cSSepherosa Ziehau * transmit key and that is not set. In that case the
1379841ab66cSSepherosa Ziehau * buffer may not be expanded as needed by the cipher
1380841ab66cSSepherosa Ziehau * routines, but they will/should discard it.
1381841ab66cSSepherosa Ziehau */
138232176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY) {
138332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_STA ||
138432176cfdSRui Paulo !IEEE80211_IS_MULTICAST(eh.ether_dhost) ||
138532176cfdSRui Paulo (vap->iv_opmode == IEEE80211_M_WDS &&
138632176cfdSRui Paulo (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
138732176cfdSRui Paulo key = ieee80211_crypto_getucastkey(vap, ni);
1388841ab66cSSepherosa Ziehau else
138932176cfdSRui Paulo key = ieee80211_crypto_getmcastkey(vap, ni);
139032176cfdSRui Paulo if (key == NULL && (m->m_flags & M_EAPOL) == 0) {
139132176cfdSRui Paulo IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
139232176cfdSRui Paulo eh.ether_dhost,
139332176cfdSRui Paulo "no default transmit key (%s) deftxkey %u",
139432176cfdSRui Paulo __func__, vap->iv_def_txkey);
139532176cfdSRui Paulo vap->iv_stats.is_tx_nodefkey++;
1396a92bce5eSSepherosa Ziehau goto bad;
1397841ab66cSSepherosa Ziehau }
1398841ab66cSSepherosa Ziehau } else
1399841ab66cSSepherosa Ziehau key = NULL;
1400841ab66cSSepherosa Ziehau /*
1401841ab66cSSepherosa Ziehau * XXX Some ap's don't handle QoS-encapsulated EAPOL
1402841ab66cSSepherosa Ziehau * frames so suppress use. This may be an issue if other
1403841ab66cSSepherosa Ziehau * ap's require all data frames to be QoS-encapsulated
1404841ab66cSSepherosa Ziehau * once negotiated in which case we'll need to make this
1405841ab66cSSepherosa Ziehau * configurable.
1406085ff963SMatthew Dillon * NB: mesh data frames are QoS.
1407841ab66cSSepherosa Ziehau */
1408085ff963SMatthew Dillon addqos = ((ni->ni_flags & (IEEE80211_NODE_QOS|IEEE80211_NODE_HT)) ||
1409085ff963SMatthew Dillon (vap->iv_opmode == IEEE80211_M_MBSS)) &&
141032176cfdSRui Paulo (m->m_flags & M_EAPOL) == 0;
1411841ab66cSSepherosa Ziehau if (addqos)
1412841ab66cSSepherosa Ziehau hdrsize = sizeof(struct ieee80211_qosframe);
1413841ab66cSSepherosa Ziehau else
1414841ab66cSSepherosa Ziehau hdrsize = sizeof(struct ieee80211_frame);
141532176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
141632176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) {
141732176cfdSRui Paulo /*
141832176cfdSRui Paulo * Mesh data frames are encapsulated according to the
141932176cfdSRui Paulo * rules of Section 11B.8.5 (p.139 of D3.0 spec).
142032176cfdSRui Paulo * o Group Addressed data (aka multicast) originating
142132176cfdSRui Paulo * at the local sta are sent w/ 3-address format and
142232176cfdSRui Paulo * address extension mode 00
142332176cfdSRui Paulo * o Individually Addressed data (aka unicast) originating
142432176cfdSRui Paulo * at the local sta are sent w/ 4-address format and
142532176cfdSRui Paulo * address extension mode 00
142632176cfdSRui Paulo * o Group Addressed data forwarded from a non-mesh sta are
142732176cfdSRui Paulo * sent w/ 3-address format and address extension mode 01
142832176cfdSRui Paulo * o Individually Address data from another sta are sent
142932176cfdSRui Paulo * w/ 4-address format and address extension mode 10
143032176cfdSRui Paulo */
143132176cfdSRui Paulo is4addr = 0; /* NB: don't use, disable */
1432085ff963SMatthew Dillon if (!IEEE80211_IS_MULTICAST(eh.ether_dhost)) {
1433085ff963SMatthew Dillon rt = ieee80211_mesh_rt_find(vap, eh.ether_dhost);
1434085ff963SMatthew Dillon KASSERT(rt != NULL, ("route is NULL"));
1435085ff963SMatthew Dillon dir = IEEE80211_FC1_DIR_DSTODS;
1436085ff963SMatthew Dillon hdrsize += IEEE80211_ADDR_LEN;
1437085ff963SMatthew Dillon if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1438085ff963SMatthew Dillon if (IEEE80211_ADDR_EQ(rt->rt_mesh_gate,
1439085ff963SMatthew Dillon vap->iv_myaddr)) {
1440085ff963SMatthew Dillon IEEE80211_NOTE_MAC(vap,
1441085ff963SMatthew Dillon IEEE80211_MSG_MESH,
1442085ff963SMatthew Dillon eh.ether_dhost,
1443085ff963SMatthew Dillon "%s", "trying to send to ourself");
1444085ff963SMatthew Dillon goto bad;
1445085ff963SMatthew Dillon }
1446085ff963SMatthew Dillon meshae = IEEE80211_MESH_AE_10;
1447085ff963SMatthew Dillon meshhdrsize =
1448085ff963SMatthew Dillon sizeof(struct ieee80211_meshcntl_ae10);
144932176cfdSRui Paulo } else {
1450085ff963SMatthew Dillon meshae = IEEE80211_MESH_AE_00;
1451085ff963SMatthew Dillon meshhdrsize =
1452085ff963SMatthew Dillon sizeof(struct ieee80211_meshcntl);
1453085ff963SMatthew Dillon }
1454085ff963SMatthew Dillon } else {
1455085ff963SMatthew Dillon dir = IEEE80211_FC1_DIR_FROMDS;
1456085ff963SMatthew Dillon if (!IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr)) {
1457085ff963SMatthew Dillon /* proxy group */
1458085ff963SMatthew Dillon meshae = IEEE80211_MESH_AE_01;
1459085ff963SMatthew Dillon meshhdrsize =
1460085ff963SMatthew Dillon sizeof(struct ieee80211_meshcntl_ae01);
1461085ff963SMatthew Dillon } else {
1462085ff963SMatthew Dillon /* group */
1463085ff963SMatthew Dillon meshae = IEEE80211_MESH_AE_00;
1464085ff963SMatthew Dillon meshhdrsize = sizeof(struct ieee80211_meshcntl);
1465085ff963SMatthew Dillon }
146632176cfdSRui Paulo }
146732176cfdSRui Paulo } else {
146832176cfdSRui Paulo #endif
146932176cfdSRui Paulo /*
147032176cfdSRui Paulo * 4-address frames need to be generated for:
147132176cfdSRui Paulo * o packets sent through a WDS vap (IEEE80211_M_WDS)
147232176cfdSRui Paulo * o packets sent through a vap marked for relaying
147332176cfdSRui Paulo * (e.g. a station operating with dynamic WDS)
147432176cfdSRui Paulo */
147532176cfdSRui Paulo is4addr = vap->iv_opmode == IEEE80211_M_WDS ||
147632176cfdSRui Paulo ((vap->iv_flags_ext & IEEE80211_FEXT_4ADDR) &&
147732176cfdSRui Paulo !IEEE80211_ADDR_EQ(eh.ether_shost, vap->iv_myaddr));
147832176cfdSRui Paulo if (is4addr)
147932176cfdSRui Paulo hdrsize += IEEE80211_ADDR_LEN;
148032176cfdSRui Paulo meshhdrsize = meshae = 0;
148132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
148232176cfdSRui Paulo }
148332176cfdSRui Paulo #endif
148432176cfdSRui Paulo /*
148532176cfdSRui Paulo * Honor driver DATAPAD requirement.
148632176cfdSRui Paulo */
1487841ab66cSSepherosa Ziehau if (ic->ic_flags & IEEE80211_F_DATAPAD)
148832176cfdSRui Paulo hdrspace = roundup(hdrsize, sizeof(uint32_t));
148932176cfdSRui Paulo else
149032176cfdSRui Paulo hdrspace = hdrsize;
149132176cfdSRui Paulo
149232176cfdSRui Paulo if (__predict_true((m->m_flags & M_FF) == 0)) {
149332176cfdSRui Paulo /*
149432176cfdSRui Paulo * Normal frame.
149532176cfdSRui Paulo */
149632176cfdSRui Paulo m = ieee80211_mbuf_adjust(vap, hdrspace + meshhdrsize, key, m);
1497841ab66cSSepherosa Ziehau if (m == NULL) {
1498841ab66cSSepherosa Ziehau /* NB: ieee80211_mbuf_adjust handles msgs+statistics */
1499f186073cSJoerg Sonnenberger goto bad;
1500f186073cSJoerg Sonnenberger }
150132176cfdSRui Paulo /* NB: this could be optimized 'cuz of ieee80211_mbuf_adjust */
1502f186073cSJoerg Sonnenberger m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
1503f186073cSJoerg Sonnenberger llc = mtod(m, struct llc *);
1504f186073cSJoerg Sonnenberger llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1505f186073cSJoerg Sonnenberger llc->llc_control = LLC_UI;
1506f186073cSJoerg Sonnenberger llc->llc_snap.org_code[0] = 0;
1507f186073cSJoerg Sonnenberger llc->llc_snap.org_code[1] = 0;
1508f186073cSJoerg Sonnenberger llc->llc_snap.org_code[2] = 0;
1509f186073cSJoerg Sonnenberger llc->llc_snap.ether_type = eh.ether_type;
151032176cfdSRui Paulo } else {
151132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
151232176cfdSRui Paulo /*
15134f655ef5SMatthew Dillon * Aggregated frame. Check if it's for AMSDU or FF.
15144f655ef5SMatthew Dillon *
15154f655ef5SMatthew Dillon * XXX TODO: IEEE80211_NODE_AMSDU* isn't implemented
15164f655ef5SMatthew Dillon * anywhere for some reason. But, since 11n requires
15174f655ef5SMatthew Dillon * AMSDU RX, we can just assume "11n" == "AMSDU".
151832176cfdSRui Paulo */
15194f655ef5SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG, "%s: called; M_FF\n", __func__);
15204f655ef5SMatthew Dillon if (ieee80211_amsdu_tx_ok(ni)) {
15214f655ef5SMatthew Dillon m = ieee80211_amsdu_encap(vap, m, hdrspace + meshhdrsize, key);
15224f655ef5SMatthew Dillon is_amsdu = 1;
15234f655ef5SMatthew Dillon } else {
152432176cfdSRui Paulo m = ieee80211_ff_encap(vap, m, hdrspace + meshhdrsize, key);
15254f655ef5SMatthew Dillon }
152632176cfdSRui Paulo if (m == NULL)
152732176cfdSRui Paulo #endif
152832176cfdSRui Paulo goto bad;
152932176cfdSRui Paulo }
1530841ab66cSSepherosa Ziehau datalen = m->m_pkthdr.len; /* NB: w/o 802.11 header */
1531841ab66cSSepherosa Ziehau
1532b5523eacSSascha Wildner M_PREPEND(m, hdrspace + meshhdrsize, M_NOWAIT);
1533f186073cSJoerg Sonnenberger if (m == NULL) {
153432176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
1535f186073cSJoerg Sonnenberger goto bad;
1536f186073cSJoerg Sonnenberger }
1537f186073cSJoerg Sonnenberger wh = mtod(m, struct ieee80211_frame *);
1538f186073cSJoerg Sonnenberger wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1539f186073cSJoerg Sonnenberger *(uint16_t *)wh->i_dur = 0;
154032176cfdSRui Paulo qos = NULL; /* NB: quiet compiler */
154132176cfdSRui Paulo if (is4addr) {
154232176cfdSRui Paulo wh->i_fc[1] = IEEE80211_FC1_DIR_DSTODS;
154332176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
154432176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
154532176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
154632176cfdSRui Paulo IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, eh.ether_shost);
154732176cfdSRui Paulo } else switch (vap->iv_opmode) {
1548f186073cSJoerg Sonnenberger case IEEE80211_M_STA:
1549f186073cSJoerg Sonnenberger wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1550f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
1551f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1552f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
1553f186073cSJoerg Sonnenberger break;
1554f186073cSJoerg Sonnenberger case IEEE80211_M_IBSS:
1555f186073cSJoerg Sonnenberger case IEEE80211_M_AHDEMO:
1556f186073cSJoerg Sonnenberger wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1557f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1558f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
1559841ab66cSSepherosa Ziehau /*
156032176cfdSRui Paulo * NB: always use the bssid from iv_bss as the
1561841ab66cSSepherosa Ziehau * neighbor's may be stale after an ibss merge
1562841ab66cSSepherosa Ziehau */
156332176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_bssid);
1564f186073cSJoerg Sonnenberger break;
1565f186073cSJoerg Sonnenberger case IEEE80211_M_HOSTAP:
1566f186073cSJoerg Sonnenberger wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
1567f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
1568f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
1569f186073cSJoerg Sonnenberger IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
1570f186073cSJoerg Sonnenberger break;
157132176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
157232176cfdSRui Paulo case IEEE80211_M_MBSS:
157332176cfdSRui Paulo /* NB: offset by hdrspace to deal with DATAPAD */
157432176cfdSRui Paulo mc = (struct ieee80211_meshcntl_ae10 *)
157532176cfdSRui Paulo (mtod(m, uint8_t *) + hdrspace);
1576085ff963SMatthew Dillon wh->i_fc[1] = dir;
157732176cfdSRui Paulo switch (meshae) {
1578085ff963SMatthew Dillon case IEEE80211_MESH_AE_00: /* no proxy */
157932176cfdSRui Paulo mc->mc_flags = 0;
1580085ff963SMatthew Dillon if (dir == IEEE80211_FC1_DIR_DSTODS) { /* ucast */
1581085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr1,
1582085ff963SMatthew Dillon ni->ni_macaddr);
1583085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr2,
1584085ff963SMatthew Dillon vap->iv_myaddr);
1585085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr3,
1586085ff963SMatthew Dillon eh.ether_dhost);
1587085ff963SMatthew Dillon IEEE80211_ADDR_COPY(WH4(wh)->i_addr4,
1588085ff963SMatthew Dillon eh.ether_shost);
1589085ff963SMatthew Dillon qos =((struct ieee80211_qosframe_addr4 *)
1590085ff963SMatthew Dillon wh)->i_qos;
1591085ff963SMatthew Dillon } else if (dir == IEEE80211_FC1_DIR_FROMDS) {
1592085ff963SMatthew Dillon /* mcast */
1593085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr1,
1594085ff963SMatthew Dillon eh.ether_dhost);
1595085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr2,
1596085ff963SMatthew Dillon vap->iv_myaddr);
1597085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr3,
1598085ff963SMatthew Dillon eh.ether_shost);
1599085ff963SMatthew Dillon qos = ((struct ieee80211_qosframe *)
1600085ff963SMatthew Dillon wh)->i_qos;
1601085ff963SMatthew Dillon }
160232176cfdSRui Paulo break;
1603085ff963SMatthew Dillon case IEEE80211_MESH_AE_01: /* mcast, proxy */
160432176cfdSRui Paulo wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
160532176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
160632176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
160732176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_myaddr);
160832176cfdSRui Paulo mc->mc_flags = 1;
1609085ff963SMatthew Dillon IEEE80211_ADDR_COPY(MC01(mc)->mc_addr4,
1610085ff963SMatthew Dillon eh.ether_shost);
161132176cfdSRui Paulo qos = ((struct ieee80211_qosframe *) wh)->i_qos;
161232176cfdSRui Paulo break;
1613085ff963SMatthew Dillon case IEEE80211_MESH_AE_10: /* ucast, proxy */
1614085ff963SMatthew Dillon KASSERT(rt != NULL, ("route is NULL"));
1615085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr1, rt->rt_nexthop);
161632176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
1617085ff963SMatthew Dillon IEEE80211_ADDR_COPY(wh->i_addr3, rt->rt_mesh_gate);
161832176cfdSRui Paulo IEEE80211_ADDR_COPY(WH4(wh)->i_addr4, vap->iv_myaddr);
1619085ff963SMatthew Dillon mc->mc_flags = IEEE80211_MESH_AE_10;
1620085ff963SMatthew Dillon IEEE80211_ADDR_COPY(mc->mc_addr5, eh.ether_dhost);
1621085ff963SMatthew Dillon IEEE80211_ADDR_COPY(mc->mc_addr6, eh.ether_shost);
162232176cfdSRui Paulo qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
162332176cfdSRui Paulo break;
162432176cfdSRui Paulo default:
162532176cfdSRui Paulo KASSERT(0, ("meshae %d", meshae));
162632176cfdSRui Paulo break;
162732176cfdSRui Paulo }
162832176cfdSRui Paulo mc->mc_ttl = ms->ms_ttl;
162932176cfdSRui Paulo ms->ms_seq++;
16304f655ef5SMatthew Dillon le32enc(mc->mc_seq, ms->ms_seq);
163132176cfdSRui Paulo break;
163232176cfdSRui Paulo #endif
163332176cfdSRui Paulo case IEEE80211_M_WDS: /* NB: is4addr should always be true */
163432176cfdSRui Paulo default:
1635f186073cSJoerg Sonnenberger goto bad;
1636f186073cSJoerg Sonnenberger }
1637841ab66cSSepherosa Ziehau if (m->m_flags & M_MORE_DATA)
1638841ab66cSSepherosa Ziehau wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1639841ab66cSSepherosa Ziehau if (addqos) {
1640841ab66cSSepherosa Ziehau int ac, tid;
1641841ab66cSSepherosa Ziehau
164232176cfdSRui Paulo if (is4addr) {
164332176cfdSRui Paulo qos = ((struct ieee80211_qosframe_addr4 *) wh)->i_qos;
164432176cfdSRui Paulo /* NB: mesh case handled earlier */
164532176cfdSRui Paulo } else if (vap->iv_opmode != IEEE80211_M_MBSS)
164632176cfdSRui Paulo qos = ((struct ieee80211_qosframe *) wh)->i_qos;
1647841ab66cSSepherosa Ziehau ac = M_WME_GETAC(m);
1648841ab66cSSepherosa Ziehau /* map from access class/queue to 11e header priorty value */
1649841ab66cSSepherosa Ziehau tid = WME_AC_TO_TID(ac);
165032176cfdSRui Paulo qos[0] = tid & IEEE80211_QOS_TID;
1651841ab66cSSepherosa Ziehau if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy)
165232176cfdSRui Paulo qos[0] |= IEEE80211_QOS_ACKPOLICY_NOACK;
1653085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
1654085ff963SMatthew Dillon if (vap->iv_opmode == IEEE80211_M_MBSS)
1655085ff963SMatthew Dillon qos[1] = IEEE80211_QOS_MC;
1656085ff963SMatthew Dillon else
1657085ff963SMatthew Dillon #endif
165832176cfdSRui Paulo qos[1] = 0;
165932176cfdSRui Paulo wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
1660841ab66cSSepherosa Ziehau
16614f655ef5SMatthew Dillon /*
16624f655ef5SMatthew Dillon * If this is an A-MSDU then ensure we set the
16634f655ef5SMatthew Dillon * relevant field.
16644f655ef5SMatthew Dillon */
16654f655ef5SMatthew Dillon if (is_amsdu)
16664f655ef5SMatthew Dillon qos[0] |= IEEE80211_QOS_AMSDU;
16674f655ef5SMatthew Dillon
166832176cfdSRui Paulo if ((m->m_flags & M_AMPDU_MPDU) == 0) {
166932176cfdSRui Paulo /*
167032176cfdSRui Paulo * NB: don't assign a sequence # to potential
167132176cfdSRui Paulo * aggregates; we expect this happens at the
167232176cfdSRui Paulo * point the frame comes off any aggregation q
167332176cfdSRui Paulo * as otherwise we may introduce holes in the
167432176cfdSRui Paulo * BA sequence space and/or make window accouting
167532176cfdSRui Paulo * more difficult.
167632176cfdSRui Paulo *
167732176cfdSRui Paulo * XXX may want to control this with a driver
167832176cfdSRui Paulo * capability; this may also change when we pull
167932176cfdSRui Paulo * aggregation up into net80211
168032176cfdSRui Paulo */
168132176cfdSRui Paulo seqno = ni->ni_txseqs[tid]++;
1682841ab66cSSepherosa Ziehau *(uint16_t *)wh->i_seq =
168332176cfdSRui Paulo htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
168432176cfdSRui Paulo M_SEQNO_SET(m, seqno);
1685841ab66cSSepherosa Ziehau }
168632176cfdSRui Paulo } else {
168732176cfdSRui Paulo seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
168832176cfdSRui Paulo *(uint16_t *)wh->i_seq =
168932176cfdSRui Paulo htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
169032176cfdSRui Paulo M_SEQNO_SET(m, seqno);
16914f655ef5SMatthew Dillon
16924f655ef5SMatthew Dillon /*
16934f655ef5SMatthew Dillon * XXX TODO: we shouldn't allow EAPOL, etc that would
16944f655ef5SMatthew Dillon * be forced to be non-QoS traffic to be A-MSDU encapsulated.
16954f655ef5SMatthew Dillon */
16964f655ef5SMatthew Dillon if (is_amsdu)
16974f655ef5SMatthew Dillon kprintf("%s: XXX ERROR: is_amsdu set; not QoS!\n",
16984f655ef5SMatthew Dillon __func__);
169932176cfdSRui Paulo }
170032176cfdSRui Paulo
170132176cfdSRui Paulo
170232176cfdSRui Paulo /* check if xmit fragmentation is required */
170332176cfdSRui Paulo txfrag = (m->m_pkthdr.len > vap->iv_fragthreshold &&
170432176cfdSRui Paulo !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
170532176cfdSRui Paulo (vap->iv_caps & IEEE80211_C_TXFRAG) &&
170632176cfdSRui Paulo (m->m_flags & (M_FF | M_AMPDU_MPDU)) == 0);
1707841ab66cSSepherosa Ziehau if (key != NULL) {
1708841ab66cSSepherosa Ziehau /*
1709841ab66cSSepherosa Ziehau * IEEE 802.1X: send EAPOL frames always in the clear.
1710841ab66cSSepherosa Ziehau * WPA/WPA2: encrypt EAPOL keys when pairwise keys are set.
1711841ab66cSSepherosa Ziehau */
171232176cfdSRui Paulo if ((m->m_flags & M_EAPOL) == 0 ||
171332176cfdSRui Paulo ((vap->iv_flags & IEEE80211_F_WPA) &&
171432176cfdSRui Paulo (vap->iv_opmode == IEEE80211_M_STA ?
171532176cfdSRui Paulo !IEEE80211_KEY_UNDEFINED(key) :
171632176cfdSRui Paulo !IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)))) {
1717085ff963SMatthew Dillon wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
171832176cfdSRui Paulo if (!ieee80211_crypto_enmic(vap, key, m, txfrag)) {
171932176cfdSRui Paulo IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT,
172032176cfdSRui Paulo eh.ether_dhost,
172132176cfdSRui Paulo "%s", "enmic failed, discard frame");
172232176cfdSRui Paulo vap->iv_stats.is_crypto_enmicfail++;
1723841ab66cSSepherosa Ziehau goto bad;
1724841ab66cSSepherosa Ziehau }
1725841ab66cSSepherosa Ziehau }
1726841ab66cSSepherosa Ziehau }
172732176cfdSRui Paulo if (txfrag && !ieee80211_fragment(vap, m, hdrsize,
172832176cfdSRui Paulo key != NULL ? key->wk_cipher->ic_header : 0, vap->iv_fragthreshold))
172932176cfdSRui Paulo goto bad;
173032176cfdSRui Paulo
173132176cfdSRui Paulo m->m_flags |= M_ENCAP; /* mark encapsulated */
1732841ab66cSSepherosa Ziehau
1733841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_data);
173432176cfdSRui Paulo if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1735fb134238SSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_mcast);
173632176cfdSRui Paulo m->m_flags |= M_MCAST;
173732176cfdSRui Paulo } else
1738fb134238SSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_ucast);
1739841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT_ADD(ni, tx_bytes, datalen);
1740841ab66cSSepherosa Ziehau
1741f186073cSJoerg Sonnenberger return m;
1742f186073cSJoerg Sonnenberger bad:
1743f186073cSJoerg Sonnenberger if (m != NULL)
1744f186073cSJoerg Sonnenberger m_freem(m);
1745f186073cSJoerg Sonnenberger return NULL;
174632176cfdSRui Paulo #undef WH4
1747085ff963SMatthew Dillon #undef MC01
174832176cfdSRui Paulo }
174932176cfdSRui Paulo
17504f655ef5SMatthew Dillon void
17514f655ef5SMatthew Dillon ieee80211_free_mbuf(struct mbuf *m)
17524f655ef5SMatthew Dillon {
17534f655ef5SMatthew Dillon struct mbuf *next;
17544f655ef5SMatthew Dillon
17554f655ef5SMatthew Dillon if (m == NULL)
17564f655ef5SMatthew Dillon return;
17574f655ef5SMatthew Dillon
17584f655ef5SMatthew Dillon do {
17594f655ef5SMatthew Dillon next = m->m_nextpkt;
17604f655ef5SMatthew Dillon m->m_nextpkt = NULL;
17614f655ef5SMatthew Dillon m_freem(m);
17624f655ef5SMatthew Dillon } while ((m = next) != NULL);
17634f655ef5SMatthew Dillon }
17644f655ef5SMatthew Dillon
176532176cfdSRui Paulo /*
176632176cfdSRui Paulo * Fragment the frame according to the specified mtu.
176732176cfdSRui Paulo * The size of the 802.11 header (w/o padding) is provided
176832176cfdSRui Paulo * so we don't need to recalculate it. We create a new
176932176cfdSRui Paulo * mbuf for each fragment and chain it through m_nextpkt;
177032176cfdSRui Paulo * we might be able to optimize this by reusing the original
177132176cfdSRui Paulo * packet's mbufs but that is significantly more complicated.
177232176cfdSRui Paulo */
177332176cfdSRui Paulo static int
177432176cfdSRui Paulo ieee80211_fragment(struct ieee80211vap *vap, struct mbuf *m0,
177532176cfdSRui Paulo u_int hdrsize, u_int ciphdrsize, u_int mtu)
177632176cfdSRui Paulo {
1777085ff963SMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
177832176cfdSRui Paulo struct ieee80211_frame *wh, *whf;
17794f655ef5SMatthew Dillon struct mbuf *m, *prev;
178032176cfdSRui Paulo u_int totalhdrsize, fragno, fragsize, off, remainder, payload;
1781085ff963SMatthew Dillon u_int hdrspace;
178232176cfdSRui Paulo
178332176cfdSRui Paulo KASSERT(m0->m_nextpkt == NULL, ("mbuf already chained?"));
178432176cfdSRui Paulo KASSERT(m0->m_pkthdr.len > mtu,
178532176cfdSRui Paulo ("pktlen %u mtu %u", m0->m_pkthdr.len, mtu));
178632176cfdSRui Paulo
1787085ff963SMatthew Dillon /*
1788085ff963SMatthew Dillon * Honor driver DATAPAD requirement.
1789085ff963SMatthew Dillon */
1790085ff963SMatthew Dillon if (ic->ic_flags & IEEE80211_F_DATAPAD)
1791085ff963SMatthew Dillon hdrspace = roundup(hdrsize, sizeof(uint32_t));
1792085ff963SMatthew Dillon else
1793085ff963SMatthew Dillon hdrspace = hdrsize;
1794085ff963SMatthew Dillon
179532176cfdSRui Paulo wh = mtod(m0, struct ieee80211_frame *);
179632176cfdSRui Paulo /* NB: mark the first frag; it will be propagated below */
179732176cfdSRui Paulo wh->i_fc[1] |= IEEE80211_FC1_MORE_FRAG;
1798085ff963SMatthew Dillon totalhdrsize = hdrspace + ciphdrsize;
179932176cfdSRui Paulo fragno = 1;
180032176cfdSRui Paulo off = mtu - ciphdrsize;
180132176cfdSRui Paulo remainder = m0->m_pkthdr.len - off;
180232176cfdSRui Paulo prev = m0;
180332176cfdSRui Paulo do {
180432176cfdSRui Paulo fragsize = totalhdrsize + remainder;
180532176cfdSRui Paulo if (fragsize > mtu)
180632176cfdSRui Paulo fragsize = mtu;
180732176cfdSRui Paulo /* XXX fragsize can be >2048! */
180832176cfdSRui Paulo KASSERT(fragsize < MCLBYTES,
180932176cfdSRui Paulo ("fragment size %u too big!", fragsize));
181032176cfdSRui Paulo if (fragsize > MHLEN)
1811b5523eacSSascha Wildner m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
181232176cfdSRui Paulo else
1813b5523eacSSascha Wildner m = m_gethdr(M_NOWAIT, MT_DATA);
181432176cfdSRui Paulo if (m == NULL)
181532176cfdSRui Paulo goto bad;
181632176cfdSRui Paulo /* leave room to prepend any cipher header */
181732176cfdSRui Paulo m_align(m, fragsize - ciphdrsize);
181832176cfdSRui Paulo
181932176cfdSRui Paulo /*
182032176cfdSRui Paulo * Form the header in the fragment. Note that since
182132176cfdSRui Paulo * we mark the first fragment with the MORE_FRAG bit
182232176cfdSRui Paulo * it automatically is propagated to each fragment; we
182332176cfdSRui Paulo * need only clear it on the last fragment (done below).
1824085ff963SMatthew Dillon * NB: frag 1+ dont have Mesh Control field present.
182532176cfdSRui Paulo */
182632176cfdSRui Paulo whf = mtod(m, struct ieee80211_frame *);
182732176cfdSRui Paulo memcpy(whf, wh, hdrsize);
1828085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
1829085ff963SMatthew Dillon if (vap->iv_opmode == IEEE80211_M_MBSS) {
1830085ff963SMatthew Dillon if (IEEE80211_IS_DSTODS(wh))
1831085ff963SMatthew Dillon ((struct ieee80211_qosframe_addr4 *)
1832085ff963SMatthew Dillon whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1833085ff963SMatthew Dillon else
1834085ff963SMatthew Dillon ((struct ieee80211_qosframe *)
1835085ff963SMatthew Dillon whf)->i_qos[1] &= ~IEEE80211_QOS_MC;
1836085ff963SMatthew Dillon }
1837085ff963SMatthew Dillon #endif
183832176cfdSRui Paulo *(uint16_t *)&whf->i_seq[0] |= htole16(
183932176cfdSRui Paulo (fragno & IEEE80211_SEQ_FRAG_MASK) <<
184032176cfdSRui Paulo IEEE80211_SEQ_FRAG_SHIFT);
184132176cfdSRui Paulo fragno++;
184232176cfdSRui Paulo
184332176cfdSRui Paulo payload = fragsize - totalhdrsize;
184432176cfdSRui Paulo /* NB: destination is known to be contiguous */
1845085ff963SMatthew Dillon
1846085ff963SMatthew Dillon m_copydata(m0, off, payload, mtod(m, uint8_t *) + hdrspace);
1847085ff963SMatthew Dillon m->m_len = hdrspace + payload;
1848085ff963SMatthew Dillon m->m_pkthdr.len = hdrspace + payload;
184932176cfdSRui Paulo m->m_flags |= M_FRAG;
185032176cfdSRui Paulo
185132176cfdSRui Paulo /* chain up the fragment */
185232176cfdSRui Paulo prev->m_nextpkt = m;
185332176cfdSRui Paulo prev = m;
185432176cfdSRui Paulo
185532176cfdSRui Paulo /* deduct fragment just formed */
185632176cfdSRui Paulo remainder -= payload;
185732176cfdSRui Paulo off += payload;
185832176cfdSRui Paulo } while (remainder != 0);
185932176cfdSRui Paulo
186032176cfdSRui Paulo /* set the last fragment */
186132176cfdSRui Paulo m->m_flags |= M_LASTFRAG;
186232176cfdSRui Paulo whf->i_fc[1] &= ~IEEE80211_FC1_MORE_FRAG;
186332176cfdSRui Paulo
186432176cfdSRui Paulo /* strip first mbuf now that everything has been copied */
186532176cfdSRui Paulo m_adj(m0, -(m0->m_pkthdr.len - (mtu - ciphdrsize)));
186632176cfdSRui Paulo m0->m_flags |= M_FIRSTFRAG | M_FRAG;
186732176cfdSRui Paulo
186832176cfdSRui Paulo vap->iv_stats.is_tx_fragframes++;
186932176cfdSRui Paulo vap->iv_stats.is_tx_frags += fragno-1;
187032176cfdSRui Paulo
187132176cfdSRui Paulo return 1;
187232176cfdSRui Paulo bad:
187332176cfdSRui Paulo /* reclaim fragments but leave original frame for caller to free */
18744f655ef5SMatthew Dillon ieee80211_free_mbuf(m0->m_nextpkt);
187532176cfdSRui Paulo m0->m_nextpkt = NULL;
187632176cfdSRui Paulo return 0;
1877f186073cSJoerg Sonnenberger }
1878f186073cSJoerg Sonnenberger
1879f186073cSJoerg Sonnenberger /*
1880f186073cSJoerg Sonnenberger * Add a supported rates element id to a frame.
1881f186073cSJoerg Sonnenberger */
1882f186073cSJoerg Sonnenberger uint8_t *
1883f186073cSJoerg Sonnenberger ieee80211_add_rates(uint8_t *frm, const struct ieee80211_rateset *rs)
1884f186073cSJoerg Sonnenberger {
1885f186073cSJoerg Sonnenberger int nrates;
1886f186073cSJoerg Sonnenberger
1887f186073cSJoerg Sonnenberger *frm++ = IEEE80211_ELEMID_RATES;
1888f186073cSJoerg Sonnenberger nrates = rs->rs_nrates;
1889f186073cSJoerg Sonnenberger if (nrates > IEEE80211_RATE_SIZE)
1890f186073cSJoerg Sonnenberger nrates = IEEE80211_RATE_SIZE;
1891f186073cSJoerg Sonnenberger *frm++ = nrates;
1892f186073cSJoerg Sonnenberger memcpy(frm, rs->rs_rates, nrates);
1893f186073cSJoerg Sonnenberger return frm + nrates;
1894f186073cSJoerg Sonnenberger }
1895f186073cSJoerg Sonnenberger
1896f186073cSJoerg Sonnenberger /*
1897f186073cSJoerg Sonnenberger * Add an extended supported rates element id to a frame.
1898f186073cSJoerg Sonnenberger */
1899f186073cSJoerg Sonnenberger uint8_t *
1900f186073cSJoerg Sonnenberger ieee80211_add_xrates(uint8_t *frm, const struct ieee80211_rateset *rs)
1901f186073cSJoerg Sonnenberger {
1902f186073cSJoerg Sonnenberger /*
1903f186073cSJoerg Sonnenberger * Add an extended supported rates element if operating in 11g mode.
1904f186073cSJoerg Sonnenberger */
1905f186073cSJoerg Sonnenberger if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
1906f186073cSJoerg Sonnenberger int nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
1907f186073cSJoerg Sonnenberger *frm++ = IEEE80211_ELEMID_XRATES;
1908f186073cSJoerg Sonnenberger *frm++ = nrates;
1909f186073cSJoerg Sonnenberger memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
1910f186073cSJoerg Sonnenberger frm += nrates;
1911f186073cSJoerg Sonnenberger }
1912f186073cSJoerg Sonnenberger return frm;
1913f186073cSJoerg Sonnenberger }
1914f186073cSJoerg Sonnenberger
1915f186073cSJoerg Sonnenberger /*
191632176cfdSRui Paulo * Add an ssid element to a frame.
1917f186073cSJoerg Sonnenberger */
1918e218318cSMatthew Dillon uint8_t *
1919f186073cSJoerg Sonnenberger ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len)
1920f186073cSJoerg Sonnenberger {
1921f186073cSJoerg Sonnenberger *frm++ = IEEE80211_ELEMID_SSID;
1922f186073cSJoerg Sonnenberger *frm++ = len;
1923f186073cSJoerg Sonnenberger memcpy(frm, ssid, len);
1924f186073cSJoerg Sonnenberger return frm + len;
1925f186073cSJoerg Sonnenberger }
1926f186073cSJoerg Sonnenberger
1927841ab66cSSepherosa Ziehau /*
1928841ab66cSSepherosa Ziehau * Add an erp element to a frame.
1929841ab66cSSepherosa Ziehau */
1930841ab66cSSepherosa Ziehau static uint8_t *
1931841ab66cSSepherosa Ziehau ieee80211_add_erp(uint8_t *frm, struct ieee80211com *ic)
1932f186073cSJoerg Sonnenberger {
1933841ab66cSSepherosa Ziehau uint8_t erp;
1934f186073cSJoerg Sonnenberger
1935841ab66cSSepherosa Ziehau *frm++ = IEEE80211_ELEMID_ERP;
1936841ab66cSSepherosa Ziehau *frm++ = 1;
1937841ab66cSSepherosa Ziehau erp = 0;
1938841ab66cSSepherosa Ziehau if (ic->ic_nonerpsta != 0)
1939841ab66cSSepherosa Ziehau erp |= IEEE80211_ERP_NON_ERP_PRESENT;
1940841ab66cSSepherosa Ziehau if (ic->ic_flags & IEEE80211_F_USEPROT)
1941841ab66cSSepherosa Ziehau erp |= IEEE80211_ERP_USE_PROTECTION;
1942841ab66cSSepherosa Ziehau if (ic->ic_flags & IEEE80211_F_USEBARKER)
1943841ab66cSSepherosa Ziehau erp |= IEEE80211_ERP_LONG_PREAMBLE;
1944841ab66cSSepherosa Ziehau *frm++ = erp;
1945841ab66cSSepherosa Ziehau return frm;
1946841ab66cSSepherosa Ziehau }
1947841ab66cSSepherosa Ziehau
1948841ab66cSSepherosa Ziehau /*
194932176cfdSRui Paulo * Add a CFParams element to a frame.
1950841ab66cSSepherosa Ziehau */
1951841ab66cSSepherosa Ziehau static uint8_t *
195232176cfdSRui Paulo ieee80211_add_cfparms(uint8_t *frm, struct ieee80211com *ic)
1953841ab66cSSepherosa Ziehau {
195432176cfdSRui Paulo #define ADDSHORT(frm, v) do { \
19554f655ef5SMatthew Dillon le16enc(frm, v); \
195632176cfdSRui Paulo frm += 2; \
195732176cfdSRui Paulo } while (0)
195832176cfdSRui Paulo *frm++ = IEEE80211_ELEMID_CFPARMS;
195932176cfdSRui Paulo *frm++ = 6;
196032176cfdSRui Paulo *frm++ = 0; /* CFP count */
196132176cfdSRui Paulo *frm++ = 2; /* CFP period */
196232176cfdSRui Paulo ADDSHORT(frm, 0); /* CFP MaxDuration (TU) */
196332176cfdSRui Paulo ADDSHORT(frm, 0); /* CFP CurRemaining (TU) */
1964841ab66cSSepherosa Ziehau return frm;
196532176cfdSRui Paulo #undef ADDSHORT
196632176cfdSRui Paulo }
196732176cfdSRui Paulo
196832176cfdSRui Paulo static __inline uint8_t *
196932176cfdSRui Paulo add_appie(uint8_t *frm, const struct ieee80211_appie *ie)
197032176cfdSRui Paulo {
197132176cfdSRui Paulo memcpy(frm, ie->ie_data, ie->ie_len);
197232176cfdSRui Paulo return frm + ie->ie_len;
197332176cfdSRui Paulo }
197432176cfdSRui Paulo
197532176cfdSRui Paulo static __inline uint8_t *
197632176cfdSRui Paulo add_ie(uint8_t *frm, const uint8_t *ie)
197732176cfdSRui Paulo {
197832176cfdSRui Paulo memcpy(frm, ie, 2 + ie[1]);
197932176cfdSRui Paulo return frm + 2 + ie[1];
1980841ab66cSSepherosa Ziehau }
1981841ab66cSSepherosa Ziehau
1982841ab66cSSepherosa Ziehau #define WME_OUI_BYTES 0x00, 0x50, 0xf2
1983841ab66cSSepherosa Ziehau /*
1984841ab66cSSepherosa Ziehau * Add a WME information element to a frame.
1985841ab66cSSepherosa Ziehau */
19864f655ef5SMatthew Dillon uint8_t *
1987841ab66cSSepherosa Ziehau ieee80211_add_wme_info(uint8_t *frm, struct ieee80211_wme_state *wme)
1988841ab66cSSepherosa Ziehau {
1989841ab66cSSepherosa Ziehau static const struct ieee80211_wme_info info = {
1990841ab66cSSepherosa Ziehau .wme_id = IEEE80211_ELEMID_VENDOR,
1991841ab66cSSepherosa Ziehau .wme_len = sizeof(struct ieee80211_wme_info) - 2,
1992841ab66cSSepherosa Ziehau .wme_oui = { WME_OUI_BYTES },
1993841ab66cSSepherosa Ziehau .wme_type = WME_OUI_TYPE,
1994841ab66cSSepherosa Ziehau .wme_subtype = WME_INFO_OUI_SUBTYPE,
1995841ab66cSSepherosa Ziehau .wme_version = WME_VERSION,
1996841ab66cSSepherosa Ziehau .wme_info = 0,
1997841ab66cSSepherosa Ziehau };
1998841ab66cSSepherosa Ziehau memcpy(frm, &info, sizeof(info));
1999841ab66cSSepherosa Ziehau return frm + sizeof(info);
2000841ab66cSSepherosa Ziehau }
2001841ab66cSSepherosa Ziehau
2002841ab66cSSepherosa Ziehau /*
2003841ab66cSSepherosa Ziehau * Add a WME parameters element to a frame.
2004841ab66cSSepherosa Ziehau */
2005841ab66cSSepherosa Ziehau static uint8_t *
2006841ab66cSSepherosa Ziehau ieee80211_add_wme_param(uint8_t *frm, struct ieee80211_wme_state *wme)
2007841ab66cSSepherosa Ziehau {
2008841ab66cSSepherosa Ziehau #define SM(_v, _f) (((_v) << _f##_S) & _f)
2009841ab66cSSepherosa Ziehau #define ADDSHORT(frm, v) do { \
20104f655ef5SMatthew Dillon le16enc(frm, v); \
2011841ab66cSSepherosa Ziehau frm += 2; \
2012841ab66cSSepherosa Ziehau } while (0)
2013841ab66cSSepherosa Ziehau /* NB: this works 'cuz a param has an info at the front */
2014841ab66cSSepherosa Ziehau static const struct ieee80211_wme_info param = {
2015841ab66cSSepherosa Ziehau .wme_id = IEEE80211_ELEMID_VENDOR,
2016841ab66cSSepherosa Ziehau .wme_len = sizeof(struct ieee80211_wme_param) - 2,
2017841ab66cSSepherosa Ziehau .wme_oui = { WME_OUI_BYTES },
2018841ab66cSSepherosa Ziehau .wme_type = WME_OUI_TYPE,
2019841ab66cSSepherosa Ziehau .wme_subtype = WME_PARAM_OUI_SUBTYPE,
2020841ab66cSSepherosa Ziehau .wme_version = WME_VERSION,
2021841ab66cSSepherosa Ziehau };
2022841ab66cSSepherosa Ziehau int i;
2023841ab66cSSepherosa Ziehau
2024841ab66cSSepherosa Ziehau memcpy(frm, ¶m, sizeof(param));
2025841ab66cSSepherosa Ziehau frm += __offsetof(struct ieee80211_wme_info, wme_info);
2026841ab66cSSepherosa Ziehau *frm++ = wme->wme_bssChanParams.cap_info; /* AC info */
2027841ab66cSSepherosa Ziehau *frm++ = 0; /* reserved field */
2028841ab66cSSepherosa Ziehau for (i = 0; i < WME_NUM_AC; i++) {
2029841ab66cSSepherosa Ziehau const struct wmeParams *ac =
2030841ab66cSSepherosa Ziehau &wme->wme_bssChanParams.cap_wmeParams[i];
2031841ab66cSSepherosa Ziehau *frm++ = SM(i, WME_PARAM_ACI)
2032841ab66cSSepherosa Ziehau | SM(ac->wmep_acm, WME_PARAM_ACM)
2033841ab66cSSepherosa Ziehau | SM(ac->wmep_aifsn, WME_PARAM_AIFSN)
2034841ab66cSSepherosa Ziehau ;
2035841ab66cSSepherosa Ziehau *frm++ = SM(ac->wmep_logcwmax, WME_PARAM_LOGCWMAX)
2036841ab66cSSepherosa Ziehau | SM(ac->wmep_logcwmin, WME_PARAM_LOGCWMIN)
2037841ab66cSSepherosa Ziehau ;
2038841ab66cSSepherosa Ziehau ADDSHORT(frm, ac->wmep_txopLimit);
2039841ab66cSSepherosa Ziehau }
2040841ab66cSSepherosa Ziehau return frm;
2041841ab66cSSepherosa Ziehau #undef SM
2042841ab66cSSepherosa Ziehau #undef ADDSHORT
2043841ab66cSSepherosa Ziehau }
2044841ab66cSSepherosa Ziehau #undef WME_OUI_BYTES
2045841ab66cSSepherosa Ziehau
2046841ab66cSSepherosa Ziehau /*
204732176cfdSRui Paulo * Add an 11h Power Constraint element to a frame.
204832176cfdSRui Paulo */
204932176cfdSRui Paulo static uint8_t *
205032176cfdSRui Paulo ieee80211_add_powerconstraint(uint8_t *frm, struct ieee80211vap *vap)
205132176cfdSRui Paulo {
205232176cfdSRui Paulo const struct ieee80211_channel *c = vap->iv_bss->ni_chan;
205332176cfdSRui Paulo /* XXX per-vap tx power limit? */
205432176cfdSRui Paulo int8_t limit = vap->iv_ic->ic_txpowlimit / 2;
205532176cfdSRui Paulo
205632176cfdSRui Paulo frm[0] = IEEE80211_ELEMID_PWRCNSTR;
205732176cfdSRui Paulo frm[1] = 1;
205832176cfdSRui Paulo frm[2] = c->ic_maxregpower > limit ? c->ic_maxregpower - limit : 0;
205932176cfdSRui Paulo return frm + 3;
206032176cfdSRui Paulo }
206132176cfdSRui Paulo
206232176cfdSRui Paulo /*
206332176cfdSRui Paulo * Add an 11h Power Capability element to a frame.
206432176cfdSRui Paulo */
206532176cfdSRui Paulo static uint8_t *
206632176cfdSRui Paulo ieee80211_add_powercapability(uint8_t *frm, const struct ieee80211_channel *c)
206732176cfdSRui Paulo {
206832176cfdSRui Paulo frm[0] = IEEE80211_ELEMID_PWRCAP;
206932176cfdSRui Paulo frm[1] = 2;
207032176cfdSRui Paulo frm[2] = c->ic_minpower;
207132176cfdSRui Paulo frm[3] = c->ic_maxpower;
207232176cfdSRui Paulo return frm + 4;
207332176cfdSRui Paulo }
207432176cfdSRui Paulo
207532176cfdSRui Paulo /*
207632176cfdSRui Paulo * Add an 11h Supported Channels element to a frame.
207732176cfdSRui Paulo */
207832176cfdSRui Paulo static uint8_t *
207932176cfdSRui Paulo ieee80211_add_supportedchannels(uint8_t *frm, struct ieee80211com *ic)
208032176cfdSRui Paulo {
208132176cfdSRui Paulo static const int ielen = 26;
208232176cfdSRui Paulo
208332176cfdSRui Paulo frm[0] = IEEE80211_ELEMID_SUPPCHAN;
208432176cfdSRui Paulo frm[1] = ielen;
208532176cfdSRui Paulo /* XXX not correct */
208632176cfdSRui Paulo memcpy(frm+2, ic->ic_chan_avail, ielen);
208732176cfdSRui Paulo return frm + 2 + ielen;
208832176cfdSRui Paulo }
208932176cfdSRui Paulo
209032176cfdSRui Paulo /*
2091085ff963SMatthew Dillon * Add an 11h Quiet time element to a frame.
2092085ff963SMatthew Dillon */
2093085ff963SMatthew Dillon static uint8_t *
2094085ff963SMatthew Dillon ieee80211_add_quiet(uint8_t *frm, struct ieee80211vap *vap)
2095085ff963SMatthew Dillon {
2096085ff963SMatthew Dillon struct ieee80211_quiet_ie *quiet = (struct ieee80211_quiet_ie *) frm;
2097085ff963SMatthew Dillon
2098085ff963SMatthew Dillon quiet->quiet_ie = IEEE80211_ELEMID_QUIET;
2099085ff963SMatthew Dillon quiet->len = 6;
2100085ff963SMatthew Dillon if (vap->iv_quiet_count_value == 1)
2101085ff963SMatthew Dillon vap->iv_quiet_count_value = vap->iv_quiet_count;
2102085ff963SMatthew Dillon else if (vap->iv_quiet_count_value > 1)
2103085ff963SMatthew Dillon vap->iv_quiet_count_value--;
2104085ff963SMatthew Dillon
2105085ff963SMatthew Dillon if (vap->iv_quiet_count_value == 0) {
2106085ff963SMatthew Dillon /* value 0 is reserved as per 802.11h standerd */
2107085ff963SMatthew Dillon vap->iv_quiet_count_value = 1;
2108085ff963SMatthew Dillon }
2109085ff963SMatthew Dillon
2110085ff963SMatthew Dillon quiet->tbttcount = vap->iv_quiet_count_value;
2111085ff963SMatthew Dillon quiet->period = vap->iv_quiet_period;
2112085ff963SMatthew Dillon quiet->duration = htole16(vap->iv_quiet_duration);
2113085ff963SMatthew Dillon quiet->offset = htole16(vap->iv_quiet_offset);
2114085ff963SMatthew Dillon return frm + sizeof(*quiet);
2115085ff963SMatthew Dillon }
2116085ff963SMatthew Dillon
2117085ff963SMatthew Dillon /*
211832176cfdSRui Paulo * Add an 11h Channel Switch Announcement element to a frame.
211932176cfdSRui Paulo * Note that we use the per-vap CSA count to adjust the global
212032176cfdSRui Paulo * counter so we can use this routine to form probe response
212132176cfdSRui Paulo * frames and get the current count.
212232176cfdSRui Paulo */
212332176cfdSRui Paulo static uint8_t *
212432176cfdSRui Paulo ieee80211_add_csa(uint8_t *frm, struct ieee80211vap *vap)
212532176cfdSRui Paulo {
212632176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic;
212732176cfdSRui Paulo struct ieee80211_csa_ie *csa = (struct ieee80211_csa_ie *) frm;
212832176cfdSRui Paulo
212932176cfdSRui Paulo csa->csa_ie = IEEE80211_ELEMID_CSA;
213032176cfdSRui Paulo csa->csa_len = 3;
213132176cfdSRui Paulo csa->csa_mode = 1; /* XXX force quiet on channel */
213232176cfdSRui Paulo csa->csa_newchan = ieee80211_chan2ieee(ic, ic->ic_csa_newchan);
213332176cfdSRui Paulo csa->csa_count = ic->ic_csa_count - vap->iv_csa_count;
213432176cfdSRui Paulo return frm + sizeof(*csa);
213532176cfdSRui Paulo }
213632176cfdSRui Paulo
213732176cfdSRui Paulo /*
213832176cfdSRui Paulo * Add an 11h country information element to a frame.
213932176cfdSRui Paulo */
214032176cfdSRui Paulo static uint8_t *
214132176cfdSRui Paulo ieee80211_add_countryie(uint8_t *frm, struct ieee80211com *ic)
214232176cfdSRui Paulo {
214332176cfdSRui Paulo
214432176cfdSRui Paulo if (ic->ic_countryie == NULL ||
214532176cfdSRui Paulo ic->ic_countryie_chan != ic->ic_bsschan) {
214632176cfdSRui Paulo /*
214732176cfdSRui Paulo * Handle lazy construction of ie. This is done on
214832176cfdSRui Paulo * first use and after a channel change that requires
214932176cfdSRui Paulo * re-calculation.
215032176cfdSRui Paulo */
215132176cfdSRui Paulo if (ic->ic_countryie != NULL)
21524f655ef5SMatthew Dillon IEEE80211_FREE(ic->ic_countryie, M_80211_NODE_IE);
215332176cfdSRui Paulo ic->ic_countryie = ieee80211_alloc_countryie(ic);
215432176cfdSRui Paulo if (ic->ic_countryie == NULL)
215532176cfdSRui Paulo return frm;
215632176cfdSRui Paulo ic->ic_countryie_chan = ic->ic_bsschan;
215732176cfdSRui Paulo }
215832176cfdSRui Paulo return add_appie(frm, ic->ic_countryie);
215932176cfdSRui Paulo }
216032176cfdSRui Paulo
2161085ff963SMatthew Dillon uint8_t *
2162085ff963SMatthew Dillon ieee80211_add_wpa(uint8_t *frm, const struct ieee80211vap *vap)
2163085ff963SMatthew Dillon {
2164085ff963SMatthew Dillon if (vap->iv_flags & IEEE80211_F_WPA1 && vap->iv_wpa_ie != NULL)
2165085ff963SMatthew Dillon return (add_ie(frm, vap->iv_wpa_ie));
2166085ff963SMatthew Dillon else {
2167085ff963SMatthew Dillon /* XXX else complain? */
2168085ff963SMatthew Dillon return (frm);
2169085ff963SMatthew Dillon }
2170085ff963SMatthew Dillon }
2171085ff963SMatthew Dillon
2172085ff963SMatthew Dillon uint8_t *
2173085ff963SMatthew Dillon ieee80211_add_rsn(uint8_t *frm, const struct ieee80211vap *vap)
2174085ff963SMatthew Dillon {
2175085ff963SMatthew Dillon if (vap->iv_flags & IEEE80211_F_WPA2 && vap->iv_rsn_ie != NULL)
2176085ff963SMatthew Dillon return (add_ie(frm, vap->iv_rsn_ie));
2177085ff963SMatthew Dillon else {
2178085ff963SMatthew Dillon /* XXX else complain? */
2179085ff963SMatthew Dillon return (frm);
2180085ff963SMatthew Dillon }
2181085ff963SMatthew Dillon }
2182085ff963SMatthew Dillon
2183085ff963SMatthew Dillon uint8_t *
2184085ff963SMatthew Dillon ieee80211_add_qos(uint8_t *frm, const struct ieee80211_node *ni)
2185085ff963SMatthew Dillon {
2186085ff963SMatthew Dillon if (ni->ni_flags & IEEE80211_NODE_QOS) {
2187085ff963SMatthew Dillon *frm++ = IEEE80211_ELEMID_QOS;
2188085ff963SMatthew Dillon *frm++ = 1;
2189085ff963SMatthew Dillon *frm++ = 0;
2190085ff963SMatthew Dillon }
2191085ff963SMatthew Dillon
2192085ff963SMatthew Dillon return (frm);
2193085ff963SMatthew Dillon }
2194085ff963SMatthew Dillon
219532176cfdSRui Paulo /*
2196841ab66cSSepherosa Ziehau * Send a probe request frame with the specified ssid
2197841ab66cSSepherosa Ziehau * and any optional information element data.
2198841ab66cSSepherosa Ziehau */
2199841ab66cSSepherosa Ziehau int
2200841ab66cSSepherosa Ziehau ieee80211_send_probereq(struct ieee80211_node *ni,
2201841ab66cSSepherosa Ziehau const uint8_t sa[IEEE80211_ADDR_LEN],
2202841ab66cSSepherosa Ziehau const uint8_t da[IEEE80211_ADDR_LEN],
2203841ab66cSSepherosa Ziehau const uint8_t bssid[IEEE80211_ADDR_LEN],
220432176cfdSRui Paulo const uint8_t *ssid, size_t ssidlen)
2205841ab66cSSepherosa Ziehau {
220632176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
2207841ab66cSSepherosa Ziehau struct ieee80211com *ic = ni->ni_ic;
220832176cfdSRui Paulo const struct ieee80211_txparam *tp;
220932176cfdSRui Paulo struct ieee80211_bpf_params params;
2210085ff963SMatthew Dillon struct ieee80211_frame *wh;
221132176cfdSRui Paulo const struct ieee80211_rateset *rs;
2212841ab66cSSepherosa Ziehau struct mbuf *m;
2213841ab66cSSepherosa Ziehau uint8_t *frm;
2214085ff963SMatthew Dillon int ret;
2215841ab66cSSepherosa Ziehau
221632176cfdSRui Paulo if (vap->iv_state == IEEE80211_S_CAC) {
221732176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
221832176cfdSRui Paulo "block %s frame in CAC state", "probe request");
221932176cfdSRui Paulo vap->iv_stats.is_tx_badstate++;
222032176cfdSRui Paulo return EIO; /* XXX */
222132176cfdSRui Paulo }
222232176cfdSRui Paulo
2223841ab66cSSepherosa Ziehau /*
2224841ab66cSSepherosa Ziehau * Hold a reference on the node so it doesn't go away until after
2225841ab66cSSepherosa Ziehau * the xmit is complete all the way in the driver. On error we
2226841ab66cSSepherosa Ziehau * will remove our reference.
2227841ab66cSSepherosa Ziehau */
222832176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
22291e290df3SAntonio Huete Jimenez "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2230841ab66cSSepherosa Ziehau __func__, __LINE__,
2231085ff963SMatthew Dillon ni, ether_sprintf(ni->ni_macaddr),
2232841ab66cSSepherosa Ziehau ieee80211_node_refcnt(ni)+1);
2233841ab66cSSepherosa Ziehau ieee80211_ref_node(ni);
2234841ab66cSSepherosa Ziehau
2235841ab66cSSepherosa Ziehau /*
2236841ab66cSSepherosa Ziehau * prreq frame format
2237841ab66cSSepherosa Ziehau * [tlv] ssid
2238841ab66cSSepherosa Ziehau * [tlv] supported rates
223932176cfdSRui Paulo * [tlv] RSN (optional)
2240841ab66cSSepherosa Ziehau * [tlv] extended supported rates
224132176cfdSRui Paulo * [tlv] WPA (optional)
2242841ab66cSSepherosa Ziehau * [tlv] user-specified ie's
2243841ab66cSSepherosa Ziehau */
2244841ab66cSSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
22454ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
2246841ab66cSSepherosa Ziehau 2 + IEEE80211_NWID_LEN
2247841ab66cSSepherosa Ziehau + 2 + IEEE80211_RATE_SIZE
224832176cfdSRui Paulo + sizeof(struct ieee80211_ie_wpa)
2249841ab66cSSepherosa Ziehau + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
225032176cfdSRui Paulo + sizeof(struct ieee80211_ie_wpa)
225132176cfdSRui Paulo + (vap->iv_appie_probereq != NULL ?
225232176cfdSRui Paulo vap->iv_appie_probereq->ie_len : 0)
2253841ab66cSSepherosa Ziehau );
2254841ab66cSSepherosa Ziehau if (m == NULL) {
225532176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
2256841ab66cSSepherosa Ziehau ieee80211_free_node(ni);
2257841ab66cSSepherosa Ziehau return ENOMEM;
2258841ab66cSSepherosa Ziehau }
2259841ab66cSSepherosa Ziehau
2260841ab66cSSepherosa Ziehau frm = ieee80211_add_ssid(frm, ssid, ssidlen);
226132176cfdSRui Paulo rs = ieee80211_get_suprates(ic, ic->ic_curchan);
226232176cfdSRui Paulo frm = ieee80211_add_rates(frm, rs);
2263085ff963SMatthew Dillon frm = ieee80211_add_rsn(frm, vap);
226432176cfdSRui Paulo frm = ieee80211_add_xrates(frm, rs);
2265085ff963SMatthew Dillon frm = ieee80211_add_wpa(frm, vap);
226632176cfdSRui Paulo if (vap->iv_appie_probereq != NULL)
226732176cfdSRui Paulo frm = add_appie(frm, vap->iv_appie_probereq);
2268841ab66cSSepherosa Ziehau m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2269841ab66cSSepherosa Ziehau
227032176cfdSRui Paulo KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ieee80211_frame),
227132176cfdSRui Paulo ("leading space %zd", M_LEADINGSPACE(m)));
2272b5523eacSSascha Wildner M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
227332176cfdSRui Paulo if (m == NULL) {
227432176cfdSRui Paulo /* NB: cannot happen */
227532176cfdSRui Paulo ieee80211_free_node(ni);
2276841ab66cSSepherosa Ziehau return ENOMEM;
227732176cfdSRui Paulo }
2278841ab66cSSepherosa Ziehau
2279085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
2280085ff963SMatthew Dillon wh = mtod(m, struct ieee80211_frame *);
228132176cfdSRui Paulo ieee80211_send_setup(ni, m,
2282841ab66cSSepherosa Ziehau IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_REQ,
228332176cfdSRui Paulo IEEE80211_NONQOS_TID, sa, da, bssid);
2284841ab66cSSepherosa Ziehau /* XXX power management? */
228532176cfdSRui Paulo m->m_flags |= M_ENCAP; /* mark encapsulated */
228632176cfdSRui Paulo
228732176cfdSRui Paulo M_WME_SETAC(m, WME_AC_BE);
2288841ab66cSSepherosa Ziehau
2289841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_probereq);
2290841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_mgmt);
2291841ab66cSSepherosa Ziehau
229232176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
22931e290df3SAntonio Huete Jimenez "send probe req on channel %u bssid %s ssid \"%.*s\"\n",
2294085ff963SMatthew Dillon ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(bssid),
2295f92fae3fSSascha Wildner (int)ssidlen, ssid);
2296841ab66cSSepherosa Ziehau
229732176cfdSRui Paulo memset(¶ms, 0, sizeof(params));
229832176cfdSRui Paulo params.ibp_pri = M_WME_GETAC(m);
229932176cfdSRui Paulo tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
230032176cfdSRui Paulo params.ibp_rate0 = tp->mgmtrate;
230132176cfdSRui Paulo if (IEEE80211_IS_MULTICAST(da)) {
230232176cfdSRui Paulo params.ibp_flags |= IEEE80211_BPF_NOACK;
230332176cfdSRui Paulo params.ibp_try0 = 1;
230432176cfdSRui Paulo } else
230532176cfdSRui Paulo params.ibp_try0 = tp->maxretry;
230632176cfdSRui Paulo params.ibp_power = ni->ni_txpower;
2307085ff963SMatthew Dillon ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2308085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
2309085ff963SMatthew Dillon return (ret);
2310841ab66cSSepherosa Ziehau }
2311841ab66cSSepherosa Ziehau
2312841ab66cSSepherosa Ziehau /*
2313841ab66cSSepherosa Ziehau * Calculate capability information for mgt frames.
2314841ab66cSSepherosa Ziehau */
231532176cfdSRui Paulo uint16_t
231632176cfdSRui Paulo ieee80211_getcapinfo(struct ieee80211vap *vap, struct ieee80211_channel *chan)
2317841ab66cSSepherosa Ziehau {
231832176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic;
2319841ab66cSSepherosa Ziehau uint16_t capinfo;
2320841ab66cSSepherosa Ziehau
232132176cfdSRui Paulo KASSERT(vap->iv_opmode != IEEE80211_M_STA, ("station mode"));
2322841ab66cSSepherosa Ziehau
232332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2324841ab66cSSepherosa Ziehau capinfo = IEEE80211_CAPINFO_ESS;
232532176cfdSRui Paulo else if (vap->iv_opmode == IEEE80211_M_IBSS)
2326841ab66cSSepherosa Ziehau capinfo = IEEE80211_CAPINFO_IBSS;
2327841ab66cSSepherosa Ziehau else
2328841ab66cSSepherosa Ziehau capinfo = 0;
232932176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY)
2330841ab66cSSepherosa Ziehau capinfo |= IEEE80211_CAPINFO_PRIVACY;
233132176cfdSRui Paulo if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
233232176cfdSRui Paulo IEEE80211_IS_CHAN_2GHZ(chan))
2333841ab66cSSepherosa Ziehau capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2334841ab66cSSepherosa Ziehau if (ic->ic_flags & IEEE80211_F_SHSLOT)
2335841ab66cSSepherosa Ziehau capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
233632176cfdSRui Paulo if (IEEE80211_IS_CHAN_5GHZ(chan) && (vap->iv_flags & IEEE80211_F_DOTH))
233732176cfdSRui Paulo capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2338841ab66cSSepherosa Ziehau return capinfo;
2339f186073cSJoerg Sonnenberger }
2340f186073cSJoerg Sonnenberger
2341f186073cSJoerg Sonnenberger /*
2342f186073cSJoerg Sonnenberger * Send a management frame. The node is for the destination (or ic_bss
2343f186073cSJoerg Sonnenberger * when in station mode). Nodes other than ic_bss have their reference
2344f186073cSJoerg Sonnenberger * count bumped to reflect our use for an indeterminant time.
2345f186073cSJoerg Sonnenberger */
2346f186073cSJoerg Sonnenberger int
234732176cfdSRui Paulo ieee80211_send_mgmt(struct ieee80211_node *ni, int type, int arg)
2348f186073cSJoerg Sonnenberger {
234932176cfdSRui Paulo #define HTFLAGS (IEEE80211_NODE_HT | IEEE80211_NODE_HTCOMPAT)
235032176cfdSRui Paulo #define senderr(_x, _v) do { vap->iv_stats._v++; ret = _x; goto bad; } while (0)
235132176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
235232176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
235332176cfdSRui Paulo struct ieee80211_node *bss = vap->iv_bss;
235432176cfdSRui Paulo struct ieee80211_bpf_params params;
2355f186073cSJoerg Sonnenberger struct mbuf *m;
2356f186073cSJoerg Sonnenberger uint8_t *frm;
2357f186073cSJoerg Sonnenberger uint16_t capinfo;
235832176cfdSRui Paulo int has_challenge, is_shared_key, ret, status;
2359f186073cSJoerg Sonnenberger
2360f186073cSJoerg Sonnenberger KASSERT(ni != NULL, ("null node"));
2361f186073cSJoerg Sonnenberger
2362f186073cSJoerg Sonnenberger /*
2363f186073cSJoerg Sonnenberger * Hold a reference on the node so it doesn't go away until after
2364f186073cSJoerg Sonnenberger * the xmit is complete all the way in the driver. On error we
2365f186073cSJoerg Sonnenberger * will remove our reference.
2366f186073cSJoerg Sonnenberger */
236732176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
23681e290df3SAntonio Huete Jimenez "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2369841ab66cSSepherosa Ziehau __func__, __LINE__,
2370085ff963SMatthew Dillon ni, ether_sprintf(ni->ni_macaddr),
2371841ab66cSSepherosa Ziehau ieee80211_node_refcnt(ni)+1);
2372f186073cSJoerg Sonnenberger ieee80211_ref_node(ni);
2373841ab66cSSepherosa Ziehau
237432176cfdSRui Paulo memset(¶ms, 0, sizeof(params));
2375f186073cSJoerg Sonnenberger switch (type) {
2376f186073cSJoerg Sonnenberger
2377f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_AUTH:
2378841ab66cSSepherosa Ziehau status = arg >> 16;
2379841ab66cSSepherosa Ziehau arg &= 0xffff;
2380841ab66cSSepherosa Ziehau has_challenge = ((arg == IEEE80211_AUTH_SHARED_CHALLENGE ||
2381841ab66cSSepherosa Ziehau arg == IEEE80211_AUTH_SHARED_RESPONSE) &&
2382841ab66cSSepherosa Ziehau ni->ni_challenge != NULL);
2383841ab66cSSepherosa Ziehau
2384841ab66cSSepherosa Ziehau /*
2385841ab66cSSepherosa Ziehau * Deduce whether we're doing open authentication or
2386841ab66cSSepherosa Ziehau * shared key authentication. We do the latter if
2387841ab66cSSepherosa Ziehau * we're in the middle of a shared key authentication
2388841ab66cSSepherosa Ziehau * handshake or if we're initiating an authentication
2389841ab66cSSepherosa Ziehau * request and configured to use shared key.
2390841ab66cSSepherosa Ziehau */
2391841ab66cSSepherosa Ziehau is_shared_key = has_challenge ||
2392841ab66cSSepherosa Ziehau arg >= IEEE80211_AUTH_SHARED_RESPONSE ||
2393841ab66cSSepherosa Ziehau (arg == IEEE80211_AUTH_SHARED_REQUEST &&
239432176cfdSRui Paulo bss->ni_authmode == IEEE80211_AUTH_SHARED);
2395841ab66cSSepherosa Ziehau
2396841ab66cSSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
23974ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
2398841ab66cSSepherosa Ziehau 3 * sizeof(uint16_t)
2399841ab66cSSepherosa Ziehau + (has_challenge && status == IEEE80211_STATUS_SUCCESS ?
2400841ab66cSSepherosa Ziehau sizeof(uint16_t)+IEEE80211_CHALLENGE_LEN : 0)
2401841ab66cSSepherosa Ziehau );
2402f186073cSJoerg Sonnenberger if (m == NULL)
2403841ab66cSSepherosa Ziehau senderr(ENOMEM, is_tx_nobuf);
2404841ab66cSSepherosa Ziehau
2405841ab66cSSepherosa Ziehau ((uint16_t *)frm)[0] =
2406841ab66cSSepherosa Ziehau (is_shared_key) ? htole16(IEEE80211_AUTH_ALG_SHARED)
2407841ab66cSSepherosa Ziehau : htole16(IEEE80211_AUTH_ALG_OPEN);
2408f186073cSJoerg Sonnenberger ((uint16_t *)frm)[1] = htole16(arg); /* sequence number */
2409841ab66cSSepherosa Ziehau ((uint16_t *)frm)[2] = htole16(status);/* status */
2410841ab66cSSepherosa Ziehau
2411841ab66cSSepherosa Ziehau if (has_challenge && status == IEEE80211_STATUS_SUCCESS) {
2412841ab66cSSepherosa Ziehau ((uint16_t *)frm)[3] =
2413841ab66cSSepherosa Ziehau htole16((IEEE80211_CHALLENGE_LEN << 8) |
2414841ab66cSSepherosa Ziehau IEEE80211_ELEMID_CHALLENGE);
2415841ab66cSSepherosa Ziehau memcpy(&((uint16_t *)frm)[4], ni->ni_challenge,
2416841ab66cSSepherosa Ziehau IEEE80211_CHALLENGE_LEN);
2417841ab66cSSepherosa Ziehau m->m_pkthdr.len = m->m_len =
2418841ab66cSSepherosa Ziehau 4 * sizeof(uint16_t) + IEEE80211_CHALLENGE_LEN;
2419841ab66cSSepherosa Ziehau if (arg == IEEE80211_AUTH_SHARED_RESPONSE) {
242032176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
242132176cfdSRui Paulo "request encrypt frame (%s)", __func__);
242232176cfdSRui Paulo /* mark frame for encryption */
242332176cfdSRui Paulo params.ibp_flags |= IEEE80211_BPF_CRYPTO;
2424841ab66cSSepherosa Ziehau }
2425841ab66cSSepherosa Ziehau } else
2426841ab66cSSepherosa Ziehau m->m_pkthdr.len = m->m_len = 3 * sizeof(uint16_t);
2427841ab66cSSepherosa Ziehau
2428841ab66cSSepherosa Ziehau /* XXX not right for shared key */
2429841ab66cSSepherosa Ziehau if (status == IEEE80211_STATUS_SUCCESS)
2430841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_auth);
2431841ab66cSSepherosa Ziehau else
2432841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_auth_fail);
2433841ab66cSSepherosa Ziehau
243432176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_STA)
243532176cfdSRui Paulo ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
243632176cfdSRui Paulo (void *) vap->iv_state);
2437f186073cSJoerg Sonnenberger break;
2438f186073cSJoerg Sonnenberger
2439f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_DEAUTH:
244032176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_AUTH, ni,
24414f655ef5SMatthew Dillon "send station deauthenticate (reason: %d (%s))", arg,
24424f655ef5SMatthew Dillon ieee80211_reason_to_string(arg));
24434ac84526SSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
24444ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
24454ac84526SSepherosa Ziehau sizeof(uint16_t));
2446f186073cSJoerg Sonnenberger if (m == NULL)
2447841ab66cSSepherosa Ziehau senderr(ENOMEM, is_tx_nobuf);
2448841ab66cSSepherosa Ziehau *(uint16_t *)frm = htole16(arg); /* reason */
2449841ab66cSSepherosa Ziehau m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2450841ab66cSSepherosa Ziehau
2451841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_deauth);
2452841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT_SET(ni, tx_deauth_code, arg);
2453841ab66cSSepherosa Ziehau
2454841ab66cSSepherosa Ziehau ieee80211_node_unauthorize(ni); /* port closed */
2455f186073cSJoerg Sonnenberger break;
2456f186073cSJoerg Sonnenberger
2457f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2458208a1285SSepherosa Ziehau case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2459f186073cSJoerg Sonnenberger /*
2460f186073cSJoerg Sonnenberger * asreq frame format
2461f186073cSJoerg Sonnenberger * [2] capability information
2462f186073cSJoerg Sonnenberger * [2] listen interval
2463f186073cSJoerg Sonnenberger * [6*] current AP address (reassoc only)
2464f186073cSJoerg Sonnenberger * [tlv] ssid
2465f186073cSJoerg Sonnenberger * [tlv] supported rates
2466f186073cSJoerg Sonnenberger * [tlv] extended supported rates
246732176cfdSRui Paulo * [4] power capability (optional)
246832176cfdSRui Paulo * [28] supported channels (optional)
246932176cfdSRui Paulo * [tlv] HT capabilities
247032176cfdSRui Paulo * [tlv] WME (optional)
247132176cfdSRui Paulo * [tlv] Vendor OUI HT capabilities (optional)
247232176cfdSRui Paulo * [tlv] Atheros capabilities (if negotiated)
247332176cfdSRui Paulo * [tlv] AppIE's (optional)
2474f186073cSJoerg Sonnenberger */
2475841ab66cSSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
24764ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
2477841ab66cSSepherosa Ziehau sizeof(uint16_t)
2478f186073cSJoerg Sonnenberger + sizeof(uint16_t)
2479f186073cSJoerg Sonnenberger + IEEE80211_ADDR_LEN
2480841ab66cSSepherosa Ziehau + 2 + IEEE80211_NWID_LEN
2481f186073cSJoerg Sonnenberger + 2 + IEEE80211_RATE_SIZE
2482841ab66cSSepherosa Ziehau + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
248332176cfdSRui Paulo + 4
248432176cfdSRui Paulo + 2 + 26
2485841ab66cSSepherosa Ziehau + sizeof(struct ieee80211_wme_info)
248632176cfdSRui Paulo + sizeof(struct ieee80211_ie_htcap)
248732176cfdSRui Paulo + 4 + sizeof(struct ieee80211_ie_htcap)
248832176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
248932176cfdSRui Paulo + sizeof(struct ieee80211_ath_ie)
249032176cfdSRui Paulo #endif
249132176cfdSRui Paulo + (vap->iv_appie_wpa != NULL ?
249232176cfdSRui Paulo vap->iv_appie_wpa->ie_len : 0)
249332176cfdSRui Paulo + (vap->iv_appie_assocreq != NULL ?
249432176cfdSRui Paulo vap->iv_appie_assocreq->ie_len : 0)
2495841ab66cSSepherosa Ziehau );
2496f186073cSJoerg Sonnenberger if (m == NULL)
2497841ab66cSSepherosa Ziehau senderr(ENOMEM, is_tx_nobuf);
2498f186073cSJoerg Sonnenberger
249932176cfdSRui Paulo KASSERT(vap->iv_opmode == IEEE80211_M_STA,
250032176cfdSRui Paulo ("wrong mode %u", vap->iv_opmode));
2501841ab66cSSepherosa Ziehau capinfo = IEEE80211_CAPINFO_ESS;
250232176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_PRIVACY)
2503f186073cSJoerg Sonnenberger capinfo |= IEEE80211_CAPINFO_PRIVACY;
2504f186073cSJoerg Sonnenberger /*
2505f186073cSJoerg Sonnenberger * NB: Some 11a AP's reject the request when
250632176cfdSRui Paulo * short premable is set.
2507f186073cSJoerg Sonnenberger */
250832176cfdSRui Paulo if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
250932176cfdSRui Paulo IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2510f186073cSJoerg Sonnenberger capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
251132176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
2512841ab66cSSepherosa Ziehau (ic->ic_caps & IEEE80211_C_SHSLOT))
2513f186073cSJoerg Sonnenberger capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
251432176cfdSRui Paulo if ((ni->ni_capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) &&
251532176cfdSRui Paulo (vap->iv_flags & IEEE80211_F_DOTH))
251632176cfdSRui Paulo capinfo |= IEEE80211_CAPINFO_SPECTRUM_MGMT;
2517f186073cSJoerg Sonnenberger *(uint16_t *)frm = htole16(capinfo);
2518f186073cSJoerg Sonnenberger frm += 2;
2519f186073cSJoerg Sonnenberger
252032176cfdSRui Paulo KASSERT(bss->ni_intval != 0, ("beacon interval is zero!"));
252122b21df8SSepherosa Ziehau *(uint16_t *)frm = htole16(howmany(ic->ic_lintval,
252232176cfdSRui Paulo bss->ni_intval));
2523f186073cSJoerg Sonnenberger frm += 2;
2524f186073cSJoerg Sonnenberger
2525f186073cSJoerg Sonnenberger if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
252632176cfdSRui Paulo IEEE80211_ADDR_COPY(frm, bss->ni_bssid);
2527f186073cSJoerg Sonnenberger frm += IEEE80211_ADDR_LEN;
2528f186073cSJoerg Sonnenberger }
2529f186073cSJoerg Sonnenberger
2530f186073cSJoerg Sonnenberger frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
253132176cfdSRui Paulo frm = ieee80211_add_rates(frm, &ni->ni_rates);
2532085ff963SMatthew Dillon frm = ieee80211_add_rsn(frm, vap);
253332176cfdSRui Paulo frm = ieee80211_add_xrates(frm, &ni->ni_rates);
253432176cfdSRui Paulo if (capinfo & IEEE80211_CAPINFO_SPECTRUM_MGMT) {
253532176cfdSRui Paulo frm = ieee80211_add_powercapability(frm,
253632176cfdSRui Paulo ic->ic_curchan);
253732176cfdSRui Paulo frm = ieee80211_add_supportedchannels(frm, ic);
253832176cfdSRui Paulo }
25394f898719SImre Vadász
25404f898719SImre Vadász /*
25414f898719SImre Vadász * Check the channel - we may be using an 11n NIC with an
25424f898719SImre Vadász * 11n capable station, but we're configured to be an 11b
25434f898719SImre Vadász * channel.
25444f898719SImre Vadász */
254532176cfdSRui Paulo if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
25464f898719SImre Vadász IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
254732176cfdSRui Paulo ni->ni_ies.htcap_ie != NULL &&
25484f898719SImre Vadász ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_HTCAP) {
254932176cfdSRui Paulo frm = ieee80211_add_htcap(frm, ni);
25504f898719SImre Vadász }
2551085ff963SMatthew Dillon frm = ieee80211_add_wpa(frm, vap);
255232176cfdSRui Paulo if ((ic->ic_flags & IEEE80211_F_WME) &&
255332176cfdSRui Paulo ni->ni_ies.wme_ie != NULL)
255432176cfdSRui Paulo frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
25554f898719SImre Vadász
25564f898719SImre Vadász /*
25574f898719SImre Vadász * Same deal - only send HT info if we're on an 11n
25584f898719SImre Vadász * capable channel.
25594f898719SImre Vadász */
256032176cfdSRui Paulo if ((vap->iv_flags_ht & IEEE80211_FHT_HT) &&
25614f898719SImre Vadász IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
256232176cfdSRui Paulo ni->ni_ies.htcap_ie != NULL &&
25634f898719SImre Vadász ni->ni_ies.htcap_ie[0] == IEEE80211_ELEMID_VENDOR) {
256432176cfdSRui Paulo frm = ieee80211_add_htcap_vendor(frm, ni);
25654f898719SImre Vadász }
256632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
256732176cfdSRui Paulo if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS)) {
256832176cfdSRui Paulo frm = ieee80211_add_ath(frm,
256932176cfdSRui Paulo IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
257032176cfdSRui Paulo ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
257132176cfdSRui Paulo ni->ni_authmode != IEEE80211_AUTH_8021X) ?
257232176cfdSRui Paulo vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
257332176cfdSRui Paulo }
257432176cfdSRui Paulo #endif /* IEEE80211_SUPPORT_SUPERG */
257532176cfdSRui Paulo if (vap->iv_appie_assocreq != NULL)
257632176cfdSRui Paulo frm = add_appie(frm, vap->iv_appie_assocreq);
2577f186073cSJoerg Sonnenberger m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2578f186073cSJoerg Sonnenberger
257932176cfdSRui Paulo ieee80211_add_callback(m, ieee80211_tx_mgt_cb,
258032176cfdSRui Paulo (void *) vap->iv_state);
2581f186073cSJoerg Sonnenberger break;
2582f186073cSJoerg Sonnenberger
2583f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2584f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2585f186073cSJoerg Sonnenberger /*
258632176cfdSRui Paulo * asresp frame format
2587f186073cSJoerg Sonnenberger * [2] capability information
2588f186073cSJoerg Sonnenberger * [2] status
2589f186073cSJoerg Sonnenberger * [2] association ID
2590f186073cSJoerg Sonnenberger * [tlv] supported rates
2591f186073cSJoerg Sonnenberger * [tlv] extended supported rates
259232176cfdSRui Paulo * [tlv] HT capabilities (standard, if STA enabled)
259332176cfdSRui Paulo * [tlv] HT information (standard, if STA enabled)
259432176cfdSRui Paulo * [tlv] WME (if configured and STA enabled)
259532176cfdSRui Paulo * [tlv] HT capabilities (vendor OUI, if STA enabled)
259632176cfdSRui Paulo * [tlv] HT information (vendor OUI, if STA enabled)
259732176cfdSRui Paulo * [tlv] Atheros capabilities (if STA enabled)
259832176cfdSRui Paulo * [tlv] AppIE's (optional)
2599f186073cSJoerg Sonnenberger */
2600841ab66cSSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
26014ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
2602841ab66cSSepherosa Ziehau sizeof(uint16_t)
2603f186073cSJoerg Sonnenberger + sizeof(uint16_t)
2604f186073cSJoerg Sonnenberger + sizeof(uint16_t)
2605f186073cSJoerg Sonnenberger + 2 + IEEE80211_RATE_SIZE
2606841ab66cSSepherosa Ziehau + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
260732176cfdSRui Paulo + sizeof(struct ieee80211_ie_htcap) + 4
260832176cfdSRui Paulo + sizeof(struct ieee80211_ie_htinfo) + 4
2609841ab66cSSepherosa Ziehau + sizeof(struct ieee80211_wme_param)
261032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
261132176cfdSRui Paulo + sizeof(struct ieee80211_ath_ie)
261232176cfdSRui Paulo #endif
261332176cfdSRui Paulo + (vap->iv_appie_assocresp != NULL ?
261432176cfdSRui Paulo vap->iv_appie_assocresp->ie_len : 0)
2615841ab66cSSepherosa Ziehau );
2616f186073cSJoerg Sonnenberger if (m == NULL)
2617841ab66cSSepherosa Ziehau senderr(ENOMEM, is_tx_nobuf);
2618f186073cSJoerg Sonnenberger
261932176cfdSRui Paulo capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
2620f186073cSJoerg Sonnenberger *(uint16_t *)frm = htole16(capinfo);
2621f186073cSJoerg Sonnenberger frm += 2;
2622f186073cSJoerg Sonnenberger
2623f186073cSJoerg Sonnenberger *(uint16_t *)frm = htole16(arg); /* status */
2624f186073cSJoerg Sonnenberger frm += 2;
2625f186073cSJoerg Sonnenberger
2626841ab66cSSepherosa Ziehau if (arg == IEEE80211_STATUS_SUCCESS) {
2627f186073cSJoerg Sonnenberger *(uint16_t *)frm = htole16(ni->ni_associd);
2628841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_assoc);
2629841ab66cSSepherosa Ziehau } else
2630841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_assoc_fail);
2631f186073cSJoerg Sonnenberger frm += 2;
2632f186073cSJoerg Sonnenberger
263332176cfdSRui Paulo frm = ieee80211_add_rates(frm, &ni->ni_rates);
263432176cfdSRui Paulo frm = ieee80211_add_xrates(frm, &ni->ni_rates);
263532176cfdSRui Paulo /* NB: respond according to what we received */
263632176cfdSRui Paulo if ((ni->ni_flags & HTFLAGS) == IEEE80211_NODE_HT) {
263732176cfdSRui Paulo frm = ieee80211_add_htcap(frm, ni);
263832176cfdSRui Paulo frm = ieee80211_add_htinfo(frm, ni);
263932176cfdSRui Paulo }
264032176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_WME) &&
264132176cfdSRui Paulo ni->ni_ies.wme_ie != NULL)
2642841ab66cSSepherosa Ziehau frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
264332176cfdSRui Paulo if ((ni->ni_flags & HTFLAGS) == HTFLAGS) {
264432176cfdSRui Paulo frm = ieee80211_add_htcap_vendor(frm, ni);
264532176cfdSRui Paulo frm = ieee80211_add_htinfo_vendor(frm, ni);
264632176cfdSRui Paulo }
264732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
264832176cfdSRui Paulo if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS))
264932176cfdSRui Paulo frm = ieee80211_add_ath(frm,
265032176cfdSRui Paulo IEEE80211_ATH_CAP(vap, ni, IEEE80211_F_ATHEROS),
265132176cfdSRui Paulo ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
265232176cfdSRui Paulo ni->ni_authmode != IEEE80211_AUTH_8021X) ?
265332176cfdSRui Paulo vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
265432176cfdSRui Paulo #endif /* IEEE80211_SUPPORT_SUPERG */
265532176cfdSRui Paulo if (vap->iv_appie_assocresp != NULL)
265632176cfdSRui Paulo frm = add_appie(frm, vap->iv_appie_assocresp);
2657f186073cSJoerg Sonnenberger m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2658f186073cSJoerg Sonnenberger break;
2659f186073cSJoerg Sonnenberger
2660f186073cSJoerg Sonnenberger case IEEE80211_FC0_SUBTYPE_DISASSOC:
266132176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ASSOC, ni,
26624f655ef5SMatthew Dillon "send station disassociate (reason: %d (%s))", arg,
26634f655ef5SMatthew Dillon ieee80211_reason_to_string(arg));
26644ac84526SSepherosa Ziehau m = ieee80211_getmgtframe(&frm,
26654ac84526SSepherosa Ziehau ic->ic_headroom + sizeof(struct ieee80211_frame),
26664ac84526SSepherosa Ziehau sizeof(uint16_t));
2667f186073cSJoerg Sonnenberger if (m == NULL)
2668841ab66cSSepherosa Ziehau senderr(ENOMEM, is_tx_nobuf);
2669841ab66cSSepherosa Ziehau *(uint16_t *)frm = htole16(arg); /* reason */
2670841ab66cSSepherosa Ziehau m->m_pkthdr.len = m->m_len = sizeof(uint16_t);
2671841ab66cSSepherosa Ziehau
2672841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT(ni, tx_disassoc);
2673841ab66cSSepherosa Ziehau IEEE80211_NODE_STAT_SET(ni, tx_disassoc_code, arg);
2674f186073cSJoerg Sonnenberger break;
2675f186073cSJoerg Sonnenberger
2676f186073cSJoerg Sonnenberger default:
267732176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
267832176cfdSRui Paulo "invalid mgmt frame type %u", type);
2679f186073cSJoerg Sonnenberger senderr(EINVAL, is_tx_unknownmgt);
2680f186073cSJoerg Sonnenberger /* NOTREACHED */
2681f186073cSJoerg Sonnenberger }
268232176cfdSRui Paulo
268332176cfdSRui Paulo /* NB: force non-ProbeResp frames to the highest queue */
268432176cfdSRui Paulo params.ibp_pri = WME_AC_VO;
268532176cfdSRui Paulo params.ibp_rate0 = bss->ni_txparms->mgmtrate;
268632176cfdSRui Paulo /* NB: we know all frames are unicast */
268732176cfdSRui Paulo params.ibp_try0 = bss->ni_txparms->maxretry;
268832176cfdSRui Paulo params.ibp_power = bss->ni_txpower;
268932176cfdSRui Paulo return ieee80211_mgmt_output(ni, m, type, ¶ms);
2690f186073cSJoerg Sonnenberger bad:
2691841ab66cSSepherosa Ziehau ieee80211_free_node(ni);
2692f186073cSJoerg Sonnenberger return ret;
2693f186073cSJoerg Sonnenberger #undef senderr
269432176cfdSRui Paulo #undef HTFLAGS
2695f186073cSJoerg Sonnenberger }
2696841ab66cSSepherosa Ziehau
2697841ab66cSSepherosa Ziehau /*
269832176cfdSRui Paulo * Return an mbuf with a probe response frame in it.
269932176cfdSRui Paulo * Space is left to prepend and 802.11 header at the
270032176cfdSRui Paulo * front but it's left to the caller to fill in.
27013da93495SSepherosa Ziehau */
27023da93495SSepherosa Ziehau struct mbuf *
270332176cfdSRui Paulo ieee80211_alloc_proberesp(struct ieee80211_node *bss, int legacy)
27043da93495SSepherosa Ziehau {
270532176cfdSRui Paulo struct ieee80211vap *vap = bss->ni_vap;
270632176cfdSRui Paulo struct ieee80211com *ic = bss->ni_ic;
270732176cfdSRui Paulo const struct ieee80211_rateset *rs;
27083da93495SSepherosa Ziehau struct mbuf *m;
270932176cfdSRui Paulo uint16_t capinfo;
271032176cfdSRui Paulo uint8_t *frm;
27113da93495SSepherosa Ziehau
271232176cfdSRui Paulo /*
271332176cfdSRui Paulo * probe response frame format
271432176cfdSRui Paulo * [8] time stamp
271532176cfdSRui Paulo * [2] beacon interval
271632176cfdSRui Paulo * [2] cabability information
271732176cfdSRui Paulo * [tlv] ssid
271832176cfdSRui Paulo * [tlv] supported rates
271932176cfdSRui Paulo * [tlv] parameter set (FH/DS)
272032176cfdSRui Paulo * [tlv] parameter set (IBSS)
272132176cfdSRui Paulo * [tlv] country (optional)
272232176cfdSRui Paulo * [3] power control (optional)
272332176cfdSRui Paulo * [5] channel switch announcement (CSA) (optional)
272432176cfdSRui Paulo * [tlv] extended rate phy (ERP)
272532176cfdSRui Paulo * [tlv] extended supported rates
272632176cfdSRui Paulo * [tlv] RSN (optional)
272732176cfdSRui Paulo * [tlv] HT capabilities
272832176cfdSRui Paulo * [tlv] HT information
272932176cfdSRui Paulo * [tlv] WPA (optional)
273032176cfdSRui Paulo * [tlv] WME (optional)
273132176cfdSRui Paulo * [tlv] Vendor OUI HT capabilities (optional)
273232176cfdSRui Paulo * [tlv] Vendor OUI HT information (optional)
273332176cfdSRui Paulo * [tlv] Atheros capabilities
273432176cfdSRui Paulo * [tlv] AppIE's (optional)
273532176cfdSRui Paulo * [tlv] Mesh ID (MBSS)
273632176cfdSRui Paulo * [tlv] Mesh Conf (MBSS)
273732176cfdSRui Paulo */
273832176cfdSRui Paulo m = ieee80211_getmgtframe(&frm,
273932176cfdSRui Paulo ic->ic_headroom + sizeof(struct ieee80211_frame),
274032176cfdSRui Paulo 8
274132176cfdSRui Paulo + sizeof(uint16_t)
274232176cfdSRui Paulo + sizeof(uint16_t)
274332176cfdSRui Paulo + 2 + IEEE80211_NWID_LEN
274432176cfdSRui Paulo + 2 + IEEE80211_RATE_SIZE
274532176cfdSRui Paulo + 7 /* max(7,3) */
274632176cfdSRui Paulo + IEEE80211_COUNTRY_MAX_SIZE
274732176cfdSRui Paulo + 3
274832176cfdSRui Paulo + sizeof(struct ieee80211_csa_ie)
2749085ff963SMatthew Dillon + sizeof(struct ieee80211_quiet_ie)
275032176cfdSRui Paulo + 3
275132176cfdSRui Paulo + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
275232176cfdSRui Paulo + sizeof(struct ieee80211_ie_wpa)
275332176cfdSRui Paulo + sizeof(struct ieee80211_ie_htcap)
275432176cfdSRui Paulo + sizeof(struct ieee80211_ie_htinfo)
275532176cfdSRui Paulo + sizeof(struct ieee80211_ie_wpa)
275632176cfdSRui Paulo + sizeof(struct ieee80211_wme_param)
275732176cfdSRui Paulo + 4 + sizeof(struct ieee80211_ie_htcap)
275832176cfdSRui Paulo + 4 + sizeof(struct ieee80211_ie_htinfo)
275932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
276032176cfdSRui Paulo + sizeof(struct ieee80211_ath_ie)
276132176cfdSRui Paulo #endif
276232176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
276332176cfdSRui Paulo + 2 + IEEE80211_MESHID_LEN
276432176cfdSRui Paulo + sizeof(struct ieee80211_meshconf_ie)
276532176cfdSRui Paulo #endif
276632176cfdSRui Paulo + (vap->iv_appie_proberesp != NULL ?
276732176cfdSRui Paulo vap->iv_appie_proberesp->ie_len : 0)
276832176cfdSRui Paulo );
276932176cfdSRui Paulo if (m == NULL) {
277032176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
27713da93495SSepherosa Ziehau return NULL;
277232176cfdSRui Paulo }
27733da93495SSepherosa Ziehau
277432176cfdSRui Paulo memset(frm, 0, 8); /* timestamp should be filled later */
277532176cfdSRui Paulo frm += 8;
277632176cfdSRui Paulo *(uint16_t *)frm = htole16(bss->ni_intval);
277732176cfdSRui Paulo frm += 2;
277832176cfdSRui Paulo capinfo = ieee80211_getcapinfo(vap, bss->ni_chan);
277932176cfdSRui Paulo *(uint16_t *)frm = htole16(capinfo);
278032176cfdSRui Paulo frm += 2;
27813da93495SSepherosa Ziehau
278232176cfdSRui Paulo frm = ieee80211_add_ssid(frm, bss->ni_essid, bss->ni_esslen);
278332176cfdSRui Paulo rs = ieee80211_get_suprates(ic, bss->ni_chan);
278432176cfdSRui Paulo frm = ieee80211_add_rates(frm, rs);
278532176cfdSRui Paulo
278632176cfdSRui Paulo if (IEEE80211_IS_CHAN_FHSS(bss->ni_chan)) {
278732176cfdSRui Paulo *frm++ = IEEE80211_ELEMID_FHPARMS;
278832176cfdSRui Paulo *frm++ = 5;
278932176cfdSRui Paulo *frm++ = bss->ni_fhdwell & 0x00ff;
279032176cfdSRui Paulo *frm++ = (bss->ni_fhdwell >> 8) & 0x00ff;
279132176cfdSRui Paulo *frm++ = IEEE80211_FH_CHANSET(
279232176cfdSRui Paulo ieee80211_chan2ieee(ic, bss->ni_chan));
279332176cfdSRui Paulo *frm++ = IEEE80211_FH_CHANPAT(
279432176cfdSRui Paulo ieee80211_chan2ieee(ic, bss->ni_chan));
279532176cfdSRui Paulo *frm++ = bss->ni_fhindex;
279632176cfdSRui Paulo } else {
279732176cfdSRui Paulo *frm++ = IEEE80211_ELEMID_DSPARMS;
279832176cfdSRui Paulo *frm++ = 1;
279932176cfdSRui Paulo *frm++ = ieee80211_chan2ieee(ic, bss->ni_chan);
280032176cfdSRui Paulo }
280132176cfdSRui Paulo
280232176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS) {
280332176cfdSRui Paulo *frm++ = IEEE80211_ELEMID_IBSSPARMS;
280432176cfdSRui Paulo *frm++ = 2;
280532176cfdSRui Paulo *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
280632176cfdSRui Paulo }
280732176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_DOTH) ||
280832176cfdSRui Paulo (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
280932176cfdSRui Paulo frm = ieee80211_add_countryie(frm, ic);
281032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_DOTH) {
281132176cfdSRui Paulo if (IEEE80211_IS_CHAN_5GHZ(bss->ni_chan))
281232176cfdSRui Paulo frm = ieee80211_add_powerconstraint(frm, vap);
281332176cfdSRui Paulo if (ic->ic_flags & IEEE80211_F_CSAPENDING)
281432176cfdSRui Paulo frm = ieee80211_add_csa(frm, vap);
281532176cfdSRui Paulo }
2816085ff963SMatthew Dillon if (vap->iv_flags & IEEE80211_F_DOTH) {
2817085ff963SMatthew Dillon if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
2818085ff963SMatthew Dillon (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
2819085ff963SMatthew Dillon if (vap->iv_quiet)
2820085ff963SMatthew Dillon frm = ieee80211_add_quiet(frm, vap);
2821085ff963SMatthew Dillon }
2822085ff963SMatthew Dillon }
282332176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(bss->ni_chan))
282432176cfdSRui Paulo frm = ieee80211_add_erp(frm, ic);
282532176cfdSRui Paulo frm = ieee80211_add_xrates(frm, rs);
2826085ff963SMatthew Dillon frm = ieee80211_add_rsn(frm, vap);
282732176cfdSRui Paulo /*
282832176cfdSRui Paulo * NB: legacy 11b clients do not get certain ie's.
282932176cfdSRui Paulo * The caller identifies such clients by passing
283032176cfdSRui Paulo * a token in legacy to us. Could expand this to be
283132176cfdSRui Paulo * any legacy client for stuff like HT ie's.
283232176cfdSRui Paulo */
283332176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
283432176cfdSRui Paulo legacy != IEEE80211_SEND_LEGACY_11B) {
283532176cfdSRui Paulo frm = ieee80211_add_htcap(frm, bss);
283632176cfdSRui Paulo frm = ieee80211_add_htinfo(frm, bss);
283732176cfdSRui Paulo }
2838085ff963SMatthew Dillon frm = ieee80211_add_wpa(frm, vap);
283932176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_WME)
284032176cfdSRui Paulo frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
284132176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(bss->ni_chan) &&
284232176cfdSRui Paulo (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) &&
284332176cfdSRui Paulo legacy != IEEE80211_SEND_LEGACY_11B) {
284432176cfdSRui Paulo frm = ieee80211_add_htcap_vendor(frm, bss);
284532176cfdSRui Paulo frm = ieee80211_add_htinfo_vendor(frm, bss);
284632176cfdSRui Paulo }
284732176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
284832176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_ATHEROS) &&
284932176cfdSRui Paulo legacy != IEEE80211_SEND_LEGACY_11B)
285032176cfdSRui Paulo frm = ieee80211_add_athcaps(frm, bss);
285132176cfdSRui Paulo #endif
285232176cfdSRui Paulo if (vap->iv_appie_proberesp != NULL)
285332176cfdSRui Paulo frm = add_appie(frm, vap->iv_appie_proberesp);
285432176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
285532176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) {
285632176cfdSRui Paulo frm = ieee80211_add_meshid(frm, vap);
285732176cfdSRui Paulo frm = ieee80211_add_meshconf(frm, vap);
285832176cfdSRui Paulo }
285932176cfdSRui Paulo #endif
286032176cfdSRui Paulo m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
28613da93495SSepherosa Ziehau
28623da93495SSepherosa Ziehau return m;
28633da93495SSepherosa Ziehau }
28643da93495SSepherosa Ziehau
28653da93495SSepherosa Ziehau /*
286632176cfdSRui Paulo * Send a probe response frame to the specified mac address.
286732176cfdSRui Paulo * This does not go through the normal mgt frame api so we
286832176cfdSRui Paulo * can specify the destination address and re-use the bss node
286932176cfdSRui Paulo * for the sta reference.
2870841ab66cSSepherosa Ziehau */
287132176cfdSRui Paulo int
287232176cfdSRui Paulo ieee80211_send_proberesp(struct ieee80211vap *vap,
287332176cfdSRui Paulo const uint8_t da[IEEE80211_ADDR_LEN], int legacy)
2874841ab66cSSepherosa Ziehau {
287532176cfdSRui Paulo struct ieee80211_node *bss = vap->iv_bss;
287632176cfdSRui Paulo struct ieee80211com *ic = vap->iv_ic;
2877085ff963SMatthew Dillon struct ieee80211_frame *wh;
2878841ab66cSSepherosa Ziehau struct mbuf *m;
2879085ff963SMatthew Dillon int ret;
288032176cfdSRui Paulo
288132176cfdSRui Paulo if (vap->iv_state == IEEE80211_S_CAC) {
288232176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, bss,
288332176cfdSRui Paulo "block %s frame in CAC state", "probe response");
288432176cfdSRui Paulo vap->iv_stats.is_tx_badstate++;
288532176cfdSRui Paulo return EIO; /* XXX */
288632176cfdSRui Paulo }
288732176cfdSRui Paulo
288832176cfdSRui Paulo /*
288932176cfdSRui Paulo * Hold a reference on the node so it doesn't go away until after
289032176cfdSRui Paulo * the xmit is complete all the way in the driver. On error we
289132176cfdSRui Paulo * will remove our reference.
289232176cfdSRui Paulo */
289332176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
28941e290df3SAntonio Huete Jimenez "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
2895085ff963SMatthew Dillon __func__, __LINE__, bss, ether_sprintf(bss->ni_macaddr),
289632176cfdSRui Paulo ieee80211_node_refcnt(bss)+1);
289732176cfdSRui Paulo ieee80211_ref_node(bss);
289832176cfdSRui Paulo
289932176cfdSRui Paulo m = ieee80211_alloc_proberesp(bss, legacy);
290032176cfdSRui Paulo if (m == NULL) {
290132176cfdSRui Paulo ieee80211_free_node(bss);
290232176cfdSRui Paulo return ENOMEM;
290332176cfdSRui Paulo }
290432176cfdSRui Paulo
2905b5523eacSSascha Wildner M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
290632176cfdSRui Paulo KASSERT(m != NULL, ("no room for header"));
290732176cfdSRui Paulo
2908085ff963SMatthew Dillon IEEE80211_TX_LOCK(ic);
2909085ff963SMatthew Dillon wh = mtod(m, struct ieee80211_frame *);
291032176cfdSRui Paulo ieee80211_send_setup(bss, m,
291132176cfdSRui Paulo IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP,
291232176cfdSRui Paulo IEEE80211_NONQOS_TID, vap->iv_myaddr, da, bss->ni_bssid);
291332176cfdSRui Paulo /* XXX power management? */
291432176cfdSRui Paulo m->m_flags |= M_ENCAP; /* mark encapsulated */
291532176cfdSRui Paulo
291632176cfdSRui Paulo M_WME_SETAC(m, WME_AC_BE);
291732176cfdSRui Paulo
291832176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG | IEEE80211_MSG_DUMPPKTS,
29191e290df3SAntonio Huete Jimenez "send probe resp on channel %u to %s%s\n",
2920085ff963SMatthew Dillon ieee80211_chan2ieee(ic, ic->ic_curchan), ether_sprintf(da),
292132176cfdSRui Paulo legacy ? " <legacy>" : "");
292232176cfdSRui Paulo IEEE80211_NODE_STAT(bss, tx_mgmt);
292332176cfdSRui Paulo
2924085ff963SMatthew Dillon ret = ieee80211_raw_output(vap, bss, m, NULL);
2925085ff963SMatthew Dillon IEEE80211_TX_UNLOCK(ic);
2926085ff963SMatthew Dillon return (ret);
292732176cfdSRui Paulo }
292832176cfdSRui Paulo
292932176cfdSRui Paulo /*
293032176cfdSRui Paulo * Allocate and build a RTS (Request To Send) control frame.
293132176cfdSRui Paulo */
293232176cfdSRui Paulo struct mbuf *
293332176cfdSRui Paulo ieee80211_alloc_rts(struct ieee80211com *ic,
293432176cfdSRui Paulo const uint8_t ra[IEEE80211_ADDR_LEN],
293532176cfdSRui Paulo const uint8_t ta[IEEE80211_ADDR_LEN],
293632176cfdSRui Paulo uint16_t dur)
293732176cfdSRui Paulo {
293832176cfdSRui Paulo struct ieee80211_frame_rts *rts;
293932176cfdSRui Paulo struct mbuf *m;
294032176cfdSRui Paulo
294132176cfdSRui Paulo /* XXX honor ic_headroom */
2942b5523eacSSascha Wildner m = m_gethdr(M_NOWAIT, MT_DATA);
294332176cfdSRui Paulo if (m != NULL) {
294432176cfdSRui Paulo rts = mtod(m, struct ieee80211_frame_rts *);
294532176cfdSRui Paulo rts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
294632176cfdSRui Paulo IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_RTS;
294732176cfdSRui Paulo rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
294832176cfdSRui Paulo *(u_int16_t *)rts->i_dur = htole16(dur);
294932176cfdSRui Paulo IEEE80211_ADDR_COPY(rts->i_ra, ra);
295032176cfdSRui Paulo IEEE80211_ADDR_COPY(rts->i_ta, ta);
295132176cfdSRui Paulo
295232176cfdSRui Paulo m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
295332176cfdSRui Paulo }
295432176cfdSRui Paulo return m;
295532176cfdSRui Paulo }
295632176cfdSRui Paulo
295732176cfdSRui Paulo /*
295832176cfdSRui Paulo * Allocate and build a CTS (Clear To Send) control frame.
295932176cfdSRui Paulo */
296032176cfdSRui Paulo struct mbuf *
296132176cfdSRui Paulo ieee80211_alloc_cts(struct ieee80211com *ic,
296232176cfdSRui Paulo const uint8_t ra[IEEE80211_ADDR_LEN], uint16_t dur)
296332176cfdSRui Paulo {
296432176cfdSRui Paulo struct ieee80211_frame_cts *cts;
296532176cfdSRui Paulo struct mbuf *m;
296632176cfdSRui Paulo
296732176cfdSRui Paulo /* XXX honor ic_headroom */
2968b5523eacSSascha Wildner m = m_gethdr(M_NOWAIT, MT_DATA);
296932176cfdSRui Paulo if (m != NULL) {
297032176cfdSRui Paulo cts = mtod(m, struct ieee80211_frame_cts *);
297132176cfdSRui Paulo cts->i_fc[0] = IEEE80211_FC0_VERSION_0 |
297232176cfdSRui Paulo IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_CTS;
297332176cfdSRui Paulo cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
297432176cfdSRui Paulo *(u_int16_t *)cts->i_dur = htole16(dur);
297532176cfdSRui Paulo IEEE80211_ADDR_COPY(cts->i_ra, ra);
297632176cfdSRui Paulo
297732176cfdSRui Paulo m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
297832176cfdSRui Paulo }
297932176cfdSRui Paulo return m;
298032176cfdSRui Paulo }
298132176cfdSRui Paulo
298232176cfdSRui Paulo static void
2983085ff963SMatthew Dillon ieee80211_tx_mgt_timeout(void *arg)
298432176cfdSRui Paulo {
2985085ff963SMatthew Dillon struct ieee80211vap *vap = arg;
298632176cfdSRui Paulo
2987085ff963SMatthew Dillon IEEE80211_LOCK(vap->iv_ic);
298832176cfdSRui Paulo if (vap->iv_state != IEEE80211_S_INIT &&
298932176cfdSRui Paulo (vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0) {
299032176cfdSRui Paulo /*
299132176cfdSRui Paulo * NB: it's safe to specify a timeout as the reason here;
299232176cfdSRui Paulo * it'll only be used in the right state.
299332176cfdSRui Paulo */
2994085ff963SMatthew Dillon ieee80211_new_state_locked(vap, IEEE80211_S_SCAN,
299532176cfdSRui Paulo IEEE80211_SCAN_FAIL_TIMEOUT);
299632176cfdSRui Paulo }
2997085ff963SMatthew Dillon IEEE80211_UNLOCK(vap->iv_ic);
299832176cfdSRui Paulo }
299932176cfdSRui Paulo
3000085ff963SMatthew Dillon /*
3001085ff963SMatthew Dillon * This is the callback set on net80211-sourced transmitted
3002085ff963SMatthew Dillon * authentication request frames.
3003085ff963SMatthew Dillon *
3004085ff963SMatthew Dillon * This does a couple of things:
3005085ff963SMatthew Dillon *
3006085ff963SMatthew Dillon * + If the frame transmitted was a success, it schedules a future
3007085ff963SMatthew Dillon * event which will transition the interface to scan.
3008085ff963SMatthew Dillon * If a state transition _then_ occurs before that event occurs,
3009085ff963SMatthew Dillon * said state transition will cancel this callout.
3010085ff963SMatthew Dillon *
3011085ff963SMatthew Dillon * + If the frame transmit was a failure, it immediately schedules
3012085ff963SMatthew Dillon * the transition back to scan.
3013085ff963SMatthew Dillon */
301432176cfdSRui Paulo static void
301532176cfdSRui Paulo ieee80211_tx_mgt_cb(struct ieee80211_node *ni, void *arg, int status)
301632176cfdSRui Paulo {
301732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
301832176cfdSRui Paulo enum ieee80211_state ostate = (enum ieee80211_state) arg;
301932176cfdSRui Paulo
302032176cfdSRui Paulo /*
302132176cfdSRui Paulo * Frame transmit completed; arrange timer callback. If
30224f655ef5SMatthew Dillon * transmit was successfully we wait for response. Otherwise
302332176cfdSRui Paulo * we arrange an immediate callback instead of doing the
302432176cfdSRui Paulo * callback directly since we don't know what state the driver
302532176cfdSRui Paulo * is in (e.g. what locks it is holding). This work should
302632176cfdSRui Paulo * not be too time-critical and not happen too often so the
302732176cfdSRui Paulo * added overhead is acceptable.
302832176cfdSRui Paulo *
302932176cfdSRui Paulo * XXX what happens if !acked but response shows up before callback?
303032176cfdSRui Paulo */
3031085ff963SMatthew Dillon if (vap->iv_state == ostate) {
303232176cfdSRui Paulo callout_reset(&vap->iv_mgtsend,
303332176cfdSRui Paulo status == 0 ? IEEE80211_TRANS_WAIT*hz : 0,
3034085ff963SMatthew Dillon ieee80211_tx_mgt_timeout, vap);
3035085ff963SMatthew Dillon }
303632176cfdSRui Paulo }
303732176cfdSRui Paulo
303832176cfdSRui Paulo static void
303932176cfdSRui Paulo ieee80211_beacon_construct(struct mbuf *m, uint8_t *frm,
30404f655ef5SMatthew Dillon struct ieee80211_node *ni)
304132176cfdSRui Paulo {
304232176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
30434f655ef5SMatthew Dillon struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
304432176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
304532176cfdSRui Paulo struct ieee80211_rateset *rs = &ni->ni_rates;
3046841ab66cSSepherosa Ziehau uint16_t capinfo;
3047841ab66cSSepherosa Ziehau
3048841ab66cSSepherosa Ziehau /*
3049841ab66cSSepherosa Ziehau * beacon frame format
3050841ab66cSSepherosa Ziehau * [8] time stamp
3051841ab66cSSepherosa Ziehau * [2] beacon interval
3052841ab66cSSepherosa Ziehau * [2] cabability information
3053841ab66cSSepherosa Ziehau * [tlv] ssid
3054841ab66cSSepherosa Ziehau * [tlv] supported rates
3055841ab66cSSepherosa Ziehau * [3] parameter set (DS)
305632176cfdSRui Paulo * [8] CF parameter set (optional)
3057841ab66cSSepherosa Ziehau * [tlv] parameter set (IBSS/TIM)
305832176cfdSRui Paulo * [tlv] country (optional)
305932176cfdSRui Paulo * [3] power control (optional)
306032176cfdSRui Paulo * [5] channel switch announcement (CSA) (optional)
3061841ab66cSSepherosa Ziehau * [tlv] extended rate phy (ERP)
3062841ab66cSSepherosa Ziehau * [tlv] extended supported rates
306332176cfdSRui Paulo * [tlv] RSN parameters
306432176cfdSRui Paulo * [tlv] HT capabilities
306532176cfdSRui Paulo * [tlv] HT information
3066841ab66cSSepherosa Ziehau * XXX Vendor-specific OIDs (e.g. Atheros)
306732176cfdSRui Paulo * [tlv] WPA parameters
306832176cfdSRui Paulo * [tlv] WME parameters
306932176cfdSRui Paulo * [tlv] Vendor OUI HT capabilities (optional)
307032176cfdSRui Paulo * [tlv] Vendor OUI HT information (optional)
307132176cfdSRui Paulo * [tlv] Atheros capabilities (optional)
307232176cfdSRui Paulo * [tlv] TDMA parameters (optional)
307332176cfdSRui Paulo * [tlv] Mesh ID (MBSS)
307432176cfdSRui Paulo * [tlv] Mesh Conf (MBSS)
307532176cfdSRui Paulo * [tlv] application data (optional)
3076841ab66cSSepherosa Ziehau */
307732176cfdSRui Paulo
307832176cfdSRui Paulo memset(bo, 0, sizeof(*bo));
3079841ab66cSSepherosa Ziehau
3080841ab66cSSepherosa Ziehau memset(frm, 0, 8); /* XXX timestamp is set by hardware/driver */
3081841ab66cSSepherosa Ziehau frm += 8;
3082841ab66cSSepherosa Ziehau *(uint16_t *)frm = htole16(ni->ni_intval);
3083841ab66cSSepherosa Ziehau frm += 2;
308432176cfdSRui Paulo capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3085841ab66cSSepherosa Ziehau bo->bo_caps = (uint16_t *)frm;
3086841ab66cSSepherosa Ziehau *(uint16_t *)frm = htole16(capinfo);
3087841ab66cSSepherosa Ziehau frm += 2;
3088841ab66cSSepherosa Ziehau *frm++ = IEEE80211_ELEMID_SSID;
308932176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_HIDESSID) == 0) {
3090841ab66cSSepherosa Ziehau *frm++ = ni->ni_esslen;
3091841ab66cSSepherosa Ziehau memcpy(frm, ni->ni_essid, ni->ni_esslen);
3092841ab66cSSepherosa Ziehau frm += ni->ni_esslen;
3093841ab66cSSepherosa Ziehau } else
3094841ab66cSSepherosa Ziehau *frm++ = 0;
3095841ab66cSSepherosa Ziehau frm = ieee80211_add_rates(frm, rs);
309632176cfdSRui Paulo if (!IEEE80211_IS_CHAN_FHSS(ni->ni_chan)) {
3097841ab66cSSepherosa Ziehau *frm++ = IEEE80211_ELEMID_DSPARMS;
3098841ab66cSSepherosa Ziehau *frm++ = 1;
3099841ab66cSSepherosa Ziehau *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
3100841ab66cSSepherosa Ziehau }
310132176cfdSRui Paulo if (ic->ic_flags & IEEE80211_F_PCF) {
310232176cfdSRui Paulo bo->bo_cfp = frm;
310332176cfdSRui Paulo frm = ieee80211_add_cfparms(frm, ic);
310432176cfdSRui Paulo }
3105841ab66cSSepherosa Ziehau bo->bo_tim = frm;
310632176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_IBSS) {
3107841ab66cSSepherosa Ziehau *frm++ = IEEE80211_ELEMID_IBSSPARMS;
3108841ab66cSSepherosa Ziehau *frm++ = 2;
3109841ab66cSSepherosa Ziehau *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
3110841ab66cSSepherosa Ziehau bo->bo_tim_len = 0;
311132176cfdSRui Paulo } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
311232176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_MBSS) {
311332176cfdSRui Paulo /* TIM IE is the same for Mesh and Hostap */
3114841ab66cSSepherosa Ziehau struct ieee80211_tim_ie *tie = (struct ieee80211_tim_ie *) frm;
3115841ab66cSSepherosa Ziehau
3116841ab66cSSepherosa Ziehau tie->tim_ie = IEEE80211_ELEMID_TIM;
3117841ab66cSSepherosa Ziehau tie->tim_len = 4; /* length */
3118841ab66cSSepherosa Ziehau tie->tim_count = 0; /* DTIM count */
311932176cfdSRui Paulo tie->tim_period = vap->iv_dtim_period; /* DTIM period */
3120841ab66cSSepherosa Ziehau tie->tim_bitctl = 0; /* bitmap control */
3121841ab66cSSepherosa Ziehau tie->tim_bitmap[0] = 0; /* Partial Virtual Bitmap */
3122841ab66cSSepherosa Ziehau frm += sizeof(struct ieee80211_tim_ie);
3123841ab66cSSepherosa Ziehau bo->bo_tim_len = 1;
3124841ab66cSSepherosa Ziehau }
312532176cfdSRui Paulo bo->bo_tim_trailer = frm;
312632176cfdSRui Paulo if ((vap->iv_flags & IEEE80211_F_DOTH) ||
312732176cfdSRui Paulo (vap->iv_flags_ext & IEEE80211_FEXT_DOTD))
312832176cfdSRui Paulo frm = ieee80211_add_countryie(frm, ic);
312932176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_DOTH) {
313032176cfdSRui Paulo if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan))
313132176cfdSRui Paulo frm = ieee80211_add_powerconstraint(frm, vap);
313232176cfdSRui Paulo bo->bo_csa = frm;
313332176cfdSRui Paulo if (ic->ic_flags & IEEE80211_F_CSAPENDING)
313432176cfdSRui Paulo frm = ieee80211_add_csa(frm, vap);
313532176cfdSRui Paulo } else
313632176cfdSRui Paulo bo->bo_csa = frm;
3137085ff963SMatthew Dillon
3138085ff963SMatthew Dillon if (vap->iv_flags & IEEE80211_F_DOTH) {
3139085ff963SMatthew Dillon bo->bo_quiet = frm;
3140085ff963SMatthew Dillon if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3141085ff963SMatthew Dillon (vap->iv_flags_ext & IEEE80211_FEXT_DFS)) {
3142085ff963SMatthew Dillon if (vap->iv_quiet)
3143085ff963SMatthew Dillon frm = ieee80211_add_quiet(frm,vap);
3144085ff963SMatthew Dillon }
3145085ff963SMatthew Dillon } else
3146085ff963SMatthew Dillon bo->bo_quiet = frm;
3147085ff963SMatthew Dillon
314832176cfdSRui Paulo if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) {
3149841ab66cSSepherosa Ziehau bo->bo_erp = frm;
3150841ab66cSSepherosa Ziehau frm = ieee80211_add_erp(frm, ic);
3151841ab66cSSepherosa Ziehau }
315232176cfdSRui Paulo frm = ieee80211_add_xrates(frm, rs);
3153085ff963SMatthew Dillon frm = ieee80211_add_rsn(frm, vap);
315432176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
315532176cfdSRui Paulo frm = ieee80211_add_htcap(frm, ni);
315632176cfdSRui Paulo bo->bo_htinfo = frm;
315732176cfdSRui Paulo frm = ieee80211_add_htinfo(frm, ni);
315832176cfdSRui Paulo }
3159085ff963SMatthew Dillon frm = ieee80211_add_wpa(frm, vap);
316032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_WME) {
316132176cfdSRui Paulo bo->bo_wme = frm;
316232176cfdSRui Paulo frm = ieee80211_add_wme_param(frm, &ic->ic_wme);
316332176cfdSRui Paulo }
316432176cfdSRui Paulo if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
316532176cfdSRui Paulo (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT)) {
316632176cfdSRui Paulo frm = ieee80211_add_htcap_vendor(frm, ni);
316732176cfdSRui Paulo frm = ieee80211_add_htinfo_vendor(frm, ni);
316832176cfdSRui Paulo }
316932176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
317032176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_ATHEROS) {
317132176cfdSRui Paulo bo->bo_ath = frm;
317232176cfdSRui Paulo frm = ieee80211_add_athcaps(frm, ni);
317332176cfdSRui Paulo }
317432176cfdSRui Paulo #endif
317532176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA
317632176cfdSRui Paulo if (vap->iv_caps & IEEE80211_C_TDMA) {
317732176cfdSRui Paulo bo->bo_tdma = frm;
317832176cfdSRui Paulo frm = ieee80211_add_tdma(frm, vap);
317932176cfdSRui Paulo }
318032176cfdSRui Paulo #endif
318132176cfdSRui Paulo if (vap->iv_appie_beacon != NULL) {
318232176cfdSRui Paulo bo->bo_appie = frm;
318332176cfdSRui Paulo bo->bo_appie_len = vap->iv_appie_beacon->ie_len;
318432176cfdSRui Paulo frm = add_appie(frm, vap->iv_appie_beacon);
318532176cfdSRui Paulo }
318632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
318732176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS) {
318832176cfdSRui Paulo frm = ieee80211_add_meshid(frm, vap);
318932176cfdSRui Paulo bo->bo_meshconf = frm;
319032176cfdSRui Paulo frm = ieee80211_add_meshconf(frm, vap);
319132176cfdSRui Paulo }
319232176cfdSRui Paulo #endif
319332176cfdSRui Paulo bo->bo_tim_trailer_len = frm - bo->bo_tim_trailer;
319432176cfdSRui Paulo bo->bo_csa_trailer_len = frm - bo->bo_csa;
319532176cfdSRui Paulo m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
319632176cfdSRui Paulo }
319732176cfdSRui Paulo
319832176cfdSRui Paulo /*
319932176cfdSRui Paulo * Allocate a beacon frame and fillin the appropriate bits.
320032176cfdSRui Paulo */
320132176cfdSRui Paulo struct mbuf *
32024f655ef5SMatthew Dillon ieee80211_beacon_alloc(struct ieee80211_node *ni)
320332176cfdSRui Paulo {
320432176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
320532176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
320632176cfdSRui Paulo struct ifnet *ifp = vap->iv_ifp;
320732176cfdSRui Paulo struct ieee80211_frame *wh;
320832176cfdSRui Paulo struct mbuf *m;
320932176cfdSRui Paulo int pktlen;
321032176cfdSRui Paulo uint8_t *frm;
321132176cfdSRui Paulo
321232176cfdSRui Paulo /*
321332176cfdSRui Paulo * beacon frame format
321432176cfdSRui Paulo * [8] time stamp
321532176cfdSRui Paulo * [2] beacon interval
321632176cfdSRui Paulo * [2] cabability information
321732176cfdSRui Paulo * [tlv] ssid
321832176cfdSRui Paulo * [tlv] supported rates
321932176cfdSRui Paulo * [3] parameter set (DS)
322032176cfdSRui Paulo * [8] CF parameter set (optional)
322132176cfdSRui Paulo * [tlv] parameter set (IBSS/TIM)
322232176cfdSRui Paulo * [tlv] country (optional)
322332176cfdSRui Paulo * [3] power control (optional)
322432176cfdSRui Paulo * [5] channel switch announcement (CSA) (optional)
322532176cfdSRui Paulo * [tlv] extended rate phy (ERP)
322632176cfdSRui Paulo * [tlv] extended supported rates
322732176cfdSRui Paulo * [tlv] RSN parameters
322832176cfdSRui Paulo * [tlv] HT capabilities
322932176cfdSRui Paulo * [tlv] HT information
323032176cfdSRui Paulo * [tlv] Vendor OUI HT capabilities (optional)
323132176cfdSRui Paulo * [tlv] Vendor OUI HT information (optional)
323232176cfdSRui Paulo * XXX Vendor-specific OIDs (e.g. Atheros)
323332176cfdSRui Paulo * [tlv] WPA parameters
323432176cfdSRui Paulo * [tlv] WME parameters
323532176cfdSRui Paulo * [tlv] TDMA parameters (optional)
323632176cfdSRui Paulo * [tlv] Mesh ID (MBSS)
323732176cfdSRui Paulo * [tlv] Mesh Conf (MBSS)
323832176cfdSRui Paulo * [tlv] application data (optional)
323932176cfdSRui Paulo * NB: we allocate the max space required for the TIM bitmap.
324032176cfdSRui Paulo * XXX how big is this?
324132176cfdSRui Paulo */
324232176cfdSRui Paulo pktlen = 8 /* time stamp */
324332176cfdSRui Paulo + sizeof(uint16_t) /* beacon interval */
324432176cfdSRui Paulo + sizeof(uint16_t) /* capabilities */
324532176cfdSRui Paulo + 2 + ni->ni_esslen /* ssid */
324632176cfdSRui Paulo + 2 + IEEE80211_RATE_SIZE /* supported rates */
324732176cfdSRui Paulo + 2 + 1 /* DS parameters */
324832176cfdSRui Paulo + 2 + 6 /* CF parameters */
324932176cfdSRui Paulo + 2 + 4 + vap->iv_tim_len /* DTIM/IBSSPARMS */
325032176cfdSRui Paulo + IEEE80211_COUNTRY_MAX_SIZE /* country */
325132176cfdSRui Paulo + 2 + 1 /* power control */
325232176cfdSRui Paulo + sizeof(struct ieee80211_csa_ie) /* CSA */
3253085ff963SMatthew Dillon + sizeof(struct ieee80211_quiet_ie) /* Quiet */
325432176cfdSRui Paulo + 2 + 1 /* ERP */
325532176cfdSRui Paulo + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
325632176cfdSRui Paulo + (vap->iv_caps & IEEE80211_C_WPA ? /* WPA 1+2 */
325732176cfdSRui Paulo 2*sizeof(struct ieee80211_ie_wpa) : 0)
325832176cfdSRui Paulo /* XXX conditional? */
325932176cfdSRui Paulo + 4+2*sizeof(struct ieee80211_ie_htcap)/* HT caps */
326032176cfdSRui Paulo + 4+2*sizeof(struct ieee80211_ie_htinfo)/* HT info */
326132176cfdSRui Paulo + (vap->iv_caps & IEEE80211_C_WME ? /* WME */
326232176cfdSRui Paulo sizeof(struct ieee80211_wme_param) : 0)
326332176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
326432176cfdSRui Paulo + sizeof(struct ieee80211_ath_ie) /* ATH */
326532176cfdSRui Paulo #endif
326632176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA
326732176cfdSRui Paulo + (vap->iv_caps & IEEE80211_C_TDMA ? /* TDMA */
326832176cfdSRui Paulo sizeof(struct ieee80211_tdma_param) : 0)
326932176cfdSRui Paulo #endif
327032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
327132176cfdSRui Paulo + 2 + ni->ni_meshidlen
327232176cfdSRui Paulo + sizeof(struct ieee80211_meshconf_ie)
327332176cfdSRui Paulo #endif
327432176cfdSRui Paulo + IEEE80211_MAX_APPIE
327532176cfdSRui Paulo ;
327632176cfdSRui Paulo m = ieee80211_getmgtframe(&frm,
327732176cfdSRui Paulo ic->ic_headroom + sizeof(struct ieee80211_frame), pktlen);
327832176cfdSRui Paulo if (m == NULL) {
327932176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
328032176cfdSRui Paulo "%s: cannot get buf; size %u\n", __func__, pktlen);
328132176cfdSRui Paulo vap->iv_stats.is_tx_nobuf++;
328232176cfdSRui Paulo return NULL;
328332176cfdSRui Paulo }
32844f655ef5SMatthew Dillon ieee80211_beacon_construct(m, frm, ni);
3285841ab66cSSepherosa Ziehau
3286b5523eacSSascha Wildner M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
3287841ab66cSSepherosa Ziehau KASSERT(m != NULL, ("no space for 802.11 header?"));
3288841ab66cSSepherosa Ziehau wh = mtod(m, struct ieee80211_frame *);
3289841ab66cSSepherosa Ziehau wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3290841ab66cSSepherosa Ziehau IEEE80211_FC0_SUBTYPE_BEACON;
3291841ab66cSSepherosa Ziehau wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3292841ab66cSSepherosa Ziehau *(uint16_t *)wh->i_dur = 0;
3293841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
329432176cfdSRui Paulo IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3295841ab66cSSepherosa Ziehau IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
3296841ab66cSSepherosa Ziehau *(uint16_t *)wh->i_seq = 0;
3297841ab66cSSepherosa Ziehau
3298841ab66cSSepherosa Ziehau return m;
3299841ab66cSSepherosa Ziehau }
3300841ab66cSSepherosa Ziehau
3301841ab66cSSepherosa Ziehau /*
3302841ab66cSSepherosa Ziehau * Update the dynamic parts of a beacon frame based on the current state.
3303841ab66cSSepherosa Ziehau */
3304841ab66cSSepherosa Ziehau int
33054f655ef5SMatthew Dillon ieee80211_beacon_update(struct ieee80211_node *ni, struct mbuf *m, int mcast)
3306841ab66cSSepherosa Ziehau {
330732176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
33084f655ef5SMatthew Dillon struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
330932176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
3310841ab66cSSepherosa Ziehau int len_changed = 0;
3311841ab66cSSepherosa Ziehau uint16_t capinfo;
3312085ff963SMatthew Dillon struct ieee80211_frame *wh;
3313085ff963SMatthew Dillon ieee80211_seq seqno;
3314841ab66cSSepherosa Ziehau
3315085ff963SMatthew Dillon IEEE80211_LOCK(ic);
331632176cfdSRui Paulo /*
331732176cfdSRui Paulo * Handle 11h channel change when we've reached the count.
331832176cfdSRui Paulo * We must recalculate the beacon frame contents to account
331932176cfdSRui Paulo * for the new channel. Note we do this only for the first
332032176cfdSRui Paulo * vap that reaches this point; subsequent vaps just update
332132176cfdSRui Paulo * their beacon state to reflect the recalculated channel.
332232176cfdSRui Paulo */
332332176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_CSA) &&
332432176cfdSRui Paulo vap->iv_csa_count == ic->ic_csa_count) {
332532176cfdSRui Paulo vap->iv_csa_count = 0;
332632176cfdSRui Paulo /*
332732176cfdSRui Paulo * Effect channel change before reconstructing the beacon
332832176cfdSRui Paulo * frame contents as many places reference ni_chan.
332932176cfdSRui Paulo */
333032176cfdSRui Paulo if (ic->ic_csa_newchan != NULL)
333132176cfdSRui Paulo ieee80211_csa_completeswitch(ic);
333232176cfdSRui Paulo /*
333332176cfdSRui Paulo * NB: ieee80211_beacon_construct clears all pending
333432176cfdSRui Paulo * updates in bo_flags so we don't need to explicitly
333532176cfdSRui Paulo * clear IEEE80211_BEACON_CSA.
333632176cfdSRui Paulo */
333732176cfdSRui Paulo ieee80211_beacon_construct(m,
33384f655ef5SMatthew Dillon mtod(m, uint8_t*) + sizeof(struct ieee80211_frame), ni);
333932176cfdSRui Paulo
334032176cfdSRui Paulo /* XXX do WME aggressive mode processing? */
3341085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
334232176cfdSRui Paulo return 1; /* just assume length changed */
334332176cfdSRui Paulo }
3344841ab66cSSepherosa Ziehau
3345085ff963SMatthew Dillon wh = mtod(m, struct ieee80211_frame *);
3346085ff963SMatthew Dillon seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID]++;
3347085ff963SMatthew Dillon *(uint16_t *)&wh->i_seq[0] =
3348085ff963SMatthew Dillon htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3349085ff963SMatthew Dillon M_SEQNO_SET(m, seqno);
3350085ff963SMatthew Dillon
3351841ab66cSSepherosa Ziehau /* XXX faster to recalculate entirely or just changes? */
335232176cfdSRui Paulo capinfo = ieee80211_getcapinfo(vap, ni->ni_chan);
3353841ab66cSSepherosa Ziehau *bo->bo_caps = htole16(capinfo);
3354841ab66cSSepherosa Ziehau
335532176cfdSRui Paulo if (vap->iv_flags & IEEE80211_F_WME) {
3356841ab66cSSepherosa Ziehau struct ieee80211_wme_state *wme = &ic->ic_wme;
3357841ab66cSSepherosa Ziehau
3358841ab66cSSepherosa Ziehau /*
33594f655ef5SMatthew Dillon * Check for aggressive mode change. When there is
3360841ab66cSSepherosa Ziehau * significant high priority traffic in the BSS
3361841ab66cSSepherosa Ziehau * throttle back BE traffic by using conservative
33624f655ef5SMatthew Dillon * parameters. Otherwise BE uses aggressive params
3363841ab66cSSepherosa Ziehau * to optimize performance of legacy/non-QoS traffic.
3364841ab66cSSepherosa Ziehau */
3365841ab66cSSepherosa Ziehau if (wme->wme_flags & WME_F_AGGRMODE) {
3366841ab66cSSepherosa Ziehau if (wme->wme_hipri_traffic >
3367841ab66cSSepherosa Ziehau wme->wme_hipri_switch_thresh) {
336832176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3369841ab66cSSepherosa Ziehau "%s: traffic %u, disable aggressive mode\n",
3370841ab66cSSepherosa Ziehau __func__, wme->wme_hipri_traffic);
3371841ab66cSSepherosa Ziehau wme->wme_flags &= ~WME_F_AGGRMODE;
337232176cfdSRui Paulo ieee80211_wme_updateparams_locked(vap);
3373841ab66cSSepherosa Ziehau wme->wme_hipri_traffic =
3374841ab66cSSepherosa Ziehau wme->wme_hipri_switch_hysteresis;
3375841ab66cSSepherosa Ziehau } else
3376841ab66cSSepherosa Ziehau wme->wme_hipri_traffic = 0;
3377841ab66cSSepherosa Ziehau } else {
3378841ab66cSSepherosa Ziehau if (wme->wme_hipri_traffic <=
3379841ab66cSSepherosa Ziehau wme->wme_hipri_switch_thresh) {
338032176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
3381841ab66cSSepherosa Ziehau "%s: traffic %u, enable aggressive mode\n",
3382841ab66cSSepherosa Ziehau __func__, wme->wme_hipri_traffic);
3383841ab66cSSepherosa Ziehau wme->wme_flags |= WME_F_AGGRMODE;
338432176cfdSRui Paulo ieee80211_wme_updateparams_locked(vap);
3385841ab66cSSepherosa Ziehau wme->wme_hipri_traffic = 0;
3386841ab66cSSepherosa Ziehau } else
3387841ab66cSSepherosa Ziehau wme->wme_hipri_traffic =
3388841ab66cSSepherosa Ziehau wme->wme_hipri_switch_hysteresis;
3389841ab66cSSepherosa Ziehau }
339032176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_WME)) {
3391841ab66cSSepherosa Ziehau (void) ieee80211_add_wme_param(bo->bo_wme, wme);
339232176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_WME);
3393841ab66cSSepherosa Ziehau }
3394841ab66cSSepherosa Ziehau }
3395841ab66cSSepherosa Ziehau
339632176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_HTINFO)) {
339732176cfdSRui Paulo ieee80211_ht_update_beacon(vap, bo);
339832176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_HTINFO);
339932176cfdSRui Paulo }
340032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_TDMA
340132176cfdSRui Paulo if (vap->iv_caps & IEEE80211_C_TDMA) {
340232176cfdSRui Paulo /*
340332176cfdSRui Paulo * NB: the beacon is potentially updated every TBTT.
340432176cfdSRui Paulo */
340532176cfdSRui Paulo ieee80211_tdma_update_beacon(vap, bo);
340632176cfdSRui Paulo }
340732176cfdSRui Paulo #endif
340832176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_MESH
340932176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_MBSS)
341032176cfdSRui Paulo ieee80211_mesh_update_beacon(vap, bo);
341132176cfdSRui Paulo #endif
341232176cfdSRui Paulo
341332176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
341432176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_MBSS) { /* NB: no IBSS support*/
3415841ab66cSSepherosa Ziehau struct ieee80211_tim_ie *tie =
3416841ab66cSSepherosa Ziehau (struct ieee80211_tim_ie *) bo->bo_tim;
341732176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_TIM)) {
3418841ab66cSSepherosa Ziehau u_int timlen, timoff, i;
3419841ab66cSSepherosa Ziehau /*
3420841ab66cSSepherosa Ziehau * ATIM/DTIM needs updating. If it fits in the
3421841ab66cSSepherosa Ziehau * current space allocated then just copy in the
3422841ab66cSSepherosa Ziehau * new bits. Otherwise we need to move any trailing
3423841ab66cSSepherosa Ziehau * data to make room. Note that we know there is
3424841ab66cSSepherosa Ziehau * contiguous space because ieee80211_beacon_allocate
3425841ab66cSSepherosa Ziehau * insures there is space in the mbuf to write a
342632176cfdSRui Paulo * maximal-size virtual bitmap (based on iv_max_aid).
3427841ab66cSSepherosa Ziehau */
3428841ab66cSSepherosa Ziehau /*
3429841ab66cSSepherosa Ziehau * Calculate the bitmap size and offset, copy any
3430841ab66cSSepherosa Ziehau * trailer out of the way, and then copy in the
3431841ab66cSSepherosa Ziehau * new bitmap and update the information element.
3432841ab66cSSepherosa Ziehau * Note that the tim bitmap must contain at least
3433841ab66cSSepherosa Ziehau * one byte and any offset must be even.
3434841ab66cSSepherosa Ziehau */
343532176cfdSRui Paulo if (vap->iv_ps_pending != 0) {
3436841ab66cSSepherosa Ziehau timoff = 128; /* impossibly large */
343732176cfdSRui Paulo for (i = 0; i < vap->iv_tim_len; i++)
343832176cfdSRui Paulo if (vap->iv_tim_bitmap[i]) {
3439841ab66cSSepherosa Ziehau timoff = i &~ 1;
3440841ab66cSSepherosa Ziehau break;
3441841ab66cSSepherosa Ziehau }
3442841ab66cSSepherosa Ziehau KASSERT(timoff != 128, ("tim bitmap empty!"));
344332176cfdSRui Paulo for (i = vap->iv_tim_len-1; i >= timoff; i--)
344432176cfdSRui Paulo if (vap->iv_tim_bitmap[i])
3445841ab66cSSepherosa Ziehau break;
3446841ab66cSSepherosa Ziehau timlen = 1 + (i - timoff);
3447841ab66cSSepherosa Ziehau } else {
3448841ab66cSSepherosa Ziehau timoff = 0;
3449841ab66cSSepherosa Ziehau timlen = 1;
3450841ab66cSSepherosa Ziehau }
3451841ab66cSSepherosa Ziehau if (timlen != bo->bo_tim_len) {
3452841ab66cSSepherosa Ziehau /* copy up/down trailer */
3453841ab66cSSepherosa Ziehau int adjust = tie->tim_bitmap+timlen
345432176cfdSRui Paulo - bo->bo_tim_trailer;
3455afd2da4dSMatthew Dillon bcopy(bo->bo_tim_trailer,
345632176cfdSRui Paulo bo->bo_tim_trailer+adjust,
345732176cfdSRui Paulo bo->bo_tim_trailer_len);
345832176cfdSRui Paulo bo->bo_tim_trailer += adjust;
3459841ab66cSSepherosa Ziehau bo->bo_erp += adjust;
346032176cfdSRui Paulo bo->bo_htinfo += adjust;
3461085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_SUPERG
346232176cfdSRui Paulo bo->bo_ath += adjust;
346332176cfdSRui Paulo #endif
3464085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_TDMA
346532176cfdSRui Paulo bo->bo_tdma += adjust;
346632176cfdSRui Paulo #endif
3467085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
346832176cfdSRui Paulo bo->bo_meshconf += adjust;
346932176cfdSRui Paulo #endif
347032176cfdSRui Paulo bo->bo_appie += adjust;
347132176cfdSRui Paulo bo->bo_wme += adjust;
347232176cfdSRui Paulo bo->bo_csa += adjust;
3473085ff963SMatthew Dillon bo->bo_quiet += adjust;
3474841ab66cSSepherosa Ziehau bo->bo_tim_len = timlen;
3475841ab66cSSepherosa Ziehau
3476841ab66cSSepherosa Ziehau /* update information element */
3477841ab66cSSepherosa Ziehau tie->tim_len = 3 + timlen;
3478841ab66cSSepherosa Ziehau tie->tim_bitctl = timoff;
3479841ab66cSSepherosa Ziehau len_changed = 1;
3480841ab66cSSepherosa Ziehau }
348132176cfdSRui Paulo memcpy(tie->tim_bitmap, vap->iv_tim_bitmap + timoff,
3482841ab66cSSepherosa Ziehau bo->bo_tim_len);
3483841ab66cSSepherosa Ziehau
348432176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_TIM);
3485841ab66cSSepherosa Ziehau
348632176cfdSRui Paulo IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
3487841ab66cSSepherosa Ziehau "%s: TIM updated, pending %u, off %u, len %u\n",
348832176cfdSRui Paulo __func__, vap->iv_ps_pending, timoff, timlen);
3489841ab66cSSepherosa Ziehau }
3490841ab66cSSepherosa Ziehau /* count down DTIM period */
3491841ab66cSSepherosa Ziehau if (tie->tim_count == 0)
3492841ab66cSSepherosa Ziehau tie->tim_count = tie->tim_period - 1;
3493841ab66cSSepherosa Ziehau else
3494841ab66cSSepherosa Ziehau tie->tim_count--;
3495841ab66cSSepherosa Ziehau /* update state for buffered multicast frames on DTIM */
3496841ab66cSSepherosa Ziehau if (mcast && tie->tim_count == 0)
3497841ab66cSSepherosa Ziehau tie->tim_bitctl |= 1;
3498841ab66cSSepherosa Ziehau else
3499841ab66cSSepherosa Ziehau tie->tim_bitctl &= ~1;
350032176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_CSA)) {
350132176cfdSRui Paulo struct ieee80211_csa_ie *csa =
350232176cfdSRui Paulo (struct ieee80211_csa_ie *) bo->bo_csa;
350332176cfdSRui Paulo
350432176cfdSRui Paulo /*
350532176cfdSRui Paulo * Insert or update CSA ie. If we're just starting
350632176cfdSRui Paulo * to count down to the channel switch then we need
350732176cfdSRui Paulo * to insert the CSA ie. Otherwise we just need to
350832176cfdSRui Paulo * drop the count. The actual change happens above
350932176cfdSRui Paulo * when the vap's count reaches the target count.
351032176cfdSRui Paulo */
351132176cfdSRui Paulo if (vap->iv_csa_count == 0) {
351232176cfdSRui Paulo memmove(&csa[1], csa, bo->bo_csa_trailer_len);
351332176cfdSRui Paulo bo->bo_erp += sizeof(*csa);
351432176cfdSRui Paulo bo->bo_htinfo += sizeof(*csa);
351532176cfdSRui Paulo bo->bo_wme += sizeof(*csa);
3516085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_SUPERG
351732176cfdSRui Paulo bo->bo_ath += sizeof(*csa);
351832176cfdSRui Paulo #endif
3519085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_TDMA
352032176cfdSRui Paulo bo->bo_tdma += sizeof(*csa);
352132176cfdSRui Paulo #endif
3522085ff963SMatthew Dillon #ifdef IEEE80211_SUPPORT_MESH
352332176cfdSRui Paulo bo->bo_meshconf += sizeof(*csa);
352432176cfdSRui Paulo #endif
352532176cfdSRui Paulo bo->bo_appie += sizeof(*csa);
352632176cfdSRui Paulo bo->bo_csa_trailer_len += sizeof(*csa);
3527085ff963SMatthew Dillon bo->bo_quiet += sizeof(*csa);
352832176cfdSRui Paulo bo->bo_tim_trailer_len += sizeof(*csa);
352932176cfdSRui Paulo m->m_len += sizeof(*csa);
353032176cfdSRui Paulo m->m_pkthdr.len += sizeof(*csa);
353132176cfdSRui Paulo
353232176cfdSRui Paulo ieee80211_add_csa(bo->bo_csa, vap);
353332176cfdSRui Paulo } else
353432176cfdSRui Paulo csa->csa_count--;
353532176cfdSRui Paulo vap->iv_csa_count++;
353632176cfdSRui Paulo /* NB: don't clear IEEE80211_BEACON_CSA */
353732176cfdSRui Paulo }
3538085ff963SMatthew Dillon if (IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
3539085ff963SMatthew Dillon (vap->iv_flags_ext & IEEE80211_FEXT_DFS) ){
3540085ff963SMatthew Dillon if (vap->iv_quiet)
3541085ff963SMatthew Dillon ieee80211_add_quiet(bo->bo_quiet, vap);
3542085ff963SMatthew Dillon }
354332176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_ERP)) {
3544841ab66cSSepherosa Ziehau /*
3545841ab66cSSepherosa Ziehau * ERP element needs updating.
3546841ab66cSSepherosa Ziehau */
3547841ab66cSSepherosa Ziehau (void) ieee80211_add_erp(bo->bo_erp, ic);
354832176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_ERP);
3549841ab66cSSepherosa Ziehau }
355032176cfdSRui Paulo #ifdef IEEE80211_SUPPORT_SUPERG
355132176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_ATH)) {
355232176cfdSRui Paulo ieee80211_add_athcaps(bo->bo_ath, ni);
355332176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_ATH);
35546dd1c373SSepherosa Ziehau }
3555841ab66cSSepherosa Ziehau #endif
3556841ab66cSSepherosa Ziehau }
355732176cfdSRui Paulo if (isset(bo->bo_flags, IEEE80211_BEACON_APPIE)) {
355832176cfdSRui Paulo const struct ieee80211_appie *aie = vap->iv_appie_beacon;
355932176cfdSRui Paulo int aielen;
356032176cfdSRui Paulo uint8_t *frm;
3561841ab66cSSepherosa Ziehau
356232176cfdSRui Paulo aielen = 0;
356332176cfdSRui Paulo if (aie != NULL)
356432176cfdSRui Paulo aielen += aie->ie_len;
356532176cfdSRui Paulo if (aielen != bo->bo_appie_len) {
356632176cfdSRui Paulo /* copy up/down trailer */
356732176cfdSRui Paulo int adjust = aielen - bo->bo_appie_len;
3568afd2da4dSMatthew Dillon bcopy(bo->bo_tim_trailer, bo->bo_tim_trailer+adjust,
356932176cfdSRui Paulo bo->bo_tim_trailer_len);
357032176cfdSRui Paulo bo->bo_tim_trailer += adjust;
357132176cfdSRui Paulo bo->bo_appie += adjust;
357232176cfdSRui Paulo bo->bo_appie_len = aielen;
3573841ab66cSSepherosa Ziehau
357432176cfdSRui Paulo len_changed = 1;
3575841ab66cSSepherosa Ziehau }
357632176cfdSRui Paulo frm = bo->bo_appie;
357732176cfdSRui Paulo if (aie != NULL)
357832176cfdSRui Paulo frm = add_appie(frm, aie);
357932176cfdSRui Paulo clrbit(bo->bo_flags, IEEE80211_BEACON_APPIE);
35802d7dda79SSepherosa Ziehau }
3581085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
35822d7dda79SSepherosa Ziehau
358332176cfdSRui Paulo return len_changed;
3584322b19a8SSepherosa Ziehau }
3585085ff963SMatthew Dillon
3586085ff963SMatthew Dillon /*
3587085ff963SMatthew Dillon * Do Ethernet-LLC encapsulation for each payload in a fast frame
3588085ff963SMatthew Dillon * tunnel encapsulation. The frame is assumed to have an Ethernet
3589085ff963SMatthew Dillon * header at the front that must be stripped before prepending the
3590085ff963SMatthew Dillon * LLC followed by the Ethernet header passed in (with an Ethernet
3591085ff963SMatthew Dillon * type that specifies the payload size).
3592085ff963SMatthew Dillon */
3593085ff963SMatthew Dillon struct mbuf *
3594085ff963SMatthew Dillon ieee80211_ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
3595085ff963SMatthew Dillon const struct ether_header *eh)
3596085ff963SMatthew Dillon {
3597085ff963SMatthew Dillon struct llc *llc;
3598085ff963SMatthew Dillon uint16_t payload;
3599085ff963SMatthew Dillon
3600085ff963SMatthew Dillon /* XXX optimize by combining m_adj+M_PREPEND */
3601085ff963SMatthew Dillon m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
3602085ff963SMatthew Dillon llc = mtod(m, struct llc *);
3603085ff963SMatthew Dillon llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
3604085ff963SMatthew Dillon llc->llc_control = LLC_UI;
3605085ff963SMatthew Dillon llc->llc_snap.org_code[0] = 0;
3606085ff963SMatthew Dillon llc->llc_snap.org_code[1] = 0;
3607085ff963SMatthew Dillon llc->llc_snap.org_code[2] = 0;
3608085ff963SMatthew Dillon llc->llc_snap.ether_type = eh->ether_type;
3609085ff963SMatthew Dillon payload = m->m_pkthdr.len; /* NB: w/o Ethernet header */
3610085ff963SMatthew Dillon
3611b5523eacSSascha Wildner M_PREPEND(m, sizeof(struct ether_header), M_NOWAIT);
3612085ff963SMatthew Dillon if (m == NULL) { /* XXX cannot happen */
3613085ff963SMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
3614085ff963SMatthew Dillon "%s: no space for ether_header\n", __func__);
3615085ff963SMatthew Dillon vap->iv_stats.is_tx_nobuf++;
3616085ff963SMatthew Dillon return NULL;
3617085ff963SMatthew Dillon }
3618085ff963SMatthew Dillon ETHER_HEADER_COPY(mtod(m, void *), eh);
3619085ff963SMatthew Dillon mtod(m, struct ether_header *)->ether_type = htons(payload);
3620085ff963SMatthew Dillon return m;
3621085ff963SMatthew Dillon }
3622085ff963SMatthew Dillon
3623085ff963SMatthew Dillon /*
3624085ff963SMatthew Dillon * Complete an mbuf transmission.
3625085ff963SMatthew Dillon *
3626085ff963SMatthew Dillon * For now, this simply processes a completed frame after the
3627085ff963SMatthew Dillon * driver has completed it's transmission and/or retransmission.
3628085ff963SMatthew Dillon * It assumes the frame is an 802.11 encapsulated frame.
3629085ff963SMatthew Dillon *
3630085ff963SMatthew Dillon * Later on it will grow to become the exit path for a given frame
3631085ff963SMatthew Dillon * from the driver and, depending upon how it's been encapsulated
3632085ff963SMatthew Dillon * and already transmitted, it may end up doing A-MPDU retransmission,
3633085ff963SMatthew Dillon * power save requeuing, etc.
3634085ff963SMatthew Dillon *
3635085ff963SMatthew Dillon * In order for the above to work, the driver entry point to this
3636085ff963SMatthew Dillon * must not hold any driver locks. Thus, the driver needs to delay
3637085ff963SMatthew Dillon * any actual mbuf completion until it can release said locks.
3638085ff963SMatthew Dillon *
3639085ff963SMatthew Dillon * This frees the mbuf and if the mbuf has a node reference,
3640085ff963SMatthew Dillon * the node reference will be freed.
3641085ff963SMatthew Dillon */
3642085ff963SMatthew Dillon void
3643085ff963SMatthew Dillon ieee80211_tx_complete(struct ieee80211_node *ni, struct mbuf *m, int status)
3644085ff963SMatthew Dillon {
3645085ff963SMatthew Dillon
3646085ff963SMatthew Dillon if (ni != NULL) {
36474f655ef5SMatthew Dillon struct ifnet *ifp = ni->ni_vap->iv_ifp;
36484f655ef5SMatthew Dillon
36494f655ef5SMatthew Dillon if (status == 0) {
36504f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3651664776f3SImre Vadász #if defined(__DragonFly__)
3652664776f3SImre Vadász /*
3653664776f3SImre Vadász * On DragonFly, IFCOUNTER_OBYTES and
3654664776f3SImre Vadász * IFCOUNTER_OMCASTS increments are currently done
3655664776f3SImre Vadász * by ifq_dispatch() already.
3656664776f3SImre Vadász */
3657664776f3SImre Vadász #else
3658664776f3SImre Vadász if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
36594f655ef5SMatthew Dillon if (m->m_flags & M_MCAST)
36604f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
3661664776f3SImre Vadász #endif
36624f655ef5SMatthew Dillon } else
36634f655ef5SMatthew Dillon if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3664085ff963SMatthew Dillon if (m->m_flags & M_TXCB)
3665085ff963SMatthew Dillon ieee80211_process_callback(ni, m, status);
3666085ff963SMatthew Dillon ieee80211_free_node(ni);
3667085ff963SMatthew Dillon }
3668085ff963SMatthew Dillon m_freem(m);
3669085ff963SMatthew Dillon }
3670