ieee80211_superg.c revision 191541
1190391Ssam/*-
2190391Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3190391Ssam * All rights reserved.
4190391Ssam *
5190391Ssam * Redistribution and use in source and binary forms, with or without
6190391Ssam * modification, are permitted provided that the following conditions
7190391Ssam * are met:
8190391Ssam * 1. Redistributions of source code must retain the above copyright
9190391Ssam *    notice, this list of conditions and the following disclaimer.
10190391Ssam * 2. Redistributions in binary form must reproduce the above copyright
11190391Ssam *    notice, this list of conditions and the following disclaimer in the
12190391Ssam *    documentation and/or other materials provided with the distribution.
13190391Ssam *
14190391Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15190391Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16190391Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17190391Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18190391Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19190391Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20190391Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21190391Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22190391Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23190391Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24190391Ssam */
25190391Ssam
26190391Ssam#include <sys/cdefs.h>
27190391Ssam__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_superg.c 191541 2009-04-26 21:21:07Z sam $");
28190391Ssam
29190391Ssam#include "opt_wlan.h"
30190391Ssam
31190391Ssam#include <sys/param.h>
32190391Ssam#include <sys/systm.h>
33190391Ssam#include <sys/mbuf.h>
34190391Ssam#include <sys/kernel.h>
35190391Ssam#include <sys/endian.h>
36190391Ssam
37190391Ssam#include <sys/socket.h>
38190391Ssam
39190391Ssam#include <net/bpf.h>
40190391Ssam#include <net/ethernet.h>
41190391Ssam#include <net/if.h>
42190391Ssam#include <net/if_llc.h>
43190391Ssam#include <net/if_media.h>
44190391Ssam
45190391Ssam#include <net80211/ieee80211_var.h>
46190391Ssam#include <net80211/ieee80211_input.h>
47190391Ssam#include <net80211/ieee80211_phy.h>
48190391Ssam#include <net80211/ieee80211_superg.h>
49190391Ssam
50190455Ssam/*
51190455Ssam * Atheros fast-frame encapsulation format.
52190455Ssam * FF max payload:
53190455Ssam * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500:
54190455Ssam *   8   +   4   +  4   +   14  +   8   + 1500 +  6   +   14  +   8   + 1500
55190455Ssam * = 3066
56190455Ssam */
57190455Ssam/* fast frame header is 32-bits */
58190455Ssam#define	ATH_FF_PROTO	0x0000003f	/* protocol */
59190455Ssam#define	ATH_FF_PROTO_S	0
60190455Ssam#define	ATH_FF_FTYPE	0x000000c0	/* frame type */
61190455Ssam#define	ATH_FF_FTYPE_S	6
62190455Ssam#define	ATH_FF_HLEN32	0x00000300	/* optional hdr length */
63190455Ssam#define	ATH_FF_HLEN32_S	8
64190455Ssam#define	ATH_FF_SEQNUM	0x001ffc00	/* sequence number */
65190455Ssam#define	ATH_FF_SEQNUM_S	10
66190455Ssam#define	ATH_FF_OFFSET	0xffe00000	/* offset to 2nd payload */
67190455Ssam#define	ATH_FF_OFFSET_S	21
68190455Ssam
69190455Ssam#define	ATH_FF_MAX_HDR_PAD	4
70190455Ssam#define	ATH_FF_MAX_SEP_PAD	6
71190455Ssam#define	ATH_FF_MAX_HDR		30
72190455Ssam
73190455Ssam#define	ATH_FF_PROTO_L2TUNNEL	0	/* L2 tunnel protocol */
74190455Ssam#define	ATH_FF_ETH_TYPE		0x88bd	/* Ether type for encapsulated frames */
75190455Ssam#define	ATH_FF_SNAP_ORGCODE_0	0x00
76190455Ssam#define	ATH_FF_SNAP_ORGCODE_1	0x03
77190455Ssam#define	ATH_FF_SNAP_ORGCODE_2	0x7f
78190455Ssam
79190579Ssam#define	ATH_FF_TXQMIN	2		/* min txq depth for staging */
80190579Ssam#define	ATH_FF_TXQMAX	50		/* maximum # of queued frames allowed */
81190579Ssam#define	ATH_FF_STAGEMAX	5		/* max waiting period for staged frame*/
82190579Ssam
83190391Ssam#define	ETHER_HEADER_COPY(dst, src) \
84190391Ssam	memcpy(dst, src, sizeof(struct ether_header))
85190391Ssam
86190579Ssam/* XXX public for sysctl hookup */
87190579Ssamint	ieee80211_ffppsmin = 2;		/* pps threshold for ff aggregation */
88190579Ssamint	ieee80211_ffagemax = -1;	/* max time frames held on stage q */
89190579Ssam
90190391Ssamvoid
91190391Ssamieee80211_superg_attach(struct ieee80211com *ic)
92190391Ssam{
93190579Ssam	ieee80211_ffagemax = msecs_to_ticks(150);
94190391Ssam}
95190391Ssam
96190391Ssamvoid
97190391Ssamieee80211_superg_detach(struct ieee80211com *ic)
98190391Ssam{
99190391Ssam}
100190391Ssam
101190391Ssamvoid
102190391Ssamieee80211_superg_vattach(struct ieee80211vap *vap)
103190391Ssam{
104190391Ssam	if (vap->iv_caps & IEEE80211_C_FF)
105190391Ssam		vap->iv_flags |= IEEE80211_F_FF;
106190450Ssam	/* NB: we only implement sta mode */
107190450Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
108190450Ssam	    (vap->iv_caps & IEEE80211_C_TURBOP))
109190391Ssam		vap->iv_flags |= IEEE80211_F_TURBOP;
110190391Ssam}
111190391Ssam
112190391Ssamvoid
113190391Ssamieee80211_superg_vdetach(struct ieee80211vap *vap)
114190391Ssam{
115190391Ssam}
116190391Ssam
117190391Ssam#define	ATH_OUI_BYTES		0x00, 0x03, 0x7f
118190391Ssam/*
119190391Ssam * Add a WME information element to a frame.
120190391Ssam */
121190391Ssamuint8_t *
122190451Ssamieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix)
123190391Ssam{
124190391Ssam	static const struct ieee80211_ath_ie info = {
125190391Ssam		.ath_id		= IEEE80211_ELEMID_VENDOR,
126190391Ssam		.ath_len	= sizeof(struct ieee80211_ath_ie) - 2,
127190391Ssam		.ath_oui	= { ATH_OUI_BYTES },
128190391Ssam		.ath_oui_type	= ATH_OUI_TYPE,
129190391Ssam		.ath_oui_subtype= ATH_OUI_SUBTYPE,
130190391Ssam		.ath_version	= ATH_OUI_VERSION,
131190391Ssam	};
132190391Ssam	struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm;
133190391Ssam
134190391Ssam	memcpy(frm, &info, sizeof(info));
135190391Ssam	ath->ath_capability = caps;
136190451Ssam	if (defkeyix != IEEE80211_KEYIX_NONE) {
137190451Ssam		ath->ath_defkeyix[0] = (defkeyix & 0xff);
138190451Ssam		ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff);
139190451Ssam	} else {
140190451Ssam		ath->ath_defkeyix[0] = 0xff;
141190451Ssam		ath->ath_defkeyix[1] = 0x7f;
142190451Ssam	}
143190391Ssam	return frm + sizeof(info);
144190391Ssam}
145190391Ssam#undef ATH_OUI_BYTES
146190391Ssam
147190451Ssamuint8_t *
148190451Ssamieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss)
149190451Ssam{
150190451Ssam	const struct ieee80211vap *vap = bss->ni_vap;
151190451Ssam
152190451Ssam	return ieee80211_add_ath(frm,
153190451Ssam	    vap->iv_flags & IEEE80211_F_ATHEROS,
154190451Ssam	    ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
155190451Ssam	    bss->ni_authmode != IEEE80211_AUTH_8021X) ?
156190451Ssam	    vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
157190451Ssam}
158190451Ssam
159190391Ssamvoid
160190391Ssamieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie)
161190391Ssam{
162190391Ssam	const struct ieee80211_ath_ie *ath =
163190391Ssam		(const struct ieee80211_ath_ie *) ie;
164190391Ssam
165190391Ssam	ni->ni_ath_flags = ath->ath_capability;
166190391Ssam	ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix);
167190391Ssam}
168190391Ssam
169190391Ssamint
170190391Ssamieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
171190391Ssam	const struct ieee80211_frame *wh)
172190391Ssam{
173190391Ssam	struct ieee80211vap *vap = ni->ni_vap;
174190391Ssam	const struct ieee80211_ath_ie *ath;
175190391Ssam	u_int len = frm[1];
176190391Ssam	int capschanged;
177190391Ssam	uint16_t defkeyix;
178190391Ssam
179190391Ssam	if (len < sizeof(struct ieee80211_ath_ie)-2) {
180190391Ssam		IEEE80211_DISCARD_IE(vap,
181190391Ssam		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
182190391Ssam		    wh, "Atheros", "too short, len %u", len);
183190391Ssam		return -1;
184190391Ssam	}
185190391Ssam	ath = (const struct ieee80211_ath_ie *)frm;
186190391Ssam	capschanged = (ni->ni_ath_flags != ath->ath_capability);
187190391Ssam	defkeyix = LE_READ_2(ath->ath_defkeyix);
188190391Ssam	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
189190391Ssam		ni->ni_ath_flags = ath->ath_capability;
190190391Ssam		ni->ni_ath_defkeyix = defkeyix;
191190391Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
192190391Ssam		    "ath ie change: new caps 0x%x defkeyix 0x%x",
193190391Ssam		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
194190391Ssam	}
195190391Ssam	if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) {
196190391Ssam		uint16_t curflags, newflags;
197190391Ssam
198190391Ssam		/*
199190391Ssam		 * Check for turbo mode switch.  Calculate flags
200190391Ssam		 * for the new mode and effect the switch.
201190391Ssam		 */
202190391Ssam		newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags;
203190391Ssam		/* NB: BOOST is not in ic_flags, so get it from the ie */
204190391Ssam		if (ath->ath_capability & ATHEROS_CAP_BOOST)
205190391Ssam			newflags |= IEEE80211_CHAN_TURBO;
206190391Ssam		else
207190391Ssam			newflags &= ~IEEE80211_CHAN_TURBO;
208190391Ssam		if (newflags != curflags)
209190391Ssam			ieee80211_dturbo_switch(vap, newflags);
210190391Ssam	}
211190391Ssam	return capschanged;
212190391Ssam}
213190391Ssam
214190391Ssam/*
215190391Ssam * Decap the encapsulated frame pair and dispatch the first
216190391Ssam * for delivery.  The second frame is returned for delivery
217190391Ssam * via the normal path.
218190391Ssam */
219190391Ssamstruct mbuf *
220190391Ssamieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m)
221190391Ssam{
222190391Ssam#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
223190391Ssam#define	MS(x,f)	(((x) & f) >> f##_S)
224190391Ssam	struct ieee80211vap *vap = ni->ni_vap;
225190391Ssam	struct llc *llc;
226190391Ssam	uint32_t ath;
227190391Ssam	struct mbuf *n;
228190391Ssam	int framelen;
229190391Ssam
230190391Ssam	/* NB: we assume caller does this check for us */
231190391Ssam	KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF),
232190391Ssam	    ("ff not negotiated"));
233190391Ssam	/*
234190391Ssam	 * Check for fast-frame tunnel encapsulation.
235190391Ssam	 */
236190391Ssam	if (m->m_pkthdr.len < 3*FF_LLC_SIZE)
237190391Ssam		return m;
238190391Ssam	if (m->m_len < FF_LLC_SIZE &&
239190391Ssam	    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
240190391Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
241190391Ssam		    ni->ni_macaddr, "fast-frame",
242190391Ssam		    "%s", "m_pullup(llc) failed");
243190391Ssam		vap->iv_stats.is_rx_tooshort++;
244190391Ssam		return NULL;
245190391Ssam	}
246190391Ssam	llc = (struct llc *)(mtod(m, uint8_t *) +
247190391Ssam	    sizeof(struct ether_header));
248190391Ssam	if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE))
249190391Ssam		return m;
250190391Ssam	m_adj(m, FF_LLC_SIZE);
251190391Ssam	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
252190391Ssam	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
253190391Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
254190391Ssam		    ni->ni_macaddr, "fast-frame",
255190391Ssam		    "unsupport tunnel protocol, header 0x%x", ath);
256190391Ssam		vap->iv_stats.is_ff_badhdr++;
257190391Ssam		m_freem(m);
258190391Ssam		return NULL;
259190391Ssam	}
260190391Ssam	/* NB: skip header and alignment padding */
261190391Ssam	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
262190391Ssam
263190391Ssam	vap->iv_stats.is_ff_decap++;
264190391Ssam
265190391Ssam	/*
266190391Ssam	 * Decap the first frame, bust it apart from the
267190391Ssam	 * second and deliver; then decap the second frame
268190391Ssam	 * and return it to the caller for normal delivery.
269190391Ssam	 */
270190391Ssam	m = ieee80211_decap1(m, &framelen);
271190391Ssam	if (m == NULL) {
272190391Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
273190391Ssam		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
274190391Ssam		vap->iv_stats.is_ff_tooshort++;
275190391Ssam		return NULL;
276190391Ssam	}
277190391Ssam	n = m_split(m, framelen, M_NOWAIT);
278190391Ssam	if (n == NULL) {
279190391Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
280190391Ssam		    ni->ni_macaddr, "fast-frame",
281190391Ssam		    "%s", "unable to split encapsulated frames");
282190391Ssam		vap->iv_stats.is_ff_split++;
283190391Ssam		m_freem(m);			/* NB: must reclaim */
284190391Ssam		return NULL;
285190391Ssam	}
286190391Ssam	/* XXX not right for WDS */
287190391Ssam	vap->iv_deliver_data(vap, ni, m);	/* 1st of pair */
288190391Ssam
289190391Ssam	/*
290190391Ssam	 * Decap second frame.
291190391Ssam	 */
292190391Ssam	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
293190391Ssam	n = ieee80211_decap1(n, &framelen);
294190391Ssam	if (n == NULL) {
295190391Ssam		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
296190391Ssam		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
297190391Ssam		vap->iv_stats.is_ff_tooshort++;
298190391Ssam	}
299190391Ssam	/* XXX verify framelen against mbuf contents */
300190391Ssam	return n;				/* 2nd delivered by caller */
301190391Ssam#undef MS
302190391Ssam#undef FF_LLC_SIZE
303190391Ssam}
304190391Ssam
305190391Ssam/*
306190391Ssam * Do Ethernet-LLC encapsulation for each payload in a fast frame
307190391Ssam * tunnel encapsulation.  The frame is assumed to have an Ethernet
308190391Ssam * header at the front that must be stripped before prepending the
309190391Ssam * LLC followed by the Ethernet header passed in (with an Ethernet
310190391Ssam * type that specifies the payload size).
311190391Ssam */
312190391Ssamstatic struct mbuf *
313190391Ssamff_encap1(struct ieee80211vap *vap, struct mbuf *m,
314190391Ssam	const struct ether_header *eh)
315190391Ssam{
316190391Ssam	struct llc *llc;
317190391Ssam	uint16_t payload;
318190391Ssam
319190391Ssam	/* XXX optimize by combining m_adj+M_PREPEND */
320190391Ssam	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
321190391Ssam	llc = mtod(m, struct llc *);
322190391Ssam	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
323190391Ssam	llc->llc_control = LLC_UI;
324190391Ssam	llc->llc_snap.org_code[0] = 0;
325190391Ssam	llc->llc_snap.org_code[1] = 0;
326190391Ssam	llc->llc_snap.org_code[2] = 0;
327190391Ssam	llc->llc_snap.ether_type = eh->ether_type;
328190391Ssam	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
329190391Ssam
330190391Ssam	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
331190391Ssam	if (m == NULL) {		/* XXX cannot happen */
332190391Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
333190391Ssam			"%s: no space for ether_header\n", __func__);
334190391Ssam		vap->iv_stats.is_tx_nobuf++;
335190391Ssam		return NULL;
336190391Ssam	}
337190391Ssam	ETHER_HEADER_COPY(mtod(m, void *), eh);
338190391Ssam	mtod(m, struct ether_header *)->ether_type = htons(payload);
339190391Ssam	return m;
340190391Ssam}
341190391Ssam
342190391Ssam/*
343190391Ssam * Fast frame encapsulation.  There must be two packets
344190391Ssam * chained with m_nextpkt.  We do header adjustment for
345190391Ssam * each, add the tunnel encapsulation, and then concatenate
346190391Ssam * the mbuf chains to form a single frame for transmission.
347190391Ssam */
348190391Ssamstruct mbuf *
349190391Ssamieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
350190391Ssam	struct ieee80211_key *key)
351190391Ssam{
352190391Ssam	struct mbuf *m2;
353190391Ssam	struct ether_header eh1, eh2;
354190391Ssam	struct llc *llc;
355190391Ssam	struct mbuf *m;
356190391Ssam	int pad;
357190391Ssam
358190391Ssam	m2 = m1->m_nextpkt;
359190391Ssam	if (m2 == NULL) {
360190391Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
361190391Ssam		    "%s: only one frame\n", __func__);
362190391Ssam		goto bad;
363190391Ssam	}
364190391Ssam	m1->m_nextpkt = NULL;
365190391Ssam	/*
366190579Ssam	 * Include fast frame headers in adjusting header layout.
367190391Ssam	 */
368190579Ssam	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
369190579Ssam	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
370190391Ssam	m1 = ieee80211_mbuf_adjust(vap,
371190391Ssam		hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 +
372190391Ssam		    sizeof(struct ether_header),
373190391Ssam		key, m1);
374190391Ssam	if (m1 == NULL) {
375190391Ssam		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
376190391Ssam		m_freem(m2);
377190391Ssam		goto bad;
378190391Ssam	}
379190391Ssam
380190391Ssam	/*
381190391Ssam	 * Copy second frame's Ethernet header out of line
382190391Ssam	 * and adjust for encapsulation headers.  Note that
383190391Ssam	 * we make room for padding in case there isn't room
384190391Ssam	 * at the end of first frame.
385190391Ssam	 */
386190391Ssam	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
387190391Ssam	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
388190391Ssam	m2 = ieee80211_mbuf_adjust(vap,
389190391Ssam		ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header),
390190391Ssam		NULL, m2);
391190391Ssam	if (m2 == NULL) {
392190391Ssam		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
393190391Ssam		goto bad;
394190391Ssam	}
395190391Ssam
396190391Ssam	/*
397190391Ssam	 * Now do tunnel encapsulation.  First, each
398190391Ssam	 * frame gets a standard encapsulation.
399190391Ssam	 */
400190391Ssam	m1 = ff_encap1(vap, m1, &eh1);
401190391Ssam	if (m1 == NULL)
402190391Ssam		goto bad;
403190391Ssam	m2 = ff_encap1(vap, m2, &eh2);
404190391Ssam	if (m2 == NULL)
405190391Ssam		goto bad;
406190391Ssam
407190391Ssam	/*
408190391Ssam	 * Pad leading frame to a 4-byte boundary.  If there
409190391Ssam	 * is space at the end of the first frame, put it
410190391Ssam	 * there; otherwise prepend to the front of the second
411190391Ssam	 * frame.  We know doing the second will always work
412190391Ssam	 * because we reserve space above.  We prefer appending
413190391Ssam	 * as this typically has better DMA alignment properties.
414190391Ssam	 */
415190391Ssam	for (m = m1; m->m_next != NULL; m = m->m_next)
416190391Ssam		;
417190391Ssam	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
418190391Ssam	if (pad) {
419190391Ssam		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
420190391Ssam			m2->m_data -= pad;
421190391Ssam			m2->m_len += pad;
422190391Ssam			m2->m_pkthdr.len += pad;
423190391Ssam		} else {				/* append to first */
424190391Ssam			m->m_len += pad;
425190391Ssam			m1->m_pkthdr.len += pad;
426190391Ssam		}
427190391Ssam	}
428190391Ssam
429190391Ssam	/*
430190391Ssam	 * Now, stick 'em together and prepend the tunnel headers;
431190391Ssam	 * first the Atheros tunnel header (all zero for now) and
432190391Ssam	 * then a special fast frame LLC.
433190391Ssam	 *
434190391Ssam	 * XXX optimize by prepending together
435190391Ssam	 */
436190391Ssam	m->m_next = m2;			/* NB: last mbuf from above */
437190391Ssam	m1->m_pkthdr.len += m2->m_pkthdr.len;
438190391Ssam	M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT);
439190391Ssam	if (m1 == NULL) {		/* XXX cannot happen */
440190391Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
441190391Ssam		    "%s: no space for tunnel header\n", __func__);
442190391Ssam		vap->iv_stats.is_tx_nobuf++;
443190391Ssam		return NULL;
444190391Ssam	}
445190391Ssam	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
446190391Ssam
447190391Ssam	M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT);
448190391Ssam	if (m1 == NULL) {		/* XXX cannot happen */
449190391Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
450190391Ssam		    "%s: no space for llc header\n", __func__);
451190391Ssam		vap->iv_stats.is_tx_nobuf++;
452190391Ssam		return NULL;
453190391Ssam	}
454190391Ssam	llc = mtod(m1, struct llc *);
455190391Ssam	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
456190391Ssam	llc->llc_control = LLC_UI;
457190391Ssam	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
458190391Ssam	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
459190391Ssam	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
460190391Ssam	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
461190391Ssam
462190391Ssam	vap->iv_stats.is_ff_encap++;
463190391Ssam
464190391Ssam	return m1;
465190391Ssambad:
466190391Ssam	if (m1 != NULL)
467190391Ssam		m_freem(m1);
468190391Ssam	if (m2 != NULL)
469190391Ssam		m_freem(m2);
470190391Ssam	return NULL;
471190391Ssam}
472190391Ssam
473190579Ssamstatic void
474190579Ssamff_transmit(struct ieee80211_node *ni, struct mbuf *m)
475190579Ssam{
476190579Ssam	struct ieee80211vap *vap = ni->ni_vap;
477190579Ssam	int error;
478190579Ssam
479190579Ssam	/* encap and xmit */
480190579Ssam	m = ieee80211_encap(vap, ni, m);
481190579Ssam	if (m != NULL) {
482190579Ssam		struct ifnet *ifp = vap->iv_ifp;
483190579Ssam		struct ifnet *parent = ni->ni_ic->ic_ifp;
484190579Ssam
485191541Ssam		if (bpf_peers_present(vap->iv_rawbpf))
486191541Ssam			bpf_mtap(vap->iv_rawbpf, m);
487191541Ssam
488190579Ssam		error = parent->if_transmit(parent, m);
489190579Ssam		if (error != 0) {
490190579Ssam			/* NB: IFQ_HANDOFF reclaims mbuf */
491190579Ssam			ieee80211_free_node(ni);
492190579Ssam		} else {
493190579Ssam			ifp->if_opackets++;
494190579Ssam		}
495190579Ssam	} else
496190579Ssam		ieee80211_free_node(ni);
497190579Ssam}
498190579Ssam
499190391Ssam/*
500190579Ssam * Flush frames to device; note we re-use the linked list
501190579Ssam * the frames were stored on and use the sentinel (unchanged)
502190579Ssam * which may be non-NULL.
503190579Ssam */
504190579Ssamstatic void
505190579Ssamff_flush(struct mbuf *head, struct mbuf *last)
506190579Ssam{
507190579Ssam	struct mbuf *m, *next;
508190579Ssam	struct ieee80211_node *ni;
509190579Ssam	struct ieee80211vap *vap;
510190579Ssam
511190579Ssam	for (m = head; m != last; m = next) {
512190579Ssam		next = m->m_nextpkt;
513190579Ssam		m->m_nextpkt = NULL;
514190579Ssam
515190579Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
516190579Ssam		vap = ni->ni_vap;
517190579Ssam
518190579Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
519190579Ssam		    "%s: flush frame, age %u", __func__, M_AGE_GET(m));
520190579Ssam		vap->iv_stats.is_ff_flush++;
521190579Ssam
522190579Ssam		ff_transmit(ni, m);
523190579Ssam	}
524190579Ssam}
525190579Ssam
526190579Ssam/*
527190579Ssam * Age frames on the staging queue.
528190579Ssam */
529190579Ssamvoid
530190579Ssamieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq, int quanta)
531190579Ssam{
532190579Ssam	struct mbuf *m, *head;
533190579Ssam	struct ieee80211_node *ni;
534190579Ssam	struct ieee80211_tx_ampdu *tap;
535190579Ssam
536190579Ssam	KASSERT(sq->head != NULL, ("stageq empty"));
537190579Ssam
538190579Ssam	IEEE80211_LOCK(ic);
539190579Ssam	head = sq->head;
540190579Ssam	while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) {
541190579Ssam		/* clear tap ref to frame */
542190579Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
543190579Ssam		tap = &ni->ni_tx_ampdu[M_WME_GETAC(m)];
544190579Ssam		KASSERT(tap->txa_private == m, ("staging queue empty"));
545190579Ssam		tap->txa_private = NULL;
546190579Ssam
547190579Ssam		sq->head = m->m_nextpkt;
548190579Ssam		sq->depth--;
549190579Ssam		ic->ic_stageqdepth--;
550190579Ssam	}
551190579Ssam	if (m == NULL)
552190579Ssam		sq->tail = NULL;
553190579Ssam	else
554190579Ssam		M_AGE_SUB(m, quanta);
555190579Ssam	IEEE80211_UNLOCK(ic);
556190579Ssam
557190579Ssam	ff_flush(head, m);
558190579Ssam}
559190579Ssam
560190579Ssamstatic void
561190579Ssamstageq_add(struct ieee80211_stageq *sq, struct mbuf *m)
562190579Ssam{
563190579Ssam	int age = ieee80211_ffagemax;
564190579Ssam	if (sq->tail != NULL) {
565190579Ssam		sq->tail->m_nextpkt = m;
566190579Ssam		age -= M_AGE_GET(sq->head);
567190579Ssam	} else
568190579Ssam		sq->head = m;
569190579Ssam	KASSERT(age >= 0, ("age %d", age));
570190579Ssam	M_AGE_SET(m, age);
571190579Ssam	m->m_nextpkt = NULL;
572190579Ssam	sq->tail = m;
573190579Ssam	sq->depth++;
574190579Ssam}
575190579Ssam
576190579Ssamstatic void
577190579Ssamstageq_remove(struct ieee80211_stageq *sq, struct mbuf *mstaged)
578190579Ssam{
579190579Ssam	struct mbuf *m, *mprev;
580190579Ssam
581190579Ssam	mprev = NULL;
582190579Ssam	for (m = sq->head; m != NULL; m = m->m_nextpkt) {
583190579Ssam		if (m == mstaged) {
584190579Ssam			if (mprev == NULL)
585190579Ssam				sq->head = m->m_nextpkt;
586190579Ssam			else
587190579Ssam				mprev->m_nextpkt = m->m_nextpkt;
588190579Ssam			if (sq->tail == m)
589190579Ssam				sq->tail = mprev;
590190579Ssam			sq->depth--;
591190579Ssam			return;
592190579Ssam		}
593190579Ssam		mprev = m;
594190579Ssam	}
595190579Ssam	printf("%s: packet not found\n", __func__);
596190579Ssam}
597190579Ssam
598190579Ssamstatic uint32_t
599190579Ssamff_approx_txtime(struct ieee80211_node *ni,
600190579Ssam	const struct mbuf *m1, const struct mbuf *m2)
601190579Ssam{
602190579Ssam	struct ieee80211com *ic = ni->ni_ic;
603190579Ssam	struct ieee80211vap *vap = ni->ni_vap;
604190579Ssam	uint32_t framelen;
605190579Ssam
606190579Ssam	/*
607190579Ssam	 * Approximate the frame length to be transmitted. A swag to add
608190579Ssam	 * the following maximal values to the skb payload:
609190579Ssam	 *   - 32: 802.11 encap + CRC
610190579Ssam	 *   - 24: encryption overhead (if wep bit)
611190579Ssam	 *   - 4 + 6: fast-frame header and padding
612190579Ssam	 *   - 16: 2 LLC FF tunnel headers
613190579Ssam	 *   - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd)
614190579Ssam	 */
615190579Ssam	framelen = m1->m_pkthdr.len + 32 +
616190579Ssam	    ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR;
617190579Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY)
618190579Ssam		framelen += 24;
619190579Ssam	if (m2 != NULL)
620190579Ssam		framelen += m2->m_pkthdr.len;
621190579Ssam	return ieee80211_compute_duration(ic->ic_rt, framelen, ni->ni_txrate, 0);
622190579Ssam}
623190579Ssam
624190579Ssam/*
625190579Ssam * Check if the supplied frame can be partnered with an existing
626190579Ssam * or pending frame.  Return a reference to any frame that should be
627190579Ssam * sent on return; otherwise return NULL.
628190579Ssam */
629190579Ssamstruct mbuf *
630190579Ssamieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m)
631190579Ssam{
632190579Ssam	struct ieee80211vap *vap = ni->ni_vap;
633190579Ssam	struct ieee80211com *ic = ni->ni_ic;
634190579Ssam	const int pri = M_WME_GETAC(m);
635190579Ssam	struct ieee80211_stageq *sq;
636190579Ssam	struct ieee80211_tx_ampdu *tap;
637190579Ssam	struct mbuf *mstaged;
638190579Ssam	uint32_t txtime, limit;
639190579Ssam
640190579Ssam	/*
641190579Ssam	 * Check if the supplied frame can be aggregated.
642190579Ssam	 *
643190579Ssam	 * NB: we allow EAPOL frames to be aggregated with other ucast traffic.
644190579Ssam	 *     Do 802.1x EAPOL frames proceed in the clear? Then they couldn't
645190579Ssam	 *     be aggregated with other types of frames when encryption is on?
646190579Ssam	 */
647190579Ssam	IEEE80211_LOCK(ic);
648190579Ssam	tap = &ni->ni_tx_ampdu[pri];
649190579Ssam	mstaged = tap->txa_private;		/* NB: we reuse AMPDU state */
650190579Ssam	ieee80211_txampdu_count_packet(tap);
651190579Ssam
652190579Ssam	/*
653190579Ssam	 * When not in station mode never aggregate a multicast
654190579Ssam	 * frame; this insures, for example, that a combined frame
655190579Ssam	 * does not require multiple encryption keys.
656190579Ssam	 */
657190579Ssam	if (vap->iv_opmode != IEEE80211_M_STA &&
658190579Ssam	    ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) {
659190579Ssam		/* XXX flush staged frame? */
660190579Ssam		IEEE80211_UNLOCK(ic);
661190579Ssam		return m;
662190579Ssam	}
663190579Ssam	/*
664190579Ssam	 * If there is no frame to combine with and the pps is
665190579Ssam	 * too low; then do not attempt to aggregate this frame.
666190579Ssam	 */
667190579Ssam	if (mstaged == NULL &&
668190579Ssam	    ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) {
669190579Ssam		IEEE80211_UNLOCK(ic);
670190579Ssam		return m;
671190579Ssam	}
672190579Ssam	sq = &ic->ic_ff_stageq[pri];
673190579Ssam	/*
674190579Ssam	 * Check the txop limit to insure the aggregate fits.
675190579Ssam	 */
676190579Ssam	limit = IEEE80211_TXOP_TO_US(
677190579Ssam		ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit);
678190579Ssam	if (limit != 0 &&
679190579Ssam	    (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) {
680190579Ssam		/*
681190579Ssam		 * Aggregate too long, return to the caller for direct
682190579Ssam		 * transmission.  In addition, flush any pending frame
683190579Ssam		 * before sending this one.
684190579Ssam		 */
685190579Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
686190579Ssam		    "%s: txtime %u exceeds txop limit %u\n",
687190579Ssam		    __func__, txtime, limit);
688190579Ssam
689190579Ssam		tap->txa_private = NULL;
690190579Ssam		if (mstaged != NULL)
691190579Ssam			stageq_remove(sq, mstaged);
692190579Ssam		IEEE80211_UNLOCK(ic);
693190579Ssam
694190579Ssam		if (mstaged != NULL) {
695190579Ssam			IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
696190579Ssam			    "%s: flush staged frame", __func__);
697190579Ssam			/* encap and xmit */
698190579Ssam			ff_transmit(ni, mstaged);
699190579Ssam		}
700190579Ssam		return m;		/* NB: original frame */
701190579Ssam	}
702190579Ssam	/*
703190579Ssam	 * An aggregation candidate.  If there's a frame to partner
704190579Ssam	 * with then combine and return for processing.  Otherwise
705190579Ssam	 * save this frame and wait for a partner to show up (or
706190579Ssam	 * the frame to be flushed).  Note that staged frames also
707190579Ssam	 * hold their node reference.
708190579Ssam	 */
709190579Ssam	if (mstaged != NULL) {
710190579Ssam		tap->txa_private = NULL;
711190579Ssam		stageq_remove(sq, mstaged);
712190579Ssam		IEEE80211_UNLOCK(ic);
713190579Ssam
714190579Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
715190579Ssam		    "%s: aggregate fast-frame", __func__);
716190579Ssam		/*
717190579Ssam		 * Release the node reference; we only need
718190579Ssam		 * the one already in mstaged.
719190579Ssam		 */
720190579Ssam		KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni,
721190579Ssam		    ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni));
722190579Ssam		ieee80211_free_node(ni);
723190579Ssam
724190579Ssam		m->m_nextpkt = NULL;
725190579Ssam		mstaged->m_nextpkt = m;
726190579Ssam		mstaged->m_flags |= M_FF; /* NB: mark for encap work */
727190579Ssam	} else {
728190579Ssam		m->m_pkthdr.rcvif = (void *)ni;	/* NB: hold node reference */
729190579Ssam
730190579Ssam		KASSERT(tap->txa_private == NULL,
731190579Ssam		    ("txa_private %p", tap->txa_private));
732190579Ssam		tap->txa_private = m;
733190579Ssam
734190579Ssam		stageq_add(sq, m);
735190579Ssam		ic->ic_stageqdepth++;
736190579Ssam		IEEE80211_UNLOCK(ic);
737190579Ssam
738190579Ssam		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
739190579Ssam		    "%s: stage frame, %u queued", __func__, sq->depth);
740190579Ssam		/* NB: mstaged is NULL */
741190579Ssam	}
742190579Ssam	return mstaged;
743190579Ssam}
744190579Ssam
745190579Ssamvoid
746190579Ssamieee80211_ff_node_init(struct ieee80211_node *ni)
747190579Ssam{
748190579Ssam	/*
749190579Ssam	 * Clean FF state on re-associate.  This handles the case
750190579Ssam	 * where a station leaves w/o notifying us and then returns
751190579Ssam	 * before node is reaped for inactivity.
752190579Ssam	 */
753190579Ssam	ieee80211_ff_node_cleanup(ni);
754190579Ssam}
755190579Ssam
756190579Ssamvoid
757190579Ssamieee80211_ff_node_cleanup(struct ieee80211_node *ni)
758190579Ssam{
759190579Ssam	struct ieee80211com *ic = ni->ni_ic;
760190579Ssam	struct ieee80211_tx_ampdu *tap;
761190579Ssam	struct mbuf *m, *head;
762190579Ssam	int ac;
763190579Ssam
764190579Ssam	IEEE80211_LOCK(ic);
765190579Ssam	head = NULL;
766190579Ssam	for (ac = 0; ac < WME_NUM_AC; ac++) {
767190579Ssam		tap = &ni->ni_tx_ampdu[ac];
768190579Ssam		m = tap->txa_private;
769190579Ssam		if (m != NULL) {
770190579Ssam			tap->txa_private = NULL;
771190579Ssam			stageq_remove(&ic->ic_ff_stageq[ac], m);
772190579Ssam			m->m_nextpkt = head;
773190579Ssam			head = m;
774190579Ssam		}
775190579Ssam	}
776190579Ssam	IEEE80211_UNLOCK(ic);
777190579Ssam
778190579Ssam	for (m = head; m != NULL; m = m->m_nextpkt) {
779190579Ssam		m_freem(m);
780190579Ssam		ieee80211_free_node(ni);
781190579Ssam	}
782190579Ssam}
783190579Ssam
784190579Ssam/*
785190391Ssam * Switch between turbo and non-turbo operating modes.
786190391Ssam * Use the specified channel flags to locate the new
787190391Ssam * channel, update 802.11 state, and then call back into
788190391Ssam * the driver to effect the change.
789190391Ssam */
790190391Ssamvoid
791190391Ssamieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags)
792190391Ssam{
793190391Ssam	struct ieee80211com *ic = vap->iv_ic;
794190391Ssam	struct ieee80211_channel *chan;
795190391Ssam
796190391Ssam	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
797190391Ssam	if (chan == NULL) {		/* XXX should not happen */
798190391Ssam		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
799190391Ssam		    "%s: no channel with freq %u flags 0x%x\n",
800190391Ssam		    __func__, ic->ic_bsschan->ic_freq, newflags);
801190391Ssam		return;
802190391Ssam	}
803190391Ssam
804190391Ssam	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
805190391Ssam	    "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
806190391Ssam	    ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
807190391Ssam	    ieee80211_phymode_name[ieee80211_chan2mode(chan)],
808190391Ssam	    chan->ic_freq, chan->ic_flags);
809190391Ssam
810190391Ssam	ic->ic_bsschan = chan;
811190391Ssam	ic->ic_prevchan = ic->ic_curchan;
812190391Ssam	ic->ic_curchan = chan;
813190532Ssam	ic->ic_rt = ieee80211_get_ratetable(chan);
814190391Ssam	ic->ic_set_channel(ic);
815190391Ssam	/* NB: do not need to reset ERP state 'cuz we're in sta mode */
816190391Ssam}
817190391Ssam
818190391Ssam/*
819190391Ssam * Return the current ``state'' of an Atheros capbility.
820190391Ssam * If associated in station mode report the negotiated
821190391Ssam * setting. Otherwise report the current setting.
822190391Ssam */
823190391Ssamstatic int
824190391Ssamgetathcap(struct ieee80211vap *vap, int cap)
825190391Ssam{
826190391Ssam	if (vap->iv_opmode == IEEE80211_M_STA &&
827190391Ssam	    vap->iv_state == IEEE80211_S_RUN)
828190391Ssam		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
829190391Ssam	else
830190391Ssam		return (vap->iv_flags & cap) != 0;
831190391Ssam}
832190391Ssam
833190391Ssamstatic int
834190391Ssamsuperg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
835190391Ssam{
836190391Ssam	switch (ireq->i_type) {
837190391Ssam	case IEEE80211_IOC_FF:
838190391Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
839190391Ssam		break;
840190391Ssam	case IEEE80211_IOC_TURBOP:
841190391Ssam		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
842190391Ssam		break;
843190391Ssam	default:
844190391Ssam		return ENOSYS;
845190391Ssam	}
846190391Ssam	return 0;
847190391Ssam}
848190391SsamIEEE80211_IOCTL_GET(superg, superg_ioctl_get80211);
849190391Ssam
850190391Ssamstatic int
851190391Ssamsuperg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
852190391Ssam{
853190391Ssam	switch (ireq->i_type) {
854190391Ssam	case IEEE80211_IOC_FF:
855190391Ssam		if (ireq->i_val) {
856190391Ssam			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
857190391Ssam				return EOPNOTSUPP;
858190391Ssam			vap->iv_flags |= IEEE80211_F_FF;
859190391Ssam		} else
860190391Ssam			vap->iv_flags &= ~IEEE80211_F_FF;
861190530Ssam		return ENETRESET;
862190391Ssam	case IEEE80211_IOC_TURBOP:
863190391Ssam		if (ireq->i_val) {
864190391Ssam			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
865190391Ssam				return EOPNOTSUPP;
866190391Ssam			vap->iv_flags |= IEEE80211_F_TURBOP;
867190391Ssam		} else
868190391Ssam			vap->iv_flags &= ~IEEE80211_F_TURBOP;
869190391Ssam		return ENETRESET;
870190391Ssam	default:
871190391Ssam		return ENOSYS;
872190391Ssam	}
873190391Ssam	return 0;
874190391Ssam}
875190391SsamIEEE80211_IOCTL_SET(superg, superg_ioctl_set80211);
876