libwifi 0.0.3
An 802.11 Frame Parsing and Generation library in C
common.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 "../../core/frame/tag.h"
18#include "../misc/security.h"
19
20#include <errno.h>
21#include <stdlib.h>
22#include <string.h>
23
31void libwifi_handle_ssid_tag(void *target, int target_type, const char *tag_data, int tag_len) {
32 int hidden = 0;
33 int null_ssid = 1;
34
35 if (tag_len <= 0) {
36 hidden = 1;
37 } else if (tag_len > 32) {
38 tag_len = 32;
39 }
40
41 for (int i = 0; i < tag_len; i++) {
42 if (memcmp(&tag_data[i], "\x00", 1) != 0) {
43 null_ssid = 0;
44 break;
45 }
46 }
47
48 if (null_ssid) {
49 hidden = 1;
50 }
51
52 if (target_type == LIBWIFI_BSS) {
53 struct libwifi_bss *bss = (struct libwifi_bss *) target;
54 memcpy(bss->ssid, tag_data, tag_len);
55 bss->hidden = hidden;
56 } else if (target_type == LIBWIFI_STA) {
57 struct libwifi_sta *sta = (struct libwifi_sta *) target;
58 memcpy(sta->ssid, tag_data, tag_len);
59 }
60}
61
68int libwifi_bss_handle_rsn_tag(struct libwifi_bss *bss, const unsigned char *rsn_data, int rsn_len) {
69 struct libwifi_rsn_info rsn_info = {0};
70
71 bss->encryption_info &= ~(unsigned int) WEP;
72
73 int min_len = sizeof(rsn_info.rsn_version) + sizeof(struct libwifi_cipher_suite);
74 if (rsn_len < min_len) {
75 return -EINVAL;
76 }
77
78 const unsigned char *rsn_end = rsn_data + rsn_len;
79
80 if ((libwifi_get_rsn_info(&rsn_info, rsn_data, rsn_end) != 0)) {
81 return -EINVAL;
82 }
83
84 libwifi_enumerate_rsn_suites(&rsn_info, bss);
85
86 memcpy(&bss->rsn_info, &rsn_info, sizeof(struct libwifi_rsn_info));
87
88 return 0;
89}
90
102int libwifi_bss_handle_msft_tag(struct libwifi_bss *bss, const unsigned char *msft_data, int msft_len) {
103 struct libwifi_wpa_info wpa_info = {0};
104 struct libwifi_tag_vendor_header *vendor_header = (struct libwifi_tag_vendor_header *) msft_data;
105
106 switch (vendor_header->type) {
108 bss->encryption_info &= ~(unsigned int) WEP;
109 bss->encryption_info |= WPA;
110
111 // Skip 4 bytes for the OUI (3) and Vendor Tag Type (1)
112 const unsigned char *wpa_data = msft_data + sizeof(struct libwifi_tag_vendor_header);
113 const unsigned char *wpa_end = msft_data + (msft_len + sizeof(struct libwifi_tag_vendor_header));
114
115 if ((libwifi_get_wpa_info(&wpa_info, wpa_data, wpa_end) != 0)) {
116 return -EINVAL;
117 }
118
119 libwifi_enumerate_wpa_suites(&wpa_info, bss);
120
121 memcpy(&bss->wpa_info, &wpa_info, sizeof(struct libwifi_wpa_info));
122 break;
124 // WMM/WME Supported
125 break;
127 bss->wps = 1;
128 break;
129 }
130
131 return 0;
132}
133
140 struct libwifi_tag_vendor_header *vendor_header = NULL;
141
142 do {
143 switch (it->tag_header->tag_num) {
144 case TAG_SSID:
145 libwifi_handle_ssid_tag((void *) bss, LIBWIFI_BSS, (const char *) it->tag_data,
146 it->tag_header->tag_len);
147 break;
148 case TAG_DS_PARAMETER:
149 case TAG_HT_OPERATION:
150 memcpy(&bss->channel, it->tag_data, 1);
151 break;
152 case TAG_RSN:
153 if ((libwifi_bss_handle_rsn_tag(bss, it->tag_data, it->tag_header->tag_len) != 0)) {
154 return -EINVAL;
155 };
156 break;
158 vendor_header = (struct libwifi_tag_vendor_header *) it->tag_data;
159
160 if (memcmp(vendor_header->oui, MICROSOFT_OUI, 3) == 0) {
161 if ((libwifi_bss_handle_msft_tag(bss, it->tag_data, it->tag_header->tag_len) != 0)) {
162 return -EINVAL;
163 }
164 }
165 break;
166 }
167 } while (libwifi_tag_iterator_next(it) != -1);
168
169 return 0;
170}
171
178 do {
179 switch (it->tag_header->tag_num) {
180 case TAG_SSID:
181 libwifi_handle_ssid_tag(sta, LIBWIFI_STA, (const char *) it->tag_data,
182 it->tag_header->tag_len);
183 break;
184 case TAG_DS_PARAMETER:
185 memcpy(&sta->channel, it->tag_data, 1);
186 break;
187 }
188 } while (libwifi_tag_iterator_next(it) != -1);
189
190 return 0;
191}
int libwifi_bss_handle_rsn_tag(struct libwifi_bss *bss, const unsigned char *rsn_data, int rsn_len)
Handle the RSN Tagged Parameter.
Definition: common.c:68
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
int libwifi_sta_tag_parser(struct libwifi_sta *sta, struct libwifi_tag_iterator *it)
This function is a parser for common and useful tags found in frames usually originating from the STA...
Definition: common.c:177
void libwifi_handle_ssid_tag(void *target, int target_type, const char *tag_data, int tag_len)
Different implementations can have variations of hidden SSIDs.
Definition: common.c:31
int libwifi_bss_handle_msft_tag(struct libwifi_bss *bss, const unsigned char *msft_data, int msft_len)
The Microsoft vendor tag is used to advertise WPA and WPS information, as well as some other features...
Definition: common.c:102
#define LIBWIFI_BSS
Definition: common.h:24
#define LIBWIFI_STA
Definition: common.h:25
#define MICROSOFT_OUI_TYPE_WMM
Definition: security.h:36
#define WEP
Definition: security.h:86
#define WPA
Definition: security.h:87
#define MICROSOFT_OUI_TYPE_WPA
Definition: security.h:35
#define MICROSOFT_OUI_TYPE_WPS
Definition: security.h:37
#define MICROSOFT_OUI
Definition: security.h:31
void libwifi_enumerate_rsn_suites(struct libwifi_rsn_info *rsn_info, struct libwifi_bss *bss)
This function will enumerate over a supplied struct libwifi_rsn_info and write the following into a s...
Definition: security.c:123
int libwifi_get_wpa_info(struct libwifi_wpa_info *info, const unsigned char *tag_data, const unsigned char *tag_end)
Similar to libwifi_get_rsn_info, WPA Information is supplied via the raw tag data.
Definition: security.c:315
int libwifi_get_rsn_info(struct libwifi_rsn_info *info, const unsigned char *tag_data, const unsigned char *tag_end)
RSN Information is supplied via the raw tag data.
Definition: security.c:32
void libwifi_enumerate_wpa_suites(struct libwifi_wpa_info *wpa_info, struct libwifi_bss *bss)
Similarly to libwifi_enumerate_wpa_suites, this function will enumerate over a supplied struct libwif...
Definition: security.c:399
int8_t hidden
Definition: common.h:49
uint64_t encryption_info
Definition: common.h:52
struct libwifi_wpa_info wpa_info
Definition: common.h:54
char ssid[33]
Definition: common.h:48
uint8_t wps
Definition: common.h:51
struct libwifi_rsn_info rsn_info
Definition: common.h:55
libwifi Representation of a WPA or RSN cipher suite ┌────────────────────────┬────────────┐ │ OUI │ S...
Definition: security.h:172
libwifi Representation of a 802.11 RSN Information Element ┌───────────────────────────────────┐ │ Ve...
Definition: security.h:220
uint16_t rsn_version
Definition: security.h:221
uint8_t channel
Definition: common.h:82
char ssid[33]
Definition: common.h:87
uint8_t tag_num
Definition: tag.h:216
uint8_t tag_len
Definition: tag.h:217
A libwifi_tag_iterator is used to iterate through a list of tagged parameters in a wifi frame.
Definition: tag_iterator.h:28
struct libwifi_tag_header * tag_header
Definition: tag_iterator.h:29
const unsigned char * tag_data
Definition: tag_iterator.h:30
unsigned char oui[3]
Definition: tag.h:243
libwifi Representation of a Microsoft WPA Information Element ┌───────────────────────────────────┐ │...
Definition: security.h:193
uint8_t tag_len
Definition: tag.h:1
@ TAG_RSN
Definition: tag.h:57
@ TAG_SSID
Definition: tag.h:24
@ TAG_VENDOR_SPECIFIC
Definition: tag.h:207
@ TAG_HT_OPERATION
Definition: tag.h:70
@ TAG_DS_PARAMETER
Definition: tag.h:27
int libwifi_tag_iterator_next(struct libwifi_tag_iterator *it)
Iterate towards the next tagged parameter in a libwifi tag iterator.
Definition: tag_iterator.c:31