xref: /freebsd/sys/net80211/ieee80211_superg.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_wlan.h"
32 
33 #ifdef	IEEE80211_SUPPORT_SUPERG
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/endian.h>
40 
41 #include <sys/socket.h>
42 
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 #include <net/bpf.h>
48 #include <net/ethernet.h>
49 
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_input.h>
52 #include <net80211/ieee80211_phy.h>
53 #include <net80211/ieee80211_superg.h>
54 
55 /*
56  * Atheros fast-frame encapsulation format.
57  * FF max payload:
58  * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500:
59  *   8   +   4   +  4   +   14  +   8   + 1500 +  6   +   14  +   8   + 1500
60  * = 3066
61  */
62 /* fast frame header is 32-bits */
63 #define	ATH_FF_PROTO	0x0000003f	/* protocol */
64 #define	ATH_FF_PROTO_S	0
65 #define	ATH_FF_FTYPE	0x000000c0	/* frame type */
66 #define	ATH_FF_FTYPE_S	6
67 #define	ATH_FF_HLEN32	0x00000300	/* optional hdr length */
68 #define	ATH_FF_HLEN32_S	8
69 #define	ATH_FF_SEQNUM	0x001ffc00	/* sequence number */
70 #define	ATH_FF_SEQNUM_S	10
71 #define	ATH_FF_OFFSET	0xffe00000	/* offset to 2nd payload */
72 #define	ATH_FF_OFFSET_S	21
73 
74 #define	ATH_FF_MAX_HDR_PAD	4
75 #define	ATH_FF_MAX_SEP_PAD	6
76 #define	ATH_FF_MAX_HDR		30
77 
78 #define	ATH_FF_PROTO_L2TUNNEL	0	/* L2 tunnel protocol */
79 #define	ATH_FF_ETH_TYPE		0x88bd	/* Ether type for encapsulated frames */
80 #define	ATH_FF_SNAP_ORGCODE_0	0x00
81 #define	ATH_FF_SNAP_ORGCODE_1	0x03
82 #define	ATH_FF_SNAP_ORGCODE_2	0x7f
83 
84 #define	ATH_FF_TXQMIN	2		/* min txq depth for staging */
85 #define	ATH_FF_TXQMAX	50		/* maximum # of queued frames allowed */
86 #define	ATH_FF_STAGEMAX	5		/* max waiting period for staged frame*/
87 
88 #define	ETHER_HEADER_COPY(dst, src) \
89 	memcpy(dst, src, sizeof(struct ether_header))
90 
91 static	int ieee80211_ffppsmin = 2;	/* pps threshold for ff aggregation */
92 SYSCTL_INT(_net_wlan, OID_AUTO, ffppsmin, CTLFLAG_RW,
93 	&ieee80211_ffppsmin, 0, "min packet rate before fast-frame staging");
94 static	int ieee80211_ffagemax = -1;	/* max time frames held on stage q */
95 SYSCTL_PROC(_net_wlan, OID_AUTO, ffagemax, CTLTYPE_INT | CTLFLAG_RW,
96 	&ieee80211_ffagemax, 0, ieee80211_sysctl_msecs_ticks, "I",
97 	"max hold time for fast-frame staging (ms)");
98 
99 static void
100 ff_age_all(void *arg, int npending)
101 {
102 	struct ieee80211com *ic = arg;
103 
104 	/* XXX cache timer value somewhere (racy) */
105 	ieee80211_ff_age_all(ic, ieee80211_ffagemax + 1);
106 }
107 
108 void
109 ieee80211_superg_attach(struct ieee80211com *ic)
110 {
111 	struct ieee80211_superg *sg;
112 
113 	IEEE80211_FF_LOCK_INIT(ic, ic->ic_name);
114 
115 	sg = (struct ieee80211_superg *) IEEE80211_MALLOC(
116 	     sizeof(struct ieee80211_superg), M_80211_VAP,
117 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
118 	if (sg == NULL) {
119 		printf("%s: cannot allocate SuperG state block\n",
120 		    __func__);
121 		return;
122 	}
123 	TIMEOUT_TASK_INIT(ic->ic_tq, &sg->ff_qtimer, 0, ff_age_all, ic);
124 	ic->ic_superg = sg;
125 
126 	/*
127 	 * Default to not being so aggressive for FF/AMSDU
128 	 * aging, otherwise we may hold a frame around
129 	 * for way too long before we expire it out.
130 	 */
131 	ieee80211_ffagemax = msecs_to_ticks(2);
132 }
133 
134 void
135 ieee80211_superg_detach(struct ieee80211com *ic)
136 {
137 
138 	if (ic->ic_superg != NULL) {
139 		struct timeout_task *qtask = &ic->ic_superg->ff_qtimer;
140 
141 		while (taskqueue_cancel_timeout(ic->ic_tq, qtask, NULL) != 0)
142 			taskqueue_drain_timeout(ic->ic_tq, qtask);
143 		IEEE80211_FREE(ic->ic_superg, M_80211_VAP);
144 		ic->ic_superg = NULL;
145 	}
146 	IEEE80211_FF_LOCK_DESTROY(ic);
147 }
148 
149 void
150 ieee80211_superg_vattach(struct ieee80211vap *vap)
151 {
152 	struct ieee80211com *ic = vap->iv_ic;
153 
154 	if (ic->ic_superg == NULL)	/* NB: can't do fast-frames w/o state */
155 		vap->iv_caps &= ~IEEE80211_C_FF;
156 	if (vap->iv_caps & IEEE80211_C_FF)
157 		vap->iv_flags |= IEEE80211_F_FF;
158 	/* NB: we only implement sta mode */
159 	if (vap->iv_opmode == IEEE80211_M_STA &&
160 	    (vap->iv_caps & IEEE80211_C_TURBOP))
161 		vap->iv_flags |= IEEE80211_F_TURBOP;
162 }
163 
164 void
165 ieee80211_superg_vdetach(struct ieee80211vap *vap)
166 {
167 }
168 
169 #define	ATH_OUI_BYTES		0x00, 0x03, 0x7f
170 /*
171  * Add a WME information element to a frame.
172  */
173 uint8_t *
174 ieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix)
175 {
176 	static const struct ieee80211_ath_ie info = {
177 		.ath_id		= IEEE80211_ELEMID_VENDOR,
178 		.ath_len	= sizeof(struct ieee80211_ath_ie) - 2,
179 		.ath_oui	= { ATH_OUI_BYTES },
180 		.ath_oui_type	= ATH_OUI_TYPE,
181 		.ath_oui_subtype= ATH_OUI_SUBTYPE,
182 		.ath_version	= ATH_OUI_VERSION,
183 	};
184 	struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm;
185 
186 	memcpy(frm, &info, sizeof(info));
187 	ath->ath_capability = caps;
188 	if (defkeyix != IEEE80211_KEYIX_NONE) {
189 		ath->ath_defkeyix[0] = (defkeyix & 0xff);
190 		ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff);
191 	} else {
192 		ath->ath_defkeyix[0] = 0xff;
193 		ath->ath_defkeyix[1] = 0x7f;
194 	}
195 	return frm + sizeof(info);
196 }
197 #undef ATH_OUI_BYTES
198 
199 uint8_t *
200 ieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss)
201 {
202 	const struct ieee80211vap *vap = bss->ni_vap;
203 
204 	return ieee80211_add_ath(frm,
205 	    vap->iv_flags & IEEE80211_F_ATHEROS,
206 	    ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
207 	    bss->ni_authmode != IEEE80211_AUTH_8021X) ?
208 	    vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
209 }
210 
211 void
212 ieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie)
213 {
214 	const struct ieee80211_ath_ie *ath =
215 		(const struct ieee80211_ath_ie *) ie;
216 
217 	ni->ni_ath_flags = ath->ath_capability;
218 	ni->ni_ath_defkeyix = le16dec(&ath->ath_defkeyix);
219 }
220 
221 int
222 ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
223 	const struct ieee80211_frame *wh)
224 {
225 	struct ieee80211vap *vap = ni->ni_vap;
226 	const struct ieee80211_ath_ie *ath;
227 	u_int len = frm[1];
228 	int capschanged;
229 	uint16_t defkeyix;
230 
231 	if (len < sizeof(struct ieee80211_ath_ie)-2) {
232 		IEEE80211_DISCARD_IE(vap,
233 		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
234 		    wh, "Atheros", "too short, len %u", len);
235 		return -1;
236 	}
237 	ath = (const struct ieee80211_ath_ie *)frm;
238 	capschanged = (ni->ni_ath_flags != ath->ath_capability);
239 	defkeyix = le16dec(ath->ath_defkeyix);
240 	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
241 		ni->ni_ath_flags = ath->ath_capability;
242 		ni->ni_ath_defkeyix = defkeyix;
243 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
244 		    "ath ie change: new caps 0x%x defkeyix 0x%x",
245 		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
246 	}
247 	if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) {
248 		uint16_t curflags, newflags;
249 
250 		/*
251 		 * Check for turbo mode switch.  Calculate flags
252 		 * for the new mode and effect the switch.
253 		 */
254 		newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags;
255 		/* NB: BOOST is not in ic_flags, so get it from the ie */
256 		if (ath->ath_capability & ATHEROS_CAP_BOOST)
257 			newflags |= IEEE80211_CHAN_TURBO;
258 		else
259 			newflags &= ~IEEE80211_CHAN_TURBO;
260 		if (newflags != curflags)
261 			ieee80211_dturbo_switch(vap, newflags);
262 	}
263 	return capschanged;
264 }
265 
266 /*
267  * Decap the encapsulated frame pair and dispatch the first
268  * for delivery.  The second frame is returned for delivery
269  * via the normal path.
270  */
271 struct mbuf *
272 ieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m)
273 {
274 #define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
275 #define	MS(x,f)	(((x) & f) >> f##_S)
276 	struct ieee80211vap *vap = ni->ni_vap;
277 	struct llc *llc;
278 	uint32_t ath;
279 	struct mbuf *n;
280 	int framelen;
281 
282 	/* NB: we assume caller does this check for us */
283 	KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF),
284 	    ("ff not negotiated"));
285 	/*
286 	 * Check for fast-frame tunnel encapsulation.
287 	 */
288 	if (m->m_pkthdr.len < 3*FF_LLC_SIZE)
289 		return m;
290 	if (m->m_len < FF_LLC_SIZE &&
291 	    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
292 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
293 		    ni->ni_macaddr, "fast-frame",
294 		    "%s", "m_pullup(llc) failed");
295 		vap->iv_stats.is_rx_tooshort++;
296 		return NULL;
297 	}
298 	llc = (struct llc *)(mtod(m, uint8_t *) +
299 	    sizeof(struct ether_header));
300 	if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE))
301 		return m;
302 	m_adj(m, FF_LLC_SIZE);
303 	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
304 	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
305 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
306 		    ni->ni_macaddr, "fast-frame",
307 		    "unsupport tunnel protocol, header 0x%x", ath);
308 		vap->iv_stats.is_ff_badhdr++;
309 		m_freem(m);
310 		return NULL;
311 	}
312 	/* NB: skip header and alignment padding */
313 	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
314 
315 	vap->iv_stats.is_ff_decap++;
316 
317 	/*
318 	 * Decap the first frame, bust it apart from the
319 	 * second and deliver; then decap the second frame
320 	 * and return it to the caller for normal delivery.
321 	 */
322 	m = ieee80211_decap1(m, &framelen);
323 	if (m == NULL) {
324 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
325 		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
326 		vap->iv_stats.is_ff_tooshort++;
327 		return NULL;
328 	}
329 	n = m_split(m, framelen, M_NOWAIT);
330 	if (n == NULL) {
331 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
332 		    ni->ni_macaddr, "fast-frame",
333 		    "%s", "unable to split encapsulated frames");
334 		vap->iv_stats.is_ff_split++;
335 		m_freem(m);			/* NB: must reclaim */
336 		return NULL;
337 	}
338 	/* XXX not right for WDS */
339 	vap->iv_deliver_data(vap, ni, m);	/* 1st of pair */
340 
341 	/*
342 	 * Decap second frame.
343 	 */
344 	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
345 	n = ieee80211_decap1(n, &framelen);
346 	if (n == NULL) {
347 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
348 		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
349 		vap->iv_stats.is_ff_tooshort++;
350 	}
351 	/* XXX verify framelen against mbuf contents */
352 	return n;				/* 2nd delivered by caller */
353 #undef MS
354 #undef FF_LLC_SIZE
355 }
356 
357 /*
358  * Fast frame encapsulation.  There must be two packets
359  * chained with m_nextpkt.  We do header adjustment for
360  * each, add the tunnel encapsulation, and then concatenate
361  * the mbuf chains to form a single frame for transmission.
362  */
363 struct mbuf *
364 ieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
365 	struct ieee80211_key *key)
366 {
367 	struct mbuf *m2;
368 	struct ether_header eh1, eh2;
369 	struct llc *llc;
370 	struct mbuf *m;
371 	int pad;
372 
373 	m2 = m1->m_nextpkt;
374 	if (m2 == NULL) {
375 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
376 		    "%s: only one frame\n", __func__);
377 		goto bad;
378 	}
379 	m1->m_nextpkt = NULL;
380 
381 	/*
382 	 * Adjust to include 802.11 header requirement.
383 	 */
384 	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
385 	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
386 	m1 = ieee80211_mbuf_adjust(vap, hdrspace, key, m1);
387 	if (m1 == NULL) {
388 		printf("%s: failed initial mbuf_adjust\n", __func__);
389 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
390 		m_freem(m2);
391 		goto bad;
392 	}
393 
394 	/*
395 	 * Copy second frame's Ethernet header out of line
396 	 * and adjust for possible padding in case there isn't room
397 	 * at the end of first frame.
398 	 */
399 	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
400 	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
401 	m2 = ieee80211_mbuf_adjust(vap, 4, NULL, m2);
402 	if (m2 == NULL) {
403 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
404 		printf("%s: failed second \n", __func__);
405 		goto bad;
406 	}
407 
408 	/*
409 	 * Now do tunnel encapsulation.  First, each
410 	 * frame gets a standard encapsulation.
411 	 */
412 	m1 = ieee80211_ff_encap1(vap, m1, &eh1);
413 	if (m1 == NULL)
414 		goto bad;
415 	m2 = ieee80211_ff_encap1(vap, m2, &eh2);
416 	if (m2 == NULL)
417 		goto bad;
418 
419 	/*
420 	 * Pad leading frame to a 4-byte boundary.  If there
421 	 * is space at the end of the first frame, put it
422 	 * there; otherwise prepend to the front of the second
423 	 * frame.  We know doing the second will always work
424 	 * because we reserve space above.  We prefer appending
425 	 * as this typically has better DMA alignment properties.
426 	 */
427 	for (m = m1; m->m_next != NULL; m = m->m_next)
428 		;
429 	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
430 	if (pad) {
431 		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
432 			m2->m_data -= pad;
433 			m2->m_len += pad;
434 			m2->m_pkthdr.len += pad;
435 		} else {				/* append to first */
436 			m->m_len += pad;
437 			m1->m_pkthdr.len += pad;
438 		}
439 	}
440 
441 	/*
442 	 * A-MSDU's are just appended; the "I'm A-MSDU!" bit is in the
443 	 * QoS header.
444 	 *
445 	 * XXX optimize by prepending together
446 	 */
447 	m->m_next = m2;			/* NB: last mbuf from above */
448 	m1->m_pkthdr.len += m2->m_pkthdr.len;
449 	M_PREPEND(m1, sizeof(uint32_t)+2, M_NOWAIT);
450 	if (m1 == NULL) {		/* XXX cannot happen */
451 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
452 		    "%s: no space for tunnel header\n", __func__);
453 		vap->iv_stats.is_tx_nobuf++;
454 		return NULL;
455 	}
456 	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
457 
458 	M_PREPEND(m1, sizeof(struct llc), M_NOWAIT);
459 	if (m1 == NULL) {		/* XXX cannot happen */
460 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
461 		    "%s: no space for llc header\n", __func__);
462 		vap->iv_stats.is_tx_nobuf++;
463 		return NULL;
464 	}
465 	llc = mtod(m1, struct llc *);
466 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
467 	llc->llc_control = LLC_UI;
468 	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
469 	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
470 	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
471 	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
472 
473 	vap->iv_stats.is_ff_encap++;
474 
475 	return m1;
476 bad:
477 	vap->iv_stats.is_ff_encapfail++;
478 	if (m1 != NULL)
479 		m_freem(m1);
480 	if (m2 != NULL)
481 		m_freem(m2);
482 	return NULL;
483 }
484 
485 /*
486  * A-MSDU encapsulation.
487  *
488  * This assumes just two frames for now, since we're borrowing the
489  * same queuing code and infrastructure as fast-frames.
490  *
491  * There must be two packets chained with m_nextpkt.
492  * We do header adjustment for each, and then concatenate the mbuf chains
493  * to form a single frame for transmission.
494  */
495 struct mbuf *
496 ieee80211_amsdu_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
497 	struct ieee80211_key *key)
498 {
499 	struct mbuf *m2;
500 	struct ether_header eh1, eh2;
501 	struct mbuf *m;
502 	int pad;
503 
504 	m2 = m1->m_nextpkt;
505 	if (m2 == NULL) {
506 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
507 		    "%s: only one frame\n", __func__);
508 		goto bad;
509 	}
510 	m1->m_nextpkt = NULL;
511 
512 	/*
513 	 * Include A-MSDU header in adjusting header layout.
514 	 */
515 	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
516 	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
517 	m1 = ieee80211_mbuf_adjust(vap,
518 		hdrspace + sizeof(struct llc) + sizeof(uint32_t) +
519 		    sizeof(struct ether_header),
520 		key, m1);
521 	if (m1 == NULL) {
522 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
523 		m_freem(m2);
524 		goto bad;
525 	}
526 
527 	/*
528 	 * Copy second frame's Ethernet header out of line
529 	 * and adjust for encapsulation headers.  Note that
530 	 * we make room for padding in case there isn't room
531 	 * at the end of first frame.
532 	 */
533 	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
534 	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
535 	m2 = ieee80211_mbuf_adjust(vap, 4, NULL, m2);
536 	if (m2 == NULL) {
537 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
538 		goto bad;
539 	}
540 
541 	/*
542 	 * Now do tunnel encapsulation.  First, each
543 	 * frame gets a standard encapsulation.
544 	 */
545 	m1 = ieee80211_ff_encap1(vap, m1, &eh1);
546 	if (m1 == NULL)
547 		goto bad;
548 	m2 = ieee80211_ff_encap1(vap, m2, &eh2);
549 	if (m2 == NULL)
550 		goto bad;
551 
552 	/*
553 	 * Pad leading frame to a 4-byte boundary.  If there
554 	 * is space at the end of the first frame, put it
555 	 * there; otherwise prepend to the front of the second
556 	 * frame.  We know doing the second will always work
557 	 * because we reserve space above.  We prefer appending
558 	 * as this typically has better DMA alignment properties.
559 	 */
560 	for (m = m1; m->m_next != NULL; m = m->m_next)
561 		;
562 	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
563 	if (pad) {
564 		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
565 			m2->m_data -= pad;
566 			m2->m_len += pad;
567 			m2->m_pkthdr.len += pad;
568 		} else {				/* append to first */
569 			m->m_len += pad;
570 			m1->m_pkthdr.len += pad;
571 		}
572 	}
573 
574 	/*
575 	 * Now, stick 'em together.
576 	 */
577 	m->m_next = m2;			/* NB: last mbuf from above */
578 	m1->m_pkthdr.len += m2->m_pkthdr.len;
579 
580 	vap->iv_stats.is_amsdu_encap++;
581 
582 	return m1;
583 bad:
584 	vap->iv_stats.is_amsdu_encapfail++;
585 	if (m1 != NULL)
586 		m_freem(m1);
587 	if (m2 != NULL)
588 		m_freem(m2);
589 	return NULL;
590 }
591 
592 
593 static void
594 ff_transmit(struct ieee80211_node *ni, struct mbuf *m)
595 {
596 	struct ieee80211vap *vap = ni->ni_vap;
597 	struct ieee80211com *ic = ni->ni_ic;
598 
599 	IEEE80211_TX_LOCK_ASSERT(ic);
600 
601 	/* encap and xmit */
602 	m = ieee80211_encap(vap, ni, m);
603 	if (m != NULL)
604 		(void) ieee80211_parent_xmitpkt(ic, m);
605 	else
606 		ieee80211_free_node(ni);
607 }
608 
609 /*
610  * Flush frames to device; note we re-use the linked list
611  * the frames were stored on and use the sentinel (unchanged)
612  * which may be non-NULL.
613  */
614 static void
615 ff_flush(struct mbuf *head, struct mbuf *last)
616 {
617 	struct mbuf *m, *next;
618 	struct ieee80211_node *ni;
619 	struct ieee80211vap *vap;
620 
621 	for (m = head; m != last; m = next) {
622 		next = m->m_nextpkt;
623 		m->m_nextpkt = NULL;
624 
625 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
626 		vap = ni->ni_vap;
627 
628 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
629 		    "%s: flush frame, age %u", __func__, M_AGE_GET(m));
630 		vap->iv_stats.is_ff_flush++;
631 
632 		ff_transmit(ni, m);
633 	}
634 }
635 
636 /*
637  * Age frames on the staging queue.
638  */
639 void
640 ieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq,
641     int quanta)
642 {
643 	struct mbuf *m, *head;
644 	struct ieee80211_node *ni;
645 
646 	IEEE80211_FF_LOCK(ic);
647 	if (sq->depth == 0) {
648 		IEEE80211_FF_UNLOCK(ic);
649 		return;		/* nothing to do */
650 	}
651 
652 	KASSERT(sq->head != NULL, ("stageq empty"));
653 
654 	head = sq->head;
655 	while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) {
656 		int tid = WME_AC_TO_TID(M_WME_GETAC(m));
657 
658 		/* clear staging ref to frame */
659 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
660 		KASSERT(ni->ni_tx_superg[tid] == m, ("staging queue empty"));
661 		ni->ni_tx_superg[tid] = NULL;
662 
663 		sq->head = m->m_nextpkt;
664 		sq->depth--;
665 	}
666 	if (m == NULL)
667 		sq->tail = NULL;
668 	else
669 		M_AGE_SUB(m, quanta);
670 	IEEE80211_FF_UNLOCK(ic);
671 
672 	IEEE80211_TX_LOCK(ic);
673 	ff_flush(head, m);
674 	IEEE80211_TX_UNLOCK(ic);
675 }
676 
677 static void
678 stageq_add(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *m)
679 {
680 	int age = ieee80211_ffagemax;
681 
682 	IEEE80211_FF_LOCK_ASSERT(ic);
683 
684 	if (sq->tail != NULL) {
685 		sq->tail->m_nextpkt = m;
686 		age -= M_AGE_GET(sq->head);
687 	} else {
688 		sq->head = m;
689 
690 		struct timeout_task *qtask = &ic->ic_superg->ff_qtimer;
691 		taskqueue_enqueue_timeout(ic->ic_tq, qtask, age);
692 	}
693 	KASSERT(age >= 0, ("age %d", age));
694 	M_AGE_SET(m, age);
695 	m->m_nextpkt = NULL;
696 	sq->tail = m;
697 	sq->depth++;
698 }
699 
700 static void
701 stageq_remove(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *mstaged)
702 {
703 	struct mbuf *m, *mprev;
704 
705 	IEEE80211_FF_LOCK_ASSERT(ic);
706 
707 	mprev = NULL;
708 	for (m = sq->head; m != NULL; m = m->m_nextpkt) {
709 		if (m == mstaged) {
710 			if (mprev == NULL)
711 				sq->head = m->m_nextpkt;
712 			else
713 				mprev->m_nextpkt = m->m_nextpkt;
714 			if (sq->tail == m)
715 				sq->tail = mprev;
716 			sq->depth--;
717 			return;
718 		}
719 		mprev = m;
720 	}
721 	printf("%s: packet not found\n", __func__);
722 }
723 
724 static uint32_t
725 ff_approx_txtime(struct ieee80211_node *ni,
726 	const struct mbuf *m1, const struct mbuf *m2)
727 {
728 	struct ieee80211com *ic = ni->ni_ic;
729 	struct ieee80211vap *vap = ni->ni_vap;
730 	uint32_t framelen;
731 	uint32_t frame_time;
732 
733 	/*
734 	 * Approximate the frame length to be transmitted. A swag to add
735 	 * the following maximal values to the skb payload:
736 	 *   - 32: 802.11 encap + CRC
737 	 *   - 24: encryption overhead (if wep bit)
738 	 *   - 4 + 6: fast-frame header and padding
739 	 *   - 16: 2 LLC FF tunnel headers
740 	 *   - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd)
741 	 */
742 	framelen = m1->m_pkthdr.len + 32 +
743 	    ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR;
744 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
745 		framelen += 24;
746 	if (m2 != NULL)
747 		framelen += m2->m_pkthdr.len;
748 
749 	/*
750 	 * For now, we assume non-shortgi, 20MHz, just because I want to
751 	 * at least test 802.11n.
752 	 */
753 	if (ni->ni_txrate & IEEE80211_RATE_MCS)
754 		frame_time = ieee80211_compute_duration_ht(framelen,
755 		    ni->ni_txrate,
756 		    IEEE80211_HT_RC_2_STREAMS(ni->ni_txrate),
757 		    0, /* isht40 */
758 		    0); /* isshortgi */
759 	else
760 		frame_time = ieee80211_compute_duration(ic->ic_rt, framelen,
761 			    ni->ni_txrate, 0);
762 	return (frame_time);
763 }
764 
765 /*
766  * Check if the supplied frame can be partnered with an existing
767  * or pending frame.  Return a reference to any frame that should be
768  * sent on return; otherwise return NULL.
769  */
770 struct mbuf *
771 ieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m)
772 {
773 	struct ieee80211vap *vap = ni->ni_vap;
774 	struct ieee80211com *ic = ni->ni_ic;
775 	struct ieee80211_superg *sg = ic->ic_superg;
776 	const int pri = M_WME_GETAC(m);
777 	struct ieee80211_stageq *sq;
778 	struct ieee80211_tx_ampdu *tap;
779 	struct mbuf *mstaged;
780 	uint32_t txtime, limit;
781 
782 	IEEE80211_TX_UNLOCK_ASSERT(ic);
783 
784 	IEEE80211_LOCK(ic);
785 	limit = IEEE80211_TXOP_TO_US(
786 	    ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit);
787 	IEEE80211_UNLOCK(ic);
788 
789 	/*
790 	 * Check if the supplied frame can be aggregated.
791 	 *
792 	 * NB: we allow EAPOL frames to be aggregated with other ucast traffic.
793 	 *     Do 802.1x EAPOL frames proceed in the clear? Then they couldn't
794 	 *     be aggregated with other types of frames when encryption is on?
795 	 */
796 	IEEE80211_FF_LOCK(ic);
797 	tap = &ni->ni_tx_ampdu[WME_AC_TO_TID(pri)];
798 	mstaged = ni->ni_tx_superg[WME_AC_TO_TID(pri)];
799 	/* XXX NOTE: reusing packet counter state from A-MPDU */
800 	/*
801 	 * XXX NOTE: this means we're double-counting; it should just
802 	 * be done in ieee80211_output.c once for both superg and A-MPDU.
803 	 */
804 	ieee80211_txampdu_count_packet(tap);
805 
806 	/*
807 	 * When not in station mode never aggregate a multicast
808 	 * frame; this insures, for example, that a combined frame
809 	 * does not require multiple encryption keys.
810 	 */
811 	if (vap->iv_opmode != IEEE80211_M_STA &&
812 	    ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) {
813 		/* XXX flush staged frame? */
814 		IEEE80211_FF_UNLOCK(ic);
815 		return m;
816 	}
817 	/*
818 	 * If there is no frame to combine with and the pps is
819 	 * too low; then do not attempt to aggregate this frame.
820 	 */
821 	if (mstaged == NULL &&
822 	    ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) {
823 		IEEE80211_FF_UNLOCK(ic);
824 		return m;
825 	}
826 	sq = &sg->ff_stageq[pri];
827 	/*
828 	 * Check the txop limit to insure the aggregate fits.
829 	 */
830 	if (limit != 0 &&
831 	    (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) {
832 		/*
833 		 * Aggregate too long, return to the caller for direct
834 		 * transmission.  In addition, flush any pending frame
835 		 * before sending this one.
836 		 */
837 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
838 		    "%s: txtime %u exceeds txop limit %u\n",
839 		    __func__, txtime, limit);
840 
841 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = NULL;
842 		if (mstaged != NULL)
843 			stageq_remove(ic, sq, mstaged);
844 		IEEE80211_FF_UNLOCK(ic);
845 
846 		if (mstaged != NULL) {
847 			IEEE80211_TX_LOCK(ic);
848 			IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
849 			    "%s: flush staged frame", __func__);
850 			/* encap and xmit */
851 			ff_transmit(ni, mstaged);
852 			IEEE80211_TX_UNLOCK(ic);
853 		}
854 		return m;		/* NB: original frame */
855 	}
856 	/*
857 	 * An aggregation candidate.  If there's a frame to partner
858 	 * with then combine and return for processing.  Otherwise
859 	 * save this frame and wait for a partner to show up (or
860 	 * the frame to be flushed).  Note that staged frames also
861 	 * hold their node reference.
862 	 */
863 	if (mstaged != NULL) {
864 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = NULL;
865 		stageq_remove(ic, sq, mstaged);
866 		IEEE80211_FF_UNLOCK(ic);
867 
868 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
869 		    "%s: aggregate fast-frame", __func__);
870 		/*
871 		 * Release the node reference; we only need
872 		 * the one already in mstaged.
873 		 */
874 		KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni,
875 		    ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni));
876 		ieee80211_free_node(ni);
877 
878 		m->m_nextpkt = NULL;
879 		mstaged->m_nextpkt = m;
880 		mstaged->m_flags |= M_FF; /* NB: mark for encap work */
881 	} else {
882 		KASSERT(ni->ni_tx_superg[WME_AC_TO_TID(pri)] == NULL,
883 		    ("ni_tx_superg[]: %p",
884 		    ni->ni_tx_superg[WME_AC_TO_TID(pri)]));
885 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = m;
886 
887 		stageq_add(ic, sq, m);
888 		IEEE80211_FF_UNLOCK(ic);
889 
890 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
891 		    "%s: stage frame, %u queued", __func__, sq->depth);
892 		/* NB: mstaged is NULL */
893 	}
894 	return mstaged;
895 }
896 
897 struct mbuf *
898 ieee80211_amsdu_check(struct ieee80211_node *ni, struct mbuf *m)
899 {
900 	/*
901 	 * XXX TODO: actually enforce the node support
902 	 * and HTCAP requirements for the maximum A-MSDU
903 	 * size.
904 	 */
905 
906 	/* First: software A-MSDU transmit? */
907 	if (! ieee80211_amsdu_tx_ok(ni))
908 		return (m);
909 
910 	/* Next - EAPOL? Nope, don't aggregate; we don't QoS encap them */
911 	if (m->m_flags & (M_EAPOL | M_MCAST | M_BCAST))
912 		return (m);
913 
914 	/* Next - needs to be a data frame, non-broadcast, etc */
915 	if (ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost))
916 		return (m);
917 
918 	return (ieee80211_ff_check(ni, m));
919 }
920 
921 void
922 ieee80211_ff_node_init(struct ieee80211_node *ni)
923 {
924 	/*
925 	 * Clean FF state on re-associate.  This handles the case
926 	 * where a station leaves w/o notifying us and then returns
927 	 * before node is reaped for inactivity.
928 	 */
929 	ieee80211_ff_node_cleanup(ni);
930 }
931 
932 void
933 ieee80211_ff_node_cleanup(struct ieee80211_node *ni)
934 {
935 	struct ieee80211com *ic = ni->ni_ic;
936 	struct ieee80211_superg *sg = ic->ic_superg;
937 	struct mbuf *m, *next_m, *head;
938 	int tid;
939 
940 	IEEE80211_FF_LOCK(ic);
941 	head = NULL;
942 	for (tid = 0; tid < WME_NUM_TID; tid++) {
943 		int ac = TID_TO_WME_AC(tid);
944 		/*
945 		 * XXX Initialise the packet counter.
946 		 *
947 		 * This may be double-work for 11n stations;
948 		 * but without it we never setup things.
949 		 */
950 		ieee80211_txampdu_init_pps(&ni->ni_tx_ampdu[tid]);
951 		m = ni->ni_tx_superg[tid];
952 		if (m != NULL) {
953 			ni->ni_tx_superg[tid] = NULL;
954 			stageq_remove(ic, &sg->ff_stageq[ac], m);
955 			m->m_nextpkt = head;
956 			head = m;
957 		}
958 	}
959 	IEEE80211_FF_UNLOCK(ic);
960 
961 	/*
962 	 * Free mbufs, taking care to not dereference the mbuf after
963 	 * we free it (hence grabbing m_nextpkt before we free it.)
964 	 */
965 	m = head;
966 	while (m != NULL) {
967 		next_m = m->m_nextpkt;
968 		m_freem(m);
969 		ieee80211_free_node(ni);
970 		m = next_m;
971 	}
972 }
973 
974 /*
975  * Switch between turbo and non-turbo operating modes.
976  * Use the specified channel flags to locate the new
977  * channel, update 802.11 state, and then call back into
978  * the driver to effect the change.
979  */
980 void
981 ieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags)
982 {
983 	struct ieee80211com *ic = vap->iv_ic;
984 	struct ieee80211_channel *chan;
985 
986 	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
987 	if (chan == NULL) {		/* XXX should not happen */
988 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
989 		    "%s: no channel with freq %u flags 0x%x\n",
990 		    __func__, ic->ic_bsschan->ic_freq, newflags);
991 		return;
992 	}
993 
994 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
995 	    "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
996 	    ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
997 	    ieee80211_phymode_name[ieee80211_chan2mode(chan)],
998 	    chan->ic_freq, chan->ic_flags);
999 
1000 	ic->ic_bsschan = chan;
1001 	ic->ic_prevchan = ic->ic_curchan;
1002 	ic->ic_curchan = chan;
1003 	ic->ic_rt = ieee80211_get_ratetable(chan);
1004 	ic->ic_set_channel(ic);
1005 	ieee80211_radiotap_chan_change(ic);
1006 	/* NB: do not need to reset ERP state 'cuz we're in sta mode */
1007 }
1008 
1009 /*
1010  * Return the current ``state'' of an Atheros capbility.
1011  * If associated in station mode report the negotiated
1012  * setting. Otherwise report the current setting.
1013  */
1014 static int
1015 getathcap(struct ieee80211vap *vap, int cap)
1016 {
1017 	if (vap->iv_opmode == IEEE80211_M_STA &&
1018 	    vap->iv_state == IEEE80211_S_RUN)
1019 		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
1020 	else
1021 		return (vap->iv_flags & cap) != 0;
1022 }
1023 
1024 static int
1025 superg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
1026 {
1027 	switch (ireq->i_type) {
1028 	case IEEE80211_IOC_FF:
1029 		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
1030 		break;
1031 	case IEEE80211_IOC_TURBOP:
1032 		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
1033 		break;
1034 	default:
1035 		return ENOSYS;
1036 	}
1037 	return 0;
1038 }
1039 IEEE80211_IOCTL_GET(superg, superg_ioctl_get80211);
1040 
1041 static int
1042 superg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
1043 {
1044 	switch (ireq->i_type) {
1045 	case IEEE80211_IOC_FF:
1046 		if (ireq->i_val) {
1047 			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
1048 				return EOPNOTSUPP;
1049 			vap->iv_flags |= IEEE80211_F_FF;
1050 		} else
1051 			vap->iv_flags &= ~IEEE80211_F_FF;
1052 		return ENETRESET;
1053 	case IEEE80211_IOC_TURBOP:
1054 		if (ireq->i_val) {
1055 			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
1056 				return EOPNOTSUPP;
1057 			vap->iv_flags |= IEEE80211_F_TURBOP;
1058 		} else
1059 			vap->iv_flags &= ~IEEE80211_F_TURBOP;
1060 		return ENETRESET;
1061 	default:
1062 		return ENOSYS;
1063 	}
1064 }
1065 IEEE80211_IOCTL_SET(superg, superg_ioctl_set80211);
1066 
1067 #endif	/* IEEE80211_SUPPORT_SUPERG */
1068