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 | /* Scanning period. */
|
---|
57 | #define SCAN_PERIOD_USEC 35000000
|
---|
58 |
|
---|
59 | /* Time to wait for beacons on channel. */
|
---|
60 | #define SCAN_CHANNEL_WAIT_USEC 200000
|
---|
61 |
|
---|
62 | /* Max time to keep scan result. */
|
---|
63 | #define MAX_KEEP_SCAN_SPAN_SEC 120
|
---|
64 |
|
---|
65 | /* Security bit in capability info field. */
|
---|
66 | #define CAP_SECURITY 0x10
|
---|
67 |
|
---|
68 | /* Protocol type used in EAPOL frames. */
|
---|
69 | #define ETH_TYPE_PAE 0x888E
|
---|
70 |
|
---|
71 | /* WPA OUI used in vendor specific IE. */
|
---|
72 | #define WPA_OUI 0x0050F201
|
---|
73 |
|
---|
74 | /* GTK OUI used in vendor specific IE. */
|
---|
75 | #define GTK_OUI 0x000FAC01
|
---|
76 |
|
---|
77 | /* Max PTK key length. */
|
---|
78 | #define MAX_PTK_LENGTH 64
|
---|
79 |
|
---|
80 | /* Max GTK key length. */
|
---|
81 | #define MAX_GTK_LENGTH 64
|
---|
82 |
|
---|
83 | /* KEK offset inside PTK. */
|
---|
84 | #define KEK_OFFSET 16
|
---|
85 |
|
---|
86 | /* TK offset inside PTK. */
|
---|
87 | #define TK_OFFSET 32
|
---|
88 |
|
---|
89 | /* Length of CCMP header we need to reserve. */
|
---|
90 | #define IEEE80211_CCMP_HEADER_LENGTH 8
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Length of data to be encrypted by PRF function:
|
---|
94 | * NONCE + SNONCE (2 * 32) + DEST_MAC + SOURCE_MAC (2 * ETH_ADDR)
|
---|
95 | */
|
---|
96 | #define PRF_CRYPT_DATA_LENGTH 2*32 + 2*ETH_ADDR
|
---|
97 |
|
---|
98 | /** IEEE 802.11 PTK key length. */
|
---|
99 | typedef enum {
|
---|
100 | IEEE80211_PTK_CCMP_LENGTH = 48,
|
---|
101 | IEEE80211_PTK_TKIP_LENGTH = 64
|
---|
102 | } ieee80211_ptk_length_t;
|
---|
103 |
|
---|
104 | /** IEEE 802.11 GTK key length. */
|
---|
105 | typedef enum {
|
---|
106 | IEEE80211_GTK_CCMP_LENGTH = 16,
|
---|
107 | IEEE80211_GTK_TKIP_LENGTH = 32
|
---|
108 | } ieee80211_gtk_length_t;
|
---|
109 |
|
---|
110 | /** IEEE 802.11 frame types. */
|
---|
111 | typedef enum {
|
---|
112 | IEEE80211_MGMT_FRAME = 0x0,
|
---|
113 | IEEE80211_CTRL_FRAME = 0x4,
|
---|
114 | IEEE80211_DATA_FRAME = 0x8,
|
---|
115 | IEEE80211_EXT_FRAME = 0xC
|
---|
116 | } ieee80211_frame_type_t;
|
---|
117 |
|
---|
118 | /** IEEE 802.11 management frame subtypes. */
|
---|
119 | typedef enum {
|
---|
120 | IEEE80211_MGMT_ASSOC_REQ_FRAME = 0x00,
|
---|
121 | IEEE80211_MGMT_ASSOC_RESP_FRAME = 0x10,
|
---|
122 | IEEE80211_MGMT_REASSOC_REQ_FRAME = 0x20,
|
---|
123 | IEEE80211_MGMT_REASSOC_RESP_FRAME = 0x30,
|
---|
124 | IEEE80211_MGMT_PROBE_REQ_FRAME = 0x40,
|
---|
125 | IEEE80211_MGMT_PROBE_RESP_FRAME = 0x50,
|
---|
126 | IEEE80211_MGMT_BEACON_FRAME = 0x80,
|
---|
127 | IEEE80211_MGMT_DISASSOC_FRAME = 0xA0,
|
---|
128 | IEEE80211_MGMT_AUTH_FRAME = 0xB0,
|
---|
129 | IEEE80211_MGMT_DEAUTH_FRAME = 0xC0,
|
---|
130 | } ieee80211_frame_mgmt_subtype_t;
|
---|
131 |
|
---|
132 | /** IEEE 802.11 data frame subtypes. */
|
---|
133 | typedef enum {
|
---|
134 | IEEE80211_DATA_DATA_FRAME = 0x0000,
|
---|
135 | IEEE80211_DATA_QOS_FRAME = 0x0080
|
---|
136 | } ieee80211_frame_data_subtype_t;
|
---|
137 |
|
---|
138 | /** IEEE 802.11 frame control value masks. */
|
---|
139 | typedef enum {
|
---|
140 | IEEE80211_FRAME_CTRL_FRAME_TYPE = 0x000C,
|
---|
141 | IEEE80211_FRAME_CTRL_FRAME_SUBTYPE = 0x00F0,
|
---|
142 | IEEE80211_FRAME_CTRL_PROTECTED = 0x4000
|
---|
143 | } ieee80211_frame_ctrl_mask_t;
|
---|
144 |
|
---|
145 | /** IEEE 802.11 frame control DS field values. */
|
---|
146 | typedef enum {
|
---|
147 | IEEE80211_FRAME_CTRL_TODS = 0x0100,
|
---|
148 | IEEE80211_FRAME_CTRL_FROMDS = 0x0200
|
---|
149 | } ieee80211_frame_ctrl_ds_t;
|
---|
150 |
|
---|
151 | /** IEEE 802.11 authentication cipher suites values. */
|
---|
152 | typedef enum {
|
---|
153 | IEEE80211_AUTH_CIPHER_TKIP = 0x02,
|
---|
154 | IEEE80211_AUTH_CIPHER_CCMP = 0x04
|
---|
155 | } ieee80211_auth_cipher_type_t;
|
---|
156 |
|
---|
157 | /** IEEE 802.11 AKM suites values. */
|
---|
158 | typedef enum {
|
---|
159 | IEEE80211_AUTH_AKM_8021X = 0x01,
|
---|
160 | IEEE80211_AUTH_AKM_PSK = 0x02
|
---|
161 | } ieee80211_auth_akm_type_t;
|
---|
162 |
|
---|
163 | typedef enum {
|
---|
164 | IEEE80211_EAPOL_START = 0x1,
|
---|
165 | IEEE80211_EAPOL_KEY = 0x3
|
---|
166 | } ieee80211_eapol_frame_type_t;
|
---|
167 |
|
---|
168 | typedef enum {
|
---|
169 | IEEE80211_EAPOL_KEY_KEYINFO_KEYTYPE = 0x0008,
|
---|
170 | IEEE80211_EAPOL_KEY_KEYINFO_INSTALL = 0x0040,
|
---|
171 | IEEE80211_EAPOL_KEY_KEYINFO_ACK = 0x0080,
|
---|
172 | IEEE80211_EAPOL_KEY_KEYINFO_MIC = 0x0100,
|
---|
173 | IEEE80211_EAPOL_KEY_KEYINFO_SECURE = 0x0200,
|
---|
174 | IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA = 0x1000
|
---|
175 | } ieee80211_eapol_key_keyinfo_t;
|
---|
176 |
|
---|
177 | /** IEEE 802.11 information element types. */
|
---|
178 | typedef enum {
|
---|
179 | IEEE80211_SSID_IE = 0, /**< Target SSID. */
|
---|
180 | IEEE80211_RATES_IE = 1, /**< Supported data rates. */
|
---|
181 | IEEE80211_CHANNEL_IE = 3, /**< Current channel number. */
|
---|
182 | IEEE80211_CHALLENGE_IE = 16, /**< Challenge text. */
|
---|
183 | IEEE80211_RSN_IE = 48, /**< RSN. */
|
---|
184 | IEEE80211_EXT_RATES_IE = 50, /**< Extended data rates. */
|
---|
185 | IEEE80211_VENDOR_IE = 221 /**< Vendor specific IE. */
|
---|
186 | } ieee80211_ie_type_t;
|
---|
187 |
|
---|
188 | /** IEEE 802.11 authentication phases. */
|
---|
189 | typedef enum {
|
---|
190 | IEEE80211_AUTH_DISCONNECTED,
|
---|
191 | IEEE80211_AUTH_AUTHENTICATED,
|
---|
192 | IEEE80211_AUTH_ASSOCIATED,
|
---|
193 | IEEE80211_AUTH_CONNECTED
|
---|
194 | } ieee80211_auth_phase_t;
|
---|
195 |
|
---|
196 | /** Link with scan result info. */
|
---|
197 | typedef struct {
|
---|
198 | link_t link;
|
---|
199 | time_t last_beacon;
|
---|
200 | ieee80211_scan_result_t scan_result;
|
---|
201 | uint8_t rsn_copy[256];
|
---|
202 | size_t rsn_copy_len;
|
---|
203 | } ieee80211_scan_result_link_t;
|
---|
204 |
|
---|
205 | /** List of scan results info. */
|
---|
206 | typedef struct {
|
---|
207 | list_t list;
|
---|
208 | fibril_mutex_t results_mutex;
|
---|
209 | size_t size;
|
---|
210 | } ieee80211_scan_result_list_t;
|
---|
211 |
|
---|
212 | /** BSSID info. */
|
---|
213 | typedef struct {
|
---|
214 | uint16_t aid;
|
---|
215 | char password[IEEE80211_MAX_PASSW_LEN];
|
---|
216 | uint8_t ptk[MAX_PTK_LENGTH];
|
---|
217 | uint8_t gtk[MAX_GTK_LENGTH];
|
---|
218 | ieee80211_scan_result_link_t *res_link;
|
---|
219 | } ieee80211_bssid_info_t;
|
---|
220 |
|
---|
221 | /** IEEE 802.11 WiFi device structure. */
|
---|
222 | struct ieee80211_dev {
|
---|
223 | /** Backing DDF device. */
|
---|
224 | ddf_dev_t *ddf_dev;
|
---|
225 |
|
---|
226 | /** Pointer to implemented IEEE 802.11 device operations. */
|
---|
227 | ieee80211_ops_t *ops;
|
---|
228 |
|
---|
229 | /** Pointer to implemented IEEE 802.11 interface operations. */
|
---|
230 | ieee80211_iface_t *iface;
|
---|
231 |
|
---|
232 | /** Pointer to driver specific data. */
|
---|
233 | void *specific;
|
---|
234 |
|
---|
235 | /** Current operating frequency. */
|
---|
236 | uint16_t current_freq;
|
---|
237 |
|
---|
238 | /** Current operating mode. */
|
---|
239 | ieee80211_operating_mode_t current_op_mode;
|
---|
240 |
|
---|
241 | /** Info about BSSID we are connected to. */
|
---|
242 | ieee80211_bssid_info_t bssid_info;
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Flag indicating that data traffic is encrypted by HW key
|
---|
246 | * that is set up in device.
|
---|
247 | */
|
---|
248 | bool using_hw_key;
|
---|
249 |
|
---|
250 | /** BSSIDs we listen to. */
|
---|
251 | nic_address_t bssid_mask;
|
---|
252 |
|
---|
253 | /** List of APs in neighborhood. */
|
---|
254 | ieee80211_scan_result_list_t ap_list;
|
---|
255 |
|
---|
256 | /** Current sequence number used in data frames. */
|
---|
257 | uint16_t sequence_number;
|
---|
258 |
|
---|
259 | /** Current authentication phase. */
|
---|
260 | ieee80211_auth_phase_t current_auth_phase;
|
---|
261 |
|
---|
262 | /** Flag indicating whether client wants connect to network. */
|
---|
263 | bool pending_conn_req;
|
---|
264 |
|
---|
265 | /** Scanning guard. */
|
---|
266 | fibril_mutex_t scan_mutex;
|
---|
267 |
|
---|
268 | /** General purpose guard. */
|
---|
269 | fibril_mutex_t gen_mutex;
|
---|
270 |
|
---|
271 | /** General purpose condition variable. */
|
---|
272 | fibril_condvar_t gen_cond;
|
---|
273 |
|
---|
274 | /** Indicates whether device is fully initialized. */
|
---|
275 | bool ready;
|
---|
276 |
|
---|
277 | /** Indicates whether driver has already started. */
|
---|
278 | bool started;
|
---|
279 | };
|
---|
280 |
|
---|
281 | /** IEEE 802.3 (ethernet) header. */
|
---|
282 | typedef struct {
|
---|
283 | uint8_t dest_addr[ETH_ADDR];
|
---|
284 | uint8_t src_addr[ETH_ADDR];
|
---|
285 | uint16_t proto; /**< Big Endian value! */
|
---|
286 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
287 | eth_header_t;
|
---|
288 |
|
---|
289 | /** IEEE 802.11 management header structure. */
|
---|
290 | typedef struct {
|
---|
291 | uint16_t frame_ctrl; /**< Little Endian value! */
|
---|
292 | uint16_t duration_id; /**< Little Endian value! */
|
---|
293 | uint8_t dest_addr[ETH_ADDR];
|
---|
294 | uint8_t src_addr[ETH_ADDR];
|
---|
295 | uint8_t bssid[ETH_ADDR];
|
---|
296 | uint16_t seq_ctrl; /**< Little Endian value! */
|
---|
297 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
298 | ieee80211_mgmt_header_t;
|
---|
299 |
|
---|
300 | /** IEEE 802.11 data header structure. */
|
---|
301 | typedef struct {
|
---|
302 | uint16_t frame_ctrl; /**< Little Endian value! */
|
---|
303 | uint16_t duration_id; /**< Little Endian value! */
|
---|
304 | uint8_t address1[ETH_ADDR];
|
---|
305 | uint8_t address2[ETH_ADDR];
|
---|
306 | uint8_t address3[ETH_ADDR];
|
---|
307 | uint16_t seq_ctrl; /**< Little Endian value! */
|
---|
308 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
309 | ieee80211_data_header_t;
|
---|
310 |
|
---|
311 | /** IEEE 802.11 information element header. */
|
---|
312 | typedef struct {
|
---|
313 | uint8_t element_id;
|
---|
314 | uint8_t length;
|
---|
315 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
316 | ieee80211_ie_header_t;
|
---|
317 |
|
---|
318 | /** IEEE 802.11 authentication frame body. */
|
---|
319 | typedef struct {
|
---|
320 | uint16_t auth_alg; /**< Little Endian value! */
|
---|
321 | uint16_t auth_trans_no; /**< Little Endian value! */
|
---|
322 | uint16_t status; /**< Little Endian value! */
|
---|
323 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
324 | ieee80211_auth_body_t;
|
---|
325 |
|
---|
326 | /** IEEE 802.11 deauthentication frame body. */
|
---|
327 | typedef struct {
|
---|
328 | uint16_t reason; /**< Little Endian value! */
|
---|
329 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
330 | ieee80211_deauth_body_t;
|
---|
331 |
|
---|
332 | /** IEEE 802.11 association request frame body. */
|
---|
333 | typedef struct {
|
---|
334 | uint16_t capability; /**< Little Endian value! */
|
---|
335 | uint16_t listen_interval; /**< Little Endian value! */
|
---|
336 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
337 | ieee80211_assoc_req_body_t;
|
---|
338 |
|
---|
339 | /** IEEE 802.11 association response frame body. */
|
---|
340 | typedef struct {
|
---|
341 | uint16_t capability; /**< Little Endian value! */
|
---|
342 | uint16_t status; /**< Little Endian value! */
|
---|
343 | uint16_t aid; /**< Little Endian value! */
|
---|
344 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
345 | ieee80211_assoc_resp_body_t;
|
---|
346 |
|
---|
347 | /** IEEE 802.11 beacon frame body start. */
|
---|
348 | typedef struct {
|
---|
349 | uint8_t timestamp[8];
|
---|
350 | uint16_t beacon_interval; /**< Little Endian value! */
|
---|
351 | uint16_t capability; /**< Little Endian value! */
|
---|
352 | } __attribute__((packed)) __attribute__ ((aligned(2)))
|
---|
353 | ieee80211_beacon_start_t;
|
---|
354 |
|
---|
355 | /** IEEE 802.11i EAPOL-Key frame format. */
|
---|
356 | typedef struct {
|
---|
357 | uint8_t proto_version;
|
---|
358 | uint8_t packet_type;
|
---|
359 | uint16_t body_length; /**< Big Endian value! */
|
---|
360 | uint8_t descriptor_type;
|
---|
361 | uint16_t key_info; /**< Big Endian value! */
|
---|
362 | uint16_t key_length; /**< Big Endian value! */
|
---|
363 | uint8_t key_replay_counter[8];
|
---|
364 | uint8_t key_nonce[32];
|
---|
365 | uint8_t eapol_key_iv[16];
|
---|
366 | uint8_t key_rsc[8];
|
---|
367 | uint8_t reserved[8];
|
---|
368 | uint8_t key_mic[16];
|
---|
369 | uint16_t key_data_length; /**< Big Endian value! */
|
---|
370 | } __attribute__((packed)) ieee80211_eapol_key_frame_t;
|
---|
371 |
|
---|
372 | #define ieee80211_scan_result_list_foreach(results, iter) \
|
---|
373 | list_foreach((results).list, link, ieee80211_scan_result_link_t, (iter))
|
---|
374 |
|
---|
375 | static inline void ieee80211_scan_result_list_init(
|
---|
376 | ieee80211_scan_result_list_t *results)
|
---|
377 | {
|
---|
378 | list_initialize(&results->list);
|
---|
379 | fibril_mutex_initialize(&results->results_mutex);
|
---|
380 | }
|
---|
381 |
|
---|
382 | static inline void ieee80211_scan_result_list_remove(
|
---|
383 | ieee80211_scan_result_list_t *results,
|
---|
384 | ieee80211_scan_result_link_t *result)
|
---|
385 | {
|
---|
386 | list_remove(&result->link);
|
---|
387 | results->size--;
|
---|
388 | }
|
---|
389 |
|
---|
390 | static inline void ieee80211_scan_result_list_append(
|
---|
391 | ieee80211_scan_result_list_t *results,
|
---|
392 | ieee80211_scan_result_link_t *result)
|
---|
393 | {
|
---|
394 | list_append(&result->link, &results->list);
|
---|
395 | results->size++;
|
---|
396 | }
|
---|
397 |
|
---|
398 | extern void ieee80211_set_connect_request(ieee80211_dev_t *ieee80211_dev);
|
---|
399 | extern bool ieee80211_pending_connect_request(ieee80211_dev_t *ieee80211_dev);
|
---|
400 | extern ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t
|
---|
401 | *ieee80211_dev);
|
---|
402 | extern void ieee80211_set_auth_phase(ieee80211_dev_t *ieee80211_dev,
|
---|
403 | ieee80211_auth_phase_t auth_phase);
|
---|
404 | extern int ieee80211_probe_request(ieee80211_dev_t *ieee80211_dev,
|
---|
405 | char *ssid);
|
---|
406 | extern int ieee80211_authenticate(ieee80211_dev_t *ieee80211_dev);
|
---|
407 | extern int ieee80211_associate(ieee80211_dev_t *ieee80211_dev,
|
---|
408 | char *password);
|
---|
409 | extern int ieee80211_deauthenticate(ieee80211_dev_t *ieee80211_dev);
|
---|
410 |
|
---|
411 | #endif /* LIBN_IEEE80211_H */
|
---|
412 |
|
---|
413 | /** @}
|
---|
414 | */
|
---|