1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*-
* Copyright (c) 2009 Sam Leffler, Errno Consulting
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* IEEE 802.11 radiotap support.
*/
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_llc.h>
#include <net/if_media.h>
#include <net80211/ieee80211_var.h>
static int radiotap_offset(struct ieee80211_radiotap_header *, int, int);
void
ieee80211_radiotap_attach(struct ieee80211com *ic,
struct ieee80211_radiotap_header *th, int tlen, uint32_t tx_radiotap,
struct ieee80211_radiotap_header *rh, int rlen, uint32_t rx_radiotap)
{
ieee80211_radiotap_attachv(ic, th, tlen, 0, tx_radiotap,
rh, rlen, 0, rx_radiotap);
}
void
ieee80211_radiotap_attachv(struct ieee80211com *ic,
struct ieee80211_radiotap_header *th,
int tlen, int n_tx_v, uint32_t tx_radiotap,
struct ieee80211_radiotap_header *rh,
int rlen, int n_rx_v, uint32_t rx_radiotap)
{
#define B(_v) (1<<(_v))
int off;
th->it_len = htole16(roundup2(tlen, sizeof(uint32_t)));
th->it_present = htole32(tx_radiotap);
ic->ic_th = th;
/* calculate offset to channel data */
off = -1;
if (tx_radiotap & B(IEEE80211_RADIOTAP_CHANNEL))
off = radiotap_offset(th, n_tx_v, IEEE80211_RADIOTAP_CHANNEL);
else if (tx_radiotap & B(IEEE80211_RADIOTAP_XCHANNEL))
off = radiotap_offset(th, n_tx_v, IEEE80211_RADIOTAP_XCHANNEL);
if (off == -1) {
if_printf(ic->ic_ifp, "%s: no tx channel, radiotap 0x%x\n",
__func__, tx_radiotap);
/* NB: we handle this case but data will have no chan spec */
} else
ic->ic_txchan = ((uint8_t *) th) + off;
rh->it_len = htole16(roundup2(rlen, sizeof(uint32_t)));
rh->it_present = htole32(rx_radiotap);
ic->ic_rh = rh;
/* calculate offset to channel data */
off = -1;
if (rx_radiotap & B(IEEE80211_RADIOTAP_CHANNEL))
off = radiotap_offset(rh, n_rx_v, IEEE80211_RADIOTAP_CHANNEL);
else if (rx_radiotap & B(IEEE80211_RADIOTAP_XCHANNEL))
off = radiotap_offset(rh, n_rx_v, IEEE80211_RADIOTAP_XCHANNEL);
if (off == -1) {
if_printf(ic->ic_ifp, "%s: no rx channel, radiotap 0x%x\n",
__func__, rx_radiotap);
/* NB: we handle this case but data will have no chan spec */
} else
ic->ic_rxchan = ((uint8_t *) rh) + off;
#undef B
}
void
ieee80211_radiotap_detach(struct ieee80211com *ic)
{
}
void
ieee80211_radiotap_vattach(struct ieee80211vap *vap)
{
struct ieee80211com *ic = vap->iv_ic;
struct ieee80211_radiotap_header *th = ic->ic_th;
if (th != NULL && ic->ic_rh != NULL) {
/* radiotap DLT for raw 802.11 frames */
bpfattach2(vap->iv_ifp, DLT_IEEE802_11_RADIO,
sizeof(struct ieee80211_frame) + le16toh(th->it_len),
&vap->iv_rawbpf);
}
}
void
ieee80211_radiotap_vdetach(struct ieee80211vap *vap)
{
/* NB: bpfattach is called by ether_ifdetach and claims all taps */
}
static void
set_channel(void *p, const struct ieee80211_channel *c)
{
struct {
uint16_t freq;
uint16_t flags;
} *rc = p;
rc->freq = htole16(c->ic_freq);
rc->flags = htole16(c->ic_flags);
}
static void
set_xchannel(void *p, const struct ieee80211_channel *c)
{
struct {
uint32_t flags;
uint16_t freq;
uint8_t ieee;
uint8_t maxpow;
} *rc = p;
rc->flags = htole32(c->ic_flags);
rc->freq = htole16(c->ic_freq);
rc->ieee = c->ic_ieee;
rc->maxpow = c->ic_maxregpower;
}
/*
* Update radiotap state on channel change.
*/
void
ieee80211_radiotap_chan_change(struct ieee80211com *ic)
{
if (ic->ic_rxchan != NULL) {
struct ieee80211_radiotap_header *rh = ic->ic_rh;
if (rh->it_present & htole32(1<<IEEE80211_RADIOTAP_XCHANNEL))
set_xchannel(ic->ic_rxchan, ic->ic_curchan);
else if (rh->it_present & htole32(1<<IEEE80211_RADIOTAP_CHANNEL))
set_channel(ic->ic_rxchan, ic->ic_curchan);
}
if (ic->ic_txchan != NULL) {
struct ieee80211_radiotap_header *th = ic->ic_th;
if (th->it_present & htole32(1<<IEEE80211_RADIOTAP_XCHANNEL))
set_xchannel(ic->ic_txchan, ic->ic_curchan);
else if (th->it_present & htole32(1<<IEEE80211_RADIOTAP_CHANNEL))
set_channel(ic->ic_txchan, ic->ic_curchan);
}
}
/*
* Distribute radiotap data (+packet) to all monitor mode
* vaps with an active tap other than vap0.
*/
static void
spam_vaps(struct ieee80211vap *vap0, struct mbuf *m,
struct ieee80211_radiotap_header *rh, int len)
{
struct ieee80211com *ic = vap0->iv_ic;
struct ieee80211vap *vap;
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (vap != vap0 &&
vap->iv_opmode == IEEE80211_M_MONITOR &&
(vap->iv_flags_ext & IEEE80211_FEXT_BPF) &&
vap->iv_state != IEEE80211_S_INIT)
bpf_mtap2(vap->iv_rawbpf, rh, len, m);
}
}
/*
* Dispatch radiotap data for transmitted packet.
*/
void
ieee80211_radiotap_tx(struct ieee80211vap *vap0, struct mbuf *m)
{
struct ieee80211com *ic = vap0->iv_ic;
struct ieee80211_radiotap_header *th = ic->ic_th;
int len;
KASSERT(th != NULL, ("no tx radiotap header"));
len = le16toh(th->it_len);
if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF)
bpf_mtap2(vap0->iv_rawbpf, th, len, m);
/*
* Spam monitor mode vaps.
*/
if (ic->ic_montaps != 0)
spam_vaps(vap0, m, th, len);
}
/*
* Dispatch radiotap data for received packet.
*/
void
ieee80211_radiotap_rx(struct ieee80211vap *vap0, struct mbuf *m)
{
struct ieee80211com *ic = vap0->iv_ic;
struct ieee80211_radiotap_header *rh = ic->ic_rh;
int len;
KASSERT(rh != NULL, ("no rx radiotap header"));
len = le16toh(rh->it_len);
if (vap0->iv_flags_ext & IEEE80211_FEXT_BPF)
bpf_mtap2(vap0->iv_rawbpf, rh, len, m);
/*
* Spam monitor mode vaps with unicast frames. Multicast
* frames are handled by passing through ieee80211_input_all
* which distributes copies to the monitor mode vaps.
*/
if (ic->ic_montaps != 0 && (m->m_flags & M_BCAST) == 0)
spam_vaps(vap0, m, rh, len);
}
/*
* Dispatch radiotap data for a packet received outside the normal
* rx processing path; this is used, for example, to handle frames
* received with errors that would otherwise be dropped.
*/
void
ieee80211_radiotap_rx_all(struct ieee80211com *ic, struct mbuf *m)
{
struct ieee80211_radiotap_header *rh = ic->ic_rh;
int len = le16toh(rh->it_len);
struct ieee80211vap *vap;
/* XXX locking? */
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
if (ieee80211_radiotap_active_vap(vap) &&
vap->iv_state != IEEE80211_S_INIT)
bpf_mtap2(vap->iv_rawbpf, rh, len, m);
}
}
/*
* Return the offset of the specified item in the radiotap
* header description. If the item is not present or is not
* known -1 is returned.
*/
static int
radiotap_offset(struct ieee80211_radiotap_header *rh,
int n_vendor_attributes, int item)
{
static const struct {
size_t align, width;
} items[] = {
[IEEE80211_RADIOTAP_TSFT] = {
.align = sizeof(uint64_t),
.width = sizeof(uint64_t),
},
[IEEE80211_RADIOTAP_FLAGS] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_RATE] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_CHANNEL] = {
.align = sizeof(uint16_t),
.width = 2*sizeof(uint16_t),
},
[IEEE80211_RADIOTAP_FHSS] = {
.align = sizeof(uint16_t),
.width = sizeof(uint16_t),
},
[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_DBM_ANTNOISE] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_LOCK_QUALITY] = {
.align = sizeof(uint16_t),
.width = sizeof(uint16_t),
},
[IEEE80211_RADIOTAP_TX_ATTENUATION] = {
.align = sizeof(uint16_t),
.width = sizeof(uint16_t),
},
[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = {
.align = sizeof(uint16_t),
.width = sizeof(uint16_t),
},
[IEEE80211_RADIOTAP_DBM_TX_POWER] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_ANTENNA] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_DB_ANTNOISE] = {
.align = sizeof(uint8_t),
.width = sizeof(uint8_t),
},
[IEEE80211_RADIOTAP_XCHANNEL] = {
.align = sizeof(uint32_t),
.width = 2*sizeof(uint32_t),
},
[IEEE80211_RADIOTAP_MCS] = {
.align = sizeof(uint8_t),
.width = 3*sizeof(uint8_t),
},
};
uint32_t present = le32toh(rh->it_present);
int off, i;
off = sizeof(struct ieee80211_radiotap_header);
off += n_vendor_attributes * (sizeof(uint32_t));
for (i = 0; i < IEEE80211_RADIOTAP_EXT; i++) {
if ((present & (1<<i)) == 0)
continue;
if (items[i].align == 0) {
/* NB: unidentified element, don't guess */
printf("%s: unknown item %d\n", __func__, i);
return -1;
}
off = roundup2(off, items[i].align);
if (i == item) {
if (off + items[i].width > le16toh(rh->it_len)) {
/* NB: item does not fit in header data */
printf("%s: item %d not in header data, "
"off %d width %zu len %d\n", __func__, i,
off, items[i].width, le16toh(rh->it_len));
return -1;
}
return off;
}
off += items[i].width;
}
return -1;
}
|