libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
timing_ad.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 "common.h"
17#include "timing_ad.h"
18#include "../../core/frame/management/timing_ad.h"
19#include "../../core/misc/epoch.h"
20#include "../../core/frame/tag.h"
21
22#include <errno.h>
23#include <stdlib.h>
24#include <string.h>
25
27 const unsigned char destination[6],
28 const unsigned char transmitter[6],
29 const unsigned char address3[6],
30 struct libwifi_timing_advert_fields *adv_fields,
31 const char country[3],
32 uint16_t max_reg_power,
33 uint8_t max_tx_power,
34 uint8_t tx_power_used,
35 uint8_t noise_floor) {
36 memset(adv, 0, sizeof(struct libwifi_timing_advert));
37
40 memcpy(&adv->frame_header.addr1, destination, 6);
41 memcpy(&adv->frame_header.addr2, transmitter, 6);
42 memcpy(&adv->frame_header.addr3, address3, 6);
43 adv->frame_header.seq_control.sequence_number = (rand() % 4096);
44
49 memcpy(adv->fixed_parameters.country, country, sizeof(adv->fixed_parameters.country));
54
55 if (adv_fields == NULL) {
56 return -EINVAL;
57 }
58
59 // Maximum element size is 17
60 unsigned char element_data[17] = {0};
61 size_t element_data_len = 0;
62 int offset = 0;
63
64 memcpy(element_data, &adv_fields->timing_capabilities, sizeof(adv_fields->timing_capabilities));
65 offset += sizeof(adv_fields->timing_capabilities);
66
67 switch (adv_fields->timing_capabilities) {
68 case 1: { /* Time Value and Time Error fields present */
69 memcpy(element_data + offset, &adv_fields->time_value, sizeof(adv_fields->time_value));
70 offset += sizeof(adv_fields->time_value);
71 memcpy(element_data + offset, &adv_fields->time_error, sizeof(adv_fields->time_error));
72 offset += sizeof(adv_fields->time_error);
73 break;
74 }
75 case 2: { /* Time Value, Time Error, and Time Update fields present */
76 memcpy(element_data + offset, &adv_fields->time_value, sizeof(adv_fields->time_value));
77 offset += sizeof(adv_fields->time_value);
78 memcpy(element_data + offset, &adv_fields->time_error, sizeof(adv_fields->time_error));
79 offset += sizeof(adv_fields->time_error);
80 memcpy(element_data + offset, &adv_fields->time_update, sizeof(adv_fields->time_update));
81 offset += sizeof(adv_fields->time_update);
82 }
83 default:
84 break;
85 }
86
87 element_data_len = offset;
88
89 int ret = libwifi_quick_add_tag(&adv->tags, TAG_TIME_ADVERTISEMENT, element_data, element_data_len);
90
91 return ret;
92}
93
95 return sizeof(struct libwifi_mgmt_unordered_frame_header) +
97 adv->tags.length;
98}
99
100size_t libwifi_dump_timing_advert(struct libwifi_timing_advert *adv, unsigned char *buf, size_t buf_len) {
101 size_t adv_len = libwifi_get_timing_advert_length(adv);
102 if (adv_len > buf_len) {
103 return -1;
104 }
105
106 size_t offset = 0;
107 memcpy(buf + offset, &adv->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
108 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
109
110 memcpy(buf + offset, &adv->fixed_parameters, sizeof(struct libwifi_timing_advert_fixed_params));
111 offset += sizeof(struct libwifi_timing_advert_fixed_params);
112
113 memcpy(buf + offset, adv->tags.parameters, adv->tags.length);
114 offset += adv->tags.length;
115
116 return adv_len;
117}
118
120 free(adv->tags.parameters);
121}
#define BYTESWAP16(x)
Definition: byteswap.h:22
#define BYTESWAP64(x)
Definition: byteswap.h:24
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
char country[3]
Definition: timing_ad.h:4
uint16_t max_reg_power
Definition: timing_ad.h:5
uint8_t max_tx_power
Definition: timing_ad.h:6
uint8_t noise_floor
Definition: timing_ad.h:8
uint8_t tx_power_used
Definition: timing_ad.h:7
unsigned long long libwifi_get_epoch(void)
Get the current system time in epoch.
Definition: epoch.c:19
@ SUBTYPE_TIME_ADV
Definition: frame.h:43
@ TYPE_MANAGEMENT
Definition: frame.h:31
#define LIBWIFI_DEFAULT_BEACON_INTERVAL
A sane default for a beacon_interval field.
Definition: common.h:45
#define LIBWIFI_DEFAULT_AP_CAPABS
A sane default for an AP-side capabilities information field.
Definition: common.h:24
unsigned int type
Definition: frame.h:123
unsigned int subtype
Definition: frame.h:124
struct libwifi_seq_control seq_control
Definition: frame.h:213
struct libwifi_frame_ctrl frame_control
Definition: frame.h:208
unsigned int sequence_number
Definition: frame.h:133
unsigned char * parameters
Definition: tag.h:235
unsigned char time_value[10]
Definition: timing_ad.h:59
unsigned char time_error[5]
Definition: timing_ad.h:60
unsigned char time_update[1]
Definition: timing_ad.h:61
struct libwifi_tagged_parameters tags
Definition: timing_ad.h:79
struct libwifi_timing_advert_fixed_params fixed_parameters
Definition: timing_ad.h:78
struct libwifi_mgmt_unordered_frame_header frame_header
Definition: timing_ad.h:77
int libwifi_quick_add_tag(struct libwifi_tagged_parameters *tags, int tag_number, const unsigned char *tag_data, size_t tag_length)
Add a tagged parameter via tag number and data to a management frame.
Definition: tag.c:118
@ TAG_TIME_ADVERTISEMENT
Definition: tag.h:78
size_t length
Definition: tag.h:0
size_t libwifi_dump_timing_advert(struct libwifi_timing_advert *adv, unsigned char *buf, size_t buf_len)
Dump a libwifi_timing_advert into a raw format for packet injection.
Definition: timing_ad.c:100
void libwifi_free_timing_advert(struct libwifi_timing_advert *adv)
Free any memory claimed by a libwifi_timing_advert back to the system.
Definition: timing_ad.c:119
size_t libwifi_get_timing_advert_length(struct libwifi_timing_advert *adv)
Get the length of the specified libwifi_timing_advert struct.
Definition: timing_ad.c:94
int libwifi_create_timing_advert(struct libwifi_timing_advert *adv, const unsigned char destination[6], const unsigned char transmitter[6], const unsigned char address3[6], struct libwifi_timing_advert_fields *adv_fields, const char country[3], uint16_t max_reg_power, uint8_t max_tx_power, uint8_t tx_power_used, uint8_t noise_floor)
Create a populated libwifi_timing_advert struct.
Definition: timing_ad.c:26