xref: /freebsd/sys/net80211/ieee80211_superg.c (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
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 #define	MS(x,f)	(((x) & f) >> f##_S)
277 	struct ieee80211vap *vap = ni->ni_vap;
278 	struct llc *llc;
279 	uint32_t ath;
280 	struct mbuf *n;
281 	int framelen;
282 
283 	/* NB: we assume caller does this check for us */
284 	KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF),
285 	    ("ff not negotiated"));
286 	/*
287 	 * Check for fast-frame tunnel encapsulation.
288 	 */
289 	if (m->m_pkthdr.len < 3*FF_LLC_SIZE)
290 		return m;
291 	if (m->m_len < FF_LLC_SIZE &&
292 	    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
293 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
294 		    ni->ni_macaddr, "fast-frame",
295 		    "%s", "m_pullup(llc) failed");
296 		vap->iv_stats.is_rx_tooshort++;
297 		return NULL;
298 	}
299 	llc = (struct llc *)(mtod(m, uint8_t *) +
300 	    sizeof(struct ether_header));
301 	if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE))
302 		return m;
303 	m_adj(m, FF_LLC_SIZE);
304 	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
305 	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
306 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
307 		    ni->ni_macaddr, "fast-frame",
308 		    "unsupport tunnel protocol, header 0x%x", ath);
309 		vap->iv_stats.is_ff_badhdr++;
310 		m_freem(m);
311 		return NULL;
312 	}
313 	/* NB: skip header and alignment padding */
314 	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
315 
316 	vap->iv_stats.is_ff_decap++;
317 
318 	/*
319 	 * Decap the first frame, bust it apart from the
320 	 * second and deliver; then decap the second frame
321 	 * and return it to the caller for normal delivery.
322 	 */
323 	m = ieee80211_decap1(m, &framelen);
324 	if (m == NULL) {
325 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
326 		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
327 		vap->iv_stats.is_ff_tooshort++;
328 		return NULL;
329 	}
330 	n = m_split(m, framelen, M_NOWAIT);
331 	if (n == NULL) {
332 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
333 		    ni->ni_macaddr, "fast-frame",
334 		    "%s", "unable to split encapsulated frames");
335 		vap->iv_stats.is_ff_split++;
336 		m_freem(m);			/* NB: must reclaim */
337 		return NULL;
338 	}
339 	/* XXX not right for WDS */
340 	vap->iv_deliver_data(vap, ni, m);	/* 1st of pair */
341 
342 	/*
343 	 * Decap second frame.
344 	 */
345 	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
346 	n = ieee80211_decap1(n, &framelen);
347 	if (n == NULL) {
348 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
349 		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
350 		vap->iv_stats.is_ff_tooshort++;
351 	}
352 	/* XXX verify framelen against mbuf contents */
353 	return n;				/* 2nd delivered by caller */
354 #undef MS
355 #undef FF_LLC_SIZE
356 }
357 
358 /*
359  * Fast frame encapsulation.  There must be two packets
360  * chained with m_nextpkt.  We do header adjustment for
361  * each, add the tunnel encapsulation, and then concatenate
362  * the mbuf chains to form a single frame for transmission.
363  */
364 struct mbuf *
365 ieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
366 	struct ieee80211_key *key)
367 {
368 	struct mbuf *m2;
369 	struct ether_header eh1, eh2;
370 	struct llc *llc;
371 	struct mbuf *m;
372 	int pad;
373 
374 	m2 = m1->m_nextpkt;
375 	if (m2 == NULL) {
376 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
377 		    "%s: only one frame\n", __func__);
378 		goto bad;
379 	}
380 	m1->m_nextpkt = NULL;
381 
382 	/*
383 	 * Adjust to include 802.11 header requirement.
384 	 */
385 	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
386 	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
387 	m1 = ieee80211_mbuf_adjust(vap, hdrspace, key, m1);
388 	if (m1 == NULL) {
389 		printf("%s: failed initial mbuf_adjust\n", __func__);
390 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
391 		m_freem(m2);
392 		goto bad;
393 	}
394 
395 	/*
396 	 * Copy second frame's Ethernet header out of line
397 	 * and adjust for possible padding in case there isn't room
398 	 * at the end of first frame.
399 	 */
400 	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
401 	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
402 	m2 = ieee80211_mbuf_adjust(vap, 4, NULL, m2);
403 	if (m2 == NULL) {
404 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
405 		printf("%s: failed second \n", __func__);
406 		goto bad;
407 	}
408 
409 	/*
410 	 * Now do tunnel encapsulation.  First, each
411 	 * frame gets a standard encapsulation.
412 	 */
413 	m1 = ieee80211_ff_encap1(vap, m1, &eh1);
414 	if (m1 == NULL)
415 		goto bad;
416 	m2 = ieee80211_ff_encap1(vap, m2, &eh2);
417 	if (m2 == NULL)
418 		goto bad;
419 
420 	/*
421 	 * Pad leading frame to a 4-byte boundary.  If there
422 	 * is space at the end of the first frame, put it
423 	 * there; otherwise prepend to the front of the second
424 	 * frame.  We know doing the second will always work
425 	 * because we reserve space above.  We prefer appending
426 	 * as this typically has better DMA alignment properties.
427 	 */
428 	for (m = m1; m->m_next != NULL; m = m->m_next)
429 		;
430 	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
431 	if (pad) {
432 		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
433 			m2->m_data -= pad;
434 			m2->m_len += pad;
435 			m2->m_pkthdr.len += pad;
436 		} else {				/* append to first */
437 			m->m_len += pad;
438 			m1->m_pkthdr.len += pad;
439 		}
440 	}
441 
442 	/*
443 	 * A-MSDU's are just appended; the "I'm A-MSDU!" bit is in the
444 	 * QoS header.
445 	 *
446 	 * XXX optimize by prepending together
447 	 */
448 	m->m_next = m2;			/* NB: last mbuf from above */
449 	m1->m_pkthdr.len += m2->m_pkthdr.len;
450 	M_PREPEND(m1, sizeof(uint32_t)+2, M_NOWAIT);
451 	if (m1 == NULL) {		/* XXX cannot happen */
452 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
453 		    "%s: no space for tunnel header\n", __func__);
454 		vap->iv_stats.is_tx_nobuf++;
455 		return NULL;
456 	}
457 	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
458 
459 	M_PREPEND(m1, sizeof(struct llc), M_NOWAIT);
460 	if (m1 == NULL) {		/* XXX cannot happen */
461 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
462 		    "%s: no space for llc header\n", __func__);
463 		vap->iv_stats.is_tx_nobuf++;
464 		return NULL;
465 	}
466 	llc = mtod(m1, struct llc *);
467 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
468 	llc->llc_control = LLC_UI;
469 	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
470 	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
471 	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
472 	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
473 
474 	vap->iv_stats.is_ff_encap++;
475 
476 	return m1;
477 bad:
478 	vap->iv_stats.is_ff_encapfail++;
479 	if (m1 != NULL)
480 		m_freem(m1);
481 	if (m2 != NULL)
482 		m_freem(m2);
483 	return NULL;
484 }
485 
486 /*
487  * A-MSDU encapsulation.
488  *
489  * This assumes just two frames for now, since we're borrowing the
490  * same queuing code and infrastructure as fast-frames.
491  *
492  * There must be two packets chained with m_nextpkt.
493  * We do header adjustment for each, and then concatenate the mbuf chains
494  * to form a single frame for transmission.
495  */
496 struct mbuf *
497 ieee80211_amsdu_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
498 	struct ieee80211_key *key)
499 {
500 	struct mbuf *m2;
501 	struct ether_header eh1, eh2;
502 	struct mbuf *m;
503 	int pad;
504 
505 	m2 = m1->m_nextpkt;
506 	if (m2 == NULL) {
507 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
508 		    "%s: only one frame\n", __func__);
509 		goto bad;
510 	}
511 	m1->m_nextpkt = NULL;
512 
513 	/*
514 	 * Include A-MSDU header in adjusting header layout.
515 	 */
516 	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
517 	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
518 	m1 = ieee80211_mbuf_adjust(vap,
519 		hdrspace + sizeof(struct llc) + sizeof(uint32_t) +
520 		    sizeof(struct ether_header),
521 		key, m1);
522 	if (m1 == NULL) {
523 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
524 		m_freem(m2);
525 		goto bad;
526 	}
527 
528 	/*
529 	 * Copy second frame's Ethernet header out of line
530 	 * and adjust for encapsulation headers.  Note that
531 	 * we make room for padding in case there isn't room
532 	 * at the end of first frame.
533 	 */
534 	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
535 	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
536 	m2 = ieee80211_mbuf_adjust(vap, 4, NULL, m2);
537 	if (m2 == NULL) {
538 		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
539 		goto bad;
540 	}
541 
542 	/*
543 	 * Now do tunnel encapsulation.  First, each
544 	 * frame gets a standard encapsulation.
545 	 */
546 	m1 = ieee80211_ff_encap1(vap, m1, &eh1);
547 	if (m1 == NULL)
548 		goto bad;
549 	m2 = ieee80211_ff_encap1(vap, m2, &eh2);
550 	if (m2 == NULL)
551 		goto bad;
552 
553 	/*
554 	 * Pad leading frame to a 4-byte boundary.  If there
555 	 * is space at the end of the first frame, put it
556 	 * there; otherwise prepend to the front of the second
557 	 * frame.  We know doing the second will always work
558 	 * because we reserve space above.  We prefer appending
559 	 * as this typically has better DMA alignment properties.
560 	 */
561 	for (m = m1; m->m_next != NULL; m = m->m_next)
562 		;
563 	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
564 	if (pad) {
565 		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
566 			m2->m_data -= pad;
567 			m2->m_len += pad;
568 			m2->m_pkthdr.len += pad;
569 		} else {				/* append to first */
570 			m->m_len += pad;
571 			m1->m_pkthdr.len += pad;
572 		}
573 	}
574 
575 	/*
576 	 * Now, stick 'em together.
577 	 */
578 	m->m_next = m2;			/* NB: last mbuf from above */
579 	m1->m_pkthdr.len += m2->m_pkthdr.len;
580 
581 	vap->iv_stats.is_amsdu_encap++;
582 
583 	return m1;
584 bad:
585 	vap->iv_stats.is_amsdu_encapfail++;
586 	if (m1 != NULL)
587 		m_freem(m1);
588 	if (m2 != NULL)
589 		m_freem(m2);
590 	return NULL;
591 }
592 
593 
594 static void
595 ff_transmit(struct ieee80211_node *ni, struct mbuf *m)
596 {
597 	struct ieee80211vap *vap = ni->ni_vap;
598 	struct ieee80211com *ic = ni->ni_ic;
599 
600 	IEEE80211_TX_LOCK_ASSERT(ic);
601 
602 	/* encap and xmit */
603 	m = ieee80211_encap(vap, ni, m);
604 	if (m != NULL)
605 		(void) ieee80211_parent_xmitpkt(ic, m);
606 	else
607 		ieee80211_free_node(ni);
608 }
609 
610 /*
611  * Flush frames to device; note we re-use the linked list
612  * the frames were stored on and use the sentinel (unchanged)
613  * which may be non-NULL.
614  */
615 static void
616 ff_flush(struct mbuf *head, struct mbuf *last)
617 {
618 	struct mbuf *m, *next;
619 	struct ieee80211_node *ni;
620 	struct ieee80211vap *vap;
621 
622 	for (m = head; m != last; m = next) {
623 		next = m->m_nextpkt;
624 		m->m_nextpkt = NULL;
625 
626 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
627 		vap = ni->ni_vap;
628 
629 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
630 		    "%s: flush frame, age %u", __func__, M_AGE_GET(m));
631 		vap->iv_stats.is_ff_flush++;
632 
633 		ff_transmit(ni, m);
634 	}
635 }
636 
637 /*
638  * Age frames on the staging queue.
639  */
640 void
641 ieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq,
642     int quanta)
643 {
644 	struct mbuf *m, *head;
645 	struct ieee80211_node *ni;
646 
647 	IEEE80211_FF_LOCK(ic);
648 	if (sq->depth == 0) {
649 		IEEE80211_FF_UNLOCK(ic);
650 		return;		/* nothing to do */
651 	}
652 
653 	KASSERT(sq->head != NULL, ("stageq empty"));
654 
655 	head = sq->head;
656 	while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) {
657 		int tid = WME_AC_TO_TID(M_WME_GETAC(m));
658 
659 		/* clear staging ref to frame */
660 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
661 		KASSERT(ni->ni_tx_superg[tid] == m, ("staging queue empty"));
662 		ni->ni_tx_superg[tid] = NULL;
663 
664 		sq->head = m->m_nextpkt;
665 		sq->depth--;
666 	}
667 	if (m == NULL)
668 		sq->tail = NULL;
669 	else
670 		M_AGE_SUB(m, quanta);
671 	IEEE80211_FF_UNLOCK(ic);
672 
673 	IEEE80211_TX_LOCK(ic);
674 	ff_flush(head, m);
675 	IEEE80211_TX_UNLOCK(ic);
676 }
677 
678 static void
679 stageq_add(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *m)
680 {
681 	int age = ieee80211_ffagemax;
682 
683 	IEEE80211_FF_LOCK_ASSERT(ic);
684 
685 	if (sq->tail != NULL) {
686 		sq->tail->m_nextpkt = m;
687 		age -= M_AGE_GET(sq->head);
688 	} else {
689 		sq->head = m;
690 
691 		struct timeout_task *qtask = &ic->ic_superg->ff_qtimer;
692 		taskqueue_enqueue_timeout(ic->ic_tq, qtask, age);
693 	}
694 	KASSERT(age >= 0, ("age %d", age));
695 	M_AGE_SET(m, age);
696 	m->m_nextpkt = NULL;
697 	sq->tail = m;
698 	sq->depth++;
699 }
700 
701 static void
702 stageq_remove(struct ieee80211com *ic, struct ieee80211_stageq *sq, struct mbuf *mstaged)
703 {
704 	struct mbuf *m, *mprev;
705 
706 	IEEE80211_FF_LOCK_ASSERT(ic);
707 
708 	mprev = NULL;
709 	for (m = sq->head; m != NULL; m = m->m_nextpkt) {
710 		if (m == mstaged) {
711 			if (mprev == NULL)
712 				sq->head = m->m_nextpkt;
713 			else
714 				mprev->m_nextpkt = m->m_nextpkt;
715 			if (sq->tail == m)
716 				sq->tail = mprev;
717 			sq->depth--;
718 			return;
719 		}
720 		mprev = m;
721 	}
722 	printf("%s: packet not found\n", __func__);
723 }
724 
725 static uint32_t
726 ff_approx_txtime(struct ieee80211_node *ni,
727 	const struct mbuf *m1, const struct mbuf *m2)
728 {
729 	struct ieee80211com *ic = ni->ni_ic;
730 	struct ieee80211vap *vap = ni->ni_vap;
731 	uint32_t framelen;
732 	uint32_t frame_time;
733 
734 	/*
735 	 * Approximate the frame length to be transmitted. A swag to add
736 	 * the following maximal values to the skb payload:
737 	 *   - 32: 802.11 encap + CRC
738 	 *   - 24: encryption overhead (if wep bit)
739 	 *   - 4 + 6: fast-frame header and padding
740 	 *   - 16: 2 LLC FF tunnel headers
741 	 *   - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd)
742 	 */
743 	framelen = m1->m_pkthdr.len + 32 +
744 	    ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR;
745 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
746 		framelen += 24;
747 	if (m2 != NULL)
748 		framelen += m2->m_pkthdr.len;
749 
750 	/*
751 	 * For now, we assume non-shortgi, 20MHz, just because I want to
752 	 * at least test 802.11n.
753 	 */
754 	if (ni->ni_txrate & IEEE80211_RATE_MCS)
755 		frame_time = ieee80211_compute_duration_ht(framelen,
756 		    ni->ni_txrate,
757 		    IEEE80211_HT_RC_2_STREAMS(ni->ni_txrate),
758 		    0, /* isht40 */
759 		    0); /* isshortgi */
760 	else
761 		frame_time = ieee80211_compute_duration(ic->ic_rt, framelen,
762 			    ni->ni_txrate, 0);
763 	return (frame_time);
764 }
765 
766 /*
767  * Check if the supplied frame can be partnered with an existing
768  * or pending frame.  Return a reference to any frame that should be
769  * sent on return; otherwise return NULL.
770  */
771 struct mbuf *
772 ieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m)
773 {
774 	struct ieee80211vap *vap = ni->ni_vap;
775 	struct ieee80211com *ic = ni->ni_ic;
776 	struct ieee80211_superg *sg = ic->ic_superg;
777 	const int pri = M_WME_GETAC(m);
778 	struct ieee80211_stageq *sq;
779 	struct ieee80211_tx_ampdu *tap;
780 	struct mbuf *mstaged;
781 	uint32_t txtime, limit;
782 
783 	IEEE80211_TX_UNLOCK_ASSERT(ic);
784 
785 	IEEE80211_LOCK(ic);
786 	limit = IEEE80211_TXOP_TO_US(
787 	    ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit);
788 	IEEE80211_UNLOCK(ic);
789 
790 	/*
791 	 * Check if the supplied frame can be aggregated.
792 	 *
793 	 * NB: we allow EAPOL frames to be aggregated with other ucast traffic.
794 	 *     Do 802.1x EAPOL frames proceed in the clear? Then they couldn't
795 	 *     be aggregated with other types of frames when encryption is on?
796 	 */
797 	IEEE80211_FF_LOCK(ic);
798 	tap = &ni->ni_tx_ampdu[WME_AC_TO_TID(pri)];
799 	mstaged = ni->ni_tx_superg[WME_AC_TO_TID(pri)];
800 	/* XXX NOTE: reusing packet counter state from A-MPDU */
801 	/*
802 	 * XXX NOTE: this means we're double-counting; it should just
803 	 * be done in ieee80211_output.c once for both superg and A-MPDU.
804 	 */
805 	ieee80211_txampdu_count_packet(tap);
806 
807 	/*
808 	 * When not in station mode never aggregate a multicast
809 	 * frame; this insures, for example, that a combined frame
810 	 * does not require multiple encryption keys.
811 	 */
812 	if (vap->iv_opmode != IEEE80211_M_STA &&
813 	    ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) {
814 		/* XXX flush staged frame? */
815 		IEEE80211_FF_UNLOCK(ic);
816 		return m;
817 	}
818 	/*
819 	 * If there is no frame to combine with and the pps is
820 	 * too low; then do not attempt to aggregate this frame.
821 	 */
822 	if (mstaged == NULL &&
823 	    ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) {
824 		IEEE80211_FF_UNLOCK(ic);
825 		return m;
826 	}
827 	sq = &sg->ff_stageq[pri];
828 	/*
829 	 * Check the txop limit to insure the aggregate fits.
830 	 */
831 	if (limit != 0 &&
832 	    (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) {
833 		/*
834 		 * Aggregate too long, return to the caller for direct
835 		 * transmission.  In addition, flush any pending frame
836 		 * before sending this one.
837 		 */
838 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
839 		    "%s: txtime %u exceeds txop limit %u\n",
840 		    __func__, txtime, limit);
841 
842 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = NULL;
843 		if (mstaged != NULL)
844 			stageq_remove(ic, sq, mstaged);
845 		IEEE80211_FF_UNLOCK(ic);
846 
847 		if (mstaged != NULL) {
848 			IEEE80211_TX_LOCK(ic);
849 			IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
850 			    "%s: flush staged frame", __func__);
851 			/* encap and xmit */
852 			ff_transmit(ni, mstaged);
853 			IEEE80211_TX_UNLOCK(ic);
854 		}
855 		return m;		/* NB: original frame */
856 	}
857 	/*
858 	 * An aggregation candidate.  If there's a frame to partner
859 	 * with then combine and return for processing.  Otherwise
860 	 * save this frame and wait for a partner to show up (or
861 	 * the frame to be flushed).  Note that staged frames also
862 	 * hold their node reference.
863 	 */
864 	if (mstaged != NULL) {
865 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = NULL;
866 		stageq_remove(ic, sq, mstaged);
867 		IEEE80211_FF_UNLOCK(ic);
868 
869 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
870 		    "%s: aggregate fast-frame", __func__);
871 		/*
872 		 * Release the node reference; we only need
873 		 * the one already in mstaged.
874 		 */
875 		KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni,
876 		    ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni));
877 		ieee80211_free_node(ni);
878 
879 		m->m_nextpkt = NULL;
880 		mstaged->m_nextpkt = m;
881 		mstaged->m_flags |= M_FF; /* NB: mark for encap work */
882 	} else {
883 		KASSERT(ni->ni_tx_superg[WME_AC_TO_TID(pri)] == NULL,
884 		    ("ni_tx_superg[]: %p",
885 		    ni->ni_tx_superg[WME_AC_TO_TID(pri)]));
886 		ni->ni_tx_superg[WME_AC_TO_TID(pri)] = m;
887 
888 		stageq_add(ic, sq, m);
889 		IEEE80211_FF_UNLOCK(ic);
890 
891 		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
892 		    "%s: stage frame, %u queued", __func__, sq->depth);
893 		/* NB: mstaged is NULL */
894 	}
895 	return mstaged;
896 }
897 
898 struct mbuf *
899 ieee80211_amsdu_check(struct ieee80211_node *ni, struct mbuf *m)
900 {
901 	/*
902 	 * XXX TODO: actually enforce the node support
903 	 * and HTCAP requirements for the maximum A-MSDU
904 	 * size.
905 	 */
906 
907 	/* First: software A-MSDU transmit? */
908 	if (! ieee80211_amsdu_tx_ok(ni))
909 		return (m);
910 
911 	/* Next - EAPOL? Nope, don't aggregate; we don't QoS encap them */
912 	if (m->m_flags & (M_EAPOL | M_MCAST | M_BCAST))
913 		return (m);
914 
915 	/* Next - needs to be a data frame, non-broadcast, etc */
916 	if (ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost))
917 		return (m);
918 
919 	return (ieee80211_ff_check(ni, m));
920 }
921 
922 void
923 ieee80211_ff_node_init(struct ieee80211_node *ni)
924 {
925 	/*
926 	 * Clean FF state on re-associate.  This handles the case
927 	 * where a station leaves w/o notifying us and then returns
928 	 * before node is reaped for inactivity.
929 	 */
930 	ieee80211_ff_node_cleanup(ni);
931 }
932 
933 void
934 ieee80211_ff_node_cleanup(struct ieee80211_node *ni)
935 {
936 	struct ieee80211com *ic = ni->ni_ic;
937 	struct ieee80211_superg *sg = ic->ic_superg;
938 	struct mbuf *m, *next_m, *head;
939 	int tid;
940 
941 	IEEE80211_FF_LOCK(ic);
942 	head = NULL;
943 	for (tid = 0; tid < WME_NUM_TID; tid++) {
944 		int ac = TID_TO_WME_AC(tid);
945 		/*
946 		 * XXX Initialise the packet counter.
947 		 *
948 		 * This may be double-work for 11n stations;
949 		 * but without it we never setup things.
950 		 */
951 		ieee80211_txampdu_init_pps(&ni->ni_tx_ampdu[tid]);
952 		m = ni->ni_tx_superg[tid];
953 		if (m != NULL) {
954 			ni->ni_tx_superg[tid] = NULL;
955 			stageq_remove(ic, &sg->ff_stageq[ac], m);
956 			m->m_nextpkt = head;
957 			head = m;
958 		}
959 	}
960 	IEEE80211_FF_UNLOCK(ic);
961 
962 	/*
963 	 * Free mbufs, taking care to not dereference the mbuf after
964 	 * we free it (hence grabbing m_nextpkt before we free it.)
965 	 */
966 	m = head;
967 	while (m != NULL) {
968 		next_m = m->m_nextpkt;
969 		m_freem(m);
970 		ieee80211_free_node(ni);
971 		m = next_m;
972 	}
973 }
974 
975 /*
976  * Switch between turbo and non-turbo operating modes.
977  * Use the specified channel flags to locate the new
978  * channel, update 802.11 state, and then call back into
979  * the driver to effect the change.
980  */
981 void
982 ieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags)
983 {
984 	struct ieee80211com *ic = vap->iv_ic;
985 	struct ieee80211_channel *chan;
986 
987 	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
988 	if (chan == NULL) {		/* XXX should not happen */
989 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
990 		    "%s: no channel with freq %u flags 0x%x\n",
991 		    __func__, ic->ic_bsschan->ic_freq, newflags);
992 		return;
993 	}
994 
995 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
996 	    "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
997 	    ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
998 	    ieee80211_phymode_name[ieee80211_chan2mode(chan)],
999 	    chan->ic_freq, chan->ic_flags);
1000 
1001 	ic->ic_bsschan = chan;
1002 	ic->ic_prevchan = ic->ic_curchan;
1003 	ic->ic_curchan = chan;
1004 	ic->ic_rt = ieee80211_get_ratetable(chan);
1005 	ic->ic_set_channel(ic);
1006 	ieee80211_radiotap_chan_change(ic);
1007 	/* NB: do not need to reset ERP state 'cuz we're in sta mode */
1008 }
1009 
1010 /*
1011  * Return the current ``state'' of an Atheros capbility.
1012  * If associated in station mode report the negotiated
1013  * setting. Otherwise report the current setting.
1014  */
1015 static int
1016 getathcap(struct ieee80211vap *vap, int cap)
1017 {
1018 	if (vap->iv_opmode == IEEE80211_M_STA &&
1019 	    vap->iv_state == IEEE80211_S_RUN)
1020 		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
1021 	else
1022 		return (vap->iv_flags & cap) != 0;
1023 }
1024 
1025 static int
1026 superg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
1027 {
1028 	switch (ireq->i_type) {
1029 	case IEEE80211_IOC_FF:
1030 		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
1031 		break;
1032 	case IEEE80211_IOC_TURBOP:
1033 		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
1034 		break;
1035 	default:
1036 		return ENOSYS;
1037 	}
1038 	return 0;
1039 }
1040 IEEE80211_IOCTL_GET(superg, superg_ioctl_get80211);
1041 
1042 static int
1043 superg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
1044 {
1045 	switch (ireq->i_type) {
1046 	case IEEE80211_IOC_FF:
1047 		if (ireq->i_val) {
1048 			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
1049 				return EOPNOTSUPP;
1050 			vap->iv_flags |= IEEE80211_F_FF;
1051 		} else
1052 			vap->iv_flags &= ~IEEE80211_F_FF;
1053 		return ENETRESET;
1054 	case IEEE80211_IOC_TURBOP:
1055 		if (ireq->i_val) {
1056 			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
1057 				return EOPNOTSUPP;
1058 			vap->iv_flags |= IEEE80211_F_TURBOP;
1059 		} else
1060 			vap->iv_flags &= ~IEEE80211_F_TURBOP;
1061 		return ENETRESET;
1062 	default:
1063 		return ENOSYS;
1064 	}
1065 }
1066 IEEE80211_IOCTL_SET(superg, superg_ioctl_set80211);
1067 
1068 #endif	/* IEEE80211_SUPPORT_SUPERG */
1069