libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
deauthentication.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 "deauthentication.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) +
29 sizeof(struct libwifi_deauth_fixed_parameters) + deauth->tags.length;
30}
31
37 const unsigned char receiver[6],
38 const unsigned char transmitter[6],
39 const unsigned char address3[6],
40 uint16_t reason_code) {
41 memset(deauth, 0, sizeof(struct libwifi_deauth));
42
45 memcpy(&deauth->frame_header.addr1, receiver, 6);
46 memcpy(&deauth->frame_header.addr2, transmitter, 6);
47 memcpy(&deauth->frame_header.addr3, address3, 6);
48
49 deauth->frame_header.seq_control.sequence_number = (rand() % 4096);
50
51 memcpy(&deauth->fixed_parameters.reason_code, &reason_code, sizeof(reason_code));
52
53 return 0;
54}
55
60size_t libwifi_dump_deauth(struct libwifi_deauth *deauth, unsigned char *buf, size_t buf_len) {
61 size_t deauth_len = libwifi_get_deauth_length(deauth);
62 if (deauth_len > buf_len) {
63 return -EINVAL;
64 }
65
66 size_t offset = 0;
67 memcpy(buf + offset, &deauth->frame_header, sizeof(struct libwifi_mgmt_unordered_frame_header));
68 offset += sizeof(struct libwifi_mgmt_unordered_frame_header);
69
70 memcpy(buf + offset, &deauth->fixed_parameters, sizeof(struct libwifi_deauth_fixed_parameters));
71 offset += sizeof(struct libwifi_deauth_fixed_parameters);
72
73 memcpy(buf + offset, deauth->tags.parameters, deauth->tags.length);
74 offset += deauth->tags.length;
75
76 return deauth_len;
77}
78
83void libwifi_free_deauth(struct libwifi_deauth *deauth) {
84 free(deauth->tags.parameters);
85}
struct libwifi_tagged_parameters tags
Definition: assoc_request.h:2
uint16_t reason_code
@ SUBTYPE_DEAUTH
Definition: frame.h:49
@ TYPE_MANAGEMENT
Definition: frame.h:31
size_t libwifi_dump_deauth(struct libwifi_deauth *deauth, unsigned char *buf, size_t buf_len)
Copy a libwifi_deauth into a regular unsigned char buffer.
void libwifi_free_deauth(struct libwifi_deauth *deauth)
Because the tagged parameters memory is managed inside of the library, the library must be the one to...
int libwifi_create_deauth(struct libwifi_deauth *deauth, const unsigned char receiver[6], const unsigned char transmitter[6], const unsigned char address3[6], uint16_t reason_code)
The generated deauthentication frame contains only the supplied receiver, transmitter and reason_code...
size_t libwifi_get_deauth_length(struct libwifi_deauth *deauth)
The length of a deauth frame is the sum of the header length, the fixed parameters length,...
Deauthentication Layout ────────────────────────── ┌──────────────────────────┐ │ Header │ Bytes: 24 ...
struct libwifi_tagged_parameters tags
struct libwifi_deauth_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