ieee80211_superg.c revision 190450
1/*-
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_superg.c 190450 2009-03-26 19:07:56Z sam $");
28
29#include "opt_wlan.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/mbuf.h>
34#include <sys/kernel.h>
35#include <sys/endian.h>
36
37#include <sys/socket.h>
38
39#include <net/bpf.h>
40#include <net/ethernet.h>
41#include <net/if.h>
42#include <net/if_llc.h>
43#include <net/if_media.h>
44
45#include <net80211/ieee80211_var.h>
46#include <net80211/ieee80211_input.h>
47#include <net80211/ieee80211_phy.h>
48#include <net80211/ieee80211_superg.h>
49
50#define	ETHER_HEADER_COPY(dst, src) \
51	memcpy(dst, src, sizeof(struct ether_header))
52
53void
54ieee80211_superg_attach(struct ieee80211com *ic)
55{
56}
57
58void
59ieee80211_superg_detach(struct ieee80211com *ic)
60{
61}
62
63void
64ieee80211_superg_vattach(struct ieee80211vap *vap)
65{
66	if (vap->iv_caps & IEEE80211_C_FF)
67		vap->iv_flags |= IEEE80211_F_FF;
68	/* NB: we only implement sta mode */
69	if (vap->iv_opmode == IEEE80211_M_STA &&
70	    (vap->iv_caps & IEEE80211_C_TURBOP))
71		vap->iv_flags |= IEEE80211_F_TURBOP;
72}
73
74void
75ieee80211_superg_vdetach(struct ieee80211vap *vap)
76{
77}
78
79#define	ATH_OUI_BYTES		0x00, 0x03, 0x7f
80/*
81 * Add a WME information element to a frame.
82 */
83uint8_t *
84ieee80211_add_ath(uint8_t *frm, uint8_t caps, uint16_t defkeyix)
85{
86	static const struct ieee80211_ath_ie info = {
87		.ath_id		= IEEE80211_ELEMID_VENDOR,
88		.ath_len	= sizeof(struct ieee80211_ath_ie) - 2,
89		.ath_oui	= { ATH_OUI_BYTES },
90		.ath_oui_type	= ATH_OUI_TYPE,
91		.ath_oui_subtype= ATH_OUI_SUBTYPE,
92		.ath_version	= ATH_OUI_VERSION,
93	};
94	struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm;
95
96	memcpy(frm, &info, sizeof(info));
97	ath->ath_capability = caps;
98	ath->ath_defkeyix[0] = (defkeyix & 0xff);
99	ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff);
100	return frm + sizeof(info);
101}
102#undef ATH_OUI_BYTES
103
104void
105ieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie)
106{
107	const struct ieee80211_ath_ie *ath =
108		(const struct ieee80211_ath_ie *) ie;
109
110	ni->ni_ath_flags = ath->ath_capability;
111	ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix);
112}
113
114int
115ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
116	const struct ieee80211_frame *wh)
117{
118	struct ieee80211vap *vap = ni->ni_vap;
119	const struct ieee80211_ath_ie *ath;
120	u_int len = frm[1];
121	int capschanged;
122	uint16_t defkeyix;
123
124	if (len < sizeof(struct ieee80211_ath_ie)-2) {
125		IEEE80211_DISCARD_IE(vap,
126		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
127		    wh, "Atheros", "too short, len %u", len);
128		return -1;
129	}
130	ath = (const struct ieee80211_ath_ie *)frm;
131	capschanged = (ni->ni_ath_flags != ath->ath_capability);
132	defkeyix = LE_READ_2(ath->ath_defkeyix);
133	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
134		ni->ni_ath_flags = ath->ath_capability;
135		ni->ni_ath_defkeyix = defkeyix;
136		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
137		    "ath ie change: new caps 0x%x defkeyix 0x%x",
138		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
139	}
140	if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) {
141		uint16_t curflags, newflags;
142
143		/*
144		 * Check for turbo mode switch.  Calculate flags
145		 * for the new mode and effect the switch.
146		 */
147		newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags;
148		/* NB: BOOST is not in ic_flags, so get it from the ie */
149		if (ath->ath_capability & ATHEROS_CAP_BOOST)
150			newflags |= IEEE80211_CHAN_TURBO;
151		else
152			newflags &= ~IEEE80211_CHAN_TURBO;
153		if (newflags != curflags)
154			ieee80211_dturbo_switch(vap, newflags);
155	}
156	return capschanged;
157}
158
159/*
160 * Decap the encapsulated frame pair and dispatch the first
161 * for delivery.  The second frame is returned for delivery
162 * via the normal path.
163 */
164struct mbuf *
165ieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m)
166{
167#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
168#define	MS(x,f)	(((x) & f) >> f##_S)
169	struct ieee80211vap *vap = ni->ni_vap;
170	struct llc *llc;
171	uint32_t ath;
172	struct mbuf *n;
173	int framelen;
174
175	/* NB: we assume caller does this check for us */
176	KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF),
177	    ("ff not negotiated"));
178	/*
179	 * Check for fast-frame tunnel encapsulation.
180	 */
181	if (m->m_pkthdr.len < 3*FF_LLC_SIZE)
182		return m;
183	if (m->m_len < FF_LLC_SIZE &&
184	    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
185		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
186		    ni->ni_macaddr, "fast-frame",
187		    "%s", "m_pullup(llc) failed");
188		vap->iv_stats.is_rx_tooshort++;
189		return NULL;
190	}
191	llc = (struct llc *)(mtod(m, uint8_t *) +
192	    sizeof(struct ether_header));
193	if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE))
194		return m;
195	m_adj(m, FF_LLC_SIZE);
196	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
197	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
198		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
199		    ni->ni_macaddr, "fast-frame",
200		    "unsupport tunnel protocol, header 0x%x", ath);
201		vap->iv_stats.is_ff_badhdr++;
202		m_freem(m);
203		return NULL;
204	}
205	/* NB: skip header and alignment padding */
206	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
207
208	vap->iv_stats.is_ff_decap++;
209
210	/*
211	 * Decap the first frame, bust it apart from the
212	 * second and deliver; then decap the second frame
213	 * and return it to the caller for normal delivery.
214	 */
215	m = ieee80211_decap1(m, &framelen);
216	if (m == NULL) {
217		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
218		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
219		vap->iv_stats.is_ff_tooshort++;
220		return NULL;
221	}
222	n = m_split(m, framelen, M_NOWAIT);
223	if (n == NULL) {
224		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
225		    ni->ni_macaddr, "fast-frame",
226		    "%s", "unable to split encapsulated frames");
227		vap->iv_stats.is_ff_split++;
228		m_freem(m);			/* NB: must reclaim */
229		return NULL;
230	}
231	/* XXX not right for WDS */
232	vap->iv_deliver_data(vap, ni, m);	/* 1st of pair */
233
234	/*
235	 * Decap second frame.
236	 */
237	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
238	n = ieee80211_decap1(n, &framelen);
239	if (n == NULL) {
240		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
241		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
242		vap->iv_stats.is_ff_tooshort++;
243	}
244	/* XXX verify framelen against mbuf contents */
245	return n;				/* 2nd delivered by caller */
246#undef MS
247#undef FF_LLC_SIZE
248}
249
250/*
251 * Do Ethernet-LLC encapsulation for each payload in a fast frame
252 * tunnel encapsulation.  The frame is assumed to have an Ethernet
253 * header at the front that must be stripped before prepending the
254 * LLC followed by the Ethernet header passed in (with an Ethernet
255 * type that specifies the payload size).
256 */
257static struct mbuf *
258ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
259	const struct ether_header *eh)
260{
261	struct llc *llc;
262	uint16_t payload;
263
264	/* XXX optimize by combining m_adj+M_PREPEND */
265	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
266	llc = mtod(m, struct llc *);
267	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
268	llc->llc_control = LLC_UI;
269	llc->llc_snap.org_code[0] = 0;
270	llc->llc_snap.org_code[1] = 0;
271	llc->llc_snap.org_code[2] = 0;
272	llc->llc_snap.ether_type = eh->ether_type;
273	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
274
275	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
276	if (m == NULL) {		/* XXX cannot happen */
277		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
278			"%s: no space for ether_header\n", __func__);
279		vap->iv_stats.is_tx_nobuf++;
280		return NULL;
281	}
282	ETHER_HEADER_COPY(mtod(m, void *), eh);
283	mtod(m, struct ether_header *)->ether_type = htons(payload);
284	return m;
285}
286
287/*
288 * Fast frame encapsulation.  There must be two packets
289 * chained with m_nextpkt.  We do header adjustment for
290 * each, add the tunnel encapsulation, and then concatenate
291 * the mbuf chains to form a single frame for transmission.
292 */
293struct mbuf *
294ieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
295	struct ieee80211_key *key)
296{
297	struct mbuf *m2;
298	struct ether_header eh1, eh2;
299	struct llc *llc;
300	struct mbuf *m;
301	int pad;
302
303	m2 = m1->m_nextpkt;
304	if (m2 == NULL) {
305		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
306		    "%s: only one frame\n", __func__);
307		goto bad;
308	}
309	m1->m_nextpkt = NULL;
310	/*
311	 * Include fast frame headers in adjusting header
312	 * layout; this allocates space according to what
313	 * ff_encap will do.
314	 */
315	m1 = ieee80211_mbuf_adjust(vap,
316		hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 +
317		    sizeof(struct ether_header),
318		key, m1);
319	if (m1 == NULL) {
320		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
321		m_freem(m2);
322		goto bad;
323	}
324
325	/*
326	 * Copy second frame's Ethernet header out of line
327	 * and adjust for encapsulation headers.  Note that
328	 * we make room for padding in case there isn't room
329	 * at the end of first frame.
330	 */
331	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
332	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
333	m2 = ieee80211_mbuf_adjust(vap,
334		ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header),
335		NULL, m2);
336	if (m2 == NULL) {
337		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
338		goto bad;
339	}
340
341	/*
342	 * Now do tunnel encapsulation.  First, each
343	 * frame gets a standard encapsulation.
344	 */
345	m1 = ff_encap1(vap, m1, &eh1);
346	if (m1 == NULL)
347		goto bad;
348	m2 = ff_encap1(vap, m2, &eh2);
349	if (m2 == NULL)
350		goto bad;
351
352	/*
353	 * Pad leading frame to a 4-byte boundary.  If there
354	 * is space at the end of the first frame, put it
355	 * there; otherwise prepend to the front of the second
356	 * frame.  We know doing the second will always work
357	 * because we reserve space above.  We prefer appending
358	 * as this typically has better DMA alignment properties.
359	 */
360	for (m = m1; m->m_next != NULL; m = m->m_next)
361		;
362	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
363	if (pad) {
364		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
365			m2->m_data -= pad;
366			m2->m_len += pad;
367			m2->m_pkthdr.len += pad;
368		} else {				/* append to first */
369			m->m_len += pad;
370			m1->m_pkthdr.len += pad;
371		}
372	}
373
374	/*
375	 * Now, stick 'em together and prepend the tunnel headers;
376	 * first the Atheros tunnel header (all zero for now) and
377	 * then a special fast frame LLC.
378	 *
379	 * XXX optimize by prepending together
380	 */
381	m->m_next = m2;			/* NB: last mbuf from above */
382	m1->m_pkthdr.len += m2->m_pkthdr.len;
383	M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT);
384	if (m1 == NULL) {		/* XXX cannot happen */
385		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
386		    "%s: no space for tunnel header\n", __func__);
387		vap->iv_stats.is_tx_nobuf++;
388		return NULL;
389	}
390	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
391
392	M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT);
393	if (m1 == NULL) {		/* XXX cannot happen */
394		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
395		    "%s: no space for llc header\n", __func__);
396		vap->iv_stats.is_tx_nobuf++;
397		return NULL;
398	}
399	llc = mtod(m1, struct llc *);
400	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
401	llc->llc_control = LLC_UI;
402	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
403	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
404	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
405	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
406
407	vap->iv_stats.is_ff_encap++;
408
409	return m1;
410bad:
411	if (m1 != NULL)
412		m_freem(m1);
413	if (m2 != NULL)
414		m_freem(m2);
415	return NULL;
416}
417
418/*
419 * Switch between turbo and non-turbo operating modes.
420 * Use the specified channel flags to locate the new
421 * channel, update 802.11 state, and then call back into
422 * the driver to effect the change.
423 */
424void
425ieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags)
426{
427	struct ieee80211com *ic = vap->iv_ic;
428	struct ieee80211_channel *chan;
429
430	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
431	if (chan == NULL) {		/* XXX should not happen */
432		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
433		    "%s: no channel with freq %u flags 0x%x\n",
434		    __func__, ic->ic_bsschan->ic_freq, newflags);
435		return;
436	}
437
438	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
439	    "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
440	    ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
441	    ieee80211_phymode_name[ieee80211_chan2mode(chan)],
442	    chan->ic_freq, chan->ic_flags);
443
444	ic->ic_bsschan = chan;
445	ic->ic_prevchan = ic->ic_curchan;
446	ic->ic_curchan = chan;
447	ic->ic_set_channel(ic);
448	/* NB: do not need to reset ERP state 'cuz we're in sta mode */
449}
450
451/*
452 * Return the current ``state'' of an Atheros capbility.
453 * If associated in station mode report the negotiated
454 * setting. Otherwise report the current setting.
455 */
456static int
457getathcap(struct ieee80211vap *vap, int cap)
458{
459	if (vap->iv_opmode == IEEE80211_M_STA &&
460	    vap->iv_state == IEEE80211_S_RUN)
461		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
462	else
463		return (vap->iv_flags & cap) != 0;
464}
465
466static int
467superg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
468{
469	switch (ireq->i_type) {
470	case IEEE80211_IOC_FF:
471		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
472		break;
473	case IEEE80211_IOC_TURBOP:
474		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
475		break;
476	default:
477		return ENOSYS;
478	}
479	return 0;
480}
481IEEE80211_IOCTL_GET(superg, superg_ioctl_get80211);
482
483static int
484superg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
485{
486	switch (ireq->i_type) {
487	case IEEE80211_IOC_FF:
488		if (ireq->i_val) {
489			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
490				return EOPNOTSUPP;
491			vap->iv_flags |= IEEE80211_F_FF;
492		} else
493			vap->iv_flags &= ~IEEE80211_F_FF;
494		return ERESTART;
495	case IEEE80211_IOC_TURBOP:
496		if (ireq->i_val) {
497			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
498				return EOPNOTSUPP;
499			vap->iv_flags |= IEEE80211_F_TURBOP;
500		} else
501			vap->iv_flags &= ~IEEE80211_F_TURBOP;
502		return ENETRESET;
503	default:
504		return ENOSYS;
505	}
506	return 0;
507}
508IEEE80211_IOCTL_SET(superg, superg_ioctl_set80211);
509