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