source: mainline/uspace/lib/ieee80211/src/ieee80211.c@ e71c023

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e71c023 was e71c023, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Fix stack corruption in ieee80211 (courtesy of hosted mode diagnostics).

  • Property mode set to 100644
File size: 47.8 KB
RevLine 
[59fa7ab]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/** @addtogroup libieee80211
30 * @{
31 */
32
33/** @file ieee80211.c
[8a64320e]34 *
[59fa7ab]35 * IEEE 802.11 interface implementation.
36 */
37
[1dcc0b9]38#include <stdio.h>
39#include <crypto.h>
40#include <str.h>
[59fa7ab]41#include <macros.h>
42#include <errno.h>
43#include <ieee80211.h>
44#include <ieee80211_impl.h>
45#include <ieee80211_iface_impl.h>
46#include <ieee80211_private.h>
47#include <ops/ieee80211.h>
48
[8a64320e]49#define IEEE80211_DATA_RATES_SIZE 8
50#define IEEE80211_EXT_DATA_RATES_SIZE 4
[1dcc0b9]51
[053fc2b]52#define ATOMIC_GET(state)
53
[1dcc0b9]54/** Frame encapsulation used in IEEE 802.11. */
[8a64320e]55static const uint8_t rfc1042_header[] = {
[1b20da0]56 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
[1dcc0b9]57};
58
[d7dadcb4]59/** Broadcast MAC address. */
[59fa7ab]60static const uint8_t ieee80211_broadcast_mac_addr[] = {
[8a64320e]61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
[59fa7ab]62};
63
[8a64320e]64/** Check data frame.
65 *
[1dcc0b9]66 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]67 *
[1dcc0b9]68 * @return True if it is data frame, otherwise false.
[8a64320e]69 *
[59fa7ab]70 */
71inline bool ieee80211_is_data_frame(uint16_t frame_ctrl)
72{
73 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]74
[8a64320e]75 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
76 IEEE80211_DATA_FRAME;
[59fa7ab]77}
78
[8a64320e]79/** Check management frame.
80 *
[1dcc0b9]81 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]82 *
[1dcc0b9]83 * @return True if it is management frame, otherwise false.
[8a64320e]84 *
[1dcc0b9]85 */
[59fa7ab]86inline bool ieee80211_is_mgmt_frame(uint16_t frame_ctrl)
87{
88 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]89
[8a64320e]90 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
91 IEEE80211_MGMT_FRAME;
[59fa7ab]92}
93
[8a64320e]94/** Check management beacon frame.
95 *
[1dcc0b9]96 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]97 *
[1dcc0b9]98 * @return True if it is beacon frame, otherwise false.
[8a64320e]99 *
[1dcc0b9]100 */
[59fa7ab]101inline bool ieee80211_is_beacon_frame(uint16_t frame_ctrl)
102{
103 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]104
[8a64320e]105 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
106 IEEE80211_MGMT_BEACON_FRAME;
[59fa7ab]107}
108
[8a64320e]109/** Check management probe response frame.
110 *
[1dcc0b9]111 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]112 *
[1dcc0b9]113 * @return True if it is probe resp frame, otherwise false.
[8a64320e]114 *
[1dcc0b9]115 */
[59fa7ab]116inline bool ieee80211_is_probe_response_frame(uint16_t frame_ctrl)
117{
118 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]119
[8a64320e]120 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
121 IEEE80211_MGMT_PROBE_RESP_FRAME;
[59fa7ab]122}
123
[8a64320e]124/** Check management authentication frame.
125 *
[1dcc0b9]126 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]127 *
[1dcc0b9]128 * @return True if it is auth frame, otherwise false.
[8a64320e]129 *
[1dcc0b9]130 */
131inline bool ieee80211_is_auth_frame(uint16_t frame_ctrl)
132{
133 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]134
[8a64320e]135 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
136 IEEE80211_MGMT_AUTH_FRAME;
[1dcc0b9]137}
138
[8a64320e]139/** Check management association response frame.
140 *
[1dcc0b9]141 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]142 *
[1dcc0b9]143 * @return True if it is assoc resp frame, otherwise false.
[8a64320e]144 *
[1dcc0b9]145 */
146inline bool ieee80211_is_assoc_response_frame(uint16_t frame_ctrl)
147{
148 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]149
[8a64320e]150 return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
151 IEEE80211_MGMT_ASSOC_RESP_FRAME;
[1dcc0b9]152}
153
[8a64320e]154/** Check data frame "to distribution system" direction.
155 *
[1dcc0b9]156 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]157 *
[1dcc0b9]158 * @return True if it is TODS frame, otherwise false.
[8a64320e]159 *
[1dcc0b9]160 */
[cc575ef9]161inline bool ieee80211_is_tods_frame(uint16_t frame_ctrl)
[1dcc0b9]162{
163 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]164
[1dcc0b9]165 return (frame_ctrl & IEEE80211_FRAME_CTRL_TODS);
166}
167
[8a64320e]168/** Check data frame "from distribution system" direction.
169 *
[1dcc0b9]170 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]171 *
[1dcc0b9]172 * @return True if it is FROMDS frame, otherwise false.
[8a64320e]173 *
[1dcc0b9]174 */
[cc575ef9]175inline bool ieee80211_is_fromds_frame(uint16_t frame_ctrl)
[1dcc0b9]176{
177 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]178
[1dcc0b9]179 return (frame_ctrl & IEEE80211_FRAME_CTRL_FROMDS);
180}
181
[8a64320e]182/** Check if it is data frame containing payload data.
183 *
[1dcc0b9]184 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]185 *
[1dcc0b9]186 * @return True if it has payload data, otherwise false.
[8a64320e]187 *
[1dcc0b9]188 */
189static inline bool ieee80211_has_data_frame(uint16_t frame_ctrl)
190{
191 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]192
[8a64320e]193 return (frame_ctrl & (IEEE80211_FRAME_CTRL_FRAME_TYPE | 0x40)) ==
194 IEEE80211_DATA_FRAME;
[1dcc0b9]195}
196
[8a64320e]197/** Check if it is encrypted frame.
198 *
[1dcc0b9]199 * @param frame_ctrl Frame control field in little endian (!).
[8a64320e]200 *
[1dcc0b9]201 * @return True if the frame is encrypted, otherwise false.
[8a64320e]202 *
[1dcc0b9]203 */
204static inline bool ieee80211_is_encrypted_frame(uint16_t frame_ctrl)
205{
206 frame_ctrl = uint16_t_le2host(frame_ctrl);
[a35b458]207
[cc575ef9]208 return (frame_ctrl & IEEE80211_FRAME_CTRL_PROTECTED);
[1dcc0b9]209}
210
[8a64320e]211/** Check if PAE packet is EAPOL-Key frame.
212 *
[1dcc0b9]213 * @param key_frame Pointer to start of EAPOL frame.
[8a64320e]214 *
[1dcc0b9]215 * @return True if it is EAPOL-Key frame, otherwise false.
[8a64320e]216 *
[1dcc0b9]217 */
[8a64320e]218static inline bool
[18b6a88]219ieee80211_is_eapol_key_frame(ieee80211_eapol_key_frame_t *key_frame)
[1dcc0b9]220{
221 return (key_frame->packet_type == IEEE80211_EAPOL_KEY);
222}
223
[8a64320e]224/** Generate packet sequence number.
225 *
[1dcc0b9]226 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]227 *
[1dcc0b9]228 * @return True if it has payload data, otherwise false.
[8a64320e]229 *
[1dcc0b9]230 */
231static uint16_t ieee80211_get_sequence_number(ieee80211_dev_t *ieee80211_dev)
232{
233 uint16_t ret_val = ieee80211_dev->sequence_number;
234 ieee80211_dev->sequence_number += (1 << 4);
[a35b458]235
[1dcc0b9]236 return ret_val;
237}
238
[8a64320e]239/** Get driver-specific structure for IEEE 802.11 device.
240 *
[59fa7ab]241 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]242 *
[59fa7ab]243 * @return Driver-specific structure.
[8a64320e]244 *
[59fa7ab]245 */
[8a64320e]246void *ieee80211_get_specific(ieee80211_dev_t *ieee80211_dev)
[59fa7ab]247{
248 return ieee80211_dev->specific;
249}
250
[8a64320e]251/** Set driver-specific structure for IEEE 802.11 device.
252 *
[59fa7ab]253 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]254 * @param specific Driver-specific structure.
255 *
[59fa7ab]256 */
[8a64320e]257void ieee80211_set_specific(ieee80211_dev_t *ieee80211_dev,
258 void *specific)
[59fa7ab]259{
260 ieee80211_dev->specific = specific;
261}
262
[8a64320e]263/** Get related DDF device.
264 *
[59fa7ab]265 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]266 *
[59fa7ab]267 * @return DDF device.
[8a64320e]268 *
[59fa7ab]269 */
[8a64320e]270ddf_dev_t *ieee80211_get_ddf_dev(ieee80211_dev_t *ieee80211_dev)
[59fa7ab]271{
272 return ieee80211_dev->ddf_dev;
273}
274
[8a64320e]275/** Query current operating mode of IEEE 802.11 device.
276 *
[59fa7ab]277 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]278 *
[59fa7ab]279 * @return Current IEEE 802.11 operating mode.
[8a64320e]280 *
[59fa7ab]281 */
[8a64320e]282ieee80211_operating_mode_t
[18b6a88]283ieee80211_query_current_op_mode(ieee80211_dev_t *ieee80211_dev)
[59fa7ab]284{
[053fc2b]285 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
286 ieee80211_operating_mode_t op_mode = ieee80211_dev->current_op_mode;
287 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]288
[053fc2b]289 return op_mode;
[59fa7ab]290}
291
[8a64320e]292/** Query current frequency of IEEE 802.11 device.
293 *
[59fa7ab]294 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]295 *
[59fa7ab]296 * @return Current device operating frequency.
[8a64320e]297 *
[59fa7ab]298 */
[8a64320e]299uint16_t ieee80211_query_current_freq(ieee80211_dev_t *ieee80211_dev)
[59fa7ab]300{
[053fc2b]301 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
302 uint16_t current_freq = ieee80211_dev->current_freq;
303 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]304
[053fc2b]305 return current_freq;
[59fa7ab]306}
307
[8a64320e]308/** Query BSSID the device is connected to.
309 *
[053fc2b]310 * Note: Expecting locked results_mutex.
[8a64320e]311 *
[1dcc0b9]312 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]313 * @param bssid Pointer to structure where should be stored BSSID.
314 *
[1dcc0b9]315 */
[8a64320e]316void ieee80211_query_bssid(ieee80211_dev_t *ieee80211_dev,
317 nic_address_t *bssid)
[1dcc0b9]318{
[053fc2b]319 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[a35b458]320
[8a64320e]321 if (bssid) {
322 ieee80211_scan_result_link_t *res_link =
323 ieee80211_dev->bssid_info.res_link;
[a35b458]324
[8a64320e]325 if (res_link) {
326 memcpy(bssid, &res_link->scan_result.bssid,
327 sizeof(nic_address_t));
[053fc2b]328 } else {
329 nic_address_t broadcast_addr;
330 memcpy(broadcast_addr.address,
[8a64320e]331 ieee80211_broadcast_mac_addr, ETH_ADDR);
[053fc2b]332 memcpy(bssid, &broadcast_addr, sizeof(nic_address_t));
333 }
[1dcc0b9]334 }
[a35b458]335
[053fc2b]336 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[1dcc0b9]337}
338
[8a64320e]339/** Get AID of network we are connected to.
340 *
[1dcc0b9]341 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]342 *
[1dcc0b9]343 * @return AID.
[8a64320e]344 *
[1dcc0b9]345 */
[8a64320e]346uint16_t ieee80211_get_aid(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]347{
[053fc2b]348 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
349 uint16_t aid = ieee80211_dev->bssid_info.aid;
350 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]351
[053fc2b]352 return aid;
[1dcc0b9]353}
354
[1b20da0]355/** Get pairwise security suite used for HW encryption.
[8a64320e]356 *
[1dcc0b9]357 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]358 *
[1dcc0b9]359 * @return Security suite indicator.
[8a64320e]360 *
[1dcc0b9]361 */
[8a64320e]362int ieee80211_get_pairwise_security(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]363{
[053fc2b]364 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[8a64320e]365 ieee80211_scan_result_link_t *auth_link =
366 ieee80211_dev->bssid_info.res_link;
[053fc2b]367 int suite = auth_link->scan_result.security.pair_alg;
368 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]369
[053fc2b]370 return suite;
[1dcc0b9]371}
372
[8a64320e]373/** Check if IEEE 802.11 device is connected to network.
374 *
[1dcc0b9]375 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]376 *
[1dcc0b9]377 * @return True if device is connected to network, otherwise false.
[8a64320e]378 *
[1dcc0b9]379 */
[8a64320e]380bool ieee80211_is_connected(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]381{
[053fc2b]382 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[8a64320e]383 bool conn_state =
384 ieee80211_dev->current_auth_phase == IEEE80211_AUTH_CONNECTED;
[053fc2b]385 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]386
[053fc2b]387 return conn_state;
388}
389
390void ieee80211_set_auth_phase(ieee80211_dev_t *ieee80211_dev,
[8a64320e]391 ieee80211_auth_phase_t auth_phase)
[053fc2b]392{
393 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
394 ieee80211_dev->current_auth_phase = auth_phase;
395 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
396}
397
[8a64320e]398ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t *ieee80211_dev)
[053fc2b]399{
400 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
401 ieee80211_auth_phase_t conn_state = ieee80211_dev->current_auth_phase;
402 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]403
[053fc2b]404 return conn_state;
405}
406
407void ieee80211_set_connect_request(ieee80211_dev_t *ieee80211_dev)
408{
409 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
410 ieee80211_dev->pending_conn_req = true;
411 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
412}
413
414bool ieee80211_pending_connect_request(ieee80211_dev_t *ieee80211_dev)
415{
416 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
417 bool conn_request = ieee80211_dev->pending_conn_req;
418 ieee80211_dev->pending_conn_req = false;
419 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]420
[053fc2b]421 return conn_request;
[1dcc0b9]422}
423
[8a64320e]424/** Report current operating mode for IEEE 802.11 device.
425 *
[59fa7ab]426 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]427 * @param op_mode Current IEEE 802.11 operating mode.
428 *
[59fa7ab]429 */
[8a64320e]430void ieee80211_report_current_op_mode(ieee80211_dev_t *ieee80211_dev,
431 ieee80211_operating_mode_t op_mode)
[59fa7ab]432{
[053fc2b]433 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[59fa7ab]434 ieee80211_dev->current_op_mode = op_mode;
[053fc2b]435 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[59fa7ab]436}
437
[8a64320e]438/** Report current frequency for IEEE 802.11 device.
439 *
[59fa7ab]440 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]441 * @param freq Current device operating frequency.
442 *
[59fa7ab]443 */
[8a64320e]444void ieee80211_report_current_freq(ieee80211_dev_t *ieee80211_dev,
445 uint16_t freq)
[59fa7ab]446{
[053fc2b]447 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[59fa7ab]448 ieee80211_dev->current_freq = freq;
[053fc2b]449 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[59fa7ab]450}
451
[8a64320e]452/** Check if IEEE 802.11 device is ready (fully initialized).
453 *
[1dcc0b9]454 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]455 *
[1dcc0b9]456 * @return True if device is ready to work, otherwise false.
[8a64320e]457 *
[1dcc0b9]458 */
[8a64320e]459bool ieee80211_is_ready(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]460{
461 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
462 bool ready_state = ieee80211_dev->ready;
463 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]464
[1dcc0b9]465 return ready_state;
466}
467
[8a64320e]468/** Set IEEE 802.11 device to ready state.
469 *
[1dcc0b9]470 * @param ieee80211_dev IEEE 802.11 device.
[8a64320e]471 * @param ready Ready state to be set.
472 *
[1dcc0b9]473 */
[8a64320e]474void ieee80211_set_ready(ieee80211_dev_t *ieee80211_dev, bool ready)
[1dcc0b9]475{
476 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
477 ieee80211_dev->ready = ready;
478 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
479}
480
[8a64320e]481extern bool ieee80211_query_using_key(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]482{
[053fc2b]483 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
484 bool using_key = ieee80211_dev->using_hw_key;
485 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]486
[053fc2b]487 return using_key;
[1dcc0b9]488}
489
[8a64320e]490void ieee80211_setup_key_confirm(ieee80211_dev_t *ieee80211_dev,
491 bool using_key)
[1dcc0b9]492{
[053fc2b]493 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[1dcc0b9]494 ieee80211_dev->using_hw_key = using_key;
[053fc2b]495 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
496}
497
[b7fd2a0]498static errno_t ieee80211_scan(void *arg)
[053fc2b]499{
500 assert(arg);
[a35b458]501
[053fc2b]502 ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *) arg;
[a35b458]503
[8a64320e]504 while (true) {
[053fc2b]505 ieee80211_dev->ops->scan(ieee80211_dev);
[5f97ef44]506 fibril_usleep(SCAN_PERIOD_USEC);
[053fc2b]507 }
[a35b458]508
[053fc2b]509 return EOK;
[1dcc0b9]510}
511
[8a64320e]512/** Implementation of NIC open callback for IEEE 802.11 devices.
513 *
[59fa7ab]514 * @param fun NIC function.
[8a64320e]515 *
[cde999a]516 * @return EOK if succeed, error code otherwise.
[8a64320e]517 *
[59fa7ab]518 */
[b7fd2a0]519static errno_t ieee80211_open(ddf_fun_t *fun)
[59fa7ab]520{
521 nic_t *nic_data = nic_get_from_ddf_fun(fun);
522 ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
[a35b458]523
[8a64320e]524 if (ieee80211_dev->started)
[59fa7ab]525 return EOK;
[a35b458]526
[8a64320e]527 ieee80211_dev->started = true;
[a35b458]528
[b7fd2a0]529 errno_t rc = ieee80211_dev->ops->start(ieee80211_dev);
[8a64320e]530 if (rc != EOK)
[59fa7ab]531 return rc;
[a35b458]532
[053fc2b]533 /* Add scanning fibril. */
534 fid_t fibril = fibril_create(ieee80211_scan, ieee80211_dev);
[8a64320e]535 if (fibril == 0)
[053fc2b]536 return ENOMEM;
[a35b458]537
[053fc2b]538 fibril_add_ready(fibril);
[a35b458]539
[59fa7ab]540 return EOK;
541}
542
[8a64320e]543/** Send frame handler.
544 *
545 * @param nic Pointer to NIC device.
[1dcc0b9]546 * @param data Data buffer.
547 * @param size Data buffer size.
[8a64320e]548 *
[59fa7ab]549 */
550static void ieee80211_send_frame(nic_t *nic, void *data, size_t size)
551{
[8a64320e]552 ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
553 nic_get_specific(nic);
[a35b458]554
[8a64320e]555 ieee80211_auth_phase_t auth_phase =
556 ieee80211_get_auth_phase(ieee80211_dev);
557 if ((auth_phase != IEEE80211_AUTH_ASSOCIATED) &&
558 (auth_phase != IEEE80211_AUTH_CONNECTED))
[1dcc0b9]559 return;
[a35b458]560
[1dcc0b9]561 ieee80211_scan_result_t *auth_data =
[8a64320e]562 &ieee80211_dev->bssid_info.res_link->scan_result;
[a35b458]563
[1dcc0b9]564 /* We drop part of IEEE 802.3 ethernet header. */
565 size_t drop_bytes = sizeof(eth_header_t) - 2;
[a35b458]566
[8a64320e]567 size_t complete_size = (size - drop_bytes) +
568 sizeof(ieee80211_data_header_t) +
569 ARRAY_SIZE(rfc1042_header);
[a35b458]570
[1dcc0b9]571 /* Init crypto data. */
[cc575ef9]572 bool add_mic = false;
[e71c023]573 const size_t max_head_space = max(IEEE80211_TKIP_HEADER_LENGTH,
574 IEEE80211_CCMP_HEADER_LENGTH);
[cc575ef9]575 size_t head_space = 0, mic_space = 0;
[1dcc0b9]576 uint16_t crypto = 0;
[e71c023]577 uint8_t head_data[max_head_space];
578 memset(head_data, 0, max_head_space);
[a35b458]579
[a931b7b]580 // TODO: Distinguish used key (pair/group) by dest address ?
[8a64320e]581 if (ieee80211_query_using_key(ieee80211_dev)) {
[1dcc0b9]582 int sec_suite = auth_data->security.pair_alg;
[8a64320e]583 switch (sec_suite) {
584 case IEEE80211_SECURITY_SUITE_TKIP:
585 head_space = IEEE80211_TKIP_HEADER_LENGTH;
586 mic_space = MIC_LENGTH;
587 add_mic = true;
588 break;
589 case IEEE80211_SECURITY_SUITE_CCMP:
590 head_space = IEEE80211_CCMP_HEADER_LENGTH;
591 head_data[3] = 0x20;
592 break;
593 default:
594 break;
[1dcc0b9]595 }
[a35b458]596
[1dcc0b9]597 crypto = uint16_t_le2host(IEEE80211_FRAME_CTRL_PROTECTED);
598 }
[a35b458]599
[cc575ef9]600 complete_size += head_space + mic_space;
[a35b458]601
[1dcc0b9]602 void *complete_buffer = malloc(complete_size);
[8a64320e]603 if (!complete_buffer)
604 return;
[a35b458]605
[1dcc0b9]606 memset(complete_buffer, 0, complete_size);
[a35b458]607
[8a64320e]608 if (head_space)
[1dcc0b9]609 memcpy(complete_buffer + sizeof(ieee80211_data_header_t),
[8a64320e]610 head_data, head_space);
[a35b458]611
[cc575ef9]612 memcpy(complete_buffer + sizeof(ieee80211_data_header_t) + head_space,
[8a64320e]613 rfc1042_header, ARRAY_SIZE(rfc1042_header));
[a35b458]614
[8a64320e]615 memcpy(complete_buffer + sizeof(ieee80211_data_header_t) +
616 ARRAY_SIZE(rfc1042_header) + head_space,
617 data + drop_bytes, size - drop_bytes);
[a35b458]618
[1dcc0b9]619 ieee80211_data_header_t *data_header =
[8a64320e]620 (ieee80211_data_header_t *) complete_buffer;
621 data_header->frame_ctrl =
622 uint16_t_le2host(IEEE80211_DATA_FRAME) |
623 uint16_t_le2host(IEEE80211_DATA_DATA_FRAME) |
624 uint16_t_le2host(IEEE80211_FRAME_CTRL_TODS) |
625 crypto;
[1dcc0b9]626 data_header->seq_ctrl = ieee80211_get_sequence_number(ieee80211_dev);
[a35b458]627
[1dcc0b9]628 /* BSSID, SA, DA. */
[8a64320e]629 memcpy(data_header->address1, auth_data->bssid.address, ETH_ADDR);
[1dcc0b9]630 memcpy(data_header->address2, data + ETH_ADDR, ETH_ADDR);
631 memcpy(data_header->address3, data, ETH_ADDR);
[a35b458]632
[8a64320e]633 if (add_mic) {
[cc575ef9]634 size_t size_wo_mic = complete_size - MIC_LENGTH;
635 uint8_t *tx_mic = ieee80211_dev->bssid_info.ptk +
[8a64320e]636 TK_OFFSET + IEEE80211_TKIP_TX_MIC_OFFSET;
[cc575ef9]637 ieee80211_michael_mic(tx_mic, complete_buffer, size_wo_mic,
[8a64320e]638 complete_buffer + size_wo_mic);
[cc575ef9]639 }
[a35b458]640
[8a64320e]641 ieee80211_dev->ops->tx_handler(ieee80211_dev,
642 complete_buffer, complete_size);
[a35b458]643
[1dcc0b9]644 free(complete_buffer);
[59fa7ab]645}
646
[8a64320e]647/** Fill out IEEE 802.11 device functions implementations.
648 *
649 * @param ieee80211_dev IEEE 802.11 device.
650 * @param ieee80211_ops Callbacks implementation.
[59fa7ab]651 * @param ieee80211_iface Interface functions implementation.
[8a64320e]652 * @param nic_iface NIC interface functions implementation.
653 * @param nic_dev_ops NIC device functions implementation.
654 *
655 * @return EINVAL when missing pointer to ieee80211_ops
656 * or ieee80211_iface, otherwise EOK.
657 *
[59fa7ab]658 */
[b7fd2a0]659static errno_t ieee80211_implement(ieee80211_dev_t *ieee80211_dev,
[8a64320e]660 ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
661 nic_iface_t *nic_iface, ddf_dev_ops_t *nic_dev_ops)
[59fa7ab]662{
[8a64320e]663 if (ieee80211_ops) {
664 if (!ieee80211_ops->start)
[59fa7ab]665 ieee80211_ops->start = ieee80211_start_impl;
[a35b458]666
[8a64320e]667 if (!ieee80211_ops->tx_handler)
[59fa7ab]668 ieee80211_ops->tx_handler = ieee80211_tx_handler_impl;
[a35b458]669
[8a64320e]670 if (!ieee80211_ops->set_freq)
[59fa7ab]671 ieee80211_ops->set_freq = ieee80211_set_freq_impl;
[a35b458]672
[8a64320e]673 if (!ieee80211_ops->bssid_change)
674 ieee80211_ops->bssid_change = ieee80211_bssid_change_impl;
[a35b458]675
[8a64320e]676 if (!ieee80211_ops->key_config)
[1dcc0b9]677 ieee80211_ops->key_config = ieee80211_key_config_impl;
[a35b458]678
[8a64320e]679 if (!ieee80211_ops->scan)
[59fa7ab]680 ieee80211_ops->scan = ieee80211_scan_impl;
[8a64320e]681 } else
[59fa7ab]682 return EINVAL;
[a35b458]683
[59fa7ab]684 ieee80211_dev->ops = ieee80211_ops;
[a35b458]685
[8a64320e]686 if (ieee80211_iface) {
687 if (nic_dev_ops)
[59fa7ab]688 if (!nic_dev_ops->interfaces[IEEE80211_DEV_IFACE])
[8a64320e]689 nic_dev_ops->interfaces[IEEE80211_DEV_IFACE] =
690 ieee80211_iface;
[a35b458]691
[8a64320e]692 if (!ieee80211_iface->get_scan_results)
693 ieee80211_iface->get_scan_results =
694 ieee80211_get_scan_results_impl;
[a35b458]695
[8a64320e]696 if (!ieee80211_iface->connect)
[59fa7ab]697 ieee80211_iface->connect = ieee80211_connect_impl;
[a35b458]698
[8a64320e]699 if (!ieee80211_iface->disconnect)
[1dcc0b9]700 ieee80211_iface->disconnect = ieee80211_disconnect_impl;
[8a64320e]701 } else
[59fa7ab]702 return EINVAL;
[a35b458]703
[8a64320e]704 if (nic_dev_ops) {
705 if (!nic_dev_ops->open)
[59fa7ab]706 nic_dev_ops->open = ieee80211_open;
[8a64320e]707 } else
[59fa7ab]708 return EINVAL;
[a35b458]709
[59fa7ab]710 ieee80211_dev->iface = ieee80211_iface;
[a35b458]711
[59fa7ab]712 nic_driver_implement(NULL, nic_dev_ops, nic_iface);
[a35b458]713
[59fa7ab]714 return EOK;
715}
716
[8a64320e]717/** Allocate IEEE802.11 device structure.
718 *
[59fa7ab]719 * @return Pointer to allocated IEEE802.11 device structure.
[8a64320e]720 *
[59fa7ab]721 */
[193d280c]722ieee80211_dev_t *ieee80211_device_create(void)
[59fa7ab]723{
724 return calloc(1, sizeof(ieee80211_dev_t));
725}
726
[8a64320e]727/** Initialize an IEEE802.11 framework structure.
728 *
[59fa7ab]729 * @param ieee80211_dev Device structure to initialize.
[8a64320e]730 * @param ddf_dev Pointer to backing DDF device structure.
731 *
[cde999a]732 * @return EOK if succeed, error code otherwise.
[8a64320e]733 *
[59fa7ab]734 */
[b7fd2a0]735errno_t ieee80211_device_init(ieee80211_dev_t *ieee80211_dev, ddf_dev_t *ddf_dev)
[59fa7ab]736{
737 ieee80211_dev->ddf_dev = ddf_dev;
738 ieee80211_dev->started = false;
[1dcc0b9]739 ieee80211_dev->ready = false;
740 ieee80211_dev->using_hw_key = false;
[053fc2b]741 ieee80211_dev->pending_conn_req = false;
[59fa7ab]742 ieee80211_dev->current_op_mode = IEEE80211_OPMODE_STATION;
[1dcc0b9]743 ieee80211_dev->current_auth_phase = IEEE80211_AUTH_DISCONNECTED;
[a35b458]744
[8a64320e]745 memcpy(ieee80211_dev->bssid_mask.address, ieee80211_broadcast_mac_addr,
746 ETH_ADDR);
[a35b458]747
[1dcc0b9]748 ieee80211_scan_result_list_init(&ieee80211_dev->ap_list);
[a35b458]749
[053fc2b]750 fibril_mutex_initialize(&ieee80211_dev->scan_mutex);
[1dcc0b9]751 fibril_mutex_initialize(&ieee80211_dev->gen_mutex);
752 fibril_condvar_initialize(&ieee80211_dev->gen_cond);
[a35b458]753
[59fa7ab]754 /* Bind NIC to device */
755 nic_t *nic = nic_create_and_bind(ddf_dev);
[8a64320e]756 if (!nic)
[59fa7ab]757 return ENOMEM;
[a35b458]758
[59fa7ab]759 nic_set_specific(nic, ieee80211_dev);
[a35b458]760
[59fa7ab]761 return EOK;
762}
763
[8a64320e]764/** IEEE802.11 WiFi framework initialization.
765 *
766 * @param ieee80211_dev Device structure to initialize.
767 * @param ieee80211_ops Structure with implemented IEEE802.11
768 * device operations.
769 * @param ieee80211_iface Structure with implemented IEEE802.11
770 * interface operations.
771 *
[cde999a]772 * @return EOK if succeed, error code otherwise.
[8a64320e]773 *
[59fa7ab]774 */
[b7fd2a0]775errno_t ieee80211_init(ieee80211_dev_t *ieee80211_dev,
[8a64320e]776 ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
777 nic_iface_t *ieee80211_nic_iface, ddf_dev_ops_t *ieee80211_nic_dev_ops)
[59fa7ab]778{
[b7fd2a0]779 errno_t rc = ieee80211_implement(ieee80211_dev,
[8a64320e]780 ieee80211_ops, ieee80211_iface,
781 ieee80211_nic_iface, ieee80211_nic_dev_ops);
782 if (rc != EOK)
[59fa7ab]783 return rc;
[a35b458]784
[59fa7ab]785 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
[a35b458]786
[8a64320e]787 /* TODO: Set NIC handlers here. */
[59fa7ab]788 nic_set_send_frame_handler(nic, ieee80211_send_frame);
[a35b458]789
[8a64320e]790 ddf_fun_t *fun = ddf_fun_create(ieee80211_dev->ddf_dev, fun_exposed,
791 "port0");
792 if (fun == NULL)
[59fa7ab]793 return EINVAL;
[a35b458]794
[59fa7ab]795 nic_set_ddf_fun(nic, fun);
796 ddf_fun_set_ops(fun, ieee80211_nic_dev_ops);
[a35b458]797
[59fa7ab]798 rc = ddf_fun_bind(fun);
799 if (rc != EOK) {
800 ddf_fun_destroy(fun);
801 return rc;
802 }
[a35b458]803
[59fa7ab]804 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
805 if (rc != EOK) {
806 ddf_fun_unbind(fun);
807 return rc;
808 }
[a35b458]809
[59fa7ab]810 rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_IEEE80211);
811 if (rc != EOK) {
812 ddf_fun_unbind(fun);
813 return rc;
814 }
[a35b458]815
[59fa7ab]816 return EOK;
817}
818
[8a64320e]819/** Convert frequency value to channel number.
820 *
[1dcc0b9]821 * @param freq IEEE 802.11 operating frequency.
[8a64320e]822 *
[1dcc0b9]823 * @return Operating channel number.
[8a64320e]824 *
[1dcc0b9]825 */
[59fa7ab]826static uint8_t ieee80211_freq_to_channel(uint16_t freq)
827{
828 return (freq - IEEE80211_FIRST_FREQ) / IEEE80211_CHANNEL_GAP + 1;
829}
830
[1dcc0b9]831static void ieee80211_prepare_ie_header(void **ie_header,
[8a64320e]832 uint8_t id, uint8_t length, void *data)
[1dcc0b9]833{
834 ieee80211_ie_header_t *header =
[8a64320e]835 (ieee80211_ie_header_t *) *ie_header;
[a35b458]836
[1dcc0b9]837 header->element_id = id;
838 header->length = length;
[a35b458]839
[1dcc0b9]840 memcpy(*ie_header + sizeof(ieee80211_ie_header_t), data, length);
[a35b458]841
[8a64320e]842 *ie_header = (void *) ((void *) header +
843 sizeof(ieee80211_ie_header_t) + length);
[1dcc0b9]844}
845
[8a64320e]846/** Probe request implementation.
847 *
[59fa7ab]848 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]849 * @param ssid Probing SSID or NULL if broadcast.
850 *
[cde999a]851 * @return EOK if succeed, error code otherwise.
[8a64320e]852 *
[59fa7ab]853 */
[b7fd2a0]854errno_t ieee80211_probe_request(ieee80211_dev_t *ieee80211_dev, char *ssid)
[59fa7ab]855{
856 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
857 nic_address_t nic_address;
858 nic_query_address(nic, &nic_address);
[a35b458]859
[1dcc0b9]860 size_t ssid_data_size = (ssid != NULL) ? str_size(ssid) : 0;
861 size_t channel_data_size = 1;
[a35b458]862
[8a64320e]863 uint8_t channel =
864 ieee80211_freq_to_channel(ieee80211_dev->current_freq);
[a35b458]865
[8a64320e]866 /*
867 * 4 headers - (ssid, rates, ext rates, current channel)
868 * and their data lengths.
[59fa7ab]869 */
[8a64320e]870 size_t payload_size =
871 sizeof(ieee80211_ie_header_t) * 4 +
872 ssid_data_size +
873 IEEE80211_DATA_RATES_SIZE + IEEE80211_EXT_DATA_RATES_SIZE +
874 channel_data_size;
[a35b458]875
[59fa7ab]876 size_t buffer_size = sizeof(ieee80211_mgmt_header_t) + payload_size;
877 void *buffer = malloc(buffer_size);
[8a64320e]878 if (!buffer)
879 return ENOMEM;
[a35b458]880
[59fa7ab]881 memset(buffer, 0, buffer_size);
[a35b458]882
[8a64320e]883 ieee80211_mgmt_header_t *mgmt_header =
884 (ieee80211_mgmt_header_t *) buffer;
[a35b458]885
[8a64320e]886 mgmt_header->frame_ctrl =
887 host2uint16_t_le(IEEE80211_MGMT_FRAME |
888 IEEE80211_MGMT_PROBE_REQ_FRAME);
[59fa7ab]889 memcpy(mgmt_header->dest_addr, ieee80211_broadcast_mac_addr, ETH_ADDR);
890 memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
891 memcpy(mgmt_header->bssid, ieee80211_broadcast_mac_addr, ETH_ADDR);
[8a64320e]892 mgmt_header->seq_ctrl =
893 host2uint16_t_le(ieee80211_get_sequence_number(ieee80211_dev));
[a35b458]894
[1dcc0b9]895 /* Jump to payload. */
896 void *it = (void *) buffer + sizeof(ieee80211_mgmt_header_t);
[8a64320e]897 ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE, ssid_data_size,
898 (void *) ssid);
899 ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
900 IEEE80211_DATA_RATES_SIZE, (void *) &ieee80211bg_data_rates);
901 ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
902 IEEE80211_EXT_DATA_RATES_SIZE,
903 (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
904 ieee80211_prepare_ie_header(&it, IEEE80211_CHANNEL_IE,
905 channel_data_size, (void *) &channel);
[a35b458]906
[59fa7ab]907 ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
[a35b458]908
[59fa7ab]909 free(buffer);
[a35b458]910
[59fa7ab]911 return EOK;
912}
913
[8a64320e]914/** IEEE 802.11 authentication implementation.
915 *
[59fa7ab]916 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]917 *
[cde999a]918 * @return EOK if succeed, error code otherwise.
[8a64320e]919 *
[59fa7ab]920 */
[b7fd2a0]921errno_t ieee80211_authenticate(ieee80211_dev_t *ieee80211_dev)
[59fa7ab]922{
923 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
924 nic_address_t nic_address;
925 nic_query_address(nic, &nic_address);
[a35b458]926
[1dcc0b9]927 ieee80211_scan_result_t *auth_data =
[8a64320e]928 &ieee80211_dev->bssid_info.res_link->scan_result;
[a35b458]929
[8a64320e]930 size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
931 sizeof(ieee80211_auth_body_t);
[a35b458]932
[59fa7ab]933 void *buffer = malloc(buffer_size);
[8a64320e]934 if (!buffer)
935 return ENOMEM;
[a35b458]936
[59fa7ab]937 memset(buffer, 0, buffer_size);
[a35b458]938
[8a64320e]939 ieee80211_mgmt_header_t *mgmt_header =
940 (ieee80211_mgmt_header_t *) buffer;
[a35b458]941
[8a64320e]942 mgmt_header->frame_ctrl =
943 host2uint16_t_le(IEEE80211_MGMT_FRAME |
944 IEEE80211_MGMT_AUTH_FRAME);
[1dcc0b9]945 memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
[59fa7ab]946 memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
[1dcc0b9]947 memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
[a35b458]948
[59fa7ab]949 ieee80211_auth_body_t *auth_body =
[8a64320e]950 (ieee80211_auth_body_t *)
951 (buffer + sizeof(ieee80211_mgmt_header_t));
[59fa7ab]952 auth_body->auth_alg = host2uint16_t_le(0);
[1dcc0b9]953 auth_body->auth_trans_no = host2uint16_t_le(1);
[a35b458]954
[59fa7ab]955 ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
[a35b458]956
[59fa7ab]957 free(buffer);
[a35b458]958
[59fa7ab]959 return EOK;
960}
961
[8a64320e]962/** IEEE 802.11 association implementation.
963 *
[1dcc0b9]964 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]965 * @param password Passphrase to be used in encrypted communication
966 * or NULL for open networks.
967 *
[cde999a]968 * @return EOK if succeed, error code otherwise.
[8a64320e]969 *
[1dcc0b9]970 */
[b7fd2a0]971errno_t ieee80211_associate(ieee80211_dev_t *ieee80211_dev, char *password)
[1dcc0b9]972{
973 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
974 nic_address_t nic_address;
975 nic_query_address(nic, &nic_address);
[a35b458]976
[1dcc0b9]977 ieee80211_scan_result_link_t *auth_link =
[8a64320e]978 ieee80211_dev->bssid_info.res_link;
[a35b458]979
[1dcc0b9]980 ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
[a35b458]981
[1dcc0b9]982 size_t ssid_data_size = str_size(auth_data->ssid);
[a35b458]983
[8a64320e]984 size_t payload_size =
985 sizeof(ieee80211_ie_header_t) * 3 +
986 ssid_data_size +
987 IEEE80211_DATA_RATES_SIZE +
988 IEEE80211_EXT_DATA_RATES_SIZE;
[a35b458]989
[8a64320e]990 size_t buffer_size =
991 sizeof(ieee80211_mgmt_header_t) +
992 sizeof(ieee80211_assoc_req_body_t) +
993 payload_size;
[a35b458]994
[8a64320e]995 if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
996 (auth_data->security.type == IEEE80211_SECURITY_WPA2))
[a931b7b]997 buffer_size += auth_link->auth_ie_len;
[a35b458]998
[1dcc0b9]999 void *buffer = malloc(buffer_size);
[8a64320e]1000 if (!buffer)
1001 return ENOMEM;
[a35b458]1002
[1dcc0b9]1003 memset(buffer, 0, buffer_size);
[a35b458]1004
[8a64320e]1005 ieee80211_mgmt_header_t *mgmt_header =
1006 (ieee80211_mgmt_header_t *) buffer;
[a35b458]1007
[8a64320e]1008 mgmt_header->frame_ctrl =
1009 host2uint16_t_le(IEEE80211_MGMT_FRAME |
1010 IEEE80211_MGMT_ASSOC_REQ_FRAME);
[1dcc0b9]1011 memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
1012 memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
1013 memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
[a35b458]1014
[1dcc0b9]1015 ieee80211_assoc_req_body_t *assoc_body =
[8a64320e]1016 (ieee80211_assoc_req_body_t *)
1017 (buffer + sizeof(ieee80211_mgmt_header_t));
[1dcc0b9]1018 assoc_body->listen_interval = host2uint16_t_le(1);
[a35b458]1019
[1dcc0b9]1020 /* Jump to payload. */
1021 void *it = buffer + sizeof(ieee80211_mgmt_header_t) +
[8a64320e]1022 sizeof(ieee80211_assoc_req_body_t);
1023 ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE,
1024 ssid_data_size, (void *) auth_data->ssid);
1025 ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
1026 IEEE80211_DATA_RATES_SIZE, (void *) &ieee80211bg_data_rates);
1027 ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
1028 IEEE80211_EXT_DATA_RATES_SIZE,
1029 (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
[a35b458]1030
[8a64320e]1031 if (auth_data->security.type != IEEE80211_SECURITY_OPEN)
[1dcc0b9]1032 assoc_body->capability |= host2uint16_t_le(CAP_SECURITY);
[a35b458]1033
[8a64320e]1034 if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
1035 (auth_data->security.type == IEEE80211_SECURITY_WPA2))
1036 memcpy(it, auth_link->auth_ie, auth_link->auth_ie_len);
[a35b458]1037
[1dcc0b9]1038 ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
[a35b458]1039
[8a64320e]1040 /*
1041 * Save password to be used in eventual authentication handshake.
[1dcc0b9]1042 */
[053fc2b]1043 memset(ieee80211_dev->bssid_info.password, 0, IEEE80211_MAX_PASSW_LEN);
[8a64320e]1044 memcpy(ieee80211_dev->bssid_info.password, password,
1045 str_size(password));
[a35b458]1046
[1dcc0b9]1047 free(buffer);
[a35b458]1048
[1dcc0b9]1049 return EOK;
1050}
1051
[8a64320e]1052/** IEEE 802.11 deauthentication implementation.
1053 *
[a931b7b]1054 * Note: Expecting locked results_mutex or scan_mutex.
[8a64320e]1055 *
[1dcc0b9]1056 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1057 *
[cde999a]1058 * @return EOK if succeed, error code otherwise.
[8a64320e]1059 *
[1dcc0b9]1060 */
[b7fd2a0]1061errno_t ieee80211_deauthenticate(ieee80211_dev_t *ieee80211_dev)
[1dcc0b9]1062{
1063 ieee80211_scan_result_t *auth_data =
[8a64320e]1064 &ieee80211_dev->bssid_info.res_link->scan_result;
[a35b458]1065
[1dcc0b9]1066 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
1067 nic_address_t nic_address;
1068 nic_query_address(nic, &nic_address);
[a35b458]1069
[8a64320e]1070 size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
1071 sizeof(ieee80211_deauth_body_t);
[a35b458]1072
[1dcc0b9]1073 void *buffer = malloc(buffer_size);
[8a64320e]1074 if (!buffer)
1075 return ENOMEM;
[a35b458]1076
[1dcc0b9]1077 memset(buffer, 0, buffer_size);
[a35b458]1078
[8a64320e]1079 ieee80211_mgmt_header_t *mgmt_header =
1080 (ieee80211_mgmt_header_t *) buffer;
[a35b458]1081
[8a64320e]1082 mgmt_header->frame_ctrl =
1083 host2uint16_t_le(IEEE80211_MGMT_FRAME |
1084 IEEE80211_MGMT_DEAUTH_FRAME);
[1dcc0b9]1085 memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
1086 memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
1087 memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
[a35b458]1088
[1dcc0b9]1089 ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
[a35b458]1090
[1dcc0b9]1091 free(buffer);
[a35b458]1092
[053fc2b]1093 ieee80211_dev->bssid_info.res_link = NULL;
1094 ieee80211_dev->ops->bssid_change(ieee80211_dev, false);
[a35b458]1095
[8a64320e]1096 if (ieee80211_query_using_key(ieee80211_dev))
[053fc2b]1097 ieee80211_dev->ops->key_config(ieee80211_dev, NULL, false);
[a35b458]1098
[053fc2b]1099 ieee80211_set_auth_phase(ieee80211_dev, IEEE80211_AUTH_DISCONNECTED);
[a35b458]1100
[1dcc0b9]1101 return EOK;
1102}
1103
1104static void ieee80211_process_auth_info(ieee80211_scan_result_link_t *ap_data,
[8a64320e]1105 void *buffer)
[1dcc0b9]1106{
1107 uint8_t *it = (uint8_t *) buffer;
[a35b458]1108
[1dcc0b9]1109 uint16_t *version = (uint16_t *) it;
[8a64320e]1110 if (uint16_t_le2host(*version) != 0x1) {
[1dcc0b9]1111 ap_data->scan_result.security.type = -1;
1112 return;
1113 }
[a35b458]1114
[1dcc0b9]1115 it += sizeof(uint16_t);
[a35b458]1116
[8a64320e]1117 uint32_t group_cipher = *(it + 3);
1118 switch (group_cipher) {
1119 case IEEE80211_AUTH_CIPHER_TKIP:
1120 ap_data->scan_result.security.group_alg =
1121 IEEE80211_SECURITY_SUITE_TKIP;
1122 break;
1123 case IEEE80211_AUTH_CIPHER_CCMP:
1124 ap_data->scan_result.security.group_alg =
1125 IEEE80211_SECURITY_SUITE_CCMP;
1126 break;
1127 default:
1128 ap_data->scan_result.security.group_alg = -1;
[1dcc0b9]1129 }
[a35b458]1130
[8a64320e]1131 it += 4 * sizeof(uint8_t);
[a35b458]1132
[1dcc0b9]1133 uint16_t *pairwise_count = (uint16_t *) it;
[8a64320e]1134 uint32_t pairwise_cipher = *(it + sizeof(uint16_t) + 3);
1135 switch (pairwise_cipher) {
1136 case IEEE80211_AUTH_CIPHER_TKIP:
1137 ap_data->scan_result.security.pair_alg =
1138 IEEE80211_SECURITY_SUITE_TKIP;
1139 break;
1140 case IEEE80211_AUTH_CIPHER_CCMP:
1141 ap_data->scan_result.security.pair_alg =
1142 IEEE80211_SECURITY_SUITE_CCMP;
1143 break;
1144 default:
1145 ap_data->scan_result.security.pair_alg = -1;
[1dcc0b9]1146 }
[a35b458]1147
[8a64320e]1148 it += 2 * sizeof(uint16_t) +
1149 uint16_t_le2host(*pairwise_count) * sizeof(uint32_t);
[a35b458]1150
[8a64320e]1151 uint32_t auth_suite = *(it + 3);
1152 switch (auth_suite) {
1153 case IEEE80211_AUTH_AKM_PSK:
1154 ap_data->scan_result.security.auth =
1155 IEEE80211_SECURITY_AUTH_PSK;
1156 break;
1157 case IEEE80211_AUTH_AKM_8021X:
1158 ap_data->scan_result.security.auth =
1159 IEEE80211_SECURITY_AUTH_8021X;
1160 break;
1161 default:
1162 ap_data->scan_result.security.auth = -1;
[1dcc0b9]1163 }
1164}
1165
[a931b7b]1166static void copy_auth_ie(ieee80211_ie_header_t *ie_header,
[8a64320e]1167 ieee80211_scan_result_link_t *ap_data, void *it)
[a931b7b]1168{
[8a64320e]1169 ap_data->auth_ie_len = ie_header->length +
1170 sizeof(ieee80211_ie_header_t);
[a35b458]1171
[a931b7b]1172 memcpy(ap_data->auth_ie, it, ap_data->auth_ie_len);
1173}
1174
[1dcc0b9]1175static uint8_t *ieee80211_process_ies(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1176 ieee80211_scan_result_link_t *ap_data, void *buffer, size_t buffer_size)
[1dcc0b9]1177{
1178 void *it = buffer;
[8a64320e]1179 while ((it + sizeof(ieee80211_ie_header_t)) < buffer + buffer_size) {
1180 ieee80211_ie_header_t *ie_header =
1181 (ieee80211_ie_header_t *) it;
[1dcc0b9]1182 uint8_t *channel;
[09044cb]1183 uint32_t oui;
[a35b458]1184
[8a64320e]1185 switch (ie_header->element_id) {
1186 case IEEE80211_CHANNEL_IE:
1187 if (!ap_data)
1188 break;
[a35b458]1189
[8a64320e]1190 channel = (uint8_t *)
1191 (it + sizeof(ieee80211_ie_header_t));
1192 ap_data->scan_result.channel = *channel;
1193 break;
1194 case IEEE80211_RSN_IE:
1195 if (!ap_data)
[1dcc0b9]1196 break;
[a35b458]1197
[8a64320e]1198 ap_data->scan_result.security.type =
1199 IEEE80211_SECURITY_WPA2;
1200 ieee80211_process_auth_info(ap_data,
1201 it + sizeof(ieee80211_ie_header_t));
1202 copy_auth_ie(ie_header, ap_data, it);
1203 break;
1204 case IEEE80211_VENDOR_IE:
1205 oui = uint32be_from_seq(it +
1206 sizeof(ieee80211_ie_header_t));
[a35b458]1207
[8a64320e]1208 if (oui == WPA_OUI) {
1209 if (!ap_data)
1210 break;
[a35b458]1211
[8a64320e]1212 /* Prefering WPA2. */
1213 if (ap_data->scan_result.security.type ==
1214 IEEE80211_SECURITY_WPA2)
[1dcc0b9]1215 break;
[a35b458]1216
[1dcc0b9]1217 ap_data->scan_result.security.type =
[8a64320e]1218 IEEE80211_SECURITY_WPA;
[a35b458]1219
[8a64320e]1220 ieee80211_process_auth_info(ap_data,
1221 it + sizeof(ieee80211_ie_header_t) +
1222 sizeof(uint32_t));
[a931b7b]1223 copy_auth_ie(ie_header, ap_data, it);
[8a64320e]1224 } else if (oui == GTK_OUI) {
1225 return it +
1226 sizeof(ieee80211_ie_header_t) +
1227 sizeof(uint32_t);
1228 }
[1dcc0b9]1229 }
[a35b458]1230
[1dcc0b9]1231 it += sizeof(ieee80211_ie_header_t) + ie_header->length;
1232 }
[a35b458]1233
[1dcc0b9]1234 return NULL;
1235}
1236
[8a64320e]1237/** Process probe response and store results.
1238 *
[59fa7ab]1239 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1240 * @param mgmt_header Pointer to start of management frame header.
1241 *
[cde999a]1242 * @return EOK if succeed, error code otherwise.
[8a64320e]1243 *
[59fa7ab]1244 */
[b7fd2a0]1245static errno_t ieee80211_process_probe_response(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1246 ieee80211_mgmt_header_t *mgmt_header, size_t buffer_size)
[59fa7ab]1247{
[8a64320e]1248 ieee80211_beacon_start_t *beacon_body = (ieee80211_beacon_start_t *)
1249 ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
[a35b458]1250
[8a64320e]1251 ieee80211_ie_header_t *ssid_ie_header = (ieee80211_ie_header_t *)
1252 ((void *) beacon_body + sizeof(ieee80211_beacon_start_t));
[a35b458]1253
[1dcc0b9]1254 /* Not empty SSID. */
[8a64320e]1255 if (ssid_ie_header->length > 0) {
[1dcc0b9]1256 ieee80211_scan_result_list_t *result_list =
[8a64320e]1257 &ieee80211_dev->ap_list;
[a35b458]1258
[8a64320e]1259 uint8_t *ssid_start = (uint8_t *) ((void *) ssid_ie_header +
1260 sizeof(ieee80211_ie_header_t));
[1dcc0b9]1261 char ssid[IEEE80211_MAX_SSID_LENGTH];
[a35b458]1262
[1dcc0b9]1263 memcpy(ssid, ssid_start, ssid_ie_header->length);
1264 ssid[ssid_ie_header->length] = '\0';
[a35b458]1265
[1dcc0b9]1266 /* Check whether SSID is already in results. */
1267 ieee80211_scan_result_list_foreach(*result_list, result) {
[8a64320e]1268 if (!str_cmp(ssid, result->scan_result.ssid)) {
[1dcc0b9]1269 result->last_beacon = time(NULL);
1270 return EOK;
1271 }
1272 }
[a35b458]1273
[1dcc0b9]1274 /* Results are full. */
[8a64320e]1275 if (result_list->size == IEEE80211_MAX_RESULTS_LENGTH - 1)
[1dcc0b9]1276 return EOK;
[a35b458]1277
[8a64320e]1278 ieee80211_scan_result_link_t *ap_data =
1279 malloc(sizeof(ieee80211_scan_result_link_t));
1280 if (!ap_data)
1281 return ENOMEM;
[a35b458]1282
[1dcc0b9]1283 memset(ap_data, 0, sizeof(ieee80211_scan_result_link_t));
1284 link_initialize(&ap_data->link);
[a35b458]1285
[8a64320e]1286 memcpy(ap_data->scan_result.bssid.address,
1287 mgmt_header->bssid, ETH_ADDR);
1288 memcpy(ap_data->scan_result.ssid, ssid,
1289 ssid_ie_header->length + 1);
[a35b458]1290
[8a64320e]1291 if (uint16_t_le2host(beacon_body->capability) & CAP_SECURITY) {
1292 ap_data->scan_result.security.type =
1293 IEEE80211_SECURITY_WEP;
[1dcc0b9]1294 } else {
[8a64320e]1295 ap_data->scan_result.security.type =
1296 IEEE80211_SECURITY_OPEN;
[1dcc0b9]1297 ap_data->scan_result.security.auth = -1;
1298 ap_data->scan_result.security.pair_alg = -1;
1299 ap_data->scan_result.security.group_alg = -1;
1300 }
[a35b458]1301
[1dcc0b9]1302 void *rest_ies_start = ssid_start + ssid_ie_header->length;
[8a64320e]1303 size_t rest_buffer_size =
1304 buffer_size -
1305 sizeof(ieee80211_mgmt_header_t) -
1306 sizeof(ieee80211_beacon_start_t) -
1307 sizeof(ieee80211_ie_header_t) -
1308 ssid_ie_header->length;
[a35b458]1309
[8a64320e]1310 ieee80211_process_ies(ieee80211_dev, ap_data, rest_ies_start,
1311 rest_buffer_size);
[a35b458]1312
[1dcc0b9]1313 ap_data->last_beacon = time(NULL);
[a35b458]1314
[053fc2b]1315 fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
[1dcc0b9]1316 ieee80211_scan_result_list_append(result_list, ap_data);
[053fc2b]1317 fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
[1dcc0b9]1318 }
[a35b458]1319
[1dcc0b9]1320 return EOK;
1321}
[59fa7ab]1322
[8a64320e]1323/** Process authentication response.
1324 *
[1dcc0b9]1325 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1326 * @param mgmt_header Pointer to start of management frame header.
1327 *
[cde999a]1328 * @return EOK if succeed, error code otherwise.
[8a64320e]1329 *
[1dcc0b9]1330 */
[b7fd2a0]1331static errno_t ieee80211_process_auth_response(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1332 ieee80211_mgmt_header_t *mgmt_header)
[1dcc0b9]1333{
[053fc2b]1334 ieee80211_auth_body_t *auth_body =
[8a64320e]1335 (ieee80211_auth_body_t *)
1336 ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
[a35b458]1337
[8a64320e]1338 if (auth_body->status != 0)
1339 ieee80211_set_auth_phase(ieee80211_dev,
1340 IEEE80211_AUTH_DISCONNECTED);
1341 else
1342 ieee80211_set_auth_phase(ieee80211_dev,
1343 IEEE80211_AUTH_AUTHENTICATED);
[a35b458]1344
[053fc2b]1345 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[1dcc0b9]1346 fibril_condvar_signal(&ieee80211_dev->gen_cond);
1347 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]1348
[1dcc0b9]1349 return EOK;
1350}
1351
[8a64320e]1352/** Process association response.
1353 *
[1dcc0b9]1354 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1355 * @param mgmt_header Pointer to start of management frame header.
1356 *
[cde999a]1357 * @return EOK if succeed, error code otherwise.
[8a64320e]1358 *
[1dcc0b9]1359 */
[b7fd2a0]1360static errno_t ieee80211_process_assoc_response(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1361 ieee80211_mgmt_header_t *mgmt_header)
[1dcc0b9]1362{
1363 ieee80211_assoc_resp_body_t *assoc_resp =
[8a64320e]1364 (ieee80211_assoc_resp_body_t *) ((void *) mgmt_header +
1365 sizeof(ieee80211_mgmt_header_t));
[a35b458]1366
[8a64320e]1367 if (assoc_resp->status != 0)
1368 ieee80211_set_auth_phase(ieee80211_dev,
1369 IEEE80211_AUTH_DISCONNECTED);
1370 else {
1371 ieee80211_dev->bssid_info.aid =
1372 uint16_t_le2host(assoc_resp->aid);
1373 ieee80211_set_auth_phase(ieee80211_dev,
1374 IEEE80211_AUTH_ASSOCIATED);
[053fc2b]1375 ieee80211_dev->ops->bssid_change(ieee80211_dev, true);
1376 }
[a35b458]1377
[053fc2b]1378 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
[1dcc0b9]1379 fibril_condvar_signal(&ieee80211_dev->gen_cond);
1380 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
[a35b458]1381
[1dcc0b9]1382 return EOK;
1383}
1384
[b7fd2a0]1385static errno_t ieee80211_process_4way_handshake(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1386 void *buffer, size_t buffer_size)
[1dcc0b9]1387{
[8a64320e]1388 ieee80211_eapol_key_frame_t *key_frame =
1389 (ieee80211_eapol_key_frame_t *) buffer;
[a35b458]1390
[8a64320e]1391 ieee80211_scan_result_link_t *auth_link =
1392 ieee80211_dev->bssid_info.res_link;
[a35b458]1393
[1dcc0b9]1394 ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
[a35b458]1395
[a931b7b]1396 /* We don't support 802.1X authentication yet. */
[8a64320e]1397 if (auth_data->security.auth == IEEE80211_AUTH_AKM_8021X)
[a931b7b]1398 return ENOTSUP;
[a35b458]1399
[1dcc0b9]1400 uint8_t *ptk = ieee80211_dev->bssid_info.ptk;
1401 uint8_t *gtk = ieee80211_dev->bssid_info.gtk;
[09044cb]1402 uint8_t gtk_id = 1;
[a35b458]1403
[a931b7b]1404 bool handshake_done = false;
[a35b458]1405
[8a64320e]1406 bool old_wpa =
1407 auth_data->security.type == IEEE80211_SECURITY_WPA;
[a35b458]1408
[a931b7b]1409 bool key_phase =
[8a64320e]1410 uint16_t_be2host(key_frame->key_info) &
1411 IEEE80211_EAPOL_KEY_KEYINFO_MIC;
[a35b458]1412
[8a64320e]1413 bool final_phase =
1414 uint16_t_be2host(key_frame->key_info) &
1415 IEEE80211_EAPOL_KEY_KEYINFO_SECURE;
[a35b458]1416
[8a64320e]1417 bool ccmp_used =
1418 (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP) ||
1419 (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP);
[a35b458]1420
[1dcc0b9]1421 size_t ptk_key_length, gtk_key_length;
[a931b7b]1422 hash_func_t mic_hash;
[8a64320e]1423 if (ccmp_used)
[a931b7b]1424 mic_hash = HASH_SHA1;
[8a64320e]1425 else
[a931b7b]1426 mic_hash = HASH_MD5;
[a35b458]1427
[8a64320e]1428 if (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP)
[a931b7b]1429 ptk_key_length = IEEE80211_PTK_CCMP_LENGTH;
[8a64320e]1430 else
[a931b7b]1431 ptk_key_length = IEEE80211_PTK_TKIP_LENGTH;
[a35b458]1432
[8a64320e]1433 if (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP)
[a931b7b]1434 gtk_key_length = IEEE80211_GTK_CCMP_LENGTH;
[8a64320e]1435 else
[a931b7b]1436 gtk_key_length = IEEE80211_GTK_TKIP_LENGTH;
[a35b458]1437
[8a64320e]1438 size_t output_size =
1439 sizeof(eth_header_t) +
1440 sizeof(ieee80211_eapol_key_frame_t);
[a35b458]1441
[8a64320e]1442 if (!(uint16_t_be2host(key_frame->key_info) &
1443 IEEE80211_EAPOL_KEY_KEYINFO_MIC))
[a931b7b]1444 output_size += auth_link->auth_ie_len;
[a35b458]1445
[1dcc0b9]1446 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
1447 nic_address_t nic_address;
1448 nic_query_address(nic, &nic_address);
[a35b458]1449
[1dcc0b9]1450 void *output_buffer = malloc(output_size);
[8a64320e]1451 if (!output_buffer)
1452 return ENOMEM;
[a35b458]1453
[1dcc0b9]1454 memset(output_buffer, 0, output_size);
[a35b458]1455
[1dcc0b9]1456 /* Setup ethernet header. */
1457 eth_header_t *eth_header = (eth_header_t *) output_buffer;
[8a64320e]1458 memcpy(eth_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
[1dcc0b9]1459 memcpy(eth_header->src_addr, nic_address.address, ETH_ADDR);
1460 eth_header->proto = host2uint16_t_be(ETH_TYPE_PAE);
[a35b458]1461
[1dcc0b9]1462 ieee80211_eapol_key_frame_t *output_key_frame =
[8a64320e]1463 (ieee80211_eapol_key_frame_t *)
1464 (output_buffer + sizeof(eth_header_t));
[a35b458]1465
[1dcc0b9]1466 /* Copy content of incoming EAPOL-Key frame. */
[8a64320e]1467 memcpy((void *) output_key_frame, buffer,
1468 sizeof(ieee80211_eapol_key_frame_t));
[a35b458]1469
[1dcc0b9]1470 output_key_frame->proto_version = 0x1;
1471 output_key_frame->body_length =
[8a64320e]1472 host2uint16_t_be(output_size - sizeof(eth_header_t) - 4);
1473 output_key_frame->key_info &=
1474 ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_ACK);
[a35b458]1475
[8a64320e]1476 if (key_phase) {
1477 output_key_frame->key_info &=
1478 ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA);
1479 output_key_frame->key_info &=
1480 ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_INSTALL);
[1dcc0b9]1481 output_key_frame->key_data_length = 0;
1482 memset(output_key_frame->key_nonce, 0, 32);
1483 memset(output_key_frame->key_mic, 0, 16);
1484 memset(output_key_frame->key_rsc, 0, 8);
1485 memset(output_key_frame->eapol_key_iv, 0, 16);
[a35b458]1486
[1dcc0b9]1487 /* Derive GTK and save it. */
[8a64320e]1488 if (final_phase) {
1489 uint16_t key_data_length =
1490 uint16_t_be2host(key_frame->key_data_length);
[a931b7b]1491 uint8_t key_data[key_data_length];
[8a64320e]1492 uint8_t *data_ptr = (uint8_t *)
1493 (buffer + sizeof(ieee80211_eapol_key_frame_t));
[a35b458]1494
[b7fd2a0]1495 errno_t rc;
[a931b7b]1496 uint8_t work_key[32];
[a35b458]1497
[8a64320e]1498 if (ccmp_used) {
1499 rc = ieee80211_aes_key_unwrap(ptk + KEK_OFFSET,
1500 data_ptr, key_data_length, key_data);
[a931b7b]1501 } else {
1502 memcpy(work_key, key_frame->eapol_key_iv, 16);
1503 memcpy(work_key + 16, ptk + KEK_OFFSET, 16);
[8a64320e]1504 rc = ieee80211_rc4_key_unwrap(work_key,
1505 data_ptr, key_data_length, key_data);
[a931b7b]1506 }
[a35b458]1507
[8a64320e]1508 if (rc == EOK) {
[09044cb]1509 uint8_t *key_data_ptr = old_wpa ? key_data :
[8a64320e]1510 ieee80211_process_ies(ieee80211_dev,
1511 NULL, key_data, key_data_length);
[a35b458]1512
[8a64320e]1513 if (key_data_ptr) {
[09044cb]1514 uint8_t *key_ptr;
[a35b458]1515
[8a64320e]1516 if (old_wpa)
[09044cb]1517 key_ptr = key_data_ptr;
[8a64320e]1518 else {
[09044cb]1519 gtk_id = *key_data_ptr & 0x3;
1520 key_ptr = key_data_ptr + 2;
1521 }
[a35b458]1522
[a931b7b]1523 memcpy(gtk, key_ptr, gtk_key_length);
1524 handshake_done = true;
1525 }
[1dcc0b9]1526 }
1527 }
1528 } else {
[8a64320e]1529 output_key_frame->key_info |=
1530 host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_MIC);
[1dcc0b9]1531 output_key_frame->key_data_length =
[8a64320e]1532 host2uint16_t_be(auth_link->auth_ie_len);
1533 memcpy((void *) output_key_frame +
1534 sizeof(ieee80211_eapol_key_frame_t),
1535 auth_link->auth_ie, auth_link->auth_ie_len);
[a35b458]1536
[1dcc0b9]1537 /* Compute PMK. */
1538 uint8_t pmk[PBKDF2_KEY_LENGTH];
1539 pbkdf2((uint8_t *) ieee80211_dev->bssid_info.password,
[8a64320e]1540 str_size(ieee80211_dev->bssid_info.password),
1541 (uint8_t *) auth_data->ssid,
1542 str_size(auth_data->ssid), pmk);
[a35b458]1543
[1dcc0b9]1544 uint8_t *anonce = key_frame->key_nonce;
[a35b458]1545
[1dcc0b9]1546 /* Generate SNONCE. */
1547 uint8_t snonce[32];
1548 rnd_sequence(snonce, 32);
[a35b458]1549
[1dcc0b9]1550 memcpy(output_key_frame->key_nonce, snonce, 32);
[a35b458]1551
[1dcc0b9]1552 uint8_t *dest_addr = eth_header->dest_addr;
1553 uint8_t *src_addr = eth_header->src_addr;
[a35b458]1554
[1dcc0b9]1555 /* Derive PTK and save it. */
1556 uint8_t crypt_data[PRF_CRYPT_DATA_LENGTH];
[8a64320e]1557 memcpy(crypt_data,
1558 min_sequence(dest_addr, src_addr, ETH_ADDR), ETH_ADDR);
1559 memcpy(crypt_data + ETH_ADDR,
1560 max_sequence(dest_addr, src_addr, ETH_ADDR), ETH_ADDR);
[3bacee1]1561 memcpy(crypt_data + 2 * ETH_ADDR,
[8a64320e]1562 min_sequence(anonce, snonce, 32), 32);
[3bacee1]1563 memcpy(crypt_data + 2 * ETH_ADDR + 32,
[8a64320e]1564 max_sequence(anonce, snonce, 32), 32);
[a931b7b]1565 ieee80211_prf(pmk, crypt_data, ptk, ptk_key_length);
[1dcc0b9]1566 }
[a35b458]1567
[1dcc0b9]1568 /* Compute MIC of key frame data from KCK part of PTK. */
[a931b7b]1569 uint8_t mic[mic_hash];
[8a64320e]1570 hmac(ptk, 16, (uint8_t *) output_key_frame,
1571 output_size - sizeof(eth_header_t), mic, mic_hash);
[a35b458]1572
[1dcc0b9]1573 memcpy(output_key_frame->key_mic, mic, 16);
[a35b458]1574
[1dcc0b9]1575 ieee80211_send_frame(nic, output_buffer, output_size);
[a35b458]1576
[1dcc0b9]1577 free(output_buffer);
[a35b458]1578
[a931b7b]1579 ieee80211_key_config_t key_config;
[a35b458]1580
[a931b7b]1581 /* Insert Pairwise key. */
[8a64320e]1582 if ((key_phase && old_wpa) || (final_phase && !old_wpa)) {
[1dcc0b9]1583 key_config.suite = auth_data->security.pair_alg;
1584 key_config.flags =
[8a64320e]1585 IEEE80211_KEY_FLAG_TYPE_PAIRWISE;
1586 memcpy(key_config.data,
1587 ptk + TK_OFFSET, ptk_key_length - TK_OFFSET);
[a35b458]1588
[1dcc0b9]1589 ieee80211_dev->ops->key_config(ieee80211_dev,
[8a64320e]1590 &key_config, true);
[a931b7b]1591 }
[a35b458]1592
[a931b7b]1593 /* Insert Group key. */
[8a64320e]1594 if (final_phase) {
[09044cb]1595 key_config.id = gtk_id;
[1dcc0b9]1596 key_config.suite = auth_data->security.group_alg;
[8a64320e]1597 key_config.flags = IEEE80211_KEY_FLAG_TYPE_GROUP;
[1dcc0b9]1598 memcpy(key_config.data, gtk, gtk_key_length);
[a35b458]1599
[1dcc0b9]1600 ieee80211_dev->ops->key_config(ieee80211_dev,
[8a64320e]1601 &key_config, true);
[a931b7b]1602 }
[a35b458]1603
[a931b7b]1604 /* Signal successful handshake completion. */
[8a64320e]1605 if (handshake_done) {
[1dcc0b9]1606 fibril_mutex_lock(&ieee80211_dev->gen_mutex);
1607 fibril_condvar_signal(&ieee80211_dev->gen_cond);
1608 fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
1609 }
[a35b458]1610
[1dcc0b9]1611 return EOK;
1612}
1613
[b7fd2a0]1614static errno_t ieee80211_process_eapol_frame(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1615 void *buffer, size_t buffer_size)
[1dcc0b9]1616{
[8a64320e]1617 ieee80211_eapol_key_frame_t *key_frame =
1618 (ieee80211_eapol_key_frame_t *) buffer;
[a35b458]1619
[8a64320e]1620 if (ieee80211_is_eapol_key_frame(key_frame))
[1dcc0b9]1621 return ieee80211_process_4way_handshake(ieee80211_dev, buffer,
[8a64320e]1622 buffer_size);
[a35b458]1623
[1dcc0b9]1624 return EOK;
1625}
1626
[8a64320e]1627/** Process data frame.
1628 *
[1dcc0b9]1629 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1630 * @param buffer Data buffer starting with IEEE 802.11 data header.
1631 * @param buffer_size Size of buffer.
1632 *
[cde999a]1633 * @return EOK if succeed, error code otherwise.
[8a64320e]1634 *
[1dcc0b9]1635 */
[b7fd2a0]1636static errno_t ieee80211_process_data(ieee80211_dev_t *ieee80211_dev,
[8a64320e]1637 void *buffer, size_t buffer_size)
[1dcc0b9]1638{
[8a64320e]1639 ieee80211_data_header_t *data_header =
1640 (ieee80211_data_header_t *) buffer;
[a35b458]1641
[8a64320e]1642 if (ieee80211_has_data_frame(data_header->frame_ctrl)) {
[1dcc0b9]1643 nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
[8a64320e]1644 size_t strip_length = sizeof(ieee80211_data_header_t) +
1645 ARRAY_SIZE(rfc1042_header);
[a35b458]1646
[a931b7b]1647 /* TODO: Different by used security alg. */
1648 /* TODO: Trim frame by used security alg. */
1649 // TODO: Distinguish used key (pair/group) by dest address ?
[8a64320e]1650 if (ieee80211_is_encrypted_frame(data_header->frame_ctrl))
[1dcc0b9]1651 strip_length += 8;
[a35b458]1652
[1dcc0b9]1653 /* Process 4-way authentication handshake. */
1654 uint16_t *proto = (uint16_t *) (buffer + strip_length);
[8a64320e]1655 if (uint16_t_be2host(*proto) == ETH_TYPE_PAE)
[1dcc0b9]1656 return ieee80211_process_eapol_frame(ieee80211_dev,
[8a64320e]1657 buffer + strip_length + sizeof(uint16_t),
1658 buffer_size - strip_length - sizeof(uint16_t));
[a35b458]1659
[8a64320e]1660 /*
1661 * Note: ETH protocol ID is already there, so we don't create
1662 * whole ETH header.
1663 */
1664 size_t frame_size =
1665 buffer_size - strip_length + sizeof(eth_header_t) - 2;
[1dcc0b9]1666 nic_frame_t *frame = nic_alloc_frame(nic, frame_size);
[a35b458]1667
[3bacee1]1668 if (frame == NULL)
[1dcc0b9]1669 return ENOMEM;
[a35b458]1670
[8a64320e]1671 uint8_t *src_addr =
1672 ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
1673 data_header->address3 : data_header->address2;
1674 uint8_t *dest_addr =
1675 ieee80211_is_tods_frame(data_header->frame_ctrl) ?
1676 data_header->address3 : data_header->address1;
[a35b458]1677
[8a64320e]1678 eth_header_t *eth_header = (eth_header_t *) frame->data;
[1dcc0b9]1679 memcpy(eth_header->src_addr, src_addr, ETH_ADDR);
1680 memcpy(eth_header->dest_addr, dest_addr, ETH_ADDR);
[a35b458]1681
[8a64320e]1682 memcpy(frame->data + sizeof(eth_header_t) - 2,
1683 buffer + strip_length, buffer_size - strip_length);
[a35b458]1684
[1dcc0b9]1685 nic_received_frame(nic, frame);
[59fa7ab]1686 }
[a35b458]1687
[59fa7ab]1688 return EOK;
1689}
1690
[8a64320e]1691/** IEEE 802.11 RX frames handler.
1692 *
[59fa7ab]1693 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
[8a64320e]1694 * @param buffer Buffer with data.
1695 * @param buffer_size Size of buffer.
1696 *
[cde999a]1697 * @return EOK if succeed, error code otherwise.
[8a64320e]1698 *
[59fa7ab]1699 */
[b7fd2a0]1700errno_t ieee80211_rx_handler(ieee80211_dev_t *ieee80211_dev, void *buffer,
[8a64320e]1701 size_t buffer_size)
[59fa7ab]1702{
1703 uint16_t frame_ctrl = *((uint16_t *) buffer);
[a35b458]1704
[8a64320e]1705 if (ieee80211_is_mgmt_frame(frame_ctrl)) {
[59fa7ab]1706 ieee80211_mgmt_header_t *mgmt_header =
[8a64320e]1707 (ieee80211_mgmt_header_t *) buffer;
[a35b458]1708
[8a64320e]1709 if ((ieee80211_is_probe_response_frame(mgmt_header->frame_ctrl)) ||
1710 (ieee80211_is_beacon_frame(mgmt_header->frame_ctrl)))
[59fa7ab]1711 return ieee80211_process_probe_response(ieee80211_dev,
[8a64320e]1712 mgmt_header, buffer_size);
[a35b458]1713
[8a64320e]1714 if (ieee80211_is_auth_frame(mgmt_header->frame_ctrl))
[1dcc0b9]1715 return ieee80211_process_auth_response(ieee80211_dev,
[8a64320e]1716 mgmt_header);
[a35b458]1717
[8a64320e]1718 if (ieee80211_is_assoc_response_frame(mgmt_header->frame_ctrl))
[1dcc0b9]1719 return ieee80211_process_assoc_response(ieee80211_dev,
[8a64320e]1720 mgmt_header);
1721 } else if (ieee80211_is_data_frame(frame_ctrl))
1722 return ieee80211_process_data(ieee80211_dev, buffer,
1723 buffer_size);
[a35b458]1724
[59fa7ab]1725 return EOK;
1726}
1727
1728/** @}
1729 */
Note: See TracBrowser for help on using the repository browser.