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 "../../core/frame/management/beacon.h"
17#include "../../core/frame/frame.h"
18#include "../../core/frame/tag.h"
19#include "../../core/frame/tag_iterator.h"
20#include "../../core/misc/types.h"
21#include "../../parse/misc/security.h"
22#include "beacon.h"
23#include "common.h"
24
25#include <errno.h>
26#include <stdlib.h>
27#include <string.h>
28
41int libwifi_parse_beacon(struct libwifi_bss *bss, struct libwifi_frame *frame) {
42 memset(bss, 0, sizeof(struct libwifi_bss));
43
45 return -EINVAL;
46 }
47
48 if (frame->frame_control.flags.ordered) {
49 memcpy(bss->receiver, frame->header.mgmt_ordered.addr1, 6);
50 memcpy(bss->transmitter, frame->header.mgmt_ordered.addr2, 6);
51 memcpy(bss->bssid, frame->header.mgmt_ordered.addr3, 6);
52 } else {
53 memcpy(bss->receiver, frame->header.mgmt_unordered.addr1, 6);
54 memcpy(bss->transmitter, frame->header.mgmt_unordered.addr2, 6);
55 memcpy(bss->bssid, frame->header.mgmt_unordered.addr3, 6);
56 }
57
58 // Fixed Parameters must be present
59 if (frame->len <= (frame->header_len + sizeof(struct libwifi_beacon_fixed_parameters))) {
60 return -EINVAL;
61 }
62
63 // At least one Tagged Parameter must be present
64 if (frame->len < (frame->header_len + sizeof(struct libwifi_beacon_fixed_parameters) + 2)) {
65 return -EINVAL;
66 }
67
68 struct libwifi_beacon_fixed_parameters *fixed_params =
69 (struct libwifi_beacon_fixed_parameters *) frame->body;
71 bss->encryption_info |= WEP;
72 }
73
74 bss->tags.length = (frame->len - (frame->header_len + sizeof(struct libwifi_beacon_fixed_parameters)));
75 const unsigned char *tagged_params = frame->body + sizeof(struct libwifi_beacon_fixed_parameters);
76 bss->tags.parameters = malloc(bss->tags.length);
77 memcpy(bss->tags.parameters, tagged_params, bss->tags.length);
78
79 // Iterate through common BSS tagged parameters (WPA, RSN, etc)
80 struct libwifi_tag_iterator it;
81 memset(&it, 0, sizeof(struct libwifi_tag_iterator));
82 if (libwifi_tag_iterator_init(&it, bss->tags.parameters, bss->tags.length) != 0) {
83 return -EINVAL;
84 }
85 if (libwifi_bss_tag_parser(bss, &it) != 0) {
86 return -EINVAL;
87 };
88
89 return 0;
90}
int libwifi_bss_tag_parser(struct libwifi_bss *bss, struct libwifi_tag_iterator *it)
This function is a parser for common and useful tags found in frames usually originating from the BSS...
Definition: common.c:139
#define WEP
Definition: security.h:86
@ SUBTYPE_BEACON
Definition: frame.h:45
@ TYPE_MANAGEMENT
Definition: frame.h:31
int libwifi_parse_beacon(struct libwifi_bss *bss, struct libwifi_frame *frame)
libwifi_parse_beacon will parse useful fields out of a supplied beacon frame in the format of a struc...
Definition: beacon.c:41
struct libwifi_tagged_parameters tags
Definition: common.h:56
unsigned char transmitter[6]
Definition: common.h:45
uint64_t encryption_info
Definition: common.h:52
unsigned char receiver[6]
Definition: common.h:46
unsigned char bssid[6]
Definition: common.h:47
unsigned int ordered
Definition: frame.h:115
unsigned int type
Definition: frame.h:123
struct libwifi_frame_ctrl_flags flags
Definition: frame.h:125
unsigned int subtype
Definition: frame.h:124
union libwifi_frame_header header
Definition: frame.h:312
unsigned char * body
Definition: frame.h:314
size_t len
Definition: frame.h:311
struct libwifi_frame_ctrl frame_control
Definition: frame.h:310
size_t header_len
Definition: frame.h:313
unsigned char addr3[6]
Definition: frame.h:185
unsigned char addr2[6]
Definition: frame.h:184
unsigned char addr1[6]
Definition: frame.h:183
A libwifi_tag_iterator is used to iterate through a list of tagged parameters in a wifi frame.
Definition: tag_iterator.h:28
unsigned char * parameters
Definition: tag.h:235
int libwifi_tag_iterator_init(struct libwifi_tag_iterator *it, const void *tags_start, size_t data_len)
Initialise a libwifi frame tag iterator.
Definition: tag_iterator.c:21
#define libwifi_check_capabilities(x, cap)
Definition: types.h:199
@ CAPABILITIES_PRIVACY
Definition: types.h:205
struct libwifi_mgmt_unordered_frame_header mgmt_unordered
Definition: frame.h:290
struct libwifi_mgmt_ordered_frame_header mgmt_ordered
Definition: frame.h:289