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