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