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