xref: /freebsd/sys/net80211/ieee80211_radiotap.c (revision 5463c4a4)
1 /*-
2  * Copyright (c) 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$");
28 
29 /*
30  * IEEE 802.11 radiotap support.
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <sys/malloc.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 
41 #include <sys/socket.h>
42 
43 #include <net/bpf.h>
44 #include <net/if.h>
45 #include <net/if_llc.h>
46 #include <net/if_media.h>
47 
48 #include <net80211/ieee80211_var.h>
49 
50 static int radiotap_offset(struct ieee80211_radiotap_header *, int);
51 
52 void
53 ieee80211_radiotap_attach(struct ieee80211com *ic,
54 	struct ieee80211_radiotap_header *th, int tlen, uint32_t tx_radiotap,
55 	struct ieee80211_radiotap_header *rh, int rlen, uint32_t rx_radiotap)
56 {
57 #define	B(_v)	(1<<(_v))
58 	int off;
59 
60 	th->it_len = htole16(roundup2(tlen, sizeof(uint32_t)));
61 	th->it_present = htole32(tx_radiotap);
62 	ic->ic_th = th;
63 	/* calculate offset to channel data */
64 	off = -1;
65 	if (tx_radiotap & B(IEEE80211_RADIOTAP_CHANNEL))
66 		off = radiotap_offset(th, IEEE80211_RADIOTAP_CHANNEL);
67 	else if (tx_radiotap & B(IEEE80211_RADIOTAP_XCHANNEL))
68 		off = radiotap_offset(th, IEEE80211_RADIOTAP_XCHANNEL);
69 	if (off == -1) {
70 		if_printf(ic->ic_ifp, "%s: no tx channel, radiotap 0x%x",
71 		    __func__, tx_radiotap);
72 		/* NB: we handle this case but data will have no chan spec */
73 	} else
74 		ic->ic_txchan = ((uint8_t *) th) + off;
75 
76 	rh->it_len = htole16(roundup2(rlen, sizeof(uint32_t)));
77 	rh->it_present = htole32(rx_radiotap);
78 	ic->ic_rh = rh;
79 	/* calculate offset to channel data */
80 	off = -1;
81 	if (rx_radiotap & B(IEEE80211_RADIOTAP_CHANNEL))
82 		off = radiotap_offset(rh, IEEE80211_RADIOTAP_CHANNEL);
83 	else if (rx_radiotap & B(IEEE80211_RADIOTAP_XCHANNEL))
84 		off = radiotap_offset(rh, IEEE80211_RADIOTAP_XCHANNEL);
85 	if (off == -1) {
86 		if_printf(ic->ic_ifp, "%s: no rx channel, radiotap 0x%x",
87 		    __func__, rx_radiotap);
88 		/* NB: we handle this case but data will have no chan spec */
89 	} else
90 		ic->ic_rxchan = ((uint8_t *) rh) + off;
91 #undef B
92 }
93 
94 void
95 ieee80211_radiotap_detach(struct ieee80211com *ic)
96 {
97 }
98 
99 void
100 ieee80211_radiotap_vattach(struct ieee80211vap *vap)
101 {
102 	struct ieee80211com *ic = vap->iv_ic;
103 	struct ieee80211_radiotap_header *th = ic->ic_th;
104 
105 	KASSERT(th != NULL, ("no radiotap setup"));
106 
107 	/* radiotap DLT for raw 802.11 frames */
108 	bpfattach2(vap->iv_ifp, DLT_IEEE802_11_RADIO,
109 	    sizeof(struct ieee80211_frame) + le16toh(th->it_len),
110 	    &vap->iv_rawbpf);
111 }
112 
113 void
114 ieee80211_radiotap_vdetach(struct ieee80211vap *vap)
115 {
116 	/* NB: bpfattach is called by ether_ifdetach and claims all taps */
117 }
118 
119 static void
120 set_channel(void *p, const struct ieee80211_channel *c)
121 {
122 	struct {
123 		uint16_t	freq;
124 		uint16_t	flags;
125 	} *rc = p;
126 
127 	rc->freq = htole16(c->ic_freq);
128 	rc->flags = htole16(c->ic_flags);
129 }
130 
131 static void
132 set_xchannel(void *p, const struct ieee80211_channel *c)
133 {
134 	struct {
135 		uint32_t	flags;
136 		uint16_t	freq;
137 		uint8_t		ieee;
138 		uint8_t		maxpow;
139 	} *rc = p;
140 
141 	rc->flags = htole32(c->ic_flags);
142 	rc->freq = htole16(c->ic_freq);
143 	rc->ieee = c->ic_ieee;
144 	rc->maxpow = c->ic_maxregpower;
145 }
146 
147 /*
148  * Update radiotap state on channel change.
149  */
150 void
151 ieee80211_radiotap_chan_change(struct ieee80211com *ic)
152 {
153 	if (ic->ic_rxchan != NULL) {
154 		struct ieee80211_radiotap_header *rh = ic->ic_rh;
155 
156 		if (rh->it_present & (1<<IEEE80211_RADIOTAP_XCHANNEL))
157 			set_xchannel(ic->ic_rxchan, ic->ic_curchan);
158 		else if (rh->it_present & (1<<IEEE80211_RADIOTAP_CHANNEL))
159 			set_channel(ic->ic_rxchan, ic->ic_curchan);
160 	}
161 	if (ic->ic_txchan != NULL) {
162 		struct ieee80211_radiotap_header *th = ic->ic_th;
163 
164 		if (th->it_present & (1<<IEEE80211_RADIOTAP_XCHANNEL))
165 			set_xchannel(ic->ic_txchan, ic->ic_curchan);
166 		else if (th->it_present & (1<<IEEE80211_RADIOTAP_CHANNEL))
167 			set_channel(ic->ic_txchan, ic->ic_curchan);
168 	}
169 }
170 
171 static void
172 dispatch_radiotap(struct ieee80211vap *vap0, struct mbuf *m,
173 	struct ieee80211_radiotap_header *rh)
174 {
175 	struct ieee80211com *ic = vap0->iv_ic;
176 	int len = le16toh(rh->it_len);
177 
178 	if (ieee80211_radiotap_active_vap(vap0))
179 		bpf_mtap2(vap0->iv_rawbpf, rh, len, m);
180 	if (ic->ic_monvaps) {
181 		struct ieee80211vap *vap;
182 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
183 			if (vap->iv_opmode == IEEE80211_M_MONITOR &&
184 			    vap != vap0 && ieee80211_radiotap_active_vap(vap))
185 				bpf_mtap2(vap->iv_rawbpf, rh, len, m);
186 		}
187 	}
188 }
189 
190 /*
191  * Dispatch radiotap data for transmitted packet.
192  */
193 void
194 ieee80211_radiotap_tx(struct ieee80211vap *vap0, struct mbuf *m)
195 {
196 	dispatch_radiotap(vap0, m, vap0->iv_ic->ic_th);
197 }
198 
199 /*
200  * Dispatch radiotap data for received packet.
201  */
202 void
203 ieee80211_radiotap_rx(struct ieee80211vap *vap0, struct mbuf *m)
204 {
205 	dispatch_radiotap(vap0, m, vap0->iv_ic->ic_rh);
206 }
207 
208 /*
209  * Dispatch radiotap data for a packet received outside the normal
210  * rx processing path; this is used, for example, to handle frames
211  * received with errors that would otherwise be dropped.
212  */
213 void
214 ieee80211_radiotap_rx_all(struct ieee80211com *ic, struct mbuf *m)
215 {
216 	struct ieee80211_radiotap_header *rh = ic->ic_rh;
217 	int len = le16toh(rh->it_len);
218 	struct ieee80211vap *vap;
219 
220 	/* XXX locking? */
221 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
222 		if (ieee80211_radiotap_active_vap(vap))
223 			bpf_mtap2(vap->iv_rawbpf, rh, len, m);
224 	}
225 }
226 
227 /*
228  * Return the offset of the specified item in the radiotap
229  * header description.  If the item is not present or is not
230  * known -1 is returned.
231  */
232 static int
233 radiotap_offset(struct ieee80211_radiotap_header *rh, int item)
234 {
235 	static const struct {
236 		size_t	align, width;
237 	} items[] = {
238 		[IEEE80211_RADIOTAP_TSFT] = {
239 		    .align	= sizeof(uint64_t),
240 		    .width	= sizeof(uint64_t),
241 		},
242 		[IEEE80211_RADIOTAP_FLAGS] = {
243 		    .align	= sizeof(uint8_t),
244 		    .width	= sizeof(uint8_t),
245 		},
246 		[IEEE80211_RADIOTAP_RATE] = {
247 		    .align	= sizeof(uint8_t),
248 		    .width	= sizeof(uint8_t),
249 		},
250 		[IEEE80211_RADIOTAP_CHANNEL] = {
251 		    .align	= sizeof(uint16_t),
252 		    .width	= 2*sizeof(uint16_t),
253 		},
254 		[IEEE80211_RADIOTAP_FHSS] = {
255 		    .align	= sizeof(uint16_t),
256 		    .width	= sizeof(uint16_t),
257 		},
258 		[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = {
259 		    .align	= sizeof(uint8_t),
260 		    .width	= sizeof(uint8_t),
261 		},
262 		[IEEE80211_RADIOTAP_DBM_ANTNOISE] = {
263 		    .align	= sizeof(uint8_t),
264 		    .width	= sizeof(uint8_t),
265 		},
266 		[IEEE80211_RADIOTAP_LOCK_QUALITY] = {
267 		    .align	= sizeof(uint16_t),
268 		    .width	= sizeof(uint16_t),
269 		},
270 		[IEEE80211_RADIOTAP_TX_ATTENUATION] = {
271 		    .align	= sizeof(uint16_t),
272 		    .width	= sizeof(uint16_t),
273 		},
274 		[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = {
275 		    .align	= sizeof(uint16_t),
276 		    .width	= sizeof(uint16_t),
277 		},
278 		[IEEE80211_RADIOTAP_DBM_TX_POWER] = {
279 		    .align	= sizeof(uint8_t),
280 		    .width	= sizeof(uint8_t),
281 		},
282 		[IEEE80211_RADIOTAP_ANTENNA] = {
283 		    .align	= sizeof(uint8_t),
284 		    .width	= sizeof(uint8_t),
285 		},
286 		[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = {
287 		    .align	= sizeof(uint8_t),
288 		    .width	= sizeof(uint8_t),
289 		},
290 		[IEEE80211_RADIOTAP_DB_ANTNOISE] = {
291 		    .align	= sizeof(uint8_t),
292 		    .width	= sizeof(uint8_t),
293 		},
294 		[IEEE80211_RADIOTAP_XCHANNEL] = {
295 		    .align	= sizeof(uint32_t),
296 		    .width	= 2*sizeof(uint32_t),
297 		},
298 	};
299 	uint32_t present = le32toh(rh->it_present);
300 	int off, i;
301 
302 	off = sizeof(struct ieee80211_radiotap_header);
303 	for (i = 0; i < IEEE80211_RADIOTAP_EXT; i++) {
304 		if ((present & (1<<i)) == 0)
305 			continue;
306 		if (items[i].align == 0) {
307 			/* NB: unidentified element, don't guess */
308 			printf("%s: unknown item %d\n", __func__, i);
309 			return -1;
310 		}
311 		off = roundup2(off, items[i].align);
312 		if (i == item) {
313 			if (off + items[i].width > le16toh(rh->it_len)) {
314 				/* NB: item does not fit in header data */
315 				printf("%s: item %d not in header data, "
316 				    "off %d width %zu len %d\n", __func__, i,
317 				    off, items[i].width, le16toh(rh->it_len));
318 				return -1;
319 			}
320 			return off;
321 		}
322 		off += items[i].width;
323 	}
324 	return -1;
325 }
326