Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wpa_supplicant - P2P |
| 3 | * Copyright (c) 2009-2010, Atheros Communications |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4 | * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "eloop.h" |
| 14 | #include "common/ieee802_11_common.h" |
| 15 | #include "common/ieee802_11_defs.h" |
| 16 | #include "common/wpa_ctrl.h" |
| 17 | #include "wps/wps_i.h" |
| 18 | #include "p2p/p2p.h" |
| 19 | #include "ap/hostapd.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 20 | #include "ap/ap_config.h" |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 21 | #include "ap/sta_info.h" |
| 22 | #include "ap/ap_drv_ops.h" |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 23 | #include "ap/wps_hostapd.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 24 | #include "ap/p2p_hostapd.h" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 25 | #include "eapol_supp/eapol_supp_sm.h" |
| 26 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 27 | #include "wpa_supplicant_i.h" |
| 28 | #include "driver_i.h" |
| 29 | #include "ap.h" |
| 30 | #include "config_ssid.h" |
| 31 | #include "config.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 32 | #include "notify.h" |
| 33 | #include "scan.h" |
| 34 | #include "bss.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 35 | #include "offchannel.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 36 | #include "wps_supplicant.h" |
| 37 | #include "p2p_supplicant.h" |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 38 | #include "wifi_display.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /* |
| 42 | * How many times to try to scan to find the GO before giving up on join |
| 43 | * request. |
| 44 | */ |
| 45 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 |
| 46 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 47 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 |
| 48 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 49 | #ifndef P2P_MAX_CLIENT_IDLE |
| 50 | /* |
| 51 | * How many seconds to try to reconnect to the GO when connection in P2P client |
| 52 | * role has been lost. |
| 53 | */ |
| 54 | #define P2P_MAX_CLIENT_IDLE 10 |
| 55 | #endif /* P2P_MAX_CLIENT_IDLE */ |
| 56 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 57 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
| 58 | /* |
| 59 | * How many seconds to wait for initial 4-way handshake to get completed after |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 60 | * WPS provisioning step or after the re-invocation of a persistent group on a |
| 61 | * P2P Client. |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 62 | */ |
| 63 | #define P2P_MAX_INITIAL_CONN_WAIT 10 |
| 64 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ |
| 65 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 66 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO |
| 67 | /* |
| 68 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 69 | * WPS provisioning step on the GO. This controls the extra time the P2P |
| 70 | * operation is considered to be in progress (e.g., to delay other scans) after |
| 71 | * WPS provisioning has been completed on the GO during group formation. |
| 72 | */ |
| 73 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 |
| 74 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ |
| 75 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 76 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE |
| 77 | /* |
| 78 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 79 | * re-invocation of a persistent group on the GO when the client is expected |
| 80 | * to connect automatically (no user interaction). |
| 81 | */ |
| 82 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 |
| 83 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ |
| 84 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 85 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 86 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 87 | enum p2p_group_removal_reason { |
| 88 | P2P_GROUP_REMOVAL_UNKNOWN, |
| 89 | P2P_GROUP_REMOVAL_SILENT, |
| 90 | P2P_GROUP_REMOVAL_FORMATION_FAILED, |
| 91 | P2P_GROUP_REMOVAL_REQUESTED, |
| 92 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, |
| 93 | P2P_GROUP_REMOVAL_UNAVAILABLE, |
| 94 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 95 | P2P_GROUP_REMOVAL_PSK_FAILURE, |
| 96 | P2P_GROUP_REMOVAL_FREQ_CONFLICT |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 99 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 100 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 101 | static struct wpa_supplicant * |
| 102 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 103 | int go); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 104 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 105 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 106 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 107 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
| 109 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 110 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 111 | int auto_join, int freq, |
| 112 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
| 114 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
| 115 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
| 116 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 117 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 118 | void *timeout_ctx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 119 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 120 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 121 | int group_added); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 122 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 123 | static void wpas_stop_listen(void *ctx); |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 124 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 125 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 126 | |
| 127 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Get the number of concurrent channels that the HW can operate, but that are |
| 130 | * currently not in use by any of the wpa_supplicant interfaces. |
| 131 | */ |
| 132 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) |
| 133 | { |
| 134 | int *freqs; |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 135 | int num, unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 136 | |
| 137 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 138 | if (!freqs) |
| 139 | return -1; |
| 140 | |
| 141 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 142 | wpa_s->num_multichan_concurrent); |
| 143 | os_free(freqs); |
| 144 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 145 | unused = wpa_s->num_multichan_concurrent - num; |
| 146 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); |
| 147 | return unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * Get the frequencies that are currently in use by one or more of the virtual |
| 153 | * interfaces, and that are also valid for P2P operation. |
| 154 | */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 155 | static unsigned int |
| 156 | wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, |
| 157 | struct wpa_used_freq_data *p2p_freqs, |
| 158 | unsigned int len) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 159 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 160 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 161 | unsigned int num, i, j; |
| 162 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 163 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 164 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 165 | if (!freqs) |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 166 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 167 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 168 | num = get_shared_radio_freqs_data(wpa_s, freqs, |
| 169 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 170 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 171 | os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 172 | |
| 173 | for (i = 0, j = 0; i < num && j < len; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 174 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 175 | p2p_freqs[j++] = freqs[i]; |
| 176 | } |
| 177 | |
| 178 | os_free(freqs); |
| 179 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 180 | dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 181 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 182 | return j; |
| 183 | } |
| 184 | |
| 185 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 186 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, |
| 187 | int freq) |
| 188 | { |
| 189 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 190 | return; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 191 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 192 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 193 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 194 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", |
| 195 | freq); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 196 | freq = 0; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 197 | } |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 198 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); |
| 199 | } |
| 200 | |
| 201 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 202 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 203 | struct wpa_scan_results *scan_res) |
| 204 | { |
| 205 | size_t i; |
| 206 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 207 | if (wpa_s->p2p_scan_work) { |
| 208 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; |
| 209 | wpa_s->p2p_scan_work = NULL; |
| 210 | radio_work_done(work); |
| 211 | } |
| 212 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 213 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 214 | return; |
| 215 | |
| 216 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", |
| 217 | (int) scan_res->num); |
| 218 | |
| 219 | for (i = 0; i < scan_res->num; i++) { |
| 220 | struct wpa_scan_res *bss = scan_res->res[i]; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 221 | struct os_reltime time_tmp_age, entry_ts; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 222 | const u8 *ies; |
| 223 | size_t ies_len; |
| 224 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 225 | time_tmp_age.sec = bss->age / 1000; |
| 226 | time_tmp_age.usec = (bss->age % 1000) * 1000; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 227 | os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 228 | |
| 229 | ies = (const u8 *) (bss + 1); |
| 230 | ies_len = bss->ie_len; |
| 231 | if (bss->beacon_ie_len > 0 && |
| 232 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && |
| 233 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { |
| 234 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " |
| 235 | MACSTR, MAC2STR(bss->bssid)); |
| 236 | ies = ies + ies_len; |
| 237 | ies_len = bss->beacon_ie_len; |
| 238 | } |
| 239 | |
| 240 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 241 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 242 | bss->freq, &entry_ts, bss->level, |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 243 | ies, ies_len) > 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 244 | break; |
| 245 | } |
| 246 | |
| 247 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 248 | } |
| 249 | |
| 250 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 251 | static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) |
| 252 | { |
| 253 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 254 | struct wpa_driver_scan_params *params = work->ctx; |
| 255 | int ret; |
| 256 | |
| 257 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 258 | if (!work->started) { |
| 259 | wpa_scan_free_params(params); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | wpa_s->p2p_scan_work = NULL; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | ret = wpa_drv_scan(wpa_s, params); |
| 268 | wpa_scan_free_params(params); |
| 269 | work->ctx = NULL; |
| 270 | if (ret) { |
| 271 | radio_work_done(work); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | os_get_reltime(&wpa_s->scan_trigger_time); |
| 276 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
| 277 | wpa_s->own_scan_requested = 1; |
| 278 | wpa_s->p2p_scan_work = work; |
| 279 | } |
| 280 | |
| 281 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 282 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
| 283 | unsigned int num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 284 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | { |
| 286 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 287 | struct wpa_driver_scan_params *params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 288 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 289 | size_t ielen; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 290 | u8 *n; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | |
| 292 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 293 | return -1; |
| 294 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 295 | if (wpa_s->p2p_scan_work) { |
| 296 | wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); |
| 297 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 300 | params = os_zalloc(sizeof(*params)); |
| 301 | if (params == NULL) |
| 302 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 303 | |
| 304 | /* P2P Wildcard SSID */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 305 | params->num_ssids = 1; |
| 306 | n = os_malloc(P2P_WILDCARD_SSID_LEN); |
| 307 | if (n == NULL) |
| 308 | goto fail; |
| 309 | os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 310 | params->ssids[0].ssid = n; |
| 311 | params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 312 | |
| 313 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 314 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
| 315 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 316 | num_req_dev_types, req_dev_types); |
| 317 | if (wps_ie == NULL) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 318 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 319 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 320 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 321 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 322 | if (ies == NULL) { |
| 323 | wpabuf_free(wps_ie); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 324 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 325 | } |
| 326 | wpabuf_put_buf(ies, wps_ie); |
| 327 | wpabuf_free(wps_ie); |
| 328 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 329 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 330 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 331 | params->p2p_probe = 1; |
| 332 | n = os_malloc(wpabuf_len(ies)); |
| 333 | if (n == NULL) { |
| 334 | wpabuf_free(ies); |
| 335 | goto fail; |
| 336 | } |
| 337 | os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); |
| 338 | params->extra_ies = n; |
| 339 | params->extra_ies_len = wpabuf_len(ies); |
| 340 | wpabuf_free(ies); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 341 | |
| 342 | switch (type) { |
| 343 | case P2P_SCAN_SOCIAL: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 344 | params->freqs = os_malloc(4 * sizeof(int)); |
| 345 | if (params->freqs == NULL) |
| 346 | goto fail; |
| 347 | params->freqs[0] = 2412; |
| 348 | params->freqs[1] = 2437; |
| 349 | params->freqs[2] = 2462; |
| 350 | params->freqs[3] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 351 | break; |
| 352 | case P2P_SCAN_FULL: |
| 353 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 354 | case P2P_SCAN_SOCIAL_PLUS_ONE: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 355 | params->freqs = os_malloc(5 * sizeof(int)); |
| 356 | if (params->freqs == NULL) |
| 357 | goto fail; |
| 358 | params->freqs[0] = 2412; |
| 359 | params->freqs[1] = 2437; |
| 360 | params->freqs[2] = 2462; |
| 361 | params->freqs[3] = freq; |
| 362 | params->freqs[4] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 366 | radio_remove_works(wpa_s, "p2p-scan", 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 367 | if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, |
| 368 | params) < 0) |
| 369 | goto fail; |
| 370 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 371 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 372 | fail: |
| 373 | wpa_scan_free_params(params); |
| 374 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 378 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
| 379 | { |
| 380 | switch (p2p_group_interface) { |
| 381 | case P2P_GROUP_INTERFACE_PENDING: |
| 382 | return WPA_IF_P2P_GROUP; |
| 383 | case P2P_GROUP_INTERFACE_GO: |
| 384 | return WPA_IF_P2P_GO; |
| 385 | case P2P_GROUP_INTERFACE_CLIENT: |
| 386 | return WPA_IF_P2P_CLIENT; |
| 387 | } |
| 388 | |
| 389 | return WPA_IF_P2P_GROUP; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
| 394 | const u8 *ssid, |
| 395 | size_t ssid_len, int *go) |
| 396 | { |
| 397 | struct wpa_ssid *s; |
| 398 | |
| 399 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 400 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 401 | if (s->disabled != 0 || !s->p2p_group || |
| 402 | s->ssid_len != ssid_len || |
| 403 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
| 404 | continue; |
| 405 | if (s->mode == WPAS_MODE_P2P_GO && |
| 406 | s != wpa_s->current_ssid) |
| 407 | continue; |
| 408 | if (go) |
| 409 | *go = s->mode == WPAS_MODE_P2P_GO; |
| 410 | return wpa_s; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 418 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, |
| 419 | enum p2p_group_removal_reason removal_reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 420 | { |
| 421 | struct wpa_ssid *ssid; |
| 422 | char *gtype; |
| 423 | const char *reason; |
| 424 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 425 | ssid = wpa_s->current_ssid; |
| 426 | if (ssid == NULL) { |
| 427 | /* |
| 428 | * The current SSID was not known, but there may still be a |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 429 | * pending P2P group interface waiting for provisioning or a |
| 430 | * P2P group that is trying to reconnect. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 431 | */ |
| 432 | ssid = wpa_s->conf->ssid; |
| 433 | while (ssid) { |
Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 434 | if (ssid->p2p_group && ssid->disabled != 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 435 | break; |
| 436 | ssid = ssid->next; |
| 437 | } |
Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 438 | if (ssid == NULL && |
| 439 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) |
| 440 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 441 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " |
| 442 | "not found"); |
| 443 | return -1; |
| 444 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 445 | } |
| 446 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) |
| 447 | gtype = "GO"; |
| 448 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || |
| 449 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { |
| 450 | wpa_s->reassociate = 0; |
| 451 | wpa_s->disconnected = 1; |
| 452 | wpa_supplicant_deauthenticate(wpa_s, |
| 453 | WLAN_REASON_DEAUTH_LEAVING); |
| 454 | gtype = "client"; |
| 455 | } else |
| 456 | gtype = "GO"; |
| 457 | if (wpa_s->cross_connect_in_use) { |
| 458 | wpa_s->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 459 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 460 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 461 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 462 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 463 | switch (removal_reason) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 464 | case P2P_GROUP_REMOVAL_REQUESTED: |
| 465 | reason = " reason=REQUESTED"; |
| 466 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 467 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: |
| 468 | reason = " reason=FORMATION_FAILED"; |
| 469 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 470 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: |
| 471 | reason = " reason=IDLE"; |
| 472 | break; |
| 473 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
| 474 | reason = " reason=UNAVAILABLE"; |
| 475 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 476 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
| 477 | reason = " reason=GO_ENDING_SESSION"; |
| 478 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 479 | case P2P_GROUP_REMOVAL_PSK_FAILURE: |
| 480 | reason = " reason=PSK_FAILURE"; |
| 481 | break; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 482 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: |
| 483 | reason = " reason=FREQ_CONFLICT"; |
| 484 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 485 | default: |
| 486 | reason = ""; |
| 487 | break; |
| 488 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 489 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 490 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 491 | P2P_EVENT_GROUP_REMOVED "%s %s%s", |
| 492 | wpa_s->ifname, gtype, reason); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 493 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 494 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 495 | if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) |
| 496 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 497 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 498 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 499 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 500 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 501 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " |
| 502 | "timeout"); |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 503 | wpa_s->p2p_in_provisioning = 0; |
| 504 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 505 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 506 | wpa_s->p2p_in_invitation = 0; |
| 507 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 508 | /* |
| 509 | * Make sure wait for the first client does not remain active after the |
| 510 | * group has been removed. |
| 511 | */ |
| 512 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 513 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 514 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 515 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); |
| 516 | |
| 517 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 518 | struct wpa_global *global; |
| 519 | char *ifname; |
| 520 | enum wpa_driver_if_type type; |
| 521 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", |
| 522 | wpa_s->ifname); |
| 523 | global = wpa_s->global; |
| 524 | ifname = os_strdup(wpa_s->ifname); |
| 525 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); |
Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 526 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 527 | wpa_s = global->ifaces; |
| 528 | if (wpa_s && ifname) |
| 529 | wpa_drv_if_remove(wpa_s, type, ifname); |
| 530 | os_free(ifname); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 531 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 534 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 535 | wpa_s->global->p2p_group_formation = NULL; |
| 536 | wpa_s->p2p_in_provisioning = 0; |
| 537 | } |
| 538 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 539 | wpa_s->show_group_started = 0; |
| 540 | os_free(wpa_s->go_params); |
| 541 | wpa_s->go_params = NULL; |
| 542 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 543 | wpa_s->waiting_presence_resp = 0; |
| 544 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 545 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); |
| 546 | if (ssid && (ssid->p2p_group || |
| 547 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || |
| 548 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { |
| 549 | int id = ssid->id; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 550 | if (ssid == wpa_s->current_ssid) { |
| 551 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 552 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 553 | wpa_s->current_ssid = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 554 | } |
| 555 | /* |
| 556 | * Networks objects created during any P2P activities are not |
| 557 | * exposed out as they might/will confuse certain non-P2P aware |
| 558 | * applications since these network objects won't behave like |
| 559 | * regular ones. |
| 560 | * |
| 561 | * Likewise, we don't send out network removed signals for such |
| 562 | * network objects. |
| 563 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 564 | wpa_config_remove_network(wpa_s->conf, id); |
| 565 | wpa_supplicant_clear_status(wpa_s); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 566 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 567 | } else { |
| 568 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " |
| 569 | "found"); |
| 570 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 571 | if (wpa_s->ap_iface) |
| 572 | wpa_supplicant_ap_deinit(wpa_s); |
| 573 | else |
| 574 | wpa_drv_deinit_p2p_cli(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 575 | |
| 576 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, |
| 581 | u8 *go_dev_addr, |
| 582 | const u8 *ssid, size_t ssid_len) |
| 583 | { |
| 584 | struct wpa_bss *bss; |
| 585 | const u8 *bssid; |
| 586 | struct wpabuf *p2p; |
| 587 | u8 group_capab; |
| 588 | const u8 *addr; |
| 589 | |
| 590 | if (wpa_s->go_params) |
| 591 | bssid = wpa_s->go_params->peer_interface_addr; |
| 592 | else |
| 593 | bssid = wpa_s->bssid; |
| 594 | |
| 595 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 596 | if (bss == NULL && wpa_s->go_params && |
| 597 | !is_zero_ether_addr(wpa_s->go_params->peer_device_addr)) |
| 598 | bss = wpa_bss_get_p2p_dev_addr( |
| 599 | wpa_s, wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 600 | if (bss == NULL) { |
| 601 | u8 iface_addr[ETH_ALEN]; |
| 602 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, |
| 603 | iface_addr) == 0) |
| 604 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); |
| 605 | } |
| 606 | if (bss == NULL) { |
| 607 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 608 | "group is persistent - BSS " MACSTR " not found", |
| 609 | MAC2STR(bssid)); |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 614 | if (p2p == NULL) |
| 615 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, |
| 616 | P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 617 | if (p2p == NULL) { |
| 618 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 619 | "group is persistent - BSS " MACSTR |
| 620 | " did not include P2P IE", MAC2STR(bssid)); |
| 621 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", |
| 622 | (u8 *) (bss + 1), bss->ie_len); |
| 623 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", |
| 624 | ((u8 *) bss + 1) + bss->ie_len, |
| 625 | bss->beacon_ie_len); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | group_capab = p2p_get_group_capab(p2p); |
| 630 | addr = p2p_get_go_dev_addr(p2p); |
| 631 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " |
| 632 | "group_capab=0x%x", group_capab); |
| 633 | if (addr) { |
| 634 | os_memcpy(go_dev_addr, addr, ETH_ALEN); |
| 635 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, |
| 636 | MAC2STR(addr)); |
| 637 | } else |
| 638 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 639 | wpabuf_free(p2p); |
| 640 | |
| 641 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " |
| 642 | "go_dev_addr=" MACSTR, |
| 643 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); |
| 644 | |
| 645 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; |
| 646 | } |
| 647 | |
| 648 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 649 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
| 650 | struct wpa_ssid *ssid, |
| 651 | const u8 *go_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 652 | { |
| 653 | struct wpa_ssid *s; |
| 654 | int changed = 0; |
| 655 | |
| 656 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " |
| 657 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); |
| 658 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 659 | if (s->disabled == 2 && |
| 660 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && |
| 661 | s->ssid_len == ssid->ssid_len && |
| 662 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | if (s) { |
| 667 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " |
| 668 | "entry"); |
| 669 | if (ssid->passphrase && !s->passphrase) |
| 670 | changed = 1; |
| 671 | else if (ssid->passphrase && s->passphrase && |
| 672 | os_strcmp(ssid->passphrase, s->passphrase) != 0) |
| 673 | changed = 1; |
| 674 | } else { |
| 675 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " |
| 676 | "entry"); |
| 677 | changed = 1; |
| 678 | s = wpa_config_add_network(wpa_s->conf); |
| 679 | if (s == NULL) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 680 | return -1; |
| 681 | |
| 682 | /* |
| 683 | * Instead of network_added we emit persistent_group_added |
| 684 | * notification. Also to keep the defense checks in |
| 685 | * persistent_group obj registration method, we set the |
| 686 | * relevant flags in s to designate it as a persistent group. |
| 687 | */ |
| 688 | s->p2p_group = 1; |
| 689 | s->p2p_persistent_group = 1; |
| 690 | wpas_notify_persistent_group_added(wpa_s, s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 691 | wpa_config_set_network_defaults(s); |
| 692 | } |
| 693 | |
| 694 | s->p2p_group = 1; |
| 695 | s->p2p_persistent_group = 1; |
| 696 | s->disabled = 2; |
| 697 | s->bssid_set = 1; |
| 698 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); |
| 699 | s->mode = ssid->mode; |
| 700 | s->auth_alg = WPA_AUTH_ALG_OPEN; |
| 701 | s->key_mgmt = WPA_KEY_MGMT_PSK; |
| 702 | s->proto = WPA_PROTO_RSN; |
| 703 | s->pairwise_cipher = WPA_CIPHER_CCMP; |
| 704 | s->export_keys = 1; |
| 705 | if (ssid->passphrase) { |
| 706 | os_free(s->passphrase); |
| 707 | s->passphrase = os_strdup(ssid->passphrase); |
| 708 | } |
| 709 | if (ssid->psk_set) { |
| 710 | s->psk_set = 1; |
| 711 | os_memcpy(s->psk, ssid->psk, 32); |
| 712 | } |
| 713 | if (s->passphrase && !s->psk_set) |
| 714 | wpa_config_update_psk(s); |
| 715 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { |
| 716 | os_free(s->ssid); |
| 717 | s->ssid = os_malloc(ssid->ssid_len); |
| 718 | } |
| 719 | if (s->ssid) { |
| 720 | s->ssid_len = ssid->ssid_len; |
| 721 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); |
| 722 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 723 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { |
| 724 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); |
| 725 | wpa_s->global->add_psk = NULL; |
| 726 | changed = 1; |
| 727 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 728 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 729 | if (changed && wpa_s->conf->update_config && |
| 730 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { |
| 731 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 732 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 733 | |
| 734 | return s->id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 738 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
| 739 | const u8 *addr) |
| 740 | { |
| 741 | struct wpa_ssid *ssid, *s; |
| 742 | u8 *n; |
| 743 | size_t i; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 744 | int found = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 745 | |
| 746 | ssid = wpa_s->current_ssid; |
| 747 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 748 | !ssid->p2p_persistent_group) |
| 749 | return; |
| 750 | |
| 751 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 752 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 753 | continue; |
| 754 | |
| 755 | if (s->ssid_len == ssid->ssid_len && |
| 756 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | if (s == NULL) |
| 761 | return; |
| 762 | |
| 763 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { |
| 764 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 765 | ETH_ALEN) != 0) |
| 766 | continue; |
| 767 | |
| 768 | if (i == s->num_p2p_clients - 1) |
| 769 | return; /* already the most recent entry */ |
| 770 | |
| 771 | /* move the entry to mark it most recent */ |
| 772 | os_memmove(s->p2p_client_list + i * ETH_ALEN, |
| 773 | s->p2p_client_list + (i + 1) * ETH_ALEN, |
| 774 | (s->num_p2p_clients - i - 1) * ETH_ALEN); |
| 775 | os_memcpy(s->p2p_client_list + |
| 776 | (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN); |
| 777 | found = 1; |
| 778 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 781 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { |
| 782 | n = os_realloc_array(s->p2p_client_list, |
| 783 | s->num_p2p_clients + 1, ETH_ALEN); |
| 784 | if (n == NULL) |
| 785 | return; |
| 786 | os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); |
| 787 | s->p2p_client_list = n; |
| 788 | s->num_p2p_clients++; |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 789 | } else if (!found && s->p2p_client_list) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 790 | /* Not enough room for an additional entry - drop the oldest |
| 791 | * entry */ |
| 792 | os_memmove(s->p2p_client_list, |
| 793 | s->p2p_client_list + ETH_ALEN, |
| 794 | (s->num_p2p_clients - 1) * ETH_ALEN); |
| 795 | os_memcpy(s->p2p_client_list + |
| 796 | (s->num_p2p_clients - 1) * ETH_ALEN, |
| 797 | addr, ETH_ALEN); |
| 798 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 799 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 800 | if (wpa_s->parent->conf->update_config && |
| 801 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 802 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame^] | 806 | static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s, |
| 807 | int go, struct wpa_ssid *ssid, int freq, |
| 808 | const u8 *psk, const char *passphrase, |
| 809 | const u8 *go_dev_addr, int persistent, |
| 810 | const char *extra) |
| 811 | { |
| 812 | const char *ssid_txt; |
| 813 | char psk_txt[65]; |
| 814 | |
| 815 | if (psk) |
| 816 | wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32); |
| 817 | else |
| 818 | psk_txt[0] = '\0'; |
| 819 | |
| 820 | if (ssid) |
| 821 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 822 | else |
| 823 | ssid_txt = ""; |
| 824 | |
| 825 | if (passphrase && passphrase[0] == '\0') |
| 826 | passphrase = NULL; |
| 827 | |
| 828 | /* |
| 829 | * Include PSK/passphrase only in the control interface message and |
| 830 | * leave it out from the debug log entry. |
| 831 | */ |
| 832 | wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO, |
| 833 | P2P_EVENT_GROUP_STARTED |
| 834 | "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr=" |
| 835 | MACSTR "%s%s", |
| 836 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, |
| 837 | psk ? " psk=" : "", psk_txt, |
| 838 | passphrase ? " passphrase=\"" : "", |
| 839 | passphrase ? passphrase : "", |
| 840 | passphrase ? "\"" : "", |
| 841 | MAC2STR(go_dev_addr), |
| 842 | persistent ? " [PERSISTENT]" : "", extra); |
| 843 | wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 844 | "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s", |
| 845 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, |
| 846 | MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "", |
| 847 | extra); |
| 848 | } |
| 849 | |
| 850 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 851 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
| 852 | int success) |
| 853 | { |
| 854 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 855 | int client; |
| 856 | int persistent; |
| 857 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 858 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * This callback is likely called for the main interface. Update wpa_s |
| 862 | * to use the group interface if a new interface was created for the |
| 863 | * group. |
| 864 | */ |
| 865 | if (wpa_s->global->p2p_group_formation) |
| 866 | wpa_s = wpa_s->global->p2p_group_formation; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 867 | if (wpa_s->p2p_go_group_formation_completed) { |
| 868 | wpa_s->global->p2p_group_formation = NULL; |
| 869 | wpa_s->p2p_in_provisioning = 0; |
| 870 | } |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 871 | wpa_s->p2p_in_invitation = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 872 | |
| 873 | if (!success) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 874 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 875 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 876 | wpas_p2p_group_delete(wpa_s, |
| 877 | P2P_GROUP_REMOVAL_FORMATION_FAILED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 878 | return; |
| 879 | } |
| 880 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 881 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 882 | P2P_EVENT_GROUP_FORMATION_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 883 | |
| 884 | ssid = wpa_s->current_ssid; |
| 885 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 886 | ssid->mode = WPAS_MODE_P2P_GO; |
| 887 | p2p_group_notif_formation_done(wpa_s->p2p_group); |
| 888 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); |
| 889 | } |
| 890 | |
| 891 | persistent = 0; |
| 892 | if (ssid) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 893 | client = ssid->mode == WPAS_MODE_INFRA; |
| 894 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 895 | persistent = ssid->p2p_persistent_group; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 896 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
| 897 | ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 898 | } else |
| 899 | persistent = wpas_p2p_persistent_group(wpa_s, |
| 900 | go_dev_addr, |
| 901 | ssid->ssid, |
| 902 | ssid->ssid_len); |
| 903 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 904 | client = wpa_s->p2p_group_interface == |
| 905 | P2P_GROUP_INTERFACE_CLIENT; |
| 906 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 907 | } |
| 908 | |
| 909 | wpa_s->show_group_started = 0; |
| 910 | if (client) { |
| 911 | /* |
| 912 | * Indicate event only after successfully completed 4-way |
| 913 | * handshake, i.e., when the interface is ready for data |
| 914 | * packets. |
| 915 | */ |
| 916 | wpa_s->show_group_started = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 917 | } else { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame^] | 918 | wpas_p2p_group_started(wpa_s, 1, ssid, |
| 919 | ssid ? ssid->frequency : 0, |
| 920 | ssid && ssid->passphrase == NULL && |
| 921 | ssid->psk_set ? ssid->psk : NULL, |
| 922 | ssid ? ssid->passphrase : NULL, |
| 923 | go_dev_addr, persistent, ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 924 | wpas_p2p_cross_connect_setup(wpa_s); |
| 925 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 926 | } |
| 927 | |
| 928 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 929 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 930 | ssid, go_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 931 | else { |
| 932 | os_free(wpa_s->global->add_psk); |
| 933 | wpa_s->global->add_psk = NULL; |
| 934 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 935 | if (network_id < 0 && ssid) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 936 | network_id = ssid->id; |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 937 | if (!client) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 938 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 939 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 940 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 944 | struct send_action_work { |
| 945 | unsigned int freq; |
| 946 | u8 dst[ETH_ALEN]; |
| 947 | u8 src[ETH_ALEN]; |
| 948 | u8 bssid[ETH_ALEN]; |
| 949 | size_t len; |
| 950 | unsigned int wait_time; |
| 951 | u8 buf[0]; |
| 952 | }; |
| 953 | |
| 954 | |
| 955 | static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, |
| 956 | void *timeout_ctx) |
| 957 | { |
| 958 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 959 | |
| 960 | if (!wpa_s->p2p_send_action_work) |
| 961 | return; |
| 962 | |
| 963 | wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); |
| 964 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 965 | radio_work_done(wpa_s->p2p_send_action_work); |
| 966 | wpa_s->p2p_send_action_work = NULL; |
| 967 | } |
| 968 | |
| 969 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 970 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
| 971 | unsigned int freq, |
| 972 | const u8 *dst, const u8 *src, |
| 973 | const u8 *bssid, |
| 974 | const u8 *data, size_t data_len, |
| 975 | enum offchannel_send_action_result |
| 976 | result) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 977 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 978 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 979 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 980 | if (wpa_s->p2p_send_action_work) { |
| 981 | struct send_action_work *awork; |
| 982 | awork = wpa_s->p2p_send_action_work->ctx; |
| 983 | if (awork->wait_time == 0) { |
| 984 | os_free(awork); |
| 985 | radio_work_done(wpa_s->p2p_send_action_work); |
| 986 | wpa_s->p2p_send_action_work = NULL; |
| 987 | } else { |
| 988 | /* |
| 989 | * In theory, this should not be needed, but number of |
| 990 | * places in the P2P code is still using non-zero wait |
| 991 | * time for the last Action frame in the sequence and |
| 992 | * some of these do not call send_action_done(). |
| 993 | */ |
| 994 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 995 | wpa_s, NULL); |
| 996 | eloop_register_timeout( |
| 997 | 0, awork->wait_time * 1000, |
| 998 | wpas_p2p_send_action_work_timeout, |
| 999 | wpa_s, NULL); |
| 1000 | } |
| 1001 | } |
| 1002 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1003 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 1004 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1005 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1006 | switch (result) { |
| 1007 | case OFFCHANNEL_SEND_ACTION_SUCCESS: |
| 1008 | res = P2P_SEND_ACTION_SUCCESS; |
| 1009 | break; |
| 1010 | case OFFCHANNEL_SEND_ACTION_NO_ACK: |
| 1011 | res = P2P_SEND_ACTION_NO_ACK; |
| 1012 | break; |
| 1013 | case OFFCHANNEL_SEND_ACTION_FAILED: |
| 1014 | res = P2P_SEND_ACTION_FAILED; |
| 1015 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1018 | p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1019 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1020 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
| 1021 | wpa_s->pending_pd_before_join && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1022 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1023 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && |
| 1024 | wpa_s->p2p_fallback_to_go_neg) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1025 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1026 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " |
| 1027 | "during p2p_connect-auto"); |
| 1028 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 1029 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1034 | static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) |
| 1035 | { |
| 1036 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1037 | struct send_action_work *awork = work->ctx; |
| 1038 | |
| 1039 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1040 | if (work->started) { |
| 1041 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1042 | wpa_s, NULL); |
| 1043 | wpa_s->p2p_send_action_work = NULL; |
| 1044 | offchannel_send_action_done(wpa_s); |
| 1045 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1046 | os_free(awork); |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, |
| 1051 | awork->bssid, awork->buf, awork->len, |
| 1052 | awork->wait_time, |
| 1053 | wpas_p2p_send_action_tx_status, 1) < 0) { |
| 1054 | os_free(awork); |
| 1055 | radio_work_done(work); |
| 1056 | return; |
| 1057 | } |
| 1058 | wpa_s->p2p_send_action_work = work; |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | static int wpas_send_action_work(struct wpa_supplicant *wpa_s, |
| 1063 | unsigned int freq, const u8 *dst, |
| 1064 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1065 | size_t len, unsigned int wait_time) |
| 1066 | { |
| 1067 | struct send_action_work *awork; |
| 1068 | |
| 1069 | if (wpa_s->p2p_send_action_work) { |
| 1070 | wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
| 1074 | awork = os_zalloc(sizeof(*awork) + len); |
| 1075 | if (awork == NULL) |
| 1076 | return -1; |
| 1077 | |
| 1078 | awork->freq = freq; |
| 1079 | os_memcpy(awork->dst, dst, ETH_ALEN); |
| 1080 | os_memcpy(awork->src, src, ETH_ALEN); |
| 1081 | os_memcpy(awork->bssid, bssid, ETH_ALEN); |
| 1082 | awork->len = len; |
| 1083 | awork->wait_time = wait_time; |
| 1084 | os_memcpy(awork->buf, buf, len); |
| 1085 | |
| 1086 | if (radio_add_work(wpa_s, freq, "p2p-send-action", 0, |
| 1087 | wpas_send_action_cb, awork) < 0) { |
| 1088 | os_free(awork); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1096 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, |
| 1097 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1098 | size_t len, unsigned int wait_time) |
| 1099 | { |
| 1100 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1101 | int listen_freq = -1, send_freq = -1; |
| 1102 | |
| 1103 | if (wpa_s->p2p_listen_work) |
| 1104 | listen_freq = wpa_s->p2p_listen_work->freq; |
| 1105 | if (wpa_s->p2p_send_action_work) |
| 1106 | send_freq = wpa_s->p2p_send_action_work->freq; |
| 1107 | if (listen_freq != (int) freq && send_freq != (int) freq) { |
| 1108 | wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)", |
| 1109 | listen_freq, send_freq); |
| 1110 | return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, |
| 1111 | len, wait_time); |
| 1112 | } |
| 1113 | |
| 1114 | wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1115 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
| 1116 | wait_time, |
| 1117 | wpas_p2p_send_action_tx_status, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | static void wpas_send_action_done(void *ctx) |
| 1122 | { |
| 1123 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1124 | |
| 1125 | if (wpa_s->p2p_send_action_work) { |
| 1126 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1127 | wpa_s, NULL); |
| 1128 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 1129 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1130 | wpa_s->p2p_send_action_work = NULL; |
| 1131 | } |
| 1132 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1133 | offchannel_send_action_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, |
| 1138 | struct p2p_go_neg_results *params) |
| 1139 | { |
| 1140 | if (wpa_s->go_params == NULL) { |
| 1141 | wpa_s->go_params = os_malloc(sizeof(*params)); |
| 1142 | if (wpa_s->go_params == NULL) |
| 1143 | return -1; |
| 1144 | } |
| 1145 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, |
| 1151 | struct p2p_go_neg_results *res) |
| 1152 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1153 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR |
| 1154 | " dev_addr " MACSTR " wps_method %d", |
| 1155 | MAC2STR(res->peer_interface_addr), |
| 1156 | MAC2STR(res->peer_device_addr), res->wps_method); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1157 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
| 1158 | res->ssid, res->ssid_len); |
| 1159 | wpa_supplicant_ap_deinit(wpa_s); |
| 1160 | wpas_copy_go_neg_results(wpa_s, res); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1161 | if (res->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1162 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1163 | #ifdef CONFIG_WPS_NFC |
| 1164 | } else if (res->wps_method == WPS_NFC) { |
| 1165 | wpas_wps_start_nfc(wpa_s, res->peer_device_addr, |
| 1166 | res->peer_interface_addr, |
| 1167 | wpa_s->parent->p2p_oob_dev_pw, |
| 1168 | wpa_s->parent->p2p_oob_dev_pw_id, 1, |
| 1169 | wpa_s->parent->p2p_oob_dev_pw_id == |
| 1170 | DEV_PW_NFC_CONNECTION_HANDOVER ? |
| 1171 | wpa_s->parent->p2p_peer_oob_pubkey_hash : |
| 1172 | NULL, |
| 1173 | NULL, 0, 0); |
| 1174 | #endif /* CONFIG_WPS_NFC */ |
| 1175 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1176 | u16 dev_pw_id = DEV_PW_DEFAULT; |
| 1177 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) |
| 1178 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; |
| 1179 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
| 1180 | wpa_s->p2p_pin, 1, dev_pw_id); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1185 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, |
| 1186 | struct wpa_ssid *ssid) |
| 1187 | { |
| 1188 | struct wpa_ssid *persistent; |
| 1189 | struct psk_list_entry *psk; |
| 1190 | struct hostapd_data *hapd; |
| 1191 | |
| 1192 | if (!wpa_s->ap_iface) |
| 1193 | return; |
| 1194 | |
| 1195 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 1196 | ssid->ssid_len); |
| 1197 | if (persistent == NULL) |
| 1198 | return; |
| 1199 | |
| 1200 | hapd = wpa_s->ap_iface->bss[0]; |
| 1201 | |
| 1202 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, |
| 1203 | list) { |
| 1204 | struct hostapd_wpa_psk *hpsk; |
| 1205 | |
| 1206 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " |
| 1207 | MACSTR " psk=%d", |
| 1208 | MAC2STR(psk->addr), psk->p2p); |
| 1209 | hpsk = os_zalloc(sizeof(*hpsk)); |
| 1210 | if (hpsk == NULL) |
| 1211 | break; |
| 1212 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); |
| 1213 | if (psk->p2p) |
| 1214 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); |
| 1215 | else |
| 1216 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); |
| 1217 | hpsk->next = hapd->conf->ssid.wpa_psk; |
| 1218 | hapd->conf->ssid.wpa_psk = hpsk; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1223 | static void p2p_go_configured(void *ctx, void *data) |
| 1224 | { |
| 1225 | struct wpa_supplicant *wpa_s = ctx; |
| 1226 | struct p2p_go_neg_results *params = data; |
| 1227 | struct wpa_ssid *ssid; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1228 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1229 | |
| 1230 | ssid = wpa_s->current_ssid; |
| 1231 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { |
| 1232 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); |
| 1233 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 1234 | wpa_s->global->p2p_group_formation = NULL; |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame^] | 1235 | wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency, |
| 1236 | params->passphrase[0] == '\0' ? |
| 1237 | params->psk : NULL, |
| 1238 | params->passphrase, |
| 1239 | wpa_s->global->p2p_dev_addr, |
| 1240 | params->persistent_group, ""); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1241 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1242 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1243 | if (params->persistent_group) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1244 | network_id = wpas_p2p_store_persistent_group( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1245 | wpa_s->parent, ssid, |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1246 | wpa_s->global->p2p_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1247 | wpas_p2p_add_psk_list(wpa_s, ssid); |
| 1248 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1249 | if (network_id < 0) |
| 1250 | network_id = ssid->id; |
| 1251 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1252 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1253 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1254 | |
| 1255 | if (wpa_s->p2p_first_connection_timeout) { |
| 1256 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1257 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", |
| 1258 | wpa_s->p2p_first_connection_timeout); |
| 1259 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1260 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1261 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1262 | wpa_s->parent, NULL); |
| 1263 | eloop_register_timeout( |
| 1264 | wpa_s->p2p_first_connection_timeout, 0, |
| 1265 | wpas_p2p_group_formation_timeout, |
| 1266 | wpa_s->parent, NULL); |
| 1267 | } |
| 1268 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); |
| 1273 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, |
| 1274 | params->peer_interface_addr)) { |
| 1275 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " |
| 1276 | "filtering"); |
| 1277 | return; |
| 1278 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1279 | if (params->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1280 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1281 | params->peer_device_addr); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1282 | #ifdef CONFIG_WPS_NFC |
| 1283 | } else if (params->wps_method == WPS_NFC) { |
| 1284 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 1285 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 1286 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 1287 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 1288 | return; |
| 1289 | } |
| 1290 | wpas_ap_wps_add_nfc_pw( |
| 1291 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 1292 | wpa_s->parent->p2p_oob_dev_pw, |
| 1293 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 1294 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 1295 | #endif /* CONFIG_WPS_NFC */ |
| 1296 | } else if (wpa_s->p2p_pin[0]) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1297 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1298 | wpa_s->p2p_pin, NULL, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1299 | os_free(wpa_s->go_params); |
| 1300 | wpa_s->go_params = NULL; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, |
| 1305 | struct p2p_go_neg_results *params, |
| 1306 | int group_formation) |
| 1307 | { |
| 1308 | struct wpa_ssid *ssid; |
| 1309 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1310 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); |
| 1311 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { |
| 1312 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " |
| 1313 | "results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1314 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1315 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1316 | |
| 1317 | ssid = wpa_config_add_network(wpa_s->conf); |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1318 | if (ssid == NULL) { |
| 1319 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1320 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1321 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1322 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1323 | wpa_s->show_group_started = 0; |
| 1324 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1325 | wpa_config_set_network_defaults(ssid); |
| 1326 | ssid->temporary = 1; |
| 1327 | ssid->p2p_group = 1; |
| 1328 | ssid->p2p_persistent_group = params->persistent_group; |
| 1329 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : |
| 1330 | WPAS_MODE_P2P_GO; |
| 1331 | ssid->frequency = params->freq; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1332 | ssid->ht40 = params->ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1333 | ssid->vht = params->vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1334 | ssid->ssid = os_zalloc(params->ssid_len + 1); |
| 1335 | if (ssid->ssid) { |
| 1336 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 1337 | ssid->ssid_len = params->ssid_len; |
| 1338 | } |
| 1339 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1340 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1341 | ssid->proto = WPA_PROTO_RSN; |
| 1342 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1343 | if (os_strlen(params->passphrase) > 0) { |
| 1344 | ssid->passphrase = os_strdup(params->passphrase); |
| 1345 | if (ssid->passphrase == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1346 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 1347 | "P2P: Failed to copy passphrase for GO"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1348 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1349 | return; |
| 1350 | } |
| 1351 | } else |
| 1352 | ssid->passphrase = NULL; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1353 | ssid->psk_set = params->psk_set; |
| 1354 | if (ssid->psk_set) |
| 1355 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1356 | else if (ssid->passphrase) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1357 | wpa_config_update_psk(ssid); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1358 | ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1359 | |
| 1360 | wpa_s->ap_configured_cb = p2p_go_configured; |
| 1361 | wpa_s->ap_configured_cb_ctx = wpa_s; |
| 1362 | wpa_s->ap_configured_cb_data = wpa_s->go_params; |
Dmitry Shmidt | 6dc03bd | 2014-05-16 10:40:13 -0700 | [diff] [blame] | 1363 | wpa_s->scan_req = NORMAL_SCAN_REQ; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1364 | wpa_s->connect_without_scan = ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1365 | wpa_s->reassociate = 1; |
| 1366 | wpa_s->disconnected = 0; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1367 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " |
| 1368 | "start GO)"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1369 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, |
| 1374 | const struct wpa_supplicant *src) |
| 1375 | { |
| 1376 | struct wpa_config *d; |
| 1377 | const struct wpa_config *s; |
| 1378 | |
| 1379 | d = dst->conf; |
| 1380 | s = src->conf; |
| 1381 | |
| 1382 | #define C(n) if (s->n) d->n = os_strdup(s->n) |
| 1383 | C(device_name); |
| 1384 | C(manufacturer); |
| 1385 | C(model_name); |
| 1386 | C(model_number); |
| 1387 | C(serial_number); |
| 1388 | C(config_methods); |
| 1389 | #undef C |
| 1390 | |
| 1391 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
| 1392 | os_memcpy(d->sec_device_type, s->sec_device_type, |
| 1393 | sizeof(d->sec_device_type)); |
| 1394 | d->num_sec_device_types = s->num_sec_device_types; |
| 1395 | |
| 1396 | d->p2p_group_idle = s->p2p_group_idle; |
| 1397 | d->p2p_intra_bss = s->p2p_intra_bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1398 | d->persistent_reconnect = s->persistent_reconnect; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1399 | d->max_num_sta = s->max_num_sta; |
| 1400 | d->pbc_in_m1 = s->pbc_in_m1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1401 | d->ignore_old_scan_res = s->ignore_old_scan_res; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1402 | d->beacon_int = s->beacon_int; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 1403 | d->dtim_period = s->dtim_period; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1404 | d->disassoc_low_ack = s->disassoc_low_ack; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1405 | d->disable_scan_offload = s->disable_scan_offload; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1406 | |
| 1407 | if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) { |
| 1408 | d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey); |
| 1409 | d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey); |
| 1410 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1414 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, |
| 1415 | char *ifname, size_t len) |
| 1416 | { |
| 1417 | char *ifname_ptr = wpa_s->ifname; |
| 1418 | |
| 1419 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, |
| 1420 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 1421 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; |
| 1422 | } |
| 1423 | |
| 1424 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); |
| 1425 | if (os_strlen(ifname) >= IFNAMSIZ && |
| 1426 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { |
| 1427 | /* Try to avoid going over the IFNAMSIZ length limit */ |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1428 | os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1433 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 1434 | enum wpa_driver_if_type type) |
| 1435 | { |
| 1436 | char ifname[120], force_ifname[120]; |
| 1437 | |
| 1438 | if (wpa_s->pending_interface_name[0]) { |
| 1439 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " |
| 1440 | "- skip creation of a new one"); |
| 1441 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { |
| 1442 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " |
| 1443 | "unknown?! ifname='%s'", |
| 1444 | wpa_s->pending_interface_name); |
| 1445 | return -1; |
| 1446 | } |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1450 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1451 | force_ifname[0] = '\0'; |
| 1452 | |
| 1453 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", |
| 1454 | ifname); |
| 1455 | wpa_s->p2p_group_idx++; |
| 1456 | |
| 1457 | wpa_s->pending_interface_type = type; |
| 1458 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, |
| 1459 | wpa_s->pending_interface_addr, NULL) < 0) { |
| 1460 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
| 1461 | "interface"); |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
| 1465 | if (force_ifname[0]) { |
| 1466 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", |
| 1467 | force_ifname); |
| 1468 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, |
| 1469 | sizeof(wpa_s->pending_interface_name)); |
| 1470 | } else |
| 1471 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 1472 | sizeof(wpa_s->pending_interface_name)); |
| 1473 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " |
| 1474 | MACSTR, wpa_s->pending_interface_name, |
| 1475 | MAC2STR(wpa_s->pending_interface_addr)); |
| 1476 | |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | static void wpas_p2p_remove_pending_group_interface( |
| 1482 | struct wpa_supplicant *wpa_s) |
| 1483 | { |
| 1484 | if (!wpa_s->pending_interface_name[0] || |
| 1485 | is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 1486 | return; /* No pending virtual interface */ |
| 1487 | |
| 1488 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", |
| 1489 | wpa_s->pending_interface_name); |
| 1490 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, |
| 1491 | wpa_s->pending_interface_name); |
| 1492 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1493 | wpa_s->pending_interface_name[0] = '\0'; |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | static struct wpa_supplicant * |
| 1498 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) |
| 1499 | { |
| 1500 | struct wpa_interface iface; |
| 1501 | struct wpa_supplicant *group_wpa_s; |
| 1502 | |
| 1503 | if (!wpa_s->pending_interface_name[0]) { |
| 1504 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); |
| 1505 | if (!wpas_p2p_create_iface(wpa_s)) |
| 1506 | return NULL; |
| 1507 | /* |
| 1508 | * Something has forced us to remove the pending interface; try |
| 1509 | * to create a new one and hope for the best that we will get |
| 1510 | * the same local address. |
| 1511 | */ |
| 1512 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
| 1513 | WPA_IF_P2P_CLIENT) < 0) |
| 1514 | return NULL; |
| 1515 | } |
| 1516 | |
| 1517 | os_memset(&iface, 0, sizeof(iface)); |
| 1518 | iface.ifname = wpa_s->pending_interface_name; |
| 1519 | iface.driver = wpa_s->driver->name; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1520 | if (wpa_s->conf->ctrl_interface == NULL && |
| 1521 | wpa_s->parent != wpa_s && |
| 1522 | wpa_s->p2p_mgmt && |
| 1523 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) |
| 1524 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; |
| 1525 | else |
| 1526 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1527 | iface.driver_param = wpa_s->conf->driver_param; |
| 1528 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 1529 | if (group_wpa_s == NULL) { |
| 1530 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " |
| 1531 | "wpa_supplicant interface"); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | wpa_s->pending_interface_name[0] = '\0'; |
| 1535 | group_wpa_s->parent = wpa_s; |
| 1536 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : |
| 1537 | P2P_GROUP_INTERFACE_CLIENT; |
| 1538 | wpa_s->global->p2p_group_formation = group_wpa_s; |
| 1539 | |
| 1540 | wpas_p2p_clone_config(group_wpa_s, wpa_s); |
| 1541 | |
| 1542 | return group_wpa_s; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 1547 | void *timeout_ctx) |
| 1548 | { |
| 1549 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 1550 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1551 | wpas_p2p_group_formation_failed(wpa_s); |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s) |
| 1556 | { |
| 1557 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1558 | wpa_s->parent, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1559 | if (wpa_s->global->p2p) |
| 1560 | p2p_group_formation_failed(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1561 | wpas_group_formation_completed(wpa_s, 0); |
| 1562 | } |
| 1563 | |
| 1564 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1565 | static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) |
| 1566 | { |
| 1567 | wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); |
| 1568 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1569 | wpa_s->parent, NULL); |
| 1570 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1571 | wpa_s->parent, NULL); |
| 1572 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 1573 | } |
| 1574 | |
| 1575 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1576 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) |
| 1577 | { |
| 1578 | if (wpa_s->global->p2p_group_formation != wpa_s) |
| 1579 | return; |
| 1580 | /* Speed up group formation timeout since this cannot succeed */ |
| 1581 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1582 | wpa_s->parent, NULL); |
| 1583 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1584 | wpa_s->parent, NULL); |
| 1585 | } |
| 1586 | |
| 1587 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1588 | static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1589 | { |
| 1590 | struct wpa_supplicant *wpa_s = ctx; |
| 1591 | |
| 1592 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1593 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1594 | wpa_s->off_channel_freq = 0; |
| 1595 | wpa_s->roc_waiting_drv_freq = 0; |
| 1596 | } |
| 1597 | |
| 1598 | if (res->status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1599 | wpa_msg_global(wpa_s, MSG_INFO, |
| 1600 | P2P_EVENT_GO_NEG_FAILURE "status=%d", |
| 1601 | res->status); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1602 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1603 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1604 | return; |
| 1605 | } |
| 1606 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1607 | if (wpa_s->p2p_go_ht40) |
| 1608 | res->ht40 = 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1609 | if (wpa_s->p2p_go_vht) |
| 1610 | res->vht = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1611 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1612 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " |
| 1613 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR |
| 1614 | " wps_method=%s", |
| 1615 | res->role_go ? "GO" : "client", res->freq, res->ht40, |
| 1616 | MAC2STR(res->peer_device_addr), |
| 1617 | MAC2STR(res->peer_interface_addr), |
| 1618 | p2p_wps_method_text(res->wps_method)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1619 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1620 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1621 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
| 1622 | struct wpa_ssid *ssid; |
| 1623 | ssid = wpa_config_get_network(wpa_s->conf, |
| 1624 | wpa_s->p2p_persistent_id); |
| 1625 | if (ssid && ssid->disabled == 2 && |
| 1626 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { |
| 1627 | size_t len = os_strlen(ssid->passphrase); |
| 1628 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " |
| 1629 | "on requested persistent group"); |
| 1630 | os_memcpy(res->passphrase, ssid->passphrase, len); |
| 1631 | res->passphrase[len] = '\0'; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1635 | if (wpa_s->create_p2p_iface) { |
| 1636 | struct wpa_supplicant *group_wpa_s = |
| 1637 | wpas_p2p_init_group_interface(wpa_s, res->role_go); |
| 1638 | if (group_wpa_s == NULL) { |
| 1639 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 1640 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, |
| 1641 | wpa_s, NULL); |
| 1642 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1643 | return; |
| 1644 | } |
| 1645 | if (group_wpa_s != wpa_s) { |
| 1646 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
| 1647 | sizeof(group_wpa_s->p2p_pin)); |
| 1648 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
| 1649 | } |
| 1650 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1651 | wpa_s->pending_interface_name[0] = '\0'; |
| 1652 | group_wpa_s->p2p_in_provisioning = 1; |
| 1653 | |
| 1654 | if (res->role_go) |
| 1655 | wpas_start_wps_go(group_wpa_s, res, 1); |
| 1656 | else |
| 1657 | wpas_start_wps_enrollee(group_wpa_s, res); |
| 1658 | } else { |
| 1659 | wpa_s->p2p_in_provisioning = 1; |
| 1660 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1661 | |
| 1662 | if (res->role_go) |
| 1663 | wpas_start_wps_go(wpa_s, res, 1); |
| 1664 | else |
| 1665 | wpas_start_wps_enrollee(ctx, res); |
| 1666 | } |
| 1667 | |
| 1668 | wpa_s->p2p_long_listen = 0; |
| 1669 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 1670 | |
| 1671 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1672 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
| 1673 | (res->peer_config_timeout % 100) * 10000, |
| 1674 | wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1675 | } |
| 1676 | |
| 1677 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1678 | static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1679 | { |
| 1680 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1681 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
| 1682 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1683 | |
| 1684 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); |
| 1685 | } |
| 1686 | |
| 1687 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1688 | static void wpas_dev_found(void *ctx, const u8 *addr, |
| 1689 | const struct p2p_peer_info *info, |
| 1690 | int new_device) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1691 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1692 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1693 | struct wpa_supplicant *wpa_s = ctx; |
| 1694 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1695 | char *wfd_dev_info_hex = NULL; |
| 1696 | |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1697 | #ifdef CONFIG_WIFI_DISPLAY |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1698 | wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, |
| 1699 | WFD_SUBELEM_DEVICE_INFO); |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1700 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1701 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1702 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
| 1703 | " p2p_dev_addr=" MACSTR |
| 1704 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 1705 | "dev_capab=0x%x group_capab=0x%x%s%s%s", |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1706 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
| 1707 | wps_dev_type_bin2str(info->pri_dev_type, devtype, |
| 1708 | sizeof(devtype)), |
| 1709 | info->device_name, info->config_methods, |
| 1710 | info->dev_capab, info->group_capab, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1711 | wfd_dev_info_hex ? " wfd_dev_info=0x" : "", |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 1712 | wfd_dev_info_hex ? wfd_dev_info_hex : "", |
| 1713 | info->vendor_elems ? " vendor_elems=1" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1714 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1715 | os_free(wfd_dev_info_hex); |
Dmitry Shmidt | 9767226 | 2014-02-03 13:02:54 -0800 | [diff] [blame] | 1716 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1717 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1718 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
| 1723 | { |
| 1724 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1725 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1726 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
| 1727 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1728 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1729 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
| 1730 | } |
| 1731 | |
| 1732 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1733 | static void wpas_find_stopped(void *ctx) |
| 1734 | { |
| 1735 | struct wpa_supplicant *wpa_s = ctx; |
| 1736 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
| 1737 | } |
| 1738 | |
| 1739 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1740 | struct wpas_p2p_listen_work { |
| 1741 | unsigned int freq; |
| 1742 | unsigned int duration; |
| 1743 | struct wpabuf *probe_resp_ie; |
| 1744 | }; |
| 1745 | |
| 1746 | |
| 1747 | static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) |
| 1748 | { |
| 1749 | if (lwork == NULL) |
| 1750 | return; |
| 1751 | wpabuf_free(lwork->probe_resp_ie); |
| 1752 | os_free(lwork); |
| 1753 | } |
| 1754 | |
| 1755 | |
| 1756 | static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) |
| 1757 | { |
| 1758 | struct wpas_p2p_listen_work *lwork; |
| 1759 | |
| 1760 | if (!wpa_s->p2p_listen_work) |
| 1761 | return; |
| 1762 | |
| 1763 | lwork = wpa_s->p2p_listen_work->ctx; |
| 1764 | wpas_p2p_listen_work_free(lwork); |
| 1765 | radio_work_done(wpa_s->p2p_listen_work); |
| 1766 | wpa_s->p2p_listen_work = NULL; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) |
| 1771 | { |
| 1772 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1773 | struct wpas_p2p_listen_work *lwork = work->ctx; |
| 1774 | |
| 1775 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1776 | if (work->started) { |
| 1777 | wpa_s->p2p_listen_work = NULL; |
| 1778 | wpas_stop_listen(wpa_s); |
| 1779 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1780 | wpas_p2p_listen_work_free(lwork); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | wpa_s->p2p_listen_work = work; |
| 1785 | |
| 1786 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); |
| 1787 | |
| 1788 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { |
| 1789 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " |
| 1790 | "report received Probe Request frames"); |
| 1791 | wpas_p2p_listen_work_done(wpa_s); |
| 1792 | return; |
| 1793 | } |
| 1794 | |
| 1795 | wpa_s->pending_listen_freq = lwork->freq; |
| 1796 | wpa_s->pending_listen_duration = lwork->duration; |
| 1797 | |
| 1798 | if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0) |
| 1799 | { |
| 1800 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " |
| 1801 | "to remain on channel (%u MHz) for Listen " |
| 1802 | "state", lwork->freq); |
| 1803 | wpas_p2p_listen_work_done(wpa_s); |
| 1804 | wpa_s->pending_listen_freq = 0; |
| 1805 | return; |
| 1806 | } |
| 1807 | wpa_s->off_channel_freq = 0; |
| 1808 | wpa_s->roc_waiting_drv_freq = lwork->freq; |
| 1809 | } |
| 1810 | |
| 1811 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1812 | static int wpas_start_listen(void *ctx, unsigned int freq, |
| 1813 | unsigned int duration, |
| 1814 | const struct wpabuf *probe_resp_ie) |
| 1815 | { |
| 1816 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1817 | struct wpas_p2p_listen_work *lwork; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1818 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1819 | if (wpa_s->p2p_listen_work) { |
| 1820 | wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1821 | return -1; |
| 1822 | } |
| 1823 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1824 | lwork = os_zalloc(sizeof(*lwork)); |
| 1825 | if (lwork == NULL) |
| 1826 | return -1; |
| 1827 | lwork->freq = freq; |
| 1828 | lwork->duration = duration; |
| 1829 | if (probe_resp_ie) { |
| 1830 | lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); |
| 1831 | if (lwork->probe_resp_ie == NULL) { |
| 1832 | wpas_p2p_listen_work_free(lwork); |
| 1833 | return -1; |
| 1834 | } |
| 1835 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1836 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1837 | if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, |
| 1838 | lwork) < 0) { |
| 1839 | wpas_p2p_listen_work_free(lwork); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1840 | return -1; |
| 1841 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1842 | |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | |
| 1847 | static void wpas_stop_listen(void *ctx) |
| 1848 | { |
| 1849 | struct wpa_supplicant *wpa_s = ctx; |
| 1850 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1851 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1852 | wpa_s->off_channel_freq = 0; |
| 1853 | wpa_s->roc_waiting_drv_freq = 0; |
| 1854 | } |
| 1855 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
| 1856 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1857 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | |
| 1861 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) |
| 1862 | { |
| 1863 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1864 | return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1868 | /* |
| 1869 | * DNS Header section is used only to calculate compression pointers, so the |
| 1870 | * contents of this data does not matter, but the length needs to be reserved |
| 1871 | * in the virtual packet. |
| 1872 | */ |
| 1873 | #define DNS_HEADER_LEN 12 |
| 1874 | |
| 1875 | /* |
| 1876 | * 27-octet in-memory packet from P2P specification containing two implied |
| 1877 | * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN |
| 1878 | */ |
| 1879 | #define P2P_SD_IN_MEMORY_LEN 27 |
| 1880 | |
| 1881 | static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start, |
| 1882 | u8 **spos, const u8 *end) |
| 1883 | { |
| 1884 | while (*spos < end) { |
| 1885 | u8 val = ((*spos)[0] & 0xc0) >> 6; |
| 1886 | int len; |
| 1887 | |
| 1888 | if (val == 1 || val == 2) { |
| 1889 | /* These are reserved values in RFC 1035 */ |
| 1890 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1891 | "sequence starting with 0x%x", val); |
| 1892 | return -1; |
| 1893 | } |
| 1894 | |
| 1895 | if (val == 3) { |
| 1896 | u16 offset; |
| 1897 | u8 *spos_tmp; |
| 1898 | |
| 1899 | /* Offset */ |
| 1900 | if (*spos + 2 > end) { |
| 1901 | wpa_printf(MSG_DEBUG, "P2P: No room for full " |
| 1902 | "DNS offset field"); |
| 1903 | return -1; |
| 1904 | } |
| 1905 | |
| 1906 | offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1]; |
| 1907 | if (offset >= *spos - start) { |
| 1908 | wpa_printf(MSG_DEBUG, "P2P: Invalid DNS " |
| 1909 | "pointer offset %u", offset); |
| 1910 | return -1; |
| 1911 | } |
| 1912 | |
| 1913 | (*spos) += 2; |
| 1914 | spos_tmp = start + offset; |
| 1915 | return p2p_sd_dns_uncompress_label(upos, uend, start, |
| 1916 | &spos_tmp, |
| 1917 | *spos - 2); |
| 1918 | } |
| 1919 | |
| 1920 | /* Label */ |
| 1921 | len = (*spos)[0] & 0x3f; |
| 1922 | if (len == 0) |
| 1923 | return 0; |
| 1924 | |
| 1925 | (*spos)++; |
| 1926 | if (*spos + len > end) { |
| 1927 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1928 | "sequence - no room for label with length " |
| 1929 | "%u", len); |
| 1930 | return -1; |
| 1931 | } |
| 1932 | |
| 1933 | if (*upos + len + 2 > uend) |
| 1934 | return -2; |
| 1935 | |
| 1936 | os_memcpy(*upos, *spos, len); |
| 1937 | *spos += len; |
| 1938 | *upos += len; |
| 1939 | (*upos)[0] = '.'; |
| 1940 | (*upos)++; |
| 1941 | (*upos)[0] = '\0'; |
| 1942 | } |
| 1943 | |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | |
| 1948 | /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet. |
| 1949 | * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is |
| 1950 | * not large enough */ |
| 1951 | static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg, |
| 1952 | size_t msg_len, size_t offset) |
| 1953 | { |
| 1954 | /* 27-octet in-memory packet from P2P specification */ |
| 1955 | const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01" |
| 1956 | "\x04_udp\xC0\x11\x00\x0C\x00\x01"; |
| 1957 | u8 *tmp, *end, *spos; |
| 1958 | char *upos, *uend; |
| 1959 | int ret = 0; |
| 1960 | |
| 1961 | if (buf_len < 2) |
| 1962 | return -1; |
| 1963 | if (offset > msg_len) |
| 1964 | return -1; |
| 1965 | |
| 1966 | tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len); |
| 1967 | if (tmp == NULL) |
| 1968 | return -1; |
| 1969 | spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN; |
| 1970 | end = spos + msg_len; |
| 1971 | spos += offset; |
| 1972 | |
| 1973 | os_memset(tmp, 0, DNS_HEADER_LEN); |
| 1974 | os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN); |
| 1975 | os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len); |
| 1976 | |
| 1977 | upos = buf; |
| 1978 | uend = buf + buf_len; |
| 1979 | |
| 1980 | ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end); |
| 1981 | if (ret) { |
| 1982 | os_free(tmp); |
| 1983 | return ret; |
| 1984 | } |
| 1985 | |
| 1986 | if (upos == buf) { |
| 1987 | upos[0] = '.'; |
| 1988 | upos[1] = '\0'; |
| 1989 | } else if (upos[-1] == '.') |
| 1990 | upos[-1] = '\0'; |
| 1991 | |
| 1992 | os_free(tmp); |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
| 1996 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1997 | static struct p2p_srv_bonjour * |
| 1998 | wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, |
| 1999 | const struct wpabuf *query) |
| 2000 | { |
| 2001 | struct p2p_srv_bonjour *bsrv; |
| 2002 | size_t len; |
| 2003 | |
| 2004 | len = wpabuf_len(query); |
| 2005 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2006 | struct p2p_srv_bonjour, list) { |
| 2007 | if (len == wpabuf_len(bsrv->query) && |
| 2008 | os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), |
| 2009 | len) == 0) |
| 2010 | return bsrv; |
| 2011 | } |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | static struct p2p_srv_upnp * |
| 2017 | wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2018 | const char *service) |
| 2019 | { |
| 2020 | struct p2p_srv_upnp *usrv; |
| 2021 | |
| 2022 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2023 | struct p2p_srv_upnp, list) { |
| 2024 | if (version == usrv->version && |
| 2025 | os_strcmp(service, usrv->service) == 0) |
| 2026 | return usrv; |
| 2027 | } |
| 2028 | return NULL; |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, |
| 2033 | u8 srv_trans_id) |
| 2034 | { |
| 2035 | u8 *len_pos; |
| 2036 | |
| 2037 | if (wpabuf_tailroom(resp) < 5) |
| 2038 | return; |
| 2039 | |
| 2040 | /* Length (to be filled) */ |
| 2041 | len_pos = wpabuf_put(resp, 2); |
| 2042 | wpabuf_put_u8(resp, srv_proto); |
| 2043 | wpabuf_put_u8(resp, srv_trans_id); |
| 2044 | /* Status Code */ |
| 2045 | wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); |
| 2046 | /* Response Data: empty */ |
| 2047 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, |
| 2052 | struct wpabuf *resp, u8 srv_trans_id) |
| 2053 | { |
| 2054 | struct p2p_srv_bonjour *bsrv; |
| 2055 | u8 *len_pos; |
| 2056 | |
| 2057 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); |
| 2058 | |
| 2059 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2060 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 2061 | return; |
| 2062 | } |
| 2063 | |
| 2064 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2065 | struct p2p_srv_bonjour, list) { |
| 2066 | if (wpabuf_tailroom(resp) < |
| 2067 | 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) |
| 2068 | return; |
| 2069 | /* Length (to be filled) */ |
| 2070 | len_pos = wpabuf_put(resp, 2); |
| 2071 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2072 | wpabuf_put_u8(resp, srv_trans_id); |
| 2073 | /* Status Code */ |
| 2074 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2075 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2076 | wpabuf_head(bsrv->resp), |
| 2077 | wpabuf_len(bsrv->resp)); |
| 2078 | /* Response Data */ |
| 2079 | wpabuf_put_buf(resp, bsrv->query); /* Key */ |
| 2080 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2081 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2082 | 2); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2087 | static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query, |
| 2088 | size_t query_len) |
| 2089 | { |
| 2090 | char str_rx[256], str_srv[256]; |
| 2091 | |
| 2092 | if (query_len < 3 || wpabuf_len(bsrv->query) < 3) |
| 2093 | return 0; /* Too short to include DNS Type and Version */ |
| 2094 | if (os_memcmp(query + query_len - 3, |
| 2095 | wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3, |
| 2096 | 3) != 0) |
| 2097 | return 0; /* Mismatch in DNS Type or Version */ |
| 2098 | if (query_len == wpabuf_len(bsrv->query) && |
| 2099 | os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0) |
| 2100 | return 1; /* Binary match */ |
| 2101 | |
| 2102 | if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3, |
| 2103 | 0)) |
| 2104 | return 0; /* Failed to uncompress query */ |
| 2105 | if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv), |
| 2106 | wpabuf_head(bsrv->query), |
| 2107 | wpabuf_len(bsrv->query) - 3, 0)) |
| 2108 | return 0; /* Failed to uncompress service */ |
| 2109 | |
| 2110 | return os_strcmp(str_rx, str_srv) == 0; |
| 2111 | } |
| 2112 | |
| 2113 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2114 | static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, |
| 2115 | struct wpabuf *resp, u8 srv_trans_id, |
| 2116 | const u8 *query, size_t query_len) |
| 2117 | { |
| 2118 | struct p2p_srv_bonjour *bsrv; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2119 | u8 *len_pos; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2120 | int matches = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2121 | |
| 2122 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", |
| 2123 | query, query_len); |
| 2124 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2125 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 2126 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, |
| 2127 | srv_trans_id); |
| 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | if (query_len == 0) { |
| 2132 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2133 | return; |
| 2134 | } |
| 2135 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2136 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2137 | struct p2p_srv_bonjour, list) { |
| 2138 | if (!match_bonjour_query(bsrv, query, query_len)) |
| 2139 | continue; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2140 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2141 | if (wpabuf_tailroom(resp) < |
| 2142 | 5 + query_len + wpabuf_len(bsrv->resp)) |
| 2143 | return; |
| 2144 | |
| 2145 | matches++; |
| 2146 | |
| 2147 | /* Length (to be filled) */ |
| 2148 | len_pos = wpabuf_put(resp, 2); |
| 2149 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2150 | wpabuf_put_u8(resp, srv_trans_id); |
| 2151 | |
| 2152 | /* Status Code */ |
| 2153 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2154 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2155 | wpabuf_head(bsrv->resp), |
| 2156 | wpabuf_len(bsrv->resp)); |
| 2157 | |
| 2158 | /* Response Data */ |
| 2159 | wpabuf_put_data(resp, query, query_len); /* Key */ |
| 2160 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2161 | |
| 2162 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2163 | } |
| 2164 | |
| 2165 | if (matches == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2166 | wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " |
| 2167 | "available"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2168 | if (wpabuf_tailroom(resp) < 5) |
| 2169 | return; |
| 2170 | |
| 2171 | /* Length (to be filled) */ |
| 2172 | len_pos = wpabuf_put(resp, 2); |
| 2173 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2174 | wpabuf_put_u8(resp, srv_trans_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2175 | |
| 2176 | /* Status Code */ |
| 2177 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2178 | /* Response Data: empty */ |
| 2179 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2180 | 2); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2181 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, |
| 2186 | struct wpabuf *resp, u8 srv_trans_id) |
| 2187 | { |
| 2188 | struct p2p_srv_upnp *usrv; |
| 2189 | u8 *len_pos; |
| 2190 | |
| 2191 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); |
| 2192 | |
| 2193 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2194 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2195 | return; |
| 2196 | } |
| 2197 | |
| 2198 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2199 | struct p2p_srv_upnp, list) { |
| 2200 | if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) |
| 2201 | return; |
| 2202 | |
| 2203 | /* Length (to be filled) */ |
| 2204 | len_pos = wpabuf_put(resp, 2); |
| 2205 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2206 | wpabuf_put_u8(resp, srv_trans_id); |
| 2207 | |
| 2208 | /* Status Code */ |
| 2209 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2210 | /* Response Data */ |
| 2211 | wpabuf_put_u8(resp, usrv->version); |
| 2212 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2213 | usrv->service); |
| 2214 | wpabuf_put_str(resp, usrv->service); |
| 2215 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2216 | 2); |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, |
| 2222 | struct wpabuf *resp, u8 srv_trans_id, |
| 2223 | const u8 *query, size_t query_len) |
| 2224 | { |
| 2225 | struct p2p_srv_upnp *usrv; |
| 2226 | u8 *len_pos; |
| 2227 | u8 version; |
| 2228 | char *str; |
| 2229 | int count = 0; |
| 2230 | |
| 2231 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", |
| 2232 | query, query_len); |
| 2233 | |
| 2234 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2235 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2236 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, |
| 2237 | srv_trans_id); |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | if (query_len == 0) { |
| 2242 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2243 | return; |
| 2244 | } |
| 2245 | |
| 2246 | if (wpabuf_tailroom(resp) < 5) |
| 2247 | return; |
| 2248 | |
| 2249 | /* Length (to be filled) */ |
| 2250 | len_pos = wpabuf_put(resp, 2); |
| 2251 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2252 | wpabuf_put_u8(resp, srv_trans_id); |
| 2253 | |
| 2254 | version = query[0]; |
| 2255 | str = os_malloc(query_len); |
| 2256 | if (str == NULL) |
| 2257 | return; |
| 2258 | os_memcpy(str, query + 1, query_len - 1); |
| 2259 | str[query_len - 1] = '\0'; |
| 2260 | |
| 2261 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2262 | struct p2p_srv_upnp, list) { |
| 2263 | if (version != usrv->version) |
| 2264 | continue; |
| 2265 | |
| 2266 | if (os_strcmp(str, "ssdp:all") != 0 && |
| 2267 | os_strstr(usrv->service, str) == NULL) |
| 2268 | continue; |
| 2269 | |
| 2270 | if (wpabuf_tailroom(resp) < 2) |
| 2271 | break; |
| 2272 | if (count == 0) { |
| 2273 | /* Status Code */ |
| 2274 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2275 | /* Response Data */ |
| 2276 | wpabuf_put_u8(resp, version); |
| 2277 | } else |
| 2278 | wpabuf_put_u8(resp, ','); |
| 2279 | |
| 2280 | count++; |
| 2281 | |
| 2282 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2283 | usrv->service); |
| 2284 | if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) |
| 2285 | break; |
| 2286 | wpabuf_put_str(resp, usrv->service); |
| 2287 | } |
| 2288 | os_free(str); |
| 2289 | |
| 2290 | if (count == 0) { |
| 2291 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " |
| 2292 | "available"); |
| 2293 | /* Status Code */ |
| 2294 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2295 | /* Response Data: empty */ |
| 2296 | } |
| 2297 | |
| 2298 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2299 | } |
| 2300 | |
| 2301 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2302 | #ifdef CONFIG_WIFI_DISPLAY |
| 2303 | static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s, |
| 2304 | struct wpabuf *resp, u8 srv_trans_id, |
| 2305 | const u8 *query, size_t query_len) |
| 2306 | { |
| 2307 | const u8 *pos; |
| 2308 | u8 role; |
| 2309 | u8 *len_pos; |
| 2310 | |
| 2311 | wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len); |
| 2312 | |
| 2313 | if (!wpa_s->global->wifi_display) { |
| 2314 | wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available"); |
| 2315 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY, |
| 2316 | srv_trans_id); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
| 2320 | if (query_len < 1) { |
| 2321 | wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device " |
| 2322 | "Role"); |
| 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | if (wpabuf_tailroom(resp) < 5) |
| 2327 | return; |
| 2328 | |
| 2329 | pos = query; |
| 2330 | role = *pos++; |
| 2331 | wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role); |
| 2332 | |
| 2333 | /* TODO: role specific handling */ |
| 2334 | |
| 2335 | /* Length (to be filled) */ |
| 2336 | len_pos = wpabuf_put(resp, 2); |
| 2337 | wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY); |
| 2338 | wpabuf_put_u8(resp, srv_trans_id); |
| 2339 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */ |
| 2340 | |
| 2341 | while (pos < query + query_len) { |
| 2342 | if (*pos < MAX_WFD_SUBELEMS && |
| 2343 | wpa_s->global->wfd_subelem[*pos] && |
| 2344 | wpabuf_tailroom(resp) >= |
| 2345 | wpabuf_len(wpa_s->global->wfd_subelem[*pos])) { |
| 2346 | wpa_printf(MSG_DEBUG, "P2P: Add WSD response " |
| 2347 | "subelement %u", *pos); |
| 2348 | wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]); |
| 2349 | } |
| 2350 | pos++; |
| 2351 | } |
| 2352 | |
| 2353 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2354 | } |
| 2355 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2356 | |
| 2357 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2358 | static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, |
| 2359 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2360 | { |
| 2361 | struct wpa_supplicant *wpa_s = ctx; |
| 2362 | const u8 *pos = tlvs; |
| 2363 | const u8 *end = tlvs + tlvs_len; |
| 2364 | const u8 *tlv_end; |
| 2365 | u16 slen; |
| 2366 | struct wpabuf *resp; |
| 2367 | u8 srv_proto, srv_trans_id; |
| 2368 | size_t buf_len; |
| 2369 | char *buf; |
| 2370 | |
| 2371 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", |
| 2372 | tlvs, tlvs_len); |
| 2373 | buf_len = 2 * tlvs_len + 1; |
| 2374 | buf = os_malloc(buf_len); |
| 2375 | if (buf) { |
| 2376 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2377 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " |
| 2378 | MACSTR " %u %u %s", |
| 2379 | freq, MAC2STR(sa), dialog_token, update_indic, |
| 2380 | buf); |
| 2381 | os_free(buf); |
| 2382 | } |
| 2383 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2384 | if (wpa_s->p2p_sd_over_ctrl_iface) { |
| 2385 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2386 | update_indic, tlvs, tlvs_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2387 | return; /* to be processed by an external program */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2388 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2389 | |
| 2390 | resp = wpabuf_alloc(10000); |
| 2391 | if (resp == NULL) |
| 2392 | return; |
| 2393 | |
| 2394 | while (pos + 1 < end) { |
| 2395 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); |
| 2396 | slen = WPA_GET_LE16(pos); |
| 2397 | pos += 2; |
| 2398 | if (pos + slen > end || slen < 2) { |
| 2399 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " |
| 2400 | "length"); |
| 2401 | wpabuf_free(resp); |
| 2402 | return; |
| 2403 | } |
| 2404 | tlv_end = pos + slen; |
| 2405 | |
| 2406 | srv_proto = *pos++; |
| 2407 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2408 | srv_proto); |
| 2409 | srv_trans_id = *pos++; |
| 2410 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2411 | srv_trans_id); |
| 2412 | |
| 2413 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", |
| 2414 | pos, tlv_end - pos); |
| 2415 | |
| 2416 | |
| 2417 | if (wpa_s->force_long_sd) { |
| 2418 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " |
| 2419 | "response"); |
| 2420 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2421 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2422 | goto done; |
| 2423 | } |
| 2424 | |
| 2425 | switch (srv_proto) { |
| 2426 | case P2P_SERV_ALL_SERVICES: |
| 2427 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " |
| 2428 | "for all services"); |
| 2429 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && |
| 2430 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2431 | wpa_printf(MSG_DEBUG, "P2P: No service " |
| 2432 | "discovery protocols available"); |
| 2433 | wpas_sd_add_proto_not_avail( |
| 2434 | resp, P2P_SERV_ALL_SERVICES, |
| 2435 | srv_trans_id); |
| 2436 | break; |
| 2437 | } |
| 2438 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2439 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2440 | break; |
| 2441 | case P2P_SERV_BONJOUR: |
| 2442 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, |
| 2443 | pos, tlv_end - pos); |
| 2444 | break; |
| 2445 | case P2P_SERV_UPNP: |
| 2446 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, |
| 2447 | pos, tlv_end - pos); |
| 2448 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2449 | #ifdef CONFIG_WIFI_DISPLAY |
| 2450 | case P2P_SERV_WIFI_DISPLAY: |
| 2451 | wpas_sd_req_wfd(wpa_s, resp, srv_trans_id, |
| 2452 | pos, tlv_end - pos); |
| 2453 | break; |
| 2454 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2455 | default: |
| 2456 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " |
| 2457 | "protocol %u", srv_proto); |
| 2458 | wpas_sd_add_proto_not_avail(resp, srv_proto, |
| 2459 | srv_trans_id); |
| 2460 | break; |
| 2461 | } |
| 2462 | |
| 2463 | pos = tlv_end; |
| 2464 | } |
| 2465 | |
| 2466 | done: |
| 2467 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2468 | update_indic, tlvs, tlvs_len); |
| 2469 | |
| 2470 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
| 2471 | |
| 2472 | wpabuf_free(resp); |
| 2473 | } |
| 2474 | |
| 2475 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2476 | static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, |
| 2477 | const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2478 | { |
| 2479 | struct wpa_supplicant *wpa_s = ctx; |
| 2480 | const u8 *pos = tlvs; |
| 2481 | const u8 *end = tlvs + tlvs_len; |
| 2482 | const u8 *tlv_end; |
| 2483 | u16 slen; |
| 2484 | size_t buf_len; |
| 2485 | char *buf; |
| 2486 | |
| 2487 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", |
| 2488 | tlvs, tlvs_len); |
| 2489 | if (tlvs_len > 1500) { |
| 2490 | /* TODO: better way for handling this */ |
| 2491 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2492 | P2P_EVENT_SERV_DISC_RESP MACSTR |
| 2493 | " %u <long response: %u bytes>", |
| 2494 | MAC2STR(sa), update_indic, |
| 2495 | (unsigned int) tlvs_len); |
| 2496 | } else { |
| 2497 | buf_len = 2 * tlvs_len + 1; |
| 2498 | buf = os_malloc(buf_len); |
| 2499 | if (buf) { |
| 2500 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2501 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2502 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", |
| 2503 | MAC2STR(sa), update_indic, buf); |
| 2504 | os_free(buf); |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | while (pos < end) { |
| 2509 | u8 srv_proto, srv_trans_id, status; |
| 2510 | |
| 2511 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); |
| 2512 | slen = WPA_GET_LE16(pos); |
| 2513 | pos += 2; |
| 2514 | if (pos + slen > end || slen < 3) { |
| 2515 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " |
| 2516 | "length"); |
| 2517 | return; |
| 2518 | } |
| 2519 | tlv_end = pos + slen; |
| 2520 | |
| 2521 | srv_proto = *pos++; |
| 2522 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2523 | srv_proto); |
| 2524 | srv_trans_id = *pos++; |
| 2525 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2526 | srv_trans_id); |
| 2527 | status = *pos++; |
| 2528 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", |
| 2529 | status); |
| 2530 | |
| 2531 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", |
| 2532 | pos, tlv_end - pos); |
| 2533 | |
| 2534 | pos = tlv_end; |
| 2535 | } |
| 2536 | |
| 2537 | wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); |
| 2538 | } |
| 2539 | |
| 2540 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2541 | u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2542 | const struct wpabuf *tlvs) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2543 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2544 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2545 | return 0; |
| 2546 | return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2550 | u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2551 | u8 version, const char *query) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2552 | { |
| 2553 | struct wpabuf *tlvs; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2554 | u64 ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2555 | |
| 2556 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); |
| 2557 | if (tlvs == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2558 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2559 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); |
| 2560 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ |
| 2561 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ |
| 2562 | wpabuf_put_u8(tlvs, version); |
| 2563 | wpabuf_put_str(tlvs, query); |
| 2564 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); |
| 2565 | wpabuf_free(tlvs); |
| 2566 | return ret; |
| 2567 | } |
| 2568 | |
| 2569 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2570 | #ifdef CONFIG_WIFI_DISPLAY |
| 2571 | |
| 2572 | static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2573 | const struct wpabuf *tlvs) |
| 2574 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2575 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2576 | return 0; |
| 2577 | return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs); |
| 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | #define MAX_WFD_SD_SUBELEMS 20 |
| 2582 | |
| 2583 | static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role, |
| 2584 | const char *subelems) |
| 2585 | { |
| 2586 | u8 *len; |
| 2587 | const char *pos; |
| 2588 | int val; |
| 2589 | int count = 0; |
| 2590 | |
| 2591 | len = wpabuf_put(tlvs, 2); |
| 2592 | wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */ |
| 2593 | wpabuf_put_u8(tlvs, id); /* Service Transaction ID */ |
| 2594 | |
| 2595 | wpabuf_put_u8(tlvs, role); |
| 2596 | |
| 2597 | pos = subelems; |
| 2598 | while (*pos) { |
| 2599 | val = atoi(pos); |
| 2600 | if (val >= 0 && val < 256) { |
| 2601 | wpabuf_put_u8(tlvs, val); |
| 2602 | count++; |
| 2603 | if (count == MAX_WFD_SD_SUBELEMS) |
| 2604 | break; |
| 2605 | } |
| 2606 | pos = os_strchr(pos + 1, ','); |
| 2607 | if (pos == NULL) |
| 2608 | break; |
| 2609 | pos++; |
| 2610 | } |
| 2611 | |
| 2612 | WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2); |
| 2613 | } |
| 2614 | |
| 2615 | |
| 2616 | u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s, |
| 2617 | const u8 *dst, const char *role) |
| 2618 | { |
| 2619 | struct wpabuf *tlvs; |
| 2620 | u64 ret; |
| 2621 | const char *subelems; |
| 2622 | u8 id = 1; |
| 2623 | |
| 2624 | subelems = os_strchr(role, ' '); |
| 2625 | if (subelems == NULL) |
| 2626 | return 0; |
| 2627 | subelems++; |
| 2628 | |
| 2629 | tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS)); |
| 2630 | if (tlvs == NULL) |
| 2631 | return 0; |
| 2632 | |
| 2633 | if (os_strstr(role, "[source]")) |
| 2634 | wfd_add_sd_req_role(tlvs, id++, 0x00, subelems); |
| 2635 | if (os_strstr(role, "[pri-sink]")) |
| 2636 | wfd_add_sd_req_role(tlvs, id++, 0x01, subelems); |
| 2637 | if (os_strstr(role, "[sec-sink]")) |
| 2638 | wfd_add_sd_req_role(tlvs, id++, 0x02, subelems); |
| 2639 | if (os_strstr(role, "[source+sink]")) |
| 2640 | wfd_add_sd_req_role(tlvs, id++, 0x03, subelems); |
| 2641 | |
| 2642 | ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs); |
| 2643 | wpabuf_free(tlvs); |
| 2644 | return ret; |
| 2645 | } |
| 2646 | |
| 2647 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2648 | |
| 2649 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2650 | int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2651 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2652 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2653 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2654 | return p2p_sd_cancel_request(wpa_s->global->p2p, |
| 2655 | (void *) (uintptr_t) req); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | |
| 2659 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, |
| 2660 | const u8 *dst, u8 dialog_token, |
| 2661 | const struct wpabuf *resp_tlvs) |
| 2662 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2663 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2664 | return; |
| 2665 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, |
| 2666 | resp_tlvs); |
| 2667 | } |
| 2668 | |
Dmitry Shmidt | eaf261d | 2013-08-14 15:30:08 -0700 | [diff] [blame] | 2669 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2670 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) |
| 2671 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2672 | if (wpa_s->global->p2p) |
| 2673 | p2p_sd_service_update(wpa_s->global->p2p); |
| 2674 | } |
| 2675 | |
| 2676 | |
| 2677 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) |
| 2678 | { |
| 2679 | dl_list_del(&bsrv->list); |
| 2680 | wpabuf_free(bsrv->query); |
| 2681 | wpabuf_free(bsrv->resp); |
| 2682 | os_free(bsrv); |
| 2683 | } |
| 2684 | |
| 2685 | |
| 2686 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) |
| 2687 | { |
| 2688 | dl_list_del(&usrv->list); |
| 2689 | os_free(usrv->service); |
| 2690 | os_free(usrv); |
| 2691 | } |
| 2692 | |
| 2693 | |
| 2694 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) |
| 2695 | { |
| 2696 | struct p2p_srv_bonjour *bsrv, *bn; |
| 2697 | struct p2p_srv_upnp *usrv, *un; |
| 2698 | |
| 2699 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, |
| 2700 | struct p2p_srv_bonjour, list) |
| 2701 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2702 | |
| 2703 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, |
| 2704 | struct p2p_srv_upnp, list) |
| 2705 | wpas_p2p_srv_upnp_free(usrv); |
| 2706 | |
| 2707 | wpas_p2p_sd_service_update(wpa_s); |
| 2708 | } |
| 2709 | |
| 2710 | |
| 2711 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, |
| 2712 | struct wpabuf *query, struct wpabuf *resp) |
| 2713 | { |
| 2714 | struct p2p_srv_bonjour *bsrv; |
| 2715 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2716 | bsrv = os_zalloc(sizeof(*bsrv)); |
| 2717 | if (bsrv == NULL) |
| 2718 | return -1; |
| 2719 | bsrv->query = query; |
| 2720 | bsrv->resp = resp; |
| 2721 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); |
| 2722 | |
| 2723 | wpas_p2p_sd_service_update(wpa_s); |
| 2724 | return 0; |
| 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, |
| 2729 | const struct wpabuf *query) |
| 2730 | { |
| 2731 | struct p2p_srv_bonjour *bsrv; |
| 2732 | |
| 2733 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); |
| 2734 | if (bsrv == NULL) |
| 2735 | return -1; |
| 2736 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2737 | wpas_p2p_sd_service_update(wpa_s); |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
| 2741 | |
| 2742 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2743 | const char *service) |
| 2744 | { |
| 2745 | struct p2p_srv_upnp *usrv; |
| 2746 | |
| 2747 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) |
| 2748 | return 0; /* Already listed */ |
| 2749 | usrv = os_zalloc(sizeof(*usrv)); |
| 2750 | if (usrv == NULL) |
| 2751 | return -1; |
| 2752 | usrv->version = version; |
| 2753 | usrv->service = os_strdup(service); |
| 2754 | if (usrv->service == NULL) { |
| 2755 | os_free(usrv); |
| 2756 | return -1; |
| 2757 | } |
| 2758 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); |
| 2759 | |
| 2760 | wpas_p2p_sd_service_update(wpa_s); |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | |
| 2765 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2766 | const char *service) |
| 2767 | { |
| 2768 | struct p2p_srv_upnp *usrv; |
| 2769 | |
| 2770 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); |
| 2771 | if (usrv == NULL) |
| 2772 | return -1; |
| 2773 | wpas_p2p_srv_upnp_free(usrv); |
| 2774 | wpas_p2p_sd_service_update(wpa_s); |
| 2775 | return 0; |
| 2776 | } |
| 2777 | |
| 2778 | |
| 2779 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, |
| 2780 | const u8 *peer, const char *params, |
| 2781 | unsigned int generated_pin) |
| 2782 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2783 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR |
| 2784 | " %08d%s", MAC2STR(peer), generated_pin, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
| 2788 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, |
| 2789 | const u8 *peer, const char *params) |
| 2790 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2791 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR |
| 2792 | "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2796 | static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, |
| 2797 | const u8 *dev_addr, const u8 *pri_dev_type, |
| 2798 | const char *dev_name, u16 supp_config_methods, |
| 2799 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
| 2800 | size_t group_id_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2801 | { |
| 2802 | struct wpa_supplicant *wpa_s = ctx; |
| 2803 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2804 | char params[300]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2805 | u8 empty_dev_type[8]; |
| 2806 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2807 | struct wpa_supplicant *group = NULL; |
| 2808 | |
| 2809 | if (group_id) { |
| 2810 | for (group = wpa_s->global->ifaces; group; group = group->next) |
| 2811 | { |
| 2812 | struct wpa_ssid *s = group->current_ssid; |
| 2813 | if (s != NULL && |
| 2814 | s->mode == WPAS_MODE_P2P_GO && |
| 2815 | group_id_len - ETH_ALEN == s->ssid_len && |
| 2816 | os_memcmp(group_id + ETH_ALEN, s->ssid, |
| 2817 | s->ssid_len) == 0) |
| 2818 | break; |
| 2819 | } |
| 2820 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2821 | |
| 2822 | if (pri_dev_type == NULL) { |
| 2823 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); |
| 2824 | pri_dev_type = empty_dev_type; |
| 2825 | } |
| 2826 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR |
| 2827 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2828 | "dev_capab=0x%x group_capab=0x%x%s%s", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2829 | MAC2STR(dev_addr), |
| 2830 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 2831 | sizeof(devtype)), |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2832 | dev_name, supp_config_methods, dev_capab, group_capab, |
| 2833 | group ? " group=" : "", |
| 2834 | group ? group->ifname : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2835 | params[sizeof(params) - 1] = '\0'; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2836 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2837 | if (config_methods & WPS_CONFIG_DISPLAY) { |
| 2838 | generated_pin = wps_generate_pin(); |
| 2839 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2840 | generated_pin); |
| 2841 | } else if (config_methods & WPS_CONFIG_KEYPAD) |
| 2842 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
| 2843 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2844 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ |
| 2845 | MACSTR "%s", MAC2STR(peer), params); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2846 | |
| 2847 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, |
| 2848 | P2P_PROV_DISC_SUCCESS, |
| 2849 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2853 | static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2854 | { |
| 2855 | struct wpa_supplicant *wpa_s = ctx; |
| 2856 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2857 | char params[20]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2858 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2859 | if (wpa_s->pending_pd_before_join && |
| 2860 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
| 2861 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { |
| 2862 | wpa_s->pending_pd_before_join = 0; |
| 2863 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2864 | "join-existing-group operation"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2865 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2866 | return; |
| 2867 | } |
| 2868 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2869 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
| 2870 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) |
| 2871 | os_snprintf(params, sizeof(params), " peer_go=%d", |
| 2872 | wpa_s->pending_pd_use == AUTO_PD_JOIN); |
| 2873 | else |
| 2874 | params[0] = '\0'; |
| 2875 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2876 | if (config_methods & WPS_CONFIG_DISPLAY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2877 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2878 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
| 2879 | generated_pin = wps_generate_pin(); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2880 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2881 | generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2882 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2883 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP |
| 2884 | MACSTR "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2885 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2886 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2887 | P2P_PROV_DISC_SUCCESS, |
| 2888 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2892 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
| 2893 | enum p2p_prov_disc_status status) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2894 | { |
| 2895 | struct wpa_supplicant *wpa_s = ctx; |
| 2896 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2897 | if (wpa_s->p2p_fallback_to_go_neg) { |
| 2898 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " |
| 2899 | "failed - fall back to GO Negotiation"); |
| 2900 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 2901 | return; |
| 2902 | } |
| 2903 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2904 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2905 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2906 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2907 | "join-existing-group operation (no ACK for PD " |
| 2908 | "Req attempts)"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2909 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2910 | return; |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2911 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2912 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2913 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2914 | " p2p_dev_addr=" MACSTR " status=%d", |
| 2915 | MAC2STR(peer), status); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2916 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2917 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2918 | status, 0, 0); |
| 2919 | } |
| 2920 | |
| 2921 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2922 | static int freq_included(const struct p2p_channels *channels, unsigned int freq) |
| 2923 | { |
| 2924 | if (channels == NULL) |
| 2925 | return 1; /* Assume no restrictions */ |
| 2926 | return p2p_channels_includes_freq(channels, freq); |
| 2927 | |
| 2928 | } |
| 2929 | |
| 2930 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2931 | /** |
| 2932 | * Pick the best frequency to use from all the currently used frequencies. |
| 2933 | */ |
| 2934 | static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s, |
| 2935 | struct wpa_used_freq_data *freqs, |
| 2936 | unsigned int num) |
| 2937 | { |
| 2938 | unsigned int i, c; |
| 2939 | |
| 2940 | /* find a candidate freq that is supported by P2P */ |
| 2941 | for (c = 0; c < num; c++) |
| 2942 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq)) |
| 2943 | break; |
| 2944 | |
| 2945 | if (c == num) |
| 2946 | return 0; |
| 2947 | |
| 2948 | /* once we have a candidate, try to find a 'better' one */ |
| 2949 | for (i = c + 1; i < num; i++) { |
| 2950 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) |
| 2951 | continue; |
| 2952 | |
| 2953 | /* |
| 2954 | * 1. Infrastructure station interfaces have higher preference. |
| 2955 | * 2. P2P Clients have higher preference. |
| 2956 | * 3. All others. |
| 2957 | */ |
| 2958 | if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) { |
| 2959 | c = i; |
| 2960 | break; |
| 2961 | } |
| 2962 | |
| 2963 | if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT)) |
| 2964 | c = i; |
| 2965 | } |
| 2966 | return freqs[c].freq; |
| 2967 | } |
| 2968 | |
| 2969 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2970 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
| 2971 | const u8 *go_dev_addr, const u8 *ssid, |
| 2972 | size_t ssid_len, int *go, u8 *group_bssid, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2973 | int *force_freq, int persistent_group, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2974 | const struct p2p_channels *channels, |
| 2975 | int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2976 | { |
| 2977 | struct wpa_supplicant *wpa_s = ctx; |
| 2978 | struct wpa_ssid *s; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2979 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2980 | struct wpa_supplicant *grp; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2981 | int best_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2982 | |
| 2983 | if (!persistent_group) { |
| 2984 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2985 | " to join an active group (SSID: %s)", |
| 2986 | MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2987 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2988 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
| 2989 | == 0 || |
| 2990 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { |
| 2991 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
| 2992 | "authorized invitation"); |
| 2993 | goto accept_inv; |
| 2994 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2995 | |
| 2996 | #ifdef CONFIG_WPS_NFC |
| 2997 | if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled && |
| 2998 | dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) { |
| 2999 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag"); |
| 3000 | wpa_s->parent->p2p_wps_method = WPS_NFC; |
| 3001 | wpa_s->parent->pending_join_wps_method = WPS_NFC; |
| 3002 | os_memcpy(wpa_s->parent->pending_join_dev_addr, |
| 3003 | go_dev_addr, ETH_ALEN); |
| 3004 | os_memcpy(wpa_s->parent->pending_join_iface_addr, |
| 3005 | bssid, ETH_ALEN); |
| 3006 | goto accept_inv; |
| 3007 | } |
| 3008 | #endif /* CONFIG_WPS_NFC */ |
| 3009 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3010 | /* |
| 3011 | * Do not accept the invitation automatically; notify user and |
| 3012 | * request approval. |
| 3013 | */ |
| 3014 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 3015 | } |
| 3016 | |
| 3017 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
| 3018 | if (grp) { |
| 3019 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " |
| 3020 | "running persistent group"); |
| 3021 | if (*go) |
| 3022 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); |
| 3023 | goto accept_inv; |
| 3024 | } |
| 3025 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3026 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 3027 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { |
| 3028 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " |
| 3029 | "invitation to re-invoke a persistent group"); |
Dmitry Shmidt | 76cd2cc | 2014-05-27 12:56:04 -0700 | [diff] [blame] | 3030 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3031 | } else if (!wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3032 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 3033 | |
| 3034 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 3035 | if (s->disabled == 2 && |
| 3036 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && |
| 3037 | s->ssid_len == ssid_len && |
| 3038 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 3039 | break; |
| 3040 | } |
| 3041 | |
| 3042 | if (!s) { |
| 3043 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 3044 | " requested reinvocation of an unknown group", |
| 3045 | MAC2STR(sa)); |
| 3046 | return P2P_SC_FAIL_UNKNOWN_GROUP; |
| 3047 | } |
| 3048 | |
| 3049 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { |
| 3050 | *go = 1; |
| 3051 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 3052 | wpa_printf(MSG_DEBUG, "P2P: The only available " |
| 3053 | "interface is already in use - reject " |
| 3054 | "invitation"); |
| 3055 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 3056 | } |
| 3057 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); |
| 3058 | } else if (s->mode == WPAS_MODE_P2P_GO) { |
| 3059 | *go = 1; |
| 3060 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) |
| 3061 | { |
| 3062 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 3063 | "interface address for the group"); |
| 3064 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 3065 | } |
| 3066 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, |
| 3067 | ETH_ALEN); |
| 3068 | } |
| 3069 | |
| 3070 | accept_inv: |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3071 | wpas_p2p_set_own_freq_preference(wpa_s, 0); |
| 3072 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3073 | best_freq = 0; |
| 3074 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 3075 | sizeof(struct wpa_used_freq_data)); |
| 3076 | if (freqs) { |
| 3077 | int num_channels = wpa_s->num_multichan_concurrent; |
| 3078 | int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels); |
| 3079 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
| 3080 | os_free(freqs); |
| 3081 | } |
| 3082 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3083 | /* Get one of the frequencies currently in use */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3084 | if (best_freq > 0) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3085 | wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces"); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3086 | wpas_p2p_set_own_freq_preference(wpa_s, best_freq); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3087 | |
| 3088 | if (wpa_s->num_multichan_concurrent < 2 || |
| 3089 | wpas_p2p_num_unused_channels(wpa_s) < 1) { |
| 3090 | wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3091 | *force_freq = best_freq; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 3092 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3095 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 3096 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 3097 | if (*go == 0) { |
| 3098 | /* We are the client */ |
| 3099 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " |
| 3100 | "running a GO but we are capable of MCC, " |
| 3101 | "figure out the best channel to use"); |
| 3102 | *force_freq = 0; |
| 3103 | } else if (!freq_included(channels, *force_freq)) { |
| 3104 | /* We are the GO, and *force_freq is not in the |
| 3105 | * intersection */ |
| 3106 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 3107 | "in intersection but we are capable of MCC, " |
| 3108 | "figure out the best channel to use", |
| 3109 | *force_freq); |
| 3110 | *force_freq = 0; |
| 3111 | } |
| 3112 | } |
| 3113 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3114 | return P2P_SC_SUCCESS; |
| 3115 | } |
| 3116 | |
| 3117 | |
| 3118 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, |
| 3119 | const u8 *ssid, size_t ssid_len, |
| 3120 | const u8 *go_dev_addr, u8 status, |
| 3121 | int op_freq) |
| 3122 | { |
| 3123 | struct wpa_supplicant *wpa_s = ctx; |
| 3124 | struct wpa_ssid *s; |
| 3125 | |
| 3126 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 3127 | if (s->disabled == 2 && |
| 3128 | s->ssid_len == ssid_len && |
| 3129 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 3130 | break; |
| 3131 | } |
| 3132 | |
| 3133 | if (status == P2P_SC_SUCCESS) { |
| 3134 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3135 | " was accepted; op_freq=%d MHz, SSID=%s", |
| 3136 | MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3137 | if (s) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 3138 | int go = s->mode == WPAS_MODE_P2P_GO; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3139 | wpas_p2p_group_add_persistent( |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3140 | wpa_s, s, go, 0, op_freq, 0, 0, NULL, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3141 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3142 | } else if (bssid) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3143 | wpa_s->user_initiated_pd = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3144 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3145 | wpa_s->p2p_wps_method, 0, op_freq, |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 3146 | ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3147 | } |
| 3148 | return; |
| 3149 | } |
| 3150 | |
| 3151 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3152 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 3153 | " was rejected (status %u)", MAC2STR(sa), status); |
| 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | if (!s) { |
| 3158 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3159 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3160 | P2P_EVENT_INVITATION_RECEIVED |
| 3161 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3162 | " bssid=" MACSTR " unknown-network", |
| 3163 | MAC2STR(sa), MAC2STR(go_dev_addr), |
| 3164 | MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3165 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3166 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3167 | P2P_EVENT_INVITATION_RECEIVED |
| 3168 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3169 | " unknown-network", |
| 3170 | MAC2STR(sa), MAC2STR(go_dev_addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3171 | } |
| 3172 | return; |
| 3173 | } |
| 3174 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3175 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3176 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3177 | "sa=" MACSTR " persistent=%d freq=%d", |
| 3178 | MAC2STR(sa), s->id, op_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3179 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3180 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3181 | "sa=" MACSTR " persistent=%d", |
| 3182 | MAC2STR(sa), s->id); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3183 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3187 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, |
| 3188 | struct wpa_ssid *ssid, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3189 | const u8 *peer, int inv) |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3190 | { |
| 3191 | size_t i; |
| 3192 | |
| 3193 | if (ssid == NULL) |
| 3194 | return; |
| 3195 | |
| 3196 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { |
| 3197 | if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer, |
| 3198 | ETH_ALEN) == 0) |
| 3199 | break; |
| 3200 | } |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 3201 | if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3202 | if (ssid->mode != WPAS_MODE_P2P_GO && |
| 3203 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { |
| 3204 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " |
| 3205 | "due to invitation result", ssid->id); |
| 3206 | wpas_notify_network_removed(wpa_s, ssid); |
| 3207 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 3208 | return; |
| 3209 | } |
| 3210 | return; /* Peer not found in client list */ |
| 3211 | } |
| 3212 | |
| 3213 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3214 | "group %d client list%s", |
| 3215 | MAC2STR(peer), ssid->id, |
| 3216 | inv ? " due to invitation result" : ""); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3217 | os_memmove(ssid->p2p_client_list + i * ETH_ALEN, |
| 3218 | ssid->p2p_client_list + (i + 1) * ETH_ALEN, |
| 3219 | (ssid->num_p2p_clients - i - 1) * ETH_ALEN); |
| 3220 | ssid->num_p2p_clients--; |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3221 | if (wpa_s->parent->conf->update_config && |
| 3222 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 3223 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | |
| 3227 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, |
| 3228 | const u8 *peer) |
| 3229 | { |
| 3230 | struct wpa_ssid *ssid; |
| 3231 | |
| 3232 | wpa_s = wpa_s->global->p2p_invite_group; |
| 3233 | if (wpa_s == NULL) |
| 3234 | return; /* No known invitation group */ |
| 3235 | ssid = wpa_s->current_ssid; |
| 3236 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 3237 | !ssid->p2p_persistent_group) |
| 3238 | return; /* Not operating as a GO in persistent group */ |
| 3239 | ssid = wpas_p2p_get_persistent(wpa_s->parent, peer, |
| 3240 | ssid->ssid, ssid->ssid_len); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3241 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3245 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3246 | const struct p2p_channels *channels, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3247 | const u8 *peer, int neg_freq, |
| 3248 | int peer_oper_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3249 | { |
| 3250 | struct wpa_supplicant *wpa_s = ctx; |
| 3251 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3252 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3253 | |
| 3254 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3255 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3256 | "status=%d " MACSTR, |
| 3257 | status, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3258 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3259 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3260 | "status=%d ", status); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3261 | } |
| 3262 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
| 3263 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3264 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, |
| 3265 | status, MAC2STR(peer)); |
| 3266 | if (wpa_s->pending_invite_ssid_id == -1) { |
| 3267 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) |
| 3268 | wpas_remove_persistent_client(wpa_s, peer); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3269 | return; /* Invitation to active group */ |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3270 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3271 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3272 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3273 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " |
| 3274 | "invitation exchange to indicate readiness for " |
| 3275 | "re-invocation"); |
| 3276 | } |
| 3277 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3278 | if (status != P2P_SC_SUCCESS) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3279 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { |
| 3280 | ssid = wpa_config_get_network( |
| 3281 | wpa_s->conf, wpa_s->pending_invite_ssid_id); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3282 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3283 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3284 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 3285 | return; |
| 3286 | } |
| 3287 | |
| 3288 | ssid = wpa_config_get_network(wpa_s->conf, |
| 3289 | wpa_s->pending_invite_ssid_id); |
| 3290 | if (ssid == NULL) { |
| 3291 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " |
| 3292 | "data matching with invitation"); |
| 3293 | return; |
| 3294 | } |
| 3295 | |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3296 | /* |
| 3297 | * The peer could have missed our ctrl::ack frame for Invitation |
| 3298 | * Response and continue retransmitting the frame. To reduce the |
| 3299 | * likelihood of the peer not getting successful TX status for the |
| 3300 | * Invitation Response frame, wait a short time here before starting |
| 3301 | * the persistent group so that we will remain on the current channel to |
| 3302 | * acknowledge any possible retransmission from the peer. |
| 3303 | */ |
| 3304 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " |
| 3305 | "starting persistent group"); |
| 3306 | os_sleep(0, 50000); |
| 3307 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3308 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && |
| 3309 | freq_included(channels, neg_freq)) |
| 3310 | freq = neg_freq; |
| 3311 | else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO && |
| 3312 | freq_included(channels, peer_oper_freq)) |
| 3313 | freq = peer_oper_freq; |
| 3314 | else |
| 3315 | freq = 0; |
| 3316 | |
| 3317 | wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s", |
| 3318 | freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3319 | wpas_p2p_group_add_persistent(wpa_s, ssid, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 3320 | ssid->mode == WPAS_MODE_P2P_GO, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3321 | wpa_s->p2p_persistent_go_freq, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 3322 | freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3323 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, |
| 3324 | channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3325 | ssid->mode == WPAS_MODE_P2P_GO ? |
| 3326 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 3327 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3331 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
| 3332 | unsigned int freq) |
| 3333 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3334 | if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq)) |
| 3335 | return 1; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 3336 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | |
| 3340 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) |
| 3341 | { |
| 3342 | reg->channel[reg->channels] = chan; |
| 3343 | reg->channels++; |
| 3344 | } |
| 3345 | |
| 3346 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3347 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3348 | struct p2p_channels *chan, |
| 3349 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3350 | { |
| 3351 | int i, cla = 0; |
| 3352 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3353 | os_memset(cli_chan, 0, sizeof(*cli_chan)); |
| 3354 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3355 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " |
| 3356 | "band"); |
| 3357 | |
| 3358 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ |
| 3359 | chan->reg_class[cla].reg_class = 81; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3360 | chan->reg_class[cla].channels = 0; |
| 3361 | for (i = 0; i < 11; i++) { |
| 3362 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) |
| 3363 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); |
| 3364 | } |
| 3365 | if (chan->reg_class[cla].channels) |
| 3366 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3367 | |
| 3368 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " |
| 3369 | "band"); |
| 3370 | |
| 3371 | /* Operating class 115 - 5 GHz, channels 36-48 */ |
| 3372 | chan->reg_class[cla].reg_class = 115; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3373 | chan->reg_class[cla].channels = 0; |
| 3374 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) |
| 3375 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); |
| 3376 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) |
| 3377 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); |
| 3378 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) |
| 3379 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); |
| 3380 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) |
| 3381 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); |
| 3382 | if (chan->reg_class[cla].channels) |
| 3383 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3384 | |
| 3385 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " |
| 3386 | "band"); |
| 3387 | |
| 3388 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ |
| 3389 | chan->reg_class[cla].reg_class = 124; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3390 | chan->reg_class[cla].channels = 0; |
| 3391 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) |
| 3392 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); |
| 3393 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) |
| 3394 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); |
| 3395 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) |
| 3396 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); |
| 3397 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) |
| 3398 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); |
| 3399 | if (chan->reg_class[cla].channels) |
| 3400 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3401 | |
| 3402 | chan->reg_classes = cla; |
| 3403 | return 0; |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, |
| 3408 | u16 num_modes, |
| 3409 | enum hostapd_hw_mode mode) |
| 3410 | { |
| 3411 | u16 i; |
| 3412 | |
| 3413 | for (i = 0; i < num_modes; i++) { |
| 3414 | if (modes[i].mode == mode) |
| 3415 | return &modes[i]; |
| 3416 | } |
| 3417 | |
| 3418 | return NULL; |
| 3419 | } |
| 3420 | |
| 3421 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3422 | enum chan_allowed { |
| 3423 | NOT_ALLOWED, PASSIVE_ONLY, ALLOWED |
| 3424 | }; |
| 3425 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3426 | static int has_channel(struct wpa_global *global, |
| 3427 | struct hostapd_hw_modes *mode, u8 chan, int *flags) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3428 | { |
| 3429 | int i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3430 | unsigned int freq; |
| 3431 | |
| 3432 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + |
| 3433 | chan * 5; |
| 3434 | if (wpas_p2p_disallowed_freq(global, freq)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3435 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3436 | |
| 3437 | for (i = 0; i < mode->num_channels; i++) { |
| 3438 | if (mode->channels[i].chan == chan) { |
| 3439 | if (flags) |
| 3440 | *flags = mode->channels[i].flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3441 | if (mode->channels[i].flag & |
| 3442 | (HOSTAPD_CHAN_DISABLED | |
| 3443 | HOSTAPD_CHAN_RADAR)) |
| 3444 | return NOT_ALLOWED; |
| 3445 | if (mode->channels[i].flag & |
| 3446 | (HOSTAPD_CHAN_PASSIVE_SCAN | |
| 3447 | HOSTAPD_CHAN_NO_IBSS)) |
| 3448 | return PASSIVE_ONLY; |
| 3449 | return ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3453 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | |
| 3457 | struct p2p_oper_class_map { |
| 3458 | enum hostapd_hw_mode mode; |
| 3459 | u8 op_class; |
| 3460 | u8 min_chan; |
| 3461 | u8 max_chan; |
| 3462 | u8 inc; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3463 | enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3464 | }; |
| 3465 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3466 | static struct p2p_oper_class_map op_class[] = { |
| 3467 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3468 | #if 0 /* Do not enable HT40 on 2 GHz for now */ |
| 3469 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, |
| 3470 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, |
| 3471 | #endif |
| 3472 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
| 3473 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, |
| 3474 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, |
| 3475 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, |
| 3476 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, |
| 3477 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3478 | |
| 3479 | /* |
| 3480 | * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center |
| 3481 | * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of |
| 3482 | * 80 MHz, but currently use the following definition for simplicity |
| 3483 | * (these center frequencies are not actual channels, which makes |
| 3484 | * has_channel() fail). wpas_p2p_verify_80mhz() should take care of |
| 3485 | * removing invalid channels. |
| 3486 | */ |
| 3487 | { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3488 | { -1, 0, 0, 0, 0, BW20 } |
| 3489 | }; |
| 3490 | |
| 3491 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3492 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, |
| 3493 | struct hostapd_hw_modes *mode, |
| 3494 | u8 channel) |
| 3495 | { |
| 3496 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; |
| 3497 | unsigned int i; |
| 3498 | |
| 3499 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 3500 | return 0; |
| 3501 | |
| 3502 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) |
| 3503 | /* |
| 3504 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), |
| 3505 | * so the center channel is 6 channels away from the start/end. |
| 3506 | */ |
| 3507 | if (channel >= center_channels[i] - 6 && |
| 3508 | channel <= center_channels[i] + 6) |
| 3509 | return center_channels[i]; |
| 3510 | |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | |
| 3515 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, |
| 3516 | struct hostapd_hw_modes *mode, |
| 3517 | u8 channel, u8 bw) |
| 3518 | { |
| 3519 | u8 center_chan; |
| 3520 | int i, flags; |
| 3521 | enum chan_allowed res, ret = ALLOWED; |
| 3522 | |
| 3523 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3524 | if (!center_chan) |
| 3525 | return NOT_ALLOWED; |
| 3526 | if (center_chan >= 58 && center_chan <= 138) |
| 3527 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ |
| 3528 | |
| 3529 | /* check all the channels are available */ |
| 3530 | for (i = 0; i < 4; i++) { |
| 3531 | int adj_chan = center_chan - 6 + i * 4; |
| 3532 | |
| 3533 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); |
| 3534 | if (res == NOT_ALLOWED) |
| 3535 | return NOT_ALLOWED; |
| 3536 | if (res == PASSIVE_ONLY) |
| 3537 | ret = PASSIVE_ONLY; |
| 3538 | |
| 3539 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) |
| 3540 | return NOT_ALLOWED; |
| 3541 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) |
| 3542 | return NOT_ALLOWED; |
| 3543 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) |
| 3544 | return NOT_ALLOWED; |
| 3545 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) |
| 3546 | return NOT_ALLOWED; |
| 3547 | } |
| 3548 | |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
| 3552 | |
| 3553 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, |
| 3554 | struct hostapd_hw_modes *mode, |
| 3555 | u8 channel, u8 bw) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3556 | { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3557 | int flag = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3558 | enum chan_allowed res, res2; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3559 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3560 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); |
| 3561 | if (bw == BW40MINUS) { |
| 3562 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) |
| 3563 | return NOT_ALLOWED; |
| 3564 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); |
| 3565 | } else if (bw == BW40PLUS) { |
| 3566 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) |
| 3567 | return NOT_ALLOWED; |
| 3568 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); |
| 3569 | } else if (bw == BW80) { |
| 3570 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); |
| 3571 | } |
| 3572 | |
| 3573 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) |
| 3574 | return NOT_ALLOWED; |
| 3575 | if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY) |
| 3576 | return PASSIVE_ONLY; |
| 3577 | return res; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3581 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3582 | struct p2p_channels *chan, |
| 3583 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3584 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3585 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3586 | int cla, op, cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3587 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3588 | if (wpa_s->hw.modes == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3589 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
| 3590 | "of all supported channels; assume dualband " |
| 3591 | "support"); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3592 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3595 | cla = cli_cla = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3596 | |
| 3597 | for (op = 0; op_class[op].op_class; op++) { |
| 3598 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3599 | u8 ch; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3600 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3601 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3602 | mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3603 | if (mode == NULL) |
| 3604 | continue; |
| 3605 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3606 | enum chan_allowed res; |
| 3607 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
| 3608 | if (res == ALLOWED) { |
| 3609 | if (reg == NULL) { |
| 3610 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", |
| 3611 | o->op_class); |
| 3612 | reg = &chan->reg_class[cla]; |
| 3613 | cla++; |
| 3614 | reg->reg_class = o->op_class; |
| 3615 | } |
| 3616 | reg->channel[reg->channels] = ch; |
| 3617 | reg->channels++; |
| 3618 | } else if (res == PASSIVE_ONLY && |
| 3619 | wpa_s->conf->p2p_add_cli_chan) { |
| 3620 | if (cli_reg == NULL) { |
| 3621 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", |
| 3622 | o->op_class); |
| 3623 | cli_reg = &cli_chan->reg_class[cli_cla]; |
| 3624 | cli_cla++; |
| 3625 | cli_reg->reg_class = o->op_class; |
| 3626 | } |
| 3627 | cli_reg->channel[cli_reg->channels] = ch; |
| 3628 | cli_reg->channels++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3629 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3630 | } |
| 3631 | if (reg) { |
| 3632 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", |
| 3633 | reg->channel, reg->channels); |
| 3634 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3635 | if (cli_reg) { |
| 3636 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", |
| 3637 | cli_reg->channel, cli_reg->channels); |
| 3638 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | chan->reg_classes = cla; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3642 | cli_chan->reg_classes = cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3643 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3644 | return 0; |
| 3645 | } |
| 3646 | |
| 3647 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3648 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, |
| 3649 | struct hostapd_hw_modes *mode, u8 channel) |
| 3650 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3651 | int op; |
| 3652 | enum chan_allowed ret; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3653 | |
| 3654 | for (op = 0; op_class[op].op_class; op++) { |
| 3655 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3656 | u8 ch; |
| 3657 | |
| 3658 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
| 3659 | if (o->mode != HOSTAPD_MODE_IEEE80211A || |
| 3660 | o->bw == BW20 || ch != channel) |
| 3661 | continue; |
| 3662 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3663 | if (ret == ALLOWED) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3664 | return (o->bw == BW40MINUS) ? -1 : 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
| 3670 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3671 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, |
| 3672 | struct hostapd_hw_modes *mode, u8 channel) |
| 3673 | { |
| 3674 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) |
| 3675 | return 0; |
| 3676 | |
| 3677 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3678 | } |
| 3679 | |
| 3680 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3681 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, |
| 3682 | size_t buf_len) |
| 3683 | { |
| 3684 | struct wpa_supplicant *wpa_s = ctx; |
| 3685 | |
| 3686 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3687 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) |
| 3688 | break; |
| 3689 | } |
| 3690 | if (wpa_s == NULL) |
| 3691 | return -1; |
| 3692 | |
| 3693 | return wpa_drv_get_noa(wpa_s, buf, buf_len); |
| 3694 | } |
| 3695 | |
| 3696 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3697 | struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s, |
| 3698 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3699 | { |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3700 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3701 | struct wpa_ssid *s = wpa_s->current_ssid; |
| 3702 | if (s == NULL) |
| 3703 | continue; |
| 3704 | if (s->mode != WPAS_MODE_P2P_GO && |
| 3705 | s->mode != WPAS_MODE_AP && |
| 3706 | s->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
| 3707 | continue; |
| 3708 | if (s->ssid_len != ssid_len || |
| 3709 | os_memcmp(s, s->ssid, ssid_len) != 0) |
| 3710 | continue; |
| 3711 | return wpa_s; |
| 3712 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3713 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3714 | return NULL; |
| 3715 | |
| 3716 | } |
| 3717 | |
| 3718 | |
| 3719 | struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s, |
| 3720 | const u8 *peer_dev_addr) |
| 3721 | { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3722 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3723 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 3724 | if (ssid == NULL) |
| 3725 | continue; |
| 3726 | if (ssid->mode != WPAS_MODE_INFRA) |
| 3727 | continue; |
| 3728 | if (wpa_s->wpa_state != WPA_COMPLETED && |
| 3729 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) |
| 3730 | continue; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3731 | if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0) |
| 3732 | return wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3733 | } |
| 3734 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3735 | return NULL; |
| 3736 | } |
| 3737 | |
| 3738 | |
| 3739 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
| 3740 | { |
| 3741 | struct wpa_supplicant *wpa_s = ctx; |
| 3742 | |
| 3743 | return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3747 | static int wpas_is_concurrent_session_active(void *ctx) |
| 3748 | { |
| 3749 | struct wpa_supplicant *wpa_s = ctx; |
| 3750 | struct wpa_supplicant *ifs; |
| 3751 | |
| 3752 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 3753 | if (ifs == wpa_s) |
| 3754 | continue; |
| 3755 | if (ifs->wpa_state > WPA_ASSOCIATED) |
| 3756 | return 1; |
| 3757 | } |
| 3758 | return 0; |
| 3759 | } |
| 3760 | |
| 3761 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3762 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) |
| 3763 | { |
| 3764 | struct wpa_supplicant *wpa_s = ctx; |
| 3765 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); |
| 3766 | } |
| 3767 | |
| 3768 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3769 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s) |
| 3770 | { |
| 3771 | struct wpa_interface iface; |
| 3772 | struct wpa_supplicant *p2pdev_wpa_s; |
| 3773 | char ifname[100]; |
| 3774 | char force_name[100]; |
| 3775 | int ret; |
| 3776 | |
| 3777 | os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", |
| 3778 | wpa_s->ifname); |
| 3779 | force_name[0] = '\0'; |
| 3780 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; |
| 3781 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, |
| 3782 | force_name, wpa_s->pending_interface_addr, NULL); |
| 3783 | if (ret < 0) { |
| 3784 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); |
| 3785 | return ret; |
| 3786 | } |
| 3787 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 3788 | sizeof(wpa_s->pending_interface_name)); |
| 3789 | |
| 3790 | os_memset(&iface, 0, sizeof(iface)); |
| 3791 | iface.p2p_mgmt = 1; |
| 3792 | iface.ifname = wpa_s->pending_interface_name; |
| 3793 | iface.driver = wpa_s->driver->name; |
| 3794 | iface.driver_param = wpa_s->conf->driver_param; |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3795 | |
| 3796 | /* |
| 3797 | * If a P2P Device configuration file was given, use it as the interface |
| 3798 | * configuration file (instead of using parent's configuration file. |
| 3799 | */ |
| 3800 | if (wpa_s->conf_p2p_dev) { |
| 3801 | iface.confname = wpa_s->conf_p2p_dev; |
| 3802 | iface.ctrl_interface = NULL; |
| 3803 | } else { |
| 3804 | iface.confname = wpa_s->confname; |
| 3805 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
| 3806 | } |
| 3807 | iface.conf_p2p_dev = NULL; |
| 3808 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3809 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 3810 | if (!p2pdev_wpa_s) { |
| 3811 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); |
| 3812 | return -1; |
| 3813 | } |
| 3814 | p2pdev_wpa_s->parent = wpa_s; |
| 3815 | |
| 3816 | wpa_s->pending_interface_name[0] = '\0'; |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3821 | static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, |
| 3822 | const u8 *noa, size_t noa_len) |
| 3823 | { |
| 3824 | struct wpa_supplicant *wpa_s, *intf = ctx; |
| 3825 | char hex[100]; |
| 3826 | |
| 3827 | for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3828 | if (wpa_s->waiting_presence_resp) |
| 3829 | break; |
| 3830 | } |
| 3831 | if (!wpa_s) { |
| 3832 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); |
| 3833 | return; |
| 3834 | } |
| 3835 | wpa_s->waiting_presence_resp = 0; |
| 3836 | |
| 3837 | wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); |
| 3838 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR |
| 3839 | " status=%u noa=%s", MAC2STR(src), status, hex); |
| 3840 | } |
| 3841 | |
| 3842 | |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 3843 | static int _wpas_p2p_in_progress(void *ctx) |
| 3844 | { |
| 3845 | struct wpa_supplicant *wpa_s = ctx; |
| 3846 | return wpas_p2p_in_progress(wpa_s); |
| 3847 | } |
| 3848 | |
| 3849 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3850 | /** |
| 3851 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant |
| 3852 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3853 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3854 | * Returns: 0 on success, -1 on failure |
| 3855 | */ |
| 3856 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) |
| 3857 | { |
| 3858 | struct p2p_config p2p; |
| 3859 | unsigned int r; |
| 3860 | int i; |
| 3861 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3862 | if (wpa_s->conf->p2p_disabled) |
| 3863 | return 0; |
| 3864 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3865 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 3866 | return 0; |
| 3867 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3868 | if (global->p2p) |
| 3869 | return 0; |
| 3870 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3871 | os_memset(&p2p, 0, sizeof(p2p)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3872 | p2p.cb_ctx = wpa_s; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3873 | p2p.debug_print = wpas_p2p_debug_print; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3874 | p2p.p2p_scan = wpas_p2p_scan; |
| 3875 | p2p.send_action = wpas_send_action; |
| 3876 | p2p.send_action_done = wpas_send_action_done; |
| 3877 | p2p.go_neg_completed = wpas_go_neg_completed; |
| 3878 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; |
| 3879 | p2p.dev_found = wpas_dev_found; |
| 3880 | p2p.dev_lost = wpas_dev_lost; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3881 | p2p.find_stopped = wpas_find_stopped; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3882 | p2p.start_listen = wpas_start_listen; |
| 3883 | p2p.stop_listen = wpas_stop_listen; |
| 3884 | p2p.send_probe_resp = wpas_send_probe_resp; |
| 3885 | p2p.sd_request = wpas_sd_request; |
| 3886 | p2p.sd_response = wpas_sd_response; |
| 3887 | p2p.prov_disc_req = wpas_prov_disc_req; |
| 3888 | p2p.prov_disc_resp = wpas_prov_disc_resp; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3889 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3890 | p2p.invitation_process = wpas_invitation_process; |
| 3891 | p2p.invitation_received = wpas_invitation_received; |
| 3892 | p2p.invitation_result = wpas_invitation_result; |
| 3893 | p2p.get_noa = wpas_get_noa; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3894 | p2p.go_connected = wpas_go_connected; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3895 | p2p.presence_resp = wpas_presence_resp; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3896 | p2p.is_concurrent_session_active = wpas_is_concurrent_session_active; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 3897 | p2p.is_p2p_in_progress = _wpas_p2p_in_progress; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3898 | |
| 3899 | os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3900 | os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3901 | p2p.dev_name = wpa_s->conf->device_name; |
| 3902 | p2p.manufacturer = wpa_s->conf->manufacturer; |
| 3903 | p2p.model_name = wpa_s->conf->model_name; |
| 3904 | p2p.model_number = wpa_s->conf->model_number; |
| 3905 | p2p.serial_number = wpa_s->conf->serial_number; |
| 3906 | if (wpa_s->wps) { |
| 3907 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); |
| 3908 | p2p.config_methods = wpa_s->wps->config_methods; |
| 3909 | } |
| 3910 | |
| 3911 | if (wpa_s->conf->p2p_listen_reg_class && |
| 3912 | wpa_s->conf->p2p_listen_channel) { |
| 3913 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 3914 | p2p.channel = wpa_s->conf->p2p_listen_channel; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3915 | p2p.channel_forced = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3916 | } else { |
| 3917 | p2p.reg_class = 81; |
| 3918 | /* |
| 3919 | * Pick one of the social channels randomly as the listen |
| 3920 | * channel. |
| 3921 | */ |
| 3922 | os_get_random((u8 *) &r, sizeof(r)); |
| 3923 | p2p.channel = 1 + (r % 3) * 5; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3924 | p2p.channel_forced = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3925 | } |
| 3926 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
| 3927 | |
| 3928 | if (wpa_s->conf->p2p_oper_reg_class && |
| 3929 | wpa_s->conf->p2p_oper_channel) { |
| 3930 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 3931 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; |
| 3932 | p2p.cfg_op_channel = 1; |
| 3933 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " |
| 3934 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3935 | |
| 3936 | } else { |
| 3937 | p2p.op_reg_class = 81; |
| 3938 | /* |
| 3939 | * Use random operation channel from (1, 6, 11) if no other |
| 3940 | * preference is indicated. |
| 3941 | */ |
| 3942 | os_get_random((u8 *) &r, sizeof(r)); |
| 3943 | p2p.op_channel = 1 + (r % 3) * 5; |
| 3944 | p2p.cfg_op_channel = 0; |
| 3945 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " |
| 3946 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3947 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 3948 | |
| 3949 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { |
| 3950 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; |
| 3951 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; |
| 3952 | } |
| 3953 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3954 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 3955 | os_memcpy(p2p.country, wpa_s->conf->country, 2); |
| 3956 | p2p.country[2] = 0x04; |
| 3957 | } else |
| 3958 | os_memcpy(p2p.country, "XX\x04", 3); |
| 3959 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3960 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3961 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
| 3962 | "channel list"); |
| 3963 | return -1; |
| 3964 | } |
| 3965 | |
| 3966 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
| 3967 | WPS_DEV_TYPE_LEN); |
| 3968 | |
| 3969 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3970 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, |
| 3971 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3972 | |
| 3973 | p2p.concurrent_operations = !!(wpa_s->drv_flags & |
| 3974 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); |
| 3975 | |
| 3976 | p2p.max_peers = 100; |
| 3977 | |
| 3978 | if (wpa_s->conf->p2p_ssid_postfix) { |
| 3979 | p2p.ssid_postfix_len = |
| 3980 | os_strlen(wpa_s->conf->p2p_ssid_postfix); |
| 3981 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) |
| 3982 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); |
| 3983 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, |
| 3984 | p2p.ssid_postfix_len); |
| 3985 | } |
| 3986 | |
| 3987 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
| 3988 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3989 | p2p.max_listen = wpa_s->max_remain_on_chan; |
| 3990 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 3991 | if (wpa_s->conf->p2p_passphrase_len >= 8 && |
| 3992 | wpa_s->conf->p2p_passphrase_len <= 63) |
| 3993 | p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len; |
| 3994 | else |
| 3995 | p2p.passphrase_len = 8; |
| 3996 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3997 | global->p2p = p2p_init(&p2p); |
| 3998 | if (global->p2p == NULL) |
| 3999 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4000 | global->p2p_init_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4001 | |
| 4002 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 4003 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 4004 | continue; |
| 4005 | p2p_add_wps_vendor_extension( |
| 4006 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 4007 | } |
| 4008 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4009 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); |
| 4010 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4011 | return 0; |
| 4012 | } |
| 4013 | |
| 4014 | |
| 4015 | /** |
| 4016 | * wpas_p2p_deinit - Deinitialize per-interface P2P data |
| 4017 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4018 | * |
| 4019 | * This function deinitialize per-interface P2P data. |
| 4020 | */ |
| 4021 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) |
| 4022 | { |
| 4023 | if (wpa_s->driver && wpa_s->drv_priv) |
| 4024 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4025 | |
| 4026 | if (wpa_s->go_params) { |
| 4027 | /* Clear any stored provisioning info */ |
| 4028 | p2p_clear_provisioning_info( |
| 4029 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4030 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4033 | os_free(wpa_s->go_params); |
| 4034 | wpa_s->go_params = NULL; |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 4035 | eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4036 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4037 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4038 | wpa_s->p2p_long_listen = 0; |
| 4039 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 4040 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 4041 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 4042 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4043 | wpas_p2p_listen_work_done(wpa_s); |
| 4044 | if (wpa_s->p2p_send_action_work) { |
| 4045 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 4046 | radio_work_done(wpa_s->p2p_send_action_work); |
| 4047 | wpa_s->p2p_send_action_work = NULL; |
| 4048 | } |
| 4049 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4050 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4051 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 4052 | wpa_s->p2p_oob_dev_pw = NULL; |
| 4053 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4054 | /* TODO: remove group interface from the driver if this wpa_s instance |
| 4055 | * is on top of a P2P group interface */ |
| 4056 | } |
| 4057 | |
| 4058 | |
| 4059 | /** |
| 4060 | * wpas_p2p_deinit_global - Deinitialize global P2P module |
| 4061 | * @global: Pointer to global data from wpa_supplicant_init() |
| 4062 | * |
| 4063 | * This function deinitializes the global (per device) P2P module. |
| 4064 | */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4065 | static void wpas_p2p_deinit_global(struct wpa_global *global) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4066 | { |
| 4067 | struct wpa_supplicant *wpa_s, *tmp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4068 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4069 | wpa_s = global->ifaces; |
| 4070 | if (wpa_s) |
| 4071 | wpas_p2p_service_flush(wpa_s); |
| 4072 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4073 | if (global->p2p == NULL) |
| 4074 | return; |
| 4075 | |
| 4076 | /* Remove remaining P2P group interfaces */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4077 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
| 4078 | wpa_s = wpa_s->next; |
| 4079 | while (wpa_s) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4080 | tmp = global->ifaces; |
| 4081 | while (tmp && |
| 4082 | (tmp == wpa_s || |
| 4083 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { |
| 4084 | tmp = tmp->next; |
| 4085 | } |
| 4086 | if (tmp == NULL) |
| 4087 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4088 | /* Disconnect from the P2P group and deinit the interface */ |
| 4089 | wpas_p2p_disconnect(tmp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | /* |
| 4093 | * Deinit GO data on any possibly remaining interface (if main |
| 4094 | * interface is used as GO). |
| 4095 | */ |
| 4096 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 4097 | if (wpa_s->ap_iface) |
| 4098 | wpas_p2p_group_deinit(wpa_s); |
| 4099 | } |
| 4100 | |
| 4101 | p2p_deinit(global->p2p); |
| 4102 | global->p2p = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4103 | global->p2p_init_wpa_s = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
| 4107 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) |
| 4108 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4109 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) && |
| 4110 | wpa_s->conf->p2p_no_group_iface) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4111 | return 0; /* separate interface disabled per configuration */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4112 | if (wpa_s->drv_flags & |
| 4113 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | |
| 4114 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) |
| 4115 | return 1; /* P2P group requires a new interface in every case |
| 4116 | */ |
| 4117 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) |
| 4118 | return 0; /* driver does not support concurrent operations */ |
| 4119 | if (wpa_s->global->ifaces->next) |
| 4120 | return 1; /* more that one interface already in use */ |
| 4121 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) |
| 4122 | return 1; /* this interface is already in use */ |
| 4123 | return 0; |
| 4124 | } |
| 4125 | |
| 4126 | |
| 4127 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, |
| 4128 | const u8 *peer_addr, |
| 4129 | enum p2p_wps_method wps_method, |
| 4130 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4131 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4132 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4133 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4134 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4135 | persistent_group = 2; |
| 4136 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4137 | /* |
| 4138 | * Increase GO config timeout if HT40 is used since it takes some time |
| 4139 | * to scan channels for coex purposes before the BSS can be started. |
| 4140 | */ |
| 4141 | p2p_set_config_timeout(wpa_s->global->p2p, |
| 4142 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); |
| 4143 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4144 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
| 4145 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4146 | persistent_group, ssid ? ssid->ssid : NULL, |
| 4147 | ssid ? ssid->ssid_len : 0, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4148 | wpa_s->p2p_pd_before_go_neg, pref_freq, |
| 4149 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4150 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | |
| 4154 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, |
| 4155 | const u8 *peer_addr, |
| 4156 | enum p2p_wps_method wps_method, |
| 4157 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4158 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4159 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4160 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4161 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4162 | persistent_group = 2; |
| 4163 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4164 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
| 4165 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4166 | persistent_group, ssid ? ssid->ssid : NULL, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4167 | ssid ? ssid->ssid_len : 0, pref_freq, |
| 4168 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4169 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | |
| 4173 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
| 4174 | { |
| 4175 | wpa_s->p2p_join_scan_count++; |
| 4176 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", |
| 4177 | wpa_s->p2p_join_scan_count); |
| 4178 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { |
| 4179 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR |
| 4180 | " for join operationg - stop join attempt", |
| 4181 | MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4182 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4183 | if (wpa_s->p2p_auto_pd) { |
| 4184 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4185 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4186 | P2P_EVENT_PROV_DISC_FAILURE |
| 4187 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4188 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4189 | return; |
| 4190 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4191 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4192 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4197 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
| 4198 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4199 | int res; |
| 4200 | unsigned int num, i; |
| 4201 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4202 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4203 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 4204 | /* Multiple channels are supported and not all are in use */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4205 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4206 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4207 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4208 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 4209 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4210 | if (!freqs) |
| 4211 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4212 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4213 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4214 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4215 | |
| 4216 | for (i = 0; i < num; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4217 | if (freqs[i].freq == freq) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4218 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", |
| 4219 | freq); |
| 4220 | res = 0; |
| 4221 | goto exit_free; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4225 | wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies"); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4226 | res = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4227 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4228 | exit_free: |
| 4229 | os_free(freqs); |
| 4230 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | |
| 4234 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
| 4235 | const u8 *peer_dev_addr) |
| 4236 | { |
| 4237 | struct wpa_bss *bss; |
| 4238 | int updated; |
| 4239 | |
| 4240 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); |
| 4241 | if (bss == NULL) |
| 4242 | return -1; |
| 4243 | if (bss->last_update_idx < wpa_s->bss_update_idx) { |
| 4244 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " |
| 4245 | "last scan"); |
| 4246 | return 0; |
| 4247 | } |
| 4248 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4249 | updated = os_reltime_before(&wpa_s->p2p_auto_started, |
| 4250 | &bss->last_update); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4251 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " |
| 4252 | "%ld.%06ld (%supdated in last scan)", |
| 4253 | bss->last_update.sec, bss->last_update.usec, |
| 4254 | updated ? "": "not "); |
| 4255 | |
| 4256 | return updated; |
| 4257 | } |
| 4258 | |
| 4259 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4260 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
| 4261 | struct wpa_scan_results *scan_res) |
| 4262 | { |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4263 | struct wpa_bss *bss = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4264 | int freq; |
| 4265 | u8 iface_addr[ETH_ALEN]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4266 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4267 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4268 | |
| 4269 | if (wpa_s->global->p2p_disabled) |
| 4270 | return; |
| 4271 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4272 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
| 4273 | scan_res ? (int) scan_res->num : -1, |
| 4274 | wpa_s->p2p_auto_join ? "auto_" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4275 | |
| 4276 | if (scan_res) |
| 4277 | wpas_p2p_scan_res_handler(wpa_s, scan_res); |
| 4278 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4279 | if (wpa_s->p2p_auto_pd) { |
| 4280 | int join = wpas_p2p_peer_go(wpa_s, |
| 4281 | wpa_s->pending_join_dev_addr); |
| 4282 | if (join == 0 && |
| 4283 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { |
| 4284 | wpa_s->auto_pd_scan_retry++; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4285 | bss = wpa_bss_get_bssid_latest( |
| 4286 | wpa_s, wpa_s->pending_join_dev_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4287 | if (bss) { |
| 4288 | freq = bss->freq; |
| 4289 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " |
| 4290 | "the peer " MACSTR " at %d MHz", |
| 4291 | wpa_s->auto_pd_scan_retry, |
| 4292 | MAC2STR(wpa_s-> |
| 4293 | pending_join_dev_addr), |
| 4294 | freq); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4295 | wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4296 | return; |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | if (join < 0) |
| 4301 | join = 0; |
| 4302 | |
| 4303 | wpa_s->p2p_auto_pd = 0; |
| 4304 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; |
| 4305 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", |
| 4306 | MAC2STR(wpa_s->pending_join_dev_addr), join); |
| 4307 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
| 4308 | wpa_s->pending_join_dev_addr, |
| 4309 | wpa_s->pending_pd_config_methods, join, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4310 | 0, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4311 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4312 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4313 | P2P_EVENT_PROV_DISC_FAILURE |
| 4314 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4315 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4316 | } |
| 4317 | return; |
| 4318 | } |
| 4319 | |
| 4320 | if (wpa_s->p2p_auto_join) { |
| 4321 | int join = wpas_p2p_peer_go(wpa_s, |
| 4322 | wpa_s->pending_join_dev_addr); |
| 4323 | if (join < 0) { |
| 4324 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " |
| 4325 | "running a GO -> use GO Negotiation"); |
| 4326 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, |
| 4327 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, |
| 4328 | wpa_s->p2p_persistent_group, 0, 0, 0, |
| 4329 | wpa_s->p2p_go_intent, |
| 4330 | wpa_s->p2p_connect_freq, |
| 4331 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4332 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4333 | wpa_s->p2p_go_ht40, |
| 4334 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
| 4338 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " |
| 4339 | "try to join the group", join ? "" : |
| 4340 | " in older scan"); |
| 4341 | if (!join) |
| 4342 | wpa_s->p2p_fallback_to_go_neg = 1; |
| 4343 | } |
| 4344 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4345 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4346 | wpa_s->pending_join_iface_addr); |
| 4347 | if (freq < 0 && |
| 4348 | p2p_get_interface_addr(wpa_s->global->p2p, |
| 4349 | wpa_s->pending_join_dev_addr, |
| 4350 | iface_addr) == 0 && |
| 4351 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) |
| 4352 | { |
| 4353 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " |
| 4354 | "address for join from " MACSTR " to " MACSTR |
| 4355 | " based on newly discovered P2P peer entry", |
| 4356 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4357 | MAC2STR(iface_addr)); |
| 4358 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, |
| 4359 | ETH_ALEN); |
| 4360 | |
| 4361 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4362 | wpa_s->pending_join_iface_addr); |
| 4363 | } |
| 4364 | if (freq >= 0) { |
| 4365 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
| 4366 | "from P2P peer table: %d MHz", freq); |
| 4367 | } |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4368 | if (wpa_s->p2p_join_ssid_len) { |
| 4369 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4370 | MACSTR " and SSID %s", |
| 4371 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4372 | wpa_ssid_txt(wpa_s->p2p_join_ssid, |
| 4373 | wpa_s->p2p_join_ssid_len)); |
| 4374 | bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, |
| 4375 | wpa_s->p2p_join_ssid, |
| 4376 | wpa_s->p2p_join_ssid_len); |
| 4377 | } |
| 4378 | if (!bss) { |
| 4379 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4380 | MACSTR, MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4381 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4382 | wpa_s->pending_join_iface_addr); |
| 4383 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4384 | if (bss) { |
| 4385 | freq = bss->freq; |
| 4386 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4387 | "from BSS table: %d MHz (SSID %s)", freq, |
| 4388 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4389 | } |
| 4390 | if (freq > 0) { |
| 4391 | u16 method; |
| 4392 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4393 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4394 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4395 | P2P_EVENT_GROUP_FORMATION_FAILURE |
| 4396 | "reason=FREQ_CONFLICT"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4397 | return; |
| 4398 | } |
| 4399 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4400 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
| 4401 | "prior to joining an existing group (GO " MACSTR |
| 4402 | " freq=%u MHz)", |
| 4403 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
| 4404 | wpa_s->pending_pd_before_join = 1; |
| 4405 | |
| 4406 | switch (wpa_s->pending_join_wps_method) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4407 | case WPS_PIN_DISPLAY: |
| 4408 | method = WPS_CONFIG_KEYPAD; |
| 4409 | break; |
| 4410 | case WPS_PIN_KEYPAD: |
| 4411 | method = WPS_CONFIG_DISPLAY; |
| 4412 | break; |
| 4413 | case WPS_PBC: |
| 4414 | method = WPS_CONFIG_PUSHBUTTON; |
| 4415 | break; |
| 4416 | default: |
| 4417 | method = 0; |
| 4418 | break; |
| 4419 | } |
| 4420 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4421 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
| 4422 | wpa_s->pending_join_dev_addr) == |
| 4423 | method)) { |
| 4424 | /* |
| 4425 | * We have already performed provision discovery for |
| 4426 | * joining the group. Proceed directly to join |
| 4427 | * operation without duplicated provision discovery. */ |
| 4428 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
| 4429 | "with " MACSTR " already done - proceed to " |
| 4430 | "join", |
| 4431 | MAC2STR(wpa_s->pending_join_dev_addr)); |
| 4432 | wpa_s->pending_pd_before_join = 0; |
| 4433 | goto start; |
| 4434 | } |
| 4435 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4436 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4437 | wpa_s->pending_join_dev_addr, method, 1, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4438 | freq, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4439 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
| 4440 | "Discovery Request before joining an " |
| 4441 | "existing group"); |
| 4442 | wpa_s->pending_pd_before_join = 0; |
| 4443 | goto start; |
| 4444 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4445 | return; |
| 4446 | } |
| 4447 | |
| 4448 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
| 4449 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4450 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4451 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4452 | return; |
| 4453 | |
| 4454 | start: |
| 4455 | /* Start join operation immediately */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4456 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4460 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 4461 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4462 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4463 | int ret; |
| 4464 | struct wpa_driver_scan_params params; |
| 4465 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4466 | size_t ielen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4467 | int freqs[2] = { 0, 0 }; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4468 | |
| 4469 | os_memset(¶ms, 0, sizeof(params)); |
| 4470 | |
| 4471 | /* P2P Wildcard SSID */ |
| 4472 | params.num_ssids = 1; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4473 | if (ssid && ssid_len) { |
| 4474 | params.ssids[0].ssid = ssid; |
| 4475 | params.ssids[0].ssid_len = ssid_len; |
| 4476 | os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len); |
| 4477 | wpa_s->p2p_join_ssid_len = ssid_len; |
| 4478 | } else { |
| 4479 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 4480 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 4481 | wpa_s->p2p_join_ssid_len = 0; |
| 4482 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4483 | |
| 4484 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4485 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
| 4486 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, |
| 4487 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4488 | if (wps_ie == NULL) { |
| 4489 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4490 | return; |
| 4491 | } |
| 4492 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4493 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 4494 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4495 | if (ies == NULL) { |
| 4496 | wpabuf_free(wps_ie); |
| 4497 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4498 | return; |
| 4499 | } |
| 4500 | wpabuf_put_buf(ies, wps_ie); |
| 4501 | wpabuf_free(wps_ie); |
| 4502 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4503 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4504 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4505 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4506 | params.extra_ies = wpabuf_head(ies); |
| 4507 | params.extra_ies_len = wpabuf_len(ies); |
Dmitry Shmidt | 9e3f8ee | 2014-01-17 10:52:01 -0800 | [diff] [blame] | 4508 | |
| 4509 | if (!freq) { |
| 4510 | int oper_freq; |
| 4511 | /* |
| 4512 | * If freq is not provided, check the operating freq of the GO |
| 4513 | * and use a single channel scan on if possible. |
| 4514 | */ |
| 4515 | oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4516 | wpa_s->pending_join_iface_addr); |
| 4517 | if (oper_freq > 0) |
| 4518 | freq = oper_freq; |
| 4519 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4520 | if (freq > 0) { |
| 4521 | freqs[0] = freq; |
| 4522 | params.freqs = freqs; |
| 4523 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4524 | |
| 4525 | /* |
| 4526 | * Run a scan to update BSS table and start Provision Discovery once |
| 4527 | * the new scan results become available. |
| 4528 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4529 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4530 | if (!ret) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4531 | os_get_reltime(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4532 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4533 | wpa_s->own_scan_requested = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4534 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4535 | |
| 4536 | wpabuf_free(ies); |
| 4537 | |
| 4538 | if (ret) { |
| 4539 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " |
| 4540 | "try again later"); |
| 4541 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4542 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4543 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4548 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) |
| 4549 | { |
| 4550 | struct wpa_supplicant *wpa_s = eloop_ctx; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4551 | wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
| 4554 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4555 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4556 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4557 | int auto_join, int op_freq, |
| 4558 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4559 | { |
| 4560 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4561 | MACSTR " dev " MACSTR " op_freq=%d)%s", |
| 4562 | MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4563 | auto_join ? " (auto_join)" : ""); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4564 | if (ssid && ssid_len) { |
| 4565 | wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s", |
| 4566 | wpa_ssid_txt(ssid, ssid_len)); |
| 4567 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4568 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4569 | wpa_s->p2p_auto_pd = 0; |
| 4570 | wpa_s->p2p_auto_join = !!auto_join; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4571 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
| 4572 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); |
| 4573 | wpa_s->pending_join_wps_method = wps_method; |
| 4574 | |
| 4575 | /* Make sure we are not running find during connection establishment */ |
| 4576 | wpas_p2p_stop_find(wpa_s); |
| 4577 | |
| 4578 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4579 | wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4580 | return 0; |
| 4581 | } |
| 4582 | |
| 4583 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4584 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 4585 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4586 | { |
| 4587 | struct wpa_supplicant *group; |
| 4588 | struct p2p_go_neg_results res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4589 | struct wpa_bss *bss; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4590 | |
| 4591 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
| 4592 | if (group == NULL) |
| 4593 | return -1; |
| 4594 | if (group != wpa_s) { |
| 4595 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
| 4596 | sizeof(group->p2p_pin)); |
| 4597 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 4598 | } else { |
| 4599 | /* |
| 4600 | * Need to mark the current interface for p2p_group_formation |
| 4601 | * when a separate group interface is not used. This is needed |
| 4602 | * to allow p2p_cancel stop a pending p2p_connect-join. |
| 4603 | * wpas_p2p_init_group_interface() addresses this for the case |
| 4604 | * where a separate group interface is used. |
| 4605 | */ |
| 4606 | wpa_s->global->p2p_group_formation = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | group->p2p_in_provisioning = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4610 | group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4611 | |
| 4612 | os_memset(&res, 0, sizeof(res)); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4613 | os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4614 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, |
| 4615 | ETH_ALEN); |
| 4616 | res.wps_method = wpa_s->pending_join_wps_method; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4617 | if (freq && ssid && ssid_len) { |
| 4618 | res.freq = freq; |
| 4619 | res.ssid_len = ssid_len; |
| 4620 | os_memcpy(res.ssid, ssid, ssid_len); |
| 4621 | } else { |
| 4622 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4623 | wpa_s->pending_join_iface_addr); |
| 4624 | if (bss) { |
| 4625 | res.freq = bss->freq; |
| 4626 | res.ssid_len = bss->ssid_len; |
| 4627 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); |
| 4628 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)", |
| 4629 | bss->freq, |
| 4630 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
| 4631 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4632 | } |
| 4633 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4634 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 4635 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " |
| 4636 | "starting client"); |
| 4637 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 4638 | wpa_s->off_channel_freq = 0; |
| 4639 | wpa_s->roc_waiting_drv_freq = 0; |
| 4640 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4641 | wpas_start_wps_enrollee(group, &res); |
| 4642 | |
| 4643 | /* |
| 4644 | * Allow a longer timeout for join-a-running-group than normal 15 |
| 4645 | * second group formation timeout since the GO may not have authorized |
| 4646 | * our connection yet. |
| 4647 | */ |
| 4648 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4649 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, |
| 4650 | wpa_s, NULL); |
| 4651 | |
| 4652 | return 0; |
| 4653 | } |
| 4654 | |
| 4655 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4656 | static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4657 | int *force_freq, int *pref_freq, int go) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4658 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4659 | struct wpa_used_freq_data *freqs; |
| 4660 | int res, best_freq, num_unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4661 | unsigned int freq_in_use = 0, num, i; |
| 4662 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4663 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 4664 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4665 | if (!freqs) |
| 4666 | return -1; |
| 4667 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4668 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4669 | wpa_s->num_multichan_concurrent); |
| 4670 | |
| 4671 | /* |
| 4672 | * It is possible that the total number of used frequencies is bigger |
| 4673 | * than the number of frequencies used for P2P, so get the system wide |
| 4674 | * number of unused frequencies. |
| 4675 | */ |
| 4676 | num_unused = wpas_p2p_num_unused_channels(wpa_s); |
| 4677 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4678 | wpa_printf(MSG_DEBUG, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4679 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d", |
| 4680 | freq, wpa_s->num_multichan_concurrent, num, num_unused); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4681 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4682 | if (freq > 0) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4683 | int ret; |
| 4684 | if (go) |
| 4685 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); |
| 4686 | else |
| 4687 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); |
| 4688 | if (!ret) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4689 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " |
| 4690 | "(%u MHz) is not supported for P2P uses", |
| 4691 | freq); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4692 | res = -3; |
| 4693 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4694 | } |
| 4695 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4696 | for (i = 0; i < num; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4697 | if (freqs[i].freq == freq) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4698 | freq_in_use = 1; |
| 4699 | } |
| 4700 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4701 | if (num_unused <= 0 && !freq_in_use) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4702 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", |
| 4703 | freq); |
| 4704 | res = -2; |
| 4705 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4706 | } |
| 4707 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
| 4708 | "requested channel (%u MHz)", freq); |
| 4709 | *force_freq = freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4710 | goto exit_ok; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4713 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4714 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4715 | /* We have a candidate frequency to use */ |
| 4716 | if (best_freq > 0) { |
| 4717 | if (*pref_freq == 0 && num_unused > 0) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4718 | wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4719 | best_freq); |
| 4720 | *pref_freq = best_freq; |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4721 | } else { |
| 4722 | wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4723 | best_freq); |
| 4724 | *force_freq = best_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4725 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4726 | } else if (num_unused > 0) { |
| 4727 | wpa_printf(MSG_DEBUG, |
| 4728 | "P2P: Current operating channels are not available for P2P. Try to use another channel"); |
| 4729 | *force_freq = 0; |
| 4730 | } else { |
| 4731 | wpa_printf(MSG_DEBUG, |
| 4732 | "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); |
| 4733 | res = -2; |
| 4734 | goto exit_free; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | exit_ok: |
| 4738 | res = 0; |
| 4739 | exit_free: |
| 4740 | os_free(freqs); |
| 4741 | return res; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4745 | /** |
| 4746 | * wpas_p2p_connect - Request P2P Group Formation to be started |
| 4747 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4748 | * @peer_addr: Address of the peer P2P Device |
| 4749 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode |
| 4750 | * @persistent_group: Whether to create a persistent group |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4751 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4752 | * @join: Whether to join an existing group (as a client) instead of starting |
| 4753 | * Group Owner negotiation; @peer_addr is BSSID in that case |
| 4754 | * @auth: Whether to only authorize the connection instead of doing that and |
| 4755 | * initiating Group Owner negotiation |
| 4756 | * @go_intent: GO Intent or -1 to use default |
| 4757 | * @freq: Frequency for the group or 0 for auto-selection |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4758 | * @persistent_id: Persistent group credentials to use for forcing GO |
| 4759 | * parameters or -1 to generate new values (SSID/passphrase) |
| 4760 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an |
| 4761 | * interoperability workaround when initiating group formation |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4762 | * @ht40: Start GO with 40 MHz channel width |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4763 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4764 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
| 4765 | * failure, -2 on failure due to channel not currently available, |
| 4766 | * -3 if forced channel is not supported |
| 4767 | */ |
| 4768 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4769 | const char *pin, enum p2p_wps_method wps_method, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4770 | int persistent_group, int auto_join, int join, int auth, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4771 | int go_intent, int freq, int persistent_id, int pd, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4772 | int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4773 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4774 | int force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4775 | int ret = 0, res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4776 | enum wpa_driver_if_type iftype; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4777 | const u8 *if_addr; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4778 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4779 | |
| 4780 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4781 | return -1; |
| 4782 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4783 | if (persistent_id >= 0) { |
| 4784 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); |
| 4785 | if (ssid == NULL || ssid->disabled != 2 || |
| 4786 | ssid->mode != WPAS_MODE_P2P_GO) |
| 4787 | return -1; |
| 4788 | } |
| 4789 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4790 | os_free(wpa_s->global->add_psk); |
| 4791 | wpa_s->global->add_psk = NULL; |
| 4792 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4793 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 4794 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4795 | if (go_intent < 0) |
| 4796 | go_intent = wpa_s->conf->p2p_go_intent; |
| 4797 | |
| 4798 | if (!auth) |
| 4799 | wpa_s->p2p_long_listen = 0; |
| 4800 | |
| 4801 | wpa_s->p2p_wps_method = wps_method; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4802 | wpa_s->p2p_persistent_group = !!persistent_group; |
| 4803 | wpa_s->p2p_persistent_id = persistent_id; |
| 4804 | wpa_s->p2p_go_intent = go_intent; |
| 4805 | wpa_s->p2p_connect_freq = freq; |
| 4806 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4807 | wpa_s->p2p_pd_before_go_neg = !!pd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4808 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4809 | wpa_s->p2p_go_vht = !!vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4810 | |
| 4811 | if (pin) |
| 4812 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); |
| 4813 | else if (wps_method == WPS_PIN_DISPLAY) { |
| 4814 | ret = wps_generate_pin(); |
| 4815 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", |
| 4816 | ret); |
| 4817 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", |
| 4818 | wpa_s->p2p_pin); |
| 4819 | } else |
| 4820 | wpa_s->p2p_pin[0] = '\0'; |
| 4821 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4822 | if (join || auto_join) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4823 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
| 4824 | if (auth) { |
| 4825 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " |
| 4826 | "connect a running group from " MACSTR, |
| 4827 | MAC2STR(peer_addr)); |
| 4828 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 4829 | return ret; |
| 4830 | } |
| 4831 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
| 4832 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
| 4833 | iface_addr) < 0) { |
| 4834 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
| 4835 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
| 4836 | dev_addr); |
| 4837 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4838 | if (auto_join) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4839 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4840 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " |
| 4841 | "%ld.%06ld", |
| 4842 | wpa_s->p2p_auto_started.sec, |
| 4843 | wpa_s->p2p_auto_started.usec); |
| 4844 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4845 | wpa_s->user_initiated_pd = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4846 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4847 | auto_join, freq, NULL, 0) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4848 | return -1; |
| 4849 | return ret; |
| 4850 | } |
| 4851 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4852 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 4853 | go_intent == 15); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4854 | if (res) |
| 4855 | return res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 4856 | wpas_p2p_set_own_freq_preference(wpa_s, |
| 4857 | force_freq ? force_freq : pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4858 | |
| 4859 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 4860 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4861 | if (wpa_s->create_p2p_iface) { |
| 4862 | /* Prepare to add a new interface for the group */ |
| 4863 | iftype = WPA_IF_P2P_GROUP; |
| 4864 | if (go_intent == 15) |
| 4865 | iftype = WPA_IF_P2P_GO; |
| 4866 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 4867 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 4868 | "interface for the group"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4869 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4870 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4871 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4872 | if_addr = wpa_s->pending_interface_addr; |
| 4873 | } else |
| 4874 | if_addr = wpa_s->own_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4875 | |
| 4876 | if (auth) { |
| 4877 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4878 | go_intent, if_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4879 | force_freq, persistent_group, ssid, |
| 4880 | pref_freq) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4881 | return -1; |
| 4882 | return ret; |
| 4883 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4884 | |
| 4885 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, |
| 4886 | go_intent, if_addr, force_freq, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4887 | persistent_group, ssid, pref_freq) < 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4888 | if (wpa_s->create_p2p_iface) |
| 4889 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4890 | return -1; |
| 4891 | } |
| 4892 | return ret; |
| 4893 | } |
| 4894 | |
| 4895 | |
| 4896 | /** |
| 4897 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start |
| 4898 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4899 | * @freq: Frequency of the channel in MHz |
| 4900 | * @duration: Duration of the stay on the channel in milliseconds |
| 4901 | * |
| 4902 | * This callback is called when the driver indicates that it has started the |
| 4903 | * requested remain-on-channel duration. |
| 4904 | */ |
| 4905 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4906 | unsigned int freq, unsigned int duration) |
| 4907 | { |
| 4908 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4909 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4910 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
| 4911 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, |
| 4912 | wpa_s->pending_listen_duration); |
| 4913 | wpa_s->pending_listen_freq = 0; |
Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 4914 | } else { |
| 4915 | wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)", |
| 4916 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, |
| 4917 | freq, duration); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | |
| 4922 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, |
| 4923 | unsigned int timeout) |
| 4924 | { |
| 4925 | /* Limit maximum Listen state time based on driver limitation. */ |
| 4926 | if (timeout > wpa_s->max_remain_on_chan) |
| 4927 | timeout = wpa_s->max_remain_on_chan; |
| 4928 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4929 | return p2p_listen(wpa_s->global->p2p, timeout); |
| 4930 | } |
| 4931 | |
| 4932 | |
| 4933 | /** |
| 4934 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout |
| 4935 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4936 | * @freq: Frequency of the channel in MHz |
| 4937 | * |
| 4938 | * This callback is called when the driver indicates that a remain-on-channel |
| 4939 | * operation has been completed, i.e., the duration on the requested channel |
| 4940 | * has timed out. |
| 4941 | */ |
| 4942 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4943 | unsigned int freq) |
| 4944 | { |
| 4945 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
| 4946 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4947 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4948 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4949 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4950 | return; |
| 4951 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
| 4952 | return; /* P2P module started a new operation */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4953 | if (offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4954 | return; |
| 4955 | if (wpa_s->p2p_long_listen > 0) |
| 4956 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
| 4957 | if (wpa_s->p2p_long_listen > 0) { |
| 4958 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); |
| 4959 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4960 | } else { |
| 4961 | /* |
| 4962 | * When listen duration is over, stop listen & update p2p_state |
| 4963 | * to IDLE. |
| 4964 | */ |
| 4965 | p2p_stop_listen(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | |
| 4970 | /** |
| 4971 | * wpas_p2p_group_remove - Remove a P2P group |
| 4972 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4973 | * @ifname: Network interface name of the group interface or "*" to remove all |
| 4974 | * groups |
| 4975 | * Returns: 0 on success, -1 on failure |
| 4976 | * |
| 4977 | * This function is used to remove a P2P group. This can be used to disconnect |
| 4978 | * from a group in which the local end is a P2P Client or to end a P2P Group in |
| 4979 | * case the local end is the Group Owner. If a virtual network interface was |
| 4980 | * created for this group, that interface will be removed. Otherwise, only the |
| 4981 | * configured P2P group network will be removed from the interface. |
| 4982 | */ |
| 4983 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) |
| 4984 | { |
| 4985 | struct wpa_global *global = wpa_s->global; |
| 4986 | |
| 4987 | if (os_strcmp(ifname, "*") == 0) { |
| 4988 | struct wpa_supplicant *prev; |
| 4989 | wpa_s = global->ifaces; |
| 4990 | while (wpa_s) { |
| 4991 | prev = wpa_s; |
| 4992 | wpa_s = wpa_s->next; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4993 | if (prev->p2p_group_interface != |
| 4994 | NOT_P2P_GROUP_INTERFACE || |
| 4995 | (prev->current_ssid && |
| 4996 | prev->current_ssid->p2p_group)) |
| 4997 | wpas_p2p_disconnect(prev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4998 | } |
| 4999 | return 0; |
| 5000 | } |
| 5001 | |
| 5002 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5003 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5004 | break; |
| 5005 | } |
| 5006 | |
| 5007 | return wpas_p2p_disconnect(wpa_s); |
| 5008 | } |
| 5009 | |
| 5010 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5011 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) |
| 5012 | { |
| 5013 | unsigned int r; |
| 5014 | |
| 5015 | if (freq == 2) { |
| 5016 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " |
| 5017 | "band"); |
| 5018 | if (wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5019 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5020 | wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5021 | freq = wpa_s->best_24_freq; |
| 5022 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " |
| 5023 | "channel: %d MHz", freq); |
| 5024 | } else { |
| 5025 | os_get_random((u8 *) &r, sizeof(r)); |
| 5026 | freq = 2412 + (r % 3) * 25; |
| 5027 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " |
| 5028 | "channel: %d MHz", freq); |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | if (freq == 5) { |
| 5033 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " |
| 5034 | "band"); |
| 5035 | if (wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5036 | p2p_supported_freq_go(wpa_s->global->p2p, |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5037 | wpa_s->best_5_freq)) { |
| 5038 | freq = wpa_s->best_5_freq; |
| 5039 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " |
| 5040 | "channel: %d MHz", freq); |
| 5041 | } else { |
| 5042 | os_get_random((u8 *) &r, sizeof(r)); |
| 5043 | freq = 5180 + (r % 4) * 20; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5044 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5045 | wpa_printf(MSG_DEBUG, "P2P: Could not select " |
| 5046 | "5 GHz channel for P2P group"); |
| 5047 | return -1; |
| 5048 | } |
| 5049 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " |
| 5050 | "channel: %d MHz", freq); |
| 5051 | } |
| 5052 | } |
| 5053 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5054 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5055 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " |
| 5056 | "(%u MHz) is not supported for P2P uses", |
| 5057 | freq); |
| 5058 | return -1; |
| 5059 | } |
| 5060 | |
| 5061 | return freq; |
| 5062 | } |
| 5063 | |
| 5064 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5065 | static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s, |
| 5066 | struct p2p_go_neg_results *params, |
| 5067 | const struct p2p_channels *channels) |
| 5068 | { |
| 5069 | unsigned int i, r; |
| 5070 | |
| 5071 | /* first try some random selection of the social channels */ |
| 5072 | os_get_random((u8 *) &r, sizeof(r)); |
| 5073 | |
| 5074 | for (i = 0; i < 3; i++) { |
| 5075 | params->freq = 2412 + ((r + i) % 3) * 25; |
| 5076 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5077 | freq_included(channels, params->freq)) |
| 5078 | goto out; |
| 5079 | } |
| 5080 | |
| 5081 | /* try all channels in reg. class 81 */ |
| 5082 | for (i = 0; i < 11; i++) { |
| 5083 | params->freq = 2412 + i * 5; |
| 5084 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5085 | freq_included(channels, params->freq)) |
| 5086 | goto out; |
| 5087 | } |
| 5088 | |
| 5089 | wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel allowed"); |
| 5090 | return -1; |
| 5091 | out: |
| 5092 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)", |
| 5093 | params->freq); |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
| 5097 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5098 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
| 5099 | struct p2p_go_neg_results *params, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5100 | int freq, int ht40, int vht, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5101 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5102 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5103 | struct wpa_used_freq_data *freqs; |
| 5104 | unsigned int pref_freq, cand_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5105 | unsigned int num, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5106 | |
| 5107 | os_memset(params, 0, sizeof(*params)); |
| 5108 | params->role_go = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5109 | params->ht40 = ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5110 | params->vht = vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5111 | if (freq) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5112 | if (!freq_included(channels, freq)) { |
| 5113 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 5114 | "accepted", freq); |
| 5115 | return -1; |
| 5116 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5117 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " |
| 5118 | "frequency %d MHz", freq); |
| 5119 | params->freq = freq; |
| 5120 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
| 5121 | wpa_s->conf->p2p_oper_channel >= 1 && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5122 | wpa_s->conf->p2p_oper_channel <= 11 && |
| 5123 | freq_included(channels, |
| 5124 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5125 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
| 5126 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 5127 | "frequency %d MHz", params->freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5128 | } else if ((wpa_s->conf->p2p_oper_reg_class == 115 || |
| 5129 | wpa_s->conf->p2p_oper_reg_class == 116 || |
| 5130 | wpa_s->conf->p2p_oper_reg_class == 117 || |
| 5131 | wpa_s->conf->p2p_oper_reg_class == 124 || |
| 5132 | wpa_s->conf->p2p_oper_reg_class == 126 || |
| 5133 | wpa_s->conf->p2p_oper_reg_class == 127) && |
| 5134 | freq_included(channels, |
| 5135 | 5000 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5136 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
| 5137 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 5138 | "frequency %d MHz", params->freq); |
| 5139 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5140 | wpa_s->best_overall_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5141 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5142 | wpa_s->best_overall_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5143 | freq_included(channels, wpa_s->best_overall_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5144 | params->freq = wpa_s->best_overall_freq; |
| 5145 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " |
| 5146 | "channel %d MHz", params->freq); |
| 5147 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5148 | wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5149 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5150 | wpa_s->best_24_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5151 | freq_included(channels, wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5152 | params->freq = wpa_s->best_24_freq; |
| 5153 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " |
| 5154 | "channel %d MHz", params->freq); |
| 5155 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5156 | wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5157 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5158 | wpa_s->best_5_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5159 | freq_included(channels, wpa_s->best_5_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5160 | params->freq = wpa_s->best_5_freq; |
| 5161 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " |
| 5162 | "channel %d MHz", params->freq); |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 5163 | } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p, |
| 5164 | channels))) { |
| 5165 | params->freq = pref_freq; |
| 5166 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " |
| 5167 | "channels", params->freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5168 | } else { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5169 | /* no preference, select some channel */ |
| 5170 | if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5171 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5172 | } |
| 5173 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5174 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 5175 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5176 | if (!freqs) |
| 5177 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5178 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5179 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5180 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5181 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5182 | cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
| 5183 | |
| 5184 | /* First try the best used frequency if possible */ |
| 5185 | if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) { |
| 5186 | params->freq = cand_freq; |
| 5187 | } else if (!freq) { |
| 5188 | /* Try any of the used frequencies */ |
| 5189 | for (i = 0; i < num; i++) { |
| 5190 | if (freq_included(channels, freqs[i].freq)) { |
| 5191 | wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)", |
| 5192 | freqs[i].freq); |
| 5193 | params->freq = freqs[i].freq; |
| 5194 | break; |
| 5195 | } |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5196 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5197 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5198 | if (i == num) { |
| 5199 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5200 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using"); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5201 | os_free(freqs); |
| 5202 | return -1; |
| 5203 | } else { |
| 5204 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels"); |
| 5205 | } |
| 5206 | } |
| 5207 | } else { |
| 5208 | for (i = 0; i < num; i++) { |
| 5209 | if (freqs[i].freq == freq) |
| 5210 | break; |
| 5211 | } |
| 5212 | |
| 5213 | if (i == num) { |
| 5214 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
| 5215 | if (freq) |
| 5216 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq); |
| 5217 | os_free(freqs); |
| 5218 | return -1; |
| 5219 | } else { |
| 5220 | wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels"); |
| 5221 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5222 | } |
| 5223 | } |
| 5224 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5225 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5226 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | |
| 5230 | static struct wpa_supplicant * |
| 5231 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 5232 | int go) |
| 5233 | { |
| 5234 | struct wpa_supplicant *group_wpa_s; |
| 5235 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5236 | if (!wpas_p2p_create_iface(wpa_s)) { |
| 5237 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group " |
| 5238 | "operations"); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5239 | wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5240 | return wpa_s; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5241 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5242 | |
| 5243 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5244 | WPA_IF_P2P_CLIENT) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5245 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5246 | "P2P: Failed to add group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5247 | return NULL; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5248 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5249 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); |
| 5250 | if (group_wpa_s == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5251 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5252 | "P2P: Failed to initialize group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5253 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5254 | return NULL; |
| 5255 | } |
| 5256 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5257 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", |
| 5258 | group_wpa_s->ifname); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5259 | group_wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5260 | return group_wpa_s; |
| 5261 | } |
| 5262 | |
| 5263 | |
| 5264 | /** |
| 5265 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner |
| 5266 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 5267 | * @persistent_group: Whether to create a persistent group |
| 5268 | * @freq: Frequency for the group or 0 to indicate no hardcoding |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5269 | * @ht40: Start GO with 40 MHz channel width |
| 5270 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5271 | * Returns: 0 on success, -1 on failure |
| 5272 | * |
| 5273 | * This function creates a new P2P group with the local end as the Group Owner, |
| 5274 | * i.e., without using Group Owner Negotiation. |
| 5275 | */ |
| 5276 | int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5277 | int freq, int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5278 | { |
| 5279 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5280 | |
| 5281 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5282 | return -1; |
| 5283 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5284 | os_free(wpa_s->global->add_psk); |
| 5285 | wpa_s->global->add_psk = NULL; |
| 5286 | |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5287 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5288 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5289 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5290 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5291 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 5292 | if (freq < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5293 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5294 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5295 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5296 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5297 | if (params.freq && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5298 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5299 | wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " |
| 5300 | "(%u MHz) is not supported for P2P uses", |
| 5301 | params.freq); |
| 5302 | return -1; |
| 5303 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5304 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
| 5305 | params.persistent_group = persistent_group; |
| 5306 | |
| 5307 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); |
| 5308 | if (wpa_s == NULL) |
| 5309 | return -1; |
| 5310 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5311 | |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
| 5315 | |
| 5316 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5317 | struct wpa_ssid *params, int addr_allocated, |
| 5318 | int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5319 | { |
| 5320 | struct wpa_ssid *ssid; |
| 5321 | |
| 5322 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); |
| 5323 | if (wpa_s == NULL) |
| 5324 | return -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5325 | wpa_s->p2p_last_4way_hs_fail = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5326 | |
| 5327 | wpa_supplicant_ap_deinit(wpa_s); |
| 5328 | |
| 5329 | ssid = wpa_config_add_network(wpa_s->conf); |
| 5330 | if (ssid == NULL) |
| 5331 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5332 | wpa_config_set_network_defaults(ssid); |
| 5333 | ssid->temporary = 1; |
| 5334 | ssid->proto = WPA_PROTO_RSN; |
| 5335 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
| 5336 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 5337 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 5338 | ssid->ssid = os_malloc(params->ssid_len); |
| 5339 | if (ssid->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5340 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 5341 | return -1; |
| 5342 | } |
| 5343 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 5344 | ssid->ssid_len = params->ssid_len; |
| 5345 | ssid->p2p_group = 1; |
| 5346 | ssid->export_keys = 1; |
| 5347 | if (params->psk_set) { |
| 5348 | os_memcpy(ssid->psk, params->psk, 32); |
| 5349 | ssid->psk_set = 1; |
| 5350 | } |
| 5351 | if (params->passphrase) |
| 5352 | ssid->passphrase = os_strdup(params->passphrase); |
| 5353 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5354 | wpa_s->show_group_started = 1; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5355 | wpa_s->p2p_in_invitation = 1; |
| 5356 | wpa_s->p2p_invite_go_freq = freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5357 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5358 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5359 | NULL); |
| 5360 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5361 | wpas_p2p_group_formation_timeout, |
| 5362 | wpa_s->parent, NULL); |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 5363 | wpa_supplicant_select_network(wpa_s, ssid); |
| 5364 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
| 5367 | |
| 5368 | |
| 5369 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, |
| 5370 | struct wpa_ssid *ssid, int addr_allocated, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5371 | int force_freq, int neg_freq, int ht40, |
| 5372 | int vht, const struct p2p_channels *channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5373 | int connection_timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5374 | { |
| 5375 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5376 | int go = 0, freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5377 | |
| 5378 | if (ssid->disabled != 2 || ssid->ssid == NULL) |
| 5379 | return -1; |
| 5380 | |
| 5381 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
| 5382 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { |
| 5383 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " |
| 5384 | "already running"); |
| 5385 | return 0; |
| 5386 | } |
| 5387 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5388 | os_free(wpa_s->global->add_psk); |
| 5389 | wpa_s->global->add_psk = NULL; |
| 5390 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5391 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5392 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5393 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5394 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5395 | |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5396 | if (force_freq > 0) { |
| 5397 | freq = wpas_p2p_select_go_freq(wpa_s, force_freq); |
| 5398 | if (freq < 0) |
| 5399 | return -1; |
| 5400 | } else { |
| 5401 | freq = wpas_p2p_select_go_freq(wpa_s, neg_freq); |
| 5402 | if (freq < 0 || (freq > 0 && !freq_included(channels, freq))) |
| 5403 | freq = 0; |
| 5404 | } |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5405 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5406 | if (ssid->mode == WPAS_MODE_INFRA) |
| 5407 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq); |
| 5408 | |
| 5409 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 5410 | return -1; |
| 5411 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5412 | if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5413 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5414 | |
| 5415 | params.role_go = 1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5416 | params.psk_set = ssid->psk_set; |
| 5417 | if (params.psk_set) |
| 5418 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5419 | if (ssid->passphrase) { |
| 5420 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { |
| 5421 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " |
| 5422 | "persistent group"); |
| 5423 | return -1; |
| 5424 | } |
| 5425 | os_strlcpy(params.passphrase, ssid->passphrase, |
| 5426 | sizeof(params.passphrase)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5427 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5428 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); |
| 5429 | params.ssid_len = ssid->ssid_len; |
| 5430 | params.persistent_group = 1; |
| 5431 | |
| 5432 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); |
| 5433 | if (wpa_s == NULL) |
| 5434 | return -1; |
| 5435 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5436 | wpa_s->p2p_first_connection_timeout = connection_timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5437 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5438 | |
| 5439 | return 0; |
| 5440 | } |
| 5441 | |
| 5442 | |
| 5443 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, |
| 5444 | struct wpabuf *proberesp_ies) |
| 5445 | { |
| 5446 | struct wpa_supplicant *wpa_s = ctx; |
| 5447 | if (wpa_s->ap_iface) { |
| 5448 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5449 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
| 5450 | wpabuf_free(beacon_ies); |
| 5451 | wpabuf_free(proberesp_ies); |
| 5452 | return; |
| 5453 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5454 | if (beacon_ies) { |
| 5455 | wpabuf_free(hapd->p2p_beacon_ie); |
| 5456 | hapd->p2p_beacon_ie = beacon_ies; |
| 5457 | } |
| 5458 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 5459 | hapd->p2p_probe_resp_ie = proberesp_ies; |
| 5460 | } else { |
| 5461 | wpabuf_free(beacon_ies); |
| 5462 | wpabuf_free(proberesp_ies); |
| 5463 | } |
| 5464 | wpa_supplicant_ap_update_beacon(wpa_s); |
| 5465 | } |
| 5466 | |
| 5467 | |
| 5468 | static void wpas_p2p_idle_update(void *ctx, int idle) |
| 5469 | { |
| 5470 | struct wpa_supplicant *wpa_s = ctx; |
| 5471 | if (!wpa_s->ap_iface) |
| 5472 | return; |
| 5473 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5474 | if (idle) { |
| 5475 | if (wpa_s->global->p2p_fail_on_wps_complete && |
| 5476 | wpa_s->p2p_in_provisioning) { |
| 5477 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5478 | return; |
| 5479 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5480 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5481 | } else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5482 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 5483 | } |
| 5484 | |
| 5485 | |
| 5486 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5487 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5488 | { |
| 5489 | struct p2p_group *group; |
| 5490 | struct p2p_group_config *cfg; |
| 5491 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5492 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5493 | return NULL; |
| 5494 | |
| 5495 | cfg = os_zalloc(sizeof(*cfg)); |
| 5496 | if (cfg == NULL) |
| 5497 | return NULL; |
| 5498 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5499 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5500 | cfg->persistent_group = 2; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5501 | else if (ssid->p2p_persistent_group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5502 | cfg->persistent_group = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5503 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
| 5504 | if (wpa_s->max_stations && |
| 5505 | wpa_s->max_stations < wpa_s->conf->max_num_sta) |
| 5506 | cfg->max_clients = wpa_s->max_stations; |
| 5507 | else |
| 5508 | cfg->max_clients = wpa_s->conf->max_num_sta; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5509 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); |
| 5510 | cfg->ssid_len = ssid->ssid_len; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5511 | cfg->freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5512 | cfg->cb_ctx = wpa_s; |
| 5513 | cfg->ie_update = wpas_p2p_ie_update; |
| 5514 | cfg->idle_update = wpas_p2p_idle_update; |
| 5515 | |
| 5516 | group = p2p_group_init(wpa_s->global->p2p, cfg); |
| 5517 | if (group == NULL) |
| 5518 | os_free(cfg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5519 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5520 | p2p_group_notif_formation_done(group); |
| 5521 | wpa_s->p2p_group = group; |
| 5522 | return group; |
| 5523 | } |
| 5524 | |
| 5525 | |
| 5526 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 5527 | int registrar) |
| 5528 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5529 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5530 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5531 | if (!wpa_s->p2p_in_provisioning) { |
| 5532 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " |
| 5533 | "provisioning not in progress"); |
| 5534 | return; |
| 5535 | } |
| 5536 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5537 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5538 | u8 go_dev_addr[ETH_ALEN]; |
| 5539 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); |
| 5540 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5541 | ssid->ssid_len); |
| 5542 | /* Clear any stored provisioning info */ |
| 5543 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); |
| 5544 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5545 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5546 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5547 | NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5548 | wpa_s->p2p_go_group_formation_completed = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5549 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5550 | /* |
| 5551 | * Use a separate timeout for initial data connection to |
| 5552 | * complete to allow the group to be removed automatically if |
| 5553 | * something goes wrong in this step before the P2P group idle |
| 5554 | * timeout mechanism is taken into use. |
| 5555 | */ |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5556 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5557 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", |
| 5558 | P2P_MAX_INITIAL_CONN_WAIT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5559 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5560 | wpas_p2p_group_formation_timeout, |
| 5561 | wpa_s->parent, NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5562 | } else if (ssid) { |
| 5563 | /* |
| 5564 | * Use a separate timeout for initial data connection to |
| 5565 | * complete to allow the group to be removed automatically if |
| 5566 | * the client does not complete data connection successfully. |
| 5567 | */ |
| 5568 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5569 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", |
| 5570 | P2P_MAX_INITIAL_CONN_WAIT_GO); |
| 5571 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, |
| 5572 | wpas_p2p_group_formation_timeout, |
| 5573 | wpa_s->parent, NULL); |
| 5574 | /* |
| 5575 | * Complete group formation on first successful data connection |
| 5576 | */ |
| 5577 | wpa_s->p2p_go_group_formation_completed = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5578 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5579 | if (wpa_s->global->p2p) |
| 5580 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5581 | wpas_group_formation_completed(wpa_s, 1); |
| 5582 | } |
| 5583 | |
| 5584 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5585 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
| 5586 | struct wps_event_fail *fail) |
| 5587 | { |
| 5588 | if (!wpa_s->p2p_in_provisioning) { |
| 5589 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " |
| 5590 | "provisioning not in progress"); |
| 5591 | return; |
| 5592 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5593 | |
| 5594 | if (wpa_s->go_params) { |
| 5595 | p2p_clear_provisioning_info( |
| 5596 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5597 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5598 | } |
| 5599 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5600 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5601 | |
| 5602 | if (wpa_s == wpa_s->global->p2p_group_formation) { |
| 5603 | /* |
| 5604 | * Allow some time for the failed WPS negotiation exchange to |
| 5605 | * complete, but remove the group since group formation cannot |
| 5606 | * succeed after provisioning failure. |
| 5607 | */ |
| 5608 | wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); |
| 5609 | wpa_s->global->p2p_fail_on_wps_complete = 1; |
| 5610 | eloop_deplete_timeout(0, 50000, |
| 5611 | wpas_p2p_group_formation_timeout, |
| 5612 | wpa_s->parent, NULL); |
| 5613 | } |
| 5614 | } |
| 5615 | |
| 5616 | |
| 5617 | int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) |
| 5618 | { |
| 5619 | if (!wpa_s->global->p2p_fail_on_wps_complete || |
| 5620 | !wpa_s->p2p_in_provisioning) |
| 5621 | return 0; |
| 5622 | |
| 5623 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5624 | |
| 5625 | return 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5629 | int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5630 | const char *config_method, |
| 5631 | enum wpas_p2p_prov_disc_use use) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5632 | { |
| 5633 | u16 config_methods; |
| 5634 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5635 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5636 | wpa_s->pending_pd_use = NORMAL_PD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5637 | if (os_strncmp(config_method, "display", 7) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5638 | config_methods = WPS_CONFIG_DISPLAY; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5639 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5640 | config_methods = WPS_CONFIG_KEYPAD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5641 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
| 5642 | os_strncmp(config_method, "pushbutton", 10) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5643 | config_methods = WPS_CONFIG_PUSHBUTTON; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5644 | else { |
| 5645 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5646 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5647 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5648 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5649 | if (use == WPAS_P2P_PD_AUTO) { |
| 5650 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); |
| 5651 | wpa_s->pending_pd_config_methods = config_methods; |
| 5652 | wpa_s->p2p_auto_pd = 1; |
| 5653 | wpa_s->p2p_auto_join = 0; |
| 5654 | wpa_s->pending_pd_before_join = 0; |
| 5655 | wpa_s->auto_pd_scan_retry = 0; |
| 5656 | wpas_p2p_stop_find(wpa_s); |
| 5657 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5658 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5659 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", |
| 5660 | wpa_s->p2p_auto_started.sec, |
| 5661 | wpa_s->p2p_auto_started.usec); |
| 5662 | wpas_p2p_join_scan(wpa_s, NULL); |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5666 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 5667 | return -1; |
| 5668 | |
| 5669 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5670 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5671 | 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | |
| 5675 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, |
| 5676 | char *end) |
| 5677 | { |
| 5678 | return p2p_scan_result_text(ies, ies_len, buf, end); |
| 5679 | } |
| 5680 | |
| 5681 | |
| 5682 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
| 5683 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5684 | if (!offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5685 | return; |
| 5686 | |
| 5687 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " |
| 5688 | "operation request"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5689 | offchannel_clear_pending_action_tx(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | |
| 5693 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
| 5694 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5695 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5696 | const u8 *dev_id, unsigned int search_delay) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5697 | { |
| 5698 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5699 | wpa_s->p2p_long_listen = 0; |
| 5700 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5701 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || |
| 5702 | wpa_s->p2p_in_provisioning) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5703 | return -1; |
| 5704 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5705 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 5706 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5707 | return p2p_find(wpa_s->global->p2p, timeout, type, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5708 | num_req_dev_types, req_dev_types, dev_id, |
| 5709 | search_delay); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5713 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5714 | { |
| 5715 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5716 | wpa_s->p2p_long_listen = 0; |
| 5717 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5718 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5719 | |
| 5720 | if (wpa_s->global->p2p) |
| 5721 | p2p_stop_find(wpa_s->global->p2p); |
| 5722 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5723 | return 0; |
| 5724 | } |
| 5725 | |
| 5726 | |
| 5727 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) |
| 5728 | { |
| 5729 | if (wpas_p2p_stop_find_oper(wpa_s) > 0) |
| 5730 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5731 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5732 | } |
| 5733 | |
| 5734 | |
| 5735 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5736 | { |
| 5737 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5738 | wpa_s->p2p_long_listen = 0; |
| 5739 | } |
| 5740 | |
| 5741 | |
| 5742 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) |
| 5743 | { |
| 5744 | int res; |
| 5745 | |
| 5746 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5747 | return -1; |
| 5748 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5749 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5750 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5751 | |
| 5752 | if (timeout == 0) { |
| 5753 | /* |
| 5754 | * This is a request for unlimited Listen state. However, at |
| 5755 | * least for now, this is mapped to a Listen state for one |
| 5756 | * hour. |
| 5757 | */ |
| 5758 | timeout = 3600; |
| 5759 | } |
| 5760 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5761 | wpa_s->p2p_long_listen = 0; |
| 5762 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5763 | /* |
| 5764 | * Stop previous find/listen operation to avoid trying to request a new |
| 5765 | * remain-on-channel operation while the driver is still running the |
| 5766 | * previous one. |
| 5767 | */ |
| 5768 | if (wpa_s->global->p2p) |
| 5769 | p2p_stop_find(wpa_s->global->p2p); |
| 5770 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5771 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
| 5772 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { |
| 5773 | wpa_s->p2p_long_listen = timeout * 1000; |
| 5774 | eloop_register_timeout(timeout, 0, |
| 5775 | wpas_p2p_long_listen_timeout, |
| 5776 | wpa_s, NULL); |
| 5777 | } |
| 5778 | |
| 5779 | return res; |
| 5780 | } |
| 5781 | |
| 5782 | |
| 5783 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 5784 | u8 *buf, size_t len, int p2p_group) |
| 5785 | { |
| 5786 | struct wpabuf *p2p_ie; |
| 5787 | int ret; |
| 5788 | |
| 5789 | if (wpa_s->global->p2p_disabled) |
| 5790 | return -1; |
| 5791 | if (wpa_s->global->p2p == NULL) |
| 5792 | return -1; |
| 5793 | if (bss == NULL) |
| 5794 | return -1; |
| 5795 | |
| 5796 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
| 5797 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, |
| 5798 | p2p_group, p2p_ie); |
| 5799 | wpabuf_free(p2p_ie); |
| 5800 | |
| 5801 | return ret; |
| 5802 | } |
| 5803 | |
| 5804 | |
| 5805 | int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5806 | const u8 *dst, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5807 | const u8 *ie, size_t ie_len, int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5808 | { |
| 5809 | if (wpa_s->global->p2p_disabled) |
| 5810 | return 0; |
| 5811 | if (wpa_s->global->p2p == NULL) |
| 5812 | return 0; |
| 5813 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5814 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
| 5815 | ie, ie_len)) { |
| 5816 | case P2P_PREQ_NOT_P2P: |
| 5817 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, |
| 5818 | ssi_signal); |
| 5819 | /* fall through */ |
| 5820 | case P2P_PREQ_MALFORMED: |
| 5821 | case P2P_PREQ_NOT_LISTEN: |
| 5822 | case P2P_PREQ_NOT_PROCESSED: |
| 5823 | default: /* make gcc happy */ |
| 5824 | return 0; |
| 5825 | case P2P_PREQ_PROCESSED: |
| 5826 | return 1; |
| 5827 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | |
| 5831 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, |
| 5832 | const u8 *sa, const u8 *bssid, |
| 5833 | u8 category, const u8 *data, size_t len, int freq) |
| 5834 | { |
| 5835 | if (wpa_s->global->p2p_disabled) |
| 5836 | return; |
| 5837 | if (wpa_s->global->p2p == NULL) |
| 5838 | return; |
| 5839 | |
| 5840 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, |
| 5841 | freq); |
| 5842 | } |
| 5843 | |
| 5844 | |
| 5845 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) |
| 5846 | { |
| 5847 | if (wpa_s->global->p2p_disabled) |
| 5848 | return; |
| 5849 | if (wpa_s->global->p2p == NULL) |
| 5850 | return; |
| 5851 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5852 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5853 | } |
| 5854 | |
| 5855 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5856 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5857 | { |
| 5858 | p2p_group_deinit(wpa_s->p2p_group); |
| 5859 | wpa_s->p2p_group = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5860 | |
| 5861 | wpa_s->ap_configured_cb = NULL; |
| 5862 | wpa_s->ap_configured_cb_ctx = NULL; |
| 5863 | wpa_s->ap_configured_cb_data = NULL; |
| 5864 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | |
| 5868 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) |
| 5869 | { |
| 5870 | wpa_s->p2p_long_listen = 0; |
| 5871 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5872 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5873 | return -1; |
| 5874 | |
| 5875 | return p2p_reject(wpa_s->global->p2p, addr); |
| 5876 | } |
| 5877 | |
| 5878 | |
| 5879 | /* Invite to reinvoke a persistent group */ |
| 5880 | int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5881 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5882 | int ht40, int vht, int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5883 | { |
| 5884 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5885 | u8 *bssid = NULL; |
| 5886 | int force_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5887 | int res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5888 | int no_pref_freq_given = pref_freq == 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5889 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5890 | wpa_s->global->p2p_invite_group = NULL; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5891 | if (peer_addr) |
| 5892 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 5893 | else |
| 5894 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5895 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5896 | wpa_s->p2p_persistent_go_freq = freq; |
| 5897 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5898 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5899 | role = P2P_INVITE_ROLE_GO; |
| 5900 | if (peer_addr == NULL) { |
| 5901 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " |
| 5902 | "address in invitation command"); |
| 5903 | return -1; |
| 5904 | } |
| 5905 | if (wpas_p2p_create_iface(wpa_s)) { |
| 5906 | if (wpas_p2p_add_group_interface(wpa_s, |
| 5907 | WPA_IF_P2P_GO) < 0) { |
| 5908 | wpa_printf(MSG_ERROR, "P2P: Failed to " |
| 5909 | "allocate a new interface for the " |
| 5910 | "group"); |
| 5911 | return -1; |
| 5912 | } |
| 5913 | bssid = wpa_s->pending_interface_addr; |
| 5914 | } else |
| 5915 | bssid = wpa_s->own_addr; |
| 5916 | } else { |
| 5917 | role = P2P_INVITE_ROLE_CLIENT; |
| 5918 | peer_addr = ssid->bssid; |
| 5919 | } |
| 5920 | wpa_s->pending_invite_ssid_id = ssid->id; |
| 5921 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5922 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5923 | role == P2P_INVITE_ROLE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5924 | if (res) |
| 5925 | return res; |
Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 5926 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5927 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5928 | return -1; |
| 5929 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5930 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 5931 | no_pref_freq_given && pref_freq > 0 && |
| 5932 | wpa_s->num_multichan_concurrent > 1 && |
| 5933 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 5934 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", |
| 5935 | pref_freq); |
| 5936 | pref_freq = 0; |
| 5937 | } |
| 5938 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5939 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5940 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5941 | 1, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
| 5944 | |
| 5945 | /* Invite to join an active group */ |
| 5946 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, |
| 5947 | const u8 *peer_addr, const u8 *go_dev_addr) |
| 5948 | { |
| 5949 | struct wpa_global *global = wpa_s->global; |
| 5950 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5951 | u8 *bssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5952 | struct wpa_ssid *ssid; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5953 | int persistent; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5954 | int freq = 0, force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5955 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5956 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5957 | wpa_s->p2p_persistent_go_freq = 0; |
| 5958 | wpa_s->p2p_go_ht40 = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5959 | wpa_s->p2p_go_vht = 0; |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5960 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5961 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5962 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5963 | break; |
| 5964 | } |
| 5965 | if (wpa_s == NULL) { |
| 5966 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); |
| 5967 | return -1; |
| 5968 | } |
| 5969 | |
| 5970 | ssid = wpa_s->current_ssid; |
| 5971 | if (ssid == NULL) { |
| 5972 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " |
| 5973 | "invitation"); |
| 5974 | return -1; |
| 5975 | } |
| 5976 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5977 | wpa_s->global->p2p_invite_group = wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5978 | persistent = ssid->p2p_persistent_group && |
| 5979 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, |
| 5980 | ssid->ssid, ssid->ssid_len); |
| 5981 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5982 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5983 | role = P2P_INVITE_ROLE_ACTIVE_GO; |
| 5984 | bssid = wpa_s->own_addr; |
| 5985 | if (go_dev_addr == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5986 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5987 | freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5988 | } else { |
| 5989 | role = P2P_INVITE_ROLE_CLIENT; |
| 5990 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { |
| 5991 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " |
| 5992 | "invite to current group"); |
| 5993 | return -1; |
| 5994 | } |
| 5995 | bssid = wpa_s->bssid; |
| 5996 | if (go_dev_addr == NULL && |
| 5997 | !is_zero_ether_addr(wpa_s->go_dev_addr)) |
| 5998 | go_dev_addr = wpa_s->go_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5999 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 6000 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6001 | } |
| 6002 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 6003 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6004 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6005 | return -1; |
| 6006 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6007 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 6008 | role == P2P_INVITE_ROLE_ACTIVE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6009 | if (res) |
| 6010 | return res; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 6011 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6012 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6013 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6014 | ssid->ssid, ssid->ssid_len, force_freq, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6015 | go_dev_addr, persistent, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | |
| 6019 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) |
| 6020 | { |
| 6021 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6022 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6023 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6024 | int persistent; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6025 | int freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6026 | u8 ip[3 * 4]; |
| 6027 | char ip_addr[100]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6028 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6029 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6030 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6031 | wpa_s->parent, NULL); |
| 6032 | } |
| 6033 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6034 | if (!wpa_s->show_group_started || !ssid) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6035 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6036 | |
| 6037 | wpa_s->show_group_started = 0; |
| 6038 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6039 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 6040 | if (ssid->bssid_set) |
| 6041 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); |
| 6042 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 6043 | ssid->ssid_len); |
| 6044 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); |
| 6045 | |
| 6046 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 6047 | wpa_s->global->p2p_group_formation = NULL; |
| 6048 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6049 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 6050 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6051 | |
| 6052 | ip_addr[0] = '\0'; |
| 6053 | if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) { |
| 6054 | os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u " |
| 6055 | "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u", |
| 6056 | ip[0], ip[1], ip[2], ip[3], |
| 6057 | ip[4], ip[5], ip[6], ip[7], |
| 6058 | ip[8], ip[9], ip[10], ip[11]); |
| 6059 | } |
| 6060 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame^] | 6061 | wpas_p2p_group_started(wpa_s, 0, ssid, freq, |
| 6062 | ssid->passphrase == NULL && ssid->psk_set ? |
| 6063 | ssid->psk : NULL, |
| 6064 | ssid->passphrase, go_dev_addr, persistent, |
| 6065 | ip_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6066 | |
| 6067 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6068 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 6069 | ssid, go_dev_addr); |
| 6070 | if (network_id < 0) |
| 6071 | network_id = ssid->id; |
| 6072 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6073 | } |
| 6074 | |
| 6075 | |
| 6076 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, |
| 6077 | u32 interval1, u32 duration2, u32 interval2) |
| 6078 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6079 | int ret; |
| 6080 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6081 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6082 | return -1; |
| 6083 | |
| 6084 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
| 6085 | wpa_s->current_ssid == NULL || |
| 6086 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) |
| 6087 | return -1; |
| 6088 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6089 | ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, |
| 6090 | wpa_s->own_addr, wpa_s->assoc_freq, |
| 6091 | duration1, interval1, duration2, interval2); |
| 6092 | if (ret == 0) |
| 6093 | wpa_s->waiting_presence_resp = 1; |
| 6094 | |
| 6095 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | |
| 6099 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, |
| 6100 | unsigned int interval) |
| 6101 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6102 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6103 | return -1; |
| 6104 | |
| 6105 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
| 6106 | } |
| 6107 | |
| 6108 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6109 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
| 6110 | { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6111 | if (wpa_s->current_ssid == NULL) { |
| 6112 | /* |
| 6113 | * current_ssid can be cleared when P2P client interface gets |
| 6114 | * disconnected, so assume this interface was used as P2P |
| 6115 | * client. |
| 6116 | */ |
| 6117 | return 1; |
| 6118 | } |
| 6119 | return wpa_s->current_ssid->p2p_group && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6120 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; |
| 6121 | } |
| 6122 | |
| 6123 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6124 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
| 6125 | { |
| 6126 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6127 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6128 | if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6129 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
| 6130 | "disabled"); |
| 6131 | return; |
| 6132 | } |
| 6133 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6134 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " |
| 6135 | "group"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6136 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | |
| 6140 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) |
| 6141 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6142 | int timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6143 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6144 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6145 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
| 6146 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6147 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6148 | return; |
| 6149 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6150 | timeout = wpa_s->conf->p2p_group_idle; |
| 6151 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && |
| 6152 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) |
| 6153 | timeout = P2P_MAX_CLIENT_IDLE; |
| 6154 | |
| 6155 | if (timeout == 0) |
| 6156 | return; |
| 6157 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6158 | if (timeout < 0) { |
| 6159 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) |
| 6160 | timeout = 0; /* special client mode no-timeout */ |
| 6161 | else |
| 6162 | return; |
| 6163 | } |
| 6164 | |
| 6165 | if (wpa_s->p2p_in_provisioning) { |
| 6166 | /* |
| 6167 | * Use the normal group formation timeout during the |
| 6168 | * provisioning phase to avoid terminating this process too |
| 6169 | * early due to group idle timeout. |
| 6170 | */ |
| 6171 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6172 | "during provisioning"); |
| 6173 | return; |
| 6174 | } |
Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 6175 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6176 | if (wpa_s->show_group_started) { |
| 6177 | /* |
| 6178 | * Use the normal group formation timeout between the end of |
| 6179 | * the provisioning phase and completion of 4-way handshake to |
| 6180 | * avoid terminating this process too early due to group idle |
| 6181 | * timeout. |
| 6182 | */ |
| 6183 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6184 | "while waiting for initial 4-way handshake to " |
| 6185 | "complete"); |
| 6186 | return; |
| 6187 | } |
| 6188 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6189 | wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds", |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6190 | timeout); |
| 6191 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, |
| 6192 | wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6196 | /* Returns 1 if the interface was removed */ |
| 6197 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
| 6198 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6199 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6200 | { |
| 6201 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6202 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6203 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6204 | if (!locally_generated) |
| 6205 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6206 | ie_len); |
| 6207 | |
| 6208 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && |
| 6209 | wpa_s->current_ssid && |
| 6210 | wpa_s->current_ssid->p2p_group && |
| 6211 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { |
| 6212 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " |
| 6213 | "session is ending"); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6214 | if (wpas_p2p_group_delete(wpa_s, |
| 6215 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) |
| 6216 | > 0) |
| 6217 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6218 | } |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6219 | |
| 6220 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | |
| 6224 | void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6225 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6226 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6227 | { |
| 6228 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6229 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6230 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6231 | if (!locally_generated) |
| 6232 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6233 | ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | |
| 6237 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) |
| 6238 | { |
| 6239 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6240 | |
| 6241 | if (p2p == NULL) |
| 6242 | return; |
| 6243 | |
| 6244 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 6245 | return; |
| 6246 | |
| 6247 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
| 6248 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); |
| 6249 | |
| 6250 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
| 6251 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); |
| 6252 | |
| 6253 | if (wpa_s->wps && |
| 6254 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) |
| 6255 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); |
| 6256 | |
| 6257 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) |
| 6258 | p2p_set_uuid(p2p, wpa_s->wps->uuid); |
| 6259 | |
| 6260 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { |
| 6261 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); |
| 6262 | p2p_set_model_name(p2p, wpa_s->conf->model_name); |
| 6263 | p2p_set_model_number(p2p, wpa_s->conf->model_number); |
| 6264 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); |
| 6265 | } |
| 6266 | |
| 6267 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
| 6268 | p2p_set_sec_dev_types(p2p, |
| 6269 | (void *) wpa_s->conf->sec_device_type, |
| 6270 | wpa_s->conf->num_sec_device_types); |
| 6271 | |
| 6272 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
| 6273 | int i; |
| 6274 | p2p_remove_wps_vendor_extensions(p2p); |
| 6275 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 6276 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 6277 | continue; |
| 6278 | p2p_add_wps_vendor_extension( |
| 6279 | p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 6280 | } |
| 6281 | } |
| 6282 | |
| 6283 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
| 6284 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 6285 | char country[3]; |
| 6286 | country[0] = wpa_s->conf->country[0]; |
| 6287 | country[1] = wpa_s->conf->country[1]; |
| 6288 | country[2] = 0x04; |
| 6289 | p2p_set_country(p2p, country); |
| 6290 | } |
| 6291 | |
| 6292 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { |
| 6293 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, |
| 6294 | wpa_s->conf->p2p_ssid_postfix ? |
| 6295 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : |
| 6296 | 0); |
| 6297 | } |
| 6298 | |
| 6299 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) |
| 6300 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6301 | |
| 6302 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { |
| 6303 | u8 reg_class, channel; |
| 6304 | int ret; |
| 6305 | unsigned int r; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6306 | u8 channel_forced; |
| 6307 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6308 | if (wpa_s->conf->p2p_listen_reg_class && |
| 6309 | wpa_s->conf->p2p_listen_channel) { |
| 6310 | reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 6311 | channel = wpa_s->conf->p2p_listen_channel; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6312 | channel_forced = 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6313 | } else { |
| 6314 | reg_class = 81; |
| 6315 | /* |
| 6316 | * Pick one of the social channels randomly as the |
| 6317 | * listen channel. |
| 6318 | */ |
| 6319 | os_get_random((u8 *) &r, sizeof(r)); |
| 6320 | channel = 1 + (r % 3) * 5; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6321 | channel_forced = 0; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6322 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6323 | ret = p2p_set_listen_channel(p2p, reg_class, channel, |
| 6324 | channel_forced); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6325 | if (ret) |
| 6326 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " |
| 6327 | "failed: %d", ret); |
| 6328 | } |
| 6329 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { |
| 6330 | u8 op_reg_class, op_channel, cfg_op_channel; |
| 6331 | int ret = 0; |
| 6332 | unsigned int r; |
| 6333 | if (wpa_s->conf->p2p_oper_reg_class && |
| 6334 | wpa_s->conf->p2p_oper_channel) { |
| 6335 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 6336 | op_channel = wpa_s->conf->p2p_oper_channel; |
| 6337 | cfg_op_channel = 1; |
| 6338 | } else { |
| 6339 | op_reg_class = 81; |
| 6340 | /* |
| 6341 | * Use random operation channel from (1, 6, 11) |
| 6342 | *if no other preference is indicated. |
| 6343 | */ |
| 6344 | os_get_random((u8 *) &r, sizeof(r)); |
| 6345 | op_channel = 1 + (r % 3) * 5; |
| 6346 | cfg_op_channel = 0; |
| 6347 | } |
| 6348 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, |
| 6349 | cfg_op_channel); |
| 6350 | if (ret) |
| 6351 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " |
| 6352 | "failed: %d", ret); |
| 6353 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6354 | |
| 6355 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { |
| 6356 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, |
| 6357 | wpa_s->conf->p2p_pref_chan) < 0) { |
| 6358 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " |
| 6359 | "update failed"); |
| 6360 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6361 | |
| 6362 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { |
| 6363 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " |
| 6364 | "update failed"); |
| 6365 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6366 | } |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 6367 | |
| 6368 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN) |
| 6369 | p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6370 | } |
| 6371 | |
| 6372 | |
| 6373 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, |
| 6374 | int duration) |
| 6375 | { |
| 6376 | if (!wpa_s->ap_iface) |
| 6377 | return -1; |
| 6378 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, |
| 6379 | duration); |
| 6380 | } |
| 6381 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6382 | |
| 6383 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) |
| 6384 | { |
| 6385 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6386 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6387 | |
| 6388 | wpa_s->global->cross_connection = enabled; |
| 6389 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); |
| 6390 | |
| 6391 | if (!enabled) { |
| 6392 | struct wpa_supplicant *iface; |
| 6393 | |
| 6394 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 6395 | { |
| 6396 | if (iface->cross_connect_enabled == 0) |
| 6397 | continue; |
| 6398 | |
| 6399 | iface->cross_connect_enabled = 0; |
| 6400 | iface->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6401 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6402 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6403 | iface->ifname, |
| 6404 | iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | return 0; |
| 6409 | } |
| 6410 | |
| 6411 | |
| 6412 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) |
| 6413 | { |
| 6414 | struct wpa_supplicant *iface; |
| 6415 | |
| 6416 | if (!uplink->global->cross_connection) |
| 6417 | return; |
| 6418 | |
| 6419 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6420 | if (!iface->cross_connect_enabled) |
| 6421 | continue; |
| 6422 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6423 | 0) |
| 6424 | continue; |
| 6425 | if (iface->ap_iface == NULL) |
| 6426 | continue; |
| 6427 | if (iface->cross_connect_in_use) |
| 6428 | continue; |
| 6429 | |
| 6430 | iface->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6431 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6432 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6433 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | |
| 6438 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) |
| 6439 | { |
| 6440 | struct wpa_supplicant *iface; |
| 6441 | |
| 6442 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6443 | if (!iface->cross_connect_enabled) |
| 6444 | continue; |
| 6445 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6446 | 0) |
| 6447 | continue; |
| 6448 | if (!iface->cross_connect_in_use) |
| 6449 | continue; |
| 6450 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6451 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6452 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6453 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6454 | iface->cross_connect_in_use = 0; |
| 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | |
| 6459 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) |
| 6460 | { |
| 6461 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || |
| 6462 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || |
| 6463 | wpa_s->cross_connect_disallowed) |
| 6464 | wpas_p2p_disable_cross_connect(wpa_s); |
| 6465 | else |
| 6466 | wpas_p2p_enable_cross_connect(wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6467 | if (!wpa_s->ap_iface && |
| 6468 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6469 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | |
| 6473 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) |
| 6474 | { |
| 6475 | wpas_p2p_disable_cross_connect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6476 | if (!wpa_s->ap_iface && |
| 6477 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, |
| 6478 | wpa_s, NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6479 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 6480 | } |
| 6481 | |
| 6482 | |
| 6483 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) |
| 6484 | { |
| 6485 | struct wpa_supplicant *iface; |
| 6486 | |
| 6487 | if (!wpa_s->global->cross_connection) |
| 6488 | return; |
| 6489 | |
| 6490 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6491 | if (iface == wpa_s) |
| 6492 | continue; |
| 6493 | if (iface->drv_flags & |
| 6494 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) |
| 6495 | continue; |
| 6496 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) |
| 6497 | continue; |
| 6498 | |
| 6499 | wpa_s->cross_connect_enabled = 1; |
| 6500 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, |
| 6501 | sizeof(wpa_s->cross_connect_uplink)); |
| 6502 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " |
| 6503 | "%s to %s whenever uplink is available", |
| 6504 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
| 6505 | |
| 6506 | if (iface->ap_iface || iface->current_ssid == NULL || |
| 6507 | iface->current_ssid->mode != WPAS_MODE_INFRA || |
| 6508 | iface->cross_connect_disallowed || |
| 6509 | iface->wpa_state != WPA_COMPLETED) |
| 6510 | break; |
| 6511 | |
| 6512 | wpa_s->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6513 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6514 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6515 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6516 | break; |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | |
| 6521 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) |
| 6522 | { |
| 6523 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && |
| 6524 | !wpa_s->p2p_in_provisioning) |
| 6525 | return 0; /* not P2P client operation */ |
| 6526 | |
| 6527 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " |
| 6528 | "session overlap"); |
| 6529 | if (wpa_s != wpa_s->parent) |
| 6530 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 6531 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6532 | return 1; |
| 6533 | } |
| 6534 | |
| 6535 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 6536 | void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx) |
| 6537 | { |
| 6538 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6539 | wpas_p2p_notif_pbc_overlap(wpa_s); |
| 6540 | } |
| 6541 | |
| 6542 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6543 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) |
| 6544 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6545 | struct p2p_channels chan, cli_chan; |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 6546 | struct wpa_supplicant *ifs; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6547 | |
| 6548 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) |
| 6549 | return; |
| 6550 | |
| 6551 | os_memset(&chan, 0, sizeof(chan)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6552 | os_memset(&cli_chan, 0, sizeof(cli_chan)); |
| 6553 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6554 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " |
| 6555 | "channel list"); |
| 6556 | return; |
| 6557 | } |
| 6558 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6559 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 6560 | |
| 6561 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 6562 | int freq; |
| 6563 | if (!ifs->current_ssid || |
| 6564 | !ifs->current_ssid->p2p_group || |
| 6565 | (ifs->current_ssid->mode != WPAS_MODE_P2P_GO && |
| 6566 | ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)) |
| 6567 | continue; |
| 6568 | freq = ifs->current_ssid->frequency; |
| 6569 | if (freq_included(&chan, freq)) { |
| 6570 | wpa_dbg(ifs, MSG_DEBUG, |
| 6571 | "P2P GO operating frequency %d MHz in valid range", |
| 6572 | freq); |
| 6573 | continue; |
| 6574 | } |
| 6575 | |
| 6576 | wpa_dbg(ifs, MSG_DEBUG, |
| 6577 | "P2P GO operating in invalid frequency %d MHz", freq); |
| 6578 | /* TODO: Consider using CSA or removing the group within |
| 6579 | * wpa_supplicant */ |
| 6580 | wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP); |
| 6581 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6585 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, |
| 6586 | struct wpa_scan_results *scan_res) |
| 6587 | { |
| 6588 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 6589 | } |
| 6590 | |
| 6591 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6592 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) |
| 6593 | { |
| 6594 | struct wpa_global *global = wpa_s->global; |
| 6595 | int found = 0; |
| 6596 | const u8 *peer; |
| 6597 | |
| 6598 | if (global->p2p == NULL) |
| 6599 | return -1; |
| 6600 | |
| 6601 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
| 6602 | |
| 6603 | if (wpa_s->pending_interface_name[0] && |
| 6604 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 6605 | found = 1; |
| 6606 | |
| 6607 | peer = p2p_get_go_neg_peer(global->p2p); |
| 6608 | if (peer) { |
| 6609 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " |
| 6610 | MACSTR, MAC2STR(peer)); |
| 6611 | p2p_unauthorize(global->p2p, peer); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6612 | found = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6613 | } |
| 6614 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6615 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { |
| 6616 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); |
| 6617 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; |
| 6618 | found = 1; |
| 6619 | } |
| 6620 | |
| 6621 | if (wpa_s->pending_pd_before_join) { |
| 6622 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); |
| 6623 | wpa_s->pending_pd_before_join = 0; |
| 6624 | found = 1; |
| 6625 | } |
| 6626 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6627 | wpas_p2p_stop_find(wpa_s); |
| 6628 | |
| 6629 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6630 | if (wpa_s == global->p2p_group_formation && |
| 6631 | (wpa_s->p2p_in_provisioning || |
| 6632 | wpa_s->parent->pending_interface_type == |
| 6633 | WPA_IF_P2P_CLIENT)) { |
| 6634 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
| 6635 | "formation found - cancelling", |
| 6636 | wpa_s->ifname); |
| 6637 | found = 1; |
| 6638 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6639 | wpa_s->parent, NULL); |
Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 6640 | if (wpa_s->p2p_in_provisioning) { |
| 6641 | wpas_group_formation_completed(wpa_s, 0); |
| 6642 | break; |
| 6643 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6644 | wpas_p2p_group_delete(wpa_s, |
| 6645 | P2P_GROUP_REMOVAL_REQUESTED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6646 | break; |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 6647 | } else if (wpa_s->p2p_in_invitation) { |
| 6648 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling", |
| 6649 | wpa_s->ifname); |
| 6650 | found = 1; |
| 6651 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6652 | } |
| 6653 | } |
| 6654 | |
| 6655 | if (!found) { |
| 6656 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); |
| 6657 | return -1; |
| 6658 | } |
| 6659 | |
| 6660 | return 0; |
| 6661 | } |
| 6662 | |
| 6663 | |
| 6664 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) |
| 6665 | { |
| 6666 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6667 | return; |
| 6668 | |
| 6669 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " |
| 6670 | "being available anymore"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6671 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6672 | } |
| 6673 | |
| 6674 | |
| 6675 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, |
| 6676 | int freq_24, int freq_5, int freq_overall) |
| 6677 | { |
| 6678 | struct p2p_data *p2p = wpa_s->global->p2p; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6679 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6680 | return; |
| 6681 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); |
| 6682 | } |
| 6683 | |
| 6684 | |
| 6685 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) |
| 6686 | { |
| 6687 | u8 peer[ETH_ALEN]; |
| 6688 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6689 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6690 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6691 | return -1; |
| 6692 | |
| 6693 | if (hwaddr_aton(addr, peer)) |
| 6694 | return -1; |
| 6695 | |
| 6696 | return p2p_unauthorize(p2p, peer); |
| 6697 | } |
| 6698 | |
| 6699 | |
| 6700 | /** |
| 6701 | * wpas_p2p_disconnect - Disconnect from a P2P Group |
| 6702 | * @wpa_s: Pointer to wpa_supplicant data |
| 6703 | * Returns: 0 on success, -1 on failure |
| 6704 | * |
| 6705 | * This can be used to disconnect from a group in which the local end is a P2P |
| 6706 | * Client or to end a P2P Group in case the local end is the Group Owner. If a |
| 6707 | * virtual network interface was created for this group, that interface will be |
| 6708 | * removed. Otherwise, only the configured P2P group network will be removed |
| 6709 | * from the interface. |
| 6710 | */ |
| 6711 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) |
| 6712 | { |
| 6713 | |
| 6714 | if (wpa_s == NULL) |
| 6715 | return -1; |
| 6716 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6717 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? |
| 6718 | -1 : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6719 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6720 | |
| 6721 | |
| 6722 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) |
| 6723 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6724 | int ret; |
| 6725 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6726 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6727 | return 0; |
| 6728 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6729 | ret = p2p_in_progress(wpa_s->global->p2p); |
| 6730 | if (ret == 0) { |
| 6731 | /* |
| 6732 | * Check whether there is an ongoing WPS provisioning step (or |
| 6733 | * other parts of group formation) on another interface since |
| 6734 | * p2p_in_progress() does not report this to avoid issues for |
| 6735 | * scans during such provisioning step. |
| 6736 | */ |
| 6737 | if (wpa_s->global->p2p_group_formation && |
| 6738 | wpa_s->global->p2p_group_formation != wpa_s) { |
| 6739 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " |
| 6740 | "in group formation", |
| 6741 | wpa_s->global->p2p_group_formation->ifname); |
| 6742 | ret = 1; |
| 6743 | } |
| 6744 | } |
| 6745 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6746 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6747 | struct os_reltime now; |
| 6748 | os_get_reltime(&now); |
| 6749 | if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, |
| 6750 | P2P_MAX_INITIAL_CONN_WAIT_GO)) { |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6751 | /* Wait for the first client has expired */ |
| 6752 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 6753 | } else { |
| 6754 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); |
| 6755 | ret = 1; |
| 6756 | } |
| 6757 | } |
| 6758 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6759 | return ret; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | |
| 6763 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, |
| 6764 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6765 | { |
| 6766 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && |
| 6767 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6768 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6769 | /** |
| 6770 | * Remove the network by scheduling the group formation |
| 6771 | * timeout to happen immediately. The teardown code |
| 6772 | * needs to be scheduled to run asynch later so that we |
| 6773 | * don't delete data from under ourselves unexpectedly. |
| 6774 | * Calling wpas_p2p_group_formation_timeout directly |
| 6775 | * causes a series of crashes in WPS failure scenarios. |
| 6776 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6777 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
| 6778 | "P2P group network getting removed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6779 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 6780 | wpa_s->parent, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6781 | } |
| 6782 | } |
| 6783 | |
| 6784 | |
| 6785 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6786 | const u8 *addr, const u8 *ssid, |
| 6787 | size_t ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6788 | { |
| 6789 | struct wpa_ssid *s; |
| 6790 | size_t i; |
| 6791 | |
| 6792 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 6793 | if (s->disabled != 2) |
| 6794 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6795 | if (ssid && |
| 6796 | (ssid_len != s->ssid_len || |
| 6797 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) |
| 6798 | continue; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6799 | if (addr == NULL) { |
| 6800 | if (s->mode == WPAS_MODE_P2P_GO) |
| 6801 | return s; |
| 6802 | continue; |
| 6803 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6804 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
| 6805 | return s; /* peer is GO in the persistent group */ |
| 6806 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) |
| 6807 | continue; |
| 6808 | for (i = 0; i < s->num_p2p_clients; i++) { |
| 6809 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, |
| 6810 | addr, ETH_ALEN) == 0) |
| 6811 | return s; /* peer is P2P client in persistent |
| 6812 | * group */ |
| 6813 | } |
| 6814 | } |
| 6815 | |
| 6816 | return NULL; |
| 6817 | } |
| 6818 | |
| 6819 | |
| 6820 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, |
| 6821 | const u8 *addr) |
| 6822 | { |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6823 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6824 | wpa_s->parent, NULL) > 0) { |
| 6825 | /* |
| 6826 | * This can happen if WPS provisioning step is not terminated |
| 6827 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the |
| 6828 | * peer was able to connect, there is no need to time out group |
| 6829 | * formation after this, though. In addition, this is used with |
| 6830 | * the initial connection wait on the GO as a separate formation |
| 6831 | * timeout and as such, expected to be hit after the initial WPS |
| 6832 | * provisioning step. |
| 6833 | */ |
| 6834 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); |
| 6835 | } |
| 6836 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 6837 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); |
| 6838 | wpa_s->p2p_go_group_formation_completed = 1; |
| 6839 | wpa_s->global->p2p_group_formation = NULL; |
| 6840 | wpa_s->p2p_in_provisioning = 0; |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 6841 | wpa_s->p2p_in_invitation = 0; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6842 | } |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6843 | wpa_s->global->p2p_go_wait_client.sec = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6844 | if (addr == NULL) |
| 6845 | return; |
| 6846 | wpas_p2p_add_persistent_group_client(wpa_s, addr); |
| 6847 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6848 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6849 | |
| 6850 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 6851 | int group_added) |
| 6852 | { |
| 6853 | struct wpa_supplicant *group = wpa_s; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6854 | if (wpa_s->global->p2p_group_formation) |
| 6855 | group = wpa_s->global->p2p_group_formation; |
| 6856 | wpa_s = wpa_s->parent; |
| 6857 | offchannel_send_action_done(wpa_s); |
| 6858 | if (group_added) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6859 | wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6860 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); |
| 6861 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, |
| 6862 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, |
| 6863 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, |
| 6864 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6865 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6866 | wpa_s->p2p_go_ht40, |
| 6867 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | |
| 6871 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) |
| 6872 | { |
| 6873 | if (!wpa_s->p2p_fallback_to_go_neg || |
| 6874 | wpa_s->p2p_in_provisioning <= 5) |
| 6875 | return 0; |
| 6876 | |
| 6877 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) |
| 6878 | return 0; /* peer operating as a GO */ |
| 6879 | |
| 6880 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " |
| 6881 | "fallback to GO Negotiation"); |
| 6882 | wpas_p2p_fallback_to_go_neg(wpa_s, 1); |
| 6883 | |
| 6884 | return 1; |
| 6885 | } |
| 6886 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6887 | |
| 6888 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) |
| 6889 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6890 | struct wpa_supplicant *ifs; |
| 6891 | |
| 6892 | if (wpa_s->wpa_state > WPA_SCANNING) { |
| 6893 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " |
| 6894 | "concurrent operation", |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 6895 | wpa_s->conf->p2p_search_delay); |
| 6896 | return wpa_s->conf->p2p_search_delay; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6897 | } |
| 6898 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 6899 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, |
| 6900 | radio_list) { |
| 6901 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6902 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " |
| 6903 | "delay due to concurrent operation on " |
| 6904 | "interface %s", |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 6905 | wpa_s->conf->p2p_search_delay, |
| 6906 | ifs->ifname); |
| 6907 | return wpa_s->conf->p2p_search_delay; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | return 0; |
| 6912 | } |
| 6913 | |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 6914 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6915 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, |
| 6916 | struct wpa_ssid *s, const u8 *addr, |
| 6917 | int iface_addr) |
| 6918 | { |
| 6919 | struct psk_list_entry *psk, *tmp; |
| 6920 | int changed = 0; |
| 6921 | |
| 6922 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, |
| 6923 | list) { |
| 6924 | if ((iface_addr && !psk->p2p && |
| 6925 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || |
| 6926 | (!iface_addr && psk->p2p && |
| 6927 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { |
| 6928 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6929 | "P2P: Remove persistent group PSK list entry for " |
| 6930 | MACSTR " p2p=%u", |
| 6931 | MAC2STR(psk->addr), psk->p2p); |
| 6932 | dl_list_del(&psk->list); |
| 6933 | os_free(psk); |
| 6934 | changed++; |
| 6935 | } |
| 6936 | } |
| 6937 | |
| 6938 | return changed; |
| 6939 | } |
| 6940 | |
| 6941 | |
| 6942 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, |
| 6943 | const u8 *p2p_dev_addr, |
| 6944 | const u8 *psk, size_t psk_len) |
| 6945 | { |
| 6946 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6947 | struct wpa_ssid *persistent; |
Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 6948 | struct psk_list_entry *p, *last; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6949 | |
| 6950 | if (psk_len != sizeof(p->psk)) |
| 6951 | return; |
| 6952 | |
| 6953 | if (p2p_dev_addr) { |
| 6954 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR |
| 6955 | " p2p_dev_addr=" MACSTR, |
| 6956 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 6957 | if (is_zero_ether_addr(p2p_dev_addr)) |
| 6958 | p2p_dev_addr = NULL; |
| 6959 | } else { |
| 6960 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, |
| 6961 | MAC2STR(mac_addr)); |
| 6962 | } |
| 6963 | |
| 6964 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6965 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); |
| 6966 | /* To be added to persistent group once created */ |
| 6967 | if (wpa_s->global->add_psk == NULL) { |
| 6968 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); |
| 6969 | if (wpa_s->global->add_psk == NULL) |
| 6970 | return; |
| 6971 | } |
| 6972 | p = wpa_s->global->add_psk; |
| 6973 | if (p2p_dev_addr) { |
| 6974 | p->p2p = 1; |
| 6975 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6976 | } else { |
| 6977 | p->p2p = 0; |
| 6978 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6979 | } |
| 6980 | os_memcpy(p->psk, psk, psk_len); |
| 6981 | return; |
| 6982 | } |
| 6983 | |
| 6984 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { |
| 6985 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); |
| 6986 | return; |
| 6987 | } |
| 6988 | |
| 6989 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 6990 | ssid->ssid_len); |
| 6991 | if (!persistent) { |
| 6992 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); |
| 6993 | return; |
| 6994 | } |
| 6995 | |
| 6996 | p = os_zalloc(sizeof(*p)); |
| 6997 | if (p == NULL) |
| 6998 | return; |
| 6999 | if (p2p_dev_addr) { |
| 7000 | p->p2p = 1; |
| 7001 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 7002 | } else { |
| 7003 | p->p2p = 0; |
| 7004 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 7005 | } |
| 7006 | os_memcpy(p->psk, psk, psk_len); |
| 7007 | |
Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 7008 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS && |
| 7009 | (last = dl_list_last(&persistent->psk_list, |
| 7010 | struct psk_list_entry, list))) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7011 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " |
| 7012 | MACSTR " (p2p=%u) to make room for a new one", |
| 7013 | MAC2STR(last->addr), last->p2p); |
| 7014 | dl_list_del(&last->list); |
| 7015 | os_free(last); |
| 7016 | } |
| 7017 | |
| 7018 | wpas_p2p_remove_psk_entry(wpa_s->parent, persistent, |
| 7019 | p2p_dev_addr ? p2p_dev_addr : mac_addr, |
| 7020 | p2p_dev_addr == NULL); |
| 7021 | if (p2p_dev_addr) { |
| 7022 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" |
| 7023 | MACSTR, MAC2STR(p2p_dev_addr)); |
| 7024 | } else { |
| 7025 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, |
| 7026 | MAC2STR(mac_addr)); |
| 7027 | } |
| 7028 | dl_list_add(&persistent->psk_list, &p->list); |
| 7029 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7030 | if (wpa_s->parent->conf->update_config && |
| 7031 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 7032 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7033 | } |
| 7034 | |
| 7035 | |
| 7036 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, |
| 7037 | struct wpa_ssid *s, const u8 *addr, |
| 7038 | int iface_addr) |
| 7039 | { |
| 7040 | int res; |
| 7041 | |
| 7042 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 7043 | if (res > 0 && wpa_s->conf->update_config && |
| 7044 | wpa_config_write(wpa_s->confname, wpa_s->conf)) |
| 7045 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 7046 | "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7047 | } |
| 7048 | |
| 7049 | |
| 7050 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, |
| 7051 | const u8 *peer, int iface_addr) |
| 7052 | { |
| 7053 | struct hostapd_data *hapd; |
| 7054 | struct hostapd_wpa_psk *psk, *prev, *rem; |
| 7055 | struct sta_info *sta; |
| 7056 | |
| 7057 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || |
| 7058 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) |
| 7059 | return; |
| 7060 | |
| 7061 | /* Remove per-station PSK entry */ |
| 7062 | hapd = wpa_s->ap_iface->bss[0]; |
| 7063 | prev = NULL; |
| 7064 | psk = hapd->conf->ssid.wpa_psk; |
| 7065 | while (psk) { |
| 7066 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || |
| 7067 | (!iface_addr && |
| 7068 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { |
| 7069 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " |
| 7070 | MACSTR " iface_addr=%d", |
| 7071 | MAC2STR(peer), iface_addr); |
| 7072 | if (prev) |
| 7073 | prev->next = psk->next; |
| 7074 | else |
| 7075 | hapd->conf->ssid.wpa_psk = psk->next; |
| 7076 | rem = psk; |
| 7077 | psk = psk->next; |
| 7078 | os_free(rem); |
| 7079 | } else { |
| 7080 | prev = psk; |
| 7081 | psk = psk->next; |
| 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | /* Disconnect from group */ |
| 7086 | if (iface_addr) |
| 7087 | sta = ap_get_sta(hapd, peer); |
| 7088 | else |
| 7089 | sta = ap_get_sta_p2p(hapd, peer); |
| 7090 | if (sta) { |
| 7091 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR |
| 7092 | " (iface_addr=%d) from group", |
| 7093 | MAC2STR(peer), iface_addr); |
| 7094 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 7095 | WLAN_REASON_DEAUTH_LEAVING); |
| 7096 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, |
| 7102 | int iface_addr) |
| 7103 | { |
| 7104 | struct wpa_ssid *s; |
| 7105 | struct wpa_supplicant *w; |
| 7106 | |
| 7107 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); |
| 7108 | |
| 7109 | /* Remove from any persistent group */ |
| 7110 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 7111 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 7112 | continue; |
| 7113 | if (!iface_addr) |
| 7114 | wpas_remove_persistent_peer(wpa_s, s, peer, 0); |
| 7115 | wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr); |
| 7116 | } |
| 7117 | |
| 7118 | /* Remove from any operating group */ |
| 7119 | for (w = wpa_s->global->ifaces; w; w = w->next) |
| 7120 | wpas_p2p_remove_client_go(w, peer, iface_addr); |
| 7121 | } |
| 7122 | |
| 7123 | |
| 7124 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) |
| 7125 | { |
| 7126 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 7127 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); |
| 7128 | } |
| 7129 | |
| 7130 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 7131 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) |
| 7132 | { |
| 7133 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 7134 | |
| 7135 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); |
| 7136 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); |
| 7137 | } |
| 7138 | |
| 7139 | |
| 7140 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, |
| 7141 | struct wpa_ssid *ssid) |
| 7142 | { |
| 7143 | struct wpa_supplicant *iface; |
| 7144 | |
| 7145 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7146 | if (!iface->current_ssid || |
| 7147 | iface->current_ssid->frequency == freq || |
| 7148 | (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && |
| 7149 | !iface->current_ssid->p2p_group)) |
| 7150 | continue; |
| 7151 | |
| 7152 | /* Remove the connection with least priority */ |
| 7153 | if (!wpas_is_p2p_prioritized(iface)) { |
| 7154 | /* STA connection has priority over existing |
| 7155 | * P2P connection, so remove the interface. */ |
| 7156 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); |
| 7157 | eloop_register_timeout(0, 0, |
| 7158 | wpas_p2p_group_freq_conflict, |
| 7159 | iface, NULL); |
| 7160 | /* If connection in progress is P2P connection, do not |
| 7161 | * proceed for the connection. */ |
| 7162 | if (wpa_s == iface) |
| 7163 | return -1; |
| 7164 | else |
| 7165 | return 0; |
| 7166 | } else { |
| 7167 | /* P2P connection has priority, disable the STA network |
| 7168 | */ |
| 7169 | wpa_supplicant_disable_network(wpa_s->global->ifaces, |
| 7170 | ssid); |
| 7171 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, |
| 7172 | WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); |
| 7173 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, |
| 7174 | ETH_ALEN); |
| 7175 | /* If P2P connection is in progress, continue |
| 7176 | * connecting...*/ |
| 7177 | if (wpa_s == iface) |
| 7178 | return 0; |
| 7179 | else |
| 7180 | return -1; |
| 7181 | } |
| 7182 | } |
| 7183 | |
| 7184 | return 0; |
| 7185 | } |
| 7186 | |
| 7187 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7188 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) |
| 7189 | { |
| 7190 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 7191 | |
| 7192 | if (ssid == NULL || !ssid->p2p_group) |
| 7193 | return 0; |
| 7194 | |
| 7195 | if (wpa_s->p2p_last_4way_hs_fail && |
| 7196 | wpa_s->p2p_last_4way_hs_fail == ssid) { |
| 7197 | u8 go_dev_addr[ETH_ALEN]; |
| 7198 | struct wpa_ssid *persistent; |
| 7199 | |
| 7200 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, |
| 7201 | ssid->ssid, |
| 7202 | ssid->ssid_len) <= 0) { |
| 7203 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); |
| 7204 | goto disconnect; |
| 7205 | } |
| 7206 | |
| 7207 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" |
| 7208 | MACSTR, MAC2STR(go_dev_addr)); |
| 7209 | persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr, |
| 7210 | ssid->ssid, |
| 7211 | ssid->ssid_len); |
| 7212 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { |
| 7213 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); |
| 7214 | goto disconnect; |
| 7215 | } |
| 7216 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 7217 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", |
| 7218 | persistent->id); |
| 7219 | disconnect: |
| 7220 | wpa_s->p2p_last_4way_hs_fail = NULL; |
| 7221 | /* |
| 7222 | * Remove the group from a timeout to avoid issues with caller |
| 7223 | * continuing to use the interface if this is on a P2P group |
| 7224 | * interface. |
| 7225 | */ |
| 7226 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, |
| 7227 | wpa_s, NULL); |
| 7228 | return 1; |
| 7229 | } |
| 7230 | |
| 7231 | wpa_s->p2p_last_4way_hs_fail = ssid; |
| 7232 | return 0; |
| 7233 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7234 | |
| 7235 | |
| 7236 | #ifdef CONFIG_WPS_NFC |
| 7237 | |
| 7238 | static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc, |
| 7239 | struct wpabuf *p2p) |
| 7240 | { |
| 7241 | struct wpabuf *ret; |
| 7242 | size_t wsc_len; |
| 7243 | |
| 7244 | if (p2p == NULL) { |
| 7245 | wpabuf_free(wsc); |
| 7246 | wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover"); |
| 7247 | return NULL; |
| 7248 | } |
| 7249 | |
| 7250 | wsc_len = wsc ? wpabuf_len(wsc) : 0; |
| 7251 | ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p)); |
| 7252 | if (ret == NULL) { |
| 7253 | wpabuf_free(wsc); |
| 7254 | wpabuf_free(p2p); |
| 7255 | return NULL; |
| 7256 | } |
| 7257 | |
| 7258 | wpabuf_put_be16(ret, wsc_len); |
| 7259 | if (wsc) |
| 7260 | wpabuf_put_buf(ret, wsc); |
| 7261 | wpabuf_put_be16(ret, wpabuf_len(p2p)); |
| 7262 | wpabuf_put_buf(ret, p2p); |
| 7263 | |
| 7264 | wpabuf_free(wsc); |
| 7265 | wpabuf_free(p2p); |
| 7266 | wpa_hexdump_buf(MSG_DEBUG, |
| 7267 | "P2P: Generated NFC connection handover message", ret); |
| 7268 | |
| 7269 | if (ndef && ret) { |
| 7270 | struct wpabuf *tmp; |
| 7271 | tmp = ndef_build_p2p(ret); |
| 7272 | wpabuf_free(ret); |
| 7273 | if (tmp == NULL) { |
| 7274 | wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request"); |
| 7275 | return NULL; |
| 7276 | } |
| 7277 | ret = tmp; |
| 7278 | } |
| 7279 | |
| 7280 | return ret; |
| 7281 | } |
| 7282 | |
| 7283 | |
| 7284 | static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s, |
| 7285 | struct wpa_ssid **ssid, u8 *go_dev_addr) |
| 7286 | { |
| 7287 | struct wpa_supplicant *iface; |
| 7288 | |
| 7289 | if (go_dev_addr) |
| 7290 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 7291 | if (ssid) |
| 7292 | *ssid = NULL; |
| 7293 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7294 | if (iface->wpa_state < WPA_ASSOCIATING || |
| 7295 | iface->current_ssid == NULL || iface->assoc_freq == 0 || |
| 7296 | !iface->current_ssid->p2p_group || |
| 7297 | iface->current_ssid->mode != WPAS_MODE_INFRA) |
| 7298 | continue; |
| 7299 | if (ssid) |
| 7300 | *ssid = iface->current_ssid; |
| 7301 | if (go_dev_addr) |
| 7302 | os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN); |
| 7303 | return iface->assoc_freq; |
| 7304 | } |
| 7305 | return 0; |
| 7306 | } |
| 7307 | |
| 7308 | |
| 7309 | struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s, |
| 7310 | int ndef) |
| 7311 | { |
| 7312 | struct wpabuf *wsc, *p2p; |
| 7313 | struct wpa_ssid *ssid; |
| 7314 | u8 go_dev_addr[ETH_ALEN]; |
| 7315 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7316 | |
| 7317 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) { |
| 7318 | wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request"); |
| 7319 | return NULL; |
| 7320 | } |
| 7321 | |
| 7322 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7323 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7324 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) { |
| 7325 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request"); |
| 7326 | return NULL; |
| 7327 | } |
| 7328 | |
| 7329 | if (cli_freq == 0) { |
| 7330 | wsc = wps_build_nfc_handover_req_p2p( |
| 7331 | wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey); |
| 7332 | } else |
| 7333 | wsc = NULL; |
| 7334 | p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq, |
| 7335 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7336 | ssid ? ssid->ssid_len : 0); |
| 7337 | |
| 7338 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7339 | } |
| 7340 | |
| 7341 | |
| 7342 | struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s, |
| 7343 | int ndef, int tag) |
| 7344 | { |
| 7345 | struct wpabuf *wsc, *p2p; |
| 7346 | struct wpa_ssid *ssid; |
| 7347 | u8 go_dev_addr[ETH_ALEN]; |
| 7348 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7349 | |
| 7350 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7351 | return NULL; |
| 7352 | |
| 7353 | if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7354 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7355 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7356 | return NULL; |
| 7357 | |
| 7358 | if (cli_freq == 0) { |
| 7359 | wsc = wps_build_nfc_handover_sel_p2p( |
| 7360 | wpa_s->parent->wps, |
| 7361 | tag ? wpa_s->conf->wps_nfc_dev_pw_id : |
| 7362 | DEV_PW_NFC_CONNECTION_HANDOVER, |
| 7363 | wpa_s->conf->wps_nfc_dh_pubkey, |
| 7364 | tag ? wpa_s->conf->wps_nfc_dev_pw : NULL); |
| 7365 | } else |
| 7366 | wsc = NULL; |
| 7367 | p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq, |
| 7368 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7369 | ssid ? ssid->ssid_len : 0); |
| 7370 | |
| 7371 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7372 | } |
| 7373 | |
| 7374 | |
| 7375 | static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s, |
| 7376 | struct p2p_nfc_params *params) |
| 7377 | { |
| 7378 | wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC " |
| 7379 | "connection handover (freq=%d)", |
| 7380 | params->go_freq); |
| 7381 | |
| 7382 | if (params->go_freq && params->go_ssid_len) { |
| 7383 | wpa_s->p2p_wps_method = WPS_NFC; |
| 7384 | wpa_s->pending_join_wps_method = WPS_NFC; |
| 7385 | os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN); |
| 7386 | os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr, |
| 7387 | ETH_ALEN); |
| 7388 | return wpas_p2p_join_start(wpa_s, params->go_freq, |
| 7389 | params->go_ssid, |
| 7390 | params->go_ssid_len); |
| 7391 | } |
| 7392 | |
| 7393 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7394 | WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent, |
| 7395 | params->go_freq, -1, 0, 1, 1); |
| 7396 | } |
| 7397 | |
| 7398 | |
| 7399 | static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s, |
| 7400 | struct p2p_nfc_params *params, int tag) |
| 7401 | { |
| 7402 | int res, persistent; |
| 7403 | struct wpa_ssid *ssid; |
| 7404 | |
| 7405 | wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC " |
| 7406 | "connection handover"); |
| 7407 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 7408 | ssid = wpa_s->current_ssid; |
| 7409 | if (ssid == NULL) |
| 7410 | continue; |
| 7411 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 7412 | continue; |
| 7413 | if (wpa_s->ap_iface == NULL) |
| 7414 | continue; |
| 7415 | break; |
| 7416 | } |
| 7417 | if (wpa_s == NULL) { |
| 7418 | wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface"); |
| 7419 | return -1; |
| 7420 | } |
| 7421 | |
| 7422 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 7423 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 7424 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 7425 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 7426 | return -1; |
| 7427 | } |
| 7428 | res = wpas_ap_wps_add_nfc_pw( |
| 7429 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 7430 | wpa_s->parent->p2p_oob_dev_pw, |
| 7431 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 7432 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 7433 | if (res) |
| 7434 | return res; |
| 7435 | |
| 7436 | if (!tag) { |
| 7437 | wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation"); |
| 7438 | return 0; |
| 7439 | } |
| 7440 | |
| 7441 | if (!params->peer || |
| 7442 | !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE)) |
| 7443 | return 0; |
| 7444 | |
| 7445 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR |
| 7446 | " to join", MAC2STR(params->peer->p2p_device_addr)); |
| 7447 | |
| 7448 | wpa_s->global->p2p_invite_group = wpa_s; |
| 7449 | persistent = ssid->p2p_persistent_group && |
| 7450 | wpas_p2p_get_persistent(wpa_s->parent, |
| 7451 | params->peer->p2p_device_addr, |
| 7452 | ssid->ssid, ssid->ssid_len); |
| 7453 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 7454 | |
| 7455 | return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr, |
| 7456 | P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr, |
| 7457 | ssid->ssid, ssid->ssid_len, ssid->frequency, |
| 7458 | wpa_s->global->p2p_dev_addr, persistent, 0, |
| 7459 | wpa_s->parent->p2p_oob_dev_pw_id); |
| 7460 | } |
| 7461 | |
| 7462 | |
| 7463 | static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s, |
| 7464 | struct p2p_nfc_params *params, |
| 7465 | int forced_freq) |
| 7466 | { |
| 7467 | wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC " |
| 7468 | "connection handover"); |
| 7469 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7470 | WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent, |
| 7471 | forced_freq, -1, 0, 1, 1); |
| 7472 | } |
| 7473 | |
| 7474 | |
| 7475 | static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s, |
| 7476 | struct p2p_nfc_params *params, |
| 7477 | int forced_freq) |
| 7478 | { |
| 7479 | int res; |
| 7480 | |
| 7481 | wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC " |
| 7482 | "connection handover"); |
| 7483 | res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7484 | WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent, |
| 7485 | forced_freq, -1, 0, 1, 1); |
| 7486 | if (res) |
| 7487 | return res; |
| 7488 | |
| 7489 | res = wpas_p2p_listen(wpa_s, 60); |
| 7490 | if (res) { |
| 7491 | p2p_unauthorize(wpa_s->global->p2p, |
| 7492 | params->peer->p2p_device_addr); |
| 7493 | } |
| 7494 | |
| 7495 | return res; |
| 7496 | } |
| 7497 | |
| 7498 | |
| 7499 | static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s, |
| 7500 | const struct wpabuf *data, |
| 7501 | int sel, int tag, int forced_freq) |
| 7502 | { |
| 7503 | const u8 *pos, *end; |
| 7504 | u16 len, id; |
| 7505 | struct p2p_nfc_params params; |
| 7506 | int res; |
| 7507 | |
| 7508 | os_memset(¶ms, 0, sizeof(params)); |
| 7509 | params.sel = sel; |
| 7510 | |
| 7511 | wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data); |
| 7512 | |
| 7513 | pos = wpabuf_head(data); |
| 7514 | end = pos + wpabuf_len(data); |
| 7515 | |
| 7516 | if (end - pos < 2) { |
| 7517 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC " |
| 7518 | "attributes"); |
| 7519 | return -1; |
| 7520 | } |
| 7521 | len = WPA_GET_BE16(pos); |
| 7522 | pos += 2; |
| 7523 | if (pos + len > end) { |
| 7524 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC " |
| 7525 | "attributes"); |
| 7526 | return -1; |
| 7527 | } |
| 7528 | params.wsc_attr = pos; |
| 7529 | params.wsc_len = len; |
| 7530 | pos += len; |
| 7531 | |
| 7532 | if (end - pos < 2) { |
| 7533 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P " |
| 7534 | "attributes"); |
| 7535 | return -1; |
| 7536 | } |
| 7537 | len = WPA_GET_BE16(pos); |
| 7538 | pos += 2; |
| 7539 | if (pos + len > end) { |
| 7540 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P " |
| 7541 | "attributes"); |
| 7542 | return -1; |
| 7543 | } |
| 7544 | params.p2p_attr = pos; |
| 7545 | params.p2p_len = len; |
| 7546 | pos += len; |
| 7547 | |
| 7548 | wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes", |
| 7549 | params.wsc_attr, params.wsc_len); |
| 7550 | wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes", |
| 7551 | params.p2p_attr, params.p2p_len); |
| 7552 | if (pos < end) { |
| 7553 | wpa_hexdump(MSG_DEBUG, |
| 7554 | "P2P: Ignored extra data after P2P attributes", |
| 7555 | pos, end - pos); |
| 7556 | } |
| 7557 | |
| 7558 | res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms); |
| 7559 | if (res) |
| 7560 | return res; |
| 7561 | |
| 7562 | if (params.next_step == NO_ACTION) |
| 7563 | return 0; |
| 7564 | |
| 7565 | if (params.next_step == BOTH_GO) { |
| 7566 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR, |
| 7567 | MAC2STR(params.peer->p2p_device_addr)); |
| 7568 | return 0; |
| 7569 | } |
| 7570 | |
| 7571 | if (params.next_step == PEER_CLIENT) { |
| 7572 | if (!is_zero_ether_addr(params.go_dev_addr)) { |
| 7573 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7574 | "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR |
| 7575 | " ssid=\"%s\"", |
| 7576 | MAC2STR(params.peer->p2p_device_addr), |
| 7577 | params.go_freq, |
| 7578 | MAC2STR(params.go_dev_addr), |
| 7579 | wpa_ssid_txt(params.go_ssid, |
| 7580 | params.go_ssid_len)); |
| 7581 | } else { |
| 7582 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7583 | "peer=" MACSTR " freq=%d", |
| 7584 | MAC2STR(params.peer->p2p_device_addr), |
| 7585 | params.go_freq); |
| 7586 | } |
| 7587 | return 0; |
| 7588 | } |
| 7589 | |
| 7590 | if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) { |
| 7591 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer=" |
| 7592 | MACSTR, MAC2STR(params.peer->p2p_device_addr)); |
| 7593 | return 0; |
| 7594 | } |
| 7595 | |
| 7596 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7597 | wpa_s->p2p_oob_dev_pw = NULL; |
| 7598 | |
| 7599 | if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { |
| 7600 | wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw " |
| 7601 | "received"); |
| 7602 | return -1; |
| 7603 | } |
| 7604 | |
| 7605 | id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN); |
| 7606 | wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id); |
| 7607 | wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash", |
| 7608 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7609 | os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash, |
| 7610 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7611 | wpa_s->p2p_peer_oob_pk_hash_known = 1; |
| 7612 | |
| 7613 | if (tag) { |
| 7614 | if (id < 0x10) { |
| 7615 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid " |
| 7616 | "peer OOB Device Password Id %u", id); |
| 7617 | return -1; |
| 7618 | } |
| 7619 | wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB " |
| 7620 | "Device Password Id %u", id); |
| 7621 | wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password", |
| 7622 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7623 | params.oob_dev_pw_len - |
| 7624 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7625 | wpa_s->p2p_oob_dev_pw_id = id; |
| 7626 | wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy( |
| 7627 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7628 | params.oob_dev_pw_len - |
| 7629 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7630 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7631 | return -1; |
| 7632 | |
| 7633 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7634 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7635 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7636 | return -1; |
| 7637 | } else { |
| 7638 | wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake " |
| 7639 | "without Device Password"); |
| 7640 | wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; |
| 7641 | } |
| 7642 | |
| 7643 | switch (params.next_step) { |
| 7644 | case NO_ACTION: |
| 7645 | case BOTH_GO: |
| 7646 | case PEER_CLIENT: |
| 7647 | /* already covered above */ |
| 7648 | return 0; |
| 7649 | case JOIN_GROUP: |
| 7650 | return wpas_p2p_nfc_join_group(wpa_s, ¶ms); |
| 7651 | case AUTH_JOIN: |
| 7652 | return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag); |
| 7653 | case INIT_GO_NEG: |
| 7654 | return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq); |
| 7655 | case RESP_GO_NEG: |
| 7656 | /* TODO: use own OOB Dev Pw */ |
| 7657 | return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq); |
| 7658 | } |
| 7659 | |
| 7660 | return -1; |
| 7661 | } |
| 7662 | |
| 7663 | |
| 7664 | int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s, |
| 7665 | const struct wpabuf *data, int forced_freq) |
| 7666 | { |
| 7667 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7668 | return -1; |
| 7669 | |
| 7670 | return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq); |
| 7671 | } |
| 7672 | |
| 7673 | |
| 7674 | int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init, |
| 7675 | const struct wpabuf *req, |
| 7676 | const struct wpabuf *sel, int forced_freq) |
| 7677 | { |
| 7678 | struct wpabuf *tmp; |
| 7679 | int ret; |
| 7680 | |
| 7681 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7682 | return -1; |
| 7683 | |
| 7684 | wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported"); |
| 7685 | |
| 7686 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req", |
| 7687 | wpabuf_head(req), wpabuf_len(req)); |
| 7688 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel", |
| 7689 | wpabuf_head(sel), wpabuf_len(sel)); |
| 7690 | if (forced_freq) |
| 7691 | wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq); |
| 7692 | tmp = ndef_parse_p2p(init ? sel : req); |
| 7693 | if (tmp == NULL) { |
| 7694 | wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF"); |
| 7695 | return -1; |
| 7696 | } |
| 7697 | |
| 7698 | ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0, |
| 7699 | forced_freq); |
| 7700 | wpabuf_free(tmp); |
| 7701 | |
| 7702 | return ret; |
| 7703 | } |
| 7704 | |
| 7705 | |
| 7706 | int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled) |
| 7707 | { |
| 7708 | const u8 *if_addr; |
| 7709 | int go_intent = wpa_s->conf->p2p_go_intent; |
| 7710 | struct wpa_supplicant *iface; |
| 7711 | |
| 7712 | if (wpa_s->global->p2p == NULL) |
| 7713 | return -1; |
| 7714 | |
| 7715 | if (!enabled) { |
| 7716 | wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag"); |
| 7717 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 7718 | { |
| 7719 | if (!iface->ap_iface) |
| 7720 | continue; |
| 7721 | hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]); |
| 7722 | } |
| 7723 | p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0, |
| 7724 | 0, NULL); |
| 7725 | if (wpa_s->p2p_nfc_tag_enabled) |
| 7726 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 7727 | wpa_s->p2p_nfc_tag_enabled = 0; |
| 7728 | return 0; |
| 7729 | } |
| 7730 | |
| 7731 | if (wpa_s->global->p2p_disabled) |
| 7732 | return -1; |
| 7733 | |
| 7734 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL || |
| 7735 | wpa_s->conf->wps_nfc_dh_privkey == NULL || |
| 7736 | wpa_s->conf->wps_nfc_dev_pw == NULL || |
| 7737 | wpa_s->conf->wps_nfc_dev_pw_id < 0x10) { |
| 7738 | wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured " |
| 7739 | "to allow static handover cases"); |
| 7740 | return -1; |
| 7741 | } |
| 7742 | |
| 7743 | wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag"); |
| 7744 | |
| 7745 | wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7746 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7747 | wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7748 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7749 | return -1; |
| 7750 | wpa_s->p2p_peer_oob_pk_hash_known = 0; |
| 7751 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 7752 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO || |
| 7753 | wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) { |
| 7754 | /* |
| 7755 | * P2P Group Interface present and the command came on group |
| 7756 | * interface, so enable the token for the current interface. |
| 7757 | */ |
| 7758 | wpa_s->create_p2p_iface = 0; |
| 7759 | } else { |
| 7760 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 7761 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7762 | |
| 7763 | if (wpa_s->create_p2p_iface) { |
| 7764 | enum wpa_driver_if_type iftype; |
| 7765 | /* Prepare to add a new interface for the group */ |
| 7766 | iftype = WPA_IF_P2P_GROUP; |
| 7767 | if (go_intent == 15) |
| 7768 | iftype = WPA_IF_P2P_GO; |
| 7769 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 7770 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 7771 | "interface for the group"); |
| 7772 | return -1; |
| 7773 | } |
| 7774 | |
| 7775 | if_addr = wpa_s->pending_interface_addr; |
| 7776 | } else |
| 7777 | if_addr = wpa_s->own_addr; |
| 7778 | |
| 7779 | wpa_s->p2p_nfc_tag_enabled = enabled; |
| 7780 | |
| 7781 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7782 | struct hostapd_data *hapd; |
| 7783 | if (iface->ap_iface == NULL) |
| 7784 | continue; |
| 7785 | hapd = iface->ap_iface->bss[0]; |
| 7786 | wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); |
| 7787 | hapd->conf->wps_nfc_dh_pubkey = |
| 7788 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey); |
| 7789 | wpabuf_free(hapd->conf->wps_nfc_dh_privkey); |
| 7790 | hapd->conf->wps_nfc_dh_privkey = |
| 7791 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey); |
| 7792 | wpabuf_free(hapd->conf->wps_nfc_dev_pw); |
| 7793 | hapd->conf->wps_nfc_dev_pw = |
| 7794 | wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7795 | hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7796 | |
| 7797 | if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) { |
| 7798 | wpa_dbg(iface, MSG_DEBUG, |
| 7799 | "P2P: Failed to enable NFC Tag for GO"); |
| 7800 | } |
| 7801 | } |
| 7802 | p2p_set_authorized_oob_dev_pw_id( |
| 7803 | wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent, |
| 7804 | if_addr); |
| 7805 | |
| 7806 | return 0; |
| 7807 | } |
| 7808 | |
| 7809 | #endif /* CONFIG_WPS_NFC */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 7810 | |
| 7811 | |
| 7812 | static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, |
| 7813 | struct wpa_used_freq_data *freqs, |
| 7814 | unsigned int num) |
| 7815 | { |
| 7816 | u8 curr_chan, cand, chan; |
| 7817 | unsigned int i; |
| 7818 | |
| 7819 | curr_chan = p2p_get_listen_channel(wpa_s->global->p2p); |
| 7820 | for (i = 0, cand = 0; i < num; i++) { |
| 7821 | ieee80211_freq_to_chan(freqs[i].freq, &chan); |
| 7822 | if (curr_chan == chan) { |
| 7823 | cand = 0; |
| 7824 | break; |
| 7825 | } |
| 7826 | |
| 7827 | if (chan == 1 || chan == 6 || chan == 11) |
| 7828 | cand = chan; |
| 7829 | } |
| 7830 | |
| 7831 | if (cand) { |
| 7832 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 7833 | "P2P: Update Listen channel to %u baased on operating channel", |
| 7834 | cand); |
| 7835 | p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0); |
| 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | |
| 7840 | void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s) |
| 7841 | { |
| 7842 | struct wpa_used_freq_data *freqs; |
| 7843 | unsigned int num = wpa_s->num_multichan_concurrent; |
| 7844 | |
| 7845 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7846 | return; |
| 7847 | |
| 7848 | /* |
| 7849 | * If possible, optimize the Listen channel to be a channel that is |
| 7850 | * already used by one of the other interfaces. |
| 7851 | */ |
| 7852 | if (!wpa_s->conf->p2p_optimize_listen_chan) |
| 7853 | return; |
| 7854 | |
| 7855 | if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED) |
| 7856 | return; |
| 7857 | |
| 7858 | freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); |
| 7859 | if (!freqs) |
| 7860 | return; |
| 7861 | |
| 7862 | num = get_shared_radio_freqs_data(wpa_s, freqs, num); |
| 7863 | |
| 7864 | wpas_p2p_optimize_listen_channel(wpa_s, freqs, num); |
| 7865 | os_free(freqs); |
| 7866 | } |
| 7867 | |
| 7868 | |
| 7869 | void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s) |
| 7870 | { |
| 7871 | if (wpa_s == wpa_s->parent) |
| 7872 | wpas_p2p_group_remove(wpa_s, "*"); |
| 7873 | if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) { |
| 7874 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing " |
| 7875 | "the management interface is being removed"); |
| 7876 | wpas_p2p_deinit_global(wpa_s->global); |
| 7877 | } |
| 7878 | } |
| 7879 | |
| 7880 | |
| 7881 | void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s) |
| 7882 | { |
| 7883 | if (wpa_s->ap_iface->bss) |
| 7884 | wpa_s->ap_iface->bss[0]->p2p_group = NULL; |
| 7885 | wpas_p2p_group_deinit(wpa_s); |
| 7886 | } |