radiotap.c
Go to the documentation of this file.
00001 /*
00002  * Radiotap parser
00003  *
00004  * Copyright 2007               Andy Green <andy@warmcat.com>
00005  * Copyright 2009               Johannes Berg <johannes@sipsolutions.net>
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License version 2 as
00009  * published by the Free Software Foundation.
00010  *
00011  * Alternatively, this software may be distributed under the terms of BSD
00012  * license.
00013  *
00014  * See COPYING for more details.
00015  */
00016 #include "radiotap_iter.h"
00017 #include "platform.h"
00018 
00019 /* function prototypes and related defs are in radiotap_iter.h */
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          * add more here as they are defined in radiotap.h
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         /* Linux only supports version 0 radiotap format */
00096         if (radiotap_header->it_version)
00097                 return -EINVAL;
00098 
00099         /* sanity check for allowed length and radiotap length field */
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         /* find payload start allowing for extended bitmap(s) */
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                          * check for insanity where the present bitmaps
00128                          * keep claiming to extend up to or even beyond the
00129                          * stated radiotap header length
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                  * no need to check again for blowing past stated radiotap
00142                  * header length, because ieee80211_radiotap_iterator_next
00143                  * checks it before it is dereferenced
00144                  */
00145         }
00146 
00147         iterator->this_arg = iterator->_arg;
00148 
00149         /* we are all initialized happily */
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) /* erroneous override */
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                 /* if no more EXT bits, that's it */
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; /* arg not present */
00237 
00238                 /* get alignment/size of data */
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                                 /* all set */
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                                 /* skip all subsequent data */
00266                                 iterator->_arg = iterator->_next_ns_data;
00267                                 /* give up on this namespace */
00268                                 iterator->current_namespace = NULL;
00269                                 goto next_entry;
00270                         }
00271                         break;
00272                 }
00273 
00274                 /*
00275                  * arg is present, account for alignment padding
00276                  *
00277                  * Note that these alignments are relative to the start
00278                  * of the radiotap header.  There is no guarantee
00279                  * that the radiotap header itself is aligned on any
00280                  * kind of boundary.
00281                  *
00282                  * The above is why get_unaligned() is used to dereference
00283                  * multibyte elements from the radiotap area.
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                  * this is what we will return to user, but we need to
00315                  * move on first so next call has something fresh to test
00316                  */
00317                 iterator->this_arg_index = iterator->_arg_index;
00318                 iterator->this_arg = iterator->_arg;
00319                 iterator->this_arg_size = size;
00320 
00321                 /* internally move on the size of this arg */
00322                 iterator->_arg += size;
00323 
00324                 /*
00325                  * check for insanity where we are given a bitmap that
00326                  * claims to have more arg content than the length of the
00327                  * radiotap section.  We will normally end up equalling this
00328                  * max_length on the last arg, never exceeding it.
00329                  */
00330 
00331                 if ((unsigned long)iterator->_arg -
00332                     (unsigned long)iterator->_rtheader >
00333                     (unsigned long)iterator->_max_length)
00334                         return -EINVAL;
00335 
00336                 /* these special ones are valid in each bitmap word */
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                          * If parser didn't register this vendor
00344                          * namespace with us, allow it to show it
00345                          * as 'raw. Do do that, set argument index
00346                          * to vendor namespace.
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                          * bit 31 was set, there is more
00361                          * -- move to next u32 bitmap
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                         /* we've got a hit! */
00374                         hit = 1;
00375  next_entry:
00376                         iterator->_bitmap_shifter >>= 1;
00377                         iterator->_arg_index++;
00378                 }
00379 
00380                 /* if we found a valid arg earlier, return it now */
00381                 if (hit)
00382                         return 0;
00383         }
00384 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:55