1 | /*
|
---|
2 | * Copyright (c) 2015 Jan Kolarik
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @addtogroup libieee80211
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /** @file ieee80211.h
|
---|
35 | *
|
---|
36 | * Internal IEEE 802.11 header that should not be included.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #ifndef LIBNET_IEEE80211_PRIVATE_H
|
---|
40 | #define LIBNET_IEEE80211_PRIVATE_H
|
---|
41 |
|
---|
42 | #include <fibril_synch.h>
|
---|
43 | #include <byteorder.h>
|
---|
44 | #include <ddf/driver.h>
|
---|
45 | #include <sys/types.h>
|
---|
46 |
|
---|
47 | #include <ieee80211/ieee80211.h>
|
---|
48 | #include "ieee80211.h"
|
---|
49 |
|
---|
50 | /* Timeout in us for waiting to authentication/association response. */
|
---|
51 | #define AUTH_TIMEOUT 200000
|
---|
52 |
|
---|
53 | /* Timeout in us for waiting to finish 4-way handshake process. */
|
---|
54 | #define HANDSHAKE_TIMEOUT 3000000
|
---|
55 |
|
---|
56 | /* Max period to rerun scan. */
|
---|
57 | #define MAX_SCAN_SPAN_SEC 30
|
---|
58 |
|
---|
59 | /* Max time to keep scan result. */
|
---|
60 | #define MAX_KEEP_SCAN_SPAN_SEC 120
|
---|
61 |
|
---|
62 | /* Security bit in capability info field. */
|
---|
63 | #define CAP_SECURITY 0x10
|
---|
64 |
|
---|
65 | /* Protocol type used in EAPOL frames. */
|
---|
66 | #define ETH_TYPE_PAE 0x888E
|
---|
67 |
|
---|
68 | /* WPA OUI used in vendor specific IE. */
|
---|
69 | #define WPA_OUI 0x0050F201
|
---|
70 |
|
---|
71 | /* GTK OUI used in vendor specific IE. */
|
---|
72 | #define GTK_OUI 0x000FAC01
|
---|
73 |
|
---|
74 | /* Max PTK key length. */
|
---|
75 | #define MAX_PTK_LENGTH 64
|
---|
76 |
|
---|
77 | /* Max GTK key length. */
|
---|
78 | #define MAX_GTK_LENGTH 64
|
---|
79 |
|
---|
80 | /* KEK offset inside PTK. */
|
---|
81 | #define KEK_OFFSET 16
|
---|
82 |
|
---|
83 | /* TK offset inside PTK. */
|
---|
84 | #define TK_OFFSET 32
|
---|
85 |
|
---|
86 | /* Length of CCMP header we need to reserve. */
|
---|
87 | #define IEEE80211_CCMP_HEADER_LENGTH 8
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Length of data to be encrypted by PRF function:
|
---|
91 | * NONCE + SNONCE (2 * 32) + DEST_MAC + SOURCE_MAC (2 * ETH_ADDR)
|
---|
92 | */
|
---|
93 | #define PRF_CRYPT_DATA_LENGTH 2*32 + 2*ETH_ADDR
|
---|
94 |
|
---|
95 | /** IEEE 802.11 PTK key length. */
|
---|
96 | typedef enum {
|
---|
97 | IEEE80211_PTK_CCMP_LENGTH = 48,
|
---|
98 | IEEE80211_PTK_TKIP_LENGTH = 64
|
---|
99 | } ieee80211_ptk_length_t;
|
---|
100 |
|
---|
101 | /** IEEE 802.11 GTK key length. */
|
---|
102 | typedef enum {
|
---|
103 | IEEE80211_GTK_CCMP_LENGTH = 16,
|
---|
104 | IEEE80211_GTK_TKIP_LENGTH = 32
|
---|
105 | } ieee80211_gtk_length_t;
|
---|
106 |
|
---|
107 | /** IEEE 802.11 frame types. */
|
---|
108 | typedef enum {
|
---|
109 | IEEE80211_MGMT_FRAME = 0x0,
|
---|
110 | IEEE80211_CTRL_FRAME = 0x4,
|
---|
111 | IEEE80211_DATA_FRAME = 0x8,
|
---|
112 | IEEE80211_EXT_FRAME = 0xC
|
---|
113 | } ieee80211_frame_type_t;
|
---|
114 |
|
---|
115 | /** IEEE 802.11 management frame subtypes. */
|
---|
116 | typedef enum {
|
---|
117 | IEEE80211_MGMT_ASSOC_REQ_FRAME = 0x00,
|
---|
118 | IEEE80211_MGMT_ASSOC_RESP_FRAME = 0x10,
|
---|
119 | IEEE80211_MGMT_REASSOC_REQ_FRAME = 0x20,
|
---|
120 | IEEE80211_MGMT_REASSOC_RESP_FRAME = 0x30,
|
---|
121 | IEEE80211_MGMT_PROBE_REQ_FRAME = 0x40,
|
---|
122 | IEEE80211_MGMT_PROBE_RESP_FRAME = 0x50,
|
---|
123 | IEEE80211_MGMT_BEACON_FRAME = 0x80,
|
---|
124 | IEEE80211_MGMT_DISASSOC_FRAME = 0xA0,
|
---|
125 | IEEE80211_MGMT_AUTH_FRAME = 0xB0,
|
---|
126 | IEEE80211_MGMT_DEAUTH_FRAME = 0xC0,
|
---|
127 | } ieee80211_frame_mgmt_subtype_t;
|
---|
128 |
|
---|
129 | /** IEEE 802.11 data frame subtypes. */
|
---|
130 | typedef enum {
|
---|
131 | IEEE80211_DATA_DATA_FRAME = 0x0000,
|
---|
132 | IEEE80211_DATA_QOS_FRAME = 0x0080
|
---|
133 | } ieee80211_frame_data_subtype_t;
|
---|
134 |
|
---|
135 | /** IEEE 802.11 frame control value masks. */
|
---|
136 | typedef enum {
|
---|
137 | IEEE80211_FRAME_CTRL_FRAME_TYPE = 0x000C,
|
---|
138 | IEEE80211_FRAME_CTRL_FRAME_SUBTYPE = 0x00F0,
|
---|
139 | IEEE80211_FRAME_CTRL_PROTECTED = 0x4000
|
---|
140 | } ieee80211_frame_ctrl_mask_t;
|
---|
141 |
|
---|
142 | /** IEEE 802.11 frame control DS field values. */
|
---|
143 | typedef enum {
|
---|
144 | IEEE80211_FRAME_CTRL_TODS = 0x0100,
|
---|
145 | IEEE80211_FRAME_CTRL_FROMDS = 0x0200
|
---|
146 | } ieee80211_frame_ctrl_ds_t;
|
---|
147 |
|
---|
148 | /** IEEE 802.11 authentication cipher suites values. */
|
---|
149 | typedef enum {
|
---|
150 | IEEE80211_AUTH_CIPHER_TKIP = 0x02,
|
---|
151 | IEEE80211_AUTH_CIPHER_CCMP = 0x04
|
---|
152 | } ieee80211_auth_cipher_type_t;
|
---|
153 |
|
---|
154 | /** IEEE 802.11 AKM suites values. */
|
---|
155 | typedef enum {
|
---|
156 | IEEE80211_AUTH_AKM_8021X = 0x01,
|
---|
157 | IEEE80211_AUTH_AKM_PSK = 0x02
|
---|
158 | } ieee80211_auth_akm_type_t;
|
---|
159 |
|
---|
160 | typedef enum {
|
---|
161 | IEEE80211_EAPOL_START = 0x1,
|
---|
162 | IEEE80211_EAPOL_KEY = 0x3
|
---|
163 | } ieee80211_eapol_frame_type_t;
|
---|
164 |
|
---|
165 | typedef enum {
|
---|
166 | IEEE80211_EAPOL_KEY_KEYINFO_KEYTYPE = 0x0008,
|
---|
167 | IEEE80211_EAPOL_KEY_KEYINFO_INSTALL = 0x0040,
|
---|
168 | IEEE80211_EAPOL_KEY_KEYINFO_ACK = 0x0080,
|
---|
169 | IEEE80211_EAPOL_KEY_KEYINFO_MIC = 0x0100,
|
---|
170 | IEEE80211_EAPOL_KEY_KEYINFO_SECURE = 0x0200,
|
---|
171 | IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA = 0x1000
|
---|
172 | } ieee80211_eapol_key_keyinfo_t;
|
---|
173 |
|
---|
174 | /** IEEE 802.11 information element types. */
|
---|
175 | typedef enum {
|
---|
176 | IEEE80211_SSID_IE = 0, /**< Target SSID. */
|
---|
177 | IEEE80211_RATES_IE = 1, /**< Supported data rates. */
|
---|
178 | IEEE80211_CHANNEL_IE = 3, /**< Current channel number. */
|
---|
179 | IEEE80211_CHALLENGE_IE = 16, /**< Challenge text. */
|
---|
180 | IEEE80211_RSN_IE = 48, /**< RSN. */
|
---|
181 | IEEE80211_EXT_RATES_IE = 50, /**< Extended data rates. */
|
---|
182 | IEEE80211_VENDOR_IE = 221 /**< Vendor specific IE. */
|
---|
183 | } ieee80211_ie_type_t;
|
---|
184 |
|
---|
185 | /** IEEE 802.11 authentication phases. */
|
---|
186 | typedef enum {
|
---|
187 | IEEE80211_AUTH_DISCONNECTED,
|
---|
188 | IEEE80211_AUTH_AUTHENTICATED,
|
---|
189 | IEEE80211_AUTH_ASSOCIATED
|
---|
190 | } ieee80211_auth_phase_t;
|
---|
191 |
|
---|
192 | /** Link with scan result info. */
|
---|
193 | typedef struct {
|
---|
194 | link_t link;
|
---|
195 | time_t last_beacon;
|
---|
196 | ieee80211_scan_result_t scan_result;
|
---|
197 | uint8_t rsn_copy[256];
|
---|
198 | size_t rsn_copy_len;
|
---|
199 | } ieee80211_scan_result_link_t;
|
---|
200 |
|
---|
201 | /** List of scan results info. */
|
---|
202 | typedef struct {
|
---|
203 | list_t list;
|
---|
204 | time_t last_scan;
|
---|
205 | fibril_mutex_t scan_mutex;
|
---|
206 | size_t size;
|
---|
207 | } ieee80211_scan_result_list_t;
|
---|
208 |
|
---|
209 | /** BSSID info. */
|
---|
210 | typedef struct {
|
---|
211 | uint16_t aid;
|
---|
212 | char password[IEEE80211_WPA_MAX_PASSWORD_LENGTH];
|
---|
213 | uint8_t ptk[MAX_PTK_LENGTH];
|
---|
214 | uint8_t gtk[MAX_GTK_LENGTH];
|
---|
215 | ieee80211_scan_result_link_t *res_link;
|
---|
216 | } ieee80211_bssid_info_t;
|
---|
217 |
|
---|
218 | /** IEEE 802.11 WiFi device structure. */
|
---|
219 | struct ieee80211_dev {
|
---|
220 | /** Backing DDF device. */
|
---|
221 | ddf_dev_t *ddf_dev;
|
---|
222 |
|
---|
223 | /** Pointer to implemented IEEE 802.11 device operations. */
|
---|
224 | ieee80211_ops_t *ops;
|
---|
225 |
|
---|
226 | /** Pointer to implemented IEEE 802.11 interface operations. */
|
---|
227 | ieee80211_iface_t *iface;
|
---|
228 |
|
---|
229 | /** Pointer to driver specific data. */
|
---|
230 | void *specific;
|
---|
231 |
|
---|
232 | /** Current operating frequency. */
|
---|
233 | uint16_t current_freq;
|
---|
234 |
|
---|
235 | /** Current operating mode. */
|
---|
236 | ieee80211_operating_mode_t current_op_mode;
|
---|
237 |
|
---|
238 | /** Info about BSSID we are connected to. */
|
---|
239 | ieee80211_bssid_info_t bssid_info;
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Flag indicating that data traffic is encrypted by HW key
|
---|
243 | * that is set up in device.
|
---|
244 | */
|
---|
245 | bool using_hw_key;
|
---|
246 |
|
---|
247 | /** BSSIDs we listen to. */
|
---|
248 | nic_address_t bssid_mask;
|
---|
249 |
|
---|
250 | /** List of APs in neighborhood. */
|
---|
251 | ieee80211_scan_result_list_t ap_list;
|
---|
252 |
|
---|
253 | /** Current sequence number used in data frames. */
|
---|
254 | uint16_t sequence_number;
|
---|
255 |
|
---|
256 | /** Current authentication phase. */
|
---|
257 | ieee80211_auth_phase_t current_auth_phase;
|
---|
258 |
|
---|
259 | /** General purpose guard. */
|
---|
260 | fibril_mutex_t gen_mutex;
|
---|
261 |
|
---|
262 | /** General purpose condition variable. */
|
---|
263 | fibril_condvar_t gen_cond;
|
---|
264 |
|
---|
265 | /** Indicates whether device is fully initialized. */
|
---|
266 | bool ready;
|
---|
267 |
|
---|
268 | /** Indicates whether driver has already started. */
|
---|
269 | bool started;
|
---|
270 | };
|
---|
271 |
|
---|
272 | /** IEEE 802.3 (ethernet) header. */
|
---|
273 | typedef struct {
|
---|
274 | uint8_t dest_addr[ETH_ADDR];
|
---|
275 | uint8_t src_addr[ETH_ADDR];
|
---|
276 | uint16_t proto; /**< Big Endian value! */
|
---|
277 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
278 | eth_header_t;
|
---|
279 |
|
---|
280 | /** IEEE 802.11 management header structure. */
|
---|
281 | typedef struct {
|
---|
282 | uint16_t frame_ctrl; /**< Little Endian value! */
|
---|
283 | uint16_t duration_id; /**< Little Endian value! */
|
---|
284 | uint8_t dest_addr[ETH_ADDR];
|
---|
285 | uint8_t src_addr[ETH_ADDR];
|
---|
286 | uint8_t bssid[ETH_ADDR];
|
---|
287 | uint16_t seq_ctrl; /**< Little Endian value! */
|
---|
288 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
289 | ieee80211_mgmt_header_t;
|
---|
290 |
|
---|
291 | /** IEEE 802.11 data header structure. */
|
---|
292 | typedef struct {
|
---|
293 | uint16_t frame_ctrl; /**< Little Endian value! */
|
---|
294 | uint16_t duration_id; /**< Little Endian value! */
|
---|
295 | uint8_t address1[ETH_ADDR];
|
---|
296 | uint8_t address2[ETH_ADDR];
|
---|
297 | uint8_t address3[ETH_ADDR];
|
---|
298 | uint16_t seq_ctrl; /**< Little Endian value! */
|
---|
299 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
300 | ieee80211_data_header_t;
|
---|
301 |
|
---|
302 | /** IEEE 802.11 information element header. */
|
---|
303 | typedef struct {
|
---|
304 | uint8_t element_id;
|
---|
305 | uint8_t length;
|
---|
306 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
307 | ieee80211_ie_header_t;
|
---|
308 |
|
---|
309 | /** IEEE 802.11 authentication frame body. */
|
---|
310 | typedef struct {
|
---|
311 | uint16_t auth_alg; /**< Little Endian value! */
|
---|
312 | uint16_t auth_trans_no; /**< Little Endian value! */
|
---|
313 | uint16_t status; /**< Little Endian value! */
|
---|
314 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
315 | ieee80211_auth_body_t;
|
---|
316 |
|
---|
317 | /** IEEE 802.11 deauthentication frame body. */
|
---|
318 | typedef struct {
|
---|
319 | uint16_t reason; /**< Little Endian value! */
|
---|
320 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
321 | ieee80211_deauth_body_t;
|
---|
322 |
|
---|
323 | /** IEEE 802.11 association request frame body. */
|
---|
324 | typedef struct {
|
---|
325 | uint16_t capability; /**< Little Endian value! */
|
---|
326 | uint16_t listen_interval; /**< Little Endian value! */
|
---|
327 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
328 | ieee80211_assoc_req_body_t;
|
---|
329 |
|
---|
330 | /** IEEE 802.11 association response frame body. */
|
---|
331 | typedef struct {
|
---|
332 | uint16_t capability; /**< Little Endian value! */
|
---|
333 | uint16_t status; /**< Little Endian value! */
|
---|
334 | uint16_t aid; /**< Little Endian value! */
|
---|
335 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
336 | ieee80211_assoc_resp_body_t;
|
---|
337 |
|
---|
338 | /** IEEE 802.11 beacon frame body start. */
|
---|
339 | typedef struct {
|
---|
340 | uint8_t timestamp[8];
|
---|
341 | uint16_t beacon_interval; /**< Little Endian value! */
|
---|
342 | uint16_t capability; /**< Little Endian value! */
|
---|
343 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
344 | ieee80211_beacon_start_t;
|
---|
345 |
|
---|
346 | /** IEEE 802.11i EAPOL-Key frame format. */
|
---|
347 | typedef struct {
|
---|
348 | uint8_t proto_version;
|
---|
349 | uint8_t packet_type;
|
---|
350 | uint16_t body_length; /**< Big Endian value! */
|
---|
351 | uint8_t descriptor_type;
|
---|
352 | uint16_t key_info; /**< Big Endian value! */
|
---|
353 | uint16_t key_length; /**< Big Endian value! */
|
---|
354 | uint8_t key_replay_counter[8];
|
---|
355 | uint8_t key_nonce[32];
|
---|
356 | uint8_t eapol_key_iv[16];
|
---|
357 | uint8_t key_rsc[8];
|
---|
358 | uint8_t reserved[8];
|
---|
359 | uint8_t key_mic[16];
|
---|
360 | uint16_t key_data_length; /**< Big Endian value! */
|
---|
361 | } __attribute__((packed)) ieee80211_eapol_key_frame_t;
|
---|
362 |
|
---|
363 | #define ieee80211_scan_result_list_foreach(results, iter) \
|
---|
364 | list_foreach((results).list, link, ieee80211_scan_result_link_t, (iter))
|
---|
365 |
|
---|
366 | static inline void ieee80211_scan_result_list_init(
|
---|
367 | ieee80211_scan_result_list_t *results)
|
---|
368 | {
|
---|
369 | list_initialize(&results->list);
|
---|
370 | fibril_mutex_initialize(&results->scan_mutex);
|
---|
371 | }
|
---|
372 |
|
---|
373 | static inline void ieee80211_scan_result_list_remove(
|
---|
374 | ieee80211_scan_result_list_t *results,
|
---|
375 | ieee80211_scan_result_link_t *result)
|
---|
376 | {
|
---|
377 | list_remove(&result->link);
|
---|
378 | results->size--;
|
---|
379 | }
|
---|
380 |
|
---|
381 | static inline void ieee80211_scan_result_list_append(
|
---|
382 | ieee80211_scan_result_list_t *results,
|
---|
383 | ieee80211_scan_result_link_t *result)
|
---|
384 | {
|
---|
385 | list_append(&result->link, &results->list);
|
---|
386 | results->size++;
|
---|
387 | }
|
---|
388 |
|
---|
389 | extern int ieee80211_probe_request(ieee80211_dev_t *ieee80211_dev,
|
---|
390 | char *ssid);
|
---|
391 | extern int ieee80211_authenticate(ieee80211_dev_t *ieee80211_dev);
|
---|
392 | extern int ieee80211_associate(ieee80211_dev_t *ieee80211_dev,
|
---|
393 | char *password);
|
---|
394 | extern int ieee80211_deauthenticate(ieee80211_dev_t *ieee80211_dev);
|
---|
395 |
|
---|
396 | #endif /* LIBN_IEEE80211_H */
|
---|
397 |
|
---|
398 | /** @}
|
---|
399 | */
|
---|