Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ieee80211/src/ieee80211.c

    r1b20da0 ra35b458  
    7272{
    7373        frame_ctrl = uint16_t_le2host(frame_ctrl);
    74        
     74
    7575        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
    7676            IEEE80211_DATA_FRAME;
     
    8787{
    8888        frame_ctrl = uint16_t_le2host(frame_ctrl);
    89        
     89
    9090        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
    9191            IEEE80211_MGMT_FRAME;
     
    102102{
    103103        frame_ctrl = uint16_t_le2host(frame_ctrl);
    104        
     104
    105105        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
    106106            IEEE80211_MGMT_BEACON_FRAME;
     
    117117{
    118118        frame_ctrl = uint16_t_le2host(frame_ctrl);
    119        
     119
    120120        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
    121121            IEEE80211_MGMT_PROBE_RESP_FRAME;
     
    132132{
    133133        frame_ctrl = uint16_t_le2host(frame_ctrl);
    134        
     134
    135135        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
    136136            IEEE80211_MGMT_AUTH_FRAME;
     
    147147{
    148148        frame_ctrl = uint16_t_le2host(frame_ctrl);
    149        
     149
    150150        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
    151151            IEEE80211_MGMT_ASSOC_RESP_FRAME;
     
    162162{
    163163        frame_ctrl = uint16_t_le2host(frame_ctrl);
    164        
     164
    165165        return (frame_ctrl & IEEE80211_FRAME_CTRL_TODS);
    166166}
     
    176176{
    177177        frame_ctrl = uint16_t_le2host(frame_ctrl);
    178        
     178
    179179        return (frame_ctrl & IEEE80211_FRAME_CTRL_FROMDS);
    180180}
     
    190190{
    191191        frame_ctrl = uint16_t_le2host(frame_ctrl);
    192        
     192
    193193        return (frame_ctrl & (IEEE80211_FRAME_CTRL_FRAME_TYPE | 0x40)) ==
    194194            IEEE80211_DATA_FRAME;
     
    205205{
    206206        frame_ctrl = uint16_t_le2host(frame_ctrl);
    207        
     207
    208208        return (frame_ctrl & IEEE80211_FRAME_CTRL_PROTECTED);
    209209}
     
    233233        uint16_t ret_val = ieee80211_dev->sequence_number;
    234234        ieee80211_dev->sequence_number += (1 << 4);
    235        
     235
    236236        return ret_val;
    237237}
     
    286286        ieee80211_operating_mode_t op_mode = ieee80211_dev->current_op_mode;
    287287        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    288        
     288
    289289        return op_mode;
    290290}
     
    302302        uint16_t current_freq = ieee80211_dev->current_freq;
    303303        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    304        
     304
    305305        return current_freq;
    306306}
     
    318318{
    319319        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    320        
     320
    321321        if (bssid) {
    322322                ieee80211_scan_result_link_t *res_link =
    323323                    ieee80211_dev->bssid_info.res_link;
    324                
     324
    325325                if (res_link) {
    326326                        memcpy(bssid, &res_link->scan_result.bssid,
     
    333333                }
    334334        }
    335        
     335
    336336        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    337337}
     
    349349        uint16_t aid = ieee80211_dev->bssid_info.aid;
    350350        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    351        
     351
    352352        return aid;
    353353}
     
    367367        int suite = auth_link->scan_result.security.pair_alg;
    368368        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    369        
     369
    370370        return suite;
    371371}
     
    384384            ieee80211_dev->current_auth_phase == IEEE80211_AUTH_CONNECTED;
    385385        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    386        
     386
    387387        return conn_state;
    388388}
     
    401401        ieee80211_auth_phase_t conn_state = ieee80211_dev->current_auth_phase;
    402402        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    403        
     403
    404404        return conn_state;
    405405}
     
    418418        ieee80211_dev->pending_conn_req = false;
    419419        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    420        
     420
    421421        return conn_request;
    422422}
     
    462462        bool ready_state = ieee80211_dev->ready;
    463463        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    464        
     464
    465465        return ready_state;
    466466}
     
    484484        bool using_key = ieee80211_dev->using_hw_key;
    485485        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    486        
     486
    487487        return using_key;
    488488}
     
    499499{
    500500        assert(arg);
    501        
     501
    502502        ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *) arg;
    503        
     503
    504504        while (true) {
    505505                ieee80211_dev->ops->scan(ieee80211_dev);
    506506                async_usleep(SCAN_PERIOD_USEC);
    507507        }
    508        
     508
    509509        return EOK;
    510510}
     
    521521        nic_t *nic_data = nic_get_from_ddf_fun(fun);
    522522        ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
    523        
     523
    524524        if (ieee80211_dev->started)
    525525                return EOK;
    526        
     526
    527527        ieee80211_dev->started = true;
    528        
     528
    529529        errno_t rc = ieee80211_dev->ops->start(ieee80211_dev);
    530530        if (rc != EOK)
    531531                return rc;
    532        
     532
    533533        /* Add scanning fibril. */
    534534        fid_t fibril = fibril_create(ieee80211_scan, ieee80211_dev);
    535535        if (fibril == 0)
    536536                return ENOMEM;
    537        
     537
    538538        fibril_add_ready(fibril);
    539        
     539
    540540        return EOK;
    541541}
     
    552552        ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
    553553            nic_get_specific(nic);
    554        
     554
    555555        ieee80211_auth_phase_t auth_phase =
    556556            ieee80211_get_auth_phase(ieee80211_dev);
     
    558558            (auth_phase != IEEE80211_AUTH_CONNECTED))
    559559                return;
    560        
     560
    561561        ieee80211_scan_result_t *auth_data =
    562562            &ieee80211_dev->bssid_info.res_link->scan_result;
    563        
     563
    564564        /* We drop part of IEEE 802.3 ethernet header. */
    565565        size_t drop_bytes = sizeof(eth_header_t) - 2;
    566        
     566
    567567        size_t complete_size = (size - drop_bytes) +
    568568            sizeof(ieee80211_data_header_t) +
    569569            ARRAY_SIZE(rfc1042_header);
    570        
     570
    571571        /* Init crypto data. */
    572572        bool add_mic = false;
     
    575575        uint8_t head_data[head_space];
    576576        memset(head_data, 0, head_space);
    577        
     577
    578578        // TODO: Distinguish used key (pair/group) by dest address ?
    579579        if (ieee80211_query_using_key(ieee80211_dev)) {
     
    592592                        break;
    593593                }
    594                
     594
    595595                crypto = uint16_t_le2host(IEEE80211_FRAME_CTRL_PROTECTED);
    596596        }
    597        
     597
    598598        complete_size += head_space + mic_space;
    599        
     599
    600600        void *complete_buffer = malloc(complete_size);
    601601        if (!complete_buffer)
    602602                return;
    603        
     603
    604604        memset(complete_buffer, 0, complete_size);
    605        
     605
    606606        if (head_space)
    607607                memcpy(complete_buffer + sizeof(ieee80211_data_header_t),
    608608                    head_data, head_space);
    609        
     609
    610610        memcpy(complete_buffer + sizeof(ieee80211_data_header_t) + head_space,
    611611            rfc1042_header, ARRAY_SIZE(rfc1042_header));
    612        
     612
    613613        memcpy(complete_buffer + sizeof(ieee80211_data_header_t) +
    614614            ARRAY_SIZE(rfc1042_header) + head_space,
    615615            data + drop_bytes, size - drop_bytes);
    616        
     616
    617617        ieee80211_data_header_t *data_header =
    618618            (ieee80211_data_header_t *) complete_buffer;
     
    623623            crypto;
    624624        data_header->seq_ctrl = ieee80211_get_sequence_number(ieee80211_dev);
    625        
     625
    626626        /* BSSID, SA, DA. */
    627627        memcpy(data_header->address1, auth_data->bssid.address, ETH_ADDR);
    628628        memcpy(data_header->address2, data + ETH_ADDR, ETH_ADDR);
    629629        memcpy(data_header->address3, data, ETH_ADDR);
    630        
     630
    631631        if (add_mic) {
    632632                size_t size_wo_mic = complete_size - MIC_LENGTH;
     
    636636                    complete_buffer + size_wo_mic);
    637637        }
    638        
     638
    639639        ieee80211_dev->ops->tx_handler(ieee80211_dev,
    640640            complete_buffer, complete_size);
    641        
     641
    642642        free(complete_buffer);
    643643}
     
    662662                if (!ieee80211_ops->start)
    663663                        ieee80211_ops->start = ieee80211_start_impl;
    664                
     664
    665665                if (!ieee80211_ops->tx_handler)
    666666                        ieee80211_ops->tx_handler = ieee80211_tx_handler_impl;
    667                
     667
    668668                if (!ieee80211_ops->set_freq)
    669669                        ieee80211_ops->set_freq = ieee80211_set_freq_impl;
    670                
     670
    671671                if (!ieee80211_ops->bssid_change)
    672672                        ieee80211_ops->bssid_change = ieee80211_bssid_change_impl;
    673                
     673
    674674                if (!ieee80211_ops->key_config)
    675675                        ieee80211_ops->key_config = ieee80211_key_config_impl;
    676                
     676
    677677                if (!ieee80211_ops->scan)
    678678                        ieee80211_ops->scan = ieee80211_scan_impl;
    679679        } else
    680680                return EINVAL;
    681        
     681
    682682        ieee80211_dev->ops = ieee80211_ops;
    683        
     683
    684684        if (ieee80211_iface) {
    685685                if (nic_dev_ops)
     
    687687                                nic_dev_ops->interfaces[IEEE80211_DEV_IFACE] =
    688688                                    ieee80211_iface;
    689                
     689
    690690                if (!ieee80211_iface->get_scan_results)
    691691                        ieee80211_iface->get_scan_results =
    692692                            ieee80211_get_scan_results_impl;
    693                
     693
    694694                if (!ieee80211_iface->connect)
    695695                        ieee80211_iface->connect = ieee80211_connect_impl;
    696                
     696
    697697                if (!ieee80211_iface->disconnect)
    698698                        ieee80211_iface->disconnect = ieee80211_disconnect_impl;
    699699        } else
    700700                return EINVAL;
    701        
     701
    702702        if (nic_dev_ops) {
    703703                if (!nic_dev_ops->open)
     
    705705        } else
    706706                return EINVAL;
    707        
     707
    708708        ieee80211_dev->iface = ieee80211_iface;
    709        
     709
    710710        nic_driver_implement(NULL, nic_dev_ops, nic_iface);
    711        
     711
    712712        return EOK;
    713713}
     
    740740        ieee80211_dev->current_op_mode = IEEE80211_OPMODE_STATION;
    741741        ieee80211_dev->current_auth_phase = IEEE80211_AUTH_DISCONNECTED;
    742        
     742
    743743        memcpy(ieee80211_dev->bssid_mask.address, ieee80211_broadcast_mac_addr,
    744744            ETH_ADDR);
    745        
     745
    746746        ieee80211_scan_result_list_init(&ieee80211_dev->ap_list);
    747        
     747
    748748        fibril_mutex_initialize(&ieee80211_dev->scan_mutex);
    749749        fibril_mutex_initialize(&ieee80211_dev->gen_mutex);
    750750        fibril_condvar_initialize(&ieee80211_dev->gen_cond);
    751        
     751
    752752        /* Bind NIC to device */
    753753        nic_t *nic = nic_create_and_bind(ddf_dev);
    754754        if (!nic)
    755755                return ENOMEM;
    756        
     756
    757757        nic_set_specific(nic, ieee80211_dev);
    758        
     758
    759759        return EOK;
    760760}
     
    780780        if (rc != EOK)
    781781                return rc;
    782        
     782
    783783        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    784        
     784
    785785        /* TODO: Set NIC handlers here. */
    786786        nic_set_send_frame_handler(nic, ieee80211_send_frame);
    787        
     787
    788788        ddf_fun_t *fun = ddf_fun_create(ieee80211_dev->ddf_dev, fun_exposed,
    789789            "port0");
    790790        if (fun == NULL)
    791791                return EINVAL;
    792        
     792
    793793        nic_set_ddf_fun(nic, fun);
    794794        ddf_fun_set_ops(fun, ieee80211_nic_dev_ops);
    795        
     795
    796796        rc = ddf_fun_bind(fun);
    797797        if (rc != EOK) {
     
    799799                return rc;
    800800        }
    801        
     801
    802802        rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
    803803        if (rc != EOK) {
     
    805805                return rc;
    806806        }
    807        
     807
    808808        rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_IEEE80211);
    809809        if (rc != EOK) {
     
    811811                return rc;
    812812        }
    813        
     813
    814814        return EOK;
    815815}
     
    832832        ieee80211_ie_header_t *header =
    833833            (ieee80211_ie_header_t *) *ie_header;
    834        
     834
    835835        header->element_id = id;
    836836        header->length = length;
    837        
     837
    838838        memcpy(*ie_header + sizeof(ieee80211_ie_header_t), data, length);
    839        
     839
    840840        *ie_header = (void *) ((void *) header +
    841841            sizeof(ieee80211_ie_header_t) + length);
     
    855855        nic_address_t nic_address;
    856856        nic_query_address(nic, &nic_address);
    857        
     857
    858858        size_t ssid_data_size = (ssid != NULL) ? str_size(ssid) : 0;
    859859        size_t channel_data_size = 1;
    860        
     860
    861861        uint8_t channel =
    862862            ieee80211_freq_to_channel(ieee80211_dev->current_freq);
    863        
     863
    864864        /*
    865865         * 4 headers - (ssid, rates, ext rates, current channel)
     
    871871            IEEE80211_DATA_RATES_SIZE + IEEE80211_EXT_DATA_RATES_SIZE +
    872872            channel_data_size;
    873        
     873
    874874        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) + payload_size;
    875875        void *buffer = malloc(buffer_size);
    876876        if (!buffer)
    877877                return ENOMEM;
    878        
     878
    879879        memset(buffer, 0, buffer_size);
    880        
     880
    881881        ieee80211_mgmt_header_t *mgmt_header =
    882882            (ieee80211_mgmt_header_t *) buffer;
    883        
     883
    884884        mgmt_header->frame_ctrl =
    885885            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     
    890890        mgmt_header->seq_ctrl =
    891891            host2uint16_t_le(ieee80211_get_sequence_number(ieee80211_dev));
    892        
     892
    893893        /* Jump to payload. */
    894894        void *it = (void *) buffer + sizeof(ieee80211_mgmt_header_t);
     
    902902        ieee80211_prepare_ie_header(&it, IEEE80211_CHANNEL_IE,
    903903            channel_data_size, (void *) &channel);
    904        
     904
    905905        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
    906        
     906
    907907        free(buffer);
    908        
     908
    909909        return EOK;
    910910}
     
    922922        nic_address_t nic_address;
    923923        nic_query_address(nic, &nic_address);
    924        
     924
    925925        ieee80211_scan_result_t *auth_data =
    926926            &ieee80211_dev->bssid_info.res_link->scan_result;
    927        
     927
    928928        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
    929929            sizeof(ieee80211_auth_body_t);
    930        
     930
    931931        void *buffer = malloc(buffer_size);
    932932        if (!buffer)
    933933                return ENOMEM;
    934        
     934
    935935        memset(buffer, 0, buffer_size);
    936        
     936
    937937        ieee80211_mgmt_header_t *mgmt_header =
    938938            (ieee80211_mgmt_header_t *) buffer;
    939        
     939
    940940        mgmt_header->frame_ctrl =
    941941            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     
    944944        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
    945945        memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
    946        
     946
    947947        ieee80211_auth_body_t *auth_body =
    948948            (ieee80211_auth_body_t *)
     
    950950        auth_body->auth_alg = host2uint16_t_le(0);
    951951        auth_body->auth_trans_no = host2uint16_t_le(1);
    952        
     952
    953953        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
    954        
     954
    955955        free(buffer);
    956        
     956
    957957        return EOK;
    958958}
     
    972972        nic_address_t nic_address;
    973973        nic_query_address(nic, &nic_address);
    974        
     974
    975975        ieee80211_scan_result_link_t *auth_link =
    976976            ieee80211_dev->bssid_info.res_link;
    977        
     977
    978978        ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
    979        
     979
    980980        size_t ssid_data_size = str_size(auth_data->ssid);
    981        
     981
    982982        size_t payload_size =
    983983            sizeof(ieee80211_ie_header_t) * 3 +
     
    985985            IEEE80211_DATA_RATES_SIZE +
    986986            IEEE80211_EXT_DATA_RATES_SIZE;
    987        
     987
    988988        size_t buffer_size =
    989989            sizeof(ieee80211_mgmt_header_t) +
    990990            sizeof(ieee80211_assoc_req_body_t) +
    991991            payload_size;
    992        
     992
    993993        if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
    994994            (auth_data->security.type == IEEE80211_SECURITY_WPA2))
    995995                buffer_size += auth_link->auth_ie_len;
    996        
     996
    997997        void *buffer = malloc(buffer_size);
    998998        if (!buffer)
    999999                return ENOMEM;
    1000        
     1000
    10011001        memset(buffer, 0, buffer_size);
    1002        
     1002
    10031003        ieee80211_mgmt_header_t *mgmt_header =
    10041004            (ieee80211_mgmt_header_t *) buffer;
    1005        
     1005
    10061006        mgmt_header->frame_ctrl =
    10071007            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     
    10101010        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
    10111011        memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
    1012        
     1012
    10131013        ieee80211_assoc_req_body_t *assoc_body =
    10141014            (ieee80211_assoc_req_body_t *)
    10151015            (buffer + sizeof(ieee80211_mgmt_header_t));
    10161016        assoc_body->listen_interval = host2uint16_t_le(1);
    1017        
     1017
    10181018        /* Jump to payload. */
    10191019        void *it = buffer + sizeof(ieee80211_mgmt_header_t) +
     
    10261026            IEEE80211_EXT_DATA_RATES_SIZE,
    10271027            (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
    1028        
     1028
    10291029        if (auth_data->security.type != IEEE80211_SECURITY_OPEN)
    10301030                assoc_body->capability |= host2uint16_t_le(CAP_SECURITY);
    1031        
     1031
    10321032        if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
    10331033            (auth_data->security.type == IEEE80211_SECURITY_WPA2))
    10341034                memcpy(it, auth_link->auth_ie, auth_link->auth_ie_len);
    1035        
     1035
    10361036        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
    1037        
     1037
    10381038        /*
    10391039         * Save password to be used in eventual authentication handshake.
     
    10421042        memcpy(ieee80211_dev->bssid_info.password, password,
    10431043            str_size(password));
    1044        
     1044
    10451045        free(buffer);
    1046        
     1046
    10471047        return EOK;
    10481048}
     
    10611061        ieee80211_scan_result_t *auth_data =
    10621062            &ieee80211_dev->bssid_info.res_link->scan_result;
    1063        
     1063
    10641064        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    10651065        nic_address_t nic_address;
    10661066        nic_query_address(nic, &nic_address);
    1067        
     1067
    10681068        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
    10691069            sizeof(ieee80211_deauth_body_t);
    1070        
     1070
    10711071        void *buffer = malloc(buffer_size);
    10721072        if (!buffer)
    10731073                return ENOMEM;
    1074        
     1074
    10751075        memset(buffer, 0, buffer_size);
    1076        
     1076
    10771077        ieee80211_mgmt_header_t *mgmt_header =
    10781078            (ieee80211_mgmt_header_t *) buffer;
    1079        
     1079
    10801080        mgmt_header->frame_ctrl =
    10811081            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     
    10841084        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
    10851085        memcpy(mgmt_header->bssid, auth_data->bssid.address, ETH_ADDR);
    1086        
     1086
    10871087        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
    1088        
     1088
    10891089        free(buffer);
    1090        
     1090
    10911091        ieee80211_dev->bssid_info.res_link = NULL;
    10921092        ieee80211_dev->ops->bssid_change(ieee80211_dev, false);
    1093        
     1093
    10941094        if (ieee80211_query_using_key(ieee80211_dev))
    10951095                ieee80211_dev->ops->key_config(ieee80211_dev, NULL, false);
    1096        
     1096
    10971097        ieee80211_set_auth_phase(ieee80211_dev, IEEE80211_AUTH_DISCONNECTED);
    1098        
     1098
    10991099        return EOK;
    11001100}
     
    11041104{
    11051105        uint8_t *it = (uint8_t *) buffer;
    1106        
     1106
    11071107        uint16_t *version = (uint16_t *) it;
    11081108        if (uint16_t_le2host(*version) != 0x1) {
     
    11101110                return;
    11111111        }
    1112        
     1112
    11131113        it += sizeof(uint16_t);
    1114        
     1114
    11151115        uint32_t group_cipher = *(it + 3);
    11161116        switch (group_cipher) {
     
    11261126                ap_data->scan_result.security.group_alg = -1;
    11271127        }
    1128        
     1128
    11291129        it += 4 * sizeof(uint8_t);
    1130        
     1130
    11311131        uint16_t *pairwise_count = (uint16_t *) it;
    11321132        uint32_t pairwise_cipher = *(it + sizeof(uint16_t) + 3);
     
    11431143                ap_data->scan_result.security.pair_alg = -1;
    11441144        }
    1145        
     1145
    11461146        it += 2 * sizeof(uint16_t) +
    11471147            uint16_t_le2host(*pairwise_count) * sizeof(uint32_t);
    1148        
     1148
    11491149        uint32_t auth_suite = *(it + 3);
    11501150        switch (auth_suite) {
     
    11671167        ap_data->auth_ie_len = ie_header->length +
    11681168            sizeof(ieee80211_ie_header_t);
    1169        
     1169
    11701170        memcpy(ap_data->auth_ie, it, ap_data->auth_ie_len);
    11711171}
     
    11801180                uint8_t *channel;
    11811181                uint32_t oui;
    1182                
     1182
    11831183                switch (ie_header->element_id) {
    11841184                case IEEE80211_CHANNEL_IE:
    11851185                        if (!ap_data)
    11861186                                break;
    1187                        
     1187
    11881188                        channel = (uint8_t *)
    11891189                            (it + sizeof(ieee80211_ie_header_t));
     
    11931193                        if (!ap_data)
    11941194                                break;
    1195                        
     1195
    11961196                        ap_data->scan_result.security.type =
    11971197                            IEEE80211_SECURITY_WPA2;
     
    12031203                        oui = uint32be_from_seq(it +
    12041204                            sizeof(ieee80211_ie_header_t));
    1205                        
     1205
    12061206                        if (oui == WPA_OUI) {
    12071207                                if (!ap_data)
    12081208                                        break;
    1209                                
     1209
    12101210                                /* Prefering WPA2. */
    12111211                                if (ap_data->scan_result.security.type ==
    12121212                                    IEEE80211_SECURITY_WPA2)
    12131213                                        break;
    1214                                
     1214
    12151215                                ap_data->scan_result.security.type =
    12161216                                    IEEE80211_SECURITY_WPA;
    1217                                
     1217
    12181218                                ieee80211_process_auth_info(ap_data,
    12191219                                    it + sizeof(ieee80211_ie_header_t) +
     
    12261226                        }
    12271227                }
    1228                
     1228
    12291229                it += sizeof(ieee80211_ie_header_t) + ie_header->length;
    12301230        }
    1231        
     1231
    12321232        return NULL;
    12331233}
     
    12461246        ieee80211_beacon_start_t *beacon_body = (ieee80211_beacon_start_t *)
    12471247            ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
    1248        
     1248
    12491249        ieee80211_ie_header_t *ssid_ie_header = (ieee80211_ie_header_t *)
    12501250            ((void *) beacon_body + sizeof(ieee80211_beacon_start_t));
    1251        
     1251
    12521252        /* Not empty SSID. */
    12531253        if (ssid_ie_header->length > 0) {
    12541254                ieee80211_scan_result_list_t *result_list =
    12551255                    &ieee80211_dev->ap_list;
    1256                
     1256
    12571257                uint8_t *ssid_start = (uint8_t *) ((void *) ssid_ie_header +
    12581258                    sizeof(ieee80211_ie_header_t));
    12591259                char ssid[IEEE80211_MAX_SSID_LENGTH];
    1260                
     1260
    12611261                memcpy(ssid, ssid_start, ssid_ie_header->length);
    12621262                ssid[ssid_ie_header->length] = '\0';
    1263                
     1263
    12641264                /* Check whether SSID is already in results. */
    12651265                ieee80211_scan_result_list_foreach(*result_list, result) {
     
    12691269                        }
    12701270                }
    1271                
     1271
    12721272                /* Results are full. */
    12731273                if (result_list->size == IEEE80211_MAX_RESULTS_LENGTH - 1)
    12741274                        return EOK;
    1275                
     1275
    12761276                ieee80211_scan_result_link_t *ap_data =
    12771277                    malloc(sizeof(ieee80211_scan_result_link_t));
    12781278                if (!ap_data)
    12791279                        return ENOMEM;
    1280                
     1280
    12811281                memset(ap_data, 0, sizeof(ieee80211_scan_result_link_t));
    12821282                link_initialize(&ap_data->link);
    1283                
     1283
    12841284                memcpy(ap_data->scan_result.bssid.address,
    12851285                    mgmt_header->bssid, ETH_ADDR);
    12861286                memcpy(ap_data->scan_result.ssid, ssid,
    12871287                    ssid_ie_header->length + 1);
    1288                
     1288
    12891289                if (uint16_t_le2host(beacon_body->capability) & CAP_SECURITY) {
    12901290                        ap_data->scan_result.security.type =
     
    12971297                        ap_data->scan_result.security.group_alg = -1;
    12981298                }
    1299                
     1299
    13001300                void *rest_ies_start = ssid_start + ssid_ie_header->length;
    13011301                size_t rest_buffer_size =
     
    13051305                    sizeof(ieee80211_ie_header_t) -
    13061306                    ssid_ie_header->length;
    1307                
     1307
    13081308                ieee80211_process_ies(ieee80211_dev, ap_data, rest_ies_start,
    13091309                    rest_buffer_size);
    1310                
     1310
    13111311                ap_data->last_beacon = time(NULL);
    1312                
     1312
    13131313                fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
    13141314                ieee80211_scan_result_list_append(result_list, ap_data);
    13151315                fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
    13161316        }
    1317        
     1317
    13181318        return EOK;
    13191319}
     
    13331333            (ieee80211_auth_body_t *)
    13341334            ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
    1335        
     1335
    13361336        if (auth_body->status != 0)
    13371337                ieee80211_set_auth_phase(ieee80211_dev,
     
    13401340                ieee80211_set_auth_phase(ieee80211_dev,
    13411341                    IEEE80211_AUTH_AUTHENTICATED);
    1342        
     1342
    13431343        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    13441344        fibril_condvar_signal(&ieee80211_dev->gen_cond);
    13451345        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    1346        
     1346
    13471347        return EOK;
    13481348}
     
    13621362            (ieee80211_assoc_resp_body_t *) ((void *) mgmt_header +
    13631363            sizeof(ieee80211_mgmt_header_t));
    1364        
     1364
    13651365        if (assoc_resp->status != 0)
    13661366                ieee80211_set_auth_phase(ieee80211_dev,
     
    13731373                ieee80211_dev->ops->bssid_change(ieee80211_dev, true);
    13741374        }
    1375        
     1375
    13761376        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    13771377        fibril_condvar_signal(&ieee80211_dev->gen_cond);
    13781378        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    1379        
     1379
    13801380        return EOK;
    13811381}
     
    13861386        ieee80211_eapol_key_frame_t *key_frame =
    13871387            (ieee80211_eapol_key_frame_t *) buffer;
    1388        
     1388
    13891389        ieee80211_scan_result_link_t *auth_link =
    13901390            ieee80211_dev->bssid_info.res_link;
    1391        
     1391
    13921392        ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
    1393        
     1393
    13941394        /* We don't support 802.1X authentication yet. */
    13951395        if (auth_data->security.auth == IEEE80211_AUTH_AKM_8021X)
    13961396                return ENOTSUP;
    1397        
     1397
    13981398        uint8_t *ptk = ieee80211_dev->bssid_info.ptk;
    13991399        uint8_t *gtk = ieee80211_dev->bssid_info.gtk;
    14001400        uint8_t gtk_id = 1;
    1401        
     1401
    14021402        bool handshake_done = false;
    1403        
     1403
    14041404        bool old_wpa =
    14051405            auth_data->security.type == IEEE80211_SECURITY_WPA;
    1406        
     1406
    14071407        bool key_phase =
    14081408            uint16_t_be2host(key_frame->key_info) &
    14091409            IEEE80211_EAPOL_KEY_KEYINFO_MIC;
    1410        
     1410
    14111411        bool final_phase =
    14121412            uint16_t_be2host(key_frame->key_info) &
    14131413            IEEE80211_EAPOL_KEY_KEYINFO_SECURE;
    1414        
     1414
    14151415        bool ccmp_used =
    14161416            (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP) ||
    14171417            (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP);
    1418        
     1418
    14191419        size_t ptk_key_length, gtk_key_length;
    14201420        hash_func_t mic_hash;
     
    14231423        else
    14241424                mic_hash = HASH_MD5;
    1425        
     1425
    14261426        if (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP)
    14271427                ptk_key_length = IEEE80211_PTK_CCMP_LENGTH;
    14281428        else
    14291429                ptk_key_length = IEEE80211_PTK_TKIP_LENGTH;
    1430        
     1430
    14311431        if (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP)
    14321432                gtk_key_length = IEEE80211_GTK_CCMP_LENGTH;
    14331433        else
    14341434                gtk_key_length = IEEE80211_GTK_TKIP_LENGTH;
    1435        
     1435
    14361436        size_t output_size =
    14371437            sizeof(eth_header_t) +
    14381438            sizeof(ieee80211_eapol_key_frame_t);
    1439        
     1439
    14401440        if (!(uint16_t_be2host(key_frame->key_info) &
    14411441            IEEE80211_EAPOL_KEY_KEYINFO_MIC))
    14421442                output_size += auth_link->auth_ie_len;
    1443        
     1443
    14441444        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    14451445        nic_address_t nic_address;
    14461446        nic_query_address(nic, &nic_address);
    1447        
     1447
    14481448        void *output_buffer = malloc(output_size);
    14491449        if (!output_buffer)
    14501450                return ENOMEM;
    1451        
     1451
    14521452        memset(output_buffer, 0, output_size);
    1453        
     1453
    14541454        /* Setup ethernet header. */
    14551455        eth_header_t *eth_header = (eth_header_t *) output_buffer;
     
    14571457        memcpy(eth_header->src_addr, nic_address.address, ETH_ADDR);
    14581458        eth_header->proto = host2uint16_t_be(ETH_TYPE_PAE);
    1459        
     1459
    14601460        ieee80211_eapol_key_frame_t *output_key_frame =
    14611461            (ieee80211_eapol_key_frame_t *)
    14621462            (output_buffer + sizeof(eth_header_t));
    1463        
     1463
    14641464        /* Copy content of incoming EAPOL-Key frame. */
    14651465        memcpy((void *) output_key_frame, buffer,
    14661466            sizeof(ieee80211_eapol_key_frame_t));
    1467        
     1467
    14681468        output_key_frame->proto_version = 0x1;
    14691469        output_key_frame->body_length =
     
    14711471        output_key_frame->key_info &=
    14721472            ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_ACK);
    1473        
     1473
    14741474        if (key_phase) {
    14751475                output_key_frame->key_info &=
     
    14821482                memset(output_key_frame->key_rsc, 0, 8);
    14831483                memset(output_key_frame->eapol_key_iv, 0, 16);
    1484                
     1484
    14851485                /* Derive GTK and save it. */
    14861486                if (final_phase) {
     
    14901490                        uint8_t *data_ptr = (uint8_t *)
    14911491                            (buffer + sizeof(ieee80211_eapol_key_frame_t));
    1492                        
     1492
    14931493                        errno_t rc;
    14941494                        uint8_t work_key[32];
    1495                        
     1495
    14961496                        if (ccmp_used) {
    14971497                                rc = ieee80211_aes_key_unwrap(ptk + KEK_OFFSET,
     
    15031503                                    data_ptr, key_data_length, key_data);
    15041504                        }
    1505                        
     1505
    15061506                        if (rc == EOK) {
    15071507                                uint8_t *key_data_ptr = old_wpa ? key_data :
    15081508                                    ieee80211_process_ies(ieee80211_dev,
    15091509                                    NULL, key_data, key_data_length);
    1510                                
     1510
    15111511                                if (key_data_ptr) {
    15121512                                        uint8_t *key_ptr;
    1513                                        
     1513
    15141514                                        if (old_wpa)
    15151515                                                key_ptr = key_data_ptr;
     
    15181518                                                key_ptr = key_data_ptr + 2;
    15191519                                        }
    1520                                        
     1520
    15211521                                        memcpy(gtk, key_ptr, gtk_key_length);
    15221522                                        handshake_done = true;
     
    15321532                    sizeof(ieee80211_eapol_key_frame_t),
    15331533                    auth_link->auth_ie, auth_link->auth_ie_len);
    1534                
     1534
    15351535                /* Compute PMK. */
    15361536                uint8_t pmk[PBKDF2_KEY_LENGTH];
     
    15391539                    (uint8_t *) auth_data->ssid,
    15401540                    str_size(auth_data->ssid), pmk);
    1541                
     1541
    15421542                uint8_t *anonce = key_frame->key_nonce;
    1543                
     1543
    15441544                /* Generate SNONCE. */
    15451545                uint8_t snonce[32];
    15461546                rnd_sequence(snonce, 32);
    1547                
     1547
    15481548                memcpy(output_key_frame->key_nonce, snonce, 32);
    1549                
     1549
    15501550                uint8_t *dest_addr = eth_header->dest_addr;
    15511551                uint8_t *src_addr = eth_header->src_addr;
    1552                
     1552
    15531553                /* Derive PTK and save it. */
    15541554                uint8_t crypt_data[PRF_CRYPT_DATA_LENGTH];
     
    15631563                ieee80211_prf(pmk, crypt_data, ptk, ptk_key_length);
    15641564        }
    1565        
     1565
    15661566        /* Compute MIC of key frame data from KCK part of PTK. */
    15671567        uint8_t mic[mic_hash];
    15681568        hmac(ptk, 16, (uint8_t *) output_key_frame,
    15691569            output_size - sizeof(eth_header_t), mic, mic_hash);
    1570        
     1570
    15711571        memcpy(output_key_frame->key_mic, mic, 16);
    1572        
     1572
    15731573        ieee80211_send_frame(nic, output_buffer, output_size);
    1574        
     1574
    15751575        free(output_buffer);
    1576        
     1576
    15771577        ieee80211_key_config_t key_config;
    1578        
     1578
    15791579        /* Insert Pairwise key. */
    15801580        if ((key_phase && old_wpa) || (final_phase && !old_wpa)) {
     
    15841584                memcpy(key_config.data,
    15851585                    ptk + TK_OFFSET, ptk_key_length - TK_OFFSET);
    1586                
     1586
    15871587                ieee80211_dev->ops->key_config(ieee80211_dev,
    15881588                    &key_config, true);
    15891589        }
    1590        
     1590
    15911591        /* Insert Group key. */
    15921592        if (final_phase) {
     
    15951595                key_config.flags = IEEE80211_KEY_FLAG_TYPE_GROUP;
    15961596                memcpy(key_config.data, gtk, gtk_key_length);
    1597                
     1597
    15981598                ieee80211_dev->ops->key_config(ieee80211_dev,
    15991599                    &key_config, true);
    16001600        }
    1601        
     1601
    16021602        /* Signal successful handshake completion. */
    16031603        if (handshake_done) {
     
    16061606                fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    16071607        }
    1608        
     1608
    16091609        return EOK;
    16101610}
     
    16151615        ieee80211_eapol_key_frame_t *key_frame =
    16161616            (ieee80211_eapol_key_frame_t *) buffer;
    1617        
     1617
    16181618        if (ieee80211_is_eapol_key_frame(key_frame))
    16191619                return ieee80211_process_4way_handshake(ieee80211_dev, buffer,
    16201620                    buffer_size);
    1621        
     1621
    16221622        return EOK;
    16231623}
     
    16371637        ieee80211_data_header_t *data_header =
    16381638            (ieee80211_data_header_t *) buffer;
    1639        
     1639
    16401640        if (ieee80211_has_data_frame(data_header->frame_ctrl)) {
    16411641                nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    16421642                size_t strip_length = sizeof(ieee80211_data_header_t) +
    16431643                    ARRAY_SIZE(rfc1042_header);
    1644                
     1644
    16451645                /* TODO: Different by used security alg. */
    16461646                /* TODO: Trim frame by used security alg. */
     
    16481648                if (ieee80211_is_encrypted_frame(data_header->frame_ctrl))
    16491649                        strip_length += 8;
    1650                
     1650
    16511651                /* Process 4-way authentication handshake. */
    16521652                uint16_t *proto = (uint16_t *) (buffer + strip_length);
     
    16551655                            buffer + strip_length + sizeof(uint16_t),
    16561656                            buffer_size - strip_length - sizeof(uint16_t));
    1657                
     1657
    16581658                /*
    16591659                 * Note: ETH protocol ID is already there, so we don't create
     
    16631663                    buffer_size - strip_length + sizeof(eth_header_t) - 2;
    16641664                nic_frame_t *frame = nic_alloc_frame(nic, frame_size);
    1665                
     1665
    16661666                if(frame == NULL)
    16671667                        return ENOMEM;
    1668                
     1668
    16691669                uint8_t *src_addr =
    16701670                    ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
     
    16731673                    ieee80211_is_tods_frame(data_header->frame_ctrl) ?
    16741674                    data_header->address3 : data_header->address1;
    1675                
     1675
    16761676                eth_header_t *eth_header = (eth_header_t *) frame->data;
    16771677                memcpy(eth_header->src_addr, src_addr, ETH_ADDR);
    16781678                memcpy(eth_header->dest_addr, dest_addr, ETH_ADDR);
    1679                
     1679
    16801680                memcpy(frame->data + sizeof(eth_header_t) - 2,
    16811681                    buffer + strip_length, buffer_size - strip_length);
    1682                
     1682
    16831683                nic_received_frame(nic, frame);
    16841684        }
    1685        
     1685
    16861686        return EOK;
    16871687}
     
    17001700{
    17011701        uint16_t frame_ctrl = *((uint16_t *) buffer);
    1702        
     1702
    17031703        if (ieee80211_is_mgmt_frame(frame_ctrl)) {
    17041704                ieee80211_mgmt_header_t *mgmt_header =
    17051705                    (ieee80211_mgmt_header_t *) buffer;
    1706                
     1706
    17071707                if ((ieee80211_is_probe_response_frame(mgmt_header->frame_ctrl)) ||
    17081708                    (ieee80211_is_beacon_frame(mgmt_header->frame_ctrl)))
    17091709                        return ieee80211_process_probe_response(ieee80211_dev,
    17101710                            mgmt_header, buffer_size);
    1711                
     1711
    17121712                if (ieee80211_is_auth_frame(mgmt_header->frame_ctrl))
    17131713                        return ieee80211_process_auth_response(ieee80211_dev,
    17141714                            mgmt_header);
    1715                
     1715
    17161716                if (ieee80211_is_assoc_response_frame(mgmt_header->frame_ctrl))
    17171717                        return ieee80211_process_assoc_response(ieee80211_dev,
     
    17201720                return ieee80211_process_data(ieee80211_dev, buffer,
    17211721                    buffer_size);
    1722        
     1722
    17231723        return EOK;
    17241724}
Note: See TracChangeset for help on using the changeset viewer.