libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
beacon.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 "beacon.h"
17#include "../../core/frame/tag.h"
18#include "../../core/frame/tag_iterator.h"
19#include "../../core/misc/byteswap.h"
20#include "../../core/misc/epoch.h"
21#include "common.h"
22
23#include <errno.h>
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/types.h>
28
34 return sizeof(struct libwifi_mgmt_unordered_frame_header) +
35 sizeof(struct libwifi_beacon_fixed_parameters) +
36 beacon->tags.length;
37}
38
42int libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid) {
43 int ret = 0;
44
45 if (beacon->tags.length != 0) {
46 ret = libwifi_remove_tag(&beacon->tags, TAG_SSID);
47 if (ret != 0) {
48 return ret;
49 }
50 }
51
52 ret = libwifi_quick_add_tag(&beacon->tags, TAG_SSID, (void *) ssid, strlen(ssid));
53
54 return ret;
55}
56
61 int ret = 0;
62
63 if (beacon->tags.length != 0) {
65 if (ret != 0) {
66 return ret;
67 }
68 }
69
70 const unsigned char *chan = (const unsigned char *) &channel;
71
72 ret = libwifi_quick_add_tag(&beacon->tags, TAG_DS_PARAMETER, chan, 1);
73
74 return ret;
75}
76
82 const unsigned char receiver[6],
83 const unsigned char transmitter[6],
84 const unsigned char address3[6],
85 const char *ssid,
86 uint8_t channel) {
87 memset(beacon, 0, sizeof(struct libwifi_beacon));
88
91 memcpy(&beacon->frame_header.addr1, receiver, 6);
92 memcpy(&beacon->frame_header.addr2, transmitter, 6);
93 memcpy(&beacon->frame_header.addr3, address3, 6);
94 beacon->frame_header.seq_control.sequence_number = (rand() % 4096);
95
99
100 int ret = libwifi_set_beacon_ssid(beacon, ssid);
101 if (ret != 0) {
102 return ret;
103 }
104
105 ret = libwifi_set_beacon_channel(beacon, channel);
106
107 return ret;
108}
109
114size_t libwifi_dump_beacon(struct libwifi_beacon *beacon, unsigned char *buf, size_t buf_len) {
115 size_t beacon_len = libwifi_get_beacon_length(beacon);
116 if (beacon_len > buf_len) {
117 return -EINVAL;
118 }
119
120 size_t offset = 0;
121 memcpy(buf + offset, &beacon->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
122 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
123
124 memcpy(buf + offset, &beacon->fixed_parameters, sizeof(struct libwifi_beacon_fixed_parameters));
125 offset += sizeof(struct libwifi_beacon_fixed_parameters);
126
127 memcpy(buf + offset, beacon->tags.parameters, beacon->tags.length);
128 offset += beacon->tags.length;
129
130 return beacon_len;
131}
132
138 free(beacon->tags.parameters);
139}
#define BYTESWAP16(x)
Definition: byteswap.h:22
#define BYTESWAP64(x)
Definition: byteswap.h:24
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
struct libwifi_radiotap_channel channel
Definition: radiotap.h:3
unsigned long long libwifi_get_epoch(void)
Get the current system time in epoch.
Definition: epoch.c:19
@ SUBTYPE_BEACON
Definition: frame.h:45
@ TYPE_MANAGEMENT
Definition: frame.h:31
int libwifi_create_beacon(struct libwifi_beacon *beacon, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], const char *ssid, uint8_t channel)
The generated beacon frame is made with sane defaults defined in common.h.
Definition: beacon.c:81
size_t libwifi_dump_beacon(struct libwifi_beacon *beacon, unsigned char *buf, size_t buf_len)
Copy a libwifi_beacon into a regular unsigned char buffer.
Definition: beacon.c:114
int libwifi_set_beacon_channel(struct libwifi_beacon *beacon, uint8_t channel)
Simple helper to set the beacon DS tag by removing it and then adding it back with the new value.
Definition: beacon.c:60
int libwifi_set_beacon_ssid(struct libwifi_beacon *beacon, const char *ssid)
Simple helper to set the beacon SSID tag by removing it and then adding it back with the new value.
Definition: beacon.c:42
void libwifi_free_beacon(struct libwifi_beacon *beacon)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
Definition: beacon.c:137
size_t libwifi_get_beacon_length(struct libwifi_beacon *beacon)
The length of a beacon frame is the sum of the header length, the fixed parameters length,...
Definition: beacon.c:33
#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
struct libwifi_tagged_parameters tags
Definition: beacon.h:51
struct libwifi_mgmt_unordered_frame_header frame_header
Definition: beacon.h:49
struct libwifi_beacon_fixed_parameters fixed_parameters
Definition: beacon.h:50
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
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
int libwifi_remove_tag(struct libwifi_tagged_parameters *tags, int tag_number)
Remove a tagged parameter from a list of frame tagged parameters.
Definition: tag.c:55
@ TAG_SSID
Definition: tag.h:24
@ TAG_DS_PARAMETER
Definition: tag.h:27
size_t length
Definition: tag.h:0