xref: /aosp_15_r20/external/wpa_supplicant_8/src/utils/radiotap.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1*03f9172cSAndroid Build Coastguard Worker /*
2*03f9172cSAndroid Build Coastguard Worker  * Radiotap parser
3*03f9172cSAndroid Build Coastguard Worker  *
4*03f9172cSAndroid Build Coastguard Worker  * Copyright 2007		Andy Green <[email protected]>
5*03f9172cSAndroid Build Coastguard Worker  * Copyright 2009		Johannes Berg <[email protected]>
6*03f9172cSAndroid Build Coastguard Worker  *
7*03f9172cSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify
8*03f9172cSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License version 2 as
9*03f9172cSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
10*03f9172cSAndroid Build Coastguard Worker  *
11*03f9172cSAndroid Build Coastguard Worker  * Alternatively, this software may be distributed under the terms of ISC
12*03f9172cSAndroid Build Coastguard Worker  * license, see COPYING for more details.
13*03f9172cSAndroid Build Coastguard Worker  */
14*03f9172cSAndroid Build Coastguard Worker #include "platform.h"
15*03f9172cSAndroid Build Coastguard Worker #include "radiotap_iter.h"
16*03f9172cSAndroid Build Coastguard Worker 
17*03f9172cSAndroid Build Coastguard Worker /* function prototypes and related defs are in radiotap_iter.h */
18*03f9172cSAndroid Build Coastguard Worker 
19*03f9172cSAndroid Build Coastguard Worker static const struct radiotap_align_size rtap_namespace_sizes[] = {
20*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
21*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
22*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
23*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
24*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
25*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
26*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
27*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
28*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
29*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
30*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
31*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
32*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
33*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
34*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
35*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
36*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
37*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
38*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_MCS] = { .align = 1, .size = 3, },
39*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_AMPDU_STATUS] = { .align = 4, .size = 8, },
40*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_VHT] = { .align = 2, .size = 12, },
41*03f9172cSAndroid Build Coastguard Worker 	[IEEE80211_RADIOTAP_TIMESTAMP] = { .align = 8, .size = 12, },
42*03f9172cSAndroid Build Coastguard Worker 	/*
43*03f9172cSAndroid Build Coastguard Worker 	 * add more here as they are defined in radiotap.h
44*03f9172cSAndroid Build Coastguard Worker 	 */
45*03f9172cSAndroid Build Coastguard Worker };
46*03f9172cSAndroid Build Coastguard Worker 
47*03f9172cSAndroid Build Coastguard Worker static const struct ieee80211_radiotap_namespace radiotap_ns = {
48*03f9172cSAndroid Build Coastguard Worker 	.n_bits = sizeof(rtap_namespace_sizes) / sizeof(rtap_namespace_sizes[0]),
49*03f9172cSAndroid Build Coastguard Worker 	.align_size = rtap_namespace_sizes,
50*03f9172cSAndroid Build Coastguard Worker };
51*03f9172cSAndroid Build Coastguard Worker 
52*03f9172cSAndroid Build Coastguard Worker /**
53*03f9172cSAndroid Build Coastguard Worker  * ieee80211_radiotap_iterator_init - radiotap parser iterator initialization
54*03f9172cSAndroid Build Coastguard Worker  * @iterator: radiotap_iterator to initialize
55*03f9172cSAndroid Build Coastguard Worker  * @radiotap_header: radiotap header to parse
56*03f9172cSAndroid Build Coastguard Worker  * @max_length: total length we can parse into (eg, whole packet length)
57*03f9172cSAndroid Build Coastguard Worker  *
58*03f9172cSAndroid Build Coastguard Worker  * Returns: 0 or a negative error code if there is a problem.
59*03f9172cSAndroid Build Coastguard Worker  *
60*03f9172cSAndroid Build Coastguard Worker  * This function initializes an opaque iterator struct which can then
61*03f9172cSAndroid Build Coastguard Worker  * be passed to ieee80211_radiotap_iterator_next() to visit every radiotap
62*03f9172cSAndroid Build Coastguard Worker  * argument which is present in the header.  It knows about extended
63*03f9172cSAndroid Build Coastguard Worker  * present headers and handles them.
64*03f9172cSAndroid Build Coastguard Worker  *
65*03f9172cSAndroid Build Coastguard Worker  * How to use:
66*03f9172cSAndroid Build Coastguard Worker  * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
67*03f9172cSAndroid Build Coastguard Worker  * struct ieee80211_radiotap_iterator (no need to init the struct beforehand)
68*03f9172cSAndroid Build Coastguard Worker  * checking for a good 0 return code.  Then loop calling
69*03f9172cSAndroid Build Coastguard Worker  * __ieee80211_radiotap_iterator_next()... it returns either 0,
70*03f9172cSAndroid Build Coastguard Worker  * -ENOENT if there are no more args to parse, or -EINVAL if there is a problem.
71*03f9172cSAndroid Build Coastguard Worker  * The iterator's @this_arg member points to the start of the argument
72*03f9172cSAndroid Build Coastguard Worker  * associated with the current argument index that is present, which can be
73*03f9172cSAndroid Build Coastguard Worker  * found in the iterator's @this_arg_index member.  This arg index corresponds
74*03f9172cSAndroid Build Coastguard Worker  * to the IEEE80211_RADIOTAP_... defines.
75*03f9172cSAndroid Build Coastguard Worker  *
76*03f9172cSAndroid Build Coastguard Worker  * Radiotap header length:
77*03f9172cSAndroid Build Coastguard Worker  * You can find the CPU-endian total radiotap header length in
78*03f9172cSAndroid Build Coastguard Worker  * iterator->max_length after executing ieee80211_radiotap_iterator_init()
79*03f9172cSAndroid Build Coastguard Worker  * successfully.
80*03f9172cSAndroid Build Coastguard Worker  *
81*03f9172cSAndroid Build Coastguard Worker  * Alignment Gotcha:
82*03f9172cSAndroid Build Coastguard Worker  * You must take care when dereferencing iterator.this_arg
83*03f9172cSAndroid Build Coastguard Worker  * for multibyte types... the pointer is not aligned.  Use
84*03f9172cSAndroid Build Coastguard Worker  * get_unaligned((type *)iterator.this_arg) to dereference
85*03f9172cSAndroid Build Coastguard Worker  * iterator.this_arg for type "type" safely on all arches.
86*03f9172cSAndroid Build Coastguard Worker  *
87*03f9172cSAndroid Build Coastguard Worker  * Example code: parse.c
88*03f9172cSAndroid Build Coastguard Worker  */
89*03f9172cSAndroid Build Coastguard Worker 
ieee80211_radiotap_iterator_init(struct ieee80211_radiotap_iterator * iterator,struct ieee80211_radiotap_header * radiotap_header,int max_length,const struct ieee80211_radiotap_vendor_namespaces * vns)90*03f9172cSAndroid Build Coastguard Worker int ieee80211_radiotap_iterator_init(
91*03f9172cSAndroid Build Coastguard Worker 	struct ieee80211_radiotap_iterator *iterator,
92*03f9172cSAndroid Build Coastguard Worker 	struct ieee80211_radiotap_header *radiotap_header,
93*03f9172cSAndroid Build Coastguard Worker 	int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
94*03f9172cSAndroid Build Coastguard Worker {
95*03f9172cSAndroid Build Coastguard Worker 	/* must at least have the radiotap header */
96*03f9172cSAndroid Build Coastguard Worker 	if (max_length < (int)sizeof(struct ieee80211_radiotap_header))
97*03f9172cSAndroid Build Coastguard Worker 		return -EINVAL;
98*03f9172cSAndroid Build Coastguard Worker 
99*03f9172cSAndroid Build Coastguard Worker 	/* Linux only supports version 0 radiotap format */
100*03f9172cSAndroid Build Coastguard Worker 	if (radiotap_header->it_version)
101*03f9172cSAndroid Build Coastguard Worker 		return -EINVAL;
102*03f9172cSAndroid Build Coastguard Worker 
103*03f9172cSAndroid Build Coastguard Worker 	/* sanity check for allowed length and radiotap length field */
104*03f9172cSAndroid Build Coastguard Worker 	if (max_length < get_unaligned_le16(&radiotap_header->it_len))
105*03f9172cSAndroid Build Coastguard Worker 		return -EINVAL;
106*03f9172cSAndroid Build Coastguard Worker 
107*03f9172cSAndroid Build Coastguard Worker 	iterator->_rtheader = radiotap_header;
108*03f9172cSAndroid Build Coastguard Worker 	iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
109*03f9172cSAndroid Build Coastguard Worker 	iterator->_arg_index = 0;
110*03f9172cSAndroid Build Coastguard Worker 	iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
111*03f9172cSAndroid Build Coastguard Worker 	iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
112*03f9172cSAndroid Build Coastguard Worker 	iterator->_next_ns_data = NULL;
113*03f9172cSAndroid Build Coastguard Worker 	iterator->_reset_on_ext = 0;
114*03f9172cSAndroid Build Coastguard Worker 	iterator->_next_bitmap = (le32 *) (((u8 *) radiotap_header) + offsetof(struct ieee80211_radiotap_header, it_present));
115*03f9172cSAndroid Build Coastguard Worker 	iterator->_next_bitmap++;
116*03f9172cSAndroid Build Coastguard Worker 	iterator->_vns = vns;
117*03f9172cSAndroid Build Coastguard Worker 	iterator->current_namespace = &radiotap_ns;
118*03f9172cSAndroid Build Coastguard Worker 	iterator->is_radiotap_ns = 1;
119*03f9172cSAndroid Build Coastguard Worker #ifdef RADIOTAP_SUPPORT_OVERRIDES
120*03f9172cSAndroid Build Coastguard Worker 	iterator->n_overrides = 0;
121*03f9172cSAndroid Build Coastguard Worker 	iterator->overrides = NULL;
122*03f9172cSAndroid Build Coastguard Worker #endif
123*03f9172cSAndroid Build Coastguard Worker 
124*03f9172cSAndroid Build Coastguard Worker 	/* find payload start allowing for extended bitmap(s) */
125*03f9172cSAndroid Build Coastguard Worker 
126*03f9172cSAndroid Build Coastguard Worker 	if (iterator->_bitmap_shifter & BIT(IEEE80211_RADIOTAP_EXT)) {
127*03f9172cSAndroid Build Coastguard Worker 		if ((unsigned long)iterator->_arg -
128*03f9172cSAndroid Build Coastguard Worker 		    (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
129*03f9172cSAndroid Build Coastguard Worker 		    (unsigned long)iterator->_max_length)
130*03f9172cSAndroid Build Coastguard Worker 			return -EINVAL;
131*03f9172cSAndroid Build Coastguard Worker 		while (get_unaligned_le32(iterator->_arg) &
132*03f9172cSAndroid Build Coastguard Worker 		       BIT(IEEE80211_RADIOTAP_EXT)) {
133*03f9172cSAndroid Build Coastguard Worker 			iterator->_arg += sizeof(uint32_t);
134*03f9172cSAndroid Build Coastguard Worker 
135*03f9172cSAndroid Build Coastguard Worker 			/*
136*03f9172cSAndroid Build Coastguard Worker 			 * check for insanity where the present bitmaps
137*03f9172cSAndroid Build Coastguard Worker 			 * keep claiming to extend up to or even beyond the
138*03f9172cSAndroid Build Coastguard Worker 			 * stated radiotap header length
139*03f9172cSAndroid Build Coastguard Worker 			 */
140*03f9172cSAndroid Build Coastguard Worker 
141*03f9172cSAndroid Build Coastguard Worker 			if ((unsigned long)iterator->_arg -
142*03f9172cSAndroid Build Coastguard Worker 			    (unsigned long)iterator->_rtheader +
143*03f9172cSAndroid Build Coastguard Worker 			    sizeof(uint32_t) >
144*03f9172cSAndroid Build Coastguard Worker 			    (unsigned long)iterator->_max_length)
145*03f9172cSAndroid Build Coastguard Worker 				return -EINVAL;
146*03f9172cSAndroid Build Coastguard Worker 		}
147*03f9172cSAndroid Build Coastguard Worker 
148*03f9172cSAndroid Build Coastguard Worker 		iterator->_arg += sizeof(uint32_t);
149*03f9172cSAndroid Build Coastguard Worker 
150*03f9172cSAndroid Build Coastguard Worker 		/*
151*03f9172cSAndroid Build Coastguard Worker 		 * no need to check again for blowing past stated radiotap
152*03f9172cSAndroid Build Coastguard Worker 		 * header length, because ieee80211_radiotap_iterator_next
153*03f9172cSAndroid Build Coastguard Worker 		 * checks it before it is dereferenced
154*03f9172cSAndroid Build Coastguard Worker 		 */
155*03f9172cSAndroid Build Coastguard Worker 	}
156*03f9172cSAndroid Build Coastguard Worker 
157*03f9172cSAndroid Build Coastguard Worker 	iterator->this_arg = iterator->_arg;
158*03f9172cSAndroid Build Coastguard Worker 	iterator->this_arg_index = 0;
159*03f9172cSAndroid Build Coastguard Worker 	iterator->this_arg_size = 0;
160*03f9172cSAndroid Build Coastguard Worker 
161*03f9172cSAndroid Build Coastguard Worker 	/* we are all initialized happily */
162*03f9172cSAndroid Build Coastguard Worker 
163*03f9172cSAndroid Build Coastguard Worker 	return 0;
164*03f9172cSAndroid Build Coastguard Worker }
165*03f9172cSAndroid Build Coastguard Worker 
find_ns(struct ieee80211_radiotap_iterator * iterator,uint32_t oui,uint8_t subns)166*03f9172cSAndroid Build Coastguard Worker static void find_ns(struct ieee80211_radiotap_iterator *iterator,
167*03f9172cSAndroid Build Coastguard Worker 		    uint32_t oui, uint8_t subns)
168*03f9172cSAndroid Build Coastguard Worker {
169*03f9172cSAndroid Build Coastguard Worker 	int i;
170*03f9172cSAndroid Build Coastguard Worker 
171*03f9172cSAndroid Build Coastguard Worker 	iterator->current_namespace = NULL;
172*03f9172cSAndroid Build Coastguard Worker 
173*03f9172cSAndroid Build Coastguard Worker 	if (!iterator->_vns)
174*03f9172cSAndroid Build Coastguard Worker 		return;
175*03f9172cSAndroid Build Coastguard Worker 
176*03f9172cSAndroid Build Coastguard Worker 	for (i = 0; i < iterator->_vns->n_ns; i++) {
177*03f9172cSAndroid Build Coastguard Worker 		if (iterator->_vns->ns[i].oui != oui)
178*03f9172cSAndroid Build Coastguard Worker 			continue;
179*03f9172cSAndroid Build Coastguard Worker 		if (iterator->_vns->ns[i].subns != subns)
180*03f9172cSAndroid Build Coastguard Worker 			continue;
181*03f9172cSAndroid Build Coastguard Worker 
182*03f9172cSAndroid Build Coastguard Worker 		iterator->current_namespace = &iterator->_vns->ns[i];
183*03f9172cSAndroid Build Coastguard Worker 		break;
184*03f9172cSAndroid Build Coastguard Worker 	}
185*03f9172cSAndroid Build Coastguard Worker }
186*03f9172cSAndroid Build Coastguard Worker 
187*03f9172cSAndroid Build Coastguard Worker #ifdef RADIOTAP_SUPPORT_OVERRIDES
find_override(struct ieee80211_radiotap_iterator * iterator,int * align,int * size)188*03f9172cSAndroid Build Coastguard Worker static int find_override(struct ieee80211_radiotap_iterator *iterator,
189*03f9172cSAndroid Build Coastguard Worker 			 int *align, int *size)
190*03f9172cSAndroid Build Coastguard Worker {
191*03f9172cSAndroid Build Coastguard Worker 	int i;
192*03f9172cSAndroid Build Coastguard Worker 
193*03f9172cSAndroid Build Coastguard Worker 	if (!iterator->overrides)
194*03f9172cSAndroid Build Coastguard Worker 		return 0;
195*03f9172cSAndroid Build Coastguard Worker 
196*03f9172cSAndroid Build Coastguard Worker 	for (i = 0; i < iterator->n_overrides; i++) {
197*03f9172cSAndroid Build Coastguard Worker 		if (iterator->_arg_index == iterator->overrides[i].field) {
198*03f9172cSAndroid Build Coastguard Worker 			*align = iterator->overrides[i].align;
199*03f9172cSAndroid Build Coastguard Worker 			*size = iterator->overrides[i].size;
200*03f9172cSAndroid Build Coastguard Worker 			if (!*align) /* erroneous override */
201*03f9172cSAndroid Build Coastguard Worker 				return 0;
202*03f9172cSAndroid Build Coastguard Worker 			return 1;
203*03f9172cSAndroid Build Coastguard Worker 		}
204*03f9172cSAndroid Build Coastguard Worker 	}
205*03f9172cSAndroid Build Coastguard Worker 
206*03f9172cSAndroid Build Coastguard Worker 	return 0;
207*03f9172cSAndroid Build Coastguard Worker }
208*03f9172cSAndroid Build Coastguard Worker #endif
209*03f9172cSAndroid Build Coastguard Worker 
210*03f9172cSAndroid Build Coastguard Worker 
211*03f9172cSAndroid Build Coastguard Worker /**
212*03f9172cSAndroid Build Coastguard Worker  * ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg
213*03f9172cSAndroid Build Coastguard Worker  * @iterator: radiotap_iterator to move to next arg (if any)
214*03f9172cSAndroid Build Coastguard Worker  *
215*03f9172cSAndroid Build Coastguard Worker  * Returns: 0 if there is an argument to handle,
216*03f9172cSAndroid Build Coastguard Worker  * -ENOENT if there are no more args or -EINVAL
217*03f9172cSAndroid Build Coastguard Worker  * if there is something else wrong.
218*03f9172cSAndroid Build Coastguard Worker  *
219*03f9172cSAndroid Build Coastguard Worker  * This function provides the next radiotap arg index (IEEE80211_RADIOTAP_*)
220*03f9172cSAndroid Build Coastguard Worker  * in @this_arg_index and sets @this_arg to point to the
221*03f9172cSAndroid Build Coastguard Worker  * payload for the field.  It takes care of alignment handling and extended
222*03f9172cSAndroid Build Coastguard Worker  * present fields.  @this_arg can be changed by the caller (eg,
223*03f9172cSAndroid Build Coastguard Worker  * incremented to move inside a compound argument like
224*03f9172cSAndroid Build Coastguard Worker  * IEEE80211_RADIOTAP_CHANNEL).  The args pointed to are in
225*03f9172cSAndroid Build Coastguard Worker  * little-endian format whatever the endianness of your CPU.
226*03f9172cSAndroid Build Coastguard Worker  *
227*03f9172cSAndroid Build Coastguard Worker  * Alignment Gotcha:
228*03f9172cSAndroid Build Coastguard Worker  * You must take care when dereferencing iterator.this_arg
229*03f9172cSAndroid Build Coastguard Worker  * for multibyte types... the pointer is not aligned.  Use
230*03f9172cSAndroid Build Coastguard Worker  * get_unaligned((type *)iterator.this_arg) to dereference
231*03f9172cSAndroid Build Coastguard Worker  * iterator.this_arg for type "type" safely on all arches.
232*03f9172cSAndroid Build Coastguard Worker  */
233*03f9172cSAndroid Build Coastguard Worker 
ieee80211_radiotap_iterator_next(struct ieee80211_radiotap_iterator * iterator)234*03f9172cSAndroid Build Coastguard Worker int ieee80211_radiotap_iterator_next(
235*03f9172cSAndroid Build Coastguard Worker 	struct ieee80211_radiotap_iterator *iterator)
236*03f9172cSAndroid Build Coastguard Worker {
237*03f9172cSAndroid Build Coastguard Worker 	while (1) {
238*03f9172cSAndroid Build Coastguard Worker 		int hit = 0;
239*03f9172cSAndroid Build Coastguard Worker 		int pad, align, size, subns;
240*03f9172cSAndroid Build Coastguard Worker 		uint32_t oui;
241*03f9172cSAndroid Build Coastguard Worker 
242*03f9172cSAndroid Build Coastguard Worker 		/* if no more EXT bits, that's it */
243*03f9172cSAndroid Build Coastguard Worker 		if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
244*03f9172cSAndroid Build Coastguard Worker 		    !(iterator->_bitmap_shifter & 1))
245*03f9172cSAndroid Build Coastguard Worker 			return -ENOENT;
246*03f9172cSAndroid Build Coastguard Worker 
247*03f9172cSAndroid Build Coastguard Worker 		if (!(iterator->_bitmap_shifter & 1))
248*03f9172cSAndroid Build Coastguard Worker 			goto next_entry; /* arg not present */
249*03f9172cSAndroid Build Coastguard Worker 
250*03f9172cSAndroid Build Coastguard Worker 		/* get alignment/size of data */
251*03f9172cSAndroid Build Coastguard Worker 		switch (iterator->_arg_index % 32) {
252*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
253*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_EXT:
254*03f9172cSAndroid Build Coastguard Worker 			align = 1;
255*03f9172cSAndroid Build Coastguard Worker 			size = 0;
256*03f9172cSAndroid Build Coastguard Worker 			break;
257*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
258*03f9172cSAndroid Build Coastguard Worker 			align = 2;
259*03f9172cSAndroid Build Coastguard Worker 			size = 6;
260*03f9172cSAndroid Build Coastguard Worker 			break;
261*03f9172cSAndroid Build Coastguard Worker 		default:
262*03f9172cSAndroid Build Coastguard Worker #ifdef RADIOTAP_SUPPORT_OVERRIDES
263*03f9172cSAndroid Build Coastguard Worker 			if (find_override(iterator, &align, &size)) {
264*03f9172cSAndroid Build Coastguard Worker 				/* all set */
265*03f9172cSAndroid Build Coastguard Worker 			} else
266*03f9172cSAndroid Build Coastguard Worker #endif
267*03f9172cSAndroid Build Coastguard Worker 			if (!iterator->current_namespace ||
268*03f9172cSAndroid Build Coastguard Worker 			    iterator->_arg_index >= iterator->current_namespace->n_bits) {
269*03f9172cSAndroid Build Coastguard Worker 				if (iterator->current_namespace == &radiotap_ns)
270*03f9172cSAndroid Build Coastguard Worker 					return -ENOENT;
271*03f9172cSAndroid Build Coastguard Worker 				align = 0;
272*03f9172cSAndroid Build Coastguard Worker 			} else {
273*03f9172cSAndroid Build Coastguard Worker 				align = iterator->current_namespace->align_size[iterator->_arg_index].align;
274*03f9172cSAndroid Build Coastguard Worker 				size = iterator->current_namespace->align_size[iterator->_arg_index].size;
275*03f9172cSAndroid Build Coastguard Worker 			}
276*03f9172cSAndroid Build Coastguard Worker 			if (!align) {
277*03f9172cSAndroid Build Coastguard Worker 				/* skip all subsequent data */
278*03f9172cSAndroid Build Coastguard Worker 				iterator->_arg = iterator->_next_ns_data;
279*03f9172cSAndroid Build Coastguard Worker 				/* give up on this namespace */
280*03f9172cSAndroid Build Coastguard Worker 				iterator->current_namespace = NULL;
281*03f9172cSAndroid Build Coastguard Worker 				goto next_entry;
282*03f9172cSAndroid Build Coastguard Worker 			}
283*03f9172cSAndroid Build Coastguard Worker 			break;
284*03f9172cSAndroid Build Coastguard Worker 		}
285*03f9172cSAndroid Build Coastguard Worker 
286*03f9172cSAndroid Build Coastguard Worker 		/*
287*03f9172cSAndroid Build Coastguard Worker 		 * arg is present, account for alignment padding
288*03f9172cSAndroid Build Coastguard Worker 		 *
289*03f9172cSAndroid Build Coastguard Worker 		 * Note that these alignments are relative to the start
290*03f9172cSAndroid Build Coastguard Worker 		 * of the radiotap header.  There is no guarantee
291*03f9172cSAndroid Build Coastguard Worker 		 * that the radiotap header itself is aligned on any
292*03f9172cSAndroid Build Coastguard Worker 		 * kind of boundary.
293*03f9172cSAndroid Build Coastguard Worker 		 *
294*03f9172cSAndroid Build Coastguard Worker 		 * The above is why get_unaligned() is used to dereference
295*03f9172cSAndroid Build Coastguard Worker 		 * multibyte elements from the radiotap area.
296*03f9172cSAndroid Build Coastguard Worker 		 */
297*03f9172cSAndroid Build Coastguard Worker 
298*03f9172cSAndroid Build Coastguard Worker 		pad = ((unsigned long)iterator->_arg -
299*03f9172cSAndroid Build Coastguard Worker 		       (unsigned long)iterator->_rtheader) & (align - 1);
300*03f9172cSAndroid Build Coastguard Worker 
301*03f9172cSAndroid Build Coastguard Worker 		if (pad)
302*03f9172cSAndroid Build Coastguard Worker 			iterator->_arg += align - pad;
303*03f9172cSAndroid Build Coastguard Worker 
304*03f9172cSAndroid Build Coastguard Worker 		if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
305*03f9172cSAndroid Build Coastguard Worker 			int vnslen;
306*03f9172cSAndroid Build Coastguard Worker 
307*03f9172cSAndroid Build Coastguard Worker 			if ((unsigned long)iterator->_arg + size -
308*03f9172cSAndroid Build Coastguard Worker 			    (unsigned long)iterator->_rtheader >
309*03f9172cSAndroid Build Coastguard Worker 			    (unsigned long)iterator->_max_length)
310*03f9172cSAndroid Build Coastguard Worker 				return -EINVAL;
311*03f9172cSAndroid Build Coastguard Worker 
312*03f9172cSAndroid Build Coastguard Worker 			oui = (*iterator->_arg << 16) |
313*03f9172cSAndroid Build Coastguard Worker 				(*(iterator->_arg + 1) << 8) |
314*03f9172cSAndroid Build Coastguard Worker 				*(iterator->_arg + 2);
315*03f9172cSAndroid Build Coastguard Worker 			subns = *(iterator->_arg + 3);
316*03f9172cSAndroid Build Coastguard Worker 
317*03f9172cSAndroid Build Coastguard Worker 			find_ns(iterator, oui, subns);
318*03f9172cSAndroid Build Coastguard Worker 
319*03f9172cSAndroid Build Coastguard Worker 			vnslen = get_unaligned_le16(iterator->_arg + 4);
320*03f9172cSAndroid Build Coastguard Worker 			iterator->_next_ns_data = iterator->_arg + size + vnslen;
321*03f9172cSAndroid Build Coastguard Worker 			if (!iterator->current_namespace)
322*03f9172cSAndroid Build Coastguard Worker 				size += vnslen;
323*03f9172cSAndroid Build Coastguard Worker 		}
324*03f9172cSAndroid Build Coastguard Worker 
325*03f9172cSAndroid Build Coastguard Worker 		/*
326*03f9172cSAndroid Build Coastguard Worker 		 * this is what we will return to user, but we need to
327*03f9172cSAndroid Build Coastguard Worker 		 * move on first so next call has something fresh to test
328*03f9172cSAndroid Build Coastguard Worker 		 */
329*03f9172cSAndroid Build Coastguard Worker 		iterator->this_arg_index = iterator->_arg_index;
330*03f9172cSAndroid Build Coastguard Worker 		iterator->this_arg = iterator->_arg;
331*03f9172cSAndroid Build Coastguard Worker 		iterator->this_arg_size = size;
332*03f9172cSAndroid Build Coastguard Worker 
333*03f9172cSAndroid Build Coastguard Worker 		/* internally move on the size of this arg */
334*03f9172cSAndroid Build Coastguard Worker 		iterator->_arg += size;
335*03f9172cSAndroid Build Coastguard Worker 
336*03f9172cSAndroid Build Coastguard Worker 		/*
337*03f9172cSAndroid Build Coastguard Worker 		 * check for insanity where we are given a bitmap that
338*03f9172cSAndroid Build Coastguard Worker 		 * claims to have more arg content than the length of the
339*03f9172cSAndroid Build Coastguard Worker 		 * radiotap section.  We will normally end up equalling this
340*03f9172cSAndroid Build Coastguard Worker 		 * max_length on the last arg, never exceeding it.
341*03f9172cSAndroid Build Coastguard Worker 		 */
342*03f9172cSAndroid Build Coastguard Worker 
343*03f9172cSAndroid Build Coastguard Worker 		if ((unsigned long)iterator->_arg -
344*03f9172cSAndroid Build Coastguard Worker 		    (unsigned long)iterator->_rtheader >
345*03f9172cSAndroid Build Coastguard Worker 		    (unsigned long)iterator->_max_length)
346*03f9172cSAndroid Build Coastguard Worker 			return -EINVAL;
347*03f9172cSAndroid Build Coastguard Worker 
348*03f9172cSAndroid Build Coastguard Worker 		/* these special ones are valid in each bitmap word */
349*03f9172cSAndroid Build Coastguard Worker 		switch (iterator->_arg_index % 32) {
350*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
351*03f9172cSAndroid Build Coastguard Worker 			iterator->_reset_on_ext = 1;
352*03f9172cSAndroid Build Coastguard Worker 
353*03f9172cSAndroid Build Coastguard Worker 			iterator->is_radiotap_ns = 0;
354*03f9172cSAndroid Build Coastguard Worker 			/*
355*03f9172cSAndroid Build Coastguard Worker 			 * If parser didn't register this vendor
356*03f9172cSAndroid Build Coastguard Worker 			 * namespace with us, allow it to show it
357*03f9172cSAndroid Build Coastguard Worker 			 * as 'raw. Do do that, set argument index
358*03f9172cSAndroid Build Coastguard Worker 			 * to vendor namespace.
359*03f9172cSAndroid Build Coastguard Worker 			 */
360*03f9172cSAndroid Build Coastguard Worker 			iterator->this_arg_index =
361*03f9172cSAndroid Build Coastguard Worker 				IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
362*03f9172cSAndroid Build Coastguard Worker 			if (!iterator->current_namespace)
363*03f9172cSAndroid Build Coastguard Worker 				hit = 1;
364*03f9172cSAndroid Build Coastguard Worker 			goto next_entry;
365*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
366*03f9172cSAndroid Build Coastguard Worker 			iterator->_reset_on_ext = 1;
367*03f9172cSAndroid Build Coastguard Worker 			iterator->current_namespace = &radiotap_ns;
368*03f9172cSAndroid Build Coastguard Worker 			iterator->is_radiotap_ns = 1;
369*03f9172cSAndroid Build Coastguard Worker 			goto next_entry;
370*03f9172cSAndroid Build Coastguard Worker 		case IEEE80211_RADIOTAP_EXT:
371*03f9172cSAndroid Build Coastguard Worker 			/*
372*03f9172cSAndroid Build Coastguard Worker 			 * bit 31 was set, there is more
373*03f9172cSAndroid Build Coastguard Worker 			 * -- move to next u32 bitmap
374*03f9172cSAndroid Build Coastguard Worker 			 */
375*03f9172cSAndroid Build Coastguard Worker 			iterator->_bitmap_shifter =
376*03f9172cSAndroid Build Coastguard Worker 				get_unaligned_le32(iterator->_next_bitmap);
377*03f9172cSAndroid Build Coastguard Worker 			iterator->_next_bitmap++;
378*03f9172cSAndroid Build Coastguard Worker 			if (iterator->_reset_on_ext)
379*03f9172cSAndroid Build Coastguard Worker 				iterator->_arg_index = 0;
380*03f9172cSAndroid Build Coastguard Worker 			else
381*03f9172cSAndroid Build Coastguard Worker 				iterator->_arg_index++;
382*03f9172cSAndroid Build Coastguard Worker 			iterator->_reset_on_ext = 0;
383*03f9172cSAndroid Build Coastguard Worker 			break;
384*03f9172cSAndroid Build Coastguard Worker 		default:
385*03f9172cSAndroid Build Coastguard Worker 			/* we've got a hit! */
386*03f9172cSAndroid Build Coastguard Worker 			hit = 1;
387*03f9172cSAndroid Build Coastguard Worker  next_entry:
388*03f9172cSAndroid Build Coastguard Worker 			iterator->_bitmap_shifter >>= 1;
389*03f9172cSAndroid Build Coastguard Worker 			iterator->_arg_index++;
390*03f9172cSAndroid Build Coastguard Worker 		}
391*03f9172cSAndroid Build Coastguard Worker 
392*03f9172cSAndroid Build Coastguard Worker 		/* if we found a valid arg earlier, return it now */
393*03f9172cSAndroid Build Coastguard Worker 		if (hit)
394*03f9172cSAndroid Build Coastguard Worker 			return 0;
395*03f9172cSAndroid Build Coastguard Worker 	}
396*03f9172cSAndroid Build Coastguard Worker }
397