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 | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 23 | #include "ap/p2p_hostapd.h" |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 24 | #include "eapol_supp/eapol_supp_sm.h" |
| 25 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | #include "wpa_supplicant_i.h" |
| 27 | #include "driver_i.h" |
| 28 | #include "ap.h" |
| 29 | #include "config_ssid.h" |
| 30 | #include "config.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 31 | #include "notify.h" |
| 32 | #include "scan.h" |
| 33 | #include "bss.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 34 | #include "offchannel.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 35 | #include "wps_supplicant.h" |
| 36 | #include "p2p_supplicant.h" |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 37 | #include "wifi_display.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | /* |
| 41 | * How many times to try to scan to find the GO before giving up on join |
| 42 | * request. |
| 43 | */ |
| 44 | #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 |
| 45 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 46 | #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 |
| 47 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 48 | #ifndef P2P_MAX_CLIENT_IDLE |
| 49 | /* |
| 50 | * How many seconds to try to reconnect to the GO when connection in P2P client |
| 51 | * role has been lost. |
| 52 | */ |
| 53 | #define P2P_MAX_CLIENT_IDLE 10 |
| 54 | #endif /* P2P_MAX_CLIENT_IDLE */ |
| 55 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 56 | #ifndef P2P_MAX_INITIAL_CONN_WAIT |
| 57 | /* |
| 58 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 59 | * WPS provisioning step. |
| 60 | */ |
| 61 | #define P2P_MAX_INITIAL_CONN_WAIT 10 |
| 62 | #endif /* P2P_MAX_INITIAL_CONN_WAIT */ |
| 63 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 64 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO |
| 65 | /* |
| 66 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 67 | * WPS provisioning step on the GO. This controls the extra time the P2P |
| 68 | * operation is considered to be in progress (e.g., to delay other scans) after |
| 69 | * WPS provisioning has been completed on the GO during group formation. |
| 70 | */ |
| 71 | #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 |
| 72 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ |
| 73 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 74 | #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE |
| 75 | /* |
| 76 | * How many seconds to wait for initial 4-way handshake to get completed after |
| 77 | * re-invocation of a persistent group on the GO when the client is expected |
| 78 | * to connect automatically (no user interaction). |
| 79 | */ |
| 80 | #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 |
| 81 | #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ |
| 82 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 83 | #ifndef P2P_CONCURRENT_SEARCH_DELAY |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 84 | #define P2P_CONCURRENT_SEARCH_DELAY 500 |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 85 | #endif /* P2P_CONCURRENT_SEARCH_DELAY */ |
| 86 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 87 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 88 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 89 | enum p2p_group_removal_reason { |
| 90 | P2P_GROUP_REMOVAL_UNKNOWN, |
| 91 | P2P_GROUP_REMOVAL_SILENT, |
| 92 | P2P_GROUP_REMOVAL_FORMATION_FAILED, |
| 93 | P2P_GROUP_REMOVAL_REQUESTED, |
| 94 | P2P_GROUP_REMOVAL_IDLE_TIMEOUT, |
| 95 | P2P_GROUP_REMOVAL_UNAVAILABLE, |
| 96 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 97 | P2P_GROUP_REMOVAL_PSK_FAILURE, |
| 98 | P2P_GROUP_REMOVAL_FREQ_CONFLICT |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Jouni Malinen | dc7b713 | 2012-09-14 12:53:47 -0700 | [diff] [blame] | 101 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 102 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); |
| 103 | static struct wpa_supplicant * |
| 104 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 105 | int go); |
| 106 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 107 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); |
| 109 | static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 110 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
| 111 | int auto_join); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 112 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); |
| 113 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); |
| 114 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); |
| 115 | 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] | 116 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 117 | void *timeout_ctx); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 118 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 119 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 120 | int group_added); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 121 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 122 | |
| 123 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Get the number of concurrent channels that the HW can operate, but that are |
| 126 | * currently not in use by any of the wpa_supplicant interfaces. |
| 127 | */ |
| 128 | static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) |
| 129 | { |
| 130 | int *freqs; |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 131 | int num, unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 132 | |
| 133 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 134 | if (!freqs) |
| 135 | return -1; |
| 136 | |
| 137 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 138 | wpa_s->num_multichan_concurrent); |
| 139 | os_free(freqs); |
| 140 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 141 | unused = wpa_s->num_multichan_concurrent - num; |
| 142 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); |
| 143 | return unused; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * Get the frequencies that are currently in use by one or more of the virtual |
| 149 | * interfaces, and that are also valid for P2P operation. |
| 150 | */ |
| 151 | static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, |
| 152 | int *p2p_freqs, unsigned int len) |
| 153 | { |
| 154 | int *freqs; |
| 155 | unsigned int num, i, j; |
| 156 | |
| 157 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 158 | if (!freqs) |
| 159 | return -1; |
| 160 | |
| 161 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 162 | wpa_s->num_multichan_concurrent); |
| 163 | |
| 164 | os_memset(p2p_freqs, 0, sizeof(int) * len); |
| 165 | |
| 166 | for (i = 0, j = 0; i < num && j < len; i++) { |
| 167 | if (p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 168 | p2p_freqs[j++] = freqs[i]; |
| 169 | } |
| 170 | |
| 171 | os_free(freqs); |
| 172 | |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 173 | dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j); |
| 174 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 175 | return j; |
| 176 | } |
| 177 | |
| 178 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 179 | static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, |
| 180 | int freq) |
| 181 | { |
| 182 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 183 | return; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 184 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 185 | freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 186 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 187 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", |
| 188 | freq); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 189 | freq = 0; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 190 | } |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 191 | p2p_set_own_freq_preference(wpa_s->global->p2p, freq); |
| 192 | } |
| 193 | |
| 194 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 195 | static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, |
| 196 | struct wpa_scan_results *scan_res) |
| 197 | { |
| 198 | size_t i; |
| 199 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 200 | if (wpa_s->p2p_scan_work) { |
| 201 | struct wpa_radio_work *work = wpa_s->p2p_scan_work; |
| 202 | wpa_s->p2p_scan_work = NULL; |
| 203 | radio_work_done(work); |
| 204 | } |
| 205 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 206 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 207 | return; |
| 208 | |
| 209 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", |
| 210 | (int) scan_res->num); |
| 211 | |
| 212 | for (i = 0; i < scan_res->num; i++) { |
| 213 | struct wpa_scan_res *bss = scan_res->res[i]; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 214 | struct os_reltime time_tmp_age, entry_ts; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 215 | const u8 *ies; |
| 216 | size_t ies_len; |
| 217 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 218 | time_tmp_age.sec = bss->age / 1000; |
| 219 | time_tmp_age.usec = (bss->age % 1000) * 1000; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 220 | os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 221 | |
| 222 | ies = (const u8 *) (bss + 1); |
| 223 | ies_len = bss->ie_len; |
| 224 | if (bss->beacon_ie_len > 0 && |
| 225 | !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && |
| 226 | wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { |
| 227 | wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " |
| 228 | MACSTR, MAC2STR(bss->bssid)); |
| 229 | ies = ies + ies_len; |
| 230 | ies_len = bss->beacon_ie_len; |
| 231 | } |
| 232 | |
| 233 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 234 | if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 235 | bss->freq, &entry_ts, bss->level, |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 236 | ies, ies_len) > 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | |
| 240 | p2p_scan_res_handled(wpa_s->global->p2p); |
| 241 | } |
| 242 | |
| 243 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 244 | static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) |
| 245 | { |
| 246 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 247 | struct wpa_driver_scan_params *params = work->ctx; |
| 248 | int ret; |
| 249 | |
| 250 | if (deinit) { |
| 251 | wpa_scan_free_params(params); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | ret = wpa_drv_scan(wpa_s, params); |
| 256 | wpa_scan_free_params(params); |
| 257 | work->ctx = NULL; |
| 258 | if (ret) { |
| 259 | radio_work_done(work); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | os_get_reltime(&wpa_s->scan_trigger_time); |
| 264 | wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; |
| 265 | wpa_s->own_scan_requested = 1; |
| 266 | wpa_s->p2p_scan_work = work; |
| 267 | } |
| 268 | |
| 269 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 270 | static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, |
| 271 | unsigned int num_req_dev_types, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 272 | const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 273 | { |
| 274 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 275 | struct wpa_driver_scan_params *params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 276 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 277 | size_t ielen; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 278 | u8 *n; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 279 | |
| 280 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 281 | return -1; |
| 282 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 283 | if (wpa_s->p2p_scan_work) { |
| 284 | wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); |
| 285 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 288 | params = os_zalloc(sizeof(*params)); |
| 289 | if (params == NULL) |
| 290 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | |
| 292 | /* P2P Wildcard SSID */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 293 | params->num_ssids = 1; |
| 294 | n = os_malloc(P2P_WILDCARD_SSID_LEN); |
| 295 | if (n == NULL) |
| 296 | goto fail; |
| 297 | os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); |
| 298 | params->ssids[0].ssid = n; |
| 299 | params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 300 | |
| 301 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 302 | wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, |
| 303 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 304 | num_req_dev_types, req_dev_types); |
| 305 | if (wps_ie == NULL) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 306 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 307 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 308 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 309 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 310 | if (ies == NULL) { |
| 311 | wpabuf_free(wps_ie); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 312 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 313 | } |
| 314 | wpabuf_put_buf(ies, wps_ie); |
| 315 | wpabuf_free(wps_ie); |
| 316 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 317 | p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 318 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 319 | params->p2p_probe = 1; |
| 320 | n = os_malloc(wpabuf_len(ies)); |
| 321 | if (n == NULL) { |
| 322 | wpabuf_free(ies); |
| 323 | goto fail; |
| 324 | } |
| 325 | os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); |
| 326 | params->extra_ies = n; |
| 327 | params->extra_ies_len = wpabuf_len(ies); |
| 328 | wpabuf_free(ies); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | |
| 330 | switch (type) { |
| 331 | case P2P_SCAN_SOCIAL: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 332 | params->freqs = os_malloc(4 * sizeof(int)); |
| 333 | if (params->freqs == NULL) |
| 334 | goto fail; |
| 335 | params->freqs[0] = 2412; |
| 336 | params->freqs[1] = 2437; |
| 337 | params->freqs[2] = 2462; |
| 338 | params->freqs[3] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 339 | break; |
| 340 | case P2P_SCAN_FULL: |
| 341 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 342 | case P2P_SCAN_SOCIAL_PLUS_ONE: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 343 | params->freqs = os_malloc(5 * sizeof(int)); |
| 344 | if (params->freqs == NULL) |
| 345 | goto fail; |
| 346 | params->freqs[0] = 2412; |
| 347 | params->freqs[1] = 2437; |
| 348 | params->freqs[2] = 2462; |
| 349 | params->freqs[3] = freq; |
| 350 | params->freqs[4] = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 354 | radio_remove_unstarted_work(wpa_s, "p2p-scan"); |
| 355 | if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, |
| 356 | params) < 0) |
| 357 | goto fail; |
| 358 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 359 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 360 | fail: |
| 361 | wpa_scan_free_params(params); |
| 362 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 366 | static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) |
| 367 | { |
| 368 | switch (p2p_group_interface) { |
| 369 | case P2P_GROUP_INTERFACE_PENDING: |
| 370 | return WPA_IF_P2P_GROUP; |
| 371 | case P2P_GROUP_INTERFACE_GO: |
| 372 | return WPA_IF_P2P_GO; |
| 373 | case P2P_GROUP_INTERFACE_CLIENT: |
| 374 | return WPA_IF_P2P_CLIENT; |
| 375 | } |
| 376 | |
| 377 | return WPA_IF_P2P_GROUP; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, |
| 382 | const u8 *ssid, |
| 383 | size_t ssid_len, int *go) |
| 384 | { |
| 385 | struct wpa_ssid *s; |
| 386 | |
| 387 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 388 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 389 | if (s->disabled != 0 || !s->p2p_group || |
| 390 | s->ssid_len != ssid_len || |
| 391 | os_memcmp(ssid, s->ssid, ssid_len) != 0) |
| 392 | continue; |
| 393 | if (s->mode == WPAS_MODE_P2P_GO && |
| 394 | s != wpa_s->current_ssid) |
| 395 | continue; |
| 396 | if (go) |
| 397 | *go = s->mode == WPAS_MODE_P2P_GO; |
| 398 | return wpa_s; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 406 | static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, |
| 407 | enum p2p_group_removal_reason removal_reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 408 | { |
| 409 | struct wpa_ssid *ssid; |
| 410 | char *gtype; |
| 411 | const char *reason; |
| 412 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 413 | ssid = wpa_s->current_ssid; |
| 414 | if (ssid == NULL) { |
| 415 | /* |
| 416 | * The current SSID was not known, but there may still be a |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 417 | * pending P2P group interface waiting for provisioning or a |
| 418 | * P2P group that is trying to reconnect. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 419 | */ |
| 420 | ssid = wpa_s->conf->ssid; |
| 421 | while (ssid) { |
Jouni Malinen | 9d71283 | 2012-10-05 11:01:57 -0700 | [diff] [blame] | 422 | if (ssid->p2p_group && ssid->disabled != 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 423 | break; |
| 424 | ssid = ssid->next; |
| 425 | } |
Jouni Malinen | 5c44edb | 2012-08-31 21:35:32 +0300 | [diff] [blame] | 426 | if (ssid == NULL && |
| 427 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) |
| 428 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 429 | wpa_printf(MSG_ERROR, "P2P: P2P group interface " |
| 430 | "not found"); |
| 431 | return -1; |
| 432 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 433 | } |
| 434 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) |
| 435 | gtype = "GO"; |
| 436 | else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || |
| 437 | (ssid && ssid->mode == WPAS_MODE_INFRA)) { |
| 438 | wpa_s->reassociate = 0; |
| 439 | wpa_s->disconnected = 1; |
| 440 | wpa_supplicant_deauthenticate(wpa_s, |
| 441 | WLAN_REASON_DEAUTH_LEAVING); |
| 442 | gtype = "client"; |
| 443 | } else |
| 444 | gtype = "GO"; |
| 445 | if (wpa_s->cross_connect_in_use) { |
| 446 | wpa_s->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 447 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 448 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 449 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 450 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 451 | switch (removal_reason) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 452 | case P2P_GROUP_REMOVAL_REQUESTED: |
| 453 | reason = " reason=REQUESTED"; |
| 454 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 455 | case P2P_GROUP_REMOVAL_FORMATION_FAILED: |
| 456 | reason = " reason=FORMATION_FAILED"; |
| 457 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 458 | case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: |
| 459 | reason = " reason=IDLE"; |
| 460 | break; |
| 461 | case P2P_GROUP_REMOVAL_UNAVAILABLE: |
| 462 | reason = " reason=UNAVAILABLE"; |
| 463 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 464 | case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: |
| 465 | reason = " reason=GO_ENDING_SESSION"; |
| 466 | break; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 467 | case P2P_GROUP_REMOVAL_PSK_FAILURE: |
| 468 | reason = " reason=PSK_FAILURE"; |
| 469 | break; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 470 | case P2P_GROUP_REMOVAL_FREQ_CONFLICT: |
| 471 | reason = " reason=FREQ_CONFLICT"; |
| 472 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 473 | default: |
| 474 | reason = ""; |
| 475 | break; |
| 476 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 477 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 478 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 479 | P2P_EVENT_GROUP_REMOVED "%s %s%s", |
| 480 | wpa_s->ifname, gtype, reason); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 481 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 482 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 483 | if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) |
| 484 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 485 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 486 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 487 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 488 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 489 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " |
| 490 | "timeout"); |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 491 | wpa_s->p2p_in_provisioning = 0; |
| 492 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 493 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 494 | /* |
| 495 | * Make sure wait for the first client does not remain active after the |
| 496 | * group has been removed. |
| 497 | */ |
| 498 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 499 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 500 | if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 501 | wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); |
| 502 | |
| 503 | if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { |
| 504 | struct wpa_global *global; |
| 505 | char *ifname; |
| 506 | enum wpa_driver_if_type type; |
| 507 | wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", |
| 508 | wpa_s->ifname); |
| 509 | global = wpa_s->global; |
| 510 | ifname = os_strdup(wpa_s->ifname); |
| 511 | type = wpas_p2p_if_type(wpa_s->p2p_group_interface); |
Dmitry Shmidt | e15c7b5 | 2011-08-03 15:04:35 -0700 | [diff] [blame] | 512 | wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 513 | wpa_s = global->ifaces; |
| 514 | if (wpa_s && ifname) |
| 515 | wpa_drv_if_remove(wpa_s, type, ifname); |
| 516 | os_free(ifname); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 517 | return 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 520 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 521 | wpa_s->global->p2p_group_formation = NULL; |
| 522 | wpa_s->p2p_in_provisioning = 0; |
| 523 | } |
| 524 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 525 | wpa_s->show_group_started = 0; |
| 526 | os_free(wpa_s->go_params); |
| 527 | wpa_s->go_params = NULL; |
| 528 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 529 | wpa_s->waiting_presence_resp = 0; |
| 530 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 531 | wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); |
| 532 | if (ssid && (ssid->p2p_group || |
| 533 | ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || |
| 534 | (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { |
| 535 | int id = ssid->id; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 536 | if (ssid == wpa_s->current_ssid) { |
| 537 | wpa_sm_set_config(wpa_s->wpa, NULL); |
| 538 | eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 539 | wpa_s->current_ssid = NULL; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 540 | } |
| 541 | /* |
| 542 | * Networks objects created during any P2P activities are not |
| 543 | * exposed out as they might/will confuse certain non-P2P aware |
| 544 | * applications since these network objects won't behave like |
| 545 | * regular ones. |
| 546 | * |
| 547 | * Likewise, we don't send out network removed signals for such |
| 548 | * network objects. |
| 549 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 550 | wpa_config_remove_network(wpa_s->conf, id); |
| 551 | wpa_supplicant_clear_status(wpa_s); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 552 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 553 | } else { |
| 554 | wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " |
| 555 | "found"); |
| 556 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 557 | if (wpa_s->ap_iface) |
| 558 | wpa_supplicant_ap_deinit(wpa_s); |
| 559 | else |
| 560 | wpa_drv_deinit_p2p_cli(wpa_s); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 561 | |
| 562 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, |
| 567 | u8 *go_dev_addr, |
| 568 | const u8 *ssid, size_t ssid_len) |
| 569 | { |
| 570 | struct wpa_bss *bss; |
| 571 | const u8 *bssid; |
| 572 | struct wpabuf *p2p; |
| 573 | u8 group_capab; |
| 574 | const u8 *addr; |
| 575 | |
| 576 | if (wpa_s->go_params) |
| 577 | bssid = wpa_s->go_params->peer_interface_addr; |
| 578 | else |
| 579 | bssid = wpa_s->bssid; |
| 580 | |
| 581 | bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); |
| 582 | if (bss == NULL) { |
| 583 | u8 iface_addr[ETH_ALEN]; |
| 584 | if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, |
| 585 | iface_addr) == 0) |
| 586 | bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); |
| 587 | } |
| 588 | if (bss == NULL) { |
| 589 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 590 | "group is persistent - BSS " MACSTR " not found", |
| 591 | MAC2STR(bssid)); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 596 | if (p2p == NULL) |
| 597 | p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, |
| 598 | P2P_IE_VENDOR_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 599 | if (p2p == NULL) { |
| 600 | wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " |
| 601 | "group is persistent - BSS " MACSTR |
| 602 | " did not include P2P IE", MAC2STR(bssid)); |
| 603 | wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", |
| 604 | (u8 *) (bss + 1), bss->ie_len); |
| 605 | wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", |
| 606 | ((u8 *) bss + 1) + bss->ie_len, |
| 607 | bss->beacon_ie_len); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | group_capab = p2p_get_group_capab(p2p); |
| 612 | addr = p2p_get_go_dev_addr(p2p); |
| 613 | wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " |
| 614 | "group_capab=0x%x", group_capab); |
| 615 | if (addr) { |
| 616 | os_memcpy(go_dev_addr, addr, ETH_ALEN); |
| 617 | wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, |
| 618 | MAC2STR(addr)); |
| 619 | } else |
| 620 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 621 | wpabuf_free(p2p); |
| 622 | |
| 623 | wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " |
| 624 | "go_dev_addr=" MACSTR, |
| 625 | MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); |
| 626 | |
| 627 | return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; |
| 628 | } |
| 629 | |
| 630 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 631 | static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, |
| 632 | struct wpa_ssid *ssid, |
| 633 | const u8 *go_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 634 | { |
| 635 | struct wpa_ssid *s; |
| 636 | int changed = 0; |
| 637 | |
| 638 | wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " |
| 639 | "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); |
| 640 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 641 | if (s->disabled == 2 && |
| 642 | os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && |
| 643 | s->ssid_len == ssid->ssid_len && |
| 644 | os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | if (s) { |
| 649 | wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " |
| 650 | "entry"); |
| 651 | if (ssid->passphrase && !s->passphrase) |
| 652 | changed = 1; |
| 653 | else if (ssid->passphrase && s->passphrase && |
| 654 | os_strcmp(ssid->passphrase, s->passphrase) != 0) |
| 655 | changed = 1; |
| 656 | } else { |
| 657 | wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " |
| 658 | "entry"); |
| 659 | changed = 1; |
| 660 | s = wpa_config_add_network(wpa_s->conf); |
| 661 | if (s == NULL) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 662 | return -1; |
| 663 | |
| 664 | /* |
| 665 | * Instead of network_added we emit persistent_group_added |
| 666 | * notification. Also to keep the defense checks in |
| 667 | * persistent_group obj registration method, we set the |
| 668 | * relevant flags in s to designate it as a persistent group. |
| 669 | */ |
| 670 | s->p2p_group = 1; |
| 671 | s->p2p_persistent_group = 1; |
| 672 | wpas_notify_persistent_group_added(wpa_s, s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 673 | wpa_config_set_network_defaults(s); |
| 674 | } |
| 675 | |
| 676 | s->p2p_group = 1; |
| 677 | s->p2p_persistent_group = 1; |
| 678 | s->disabled = 2; |
| 679 | s->bssid_set = 1; |
| 680 | os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); |
| 681 | s->mode = ssid->mode; |
| 682 | s->auth_alg = WPA_AUTH_ALG_OPEN; |
| 683 | s->key_mgmt = WPA_KEY_MGMT_PSK; |
| 684 | s->proto = WPA_PROTO_RSN; |
| 685 | s->pairwise_cipher = WPA_CIPHER_CCMP; |
| 686 | s->export_keys = 1; |
| 687 | if (ssid->passphrase) { |
| 688 | os_free(s->passphrase); |
| 689 | s->passphrase = os_strdup(ssid->passphrase); |
| 690 | } |
| 691 | if (ssid->psk_set) { |
| 692 | s->psk_set = 1; |
| 693 | os_memcpy(s->psk, ssid->psk, 32); |
| 694 | } |
| 695 | if (s->passphrase && !s->psk_set) |
| 696 | wpa_config_update_psk(s); |
| 697 | if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { |
| 698 | os_free(s->ssid); |
| 699 | s->ssid = os_malloc(ssid->ssid_len); |
| 700 | } |
| 701 | if (s->ssid) { |
| 702 | s->ssid_len = ssid->ssid_len; |
| 703 | os_memcpy(s->ssid, ssid->ssid, s->ssid_len); |
| 704 | } |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 705 | if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { |
| 706 | dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); |
| 707 | wpa_s->global->add_psk = NULL; |
| 708 | changed = 1; |
| 709 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 710 | |
| 711 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 712 | if (changed && wpa_s->conf->update_config && |
| 713 | wpa_config_write(wpa_s->confname, wpa_s->conf)) { |
| 714 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 715 | } |
| 716 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 717 | |
| 718 | return s->id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 722 | static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, |
| 723 | const u8 *addr) |
| 724 | { |
| 725 | struct wpa_ssid *ssid, *s; |
| 726 | u8 *n; |
| 727 | size_t i; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 728 | int found = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 729 | |
| 730 | ssid = wpa_s->current_ssid; |
| 731 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 732 | !ssid->p2p_persistent_group) |
| 733 | return; |
| 734 | |
| 735 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 736 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 737 | continue; |
| 738 | |
| 739 | if (s->ssid_len == ssid->ssid_len && |
| 740 | os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | if (s == NULL) |
| 745 | return; |
| 746 | |
| 747 | for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { |
| 748 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 749 | ETH_ALEN) != 0) |
| 750 | continue; |
| 751 | |
| 752 | if (i == s->num_p2p_clients - 1) |
| 753 | return; /* already the most recent entry */ |
| 754 | |
| 755 | /* move the entry to mark it most recent */ |
| 756 | os_memmove(s->p2p_client_list + i * ETH_ALEN, |
| 757 | s->p2p_client_list + (i + 1) * ETH_ALEN, |
| 758 | (s->num_p2p_clients - i - 1) * ETH_ALEN); |
| 759 | os_memcpy(s->p2p_client_list + |
| 760 | (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN); |
| 761 | found = 1; |
| 762 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 765 | if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { |
| 766 | n = os_realloc_array(s->p2p_client_list, |
| 767 | s->num_p2p_clients + 1, ETH_ALEN); |
| 768 | if (n == NULL) |
| 769 | return; |
| 770 | os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); |
| 771 | s->p2p_client_list = n; |
| 772 | s->num_p2p_clients++; |
| 773 | } else if (!found) { |
| 774 | /* Not enough room for an additional entry - drop the oldest |
| 775 | * entry */ |
| 776 | os_memmove(s->p2p_client_list, |
| 777 | s->p2p_client_list + ETH_ALEN, |
| 778 | (s->num_p2p_clients - 1) * ETH_ALEN); |
| 779 | os_memcpy(s->p2p_client_list + |
| 780 | (s->num_p2p_clients - 1) * ETH_ALEN, |
| 781 | addr, ETH_ALEN); |
| 782 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 783 | |
| 784 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 785 | if (wpa_s->parent->conf->update_config && |
| 786 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 787 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 788 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 789 | } |
| 790 | |
| 791 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 792 | static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, |
| 793 | int success) |
| 794 | { |
| 795 | struct wpa_ssid *ssid; |
| 796 | const char *ssid_txt; |
| 797 | int client; |
| 798 | int persistent; |
| 799 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 800 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * This callback is likely called for the main interface. Update wpa_s |
| 804 | * to use the group interface if a new interface was created for the |
| 805 | * group. |
| 806 | */ |
| 807 | if (wpa_s->global->p2p_group_formation) |
| 808 | wpa_s = wpa_s->global->p2p_group_formation; |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 809 | if (wpa_s->p2p_go_group_formation_completed) { |
| 810 | wpa_s->global->p2p_group_formation = NULL; |
| 811 | wpa_s->p2p_in_provisioning = 0; |
| 812 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 813 | |
| 814 | if (!success) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 815 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 816 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 817 | wpas_p2p_group_delete(wpa_s, |
| 818 | P2P_GROUP_REMOVAL_FORMATION_FAILED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 822 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 823 | P2P_EVENT_GROUP_FORMATION_SUCCESS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 824 | |
| 825 | ssid = wpa_s->current_ssid; |
| 826 | if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 827 | ssid->mode = WPAS_MODE_P2P_GO; |
| 828 | p2p_group_notif_formation_done(wpa_s->p2p_group); |
| 829 | wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); |
| 830 | } |
| 831 | |
| 832 | persistent = 0; |
| 833 | if (ssid) { |
| 834 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 835 | client = ssid->mode == WPAS_MODE_INFRA; |
| 836 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 837 | persistent = ssid->p2p_persistent_group; |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 838 | os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, |
| 839 | ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 840 | } else |
| 841 | persistent = wpas_p2p_persistent_group(wpa_s, |
| 842 | go_dev_addr, |
| 843 | ssid->ssid, |
| 844 | ssid->ssid_len); |
| 845 | } else { |
| 846 | ssid_txt = ""; |
| 847 | client = wpa_s->p2p_group_interface == |
| 848 | P2P_GROUP_INTERFACE_CLIENT; |
| 849 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 850 | } |
| 851 | |
| 852 | wpa_s->show_group_started = 0; |
| 853 | if (client) { |
| 854 | /* |
| 855 | * Indicate event only after successfully completed 4-way |
| 856 | * handshake, i.e., when the interface is ready for data |
| 857 | * packets. |
| 858 | */ |
| 859 | wpa_s->show_group_started = 1; |
| 860 | } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { |
| 861 | char psk[65]; |
| 862 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 863 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 864 | "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" |
| 865 | MACSTR "%s", |
| 866 | wpa_s->ifname, ssid_txt, ssid->frequency, psk, |
| 867 | MAC2STR(go_dev_addr), |
| 868 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 869 | wpas_p2p_cross_connect_setup(wpa_s); |
| 870 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 871 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 872 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 873 | "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " |
| 874 | "go_dev_addr=" MACSTR "%s", |
| 875 | wpa_s->ifname, ssid_txt, |
| 876 | ssid ? ssid->frequency : 0, |
| 877 | ssid && ssid->passphrase ? ssid->passphrase : "", |
| 878 | MAC2STR(go_dev_addr), |
| 879 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 880 | wpas_p2p_cross_connect_setup(wpa_s); |
| 881 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 882 | } |
| 883 | |
| 884 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 885 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 886 | ssid, go_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 887 | else { |
| 888 | os_free(wpa_s->global->add_psk); |
| 889 | wpa_s->global->add_psk = NULL; |
| 890 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 891 | if (network_id < 0 && ssid) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 892 | network_id = ssid->id; |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 893 | if (!client) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 894 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 895 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 896 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 900 | struct send_action_work { |
| 901 | unsigned int freq; |
| 902 | u8 dst[ETH_ALEN]; |
| 903 | u8 src[ETH_ALEN]; |
| 904 | u8 bssid[ETH_ALEN]; |
| 905 | size_t len; |
| 906 | unsigned int wait_time; |
| 907 | u8 buf[0]; |
| 908 | }; |
| 909 | |
| 910 | |
| 911 | static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, |
| 912 | void *timeout_ctx) |
| 913 | { |
| 914 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 915 | |
| 916 | if (!wpa_s->p2p_send_action_work) |
| 917 | return; |
| 918 | |
| 919 | wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); |
| 920 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 921 | radio_work_done(wpa_s->p2p_send_action_work); |
| 922 | wpa_s->p2p_send_action_work = NULL; |
| 923 | } |
| 924 | |
| 925 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 926 | static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, |
| 927 | unsigned int freq, |
| 928 | const u8 *dst, const u8 *src, |
| 929 | const u8 *bssid, |
| 930 | const u8 *data, size_t data_len, |
| 931 | enum offchannel_send_action_result |
| 932 | result) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 933 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 934 | enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 935 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 936 | if (wpa_s->p2p_send_action_work) { |
| 937 | struct send_action_work *awork; |
| 938 | awork = wpa_s->p2p_send_action_work->ctx; |
| 939 | if (awork->wait_time == 0) { |
| 940 | os_free(awork); |
| 941 | radio_work_done(wpa_s->p2p_send_action_work); |
| 942 | wpa_s->p2p_send_action_work = NULL; |
| 943 | } else { |
| 944 | /* |
| 945 | * In theory, this should not be needed, but number of |
| 946 | * places in the P2P code is still using non-zero wait |
| 947 | * time for the last Action frame in the sequence and |
| 948 | * some of these do not call send_action_done(). |
| 949 | */ |
| 950 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 951 | wpa_s, NULL); |
| 952 | eloop_register_timeout( |
| 953 | 0, awork->wait_time * 1000, |
| 954 | wpas_p2p_send_action_work_timeout, |
| 955 | wpa_s, NULL); |
| 956 | } |
| 957 | } |
| 958 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 959 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 960 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 961 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 962 | switch (result) { |
| 963 | case OFFCHANNEL_SEND_ACTION_SUCCESS: |
| 964 | res = P2P_SEND_ACTION_SUCCESS; |
| 965 | break; |
| 966 | case OFFCHANNEL_SEND_ACTION_NO_ACK: |
| 967 | res = P2P_SEND_ACTION_NO_ACK; |
| 968 | break; |
| 969 | case OFFCHANNEL_SEND_ACTION_FAILED: |
| 970 | res = P2P_SEND_ACTION_FAILED; |
| 971 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 974 | 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] | 975 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 976 | if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && |
| 977 | wpa_s->pending_pd_before_join && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 978 | (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 979 | os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && |
| 980 | wpa_s->p2p_fallback_to_go_neg) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 981 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 982 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " |
| 983 | "during p2p_connect-auto"); |
| 984 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 985 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 990 | static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) |
| 991 | { |
| 992 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 993 | struct send_action_work *awork = work->ctx; |
| 994 | |
| 995 | if (deinit) { |
| 996 | os_free(awork); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, |
| 1001 | awork->bssid, awork->buf, awork->len, |
| 1002 | awork->wait_time, |
| 1003 | wpas_p2p_send_action_tx_status, 1) < 0) { |
| 1004 | os_free(awork); |
| 1005 | radio_work_done(work); |
| 1006 | return; |
| 1007 | } |
| 1008 | wpa_s->p2p_send_action_work = work; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | static int wpas_send_action_work(struct wpa_supplicant *wpa_s, |
| 1013 | unsigned int freq, const u8 *dst, |
| 1014 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1015 | size_t len, unsigned int wait_time) |
| 1016 | { |
| 1017 | struct send_action_work *awork; |
| 1018 | |
| 1019 | if (wpa_s->p2p_send_action_work) { |
| 1020 | wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
| 1024 | awork = os_zalloc(sizeof(*awork) + len); |
| 1025 | if (awork == NULL) |
| 1026 | return -1; |
| 1027 | |
| 1028 | awork->freq = freq; |
| 1029 | os_memcpy(awork->dst, dst, ETH_ALEN); |
| 1030 | os_memcpy(awork->src, src, ETH_ALEN); |
| 1031 | os_memcpy(awork->bssid, bssid, ETH_ALEN); |
| 1032 | awork->len = len; |
| 1033 | awork->wait_time = wait_time; |
| 1034 | os_memcpy(awork->buf, buf, len); |
| 1035 | |
| 1036 | if (radio_add_work(wpa_s, freq, "p2p-send-action", 0, |
| 1037 | wpas_send_action_cb, awork) < 0) { |
| 1038 | os_free(awork); |
| 1039 | return -1; |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1046 | static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, |
| 1047 | const u8 *src, const u8 *bssid, const u8 *buf, |
| 1048 | size_t len, unsigned int wait_time) |
| 1049 | { |
| 1050 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1051 | int listen_freq = -1, send_freq = -1; |
| 1052 | |
| 1053 | if (wpa_s->p2p_listen_work) |
| 1054 | listen_freq = wpa_s->p2p_listen_work->freq; |
| 1055 | if (wpa_s->p2p_send_action_work) |
| 1056 | send_freq = wpa_s->p2p_send_action_work->freq; |
| 1057 | if (listen_freq != (int) freq && send_freq != (int) freq) { |
| 1058 | wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)", |
| 1059 | listen_freq, send_freq); |
| 1060 | return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, |
| 1061 | len, wait_time); |
| 1062 | } |
| 1063 | |
| 1064 | 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] | 1065 | return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, |
| 1066 | wait_time, |
| 1067 | wpas_p2p_send_action_tx_status, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | static void wpas_send_action_done(void *ctx) |
| 1072 | { |
| 1073 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1074 | |
| 1075 | if (wpa_s->p2p_send_action_work) { |
| 1076 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, |
| 1077 | wpa_s, NULL); |
| 1078 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 1079 | radio_work_done(wpa_s->p2p_send_action_work); |
| 1080 | wpa_s->p2p_send_action_work = NULL; |
| 1081 | } |
| 1082 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1083 | offchannel_send_action_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, |
| 1088 | struct p2p_go_neg_results *params) |
| 1089 | { |
| 1090 | if (wpa_s->go_params == NULL) { |
| 1091 | wpa_s->go_params = os_malloc(sizeof(*params)); |
| 1092 | if (wpa_s->go_params == NULL) |
| 1093 | return -1; |
| 1094 | } |
| 1095 | os_memcpy(wpa_s->go_params, params, sizeof(*params)); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, |
| 1101 | struct p2p_go_neg_results *res) |
| 1102 | { |
| 1103 | wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR, |
| 1104 | MAC2STR(res->peer_interface_addr)); |
| 1105 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", |
| 1106 | res->ssid, res->ssid_len); |
| 1107 | wpa_supplicant_ap_deinit(wpa_s); |
| 1108 | wpas_copy_go_neg_results(wpa_s, res); |
| 1109 | if (res->wps_method == WPS_PBC) |
| 1110 | wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); |
| 1111 | else { |
| 1112 | u16 dev_pw_id = DEV_PW_DEFAULT; |
| 1113 | if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) |
| 1114 | dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; |
| 1115 | wpas_wps_start_pin(wpa_s, res->peer_interface_addr, |
| 1116 | wpa_s->p2p_pin, 1, dev_pw_id); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1121 | static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, |
| 1122 | struct wpa_ssid *ssid) |
| 1123 | { |
| 1124 | struct wpa_ssid *persistent; |
| 1125 | struct psk_list_entry *psk; |
| 1126 | struct hostapd_data *hapd; |
| 1127 | |
| 1128 | if (!wpa_s->ap_iface) |
| 1129 | return; |
| 1130 | |
| 1131 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 1132 | ssid->ssid_len); |
| 1133 | if (persistent == NULL) |
| 1134 | return; |
| 1135 | |
| 1136 | hapd = wpa_s->ap_iface->bss[0]; |
| 1137 | |
| 1138 | dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, |
| 1139 | list) { |
| 1140 | struct hostapd_wpa_psk *hpsk; |
| 1141 | |
| 1142 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " |
| 1143 | MACSTR " psk=%d", |
| 1144 | MAC2STR(psk->addr), psk->p2p); |
| 1145 | hpsk = os_zalloc(sizeof(*hpsk)); |
| 1146 | if (hpsk == NULL) |
| 1147 | break; |
| 1148 | os_memcpy(hpsk->psk, psk->psk, PMK_LEN); |
| 1149 | if (psk->p2p) |
| 1150 | os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); |
| 1151 | else |
| 1152 | os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); |
| 1153 | hpsk->next = hapd->conf->ssid.wpa_psk; |
| 1154 | hapd->conf->ssid.wpa_psk = hpsk; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1159 | static void p2p_go_configured(void *ctx, void *data) |
| 1160 | { |
| 1161 | struct wpa_supplicant *wpa_s = ctx; |
| 1162 | struct p2p_go_neg_results *params = data; |
| 1163 | struct wpa_ssid *ssid; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1164 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1165 | |
| 1166 | ssid = wpa_s->current_ssid; |
| 1167 | if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { |
| 1168 | wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); |
| 1169 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 1170 | wpa_s->global->p2p_group_formation = NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1171 | if (os_strlen(params->passphrase) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1172 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1173 | P2P_EVENT_GROUP_STARTED |
| 1174 | "%s GO ssid=\"%s\" freq=%d " |
| 1175 | "passphrase=\"%s\" go_dev_addr=" MACSTR |
| 1176 | "%s", wpa_s->ifname, |
| 1177 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1178 | ssid->frequency, params->passphrase, |
| 1179 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1180 | params->persistent_group ? |
| 1181 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1182 | } else { |
| 1183 | char psk[65]; |
| 1184 | wpa_snprintf_hex(psk, sizeof(psk), params->psk, |
| 1185 | sizeof(params->psk)); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1186 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 1187 | P2P_EVENT_GROUP_STARTED |
| 1188 | "%s GO ssid=\"%s\" freq=%d psk=%s " |
| 1189 | "go_dev_addr=" MACSTR "%s", |
| 1190 | wpa_s->ifname, |
| 1191 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len), |
| 1192 | ssid->frequency, psk, |
| 1193 | MAC2STR(wpa_s->global->p2p_dev_addr), |
| 1194 | params->persistent_group ? |
| 1195 | " [PERSISTENT]" : ""); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1196 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1197 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1198 | os_get_reltime(&wpa_s->global->p2p_go_wait_client); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1199 | if (params->persistent_group) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1200 | network_id = wpas_p2p_store_persistent_group( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1201 | wpa_s->parent, ssid, |
Dmitry Shmidt | 497c1d5 | 2011-07-21 15:19:46 -0700 | [diff] [blame] | 1202 | wpa_s->global->p2p_dev_addr); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1203 | wpas_p2p_add_psk_list(wpa_s, ssid); |
| 1204 | } |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1205 | if (network_id < 0) |
| 1206 | network_id = ssid->id; |
| 1207 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1208 | wpas_p2p_cross_connect_setup(wpa_s); |
| 1209 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 1210 | |
| 1211 | if (wpa_s->p2p_first_connection_timeout) { |
| 1212 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1213 | "P2P: Start group formation timeout of %d seconds until first data connection on GO", |
| 1214 | wpa_s->p2p_first_connection_timeout); |
| 1215 | wpa_s->p2p_go_group_formation_completed = 0; |
| 1216 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1217 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1218 | wpa_s->parent, NULL); |
| 1219 | eloop_register_timeout( |
| 1220 | wpa_s->p2p_first_connection_timeout, 0, |
| 1221 | wpas_p2p_group_formation_timeout, |
| 1222 | wpa_s->parent, NULL); |
| 1223 | } |
| 1224 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); |
| 1229 | if (wpa_supplicant_ap_mac_addr_filter(wpa_s, |
| 1230 | params->peer_interface_addr)) { |
| 1231 | wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " |
| 1232 | "filtering"); |
| 1233 | return; |
| 1234 | } |
| 1235 | if (params->wps_method == WPS_PBC) |
| 1236 | wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1237 | params->peer_device_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1238 | else if (wpa_s->p2p_pin[0]) |
| 1239 | wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1240 | wpa_s->p2p_pin, NULL, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1241 | os_free(wpa_s->go_params); |
| 1242 | wpa_s->go_params = NULL; |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, |
| 1247 | struct p2p_go_neg_results *params, |
| 1248 | int group_formation) |
| 1249 | { |
| 1250 | struct wpa_ssid *ssid; |
| 1251 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1252 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); |
| 1253 | if (wpas_copy_go_neg_results(wpa_s, params) < 0) { |
| 1254 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " |
| 1255 | "results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1256 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1257 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1258 | |
| 1259 | ssid = wpa_config_add_network(wpa_s->conf); |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1260 | if (ssid == NULL) { |
| 1261 | 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] | 1262 | return; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1263 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1264 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1265 | wpa_s->show_group_started = 0; |
| 1266 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1267 | wpa_config_set_network_defaults(ssid); |
| 1268 | ssid->temporary = 1; |
| 1269 | ssid->p2p_group = 1; |
| 1270 | ssid->p2p_persistent_group = params->persistent_group; |
| 1271 | ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : |
| 1272 | WPAS_MODE_P2P_GO; |
| 1273 | ssid->frequency = params->freq; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1274 | ssid->ht40 = params->ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1275 | ssid->vht = params->vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1276 | ssid->ssid = os_zalloc(params->ssid_len + 1); |
| 1277 | if (ssid->ssid) { |
| 1278 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 1279 | ssid->ssid_len = params->ssid_len; |
| 1280 | } |
| 1281 | ssid->auth_alg = WPA_AUTH_ALG_OPEN; |
| 1282 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 1283 | ssid->proto = WPA_PROTO_RSN; |
| 1284 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1285 | if (os_strlen(params->passphrase) > 0) { |
| 1286 | ssid->passphrase = os_strdup(params->passphrase); |
| 1287 | if (ssid->passphrase == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1288 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 1289 | "P2P: Failed to copy passphrase for GO"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1290 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 1291 | return; |
| 1292 | } |
| 1293 | } else |
| 1294 | ssid->passphrase = NULL; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1295 | ssid->psk_set = params->psk_set; |
| 1296 | if (ssid->psk_set) |
| 1297 | os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1298 | else if (ssid->passphrase) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1299 | wpa_config_update_psk(ssid); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1300 | ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1301 | |
| 1302 | wpa_s->ap_configured_cb = p2p_go_configured; |
| 1303 | wpa_s->ap_configured_cb_ctx = wpa_s; |
| 1304 | wpa_s->ap_configured_cb_data = wpa_s->go_params; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 1305 | wpa_s->connect_without_scan = ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1306 | wpa_s->reassociate = 1; |
| 1307 | wpa_s->disconnected = 0; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 1308 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " |
| 1309 | "start GO)"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1310 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | static void wpas_p2p_clone_config(struct wpa_supplicant *dst, |
| 1315 | const struct wpa_supplicant *src) |
| 1316 | { |
| 1317 | struct wpa_config *d; |
| 1318 | const struct wpa_config *s; |
| 1319 | |
| 1320 | d = dst->conf; |
| 1321 | s = src->conf; |
| 1322 | |
| 1323 | #define C(n) if (s->n) d->n = os_strdup(s->n) |
| 1324 | C(device_name); |
| 1325 | C(manufacturer); |
| 1326 | C(model_name); |
| 1327 | C(model_number); |
| 1328 | C(serial_number); |
| 1329 | C(config_methods); |
| 1330 | #undef C |
| 1331 | |
| 1332 | os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); |
| 1333 | os_memcpy(d->sec_device_type, s->sec_device_type, |
| 1334 | sizeof(d->sec_device_type)); |
| 1335 | d->num_sec_device_types = s->num_sec_device_types; |
| 1336 | |
| 1337 | d->p2p_group_idle = s->p2p_group_idle; |
| 1338 | d->p2p_intra_bss = s->p2p_intra_bss; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1339 | d->persistent_reconnect = s->persistent_reconnect; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1340 | d->max_num_sta = s->max_num_sta; |
| 1341 | d->pbc_in_m1 = s->pbc_in_m1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1342 | d->ignore_old_scan_res = s->ignore_old_scan_res; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1343 | d->beacon_int = s->beacon_int; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1344 | d->disassoc_low_ack = s->disassoc_low_ack; |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1345 | d->disable_scan_offload = s->disable_scan_offload; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1349 | static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, |
| 1350 | char *ifname, size_t len) |
| 1351 | { |
| 1352 | char *ifname_ptr = wpa_s->ifname; |
| 1353 | |
| 1354 | if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, |
| 1355 | os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 1356 | ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; |
| 1357 | } |
| 1358 | |
| 1359 | os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); |
| 1360 | if (os_strlen(ifname) >= IFNAMSIZ && |
| 1361 | os_strlen(wpa_s->ifname) < IFNAMSIZ) { |
| 1362 | /* Try to avoid going over the IFNAMSIZ length limit */ |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1363 | os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1368 | static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, |
| 1369 | enum wpa_driver_if_type type) |
| 1370 | { |
| 1371 | char ifname[120], force_ifname[120]; |
| 1372 | |
| 1373 | if (wpa_s->pending_interface_name[0]) { |
| 1374 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " |
| 1375 | "- skip creation of a new one"); |
| 1376 | if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { |
| 1377 | wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " |
| 1378 | "unknown?! ifname='%s'", |
| 1379 | wpa_s->pending_interface_name); |
| 1380 | return -1; |
| 1381 | } |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1385 | wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1386 | force_ifname[0] = '\0'; |
| 1387 | |
| 1388 | wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", |
| 1389 | ifname); |
| 1390 | wpa_s->p2p_group_idx++; |
| 1391 | |
| 1392 | wpa_s->pending_interface_type = type; |
| 1393 | if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, |
| 1394 | wpa_s->pending_interface_addr, NULL) < 0) { |
| 1395 | wpa_printf(MSG_ERROR, "P2P: Failed to create new group " |
| 1396 | "interface"); |
| 1397 | return -1; |
| 1398 | } |
| 1399 | |
| 1400 | if (force_ifname[0]) { |
| 1401 | wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", |
| 1402 | force_ifname); |
| 1403 | os_strlcpy(wpa_s->pending_interface_name, force_ifname, |
| 1404 | sizeof(wpa_s->pending_interface_name)); |
| 1405 | } else |
| 1406 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 1407 | sizeof(wpa_s->pending_interface_name)); |
| 1408 | wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " |
| 1409 | MACSTR, wpa_s->pending_interface_name, |
| 1410 | MAC2STR(wpa_s->pending_interface_addr)); |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | static void wpas_p2p_remove_pending_group_interface( |
| 1417 | struct wpa_supplicant *wpa_s) |
| 1418 | { |
| 1419 | if (!wpa_s->pending_interface_name[0] || |
| 1420 | is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 1421 | return; /* No pending virtual interface */ |
| 1422 | |
| 1423 | wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", |
| 1424 | wpa_s->pending_interface_name); |
| 1425 | wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, |
| 1426 | wpa_s->pending_interface_name); |
| 1427 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1428 | wpa_s->pending_interface_name[0] = '\0'; |
| 1429 | } |
| 1430 | |
| 1431 | |
| 1432 | static struct wpa_supplicant * |
| 1433 | wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) |
| 1434 | { |
| 1435 | struct wpa_interface iface; |
| 1436 | struct wpa_supplicant *group_wpa_s; |
| 1437 | |
| 1438 | if (!wpa_s->pending_interface_name[0]) { |
| 1439 | wpa_printf(MSG_ERROR, "P2P: No pending group interface"); |
| 1440 | if (!wpas_p2p_create_iface(wpa_s)) |
| 1441 | return NULL; |
| 1442 | /* |
| 1443 | * Something has forced us to remove the pending interface; try |
| 1444 | * to create a new one and hope for the best that we will get |
| 1445 | * the same local address. |
| 1446 | */ |
| 1447 | if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : |
| 1448 | WPA_IF_P2P_CLIENT) < 0) |
| 1449 | return NULL; |
| 1450 | } |
| 1451 | |
| 1452 | os_memset(&iface, 0, sizeof(iface)); |
| 1453 | iface.ifname = wpa_s->pending_interface_name; |
| 1454 | iface.driver = wpa_s->driver->name; |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1455 | if (wpa_s->conf->ctrl_interface == NULL && |
| 1456 | wpa_s->parent != wpa_s && |
| 1457 | wpa_s->p2p_mgmt && |
| 1458 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) |
| 1459 | iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; |
| 1460 | else |
| 1461 | iface.ctrl_interface = wpa_s->conf->ctrl_interface; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1462 | iface.driver_param = wpa_s->conf->driver_param; |
| 1463 | group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 1464 | if (group_wpa_s == NULL) { |
| 1465 | wpa_printf(MSG_ERROR, "P2P: Failed to create new " |
| 1466 | "wpa_supplicant interface"); |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | wpa_s->pending_interface_name[0] = '\0'; |
| 1470 | group_wpa_s->parent = wpa_s; |
| 1471 | group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : |
| 1472 | P2P_GROUP_INTERFACE_CLIENT; |
| 1473 | wpa_s->global->p2p_group_formation = group_wpa_s; |
| 1474 | |
| 1475 | wpas_p2p_clone_config(group_wpa_s, wpa_s); |
| 1476 | |
| 1477 | return group_wpa_s; |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | static void wpas_p2p_group_formation_timeout(void *eloop_ctx, |
| 1482 | void *timeout_ctx) |
| 1483 | { |
| 1484 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 1485 | wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 1486 | wpas_p2p_group_formation_failed(wpa_s); |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s) |
| 1491 | { |
| 1492 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1493 | wpa_s->parent, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1494 | if (wpa_s->global->p2p) |
| 1495 | p2p_group_formation_failed(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1496 | wpas_group_formation_completed(wpa_s, 0); |
| 1497 | } |
| 1498 | |
| 1499 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1500 | static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) |
| 1501 | { |
| 1502 | wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); |
| 1503 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1504 | wpa_s->parent, NULL); |
| 1505 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1506 | wpa_s->parent, NULL); |
| 1507 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 1508 | } |
| 1509 | |
| 1510 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1511 | void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) |
| 1512 | { |
| 1513 | if (wpa_s->global->p2p_group_formation != wpa_s) |
| 1514 | return; |
| 1515 | /* Speed up group formation timeout since this cannot succeed */ |
| 1516 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 1517 | wpa_s->parent, NULL); |
| 1518 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 1519 | wpa_s->parent, NULL); |
| 1520 | } |
| 1521 | |
| 1522 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1523 | 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] | 1524 | { |
| 1525 | struct wpa_supplicant *wpa_s = ctx; |
| 1526 | |
| 1527 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1528 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1529 | wpa_s->off_channel_freq = 0; |
| 1530 | wpa_s->roc_waiting_drv_freq = 0; |
| 1531 | } |
| 1532 | |
| 1533 | if (res->status) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1534 | wpa_msg_global(wpa_s, MSG_INFO, |
| 1535 | P2P_EVENT_GO_NEG_FAILURE "status=%d", |
| 1536 | res->status); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1537 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1538 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1542 | if (wpa_s->p2p_go_ht40) |
| 1543 | res->ht40 = 1; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1544 | if (wpa_s->p2p_go_vht) |
| 1545 | res->vht = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1546 | |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 1547 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " |
| 1548 | "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR |
| 1549 | " wps_method=%s", |
| 1550 | res->role_go ? "GO" : "client", res->freq, res->ht40, |
| 1551 | MAC2STR(res->peer_device_addr), |
| 1552 | MAC2STR(res->peer_interface_addr), |
| 1553 | p2p_wps_method_text(res->wps_method)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1554 | wpas_notify_p2p_go_neg_completed(wpa_s, res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1555 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1556 | if (res->role_go && wpa_s->p2p_persistent_id >= 0) { |
| 1557 | struct wpa_ssid *ssid; |
| 1558 | ssid = wpa_config_get_network(wpa_s->conf, |
| 1559 | wpa_s->p2p_persistent_id); |
| 1560 | if (ssid && ssid->disabled == 2 && |
| 1561 | ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { |
| 1562 | size_t len = os_strlen(ssid->passphrase); |
| 1563 | wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " |
| 1564 | "on requested persistent group"); |
| 1565 | os_memcpy(res->passphrase, ssid->passphrase, len); |
| 1566 | res->passphrase[len] = '\0'; |
| 1567 | } |
| 1568 | } |
| 1569 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1570 | if (wpa_s->create_p2p_iface) { |
| 1571 | struct wpa_supplicant *group_wpa_s = |
| 1572 | wpas_p2p_init_group_interface(wpa_s, res->role_go); |
| 1573 | if (group_wpa_s == NULL) { |
| 1574 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 1575 | return; |
| 1576 | } |
| 1577 | if (group_wpa_s != wpa_s) { |
| 1578 | os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, |
| 1579 | sizeof(group_wpa_s->p2p_pin)); |
| 1580 | group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; |
| 1581 | } |
| 1582 | os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); |
| 1583 | wpa_s->pending_interface_name[0] = '\0'; |
| 1584 | group_wpa_s->p2p_in_provisioning = 1; |
| 1585 | |
| 1586 | if (res->role_go) |
| 1587 | wpas_start_wps_go(group_wpa_s, res, 1); |
| 1588 | else |
| 1589 | wpas_start_wps_enrollee(group_wpa_s, res); |
| 1590 | } else { |
| 1591 | wpa_s->p2p_in_provisioning = 1; |
| 1592 | wpa_s->global->p2p_group_formation = wpa_s; |
| 1593 | |
| 1594 | if (res->role_go) |
| 1595 | wpas_start_wps_go(wpa_s, res, 1); |
| 1596 | else |
| 1597 | wpas_start_wps_enrollee(ctx, res); |
| 1598 | } |
| 1599 | |
| 1600 | wpa_s->p2p_long_listen = 0; |
| 1601 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 1602 | |
| 1603 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1604 | eloop_register_timeout(15 + res->peer_config_timeout / 100, |
| 1605 | (res->peer_config_timeout % 100) * 10000, |
| 1606 | wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 1607 | } |
| 1608 | |
| 1609 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1610 | 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] | 1611 | { |
| 1612 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1613 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR |
| 1614 | " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1615 | |
| 1616 | wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); |
| 1617 | } |
| 1618 | |
| 1619 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1620 | static void wpas_dev_found(void *ctx, const u8 *addr, |
| 1621 | const struct p2p_peer_info *info, |
| 1622 | int new_device) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1623 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1624 | #ifndef CONFIG_NO_STDOUT_DEBUG |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1625 | struct wpa_supplicant *wpa_s = ctx; |
| 1626 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1627 | char *wfd_dev_info_hex = NULL; |
| 1628 | |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1629 | #ifdef CONFIG_WIFI_DISPLAY |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1630 | wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, |
| 1631 | WFD_SUBELEM_DEVICE_INFO); |
Irfan Sheriff | 8367dc9 | 2012-09-09 17:08:19 -0700 | [diff] [blame] | 1632 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1633 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1634 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR |
| 1635 | " p2p_dev_addr=" MACSTR |
| 1636 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
| 1637 | "dev_capab=0x%x group_capab=0x%x%s%s", |
| 1638 | MAC2STR(addr), MAC2STR(info->p2p_device_addr), |
| 1639 | wps_dev_type_bin2str(info->pri_dev_type, devtype, |
| 1640 | sizeof(devtype)), |
| 1641 | info->device_name, info->config_methods, |
| 1642 | info->dev_capab, info->group_capab, |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1643 | wfd_dev_info_hex ? " wfd_dev_info=0x" : "", |
| 1644 | wfd_dev_info_hex ? wfd_dev_info_hex : ""); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1645 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1646 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 1647 | os_free(wfd_dev_info_hex); |
| 1648 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1649 | wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); |
| 1650 | } |
| 1651 | |
| 1652 | |
| 1653 | static void wpas_dev_lost(void *ctx, const u8 *dev_addr) |
| 1654 | { |
| 1655 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1656 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1657 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST |
| 1658 | "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1659 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1660 | wpas_notify_p2p_device_lost(wpa_s, dev_addr); |
| 1661 | } |
| 1662 | |
| 1663 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1664 | static void wpas_find_stopped(void *ctx) |
| 1665 | { |
| 1666 | struct wpa_supplicant *wpa_s = ctx; |
| 1667 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); |
| 1668 | } |
| 1669 | |
| 1670 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1671 | struct wpas_p2p_listen_work { |
| 1672 | unsigned int freq; |
| 1673 | unsigned int duration; |
| 1674 | struct wpabuf *probe_resp_ie; |
| 1675 | }; |
| 1676 | |
| 1677 | |
| 1678 | static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) |
| 1679 | { |
| 1680 | if (lwork == NULL) |
| 1681 | return; |
| 1682 | wpabuf_free(lwork->probe_resp_ie); |
| 1683 | os_free(lwork); |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) |
| 1688 | { |
| 1689 | struct wpas_p2p_listen_work *lwork; |
| 1690 | |
| 1691 | if (!wpa_s->p2p_listen_work) |
| 1692 | return; |
| 1693 | |
| 1694 | lwork = wpa_s->p2p_listen_work->ctx; |
| 1695 | wpas_p2p_listen_work_free(lwork); |
| 1696 | radio_work_done(wpa_s->p2p_listen_work); |
| 1697 | wpa_s->p2p_listen_work = NULL; |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) |
| 1702 | { |
| 1703 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 1704 | struct wpas_p2p_listen_work *lwork = work->ctx; |
| 1705 | |
| 1706 | if (deinit) { |
| 1707 | wpas_p2p_listen_work_free(lwork); |
| 1708 | return; |
| 1709 | } |
| 1710 | |
| 1711 | wpa_s->p2p_listen_work = work; |
| 1712 | |
| 1713 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); |
| 1714 | |
| 1715 | if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { |
| 1716 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " |
| 1717 | "report received Probe Request frames"); |
| 1718 | wpas_p2p_listen_work_done(wpa_s); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
| 1722 | wpa_s->pending_listen_freq = lwork->freq; |
| 1723 | wpa_s->pending_listen_duration = lwork->duration; |
| 1724 | |
| 1725 | if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0) |
| 1726 | { |
| 1727 | wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " |
| 1728 | "to remain on channel (%u MHz) for Listen " |
| 1729 | "state", lwork->freq); |
| 1730 | wpas_p2p_listen_work_done(wpa_s); |
| 1731 | wpa_s->pending_listen_freq = 0; |
| 1732 | return; |
| 1733 | } |
| 1734 | wpa_s->off_channel_freq = 0; |
| 1735 | wpa_s->roc_waiting_drv_freq = lwork->freq; |
| 1736 | } |
| 1737 | |
| 1738 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1739 | static int wpas_start_listen(void *ctx, unsigned int freq, |
| 1740 | unsigned int duration, |
| 1741 | const struct wpabuf *probe_resp_ie) |
| 1742 | { |
| 1743 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1744 | struct wpas_p2p_listen_work *lwork; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1745 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1746 | if (wpa_s->p2p_listen_work) { |
| 1747 | 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] | 1748 | return -1; |
| 1749 | } |
| 1750 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1751 | lwork = os_zalloc(sizeof(*lwork)); |
| 1752 | if (lwork == NULL) |
| 1753 | return -1; |
| 1754 | lwork->freq = freq; |
| 1755 | lwork->duration = duration; |
| 1756 | if (probe_resp_ie) { |
| 1757 | lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); |
| 1758 | if (lwork->probe_resp_ie == NULL) { |
| 1759 | wpas_p2p_listen_work_free(lwork); |
| 1760 | return -1; |
| 1761 | } |
| 1762 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1763 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1764 | if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, |
| 1765 | lwork) < 0) { |
| 1766 | wpas_p2p_listen_work_free(lwork); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1767 | return -1; |
| 1768 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1769 | |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | |
| 1774 | static void wpas_stop_listen(void *ctx) |
| 1775 | { |
| 1776 | struct wpa_supplicant *wpa_s = ctx; |
| 1777 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 1778 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 1779 | wpa_s->off_channel_freq = 0; |
| 1780 | wpa_s->roc_waiting_drv_freq = 0; |
| 1781 | } |
| 1782 | wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); |
| 1783 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 1784 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) |
| 1789 | { |
| 1790 | struct wpa_supplicant *wpa_s = ctx; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1791 | 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] | 1792 | } |
| 1793 | |
| 1794 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1795 | /* |
| 1796 | * DNS Header section is used only to calculate compression pointers, so the |
| 1797 | * contents of this data does not matter, but the length needs to be reserved |
| 1798 | * in the virtual packet. |
| 1799 | */ |
| 1800 | #define DNS_HEADER_LEN 12 |
| 1801 | |
| 1802 | /* |
| 1803 | * 27-octet in-memory packet from P2P specification containing two implied |
| 1804 | * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN |
| 1805 | */ |
| 1806 | #define P2P_SD_IN_MEMORY_LEN 27 |
| 1807 | |
| 1808 | static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start, |
| 1809 | u8 **spos, const u8 *end) |
| 1810 | { |
| 1811 | while (*spos < end) { |
| 1812 | u8 val = ((*spos)[0] & 0xc0) >> 6; |
| 1813 | int len; |
| 1814 | |
| 1815 | if (val == 1 || val == 2) { |
| 1816 | /* These are reserved values in RFC 1035 */ |
| 1817 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1818 | "sequence starting with 0x%x", val); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | |
| 1822 | if (val == 3) { |
| 1823 | u16 offset; |
| 1824 | u8 *spos_tmp; |
| 1825 | |
| 1826 | /* Offset */ |
| 1827 | if (*spos + 2 > end) { |
| 1828 | wpa_printf(MSG_DEBUG, "P2P: No room for full " |
| 1829 | "DNS offset field"); |
| 1830 | return -1; |
| 1831 | } |
| 1832 | |
| 1833 | offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1]; |
| 1834 | if (offset >= *spos - start) { |
| 1835 | wpa_printf(MSG_DEBUG, "P2P: Invalid DNS " |
| 1836 | "pointer offset %u", offset); |
| 1837 | return -1; |
| 1838 | } |
| 1839 | |
| 1840 | (*spos) += 2; |
| 1841 | spos_tmp = start + offset; |
| 1842 | return p2p_sd_dns_uncompress_label(upos, uend, start, |
| 1843 | &spos_tmp, |
| 1844 | *spos - 2); |
| 1845 | } |
| 1846 | |
| 1847 | /* Label */ |
| 1848 | len = (*spos)[0] & 0x3f; |
| 1849 | if (len == 0) |
| 1850 | return 0; |
| 1851 | |
| 1852 | (*spos)++; |
| 1853 | if (*spos + len > end) { |
| 1854 | wpa_printf(MSG_DEBUG, "P2P: Invalid domain name " |
| 1855 | "sequence - no room for label with length " |
| 1856 | "%u", len); |
| 1857 | return -1; |
| 1858 | } |
| 1859 | |
| 1860 | if (*upos + len + 2 > uend) |
| 1861 | return -2; |
| 1862 | |
| 1863 | os_memcpy(*upos, *spos, len); |
| 1864 | *spos += len; |
| 1865 | *upos += len; |
| 1866 | (*upos)[0] = '.'; |
| 1867 | (*upos)++; |
| 1868 | (*upos)[0] = '\0'; |
| 1869 | } |
| 1870 | |
| 1871 | return 0; |
| 1872 | } |
| 1873 | |
| 1874 | |
| 1875 | /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet. |
| 1876 | * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is |
| 1877 | * not large enough */ |
| 1878 | static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg, |
| 1879 | size_t msg_len, size_t offset) |
| 1880 | { |
| 1881 | /* 27-octet in-memory packet from P2P specification */ |
| 1882 | const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01" |
| 1883 | "\x04_udp\xC0\x11\x00\x0C\x00\x01"; |
| 1884 | u8 *tmp, *end, *spos; |
| 1885 | char *upos, *uend; |
| 1886 | int ret = 0; |
| 1887 | |
| 1888 | if (buf_len < 2) |
| 1889 | return -1; |
| 1890 | if (offset > msg_len) |
| 1891 | return -1; |
| 1892 | |
| 1893 | tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len); |
| 1894 | if (tmp == NULL) |
| 1895 | return -1; |
| 1896 | spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN; |
| 1897 | end = spos + msg_len; |
| 1898 | spos += offset; |
| 1899 | |
| 1900 | os_memset(tmp, 0, DNS_HEADER_LEN); |
| 1901 | os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN); |
| 1902 | os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len); |
| 1903 | |
| 1904 | upos = buf; |
| 1905 | uend = buf + buf_len; |
| 1906 | |
| 1907 | ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end); |
| 1908 | if (ret) { |
| 1909 | os_free(tmp); |
| 1910 | return ret; |
| 1911 | } |
| 1912 | |
| 1913 | if (upos == buf) { |
| 1914 | upos[0] = '.'; |
| 1915 | upos[1] = '\0'; |
| 1916 | } else if (upos[-1] == '.') |
| 1917 | upos[-1] = '\0'; |
| 1918 | |
| 1919 | os_free(tmp); |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
| 1923 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1924 | static struct p2p_srv_bonjour * |
| 1925 | wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, |
| 1926 | const struct wpabuf *query) |
| 1927 | { |
| 1928 | struct p2p_srv_bonjour *bsrv; |
| 1929 | size_t len; |
| 1930 | |
| 1931 | len = wpabuf_len(query); |
| 1932 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1933 | struct p2p_srv_bonjour, list) { |
| 1934 | if (len == wpabuf_len(bsrv->query) && |
| 1935 | os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), |
| 1936 | len) == 0) |
| 1937 | return bsrv; |
| 1938 | } |
| 1939 | return NULL; |
| 1940 | } |
| 1941 | |
| 1942 | |
| 1943 | static struct p2p_srv_upnp * |
| 1944 | wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 1945 | const char *service) |
| 1946 | { |
| 1947 | struct p2p_srv_upnp *usrv; |
| 1948 | |
| 1949 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 1950 | struct p2p_srv_upnp, list) { |
| 1951 | if (version == usrv->version && |
| 1952 | os_strcmp(service, usrv->service) == 0) |
| 1953 | return usrv; |
| 1954 | } |
| 1955 | return NULL; |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, |
| 1960 | u8 srv_trans_id) |
| 1961 | { |
| 1962 | u8 *len_pos; |
| 1963 | |
| 1964 | if (wpabuf_tailroom(resp) < 5) |
| 1965 | return; |
| 1966 | |
| 1967 | /* Length (to be filled) */ |
| 1968 | len_pos = wpabuf_put(resp, 2); |
| 1969 | wpabuf_put_u8(resp, srv_proto); |
| 1970 | wpabuf_put_u8(resp, srv_trans_id); |
| 1971 | /* Status Code */ |
| 1972 | wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); |
| 1973 | /* Response Data: empty */ |
| 1974 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 1975 | } |
| 1976 | |
| 1977 | |
| 1978 | static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, |
| 1979 | struct wpabuf *resp, u8 srv_trans_id) |
| 1980 | { |
| 1981 | struct p2p_srv_bonjour *bsrv; |
| 1982 | u8 *len_pos; |
| 1983 | |
| 1984 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); |
| 1985 | |
| 1986 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 1987 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 1988 | return; |
| 1989 | } |
| 1990 | |
| 1991 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 1992 | struct p2p_srv_bonjour, list) { |
| 1993 | if (wpabuf_tailroom(resp) < |
| 1994 | 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) |
| 1995 | return; |
| 1996 | /* Length (to be filled) */ |
| 1997 | len_pos = wpabuf_put(resp, 2); |
| 1998 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 1999 | wpabuf_put_u8(resp, srv_trans_id); |
| 2000 | /* Status Code */ |
| 2001 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2002 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2003 | wpabuf_head(bsrv->resp), |
| 2004 | wpabuf_len(bsrv->resp)); |
| 2005 | /* Response Data */ |
| 2006 | wpabuf_put_buf(resp, bsrv->query); /* Key */ |
| 2007 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2008 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2009 | 2); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2014 | static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query, |
| 2015 | size_t query_len) |
| 2016 | { |
| 2017 | char str_rx[256], str_srv[256]; |
| 2018 | |
| 2019 | if (query_len < 3 || wpabuf_len(bsrv->query) < 3) |
| 2020 | return 0; /* Too short to include DNS Type and Version */ |
| 2021 | if (os_memcmp(query + query_len - 3, |
| 2022 | wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3, |
| 2023 | 3) != 0) |
| 2024 | return 0; /* Mismatch in DNS Type or Version */ |
| 2025 | if (query_len == wpabuf_len(bsrv->query) && |
| 2026 | os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0) |
| 2027 | return 1; /* Binary match */ |
| 2028 | |
| 2029 | if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3, |
| 2030 | 0)) |
| 2031 | return 0; /* Failed to uncompress query */ |
| 2032 | if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv), |
| 2033 | wpabuf_head(bsrv->query), |
| 2034 | wpabuf_len(bsrv->query) - 3, 0)) |
| 2035 | return 0; /* Failed to uncompress service */ |
| 2036 | |
| 2037 | return os_strcmp(str_rx, str_srv) == 0; |
| 2038 | } |
| 2039 | |
| 2040 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2041 | static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, |
| 2042 | struct wpabuf *resp, u8 srv_trans_id, |
| 2043 | const u8 *query, size_t query_len) |
| 2044 | { |
| 2045 | struct p2p_srv_bonjour *bsrv; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2046 | u8 *len_pos; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2047 | int matches = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2048 | |
| 2049 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", |
| 2050 | query, query_len); |
| 2051 | if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2052 | wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); |
| 2053 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, |
| 2054 | srv_trans_id); |
| 2055 | return; |
| 2056 | } |
| 2057 | |
| 2058 | if (query_len == 0) { |
| 2059 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2060 | return; |
| 2061 | } |
| 2062 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2063 | dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, |
| 2064 | struct p2p_srv_bonjour, list) { |
| 2065 | if (!match_bonjour_query(bsrv, query, query_len)) |
| 2066 | continue; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2067 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2068 | if (wpabuf_tailroom(resp) < |
| 2069 | 5 + query_len + wpabuf_len(bsrv->resp)) |
| 2070 | return; |
| 2071 | |
| 2072 | matches++; |
| 2073 | |
| 2074 | /* Length (to be filled) */ |
| 2075 | len_pos = wpabuf_put(resp, 2); |
| 2076 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2077 | wpabuf_put_u8(resp, srv_trans_id); |
| 2078 | |
| 2079 | /* Status Code */ |
| 2080 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2081 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", |
| 2082 | wpabuf_head(bsrv->resp), |
| 2083 | wpabuf_len(bsrv->resp)); |
| 2084 | |
| 2085 | /* Response Data */ |
| 2086 | wpabuf_put_data(resp, query, query_len); /* Key */ |
| 2087 | wpabuf_put_buf(resp, bsrv->resp); /* Value */ |
| 2088 | |
| 2089 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2090 | } |
| 2091 | |
| 2092 | if (matches == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2093 | wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " |
| 2094 | "available"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2095 | if (wpabuf_tailroom(resp) < 5) |
| 2096 | return; |
| 2097 | |
| 2098 | /* Length (to be filled) */ |
| 2099 | len_pos = wpabuf_put(resp, 2); |
| 2100 | wpabuf_put_u8(resp, P2P_SERV_BONJOUR); |
| 2101 | wpabuf_put_u8(resp, srv_trans_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2102 | |
| 2103 | /* Status Code */ |
| 2104 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2105 | /* Response Data: empty */ |
| 2106 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2107 | 2); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2108 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, |
| 2113 | struct wpabuf *resp, u8 srv_trans_id) |
| 2114 | { |
| 2115 | struct p2p_srv_upnp *usrv; |
| 2116 | u8 *len_pos; |
| 2117 | |
| 2118 | wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); |
| 2119 | |
| 2120 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2121 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2122 | return; |
| 2123 | } |
| 2124 | |
| 2125 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2126 | struct p2p_srv_upnp, list) { |
| 2127 | if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) |
| 2128 | return; |
| 2129 | |
| 2130 | /* Length (to be filled) */ |
| 2131 | len_pos = wpabuf_put(resp, 2); |
| 2132 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2133 | wpabuf_put_u8(resp, srv_trans_id); |
| 2134 | |
| 2135 | /* Status Code */ |
| 2136 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2137 | /* Response Data */ |
| 2138 | wpabuf_put_u8(resp, usrv->version); |
| 2139 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2140 | usrv->service); |
| 2141 | wpabuf_put_str(resp, usrv->service); |
| 2142 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - |
| 2143 | 2); |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | |
| 2148 | static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, |
| 2149 | struct wpabuf *resp, u8 srv_trans_id, |
| 2150 | const u8 *query, size_t query_len) |
| 2151 | { |
| 2152 | struct p2p_srv_upnp *usrv; |
| 2153 | u8 *len_pos; |
| 2154 | u8 version; |
| 2155 | char *str; |
| 2156 | int count = 0; |
| 2157 | |
| 2158 | wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", |
| 2159 | query, query_len); |
| 2160 | |
| 2161 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { |
| 2162 | wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); |
| 2163 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, |
| 2164 | srv_trans_id); |
| 2165 | return; |
| 2166 | } |
| 2167 | |
| 2168 | if (query_len == 0) { |
| 2169 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2170 | return; |
| 2171 | } |
| 2172 | |
| 2173 | if (wpabuf_tailroom(resp) < 5) |
| 2174 | return; |
| 2175 | |
| 2176 | /* Length (to be filled) */ |
| 2177 | len_pos = wpabuf_put(resp, 2); |
| 2178 | wpabuf_put_u8(resp, P2P_SERV_UPNP); |
| 2179 | wpabuf_put_u8(resp, srv_trans_id); |
| 2180 | |
| 2181 | version = query[0]; |
| 2182 | str = os_malloc(query_len); |
| 2183 | if (str == NULL) |
| 2184 | return; |
| 2185 | os_memcpy(str, query + 1, query_len - 1); |
| 2186 | str[query_len - 1] = '\0'; |
| 2187 | |
| 2188 | dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, |
| 2189 | struct p2p_srv_upnp, list) { |
| 2190 | if (version != usrv->version) |
| 2191 | continue; |
| 2192 | |
| 2193 | if (os_strcmp(str, "ssdp:all") != 0 && |
| 2194 | os_strstr(usrv->service, str) == NULL) |
| 2195 | continue; |
| 2196 | |
| 2197 | if (wpabuf_tailroom(resp) < 2) |
| 2198 | break; |
| 2199 | if (count == 0) { |
| 2200 | /* Status Code */ |
| 2201 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); |
| 2202 | /* Response Data */ |
| 2203 | wpabuf_put_u8(resp, version); |
| 2204 | } else |
| 2205 | wpabuf_put_u8(resp, ','); |
| 2206 | |
| 2207 | count++; |
| 2208 | |
| 2209 | wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", |
| 2210 | usrv->service); |
| 2211 | if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) |
| 2212 | break; |
| 2213 | wpabuf_put_str(resp, usrv->service); |
| 2214 | } |
| 2215 | os_free(str); |
| 2216 | |
| 2217 | if (count == 0) { |
| 2218 | wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " |
| 2219 | "available"); |
| 2220 | /* Status Code */ |
| 2221 | wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); |
| 2222 | /* Response Data: empty */ |
| 2223 | } |
| 2224 | |
| 2225 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2226 | } |
| 2227 | |
| 2228 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2229 | #ifdef CONFIG_WIFI_DISPLAY |
| 2230 | static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s, |
| 2231 | struct wpabuf *resp, u8 srv_trans_id, |
| 2232 | const u8 *query, size_t query_len) |
| 2233 | { |
| 2234 | const u8 *pos; |
| 2235 | u8 role; |
| 2236 | u8 *len_pos; |
| 2237 | |
| 2238 | wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len); |
| 2239 | |
| 2240 | if (!wpa_s->global->wifi_display) { |
| 2241 | wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available"); |
| 2242 | wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY, |
| 2243 | srv_trans_id); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | if (query_len < 1) { |
| 2248 | wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device " |
| 2249 | "Role"); |
| 2250 | return; |
| 2251 | } |
| 2252 | |
| 2253 | if (wpabuf_tailroom(resp) < 5) |
| 2254 | return; |
| 2255 | |
| 2256 | pos = query; |
| 2257 | role = *pos++; |
| 2258 | wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role); |
| 2259 | |
| 2260 | /* TODO: role specific handling */ |
| 2261 | |
| 2262 | /* Length (to be filled) */ |
| 2263 | len_pos = wpabuf_put(resp, 2); |
| 2264 | wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY); |
| 2265 | wpabuf_put_u8(resp, srv_trans_id); |
| 2266 | wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */ |
| 2267 | |
| 2268 | while (pos < query + query_len) { |
| 2269 | if (*pos < MAX_WFD_SUBELEMS && |
| 2270 | wpa_s->global->wfd_subelem[*pos] && |
| 2271 | wpabuf_tailroom(resp) >= |
| 2272 | wpabuf_len(wpa_s->global->wfd_subelem[*pos])) { |
| 2273 | wpa_printf(MSG_DEBUG, "P2P: Add WSD response " |
| 2274 | "subelement %u", *pos); |
| 2275 | wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]); |
| 2276 | } |
| 2277 | pos++; |
| 2278 | } |
| 2279 | |
| 2280 | WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); |
| 2281 | } |
| 2282 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2283 | |
| 2284 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2285 | static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, |
| 2286 | u16 update_indic, const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2287 | { |
| 2288 | struct wpa_supplicant *wpa_s = ctx; |
| 2289 | const u8 *pos = tlvs; |
| 2290 | const u8 *end = tlvs + tlvs_len; |
| 2291 | const u8 *tlv_end; |
| 2292 | u16 slen; |
| 2293 | struct wpabuf *resp; |
| 2294 | u8 srv_proto, srv_trans_id; |
| 2295 | size_t buf_len; |
| 2296 | char *buf; |
| 2297 | |
| 2298 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", |
| 2299 | tlvs, tlvs_len); |
| 2300 | buf_len = 2 * tlvs_len + 1; |
| 2301 | buf = os_malloc(buf_len); |
| 2302 | if (buf) { |
| 2303 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2304 | wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " |
| 2305 | MACSTR " %u %u %s", |
| 2306 | freq, MAC2STR(sa), dialog_token, update_indic, |
| 2307 | buf); |
| 2308 | os_free(buf); |
| 2309 | } |
| 2310 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2311 | if (wpa_s->p2p_sd_over_ctrl_iface) { |
| 2312 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2313 | update_indic, tlvs, tlvs_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2314 | return; /* to be processed by an external program */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2315 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2316 | |
| 2317 | resp = wpabuf_alloc(10000); |
| 2318 | if (resp == NULL) |
| 2319 | return; |
| 2320 | |
| 2321 | while (pos + 1 < end) { |
| 2322 | wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); |
| 2323 | slen = WPA_GET_LE16(pos); |
| 2324 | pos += 2; |
| 2325 | if (pos + slen > end || slen < 2) { |
| 2326 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " |
| 2327 | "length"); |
| 2328 | wpabuf_free(resp); |
| 2329 | return; |
| 2330 | } |
| 2331 | tlv_end = pos + slen; |
| 2332 | |
| 2333 | srv_proto = *pos++; |
| 2334 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2335 | srv_proto); |
| 2336 | srv_trans_id = *pos++; |
| 2337 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2338 | srv_trans_id); |
| 2339 | |
| 2340 | wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", |
| 2341 | pos, tlv_end - pos); |
| 2342 | |
| 2343 | |
| 2344 | if (wpa_s->force_long_sd) { |
| 2345 | wpa_printf(MSG_DEBUG, "P2P: SD test - force long " |
| 2346 | "response"); |
| 2347 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2348 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2349 | goto done; |
| 2350 | } |
| 2351 | |
| 2352 | switch (srv_proto) { |
| 2353 | case P2P_SERV_ALL_SERVICES: |
| 2354 | wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " |
| 2355 | "for all services"); |
| 2356 | if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && |
| 2357 | dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { |
| 2358 | wpa_printf(MSG_DEBUG, "P2P: No service " |
| 2359 | "discovery protocols available"); |
| 2360 | wpas_sd_add_proto_not_avail( |
| 2361 | resp, P2P_SERV_ALL_SERVICES, |
| 2362 | srv_trans_id); |
| 2363 | break; |
| 2364 | } |
| 2365 | wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); |
| 2366 | wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); |
| 2367 | break; |
| 2368 | case P2P_SERV_BONJOUR: |
| 2369 | wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, |
| 2370 | pos, tlv_end - pos); |
| 2371 | break; |
| 2372 | case P2P_SERV_UPNP: |
| 2373 | wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, |
| 2374 | pos, tlv_end - pos); |
| 2375 | break; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2376 | #ifdef CONFIG_WIFI_DISPLAY |
| 2377 | case P2P_SERV_WIFI_DISPLAY: |
| 2378 | wpas_sd_req_wfd(wpa_s, resp, srv_trans_id, |
| 2379 | pos, tlv_end - pos); |
| 2380 | break; |
| 2381 | #endif /* CONFIG_WIFI_DISPLAY */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2382 | default: |
| 2383 | wpa_printf(MSG_DEBUG, "P2P: Unavailable service " |
| 2384 | "protocol %u", srv_proto); |
| 2385 | wpas_sd_add_proto_not_avail(resp, srv_proto, |
| 2386 | srv_trans_id); |
| 2387 | break; |
| 2388 | } |
| 2389 | |
| 2390 | pos = tlv_end; |
| 2391 | } |
| 2392 | |
| 2393 | done: |
| 2394 | wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, |
| 2395 | update_indic, tlvs, tlvs_len); |
| 2396 | |
| 2397 | wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); |
| 2398 | |
| 2399 | wpabuf_free(resp); |
| 2400 | } |
| 2401 | |
| 2402 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2403 | static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, |
| 2404 | const u8 *tlvs, size_t tlvs_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2405 | { |
| 2406 | struct wpa_supplicant *wpa_s = ctx; |
| 2407 | const u8 *pos = tlvs; |
| 2408 | const u8 *end = tlvs + tlvs_len; |
| 2409 | const u8 *tlv_end; |
| 2410 | u16 slen; |
| 2411 | size_t buf_len; |
| 2412 | char *buf; |
| 2413 | |
| 2414 | wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", |
| 2415 | tlvs, tlvs_len); |
| 2416 | if (tlvs_len > 1500) { |
| 2417 | /* TODO: better way for handling this */ |
| 2418 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2419 | P2P_EVENT_SERV_DISC_RESP MACSTR |
| 2420 | " %u <long response: %u bytes>", |
| 2421 | MAC2STR(sa), update_indic, |
| 2422 | (unsigned int) tlvs_len); |
| 2423 | } else { |
| 2424 | buf_len = 2 * tlvs_len + 1; |
| 2425 | buf = os_malloc(buf_len); |
| 2426 | if (buf) { |
| 2427 | wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); |
| 2428 | wpa_msg_ctrl(wpa_s, MSG_INFO, |
| 2429 | P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", |
| 2430 | MAC2STR(sa), update_indic, buf); |
| 2431 | os_free(buf); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | while (pos < end) { |
| 2436 | u8 srv_proto, srv_trans_id, status; |
| 2437 | |
| 2438 | wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); |
| 2439 | slen = WPA_GET_LE16(pos); |
| 2440 | pos += 2; |
| 2441 | if (pos + slen > end || slen < 3) { |
| 2442 | wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " |
| 2443 | "length"); |
| 2444 | return; |
| 2445 | } |
| 2446 | tlv_end = pos + slen; |
| 2447 | |
| 2448 | srv_proto = *pos++; |
| 2449 | wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", |
| 2450 | srv_proto); |
| 2451 | srv_trans_id = *pos++; |
| 2452 | wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", |
| 2453 | srv_trans_id); |
| 2454 | status = *pos++; |
| 2455 | wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", |
| 2456 | status); |
| 2457 | |
| 2458 | wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", |
| 2459 | pos, tlv_end - pos); |
| 2460 | |
| 2461 | pos = tlv_end; |
| 2462 | } |
| 2463 | |
| 2464 | wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); |
| 2465 | } |
| 2466 | |
| 2467 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2468 | u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2469 | const struct wpabuf *tlvs) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2470 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2471 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2472 | return 0; |
| 2473 | return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2477 | u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2478 | u8 version, const char *query) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2479 | { |
| 2480 | struct wpabuf *tlvs; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2481 | u64 ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2482 | |
| 2483 | tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); |
| 2484 | if (tlvs == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2485 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2486 | wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); |
| 2487 | wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ |
| 2488 | wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ |
| 2489 | wpabuf_put_u8(tlvs, version); |
| 2490 | wpabuf_put_str(tlvs, query); |
| 2491 | ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); |
| 2492 | wpabuf_free(tlvs); |
| 2493 | return ret; |
| 2494 | } |
| 2495 | |
| 2496 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2497 | #ifdef CONFIG_WIFI_DISPLAY |
| 2498 | |
| 2499 | static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst, |
| 2500 | const struct wpabuf *tlvs) |
| 2501 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2502 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2503 | return 0; |
| 2504 | return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs); |
| 2505 | } |
| 2506 | |
| 2507 | |
| 2508 | #define MAX_WFD_SD_SUBELEMS 20 |
| 2509 | |
| 2510 | static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role, |
| 2511 | const char *subelems) |
| 2512 | { |
| 2513 | u8 *len; |
| 2514 | const char *pos; |
| 2515 | int val; |
| 2516 | int count = 0; |
| 2517 | |
| 2518 | len = wpabuf_put(tlvs, 2); |
| 2519 | wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */ |
| 2520 | wpabuf_put_u8(tlvs, id); /* Service Transaction ID */ |
| 2521 | |
| 2522 | wpabuf_put_u8(tlvs, role); |
| 2523 | |
| 2524 | pos = subelems; |
| 2525 | while (*pos) { |
| 2526 | val = atoi(pos); |
| 2527 | if (val >= 0 && val < 256) { |
| 2528 | wpabuf_put_u8(tlvs, val); |
| 2529 | count++; |
| 2530 | if (count == MAX_WFD_SD_SUBELEMS) |
| 2531 | break; |
| 2532 | } |
| 2533 | pos = os_strchr(pos + 1, ','); |
| 2534 | if (pos == NULL) |
| 2535 | break; |
| 2536 | pos++; |
| 2537 | } |
| 2538 | |
| 2539 | WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2); |
| 2540 | } |
| 2541 | |
| 2542 | |
| 2543 | u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s, |
| 2544 | const u8 *dst, const char *role) |
| 2545 | { |
| 2546 | struct wpabuf *tlvs; |
| 2547 | u64 ret; |
| 2548 | const char *subelems; |
| 2549 | u8 id = 1; |
| 2550 | |
| 2551 | subelems = os_strchr(role, ' '); |
| 2552 | if (subelems == NULL) |
| 2553 | return 0; |
| 2554 | subelems++; |
| 2555 | |
| 2556 | tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS)); |
| 2557 | if (tlvs == NULL) |
| 2558 | return 0; |
| 2559 | |
| 2560 | if (os_strstr(role, "[source]")) |
| 2561 | wfd_add_sd_req_role(tlvs, id++, 0x00, subelems); |
| 2562 | if (os_strstr(role, "[pri-sink]")) |
| 2563 | wfd_add_sd_req_role(tlvs, id++, 0x01, subelems); |
| 2564 | if (os_strstr(role, "[sec-sink]")) |
| 2565 | wfd_add_sd_req_role(tlvs, id++, 0x02, subelems); |
| 2566 | if (os_strstr(role, "[source+sink]")) |
| 2567 | wfd_add_sd_req_role(tlvs, id++, 0x03, subelems); |
| 2568 | |
| 2569 | ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs); |
| 2570 | wpabuf_free(tlvs); |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
| 2574 | #endif /* CONFIG_WIFI_DISPLAY */ |
| 2575 | |
| 2576 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2577 | int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2578 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2579 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2580 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2581 | return p2p_sd_cancel_request(wpa_s->global->p2p, |
| 2582 | (void *) (uintptr_t) req); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | |
| 2586 | void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, |
| 2587 | const u8 *dst, u8 dialog_token, |
| 2588 | const struct wpabuf *resp_tlvs) |
| 2589 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2590 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 2591 | return; |
| 2592 | p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, |
| 2593 | resp_tlvs); |
| 2594 | } |
| 2595 | |
Dmitry Shmidt | eaf261d | 2013-08-14 15:30:08 -0700 | [diff] [blame] | 2596 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2597 | void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) |
| 2598 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2599 | if (wpa_s->global->p2p) |
| 2600 | p2p_sd_service_update(wpa_s->global->p2p); |
| 2601 | } |
| 2602 | |
| 2603 | |
| 2604 | static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) |
| 2605 | { |
| 2606 | dl_list_del(&bsrv->list); |
| 2607 | wpabuf_free(bsrv->query); |
| 2608 | wpabuf_free(bsrv->resp); |
| 2609 | os_free(bsrv); |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) |
| 2614 | { |
| 2615 | dl_list_del(&usrv->list); |
| 2616 | os_free(usrv->service); |
| 2617 | os_free(usrv); |
| 2618 | } |
| 2619 | |
| 2620 | |
| 2621 | void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) |
| 2622 | { |
| 2623 | struct p2p_srv_bonjour *bsrv, *bn; |
| 2624 | struct p2p_srv_upnp *usrv, *un; |
| 2625 | |
| 2626 | dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, |
| 2627 | struct p2p_srv_bonjour, list) |
| 2628 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2629 | |
| 2630 | dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, |
| 2631 | struct p2p_srv_upnp, list) |
| 2632 | wpas_p2p_srv_upnp_free(usrv); |
| 2633 | |
| 2634 | wpas_p2p_sd_service_update(wpa_s); |
| 2635 | } |
| 2636 | |
| 2637 | |
| 2638 | int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, |
| 2639 | struct wpabuf *query, struct wpabuf *resp) |
| 2640 | { |
| 2641 | struct p2p_srv_bonjour *bsrv; |
| 2642 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2643 | bsrv = os_zalloc(sizeof(*bsrv)); |
| 2644 | if (bsrv == NULL) |
| 2645 | return -1; |
| 2646 | bsrv->query = query; |
| 2647 | bsrv->resp = resp; |
| 2648 | dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); |
| 2649 | |
| 2650 | wpas_p2p_sd_service_update(wpa_s); |
| 2651 | return 0; |
| 2652 | } |
| 2653 | |
| 2654 | |
| 2655 | int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, |
| 2656 | const struct wpabuf *query) |
| 2657 | { |
| 2658 | struct p2p_srv_bonjour *bsrv; |
| 2659 | |
| 2660 | bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); |
| 2661 | if (bsrv == NULL) |
| 2662 | return -1; |
| 2663 | wpas_p2p_srv_bonjour_free(bsrv); |
| 2664 | wpas_p2p_sd_service_update(wpa_s); |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | |
| 2669 | int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2670 | const char *service) |
| 2671 | { |
| 2672 | struct p2p_srv_upnp *usrv; |
| 2673 | |
| 2674 | if (wpas_p2p_service_get_upnp(wpa_s, version, service)) |
| 2675 | return 0; /* Already listed */ |
| 2676 | usrv = os_zalloc(sizeof(*usrv)); |
| 2677 | if (usrv == NULL) |
| 2678 | return -1; |
| 2679 | usrv->version = version; |
| 2680 | usrv->service = os_strdup(service); |
| 2681 | if (usrv->service == NULL) { |
| 2682 | os_free(usrv); |
| 2683 | return -1; |
| 2684 | } |
| 2685 | dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); |
| 2686 | |
| 2687 | wpas_p2p_sd_service_update(wpa_s); |
| 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | |
| 2692 | int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, |
| 2693 | const char *service) |
| 2694 | { |
| 2695 | struct p2p_srv_upnp *usrv; |
| 2696 | |
| 2697 | usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); |
| 2698 | if (usrv == NULL) |
| 2699 | return -1; |
| 2700 | wpas_p2p_srv_upnp_free(usrv); |
| 2701 | wpas_p2p_sd_service_update(wpa_s); |
| 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, |
| 2707 | const u8 *peer, const char *params, |
| 2708 | unsigned int generated_pin) |
| 2709 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2710 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR |
| 2711 | " %08d%s", MAC2STR(peer), generated_pin, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | |
| 2715 | static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, |
| 2716 | const u8 *peer, const char *params) |
| 2717 | { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2718 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR |
| 2719 | "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2723 | static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, |
| 2724 | const u8 *dev_addr, const u8 *pri_dev_type, |
| 2725 | const char *dev_name, u16 supp_config_methods, |
| 2726 | u8 dev_capab, u8 group_capab, const u8 *group_id, |
| 2727 | size_t group_id_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2728 | { |
| 2729 | struct wpa_supplicant *wpa_s = ctx; |
| 2730 | char devtype[WPS_DEV_TYPE_BUFSIZE]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2731 | char params[300]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2732 | u8 empty_dev_type[8]; |
| 2733 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2734 | struct wpa_supplicant *group = NULL; |
| 2735 | |
| 2736 | if (group_id) { |
| 2737 | for (group = wpa_s->global->ifaces; group; group = group->next) |
| 2738 | { |
| 2739 | struct wpa_ssid *s = group->current_ssid; |
| 2740 | if (s != NULL && |
| 2741 | s->mode == WPAS_MODE_P2P_GO && |
| 2742 | group_id_len - ETH_ALEN == s->ssid_len && |
| 2743 | os_memcmp(group_id + ETH_ALEN, s->ssid, |
| 2744 | s->ssid_len) == 0) |
| 2745 | break; |
| 2746 | } |
| 2747 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2748 | |
| 2749 | if (pri_dev_type == NULL) { |
| 2750 | os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); |
| 2751 | pri_dev_type = empty_dev_type; |
| 2752 | } |
| 2753 | os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR |
| 2754 | " pri_dev_type=%s name='%s' config_methods=0x%x " |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2755 | "dev_capab=0x%x group_capab=0x%x%s%s", |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2756 | MAC2STR(dev_addr), |
| 2757 | wps_dev_type_bin2str(pri_dev_type, devtype, |
| 2758 | sizeof(devtype)), |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2759 | dev_name, supp_config_methods, dev_capab, group_capab, |
| 2760 | group ? " group=" : "", |
| 2761 | group ? group->ifname : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2762 | params[sizeof(params) - 1] = '\0'; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2763 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2764 | if (config_methods & WPS_CONFIG_DISPLAY) { |
| 2765 | generated_pin = wps_generate_pin(); |
| 2766 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2767 | generated_pin); |
| 2768 | } else if (config_methods & WPS_CONFIG_KEYPAD) |
| 2769 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
| 2770 | else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2771 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ |
| 2772 | MACSTR "%s", MAC2STR(peer), params); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2773 | |
| 2774 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, |
| 2775 | P2P_PROV_DISC_SUCCESS, |
| 2776 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2780 | 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] | 2781 | { |
| 2782 | struct wpa_supplicant *wpa_s = ctx; |
| 2783 | unsigned int generated_pin = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2784 | char params[20]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2785 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2786 | if (wpa_s->pending_pd_before_join && |
| 2787 | (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || |
| 2788 | os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { |
| 2789 | wpa_s->pending_pd_before_join = 0; |
| 2790 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2791 | "join-existing-group operation"); |
| 2792 | wpas_p2p_join_start(wpa_s); |
| 2793 | return; |
| 2794 | } |
| 2795 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2796 | if (wpa_s->pending_pd_use == AUTO_PD_JOIN || |
| 2797 | wpa_s->pending_pd_use == AUTO_PD_GO_NEG) |
| 2798 | os_snprintf(params, sizeof(params), " peer_go=%d", |
| 2799 | wpa_s->pending_pd_use == AUTO_PD_JOIN); |
| 2800 | else |
| 2801 | params[0] = '\0'; |
| 2802 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2803 | if (config_methods & WPS_CONFIG_DISPLAY) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2804 | wpas_prov_disc_local_keypad(wpa_s, peer, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2805 | else if (config_methods & WPS_CONFIG_KEYPAD) { |
| 2806 | generated_pin = wps_generate_pin(); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2807 | wpas_prov_disc_local_display(wpa_s, peer, params, |
| 2808 | generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2809 | } else if (config_methods & WPS_CONFIG_PUSHBUTTON) |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2810 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP |
| 2811 | MACSTR "%s", MAC2STR(peer), params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2812 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2813 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2814 | P2P_PROV_DISC_SUCCESS, |
| 2815 | config_methods, generated_pin); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2819 | static void wpas_prov_disc_fail(void *ctx, const u8 *peer, |
| 2820 | enum p2p_prov_disc_status status) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2821 | { |
| 2822 | struct wpa_supplicant *wpa_s = ctx; |
| 2823 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2824 | if (wpa_s->p2p_fallback_to_go_neg) { |
| 2825 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " |
| 2826 | "failed - fall back to GO Negotiation"); |
| 2827 | wpas_p2p_fallback_to_go_neg(wpa_s, 0); |
| 2828 | return; |
| 2829 | } |
| 2830 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2831 | if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2832 | wpa_s->pending_pd_before_join = 0; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2833 | wpa_printf(MSG_DEBUG, "P2P: Starting pending " |
| 2834 | "join-existing-group operation (no ACK for PD " |
| 2835 | "Req attempts)"); |
| 2836 | wpas_p2p_join_start(wpa_s); |
| 2837 | return; |
Dmitry Shmidt | 2b38048 | 2012-09-13 10:52:13 -0700 | [diff] [blame] | 2838 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2839 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 2840 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE |
| 2841 | " p2p_dev_addr=" MACSTR " status=%d", |
| 2842 | MAC2STR(peer), status); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2843 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 2844 | wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, |
| 2845 | status, 0, 0); |
| 2846 | } |
| 2847 | |
| 2848 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2849 | static int freq_included(const struct p2p_channels *channels, unsigned int freq) |
| 2850 | { |
| 2851 | if (channels == NULL) |
| 2852 | return 1; /* Assume no restrictions */ |
| 2853 | return p2p_channels_includes_freq(channels, freq); |
| 2854 | |
| 2855 | } |
| 2856 | |
| 2857 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2858 | static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, |
| 2859 | const u8 *go_dev_addr, const u8 *ssid, |
| 2860 | size_t ssid_len, int *go, u8 *group_bssid, |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2861 | int *force_freq, int persistent_group, |
| 2862 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2863 | { |
| 2864 | struct wpa_supplicant *wpa_s = ctx; |
| 2865 | struct wpa_ssid *s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2866 | int res; |
| 2867 | struct wpa_supplicant *grp; |
| 2868 | |
| 2869 | if (!persistent_group) { |
| 2870 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2871 | " to join an active group", MAC2STR(sa)); |
| 2872 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2873 | (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) |
| 2874 | == 0 || |
| 2875 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { |
| 2876 | wpa_printf(MSG_DEBUG, "P2P: Accept previously " |
| 2877 | "authorized invitation"); |
| 2878 | goto accept_inv; |
| 2879 | } |
| 2880 | /* |
| 2881 | * Do not accept the invitation automatically; notify user and |
| 2882 | * request approval. |
| 2883 | */ |
| 2884 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2885 | } |
| 2886 | |
| 2887 | grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); |
| 2888 | if (grp) { |
| 2889 | wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " |
| 2890 | "running persistent group"); |
| 2891 | if (*go) |
| 2892 | os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); |
| 2893 | goto accept_inv; |
| 2894 | } |
| 2895 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 2896 | if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && |
| 2897 | os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { |
| 2898 | wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " |
| 2899 | "invitation to re-invoke a persistent group"); |
| 2900 | } else if (!wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2901 | return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; |
| 2902 | |
| 2903 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2904 | if (s->disabled == 2 && |
| 2905 | os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && |
| 2906 | s->ssid_len == ssid_len && |
| 2907 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2908 | break; |
| 2909 | } |
| 2910 | |
| 2911 | if (!s) { |
| 2912 | wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR |
| 2913 | " requested reinvocation of an unknown group", |
| 2914 | MAC2STR(sa)); |
| 2915 | return P2P_SC_FAIL_UNKNOWN_GROUP; |
| 2916 | } |
| 2917 | |
| 2918 | if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { |
| 2919 | *go = 1; |
| 2920 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { |
| 2921 | wpa_printf(MSG_DEBUG, "P2P: The only available " |
| 2922 | "interface is already in use - reject " |
| 2923 | "invitation"); |
| 2924 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2925 | } |
| 2926 | os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); |
| 2927 | } else if (s->mode == WPAS_MODE_P2P_GO) { |
| 2928 | *go = 1; |
| 2929 | if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) |
| 2930 | { |
| 2931 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 2932 | "interface address for the group"); |
| 2933 | return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; |
| 2934 | } |
| 2935 | os_memcpy(group_bssid, wpa_s->pending_interface_addr, |
| 2936 | ETH_ALEN); |
| 2937 | } |
| 2938 | |
| 2939 | accept_inv: |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2940 | wpas_p2p_set_own_freq_preference(wpa_s, 0); |
| 2941 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2942 | /* Get one of the frequencies currently in use */ |
| 2943 | if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) { |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2944 | wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces"); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 2945 | wpas_p2p_set_own_freq_preference(wpa_s, res); |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 2946 | |
| 2947 | if (wpa_s->num_multichan_concurrent < 2 || |
| 2948 | wpas_p2p_num_unused_channels(wpa_s) < 1) { |
| 2949 | wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); |
| 2950 | *force_freq = res; |
| 2951 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 2954 | if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && |
| 2955 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2956 | if (*go == 0) { |
| 2957 | /* We are the client */ |
| 2958 | wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " |
| 2959 | "running a GO but we are capable of MCC, " |
| 2960 | "figure out the best channel to use"); |
| 2961 | *force_freq = 0; |
| 2962 | } else if (!freq_included(channels, *force_freq)) { |
| 2963 | /* We are the GO, and *force_freq is not in the |
| 2964 | * intersection */ |
| 2965 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 2966 | "in intersection but we are capable of MCC, " |
| 2967 | "figure out the best channel to use", |
| 2968 | *force_freq); |
| 2969 | *force_freq = 0; |
| 2970 | } |
| 2971 | } |
| 2972 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2973 | return P2P_SC_SUCCESS; |
| 2974 | } |
| 2975 | |
| 2976 | |
| 2977 | static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, |
| 2978 | const u8 *ssid, size_t ssid_len, |
| 2979 | const u8 *go_dev_addr, u8 status, |
| 2980 | int op_freq) |
| 2981 | { |
| 2982 | struct wpa_supplicant *wpa_s = ctx; |
| 2983 | struct wpa_ssid *s; |
| 2984 | |
| 2985 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 2986 | if (s->disabled == 2 && |
| 2987 | s->ssid_len == ssid_len && |
| 2988 | os_memcmp(ssid, s->ssid, ssid_len) == 0) |
| 2989 | break; |
| 2990 | } |
| 2991 | |
| 2992 | if (status == P2P_SC_SUCCESS) { |
| 2993 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 2994 | " was accepted; op_freq=%d MHz", |
| 2995 | MAC2STR(sa), op_freq); |
| 2996 | if (s) { |
Dmitry Shmidt | 91c40cd | 2012-09-25 14:23:53 -0700 | [diff] [blame] | 2997 | int go = s->mode == WPAS_MODE_P2P_GO; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2998 | wpas_p2p_group_add_persistent( |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2999 | wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3000 | go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3001 | } else if (bssid) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3002 | wpa_s->user_initiated_pd = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3003 | wpas_p2p_join(wpa_s, bssid, go_dev_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3004 | wpa_s->p2p_wps_method, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3005 | } |
| 3006 | return; |
| 3007 | } |
| 3008 | |
| 3009 | if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3010 | wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR |
| 3011 | " was rejected (status %u)", MAC2STR(sa), status); |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | if (!s) { |
| 3016 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3017 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3018 | P2P_EVENT_INVITATION_RECEIVED |
| 3019 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3020 | " bssid=" MACSTR " unknown-network", |
| 3021 | MAC2STR(sa), MAC2STR(go_dev_addr), |
| 3022 | MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3023 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3024 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3025 | P2P_EVENT_INVITATION_RECEIVED |
| 3026 | "sa=" MACSTR " go_dev_addr=" MACSTR |
| 3027 | " unknown-network", |
| 3028 | MAC2STR(sa), MAC2STR(go_dev_addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3029 | } |
| 3030 | return; |
| 3031 | } |
| 3032 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3033 | if (s->mode == WPAS_MODE_P2P_GO && op_freq) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3034 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3035 | "sa=" MACSTR " persistent=%d freq=%d", |
| 3036 | MAC2STR(sa), s->id, op_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3037 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3038 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED |
| 3039 | "sa=" MACSTR " persistent=%d", |
| 3040 | MAC2STR(sa), s->id); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3041 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3045 | static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, |
| 3046 | struct wpa_ssid *ssid, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3047 | const u8 *peer, int inv) |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3048 | { |
| 3049 | size_t i; |
| 3050 | |
| 3051 | if (ssid == NULL) |
| 3052 | return; |
| 3053 | |
| 3054 | for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { |
| 3055 | if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer, |
| 3056 | ETH_ALEN) == 0) |
| 3057 | break; |
| 3058 | } |
| 3059 | if (i >= ssid->num_p2p_clients) { |
| 3060 | if (ssid->mode != WPAS_MODE_P2P_GO && |
| 3061 | os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { |
| 3062 | wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " |
| 3063 | "due to invitation result", ssid->id); |
| 3064 | wpas_notify_network_removed(wpa_s, ssid); |
| 3065 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 3066 | return; |
| 3067 | } |
| 3068 | return; /* Peer not found in client list */ |
| 3069 | } |
| 3070 | |
| 3071 | wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3072 | "group %d client list%s", |
| 3073 | MAC2STR(peer), ssid->id, |
| 3074 | inv ? " due to invitation result" : ""); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3075 | os_memmove(ssid->p2p_client_list + i * ETH_ALEN, |
| 3076 | ssid->p2p_client_list + (i + 1) * ETH_ALEN, |
| 3077 | (ssid->num_p2p_clients - i - 1) * ETH_ALEN); |
| 3078 | ssid->num_p2p_clients--; |
| 3079 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 3080 | if (wpa_s->parent->conf->update_config && |
| 3081 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 3082 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 3083 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 3084 | } |
| 3085 | |
| 3086 | |
| 3087 | static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, |
| 3088 | const u8 *peer) |
| 3089 | { |
| 3090 | struct wpa_ssid *ssid; |
| 3091 | |
| 3092 | wpa_s = wpa_s->global->p2p_invite_group; |
| 3093 | if (wpa_s == NULL) |
| 3094 | return; /* No known invitation group */ |
| 3095 | ssid = wpa_s->current_ssid; |
| 3096 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || |
| 3097 | !ssid->p2p_persistent_group) |
| 3098 | return; /* Not operating as a GO in persistent group */ |
| 3099 | ssid = wpas_p2p_get_persistent(wpa_s->parent, peer, |
| 3100 | ssid->ssid, ssid->ssid_len); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3101 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3105 | static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3106 | const struct p2p_channels *channels, |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3107 | const u8 *peer, int neg_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3108 | { |
| 3109 | struct wpa_supplicant *wpa_s = ctx; |
| 3110 | struct wpa_ssid *ssid; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3111 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3112 | |
| 3113 | if (bssid) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3114 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3115 | "status=%d " MACSTR, |
| 3116 | status, MAC2STR(bssid)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3117 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3118 | wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT |
| 3119 | "status=%d ", status); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3120 | } |
| 3121 | wpas_notify_p2p_invitation_result(wpa_s, status, bssid); |
| 3122 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3123 | wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, |
| 3124 | status, MAC2STR(peer)); |
| 3125 | if (wpa_s->pending_invite_ssid_id == -1) { |
| 3126 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) |
| 3127 | wpas_remove_persistent_client(wpa_s, peer); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3128 | return; /* Invitation to active group */ |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3129 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3130 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 3131 | if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { |
| 3132 | wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " |
| 3133 | "invitation exchange to indicate readiness for " |
| 3134 | "re-invocation"); |
| 3135 | } |
| 3136 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3137 | if (status != P2P_SC_SUCCESS) { |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3138 | if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { |
| 3139 | ssid = wpa_config_get_network( |
| 3140 | wpa_s->conf, wpa_s->pending_invite_ssid_id); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3141 | wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 3142 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3143 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 3144 | return; |
| 3145 | } |
| 3146 | |
| 3147 | ssid = wpa_config_get_network(wpa_s->conf, |
| 3148 | wpa_s->pending_invite_ssid_id); |
| 3149 | if (ssid == NULL) { |
| 3150 | wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " |
| 3151 | "data matching with invitation"); |
| 3152 | return; |
| 3153 | } |
| 3154 | |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3155 | /* |
| 3156 | * The peer could have missed our ctrl::ack frame for Invitation |
| 3157 | * Response and continue retransmitting the frame. To reduce the |
| 3158 | * likelihood of the peer not getting successful TX status for the |
| 3159 | * Invitation Response frame, wait a short time here before starting |
| 3160 | * the persistent group so that we will remain on the current channel to |
| 3161 | * acknowledge any possible retransmission from the peer. |
| 3162 | */ |
Irfan Sheriff | 81931b8 | 2012-10-16 21:40:46 -0700 | [diff] [blame] | 3163 | #ifndef ANDROID_P2P |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3164 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " |
| 3165 | "starting persistent group"); |
| 3166 | os_sleep(0, 50000); |
Irfan Sheriff | 81931b8 | 2012-10-16 21:40:46 -0700 | [diff] [blame] | 3167 | #else |
| 3168 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before " |
| 3169 | "starting persistent group"); |
| 3170 | os_sleep(0, 100000); |
| 3171 | #endif |
Jouni Malinen | 2c5b17d | 2012-10-13 21:52:46 -0700 | [diff] [blame] | 3172 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3173 | freq = wpa_s->p2p_persistent_go_freq; |
| 3174 | if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && |
| 3175 | freq_included(channels, neg_freq)) { |
| 3176 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode", |
| 3177 | neg_freq); |
| 3178 | freq = neg_freq; |
| 3179 | } |
| 3180 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3181 | wpas_p2p_group_add_persistent(wpa_s, ssid, |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 3182 | ssid->mode == WPAS_MODE_P2P_GO, |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 3183 | freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3184 | wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, |
| 3185 | channels, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 3186 | ssid->mode == WPAS_MODE_P2P_GO ? |
| 3187 | P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : |
| 3188 | 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3192 | static int wpas_p2p_disallowed_freq(struct wpa_global *global, |
| 3193 | unsigned int freq) |
| 3194 | { |
Dmitry Shmidt | 4ce9c87 | 2013-10-24 11:08:13 -0700 | [diff] [blame] | 3195 | return freq_range_list_includes(&global->p2p_disallow_freq, freq); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) |
| 3200 | { |
| 3201 | reg->channel[reg->channels] = chan; |
| 3202 | reg->channels++; |
| 3203 | } |
| 3204 | |
| 3205 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3206 | static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3207 | struct p2p_channels *chan, |
| 3208 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3209 | { |
| 3210 | int i, cla = 0; |
| 3211 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3212 | os_memset(cli_chan, 0, sizeof(*cli_chan)); |
| 3213 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3214 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " |
| 3215 | "band"); |
| 3216 | |
| 3217 | /* Operating class 81 - 2.4 GHz band channels 1..13 */ |
| 3218 | chan->reg_class[cla].reg_class = 81; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3219 | chan->reg_class[cla].channels = 0; |
| 3220 | for (i = 0; i < 11; i++) { |
| 3221 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) |
| 3222 | wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); |
| 3223 | } |
| 3224 | if (chan->reg_class[cla].channels) |
| 3225 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3226 | |
| 3227 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " |
| 3228 | "band"); |
| 3229 | |
| 3230 | /* Operating class 115 - 5 GHz, channels 36-48 */ |
| 3231 | chan->reg_class[cla].reg_class = 115; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3232 | chan->reg_class[cla].channels = 0; |
| 3233 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) |
| 3234 | wpas_p2p_add_chan(&chan->reg_class[cla], 36); |
| 3235 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) |
| 3236 | wpas_p2p_add_chan(&chan->reg_class[cla], 40); |
| 3237 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) |
| 3238 | wpas_p2p_add_chan(&chan->reg_class[cla], 44); |
| 3239 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) |
| 3240 | wpas_p2p_add_chan(&chan->reg_class[cla], 48); |
| 3241 | if (chan->reg_class[cla].channels) |
| 3242 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3243 | |
| 3244 | wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " |
| 3245 | "band"); |
| 3246 | |
| 3247 | /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ |
| 3248 | chan->reg_class[cla].reg_class = 124; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3249 | chan->reg_class[cla].channels = 0; |
| 3250 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) |
| 3251 | wpas_p2p_add_chan(&chan->reg_class[cla], 149); |
| 3252 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) |
| 3253 | wpas_p2p_add_chan(&chan->reg_class[cla], 153); |
| 3254 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) |
| 3255 | wpas_p2p_add_chan(&chan->reg_class[cla], 157); |
| 3256 | if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) |
| 3257 | wpas_p2p_add_chan(&chan->reg_class[cla], 161); |
| 3258 | if (chan->reg_class[cla].channels) |
| 3259 | cla++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3260 | |
| 3261 | chan->reg_classes = cla; |
| 3262 | return 0; |
| 3263 | } |
| 3264 | |
| 3265 | |
| 3266 | static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, |
| 3267 | u16 num_modes, |
| 3268 | enum hostapd_hw_mode mode) |
| 3269 | { |
| 3270 | u16 i; |
| 3271 | |
| 3272 | for (i = 0; i < num_modes; i++) { |
| 3273 | if (modes[i].mode == mode) |
| 3274 | return &modes[i]; |
| 3275 | } |
| 3276 | |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
| 3280 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3281 | enum chan_allowed { |
| 3282 | NOT_ALLOWED, PASSIVE_ONLY, ALLOWED |
| 3283 | }; |
| 3284 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3285 | static int has_channel(struct wpa_global *global, |
| 3286 | struct hostapd_hw_modes *mode, u8 chan, int *flags) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3287 | { |
| 3288 | int i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3289 | unsigned int freq; |
| 3290 | |
| 3291 | freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + |
| 3292 | chan * 5; |
| 3293 | if (wpas_p2p_disallowed_freq(global, freq)) |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3294 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3295 | |
| 3296 | for (i = 0; i < mode->num_channels; i++) { |
| 3297 | if (mode->channels[i].chan == chan) { |
| 3298 | if (flags) |
| 3299 | *flags = mode->channels[i].flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3300 | if (mode->channels[i].flag & |
| 3301 | (HOSTAPD_CHAN_DISABLED | |
| 3302 | HOSTAPD_CHAN_RADAR)) |
| 3303 | return NOT_ALLOWED; |
| 3304 | if (mode->channels[i].flag & |
| 3305 | (HOSTAPD_CHAN_PASSIVE_SCAN | |
| 3306 | HOSTAPD_CHAN_NO_IBSS)) |
| 3307 | return PASSIVE_ONLY; |
| 3308 | return ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3312 | return NOT_ALLOWED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | |
| 3316 | struct p2p_oper_class_map { |
| 3317 | enum hostapd_hw_mode mode; |
| 3318 | u8 op_class; |
| 3319 | u8 min_chan; |
| 3320 | u8 max_chan; |
| 3321 | u8 inc; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3322 | enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3323 | }; |
| 3324 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3325 | static struct p2p_oper_class_map op_class[] = { |
| 3326 | { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3327 | #if 0 /* Do not enable HT40 on 2 GHz for now */ |
| 3328 | { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, |
| 3329 | { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, |
| 3330 | #endif |
| 3331 | { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, |
| 3332 | { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, |
| 3333 | { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, |
| 3334 | { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, |
| 3335 | { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, |
| 3336 | { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3337 | |
| 3338 | /* |
| 3339 | * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center |
| 3340 | * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of |
| 3341 | * 80 MHz, but currently use the following definition for simplicity |
| 3342 | * (these center frequencies are not actual channels, which makes |
| 3343 | * has_channel() fail). wpas_p2p_verify_80mhz() should take care of |
| 3344 | * removing invalid channels. |
| 3345 | */ |
| 3346 | { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 }, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3347 | { -1, 0, 0, 0, 0, BW20 } |
| 3348 | }; |
| 3349 | |
| 3350 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3351 | static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, |
| 3352 | struct hostapd_hw_modes *mode, |
| 3353 | u8 channel) |
| 3354 | { |
| 3355 | u8 center_channels[] = { 42, 58, 106, 122, 138, 155 }; |
| 3356 | unsigned int i; |
| 3357 | |
| 3358 | if (mode->mode != HOSTAPD_MODE_IEEE80211A) |
| 3359 | return 0; |
| 3360 | |
| 3361 | for (i = 0; i < ARRAY_SIZE(center_channels); i++) |
| 3362 | /* |
| 3363 | * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), |
| 3364 | * so the center channel is 6 channels away from the start/end. |
| 3365 | */ |
| 3366 | if (channel >= center_channels[i] - 6 && |
| 3367 | channel <= center_channels[i] + 6) |
| 3368 | return center_channels[i]; |
| 3369 | |
| 3370 | return 0; |
| 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, |
| 3375 | struct hostapd_hw_modes *mode, |
| 3376 | u8 channel, u8 bw) |
| 3377 | { |
| 3378 | u8 center_chan; |
| 3379 | int i, flags; |
| 3380 | enum chan_allowed res, ret = ALLOWED; |
| 3381 | |
| 3382 | center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3383 | if (!center_chan) |
| 3384 | return NOT_ALLOWED; |
| 3385 | if (center_chan >= 58 && center_chan <= 138) |
| 3386 | return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ |
| 3387 | |
| 3388 | /* check all the channels are available */ |
| 3389 | for (i = 0; i < 4; i++) { |
| 3390 | int adj_chan = center_chan - 6 + i * 4; |
| 3391 | |
| 3392 | res = has_channel(wpa_s->global, mode, adj_chan, &flags); |
| 3393 | if (res == NOT_ALLOWED) |
| 3394 | return NOT_ALLOWED; |
| 3395 | if (res == PASSIVE_ONLY) |
| 3396 | ret = PASSIVE_ONLY; |
| 3397 | |
| 3398 | if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) |
| 3399 | return NOT_ALLOWED; |
| 3400 | if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) |
| 3401 | return NOT_ALLOWED; |
| 3402 | if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) |
| 3403 | return NOT_ALLOWED; |
| 3404 | if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) |
| 3405 | return NOT_ALLOWED; |
| 3406 | } |
| 3407 | |
| 3408 | return ret; |
| 3409 | } |
| 3410 | |
| 3411 | |
| 3412 | static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, |
| 3413 | struct hostapd_hw_modes *mode, |
| 3414 | u8 channel, u8 bw) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3415 | { |
| 3416 | int flag; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3417 | enum chan_allowed res, res2; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3418 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3419 | res2 = res = has_channel(wpa_s->global, mode, channel, &flag); |
| 3420 | if (bw == BW40MINUS) { |
| 3421 | if (!(flag & HOSTAPD_CHAN_HT40MINUS)) |
| 3422 | return NOT_ALLOWED; |
| 3423 | res2 = has_channel(wpa_s->global, mode, channel - 4, NULL); |
| 3424 | } else if (bw == BW40PLUS) { |
| 3425 | if (!(flag & HOSTAPD_CHAN_HT40PLUS)) |
| 3426 | return NOT_ALLOWED; |
| 3427 | res2 = has_channel(wpa_s->global, mode, channel + 4, NULL); |
| 3428 | } else if (bw == BW80) { |
| 3429 | res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw); |
| 3430 | } |
| 3431 | |
| 3432 | if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) |
| 3433 | return NOT_ALLOWED; |
| 3434 | if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY) |
| 3435 | return PASSIVE_ONLY; |
| 3436 | return res; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3440 | static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3441 | struct p2p_channels *chan, |
| 3442 | struct p2p_channels *cli_chan) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3443 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3444 | struct hostapd_hw_modes *mode; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3445 | int cla, op, cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3446 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3447 | if (wpa_s->hw.modes == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3448 | wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " |
| 3449 | "of all supported channels; assume dualband " |
| 3450 | "support"); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3451 | return wpas_p2p_default_channels(wpa_s, chan, cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3454 | cla = cli_cla = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3455 | |
| 3456 | for (op = 0; op_class[op].op_class; op++) { |
| 3457 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3458 | u8 ch; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3459 | struct p2p_reg_class *reg = NULL, *cli_reg = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3460 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3461 | 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] | 3462 | if (mode == NULL) |
| 3463 | continue; |
| 3464 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3465 | enum chan_allowed res; |
| 3466 | res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
| 3467 | if (res == ALLOWED) { |
| 3468 | if (reg == NULL) { |
| 3469 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", |
| 3470 | o->op_class); |
| 3471 | reg = &chan->reg_class[cla]; |
| 3472 | cla++; |
| 3473 | reg->reg_class = o->op_class; |
| 3474 | } |
| 3475 | reg->channel[reg->channels] = ch; |
| 3476 | reg->channels++; |
| 3477 | } else if (res == PASSIVE_ONLY && |
| 3478 | wpa_s->conf->p2p_add_cli_chan) { |
| 3479 | if (cli_reg == NULL) { |
| 3480 | wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", |
| 3481 | o->op_class); |
| 3482 | cli_reg = &cli_chan->reg_class[cli_cla]; |
| 3483 | cli_cla++; |
| 3484 | cli_reg->reg_class = o->op_class; |
| 3485 | } |
| 3486 | cli_reg->channel[cli_reg->channels] = ch; |
| 3487 | cli_reg->channels++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3488 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3489 | } |
| 3490 | if (reg) { |
| 3491 | wpa_hexdump(MSG_DEBUG, "P2P: Channels", |
| 3492 | reg->channel, reg->channels); |
| 3493 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3494 | if (cli_reg) { |
| 3495 | wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", |
| 3496 | cli_reg->channel, cli_reg->channels); |
| 3497 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | chan->reg_classes = cla; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3501 | cli_chan->reg_classes = cli_cla; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3502 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3503 | return 0; |
| 3504 | } |
| 3505 | |
| 3506 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3507 | int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, |
| 3508 | struct hostapd_hw_modes *mode, u8 channel) |
| 3509 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3510 | int op; |
| 3511 | enum chan_allowed ret; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3512 | |
| 3513 | for (op = 0; op_class[op].op_class; op++) { |
| 3514 | struct p2p_oper_class_map *o = &op_class[op]; |
| 3515 | u8 ch; |
| 3516 | |
| 3517 | for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { |
| 3518 | if (o->mode != HOSTAPD_MODE_IEEE80211A || |
| 3519 | o->bw == BW20 || ch != channel) |
| 3520 | continue; |
| 3521 | ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3522 | if (ret == ALLOWED) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3523 | return (o->bw == BW40MINUS) ? -1 : 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3524 | } |
| 3525 | } |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
| 3529 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3530 | int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, |
| 3531 | struct hostapd_hw_modes *mode, u8 channel) |
| 3532 | { |
| 3533 | if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80)) |
| 3534 | return 0; |
| 3535 | |
| 3536 | return wpas_p2p_get_center_80mhz(wpa_s, mode, channel); |
| 3537 | } |
| 3538 | |
| 3539 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3540 | static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, |
| 3541 | size_t buf_len) |
| 3542 | { |
| 3543 | struct wpa_supplicant *wpa_s = ctx; |
| 3544 | |
| 3545 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3546 | if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) |
| 3547 | break; |
| 3548 | } |
| 3549 | if (wpa_s == NULL) |
| 3550 | return -1; |
| 3551 | |
| 3552 | return wpa_drv_get_noa(wpa_s, buf, buf_len); |
| 3553 | } |
| 3554 | |
| 3555 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3556 | static int wpas_go_connected(void *ctx, const u8 *dev_addr) |
| 3557 | { |
| 3558 | struct wpa_supplicant *wpa_s = ctx; |
| 3559 | |
| 3560 | for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3561 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 3562 | if (ssid == NULL) |
| 3563 | continue; |
| 3564 | if (ssid->mode != WPAS_MODE_INFRA) |
| 3565 | continue; |
| 3566 | if (wpa_s->wpa_state != WPA_COMPLETED && |
| 3567 | wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) |
| 3568 | continue; |
| 3569 | if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0) |
| 3570 | return 1; |
| 3571 | } |
| 3572 | |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
| 3576 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3577 | static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) |
| 3578 | { |
| 3579 | struct wpa_supplicant *wpa_s = ctx; |
| 3580 | wpa_msg_global(wpa_s, level, "P2P: %s", msg); |
| 3581 | } |
| 3582 | |
| 3583 | |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3584 | int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s) |
| 3585 | { |
| 3586 | struct wpa_interface iface; |
| 3587 | struct wpa_supplicant *p2pdev_wpa_s; |
| 3588 | char ifname[100]; |
| 3589 | char force_name[100]; |
| 3590 | int ret; |
| 3591 | |
| 3592 | os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", |
| 3593 | wpa_s->ifname); |
| 3594 | force_name[0] = '\0'; |
| 3595 | wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; |
| 3596 | ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL, |
| 3597 | force_name, wpa_s->pending_interface_addr, NULL); |
| 3598 | if (ret < 0) { |
| 3599 | wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); |
| 3600 | return ret; |
| 3601 | } |
| 3602 | os_strlcpy(wpa_s->pending_interface_name, ifname, |
| 3603 | sizeof(wpa_s->pending_interface_name)); |
| 3604 | |
| 3605 | os_memset(&iface, 0, sizeof(iface)); |
| 3606 | iface.p2p_mgmt = 1; |
| 3607 | iface.ifname = wpa_s->pending_interface_name; |
| 3608 | iface.driver = wpa_s->driver->name; |
| 3609 | iface.driver_param = wpa_s->conf->driver_param; |
| 3610 | iface.confname = wpa_s->confname; |
| 3611 | p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); |
| 3612 | if (!p2pdev_wpa_s) { |
| 3613 | wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); |
| 3614 | return -1; |
| 3615 | } |
| 3616 | p2pdev_wpa_s->parent = wpa_s; |
| 3617 | |
| 3618 | wpa_s->pending_interface_name[0] = '\0'; |
| 3619 | return 0; |
| 3620 | } |
| 3621 | |
| 3622 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 3623 | static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, |
| 3624 | const u8 *noa, size_t noa_len) |
| 3625 | { |
| 3626 | struct wpa_supplicant *wpa_s, *intf = ctx; |
| 3627 | char hex[100]; |
| 3628 | |
| 3629 | for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3630 | if (wpa_s->waiting_presence_resp) |
| 3631 | break; |
| 3632 | } |
| 3633 | if (!wpa_s) { |
| 3634 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); |
| 3635 | return; |
| 3636 | } |
| 3637 | wpa_s->waiting_presence_resp = 0; |
| 3638 | |
| 3639 | wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); |
| 3640 | wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR |
| 3641 | " status=%u noa=%s", MAC2STR(src), status, hex); |
| 3642 | } |
| 3643 | |
| 3644 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3645 | /** |
| 3646 | * wpas_p2p_init - Initialize P2P module for %wpa_supplicant |
| 3647 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3648 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3649 | * Returns: 0 on success, -1 on failure |
| 3650 | */ |
| 3651 | int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) |
| 3652 | { |
| 3653 | struct p2p_config p2p; |
| 3654 | unsigned int r; |
| 3655 | int i; |
| 3656 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3657 | if (wpa_s->conf->p2p_disabled) |
| 3658 | return 0; |
| 3659 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3660 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 3661 | return 0; |
| 3662 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3663 | if (global->p2p) |
| 3664 | return 0; |
| 3665 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3666 | os_memset(&p2p, 0, sizeof(p2p)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3667 | p2p.cb_ctx = wpa_s; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3668 | p2p.debug_print = wpas_p2p_debug_print; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3669 | p2p.p2p_scan = wpas_p2p_scan; |
| 3670 | p2p.send_action = wpas_send_action; |
| 3671 | p2p.send_action_done = wpas_send_action_done; |
| 3672 | p2p.go_neg_completed = wpas_go_neg_completed; |
| 3673 | p2p.go_neg_req_rx = wpas_go_neg_req_rx; |
| 3674 | p2p.dev_found = wpas_dev_found; |
| 3675 | p2p.dev_lost = wpas_dev_lost; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3676 | p2p.find_stopped = wpas_find_stopped; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3677 | p2p.start_listen = wpas_start_listen; |
| 3678 | p2p.stop_listen = wpas_stop_listen; |
| 3679 | p2p.send_probe_resp = wpas_send_probe_resp; |
| 3680 | p2p.sd_request = wpas_sd_request; |
| 3681 | p2p.sd_response = wpas_sd_response; |
| 3682 | p2p.prov_disc_req = wpas_prov_disc_req; |
| 3683 | p2p.prov_disc_resp = wpas_prov_disc_resp; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 3684 | p2p.prov_disc_fail = wpas_prov_disc_fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3685 | p2p.invitation_process = wpas_invitation_process; |
| 3686 | p2p.invitation_received = wpas_invitation_received; |
| 3687 | p2p.invitation_result = wpas_invitation_result; |
| 3688 | p2p.get_noa = wpas_get_noa; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3689 | p2p.go_connected = wpas_go_connected; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 3690 | p2p.presence_resp = wpas_presence_resp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3691 | |
| 3692 | 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] | 3693 | 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] | 3694 | p2p.dev_name = wpa_s->conf->device_name; |
| 3695 | p2p.manufacturer = wpa_s->conf->manufacturer; |
| 3696 | p2p.model_name = wpa_s->conf->model_name; |
| 3697 | p2p.model_number = wpa_s->conf->model_number; |
| 3698 | p2p.serial_number = wpa_s->conf->serial_number; |
| 3699 | if (wpa_s->wps) { |
| 3700 | os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); |
| 3701 | p2p.config_methods = wpa_s->wps->config_methods; |
| 3702 | } |
| 3703 | |
| 3704 | if (wpa_s->conf->p2p_listen_reg_class && |
| 3705 | wpa_s->conf->p2p_listen_channel) { |
| 3706 | p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 3707 | p2p.channel = wpa_s->conf->p2p_listen_channel; |
| 3708 | } else { |
| 3709 | p2p.reg_class = 81; |
| 3710 | /* |
| 3711 | * Pick one of the social channels randomly as the listen |
| 3712 | * channel. |
| 3713 | */ |
| 3714 | os_get_random((u8 *) &r, sizeof(r)); |
| 3715 | p2p.channel = 1 + (r % 3) * 5; |
| 3716 | } |
| 3717 | wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); |
| 3718 | |
| 3719 | if (wpa_s->conf->p2p_oper_reg_class && |
| 3720 | wpa_s->conf->p2p_oper_channel) { |
| 3721 | p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 3722 | p2p.op_channel = wpa_s->conf->p2p_oper_channel; |
| 3723 | p2p.cfg_op_channel = 1; |
| 3724 | wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " |
| 3725 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3726 | |
| 3727 | } else { |
| 3728 | p2p.op_reg_class = 81; |
| 3729 | /* |
| 3730 | * Use random operation channel from (1, 6, 11) if no other |
| 3731 | * preference is indicated. |
| 3732 | */ |
| 3733 | os_get_random((u8 *) &r, sizeof(r)); |
| 3734 | p2p.op_channel = 1 + (r % 3) * 5; |
| 3735 | p2p.cfg_op_channel = 0; |
| 3736 | wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " |
| 3737 | "%d:%d", p2p.op_reg_class, p2p.op_channel); |
| 3738 | } |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 3739 | |
| 3740 | if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { |
| 3741 | p2p.pref_chan = wpa_s->conf->p2p_pref_chan; |
| 3742 | p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; |
| 3743 | } |
| 3744 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3745 | if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 3746 | os_memcpy(p2p.country, wpa_s->conf->country, 2); |
| 3747 | p2p.country[2] = 0x04; |
| 3748 | } else |
| 3749 | os_memcpy(p2p.country, "XX\x04", 3); |
| 3750 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3751 | if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3752 | wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " |
| 3753 | "channel list"); |
| 3754 | return -1; |
| 3755 | } |
| 3756 | |
| 3757 | os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, |
| 3758 | WPS_DEV_TYPE_LEN); |
| 3759 | |
| 3760 | p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; |
| 3761 | os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, |
| 3762 | p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); |
| 3763 | |
| 3764 | p2p.concurrent_operations = !!(wpa_s->drv_flags & |
| 3765 | WPA_DRIVER_FLAGS_P2P_CONCURRENT); |
| 3766 | |
| 3767 | p2p.max_peers = 100; |
| 3768 | |
| 3769 | if (wpa_s->conf->p2p_ssid_postfix) { |
| 3770 | p2p.ssid_postfix_len = |
| 3771 | os_strlen(wpa_s->conf->p2p_ssid_postfix); |
| 3772 | if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) |
| 3773 | p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); |
| 3774 | os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, |
| 3775 | p2p.ssid_postfix_len); |
| 3776 | } |
| 3777 | |
| 3778 | p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; |
| 3779 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3780 | p2p.max_listen = wpa_s->max_remain_on_chan; |
| 3781 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3782 | global->p2p = p2p_init(&p2p); |
| 3783 | if (global->p2p == NULL) |
| 3784 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3785 | global->p2p_init_wpa_s = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3786 | |
| 3787 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 3788 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 3789 | continue; |
| 3790 | p2p_add_wps_vendor_extension( |
| 3791 | global->p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 3792 | } |
| 3793 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 3794 | p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); |
| 3795 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3796 | return 0; |
| 3797 | } |
| 3798 | |
| 3799 | |
| 3800 | /** |
| 3801 | * wpas_p2p_deinit - Deinitialize per-interface P2P data |
| 3802 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 3803 | * |
| 3804 | * This function deinitialize per-interface P2P data. |
| 3805 | */ |
| 3806 | void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) |
| 3807 | { |
| 3808 | if (wpa_s->driver && wpa_s->drv_priv) |
| 3809 | wpa_drv_probe_req_report(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3810 | |
| 3811 | if (wpa_s->go_params) { |
| 3812 | /* Clear any stored provisioning info */ |
| 3813 | p2p_clear_provisioning_info( |
| 3814 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3815 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3816 | } |
| 3817 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3818 | os_free(wpa_s->go_params); |
| 3819 | wpa_s->go_params = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3820 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 3821 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 3822 | wpa_s->p2p_long_listen = 0; |
| 3823 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 3824 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 3825 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 3826 | eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 3827 | wpas_p2p_listen_work_done(wpa_s); |
| 3828 | if (wpa_s->p2p_send_action_work) { |
| 3829 | os_free(wpa_s->p2p_send_action_work->ctx); |
| 3830 | radio_work_done(wpa_s->p2p_send_action_work); |
| 3831 | wpa_s->p2p_send_action_work = NULL; |
| 3832 | } |
| 3833 | eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3834 | |
| 3835 | /* TODO: remove group interface from the driver if this wpa_s instance |
| 3836 | * is on top of a P2P group interface */ |
| 3837 | } |
| 3838 | |
| 3839 | |
| 3840 | /** |
| 3841 | * wpas_p2p_deinit_global - Deinitialize global P2P module |
| 3842 | * @global: Pointer to global data from wpa_supplicant_init() |
| 3843 | * |
| 3844 | * This function deinitializes the global (per device) P2P module. |
| 3845 | */ |
| 3846 | void wpas_p2p_deinit_global(struct wpa_global *global) |
| 3847 | { |
| 3848 | struct wpa_supplicant *wpa_s, *tmp; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3849 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3850 | wpa_s = global->ifaces; |
| 3851 | if (wpa_s) |
| 3852 | wpas_p2p_service_flush(wpa_s); |
| 3853 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3854 | if (global->p2p == NULL) |
| 3855 | return; |
| 3856 | |
| 3857 | /* Remove remaining P2P group interfaces */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3858 | while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) |
| 3859 | wpa_s = wpa_s->next; |
| 3860 | while (wpa_s) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3861 | tmp = global->ifaces; |
| 3862 | while (tmp && |
| 3863 | (tmp == wpa_s || |
| 3864 | tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { |
| 3865 | tmp = tmp->next; |
| 3866 | } |
| 3867 | if (tmp == NULL) |
| 3868 | break; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3869 | /* Disconnect from the P2P group and deinit the interface */ |
| 3870 | wpas_p2p_disconnect(tmp); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | /* |
| 3874 | * Deinit GO data on any possibly remaining interface (if main |
| 3875 | * interface is used as GO). |
| 3876 | */ |
| 3877 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 3878 | if (wpa_s->ap_iface) |
| 3879 | wpas_p2p_group_deinit(wpa_s); |
| 3880 | } |
| 3881 | |
| 3882 | p2p_deinit(global->p2p); |
| 3883 | global->p2p = NULL; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3884 | global->p2p_init_wpa_s = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | |
| 3888 | static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) |
| 3889 | { |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 3890 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) && |
| 3891 | wpa_s->conf->p2p_no_group_iface) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3892 | return 0; /* separate interface disabled per configuration */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3893 | if (wpa_s->drv_flags & |
| 3894 | (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | |
| 3895 | WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) |
| 3896 | return 1; /* P2P group requires a new interface in every case |
| 3897 | */ |
| 3898 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) |
| 3899 | return 0; /* driver does not support concurrent operations */ |
| 3900 | if (wpa_s->global->ifaces->next) |
| 3901 | return 1; /* more that one interface already in use */ |
| 3902 | if (wpa_s->wpa_state >= WPA_AUTHENTICATING) |
| 3903 | return 1; /* this interface is already in use */ |
| 3904 | return 0; |
| 3905 | } |
| 3906 | |
| 3907 | |
| 3908 | static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, |
| 3909 | const u8 *peer_addr, |
| 3910 | enum p2p_wps_method wps_method, |
| 3911 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3912 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3913 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3914 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3915 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 3916 | persistent_group = 2; |
| 3917 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3918 | /* |
| 3919 | * Increase GO config timeout if HT40 is used since it takes some time |
| 3920 | * to scan channels for coex purposes before the BSS can be started. |
| 3921 | */ |
| 3922 | p2p_set_config_timeout(wpa_s->global->p2p, |
| 3923 | wpa_s->p2p_go_ht40 ? 255 : 100, 20); |
| 3924 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3925 | return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, |
| 3926 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3927 | persistent_group, ssid ? ssid->ssid : NULL, |
| 3928 | ssid ? ssid->ssid_len : 0, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3929 | wpa_s->p2p_pd_before_go_neg, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | |
| 3933 | static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, |
| 3934 | const u8 *peer_addr, |
| 3935 | enum p2p_wps_method wps_method, |
| 3936 | int go_intent, const u8 *own_interface_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3937 | unsigned int force_freq, int persistent_group, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3938 | struct wpa_ssid *ssid, unsigned int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3939 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3940 | if (persistent_group && wpa_s->conf->persistent_reconnect) |
| 3941 | persistent_group = 2; |
| 3942 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3943 | return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, |
| 3944 | go_intent, own_interface_addr, force_freq, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3945 | persistent_group, ssid ? ssid->ssid : NULL, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3946 | ssid ? ssid->ssid_len : 0, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | |
| 3950 | static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) |
| 3951 | { |
| 3952 | wpa_s->p2p_join_scan_count++; |
| 3953 | wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", |
| 3954 | wpa_s->p2p_join_scan_count); |
| 3955 | if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { |
| 3956 | wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR |
| 3957 | " for join operationg - stop join attempt", |
| 3958 | MAC2STR(wpa_s->pending_join_iface_addr)); |
| 3959 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3960 | if (wpa_s->p2p_auto_pd) { |
| 3961 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3962 | wpa_msg_global(wpa_s, MSG_INFO, |
| 3963 | P2P_EVENT_PROV_DISC_FAILURE |
| 3964 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 3965 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3966 | return; |
| 3967 | } |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 3968 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 3969 | P2P_EVENT_GROUP_FORMATION_FAILURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3974 | static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) |
| 3975 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3976 | int *freqs, res, num, i; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3977 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3978 | if (wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 3979 | /* Multiple channels are supported and not all are in use */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3980 | return 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3981 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3982 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3983 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 3984 | if (!freqs) |
| 3985 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 3986 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 3987 | num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 3988 | wpa_s->num_multichan_concurrent); |
| 3989 | if (num < 0) { |
| 3990 | res = 1; |
| 3991 | goto exit_free; |
| 3992 | } |
| 3993 | |
| 3994 | for (i = 0; i < num; i++) { |
| 3995 | if (freqs[i] == freq) { |
| 3996 | wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", |
| 3997 | freq); |
| 3998 | res = 0; |
| 3999 | goto exit_free; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4000 | } |
| 4001 | } |
| 4002 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4003 | res = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4004 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4005 | exit_free: |
| 4006 | os_free(freqs); |
| 4007 | return res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | |
| 4011 | static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, |
| 4012 | const u8 *peer_dev_addr) |
| 4013 | { |
| 4014 | struct wpa_bss *bss; |
| 4015 | int updated; |
| 4016 | |
| 4017 | bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); |
| 4018 | if (bss == NULL) |
| 4019 | return -1; |
| 4020 | if (bss->last_update_idx < wpa_s->bss_update_idx) { |
| 4021 | wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " |
| 4022 | "last scan"); |
| 4023 | return 0; |
| 4024 | } |
| 4025 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4026 | updated = os_reltime_before(&wpa_s->p2p_auto_started, |
| 4027 | &bss->last_update); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4028 | wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " |
| 4029 | "%ld.%06ld (%supdated in last scan)", |
| 4030 | bss->last_update.sec, bss->last_update.usec, |
| 4031 | updated ? "": "not "); |
| 4032 | |
| 4033 | return updated; |
| 4034 | } |
| 4035 | |
| 4036 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4037 | static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, |
| 4038 | struct wpa_scan_results *scan_res) |
| 4039 | { |
| 4040 | struct wpa_bss *bss; |
| 4041 | int freq; |
| 4042 | u8 iface_addr[ETH_ALEN]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4043 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4044 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4045 | |
| 4046 | if (wpa_s->global->p2p_disabled) |
| 4047 | return; |
| 4048 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4049 | wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", |
| 4050 | scan_res ? (int) scan_res->num : -1, |
| 4051 | wpa_s->p2p_auto_join ? "auto_" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4052 | |
| 4053 | if (scan_res) |
| 4054 | wpas_p2p_scan_res_handler(wpa_s, scan_res); |
| 4055 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4056 | if (wpa_s->p2p_auto_pd) { |
| 4057 | int join = wpas_p2p_peer_go(wpa_s, |
| 4058 | wpa_s->pending_join_dev_addr); |
| 4059 | if (join == 0 && |
| 4060 | wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { |
| 4061 | wpa_s->auto_pd_scan_retry++; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4062 | bss = wpa_bss_get_bssid_latest( |
| 4063 | wpa_s, wpa_s->pending_join_dev_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4064 | if (bss) { |
| 4065 | freq = bss->freq; |
| 4066 | wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " |
| 4067 | "the peer " MACSTR " at %d MHz", |
| 4068 | wpa_s->auto_pd_scan_retry, |
| 4069 | MAC2STR(wpa_s-> |
| 4070 | pending_join_dev_addr), |
| 4071 | freq); |
| 4072 | wpas_p2p_join_scan_req(wpa_s, freq); |
| 4073 | return; |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | if (join < 0) |
| 4078 | join = 0; |
| 4079 | |
| 4080 | wpa_s->p2p_auto_pd = 0; |
| 4081 | wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; |
| 4082 | wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", |
| 4083 | MAC2STR(wpa_s->pending_join_dev_addr), join); |
| 4084 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
| 4085 | wpa_s->pending_join_dev_addr, |
| 4086 | wpa_s->pending_pd_config_methods, join, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4087 | 0, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4088 | wpa_s->p2p_auto_pd = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4089 | wpa_msg_global(wpa_s, MSG_INFO, |
| 4090 | P2P_EVENT_PROV_DISC_FAILURE |
| 4091 | " p2p_dev_addr=" MACSTR " status=N/A", |
| 4092 | MAC2STR(wpa_s->pending_join_dev_addr)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4093 | } |
| 4094 | return; |
| 4095 | } |
| 4096 | |
| 4097 | if (wpa_s->p2p_auto_join) { |
| 4098 | int join = wpas_p2p_peer_go(wpa_s, |
| 4099 | wpa_s->pending_join_dev_addr); |
| 4100 | if (join < 0) { |
| 4101 | wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " |
| 4102 | "running a GO -> use GO Negotiation"); |
| 4103 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, |
| 4104 | wpa_s->p2p_pin, wpa_s->p2p_wps_method, |
| 4105 | wpa_s->p2p_persistent_group, 0, 0, 0, |
| 4106 | wpa_s->p2p_go_intent, |
| 4107 | wpa_s->p2p_connect_freq, |
| 4108 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4109 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4110 | wpa_s->p2p_go_ht40, |
| 4111 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4112 | return; |
| 4113 | } |
| 4114 | |
| 4115 | wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " |
| 4116 | "try to join the group", join ? "" : |
| 4117 | " in older scan"); |
| 4118 | if (!join) |
| 4119 | wpa_s->p2p_fallback_to_go_neg = 1; |
| 4120 | } |
| 4121 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4122 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4123 | wpa_s->pending_join_iface_addr); |
| 4124 | if (freq < 0 && |
| 4125 | p2p_get_interface_addr(wpa_s->global->p2p, |
| 4126 | wpa_s->pending_join_dev_addr, |
| 4127 | iface_addr) == 0 && |
| 4128 | os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) |
| 4129 | { |
| 4130 | wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " |
| 4131 | "address for join from " MACSTR " to " MACSTR |
| 4132 | " based on newly discovered P2P peer entry", |
| 4133 | MAC2STR(wpa_s->pending_join_iface_addr), |
| 4134 | MAC2STR(iface_addr)); |
| 4135 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, |
| 4136 | ETH_ALEN); |
| 4137 | |
| 4138 | freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4139 | wpa_s->pending_join_iface_addr); |
| 4140 | } |
| 4141 | if (freq >= 0) { |
| 4142 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
| 4143 | "from P2P peer table: %d MHz", freq); |
| 4144 | } |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4145 | bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4146 | if (bss) { |
| 4147 | freq = bss->freq; |
| 4148 | wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4149 | "from BSS table: %d MHz (SSID %s)", freq, |
| 4150 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4151 | } |
| 4152 | if (freq > 0) { |
| 4153 | u16 method; |
| 4154 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4155 | if (wpas_check_freq_conflict(wpa_s, freq) > 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4156 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 4157 | P2P_EVENT_GROUP_FORMATION_FAILURE |
| 4158 | "reason=FREQ_CONFLICT"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4159 | return; |
| 4160 | } |
| 4161 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4162 | wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " |
| 4163 | "prior to joining an existing group (GO " MACSTR |
| 4164 | " freq=%u MHz)", |
| 4165 | MAC2STR(wpa_s->pending_join_dev_addr), freq); |
| 4166 | wpa_s->pending_pd_before_join = 1; |
| 4167 | |
| 4168 | switch (wpa_s->pending_join_wps_method) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4169 | case WPS_PIN_DISPLAY: |
| 4170 | method = WPS_CONFIG_KEYPAD; |
| 4171 | break; |
| 4172 | case WPS_PIN_KEYPAD: |
| 4173 | method = WPS_CONFIG_DISPLAY; |
| 4174 | break; |
| 4175 | case WPS_PBC: |
| 4176 | method = WPS_CONFIG_PUSHBUTTON; |
| 4177 | break; |
| 4178 | default: |
| 4179 | method = 0; |
| 4180 | break; |
| 4181 | } |
| 4182 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4183 | if ((p2p_get_provisioning_info(wpa_s->global->p2p, |
| 4184 | wpa_s->pending_join_dev_addr) == |
| 4185 | method)) { |
| 4186 | /* |
| 4187 | * We have already performed provision discovery for |
| 4188 | * joining the group. Proceed directly to join |
| 4189 | * operation without duplicated provision discovery. */ |
| 4190 | wpa_printf(MSG_DEBUG, "P2P: Provision discovery " |
| 4191 | "with " MACSTR " already done - proceed to " |
| 4192 | "join", |
| 4193 | MAC2STR(wpa_s->pending_join_dev_addr)); |
| 4194 | wpa_s->pending_pd_before_join = 0; |
| 4195 | goto start; |
| 4196 | } |
| 4197 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4198 | if (p2p_prov_disc_req(wpa_s->global->p2p, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4199 | wpa_s->pending_join_dev_addr, method, 1, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4200 | freq, wpa_s->user_initiated_pd) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4201 | wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " |
| 4202 | "Discovery Request before joining an " |
| 4203 | "existing group"); |
| 4204 | wpa_s->pending_pd_before_join = 0; |
| 4205 | goto start; |
| 4206 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4207 | return; |
| 4208 | } |
| 4209 | |
| 4210 | wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); |
| 4211 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4212 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4213 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4214 | return; |
| 4215 | |
| 4216 | start: |
| 4217 | /* Start join operation immediately */ |
| 4218 | wpas_p2p_join_start(wpa_s); |
| 4219 | } |
| 4220 | |
| 4221 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4222 | static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4223 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4224 | int ret; |
| 4225 | struct wpa_driver_scan_params params; |
| 4226 | struct wpabuf *wps_ie, *ies; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4227 | size_t ielen; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4228 | int freqs[2] = { 0, 0 }; |
Dmitry Shmidt | ad266fb | 2012-08-24 17:03:35 -0700 | [diff] [blame] | 4229 | #ifdef ANDROID_P2P |
| 4230 | int oper_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4231 | |
Dmitry Shmidt | ad266fb | 2012-08-24 17:03:35 -0700 | [diff] [blame] | 4232 | /* If freq is not provided, check the operating freq of the GO and do a |
| 4233 | * a directed scan to save time |
| 4234 | */ |
| 4235 | if(!freq) { |
| 4236 | freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, |
| 4237 | wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq; |
| 4238 | } |
| 4239 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4240 | os_memset(¶ms, 0, sizeof(params)); |
| 4241 | |
| 4242 | /* P2P Wildcard SSID */ |
| 4243 | params.num_ssids = 1; |
| 4244 | params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; |
| 4245 | params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; |
| 4246 | |
| 4247 | wpa_s->wps->dev.p2p = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4248 | wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, |
| 4249 | wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, |
| 4250 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4251 | if (wps_ie == NULL) { |
| 4252 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4253 | return; |
| 4254 | } |
| 4255 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4256 | ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 4257 | ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4258 | if (ies == NULL) { |
| 4259 | wpabuf_free(wps_ie); |
| 4260 | wpas_p2p_scan_res_join(wpa_s, NULL); |
| 4261 | return; |
| 4262 | } |
| 4263 | wpabuf_put_buf(ies, wps_ie); |
| 4264 | wpabuf_free(wps_ie); |
| 4265 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4266 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4267 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4268 | params.p2p_probe = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4269 | params.extra_ies = wpabuf_head(ies); |
| 4270 | params.extra_ies_len = wpabuf_len(ies); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4271 | if (freq > 0) { |
| 4272 | freqs[0] = freq; |
| 4273 | params.freqs = freqs; |
| 4274 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4275 | |
| 4276 | /* |
| 4277 | * Run a scan to update BSS table and start Provision Discovery once |
| 4278 | * the new scan results become available. |
| 4279 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4280 | ret = wpa_drv_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4281 | if (!ret) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4282 | os_get_reltime(&wpa_s->scan_trigger_time); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4283 | wpa_s->scan_res_handler = wpas_p2p_scan_res_join; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4284 | wpa_s->own_scan_requested = 1; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4285 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4286 | |
| 4287 | wpabuf_free(ies); |
| 4288 | |
| 4289 | if (ret) { |
| 4290 | wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " |
| 4291 | "try again later"); |
| 4292 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
| 4293 | eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); |
| 4294 | wpas_p2p_check_join_scan_limit(wpa_s); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4299 | static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) |
| 4300 | { |
| 4301 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 4302 | wpas_p2p_join_scan_req(wpa_s, 0); |
| 4303 | } |
| 4304 | |
| 4305 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4306 | 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] | 4307 | const u8 *dev_addr, enum p2p_wps_method wps_method, |
| 4308 | int auto_join) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4309 | { |
| 4310 | wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4311 | MACSTR " dev " MACSTR ")%s", |
| 4312 | MAC2STR(iface_addr), MAC2STR(dev_addr), |
| 4313 | auto_join ? " (auto_join)" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4314 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4315 | wpa_s->p2p_auto_pd = 0; |
| 4316 | wpa_s->p2p_auto_join = !!auto_join; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4317 | os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); |
| 4318 | os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); |
| 4319 | wpa_s->pending_join_wps_method = wps_method; |
| 4320 | |
| 4321 | /* Make sure we are not running find during connection establishment */ |
| 4322 | wpas_p2p_stop_find(wpa_s); |
| 4323 | |
| 4324 | wpa_s->p2p_join_scan_count = 0; |
| 4325 | wpas_p2p_join_scan(wpa_s, NULL); |
| 4326 | return 0; |
| 4327 | } |
| 4328 | |
| 4329 | |
| 4330 | static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s) |
| 4331 | { |
| 4332 | struct wpa_supplicant *group; |
| 4333 | struct p2p_go_neg_results res; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4334 | struct wpa_bss *bss; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4335 | |
| 4336 | group = wpas_p2p_get_group_iface(wpa_s, 0, 0); |
| 4337 | if (group == NULL) |
| 4338 | return -1; |
| 4339 | if (group != wpa_s) { |
| 4340 | os_memcpy(group->p2p_pin, wpa_s->p2p_pin, |
| 4341 | sizeof(group->p2p_pin)); |
| 4342 | group->p2p_wps_method = wpa_s->p2p_wps_method; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 4343 | } else { |
| 4344 | /* |
| 4345 | * Need to mark the current interface for p2p_group_formation |
| 4346 | * when a separate group interface is not used. This is needed |
| 4347 | * to allow p2p_cancel stop a pending p2p_connect-join. |
| 4348 | * wpas_p2p_init_group_interface() addresses this for the case |
| 4349 | * where a separate group interface is used. |
| 4350 | */ |
| 4351 | wpa_s->global->p2p_group_formation = wpa_s; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | group->p2p_in_provisioning = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4355 | 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] | 4356 | |
| 4357 | os_memset(&res, 0, sizeof(res)); |
| 4358 | os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, |
| 4359 | ETH_ALEN); |
| 4360 | res.wps_method = wpa_s->pending_join_wps_method; |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 4361 | bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4362 | if (bss) { |
| 4363 | res.freq = bss->freq; |
| 4364 | res.ssid_len = bss->ssid_len; |
| 4365 | os_memcpy(res.ssid, bss->ssid, bss->ssid_len); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 4366 | wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency " |
| 4367 | "from BSS table: %d MHz (SSID %s)", bss->freq, |
| 4368 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 4371 | if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { |
| 4372 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " |
| 4373 | "starting client"); |
| 4374 | wpa_drv_cancel_remain_on_channel(wpa_s); |
| 4375 | wpa_s->off_channel_freq = 0; |
| 4376 | wpa_s->roc_waiting_drv_freq = 0; |
| 4377 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4378 | wpas_start_wps_enrollee(group, &res); |
| 4379 | |
| 4380 | /* |
| 4381 | * Allow a longer timeout for join-a-running-group than normal 15 |
| 4382 | * second group formation timeout since the GO may not have authorized |
| 4383 | * our connection yet. |
| 4384 | */ |
| 4385 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); |
| 4386 | eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, |
| 4387 | wpa_s, NULL); |
| 4388 | |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
| 4392 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4393 | 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] | 4394 | int *force_freq, int *pref_freq, int go) |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4395 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4396 | int *freqs, res; |
| 4397 | unsigned int freq_in_use = 0, num, i; |
| 4398 | |
| 4399 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4400 | if (!freqs) |
| 4401 | return -1; |
| 4402 | |
| 4403 | num = get_shared_radio_freqs(wpa_s, freqs, |
| 4404 | wpa_s->num_multichan_concurrent); |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4405 | wpa_printf(MSG_DEBUG, |
| 4406 | "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u", |
| 4407 | freq, wpa_s->num_multichan_concurrent, num); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4408 | |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4409 | if (freq > 0) { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4410 | int ret; |
| 4411 | if (go) |
| 4412 | ret = p2p_supported_freq(wpa_s->global->p2p, freq); |
| 4413 | else |
| 4414 | ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); |
| 4415 | if (!ret) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4416 | wpa_printf(MSG_DEBUG, "P2P: The forced channel " |
| 4417 | "(%u MHz) is not supported for P2P uses", |
| 4418 | freq); |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4419 | res = -3; |
| 4420 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4421 | } |
| 4422 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4423 | for (i = 0; i < num; i++) { |
| 4424 | if (freqs[i] == freq) |
| 4425 | freq_in_use = 1; |
| 4426 | } |
| 4427 | |
| 4428 | if (num == wpa_s->num_multichan_concurrent && !freq_in_use) { |
| 4429 | wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", |
| 4430 | freq); |
| 4431 | res = -2; |
| 4432 | goto exit_free; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4433 | } |
| 4434 | wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " |
| 4435 | "requested channel (%u MHz)", freq); |
| 4436 | *force_freq = freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4437 | goto exit_ok; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4438 | } |
| 4439 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4440 | for (i = 0; i < num; i++) { |
| 4441 | if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i])) |
| 4442 | continue; |
| 4443 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4444 | if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4445 | wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", |
| 4446 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4447 | *pref_freq = freqs[i]; |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4448 | } else { |
| 4449 | wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use", |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4450 | freqs[i]); |
Dmitry Shmidt | 51a47d5 | 2013-09-10 10:52:57 -0700 | [diff] [blame] | 4451 | *force_freq = freqs[i]; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4452 | } |
| 4453 | break; |
| 4454 | } |
| 4455 | |
| 4456 | if (i == num) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4457 | if (num < wpa_s->num_multichan_concurrent && num > 0) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4458 | wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel"); |
| 4459 | *force_freq = 0; |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4460 | } else if (num < wpa_s->num_multichan_concurrent) { |
| 4461 | wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel"); |
| 4462 | *force_freq = 0; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4463 | } else { |
| 4464 | wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); |
| 4465 | res = -2; |
| 4466 | goto exit_free; |
| 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | exit_ok: |
| 4471 | res = 0; |
| 4472 | exit_free: |
| 4473 | os_free(freqs); |
| 4474 | return res; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4478 | /** |
| 4479 | * wpas_p2p_connect - Request P2P Group Formation to be started |
| 4480 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4481 | * @peer_addr: Address of the peer P2P Device |
| 4482 | * @pin: PIN to use during provisioning or %NULL to indicate PBC mode |
| 4483 | * @persistent_group: Whether to create a persistent group |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4484 | * @auto_join: Whether to select join vs. GO Negotiation automatically |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4485 | * @join: Whether to join an existing group (as a client) instead of starting |
| 4486 | * Group Owner negotiation; @peer_addr is BSSID in that case |
| 4487 | * @auth: Whether to only authorize the connection instead of doing that and |
| 4488 | * initiating Group Owner negotiation |
| 4489 | * @go_intent: GO Intent or -1 to use default |
| 4490 | * @freq: Frequency for the group or 0 for auto-selection |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4491 | * @persistent_id: Persistent group credentials to use for forcing GO |
| 4492 | * parameters or -1 to generate new values (SSID/passphrase) |
| 4493 | * @pd: Whether to send Provision Discovery prior to GO Negotiation as an |
| 4494 | * interoperability workaround when initiating group formation |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4495 | * @ht40: Start GO with 40 MHz channel width |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4496 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4497 | * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified |
| 4498 | * failure, -2 on failure due to channel not currently available, |
| 4499 | * -3 if forced channel is not supported |
| 4500 | */ |
| 4501 | int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 4502 | const char *pin, enum p2p_wps_method wps_method, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4503 | int persistent_group, int auto_join, int join, int auth, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4504 | int go_intent, int freq, int persistent_id, int pd, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4505 | int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4506 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4507 | int force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4508 | int ret = 0, res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4509 | enum wpa_driver_if_type iftype; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4510 | const u8 *if_addr; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4511 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4512 | |
| 4513 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4514 | return -1; |
| 4515 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4516 | if (persistent_id >= 0) { |
| 4517 | ssid = wpa_config_get_network(wpa_s->conf, persistent_id); |
| 4518 | if (ssid == NULL || ssid->disabled != 2 || |
| 4519 | ssid->mode != WPAS_MODE_P2P_GO) |
| 4520 | return -1; |
| 4521 | } |
| 4522 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4523 | os_free(wpa_s->global->add_psk); |
| 4524 | wpa_s->global->add_psk = NULL; |
| 4525 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4526 | wpa_s->global->p2p_fail_on_wps_complete = 0; |
| 4527 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4528 | if (go_intent < 0) |
| 4529 | go_intent = wpa_s->conf->p2p_go_intent; |
| 4530 | |
| 4531 | if (!auth) |
| 4532 | wpa_s->p2p_long_listen = 0; |
| 4533 | |
| 4534 | wpa_s->p2p_wps_method = wps_method; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4535 | wpa_s->p2p_persistent_group = !!persistent_group; |
| 4536 | wpa_s->p2p_persistent_id = persistent_id; |
| 4537 | wpa_s->p2p_go_intent = go_intent; |
| 4538 | wpa_s->p2p_connect_freq = freq; |
| 4539 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 4540 | wpa_s->p2p_pd_before_go_neg = !!pd; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4541 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4542 | wpa_s->p2p_go_vht = !!vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4543 | |
| 4544 | if (pin) |
| 4545 | os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); |
| 4546 | else if (wps_method == WPS_PIN_DISPLAY) { |
| 4547 | ret = wps_generate_pin(); |
| 4548 | os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", |
| 4549 | ret); |
| 4550 | wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", |
| 4551 | wpa_s->p2p_pin); |
| 4552 | } else |
| 4553 | wpa_s->p2p_pin[0] = '\0'; |
| 4554 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4555 | if (join || auto_join) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4556 | u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; |
| 4557 | if (auth) { |
| 4558 | wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " |
| 4559 | "connect a running group from " MACSTR, |
| 4560 | MAC2STR(peer_addr)); |
| 4561 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 4562 | return ret; |
| 4563 | } |
| 4564 | os_memcpy(dev_addr, peer_addr, ETH_ALEN); |
| 4565 | if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, |
| 4566 | iface_addr) < 0) { |
| 4567 | os_memcpy(iface_addr, peer_addr, ETH_ALEN); |
| 4568 | p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, |
| 4569 | dev_addr); |
| 4570 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4571 | if (auto_join) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4572 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4573 | wpa_printf(MSG_DEBUG, "P2P: Auto join started at " |
| 4574 | "%ld.%06ld", |
| 4575 | wpa_s->p2p_auto_started.sec, |
| 4576 | wpa_s->p2p_auto_started.usec); |
| 4577 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4578 | wpa_s->user_initiated_pd = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4579 | if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, |
| 4580 | auto_join) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4581 | return -1; |
| 4582 | return ret; |
| 4583 | } |
| 4584 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4585 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 4586 | go_intent == 15); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4587 | if (res) |
| 4588 | return res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 4589 | wpas_p2p_set_own_freq_preference(wpa_s, |
| 4590 | force_freq ? force_freq : pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4591 | |
| 4592 | wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); |
| 4593 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4594 | if (wpa_s->create_p2p_iface) { |
| 4595 | /* Prepare to add a new interface for the group */ |
| 4596 | iftype = WPA_IF_P2P_GROUP; |
| 4597 | if (go_intent == 15) |
| 4598 | iftype = WPA_IF_P2P_GO; |
| 4599 | if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { |
| 4600 | wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " |
| 4601 | "interface for the group"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4602 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4603 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4604 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4605 | if_addr = wpa_s->pending_interface_addr; |
| 4606 | } else |
| 4607 | if_addr = wpa_s->own_addr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4608 | |
| 4609 | if (auth) { |
| 4610 | if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4611 | go_intent, if_addr, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4612 | force_freq, persistent_group, ssid, |
| 4613 | pref_freq) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4614 | return -1; |
| 4615 | return ret; |
| 4616 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4617 | |
| 4618 | if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, |
| 4619 | go_intent, if_addr, force_freq, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4620 | persistent_group, ssid, pref_freq) < 0) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4621 | if (wpa_s->create_p2p_iface) |
| 4622 | wpas_p2p_remove_pending_group_interface(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4623 | return -1; |
| 4624 | } |
| 4625 | return ret; |
| 4626 | } |
| 4627 | |
| 4628 | |
| 4629 | /** |
| 4630 | * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start |
| 4631 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4632 | * @freq: Frequency of the channel in MHz |
| 4633 | * @duration: Duration of the stay on the channel in milliseconds |
| 4634 | * |
| 4635 | * This callback is called when the driver indicates that it has started the |
| 4636 | * requested remain-on-channel duration. |
| 4637 | */ |
| 4638 | void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4639 | unsigned int freq, unsigned int duration) |
| 4640 | { |
| 4641 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4642 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4643 | if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { |
| 4644 | p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, |
| 4645 | wpa_s->pending_listen_duration); |
| 4646 | wpa_s->pending_listen_freq = 0; |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | |
| 4651 | static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, |
| 4652 | unsigned int timeout) |
| 4653 | { |
| 4654 | /* Limit maximum Listen state time based on driver limitation. */ |
| 4655 | if (timeout > wpa_s->max_remain_on_chan) |
| 4656 | timeout = wpa_s->max_remain_on_chan; |
| 4657 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4658 | return p2p_listen(wpa_s->global->p2p, timeout); |
| 4659 | } |
| 4660 | |
| 4661 | |
| 4662 | /** |
| 4663 | * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout |
| 4664 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4665 | * @freq: Frequency of the channel in MHz |
| 4666 | * |
| 4667 | * This callback is called when the driver indicates that a remain-on-channel |
| 4668 | * operation has been completed, i.e., the duration on the requested channel |
| 4669 | * has timed out. |
| 4670 | */ |
| 4671 | void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, |
| 4672 | unsigned int freq) |
| 4673 | { |
| 4674 | wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " |
| 4675 | "(p2p_long_listen=%d ms pending_action_tx=%p)", |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4676 | wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 4677 | wpas_p2p_listen_work_done(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4678 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4679 | return; |
| 4680 | if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) |
| 4681 | return; /* P2P module started a new operation */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4682 | if (offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4683 | return; |
| 4684 | if (wpa_s->p2p_long_listen > 0) |
| 4685 | wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; |
| 4686 | if (wpa_s->p2p_long_listen > 0) { |
| 4687 | wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); |
| 4688 | wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4689 | } else { |
| 4690 | /* |
| 4691 | * When listen duration is over, stop listen & update p2p_state |
| 4692 | * to IDLE. |
| 4693 | */ |
| 4694 | p2p_stop_listen(wpa_s->global->p2p); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | |
| 4699 | /** |
| 4700 | * wpas_p2p_group_remove - Remove a P2P group |
| 4701 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4702 | * @ifname: Network interface name of the group interface or "*" to remove all |
| 4703 | * groups |
| 4704 | * Returns: 0 on success, -1 on failure |
| 4705 | * |
| 4706 | * This function is used to remove a P2P group. This can be used to disconnect |
| 4707 | * from a group in which the local end is a P2P Client or to end a P2P Group in |
| 4708 | * case the local end is the Group Owner. If a virtual network interface was |
| 4709 | * created for this group, that interface will be removed. Otherwise, only the |
| 4710 | * configured P2P group network will be removed from the interface. |
| 4711 | */ |
| 4712 | int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) |
| 4713 | { |
| 4714 | struct wpa_global *global = wpa_s->global; |
| 4715 | |
| 4716 | if (os_strcmp(ifname, "*") == 0) { |
| 4717 | struct wpa_supplicant *prev; |
| 4718 | wpa_s = global->ifaces; |
| 4719 | while (wpa_s) { |
| 4720 | prev = wpa_s; |
| 4721 | wpa_s = wpa_s->next; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 4722 | if (prev->p2p_group_interface != |
| 4723 | NOT_P2P_GROUP_INTERFACE || |
| 4724 | (prev->current_ssid && |
| 4725 | prev->current_ssid->p2p_group)) |
| 4726 | wpas_p2p_disconnect(prev); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4727 | } |
| 4728 | return 0; |
| 4729 | } |
| 4730 | |
| 4731 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 4732 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 4733 | break; |
| 4734 | } |
| 4735 | |
| 4736 | return wpas_p2p_disconnect(wpa_s); |
| 4737 | } |
| 4738 | |
| 4739 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4740 | static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) |
| 4741 | { |
| 4742 | unsigned int r; |
| 4743 | |
| 4744 | if (freq == 2) { |
| 4745 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " |
| 4746 | "band"); |
| 4747 | if (wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4748 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4749 | wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4750 | freq = wpa_s->best_24_freq; |
| 4751 | wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " |
| 4752 | "channel: %d MHz", freq); |
| 4753 | } else { |
| 4754 | os_get_random((u8 *) &r, sizeof(r)); |
| 4755 | freq = 2412 + (r % 3) * 25; |
| 4756 | wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " |
| 4757 | "channel: %d MHz", freq); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | if (freq == 5) { |
| 4762 | wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " |
| 4763 | "band"); |
| 4764 | if (wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4765 | p2p_supported_freq_go(wpa_s->global->p2p, |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4766 | wpa_s->best_5_freq)) { |
| 4767 | freq = wpa_s->best_5_freq; |
| 4768 | wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " |
| 4769 | "channel: %d MHz", freq); |
| 4770 | } else { |
| 4771 | os_get_random((u8 *) &r, sizeof(r)); |
| 4772 | freq = 5180 + (r % 4) * 20; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4773 | if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4774 | wpa_printf(MSG_DEBUG, "P2P: Could not select " |
| 4775 | "5 GHz channel for P2P group"); |
| 4776 | return -1; |
| 4777 | } |
| 4778 | wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " |
| 4779 | "channel: %d MHz", freq); |
| 4780 | } |
| 4781 | } |
| 4782 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4783 | if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4784 | wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " |
| 4785 | "(%u MHz) is not supported for P2P uses", |
| 4786 | freq); |
| 4787 | return -1; |
| 4788 | } |
| 4789 | |
| 4790 | return freq; |
| 4791 | } |
| 4792 | |
| 4793 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4794 | static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, |
| 4795 | struct p2p_go_neg_results *params, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4796 | int freq, int ht40, int vht, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4797 | const struct p2p_channels *channels) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4798 | { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4799 | int res, *freqs; |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4800 | unsigned int pref_freq; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4801 | unsigned int num, i; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4802 | |
| 4803 | os_memset(params, 0, sizeof(*params)); |
| 4804 | params->role_go = 1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 4805 | params->ht40 = ht40; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4806 | params->vht = vht; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4807 | if (freq) { |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4808 | if (!freq_included(channels, freq)) { |
| 4809 | wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " |
| 4810 | "accepted", freq); |
| 4811 | return -1; |
| 4812 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4813 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " |
| 4814 | "frequency %d MHz", freq); |
| 4815 | params->freq = freq; |
| 4816 | } else if (wpa_s->conf->p2p_oper_reg_class == 81 && |
| 4817 | wpa_s->conf->p2p_oper_channel >= 1 && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4818 | wpa_s->conf->p2p_oper_channel <= 11 && |
| 4819 | freq_included(channels, |
| 4820 | 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4821 | params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4822 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4823 | "frequency %d MHz", params->freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4824 | } else if ((wpa_s->conf->p2p_oper_reg_class == 115 || |
| 4825 | wpa_s->conf->p2p_oper_reg_class == 116 || |
| 4826 | wpa_s->conf->p2p_oper_reg_class == 117 || |
| 4827 | wpa_s->conf->p2p_oper_reg_class == 124 || |
| 4828 | wpa_s->conf->p2p_oper_reg_class == 126 || |
| 4829 | wpa_s->conf->p2p_oper_reg_class == 127) && |
| 4830 | freq_included(channels, |
| 4831 | 5000 + 5 * wpa_s->conf->p2p_oper_channel)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4832 | params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; |
| 4833 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " |
| 4834 | "frequency %d MHz", params->freq); |
| 4835 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4836 | wpa_s->best_overall_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4837 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4838 | wpa_s->best_overall_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4839 | freq_included(channels, wpa_s->best_overall_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4840 | params->freq = wpa_s->best_overall_freq; |
| 4841 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " |
| 4842 | "channel %d MHz", params->freq); |
| 4843 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4844 | wpa_s->best_24_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4845 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4846 | wpa_s->best_24_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4847 | freq_included(channels, wpa_s->best_24_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4848 | params->freq = wpa_s->best_24_freq; |
| 4849 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " |
| 4850 | "channel %d MHz", params->freq); |
| 4851 | } else if (wpa_s->conf->p2p_oper_channel == 0 && |
| 4852 | wpa_s->best_5_freq > 0 && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4853 | p2p_supported_freq_go(wpa_s->global->p2p, |
| 4854 | wpa_s->best_5_freq) && |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4855 | freq_included(channels, wpa_s->best_5_freq)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4856 | params->freq = wpa_s->best_5_freq; |
| 4857 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " |
| 4858 | "channel %d MHz", params->freq); |
Dmitry Shmidt | 44c9578 | 2013-05-17 09:51:35 -0700 | [diff] [blame] | 4859 | } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p, |
| 4860 | channels))) { |
| 4861 | params->freq = pref_freq; |
| 4862 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " |
| 4863 | "channels", params->freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4864 | } else { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4865 | int chan; |
| 4866 | for (chan = 0; chan < 11; chan++) { |
| 4867 | params->freq = 2412 + chan * 5; |
| 4868 | if (!wpas_p2p_disallowed_freq(wpa_s->global, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 4869 | params->freq) && |
| 4870 | freq_included(channels, params->freq)) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4871 | break; |
| 4872 | } |
| 4873 | if (chan == 11) { |
| 4874 | wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel " |
| 4875 | "allowed"); |
| 4876 | return -1; |
| 4877 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4878 | wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " |
| 4879 | "known)", params->freq); |
| 4880 | } |
| 4881 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4882 | freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); |
| 4883 | if (!freqs) |
| 4884 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4885 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4886 | res = wpas_p2p_valid_oper_freqs(wpa_s, freqs, |
| 4887 | wpa_s->num_multichan_concurrent); |
| 4888 | if (res < 0) { |
| 4889 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4890 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4891 | } |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4892 | num = res; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4893 | |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4894 | for (i = 0; i < num; i++) { |
| 4895 | if (freq && freqs[i] == freq) |
| 4896 | break; |
| 4897 | if (!freq && freq_included(channels, freqs[i])) { |
| 4898 | wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)", |
| 4899 | freqs[i]); |
| 4900 | params->freq = freqs[i]; |
| 4901 | break; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4902 | } |
| 4903 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 4904 | |
| 4905 | if (i == num) { |
| 4906 | if (wpas_p2p_num_unused_channels(wpa_s) <= 0) { |
| 4907 | if (freq) |
| 4908 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq); |
| 4909 | else |
| 4910 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using"); |
| 4911 | os_free(freqs); |
| 4912 | return -1; |
| 4913 | } else if (num == 0) { |
| 4914 | wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels"); |
| 4915 | } else { |
| 4916 | wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels"); |
| 4917 | } |
| 4918 | } |
| 4919 | |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 4920 | os_free(freqs); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4921 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | |
| 4925 | static struct wpa_supplicant * |
| 4926 | wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, |
| 4927 | int go) |
| 4928 | { |
| 4929 | struct wpa_supplicant *group_wpa_s; |
| 4930 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4931 | if (!wpas_p2p_create_iface(wpa_s)) { |
| 4932 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group " |
| 4933 | "operations"); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4934 | wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4935 | return wpa_s; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4936 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4937 | |
| 4938 | 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] | 4939 | WPA_IF_P2P_CLIENT) < 0) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4940 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 4941 | "P2P: Failed to add group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4942 | return NULL; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4943 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4944 | group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); |
| 4945 | if (group_wpa_s == NULL) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 4946 | wpa_msg_global(wpa_s, MSG_ERROR, |
| 4947 | "P2P: Failed to initialize group interface"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4948 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 4949 | return NULL; |
| 4950 | } |
| 4951 | |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 4952 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", |
| 4953 | group_wpa_s->ifname); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 4954 | group_wpa_s->p2p_first_connection_timeout = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4955 | return group_wpa_s; |
| 4956 | } |
| 4957 | |
| 4958 | |
| 4959 | /** |
| 4960 | * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner |
| 4961 | * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() |
| 4962 | * @persistent_group: Whether to create a persistent group |
| 4963 | * @freq: Frequency for the group or 0 to indicate no hardcoding |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4964 | * @ht40: Start GO with 40 MHz channel width |
| 4965 | * @vht: Start GO with VHT support |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4966 | * Returns: 0 on success, -1 on failure |
| 4967 | * |
| 4968 | * This function creates a new P2P group with the local end as the Group Owner, |
| 4969 | * i.e., without using Group Owner Negotiation. |
| 4970 | */ |
| 4971 | 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] | 4972 | int freq, int ht40, int vht) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4973 | { |
| 4974 | struct p2p_go_neg_results params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4975 | |
| 4976 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 4977 | return -1; |
| 4978 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 4979 | os_free(wpa_s->global->add_psk); |
| 4980 | wpa_s->global->add_psk = NULL; |
| 4981 | |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 4982 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4983 | wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4984 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | dca3979 | 2011-09-06 11:17:33 -0700 | [diff] [blame] | 4985 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 4986 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 4987 | if (freq < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4988 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4989 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4990 | 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] | 4991 | return -1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4992 | if (params.freq && |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 4993 | !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 4994 | wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " |
| 4995 | "(%u MHz) is not supported for P2P uses", |
| 4996 | params.freq); |
| 4997 | return -1; |
| 4998 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4999 | p2p_go_params(wpa_s->global->p2p, ¶ms); |
| 5000 | params.persistent_group = persistent_group; |
| 5001 | |
| 5002 | wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); |
| 5003 | if (wpa_s == NULL) |
| 5004 | return -1; |
| 5005 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5006 | |
| 5007 | return 0; |
| 5008 | } |
| 5009 | |
| 5010 | |
| 5011 | static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, |
| 5012 | struct wpa_ssid *params, int addr_allocated) |
| 5013 | { |
| 5014 | struct wpa_ssid *ssid; |
| 5015 | |
| 5016 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); |
| 5017 | if (wpa_s == NULL) |
| 5018 | return -1; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5019 | wpa_s->p2p_last_4way_hs_fail = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5020 | |
| 5021 | wpa_supplicant_ap_deinit(wpa_s); |
| 5022 | |
| 5023 | ssid = wpa_config_add_network(wpa_s->conf); |
| 5024 | if (ssid == NULL) |
| 5025 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5026 | wpa_config_set_network_defaults(ssid); |
| 5027 | ssid->temporary = 1; |
| 5028 | ssid->proto = WPA_PROTO_RSN; |
| 5029 | ssid->pairwise_cipher = WPA_CIPHER_CCMP; |
| 5030 | ssid->group_cipher = WPA_CIPHER_CCMP; |
| 5031 | ssid->key_mgmt = WPA_KEY_MGMT_PSK; |
| 5032 | ssid->ssid = os_malloc(params->ssid_len); |
| 5033 | if (ssid->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5034 | wpa_config_remove_network(wpa_s->conf, ssid->id); |
| 5035 | return -1; |
| 5036 | } |
| 5037 | os_memcpy(ssid->ssid, params->ssid, params->ssid_len); |
| 5038 | ssid->ssid_len = params->ssid_len; |
| 5039 | ssid->p2p_group = 1; |
| 5040 | ssid->export_keys = 1; |
| 5041 | if (params->psk_set) { |
| 5042 | os_memcpy(ssid->psk, params->psk, 32); |
| 5043 | ssid->psk_set = 1; |
| 5044 | } |
| 5045 | if (params->passphrase) |
| 5046 | ssid->passphrase = os_strdup(params->passphrase); |
| 5047 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5048 | wpa_s->show_group_started = 1; |
| 5049 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 5050 | wpa_supplicant_select_network(wpa_s, ssid); |
| 5051 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, |
| 5057 | struct wpa_ssid *ssid, int addr_allocated, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5058 | int freq, int ht40, int vht, |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5059 | const struct p2p_channels *channels, |
| 5060 | int connection_timeout) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5061 | { |
| 5062 | struct p2p_go_neg_results params; |
| 5063 | int go = 0; |
| 5064 | |
| 5065 | if (ssid->disabled != 2 || ssid->ssid == NULL) |
| 5066 | return -1; |
| 5067 | |
| 5068 | if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && |
| 5069 | go == (ssid->mode == WPAS_MODE_P2P_GO)) { |
| 5070 | wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " |
| 5071 | "already running"); |
| 5072 | return 0; |
| 5073 | } |
| 5074 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 5075 | os_free(wpa_s->global->add_psk); |
| 5076 | wpa_s->global->add_psk = NULL; |
| 5077 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5078 | /* Make sure we are not running find during connection establishment */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5079 | wpas_p2p_stop_find_oper(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5080 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5081 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5082 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5083 | if (ssid->mode == WPAS_MODE_INFRA) |
| 5084 | return wpas_start_p2p_client(wpa_s, ssid, addr_allocated); |
| 5085 | |
| 5086 | if (ssid->mode != WPAS_MODE_P2P_GO) |
| 5087 | return -1; |
| 5088 | |
Dmitry Shmidt | 8bae413 | 2013-06-06 11:25:10 -0700 | [diff] [blame] | 5089 | freq = wpas_p2p_select_go_freq(wpa_s, freq); |
| 5090 | if (freq < 0) |
| 5091 | return -1; |
| 5092 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5093 | 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] | 5094 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5095 | |
| 5096 | params.role_go = 1; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5097 | params.psk_set = ssid->psk_set; |
| 5098 | if (params.psk_set) |
| 5099 | os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5100 | if (ssid->passphrase) { |
| 5101 | if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { |
| 5102 | wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " |
| 5103 | "persistent group"); |
| 5104 | return -1; |
| 5105 | } |
| 5106 | os_strlcpy(params.passphrase, ssid->passphrase, |
| 5107 | sizeof(params.passphrase)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5108 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5109 | os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); |
| 5110 | params.ssid_len = ssid->ssid_len; |
| 5111 | params.persistent_group = 1; |
| 5112 | |
| 5113 | wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); |
| 5114 | if (wpa_s == NULL) |
| 5115 | return -1; |
| 5116 | |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5117 | wpa_s->p2p_first_connection_timeout = connection_timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5118 | wpas_start_wps_go(wpa_s, ¶ms, 0); |
| 5119 | |
| 5120 | return 0; |
| 5121 | } |
| 5122 | |
| 5123 | |
| 5124 | static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, |
| 5125 | struct wpabuf *proberesp_ies) |
| 5126 | { |
| 5127 | struct wpa_supplicant *wpa_s = ctx; |
| 5128 | if (wpa_s->ap_iface) { |
| 5129 | struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5130 | if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { |
| 5131 | wpabuf_free(beacon_ies); |
| 5132 | wpabuf_free(proberesp_ies); |
| 5133 | return; |
| 5134 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5135 | if (beacon_ies) { |
| 5136 | wpabuf_free(hapd->p2p_beacon_ie); |
| 5137 | hapd->p2p_beacon_ie = beacon_ies; |
| 5138 | } |
| 5139 | wpabuf_free(hapd->p2p_probe_resp_ie); |
| 5140 | hapd->p2p_probe_resp_ie = proberesp_ies; |
| 5141 | } else { |
| 5142 | wpabuf_free(beacon_ies); |
| 5143 | wpabuf_free(proberesp_ies); |
| 5144 | } |
| 5145 | wpa_supplicant_ap_update_beacon(wpa_s); |
| 5146 | } |
| 5147 | |
| 5148 | |
| 5149 | static void wpas_p2p_idle_update(void *ctx, int idle) |
| 5150 | { |
| 5151 | struct wpa_supplicant *wpa_s = ctx; |
| 5152 | if (!wpa_s->ap_iface) |
| 5153 | return; |
| 5154 | wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5155 | if (idle) { |
| 5156 | if (wpa_s->global->p2p_fail_on_wps_complete && |
| 5157 | wpa_s->p2p_in_provisioning) { |
| 5158 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5159 | return; |
| 5160 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5161 | wpas_p2p_set_group_idle_timeout(wpa_s); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5162 | } else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5163 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); |
| 5164 | } |
| 5165 | |
| 5166 | |
| 5167 | struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5168 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5169 | { |
| 5170 | struct p2p_group *group; |
| 5171 | struct p2p_group_config *cfg; |
| 5172 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5173 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5174 | return NULL; |
| 5175 | |
| 5176 | cfg = os_zalloc(sizeof(*cfg)); |
| 5177 | if (cfg == NULL) |
| 5178 | return NULL; |
| 5179 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5180 | if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5181 | cfg->persistent_group = 2; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5182 | else if (ssid->p2p_persistent_group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5183 | cfg->persistent_group = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5184 | os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); |
| 5185 | if (wpa_s->max_stations && |
| 5186 | wpa_s->max_stations < wpa_s->conf->max_num_sta) |
| 5187 | cfg->max_clients = wpa_s->max_stations; |
| 5188 | else |
| 5189 | cfg->max_clients = wpa_s->conf->max_num_sta; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5190 | os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); |
| 5191 | cfg->ssid_len = ssid->ssid_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5192 | cfg->cb_ctx = wpa_s; |
| 5193 | cfg->ie_update = wpas_p2p_ie_update; |
| 5194 | cfg->idle_update = wpas_p2p_idle_update; |
| 5195 | |
| 5196 | group = p2p_group_init(wpa_s->global->p2p, cfg); |
| 5197 | if (group == NULL) |
| 5198 | os_free(cfg); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5199 | if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5200 | p2p_group_notif_formation_done(group); |
| 5201 | wpa_s->p2p_group = group; |
| 5202 | return group; |
| 5203 | } |
| 5204 | |
| 5205 | |
| 5206 | void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, |
| 5207 | int registrar) |
| 5208 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5209 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5210 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5211 | if (!wpa_s->p2p_in_provisioning) { |
| 5212 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " |
| 5213 | "provisioning not in progress"); |
| 5214 | return; |
| 5215 | } |
| 5216 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5217 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5218 | u8 go_dev_addr[ETH_ALEN]; |
| 5219 | os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); |
| 5220 | wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5221 | ssid->ssid_len); |
| 5222 | /* Clear any stored provisioning info */ |
| 5223 | p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); |
| 5224 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5225 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5226 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, |
| 5227 | NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5228 | wpa_s->p2p_go_group_formation_completed = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5229 | if (ssid && ssid->mode == WPAS_MODE_INFRA) { |
| 5230 | /* |
| 5231 | * Use a separate timeout for initial data connection to |
| 5232 | * complete to allow the group to be removed automatically if |
| 5233 | * something goes wrong in this step before the P2P group idle |
| 5234 | * timeout mechanism is taken into use. |
| 5235 | */ |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5236 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5237 | "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", |
| 5238 | P2P_MAX_INITIAL_CONN_WAIT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5239 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, |
| 5240 | wpas_p2p_group_formation_timeout, |
| 5241 | wpa_s->parent, NULL); |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 5242 | } else if (ssid) { |
| 5243 | /* |
| 5244 | * Use a separate timeout for initial data connection to |
| 5245 | * complete to allow the group to be removed automatically if |
| 5246 | * the client does not complete data connection successfully. |
| 5247 | */ |
| 5248 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 5249 | "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", |
| 5250 | P2P_MAX_INITIAL_CONN_WAIT_GO); |
| 5251 | eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, |
| 5252 | wpas_p2p_group_formation_timeout, |
| 5253 | wpa_s->parent, NULL); |
| 5254 | /* |
| 5255 | * Complete group formation on first successful data connection |
| 5256 | */ |
| 5257 | wpa_s->p2p_go_group_formation_completed = 0; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5258 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5259 | if (wpa_s->global->p2p) |
| 5260 | p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5261 | wpas_group_formation_completed(wpa_s, 1); |
| 5262 | } |
| 5263 | |
| 5264 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5265 | void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, |
| 5266 | struct wps_event_fail *fail) |
| 5267 | { |
| 5268 | if (!wpa_s->p2p_in_provisioning) { |
| 5269 | wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " |
| 5270 | "provisioning not in progress"); |
| 5271 | return; |
| 5272 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5273 | |
| 5274 | if (wpa_s->go_params) { |
| 5275 | p2p_clear_provisioning_info( |
| 5276 | wpa_s->global->p2p, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5277 | wpa_s->go_params->peer_device_addr); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5278 | } |
| 5279 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5280 | wpas_notify_p2p_wps_failed(wpa_s, fail); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5281 | |
| 5282 | if (wpa_s == wpa_s->global->p2p_group_formation) { |
| 5283 | /* |
| 5284 | * Allow some time for the failed WPS negotiation exchange to |
| 5285 | * complete, but remove the group since group formation cannot |
| 5286 | * succeed after provisioning failure. |
| 5287 | */ |
| 5288 | wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); |
| 5289 | wpa_s->global->p2p_fail_on_wps_complete = 1; |
| 5290 | eloop_deplete_timeout(0, 50000, |
| 5291 | wpas_p2p_group_formation_timeout, |
| 5292 | wpa_s->parent, NULL); |
| 5293 | } |
| 5294 | } |
| 5295 | |
| 5296 | |
| 5297 | int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) |
| 5298 | { |
| 5299 | if (!wpa_s->global->p2p_fail_on_wps_complete || |
| 5300 | !wpa_s->p2p_in_provisioning) |
| 5301 | return 0; |
| 5302 | |
| 5303 | wpas_p2p_grpform_fail_after_wps(wpa_s); |
| 5304 | |
| 5305 | return 1; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5309 | 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] | 5310 | const char *config_method, |
| 5311 | enum wpas_p2p_prov_disc_use use) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5312 | { |
| 5313 | u16 config_methods; |
| 5314 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5315 | wpa_s->p2p_fallback_to_go_neg = 0; |
| 5316 | wpa_s->pending_pd_use = NORMAL_PD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5317 | if (os_strncmp(config_method, "display", 7) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5318 | config_methods = WPS_CONFIG_DISPLAY; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5319 | else if (os_strncmp(config_method, "keypad", 6) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5320 | config_methods = WPS_CONFIG_KEYPAD; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5321 | else if (os_strncmp(config_method, "pbc", 3) == 0 || |
| 5322 | os_strncmp(config_method, "pushbutton", 10) == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5323 | config_methods = WPS_CONFIG_PUSHBUTTON; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5324 | else { |
| 5325 | wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5326 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5327 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5328 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5329 | if (use == WPAS_P2P_PD_AUTO) { |
| 5330 | os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); |
| 5331 | wpa_s->pending_pd_config_methods = config_methods; |
| 5332 | wpa_s->p2p_auto_pd = 1; |
| 5333 | wpa_s->p2p_auto_join = 0; |
| 5334 | wpa_s->pending_pd_before_join = 0; |
| 5335 | wpa_s->auto_pd_scan_retry = 0; |
| 5336 | wpas_p2p_stop_find(wpa_s); |
| 5337 | wpa_s->p2p_join_scan_count = 0; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5338 | os_get_reltime(&wpa_s->p2p_auto_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5339 | wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", |
| 5340 | wpa_s->p2p_auto_started.sec, |
| 5341 | wpa_s->p2p_auto_started.usec); |
| 5342 | wpas_p2p_join_scan(wpa_s, NULL); |
| 5343 | return 0; |
| 5344 | } |
| 5345 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5346 | if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) |
| 5347 | return -1; |
| 5348 | |
| 5349 | return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5350 | config_methods, use == WPAS_P2P_PD_FOR_JOIN, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5351 | 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | |
| 5355 | int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, |
| 5356 | char *end) |
| 5357 | { |
| 5358 | return p2p_scan_result_text(ies, ies_len, buf, end); |
| 5359 | } |
| 5360 | |
| 5361 | |
| 5362 | static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) |
| 5363 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5364 | if (!offchannel_pending_action_tx(wpa_s)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5365 | return; |
| 5366 | |
| 5367 | wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " |
| 5368 | "operation request"); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 5369 | offchannel_clear_pending_action_tx(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | |
| 5373 | int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, |
| 5374 | enum p2p_discovery_type type, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5375 | unsigned int num_req_dev_types, const u8 *req_dev_types, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5376 | const u8 *dev_id, unsigned int search_delay) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5377 | { |
| 5378 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5379 | wpa_s->p2p_long_listen = 0; |
| 5380 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5381 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || |
| 5382 | wpa_s->p2p_in_provisioning) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5383 | return -1; |
| 5384 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5385 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 5386 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5387 | return p2p_find(wpa_s->global->p2p, timeout, type, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5388 | num_req_dev_types, req_dev_types, dev_id, |
| 5389 | search_delay); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5393 | static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5394 | { |
| 5395 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5396 | wpa_s->p2p_long_listen = 0; |
| 5397 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5398 | eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5399 | |
| 5400 | if (wpa_s->global->p2p) |
| 5401 | p2p_stop_find(wpa_s->global->p2p); |
| 5402 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5403 | return 0; |
| 5404 | } |
| 5405 | |
| 5406 | |
| 5407 | void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) |
| 5408 | { |
| 5409 | if (wpas_p2p_stop_find_oper(wpa_s) > 0) |
| 5410 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5411 | wpas_p2p_remove_pending_group_interface(wpa_s); |
| 5412 | } |
| 5413 | |
| 5414 | |
| 5415 | static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5416 | { |
| 5417 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5418 | wpa_s->p2p_long_listen = 0; |
| 5419 | } |
| 5420 | |
| 5421 | |
| 5422 | int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) |
| 5423 | { |
| 5424 | int res; |
| 5425 | |
| 5426 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5427 | return -1; |
| 5428 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5429 | wpa_supplicant_cancel_sched_scan(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5430 | wpas_p2p_clear_pending_action_tx(wpa_s); |
| 5431 | |
| 5432 | if (timeout == 0) { |
| 5433 | /* |
| 5434 | * This is a request for unlimited Listen state. However, at |
| 5435 | * least for now, this is mapped to a Listen state for one |
| 5436 | * hour. |
| 5437 | */ |
| 5438 | timeout = 3600; |
| 5439 | } |
| 5440 | eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); |
| 5441 | wpa_s->p2p_long_listen = 0; |
| 5442 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5443 | /* |
| 5444 | * Stop previous find/listen operation to avoid trying to request a new |
| 5445 | * remain-on-channel operation while the driver is still running the |
| 5446 | * previous one. |
| 5447 | */ |
| 5448 | if (wpa_s->global->p2p) |
| 5449 | p2p_stop_find(wpa_s->global->p2p); |
| 5450 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5451 | res = wpas_p2p_listen_start(wpa_s, timeout * 1000); |
| 5452 | if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { |
| 5453 | wpa_s->p2p_long_listen = timeout * 1000; |
| 5454 | eloop_register_timeout(timeout, 0, |
| 5455 | wpas_p2p_long_listen_timeout, |
| 5456 | wpa_s, NULL); |
| 5457 | } |
| 5458 | |
| 5459 | return res; |
| 5460 | } |
| 5461 | |
| 5462 | |
| 5463 | int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
| 5464 | u8 *buf, size_t len, int p2p_group) |
| 5465 | { |
| 5466 | struct wpabuf *p2p_ie; |
| 5467 | int ret; |
| 5468 | |
| 5469 | if (wpa_s->global->p2p_disabled) |
| 5470 | return -1; |
| 5471 | if (wpa_s->global->p2p == NULL) |
| 5472 | return -1; |
| 5473 | if (bss == NULL) |
| 5474 | return -1; |
| 5475 | |
| 5476 | p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); |
| 5477 | ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, |
| 5478 | p2p_group, p2p_ie); |
| 5479 | wpabuf_free(p2p_ie); |
| 5480 | |
| 5481 | return ret; |
| 5482 | } |
| 5483 | |
| 5484 | |
| 5485 | 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] | 5486 | const u8 *dst, const u8 *bssid, |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5487 | const u8 *ie, size_t ie_len, int ssi_signal) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5488 | { |
| 5489 | if (wpa_s->global->p2p_disabled) |
| 5490 | return 0; |
| 5491 | if (wpa_s->global->p2p == NULL) |
| 5492 | return 0; |
| 5493 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5494 | switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, |
| 5495 | ie, ie_len)) { |
| 5496 | case P2P_PREQ_NOT_P2P: |
| 5497 | wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, |
| 5498 | ssi_signal); |
| 5499 | /* fall through */ |
| 5500 | case P2P_PREQ_MALFORMED: |
| 5501 | case P2P_PREQ_NOT_LISTEN: |
| 5502 | case P2P_PREQ_NOT_PROCESSED: |
| 5503 | default: /* make gcc happy */ |
| 5504 | return 0; |
| 5505 | case P2P_PREQ_PROCESSED: |
| 5506 | return 1; |
| 5507 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | |
| 5511 | void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, |
| 5512 | const u8 *sa, const u8 *bssid, |
| 5513 | u8 category, const u8 *data, size_t len, int freq) |
| 5514 | { |
| 5515 | if (wpa_s->global->p2p_disabled) |
| 5516 | return; |
| 5517 | if (wpa_s->global->p2p == NULL) |
| 5518 | return; |
| 5519 | |
| 5520 | p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, |
| 5521 | freq); |
| 5522 | } |
| 5523 | |
| 5524 | |
| 5525 | void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) |
| 5526 | { |
| 5527 | if (wpa_s->global->p2p_disabled) |
| 5528 | return; |
| 5529 | if (wpa_s->global->p2p == NULL) |
| 5530 | return; |
| 5531 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5532 | p2p_scan_ie(wpa_s->global->p2p, ies, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | |
| 5536 | void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) |
| 5537 | { |
| 5538 | p2p_group_deinit(wpa_s->p2p_group); |
| 5539 | wpa_s->p2p_group = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5540 | |
| 5541 | wpa_s->ap_configured_cb = NULL; |
| 5542 | wpa_s->ap_configured_cb_ctx = NULL; |
| 5543 | wpa_s->ap_configured_cb_data = NULL; |
| 5544 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | |
| 5548 | int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) |
| 5549 | { |
| 5550 | wpa_s->p2p_long_listen = 0; |
| 5551 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5552 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5553 | return -1; |
| 5554 | |
| 5555 | return p2p_reject(wpa_s->global->p2p, addr); |
| 5556 | } |
| 5557 | |
| 5558 | |
| 5559 | /* Invite to reinvoke a persistent group */ |
| 5560 | 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] | 5561 | struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5562 | int ht40, int vht, int pref_freq) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5563 | { |
| 5564 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5565 | u8 *bssid = NULL; |
| 5566 | int force_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5567 | int res; |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5568 | int no_pref_freq_given = pref_freq == 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5569 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5570 | wpa_s->global->p2p_invite_group = NULL; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5571 | if (peer_addr) |
| 5572 | os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); |
| 5573 | else |
| 5574 | os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5575 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5576 | wpa_s->p2p_persistent_go_freq = freq; |
| 5577 | wpa_s->p2p_go_ht40 = !!ht40; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5578 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5579 | role = P2P_INVITE_ROLE_GO; |
| 5580 | if (peer_addr == NULL) { |
| 5581 | wpa_printf(MSG_DEBUG, "P2P: Missing peer " |
| 5582 | "address in invitation command"); |
| 5583 | return -1; |
| 5584 | } |
| 5585 | if (wpas_p2p_create_iface(wpa_s)) { |
| 5586 | if (wpas_p2p_add_group_interface(wpa_s, |
| 5587 | WPA_IF_P2P_GO) < 0) { |
| 5588 | wpa_printf(MSG_ERROR, "P2P: Failed to " |
| 5589 | "allocate a new interface for the " |
| 5590 | "group"); |
| 5591 | return -1; |
| 5592 | } |
| 5593 | bssid = wpa_s->pending_interface_addr; |
| 5594 | } else |
| 5595 | bssid = wpa_s->own_addr; |
| 5596 | } else { |
| 5597 | role = P2P_INVITE_ROLE_CLIENT; |
| 5598 | peer_addr = ssid->bssid; |
| 5599 | } |
| 5600 | wpa_s->pending_invite_ssid_id = ssid->id; |
| 5601 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5602 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5603 | role == P2P_INVITE_ROLE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5604 | if (res) |
| 5605 | return res; |
Irfan Sheriff | af84a57 | 2012-09-22 16:59:30 -0700 | [diff] [blame] | 5606 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5607 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5608 | return -1; |
| 5609 | |
Dmitry Shmidt | a0d265f | 2013-11-19 13:13:41 -0800 | [diff] [blame] | 5610 | if (wpa_s->parent->conf->p2p_ignore_shared_freq && |
| 5611 | no_pref_freq_given && pref_freq > 0 && |
| 5612 | wpa_s->num_multichan_concurrent > 1 && |
| 5613 | wpas_p2p_num_unused_channels(wpa_s) > 0) { |
| 5614 | wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", |
| 5615 | pref_freq); |
| 5616 | pref_freq = 0; |
| 5617 | } |
| 5618 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5619 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5620 | ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, |
| 5621 | 1, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5622 | } |
| 5623 | |
| 5624 | |
| 5625 | /* Invite to join an active group */ |
| 5626 | int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, |
| 5627 | const u8 *peer_addr, const u8 *go_dev_addr) |
| 5628 | { |
| 5629 | struct wpa_global *global = wpa_s->global; |
| 5630 | enum p2p_invite_role role; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5631 | u8 *bssid = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5632 | struct wpa_ssid *ssid; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5633 | int persistent; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5634 | int freq = 0, force_freq = 0, pref_freq = 0; |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5635 | int res; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5636 | |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5637 | wpa_s->p2p_persistent_go_freq = 0; |
| 5638 | wpa_s->p2p_go_ht40 = 0; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5639 | wpa_s->p2p_go_vht = 0; |
Jouni Malinen | 31be0a4 | 2012-08-31 21:20:51 +0300 | [diff] [blame] | 5640 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5641 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 5642 | if (os_strcmp(wpa_s->ifname, ifname) == 0) |
| 5643 | break; |
| 5644 | } |
| 5645 | if (wpa_s == NULL) { |
| 5646 | wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); |
| 5647 | return -1; |
| 5648 | } |
| 5649 | |
| 5650 | ssid = wpa_s->current_ssid; |
| 5651 | if (ssid == NULL) { |
| 5652 | wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " |
| 5653 | "invitation"); |
| 5654 | return -1; |
| 5655 | } |
| 5656 | |
Dmitry Shmidt | 700a137 | 2013-03-15 14:14:44 -0700 | [diff] [blame] | 5657 | wpa_s->global->p2p_invite_group = wpa_s; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5658 | persistent = ssid->p2p_persistent_group && |
| 5659 | wpas_p2p_get_persistent(wpa_s->parent, peer_addr, |
| 5660 | ssid->ssid, ssid->ssid_len); |
| 5661 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5662 | if (ssid->mode == WPAS_MODE_P2P_GO) { |
| 5663 | role = P2P_INVITE_ROLE_ACTIVE_GO; |
| 5664 | bssid = wpa_s->own_addr; |
| 5665 | if (go_dev_addr == NULL) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5666 | go_dev_addr = wpa_s->global->p2p_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5667 | freq = ssid->frequency; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5668 | } else { |
| 5669 | role = P2P_INVITE_ROLE_CLIENT; |
| 5670 | if (wpa_s->wpa_state < WPA_ASSOCIATED) { |
| 5671 | wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " |
| 5672 | "invite to current group"); |
| 5673 | return -1; |
| 5674 | } |
| 5675 | bssid = wpa_s->bssid; |
| 5676 | if (go_dev_addr == NULL && |
| 5677 | !is_zero_ether_addr(wpa_s->go_dev_addr)) |
| 5678 | go_dev_addr = wpa_s->go_dev_addr; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 5679 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5680 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5681 | } |
| 5682 | wpa_s->parent->pending_invite_ssid_id = -1; |
| 5683 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5684 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5685 | return -1; |
| 5686 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 5687 | res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, |
| 5688 | role == P2P_INVITE_ROLE_ACTIVE_GO); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5689 | if (res) |
| 5690 | return res; |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 5691 | wpas_p2p_set_own_freq_preference(wpa_s, force_freq); |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5692 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5693 | return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, |
Dmitry Shmidt | 7a5e50a | 2013-03-05 12:37:16 -0800 | [diff] [blame] | 5694 | ssid->ssid, ssid->ssid_len, force_freq, |
| 5695 | go_dev_addr, persistent, pref_freq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | |
| 5699 | void wpas_p2p_completed(struct wpa_supplicant *wpa_s) |
| 5700 | { |
| 5701 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 5702 | const char *ssid_txt; |
| 5703 | u8 go_dev_addr[ETH_ALEN]; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5704 | int network_id = -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5705 | int persistent; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5706 | int freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5707 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5708 | if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { |
| 5709 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 5710 | wpa_s->parent, NULL); |
| 5711 | } |
| 5712 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5713 | if (!wpa_s->show_group_started || !ssid) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5714 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5715 | |
| 5716 | wpa_s->show_group_started = 0; |
| 5717 | |
| 5718 | ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); |
| 5719 | os_memset(go_dev_addr, 0, ETH_ALEN); |
| 5720 | if (ssid->bssid_set) |
| 5721 | os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); |
| 5722 | persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, |
| 5723 | ssid->ssid_len); |
| 5724 | os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); |
| 5725 | |
| 5726 | if (wpa_s->global->p2p_group_formation == wpa_s) |
| 5727 | wpa_s->global->p2p_group_formation = NULL; |
| 5728 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5729 | freq = wpa_s->current_bss ? wpa_s->current_bss->freq : |
| 5730 | (int) wpa_s->assoc_freq; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5731 | if (ssid->passphrase == NULL && ssid->psk_set) { |
| 5732 | char psk[65]; |
| 5733 | wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5734 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5735 | "%s client ssid=\"%s\" freq=%d psk=%s " |
| 5736 | "go_dev_addr=" MACSTR "%s", |
| 5737 | wpa_s->ifname, ssid_txt, freq, psk, |
| 5738 | MAC2STR(go_dev_addr), |
| 5739 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5740 | } else { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 5741 | wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED |
| 5742 | "%s client ssid=\"%s\" freq=%d " |
| 5743 | "passphrase=\"%s\" go_dev_addr=" MACSTR "%s", |
| 5744 | wpa_s->ifname, ssid_txt, freq, |
| 5745 | ssid->passphrase ? ssid->passphrase : "", |
| 5746 | MAC2STR(go_dev_addr), |
| 5747 | persistent ? " [PERSISTENT]" : ""); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | if (persistent) |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5751 | network_id = wpas_p2p_store_persistent_group(wpa_s->parent, |
| 5752 | ssid, go_dev_addr); |
| 5753 | if (network_id < 0) |
| 5754 | network_id = ssid->id; |
| 5755 | wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | |
| 5759 | int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, |
| 5760 | u32 interval1, u32 duration2, u32 interval2) |
| 5761 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5762 | int ret; |
| 5763 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5764 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5765 | return -1; |
| 5766 | |
| 5767 | if (wpa_s->wpa_state < WPA_ASSOCIATED || |
| 5768 | wpa_s->current_ssid == NULL || |
| 5769 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA) |
| 5770 | return -1; |
| 5771 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 5772 | ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, |
| 5773 | wpa_s->own_addr, wpa_s->assoc_freq, |
| 5774 | duration1, interval1, duration2, interval2); |
| 5775 | if (ret == 0) |
| 5776 | wpa_s->waiting_presence_resp = 1; |
| 5777 | |
| 5778 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | |
| 5782 | int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, |
| 5783 | unsigned int interval) |
| 5784 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5785 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5786 | return -1; |
| 5787 | |
| 5788 | return p2p_ext_listen(wpa_s->global->p2p, period, interval); |
| 5789 | } |
| 5790 | |
| 5791 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5792 | static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) |
| 5793 | { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 5794 | if (wpa_s->current_ssid == NULL) { |
| 5795 | /* |
| 5796 | * current_ssid can be cleared when P2P client interface gets |
| 5797 | * disconnected, so assume this interface was used as P2P |
| 5798 | * client. |
| 5799 | */ |
| 5800 | return 1; |
| 5801 | } |
| 5802 | return wpa_s->current_ssid->p2p_group && |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5803 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA; |
| 5804 | } |
| 5805 | |
| 5806 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5807 | static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) |
| 5808 | { |
| 5809 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 5810 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5811 | 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] | 5812 | wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " |
| 5813 | "disabled"); |
| 5814 | return; |
| 5815 | } |
| 5816 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5817 | wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " |
| 5818 | "group"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 5819 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | |
| 5823 | static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) |
| 5824 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5825 | int timeout; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5826 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5827 | if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 5828 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
| 5829 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5830 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 5831 | return; |
| 5832 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5833 | timeout = wpa_s->conf->p2p_group_idle; |
| 5834 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && |
| 5835 | (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) |
| 5836 | timeout = P2P_MAX_CLIENT_IDLE; |
| 5837 | |
| 5838 | if (timeout == 0) |
| 5839 | return; |
| 5840 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5841 | if (timeout < 0) { |
| 5842 | if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) |
| 5843 | timeout = 0; /* special client mode no-timeout */ |
| 5844 | else |
| 5845 | return; |
| 5846 | } |
| 5847 | |
| 5848 | if (wpa_s->p2p_in_provisioning) { |
| 5849 | /* |
| 5850 | * Use the normal group formation timeout during the |
| 5851 | * provisioning phase to avoid terminating this process too |
| 5852 | * early due to group idle timeout. |
| 5853 | */ |
| 5854 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 5855 | "during provisioning"); |
| 5856 | return; |
| 5857 | } |
Deepthi Gowri | 9e4e8ac | 2013-04-16 11:57:05 +0530 | [diff] [blame] | 5858 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5859 | if (wpa_s->show_group_started) { |
| 5860 | /* |
| 5861 | * Use the normal group formation timeout between the end of |
| 5862 | * the provisioning phase and completion of 4-way handshake to |
| 5863 | * avoid terminating this process too early due to group idle |
| 5864 | * timeout. |
| 5865 | */ |
| 5866 | wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " |
| 5867 | "while waiting for initial 4-way handshake to " |
| 5868 | "complete"); |
| 5869 | return; |
| 5870 | } |
| 5871 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5872 | 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] | 5873 | timeout); |
| 5874 | eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, |
| 5875 | wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5879 | /* Returns 1 if the interface was removed */ |
| 5880 | int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, |
| 5881 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 5882 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5883 | { |
| 5884 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5885 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5886 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5887 | if (!locally_generated) |
| 5888 | p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 5889 | ie_len); |
| 5890 | |
| 5891 | if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && |
| 5892 | wpa_s->current_ssid && |
| 5893 | wpa_s->current_ssid->p2p_group && |
| 5894 | wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { |
| 5895 | wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " |
| 5896 | "session is ending"); |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5897 | if (wpas_p2p_group_delete(wpa_s, |
| 5898 | P2P_GROUP_REMOVAL_GO_ENDING_SESSION) |
| 5899 | > 0) |
| 5900 | return 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5901 | } |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 5902 | |
| 5903 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | |
| 5907 | 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] | 5908 | u16 reason_code, const u8 *ie, size_t ie_len, |
| 5909 | int locally_generated) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5910 | { |
| 5911 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 5912 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5913 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 5914 | if (!locally_generated) |
| 5915 | p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, |
| 5916 | ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | |
| 5920 | void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) |
| 5921 | { |
| 5922 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 5923 | |
| 5924 | if (p2p == NULL) |
| 5925 | return; |
| 5926 | |
| 5927 | if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) |
| 5928 | return; |
| 5929 | |
| 5930 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) |
| 5931 | p2p_set_dev_name(p2p, wpa_s->conf->device_name); |
| 5932 | |
| 5933 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) |
| 5934 | p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); |
| 5935 | |
| 5936 | if (wpa_s->wps && |
| 5937 | (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) |
| 5938 | p2p_set_config_methods(p2p, wpa_s->wps->config_methods); |
| 5939 | |
| 5940 | if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) |
| 5941 | p2p_set_uuid(p2p, wpa_s->wps->uuid); |
| 5942 | |
| 5943 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { |
| 5944 | p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); |
| 5945 | p2p_set_model_name(p2p, wpa_s->conf->model_name); |
| 5946 | p2p_set_model_number(p2p, wpa_s->conf->model_number); |
| 5947 | p2p_set_serial_number(p2p, wpa_s->conf->serial_number); |
| 5948 | } |
| 5949 | |
| 5950 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) |
| 5951 | p2p_set_sec_dev_types(p2p, |
| 5952 | (void *) wpa_s->conf->sec_device_type, |
| 5953 | wpa_s->conf->num_sec_device_types); |
| 5954 | |
| 5955 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { |
| 5956 | int i; |
| 5957 | p2p_remove_wps_vendor_extensions(p2p); |
| 5958 | for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { |
| 5959 | if (wpa_s->conf->wps_vendor_ext[i] == NULL) |
| 5960 | continue; |
| 5961 | p2p_add_wps_vendor_extension( |
| 5962 | p2p, wpa_s->conf->wps_vendor_ext[i]); |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && |
| 5967 | wpa_s->conf->country[0] && wpa_s->conf->country[1]) { |
| 5968 | char country[3]; |
| 5969 | country[0] = wpa_s->conf->country[0]; |
| 5970 | country[1] = wpa_s->conf->country[1]; |
| 5971 | country[2] = 0x04; |
| 5972 | p2p_set_country(p2p, country); |
| 5973 | } |
| 5974 | |
| 5975 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { |
| 5976 | p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, |
| 5977 | wpa_s->conf->p2p_ssid_postfix ? |
| 5978 | os_strlen(wpa_s->conf->p2p_ssid_postfix) : |
| 5979 | 0); |
| 5980 | } |
| 5981 | |
| 5982 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) |
| 5983 | p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 5984 | |
| 5985 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { |
| 5986 | u8 reg_class, channel; |
| 5987 | int ret; |
| 5988 | unsigned int r; |
| 5989 | if (wpa_s->conf->p2p_listen_reg_class && |
| 5990 | wpa_s->conf->p2p_listen_channel) { |
| 5991 | reg_class = wpa_s->conf->p2p_listen_reg_class; |
| 5992 | channel = wpa_s->conf->p2p_listen_channel; |
| 5993 | } else { |
| 5994 | reg_class = 81; |
| 5995 | /* |
| 5996 | * Pick one of the social channels randomly as the |
| 5997 | * listen channel. |
| 5998 | */ |
| 5999 | os_get_random((u8 *) &r, sizeof(r)); |
| 6000 | channel = 1 + (r % 3) * 5; |
| 6001 | } |
| 6002 | ret = p2p_set_listen_channel(p2p, reg_class, channel); |
| 6003 | if (ret) |
| 6004 | wpa_printf(MSG_ERROR, "P2P: Own listen channel update " |
| 6005 | "failed: %d", ret); |
| 6006 | } |
| 6007 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { |
| 6008 | u8 op_reg_class, op_channel, cfg_op_channel; |
| 6009 | int ret = 0; |
| 6010 | unsigned int r; |
| 6011 | if (wpa_s->conf->p2p_oper_reg_class && |
| 6012 | wpa_s->conf->p2p_oper_channel) { |
| 6013 | op_reg_class = wpa_s->conf->p2p_oper_reg_class; |
| 6014 | op_channel = wpa_s->conf->p2p_oper_channel; |
| 6015 | cfg_op_channel = 1; |
| 6016 | } else { |
| 6017 | op_reg_class = 81; |
| 6018 | /* |
| 6019 | * Use random operation channel from (1, 6, 11) |
| 6020 | *if no other preference is indicated. |
| 6021 | */ |
| 6022 | os_get_random((u8 *) &r, sizeof(r)); |
| 6023 | op_channel = 1 + (r % 3) * 5; |
| 6024 | cfg_op_channel = 0; |
| 6025 | } |
| 6026 | ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, |
| 6027 | cfg_op_channel); |
| 6028 | if (ret) |
| 6029 | wpa_printf(MSG_ERROR, "P2P: Own oper channel update " |
| 6030 | "failed: %d", ret); |
| 6031 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6032 | |
| 6033 | if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { |
| 6034 | if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, |
| 6035 | wpa_s->conf->p2p_pref_chan) < 0) { |
| 6036 | wpa_printf(MSG_ERROR, "P2P: Preferred channel list " |
| 6037 | "update failed"); |
| 6038 | } |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6039 | |
| 6040 | if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { |
| 6041 | wpa_printf(MSG_ERROR, "P2P: No GO channel list " |
| 6042 | "update failed"); |
| 6043 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6044 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | |
| 6048 | int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, |
| 6049 | int duration) |
| 6050 | { |
| 6051 | if (!wpa_s->ap_iface) |
| 6052 | return -1; |
| 6053 | return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, |
| 6054 | duration); |
| 6055 | } |
| 6056 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6057 | |
| 6058 | int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) |
| 6059 | { |
| 6060 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6061 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6062 | |
| 6063 | wpa_s->global->cross_connection = enabled; |
| 6064 | p2p_set_cross_connect(wpa_s->global->p2p, enabled); |
| 6065 | |
| 6066 | if (!enabled) { |
| 6067 | struct wpa_supplicant *iface; |
| 6068 | |
| 6069 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) |
| 6070 | { |
| 6071 | if (iface->cross_connect_enabled == 0) |
| 6072 | continue; |
| 6073 | |
| 6074 | iface->cross_connect_enabled = 0; |
| 6075 | iface->cross_connect_in_use = 0; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6076 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6077 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6078 | iface->ifname, |
| 6079 | iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | return 0; |
| 6084 | } |
| 6085 | |
| 6086 | |
| 6087 | static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) |
| 6088 | { |
| 6089 | struct wpa_supplicant *iface; |
| 6090 | |
| 6091 | if (!uplink->global->cross_connection) |
| 6092 | return; |
| 6093 | |
| 6094 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6095 | if (!iface->cross_connect_enabled) |
| 6096 | continue; |
| 6097 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6098 | 0) |
| 6099 | continue; |
| 6100 | if (iface->ap_iface == NULL) |
| 6101 | continue; |
| 6102 | if (iface->cross_connect_in_use) |
| 6103 | continue; |
| 6104 | |
| 6105 | iface->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6106 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6107 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6108 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6109 | } |
| 6110 | } |
| 6111 | |
| 6112 | |
| 6113 | static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) |
| 6114 | { |
| 6115 | struct wpa_supplicant *iface; |
| 6116 | |
| 6117 | for (iface = uplink->global->ifaces; iface; iface = iface->next) { |
| 6118 | if (!iface->cross_connect_enabled) |
| 6119 | continue; |
| 6120 | if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != |
| 6121 | 0) |
| 6122 | continue; |
| 6123 | if (!iface->cross_connect_in_use) |
| 6124 | continue; |
| 6125 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6126 | wpa_msg_global(iface->parent, MSG_INFO, |
| 6127 | P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", |
| 6128 | iface->ifname, iface->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6129 | iface->cross_connect_in_use = 0; |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | |
| 6134 | void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) |
| 6135 | { |
| 6136 | if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || |
| 6137 | wpa_s->current_ssid->mode != WPAS_MODE_INFRA || |
| 6138 | wpa_s->cross_connect_disallowed) |
| 6139 | wpas_p2p_disable_cross_connect(wpa_s); |
| 6140 | else |
| 6141 | wpas_p2p_enable_cross_connect(wpa_s); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6142 | if (!wpa_s->ap_iface && |
| 6143 | eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) |
| 6144 | wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | |
| 6148 | void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) |
| 6149 | { |
| 6150 | wpas_p2p_disable_cross_connect(wpa_s); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6151 | if (!wpa_s->ap_iface && |
| 6152 | !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, |
| 6153 | wpa_s, NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6154 | wpas_p2p_set_group_idle_timeout(wpa_s); |
| 6155 | } |
| 6156 | |
| 6157 | |
| 6158 | static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) |
| 6159 | { |
| 6160 | struct wpa_supplicant *iface; |
| 6161 | |
| 6162 | if (!wpa_s->global->cross_connection) |
| 6163 | return; |
| 6164 | |
| 6165 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6166 | if (iface == wpa_s) |
| 6167 | continue; |
| 6168 | if (iface->drv_flags & |
| 6169 | WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) |
| 6170 | continue; |
| 6171 | if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) |
| 6172 | continue; |
| 6173 | |
| 6174 | wpa_s->cross_connect_enabled = 1; |
| 6175 | os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, |
| 6176 | sizeof(wpa_s->cross_connect_uplink)); |
| 6177 | wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " |
| 6178 | "%s to %s whenever uplink is available", |
| 6179 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
| 6180 | |
| 6181 | if (iface->ap_iface || iface->current_ssid == NULL || |
| 6182 | iface->current_ssid->mode != WPAS_MODE_INFRA || |
| 6183 | iface->cross_connect_disallowed || |
| 6184 | iface->wpa_state != WPA_COMPLETED) |
| 6185 | break; |
| 6186 | |
| 6187 | wpa_s->cross_connect_in_use = 1; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 6188 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6189 | P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", |
| 6190 | wpa_s->ifname, wpa_s->cross_connect_uplink); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6191 | break; |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | |
| 6196 | int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) |
| 6197 | { |
| 6198 | if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && |
| 6199 | !wpa_s->p2p_in_provisioning) |
| 6200 | return 0; /* not P2P client operation */ |
| 6201 | |
| 6202 | wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " |
| 6203 | "session overlap"); |
| 6204 | if (wpa_s != wpa_s->parent) |
| 6205 | wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); |
Dmitry Shmidt | 34af306 | 2013-07-11 10:46:32 -0700 | [diff] [blame] | 6206 | wpas_p2p_group_formation_failed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6207 | return 1; |
| 6208 | } |
| 6209 | |
| 6210 | |
| 6211 | void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) |
| 6212 | { |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6213 | struct p2p_channels chan, cli_chan; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6214 | |
| 6215 | if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) |
| 6216 | return; |
| 6217 | |
| 6218 | os_memset(&chan, 0, sizeof(chan)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6219 | os_memset(&cli_chan, 0, sizeof(cli_chan)); |
| 6220 | if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6221 | wpa_printf(MSG_ERROR, "P2P: Failed to update supported " |
| 6222 | "channel list"); |
| 6223 | return; |
| 6224 | } |
| 6225 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6226 | p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6230 | static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, |
| 6231 | struct wpa_scan_results *scan_res) |
| 6232 | { |
| 6233 | wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); |
| 6234 | } |
| 6235 | |
| 6236 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6237 | int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) |
| 6238 | { |
| 6239 | struct wpa_global *global = wpa_s->global; |
| 6240 | int found = 0; |
| 6241 | const u8 *peer; |
| 6242 | |
| 6243 | if (global->p2p == NULL) |
| 6244 | return -1; |
| 6245 | |
| 6246 | wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); |
| 6247 | |
| 6248 | if (wpa_s->pending_interface_name[0] && |
| 6249 | !is_zero_ether_addr(wpa_s->pending_interface_addr)) |
| 6250 | found = 1; |
| 6251 | |
| 6252 | peer = p2p_get_go_neg_peer(global->p2p); |
| 6253 | if (peer) { |
| 6254 | wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " |
| 6255 | MACSTR, MAC2STR(peer)); |
| 6256 | p2p_unauthorize(global->p2p, peer); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6257 | found = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6258 | } |
| 6259 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 6260 | if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { |
| 6261 | wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); |
| 6262 | wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; |
| 6263 | found = 1; |
| 6264 | } |
| 6265 | |
| 6266 | if (wpa_s->pending_pd_before_join) { |
| 6267 | wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); |
| 6268 | wpa_s->pending_pd_before_join = 0; |
| 6269 | found = 1; |
| 6270 | } |
| 6271 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6272 | wpas_p2p_stop_find(wpa_s); |
| 6273 | |
| 6274 | for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { |
| 6275 | if (wpa_s == global->p2p_group_formation && |
| 6276 | (wpa_s->p2p_in_provisioning || |
| 6277 | wpa_s->parent->pending_interface_type == |
| 6278 | WPA_IF_P2P_CLIENT)) { |
| 6279 | wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " |
| 6280 | "formation found - cancelling", |
| 6281 | wpa_s->ifname); |
| 6282 | found = 1; |
| 6283 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6284 | wpa_s->parent, NULL); |
Jouni Malinen | adddfc4 | 2012-10-03 14:31:41 -0700 | [diff] [blame] | 6285 | if (wpa_s->p2p_in_provisioning) { |
| 6286 | wpas_group_formation_completed(wpa_s, 0); |
| 6287 | break; |
| 6288 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6289 | wpas_p2p_group_delete(wpa_s, |
| 6290 | P2P_GROUP_REMOVAL_REQUESTED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6291 | break; |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | if (!found) { |
| 6296 | wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); |
| 6297 | return -1; |
| 6298 | } |
| 6299 | |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
| 6303 | |
| 6304 | void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) |
| 6305 | { |
| 6306 | if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) |
| 6307 | return; |
| 6308 | |
| 6309 | wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " |
| 6310 | "being available anymore"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6311 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | |
| 6315 | void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, |
| 6316 | int freq_24, int freq_5, int freq_overall) |
| 6317 | { |
| 6318 | struct p2p_data *p2p = wpa_s->global->p2p; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 6319 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6320 | return; |
| 6321 | p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); |
| 6322 | } |
| 6323 | |
| 6324 | |
| 6325 | int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) |
| 6326 | { |
| 6327 | u8 peer[ETH_ALEN]; |
| 6328 | struct p2p_data *p2p = wpa_s->global->p2p; |
| 6329 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 6330 | if (p2p == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6331 | return -1; |
| 6332 | |
| 6333 | if (hwaddr_aton(addr, peer)) |
| 6334 | return -1; |
| 6335 | |
| 6336 | return p2p_unauthorize(p2p, peer); |
| 6337 | } |
| 6338 | |
| 6339 | |
| 6340 | /** |
| 6341 | * wpas_p2p_disconnect - Disconnect from a P2P Group |
| 6342 | * @wpa_s: Pointer to wpa_supplicant data |
| 6343 | * Returns: 0 on success, -1 on failure |
| 6344 | * |
| 6345 | * This can be used to disconnect from a group in which the local end is a P2P |
| 6346 | * Client or to end a P2P Group in case the local end is the Group Owner. If a |
| 6347 | * virtual network interface was created for this group, that interface will be |
| 6348 | * removed. Otherwise, only the configured P2P group network will be removed |
| 6349 | * from the interface. |
| 6350 | */ |
| 6351 | int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) |
| 6352 | { |
| 6353 | |
| 6354 | if (wpa_s == NULL) |
| 6355 | return -1; |
| 6356 | |
Jouni Malinen | 2b89da8 | 2012-08-31 22:04:41 +0300 | [diff] [blame] | 6357 | return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? |
| 6358 | -1 : 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 6359 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6360 | |
| 6361 | |
| 6362 | int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) |
| 6363 | { |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6364 | int ret; |
| 6365 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6366 | if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) |
| 6367 | return 0; |
| 6368 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6369 | ret = p2p_in_progress(wpa_s->global->p2p); |
| 6370 | if (ret == 0) { |
| 6371 | /* |
| 6372 | * Check whether there is an ongoing WPS provisioning step (or |
| 6373 | * other parts of group formation) on another interface since |
| 6374 | * p2p_in_progress() does not report this to avoid issues for |
| 6375 | * scans during such provisioning step. |
| 6376 | */ |
| 6377 | if (wpa_s->global->p2p_group_formation && |
| 6378 | wpa_s->global->p2p_group_formation != wpa_s) { |
| 6379 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " |
| 6380 | "in group formation", |
| 6381 | wpa_s->global->p2p_group_formation->ifname); |
| 6382 | ret = 1; |
| 6383 | } |
| 6384 | } |
| 6385 | |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6386 | if (!ret && wpa_s->global->p2p_go_wait_client.sec) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 6387 | struct os_reltime now; |
| 6388 | os_get_reltime(&now); |
| 6389 | if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, |
| 6390 | P2P_MAX_INITIAL_CONN_WAIT_GO)) { |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6391 | /* Wait for the first client has expired */ |
| 6392 | wpa_s->global->p2p_go_wait_client.sec = 0; |
| 6393 | } else { |
| 6394 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); |
| 6395 | ret = 1; |
| 6396 | } |
| 6397 | } |
| 6398 | |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 6399 | return ret; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | |
| 6403 | void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, |
| 6404 | struct wpa_ssid *ssid) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6405 | { |
| 6406 | if (wpa_s->p2p_in_provisioning && ssid->p2p_group && |
| 6407 | eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6408 | wpa_s->parent, NULL) > 0) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6409 | /** |
| 6410 | * Remove the network by scheduling the group formation |
| 6411 | * timeout to happen immediately. The teardown code |
| 6412 | * needs to be scheduled to run asynch later so that we |
| 6413 | * don't delete data from under ourselves unexpectedly. |
| 6414 | * Calling wpas_p2p_group_formation_timeout directly |
| 6415 | * causes a series of crashes in WPS failure scenarios. |
| 6416 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6417 | wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " |
| 6418 | "P2P group network getting removed"); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6419 | eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, |
| 6420 | wpa_s->parent, NULL); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | |
| 6425 | struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6426 | const u8 *addr, const u8 *ssid, |
| 6427 | size_t ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6428 | { |
| 6429 | struct wpa_ssid *s; |
| 6430 | size_t i; |
| 6431 | |
| 6432 | for (s = wpa_s->conf->ssid; s; s = s->next) { |
| 6433 | if (s->disabled != 2) |
| 6434 | continue; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6435 | if (ssid && |
| 6436 | (ssid_len != s->ssid_len || |
| 6437 | os_memcmp(ssid, s->ssid, ssid_len) != 0)) |
| 6438 | continue; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6439 | if (addr == NULL) { |
| 6440 | if (s->mode == WPAS_MODE_P2P_GO) |
| 6441 | return s; |
| 6442 | continue; |
| 6443 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6444 | if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) |
| 6445 | return s; /* peer is GO in the persistent group */ |
| 6446 | if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) |
| 6447 | continue; |
| 6448 | for (i = 0; i < s->num_p2p_clients; i++) { |
| 6449 | if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, |
| 6450 | addr, ETH_ALEN) == 0) |
| 6451 | return s; /* peer is P2P client in persistent |
| 6452 | * group */ |
| 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | return NULL; |
| 6457 | } |
| 6458 | |
| 6459 | |
| 6460 | void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, |
| 6461 | const u8 *addr) |
| 6462 | { |
Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 6463 | if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, |
| 6464 | wpa_s->parent, NULL) > 0) { |
| 6465 | /* |
| 6466 | * This can happen if WPS provisioning step is not terminated |
| 6467 | * cleanly (e.g., P2P Client does not send WSC_Done). Since the |
| 6468 | * peer was able to connect, there is no need to time out group |
| 6469 | * formation after this, though. In addition, this is used with |
| 6470 | * the initial connection wait on the GO as a separate formation |
| 6471 | * timeout and as such, expected to be hit after the initial WPS |
| 6472 | * provisioning step. |
| 6473 | */ |
| 6474 | wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); |
| 6475 | } |
| 6476 | if (!wpa_s->p2p_go_group_formation_completed) { |
| 6477 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); |
| 6478 | wpa_s->p2p_go_group_formation_completed = 1; |
| 6479 | wpa_s->global->p2p_group_formation = NULL; |
| 6480 | wpa_s->p2p_in_provisioning = 0; |
| 6481 | } |
Dmitry Shmidt | 92c368d | 2013-08-29 12:37:21 -0700 | [diff] [blame] | 6482 | wpa_s->global->p2p_go_wait_client.sec = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 6483 | if (addr == NULL) |
| 6484 | return; |
| 6485 | wpas_p2p_add_persistent_group_client(wpa_s, addr); |
| 6486 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6487 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6488 | |
| 6489 | static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, |
| 6490 | int group_added) |
| 6491 | { |
| 6492 | struct wpa_supplicant *group = wpa_s; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6493 | if (wpa_s->global->p2p_group_formation) |
| 6494 | group = wpa_s->global->p2p_group_formation; |
| 6495 | wpa_s = wpa_s->parent; |
| 6496 | offchannel_send_action_done(wpa_s); |
| 6497 | if (group_added) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6498 | wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6499 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); |
| 6500 | wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, |
| 6501 | wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, |
| 6502 | 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, |
| 6503 | wpa_s->p2p_persistent_id, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6504 | wpa_s->p2p_pd_before_go_neg, |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 6505 | wpa_s->p2p_go_ht40, |
| 6506 | wpa_s->p2p_go_vht); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | |
| 6510 | int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) |
| 6511 | { |
| 6512 | if (!wpa_s->p2p_fallback_to_go_neg || |
| 6513 | wpa_s->p2p_in_provisioning <= 5) |
| 6514 | return 0; |
| 6515 | |
| 6516 | if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) |
| 6517 | return 0; /* peer operating as a GO */ |
| 6518 | |
| 6519 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " |
| 6520 | "fallback to GO Negotiation"); |
| 6521 | wpas_p2p_fallback_to_go_neg(wpa_s, 1); |
| 6522 | |
| 6523 | return 1; |
| 6524 | } |
| 6525 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6526 | |
| 6527 | unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) |
| 6528 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6529 | struct wpa_supplicant *ifs; |
| 6530 | |
| 6531 | if (wpa_s->wpa_state > WPA_SCANNING) { |
| 6532 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " |
| 6533 | "concurrent operation", |
| 6534 | P2P_CONCURRENT_SEARCH_DELAY); |
| 6535 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6536 | } |
| 6537 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 6538 | dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, |
| 6539 | radio_list) { |
| 6540 | if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 6541 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " |
| 6542 | "delay due to concurrent operation on " |
| 6543 | "interface %s", |
| 6544 | P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname); |
| 6545 | return P2P_CONCURRENT_SEARCH_DELAY; |
| 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | return 0; |
| 6550 | } |
| 6551 | |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 6552 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6553 | static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, |
| 6554 | struct wpa_ssid *s, const u8 *addr, |
| 6555 | int iface_addr) |
| 6556 | { |
| 6557 | struct psk_list_entry *psk, *tmp; |
| 6558 | int changed = 0; |
| 6559 | |
| 6560 | dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, |
| 6561 | list) { |
| 6562 | if ((iface_addr && !psk->p2p && |
| 6563 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || |
| 6564 | (!iface_addr && psk->p2p && |
| 6565 | os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { |
| 6566 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6567 | "P2P: Remove persistent group PSK list entry for " |
| 6568 | MACSTR " p2p=%u", |
| 6569 | MAC2STR(psk->addr), psk->p2p); |
| 6570 | dl_list_del(&psk->list); |
| 6571 | os_free(psk); |
| 6572 | changed++; |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | return changed; |
| 6577 | } |
| 6578 | |
| 6579 | |
| 6580 | void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, |
| 6581 | const u8 *p2p_dev_addr, |
| 6582 | const u8 *psk, size_t psk_len) |
| 6583 | { |
| 6584 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6585 | struct wpa_ssid *persistent; |
| 6586 | struct psk_list_entry *p; |
| 6587 | |
| 6588 | if (psk_len != sizeof(p->psk)) |
| 6589 | return; |
| 6590 | |
| 6591 | if (p2p_dev_addr) { |
| 6592 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR |
| 6593 | " p2p_dev_addr=" MACSTR, |
| 6594 | MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); |
| 6595 | if (is_zero_ether_addr(p2p_dev_addr)) |
| 6596 | p2p_dev_addr = NULL; |
| 6597 | } else { |
| 6598 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, |
| 6599 | MAC2STR(mac_addr)); |
| 6600 | } |
| 6601 | |
| 6602 | if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { |
| 6603 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); |
| 6604 | /* To be added to persistent group once created */ |
| 6605 | if (wpa_s->global->add_psk == NULL) { |
| 6606 | wpa_s->global->add_psk = os_zalloc(sizeof(*p)); |
| 6607 | if (wpa_s->global->add_psk == NULL) |
| 6608 | return; |
| 6609 | } |
| 6610 | p = wpa_s->global->add_psk; |
| 6611 | if (p2p_dev_addr) { |
| 6612 | p->p2p = 1; |
| 6613 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6614 | } else { |
| 6615 | p->p2p = 0; |
| 6616 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6617 | } |
| 6618 | os_memcpy(p->psk, psk, psk_len); |
| 6619 | return; |
| 6620 | } |
| 6621 | |
| 6622 | if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { |
| 6623 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); |
| 6624 | return; |
| 6625 | } |
| 6626 | |
| 6627 | persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid, |
| 6628 | ssid->ssid_len); |
| 6629 | if (!persistent) { |
| 6630 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); |
| 6631 | return; |
| 6632 | } |
| 6633 | |
| 6634 | p = os_zalloc(sizeof(*p)); |
| 6635 | if (p == NULL) |
| 6636 | return; |
| 6637 | if (p2p_dev_addr) { |
| 6638 | p->p2p = 1; |
| 6639 | os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); |
| 6640 | } else { |
| 6641 | p->p2p = 0; |
| 6642 | os_memcpy(p->addr, mac_addr, ETH_ALEN); |
| 6643 | } |
| 6644 | os_memcpy(p->psk, psk, psk_len); |
| 6645 | |
| 6646 | if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) { |
| 6647 | struct psk_list_entry *last; |
| 6648 | last = dl_list_last(&persistent->psk_list, |
| 6649 | struct psk_list_entry, list); |
| 6650 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " |
| 6651 | MACSTR " (p2p=%u) to make room for a new one", |
| 6652 | MAC2STR(last->addr), last->p2p); |
| 6653 | dl_list_del(&last->list); |
| 6654 | os_free(last); |
| 6655 | } |
| 6656 | |
| 6657 | wpas_p2p_remove_psk_entry(wpa_s->parent, persistent, |
| 6658 | p2p_dev_addr ? p2p_dev_addr : mac_addr, |
| 6659 | p2p_dev_addr == NULL); |
| 6660 | if (p2p_dev_addr) { |
| 6661 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" |
| 6662 | MACSTR, MAC2STR(p2p_dev_addr)); |
| 6663 | } else { |
| 6664 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, |
| 6665 | MAC2STR(mac_addr)); |
| 6666 | } |
| 6667 | dl_list_add(&persistent->psk_list, &p->list); |
| 6668 | |
| 6669 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 6670 | if (wpa_s->parent->conf->update_config && |
| 6671 | wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) |
| 6672 | wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); |
| 6673 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 6674 | } |
| 6675 | |
| 6676 | |
| 6677 | static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, |
| 6678 | struct wpa_ssid *s, const u8 *addr, |
| 6679 | int iface_addr) |
| 6680 | { |
| 6681 | int res; |
| 6682 | |
| 6683 | res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); |
| 6684 | if (res > 0) { |
| 6685 | #ifndef CONFIG_NO_CONFIG_WRITE |
| 6686 | if (wpa_s->conf->update_config && |
| 6687 | wpa_config_write(wpa_s->confname, wpa_s->conf)) |
| 6688 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 6689 | "P2P: Failed to update configuration"); |
| 6690 | #endif /* CONFIG_NO_CONFIG_WRITE */ |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | |
| 6695 | static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, |
| 6696 | const u8 *peer, int iface_addr) |
| 6697 | { |
| 6698 | struct hostapd_data *hapd; |
| 6699 | struct hostapd_wpa_psk *psk, *prev, *rem; |
| 6700 | struct sta_info *sta; |
| 6701 | |
| 6702 | if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || |
| 6703 | wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) |
| 6704 | return; |
| 6705 | |
| 6706 | /* Remove per-station PSK entry */ |
| 6707 | hapd = wpa_s->ap_iface->bss[0]; |
| 6708 | prev = NULL; |
| 6709 | psk = hapd->conf->ssid.wpa_psk; |
| 6710 | while (psk) { |
| 6711 | if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || |
| 6712 | (!iface_addr && |
| 6713 | os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { |
| 6714 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " |
| 6715 | MACSTR " iface_addr=%d", |
| 6716 | MAC2STR(peer), iface_addr); |
| 6717 | if (prev) |
| 6718 | prev->next = psk->next; |
| 6719 | else |
| 6720 | hapd->conf->ssid.wpa_psk = psk->next; |
| 6721 | rem = psk; |
| 6722 | psk = psk->next; |
| 6723 | os_free(rem); |
| 6724 | } else { |
| 6725 | prev = psk; |
| 6726 | psk = psk->next; |
| 6727 | } |
| 6728 | } |
| 6729 | |
| 6730 | /* Disconnect from group */ |
| 6731 | if (iface_addr) |
| 6732 | sta = ap_get_sta(hapd, peer); |
| 6733 | else |
| 6734 | sta = ap_get_sta_p2p(hapd, peer); |
| 6735 | if (sta) { |
| 6736 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR |
| 6737 | " (iface_addr=%d) from group", |
| 6738 | MAC2STR(peer), iface_addr); |
| 6739 | hostapd_drv_sta_deauth(hapd, sta->addr, |
| 6740 | WLAN_REASON_DEAUTH_LEAVING); |
| 6741 | ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | |
| 6746 | void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, |
| 6747 | int iface_addr) |
| 6748 | { |
| 6749 | struct wpa_ssid *s; |
| 6750 | struct wpa_supplicant *w; |
| 6751 | |
| 6752 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); |
| 6753 | |
| 6754 | /* Remove from any persistent group */ |
| 6755 | for (s = wpa_s->parent->conf->ssid; s; s = s->next) { |
| 6756 | if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) |
| 6757 | continue; |
| 6758 | if (!iface_addr) |
| 6759 | wpas_remove_persistent_peer(wpa_s, s, peer, 0); |
| 6760 | wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr); |
| 6761 | } |
| 6762 | |
| 6763 | /* Remove from any operating group */ |
| 6764 | for (w = wpa_s->global->ifaces; w; w = w->next) |
| 6765 | wpas_p2p_remove_client_go(w, peer, iface_addr); |
| 6766 | } |
| 6767 | |
| 6768 | |
| 6769 | static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) |
| 6770 | { |
| 6771 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6772 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); |
| 6773 | } |
| 6774 | |
| 6775 | |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame] | 6776 | static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) |
| 6777 | { |
| 6778 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 6779 | |
| 6780 | wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); |
| 6781 | wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); |
| 6782 | } |
| 6783 | |
| 6784 | |
| 6785 | int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, |
| 6786 | struct wpa_ssid *ssid) |
| 6787 | { |
| 6788 | struct wpa_supplicant *iface; |
| 6789 | |
| 6790 | for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { |
| 6791 | if (!iface->current_ssid || |
| 6792 | iface->current_ssid->frequency == freq || |
| 6793 | (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && |
| 6794 | !iface->current_ssid->p2p_group)) |
| 6795 | continue; |
| 6796 | |
| 6797 | /* Remove the connection with least priority */ |
| 6798 | if (!wpas_is_p2p_prioritized(iface)) { |
| 6799 | /* STA connection has priority over existing |
| 6800 | * P2P connection, so remove the interface. */ |
| 6801 | wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); |
| 6802 | eloop_register_timeout(0, 0, |
| 6803 | wpas_p2p_group_freq_conflict, |
| 6804 | iface, NULL); |
| 6805 | /* If connection in progress is P2P connection, do not |
| 6806 | * proceed for the connection. */ |
| 6807 | if (wpa_s == iface) |
| 6808 | return -1; |
| 6809 | else |
| 6810 | return 0; |
| 6811 | } else { |
| 6812 | /* P2P connection has priority, disable the STA network |
| 6813 | */ |
| 6814 | wpa_supplicant_disable_network(wpa_s->global->ifaces, |
| 6815 | ssid); |
| 6816 | wpa_msg(wpa_s->global->ifaces, MSG_INFO, |
| 6817 | WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); |
| 6818 | os_memset(wpa_s->global->ifaces->pending_bssid, 0, |
| 6819 | ETH_ALEN); |
| 6820 | /* If P2P connection is in progress, continue |
| 6821 | * connecting...*/ |
| 6822 | if (wpa_s == iface) |
| 6823 | return 0; |
| 6824 | else |
| 6825 | return -1; |
| 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | return 0; |
| 6830 | } |
| 6831 | |
| 6832 | |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 6833 | int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) |
| 6834 | { |
| 6835 | struct wpa_ssid *ssid = wpa_s->current_ssid; |
| 6836 | |
| 6837 | if (ssid == NULL || !ssid->p2p_group) |
| 6838 | return 0; |
| 6839 | |
| 6840 | if (wpa_s->p2p_last_4way_hs_fail && |
| 6841 | wpa_s->p2p_last_4way_hs_fail == ssid) { |
| 6842 | u8 go_dev_addr[ETH_ALEN]; |
| 6843 | struct wpa_ssid *persistent; |
| 6844 | |
| 6845 | if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, |
| 6846 | ssid->ssid, |
| 6847 | ssid->ssid_len) <= 0) { |
| 6848 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); |
| 6849 | goto disconnect; |
| 6850 | } |
| 6851 | |
| 6852 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" |
| 6853 | MACSTR, MAC2STR(go_dev_addr)); |
| 6854 | persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr, |
| 6855 | ssid->ssid, |
| 6856 | ssid->ssid_len); |
| 6857 | if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { |
| 6858 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); |
| 6859 | goto disconnect; |
| 6860 | } |
| 6861 | wpa_msg_global(wpa_s->parent, MSG_INFO, |
| 6862 | P2P_EVENT_PERSISTENT_PSK_FAIL "%d", |
| 6863 | persistent->id); |
| 6864 | disconnect: |
| 6865 | wpa_s->p2p_last_4way_hs_fail = NULL; |
| 6866 | /* |
| 6867 | * Remove the group from a timeout to avoid issues with caller |
| 6868 | * continuing to use the interface if this is on a P2P group |
| 6869 | * interface. |
| 6870 | */ |
| 6871 | eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, |
| 6872 | wpa_s, NULL); |
| 6873 | return 1; |
| 6874 | } |
| 6875 | |
| 6876 | wpa_s->p2p_last_4way_hs_fail = ssid; |
| 6877 | return 0; |
| 6878 | } |