00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "radiotap_iter.h"
00017 #include "platform.h"
00018
00019
00020
00021 static const struct radiotap_align_size rtap_namespace_sizes[] = {
00022 [IEEE80211_RADIOTAP_TSFT] = { .align = 8, .size = 8, },
00023 [IEEE80211_RADIOTAP_FLAGS] = { .align = 1, .size = 1, },
00024 [IEEE80211_RADIOTAP_RATE] = { .align = 1, .size = 1, },
00025 [IEEE80211_RADIOTAP_CHANNEL] = { .align = 2, .size = 4, },
00026 [IEEE80211_RADIOTAP_FHSS] = { .align = 2, .size = 2, },
00027 [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = { .align = 1, .size = 1, },
00028 [IEEE80211_RADIOTAP_DBM_ANTNOISE] = { .align = 1, .size = 1, },
00029 [IEEE80211_RADIOTAP_LOCK_QUALITY] = { .align = 2, .size = 2, },
00030 [IEEE80211_RADIOTAP_TX_ATTENUATION] = { .align = 2, .size = 2, },
00031 [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = { .align = 2, .size = 2, },
00032 [IEEE80211_RADIOTAP_DBM_TX_POWER] = { .align = 1, .size = 1, },
00033 [IEEE80211_RADIOTAP_ANTENNA] = { .align = 1, .size = 1, },
00034 [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = { .align = 1, .size = 1, },
00035 [IEEE80211_RADIOTAP_DB_ANTNOISE] = { .align = 1, .size = 1, },
00036 [IEEE80211_RADIOTAP_RX_FLAGS] = { .align = 2, .size = 2, },
00037 [IEEE80211_RADIOTAP_TX_FLAGS] = { .align = 2, .size = 2, },
00038 [IEEE80211_RADIOTAP_RTS_RETRIES] = { .align = 1, .size = 1, },
00039 [IEEE80211_RADIOTAP_DATA_RETRIES] = { .align = 1, .size = 1, },
00040 [IEEE80211_RADIOTAP_MCS] = { .align = 1, .size = 3, },
00041 [IEEE80211_RADIOTAP_AMPDU_STATUS] = { .align = 4, .size = 8, },
00042
00043
00044
00045 };
00046
00047 static const struct ieee80211_radiotap_namespace radiotap_ns = {
00048 .n_bits = sizeof(rtap_namespace_sizes) / sizeof(rtap_namespace_sizes[0]),
00049 .align_size = rtap_namespace_sizes,
00050 };
00051
00090 int ieee80211_radiotap_iterator_init(
00091 struct ieee80211_radiotap_iterator *iterator,
00092 struct ieee80211_radiotap_header *radiotap_header,
00093 int max_length, const struct ieee80211_radiotap_vendor_namespaces *vns)
00094 {
00095
00096 if (radiotap_header->it_version)
00097 return -EINVAL;
00098
00099
00100 if (max_length < get_unaligned_le16(&radiotap_header->it_len))
00101 return -EINVAL;
00102
00103 iterator->_rtheader = radiotap_header;
00104 iterator->_max_length = get_unaligned_le16(&radiotap_header->it_len);
00105 iterator->_arg_index = 0;
00106 iterator->_bitmap_shifter = get_unaligned_le32(&radiotap_header->it_present);
00107 iterator->_arg = (uint8_t *)radiotap_header + sizeof(*radiotap_header);
00108 iterator->_reset_on_ext = 0;
00109 iterator->_next_bitmap = &radiotap_header->it_present;
00110 iterator->_next_bitmap++;
00111 iterator->_vns = vns;
00112 iterator->current_namespace = &radiotap_ns;
00113 iterator->is_radiotap_ns = 1;
00114 #ifdef RADIOTAP_SUPPORT_OVERRIDES
00115 iterator->n_overrides = 0;
00116 iterator->overrides = NULL;
00117 #endif
00118
00119
00120
00121 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
00122 while (get_unaligned_le32(iterator->_arg) &
00123 (1 << IEEE80211_RADIOTAP_EXT)) {
00124 iterator->_arg += sizeof(uint32_t);
00125
00126
00127
00128
00129
00130
00131
00132 if ((unsigned long)iterator->_arg -
00133 (unsigned long)iterator->_rtheader >
00134 (unsigned long)iterator->_max_length)
00135 return -EINVAL;
00136 }
00137
00138 iterator->_arg += sizeof(uint32_t);
00139
00140
00141
00142
00143
00144
00145 }
00146
00147 iterator->this_arg = iterator->_arg;
00148
00149
00150
00151 return 0;
00152 }
00153
00154 static void find_ns(struct ieee80211_radiotap_iterator *iterator,
00155 uint32_t oui, uint8_t subns)
00156 {
00157 int i;
00158
00159 iterator->current_namespace = NULL;
00160
00161 if (!iterator->_vns)
00162 return;
00163
00164 for (i = 0; i < iterator->_vns->n_ns; i++) {
00165 if (iterator->_vns->ns[i].oui != oui)
00166 continue;
00167 if (iterator->_vns->ns[i].subns != subns)
00168 continue;
00169
00170 iterator->current_namespace = &iterator->_vns->ns[i];
00171 break;
00172 }
00173 }
00174
00175 #ifdef RADIOTAP_SUPPORT_OVERRIDES
00176 static int find_override(struct ieee80211_radiotap_iterator *iterator,
00177 int *align, int *size)
00178 {
00179 int i;
00180
00181 if (!iterator->overrides)
00182 return 0;
00183
00184 for (i = 0; i < iterator->n_overrides; i++) {
00185 if (iterator->_arg_index == iterator->overrides[i].field) {
00186 *align = iterator->overrides[i].align;
00187 *size = iterator->overrides[i].size;
00188 if (!*align)
00189 return 0;
00190 return 1;
00191 }
00192 }
00193
00194 return 0;
00195 }
00196 #endif
00197
00198
00222 int ieee80211_radiotap_iterator_next(
00223 struct ieee80211_radiotap_iterator *iterator)
00224 {
00225 while (1) {
00226 int hit = 0;
00227 int pad, align, size, subns;
00228 uint32_t oui;
00229
00230
00231 if ((iterator->_arg_index % 32) == IEEE80211_RADIOTAP_EXT &&
00232 !(iterator->_bitmap_shifter & 1))
00233 return -ENOENT;
00234
00235 if (!(iterator->_bitmap_shifter & 1))
00236 goto next_entry;
00237
00238
00239 switch (iterator->_arg_index % 32) {
00240 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
00241 case IEEE80211_RADIOTAP_EXT:
00242 align = 1;
00243 size = 0;
00244 break;
00245 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
00246 align = 2;
00247 size = 6;
00248 break;
00249 default:
00250 #ifdef RADIOTAP_SUPPORT_OVERRIDES
00251 if (find_override(iterator, &align, &size)) {
00252
00253 } else
00254 #endif
00255 if (!iterator->current_namespace ||
00256 iterator->_arg_index >= iterator->current_namespace->n_bits) {
00257 if (iterator->current_namespace == &radiotap_ns)
00258 return -ENOENT;
00259 align = 0;
00260 } else {
00261 align = iterator->current_namespace->align_size[iterator->_arg_index].align;
00262 size = iterator->current_namespace->align_size[iterator->_arg_index].size;
00263 }
00264 if (!align) {
00265
00266 iterator->_arg = iterator->_next_ns_data;
00267
00268 iterator->current_namespace = NULL;
00269 goto next_entry;
00270 }
00271 break;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 pad = ((unsigned long)iterator->_arg -
00287 (unsigned long)iterator->_rtheader) & (align - 1);
00288
00289 if (pad)
00290 iterator->_arg += align - pad;
00291
00292 if (iterator->_arg_index % 32 == IEEE80211_RADIOTAP_VENDOR_NAMESPACE) {
00293 int vnslen;
00294
00295 if ((unsigned long)iterator->_arg + size -
00296 (unsigned long)iterator->_rtheader >
00297 (unsigned long)iterator->_max_length)
00298 return -EINVAL;
00299
00300 oui = (*iterator->_arg << 16) |
00301 (*(iterator->_arg + 1) << 8) |
00302 *(iterator->_arg + 2);
00303 subns = *(iterator->_arg + 3);
00304
00305 find_ns(iterator, oui, subns);
00306
00307 vnslen = get_unaligned_le16(iterator->_arg + 4);
00308 iterator->_next_ns_data = iterator->_arg + size + vnslen;
00309 if (!iterator->current_namespace)
00310 size += vnslen;
00311 }
00312
00313
00314
00315
00316
00317 iterator->this_arg_index = iterator->_arg_index;
00318 iterator->this_arg = iterator->_arg;
00319 iterator->this_arg_size = size;
00320
00321
00322 iterator->_arg += size;
00323
00324
00325
00326
00327
00328
00329
00330
00331 if ((unsigned long)iterator->_arg -
00332 (unsigned long)iterator->_rtheader >
00333 (unsigned long)iterator->_max_length)
00334 return -EINVAL;
00335
00336
00337 switch (iterator->_arg_index % 32) {
00338 case IEEE80211_RADIOTAP_VENDOR_NAMESPACE:
00339 iterator->_reset_on_ext = 1;
00340
00341 iterator->is_radiotap_ns = 0;
00342
00343
00344
00345
00346
00347
00348 iterator->this_arg_index =
00349 IEEE80211_RADIOTAP_VENDOR_NAMESPACE;
00350 if (!iterator->current_namespace)
00351 hit = 1;
00352 goto next_entry;
00353 case IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE:
00354 iterator->_reset_on_ext = 1;
00355 iterator->current_namespace = &radiotap_ns;
00356 iterator->is_radiotap_ns = 1;
00357 goto next_entry;
00358 case IEEE80211_RADIOTAP_EXT:
00359
00360
00361
00362
00363 iterator->_bitmap_shifter =
00364 get_unaligned_le32(iterator->_next_bitmap);
00365 iterator->_next_bitmap++;
00366 if (iterator->_reset_on_ext)
00367 iterator->_arg_index = 0;
00368 else
00369 iterator->_arg_index++;
00370 iterator->_reset_on_ext = 0;
00371 break;
00372 default:
00373
00374 hit = 1;
00375 next_entry:
00376 iterator->_bitmap_shifter >>= 1;
00377 iterator->_arg_index++;
00378 }
00379
00380
00381 if (hit)
00382 return 0;
00383 }
00384 }