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" |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 25 | #include "ap/dfs.h" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 26 | #include "eapol_supp/eapol_supp_sm.h" |
| 27 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 28 | #include "wpa_supplicant_i.h" |
| 29 | #include "driver_i.h" |
| 30 | #include "ap.h" |
| 31 | #include "config_ssid.h" |
| 32 | #include "config.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 33 | #include "notify.h" |
| 34 | #include "scan.h" |
| 35 | #include "bss.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 36 | #include "offchannel.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | #include "wps_supplicant.h" |
| 38 | #include "p2p_supplicant.h" |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 39 | #include "wifi_display.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 | |
| 41 | |
| 42 | /* |
| 43 | * How many times to try to scan to find the GO before giving up on join |
| 44 | * request. |
| 45 | */ |
| 46 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 |
| 47 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 48 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 |
| 49 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 50 | #ifndef P2P_MAX_CLIENT_IDLE |
| 51 | /* |
| 52 | * How many seconds to try to reconnect to the GO when connection in P2P client |
| 53 | * role has been lost. |
| 54 | */ |
| 55 | #define P2P_MAX_CLIENT_IDLE 10 |
| 56 | #endif /* P2P_MAX_CLIENT_IDLE */ |
| 57 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 58 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
| 59 | /* |
| 60 | * 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] | 61 | * WPS provisioning step or after the re-invocation of a persistent group on a |
| 62 | * P2P Client. |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 63 | */ |
| 64 | #define P2P_MAX_INITIAL_CONN_WAIT 10 |
| 65 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ |
| 66 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 67 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO |
| 68 | /* |
| 69 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 70 | * WPS provisioning step on the GO. This controls the extra time the P2P |
| 71 | * operation is considered to be in progress (e.g., to delay other scans) after |
| 72 | * WPS provisioning has been completed on the GO during group formation. |
| 73 | */ |
| 74 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 |
| 75 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ |
| 76 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 77 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE |
| 78 | /* |
| 79 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 80 | * re-invocation of a persistent group on the GO when the client is expected |
| 81 | * to connect automatically (no user interaction). |
| 82 | */ |
| 83 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 |
| 84 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ |
| 85 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 86 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 87 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 88 | enum p2p_group_removal_reason { |
| 89 | P2P_GROUP_REMOVAL_UNKNOWN, |
| 90 | P2P_GROUP_REMOVAL_SILENT, |
| 91 | P2P_GROUP_REMOVAL_FORMATION_FAILED, |
| 92 | P2P_GROUP_REMOVAL_REQUESTED, |
| 93 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, |
| 94 | P2P_GROUP_REMOVAL_UNAVAILABLE, |
| 95 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 96 | P2P_GROUP_REMOVAL_PSK_FAILURE, |
| 97 | P2P_GROUP_REMOVAL_FREQ_CONFLICT |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 100 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 101 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 102 | static struct wpa_supplicant * |
| 103 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 104 | int go); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 105 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 106 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 107 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 108 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 109 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
| 110 | 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] | 111 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 112 | int auto_join, int freq, |
| 113 | const u8 *ssid, size_t ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 114 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
| 115 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
| 116 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
| 117 | 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] | 118 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 119 | void *timeout_ctx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 120 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 121 | static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 122 | int group_added); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 123 | static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 124 | static void wpas_stop_listen(void *ctx); |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 125 | 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] | 126 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 127 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 128 | enum wpa_driver_if_type type); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 129 | |
| 130 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Get the number of concurrent channels that the HW can operate, but that are |
| 133 | * currently not in use by any of the wpa_supplicant interfaces. |
| 134 | */ |
| 135 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) |
| 136 | { |
| 137 | int *freqs; |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 138 | int num, unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 139 | |
| 140 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 141 | if (!freqs) |
| 142 | return -1; |
| 143 | |
| 144 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 145 | wpa_s->num_multichan_concurrent); |
| 146 | os_free(freqs); |
| 147 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 148 | unused = wpa_s->num_multichan_concurrent - num; |
| 149 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); |
| 150 | return unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | /* |
| 155 | * Get the frequencies that are currently in use by one or more of the virtual |
| 156 | * interfaces, and that are also valid for P2P operation. |
| 157 | */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 158 | static unsigned int |
| 159 | wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, |
| 160 | struct wpa_used_freq_data *p2p_freqs, |
| 161 | unsigned int len) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 162 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 163 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 164 | unsigned int num, i, j; |
| 165 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 166 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 167 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 168 | if (!freqs) |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 169 | return 0; |
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 | num = get_shared_radio_freqs_data(wpa_s, freqs, |
| 172 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 173 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 174 | os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 175 | |
| 176 | for (i = 0, j = 0; i < num && j < len; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 177 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 178 | p2p_freqs[j++] = freqs[i]; |
| 179 | } |
| 180 | |
| 181 | os_free(freqs); |
| 182 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 183 | dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 184 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 185 | return j; |
| 186 | } |
| 187 | |
| 188 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 189 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, |
| 190 | int freq) |
| 191 | { |
| 192 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 193 | return; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 194 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 195 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 196 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 197 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", |
| 198 | freq); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 199 | freq = 0; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 200 | } |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 201 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); |
| 202 | } |
| 203 | |
| 204 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 205 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 206 | struct wpa_scan_results *scan_res) |
| 207 | { |
| 208 | size_t i; |
| 209 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 210 | if (wpa_s->p2p_scan_work) { |
| 211 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; |
| 212 | wpa_s->p2p_scan_work = NULL; |
| 213 | radio_work_done(work); |
| 214 | } |
| 215 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 216 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 217 | return; |
| 218 | |
| 219 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", |
| 220 | (int) scan_res->num); |
| 221 | |
| 222 | for (i = 0; i < scan_res->num; i++) { |
| 223 | struct wpa_scan_res *bss = scan_res->res[i]; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 224 | struct os_reltime time_tmp_age, entry_ts; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 225 | const u8 *ies; |
| 226 | size_t ies_len; |
| 227 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 228 | time_tmp_age.sec = bss->age / 1000; |
| 229 | time_tmp_age.usec = (bss->age % 1000) * 1000; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 230 | os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 231 | |
| 232 | ies = (const u8 *) (bss + 1); |
| 233 | ies_len = bss->ie_len; |
| 234 | if (bss->beacon_ie_len > 0 && |
| 235 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && |
| 236 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { |
| 237 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " |
| 238 | MACSTR, MAC2STR(bss->bssid)); |
| 239 | ies = ies + ies_len; |
| 240 | ies_len = bss->beacon_ie_len; |
| 241 | } |
| 242 | |
| 243 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 244 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 245 | bss->freq, &entry_ts, bss->level, |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 246 | ies, ies_len) > 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | |
| 250 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 251 | } |
| 252 | |
| 253 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 254 | static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) |
| 255 | { |
| 256 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 257 | struct wpa_driver_scan_params *params = work->ctx; |
| 258 | int ret; |
| 259 | |
| 260 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 261 | if (!work->started) { |
| 262 | wpa_scan_free_params(params); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | wpa_s->p2p_scan_work = NULL; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
| 270 | ret = wpa_drv_scan(wpa_s, params); |
| 271 | wpa_scan_free_params(params); |
| 272 | work->ctx = NULL; |
| 273 | if (ret) { |
| 274 | radio_work_done(work); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 275 | p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 279 | p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 280 | os_get_reltime(&wpa_s->scan_trigger_time); |
| 281 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
| 282 | wpa_s->own_scan_requested = 1; |
| 283 | wpa_s->p2p_scan_work = work; |
| 284 | } |
| 285 | |
| 286 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 287 | static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s, |
| 288 | int freq) |
| 289 | { |
| 290 | if (wpa_s->global->p2p_24ghz_social_channels && |
| 291 | (freq == 2412 || freq == 2437 || freq == 2462)) { |
| 292 | /* |
| 293 | * Search all social channels regardless of whether these have |
| 294 | * been disabled for P2P operating channel use to avoid missing |
| 295 | * peers. |
| 296 | */ |
| 297 | return 1; |
| 298 | } |
| 299 | return p2p_supported_freq(wpa_s->global->p2p, freq); |
| 300 | } |
| 301 | |
| 302 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 303 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
| 304 | unsigned int num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 305 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 306 | { |
| 307 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 308 | struct wpa_driver_scan_params *params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 309 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 310 | unsigned int num_channels = 0; |
| 311 | int social_channels_freq[] = { 2412, 2437, 2462, 60480 }; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 312 | size_t ielen; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 313 | u8 *n, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 314 | |
| 315 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 316 | return -1; |
| 317 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 318 | if (wpa_s->p2p_scan_work) { |
| 319 | wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); |
| 320 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 323 | params = os_zalloc(sizeof(*params)); |
| 324 | if (params == NULL) |
| 325 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 326 | |
| 327 | /* P2P Wildcard SSID */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 328 | params->num_ssids = 1; |
| 329 | n = os_malloc(P2P_WILDCARD_SSID_LEN); |
| 330 | if (n == NULL) |
| 331 | goto fail; |
| 332 | os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 333 | params->ssids[0].ssid = n; |
| 334 | params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 335 | |
| 336 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 337 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
| 338 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 339 | num_req_dev_types, req_dev_types); |
| 340 | if (wps_ie == NULL) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 341 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 342 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 343 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 344 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 345 | if (ies == NULL) { |
| 346 | wpabuf_free(wps_ie); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 347 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 348 | } |
| 349 | wpabuf_put_buf(ies, wps_ie); |
| 350 | wpabuf_free(wps_ie); |
| 351 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 352 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 353 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 354 | params->p2p_probe = 1; |
| 355 | n = os_malloc(wpabuf_len(ies)); |
| 356 | if (n == NULL) { |
| 357 | wpabuf_free(ies); |
| 358 | goto fail; |
| 359 | } |
| 360 | os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); |
| 361 | params->extra_ies = n; |
| 362 | params->extra_ies_len = wpabuf_len(ies); |
| 363 | wpabuf_free(ies); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 364 | |
| 365 | switch (type) { |
| 366 | case P2P_SCAN_SOCIAL: |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 367 | params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1, |
| 368 | sizeof(int)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 369 | if (params->freqs == NULL) |
| 370 | goto fail; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 371 | for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 372 | if (wpas_p2p_search_social_channel( |
| 373 | wpa_s, social_channels_freq[i])) |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 374 | params->freqs[num_channels++] = |
| 375 | social_channels_freq[i]; |
| 376 | } |
| 377 | params->freqs[num_channels++] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 378 | break; |
| 379 | case P2P_SCAN_FULL: |
| 380 | break; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 381 | case P2P_SCAN_SPECIFIC: |
| 382 | params->freqs = os_calloc(2, sizeof(int)); |
| 383 | if (params->freqs == NULL) |
| 384 | goto fail; |
| 385 | params->freqs[0] = freq; |
| 386 | params->freqs[1] = 0; |
| 387 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 388 | case P2P_SCAN_SOCIAL_PLUS_ONE: |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 389 | params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2, |
| 390 | sizeof(int)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 391 | if (params->freqs == NULL) |
| 392 | goto fail; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 393 | for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 394 | if (wpas_p2p_search_social_channel( |
| 395 | wpa_s, social_channels_freq[i])) |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 396 | params->freqs[num_channels++] = |
| 397 | social_channels_freq[i]; |
| 398 | } |
| 399 | if (p2p_supported_freq(wpa_s->global->p2p, freq)) |
| 400 | params->freqs[num_channels++] = freq; |
| 401 | params->freqs[num_channels++] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 405 | radio_remove_works(wpa_s, "p2p-scan", 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 406 | if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, |
| 407 | params) < 0) |
| 408 | goto fail; |
| 409 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 410 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 411 | fail: |
| 412 | wpa_scan_free_params(params); |
| 413 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 417 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
| 418 | { |
| 419 | switch (p2p_group_interface) { |
| 420 | case P2P_GROUP_INTERFACE_PENDING: |
| 421 | return WPA_IF_P2P_GROUP; |
| 422 | case P2P_GROUP_INTERFACE_GO: |
| 423 | return WPA_IF_P2P_GO; |
| 424 | case P2P_GROUP_INTERFACE_CLIENT: |
| 425 | return WPA_IF_P2P_CLIENT; |
| 426 | } |
| 427 | |
| 428 | return WPA_IF_P2P_GROUP; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
| 433 | const u8 *ssid, |
| 434 | size_t ssid_len, int *go) |
| 435 | { |
| 436 | struct wpa_ssid *s; |
| 437 | |
| 438 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 439 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 440 | if (s->disabled != 0 || !s->p2p_group || |
| 441 | s->ssid_len != ssid_len || |
| 442 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
| 443 | continue; |
| 444 | if (s->mode == WPAS_MODE_P2P_GO && |
| 445 | s != wpa_s->current_ssid) |
| 446 | continue; |
| 447 | if (go) |
| 448 | *go = s->mode == WPAS_MODE_P2P_GO; |
| 449 | return wpa_s; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 457 | static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx) |
| 458 | { |
| 459 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 460 | wpa_printf(MSG_DEBUG, |
| 461 | "P2P: Complete previously requested removal of %s", |
| 462 | wpa_s->ifname); |
| 463 | wpas_p2p_disconnect(wpa_s); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s, |
| 468 | struct wpa_supplicant *calling_wpa_s) |
| 469 | { |
| 470 | if (calling_wpa_s == wpa_s && wpa_s && |
| 471 | wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 472 | /* |
| 473 | * The calling wpa_s instance is going to be removed. Do that |
| 474 | * from an eloop callback to keep the instance available until |
| 475 | * the caller has returned. This my be needed, e.g., to provide |
| 476 | * control interface responses on the per-interface socket. |
| 477 | */ |
| 478 | if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect, |
| 479 | wpa_s, NULL) < 0) |
| 480 | return -1; |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | return wpas_p2p_disconnect(wpa_s); |
| 485 | } |
| 486 | |
| 487 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 488 | /* Determine total number of clients in active groups where we are the GO */ |
| 489 | static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s) |
| 490 | { |
| 491 | unsigned int count = 0; |
| 492 | struct wpa_ssid *s; |
| 493 | |
| 494 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 495 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 496 | wpa_printf(MSG_DEBUG, |
| 497 | "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d", |
| 498 | wpa_s, s, s->disabled, s->p2p_group, |
| 499 | s->mode); |
| 500 | if (!s->disabled && s->p2p_group && |
| 501 | s->mode == WPAS_MODE_P2P_GO) { |
| 502 | count += p2p_get_group_num_members( |
| 503 | wpa_s->p2p_group); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return count; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | /* Find an interface for a P2P group where we are the GO */ |
| 513 | static struct wpa_supplicant * |
| 514 | wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s) |
| 515 | { |
| 516 | struct wpa_supplicant *save = NULL; |
| 517 | struct wpa_ssid *s; |
| 518 | |
| 519 | if (!wpa_s) |
| 520 | return NULL; |
| 521 | |
| 522 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 523 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 524 | if (s->disabled || !s->p2p_group || |
| 525 | s->mode != WPAS_MODE_P2P_GO) |
| 526 | continue; |
| 527 | |
| 528 | /* Prefer a group with connected clients */ |
| 529 | if (p2p_get_group_num_members(wpa_s->p2p_group)) |
| 530 | return wpa_s; |
| 531 | save = wpa_s; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* No group with connected clients, so pick the one without (if any) */ |
| 536 | return save; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* Find an active P2P group where we are the GO */ |
| 541 | static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s, |
| 542 | u8 *bssid) |
| 543 | { |
| 544 | struct wpa_ssid *s, *empty = NULL; |
| 545 | |
| 546 | if (!wpa_s) |
| 547 | return 0; |
| 548 | |
| 549 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 550 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 551 | if (s->disabled || !s->p2p_group || |
| 552 | s->mode != WPAS_MODE_P2P_GO) |
| 553 | continue; |
| 554 | |
| 555 | os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN); |
| 556 | if (p2p_get_group_num_members(wpa_s->p2p_group)) |
| 557 | return s; |
| 558 | empty = s; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return empty; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /* Find a persistent group where we are the GO */ |
| 567 | static struct wpa_ssid * |
| 568 | wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s) |
| 569 | { |
| 570 | struct wpa_ssid *s; |
| 571 | |
| 572 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 573 | if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO) |
| 574 | return s; |
| 575 | } |
| 576 | |
| 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role) |
| 582 | { |
| 583 | struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s; |
| 584 | struct wpa_ssid *s; |
| 585 | u8 conncap = P2PS_SETUP_NONE; |
| 586 | unsigned int owned_members = 0; |
| 587 | unsigned int owner = 0; |
| 588 | unsigned int client = 0; |
| 589 | struct wpa_supplicant *go_wpa_s; |
| 590 | struct wpa_ssid *persistent_go; |
| 591 | int p2p_no_group_iface; |
| 592 | |
| 593 | wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role); |
| 594 | |
| 595 | /* |
| 596 | * For non-concurrent capable devices: |
| 597 | * If persistent_go, then no new. |
| 598 | * If GO, then no client. |
| 599 | * If client, then no GO. |
| 600 | */ |
| 601 | go_wpa_s = wpas_p2p_get_go_group(wpa_s); |
| 602 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); |
| 603 | p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface; |
| 604 | |
| 605 | wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p", |
| 606 | go_wpa_s, persistent_go); |
| 607 | |
| 608 | for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s; |
| 609 | tmp_wpa_s = tmp_wpa_s->next) { |
| 610 | for (s = tmp_wpa_s->conf->ssid; s; s = s->next) { |
| 611 | wpa_printf(MSG_DEBUG, |
| 612 | "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d", |
| 613 | tmp_wpa_s, s, s->disabled, |
| 614 | s->p2p_group, s->mode); |
| 615 | if (!s->disabled && s->p2p_group) { |
| 616 | if (s->mode == WPAS_MODE_P2P_GO) { |
| 617 | owned_members += |
| 618 | p2p_get_group_num_members( |
| 619 | tmp_wpa_s->p2p_group); |
| 620 | owner++; |
| 621 | } else |
| 622 | client++; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* If not concurrent, restrict our choices */ |
| 628 | if (p2p_no_group_iface) { |
| 629 | wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface"); |
| 630 | |
| 631 | if (client) |
| 632 | return P2PS_SETUP_NONE; |
| 633 | |
| 634 | if (go_wpa_s) { |
| 635 | if (role == P2PS_SETUP_CLIENT || |
| 636 | incoming == P2PS_SETUP_GROUP_OWNER || |
| 637 | p2p_client_limit_reached(go_wpa_s->p2p_group)) |
| 638 | return P2PS_SETUP_NONE; |
| 639 | |
| 640 | return P2PS_SETUP_GROUP_OWNER; |
| 641 | } |
| 642 | |
| 643 | if (persistent_go) { |
| 644 | if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) { |
| 645 | if (!incoming) |
| 646 | return P2PS_SETUP_GROUP_OWNER | |
| 647 | P2PS_SETUP_CLIENT; |
| 648 | if (incoming == P2PS_SETUP_NEW) { |
| 649 | u8 r; |
| 650 | |
| 651 | if (os_get_random(&r, sizeof(r)) < 0 || |
| 652 | (r & 1)) |
| 653 | return P2PS_SETUP_CLIENT; |
| 654 | return P2PS_SETUP_GROUP_OWNER; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /* If a required role has been specified, handle it here */ |
| 661 | if (role && role != P2PS_SETUP_NEW) { |
| 662 | switch (incoming) { |
| 663 | case P2PS_SETUP_NONE: |
| 664 | case P2PS_SETUP_NEW: |
| 665 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: |
| 666 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: |
| 667 | conncap = role; |
| 668 | goto grp_owner; |
| 669 | |
| 670 | case P2PS_SETUP_GROUP_OWNER: |
| 671 | /* |
| 672 | * Must be a complimentary role - cannot be a client to |
| 673 | * more than one peer. |
| 674 | */ |
| 675 | if (incoming == role || client) |
| 676 | return P2PS_SETUP_NONE; |
| 677 | |
| 678 | return P2PS_SETUP_CLIENT; |
| 679 | |
| 680 | case P2PS_SETUP_CLIENT: |
| 681 | /* Must be a complimentary role */ |
| 682 | if (incoming != role) { |
| 683 | conncap = P2PS_SETUP_GROUP_OWNER; |
| 684 | goto grp_owner; |
| 685 | } |
| 686 | |
| 687 | default: |
| 688 | return P2PS_SETUP_NONE; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * For now, we only will support ownership of one group, and being a |
| 694 | * client of one group. Therefore, if we have either an existing GO |
| 695 | * group, or an existing client group, we will not do a new GO |
| 696 | * negotiation, but rather try to re-use the existing groups. |
| 697 | */ |
| 698 | switch (incoming) { |
| 699 | case P2PS_SETUP_NONE: |
| 700 | case P2PS_SETUP_NEW: |
| 701 | if (client) |
| 702 | conncap = P2PS_SETUP_GROUP_OWNER; |
| 703 | else if (!owned_members) |
| 704 | conncap = P2PS_SETUP_NEW; |
| 705 | else if (incoming == P2PS_SETUP_NONE) |
| 706 | conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT; |
| 707 | else |
| 708 | conncap = P2PS_SETUP_CLIENT; |
| 709 | break; |
| 710 | |
| 711 | case P2PS_SETUP_CLIENT: |
| 712 | conncap = P2PS_SETUP_GROUP_OWNER; |
| 713 | break; |
| 714 | |
| 715 | case P2PS_SETUP_GROUP_OWNER: |
| 716 | if (!client) |
| 717 | conncap = P2PS_SETUP_CLIENT; |
| 718 | break; |
| 719 | |
| 720 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: |
| 721 | case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: |
| 722 | if (client) |
| 723 | conncap = P2PS_SETUP_GROUP_OWNER; |
| 724 | else { |
| 725 | u8 r; |
| 726 | |
| 727 | if (os_get_random(&r, sizeof(r)) < 0 || |
| 728 | (r & 1)) |
| 729 | conncap = P2PS_SETUP_CLIENT; |
| 730 | else |
| 731 | conncap = P2PS_SETUP_GROUP_OWNER; |
| 732 | } |
| 733 | break; |
| 734 | |
| 735 | default: |
| 736 | return P2PS_SETUP_NONE; |
| 737 | } |
| 738 | |
| 739 | grp_owner: |
| 740 | if ((conncap & P2PS_SETUP_GROUP_OWNER) || |
| 741 | (!incoming && (conncap & P2PS_SETUP_NEW))) { |
| 742 | if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group)) |
| 743 | conncap &= ~P2PS_SETUP_GROUP_OWNER; |
| 744 | wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d", |
| 745 | owner, owned_members, conncap); |
| 746 | |
| 747 | s = wpas_p2p_get_persistent_go(wpa_s); |
| 748 | |
| 749 | if (!s && !owner && p2p_no_group_iface) { |
| 750 | p2p_set_intended_addr(wpa_s->global->p2p, |
| 751 | wpa_s->own_addr); |
| 752 | } else if (!s && !owner) { |
| 753 | if (wpas_p2p_add_group_interface(wpa_s, |
| 754 | WPA_IF_P2P_GO) < 0) { |
| 755 | wpa_printf(MSG_ERROR, |
| 756 | "P2P: Failed to allocate a new interface for the group"); |
| 757 | return P2PS_SETUP_NONE; |
| 758 | } |
| 759 | wpa_s->global->pending_group_iface_for_p2ps = 1; |
| 760 | p2p_set_intended_addr(wpa_s->global->p2p, |
| 761 | wpa_s->pending_interface_addr); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | return conncap; |
| 766 | } |
| 767 | |
| 768 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 769 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, |
| 770 | enum p2p_group_removal_reason removal_reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 771 | { |
| 772 | struct wpa_ssid *ssid; |
| 773 | char *gtype; |
| 774 | const char *reason; |
| 775 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 776 | ssid = wpa_s->current_ssid; |
| 777 | if (ssid == NULL) { |
| 778 | /* |
| 779 | * The current SSID was not known, but there may still be a |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 780 | * pending P2P group interface waiting for provisioning or a |
| 781 | * P2P group that is trying to reconnect. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 782 | */ |
| 783 | ssid = wpa_s->conf->ssid; |
| 784 | while (ssid) { |
Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 785 | if (ssid->p2p_group && ssid->disabled != 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 786 | break; |
| 787 | ssid = ssid->next; |
| 788 | } |
Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 789 | if (ssid == NULL && |
| 790 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) |
| 791 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 792 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " |
| 793 | "not found"); |
| 794 | return -1; |
| 795 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 796 | } |
| 797 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) |
| 798 | gtype = "GO"; |
| 799 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || |
| 800 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { |
| 801 | wpa_s->reassociate = 0; |
| 802 | wpa_s->disconnected = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 803 | gtype = "client"; |
| 804 | } else |
| 805 | gtype = "GO"; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 806 | |
| 807 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) |
| 808 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); |
| 809 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 810 | if (os_strcmp(gtype, "client") == 0) { |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 811 | wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 812 | if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal, |
| 813 | wpa_s, NULL)) { |
| 814 | wpa_printf(MSG_DEBUG, |
| 815 | "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal"); |
| 816 | removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE; |
| 817 | eloop_cancel_timeout(wpas_p2p_psk_failure_removal, |
| 818 | wpa_s, NULL); |
| 819 | } |
| 820 | } |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 821 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 822 | if (wpa_s->cross_connect_in_use) { |
| 823 | wpa_s->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 824 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 825 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 826 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 827 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 828 | switch (removal_reason) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 829 | case P2P_GROUP_REMOVAL_REQUESTED: |
| 830 | reason = " reason=REQUESTED"; |
| 831 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 832 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: |
| 833 | reason = " reason=FORMATION_FAILED"; |
| 834 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 835 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: |
| 836 | reason = " reason=IDLE"; |
| 837 | break; |
| 838 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
| 839 | reason = " reason=UNAVAILABLE"; |
| 840 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 841 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
| 842 | reason = " reason=GO_ENDING_SESSION"; |
| 843 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 844 | case P2P_GROUP_REMOVAL_PSK_FAILURE: |
| 845 | reason = " reason=PSK_FAILURE"; |
| 846 | break; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 847 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: |
| 848 | reason = " reason=FREQ_CONFLICT"; |
| 849 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 850 | default: |
| 851 | reason = ""; |
| 852 | break; |
| 853 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 854 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 855 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 856 | P2P_EVENT_GROUP_REMOVED "%s %s%s", |
| 857 | wpa_s->ifname, gtype, reason); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 858 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 859 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 860 | if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) |
| 861 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 862 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 863 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 864 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 865 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 866 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " |
| 867 | "timeout"); |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 868 | wpa_s->p2p_in_provisioning = 0; |
| 869 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 870 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 871 | wpa_s->p2p_in_invitation = 0; |
| 872 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Make sure wait for the first client does not remain active after the |
| 875 | * group has been removed. |
| 876 | */ |
| 877 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 878 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 879 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 880 | struct wpa_global *global; |
| 881 | char *ifname; |
| 882 | enum wpa_driver_if_type type; |
| 883 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", |
| 884 | wpa_s->ifname); |
| 885 | global = wpa_s->global; |
| 886 | ifname = os_strdup(wpa_s->ifname); |
| 887 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 888 | eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL); |
Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 889 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 890 | wpa_s = global->ifaces; |
| 891 | if (wpa_s && ifname) |
| 892 | wpa_drv_if_remove(wpa_s, type, ifname); |
| 893 | os_free(ifname); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 894 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 897 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 898 | wpa_s->global->p2p_group_formation = NULL; |
| 899 | wpa_s->p2p_in_provisioning = 0; |
| 900 | } |
| 901 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 902 | wpa_s->show_group_started = 0; |
| 903 | os_free(wpa_s->go_params); |
| 904 | wpa_s->go_params = NULL; |
| 905 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 906 | os_free(wpa_s->p2p_group_common_freqs); |
| 907 | wpa_s->p2p_group_common_freqs = NULL; |
| 908 | wpa_s->p2p_group_common_freqs_num = 0; |
| 909 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 910 | wpa_s->waiting_presence_resp = 0; |
| 911 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 912 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); |
| 913 | if (ssid && (ssid->p2p_group || |
| 914 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || |
| 915 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { |
| 916 | int id = ssid->id; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 917 | if (ssid == wpa_s->current_ssid) { |
| 918 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 919 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 920 | wpa_s->current_ssid = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 921 | } |
| 922 | /* |
| 923 | * Networks objects created during any P2P activities are not |
| 924 | * exposed out as they might/will confuse certain non-P2P aware |
| 925 | * applications since these network objects won't behave like |
| 926 | * regular ones. |
| 927 | * |
| 928 | * Likewise, we don't send out network removed signals for such |
| 929 | * network objects. |
| 930 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 931 | wpa_config_remove_network(wpa_s->conf, id); |
| 932 | wpa_supplicant_clear_status(wpa_s); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 933 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 934 | } else { |
| 935 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " |
| 936 | "found"); |
| 937 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 938 | if (wpa_s->ap_iface) |
| 939 | wpa_supplicant_ap_deinit(wpa_s); |
| 940 | else |
| 941 | wpa_drv_deinit_p2p_cli(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 942 | |
| 943 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | |
| 947 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, |
| 948 | u8 *go_dev_addr, |
| 949 | const u8 *ssid, size_t ssid_len) |
| 950 | { |
| 951 | struct wpa_bss *bss; |
| 952 | const u8 *bssid; |
| 953 | struct wpabuf *p2p; |
| 954 | u8 group_capab; |
| 955 | const u8 *addr; |
| 956 | |
| 957 | if (wpa_s->go_params) |
| 958 | bssid = wpa_s->go_params->peer_interface_addr; |
| 959 | else |
| 960 | bssid = wpa_s->bssid; |
| 961 | |
| 962 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 963 | if (bss == NULL && wpa_s->go_params && |
| 964 | !is_zero_ether_addr(wpa_s->go_params->peer_device_addr)) |
| 965 | bss = wpa_bss_get_p2p_dev_addr( |
| 966 | wpa_s, wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 967 | if (bss == NULL) { |
| 968 | u8 iface_addr[ETH_ALEN]; |
| 969 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, |
| 970 | iface_addr) == 0) |
| 971 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); |
| 972 | } |
| 973 | if (bss == NULL) { |
| 974 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 975 | "group is persistent - BSS " MACSTR " not found", |
| 976 | MAC2STR(bssid)); |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 981 | if (p2p == NULL) |
| 982 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, |
| 983 | P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 984 | if (p2p == NULL) { |
| 985 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 986 | "group is persistent - BSS " MACSTR |
| 987 | " did not include P2P IE", MAC2STR(bssid)); |
| 988 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", |
| 989 | (u8 *) (bss + 1), bss->ie_len); |
| 990 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", |
| 991 | ((u8 *) bss + 1) + bss->ie_len, |
| 992 | bss->beacon_ie_len); |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | group_capab = p2p_get_group_capab(p2p); |
| 997 | addr = p2p_get_go_dev_addr(p2p); |
| 998 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " |
| 999 | "group_capab=0x%x", group_capab); |
| 1000 | if (addr) { |
| 1001 | os_memcpy(go_dev_addr, addr, ETH_ALEN); |
| 1002 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, |
| 1003 | MAC2STR(addr)); |
| 1004 | } else |
| 1005 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 1006 | wpabuf_free(p2p); |
| 1007 | |
| 1008 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " |
| 1009 | "go_dev_addr=" MACSTR, |
| 1010 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); |
| 1011 | |
| 1012 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; |
| 1013 | } |
| 1014 | |
| 1015 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1016 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
| 1017 | struct wpa_ssid *ssid, |
| 1018 | const u8 *go_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1019 | { |
| 1020 | struct wpa_ssid *s; |
| 1021 | int changed = 0; |
| 1022 | |
| 1023 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " |
| 1024 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); |
| 1025 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 1026 | if (s->disabled == 2 && |
| 1027 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && |
| 1028 | s->ssid_len == ssid->ssid_len && |
| 1029 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | if (s) { |
| 1034 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " |
| 1035 | "entry"); |
| 1036 | if (ssid->passphrase && !s->passphrase) |
| 1037 | changed = 1; |
| 1038 | else if (ssid->passphrase && s->passphrase && |
| 1039 | os_strcmp(ssid->passphrase, s->passphrase) != 0) |
| 1040 | changed = 1; |
| 1041 | } else { |
| 1042 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " |
| 1043 | "entry"); |
| 1044 | changed = 1; |
| 1045 | s = wpa_config_add_network(wpa_s->conf); |
| 1046 | if (s == NULL) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1047 | return -1; |
| 1048 | |
| 1049 | /* |
| 1050 | * Instead of network_added we emit persistent_group_added |
| 1051 | * notification. Also to keep the defense checks in |
| 1052 | * persistent_group obj registration method, we set the |
| 1053 | * relevant flags in s to designate it as a persistent group. |
| 1054 | */ |
| 1055 | s->p2p_group = 1; |
| 1056 | s->p2p_persistent_group = 1; |
| 1057 | wpas_notify_persistent_group_added(wpa_s, s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1058 | wpa_config_set_network_defaults(s); |
| 1059 | } |
| 1060 | |
| 1061 | s->p2p_group = 1; |
| 1062 | s->p2p_persistent_group = 1; |
| 1063 | s->disabled = 2; |
| 1064 | s->bssid_set = 1; |
| 1065 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); |
| 1066 | s->mode = ssid->mode; |
| 1067 | s->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1068 | s->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1069 | s->proto = WPA_PROTO_RSN; |
| 1070 | s->pairwise_cipher = WPA_CIPHER_CCMP; |
| 1071 | s->export_keys = 1; |
| 1072 | if (ssid->passphrase) { |
| 1073 | os_free(s->passphrase); |
| 1074 | s->passphrase = os_strdup(ssid->passphrase); |
| 1075 | } |
| 1076 | if (ssid->psk_set) { |
| 1077 | s->psk_set = 1; |
| 1078 | os_memcpy(s->psk, ssid->psk, 32); |
| 1079 | } |
| 1080 | if (s->passphrase && !s->psk_set) |
| 1081 | wpa_config_update_psk(s); |
| 1082 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { |
| 1083 | os_free(s->ssid); |
| 1084 | s->ssid = os_malloc(ssid->ssid_len); |
| 1085 | } |
| 1086 | if (s->ssid) { |
| 1087 | s->ssid_len = ssid->ssid_len; |
| 1088 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); |
| 1089 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1090 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { |
| 1091 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); |
| 1092 | wpa_s->global->add_psk = NULL; |
| 1093 | changed = 1; |
| 1094 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1095 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1096 | if (changed && wpa_s->conf->update_config && |
| 1097 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { |
| 1098 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 1099 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1100 | |
| 1101 | return s->id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1105 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
| 1106 | const u8 *addr) |
| 1107 | { |
| 1108 | struct wpa_ssid *ssid, *s; |
| 1109 | u8 *n; |
| 1110 | size_t i; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1111 | int found = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1112 | |
| 1113 | ssid = wpa_s->current_ssid; |
| 1114 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 1115 | !ssid->p2p_persistent_group) |
| 1116 | return; |
| 1117 | |
| 1118 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 1119 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 1120 | continue; |
| 1121 | |
| 1122 | if (s->ssid_len == ssid->ssid_len && |
| 1123 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (s == NULL) |
| 1128 | return; |
| 1129 | |
| 1130 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1131 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1132 | ETH_ALEN) != 0) |
| 1133 | continue; |
| 1134 | |
| 1135 | if (i == s->num_p2p_clients - 1) |
| 1136 | return; /* already the most recent entry */ |
| 1137 | |
| 1138 | /* move the entry to mark it most recent */ |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1139 | os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, |
| 1140 | s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, |
| 1141 | (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1142 | os_memcpy(s->p2p_client_list + |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1143 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr, |
| 1144 | ETH_ALEN); |
| 1145 | os_memset(s->p2p_client_list + |
| 1146 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, |
| 1147 | 0xff, ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1148 | found = 1; |
| 1149 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1152 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { |
| 1153 | n = os_realloc_array(s->p2p_client_list, |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1154 | s->num_p2p_clients + 1, 2 * ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1155 | if (n == NULL) |
| 1156 | return; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1157 | os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr, |
| 1158 | ETH_ALEN); |
| 1159 | os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN, |
| 1160 | 0xff, ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1161 | s->p2p_client_list = n; |
| 1162 | s->num_p2p_clients++; |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 1163 | } else if (!found && s->p2p_client_list) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1164 | /* Not enough room for an additional entry - drop the oldest |
| 1165 | * entry */ |
| 1166 | os_memmove(s->p2p_client_list, |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1167 | s->p2p_client_list + 2 * ETH_ALEN, |
| 1168 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1169 | os_memcpy(s->p2p_client_list + |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1170 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1171 | addr, ETH_ALEN); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1172 | os_memset(s->p2p_client_list + |
| 1173 | (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, |
| 1174 | 0xff, ETH_ALEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1175 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1176 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1177 | if (wpa_s->parent->conf->update_config && |
| 1178 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 1179 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1183 | static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s, |
| 1184 | int go, struct wpa_ssid *ssid, int freq, |
| 1185 | const u8 *psk, const char *passphrase, |
| 1186 | const u8 *go_dev_addr, int persistent, |
| 1187 | const char *extra) |
| 1188 | { |
| 1189 | const char *ssid_txt; |
| 1190 | char psk_txt[65]; |
| 1191 | |
| 1192 | if (psk) |
| 1193 | wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32); |
| 1194 | else |
| 1195 | psk_txt[0] = '\0'; |
| 1196 | |
| 1197 | if (ssid) |
| 1198 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 1199 | else |
| 1200 | ssid_txt = ""; |
| 1201 | |
| 1202 | if (passphrase && passphrase[0] == '\0') |
| 1203 | passphrase = NULL; |
| 1204 | |
| 1205 | /* |
| 1206 | * Include PSK/passphrase only in the control interface message and |
| 1207 | * leave it out from the debug log entry. |
| 1208 | */ |
| 1209 | wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO, |
| 1210 | P2P_EVENT_GROUP_STARTED |
| 1211 | "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr=" |
| 1212 | MACSTR "%s%s", |
| 1213 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, |
| 1214 | psk ? " psk=" : "", psk_txt, |
| 1215 | passphrase ? " passphrase=\"" : "", |
| 1216 | passphrase ? passphrase : "", |
| 1217 | passphrase ? "\"" : "", |
| 1218 | MAC2STR(go_dev_addr), |
| 1219 | persistent ? " [PERSISTENT]" : "", extra); |
| 1220 | wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 1221 | "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s", |
| 1222 | wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, |
| 1223 | MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "", |
| 1224 | extra); |
| 1225 | } |
| 1226 | |
| 1227 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1228 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
| 1229 | int success) |
| 1230 | { |
| 1231 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1232 | int client; |
| 1233 | int persistent; |
| 1234 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1235 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1236 | |
| 1237 | /* |
| 1238 | * This callback is likely called for the main interface. Update wpa_s |
| 1239 | * to use the group interface if a new interface was created for the |
| 1240 | * group. |
| 1241 | */ |
| 1242 | if (wpa_s->global->p2p_group_formation) |
| 1243 | wpa_s = wpa_s->global->p2p_group_formation; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1244 | if (wpa_s->p2p_go_group_formation_completed) { |
| 1245 | wpa_s->global->p2p_group_formation = NULL; |
| 1246 | wpa_s->p2p_in_provisioning = 0; |
| 1247 | } |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 1248 | wpa_s->p2p_in_invitation = 0; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1249 | wpa_s->group_formation_reported = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1250 | |
| 1251 | if (!success) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1252 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1253 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1254 | wpas_p2p_group_delete(wpa_s, |
| 1255 | P2P_GROUP_REMOVAL_FORMATION_FAILED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1256 | return; |
| 1257 | } |
| 1258 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1259 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1260 | P2P_EVENT_GROUP_FORMATION_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1261 | |
| 1262 | ssid = wpa_s->current_ssid; |
| 1263 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 1264 | ssid->mode = WPAS_MODE_P2P_GO; |
| 1265 | p2p_group_notif_formation_done(wpa_s->p2p_group); |
| 1266 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); |
| 1267 | } |
| 1268 | |
| 1269 | persistent = 0; |
| 1270 | if (ssid) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1271 | client = ssid->mode == WPAS_MODE_INFRA; |
| 1272 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 1273 | persistent = ssid->p2p_persistent_group; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1274 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
| 1275 | ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1276 | } else |
| 1277 | persistent = wpas_p2p_persistent_group(wpa_s, |
| 1278 | go_dev_addr, |
| 1279 | ssid->ssid, |
| 1280 | ssid->ssid_len); |
| 1281 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1282 | client = wpa_s->p2p_group_interface == |
| 1283 | P2P_GROUP_INTERFACE_CLIENT; |
| 1284 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 1285 | } |
| 1286 | |
| 1287 | wpa_s->show_group_started = 0; |
| 1288 | if (client) { |
| 1289 | /* |
| 1290 | * Indicate event only after successfully completed 4-way |
| 1291 | * handshake, i.e., when the interface is ready for data |
| 1292 | * packets. |
| 1293 | */ |
| 1294 | wpa_s->show_group_started = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1295 | } else { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1296 | wpas_p2p_group_started(wpa_s, 1, ssid, |
| 1297 | ssid ? ssid->frequency : 0, |
| 1298 | ssid && ssid->passphrase == NULL && |
| 1299 | ssid->psk_set ? ssid->psk : NULL, |
| 1300 | ssid ? ssid->passphrase : NULL, |
| 1301 | go_dev_addr, persistent, ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1302 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1303 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 1304 | } |
| 1305 | |
| 1306 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1307 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 1308 | ssid, go_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1309 | else { |
| 1310 | os_free(wpa_s->global->add_psk); |
| 1311 | wpa_s->global->add_psk = NULL; |
| 1312 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1313 | if (network_id < 0 && ssid) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1314 | network_id = ssid->id; |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 1315 | if (!client) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1316 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1317 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 1318 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1322 | struct send_action_work { |
| 1323 | unsigned int freq; |
| 1324 | u8 dst[ETH_ALEN]; |
| 1325 | u8 src[ETH_ALEN]; |
| 1326 | u8 bssid[ETH_ALEN]; |
| 1327 | size_t len; |
| 1328 | unsigned int wait_time; |
| 1329 | u8 buf[0]; |
| 1330 | }; |
| 1331 | |
| 1332 | |
| 1333 | static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, |
| 1334 | void *timeout_ctx) |
| 1335 | { |
| 1336 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 1337 | |
| 1338 | if (!wpa_s->p2p_send_action_work) |
| 1339 | return; |
| 1340 | |
| 1341 | wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); |
| 1342 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 1343 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1344 | wpa_s->p2p_send_action_work = NULL; |
| 1345 | } |
| 1346 | |
| 1347 | |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1348 | static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1349 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1350 | if (wpa_s->p2p_send_action_work) { |
| 1351 | struct send_action_work *awork; |
| 1352 | awork = wpa_s->p2p_send_action_work->ctx; |
| 1353 | if (awork->wait_time == 0) { |
| 1354 | os_free(awork); |
| 1355 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1356 | wpa_s->p2p_send_action_work = NULL; |
| 1357 | } else { |
| 1358 | /* |
| 1359 | * In theory, this should not be needed, but number of |
| 1360 | * places in the P2P code is still using non-zero wait |
| 1361 | * time for the last Action frame in the sequence and |
| 1362 | * some of these do not call send_action_done(). |
| 1363 | */ |
| 1364 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1365 | wpa_s, NULL); |
| 1366 | eloop_register_timeout( |
| 1367 | 0, awork->wait_time * 1000, |
| 1368 | wpas_p2p_send_action_work_timeout, |
| 1369 | wpa_s, NULL); |
| 1370 | } |
| 1371 | } |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | |
| 1375 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
| 1376 | unsigned int freq, |
| 1377 | const u8 *dst, const u8 *src, |
| 1378 | const u8 *bssid, |
| 1379 | const u8 *data, size_t data_len, |
| 1380 | enum offchannel_send_action_result |
| 1381 | result) |
| 1382 | { |
| 1383 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
| 1384 | |
| 1385 | wpas_p2p_action_tx_clear(wpa_s); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1386 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1387 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 1388 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1389 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1390 | switch (result) { |
| 1391 | case OFFCHANNEL_SEND_ACTION_SUCCESS: |
| 1392 | res = P2P_SEND_ACTION_SUCCESS; |
| 1393 | break; |
| 1394 | case OFFCHANNEL_SEND_ACTION_NO_ACK: |
| 1395 | res = P2P_SEND_ACTION_NO_ACK; |
| 1396 | break; |
| 1397 | case OFFCHANNEL_SEND_ACTION_FAILED: |
| 1398 | res = P2P_SEND_ACTION_FAILED; |
| 1399 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1402 | 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] | 1403 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1404 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
| 1405 | wpa_s->pending_pd_before_join && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1406 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1407 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && |
| 1408 | wpa_s->p2p_fallback_to_go_neg) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1409 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1410 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " |
| 1411 | "during p2p_connect-auto"); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1412 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1413 | P2P_EVENT_FALLBACK_TO_GO_NEG |
| 1414 | "reason=no-ACK-to-PD-Req"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1415 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 1416 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1421 | static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) |
| 1422 | { |
| 1423 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1424 | struct send_action_work *awork = work->ctx; |
| 1425 | |
| 1426 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 1427 | if (work->started) { |
| 1428 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1429 | wpa_s, NULL); |
| 1430 | wpa_s->p2p_send_action_work = NULL; |
| 1431 | offchannel_send_action_done(wpa_s); |
| 1432 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1433 | os_free(awork); |
| 1434 | return; |
| 1435 | } |
| 1436 | |
| 1437 | if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, |
| 1438 | awork->bssid, awork->buf, awork->len, |
| 1439 | awork->wait_time, |
| 1440 | wpas_p2p_send_action_tx_status, 1) < 0) { |
| 1441 | os_free(awork); |
| 1442 | radio_work_done(work); |
| 1443 | return; |
| 1444 | } |
| 1445 | wpa_s->p2p_send_action_work = work; |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | static int wpas_send_action_work(struct wpa_supplicant *wpa_s, |
| 1450 | unsigned int freq, const u8 *dst, |
| 1451 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1452 | size_t len, unsigned int wait_time) |
| 1453 | { |
| 1454 | struct send_action_work *awork; |
| 1455 | |
| 1456 | if (wpa_s->p2p_send_action_work) { |
| 1457 | wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | |
| 1461 | awork = os_zalloc(sizeof(*awork) + len); |
| 1462 | if (awork == NULL) |
| 1463 | return -1; |
| 1464 | |
| 1465 | awork->freq = freq; |
| 1466 | os_memcpy(awork->dst, dst, ETH_ALEN); |
| 1467 | os_memcpy(awork->src, src, ETH_ALEN); |
| 1468 | os_memcpy(awork->bssid, bssid, ETH_ALEN); |
| 1469 | awork->len = len; |
| 1470 | awork->wait_time = wait_time; |
| 1471 | os_memcpy(awork->buf, buf, len); |
| 1472 | |
| 1473 | if (radio_add_work(wpa_s, freq, "p2p-send-action", 0, |
| 1474 | wpas_send_action_cb, awork) < 0) { |
| 1475 | os_free(awork); |
| 1476 | return -1; |
| 1477 | } |
| 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1483 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, |
| 1484 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1485 | size_t len, unsigned int wait_time) |
| 1486 | { |
| 1487 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1488 | int listen_freq = -1, send_freq = -1; |
| 1489 | |
| 1490 | if (wpa_s->p2p_listen_work) |
| 1491 | listen_freq = wpa_s->p2p_listen_work->freq; |
| 1492 | if (wpa_s->p2p_send_action_work) |
| 1493 | send_freq = wpa_s->p2p_send_action_work->freq; |
| 1494 | if (listen_freq != (int) freq && send_freq != (int) freq) { |
| 1495 | wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)", |
| 1496 | listen_freq, send_freq); |
| 1497 | return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, |
| 1498 | len, wait_time); |
| 1499 | } |
| 1500 | |
| 1501 | 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] | 1502 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
| 1503 | wait_time, |
| 1504 | wpas_p2p_send_action_tx_status, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | static void wpas_send_action_done(void *ctx) |
| 1509 | { |
| 1510 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1511 | |
| 1512 | if (wpa_s->p2p_send_action_work) { |
| 1513 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1514 | wpa_s, NULL); |
| 1515 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 1516 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1517 | wpa_s->p2p_send_action_work = NULL; |
| 1518 | } |
| 1519 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1520 | offchannel_send_action_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | |
| 1524 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, |
| 1525 | struct p2p_go_neg_results *params) |
| 1526 | { |
| 1527 | if (wpa_s->go_params == NULL) { |
| 1528 | wpa_s->go_params = os_malloc(sizeof(*params)); |
| 1529 | if (wpa_s->go_params == NULL) |
| 1530 | return -1; |
| 1531 | } |
| 1532 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, |
| 1538 | struct p2p_go_neg_results *res) |
| 1539 | { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1540 | wpa_s->group_formation_reported = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1541 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR |
| 1542 | " dev_addr " MACSTR " wps_method %d", |
| 1543 | MAC2STR(res->peer_interface_addr), |
| 1544 | MAC2STR(res->peer_device_addr), res->wps_method); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1545 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
| 1546 | res->ssid, res->ssid_len); |
| 1547 | wpa_supplicant_ap_deinit(wpa_s); |
| 1548 | wpas_copy_go_neg_results(wpa_s, res); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1549 | if (res->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1550 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1551 | #ifdef CONFIG_WPS_NFC |
| 1552 | } else if (res->wps_method == WPS_NFC) { |
| 1553 | wpas_wps_start_nfc(wpa_s, res->peer_device_addr, |
| 1554 | res->peer_interface_addr, |
| 1555 | wpa_s->parent->p2p_oob_dev_pw, |
| 1556 | wpa_s->parent->p2p_oob_dev_pw_id, 1, |
| 1557 | wpa_s->parent->p2p_oob_dev_pw_id == |
| 1558 | DEV_PW_NFC_CONNECTION_HANDOVER ? |
| 1559 | wpa_s->parent->p2p_peer_oob_pubkey_hash : |
| 1560 | NULL, |
| 1561 | NULL, 0, 0); |
| 1562 | #endif /* CONFIG_WPS_NFC */ |
| 1563 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1564 | u16 dev_pw_id = DEV_PW_DEFAULT; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1565 | if (wpa_s->p2p_wps_method == WPS_P2PS) |
| 1566 | dev_pw_id = DEV_PW_P2PS_DEFAULT; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1567 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) |
| 1568 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; |
| 1569 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
| 1570 | wpa_s->p2p_pin, 1, dev_pw_id); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1575 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, |
| 1576 | struct wpa_ssid *ssid) |
| 1577 | { |
| 1578 | struct wpa_ssid *persistent; |
| 1579 | struct psk_list_entry *psk; |
| 1580 | struct hostapd_data *hapd; |
| 1581 | |
| 1582 | if (!wpa_s->ap_iface) |
| 1583 | return; |
| 1584 | |
| 1585 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 1586 | ssid->ssid_len); |
| 1587 | if (persistent == NULL) |
| 1588 | return; |
| 1589 | |
| 1590 | hapd = wpa_s->ap_iface->bss[0]; |
| 1591 | |
| 1592 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, |
| 1593 | list) { |
| 1594 | struct hostapd_wpa_psk *hpsk; |
| 1595 | |
| 1596 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " |
| 1597 | MACSTR " psk=%d", |
| 1598 | MAC2STR(psk->addr), psk->p2p); |
| 1599 | hpsk = os_zalloc(sizeof(*hpsk)); |
| 1600 | if (hpsk == NULL) |
| 1601 | break; |
| 1602 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); |
| 1603 | if (psk->p2p) |
| 1604 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); |
| 1605 | else |
| 1606 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); |
| 1607 | hpsk->next = hapd->conf->ssid.wpa_psk; |
| 1608 | hapd->conf->ssid.wpa_psk = hpsk; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1613 | static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s) |
| 1614 | { |
| 1615 | unsigned int i; |
| 1616 | |
| 1617 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):", |
| 1618 | wpa_s->p2p_group_common_freqs_num); |
| 1619 | |
| 1620 | for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) |
| 1621 | wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", |
| 1622 | i, wpa_s->p2p_group_common_freqs[i]); |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s, |
| 1627 | struct p2p_go_neg_results *params) |
| 1628 | { |
| 1629 | unsigned int i, len = int_array_len(wpa_s->go_params->freq_list); |
| 1630 | |
| 1631 | wpa_s->p2p_group_common_freqs_num = 0; |
| 1632 | os_free(wpa_s->p2p_group_common_freqs); |
| 1633 | wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int)); |
| 1634 | if (!wpa_s->p2p_group_common_freqs) |
| 1635 | return; |
| 1636 | |
| 1637 | for (i = 0; i < len; i++) { |
| 1638 | if (!wpa_s->go_params->freq_list[i]) |
| 1639 | break; |
| 1640 | wpa_s->p2p_group_common_freqs[i] = |
| 1641 | wpa_s->go_params->freq_list[i]; |
| 1642 | } |
| 1643 | wpa_s->p2p_group_common_freqs_num = i; |
| 1644 | } |
| 1645 | |
| 1646 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1647 | static void p2p_config_write(struct wpa_supplicant *wpa_s) |
| 1648 | { |
| 1649 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 1650 | if (wpa_s->parent->conf->update_config && |
| 1651 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 1652 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 1653 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 1654 | } |
| 1655 | |
| 1656 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1657 | static void p2p_go_configured(void *ctx, void *data) |
| 1658 | { |
| 1659 | struct wpa_supplicant *wpa_s = ctx; |
| 1660 | struct p2p_go_neg_results *params = data; |
| 1661 | struct wpa_ssid *ssid; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1662 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1663 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1664 | p2p_go_save_group_common_freqs(wpa_s, params); |
| 1665 | p2p_go_dump_common_freqs(wpa_s); |
| 1666 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1667 | ssid = wpa_s->current_ssid; |
| 1668 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { |
| 1669 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); |
| 1670 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 1671 | wpa_s->global->p2p_group_formation = NULL; |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1672 | wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency, |
| 1673 | params->passphrase[0] == '\0' ? |
| 1674 | params->psk : NULL, |
| 1675 | params->passphrase, |
| 1676 | wpa_s->global->p2p_dev_addr, |
| 1677 | params->persistent_group, ""); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1678 | wpa_s->group_formation_reported = 1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1679 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1680 | if (wpa_s->parent->p2ps_join_addr_valid) { |
| 1681 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1682 | "P2PS: Setting default PIN for " MACSTR, |
| 1683 | MAC2STR(wpa_s->parent->p2ps_join_addr)); |
| 1684 | wpa_supplicant_ap_wps_pin(wpa_s, |
| 1685 | wpa_s->parent->p2ps_join_addr, |
| 1686 | "12345670", NULL, 0, 0); |
| 1687 | wpa_s->parent->p2ps_join_addr_valid = 0; |
| 1688 | } |
| 1689 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1690 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1691 | if (params->persistent_group) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1692 | network_id = wpas_p2p_store_persistent_group( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1693 | wpa_s->parent, ssid, |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1694 | wpa_s->global->p2p_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1695 | wpas_p2p_add_psk_list(wpa_s, ssid); |
| 1696 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1697 | if (network_id < 0) |
| 1698 | network_id = ssid->id; |
| 1699 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1700 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1701 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1702 | |
| 1703 | if (wpa_s->p2p_first_connection_timeout) { |
| 1704 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1705 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", |
| 1706 | wpa_s->p2p_first_connection_timeout); |
| 1707 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1708 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1709 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1710 | wpa_s->parent, NULL); |
| 1711 | eloop_register_timeout( |
| 1712 | wpa_s->p2p_first_connection_timeout, 0, |
| 1713 | wpas_p2p_group_formation_timeout, |
| 1714 | wpa_s->parent, NULL); |
| 1715 | } |
| 1716 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1717 | return; |
| 1718 | } |
| 1719 | |
| 1720 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); |
| 1721 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, |
| 1722 | params->peer_interface_addr)) { |
| 1723 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " |
| 1724 | "filtering"); |
| 1725 | return; |
| 1726 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1727 | if (params->wps_method == WPS_PBC) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1728 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1729 | params->peer_device_addr); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1730 | #ifdef CONFIG_WPS_NFC |
| 1731 | } else if (params->wps_method == WPS_NFC) { |
| 1732 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 1733 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 1734 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 1735 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 1736 | return; |
| 1737 | } |
| 1738 | wpas_ap_wps_add_nfc_pw( |
| 1739 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 1740 | wpa_s->parent->p2p_oob_dev_pw, |
| 1741 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 1742 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 1743 | #endif /* CONFIG_WPS_NFC */ |
| 1744 | } else if (wpa_s->p2p_pin[0]) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1745 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1746 | wpa_s->p2p_pin, NULL, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1747 | os_free(wpa_s->go_params); |
| 1748 | wpa_s->go_params = NULL; |
| 1749 | } |
| 1750 | |
| 1751 | |
| 1752 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, |
| 1753 | struct p2p_go_neg_results *params, |
| 1754 | int group_formation) |
| 1755 | { |
| 1756 | struct wpa_ssid *ssid; |
| 1757 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1758 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); |
| 1759 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { |
| 1760 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " |
| 1761 | "results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1762 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1763 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1764 | |
| 1765 | ssid = wpa_config_add_network(wpa_s->conf); |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1766 | if (ssid == NULL) { |
| 1767 | 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] | 1768 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1769 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1770 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1771 | wpa_s->show_group_started = 0; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1772 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1773 | wpa_s->group_formation_reported = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1774 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1775 | wpa_config_set_network_defaults(ssid); |
| 1776 | ssid->temporary = 1; |
| 1777 | ssid->p2p_group = 1; |
| 1778 | ssid->p2p_persistent_group = params->persistent_group; |
| 1779 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : |
| 1780 | WPAS_MODE_P2P_GO; |
| 1781 | ssid->frequency = params->freq; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1782 | ssid->ht40 = params->ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1783 | ssid->vht = params->vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1784 | ssid->ssid = os_zalloc(params->ssid_len + 1); |
| 1785 | if (ssid->ssid) { |
| 1786 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 1787 | ssid->ssid_len = params->ssid_len; |
| 1788 | } |
| 1789 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1790 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1791 | ssid->proto = WPA_PROTO_RSN; |
| 1792 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1793 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 1794 | if (params->freq > 56160) { |
| 1795 | /* |
| 1796 | * Enable GCMP instead of CCMP as pairwise_cipher and |
| 1797 | * group_cipher in 60 GHz. |
| 1798 | */ |
| 1799 | ssid->pairwise_cipher = WPA_CIPHER_GCMP; |
| 1800 | ssid->group_cipher = WPA_CIPHER_GCMP; |
| 1801 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1802 | if (os_strlen(params->passphrase) > 0) { |
| 1803 | ssid->passphrase = os_strdup(params->passphrase); |
| 1804 | if (ssid->passphrase == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1805 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 1806 | "P2P: Failed to copy passphrase for GO"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1807 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1808 | return; |
| 1809 | } |
| 1810 | } else |
| 1811 | ssid->passphrase = NULL; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1812 | ssid->psk_set = params->psk_set; |
| 1813 | if (ssid->psk_set) |
| 1814 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1815 | else if (ssid->passphrase) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1816 | wpa_config_update_psk(ssid); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1817 | ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1818 | |
| 1819 | wpa_s->ap_configured_cb = p2p_go_configured; |
| 1820 | wpa_s->ap_configured_cb_ctx = wpa_s; |
| 1821 | wpa_s->ap_configured_cb_data = wpa_s->go_params; |
Dmitry Shmidt | 6dc03bd | 2014-05-16 10:40:13 -0700 | [diff] [blame] | 1822 | wpa_s->scan_req = NORMAL_SCAN_REQ; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1823 | wpa_s->connect_without_scan = ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1824 | wpa_s->reassociate = 1; |
| 1825 | wpa_s->disconnected = 0; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1826 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " |
| 1827 | "start GO)"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1828 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, |
| 1833 | const struct wpa_supplicant *src) |
| 1834 | { |
| 1835 | struct wpa_config *d; |
| 1836 | const struct wpa_config *s; |
| 1837 | |
| 1838 | d = dst->conf; |
| 1839 | s = src->conf; |
| 1840 | |
| 1841 | #define C(n) if (s->n) d->n = os_strdup(s->n) |
| 1842 | C(device_name); |
| 1843 | C(manufacturer); |
| 1844 | C(model_name); |
| 1845 | C(model_number); |
| 1846 | C(serial_number); |
| 1847 | C(config_methods); |
| 1848 | #undef C |
| 1849 | |
| 1850 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
| 1851 | os_memcpy(d->sec_device_type, s->sec_device_type, |
| 1852 | sizeof(d->sec_device_type)); |
| 1853 | d->num_sec_device_types = s->num_sec_device_types; |
| 1854 | |
| 1855 | d->p2p_group_idle = s->p2p_group_idle; |
| 1856 | d->p2p_intra_bss = s->p2p_intra_bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1857 | d->persistent_reconnect = s->persistent_reconnect; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1858 | d->max_num_sta = s->max_num_sta; |
| 1859 | d->pbc_in_m1 = s->pbc_in_m1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1860 | d->ignore_old_scan_res = s->ignore_old_scan_res; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1861 | d->beacon_int = s->beacon_int; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 1862 | d->dtim_period = s->dtim_period; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1863 | d->p2p_go_ctwindow = s->p2p_go_ctwindow; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1864 | d->disassoc_low_ack = s->disassoc_low_ack; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1865 | d->disable_scan_offload = s->disable_scan_offload; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1866 | d->passive_scan = s->passive_scan; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1867 | |
| 1868 | if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) { |
| 1869 | d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey); |
| 1870 | d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey); |
| 1871 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1875 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, |
| 1876 | char *ifname, size_t len) |
| 1877 | { |
| 1878 | char *ifname_ptr = wpa_s->ifname; |
| 1879 | |
| 1880 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, |
| 1881 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 1882 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; |
| 1883 | } |
| 1884 | |
| 1885 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); |
| 1886 | if (os_strlen(ifname) >= IFNAMSIZ && |
| 1887 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1888 | int res; |
| 1889 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1890 | /* Try to avoid going over the IFNAMSIZ length limit */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1891 | res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); |
| 1892 | if (os_snprintf_error(len, res) && len) |
| 1893 | ifname[len - 1] = '\0'; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1898 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 1899 | enum wpa_driver_if_type type) |
| 1900 | { |
| 1901 | char ifname[120], force_ifname[120]; |
| 1902 | |
| 1903 | if (wpa_s->pending_interface_name[0]) { |
| 1904 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " |
| 1905 | "- skip creation of a new one"); |
| 1906 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { |
| 1907 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " |
| 1908 | "unknown?! ifname='%s'", |
| 1909 | wpa_s->pending_interface_name); |
| 1910 | return -1; |
| 1911 | } |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1915 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1916 | force_ifname[0] = '\0'; |
| 1917 | |
| 1918 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", |
| 1919 | ifname); |
| 1920 | wpa_s->p2p_group_idx++; |
| 1921 | |
| 1922 | wpa_s->pending_interface_type = type; |
| 1923 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, |
| 1924 | wpa_s->pending_interface_addr, NULL) < 0) { |
| 1925 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
| 1926 | "interface"); |
| 1927 | return -1; |
| 1928 | } |
| 1929 | |
| 1930 | if (force_ifname[0]) { |
| 1931 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", |
| 1932 | force_ifname); |
| 1933 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, |
| 1934 | sizeof(wpa_s->pending_interface_name)); |
| 1935 | } else |
| 1936 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 1937 | sizeof(wpa_s->pending_interface_name)); |
| 1938 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " |
| 1939 | MACSTR, wpa_s->pending_interface_name, |
| 1940 | MAC2STR(wpa_s->pending_interface_addr)); |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | |
| 1946 | static void wpas_p2p_remove_pending_group_interface( |
| 1947 | struct wpa_supplicant *wpa_s) |
| 1948 | { |
| 1949 | if (!wpa_s->pending_interface_name[0] || |
| 1950 | is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 1951 | return; /* No pending virtual interface */ |
| 1952 | |
| 1953 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", |
| 1954 | wpa_s->pending_interface_name); |
| 1955 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, |
| 1956 | wpa_s->pending_interface_name); |
| 1957 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1958 | wpa_s->pending_interface_name[0] = '\0'; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1959 | wpa_s->global->pending_group_iface_for_p2ps = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | static struct wpa_supplicant * |
| 1964 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) |
| 1965 | { |
| 1966 | struct wpa_interface iface; |
| 1967 | struct wpa_supplicant *group_wpa_s; |
| 1968 | |
| 1969 | if (!wpa_s->pending_interface_name[0]) { |
| 1970 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); |
| 1971 | if (!wpas_p2p_create_iface(wpa_s)) |
| 1972 | return NULL; |
| 1973 | /* |
| 1974 | * Something has forced us to remove the pending interface; try |
| 1975 | * to create a new one and hope for the best that we will get |
| 1976 | * the same local address. |
| 1977 | */ |
| 1978 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
| 1979 | WPA_IF_P2P_CLIENT) < 0) |
| 1980 | return NULL; |
| 1981 | } |
| 1982 | |
| 1983 | os_memset(&iface, 0, sizeof(iface)); |
| 1984 | iface.ifname = wpa_s->pending_interface_name; |
| 1985 | iface.driver = wpa_s->driver->name; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1986 | if (wpa_s->conf->ctrl_interface == NULL && |
| 1987 | wpa_s->parent != wpa_s && |
| 1988 | wpa_s->p2p_mgmt && |
| 1989 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) |
| 1990 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; |
| 1991 | else |
| 1992 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1993 | iface.driver_param = wpa_s->conf->driver_param; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1994 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1995 | if (group_wpa_s == NULL) { |
| 1996 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " |
| 1997 | "wpa_supplicant interface"); |
| 1998 | return NULL; |
| 1999 | } |
| 2000 | wpa_s->pending_interface_name[0] = '\0'; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2001 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : |
| 2002 | P2P_GROUP_INTERFACE_CLIENT; |
| 2003 | wpa_s->global->p2p_group_formation = group_wpa_s; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2004 | wpa_s->global->pending_group_iface_for_p2ps = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2005 | |
| 2006 | wpas_p2p_clone_config(group_wpa_s, wpa_s); |
| 2007 | |
| 2008 | return group_wpa_s; |
| 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 2013 | void *timeout_ctx) |
| 2014 | { |
| 2015 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 2016 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 2017 | wpas_p2p_group_formation_failed(wpa_s); |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s) |
| 2022 | { |
| 2023 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 2024 | wpa_s->parent, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2025 | if (wpa_s->global->p2p) |
| 2026 | p2p_group_formation_failed(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2027 | wpas_group_formation_completed(wpa_s, 0); |
| 2028 | } |
| 2029 | |
| 2030 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2031 | static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) |
| 2032 | { |
| 2033 | wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); |
| 2034 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 2035 | wpa_s->parent, NULL); |
| 2036 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 2037 | wpa_s->parent, NULL); |
| 2038 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 2039 | } |
| 2040 | |
| 2041 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2042 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) |
| 2043 | { |
| 2044 | if (wpa_s->global->p2p_group_formation != wpa_s) |
| 2045 | return; |
| 2046 | /* Speed up group formation timeout since this cannot succeed */ |
| 2047 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 2048 | wpa_s->parent, NULL); |
| 2049 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 2050 | wpa_s->parent, NULL); |
| 2051 | } |
| 2052 | |
| 2053 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2054 | 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] | 2055 | { |
| 2056 | struct wpa_supplicant *wpa_s = ctx; |
| 2057 | |
| 2058 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 2059 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 2060 | wpa_s->off_channel_freq = 0; |
| 2061 | wpa_s->roc_waiting_drv_freq = 0; |
| 2062 | } |
| 2063 | |
| 2064 | if (res->status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2065 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2066 | P2P_EVENT_GO_NEG_FAILURE "status=%d", |
| 2067 | res->status); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2068 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2069 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 2070 | return; |
| 2071 | } |
| 2072 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2073 | if (wpa_s->p2p_go_ht40) |
| 2074 | res->ht40 = 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2075 | if (wpa_s->p2p_go_vht) |
| 2076 | res->vht = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2077 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 2078 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " |
| 2079 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR |
| 2080 | " wps_method=%s", |
| 2081 | res->role_go ? "GO" : "client", res->freq, res->ht40, |
| 2082 | MAC2STR(res->peer_device_addr), |
| 2083 | MAC2STR(res->peer_interface_addr), |
| 2084 | p2p_wps_method_text(res->wps_method)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2085 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2086 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2087 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
| 2088 | struct wpa_ssid *ssid; |
| 2089 | ssid = wpa_config_get_network(wpa_s->conf, |
| 2090 | wpa_s->p2p_persistent_id); |
| 2091 | if (ssid && ssid->disabled == 2 && |
| 2092 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { |
| 2093 | size_t len = os_strlen(ssid->passphrase); |
| 2094 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " |
| 2095 | "on requested persistent group"); |
| 2096 | os_memcpy(res->passphrase, ssid->passphrase, len); |
| 2097 | res->passphrase[len] = '\0'; |
| 2098 | } |
| 2099 | } |
| 2100 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2101 | if (wpa_s->create_p2p_iface) { |
| 2102 | struct wpa_supplicant *group_wpa_s = |
| 2103 | wpas_p2p_init_group_interface(wpa_s, res->role_go); |
| 2104 | if (group_wpa_s == NULL) { |
| 2105 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 2106 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, |
| 2107 | wpa_s, NULL); |
| 2108 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2109 | return; |
| 2110 | } |
| 2111 | if (group_wpa_s != wpa_s) { |
| 2112 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
| 2113 | sizeof(group_wpa_s->p2p_pin)); |
| 2114 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
| 2115 | } |
| 2116 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 2117 | wpa_s->pending_interface_name[0] = '\0'; |
| 2118 | group_wpa_s->p2p_in_provisioning = 1; |
| 2119 | |
| 2120 | if (res->role_go) |
| 2121 | wpas_start_wps_go(group_wpa_s, res, 1); |
| 2122 | else |
| 2123 | wpas_start_wps_enrollee(group_wpa_s, res); |
| 2124 | } else { |
| 2125 | wpa_s->p2p_in_provisioning = 1; |
| 2126 | wpa_s->global->p2p_group_formation = wpa_s; |
| 2127 | |
| 2128 | if (res->role_go) |
| 2129 | wpas_start_wps_go(wpa_s, res, 1); |
| 2130 | else |
| 2131 | wpas_start_wps_enrollee(ctx, res); |
| 2132 | } |
| 2133 | |
| 2134 | wpa_s->p2p_long_listen = 0; |
| 2135 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 2136 | |
| 2137 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 2138 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
| 2139 | (res->peer_config_timeout % 100) * 10000, |
| 2140 | wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 2141 | } |
| 2142 | |
| 2143 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2144 | 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] | 2145 | { |
| 2146 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2147 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
| 2148 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2149 | |
| 2150 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); |
| 2151 | } |
| 2152 | |
| 2153 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2154 | static void wpas_dev_found(void *ctx, const u8 *addr, |
| 2155 | const struct p2p_peer_info *info, |
| 2156 | int new_device) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2157 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2158 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2159 | struct wpa_supplicant *wpa_s = ctx; |
| 2160 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2161 | char *wfd_dev_info_hex = NULL; |
| 2162 | |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 2163 | #ifdef CONFIG_WIFI_DISPLAY |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2164 | wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, |
| 2165 | WFD_SUBELEM_DEVICE_INFO); |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 2166 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2167 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2168 | if (info->p2ps_instance) { |
| 2169 | char str[256]; |
| 2170 | const u8 *buf = wpabuf_head(info->p2ps_instance); |
| 2171 | size_t len = wpabuf_len(info->p2ps_instance); |
| 2172 | |
| 2173 | while (len) { |
| 2174 | u32 id; |
| 2175 | u16 methods; |
| 2176 | u8 str_len; |
| 2177 | |
| 2178 | if (len < 4 + 2 + 1) |
| 2179 | break; |
| 2180 | id = WPA_GET_LE32(buf); |
| 2181 | buf += sizeof(u32); |
| 2182 | methods = WPA_GET_BE16(buf); |
| 2183 | buf += sizeof(u16); |
| 2184 | str_len = *buf++; |
| 2185 | if (str_len > len - 4 - 2 - 1) |
| 2186 | break; |
| 2187 | os_memcpy(str, buf, str_len); |
| 2188 | str[str_len] = '\0'; |
| 2189 | buf += str_len; |
| 2190 | len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8); |
| 2191 | |
| 2192 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2193 | P2P_EVENT_DEVICE_FOUND MACSTR |
| 2194 | " p2p_dev_addr=" MACSTR |
| 2195 | " pri_dev_type=%s name='%s'" |
| 2196 | " config_methods=0x%x" |
| 2197 | " dev_capab=0x%x" |
| 2198 | " group_capab=0x%x" |
| 2199 | " adv_id=%x asp_svc=%s%s", |
| 2200 | MAC2STR(addr), |
| 2201 | MAC2STR(info->p2p_device_addr), |
| 2202 | wps_dev_type_bin2str( |
| 2203 | info->pri_dev_type, |
| 2204 | devtype, sizeof(devtype)), |
| 2205 | info->device_name, methods, |
| 2206 | info->dev_capab, info->group_capab, |
| 2207 | id, str, |
| 2208 | info->vendor_elems ? |
| 2209 | " vendor_elems=1" : ""); |
| 2210 | } |
| 2211 | goto done; |
| 2212 | } |
| 2213 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2214 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
| 2215 | " p2p_dev_addr=" MACSTR |
| 2216 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2217 | "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d", |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2218 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
| 2219 | wps_dev_type_bin2str(info->pri_dev_type, devtype, |
| 2220 | sizeof(devtype)), |
| 2221 | info->device_name, info->config_methods, |
| 2222 | info->dev_capab, info->group_capab, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2223 | wfd_dev_info_hex ? " wfd_dev_info=0x" : "", |
Dmitry Shmidt | 2e67f06 | 2014-07-16 09:55:28 -0700 | [diff] [blame] | 2224 | wfd_dev_info_hex ? wfd_dev_info_hex : "", |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2225 | info->vendor_elems ? " vendor_elems=1" : "", |
| 2226 | new_device); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2227 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2228 | done: |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2229 | os_free(wfd_dev_info_hex); |
Dmitry Shmidt | 9767226 | 2014-02-03 13:02:54 -0800 | [diff] [blame] | 2230 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 2231 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2232 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); |
| 2233 | } |
| 2234 | |
| 2235 | |
| 2236 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
| 2237 | { |
| 2238 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2239 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2240 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
| 2241 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2242 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2243 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
| 2244 | } |
| 2245 | |
| 2246 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2247 | static void wpas_find_stopped(void *ctx) |
| 2248 | { |
| 2249 | struct wpa_supplicant *wpa_s = ctx; |
| 2250 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
| 2251 | } |
| 2252 | |
| 2253 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2254 | struct wpas_p2p_listen_work { |
| 2255 | unsigned int freq; |
| 2256 | unsigned int duration; |
| 2257 | struct wpabuf *probe_resp_ie; |
| 2258 | }; |
| 2259 | |
| 2260 | |
| 2261 | static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) |
| 2262 | { |
| 2263 | if (lwork == NULL) |
| 2264 | return; |
| 2265 | wpabuf_free(lwork->probe_resp_ie); |
| 2266 | os_free(lwork); |
| 2267 | } |
| 2268 | |
| 2269 | |
| 2270 | static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) |
| 2271 | { |
| 2272 | struct wpas_p2p_listen_work *lwork; |
| 2273 | |
| 2274 | if (!wpa_s->p2p_listen_work) |
| 2275 | return; |
| 2276 | |
| 2277 | lwork = wpa_s->p2p_listen_work->ctx; |
| 2278 | wpas_p2p_listen_work_free(lwork); |
| 2279 | radio_work_done(wpa_s->p2p_listen_work); |
| 2280 | wpa_s->p2p_listen_work = NULL; |
| 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) |
| 2285 | { |
| 2286 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 2287 | struct wpas_p2p_listen_work *lwork = work->ctx; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2288 | unsigned int duration; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2289 | |
| 2290 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 2291 | if (work->started) { |
| 2292 | wpa_s->p2p_listen_work = NULL; |
| 2293 | wpas_stop_listen(wpa_s); |
| 2294 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2295 | wpas_p2p_listen_work_free(lwork); |
| 2296 | return; |
| 2297 | } |
| 2298 | |
| 2299 | wpa_s->p2p_listen_work = work; |
| 2300 | |
| 2301 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); |
| 2302 | |
| 2303 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { |
| 2304 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " |
| 2305 | "report received Probe Request frames"); |
| 2306 | wpas_p2p_listen_work_done(wpa_s); |
| 2307 | return; |
| 2308 | } |
| 2309 | |
| 2310 | wpa_s->pending_listen_freq = lwork->freq; |
| 2311 | wpa_s->pending_listen_duration = lwork->duration; |
| 2312 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2313 | duration = lwork->duration; |
| 2314 | #ifdef CONFIG_TESTING_OPTIONS |
| 2315 | if (wpa_s->extra_roc_dur) { |
| 2316 | wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u", |
| 2317 | duration, duration + wpa_s->extra_roc_dur); |
| 2318 | duration += wpa_s->extra_roc_dur; |
| 2319 | } |
| 2320 | #endif /* CONFIG_TESTING_OPTIONS */ |
| 2321 | |
| 2322 | if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2323 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " |
| 2324 | "to remain on channel (%u MHz) for Listen " |
| 2325 | "state", lwork->freq); |
| 2326 | wpas_p2p_listen_work_done(wpa_s); |
| 2327 | wpa_s->pending_listen_freq = 0; |
| 2328 | return; |
| 2329 | } |
| 2330 | wpa_s->off_channel_freq = 0; |
| 2331 | wpa_s->roc_waiting_drv_freq = lwork->freq; |
| 2332 | } |
| 2333 | |
| 2334 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2335 | static int wpas_start_listen(void *ctx, unsigned int freq, |
| 2336 | unsigned int duration, |
| 2337 | const struct wpabuf *probe_resp_ie) |
| 2338 | { |
| 2339 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2340 | struct wpas_p2p_listen_work *lwork; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2341 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2342 | if (wpa_s->p2p_listen_work) { |
| 2343 | 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] | 2344 | return -1; |
| 2345 | } |
| 2346 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2347 | lwork = os_zalloc(sizeof(*lwork)); |
| 2348 | if (lwork == NULL) |
| 2349 | return -1; |
| 2350 | lwork->freq = freq; |
| 2351 | lwork->duration = duration; |
| 2352 | if (probe_resp_ie) { |
| 2353 | lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); |
| 2354 | if (lwork->probe_resp_ie == NULL) { |
| 2355 | wpas_p2p_listen_work_free(lwork); |
| 2356 | return -1; |
| 2357 | } |
| 2358 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2359 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2360 | if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, |
| 2361 | lwork) < 0) { |
| 2362 | wpas_p2p_listen_work_free(lwork); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2363 | return -1; |
| 2364 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2365 | |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
| 2369 | |
| 2370 | static void wpas_stop_listen(void *ctx) |
| 2371 | { |
| 2372 | struct wpa_supplicant *wpa_s = ctx; |
| 2373 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 2374 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 2375 | wpa_s->off_channel_freq = 0; |
| 2376 | wpa_s->roc_waiting_drv_freq = 0; |
| 2377 | } |
| 2378 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
| 2379 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2380 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | |
| 2384 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) |
| 2385 | { |
| 2386 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2387 | 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] | 2388 | } |
| 2389 | |
| 2390 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2391 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, |
| 2392 | const u8 *peer, const char *params, |
| 2393 | unsigned int generated_pin) |
| 2394 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2395 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR |
| 2396 | " %08d%s", MAC2STR(peer), generated_pin, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, |
| 2401 | const u8 *peer, const char *params) |
| 2402 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2403 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR |
| 2404 | "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2408 | static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, |
| 2409 | const u8 *dev_addr, const u8 *pri_dev_type, |
| 2410 | const char *dev_name, u16 supp_config_methods, |
| 2411 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
| 2412 | size_t group_id_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2413 | { |
| 2414 | struct wpa_supplicant *wpa_s = ctx; |
| 2415 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2416 | char params[300]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2417 | u8 empty_dev_type[8]; |
| 2418 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2419 | struct wpa_supplicant *group = NULL; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2420 | int res; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2421 | |
| 2422 | if (group_id) { |
| 2423 | for (group = wpa_s->global->ifaces; group; group = group->next) |
| 2424 | { |
| 2425 | struct wpa_ssid *s = group->current_ssid; |
| 2426 | if (s != NULL && |
| 2427 | s->mode == WPAS_MODE_P2P_GO && |
| 2428 | group_id_len - ETH_ALEN == s->ssid_len && |
| 2429 | os_memcmp(group_id + ETH_ALEN, s->ssid, |
| 2430 | s->ssid_len) == 0) |
| 2431 | break; |
| 2432 | } |
| 2433 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2434 | |
| 2435 | if (pri_dev_type == NULL) { |
| 2436 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); |
| 2437 | pri_dev_type = empty_dev_type; |
| 2438 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2439 | res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR |
| 2440 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
| 2441 | "dev_capab=0x%x group_capab=0x%x%s%s", |
| 2442 | MAC2STR(dev_addr), |
| 2443 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 2444 | sizeof(devtype)), |
| 2445 | dev_name, supp_config_methods, dev_capab, group_capab, |
| 2446 | group ? " group=" : "", |
| 2447 | group ? group->ifname : ""); |
| 2448 | if (os_snprintf_error(sizeof(params), res)) |
| 2449 | wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2450 | params[sizeof(params) - 1] = '\0'; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2451 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2452 | if (config_methods & WPS_CONFIG_DISPLAY) { |
| 2453 | generated_pin = wps_generate_pin(); |
| 2454 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2455 | generated_pin); |
| 2456 | } else if (config_methods & WPS_CONFIG_KEYPAD) |
| 2457 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
| 2458 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2459 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ |
| 2460 | MACSTR "%s", MAC2STR(peer), params); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2461 | |
| 2462 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, |
| 2463 | P2P_PROV_DISC_SUCCESS, |
| 2464 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2468 | 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] | 2469 | { |
| 2470 | struct wpa_supplicant *wpa_s = ctx; |
| 2471 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2472 | char params[20]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2473 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2474 | if (wpa_s->pending_pd_before_join && |
| 2475 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
| 2476 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { |
| 2477 | wpa_s->pending_pd_before_join = 0; |
| 2478 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2479 | "join-existing-group operation"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2480 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2481 | return; |
| 2482 | } |
| 2483 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2484 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2485 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) { |
| 2486 | int res; |
| 2487 | |
| 2488 | res = os_snprintf(params, sizeof(params), " peer_go=%d", |
| 2489 | wpa_s->pending_pd_use == AUTO_PD_JOIN); |
| 2490 | if (os_snprintf_error(sizeof(params), res)) |
| 2491 | params[sizeof(params) - 1] = '\0'; |
| 2492 | } else |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2493 | params[0] = '\0'; |
| 2494 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2495 | if (config_methods & WPS_CONFIG_DISPLAY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2496 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2497 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
| 2498 | generated_pin = wps_generate_pin(); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2499 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2500 | generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2501 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2502 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP |
| 2503 | MACSTR "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2504 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2505 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2506 | P2P_PROV_DISC_SUCCESS, |
| 2507 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2511 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2512 | enum p2p_prov_disc_status status, |
| 2513 | u32 adv_id, const u8 *adv_mac, |
| 2514 | const char *deferred_session_resp) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2515 | { |
| 2516 | struct wpa_supplicant *wpa_s = ctx; |
| 2517 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2518 | if (wpa_s->p2p_fallback_to_go_neg) { |
| 2519 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " |
| 2520 | "failed - fall back to GO Negotiation"); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2521 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 2522 | P2P_EVENT_FALLBACK_TO_GO_NEG |
| 2523 | "reason=PD-failed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2524 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 2525 | return; |
| 2526 | } |
| 2527 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2528 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2529 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2530 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2531 | "join-existing-group operation (no ACK for PD " |
| 2532 | "Req attempts)"); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2533 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2534 | return; |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2535 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2536 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2537 | if (adv_id && adv_mac && deferred_session_resp) { |
| 2538 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2539 | " p2p_dev_addr=" MACSTR " status=%d adv_id=%x" |
| 2540 | " deferred_session_resp='%s'", |
| 2541 | MAC2STR(peer), status, adv_id, |
| 2542 | deferred_session_resp); |
| 2543 | } else if (adv_id && adv_mac) { |
| 2544 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2545 | " p2p_dev_addr=" MACSTR " status=%d adv_id=%x", |
| 2546 | MAC2STR(peer), status, adv_id); |
| 2547 | } else { |
| 2548 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2549 | " p2p_dev_addr=" MACSTR " status=%d", |
| 2550 | MAC2STR(peer), status); |
| 2551 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2552 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2553 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2554 | status, 0, 0); |
| 2555 | } |
| 2556 | |
| 2557 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2558 | static int freq_included(const struct p2p_channels *channels, unsigned int freq) |
| 2559 | { |
| 2560 | if (channels == NULL) |
| 2561 | return 1; /* Assume no restrictions */ |
| 2562 | return p2p_channels_includes_freq(channels, freq); |
| 2563 | |
| 2564 | } |
| 2565 | |
| 2566 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2567 | /** |
| 2568 | * Pick the best frequency to use from all the currently used frequencies. |
| 2569 | */ |
| 2570 | static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s, |
| 2571 | struct wpa_used_freq_data *freqs, |
| 2572 | unsigned int num) |
| 2573 | { |
| 2574 | unsigned int i, c; |
| 2575 | |
| 2576 | /* find a candidate freq that is supported by P2P */ |
| 2577 | for (c = 0; c < num; c++) |
| 2578 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq)) |
| 2579 | break; |
| 2580 | |
| 2581 | if (c == num) |
| 2582 | return 0; |
| 2583 | |
| 2584 | /* once we have a candidate, try to find a 'better' one */ |
| 2585 | for (i = c + 1; i < num; i++) { |
| 2586 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) |
| 2587 | continue; |
| 2588 | |
| 2589 | /* |
| 2590 | * 1. Infrastructure station interfaces have higher preference. |
| 2591 | * 2. P2P Clients have higher preference. |
| 2592 | * 3. All others. |
| 2593 | */ |
| 2594 | if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) { |
| 2595 | c = i; |
| 2596 | break; |
| 2597 | } |
| 2598 | |
| 2599 | if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT)) |
| 2600 | c = i; |
| 2601 | } |
| 2602 | return freqs[c].freq; |
| 2603 | } |
| 2604 | |
| 2605 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2606 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
| 2607 | const u8 *go_dev_addr, const u8 *ssid, |
| 2608 | size_t ssid_len, int *go, u8 *group_bssid, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2609 | int *force_freq, int persistent_group, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2610 | const struct p2p_channels *channels, |
| 2611 | int dev_pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2612 | { |
| 2613 | struct wpa_supplicant *wpa_s = ctx; |
| 2614 | struct wpa_ssid *s; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2615 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2616 | struct wpa_supplicant *grp; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2617 | int best_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2618 | |
| 2619 | if (!persistent_group) { |
| 2620 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2621 | " to join an active group (SSID: %s)", |
| 2622 | MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2623 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2624 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
| 2625 | == 0 || |
| 2626 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { |
| 2627 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
| 2628 | "authorized invitation"); |
| 2629 | goto accept_inv; |
| 2630 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2631 | |
| 2632 | #ifdef CONFIG_WPS_NFC |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2633 | if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled && |
| 2634 | dev_pw_id == wpa_s->p2p_oob_dev_pw_id) { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2635 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag"); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2636 | wpa_s->p2p_wps_method = WPS_NFC; |
| 2637 | wpa_s->pending_join_wps_method = WPS_NFC; |
| 2638 | os_memcpy(wpa_s->pending_join_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2639 | go_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 2640 | os_memcpy(wpa_s->pending_join_iface_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2641 | bssid, ETH_ALEN); |
| 2642 | goto accept_inv; |
| 2643 | } |
| 2644 | #endif /* CONFIG_WPS_NFC */ |
| 2645 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2646 | /* |
| 2647 | * Do not accept the invitation automatically; notify user and |
| 2648 | * request approval. |
| 2649 | */ |
| 2650 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2651 | } |
| 2652 | |
| 2653 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
| 2654 | if (grp) { |
| 2655 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " |
| 2656 | "running persistent group"); |
| 2657 | if (*go) |
| 2658 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); |
| 2659 | goto accept_inv; |
| 2660 | } |
| 2661 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2662 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2663 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { |
| 2664 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " |
| 2665 | "invitation to re-invoke a persistent group"); |
Dmitry Shmidt | 76cd2cc | 2014-05-27 12:56:04 -0700 | [diff] [blame] | 2666 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2667 | } else if (!wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2668 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2669 | |
| 2670 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2671 | if (s->disabled == 2 && |
| 2672 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && |
| 2673 | s->ssid_len == ssid_len && |
| 2674 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2675 | break; |
| 2676 | } |
| 2677 | |
| 2678 | if (!s) { |
| 2679 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2680 | " requested reinvocation of an unknown group", |
| 2681 | MAC2STR(sa)); |
| 2682 | return P2P_SC_FAIL_UNKNOWN_GROUP; |
| 2683 | } |
| 2684 | |
| 2685 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { |
| 2686 | *go = 1; |
| 2687 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 2688 | wpa_printf(MSG_DEBUG, "P2P: The only available " |
| 2689 | "interface is already in use - reject " |
| 2690 | "invitation"); |
| 2691 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2692 | } |
| 2693 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); |
| 2694 | } else if (s->mode == WPAS_MODE_P2P_GO) { |
| 2695 | *go = 1; |
| 2696 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) |
| 2697 | { |
| 2698 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 2699 | "interface address for the group"); |
| 2700 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2701 | } |
| 2702 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, |
| 2703 | ETH_ALEN); |
| 2704 | } |
| 2705 | |
| 2706 | accept_inv: |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2707 | wpas_p2p_set_own_freq_preference(wpa_s, 0); |
| 2708 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2709 | best_freq = 0; |
| 2710 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 2711 | sizeof(struct wpa_used_freq_data)); |
| 2712 | if (freqs) { |
| 2713 | int num_channels = wpa_s->num_multichan_concurrent; |
| 2714 | int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels); |
| 2715 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
| 2716 | os_free(freqs); |
| 2717 | } |
| 2718 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2719 | /* Get one of the frequencies currently in use */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2720 | if (best_freq > 0) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2721 | 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] | 2722 | wpas_p2p_set_own_freq_preference(wpa_s, best_freq); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2723 | |
| 2724 | if (wpa_s->num_multichan_concurrent < 2 || |
| 2725 | wpas_p2p_num_unused_channels(wpa_s) < 1) { |
| 2726 | 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] | 2727 | *force_freq = best_freq; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2728 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2731 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 2732 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2733 | if (*go == 0) { |
| 2734 | /* We are the client */ |
| 2735 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " |
| 2736 | "running a GO but we are capable of MCC, " |
| 2737 | "figure out the best channel to use"); |
| 2738 | *force_freq = 0; |
| 2739 | } else if (!freq_included(channels, *force_freq)) { |
| 2740 | /* We are the GO, and *force_freq is not in the |
| 2741 | * intersection */ |
| 2742 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 2743 | "in intersection but we are capable of MCC, " |
| 2744 | "figure out the best channel to use", |
| 2745 | *force_freq); |
| 2746 | *force_freq = 0; |
| 2747 | } |
| 2748 | } |
| 2749 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2750 | return P2P_SC_SUCCESS; |
| 2751 | } |
| 2752 | |
| 2753 | |
| 2754 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, |
| 2755 | const u8 *ssid, size_t ssid_len, |
| 2756 | const u8 *go_dev_addr, u8 status, |
| 2757 | int op_freq) |
| 2758 | { |
| 2759 | struct wpa_supplicant *wpa_s = ctx; |
| 2760 | struct wpa_ssid *s; |
| 2761 | |
| 2762 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2763 | if (s->disabled == 2 && |
| 2764 | s->ssid_len == ssid_len && |
| 2765 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2766 | break; |
| 2767 | } |
| 2768 | |
| 2769 | if (status == P2P_SC_SUCCESS) { |
| 2770 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2771 | " was accepted; op_freq=%d MHz, SSID=%s", |
| 2772 | MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2773 | if (s) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 2774 | int go = s->mode == WPAS_MODE_P2P_GO; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2775 | wpas_p2p_group_add_persistent( |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2776 | wpa_s, s, go, 0, op_freq, 0, 0, NULL, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 2777 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2778 | } else if (bssid) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2779 | wpa_s->user_initiated_pd = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2780 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2781 | wpa_s->p2p_wps_method, 0, op_freq, |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 2782 | ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2783 | } |
| 2784 | return; |
| 2785 | } |
| 2786 | |
| 2787 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 2788 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 2789 | " was rejected (status %u)", MAC2STR(sa), status); |
| 2790 | return; |
| 2791 | } |
| 2792 | |
| 2793 | if (!s) { |
| 2794 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2795 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2796 | P2P_EVENT_INVITATION_RECEIVED |
| 2797 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 2798 | " bssid=" MACSTR " unknown-network", |
| 2799 | MAC2STR(sa), MAC2STR(go_dev_addr), |
| 2800 | MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2801 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2802 | wpa_msg_global(wpa_s, MSG_INFO, |
| 2803 | P2P_EVENT_INVITATION_RECEIVED |
| 2804 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 2805 | " unknown-network", |
| 2806 | MAC2STR(sa), MAC2STR(go_dev_addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2807 | } |
| 2808 | return; |
| 2809 | } |
| 2810 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2811 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2812 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 2813 | "sa=" MACSTR " persistent=%d freq=%d", |
| 2814 | MAC2STR(sa), s->id, op_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2815 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2816 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 2817 | "sa=" MACSTR " persistent=%d", |
| 2818 | MAC2STR(sa), s->id); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2819 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2823 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, |
| 2824 | struct wpa_ssid *ssid, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2825 | const u8 *peer, int inv) |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2826 | { |
| 2827 | size_t i; |
| 2828 | |
| 2829 | if (ssid == NULL) |
| 2830 | return; |
| 2831 | |
| 2832 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 2833 | if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2834 | ETH_ALEN) == 0) |
| 2835 | break; |
| 2836 | } |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2837 | if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2838 | if (ssid->mode != WPAS_MODE_P2P_GO && |
| 2839 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { |
| 2840 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " |
| 2841 | "due to invitation result", ssid->id); |
| 2842 | wpas_notify_network_removed(wpa_s, ssid); |
| 2843 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 2844 | return; |
| 2845 | } |
| 2846 | return; /* Peer not found in client list */ |
| 2847 | } |
| 2848 | |
| 2849 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2850 | "group %d client list%s", |
| 2851 | MAC2STR(peer), ssid->id, |
| 2852 | inv ? " due to invitation result" : ""); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 2853 | os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN, |
| 2854 | ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN, |
| 2855 | (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2856 | ssid->num_p2p_clients--; |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2857 | if (wpa_s->parent->conf->update_config && |
| 2858 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 2859 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | |
| 2863 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, |
| 2864 | const u8 *peer) |
| 2865 | { |
| 2866 | struct wpa_ssid *ssid; |
| 2867 | |
| 2868 | wpa_s = wpa_s->global->p2p_invite_group; |
| 2869 | if (wpa_s == NULL) |
| 2870 | return; /* No known invitation group */ |
| 2871 | ssid = wpa_s->current_ssid; |
| 2872 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 2873 | !ssid->p2p_persistent_group) |
| 2874 | return; /* Not operating as a GO in persistent group */ |
| 2875 | ssid = wpas_p2p_get_persistent(wpa_s->parent, peer, |
| 2876 | ssid->ssid, ssid->ssid_len); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2877 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2881 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2882 | const struct p2p_channels *channels, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2883 | const u8 *peer, int neg_freq, |
| 2884 | int peer_oper_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2885 | { |
| 2886 | struct wpa_supplicant *wpa_s = ctx; |
| 2887 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2888 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2889 | |
| 2890 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2891 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 2892 | "status=%d " MACSTR, |
| 2893 | status, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2894 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2895 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 2896 | "status=%d ", status); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2897 | } |
| 2898 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
| 2899 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2900 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, |
| 2901 | status, MAC2STR(peer)); |
| 2902 | if (wpa_s->pending_invite_ssid_id == -1) { |
| 2903 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) |
| 2904 | wpas_remove_persistent_client(wpa_s, peer); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2905 | return; /* Invitation to active group */ |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2906 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2907 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2908 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 2909 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " |
| 2910 | "invitation exchange to indicate readiness for " |
| 2911 | "re-invocation"); |
| 2912 | } |
| 2913 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2914 | if (status != P2P_SC_SUCCESS) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2915 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { |
| 2916 | ssid = wpa_config_get_network( |
| 2917 | wpa_s->conf, wpa_s->pending_invite_ssid_id); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2918 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2919 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2920 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 2921 | return; |
| 2922 | } |
| 2923 | |
| 2924 | ssid = wpa_config_get_network(wpa_s->conf, |
| 2925 | wpa_s->pending_invite_ssid_id); |
| 2926 | if (ssid == NULL) { |
| 2927 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " |
| 2928 | "data matching with invitation"); |
| 2929 | return; |
| 2930 | } |
| 2931 | |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 2932 | /* |
| 2933 | * The peer could have missed our ctrl::ack frame for Invitation |
| 2934 | * Response and continue retransmitting the frame. To reduce the |
| 2935 | * likelihood of the peer not getting successful TX status for the |
| 2936 | * Invitation Response frame, wait a short time here before starting |
| 2937 | * the persistent group so that we will remain on the current channel to |
| 2938 | * acknowledge any possible retransmission from the peer. |
| 2939 | */ |
| 2940 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " |
| 2941 | "starting persistent group"); |
| 2942 | os_sleep(0, 50000); |
| 2943 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2944 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && |
| 2945 | freq_included(channels, neg_freq)) |
| 2946 | freq = neg_freq; |
| 2947 | else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO && |
| 2948 | freq_included(channels, peer_oper_freq)) |
| 2949 | freq = peer_oper_freq; |
| 2950 | else |
| 2951 | freq = 0; |
| 2952 | |
| 2953 | wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s", |
| 2954 | freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2955 | wpas_p2p_group_add_persistent(wpa_s, ssid, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 2956 | ssid->mode == WPAS_MODE_P2P_GO, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 2957 | wpa_s->p2p_persistent_go_freq, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 2958 | freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2959 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, |
| 2960 | channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 2961 | ssid->mode == WPAS_MODE_P2P_GO ? |
| 2962 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 2963 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2967 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
| 2968 | unsigned int freq) |
| 2969 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2970 | if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq)) |
| 2971 | return 1; |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 2972 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | |
| 2976 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) |
| 2977 | { |
| 2978 | reg->channel[reg->channels] = chan; |
| 2979 | reg->channels++; |
| 2980 | } |
| 2981 | |
| 2982 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2983 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2984 | struct p2p_channels *chan, |
| 2985 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2986 | { |
| 2987 | int i, cla = 0; |
| 2988 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2989 | wpa_s->global->p2p_24ghz_social_channels = 1; |
| 2990 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2991 | os_memset(cli_chan, 0, sizeof(*cli_chan)); |
| 2992 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2993 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " |
| 2994 | "band"); |
| 2995 | |
| 2996 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ |
| 2997 | chan->reg_class[cla].reg_class = 81; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2998 | chan->reg_class[cla].channels = 0; |
| 2999 | for (i = 0; i < 11; i++) { |
| 3000 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) |
| 3001 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); |
| 3002 | } |
| 3003 | if (chan->reg_class[cla].channels) |
| 3004 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3005 | |
| 3006 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " |
| 3007 | "band"); |
| 3008 | |
| 3009 | /* Operating class 115 - 5 GHz, channels 36-48 */ |
| 3010 | chan->reg_class[cla].reg_class = 115; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3011 | chan->reg_class[cla].channels = 0; |
| 3012 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) |
| 3013 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); |
| 3014 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) |
| 3015 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); |
| 3016 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) |
| 3017 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); |
| 3018 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) |
| 3019 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); |
| 3020 | if (chan->reg_class[cla].channels) |
| 3021 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3022 | |
| 3023 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " |
| 3024 | "band"); |
| 3025 | |
| 3026 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ |
| 3027 | chan->reg_class[cla].reg_class = 124; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3028 | chan->reg_class[cla].channels = 0; |
| 3029 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) |
| 3030 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); |
| 3031 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) |
| 3032 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); |
| 3033 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) |
| 3034 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); |
| 3035 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) |
| 3036 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); |
| 3037 | if (chan->reg_class[cla].channels) |
| 3038 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3039 | |
| 3040 | chan->reg_classes = cla; |
| 3041 | return 0; |
| 3042 | } |
| 3043 | |
| 3044 | |
| 3045 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, |
| 3046 | u16 num_modes, |
| 3047 | enum hostapd_hw_mode mode) |
| 3048 | { |
| 3049 | u16 i; |
| 3050 | |
| 3051 | for (i = 0; i < num_modes; i++) { |
| 3052 | if (modes[i].mode == mode) |
| 3053 | return &modes[i]; |
| 3054 | } |
| 3055 | |
| 3056 | return NULL; |
| 3057 | } |
| 3058 | |
| 3059 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3060 | enum chan_allowed { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3061 | NOT_ALLOWED, NO_IR, ALLOWED |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3062 | }; |
| 3063 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3064 | static int has_channel(struct wpa_global *global, |
| 3065 | struct hostapd_hw_modes *mode, u8 chan, int *flags) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3066 | { |
| 3067 | int i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3068 | unsigned int freq; |
| 3069 | |
| 3070 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + |
| 3071 | chan * 5; |
| 3072 | if (wpas_p2p_disallowed_freq(global, freq)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3073 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3074 | |
| 3075 | for (i = 0; i < mode->num_channels; i++) { |
| 3076 | if (mode->channels[i].chan == chan) { |
| 3077 | if (flags) |
| 3078 | *flags = mode->channels[i].flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3079 | if (mode->channels[i].flag & |
| 3080 | (HOSTAPD_CHAN_DISABLED | |
| 3081 | HOSTAPD_CHAN_RADAR)) |
| 3082 | return NOT_ALLOWED; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3083 | if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR) |
| 3084 | return NO_IR; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3085 | return ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3089 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | struct p2p_oper_class_map { |
| 3094 | enum hostapd_hw_mode mode; |
| 3095 | u8 op_class; |
| 3096 | u8 min_chan; |
| 3097 | u8 max_chan; |
| 3098 | u8 inc; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3099 | enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3100 | }; |
| 3101 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3102 | static struct p2p_oper_class_map op_class[] = { |
| 3103 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3104 | #if 0 /* Do not enable HT40 on 2 GHz for now */ |
| 3105 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, |
| 3106 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, |
| 3107 | #endif |
| 3108 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
| 3109 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, |
| 3110 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, |
| 3111 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, |
| 3112 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, |
| 3113 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3114 | |
| 3115 | /* |
| 3116 | * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center |
| 3117 | * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of |
| 3118 | * 80 MHz, but currently use the following definition for simplicity |
| 3119 | * (these center frequencies are not actual channels, which makes |
| 3120 | * has_channel() fail). wpas_p2p_verify_80mhz() should take care of |
| 3121 | * removing invalid channels. |
| 3122 | */ |
| 3123 | { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 }, |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3124 | { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3125 | { -1, 0, 0, 0, 0, BW20 } |
| 3126 | }; |
| 3127 | |
| 3128 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3129 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, |
| 3130 | struct hostapd_hw_modes *mode, |
| 3131 | u8 channel) |
| 3132 | { |
| 3133 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; |
| 3134 | unsigned int i; |
| 3135 | |
| 3136 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 3137 | return 0; |
| 3138 | |
| 3139 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) |
| 3140 | /* |
| 3141 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), |
| 3142 | * so the center channel is 6 channels away from the start/end. |
| 3143 | */ |
| 3144 | if (channel >= center_channels[i] - 6 && |
| 3145 | channel <= center_channels[i] + 6) |
| 3146 | return center_channels[i]; |
| 3147 | |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, |
| 3153 | struct hostapd_hw_modes *mode, |
| 3154 | u8 channel, u8 bw) |
| 3155 | { |
| 3156 | u8 center_chan; |
| 3157 | int i, flags; |
| 3158 | enum chan_allowed res, ret = ALLOWED; |
| 3159 | |
| 3160 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3161 | if (!center_chan) |
| 3162 | return NOT_ALLOWED; |
| 3163 | if (center_chan >= 58 && center_chan <= 138) |
| 3164 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ |
| 3165 | |
| 3166 | /* check all the channels are available */ |
| 3167 | for (i = 0; i < 4; i++) { |
| 3168 | int adj_chan = center_chan - 6 + i * 4; |
| 3169 | |
| 3170 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); |
| 3171 | if (res == NOT_ALLOWED) |
| 3172 | return NOT_ALLOWED; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3173 | if (res == NO_IR) |
| 3174 | ret = NO_IR; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3175 | |
| 3176 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) |
| 3177 | return NOT_ALLOWED; |
| 3178 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) |
| 3179 | return NOT_ALLOWED; |
| 3180 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) |
| 3181 | return NOT_ALLOWED; |
| 3182 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) |
| 3183 | return NOT_ALLOWED; |
| 3184 | } |
| 3185 | |
| 3186 | return ret; |
| 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, |
| 3191 | struct hostapd_hw_modes *mode, |
| 3192 | u8 channel, u8 bw) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3193 | { |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 3194 | int flag = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3195 | enum chan_allowed res, res2; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3196 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3197 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); |
| 3198 | if (bw == BW40MINUS) { |
| 3199 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) |
| 3200 | return NOT_ALLOWED; |
| 3201 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); |
| 3202 | } else if (bw == BW40PLUS) { |
| 3203 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) |
| 3204 | return NOT_ALLOWED; |
| 3205 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); |
| 3206 | } else if (bw == BW80) { |
| 3207 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); |
| 3208 | } |
| 3209 | |
| 3210 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) |
| 3211 | return NOT_ALLOWED; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3212 | if (res == NO_IR || res2 == NO_IR) |
| 3213 | return NO_IR; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3214 | return res; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3218 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3219 | struct p2p_channels *chan, |
| 3220 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3221 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3222 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3223 | int cla, op, cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3224 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3225 | if (wpa_s->hw.modes == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3226 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
| 3227 | "of all supported channels; assume dualband " |
| 3228 | "support"); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3229 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3230 | } |
| 3231 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3232 | cla = cli_cla = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3233 | |
| 3234 | for (op = 0; op_class[op].op_class; op++) { |
| 3235 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3236 | u8 ch; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3237 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3238 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3239 | 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] | 3240 | if (mode == NULL) |
| 3241 | continue; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3242 | if (mode->mode == HOSTAPD_MODE_IEEE80211G) |
| 3243 | wpa_s->global->p2p_24ghz_social_channels = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3244 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3245 | enum chan_allowed res; |
| 3246 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
| 3247 | if (res == ALLOWED) { |
| 3248 | if (reg == NULL) { |
| 3249 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", |
| 3250 | o->op_class); |
| 3251 | reg = &chan->reg_class[cla]; |
| 3252 | cla++; |
| 3253 | reg->reg_class = o->op_class; |
| 3254 | } |
| 3255 | reg->channel[reg->channels] = ch; |
| 3256 | reg->channels++; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3257 | } else if (res == NO_IR && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3258 | wpa_s->conf->p2p_add_cli_chan) { |
| 3259 | if (cli_reg == NULL) { |
| 3260 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", |
| 3261 | o->op_class); |
| 3262 | cli_reg = &cli_chan->reg_class[cli_cla]; |
| 3263 | cli_cla++; |
| 3264 | cli_reg->reg_class = o->op_class; |
| 3265 | } |
| 3266 | cli_reg->channel[cli_reg->channels] = ch; |
| 3267 | cli_reg->channels++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3268 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3269 | } |
| 3270 | if (reg) { |
| 3271 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", |
| 3272 | reg->channel, reg->channels); |
| 3273 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3274 | if (cli_reg) { |
| 3275 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", |
| 3276 | cli_reg->channel, cli_reg->channels); |
| 3277 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | chan->reg_classes = cla; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3281 | cli_chan->reg_classes = cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3282 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3287 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, |
| 3288 | struct hostapd_hw_modes *mode, u8 channel) |
| 3289 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3290 | int op; |
| 3291 | enum chan_allowed ret; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3292 | |
| 3293 | for (op = 0; op_class[op].op_class; op++) { |
| 3294 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3295 | u8 ch; |
| 3296 | |
| 3297 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
| 3298 | if (o->mode != HOSTAPD_MODE_IEEE80211A || |
| 3299 | o->bw == BW20 || ch != channel) |
| 3300 | continue; |
| 3301 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3302 | if (ret == ALLOWED) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3303 | return (o->bw == BW40MINUS) ? -1 : 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
| 3309 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3310 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, |
| 3311 | struct hostapd_hw_modes *mode, u8 channel) |
| 3312 | { |
| 3313 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) |
| 3314 | return 0; |
| 3315 | |
| 3316 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3317 | } |
| 3318 | |
| 3319 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3320 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, |
| 3321 | size_t buf_len) |
| 3322 | { |
| 3323 | struct wpa_supplicant *wpa_s = ctx; |
| 3324 | |
| 3325 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3326 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) |
| 3327 | break; |
| 3328 | } |
| 3329 | if (wpa_s == NULL) |
| 3330 | return -1; |
| 3331 | |
| 3332 | return wpa_drv_get_noa(wpa_s, buf, buf_len); |
| 3333 | } |
| 3334 | |
| 3335 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3336 | struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s, |
| 3337 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3338 | { |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3339 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3340 | struct wpa_ssid *s = wpa_s->current_ssid; |
| 3341 | if (s == NULL) |
| 3342 | continue; |
| 3343 | if (s->mode != WPAS_MODE_P2P_GO && |
| 3344 | s->mode != WPAS_MODE_AP && |
| 3345 | s->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
| 3346 | continue; |
| 3347 | if (s->ssid_len != ssid_len || |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 3348 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3349 | continue; |
| 3350 | return wpa_s; |
| 3351 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3352 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3353 | return NULL; |
| 3354 | |
| 3355 | } |
| 3356 | |
| 3357 | |
| 3358 | struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s, |
| 3359 | const u8 *peer_dev_addr) |
| 3360 | { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3361 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3362 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 3363 | if (ssid == NULL) |
| 3364 | continue; |
| 3365 | if (ssid->mode != WPAS_MODE_INFRA) |
| 3366 | continue; |
| 3367 | if (wpa_s->wpa_state != WPA_COMPLETED && |
| 3368 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) |
| 3369 | continue; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3370 | if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0) |
| 3371 | return wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3372 | } |
| 3373 | |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 3374 | return NULL; |
| 3375 | } |
| 3376 | |
| 3377 | |
| 3378 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
| 3379 | { |
| 3380 | struct wpa_supplicant *wpa_s = ctx; |
| 3381 | |
| 3382 | return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3386 | static int wpas_is_concurrent_session_active(void *ctx) |
| 3387 | { |
| 3388 | struct wpa_supplicant *wpa_s = ctx; |
| 3389 | struct wpa_supplicant *ifs; |
| 3390 | |
| 3391 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 3392 | if (ifs == wpa_s) |
| 3393 | continue; |
| 3394 | if (ifs->wpa_state > WPA_ASSOCIATED) |
| 3395 | return 1; |
| 3396 | } |
| 3397 | return 0; |
| 3398 | } |
| 3399 | |
| 3400 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3401 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) |
| 3402 | { |
| 3403 | struct wpa_supplicant *wpa_s = ctx; |
| 3404 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); |
| 3405 | } |
| 3406 | |
| 3407 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 3408 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s, |
| 3409 | const char *conf_p2p_dev) |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3410 | { |
| 3411 | struct wpa_interface iface; |
| 3412 | struct wpa_supplicant *p2pdev_wpa_s; |
| 3413 | char ifname[100]; |
| 3414 | char force_name[100]; |
| 3415 | int ret; |
| 3416 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3417 | ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", |
| 3418 | wpa_s->ifname); |
| 3419 | if (os_snprintf_error(sizeof(ifname), ret)) |
| 3420 | return -1; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3421 | force_name[0] = '\0'; |
| 3422 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; |
| 3423 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, |
| 3424 | force_name, wpa_s->pending_interface_addr, NULL); |
| 3425 | if (ret < 0) { |
| 3426 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); |
| 3427 | return ret; |
| 3428 | } |
| 3429 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 3430 | sizeof(wpa_s->pending_interface_name)); |
| 3431 | |
| 3432 | os_memset(&iface, 0, sizeof(iface)); |
| 3433 | iface.p2p_mgmt = 1; |
| 3434 | iface.ifname = wpa_s->pending_interface_name; |
| 3435 | iface.driver = wpa_s->driver->name; |
| 3436 | iface.driver_param = wpa_s->conf->driver_param; |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3437 | |
| 3438 | /* |
| 3439 | * If a P2P Device configuration file was given, use it as the interface |
| 3440 | * configuration file (instead of using parent's configuration file. |
| 3441 | */ |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 3442 | if (conf_p2p_dev) { |
| 3443 | iface.confname = conf_p2p_dev; |
Dmitry Shmidt | 2ac5f60 | 2014-03-07 10:08:21 -0800 | [diff] [blame] | 3444 | iface.ctrl_interface = NULL; |
| 3445 | } else { |
| 3446 | iface.confname = wpa_s->confname; |
| 3447 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
| 3448 | } |
| 3449 | iface.conf_p2p_dev = NULL; |
| 3450 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 3451 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3452 | if (!p2pdev_wpa_s) { |
| 3453 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); |
| 3454 | return -1; |
| 3455 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3456 | wpa_s->p2p_dev = p2pdev_wpa_s; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3457 | |
| 3458 | wpa_s->pending_interface_name[0] = '\0'; |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
| 3462 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3463 | static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, |
| 3464 | const u8 *noa, size_t noa_len) |
| 3465 | { |
| 3466 | struct wpa_supplicant *wpa_s, *intf = ctx; |
| 3467 | char hex[100]; |
| 3468 | |
| 3469 | for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3470 | if (wpa_s->waiting_presence_resp) |
| 3471 | break; |
| 3472 | } |
| 3473 | if (!wpa_s) { |
| 3474 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); |
| 3475 | return; |
| 3476 | } |
| 3477 | wpa_s->waiting_presence_resp = 0; |
| 3478 | |
| 3479 | wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); |
| 3480 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR |
| 3481 | " status=%u noa=%s", MAC2STR(src), status, hex); |
| 3482 | } |
| 3483 | |
| 3484 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3485 | static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid, |
| 3486 | size_t ssid_len, u8 *go_dev_addr, |
| 3487 | u8 *ret_ssid, size_t *ret_ssid_len) |
| 3488 | { |
| 3489 | struct wpa_supplicant *wpa_s = ctx; |
| 3490 | struct wpa_ssid *s; |
| 3491 | |
| 3492 | s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len); |
| 3493 | if (s) { |
| 3494 | os_memcpy(ret_ssid, s->ssid, s->ssid_len); |
| 3495 | *ret_ssid_len = s->ssid_len; |
| 3496 | os_memcpy(go_dev_addr, s->bssid, ETH_ALEN); |
| 3497 | return 1; |
| 3498 | } |
| 3499 | |
| 3500 | return 0; |
| 3501 | } |
| 3502 | |
| 3503 | |
| 3504 | static int wpas_get_go_info(void *ctx, u8 *intended_addr, |
| 3505 | u8 *ssid, size_t *ssid_len, int *group_iface) |
| 3506 | { |
| 3507 | struct wpa_supplicant *wpa_s = ctx; |
| 3508 | struct wpa_ssid *s; |
| 3509 | u8 bssid[ETH_ALEN]; |
| 3510 | |
| 3511 | s = wpas_p2p_group_go_ssid(wpa_s, bssid); |
| 3512 | if (!s) { |
| 3513 | s = wpas_p2p_get_persistent_go(wpa_s); |
| 3514 | if (s) |
| 3515 | os_memcpy(bssid, s->bssid, ETH_ALEN); |
| 3516 | } |
| 3517 | |
| 3518 | *group_iface = wpas_p2p_create_iface(wpa_s); |
| 3519 | if (!s) |
| 3520 | return 0; |
| 3521 | |
| 3522 | os_memcpy(intended_addr, bssid, ETH_ALEN); |
| 3523 | os_memcpy(ssid, s->ssid, s->ssid_len); |
| 3524 | *ssid_len = s->ssid_len; |
| 3525 | |
| 3526 | return 1; |
| 3527 | } |
| 3528 | |
| 3529 | |
| 3530 | static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go, |
| 3531 | const u8 *ssid, size_t ssid_len) |
| 3532 | { |
| 3533 | struct wpa_supplicant *wpa_s = ctx; |
| 3534 | struct wpa_ssid *s; |
| 3535 | int save_config = 0; |
| 3536 | size_t i; |
| 3537 | |
| 3538 | /* Start with our first choice of Persistent Groups */ |
| 3539 | while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) { |
| 3540 | if (go && ssid && ssid_len && |
| 3541 | s->ssid_len == ssid_len && |
| 3542 | os_memcmp(go, s->bssid, ETH_ALEN) == 0 && |
| 3543 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 3544 | break; |
| 3545 | |
| 3546 | /* Remove stale persistent group */ |
| 3547 | if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) { |
| 3548 | wpa_config_remove_network(wpa_s->conf, s->id); |
| 3549 | save_config = 1; |
| 3550 | continue; |
| 3551 | } |
| 3552 | |
| 3553 | for (i = 0; i < s->num_p2p_clients; i++) { |
| 3554 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, |
| 3555 | peer, ETH_ALEN) != 0) |
| 3556 | continue; |
| 3557 | |
| 3558 | os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, |
| 3559 | s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, |
| 3560 | (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); |
| 3561 | break; |
| 3562 | } |
| 3563 | s->num_p2p_clients--; |
| 3564 | save_config = 1; |
| 3565 | } |
| 3566 | |
| 3567 | if (save_config) |
| 3568 | p2p_config_write(wpa_s); |
| 3569 | |
| 3570 | /* Return TRUE if valid SSID remains */ |
| 3571 | return s != NULL; |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev, |
| 3576 | const u8 *adv_mac, const u8 *ses_mac, |
| 3577 | const u8 *grp_mac, u32 adv_id, u32 ses_id, |
| 3578 | u8 conncap, int passwd_id, |
| 3579 | const u8 *persist_ssid, |
| 3580 | size_t persist_ssid_size, int response_done, |
| 3581 | int prov_start, const char *session_info) |
| 3582 | { |
| 3583 | struct wpa_supplicant *wpa_s = ctx; |
| 3584 | u8 mac[ETH_ALEN]; |
| 3585 | struct wpa_ssid *persistent_go, *stale, *s; |
| 3586 | int save_config = 0; |
| 3587 | struct wpa_supplicant *go_wpa_s; |
| 3588 | |
| 3589 | if (!dev) |
| 3590 | return; |
| 3591 | |
| 3592 | os_memset(mac, 0, ETH_ALEN); |
| 3593 | if (!adv_mac) |
| 3594 | adv_mac = mac; |
| 3595 | if (!ses_mac) |
| 3596 | ses_mac = mac; |
| 3597 | if (!grp_mac) |
| 3598 | grp_mac = mac; |
| 3599 | |
| 3600 | if (prov_start) { |
| 3601 | if (session_info == NULL) { |
| 3602 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3603 | P2P_EVENT_P2PS_PROVISION_START MACSTR |
| 3604 | " adv_id=%x conncap=%x" |
| 3605 | " adv_mac=" MACSTR |
| 3606 | " session=%x mac=" MACSTR |
| 3607 | " dev_passwd_id=%d", |
| 3608 | MAC2STR(dev), adv_id, conncap, |
| 3609 | MAC2STR(adv_mac), |
| 3610 | ses_id, MAC2STR(ses_mac), |
| 3611 | passwd_id); |
| 3612 | } else { |
| 3613 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3614 | P2P_EVENT_P2PS_PROVISION_START MACSTR |
| 3615 | " adv_id=%x conncap=%x" |
| 3616 | " adv_mac=" MACSTR |
| 3617 | " session=%x mac=" MACSTR |
| 3618 | " dev_passwd_id=%d info='%s'", |
| 3619 | MAC2STR(dev), adv_id, conncap, |
| 3620 | MAC2STR(adv_mac), |
| 3621 | ses_id, MAC2STR(ses_mac), |
| 3622 | passwd_id, session_info); |
| 3623 | } |
| 3624 | return; |
| 3625 | } |
| 3626 | |
| 3627 | go_wpa_s = wpas_p2p_get_go_group(wpa_s); |
| 3628 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); |
| 3629 | |
| 3630 | if (status && status != P2P_SC_SUCCESS_DEFERRED) { |
| 3631 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) |
| 3632 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); |
| 3633 | |
| 3634 | if (persistent_go && !persistent_go->num_p2p_clients) { |
| 3635 | /* remove empty persistent GO */ |
| 3636 | wpa_config_remove_network(wpa_s->conf, |
| 3637 | persistent_go->id); |
| 3638 | } |
| 3639 | |
| 3640 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3641 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR |
| 3642 | " status=%d" |
| 3643 | " adv_id=%x adv_mac=" MACSTR |
| 3644 | " session=%x mac=" MACSTR, |
| 3645 | MAC2STR(dev), status, |
| 3646 | adv_id, MAC2STR(adv_mac), |
| 3647 | ses_id, MAC2STR(ses_mac)); |
| 3648 | return; |
| 3649 | } |
| 3650 | |
| 3651 | /* Clean up stale persistent groups with this device */ |
| 3652 | s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid, |
| 3653 | persist_ssid_size); |
| 3654 | for (;;) { |
| 3655 | stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0); |
| 3656 | if (!stale) |
| 3657 | break; |
| 3658 | |
| 3659 | if (s && s->ssid_len == stale->ssid_len && |
| 3660 | os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 && |
| 3661 | os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0) |
| 3662 | break; |
| 3663 | |
| 3664 | /* Remove stale persistent group */ |
| 3665 | if (stale->mode != WPAS_MODE_P2P_GO || |
| 3666 | stale->num_p2p_clients <= 1) { |
| 3667 | wpa_config_remove_network(wpa_s->conf, stale->id); |
| 3668 | } else { |
| 3669 | size_t i; |
| 3670 | |
| 3671 | for (i = 0; i < stale->num_p2p_clients; i++) { |
| 3672 | if (os_memcmp(stale->p2p_client_list + |
| 3673 | i * ETH_ALEN, |
| 3674 | dev, ETH_ALEN) == 0) { |
| 3675 | os_memmove(stale->p2p_client_list + |
| 3676 | i * ETH_ALEN, |
| 3677 | stale->p2p_client_list + |
| 3678 | (i + 1) * ETH_ALEN, |
| 3679 | (stale->num_p2p_clients - |
| 3680 | i - 1) * ETH_ALEN); |
| 3681 | break; |
| 3682 | } |
| 3683 | } |
| 3684 | stale->num_p2p_clients--; |
| 3685 | } |
| 3686 | save_config = 1; |
| 3687 | } |
| 3688 | |
| 3689 | if (save_config) |
| 3690 | p2p_config_write(wpa_s); |
| 3691 | |
| 3692 | if (s) { |
| 3693 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) |
| 3694 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); |
| 3695 | |
| 3696 | if (persistent_go && s != persistent_go && |
| 3697 | !persistent_go->num_p2p_clients) { |
| 3698 | /* remove empty persistent GO */ |
| 3699 | wpa_config_remove_network(wpa_s->conf, |
| 3700 | persistent_go->id); |
| 3701 | /* Save config */ |
| 3702 | } |
| 3703 | |
| 3704 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3705 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR |
| 3706 | " status=%d" |
| 3707 | " adv_id=%x adv_mac=" MACSTR |
| 3708 | " session=%x mac=" MACSTR |
| 3709 | " persist=%d", |
| 3710 | MAC2STR(dev), status, |
| 3711 | adv_id, MAC2STR(adv_mac), |
| 3712 | ses_id, MAC2STR(ses_mac), s->id); |
| 3713 | return; |
| 3714 | } |
| 3715 | |
| 3716 | if (conncap == P2PS_SETUP_GROUP_OWNER) { |
| 3717 | const char *go_ifname = NULL; |
| 3718 | if (!go_wpa_s) { |
| 3719 | wpa_s->global->pending_p2ps_group = 1; |
| 3720 | |
| 3721 | if (wpa_s->conf->p2p_no_group_iface) |
| 3722 | go_ifname = wpa_s->ifname; |
| 3723 | else if (wpa_s->pending_interface_name[0]) |
| 3724 | go_ifname = wpa_s->pending_interface_name; |
| 3725 | |
| 3726 | if (!go_ifname) { |
| 3727 | wpas_p2ps_prov_complete( |
| 3728 | wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP, |
| 3729 | dev, adv_mac, ses_mac, |
| 3730 | NULL, adv_id, ses_id, 0, 0, |
| 3731 | NULL, 0, 0, 0, NULL); |
| 3732 | return; |
| 3733 | } |
| 3734 | |
| 3735 | /* If PD Resp complete, start up the GO */ |
| 3736 | if (response_done && persistent_go) { |
| 3737 | wpas_p2p_group_add_persistent( |
| 3738 | wpa_s, persistent_go, |
| 3739 | 0, 0, 0, 0, 0, NULL, |
| 3740 | persistent_go->mode == |
| 3741 | WPAS_MODE_P2P_GO ? |
| 3742 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 3743 | 0); |
| 3744 | } else if (response_done) { |
| 3745 | wpas_p2p_group_add(wpa_s, 1, 0, 0, 0); |
| 3746 | } |
| 3747 | |
| 3748 | if (passwd_id == DEV_PW_P2PS_DEFAULT) { |
| 3749 | os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN); |
| 3750 | wpa_s->p2ps_join_addr_valid = 1; |
| 3751 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 3752 | "P2PS: Saving PIN for " MACSTR, |
| 3753 | MAC2STR(dev)); |
| 3754 | } |
| 3755 | } else if (passwd_id == DEV_PW_P2PS_DEFAULT) { |
| 3756 | go_ifname = go_wpa_s->ifname; |
| 3757 | |
| 3758 | wpa_dbg(go_wpa_s, MSG_DEBUG, |
| 3759 | "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev)); |
| 3760 | wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670", |
| 3761 | NULL, 0, 0); |
| 3762 | |
| 3763 | os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN); |
| 3764 | wpa_s->p2ps_join_addr_valid = 1; |
| 3765 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 3766 | "P2PS: Saving PIN for " MACSTR, MAC2STR(dev)); |
| 3767 | } |
| 3768 | |
| 3769 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3770 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR |
| 3771 | " status=%d conncap=%x" |
| 3772 | " adv_id=%x adv_mac=" MACSTR |
| 3773 | " session=%x mac=" MACSTR |
| 3774 | " dev_passwd_id=%d go=%s", |
| 3775 | MAC2STR(dev), status, conncap, |
| 3776 | adv_id, MAC2STR(adv_mac), |
| 3777 | ses_id, MAC2STR(ses_mac), |
| 3778 | passwd_id, go_ifname); |
| 3779 | return; |
| 3780 | } |
| 3781 | |
| 3782 | if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) |
| 3783 | wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); |
| 3784 | |
| 3785 | if (persistent_go && !persistent_go->num_p2p_clients) { |
| 3786 | /* remove empty persistent GO */ |
| 3787 | wpa_config_remove_network(wpa_s->conf, persistent_go->id); |
| 3788 | } |
| 3789 | |
| 3790 | if (conncap == P2PS_SETUP_CLIENT) { |
| 3791 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3792 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR |
| 3793 | " status=%d conncap=%x" |
| 3794 | " adv_id=%x adv_mac=" MACSTR |
| 3795 | " session=%x mac=" MACSTR |
| 3796 | " dev_passwd_id=%d join=" MACSTR, |
| 3797 | MAC2STR(dev), status, conncap, |
| 3798 | adv_id, MAC2STR(adv_mac), |
| 3799 | ses_id, MAC2STR(ses_mac), |
| 3800 | passwd_id, MAC2STR(grp_mac)); |
| 3801 | } else { |
| 3802 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3803 | P2P_EVENT_P2PS_PROVISION_DONE MACSTR |
| 3804 | " status=%d conncap=%x" |
| 3805 | " adv_id=%x adv_mac=" MACSTR |
| 3806 | " session=%x mac=" MACSTR |
| 3807 | " dev_passwd_id=%d", |
| 3808 | MAC2STR(dev), status, conncap, |
| 3809 | adv_id, MAC2STR(adv_mac), |
| 3810 | ses_id, MAC2STR(ses_mac), |
| 3811 | passwd_id); |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 3816 | static int _wpas_p2p_in_progress(void *ctx) |
| 3817 | { |
| 3818 | struct wpa_supplicant *wpa_s = ctx; |
| 3819 | return wpas_p2p_in_progress(wpa_s); |
| 3820 | } |
| 3821 | |
| 3822 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3823 | static int wpas_prov_disc_resp_cb(void *ctx) |
| 3824 | { |
| 3825 | struct wpa_supplicant *wpa_s = ctx; |
| 3826 | struct wpa_ssid *persistent_go; |
| 3827 | |
| 3828 | if (!wpa_s->global->pending_p2ps_group) |
| 3829 | return 0; |
| 3830 | |
| 3831 | wpa_s->global->pending_p2ps_group = 0; |
| 3832 | |
| 3833 | if (wpas_p2p_get_go_group(wpa_s)) |
| 3834 | return 0; |
| 3835 | persistent_go = wpas_p2p_get_persistent_go(wpa_s); |
| 3836 | |
| 3837 | if (persistent_go) { |
| 3838 | wpas_p2p_group_add_persistent( |
| 3839 | wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL, |
| 3840 | persistent_go->mode == WPAS_MODE_P2P_GO ? |
| 3841 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
| 3842 | } else { |
| 3843 | wpas_p2p_group_add(wpa_s, 1, 0, 0, 0); |
| 3844 | } |
| 3845 | |
| 3846 | return 1; |
| 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; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3859 | int i; |
| 3860 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3861 | if (wpa_s->conf->p2p_disabled) |
| 3862 | return 0; |
| 3863 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3864 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 3865 | return 0; |
| 3866 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3867 | if (global->p2p) |
| 3868 | return 0; |
| 3869 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3870 | os_memset(&p2p, 0, sizeof(p2p)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3871 | p2p.cb_ctx = wpa_s; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3872 | p2p.debug_print = wpas_p2p_debug_print; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3873 | p2p.p2p_scan = wpas_p2p_scan; |
| 3874 | p2p.send_action = wpas_send_action; |
| 3875 | p2p.send_action_done = wpas_send_action_done; |
| 3876 | p2p.go_neg_completed = wpas_go_neg_completed; |
| 3877 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; |
| 3878 | p2p.dev_found = wpas_dev_found; |
| 3879 | p2p.dev_lost = wpas_dev_lost; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3880 | p2p.find_stopped = wpas_find_stopped; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3881 | p2p.start_listen = wpas_start_listen; |
| 3882 | p2p.stop_listen = wpas_stop_listen; |
| 3883 | p2p.send_probe_resp = wpas_send_probe_resp; |
| 3884 | p2p.sd_request = wpas_sd_request; |
| 3885 | p2p.sd_response = wpas_sd_response; |
| 3886 | p2p.prov_disc_req = wpas_prov_disc_req; |
| 3887 | p2p.prov_disc_resp = wpas_prov_disc_resp; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3888 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3889 | p2p.invitation_process = wpas_invitation_process; |
| 3890 | p2p.invitation_received = wpas_invitation_received; |
| 3891 | p2p.invitation_result = wpas_invitation_result; |
| 3892 | p2p.get_noa = wpas_get_noa; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3893 | p2p.go_connected = wpas_go_connected; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3894 | p2p.presence_resp = wpas_presence_resp; |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 3895 | p2p.is_concurrent_session_active = wpas_is_concurrent_session_active; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 3896 | p2p.is_p2p_in_progress = _wpas_p2p_in_progress; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 3897 | p2p.get_persistent_group = wpas_get_persistent_group; |
| 3898 | p2p.get_go_info = wpas_get_go_info; |
| 3899 | p2p.remove_stale_groups = wpas_remove_stale_groups; |
| 3900 | p2p.p2ps_prov_complete = wpas_p2ps_prov_complete; |
| 3901 | p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb; |
| 3902 | p2p.p2ps_group_capability = p2ps_group_capability; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3903 | |
| 3904 | 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] | 3905 | 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] | 3906 | p2p.dev_name = wpa_s->conf->device_name; |
| 3907 | p2p.manufacturer = wpa_s->conf->manufacturer; |
| 3908 | p2p.model_name = wpa_s->conf->model_name; |
| 3909 | p2p.model_number = wpa_s->conf->model_number; |
| 3910 | p2p.serial_number = wpa_s->conf->serial_number; |
| 3911 | if (wpa_s->wps) { |
| 3912 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); |
| 3913 | p2p.config_methods = wpa_s->wps->config_methods; |
| 3914 | } |
| 3915 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3916 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { |
| 3917 | wpa_printf(MSG_ERROR, |
| 3918 | "P2P: Failed to configure supported channel list"); |
| 3919 | return -1; |
| 3920 | } |
| 3921 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3922 | if (wpa_s->conf->p2p_listen_reg_class && |
| 3923 | wpa_s->conf->p2p_listen_channel) { |
| 3924 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 3925 | p2p.channel = wpa_s->conf->p2p_listen_channel; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3926 | p2p.channel_forced = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3927 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3928 | /* |
| 3929 | * Pick one of the social channels randomly as the listen |
| 3930 | * channel. |
| 3931 | */ |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3932 | if (p2p_config_get_random_social(&p2p, &p2p.reg_class, |
| 3933 | &p2p.channel) != 0) { |
| 3934 | wpa_printf(MSG_ERROR, |
| 3935 | "P2P: Failed to select random social channel as listen channel"); |
| 3936 | return -1; |
| 3937 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 3938 | p2p.channel_forced = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3939 | } |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3940 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d", |
| 3941 | p2p.reg_class, p2p.channel); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3942 | |
| 3943 | if (wpa_s->conf->p2p_oper_reg_class && |
| 3944 | wpa_s->conf->p2p_oper_channel) { |
| 3945 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 3946 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; |
| 3947 | p2p.cfg_op_channel = 1; |
| 3948 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " |
| 3949 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3950 | |
| 3951 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3952 | /* |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3953 | * Use random operation channel from 2.4 GHz band social |
| 3954 | * channels (1, 6, 11) or band 60 GHz social channel (2) if no |
| 3955 | * other preference is indicated. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3956 | */ |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 3957 | if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class, |
| 3958 | &p2p.op_channel) != 0) { |
| 3959 | wpa_printf(MSG_ERROR, |
| 3960 | "P2P: Failed to select random social channel as operation channel"); |
| 3961 | return -1; |
| 3962 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3963 | p2p.cfg_op_channel = 0; |
| 3964 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " |
| 3965 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3966 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 3967 | |
| 3968 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { |
| 3969 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; |
| 3970 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; |
| 3971 | } |
| 3972 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3973 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 3974 | os_memcpy(p2p.country, wpa_s->conf->country, 2); |
| 3975 | p2p.country[2] = 0x04; |
| 3976 | } else |
| 3977 | os_memcpy(p2p.country, "XX\x04", 3); |
| 3978 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3979 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
| 3980 | WPS_DEV_TYPE_LEN); |
| 3981 | |
| 3982 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3983 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, |
| 3984 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3985 | |
| 3986 | p2p.concurrent_operations = !!(wpa_s->drv_flags & |
| 3987 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); |
| 3988 | |
| 3989 | p2p.max_peers = 100; |
| 3990 | |
| 3991 | if (wpa_s->conf->p2p_ssid_postfix) { |
| 3992 | p2p.ssid_postfix_len = |
| 3993 | os_strlen(wpa_s->conf->p2p_ssid_postfix); |
| 3994 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) |
| 3995 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); |
| 3996 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, |
| 3997 | p2p.ssid_postfix_len); |
| 3998 | } |
| 3999 | |
| 4000 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
| 4001 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4002 | p2p.max_listen = wpa_s->max_remain_on_chan; |
| 4003 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 4004 | if (wpa_s->conf->p2p_passphrase_len >= 8 && |
| 4005 | wpa_s->conf->p2p_passphrase_len <= 63) |
| 4006 | p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len; |
| 4007 | else |
| 4008 | p2p.passphrase_len = 8; |
| 4009 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4010 | global->p2p = p2p_init(&p2p); |
| 4011 | if (global->p2p == NULL) |
| 4012 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4013 | global->p2p_init_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4014 | |
| 4015 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 4016 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 4017 | continue; |
| 4018 | p2p_add_wps_vendor_extension( |
| 4019 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 4020 | } |
| 4021 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4022 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); |
| 4023 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4024 | return 0; |
| 4025 | } |
| 4026 | |
| 4027 | |
| 4028 | /** |
| 4029 | * wpas_p2p_deinit - Deinitialize per-interface P2P data |
| 4030 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4031 | * |
| 4032 | * This function deinitialize per-interface P2P data. |
| 4033 | */ |
| 4034 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) |
| 4035 | { |
| 4036 | if (wpa_s->driver && wpa_s->drv_priv) |
| 4037 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4038 | |
| 4039 | if (wpa_s->go_params) { |
| 4040 | /* Clear any stored provisioning info */ |
| 4041 | p2p_clear_provisioning_info( |
| 4042 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4043 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4044 | } |
| 4045 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4046 | os_free(wpa_s->go_params); |
| 4047 | wpa_s->go_params = NULL; |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 4048 | eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4049 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4050 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4051 | wpa_s->p2p_long_listen = 0; |
| 4052 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 4053 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 4054 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 4055 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4056 | wpas_p2p_listen_work_done(wpa_s); |
| 4057 | if (wpa_s->p2p_send_action_work) { |
| 4058 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 4059 | radio_work_done(wpa_s->p2p_send_action_work); |
| 4060 | wpa_s->p2p_send_action_work = NULL; |
| 4061 | } |
| 4062 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4063 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4064 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 4065 | wpa_s->p2p_oob_dev_pw = NULL; |
| 4066 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4067 | os_free(wpa_s->p2p_group_common_freqs); |
| 4068 | wpa_s->p2p_group_common_freqs = NULL; |
| 4069 | wpa_s->p2p_group_common_freqs_num = 0; |
| 4070 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4071 | /* TODO: remove group interface from the driver if this wpa_s instance |
| 4072 | * is on top of a P2P group interface */ |
| 4073 | } |
| 4074 | |
| 4075 | |
| 4076 | /** |
| 4077 | * wpas_p2p_deinit_global - Deinitialize global P2P module |
| 4078 | * @global: Pointer to global data from wpa_supplicant_init() |
| 4079 | * |
| 4080 | * This function deinitializes the global (per device) P2P module. |
| 4081 | */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4082 | static void wpas_p2p_deinit_global(struct wpa_global *global) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4083 | { |
| 4084 | struct wpa_supplicant *wpa_s, *tmp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4085 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4086 | wpa_s = global->ifaces; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4087 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4088 | wpas_p2p_service_flush(global->p2p_init_wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4089 | |
| 4090 | /* Remove remaining P2P group interfaces */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4091 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
| 4092 | wpa_s = wpa_s->next; |
| 4093 | while (wpa_s) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4094 | tmp = global->ifaces; |
| 4095 | while (tmp && |
| 4096 | (tmp == wpa_s || |
| 4097 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { |
| 4098 | tmp = tmp->next; |
| 4099 | } |
| 4100 | if (tmp == NULL) |
| 4101 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4102 | /* Disconnect from the P2P group and deinit the interface */ |
| 4103 | wpas_p2p_disconnect(tmp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | /* |
| 4107 | * Deinit GO data on any possibly remaining interface (if main |
| 4108 | * interface is used as GO). |
| 4109 | */ |
| 4110 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 4111 | if (wpa_s->ap_iface) |
| 4112 | wpas_p2p_group_deinit(wpa_s); |
| 4113 | } |
| 4114 | |
| 4115 | p2p_deinit(global->p2p); |
| 4116 | global->p2p = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4117 | global->p2p_init_wpa_s = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | |
| 4121 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) |
| 4122 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 4123 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) && |
| 4124 | wpa_s->conf->p2p_no_group_iface) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4125 | return 0; /* separate interface disabled per configuration */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4126 | if (wpa_s->drv_flags & |
| 4127 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | |
| 4128 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) |
| 4129 | return 1; /* P2P group requires a new interface in every case |
| 4130 | */ |
| 4131 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) |
| 4132 | return 0; /* driver does not support concurrent operations */ |
| 4133 | if (wpa_s->global->ifaces->next) |
| 4134 | return 1; /* more that one interface already in use */ |
| 4135 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) |
| 4136 | return 1; /* this interface is already in use */ |
| 4137 | return 0; |
| 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, |
| 4142 | const u8 *peer_addr, |
| 4143 | enum p2p_wps_method wps_method, |
| 4144 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4145 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4146 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4147 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4148 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4149 | persistent_group = 2; |
| 4150 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4151 | /* |
| 4152 | * Increase GO config timeout if HT40 is used since it takes some time |
| 4153 | * to scan channels for coex purposes before the BSS can be started. |
| 4154 | */ |
| 4155 | p2p_set_config_timeout(wpa_s->global->p2p, |
| 4156 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); |
| 4157 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4158 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
| 4159 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4160 | persistent_group, ssid ? ssid->ssid : NULL, |
| 4161 | ssid ? ssid->ssid_len : 0, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4162 | wpa_s->p2p_pd_before_go_neg, pref_freq, |
| 4163 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4164 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | |
| 4168 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, |
| 4169 | const u8 *peer_addr, |
| 4170 | enum p2p_wps_method wps_method, |
| 4171 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4172 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4173 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4174 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4175 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 4176 | persistent_group = 2; |
| 4177 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4178 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
| 4179 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4180 | persistent_group, ssid ? ssid->ssid : NULL, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4181 | ssid ? ssid->ssid_len : 0, pref_freq, |
| 4182 | wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : |
| 4183 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | |
| 4187 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
| 4188 | { |
| 4189 | wpa_s->p2p_join_scan_count++; |
| 4190 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", |
| 4191 | wpa_s->p2p_join_scan_count); |
| 4192 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { |
| 4193 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR |
| 4194 | " for join operationg - stop join attempt", |
| 4195 | MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4196 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4197 | if (wpa_s->p2p_auto_pd) { |
| 4198 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4199 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4200 | P2P_EVENT_PROV_DISC_FAILURE |
| 4201 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4202 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4203 | return; |
| 4204 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4205 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4206 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4211 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
| 4212 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4213 | int res; |
| 4214 | unsigned int num, i; |
| 4215 | struct wpa_used_freq_data *freqs; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4216 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4217 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 4218 | /* Multiple channels are supported and not all are in use */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4219 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4220 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4221 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4222 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 4223 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4224 | if (!freqs) |
| 4225 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4226 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4227 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4228 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4229 | |
| 4230 | for (i = 0; i < num; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4231 | if (freqs[i].freq == freq) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4232 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", |
| 4233 | freq); |
| 4234 | res = 0; |
| 4235 | goto exit_free; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4239 | wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies"); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4240 | res = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4241 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4242 | exit_free: |
| 4243 | os_free(freqs); |
| 4244 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | |
| 4248 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
| 4249 | const u8 *peer_dev_addr) |
| 4250 | { |
| 4251 | struct wpa_bss *bss; |
| 4252 | int updated; |
| 4253 | |
| 4254 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); |
| 4255 | if (bss == NULL) |
| 4256 | return -1; |
| 4257 | if (bss->last_update_idx < wpa_s->bss_update_idx) { |
| 4258 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " |
| 4259 | "last scan"); |
| 4260 | return 0; |
| 4261 | } |
| 4262 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4263 | updated = os_reltime_before(&wpa_s->p2p_auto_started, |
| 4264 | &bss->last_update); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4265 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " |
| 4266 | "%ld.%06ld (%supdated in last scan)", |
| 4267 | bss->last_update.sec, bss->last_update.usec, |
| 4268 | updated ? "": "not "); |
| 4269 | |
| 4270 | return updated; |
| 4271 | } |
| 4272 | |
| 4273 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4274 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
| 4275 | struct wpa_scan_results *scan_res) |
| 4276 | { |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4277 | struct wpa_bss *bss = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4278 | int freq; |
| 4279 | u8 iface_addr[ETH_ALEN]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4280 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4281 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4282 | |
| 4283 | if (wpa_s->global->p2p_disabled) |
| 4284 | return; |
| 4285 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4286 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
| 4287 | scan_res ? (int) scan_res->num : -1, |
| 4288 | wpa_s->p2p_auto_join ? "auto_" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4289 | |
| 4290 | if (scan_res) |
| 4291 | wpas_p2p_scan_res_handler(wpa_s, scan_res); |
| 4292 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4293 | if (wpa_s->p2p_auto_pd) { |
| 4294 | int join = wpas_p2p_peer_go(wpa_s, |
| 4295 | wpa_s->pending_join_dev_addr); |
| 4296 | if (join == 0 && |
| 4297 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { |
| 4298 | wpa_s->auto_pd_scan_retry++; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4299 | bss = wpa_bss_get_bssid_latest( |
| 4300 | wpa_s, wpa_s->pending_join_dev_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4301 | if (bss) { |
| 4302 | freq = bss->freq; |
| 4303 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " |
| 4304 | "the peer " MACSTR " at %d MHz", |
| 4305 | wpa_s->auto_pd_scan_retry, |
| 4306 | MAC2STR(wpa_s-> |
| 4307 | pending_join_dev_addr), |
| 4308 | freq); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4309 | wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4310 | return; |
| 4311 | } |
| 4312 | } |
| 4313 | |
| 4314 | if (join < 0) |
| 4315 | join = 0; |
| 4316 | |
| 4317 | wpa_s->p2p_auto_pd = 0; |
| 4318 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; |
| 4319 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", |
| 4320 | MAC2STR(wpa_s->pending_join_dev_addr), join); |
| 4321 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4322 | wpa_s->pending_join_dev_addr, NULL, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4323 | wpa_s->pending_pd_config_methods, join, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4324 | 0, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4325 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4326 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4327 | P2P_EVENT_PROV_DISC_FAILURE |
| 4328 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4329 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4330 | } |
| 4331 | return; |
| 4332 | } |
| 4333 | |
| 4334 | if (wpa_s->p2p_auto_join) { |
| 4335 | int join = wpas_p2p_peer_go(wpa_s, |
| 4336 | wpa_s->pending_join_dev_addr); |
| 4337 | if (join < 0) { |
| 4338 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " |
| 4339 | "running a GO -> use GO Negotiation"); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4340 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4341 | P2P_EVENT_FALLBACK_TO_GO_NEG |
| 4342 | "reason=peer-not-running-GO"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4343 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, |
| 4344 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, |
| 4345 | wpa_s->p2p_persistent_group, 0, 0, 0, |
| 4346 | wpa_s->p2p_go_intent, |
| 4347 | wpa_s->p2p_connect_freq, |
| 4348 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4349 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4350 | wpa_s->p2p_go_ht40, |
| 4351 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4352 | return; |
| 4353 | } |
| 4354 | |
| 4355 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " |
| 4356 | "try to join the group", join ? "" : |
| 4357 | " in older scan"); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4358 | if (!join) { |
| 4359 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4360 | P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4361 | wpa_s->p2p_fallback_to_go_neg = 1; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4362 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4363 | } |
| 4364 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4365 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4366 | wpa_s->pending_join_iface_addr); |
| 4367 | if (freq < 0 && |
| 4368 | p2p_get_interface_addr(wpa_s->global->p2p, |
| 4369 | wpa_s->pending_join_dev_addr, |
| 4370 | iface_addr) == 0 && |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 4371 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0 |
| 4372 | && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4373 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " |
| 4374 | "address for join from " MACSTR " to " MACSTR |
| 4375 | " based on newly discovered P2P peer entry", |
| 4376 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4377 | MAC2STR(iface_addr)); |
| 4378 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, |
| 4379 | ETH_ALEN); |
| 4380 | |
| 4381 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4382 | wpa_s->pending_join_iface_addr); |
| 4383 | } |
| 4384 | if (freq >= 0) { |
| 4385 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
| 4386 | "from P2P peer table: %d MHz", freq); |
| 4387 | } |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4388 | if (wpa_s->p2p_join_ssid_len) { |
| 4389 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4390 | MACSTR " and SSID %s", |
| 4391 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4392 | wpa_ssid_txt(wpa_s->p2p_join_ssid, |
| 4393 | wpa_s->p2p_join_ssid_len)); |
| 4394 | bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, |
| 4395 | wpa_s->p2p_join_ssid, |
| 4396 | wpa_s->p2p_join_ssid_len); |
| 4397 | } |
| 4398 | if (!bss) { |
| 4399 | wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " |
| 4400 | MACSTR, MAC2STR(wpa_s->pending_join_iface_addr)); |
| 4401 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4402 | wpa_s->pending_join_iface_addr); |
| 4403 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4404 | if (bss) { |
| 4405 | freq = bss->freq; |
| 4406 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4407 | "from BSS table: %d MHz (SSID %s)", freq, |
| 4408 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4409 | } |
| 4410 | if (freq > 0) { |
| 4411 | u16 method; |
| 4412 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4413 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4414 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4415 | P2P_EVENT_GROUP_FORMATION_FAILURE |
| 4416 | "reason=FREQ_CONFLICT"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4417 | return; |
| 4418 | } |
| 4419 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4420 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
| 4421 | "prior to joining an existing group (GO " MACSTR |
| 4422 | " freq=%u MHz)", |
| 4423 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
| 4424 | wpa_s->pending_pd_before_join = 1; |
| 4425 | |
| 4426 | switch (wpa_s->pending_join_wps_method) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4427 | case WPS_PIN_DISPLAY: |
| 4428 | method = WPS_CONFIG_KEYPAD; |
| 4429 | break; |
| 4430 | case WPS_PIN_KEYPAD: |
| 4431 | method = WPS_CONFIG_DISPLAY; |
| 4432 | break; |
| 4433 | case WPS_PBC: |
| 4434 | method = WPS_CONFIG_PUSHBUTTON; |
| 4435 | break; |
| 4436 | default: |
| 4437 | method = 0; |
| 4438 | break; |
| 4439 | } |
| 4440 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4441 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
| 4442 | wpa_s->pending_join_dev_addr) == |
| 4443 | method)) { |
| 4444 | /* |
| 4445 | * We have already performed provision discovery for |
| 4446 | * joining the group. Proceed directly to join |
| 4447 | * operation without duplicated provision discovery. */ |
| 4448 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
| 4449 | "with " MACSTR " already done - proceed to " |
| 4450 | "join", |
| 4451 | MAC2STR(wpa_s->pending_join_dev_addr)); |
| 4452 | wpa_s->pending_pd_before_join = 0; |
| 4453 | goto start; |
| 4454 | } |
| 4455 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4456 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4457 | wpa_s->pending_join_dev_addr, |
| 4458 | NULL, method, 1, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4459 | freq, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4460 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
| 4461 | "Discovery Request before joining an " |
| 4462 | "existing group"); |
| 4463 | wpa_s->pending_pd_before_join = 0; |
| 4464 | goto start; |
| 4465 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4466 | return; |
| 4467 | } |
| 4468 | |
| 4469 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
| 4470 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4471 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4472 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4473 | return; |
| 4474 | |
| 4475 | start: |
| 4476 | /* Start join operation immediately */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4477 | wpas_p2p_join_start(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4481 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, |
| 4482 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4483 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4484 | int ret; |
| 4485 | struct wpa_driver_scan_params params; |
| 4486 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4487 | size_t ielen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4488 | int freqs[2] = { 0, 0 }; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4489 | |
| 4490 | os_memset(¶ms, 0, sizeof(params)); |
| 4491 | |
| 4492 | /* P2P Wildcard SSID */ |
| 4493 | params.num_ssids = 1; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4494 | if (ssid && ssid_len) { |
| 4495 | params.ssids[0].ssid = ssid; |
| 4496 | params.ssids[0].ssid_len = ssid_len; |
| 4497 | os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len); |
| 4498 | wpa_s->p2p_join_ssid_len = ssid_len; |
| 4499 | } else { |
| 4500 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 4501 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 4502 | wpa_s->p2p_join_ssid_len = 0; |
| 4503 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4504 | |
| 4505 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4506 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
| 4507 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, |
| 4508 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4509 | if (wps_ie == NULL) { |
| 4510 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4511 | return; |
| 4512 | } |
| 4513 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4514 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 4515 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4516 | if (ies == NULL) { |
| 4517 | wpabuf_free(wps_ie); |
| 4518 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4519 | return; |
| 4520 | } |
| 4521 | wpabuf_put_buf(ies, wps_ie); |
| 4522 | wpabuf_free(wps_ie); |
| 4523 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4524 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4525 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4526 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4527 | params.extra_ies = wpabuf_head(ies); |
| 4528 | params.extra_ies_len = wpabuf_len(ies); |
Dmitry Shmidt | 9e3f8ee | 2014-01-17 10:52:01 -0800 | [diff] [blame] | 4529 | |
| 4530 | if (!freq) { |
| 4531 | int oper_freq; |
| 4532 | /* |
| 4533 | * If freq is not provided, check the operating freq of the GO |
| 4534 | * and use a single channel scan on if possible. |
| 4535 | */ |
| 4536 | oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4537 | wpa_s->pending_join_iface_addr); |
| 4538 | if (oper_freq > 0) |
| 4539 | freq = oper_freq; |
| 4540 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4541 | if (freq > 0) { |
| 4542 | freqs[0] = freq; |
| 4543 | params.freqs = freqs; |
| 4544 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4545 | |
| 4546 | /* |
| 4547 | * Run a scan to update BSS table and start Provision Discovery once |
| 4548 | * the new scan results become available. |
| 4549 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4550 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4551 | if (!ret) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4552 | os_get_reltime(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4553 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4554 | wpa_s->own_scan_requested = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4555 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4556 | |
| 4557 | wpabuf_free(ies); |
| 4558 | |
| 4559 | if (ret) { |
| 4560 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " |
| 4561 | "try again later"); |
| 4562 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4563 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4564 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4569 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) |
| 4570 | { |
| 4571 | struct wpa_supplicant *wpa_s = eloop_ctx; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4572 | wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4576 | 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] | 4577 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4578 | int auto_join, int op_freq, |
| 4579 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4580 | { |
| 4581 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4582 | MACSTR " dev " MACSTR " op_freq=%d)%s", |
| 4583 | MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4584 | auto_join ? " (auto_join)" : ""); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 4585 | if (ssid && ssid_len) { |
| 4586 | wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s", |
| 4587 | wpa_ssid_txt(ssid, ssid_len)); |
| 4588 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4589 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4590 | wpa_s->p2p_auto_pd = 0; |
| 4591 | wpa_s->p2p_auto_join = !!auto_join; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4592 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
| 4593 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); |
| 4594 | wpa_s->pending_join_wps_method = wps_method; |
| 4595 | |
| 4596 | /* Make sure we are not running find during connection establishment */ |
| 4597 | wpas_p2p_stop_find(wpa_s); |
| 4598 | |
| 4599 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4600 | wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4601 | return 0; |
| 4602 | } |
| 4603 | |
| 4604 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4605 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, |
| 4606 | const u8 *ssid, size_t ssid_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4607 | { |
| 4608 | struct wpa_supplicant *group; |
| 4609 | struct p2p_go_neg_results res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4610 | struct wpa_bss *bss; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4611 | |
| 4612 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
| 4613 | if (group == NULL) |
| 4614 | return -1; |
| 4615 | if (group != wpa_s) { |
| 4616 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
| 4617 | sizeof(group->p2p_pin)); |
| 4618 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 4619 | } else { |
| 4620 | /* |
| 4621 | * Need to mark the current interface for p2p_group_formation |
| 4622 | * when a separate group interface is not used. This is needed |
| 4623 | * to allow p2p_cancel stop a pending p2p_connect-join. |
| 4624 | * wpas_p2p_init_group_interface() addresses this for the case |
| 4625 | * where a separate group interface is used. |
| 4626 | */ |
| 4627 | wpa_s->global->p2p_group_formation = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | group->p2p_in_provisioning = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4631 | 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] | 4632 | |
| 4633 | os_memset(&res, 0, sizeof(res)); |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4634 | 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] | 4635 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, |
| 4636 | ETH_ALEN); |
| 4637 | res.wps_method = wpa_s->pending_join_wps_method; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4638 | if (freq && ssid && ssid_len) { |
| 4639 | res.freq = freq; |
| 4640 | res.ssid_len = ssid_len; |
| 4641 | os_memcpy(res.ssid, ssid, ssid_len); |
| 4642 | } else { |
| 4643 | bss = wpa_bss_get_bssid_latest(wpa_s, |
| 4644 | wpa_s->pending_join_iface_addr); |
| 4645 | if (bss) { |
| 4646 | res.freq = bss->freq; |
| 4647 | res.ssid_len = bss->ssid_len; |
| 4648 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); |
| 4649 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)", |
| 4650 | bss->freq, |
| 4651 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
| 4652 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4653 | } |
| 4654 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4655 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 4656 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " |
| 4657 | "starting client"); |
| 4658 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 4659 | wpa_s->off_channel_freq = 0; |
| 4660 | wpa_s->roc_waiting_drv_freq = 0; |
| 4661 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4662 | wpas_start_wps_enrollee(group, &res); |
| 4663 | |
| 4664 | /* |
| 4665 | * Allow a longer timeout for join-a-running-group than normal 15 |
| 4666 | * second group formation timeout since the GO may not have authorized |
| 4667 | * our connection yet. |
| 4668 | */ |
| 4669 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4670 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, |
| 4671 | wpa_s, NULL); |
| 4672 | |
| 4673 | return 0; |
| 4674 | } |
| 4675 | |
| 4676 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4677 | 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] | 4678 | int *force_freq, int *pref_freq, int go) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4679 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4680 | struct wpa_used_freq_data *freqs; |
| 4681 | int res, best_freq, num_unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4682 | unsigned int freq_in_use = 0, num, i; |
| 4683 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4684 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 4685 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4686 | if (!freqs) |
| 4687 | return -1; |
| 4688 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4689 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4690 | wpa_s->num_multichan_concurrent); |
| 4691 | |
| 4692 | /* |
| 4693 | * It is possible that the total number of used frequencies is bigger |
| 4694 | * than the number of frequencies used for P2P, so get the system wide |
| 4695 | * number of unused frequencies. |
| 4696 | */ |
| 4697 | num_unused = wpas_p2p_num_unused_channels(wpa_s); |
| 4698 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4699 | wpa_printf(MSG_DEBUG, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4700 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d", |
| 4701 | freq, wpa_s->num_multichan_concurrent, num, num_unused); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4702 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4703 | if (freq > 0) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4704 | int ret; |
| 4705 | if (go) |
| 4706 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); |
| 4707 | else |
| 4708 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); |
| 4709 | if (!ret) { |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 4710 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && |
| 4711 | ieee80211_is_dfs(freq)) { |
| 4712 | /* |
| 4713 | * If freq is a DFS channel and DFS is offloaded |
| 4714 | * to the driver, allow P2P GO to use it. |
| 4715 | */ |
| 4716 | wpa_printf(MSG_DEBUG, |
| 4717 | "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver", |
| 4718 | freq); |
| 4719 | } else { |
| 4720 | wpa_printf(MSG_DEBUG, |
| 4721 | "P2P: The forced channel (%u MHz) is not supported for P2P uses", |
| 4722 | freq); |
| 4723 | res = -3; |
| 4724 | goto exit_free; |
| 4725 | } |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4726 | } |
| 4727 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4728 | for (i = 0; i < num; i++) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4729 | if (freqs[i].freq == freq) |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4730 | freq_in_use = 1; |
| 4731 | } |
| 4732 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4733 | if (num_unused <= 0 && !freq_in_use) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4734 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", |
| 4735 | freq); |
| 4736 | res = -2; |
| 4737 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4738 | } |
| 4739 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
| 4740 | "requested channel (%u MHz)", freq); |
| 4741 | *force_freq = freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4742 | goto exit_ok; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4743 | } |
| 4744 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4745 | best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4746 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4747 | /* We have a candidate frequency to use */ |
| 4748 | if (best_freq > 0) { |
| 4749 | if (*pref_freq == 0 && num_unused > 0) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4750 | 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] | 4751 | best_freq); |
| 4752 | *pref_freq = best_freq; |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4753 | } else { |
| 4754 | 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] | 4755 | best_freq); |
| 4756 | *force_freq = best_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4757 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 4758 | } else if (num_unused > 0) { |
| 4759 | wpa_printf(MSG_DEBUG, |
| 4760 | "P2P: Current operating channels are not available for P2P. Try to use another channel"); |
| 4761 | *force_freq = 0; |
| 4762 | } else { |
| 4763 | wpa_printf(MSG_DEBUG, |
| 4764 | "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); |
| 4765 | res = -2; |
| 4766 | goto exit_free; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | exit_ok: |
| 4770 | res = 0; |
| 4771 | exit_free: |
| 4772 | os_free(freqs); |
| 4773 | return res; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4777 | /** |
| 4778 | * wpas_p2p_connect - Request P2P Group Formation to be started |
| 4779 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4780 | * @peer_addr: Address of the peer P2P Device |
| 4781 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode |
| 4782 | * @persistent_group: Whether to create a persistent group |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4783 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4784 | * @join: Whether to join an existing group (as a client) instead of starting |
| 4785 | * Group Owner negotiation; @peer_addr is BSSID in that case |
| 4786 | * @auth: Whether to only authorize the connection instead of doing that and |
| 4787 | * initiating Group Owner negotiation |
| 4788 | * @go_intent: GO Intent or -1 to use default |
| 4789 | * @freq: Frequency for the group or 0 for auto-selection |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4790 | * @persistent_id: Persistent group credentials to use for forcing GO |
| 4791 | * parameters or -1 to generate new values (SSID/passphrase) |
| 4792 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an |
| 4793 | * interoperability workaround when initiating group formation |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4794 | * @ht40: Start GO with 40 MHz channel width |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4795 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4796 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
| 4797 | * failure, -2 on failure due to channel not currently available, |
| 4798 | * -3 if forced channel is not supported |
| 4799 | */ |
| 4800 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4801 | const char *pin, enum p2p_wps_method wps_method, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4802 | int persistent_group, int auto_join, int join, int auth, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4803 | int go_intent, int freq, int persistent_id, int pd, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4804 | int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4805 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4806 | int force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4807 | int ret = 0, res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4808 | enum wpa_driver_if_type iftype; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4809 | const u8 *if_addr; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4810 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4811 | |
| 4812 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4813 | return -1; |
| 4814 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4815 | if (persistent_id >= 0) { |
| 4816 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); |
| 4817 | if (ssid == NULL || ssid->disabled != 2 || |
| 4818 | ssid->mode != WPAS_MODE_P2P_GO) |
| 4819 | return -1; |
| 4820 | } |
| 4821 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4822 | os_free(wpa_s->global->add_psk); |
| 4823 | wpa_s->global->add_psk = NULL; |
| 4824 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4825 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4826 | wpa_s->global->pending_p2ps_group = 0; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4827 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4828 | if (go_intent < 0) |
| 4829 | go_intent = wpa_s->conf->p2p_go_intent; |
| 4830 | |
| 4831 | if (!auth) |
| 4832 | wpa_s->p2p_long_listen = 0; |
| 4833 | |
| 4834 | wpa_s->p2p_wps_method = wps_method; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4835 | wpa_s->p2p_persistent_group = !!persistent_group; |
| 4836 | wpa_s->p2p_persistent_id = persistent_id; |
| 4837 | wpa_s->p2p_go_intent = go_intent; |
| 4838 | wpa_s->p2p_connect_freq = freq; |
| 4839 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4840 | wpa_s->p2p_pd_before_go_neg = !!pd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4841 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4842 | wpa_s->p2p_go_vht = !!vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4843 | |
| 4844 | if (pin) |
| 4845 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); |
| 4846 | else if (wps_method == WPS_PIN_DISPLAY) { |
| 4847 | ret = wps_generate_pin(); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4848 | res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), |
| 4849 | "%08d", ret); |
| 4850 | if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res)) |
| 4851 | wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0'; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4852 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", |
| 4853 | wpa_s->p2p_pin); |
| 4854 | } else |
| 4855 | wpa_s->p2p_pin[0] = '\0'; |
| 4856 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4857 | if (join || auto_join) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4858 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
| 4859 | if (auth) { |
| 4860 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " |
| 4861 | "connect a running group from " MACSTR, |
| 4862 | MAC2STR(peer_addr)); |
| 4863 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 4864 | return ret; |
| 4865 | } |
| 4866 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
| 4867 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
| 4868 | iface_addr) < 0) { |
| 4869 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
| 4870 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
| 4871 | dev_addr); |
| 4872 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4873 | if (auto_join) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4874 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4875 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " |
| 4876 | "%ld.%06ld", |
| 4877 | wpa_s->p2p_auto_started.sec, |
| 4878 | wpa_s->p2p_auto_started.usec); |
| 4879 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4880 | wpa_s->user_initiated_pd = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4881 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4882 | auto_join, freq, NULL, 0) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4883 | return -1; |
| 4884 | return ret; |
| 4885 | } |
| 4886 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4887 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 4888 | go_intent == 15); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4889 | if (res) |
| 4890 | return res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 4891 | wpas_p2p_set_own_freq_preference(wpa_s, |
| 4892 | force_freq ? force_freq : pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4893 | |
| 4894 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 4895 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4896 | if (wpa_s->create_p2p_iface) { |
| 4897 | /* Prepare to add a new interface for the group */ |
| 4898 | iftype = WPA_IF_P2P_GROUP; |
| 4899 | if (go_intent == 15) |
| 4900 | iftype = WPA_IF_P2P_GO; |
| 4901 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 4902 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 4903 | "interface for the group"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4904 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4905 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4906 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4907 | if_addr = wpa_s->pending_interface_addr; |
| 4908 | } else |
| 4909 | if_addr = wpa_s->own_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4910 | |
| 4911 | if (auth) { |
| 4912 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4913 | go_intent, if_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4914 | force_freq, persistent_group, ssid, |
| 4915 | pref_freq) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4916 | return -1; |
| 4917 | return ret; |
| 4918 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4919 | |
| 4920 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, |
| 4921 | go_intent, if_addr, force_freq, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4922 | persistent_group, ssid, pref_freq) < 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4923 | if (wpa_s->create_p2p_iface) |
| 4924 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4925 | return -1; |
| 4926 | } |
| 4927 | return ret; |
| 4928 | } |
| 4929 | |
| 4930 | |
| 4931 | /** |
| 4932 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start |
| 4933 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4934 | * @freq: Frequency of the channel in MHz |
| 4935 | * @duration: Duration of the stay on the channel in milliseconds |
| 4936 | * |
| 4937 | * This callback is called when the driver indicates that it has started the |
| 4938 | * requested remain-on-channel duration. |
| 4939 | */ |
| 4940 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4941 | unsigned int freq, unsigned int duration) |
| 4942 | { |
| 4943 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4944 | return; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 4945 | wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)", |
| 4946 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, |
| 4947 | wpa_s->roc_waiting_drv_freq, freq, duration); |
| 4948 | if (wpa_s->off_channel_freq && |
| 4949 | wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4950 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, |
| 4951 | wpa_s->pending_listen_duration); |
| 4952 | wpa_s->pending_listen_freq = 0; |
Dmitry Shmidt | 7f93d6f | 2014-02-21 11:22:49 -0800 | [diff] [blame] | 4953 | } else { |
| 4954 | wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)", |
| 4955 | wpa_s->off_channel_freq, wpa_s->pending_listen_freq, |
| 4956 | freq, duration); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | |
Jithu Jance | 57cea1a | 2014-08-20 10:22:04 -0700 | [diff] [blame] | 4961 | int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4962 | { |
| 4963 | /* Limit maximum Listen state time based on driver limitation. */ |
| 4964 | if (timeout > wpa_s->max_remain_on_chan) |
| 4965 | timeout = wpa_s->max_remain_on_chan; |
| 4966 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4967 | return p2p_listen(wpa_s->global->p2p, timeout); |
| 4968 | } |
| 4969 | |
| 4970 | |
| 4971 | /** |
| 4972 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout |
| 4973 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4974 | * @freq: Frequency of the channel in MHz |
| 4975 | * |
| 4976 | * This callback is called when the driver indicates that a remain-on-channel |
| 4977 | * operation has been completed, i.e., the duration on the requested channel |
| 4978 | * has timed out. |
| 4979 | */ |
| 4980 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4981 | unsigned int freq) |
| 4982 | { |
| 4983 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
| 4984 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4985 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4986 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4987 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4988 | return; |
Jithu Jance | 57cea1a | 2014-08-20 10:22:04 -0700 | [diff] [blame] | 4989 | if (wpa_s->p2p_long_listen > 0) |
| 4990 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4991 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
| 4992 | return; /* P2P module started a new operation */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4993 | if (offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4994 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4995 | if (wpa_s->p2p_long_listen > 0) { |
| 4996 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); |
| 4997 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4998 | } else { |
| 4999 | /* |
| 5000 | * When listen duration is over, stop listen & update p2p_state |
| 5001 | * to IDLE. |
| 5002 | */ |
| 5003 | p2p_stop_listen(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5004 | } |
| 5005 | } |
| 5006 | |
| 5007 | |
| 5008 | /** |
| 5009 | * wpas_p2p_group_remove - Remove a P2P group |
| 5010 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 5011 | * @ifname: Network interface name of the group interface or "*" to remove all |
| 5012 | * groups |
| 5013 | * Returns: 0 on success, -1 on failure |
| 5014 | * |
| 5015 | * This function is used to remove a P2P group. This can be used to disconnect |
| 5016 | * from a group in which the local end is a P2P Client or to end a P2P Group in |
| 5017 | * case the local end is the Group Owner. If a virtual network interface was |
| 5018 | * created for this group, that interface will be removed. Otherwise, only the |
| 5019 | * configured P2P group network will be removed from the interface. |
| 5020 | */ |
| 5021 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) |
| 5022 | { |
| 5023 | struct wpa_global *global = wpa_s->global; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5024 | struct wpa_supplicant *calling_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5025 | |
| 5026 | if (os_strcmp(ifname, "*") == 0) { |
| 5027 | struct wpa_supplicant *prev; |
| 5028 | wpa_s = global->ifaces; |
| 5029 | while (wpa_s) { |
| 5030 | prev = wpa_s; |
| 5031 | wpa_s = wpa_s->next; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 5032 | if (prev->p2p_group_interface != |
| 5033 | NOT_P2P_GROUP_INTERFACE || |
| 5034 | (prev->current_ssid && |
| 5035 | prev->current_ssid->p2p_group)) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5036 | wpas_p2p_disconnect_safely(prev, calling_wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5037 | } |
| 5038 | return 0; |
| 5039 | } |
| 5040 | |
| 5041 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5042 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5043 | break; |
| 5044 | } |
| 5045 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5046 | return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5050 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) |
| 5051 | { |
| 5052 | unsigned int r; |
| 5053 | |
| 5054 | if (freq == 2) { |
| 5055 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " |
| 5056 | "band"); |
| 5057 | if (wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5058 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5059 | wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5060 | freq = wpa_s->best_24_freq; |
| 5061 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " |
| 5062 | "channel: %d MHz", freq); |
| 5063 | } else { |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5064 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 5065 | return -1; |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5066 | freq = 2412 + (r % 3) * 25; |
| 5067 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " |
| 5068 | "channel: %d MHz", freq); |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | if (freq == 5) { |
| 5073 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " |
| 5074 | "band"); |
| 5075 | if (wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5076 | p2p_supported_freq_go(wpa_s->global->p2p, |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5077 | wpa_s->best_5_freq)) { |
| 5078 | freq = wpa_s->best_5_freq; |
| 5079 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " |
| 5080 | "channel: %d MHz", freq); |
| 5081 | } else { |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5082 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 5083 | return -1; |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5084 | freq = 5180 + (r % 4) * 20; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5085 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5086 | wpa_printf(MSG_DEBUG, "P2P: Could not select " |
| 5087 | "5 GHz channel for P2P group"); |
| 5088 | return -1; |
| 5089 | } |
| 5090 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " |
| 5091 | "channel: %d MHz", freq); |
| 5092 | } |
| 5093 | } |
| 5094 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5095 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5096 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && |
| 5097 | ieee80211_is_dfs(freq)) { |
| 5098 | /* |
| 5099 | * If freq is a DFS channel and DFS is offloaded to the |
| 5100 | * driver, allow P2P GO to use it. |
| 5101 | */ |
| 5102 | wpa_printf(MSG_DEBUG, "P2P: " |
| 5103 | "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded", |
| 5104 | __func__, freq); |
| 5105 | return freq; |
| 5106 | } |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5107 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " |
| 5108 | "(%u MHz) is not supported for P2P uses", |
| 5109 | freq); |
| 5110 | return -1; |
| 5111 | } |
| 5112 | |
| 5113 | return freq; |
| 5114 | } |
| 5115 | |
| 5116 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5117 | static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s, |
| 5118 | struct p2p_go_neg_results *params, |
| 5119 | const struct p2p_channels *channels) |
| 5120 | { |
| 5121 | unsigned int i, r; |
| 5122 | |
| 5123 | /* first try some random selection of the social channels */ |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5124 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 5125 | return -1; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5126 | |
| 5127 | for (i = 0; i < 3; i++) { |
| 5128 | params->freq = 2412 + ((r + i) % 3) * 25; |
| 5129 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5130 | freq_included(channels, params->freq) && |
| 5131 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5132 | goto out; |
| 5133 | } |
| 5134 | |
| 5135 | /* try all channels in reg. class 81 */ |
| 5136 | for (i = 0; i < 11; i++) { |
| 5137 | params->freq = 2412 + i * 5; |
| 5138 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5139 | freq_included(channels, params->freq) && |
| 5140 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5141 | goto out; |
| 5142 | } |
| 5143 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5144 | /* try all channels in operating class 115 */ |
| 5145 | for (i = 0; i < 4; i++) { |
| 5146 | params->freq = 5180 + i * 20; |
| 5147 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5148 | freq_included(channels, params->freq) && |
| 5149 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
| 5150 | goto out; |
| 5151 | } |
| 5152 | |
| 5153 | /* try all channels in operating class 124 */ |
| 5154 | for (i = 0; i < 4; i++) { |
| 5155 | params->freq = 5745 + i * 20; |
| 5156 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5157 | freq_included(channels, params->freq) && |
| 5158 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
| 5159 | goto out; |
| 5160 | } |
| 5161 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5162 | /* try social channel class 180 channel 2 */ |
| 5163 | params->freq = 58320 + 1 * 2160; |
| 5164 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5165 | freq_included(channels, params->freq) && |
| 5166 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
| 5167 | goto out; |
| 5168 | |
| 5169 | /* try all channels in reg. class 180 */ |
| 5170 | for (i = 0; i < 4; i++) { |
| 5171 | params->freq = 58320 + i * 2160; |
| 5172 | if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && |
| 5173 | freq_included(channels, params->freq) && |
| 5174 | p2p_supported_freq(wpa_s->global->p2p, params->freq)) |
| 5175 | goto out; |
| 5176 | } |
| 5177 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5178 | wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed"); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5179 | return -1; |
| 5180 | out: |
| 5181 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)", |
| 5182 | params->freq); |
| 5183 | return 0; |
| 5184 | } |
| 5185 | |
| 5186 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5187 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
| 5188 | struct p2p_go_neg_results *params, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5189 | int freq, int ht40, int vht, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5190 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5191 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5192 | struct wpa_used_freq_data *freqs; |
| 5193 | unsigned int pref_freq, cand_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5194 | unsigned int num, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5195 | |
| 5196 | os_memset(params, 0, sizeof(*params)); |
| 5197 | params->role_go = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5198 | params->ht40 = ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5199 | params->vht = vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5200 | if (freq) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5201 | if (!freq_included(channels, freq)) { |
| 5202 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 5203 | "accepted", freq); |
| 5204 | return -1; |
| 5205 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5206 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " |
| 5207 | "frequency %d MHz", freq); |
| 5208 | params->freq = freq; |
| 5209 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
| 5210 | wpa_s->conf->p2p_oper_channel >= 1 && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5211 | wpa_s->conf->p2p_oper_channel <= 11 && |
| 5212 | freq_included(channels, |
| 5213 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5214 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
| 5215 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 5216 | "frequency %d MHz", params->freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5217 | } else if ((wpa_s->conf->p2p_oper_reg_class == 115 || |
| 5218 | wpa_s->conf->p2p_oper_reg_class == 116 || |
| 5219 | wpa_s->conf->p2p_oper_reg_class == 117 || |
| 5220 | wpa_s->conf->p2p_oper_reg_class == 124 || |
| 5221 | wpa_s->conf->p2p_oper_reg_class == 126 || |
| 5222 | wpa_s->conf->p2p_oper_reg_class == 127) && |
| 5223 | freq_included(channels, |
| 5224 | 5000 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5225 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
| 5226 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 5227 | "frequency %d MHz", params->freq); |
| 5228 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5229 | wpa_s->best_overall_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5230 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5231 | wpa_s->best_overall_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5232 | freq_included(channels, wpa_s->best_overall_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5233 | params->freq = wpa_s->best_overall_freq; |
| 5234 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " |
| 5235 | "channel %d MHz", params->freq); |
| 5236 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5237 | wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5238 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5239 | wpa_s->best_24_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5240 | freq_included(channels, wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5241 | params->freq = wpa_s->best_24_freq; |
| 5242 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " |
| 5243 | "channel %d MHz", params->freq); |
| 5244 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 5245 | wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5246 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 5247 | wpa_s->best_5_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5248 | freq_included(channels, wpa_s->best_5_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5249 | params->freq = wpa_s->best_5_freq; |
| 5250 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " |
| 5251 | "channel %d MHz", params->freq); |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 5252 | } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p, |
| 5253 | channels))) { |
| 5254 | params->freq = pref_freq; |
| 5255 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " |
| 5256 | "channels", params->freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5257 | } else { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5258 | /* no preference, select some channel */ |
| 5259 | if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5260 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5261 | } |
| 5262 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5263 | freqs = os_calloc(wpa_s->num_multichan_concurrent, |
| 5264 | sizeof(struct wpa_used_freq_data)); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5265 | if (!freqs) |
| 5266 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5267 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5268 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5269 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5270 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5271 | cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); |
| 5272 | |
| 5273 | /* First try the best used frequency if possible */ |
| 5274 | if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) { |
| 5275 | params->freq = cand_freq; |
| 5276 | } else if (!freq) { |
| 5277 | /* Try any of the used frequencies */ |
| 5278 | for (i = 0; i < num; i++) { |
| 5279 | if (freq_included(channels, freqs[i].freq)) { |
| 5280 | wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)", |
| 5281 | freqs[i].freq); |
| 5282 | params->freq = freqs[i].freq; |
| 5283 | break; |
| 5284 | } |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5285 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5286 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 5287 | if (i == num) { |
| 5288 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5289 | 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] | 5290 | os_free(freqs); |
| 5291 | return -1; |
| 5292 | } else { |
| 5293 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels"); |
| 5294 | } |
| 5295 | } |
| 5296 | } else { |
| 5297 | for (i = 0; i < num; i++) { |
| 5298 | if (freqs[i].freq == freq) |
| 5299 | break; |
| 5300 | } |
| 5301 | |
| 5302 | if (i == num) { |
| 5303 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
| 5304 | if (freq) |
| 5305 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq); |
| 5306 | os_free(freqs); |
| 5307 | return -1; |
| 5308 | } else { |
| 5309 | wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels"); |
| 5310 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 5311 | } |
| 5312 | } |
| 5313 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5314 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5315 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | |
| 5319 | static struct wpa_supplicant * |
| 5320 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 5321 | int go) |
| 5322 | { |
| 5323 | struct wpa_supplicant *group_wpa_s; |
| 5324 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5325 | if (!wpas_p2p_create_iface(wpa_s)) { |
| 5326 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group " |
| 5327 | "operations"); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5328 | wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5329 | return wpa_s; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5330 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5331 | |
| 5332 | 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] | 5333 | WPA_IF_P2P_CLIENT) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5334 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5335 | "P2P: Failed to add group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5336 | return NULL; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5337 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5338 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); |
| 5339 | if (group_wpa_s == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5340 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 5341 | "P2P: Failed to initialize group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5342 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5343 | return NULL; |
| 5344 | } |
| 5345 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 5346 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", |
| 5347 | group_wpa_s->ifname); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5348 | group_wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5349 | return group_wpa_s; |
| 5350 | } |
| 5351 | |
| 5352 | |
| 5353 | /** |
| 5354 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner |
| 5355 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 5356 | * @persistent_group: Whether to create a persistent group |
| 5357 | * @freq: Frequency for the group or 0 to indicate no hardcoding |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5358 | * @ht40: Start GO with 40 MHz channel width |
| 5359 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5360 | * Returns: 0 on success, -1 on failure |
| 5361 | * |
| 5362 | * This function creates a new P2P group with the local end as the Group Owner, |
| 5363 | * i.e., without using Group Owner Negotiation. |
| 5364 | */ |
| 5365 | 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] | 5366 | int freq, int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5367 | { |
| 5368 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5369 | |
| 5370 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5371 | return -1; |
| 5372 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5373 | os_free(wpa_s->global->add_psk); |
| 5374 | wpa_s->global->add_psk = NULL; |
| 5375 | |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5376 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5377 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5378 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 5379 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5380 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 5381 | if (freq < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5382 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5383 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5384 | 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] | 5385 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5386 | if (params.freq && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5387 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5388 | if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && |
| 5389 | ieee80211_is_dfs(params.freq)) { |
| 5390 | /* |
| 5391 | * If freq is a DFS channel and DFS is offloaded to the |
| 5392 | * driver, allow P2P GO to use it. |
| 5393 | */ |
| 5394 | wpa_printf(MSG_DEBUG, |
| 5395 | "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver", |
| 5396 | __func__, params.freq); |
| 5397 | } else { |
| 5398 | wpa_printf(MSG_DEBUG, |
| 5399 | "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses", |
| 5400 | params.freq); |
| 5401 | return -1; |
| 5402 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5403 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5404 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
| 5405 | params.persistent_group = persistent_group; |
| 5406 | |
| 5407 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); |
| 5408 | if (wpa_s == NULL) |
| 5409 | return -1; |
| 5410 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5411 | |
| 5412 | return 0; |
| 5413 | } |
| 5414 | |
| 5415 | |
| 5416 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5417 | struct wpa_ssid *params, int addr_allocated, |
| 5418 | int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5419 | { |
| 5420 | struct wpa_ssid *ssid; |
| 5421 | |
| 5422 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); |
| 5423 | if (wpa_s == NULL) |
| 5424 | return -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5425 | wpa_s->p2p_last_4way_hs_fail = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5426 | |
| 5427 | wpa_supplicant_ap_deinit(wpa_s); |
| 5428 | |
| 5429 | ssid = wpa_config_add_network(wpa_s->conf); |
| 5430 | if (ssid == NULL) |
| 5431 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5432 | wpa_config_set_network_defaults(ssid); |
| 5433 | ssid->temporary = 1; |
| 5434 | ssid->proto = WPA_PROTO_RSN; |
| 5435 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
| 5436 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 5437 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 5438 | ssid->ssid = os_malloc(params->ssid_len); |
| 5439 | if (ssid->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5440 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 5441 | return -1; |
| 5442 | } |
| 5443 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 5444 | ssid->ssid_len = params->ssid_len; |
| 5445 | ssid->p2p_group = 1; |
| 5446 | ssid->export_keys = 1; |
| 5447 | if (params->psk_set) { |
| 5448 | os_memcpy(ssid->psk, params->psk, 32); |
| 5449 | ssid->psk_set = 1; |
| 5450 | } |
| 5451 | if (params->passphrase) |
| 5452 | ssid->passphrase = os_strdup(params->passphrase); |
| 5453 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5454 | wpa_s->show_group_started = 1; |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5455 | wpa_s->p2p_in_invitation = 1; |
| 5456 | wpa_s->p2p_invite_go_freq = freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5457 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5458 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5459 | NULL); |
| 5460 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5461 | wpas_p2p_group_formation_timeout, |
| 5462 | wpa_s->parent, NULL); |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 5463 | wpa_supplicant_select_network(wpa_s, ssid); |
| 5464 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5465 | return 0; |
| 5466 | } |
| 5467 | |
| 5468 | |
| 5469 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, |
| 5470 | struct wpa_ssid *ssid, int addr_allocated, |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5471 | int force_freq, int neg_freq, int ht40, |
| 5472 | int vht, const struct p2p_channels *channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5473 | int connection_timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5474 | { |
| 5475 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 96be622 | 2014-02-13 10:16:51 -0800 | [diff] [blame] | 5476 | int go = 0, freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5477 | |
| 5478 | if (ssid->disabled != 2 || ssid->ssid == NULL) |
| 5479 | return -1; |
| 5480 | |
| 5481 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
| 5482 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { |
| 5483 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " |
| 5484 | "already running"); |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5488 | os_free(wpa_s->global->add_psk); |
| 5489 | wpa_s->global->add_psk = NULL; |
| 5490 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5491 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5492 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5493 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5494 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5495 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5496 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5497 | if (force_freq > 0) { |
| 5498 | freq = wpas_p2p_select_go_freq(wpa_s, force_freq); |
| 5499 | if (freq < 0) |
| 5500 | return -1; |
| 5501 | } else { |
| 5502 | freq = wpas_p2p_select_go_freq(wpa_s, neg_freq); |
| 5503 | if (freq < 0 || |
| 5504 | (freq > 0 && !freq_included(channels, freq))) |
| 5505 | freq = 0; |
| 5506 | } |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame^] | 5507 | } else if (ssid->mode == WPAS_MODE_INFRA) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5508 | freq = neg_freq; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame^] | 5509 | if (freq <= 0 || !freq_included(channels, freq)) { |
| 5510 | struct os_reltime now; |
| 5511 | struct wpa_bss *bss = |
| 5512 | wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid); |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5513 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame^] | 5514 | os_get_reltime(&now); |
| 5515 | if (bss && |
| 5516 | !os_reltime_expired(&now, &bss->last_update, 5) && |
| 5517 | freq_included(channels, bss->freq)) |
| 5518 | freq = bss->freq; |
| 5519 | else |
| 5520 | freq = 0; |
| 5521 | } |
| 5522 | |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5523 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame^] | 5524 | } else { |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5525 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame^] | 5526 | } |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 5527 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5528 | 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] | 5529 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5530 | |
| 5531 | params.role_go = 1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5532 | params.psk_set = ssid->psk_set; |
| 5533 | if (params.psk_set) |
| 5534 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5535 | if (ssid->passphrase) { |
| 5536 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { |
| 5537 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " |
| 5538 | "persistent group"); |
| 5539 | return -1; |
| 5540 | } |
| 5541 | os_strlcpy(params.passphrase, ssid->passphrase, |
| 5542 | sizeof(params.passphrase)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5543 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5544 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); |
| 5545 | params.ssid_len = ssid->ssid_len; |
| 5546 | params.persistent_group = 1; |
| 5547 | |
| 5548 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); |
| 5549 | if (wpa_s == NULL) |
| 5550 | return -1; |
| 5551 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5552 | p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS); |
| 5553 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5554 | wpa_s->p2p_first_connection_timeout = connection_timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5555 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5556 | |
| 5557 | return 0; |
| 5558 | } |
| 5559 | |
| 5560 | |
| 5561 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, |
| 5562 | struct wpabuf *proberesp_ies) |
| 5563 | { |
| 5564 | struct wpa_supplicant *wpa_s = ctx; |
| 5565 | if (wpa_s->ap_iface) { |
| 5566 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5567 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
| 5568 | wpabuf_free(beacon_ies); |
| 5569 | wpabuf_free(proberesp_ies); |
| 5570 | return; |
| 5571 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5572 | if (beacon_ies) { |
| 5573 | wpabuf_free(hapd->p2p_beacon_ie); |
| 5574 | hapd->p2p_beacon_ie = beacon_ies; |
| 5575 | } |
| 5576 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 5577 | hapd->p2p_probe_resp_ie = proberesp_ies; |
| 5578 | } else { |
| 5579 | wpabuf_free(beacon_ies); |
| 5580 | wpabuf_free(proberesp_ies); |
| 5581 | } |
| 5582 | wpa_supplicant_ap_update_beacon(wpa_s); |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | static void wpas_p2p_idle_update(void *ctx, int idle) |
| 5587 | { |
| 5588 | struct wpa_supplicant *wpa_s = ctx; |
| 5589 | if (!wpa_s->ap_iface) |
| 5590 | return; |
| 5591 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5592 | if (idle) { |
| 5593 | if (wpa_s->global->p2p_fail_on_wps_complete && |
| 5594 | wpa_s->p2p_in_provisioning) { |
| 5595 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5596 | return; |
| 5597 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5598 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5599 | } else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5600 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 5601 | } |
| 5602 | |
| 5603 | |
| 5604 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5605 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5606 | { |
| 5607 | struct p2p_group *group; |
| 5608 | struct p2p_group_config *cfg; |
| 5609 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5610 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5611 | return NULL; |
| 5612 | |
| 5613 | cfg = os_zalloc(sizeof(*cfg)); |
| 5614 | if (cfg == NULL) |
| 5615 | return NULL; |
| 5616 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5617 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5618 | cfg->persistent_group = 2; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5619 | else if (ssid->p2p_persistent_group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5620 | cfg->persistent_group = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5621 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
| 5622 | if (wpa_s->max_stations && |
| 5623 | wpa_s->max_stations < wpa_s->conf->max_num_sta) |
| 5624 | cfg->max_clients = wpa_s->max_stations; |
| 5625 | else |
| 5626 | cfg->max_clients = wpa_s->conf->max_num_sta; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5627 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); |
| 5628 | cfg->ssid_len = ssid->ssid_len; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 5629 | cfg->freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5630 | cfg->cb_ctx = wpa_s; |
| 5631 | cfg->ie_update = wpas_p2p_ie_update; |
| 5632 | cfg->idle_update = wpas_p2p_idle_update; |
| 5633 | |
| 5634 | group = p2p_group_init(wpa_s->global->p2p, cfg); |
| 5635 | if (group == NULL) |
| 5636 | os_free(cfg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5637 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5638 | p2p_group_notif_formation_done(group); |
| 5639 | wpa_s->p2p_group = group; |
| 5640 | return group; |
| 5641 | } |
| 5642 | |
| 5643 | |
| 5644 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 5645 | int registrar) |
| 5646 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5647 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5648 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5649 | if (!wpa_s->p2p_in_provisioning) { |
| 5650 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " |
| 5651 | "provisioning not in progress"); |
| 5652 | return; |
| 5653 | } |
| 5654 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5655 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5656 | u8 go_dev_addr[ETH_ALEN]; |
| 5657 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); |
| 5658 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5659 | ssid->ssid_len); |
| 5660 | /* Clear any stored provisioning info */ |
| 5661 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); |
| 5662 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5663 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5664 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5665 | NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5666 | wpa_s->p2p_go_group_formation_completed = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5667 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5668 | /* |
| 5669 | * Use a separate timeout for initial data connection to |
| 5670 | * complete to allow the group to be removed automatically if |
| 5671 | * something goes wrong in this step before the P2P group idle |
| 5672 | * timeout mechanism is taken into use. |
| 5673 | */ |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5674 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5675 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", |
| 5676 | P2P_MAX_INITIAL_CONN_WAIT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5677 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5678 | wpas_p2p_group_formation_timeout, |
| 5679 | wpa_s->parent, NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5680 | } else if (ssid) { |
| 5681 | /* |
| 5682 | * Use a separate timeout for initial data connection to |
| 5683 | * complete to allow the group to be removed automatically if |
| 5684 | * the client does not complete data connection successfully. |
| 5685 | */ |
| 5686 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5687 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", |
| 5688 | P2P_MAX_INITIAL_CONN_WAIT_GO); |
| 5689 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, |
| 5690 | wpas_p2p_group_formation_timeout, |
| 5691 | wpa_s->parent, NULL); |
| 5692 | /* |
| 5693 | * Complete group formation on first successful data connection |
| 5694 | */ |
| 5695 | wpa_s->p2p_go_group_formation_completed = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5696 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5697 | if (wpa_s->global->p2p) |
| 5698 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5699 | wpas_group_formation_completed(wpa_s, 1); |
| 5700 | } |
| 5701 | |
| 5702 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5703 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
| 5704 | struct wps_event_fail *fail) |
| 5705 | { |
| 5706 | if (!wpa_s->p2p_in_provisioning) { |
| 5707 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " |
| 5708 | "provisioning not in progress"); |
| 5709 | return; |
| 5710 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5711 | |
| 5712 | if (wpa_s->go_params) { |
| 5713 | p2p_clear_provisioning_info( |
| 5714 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5715 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5716 | } |
| 5717 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5718 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5719 | |
| 5720 | if (wpa_s == wpa_s->global->p2p_group_formation) { |
| 5721 | /* |
| 5722 | * Allow some time for the failed WPS negotiation exchange to |
| 5723 | * complete, but remove the group since group formation cannot |
| 5724 | * succeed after provisioning failure. |
| 5725 | */ |
| 5726 | wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); |
| 5727 | wpa_s->global->p2p_fail_on_wps_complete = 1; |
| 5728 | eloop_deplete_timeout(0, 50000, |
| 5729 | wpas_p2p_group_formation_timeout, |
| 5730 | wpa_s->parent, NULL); |
| 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | |
| 5735 | int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) |
| 5736 | { |
| 5737 | if (!wpa_s->global->p2p_fail_on_wps_complete || |
| 5738 | !wpa_s->p2p_in_provisioning) |
| 5739 | return 0; |
| 5740 | |
| 5741 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5742 | |
| 5743 | return 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5747 | 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] | 5748 | const char *config_method, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5749 | enum wpas_p2p_prov_disc_use use, |
| 5750 | struct p2ps_provision *p2ps_prov) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5751 | { |
| 5752 | u16 config_methods; |
| 5753 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5754 | wpa_s->global->pending_p2ps_group = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5755 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5756 | wpa_s->pending_pd_use = NORMAL_PD; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5757 | if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) { |
| 5758 | p2ps_prov->conncap = p2ps_group_capability( |
| 5759 | wpa_s, P2PS_SETUP_NONE, p2ps_prov->role); |
| 5760 | wpa_printf(MSG_DEBUG, |
| 5761 | "P2P: %s conncap: %d - ASP parsed: %x %x %d %s", |
| 5762 | __func__, p2ps_prov->conncap, |
| 5763 | p2ps_prov->adv_id, p2ps_prov->conncap, |
| 5764 | p2ps_prov->status, p2ps_prov->info); |
| 5765 | |
| 5766 | config_methods = 0; |
| 5767 | } else if (os_strncmp(config_method, "display", 7) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5768 | config_methods = WPS_CONFIG_DISPLAY; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5769 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5770 | config_methods = WPS_CONFIG_KEYPAD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5771 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
| 5772 | os_strncmp(config_method, "pushbutton", 10) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5773 | config_methods = WPS_CONFIG_PUSHBUTTON; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5774 | else { |
| 5775 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5776 | os_free(p2ps_prov); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5777 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5778 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5779 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5780 | if (use == WPAS_P2P_PD_AUTO) { |
| 5781 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); |
| 5782 | wpa_s->pending_pd_config_methods = config_methods; |
| 5783 | wpa_s->p2p_auto_pd = 1; |
| 5784 | wpa_s->p2p_auto_join = 0; |
| 5785 | wpa_s->pending_pd_before_join = 0; |
| 5786 | wpa_s->auto_pd_scan_retry = 0; |
| 5787 | wpas_p2p_stop_find(wpa_s); |
| 5788 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 5789 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5790 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", |
| 5791 | wpa_s->p2p_auto_started.sec, |
| 5792 | wpa_s->p2p_auto_started.usec); |
| 5793 | wpas_p2p_join_scan(wpa_s, NULL); |
| 5794 | return 0; |
| 5795 | } |
| 5796 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5797 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) { |
| 5798 | os_free(p2ps_prov); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5799 | return -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5800 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5801 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5802 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5803 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5804 | 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | |
| 5808 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, |
| 5809 | char *end) |
| 5810 | { |
| 5811 | return p2p_scan_result_text(ies, ies_len, buf, end); |
| 5812 | } |
| 5813 | |
| 5814 | |
| 5815 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
| 5816 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5817 | if (!offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5818 | return; |
| 5819 | |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 5820 | wpas_p2p_action_tx_clear(wpa_s); |
| 5821 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5822 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " |
| 5823 | "operation request"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5824 | offchannel_clear_pending_action_tx(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5825 | } |
| 5826 | |
| 5827 | |
| 5828 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
| 5829 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5830 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5831 | const u8 *dev_id, unsigned int search_delay, |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5832 | u8 seek_cnt, const char **seek_string, int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5833 | { |
| 5834 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5835 | wpa_s->p2p_long_listen = 0; |
| 5836 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5837 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || |
| 5838 | wpa_s->p2p_in_provisioning) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5839 | return -1; |
| 5840 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5841 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 5842 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5843 | return p2p_find(wpa_s->global->p2p, timeout, type, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5844 | num_req_dev_types, req_dev_types, dev_id, |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 5845 | search_delay, seek_cnt, seek_string, freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5849 | static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s, |
| 5850 | struct wpa_scan_results *scan_res) |
| 5851 | { |
| 5852 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 5853 | |
| 5854 | if (wpa_s->p2p_scan_work) { |
| 5855 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; |
| 5856 | wpa_s->p2p_scan_work = NULL; |
| 5857 | radio_work_done(work); |
| 5858 | } |
| 5859 | |
| 5860 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5861 | return; |
| 5862 | |
| 5863 | /* |
| 5864 | * Indicate that results have been processed so that the P2P module can |
| 5865 | * continue pending tasks. |
| 5866 | */ |
| 5867 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5872 | { |
| 5873 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5874 | wpa_s->p2p_long_listen = 0; |
| 5875 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5876 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5877 | |
| 5878 | if (wpa_s->global->p2p) |
| 5879 | p2p_stop_find(wpa_s->global->p2p); |
| 5880 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5881 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) { |
| 5882 | wpa_printf(MSG_DEBUG, |
| 5883 | "P2P: Do not consider the scan results after stop_find"); |
| 5884 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search; |
| 5885 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | |
| 5889 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) |
| 5890 | { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 5891 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 5892 | if (!wpa_s->global->pending_group_iface_for_p2ps) |
| 5893 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | |
| 5897 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5898 | { |
| 5899 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5900 | wpa_s->p2p_long_listen = 0; |
| 5901 | } |
| 5902 | |
| 5903 | |
| 5904 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) |
| 5905 | { |
| 5906 | int res; |
| 5907 | |
| 5908 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5909 | return -1; |
| 5910 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5911 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5912 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5913 | |
| 5914 | if (timeout == 0) { |
| 5915 | /* |
| 5916 | * This is a request for unlimited Listen state. However, at |
| 5917 | * least for now, this is mapped to a Listen state for one |
| 5918 | * hour. |
| 5919 | */ |
| 5920 | timeout = 3600; |
| 5921 | } |
| 5922 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5923 | wpa_s->p2p_long_listen = 0; |
| 5924 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5925 | /* |
| 5926 | * Stop previous find/listen operation to avoid trying to request a new |
| 5927 | * remain-on-channel operation while the driver is still running the |
| 5928 | * previous one. |
| 5929 | */ |
| 5930 | if (wpa_s->global->p2p) |
| 5931 | p2p_stop_find(wpa_s->global->p2p); |
| 5932 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5933 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
| 5934 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { |
| 5935 | wpa_s->p2p_long_listen = timeout * 1000; |
| 5936 | eloop_register_timeout(timeout, 0, |
| 5937 | wpas_p2p_long_listen_timeout, |
| 5938 | wpa_s, NULL); |
| 5939 | } |
| 5940 | |
| 5941 | return res; |
| 5942 | } |
| 5943 | |
| 5944 | |
| 5945 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 5946 | u8 *buf, size_t len, int p2p_group) |
| 5947 | { |
| 5948 | struct wpabuf *p2p_ie; |
| 5949 | int ret; |
| 5950 | |
| 5951 | if (wpa_s->global->p2p_disabled) |
| 5952 | return -1; |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 5953 | if (wpa_s->conf->p2p_disabled) |
| 5954 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5955 | if (wpa_s->global->p2p == NULL) |
| 5956 | return -1; |
| 5957 | if (bss == NULL) |
| 5958 | return -1; |
| 5959 | |
| 5960 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
| 5961 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, |
| 5962 | p2p_group, p2p_ie); |
| 5963 | wpabuf_free(p2p_ie); |
| 5964 | |
| 5965 | return ret; |
| 5966 | } |
| 5967 | |
| 5968 | |
| 5969 | 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] | 5970 | const u8 *dst, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5971 | const u8 *ie, size_t ie_len, int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5972 | { |
| 5973 | if (wpa_s->global->p2p_disabled) |
| 5974 | return 0; |
| 5975 | if (wpa_s->global->p2p == NULL) |
| 5976 | return 0; |
| 5977 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5978 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
| 5979 | ie, ie_len)) { |
| 5980 | case P2P_PREQ_NOT_P2P: |
| 5981 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, |
| 5982 | ssi_signal); |
| 5983 | /* fall through */ |
| 5984 | case P2P_PREQ_MALFORMED: |
| 5985 | case P2P_PREQ_NOT_LISTEN: |
| 5986 | case P2P_PREQ_NOT_PROCESSED: |
| 5987 | default: /* make gcc happy */ |
| 5988 | return 0; |
| 5989 | case P2P_PREQ_PROCESSED: |
| 5990 | return 1; |
| 5991 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | |
| 5995 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, |
| 5996 | const u8 *sa, const u8 *bssid, |
| 5997 | u8 category, const u8 *data, size_t len, int freq) |
| 5998 | { |
| 5999 | if (wpa_s->global->p2p_disabled) |
| 6000 | return; |
| 6001 | if (wpa_s->global->p2p == NULL) |
| 6002 | return; |
| 6003 | |
| 6004 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, |
| 6005 | freq); |
| 6006 | } |
| 6007 | |
| 6008 | |
| 6009 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) |
| 6010 | { |
| 6011 | if (wpa_s->global->p2p_disabled) |
| 6012 | return; |
| 6013 | if (wpa_s->global->p2p == NULL) |
| 6014 | return; |
| 6015 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6016 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6017 | } |
| 6018 | |
| 6019 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6020 | static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6021 | { |
| 6022 | p2p_group_deinit(wpa_s->p2p_group); |
| 6023 | wpa_s->p2p_group = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6024 | |
| 6025 | wpa_s->ap_configured_cb = NULL; |
| 6026 | wpa_s->ap_configured_cb_ctx = NULL; |
| 6027 | wpa_s->ap_configured_cb_data = NULL; |
| 6028 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | |
| 6032 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) |
| 6033 | { |
| 6034 | wpa_s->p2p_long_listen = 0; |
| 6035 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6036 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6037 | return -1; |
| 6038 | |
| 6039 | return p2p_reject(wpa_s->global->p2p, addr); |
| 6040 | } |
| 6041 | |
| 6042 | |
| 6043 | /* Invite to reinvoke a persistent group */ |
| 6044 | 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] | 6045 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6046 | int ht40, int vht, int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6047 | { |
| 6048 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 6049 | u8 *bssid = NULL; |
| 6050 | int force_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6051 | int res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 6052 | int no_pref_freq_given = pref_freq == 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6053 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 6054 | wpa_s->global->p2p_invite_group = NULL; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6055 | if (peer_addr) |
| 6056 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 6057 | else |
| 6058 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6059 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 6060 | wpa_s->p2p_persistent_go_freq = freq; |
| 6061 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6062 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 6063 | role = P2P_INVITE_ROLE_GO; |
| 6064 | if (peer_addr == NULL) { |
| 6065 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " |
| 6066 | "address in invitation command"); |
| 6067 | return -1; |
| 6068 | } |
| 6069 | if (wpas_p2p_create_iface(wpa_s)) { |
| 6070 | if (wpas_p2p_add_group_interface(wpa_s, |
| 6071 | WPA_IF_P2P_GO) < 0) { |
| 6072 | wpa_printf(MSG_ERROR, "P2P: Failed to " |
| 6073 | "allocate a new interface for the " |
| 6074 | "group"); |
| 6075 | return -1; |
| 6076 | } |
| 6077 | bssid = wpa_s->pending_interface_addr; |
| 6078 | } else |
| 6079 | bssid = wpa_s->own_addr; |
| 6080 | } else { |
| 6081 | role = P2P_INVITE_ROLE_CLIENT; |
| 6082 | peer_addr = ssid->bssid; |
| 6083 | } |
| 6084 | wpa_s->pending_invite_ssid_id = ssid->id; |
| 6085 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6086 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 6087 | role == P2P_INVITE_ROLE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6088 | if (res) |
| 6089 | return res; |
Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 6090 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6091 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6092 | return -1; |
| 6093 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 6094 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 6095 | no_pref_freq_given && pref_freq > 0 && |
| 6096 | wpa_s->num_multichan_concurrent > 1 && |
| 6097 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 6098 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", |
| 6099 | pref_freq); |
| 6100 | pref_freq = 0; |
| 6101 | } |
| 6102 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 6103 | /* |
| 6104 | * Stop any find/listen operations before invitation and possibly |
| 6105 | * connection establishment. |
| 6106 | */ |
| 6107 | wpas_p2p_stop_find_oper(wpa_s); |
| 6108 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6109 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6110 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6111 | 1, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | |
| 6115 | /* Invite to join an active group */ |
| 6116 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, |
| 6117 | const u8 *peer_addr, const u8 *go_dev_addr) |
| 6118 | { |
| 6119 | struct wpa_global *global = wpa_s->global; |
| 6120 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 6121 | u8 *bssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6122 | struct wpa_ssid *ssid; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6123 | int persistent; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 6124 | int freq = 0, force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6125 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6126 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 6127 | wpa_s->p2p_persistent_go_freq = 0; |
| 6128 | wpa_s->p2p_go_ht40 = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6129 | wpa_s->p2p_go_vht = 0; |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 6130 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6131 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6132 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 6133 | break; |
| 6134 | } |
| 6135 | if (wpa_s == NULL) { |
| 6136 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); |
| 6137 | return -1; |
| 6138 | } |
| 6139 | |
| 6140 | ssid = wpa_s->current_ssid; |
| 6141 | if (ssid == NULL) { |
| 6142 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " |
| 6143 | "invitation"); |
| 6144 | return -1; |
| 6145 | } |
| 6146 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 6147 | wpa_s->global->p2p_invite_group = wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6148 | persistent = ssid->p2p_persistent_group && |
| 6149 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, |
| 6150 | ssid->ssid, ssid->ssid_len); |
| 6151 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6152 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 6153 | role = P2P_INVITE_ROLE_ACTIVE_GO; |
| 6154 | bssid = wpa_s->own_addr; |
| 6155 | if (go_dev_addr == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6156 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 6157 | freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6158 | } else { |
| 6159 | role = P2P_INVITE_ROLE_CLIENT; |
| 6160 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { |
| 6161 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " |
| 6162 | "invite to current group"); |
| 6163 | return -1; |
| 6164 | } |
| 6165 | bssid = wpa_s->bssid; |
| 6166 | if (go_dev_addr == NULL && |
| 6167 | !is_zero_ether_addr(wpa_s->go_dev_addr)) |
| 6168 | go_dev_addr = wpa_s->go_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 6169 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 6170 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6171 | } |
| 6172 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 6173 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6174 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6175 | return -1; |
| 6176 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6177 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 6178 | role == P2P_INVITE_ROLE_ACTIVE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6179 | if (res) |
| 6180 | return res; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 6181 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6182 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6183 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 6184 | ssid->ssid, ssid->ssid_len, force_freq, |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6185 | go_dev_addr, persistent, pref_freq, -1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
| 6188 | |
| 6189 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) |
| 6190 | { |
| 6191 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6192 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6193 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6194 | int persistent; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6195 | int freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6196 | u8 ip[3 * 4]; |
| 6197 | char ip_addr[100]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6198 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6199 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6200 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6201 | wpa_s->parent, NULL); |
| 6202 | } |
| 6203 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6204 | if (!wpa_s->show_group_started || !ssid) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6205 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6206 | |
| 6207 | wpa_s->show_group_started = 0; |
| 6208 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6209 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 6210 | if (ssid->bssid_set) |
| 6211 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); |
| 6212 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 6213 | ssid->ssid_len); |
| 6214 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); |
| 6215 | |
| 6216 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 6217 | wpa_s->global->p2p_group_formation = NULL; |
| 6218 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6219 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 6220 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6221 | |
| 6222 | ip_addr[0] = '\0'; |
| 6223 | if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6224 | int res; |
| 6225 | |
| 6226 | res = os_snprintf(ip_addr, sizeof(ip_addr), |
| 6227 | " ip_addr=%u.%u.%u.%u " |
| 6228 | "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u", |
| 6229 | ip[0], ip[1], ip[2], ip[3], |
| 6230 | ip[4], ip[5], ip[6], ip[7], |
| 6231 | ip[8], ip[9], ip[10], ip[11]); |
| 6232 | if (os_snprintf_error(sizeof(ip_addr), res)) |
| 6233 | ip_addr[0] = '\0'; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 6234 | } |
| 6235 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 6236 | wpas_p2p_group_started(wpa_s, 0, ssid, freq, |
| 6237 | ssid->passphrase == NULL && ssid->psk_set ? |
| 6238 | ssid->psk : NULL, |
| 6239 | ssid->passphrase, go_dev_addr, persistent, |
| 6240 | ip_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6241 | |
| 6242 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6243 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 6244 | ssid, go_dev_addr); |
| 6245 | if (network_id < 0) |
| 6246 | network_id = ssid->id; |
| 6247 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6248 | } |
| 6249 | |
| 6250 | |
| 6251 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, |
| 6252 | u32 interval1, u32 duration2, u32 interval2) |
| 6253 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6254 | int ret; |
| 6255 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6256 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6257 | return -1; |
| 6258 | |
| 6259 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
| 6260 | wpa_s->current_ssid == NULL || |
| 6261 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) |
| 6262 | return -1; |
| 6263 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6264 | ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, |
| 6265 | wpa_s->own_addr, wpa_s->assoc_freq, |
| 6266 | duration1, interval1, duration2, interval2); |
| 6267 | if (ret == 0) |
| 6268 | wpa_s->waiting_presence_resp = 1; |
| 6269 | |
| 6270 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | |
| 6274 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, |
| 6275 | unsigned int interval) |
| 6276 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6277 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6278 | return -1; |
| 6279 | |
| 6280 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
| 6281 | } |
| 6282 | |
| 6283 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6284 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
| 6285 | { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 6286 | if (wpa_s->current_ssid == NULL) { |
| 6287 | /* |
| 6288 | * current_ssid can be cleared when P2P client interface gets |
| 6289 | * disconnected, so assume this interface was used as P2P |
| 6290 | * client. |
| 6291 | */ |
| 6292 | return 1; |
| 6293 | } |
| 6294 | return wpa_s->current_ssid->p2p_group && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6295 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; |
| 6296 | } |
| 6297 | |
| 6298 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6299 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
| 6300 | { |
| 6301 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6302 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6303 | 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] | 6304 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
| 6305 | "disabled"); |
| 6306 | return; |
| 6307 | } |
| 6308 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6309 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " |
| 6310 | "group"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6311 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | |
| 6315 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) |
| 6316 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6317 | int timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6318 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6319 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6320 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
| 6321 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6322 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6323 | return; |
| 6324 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6325 | timeout = wpa_s->conf->p2p_group_idle; |
| 6326 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && |
| 6327 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) |
| 6328 | timeout = P2P_MAX_CLIENT_IDLE; |
| 6329 | |
| 6330 | if (timeout == 0) |
| 6331 | return; |
| 6332 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6333 | if (timeout < 0) { |
| 6334 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) |
| 6335 | timeout = 0; /* special client mode no-timeout */ |
| 6336 | else |
| 6337 | return; |
| 6338 | } |
| 6339 | |
| 6340 | if (wpa_s->p2p_in_provisioning) { |
| 6341 | /* |
| 6342 | * Use the normal group formation timeout during the |
| 6343 | * provisioning phase to avoid terminating this process too |
| 6344 | * early due to group idle timeout. |
| 6345 | */ |
| 6346 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6347 | "during provisioning"); |
| 6348 | return; |
| 6349 | } |
Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 6350 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6351 | if (wpa_s->show_group_started) { |
| 6352 | /* |
| 6353 | * Use the normal group formation timeout between the end of |
| 6354 | * the provisioning phase and completion of 4-way handshake to |
| 6355 | * avoid terminating this process too early due to group idle |
| 6356 | * timeout. |
| 6357 | */ |
| 6358 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 6359 | "while waiting for initial 4-way handshake to " |
| 6360 | "complete"); |
| 6361 | return; |
| 6362 | } |
| 6363 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6364 | 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] | 6365 | timeout); |
| 6366 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, |
| 6367 | wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6368 | } |
| 6369 | |
| 6370 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6371 | /* Returns 1 if the interface was removed */ |
| 6372 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
| 6373 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6374 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6375 | { |
| 6376 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6377 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6378 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6379 | if (!locally_generated) |
| 6380 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6381 | ie_len); |
| 6382 | |
| 6383 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && |
| 6384 | wpa_s->current_ssid && |
| 6385 | wpa_s->current_ssid->p2p_group && |
| 6386 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { |
| 6387 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " |
| 6388 | "session is ending"); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6389 | if (wpas_p2p_group_delete(wpa_s, |
| 6390 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) |
| 6391 | > 0) |
| 6392 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6393 | } |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6394 | |
| 6395 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | |
| 6399 | 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] | 6400 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 6401 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6402 | { |
| 6403 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6404 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6405 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6406 | if (!locally_generated) |
| 6407 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 6408 | ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | |
| 6412 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) |
| 6413 | { |
| 6414 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6415 | |
| 6416 | if (p2p == NULL) |
| 6417 | return; |
| 6418 | |
| 6419 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 6420 | return; |
| 6421 | |
| 6422 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
| 6423 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); |
| 6424 | |
| 6425 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
| 6426 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); |
| 6427 | |
| 6428 | if (wpa_s->wps && |
| 6429 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) |
| 6430 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); |
| 6431 | |
| 6432 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) |
| 6433 | p2p_set_uuid(p2p, wpa_s->wps->uuid); |
| 6434 | |
| 6435 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { |
| 6436 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); |
| 6437 | p2p_set_model_name(p2p, wpa_s->conf->model_name); |
| 6438 | p2p_set_model_number(p2p, wpa_s->conf->model_number); |
| 6439 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); |
| 6440 | } |
| 6441 | |
| 6442 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
| 6443 | p2p_set_sec_dev_types(p2p, |
| 6444 | (void *) wpa_s->conf->sec_device_type, |
| 6445 | wpa_s->conf->num_sec_device_types); |
| 6446 | |
| 6447 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
| 6448 | int i; |
| 6449 | p2p_remove_wps_vendor_extensions(p2p); |
| 6450 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 6451 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 6452 | continue; |
| 6453 | p2p_add_wps_vendor_extension( |
| 6454 | p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 6455 | } |
| 6456 | } |
| 6457 | |
| 6458 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
| 6459 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 6460 | char country[3]; |
| 6461 | country[0] = wpa_s->conf->country[0]; |
| 6462 | country[1] = wpa_s->conf->country[1]; |
| 6463 | country[2] = 0x04; |
| 6464 | p2p_set_country(p2p, country); |
| 6465 | } |
| 6466 | |
| 6467 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { |
| 6468 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, |
| 6469 | wpa_s->conf->p2p_ssid_postfix ? |
| 6470 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : |
| 6471 | 0); |
| 6472 | } |
| 6473 | |
| 6474 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) |
| 6475 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6476 | |
| 6477 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { |
| 6478 | u8 reg_class, channel; |
| 6479 | int ret; |
| 6480 | unsigned int r; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6481 | u8 channel_forced; |
| 6482 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6483 | if (wpa_s->conf->p2p_listen_reg_class && |
| 6484 | wpa_s->conf->p2p_listen_channel) { |
| 6485 | reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 6486 | channel = wpa_s->conf->p2p_listen_channel; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6487 | channel_forced = 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6488 | } else { |
| 6489 | reg_class = 81; |
| 6490 | /* |
| 6491 | * Pick one of the social channels randomly as the |
| 6492 | * listen channel. |
| 6493 | */ |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 6494 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 6495 | channel = 1; |
| 6496 | else |
| 6497 | channel = 1 + (r % 3) * 5; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6498 | channel_forced = 0; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6499 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 6500 | ret = p2p_set_listen_channel(p2p, reg_class, channel, |
| 6501 | channel_forced); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6502 | if (ret) |
| 6503 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " |
| 6504 | "failed: %d", ret); |
| 6505 | } |
| 6506 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { |
| 6507 | u8 op_reg_class, op_channel, cfg_op_channel; |
| 6508 | int ret = 0; |
| 6509 | unsigned int r; |
| 6510 | if (wpa_s->conf->p2p_oper_reg_class && |
| 6511 | wpa_s->conf->p2p_oper_channel) { |
| 6512 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 6513 | op_channel = wpa_s->conf->p2p_oper_channel; |
| 6514 | cfg_op_channel = 1; |
| 6515 | } else { |
| 6516 | op_reg_class = 81; |
| 6517 | /* |
| 6518 | * Use random operation channel from (1, 6, 11) |
| 6519 | *if no other preference is indicated. |
| 6520 | */ |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 6521 | if (os_get_random((u8 *) &r, sizeof(r)) < 0) |
| 6522 | op_channel = 1; |
| 6523 | else |
| 6524 | op_channel = 1 + (r % 3) * 5; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 6525 | cfg_op_channel = 0; |
| 6526 | } |
| 6527 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, |
| 6528 | cfg_op_channel); |
| 6529 | if (ret) |
| 6530 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " |
| 6531 | "failed: %d", ret); |
| 6532 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6533 | |
| 6534 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { |
| 6535 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, |
| 6536 | wpa_s->conf->p2p_pref_chan) < 0) { |
| 6537 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " |
| 6538 | "update failed"); |
| 6539 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6540 | |
| 6541 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { |
| 6542 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " |
| 6543 | "update failed"); |
| 6544 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6545 | } |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 6546 | |
| 6547 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN) |
| 6548 | p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | |
| 6552 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, |
| 6553 | int duration) |
| 6554 | { |
| 6555 | if (!wpa_s->ap_iface) |
| 6556 | return -1; |
| 6557 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, |
| 6558 | duration); |
| 6559 | } |
| 6560 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6561 | |
| 6562 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) |
| 6563 | { |
| 6564 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6565 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6566 | |
| 6567 | wpa_s->global->cross_connection = enabled; |
| 6568 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); |
| 6569 | |
| 6570 | if (!enabled) { |
| 6571 | struct wpa_supplicant *iface; |
| 6572 | |
| 6573 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 6574 | { |
| 6575 | if (iface->cross_connect_enabled == 0) |
| 6576 | continue; |
| 6577 | |
| 6578 | iface->cross_connect_enabled = 0; |
| 6579 | iface->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6580 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6581 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6582 | iface->ifname, |
| 6583 | iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6584 | } |
| 6585 | } |
| 6586 | |
| 6587 | return 0; |
| 6588 | } |
| 6589 | |
| 6590 | |
| 6591 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) |
| 6592 | { |
| 6593 | struct wpa_supplicant *iface; |
| 6594 | |
| 6595 | if (!uplink->global->cross_connection) |
| 6596 | return; |
| 6597 | |
| 6598 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6599 | if (!iface->cross_connect_enabled) |
| 6600 | continue; |
| 6601 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6602 | 0) |
| 6603 | continue; |
| 6604 | if (iface->ap_iface == NULL) |
| 6605 | continue; |
| 6606 | if (iface->cross_connect_in_use) |
| 6607 | continue; |
| 6608 | |
| 6609 | iface->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6610 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6611 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6612 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | |
| 6617 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) |
| 6618 | { |
| 6619 | struct wpa_supplicant *iface; |
| 6620 | |
| 6621 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6622 | if (!iface->cross_connect_enabled) |
| 6623 | continue; |
| 6624 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6625 | 0) |
| 6626 | continue; |
| 6627 | if (!iface->cross_connect_in_use) |
| 6628 | continue; |
| 6629 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6630 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6631 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6632 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6633 | iface->cross_connect_in_use = 0; |
| 6634 | } |
| 6635 | } |
| 6636 | |
| 6637 | |
| 6638 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) |
| 6639 | { |
| 6640 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || |
| 6641 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || |
| 6642 | wpa_s->cross_connect_disallowed) |
| 6643 | wpas_p2p_disable_cross_connect(wpa_s); |
| 6644 | else |
| 6645 | wpas_p2p_enable_cross_connect(wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6646 | if (!wpa_s->ap_iface && |
| 6647 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6648 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | |
| 6652 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) |
| 6653 | { |
| 6654 | wpas_p2p_disable_cross_connect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6655 | if (!wpa_s->ap_iface && |
| 6656 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, |
| 6657 | wpa_s, NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6658 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 6659 | } |
| 6660 | |
| 6661 | |
| 6662 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) |
| 6663 | { |
| 6664 | struct wpa_supplicant *iface; |
| 6665 | |
| 6666 | if (!wpa_s->global->cross_connection) |
| 6667 | return; |
| 6668 | |
| 6669 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6670 | if (iface == wpa_s) |
| 6671 | continue; |
| 6672 | if (iface->drv_flags & |
| 6673 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) |
| 6674 | continue; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 6675 | if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) && |
| 6676 | iface != wpa_s->parent) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6677 | continue; |
| 6678 | |
| 6679 | wpa_s->cross_connect_enabled = 1; |
| 6680 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, |
| 6681 | sizeof(wpa_s->cross_connect_uplink)); |
| 6682 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " |
| 6683 | "%s to %s whenever uplink is available", |
| 6684 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
| 6685 | |
| 6686 | if (iface->ap_iface || iface->current_ssid == NULL || |
| 6687 | iface->current_ssid->mode != WPAS_MODE_INFRA || |
| 6688 | iface->cross_connect_disallowed || |
| 6689 | iface->wpa_state != WPA_COMPLETED) |
| 6690 | break; |
| 6691 | |
| 6692 | wpa_s->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6693 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6694 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6695 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6696 | break; |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | |
| 6701 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) |
| 6702 | { |
| 6703 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && |
| 6704 | !wpa_s->p2p_in_provisioning) |
| 6705 | return 0; /* not P2P client operation */ |
| 6706 | |
| 6707 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " |
| 6708 | "session overlap"); |
| 6709 | if (wpa_s != wpa_s->parent) |
| 6710 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 6711 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6712 | return 1; |
| 6713 | } |
| 6714 | |
| 6715 | |
Dmitry Shmidt | df5a7e4 | 2014-04-02 12:59:59 -0700 | [diff] [blame] | 6716 | void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx) |
| 6717 | { |
| 6718 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6719 | wpas_p2p_notif_pbc_overlap(wpa_s); |
| 6720 | } |
| 6721 | |
| 6722 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6723 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) |
| 6724 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6725 | struct p2p_channels chan, cli_chan; |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 6726 | struct wpa_supplicant *ifs; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6727 | |
| 6728 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) |
| 6729 | return; |
| 6730 | |
| 6731 | os_memset(&chan, 0, sizeof(chan)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6732 | os_memset(&cli_chan, 0, sizeof(cli_chan)); |
| 6733 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6734 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " |
| 6735 | "channel list"); |
| 6736 | return; |
| 6737 | } |
| 6738 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6739 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); |
Dmitry Shmidt | 684785c | 2014-05-12 13:34:29 -0700 | [diff] [blame] | 6740 | |
| 6741 | for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { |
| 6742 | int freq; |
| 6743 | if (!ifs->current_ssid || |
| 6744 | !ifs->current_ssid->p2p_group || |
| 6745 | (ifs->current_ssid->mode != WPAS_MODE_P2P_GO && |
| 6746 | ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)) |
| 6747 | continue; |
| 6748 | freq = ifs->current_ssid->frequency; |
| 6749 | if (freq_included(&chan, freq)) { |
| 6750 | wpa_dbg(ifs, MSG_DEBUG, |
| 6751 | "P2P GO operating frequency %d MHz in valid range", |
| 6752 | freq); |
| 6753 | continue; |
| 6754 | } |
| 6755 | |
| 6756 | wpa_dbg(ifs, MSG_DEBUG, |
| 6757 | "P2P GO operating in invalid frequency %d MHz", freq); |
| 6758 | /* TODO: Consider using CSA or removing the group within |
| 6759 | * wpa_supplicant */ |
| 6760 | wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP); |
| 6761 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6762 | } |
| 6763 | |
| 6764 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6765 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, |
| 6766 | struct wpa_scan_results *scan_res) |
| 6767 | { |
| 6768 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 6769 | } |
| 6770 | |
| 6771 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6772 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) |
| 6773 | { |
| 6774 | struct wpa_global *global = wpa_s->global; |
| 6775 | int found = 0; |
| 6776 | const u8 *peer; |
| 6777 | |
| 6778 | if (global->p2p == NULL) |
| 6779 | return -1; |
| 6780 | |
| 6781 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
| 6782 | |
| 6783 | if (wpa_s->pending_interface_name[0] && |
| 6784 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 6785 | found = 1; |
| 6786 | |
| 6787 | peer = p2p_get_go_neg_peer(global->p2p); |
| 6788 | if (peer) { |
| 6789 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " |
| 6790 | MACSTR, MAC2STR(peer)); |
| 6791 | p2p_unauthorize(global->p2p, peer); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6792 | found = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6793 | } |
| 6794 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6795 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { |
| 6796 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); |
| 6797 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; |
| 6798 | found = 1; |
| 6799 | } |
| 6800 | |
| 6801 | if (wpa_s->pending_pd_before_join) { |
| 6802 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); |
| 6803 | wpa_s->pending_pd_before_join = 0; |
| 6804 | found = 1; |
| 6805 | } |
| 6806 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6807 | wpas_p2p_stop_find(wpa_s); |
| 6808 | |
| 6809 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6810 | if (wpa_s == global->p2p_group_formation && |
| 6811 | (wpa_s->p2p_in_provisioning || |
| 6812 | wpa_s->parent->pending_interface_type == |
| 6813 | WPA_IF_P2P_CLIENT)) { |
| 6814 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
| 6815 | "formation found - cancelling", |
| 6816 | wpa_s->ifname); |
| 6817 | found = 1; |
| 6818 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6819 | wpa_s->parent, NULL); |
Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 6820 | if (wpa_s->p2p_in_provisioning) { |
| 6821 | wpas_group_formation_completed(wpa_s, 0); |
| 6822 | break; |
| 6823 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6824 | wpas_p2p_group_delete(wpa_s, |
| 6825 | P2P_GROUP_REMOVAL_REQUESTED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6826 | break; |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 6827 | } else if (wpa_s->p2p_in_invitation) { |
| 6828 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling", |
| 6829 | wpa_s->ifname); |
| 6830 | found = 1; |
| 6831 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | if (!found) { |
| 6836 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); |
| 6837 | return -1; |
| 6838 | } |
| 6839 | |
| 6840 | return 0; |
| 6841 | } |
| 6842 | |
| 6843 | |
| 6844 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) |
| 6845 | { |
| 6846 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6847 | return; |
| 6848 | |
| 6849 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " |
| 6850 | "being available anymore"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6851 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | |
| 6855 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, |
| 6856 | int freq_24, int freq_5, int freq_overall) |
| 6857 | { |
| 6858 | struct p2p_data *p2p = wpa_s->global->p2p; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6859 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6860 | return; |
| 6861 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); |
| 6862 | } |
| 6863 | |
| 6864 | |
| 6865 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) |
| 6866 | { |
| 6867 | u8 peer[ETH_ALEN]; |
| 6868 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6869 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6870 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6871 | return -1; |
| 6872 | |
| 6873 | if (hwaddr_aton(addr, peer)) |
| 6874 | return -1; |
| 6875 | |
| 6876 | return p2p_unauthorize(p2p, peer); |
| 6877 | } |
| 6878 | |
| 6879 | |
| 6880 | /** |
| 6881 | * wpas_p2p_disconnect - Disconnect from a P2P Group |
| 6882 | * @wpa_s: Pointer to wpa_supplicant data |
| 6883 | * Returns: 0 on success, -1 on failure |
| 6884 | * |
| 6885 | * This can be used to disconnect from a group in which the local end is a P2P |
| 6886 | * Client or to end a P2P Group in case the local end is the Group Owner. If a |
| 6887 | * virtual network interface was created for this group, that interface will be |
| 6888 | * removed. Otherwise, only the configured P2P group network will be removed |
| 6889 | * from the interface. |
| 6890 | */ |
| 6891 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) |
| 6892 | { |
| 6893 | |
| 6894 | if (wpa_s == NULL) |
| 6895 | return -1; |
| 6896 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6897 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? |
| 6898 | -1 : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6899 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6900 | |
| 6901 | |
| 6902 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) |
| 6903 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6904 | int ret; |
| 6905 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6906 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6907 | return 0; |
| 6908 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6909 | ret = p2p_in_progress(wpa_s->global->p2p); |
| 6910 | if (ret == 0) { |
| 6911 | /* |
| 6912 | * Check whether there is an ongoing WPS provisioning step (or |
| 6913 | * other parts of group formation) on another interface since |
| 6914 | * p2p_in_progress() does not report this to avoid issues for |
| 6915 | * scans during such provisioning step. |
| 6916 | */ |
| 6917 | if (wpa_s->global->p2p_group_formation && |
| 6918 | wpa_s->global->p2p_group_formation != wpa_s) { |
| 6919 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " |
| 6920 | "in group formation", |
| 6921 | wpa_s->global->p2p_group_formation->ifname); |
| 6922 | ret = 1; |
| 6923 | } |
| 6924 | } |
| 6925 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6926 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 6927 | struct os_reltime now; |
| 6928 | os_get_reltime(&now); |
| 6929 | if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, |
| 6930 | P2P_MAX_INITIAL_CONN_WAIT_GO)) { |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6931 | /* Wait for the first client has expired */ |
| 6932 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 6933 | } else { |
| 6934 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); |
| 6935 | ret = 1; |
| 6936 | } |
| 6937 | } |
| 6938 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6939 | return ret; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6940 | } |
| 6941 | |
| 6942 | |
| 6943 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, |
| 6944 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6945 | { |
| 6946 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && |
| 6947 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6948 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6949 | /** |
| 6950 | * Remove the network by scheduling the group formation |
| 6951 | * timeout to happen immediately. The teardown code |
| 6952 | * needs to be scheduled to run asynch later so that we |
| 6953 | * don't delete data from under ourselves unexpectedly. |
| 6954 | * Calling wpas_p2p_group_formation_timeout directly |
| 6955 | * causes a series of crashes in WPS failure scenarios. |
| 6956 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6957 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
| 6958 | "P2P group network getting removed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6959 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 6960 | wpa_s->parent, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6961 | } |
| 6962 | } |
| 6963 | |
| 6964 | |
| 6965 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6966 | const u8 *addr, const u8 *ssid, |
| 6967 | size_t ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6968 | { |
| 6969 | struct wpa_ssid *s; |
| 6970 | size_t i; |
| 6971 | |
| 6972 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 6973 | if (s->disabled != 2) |
| 6974 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6975 | if (ssid && |
| 6976 | (ssid_len != s->ssid_len || |
| 6977 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) |
| 6978 | continue; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6979 | if (addr == NULL) { |
| 6980 | if (s->mode == WPAS_MODE_P2P_GO) |
| 6981 | return s; |
| 6982 | continue; |
| 6983 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6984 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
| 6985 | return s; /* peer is GO in the persistent group */ |
| 6986 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) |
| 6987 | continue; |
| 6988 | for (i = 0; i < s->num_p2p_clients; i++) { |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 6989 | if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6990 | addr, ETH_ALEN) == 0) |
| 6991 | return s; /* peer is P2P client in persistent |
| 6992 | * group */ |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | return NULL; |
| 6997 | } |
| 6998 | |
| 6999 | |
| 7000 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, |
| 7001 | const u8 *addr) |
| 7002 | { |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 7003 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 7004 | wpa_s->parent, NULL) > 0) { |
| 7005 | /* |
| 7006 | * This can happen if WPS provisioning step is not terminated |
| 7007 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the |
| 7008 | * peer was able to connect, there is no need to time out group |
| 7009 | * formation after this, though. In addition, this is used with |
| 7010 | * the initial connection wait on the GO as a separate formation |
| 7011 | * timeout and as such, expected to be hit after the initial WPS |
| 7012 | * provisioning step. |
| 7013 | */ |
| 7014 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 7015 | |
| 7016 | if (!wpa_s->p2p_go_group_formation_completed && |
| 7017 | !wpa_s->group_formation_reported) { |
| 7018 | /* |
| 7019 | * GO has not yet notified group formation success since |
| 7020 | * the WPS step was not completed cleanly. Do that |
| 7021 | * notification now since the P2P Client was able to |
| 7022 | * connect and as such, must have received the |
| 7023 | * credential from the WPS step. |
| 7024 | */ |
| 7025 | if (wpa_s->global->p2p) |
| 7026 | p2p_wps_success_cb(wpa_s->global->p2p, addr); |
| 7027 | wpas_group_formation_completed(wpa_s, 1); |
| 7028 | } |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 7029 | } |
| 7030 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 7031 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); |
| 7032 | wpa_s->p2p_go_group_formation_completed = 1; |
| 7033 | wpa_s->global->p2p_group_formation = NULL; |
| 7034 | wpa_s->p2p_in_provisioning = 0; |
Dmitry Shmidt | 21de214 | 2014-04-08 10:50:52 -0700 | [diff] [blame] | 7035 | wpa_s->p2p_in_invitation = 0; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 7036 | } |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 7037 | wpa_s->global->p2p_go_wait_client.sec = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 7038 | if (addr == NULL) |
| 7039 | return; |
| 7040 | wpas_p2p_add_persistent_group_client(wpa_s, addr); |
| 7041 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 7042 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7043 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7044 | static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 7045 | int group_added) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7046 | { |
| 7047 | struct wpa_supplicant *group = wpa_s; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7048 | int ret = 0; |
| 7049 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7050 | if (wpa_s->global->p2p_group_formation) |
| 7051 | group = wpa_s->global->p2p_group_formation; |
| 7052 | wpa_s = wpa_s->parent; |
| 7053 | offchannel_send_action_done(wpa_s); |
| 7054 | if (group_added) |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7055 | ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7056 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); |
| 7057 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, |
| 7058 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, |
| 7059 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, |
| 7060 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7061 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 7062 | wpa_s->p2p_go_ht40, |
| 7063 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7064 | return ret; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7065 | } |
| 7066 | |
| 7067 | |
| 7068 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) |
| 7069 | { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7070 | int res; |
| 7071 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7072 | if (!wpa_s->p2p_fallback_to_go_neg || |
| 7073 | wpa_s->p2p_in_provisioning <= 5) |
| 7074 | return 0; |
| 7075 | |
| 7076 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) |
| 7077 | return 0; /* peer operating as a GO */ |
| 7078 | |
| 7079 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " |
| 7080 | "fallback to GO Negotiation"); |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7081 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG |
| 7082 | "reason=GO-not-found"); |
| 7083 | res = wpas_p2p_fallback_to_go_neg(wpa_s, 1); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7084 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 7085 | return res == 1 ? 2 : 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 7086 | } |
| 7087 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7088 | |
| 7089 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) |
| 7090 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7091 | struct wpa_supplicant *ifs; |
| 7092 | |
| 7093 | if (wpa_s->wpa_state > WPA_SCANNING) { |
| 7094 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " |
| 7095 | "concurrent operation", |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 7096 | wpa_s->conf->p2p_search_delay); |
| 7097 | return wpa_s->conf->p2p_search_delay; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7098 | } |
| 7099 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 7100 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, |
| 7101 | radio_list) { |
| 7102 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7103 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " |
| 7104 | "delay due to concurrent operation on " |
| 7105 | "interface %s", |
Dmitry Shmidt | 09f57ba | 2014-06-10 16:07:13 -0700 | [diff] [blame] | 7106 | wpa_s->conf->p2p_search_delay, |
| 7107 | ifs->ifname); |
| 7108 | return wpa_s->conf->p2p_search_delay; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | return 0; |
| 7113 | } |
| 7114 | |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 7115 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7116 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, |
| 7117 | struct wpa_ssid *s, const u8 *addr, |
| 7118 | int iface_addr) |
| 7119 | { |
| 7120 | struct psk_list_entry *psk, *tmp; |
| 7121 | int changed = 0; |
| 7122 | |
| 7123 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, |
| 7124 | list) { |
| 7125 | if ((iface_addr && !psk->p2p && |
| 7126 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || |
| 7127 | (!iface_addr && psk->p2p && |
| 7128 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { |
| 7129 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 7130 | "P2P: Remove persistent group PSK list entry for " |
| 7131 | MACSTR " p2p=%u", |
| 7132 | MAC2STR(psk->addr), psk->p2p); |
| 7133 | dl_list_del(&psk->list); |
| 7134 | os_free(psk); |
| 7135 | changed++; |
| 7136 | } |
| 7137 | } |
| 7138 | |
| 7139 | return changed; |
| 7140 | } |
| 7141 | |
| 7142 | |
| 7143 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, |
| 7144 | const u8 *p2p_dev_addr, |
| 7145 | const u8 *psk, size_t psk_len) |
| 7146 | { |
| 7147 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 7148 | struct wpa_ssid *persistent; |
Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 7149 | struct psk_list_entry *p, *last; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7150 | |
| 7151 | if (psk_len != sizeof(p->psk)) |
| 7152 | return; |
| 7153 | |
| 7154 | if (p2p_dev_addr) { |
| 7155 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR |
| 7156 | " p2p_dev_addr=" MACSTR, |
| 7157 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 7158 | if (is_zero_ether_addr(p2p_dev_addr)) |
| 7159 | p2p_dev_addr = NULL; |
| 7160 | } else { |
| 7161 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, |
| 7162 | MAC2STR(mac_addr)); |
| 7163 | } |
| 7164 | |
| 7165 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 7166 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); |
| 7167 | /* To be added to persistent group once created */ |
| 7168 | if (wpa_s->global->add_psk == NULL) { |
| 7169 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); |
| 7170 | if (wpa_s->global->add_psk == NULL) |
| 7171 | return; |
| 7172 | } |
| 7173 | p = wpa_s->global->add_psk; |
| 7174 | if (p2p_dev_addr) { |
| 7175 | p->p2p = 1; |
| 7176 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 7177 | } else { |
| 7178 | p->p2p = 0; |
| 7179 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 7180 | } |
| 7181 | os_memcpy(p->psk, psk, psk_len); |
| 7182 | return; |
| 7183 | } |
| 7184 | |
| 7185 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { |
| 7186 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); |
| 7187 | return; |
| 7188 | } |
| 7189 | |
| 7190 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 7191 | ssid->ssid_len); |
| 7192 | if (!persistent) { |
| 7193 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); |
| 7194 | return; |
| 7195 | } |
| 7196 | |
| 7197 | p = os_zalloc(sizeof(*p)); |
| 7198 | if (p == NULL) |
| 7199 | return; |
| 7200 | if (p2p_dev_addr) { |
| 7201 | p->p2p = 1; |
| 7202 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 7203 | } else { |
| 7204 | p->p2p = 0; |
| 7205 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 7206 | } |
| 7207 | os_memcpy(p->psk, psk, psk_len); |
| 7208 | |
Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 7209 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS && |
| 7210 | (last = dl_list_last(&persistent->psk_list, |
| 7211 | struct psk_list_entry, list))) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7212 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " |
| 7213 | MACSTR " (p2p=%u) to make room for a new one", |
| 7214 | MAC2STR(last->addr), last->p2p); |
| 7215 | dl_list_del(&last->list); |
| 7216 | os_free(last); |
| 7217 | } |
| 7218 | |
| 7219 | wpas_p2p_remove_psk_entry(wpa_s->parent, persistent, |
| 7220 | p2p_dev_addr ? p2p_dev_addr : mac_addr, |
| 7221 | p2p_dev_addr == NULL); |
| 7222 | if (p2p_dev_addr) { |
| 7223 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" |
| 7224 | MACSTR, MAC2STR(p2p_dev_addr)); |
| 7225 | } else { |
| 7226 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, |
| 7227 | MAC2STR(mac_addr)); |
| 7228 | } |
| 7229 | dl_list_add(&persistent->psk_list, &p->list); |
| 7230 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7231 | if (wpa_s->parent->conf->update_config && |
| 7232 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 7233 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | |
| 7237 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, |
| 7238 | struct wpa_ssid *s, const u8 *addr, |
| 7239 | int iface_addr) |
| 7240 | { |
| 7241 | int res; |
| 7242 | |
| 7243 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 7244 | if (res > 0 && wpa_s->conf->update_config && |
| 7245 | wpa_config_write(wpa_s->confname, wpa_s->conf)) |
| 7246 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 7247 | "P2P: Failed to update configuration"); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7248 | } |
| 7249 | |
| 7250 | |
| 7251 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, |
| 7252 | const u8 *peer, int iface_addr) |
| 7253 | { |
| 7254 | struct hostapd_data *hapd; |
| 7255 | struct hostapd_wpa_psk *psk, *prev, *rem; |
| 7256 | struct sta_info *sta; |
| 7257 | |
| 7258 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || |
| 7259 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) |
| 7260 | return; |
| 7261 | |
| 7262 | /* Remove per-station PSK entry */ |
| 7263 | hapd = wpa_s->ap_iface->bss[0]; |
| 7264 | prev = NULL; |
| 7265 | psk = hapd->conf->ssid.wpa_psk; |
| 7266 | while (psk) { |
| 7267 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || |
| 7268 | (!iface_addr && |
| 7269 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { |
| 7270 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " |
| 7271 | MACSTR " iface_addr=%d", |
| 7272 | MAC2STR(peer), iface_addr); |
| 7273 | if (prev) |
| 7274 | prev->next = psk->next; |
| 7275 | else |
| 7276 | hapd->conf->ssid.wpa_psk = psk->next; |
| 7277 | rem = psk; |
| 7278 | psk = psk->next; |
| 7279 | os_free(rem); |
| 7280 | } else { |
| 7281 | prev = psk; |
| 7282 | psk = psk->next; |
| 7283 | } |
| 7284 | } |
| 7285 | |
| 7286 | /* Disconnect from group */ |
| 7287 | if (iface_addr) |
| 7288 | sta = ap_get_sta(hapd, peer); |
| 7289 | else |
| 7290 | sta = ap_get_sta_p2p(hapd, peer); |
| 7291 | if (sta) { |
| 7292 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR |
| 7293 | " (iface_addr=%d) from group", |
| 7294 | MAC2STR(peer), iface_addr); |
| 7295 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 7296 | WLAN_REASON_DEAUTH_LEAVING); |
| 7297 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); |
| 7298 | } |
| 7299 | } |
| 7300 | |
| 7301 | |
| 7302 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, |
| 7303 | int iface_addr) |
| 7304 | { |
| 7305 | struct wpa_ssid *s; |
| 7306 | struct wpa_supplicant *w; |
| 7307 | |
| 7308 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); |
| 7309 | |
| 7310 | /* Remove from any persistent group */ |
| 7311 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 7312 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 7313 | continue; |
| 7314 | if (!iface_addr) |
| 7315 | wpas_remove_persistent_peer(wpa_s, s, peer, 0); |
| 7316 | wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr); |
| 7317 | } |
| 7318 | |
| 7319 | /* Remove from any operating group */ |
| 7320 | for (w = wpa_s->global->ifaces; w; w = w->next) |
| 7321 | wpas_p2p_remove_client_go(w, peer, iface_addr); |
| 7322 | } |
| 7323 | |
| 7324 | |
| 7325 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) |
| 7326 | { |
| 7327 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 7328 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); |
| 7329 | } |
| 7330 | |
| 7331 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 7332 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) |
| 7333 | { |
| 7334 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 7335 | |
| 7336 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); |
| 7337 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); |
| 7338 | } |
| 7339 | |
| 7340 | |
| 7341 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, |
| 7342 | struct wpa_ssid *ssid) |
| 7343 | { |
| 7344 | struct wpa_supplicant *iface; |
| 7345 | |
| 7346 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7347 | if (!iface->current_ssid || |
| 7348 | iface->current_ssid->frequency == freq || |
| 7349 | (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && |
| 7350 | !iface->current_ssid->p2p_group)) |
| 7351 | continue; |
| 7352 | |
| 7353 | /* Remove the connection with least priority */ |
| 7354 | if (!wpas_is_p2p_prioritized(iface)) { |
| 7355 | /* STA connection has priority over existing |
| 7356 | * P2P connection, so remove the interface. */ |
| 7357 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); |
| 7358 | eloop_register_timeout(0, 0, |
| 7359 | wpas_p2p_group_freq_conflict, |
| 7360 | iface, NULL); |
| 7361 | /* If connection in progress is P2P connection, do not |
| 7362 | * proceed for the connection. */ |
| 7363 | if (wpa_s == iface) |
| 7364 | return -1; |
| 7365 | else |
| 7366 | return 0; |
| 7367 | } else { |
| 7368 | /* P2P connection has priority, disable the STA network |
| 7369 | */ |
| 7370 | wpa_supplicant_disable_network(wpa_s->global->ifaces, |
| 7371 | ssid); |
| 7372 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, |
| 7373 | WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); |
| 7374 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, |
| 7375 | ETH_ALEN); |
| 7376 | /* If P2P connection is in progress, continue |
| 7377 | * connecting...*/ |
| 7378 | if (wpa_s == iface) |
| 7379 | return 0; |
| 7380 | else |
| 7381 | return -1; |
| 7382 | } |
| 7383 | } |
| 7384 | |
| 7385 | return 0; |
| 7386 | } |
| 7387 | |
| 7388 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 7389 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) |
| 7390 | { |
| 7391 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 7392 | |
| 7393 | if (ssid == NULL || !ssid->p2p_group) |
| 7394 | return 0; |
| 7395 | |
| 7396 | if (wpa_s->p2p_last_4way_hs_fail && |
| 7397 | wpa_s->p2p_last_4way_hs_fail == ssid) { |
| 7398 | u8 go_dev_addr[ETH_ALEN]; |
| 7399 | struct wpa_ssid *persistent; |
| 7400 | |
| 7401 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, |
| 7402 | ssid->ssid, |
| 7403 | ssid->ssid_len) <= 0) { |
| 7404 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); |
| 7405 | goto disconnect; |
| 7406 | } |
| 7407 | |
| 7408 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" |
| 7409 | MACSTR, MAC2STR(go_dev_addr)); |
| 7410 | persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr, |
| 7411 | ssid->ssid, |
| 7412 | ssid->ssid_len); |
| 7413 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { |
| 7414 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); |
| 7415 | goto disconnect; |
| 7416 | } |
| 7417 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 7418 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", |
| 7419 | persistent->id); |
| 7420 | disconnect: |
| 7421 | wpa_s->p2p_last_4way_hs_fail = NULL; |
| 7422 | /* |
| 7423 | * Remove the group from a timeout to avoid issues with caller |
| 7424 | * continuing to use the interface if this is on a P2P group |
| 7425 | * interface. |
| 7426 | */ |
| 7427 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, |
| 7428 | wpa_s, NULL); |
| 7429 | return 1; |
| 7430 | } |
| 7431 | |
| 7432 | wpa_s->p2p_last_4way_hs_fail = ssid; |
| 7433 | return 0; |
| 7434 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7435 | |
| 7436 | |
| 7437 | #ifdef CONFIG_WPS_NFC |
| 7438 | |
| 7439 | static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc, |
| 7440 | struct wpabuf *p2p) |
| 7441 | { |
| 7442 | struct wpabuf *ret; |
| 7443 | size_t wsc_len; |
| 7444 | |
| 7445 | if (p2p == NULL) { |
| 7446 | wpabuf_free(wsc); |
| 7447 | wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover"); |
| 7448 | return NULL; |
| 7449 | } |
| 7450 | |
| 7451 | wsc_len = wsc ? wpabuf_len(wsc) : 0; |
| 7452 | ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p)); |
| 7453 | if (ret == NULL) { |
| 7454 | wpabuf_free(wsc); |
| 7455 | wpabuf_free(p2p); |
| 7456 | return NULL; |
| 7457 | } |
| 7458 | |
| 7459 | wpabuf_put_be16(ret, wsc_len); |
| 7460 | if (wsc) |
| 7461 | wpabuf_put_buf(ret, wsc); |
| 7462 | wpabuf_put_be16(ret, wpabuf_len(p2p)); |
| 7463 | wpabuf_put_buf(ret, p2p); |
| 7464 | |
| 7465 | wpabuf_free(wsc); |
| 7466 | wpabuf_free(p2p); |
| 7467 | wpa_hexdump_buf(MSG_DEBUG, |
| 7468 | "P2P: Generated NFC connection handover message", ret); |
| 7469 | |
| 7470 | if (ndef && ret) { |
| 7471 | struct wpabuf *tmp; |
| 7472 | tmp = ndef_build_p2p(ret); |
| 7473 | wpabuf_free(ret); |
| 7474 | if (tmp == NULL) { |
| 7475 | wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request"); |
| 7476 | return NULL; |
| 7477 | } |
| 7478 | ret = tmp; |
| 7479 | } |
| 7480 | |
| 7481 | return ret; |
| 7482 | } |
| 7483 | |
| 7484 | |
| 7485 | static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s, |
| 7486 | struct wpa_ssid **ssid, u8 *go_dev_addr) |
| 7487 | { |
| 7488 | struct wpa_supplicant *iface; |
| 7489 | |
| 7490 | if (go_dev_addr) |
| 7491 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 7492 | if (ssid) |
| 7493 | *ssid = NULL; |
| 7494 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7495 | if (iface->wpa_state < WPA_ASSOCIATING || |
| 7496 | iface->current_ssid == NULL || iface->assoc_freq == 0 || |
| 7497 | !iface->current_ssid->p2p_group || |
| 7498 | iface->current_ssid->mode != WPAS_MODE_INFRA) |
| 7499 | continue; |
| 7500 | if (ssid) |
| 7501 | *ssid = iface->current_ssid; |
| 7502 | if (go_dev_addr) |
| 7503 | os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN); |
| 7504 | return iface->assoc_freq; |
| 7505 | } |
| 7506 | return 0; |
| 7507 | } |
| 7508 | |
| 7509 | |
| 7510 | struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s, |
| 7511 | int ndef) |
| 7512 | { |
| 7513 | struct wpabuf *wsc, *p2p; |
| 7514 | struct wpa_ssid *ssid; |
| 7515 | u8 go_dev_addr[ETH_ALEN]; |
| 7516 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7517 | |
| 7518 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) { |
| 7519 | wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request"); |
| 7520 | return NULL; |
| 7521 | } |
| 7522 | |
| 7523 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7524 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7525 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) { |
| 7526 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request"); |
| 7527 | return NULL; |
| 7528 | } |
| 7529 | |
| 7530 | if (cli_freq == 0) { |
| 7531 | wsc = wps_build_nfc_handover_req_p2p( |
| 7532 | wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey); |
| 7533 | } else |
| 7534 | wsc = NULL; |
| 7535 | p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq, |
| 7536 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7537 | ssid ? ssid->ssid_len : 0); |
| 7538 | |
| 7539 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7540 | } |
| 7541 | |
| 7542 | |
| 7543 | struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s, |
| 7544 | int ndef, int tag) |
| 7545 | { |
| 7546 | struct wpabuf *wsc, *p2p; |
| 7547 | struct wpa_ssid *ssid; |
| 7548 | u8 go_dev_addr[ETH_ALEN]; |
| 7549 | int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); |
| 7550 | |
| 7551 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7552 | return NULL; |
| 7553 | |
| 7554 | if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7555 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7556 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7557 | return NULL; |
| 7558 | |
| 7559 | if (cli_freq == 0) { |
| 7560 | wsc = wps_build_nfc_handover_sel_p2p( |
| 7561 | wpa_s->parent->wps, |
| 7562 | tag ? wpa_s->conf->wps_nfc_dev_pw_id : |
| 7563 | DEV_PW_NFC_CONNECTION_HANDOVER, |
| 7564 | wpa_s->conf->wps_nfc_dh_pubkey, |
| 7565 | tag ? wpa_s->conf->wps_nfc_dev_pw : NULL); |
| 7566 | } else |
| 7567 | wsc = NULL; |
| 7568 | p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq, |
| 7569 | go_dev_addr, ssid ? ssid->ssid : NULL, |
| 7570 | ssid ? ssid->ssid_len : 0); |
| 7571 | |
| 7572 | return wpas_p2p_nfc_handover(ndef, wsc, p2p); |
| 7573 | } |
| 7574 | |
| 7575 | |
| 7576 | static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s, |
| 7577 | struct p2p_nfc_params *params) |
| 7578 | { |
| 7579 | wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC " |
| 7580 | "connection handover (freq=%d)", |
| 7581 | params->go_freq); |
| 7582 | |
| 7583 | if (params->go_freq && params->go_ssid_len) { |
| 7584 | wpa_s->p2p_wps_method = WPS_NFC; |
| 7585 | wpa_s->pending_join_wps_method = WPS_NFC; |
| 7586 | os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN); |
| 7587 | os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr, |
| 7588 | ETH_ALEN); |
| 7589 | return wpas_p2p_join_start(wpa_s, params->go_freq, |
| 7590 | params->go_ssid, |
| 7591 | params->go_ssid_len); |
| 7592 | } |
| 7593 | |
| 7594 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7595 | WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent, |
| 7596 | params->go_freq, -1, 0, 1, 1); |
| 7597 | } |
| 7598 | |
| 7599 | |
| 7600 | static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s, |
| 7601 | struct p2p_nfc_params *params, int tag) |
| 7602 | { |
| 7603 | int res, persistent; |
| 7604 | struct wpa_ssid *ssid; |
| 7605 | |
| 7606 | wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC " |
| 7607 | "connection handover"); |
| 7608 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 7609 | ssid = wpa_s->current_ssid; |
| 7610 | if (ssid == NULL) |
| 7611 | continue; |
| 7612 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 7613 | continue; |
| 7614 | if (wpa_s->ap_iface == NULL) |
| 7615 | continue; |
| 7616 | break; |
| 7617 | } |
| 7618 | if (wpa_s == NULL) { |
| 7619 | wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface"); |
| 7620 | return -1; |
| 7621 | } |
| 7622 | |
| 7623 | if (wpa_s->parent->p2p_oob_dev_pw_id != |
| 7624 | DEV_PW_NFC_CONNECTION_HANDOVER && |
| 7625 | !wpa_s->parent->p2p_oob_dev_pw) { |
| 7626 | wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); |
| 7627 | return -1; |
| 7628 | } |
| 7629 | res = wpas_ap_wps_add_nfc_pw( |
| 7630 | wpa_s, wpa_s->parent->p2p_oob_dev_pw_id, |
| 7631 | wpa_s->parent->p2p_oob_dev_pw, |
| 7632 | wpa_s->parent->p2p_peer_oob_pk_hash_known ? |
| 7633 | wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL); |
| 7634 | if (res) |
| 7635 | return res; |
| 7636 | |
| 7637 | if (!tag) { |
| 7638 | wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation"); |
| 7639 | return 0; |
| 7640 | } |
| 7641 | |
| 7642 | if (!params->peer || |
| 7643 | !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE)) |
| 7644 | return 0; |
| 7645 | |
| 7646 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR |
| 7647 | " to join", MAC2STR(params->peer->p2p_device_addr)); |
| 7648 | |
| 7649 | wpa_s->global->p2p_invite_group = wpa_s; |
| 7650 | persistent = ssid->p2p_persistent_group && |
| 7651 | wpas_p2p_get_persistent(wpa_s->parent, |
| 7652 | params->peer->p2p_device_addr, |
| 7653 | ssid->ssid, ssid->ssid_len); |
| 7654 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 7655 | |
| 7656 | return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr, |
| 7657 | P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr, |
| 7658 | ssid->ssid, ssid->ssid_len, ssid->frequency, |
| 7659 | wpa_s->global->p2p_dev_addr, persistent, 0, |
| 7660 | wpa_s->parent->p2p_oob_dev_pw_id); |
| 7661 | } |
| 7662 | |
| 7663 | |
| 7664 | static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s, |
| 7665 | struct p2p_nfc_params *params, |
| 7666 | int forced_freq) |
| 7667 | { |
| 7668 | wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC " |
| 7669 | "connection handover"); |
| 7670 | return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7671 | WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent, |
| 7672 | forced_freq, -1, 0, 1, 1); |
| 7673 | } |
| 7674 | |
| 7675 | |
| 7676 | static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s, |
| 7677 | struct p2p_nfc_params *params, |
| 7678 | int forced_freq) |
| 7679 | { |
| 7680 | int res; |
| 7681 | |
| 7682 | wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC " |
| 7683 | "connection handover"); |
| 7684 | res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, |
| 7685 | WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent, |
| 7686 | forced_freq, -1, 0, 1, 1); |
| 7687 | if (res) |
| 7688 | return res; |
| 7689 | |
| 7690 | res = wpas_p2p_listen(wpa_s, 60); |
| 7691 | if (res) { |
| 7692 | p2p_unauthorize(wpa_s->global->p2p, |
| 7693 | params->peer->p2p_device_addr); |
| 7694 | } |
| 7695 | |
| 7696 | return res; |
| 7697 | } |
| 7698 | |
| 7699 | |
| 7700 | static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s, |
| 7701 | const struct wpabuf *data, |
| 7702 | int sel, int tag, int forced_freq) |
| 7703 | { |
| 7704 | const u8 *pos, *end; |
| 7705 | u16 len, id; |
| 7706 | struct p2p_nfc_params params; |
| 7707 | int res; |
| 7708 | |
| 7709 | os_memset(¶ms, 0, sizeof(params)); |
| 7710 | params.sel = sel; |
| 7711 | |
| 7712 | wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data); |
| 7713 | |
| 7714 | pos = wpabuf_head(data); |
| 7715 | end = pos + wpabuf_len(data); |
| 7716 | |
| 7717 | if (end - pos < 2) { |
| 7718 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC " |
| 7719 | "attributes"); |
| 7720 | return -1; |
| 7721 | } |
| 7722 | len = WPA_GET_BE16(pos); |
| 7723 | pos += 2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 7724 | if (len > end - pos) { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7725 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC " |
| 7726 | "attributes"); |
| 7727 | return -1; |
| 7728 | } |
| 7729 | params.wsc_attr = pos; |
| 7730 | params.wsc_len = len; |
| 7731 | pos += len; |
| 7732 | |
| 7733 | if (end - pos < 2) { |
| 7734 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P " |
| 7735 | "attributes"); |
| 7736 | return -1; |
| 7737 | } |
| 7738 | len = WPA_GET_BE16(pos); |
| 7739 | pos += 2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 7740 | if (len > end - pos) { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7741 | wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P " |
| 7742 | "attributes"); |
| 7743 | return -1; |
| 7744 | } |
| 7745 | params.p2p_attr = pos; |
| 7746 | params.p2p_len = len; |
| 7747 | pos += len; |
| 7748 | |
| 7749 | wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes", |
| 7750 | params.wsc_attr, params.wsc_len); |
| 7751 | wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes", |
| 7752 | params.p2p_attr, params.p2p_len); |
| 7753 | if (pos < end) { |
| 7754 | wpa_hexdump(MSG_DEBUG, |
| 7755 | "P2P: Ignored extra data after P2P attributes", |
| 7756 | pos, end - pos); |
| 7757 | } |
| 7758 | |
| 7759 | res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms); |
| 7760 | if (res) |
| 7761 | return res; |
| 7762 | |
| 7763 | if (params.next_step == NO_ACTION) |
| 7764 | return 0; |
| 7765 | |
| 7766 | if (params.next_step == BOTH_GO) { |
| 7767 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR, |
| 7768 | MAC2STR(params.peer->p2p_device_addr)); |
| 7769 | return 0; |
| 7770 | } |
| 7771 | |
| 7772 | if (params.next_step == PEER_CLIENT) { |
| 7773 | if (!is_zero_ether_addr(params.go_dev_addr)) { |
| 7774 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7775 | "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR |
| 7776 | " ssid=\"%s\"", |
| 7777 | MAC2STR(params.peer->p2p_device_addr), |
| 7778 | params.go_freq, |
| 7779 | MAC2STR(params.go_dev_addr), |
| 7780 | wpa_ssid_txt(params.go_ssid, |
| 7781 | params.go_ssid_len)); |
| 7782 | } else { |
| 7783 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT |
| 7784 | "peer=" MACSTR " freq=%d", |
| 7785 | MAC2STR(params.peer->p2p_device_addr), |
| 7786 | params.go_freq); |
| 7787 | } |
| 7788 | return 0; |
| 7789 | } |
| 7790 | |
| 7791 | if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) { |
| 7792 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer=" |
| 7793 | MACSTR, MAC2STR(params.peer->p2p_device_addr)); |
| 7794 | return 0; |
| 7795 | } |
| 7796 | |
| 7797 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7798 | wpa_s->p2p_oob_dev_pw = NULL; |
| 7799 | |
| 7800 | if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { |
| 7801 | wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw " |
| 7802 | "received"); |
| 7803 | return -1; |
| 7804 | } |
| 7805 | |
| 7806 | id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN); |
| 7807 | wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id); |
| 7808 | wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash", |
| 7809 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7810 | os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash, |
| 7811 | params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); |
| 7812 | wpa_s->p2p_peer_oob_pk_hash_known = 1; |
| 7813 | |
| 7814 | if (tag) { |
| 7815 | if (id < 0x10) { |
| 7816 | wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid " |
| 7817 | "peer OOB Device Password Id %u", id); |
| 7818 | return -1; |
| 7819 | } |
| 7820 | wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB " |
| 7821 | "Device Password Id %u", id); |
| 7822 | wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password", |
| 7823 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7824 | params.oob_dev_pw_len - |
| 7825 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7826 | wpa_s->p2p_oob_dev_pw_id = id; |
| 7827 | wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy( |
| 7828 | params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, |
| 7829 | params.oob_dev_pw_len - |
| 7830 | WPS_OOB_PUBKEY_HASH_LEN - 2); |
| 7831 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7832 | return -1; |
| 7833 | |
| 7834 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && |
| 7835 | wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, |
| 7836 | &wpa_s->conf->wps_nfc_dh_privkey) < 0) |
| 7837 | return -1; |
| 7838 | } else { |
| 7839 | wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake " |
| 7840 | "without Device Password"); |
| 7841 | wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; |
| 7842 | } |
| 7843 | |
| 7844 | switch (params.next_step) { |
| 7845 | case NO_ACTION: |
| 7846 | case BOTH_GO: |
| 7847 | case PEER_CLIENT: |
| 7848 | /* already covered above */ |
| 7849 | return 0; |
| 7850 | case JOIN_GROUP: |
| 7851 | return wpas_p2p_nfc_join_group(wpa_s, ¶ms); |
| 7852 | case AUTH_JOIN: |
| 7853 | return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag); |
| 7854 | case INIT_GO_NEG: |
| 7855 | return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq); |
| 7856 | case RESP_GO_NEG: |
| 7857 | /* TODO: use own OOB Dev Pw */ |
| 7858 | return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq); |
| 7859 | } |
| 7860 | |
| 7861 | return -1; |
| 7862 | } |
| 7863 | |
| 7864 | |
| 7865 | int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s, |
| 7866 | const struct wpabuf *data, int forced_freq) |
| 7867 | { |
| 7868 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7869 | return -1; |
| 7870 | |
| 7871 | return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq); |
| 7872 | } |
| 7873 | |
| 7874 | |
| 7875 | int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init, |
| 7876 | const struct wpabuf *req, |
| 7877 | const struct wpabuf *sel, int forced_freq) |
| 7878 | { |
| 7879 | struct wpabuf *tmp; |
| 7880 | int ret; |
| 7881 | |
| 7882 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 7883 | return -1; |
| 7884 | |
| 7885 | wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported"); |
| 7886 | |
| 7887 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req", |
| 7888 | wpabuf_head(req), wpabuf_len(req)); |
| 7889 | wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel", |
| 7890 | wpabuf_head(sel), wpabuf_len(sel)); |
| 7891 | if (forced_freq) |
| 7892 | wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq); |
| 7893 | tmp = ndef_parse_p2p(init ? sel : req); |
| 7894 | if (tmp == NULL) { |
| 7895 | wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF"); |
| 7896 | return -1; |
| 7897 | } |
| 7898 | |
| 7899 | ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0, |
| 7900 | forced_freq); |
| 7901 | wpabuf_free(tmp); |
| 7902 | |
| 7903 | return ret; |
| 7904 | } |
| 7905 | |
| 7906 | |
| 7907 | int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled) |
| 7908 | { |
| 7909 | const u8 *if_addr; |
| 7910 | int go_intent = wpa_s->conf->p2p_go_intent; |
| 7911 | struct wpa_supplicant *iface; |
| 7912 | |
| 7913 | if (wpa_s->global->p2p == NULL) |
| 7914 | return -1; |
| 7915 | |
| 7916 | if (!enabled) { |
| 7917 | wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag"); |
| 7918 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 7919 | { |
| 7920 | if (!iface->ap_iface) |
| 7921 | continue; |
| 7922 | hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]); |
| 7923 | } |
| 7924 | p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0, |
| 7925 | 0, NULL); |
| 7926 | if (wpa_s->p2p_nfc_tag_enabled) |
| 7927 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 7928 | wpa_s->p2p_nfc_tag_enabled = 0; |
| 7929 | return 0; |
| 7930 | } |
| 7931 | |
| 7932 | if (wpa_s->global->p2p_disabled) |
| 7933 | return -1; |
| 7934 | |
| 7935 | if (wpa_s->conf->wps_nfc_dh_pubkey == NULL || |
| 7936 | wpa_s->conf->wps_nfc_dh_privkey == NULL || |
| 7937 | wpa_s->conf->wps_nfc_dev_pw == NULL || |
| 7938 | wpa_s->conf->wps_nfc_dev_pw_id < 0x10) { |
| 7939 | wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured " |
| 7940 | "to allow static handover cases"); |
| 7941 | return -1; |
| 7942 | } |
| 7943 | |
| 7944 | wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag"); |
| 7945 | |
| 7946 | wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7947 | wpabuf_free(wpa_s->p2p_oob_dev_pw); |
| 7948 | wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7949 | if (wpa_s->p2p_oob_dev_pw == NULL) |
| 7950 | return -1; |
| 7951 | wpa_s->p2p_peer_oob_pk_hash_known = 0; |
| 7952 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 7953 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO || |
| 7954 | wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) { |
| 7955 | /* |
| 7956 | * P2P Group Interface present and the command came on group |
| 7957 | * interface, so enable the token for the current interface. |
| 7958 | */ |
| 7959 | wpa_s->create_p2p_iface = 0; |
| 7960 | } else { |
| 7961 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 7962 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 7963 | |
| 7964 | if (wpa_s->create_p2p_iface) { |
| 7965 | enum wpa_driver_if_type iftype; |
| 7966 | /* Prepare to add a new interface for the group */ |
| 7967 | iftype = WPA_IF_P2P_GROUP; |
| 7968 | if (go_intent == 15) |
| 7969 | iftype = WPA_IF_P2P_GO; |
| 7970 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 7971 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 7972 | "interface for the group"); |
| 7973 | return -1; |
| 7974 | } |
| 7975 | |
| 7976 | if_addr = wpa_s->pending_interface_addr; |
| 7977 | } else |
| 7978 | if_addr = wpa_s->own_addr; |
| 7979 | |
| 7980 | wpa_s->p2p_nfc_tag_enabled = enabled; |
| 7981 | |
| 7982 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 7983 | struct hostapd_data *hapd; |
| 7984 | if (iface->ap_iface == NULL) |
| 7985 | continue; |
| 7986 | hapd = iface->ap_iface->bss[0]; |
| 7987 | wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); |
| 7988 | hapd->conf->wps_nfc_dh_pubkey = |
| 7989 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey); |
| 7990 | wpabuf_free(hapd->conf->wps_nfc_dh_privkey); |
| 7991 | hapd->conf->wps_nfc_dh_privkey = |
| 7992 | wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey); |
| 7993 | wpabuf_free(hapd->conf->wps_nfc_dev_pw); |
| 7994 | hapd->conf->wps_nfc_dev_pw = |
| 7995 | wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); |
| 7996 | hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; |
| 7997 | |
| 7998 | if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) { |
| 7999 | wpa_dbg(iface, MSG_DEBUG, |
| 8000 | "P2P: Failed to enable NFC Tag for GO"); |
| 8001 | } |
| 8002 | } |
| 8003 | p2p_set_authorized_oob_dev_pw_id( |
| 8004 | wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent, |
| 8005 | if_addr); |
| 8006 | |
| 8007 | return 0; |
| 8008 | } |
| 8009 | |
| 8010 | #endif /* CONFIG_WPS_NFC */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 8011 | |
| 8012 | |
| 8013 | static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, |
| 8014 | struct wpa_used_freq_data *freqs, |
| 8015 | unsigned int num) |
| 8016 | { |
| 8017 | u8 curr_chan, cand, chan; |
| 8018 | unsigned int i; |
| 8019 | |
| 8020 | curr_chan = p2p_get_listen_channel(wpa_s->global->p2p); |
| 8021 | for (i = 0, cand = 0; i < num; i++) { |
| 8022 | ieee80211_freq_to_chan(freqs[i].freq, &chan); |
| 8023 | if (curr_chan == chan) { |
| 8024 | cand = 0; |
| 8025 | break; |
| 8026 | } |
| 8027 | |
| 8028 | if (chan == 1 || chan == 6 || chan == 11) |
| 8029 | cand = chan; |
| 8030 | } |
| 8031 | |
| 8032 | if (cand) { |
| 8033 | wpa_dbg(wpa_s, MSG_DEBUG, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 8034 | "P2P: Update Listen channel to %u based on operating channel", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 8035 | cand); |
| 8036 | p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0); |
| 8037 | } |
| 8038 | } |
| 8039 | |
| 8040 | |
| 8041 | void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s) |
| 8042 | { |
| 8043 | struct wpa_used_freq_data *freqs; |
| 8044 | unsigned int num = wpa_s->num_multichan_concurrent; |
| 8045 | |
| 8046 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 8047 | return; |
| 8048 | |
| 8049 | /* |
| 8050 | * If possible, optimize the Listen channel to be a channel that is |
| 8051 | * already used by one of the other interfaces. |
| 8052 | */ |
| 8053 | if (!wpa_s->conf->p2p_optimize_listen_chan) |
| 8054 | return; |
| 8055 | |
| 8056 | if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED) |
| 8057 | return; |
| 8058 | |
| 8059 | freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); |
| 8060 | if (!freqs) |
| 8061 | return; |
| 8062 | |
| 8063 | num = get_shared_radio_freqs_data(wpa_s, freqs, num); |
| 8064 | |
| 8065 | wpas_p2p_optimize_listen_channel(wpa_s, freqs, num); |
| 8066 | os_free(freqs); |
| 8067 | } |
| 8068 | |
| 8069 | |
| 8070 | void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s) |
| 8071 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 8072 | if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) { |
| 8073 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing " |
| 8074 | "the management interface is being removed"); |
| 8075 | wpas_p2p_deinit_global(wpa_s->global); |
| 8076 | } |
| 8077 | } |
| 8078 | |
| 8079 | |
| 8080 | void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s) |
| 8081 | { |
| 8082 | if (wpa_s->ap_iface->bss) |
| 8083 | wpa_s->ap_iface->bss[0]->p2p_group = NULL; |
| 8084 | wpas_p2p_group_deinit(wpa_s); |
| 8085 | } |