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.h"
18#include "../../core/radiotap/radiotap_iter.h"
19
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define LIBWIFI_RADIOTAP_HEADER_LEN 8
25
26size_t libwifi_create_radiotap(struct libwifi_radiotap_info *info, char *radiotap_header) {
27 struct ieee80211_radiotap_header rtap_hdr = {0};
28 rtap_hdr.it_version = 0;
29 rtap_hdr.it_pad = 0;
30 rtap_hdr.it_present = info->present;
31 rtap_hdr.it_len = sizeof(struct ieee80211_radiotap_header);
32
34 int offset = 0;
35
36 uint32_t presence_bit = rtap_hdr.it_present;
37 for (int field = 0; field < radiotap_ns.n_bits; field++) {
38 if (presence_bit & 1) {
39 switch (field) {
41 memcpy(rtap_data + offset, &info->channel.freq, sizeof(info->channel.freq));
42 offset += sizeof(info->channel.freq);
43 memcpy(rtap_data + offset, &info->channel.flags, sizeof(info->channel.flags));
44 offset += sizeof(info->channel.flags);
45 break;
47 memcpy(rtap_data + offset, &info->rate_raw, sizeof(info->rate_raw));
48 offset += sizeof(info->rate_raw);
49 break;
51 memcpy(rtap_data + offset, &info->signal, sizeof(info->signal));
52 offset += sizeof(info->signal);
53 break;
55 for (int i = 0; i < info->antenna_count; i++) {
56 memcpy(rtap_data + offset, &info->antennas->antenna_number,
57 sizeof(info->antennas->antenna_number));
58 offset += sizeof(info->antennas->antenna_number);
59 memcpy(rtap_data + offset, &info->antennas->signal, sizeof(info->antennas->signal));
60 offset += sizeof(info->antennas->signal);
61 }
62 break;
64 break;
66 memcpy(rtap_data + offset, &info->flags, sizeof(info->flags));
67 offset += sizeof(info->flags);
68 break;
70 memcpy(rtap_data + offset, &info->extended_flags, sizeof(info->extended_flags));
71 offset += sizeof(info->extended_flags);
72 break;
74 memcpy(rtap_data + offset, &info->rx_flags, sizeof(info->rx_flags));
75 offset += sizeof(info->rx_flags);
76 break;
78 memcpy(rtap_data + offset, &info->tx_flags, sizeof(info->tx_flags));
79 offset += sizeof(info->tx_flags);
80 break;
82 memcpy(rtap_data + offset, &info->mcs.known, sizeof(info->mcs.known));
83 offset += sizeof(info->mcs.known);
84 memcpy(rtap_data + offset, &info->mcs.flags, sizeof(info->mcs.flags));
85 offset += sizeof(info->mcs.flags);
86 memcpy(rtap_data + offset, &info->mcs.mcs, sizeof(info->mcs.mcs));
87 offset += sizeof(info->mcs.mcs);
88 break;
90 memcpy(rtap_data + offset, &info->tx_power, sizeof(info->tx_power));
91 offset += sizeof(info->tx_power);
92 break;
94 memcpy(rtap_data + offset, &info->timestamp.timestamp, sizeof(info->timestamp.timestamp));
95 offset += sizeof(info->timestamp.timestamp);
96 memcpy(rtap_data + offset, &info->timestamp.accuracy, sizeof(info->timestamp.accuracy));
97 offset += sizeof(info->timestamp.accuracy);
98 memcpy(rtap_data + offset, &info->timestamp.unit, sizeof(info->timestamp.unit));
99 offset += sizeof(info->timestamp.unit);
100 memcpy(rtap_data + offset, &info->timestamp.flags, sizeof(info->timestamp.flags));
101 offset += sizeof(info->timestamp.flags);
102 break;
104 memcpy(rtap_data + offset, &info->rts_retries, sizeof(info->rts_retries));
105 offset += sizeof(info->rts_retries);
106 break;
108 memcpy(rtap_data + offset, &info->data_retries, sizeof(info->data_retries));
109 offset += sizeof(info->data_retries);
110 break;
111 }
112 }
113
114 presence_bit >>= 1;
115 }
116
117 rtap_hdr.it_len += offset;
118
119 memcpy(radiotap_header, &rtap_hdr, sizeof(struct ieee80211_radiotap_header));
120 memcpy(radiotap_header + sizeof(struct ieee80211_radiotap_header), &rtap_data, offset);
121
122 return rtap_hdr.it_len;
123}
#define LIBWIFI_MAX_RADIOTAP_LEN
Definition: radiotap.h:21
const struct ieee80211_radiotap_namespace radiotap_ns
Definition: radiotap.c:135
@ 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
size_t libwifi_create_radiotap(struct libwifi_radiotap_info *info, char *radiotap_header)
Definition: radiotap.c:26
#define LIBWIFI_RADIOTAP_HEADER_LEN
Definition: radiotap.c:24
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
uint8_t it_version
@it_version: radiotap version, always 0
Definition: radiotap.h:45
uint8_t it_pad
@it_pad: padding (or alignment)
Definition: radiotap.h:50
uint32_t it_present
@it_present: (first) present word
Definition: radiotap.h:60
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