~ubuntu-branches/ubuntu/trusty/wpa/trusty

« back to all changes in this revision

Viewing changes to src/drivers/driver_test.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2014-03-04 16:13:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140304161324-md40gw8imcectbuu
Tags: 2.1-0ubuntu1
* New upstream release (LP: #1099755)
* debian/get-orig-source: update for new git repository for the current
  hostap/wpasupplicant versions.
* Dropped patches due to being applied upstream and included in the current
  source tarball:
  - debian/patches/11_wpa_gui_ftbfs_gcc_4_7.patch
  - debian/patches/13_human_readable_signal.patch
  - debian/patches/git_deinit_p2p_context_on_mgmt_remove_ff1f9c8.patch
  - debian/patches/libnl3-includes.patch
* debian/patches/git_accept_client_cert_from_server.patch: revert the commit:
  "OpenSSL: Do not accept SSL Client certificate for server", which breaks
  many AAA servers that include both client and server EKUs. Cherry-picked
  from hostap git commit b62d5b5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Testing driver interface for a simulated network driver
3
3
 * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
4
4
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 2 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * Alternatively, this software may be distributed under the terms of BSD
10
 
 * license.
11
 
 *
12
 
 * See README and COPYING for more details.
 
5
 * This software may be distributed under the terms of the BSD license.
 
6
 * See README for more details.
13
7
 */
14
8
 
15
9
/* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
34
28
#include "common/ieee802_11_defs.h"
35
29
#include "crypto/sha1.h"
36
30
#include "l2_packet/l2_packet.h"
37
 
#include "p2p/p2p.h"
38
31
#include "wps/wps.h"
39
32
#include "driver.h"
40
33
 
108
101
        unsigned int remain_on_channel_duration;
109
102
 
110
103
        int current_freq;
111
 
 
112
 
        struct p2p_data *p2p;
113
 
        unsigned int off_channel_freq;
114
 
        struct wpabuf *pending_action_tx;
115
 
        u8 pending_action_src[ETH_ALEN];
116
 
        u8 pending_action_dst[ETH_ALEN];
117
 
        u8 pending_action_bssid[ETH_ALEN];
118
 
        unsigned int pending_action_freq;
119
 
        unsigned int pending_action_no_cck;
120
 
        unsigned int pending_listen_freq;
121
 
        unsigned int pending_listen_duration;
122
 
        int pending_p2p_scan;
123
 
        struct sockaddr *probe_from;
124
 
        socklen_t probe_from_len;
125
104
};
126
105
 
127
106
 
131
110
static void wpa_driver_test_close_test_socket(
132
111
        struct wpa_driver_test_data *drv);
133
112
static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx);
134
 
static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv);
135
113
 
136
114
 
137
115
static void test_driver_free_bss(struct test_driver_bss *bss)
309
287
 
310
288
 
311
289
static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
312
 
                                     size_t data_len)
 
290
                                     size_t data_len, int noack)
313
291
{
314
292
        struct test_driver_bss *dbss = priv;
315
293
        struct wpa_driver_test_data *drv = dbss->drv;
485
463
        event.tx_status.ack = ret >= 0;
486
464
        wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
487
465
 
488
 
#ifdef CONFIG_P2P
489
 
        if (drv->p2p &&
490
 
            WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
491
 
            WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
492
 
                if (drv->pending_action_tx == NULL) {
493
 
                        wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
494
 
                                   "no pending operation");
495
 
                        return ret;
496
 
                }
497
 
 
498
 
                if (os_memcmp(hdr->addr1, drv->pending_action_dst, ETH_ALEN) !=
499
 
                    0) {
500
 
                        wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
501
 
                                   "unknown destination address");
502
 
                        return ret;
503
 
                }
504
 
 
505
 
                wpabuf_free(drv->pending_action_tx);
506
 
                drv->pending_action_tx = NULL;
507
 
 
508
 
                p2p_send_action_cb(drv->p2p, drv->pending_action_freq,
509
 
                                   drv->pending_action_dst,
510
 
                                   drv->pending_action_src,
511
 
                                   drv->pending_action_bssid,
512
 
                                   ret >= 0);
513
 
        }
514
 
#endif /* CONFIG_P2P */
515
 
 
516
466
        return ret;
517
467
}
518
468
 
559
509
                event.rx_probe_req.ie = ie;
560
510
                event.rx_probe_req.ie_len = ielen;
561
511
                wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, &event);
562
 
#ifdef CONFIG_P2P
563
 
                if (drv->p2p)
564
 
                        p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen);
565
 
#endif /* CONFIG_P2P */
566
512
        }
567
513
 
568
514
        dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
1065
1011
                              const char *ifname, const u8 *addr,
1066
1012
                              void *bss_ctx, void **drv_priv,
1067
1013
                              char *force_ifname, u8 *if_addr,
1068
 
                              const char *bridge)
 
1014
                              const char *bridge, int use_existing)
1069
1015
{
1070
1016
        struct test_driver_bss *dbss = priv;
1071
1017
        struct wpa_driver_test_data *drv = dbss->drv;
1281
1227
                        alen = sizeof(addr_un);
1282
1228
                }
1283
1229
                if (bind(drv->test_socket, addr, alen) < 0) {
1284
 
                        perror("bind(PF_UNIX)");
 
1230
                        perror("test-driver-init: bind(PF_UNIX)");
1285
1231
                        close(drv->test_socket);
1286
1232
                        if (drv->own_socket_path)
1287
1233
                                unlink(drv->own_socket_path);
1319
1265
 
1320
1266
static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1321
1267
{
1322
 
        struct wpa_driver_test_data *drv = eloop_ctx;
1323
1268
        wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1324
 
        if (drv->pending_p2p_scan && drv->p2p) {
1325
 
#ifdef CONFIG_P2P
1326
 
                size_t i;
1327
 
                for (i = 0; i < drv->num_scanres; i++) {
1328
 
                        struct wpa_scan_res *bss = drv->scanres[i];
1329
 
                        if (p2p_scan_res_handler(drv->p2p, bss->bssid,
1330
 
                                                 bss->freq, bss->level,
1331
 
                                                 (const u8 *) (bss + 1),
1332
 
                                                 bss->ie_len) > 0)
1333
 
                                return;
1334
 
                }
1335
 
                p2p_scan_res_handled(drv->p2p);
1336
 
#endif /* CONFIG_P2P */
1337
 
                return;
1338
 
        }
1339
1269
        wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1340
1270
}
1341
1271
 
1484
1414
        if (res == NULL)
1485
1415
                return NULL;
1486
1416
 
1487
 
        res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
 
1417
        res->res = os_calloc(drv->num_scanres, sizeof(struct wpa_scan_res *));
1488
1418
        if (res->res == NULL) {
1489
1419
                os_free(res);
1490
1420
                return NULL;
1720
1650
}
1721
1651
 
1722
1652
 
1723
 
static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
1724
 
                                        int reason_code)
1725
 
{
1726
 
        struct test_driver_bss *dbss = priv;
1727
 
        struct wpa_driver_test_data *drv = dbss->drv;
1728
 
        wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1729
 
                   __func__, MAC2STR(addr), reason_code);
1730
 
        os_memset(dbss->bssid, 0, ETH_ALEN);
1731
 
        drv->associated = 0;
1732
 
        wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1733
 
        return wpa_driver_test_send_disassoc(drv);
1734
 
}
1735
 
 
1736
 
 
1737
1653
static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1738
1654
{
1739
1655
        const u8 *end, *pos;
1967
1883
                                data_len - (mgmt->u.probe_req.variable - data);
1968
1884
                        wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ,
1969
1885
                                             &event);
1970
 
#ifdef CONFIG_P2P
1971
 
                        if (drv->p2p)
1972
 
                                p2p_probe_req_rx(drv->p2p, mgmt->sa,
1973
 
                                                 mgmt->da, mgmt->bssid,
1974
 
                                                 event.rx_probe_req.ie,
1975
 
                                                 event.rx_probe_req.ie_len);
1976
 
#endif /* CONFIG_P2P */
1977
1886
                }
1978
1887
        }
1979
 
 
1980
 
#ifdef CONFIG_P2P
1981
 
        if (drv->p2p &&
1982
 
            WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1983
 
            WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
1984
 
                size_t hdr_len;
1985
 
                hdr_len = (const u8 *)
1986
 
                        &mgmt->u.action.u.vs_public_action.action - data;
1987
 
                p2p_rx_action(drv->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
1988
 
                              mgmt->u.action.category,
1989
 
                              &mgmt->u.action.u.vs_public_action.action,
1990
 
                              data_len - hdr_len, freq);
1991
 
        }
1992
 
#endif /* CONFIG_P2P */
1993
 
 
1994
1888
}
1995
1889
 
1996
1890
 
2006
1900
        bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
2007
1901
 
2008
1902
        /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
2009
 
#ifdef CONFIG_P2P
2010
 
        if (drv->probe_req_report && drv->p2p && data_len) {
2011
 
                const char *d = (const char *) data;
2012
 
                u8 sa[ETH_ALEN];
2013
 
                u8 ie[512];
2014
 
                size_t ielen;
2015
 
 
2016
 
                if (hwaddr_aton(d, sa))
2017
 
                        return;
2018
 
                d += 18;
2019
 
                while (*d == ' ')
2020
 
                        d++;
2021
 
                ielen = os_strlen(d) / 2;
2022
 
                if (ielen > sizeof(ie))
2023
 
                        ielen = sizeof(ie);
2024
 
                if (hexstr2bin(d, ie, ielen) < 0)
2025
 
                        ielen = 0;
2026
 
                drv->probe_from = from;
2027
 
                drv->probe_from_len = fromlen;
2028
 
                p2p_probe_req_rx(drv->p2p, sa, NULL, NULL, ie, ielen);
2029
 
                drv->probe_from = NULL;
2030
 
        }
2031
 
#endif /* CONFIG_P2P */
2032
1903
 
2033
1904
        if (!drv->ibss)
2034
1905
                return;
2185
2056
        struct test_client_socket *cli, *prev;
2186
2057
        int i;
2187
2058
 
2188
 
#ifdef CONFIG_P2P
2189
 
        if (drv->p2p)
2190
 
                p2p_deinit(drv->p2p);
2191
 
        wpabuf_free(drv->pending_action_tx);
2192
 
#endif /* CONFIG_P2P */
2193
 
 
2194
2059
        cli = drv->cli;
2195
2060
        while (cli) {
2196
2061
                prev = cli;
2258
2123
        os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
2259
2124
        if (bind(drv->test_socket, (struct sockaddr *) &addr,
2260
2125
                 sizeof(addr)) < 0) {
2261
 
                perror("bind(PF_UNIX)");
 
2126
                perror("test-driver-attach: bind(PF_UNIX)");
2262
2127
                close(drv->test_socket);
2263
2128
                unlink(drv->own_socket_path);
2264
2129
                os_free(drv->own_socket_path);
2387
2252
                drv->use_associnfo = 1;
2388
2253
        }
2389
2254
 
2390
 
        if (os_strstr(param, "p2p_mgmt=1")) {
2391
 
                wpa_printf(MSG_DEBUG, "test_driver: Use internal P2P "
2392
 
                           "management");
2393
 
                if (wpa_driver_test_init_p2p(drv) < 0)
2394
 
                        return -1;
2395
 
        }
2396
 
 
2397
2255
        return 0;
2398
2256
}
2399
2257
 
2483
2341
 
2484
2342
static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
2485
2343
{
2486
 
        struct test_driver_bss *dbss = priv;
2487
 
        struct wpa_driver_test_data *drv = dbss->drv;
2488
2344
        os_memset(capa, 0, sizeof(*capa));
2489
2345
        capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2490
2346
                WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2500
2356
        capa->auth = WPA_DRIVER_AUTH_OPEN |
2501
2357
                WPA_DRIVER_AUTH_SHARED |
2502
2358
                WPA_DRIVER_AUTH_LEAP;
2503
 
        if (drv->p2p)
2504
 
                capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT;
2505
2359
        capa->flags |= WPA_DRIVER_FLAGS_AP;
2506
2360
        capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2507
2361
        capa->flags |= WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE;
2576
2430
 
2577
2431
        *num_modes = 3;
2578
2432
        *flags = 0;
2579
 
        modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
 
2433
        modes = os_calloc(*num_modes, sizeof(struct hostapd_hw_modes));
2580
2434
        if (modes == NULL)
2581
2435
                return NULL;
2582
2436
        modes[0].mode = HOSTAPD_MODE_IEEE80211G;
2583
2437
        modes[0].num_channels = 11;
2584
2438
        modes[0].num_rates = 12;
2585
 
        modes[0].channels =
2586
 
                os_zalloc(11 * sizeof(struct hostapd_channel_data));
2587
 
        modes[0].rates = os_zalloc(modes[0].num_rates * sizeof(int));
 
2439
        modes[0].channels = os_calloc(11, sizeof(struct hostapd_channel_data));
 
2440
        modes[0].rates = os_calloc(modes[0].num_rates, sizeof(int));
2588
2441
        if (modes[0].channels == NULL || modes[0].rates == NULL)
2589
2442
                goto fail;
2590
2443
        for (i = 0; i < 11; i++) {
2608
2461
        modes[1].mode = HOSTAPD_MODE_IEEE80211B;
2609
2462
        modes[1].num_channels = 11;
2610
2463
        modes[1].num_rates = 4;
2611
 
        modes[1].channels =
2612
 
                os_zalloc(11 * sizeof(struct hostapd_channel_data));
2613
 
        modes[1].rates = os_zalloc(modes[1].num_rates * sizeof(int));
 
2464
        modes[1].channels = os_calloc(11, sizeof(struct hostapd_channel_data));
 
2465
        modes[1].rates = os_calloc(modes[1].num_rates, sizeof(int));
2614
2466
        if (modes[1].channels == NULL || modes[1].rates == NULL)
2615
2467
                goto fail;
2616
2468
        for (i = 0; i < 11; i++) {
2626
2478
        modes[2].mode = HOSTAPD_MODE_IEEE80211A;
2627
2479
        modes[2].num_channels = 1;
2628
2480
        modes[2].num_rates = 8;
2629
 
        modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2630
 
        modes[2].rates = os_zalloc(modes[2].num_rates * sizeof(int));
 
2481
        modes[2].channels = os_calloc(1, sizeof(struct hostapd_channel_data));
 
2482
        modes[2].rates = os_calloc(modes[2].num_rates, sizeof(int));
2631
2483
        if (modes[2].channels == NULL || modes[2].rates == NULL)
2632
2484
                goto fail;
2633
2485
        modes[2].channels[0].chan = 60;
2705
2557
        os_memcpy(hdr->addr2, src, ETH_ALEN);
2706
2558
        os_memcpy(hdr->addr3, bssid, ETH_ALEN);
2707
2559
 
2708
 
        ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len);
 
2560
        ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len, 0);
2709
2561
        os_free(buf);
2710
2562
        return ret;
2711
2563
}
2712
2564
 
2713
2565
 
2714
 
#ifdef CONFIG_P2P
2715
 
static void test_send_action_cb(void *eloop_ctx, void *timeout_ctx)
2716
 
{
2717
 
        struct wpa_driver_test_data *drv = eloop_ctx;
2718
 
 
2719
 
        if (drv->pending_action_tx == NULL)
2720
 
                return;
2721
 
 
2722
 
        if (drv->off_channel_freq != drv->pending_action_freq) {
2723
 
                wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
2724
 
                           "waiting for another freq=%u",
2725
 
                           drv->pending_action_freq);
2726
 
                return;
2727
 
        }
2728
 
        wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
2729
 
                   MACSTR, MAC2STR(drv->pending_action_dst));
2730
 
        wpa_driver_test_send_action(drv, drv->pending_action_freq, 0,
2731
 
                                    drv->pending_action_dst,
2732
 
                                    drv->pending_action_src,
2733
 
                                    drv->pending_action_bssid,
2734
 
                                    wpabuf_head(drv->pending_action_tx),
2735
 
                                    wpabuf_len(drv->pending_action_tx),
2736
 
                                    drv->pending_action_no_cck);
2737
 
}
2738
 
#endif /* CONFIG_P2P */
2739
 
 
2740
 
 
2741
2566
static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx)
2742
2567
{
2743
2568
        struct wpa_driver_test_data *drv = eloop_ctx;
2749
2574
        data.remain_on_channel.freq = drv->remain_on_channel_freq;
2750
2575
        data.remain_on_channel.duration = drv->remain_on_channel_duration;
2751
2576
 
2752
 
        if (drv->p2p)
2753
 
                drv->off_channel_freq = 0;
2754
 
 
2755
2577
        drv->remain_on_channel_freq = 0;
2756
2578
 
2757
2579
        wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data);
2785
2607
        data.remain_on_channel.duration = duration;
2786
2608
        wpa_supplicant_event(drv->ctx, EVENT_REMAIN_ON_CHANNEL, &data);
2787
2609
 
2788
 
#ifdef CONFIG_P2P
2789
 
        if (drv->p2p) {
2790
 
                drv->off_channel_freq = drv->remain_on_channel_freq;
2791
 
                test_send_action_cb(drv, NULL);
2792
 
                if (drv->off_channel_freq == drv->pending_listen_freq) {
2793
 
                        p2p_listen_cb(drv->p2p, drv->pending_listen_freq,
2794
 
                                      drv->pending_listen_duration);
2795
 
                        drv->pending_listen_freq = 0;
2796
 
                }
2797
 
        }
2798
 
#endif /* CONFIG_P2P */
2799
 
 
2800
2610
        return 0;
2801
2611
}
2802
2612
 
2824
2634
}
2825
2635
 
2826
2636
 
2827
 
#ifdef CONFIG_P2P
2828
 
 
2829
 
static int wpa_driver_test_p2p_find(void *priv, unsigned int timeout, int type)
2830
 
{
2831
 
        struct wpa_driver_test_data *drv = priv;
2832
 
        wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
2833
 
        if (!drv->p2p)
2834
 
                return -1;
2835
 
        return p2p_find(drv->p2p, timeout, type, 0, NULL, NULL);
2836
 
}
2837
 
 
2838
 
 
2839
 
static int wpa_driver_test_p2p_stop_find(void *priv)
2840
 
{
2841
 
        struct wpa_driver_test_data *drv = priv;
2842
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
2843
 
        if (!drv->p2p)
2844
 
                return -1;
2845
 
        p2p_stop_find(drv->p2p);
2846
 
        return 0;
2847
 
}
2848
 
 
2849
 
 
2850
 
static int wpa_driver_test_p2p_listen(void *priv, unsigned int timeout)
2851
 
{
2852
 
        struct wpa_driver_test_data *drv = priv;
2853
 
        wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
2854
 
        if (!drv->p2p)
2855
 
                return -1;
2856
 
        return p2p_listen(drv->p2p, timeout);
2857
 
}
2858
 
 
2859
 
 
2860
 
static int wpa_driver_test_p2p_connect(void *priv, const u8 *peer_addr,
2861
 
                                       int wps_method, int go_intent,
2862
 
                                       const u8 *own_interface_addr,
2863
 
                                       unsigned int force_freq,
2864
 
                                       int persistent_group)
2865
 
{
2866
 
        struct wpa_driver_test_data *drv = priv;
2867
 
        wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR " wps_method=%d "
2868
 
                   "go_intent=%d "
2869
 
                   "own_interface_addr=" MACSTR " force_freq=%u "
2870
 
                   "persistent_group=%d)",
2871
 
                   __func__, MAC2STR(peer_addr), wps_method, go_intent,
2872
 
                   MAC2STR(own_interface_addr), force_freq, persistent_group);
2873
 
        if (!drv->p2p)
2874
 
                return -1;
2875
 
        return p2p_connect(drv->p2p, peer_addr, wps_method, go_intent,
2876
 
                           own_interface_addr, force_freq, persistent_group);
2877
 
}
2878
 
 
2879
 
 
2880
 
static int wpa_driver_test_wps_success_cb(void *priv, const u8 *peer_addr)
2881
 
{
2882
 
        struct wpa_driver_test_data *drv = priv;
2883
 
        wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR ")",
2884
 
                   __func__, MAC2STR(peer_addr));
2885
 
        if (!drv->p2p)
2886
 
                return -1;
2887
 
        p2p_wps_success_cb(drv->p2p, peer_addr);
2888
 
        return 0;
2889
 
}
2890
 
 
2891
 
 
2892
 
static int wpa_driver_test_p2p_group_formation_failed(void *priv)
2893
 
{
2894
 
        struct wpa_driver_test_data *drv = priv;
2895
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
2896
 
        if (!drv->p2p)
2897
 
                return -1;
2898
 
        p2p_group_formation_failed(drv->p2p);
2899
 
        return 0;
2900
 
}
2901
 
 
2902
 
 
2903
 
static int wpa_driver_test_p2p_set_params(void *priv,
2904
 
                                          const struct p2p_params *params)
2905
 
{
2906
 
        struct wpa_driver_test_data *drv = priv;
2907
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
2908
 
        if (!drv->p2p)
2909
 
                return -1;
2910
 
        if (p2p_set_dev_name(drv->p2p, params->dev_name) < 0 ||
2911
 
            p2p_set_pri_dev_type(drv->p2p, params->pri_dev_type) < 0 ||
2912
 
            p2p_set_sec_dev_types(drv->p2p, params->sec_dev_type,
2913
 
                                  params->num_sec_dev_types) < 0)
2914
 
                return -1;
2915
 
        return 0;
2916
 
}
2917
 
 
2918
 
 
2919
 
static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
2920
 
                         unsigned int num_req_dev_types,
2921
 
                         const u8 *req_dev_types, const u8 *dev_id)
2922
 
{
2923
 
        struct wpa_driver_test_data *drv = ctx;
2924
 
        struct wpa_driver_scan_params params;
2925
 
        int ret;
2926
 
        struct wpabuf *wps_ie, *ies;
2927
 
        int social_channels[] = { 2412, 2437, 2462, 0, 0 };
2928
 
        size_t ielen;
2929
 
 
2930
 
        wpa_printf(MSG_DEBUG, "%s(type=%d freq=%d)",
2931
 
                   __func__, type, freq);
2932
 
 
2933
 
        os_memset(&params, 0, sizeof(params));
2934
 
 
2935
 
        /* P2P Wildcard SSID */
2936
 
        params.num_ssids = 1;
2937
 
        params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
2938
 
        params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
2939
 
 
2940
 
#if 0 /* TODO: WPS IE */
2941
 
        wpa_s->wps->dev.p2p = 1;
2942
 
        wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
2943
 
                                        WPS_REQ_ENROLLEE);
2944
 
#else
2945
 
        wps_ie = wpabuf_alloc(1);
2946
 
#endif
2947
 
        if (wps_ie == NULL)
2948
 
                return -1;
2949
 
 
2950
 
        ielen = p2p_scan_ie_buf_len(drv->p2p);
2951
 
        ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
2952
 
        if (ies == NULL) {
2953
 
                wpabuf_free(wps_ie);
2954
 
                return -1;
2955
 
        }
2956
 
        wpabuf_put_buf(ies, wps_ie);
2957
 
        wpabuf_free(wps_ie);
2958
 
 
2959
 
        p2p_scan_ie(drv->p2p, ies, dev_id);
2960
 
 
2961
 
        params.extra_ies = wpabuf_head(ies);
2962
 
        params.extra_ies_len = wpabuf_len(ies);
2963
 
 
2964
 
        switch (type) {
2965
 
        case P2P_SCAN_SOCIAL:
2966
 
                params.freqs = social_channels;
2967
 
                break;
2968
 
        case P2P_SCAN_FULL:
2969
 
                break;
2970
 
        case P2P_SCAN_SPECIFIC:
2971
 
                social_channels[0] = freq;
2972
 
                social_channels[1] = 0;
2973
 
                params.freqs = social_channels;
2974
 
                break;
2975
 
        case P2P_SCAN_SOCIAL_PLUS_ONE:
2976
 
                social_channels[3] = freq;
2977
 
                params.freqs = social_channels;
2978
 
                break;
2979
 
        }
2980
 
 
2981
 
        drv->pending_p2p_scan = 1;
2982
 
        ret = wpa_driver_test_scan(drv, &params);
2983
 
 
2984
 
        wpabuf_free(ies);
2985
 
 
2986
 
        return ret;
2987
 
}
2988
 
 
2989
 
 
2990
 
static int test_send_action(void *ctx, unsigned int freq, const u8 *dst,
2991
 
                            const u8 *src, const u8 *bssid, const u8 *buf,
2992
 
                            size_t len, unsigned int wait_time)
2993
 
{
2994
 
        struct wpa_driver_test_data *drv = ctx;
2995
 
 
2996
 
        wpa_printf(MSG_DEBUG, "%s(freq=%u dst=" MACSTR " src=" MACSTR
2997
 
                   " bssid=" MACSTR " len=%d",
2998
 
                   __func__, freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
2999
 
                   (int) len);
3000
 
        if (freq <= 0) {
3001
 
                wpa_printf(MSG_WARNING, "P2P: No frequency specified for "
3002
 
                           "action frame TX");
3003
 
                return -1;
3004
 
        }
3005
 
 
3006
 
        if (drv->pending_action_tx) {
3007
 
                wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
3008
 
                           "to " MACSTR, MAC2STR(drv->pending_action_dst));
3009
 
                wpabuf_free(drv->pending_action_tx);
3010
 
        }
3011
 
        drv->pending_action_tx = wpabuf_alloc(len);
3012
 
        if (drv->pending_action_tx == NULL)
3013
 
                return -1;
3014
 
        wpabuf_put_data(drv->pending_action_tx, buf, len);
3015
 
        os_memcpy(drv->pending_action_src, src, ETH_ALEN);
3016
 
        os_memcpy(drv->pending_action_dst, dst, ETH_ALEN);
3017
 
        os_memcpy(drv->pending_action_bssid, bssid, ETH_ALEN);
3018
 
        drv->pending_action_freq = freq;
3019
 
        drv->pending_action_no_cck = 1;
3020
 
 
3021
 
        if (drv->off_channel_freq == freq) {
3022
 
                /* Already on requested channel; send immediately */
3023
 
                /* TODO: Would there ever be need to extend the current
3024
 
                 * duration on the channel? */
3025
 
                eloop_cancel_timeout(test_send_action_cb, drv, NULL);
3026
 
                eloop_register_timeout(0, 0, test_send_action_cb, drv, NULL);
3027
 
                return 0;
3028
 
        }
3029
 
 
3030
 
        wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
3031
 
                   "once the driver gets to the requested channel");
3032
 
        if (wpa_driver_test_remain_on_channel(drv, freq, wait_time) < 0) {
3033
 
                wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
3034
 
                           "to remain on channel (%u MHz) for Action "
3035
 
                           "Frame TX", freq);
3036
 
                return -1;
3037
 
        }
3038
 
 
3039
 
        return 0;
3040
 
}
3041
 
 
3042
 
 
3043
 
static void test_send_action_done(void *ctx)
3044
 
{
3045
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3046
 
        /* TODO */
3047
 
}
3048
 
 
3049
 
 
3050
 
static void test_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
3051
 
{
3052
 
        struct wpa_driver_test_data *drv = ctx;
3053
 
        union wpa_event_data event;
3054
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3055
 
        os_memset(&event, 0, sizeof(event));
3056
 
        event.p2p_go_neg_completed.res = res;
3057
 
        wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_COMPLETED, &event);
3058
 
}
3059
 
 
3060
 
 
3061
 
static void test_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
3062
 
{
3063
 
        struct wpa_driver_test_data *drv = ctx;
3064
 
        union wpa_event_data event;
3065
 
        wpa_printf(MSG_DEBUG, "%s(src=" MACSTR ")", __func__, MAC2STR(src));
3066
 
        os_memset(&event, 0, sizeof(event));
3067
 
        event.p2p_go_neg_req_rx.src = src;
3068
 
        event.p2p_go_neg_req_rx.dev_passwd_id = dev_passwd_id;
3069
 
        wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_REQ_RX, &event);
3070
 
}
3071
 
 
3072
 
 
3073
 
static void test_dev_found(void *ctx, const u8 *addr,
3074
 
                           const struct p2p_peer_info *info, int new_device)
3075
 
{
3076
 
        struct wpa_driver_test_data *drv = ctx;
3077
 
        union wpa_event_data event;
3078
 
        char devtype[WPS_DEV_TYPE_BUFSIZE];
3079
 
        wpa_printf(MSG_DEBUG, "%s(" MACSTR " p2p_dev_addr=" MACSTR
3080
 
                   " pri_dev_type=%s name='%s' config_methods=0x%x "
3081
 
                   "dev_capab=0x%x group_capab=0x%x)",
3082
 
                   __func__, MAC2STR(addr), MAC2STR(info->p2p_device_addr),
3083
 
                   wps_dev_type_bin2str(info->pri_dev_type, devtype,
3084
 
                                        sizeof(devtype)),
3085
 
                   info->device_name, info->config_methods, info->dev_capab,
3086
 
                   info->group_capab);
3087
 
 
3088
 
        os_memset(&event, 0, sizeof(event));
3089
 
        event.p2p_dev_found.addr = addr;
3090
 
        event.p2p_dev_found.dev_addr = info->p2p_device_addr;
3091
 
        event.p2p_dev_found.pri_dev_type = info->pri_dev_type;
3092
 
        event.p2p_dev_found.dev_name = info->device_name;
3093
 
        event.p2p_dev_found.config_methods = info->config_methods;
3094
 
        event.p2p_dev_found.dev_capab = info->dev_capab;
3095
 
        event.p2p_dev_found.group_capab = info->group_capab;
3096
 
        wpa_supplicant_event(drv->ctx, EVENT_P2P_DEV_FOUND, &event);
3097
 
}
3098
 
 
3099
 
 
3100
 
static int test_start_listen(void *ctx, unsigned int freq,
3101
 
                             unsigned int duration,
3102
 
                             const struct wpabuf *probe_resp_ie)
3103
 
{
3104
 
        struct wpa_driver_test_data *drv = ctx;
3105
 
 
3106
 
        wpa_printf(MSG_DEBUG, "%s(freq=%u duration=%u)",
3107
 
                   __func__, freq, duration);
3108
 
 
3109
 
        if (wpa_driver_test_probe_req_report(drv, 1) < 0)
3110
 
                return -1;
3111
 
 
3112
 
        drv->pending_listen_freq = freq;
3113
 
        drv->pending_listen_duration = duration;
3114
 
 
3115
 
        if (wpa_driver_test_remain_on_channel(drv, freq, duration) < 0) {
3116
 
                drv->pending_listen_freq = 0;
3117
 
                return -1;
3118
 
        }
3119
 
 
3120
 
        return 0;
3121
 
}
3122
 
 
3123
 
 
3124
 
static void test_stop_listen(void *ctx)
3125
 
{
3126
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3127
 
        /* TODO */
3128
 
}
3129
 
 
3130
 
 
3131
 
static int test_send_probe_resp(void *ctx, const struct wpabuf *buf)
3132
 
{
3133
 
        struct wpa_driver_test_data *drv = ctx;
3134
 
        char resp[512], *pos, *end;
3135
 
        int ret;
3136
 
        const struct ieee80211_mgmt *mgmt;
3137
 
        const u8 *ie, *ie_end;
3138
 
 
3139
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3140
 
        wpa_hexdump_buf(MSG_MSGDUMP, "Probe Response", buf);
3141
 
        if (wpabuf_len(buf) < 24)
3142
 
                return -1;
3143
 
        if (!drv->probe_from) {
3144
 
                wpa_printf(MSG_DEBUG, "%s: probe_from not set", __func__);
3145
 
                return -1;
3146
 
        }
3147
 
 
3148
 
        pos = resp;
3149
 
        end = resp + sizeof(resp);
3150
 
 
3151
 
        mgmt = wpabuf_head(buf);
3152
 
 
3153
 
        /* reply: SCANRESP BSSID SSID IEs */
3154
 
        ret = os_snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
3155
 
                          MAC2STR(mgmt->bssid));
3156
 
        if (ret < 0 || ret >= end - pos)
3157
 
                return -1;
3158
 
        pos += ret;
3159
 
 
3160
 
        ie = mgmt->u.probe_resp.variable;
3161
 
        ie_end = wpabuf_head_u8(buf) + wpabuf_len(buf);
3162
 
        if (ie_end - ie < 2 || ie[0] != WLAN_EID_SSID ||
3163
 
            ie + 2 + ie[1] > ie_end)
3164
 
                return -1;
3165
 
        pos += wpa_snprintf_hex(pos, end - pos, ie + 2, ie[1]);
3166
 
 
3167
 
        ret = os_snprintf(pos, end - pos, " ");
3168
 
        if (ret < 0 || ret >= end - pos)
3169
 
                return -1;
3170
 
        pos += ret;
3171
 
        pos += wpa_snprintf_hex(pos, end - pos, ie, ie_end - ie);
3172
 
 
3173
 
        sendto(drv->test_socket, resp, pos - resp, 0,
3174
 
               drv->probe_from, drv->probe_from_len);
3175
 
 
3176
 
        return 0;
3177
 
}
3178
 
 
3179
 
 
3180
 
static void test_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
3181
 
                            u16 update_indic, const u8 *tlvs, size_t tlvs_len)
3182
 
{
3183
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3184
 
        /* TODO */
3185
 
}
3186
 
 
3187
 
 
3188
 
static void test_sd_response(void *ctx, const u8 *sa, u16 update_indic,
3189
 
                             const u8 *tlvs, size_t tlvs_len)
3190
 
{
3191
 
        wpa_printf(MSG_DEBUG, "%s", __func__);
3192
 
        /* TODO */
3193
 
}
3194
 
 
3195
 
 
3196
 
static void test_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3197
 
                               const u8 *dev_addr, const u8 *pri_dev_type,
3198
 
                               const char *dev_name, u16 supp_config_methods,
3199
 
                               u8 dev_capab, u8 group_capab,
3200
 
                               const u8 *group_id, size_t group_id_len)
3201
 
{
3202
 
        wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
3203
 
                   __func__, MAC2STR(peer), config_methods);
3204
 
        /* TODO */
3205
 
}
3206
 
 
3207
 
 
3208
 
static void test_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3209
 
{
3210
 
        wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
3211
 
                   __func__, MAC2STR(peer), config_methods);
3212
 
        /* TODO */
3213
 
}
3214
 
 
3215
 
#endif /* CONFIG_P2P */
3216
 
 
3217
 
 
3218
 
static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv)
3219
 
{
3220
 
#ifdef CONFIG_P2P
3221
 
        struct p2p_config p2p;
3222
 
        unsigned int r;
3223
 
        int i;
3224
 
 
3225
 
        os_memset(&p2p, 0, sizeof(p2p));
3226
 
        p2p.msg_ctx = drv->ctx;
3227
 
        p2p.cb_ctx = drv;
3228
 
        p2p.p2p_scan = test_p2p_scan;
3229
 
        p2p.send_action = test_send_action;
3230
 
        p2p.send_action_done = test_send_action_done;
3231
 
        p2p.go_neg_completed = test_go_neg_completed;
3232
 
        p2p.go_neg_req_rx = test_go_neg_req_rx;
3233
 
        p2p.dev_found = test_dev_found;
3234
 
        p2p.start_listen = test_start_listen;
3235
 
        p2p.stop_listen = test_stop_listen;
3236
 
        p2p.send_probe_resp = test_send_probe_resp;
3237
 
        p2p.sd_request = test_sd_request;
3238
 
        p2p.sd_response = test_sd_response;
3239
 
        p2p.prov_disc_req = test_prov_disc_req;
3240
 
        p2p.prov_disc_resp = test_prov_disc_resp;
3241
 
 
3242
 
        os_memcpy(p2p.dev_addr, drv->own_addr, ETH_ALEN);
3243
 
 
3244
 
        p2p.reg_class = 12; /* TODO: change depending on location */
3245
 
        /*
3246
 
         * Pick one of the social channels randomly as the listen
3247
 
         * channel.
3248
 
         */
3249
 
        os_get_random((u8 *) &r, sizeof(r));
3250
 
        p2p.channel = 1 + (r % 3) * 5;
3251
 
 
3252
 
        /* TODO: change depending on location */
3253
 
        p2p.op_reg_class = 12;
3254
 
        /*
3255
 
         * For initial tests, pick the operation channel randomly.
3256
 
         * TODO: Use scan results (etc.) to select the best channel.
3257
 
         */
3258
 
        p2p.op_channel = 1 + r % 11;
3259
 
 
3260
 
        os_memcpy(p2p.country, "US ", 3);
3261
 
 
3262
 
        /* FIX: fetch available channels from the driver */
3263
 
        p2p.channels.reg_classes = 1;
3264
 
        p2p.channels.reg_class[0].reg_class = 12; /* US/12 = 2.4 GHz band */
3265
 
        p2p.channels.reg_class[0].channels = 11;
3266
 
        for (i = 0; i < 11; i++)
3267
 
                p2p.channels.reg_class[0].channel[i] = i + 1;
3268
 
 
3269
 
        p2p.max_peers = 100;
3270
 
 
3271
 
        drv->p2p = p2p_init(&p2p);
3272
 
        if (drv->p2p == NULL)
3273
 
                return -1;
3274
 
        return 0;
3275
 
#else /* CONFIG_P2P */
3276
 
        wpa_printf(MSG_INFO, "driver_test: P2P support not included");
3277
 
        return -1;
3278
 
#endif /* CONFIG_P2P */
3279
 
}
3280
 
 
3281
 
 
3282
2637
const struct wpa_driver_ops wpa_driver_test_ops = {
3283
2638
        "test",
3284
2639
        "wpa_supplicant test driver",
3304
2659
        .deinit = wpa_driver_test_deinit,
3305
2660
        .set_param = wpa_driver_test_set_param,
3306
2661
        .deauthenticate = wpa_driver_test_deauthenticate,
3307
 
        .disassociate = wpa_driver_test_disassociate,
3308
2662
        .associate = wpa_driver_test_associate,
3309
2663
        .get_capa = wpa_driver_test_get_capa,
3310
2664
        .get_mac_addr = wpa_driver_test_get_mac_addr,
3321
2675
        .remain_on_channel = wpa_driver_test_remain_on_channel,
3322
2676
        .cancel_remain_on_channel = wpa_driver_test_cancel_remain_on_channel,
3323
2677
        .probe_req_report = wpa_driver_test_probe_req_report,
3324
 
#ifdef CONFIG_P2P
3325
 
        .p2p_find = wpa_driver_test_p2p_find,
3326
 
        .p2p_stop_find = wpa_driver_test_p2p_stop_find,
3327
 
        .p2p_listen = wpa_driver_test_p2p_listen,
3328
 
        .p2p_connect = wpa_driver_test_p2p_connect,
3329
 
        .wps_success_cb = wpa_driver_test_wps_success_cb,
3330
 
        .p2p_group_formation_failed =
3331
 
        wpa_driver_test_p2p_group_formation_failed,
3332
 
        .p2p_set_params = wpa_driver_test_p2p_set_params,
3333
 
#endif /* CONFIG_P2P */
3334
2678
};