libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
authentication.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 "authentication.h"
17#include "../../core/misc/byteswap.h"
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22
28 return sizeof(struct libwifi_mgmt_unordered_frame_header) + sizeof(struct libwifi_auth_fixed_parameters) +
29 auth->tags.length;
30}
31
36 const unsigned char receiver[6],
37 const unsigned char transmitter[6],
38 const unsigned char address3[6],
39 uint16_t algorithm_number,
40 uint16_t transaction_sequence,
41 uint16_t status_code) {
42 memset(auth, 0, sizeof(struct libwifi_auth));
43
46 memcpy(&auth->frame_header.addr1, receiver, 6);
47 memcpy(&auth->frame_header.addr2, transmitter, 6);
48 memcpy(&auth->frame_header.addr3, address3, 6);
49 auth->frame_header.seq_control.sequence_number = (rand() % 4096);
50
54
55 return 0;
56}
57
62size_t libwifi_dump_auth(struct libwifi_auth *auth, unsigned char *buf, size_t buf_len) {
63 size_t auth_len = libwifi_get_auth_length(auth);
64 if (auth_len > buf_len) {
65 return -EINVAL;
66 }
67
68 size_t offset = 0;
69 memcpy(buf + offset, &auth->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
70 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
71
72 memcpy(buf + offset, &auth->fixed_parameters, sizeof(struct libwifi_auth_fixed_parameters));
73 offset += sizeof(struct libwifi_auth_fixed_parameters);
74
75 memcpy(buf + offset, auth->tags.parameters, auth->tags.length);
76 offset += auth->tags.length;
77
78 return auth_len;
79}
80
85void libwifi_free_auth(struct libwifi_auth *auth) {
86 free(auth->tags.parameters);
87}
size_t libwifi_get_auth_length(struct libwifi_auth *auth)
The length of an authentication frame is the sum of the header length, the fixed parameters length,...
int libwifi_create_auth(struct libwifi_auth *auth, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint16_t algorithm_number, uint16_t transaction_sequence, uint16_t status_code)
The generated authentication frame is made with sane defaults defined in common.h.
size_t libwifi_dump_auth(struct libwifi_auth *auth, unsigned char *buf, size_t buf_len)
Copy a libwifi_auth into a regular unsigned char buffer.
void libwifi_free_auth(struct libwifi_auth *auth)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
uint16_t status_code
Definition: assoc_response.h:1
uint16_t transaction_sequence
Definition: authentication.h:1
uint16_t algorithm_number
Definition: authentication.h:0
@ SUBTYPE_AUTH
Definition: frame.h:48
@ TYPE_MANAGEMENT
Definition: frame.h:31
struct libwifi_tagged_parameters tags
struct libwifi_auth_fixed_parameters fixed_parameters
struct libwifi_mgmt_unordered_frame_header frame_header
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
size_t length
Definition: tag.h:0