Blame view

net/wireless/radiotap.c 11.7 KB
179f831bc   Andy Green   [PATCH] cfg80211:...
1
2
3
4
  /*
   * Radiotap parser
   *
   * Copyright 2007		Andy Green <andy@warmcat.com>
33e5a2f77   Johannes Berg   wireless: update ...
5
6
7
8
9
10
11
12
13
14
   * Copyright 2009		Johannes Berg <johannes@sipsolutions.net>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   * Alternatively, this software may be distributed under the terms of BSD
   * license.
   *
   * See COPYING for more details.
179f831bc   Andy Green   [PATCH] cfg80211:...
15
   */
942623166   Nikitas Angelinas   net/wireless: use...
16
  #include <linux/kernel.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
17
  #include <linux/export.h>
179f831bc   Andy Green   [PATCH] cfg80211:...
18
19
20
21
22
  #include <net/cfg80211.h>
  #include <net/ieee80211_radiotap.h>
  #include <asm/unaligned.h>
  
  /* function prototypes and related defs are in include/net/cfg80211.h */
33e5a2f77   Johannes Berg   wireless: update ...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  static const struct radiotap_align_size rtap_namespace_sizes[] = {
  	[IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
  	[IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
  	[IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
  	[IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
  	[IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
48613ece3   Johannes Berg   wireless: add rad...
42
43
  	[IEEE80211_RADIOTAP_MCS] = { .align = 1, .size = 3, },
  	[IEEE80211_RADIOTAP_AMPDU_STATUS] = { .align = 4, .size = 8, },
33e5a2f77   Johannes Berg   wireless: update ...
44
45
46
47
48
49
  	/*
  	 * add more here as they are defined in radiotap.h
  	 */
  };
  
  static const struct ieee80211_radiotap_namespace radiotap_ns = {
942623166   Nikitas Angelinas   net/wireless: use...
50
  	.n_bits = ARRAY_SIZE(rtap_namespace_sizes),
33e5a2f77   Johannes Berg   wireless: update ...
51
52
  	.align_size = rtap_namespace_sizes,
  };
179f831bc   Andy Green   [PATCH] cfg80211:...
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
  /**
   * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
   * @iterator: radiotap_iterator to initialize
   * @radiotap_header: radiotap header to parse
   * @max_length: total length we can parse into (eg, whole packet length)
   *
   * Returns: 0 or a negative error code if there is a problem.
   *
   * This function initializes an opaque iterator struct which can then
   * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
   * argument which is present in the header.  It knows about extended
   * present headers and handles them.
   *
   * How to use:
   * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
   * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
   * checking for a good 0 return code.  Then loop calling
   * __ieee80211_radiotap_iterator_next()... it returns either 0,
   * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
   * The iterator's @this_arg member points to the start of the argument
   * associated with the current argument index that is present, which can be
   * found in the iterator's @this_arg_index member.  This arg index corresponds
   * to the IEEE80211_RADIOTAP_... defines.
   *
   * Radiotap header length:
   * You can find the CPU-endian total radiotap header length in
   * iterator->max_length after executing ieee80211_radiotap_iterator_init()
   * successfully.
   *
   * Alignment Gotcha:
   * You must take care when dereferencing iterator.this_arg
   * for multibyte types... the pointer is not aligned.  Use
   * get_unaligned((type *)iterator.this_arg) to dereference
   * iterator.this_arg for type "type" safely on all arches.
   *
   * Example code:
   * See Documentation/networking/radiotap-headers.txt
   */
  
  int ieee80211_radiotap_iterator_init(
33e5a2f77   Johannes Berg   wireless: update ...
93
94
95
  	struct ieee80211_radiotap_iterator *iterator,
  	struct ieee80211_radiotap_header *radiotap_header,
  	int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
179f831bc   Andy Green   [PATCH] cfg80211:...
96
  {
f5563318f   Johannes Berg   wireless: radiota...
97
98
99
  	/* check the radiotap header can actually be present */
  	if (max_length < sizeof(struct ieee80211_radiotap_header))
  		return -EINVAL;
179f831bc   Andy Green   [PATCH] cfg80211:...
100
101
102
103
104
  	/* Linux only supports version 0 radiotap format */
  	if (radiotap_header->it_version)
  		return -EINVAL;
  
  	/* sanity check for allowed length and radiotap length field */
ae7245cbf   Harvey Harrison   wireless: use get...
105
  	if (max_length < get_unaligned_le16(&radiotap_header->it_len))
179f831bc   Andy Green   [PATCH] cfg80211:...
106
  		return -EINVAL;
33e5a2f77   Johannes Berg   wireless: update ...
107
108
109
110
111
112
113
114
115
116
117
  	iterator->_rtheader = radiotap_header;
  	iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
  	iterator->_arg_index = 0;
  	iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
  	iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
  	iterator->_reset_on_ext = 0;
  	iterator->_next_bitmap = &radiotap_header->it_present;
  	iterator->_next_bitmap++;
  	iterator->_vns = vns;
  	iterator->current_namespace = &radiotap_ns;
  	iterator->is_radiotap_ns = 1;
179f831bc   Andy Green   [PATCH] cfg80211:...
118
119
  
  	/* find payload start allowing for extended bitmap(s) */
33e5a2f77   Johannes Berg   wireless: update ...
120
  	if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
bd02cd254   Johannes Berg   radiotap: fix bit...
121
122
123
124
  		if ((unsigned long)iterator->_arg -
  		    (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
  		    (unsigned long)iterator->_max_length)
  			return -EINVAL;
33e5a2f77   Johannes Berg   wireless: update ...
125
126
127
  		while (get_unaligned_le32(iterator->_arg) &
  					(1 << IEEE80211_RADIOTAP_EXT)) {
  			iterator->_arg += sizeof(uint32_t);
179f831bc   Andy Green   [PATCH] cfg80211:...
128
129
130
131
132
133
  
  			/*
  			 * check for insanity where the present bitmaps
  			 * keep claiming to extend up to or even beyond the
  			 * stated radiotap header length
  			 */
33e5a2f77   Johannes Berg   wireless: update ...
134
  			if ((unsigned long)iterator->_arg -
f5563318f   Johannes Berg   wireless: radiota...
135
136
  			    (unsigned long)iterator->_rtheader +
  			    sizeof(uint32_t) >
33e5a2f77   Johannes Berg   wireless: update ...
137
  			    (unsigned long)iterator->_max_length)
179f831bc   Andy Green   [PATCH] cfg80211:...
138
139
  				return -EINVAL;
  		}
33e5a2f77   Johannes Berg   wireless: update ...
140
  		iterator->_arg += sizeof(uint32_t);
179f831bc   Andy Green   [PATCH] cfg80211:...
141
142
143
144
145
146
147
  
  		/*
  		 * no need to check again for blowing past stated radiotap
  		 * header length, because ieee80211_radiotap_iterator_next
  		 * checks it before it is dereferenced
  		 */
  	}
33e5a2f77   Johannes Berg   wireless: update ...
148
  	iterator->this_arg = iterator->_arg;
179f831bc   Andy Green   [PATCH] cfg80211:...
149
150
151
152
153
  	/* we are all initialized happily */
  
  	return 0;
  }
  EXPORT_SYMBOL(ieee80211_radiotap_iterator_init);
33e5a2f77   Johannes Berg   wireless: update ...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  static void find_ns(struct ieee80211_radiotap_iterator *iterator,
  		    uint32_t oui, uint8_t subns)
  {
  	int i;
  
  	iterator->current_namespace = NULL;
  
  	if (!iterator->_vns)
  		return;
  
  	for (i = 0; i < iterator->_vns->n_ns; i++) {
  		if (iterator->_vns->ns[i].oui != oui)
  			continue;
  		if (iterator->_vns->ns[i].subns != subns)
  			continue;
  
  		iterator->current_namespace = &iterator->_vns->ns[i];
  		break;
  	}
  }
179f831bc   Andy Green   [PATCH] cfg80211:...
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
  
  /**
   * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
   * @iterator: radiotap_iterator to move to next arg (if any)
   *
   * Returns: 0 if there is an argument to handle,
   * -ENOENT if there are no more args or -EINVAL
   * if there is something else wrong.
   *
   * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
   * in @this_arg_index and sets @this_arg to point to the
   * payload for the field.  It takes care of alignment handling and extended
   * present fields.  @this_arg can be changed by the caller (eg,
   * incremented to move inside a compound argument like
   * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
   * little-endian format whatever the endianess of your CPU.
   *
   * Alignment Gotcha:
   * You must take care when dereferencing iterator.this_arg
   * for multibyte types... the pointer is not aligned.  Use
   * get_unaligned((type *)iterator.this_arg) to dereference
   * iterator.this_arg for type "type" safely on all arches.
   */
  
  int ieee80211_radiotap_iterator_next(
33e5a2f77   Johannes Berg   wireless: update ...
199
  	struct ieee80211_radiotap_iterator *iterator)
179f831bc   Andy Green   [PATCH] cfg80211:...
200
  {
33e5a2f77   Johannes Berg   wireless: update ...
201
  	while (1) {
179f831bc   Andy Green   [PATCH] cfg80211:...
202
  		int hit = 0;
9ebad4ab8   Johannes Berg   radiotap: fix ven...
203
  		int pad, align, size, subns;
33e5a2f77   Johannes Berg   wireless: update ...
204
  		uint32_t oui;
179f831bc   Andy Green   [PATCH] cfg80211:...
205

33e5a2f77   Johannes Berg   wireless: update ...
206
207
208
209
210
211
  		/* if no more EXT bits, that's it */
  		if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
  		    !(iterator->_bitmap_shifter & 1))
  			return -ENOENT;
  
  		if (!(iterator->_bitmap_shifter & 1))
179f831bc   Andy Green   [PATCH] cfg80211:...
212
  			goto next_entry; /* arg not present */
33e5a2f77   Johannes Berg   wireless: update ...
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
  		/* get alignment/size of data */
  		switch (iterator->_arg_index % 32) {
  		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
  		case IEEE80211_RADIOTAP_EXT:
  			align = 1;
  			size = 0;
  			break;
  		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
  			align = 2;
  			size = 6;
  			break;
  		default:
  			if (!iterator->current_namespace ||
  			    iterator->_arg_index >= iterator->current_namespace->n_bits) {
  				if (iterator->current_namespace == &radiotap_ns)
  					return -ENOENT;
  				align = 0;
  			} else {
  				align = iterator->current_namespace->align_size[iterator->_arg_index].align;
  				size = iterator->current_namespace->align_size[iterator->_arg_index].size;
  			}
  			if (!align) {
  				/* skip all subsequent data */
  				iterator->_arg = iterator->_next_ns_data;
  				/* give up on this namespace */
  				iterator->current_namespace = NULL;
  				goto next_entry;
  			}
  			break;
  		}
179f831bc   Andy Green   [PATCH] cfg80211:...
243
244
  		/*
  		 * arg is present, account for alignment padding
179f831bc   Andy Green   [PATCH] cfg80211:...
245
  		 *
33e5a2f77   Johannes Berg   wireless: update ...
246
247
  		 * Note that these alignments are relative to the start
  		 * of the radiotap header.  There is no guarantee
179f831bc   Andy Green   [PATCH] cfg80211:...
248
249
250
  		 * that the radiotap header itself is aligned on any
  		 * kind of boundary.
  		 *
33e5a2f77   Johannes Berg   wireless: update ...
251
252
  		 * The above is why get_unaligned() is used to dereference
  		 * multibyte elements from the radiotap area.
179f831bc   Andy Green   [PATCH] cfg80211:...
253
  		 */
33e5a2f77   Johannes Berg   wireless: update ...
254
255
  		pad = ((unsigned long)iterator->_arg -
  		       (unsigned long)iterator->_rtheader) & (align - 1);
179f831bc   Andy Green   [PATCH] cfg80211:...
256
257
  
  		if (pad)
33e5a2f77   Johannes Berg   wireless: update ...
258
  			iterator->_arg += align - pad;
179f831bc   Andy Green   [PATCH] cfg80211:...
259

9ebad4ab8   Johannes Berg   radiotap: fix ven...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  		if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
  			int vnslen;
  
  			if ((unsigned long)iterator->_arg + size -
  			    (unsigned long)iterator->_rtheader >
  			    (unsigned long)iterator->_max_length)
  				return -EINVAL;
  
  			oui = (*iterator->_arg << 16) |
  				(*(iterator->_arg + 1) << 8) |
  				*(iterator->_arg + 2);
  			subns = *(iterator->_arg + 3);
  
  			find_ns(iterator, oui, subns);
  
  			vnslen = get_unaligned_le16(iterator->_arg + 4);
  			iterator->_next_ns_data = iterator->_arg + size + vnslen;
  			if (!iterator->current_namespace)
  				size += vnslen;
  		}
179f831bc   Andy Green   [PATCH] cfg80211:...
280
281
282
283
  		/*
  		 * this is what we will return to user, but we need to
  		 * move on first so next call has something fresh to test
  		 */
33e5a2f77   Johannes Berg   wireless: update ...
284
285
286
  		iterator->this_arg_index = iterator->_arg_index;
  		iterator->this_arg = iterator->_arg;
  		iterator->this_arg_size = size;
179f831bc   Andy Green   [PATCH] cfg80211:...
287
288
  
  		/* internally move on the size of this arg */
33e5a2f77   Johannes Berg   wireless: update ...
289
  		iterator->_arg += size;
179f831bc   Andy Green   [PATCH] cfg80211:...
290
291
292
293
294
295
296
  
  		/*
  		 * check for insanity where we are given a bitmap that
  		 * claims to have more arg content than the length of the
  		 * radiotap section.  We will normally end up equalling this
  		 * max_length on the last arg, never exceeding it.
  		 */
33e5a2f77   Johannes Berg   wireless: update ...
297
298
299
  		if ((unsigned long)iterator->_arg -
  		    (unsigned long)iterator->_rtheader >
  		    (unsigned long)iterator->_max_length)
179f831bc   Andy Green   [PATCH] cfg80211:...
300
  			return -EINVAL;
33e5a2f77   Johannes Berg   wireless: update ...
301
302
303
  		/* these special ones are valid in each bitmap word */
  		switch (iterator->_arg_index % 32) {
  		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
33e5a2f77   Johannes Berg   wireless: update ...
304
  			iterator->_reset_on_ext = 1;
33e5a2f77   Johannes Berg   wireless: update ...
305
  			iterator->is_radiotap_ns = 0;
9ebad4ab8   Johannes Berg   radiotap: fix ven...
306
307
308
309
310
311
  			/*
  			 * If parser didn't register this vendor
  			 * namespace with us, allow it to show it
  			 * as 'raw. Do do that, set argument index
  			 * to vendor namespace.
  			 */
33e5a2f77   Johannes Berg   wireless: update ...
312
313
  			iterator->this_arg_index =
  				IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
9ebad4ab8   Johannes Berg   radiotap: fix ven...
314
315
316
  			if (!iterator->current_namespace)
  				hit = 1;
  			goto next_entry;
33e5a2f77   Johannes Berg   wireless: update ...
317
  		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
33e5a2f77   Johannes Berg   wireless: update ...
318
319
320
  			iterator->_reset_on_ext = 1;
  			iterator->current_namespace = &radiotap_ns;
  			iterator->is_radiotap_ns = 1;
9ebad4ab8   Johannes Berg   radiotap: fix ven...
321
  			goto next_entry;
33e5a2f77   Johannes Berg   wireless: update ...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  		case IEEE80211_RADIOTAP_EXT:
  			/*
  			 * bit 31 was set, there is more
  			 * -- move to next u32 bitmap
  			 */
  			iterator->_bitmap_shifter =
  				get_unaligned_le32(iterator->_next_bitmap);
  			iterator->_next_bitmap++;
  			if (iterator->_reset_on_ext)
  				iterator->_arg_index = 0;
  			else
  				iterator->_arg_index++;
  			iterator->_reset_on_ext = 0;
  			break;
  		default:
  			/* we've got a hit! */
  			hit = 1;
   next_entry:
  			iterator->_bitmap_shifter >>= 1;
  			iterator->_arg_index++;
  		}
179f831bc   Andy Green   [PATCH] cfg80211:...
343
344
345
346
347
  
  		/* if we found a valid arg earlier, return it now */
  		if (hit)
  			return 0;
  	}
179f831bc   Andy Green   [PATCH] cfg80211:...
348
349
  }
  EXPORT_SYMBOL(ieee80211_radiotap_iterator_next);