libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
radiotap.c
Go to the documentation of this file.
1/* Copyright 2021 The libwifi Authors
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "radiotap.h"
17#include "../../core/radiotap/radiotap_iter.h"
18
19#include <errno.h>
20#include <endian.h>
21#include <stdint.h>
22
27int libwifi_parse_radiotap_info(struct libwifi_radiotap_info *info, const unsigned char *frame, size_t frame_len) {
28 memset(info, 0, sizeof(struct libwifi_radiotap_info));
29
30 if (frame_len < sizeof(struct ieee80211_radiotap_header)) {
31 return -EINVAL;
32 }
33
34 struct ieee80211_radiotap_header *rh = (struct ieee80211_radiotap_header *) frame;
35 struct ieee80211_radiotap_iterator it = {0};
36 int ret = ieee80211_radiotap_iterator_init(&it, (void *) frame, rh->it_len, NULL);
37
38 int skipped_antenna = 0;
39 info->length = rh->it_len;
40
41 while (!ret) {
42 switch (it.this_arg_index) {
44 info->channel.freq = le16toh(*(uint16_t *) it.this_arg);
45 info->channel.flags = le16toh(*(uint16_t *) (it.this_arg + 2));
46 break;
48 info->rate_raw = *it.this_arg;
49 info->rate = (*it.this_arg / 2.0);
50 break;
52 // Radiotap Headers will include the ANTSIGNAL without an explicit Antenna definition.
53 if (!skipped_antenna) {
54 info->signal = *it.this_arg;
55 skipped_antenna = 1;
56 break;
57 }
58
60 struct libwifi_radiotap_antenna antenna = {.antenna_number = info->antenna_count,
61 .signal = *it.this_arg};
62 info->antennas[info->antenna_count] = antenna;
63 info->antenna_count += 1;
64 }
65 break;
67 info->antennas[info->antenna_count - 1].antenna_number = *it.this_arg;
68 break;
70 break;
72 info->flags = *it.this_arg;
73 break;
75 info->extended_flags = *it.this_arg;
76 break;
78 info->rx_flags = *it.this_arg;
79 break;
81 info->tx_flags = *it.this_arg;
82 break;
84 info->mcs.known = *(uint8_t *) it.this_arg;
85 info->mcs.flags = *(uint8_t *) (it.this_arg + 2);
86 info->mcs.mcs = *(uint8_t *) (it.this_arg + 3);
87 break;
89 info->tx_power = *it.this_arg;
90 break;
92 info->timestamp.timestamp = le64toh(*(uint64_t *) it.this_arg);
93 info->timestamp.accuracy = le16toh(*(uint16_t *) (it.this_arg + 2));
94 info->timestamp.unit = *(uint8_t *) (it.this_arg + 3);
95 info->timestamp.flags = *(uint8_t *) (it.this_arg + 4);
96 break;
98 info->rts_retries = *it.this_arg;
99 break;
101 info->data_retries = *it.this_arg;
102 break;
103 }
104
106 }
107
108 return 0;
109}
110
115int8_t libwifi_parse_radiotap_rssi(const unsigned char *frame) {
116 struct ieee80211_radiotap_header *rh = (struct ieee80211_radiotap_header *) frame;
117
118 int8_t rssi = 0;
119
121 int ret = ieee80211_radiotap_iterator_init(&it, (void *) frame, rh->it_len, NULL);
122
123 while (!ret) {
125 rssi = *it.this_arg;
126 break;
127 }
128
130 }
131
132 return rssi;
133}
#define LIBWIFI_MAX_RADIOTAP_ANTENNAS
Definition: radiotap.h:22
EXPORT int ieee80211_radiotap_iterator_next(struct ieee80211_radiotap_iterator *iterator)
ieee80211_radiotap_iterator_next - return next radiotap parser iterator arg @iterator: radiotap_itera...
Definition: radiotap.c:317
EXPORT int 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)
ieee80211_radiotap_iterator_init - radiotap parser iterator initialization @iterator: radiotap_iterat...
Definition: radiotap.c:179
@ IEEE80211_RADIOTAP_EXT
Definition: radiotap.h:97
@ IEEE80211_RADIOTAP_DATA_RETRIES
Definition: radiotap.h:87
@ IEEE80211_RADIOTAP_DBM_TX_POWER
Definition: radiotap.h:80
@ IEEE80211_RADIOTAP_TX_FLAGS
Definition: radiotap.h:85
@ IEEE80211_RADIOTAP_RTS_RETRIES
Definition: radiotap.h:86
@ IEEE80211_RADIOTAP_MCS
Definition: radiotap.h:89
@ IEEE80211_RADIOTAP_CHANNEL
Definition: radiotap.h:73
@ IEEE80211_RADIOTAP_FLAGS
Definition: radiotap.h:71
@ IEEE80211_RADIOTAP_TIMESTAMP
Definition: radiotap.h:92
@ IEEE80211_RADIOTAP_RATE
Definition: radiotap.h:72
@ IEEE80211_RADIOTAP_DBM_ANTNOISE
Definition: radiotap.h:76
@ IEEE80211_RADIOTAP_ANTENNA
Definition: radiotap.h:81
@ IEEE80211_RADIOTAP_DBM_ANTSIGNAL
Definition: radiotap.h:75
@ IEEE80211_RADIOTAP_RX_FLAGS
Definition: radiotap.h:84
int libwifi_parse_radiotap_info(struct libwifi_radiotap_info *info, const unsigned char *frame, size_t frame_len)
The libwifi radiotap parser uses the usual ieee80211_radiotap_iterator to parse incoming radiotap hea...
Definition: radiotap.c:27
int8_t libwifi_parse_radiotap_rssi(const unsigned char *frame)
A simpler function than the main libwifi_parse_radiotap_info function, designed to extract only the s...
Definition: radiotap.c:115
struct ieee82011_radiotap_header - base radiotap header
Definition: radiotap.h:41
uint16_t it_len
@it_len: overall radiotap header length
Definition: radiotap.h:55
struct ieee80211_radiotap_iterator - tracks walk thru present radiotap args @this_arg_index: index of...
Definition: radiotap_iter.h:65
The radiotap antenna field consists of an antenna number and signal in dBm.
Definition: radiotap.h:36
uint32_t extended_flags
Definition: radiotap.h:71
struct libwifi_radiotap_timestamp timestamp
Definition: radiotap.h:76
uint8_t data_retries
Definition: radiotap.h:78
struct libwifi_radiotap_antenna antennas[LIBWIFI_MAX_RADIOTAP_ANTENNAS]
Definition: radiotap.h:68
struct libwifi_radiotap_channel channel
Definition: radiotap.h:64
uint8_t antenna_count
Definition: radiotap.h:67
struct libwifi_radiotap_mcs mcs
Definition: radiotap.h:74