2 * Copyright (c) 2015 Jan Kolarik
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 * @addtogroup libieee80211
36 * Internal IEEE 802.11 header that should not be included.
39 #ifndef LIB_IEEE80211_PRIVATE_H
40 #define LIB_IEEE80211_PRIVATE_H
42 #include <fibril_synch.h>
43 #include <byteorder.h>
44 #include <ddf/driver.h>
47 #include <ieee80211/ieee80211.h>
48 #include "ieee80211.h"
50 /** Timeout in us for waiting to authentication/association response. */
51 #define AUTH_TIMEOUT 200000
53 /** Timeout in us for waiting to finish 4-way handshake process. */
54 #define HANDSHAKE_TIMEOUT 5000000
56 /** Scanning period. */
57 #define SCAN_PERIOD_USEC 35000000
59 /** Time to wait for beacons on channel. */
60 #define SCAN_CHANNEL_WAIT_USEC 200000
62 /** Max time to keep scan result. */
63 #define MAX_KEEP_SCAN_SPAN_SEC 120
65 /** Security bit in capability info field. */
66 #define CAP_SECURITY 0x10
68 /** Protocol type used in EAPOL frames. */
69 #define ETH_TYPE_PAE 0x888e
71 /** WPA OUI used in vendor specific IE. */
72 #define WPA_OUI 0x0050f201
74 /** GTK OUI used in vendor specific IE. */
75 #define GTK_OUI 0x000fac01
77 /** Max PTK key length. */
78 #define MAX_PTK_LENGTH 64
80 /** Max GTK key length. */
81 #define MAX_GTK_LENGTH 64
83 /** KEK offset inside PTK. */
86 /** TK offset inside PTK. */
89 /** Length of Michael MIC code used in TKIP security suite. */
92 /** Length of data to be encrypted by PRF function.
94 * NONCE + SNONCE (2 * 32) + DEST_MAC + SOURCE_MAC (2 * ETH_ADDR)
97 #define PRF_CRYPT_DATA_LENGTH (2 * 32 + 2 * ETH_ADDR)
99 /** Special room in header reserved for encryption. */
100 #define IEEE80211_TKIP_HEADER_LENGTH 8
101 #define IEEE80211_CCMP_HEADER_LENGTH 8
102 /* Maximum of the above. */
103 #define IEEE80211_MAX_HEADER_LENGTH 8
105 /** IEEE 802.11 PTK key length. */
106 #define IEEE80211_PTK_CCMP_LENGTH 48
107 #define IEEE80211_PTK_TKIP_LENGTH 64
109 /** IEEE 802.11 GTK key length. */
110 #define IEEE80211_GTK_CCMP_LENGTH 16
111 #define IEEE80211_GTK_TKIP_LENGTH 32
113 /** IEEE 802.11 frame types. */
115 IEEE80211_MGMT_FRAME
= 0x0,
116 IEEE80211_CTRL_FRAME
= 0x4,
117 IEEE80211_DATA_FRAME
= 0x8,
118 IEEE80211_EXT_FRAME
= 0xC
119 } ieee80211_frame_type_t
;
121 /** IEEE 802.11 management frame subtypes. */
123 IEEE80211_MGMT_ASSOC_REQ_FRAME
= 0x00,
124 IEEE80211_MGMT_ASSOC_RESP_FRAME
= 0x10,
125 IEEE80211_MGMT_REASSOC_REQ_FRAME
= 0x20,
126 IEEE80211_MGMT_REASSOC_RESP_FRAME
= 0x30,
127 IEEE80211_MGMT_PROBE_REQ_FRAME
= 0x40,
128 IEEE80211_MGMT_PROBE_RESP_FRAME
= 0x50,
129 IEEE80211_MGMT_BEACON_FRAME
= 0x80,
130 IEEE80211_MGMT_DISASSOC_FRAME
= 0xA0,
131 IEEE80211_MGMT_AUTH_FRAME
= 0xB0,
132 IEEE80211_MGMT_DEAUTH_FRAME
= 0xC0,
133 } ieee80211_frame_mgmt_subtype_t
;
135 /** IEEE 802.11 data frame subtypes. */
137 IEEE80211_DATA_DATA_FRAME
= 0x0000,
138 IEEE80211_DATA_QOS_FRAME
= 0x0080
139 } ieee80211_frame_data_subtype_t
;
141 /** IEEE 802.11 frame control value masks. */
143 IEEE80211_FRAME_CTRL_FRAME_TYPE
= 0x000C,
144 IEEE80211_FRAME_CTRL_FRAME_SUBTYPE
= 0x00F0,
145 IEEE80211_FRAME_CTRL_PROTECTED
= 0x4000
146 } ieee80211_frame_ctrl_mask_t
;
148 /** IEEE 802.11 frame control DS field values. */
150 IEEE80211_FRAME_CTRL_TODS
= 0x0100,
151 IEEE80211_FRAME_CTRL_FROMDS
= 0x0200
152 } ieee80211_frame_ctrl_ds_t
;
154 /** IEEE 802.11 authentication cipher suites values. */
156 IEEE80211_AUTH_CIPHER_TKIP
= 0x02,
157 IEEE80211_AUTH_CIPHER_CCMP
= 0x04
158 } ieee80211_auth_cipher_type_t
;
160 /** IEEE 802.11 AKM suites values. */
162 IEEE80211_AUTH_AKM_8021X
= 0x01,
163 IEEE80211_AUTH_AKM_PSK
= 0x02
164 } ieee80211_auth_akm_type_t
;
167 IEEE80211_EAPOL_START
= 0x1,
168 IEEE80211_EAPOL_KEY
= 0x3
169 } ieee80211_eapol_frame_type_t
;
172 IEEE80211_EAPOL_KEY_KEYINFO_KEYTYPE
= 0x0008,
173 IEEE80211_EAPOL_KEY_KEYINFO_KEYID
= 0x0010,
174 IEEE80211_EAPOL_KEY_KEYINFO_INSTALL
= 0x0040,
175 IEEE80211_EAPOL_KEY_KEYINFO_ACK
= 0x0080,
176 IEEE80211_EAPOL_KEY_KEYINFO_MIC
= 0x0100,
177 IEEE80211_EAPOL_KEY_KEYINFO_SECURE
= 0x0200,
178 IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA
= 0x1000
179 } ieee80211_eapol_key_keyinfo_t
;
181 /** IEEE 802.11 information element types. */
183 IEEE80211_SSID_IE
= 0, /**< Target SSID. */
184 IEEE80211_RATES_IE
= 1, /**< Supported data rates. */
185 IEEE80211_CHANNEL_IE
= 3, /**< Current channel number. */
186 IEEE80211_CHALLENGE_IE
= 16, /**< Challenge text. */
187 IEEE80211_RSN_IE
= 48, /**< RSN. */
188 IEEE80211_EXT_RATES_IE
= 50, /**< Extended data rates. */
189 IEEE80211_VENDOR_IE
= 221 /**< Vendor specific IE. */
190 } ieee80211_ie_type_t
;
192 /** IEEE 802.11 authentication phases. */
194 IEEE80211_AUTH_DISCONNECTED
,
195 IEEE80211_AUTH_AUTHENTICATED
,
196 IEEE80211_AUTH_ASSOCIATED
,
197 IEEE80211_AUTH_CONNECTED
198 } ieee80211_auth_phase_t
;
200 /** Link with scan result info. */
204 ieee80211_scan_result_t scan_result
;
205 uint8_t auth_ie
[256];
207 } ieee80211_scan_result_link_t
;
209 /** List of scan results info. */
212 fibril_mutex_t results_mutex
;
214 } ieee80211_scan_result_list_t
;
219 char password
[IEEE80211_MAX_PASSW_LEN
];
220 uint8_t ptk
[MAX_PTK_LENGTH
];
221 uint8_t gtk
[MAX_GTK_LENGTH
];
222 ieee80211_scan_result_link_t
*res_link
;
223 } ieee80211_bssid_info_t
;
225 /** IEEE 802.11 WiFi device structure. */
226 struct ieee80211_dev
{
227 /** Backing DDF device. */
230 /** Pointer to implemented IEEE 802.11 device operations. */
231 ieee80211_ops_t
*ops
;
233 /** Pointer to implemented IEEE 802.11 interface operations. */
234 ieee80211_iface_t
*iface
;
236 /** Pointer to driver specific data. */
239 /** Current operating frequency. */
240 uint16_t current_freq
;
242 /** Current operating mode. */
243 ieee80211_operating_mode_t current_op_mode
;
245 /** Info about BSSID we are connected to. */
246 ieee80211_bssid_info_t bssid_info
;
249 * Flag indicating that data traffic is encrypted by HW key
250 * that is set up in device.
254 /** BSSIDs we listen to. */
255 nic_address_t bssid_mask
;
257 /** List of APs in neighborhood. */
258 ieee80211_scan_result_list_t ap_list
;
260 /** Current sequence number used in data frames. */
261 uint16_t sequence_number
;
263 /** Current authentication phase. */
264 ieee80211_auth_phase_t current_auth_phase
;
266 /** Flag indicating whether client wants connect to network. */
267 bool pending_conn_req
;
269 /** Scanning guard. */
270 fibril_mutex_t scan_mutex
;
272 /** General purpose guard. */
273 fibril_mutex_t gen_mutex
;
275 /** General purpose condition variable. */
276 fibril_condvar_t gen_cond
;
278 /** Indicates whether device is fully initialized. */
281 /** Indicates whether driver has already started. */
285 /** IEEE 802.3 (ethernet) header. */
287 uint8_t dest_addr
[ETH_ADDR
];
288 uint8_t src_addr
[ETH_ADDR
];
289 uint16_t proto
; /**< Big Endian value! */
290 } __attribute__((packed
)) __attribute__((aligned(2)))
293 /** IEEE 802.11 management header structure. */
295 uint16_t frame_ctrl
; /**< Little Endian value! */
296 uint16_t duration_id
; /**< Little Endian value! */
297 uint8_t dest_addr
[ETH_ADDR
];
298 uint8_t src_addr
[ETH_ADDR
];
299 uint8_t bssid
[ETH_ADDR
];
300 uint16_t seq_ctrl
; /**< Little Endian value! */
301 } __attribute__((packed
)) __attribute__((aligned(2)))
302 ieee80211_mgmt_header_t
;
304 /** IEEE 802.11 data header structure. */
306 uint16_t frame_ctrl
; /**< Little Endian value! */
307 uint16_t duration_id
; /**< Little Endian value! */
308 uint8_t address1
[ETH_ADDR
];
309 uint8_t address2
[ETH_ADDR
];
310 uint8_t address3
[ETH_ADDR
];
311 uint16_t seq_ctrl
; /**< Little Endian value! */
312 } __attribute__((packed
)) __attribute__((aligned(2)))
313 ieee80211_data_header_t
;
315 /** IEEE 802.11 information element header. */
319 } __attribute__((packed
)) __attribute__((aligned(2)))
320 ieee80211_ie_header_t
;
322 /** IEEE 802.11 authentication frame body. */
324 uint16_t auth_alg
; /**< Little Endian value! */
325 uint16_t auth_trans_no
; /**< Little Endian value! */
326 uint16_t status
; /**< Little Endian value! */
327 } __attribute__((packed
)) __attribute__((aligned(2)))
328 ieee80211_auth_body_t
;
330 /** IEEE 802.11 deauthentication frame body. */
332 uint16_t reason
; /**< Little Endian value! */
333 } __attribute__((packed
)) __attribute__((aligned(2)))
334 ieee80211_deauth_body_t
;
336 /** IEEE 802.11 association request frame body. */
338 uint16_t capability
; /**< Little Endian value! */
339 uint16_t listen_interval
; /**< Little Endian value! */
340 } __attribute__((packed
)) __attribute__((aligned(2)))
341 ieee80211_assoc_req_body_t
;
343 /** IEEE 802.11 association response frame body. */
345 uint16_t capability
; /**< Little Endian value! */
346 uint16_t status
; /**< Little Endian value! */
347 uint16_t aid
; /**< Little Endian value! */
348 } __attribute__((packed
)) __attribute__((aligned(2)))
349 ieee80211_assoc_resp_body_t
;
351 /** IEEE 802.11 beacon frame body start. */
353 uint8_t timestamp
[8];
354 uint16_t beacon_interval
; /**< Little Endian value! */
355 uint16_t capability
; /**< Little Endian value! */
356 } __attribute__((packed
)) __attribute__((aligned(2)))
357 ieee80211_beacon_start_t
;
359 /** IEEE 802.11i EAPOL-Key frame format. */
361 uint8_t proto_version
;
363 uint16_t body_length
; /**< Big Endian value! */
364 uint8_t descriptor_type
;
365 uint16_t key_info
; /**< Big Endian value! */
366 uint16_t key_length
; /**< Big Endian value! */
367 uint8_t key_replay_counter
[8];
368 uint8_t key_nonce
[32];
369 uint8_t eapol_key_iv
[16];
373 uint16_t key_data_length
; /**< Big Endian value! */
374 } __attribute__((packed
)) ieee80211_eapol_key_frame_t
;
376 #define ieee80211_scan_result_list_foreach(results, iter) \
377 list_foreach((results).list, link, ieee80211_scan_result_link_t, (iter))
380 ieee80211_scan_result_list_init(ieee80211_scan_result_list_t
*results
)
382 list_initialize(&results
->list
);
383 fibril_mutex_initialize(&results
->results_mutex
);
387 ieee80211_scan_result_list_remove(ieee80211_scan_result_list_t
*results
,
388 ieee80211_scan_result_link_t
*result
)
390 list_remove(&result
->link
);
395 ieee80211_scan_result_list_append(ieee80211_scan_result_list_t
*results
,
396 ieee80211_scan_result_link_t
*result
)
398 list_append(&result
->link
, &results
->list
);
402 extern bool ieee80211_is_fromds_frame(uint16_t);
403 extern bool ieee80211_is_tods_frame(uint16_t);
404 extern void ieee80211_set_connect_request(ieee80211_dev_t
*);
405 extern bool ieee80211_pending_connect_request(ieee80211_dev_t
*);
406 extern ieee80211_auth_phase_t
ieee80211_get_auth_phase(ieee80211_dev_t
*);
407 extern void ieee80211_set_auth_phase(ieee80211_dev_t
*, ieee80211_auth_phase_t
);
408 extern errno_t
ieee80211_probe_request(ieee80211_dev_t
*, char *);
409 extern errno_t
ieee80211_authenticate(ieee80211_dev_t
*);
410 extern errno_t
ieee80211_associate(ieee80211_dev_t
*, char *);
411 extern errno_t
ieee80211_deauthenticate(ieee80211_dev_t
*);